-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbios.php
3598 lines (2922 loc) · 102 KB
/
bios.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
/*
Simple Query Parsing PHP CMS
(c) 2010 nkcomp.com, Dexosoft, Nazar Kuliyev, C.I.S., Kazakhstan
*/
/// BIOS
///
// now version is file size!!!!
//1.15 - db_models_validate, ln_inc, ln_dec
//1.14 - HTTPPost, HTTPPostFix, HTTP_POST_DOUBLE_SLASH - double slash replace
//1.13 - table_Edit - actionstring - can be function(id) now
//1.12 - table_edit - added action string
//1.11 - table_edit, db_list
//1.10 - form_list fix
//1.09 - DICT, T()
//1.08
//form_object_edit
//form_object_add
//db_object_del
//1.07
//1.06
// 1.05
// added static_
// added table_
/** version 1.01 - Feb 20, 2009
// added sqlite2 support
/** 1FCMS v1.00 **/
//functions go first, main() is at the end
/**
* Table Editor
* (inspired by Ruby)
* edit,delete,add new record, paging options
* TableBrowser::browse("test_table","id","id,username as user_name",10,"EVDN");
* "EVDN" - Edit, View, Delete, New
* @author Nazar Kuliev 09/2007 (projectplaylist.com)
* @package Utilities
* @version 1.05
*/
//1.06 - show serialized data
//1.05 - browsetable added
//1.04 - fix in record view
//1.03 - 13/09/2007 - $option_showtitle added, little comments added
//1.02 - pagesize=0 - means no total count
//1.01 - browse_echo removed, mainsql parameter added to browse method
function errormsg($s) {
return "<span class='errormsg'>$s</span>";
}
function sql($s,$s1="",$s2="",$s3="",$s4="",$s5="",$s6="",$s7="",$s8="",$s9="",$s10="") {
return db_query($s,$s1,$s2,$s3,$s4,$s5,$s6,$s7,$s8,$s9,$s10);
}
function ql($s,$s1="",$s2="",$s3="",$s4="",$s5="",$s6="",$s7="",$s8="",$s9="",$s10="") {
return db_query($s,$s1,$s2,$s3,$s4,$s5,$s6,$s7,$s8,$s9,$s10);
}
assert_options(ASSERT_BAIL,true);
function T($s) {
global $DICT;
if(empty($DICT) || empty($DICT[$s])) {
return $s;
} else {
return $DICT[$s];
}
}
//*
//* validate tables array - database structure, check if all fields do exist in database
//*
function db_models_validate() {
global $tables;
foreach($tables as $tablename=>$table) {
ln("table:".$tablename);
ln_inc();
foreach($table["fields"] as $field) {
//ln($field);
//ln("running query");
$res = db_query("SELECT $field FROM $tablename LIMIT 1");
}
ln_dec();
}
}
/**
* Simple function for browsing table
* calling TableBrowser, assuming that first field in table description is keyfield
*
* @param string $tablename Table in currently connected database
* @param string $mode "EVDN" Edit,View,Delete,New by default
*/
function browsetable_code($tablename,$mode="EVDN",$actstring="") {
ob_start();
browsetable($tablename,$mode,$actstring);
$s = ob_get_contents();
ob_end_clean();
return $s;
}
function browsetable($tablename,$mode="EVDN",$actionstring="") {
$fields = TableBrowser::table_fields($tablename);
TableBrowser::browse("",$tablename,$fields[0],"*",10,$mode,$actionstring);
}
function browsequery($query,$maintable,$fieldstoshow="*",$actionstring="",$perpage=10) {
ob_start();
TableBrowser::browse($query,$maintable,"id",$fieldstoshow,$perpage,"",$actionstring);
$s = ob_get_contents();
ob_end_clean();
return $s;
}
/**
* Get variable value from script get query string
*
* Parses $_SERVER['QUERY_STRING']
* @param string $variable Variable name
* @param string $default Default value, if variable wasn't found in query string
* @return string Variable value
*/
function querystring_get($variable,$default=0) {
$querystring = $_SERVER['QUERY_STRING'];
parse_str($querystring,$values);
if(!isset($values[$variable])) return $default;
else return $values[$variable];
}
/**
* Set variable in get query string
*
* If variable value already in query string it will be modified,
* else variable value will be added to end of query string, original script
* query string stays same, modified is returned
*
* @param string $variable Variable name
* @param string $value Variable value
* @return string Modified query string (with variable value added)
*/
function querystring_set($variable,$value) {
$querystring = $_SERVER['QUERY_STRING'];
$pos = strpos($querystring,"&$variable=");
if($pos===FALSE) {
if(strpos($querystring,"$variable=")===0)
$pos=0;
} else {
$pos++;
}
if($pos===FALSE) {
if($value=='') return $querystring;
if($querystring=="") return "$variable=$value";
else return $querystring."&$variable=$value";
}
$endpos = strpos($querystring,"&",$pos);
if($endpos===FALSE) $endpos=strlen($querystring);
if($value=='') {
return substr($querystring,0,max(0,$pos-1)).substr($querystring,$endpos);
} else {
return substr($querystring,0,$pos)."$variable=$value".substr($querystring,$endpos);
}
}
/**
* Show Data browse grid
*/
class TableBrowser {
var $option_showtitle = true;
var $option_selfstyles = true;
var $option_shownumbers = true;
function table_fields($tablename) {
$description = mysql_query("DESC $tablename");
$return = array();
while($line = mysql_fetch_array($description)) {
$fieldname = $line['0'];
$return[]=$fieldname;
}
return $return;
}
function table_column_widths($tablename) {
$description = mysql_query("DESC $tablename");
$return = array();
while($line = mysql_fetch_array($description)) {
$fieldname = $line['0'];
$fieldwidth = $line['1'];
preg_match("|\((.*)\)|",$fieldwidth,$match);
$fieldwidth = @$match[1];
$return[$fieldname]=$fieldwidth;
}
return $return;
}
function recorddelete($tablename,$keyfield,$id_value) {
$_SERVER['QUERY_STRING'] = querystring_set("deleteid","");
$sql = "DELETE FROM $tablename WHERE $keyfield=$id_value";
// echo $sql;
mysql_query($sql);
$backurl = $_SERVER['QUERY_STRING'];
Header("Location: ?$backurl");
}
function recordview($tablename,$keyfield,$keyfieldvalue) {
$sql = "SELECT * FROM $tablename";
if(!isset($this) || $this->option_selfstyles) {
echo "<style>
a { color: #000; }
a:visited { color: #666; }
a:hover { color: #fff; background-color:#000; }
</style>";
}
echo "<div style='font-family:verdana'>";
$title = "SHOWING ".str_replace("_"," ",$tablename);
echo "<title>$title</title>";
echo "<h1>$title</h1>";
$previd = @mysql_result(mysql_query("SELECT $keyfield FROM $tablename
WHERE $keyfield<'$keyfieldvalue' ORDER BY $keyfield DESC LIMIT 1"),0);
$nextid = @mysql_result(mysql_query("SELECT $keyfield FROM $tablename
WHERE $keyfield>'$keyfieldvalue' ORDER BY $keyfield LIMIT 1"),0);
if($previd || $nextid) {
echo "<br>";
$prevlink = querystring_set("viewid",$previd);
$nextlink = querystring_set("viewid",$nextid);
if($previd) echo "<a href=?$prevlink><<</a> ";
if($nextid) echo "<a href=?$nextlink>>></a>";
echo "<br>";
}
$backurl = querystring_set("viewid","");
if(strpos(strtolower($sql)," where ")!==FALSE) {
$sql .= " AND $keyfield='$keyfieldvalue'";
} else {
$sql .= " WHERE $keyfield='$keyfieldvalue'";
}
$result = mysql_query($sql);
$line = mysql_fetch_object($result);
$widths = TableBrowser::table_column_widths($tablename);
foreach($line as $colname => $value) {
$size = "";
echo "<br><strong>$colname</strong>";
if(strpos($colname,"serialize")!==FALSE ) {
echo "<pre>";
$v = @unserialize($value);
print_r($v);
echo "</pre>";
} else {
echo "<pre>$value</pre>";
}
}
echo "<br><a href=?$backurl>Back</a>";
echo "</div>";
}
function recordeditor($tablename,$keyfield,$fields,$id_value,$backurl=FALSE) {
$sql = "SELECT $fields FROM $tablename";
if(querystring_get("do",'')=='update') {
parse_str($_SERVER['QUERY_STRING']);
$setsql = "";
foreach($field as $colname=>$value) {
if($setsql!=="") $setsql .= ", ";
$setsql .= "$colname='$value'";
$_SERVER['QUERY_STRING'] = querystring_set("field%5B$colname%5D","");
}
$_SERVER['QUERY_STRING'] = querystring_set("editid","");
$_SERVER['QUERY_STRING'] = querystring_set("do","");
mysql_query("UPDATE $tablename SET $setsql WHERE $keyfield='$id_value'");
// $backurl = $_SERVER['QUERY_STRING'];
Header("Location: ?$backurl");
//echo "<br>record updated <br><a href=?$backurl>back</a>";
return;
}
if(!isset($this) || $this->option_selfstyles) {
echo "<style>
a { color: #000; }
a:visited { color: #666; }
a:hover { color: #fff; background-color:#000; }
</style>";
}
echo "<div style='font-family:verdana'>";
$title = "EDITING ".str_replace("_"," ",$tablename);
echo "<title>$title</title>";
echo "<h1>$title</h1>";
echo "<form>";
//drupal
if($_GET['q']) {
echo "<input type=hidden name='q' value=".$_GET['q'].">";
}
if(strpos(strtolower($sql)," where ")!==FALSE) {
$sql .= " AND $keyfield='$id_value'";
} else {
$sql .= " WHERE $keyfield='$id_value'";
}
$result = mysql_query($sql);
$line = mysql_fetch_object($result);
$widths = TableBrowser::table_column_widths($tablename);
$aliases = TableBrowser::fieldaliases($fields);
foreach($aliases as $realname => $viewname) {
if($realname==$keyfield) continue;
$size = "";
if(isset($widths[$realname])) {
$size = " size=".min($widths[$realname],120);
} else {
$size= " size=400";
}
$caption = str_replace("_"," ",$viewname);
if(substr($realname,strlen($realname)-5)=="_text") {
echo "<br>$caption<br><TEXTAREA ROWS=6 COLS=110 name=field[$realname]>".$line->$viewname."</TEXTAREA><br>";
} else {
echo "<br>$caption<br><input name=field[$realname] value='".$line->$viewname."'$size><br>";
}
}
echo "<br><input type=submit name=do value=update>";
echo "<input type=hidden name=editid value=$id_value>";
echo "<input type=hidden name=backurl value=$backurl>";
echo "</form>";
if($backurl!==FALSE) {
echo "<br><a href=?$backurl>Back</a>";
}
echo "</div>";
}
function fieldaliases($fields) {
$result = array();
//remove function calls brackets as there are commas inside of it like SUBSTR(field,40)
$fields = str_replace("(","<!--",$fields);
$fields = str_replace(")","-->",$fields);
$fields = strip_tags($fields);
$fieldnames = explode(",",$fields);
foreach($fieldnames as $fieldname) {
$fieldname_parse = explode(" as ",$fieldname);
$fieldname = trim($fieldname_parse[0]);
$caption = $fieldname;
if(isset($fieldname_parse[1])) {
$caption = trim($fieldname_parse[1]);
}
$result[$fieldname] = $caption;
}
return $result;
}
function recordnew($tablename,$keyfield,$fields) {
if(querystring_get("do",'')=='create') {
parse_str($_SERVER['QUERY_STRING']);
$fields = "";
$values = "";
foreach($field as $colname=>$value) {
$fields[] = $colname;
$values[] = "'".mysql_real_escape_string($value)."'";
}
$fields = implode(",",$fields);
$values = implode(",",$values);
$q = "INSERT INTO $tablename ($fields) VALUES ($values)";
$res = mysql_query($q);
if($res===FALSE) {
echo "Error while adding record.";
echo "<a href=?$backurl>Back</a>";
return;
}
Header("Location: ?$backurl");
//echo "<br>record updated <br><a href=?$backurl>back</a>";
return;
}
if(!isset($this) || $this->option_selfstyles) {
echo "<style>
a { color: #000; }
a:visited { color: #666; }
a:hover { color: #fff; background-color:#000; }
</style>";
}
echo "<div style='font-family:verdana'>";
$title = "NEW ".str_replace("_"," ",$tablename)." record";
echo "<title>$title</title>";
echo "<h1>$title</h1>";
echo "<form>";
//drupal
if($_GET['q']) {
echo "<input type=hidden name=q value=".$_GET['q'].">";
}
if($fields=='*' || $fields=='') {
$widths = TableBrowser::table_column_widths($tablename);
foreach($widths as $colname => $width) {
if($colname==$keyfield) {
continue;
}
if(isset($widths[$colname])) {
$size = " size=".min($widths[$colname],120);
}
echo "<br>$colname<br><input name=field[$colname] $size><br>";
}
} else {
$aliases = TableBrowser::fieldaliases($fields);
$fieldnames = explode(",",$fields);
$widths = TableBrowser::table_column_widths($tablename);
foreach($aliases as $fieldname=>$caption) {
if($fieldname==$keyfield) {
continue;
}
if(substr($fieldname,strlen($fieldname)-5)=="_text") {
echo "<br>$caption<br><TEXTAREA ROWS=6 COLS=110 name=field[$fieldname]></TEXTAREA><br>";
} else {
$size = "";
if(isset($widths[$fieldname])) {
$size = " size=".min($widths[$fieldname],120);
}
$caption = str_replace("_"," ",$caption);
echo "<br>$caption<br><input name=field[$fieldname] $size><br>";
}
}
}
echo "<br><input type=submit name=do value=create>";
$backurl = querystring_set("new","");
echo "<input type=hidden name=new value=1>";
echo "<input type=hidden name=backurl value=$backurl>";
echo "</form>";
if($backurl!==FALSE) {
echo "<br><a href=?$backurl>Back</a>";
}
echo "</div>";
}
/**
* Main method
*
* displays table data with actionstring and optional edit, delete, show and new operation links
*
* @param string $mainsql Main SQL text used to render data, if mainsql is "" simple select from table will be used
* @param string $tablename Main table name, all operations (delete,new) will be performed on it
* @param string $keyfield Key field name in table
* @param string $fields List of fields to show from main sql, * may be used, also sql 'as' keyword allowed for aliasing
* @param int $pagesize Page size for page display
* @param string $mode String containing E,V,D,N characters for Edit, View, Delete, New operations
* @param string $action_template_string Action string - ID will be replaced with keyfield value, [FIELDNAME] with FIELD values
* @return void Return nothing, generates output to browser (echo)
*/
function browse($mainsql,$tablename,$keyfield='id',$fields='*',$pagesize=10,$mode="EVDN",$action_template_string="") {
if(querystring_get("page","")!=="") {
$pagesize = querystring_get("page","");
}
if(strpos($fields,"*")!==FALSE) {
$fields_list = TableBrowser::table_fields($tablename);
$fields_list = implode(",",$fields_list);
$fields = str_replace("*",$fields_list,$fields);
}
$mode = strtoupper($mode);
if(!$mainsql) {
$sql = "SELECT $fields,$keyfield FROM $tablename ORDER BY $keyfield";
} else {
$sql = $mainsql;
}
if(strpos($mode,"E")!==FALSE && querystring_get("editid","")!="") {
$editid = querystring_get("editid");
TableBrowser::recordeditor($tablename,$keyfield,$fields,$editid,querystring_set("editid",""));
return;
}
if(strpos($mode,"V")!==FALSE && querystring_get("viewid","")!="") {
$viewid = querystring_get("viewid");
TableBrowser::recordview($tablename,$keyfield,$viewid);
return;
}
if(strpos($mode,"D")!==FALSE && querystring_get("deleteid","")!="") {
$deleteid = querystring_get("deleteid");
TableBrowser::recorddelete($tablename,$keyfield,$deleteid);
return;
}
if(strpos($mode,"N")!==FALSE && querystring_get("new","")!="") {
TableBrowser::recordnew($tablename,$keyfield,$fields);
return;
}
echo "<div style='font-family:verdana'>";
$page = 1;
$sql_with_limit = strpos($sql," LIMIT ");
//show title if call was :: or object option_showtitle==true
if(!isset($this) || $this->option_showtitle) {
$title = "LISTING ".str_replace("_"," ",$tablename);
echo "<title>$title</title>";
echo "<h1>$title</h1>";
}
$sql_full = $sql;
if($pagesize)
if($sql_with_limit===FALSE) {
$page = querystring_get("p",'1');
if($page==1) $sql_limit = " LIMIT $pagesize";
else
$sql_limit = " LIMIT ".($page-1)*$pagesize.",".$pagesize;
$sql_full = $sql.$sql_limit;
}
$result = mysql_query($sql_full);
$count_sql = "SELECT count(1) ".substr($sql,strpos(strtoupper($sql)," FROM "),strlen($sql));
$total_count = 0;
if($pagesize)
$total_count = mysql_result(mysql_query($count_sql),0);
if($sql_with_limit===FALSE)
if($pagesize)
{
if($page==1) echo "Records: 1-".min($pagesize,$total_count);
else echo "Records: ".(($page-1)*$pagesize+1)." - ".min(($page-1)*$pagesize+mysql_num_rows($result),(($page+1)*$pagesize));
echo " of $total_count";
}
if(!isset($this) || $this->option_selfstyles) {
echo "<style>
a { color: #000; }
a:visited { color: #666; }
a:hover { color: #fff; background-color:#000; }
</style>";
}
$firstline = true;
$record = ($page-1)*$pagesize;
$aliases = TableBrowser::fieldaliases($fields);
echo "<table style='font-family:verdana' border=0 cellpadding=1 cellspacing=1>\n";
///SHOWING DATA
if(mysql_num_rows($result)==0) {
echo "<tr><td>No data to show";
}
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
$fieldstoshow = $aliases;
if($fields=='*') {
$fieldstoshow = $line;
}
//TABLE HEADERS
if($firstline) {
$widths = TableBrowser::table_column_widths($tablename);
echo "<tr style='background:#000000;color:#ffffff;font-weight:bold;'>\n";
if(!isset($this) || $this->option_shownumbers) {
echo "<th width=30>#";
}
foreach($fieldstoshow as $field=>$value) {
if($fields=='*') {
$colname = $field;
$col = $value;
} else {
$colname = str_replace("_"," ",$value);
$col = $line[$field];
}
$width = "";
if(isset($widths[$colname])) {
$width = " width=".min((8*$widths[$colname]),200);
}
$viewname = str_replace("_"," ",$colname);
echo "<th $width>$colname";
}
$firstline = false;
if(strpos($mode,"V")!==FALSE || strpos($mode,"E")!==FALSE || strpos($mode,"D")!==FALSE || $action_template_string) {
echo "<td>";
}
}
echo "\t<tr>\n";
$record++;
if(!isset($this) || $this->option_shownumbers) {
echo "<td valign=top>$record";
}
//showing data
foreach($fieldstoshow as $field => $value) {
if($fields=='*') {
$colname = $field;
$col = $value;
} else {
$colname = $field;
$col = $line[$field];
}
if($colname=="day") {
$col = strftime("%d/%m/%Y %a %H:%M:%S",$col);
}
if(strpos($colname,'_time') || $colname=="time" || $colname=="creationtime" || $colname=="timestamp") {
if($col==0)
$col = '-';
else
$col = strftime("%d/%m/%Y %H:%M:%S",$col);
}
if($colname=='period')
$col = TimePeriodShort($col);
if(strlen($action_template_string)!=0 && $colname=='id') {
continue;
}
if(strlen($col)==0) $col = "-";
if(strpos($field,"serialize")!==FALSE) {
$col = @unserialize($col);
$col = "<pre>".print_r($col,true)."</pre>";
}
echo "<td valign=top>$col";
}
if(strpos($mode,"E")!==FALSE || strpos($mode,"V")!==FALSE || strpos($mode,"D")!==FALSE || $action_template_string) {
echo "<td>";
}
//EDIT TABLE LINKS
if(strpos($mode,"E")!==FALSE || strpos($mode,"V")!==FALSE || strpos($mode,"D")!==FALSE) {
if(!isset($line[$keyfield])) {
echo("</table>EXCEPTION: Keyfield [$keyfield] is not defined in mainsql (browse function - first parameter).");
return;
}
$editurl = querystring_set("editid",$line[$keyfield]);
$deleteurl = querystring_set("deleteid",$line[$keyfield]);
$viewurl = querystring_set("viewid",$line[$keyfield]);
if(strpos($mode,"E")!==FALSE)
echo "<a href=?$editurl>Modify</a>";
echo " ";
if(strpos($mode,"V")!==FALSE)
echo "<a href=?$viewurl>Show</a>";
echo " ";
if(strpos($mode,"D")!==FALSE)
echo "<a onclick=\"return confirm('Are you sure?');\" href=?$deleteurl>Delete</a>";
}
//ACTION STRING
if(strlen($action_template_string)!=0) {
$id = $line[$keyfield];
$actionstring = str_replace("ID", urlencode($id), $action_template_string);
foreach($line as $colname => $value) {
$actionstring = str_replace("[$colname]",$value,$actionstring);
}
echo " $actionstring";
}
}
echo "</table>\n";
// PAGES
if($pagesize)
if($sql_with_limit===FALSE && $total_count>$pagesize) {
if(!$page) $page = 1;
echo "<table style='font-family:verdana'>";
echo "<tr>";
echo "<td>";
$query_previous = querystring_set("p",$page-1);
$query_next = querystring_set("p",$page+1);
$query_first = querystring_set("p",1);
$lastpage = floor(($total_count-1)/$pagesize)+1;
$query_last = querystring_set("p",$lastpage);
if($page>1) {
echo "<a href=?$query_previous><<</a> ";
echo " <a href=?$query_first>first</a> ";
} else {
//echo "Previous";
}
$range = 10;
$range = querystring_get("range",$range);
for($i=max(1,$page-$range);$i<min($page+$range,($total_count/$pagesize)+1);$i++) {
$query_page = querystring_set("p",$i);
if($page==$i) {
echo " $i";
} else {
echo " <a href=?$query_page>$i</a>";
}
}
if(mysql_num_rows($result)==$pagesize && $total_count!=(($page-1)*$pagesize+mysql_num_rows($result))) {
echo " <a href=?$query_last>last</a>";
echo " <a href=?$query_next>>></a>";
}
else {
//echo "Next";
}
echo "</table>";
}
$newurl = querystring_set("new",1);
if(strpos($mode,"N")!==FALSE)
echo "<table><tr><td><a href=?$newurl>New ".str_replace("_"," ",$tablename)." record</a></table>";
echo "</div>";
}
}
$log = "";
function log_message($s) {
global $log;
$log .= $s;
}
function flash($s) {
log_message($s);
}
function database_table_fields($tablename) {
$description = mysql_query("DESC $tablename");
$return = array();
while($line = mysql_fetch_array($description)) {
$fieldname = $line['0'];
$return[]=$fieldname;
}
return $return;
}
function db_object_set($tablename,$object) {
database_object_set($tablename,$object);
}
//works only with mysql
function database_object_set($tablename,$object) {
$object_original = database_object_get($tablename,$object->id);
$fields = database_table_fields($tablename);
$fields_sql = "";
foreach($fields as $field) {
if(strtolower($field)=="id") continue;
if($object_original->$field == $object->$field) continue;
if($fields_sql) {
$fields_sql .= ", ";
}
$fields_sql .= " $field='".$object->$field."' ";
}
if($fields_sql) {
$sql = "UPDATE $tablename SET $fields_sql WHERE id=$object->id";
db_query($sql);
}
}
function database_fixslashes($text) {
$text = str_replace("\\'","'",$text);
$text = str_replace("\\\"","\"",$text);
$text = str_replace("\\\\","\\",$text);
return $text;
}
function db_object_get($tablename,$idvalue,$props=false,$ignoretables_list="") {
global $database_object_get_tables;
if($ignoretables_list) {
$database_object_get_tables = explode(",",$ignoretables_list);
foreach($database_object_get_tables as &$table) $table = trim($table);
}
$res = database_object_get($tablename,$idvalue,$props);
$database_object_get_tables = array();
return $res;
}
function connected_table($tablename,$subtable) {
global $tables;
$res = $subtable;
if(!isset($tables[$subtable])) {
$res = str_prefix($tablename).$subtable;
}
if(!isset($tables[$res])) return "";
else
return $res;
}
function str_no_prefix($s,$prefix) {
return substr($s,strlen($prefix),strlen($s));
}
function db_fetch_objects($rr) {
$res = array();
while($r = db_fetch_object($rr)) {
$res[] = $r;
}
return $res;
}
function db_fetch_key_value_list($rr,$value_field_name) {
$res = array();
while($r = db_fetch_object($rr)) {
$res[$r->id] = $r->$value_field_name;
}
return $res;
}
function table_fields($tablename) {
global $tables;
$fields = array();
if(isset($tables[$tablename]['fields']))
$fields = array_merge($fields,$tables[$tablename]['fields']);
if(isset($tables[$tablename]['fields_hidden']))
$fields = array_merge($fields,$tables[$tablename]['fields_hidden']);
return $fields;
}
$fields_stack = array();
$database_object_get_tables = array();
function database_object_get($tablename,$idvalue,$props=false) {
$o = db_fetch_object(db_query("SELECT * FROM $tablename WHERE id=%d",$idvalue));
if($o)
if($props) {
///get objects for _id fields
$fields = table_fields($tablename);
foreach($o as $field=>$field_value) {
global $fields_stack;
if(array_search($field,$fields_stack)!==FALSE) continue;
if(str_end($field,"_id")) {
$subtable = str_start($field,"_id")."s";
$subtable = connected_table($tablename,$subtable);
if($subtable) {
$member = str_start($field,"_id");
$prop = database_object_get($subtable,$field_value);
if($prop)
foreach($prop as $propname=>$propvalue) {
$o_prop_name = $propname;
$o->$o_prop_name = $propvalue;
}
}
}
}
///get linked tables
global $tables;
foreach($tables as $table=>$tableobject) {
$this_object_link = str_start($tablename,"s")."_id";
$this_object_prefix = str_prefix($tablename);
$link_table_prefix = str_prefix($table);
$table_no_prefix = $table;
if($this_object_prefix == $link_table_prefix) {
$this_object_link = str_no_prefix($this_object_link,$this_object_prefix);
$table_no_prefix = str_no_prefix($table_no_prefix,$this_object_prefix);
}
$ignore_the_table = true;
global $database_object_get_tables;
if(count($database_object_get_tables)==0) {
$ignore_the_table = false;
} else {
foreach($database_object_get_tables as $get_table) {
if($get_table==$table) {
$ignore_the_table = 0;
break;
}
}
}
if($ignore_the_table===true) {
continue;
}
$fields = table_fields($table);
foreach($fields as $fieldname) {
if($fieldname == $this_object_link) {
$rr = db_query("SELECT id FROM $table WHERE $fieldname = $idvalue");
global $fields_stack;
$fields_stack[] = $fieldname;
$propname = $table;
if($this_object_prefix == $link_table_prefix) {
$propname = str_no_prefix($table,$this_object_prefix);
}
$subobjects = array();
while($r = db_fetch_object($rr)) {
$subobject = database_object_get($table,$r->id,true);
$subobjects[] = $subobject;
}
array_pop($fields_stack);
$o->$propname = $subobjects;
}
}
}
}
return $o;
}
//convert sqlite sql dialect to mysql
function db_sqlite2mysql_sql($query) {
$query = trim($query);
if(strtolower($query)=="vacuum") return "";
if(strtolower(substr($query,0,6))=='insert') {
$low = strtolower($query);
if(strpos($low,"values (null,") && strpos($low,"(id,")) {
$query = str_ireplace("values (null, ","values (",$query);
$query = str_ireplace("(id, ","(",$query);
}
if(strpos($low,"values(null,") && strpos($low,"(id,")) {
$query = str_ireplace("values(null,","values(",$query);
$query = str_ireplace("(id,","(",$query);
}
if(strpos($low,"values(null)") && strpos($low,"(id)")) {
$query = str_ireplace("values(null)","",$query);
$query = str_ireplace("(id)","",$query);
}
}
return trim($query);
}
$db_query_error_function = "db_die";
function db_die($s) {
if(str_end(form_post("q"),'/vars')) {
echo "vars created<br>";
} else
die($s);
}
function db_query($query) {
@prf_start("db_query");
@prf_start(substr($query,0,20));