-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathurl.php
More file actions
1614 lines (1327 loc) · 47.7 KB
/
Copy pathurl.php
File metadata and controls
1614 lines (1327 loc) · 47.7 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
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
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* This file is part of Google SEO plugin for MyBB.
* Copyright (C) 2008-2011 Andreas Klauer <Andreas.Klauer@metamorpher.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
die("Direct initialization of this file is not allowed.<br /><br />
Please make sure IN_MYBB is defined.");
}
/* --- Global Variables: --- */
global $db, $mybb, $settings, $plugins, $cache;
// Required for database queries to the google_seo table. In theory this
// could be used to coerce Google SEO into managing URLs of other types.
// In practice there is no guarantee that this API will stay stable.
$db->google_seo_url = array(
GOOGLE_SEO_USER => array(
'table' => TABLE_PREFIX.'users',
'id' => 'uid',
'name' => 'username',
'scheme' => str_replace('&', '&', $settings['google_seo_url_users']),
),
GOOGLE_SEO_ANNOUNCEMENT => array(
'table' => TABLE_PREFIX.'announcements',
'id' => 'aid',
'name' => 'subject',
'scheme' => str_replace('&', '&', $settings['google_seo_url_announcements']),
),
GOOGLE_SEO_FORUM => array(
'table' => TABLE_PREFIX.'forums',
'id' => 'fid',
'name' => 'name',
'scheme' => str_replace('&', '&', $settings['google_seo_url_forums']),
),
GOOGLE_SEO_THREAD => array(
'table' => TABLE_PREFIX.'threads',
'id' => 'tid',
'name' => 'subject',
'scheme' => str_replace('&', '&', $settings['google_seo_url_threads']),
),
GOOGLE_SEO_EVENT => array(
'table' => TABLE_PREFIX.'events',
'id' => 'eid',
'name' => 'name',
'scheme' => str_replace('&', '&', $settings['google_seo_url_events']),
),
GOOGLE_SEO_CALENDAR => array(
'table' => TABLE_PREFIX.'calendars',
'id' => 'cid',
'name' => 'name',
'scheme' => str_replace('&', '&', $settings['google_seo_url_calendars']),
),
);
// Lazy Mode.
global $google_seo_url_lazy;
$google_seo_url_lazy = false;
if($settings['google_seo_url_mode'] == 'lazy'
&& $mybb->request_method != 'post')
{
$google_seo_url_lazy = random_str(4);
$db->google_seo_url[GOOGLE_SEO_ANNOUNCEMENT]['lazy'] = "announcements.php?aid={id}&google_seo={$google_seo_url_lazy}";
$db->google_seo_url[GOOGLE_SEO_CALENDAR]['lazy'] = "calendar.php?calendar={id}&google_seo={$google_seo_url_lazy}";
$db->google_seo_url[GOOGLE_SEO_EVENT]['lazy'] = "calendar.php?action=event&eid={id}&google_seo={$google_seo_url_lazy}";
$db->google_seo_url[GOOGLE_SEO_FORUM]['lazy'] = "forumdisplay.php?fid={id}&google_seo={$google_seo_url_lazy}";
$db->google_seo_url[GOOGLE_SEO_THREAD]['lazy'] = "showthread.php?tid={id}&google_seo={$google_seo_url_lazy}";
$db->google_seo_url[GOOGLE_SEO_USER]['lazy'] = "member.php?action=profile&uid={id}&google_seo={$google_seo_url_lazy}";
$google_seo_url_lazy = true;
}
// Thread Prefix
if($settings['google_seo_url_threadprefix'])
{
$db->google_seo_url[GOOGLE_SEO_THREAD]['extra'] .= ',prefix';
}
// Parents
if($db->google_seo_url[GOOGLE_SEO_FORUM]['scheme'])
{
if($settings['google_seo_url_parent_announcement'])
{
$db->google_seo_url[GOOGLE_SEO_ANNOUNCEMENT]['extra'] .= ',fid AS parent';
$db->google_seo_url[GOOGLE_SEO_ANNOUNCEMENT]['parent'] = $settings['google_seo_url_parent_announcement'];
$db->google_seo_url[GOOGLE_SEO_ANNOUNCEMENT]['parent_type'] = GOOGLE_SEO_FORUM;
}
if($settings['google_seo_url_parent_forum'])
{
$db->google_seo_url[GOOGLE_SEO_FORUM]['extra'] .= ',pid AS parent';
$db->google_seo_url[GOOGLE_SEO_FORUM]['parent'] = $settings['google_seo_url_parent_forum'];
$db->google_seo_url[GOOGLE_SEO_FORUM]['parent_type'] = GOOGLE_SEO_FORUM;
}
if($settings['google_seo_url_parent_thread'])
{
$db->google_seo_url[GOOGLE_SEO_THREAD]['extra'] .= ',fid AS parent';
$db->google_seo_url[GOOGLE_SEO_THREAD]['parent'] = $settings['google_seo_url_parent_thread'];
$db->google_seo_url[GOOGLE_SEO_THREAD]['parent_type'] = GOOGLE_SEO_FORUM;
}
}
if($db->google_seo_url[GOOGLE_SEO_CALENDAR]['scheme']
&& $settings['google_seo_url_parent_event'])
{
$db->google_seo_url[GOOGLE_SEO_EVENT]['extra'] .= ',cid AS parent';
$db->google_seo_url[GOOGLE_SEO_EVENT]['parent'] = $settings['google_seo_url_parent_event'];
$db->google_seo_url[GOOGLE_SEO_EVENT]['parent_type'] = GOOGLE_SEO_CALENDAR;
}
// Query limit. Decreases by 1 for every query.
$db->google_seo_query_limit = (int)$settings['google_seo_url_query_limit'];
if($db->google_seo_query_limit <= 0)
{
$db->google_seo_query_limit = 32767;
}
// Cache
global $google_seo_url_cache;
if($settings['google_seo_url_cache'])
{
$data = $cache->read('google_seo_url');
if(is_array($data) && $data['time'] >= TIME_NOW)
{
$google_seo_url_cache = $data;
}
if(in_array(THIS_SCRIPT, explode(",", $settings['google_seo_url_cache'])))
{
$plugins->add_hook("post_output_page", "google_seo_url_cache_hook", 100);
}
}
// There are several more global variables defined in functions below.
/* --- Hooks: --- */
$plugins->add_hook("moderation_do_merge", "google_seo_url_merge_hook", 1);
$plugins->add_hook("moderation_do_multimoveposts", "google_seo_url_merge_hook", 1);
$plugins->add_hook("class_moderation_merge_threads", "google_seo_url_after_merge_hook");
// Originally a global_start hook, it's called too late for session location
google_seo_url_hook();
/* --- URL processing: --- */
/**
* Character translation for URLs (optional).
*
* @param string The input string
* @return string The output string
*/
function google_seo_url_translation($str)
{
// Required for URL translation.
global $settings;
global $google_seo_url_translation;
if(!is_array($google_seo_url_translation))
{
// Build the translation array.
$google_seo_translation = array();
$lines = explode("\n", $settings['google_seo_url_translation']);
foreach($lines as $line)
{
$fields = explode('=', $line);
if(count($fields) == 2)
{
$key = trim($fields[0]);
if(strlen($key))
{
$value = trim($fields[1]);
$google_seo_url_translation[$key] = $value;
}
}
}
}
if($google_seo_url_translation)
{
$str = strtr($str, $google_seo_url_translation);
}
return $str;
}
/**
* Separate a string by punctuation for use in URLs.
*
* @param string The input string that may contain punctuation
* @return string The string with punctuation replaced with separator
*/
function google_seo_url_separate($str)
{
global $settings;
$pattern = $settings['google_seo_url_punctuation'];
if($pattern)
{
// Escape the pattern.
// (preg_quote breaks UTF-8 and doesn't escape -)
$pattern = preg_replace("/[\\\\\\^\\-\\[\\]\\/]/u",
"\\\\\\0",
$pattern);
// Cut off punctuation at beginning and end.
$str = preg_replace("/^[$pattern]+|[$pattern]+$/u",
"",
$str);
// Replace middle punctuation with one separator.
$str = preg_replace("/[$pattern]+/u",
$settings['google_seo_url_separator'],
$str);
}
return $str;
}
/**
* Truncate too long URLs.
*
* @param string The string to be truncated.
* @param string The word separator.
* @param int The soft limit.
* @param int The hard limit.
* @return string truncated string
*/
function google_seo_url_truncate($str)
{
global $settings;
$separator = $settings['google_seo_url_separator'];
$soft = $settings['google_seo_url_length_soft'];
$hard = $settings['google_seo_url_length_hard'];
// Cut off word past soft limit.
if($soft && my_strlen($str) > $soft)
{
// Search the separator after the soft limit.
$part = my_substr($str, $soft);
$pos = my_strpos($part, $separator);
if($pos === 0 || $pos > 0)
{
$str = my_substr($str, 0, $soft + $pos);
}
}
// Truncate hard limit.
if($hard && my_strlen($str) > $hard)
{
$str = my_substr($str, 0, $hard);
}
return $str;
}
/**
* Uniquify URLs.
*
* @param string The URL that collided with something
* @param int The ID of this item
* @return string The uniquified URL
*/
function google_seo_url_uniquify($url, $id)
{
global $settings;
$separator = $settings['google_seo_url_separator'];
$str = $settings['google_seo_url_uniquifier'];
$str = google_seo_expand($str, array('url' => $url,
'id' => $id,
'separator' => $separator));
return $str;
}
/**
* Finalize URLs. (Lowercase step done by the caller.)
*
* @param string URL to be finalized.
* @param string Scheme to be used for this URL.
* @return string Finalized URL.
*/
function google_seo_url_finalize($url, $scheme)
{
global $settings;
$url = google_seo_expand($scheme, array('url' => $url));
$test = explode('?', $url);
$test = explode('/', $test[0]);
foreach($test as $element)
{
/**
* Maximum URL length hack.
*
* Most hosts do not allow for filenames > 255 bytes in URLs.
* Trying to access such an URL results in 403 Forbidden.
*
* Since MyBB already limits length of user names and topic
* subjects this is normally not a problem. However it is still
* possible to hit the limit with UTF-8 multibyte characters.
*
* This hack causes Google SEO to fall back to stock URLs in
* the rare case where the Google SEO URL would be unuseable.
*
*/
if(strlen($element) >= 256)
{
return 0;
}
}
$url = google_seo_encode($url);
return $url;
}
/* --- URL Creation: --- */
/**
* Create a unique URL in the database for a specific item type,id.
* First fetch the title of the item from the MyBB database,
* then process (translate, separate, truncate, uniquify) that title,
* then check for existing entries in the Google SEO database,
* finally insert it into the database if it's not already there.
*
* @param string Type of the item (forums, threads, etc.)
* @param array IDs of the item (fid for forums, tid for threads, etc.)
*/
function google_seo_url_create($type, $ids)
{
global $db, $settings;
global $google_seo_url_cache;
$ids = array_map('intval', (array)$ids);
foreach($ids as $id)
{
$google_seo_url_cache[$type][$id] = 0;
}
if($db->google_seo_query_limit <= 0)
{
return;
}
$data = $db->google_seo_url[$type];
// Is Google SEO URL enabled for this type?
if($data['scheme'])
{
// Query the item title as base for our URL.
$db->google_seo_query_limit--;
$titles = $db->query("SELECT {$data['name']},{$data['id']}{$data['extra']}
FROM {$data['table']}
WHERE {$data['id']} IN ("
.implode(",", $ids)
.")");
while($row = $db->fetch_array($titles))
{
$url = $row[$data['name']];
// MyBB unfortunately allows HTML in forum names.
if($type == GOOGLE_SEO_FORUM)
{
$url = strip_tags($url);
}
// Thread Prefixes
if($row['prefix'])
{
$prefix = build_prefixes($row['prefix']);
if($prefix['prefix'])
{
$url = google_seo_expand(
$settings['google_seo_url_threadprefix'],
array('url' => $url,
'prefix' => $prefix['prefix'],
'separator' => $settings['google_seo_url_separator']));
}
}
$id = $row[$data['id']];
// Prepare the URL.
if($settings['google_seo_url_translation'])
{
$url = google_seo_url_translation($url);
}
$url = google_seo_url_separate($url);
$url = google_seo_url_truncate($url);
$uniqueurl = google_seo_url_uniquify($url, $id);
// Special case: for empty URLs we must use the unique variant
if($url == "" || $settings['google_seo_url_uniquifier_force'])
{
$url = $uniqueurl;
}
// Parents
if($row['parent'])
{
$parent_type = $db->google_seo_url[$type]['parent_type'];
$parent_id = (int)$row['parent'];
// TODO: Parents costs us an extra query. Cache?
$db->google_seo_query_limit--;
$query = $db->simple_select('google_seo',
'url AS parent',
"active=1 AND idtype={$parent_type} AND id={$parent_id}");
$parent = $db->fetch_field($query, 'parent');
if($parent)
{
$url = google_seo_expand($db->google_seo_url[$type]['parent'],
array('url' => $url,
'parent' => $parent));
$uniqueurl = google_seo_expand($db->google_seo_url[$type]['parent'],
array('url' => $url,
'parent' => $parent));
}
}
// Check for existing entry and possible collisions.
$db->google_seo_query_limit--;
$query = $db->query("SELECT url,id FROM ".TABLE_PREFIX."google_seo
WHERE active=1 AND idtype={$type} AND id<={$id}
AND url IN ('"
.$db->escape_string($url)."','"
.$db->escape_string($uniqueurl)."')
AND EXISTS(SELECT {$data['id']}
FROM {$data['table']}
WHERE {$data['id']}=id)
ORDER BY id ASC");
$urlrow = $db->fetch_array($query);
$uniquerow = $db->fetch_array($query);
// Check if the entry was not up to date anyway.
if($urlrow && $urlrow['id'] == $id && $urlrow['url'] == $url)
{
// It's up to date. Do nothing.
}
else if($uniquerow && $uniquerow['id'] == $id && $uniquerow['url'] == $uniqueurl)
{
// It's up to date for the unique URL.
$url = $uniqueurl;
}
else
{
// Use unique URL if there was a row with a different id.
if($urlrow && $urlrow['id'] != $id)
{
$url = $uniqueurl;
}
// Set old entries for us to not active.
$db->google_seo_query_limit--;
$db->write_query("UPDATE ".TABLE_PREFIX."google_seo
SET active=NULL
WHERE active=1
AND idtype={$type}
AND id={$id}");
// Insert new entry (while possibly replacing old ones).
$db->google_seo_query_limit--;
$db->write_query("REPLACE INTO ".TABLE_PREFIX."google_seo
VALUES (active,idtype,id,url),
('1','{$type}','{$id}','".$db->escape_string($url)."')");
}
// Finalize URL.
if($settings['google_seo_url_lowercase'])
{
$url = my_strtolower($url);
}
$url = google_seo_url_finalize($url, $data['scheme']);
$google_seo_url_cache[$type][$id] = $url;
}
}
}
/* --- URL Cache: --- */
/**
* Optimize queries by collecting as many IDs as possible before sending
* a request off to a database. This is a dirty trick, a hack, black magic.
*
* This list of IDs is obtained by either accessing global data structures
* of MyBB (possible when MyBB stores data in some sort of cache) or by
* querying the IDs from the database (necessary when MyBB doesn't cache
* the data by itself).
*
* The list of IDs obtained here is used later to fetch Google SEO URLs
* in a single query, and create URLs for IDs that do not have an URL yet.
*
* @param string Type of the item that caused the query.
* @param int ID of the item that caused the query.
* @return array IDs (ID as index, value 0) to be merged into the cache.
*/
function google_seo_url_optimize($type, $id)
{
global $db, $mybb, $settings, $cache;
global $google_seo_url_optimize, $google_seo_url_cache;
switch(THIS_SCRIPT)
{
case 'index.php':
// fcache
global $fcache, $google_seo_fcache;
if($fcache !== $google_seo_fcache)
{
$GLOBALS['google_seo_fcache'] =& $fcache;
foreach($fcache as $a)
{
foreach($a as $b)
{
foreach($b as $c)
{
$google_seo_url_optimize[GOOGLE_SEO_FORUM][$c['fid']] = 0;
$google_seo_url_optimize[GOOGLE_SEO_USER][$c['lastposteruid']] = 0;
$google_seo_url_optimize[GOOGLE_SEO_THREAD][$c['lastposttid']] = 0;
}
}
}
}
global $google_seo_index;
if(!$google_seo_index)
{
$google_seo_index = true;
// last user
$stats = $cache->read("stats");
$google_seo_url_optimize[GOOGLE_SEO_USER][$stats['lastuid']] = 0;
// who's online
if($mybb->settings['showwol'] != 0 && $mybb->usergroup['canviewonline'] != 0)
{
$timesearch = TIME_NOW - $mybb->settings['wolcutoff'];
$db->google_seo_query_limit--;
$query = $db->query("SELECT uid FROM ".TABLE_PREFIX."sessions
WHERE uid != '0' AND time > '$timesearch'");
while($user = $db->fetch_array($query))
{
$google_seo_url_optimize[GOOGLE_SEO_USER][$user['uid']] = 0;
}
}
}
break;
case 'portal.php':
global $query, $google_seo_portal_query;
// Hack: Let's hijack queries made by MyBB. Do not try this at home!
if($query !== $google_seo_portal_query)
{
$GLOBALS['google_seo_portal_query'] &= $query;
if($query)
{
$num_rows = $db->num_rows($query);
}
if($num_rows)
{
// Loop from current pointer to end of query.
$i = 0;
while($row = $db->fetch_array($query))
{
$i++;
$google_seo_url_optimize[GOOGLE_SEO_USER][$row['uid']] = 0;
$google_seo_url_optimize[GOOGLE_SEO_USER][$row['lastposteruid']] = 0;
$google_seo_url_optimize[GOOGLE_SEO_THREAD][$row['tid']] = 0;
$google_seo_url_optimize[GOOGLE_SEO_FORUM][$row['fid']] = 0;
$google_seo_url_optimize[GOOGLE_SEO_ANNOUNCEMENT][$row['aid']] = 0;
}
// Determine original pointer position.
$pointer = $num_rows - $i;
// Seek to beginning of query.
$db->data_seek($query, 0);
// Loop from beginning until we reach the original position.
$i = 0;
while($i < $pointer && $row = $db->fetch_array($query))
{
$i++;
$google_seo_url_optimize[GOOGLE_SEO_USER][$row['uid']] = 0;
$google_seo_url_optimize[GOOGLE_SEO_USER][$row['lastposteruid']] = 0;
$google_seo_url_optimize[GOOGLE_SEO_THREAD][$row['tid']] = 0;
$google_seo_url_optimize[GOOGLE_SEO_FORUM][$row['fid']] = 0;
$google_seo_url_optimize[GOOGLE_SEO_ANNOUNCEMENT][$row['aid']] = 0;
}
}
}
break;
case 'forumdisplay.php':
// forum_cache
global $forum_cache, $google_seo_forum_cache;
if($forum_cache !== $google_seo_forum_cache)
{
$GLOBALS['google_seo_forum_cache'] =& $forum_cache;
foreach($forum_cache as $f)
{
$google_seo_url_optimize[GOOGLE_SEO_FORUM][$f['fid']] = 0;
$google_seo_url_optimize[GOOGLE_SEO_USER][$f['lastposteruid']] = 0;
}
}
// threadcache optimization (forumdisplay.php)
global $threadcache, $google_seo_threadcache;
if($threadcache !== $google_seo_threadcache)
{
$GLOBALS['google_seo_threadcache'] =& $threadcache;
foreach($threadcache as $t)
{
$google_seo_url_optimize[GOOGLE_SEO_THREAD][$t['tid']] = 0;
$google_seo_url_optimize[GOOGLE_SEO_FORUM][$t['fid']] = 0;
$google_seo_url_optimize[GOOGLE_SEO_USER][$t['uid']] = 0;
$google_seo_url_optimize[GOOGLE_SEO_USER][$t['lastposteruid']] = 0;
}
}
break;
case 'search.php':
// thread_cache
global $thread_cache, $google_seo_thread_cache;
if($thread_cache !== $google_seo_thread_cache)
{
$GLOBALS['google_seo_thread_cache'] =& $thread_cache;
foreach($thread_cache as $t)
{
$google_seo_url_optimize[GOOGLE_SEO_THREAD][$t['tid']] = 0;
$google_seo_url_optimize[GOOGLE_SEO_FORUM][$t['fid']] = 0;
$google_seo_url_optimize[GOOGLE_SEO_USER][$t['uid']] = 0;
$google_seo_url_optimize[GOOGLE_SEO_USER][$t['lastposteruid']] = 0;
}
}
break;
case 'calendar.php':
// events_cache
global $events_cache, $google_seo_events_cache;
if($events_cache !== $google_seo_events_cache)
{
$GLOBALS['google_seo_events_cache'] =& $events_cache;
foreach($events_cache as $d)
{
foreach($d as $e)
{
$google_seo_url_optimize[GOOGLE_SEO_USER][$e['uid']] = 0;
$google_seo_url_optimize[GOOGLE_SEO_EVENT][$e['eid']] = 0;
$google_seo_url_optimize[GOOGLE_SEO_CALENDAR][$e['cid']] = 0;
}
}
}
break;
case 'online.php':
if($settings['google_seo_url_wol'])
{
global $uid_list, $aid_list, $eid_list, $fid_list, $tid_list;
// uid_list
if(count($uid_list) && $uid_list !== $google_seo_uid_list)
{
$GLOBALS['google_seo_uid_list'] =& $uid_list;
foreach($uid_list as $uid)
{
$google_seo_url_optimize[GOOGLE_SEO_USER][$uid] = 0;
}
}
// aid_list
if(count($aid_list) && $aid_list !== $google_seo_aid_list)
{
$GLOBALS['google_seo_aid_list'] =& $aid_list;
foreach($aid_list as $aid)
{
$google_seo_url_optimize[GOOGLE_SEO_ANNOUNCEMENT][$aid] = 0;
}
}
// eid_list
if(count($eid_list) && $eid_list !== $google_seo_eid_list)
{
$GLOBALS['google_seo_eid_list'] =& $eid_list;
foreach($eid_list as $eid)
{
$google_seo_url_optimize[GOOGLE_SEO_EVENT][$eid] = 0;
}
}
// fid_list
if(count($fid_list) && $fid_list !== $google_seo_fid_list)
{
$GLOBALS['google_seo_fid_list'] =& $fid_list;
foreach($fid_list as $fid)
{
$google_seo_url_optimize[GOOGLE_SEO_FORUM][$fid] = 0;
}
}
// tid_list
if(count($tid_list) && $tid_list !== $google_seo_tid_list)
{
$GLOBALS['google_seo_tid_list'] =& $tid_list;
foreach($tid_list as $tid)
{
$google_seo_url_optimize[GOOGLE_SEO_THREAD][$tid] = 0;
}
}
}
break;
}
// Include the ID that was originally requested.
$google_seo_url_optimize[$type][$id] = 0;
// Clean up
foreach($google_seo_url_optimize as $key => $value)
{
if(!$db->google_seo_url[$key]['scheme'])
{
unset($google_seo_url_optimize[$key]);
}
unset($google_seo_url_optimize[$key][0]);
unset($google_seo_url_optimize[$key]['']);
}
// return the collected IDs
$result = $google_seo_url_optimize;
$google_seo_url_optimize = array();
return $result;
}
/**
* Get the entry for $type, $id from the cache.
*
* If $id is not yet in the cache, the $id will be queried from the DB.
* To keep number of total DB queries low, all $ids that have been queued
* up to here will be fetched in the same query as well.
*
*/
function google_seo_url_cache($type, $id)
{
global $db, $settings;
global $google_seo_url_cache, $google_seo_url_lazy;
// If it's not in the cache, try loading the cache.
if($google_seo_url_cache[$type][$id] === NULL
&& $db->google_seo_query_limit > 0)
{
$google_seo_url_cache['dirty'] = 1;
// Special case: Lazy Mode
if($google_seo_url_lazy !== false)
{
if($google_seo_url_lazy === true)
{
// first time call
global $plugins;
$plugins->add_hook('pre_output_page', 'google_seo_url_lazy', 1000);
ob_start('google_seo_url_lazy');
register_shutdown_function(create_function('', 'while(@ob_end_flush());'));
$google_seo_url_lazy = array();
}
// Create a temporary placeholder (but working) URL.
$url = google_seo_expand(
$db->google_seo_url[$type]['lazy'],
array('id' => $id)
);
// strtr is SO much faster if all strings have the exact same length
// 64 is the longest lazy url assuming 10 digit ids
$url = str_pad($url, 64, "+");
$google_seo_url_cache[$type][$id] = $url;
$google_seo_url_lazy[$type][$id] = $url;
return $url;
}
// Full Mode
// Prepare database query.
if($settings["google_seo_url_lowercase"])
{
$what = "LOWER(url) AS url,id,idtype";
}
else
{
$what = "url,id,idtype";
}
// Optimize by collecting more IDs for this query.
$ids = google_seo_url_optimize($type, $id);
$condition = array();
foreach($ids as $key => $value)
{
if($value)
{
$condition[] = "(idtype={$key} AND id IN ("
.implode(",", array_map('intval', array_keys($value))).
"))";
}
}
$condition = implode(" OR ", $condition);
// Run database query.
$db->google_seo_query_limit--;
$query = $db->query("SELECT $what
FROM ".TABLE_PREFIX."google_seo
WHERE active=1
AND ({$condition})");
// Process the query results.
while($row = $db->fetch_array($query))
{
$rowid = (int)$row['id'];
$rowtype = (int)$row['idtype'];
$google_seo_url_cache[$rowtype][$rowid] =
google_seo_url_finalize($row['url'],
$db->google_seo_url[$rowtype]['scheme']);
unset($ids[$rowtype][$rowid]);
}
// Create URLs for the remaining IDs.
foreach($ids as $key => $value)
{
unset($value[0]);
if(count($value))
{
google_seo_url_create($key, array_keys($value));
}
}
}
// Return the cached entry for the originally requested type and id.
return $google_seo_url_cache[$type][$id];
}
/*
* Lazy Mode
*/
function google_seo_url_lazy($message)
{
global $plugins;
global $google_seo_url_lazy, $google_seo_url_optimize, $google_seo_url_cache;
if(!$google_seo_url_lazy)
{
// Do nothing.
return $message;
}
$lazy = $google_seo_url_lazy;
$google_seo_url_lazy = false;
// We've been lazy and now we have to pay the price.
// Prepare cache and optimize.
foreach($lazy as $key => $value)
{
// substract lazy from cache so it will redo these URLs
$google_seo_url_cache[$key] = array_diff_key((array)$google_seo_url_cache[$key],
(array)$lazy[$key]);
// add lazy to optimize so it will use a single query when redoing them
$google_seo_url_optimize[$key] = (array)$google_seo_url_optimize[$key] + (array)$lazy[$key];
}
// Make it load the URLs.
reset($lazy);
$type = key($lazy);
reset($lazy[$type]);
$id = key($lazy[$type]);
google_seo_url_cache($type, $id);
// Build the replacement foo.
$strtr = array();
foreach($lazy as $type => $list)
{
foreach($list as $id => $url)
{
$seourl = $google_seo_url_cache[$type][$id];
if($seourl)
{
$strtr[$url] = $seourl;
}
}
}
// This is the most expensive bit.
$message = strtr($message, $strtr);
return $message;
}
/*
* Populate MyBB's Cache
*/
function google_seo_url_cache_hook()
{
global $settings, $cache;
global $google_seo_url_cache;
// Do we need to update the cache?
if($google_seo_url_cache['dirty'])
{