PageRenderTime 28ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/published/SC/html/scripts/classes/database/class.database.php

https://github.com/foxluck/otdelstroy
PHP | 311 lines | 294 code | 4 blank | 13 comment | 9 complexity | 6a2f45dcfe2ccdebed24e02b993ccaa2 MD5 | raw file
  1. <?
  2. /**
  3. * Interface to database
  4. *
  5. */
  6. class DataBase{
  7. var $link;
  8. var $type;
  9. var $host;
  10. var $user;
  11. var $pass;
  12. var $database;
  13. var $result;
  14. var $last_sql;
  15. /**
  16. * Connect to server
  17. *
  18. * @param string host
  19. * @param string user_name
  20. * @param string password
  21. * @param string database_type
  22. */
  23. function connect($_host, $_user, $_pass, $_type = 'mysql'){
  24. $this->type = $_type;
  25. $this->host = $_host;
  26. $this->user = $_user;
  27. switch ($this->type){
  28. case 'mysql':
  29. $this->link = mysql_pconnect($_host, $_user, $_pass);
  30. if(!$this->link) {
  31. //log unsuccesfull connection
  32. if(sc_onWebasystServer()){
  33. $fpath = WBS_DIR.'/kernel/error_db_connect.log';
  34. $data = array();
  35. $data[] = date('Y-m-d H:i:s');
  36. $data[] = sprintf("%s@%s",$_user,$_pass);
  37. $data[] = db_getConnectData('DB_KEY');
  38. $data[] = getenv("REMOTE_ADDR");
  39. $data[] = "SC";
  40. if($fp = fopen($fpath,'a')){
  41. fwrite($fp,sprintf("%s\n",implode(";",$data)));
  42. fclose($fp);
  43. }
  44. }
  45. die("Error connect to mysql");
  46. }
  47. $ServerVersion = $this->getServerVersion();
  48. if(preg_match('/^5\./',$ServerVersion))$this->query('SET SESSION sql_mode=0');
  49. if(preg_match('/^5\.|^4\.[1-9]\./',$ServerVersion))$this->query('SET NAMES '.MYSQL_CHARSET);
  50. $this->query (sprintf("set character_set_client='%s'",MYSQL_CHARSET));
  51. $this->query (sprintf("set character_set_results='%s'",MYSQL_CHARSET));
  52. $this->query (sprintf("set collation_connection='%s_general_ci'",MYSQL_CHARSET));
  53. break;
  54. }
  55. }
  56. function selectDB($_dbName){
  57. $this->database = $_dbName;
  58. $url = str_replace('//','/',WBS_INSTALL_PATH.'/login/');
  59. switch ($this->type){
  60. case 'mysql':
  61. if(!mysql_select_db($this->database, $this->link)) {
  62. if(isset($_GET['debug']))
  63. {
  64. print htmlentities(mysql_error())."<br>";
  65. }
  66. $this->printMessage($url,"Couldn't select database");
  67. }
  68. break;
  69. }
  70. if(!$this->TableExists(DIVISIONS_TBL)){
  71. $this->printMessage($url);
  72. }
  73. }
  74. /**
  75. * Enter description here...
  76. *
  77. * @param string $_sql
  78. * @return DBResource
  79. */
  80. function query ($_sql){
  81. /*
  82. @features My
  83. */
  84. if(isset($_GET['debug'])&&$_GET['debug']=='time'){
  85. $this->last_sql = $_sql;
  86. $res = db_query($_sql);
  87. $this->result = $res["resource"];
  88. return new DBResource($this->result, $this->type, $this->link, $this->last_sql);
  89. ClassManager::includeClass('timer');
  90. static $cnt=0;
  91. $cnt++;
  92. $T = new Timer();
  93. $T->timerStart();
  94. }
  95. /*
  96. @features
  97. */
  98. $this->last_sql = $_sql;
  99. switch ($this->type){
  100. case 'mysql':
  101. mysql_select_db($this->database,$this->link);
  102. $this->result = mysql_query($this->last_sql, $this->link);
  103. if(!$this->result){
  104. pear_handler();
  105. pear_handler(PEAR::raiseError("Error executing query: ".$this->last_sql.'<br />'.mysql_error($this->link)));
  106. die("Error executing query: ".$this->last_sql.'<br />'.mysql_error($this->link));
  107. }
  108. break;
  109. }
  110. /*
  111. @features My
  112. */
  113. if(isset($_GET['debug'])&&$_GET['debug']=='time'){
  114. print '<br /><span onclick="document.getElementById(\'sqldebug__'.$cnt.'\').style.display=document.getElementById(\'sqldebug__'.$cnt.'\').style.display!=\'block\'?\'block\':\'none\'">=</span>'.sprintf('%03d',$cnt).' - <strong>'.$T->timerStop().'</strong> - '.xHtmlSpecialChars($this->last_sql);
  115. ob_start();
  116. print_r(debug_backtrace());
  117. $_t=preg_replace('/(\[function\]\s+\=\>\s+)([^\n]+)/msi','$1<span style=color:red;>$2</span>','<pre>'.xHtmlSpecialChars(ob_get_contents()).'</pre>');
  118. ob_end_clean();
  119. ?>
  120. <div id="sqldebug__<?=$cnt;?>" style="display:none;">
  121. <?php
  122. echo $_t;
  123. ?>
  124. </div>
  125. <?php
  126. }
  127. /*
  128. @features
  129. */
  130. return new DBResource($this->result, $this->type, $this->link, $this->last_sql);
  131. }
  132. /**
  133. * place holder sintax based function
  134. * @return DBResource
  135. */
  136. function ph_query(){
  137. $args = func_get_args();
  138. $tmpl = array_shift($args);
  139. $error = '';
  140. $sql = sql_placeholder_ex($tmpl, $args, $error);
  141. if ($sql === false) $sql = PLACEHOLDER_ERROR_PREFIX.$error;
  142. return $this->query($sql);
  143. }
  144. function ph_fetch(){
  145. $args = func_get_args();
  146. $tmpl = array_shift($args);
  147. $error = '';
  148. $sql = sql_placeholder_ex($tmpl, $args, $error);
  149. if ($sql === false) $sql = PLACEHOLDER_ERROR_PREFIX.$error;
  150. return $sql;
  151. }
  152. /**
  153. * Enter description here...
  154. *
  155. * @return unknown
  156. */
  157. function fetch_row (){
  158. switch ($this->type){
  159. case 'mysql':
  160. return mysql_fetch_row($this->result);
  161. break;
  162. }
  163. }
  164. /**
  165. * Enter description here...
  166. *
  167. * @return unknown
  168. */
  169. function fetch_assoc (){
  170. switch ($this->type){
  171. case 'mysql':
  172. return mysql_fetch_assoc($this->result);
  173. break;
  174. }
  175. }
  176. /**
  177. * Enter description here...
  178. *
  179. * @return unknown
  180. */
  181. function num_rows (){
  182. switch ($this->type){
  183. case 'mysql':
  184. return mysql_num_rows($this->result);
  185. break;
  186. }
  187. }
  188. /**
  189. * Enter description here...
  190. *
  191. * @return unknown
  192. */
  193. function insert_id (){
  194. return mysql_insert_id($this->link);
  195. }
  196. function getInsertedID(){
  197. return $this->insert_id();
  198. }
  199. function createTableFromXMLObject(&$xmlTable){
  200. $xmlFields = $xmlTable->xPath('/table/field');
  201. $sql = 'CREATE TABLE `'.$xmlTable->getAttribute('name').'` (';
  202. $TC = count($xmlFields);
  203. for ($j=0;$j<$TC;$j++){
  204. $sql .= ($j?',':'').'`'.$xmlFields[$j]->getAttribute('name').'` '.$xmlFields[$j]->getAttribute('type').
  205. (!is_null($xmlFields[$j]->getAttribute('length'))?'('.$xmlFields[$j]->getAttribute('length').')':'').
  206. (!is_null($xmlFields[$j]->getAttribute('attribute'))?' '.$xmlFields[$j]->getAttribute('attribute'):'').
  207. (!is_null($xmlFields[$j]->getAttribute('null'))?' '.$xmlFields[$j]->getAttribute('null'):' not null').
  208. (!is_null($xmlFields[$j]->getAttribute('extra'))?' '.$xmlFields[$j]->getAttribute('extra'):'');
  209. }
  210. $xmlIndexes = $xmlTable->xPath('/table/index');
  211. $TC = count($xmlIndexes);
  212. for ($j=0;$j<$TC;$j++){
  213. $sql .= ','.$xmlIndexes[$j]->getAttribute('type').' `'.$xmlIndexes[$j]->getAttribute('name').'`('.$xmlIndexes[$j]->getAttribute('fields').')';
  214. }
  215. $sql .= ')';
  216. $this->query($sql);
  217. }
  218. function getAllTables(){
  219. $Result = $this->query('show tables');
  220. $Tables = array();
  221. while($_Row = $Result->fetchRow())$Tables[] = strtolower($_Row[0]);
  222. return $Tables;
  223. }
  224. function TableExists($_s_TableName){
  225. return $this->isTableCreated($_s_TableName);
  226. }
  227. function isTableCreated($_TableName){
  228. /* @var $Result DBResource */
  229. $Result = $this->query('SHOW TABLES LIKE "'.xEscapeSQLstring($_TableName).'"');
  230. if($Result->getNumRows())return true;
  231. else false;
  232. }
  233. function dropTable($_s_TableName){
  234. $this->query('DROP TABLE '.xEscapeSQLstring($_s_TableName));
  235. }
  236. function getServerVersion(){
  237. $result = $this->query('SHOW VARIABLES LIKE "VERSION"');
  238. $data = $result->fetchRow();
  239. if(isset($data[1])){
  240. return $data[1];
  241. }else return 0;
  242. }
  243. function affectedRows(){
  244. switch ($this->type){
  245. case 'mysql':
  246. return mysql_affected_rows($this->link);
  247. break;
  248. }
  249. }
  250. function printMessage($url,$msg = '')
  251. {
  252. if(sc_onWebasystServer()){
  253. header("HTTP/1.0 404 Not Found");
  254. die($msg);
  255. }
  256. print '<html><head><title>Error</title>
  257. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8;"></head><body>';
  258. if($msg) print "<p>{$msg}</p>";
  259. print '<br><b>Your online store is not yet installed.</b><br><br> To activate your installation simply <a href="'.$url.'">login to your WebAsyst account</a> &mdash; this will complete your storefront setup (if you have WebAsyst Shoping Cart application installed).';
  260. print '</body></html>';
  261. die;
  262. }
  263. function freeResult()
  264. {
  265. return mysql_free_result($this->link);
  266. }
  267. }
  268. ?>