-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBookmarkManager.php
More file actions
260 lines (204 loc) · 6.99 KB
/
Copy pathBookmarkManager.php
File metadata and controls
260 lines (204 loc) · 6.99 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
namespace B;
class BookmarkManager
{
/** @var Array<?string> */
protected array $config;
protected DB $db;
protected string $user;
protected string $baseDir;
public string $requestUri;
public string $subPage = '';
public function __construct(string $requestUri = '/')
{
$getEnv = function (string $key): ?string {
$value = getenv($key);
return $value !== false ? $value : null;
};
$this->config = [
'baseDir' => $getEnv('BASE_DIR') ?? '/app/db/',
'baseUri' => $getEnv('BASE_URI') ?? '/',
'infiniteScrolling' => $getEnv('INFINITE_SCROLLING') ?? null,
];
$this->baseDir = $this->config['baseDir'];
if (!is_dir($this->baseDir)) {
throw new \Exception('invalid baseDir: ' . $this->baseDir);
}
if (!is_writeable($this->baseDir)) {
throw new \Exception('baseDir not writeable: ' . $this->baseDir);
}
$baseUri = $this->config['baseUri'];
if (strpos($requestUri, $baseUri) !== 0) {
throw new \Exception('invalid baseuri');
}
/* cut baseUri from request uri */
$this->requestUri = substr($requestUri, strlen($baseUri));
/* cut query string from request uri */
if (($p = strpos($this->requestUri, '?')) !== false) {
$this->requestUri = substr($this->requestUri, 0, $p);
}
$uriParts = explode('/', trim($this->requestUri, '/'));
if (count($uriParts) === 2) {
$this->subPage = $uriParts[1];
}
$user = $uriParts[0];
if (!preg_match('@^[a-z0-9]+$@i', $user)) {
throw new \Exception('Page not found', 404);
}
if (!is_dir($this->baseDir.$user)) {
throw new \Exception('No such user: '.$user, 404);
}
if (!is_writeable($this->baseDir.$user)) {
throw new \Exception('User dir not writeable');
}
$this->user = $user;
$this->db = new DB($this->baseDir.$user.'/b.db');
}
public function getConfig(string $key): ?string
{
if (isset($this->config[$key])) {
return $this->config[$key];
}
return null;
}
public function getDB(): DB
{
return $this->db;
}
public function handleAjaxRequest(array $postData): void
{
if (empty($postData['action'])) {
return;
}
$action = (string)$postData['action'];
$error = true;
$result = [];
if (!empty($postData['id'])) {
$id = (int)$postData['id'];
$result['id'] = $id;
} else {
$id = 0;
}
try {
switch ($action) {
case 'add':
$result['url'] = (string)$postData['url'];
$result['force'] = $postData['force'] ? true : false;
list($url, $desc) = array_pad(explode(' ', (string)$postData['url'], 2), 2, '');
$this->addBookmark($url, $desc, !empty($postData['force']));
$error = false;
break;
case 'delete':
if ($id) {
$this->db->deleteEntry($id);
$error = false;
}
break;
case 'settitle':
if ($id && !empty($postData['title'])) {
$this->db->setTitle($id, (string)$postData['title']);
$error = false;
$result['title'] = self::formatDesc((string)$postData['title'], false);
$result['rawTitle'] = (string)$postData['title'];
$result['tags'] = self::formatTags((string)$postData['title']);
}
break;
case 'setlink':
if ($id && !empty($postData['link'])) {
$this->db->setLink($id, (string)$postData['link']);
$error = false;
$result['link'] = (string)$postData['link'];
}
break;
default:
return;
}
} catch (\Exception $e) {
$result['message'] = $e->getMessage();
}
if ($error) {
$result['result'] = false;
} else {
$result['result'] = true;
}
header('Content-Type: application/json; charset=utf-8');
echo json_encode($result, JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE);
exit();
}
public function addBookmark(string $url, string $appendDesc = '', bool $force = false): bool
{
if ($this->db->exists($url)) {
throw new \Exception('Bookmark already exists.');
}
try {
$body = $this->fetch($url);
} catch (\Exception $e) {
if (!$force) {
throw $e;
}
}
if (!empty($body)) {
$desc = $this->extractTitle($body).' '.$appendDesc;
} else {
$desc = $url;
}
return $this->db->add($desc, $url);
}
private function fetch(string $url): string
{
if (($h = curl_init($url)) === false) {
throw new \Exception('could not init curl');
}
curl_setopt($h, CURLOPT_RETURNTRANSFER, true);
curl_setopt($h, CURLOPT_FOLLOWLOCATION, true);
if (($ret = curl_exec($h)) === false) {
throw new \Exception('could not fetch');
}
return (string)$ret;
}
public static function extractTitle(string $body): string
{
if (!preg_match('@<title>([^<]+)@', $body, $m)) {
return '(unknown title)';
}
$ret = $m[1];
$enc = mb_detect_encoding($ret, 'UTF-8,ISO-8859-1', true);
if ($enc !== 'UTF-8') {
$ret = mb_convert_encoding($ret, 'UTF-8', $enc);
}
$ret = trim(html_entity_decode($ret, ENT_QUOTES, 'UTF-8'));
return $ret;
}
public static function formatDesc(string $desc, bool $withTags = true): string
{
$desc = htmlspecialchars($desc);
if (preg_match_all('@#[a-z0-9-_]+@i', $desc, $m)) {
$matches = $m[0];
foreach ($matches as $tag) {
if ($withTags) {
$replaceWith = self::formatTagLink($tag);
} else {
$replaceWith = '';
}
$desc = str_replace($tag, $replaceWith, $desc);
}
}
return trim($desc);
}
public static function formatTags(string $desc): string
{
$tags = '';
$desc = htmlspecialchars($desc);
if (preg_match_all('@#[a-z0-9-_]+@i', $desc, $m)) {
$matches = $m[0];
foreach ($matches as $tag) {
$tags .= self::formatTagLink($tag).' ';
}
}
return $tags;
}
public static function formatTagLink(string $tag): string
{
return '<a class="hash" href="?filter='.rawurlencode($tag).'">'.$tag.'</a>';
}
}