Guest User

Untitled

a guest
Apr 16th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.32 KB | None | 0 0
  1. <?php
  2.  
  3. require_once(dirname(__FILE__) . '/../../conf/paths.inc.php');
  4. require_once(dirname(__FILE__) . '/core/MySQLDatabase.class.php');
  5. require_once(dirname(__FILE__) . '/core/QueryIterator.class.php');
  6. require_once(dirname(__FILE__) . '/../constants.inc.php');
  7.  
  8. define('NEW_LINE', '\n');
  9. define('BREAK', '<br />');
  10. define('RECORD_ID_FIELD_NAME', 'record_id_field_name');
  11. define('EMPTY_DB_DATE', '0000-00-00');
  12.  
  13. class BaseDao {
  14.  
  15. var $conn = null;
  16.  
  17. function BaseDao() {
  18. $this->connect();
  19. }
  20.  
  21.  
  22. function getRows($sql, $params, $test=false){
  23. if(!is_array($params)){
  24. $params = array($params);
  25. }
  26. $this->prepare($sql, $params);
  27. if($test){
  28. Util::doTest(array($sql, $params));
  29. }
  30. if(!$this->getConnected()){
  31. $this->connect();
  32. }
  33. $queryResult = $this->conn->query($sql);
  34. $it = new QueryIterator($queryResult);
  35.  
  36. $rows = array();
  37. while ($it->hasNext()) {
  38. $row = $it->next();
  39. $rows[] = $row;
  40. }
  41. return $rows;
  42. }
  43.  
  44.  
  45. function getListCount($where, $field=null){
  46. $this->connect();
  47.  
  48. $field = (null != $field) ? $field : 'ID';
  49. $sql = 'select '
  50. . ' count('.$field.') as retval '
  51. . $where
  52. . '';
  53. $queryResult = $this->conn->query($sql);
  54. $it = new QueryIterator($queryResult);
  55.  
  56. while ($it->hasNext()) {
  57. $row = $it->next();
  58. return $row['retval'];
  59. }
  60. }
  61.  
  62.  
  63. function buildListFromQuery($sql, $object){
  64. $queryResult = $this->conn->query($sql);
  65. $it = new QueryIterator($queryResult);
  66. $objectName = ucfirst(get_class($object));
  67.  
  68. $array = array();
  69. while ($it->hasNext()) {
  70. $row = $it->next();
  71. $object = new $objectName();
  72. BeanMapper::populateBean($object, $row);
  73. $array[] = $object;
  74. }
  75.  
  76. return $array;
  77. }
  78.  
  79.  
  80. function connect() {
  81. if (!class_exists('BootstrapConfig')) {
  82. require_once(dirname(__FILE__) . '/SimpleConfig.class.php');
  83. }
  84.  
  85. $bootstrap = BootstrapConfig::getInstance();
  86.  
  87. if (null == $this->conn || !$this->conn->isConnected()) {
  88. require_once(dirname(__FILE__) . '/Util.class.php');
  89. $dbHost = $bootstrap->getValue(DB_HOST);
  90. $userName = $bootstrap->getValue(DB_USER);
  91. $password = $bootstrap->getValue(DB_PASS);
  92. $dbName = $bootstrap->getValue(DB_NAME);
  93.  
  94. $this->conn =& MySQLDatabase::getInstance($dbName, $dbHost);
  95.  
  96. if (!$this->conn->connect($userName, $password)) {
  97. trigger_error($this->conn->getError(), E_USER_ERROR);
  98. }
  99. }
  100. }
  101.  
  102.  
  103. function disconnect() {
  104. $this->conn->disconnect();
  105. }
  106.  
  107.  
  108. function buildList($arr) {
  109. $first = true;
  110. foreach ($arr as $val) {
  111. if (!$first) {
  112. $sql .= ',';
  113. }
  114.  
  115. $sql = $sql . "'" . $val . "'";
  116.  
  117. $first = false;
  118. }
  119.  
  120. return $sql;
  121. }
  122.  
  123.  
  124. function setString($sql, $val) {
  125. if (!get_magic_quotes_gpc()) {
  126. // This is for things like "c:\test"
  127. $val = preg_replace("/\\\\/", "\\\\\\", $val);
  128. }
  129.  
  130. $tempval = $val;
  131.  
  132. // This escapes $ in insert values. preg_replace thinks it's a backreference otherwise.
  133. $val = preg_replace('/\$/i', '\\\\$', $tempval);
  134. $val = '\'' . $val . '\'';
  135. $sql = preg_replace('/\?/', $val, $sql, 1);
  136.  
  137. return $sql;
  138. }
  139.  
  140.  
  141. function setNumber($sql, $val) {
  142. if (is_numeric($val)) {
  143. $sql = preg_replace("/\?/", $val, $sql, 1);
  144. }
  145.  
  146. return $sql;
  147. }
  148.  
  149.  
  150. function setNull($sql) {
  151. $sql = preg_replace("/\?/", "null", $sql, 1);
  152.  
  153. return $sql;
  154. }
  155.  
  156.  
  157. function setString2($sql, $val) {
  158.  
  159. if (!get_magic_quotes_gpc()) {
  160. // This is for things like "c:\test"
  161. $val = preg_replace("/\\\\/", "\\\\\\", $val);
  162. }
  163.  
  164. $tempval = $val;
  165.  
  166. // This escapes $ in insert values. preg_replace thinks it's a backreference otherwise.
  167. $val = preg_replace('/\$/i', '\\\\$', $tempval);
  168. $val = '\'' . $val . '\'';
  169. $sql = preg_replace('/\|/', $val, $sql, 1);
  170.  
  171. return $sql;
  172. }
  173.  
  174.  
  175. function setNull2($sql) {
  176. $sql = preg_replace("/\|/", "null", $sql, 1);
  177.  
  178. return $sql;
  179. }
  180.  
  181.  
  182. function prepare2(&$sql, $paramsArr) {
  183. $isArray = is_array($paramsArr);
  184. $isEmptyArray = (0 == count($paramsArr));
  185.  
  186. if (!$isArray || $isEmptyArray) {
  187. return;
  188. }
  189.  
  190. $localSql = $sql;
  191.  
  192. $counter = 0;
  193. for ($i=0; $i<strlen($sql); $i++) {
  194. if ('|' == $sql[$i]) {
  195. if (null === $paramsArr[$counter]) {
  196. $localSql = $this->setNull2($localSql);
  197. // PHP autoconverts strings to numbers, so a string like 08000 is recognized
  198. // as the number 8000. This is not what we want in the case of zips and elsewhere.
  199. /*} elseif (is_numeric($paramsArr[$counter])) {
  200. $localSql = $this->setNumber($localSql, $paramsArr[$counter]);*/
  201. } else {
  202.  
  203. $paramsArr[$counter] = str_replace('|', '', $paramsArr[$counter]);
  204. if (get_magic_quotes_gpc()) {
  205. $fieldValue = stripslashes($paramsArr[$counter]);
  206. }
  207.  
  208. $fieldValue = Util::mysql_real_escape_string($paramsArr[$counter]);
  209.  
  210. $localSql = $this->setString2($localSql, $fieldValue);
  211. }
  212.  
  213. $counter++;
  214. }
  215. }
  216.  
  217.  
  218. $sql = $localSql;
  219. }
  220.  
  221.  
  222. /**
  223. * Right now it just replaces ? w the corresponding value in the $paramsArr.
  224. */
  225. function prepare(&$sql, $paramsArr) {
  226. return $this->prepare3($sql, $paramsArr);
  227. }
  228.  
  229.  
  230. function createTempData($token, $val, $key=null) {
  231. $sql = 'insert into ' .
  232. ' se_session ' .
  233. ' ( ' .
  234. ' token ' .
  235. ' ,`key` ' .
  236. ' ,val ' .
  237. ' ,date ' .
  238. ' ) ' .
  239. ' VALUES ' .
  240. ' (?, ?, ?, sysdate()) ';
  241.  
  242. $this->connect();
  243.  
  244. $params = array();
  245. $params[] = $token;
  246. $params[] = $key;
  247. $params[] = $val;
  248.  
  249. $this->prepare3($sql, $params);
  250. return $this->conn->query($sql);
  251. }
  252.  
  253.  
  254. function retrieveTempData($token, $key=null) {
  255. require_once(dirname(__FILE__) . '/core/QueryIterator.class.php');
  256.  
  257. $sql = ' select distinct val ' .
  258. ' from se_session ' .
  259. ' where token = ? ';
  260.  
  261. if (isset($key) && $key) {
  262. $sql .= ' and `key` = ? ';
  263. }
  264.  
  265. $params[] = $token;
  266.  
  267. if (isset($key) && $key) {
  268. $params[] = $key;
  269. }
  270.  
  271. $this->connect();
  272. $this->prepare3($sql, $params);
  273.  
  274. $queryResult = $this->conn->query($sql);
  275. $it = new QueryIterator($queryResult);
  276.  
  277. $retVal = null;
  278. while ($it->hasNext()) {
  279. $row = $it->next();
  280. $retVal = $row['val'];
  281. }
  282.  
  283. return $retVal;
  284. }
  285.  
  286.  
  287. function updateTempData($token, $val, $key=null) {
  288. $this->connect();
  289.  
  290. $sql = ' update se_session ' .
  291. ' set val = ? ' .
  292. ' where token = ? ';
  293.  
  294. $data[] = $val;
  295. $data[] = $token;
  296.  
  297. if (isset($key) && $key) {
  298. $sql .= ' and `key` = ? ';
  299. $data[] = $key;
  300. }
  301.  
  302. $this->prepare($sql, $data);
  303. return $this->conn->query($sql);
  304. }
  305.  
  306.  
  307. /**
  308. * Gets the id of the record based on the unique parameters in the given sql.
  309. */
  310. function getRecordId($sql, $params) {
  311. $this->connect();
  312. $this->prepare($sql, $params);
  313. $queryResult = $this->conn->query($sql);
  314.  
  315. $id = null;
  316. if ($queryResult && 0 < $queryResult->getRowCount()) {
  317. require_once(dirname(__FILE__) . '/core/QueryIterator.class.php');
  318. $it = new QueryIterator($queryResult);
  319.  
  320. while ($it->hasNext()) {
  321. $row = $it->next();
  322. $id = $row[RECORD_ID_FIELD_NAME];
  323. }
  324. }
  325.  
  326. return $id;
  327. }
  328.  
  329.  
  330. function basicQuery($sql, $params=null, $test=false) {
  331. $this->connect();
  332. $this->prepare($sql, $params);
  333.  
  334. if ($test) {
  335. print $sql;
  336. exit;
  337. }
  338.  
  339. $succeeded = $this->conn->query($sql);
  340.  
  341. if (is_bool($succeeded) && !$succeeded) {
  342. trigger_error('Database Action Failed', E_USER_ERROR);
  343. //require_once(dirname(__FILE__) . '/Exception.class.php');
  344. //require_once('lib/constants.errors.inc.php');
  345. //Exception::throw(DATABASE_ACTION_FAILED);
  346. }
  347.  
  348. return $succeeded;
  349. }
  350.  
  351.  
  352. function unlockTables() {
  353. global $config;
  354.  
  355. if (!($config->getBoolValue('lockTables'))) {
  356. return;
  357. }
  358.  
  359. $sql = 'UNLOCK TABLES';
  360. $this->conn->query($sql);
  361. }
  362.  
  363.  
  364. function lockTable($table) {
  365. global $config;
  366.  
  367. if (!($config->getBoolValue('lockTables'))) {
  368. return;
  369. }
  370.  
  371. if ($table) {
  372. $this->connect();
  373. $sql = "LOCK TABLE $table WRITE";
  374. $this->conn->query($sql);
  375. }
  376. }
  377.  
  378.  
  379. /*
  380. * The prepare3 function is the same as the prepare function in that it takes a string with ?'s and
  381. * replaces them with the corresponding values in the paramsArr. It is unlike prepare in that if
  382. * the paramsArr contains a param with a ? in it that gets left alone and it will not replace the
  383. * params ? with the next param. This basically means that you can dump any characters into here
  384. * and it will work fine.
  385. */
  386. function prepare3(&$sql, $paramsArr, $leaveChars=null, $replaceChar=null, $test=null) {
  387. $length = strlen($sql);
  388. $markers = array();
  389. $current = 0;
  390. $replaceChar = $replaceChar ? $replaceChar : '?';
  391.  
  392. for($i=0;$i<$length;$i++){
  393. if($sql[$i] == $replaceChar){
  394. $skipQuotes = ($paramsArr[$current] && is_array($paramsArr[$current])) ? $paramsArr[$current][1] : null;
  395. $paramsArr[$current] = ($paramsArr[$current] && is_array($paramsArr[$current])) ? $paramsArr[$current][0] : $paramsArr[$current];
  396.  
  397. if(!$leaveChars){
  398. if (get_magic_quotes_gpc()) {
  399. $paramsArr[$current] = stripslashes($paramsArr[$current]);
  400. }
  401.  
  402. $value = addslashes($paramsArr[$current]);
  403. } else {
  404. $value = $paramsArr[$current];
  405. }
  406.  
  407. $value = !isset($value) ? 'null' : $value;
  408. $value = $value != 'null' ? ($skipQuotes ? $value : '\'' . $value . '\'') : $value;
  409.  
  410. $markers[$i] = array('length' => strlen($value) - 1, 'value' => $value);
  411. $current++;
  412. }
  413. }
  414.  
  415. if($markers && is_array($markers) && count($markers) > 0){
  416. $previousLength = 0;
  417.  
  418. foreach($markers as $start => $marker){
  419. $sql = substr_replace($sql, $marker['value'], $start + $previousLength, 1);
  420.  
  421. $previousLength += $marker['length'];
  422. }
  423. }
  424. }
  425.  
  426. function setConnected($value) {$this->connected = $value;}
  427. function getConnected() {return $this->connected;}
  428. }
  429.  
  430. ?>
Add Comment
Please, Sign In to add comment