PageRenderTime 62ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/data.class.php

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