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 // check authent
if ($password && (!isset($_POST['password']) || $_POST['password'] != $password)) if ($password && (!isset($_POST['password']) || $_POST['password'] != $password))
{ {
echo '{"error": "authent"}'; echo 'error: authent';
} }
else if (isset($_POST['action'])) else if (isset($_POST['action']))
{ {
$action = $_POST['action']; $action = $_POST['action'];
$path = $datadir . $_POST['name'];
switch ($action) switch ($action)
{ {
case 'fetch': case 'fetch':
if (file_exists($datafile)) if (file_exists($path))
{ {
echo file_get_contents($datafile); echo file_get_contents($path);
}
else
{
echo '[]';
} }
break; break;
case 'push': case 'push':
$result = file_put_contents($datafile, $_POST['data']); $result = file_put_contents($path, $_POST['data']);
if ($result === false) if ($result === false)
{ {
echo '{"error": "could not save ' . $datafile . '"}'; echo 'error: could not save ' . $_POST['name'];
}
else
{
echo '{"result": "ok"}';
} }
break; break;
default: default:
echo '{"error": "unknown action ' . $action . '"}'; echo 'error: unknown action ' . $action;
break; break;
} }
} }
else 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)", hint: "Download all notes (html files in zip archive)",
action: downloadhtmlnotes action: downloadhtmlnotes
}, },
{
hint: "Download all notes (json file)",
action: downloadnotesjson
},
{ {
hint: "Insert text in todo", hint: "Insert text in todo",
action: promptinserttodo action: promptinserttodo
@ -1063,11 +1059,6 @@ function promptinserttodo()
} }
} }
function downloadnotesjson()
{
download("notes-" + timestamp() + ".json", window.localStorage.getItem("data"));
}
function downloadnotewithsubs() function downloadnotewithsubs()
{ {
var note = withsubs(); 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() function init()
{ {
migratelegacystorage(); migratelegacystorage();
@ -1298,56 +1350,23 @@ function init()
initsnippets(); initsnippets();
if (settings.sync) fetch()
{ .then( () =>
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
{ {
loadstorage(); loadstorage();
}
if (issplit()) if (issplit())
{
if (settings.defaultpreviewinsplit && name == "right")
{ {
togglepreview(); if (settings.defaultpreviewinsplit && name == "right")
{
togglepreview();
}
else if (name == "left")
{
md.focus();
}
} }
else if (name == "left") });
{
md.focus();
}
}
} }
function queryremote(params) function queryremote(params)
@ -1379,33 +1398,17 @@ function queryremote(params)
} }
else else
{ {
var data = {};
decryptstring(xhr.responseText) decryptstring(xhr.responseText)
.then(decrypted => .then(decrypted =>
{ {
data = JSON.parse(decrypted); if (decrypted.startsWith("error: "))
if (data.error)
{ {
if (data.error == "authent") failed(decrypted);
{
failed("Authent failed");
}
else
{
failed("Remote handler returned an error: " + data.error);
}
} }
else 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) function executecommand(command)
{ {
if (command.remoteonly && !settings.sync) if (command.action)
{
showtemporaryinfo(command.hint + " is not available in local mode.");
}
else if (command.action)
{ {
command.action(); command.action();
} }

View File

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