-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtxtSQL.core.php
More file actions
1976 lines (1789 loc) · 60.5 KB
/
txtSQL.core.php
File metadata and controls
1976 lines (1789 loc) · 60.5 KB
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
/************************************************************************
* txtSQL ver. 2.2 Final *
*************************************************************************
* A php class of functions which simulates and acts almost like a mySQL *
* service. *
*-----------------------------------------------------------------------*
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307 *
* USA. *
*-----------------------------------------------------------------------*
* NOTE- Tab size in this file: 8 spaces/tab *
*-----------------------------------------------------------------------*
* ©2003 Faraz Ali, ChibiGuy Production [http://txtsql.sourceforge.net] *
* File: txtsql.core.php *
************************************************************************/
/**
* The core file of the txtSQL package, this is the meat of the script
* @package txtSQL::core
* @author Faraz Ali <Faraz87@comcast.net>
* @version 2.2 Final
* @access public
*/
class txtSQLCore extends txtSQL
{
/**
* To extract data from a database, given that the row fits the given credentials
* @param mixed arg The arguments that are passed to the txtSQL as an array.
* @return mixed selected An array containing the rows that matched the where clause
* @access private
*/
function select ($arg)
{
/* If the user specified a different database, we must
* then automatically select it for them */
if ( !empty($arg['db']) )
{
if ( !$this->selectdb($arg['db']) )
{
return FALSE;
}
}
/* If we have no database selected, we have no table specified
* stop execution of script and issue an error */
if ( empty($this->_SELECTEDDB) )
{
$this->_error(E_USER_NOTICE, 'No database selected');
return FALSE;
}
elseif ( empty($arg['table']) )
{
$this->_error(E_USER_NOTICE, 'No table specified');
return FALSE;
}
/* If no selection is specified, then we will select
* all of the listed column */
elseif ( empty($arg['select']) )
{
$arg['select'] = array('*');
}
/* Read in the records and column definitions, and if an error occurs
* then we issue a warning saying table doesn't exist */
$filename = "$this->_LIBPATH/$this->_SELECTEDDB/{$arg['table']}";
if ( ($rows = $this->_readFile($filename.'.MYD')) === FALSE || ($cols = $this->_readFile($filename.'.FRM')) === FALSE )
{
$this->_error(E_USER_NOTICE, 'Table "'.$arg['table'].'" doesn\'t exist');
return FALSE;
}
if ( empty($rows) )
{
return array();
}
/* Check to see if we have a where clause to work with */
$matches = 'TRUE';
if ( isset($arg['where']) )
{
/* Create the rule to match records, this goes inside the $rowmatches()
* function statement and tells us whether the current row matches the
* given criteria or not */
if ( ($matches = $this->_buildIf($arg['where'], $cols)) === FALSE )
{
return FALSE;
}
}
/* Parse the limit clause, looking for any complications, like finish
* value larger than the start value, non-numeric values, if no
* limit is specified, or is it is not an array. */
if ( empty($arg['limit']) || (!empty($arg['limit']) && !is_array($arg['limit'])) )
{
$arg['limit']['0'] = 0;
$arg['limit']['1'] = count($rows)-1;
}
elseif ( isset($arg['limit'][0]) && !isset($arg['limit'][1]) )
{
$arg['limit'][1] = $arg['limit'][0];
$arg['limit'][0] = 0;
}
elseif ( !isset($arg['limit'][0]) || !isset($arg['limit'][1]) || $arg['limit'][0] > $arg['limit'][1] )
{
$arg['limit']['0'] = 0;
$arg['limit']['1'] = count($rows)-1;
}
$arg['limit'][0] = ( int ) $arg['limit'][0];
$arg['limit'][1] = ( int ) $arg['limit'][1];
/* If we have a wildcard as a select, then we need to
* create the selection list ourselves */
if ( $arg['select'][0] == '*' )
{
$col = $cols;
unset($col['primary']);
$arg['select'] = array_keys($col);
}
/* Create the selection index, this speeds things up tremendously
* because it saves calls to _getColPos() */
foreach ( $arg['select'] as $key => $value )
{
if ( strtolower($value) == 'primary' )
{
if ( empty($cols['primary']) )
{
$this->_error(E_USER_NOTICE, 'No primary key assigned to table '.$arg['table']);
return FALSE;
}
$value = $cols['primary'];
}
if ( ($colPos = $this->_getColPos($value, $cols)) === FALSE )
{
$this->_error(E_USER_NOTICE, 'Column \''.$value.'\' doesn\'t exist');
return FALSE;
}
$temp[$value] = $colPos;
}
$arg['select'] = $temp;
/* Initialize Some Variables */
$found = -1;
$added = -1;
$selected = array();
/* Go through each record, if the row matches and we are in our limits
* then select the row with the proper type (string, boolean, or integer) */
$function = ' foreach ( $rows as $key => $value )
{
if ( '.$matches.' )
{
$found++;
if ( $found >= $arg[\'limit\'][0] && $found <= $arg[\'limit\'][1] )
{
$added++;';
foreach ( $arg['select'] as $key => $value )
{
$function .= "\$selected[\$added]['$key'] = \$value[$value];";
}
$function .= ' if ( $found >= $arg[\'limit\'][1] )
{
break;
}
}
}
} ';
eval($function);
/* Sort the results by a key, this is a very expensive
* operation and can take quite some time which is why
* it is not reccomended for large amounts of data */
if ( !empty($arg['orderby']) && !empty($selected) && count($selected) > 0 )
{
/* We need a valid array to sort the results correctly */
if ( !is_array($arg['orderby']) || count($arg['orderby']) != 2 )
{
$this->_error(E_USER_NOTICE, 'Invalid Order By Clause; Must be array, with two values. array(string "column name", [ASC|DESC])');
return FALSE;
}
/* We cannot sort the results by a non-existing key */
if ( !array_key_exists($arg['orderby'][0], $selected[0]) )
{
$this->_error(E_USER_NOTICE, 'Cannot sort results by column \''.$arg['orderby'][0].'\'; Column not in result set');
return FALSE;
}
/* We can only sort results by ascending order or
* descending order */
if ( strtolower($arg['orderby'][1]) != 'asc' && strtolower($arg['orderby'][1]) != 'desc' )
{
$this->_error(E_USER_NOTICE, 'Results can only be sorted \'asc\' (ascending) or \'desc\' (descending)');
return FALSE;
}
$selected = $this->_qsort($selected, $arg['orderby'][0], $arg['orderby'][1]);
}
/* Apply the DISTINCT feature to the result set */
if ( !empty($arg['distinct']) )
{
if ( $this->_getColPos($arg['distinct'], $cols) === FALSE )
{
$this->_error(E_USER_NOTICE, 'Column \''.$arg['distinct'].'\' doesn\'t exist');
return FALSE;
}
$selected = $this->unique_multi_array($selected, $arg['distinct']);
}
/* Save changes in the cache */
$this->_CACHE[$filename.'.MYD'] = $rows;
$this->_CACHE[$filename.'.FRM'] = $cols;
/* Return the selected records */
return $selected;
}
/**
* To insert a row of data into a table.
* @param mixed arg The arguments that are passed to the txtSQL as an array.
* @return void
* @access private
*/
function insert ($arg)
{
/* If the user specifies a different database, then
* automatically select it for them */
if ( !empty($arg['db']) )
{
if ( !$this->selectdb($arg['db']) )
{
return FALSE;
}
}
/* If we have no database selected, or no table to work with
* then stop script execution */
if ( empty($this->_SELECTEDDB) )
{
$this->_error(E_USER_NOTICE, 'No database selected');
return FALSE;
}
elseif ( empty($arg['table']) )
{
$this->_error(E_USER_NOTICE, 'No table specified');
return FALSE;
}
/* Make sure the database isn't locked */
if ( $this->isLocked($this->_SELECTEDDB) )
{
$this->_error(E_USER_NOTICE, 'Database '.$this->_SELECTEDDB.' is locked');
return FALSE;
}
/* Check to see if the tables exist or not, if not then we cannot
* continue, so we issue an error message */
$filename = "$this->_LIBPATH/$this->_SELECTEDDB/{$arg['table']}";
if ( ($rows = $this->_readFile($filename.'.MYD')) === FALSE || ($cols = $this->_readFile($filename.'.FRM')) === FALSE )
{
$this->_error(E_USER_NOTICE, 'Table '.$arg['table'].' doesn\'t exist');
return FALSE;
}
/* Create the model of the row */
$model = array();
foreach ( $cols as $key => $value )
{
if ( $key == 'primary' ) continue;
if ( $value['auto_increment'] == 1 )
{
$model[] = ($cols[$key]['autocount']++)+1;
}
elseif ( $value['type'] == 'date' )
{
$arg['values'][$key] = '';
}
else
{
$model[] = $value['default'];
}
}
/* We first create the selection indexes inside the foreach loop,
* inside the same one, we check that max values have not been
* exceeded, the table isn't permanent, and auto increment features */
$max = count($rows);
foreach ( $arg['values'] as $key => $value )
{
unset($arg['values'][$key]);
/* If the user is referring to the primary column, then
* we substitute it with the actual primary column. We
* also check to see if the column exists or not */
if ( strtolower($key) == 'primary' )
{
if ( empty($cols['primary']) )
{
$this->_error(E_USER_NOTICE, 'No primary key assigned to table '.$arg['table']);
return FALSE;
}
$key = $cols['primary'];
}
if ( ($colPos = $this->_getColPos($key, $cols)) === FALSE )
{
$this->_error(E_USER_NOTICE, 'Column \''.$key.'\' doesn\'t exist');
return FALSE;
}
$value = array($colPos, $value);
/* Make sure that the max value for this column has not
* yet been exceeded */
if ( $cols[$key]['type'] == 'int' && $cols[$key]['max'] > 0 && $value[1] > $cols[$key]['max'] )
{
$this->_error(E_USER_NOTICE, 'Cannot exceed maximum value for column '.$key);
return FALSE;
}
elseif ( $cols[$key]['max'] > 0 && strlen($value[1]) > $cols[$key]['max'] )
{
$this->_error(E_USER_NOTICE, 'Cannot exceed maximum value for column '.$key);
return FALSE;
}
/* If the value is empty, and there is a default value
* set for this column, then we substitute the value
* with the default */
if ( empty($value[1]) && !empty($cols[$key]['default']) )
{
$value[1] = $cols[$key]['default'];
}
/* If this is an auto increment column, then we will
* will use the already incremented column value */
if ( $cols[$key]['auto_increment'] == 1 )
{
$value[1] = $model[$colPos];
}
/* Insert the new row of data into the rows of information
* with the right data type */
switch ( strtolower($cols[$key]['type']) )
{
case 'enum': if ( empty($cols[$key]['enum_val']) )
{
$cols[$key]['enum_val'] = serialize(array(''));
}
$enum_val = unserialize($cols[$key]['enum_val']);
foreach ( $enum_val as $key => $value1 )
{
if ( strtolower($value[1]) == strtolower($value1) )
{
break;
}
if ( $key == ( count($enum_val) - 1 ) )
{
$value[1] = $enum_val[$key];
break;
}
}
case 'text':
case 'string': $model[$value[0]] = (string) $value[1]; break;
case 'int': $model[$value[0]] = (integer) $value[1]; break;
case 'bool': $model[$value[0]] = (boolean) $value[1]; break;
case 'date': $model[$value[0]] = time(); break;
}
}
$rows[] = $model;
/* Save the new information in their proper files */
$fp = @fopen($filename.".MYD", 'w') or $this->_error(E_USER_ERROR, 'Error opening table '.$arg['table']);
@flock($fp, LOCK_EX);
@fwrite($fp, serialize($rows)) or $this->_error(E_USER_ERROR, 'Error writing to table '.$arg['table']);
@flock($fp, LOCK_UN);
@fclose($fp);
$fp = @fopen($filename.".FRM", 'w') or $this->_error(E_USER_ERROR, 'Error opening table '.$arg['table']);
@flock($fp, LOCK_EX);
@fwrite($fp, serialize($cols)) or $this->_error(E_USER_ERROR, 'Error writing to table '.$arg['table']);
@flock($fp, LOCK_UN);
@fclose($fp);
/* Save files to cache */
$this->_CACHE[$filename.'.MYD'] = $rows;
$this->_CACHE[$filename.'.FRM'] = $cols;
/* Return the new number of records in the database */
return TRUE;
}
/**
* Removes (a) row(s) that fit(s) the given credentials from a table. If none
* are specified, it will empty out the table.
* @param mixed arg The arguments that are passed to the txtSQL as an array.
* @return int deleted The number of rows deleted
* @access private
*/
function delete ($arg)
{
/* If the user specifies a different database, then
* automatically select it for them */
if ( !empty($arg['db']) )
{
if ( !$this->selectdb($arg['db']) )
{
return FALSE;
}
}
/* If no database is selected, or we have no table to
* work with, then stop execution of script */
if ( empty($this->_SELECTEDDB) )
{
$this->_error(E_USER_NOTICE, 'No database selected');
return FALSE;
}
elseif ( empty($arg['table']) )
{
$this->_error(E_USER_NOTICE, 'No table specified');
return FALSE;
}
/* Make sure the database isn't locked */
if ( $this->isLocked($this->_SELECTEDDB) )
{
$this->_error(E_USER_NOTICE, 'Database '.$this->_SELECTEDDB.' is locked');
return FALSE;
}
/* Check to see if the tables exist or not, if not then we cannot
* continue, so we issue an error message */
$filename = "$this->_LIBPATH/$this->_SELECTEDDB/{$arg['table']}";
if ( ($rows = $this->_readFile($filename.'.MYD')) === FALSE || ($cols = $this->_readFile($filename.'.FRM')) === FALSE )
{
$this->_error(E_USER_NOTICE, 'Table '.$arg['table'].' doesn\'t exist');
return FALSE;
}
/* Check to see if we have a where clause to work with */
if ( isset($arg['where']) )
{
/* Create the rule to match records, this goes inside the eval()
* statement and tells us whether the current row matches or not */
if ( ($matches = $this->_buildIf($arg['where'], $cols)) === FALSE )
{
return FALSE;
}
}
else
{
$rows = array();
}
/* Parse the limit clause looking for any complications
* like it not being an array, or if we don't have a numeric
* value */
if ( !isset($arg['limit']) || empty($arg['limit']) || !is_numeric($arg['limit'][0]) )
{
$arg['limit']['0'] = count($rows);
}
/* Initialize some variables */
$found = 0;
$deleted = 0;
/* Go through each record, if the row matches and we are in our limits
* then delete the row */
$function = '
foreach ( $rows as $key => $value )
{
if ( '.( isset($matches) ? $matches : 'TRUE' ).' )
{
$found++;
if ( $found <= $arg[\'limit\'][0] )
{
$deleted++;
unset($rows[$key]);
if ( $found >= $arg[\'limit\'][0] )
{
break;
}
continue;
}
break;
}
}';
eval($function);
/* Save the new record information */
$fp = @fopen($filename.".MYD", 'w') or $this->_error(E_USER_ERROR, 'Error opening table '.$arg['table']);
@flock($fp, LOCK_EX);
@fwrite($fp, serialize($rows)) or $this->_error(E_USER_ERROR, 'Error writing to table '.$arg['table']);
@flock($fp, LOCK_UN);
@fclose($fp);
/* Save files to cache */
$this->_CACHE[$filename.'.MYD'] = $rows;
$this->_CACHE[$filename.'.FRM'] = $cols;
/* Return the number of deleted rows */
return $deleted;
}
/**
* Updates a row that matches the given credentials with
* the new data
* @param mixed arg The arguments that are passed to the txtSQL as an array.
* @return int updated The number of rows that were updated
* @access private
*/
function update ($arg)
{
/* If the user specifies a different database
* then we must automatically select it for them. */
if ( !empty($arg['db']) )
{
if ( !$this->selectdb($arg['db']) )
{
return FALSE;
}
}
/* If there is no database selected, or we have no table
* selected, then stop execution of script */
if ( empty($this->_SELECTEDDB) )
{
$this->_error(E_USER_NOTICE, 'No database selected');
return FALSE;
}
elseif ( empty($arg['table']) )
{
$this->_error(E_USER_NOTICE, 'No table specified');
return FALSE;
}
/* Make sure the database isn't locked */
if ( $this->isLocked($this->_SELECTEDDB) )
{
$this->_error(E_USER_NOTICE, 'Database '.$this->_SELECTEDDB.' is locked');
return FALSE;
}
/* Check to see if the tables exist or not, if not then we cannot
* continue, so we issue an error message */
$filename = "$this->_LIBPATH/$this->_SELECTEDDB/{$arg['table']}";
if ( ($rows = $this->_readFile($filename.'.MYD')) === FALSE || ($cols = $this->_readFile($filename.'.FRM')) === FALSE )
{
$this->_error(E_USER_NOTICE, 'Table '.$arg['table'].' doesn\'t exist');
return FALSE;
}
/* Check to see if we have a where clause to work with */
if ( !isset($arg['where']) )
{
$this->_error(E_USER_NOTICE, 'Must specify a where clause');
return FALSE;
}
/* Create the rule to match records, this goes inside the eval()
* statement and tells us whether the current row matches or not */
elseif ( ($matches = $this->_buildIf($arg['where'], $cols)) === FALSE )
{
return FALSE;
}
/* If we have no values to substitute, issue a warning and return */
elseif ( !isset($arg['values']) || empty($arg['values']) )
{
$this->_error(E_USER_NOTICE, 'Must specify values to update');
return FALSE;
}
/* Parse the limit looking for any complications like
* non-numeric values, and not being an array */
if ( empty($arg['limit']) )
{
$arg['limit']['0'] = count($rows);
}
elseif ( !is_array($arg['limit']) || !is_numeric($arg['limit'][0]) || $arg['limit'][0] <= 0 )
{
$arg['limit']['0'] = count($rows);
}
/* Create the selection index, this little thing saves calls
* to _getColPos() about 10000 times, and speeds things up */
foreach ( $arg['values'] as $key => $value )
{
/* If the user specifies the primary column,
* substitute the actual column name for it. */
if ( strtolower($key) == 'primary' )
{
if ( empty($cols['primary']) )
{
$this->_error(E_USER_NOTICE, 'No primary key assigned to table '.$arg['table']);
return FALSE;
}
$key = $cols['primary'];
}
/* If the column doesn't exist */
if ( ($colPos = $this->_getColPos($key, $cols)) === FALSE )
{
$this->_error(E_USER_NOTICE, 'Column \''.$key.'\' doesn\'t exist');
return FALSE;
}
/* If the column is permanent */
if ( $cols[$key]['permanent'] == 1 )
{
$this->_error(E_USER_NOTICE, 'Column '.$key.' is set to permanent');
unset($arg['values'][$key]);
continue;
}
/* does it exceed max val? */
if ( $cols[$key]['type'] == 'int' && $cols[$key]['max'] > 0 && $value > $cols[$key]['max'] )
{
$this->_error(E_USER_NOTICE, 'Cannot exceed maximum value for column '.$key);
return FALSE;
}
elseif ( $cols[$key]['max'] > 0 && strlen($value) > $cols[$key]['max'] )
{
$this->_error(E_USER_NOTICE, 'Cannot exceed maximum value for column '.$key);
return FALSE;
}
$arg['values'][$key] = array($colPos, $value);
unset($key, $value);
}
/* Initialize some variables */
$found = 0;
$updated = 0;
/* Start going through each row of information looking for a match,
* and if it matches then updates the row with the proper information */
$function = ' foreach ( $rows as $key => $value )
{
if ( '.$matches.' )
{
$found++;
if ( $found <= $arg[\'limit\'][0] )
{
$updated++;';
foreach ( $arg['values'] as $key1 => $value1 )
{
switch ( strtolower($cols[$key1]['type']) )
{
case 'enum': if ( empty($cols[$key1]['enum_val']) )
{
$cols[$key1]['enum_val'] = serialize(array(''));
}
$enum_val = unserialize($cols[$key1]['enum_val']);
foreach ( $enum_val as $key2 => $value2 )
{
if ( strtolower($arg['values'][$key1][1]) == strtolower($value2) )
{
break;
}
if ( $key2 == ( count($enum_val) - 1 ) )
{
$arg['values'][$key1][1] = $enum_val[$key2];
break;
}
}
case 'text':
case 'string': $type = "string"; break;
case 'int': $type = "integer"; break;
case 'bool': $type = "boolean"; break;
default: $type = "string";
}
$function .= "\$rows[\$key][$value1[0]] = ( $type ) \$arg['values']['$key1'][1];";
}
$function .= ' continue;
}
break;
}
}
';
eval($function);
/* Save the new row information */
$fp = @fopen($filename.".FRM", 'w') or $this->_error(E_USER_ERROR, 'Error opening table '.$arg['table']);
@flock($fp, LOCK_EX);
@fwrite($fp, serialize($cols)) or $this->_error(E_USER_ERROR, 'Error writing to table '.$arg['table']);
@flock($fp, LOCK_UN);
@fclose($fp);
$fp = @fopen($filename.".MYD", 'w') or $this->_error(E_USER_ERROR, 'Error opening table '.$arg['table']);
@flock($fp, LOCK_EX);
@fwrite($fp, serialize($rows)) or $this->_error(E_USER_ERROR, 'Error writing to table '.$arg['table']);
@flock($fp, LOCK_UN);
@fclose($fp);
/* Save files to cache */
$this->_CACHE[$filename.'.MYD'] = $rows;
$this->_CACHE[$filename.'.FRM'] = $cols;
/* Return the number of rows that were updated */
return $updated;
}
/**
* Returns an array with a list of tables inside of a database
* @param mixed arg The arguments that are passed to the txtSQL as an array.
* @return mixed tables An array containing the tables inside of a db
* @access private
*/
function showtables ($arg = NULL)
{
/* Are we showing tables inside of another database? */
if ( !empty($arg['db']) )
{
/* Does it exist? */
if ( !$this->selectdb($arg['db']) )
{
return FALSE;
}
}
/* Is a database selected? */
if ( empty($this->_SELECTEDDB) )
{
$this->_error(E_USER_NOTICE, 'No database selected');
return FALSE;
}
/* Can we open the directory up? */
if ( ($fp = @opendir("$this->_LIBPATH/$this->_SELECTEDDB")) === FALSE )
{
$this->_error(E_USER_ERROR, 'Could not open directory, '.$this->_LIBPATH.'/'.$this->_SELECTEDDB.', for reading');
}
/* Make sure that it's a directory, and not a '..' or '.' */
$table = array();
while ( ($file = @readdir($fp)) !== FALSE )
{
if ( $file != "." && $file != ".." && $file != 'txtsql.MYI')
{
/* If it's a valid txtsql table */
$extension = substr($file, strrpos($file, '.')+1);
if ( ($extension == 'MYD' || $extension == 'FRM') && is_file("$this->_LIBPATH/$this->_SELECTEDDB/$file") )
{
$table[] = substr($file, 0, strrpos($file, '.'));
}
}
}
@closedir($fp);
/* Get only the tables that are valid */
$tables = array();
foreach ( $table as $key => $value )
{
if ( isset($temp[$value]) )
{
$tables[] = $value;
}
else
{
$temp[$value] = TRUE;
}
}
/* Return only the names of the tables */
return !empty($tables) ? $tables : array();
}
/**
* Creates a table inside of a database, with the specified credentials of the column
* @param mixed arg The arguments that are passed to the txtSQL as an array.
* @return void
* @access private
*/
function createtable ($arg=NULL)
{
/* Inside another database? */
if ( !empty($arg['db']) )
{
if ( !$this->selectdb($arg['db']) )
{
return FALSE;
}
}
/* Do we have a selected database? */
if ( empty($this->_SELECTEDDB) )
{
$this->_error(E_USER_NOTICE, 'No database selected');
return FALSE;
}
/* Make sure the database isn't locked */
if ( $this->isLocked($this->_SELECTEDDB) )
{
$this->_error(E_USER_NOTICE, 'Database '.$this->_SELECTEDDB.' is locked');
return FALSE;
}
/* Do we have a valid table name? */
if ( empty($arg['table']) || !preg_match('/^[A-Za-z0-9_]+$/', $arg['table']) )
{
$this->_error(E_USER_NOTICE, 'Table name can only contain letters, and numbers');
return FALSE;
}
/* Do we have any columns? */
if ( empty($arg['columns']) || !is_array($arg['columns']) )
{
$this->_error(E_USER_NOTICE, 'Invalid columns for table '.$arg['table']);
return FALSE;
}
/* Start creating an array and populating it with
* the column names, and types */
$cols = array('primary' => '');
$primaryset = FALSE;
foreach ( $arg['columns'] as $key => $value )
{
/* What an untouched column looks like */
$model = array('permanent' => 0,
'auto_increment' => 0,
'max' => 0,
'type' => 'string',
'default' => '',
'autocount' => (int) 0,
'enum_val' => '');
/* Column cannot be named primary */
if ( $key == 'primary' )
{
$this->_error(E_USER_NOTICE, 'Use of reserved word [primary]');
return FALSE;
}
/* $value has to be an array */
if ( (!empty($value) && !is_array($value)) || empty($key) )
{
$this->_error(E_USER_NOTICE, 'Invalid columns for table '.$arg['table']);
return FALSE;
}
/* Go through each column type */
foreach ( $value as $key1 => $value1 )
{
switch ( strtolower($key1) )
{
case 'auto_increment':
/* Need either a 1 or 0 */
$value1 = (int) $value1;
if ( $value1 < 0 || $value1 > 1 )
{
$this->_error(E_USER_NOTICE, 'Auto_increment must be a boolean 1 or 0');
return FALSE;
}
/* Has to be an integer type */
if ( isset($value['type']) && $value['type'] != 'int' && $value1 == 1 )
{
$this->_error(E_USER_NOTICE, 'auto_increment must be an integer type');
return FALSE;
}
$model['auto_increment'] = $value1;
break;
case 'permanent':
/* Need either a 1 or 0 */
$value1 = (int) $value1;
if ( $value1 < 0 || $value1 > 1 )
{
$this->_error(E_USER_NOTICE, 'Permanent must be a boolean 1 or 0');
return FALSE;
}
$model['permanent'] = $value1;
break;
case 'max':
/* Need an integer value greater than -1, less than 1,000,000 */
$value1 = (int) $value1;
if ( $value1 < 0 || $value1 > 1000000 )
{
$this->_error(E_USER_NOTICE, 'Max must be less than 1,000,000 and greater than -1');
return FALSE;
}
$model['max'] = $value1;
break;
case 'type':
/* Can only accept an integer, string, boolean */
switch ( strtolower($value1) )
{
case 'text':
$model['type'] = 'text';
break;
case 'string':
$model['type'] = 'string';
break;
case 'int':
$model['type'] = 'int';
break;
case 'bool':
$model['type'] = 'bool';
break;
case 'enum':
if ( !isset($value['enum_val']) || !is_array($value['enum_val']) || empty($value['enum_val']) )
{
$this->_error(E_USER_NOTICE, 'Missing enum\'s list of values or invalid list inputted');
return FALSE;
}
$model['type'] = 'enum';
$model['enum_val'] = serialize($value['enum_val']);
break;
case 'date':
$model['type'] = 'date';
break;
default:
$this->_error(E_USER_NOTICE, 'Invalid column type, can only accept integers, strings, and booleans');
return FALSE;
}
break;
case 'default':
$model['default'] = $value1;
break;
case 'primary':
/* Need either a 1 or 0 */
$value1 = (int) $value1;
if ( $value1 < 0 || $value1 > 1 )
{
$this->_error(E_USER_NOTICE, 'Primary must be a boolean 1 or 0');
return FALSE;
}
/* Make sure primary hasn't already been set */
if ( $primaryset === TRUE && $value1 == 1 )
{
$this->_error(E_USER_NOTICE, 'Only one primary column can be set');
return FALSE;
}
if ( $value1 == 1 )
{
/* Primary keys have to be integer and auto_increment */
$value['auto_increment'] = isset($value['auto_increment']) ? $value['auto_increment'] : 0;
$value['type'] = isset($value['type']) ? $value['type'] : 0;
if ( $value['auto_increment'] != 1 || $value['type'] != 'int' )
{
$this->_error(E_USER_NOTICE, 'Primary keys must be of type \'integer\' and auto_increment');
return FALSE;
}
$cols['primary'] = $key;
}
break;
case 'enum_val':
break;
default:
$this->_error(E_USER_NOTICE, 'Invalid column definition, ["'.$key1.'"], specified');
return FALSE;
break;
}
}
$cols[$key] = $model;
}
/* Create two files, $name.myd (empty), and $name.frm (the column defintions) */
$filename = "$this->_LIBPATH/$this->_SELECTEDDB/$arg[table]";
/* Make sure table doesn't exist already */
if ( is_file($filename.".MYD") || is_file($filename.".FRM") )
{
$this->_error(E_USER_NOTICE, 'Table '.$arg['table'].' already exists');
return FALSE;
}
/* Go ahead and create the files */
$fp = @fopen($filename.".MYD", 'w') or $this->_error(E_USER_ERROR, 'Error creating table '.$arg['table']);
@flock($fp, LOCK_EX);
@fwrite($fp, 'a:0:{}') or $this->_error(E_USER_ERROR, 'Error writing to table '.$arg['table'].' while creating it');
@flock($fp, LOCK_UN);
@fclose($fp);
@chmod($filename.".MYD", 0777);
$fp = @fopen($filename.".FRM", 'w') or $this->_error(E_USER_ERROR, 'Error creating table '.$arg['table']);
@flock($fp, LOCK_EX);
@fwrite($fp, serialize($cols)) or $this->_error(E_USER_ERROR, 'Error creating table '.$arg['table']);
@flock($fp, LOCK_UN);
@chmod($filename.".FRM", 0777);