您的位置:首页 > 编程语言 > PHP开发

【旧代码整理】/project/library/function.php 基本function部分

2016-07-15 14:16 603 查看
一个简单的php mvc框架 的 代码说明,/project/library/function.php 基本function部分:

/project/library/function.php

<?php
/*
* 想输出404的时候,随手用
*/
function _Error_404(){
header ( "HTTP/1.0 404 Not Found" );
exit;
}

/*
* __autoload 是必备的,调用/project/model下的class时可以免include
* 这是某个源码里面扣来改的
*/
function __autoload($class_name){
$class_name = strtolower($class_name);
$file = str_replace("_","/",$class_name);
$class_file = ROOT_PATH . "/{$file}.php";
if(file_exists($class_file)){
require $class_file;
}
}

/*
* 检查客户端是不是手机
*/
function is_mobile(){
$agent = $_SERVER['HTTP_USER_AGENT'];
if(strpos($agent,"NetFront") || strpos($agent,"iPhone") || strpos($agent,"MIDP-2.0") || strpos($agent,"Opera Mini") || strpos($agent,"UCWEB") || strpos($agent,"Android") || strpos($agent,"Windows CE") || strpos($agent,"SymbianOS")){
return true;
}
else{
return false;
}

}

/*
* 根据 route.php 的规则,取得从uri中解析出来的参数
* 其实这些参数解析时都放在 $GLOBALS['_SGLOBAL']['params']里了
* 就为了用的时候,省事
*/
function _get($key){
if(isset($GLOBALS['_SGLOBAL']['params'][$key])){
return $GLOBALS['_SGLOBAL']['params'][$key];
}
else{
return null;
}
}

/*
* 取得$len位长的纯字母随机字符串,某个地方扣来改的
*/
function genRandomString($len)
{
$chars = array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z"
);
$charsLen = count($chars) - 1;

shuffle($chars); // 将数组打乱

$output = "";
for ($i=0; $i<$len; $i++)
{
$output .= $chars[mt_rand(0, $charsLen)];
}

return $output;

}

/*
* 截取$len长度的中文字符串
* 网上找来的,用着还行
*/
function gb_substr($str,$start=0,$len=-1)
{
$enCount = 0;
$strLen = strlen($str);
$len *= 2;
if($len<0) $len = $strLen;
$strLen = ($len>$strLen)?$strLen:$len;
for($i=0;$i<$strLen;$i++)
if(ord(substr($str,$i,1))<0xa1) $enCount++;
$len = ($len % 2 != 0)?++$len:$len; //$len 必须为偶数
if($enCount % 2 == 0)
$re_str = substr($str,$start,$len);
else
$re_str = substr($str,$start,$len-1);
return $re_str;
}

/*
* 这两个加密解密function,好像是在Discuz里面扣出来的
* 还挺好用,安装php的时候要记得编译进 mcrypt
* $key 随便设
*/
//加密函数:encrypt
function _encrypt($encrypt,$key="123456") {
$iv = mcrypt_create_iv ( mcrypt_get_iv_size ( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB ), MCRYPT_RAND );
$passcrypt = mcrypt_encrypt ( MCRYPT_RIJNDAEL_256, $key, $encrypt, MCRYPT_MODE_ECB, $iv );
$encode = base64_encode ( $passcrypt );
return $encode;
}
//解密函数:decrypt
function _decrypt($decrypt,$key="123456") {
$decoded = base64_decode ( $decrypt );
$iv = mcrypt_create_iv ( mcrypt_get_iv_size ( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB ), MCRYPT_RAND );
$decrypted = mcrypt_decrypt ( MCRYPT_RIJNDAEL_256, $key, $decoded, MCRYPT_MODE_ECB, $iv );
return $decrypted;
}

/*
* 这是记录日志的
* 把文本$text累加入文本$logfile
*/
function _log($logfile,$text){
$dir = ROOT_PATH . '/logs';
$logfile = $dir . '/' . $logfile;
//echo $text;
return error_log($text,3,$logfile);
}

/*
* _in_time_extent:
* 检查 $time 或当前时间 是不是 在时间 $t1 和 $t2 之间
* 例如:
* $t1 = '11:30';
* $t2 = '12:30';
* $time = 111111111;
* $time = null 默认当前时间
* if(_in_time_extent($t1,$t2,$time){

echo '午餐时间';
}
if(_in_time_extent('18:00','19:00'){
echo '晚餐时间';
}
if(_in_time_extent('05:00','01:00'){
//凌晨五点,到次日凌晨1点
//运行 cron job
//...........
//...........
}
else{
echo "不在指定时间段内,cron job A 不运行";
}
*/
function _in_time_extent($t1,$t2,$time=null){
$t1 = strtotime($t1);
$t2 = strtotime($t2);
if($time == null){
$time = time();
}
if(!is_numeric($time)){
$time = strtotime($time);
}
return _in_day_extent($t1,$t2,$time);
}
function _in_day_extent($t1,$t2,$time){
$time_origin = strtotime(date('Y-m-d',$time));
$time_origin_tomorrow = $time_origin + 86400;
if($t1 > $t2){
return (($time > $time_origin) && ($time < $t2)) || (($time > $t1) && ($time < $time_origin_tomorrow));
}
elseif($t1 < $t2){
return (($time > $t1) && ($time < $t2));
}
else{
return false;
}
}

/*
* 一个极度简单好用的磁盘cache function
* 现在基本都ssd硬盘了,速度快得很
* 缓存文件保存在 /project/cache/目录下
* 默认缓存时间 一天
*/
function _jcache_set($name,$value){
$dir_cache = ROOT_PATH . '/cache';
createDir($dir_cache);
$content = serialize($value);
return file_put_contents($dir_cache.'/'.$name,$content);
}

function _jcache_get($name,$cachetime=86400){
$dir_cache = ROOT_PATH . '/cache';
if(file_exists($dir_cache.'/'.$name)){
$filemtime = filemtime($dir_cache.'/'.$name);
$cachetime_diff = time() - $filemtime;
if($cachetime_diff > $cachetime){
return null;
}
else{
$content = file_get_contents($dir_cache.'/'.$name);
return unserialize($content);
}
}
else{
return null;
}
}

/**
* 替换JSON方法(缺少json_encode时,载入/project/library/JSON.php)
* 给系统json_encode、json_decode 顶缺;
*/
if (!function_exists("json_encode")) {
include_once("JSON.php");

function json_encode($array) {
$json = new Services_JSON();
$json_array = $json->encode($array);
return $json_array;
}

/**
* 解析JSON数据
* @param string $json_data 需要解析的JSON数据
* @param bool $toarray 是否需要解析成数组
* @return array 返回解析后的数组
*/
function json_decode($json_data, $toarray = TRUE) {
$json = new Services_JSON();
$array = $json->decode($json_data);

if ($toarray) { // 需要转换成数组
$array = object2array($array);
}
return $array;
}

}

/*
* 把Object转换成Array,不记得哪里找来的,好用就行
*/
function ObjectToArray($d) {
if (is_object($d)) {
// Gets the properties of the given object
// with get_object_vars function
$d = get_object_vars($d);
}

if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return array_map(__FUNCTION__, $d);
}
else {
// Return array
return $d;
}
}
/*
* 这也忘记是哪里搞来的,将png转换成jpg,很好用
*/
// Quality is a number between 0 (best compression) and 100 (best quality)
function png2jpg($originalFile, $outputFile, $quality) {
$image = imagecreatefrompng($originalFile);
imagejpeg($image, $outputFile, $quality);
imagedestroy($image);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: