log_actions = LOG_USER_ACTIONS; } /** * Connects to the MySQL database server. * * @param string $host * @param string $db * @param string $user * @param string $pass * @return int Returns the connection ID. */ function connect($host="", $db="", $user, $pass) { // try to connect as many as 5 times for ($i = 0; $i < 5; $i++) { $this->link_id = @mysql_connect($host, $user, $pass); if (is_resource($this->link_id)) { break; } else { sleep(3); } } if(!$this->link_id) { $this->halt("Could not connect to SQL Server: $host"); die('
Sorry, but the service is currently unavailable. Please, try a few moments later!
'); return false; } // select db $sel = @mysql_select_db($db, $this->link_id); if(!$sel) { $this->halt("Could not select SQL database: $db"); return false; } // set the character set $this->run('set names "utf8"'); return $this->link_id; } /** * Starts a transaction. */ function start_transaction() { $this->run('start transaction'); } /** * Commits the transaction. */ function commit() { $this->run('commit'); } /** * Rolls back the transaction. */ function rollback() { $this->run('rollback'); } /** * Returns an array containing the entire recordset. * * @param string $sql * @return array (upon success) or bool (upon failure) */ function fetch_all_records($sql) { if (!$this->link_id) return false; $rez = mysql_query($sql, $this->link_id) OR $this->halt($sql); while($row = mysql_fetch_array($rez, MYSQL_ASSOC)) { $rows[] = $row; } return (!empty($rows) ? $rows : false); } /** * Runs a sql query (usually a select sql). * * @param string $sql * @return int Returns the query ID. */ function run($sql) { if (!$this->link_id) return false; $this->query_id = mysql_query($sql, $this->link_id); if (!$this->query_id) { $this->halt($sql); return false; } elseif ($this->log_actions && preg_match("/^(insert|update|delete|truncate)/i", $sql) && isset($_SESSION['users_id'])) { $log_sql = 'insert into ' . TABLE_USER_LOG . ' values (0, ' . $_SESSION['users_id'] . ', "'.$sql.'", now())'; mysql_query($log_sql, $this->link_id); } return $this->query_id; } /** * Runs an unbuffered query. * * @param string $sql * @return int Returns the query ID. */ function run_unbuffered($sql) { $this->query_id = mysql_unbuffered_query($sql, $this->link_id) or $this->halt( $sql ); return $this->query_id; } /** * Returns the next row in the recordset. * * @param int $query_id * @param int $resutl_type * @return array || bool false */ function next($query_id = null, $result_type = MYSQL_BOTH) { if (!is_null($query_id) && is_resource($query_id) && $row = mysql_fetch_array($query_id, $result_type)) { return $row; } elseif (is_resource($this->query_id) && $row = mysql_fetch_array($this->query_id, $result_type)) { return $row; } return false; } function fetch_row($query_id = null, $result_type = MYSQL_BOTH) { return $this->next($query_id, $result_type); } /** * Performs an insert/update sql query based on an array that * contains the data in the $column => $value format. * * @access public * @param string $table * @param array $data * @param string $action * @param string $parameters (needed make the sql part that follows after the `where` clause) * @return int Returns the number of affected rows. */ function perform($table, $data, $action = 'insert', $parameters = '') { if (empty($data) || !is_array($data)) { $this->halt('The data must be a nonempty array!'); return; } reset($data); if ($action == 'insert' || $action == 'replace') { $query = $action.' into ' . $table . ' ('; while (list($columns, ) = each($data)) { $query .= $columns . ', '; } $query = substr($query, 0, -2) . ') values ('; reset($data); while (list(, $value) = each($data)) { switch ((string)$value) { case 'now()': $query .= 'now(), '; break; case 'null': $query .= 'null, '; break; default: $query .= '\'' . $this->escape($value) . '\', '; break; } } $query = substr($query, 0, -2) . ')'; } elseif ($action == 'update') { $query = 'update ' . $table . ' set '; while (list($columns, $value) = each($data)) { switch ((string)$value) { case 'now()': $query .= $columns . ' = now(), '; break; case 'null': $query .= $columns .= ' = null, '; break; default: $query .= $columns . ' = \'' . $this->escape($value) . '\', '; break; } } $query = substr($query, 0, -2) . ' where ' . $parameters; //$fp = fopen('debug.txt', 'w'); fwrite($fp, $query); fclose($fp); } return $this->run($query); } /** * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Should be called only after calling the affected_rows method. */ function insert_id() { $insert_id = mysql_insert_id($this->link_id); return $insert_id; } /** * Returns the max number found in the given column irrespective * if it is or not an AUTO_INCREMENT column. * It is not the safest function in a multiuser evironment. * * @param string $table * @param string $column * @return int */ function last_inserted_id($table, $column) { $last_inserted_id = 0; $sql = 'select max('.$column.') as last_inserted_id from ' . $table; if ( $res = $this->run($sql) ) { if ( $row = $this->next($res) ) { $last_inserted_id = $row['last_inserted_id']; } } return ( !empty($last_inserted_id) ? $last_inserted_id : false ); } /** * Returns the number of affected rows * by the last insert/update query. */ function affected_rows() { return @mysql_affected_rows($this->link_id); } /** * Retruns the number of rows in the recordset. */ function num_rows() { return @mysql_num_rows($this->query_id); } function num_fields($resource_id = '') { if ($resource_id != '') { return @mysql_num_fields($resource_id); } else { return @mysql_num_fields($this->query_id); } } function free_result() { return @mysql_free_result($this->link_id); } function db_close() { mysql_close($this->link_id); } function has_errors() { return $this->has_errors; } function halt($msg) { global $feedback; if (is_resource($this->link_id) && mysql_errno($this->link_id) > 0) { $this->has_errors = true; $msg .= "\n" . '
Error: ' . mysql_error($this->link_id) . "\n" . ' Error number: ' . mysql_errno($this->link_id); } switch ($this->error_treatment) { case 'display': if (isset($feedback)) { $feedback->add($msg); } else { print("
SqlWizard->halt: $msg !"); } break; case 'mail': mail(DEVELOPER_EMAIL_ADDRESS, 'Error: ' . SITE_DOMAIN_NAME, SITE_DOMAIN_NAME . ' ::SqlWizard ' . "\n" . $msg .' !'); break; case 'session': if (isset($feedback)) { $feedback->add_session($msg); } else { print("

SqlWizard error: $msg !

"); } break; case 'none': // do nothing break; } } /** * Set the information needed for debuging purposes such as * the e-mail address of the developer, the url of the * currently executed script and the error treatment policy. * * @param string $mail * @param string $url * @param string $error_treatment_policy */ function set_debug( $mail, $url, $error_treatment_policy = 'dispaly') { $this->debug_email_address = $mail; $this->shop_url_address = $url; $this->error_treatment = $error_treatment_policy; } /** * Sets the error treatment policy. Valid * values are: display | mail | session | none * * @param string $error_treatment_policy */ function set_error_treatment($error_treatment_policy = 'display') { $this->error_treatment = $error_treatment_policy; } /** * Escapes a string for safe database inserting. * If the second parameter is left to its default * value this functin will also trim the input. * * @param string $str * @param bool $trim * @return string */ function escape( $str, $trim = true ) { // trim input if ($trim) $str = trim($str); // if (get_magic_quotes_gpc()) $str = stripslashes($str); // escape if(version_compare(phpversion(),"4.3.0")=="-1") { return mysql_escape_string($str); } else { return mysql_real_escape_string($str); } } function str2timestamp($date_string) { $timestamp = 0; $sql = 'select UNIX_TIMESTAMP("'.$this->escape($date_string).'")'; if ($res = $this->run($sql)) { if ($record = $this->next($res, MYSQL_NUM)) { $timestamp = $record[0]; } } return $timestamp; } /** * Switches the logging of the sql commands ON or OFF. * * @param bool $option */ function set_sqls_logging($option = true) { $this->log_actions = $option; } } ?>