change remote call (init)
This commit is contained in:
		
							parent
							
								
									e9ac918775
								
							
						
					
					
						commit
						5f049cf587
					
				
							
								
								
									
										23
									
								
								handler.php
								
								
								
								
							
							
						
						
									
										23
									
								
								handler.php
								
								
								
								
							| 
						 | 
					@ -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';
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
?>
 | 
					?>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										115
									
								
								main.js
								
								
								
								
							
							
						
						
									
										115
									
								
								main.js
								
								
								
								
							| 
						 | 
					@ -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,32 +1279,48 @@ function migratelegacystorage()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function init()
 | 
					function pushall()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	migratelegacystorage();
 | 
						var index = JSON.parse(localStorage.getItem("index"));
 | 
				
			||||||
	loadsettings();
 | 
						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;
 | 
						return Promise.all(list);
 | 
				
			||||||
	window.onclick = focuseditor;
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
	initsnippets();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function fetch()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return new Promise(function(resolve)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
		if (settings.sync)
 | 
							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();
 | 
									loadstorage();
 | 
				
			||||||
			})
 | 
									editpgpkeys();
 | 
				
			||||||
			.catch(err =>
 | 
									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);
 | 
												settings.password = prompt("Password: ", settings.password);
 | 
				
			||||||
							savesettings();
 | 
												savesettings();
 | 
				
			||||||
| 
						 | 
					@ -1325,17 +1332,28 @@ function init()
 | 
				
			||||||
						}
 | 
											}
 | 
				
			||||||
					});
 | 
										});
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		else
 | 
					 | 
				
			||||||
		{
 | 
					 | 
				
			||||||
			loadstorage();
 | 
					 | 
				
			||||||
			editpgpkeys();
 | 
					 | 
				
			||||||
			showtemporaryinfo("Pgp key empty or invalid. Enter PGP keys and refresh.");
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
		loadstorage();
 | 
								resolve();
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function init()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						migratelegacystorage();
 | 
				
			||||||
 | 
						loadsettings();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						window.onbeforeunload = checksaved;
 | 
				
			||||||
 | 
						window.onclick = focuseditor;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						initsnippets();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						fetch()
 | 
				
			||||||
 | 
						.then( () =>
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							loadstorage();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (issplit())
 | 
							if (issplit())
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
| 
						 | 
					@ -1348,6 +1366,7 @@ function init()
 | 
				
			||||||
				md.focus();
 | 
									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
 | 
										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)
 | 
					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();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,4 @@
 | 
				
			||||||
<?php
 | 
					<?php
 | 
				
			||||||
	$datafile = '../data/data.acs';
 | 
						$datadir = '../data/';
 | 
				
			||||||
	$password = '';
 | 
						$password = '';
 | 
				
			||||||
?>
 | 
					?>
 | 
				
			||||||
		Loading…
	
		Reference in New Issue