常用的全局函数,放置于app/include/global.func.php
//写入错误日志
function myLog($filename, $content) {
$handle = fopen($filename, 'a');
fwrite($handle, date('[Y-m-d H:i:s] ') . $content . "\n");
fclose($handle);
}
function weiXinToScreenLog($content,$file='') {
if(!$file){
$file = PUBLIC_PATH.'/log/'.date('Y').'/'.date('md').'/log.log';
}
$dir = dirname($file);
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}
myLog($file, $content);
}
/**
* 使用方法:unicode2utf8_2("\ue056"),\ue056来自网站http://code.iamcal.com/php/emoji/ Softbank那列数据
*/
function unicode2utf8_2($str){ //关于unicode编码转化的第二个函数,用于显示emoji表情
$str = '{"result_str":"'.$str.'"}'; //组合成json格式
$strarray = json_decode($str,true); //json转换为数组,利用 JSON 对 \uXXXX 的支持来把转义符恢复为 Unicode 字符
return $strarray['result_str'];
}
/**
* 将一个字串中含有全角的数字字符、字母、空格或'%+-()'字符转换为相应半角字符
* @access public
* @param string $str 待转换字串
* @return string $str 处理后字串
*/
function make_semiangle($str)
{
$arr = array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4',
'5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9',
'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E',
'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J',
'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O',
'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T',
'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y',
'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd',
'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i',
'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n',
'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's',
't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x',
'y' => 'y', 'z' => 'z',
'(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '【' => '[',
'】' => ']', '〖' => '[', '〗' => ']', '“' => '"', '”' => '"',
'‘' => '\'', '’' => '\'', '{' => '{', '}' => '}', '《' => '<',
'》' => '>',
'%' => '%', '+' => '+', '—' => '-', '-' => '-',
':' => ':', '。' => '.', '、' => '\\', ',' => ',',
';' => ';', '?' => '?', '!' => '!', '……' => '^', '‖' => '|',
'|' => '|', '〃' => '"', '·'=>'`','~'=>'~',
' ' => ' ','$'=>'$','@'=>'@','#'=>'#','^'=>'^','&'=>'&','*'=>'*',
'"'=>'"');
return strtr($str, $arr);
}
//创建文件夹
function mkdirs($path, $mode = 0777)
{
if(!file_exists($path))
{
mkdirs(dirname($path), $mode);
mkdir($path,$mode);
}
}
//设置cookie方法
function set_cookie($var, $value = '', $time = 0)
{
$time = $time > 0 ? $time : ($value == '' ? PHP_TIME - 3600 : 0);
$s = $_SERVER['SERVER_PORT'] == '443' ? 1 : 0;
$var = COOKIE_PRE.$var;
$_COOKIE[$var] = $value;
if(is_array($value)){
foreach($value as $k=>$v){
$v = Crypt::encrypt($v);
setcookie($var.'['.$k.']', $v, $time, COOKIE_PATH, COOKIE_DOMAIN, $s);
}
}else{
$value = Crypt::encrypt($value);
setcookie($var, $value, $time, COOKIE_PATH, COOKIE_DOMAIN, $s);
}
}
//获取cookie方法
function get_cookie($var)
{
$var = COOKIE_PRE.$var;
$r = isset($_COOKIE[$var]) ? $_COOKIE[$var] : 0;
if(!($r===0)){
$r = Crypt::decrypt($r);
}
return $r;
}
//获取文件类型
function get_filetype($filename) {
$extend = explode("." , $filename);
return strtolower($extend[count($extend) - 1]);
}
function resizeImage($im, $dest, $maxwidth, $maxheight) {
$img = getimagesize($im);
switch ($img[2]) {
case 1:
$im = @imagecreatefromgif($im);
break;
case 2:
$im = @imagecreatefromjpeg($im);
break;
case 3:
$im = @imagecreatefrompng($im);
break;
}
$pic_width = imagesx($im);
$pic_height = imagesy($im);
$resizewidth_tag = false;
$resizeheight_tag = false;
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);
}
imagejpeg($newim, $dest);
imagedestroy($newim);
} else {
imagejpeg($im, $dest);
}
}
//获取routeName
function getRouteName($i=1){
$url = $_SERVER['REQUEST_URI'];
$url = str_replace(strstr($url,'?'),'',$url);
$aItems = explode('/',$url);
$class = isset($aItems[$i])?$aItems[$i]:'';
return $class;
}
function safe_replace($string)
{
$string = str_replace('%20','',$string);
$string = str_replace('%27','',$string);
$string = str_replace('*','',$string);
$string = str_replace('"','"',$string);
$string = str_replace("'",'',$string);
$string = str_replace("\"",'',$string);
$string = str_replace('//','',$string);
$string = str_replace(';','',$string);
$string = str_replace('<','<',$string);
$string = str_replace('>','>',$string);
$string = str_replace('(','',$string);
$string = str_replace(')','',$string);
$string = str_replace("{",'',$string);
$string = str_replace('}','',$string);
return $string;
}
/**
*curl请求
*/
function httpsRequest($url, $postData="")
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if($postData){
curl_setopt($curl, CURLOPT_POST, 1); //post数据
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); // post的变量
}
ob_start();
$data = curl_exec($curl);
$data= ob_get_clean();
if (curl_errno($curl)) {
return 'ERROR '.curl_error($curl);
}
curl_close($curl);
return json_decode($data, true);
}