PHP function Collection Class source
The code is as follows:
$_value) {$_string[$_key] = tool::htmlstring ($_value); Recursive}} elseif (Is_object ($_date)) {foreach ($_date as $_key=>$_value) {$_string->$_key = tool::htmlstring ($_value ); Recursive}} else {$_string = Htmlspecialchars ($_date);} return $_string;} /** * Database Input filter * @param string $_data * @return string */static public function mysqlstring ($_data) {$_data = Trim ($_data); Return! Gpc? Addcslashes ($_data): $_data;} /** * Cleanup Session */static Public Function unsession () {if (Session_Start ()) {Session_destroy ();}} /** * Verify that NULL * @param string $STR * @param string $name * @return bool (TRUE or FALSE) */static function Validateempty ($st R, $name) {if (empty ($str)) {Self::alertback (' Warning: ') $name. ' Can't be empty! ');}} /** * Verify the same * @param string $str 1 * @param string $str 2 * @param string $alert * @return JS */static function validateal L ($str 1, $str 2, $alert) {if ($str 1! = $str 2) self::alertback (' Warning: '. $alert);} /** * Verify ID * @param number $id * @return JS */static function Validateid ($id) {if (empty ($id) | | |!is_numeric ($id)) Self::alertback (' Warning: parameter Error! ');} /** * Format string * @param string $str * @return string */static public function Formatstr ($str) {$arr = Array (', ', ' & ') , '@', '#', '%', '\'', '"', '\\', '/', '.', ',', '$', '^', '*', '(', ')', '[', ']', '{', '}', '|', '~', '`', '?', '!', '; ', ': ', '-', ' _ ', ' + ', ' = '), foreach ($arr as $v) {$str = Str_replace ($v, ', $str);} return $STR;} /** * Format time * @param int $time timestamp * @return string */static public function formatdate ($time = ' default ') {$date = $time = = ' Default '? Date (' y-m-d h:i:s ', Time ()): Date (' y-m-d h:i:s ', $time); return $date;} /** * Get real IP address * @return string */static public Function Realip () {Static $realip = NULL; if ($realip!== NULL) return $realip; if (Isset ($_server)) {if (Isset ($_server[' http_x_forwarded_for ')) {$arr = explode (', ', $_server[ ' Http_x_forwarded_for ']); foreach ($arr as $ip) {$ip = Trim ($IP); if ($ip! = ' Unknown ') { $realip = $ip; Break }}} elseif (Isset ($_server[' http_client_ip ')) {$realip = $_server[' http_client_i P ']; } else {if (Isset ($_server[' remote_addr ')) {$realip = $_server[' remote_addr ']; } else {$realip = ' 0.0.0.0 '; }}}} else {if (getenv (' http_x_forwarded_for ')) {$realip = getenv (' Http_x_forwarded_ For '); } elseif (getenv (' http_client_ip ')) {$realip = getenv (' http_client_ip '); } else {$realip = getenv (' remote_addr '); }} preg_match ('/[\d\.] {7,15}/', $realip, $ONLINEIP); $realip =!empty ($onlineip [0])? $ONLINEIP [0]: ' 0.0.0.0 '; return $realip; }/** * Load Smarty template * @param string $html * @return null; */static Public Function display () {global $tpl; $html = null; $HTMLARR = explode ('/', $_server[script_name]); $html = Str_ireplace ('. php ', '. html ', $HTMLARR [count ($HTMLARR)-1]); $dir = DirName ($_server[ Script_name]); $firstStr = substr ($dir, 0, 1), $endStr = substr ($dir, strlen ($dir)-1, 1); if ($firstStr = = '/' | | $firstStr = = ' \ \ ') $dir = substr ($dir, 1), if ($endStr! = '/' | | $endStr! = ' \ \ ') $dir = $dir. '/'; $tpl->display ($dir. $html);} /** * Create directory * @param string $dir */static public Function Createdir ($dir) {if (!is_dir ($dir)) {mkdir ($dir, 0777);}} /** * Create file (default is empty) * @param unknown_type $filename */static public Function CreateFile ($filename) {if (!is_file ($filename)) Touch ($filename);} /** * Get the variable correctly * @param string $param * @param string $type * @return string */static public function GetData ($param, $type = ' Post ') {$type = Strtolower ($type), if ($type = = ' Post ') {return tool::mysqlstring (Trim ($_post[$param));} elseif ($type = = ' Get ') {return tool::mysqlstring (Trim ($_get[$param));}} /** * Delete file * @param string $filename */static public Function Delfile ($filename) {if (file_existS ($filename)) unlink ($filename);} /** * Delete directory * @param string $path */static public Function Deldir ($path) {if (Is_dir ($path)) rmdir ($path);} /** * Delete directory and all files underground * @param string $dir * @return bool */static public Function Deldirofall ($dir) {//delete files under directory: if (Is_dir ( $dir) {$dh =opendir ($dir); while (!! $file =readdir ($DH)) {if ($file! = "." && $file! = "...") {$fullpath = $dir. " /". $file, if (!is_dir ($fullpath)) {unlink ($fullpath);} else {self::d eldirofall ($fullpath);}}} Closedir ($DH);//Delete current folder: if (RmDir ($dir)) {return true;} else {return false;}}} /** * Verify Login */static public Function Validatelogin () {if (Empty ($_session[' admin ' [' user '])) header (' location:/admin/');} /** * Add a watermark to an already existing picture * @param string $file _path * @return bool */static public Function Addmark ($file _path) {if (file_exists ($file _path) && file_exists (MARK) {//To find the name suffix of the uploaded image $ext_name = Strtolower (substr ($file _path, Strrpos ($file _ Path, '. '), strlen ($file _path));//$new _name= ' Jzy_ '. Time (). Rand (1000,9999). $ext _name; $store _path = Root_path. updir;//upload Image high Width $imginfo = getimagesize ($file _path); $width = $imginfo [0]; $height = $imginfo [1]; Add Picture watermark switch ($ext _name) {case '. gif ': $dst _im = imagecreatefromgif ($file _path); Break;case '. jpg ': $dst _im = i Magecreatefromjpeg ($file _path); Break;case '. png ': $dst _im = imagecreatefrompng ($file _path); break;} $src _im = imagecreatefrompng (Mark);//watermark image High Width $src_imginfo = getimagesize (Mark); $src _width = $src _imginfo[0]; $src _ Height = $src _imginfo[1];//The actual generation position of the water impression $src_x = $width-$src _width-10; $src _y = $height-$src _height-10;//Create a new true color image $ Nimage = Imagecreatetruecolor ($width, $height); Copy upload image to True color image imagecopy ($nimage, $dst _im, 0, 0, 0, 0, $width, $height); Copy the watermark image to the true color image on the coordinate location imagecopy ($nimage, $src _im, $src _x, $src _y, 0, 0, $src _width, $src _height);//The watermark picture after the output is generated switch ($ Ext_name) {case '. gif ': Imagegif ($nimage, $file _path); Break;case '. jpg ': imagejpeg ($nimage, $file _path); Break;case '. PNG ': Imagepng ($nimage, $file _path); }//Releasing Resources ImagedeStroy ($dst _im); Imagedestroy ($src _im); unset ($imginfo); unset ($src _imginfo);//move the generated picture @move_uploaded_file ($file _ Path, Root_path. Updir. $file _path);}} /*** 2, single-byte capture mode * @access public* @param string $str required to intercept * @param int $slen intercept length * @param int $STARTDD start tag * @r Eturn string*/static Public Function cn_substr ($STR, $slen, $STARTDD =0) {$cfg _soft_lang = pagecharset;if ($cfg _soft_lang = = ' Utf-8 ') {return Self::cn_substr_utf8 ($str, $slen, $STARTDD);} $restr = "; $c ="; $str _len = strlen ($STR); if ($str _len < $STARTDD + 1) {return ';} if ($str _len < $STARTDD + $slen | | $slen ==0) {$slen = $str _len-$STARTDD;} $ENDDD = $STARTDD + $slen -1;for ($i =0; $i < $str _len; $i + +) {if ($STARTDD ==0) {$restr. = $c;} elseif ($i > $STARTDD) {$re str. = $c;} if (Ord ($str [$i]) >0x80) {if ($str _len> $i + 1) {$c = $str [$i]. $str [$i +1];} $i + +;} else {$c = $str [$i];} if ($i >= $enddd) {if (strlen ($RESTR) +strlen ($c) > $slen) {break,} else {$restr. = $c; break;}}} return $RESTR;} /*** utf-8 Chinese Intercept, single byte capture mode * * @access public* @param string $str required to intercept * @param int $slen intercept length * @param int $STARTDD start tag * @return string*/static Public function Cn_substr_utf8 ($str, $length, $start =0) {if (strlen ($STR) < $start + 1) {return ';} Preg_match_all ("/./su", $str, $ar); $str = "; $tstr =";//in order to be compatible with the database varchar mysql4.1 The following versions, use Byte intercept for ($i =0; Isset ($ar [0] [$i]); $i + +) {if (strlen ($TSTR) < $start) {$tstr. = $ar [0][$i];} else {if (strlen ($STR) < $length + strlen ($ar [0][$i]) {$st R. = $ar [0][$i];} else {break;}}} return $STR;} /** * Delete picture, according to picture ID * @param int $image _id */static function Delpicbyimageid ($image _id) {$db _name = PREFIX. ' Images i '; $m = new Model (); $data = $m->getone ($db _name, "i.id={$image _id}", "I.path as P, i.big_img as B, i.small_img As S "), foreach ($data as $v) {@self::d elfile (root_path. $v [' P ']); @self::d elfile (root_path. $v [' B ']); @self::d Elfile ( Root_path. $v [' s ']);} $m->del (PREFIX. ' Images ', "id={$image _id}"); unset ($m);} /** * Picture Scaling * @param resource $im new picture resource (IMAGECREATEFROMJPEG/IMAGECreatefrompng/imagecreatefromgif) * @param int $maxwidth generate image width * @param int $maxheight generate image High * @param string $name generation Image name * @param string $filetype file type (. jpg/.gif/.png) */static public Function Resizeimage ($im, $maxwidth, $maxheight, $name , $filetype) {$pic _width = Imagesx ($im), $pic _height = Imagesy ($im), if ($maxwidth && $pic _width > $maxwidth) | | ($maxheight && $pic _height > $maxheight)) {if ($maxwidth && $pic _width> $maxwidth) {$widthratio = $maxwidth/$pic _width; $resizewidth _tag = true;} if ($maxheight && $pic _height> $maxheight) {$heightratio = $maxheight/$pic _height; $resizeheight _tag = true;} if ($resizewidth _tag && $resizeheight _tag) {if ($widthratio < $heightratio) $ratio = $widthratio; else$ratio = $ HeightRatio;} if ($resizewidth _tag &&! $resizeheight _tag) $ratio = $widthratio; if ($resizeheight _tag &&! $resizewidth _tag) $ratio = $heightratio; $newwidth = $pic _width * $ratio; $newheight = $pic _height * $ratio; if (Function_exiSTS ("imagecopyresampled")) {$newim = Imagecreatetruecolor ($newwidth, $newheight); imagecopyresampled ($newim, $im, 0,0,0,0, $newwidth, $newheight, $pic _width, $pic _height);} else {$newim = Imagecreate ($newwidth, $newheight); imagecopyresized ($newim, $im, 0,0,0,0, $newwidth, $newheight, $pic _ width, $pic _height);} $name = $name. $filetype; imagejpeg ($newim, $name); Imagedestroy ($newim);} else {$name = $name. $filetype; imagejpeg ($im, $name);}} /** * Download File * @param string $file _path absolute path */static public function downfile ($file _path) {//Determine if file exists $file_path = iconv (' UT F-8 ', ' gb2312 ', $file _path); Transcode If the Chinese name may appear (!file_exists ($file _path)) {exit (' file does not exist!) ');} $file _name = basename ($file _path); Get file name $file_size = filesize ($file _path); Get file Size $fp = fopen ($file _path, ' R '); Open the file header ("Content-type:application/octet-stream") in a read-only manner; header ("Accept-ranges:bytes"); Header (" Accept-length: {$file _size} "), Header (" Content-disposition:attachment;filename={$file _name} "); $buffer = 1024x768; $file _count = 0;//Determines whether the file is over while (!feof ($fp) && ($file _size-$file _count>0) {$file _data = fread ($fp, $buffer); $file _count + = $buffer; echo $file _data;} Fclose ($FP); Close File}}?>
?