|
26-03-2007, 15:58
|
|
|
|
חבר מתאריך: 01.05.06
הודעות: 7,861
|
|
כמה מחלקות ב PHP שפיתחתי.
המחלקות אמורות להיות שימושיות בעיקר לבוני הפורומים למיניהם, והיא נבדקה (לא בקפידה) ועובדת כיאות.
מחלקות:- extendedException - custom exception object
- configuration - output management
- db - MySQL connect, queries & fetching
- filter - filtering strings, arrays, variables and so on
- functions - some useful programming functions
אני בטוח שבקוד יש עוד יחסית הרבה שגיאות ותכנים לא יעילים במיוחד ולכן פרסמתי את הקוד כאן.
לינק:
http://paste.lisp.org/display/38735
הקוד:
קוד PHP:
<?
class extendedException extends Exception { # extends the parent exception class and adds - comment private $comment; function __construct($message, $code, $comment = 'false') { parent::__construct($message, $code); $this->comment = (is_string($comment)) ? ($comment) : (''); } # getting comment that only developer could see it public function getComment() { return $this->comment; } # getting the custom error - level defines the comment's show public function Error($level = false) { switch ($level) { case '1': $error = "Error in line:<b>%s</b>\n in:<b>%s</b>\n message:<b>%s</b>"; return sprintf ($error, $this->getLine(), $this->getFile(), $this->getMessage()); break; case '2': $error = "<b>%s</b><br />\nError in line:<b>%s</b>\n in:<b>%s</b>\n message:<b>%s</b><br />\n<i>%s</i>"; return sprintf ($error, $this->getCode(), $this->getLine(), $this->getFile(), $this->getMessage(), $this->getComment()); break; default: $error = "%s<br />\nError in line:<b>%s</b>\n in:<b>%s</b>\n message:<b>%s</b>"; return sprintf ($error, $this->getCode(), $this->getLine(), $this->getFile(), $this->getMessage()); break; } } } class configuration { public $global, $language, $lang, $encode; # might be used as an exceptions for the names # defines the outputs of the system function __construct($language, $lang, $encode) { if (!$language) throw new extendedException('There is no language defined', 0, 'the function should have a language argument'); $this->language = $language = strtolower(filter::filter($language, 'xss')); $this->lang = $lang = strtolower(filter::filter($lang, 'xss')); $this->encode = $encode = filter::filter($encode, 'xss'); $c = 0; // resets the counter $this->global[$language]['main'][++$c] = 'main page'; $this->global[$language]['main'][++$c] = 'text'; $this->global[$language]['main'][++$c] = 'text'; $this->global[$language]['main'][++$c] = 'etc...'; $c = 0; // resets the counter $this->global[$language]['other'][++$c] = 'other'; $this->global[$language]['other'][++$c] = 'text'; $this->global[$language]['other'][++$c] = 'text'; $this->global[$language]['other'][++$c] = 'etc...'; } public function displayOutput() { $string = ''; foreach (array_values($this->global[$this->language]) as $global_array) { $string .= "<br />"; foreach ($global_array as $global) { $string .= "\n".$global."\n"; } } return $string; } } class db { private $host, $username, $password, $dbname, $query; public $fetch; # automaticly connecting to the server when the class is being set function __construct($host = 'localhost', $username, $password = null, $dbname) { if (!$connect = mysql_pconnect($host, $username, $password)) throw new extendedException(mysql_error(), 1, 'the username or the password are invalid'); if (!$select_db = mysql_select_db($dbname, $connect)) throw new extendedException(mysql_error(), 1, 'the database name is invalid'); } # extended query function public function query($query) { $this->query = $query; if (!$this->query = mysql_query($this->query)) throw new extendedException(mysql_error(), 2, 'failure on the query run'); return $this->query; } # in case of SELECT query, it will fetch into a class variable # method - fetchs the query result into the given method and stores it in variable public function fetch($method = 'array') { if (!$this->query) throw new extendedException(mysql_error(), 3, 'failure on the query fetch run'); switch ($method) { case 'num': return @mysql_num_rows($this->query); break; case 'id': return @mysql_insert_id($this->query); break; case 'object': return mysql_fetch_object($this->query); break; case 'field': return mysql_fetch_field($this->query); break; case 'array': return mysql_fetch_array($this->query); break; case 'assoc': return mysql_fetch_assoc($this->query); break; case 'row': return mysql_fetch_row($this->query); break; case 'lengths': return mysql_fetch_lengths($this->query); break; default: throw new extendedException('Unsupported fetch method was given', 4, 'must be array/object/id/num/row/lengths/assoc'); break; } } function __destruct() { unset($this); } } class filter { private $input, $multiple_input, $filter_input; function __construct() { return null; } # filters the inserted input by the given type public function filter ($input, $type) { $this->filter_input = $input; switch ($type) { case 'strip_tags': if (gettype($this->filter_input) == 'string' || gettype($this->filter_input) == 'variable') return (string) htmlentities(trim($this->filter_input)); else throw new extendedException('Filter input type must be string or variable', 5, 'Filter function got some invalid type format'); break; case 'entitles': if (gettype($this->filter_input) == 'string' || gettype($this->filter_input) == 'variable') return (string) htmlentities(trim($this->filter_input)); else throw new extendedException('Filter input type must be string or variable', 5, 'Filter function got some invalid type format'); break; case 'entity_decode': if (gettype($this->filter_input) == 'string' || gettype($this->filter_input) == 'variable') return (string) html_entity_decode(trim($this->filter_input)); else throw new extendedException('Filter input type must be string or variable', 5, 'Filter function got some invalid type format'); break; case 'xss': if (gettype($this->filter_input) == 'string' || gettype($this->filter_input) == 'variable') return (string) htmlspecialchars(trim($this->filter_input)); else throw new extendedException('Filter input type must be string or variable', 5, 'Filter function got some invalid type format'); break; case 'escape': if (gettype($this->filter_input) == 'string' || gettype($this->filter_input) == 'variable') return (string) mysql_real_escape_string(trim($this->filter_input)); else throw new extendedException('Filter input type must be string or variable', 5, 'Filter function got some invalid type format'); break; default: throw new extendedException('No type was decleared', 6, 'Filter\'s attributes are:\'xss/entitles/entity_decode/escape\' '); break; } } # combines the function 'filter()' during some proccess # checking number of arguments and if there are multiple arguments - foreach loop will describe it into an actions # NEW option which is contains arrays in multiple arguments public function get ($input) { $numArgs = func_num_args(); if ($numArgs == 1) { switch (gettype($input)) { case'array': foreach ($input as $key => $arr_input) { $this->input[$key] = $this->filter($arr_input, 'escape'); } return $this->input; break; case 'string': case 'variable': $this->input = $this->filter($input, 'escape'); return $this->input; break; } } # new option - multiple arguments elseif ($numArgs >= 2) { // belongs to the get() function. validates if the arguments that was entered have the same method $check_method = gettype(func_get_arg(0)); foreach (func_get_args() as $key => $mArg) { if (gettype($mArg) != $check_method) throw new extendedException('The method should be the same in the all arguments', 7, "the invalid argument is the number {$key} argument"); } $this->multiple_input = func_get_args(); static $counter = 0; /* converting the arguments into an array */ foreach ($this->multiple_input as $arg) { /* ARGUMENTS */ switch (gettype($arg)) { case'array': foreach ($arg as $key => $arr_input) { /* discribing array */ $static_input[$key] = $this->filter($arr_input, 'escape'); } $this->input[$counter] = $static_input; break; case 'string': case 'variable': $this->input[$counter] = $this->filter($arg, 'escape'); break; } $counter++; /* the arguments will be converted into an array */ } return $this->input; } } } class functions extends filter { public $seconds, $minutes, $hours, $days, $months, $years; # collection of useful functions function __construct() { return null; } public function getTimeStamp ($time) { if (!$time) throw new extendedException('No date was entered to the timestamp', 8, 'Enter some timstamp date'); $T_time = time(); $D_time = strtotime($time); $stamp = $T_time - $D_time; $this->seconds = floor(abs($stamp)%60); //Seconds $this->minutes = floor((abs($stamp)/60)%60); //Minutes $this->hours = floor((abs($stamp)/3600)%24); // Hours $this->days = floor((abs($stamp)/86400)%30); //Days $this->months = floor((abs($stamp)/2592000)%12); //Months $this->years = floor((abs($stamp)/31104000)); //Years unset($T_time, $D_time, $stamp); } public function validation($input, $type) { if (!$type) throw new extendedException('No type was entered', 9, 'Enter some timstamp date'); switch ($type) { case 'address': if (preg_match('/^/^https?:\/\/[\w\-\.\/\?\&\=\%\_]+\.[\w\-\.\/\?\&\=\%\_]+$/i', $input)) return $input; else return false; break; case 'email': if (preg_match('/^[\w\_\.]+\@[\w\_\-]+\.[\w\_\-\.]+$/i', $input)) return $input; else return false; break; case 'image': if (preg_match('/^https?:\/\/[\w\-\.\/\?\&\=\%\_]+\.[\w\-\.\/\?\&\=\%\_]+\/[\w\-\.\/\?\&\=\%\_]+\.jpe?g|gif|png|bmp|tif?f$/i', $input)) return $input; else return false; break; case 'nickname': case 'password': if (preg_match('/^[\w\_\-\.]{1, 40}$/i', $input)) return $input; else return false; break; } } } ?>
תודה ל דן (Rs3K) שעזר לי בפיתוח המחלקה.
|
|