Code Duplication    Length = 433-433 lines in 2 locations

class/adminclass.php 1 location

@@ 23-455 (lines=433) @@
20
 * @author       Michael Albertsen (http://culex.dk) <[email protected]>
21
 */
22
23
class SmallworldAdmin
24
{
25
    /**
26
     * Get oldest message in Db
27
     * @returns time
28
     */
29
    public function oldestMsg()
30
    {
31
        global $xoopsDB;
32
        $date = 0;
33
        $sql     = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_messages') . ' ORDER BY created LIMIT 1';
34
        $result = $xoopsDB->queryF($sql);
35
        $counter = $xoopsDB->getRowsNum($result);
36
        if ($counter >= 1) {
37
            while ($sqlfetch = $xoopsDB->fetchArray($result)) {
38
                $date = $sqlfetch['created'];
39
            }
40
        }
41
42
        return $date;
43
    }
44
45
    /**
46
     * Get average messages sent per day
47
     * @param int $totaldays
48
     * @return int|string
49
     */
50
    public function AvgMsgDay($totaldays)
51
    {
52
        global $xoopsDB;
53
        $sql = 'SELECT count( * ) / ' . $totaldays . ' AS averg FROM ' . $xoopsDB->prefix('smallworld_messages') . '';
54
        $result = $xoopsDB->queryF($sql);
55
        while ($sqlfetch = $xoopsDB->fetchArray($result)) {
56
            $avg = number_format($sqlfetch['averg'], 2, '.', ',');
57
        }
58
59
        return $avg;
60
    }
61
62
    /**
63
     * total users using smallworld
64
     * @return int
65
     */
66
    public function TotalUsers()
67
    {
68
        global $xoopsDB;
69
        $sql = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_user') . '';
70
        $result = $xoopsDB->queryF($sql);
71
        $counter = $xoopsDB->getRowsNum($result);
72
        if ($counter < 1) {
73
            $sum = 0;
74
        } else {
75
            $i = 0;
76
            while ($myrow = $xoopsDB->fetchArray($result)) {
77
                $user[$i]['username'] = $myrow['username'];
78
                ++$i;
79
            }
80
            $all = $this->flatten($user);
81
            $sum = count(array_unique($all));
82
            $unique = array_unique($all);
83
        }
84
85
        return $sum;
86
    }
87
88
    /**
89
     * Get version of module
90
     * @returns string
91
     */
92
    public function ModuleInstallVersion()
93
    {
94
        global $xoopsModule;
95
        $version = round($xoopsModule->getVar('version') / 100, 2);
96
97
        return $version;
98
    }
99
100
    /**
101
     * Get date when Module was installed
102
     * @return string|time
103
     */
104
    public function ModuleInstallDate()
105
    {
106
        global $xoopsModule;
107
        $date = formatTimestamp($xoopsModule->getVar('last_update'), 'm');
108
109
        return $date;
110
    }
111
112
    /**
113
     * Count total days represented in db
114
     * @return float|int|time
115
     */
116
    public function CountDays()
117
    {
118
        global $xoopsDB, $xoopsModule;
119
        $date = $this->oldestMsg();
120
        $now = time();
121
        $diff = ($now - $date) / (60 * 60 * 24);
122
123
        return $diff;
124
    }
125
126
    /**
127
     * find user with most posted messages
128
     * @returns array
129
     */
130
    public function mostactiveusers_allround()
131
    {
132
        global $xoopsDB, $xoopsUser;
133
        $sql = 'SELECT uid_fk, COUNT( * ) as cnt ';
134
        $sql .= 'FROM ( ';
135
        $sql .= 'SELECT uid_fk ';
136
        $sql .= 'FROM ' . $xoopsDB->prefix('smallworld_messages') . ' ';
137
        $sql .= 'UNION ALL SELECT uid_fk ';
138
        $sql .= 'FROM ' . $xoopsDB->prefix('smallworld_comments') . ' ';
139
        $sql .= ') AS u ';
140
        $sql .= 'GROUP BY uid_fk ';
141
        $sql .= 'ORDER BY count( * ) DESC limit 20';
142
        $result = $xoopsDB->queryF($sql);
143
        $counter = $xoopsDB->getRowsNum($result);
144
145
        if ($counter < 1) {
146
            $msg = [];
147
        } else {
148
            $msg = [];
149
            $i = 1;
150
            while ($row = $xoopsDB->fetchArray($result)) {
151
                $msg['counter'][$i] = $i;
152
                $msg['img'][$i] = "<img style='margin:0px 5px;' src = '../assets/images/" . $i . ".png'>";
153
                if ($msg['counter'][$i] > 3) {
154
                    $msg['img'][$i] = '';
155
                }
156
                $msg['cnt'][$i] = $row['cnt'];
157
                $msg['from'][$i] = $xoopsUser->getUnameFromId($row['uid_fk']);
158
                ++$i;
159
            }
160
        }
161
162
        return $msg;
163
    }
164
165
    /**
166
     * find user with most posted messages in last 24 hours
167
     * @returns array
168
     */
169
    public function mostactiveusers_today()
170
    {
171
        global $xoopsDB, $xoopsUser;
172
        $sql = 'SELECT uid_fk, COUNT( * ) as cnt ';
173
        $sql .= 'FROM ( ';
174
        $sql .= 'SELECT uid_fk ';
175
        $sql .= 'FROM ' . $xoopsDB->prefix('smallworld_messages') . ' ';
176
        $sql .= 'WHERE `created` > UNIX_TIMESTAMP(DATE_SUB( NOW( ) , INTERVAL 1 DAY )) ';
177
        $sql .= 'UNION ALL SELECT uid_fk ';
178
        $sql .= 'FROM ' . $xoopsDB->prefix('smallworld_comments') . ' ';
179
        $sql .= 'WHERE `created` > UNIX_TIMESTAMP(DATE_SUB( NOW( ) , INTERVAL 1 DAY )) ';
180
        $sql .= ') AS u ';
181
        $sql .= 'GROUP BY uid_fk ';
182
        $sql .= 'ORDER BY count( * ) DESC limit 20';
183
184
        $result = $xoopsDB->queryF($sql);
185
        $msgtoday = [];
186
187
        if (0 != $xoopsDB->getRowsNum($result)) {
188
            $i = 1;
189
            while ($row = $xoopsDB->fetchArray($result)) {
190
                $msgtoday['counter'][$i] = $i;
191
                $msgtoday['img'][$i] = "<img style='margin:0px 5px;' src = '../assets/images/" . $i . ".png'>";
192
                if ($msgtoday['counter'][$i] > 3) {
193
                    $msgtoday['img'][$i] = '';
194
                }
195
                $msgtoday['cnt'][$i] = $row['cnt'];
196
                $msgtoday['from'][$i] = $xoopsUser->getUnameFromId($row['uid_fk']);
197
                ++$i;
198
            }
199
        } else {
200
            $msgtoday = [];
201
        }
202
203
        return $msgtoday;
204
    }
205
206
    /**
207
     * Find best OR worst rated users
208
     * @param string $direction
209
     * @returns array
210
     */
211
    public function topratedusers($direction)
212
    {
213
        global $xoopsUser, $xoopsDB, $xoopsTpl;
214
        $array = [];
215
216
        if ('up' === $direction) {
217
            $sql    = 'SELECT owner, count(*) AS cnt FROM ' . $xoopsDB->prefix('smallworld_vote') . " WHERE up='1' GROUP BY owner ORDER BY cnt DESC LIMIT 20";
218
            $result = $xoopsDB->queryF($sql);
219
            $count = $xoopsDB->getRowsNum($result);
220
            $i = 1;
221
            if ($count >= $i) {
222
                while ($row = $xoopsDB->fetchArray($result)) {
223
                    $array['counter'][$i] = $i;
224
                    $array['img'][$i] = "<img height='10px' width='10px' " . "style='margin:0px 5px;' src = '../assets/images/like.png'>";
225
                    if ($array['counter'][$i] > 3) {
226
                        $array['img'][$i] = '';
227
                    }
228
                    $array['cnt'][$i] = $row['cnt'];
229
                    $array['user'][$i] = $xoopsUser->getUnameFromId($row['owner']);
230
                    ++$i;
231
                }
232
            } else {
233
                $array = [];
234
            }
235
        } else {
236
            $sql    = 'SELECT owner, count(*) AS cnt FROM ' . $xoopsDB->prefix('smallworld_vote') . " WHERE down='1' GROUP BY owner ORDER BY cnt DESC LIMIT 20";
237
            $result = $xoopsDB->queryF($sql);
238
            $count = $xoopsDB->getRowsNum($result);
239
            $i = 1;
240
            if (0 != $count) {
241
                while ($row = $xoopsDB->fetchArray($result)) {
242
                    $array['counter'][$i] = $i;
243
                    $array['img'][$i] = "<img height='10px' width='10px' " . "style='margin:0px 5px;' src = '../assets/images/dislike.png'>";
244
                    if ($array['counter'][$i] > 3) {
245
                        $array['img'][$i] = '';
246
                    }
247
                    $array['cnt'][$i] = $row['cnt'];
248
                    $array['user'][$i] = $xoopsUser->getUnameFromId($row['owner']);
249
                    ++$i;
250
                }
251
            } else {
252
                $array = [];
253
            }
254
        }
255
256
        return $array;
257
    }
258
259
    /**
260
     * Get all users to loop in admin for administration
261
     * @param string $inspect
262
     * @returns array
263
     */
264
    public function getAllUsers($inspect)
265
    {
266
        global $xoopsDB, $xoopsUser, $xoopsTpl;
267
        $data = [];
268
        if ('yes' === $inspect) {
269
            $sql = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_admin') . ' WHERE (inspect_start  + inspect_stop) >= ' . time() . ' ORDER BY username';
270
        } else {
271
            $sql = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_admin') . ' WHERE (inspect_start  + inspect_stop) < ' . time() . ' ORDER BY username';
272
        }
273
        $result = $xoopsDB->queryF($sql);
274
        $count = $xoopsDB->getRowsNum($result);
275
        if (0 != $count) {
276
            while ($row = $xoopsDB->fetchArray($result)) {
277
                $data[] = $row;
278
            }
279
        }
280
        if (!empty($data)) {
281
            return $data;
282
        }
283
    }
284
285
    /**
286
     * check server if update is available
287
     * Server currently at culex.dk
288
     * Variable $version = current smallworld version number
289
     * @return string
290
     */
291
    public function doCheckUpdate()
292
    {
293
        global $pathIcon16;
294
        $version = $this->ModuleInstallVersion();
295
        $critical = false;
296
        $update = false;
297
        $rt = '';
298
        $url = 'http://www.culex.dk/updates/smallworld_version.csv';
299
        $fileC = $this->fetchURL($url, ['fopen', 'curl', 'socket']);
300
        $read = explode(';', $fileC);
301
302
        $upd_img = $pathIcon16 . '/on.png';
303
304
        if ($read[0] > $version && '1' == $read[2]) {
305
            $critical = true;
306
            $upd_img = $pathIcon16 . '/off.png';
307
        }
308
        if ($read[0] > $version && '1' != $read[2]) {
309
            $update = true;
310
            $upd_img = '../assets/images/upd_normal.png';
311
        }
312
        if ($critical) {
313
            $rt = "<div class='smallworld_update'><img src='" . $upd_img . "'>";
314
            $rt .= _AM_SMALLWORLD_UPDATE_CRITICAL_UPD . '</div>';
315
            $rt .= "<textarea class='xim_update_changelog'>" . $read[1] . '</textarea><br><br>';
316
            $rt .= _AM_SMALLWORLD_UPDATE_SERVER_FILE . "<br><a href='" . $read[3] . "'>" . $read[3] . '</a>';
317
        } elseif ($update) {
318
            $rt = "<div class='smallworld_update'><img src='" . $upd_img . "'>";
319
            $rt .= _AM_SMALLWORLD_UPDATE_NORMAL_UPD . '</div>';
320
            $rt .= "<textarea class='smallworld_update_changelog'>" . $read[1] . '</textarea><br><br>';
321
            $rt .= _AM_SMALLWORLD_UPDATE_SERVER_FILE . "<br><a href='" . $read[3] . "'>" . $read[3] . '</a>';
322
        } else {
323
            $rt = "<div class='smallworld_update'><br><img src='" . $upd_img . "'>" . _AM_SMALLWORLD_UPDATE_YOUHAVENEWESTVERSION . '</div>';
324
        }
325
326
        return $rt;
327
    }
328
329
    /**
330
     * Fetch content of comma separated text file
331
     * will attempt to use the fopen method first, then curl, then socket
332
     * @param string $url
333
     * @param array $methods
334
     * @returns string
335
     */
336
    public function fetchURL($url, $methods = ['fopen', 'curl', 'socket'])
337
    {
338
        /**
339
         *   December 21st 2010, Mathew Tinsley ([email protected])
340
         *   http://tinsology.net
341
         *
342
         *   To the extent possible under law, Mathew Tinsley has waived all copyright and related or
343
         *   neighboring rights to this work. There's absolutely no warranty.
344
         */
345
        if ('string' === gettype($methods)) {
346
            $methods = [$methods];
347
        } elseif (!is_array($methods)) {
348
            return false;
349
        }
350
        foreach ($methods as $method) {
351
            switch ($method) {
352
                case 'fopen':
353
                    //uses file_get_contents in place of fopen
354
                    //allow_url_fopen must still be enabled
355
                    if (ini_get('allow_url_fopen')) {
356
                        $contents = file_get_contents($url);
357
                        if (false !== $contents) {
358
                            return $contents;
359
                        }
360
                    }
361
                    break;
362
                case 'curl':
363
                    if (function_exists('curl_init')) {
364
                        $ch = curl_init();
365
                        curl_setopt($ch, CURLOPT_URL, $url);
366
                        curl_setopt($ch, CURLOPT_HEADER, 0);
367
                        // return the value instead of printing the response to browser
368
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
369
                        $result = curl_exec($ch);
370
                        curl_close($ch);
371
                        //return curl_exec($ch);
372
                        return $result;
373
                    }
374
                    break;
375
                case 'socket':
376
                    //make sure the url contains a protocol, otherwise $parts['host'] won't be set
377
                    if (0 !== strpos($url, 'http://') && 0 !== strpos($url, 'https://')) {
378
                        $url = 'http://' . $url;
379
                    }
380
                    $parts = parse_url($url);
381
                    if ('https' === $parts['scheme']) {
382
                        $target = 'ssl://' . $parts['host'];
383
                        $port = isset($parts['port']) ? $parts['port'] : 443;
384
                    } else {
385
                        $target = $parts['host'];
386
                        $port = isset($parts['port']) ? $parts['port'] : 80;
387
                    }
388
                    $page = isset($parts['path']) ? $parts['path'] : '';
389
                    $page .= isset($parts['query']) ? '?' . $parts['query'] : '';
390
                    $page .= isset($parts['fragment']) ? '#' . $parts['fragment'] : '';
391
                    $page = ('' == $page) ? '/' : $page;
392
                    if ($fp = fsockopen($target, $port, $errno, $errstr, 15)) {
393
                        $headers = "GET $page HTTP/1.1\r\n";
394
                        $headers .= "Host: {$parts['host']}\r\n";
395
                        $headers .= "Connection: Close\r\n\r\n";
396
                        if (fwrite($fp, $headers)) {
397
                            $resp = '';
398
                            //while not eof and an error does not occur when calling fgets
399
                            while (!feof($fp) && false !== ($curr = fgets($fp, 128))) {
400
                                $resp .= $curr;
401
                            }
402
                            if (isset($curr) && false !== $curr) {
403
                                return substr(strstr($resp, "\r\n\r\n"), 3);
404
                            }
405
                        }
406
                        fclose($fp);
407
                    }
408
                    break;
409
            }
410
        }
411
412
        return false;
413
    }
414
415
    /**
416
     * Smallworld_sanitize(array(array) )
417
     * flatten multidimentional arrays to one dimentional
418
     * @param array $array
419
     * @returns Array
420
     */
421
    public function flatten($array)
422
    {
423
        $return = [];
424
        while (count($array)) {
425
            $value = array_shift($array);
426
            if (is_array($value)) {
427
                foreach ($value as $sub) {
428
                    $array[] = $sub;
429
                }
430
            } else {
431
                $return[] = $value;
432
            }
433
        }
434
435
        return $return;
436
    }
437
438
    /**
439
     * Smallworld_sanitize($string)
440
     * @param string $text
441
     * @returns string
442
     */
443
    public function Smallworld_sanitize($text)
444
    {
445
        $text = htmlspecialchars($text, ENT_QUOTES);
446
        $myts = MyTextSanitizer::getInstance();
447
        $text = $myts->displayTarea($text, 1, 1, 1, 1);
448
        $text = str_replace("\n\r", "\n", $text);
449
        $text = str_replace("\r\n", "\n", $text);
450
        $text = str_replace("\n", '<br>', $text);
451
        $text = str_replace('"', "'", $text);
452
453
        return $text;
454
    }
455
}
456
457
/*
458
 *  Does a sync to remove orphans from smallworld db

class/SmallworldAdmin.php 1 location

@@ 23-455 (lines=433) @@
20
 * @author       Michael Albertsen (http://culex.dk) <[email protected]>
21
 */
22
23
class SmallworldAdmin
24
{
25
    /**
26
     * Get oldest message in Db
27
     * @returns time
28
     */
29
    public function oldestMsg()
30
    {
31
        global $xoopsDB;
32
        $date = 0;
33
        $sql     = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_messages') . ' ORDER BY created LIMIT 1';
34
        $result = $xoopsDB->queryF($sql);
35
        $counter = $xoopsDB->getRowsNum($result);
36
        if ($counter >= 1) {
37
            while ($sqlfetch = $xoopsDB->fetchArray($result)) {
38
                $date = $sqlfetch['created'];
39
            }
40
        }
41
42
        return $date;
43
    }
44
45
    /**
46
     * Get average messages sent per day
47
     * @param int $totaldays
48
     * @return int|string
49
     */
50
    public function AvgMsgDay($totaldays)
51
    {
52
        global $xoopsDB;
53
        $sql = 'SELECT count( * ) / ' . $totaldays . ' AS averg FROM ' . $xoopsDB->prefix('smallworld_messages') . '';
54
        $result = $xoopsDB->queryF($sql);
55
        while ($sqlfetch = $xoopsDB->fetchArray($result)) {
56
            $avg = number_format($sqlfetch['averg'], 2, '.', ',');
57
        }
58
59
        return $avg;
60
    }
61
62
    /**
63
     * total users using smallworld
64
     * @return int
65
     */
66
    public function TotalUsers()
67
    {
68
        global $xoopsDB;
69
        $sql = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_user') . '';
70
        $result = $xoopsDB->queryF($sql);
71
        $counter = $xoopsDB->getRowsNum($result);
72
        if ($counter < 1) {
73
            $sum = 0;
74
        } else {
75
            $i = 0;
76
            while ($myrow = $xoopsDB->fetchArray($result)) {
77
                $user[$i]['username'] = $myrow['username'];
78
                ++$i;
79
            }
80
            $all = $this->flatten($user);
81
            $sum = count(array_unique($all));
82
            $unique = array_unique($all);
83
        }
84
85
        return $sum;
86
    }
87
88
    /**
89
     * Get version of module
90
     * @returns string
91
     */
92
    public function ModuleInstallVersion()
93
    {
94
        global $xoopsModule;
95
        $version = round($xoopsModule->getVar('version') / 100, 2);
96
97
        return $version;
98
    }
99
100
    /**
101
     * Get date when Module was installed
102
     * @return string|time
103
     */
104
    public function ModuleInstallDate()
105
    {
106
        global $xoopsModule;
107
        $date = formatTimestamp($xoopsModule->getVar('last_update'), 'm');
108
109
        return $date;
110
    }
111
112
    /**
113
     * Count total days represented in db
114
     * @return float|int|time
115
     */
116
    public function CountDays()
117
    {
118
        global $xoopsDB, $xoopsModule;
119
        $date = $this->oldestMsg();
120
        $now = time();
121
        $diff = ($now - $date) / (60 * 60 * 24);
122
123
        return $diff;
124
    }
125
126
    /**
127
     * find user with most posted messages
128
     * @returns array
129
     */
130
    public function mostactiveusers_allround()
131
    {
132
        global $xoopsDB, $xoopsUser;
133
        $sql = 'SELECT uid_fk, COUNT( * ) as cnt ';
134
        $sql .= 'FROM ( ';
135
        $sql .= 'SELECT uid_fk ';
136
        $sql .= 'FROM ' . $xoopsDB->prefix('smallworld_messages') . ' ';
137
        $sql .= 'UNION ALL SELECT uid_fk ';
138
        $sql .= 'FROM ' . $xoopsDB->prefix('smallworld_comments') . ' ';
139
        $sql .= ') AS u ';
140
        $sql .= 'GROUP BY uid_fk ';
141
        $sql .= 'ORDER BY count( * ) DESC limit 20';
142
        $result = $xoopsDB->queryF($sql);
143
        $counter = $xoopsDB->getRowsNum($result);
144
145
        if ($counter < 1) {
146
            $msg = [];
147
        } else {
148
            $msg = [];
149
            $i = 1;
150
            while ($row = $xoopsDB->fetchArray($result)) {
151
                $msg['counter'][$i] = $i;
152
                $msg['img'][$i] = "<img style='margin:0px 5px;' src = '../assets/images/" . $i . ".png'>";
153
                if ($msg['counter'][$i] > 3) {
154
                    $msg['img'][$i] = '';
155
                }
156
                $msg['cnt'][$i] = $row['cnt'];
157
                $msg['from'][$i] = $xoopsUser->getUnameFromId($row['uid_fk']);
158
                ++$i;
159
            }
160
        }
161
162
        return $msg;
163
    }
164
165
    /**
166
     * find user with most posted messages in last 24 hours
167
     * @returns array
168
     */
169
    public function mostactiveusers_today()
170
    {
171
        global $xoopsDB, $xoopsUser;
172
        $sql = 'SELECT uid_fk, COUNT( * ) as cnt ';
173
        $sql .= 'FROM ( ';
174
        $sql .= 'SELECT uid_fk ';
175
        $sql .= 'FROM ' . $xoopsDB->prefix('smallworld_messages') . ' ';
176
        $sql .= 'WHERE `created` > UNIX_TIMESTAMP(DATE_SUB( NOW( ) , INTERVAL 1 DAY )) ';
177
        $sql .= 'UNION ALL SELECT uid_fk ';
178
        $sql .= 'FROM ' . $xoopsDB->prefix('smallworld_comments') . ' ';
179
        $sql .= 'WHERE `created` > UNIX_TIMESTAMP(DATE_SUB( NOW( ) , INTERVAL 1 DAY )) ';
180
        $sql .= ') AS u ';
181
        $sql .= 'GROUP BY uid_fk ';
182
        $sql .= 'ORDER BY count( * ) DESC limit 20';
183
184
        $result = $xoopsDB->queryF($sql);
185
        $msgtoday = [];
186
187
        if (0 != $xoopsDB->getRowsNum($result)) {
188
            $i = 1;
189
            while ($row = $xoopsDB->fetchArray($result)) {
190
                $msgtoday['counter'][$i] = $i;
191
                $msgtoday['img'][$i] = "<img style='margin:0px 5px;' src = '../assets/images/" . $i . ".png'>";
192
                if ($msgtoday['counter'][$i] > 3) {
193
                    $msgtoday['img'][$i] = '';
194
                }
195
                $msgtoday['cnt'][$i] = $row['cnt'];
196
                $msgtoday['from'][$i] = $xoopsUser->getUnameFromId($row['uid_fk']);
197
                ++$i;
198
            }
199
        } else {
200
            $msgtoday = [];
201
        }
202
203
        return $msgtoday;
204
    }
205
206
    /**
207
     * Find best OR worst rated users
208
     * @param string $direction
209
     * @returns array
210
     */
211
    public function topratedusers($direction)
212
    {
213
        global $xoopsUser, $xoopsDB, $xoopsTpl;
214
        $array = [];
215
216
        if ('up' === $direction) {
217
            $sql    = 'SELECT owner, count(*) AS cnt FROM ' . $xoopsDB->prefix('smallworld_vote') . " WHERE up='1' GROUP BY owner ORDER BY cnt DESC LIMIT 20";
218
            $result = $xoopsDB->queryF($sql);
219
            $count = $xoopsDB->getRowsNum($result);
220
            $i = 1;
221
            if ($count >= $i) {
222
                while ($row = $xoopsDB->fetchArray($result)) {
223
                    $array['counter'][$i] = $i;
224
                    $array['img'][$i] = "<img height='10px' width='10px' " . "style='margin:0px 5px;' src = '../assets/images/like.png'>";
225
                    if ($array['counter'][$i] > 3) {
226
                        $array['img'][$i] = '';
227
                    }
228
                    $array['cnt'][$i] = $row['cnt'];
229
                    $array['user'][$i] = $xoopsUser->getUnameFromId($row['owner']);
230
                    ++$i;
231
                }
232
            } else {
233
                $array = [];
234
            }
235
        } else {
236
            $sql    = 'SELECT owner, count(*) AS cnt FROM ' . $xoopsDB->prefix('smallworld_vote') . " WHERE down='1' GROUP BY owner ORDER BY cnt DESC LIMIT 20";
237
            $result = $xoopsDB->queryF($sql);
238
            $count = $xoopsDB->getRowsNum($result);
239
            $i = 1;
240
            if (0 != $count) {
241
                while ($row = $xoopsDB->fetchArray($result)) {
242
                    $array['counter'][$i] = $i;
243
                    $array['img'][$i] = "<img height='10px' width='10px' " . "style='margin:0px 5px;' src = '../assets/images/dislike.png'>";
244
                    if ($array['counter'][$i] > 3) {
245
                        $array['img'][$i] = '';
246
                    }
247
                    $array['cnt'][$i] = $row['cnt'];
248
                    $array['user'][$i] = $xoopsUser->getUnameFromId($row['owner']);
249
                    ++$i;
250
                }
251
            } else {
252
                $array = [];
253
            }
254
        }
255
256
        return $array;
257
    }
258
259
    /**
260
     * Get all users to loop in admin for administration
261
     * @param string $inspect
262
     * @returns array
263
     */
264
    public function getAllUsers($inspect)
265
    {
266
        global $xoopsDB, $xoopsUser, $xoopsTpl;
267
        $data = [];
268
        if ('yes' === $inspect) {
269
            $sql = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_admin') . ' WHERE (inspect_start  + inspect_stop) >= ' . time() . ' ORDER BY username';
270
        } else {
271
            $sql = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_admin') . ' WHERE (inspect_start  + inspect_stop) < ' . time() . ' ORDER BY username';
272
        }
273
        $result = $xoopsDB->queryF($sql);
274
        $count = $xoopsDB->getRowsNum($result);
275
        if (0 != $count) {
276
            while ($row = $xoopsDB->fetchArray($result)) {
277
                $data[] = $row;
278
            }
279
        }
280
        if (!empty($data)) {
281
            return $data;
282
        }
283
    }
284
285
    /**
286
     * check server if update is available
287
     * Server currently at culex.dk
288
     * Variable $version = current smallworld version number
289
     * @return string
290
     */
291
    public function doCheckUpdate()
292
    {
293
        global $pathIcon16;
294
        $version = $this->ModuleInstallVersion();
295
        $critical = false;
296
        $update = false;
297
        $rt = '';
298
        $url = 'http://www.culex.dk/updates/smallworld_version.csv';
299
        $fileC = $this->fetchURL($url, ['fopen', 'curl', 'socket']);
300
        $read = explode(';', $fileC);
301
302
        $upd_img = $pathIcon16 . '/on.png';
303
304
        if ($read[0] > $version && '1' == $read[2]) {
305
            $critical = true;
306
            $upd_img = $pathIcon16 . '/off.png';
307
        }
308
        if ($read[0] > $version && '1' != $read[2]) {
309
            $update = true;
310
            $upd_img = '../assets/images/upd_normal.png';
311
        }
312
        if ($critical) {
313
            $rt = "<div class='smallworld_update'><img src='" . $upd_img . "'>";
314
            $rt .= _AM_SMALLWORLD_UPDATE_CRITICAL_UPD . '</div>';
315
            $rt .= "<textarea class='xim_update_changelog'>" . $read[1] . '</textarea><br><br>';
316
            $rt .= _AM_SMALLWORLD_UPDATE_SERVER_FILE . "<br><a href='" . $read[3] . "'>" . $read[3] . '</a>';
317
        } elseif ($update) {
318
            $rt = "<div class='smallworld_update'><img src='" . $upd_img . "'>";
319
            $rt .= _AM_SMALLWORLD_UPDATE_NORMAL_UPD . '</div>';
320
            $rt .= "<textarea class='smallworld_update_changelog'>" . $read[1] . '</textarea><br><br>';
321
            $rt .= _AM_SMALLWORLD_UPDATE_SERVER_FILE . "<br><a href='" . $read[3] . "'>" . $read[3] . '</a>';
322
        } else {
323
            $rt = "<div class='smallworld_update'><br><img src='" . $upd_img . "'>" . _AM_SMALLWORLD_UPDATE_YOUHAVENEWESTVERSION . '</div>';
324
        }
325
326
        return $rt;
327
    }
328
329
    /**
330
     * Fetch content of comma separated text file
331
     * will attempt to use the fopen method first, then curl, then socket
332
     * @param string $url
333
     * @param array $methods
334
     * @returns string
335
     */
336
    public function fetchURL($url, $methods = ['fopen', 'curl', 'socket'])
337
    {
338
        /**
339
         *   December 21st 2010, Mathew Tinsley ([email protected])
340
         *   http://tinsology.net
341
         *
342
         *   To the extent possible under law, Mathew Tinsley has waived all copyright and related or
343
         *   neighboring rights to this work. There's absolutely no warranty.
344
         */
345
        if ('string' === gettype($methods)) {
346
            $methods = [$methods];
347
        } elseif (!is_array($methods)) {
348
            return false;
349
        }
350
        foreach ($methods as $method) {
351
            switch ($method) {
352
                case 'fopen':
353
                    //uses file_get_contents in place of fopen
354
                    //allow_url_fopen must still be enabled
355
                    if (ini_get('allow_url_fopen')) {
356
                        $contents = file_get_contents($url);
357
                        if (false !== $contents) {
358
                            return $contents;
359
                        }
360
                    }
361
                    break;
362
                case 'curl':
363
                    if (function_exists('curl_init')) {
364
                        $ch = curl_init();
365
                        curl_setopt($ch, CURLOPT_URL, $url);
366
                        curl_setopt($ch, CURLOPT_HEADER, 0);
367
                        // return the value instead of printing the response to browser
368
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
369
                        $result = curl_exec($ch);
370
                        curl_close($ch);
371
                        //return curl_exec($ch);
372
                        return $result;
373
                    }
374
                    break;
375
                case 'socket':
376
                    //make sure the url contains a protocol, otherwise $parts['host'] won't be set
377
                    if (0 !== strpos($url, 'http://') && 0 !== strpos($url, 'https://')) {
378
                        $url = 'http://' . $url;
379
                    }
380
                    $parts = parse_url($url);
381
                    if ('https' === $parts['scheme']) {
382
                        $target = 'ssl://' . $parts['host'];
383
                        $port = isset($parts['port']) ? $parts['port'] : 443;
384
                    } else {
385
                        $target = $parts['host'];
386
                        $port = isset($parts['port']) ? $parts['port'] : 80;
387
                    }
388
                    $page = isset($parts['path']) ? $parts['path'] : '';
389
                    $page .= isset($parts['query']) ? '?' . $parts['query'] : '';
390
                    $page .= isset($parts['fragment']) ? '#' . $parts['fragment'] : '';
391
                    $page = ('' == $page) ? '/' : $page;
392
                    if ($fp = fsockopen($target, $port, $errno, $errstr, 15)) {
393
                        $headers = "GET $page HTTP/1.1\r\n";
394
                        $headers .= "Host: {$parts['host']}\r\n";
395
                        $headers .= "Connection: Close\r\n\r\n";
396
                        if (fwrite($fp, $headers)) {
397
                            $resp = '';
398
                            //while not eof and an error does not occur when calling fgets
399
                            while (!feof($fp) && false !== ($curr = fgets($fp, 128))) {
400
                                $resp .= $curr;
401
                            }
402
                            if (isset($curr) && false !== $curr) {
403
                                return substr(strstr($resp, "\r\n\r\n"), 3);
404
                            }
405
                        }
406
                        fclose($fp);
407
                    }
408
                    break;
409
            }
410
        }
411
412
        return false;
413
    }
414
415
    /**
416
     * Smallworld_sanitize(array(array) )
417
     * flatten multidimentional arrays to one dimentional
418
     * @param array $array
419
     * @return array
420
     */
421
    public function flatten($array)
422
    {
423
        $return = [];
424
        while (count($array)) {
425
            $value = array_shift($array);
426
            if (is_array($value)) {
427
                foreach ($value as $sub) {
428
                    $array[] = $sub;
429
                }
430
            } else {
431
                $return[] = $value;
432
            }
433
        }
434
435
        return $return;
436
    }
437
438
    /**
439
     * Smallworld_sanitize($string)
440
     * @param string $text
441
     * @returns string
442
     */
443
    public function Smallworld_sanitize($text)
444
    {
445
        $text = htmlspecialchars($text, ENT_QUOTES);
446
        $myts = \MyTextSanitizer::getInstance();
447
        $text = $myts->displayTarea($text, 1, 1, 1, 1);
448
        $text = str_replace("\n\r", "\n", $text);
449
        $text = str_replace("\r\n", "\n", $text);
450
        $text = str_replace("\n", '<br>', $text);
451
        $text = str_replace('"', "'", $text);
452
453
        return $text;
454
    }
455
}
456
457
/*
458
 *  Does a sync to remove orphans from smallworld db