-
Notifications
You must be signed in to change notification settings - Fork 2
/
inc_common.php
2556 lines (2192 loc) · 91.2 KB
/
inc_common.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
if (count(get_included_files()) == 1) {header('location: http://' . $_SERVER['HTTP_HOST']); exit();}
header('X-Frame-Options: SAMEORIGIN');
if (!isset($_COOKIE['submgr_cookie_test'])) {setcookie('submgr_cookie_test', 1);}
$session_name = 'submgr';
$getcwd = getcwd();
if ($getcwd) {$session_name .= '_' . sha1($getcwd);}
session_name($session_name);
$session_start = session_start();
$nonce = get_token();
form_hash('session'); // needed here so csrf_token exists in javascript
$_SERVER['PHP_SELF'] = htmlentities($_SERVER['PHP_SELF']);
$pages = array('home', 'login', 'install', 'help', 'error');
if (isset($_GET['page'])) {$page = htmlentities($_GET['page']);} else {$page = 'home';}
if (!in_array($page, $pages)) {$page = 'error';}
if ($page != 'home') {$page_title = $page;}
if (isset($_GET['kill_session'])) {kill_session('regenerate');} // session needed for form_hash()
if (isset($_REQUEST['module'])) {$module = htmlentities($_REQUEST['module']);} else {$module = '';}
if (isset($_REQUEST['submodule'])) {$submodule = htmlentities($_REQUEST['submodule']);} else {$submodule = '';}
if (isset($_POST['submit'])) {$submit = htmlentities($_POST['submit']);} else {$submit = '';}
if (isset($_POST['submit_hidden'])) {$submit = htmlentities($_POST['submit_hidden']);}
$submit_js = $submit; // needed for javascript because this changes downstream
$gm_timestamp = time();
$gm_date_time = gmdate('Y-m-d H:i:s', $gm_timestamp);
$gm_date = gmdate('Y-m-d', $gm_timestamp);
$gm_year = gmdate('Y', $gm_timestamp);
$output = '';
$notice = '';
$db_connect = false;
$configuration_status = true;
$config = array();
$display_login = true;
$continue = true;
$form_check = true;
$errors = array();
$password_length_min = 8; $password_length_max = 72; // needed here globally for install, will be overwritten by $fields
$no_submissions_text = 'Submission Manager is currently in <b>“no submissions”</b> mode.<br>Submitters and staff may log into their accounts however no new submissions are being accepted at this time.';
$admin_only_text = 'Submission Manager is currently in <b>admin only</b> mode.<br>Only the system administrators have access at this time.';
$no_cookies_text = '<b>Submission Manager</b> requires that cookies be enabled in your web browser. Please enable cookies and try again. You may also need to empty your browser cache and restart your browser.';
if (!$session_start) {$display_login = false; exit_error('session_start failed');}
if (file_exists('config_defaults.php')) {include('config_defaults.php'); $config = $config_defaults;} else {$display_login = false; exit_error('missing config_defaults.php');}
if (file_exists('config_db.php')) {include('config_db.php');} elseif (file_exists('config_db_default.php')) {include('config_db_default.php');} else {$display_login = false; exit_error('missing config_db.php');}
if (file_exists('db_schema.php')) {include('db_schema.php');} else {$display_login = false; exit_error('missing db_schema.php');}
$modules = array(
'account' => 'account summary',
'update' => 'update your account',
'submit' => 'submit your work',
'pay_submission' => 'pay for submission',
'logout' => 'logout'
);
$modules_admin = array(
'submissions',
'contacts',
'reports',
'configuration',
'maintenance'
);
$login_required_fields = array(
'submissions' => array('submitter_id', 'title'),
'actions' => array('reader_id', 'action_type_id')
);
$local_variables = array(
'contacts' => array(
'contact_id',
'first_name',
'last_name',
'name',
'email',
'company',
'address1',
'address2',
'city',
'state',
'zip',
'country',
'phone'
),
'submissions' => array(
'submission_id',
'genre_id'
),
'payment' => array(
'price',
'cc_number',
'cc_exp_month',
'cc_exp_year',
'cc_exp_date',
'cc_csc',
'hash',
'timestamp',
'result_code',
'error'
)
);
function check_version($software, $get_remote = false)
{
if ($software == 'PHP')
{
$version = PHP_VERSION;
$min_version = '5.5.0';
}
if ($software == 'mySQL')
{
$result = @mysqli_query($GLOBALS['db_connect'], 'SELECT VERSION() AS version') or exit_error('query failure: SELECT VERSION');
$row = mysqli_fetch_assoc($result);
$version = $row['version'];
$GLOBALS['version_mysql'] = $version;
$min_version = '5.0.0';
}
if ($software == 'SubMgr')
{
$version = @file_get_contents('version.txt');
if ($version) {$version_local = trim($version);} else {$version_local = '???';}
$GLOBALS['version_local'] = $version_local;
if ($get_remote)
{
$options = array('http' => array('user_agent' => $_SERVER['HTTP_USER_AGENT'], 'timeout' => 10.0, 'ignore_errors' => true));
$context = stream_context_create($options);
$version = @file_get_contents('https://www.submissionmanager.net/version.txt', false, $context);
if ($version) {$version_remote = trim($version);} else {$version_remote = '???';}
$GLOBALS['version_remote'] = $version_remote;
}
}
if ($software == 'structure')
{
$result = @mysqli_query($GLOBALS['db_connect'], "SHOW TABLE STATUS LIKE 'config'") or exit_error('query failure: SHOW TABLE STATUS<br><br>' . mysqli_error($GLOBALS['db_connect']));
$row = mysqli_fetch_assoc($result);
$GLOBALS['version_structure'] = $row['Comment'];
}
if (isset($min_version) && version_compare($version, $min_version) < 0)
{
$GLOBALS['error_output'] = '<b>Submission Manager</b> requires ' . $software . ' version ' . $min_version . ' or higher. Please notify your system administrator.';
$GLOBALS['display_login'] = false;
exit_error();
}
}
check_version('PHP');
if (!extension_loaded('mysqli'))
{
$error_output = '<b>Submission Manager</b> requires that the mySQLi extension be enabled in your PHP configuration. Please notify your system administrator.';
$display_login = false;
exit_error();
}
function db_connect($db_host, $db_username, $db_password, $db_name = '', $db_port = '')
{
$GLOBALS['mysqli_sql_exception'] = '';
if (!$db_port) {$db_port = ini_get('mysqli.default_port');}
try {$db_connect = @mysqli_connect($db_host, $db_username, $db_password, '', $db_port);}
catch (mysqli_sql_exception $exception) {$GLOBALS['mysqli_sql_exception'] = $exception->getMessage();}
try {if ($db_connect && $db_name) {$db_select = @mysqli_select_db($db_connect, $db_name);} else {$db_select = true;}}
catch (mysqli_sql_exception $exception) {$GLOBALS['mysqli_sql_exception'] = $exception->getMessage();}
if ($db_connect && $db_select) {$GLOBALS['db_connect'] = $db_connect;} else {$GLOBALS['db_connect'] = false;}
if ($GLOBALS['db_connect'])
{
@mysqli_query($GLOBALS['db_connect'], 'SET NAMES "utf8"') or exit_error('query failure: SET NAMES utf8');
@mysqli_set_charset($GLOBALS['db_connect'], 'utf8');
}
return $GLOBALS['db_connect'];
}
function insert_from_array($table, $array)
{
foreach ($array as $value)
{
$sql_array = array();
foreach ($value as $field_name => $field_value)
{
if ($field_value != '') {$field_value = "'" . mysqli_real_escape_string($GLOBALS['db_connect'], $field_value) . "'";} else {$field_value = 'NULL';}
$sql_array[] = $field_name . ' = ' . $field_value;
}
$sql = "INSERT INTO `$table` SET " . implode(', ', $sql_array);
@mysqli_query($GLOBALS['db_connect'], $sql) or exit_error('query failure: INSERT INTO ' . $table);
}
}
function reset_defaults($table, $name, $array = '')
{
@mysqli_query($GLOBALS['db_connect'], "TRUNCATE `$table`") or exit_error('query failure: TRUNCATE ' . $table);
if ($array) {insert_from_array($table, $array);}
$GLOBALS['notice'] = $name . ' settings have been reset to the default values';
}
if (INSTALLED)
{
foreach ($config_db as $key => $value)
{
if ($key != 'password' && $key != 'port' && $value == '')
{
$display_login = false;
exit_error('missing database info from configuration file');
break;
}
}
if (!isset($config_db['port'])) {$config_db['port'] = '';}
@db_connect($config_db['host'], $config_db['username'], $config_db['password'], $config_db['name'], $config_db['port']);
if (!$GLOBALS['db_connect'])
{
$display_login = false;
exit_error('Database Connection: ' . $GLOBALS['mysqli_sql_exception']);
}
}
else
{
$page = 'install';
$display_login = false;
}
if ($GLOBALS['db_connect'])
{
check_version('mySQL');
function get_tables()
{
$GLOBALS['show_tables'] = array();
$result = @mysqli_query($GLOBALS['db_connect'], 'SHOW TABLES') or exit_error('query failure: SHOW TABLES');
while ($row = mysqli_fetch_row($result))
{
if (isset($GLOBALS['schema'][$row[0]])) {$GLOBALS['show_tables'][] = $row[0];}
}
}
get_tables();
// check for required tables
$required_tables = array('config','contacts','submissions');
foreach ($required_tables as $value)
{
if (!in_array($value, $show_tables))
{
$display_login = false;
exit_error('required tables unavailable');
break;
}
}
function check_config($array)
{
extract($GLOBALS);
$config_invalid = array();
foreach ($array as $key => $value)
{
if ($value == '' && $defaults['config'][$key]['required'])
{
$missing[] = $key;
$config_invalid[] = $key;
$form_check = false;
}
if ($defaults['config'][$key]['allowed'] == 'zero' && !is_numeric($value))
{
$errors[] = $key . ' must be a numeric value';
$config_invalid[] = $key;
$form_check = false;
}
}
if ($array['upload_path'])
{
$array['upload_path'] = str_replace('\\', '/', $array['upload_path']);
if (substr($array['upload_path'], -1) != '/') {$array['upload_path'] .= '/';}
if (!file_exists($array['upload_path']))
{
$errors[] = 'upload_path is an invalid path';
$config_invalid[] = 'upload_path';
$form_check = false;
}
}
$emails = array();
if ($array['general_dnr_email']) {$emails['general_dnr_email'] = $array['general_dnr_email'];}
if ($array['admin_email']) {$emails['admin_email'] = $array['admin_email'];}
if ($emails)
{
foreach ($emails as $key => $value)
{
if (!email_check($value))
{
if (is_numeric($key)) {$key = 'mail_admin_list';}
$bad_mails[] = $key;
$config_invalid[] = $key;
$form_check = false;
}
}
}
if (isset($array['payment_redirect_method']) && $array['payment_redirect_method'] == 'cURL' && !extension_loaded('curl'))
{
$errors[] = 'cURL extension is not loaded';
$form_check = false;
}
if (isset($array['captcha_site_key']) && isset($array['captcha_secret_key']))
{
if (($array['captcha_site_key'] && !$array['captcha_secret_key']) || (!$array['captcha_site_key'] && $array['captcha_secret_key']))
{
$errors[] = 'both captcha_site_key and captcha_secret_key must be set to use CAPTCHA';
$form_check = false;
}
if ($array['captcha_site_key'] && $array['captcha_secret_key'] && !extension_loaded('curl'))
{
$errors[] = 'cURL extension must be loaded to use CAPTCHA';
$form_check = false;
}
}
if (!$form_check)
{
if (isset($bad_mails) && $bad_mails) {$errors[] = 'invalid email(s): ' . implode(', ', $bad_mails);}
if (isset($missing) && $missing) {$errors[] = 'required field(s) missing: ' . implode(', ', $missing);}
$notice = 'ERROR! The following errors were detected:<ul>';
foreach ($errors as $value) {$notice .= '<li>' . $value . '</li>';}
$notice .= '</ul>';
if ($page == 'login' && $module == 'configuration' && $submodule == 'general') {$GLOBALS['notice'] = $notice;}
$GLOBALS['configuration_status'] = false;
}
else
{
$GLOBALS['notice'] = '';
$GLOBALS['configuration_status'] = true;
}
$GLOBALS['config_invalid'] = $config_invalid;
return $array;
}
if ($page == 'login' && $module == 'configuration' && $submodule == 'general' && isset($_SESSION['contact']['access']) && $_SESSION['contact']['access'] == 'admin')
{
if ($submit) {form_hash('validate');}
if ($submit == 'update')
{
foreach ($_SESSION['config'] as $key => $value)
{
if (isset($_POST['config'][$key]))
{
$clean = trim($_POST['config'][$key]);
$clean = stripslashes($clean);
// allow HTML
if ($defaults['config'][$key]['allowed'] != 'html') {$clean = strip_tags($clean);}
// make "0" instead of NULL
if ($defaults['config'][$key]['allowed'] == 'zero')
{
if ($key == 'submission_price') {$clean = preg_replace('/[^0-9.]/i', '', $clean);} else {$clean = preg_replace('/[^0-9]/i', '', $clean);}
if ($clean == '') {$clean = 0;}
if ($key == 'submission_price' && is_numeric($clean)) {$clean = number_format($clean, 2, '.', '');}
}
$post_config[$key] = $clean;
}
else
{
$post_config[$key] = '';
}
}
// update CSP for captcha
if (isset($post_config['captcha_site_key']) && isset($post_config['captcha_secret_key']) && $post_config['captcha_site_key'] && $post_config['captcha_secret_key'] && isset($post_config['csp']))
{
if (strpos($post_config['csp'], 'https://www.google.com/recaptcha/') === false)
{
$post_config['csp'] = str_replace('script-src \'self\'', 'script-src \'self\' https://www.google.com/recaptcha/ https://www.gstatic.com/recaptcha/', $post_config['csp']);
$post_config['csp'] .= ' frame-src \'self\' https://www.google.com/recaptcha/ https://recaptcha.google.com/recaptcha/;';
}
}
$post_config = check_config($post_config);
// update db unconditionally so valid fields get written (even if form check fails)
foreach ($post_config as $key => $value)
{
if (!in_array($key, $config_invalid))
{
if ($value === '') {$value = 'NULL';} else {$value = "'" . mysqli_real_escape_string($GLOBALS['db_connect'], $value) . "'";}
$sql = 'UPDATE config SET value = ' . $value . " WHERE name = '" . $key . "'";
@mysqli_query($GLOBALS['db_connect'], $sql) or exit_error('query failure: UPDATE config');
}
}
if ($configuration_status)
{
$notice = 'General configuration settings updated successfully';
}
else
{
$form_check = false;
}
}
if ($submit == 'reset defaults')
{
foreach ($config_defaults as $key => $value) {$config_defaults_reset[$key] = array('name' => $key, 'value' => $value);}
reset_defaults('config', 'General Configuration', $config_defaults_reset);
}
}
function get_config()
{
$GLOBALS['config'] = array(); // flush out config
$result = @mysqli_query($GLOBALS['db_connect'], 'SELECT * FROM config') or exit_error('query failure: SELECT config');
if (mysqli_num_rows($result))
{
while ($row = mysqli_fetch_assoc($result)) {$GLOBALS['config'][$row['name']] = $row['value'];}
}
else
{
exit_error('empty configuration table');
}
}
function compare_configs()
{
global $config_defaults, $config;
$keys_config_defaults = array_keys($config_defaults);
$keys_config = array_keys($config);
$missing_configs = array_diff($keys_config_defaults, $keys_config);
$extra_configs = array_diff($keys_config, $keys_config_defaults);
$GLOBALS['missing_configs'] = $missing_configs;
$GLOBALS['extra_configs'] = $extra_configs;
}
get_config();
// check for missing/extra config rows to suppress errors before data structure update
compare_configs();
if ($missing_configs)
{
foreach ($missing_configs as $value) {$config[$value] = $config_defaults[$value];}
}
if ($extra_configs)
{
foreach ($extra_configs as $value) {unset($config[$value]);}
}
// extract needed for app_url and company_name global vars
extract($config);
$app_url_slash = $app_url;
if (substr((string) $app_url_slash, -1) != '/') {$app_url_slash .= '/';}
$upload_path_year = $config['upload_path'] . $gm_year . '/';
// required config settings must be set
if (!isset($post_config)) {$config = check_config($config);}
if (isset($config['captcha_site_key']) && isset($config['captcha_secret_key']) && $config['captcha_site_key'] && $config['captcha_secret_key'] && extension_loaded('curl')) {$use_captcha = true;} else {$use_captcha = false;}
if ($use_captcha && isset($config['captcha_version'])) {$captcha_version = $config['captcha_version'];} else {$captcha_version = 2;}
if (isset($config['csp']) && $config['csp'])
{
$csp = str_replace('[nonce]', $GLOBALS['nonce'], $config['csp']);
header('Content-Security-Policy: ' . $csp);
}
}
if ($page == 'login' && isset($_SESSION['contact']['access']) && $_SESSION['contact']['access'] == 'admin')
{
if ($module == 'configuration' && $submodule == 'action_types')
{
if ($submit) {form_hash('validate');}
if ($submit == 'update')
{
foreach ($_POST['action_types'] as $key => $value)
{
$value = cleanup($value, 'strip_tags', 'stripslashes');
foreach ($value as $field_name => $field_value)
{
if ($field_name != 'description' && $field_name != 'status' && $field_value == '')
{
$form_check = false;
$errors[$key] = $field_name;
}
}
if (!isset($value['active'])) {$value['active'] = '';}
if (!isset($value['from_reader'])) {$value['from_reader'] = '';}
if (isset($value['access_groups'])) {$value['access_groups'] = implode(',', $value['access_groups']);} else {$value['access_groups'] = '';}
$post_action_types_insert[$key] = $value;
}
if (!$form_check)
{
foreach ($_SESSION['action_types'] as $key => $value)
{
if (isset($post_action_types_insert[$key])) {$post_action_types[$key] = $post_action_types_insert[$key];} else {$post_action_types[$key] = $value;}
}
$notice = 'ERROR! The following fields were empty:<ul>';
foreach ($errors as $key => $value) {$notice .= '<li>' . $_SESSION['action_types'][$key]['name'] . ' => ' . $value . '</li>';}
$notice .= '</ul>';
}
else
{
foreach ($post_action_types_insert as $key => $value)
{
$sql_array = array();
foreach ($value as $field_name => $field_value)
{
if ($field_value != '') {$field_value = "'" . mysqli_real_escape_string($GLOBALS['db_connect'], $field_value) . "'";} else {$field_value = 'NULL';}
$sql_array[] = $field_name . ' = ' . $field_value;
}
$sql = 'UPDATE action_types SET ' . implode(', ', $sql_array) . ' WHERE action_type_id = ' . $key;
@mysqli_query($GLOBALS['db_connect'], $sql) or exit_error('query failure: UPDATE action_types');
}
$notice = 'Action type settings updated successfully';
}
}
if ($submit == 'reset defaults')
{
reset_defaults('action_types', 'Action Types', $defaults['action_types']);
}
}
if ($module == 'configuration' && $submodule == 'file_types')
{
if ($submit) {form_hash('validate');}
if ($submit == 'update')
{
@mysqli_query($GLOBALS['db_connect'], 'TRUNCATE file_types') or exit_error('query failure: TRUNCATE file_types');
asort($_POST['file_types']);
$_POST['file_types'] = array_unique($_POST['file_types']);
$_POST['file_types'] = cleanup($_POST['file_types'], 'strip_tags', 'stripslashes');
foreach ($_POST['file_types'] as $value)
{
if ($value != '')
{
$value = str_replace(' ', '', $value);
$value = preg_replace('/[^A-Za-z0-9]/i', '', $value);
$value = substr($value, 0, 10);
$value = strtolower($value);
$sql = "INSERT INTO file_types SET ext = '" . mysqli_real_escape_string($GLOBALS['db_connect'], $value) . "'";
@mysqli_query($GLOBALS['db_connect'], $sql) or exit_error('query failure: INSERT file_types');
}
}
$notice = 'File Type settings updated successfully';
}
if ($submit == 'reset defaults')
{
reset_defaults('file_types', 'File Types', $defaults['file_types']);
}
if (isset($_GET['ext']) && $_GET['ext'] != '' && isset($_GET['delete']))
{
$ext = trim($_GET['ext']);
@mysqli_query($GLOBALS['db_connect'], "DELETE FROM file_types WHERE ext = '" . mysqli_real_escape_string($GLOBALS['db_connect'], $ext) . "'") or exit_error('query failure: DELETE file_types');
$notice = 'File Type ' . $ext . ' deleted';
}
}
if ($module == 'configuration' && $submodule == 'fields')
{
$fields_editable = array(
'name' => 'text',
'value' => 'text',
'maxlength' => 'text',
'enabled' => 'checkbox',
'required' => 'checkbox'
);
$fields_checkbox_disabled = array('email', 'password', 'password2', 'cc_number', 'cc_exp_month', 'cc_exp_year', 'cc_csc');
include_once('inc_lists.php');
if ($submit) {form_hash('validate');}
if ($submit == 'update')
{
foreach ($_POST['fields'] as $key => $value)
{
$value = cleanup($value, 'strip_tags', 'stripslashes');
if ($value['maxlength']) {$value['maxlength'] = preg_replace('/[^0-9]/i', '', $value['maxlength']);}
if (isset($value['enabled'])) {$value['enabled'] = 'Y';} else {$value['enabled'] = '';}
if (isset($value['required'])) {$value['required'] = 'Y';} else {$value['required'] = '';}
if (!$value['enabled']) {$value['required'] = '';}
if ($defaults['fields'][$key]['type'] == 'checkbox' && $value['value']) {$value['value'] = 'Y';}
if ($key == 'country') {$value['value'] = preg_replace('/[^A-Z]/', '', $value['value']);}
if (in_array($key, $fields_checkbox_disabled))
{
$value['enabled'] = $defaults['fields'][$key]['enabled'];
$value['required'] = $defaults['fields'][$key]['required'];
}
if (!$value['name'])
{
$form_check = false;
$errors[$key][] = 'name';
$notices[] = 'ERROR: Field Names cannot be blank';
}
if (!is_numeric($value['maxlength']) || $value['maxlength'] == 0)
{
$form_check = false;
$errors[$key][] = 'maxlength';
$notices[] = 'ERROR: Field Max Lengths must be numeric and greater than 0';
}
if ($key == 'country' && $value['value'] && !isset($lists['countries'][$value['value']]))
{
$form_check = false;
$errors[$key][] = 'value';
$notices[] = 'ERROR: Unrecognized country code';
}
if (strpos($key, 'password') !== false && ($value['maxlength'] < 8 || $value['maxlength'] > 72))
{
$form_check = false;
$errors[$key][] = 'maxlength';
$notices[] = 'ERROR: Password Max Length must be between 8 and 72';
}
if ($key == 'file' && $value['maxlength'] > 4294967295)
{
$form_check = false;
$errors[$key][] = 'maxlength';
$notices[] = 'ERROR: File maximum size is 4294967295 (4 GB)';
}
$post_fields[$key] = $value;
}
if ($form_check)
{
foreach ($post_fields as $key => $value)
{
$sql_array = array();
foreach ($value as $sub_key => $sub_value)
{
if ($sub_value != '') {$sub_value = "'" . mysqli_real_escape_string($GLOBALS['db_connect'], $sub_value) . "'";} else {$sub_value = 'NULL';}
$sql_array[] = $sub_key . ' = ' . $sub_value;
}
$sql = 'UPDATE fields SET ' . implode(', ', $sql_array) . ' WHERE field = "' . $key . '"';
@mysqli_query($GLOBALS['db_connect'], $sql) or exit_error('query failure: UPDATE fields');
}
$notice = 'Fields settings updated successfully';
}
else
{
$notices = array_unique($notices);
$notice = implode('<br>', $notices);
}
}
if ($submit == 'reset defaults')
{
reset_defaults('fields', 'Fields', $defaults['fields']);
}
}
if ($module == 'configuration' && $submodule == 'groups')
{
if ($submit) {form_hash('validate');}
if ($submit == 'update')
{
foreach ($defaults['groups'] as $key => $value)
{
$sql = 'UPDATE `groups` SET ';
if (isset($_POST['groups'][$key]))
{
if (isset($_POST['groups'][$key]['allowed_forwards'])) {$sql .= "allowed_forwards = '" . implode(',', $_POST['groups'][$key]['allowed_forwards']) . "', ";} else {$sql .= 'allowed_forwards = NULL, ';}
if (isset($_POST['groups'][$key]['blind'])) {$sql .= "blind = 'Y'";} else {$sql .= 'blind = NULL';}
}
else
{
$sql .= 'allowed_forwards = NULL, blind = NULL';
}
$sql .= " WHERE name = '$key'";
@mysqli_query($GLOBALS['db_connect'], $sql) or exit_error('query failure: UPDATE groups');
$notice = 'Group settings updated successfully';
}
}
if ($submit == 'reset defaults')
{
reset_defaults('groups', 'Groups', $defaults['groups']);
}
}
if ($module == 'configuration' && $submodule == 'genres')
{
if ($submit) {form_hash('validate');}
if ($submit == 'update')
{
if (!$_POST['genres']['new']['name']) {unset($_POST['genres']['new']);}
foreach ($_POST['genres'] as $key => $value)
{
$value = cleanup($value, 'strip_tags', 'stripslashes');
if ($value['submission_limit']) {$value['submission_limit'] = preg_replace('/[^0-9]/i', '', $value['submission_limit']);}
if ($value['price']) {$value['price'] = preg_replace('/[^0-9.]/i', '', $value['price']);}
if ($value['name'] != '')
{
$genre_names[] = strtolower($value['name']);
}
else
{
$form_check = false;
$errors[$key][] = 'name';
$notices[] = 'ERROR: Genre Names cannot be blank';
}
if ($value['submission_limit'] && !is_numeric($value['submission_limit']))
{
$form_check = false;
$errors[$key][] = 'submission_limit';
$notices[] = 'ERROR: Submission Limits must be numeric';
}
if ($value['price'] && !is_numeric($value['price']))
{
$form_check = false;
$errors[$key][] = 'price';
$notices[] = 'ERROR: Prices must be numeric';
}
if ($value['submission_limit'] && (int) $value['submission_limit'] > 255)
{
$form_check = false;
$errors[$key][] = 'submission_limit';
$notices[] = 'ERROR: Maximum submission limit is 255';
}
if ($value['price'] && (float) $value['price'] > 9999.99)
{
$form_check = false;
$errors[$key][] = 'price';
$notices[] = 'ERROR: Maximum price is $9999.99';
}
if ($value['submission_limit'] == '') {$value['submission_limit'] = '0';}
if ($value['price'] == '') {$value['price'] = '0.00';} else {$value['price'] = number_format($value['price'], 2, '.', '');}
if (!isset($value['active'])) {$value['active'] = '';}
if (!isset($value['blind'])) {$value['blind'] = '';}
$post_genres[$key] = $value;
}
$counts = array_count_values($genre_names);
foreach ($counts as $key => $value)
{
if ($value > 1)
{
$form_check = false;
$notices[] = 'ERROR: All genre names must be unique';
break;
}
}
if ($form_check)
{
foreach ($post_genres as $key => $value)
{
$sql_array = array();
foreach ($value as $field_name => $field_value)
{
if ($field_value != '') {$field_value = "'" . mysqli_real_escape_string($GLOBALS['db_connect'], $field_value) . "'";} else {$field_value = 'NULL';}
$sql_array[] = $field_name . ' = ' . $field_value;
}
if ($key != 'new')
{
$sql = 'UPDATE genres SET ' . implode(', ', $sql_array) . ' WHERE genre_id = ' . $key;
@mysqli_query($GLOBALS['db_connect'], $sql) or exit_error('query failure: UPDATE genres');
}
else
{
$sql = 'INSERT INTO genres SET ' . implode(', ', $sql_array);
@mysqli_query($GLOBALS['db_connect'], $sql) or exit_error('query failure: INSERT genres');
}
}
$notice = 'Genre settings updated successfully';
unset($post_genres['new']);
}
else
{
$notices = array_unique($notices);
$notice = implode('<br>', $notices);
}
}
if ($submit == 'reset defaults')
{
reset_defaults('genres', 'Genres', $defaults['genres']);
// need to change submissions with orphaned genres
$sql = 'UPDATE submissions SET genre_id = NULL WHERE genre_id NOT IN(' . implode(',', array_keys($defaults['genres'])) . ')';
@mysqli_query($GLOBALS['db_connect'], $sql) or exit_error('query failure: UPDATE submissions NULL genre_id');
}
if (isset($_GET['genre_id']) && $_GET['genre_id'] && is_numeric($_GET['genre_id']) && isset($_GET['delete']))
{
$genre_id = (int) trim($_GET['genre_id']);
$sql = 'DELETE FROM genres WHERE genre_id = ' . mysqli_real_escape_string($GLOBALS['db_connect'], $genre_id);
@mysqli_query($GLOBALS['db_connect'], $sql) or exit_error('query failure: DELETE genre_id');
// need to change submissions with orphaned genres
$sql = 'UPDATE submissions SET genre_id = NULL WHERE genre_id = ' . mysqli_real_escape_string($GLOBALS['db_connect'], $genre_id);
@mysqli_query($GLOBALS['db_connect'], $sql) or exit_error('query failure: UPDATE submissions NULL genre_id');
$notice = 'Genre #' . $genre_id . ' deleted';
}
}
if ($module == 'configuration' && $submodule == 'payment_vars')
{
if ($submit) {form_hash('validate');}
if ($submit == 'update')
{
$out_in = array('out' => 0, 'in' => 0);
$counts = array('$submission_id' => $out_in, '$result_code' => $out_in);
foreach ($_POST['payment_vars'] as $key => $value)
{
$value = cleanup($value, 'strip_tags', 'stripslashes');
if (is_numeric($key) && ($value['name'] == '' || $value['value'] == ''))
{
$form_check = false;
$notice = 'ERROR: both name and value must not be blank';
}
if ($key == 'new')
{
if ($value['name'] == '' && $value['value'] == '')
{
echo '';
}
elseif ($value['name'] != '' && $value['value'] != '')
{
echo '';
}
else
{
$form_check = false;
$notice = 'ERROR: both name and value must not be blank';
}
}
if (isset($counts[$value['value']])) {$counts[$value['value']][$value['direction']]++;}
$post_payment_vars[$key] = $value;
}
// check counts for $submission_id and $result_code
if ($counts['$submission_id']['in'] > 1)
{
$form_check = false;
$notice = 'ERROR: only one incoming submission_id payment variable can be used';
}
if ($counts['$result_code']['out'] > 0)
{
$form_check = false;
$notice = 'ERROR: result_code must only be an incoming payment variable (not outgoing)';
}
if ($counts['$result_code']['in'] > 1)
{
$form_check = false;
$notice = 'ERROR: only one incoming result_code payment variable can be used';
}
if ($form_check)
{
foreach ($post_payment_vars as $payment_key => $payment_value)
{
foreach ($payment_value as $payment_value_key => $payment_value_value) {$payment_value[$payment_value_key] = mysqli_real_escape_string($GLOBALS['db_connect'], $payment_value_value);}
extract($payment_value);
if ($payment_key == 'new')
{
if ($name == '' && $value == '') {break;}
$sql_start = 'INSERT INTO';
$sql_end = ", direction = '$direction'";
}
else
{
$sql_start = 'UPDATE';
$sql_end = " WHERE payment_var_id = " . mysqli_real_escape_string($GLOBALS['db_connect'], $payment_key);
}
$sql = $sql_start . " payment_vars set name = '$name', value = '$value'" . $sql_end;
@mysqli_query($GLOBALS['db_connect'], $sql) or exit_error('query failure: INSERT payment_vars');
}
$notice = 'Payment variables settings updated successfully';
}
}
if ($submit == 'reset defaults')
{
reset_defaults('payment_vars', 'Payment Variables');
@mysqli_query($GLOBALS['db_connect'], 'ALTER TABLE `payment_vars` COMMENT = ""') or exit('query failure: ALTER TABLE payment_vars COMMENT');
$payment_vars_config = array(
'payment_redirect_method',
'success_result_code',
'cc_exp_date_format',
'show_date_paid',
'show_payment_fields'
);
foreach ($payment_vars_config as $value)
{
if ($config_defaults[$value] == '') {$value_sql = 'NULL';} else {$value_sql = "'" . $config_defaults[$value] . "'";}
$sql = "UPDATE config SET value = $value_sql WHERE name = '$value'";
$result = mysqli_query($GLOBALS['db_connect'], $sql) or exit_error('query failure: INSERT config FROM payment_vars_preset');
}
}
if (isset($_GET['payment_var_id']) && $_GET['payment_var_id'] && is_numeric($_GET['payment_var_id']) && isset($_GET['delete']))
{
$payment_var_id = (int) trim($_GET['payment_var_id']);
@mysqli_query($GLOBALS['db_connect'], 'DELETE FROM payment_vars WHERE payment_var_id = ' . mysqli_real_escape_string($GLOBALS['db_connect'], $payment_var_id)) or exit_error('query failure: DELETE payment variable');
$notice = 'Payment Variable #' . $payment_var_id . ' deleted';
}
}
}
if (INSTALLED && $GLOBALS['db_connect'])
{
get_genres();
get_file_types();
get_fields();
get_action_types();
// needed to suppress errors before version 3.41 update
if (isset($fields['password']['maxlength'])) {$password_length_max = $fields['password']['maxlength'];}
if (isset($fields['file']['maxlength'])) {$max_file_size_formatted = get_bytes_formatted($fields['file']['maxlength']);}
$timezone = $config['timezone'];
$timezone_safe = (float) $timezone;
$is_DST = 0;
if ($config['dst'] && date('I', $gm_timestamp) == 1) {$timezone += 1; $is_DST = 1;}
if ($timezone >= 0) {$timezone = '+' . $timezone;}
if (strpos($timezone, '.5') !== false) {$timezone = str_replace('.5', ':30', $timezone);} else {$timezone .= ':00';}
$local_date_time = timezone_adjust($gm_date_time) . ' (GMT ' . $timezone . ')';
$local_date = substr($local_date_time, 0, 10);
@mysqli_query($GLOBALS['db_connect'], 'SET time_zone = "' . $timezone . '"') or exit_error('query failure: SET time_zone');
@$timezone_name = timezone_name_from_abbr('', $timezone_safe * 3600, $is_DST);
@date_default_timezone_set($timezone_name);
}
function get_genres()
{
global $show_tables;
$GLOBALS['genres'] = array();
if (in_array('genres', $show_tables))