forked from kasparsd/HTML5-Notepad-with-Sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.php
144 lines (117 loc) · 3.58 KB
/
sync.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
error_reporting(E_ERROR);
// if config file exists then load it
if(file_exists('config.php')) {
require_once('config.php');
}
define('DATA_DIR', isset($data_dir) ? $data_dir : 'entries'); // use this to protect files from being publicly viewable
if (isset($username) && isset($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');
exit;
}
}
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;
// Process
$local_index = get_entry('_index');
$diff_index = array_diff_key($local_index, $remote_index);
$debug = array();
$debug['new entries'] = $remote_entries;
$debug['old entries'] = get_entries();
foreach ($remote_index as $id => $item) {
if (array_key_exists($id, $local_index)) {
if ($item['timestamp'] == 0) {
// Remote entry has been deleted, remove the local too
$local_index[$id] = $item;
delete_entry($id);
unset($remote_index[$id]);
unset($remote_entries[$id]);
} elseif ($local_index[$id]['timestamp'] == 0) {
// Local entry has been deleted, remove local entry too
$remote_index[$id] = $local_index[$id];
delete_entry($id);
unset($remote_index[$id]);
unset($remote_entries[$id]);
} elseif ($item['timestamp'] > $local_index[$id]['timestamp']) {
// Remote entry is newer, replace it and don't send it back
$local_index[$id] = $item;
store_entry($id, $_POST[$id]);
unset($remote_entries[$id]);
unset($local_entries[$id]);
} elseif ($item['timestamp'] == $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_entries[$id] = get_entry($id);
}
} else {
$local_index[$id] = $remote_index[$id];
store_entry($id, $_POST[$id]);
unset($remote_entries[$id]);
}
}
foreach ($diff_index as $id => $data) {
//echo $data['timestamp'];
if ($local_index[$id]['timestamp'] !== 0) {
$remote_entries[$id] = get_entry($id);
$remote_index[$id] = $local_index[$id];
} else {
unset($remote_entries[$id]);
unset($remote_index[$id]);
}
}
store_entry('_index', $local_index);
$return = array('index' => $remote_index, 'entries' => $remote_entries, 'debug' => $debug);
echo json_encode($return);
// Helpers
function get_entry($id) {
$file = DATA_DIR . '/' . $id;
if (file_exists($file)) {
if ($le = json_decode(file_get_contents($file), true)) {
return $le; // return addslashes($le);
}
}
return array();
}
function store_entry($id, $data) {
$file = DATA_DIR . '/' . $id;
if (empty($data))
return;
if (is_array($data))
$data = json_encode($data);
// $data = stripslashes($data);
file_put_contents($file, $data)
or die_json("Can't read the file!");
}
function delete_entry($id) {
$file = DATA_DIR . '/' . $id;
if (file_exists($file))
unlink($file);
}
function die_json($message) {
echo json_encode(array('error' => $message));
exit;
}
function get_entries() {
$entries = array();
if ($handle = opendir(DATA_DIR)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$entry = json_decode(file_get_contents(DATA_DIR . '/' . $file), true);
$entries[$file] = $entry;
}
}
closedir($handle);
}
if (!empty($entries))
return json_encode($entries);
}
?>