PageRenderTime 119ms CodeModel.GetById 45ms app.highlight 19ms RepoModel.GetById 49ms app.codeStats 0ms

/lib/data.class.php

http://litepublisher.googlecode.com/
PHP | 493 lines | 420 code | 64 blank | 9 comment | 52 complexity | bd0d9673a1402887fd4d17942b0f7493 MD5 | raw file
  1<?php
  2/**
  3* Lite Publisher
  4* Copyright (C) 2010 - 2013 Vladimir Yushko http://litepublisher.ru/ http://litepublisher.com/
  5* Dual licensed under the MIT (mit.txt)
  6* and GPL (gpl.txt) licenses.
  7**/
  8
  9class tdata {
 10  public $basename;
 11  public $cache;
 12  public $coclasses;
 13  public $coinstances;
 14  public $data;
 15  public $lockcount;
 16  public $table;
 17  
 18  public static function i() {
 19    return getinstance(get_called_class());
 20  }
 21  
 22  public static function instance() {
 23    return getinstance(get_called_class());
 24  }
 25  
 26  public function __construct() {
 27    $this->lockcount = 0;
 28    $this->cache= true;
 29    $this->data= array();
 30    $this->coinstances = array();
 31    $this->coclasses = array();
 32    $this->basename = substr(get_class($this), 1);
 33    $this->create();
 34  }
 35  
 36  protected function create() {
 37  }
 38  
 39  public function __get($name) {
 40    if (method_exists($this, $get = 'get' . $name))  {
 41      return $this->$get();
 42    } elseif (array_key_exists($name, $this->data)) {
 43      return $this->data[$name];
 44    } else {
 45      foreach ($this->coinstances as $coinstance) {
 46        if (isset($coinstance->$name)) return $coinstance->$name;
 47      }
 48      return    $this->error(sprintf('The requested property "%s" not found in class  %s', $name, get_class($this)));
 49    }
 50  }
 51  
 52  public function __set($name, $value) {
 53    if (method_exists($this, $set = 'set' . $name)) {
 54      $this->$set($value);
 55      return true;
 56    }
 57    
 58    if (key_exists($name, $this->data)) {
 59      $this->data[$name] = $value;
 60      return true;
 61    }
 62    
 63    foreach ($this->coinstances as $coinstance) {
 64      if (isset($coinstance->$name)) {
 65        $coinstance->$name = $value;
 66        return true;
 67      }
 68    }
 69    
 70    return false;
 71  }
 72  
 73  public  function __call($name, $params) {
 74    if (method_exists($this, strtolower($name))) {
 75      return call_user_func_array(array($this, strtolower($name)), $params);
 76    }
 77    
 78    foreach ($this->coinstances as $coinstance) {
 79      if (method_exists($coinstance, $name) || $coinstance->method_exists($name))
 80      return call_user_func_array(array($coinstance, $name), $params);
 81    }
 82    $this->error("The requested method $name not found in class " . get_class($this));
 83  }
 84  
 85  public function __isset($name) {
 86    if (array_key_exists($name, $this->data) || method_exists($this, "get$name") || method_exists($this, "Get$name")) return true;
 87    foreach ($this->coinstances as $coinstance) {
 88      if (isset($coinstance->$name)) return true;
 89    }
 90    return false;
 91  }
 92  
 93  public function method_exists($name) {
 94    return false;
 95  }
 96  
 97  public function error($Msg, $code = 0) {
 98    throw new Exception($Msg, $code);
 99  }
100  
101  public function getbasename() {
102    return $this->basename;
103  }
104  
105  public function install() {
106    $this->externalchain('Install');
107  }
108  
109  public function uninstall() {
110    $this->externalchain('Uninstall');
111  }
112  
113  public function validate($repair = false) {
114    $this->externalchain('Validate', $repair);
115  }
116  
117  protected function externalchain($func, $arg = null) {
118    $parents = class_parents($this);
119    array_splice($parents, 0, 0, get_class($this));
120    foreach ($parents as $key => $class) {
121      $this->externalfunc($class, $func, $arg);
122    }
123  }
124  
125  public function externalfunc($class, $func, $args) {
126    if ($filename = litepublisher::$classes->getclassfilename($class, true)) {
127      $externalname = basename($filename, '.php') . '.install.php';
128      $dir = dirname($filename) . DIRECTORY_SEPARATOR;
129      $file = $dir . 'install' . DIRECTORY_SEPARATOR . $externalname;
130      if (!file_exists($file)) {
131        $file =$dir .  $externalname;
132        if (!file_exists($file)) return;
133      }
134      include_once($file);
135      $fnc = $class . $func;
136      if (function_exists($fnc)) {
137        //$fnc($this, $arg);
138        if (is_array($args)) {
139          array_unshift($args, $this);
140        } else {
141          $args = array($this, $args);
142        }
143        return call_user_func_array($fnc, $args);
144      }
145    }
146  }
147  
148  public function load() {
149    //if ($this->dbversion == 'full') return $this->LoadFromDB();
150    return tfilestorage::load($this);
151  }
152  
153  public function save() {
154    if ($this->lockcount) return;
155    if ($this->dbversion) {
156      $this->SaveToDB();
157    } else {
158      tfilestorage::save($this);
159    }
160  }
161  
162  public function savetostring() {
163    return serialize($this->data);
164  }
165  
166  public function loadfromstring($s) {
167    try {
168      if (!empty($s)) $this->data = unserialize($s) + $this->data;
169      $this->afterload();
170      return true;
171    } catch (Exception $e) {
172      echo 'Caught exception: '.  $e->getMessage() ;
173      return false;
174    }
175  }
176  
177  public function afterload() {
178    foreach ($this->coinstances as $coinstance) {
179      if (method_exists($coinstance, 'afterload')) $coinstance->afterload();
180    }
181  }
182  
183  public function lock() {
184    $this->lockcount++;
185  }
186  
187  public function unlock() {
188    if (--$this->lockcount <= 0) $this->save();
189  }
190  
191  public function getlocked() {
192    return $this->lockcount  > 0;
193  }
194  
195  public function Getclass() {
196    return get_class($this);
197  }
198  
199  public function getdbversion() {
200    return false; // dbversion == 'full';
201  }
202  
203  public function getdb($table = '') {
204    $table =$table != '' ? $table : $this->table;
205    if ($table != '') litepublisher::$db->table = $table;
206    return litepublisher::$db;
207  }
208  
209  protected function SaveToDB() {
210    $this->db->add($this->getbasename(), $this->savetostring());
211  }
212  
213  protected function LoadFromDB() {
214    if ($r = $this->db->select('basename = '. $this->getbasename() . "'")) {
215      return $this->loadfromstring($r['data']);
216    }
217  }
218  
219  protected function getthistable() {
220    return litepublisher::$db->prefix . $this->table;
221  }
222  
223  public static function get_class_name($c) {
224    return is_object($c) ? get_class($c) : trim($c);
225  }
226  
227}//class
228
229class tfilestorage {
230  public static $disabled;
231  public static $memcache = false;
232  
233  public static function save(tdata $obj) {
234    if (self::$disabled) return false;
235    return self::savetofile(litepublisher::$paths->data .$obj->getbasename(), $obj->savetostring());
236  }
237  
238  public static function load(tdata $obj) {
239    if ($s = self::loadfile(litepublisher::$paths->data . $obj->getbasename() .'.php')) {
240      return $obj->loadfromstring($s);
241    }
242    return false;
243  }
244  
245  public static function loadfile($filename) {
246    if (self::$memcache) {
247      if ($s =  self::$memcache->get($filename)) return $s;
248    }
249    
250    if (file_exists($filename)) {
251      $s = self::uncomment_php(file_get_contents($filename));
252      if (self::$memcache) self::$memcache->set($filename, $s, false, 3600);
253      return $s;
254    }
255    return false;
256  }
257  
258  public static function savetofile($base, $content) {
259    if (self::$memcache) self::$memcache->set($base . '.php', $content, false, 3600);
260    $tmp = $base .'.tmp.php';
261    if(false === file_put_contents($tmp, self::comment_php($content))) {
262      litepublisher::$options->trace(sprintf('Error write to file "%s"', $tmp));
263      return false;
264    }
265    chmod($tmp, 0666);
266    $filename = $base .'.php';
267    if (file_exists($filename)) {
268      $back = $base . '.bak.php';
269      self::delete($back);
270      rename($filename, $back);
271    }
272    if (!rename($tmp, $filename)) {
273      litepublisher::$options->trace(sprintf('Error rename temp file "%s" to "%s"', $tmp, $filename));
274      return false;
275    }
276    return true;
277  }
278  
279  public static function delete($filename) {
280    if (file_exists($filename)) {
281      if (!unlink($filename)) {
282        chmod($filename, 0666);
283        unlink($filename);
284      }
285    }
286    
287    if (self::$memcache) self::$memcache->delete($filename);
288  }
289  
290  public static function getfile($filename) {
291    if (self::$memcache) {
292      if ($s =  self::$memcache->get($filename)) return $s;
293    }
294    
295    if (file_exists($filename)) {
296      $s = file_get_contents($filename);
297      if (self::$memcache) self::$memcache->set($filename, $s, false, 3600);
298      return $s;
299    }
300    return false;
301  }
302  
303  public static function setfile($filename, $content) {
304    if (self::$memcache) self::$memcache->set($filename, $content, false, 3600);
305    file_put_contents($filename, $content);
306    @chmod($filename, 0666);
307  }
308  
309  public static function savevar($filename, &$var) {
310    return self::savetofile($filename, serialize($var));
311  }
312  
313  public static function loadvar($filename, &$var) {
314    if ($s = self::loadfile($filename . '.php')) {
315      $var = unserialize($s);
316      return true;
317    }
318    return false;
319  }
320  
321  public static function comment_php($s) {
322    return sprintf('<?php /* %s */ ?>', str_replace('*/', '**//*/', $s));
323  }
324  
325  public static function uncomment_php($s) {
326    return str_replace('**//*/', '*/', substr($s, 9, strlen($s) - 9 - 6));
327  }
328  
329}//class
330
331class tstorage extends tfilestorage {
332  public static $data;
333  private static $modified;
334  
335  public static function save(tdata $obj) {
336    self::$modified = true;
337    $base = $obj->getbasename();
338    if (!isset(self::$data[$base])) self::$data[$base] = &$obj->data;
339    return true;
340  }
341  
342  public static function load(tdata $obj) {
343    $base = $obj->getbasename();
344    if (isset(self::$data[$base])) {
345      $obj->data = &self::$data[$base];
346      $obj->afterload();
347      return true;
348    } else {
349      self::$data[$base] = &$obj->data;
350      return false;
351    }
352  }
353  
354  public static function savemodified() {
355    if (self::$modified) {
356      if (self::$disabled) return false;
357      $lock = litepublisher::$paths->data .'storage.lok';
358      if (($fh = @fopen($lock, 'w')) &&       flock($fh, LOCK_EX | LOCK_NB)) {
359        self::savetofile(litepublisher::$paths->data .'storage', serialize(self::$data));
360        flock($fh, LOCK_UN);
361        fclose($fh);
362        @chmod($lock, 0666);
363      } else {
364        tfiler::log('Storage locked, data not saved');
365      }
366      self::$modified = false;
367      return true;
368    }
369    return false;
370  }
371  
372  public static function loaddata() {
373    self::$data = array();
374    return self::loadvar(litepublisher::$paths->data . 'storage', self::$data);
375  }
376  
377}//class
378
379class tarray2prop {
380  public $array;
381public function __construct(array $a = null) { $this->array = $a; }
382public function __destruct() { unset($this->array); }
383public function __get($name) { return $this->array[$name]; }
384public function __set($name, $value) { $this->array[$name] = $value; }
385public function __isset($name) { return array_key_exists($name, $this->array); }
386public function __tostring() { return $this->array['']; }
387}//class
388
389function sqldate($date = 0) {
390  if ($date == 0) $date = time();
391  return date('Y-m-d H:i:s', $date);
392}
393
394function sqltime($date = 0) {
395  if ($date == 0) return '0000-00-00 00:00:00';
396  return date('Y-m-d H:i:s', $date);
397}
398
399function dbquote($s) {
400  return litepublisher::$db->quote($s);
401}
402
403function md5uniq() {
404  return basemd5(mt_rand() . litepublisher::$secret. microtime());
405}
406
407function basemd5($s) {
408  return trim(base64_encode(md5($s, true)), '=');
409}
410
411function strbegin($s, $begin) {
412  return strncmp($s, $begin, strlen($begin)) == 0;
413}
414
415function strbegins() {
416  $a = func_get_args();
417  $s = array_shift($a);
418  while ($begin = array_shift($a)) {
419    if (strncmp($s, $begin, strlen($begin)) == 0) return true;
420  }
421  return false;
422}
423
424function strend($s, $end) {
425  return $end == substr($s, 0 - strlen($end));
426}
427
428function strip_utf($s) {
429  $utf = "\xEF\xBB\xBF";
430  return strbegin($s, $utf) ? substr($s, strlen($utf)) : $s;
431}
432
433function array_delete(array &$a, $i) {
434  array_splice($a, $i, 1);
435}
436
437function array_delete_value(array &$a, $value) {
438  $i = array_search($value, $a);
439  if ($i !== false)         array_splice($a, $i, 1);
440}
441
442function array_clean(array &$items) {
443  $items = array_unique($items);
444  foreach (array(0, false, null, '') as $v) {
445    $i = array_search($v, $items);
446    if ($i !== false)         array_splice($items, $i, 1);
447  }
448}
449
450function array_insert(array &$a, $item, $index) {
451  array_splice($a, $index, 0, array($item));
452}
453
454function array_move(array &$a, $oldindex, $newindex) {
455  //delete and insert
456  if (($oldindex == $newindex) || !isset($a[$oldindex])) return false;
457  $item = $a[$oldindex];
458  array_splice($a, $oldindex, 1);
459  array_splice($a, $newindex, 0, array($item));
460}
461
462function strtoarray($s) {
463  $a = explode("\n", trim($s));
464  foreach ($a as $k => $v) $a[$k] = trim($v);
465  return $a;
466}
467
468function tojson($a) {
469  if (defined('JSON_NUMERIC_CHECK')) {
470    return json_encode($a, JSON_NUMERIC_CHECK | (defined('JSON_UNESCAPED_UNICODE') ? JSON_UNESCAPED_UNICODE : 0));
471  }
472  
473  return json_encode($a);
474}
475
476function jsonattr($a) {
477  return str_replace('"', '&quot;', tojson($a));
478}
479
480function toenum($v, array $a) {
481  $v = trim($v);
482  return in_array($v, $a) ? $v : $a[0];
483}
484
485function dumpstr($s) {
486  echo "<pre>\n", htmlspecialchars($s), "</pre>\n";
487}
488
489function dumpvar($v) {
490  echo "<pre>\n";
491  var_dump($v);
492  echo "</pre>\n";
493}