-
Notifications
You must be signed in to change notification settings - Fork 9
/
CommonAPI.php
943 lines (828 loc) · 29.4 KB
/
CommonAPI.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
<?php
/**
* @name EosAlpha BBS
* @copyright 2011 Alex Vie silvercircle(AT)gmail(DOT)com
*
* This software is a derived product, based on:
*
* Simple Machines Forum (SMF)
* copyright: 2011 Simple Machines (http://www.simplemachines.org)
* license: BSD, See included LICENSE.TXT for terms and conditions.
*
* @version 1.0pre
*
* this is what once was in $smcFunc[], a bit simplified for utf-8 only and entity check
* always enforced.
*
* it also implements the Hook and Cache APIs.
*/
if (!defined('SMF'))
die('No access');
class commonAPI {
private static $ent_list = '&(#\d{1,7}|quot|amp|lt|gt|nbsp);';
private static $space_chars = '\x{A0}\x{AD}\x{2000}-\x{200F}\x{201F}\x{202F}\x{3000}\x{FEFF}';
private static function ent_check($string)
{
return(preg_replace('~(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)~e', self::entity_fix($string), $string));
}
public static function entity_fix($string)
{
$num = substr($string, 0, 1) === 'x' ? hexdec(substr($string, 1)) : (int) $string;
return $num < 0x20 || $num > 0x10FFFF || ($num >= 0xD800 && $num <= 0xDFFF) || $num === 0x202E || $num === 0x202D ? '' : '&#' . $num . ';';
}
public static function ucwords($string)
{
$words = preg_split('~([\s\r\n\t]+)~', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
for ($i = 0, $n = count($words); $i < $n; $i += 2)
$words[$i] = self::ucfirst($words[$i]);
return implode('', $words);
}
public static function ucfirst($string)
{
return self::strtoupper(self::substr($string, 0, 1)) . self::substr($string, 1);
}
public static function substr($string, $start, $length = 0)
{
$ent_arr = preg_split('~(&#' . ('\d{1,7}') . ';|"|&|<|>| |.)~u', self::ent_check($string), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
return $length == 0 ? implode('', array_slice($ent_arr, $start)) : implode('', array_slice($ent_arr, $start, $length));
}
public static function strtolower($string)
{
global $sourcedir;
if(function_exists('mb_strtolower'))
return mb_strtolower($string, 'UTF-8');
require_once($sourcedir . '/lib/Subs-Charset.php');
return utf8_strtolower($string);
}
public static function strtoupper($string)
{
global $sourcedir;
if(function_exists('mb_strtoupper'))
return mb_strtoupper($string, 'UTF-8');
require_once($sourcedir . '/lib/Subs-Charset.php');
return utf8_strtoupper($string);
}
public static function truncate($string, $length)
{
$string = self::ent_check($string);
$matches = array();
preg_match('~^(' . self::$ent_list . '|.){' . self::strlen(substr($string, 0, $length)) . '}~u', $string, $matches);
$string = $matches[0];
while (strlen($string) > $length)
$string = preg_replace('~(?:' . self::$ent_list . '|.)$~u', '', $string);
return $string;
}
public static function strlen($string)
{
return strlen(preg_replace('~' . self::$ent_list . '|.~u', '_', self::ent_check($string)));
}
public static function htmltrim($string)
{
return preg_replace('~^(?:[ \t\n\r\x0B\x00' . self::$space_chars . ']| )+|(?:[ \t\n\r\x0B\x00' . self::$space_chars . ']| )+$~u', '', self::ent_check($string));
}
public static function htmlspecialchars($string, $quote_style = ENT_COMPAT)
{
return preg_replace(strtr('~(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)~e', array('&' => '&')), self::entity_fix($string), htmlspecialchars($string, $quote_style, 'UTF-8'));
}
public static function strpos($haystack, $needle, $offset = 0)
{
$haystack_arr = preg_split('~(&#\d{1,7}' . ';|"|&|<|>| |.)~u', self::ent_check($haystack), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
//$haystack_size = count($haystack_arr);
if (strlen($needle) === 1)
{
$result = array_search($needle, array_slice($haystack_arr, $offset));
return is_int($result) ? $result + $offset : false;
}
else
{
$needle_arr = preg_split('~(&#\d{1,7}' . ';|"|&|<|>| |.)~u', self::ent_check($needle), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$needle_size = count($needle_arr);
$result = array_search($needle_arr[0], array_slice($haystack_arr, $offset));
while (is_int($result))
{
$offset += $result;
if (array_slice($haystack_arr, $offset, $needle_size) === $needle_arr)
return $offset;
$result = array_search($needle_arr[0], array_slice($haystack_arr, ++$offset));
}
return false;
}
}
public static function getMessagesPerPage()
{
global $modSettings, $options;
return(empty($modSettings['disableCustomPerPage']) && !empty($options['messages_per_page']) ? $options['messages_per_page'] : $modSettings['defaultMaxMessages']);
}
}
/**
* implements the new hook API
*
* all hooks are now stored in a single array. each top level array element defines a single
* hook identified by its name.
*/
class HookAPI {
private static $hooks = array();
private static $addonsdir;
/**
* Initialize the hooks.
* This must be called immediately after loading modSettings[] from the database
*
* also: sets the addons base directory. It must exist, be writeable and be a directory.
* If any check fails, it falls back to the default hardcoded sub-folder ($boarddir/addons).
*
* @param $the_hooks string - serialized array of hooks
*
*/
public static function setHooks(&$the_hooks)
{
global $boarddir;
self::$hooks = @unserialize($the_hooks);
if(isset($GLOBALS['addonsdir']) && !empty($GLOBALS['addonsdir']) && file_exists($GLOBALS['addonsdir']) && is_dir($GLOBALS['addonsdir']))
self::$addonsdir = rtrim($GLOBALS['addonsdir'], '/\\ ') . '/';
else
self::$addonsdir = $boarddir . 'addons/';
}
/**
*
* This function is typically called from the install procedure of an addon.
* It adds one file/function to a named hook.
*
* @param type $hook string - the name of the hook
* @param type $product string - a product name. This also defines the sub-folder in which the files of the addons must be
* @param string $file string - the file to include
* @param type $function string - a function name to call
* @return type bool - true if all ok, false if the file or function could not be found.
*/
public static function addHook($hook, $product, $file, $function)
{
$ref = array('p' => $product, 'f' => $file, 'c' => $function);
if(isset(self::$hooks[$hook]) && is_array(self::$hooks[$hook])) {
foreach(self::$hooks[$hook] as $current_hook) {
if($current_hook == $ref) {
log_error(sprintf('HookAPI: duplicate hook installation detected in hook %s (product: %s, function: %s, file: %s', $hook, $ref['p'], $ref['c'], $ref['f']));
return;
}
}
}
// check the hook for validity
$file = self::$addonsdir . $ref['p'] . '/' . $ref['f'];
if(!file_exists($file)) {
log_error(sprintf('HookAPI: missing hook file while installing into hook %s (product: %s, function: %s, file: %s', $hook, $ref['p'], $ref['c'], $ref['f']));
return(false);
}
@include_once($file);
if(!is_callable($ref['c'])) {
log_error(sprintf('HookAPI: missing function while installing into hook %s (product: %s, function: %s, file: %s', $hook, $ref['p'], $ref['c'], $ref['f']));
return(false);
}
self::$hooks[$hook][] = array('p' => $product, 'f' => $file, 'c' => trim($function));
$change_array = array('integration_hooks' => serialize(self::$hooks));
updateSettings($change_array, true);
return(true);
}
/**
* Process functions of an integration hook.
*
* @param string $hook
* @param array $parameters = array()
*/
public static function callHook($hook, $parameters = array())
{
$results = array();
if(isset(self::$hooks[$hook]) && is_array(self::$hooks[$hook])) {
foreach(self::$hooks[$hook] as $current_hook) {
@include_once(self::$addonsdir . $current_hook['p'] . '/' . $current_hook['f']);
if(is_callable($current_hook['c']))
$results[$current_hook['c']] = call_user_func_array($current_hook['c'], $parameters);
}
}
return $results;
}
/**
* Integrate the output buffer hooks.
*
* It's a special case - hooks that work on the output buffer - they
* must be called via ob_start() and therefore need their own method.
*
* All functions registered under the integrate_buffer hook will run here.
*/
public static function integrateOB()
{
if(isset(self::$hooks['integrate_buffer'])) {
foreach(self::$hooks['integrate_buffer'] as $current_hook) {
@include_once(self::$addonsdir . $current_hook['p'] . '/' . $current_hook['f']);
if(is_callable($current_hook['c']))
ob_start($current_hook['c']);
}
}
}
/**
* Remove a registered hook (if found).
* It removes it from the database and updates the currently loaded hooks.
*
* @param string $hook - the name of the hook
* @param type $product string - the product name.
* @param string $file string - the file to include
* @param type $function string - a function name to call
*/
public static function removeHook($hook, $product, $file, $function)
{
$ref = array('p' => $product, 'f' => $file, 'c' => $function);
if(isset(self::$hooks[$hook]) && is_array(self::$hooks[$hook])) {
foreach(self::$hooks[$hook] as $key => $current_hook) {
if($current_hook == $ref) {
unset(self::$hooks[$hook][$key]);
if(0 == count(self::$hooks[$hook]))
unset(self::$hooks[$hook]);
$change_array = array('integration_hooks' => serialize(self::$hooks));
updateSettings($change_array, true);
return;
}
}
}
}
/**
* Remove all hooks related to the product given in $product
* product name is CASE SENSITIVE
*
* @static
* @param $product string - the product name.
*/
public static function removeAll($product)
{
$changed = false;
foreach(self::$hooks as $k => $hooks) {
foreach($hooks as $n => $hook) {
if($hook['p'] == $product) {
unset(self::$hooks[$k][$n]);
$changed = true;
}
}
if(0 == count(self::$hooks[$k]))
unset(self::$hooks[$k]);
}
if($changed) {
$change_array = array('integration_hooks' => serialize(self::$hooks));
updateSettings($change_array, true);
}
}
}
class cacheAPI {
private static $API = -1;
private static $mcached_server;
private static $basekey = '';
private static $memcached = 0;
private static $cachedir = '';
private static $cache_hits = array();
private static $cache_count = 0;
private static $memcached_hosts = '';
/**
* support for PECL new memcacheD
*/
private static function getMemcachedServer()
{
if (is_a(self::$mcached_server, 'Memcached'))
return self::$mcached_server;
$servers = explode(',', self::$memcached_hosts);
self::$mcached_server = new Memcached();
if (0 == count(self::$mcached_server->getServerList()) ) {
$h = array();
foreach($servers as $server) {
$server = explode( ':', trim($server));
$ip = $server[0];
$port = empty($server[1]) ? 11211 : $server[1];
$h[] = array($ip, $port);
}
self::$mcached_server->addServers($h);
}
return self::$mcached_server;
}
/**
* Get server for the OLD memcache implementation
*
* @param int $level caching level
*/
private static function getMemcacheServer($level = 3)
{
global $modSettings, $db_persist;
$servers = explode(',', $modSettings['cache_memcached']);
$server = explode(':', trim($servers[array_rand($servers)]));
// Don't try more times than we have servers!
$level = min(count($servers), $level);
// Don't wait too long: yes, we want the server, but we might be able to run the query faster!
if (empty($db_persist))
self::$memcached = memcache_connect($server[0], empty($server[1]) ? 11211 : $server[1]);
else
self::$memcached = memcache_pconnect($server[0], empty($server[1]) ? 11211 : $server[1]);
if (!self::$memcached && $level > 0)
self::getMemcacheServer($level - 1);
}
/**
* Initialization for the cache.
*
* @param string $desired - the given cache implementation library
* @param unknown_type $basekey
* @param array $memcached_hosts - array of memcached hosts, if passed
* @param string $cachedir - cache directory
*/
public static function init($desired, $basekey, $memcached_hosts, $cachedir)
{
self::$basekey = $basekey;
self::$memcached_hosts = $memcached_hosts;
self::$cachedir = $cachedir;
if($desired == 'apc' && function_exists('apc_store'))
self::$API = 1;
elseif($desired == 'xcache' && function_exists('xcache_get') && ini_get('xcache.var_size') > 0)
self::$API = 2;
elseif($desired == 'zend' && function_exists('output_cache_get'))
self::$API = 3;
elseif($desired == 'memcache' && function_exists('memcache_get'))
self::$API = 4;
elseif($desired == 'new_memcache' && class_exists('Memcached'))
self::$API = 5;
elseif($desired == 'file')
self::$API = 0;
// check for possible cache configuration errors
//if(((self::$API == 4 || self::$API == 5) && empty(self::$memcached_hosts)) || self::$API == -1)
// log_error(sprintf('cacheInit: desired caching system unsupported or not available (desired = %s, memcached hosts = %s', $desired, self::$memcached_hosts));
}
/**
* Verify if filesystem cache directory is set
*
* @return string - message (warning or empty if all went fine)
*/
public static function verifyFileCache()
{
global $user_info, $txt;
$msg = '';
// if we have file caching, make absolutely sure the folder exists and is writeable
// if not, throw a warning (for admins only)
if(0 == self::$API && (empty(self::$cachedir) || !file_exists(self::$cachedir) || !is_writable(self::$cachedir) )) {
self::$API = -1;
if($user_info['is_admin']) {
loadLanguage('Errors');
$msg = '
<div class="errorbox">
'.$txt['file_cache_config_error'].'<br><br>
'.sprintf($txt['file_cache_config_path'], empty(self::$cachedir) ? 'Empty value' : self::$cachedir).'
</div>
';
}
}
return($msg);
}
/**
* Disable cache.
*/
public static function disable()
{
self::$API = -1;
}
/**
* Get the active cache engine.
*/
public static function getEngine()
{
global $txt;
$engines = array('Filesystem cache', 'APC', 'Xcache', 'Zend', 'Memcached', 'New PECL Memcached');
if(-1 == self::$API)
return($txt['caching_disabled']);
else
return($engines[(int)self::$API]);
}
/**
* Get a cached value.
*
* @param string $key
* @param int $ttl = 120
*/
public static function getCache($key, $ttl = 120)
{
global $db_show_debug, $cachedir;
if(-1 == self::$API)
return(null);
self::$cache_count++;
if (isset($db_show_debug) && $db_show_debug === true) {
self::$cache_hits[self::$cache_count] = array('k' => $key, 'd' => 'get');
$st = microtime();
}
$key = self::$basekey . strtr($key, ':', '-');
switch(self::$API) {
case 5:
$key = str_replace(' ', '_', $key);
$instance = self::getMemcachedServer();
$value = $instance->get($key);
break;
case 4:
if (empty(self::$memcached))
self::getMemcacheServer();
if (!self::$memcached)
return;
$value = memcache_get(self::$memcached, $key);
break;
case 1:
$value = apc_fetch($key . 'smf');
break;
case 3:
$value = output_cache_get($key, $ttl);
break;
case 2:
$value = xcache_get($key);
break;
case 0:
if (file_exists($cachedir . '/data_' . $key . '.php') && filesize($cachedir . '/data_' . $key . '.php') > 10) {
require($cachedir . '/data_' . $key . '.php');
if (!empty($expired) && isset($value))
{
@unlink($cachedir . '/data_' . $key . '.php');
unset($value);
}
}
break;
}
if (isset($db_show_debug) && $db_show_debug === true) {
self::$cache_hits[self::$cache_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
self::$cache_hits[self::$cache_count]['s'] = isset($value) ? strlen($value) : 0;
}
if (empty($value))
return null;
else
return @unserialize($value);
}
/**
* Store a value in cache.
*
* @param string $key
* @param mixed $value
* @param int $ttl = 120
*/
public static function putCache($key, $value, $ttl = 120)
{
global $db_show_debug, $cachedir;
if(-1 == self::$API)
return;
self::$cache_count++;
if (isset($db_show_debug) && $db_show_debug === true) {
self::$cache_hits[self::$cache_count] = array('k' => $key, 'd' => 'put', 's' => $value === null ? 0 : strlen(serialize($value)));
$st = microtime();
}
$key = self::$basekey . strtr($key, ':', '-');
$value = $value === null ? null : serialize($value);
switch(self::$API) {
case 5:
$key = str_replace(' ', '_', $key);
$instance = self::getMemcachedServer();
$instance->set($key, $value, $ttl);
break;
case 4:
if (empty(self::$memcached))
self::getMemcacheServer();
if (!self::$memcached)
return;
memcache_set(self::$memcached, $key, $value, 0, $ttl);
break;
case 1:
// An extended key is needed to counteract a bug in APC.
if ($value === null)
apc_delete($key . 'smf');
else
apc_store($key . 'smf', $value, $ttl);
break;
case 3:
output_cache_put($key, $value);
break;
case 2:
if ($value === null)
xcache_unset($key);
else
xcache_set($key, $value, $ttl);
break;
case 0:
if ($value === null)
@unlink($cachedir . '/data_' . $key . '.php');
else {
$cache_data = '<' . '?' . 'php if (!defined(\'SMF\')) die; if (' . (time() + $ttl) . ' < time()) $expired = true; else{$expired = false; $value = \'' . addcslashes($value, '\\\'') . '\';}' . '?' . '>';
$fh = @fopen($cachedir . '/data_' . $key . '.php', 'w');
if ($fh)
{
// Write the file.
set_file_buffer($fh, 0);
flock($fh, LOCK_EX);
$cache_bytes = fwrite($fh, $cache_data);
flock($fh, LOCK_UN);
fclose($fh);
// Check that the cache write was successful; all the data should be written
// If it fails due to low diskspace, remove the cache file
if ($cache_bytes != strlen($cache_data))
@unlink($cachedir . '/data_' . $key . '.php');
}
}
break;
}
if (isset($db_show_debug) && $db_show_debug === true)
self::$cache_hits[self::$cache_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
}
}
/**
* Mobile Detect
* @usage require_once 'Mobile_Detect.php';
* $detect = new Mobile_Detect();
* $detect->isMobile() or $detect->isTablet()
*
* For more specific usage see the documentation inside the class.
* $detect->isAndroidOS() or $detect->isiPhone() ...
*
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
class Mobile_Detect {
protected $detectionRules;
protected $userAgent = null;
protected $accept = null;
// Assume the visitor has a desktop environment.
protected $isMobile = false;
protected $isTablet = false;
protected $phoneDeviceName = null;
protected $tabletDevicename = null;
protected $operatingSystemName = null;
protected $userAgentName = null;
// List of mobile devices (phones)
protected $phoneDevices = array(
'iPhone' => '(iPhone.*Mobile|iPod|iTunes)',
'BlackBerry' => 'BlackBerry|rim[0-9]+',
'HTC' => 'HTC|Desire',
'Nexus' => 'Nexus One|Nexus S',
'DellStreak' => 'Dell Streak',
'Motorola' => '\bDroid\b.*Build|HRI39|MOT\-',
'Samsung' => 'Samsung|GT\-P1000|SGH\-T959D|GT\-I9100|GT\-I9000',
'Sony' => 'E10i',
'Asus' => 'Asus.*Galaxy',
'Palm' => 'PalmSource|Palm', // avantgo|blazer|elaine|hiptop|plucker|xiino
'GenericPhone' => '(mmp|pocket|psp|symbian|Smartphone|smartfon|treo|up.browser|up.link|vodafone|wap|nokia|Series40|Series60|S60|SonyEricsson|N900|\bPPC\b|MAUI.*WAP.*Browser|LG\-P500)'
);
// List of tablet devices.
protected $tabletDevices = array(
'BlackBerryTablet' => 'PlayBook|RIM Tablet',
'iPad' => 'iPad.*Mobile',
'Kindle' => 'Kindle|Silk.*Accelerated',
'SamsungTablet' => 'SCH\-I800|GT\-P1000|Galaxy.*Tab',
'MotorolaTablet' => 'xoom|sholest',
'AsusTablet' => 'Transformer|TF101',
'GenericTablet' => 'Tablet|ViewPad7|LG\-V909|MID7015|BNTV250A|LogicPD Zoom2|\bA7EB\b|CatNova8|A1_07|CT704|CT1002|\bM721\b',
);
// List of mobile Operating Systems.
protected $operatingSystems = array(
'AndroidOS' => '(android.*mobile|android(?!.*mobile))',
'BlackBerryOS' => '(blackberry|rim tablet os)',
'PalmOS' => '(avantgo|blazer|elaine|hiptop|palm|plucker|xiino)',
'SymbianOS' => 'Symbian|SymbOS|Series60|Series40|\bS60\b',
'WindowsMobileOS' => 'IEMobile|Windows Phone|Windows CE.*(PPC|Smartphone)|MSIEMobile|Window Mobile|XBLWP7',
'iOS' => '(iphone|ipod|ipad)',
'FlashLiteOS' => '',
'JavaOS' => '',
'NokiaOS' => '',
'webOS' => '',
'badaOS' => '\bBada\b',
'BREWOS' => '',
);
// List of mobile User Agents.
protected $userAgents = array(
'Chrome' => '\bCrMo\b',
'Dolfin' => '\bDolfin\b',
'Opera' => '(Opera.*Mini|Opera.*Mobi)',
'Skyfire' => 'skyfire',
'IE' => 'ie*mobile',
'Firefox' => 'fennec|firefox.*maemo',
'Bolt' => 'bolt',
'TeaShark' => 'teashark',
'Blazer' => 'Blazer',
'Safari' => 'Mobile*Safari',
'Midori' => 'midori',
'GenericBrowser' => 'NokiaBrowser|OviBrowser'
);
/**
* Construct and init this instance.
*/
function __construct(){
// Merge all rules together.
$this->detectionRules = array_merge(
$this->phoneDevices,
$this->tabletDevices,
$this->operatingSystems,
$this->userAgents
);
$this->userAgent = $_SERVER['HTTP_USER_AGENT'];
$this->accept = isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : '';
if (
isset($_SERVER['HTTP_X_WAP_PROFILE']) ||
isset($_SERVER['HTTP_X_WAP_CLIENTID']) ||
isset($_SERVER['HTTP_WAP_CONNECTION']) ||
isset($_SERVER['HTTP_PROFILE']) ||
isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']) || // Reported by Nokia devices (eg. C3)
isset($_SERVER['HTTP_X_NOKIA_IPADDRESS']) ||
isset($_SERVER['HTTP_X_NOKIA_GATEWAY_ID']) ||
isset($_SERVER['HTTP_X_ORANGE_ID']) ||
isset($_SERVER['HTTP_X_VODAFONE_3GPDPCONTEXT']) ||
isset($_SERVER['HTTP_X_HUAWEI_USERID']) ||
isset($_SERVER['HTTP_UA_OS']) || // Reported by Windows Smartphones
(isset($_SERVER['HTTP_UA_CPU']) && $_SERVER['HTTP_UA_CPU'] == 'ARM') // Seen this on a HTC
) {
$this->isMobile = true;
} elseif (!empty($this->accept) && (strpos($this->accept, 'text/vnd.wap.wml') !== false || strpos($this->accept, 'application/vnd.wap.xhtml+xml') !== false)) {
$this->isMobile = true;
} else {
$this->_detect();
}
}
/**
* Get detection rules.
*/
public function getRules()
{
return $this->detectionRules;
}
/**
* Magic method to route the checks
*
* @param string $name
* @param array $arguments
*/
public function __call($name, $arguments)
{
$key = substr($name, 2);
return $this->_detect($key);
}
/**
* Run the checks
*
* @param string $key
*/
private function _detect($key='')
{
if(empty($key)){
// Begin general search.
foreach($this->detectionRules as $_key => $_regex){
if(empty($_regex)){ continue; }
if(preg_match('/'.$_regex.'/is', $this->userAgent)){
$this->isMobile = true;
return true;
}
}
return false;
} else {
// Search for a certain key.
// Make the keys lowecase so we can match: isIphone(), isiPhone(), isiphone(), etc.
$key = strtolower($key);
$_rules = array_change_key_case($this->detectionRules);
if(array_key_exists($key, $_rules)){
if(empty($_rules[$key])){ return null; }
if(preg_match('/'.$_rules[$key].'/is', $this->userAgent)){
$this->isMobile = true;
return true;
} else {
return false;
}
} else {
trigger_error("Method $key is not defined", E_USER_WARNING);
}
return false;
}
}
/**
* Returns true if any type of mobile device detected, including special ones
* @return bool
*/
public function isMobile()
{
return $this->isMobile;
}
/**
* Return true if any type of tablet device is detected.
* @return boolean
*/
public function isTablet()
{
foreach($this->tabletDevices as $_key => $_regex){
if(preg_match('/'.$_regex.'/is', $this->userAgent)){
$this->isTablet = true;
return true;
}
}
return false;
}
}
class Topiclist {
private $topiclist = array();
private $users_to_load = array();
private $topic_ids = array();
function __construct($request, $total_items) {
global $context, $txt, $user_info, $scripturl, $options, $memberContext, $modSettings;
while ($row = mysql_fetch_assoc($request))
{
censorText($row['subject']);
$this->topic_ids[] = $row['id_topic'];
$f_post_mem_href = !empty($row['id_member']) ? URL::user($row['id_member'], $row['first_member_name']) : '';
$t_href = URL::topic($row['id_topic'], $row['subject'], 0);
$l_post_mem_href = !empty($row['id_member_updated']) ? URL::user($row['id_member_updated'], $row['last_real_name'] ) : '';
$l_post_msg_href = URL::topic($row['id_topic'], $row['last_subject'], $user_info['is_guest'] ? (!empty($options['view_newest_first']) ? 0 : ((int) (($row['num_replies']) / $context['pageindex_multiplier'])) * $context['pageindex_multiplier']) : 0, $user_info['is_guest'] ? true : false, $user_info['is_guest'] ? '' : ('.msg' . $row['id_last_msg']), $user_info['is_guest'] ? ('#msg' . $row['id_last_msg']) : '#new');
$this->topiclist[$row['id_topic']] = array(
'id' => $row['id_topic'],
'id_member_started' => empty($row['id_member']) ? 0 : $row['id_member'],
'first_post' => array(
'id' => $row['id_first_msg'],
'member' => array(
'username' => $row['first_member_name'],
'name' => $row['first_member_name'],
'id' => empty($row['id_member']) ? 0 : $row['id_member'],
'href' => $f_post_mem_href,
'link' => !empty($row['id_member']) ? '<a onclick="getMcard('.$row['id_member'].', $(this));return(false);" href="' . $f_post_mem_href . '" title="' . $txt['profile_of'] . ' ' . $row['first_member_name'] . '">' . $row['first_member_name'] . '</a>' : $row['first_member_name'],
),
'time' => timeformat($row['first_poster_time']),
'timestamp' => forum_time(true, $row['first_poster_time']),
'subject' => $row['subject'],
'icon' => $row['first_icon'],
'icon_url' => getPostIcon($row['first_icon']),
'href' => $t_href,
'link' => '<a href="' . $t_href .'">' . $row['subject'] . '</a>'
),
'last_post' => array(
'id' => $row['id_last_msg'],
'member' => array(
'username' => $row['last_real_name'],
'name' => $row['last_real_name'],
'id' => $row['id_member_updated'],
'href' => $l_post_mem_href,
'link' => !empty($row['id_member_updated']) ? '<a onclick="getMcard('.$row['id_member_updated'].', $(this));return(false);" href="' . $l_post_mem_href . '">' . $row['last_real_name'] . '</a>' : $row['last_real_name']
),
'time' => timeformat($row['last_post_time']),
'timestamp' => forum_time(true, $row['last_post_time']),
'subject' => $row['last_subject'],
'href' => $l_post_msg_href,
'link' => '<a href="' . $l_post_msg_href . ($row['num_replies'] == 0 ? '' : ' rel="nofollow"') . '>' . $row['last_subject'] . '</a>'
),
'subject' => $row['subject'],
'new' => $row['new_from'] <= $row['id_msg_modified'],
'new_from' => $row['new_from'],
'newtime' => $row['new_from'],
'updated' => timeformat($row['poster_time']),
'new_href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . '#new',
'new_link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . '#new">' . $row['subject'] . '</a>',
'replies' => comma_format($row['num_replies']),
'views' => comma_format($row['num_views']),
'approved' => $row['approved'],
'unapproved_posts' => $row['unapproved_posts'],
'is_old' => !empty($modSettings['oldTopicDays']) ? (($context['time_now'] - $row['last_post_time']) > ($modSettings['oldTopicDays'] * 86400)) : false,
'is_posted_in' => false,
'prefix' => '',
'pages' => '',
'is_sticky' => !empty($modSettings['enableStickyTopics']) && !empty($row['is_sticky']),
'is_locked' => !empty($row['locked']),
'is_poll' => false,
'is_hot' => $row['num_replies'] >= $modSettings['hotTopicPosts'],
'is_very_hot' => $row['num_replies'] >= $modSettings['hotTopicVeryPosts'],
'board' => isset($row['id_board']) && !empty($row['id_board']) ? array(
'name' => $row['board_name'],
'id' => $row['id_board'],
'href' => URL::board($row['id_board'], $row['board_name'])
) : array(
'name' => '',
'id' => 0,
'href' => ''
)
);
//determineTopicClass($this->topiclist[$row['id_topic']]);
if(!empty($row['id_member']) && $row['id_member'] != $user_info['id'])
$this->users_to_load[$row['id_member']] = $row['id_member'];
}
loadMemberData($this->users_to_load);
foreach($this->topiclist as &$topic) {
if(!isset($memberContext[$topic['id_member_started']]))
loadMemberContext($topic['id_member_started']);
$topic['first_post']['member']['avatar'] = &$memberContext[$topic['id_member_started']]['avatar']['image'];
}
// figure out whether we have posted in a topic (but only if we are not the topic starter)
if (!empty($modSettings['enableParticipation']) && !$user_info['is_guest'] && !empty($this->topic_ids))
{
$result = smf_db_query( '
SELECT id_topic
FROM {db_prefix}messages
WHERE id_topic IN ({array_int:topic_list})
AND id_member = {int:current_member}
GROUP BY id_topic
LIMIT ' . count($this->topic_ids),
array(
'current_member' => $user_info['id'],
'topic_list' => $this->topic_ids,
)
);
while ($row = mysql_fetch_assoc($result)) {
if($this->topiclist[$row['id_topic']]['first_post']['member']['id'] != $user_info['id'])
$this->topiclist[$row['id_topic']]['is_posted_in'] = true;
}
mysql_free_result($result);
}
}
public function &getResult() {
return $this->topiclist;
}
}
?>