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';
}
?>

155
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,6 +1279,67 @@ function migratelegacystorage()
}
}
function pushall()
{
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)}));
});
return Promise.all(list);
}
function fetch()
{
return new Promise(function(resolve)
{
if (settings.sync)
{
var pgpkeys = localStorage.getItem("pgpkeys");
if (!pgpkeys)
{
loadstorage();
editpgpkeys();
showtemporaryinfo("Pgp key empty or invalid. Enter PGP keys and refresh.");
}
else
{
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();
init();
}
else
{
showtemporaryinfo(err);
}
});
}
}
else
{
resolve();
}
});
}
function init()
{
migratelegacystorage();
@ -1298,56 +1350,23 @@ function init()
initsnippets();
if (settings.sync)
{
if (localStorage.getItem("pgpkeys") && localStorage.getItem("pgpkeys").startsWith("-----BEGIN PGP PUBLIC KEY BLOCK-----"))
{
queryremote({action: "fetch"})
.then(data =>
{
if (data.length)
{
window.localStorage.setItem("data", JSON.stringify(data));
}
loadstorage();
})
.catch(err =>
{
if (err == "Authent failed")
{
settings.password = prompt("Password: ", settings.password);
savesettings();
init();
}
else
{
showtemporaryinfo(err);
}
});
}
else
{
loadstorage();
editpgpkeys();
showtemporaryinfo("Pgp key empty or invalid. Enter PGP keys and refresh.");
}
}
else
fetch()
.then( () =>
{
loadstorage();
}
if (issplit())
{
if (settings.defaultpreviewinsplit && name == "right")
if (issplit())
{
togglepreview();
if (settings.defaultpreviewinsplit && name == "right")
{
togglepreview();
}
else if (name == "left")
{
md.focus();
}
}
else if (name == "left")
{
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");
}
else
{
failed("Remote handler returned an error: " + data.error);
}
failed(decrypted);
}
else
{
apply(data);
apply(decrypted);
}
})
.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 = '';
?>