Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define("MySQL_MODE_ALL", chr(249) . chr(218) . chr(5) . "MySQL::MODE_ALL" . chr(5) . chr(218) . chr(249));
- define("MySQL_MODE_ONE", chr(249) . chr(218) . chr(5) . "MySQL::MODE_ONE" . chr(5) . chr(218) . chr(249));
- define("MySQL_MODE_GET", chr(249) . chr(218) . chr(5) . "MySQL::MODE_GET" . chr(5) . chr(218) . chr(249));
- class MySQL
- {
- // +------------------------------------------------------------------+
- // | Protected Variables |
- // +------------------------------------------------------------------+
- var $_credentials;
- var $_conn;
- var $_lastQuery;
- var $_sqlError;
- var $_sqlErrorLevel;
- var $_debug;
- var $_defaultMode;
- var $_autoEscape;
- var $_currentDb;
- // +------------------------------------------------------------------+
- // | Class Constructor |
- // +------------------------------------------------------------------+
- function MySQL($server = null, $username = "", $password = "", $database = "")
- {
- $this->set_mode(MySQL_MODE_GET);
- $this->use_auto_escape(true);
- $this->use_debugging(false);
- $this->use_strict_errors(true);
- $this->_conn = null;
- $this->_lastError = null;
- $this->_currentDb = null;
- if (!function_exists('mysql_connect'))
- trigger_error("MySQL support not enabled on PHP installation", E_USER_ERROR);
- if (!is_string($server))
- trigger_error(__CLASS__ . "::" . __FUNCTION__ . "() expects parameter 1 to be string, " . gettype($server) . " given", E_USER_ERROR);
- if (!is_string($username))
- trigger_error(__CLASS__ . "::" . __FUNCTION__ . "() expects parameter 2 to be string, " . gettype($username) . " given", E_USER_ERROR);
- if (!is_string($password))
- trigger_error(__CLASS__ . "::" . __FUNCTION__ . "() expects parameter 3 to be string, " . gettype($password) . " given", E_USER_ERROR);
- if (!is_string($database))
- trigger_error(__CLASS__ . "::" . __FUNCTION__ . "() expects parameter 4 to be string, " . gettype($database) . " given", E_USER_ERROR);
- $this->_credentials['server'] = $server;
- $this->_credentials['user'] = $username;
- if ($username != "")
- $this->_credentials['pass'] = $password;
- else
- $this->_credentials['pass'] = "";
- $this->_credentials['dbname'] = $database;
- }
- function _sql_error($query = null)
- {
- $this->_sqlError=array();
- $this->_sqlError['errno'] = @mysql_errno();
- if ($this->_sqlError['errno'] == 1064)
- {
- $tmpMessage = @mysql_error();
- $wrkMessageA = $tmpMessage;
- $wrkMessageB = strstr($wrkMessageA, 'at line');
- if ($wrkMessageB === FALSE)
- { $this->_sqlError['message'] = $tmpMessage; }
- else
- {
- while ($wrkMessageA != $wrkMessageB)
- {
- $wrkMessageA = $wrkMessageB;
- $wrkMessageB = strstr($wrkMessageA, 'at line');
- }
- $this->_sqlError['message'] = substr($tmpMessage, 0, strlen($wrkMessageB) * -1);
- }
- }
- else
- { $this->_sqlError['message'] = @mysql_error(); }
- $this->_lastQuery = $query;
- if ($this->_debug && !is_null($this->_lastQuery))
- ErrorHandler::log(PEAR_LOG_NOTICE, 'failed query: ' . $this->_lastQuery);
- trigger_error('' . $this->_sqlError['errno'] . ' - ' . $this->_sqlError['message'], $this->_sqlErrorLevel);
- }
- // +------------------------------------------------------------------+
- // | Protected Methods - Sanitation |
- // +------------------------------------------------------------------+
- function _is_valid_mode($mode)
- { return ($mode == MySQL_MODE_ONE || $mode == MySQL_MODE_ALL || $mode == MySQL_MODE_GET); }
- // +------------------------------------------------------------------+
- // | Protected Methods - Helper Methods |
- // +------------------------------------------------------------------+
- function _match_wildcard($pattern, $string)
- {
- $corr = addcslashes($pattern, '/\\.+^$(){}=!<>|');
- $search=array
- (
- '_',
- '%',
- '\\\.?',
- '\\\.*'
- );
- $replace=array
- (
- '.?',
- '.*',
- '_',
- '%'
- );
- if (@preg_match('/^' . str_replace($search, $replace, $corr) . '$/i', $string) > 0)
- return true;
- else
- return false;
- }
- // +------------------------------------------------------------------+
- // | Public Methods - Get/Set State |
- // +------------------------------------------------------------------+
- function get_last_error()
- {
- $lastError = $this->_sqlError;
- $this->_sqlError = null;
- return $lastError;
- }
- function set_mode($mode = null)
- {
- if (!is_string($mode))
- trigger_error(__CLASS__ . "::" . __FUNCTION__ . "() expects parameter 1 to be boolean, " . gettype($mode) . " given", E_USER_WARNING);
- elseif (!$this->_is_valid_mode($mode))
- trigger_error(__CLASS__ . "::" . __FUNCTION__ . "() expects parameter 1 to be equal to either constant MySQL_MODE_ALL, MySQL_MODE_ONE, or MySQL_MODE_GET", E_USER_WARNING);
- else
- $this->_defaultMode = $mode;
- }
- function use_auto_escape($autoEscape = null)
- {
- if (!is_bool($autoEscape))
- trigger_error(__CLASS__ . "::" . __FUNCTION__ . "() expects parameter 1 to be boolean, " . gettype($autoEscape) . " given", E_USER_WARNING);
- else
- $this->_autoEscape = $autoEscape;
- }
- function use_debugging($debug = null)
- {
- if (!is_bool($debug))
- trigger_error(__CLASS__ . "::" . __FUNCTION__ . "() expects parameter 1 to be boolean, " . gettype($debug) . " given", E_USER_WARNING);
- else
- $this->_debug = $debug;
- }
- function use_strict_errors($strictErrors = null)
- {
- if (!is_bool($strictErrors))
- trigger_error(__CLASS__ . "::" . __FUNCTION__ . "() expects parameter 1 to be boolean, " . gettype($strictErrors) . " given", E_USER_WARNING);
- elseif ($strictErrors === TRUE)
- $this->_sqlErrorLevel = E_USER_ERROR;
- else
- $this->_sqlErrorLevel = E_USER_WARNING;
- }
- // +------------------------------------------------------------------+
- // | Public Methods - Connection Handling |
- // +------------------------------------------------------------------+
- function close()
- {
- if ($this->opened())
- trigger_error("Connection is already closed", E_USER_NOTICE);
- else
- {
- if (@mysql_close($this->_conn) === FALSE)
- {
- $this->_sql_error();
- return false;
- }
- else
- {
- $this->_conn = null;
- $this->_currentDb = null;
- }
- }
- return true;
- }
- function open()
- {
- $this->_conn = @mysql_connect($this->_credentials['server'], $this->_credentials['user'], $this->_credentials['pass'], true);
- if ($this->_conn === FALSE)
- {
- $this->_sql_error();
- $this->_conn = null;
- return false;
- }
- elseif ($this->_credentials['dbname'] != '')
- {
- if ($this->select_db($this->_credentials['dbname']) === FALSE)
- {
- $this->close();
- return false;
- }
- }
- return true;
- }
- function opened()
- { return !(is_null($this->_conn) || $this->_conn === FALSE); }
- // +------------------------------------------------------------------+
- // | Public Methods - Database Navigation |
- // +------------------------------------------------------------------+
- function select_db($database = null)
- {
- if (!is_string($database))
- {
- trigger_error(__CLASS__ . "::" . __FUNCTION__ . "() expects parameter 1 to be string, " . gettype($database) . " given", E_USER_WARNING);
- return false;
- }
- if (!$this->opened())
- {
- trigger_error('Not connected to MySQL Server', $this->_sqlErrorLevel);
- return false;
- }
- if ($database != '')
- {
- if (@mysql_select_db($database, $this->_conn) === FALSE)
- {
- $this->_sql_error();
- return false;
- }
- else
- { $this->_currentDb = $database; }
- }
- return true;
- }
- // +------------------------------------------------------------------+
- // | Public Methods - Dataset Management And Querying |
- // +------------------------------------------------------------------+
- function count_affected()
- {
- if (!$this->opened())
- {
- trigger_error('Not connected to MySQL Server', $this->_sqlErrorLevel);
- return 0;
- }
- else
- { return@mysql_affected_rows($this->_conn); }
- }
- function count($resultSet = NULL)
- {
- if (is_bool($resultSet))
- { return 0; }
- elseif (is_array($resultSet))
- { return count($resultSet); }
- elseif (is_null($resultSet))
- { return 0; }
- elseif (!(is_resource($resultSet) && get_resource_type($resultSet) == 'mysql result'))
- {
- trigger_error(__CLASS__ . "::" . __FUNCTION__ . "() expects parameter 1 to be resource of type (mysql result), " . gettype($resultSet) . " given", E_USER_WARNING);
- return 0;
- }
- else
- { return@mysql_num_rows($resultSet); }
- }
- function create_value_list()
- {
- $numArgs = func_num_args();
- if ($numArgs < 1)
- {
- trigger_error(__CLASS__ . "::" . __FUNCTION__ . "() expects at least 1 parameter", E_USER_WARNING);
- return "('" . '\0\1\0' . "')";
- }
- if (!$this->opened())
- {
- trigger_error('Not connected to MySQL Server', $this->_sqlErrorLevel);
- return "('" . '\0\1\0' . "')";
- }
- $args = func_get_args();
- $result = null;
- if (is_string($args[0]))
- {
- if (!$this->_is_valid_mode($args[count($args) - 1]))
- $args[] = MySQL_MODE_GET;
- else
- $args[count($args) - 1] = MySQL_MODE_GET;
- $result = call_user_func_array(array
- (
- $this,
- "query"
- ), $args);
- }
- if (!is_null($result) || (is_resource($args[0]) && get_resource_type($args[0]) == 'mysql result'))
- {
- if (is_null($result))
- { $result = $args[0]; }
- $key = null;
- $values=array();
- while ($row = $this->fetch($result))
- {
- if (is_null($key))
- {
- $allkeys = array_keys($row);
- $key = $allkeys[0];
- }
- $values[] = $this->escape($row[$key]);
- }
- if (count($values) > 0)
- return "('" . implode("','", $values) . "')";
- else
- return "('" . '\0\1\0' . "')";
- }
- elseif (is_array($args[0]))
- {
- if (count($args[0]) < 1)
- { return "('" . '\0\1\0' . "')"; }
- elseif (is_array($args[0][0]))
- {
- $key = null;
- $values=array();
- foreach ($args[0] as $row)
- {
- if (is_null($key))
- {
- $allkeys = array_keys($row);
- $key = $allkeys[0];
- }
- $values[] = $this->escape($row[$key]);
- }
- if (count($values) > 0)
- return "('" . implode("','", $values) . "')";
- else
- return "('" . '\0\1\0' . "')";
- }
- else
- {
- $allkeys = array_keys($args[0]);
- return "('" . $this->escape($args[0][$allkeys[0]]) . "')";
- }
- }
- elseif (is_null($args[0]))
- { return "('" . '\0\1\0' . "')"; }
- else
- {
- trigger_error(__CLASS__ . "::" . __FUNCTION__ . "() expects parameter 1 to be resource of type array, (mysql result) or string, " . gettype($args[0]) . " given", E_USER_WARNING);
- return "('" . '\0\1\0' . "')";
- }
- }
- function fetch($resultSet = NULL)
- {
- if (is_bool($resultSet))
- { return false; }
- elseif (!(is_resource($resultSet) && get_resource_type($resultSet) == 'mysql result'))
- {
- trigger_error(__CLASS__ . "::" . __FUNCTION__ . "() expects parameter 1 to be resource of type (mysql result), " . gettype($resultSet) . " given", E_USER_WARNING);
- return false;
- }
- else
- { return@mysql_fetch_array($resultSet); }
- }
- function last_insert_id()
- {
- if (!$this->opened())
- {
- trigger_error('Not connected to MySQL Server', $this->_sqlErrorLevel);
- return false;
- }
- else
- {
- $resultSet = $this->query("SELECT LAST_INSERT_ID() AS 'LastId';", MySQL_MODE_ONE);
- if (!is_null($resultSet))
- { return $resultSet['LastId']; }
- else
- { return null; }
- }
- }
- function query()
- {
- $numArgs = func_num_args();
- if ($numArgs < 1)
- {
- trigger_error(__CLASS__ . "::" . __FUNCTION__ . "() expects at least 1 parameter", E_USER_WARNING);
- return false;
- }
- $mode = $this->_defaultMode;
- $args = func_get_args();
- $rawQuery = array_shift($args);
- if (!is_string($rawQuery))
- {
- trigger_error(__CLASS__ . "::" . __FUNCTION__ . "() expects parameter 1 to be string, " . gettype($rawQuery) . " given", E_USER_WARNING);
- return false;
- }
- if (!$this->opened())
- {
- trigger_error('Not connected to MySQL Server', $this->_sqlErrorLevel);
- return false;
- }
- if ($numArgs > 1)
- {
- $argMode = array_pop($args);
- if (!$this->_is_valid_mode($argMode))
- { array_push($args, $argMode); }
- else
- { $mode = $argMode; }
- if (count($args) > 0)
- {
- if ($this->_autoEscape)
- foreach ($args as $key => $string)
- {
- if (is_scalar($string) || is_null($string))
- $args[$key] = $this->escape($string);
- else
- {
- trigger_error(__CLASS__ . "::" . __FUNCTION__ . "() expects parameter " . ($key + 1) . " to be NULL or scalar, " . gettype($string) . " given", E_USER_WARNING);
- return false;
- }
- }
- $query = vsprintf($rawQuery, $args);
- }
- else
- $query = $rawQuery;
- }
- else
- $query = $rawQuery;
- $resultSet = @mysql_query($query, $this->_conn);
- if ($resultSet === FALSE)
- {
- $this->_sql_error($query);
- return false;
- }
- else
- {
- if ($mode == MySQL_MODE_ALL)
- {
- $rows=array();
- if (@mysql_num_rows($resultSet))
- while ($row = mysql_fetch_array($resultSet))
- $rows[] = $row;
- @mysql_free_result($resultSet);
- return $rows;
- }
- elseif ($mode == MySQL_MODE_ONE)
- {
- $row = null;
- if (@mysql_num_rows($resultSet))
- $row = mysql_fetch_array($resultSet);
- @mysql_free_result($resultSet);
- return $row;
- }
- else
- { return $resultSet; }
- }
- }
- function seek($resultSet = NULL, $row = NULL)
- {
- if (is_bool($resultSet))
- { return false; }
- elseif (!(is_resource($resultSet) && get_resource_type($resultSet) == 'mysql result'))
- {
- trigger_error(__CLASS__ . "::" . __FUNCTION__ . "() expects parameter 1 to be resource of type (mysql result), " . gettype($resultSet) . " given", E_USER_WARNING);
- return false;
- }
- elseif (!is_int($row))
- {
- trigger_error(__CLASS__ . "::" . __FUNCTION__ . "() expects parameter 2 to be integer, " . gettype($row) . " given", E_USER_WARNING);
- return false;
- }
- else
- { return@mysql_data_seek($resultSet, $row); }
- }
- // +------------------------------------------------------------------+
- // | Public Methods - Helper And Security Functions |
- // +------------------------------------------------------------------+
- function get_rights($location = '*')
- {
- if (!is_string($location))
- {
- trigger_error(__CLASS__ . "::" . __FUNCTION__ . "() expects parameter 1 to be string, " . gettype($location) . " given", E_USER_WARNING);
- return false;
- }
- $locationArray = explode('.', $location);
- if (count($locationArray) == 1)
- {
- if (is_null($this->_currentDb))
- $database = '*';
- else
- $database = $this->_currentDb;
- $table = $locationArray[0];
- }
- else
- {
- $database = $locationArray[0];
- $table = $locationArray[1];
- }
- $rights=Array();
- $results = $this->query('SHOW GRANTS;', MySQL_MODE_ALL);
- if ($results === false)
- { return false; }
- foreach ($results AS $row)
- {
- $stmt = $row[0];
- $matches=Array();
- preg_match('/GRANT ([\s\S]*) ON ([\s\S]*)[.]([\s\S]*) TO [\s\S]*/', $stmt, $matches);
- $matches[2] = str_replace('`', '', $matches[2]);
- $matches[3] = str_replace('`', '', $matches[3]);
- if ($matches[2] == '*' && $matches[3] == '*')
- {
- $thoseMatches = explode(',', strtoupper($matches[1]));
- if (strpos(strtoupper($stmt), "WITH GRANT OPTION") !== FALSE)
- $thoseMatches[] = "GRANT";
- $rights = array_merge($thoseMatches, $rights);
- }
- elseif ($database == '*' && $table != '*' && $matches[2] == '*' && $matches[3] != '*')
- {
- if ($this->_match_wildcard($table, $matches[3]))
- {
- $thoseMatches = explode(',', strtoupper($matches[1]));
- if (strpos(strtoupper($stmt), "WITH GRANT OPTION") !== FALSE)
- $thoseMatches[] = "GRANT";
- $rights = array_merge($thoseMatches, $rights);
- }
- }
- elseif ($database != '*' && $table == '*' && $matches[2] != '*' && $matches[3] == '*')
- {
- if ($this->_match_wildcard($database, $matches[2]))
- {
- $thoseMatches = explode(',', strtoupper($matches[1]));
- if (strpos(strtoupper($stmt), "WITH GRANT OPTION") !== FALSE)
- $thoseMatches[] = "GRANT";
- $rights = array_merge($thoseMatches, $rights);
- }
- }
- elseif ($database != '*' && $table != '*')
- {
- if (($this->_match_wildcard($database, $matches[2]) || $matches[2] == '*') && ($this->_match_wildcard($table, $matches[3]) || $matches[3] == '*'))
- {
- $thoseMatches = explode(',', strtoupper($matches[1]));
- if (strpos(strtoupper($stmt), "WITH GRANT OPTION") !== FALSE)
- $thoseMatches[] = "GRANT";
- $rights = array_merge($thoseMatches, $rights);
- }
- }
- }
- foreach ($rights as $key => $value)
- {
- $rights[$key] = strtoupper($value);
- $rights[$key] = trim($rights[$key]);
- }
- $rights = array_unique($rights);
- sort($rights);
- return $rights;
- }
- function escape($string)
- {
- if (!(is_scalar($string) || is_null($string)))
- {
- trigger_error(__CLASS__ . "::" . __FUNCTION__ . "() expects parameter 1 to be scalar, " . gettype($row) . " given", E_USER_WARNING);
- return false;
- }
- elseif ($this->opened())
- { return@mysql_real_escape_string($string, $this->_conn); }
- else
- { return@mysql_real_escape_string($string); }
- }
- // +------------------------------------------------------------------+
- // | Public Methods - Backwards Compatibility (DO NOT USE) |
- // +------------------------------------------------------------------+
- function affectedRows($result = null)
- {
- $return = $this->count_affected($result);
- return $return;
- }
- function auto_escape($autoEscape = null)
- { $this->use_auto_escape($autoEscape); }
- function connected()
- { return $this->opened(); }
- function dataSeek($result = null, $row = null)
- {
- $return = $this->seek($result, $row);
- return $return;
- }
- function dies($strictErrors = null)
- { $this->use_strict_errors($strictErrors); }
- function escape_string($string = null)
- {
- $return = $this->escape($string);
- return $return;
- }
- function fetchArray($result = null)
- {
- $return = $this->fetch($result);
- return $return;
- }
- function getLastError()
- { return $this->get_last_error(); }
- function numRows($result = null)
- {
- $return = $this->count($result);
- return $return;
- }
- function setDefaultMode($mode = null)
- { $this->set_mode($mode); }
- function getRights($location = '*')
- {
- $return = $this->get_rights($location);
- return $return;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement