/
functions.php
9500 lines (8349 loc) · 222 KB
/
functions.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
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
/**
* MyBB 1.8
* Copyright 2014 MyBB Group, All Rights Reserved
*
* Website: http://www.mybb.com
* License: http://www.mybb.com/about/license
*
*/
/**
* Outputs a page directly to the browser, parsing anything which needs to be parsed.
*
* @param string $contents The contents of the page.
*/
function output_page($contents)
{
global $db, $lang, $theme, $templates, $plugins, $mybb;
global $debug, $templatecache, $templatelist, $maintimer, $globaltime, $parsetime;
$contents = $plugins->run_hooks("pre_parse_page", $contents);
$contents = parse_page($contents);
$totaltime = format_time_duration($maintimer->stop());
$contents = $plugins->run_hooks("pre_output_page", $contents);
if($mybb->usergroup['cancp'] == 1 || $mybb->dev_mode == 1)
{
if($mybb->settings['extraadmininfo'] != 0)
{
$phptime = $maintimer->totaltime - $db->query_time;
$query_time = $db->query_time;
if($maintimer->totaltime > 0)
{
$percentphp = number_format((($phptime/$maintimer->totaltime) * 100), 2);
$percentsql = number_format((($query_time/$maintimer->totaltime) * 100), 2);
}
else
{
// if we've got a super fast script... all we can do is assume something
$percentphp = 0;
$percentsql = 0;
}
$serverload = get_server_load();
if(my_strpos(getenv("REQUEST_URI"), "?"))
{
$debuglink = htmlspecialchars_uni(getenv("REQUEST_URI")) . "&debug=1";
}
else
{
$debuglink = htmlspecialchars_uni(getenv("REQUEST_URI")) . "?debug=1";
}
$memory_usage = get_memory_usage();
if($memory_usage)
{
$memory_usage = $lang->sprintf($lang->debug_memory_usage, get_friendly_size($memory_usage));
}
else
{
$memory_usage = '';
}
// MySQLi is still MySQL, so present it that way to the user
$database_server = $db->short_title;
if($database_server == 'MySQLi')
{
$database_server = 'MySQL';
}
$generated_in = $lang->sprintf($lang->debug_generated_in, $totaltime);
$debug_weight = $lang->sprintf($lang->debug_weight, $percentphp, $percentsql, $database_server);
$sql_queries = $lang->sprintf($lang->debug_sql_queries, $db->query_count);
$server_load = $lang->sprintf($lang->debug_server_load, $serverload);
eval("\$debugstuff = \"".$templates->get("debug_summary")."\";");
$contents = str_replace("<debugstuff>", $debugstuff, $contents);
}
if($mybb->debug_mode == true)
{
debug_page();
}
}
$contents = str_replace("<debugstuff>", "", $contents);
if($mybb->settings['gzipoutput'] == 1)
{
$contents = gzip_encode($contents, $mybb->settings['gziplevel']);
}
@header("Content-type: text/html; charset={$lang->settings['charset']}");
echo $contents;
$plugins->run_hooks("post_output_page");
}
/**
* Adds a function or class to the list of code to run on shutdown.
*
* @param string|array $name The name of the function.
* @param mixed $arguments Either an array of arguments for the function or one argument
* @return boolean True if function exists, otherwise false.
*/
function add_shutdown($name, $arguments=array())
{
global $shutdown_functions;
if(!is_array($shutdown_functions))
{
$shutdown_functions = array();
}
if(!is_array($arguments))
{
$arguments = array($arguments);
}
if(is_array($name) && method_exists($name[0], $name[1]))
{
$shutdown_functions[] = array('function' => $name, 'arguments' => $arguments);
return true;
}
else if(!is_array($name) && function_exists($name))
{
$shutdown_functions[] = array('function' => $name, 'arguments' => $arguments);
return true;
}
return false;
}
/**
* Runs the shutdown items after the page has been sent to the browser.
*
*/
function run_shutdown()
{
global $config, $db, $cache, $plugins, $error_handler, $shutdown_functions, $shutdown_queries, $done_shutdown, $mybb;
if($done_shutdown == true || !$config || (isset($error_handler) && $error_handler->has_errors))
{
return;
}
if(empty($shutdown_queries) && empty($shutdown_functions))
{
// Nothing to do
return;
}
// Missing the core? Build
if(!is_object($mybb))
{
require_once MYBB_ROOT."inc/class_core.php";
$mybb = new MyBB;
// Load the settings
require MYBB_ROOT."inc/settings.php";
$mybb->settings = &$settings;
}
// If our DB has been deconstructed already (bad PHP 5.2.0), reconstruct
if(!is_object($db))
{
if(!isset($config) || empty($config['database']['type']))
{
require MYBB_ROOT."inc/config.php";
}
if(isset($config))
{
// Load DB interface
require_once MYBB_ROOT."inc/db_base.php";
require_once MYBB_ROOT . 'inc/AbstractPdoDbDriver.php';
require_once MYBB_ROOT."inc/db_".$config['database']['type'].".php";
switch($config['database']['type'])
{
case "sqlite":
$db = new DB_SQLite;
break;
case "pgsql":
$db = new DB_PgSQL;
break;
case "pgsql_pdo":
$db = new PostgresPdoDbDriver();
break;
case "mysqli":
$db = new DB_MySQLi;
break;
case "mysql_pdo":
$db = new MysqlPdoDbDriver();
break;
default:
$db = new DB_MySQL;
}
$db->connect($config['database']);
if(!defined("TABLE_PREFIX"))
{
define("TABLE_PREFIX", $config['database']['table_prefix']);
}
$db->set_table_prefix(TABLE_PREFIX);
}
}
// Cache object deconstructed? reconstruct
if(!is_object($cache))
{
require_once MYBB_ROOT."inc/class_datacache.php";
$cache = new datacache;
$cache->cache();
}
// And finally.. plugins
if(!is_object($plugins) && !defined("NO_PLUGINS") && !($mybb->settings['no_plugins'] == 1))
{
require_once MYBB_ROOT."inc/class_plugins.php";
$plugins = new pluginSystem;
$plugins->load();
}
// We have some shutdown queries needing to be run
if(is_array($shutdown_queries))
{
// Loop through and run them all
foreach($shutdown_queries as $query)
{
$db->write_query($query);
}
}
// Run any shutdown functions if we have them
if(is_array($shutdown_functions))
{
foreach($shutdown_functions as $function)
{
call_user_func_array($function['function'], $function['arguments']);
}
}
$done_shutdown = true;
}
/**
* Sends a specified amount of messages from the mail queue
*
* @param int $count The number of messages to send (Defaults to 10)
*/
function send_mail_queue($count=10)
{
global $db, $cache, $plugins;
$plugins->run_hooks("send_mail_queue_start");
// Check to see if the mail queue has messages needing to be sent
$mailcache = $cache->read("mailqueue");
if($mailcache !== false && $mailcache['queue_size'] > 0 && ($mailcache['locked'] == 0 || $mailcache['locked'] < TIME_NOW-300))
{
// Lock the queue so no other messages can be sent whilst these are (for popular boards)
$cache->update_mailqueue(0, TIME_NOW);
// Fetch emails for this page view - and send them
$query = $db->simple_select("mailqueue", "*", "", array("order_by" => "mid", "order_dir" => "asc", "limit_start" => 0, "limit" => $count));
while($email = $db->fetch_array($query))
{
// Delete the message from the queue
$db->delete_query("mailqueue", "mid='{$email['mid']}'");
if($db->affected_rows() == 1)
{
my_mail($email['mailto'], $email['subject'], $email['message'], $email['mailfrom'], "", $email['headers'], true);
}
}
// Update the mailqueue cache and remove the lock
$cache->update_mailqueue(TIME_NOW, 0);
}
$plugins->run_hooks("send_mail_queue_end");
}
/**
* Parses the contents of a page before outputting it.
*
* @param string $contents The contents of the page.
* @return string The parsed page.
*/
function parse_page($contents)
{
global $lang, $theme, $mybb, $htmldoctype, $archive_url, $error_handler;
$contents = str_replace('<navigation>', build_breadcrumb(), $contents);
$contents = str_replace('<archive_url>', $archive_url, $contents);
if($htmldoctype)
{
$contents = $htmldoctype.$contents;
}
else
{
$contents = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n".$contents;
}
$contents = str_replace("<html", "<html xmlns=\"http://www.w3.org/1999/xhtml\"", $contents);
if($lang->settings['rtl'] == 1)
{
$contents = str_replace("<html", "<html dir=\"rtl\"", $contents);
}
if($lang->settings['htmllang'])
{
$contents = str_replace("<html", "<html xml:lang=\"".$lang->settings['htmllang']."\" lang=\"".$lang->settings['htmllang']."\"", $contents);
}
if($error_handler->warnings)
{
$contents = str_replace("<body>", "<body>\n".$error_handler->show_warnings(), $contents);
}
return $contents;
}
/**
* Turn a unix timestamp in to a "friendly" date/time format for the user.
*
* @param string $format A date format (either relative, normal or PHP's date() structure).
* @param int $stamp The unix timestamp the date should be generated for.
* @param int|string $offset The offset in hours that should be applied to times. (timezones) Or an empty string to determine that automatically
* @param int $ty Whether or not to use today/yesterday formatting.
* @param boolean $adodb Whether or not to use the adodb time class for < 1970 or > 2038 times
* @return string The formatted timestamp.
*/
function my_date($format, $stamp=0, $offset="", $ty=1, $adodb=false)
{
global $mybb, $lang, $plugins;
// If the stamp isn't set, use TIME_NOW
if(empty($stamp))
{
$stamp = TIME_NOW;
}
if(!$offset && $offset != '0')
{
if(isset($mybb->user['uid']) && $mybb->user['uid'] != 0 && array_key_exists("timezone", $mybb->user))
{
$offset = (float)$mybb->user['timezone'];
$dstcorrection = $mybb->user['dst'];
}
else
{
$offset = (float)$mybb->settings['timezoneoffset'];
$dstcorrection = $mybb->settings['dstcorrection'];
}
// If DST correction is enabled, add an additional hour to the timezone.
if($dstcorrection == 1)
{
++$offset;
if(my_substr($offset, 0, 1) != "-")
{
$offset = "+".$offset;
}
}
}
if($offset == "-")
{
$offset = 0;
}
// Using ADOdb?
if($adodb == true && !function_exists('adodb_date'))
{
$adodb = false;
}
$todaysdate = $yesterdaysdate = '';
if($ty && ($format == $mybb->settings['dateformat'] || $format == 'relative' || $format == 'normal'))
{
$_stamp = TIME_NOW;
if($adodb == true)
{
$date = adodb_date($mybb->settings['dateformat'], $stamp + ($offset * 3600));
$todaysdate = adodb_date($mybb->settings['dateformat'], $_stamp + ($offset * 3600));
$yesterdaysdate = adodb_date($mybb->settings['dateformat'], ($_stamp - 86400) + ($offset * 3600));
}
else
{
$date = gmdate($mybb->settings['dateformat'], $stamp + ($offset * 3600));
$todaysdate = gmdate($mybb->settings['dateformat'], $_stamp + ($offset * 3600));
$yesterdaysdate = gmdate($mybb->settings['dateformat'], ($_stamp - 86400) + ($offset * 3600));
}
}
if($format == 'relative')
{
// Relative formats both date and time
$real_date = $real_time = '';
if($adodb == true)
{
$real_date = adodb_date($mybb->settings['dateformat'], $stamp + ($offset * 3600));
$real_time = $mybb->settings['datetimesep'];
$real_time .= adodb_date($mybb->settings['timeformat'], $stamp + ($offset * 3600));
}
else
{
$real_date = gmdate($mybb->settings['dateformat'], $stamp + ($offset * 3600));
$real_time = $mybb->settings['datetimesep'];
$real_time .= gmdate($mybb->settings['timeformat'], $stamp + ($offset * 3600));
}
if($ty != 2 && abs(TIME_NOW - $stamp) < 3600)
{
$diff = TIME_NOW - $stamp;
$relative = array('prefix' => '', 'minute' => 0, 'plural' => $lang->rel_minutes_plural, 'suffix' => $lang->rel_ago);
if($diff < 0)
{
$diff = abs($diff);
$relative['suffix'] = '';
$relative['prefix'] = $lang->rel_in;
}
$relative['minute'] = floor($diff / 60);
if($relative['minute'] <= 1)
{
$relative['minute'] = 1;
$relative['plural'] = $lang->rel_minutes_single;
}
if($diff <= 60)
{
// Less than a minute
$relative['prefix'] = $lang->rel_less_than;
}
$date = $lang->sprintf($lang->rel_time, $relative['prefix'], $relative['minute'], $relative['plural'], $relative['suffix'], $real_date, $real_time);
}
elseif($ty != 2 && abs(TIME_NOW - $stamp) < 43200)
{
$diff = TIME_NOW - $stamp;
$relative = array('prefix' => '', 'hour' => 0, 'plural' => $lang->rel_hours_plural, 'suffix' => $lang->rel_ago);
if($diff < 0)
{
$diff = abs($diff);
$relative['suffix'] = '';
$relative['prefix'] = $lang->rel_in;
}
$relative['hour'] = floor($diff / 3600);
if($relative['hour'] <= 1)
{
$relative['hour'] = 1;
$relative['plural'] = $lang->rel_hours_single;
}
$date = $lang->sprintf($lang->rel_time, $relative['prefix'], $relative['hour'], $relative['plural'], $relative['suffix'], $real_date, $real_time);
}
else
{
if($ty)
{
if($todaysdate == $date)
{
$date = $lang->sprintf($lang->today_rel, $real_date);
}
else if($yesterdaysdate == $date)
{
$date = $lang->sprintf($lang->yesterday_rel, $real_date);
}
}
$date .= $mybb->settings['datetimesep'];
if($adodb == true)
{
$date .= adodb_date($mybb->settings['timeformat'], $stamp + ($offset * 3600));
}
else
{
$date .= gmdate($mybb->settings['timeformat'], $stamp + ($offset * 3600));
}
}
}
elseif($format == 'normal')
{
// Normal format both date and time
if($ty != 2)
{
if($todaysdate == $date)
{
$date = $lang->today;
}
else if($yesterdaysdate == $date)
{
$date = $lang->yesterday;
}
}
$date .= $mybb->settings['datetimesep'];
if($adodb == true)
{
$date .= adodb_date($mybb->settings['timeformat'], $stamp + ($offset * 3600));
}
else
{
$date .= gmdate($mybb->settings['timeformat'], $stamp + ($offset * 3600));
}
}
else
{
if($ty && $format == $mybb->settings['dateformat'])
{
if($todaysdate == $date)
{
$date = $lang->today;
}
else if($yesterdaysdate == $date)
{
$date = $lang->yesterday;
}
}
else
{
if($adodb == true)
{
$date = adodb_date($format, $stamp + ($offset * 3600));
}
else
{
$date = gmdate($format, $stamp + ($offset * 3600));
}
}
}
if(is_object($plugins))
{
$date = $plugins->run_hooks("my_date", $date);
}
return $date;
}
/**
* Get a mail handler instance, a MyBB's built-in SMTP / PHP mail hander or one created by a plugin.
* @param bool $use_buitlin Whether to use MyBB's built-in mail handler.
*
* @return object A MyBB built-in mail handler or one created by plugin(s).
*/
function &get_my_mailhandler($use_buitlin = false)
{
global $mybb, $plugins;
static $my_mailhandler;
static $my_mailhandler_builtin;
if($use_buitlin)
{
// If our built-in mail handler doesn't exist, create it.
if(!is_object($my_mailhandler_builtin))
{
require_once MYBB_ROOT . "inc/class_mailhandler.php";
// Using SMTP.
if(isset($mybb->settings['mail_handler']) && $mybb->settings['mail_handler'] == 'smtp')
{
require_once MYBB_ROOT . "inc/mailhandlers/smtp.php";
$my_mailhandler_builtin = new SmtpMail();
}
// Using PHP mail().
else
{
require_once MYBB_ROOT . "inc/mailhandlers/php.php";
$my_mailhandler_builtin = new PhpMail();
if(!empty($mybb->config['mail_parameters']))
{
$my_mailhandler_builtin->additional_parameters = $mybb->config['mail_parameters'];
}
}
}
if(isset($plugins) && is_object($plugins))
{
$plugins->run_hooks('my_mailhandler_builtin_after_init', $my_mailhandler_builtin);
}
return $my_mailhandler_builtin;
}
// If our mail handler doesn't exist, create it.
if(!is_object($my_mailhandler))
{
require_once MYBB_ROOT . "inc/class_mailhandler.php";
if(isset($plugins) && is_object($plugins))
{
$plugins->run_hooks('my_mailhandler_init', $my_mailhandler);
}
// If no plugin has ever created the mail handler, resort to use the built-in one.
if(!is_object($my_mailhandler) || !($my_mailhandler instanceof MailHandler))
{
$my_mailhandler = &get_my_mailhandler(true);
}
}
return $my_mailhandler;
}
/**
* Sends an email using PHP's mail function, formatting it appropriately.
*
* @param string $to Address the email should be addressed to.
* @param string $subject The subject of the email being sent.
* @param string $message The message being sent.
* @param string $from The from address of the email, if blank, the board name will be used.
* @param string $charset The chracter set being used to send this email.
* @param string $headers
* @param boolean $keep_alive Do we wish to keep the connection to the mail server alive to send more than one message (SMTP only)
* @param string $format The format of the email to be sent (text or html). text is default
* @param string $message_text The text message of the email if being sent in html format, for email clients that don't support html
* @param string $return_email The email address to return to. Defaults to admin return email address.
* @return bool True if the mail is sent, false otherwise.
*/
function my_mail($to, $subject, $message, $from="", $charset="", $headers="", $keep_alive=false, $format="text", $message_text="", $return_email="")
{
global $mybb, $plugins;
// Get our mail handler.
$mail = &get_my_mailhandler();
// If MyBB's built-in SMTP mail handler is used, set the keep alive bit accordingly.
if($keep_alive == true && isset($mail->keep_alive) && isset($mybb->settings['mail_handler']) && $mybb->settings['mail_handler'] == 'smtp')
{
require_once MYBB_ROOT . "inc/class_mailhandler.php";
require_once MYBB_ROOT . "inc/mailhandlers/smtp.php";
if($mail instanceof MailHandler && $mail instanceof SmtpMail)
{
$mail->keep_alive = true;
}
}
// Following variables will help sequential plugins to determine how to process plugin hooks.
// Mark this variable true if the hooked plugin has sent the mail, otherwise don't modify it.
$is_mail_sent = false;
// Mark this variable false if the hooked plugin doesn't suggest sequential plugins to continue processing.
$continue_process = true;
$my_mail_parameters = array(
'to' => &$to,
'subject' => &$subject,
'message' => &$message,
'from' => &$from,
'charset' => &$charset,
'headers' => &$headers,
'keep_alive' => &$keep_alive,
'format' => &$format,
'message_text' => &$message_text,
'return_email' => &$return_email,
'is_mail_sent' => &$is_mail_sent,
'continue_process' => &$continue_process,
);
if(isset($plugins) && is_object($plugins))
{
$plugins->run_hooks('my_mail_pre_build_message', $my_mail_parameters);
}
// Build the mail message.
$mail->build_message($to, $subject, $message, $from, $charset, $headers, $format, $message_text, $return_email);
if(isset($plugins) && is_object($plugins))
{
$plugins->run_hooks('my_mail_pre_send', $my_mail_parameters);
}
// Check if the hooked plugins still suggest to send the mail.
if($continue_process)
{
$is_mail_sent = $mail->send();
}
if(isset($plugins) && is_object($plugins))
{
$plugins->run_hooks('my_mail_post_send', $my_mail_parameters);
}
return $is_mail_sent;
}
/**
* Generates a code for POST requests to prevent XSS/CSRF attacks.
* Unique for each user or guest session and rotated every 6 hours.
*
* @param int $rotation_shift Adjustment of the rotation number to generate a past/future code
* @return string The generated code
*/
function generate_post_check($rotation_shift=0)
{
global $mybb, $session;
$rotation_interval = 6 * 3600;
$rotation = floor(TIME_NOW / $rotation_interval) + $rotation_shift;
$seed = $rotation;
if($mybb->user['uid'])
{
$seed .= $mybb->user['loginkey'].$mybb->user['salt'].$mybb->user['regdate'];
}
else
{
$seed .= $session->sid;
}
if(defined('IN_ADMINCP'))
{
$seed .= 'ADMINCP';
}
$seed .= $mybb->settings['internal']['encryption_key'];
return md5($seed);
}
/**
* Verifies a POST check code is valid (i.e. generated using a rotation number from the past 24 hours)
*
* @param string $code The incoming POST check code
* @param boolean $silent Don't show an error to the user
* @return bool|void Result boolean if $silent is true, otherwise shows an error to the user
*/
function verify_post_check($code, $silent=false)
{
global $lang;
if(
generate_post_check() !== $code &&
generate_post_check(-1) !== $code &&
generate_post_check(-2) !== $code &&
generate_post_check(-3) !== $code
)
{
if($silent == true)
{
return false;
}
else
{
if(defined("IN_ADMINCP"))
{
return false;
}
else
{
error($lang->invalid_post_code);
}
}
}
else
{
return true;
}
}
/**
* Return a parent list for the specified forum.
*
* @param int $fid The forum id to get the parent list for.
* @return string The comma-separated parent list.
*/
function get_parent_list($fid)
{
global $forum_cache;
static $forumarraycache;
if(!empty($forumarraycache[$fid]))
{
return $forumarraycache[$fid]['parentlist'];
}
elseif(!empty($forum_cache[$fid]))
{
return $forum_cache[$fid]['parentlist'];
}
else
{
cache_forums();
return $forum_cache[$fid]['parentlist'];
}
}
/**
* Build a parent list of a specific forum, suitable for querying
*
* @param int $fid The forum ID
* @param string $column The column name to add to the query
* @param string $joiner The joiner for each forum for querying (OR | AND | etc)
* @param string $parentlist The parent list of the forum - if you have it
* @return string The query string generated
*/
function build_parent_list($fid, $column="fid", $joiner="OR", $parentlist="")
{
if(!$parentlist)
{
$parentlist = get_parent_list($fid);
}
$parentsexploded = explode(",", $parentlist);
$builtlist = "(";
$sep = '';
foreach($parentsexploded as $key => $val)
{
$builtlist .= "$sep$column='$val'";
$sep = " $joiner ";
}
$builtlist .= ")";
return $builtlist;
}
/**
* Load the forum cache in to memory
*
* @param boolean $force True to force a reload of the cache
* @return array The forum cache
*/
function cache_forums($force=false)
{
global $forum_cache, $cache;
if($force == true)
{
$forum_cache = $cache->read("forums", 1);
return $forum_cache;
}
if(!$forum_cache)
{
$forum_cache = $cache->read("forums");
if(!$forum_cache)
{
$cache->update_forums();
$forum_cache = $cache->read("forums", 1);
}
}
return $forum_cache;
}
/**
* Generate an array of all child and descendant forums for a specific forum.
*
* @param int $fid The forum ID
* @return Array of descendants
*/
function get_child_list($fid)
{
static $forums_by_parent;
$forums = array();
if(!is_array($forums_by_parent))
{
$forum_cache = cache_forums();
foreach($forum_cache as $forum)
{
if($forum['active'] != 0)
{
$forums_by_parent[$forum['pid']][$forum['fid']] = $forum;
}
}
}
if(isset($forums_by_parent[$fid]))
{
if(!is_array($forums_by_parent[$fid]))
{
return $forums;
}
foreach($forums_by_parent[$fid] as $forum)
{
$forums[] = (int)$forum['fid'];
$children = get_child_list($forum['fid']);
if(is_array($children))
{
$forums = array_merge($forums, $children);
}
}
}
return $forums;
}
/**
* Produce a friendly error message page
*
* @param string $error The error message to be shown
* @param string $title The title of the message shown in the title of the page and the error table
*/
function error($error="", $title="")
{
global $header, $footer, $theme, $headerinclude, $db, $templates, $lang, $mybb, $plugins;
$error = $plugins->run_hooks("error", $error);
if(!$error)
{
$error = $lang->unknown_error;
}
// AJAX error message?
if($mybb->get_input('ajax', MyBB::INPUT_INT))
{
// Send our headers.
@header("Content-type: application/json; charset={$lang->settings['charset']}");
echo json_encode(array("errors" => array($error)));
exit;
}
if(!$title)
{
$title = $mybb->settings['bbname'];
}
$timenow = my_date('relative', TIME_NOW);
reset_breadcrumb();
add_breadcrumb($lang->error);
eval("\$errorpage = \"".$templates->get("error")."\";");
output_page($errorpage);
exit;
}
/**
* Produce an error message for displaying inline on a page
*
* @param array $errors Array of errors to be shown
* @param string $title The title of the error message
* @param array $json_data JSON data to be encoded (we may want to send more data; e.g. newreply.php uses this for CAPTCHA)
* @return string The inline error HTML
*/
function inline_error($errors, $title="", $json_data=array())
{
global $theme, $mybb, $db, $lang, $templates;
if(!$title)
{
$title = $lang->please_correct_errors;
}
if(!is_array($errors))
{
$errors = array($errors);
}
// AJAX error message?
if($mybb->get_input('ajax', MyBB::INPUT_INT))
{
// Send our headers.
@header("Content-type: application/json; charset={$lang->settings['charset']}");
if(empty($json_data))
{
echo json_encode(array("errors" => $errors));
}
else
{
echo json_encode(array_merge(array("errors" => $errors), $json_data));
}
exit;
}
$errorlist = '';
foreach($errors as $error)
{
eval("\$errorlist .= \"".$templates->get("error_inline_item")."\";");
}
eval("\$errors = \"".$templates->get("error_inline")."\";");
return $errors;
}
/**
* Presents the user with a "no permission" page
*/
function error_no_permission()
{
global $mybb, $theme, $templates, $db, $lang, $plugins, $session;
$time = TIME_NOW;
$plugins->run_hooks("no_permission");