-
Notifications
You must be signed in to change notification settings - Fork 439
/
util.inc
1285 lines (1144 loc) · 34.1 KB
/
util.inc
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 BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2021 University of California
//
// BOINC 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.
//
// BOINC 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 BOINC. If not, see <http://www.gnu.org/licenses/>.
// Utility functions for BOINC web pages
// Stuff that's not web-specific (e.g. that's used in non-web scripts)
// should go in util_basic.inc
require_once("../inc/util_basic.inc");
require_once("../project/project.inc");
require_once("../inc/countries.inc");
require_once("../inc/db.inc");
require_once("../inc/boinc_db.inc");
require_once("../inc/translation.inc");
require_once("../inc/profile.inc");
require_once("../inc/bootstrap.inc");
// parse config.xml (do it here for efficiency)
//
$config = get_config();
global $master_url;
$master_url = parse_config($config , "<master_url>");
$recaptcha_public_key = parse_config($config, "<recaptcha_public_key>");
$recaptcha_private_key = parse_config($config, "<recaptcha_private_key>");
// Set parameters to defaults if not defined in config.xml
$x = parse_config($config, "<user_country>");
define('USER_COUNTRY', ($x===null)?1:(int)$x);
$x = parse_config($config, "<user_url>");
define('USER_URL', ($x===null)?1:(int)$x);
// Set parameters to defaults if not defined in project.inc
if (defined('TIMEZONE')) {
date_default_timezone_set(TIMEZONE);
} else {
date_default_timezone_set('UTC');
}
if (!defined('DISABLE_PROFILES')) {
define('DISABLE_PROFILES', false);
}
if (!defined('DISABLE_FORUMS')) {
define('DISABLE_FORUMS', false);
}
if (!defined('DISABLE_TEAMS')) {
define('DISABLE_TEAMS', false);
}
if (!defined('DISABLE_BADGES')) {
define('DISABLE_BADGES', false);
}
if (!defined('BADGE_HEIGHT_SMALL')) {
define('BADGE_HEIGHT_SMALL', 20);
}
if (!defined('BADGE_HEIGHT_MEDIUM')) {
define('BADGE_HEIGHT_MEDIUM', 24);
}
if (!defined('BADGE_HEIGHT_LARGE')) {
define('BADGE_HEIGHT_LARGE', 56);
}
if (!defined('LDAP_HOST')) {
define('LDAP_HOST', null);
}
if (!defined('POSTAL_CODE')) {
define('POSTAL_CODE', false);
}
if (!defined('NO_COMPUTING')) {
define('NO_COMPUTING', false);
}
if (!defined('NO_HOSTS')) {
define('NO_HOSTS', false);
}
if (!defined('NO_STATS')) {
define('NO_STATS', false);
}
if (!defined('NO_GLOBAL_PREFS')) {
define('NO_GLOBAL_PREFS', false);
}
// the 'home page' of the logged-in user.
// go here after login, account creation, team operations, etc.
//
if (!defined('HOME_PAGE')) {
define('HOME_PAGE', 'home.php');
}
// the page showing another user.
// Link to here wherever we show a user name.
//
if (!defined('SHOW_USER_PAGE')) {
define('SHOW_USER_PAGE', 'show_user.php');
}
if (!defined('POST_MAX_LINKS')) {
define('POST_MAX_LINKS', 0);
}
if (!defined('DARK_MODE')) {
define('DARK_MODE', false);
}
if (!defined('VALIDATE_EMAIL_TO_POST')) {
define('VALIDATE_EMAIL_TO_POST', false);
}
if (!defined('UNIQUE_USER_NAME')) {
define('UNIQUE_USER_NAME', false);
}
// don't allow anything between .php and ? in URL
//
if (array_key_exists("PATH_INFO", $_SERVER)) {
die("bad URL");
}
// sleep this long on any login failure
// (slow the rate of hacker attacks)
//
define('LOGIN_FAIL_SLEEP_SEC', 5);
$caching = false;
// if set, we're writing to a file rather than to client
$did_page_head = false;
define('KILO', 1024);
define('MEGA', 1024*KILO);
define('GIGA', 1024*MEGA);
define('TERA', 1024*GIGA);
// return true if this page is HTTPS
//
function is_https() {
return isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'];
}
function secure_url_base() {
if (defined('SECURE_URL_BASE')) return SECURE_URL_BASE;
return URL_BASE;
}
function url_base() {
return is_https()?secure_url_base():URL_BASE;
}
function send_cookie($name, $value, $permanent, $ops=false) {
global $master_url;
// the following allows independent login for projects on the same server
//
$url = parse_url($master_url);
$path = $url['path'];
if ($ops) {
$path = substr($path, 0, -1);
$path .= "_ops/";
}
$expire = $permanent?time()+3600*24*365:0;
setcookie($name, $value, $expire, $path);
}
function clear_cookie($name, $ops=false) {
global $master_url;
$url = parse_url($master_url);
$path = $url['path'];
if ($ops) {
$path = substr($path, 0, -1);
$path .= "_ops/";
}
setcookie($name, '', time()-3600, $path);
}
$g_logged_in_user = null;
$got_logged_in_user = false;
function get_logged_in_user($must_be_logged_in=true) {
global $g_logged_in_user, $got_logged_in_user;
if ($got_logged_in_user) {
// this could have been called earlier with $must_be_logged_in false
if ($must_be_logged_in) {
if ($g_logged_in_user) return $g_logged_in_user;
} else {
return $g_logged_in_user;
}
}
if (web_stopped()) return null;
$authenticator = null;
if (isset($_COOKIE['auth'])) $authenticator = $_COOKIE['auth'];
if ($authenticator) {
$authenticator = BoincDb::escape_string($authenticator);
$g_logged_in_user = BoincUser::lookup("authenticator='$authenticator'");
}
if ($must_be_logged_in && !$g_logged_in_user) {
$next_url = '';
if (array_key_exists('REQUEST_URI', $_SERVER)) {
$next_url = $_SERVER['REQUEST_URI'];
$n = strrpos($next_url, "/");
if ($n) {
$next_url = substr($next_url, $n+1);
}
}
$next_url = urlencode($next_url);
Header("Location: ".url_base()."login_form.php?next_url=$next_url");
exit;
}
$got_logged_in_user = true;
return $g_logged_in_user;
}
function show_login_info($prefix="") {
$user = get_logged_in_user(false);
if ($user) {
$url_tokens = url_tokens($user->authenticator);
echo "<nobr>$user->name · <a href=".$prefix."logout.php?$url_tokens>".tra("log out")."</a></nobr>";
} else {
echo "<a href=".$prefix."login_form.php>".tra("log in")."</a>";
}
}
$cache_control_extra="";
$is_login_page = false;
// Call this to start pages.
// Outputs some HTML boilerplate,
// then calls project_banner() (in html/project/project.inc)
// to output whatever you want at the top of your web pages.
//
// Page_head() is overridable so that projects that want to integrate BOINC
// with an existing web framework can more easily do so.
// To do so, define page_head() in the project include file.
//
if (!function_exists("page_head")){
function page_head(
$title,
// page title. Put in <title>, used as title for browser tab.
$body_attrs=null,
// <body XXXX>
// e.g. Javascript to put focus in an input field
// (onload="document.form.foo.focus()")
// or to jump to a particular post (onload="jumpToUnread();")
$is_main = false,
// if set, include schedulers.txt.
// also pass to project_banner() in case you want a different
// header for your main page.
$url_prefix="",
// prepend this to links.
// Use for web pages not in the top directory
$head_extra=null
// extra stuff to put in <head>. E.g.:
// reCAPTCHA code (create_profile.php)
// bbcode javascript (forums)
) {
global $caching, $cache_control_extra, $did_page_head;
global $is_login_page, $fixed_navbar;
if ($did_page_head) {
return;
}
$did_page_head = true;
$url_base = url_base();
$rssname = "RSS 2.0";
$rsslink = $url_base."rss_main.php";
if (!$caching) {
header("Content-type: text/html; charset=utf-8");
header("Expires: Mon, 26 Jul 1997 05:00:00 UTC");
// Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " UTC");
// always modified
header("Cache-Control: $cache_control_extra no-cache, must-revalidate, post-check=0, pre-check=0");
// for HTTP/1.1
header("Pragma: no-cache");
// for HTTP/1.0
}
echo '<!DOCTYPE html>
<html lang="en">
<head>
';
if (defined('GLOBAL_HEAD_EXTRA')) {
echo GLOBAL_HEAD_EXTRA;
}
echo '
<meta name="viewport" content="width=device-width, initial-scale=1">
';
if ($head_extra) {
echo "\n$head_extra\n";
}
if ($is_main && (!defined('NO_COMPUTING')||!NO_COMPUTING)) {
readfile("schedulers.txt");
}
$t = $title?$title:PROJECT;
echo "<title>$t</title>\n";
echo '
<meta charset="utf-8">
';
if (DARK_MODE) {
echo <<<EOT
<script>
if (window.matchMedia('(prefers-color-scheme: dark)').media === 'not all') {
console.log("foobar");
document.documentElement.style.display = 'none';
document.head.insertAdjacentHTML(
'beforeend',
'<link rel="stylesheet" href="sample_bootstrap.min.css" onload="document.documentElement.style.display = \'\'"><link rel="stylesheet" href="custom.css">'
);
}
</script>
<link rel="stylesheet" href="bootstrap_darkly.min.css" media="(prefers-color-scheme: dark)">
<link rel="stylesheet" href="custom_dark.css" media="(prefers-color-scheme: dark)">
<link rel="stylesheet" href="sample_bootstrap.min.css" media="(prefers-color-scheme: no-preference), (prefers-color-scheme: light)">
<link rel="stylesheet" href="custom.css" media="(prefers-color-scheme: no-preference), (prefers-color-scheme: light)">
EOT;
} else {
echo '
<link type="text/css" rel="stylesheet" href="'.secure_url_base().'/bootstrap.min.css" media="all">
';
}
if (defined('STYLESHEET')) {
$stylesheet = $url_base.STYLESHEET;
echo "
<link rel=stylesheet type=\"text/css\" href=\"$stylesheet\">
";
}
if (defined('STYLESHEET2')) {
$stylesheet2 = $url_base.STYLESHEET2;
echo "
<link rel=stylesheet type=\"text/css\" href=\"$stylesheet2\">
";
}
if (defined("SHORTCUT_ICON")) {
echo '<link rel="icon" type="image/x-icon" href="'.SHORTCUT_ICON.'"/>
';
}
echo "
<link rel=alternate type=\"application/rss+xml\" title=\"$rssname\" href=\"$rsslink\">
</head>
";
if ($fixed_navbar) {
$body_attrs .= ' style="padding-top:70px"';
}
echo "<body $body_attrs>";
display_cvs_versions();
echo '<div class="container-fluid">
';
switch($title) { //kludge
case tra("Log in"):
case tra("Create an account"):
case tra("Server status page"):
$is_login_page = true;
break;
default:
break;
}
project_banner($title, $url_prefix, $is_main);
}
}
// See the comments for page_head()
//
if (!function_exists("page_tail")){
function page_tail(
$show_date=false,
// true for pages that are generated periodically rather than on the fly
$url_prefix="",
// use for pages not at top level
$is_main=false
// passed to project_footer;
) {
echo "<br>\n";
project_footer($is_main, $show_date, $url_prefix);
echo '
<script src="'.secure_url_base().'/jquery.min.js"></script>
<script src="'.secure_url_base().'/bootstrap.min.js"></script>
</div>
</body>
</html>
';
}
}
function display_cvs_versions(){
global $cvs_version_tracker;
echo "\n<!-- SVN VERSIONS -->\n";
for ($i=0;$i<sizeof($cvs_version_tracker);$i++) {
echo "<!-- ".$cvs_version_tracker[$i]." -->\n";
}
}
function db_error_page() {
page_head("Database error");
echo tra("A database error occurred while handling your request; please try again later.");
page_tail();
}
function error_page($msg) {
global $generating_xml;
if ($generating_xml) {
xml_error(-1, $msg);
}
page_head(tra("Unable to handle request"));
echo $msg;
page_tail();
exit();
}
// convert time interval in seconds to a string of the form
// 'D days h hours m min s sec'.
function time_diff($x, $res=3) {
$x = (int)$x;
$days = (int)($x/86400);
$hours = (int)(($x-$days*86400)/3600);
$minutes = (int)(($x-$days*86400-$hours*3600)/60);
$seconds = $x % 60;
$s = "";
if ($days) {
$s .= "$days ".tra("days")." ";
}
if ($res>0 && ($hours || strlen($s))) {
$s .= "$hours ".tra("hours")." ";
}
if ($res>1 && ($minutes || strlen($s))) {
$s .= "$minutes ".tra("min")." ";
}
if ($res>2) {
$s .= "$seconds ".tra("sec")." ";
}
return $s;
}
function date_str($x) {
if ($x == 0) return "---";
return gmdate('j M Y', (int)$x);
}
function time_str($x) {
if ($x == 0) return "---";
return gmdate('j M Y, G:i:s', (int)$x) . " UTC";
}
function local_time_str($x) {
if ($x == 0) return "---";
return date('j M Y, H:i T', (int)$x);
}
function pretty_time_str($x) {
return time_str($x);
}
function start_table_str($class="", $style="") {
$s = $style?'style="'.$style.'"':'';
return '<div class="table">
<table '.$s.' width="100%" class="table table-condensed '.$class.'" >
';
}
function start_table($class="", $style="") {
echo start_table_str($class, $style);
}
function end_table_str() {
return '</table>
</div>
';
}
function end_table() {
echo end_table_str();
}
// Table header row with unlimited number of columns
function table_header() {
echo "<tr>\n";
$c = 'class="bg-primary"';
for ($i = 0; $i < func_num_args(); $i++) {
if (is_array(func_get_arg($i))) {
$col = func_get_arg($i);
echo "<th $c ".$col[1].">".$col[0]."</th>\n";
} else {
echo "<th $c>".func_get_arg($i)."</th>\n";
}
}
echo "</tr>\n";
}
// Table row with unlimited number of columns
function table_row() {
echo "<tr>\n";
for ($i = 0; $i < func_num_args(); $i++) {
if (is_array(func_get_arg($i))) {
$col = func_get_arg($i);
echo "<td ".$col[1].">".$col[0]."</td>\n";
} else {
echo "<td>".func_get_arg($i)."</td>\n";
}
}
echo "</tr>\n";
}
function row1($x, $ncols=2, $class="heading") {
if ($class == "heading") {
echo "<tr><th class=\"bg-primary\" colspan=\"$ncols\">$x</th></tr>\n";
} else {
echo "<tr><td class=\"$class\" colspan=\"$ncols\">$x</td></tr>\n";
}
}
define('NAME_ATTRS', 'class="text-right " style="padding-right:12px"');
define('VALUE_ATTRS', 'style="padding-left:12px"');
define('VALUE_ATTRS_ERR', 'class="danger" style="padding-left:12px"');
// a table row with 2 columns, with the left on right-aligned
function row2($x, $y, $show_error=false, $lwidth='40%') {
if ($x==="") $x="<br>";
if ($y==="") $y="<br>";
$attrs = $show_error?VALUE_ATTRS_ERR:VALUE_ATTRS;
echo "<tr>
<td width=\"$lwidth\" ".NAME_ATTRS.">$x</td>
<td $attrs >$y</td>
</tr>
";
}
// output the first part of row2();
// then write the content, followed by </td></tr>
function row2_init($x, $lwidth='40%') {
echo sprintf('<tr>
<td width="%s" %s>%s</td>
<td %s>',
$lwidth, NAME_ATTRS, $x, VALUE_ATTRS
);
}
function row2_plain($x, $y) {
echo "<tr><td>$x</td><td>$y</td></tr>\n";
}
function rowify($string) {
echo "<tr><td>$string</td></tr>";
}
function row_array($x, $attrs=null) {
echo "<tr>\n";
$i = 0;
foreach ($x as $h) {
$a = $attrs?$attrs[$i]:"";
echo "<td $a>$h</td>\n";
$i++;
}
echo "</tr>\n";
}
define ('ALIGN_RIGHT', 'style="text-align:right;"');
function row_heading_array($x, $attrs=null, $class='bg-primary') {
echo "<tr>";
$i = 0;
foreach ($x as $h) {
$a = $attrs?$attrs[$i]:"";
echo "<th $a class=\"$class\">$h</th>";
$i++;
}
echo "</tr>\n";
}
function row_heading($x, $class='bg-primary') {
echo sprintf('<tr><th class="%s" colspan=99>%s</th></tr>
', $class, $x
);
}
function url_tokens($auth) {
$now = time();
$ttok = md5((string)$now.$auth);
return "&tnow=$now&ttok=$ttok";
}
function form_tokens($auth) {
$now = time();
$ttok = md5((string)$now.$auth);
return "<input type=\"hidden\" name=\"tnow\" value=\"$now\">
<input type=\"hidden\" name=\"ttok\" value=\"$ttok\">
";
}
function valid_tokens($auth) {
$tnow = get_str('tnow', true);
$ttok = get_str('ttok', true);
if (!$tnow) {
if (isset($_POST['tnow'])) {
$tnow = $_POST['tnow'];
}
}
if (!$ttok) {
if (isset($_POST['ttok'])) {
$ttok = $_POST['ttok'];
}
}
if (!$tnow) return false;
if (!$ttok) return false;
$t = md5((string)$tnow.$auth);
if ($t != $ttok) return false;
if (time() > $tnow + 86400) return false;
return true;
}
function check_tokens($auth) {
if (valid_tokens($auth)) return;
error_page(
tra("Link has timed out. Please click Back, refresh the page, and try again.")
);
}
// Generates a legal filename from a parameter string.
function get_legal_filename($name) {
return strtr($name, array(','=>'', ' '=>'_'));
}
// Returns a string containing as many words
// (being collections of characters separated by the character $delimiter)
// as possible such that the total string length is <= $chars characters long.
// If $ellipsis is true, then an ellipsis is added to any sentence which
// is cut short.
function sub_sentence($sentence, $delimiter, $max_chars, $ellipsis=false) {
$words = explode($delimiter, $sentence);
$total_chars = 0;
$trunc = false;
$result = "";
foreach ($words as $word) {
if (strlen($result) + strlen($word) > $max_chars) {
$trunc = true;
break;
}
if ($result) {
$result .= " $word";
} else {
$result = $word;
}
}
if ($ellipsis && $trunc) {
$result .= "...";
}
return $result;
}
// use this for user RAC and result credit
//
function format_credit($x) {
return number_format($x, 2);
}
// use this when credit is likely to be large, e.g. team RAC
//
function format_credit_large($x) {
return number_format($x, 0);
}
function host_link($hostid) {
if ($hostid) {
return "<a href=\"show_host_detail.php?hostid=$hostid\">$hostid</a>";
} else {
return "---";
}
}
function open_output_buffer() {
ob_start();
ob_implicit_flush(0);
}
function close_output_buffer($filename) {
$fh = fopen($filename, "w");
$page = ob_get_contents();
ob_end_clean();
fwrite($fh, $page);
fclose($fh);
}
function bbcode_info() {
return "<br><a href=bbcode.php target=new><small>".tra("Use BBCode tags to format your text")."</small></a>\n";
}
// strip slashes if magic quotes in effect
function undo_magic_quotes($x) {
if (version_compare(PHP_VERSION, '5.4.0') < 0 && get_magic_quotes_gpc()) {
return stripslashes($x);
}
return $x;
}
// check for bogus GET args
//
function check_get_args($args) {
foreach ($_GET as $key => $val) {
if (!in_array($key, $args)) {
Header("Location: extra_arg_$key.html");
die;
}
}
}
// returns null if the arg is optional and missing
//
function get_int($name, $optional=false) {
$x=null;
if (isset($_GET[$name])) $x = $_GET[$name];
if (!is_numeric($x)) {
if ($optional) {
if ($x) {
Header("Location: non_num_arg.html");
die;
}
return null;
} else {
Header("Location: missing_arg_$name.html");
die;
}
}
return (int)$x;
}
// returns null if the arg is optional and missing
//
function post_num($name, $optional=false) {
$x = null;
if (isset($_POST[$name])) $x = $_POST[$name];
if (!is_numeric($x)) {
if ($optional) {
return null;
} else {
error_page("missing or bad parameter: $name; supplied: ".htmlspecialchars($x));
}
}
return (double)$x;
}
// returns null if the arg is optional and missing
//
function post_int($name, $optional=false) {
$x = post_num($name, $optional);
if (is_null($x)) return null;
$y = (int)$x;
if ($x != $y) {
error_page("param $name must be an integer");
}
return $y;
}
function get_array($name) {
if (isset($_GET[$name])) {
return $_GET[$name];
} else {
return array();
}
}
function get_str($name, $optional=false) {
if (isset($_GET[$name])) {
$x = $_GET[$name];
} else {
if (!$optional) {
error_page("missing or bad parameter: $name");
}
$x = null;
}
return undo_magic_quotes($x);
}
function post_str($name, $optional=false) {
if (isset($_POST[$name])) {
$x = $_POST[$name];
} else {
if (!$optional) {
error_page("missing or bad parameter: $name");
}
$x = null;
}
return undo_magic_quotes($x);
}
function post_arr($name, $optional=false) {
if (isset($_POST[$name]) && is_array($_POST[$name])) {
$x = $_POST[$name];
} else {
if (!$optional) {
error_page("missing or bad parameter: $name");
}
$x = null;
}
return $x;
}
function is_ascii($str) {
// the mb_* functions are not included by default
// return (mb_detect_encoding($passwd) -= 'ASCII');
for ($i=0; $i<strlen($str); $i++) {
$c = ord(substr($str, $i));
if ($c < 32 || $c > 127) return false;
}
return true;
}
// This function replaces some often made mistakes while entering numbers
// and gives back an error if there are false characters
// It will also be checked if the value is within certain borders
// @param string &$value reference to the value that should be verified
// @param double $low the lowest number of value if verified
// @param double $high the highest number of value if verified
// @return bool true if $value is numeric and within the defined borders,
// false if $value is not numeric, no changes were made in this case
//
function verify_numeric(&$value, $low, $high = false) {
$number = trim($value);
$number = str_replace('o', '0', $number);
$number = str_replace('O', '0', $number);
$number = str_replace('x', '', $number); //if someone enters '0x100'
$number = str_replace(',', '.', $number); // replace the german decimal separator
// if no value was entered and this is ok
//
if ($number=='' && !$low) return true;
// the supplied value contains alphabetic characters
//
if (!is_numeric($number)) return false;
if ($number < $low) return false;
if ($high) {
if ($number > $high) return false;
}
$value = (double)$number;
return true;
}
// Generate a "select" element from an array of values
//
function select_from_array($name, $array, $selection=null, $width=240) {
$out = '<select style="color:#000;"class="form-control input-sm" style="width:'.$width.'px" name="'.$name.'">"';
foreach ($array as $key => $value) {
if ($value) {
$out .= "<option ";
if ($key == $selection) {
$out .= "selected ";
}
$out .= "value=\"".$key."\">".$value."</option>\n";
}
}
$out .= "</select>\n";
return $out;
}
// Convert to entities, while preserving already-encoded entities.
// Do NOT use if $str contains valid HTML tags.
//
function boinc_htmlentities($str) {
$str = html_entity_decode($str, ENT_COMPAT, "UTF-8");
$str = htmlentities($str, ENT_COMPAT, "UTF-8");
return $str;
}
function strip_bbcode($string){
return preg_replace("/((\[.+\])+?)(.+?)((\[\/.+\])+?)/","",$string);
}
function current_url() {
$url = "http";
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") {
$url .= "s";
}
$url .= "://";
$url .= $_SERVER['SERVER_NAME'];
$url .= ":".$_SERVER['SERVER_PORT'];
if (isset($_SERVER['REQUEST_URI'])) {
$url .= $_SERVER['REQUEST_URI'];
} else {
if ($_SERVER['QUERY_STRING']) {
$url .= "?".$_SERVER['QUERY_STRING'];
}
}
return $url;
}
// Show a single link formatted to look like a button.
// @param url The destination URL of the button
// @param text The text to display on the button
// @param desc The title of the destination - typically used as a popup
// @param class The optional CSS class of the button. Defaults to a standard button
// @params extra Additional text in href tag
//
function button_text($url, $text, $desc=null, $class=null, $extra='') {
if (!$desc) {
$desc = $text;
}
if (!$class) {
$class = "btn-success btn-sm";
}
return sprintf(' <a href="%s" title="%s" class="btn %s" %s>%s</a>',
$url, $desc, $class, $extra, $text
);
}
function show_button($url, $text, $desc=null, $class=null, $extra=null) {
echo button_text($url, $text, $desc, $class, $extra);
}
// for places with a bunch of buttons, like forum posts
//
function show_button_small($url, $text, $desc=null) {
echo button_text($url, $text, $desc, "btn-primary btn-xs");
}
// used for showing icons
//
function show_image($src, $title, $alt, $height=null) {
$h = "";
if ($height) {
$h = "height=\"$height\"";
}
echo "<img class=\"icon\" border=\"0\" title=\"$title\" alt=\"$alt\" src=\"$src\" $h>";
}
function show_project_down() {
global $did_page_head;
if (!$did_page_head) {
page_head(tra("Project down for maintenance"));
}
echo tra(
"%1 is temporarily shut down for maintenance. Please try again later.",
PROJECT
);
page_tail();
exit();
}
function check_web_stopped() {
global $generating_xml;
if (web_stopped()) {
if ($generating_xml) {
xml_error(-183);
} else {
show_project_down();
}
}
}
// Connects to database server and selects database as noted in config.xml
// If only read-only access is necessary,
// tries instead to connect to <replica_db_host> if tag exists.
// DEPRECATED - use boinc_db.inc
//
function db_init($try_replica=false) {
check_web_stopped();
$retval = db_init_aux($try_replica);
if ($retval == 1) {
echo tra("Unable to connect to database - please try again later");
exit();
}
if ($retval == 2) {
echo tra("Unable to select database - please try again later");
exit();
}
return 0;
}
// return a structure indicating whether project has non-deprecated
// apps versions for various resource types,