change remote call (init)

This commit is contained in:
quenousimporte 2024-02-15 16:02:56 +01:00
parent e9ac918775
commit 5f049cf587
3 changed files with 86 additions and 94 deletions

View File

@ -5,44 +5,37 @@ require 'settings.php';
// check authent
if ($password && (!isset($_POST['password']) || $_POST['password'] != $password))
{
echo '{"error": "authent"}';
echo 'error: authent';
}
else if (isset($_POST['action']))
{
$action = $_POST['action'];
$path = $datadir . $_POST['name'];
switch ($action)
{
case 'fetch':
if (file_exists($datafile))
if (file_exists($path))
{
echo file_get_contents($datafile);
}
else
{
echo '[]';
echo file_get_contents($path);
}
break;
case 'push':
$result = file_put_contents($datafile, $_POST['data']);
$result = file_put_contents($path, $_POST['data']);
if ($result === false)
{
echo '{"error": "could not save ' . $datafile . '"}';
}
else
{
echo '{"result": "ok"}';
echo 'error: could not save ' . $_POST['name'];
}
break;
default:
echo '{"error": "unknown action ' . $action . '"}';
echo 'error: unknown action ' . $action;
break;
}
}
else
{
echo '{"error": "missing action parameter"}';
echo 'error: missing action parameter';
}
?>

115
main.js
View File

@ -186,10 +186,6 @@ var commands = [
hint: "Download all notes (html files in zip archive)",
action: downloadhtmlnotes
},
{
hint: "Download all notes (json file)",
action: downloadnotesjson
},
{
hint: "Insert text in todo",
action: promptinserttodo
@ -1063,11 +1059,6 @@ function promptinserttodo()
}
}
function downloadnotesjson()
{
download("notes-" + timestamp() + ".json", window.localStorage.getItem("data"));
}
function downloadnotewithsubs()
{
var note = withsubs();
@ -1288,32 +1279,48 @@ function migratelegacystorage()
}
}
function init()
function pushall()
{
migratelegacystorage();
loadsettings();
var index = JSON.parse(localStorage.getItem("index"));
var list = [queryremote({action: "push", name: "index", data: localStorage.getItem("index")})];
Object.keys(index).forEach(guid =>
{
list.push(queryremote({action: "push", name: guid, data: localStorage.getItem(guid)}));
});
window.onbeforeunload = checksaved;
window.onclick = focuseditor;
initsnippets();
return Promise.all(list);
}
function fetch()
{
return new Promise(function(resolve)
{
if (settings.sync)
{
if (localStorage.getItem("pgpkeys") && localStorage.getItem("pgpkeys").startsWith("-----BEGIN PGP PUBLIC KEY BLOCK-----"))
var pgpkeys = localStorage.getItem("pgpkeys");
if (!pgpkeys)
{
queryremote({action: "fetch"})
.then(data =>
{
if (data.length)
{
window.localStorage.setItem("data", JSON.stringify(data));
}
loadstorage();
})
.catch(err =>
editpgpkeys();
showtemporaryinfo("Pgp key empty or invalid. Enter PGP keys and refresh.");
}
else
{
if (err == "Authent failed")
queryremote({action: "fetch", name: "index"})
.then(filecontent =>
{
if (!filecontent)
{
return pushall();
}
else
{
// compare and fetch modified and serialize all that
}
}).catch(err =>
{
if (err == "error: authent")
{
settings.password = prompt("Password: ", settings.password);
savesettings();
@ -1325,17 +1332,28 @@ function init()
}
});
}
else
{
loadstorage();
editpgpkeys();
showtemporaryinfo("Pgp key empty or invalid. Enter PGP keys and refresh.");
}
}
else
{
loadstorage();
resolve();
}
});
}
function init()
{
migratelegacystorage();
loadsettings();
window.onbeforeunload = checksaved;
window.onclick = focuseditor;
initsnippets();
fetch()
.then( () =>
{
loadstorage();
if (issplit())
{
@ -1348,6 +1366,7 @@ function init()
md.focus();
}
}
});
}
function queryremote(params)
@ -1379,33 +1398,17 @@ function queryremote(params)
}
else
{
var data = {};
decryptstring(xhr.responseText)
.then(decrypted =>
{
data = JSON.parse(decrypted);
if (data.error)
if (decrypted.startsWith("error: "))
{
if (data.error == "authent")
{
failed("Authent failed");
failed(decrypted);
}
else
{
failed("Remote handler returned an error: " + data.error);
apply(decrypted);
}
}
else
{
apply(data);
}
})
.catch( error =>
{
failed("Could not decrypt or parse data file.");
console.error(error);
});
}
}
@ -2441,11 +2444,7 @@ function shortcutmatches(event, shortcut)
function executecommand(command)
{
if (command.remoteonly && !settings.sync)
{
showtemporaryinfo(command.hint + " is not available in local mode.");
}
else if (command.action)
if (command.action)
{
command.action();
}

View File

@ -1,4 +1,4 @@
<?php
$datafile = '../data/data.acs';
$datadir = '../data/';
$password = '';
?>