BxDolUpgradeDb.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentUpgrade Trident Upgrade Script
  7. * @{
  8. */
  9. define('BX_UPGRADE_DB_FULL_VISUAL_PROCESSING', true);
  10. define('BX_UPGRADE_DB_FULL_DEBUG_MODE', true);
  11. class BxDolUpgradeDb
  12. {
  13. protected $_bErrorChecking = true;
  14. protected $_sHost, $_sPort, $_sSocket, $_sDbname, $_sUser, $_sPassword;
  15. protected $_rLink, $_rCurrentRes, $_iCurrentResType;
  16. /**
  17. * set database parameters and connect to it
  18. */
  19. function __construct()
  20. {
  21. $this->_sHost = BX_DATABASE_HOST;
  22. $this->_sPort = BX_DATABASE_PORT;
  23. $this->_sSocket = BX_DATABASE_SOCK;
  24. $this->_sDbname = BX_DATABASE_NAME;
  25. $this->_sUser = BX_DATABASE_USER;
  26. $this->_sPassword = BX_DATABASE_PASS;
  27. $this->_iCurrentResType = MYSQL_ASSOC;
  28. $this->connect();
  29. }
  30. /**
  31. * connect to database with appointed parameters
  32. */
  33. function connect()
  34. {
  35. $full_host = $this->_sHost;
  36. $full_host .= $this->_sPort ? ':'.$this->_sPort : '';
  37. $full_host .= $this->_sSocket ? ':'.$this->_sSocket : '';
  38. $this->_rLink = @mysql_pconnect($full_host, $this->_sUser, $this->_sPassword);
  39. if (!$this->_rLink)
  40. $this->error('Database connect failed', true);
  41. if (!$this->select_db())
  42. $this->error('Database select failed', true);
  43. $this->res("SET NAMES 'utf8'");
  44. $this->res("SET sql_mode = ''");
  45. }
  46. function select_db()
  47. {
  48. return @mysql_select_db($this->_sDbname, $this->_rLink) or $this->error('Cannot complete query (select_db)');
  49. }
  50. /**
  51. * close mysql connection
  52. */
  53. function close()
  54. {
  55. mysql_close($this->_rLink);
  56. }
  57. /**
  58. * execute sql query and return one row result
  59. */
  60. function getRow($query, $arr_type = MYSQL_ASSOC)
  61. {
  62. if(!$query)
  63. return array();
  64. if($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM && $arr_type != MYSQL_BOTH)
  65. $arr_type = MYSQL_ASSOC;
  66. $res = $this->res ($query);
  67. $arr_res = array();
  68. if($res && mysql_num_rows($res)) {
  69. $arr_res = mysql_fetch_array($res, $arr_type);
  70. mysql_free_result($res);
  71. }
  72. return $arr_res;
  73. }
  74. /**
  75. * execute sql query and return a column as result
  76. */
  77. function getColumn($sQuery)
  78. {
  79. if(!$sQuery)
  80. return array();
  81. $rResult = $this->res($sQuery);
  82. $aResult = array();
  83. if($rResult) {
  84. while($aRow = mysql_fetch_array($rResult, MYSQL_NUM))
  85. $aResult[] = $aRow[0];
  86. mysql_free_result($rResult);
  87. }
  88. return $aResult;
  89. }
  90. /**
  91. * execute sql query and return one value result
  92. */
  93. function getOne($query, $index = 0)
  94. {
  95. if(!$query)
  96. return false;
  97. $res = $this->res ($query);
  98. $arr_res = array();
  99. if($res && mysql_num_rows($res))
  100. $arr_res = mysql_fetch_array($res);
  101. if(count($arr_res))
  102. return $arr_res[$index];
  103. else
  104. return false;
  105. }
  106. /**
  107. * execute sql query and return the first row of result
  108. * and keep $array type and poiter to all data
  109. */
  110. function getFirstRow($query, $arr_type = MYSQL_ASSOC)
  111. {
  112. if(!$query)
  113. return array();
  114. if($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM)
  115. $this->_iCurrentResType = MYSQL_ASSOC;
  116. else
  117. $this->_iCurrentResType = $arr_type;
  118. $this->_rCurrentRes = $this->res ($query);
  119. $arr_res = array();
  120. if($this->_rCurrentRes && mysql_num_rows($this->_rCurrentRes))
  121. $arr_res = mysql_fetch_array($this->_rCurrentRes, $this->_iCurrentResType);
  122. return $arr_res;
  123. }
  124. /**
  125. * return next row of pointed last getFirstRow calling data
  126. */
  127. function getNextRow()
  128. {
  129. $arr_res = mysql_fetch_array($this->_rCurrentRes, $this->_iCurrentResType);
  130. if($arr_res)
  131. return $arr_res;
  132. else {
  133. mysql_free_result($this->_rCurrentRes);
  134. $this->_iCurrentResType = MYSQL_ASSOC;
  135. return array();
  136. }
  137. }
  138. /**
  139. * return number of affected rows in current mysql result
  140. */
  141. function getNumRows($res = false)
  142. {
  143. if ($res)
  144. return (int)@mysql_num_rows($res);
  145. elseif (!$this->_rCurrentRes)
  146. return (int)@mysql_num_rows($this->_rCurrentRes);
  147. else
  148. return 0;
  149. }
  150. /**
  151. * execute any query return number of rows affected/false
  152. */
  153. function getAffectedRows()
  154. {
  155. return mysql_affected_rows($this->_rLink);
  156. }
  157. /**
  158. * execute any query return number of rows affected/false
  159. */
  160. function query($query)
  161. {
  162. $res = $this->res($query);
  163. if($res)
  164. return mysql_affected_rows($this->_rLink);
  165. return false;
  166. }
  167. /**
  168. * execute any query
  169. */
  170. function res($query, $bErrorChecking = true)
  171. {
  172. if(!$query)
  173. return false;
  174. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginQuery($query);
  175. $res = mysql_query($query, $this->_rLink);
  176. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endQuery($res);
  177. if (!$res && $bErrorChecking)
  178. $this->error('Database query error', false, $query);
  179. return $res;
  180. }
  181. /**
  182. * execute sql query and return table of records as result
  183. */
  184. function getAll($query, $arr_type = MYSQL_ASSOC)
  185. {
  186. if(!$query)
  187. return array();
  188. if($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM && $arr_type != MYSQL_BOTH)
  189. $arr_type = MYSQL_ASSOC;
  190. $res = $this->res ($query);
  191. $arr_res = array();
  192. if($res) {
  193. while($row = mysql_fetch_array($res, $arr_type))
  194. $arr_res[] = $row;
  195. mysql_free_result($res);
  196. }
  197. return $arr_res;
  198. }
  199. /**
  200. * execute sql query and return table of records as result
  201. */
  202. function fillArray($res, $arr_type = MYSQL_ASSOC)
  203. {
  204. if(!$res)
  205. return array();
  206. if($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM && $arr_type != MYSQL_BOTH)
  207. $arr_type = MYSQL_ASSOC;
  208. $arr_res = array();
  209. while($row = mysql_fetch_array($res, $arr_type))
  210. $arr_res[] = $row;
  211. mysql_free_result($res);
  212. return $arr_res;
  213. }
  214. /**
  215. * execute sql query and return table of records as result
  216. */
  217. function getAllWithKey($query, $sFieldKey)
  218. {
  219. if(!$query)
  220. return array();
  221. $res = $this->res ($query);
  222. $arr_res = array();
  223. if($res) {
  224. while($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
  225. $arr_res[$row[$sFieldKey]] = $row;
  226. }
  227. mysql_free_result($res);
  228. }
  229. return $arr_res;
  230. }
  231. /**
  232. * execute sql query and return table of records as result
  233. */
  234. function getPairs($query, $sFieldKey, $sFieldValue)
  235. {
  236. if(!$query)
  237. return array();
  238. $res = $this->res ($query);
  239. $arr_res = array();
  240. if($res) {
  241. while($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
  242. $arr_res[$row[$sFieldKey]] = $row[$sFieldValue];
  243. }
  244. mysql_free_result($res);
  245. }
  246. return $arr_res;
  247. }
  248. function lastId()
  249. {
  250. return mysql_insert_id($this->_rLink);
  251. }
  252. function error($text, $isForceErrorChecking = false, $sSqlQuery = '')
  253. {
  254. if ($this->_bErrorChecking || $isForceErrorChecking)
  255. $this->genMySQLErr ($text, $sSqlQuery);
  256. else
  257. $this->log($text.': '.mysql_error($this->_rLink));
  258. }
  259. function listTables()
  260. {
  261. return mysql_list_tables($GLOBALS['db']['db'], $this->_rLink);
  262. }
  263. function getEncoding()
  264. {
  265. return mysql_client_encoding($this->_rLink) or $this->error('Database get encoding error');
  266. }
  267. function genMySQLErr( $out, $query ='' )
  268. {
  269. $aBackTrace = debug_backtrace();
  270. unset( $aBackTrace[0] );
  271. if( $query ) {
  272. //try help to find error
  273. $aFoundError = array();
  274. foreach( $aBackTrace as $aCall ) {
  275. foreach( $aCall['args'] as $argNum => $argVal ) {
  276. if( is_string($argVal) and strcmp( $argVal, $query ) == 0 ) {
  277. $aFoundError['file'] = $aCall['file'];
  278. $aFoundError['line'] = $aCall['line'];
  279. $aFoundError['function'] = $aCall['function'];
  280. $aFoundError['arg'] = $argNum;
  281. }
  282. }
  283. }
  284. if( $aFoundError ) {
  285. $sFoundError = <<<EOJ
  286. Found error in the file '<b>{$aFoundError['file']}</b>' at line <b>{$aFoundError['line']}</b>.<br />
  287. Called '<b>{$aFoundError['function']}</b>' function with erroneous argument #<b>{$aFoundError['arg']}</b>.<br /><br />
  288. EOJ;
  289. }
  290. }
  291. if( BX_UPGRADE_DB_FULL_VISUAL_PROCESSING ) {
  292. ?>
  293. <div style="border:2px solid red;padding:4px;width:600px;margin:0px auto;">
  294. <div style="text-align:center;background-color:red;color:white;font-weight:bold;">Error</div>
  295. <div style="text-align:center;"><?php echo $out?></div>
  296. <?php
  297. if( BX_UPGRADE_DB_FULL_DEBUG_MODE ) {
  298. if( strlen( $query ) )
  299. echo "<div><b>Query:</b><br />{$query}</div>";
  300. echo '<div><b>Mysql error:</b><br />'.mysql_error($this->_rLink).'</div>';
  301. echo '<div style="overflow:scroll;height:300px;border:1px solid gray;">';
  302. echo $sFoundError;
  303. echo "<b>Debug backtrace:</b><br />";
  304. echoDbg( $aBackTrace );
  305. echo "<b>Called script:</b> {$_SERVER['PHP_SELF']}<br />";
  306. echo "<b>Request parameters:</b><br />";
  307. echoDbg( $_REQUEST );
  308. echo '</div>';
  309. }
  310. ?>
  311. </div>
  312. <?php
  313. } else
  314. echo $out;
  315. exit;
  316. }
  317. function setErrorChecking ($b)
  318. {
  319. $this->_bErrorChecking = $b;
  320. }
  321. function escape ($s)
  322. {
  323. return mysql_real_escape_string($s);
  324. }
  325. function prepare ($sQuery)
  326. {
  327. $aArgs = func_get_args();
  328. $sQuery = array_shift($aArgs);
  329. $iPos = 0;
  330. foreach ($aArgs as $mixedArg) {
  331. if (is_null($mixedArg))
  332. $s = 'NULL';
  333. elseif (is_numeric($mixedArg))
  334. $s = $mixedArg;
  335. else
  336. $s = "'" . mysql_real_escape_string($mixedArg) . "'";
  337. $i = bx_mb_strpos($sQuery, '?', $iPos);
  338. $sQuery = bx_mb_substr_replace($sQuery, $s, $i, 1);
  339. $iPos = $i + get_mb_len($s);
  340. }
  341. return $sQuery;
  342. }
  343. function executeSQL($sPath, $aReplace = array (), $isBreakOnError = true)
  344. {
  345. if(!file_exists($sPath) || !($rHandler = fopen($sPath, "r")))
  346. return array ('query' => "fopen($sPath, 'r')", 'error' => 'file not found or permission denied');
  347. $sQuery = "";
  348. $sDelimiter = ';';
  349. $aResult = array();
  350. while(!feof($rHandler)) {
  351. $sStr = trim(fgets($rHandler));
  352. if(empty($sStr) || $sStr[0] == "" || $sStr[0] == "#" || ($sStr[0] == "-" && $sStr[1] == "-"))
  353. continue;
  354. //--- Change delimiter ---//
  355. if(strpos($sStr, "DELIMITER //") !== false || strpos($sStr, "DELIMITER ;") !== false) {
  356. $sDelimiter = trim(str_replace('DELIMITER', '', $sStr));
  357. continue;
  358. }
  359. $sQuery .= $sStr;
  360. //--- Check for multiline query ---//
  361. if(substr($sStr, -strlen($sDelimiter)) != $sDelimiter)
  362. continue;
  363. //--- Execute query ---//
  364. if ($aReplace)
  365. $sQuery = str_replace($aReplace['from'], $aReplace['to'], $sQuery);
  366. if($sDelimiter != ';')
  367. $sQuery = str_replace($sDelimiter, "", $sQuery);
  368. $rResult = $this->res(trim($sQuery), false);
  369. if(!$rResult) {
  370. $aResult[] = array('query' => $sQuery, 'error' => mysql_error($this->_rLink));
  371. if ($isBreakOnError)
  372. break;
  373. }
  374. $sQuery = "";
  375. }
  376. fclose($rHandler);
  377. return empty($aResult) ? true : $aResult;
  378. }
  379. }
  380. /** @} */