Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time delta addition in the sync process to avoid data loss during sync with unaccurate datetime #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
HTML5 Notepad App
by Kaspars Dambis (http://konstruktors.com)
Fork by Benoît HERVIER (http://khertan.net)

Fork addition :
============
* Added support for devices without accurate time set, by adding a time delta to
the synchronization process.
* Created a QML Harmattan client for use on Nokia n950 and Nokia n9

Planned feature :
============
* Multi account support

Installation
============

1. Upload files to your server
2. Rename 'entries' folder to something random and harder to guess
3. Make that folder writable (CHMOD 0777)
4. Edit sync.php and specify your new DATA_DIR (the one you just renamed)
5. In sync.php change username and password to something unique
6. Done!

14 changes: 0 additions & 14 deletions README.txt

This file was deleted.

4 changes: 2 additions & 2 deletions cache.manifest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php header('Content-Type: text/cache-manifest'); ?>
CACHE MANIFEST
# v20121
# v20122

CACHE:
index.html
Expand All @@ -14,4 +14,4 @@

NETWORK:
sync.php
logout.php
logout.php
Binary file removed html5-notepad-app-chrome.zip
Binary file not shown.
5 changes: 3 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@

function sync() {
if (navigator.onLine) {
localStorage.setItem('clientime', (new Date().getTime() + ''));
$.post('sync.php', localStorage, function(data) {
console.log(data);
localStorage.setItem('index', JSON.stringify(data.index));
Expand Down Expand Up @@ -276,7 +277,7 @@
if (title === '')
title = 'Untitled';
if (val['timestamp'] !== 0)
$items.push('<li class="e-' + key + '"><a class="edit" href="#' + key + '">' + title + '</a> <time></time> <a class="delete" href="#' + key + '">-</a></li>');
$items.push('<li class="e-' + key + '"><a class="edit" href="#' + key + '">' + title + '</a> <time>'+ (new Date(parseInt(val['timestamp']))) + ' </time> <a class="delete" href="#' + key + '">-</a></li>');
});
$('#entries ul').hide().html($items.reverse().join('')).show();
}
Expand Down Expand Up @@ -306,4 +307,4 @@
<!-- Could Pad HTML5 App by Kaspars Dambis / http://konstruktors.com/cloud-notepad-html5 -->

</body>
</html>
</html>
Empty file modified js/coreyti-showdown/.gitignore
100755 → 100644
Empty file.
Empty file modified js/coreyti-showdown/license.txt
100755 → 100644
Empty file.
Empty file modified js/coreyti-showdown/showdown.js
100755 → 100644
Empty file.
47 changes: 41 additions & 6 deletions sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,29 @@
error_reporting(E_ERROR);

define('DATA_DIR', 'entries'); // use this to protect files from being publicly viewable
define("USERNAME", 'demo');
define('USERNAME', 'demo');
define('PASSWORD', 'demo');

// Parse incoming data
if (isset($_POST['clientTime'])) {
$time_delta = time() - bigintval($_POST['clientTime']);
unset($_POST['clientTime']);}
else {
$time_delta = 0;}

if (isset($_SERVER['HTTP_AUTHORIZATION']) && preg_match('/Basic\s+(.*)$/i', $_SERVER['HTTP_AUTHORIZATION'], $matches)) {
list($name, $password) = explode(':', base64_decode($matches[1]));
$_SERVER['PHP_AUTH_USER'] = strip_tags($name);
$_SERVER['PHP_AUTH_PW'] = strip_tags($password);
}

//set http auth headers for apache+php-cgi work around if variable gets renamed by apache
if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION']) && preg_match('/Basic\s+(.*)$/i', $_SERVER['REDIRECT_HTTP_AUTHORIZATION'], $matches)) {
list($name, $password) = explode(':', base64_decode($matches[1]));
$_SERVER['PHP_AUTH_USER'] = strip_tags($name);
$_SERVER['PHP_AUTH_PW'] = strip_tags($password);
}

if (($_SERVER['PHP_AUTH_USER'] !== USERNAME) || ($_SERVER['PHP_AUTH_PW'] !== PASSWORD)) {
header('WWW-Authenticate: Basic realm="Cloud Notes"');
header('HTTP/1.0 401 Unauthorized');
Expand All @@ -14,8 +34,6 @@

header('Content-type: application/json; charset=utf-8');

// Parse incoming data

$remote_index = (array)json_decode(stripslashes($_POST['index']), true);
unset($_POST['index']);
$remote_entries = $_POST;
Expand Down Expand Up @@ -46,23 +64,27 @@
delete_entry($id);
unset($remote_index[$id]);
unset($remote_entries[$id]);
} elseif ($item['timestamp'] > $local_index[$id]['timestamp']) {
} elseif (strval(bigintval($item['timestamp'])) - $time_delta > $local_index[$id]['timestamp']) {
// Remote entry is newer, replace it and don't send it back
$local_index[$id] = $item;
$local_index[$id]['timestamp'] = strval(bigintval($local_index[$id]['timestamp']) - $time_delta);
store_entry($id, $_POST[$id]);
unset($remote_entries[$id]);
unset($local_entries[$id]);
} elseif ($item['timestamp'] == $local_index[$id]['timestamp']) {
} elseif (strval(bigintval($item['timestamp'])) - $time_delta == $local_index[$id]['timestamp']) {
// Local entry is already the latest, don't send it back
unset($remote_entries[$id]);
unset($local_entries[$id]);
} else {
// Local entry is newer, send it back
$remote_index[$id] = $local_index[$id];
$remote_index[$id]['timestamp'] = strval(bigintval($remote_index[$id]['timestamp']) + $time_delta);
$remote_entries[$id] = get_entry($id);

}
} else {
$local_index[$id] = $remote_index[$id];
$local_index[$id]['timestamp'] = strval(bigintval($local_index[$id]['timestamp']) - $time_delta);
store_entry($id, $_POST[$id]);
unset($remote_entries[$id]);
}
Expand All @@ -73,6 +95,7 @@
if ($local_index[$id]['timestamp'] !== 0) {
$remote_entries[$id] = get_entry($id);
$remote_index[$id] = $local_index[$id];
$remote_index[$id]['timestamp'] = strval(bigintval($remote_index[$id]['timestamp']) + $time_delta);
} else {
unset($remote_entries[$id]);
unset($remote_index[$id]);
Expand All @@ -87,6 +110,18 @@

// Helpers

function bigintval($value) {
$value = trim($value);
if (ctype_digit($value)) {
return $value;
}
$value = preg_replace("/[^0-9](.*)$/", '', $value);
if (ctype_digit($value)) {
return $value;
}
return 0;
}

function get_entry($id) {
$file = DATA_DIR . '/' . $id;
if (file_exists($file)) {
Expand Down Expand Up @@ -136,4 +171,4 @@ function get_entries() {
return json_encode($entries);
}

?>
?>