PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/system/kernel/dbase.php

http://tinyshow.googlecode.com/
PHP | 297 lines | 262 code | 31 blank | 4 comment | 55 complexity | 676caa10383f62f6bfc3c39308255d85 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. class dbase{
  3. var $conn, $type, $path, $vurl;
  4. function open(){
  5. if(!$this->conn){
  6. $vurl = parse_url($this->vurl);
  7. $this->type = $vurl['scheme'];
  8. if($this->type == 'sqlite'){
  9. if(function_exists('sqlite_open')){
  10. $this->conn = sqlite_open($vurl['host'] . $vurl['path'], 0666, $error) or die('SQLite Connect Error!');
  11. }else exit('Cannot find sqlite functions!');
  12. }else{
  13. if(isset($vurl['port'])) $vurl['host'] .= ':'. $vurl['port'];
  14. $this->conn = mysql_connect($vurl['host'], $vurl['user'], $vurl['pass']) or die('MySQL Connect Error!');
  15. mysql_select_db(substr($vurl['path'], 1), $this->conn);
  16. mysql_query('set names utf8; set time_zone = \'+8:00\'');
  17. }
  18. }
  19. }
  20. function close(){
  21. if($this->conn){
  22. if($this->type == 'sqlite'){
  23. @sqlite_close($this->conn);
  24. }else{
  25. @mysql_close($this->conn);
  26. }
  27. }
  28. }
  29. function dbname(){
  30. $vurl = parse_url($this->vurl);
  31. $this->type = $vurl['scheme'];
  32. if($this->type == 'sqlite'){
  33. return $vurl['host'] . $vurl['path'];
  34. }elseif($this->type == 'mysql'){
  35. return substr($vurl['path'], 1);
  36. }
  37. return false;
  38. }
  39. function query($sql){
  40. $this->open();
  41. $sql = $this->query_sql($sql);
  42. if($this->type == 'sqlite'){
  43. return sqlite_query($this->conn, $sql);
  44. }else{
  45. return mysql_query($sql);
  46. }
  47. }
  48. // extend
  49. function query_one($sql, $buf=true){
  50. $one = false;
  51. $sql = $this->query_sql($sql);
  52. if($buf && $this->cache_sql($sql)){
  53. return $this->cache_get($sql);
  54. }elseif($res = $this->query($sql)){
  55. if($row = $this->fetch_array($res)){
  56. $one = $row[0];
  57. }
  58. $this->free_result($res);
  59. }
  60. if($buf) $this->cache_set($sql, $one);
  61. return $one;
  62. }
  63. function query_row($sql, $buf=true){
  64. $row = false;
  65. $sql = $this->query_sql($sql);
  66. if($buf && $this->cache_sql($sql)){
  67. return $this->cache_get($sql);
  68. }elseif($res = $this->query($sql)){
  69. $row = $this->fetch_assoc($res);
  70. $this->free_result($res);
  71. }
  72. if($buf) $this->cache_set($sql, $row);
  73. return $row;
  74. }
  75. function query_all($sql, $buf=true){
  76. $all = array();
  77. $sql = $this->query_sql($sql);
  78. if($buf && $this->cache_sql($sql)){
  79. return $this->cache_get($sql);
  80. }elseif($res = $this->query($sql)){
  81. while($row = $this->fetch_assoc($res)){
  82. $all[] = $row;
  83. }
  84. $this->free_result($res);
  85. }
  86. if($buf) $this->cache_set($sql, $all);
  87. return $all;
  88. }
  89. function query_str($sql, $buf=true){
  90. $str = array();
  91. $sql = $this->query_sql($sql);
  92. if($buf && $this->cache_sql($sql)){
  93. return implode(',', $this->cache_get($sql));
  94. }elseif($res = $this->query($sql)){
  95. while($row = $this->fetch_assoc($res)){
  96. $str[] = implode(',', $row);
  97. }
  98. $this->free_result($res);
  99. }
  100. if($buf) $this->cache_set($sql, $str);
  101. return implode(',', $str);
  102. }
  103. function query_list($sql, $page=1, $psiz=20, $buf=true){
  104. $sql = $this->query_sql($sql);
  105. $size = $this->query_one(preg_replace('/select(.+?)from/i', 'select count(*) from', $sql, 1), $buf);
  106. $page = intval($page);
  107. $psiz = intval($psiz);
  108. if($psiz==0) $psiz = 20;
  109. $pcnt = ceil($size / $psiz);
  110. if($page>$pcnt){ $page = $pcnt; }
  111. if($page<1){ $page = 1; }
  112. $sql .= ' limit '.(($page-1) * $psiz).','.$psiz;
  113. $list = array();
  114. $list['data'] = $this->query_all($sql, $buf);
  115. $list['size'] = $size;
  116. $list['psiz'] = $psiz;
  117. $list['pcnt'] = $pcnt;
  118. $list['page'] = $page;
  119. $list['psta'] = $page-5>0 ? $page-5 : 1;
  120. $list['pend'] = $page+5<$pcnt ? $page+5 : $pcnt;
  121. return $list;
  122. }
  123. function query_sql($sql){
  124. if(is_string($sql)){
  125. return $sql;
  126. }elseif(is_array($sql)){
  127. $_sql = '';
  128. foreach($sql as $key => $val){
  129. if(is_array($val)){
  130. $val = implode(', ', $val);
  131. if(strtolower($key) == 'values'){
  132. $val = '('. $val .')';
  133. }
  134. }
  135. $_sql .= $key.' '.$val.' ';
  136. }
  137. return $_sql;
  138. }
  139. }
  140. // caching
  141. function cache_sql($sql){
  142. return is_file($this->path . md5($sql) .'.cache');
  143. }
  144. function cache_get($sql){
  145. if(strtolower(substr($sql, 0, 6)) == 'select'){
  146. $fn = $this->path . md5($sql) .'.cache';
  147. if($fd = @file_get_contents($fn)){
  148. return unserialize($fd);
  149. }
  150. }
  151. return false;
  152. }
  153. function cache_set($sql, $res){
  154. if(strtolower(substr($sql, 0, 6)) == 'select'){
  155. $fn = $this->path . md5($sql) .'.cache';
  156. @file_put_contents($fn, serialize($res));
  157. $db = array();
  158. $fn = $this->path . md5($this->vurl) .'.cache';
  159. if($fd = @file_get_contents($fn)){
  160. $db = unserialize($fd);
  161. }
  162. foreach($this->cache_tbl($sql) as $tbl){
  163. if(!array_key_exists($tbl, $db)){
  164. $db[$tbl] = array();
  165. }
  166. if(!in_array(md5($sql), $db[$tbl])){
  167. $db[$tbl][] = md5($sql);
  168. }
  169. }
  170. @file_put_contents($fn, serialize($db));
  171. return true;
  172. }
  173. return false;
  174. }
  175. function cache_del($tbl){
  176. $db = array();
  177. $fn = $this->path . md5($this->vurl) .'.cache';
  178. if($fd = @file_get_contents($fn)){
  179. $db = unserialize($fd);
  180. }
  181. if(array_key_exists($tbl, $db)){
  182. foreach($db[$tbl] as $file){
  183. @unlink($this->path .$file .'.cache');
  184. }
  185. unset($db[$tbl]);
  186. @file_put_contents($fn, serialize($db));
  187. return true;
  188. }
  189. return false;
  190. }
  191. function cache_tbl($sql){
  192. if(strtolower(substr($sql,0,6))=='select'){
  193. $sql = preg_replace('/[`\t\r\n]+/', ' ', $sql);
  194. $sql = preg_replace('/select(.+?)from/i', '', $sql, 1);
  195. $sql = substr($sql, 0, strpos(strtolower($sql .' where '), ' where '));
  196. $tbl = explode(',', $sql);
  197. foreach($tbl as $key => $val){
  198. $tbl[$key] = trim($val);
  199. }
  200. return $tbl;
  201. }
  202. return false;
  203. }
  204. // compatible
  205. function fetch_array($res){
  206. if($this->type == 'sqlite'){
  207. return sqlite_fetch_array($res);
  208. }else{
  209. return mysql_fetch_array($res);
  210. }
  211. }
  212. function fetch_assoc($res){
  213. if($this->type == 'sqlite'){
  214. return sqlite_fetch_array($res, SQLITE_ASSOC);
  215. }else{
  216. return mysql_fetch_assoc($res);
  217. }
  218. }
  219. function free_result($res){
  220. if($this->type != 'sqlite'){
  221. return mysql_free_result($res);
  222. }
  223. }
  224. // safe check
  225. function check(){
  226. foreach($_GET as $key => $val){
  227. if(is_numeric($val)){
  228. $_GET[$key] = intval($val);
  229. }else{
  230. $_GET[$key] = $this->escape($val);
  231. }
  232. }
  233. foreach($_POST as $key => $val){
  234. if(is_numeric($val)){
  235. $_POST[$key] = intval($val);
  236. }else{
  237. $_POST[$key] = $this->escape($val);
  238. }
  239. }
  240. }
  241. function escape($str){
  242. if(is_array($str)){
  243. foreach($str as $key => $val)
  244. $str[$key] = $this->escape($val);
  245. }elseif(!get_magic_quotes_gpc()){
  246. $str = str_replace('\'','\'\'', trim($str));
  247. }
  248. return stripslashes($str);
  249. }
  250. function initiate(){
  251. defined('HT_WEBID') or die('Undefined HT_WEBID');
  252. defined('HT_DBURL') or die('Undefined HT_DBURL');
  253. }
  254. function __construct(){
  255. $this->initiate();
  256. $this->conn = null;
  257. $this->type = 'sqlite';
  258. $this->path = 'caches/'. HT_WEBID .'/';
  259. if(!is_dir($this->path)) mkdir($this->path);
  260. $this->check();
  261. }
  262. function __destruct(){
  263. if($this->conn) $this->close();
  264. }
  265. }
  266. ?>