标签:
<?php /* * string:字符串,明文或密文 * $operation:DECODE表示解密,其它表示加密; * $key:密匙; * $expiry:密文有效期。 */ function authcode($string, $operation = ‘DECODE‘, $key = ‘‘, $expiry = 0) { // 动态密匙长度,相同的明文会生成不同密文就是依靠动态密匙 $ckey_length = 4; // 密匙 $key = md5($key ? $key : C(‘auth_key‘)); // 密匙a会参与加解密 $keya = md5(substr($key, 0, 16)); // 密匙b会用来做数据完整性验证 $keyb = md5(substr($key, 16, 16)); // 密匙c用于变化生成的密文 $keyc = $ckey_length ? ($operation == ‘DECODE‘ ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : ‘‘; // 参与运算的密匙 $cryptkey = $keya.md5($keya.$keyc); $key_length = strlen($cryptkey); // 明文,前10位用来保存时间戳,解密时验证数据有效性,10到26位用来保存$keyb(密匙b), //解密时会通过这个密匙验证数据完整性 // 如果是解码的话,会从第$ckey_length位开始,因为密文前$ckey_length位保存 动态密匙,以保证解密正确 $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 <= 255; $i++) { $rndkey[$i] = ord($cryptkey[$i % $key_length]); } // 用固定的算法,打乱密匙簿,增加随机性,好像很复杂,实际上对并不会增加密文的强度 for($j = $i = 0; $i < 256; $i++) { $j = ($j + $box[$i] + $rndkey[$i]) % 256; $tmp = $box[$i]; $box[$i] = $box[$j]; $box[$j] = $tmp; } // 核心加解密部分 for($a = $j = $i = 0; $i < $string_length; $i++) { $a = ($a + 1) % 256; $j = ($j + $box[$a]) % 256; $tmp = $box[$a]; $box[$a] = $box[$j]; $box[$j] = $tmp; // 从密匙簿得出密匙进行异或,再转成字符 $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256])); } if($operation == ‘DECODE‘) { // 验证数据有效性,请看未加密明文的格式 if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) { return substr($result, 26); } else { return ‘‘; } } else { // 把动态密匙保存在密文里,这也是为什么同样的明文,生产不同密文后能解密的原因 // 因为加密后的密文可能是一些特殊字符,复制过程可能会丢失,所以用base64编码 return $keyc.str_replace(‘=‘, ‘‘, base64_encode($result)); } } function setcookies($name,$value,$life=""){ if($life){ cookie($name,authcode($value,‘RECODE‘,C(‘auth_key‘)),$life); }else{ cookie($name,authcode($value,‘RECODE‘,C(‘auth_key‘))); } } function getcookies($name){ if($_COOKIE[$name]){ return authcode($_COOKIE[$name],‘DECODE‘,C(‘auth_key‘)); }else { return false; } } /* * 模板地址纠正 */ function url_right($url){ if($url){ $url_arr=parse_url($url); if(!$url_arr[‘scheme‘]){ $url=‘http://‘.$_SERVER[‘HTTP_HOST‘]."//".$url; } return $url; }else{ return false; } } /* * 获取网站logo */ function get_url_ico($url){ $url_arr=parse_url($url); if(!$url_arr[‘scheme‘]){ $url.="http://"; } $url_arr=parse_url($url); $url=$url_arr[‘scheme‘]."://".$url_arr[‘host‘]; if(url_exists($url)){ $api_url="http://g.soz.im/{$url}/cdn.ico"; $ico=$url."/favicon.ico"; if(remote_file_exists($ico)){ return $ico; }elseif(remote_file_exists($api_url)){ return $api_url; }else{ return C(‘default_urlico‘); } }else{ return false; } } /* * 判断url是否存在 */ function url_exists($url) { $head = @get_headers($url); return is_array($head) ? true : false; } /* * 判断远程文件是否存在 */ function remote_file_exists($url) { $executeTime = ini_get(‘max_execution_time‘); ini_set(‘max_execution_time‘, 0); $headers = @get_headers($url); ini_set(‘max_execution_time‘, $executeTime); if ($headers) { $head = explode(‘ ‘, $headers[0]); if ( !empty($head[1]) && intval($head[1]) < 400) return true; } return false; } /* * 网址分类 网址列表组装函数 * @ $cate 分类数组 * @ $url 列表数组 */ function urlcombine($cate, $url) { if(!$cate or !$url){ return null; exit(); } $data = array (); for($j = 0; $j < count ( $cate ); $j ++) { $num = 0; for($i = 0; $i < count ( $url ); $i ++) { if ($url [$i] [‘cid‘] == $cate [$j] [‘id‘]) { $data [$num] [‘id‘] = $url [$i] [‘id‘]; $data [$num] [‘uid‘] = $url [$i] [‘uid‘]; $data [$num] [‘title‘] = $url [$i] [‘title‘]; $data [$num] [‘img‘] = $url [$i] [‘img‘]; $data [$num] [‘url‘] = $url [$i] [‘url‘]; $num ++; } } $url_as [$j] [‘id‘] = $cate [$j] [‘id‘]; $url_as [$j] [‘uid‘] = $cate [$j] [‘uid‘]; $url_as [$j] [‘cate_name‘] = $cate [$j] [‘title‘]; $url_as [$j] [‘data‘] = $data; $data = array (); } return $url_as; } function get_randchar($len = 6, $format = ‘ALL‘) { $is_abc = $is_numer = 0; $password = $tmp = ‘‘; switch ($format) { case ‘ALL‘ : $chars = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789‘; break; case ‘CHAR‘ : $chars = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz‘; break; case ‘NUMBER‘ : $chars = ‘0123456789‘; break; default : $chars = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789‘; break; } mt_srand ( ( double ) microtime () * 1000000 * getmypid () ); while ( strlen ( $password ) < $len ) { $tmp = substr ( $chars, (mt_rand () % strlen ( $chars )), 1 ); if (($is_numer != 1 && is_numeric ( $tmp ) && $tmp > 0) || $format == ‘CHAR‘) { $is_numer = 1; } if (($is_abc != 1 && preg_match ( ‘/[a-zA-Z]/‘, $tmp )) || $format == ‘NUMBER‘) { $is_abc = 1; } $password .= $tmp; } if ($is_numer != 1 || $is_abc != 1 || empty ( $password )) { $password = get_randchar ( $len, $format ); } return $password; } /* *查找上级 */ function find_topTree($id){ $map_find[‘id‘]=array(‘eq‘,$id); $user=M("users")->where($map_find)->field(‘id_top‘)->find(); $map_first[‘id‘]=array(‘eq‘,$user[‘id_top‘]); $tree_first=M("users")->where($map_first)->field(‘id,id_top‘)->find(); if($tree_first){ if($tree_first[‘id_top‘]!=$tree_first[‘id‘]){ $map_second[‘id‘]=array(‘eq‘,$tree_first[‘id_top‘]); $tree_second=M("users")->where($map_second)->field(‘id,id_top‘)->find(); } } if($tree_second){ if($tree_second[‘id_top‘]!=$tree_second[‘id‘]){ $map_third[‘id‘]=array(‘eq‘,$tree_second[‘id_top‘]); $tree_third=M("users")->where($map_third)->field(‘id,id_top‘)->find(); } } $tree[‘tree_top_first‘]=$tree_first; $tree[‘tree_top_second‘]=$tree_second; $tree[‘tree_top_third‘]=$tree_third; return $tree; } /* * 查找下级 */ function find_underTree($uid){ $map_all[‘id_top‘]=array(‘neq‘,1); $map_all[‘email_check‘]=array(‘eq‘,1); $map_all[‘state‘]=array(‘eq‘,1); $tree_all=M("users")->where($map_all)->field(‘id,id_top‘)->select(); foreach ( $tree_all as $key => $value ) { if ($tree_all [$key] [‘id_top‘] == $uid) { $value[‘grade‘]=1; $tree_first [] = $value; } } if($tree_first){ foreach ( $tree_first as $key => $value ) { foreach ( $tree_all as $key2 => $value2 ) { if ($tree_all [$key2] [‘id_top‘] == $tree_first [$key] [‘id‘]) { $value2[‘grade‘]=2; $tree_second [] = $value2; } } } } if($tree_second){ foreach ( $tree_second as $key => $value ) { foreach ( $tree_all as $key2 => $value2 ) { if ($tree_all [$key2] [‘id_top‘] == $tree_second [$key] [‘id‘]) { $value2[‘grade‘]=3; $tree_third [] = $value2; } } } } //$tree_under_all=array_merge($tree_first,$tree_second,$tree_third); $tree[‘tree_under_first‘]=$tree_first; $tree[‘tree_under_second‘]=$tree_second; $tree[‘tree_under_third‘]=$tree_third; //$tree[‘tree_under_all‘]=$tree_under_all; return $tree; } /* * 文件上传类 */ function uploadfile() { import(‘ORG.Net.UploadFile‘); $upload = new UploadFile(); // 实例化上传类 $upload -> maxSize = 3145728; // 设置附件上传大小 $upload -> allowExts = array(‘jpg‘, ‘gif‘, ‘png‘, ‘jpeg‘); // 设置附件上传类型 $upload -> thumb = true; $upload -> thumbMaxWidth = 300; $upload -> thumbMaxHeight = 300; $upload -> thumbPath = ‘Uploads/‘ . getcookies(‘uid‘) . ‘/thumb/‘; // 设置苏略图上传目录 $upload -> savePath = ‘Uploads/‘ . getcookies(‘uid‘) . ‘/‘; // 设置附件上传目录 $dir_1 = create_folders($upload -> thumbPath); $dir_2 = create_folders($upload -> savePath); if (!$dir_1 or !$dir_2) { $data[‘code‘] = 1; $data[‘msg‘] = "文件夹创建失败!"; return $data; } if (!$upload -> upload()) { $data[‘code‘] = 2; $data[‘msg‘] = $upload -> getErrorMsg(); } else { $files = $upload -> getUploadFileInfo(); for ($i = 0; $i < count($files); $i++) { $data[$i][‘key‘] = $files[$i][‘key‘]; $data[$i][‘filepath‘] = "/".$files[$i][‘savepath‘] . $files[$i][‘savename‘]; $data[$i][‘thumbpath‘] = "/".$files[$i][‘savepath‘] . "thumb/thumb_" . $files[$i][‘savename‘]; } } return $data; } function create_folders($dir) {//创建目录函数 return is_dir($dir) or (create_folders(dirname($dir)) and mkdir($dir, 0777)); } function UploadFiles2($path,$thumbwidth,$thumbheight,$thumbPath){ //文件上传公用函数(需要缩略图) import("ORG.Net.UploadFile"); $upload = new UploadFile(); // 实例化上传类 $upload->maxSize = C(‘file_uplode_maxSize‘) ; // 设置附件上传大小 $upload->allowExts = array(‘jpg‘, ‘gif‘, ‘png‘, ‘jpeg‘) ; // 设置附件上传类型 array(‘jpg‘, ‘gif‘, ‘png‘, ‘jpeg‘) $upload->savePath = $path ;// 设置附件上传目录 ‘./Public/Uploads/‘ $upload->saveRule = time().‘_‘.mt_rand(10,99); $upload->thumb = true; $upload->thumbMaxWidth =$thumbwidth; $upload->thumbPath=$thumbPath; $upload->thumbMaxHeight =$thumbheight; if(!$upload->upload()) { // 上传错误提示错误信息 return array(‘err‘=>1,‘msg‘=>$upload->getErrorMsg()); }else{ // 上传成功 获取上传文件信息 return $upload->getUploadFileInfo(); } } function find_user($uid){ $map_user[‘id‘]=array(‘eq‘,$uid); return M(‘users‘)->where($map_user)->find(); } function find_users($uids){ $map_user[‘id‘]=array(‘in‘,$uids); return M(‘users‘)->where($map_user)->select(); } function get_parse($str){ $data = array(); $parameter = explode(‘&‘,end(explode(‘?‘,$str))); foreach($parameter as $val){ $tmp = explode(‘=‘,$val); $data[$tmp[0]] = $tmp[1]; } return $data; } function https_request($url, $data = null) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; } function update_qq_access_token_data($openid,$arr_access_token){ $map_user[‘qq_openid‘]=array(‘eq‘,$qq_openid); $data_access_token[‘qq_access_token‘]=$arr_access_token[‘access_token‘]; $data_access_token[‘qq_refresh_token‘]=$arr_access_token[‘refresh_token‘]; $res=M(‘users‘)->where($map_user)->save($data_access_token); if($res){ return true; }else{ return false; } } function update_user_qq_token($uid){ $map_user[‘id‘]=array(‘eq‘,$uid); $data_access_token[‘qq_openid‘]=$_SESSION[‘qq_openid‘]; $data_access_token[‘qq_access_token‘]=$_SESSION[‘qq_access_token‘]; $data_access_token[‘qq_refresh_token‘]=$_SESSION[‘qq_refresh_token‘]; $res=M(‘users‘)->where($map_user)->save($data_access_token); if($res){ session(‘qq_openid‘,null); session(‘access_token‘,null); session(‘refresh_token‘,null); return true; }else{ return false; } } function get_qq_info($openid,$access_token,$appid){ $url="https://graph.qq.com/user/get_user_info?access_token={$access_token}&oauth_consumer_key={$appid}&openid={$openid}"; return json_decode(https_request($url),true); } function find_vip($id){ $map[‘id‘]=array(‘eq‘,$id); return M(‘user_vip‘)->where($map)->find(); } /* * 判断是否是手机访问 */ function is_mobile() { $_SERVER[‘ALL_HTTP‘] = isset($_SERVER[‘ALL_HTTP‘]) ? $_SERVER[‘ALL_HTTP‘] : ‘‘; $mobile_browser = ‘0‘; if(preg_match(‘/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i‘, strtolower($_SERVER[‘HTTP_USER_AGENT‘]))) $mobile_browser++; if((isset($_SERVER[‘HTTP_ACCEPT‘])) and (strpos(strtolower($_SERVER[‘HTTP_ACCEPT‘]),‘application/vnd.wap.xhtml+xml‘) !== false)) $mobile_browser++; if(isset($_SERVER[‘HTTP_X_WAP_PROFILE‘])) $mobile_browser++; if(isset($_SERVER[‘HTTP_PROFILE‘])) $mobile_browser++; $mobile_ua = strtolower(substr($_SERVER[‘HTTP_USER_AGENT‘],0,4)); $mobile_agents = array( ‘w3c ‘,‘acs-‘,‘alav‘,‘alca‘,‘amoi‘,‘audi‘,‘avan‘,‘benq‘,‘bird‘,‘blac‘, ‘blaz‘,‘brew‘,‘cell‘,‘cldc‘,‘cmd-‘,‘dang‘,‘doco‘,‘eric‘,‘hipt‘,‘inno‘, ‘ipaq‘,‘java‘,‘jigs‘,‘kddi‘,‘keji‘,‘leno‘,‘lg-c‘,‘lg-d‘,‘lg-g‘,‘lge-‘, ‘maui‘,‘maxo‘,‘midp‘,‘mits‘,‘mmef‘,‘mobi‘,‘mot-‘,‘moto‘,‘mwbp‘,‘nec-‘, ‘newt‘,‘noki‘,‘oper‘,‘palm‘,‘pana‘,‘pant‘,‘phil‘,‘play‘,‘port‘,‘prox‘, ‘qwap‘,‘sage‘,‘sams‘,‘sany‘,‘sch-‘,‘sec-‘,‘send‘,‘seri‘,‘sgh-‘,‘shar‘, ‘sie-‘,‘siem‘,‘smal‘,‘smar‘,‘sony‘,‘sph-‘,‘symb‘,‘t-mo‘,‘teli‘,‘tim-‘, ‘tosh‘,‘tsm-‘,‘upg1‘,‘upsi‘,‘vk-v‘,‘voda‘,‘wap-‘,‘wapa‘,‘wapi‘,‘wapp‘, ‘wapr‘,‘webc‘,‘winw‘,‘winw‘,‘xda‘,‘xda-‘ ); if(in_array($mobile_ua, $mobile_agents)) $mobile_browser++; if(strpos(strtolower($_SERVER[‘ALL_HTTP‘]), ‘operamini‘) !== false) $mobile_browser++; // Pre-final check to reset everything if the user is on Windows if(strpos(strtolower($_SERVER[‘HTTP_USER_AGENT‘]), ‘windows‘) !== false) $mobile_browser=0; // But WP7 is also Windows, with a slightly different characteristic if(strpos(strtolower($_SERVER[‘HTTP_USER_AGENT‘]), ‘windows phone‘) !== false) $mobile_browser++; if($mobile_browser>0) return true; else return false; } /* * 自动升级程序 */ function vip_update($uid){ $user=find_user($uid); $vips=M(‘user_vip‘)->select(); foreach ($vips as $key=> $value) { if($user[‘exp‘] > $vips[$key][‘exp_start‘] && $user[‘exp‘]<$vips[$key][‘exp_end‘] ){ $data[‘vip‘]=$vips[$key][‘id‘]; break; } } if($data[‘vip‘]!=$user[‘vip‘]){ $map[‘id‘]=array(‘eq‘,$uid); $res=M(‘users‘)->where($map)->save($data); if($res){ return $data[‘vip‘]; }else{ return false; } }else{ return false; } } ?>
标签:
原文地址:http://www.cnblogs.com/yutaoyao/p/4291848.html