<div id="" class="dp-highlighter"> <div class="bar"> <div class="tools">Php代码 <a title="收藏这段代码"><img class="star" src="http://shouce.iteye.com/images/icon_star.png" alt="收藏代码"></a> </div> </div> <ol class="dp-c" start="1"> <li> <?php </li> </li> <li> <span class="comment">/** </span> </li> <li><span class="comment"> * 助手类 </span></li> <li><span class="comment"> * @author www.shouce.ren </span></li> <li><span class="comment"> * </span></li> <li><span class="comment"> */ </span></li> <li> <span class="keyword">class Helper </span> </li> <li> { </li> <li> <span class="comment">/** </span> </li> <li><span class="comment"> * 判断当前服务器系统 </span></li> <li><span class="comment"> * @return string </span></li> <li><span class="comment"> */ </span></li> <li> <span class="keyword">public <span class="keyword">static <span class="keyword">function getOS(){ </span></span></span> </li> <li> <span class="keyword">if(PATH_SEPARATOR == <span class="string">':'){ </span></span> </li> <li> <span class="keyword">return <span class="string">'Linux'; </span></span> </li> <li> }<span class="keyword">else{ </span> </li> <li> <span class="keyword">return <span class="string">'Windows'; </span></span> </li> <li> } </li> <li> } </li> <li> <span class="comment">/** </span> </li> <li><span class="comment"> * 当前微妙数 </span></li> <li><span class="comment"> * @return number </span></li> <li><span class="comment"> */ </span></li> <li> <span class="keyword">public <span class="keyword">static <span class="keyword">function microtime_float() { </span></span></span> </li> <li> list ( <span class="vars">$usec, <span class="vars">$sec ) = <span class="func">explode ( <span class="string">" ", microtime () ); </span></span></span></span> </li> <li> <span class="keyword">return (( float ) <span class="vars">$usec + ( float ) <span class="vars">$sec); </span></span></span> </li> <li> } </li> <li> <span class="comment">/** </span> </li> <li><span class="comment"> * 切割utf-8格式的字符串(一个汉字或者字符占一个字节) </span></li> <li><span class="comment"> * </span></li> <li><span class="comment"> * @author zhao jinhan </span></li> <li><span class="comment"> * @version v1.0.0 </span></li> <li><span class="comment"> * </span></li> <li><span class="comment"> */ </span></li> <li> <span class="keyword">public <span class="keyword">static <span class="keyword">function truncate_utf8_string(<span class="vars">$string, <span class="vars">$length, <span class="vars">$etc = <span class="string">'...') { </span></span></span></span></span></span></span> </li> <li> <span class="vars">$result = <span class="string">''; </span></span> </li> <li> <span class="vars">$string = html_entity_decode ( trim ( <span class="func">strip_tags ( <span class="vars">$string ) ), ENT_QUOTES, <span class="string">'UTF-8' ); </span></span></span></span> </li> <li> <span class="vars">$strlen = <span class="func">strlen ( <span class="vars">$string ); </span></span></span> </li> <li> <span class="keyword">for(<span class="vars">$i = 0; ((<span class="vars">$i $strlen) && (<span class="vars">$length > 0)); <span class="vars">$i ++) { </span></span></span></span></span> </li> <li> <span class="keyword">if (<span class="vars">$number = <span class="func">strpos ( <span class="func">str_pad ( <span class="func">decbin ( ord ( <span class="func">substr ( <span class="vars">$string, <span class="vars">$i, 1 ) ) ), 8, <span class="string">'0', STR_PAD_LEFT ), <span class="string">'0' )) { </span></span></span></span></span></span></span></span></span></span> </li> <li> <span class="keyword">if (<span class="vars">$length </span></span> </li> <li> <span class="keyword">break; </span> </li> <li> } </li> <li> <span class="vars">$result .= <span class="func">substr ( <span class="vars">$string, <span class="vars">$i, <span class="vars">$number ); </span></span></span></span></span> </li> <li> <span class="vars">$length -= 1.0; </span> </li> <li> <span class="vars">$i += <span class="vars">$number - 1; </span></span> </li> <li> } <span class="keyword">else { </span> </li> <li> <span class="vars">$result .= <span class="func">substr ( <span class="vars">$string, <span class="vars">$i, 1 ); </span></span></span></span> </li> <li> <span class="vars">$length -= 0.5; </span> </li> <li> } </li> <li> } </li> <li> <span class="vars">$result = htmlspecialchars ( <span class="vars">$result, ENT_QUOTES, <span class="string">'UTF-8' ); </span></span></span> </li> <li> <span class="keyword">if (<span class="vars">$i $strlen) { </span></span> </li> <li> <span class="vars">$result .= <span class="vars">$etc; </span></span> </li> <li> } </li> <li> <span class="keyword">return <span class="vars">$result; </span></span> </li> <li> } </li> <li> <span class="comment">/** </span> </li> <li><span class="comment"> * 遍历文件夹 </span></li> <li><span class="comment"> * @param string $dir </span></li> <li><span class="comment"> * @param boolean $all true表示递归遍历 </span></li> <li><span class="comment"> * @return array </span></li> <li><span class="comment"> */ </span></li> <li> <span class="keyword">public <span class="keyword">static <span class="keyword">function scanfDir(<span class="vars">$dir=<span class="string">'', <span class="vars">$all = false, &<span class="vars">$ret = <span class="keyword">array()){ </span></span></span></span></span></span></span></span> </li> <li> <span class="keyword">if ( false !== (<span class="vars">$handle = opendir ( <span class="vars">$dir ))) { </span></span></span> </li> <li> <span class="keyword">while ( false !== (<span class="vars">$file = readdir ( <span class="vars">$handle )) ) { </span></span></span> </li> <li> <span class="keyword">if (!in_array(<span class="vars">$file, <span class="keyword">array(<span class="string">'.', <span class="string">'..', <span class="string">'.git', <span class="string">'.gitignore', <span class="string">'.svn', <span class="string">'.htaccess', <span class="string">'.buildpath',<span class="string">'.project'))) { </span></span></span></span></span></span></span></span></span></span></span> </li> <li> <span class="vars">$cur_path = <span class="vars">$dir . <span class="string">'/' . <span class="vars">$file; </span></span></span></span> </li> <li> <span class="keyword">if (<span class="func">is_dir ( <span class="vars">$cur_path )) { </span></span></span> </li> <li> <span class="vars">$ret[<span class="string">'dirs'][] =<span class="vars">$cur_path; </span></span></span> </li> <li> <span class="vars">$all && self::scanfDir( <span class="vars">$cur_path, <span class="vars">$all, <span class="vars">$ret); </span></span></span></span> </li> <li> } <span class="keyword">else { </span> </li> <li> <span class="vars">$ret [<span class="string">'files'] [] = <span class="vars">$cur_path; </span></span></span> </li> <li> } </li> <li> } </li> <li> } </li> <li> <span class="func">closedir ( <span class="vars">$handle ); </span></span> </li> <li> } </li> <li> <span class="keyword">return <span class="vars">$ret; </span></span> </li> <li> } </li> <li> <span class="comment">/** </span> </li> <li><span class="comment"> * 邮件发送 </span></li> <li><span class="comment"> * @param string $toemail </span></li> <li><span class="comment"> * @param string $subject </span></li> <li><span class="comment"> * @param string $message </span></li> <li><span class="comment"> * @return boolean </span></li> <li><span class="comment"> */ </span></li> <li> <span class="keyword">public <span class="keyword">static <span class="keyword">function sendMail(<span class="vars">$toemail = <span class="string">'', <span class="vars">$subject = <span class="string">'', <span class="vars">$message = <span class="string">'') { </span></span></span></span></span></span></span></span></span> </li> <li> <span class="vars">$mailer = Yii::createComponent ( <span class="string">'application.extensions.mailer.EMailer' ); </span></span> </li> <li> <span class="comment">//邮件配置 </span> </li> <li> <span class="vars">$mailer->SetLanguage(<span class="string">'zh_cn'); </span></span> </li> <li> <span class="vars">$mailer->Host = Yii::app()->params[<span class="string">'emailHost']; <span class="comment">//发送邮件服务器 </span></span></span> </li> <li> <span class="vars">$mailer->Port = Yii::app()->params[<span class="string">'emailPort']; <span class="comment">//邮件端口 </span></span></span> </li> <li> <span class="vars">$mailer->Timeout = Yii::app()->params[<span class="string">'emailTimeout'];<span class="comment">//邮件发送超时时间 </span></span></span> </li> <li> <span class="vars">$mailer->ContentType = <span class="string">'text/html';<span class="comment">//设置html格式 </span></span></span> </li> <li> <span class="vars">$mailer->SMTPAuth = true; </span> </li> <li> <span class="vars">$mailer->Username = Yii::app()->params[<span class="string">'emailUserName']; </span></span> </li> <li> <span class="vars">$mailer->Password = Yii::app()->params[<span class="string">'emailPassword']; </span></span> </li> <li> <span class="vars">$mailer->IsSMTP (); </span> </li> <li> <span class="vars">$mailer->From = <span class="vars">$mailer->Username; <span class="comment">// 发件人邮箱 </span></span></span> </li> <li> <span class="vars">$mailer->FromName = Yii::app()->params[<span class="string">'emailFormName']; <span class="comment">// 发件人姓名 </span></span></span> </li> <li> <span class="vars">$mailer->AddReplyTo ( <span class="vars">$mailer->Username ); </span></span> </li> <li> <span class="vars">$mailer->CharSet = <span class="string">'UTF-8'; </span></span> </li> <li> <span class="comment">// 添加邮件日志 </span> </li> <li> <span class="vars">$modelMail = <span class="keyword">new MailLog (); </span></span> </li> <li> <span class="vars">$modelMail->accept = <span class="vars">$toemail; </span></span> </li> <li> <span class="vars">$modelMail->subject = <span class="vars">$subject; </span></span> </li> <li> <span class="vars">$modelMail->message = <span class="vars">$message; </span></span> </li> <li> <span class="vars">$modelMail->send_status = <span class="string">'waiting'; </span></span> </li> <li> <span class="vars">$modelMail->save (); </span> </li> <li> <span class="comment">// 发送邮件 </span> </li> <li> <span class="vars">$mailer->AddAddress ( <span class="vars">$toemail ); </span></span> </li> <li> <span class="vars">$mailer->Subject = <span class="vars">$subject; </span></span> </li> <li> <span class="vars">$mailer->Body = <span class="vars">$message; </span></span> </li> <li> <span class="keyword">if (<span class="vars">$mailer->Send () === true) { </span></span> </li> <li> <span class="vars">$modelMail->times = <span class="vars">$modelMail->times + 1; </span></span> </li> <li> <span class="vars">$modelMail->send_status = <span class="string">'success'; </span></span> </li> <li> <span class="vars">$modelMail->save (); </span> </li> <li> <span class="keyword">return true; </span> </li> <li> } <span class="keyword">else { </span> </li> <li> <span class="vars">$error = <span class="vars">$mailer->ErrorInfo; </span></span> </li> <li> <span class="vars">$modelMail->times = <span class="vars">$modelMail->times + 1; </span></span> </li> <li> <span class="vars">$modelMail->send_status = <span class="string">'failed'; </span></span> </li> <li> <span class="vars">$modelMail->error = <span class="vars">$error; </span></span> </li> <li> <span class="vars">$modelMail->save (); </span> </li> <li> <span class="keyword">return false; </span> </li> <li> } </li> <li> } </li> <li> <span class="comment">/** </span> </li> <li><span class="comment"> * 判断字符串是utf-8 还是gb2312 </span></li> <li><span class="comment"> * @param unknown $str </span></li> <li><span class="comment"> * @param string $default </span></li> <li><span class="comment"> * @return string </span></li> <li><span class="comment"> */ </span></li> <li> <span class="keyword">public <span class="keyword">static <span class="keyword">function utf8_gb2312(<span class="vars">$str, <span class="vars">$default = <span class="string">'gb2312') </span></span></span></span></span></span> </li> <li> { </li> <li> <span class="vars">$str = preg_replace(<span class="string">"/[\x01-\x7F]+/", <span class="string">"", <span class="vars">$str); </span></span></span></span> </li> <li> <span class="keyword">if (<span class="func">empty<span class="keyword">empty(<span class="vars">$str)) <span class="keyword">return <span class="vars">$default; </span></span></span></span></span></span> </li> <li> <span class="vars">$preg = <span class="keyword">array( </span></span> </li> <li> <span class="string">"gb2312" => <span class="string">"/^([\xA1-\xF7][\xA0-\xFE])+$/", <span class="comment">//正则判断是否是gb2312 </span></span></span> </li> <li> <span class="string">"utf-8" => <span class="string">"/^[\x{4E00}-\x{9FA5}]+$/u", <span class="comment">//正则判断是否是汉字(utf8编码的条件了),这个范围实际上已经包含了繁体中文字了 </span></span></span> </li> <li> ); </li> <li> <span class="keyword">if (<span class="vars">$default == <span class="string">'gb2312') { </span></span></span> </li> <li> <span class="vars">$option = <span class="string">'utf-8'; </span></span> </li> <li> } <span class="keyword">else { </span> </li> <li> <span class="vars">$option = <span class="string">'gb2312'; </span></span> </li> <li> } </li> <li> <span class="keyword">if (!preg_match(<span class="vars">$preg[<span class="vars">$default], <span class="vars">$str)) { </span></span></span></span> </li> <li> <span class="keyword">return <span class="vars">$option; </span></span> </li> <li> } </li> <li> <span class="vars">$str = @iconv(<span class="vars">$default, <span class="vars">$option, <span class="vars">$str); </span></span></span></span> </li> <li> <span class="comment">//不能转成 $option, 说明原来的不是 $default </span> </li> <li> <span class="keyword">if (<span class="keyword">empty<span class="func">empty(<span class="vars">$str)) { </span></span></span></span> </li> <li> <span class="keyword">return <span class="vars">$option; </span></span> </li> <li> } </li> <li> <span class="keyword">return <span class="vars">$default; </span></span> </li> <li> } </li> <li> <span class="comment">/** </span> </li> <li><span class="comment"> * utf-8和gb2312自动转化 </span></li> <li><span class="comment"> * @param unknown $string </span></li> <li><span class="comment"> * @param string $outEncoding </span></li> <li><span class="comment"> * @return unknown|string </span></li> <li><span class="comment"> */ </span></li> <li> <span class="keyword">public <span class="keyword">static <span class="keyword">function safeEncoding(<span class="vars">$string,<span class="vars">$outEncoding = <span class="string">'UTF-8') </span></span></span></span></span></span> </li> <li> { </li> <li> <span class="vars">$encoding = <span class="string">"UTF-8"; </span></span> </li> <li> <span class="keyword">for(<span class="vars">$i = 0; <span class="vars">$i strlen ( <span class="vars">$string ); <span class="vars">$i ++) { </span></span></span></span></span> </li> <li> <span class="keyword">if (ord ( <span class="vars">$string {<span class="vars">$i} ) </span></span></span> </li> <li> <span class="keyword">continue; </span> </li> <li> <span class="keyword">if ((ord ( <span class="vars">$string {<span class="vars">$i} ) & 224) == 224) { </span></span></span> </li> <li> <span class="comment">// 第一个字节判断通过 </span> </li> <li> <span class="vars">$char = <span class="vars">$string {++ <span class="vars">$i}; </span></span></span> </li> <li> <span class="keyword">if ((ord ( <span class="vars">$char ) & 128) == 128) { </span></span> </li> <li> <span class="comment">// 第二个字节判断通过 </span> </li> <li> <span class="vars">$char = <span class="vars">$string {++ <span class="vars">$i}; </span></span></span> </li> <li> <span class="keyword">if ((ord ( <span class="vars">$char ) & 128) == 128) { </span></span> </li> <li> <span class="vars">$encoding = <span class="string">"UTF-8"; </span></span> </li> <li> <span class="keyword">break; </span> </li> <li> } </li> <li> } </li> <li> } </li> <li> <span class="keyword">if ((ord ( <span class="vars">$string {<span class="vars">$i} ) & 192) == 192) { </span></span></span> </li> <li> <span class="comment">// 第一个字节判断通过 </span> </li> <li> <span class="vars">$char = <span class="vars">$string {++ <span class="vars">$i}; </span></span></span> </li> <li> <span class="keyword">if ((ord ( <span class="vars">$char ) & 128) == 128) { </span></span> </li> <li> <span class="comment">// 第二个字节判断通过 </span> </li> <li> <span class="vars">$encoding = <span class="string">"GB2312"; </span></span> </li> <li> <span class="keyword">break; </span> </li> <li> } </li> <li> } </li> <li> } </li> <li> <span class="keyword">if (<span class="func">strtoupper ( <span class="vars">$encoding ) == <span class="func">strtoupper ( <span class="vars">$outEncoding )) </span></span></span></span></span> </li> <li> <span class="keyword">return <span class="vars">$string; </span></span> </li> <li> <span class="keyword">else </span> </li> <li> <span class="keyword">return @iconv ( <span class="vars">$encoding, <span class="vars">$outEncoding, <span class="vars">$string ); </span></span></span></span> </li> <li> } </li> <li> <span class="comment">/** </span> </li> <li><span class="comment"> * 返回二维数组中某个键名的所有值 </span></li> <li><span class="comment"> * @param input $array </span></li> <li><span class="comment"> * @param string $key </span></li> <li><span class="comment"> * @return array </span></li> <li><span class="comment"> */ </span></li> <li> <span class="keyword">public <span class="keyword">static <span class="keyword">function array_key_values(<span class="vars">$array =<span class="keyword">array(), <span class="vars">$key=<span class="string">'') </span></span></span></span></span></span></span> </li> <li> { </li> <li> <span class="vars">$ret = <span class="keyword">array(); </span></span> </li> <li> <span class="keyword">foreach((<span class="keyword">array)<span class="vars">$array <span class="keyword">as <span class="vars">$k=><span class="vars">$v){ </span></span></span></span></span></span> </li> <li> <span class="vars">$ret[<span class="vars">$k] = <span class="vars">$v[<span class="vars">$key]; </span></span></span></span> </li> <li> } </li> <li> <span class="keyword">return <span class="vars">$ret; </span></span> </li> <li> } </li> <li> <span class="comment">/** </span> </li> <li><span class="comment"> * 判断 文件/目录 是否可写(取代系统自带的 is_writeable 函数) </span></li> <li><span class="comment"> * @param string $file 文件/目录 </span></li> <li><span class="comment"> * @return boolean </span></li> <li><span class="comment"> */ </span></li> <li> <span class="keyword">public <span class="keyword">static <span class="keyword">function <span class="func">is_writeable(<span class="vars">$file) { </span></span></span></span></span> </li> <li> <span class="keyword">if (<span class="func">is_dir(<span class="vars">$file)){ </span></span></span> </li> <li> <span class="vars">$dir = <span class="vars">$file; </span></span> </li> <li> <span class="keyword">if (<span class="vars">$fp = @<span class="func">fopen(<span class="string">"$dir/test.txt", <span class="string">'w')) { </span></span></span></span></span> </li> <li> @fclose(<span class="vars">$fp); </span> </li> <li> @unlink(<span class="string">"$dir/test.txt"); </span> </li> <li> <span class="vars">$writeable = 1; </span> </li> <li> } <span class="keyword">else { </span> </li> <li> <span class="vars">$writeable = 0; </span> </li> <li> } </li> <li> } <span class="keyword">else { </span> </li> <li> <span class="keyword">if (<span class="vars">$fp = @<span class="func">fopen(<span class="vars">$file, <span class="string">'a+')) { </span></span></span></span></span> </li> <li> @fclose(<span class="vars">$fp); </span> </li> <li> <span class="vars">$writeable = 1; </span> </li> <li> } <span class="keyword">else { </span> </li> <li> <span class="vars">$writeable = 0; </span> </li> <li> } </li> <li> } </li> <li> <span class="keyword">return <span class="vars">$writeable; </span></span> </li> <li> } </li> <li> <span class="comment">/** </span> </li> <li><span class="comment"> * 格式化单位 </span></li> <li><span class="comment"> */ </span></li> <li> <span class="keyword">static <span class="keyword">public <span class="keyword">function byteFormat( <span class="vars">$size, <span class="vars">$dec = 2 ) { </span></span></span></span></span> </li> <li> <span class="vars">$a = <span class="keyword">array ( <span class="string">"B" , <span class="string">"KB" , <span class="string">"MB" , <span class="string">"GB" , <span class="string">"TB" , <span class="string">"PB" ); </span></span></span></span></span></span></span></span> </li> <li> <span class="vars">$pos = 0; </span> </li> <li> <span class="keyword">while ( <span class="vars">$size >= 1024 ) { </span></span> </li> <li> <span class="vars">$size /= 1024; </span> </li> <li> <span class="vars">$pos ++; </span> </li> <li> } </li> <li> <span class="keyword">return <span class="func">round( <span class="vars">$size, <span class="vars">$dec ) . <span class="string">" " . <span class="vars">$a[<span class="vars">$pos]; </span></span></span></span></span></span></span> </li> <li> } </li> <li> <span class="comment">/** </span> </li> <li><span class="comment"> * 下拉框,单选按钮 自动选择 </span></li> <li><span class="comment"> * </span></li> <li><span class="comment"> * @param $string 输入字符 </span></li> <li><span class="comment"> * @param $param 条件 </span></li> <li><span class="comment"> * @param $type 类型 </span></li> <li><span class="comment"> * selected checked </span></li> <li><span class="comment"> * @return string </span></li> <li><span class="comment"> */ </span></li> <li> <span class="keyword">static <span class="keyword">public <span class="keyword">function selected( <span class="vars">$string, <span class="vars">$param = 1, <span class="vars">$type = <span class="string">'select' ) { </span></span></span></span></span></span></span> </li> <li> <span class="vars">$true = false; </span> </li> <li> <span class="keyword">if ( <span class="func">is_array( <span class="vars">$param ) ) { </span></span></span> </li> <li> <span class="vars">$true = in_array( <span class="vars">$string, <span class="vars">$param ); </span></span></span> </li> <li> }<span class="keyword">elseif ( <span class="vars">$string == <span class="vars">$param ) { </span></span></span> </li> <li> <span class="vars">$true = true; </span> </li> <li> } </li> <li> <span class="vars">$return=<span class="string">''; </span></span> </li> <li> <span class="keyword">if ( <span class="vars">$true ) </span></span> </li> <li> <span class="vars">$return = <span class="vars">$type == <span class="string">'select' ? <span class="string">'selected="selected"' : <span class="string">'checked="checked"'; </span></span></span></span></span> </li> <li> <span class="func">echo <span class="vars">$return; </span></span> </li> <li> } </li> <li> <span class="comment">/** </span> </li> <li><span class="comment"> * 下载远程图片 </span></li> <li><span class="comment"> * @param string $url 图片的绝对url </span></li> <li><span class="comment"> * @param string $filepath 文件的完整路径(例如/www/images/test) ,此函数会自动根据图片url和http头信息确定图片的后缀名 </span></li> <li><span class="comment"> * @param string $filename 要保存的文件名(不含扩展名) </span></li> <li><span class="comment"> * @return mixed 下载成功返回一个描述图片信息的数组,下载失败则返回false </span></li> <li><span class="comment"> */ </span></li> <li> <span class="keyword">static <span class="keyword">public <span class="keyword">function downloadImage(<span class="vars">$url, <span class="vars">$filepath, <span class="vars">$filename) { </span></span></span></span></span></span> </li> <li> <span class="comment">//服务器返回的头信息 </span> </li> <li> <span class="vars">$responseHeaders = <span class="keyword">array(); </span></span> </li> <li> <span class="comment">//原始图片名 </span> </li> <li> <span class="vars">$originalfilename = <span class="string">''; </span></span> </li> <li> <span class="comment">//图片的后缀名 </span> </li> <li> <span class="vars">$ext = <span class="string">''; </span></span> </li> <li> <span class="vars">$ch = curl_init(<span class="vars">$url); </span></span> </li> <li> <span class="comment">//设置curl_exec返回的值包含Http头 </span> </li> <li> curl_setopt(<span class="vars">$ch, CURLOPT_HEADER, 1); </span> </li> <li> <span class="comment">//设置curl_exec返回的值包含Http内容 </span> </li> <li> curl_setopt(<span class="vars">$ch, CURLOPT_RETURNTRANSFER, 1); </span> </li> <li> <span class="comment">//设置抓取跳转(http 301,302)后的页面 </span> </li> <li> curl_setopt(<span class="vars">$ch, CURLOPT_FOLLOWLOCATION, 1); </span> </li> <li> <span class="comment">//设置最多的HTTP重定向的数量 </span> </li> <li> curl_setopt(<span class="vars">$ch, CURLOPT_MAXREDIRS, 3); </span> </li> <li> <span class="comment">//服务器返回的数据(包括http头信息和内容) </span> </li> <li> <span class="vars">$html = curl_exec(<span class="vars">$ch); </span></span> </li> <li> <span class="comment">//获取此次抓取的相关信息 </span> </li> <li> <span class="vars">$httpinfo = curl_getinfo(<span class="vars">$ch); </span></span> </li> <li> curl_close(<span class="vars">$ch); </span> </li> <li> <span class="keyword">if (<span class="vars">$html !== false) { </span></span> </li> <li> <span class="comment">//分离response的header和body,由于服务器可能使用了302跳转,所以此处需要将字符串分离为 2+跳转次数 个子串 </span> </li> <li> <span class="vars">$httpArr = <span class="func">explode(<span class="string">"\r\n\r\n", <span class="vars">$html, 2 + <span class="vars">$httpinfo[<span class="string">'redirect_count']); </span></span></span></span></span></span> </li> <li> <span class="comment">//倒数第二段是服务器最后一次response的http头 </span> </li> <li> <span class="vars">$header = <span class="vars">$httpArr[<span class="func">count(<span class="vars">$httpArr) - 2]; </span></span></span></span> </li> <li> <span class="comment">//倒数第一段是服务器最后一次response的内容 </span> </li> <li> <span class="vars">$body = <span class="vars">$httpArr[<span class="func">count(<span class="vars">$httpArr) - 1]; </span></span></span></span> </li> <li> <span class="vars">$header.=<span class="string">"\r\n"; </span></span> </li> <li> <span class="comment">//获取最后一次response的header信息 </span> </li> <li> preg_match_all(<span class="string">'/([a-z0-9-_]+):\s*([^\r\n]+)\r\n/i', <span class="vars">$header, <span class="vars">$matches); </span></span></span> </li> <li> <span class="keyword">if (!<span class="keyword">empty<span class="func">empty(<span class="vars">$matches) && <span class="func">count(<span class="vars">$matches) == 3 && !<span class="func">empty<span class="keyword">empty(<span class="vars">$matches[1]) && !<span class="keyword">empty<span class="func">empty(<span class="vars">$matches[1])) { </span></span></span></span></span></span></span></span></span></span></span></span> </li> <li> <span class="keyword">for (<span class="vars">$i = 0; <span class="vars">$i count(<span class="vars">$matches[1]); <span class="vars">$i++) { </span></span></span></span></span> </li> <li> <span class="keyword">if (<span class="func">array_key_exists(<span class="vars">$i, <span class="vars">$matches[2])) { </span></span></span></span> </li> <li> <span class="vars">$responseHeaders[<span class="vars">$matches[1][<span class="vars">$i]] = <span class="vars">$matches[2][<span class="vars">$i]; </span></span></span></span></span> </li> <li> } </li> <li> } </li> <li> } </li> <li> <span class="comment">//获取图片后缀名 </span> </li> <li> <span class="keyword">if (0 '{(?:[^\/\\\\]+)\.(jpg|jpeg|gif|png|bmp)$}i', <span class="vars">$url, <span class="vars">$matches)) { </span></span></span> </li> <li> <span class="vars">$originalfilename = <span class="vars">$matches[0]; </span></span> </li> <li> <span class="vars">$ext = <span class="vars">$matches[1]; </span></span> </li> <li> } <span class="keyword">else { </span> </li> <li> <span class="keyword">if (<span class="func">array_key_exists(<span class="string">'Content-Type', <span class="vars">$responseHeaders)) { </span></span></span></span> </li> <li> <span class="keyword">if (0 '{image/(\w+)}i', <span class="vars">$responseHeaders[<span class="string">'Content-Type'], <span class="vars">$extmatches)) { </span></span></span></span> </li> <li> <span class="vars">$ext = <span class="vars">$extmatches[1]; </span></span> </li> <li> } </li> <li> } </li> <li> } </li> <li> <span class="comment">//保存文件 </span> </li> <li> <span class="keyword">if (!<span class="func">empty<span class="keyword">empty(<span class="vars">$ext)) { </span></span></span></span> </li> <li> <span class="comment">//如果目录不存在,则先要创建目录 </span> </li> <li> <span class="keyword">if(!<span class="func">is_dir(<span class="vars">$filepath)){ </span></span></span> </li> <li> <span class="func">mkdir(<span class="vars">$filepath, 0777, true); </span></span> </li> <li> } </li> <li> <span class="vars">$filepath .= <span class="string">'/'.<span class="vars">$filename.<span class="string">".$ext"; </span></span></span></span> </li> <li> <span class="vars">$local_file = <span class="func">fopen(<span class="vars">$filepath, <span class="string">'w'); </span></span></span></span> </li> <li> <span class="keyword">if (false !== <span class="vars">$local_file) { </span></span> </li> <li> <span class="keyword">if (false !== fwrite(<span class="vars">$local_file, <span class="vars">$body)) { </span></span></span> </li> <li> fclose(<span class="vars">$local_file); </span> </li> <li> <span class="vars">$sizeinfo = <span class="func">getimagesize(<span class="vars">$filepath); </span></span></span> </li> <li> <span class="keyword">return <span class="keyword">array(<span class="string">'filepath' => <span class="func">realpath(<span class="vars">$filepath), <span class="string">'width' => <span class="vars">$sizeinfo[0], <span class="string">'height' => <span class="vars">$sizeinfo[1], <span class="string">'orginalfilename' => <span class="vars">$originalfilename, <span class="string">'filename' => <span class="func">pathinfo(<span class="vars">$filepath, PATHINFO_BASENAME)); </span></span></span></span></span></span></span></span></span></span></span></span></span></span> </li> <li> } </li> <li> } </li> <li> } </li> <li> } </li> <li> <span class="keyword">return false; </span> </li> <li> } </li> <li> <span class="comment">/** </span> </li> <li><span class="comment"> * 查找ip是否在某个段位里面 </span></li> <li><span class="comment"> * @param string $ip 要查询的ip </span></li> <li><span class="comment"> * @param $arrIP 禁止的ip </span></li> <li><span class="comment"> * @return boolean </span></li> <li><span class="comment"> */ </span></li> <li> <span class="keyword">public <span class="keyword">static <span class="keyword">function ipAccess(<span class="vars">$ip=<span class="string">'0.0.0.0', <span class="vars">$arrIP = <span class="keyword">array()){ </span></span></span></span></span></span></span> </li> <li> <span class="vars">$access = true; </span> </li> <li> <span class="vars">$ip && <span class="vars">$arr_cur_ip = <span class="func">explode(<span class="string">'.', <span class="vars">$ip); </span></span></span></span></span> </li> <li> <span class="keyword">foreach((<span class="keyword">array)<span class="vars">$arrIP <span class="keyword">as <span class="vars">$key=> <span class="vars">$value){ </span></span></span></span></span></span> </li> <li> <span class="keyword">if(<span class="vars">$value == <span class="string">'*.*.*.*'){ </span></span></span> </li> <li> <span class="vars">$access = false; <span class="comment">//禁止所有 </span></span> </li> <li> <span class="keyword">break; </span> </li> <li> } </li> <li> <span class="vars">$tmp_arr = <span class="func">explode(<span class="string">'.', <span class="vars">$value); </span></span></span></span> </li> <li> <span class="keyword">if((<span class="vars">$arr_cur_ip[0] == <span class="vars">$tmp_arr[0]) && (<span class="vars">$arr_cur_ip[1] == <span class="vars">$tmp_arr[1])) { </span></span></span></span></span> </li> <li> <span class="comment">//前两段相同 </span> </li> <li> <span class="keyword">if((<span class="vars">$arr_cur_ip[2] == <span class="vars">$tmp_arr[2]) || (<span class="vars">$tmp_arr[2] == <span class="string">'*')){ </span></span></span></span></span> </li> <li> <span class="comment">//第三段为* 或者相同 </span> </li> <li> <span class="keyword">if((<span class="vars">$arr_cur_ip[3] == <span class="vars">$tmp_arr[3]) || (<span class="vars">$tmp_arr[3] == <span class="string">'*')){ </span></span></span></span></span> </li> <li> <span class="comment">//第四段为* 或者相同 </span> </li> <li> <span class="vars">$access = false; <span class="comment">//在禁止ip列,则禁止访问 </span></span> </li> <li> <span class="keyword">break; </span> </li> <li> } </li> <li> } </li> <li> } </li> <li> } </li> <li> <span class="keyword">return <span class="vars">$access; </span></span> </li> <li> } </li> <li> <span class="comment">/** </span> </li> <li><span class="comment"> * @param string $string 原文或者密文 </span></li> <li><span class="comment"> * @param string $operation 操作(ENCODE | DECODE), 默认为 DECODE </span></li> <li><span class="comment"> * @param string $key 密钥 </span></li> <li><span class="comment"> * @param int $expiry 密文有效期, 加密时候有效, 单位 秒,0 为永久有效 </span></li> <li><span class="comment"> * @return string 处理后的 原文或者 经过 base64_encode 处理后的密文 </span></li> <li><span class="comment"> * </span></li> <li><span class="comment"> * @example </span></li> <li><span class="comment"> * </span></li> <li><span class="comment"> * $a = authcode('abc', 'ENCODE', 'key'); </span></li> <li><span class="comment"> * $b = authcode($a, 'DECODE', 'key'); // $b(abc) </span></li> <li><span class="comment"> * </span></li> <li><span class="comment"> * $a = authcode('abc', 'ENCODE', 'key', 3600); </span></li> </ol> </div>