-
Notifications
You must be signed in to change notification settings - Fork 4
/
manager.php
162 lines (128 loc) · 3.7 KB
/
manager.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
// Simple Bookmark Manager, https://github.com/invokr/Simple-Bookmark-Manager
///
/// Load bookmark storage
///
libxml_use_internal_errors(true);
$storage = array("id" => 1, "links" => array()); // Start at 1
if (file_exists("./data/bookmarks.dat")) {
$storage = unserialize(file_get_contents("./data/bookmarks.dat"));
}
///
/// Actions
///
$actions = array();
$actions['add'] = function() {
global $storage;
$id = id();
$storage["links"][$id] = array(
"link" => pparam('link', ''),
"description" => pparam('description', ''),
"tags" => explode(", ", pparam('tags', "")),
"id" => $id
);
// Fills title and favicon
fill_data($id);
if ($storage["links"][$id]['favicon'] != "") {
file_put_contents("cache/$id.ico", file_get_contents($storage["links"][$id]['favicon']));
}
// Sort, @todo: check performance for many bookmarks
usort($storage["links"], function($a, $b) {
return $a['title'] > $b['title'];
});
// Return result
return_result("0", $id);
};
$actions['modify'] = function() {
global $storage;
$id = pparam('id', -1);
if ($id === -1 || !array_key_exists($id, $storage['links'])) {
return_result("4", "Unkown link ID specified");
} else {
$storage['links'][$id]['description'] = pparam('description', '');
$storage['links'][$id]['tags'] = explode(", ", pparam('tags', ""));
return_result("0", "");
}
};
$actions['delete'] = function() {
global $storage;
$id = pparam('id', -1);
if ($id === -1 || !array_key_exists($id, $storage['links'])) {
return_result("4", "Unkown link ID specified");
} else {
unset($storage['links'][$id]);
return_result("0", "");
}
};
$actions['list'] = function() {
global $storage;
$id = pparam('id', -1);
if ($id === -1) {
return_result("0", $storage['links']);
} elseif (array_key_exists($id, $storage['links'])) {
return_result("0", $storage['links'][$id]);
} else {
return_result("3", "Unkown link ID requested");
}
};
///
/// Helper functions
///
function return_result($code, $message) {
echo json_encode(array("status" => $code, "data" => $message));
}
function pparam($key, $default) {
return array_key_exists($key, $_POST) ? $_POST[$key] : $default;
}
function id() {
global $storage;
return $storage["id"]++;
}
function fill_data($id) {
global $storage;
$doc = new DOMDocument();
$doc->strictErrorChecking = FALSE;
$doc->loadHTML(utf8_encode(file_get_contents($storage["links"][$id]["link"])));
$xml = simplexml_import_dom($doc);
$fav = "";
$t_fav1 = $xml->xpath('//link[@rel="icon"]');
$t_fav2 = $xml->xpath('//link[@rel="shortcut icon"]');
if (count($t_fav1)) {
$fav = $t_fav1[0]['href'][0]->__toString();
} else if (count($t_fav2)) {
$fav = $t_fav2[0]['href'][0]->__toString();
}
if ($fav) {
if (substr($fav, 0, 4) != "http") {
if (substr($fav, 0, 2) == "//") {
$fav = "http:".$fav;
} else {
$data = parse_url($storage["links"][$id]["link"]);
if ($fav[0] == '/') {
// absolute path
$fav = $data["scheme"]."://".$data["host"].$fav;
} else {
// relative path
$fav = $data["scheme"]."://".$data["host"]."?".$data["argument"].$fav;
}
}
}
}
$storage["links"][$id]["favicon"] = $fav;
$storage["links"][$id]["title"] = (string)$xml->xpath('//title')[0]; // title
}
///
/// Execute action and return result
///
if (!array_key_exists('action', $_GET)) {
return_result("1", "No action specified");
} elseif (!array_key_exists($_GET['action'], $actions)) {
return_result("2", "No such action");
} else {
$actions[$_GET['action']]();
}
///
/// Save bookmark storage
///
file_put_contents("./data/bookmarks.dat", serialize($storage));
?>