Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- *
- * Version: 1.1.10
- *
- *
- *
- */
- class mySQL_class {
- protected $db = array();
- public $current_key;
- private $last_key;
- private $key = 'key_';
- public $q = array(0 => NULL);
- public $qs = array(0 => NULL);
- public $nr = 0;
- public $connect_die = true;
- public $time = 0;
- public $charset = NULL;
- public $DEBUG = false;
- public $host = 'localhost';
- public $user = 'root';
- public $pass = '';
- public function add_db($key, $db = false, $host = false, $user = false, $pass = false, $charset = false) {
- if (!$db) { $db = $key; }
- if (!$host) { $host = $this->host; }
- if (!$user) { $user = $this->user; }
- if (!$pass) { $pass = $this->pass; }
- $a['host'] = $host;
- $a['user'] = $user;
- $a['pass'] = $pass;
- $a['db'] = $db;
- $a['charset'] = $charset;
- $this->db[$key] = $a;
- return true;
- }
- private function script_error($error, $line, $file, $die) {
- $html = "<b>Error:</b> $error<br /><b>Line:</b> $line<br /><b>File:</b> $file";
- if ($this->DEBUG) {
- die($html);
- }
- if ($die) {
- die('Script Error, MySQL_Class');
- }
- }
- public function connect($key = NULL, $reconnect = false, $die = true) {
- if ($key) {
- if ($this->db[$key]['connected'] AND !$reconnect) { return false; }
- if ($c = $this->db[$key]) { return $this->_connect($key, $c['host'], $c['user'], $c['pass'], $c['charset'], $die); }
- return false;
- }
- foreach ($this->db AS $key => $info) {
- if (!$info['connected'] OR $reconnect) {
- $this->_connect($key, $info['host'], $info['user'], $info['pass'], $info['charset'], $die);
- }
- }
- return true;
- }
- public function _connect ($key, $host, $user, $pass, $charset = NULL, $die = true) {
- $n = $this->key.$key;
- if ($this->$n) { $this->close($key); }
- //$this->$n = new mysqli($host, $user, $pass);
- $this->$n = @mysql_connect($host, $user, $pass);
- if ($this->$n) {
- $this->db[$key]['connected'] = true;
- if ($charset = strtolower($charset ? $charset : $this->charset)) {
- switch($charset) {
- case 'utf8':
- case 'utf-8':
- $qs1 = "SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'";
- $qs2 = "SET CHARACTER SET utf8";
- break;
- case 'iso-8859-1':
- case 'latin1':
- $qs1 = "SET NAMES 'latin1'";
- $qs2 = "SET CHARACTER SET latin1";
- break;
- default:
- $qs1 = "SET NAMES '".$charset."'";
- $qs2 = "SET CHARACTER SET $charset";
- break;
- }
- mysql_db_query($qs1, $this->$n);
- mysql_db_query($qs2, $this->$n);
- }
- return true;
- }
- $this->db[$key]['connected'] = false;
- if ($this->DEBUG AND $die) {
- $err_mes = 'Cannot connect to <i>'.$host.'</i> with username <i>'.$user.'</i>';
- if ($pass) { $err_mes .= ' and password given'; }
- else { $err_mes .= ' uten passord.'; }
- $err_mes .= '<br /><b>Mysql Error:</b> '.mysql_error();
- $this->script_error($err_mes, __LINE__, __FILE__, true);
- }
- return false;
- }
- public function close ($key) {
- $n = $this->key.$key;
- $this->db[$key]['connected'] = false;
- $v = mysql_close($this->$n);
- $this->$n = NULL;
- return $v;
- }
- public function set_key($key, $connect = false, $reconnect = false) {
- if ($this->db[$key]) {
- if ((!$this->db[$key]['connected'] AND $connect) OR $reconnect) { $this->connect($key, $reconnect); }
- $this->current_key = $key;
- return $key;
- }
- else { return false; }
- }
- public function get_key(&$key = false, $connect = false, $line = __LINE__, $file = __FILE__) {
- if ($key === false) { $key = ''; }
- if (!is_string($key)) {
- if ($this->DEBUG) { $this->script_error('Kan kan bare vare <i>string</b>. Input er <i>'.gettype($key).'</i>.', $line, $file, true); }
- return false;
- }
- if ($this->db[$key]) {}
- elseif ($this->current_key) { $key = $this->current_key; }
- elseif ($this->last_key) { $key = $this->last_key; /*$this->set_key($key, $connect, $connect_die);*/ }
- else { $key = key($this->db); }
- $this->last_key = $key;
- if (!$this->db[$key]['connected']) {
- if ($connect) { $connect = $this->connect($key, true); }
- else { $key = false; }
- }
- return $key;
- }
- public function get($qs, $key = false, $d = false, $unike_key = false) {
- $q = $this->_query($qs, $key, __LINE__, __FILE__);
- if ($error = $this->error($key, $line, $file)) {
- if ($this->DEBUG) { die($error); }
- return false;
- }
- if (@get_resource_type($q) == 'mysql result' AND @mysql_num_rows($q)) {
- return $this->result($q, $d, $unike_key);
- }
- return false;
- }
- public function query($sql, $key = false, &$errors = array(), $line = __LINE__, $file = __FILE__) {
- $sql_ = $sql;
- $connected = true;
- $key = $this->get_key($key, &$connected, $line, $file);
- if (!$connected) { return false; }
- $n = $this->key.$key;
- $sql = $this->remove_remarks($sql);
- $sql = $this->split_sql_file($sql, ';');
- //die('<pre>'.print_r($sql, 1).'</pre>');
- if (!count($sql)) { return false; }
- foreach ($sql AS $id => $qs) {
- if (trim($qs) != '') {
- $q = @mysql_db_query($this->db[$key]['db'], $qs, $this->$n);
- if ($e = mysql_error() AND $e != 'Query was empty') { $errors[$id] = $e; }
- }
- }
- if (!count($errors)) { return true; }
- if (!$this->DEBUG) { return false; }
- $mld = '<h1>MySQL Errors</h1>';
- foreach ($errors AS $id => $error) {
- $mld .= '<div style="background-color:#ccc; padding: 1px; border: 1px dotted #000;"><p>'.$error.'</p><p>'.$sql[$id].'</p></div>';;
- }
- $mld .= '<br /><fieldset><legend>Hele SQL</legend><pre>'.$sql_.'</fieldset>';
- die($mld);
- return false;
- }
- public function q($qs, $key = false, $line = __LINE__, $file = __FILE__) {
- $qs = trim($qs);
- $q = $this->_query($qs, $key, $line, $file);
- if ($error = $this->error($key, $line, $file)) {
- if ($this->DEBUG) { die($error); }
- return false;
- }
- if (@get_resource_type($q) == 'mysql result' AND @mysql_num_rows($q)) {
- return $q;
- }
- elseif (is_numeric($q)) {
- if ($this->q[$q]['insert_id']) { return $this->q[$q]['insert_id']; }
- }
- elseif (strtoupper(substr($qs, 0, 6)) == 'SELECT') { return false; }
- return true;
- //else { return $this->q[$q]; }
- }
- // IKKE tilrettelagt for nytt system, heler ikke ferdig
- public function select($hva = '*', $from = false, $where = false, $sort = false, $fra = '0', $antall = false, $distinct = false, $key = false) {
- if ($distinct) { $distinct == 'DISTINCT'; }
- if (!$from) { $from = $this->select_from; }
- $this->select_from = $from;
- // Hva skal hentes
- if (is_array($hva)) {
- foreach ($hva AS $as => $n) {
- if (is_array($n)) {
- if (is_numeric($as)) { $as = 'make_'.$as; }
- foreach ($n AS $as1 => $n1) {
- $h .= $as.'.`'.$n1.'`';
- if (!is_numeric($as1)) { $h .= ' AS '.$as1; }
- $h .= ', ';
- }
- }
- else {
- //if (is_numeric($as)) { $as = 'make_'.$as; }
- $h .= '`'.$n.'`, ';
- }
- }
- $h = substr($h, 0, -2);
- }
- else { $h = $hva;}
- $qs = sprintf('SELECT %s %s FROM %s', $distinct, $h, $from);
- // WHERE
- if (is_array($where)) {
- foreach ($where AS $a => $b) {
- if (is_array($b)) {
- if (is_numeric($a)) { $a = 'make_'.$a; }
- foreach ($b AS $c => $d) {
- $w .= $a.'.`'.$c.'` = '.$this->quote_smart($d);
- $w .= ' AND ';
- }
- $w = substr($w, 0, -4);
- }
- elseif (is_numeric($a)) {
- $w .= $b;
- }
- else {
- $w .= '`'.$a.'` = '.$this->quote_smart($b);
- }
- $w .= ' AND ';
- }
- $w = substr($w, 0, -4);
- }
- else { $w = $where;}
- if ($w) { $qs .= ' WHERE '.$w; }
- // Sortering
- if (is_array($sort)) {
- foreach ($sort AS $n) {
- $s .= '`'.$n.'`, ';
- }
- $s = substr($s, 0, -2);
- }
- else { $s = $sort; }
- if ($s) { $qs .= 'ORDER BY '.$s; }
- if (is_numeric($fra) AND $fra > 0) {
- $qs .= 'LIMIT '.$fra;
- if (is_numeric($antall) AND $antall > 0) { $qs .= ', '.$antall; }
- }
- //return $qs;
- //echo $qs.'<br />';
- $q = $this->q($qs, $key);
- $q_ = $this->last_q();
- $this->select_antall = $q['antall'];
- return $q;
- }
- public function insert($tabel = false, $v1 = false, $v2 = false, $key = false, $r_id = false, $line = __LINE__, $file = __FILE__) {
- if (!$tabel) { $tabel = $this->insert_tabel; }
- $this->insert_tabel = $tabel;
- $qs = 'INSERT INTO `'.$tabel.'`';
- if (is_string($v1)) {
- if (is_string($v2)) { $qs .= '('.$v1.') VALUES ('.$v2.')'; } // Setter "($v1) VALUES ($v2)"
- elseif (is_array($v2)) { // Hvis v1 er tekst og v2 er array
- $qs .= '('.$v1.') VALUES ';
- foreach ($v2 AS $v) { $qs .= '('.$v.'), '; }
- $qs = substr($qs, 0, -2);
- }
- else { $this->insert_error = 'Feil argumenter'; return false; }
- }
- elseif (is_array($v1)) {
- if (is_array($v2)) { $v1 = array_combine($v1, $v2); $v2 = false; }
- if (!$v2) {
- $qs .= 'SET ';
- foreach ($v1 AS $k => $v) { if ($k AND $v) { $qs .= $k.' = '.$v.', '; } }
- $qs = substr($qs, 0, -2);
- }
- else { $this->insert_error = 'Feil argumenter'; return false; }
- }
- $this->insert_qs = $qs;
- $this->insert_error = false;
- $qid = $this->_query($qs, $key, $line, $file);
- $q = $this->q[$qid];
- if ($r_id) { return $q['id']; }
- elseif ($q['error']) { $this->insert_error = $q['error']; return false; }
- else { return $q['insert_id']; }
- }
- public function update($tabel = false, $set = false, $where = false, $key = false, $r_id = false, $line = __LINE__, $file = __FILE__) {
- if (!$tabel) { $tabel = $this->update_tabel; }
- $this->update_tabel = $tabel;
- $qs = 'UPDATE `'.$tabel.'` SET ';
- if (is_string($set)) {
- $qs .= $set;
- }
- elseif (is_array($set)) {
- foreach ($set AS $k => $v) { if ($k AND $v) { $qs .= $k.' = '.$v.', '; } }
- $qs = substr($qs, 0, -2);
- }
- // WHERE
- if (is_array($where)) {
- foreach ($where AS $a => $b) {
- if (is_numeric($a)) {
- $w .= $b;
- }
- else {
- $w .= '`'.$a.'` = '.$b;
- }
- $w .= ' AND ';
- }
- $w = substr($w, 0, -4);
- }
- else { $w = $where;}
- if ($w) { $qs .= ' WHERE '.$w; }
- $this->update_qs = $qs;
- $this->update_error = false;
- $qid = $this->_query($qs, $key, $line, $file);
- $q = $this->q[$qid];
- if ($r_id) { return $q['id']; }
- elseif ($q['error']) { $this->update_error = $q['error']; return false; }
- else { return true; }
- }
- protected function _query ($qs, $key, $line, $file) {
- $connected = true;
- $key = $this->get_key($key, &$connected, $line, $file);
- if (!$connected) { return false; }
- $n = $this->key.$key;
- $id = count($this->q);
- $this->qs[] = $qs = trim($qs);
- $time_start = $this->getmicrotime();
- $this->last_q = $this->{'q'.$id} = $q = @mysql_db_query($this->db[$key]['db'], $qs, $this->$n);
- $time_end = $this->getmicrotime();
- $time = $time_end - $time_start;
- $this->time = $this->time+$time;
- $this->nr++;
- $this->q[$id]['id'] = $this->last_id = $id;
- $this->q[$id]['key'] = $key;
- $this->q[$id]['qs'] = $qs;
- $this->q[$id]['time'] = $time;
- $this->q[$id]['line'] = $line;
- $this->q[$id]['file'] = $file;
- $c = strtoupper(substr($qs, 0, strpos($qs, ' ')));
- if ($error = mysql_error($this->$n)) {
- $this->q[$id]['error'] = $error;
- $this->q[$id]['errno'] = mysql_errno($this->$n);
- return $id;
- }
- elseif (in_array($c, array('SELECT', 'SHOW'))) {
- $this->q[$id]['antall'] = @mysql_num_rows($q);
- return $q;
- }
- elseif (in_array($c, array('INSERT', 'UPDATE', 'DELETE'))) {
- $this->q[$id]['insert_id'] = mysql_insert_id($this->$n);
- return $id;
- }
- else {
- return $q;
- }
- /*
- $n = 'q'.$id;
- $this->$n = $q;
- end($this->q);
- //echo 'Key:'.key($this->q).'<br />';
- return $q;
- return key($this->q);
- */
- }
- // Convert mySQL results to a string
- // New in version 1.1.10
- public function getString(&$q) {
- if (@get_resource_type($q) == 'mysql result') {
- return $q = mysql_result($q, 0);
- }
- return $q;
- }
- // Convert mysql results to an array
- // Ny i version 1.1.7
- // Version 1.1.10: Renamed from toArray to getArray
- public function getArray(&$q, $key = false, $value = false) {
- $re = array();
- switch(true) {
- case is_string($q):
- case is_numeric($q):
- default:
- $re = array($q);
- break;
- case is_array($q):
- return $q;
- break;
- case @get_resource_type($q) == 'mysql result':
- if (@mysql_num_rows($q)) {
- while($r = mysql_fetch_assoc($q)) {
- if ($key AND $r[$key]) {
- $re[$r[$key]] = ($value AND $r[$value]) ? $r[$value] : $r;
- }
- else { $re[] = $r; }
- }
- $q = $re;
- }
- break;
- }
- return $re;
- }
- // Version 2 av result function - ikke ferdig
- public function r($r, $foreach = false) {
- $nr = @mysql_num_rows($q);
- if (!@get_resource_type($q) == 'mysql result' OR !$nr) {
- return false;
- }
- if ($foreach) {
- while($r = mysql_fetch_assoc($q)) {
- $re[] = $r;
- }
- }
- }
- public function result($q, $d = false, $unike_key = false) {
- $nr = @mysql_num_rows($q);
- if (!@get_resource_type($q) == 'mysql result' OR !$nr) {
- return false;
- }
- if ($nr > 1 OR $d) {
- while($r = mysql_fetch_assoc($q)) {
- if (count($r) == 2 AND $d) {
- reset ($r);
- $re[current($r)] = next($r);
- }
- elseif ($unike_key AND $r[$unike_key]) $re[$r[$unike_key]] = $r;
- else $re[] = $r;
- }
- }
- elseif ($nr == 1) {
- $r = mysql_fetch_assoc($q);
- if (count($r) == 1) {
- $re = current($r);
- }
- else {
- $re = $r;
- }
- }
- else {
- $re = false;
- }
- //echo $nr;
- //$this->kallet++;
- //echo 'Kallet: '.$this->kallet.'<br />';
- //print_r(mysql_fetch_array($q, MYSQL_ASSOC));
- //echo '<pre>'.print_r($re, 1).'</pre>';
- //return print_r($re, 1);
- return $re;
- }
- private function _error_notConnected($key = false) {
- $key = $this->get_key($key, false, __LINE__, __FILE__);
- $r = 'Du er ikke connected til databasen for den angitte key\'en';
- if ($key) { $r .= ' ('.$key.')'; }
- return $r.'.';
- }
- public function error($key = false, $line = __LINE__, $file = __FILE__) {
- $key = $this->get_key($key, false, $line, $file);
- if (!$this->db[$key]['connected']) {
- return $this->_error_notConnected($key);
- }
- $n = $this->key.$key;
- if ($error = mysql_error($this->$n)) {
- $html = '<br /><b>Mysql Error:</b> '.$error.'<br /><b>Query String:</b> '.end($this->qs);
- $html .= ($line ? '<br /><b>Line:</b> '.$line : '');
- $html .= ($file ? '<br /><b>File:</b> '.$file : '');
- return $html;
- }
- }
- public function errno($key = false, $line = __LINE__, $file = __FILE__) {
- $key = $this->get_key($key, false, $line, $file);
- if (!$this->db[$key]['connected']) {
- return $this->_error_notConnected($key);
- }
- $n = $this->key.$key;
- return mysql_errno($this->$n);
- }
- public function insert_id($key = false, $line = __LINE__, $file = __FILE__) {
- $n = $this->key.$this->get_key($key, $line, $file);
- return mysql_insert_id($this->$n);
- }
- public function last_q() {
- $q = end($this->q);
- return $q;
- }
- public function antall($q = false) {
- if (is_numeric($q)) { return $this->q[$q]['antall']; }
- if (@get_resource_type($q) == 'mysql result') { return mysql_num_rows($q); }
- $q = $this->last_q();
- return $q['antall'];
- }
- public function count($q = false) {
- return $this->antall($q);
- }
- public function count_qs() {
- return count($this->qs)-1;
- }
- public function out_qs($search = false, $return = false, $format = '<p><b>Time:</b> {time}<br /><b>QS:</b> {qs}</p>') {
- //return print_r($this->q, 1);
- foreach ($this->q AS $q) {
- if ((!$search OR strstr($q['qs'], $search)) AND $q['qs']) {
- //$r .= str_replace(array('{time}', '{qs}'), array($q['time'], $q['qs']), $format);
- $line = ($q['line'] ? $q['line'] : 'No Line');
- $file = ($q['file'] ? $q['file'] : 'No File');
- $r .= "
- <p>
- <b>Time:</b> {$q['time']}<br />
- <b>QS:</b> ".(isset($_GET['qs_pre']) ? '<pre>'.$q['qs'].'</pre>' : $q['qs'])."<br />
- <b>Line:</b> $line<br />
- <b>File:</b> $file
- </p>
- ";
- }
- }
- //return '<pre>'.htmlspecialchars($r);
- if ($return) { return $r; }
- else { echo $r; return true; }
- }
- // Functionen hentet fra phpmanualen
- public function quote_smart1($value, $key = false, $line = __LINE__, $file = __FILE__) {
- $this->get_key($key, true, $line, $file);
- // Stripslashes
- if (get_magic_quotes_gpc()) {
- $value = stripslashes($value);
- }
- // Quote if not a number or a numeric string
- if (!is_numeric($value)) {
- $value = "'" . @mysql_real_escape_string($value) . "'";
- }
- return $value;
- }
- public function quote_smart($value, $key = false, $line = __LINE__, $file = __FILE__) {
- $this->get_key($key, true, $line, $file);
- if( is_array($value) ) {
- return array_map(array($this, "quote_smart"), $value);
- }
- else {
- // Stripslashes
- if( get_magic_quotes_gpc() ) {
- $value = stripslashes($value);
- }
- // Quote if not a number or a numeric string
- if( !is_numeric($value) || $value[0] == '0' ) {
- $value = "'".@mysql_real_escape_string($value)."'";
- }
- return $value;
- }
- }
- public function stat($key = false, $line = __LINE__, $file = __FILE__) {
- $n = $this->key.$this->get_key($key, false, $line, $file);
- return mysql_stat($this->$n);
- }
- // Resten er tatt fra phpBB2
- //
- // remove_remarks will strip the sql comment lines out of an uploaded sql file
- //
- function remove_remarks($sql)
- {
- $lines = explode("\n", $sql);
- // try to keep mem. use down
- $sql = "";
- $linecount = count($lines);
- $output = "";
- for ($i = 0; $i < $linecount; $i++)
- {
- if (($i != ($linecount - 1)) || (strlen($lines[$i]) > 0))
- {
- if ($lines[$i][0] != "#")
- {
- $output .= $lines[$i] . "\n";
- }
- else
- {
- $output .= "\n";
- }
- // Trading a bit of speed for lower mem. use here.
- $lines[$i] = "";
- }
- }
- return $output;
- }
- //
- // split_sql_file will split an uploaded sql file into single sql statements.
- // Note: expects trim() to have already been run on $sql.
- //
- private function split_sql_file($sql, $delimiter)
- {
- // Split up our string into "possible" SQL statements.
- $tokens = explode($delimiter, $sql);
- // try to save mem.
- $sql = "";
- $output = array();
- // we don't actually care about the matches preg gives us.
- $matches = array();
- // this is faster than calling count($oktens) every time thru the loop.
- $token_count = count($tokens);
- for ($i = 0; $i < $token_count; $i++)
- {
- // Don't wanna add an empty string as the last thing in the array.
- if (($i != ($token_count - 1)) || (strlen($tokens[$i] > 0)))
- {
- // This is the total number of single quotes in the token.
- $total_quotes = preg_match_all("/'/", $tokens[$i], $matches);
- // Counts single quotes that are preceded by an odd number of backslashes,
- // which means they're escaped quotes.
- $escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$i], $matches);
- $unescaped_quotes = $total_quotes - $escaped_quotes;
- // If the number of unescaped quotes is even, then the delimiter did NOT occur inside a string literal.
- if (($unescaped_quotes % 2) == 0)
- {
- // It's a complete sql statement.
- $output[] = $tokens[$i];
- // save memory.
- $tokens[$i] = "";
- }
- else
- {
- // incomplete sql statement. keep adding tokens until we have a complete one.
- // $temp will hold what we have so far.
- $temp = $tokens[$i] . $delimiter;
- // save memory..
- $tokens[$i] = "";
- // Do we have a complete statement yet?
- $complete_stmt = false;
- for ($j = $i + 1; (!$complete_stmt && ($j < $token_count)); $j++)
- {
- // This is the total number of single quotes in the token.
- $total_quotes = preg_match_all("/'/", $tokens[$j], $matches);
- // Counts single quotes that are preceded by an odd number of backslashes,
- // which means they're escaped quotes.
- $escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$j], $matches);
- $unescaped_quotes = $total_quotes - $escaped_quotes;
- if (($unescaped_quotes % 2) == 1)
- {
- // odd number of unescaped quotes. In combination with the previous incomplete
- // statement(s), we now have a complete statement. (2 odds always make an even)
- $output[] = $temp . $tokens[$j];
- // save memory.
- $tokens[$j] = "";
- $temp = "";
- // exit the loop.
- $complete_stmt = true;
- // make sure the outer loop continues at the right point.
- $i = $j;
- }
- else
- {
- // even number of unescaped quotes. We still don't have a complete statement.
- // (1 odd and 1 even always make an odd)
- $temp .= $tokens[$j] . $delimiter;
- // save memory.
- $tokens[$j] = "";
- }
- } // for..
- } // else
- }
- }
- return $output;
- }
- // Brukes for å måle tiden
- private function getmicrotime(){
- list($usec, $sec) = explode(" ",microtime());
- return ((float)$usec + (float)$sec);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement