PHP Common Tools class Daquan, Tools category Daquan
0)); $i + +) {if ($number = Strpos (Str_pad (Decbin (substr ($string, $i, 1)), 8, ' 0 ', str_pad_left), ' 0 ')) {if ($length < 1.0) {break;} $result. = substr ($string, $i, $number); $length-= 1.0; $i + = $number-1;} else {$result. = substr ($string, $i, 1); $length-= 0.5;}} $result = Htmlspecialchars ($result, ent_quotes, ' UTF-8 '), if ($i < $strlen) {$result. = $etc;} return $result;} /*** Traversal folder * @param string $dir * @param boolean $all true means recursive traversal * @return array*/public static function scanfdir ($dir = ', $ All = False, & $ret = Array ()) {if (false!== ($handle = Opendir ($dir))) {while (false!== ($file = Readdir ($hand Le)) {if (!in_array ($file, Array ('. ', '. ', '. Git ', '. Gitignore ', '. svn ', '. htaccess ', '. BuildPath ', '. Project ')) {$cur _path = $dir. '/' . $file, if (Is_dir ($cur _path)) {$ret [' dirs '] [] = $cur _path; $all && self::scanfdir ($cur _path, $all, $ret);} else { $ret [' Files '] [] = $cur _path;}}} Closedir ($handle);} return $ret;} /*** Mail Send * @parAM String $toemail * @param string $subject * @param string $message * @return boolean*/public static function SendMail ($toem ail = ', $subject = ', $message = ') {$mailer = yii::createcomponent (' Application.extensions.mailer.EMailer ');//Mail configuration $mailer->setlanguage (' zh_cn '); $mailer->host = Yii::app ()->params[' emailhost ']; Outgoing mail Server $mailer->port = Yii::app ()->params[' Emailport ']; Mail Port $mailer->timeout = Yii::app ()->params[' emailtimeout '];//mail send time-out $mailer->contenttype = ' text/html ' ;//Set HTML format $mailer->smtpauth = true; $mailer->username = Yii::app ()->params[' emailusername ']; $mailer Password = Yii::app ()->params[' Emailpassword ']; $mailer->issmtp (); $mailer->from = $mailer->username; Sender Mailbox $mailer->fromname = Yii::app ()->params[' emailformname ']; Sender name $mailer->addreplyto ($mailer->username), $mailer->charset = ' UTF-8 ';//Add mail log $modelmail = new Maillog ( ); $modelMail->accept = $toemail; $modelMail->subject = $subject; $modElmail->message = $message; $modelMail->send_status = ' waiting '; $modelMail->save ();//Send mail $mailer-> AddAddress ($toemail); $mailer->subject = $subject; $mailer->body = $message; if ($mailer->send () = = = True) {$mod Elmail->times = $modelMail->times + 1; $modelMail->send_status = ' success '; $modelMail->save (); return true;} else {$error = $mailer->errorinfo; $modelMail->times = $modelMail->times + 1; $modelMail->send_status = ' Failed '; $modelMail->error = $error; $modelMail->save (); return false;}} /*** determines whether the string is Utf-8 or gb2312* @param unknown $str * @param string $default * @return string*/public static function utf8_gb2312 ($str, $default = ' gb2312 ') {$str = preg_replace ("/[\x01-\x7f]+/", "", $str); if (empty ($STR)) return $default; $preg = Array ("gb2312" = "/^ ([\xa1-\xf7][\xa0-\xfe]) +$/",//Regular judgment is gb2312 "utf-8" = "/^[\x{4e00}-\x{" 9fa5}]+$/u ",//regular judgment whether it is a Chinese character (UTF8 encoding conditions), this range has actually included the traditional Chinese characters); if ($default = = ' gb2312 '){$option = ' utf-8 '; } else {$option = ' gb2312 '; } if (!preg_match ($preg [$default], $str)) {return $option; } $str = @iconv ($default, $option, $STR); Cannot be turned into $option, stating that the original is not $default if (empty ($STR)) {return $option; } return $default;} /*** Utf-8 and gb2312 automatic conversion * @param unknown $string * @param string $outEncoding * @return unknown|string*/public static function Safeencoding ($string, $outEncoding = ' UTF-8 ') {$encoding = "UTF-8"; for ($i = 0; $i < strlen ($string); $i + +) {if (ord ($string {$i}) < Continue;if ((Ord ($string {$i}) & 224) = = 224) {///The first byte is judged by $char = $string {+ + $i};if ( Ord ($char) & 128) = = 128) {//second byte is judged by $char = $string {+ + $i};if (ord ($char) & x = =) {$encoding = " UTF-8 "; break;}}} if ((Ord ($string {$i}) & 192) = = 192) {//The first byte is judged by $char = $string {+ + $i};if (ord ($char) & 128) = = 128) {/ /The second byte is judged by $encoding = "GB2312"; if (Strtoupper ($encoding) = = Strtoupper ($outEncoDing) return $string; Elsereturn @iconv ($encoding, $outEncoding, $string);} /*** returns all values of a key name in a two-dimensional array * @param input $array * @param string $key * @return array*/public static function Array_key_values ($arr Ay =array (), $key = ") {$ret = array (); foreach (array) $array as $k = = $v) {$ret [$k] = $v [$key];} return $ret;} /*** determine if the file/directory is writable (replacing the system's own is_writeable function) * @param string $file File/directory * @return boolean*/public static function Is_writeab Le ($file) {if (Is_dir ($file)) {$dir = $file, if ($fp = @fopen ("$dir/test.txt", ' W ')) {@fclose ($fp); @unlink ("$dir/test.txt "); $writeable = 1;} else {$writeable = 0;}} else {if ($fp = @fopen ($file, ' A + ')) {@fclose ($fp); $writeable = 1;} else {$writeable = 0;}} return $writeable;} /*** formatted Unit */static public function Byteformat ($size, $dec = 2) {$a = array ("B", "KB", "MB", "GB", "TB", "PB"); $p OS = 0;while ($size >=) {$size/= 1024x768; $pos + +;} Return round ($size, $dec). " " . $a [$pos];} /*** drop-down box, the radio button automatically selects * * @param $string input characters * @param $param conditions * @param $typE type * Selected checked* @return string*/static public Function selected ($string, $param = 1, $type = ' select ') {$true = False;if (Is_array ($param)) {$true = In_array ($string, $param);} ElseIf ($string = = $param) {$true = true;} $return = '; if ($true) $return = $type = = ' SELECT '? ' Selected= ' selected ': ' checked= ' checked '; echo $return;} /*** Download Remote picture * @param string $url Absolute url* of the picture @param the full path of the string $filepath file (for example,/www/images/test), This function automatically determines the suffix name of the picture based on the picture URL and the HTTP header information * @param string $filename The file name to save (without extension) * @return mixed download successfully returns an array describing the picture information, and the download fails to return false*/ static public Function Downloadimage ($url, $filepath, $filename) {//The header information returned by the server $responseheaders = Array ();//original picture name $ OriginalFilename = ";//the suffix of the picture $ext ="; $ch = Curl_init ($url);//Set Curl_exec The value returned contains the HTTP header curl_setopt ($ch, Curlopt_header , 1);//Set Curl_exec The value returned contains the HTTP content curl_setopt ($ch, Curlopt_returntransfer, 1);//Set the page after fetching jump (http 301,302) curl_setopt ($ CH, curlopt_followlocation, 1);//Set the maximum number of HTTP redirects curl_setopt ($ch, Curlopt_maxredirs, 3);//data returned by the server(including HTTP header information and content) $html = curl_exec ($ch);//Get information about this crawl $httpinfo = Curl_getinfo ($ch); Curl_close ($ch); if ($html!== false) { Detach response header and body, because the server may use 302 jump, so here need to separate the string into the number of $httparr = explode ("\r\n\r\n", $html, 2 + $httpinfo [' Redirect_count ']);//The last response of the server is the HTTP header $header = $HTTPARR [Count ($HTTPARR)-2];//is the last time the server response content $ BODY = $HTTPARR [Count ($HTTPARR)-1]; $header. = "\ r \ n";//Gets header information for the last response Preg_match_all ('/([a-z0-9-_]+): \s* ([ ^\r\n]+) \r\n/i ', $header, $matches); if (!empty ($matches) && count ($matches) = = 3 &&!empty ($matches [1]) &&!empty ($matches [1])) {for ($i = 0; $i < count ($matches [1]), $i + +) {if (Array_key_exists ($i, $matches [2])) {$ responseheaders[$matches [1][$i]] = $matches [2][$i];}} Gets the picture suffix name if (0 < Preg_match ('?: [^\/\\\\]+) \. ( jpg|jpeg|gif|png|bmp) $}i ', $url, $matches)) {$originalfilename = $matches [0]; $ext = $matches [1];} else {if (array_key_ Exists (' Content-type ', $responseHeaders)) {if (0 < Preg_match (' {' {image/(\w+)}i ', $responseHeaders [' Content-type '], $extmatches)) {$ext = $extmatches [1];}} Save the file if (!empty ($ext)) {//If the directory does not exist, first create the directory if (!is_dir ($filepath)) {mkdir ($filepath, 0777, true);} $filepath. = '/'. $filename. ". $ext "; $local _file = fopen ($filepath, ' W '), if (false!== $local _file) {if (False!== fwrite ($local _file, $body)) {fclose ($l Ocal_file); $sizeinfo = getimagesize ($filepath); return array (' filepath ' = Realpath ($filepath), ' width ' = = $ Sizeinfo[0], ' height ' = $sizeinfo [1], ' orginalfilename ' + $originalfilename, ' filename ' = PathInfo ($ filepath, pathinfo_basename));}}} return false;} /*** Find out if IP is in a field * @param string $ip the ip* to query @param $arrIP Forbidden ip* @return boolean*/public static function ipaccess ($i p= ' 0.0.0.0 ', $arrIP = Array ()) {$access = true; $ip && $arr _cur_ip = Explode ('. ', $ip); foreach ((array) $arrIP as $key = = $value) {if ($value = = ' *.*.*.* ') {$access = false;//disable all break;} $tmp _arr = Explode ('. ', $value), if ($arr _cur_ip[0] = = $tmp _arr[0]) && ($arr _CUR_IP[1] = = $tmp _arr[1]) {//The first two paragraphs are the same if (($arr _cur_ip[2] = = $tmp _arr[2]) | | ($tmp _arr[2] = = ' * ')) {//The third paragraph is * or the same if (($arr _cur_ip[3] = = $tmp _arr[3]) | | ($tmp _arr[3] = = ' * ')) {//The fourth paragraph is * or the same $access = false;//disable access to break in the Forbidden IP column;}}} return $access;} /*** @param string $string original text or ciphertext * @param string $operation operation (ENCODE | DECODE), the default is decode* @param string $key key * @param int $expiry ciphertext expiration, valid for encryption, unit seconds, 0 for permanent active * @return The original text after string processing or BA Se64_encode Processed Ciphertext * * * @example * * $a = authcode (' abc ', ' Encode ', ' key '); * $b = Authcode ($a, ' DECODE ', ' key '); $b (ABC) * * $a = authcode (' abc ', ' ENCODE ', ' key ', 3600); * $b = Authcode (' abc ', ' DECODE ', ' key '); Within one hours, $b (ABC), otherwise $b null */public static function Authcode ($string, $operation = ' DECODE ', $key = ', $expiry = 3600) {$c Key_length = 4;//with the key length value 0-32;//join with the secret key, can make ciphertext no rules, even if the original and key exactly the same, the encryption results will be different each time, increase the difficulty of cracking. The greater the value, the greater the ciphertext variation, the ciphertext change = 16 $ckey _length//When this value is 0 o'clock, the random key $key = MD5 ($key? $key: ' key ') is not generated; Here you can fill in the default key value $keya = MD5 (substr ($key, 0, +)); $keYB = MD5 (substr ($key, +)); $KEYC = $ckey _length? ($operation = = ' DECODE '? substr ($string, 0, $ckey _length): substr (MD5 (Microtime ()),-$ckey _length)): "; $cry Ptkey = $keya. MD5 ($keya. $KEYC); $key _length = strlen ($cryptkey); $string = = $operation = = ' DECODE '? Base64_decode (substr ($string, $ckey _length)): sprintf ('%010d ', $expiry? $expiry + Time (): 0). SUBSTR (MD5 ($string. $keyb), 0, 16). $string; $string _length = strlen ($string); $result = "; $box = range (0, 255); $rndkey = Array (); for ($i = 0; $i <= 2 55; $i + +) {$rndkey [$i] = Ord ($cryptkey [$i% $key _length]);} for ($j = $i = 0; $i < $i + +) {$j = ($j + $box [$i] + $rndkey [$i])%, $tmp = $box [$i]; $box [$i] = $box [$j]; $b Ox [$j] = $tmp;} for ($a = $j = $i = 0; $i < $string _length; $i + +) {$a = ($a + 1)%, $j = ($j + $box [$a])%, $tmp = $box [$a]; $bo x [$a] = $box [$j]; $box [$j] = $tmp; $result. = Chr (ord ($string [$i]) ^ ($box [($box [$a] + $box [$j])% 256]));} if ($operation = = ' DECODE ') {if (substr ($result, 0, ten) = = 0 | | substr ($result, 0,)-time () > 0) && SUBSTR ($result, ten, +) = = substr (MD5 ($result, $keyb), 0, +) {return substr ($result, 26);} else {return ';}} else {return $KEYC. Str_replace (' = ', ' ', Base64_encode ($result));}} public static function GbkToUtf8 ($STR) {return iconv ("GBK", "UTF-8", $str);} /*** get all directories and files included in the input directory * Return as an associative array * author:flynetcn*/static public function Deepscandir ($dir) {$fileArr = array (); $ Dirarr = Array (), $dir = RTrim ($dir, '//'), if (Is_dir ($dir)) {$dirHandle = Opendir ($dir), while (false!== ($fileName = Readdi R ($dirHandle))) {$subFile = $dir. Directory_separator. $fileName, if (Is_file ($subFile)) {$FILEARR [] = $subFile;} elseif (Is_dir ($subFile) && str_replace ('. ', ', $ fileName) = ') {$DIRARR [] = $subFile; $arr = self::d eepscandir ($subFile); $dirArr = Array_merge ($DIRARR, $arr [' dir ']); $ Filearr = Array_merge ($FILEARR, $arr [' file ']);}} Closedir ($diRhandle);} Return Array (' dir ' = = $dirArr, ' file ' = = $FILEARR);} /*** get all the files contained in the input directory * return as an array * author:flynetcn*/static public function get_dir_files ($dir) {if (Is_file ($dir)) {return Array ($dir);} $files = Array (), if (Is_dir ($dir) && ($dir _p = Opendir ($dir))) {$ds = Directory_separator;while (($filename = read Dir ($dir _p))!== false) {if ($filename = = '. ' | | $filename = = ' ... ') {continue;} $filetype = filetype ($dir. $ds. $filename); if ($filetype = = ' dir ') {$files = Array_merge ($files, Self::get_dir_files ($ Dir. $ds. $filename));} ElseIf ($filetype = = ' file ') {$files [] = $dir. $ds. $filename;}} Closedir ($dir _p);} return $files;}}
Too much more than 1000 lines to save, view full and download
Download Address: http://www.shouce.ren/post/d/id/1700
http://www.bkjia.com/PHPjc/1078068.html www.bkjia.com true http://www.bkjia.com/PHPjc/1078068.html techarticle PHP Common Tools Class Daquan, Tools encyclopedia? php/** * Helper class * @author Www.shouce.ren * */class helper{/*** Judging current server system * @return string*/public static function GE ...