-
Notifications
You must be signed in to change notification settings - Fork 36
/
sanity.php
392 lines (326 loc) · 11.6 KB
/
sanity.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
/******************************************************************************
* SiteBar 3 - The Bookmark Server for Personal and Team Use. *
* Copyright (C) 2005-2008 Ondrej Brablc <http://brablc.com/mailto?o> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************/
require_once('./inc/usermanager.inc.php');
require_once('./inc/tree.inc.php');
require_once('./inc/faviconcache.inc.php');
header("Content-type: text/plain; charset=UTF-8");
class SB_SanityCheck
{
var $um;
var $tree;
var $db;
var $checked = array();
function __construct()
{
$this->um =& SB_UserManager::staticInstance();
$this->tree =& SB_Tree::staticInstance();
$this->db =& SB_Database::staticInstance();
if (!$this->um->isLogged() || !$this->um->isAdmin())
{
die ("Access denied!");
}
}
function run()
{
$doall = isset($_GET['do_all']);
if ($doall || isset($_GET['do_deadusers']))
{
$this->deadusers();
}
if ($doall || isset($_GET['do_orphans']))
{
$this->orphans();
}
if ($doall || isset($_GET['do_aclorphans']))
{
$this->aclorphans();
}
if ($doall || isset($_GET['do_icons']))
{
$this->convertBinaryIcons();
}
if (isset($_GET['do_fix']))
{
$this->fix();
}
if (isset($_GET['do_fixgroups']))
{
$this->fixgroups();
}
if (isset($_GET['do_fixeveryone']))
{
$this->fixeveryone();
}
echo "Done.\r";
}
function strip_slashes()
{
echo "Strip tripple slashes in link names and descriptions ...\r";
$rset = $this->db->select('lid, name, comment', 'sitebar_link');
while (($rec = $this->db->fetchRecord($rset)))
{
$update = array();
$update['name'] = str_replace("\\\\","\\", $rec['name']);
$update['comment'] = str_replace("\\\\","\\", $rec['comment']);
if ($update['name'] != $rec['name'] || $update['comment'] != $rec['comment'])
{
$this->db->update( 'sitebar_link', $update, array( 'lid' => $rec['lid']), array(1062));
}
}
echo "Strip tripple slashes in node names and descriptions ...\r";
$rset = $this->db->select('nid, name, comment', 'sitebar_node');
while (($rec = $this->db->fetchRecord($rset)))
{
$update = array();
$update['name'] = str_replace("\\\\","\\", $rec['name']);
$update['comment'] = str_replace("\\\\","\\", $rec['comment']);
if ($update['name'] != $rec['name'] || $update['comment'] != $rec['comment'])
{
$this->db->update( 'sitebar_node', $update, array( 'nid' => $rec['nid']), array(1062));
}
}
}
function _fix_zeroes(&$value)
{
$value = str_replace("\0",'\0', $value);
}
function _fix_slashes(&$value)
{
$value = str_replace("\\\\","\\", $value);
}
function _fix_apos(&$value)
{
$value = str_replace("'","'", $value);
}
function _fix_nbsp(&$value)
{
$value = str_replace(" "," ", $value);
}
function _fix_entities(&$value)
{
if ( preg_match('/[&<>\'"]/',$value) )
{
$entity = array('&','<','>',''','"');
$char = array('&','<','>','\'','"');
$value = str_replace($entity, $char, $value);
}
}
function fix()
{
echo "Walk all links in database ...\r";
$rset = $this->db->select('lid, name, comment, url', 'sitebar_link');
while (($rec = $this->db->fetchRecord($rset)))
{
$update = array();
foreach (array('name','comment','url') as $column)
{
$decoded = $rec[$column];
$this->_fix_zeroes($decoded);
$this->_fix_slashes($decoded);
$this->_fix_entities($decoded);
$this->_fix_apos($decoded);
$this->_fix_nbsp($decoded);
if ($decoded != $rec[$column])
{
echo '- '.$decoded."\r";
$update[$column] = $decoded;
}
}
if (count($update))
{
$this->db->update( 'sitebar_link', $update, array( 'lid' => $rec['lid']), array(1062));
}
}
echo "Walk all nodes in database ...\r";
$rset = $this->db->select('nid, name, comment', 'sitebar_node');
while (($rec = $this->db->fetchRecord($rset)))
{
$update = array();
foreach (array('name','comment') as $column)
{
$decoded = $rec[$column];
$this->_fix_zeroes($decoded);
$this->_fix_slashes($decoded);
$this->_fix_entities($decoded);
$this->_fix_apos($decoded);
$this->_fix_nbsp($decoded);
if ($decoded != $rec[$column])
{
echo '- '.$decoded."\r";
$update[$column] = $decoded;
}
}
if (count($update))
{
$this->db->update( 'sitebar_node', $update, array( 'nid' => $rec['nid']), array(1062));
}
}
}
function orphans()
{
echo "Fetch ids of all links in database ...\r";
$rset = $this->db->select('DISTINCT nid', 'sitebar_link');
echo "Traverse recursive all nodes and check\r";
echo " - whether the parent node exists,\r";
echo " - whether the node without parent is the root,\r";
echo " - whether there is a user for the node.\r";
while (($rec = $this->db->fetchRecord($rset)))
{
$this->checkNode($rec['nid']);
}
}
function aclorphans()
{
echo "Fetch ids of all acl nodes in database ...\r";
$rset = $this->db->select('DISTINCT nid', 'sitebar_acl');
echo "Traverse recursive all nodes and check\r";
echo " - whether the parent node exists,\r";
echo " - whether the node without parent is the root,\r";
echo " - whether there is a user for the node.\r";
while (($rec = $this->db->fetchRecord($rset)))
{
$this->checkNodeACL($rec['nid']);
}
}
function deadusers()
{
echo "Delete users with no visits ...\r";
$sql =<<<_SQL
SELECT u.uid, u.email
FROM `sitebar_user` u
WHERE u.uid > 2 AND visits = 0;
_SQL;
$rset = $this->db->raw($sql);
while (($rec = $this->db->fetchRecord($rset)))
{
echo "Deleting user #${rec['uid']} ${rec['email']}\r";
$this->um->removeUser($rec['uid']);
}
echo "\n";
}
function fixgroups()
{
$default_groups = $this->um->getParamArray('config','default_groups');
echo "For each user other than built in ...\r";
$groups = implode("','",$default_groups);
$groupCount = count($default_groups);
$sql =<<<_SQL
SELECT u.uid, SUM( IF( g.name IN ('$groups'), 1, 0) ) DGROUPS, COUNT(g.gid) AGROUPS
FROM `sitebar_user` u LEFT OUTER JOIN `sitebar_group` g ON g.uid = u.uid
WHERE u.uid > 2
GROUP BY u.uid
HAVING DGROUPS<$groupCount AND AGROUPS<>0
_SQL;
echo "Fixing users: ";
$rset = $this->db->raw($sql);
while (($rec = $this->db->fetchRecord($rset)))
{
echo $rec['uid'] . " ";
foreach ($default_groups as $group)
{
$group = array('uid'=>$rec['uid'], 'name'=>$group);
$this->um->addGroup($group);
}
}
echo "\n";
}
function fixeveryone()
{
echo "Delete membership of other that built in users ...\r";
$this->db->raw("DELETE FROM sitebar_member WHERE uid>2 AND gid=2");
echo "Delete acl of other than built in roots ...\r";
$this->db->raw("DELETE FROM sitebar_acl WHERE gid=2 and nid>2");
}
function convertBinaryIcons()
{
echo "Convert binary icons ...\r";
$rset = $this->db->select('*', 'sitebar_link', "favicon LIKE 'data:image%'");
$fc = & SB_FaviconCache::staticInstance();
$converted = 0;
while (($rec = $this->db->fetchRecord($rset)))
{
if (preg_match("/^data:image\/(.*?);base64,(.*)$/", $rec['favicon'], $reg))
{
$update = array
(
'favicon'=>$fc->saveFaviconBase64($reg[2]),
);
$this->tree->updateLink($rec['lid'], $update);
$converted++;
}
}
echo 'Converted '.$converted." favicons.\r";
}
function checkNode($nid)
{
if (isset($this->checked[$nid]))
{
return true;
}
$this->checked[$nid]++;
$rset = $this->db->select('*', 'sitebar_node', array('nid'=>$nid));
$rec = $this->db->fetchRecord($rset);
if (is_array($rec))
{
if ($rec['nid_parent']==0)
{
echo "Root: " . $rec['name'] . "\r";
$rset = $this->db->select('*', 'sitebar_root', array('nid'=>$nid));
$root = $this->db->fetchRecord($rset);
if (is_array($root))
{
$rset = $this->db->select('*', 'sitebar_user', array('uid'=>$root['uid']));
$user = $this->db->fetchRecord($rset);
if (is_array($user))
{
echo "User: " . $user['name'] . '[' . $user['email'] . ']\r';
}
else
{
echo "!!! Orfan\r";
}
}
else
{
echo "!!! Invisible\r";
}
return true;
}
if ($rec['nid'] == $rec['nid_parent'])
{
echo "!!! Recursive parent!\r";
}
return $this->checkNode($rec['nid_parent']);
}
echo "!!! Missing parent\r";
return false;
}
function checkNodeACL($nid)
{
$rset = $this->db->select('*', 'sitebar_node', array('nid'=>$nid));
$rec = $this->db->fetchRecord($rset);
if (!is_array($rec))
{
echo "!!! Missing node: " . $nid . "\r";
}
}
}
$sc = new SB_SanityCheck();
$sc->run();
?>