-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiledb.php
733 lines (687 loc) · 14.9 KB
/
filedb.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
<?php
/**
Simple Flat-file ORM for the PHP Fat-Free Framework
The contents of this file are subject to the terms of the GNU General
Public License Version 3.0. You may not use this file except in
compliance with the license. Any of the license terms and conditions
can be waived if you get permission from the copyright holder.
Copyright (c) 2009-2012 F3::Factory
Bong Cosca <[email protected]>
@package FileDB
@version 2.0.12
**/
//! Flat-file data access layer
class FileDB extends Base {
//@{ Storage formats
const
FORMAT_Plain=0,
FORMAT_Serialized=1,
FORMAT_JSON=2,
FORMAT_GZip=3;
//@}
public
//! Exposed properties
$path,$result;
private
//! Storage settings
$format,
//! Journal identifier
$journal;
/**
Begin transaction
@public
**/
function begin() {
$this->journal=base_convert(microtime(TRUE)*100,10,16);
}
/**
Rollback transaction
@public
**/
function rollback() {
if ($glob=glob($this->path.'*.jnl.'.$this->journal))
foreach ($glob as $temp) {
self::mutex(
function() use($temp) {
unlink($temp);
},
$temp
);
break;
}
$this->journal=NULL;
}
/**
Commit transaction
@public
**/
function commit() {
if ($glob=glob($this->path.'*.jnl.'.$this->journal))
foreach ($glob as $temp) {
$file=preg_replace('/\.jnl\.'.$this->journal.'$/','',$temp);
self::mutex(
function() use($temp,$file) {
@rename($temp,$file);
},
$temp,$file
);
break;
}
$this->journal=NULL;
}
/**
Retrieve contents of flat-file
@return mixed
@param $file string
@public
**/
function read($file) {
$file=$this->path.$file;
if (!is_file($file))
return array();
$text=self::getfile($file);
$out='';
switch ($this->format) {
case self::FORMAT_GZip:
$text=gzinflate($text);
case self::FORMAT_Plain:
if (ini_get('allow_url_fopen') &&
ini_get('allow_url_include'))
// Stream wrap
$file='data:text/plain,'.urlencode($text);
else {
$file=self::$vars['TEMP'].$_SERVER['SERVER_NAME'].'.'.
'php.'.self::hash($file);
self::putfile($file,$text);
}
$instance=new F3instance;
$out=$instance->sandbox($file);
break;
case self::FORMAT_Serialized:
$out=unserialize($text);
break;
case self::FORMAT_JSON:
$out=json_decode($text,TRUE);
}
return $out;
}
/**
Store PHP expression in flat-file
@param $file string
@param $expr mixed
@public
**/
function write($file,$expr) {
$file=$this->path.$file;
$auto=FALSE;
if (!$this->journal) {
$auto=TRUE;
$this->begin();
if (is_file($file))
copy($file,$file.'.jnl.'.$this->journal);
}
$file.='.jnl.'.$this->journal;
if (!$expr)
$expr=array();
$out='<?php'."\n\n".'return '.self::stringify($expr).';'."\n";
switch ($this->format) {
case self::FORMAT_GZip:
$out=gzdeflate($out);
break;
case self::FORMAT_Serialized:
$out=serialize($expr);
break;
case self::FORMAT_JSON:
$out=json_encode($expr);
}
if (self::putfile($file,$out)===FALSE)
$this->rollback();
elseif ($auto)
$this->commit();
}
/**
Convert database to another format
@return bool
@param $fmt int
@public
**/
function convert($fmt) {
$glob=glob($this->path.'*');
if ($glob) {
foreach ($glob as $file) {
$file=str_replace($this->path,'',$file);
$out=$this->read($file);
switch ($fmt) {
case self::FORMAT_GZip:
$out=gzdeflate($out);
break;
case self::FORMAT_Serialized:
$out=serialize($out);
break;
case self::FORMAT_JSON;
$out=json_encode($out);
}
$this->format=$fmt;
$this->write($file,$out);
}
return TRUE;
}
return FALSE;
}
/**
Custom session handler
@param $table string
@public
**/
function session($table='sessions') {
session_set_save_handler(
// Open
function($path,$name) {
register_shutdown_function('session_commit');
return TRUE;
},
// Close
function() {
return TRUE;
},
// Read
function($id) use($table) {
$jig=new Jig($table);
$jig->load(array('id'=>$id));
return $jig->dry()?FALSE:$jig->data;
},
// Write
function($id,$data) use($table) {
$jig=new Jig($table);
$jig->load(array('id'=>$id));
$jig->id=$id;
$jig->data=$data;
$jig->stamp=time();
$jig->save();
return TRUE;
},
// Delete
function($id) use($table) {
$jig=new Jig($table);
$jig->erase(array('id'=>$id));
return TRUE;
},
// Cleanup
function($max) use($table) {
$jig=new Jig($table);
$jig->erase(
array(
'_PHP_'=>
array(
'stamp'=>
function($stamp) use($max) {
return $stamp+$max<time();
}
)
)
);
return TRUE;
}
);
}
/**
Class constructor
@param $path string
@param $fmt int
@public
**/
function __construct($path=NULL,$fmt=self::FORMAT_Plain) {
$path=self::fixslashes(realpath(self::resolve($path)).'/');
if (!is_dir($path))
self::mkdir($path);
list($this->path,$this->format)=array($path,$fmt);
if (!isset(self::$vars['DB']))
self::$vars['DB']=$this;
}
}
//! Flat-file ORM
class Jig extends Base {
//@{ Locale-specific error/exception messages
const
TEXT_JigCriteria='Invalid criteria: %s',
TEXT_JigCallback='Invalid callback: %s',
TEXT_JigConnect='Undefined database',
TEXT_JigEmpty='Jig is empty',
TEXT_JigTable='Table %s does not exist',
TEXT_JigField='Field %s does not exist';
//@}
//@{ Locale-specific error/exception messages
const
TEXT_Criteria='Invalid criteria: %s',
TEXT_Callback='Invalid callback: %s';
//@}
//@{
//! Jig properties
public
$_id;
private
$db,$table,$object,$mod,$cond,$seq,$ofs;
//@}
/**
Jig factory
@return object
@param $obj array
@public
**/
function factory($obj) {
$self=get_class($this);
$jig=new $self($this->table,$this->db);
$jig->_id=$obj['_id'];
unset($obj['_id']);
foreach ($obj as $key=>$val)
$jig->object[$key]=$val;
return $jig;
}
/**
Evaluate query criteria
@return boolean
@param $expr array
@param $obj array
@private
**/
private function check(array $expr,array $obj) {
if (is_null($expr))
return TRUE;
if (is_array($expr)) {
$result=TRUE;
foreach ($expr as $field=>$cond) {
if ($field=='_OR_') {
if (!is_array($cond)) {
trigger_error(
sprintf(
self::TEXT_JigCriteria,
$this->stringify($cond)
)
);
return FALSE;
}
foreach ($cond as $val)
// Short circuit
if ($this->check($val,$obj))
return TRUE;
return FALSE;
}
elseif ($field=='_PHP_') {
list($key,$val)=array(key($cond),current($cond));
if (!is_array($cond) || !is_callable($val)) {
trigger_error(
sprintf(
self::TEXT_JigCallback,
$this->stringify($val)
)
);
return FALSE;
}
return isset($obj[$key])?
call_user_func($val,$obj[$key]):TRUE;
}
elseif (!isset($obj[$field]) && !is_null($obj[$field]))
$result=FALSE;
elseif (is_array($cond)) {
$map=array(
'='=>'==',
'eq'=>'==',
'gt'=>'>',
'lt'=>'<',
'gte'=>'>=',
'lte'=>'<=',
'<>'=>'!=',
'ne'=>'!='
);
foreach ($cond as $op=>$val)
$result=($op=='_OR_' || $op=='_PHP_')?
$this->check($val,$obj):
($op=='regex'?
preg_match('/'.$val.'/s',$obj[$field]):
eval(
'return $obj[$field]'.
(isset($map[$op])?$map[$op]:$op).
'(is_string($val)?'.
'self::resolve($val):$val);'));
}
else
$result=($obj[$field]==(is_string($cond)?
self::resolve($cond):$cond));
if (!$result)
break;
}
return $result;
}
return (bool)$expr;
}
/**
Return current object contents as an array
@return array
@public
**/
function cast() {
return array_merge(array('_id'=>$this->_id),$this->object);
}
/**
Return an array of objects matching criteria
@return array
@param $cond array
@param $seq array
@param $limit mixed
@param $ofs int
@param $jig boolean
@public
**/
function find(
array $cond=NULL,array $seq=NULL,$limit=0,$ofs=0,$jig=TRUE) {
$table=$this->db->read($this->table);
$result=array();
if ($table) {
if (is_array($seq))
foreach (array_reverse($seq,TRUE) as $key=>$sort)
Matrix::sort($table,$key,$sort);
foreach ($table as $key=>$obj) {
$obj['_id']=$key;
if (is_null($cond) || $this->check($cond,$obj))
$result[]=$jig?$this->factory($obj):$obj;
}
$result=array_slice($result,$ofs,$limit?:NULL);
}
$this->db->result=$result;
return $result;
}
/**
Return an array of associative arrays matching criteria
@return array
@param $cond array
@param $seq array
@param $limit mixed
@param $ofs int
@public
**/
function afind(array $cond=NULL,array $seq=NULL,$limit=0,$ofs=0) {
return $this->find($cond,$seq,$limit,$ofs,FALSE);
}
/**
Return the first object that matches the specified criteria
@return array
@param $cond array
@param $seq array
@param $ofs int
@public
**/
function findone(array $cond=NULL,array $seq=NULL,$ofs=0) {
list($result)=$this->find($cond,$seq,1,$ofs)?:array(NULL);
return $result;
}
/**
Return the array equivalent of the object matching criteria
@return array
@param $cond array
@param $seq array
@param $ofs int
@public
**/
function afindone(array $cond=NULL,array $seq=NULL,$ofs=0) {
list($result)=$this->afind($cond,$seq,1,$ofs)?:array(NULL);
return $result;
}
/**
Count objects that match condition
@return int
@param $cond array
@public
**/
function found(array $cond=NULL) {
return count($this->find($cond));
}
/**
Hydrate Jig with elements from framework array variable, keys of
which will be identical to object properties
@param $name string
@public
**/
function copyFrom($name) {
if (is_array($ref=self::ref($name))) {
foreach ($ref as $key=>$val)
$this->object[$key]=$val;
$this->mod=TRUE;
}
}
/**
Populate framework array variable with Jig properties, keys of
which will have names identical to object properties
@param $name string
@param $fields string
@public
**/
function copyTo($name,$fields=NULL) {
if ($this->dry()) {
trigger_error(self::TEXT_JigEmpty);
return;
}
if (is_string($fields))
$list=preg_split('/[\|;,]/',$fields,0,PREG_SPLIT_NO_EMPTY);
foreach (array_keys($this->object) as $field)
if (!isset($list) || in_array($field,$list)) {
$var=&self::ref($name);
$var[$field]=$this->object[$field];
}
}
/**
Dehydrate Jig
@public
**/
function reset() {
// Dehydrate
$this->_id=NULL;
$this->object=NULL;
$this->mod=NULL;
$this->cond=NULL;
$this->seq=NULL;
$this->ofs=0;
}
/**
Retrieve first object that satisfies criteria
@return mixed
@param $cond array
@param $seq array
@param $ofs int
@public
**/
function load(array $cond=NULL,array $seq=NULL,$ofs=0) {
if ($ofs>-1) {
$this->ofs=0;
if ($jig=$this->findone($cond,$seq,$ofs)) {
if (method_exists($this,'beforeLoad') &&
$this->beforeLoad()===FALSE)
return FALSE;
// Hydrate Jig
$this->_id=$jig->_id;
foreach ($jig->object as $key=>$val)
$this->object[$key]=$val;
list($this->cond,$this->seq,$this->ofs)=
array($cond,$seq,$ofs);
if (method_exists($this,'afterLoad'))
$this->afterLoad();
$this->mod=NULL;
return $this;
}
}
$this->reset();
return FALSE;
}
/**
Retrieve N-th object relative to current using the same criteria
that hydrated Jig
@return mixed
@param $count int
@public
**/
function skip($count=1) {
if ($this->dry()) {
trigger_error(self::TEXT_JigEmpty);
return FALSE;
}
return $this->load($this->cond,$this->seq,$this->ofs+$count);
}
/**
Return next record
@return array
@public
**/
function next() {
return $this->skip();
}
/**
Return previous record
@return array
@public
**/
function prev() {
return $this->skip(-1);
}
/**
Insert/update object
@public
**/
function save() {
if ($this->dry() ||
method_exists($this,'beforeSave') &&
$this->beforeSave()===FALSE)
return;
if ($this->mod) {
// Object modified
$table=$this->db->read($this->table);
$obj=$this->object;
if (!is_null($this->_id))
// Update
$id=$this->_id;
else {
// Insert with concurrency control
while (($id=base_convert(microtime(TRUE)*100,10,16)) &&
isset($table[$id])) {
usleep(mt_rand(0,100));
// Reload table
$table=$this->db->read($this->table);
}
$this->_id=$id;
}
$table[$id]=$obj;
// Save to file
$this->db->write($this->table,$table);
}
if (method_exists($this,'afterSave'))
$this->afterSave();
}
/**
Delete object/s and reset Jig
@param $cond array
@param $force boolean
@public
**/
function erase(array $cond=NULL,$force=FALSE) {
if (method_exists($this,'beforeErase') &&
$this->beforeErase()===FALSE)
return;
if (!$cond)
$cond=$this->cond;
if ($force || $cond) {
$table=$this->db->read($this->table);
foreach ($this->find($cond) as $found)
unset($table[$found->_id]);
// Save to file
$this->db->write($this->table,$table);
}
$this->reset();
if (method_exists($this,'afterErase'))
$this->afterErase();
}
/**
Return TRUE if Jig is NULL
@return boolean
@public
**/
function dry() {
return is_null($this->object);
}
/**
Synchronize Jig and underlying file
@param $table string
@param $db object
@public
**/
function sync($table,$db=NULL) {
if (!$db) {
if (isset(self::$vars['DB']) &&
is_a(self::$vars['DB'],'FileDB'))
$db=self::$vars['DB'];
else {
trigger_error(self::TEXT_JigConnect);
return;
}
}
if (method_exists($this,'beforeSync') &&
$this->beforeSync()===FALSE)
return;
// Initialize Jig
list($this->db,$this->table)=array($db,$table);
if (method_exists($this,'afterSync'))
$this->afterSync();
}
/**
Return value of Jig-mapped property
@return boolean
@param $name string
@public
**/
function &__get($name) {
return $this->object[$name];
}
/**
Assign value to Jig-mapped property
@return boolean
@param $name string
@param $val mixed
@public
**/
function __set($name,$val) {
if (!isset($this->object[$name]) || $this->object[$name]!=$val)
$this->mod=TRUE;
$this->object[$name]=$val;
}
/**
Clear value of Jig-mapped property
@return boolean
@param $name string
@public
**/
function __unset($name) {
unset($this->object[$name]);
$this->mod=TRUE;
}
/**
Return TRUE if Jig-mapped property exists
@return boolean
@param $name string
@public
**/
function __isset($name) {
return array_key_exists($name,$this->object);
}
/**
Display class name if conversion to string is attempted
@public
**/
function __toString() {
return get_class($this);
}
/**
Class constructor
@public
**/
function __construct() {
// Execute mandatory sync method
call_user_func_array(array($this,'sync'),func_get_args());
}
}