-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnuapi.php
3417 lines (2591 loc) · 105 KB
/
nuapi.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
header("Content-Type: application/json");
header("Cache-Control: no-cache, must-revalidate");
require_once('nucommon.php');
$response = array();
$response['DATA'] = '';
$response['SUCCESS'] = false;
$response['ERRORS'] = array();
$response['TIMEOUT'] = 0;
$GLOBALS['ERRORS'] = array();
$GLOBALS['EXTRAJS'] = '';
nuGetFormProperties(nuV('form_id'));
$hashData = nuHashData();
$GLOBALS['hashData'] = $hashData;
//==============================================================================
// LOGIN
//==============================================================================
if (nuV('call_type') == 'login') {
$response['DATA'] = nuLogin(nuV('username'), nuV('password'));
nuClearDebug();
} else {
$message = nuValidateUser(nuV('session_id'), $hashData); //-- VALIDATE USER
if ($message != '') {
$response['ERRORS'][] = $message;
print json_encode($response);
return;
}
}
//==============================================================================
// CHECK ACCESS FOR RUNPHP OR PRINTPDF
//==============================================================================
if (nuV('call_type') == 'check_edit') {
$J['user'] = nuCheckEdit();
$response['DATA'] = json_encode($J);
}
//==============================================================================
// CHECK ACCESS FOR RUNPHP OR PRINTPDF
//==============================================================================
if (nuV('call_type') == 'validateaccess') {
$J['id'] = nuPDForPHPParameters($hashData, nuV('validate'));
$J['iframe'] = nuV('iframe') == 0 ? 0 : 1;
$response['DATA'] = json_encode($J);
nuV('call_type', 'validateaccess'); //-- reset calltype
}
//==============================================================================
// PRINTPDF OR RUNPHP
//==============================================================================
if (nuV('call_type') == 'printpdf' or nuV('call_type') == 'runphp') {
if(nuV('iframe') == '0'){ //-- don't check for blanks if coming from an iFrame
$response['ERRORS'] = nuCheckParametersForm(nuV('form_data'));
}else{
$response['ERRORS'] = array();
}
if (count($response['ERRORS']) == 0) { //-- if no error messages
$J['id'] = nuPDForPHPParameters($hashData); //-- puts a JSON string in zzzsys_debug and returns the primary key
}
$J['iframe'] = nuV('iframe') == 0 ? 0 : 1;
$response['DATA'] = json_encode($J);
}
//==============================================================================
// PRINTBROWSE
//==============================================================================
if (nuV('call_type') == 'runprintbrowse') {
$J['id'] = nuGetBrowseHTML($hashData); //-- puts a JSON string in zzzsys_debug and returns the primary key
$response['DATA'] = json_encode($J);
}
//==============================================================================
// SAVE MOVED OBJECTS
//==============================================================================
if (nuV('call_type') == 'savemovedobjects') {
if($_SESSION['IsDemo'] == 1){$response['ERRORS'][] = 'Unavailable in Demo';} //-- ($nuConfigIsDemo = 1 in config.php)
if (count($response['ERRORS']) == 0) { //-- if no error messages
nuSaveMovedObjects();
}
$hashData = nuHashData();
$response['DATA'] = nuGetEditForm($hashData);
}
//==============================================================================
// DELETEFORM
//==============================================================================
if (nuV('call_type') == 'deleteform') {
if($_SESSION['IsDemo'] == 1){$response['ERRORS'][] = 'Unavailable in Demo';} //-- ($nuConfigIsDemo = 1 in config.php)
$beforeDelete = nuReplaceHashes(nuF('sfo_custom_code_run_before_delete'), $hashData);
eval($beforeDelete);
if (count($response['ERRORS']) == 0) { //-- if no error messages
nuSaveForm(nuV('form_data'), $hashData);
}
$hashData = nuHashData();
$response['DATA'] = nuGetEditForm($hashData);
}
//==============================================================================
// SAVEFORM
//==============================================================================
if (nuV('call_type') == 'saveform') {
$response['ERRORS'] = nuCheckForm(nuV('form_data'));
if($_SESSION['IsDemo'] == 1){$response['ERRORS'][] = 'Unavailable in Demo';} //-- ($nuConfigIsDemo = 1 in config.php)
$nuHash = $hashData; //-- used in eval() along with hash variables
$beforeSave = nuReplaceHashes(nuF('sfo_custom_code_run_before_save'), $hashData);
if (count($response['ERRORS']) == 0) { //-- if no error messages
eval($beforeSave);
if (count($response['ERRORS']) == 0) { //-- if still no error messages
nuSaveForm(nuV('form_data'), $hashData);
}
}
$hashData = nuHashData();
$response['DATA'] = nuGetEditForm($hashData);
}
//==============================================================================
// LOOKUPID
//==============================================================================
if (nuV('call_type') == 'lookupid') {
$response['DATA'] = nuGetLookupData($hashData);
}
//==============================================================================
// LOOKUPCODE
//==============================================================================
if (nuV('call_type') == 'lookupcode') {
$response['DATA'] = nuGetLookupData($hashData);
}
//==============================================================================
// AUTOCOMPLETE
//==============================================================================
if (nuV('call_type') == 'autocomplete') {
$response['DATA'] = nuGetAutocompleteData($hashData);
}
//==============================================================================
// CLONEFORM
//==============================================================================
if (nuV('call_type') == 'cloneform') {
$response['ERRORS'] = nuCheckForm(nuV('form_data'));
if (count($response['ERRORS']) == 0) {
nuSaveForm(nuV('form_data'), $hashData);
$hashData = nuHashData();
$response['DATA'] = nuGetEditForm($hashData);
}
}
//==============================================================================
// GETEDITFORM
//==============================================================================
if (nuV('call_type') == 'geteditform') {
$response['DATA'] = nuGetEditForm($hashData);
}
//==============================================================================
// GETBROWSEFORM
//==============================================================================
if (nuV('call_type') == 'getbrowseform') {
$response['DATA'] = nuGetBrowseForm($hashData);
}
//==============================================================================
// GETLOOKUPFORM
//==============================================================================
if (nuV('call_type') == 'getlookupform') {
$response['DATA'] = nuGetBrowseForm($hashData);
}
$response['ERRORS'] = array_merge($response['ERRORS'], $GLOBALS['ERRORS']);
$response['SUCCESS'] = true;
$response['TIMEOUT'] = nuV('nu_timeout');
nuRemoveTempTables($hashData);
print json_encode($response);
//===============================functions======================================
function nuGetBrowseHTML($hashData) {
$formID = nuV('form_id');
$pageNo = nuV('page_number');
$J['session'] = nuV('session_id');
$J['breadcrumb'] = nuV('breadcrumb');
$J['call_type'] = nuV('call_type');
$J['edit_browse'] = nuV('edit_browse');
$J['form_id'] = nuV('form_id');
$J['nu_user_name'] = nuV('nu_user_name');
$t = nuRunQuery("SELECT * FROM zzzsys_form WHERE zzzsys_form_id = ? ", array(nuV('form_id')));
if (nuErrorFound()) {
return;
}
$r = db_fetch_object($t);
$J['search'] = nuV('search');
$J['filter'] = nuV('filter');
$J['form_title'] = nuF('sfo_title');
$J['set_title'] = nuV('set_title');
$beforeBrowse = nuReplaceHashes(nuF('sfo_custom_code_run_before_browse'), $hashData);
eval($beforeBrowse);
return nuBuildTable($formID, $pageNo, $hashData);
}
function nuBuildTable($f, $p, $hashData) {
$tt = '__' . nuID() . '__';
$OBJ = array();
$s = "SELECT * FROM zzzsys_form WHERE zzzsys_form_id = '$f'";
$t = nuRunQuery($s);
if (nuErrorFound()) {
return;
}
$r = db_fetch_object($t);
$S = "SELECT * FROM zzzsys_browse WHERE sbr_zzzsys_form_id = ? ORDER BY sbr_order";
$T = nuRunQuery($S, array($f));
if (nuErrorFound()) {
return;
}
$searchFields = array();
$searchColumns = explode(',', nuV('search_columns')); //-- create an array of searchable columns
$columnCount = 1;
while ($R = db_fetch_object($T)) { //-- create columns
$nuObject = new stdClass;
// $nuObject->title = $R->sbr_title;
$nuObject->title = trim(preg_replace("/[^0-9a-zA-Z _]/", "", $R->sbr_title)); //-- changed 2015-04-07 by SC
$nuObject->value = $R->sbr_display;
$nuObject->align = $R->sbr_align;
$nuObject->format = $R->sbr_format;
$nuObject->width = $R->sbr_width;
$OBJ[] = $nuObject;
if (count($searchColumns) == 1) {
nuV('search_columns', nuV('search_columns') . ",$columnCount"); //-- create a comma delimited string of searchable columns
$searchFields[] = $R->sbr_display;
} else {
if (in_array($columnCount - 1, $searchColumns)) {
$searchFields[] = $R->sbr_display; //-- searchable column
}
}
$columnCount++;
}
if (nuV('edit_browse') == 'true') {
$hashData = array_merge($hashData, nuGetCurrentData()); //-- use values on current page for hash data
}
$hashedSQL = $r->sfo_sql;
$SQL = new nuSqlString($hashedSQL);
$SQL->removeAllFields();
$SQL->addField($r->sfo_primary_key);
$width = 0;
for ($i = 0; $i < count($OBJ); $i++) {
if ($OBJ[$i]->format == '') {
$SQL->addField("IFNULL(".$OBJ[$i]->value.",'') AS `" . $OBJ[$i]->title . "`");
} else {
$format = nuTextFormats();
$sql = $format[$OBJ[$i]->format]->sql;
$newField = str_replace('??', $OBJ[$i]->value, $sql);
$SQL->addField("IFNULL(".$newField.",'') AS `" . $OBJ[$i]->title . "`");
}
$width = $width + $OBJ[$i]->width;
}
if (nuV('search') != '' or nuV('filter') != '') {
if ($SQL->getWhere() == '') {
$searchString = ' WHERE ' . nuBrowseWhereClause($searchFields, nuV('search') . ' ' . nuV('filter'));
} else {
$searchString = $SQL->getWhere() . ' AND ' . nuBrowseWhereClause($searchFields, nuV('search') . ' ' . nuV('filter'));
}
$SQL->setWhere($searchString);
}
if (nuV('sort') != 0) {
$SQL->setOrderBy(' ORDER BY ' . nuV('sort') . ' ' . nuV('descending'));
}
$formattedSQL = nuReplaceHashes($SQL->SQL, $hashData);
nuRunQuery("CREATE TABLE $tt $formattedSQL");
if (nuErrorFound()) {
return;
}
$J['objects'] = $OBJ;
$J['records'] = $tt; //-- temp tablename
$j = json_encode($J);
$i = nuID();
$d = date('Y-m-d h:i:s');
nuRunQuery("INSERT INTO zzzsys_debug (zzzsys_debug_id, deb_message, deb_added) VALUES(?, ?, ?)", array($i, $j, $d));
if (nuErrorFound()) {
return;
}
return $i;
}
function nuSaveMovedObjects() {
if (nuV('nu_user_name') != 'globeadmin') {
return;
} //-- only globeadmin can save move changes
$m = nuV('moved_objects'); //-- split up objects
$f = nuV('form_id');
for ($a = 0; $a < count($m); $a++) {
$i = $m[$a]['id'];
$t = $m[$a]['top'];
$l = $m[$a]['left'];
$o = $m[$a]['order'];
$s = "UPDATE zzzsys_object SET sob_all_column_number = '10', sob_all_order_number = '$o', sob_all_top = '$t' , sob_all_left = '$l' WHERE sob_zzzsys_form_id = '$f' AND zzzsys_object_id = '$i' ";
nuRunQuery($s);
if (nuErrorFound()) {
return;
}
}
}
function nuValidateUser($session, $hashData) {
nuLogUser($session); //-- records user activity
$timeout = time() - (60 * $GLOBALS['nuSetup']->set_time_out_minutes);
nuRunQuery("DELETE FROM zzzsys_session WHERE sss_timeout < ? ", array($timeout));
if (nuErrorFound()) {
return;
}
$t = nuRunQuery("SELECT * FROM zzzsys_session WHERE zzzsys_session_id = ? ", array($session));
if (nuErrorFound()) {
return;
}
$r = db_fetch_object($t);
$time = time();
$user = $r->sss_zzzsys_user_id;
if ($user == '') {
return 'You are not currently logged in'; //-- access to nothing
}
nuRunQuery("UPDATE zzzsys_session SET sss_timeout = $time WHERE zzzsys_session_id = ? ", array($r->zzzsys_session_id));
nuV('nu_timeout', $time);
if (nuErrorFound()) {
return;
}
if ($user == 'globeadmin') {
return '';
} //-- access to everything
$formID = nuV('form_id');
$recordID = nuV('record_id');
if(!in_array($formID, $_SESSION['nu_form_access'])){
nuHomeBug();
$t = nuRunQuery("SELECT CONCAT(sfo_name, ' - ', sfo_title) FROM zzzsys_form WHERE zzzsys_form_id = '$formID'");
$r = db_fetch_row($t);
nuDisplayError("You do not have access to this form (" . $r[0] . "). \r Please contact your system administrator");
return;
}
//===================save, new, clone and delete============================
if (nuV('call_type') == 'saveform') { //-- new button also calls nuSaveForm()
$sql = "SELECT * FROM zzzsys_form WHERE zzzsys_form_id = '$formID'";
$t = nuRunQuery($sql);
$r = db_fetch_object($t);
if($_SESSION['nu_access_'.$formID]['save'] == '1') {
return "'Save' not allowed on this form for this user. \n Please contact your system administrator";
}
}
if (nuV('call_type') == 'deleteform') {
$sql = "SELECT * FROM zzzsys_form WHERE zzzsys_form_id = '$formID'";
$t = nuRunQuery($sql);
$r = db_fetch_object($t);
if($_SESSION['nu_access_'.$formID]['delete'] == '1') {
return "'Delete' not allowed on this form for this user. \n Please contact your system administrator";
}
}
if (nuV('call_type') == 'cloneform') {
$sql = "SELECT * FROM zzzsys_form WHERE zzzsys_form_id = '$formID'";
$t = nuRunQuery($sql);
$r = db_fetch_object($t);
if($_SESSION['nu_access_'.$formID]['clone'] == '1') {
return "'Clone' not allowed on this form for this user. \n Please contact your system administrator";
}
}
return '';
}
//=================test for losing access====================================
function nuHomeBug(){
$a = print_r($_SESSION['nu_form_access'],1);
$user = nuV('nu_user_name') . ' ( ' . nuV('call_type') . ' )';
$array = debug_backtrace();
$trace = '';
for($i = 0 ; $i < count($array) ; $i ++){
$trace .= $array[$i]['file'] . ' - line ' . $array[$i]['line'] . ' (' . $array[$i]['function'] . ")\n\n";
}
$debug = "
** HOME BUG **
===USER==========
$user
===BACK TRACE====
$trace
===VARIABLE====
$a
";
nuDebug($debug);
}
//=====================================================
function nuValidateRecord($r, $hashData) {
$recordID = nuV('record_id');
if ($recordID == '-1') {
return true;
}
if ($recordID == '') {
return true;
}
$r->sfo_custom_code_run_before_browse = nuGetSafePHP('sfo_custom_code_run_before_browse', $recordID, $r->sfo_custom_code_run_before_browse);
$bb = nuReplaceHashes($r->sfo_custom_code_run_before_browse, $hashData);
eval($bb);
$sfo_sql = nuReplaceHashes($r->sfo_sql, $hashData);
$SQL = new nuSqlString($sfo_sql);
$table = $SQL->getTableName();
$SQL->addWhereClause("$r->sfo_primary_key = '$recordID'");
$s = "SELECT $r->sfo_primary_key FROM $table " . $SQL->getWhere();
$t = nuRunQuery($s);
if (nuErrorFound()) {
return;
}
$r = db_fetch_row($t);
if ($r[0] == '') {
return false;
} else {
return true;
}
}
function nuDisplayError($message) {
$GLOBALS['ERRORS'][] = $message;
}
function nuHashArray($d) {
$stamp = array();
$hashData['PREVIOUS_RECORD_ID'] = $d['data'][0]['records'][0]['primary_key'];
$array = array();
for ($FORM = 0; $FORM < count($d['data']); $FORM++) { //-- loop through forms (0 being the main form)
$parentID = $d['data'][$FORM]['form_id'];
if ($FORM == 0) { //-- form
$s = "
SELECT sfo_title as form_name,
sfo_table as parent_table,
sfo_primary_key as parent_primary_key
FROM zzzsys_form
WHERE zzzsys_form_id = '$parentID'
";
} else { //-- subform
$s = "
SELECT
sob_all_title as form_name,
sob_subform_table as parent_table,
sob_subform_primary_key as parent_primary_key,
sob_all_name,
sob_subform_foreign_key
FROM zzzsys_object
WHERE zzzsys_object_id = '$parentID'
";
}
$t = nuRunQuery($s);
if (nuErrorFound()) {
return;
}
$formInfo = db_fetch_object($t);
$RECORDS = $d['data'][$FORM]['records'];
for ($R = 0; $R < count($RECORDS); $R++) { //-- loop through form field values
$prefix = '';
if($FORM > 0){
$prefix = $formInfo->sob_all_name . sprintf('%04d', $R);
}
$RECORD = $RECORDS[$R];
$PK = addslashes($RECORD['primary_key']);
$FIELDS = $RECORD['fields'];
$DEL = $RECORD['delete_record'];
for ($F = 0; $F < count($FIELDS); $F++) {
$FIELD = $FIELDS[$F];
$array[$prefix . $FIELD['field']] = $FIELD['value']; //-- build values into array
}
}
}
return $array;
}
function nuSaveForm($d, $hashData) {
$stamp = array();
$hashData['PREVIOUS_RECORD_ID'] = $d['data'][0]['records'][0]['primary_key'];
for ($FORM = 0; $FORM < count($d['data']); $FORM++) { //-- loop through forms (0 being the main form)
$parentID = $d['data'][$FORM]['form_id'];
if ($FORM == 0) { //-- form
$s = "
SELECT
sfo_title as form_name,
sfo_table as parent_table,
sfo_primary_key as parent_primary_key
FROM zzzsys_form
WHERE zzzsys_form_id = '$parentID'
";
} else { //-- subform
$s = "
SELECT
sob_all_title as form_name,
sob_subform_table as parent_table,
sob_subform_primary_key as parent_primary_key,
sob_subform_foreign_key
FROM zzzsys_object
WHERE zzzsys_object_id = '$parentID'
";
}
$t = nuRunQuery($s);
if (nuErrorFound()) {
return;
}
$formInfo = db_fetch_object($t);
if(array_key_exists('records',$d['data'][$FORM])) {
$RECORDS = $d['data'][$FORM]['records'];
for ($R = 0; $R < count($RECORDS); $R++) { //-- loop through form field values
$SET = array();
$RECORD = $RECORDS[$R];
$PK = addslashes($RECORD['primary_key']);
$FIELDS = $RECORD['fields'];
$DEL = $RECORD['delete_record'];
$validFields = nuValidateArray($parentID, $formInfo->parent_table);
$valid = array(); // ADDED by SG 9Jun2015
for ($V = 0; $V < count($validFields); $V++) {
$valid[] = $validFields[$V]->field_name;
}
for ($F = 0; $F < count($FIELDS); $F++) {
$FIELD = $FIELDS[$F];
if (in_array($FIELD['field'], $valid) and $FIELD['save'] == 1) {
if($FIELD['value'] == ''){
$SET[] = addslashes($FIELD['field']) . ' = ' . 'NULL'; //-- Update empty Fields to NULL - added by OldShatterhand77 2014-02-07
}else{
$SET[] = addslashes($FIELD['field']) . ' = ' . "'" . addslashes($FIELD['value']) . "'"; //-- build UPDATE SET values
}
}
}
if ($DEL == 'yes') {
$s = "DELETE FROM $formInfo->parent_table WHERE $formInfo->parent_primary_key = '$PK'";
} else {
if (count($SET) > 0) {
if ($PK == '') {
$PK = nuGetNewPrimaryKey($formInfo->parent_table, $formInfo->parent_primary_key); //-- insert a new record and return PK
$stamp[] = array('added', $formInfo->parent_table, $formInfo->parent_primary_key, $PK);
}
if ($FORM == 0) { //-- main form
nuV('record_id', $PK);
} else {
$SET[] = addslashes($formInfo->sob_subform_foreign_key) . ' = ' . "'" . addslashes(nuV('record_id')) . "'"; //-- add foreign key
}
$s = "UPDATE $formInfo->parent_table SET " . implode(', ', $SET) . " WHERE $formInfo->parent_primary_key = '$PK'";
$stamp[] = array('changed', $formInfo->parent_table, $formInfo->parent_primary_key, $PK);
}
if($FORM == 0) {
$hashData['RECORD_ID'] = $PK;
}
}
nuRunQuery($s);
if (nuErrorFound()) {
return;
}
}
}
}
nuLogStamp($stamp);
$nuHash = $hashData; //-- used in eval() along with hash variables
if($d['data'][0]['records'][0]['delete_record'] == 'yes'){
$eval = nuReplaceHashes(nuF('sfo_custom_code_run_after_delete'), $hashData);
}else{
$eval = nuReplaceHashes(nuF('sfo_custom_code_run_after_save'), $hashData);
}
eval($eval);
}
function nuLogStamp($stamp) { //-- records user activity
for ($i = 0; $i < count($stamp); $i++) {
$action = $stamp[$i][0];
$table = $stamp[$i][1];
$primary_key = $stamp[$i][2];
$value = $stamp[$i][3];
$user = nuV('nu_user_id');
$d = new DateTime();
$date = $d->format('Y-m-d H:i:s');
$by = $table . '_log_' . $action . '_by';
$at = $table . '_log_' . $action . '_at';
$s = "SELECT * FROM `$table` WHERE $primary_key = '$value'";
$t = nuRunQuery($s);
if (nuErrorFound()) {
return;
}
$r = db_fetch_array($t);
if (is_array($r)) {
foreach ($r as $key => $v) {
if ($key === $by) {
nuRunQuery("UPDATE `$table` set $key = '$user' WHERE $primary_key = '$value'");
if (nuErrorFound()) {
return;
}
}
if ($key === $at) {
nuRunQuery("UPDATE `$table` set $key = '$date' WHERE $primary_key = '$value'");
if (nuErrorFound()) {
return;
}
}
}
}
}
}
function nuCreateLog($id, $u) { //-- starts user activity
$d = new DateTime();
$date = $d->format('Y-m-d H:i:s');
$ip = $_SERVER['REMOTE_ADDR'];
$s = "INSERT INTO zzzsys_user_log (zzzsys_user_log_id, sul_zzzsys_user_id, sul_ip, sul_start) VALUES ('$id', '$u', '$ip', '$date');";
nuRunQuery($s);
if (nuErrorFound()) {
return;
}
}
function sisLogFailure($un) {
$id = nuID();
//-- Log Login Failure
$d = new DateTime();
$date = $d->format('Y-m-d H:i:s');
$ip = $_SERVER['REMOTE_ADDR'];
$s = "INSERT INTO zzzsys_user_login_failure (zzzsys_user_login_failure_id, sul_ip, sul_datetime, sul_username) VALUES ('$id', '$ip', '$date', '$un')";
nuRunQuery($s);
if (nuErrorFound()) {
return;
}
$setup = $GLOBALS['nuSetup'];
$interval = (is_null($setup->set_lock_login_minutes) || $setup->set_lock_login_minutes==0 ? 60 : $setup->set_lock_login_minutes);
$s = "DELETE FROM zzzsys_user_login_failure WHERE (sul_datetime < now() - INTERVAL $interval MINUTE)";
nuRunQuery($s);
if (nuErrorFound()) {
return;
}
}
function sisCountFailure($interval,$un) {
$ip = $_SERVER['REMOTE_ADDR'];
$s = "SELECT COUNT(*) as attempts FROM zzzsys_user_login_failure WHERE (sul_datetime >= now() - INTERVAL $interval MINUTE) AND sul_ip = '$ip' AND sul_username = '$un'";
$t = nuRunQuery($s);
if (nuErrorFound()) {
return;
}
$r = db_fetch_object($t);
return($r->attempts);
}
function nuLogUser($id) { //-- records user activity
$d = new DateTime();
$date = $d->format('Y-m-d H:i:s');
nuRunQuery("UPDATE zzzsys_user_log SET sul_end = '$date' WHERE zzzsys_user_log_id = '$id'");
if (nuErrorFound()) {
return;
}
}
function nuGetNewPrimaryKey($t, $p) {
global $nuDB;
$t = trim($t);
$p = trim($p);
$id = nuID();
if (db_is_auto_id($t, $p)) { //-- create auto id
$i = 'NULL';
$auto = true;
} else { //-- create string id
$i = "'$id'";
$auto = false;
}
$r = $nuDB->exec("INSERT INTO `$t` (`$p`) VALUES ($i)"); //-- insert record before updating
if ($auto) {
return $nuDB->lastInsertId();
} else {
return $id;
}
}
function nuIsForm($parentID) {
$t = nuRunQuery("SELECT * FROM zzzsys_form WHERE zzzsys_form_id = '$parentID'");
if (nuErrorFound()) {
return;
}
$r = db_fetch_object($t);
if ($r->zzzsys_form_id == '') {
return false;
} else {
return true;
}
}
function nuCheckParametersForm($d) {
$errors = array();
$i = $d['data'][0]['form_id'];
$fields = $d['data'][0]['records'][0]['fields'];
$o = array();
for($f = 0 ; $f < count($fields) ; $f ++){
$o[$fields[$f]['field']] = $fields[$f]['value']; // PHP7 '' added
}
$s = "SELECT * FROM zzzsys_object WHERE sob_all_no_blanks = '1' AND sob_zzzsys_form_id = '$i' ORDER BY sob_all_tab_number, sob_all_column_number, sob_all_order_number";
$t = nuRunQuery($s);
while($r = db_fetch_object($t)){
if($o[$r->sob_all_name] == ''){
$errors[] = "'" . addslashes($r->sob_all_title) . "' Cannot be left blank";
}
}
return $errors;
}
function nuCheckForm($d) {
$errors = array();
for ($FORM = 0; $FORM < count($d['data']); $FORM++) { //-- loop through forms (0 being the main form)
$parentID = $d['data'][$FORM]['form_id'];
if ($FORM == '0') { //-- form
$s = "
SELECT
sfo_title as form_name,
sfo_table as parent_table,
sfo_primary_key as parent_primary_key
FROM zzzsys_form
WHERE zzzsys_form_id = '$parentID'
";
$mainForm = true;
} else { //-- subform
$s = "
SELECT
sob_all_title as form_name,
sob_subform_table as parent_table,
sob_subform_primary_key as parent_primary_key
FROM zzzsys_object
WHERE zzzsys_object_id = '$parentID'
";
$mainForm = false;
}
$t = nuRunQuery($s);
if (nuErrorFound()) {
return;
}
$formInfo = db_fetch_object($t);
$validFields = nuValidateArray($parentID, $formInfo->parent_table); //-- list of valid fields on this Form
if(array_key_exists('records',$d['data'][$FORM])) $RECORDS = $d['data'][$FORM]['records'];
for ($R = 0; $R < count($RECORDS); $R++) {
$RECORD = $RECORDS[$R];
$FIELDS = $RECORD['fields'];
if ($RECORD['delete_record'] <> 'yes') { //-- does need to be checked
for ($F = 0; $F < count($FIELDS); $F++) {
$FIELD = $FIELDS[$F];
$error = nuValidField($FIELD, $validFields, $formInfo, $RECORD['primary_key'], $R + 1, $mainForm, $parentID); //-- check for valid table fieldname and value
if (count($error) != 0) {
$errors[] = $error->message;
}
}
}
}
}
return $errors;
}
function nuValidField($FIELD, $validFields, $formInfo, $primary_key, $ROW, $mainForm, $parentID) {
for ($i = 0; $i < count($validFields); $i++) {
$val = addslashes($FIELD['value']);
$fld = addslashes($FIELD['field']);
if ($validFields[$i]->field_name == $fld) { //-- using a valid field name
if (($validFields[$i]->no_blanks == '1' or $validFields[$i]->no_duplicates == '1' ) and $val == '') { //-- cannot be left blank
$ERR = nuGetTabAndTitle($fld, $mainForm, $parentID);
if ($mainForm) { //-- main form
$ERR->message = "$ERR->title " . nuTranslate('cannot be left blank') . ".. (" . nuTranslate('TAB') . " : $ERR->tab)";
} else {
$ERR->message = "$ERR->title " . nuTranslate('in subform') . " " . nuTranslate('on row') . " $ROW " . nuTranslate('cannot be left blank') . "..";
}
return $ERR;
}
if ($validFields[$i]->no_duplicates == '1') { //-- check for duplicates
$s = "
SELECT $formInfo->parent_primary_key
FROM $formInfo->parent_table
WHERE $fld = '$val' AND $formInfo->parent_primary_key != '$primary_key'
";
$t = nuRunQuery($s);
if (nuErrorFound()) {
return;
}
$r = db_fetch_row($t);
if ($r[0] != '') { //-- duplicate record
$ERR = nuGetTabAndTitle($fld, $mainForm, $parentID);
if ($mainForm) { //-- main form
$ERR->message = nuTranslate('There is already a record with a') . " $ERR->title " . nuTranslate('with a value of') . " $val.. (" . nuTranslate('TAB') . " : $ERR->tab)";
} else {
$ERR->message = nuTranslate('There is already a record with a') . " $ERR->title " . nuTranslate('with a value of') . " $val " . nuTranslate('in subform') . " $formInfo->form_name " . nuTranslate('on row') . " $ROW.. (" . nuTranslate('TAB') . " : $ERR->tab)";
}
return $ERR;
}
}
if ($validFields[$i]->format == 'text' and $validFields[$i]->format != '' and $val != '') { //-- check format
$format = nuTextFormats();
$datatype = $format[$validFields[$i]->format]->type;
if ($datatype == 'date') {
$s = "SELECT IF('$val' = DATE_FORMAT('$val','%Y-%m-%d'),'1','0')";
$t = nuRunQuery($s);
if (nuErrorFound()) {
return;
}
$r = db_fetch_row($t);
if ($r[0] == '1') {
return array();
} else {
$ERR = nuGetTabAndTitle($fld, $mainForm, $parentID);
if ($mainForm) { //-- main form
$ERR->message = "$ERR->title " . nuTranslate('has an invalid date') . ".. (" . nuTranslate('TAB') . " : $ERR->tab)";
} else {
$ERR->message = "$ERR->title " . nuTranslate('has an invalid date') . " " . nuTranslate('in subform') . " $formInfo->form_name " . nuTranslate('on row') . " $ROW.. (" . nuTranslate('TAB') . " : $ERR->tab)";
}
return $ERR;
}
}
if ($datatype == 'number') {
$s = "SELECT IF($val = $val+0,'1','0')";
$t = nuRunQuery($s);
if (nuErrorFound()) {