Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.28 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. *
  5. * Version: 1.1.10
  6. *
  7. *
  8. *
  9. */
  10.  
  11. class mySQL_class {
  12.  
  13. protected $db = array();
  14. public $current_key;
  15. private $last_key;
  16. private $key = 'key_';
  17. public $q = array(0 => NULL);
  18. public $qs = array(0 => NULL);
  19. public $nr = 0;
  20. public $connect_die = true;
  21. public $time = 0;
  22. public $charset = NULL;
  23. public $DEBUG = false;
  24.  
  25. public $host = 'localhost';
  26. public $user = 'root';
  27. public $pass = '';
  28.  
  29. public function add_db($key, $db = false, $host = false, $user = false, $pass = false, $charset = false) {
  30. if (!$db) { $db = $key; }
  31. if (!$host) { $host = $this->host; }
  32. if (!$user) { $user = $this->user; }
  33. if (!$pass) { $pass = $this->pass; }
  34.  
  35. $a['host'] = $host;
  36. $a['user'] = $user;
  37. $a['pass'] = $pass;
  38. $a['db'] = $db;
  39. $a['charset'] = $charset;
  40. $this->db[$key] = $a;
  41. return true;
  42. }
  43.  
  44. private function script_error($error, $line, $file, $die) {
  45. $html = "<b>Error:</b> $error<br /><b>Line:</b> $line<br /><b>File:</b> $file";
  46. if ($this->DEBUG) {
  47. die($html);
  48. }
  49. if ($die) {
  50. die('Script Error, MySQL_Class');
  51. }
  52. }
  53.  
  54. public function connect($key = NULL, $reconnect = false, $die = true) {
  55. if ($key) {
  56. if ($this->db[$key]['connected'] AND !$reconnect) { return false; }
  57. if ($c = $this->db[$key]) { return $this->_connect($key, $c['host'], $c['user'], $c['pass'], $c['charset'], $die); }
  58. return false;
  59. }
  60. foreach ($this->db AS $key => $info) {
  61. if (!$info['connected'] OR $reconnect) {
  62. $this->_connect($key, $info['host'], $info['user'], $info['pass'], $info['charset'], $die);
  63. }
  64. }
  65. return true;
  66. }
  67.  
  68. public function _connect ($key, $host, $user, $pass, $charset = NULL, $die = true) {
  69.  
  70. $n = $this->key.$key;
  71. if ($this->$n) { $this->close($key); }
  72. //$this->$n = new mysqli($host, $user, $pass);
  73. $this->$n = @mysql_connect($host, $user, $pass);
  74. if ($this->$n) {
  75. $this->db[$key]['connected'] = true;
  76. if ($charset = strtolower($charset ? $charset : $this->charset)) {
  77. switch($charset) {
  78. case 'utf8':
  79. case 'utf-8':
  80. $qs1 = "SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'";
  81. $qs2 = "SET CHARACTER SET utf8";
  82. break;
  83. case 'iso-8859-1':
  84. case 'latin1':
  85. $qs1 = "SET NAMES 'latin1'";
  86. $qs2 = "SET CHARACTER SET latin1";
  87.  
  88. break;
  89. default:
  90. $qs1 = "SET NAMES '".$charset."'";
  91. $qs2 = "SET CHARACTER SET $charset";
  92. break;
  93. }
  94. mysql_db_query($qs1, $this->$n);
  95. mysql_db_query($qs2, $this->$n);
  96. }
  97. return true;
  98. }
  99.  
  100. $this->db[$key]['connected'] = false;
  101. if ($this->DEBUG AND $die) {
  102. $err_mes = 'Cannot connect to <i>'.$host.'</i> with username <i>'.$user.'</i>';
  103. if ($pass) { $err_mes .= ' and password given'; }
  104. else { $err_mes .= ' uten passord.'; }
  105. $err_mes .= '<br /><b>Mysql Error:</b> '.mysql_error();
  106. $this->script_error($err_mes, __LINE__, __FILE__, true);
  107. }
  108. return false;
  109. }
  110.  
  111. public function close ($key) {
  112. $n = $this->key.$key;
  113. $this->db[$key]['connected'] = false;
  114. $v = mysql_close($this->$n);
  115. $this->$n = NULL;
  116. return $v;
  117. }
  118.  
  119. public function set_key($key, $connect = false, $reconnect = false) {
  120. if ($this->db[$key]) {
  121. if ((!$this->db[$key]['connected'] AND $connect) OR $reconnect) { $this->connect($key, $reconnect); }
  122. $this->current_key = $key;
  123. return $key;
  124. }
  125. else { return false; }
  126. }
  127.  
  128. public function get_key(&$key = false, $connect = false, $line = __LINE__, $file = __FILE__) {
  129. if ($key === false) { $key = ''; }
  130. if (!is_string($key)) {
  131. if ($this->DEBUG) { $this->script_error('Kan kan bare vare <i>string</b>. Input er <i>'.gettype($key).'</i>.', $line, $file, true); }
  132. return false;
  133. }
  134. if ($this->db[$key]) {}
  135. elseif ($this->current_key) { $key = $this->current_key; }
  136. elseif ($this->last_key) { $key = $this->last_key; /*$this->set_key($key, $connect, $connect_die);*/ }
  137. else { $key = key($this->db); }
  138.  
  139. $this->last_key = $key;
  140. if (!$this->db[$key]['connected']) {
  141. if ($connect) { $connect = $this->connect($key, true); }
  142. else { $key = false; }
  143. }
  144. return $key;
  145. }
  146.  
  147. public function get($qs, $key = false, $d = false, $unike_key = false) {
  148.  
  149. $q = $this->_query($qs, $key, __LINE__, __FILE__);
  150.  
  151.  
  152. if ($error = $this->error($key, $line, $file)) {
  153. if ($this->DEBUG) { die($error); }
  154. return false;
  155. }
  156.  
  157. if (@get_resource_type($q) == 'mysql result' AND @mysql_num_rows($q)) {
  158. return $this->result($q, $d, $unike_key);
  159. }
  160.  
  161. return false;
  162. }
  163.  
  164. public function query($sql, $key = false, &$errors = array(), $line = __LINE__, $file = __FILE__) {
  165.  
  166. $sql_ = $sql;
  167.  
  168. $connected = true;
  169. $key = $this->get_key($key, &$connected, $line, $file);
  170. if (!$connected) { return false; }
  171. $n = $this->key.$key;
  172.  
  173. $sql = $this->remove_remarks($sql);
  174. $sql = $this->split_sql_file($sql, ';');
  175. //die('<pre>'.print_r($sql, 1).'</pre>');
  176. if (!count($sql)) { return false; }
  177.  
  178. foreach ($sql AS $id => $qs) {
  179. if (trim($qs) != '') {
  180. $q = @mysql_db_query($this->db[$key]['db'], $qs, $this->$n);
  181. if ($e = mysql_error() AND $e != 'Query was empty') { $errors[$id] = $e; }
  182. }
  183. }
  184. if (!count($errors)) { return true; }
  185. if (!$this->DEBUG) { return false; }
  186.  
  187. $mld = '<h1>MySQL Errors</h1>';
  188. foreach ($errors AS $id => $error) {
  189. $mld .= '<div style="background-color:#ccc; padding: 1px; border: 1px dotted #000;"><p>'.$error.'</p><p>'.$sql[$id].'</p></div>';;
  190. }
  191. $mld .= '<br /><fieldset><legend>Hele SQL</legend><pre>'.$sql_.'</fieldset>';
  192. die($mld);
  193.  
  194. return false;
  195.  
  196. }
  197. public function q($qs, $key = false, $line = __LINE__, $file = __FILE__) {
  198.  
  199. $qs = trim($qs);
  200. $q = $this->_query($qs, $key, $line, $file);
  201. if ($error = $this->error($key, $line, $file)) {
  202. if ($this->DEBUG) { die($error); }
  203. return false;
  204. }
  205. if (@get_resource_type($q) == 'mysql result' AND @mysql_num_rows($q)) {
  206. return $q;
  207. }
  208. elseif (is_numeric($q)) {
  209. if ($this->q[$q]['insert_id']) { return $this->q[$q]['insert_id']; }
  210. }
  211. elseif (strtoupper(substr($qs, 0, 6)) == 'SELECT') { return false; }
  212. return true;
  213. //else { return $this->q[$q]; }
  214. }
  215.  
  216. // IKKE tilrettelagt for nytt system, heler ikke ferdig
  217. public function select($hva = '*', $from = false, $where = false, $sort = false, $fra = '0', $antall = false, $distinct = false, $key = false) {
  218. if ($distinct) { $distinct == 'DISTINCT'; }
  219. if (!$from) { $from = $this->select_from; }
  220. $this->select_from = $from;
  221.  
  222. // Hva skal hentes
  223. if (is_array($hva)) {
  224. foreach ($hva AS $as => $n) {
  225. if (is_array($n)) {
  226. if (is_numeric($as)) { $as = 'make_'.$as; }
  227. foreach ($n AS $as1 => $n1) {
  228. $h .= $as.'.`'.$n1.'`';
  229. if (!is_numeric($as1)) { $h .= ' AS '.$as1; }
  230. $h .= ', ';
  231. }
  232. }
  233. else {
  234. //if (is_numeric($as)) { $as = 'make_'.$as; }
  235. $h .= '`'.$n.'`, ';
  236. }
  237. }
  238. $h = substr($h, 0, -2);
  239. }
  240. else { $h = $hva;}
  241.  
  242.  
  243. $qs = sprintf('SELECT %s %s FROM %s', $distinct, $h, $from);
  244.  
  245. // WHERE
  246. if (is_array($where)) {
  247. foreach ($where AS $a => $b) {
  248. if (is_array($b)) {
  249. if (is_numeric($a)) { $a = 'make_'.$a; }
  250. foreach ($b AS $c => $d) {
  251. $w .= $a.'.`'.$c.'` = '.$this->quote_smart($d);
  252. $w .= ' AND ';
  253. }
  254. $w = substr($w, 0, -4);
  255. }
  256. elseif (is_numeric($a)) {
  257. $w .= $b;
  258. }
  259. else {
  260. $w .= '`'.$a.'` = '.$this->quote_smart($b);
  261. }
  262. $w .= ' AND ';
  263. }
  264. $w = substr($w, 0, -4);
  265. }
  266. else { $w = $where;}
  267. if ($w) { $qs .= ' WHERE '.$w; }
  268.  
  269. // Sortering
  270. if (is_array($sort)) {
  271. foreach ($sort AS $n) {
  272. $s .= '`'.$n.'`, ';
  273. }
  274. $s = substr($s, 0, -2);
  275. }
  276. else { $s = $sort; }
  277. if ($s) { $qs .= 'ORDER BY '.$s; }
  278.  
  279. if (is_numeric($fra) AND $fra > 0) {
  280. $qs .= 'LIMIT '.$fra;
  281. if (is_numeric($antall) AND $antall > 0) { $qs .= ', '.$antall; }
  282. }
  283.  
  284. //return $qs;
  285. //echo $qs.'<br />';
  286.  
  287. $q = $this->q($qs, $key);
  288. $q_ = $this->last_q();
  289. $this->select_antall = $q['antall'];
  290. return $q;
  291. }
  292.  
  293. public function insert($tabel = false, $v1 = false, $v2 = false, $key = false, $r_id = false, $line = __LINE__, $file = __FILE__) {
  294. if (!$tabel) { $tabel = $this->insert_tabel; }
  295. $this->insert_tabel = $tabel;
  296.  
  297. $qs = 'INSERT INTO `'.$tabel.'`';
  298. if (is_string($v1)) {
  299. if (is_string($v2)) { $qs .= '('.$v1.') VALUES ('.$v2.')'; } // Setter "($v1) VALUES ($v2)"
  300. elseif (is_array($v2)) { // Hvis v1 er tekst og v2 er array
  301. $qs .= '('.$v1.') VALUES ';
  302. foreach ($v2 AS $v) { $qs .= '('.$v.'), '; }
  303. $qs = substr($qs, 0, -2);
  304. }
  305. else { $this->insert_error = 'Feil argumenter'; return false; }
  306. }
  307. elseif (is_array($v1)) {
  308. if (is_array($v2)) { $v1 = array_combine($v1, $v2); $v2 = false; }
  309. if (!$v2) {
  310. $qs .= 'SET ';
  311. foreach ($v1 AS $k => $v) { if ($k AND $v) { $qs .= $k.' = '.$v.', '; } }
  312. $qs = substr($qs, 0, -2);
  313. }
  314. else { $this->insert_error = 'Feil argumenter'; return false; }
  315. }
  316.  
  317. $this->insert_qs = $qs;
  318. $this->insert_error = false;
  319. $qid = $this->_query($qs, $key, $line, $file);
  320. $q = $this->q[$qid];
  321.  
  322. if ($r_id) { return $q['id']; }
  323. elseif ($q['error']) { $this->insert_error = $q['error']; return false; }
  324. else { return $q['insert_id']; }
  325. }
  326.  
  327. public function update($tabel = false, $set = false, $where = false, $key = false, $r_id = false, $line = __LINE__, $file = __FILE__) {
  328. if (!$tabel) { $tabel = $this->update_tabel; }
  329. $this->update_tabel = $tabel;
  330.  
  331. $qs = 'UPDATE `'.$tabel.'` SET ';
  332. if (is_string($set)) {
  333. $qs .= $set;
  334. }
  335. elseif (is_array($set)) {
  336. foreach ($set AS $k => $v) { if ($k AND $v) { $qs .= $k.' = '.$v.', '; } }
  337. $qs = substr($qs, 0, -2);
  338. }
  339.  
  340. // WHERE
  341. if (is_array($where)) {
  342. foreach ($where AS $a => $b) {
  343. if (is_numeric($a)) {
  344. $w .= $b;
  345. }
  346. else {
  347. $w .= '`'.$a.'` = '.$b;
  348. }
  349. $w .= ' AND ';
  350. }
  351. $w = substr($w, 0, -4);
  352. }
  353. else { $w = $where;}
  354. if ($w) { $qs .= ' WHERE '.$w; }
  355.  
  356. $this->update_qs = $qs;
  357. $this->update_error = false;
  358. $qid = $this->_query($qs, $key, $line, $file);
  359. $q = $this->q[$qid];
  360.  
  361. if ($r_id) { return $q['id']; }
  362. elseif ($q['error']) { $this->update_error = $q['error']; return false; }
  363. else { return true; }
  364. }
  365.  
  366. protected function _query ($qs, $key, $line, $file) {
  367. $connected = true;
  368. $key = $this->get_key($key, &$connected, $line, $file);
  369. if (!$connected) { return false; }
  370. $n = $this->key.$key;
  371.  
  372.  
  373. $id = count($this->q);
  374. $this->qs[] = $qs = trim($qs);
  375.  
  376.  
  377. $time_start = $this->getmicrotime();
  378.  
  379. $this->last_q = $this->{'q'.$id} = $q = @mysql_db_query($this->db[$key]['db'], $qs, $this->$n);
  380.  
  381. $time_end = $this->getmicrotime();
  382. $time = $time_end - $time_start;
  383. $this->time = $this->time+$time;
  384.  
  385. $this->nr++;
  386.  
  387.  
  388. $this->q[$id]['id'] = $this->last_id = $id;
  389. $this->q[$id]['key'] = $key;
  390. $this->q[$id]['qs'] = $qs;
  391. $this->q[$id]['time'] = $time;
  392. $this->q[$id]['line'] = $line;
  393. $this->q[$id]['file'] = $file;
  394.  
  395.  
  396.  
  397. $c = strtoupper(substr($qs, 0, strpos($qs, ' ')));
  398.  
  399. if ($error = mysql_error($this->$n)) {
  400. $this->q[$id]['error'] = $error;
  401. $this->q[$id]['errno'] = mysql_errno($this->$n);
  402.  
  403. return $id;
  404. }
  405. elseif (in_array($c, array('SELECT', 'SHOW'))) {
  406. $this->q[$id]['antall'] = @mysql_num_rows($q);
  407. return $q;
  408. }
  409. elseif (in_array($c, array('INSERT', 'UPDATE', 'DELETE'))) {
  410. $this->q[$id]['insert_id'] = mysql_insert_id($this->$n);
  411.  
  412. return $id;
  413. }
  414. else {
  415. return $q;
  416. }
  417.  
  418. /*
  419.  
  420. $n = 'q'.$id;
  421. $this->$n = $q;
  422. end($this->q);
  423. //echo 'Key:'.key($this->q).'<br />';
  424. return $q;
  425. return key($this->q);
  426.  
  427. */
  428. }
  429.  
  430.  
  431. // Convert mySQL results to a string
  432. // New in version 1.1.10
  433. public function getString(&$q) {
  434. if (@get_resource_type($q) == 'mysql result') {
  435. return $q = mysql_result($q, 0);
  436. }
  437. return $q;
  438. }
  439.  
  440. // Convert mysql results to an array
  441. // Ny i version 1.1.7
  442. // Version 1.1.10: Renamed from toArray to getArray
  443. public function getArray(&$q, $key = false, $value = false) {
  444. $re = array();
  445.  
  446.  
  447. switch(true) {
  448. case is_string($q):
  449. case is_numeric($q):
  450. default:
  451. $re = array($q);
  452. break;
  453. case is_array($q):
  454. return $q;
  455. break;
  456.  
  457. case @get_resource_type($q) == 'mysql result':
  458. if (@mysql_num_rows($q)) {
  459.  
  460. while($r = mysql_fetch_assoc($q)) {
  461. if ($key AND $r[$key]) {
  462. $re[$r[$key]] = ($value AND $r[$value]) ? $r[$value] : $r;
  463. }
  464. else { $re[] = $r; }
  465. }
  466. $q = $re;
  467. }
  468. break;
  469.  
  470. }
  471.  
  472. return $re;
  473. }
  474.  
  475. // Version 2 av result function - ikke ferdig
  476. public function r($r, $foreach = false) {
  477. $nr = @mysql_num_rows($q);
  478. if (!@get_resource_type($q) == 'mysql result' OR !$nr) {
  479. return false;
  480. }
  481.  
  482. if ($foreach) {
  483. while($r = mysql_fetch_assoc($q)) {
  484. $re[] = $r;
  485. }
  486. }
  487. }
  488.  
  489. public function result($q, $d = false, $unike_key = false) {
  490. $nr = @mysql_num_rows($q);
  491. if (!@get_resource_type($q) == 'mysql result' OR !$nr) {
  492. return false;
  493. }
  494.  
  495. if ($nr > 1 OR $d) {
  496. while($r = mysql_fetch_assoc($q)) {
  497. if (count($r) == 2 AND $d) {
  498. reset ($r);
  499. $re[current($r)] = next($r);
  500. }
  501. elseif ($unike_key AND $r[$unike_key]) $re[$r[$unike_key]] = $r;
  502. else $re[] = $r;
  503. }
  504. }
  505. elseif ($nr == 1) {
  506. $r = mysql_fetch_assoc($q);
  507. if (count($r) == 1) {
  508. $re = current($r);
  509. }
  510. else {
  511. $re = $r;
  512. }
  513. }
  514. else {
  515. $re = false;
  516. }
  517. //echo $nr;
  518. //$this->kallet++;
  519. //echo 'Kallet: '.$this->kallet.'<br />';
  520. //print_r(mysql_fetch_array($q, MYSQL_ASSOC));
  521. //echo '<pre>'.print_r($re, 1).'</pre>';
  522. //return print_r($re, 1);
  523. return $re;
  524. }
  525.  
  526. private function _error_notConnected($key = false) {
  527. $key = $this->get_key($key, false, __LINE__, __FILE__);
  528. $r = 'Du er ikke connected til databasen for den angitte key\'en';
  529. if ($key) { $r .= ' ('.$key.')'; }
  530. return $r.'.';
  531. }
  532.  
  533. public function error($key = false, $line = __LINE__, $file = __FILE__) {
  534. $key = $this->get_key($key, false, $line, $file);
  535. if (!$this->db[$key]['connected']) {
  536. return $this->_error_notConnected($key);
  537. }
  538. $n = $this->key.$key;
  539. if ($error = mysql_error($this->$n)) {
  540. $html = '<br /><b>Mysql Error:</b> '.$error.'<br /><b>Query String:</b> '.end($this->qs);
  541. $html .= ($line ? '<br /><b>Line:</b> '.$line : '');
  542. $html .= ($file ? '<br /><b>File:</b> '.$file : '');
  543. return $html;
  544. }
  545. }
  546.  
  547. public function errno($key = false, $line = __LINE__, $file = __FILE__) {
  548. $key = $this->get_key($key, false, $line, $file);
  549. if (!$this->db[$key]['connected']) {
  550. return $this->_error_notConnected($key);
  551. }
  552. $n = $this->key.$key;
  553. return mysql_errno($this->$n);
  554. }
  555.  
  556. public function insert_id($key = false, $line = __LINE__, $file = __FILE__) {
  557. $n = $this->key.$this->get_key($key, $line, $file);
  558. return mysql_insert_id($this->$n);
  559. }
  560. public function last_q() {
  561. $q = end($this->q);
  562. return $q;
  563. }
  564. public function antall($q = false) {
  565. if (is_numeric($q)) { return $this->q[$q]['antall']; }
  566. if (@get_resource_type($q) == 'mysql result') { return mysql_num_rows($q); }
  567.  
  568. $q = $this->last_q();
  569. return $q['antall'];
  570. }
  571. public function count($q = false) {
  572. return $this->antall($q);
  573. }
  574.  
  575. public function count_qs() {
  576. return count($this->qs)-1;
  577. }
  578.  
  579. public function out_qs($search = false, $return = false, $format = '<p><b>Time:</b> {time}<br /><b>QS:</b> {qs}</p>') {
  580. //return print_r($this->q, 1);
  581. foreach ($this->q AS $q) {
  582. if ((!$search OR strstr($q['qs'], $search)) AND $q['qs']) {
  583. //$r .= str_replace(array('{time}', '{qs}'), array($q['time'], $q['qs']), $format);
  584. $line = ($q['line'] ? $q['line'] : 'No Line');
  585. $file = ($q['file'] ? $q['file'] : 'No File');
  586. $r .= "
  587. <p>
  588. <b>Time:</b> {$q['time']}<br />
  589. <b>QS:</b> ".(isset($_GET['qs_pre']) ? '<pre>'.$q['qs'].'</pre>' : $q['qs'])."<br />
  590. <b>Line:</b> $line<br />
  591. <b>File:</b> $file
  592. </p>
  593. ";
  594. }
  595. }
  596. //return '<pre>'.htmlspecialchars($r);
  597.  
  598. if ($return) { return $r; }
  599. else { echo $r; return true; }
  600. }
  601.  
  602. // Functionen hentet fra phpmanualen
  603. public function quote_smart1($value, $key = false, $line = __LINE__, $file = __FILE__) {
  604. $this->get_key($key, true, $line, $file);
  605.  
  606. // Stripslashes
  607. if (get_magic_quotes_gpc()) {
  608. $value = stripslashes($value);
  609. }
  610. // Quote if not a number or a numeric string
  611. if (!is_numeric($value)) {
  612. $value = "'" . @mysql_real_escape_string($value) . "'";
  613. }
  614. return $value;
  615. }
  616. public function quote_smart($value, $key = false, $line = __LINE__, $file = __FILE__) {
  617. $this->get_key($key, true, $line, $file);
  618.  
  619. if( is_array($value) ) {
  620. return array_map(array($this, "quote_smart"), $value);
  621. }
  622. else {
  623. // Stripslashes
  624. if( get_magic_quotes_gpc() ) {
  625. $value = stripslashes($value);
  626. }
  627. // Quote if not a number or a numeric string
  628. if( !is_numeric($value) || $value[0] == '0' ) {
  629. $value = "'".@mysql_real_escape_string($value)."'";
  630. }
  631. return $value;
  632. }
  633. }
  634.  
  635. public function stat($key = false, $line = __LINE__, $file = __FILE__) {
  636. $n = $this->key.$this->get_key($key, false, $line, $file);
  637. return mysql_stat($this->$n);
  638. }
  639.  
  640.  
  641.  
  642. // Resten er tatt fra phpBB2
  643.  
  644.  
  645. //
  646. // remove_remarks will strip the sql comment lines out of an uploaded sql file
  647. //
  648. function remove_remarks($sql)
  649. {
  650. $lines = explode("\n", $sql);
  651.  
  652. // try to keep mem. use down
  653. $sql = "";
  654.  
  655. $linecount = count($lines);
  656. $output = "";
  657.  
  658. for ($i = 0; $i < $linecount; $i++)
  659. {
  660. if (($i != ($linecount - 1)) || (strlen($lines[$i]) > 0))
  661. {
  662. if ($lines[$i][0] != "#")
  663. {
  664. $output .= $lines[$i] . "\n";
  665. }
  666. else
  667. {
  668. $output .= "\n";
  669. }
  670. // Trading a bit of speed for lower mem. use here.
  671. $lines[$i] = "";
  672. }
  673. }
  674.  
  675. return $output;
  676.  
  677. }
  678.  
  679.  
  680. //
  681. // split_sql_file will split an uploaded sql file into single sql statements.
  682. // Note: expects trim() to have already been run on $sql.
  683. //
  684.  
  685. private function split_sql_file($sql, $delimiter)
  686. {
  687. // Split up our string into "possible" SQL statements.
  688. $tokens = explode($delimiter, $sql);
  689.  
  690. // try to save mem.
  691. $sql = "";
  692. $output = array();
  693.  
  694. // we don't actually care about the matches preg gives us.
  695. $matches = array();
  696.  
  697. // this is faster than calling count($oktens) every time thru the loop.
  698. $token_count = count($tokens);
  699. for ($i = 0; $i < $token_count; $i++)
  700. {
  701. // Don't wanna add an empty string as the last thing in the array.
  702. if (($i != ($token_count - 1)) || (strlen($tokens[$i] > 0)))
  703. {
  704. // This is the total number of single quotes in the token.
  705. $total_quotes = preg_match_all("/'/", $tokens[$i], $matches);
  706. // Counts single quotes that are preceded by an odd number of backslashes,
  707. // which means they're escaped quotes.
  708. $escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$i], $matches);
  709.  
  710. $unescaped_quotes = $total_quotes - $escaped_quotes;
  711.  
  712. // If the number of unescaped quotes is even, then the delimiter did NOT occur inside a string literal.
  713. if (($unescaped_quotes % 2) == 0)
  714. {
  715. // It's a complete sql statement.
  716. $output[] = $tokens[$i];
  717. // save memory.
  718. $tokens[$i] = "";
  719. }
  720. else
  721. {
  722. // incomplete sql statement. keep adding tokens until we have a complete one.
  723. // $temp will hold what we have so far.
  724. $temp = $tokens[$i] . $delimiter;
  725. // save memory..
  726. $tokens[$i] = "";
  727.  
  728. // Do we have a complete statement yet?
  729. $complete_stmt = false;
  730.  
  731. for ($j = $i + 1; (!$complete_stmt && ($j < $token_count)); $j++)
  732. {
  733. // This is the total number of single quotes in the token.
  734. $total_quotes = preg_match_all("/'/", $tokens[$j], $matches);
  735. // Counts single quotes that are preceded by an odd number of backslashes,
  736. // which means they're escaped quotes.
  737. $escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$j], $matches);
  738.  
  739. $unescaped_quotes = $total_quotes - $escaped_quotes;
  740.  
  741. if (($unescaped_quotes % 2) == 1)
  742. {
  743. // odd number of unescaped quotes. In combination with the previous incomplete
  744. // statement(s), we now have a complete statement. (2 odds always make an even)
  745. $output[] = $temp . $tokens[$j];
  746.  
  747. // save memory.
  748. $tokens[$j] = "";
  749. $temp = "";
  750.  
  751. // exit the loop.
  752. $complete_stmt = true;
  753. // make sure the outer loop continues at the right point.
  754. $i = $j;
  755. }
  756. else
  757. {
  758. // even number of unescaped quotes. We still don't have a complete statement.
  759. // (1 odd and 1 even always make an odd)
  760. $temp .= $tokens[$j] . $delimiter;
  761. // save memory.
  762. $tokens[$j] = "";
  763. }
  764.  
  765. } // for..
  766. } // else
  767. }
  768. }
  769.  
  770. return $output;
  771. }
  772.  
  773.  
  774.  
  775.  
  776. // Brukes for å måle tiden
  777. private function getmicrotime(){
  778. list($usec, $sec) = explode(" ",microtime());
  779. return ((float)$usec + (float)$sec);
  780. }
  781. }
  782.  
  783. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement