* * @dependency none * v1.1 */ /** * Methods: * Form::lazyLoad [static] * Form::inputCheck($val,$crit) * Form::isEmail($val) * Form::isText($val) * Form::isNumerical($val,$positive = false) * Form::isUser($val) * Form::isPassword($val) */ if (!DEFINED('CONFIG_LOADED')) header("HTTP/1.1 403 Forbidden"); class Form { static public function lazyLoad() { global $form; $form = new Form(); } function inputCheck($val,$crit) { return $this->$crit($val); } function isEmail($val) { return (!empty($val) && preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $val)); } function isText($val, $extraChars = null) { return $this->isLetters($val, true, true, $extraChars); } function isNumerical($val,$positive = false) { if (!$positive) { return is_numeric($val); } else { return is_numeric($val) && ($val>=0); } } function isUser($val, $minLength = 4) { return (!empty($val) && preg_match("/^[a-zA-Z]([\._a-zA-Z0-9-]*[a-zA-Z0-9]+)*$/", $val) && (strlen($val) > $minLength) && (strlen($val) < 33)); } function isLetters($val, $allowSpace = false, $allowPunctuation = false, $extraChars = null) { if ($allowPunctuation) { $val = str_replace(array(".",",","!","?",";","-"),"",$val); } if (!empty($extraChars)) { $val = str_replace(str_split($extraChars),"",$val); } if (!$allowSpace) { return (!empty($val) && preg_match("/^[a-zA-Z]*$/", $val)); } else { return (!empty($val) && preg_match("/^[a-zA-Z]+(\ [a-zA-Z]+)*$/", $val)); } } function isAlfaNumerical($val) { return (!empty($val) && preg_match("/^[a-zA-Z0-9]*$/", $val)); } function isPassword($val, $minLength = 4) { return (!empty($val) && (strlen($val) > $minLength) && (strlen($val) < 33)); } function validIpV4($ip) { return preg_match("/(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/",$ip); } function htmlSafe($val) { return htmlentities($val); } } ?> * * @dependency none * v1.1.1 */ /** * Methods: * * MySql::connect() [private] * MySql::lazyLoad() [static] * Mysql::reconnect() [private] * Mysql::query($query, $file = "Not specified", $line = 0) * Mysql::lastQuery() * Mysql::select($query, $file = "Not specified", $line = 0) * Mysql::insertData($tableName, $fieldsArray, $dataArray, $file = "Not specified", $line = 0) * Mysql::fetchResults($count = true) * Mysql::fetchResult() * Mysql::escape($var) * Mysql::stats() * Mysql::error($query, $error, $errno, $file, $line) [private] * Mysql::destroy() */ if (!DEFINED('CONFIG_LOADED')) header("HTTP/1.1 403 Forbidden"); class MySql { private $connection = NULL; private $time = 0; private $time_total = 0; private $query_count = 0; private $show_error = false; private $die_on_error = false; private $query = NULL; private $config = array(); private $retried = false; function __construct($conf) { $this->show_error = $conf['showError']; $this->die_on_error = $conf['dieOnError']; $this->config['host'] = $conf['host']; $this->config['user'] = $conf['user']; $this->config['pass'] = $conf['pass']; $this->config['db'] = $conf['database']; } static public function lazyLoad() { global $mysql, $config; $mysql = new MySql($config['mysql']); } private function connect() { if (!$this->connection) { $this->connection = mysql_connect($this->config['host'],$this->config['user'],$this->config['pass']) or $this->error('CONNECT',mysql_error(),mysql_errno(),__FILE__,__LINE__); mysql_select_db($this->config['db']) or $this->error('SELECT DB',mysql_error(),mysql_errno(),__FILE__,__LINE__); } } private function reconnect() { $this->destroy(); $this->connect(); } function query($query, $file = "Not specified", $line = 0) { if (!$this->connection) $this->connect(); $start_time = microtime(true); if(!empty($query)) { $this->lastQuery = $query; $this->query = mysql_query($query,$this->connection); } $this->time = microtime(true) - $start_time; $this->time_total += $this->time; if (!$this->query) { if (mysql_errno() >= 2000 && !$this->retried) { $this->retried = true; $this->reconnect(); $this->query($query, $file, $line); } else { $this->error($query,mysql_error(),mysql_errno(),$file,$line); } } $this->retried = false; $this->query_count++; return $this->query; } function lastQuery() { return $this->lastQuery; } function select($query, $file = "Not specified", $line = 0) { if (!$this->connection) $this->connect(); $start_time = microtime(true); $this->query($query, $file, $line); $ret = $this->fetchResults(); $this->time = microtime(true) - $start_time; $this->time_total += $this->time; return $ret; } function selectSingle($query, $file = "Not specified", $line = 0) { $ret = $this->select($query, $file, $line); if (!$ret['count']) return false; unset($ret['count']); return end($ret); } function insertData($tableName, $fieldsArray, $dataArray, $file = "Not specified", $line = 0) { if (!$this->connection) $this->connect(); if (is_array($dataArray[0])) { foreach ($dataArray as $key => $val) { foreach ($val as $key2 => $val2) { if ($val2 === "") { $dataArray[$key][$key2] = "NULL"; } else { $dataArray[$key][$key2] = '\''.$val2.'\''; } } $dataArray[$key] = "( " . implode(",", $dataArray[$key]) . ")"; } $dataArray = implode(",", $dataArray); } else { foreach ($dataArray as $key => $val) { if ($val === "") { $dataArray[$key] = "NULL"; } else { $dataArray[$key] = '\''.$val.'\''; } } $dataArray = "( " . implode(",", $dataArray) . ")"; } foreach ($fieldsArray as $key => $val) { $fieldsArray[$key] = '`'.$val.'`'; } $this->query( "INSERT INTO `" . $tableName . "` (" . implode(",", $fieldsArray) . ") VALUES " . $dataArray, $file, $line ); return mysql_insert_id($this->connection); } function fetchResults($count = true) { if ($this->query) { $start_time = microtime(true); $i = 0; while ($data[$i++] = mysql_fetch_assoc($this->query)); unset($data[$i - 1]); if ($count) $data['count'] = $i - 1; $this->time = microtime(true) - $start_time; $this->time_total += $this->time; return $data; } } function fetchResult() { if ($this->query) { $start_time = microtime(true); $data = mysql_fetch_assoc($this->query); $this->time = microtime(true) - $start_time; $this->time_total += $this->time; return $data; } } function escape($var) { if (!$this->connection) $this->connect(); stripslashes($var); return mysql_real_escape_string($var,$this->connection); } function stats(&$stats = array()) { $stats = array("last" => $this->time , "total" => $this->time_total , "count" => $this->query_count); return "Executed ".$this->query_count." queries in ".round($this->time_total,2)." seconds."; } private function error($query, $error, $errno, $file, $line) { if ($this->show_error) { echo '
QUERY: '.$query; echo '
ERROR: '.$error; echo '
CODE: '.$errno; echo '
FILE: '.$file; echo '
LINE: '.$line; if ($this->retried) { echo '
INFO: Connection was retried but query still failed.'; } } if ($this->die_on_error) { die(); } } function destroy() { mysql_close($this->connection); $this->connection = null; $this->query = null; } } ?>