<?php if(!defined('DEDEINC')) exit('dedecms');
/**
* 时间戳小助手
*
* @version $Id: time.helper.php 1 2010-07-05 11:43:09Z tianya $
* @package DedeCMS.Helpers
* @copyright Copyright (c) 2007 - 2010, DesDev, Inc.
*/
/**
* 返回格林威治标准时间
*
* @param string $format 字符串格式
* @param string $timest 时间基准
* @return string
*/
if ( ! function_exists('MyDate'))
{
function MyDate($format='Y-m-d H:i:s', $timest=0)
{
global $cfg_cli_time;
$addtime = $cfg_cli_time * 3600;
if(empty($format))
{
$format = 'Y-m-d H:i:s';
}
return gmdate ($format, $timest+$addtime);
}
}
/**
* 从普通时间转换为Linux时间截
*
* @param string $dtime 普通时间
* @return string
*/
if ( ! function_exists('GetMkTime'))
{
function GetMkTime($dtime)
{
if(!preg_match("/[^0-9]/", $dtime))
{
return $dtime;
}
$dtime = trim($dtime);
$dt = Array(1970, 1, 1, 0, 0, 0);
$dtime = preg_replace("/[\r\n\t]|日|秒/", " ", $dtime);
$dtime = str_replace("年", "-", $dtime);
$dtime = str_replace("月", "-", $dtime);
$dtime = str_replace("时", ":", $dtime);
$dtime = str_replace("分", ":", $dtime);
$dtime = trim(preg_replace("/[ ]{1,}/", " ", $dtime));
$ds = explode(" ", $dtime);
$ymd = explode("-", $ds[0]);
if(!isset($ymd[1]))
{
$ymd = explode(".", $ds[0]);
}
if(isset($ymd[0]))
{
$dt[0] = $ymd[0];
}
if(isset($ymd[1])) $dt[1] = $ymd[1];
if(isset($ymd[2])) $dt[2] = $ymd[2];
if(strlen($dt[0])==2) $dt[0] = '20'.$dt[0];
if(isset($ds[1]))
{
$hms = explode(":", $ds[1]);
if(isset($hms[0])) $dt[3] = $hms[0];
if(isset($hms[1])) $dt[4] = $hms[1];
if(isset($hms[2])) $dt[5] = $hms[2];
}
foreach($dt as $k=>$v)
{
$v = preg_replace("/^0{1,}/", '', trim($v));
if($v=='')
{
$dt[$k] = 0;
}
}
$mt = mktime($dt[3], $dt[4], $dt[5], $dt[1], $dt[2], $dt[0]);
if(!empty($mt))
{
return $mt;
}
else
{
return time();
}
}
}
/**
* 减去时间
*
* @param int $ntime 当前时间
* @param int $ctime 减少的时间
* @return int
*/
if ( ! function_exists('SubDay'))
{
function SubDay($ntime, $ctime)
{
$dayst = 3600 * 24;
$cday = ceil(($ntime-$ctime)/$dayst);
return $cday;
}
}
/**
* 增加天数
*
* @param int $ntime 当前时间
* @param int $aday 增加天数
* @return int
*/
if ( ! function_exists('AddDay'))
{
function AddDay($ntime, $aday)
{
$dayst = 3600 * 24;
$oktime = $ntime + ($aday * $dayst);
return $oktime;
}
}
/**
* 返回格式化(Y-m-d H:i:s)的是时间
*
* @param int $mktime 时间戳
* @return string
*/
if ( ! function_exists('GetDateTimeMk'))
{
function GetDateTimeMk($mktime)
{
return MyDate('Y-m-d H:i:s',$mktime);
}
}
/**
* 返回格式化(Y-m-d)的日期
*
* @param int $mktime 时间戳
* @return string
*/
if ( ! function_exists('GetDateMk'))
{
function GetDateMk($mktime)
{
if($mktime=="0") return "暂无";
else return MyDate("Y-m-d", $mktime);
}
}
/**
* 将时间转换为距离现在的精确时间
*
* @param int $seconds 秒数
* @return string
*/
if ( ! function_exists('FloorTime'))
{
function FloorTime($seconds)
{
$times = '';
$days = floor(($seconds/86400)%30);
$hours = floor(($seconds/3600)%24);
$minutes = floor(($seconds/60)%60);
$seconds = floor($seconds%60);
if($seconds >= 1) $times .= $seconds.'秒';
if($minutes >= 1) $times = $minutes.'分钟 '.$times;
if($hours >= 1) $times = $hours.'小时 '.$times;
if($days >= 1) $times = $days.'天';
if($days > 30) return false;
$times .= '前';
return str_replace(" ", '', $times);
}
}
//其实是去除最后一个
function removeLast($str){
$arr = explode(' > ', $str);
$count = count($arr);
$result = "";
for($i=0;$i<$count-1;$i++){
$i==0 && $result .=getSelfIndex($arr[$i]);
$i!=0 && $i!=$count-2 && $result .=' > '.$arr[$i];
$i!=0 && $i==$count-2 && $result .=' > ';
}
return $result;
}
//其实是去除最后一个
function removeLast_func($str){
return substr($str,0,strlen($str)-2);
}
//得到自定义的index
function getSelfIndex($indexStr){
$indexStr = preg_replace("#<a href='(.*)'>(.*)</a>#iUs","<a href='\${1}index.html'>\${2}</a>",$indexStr);
return $indexStr;
}
//看是单数 还是双数
function getEvenOdd($num)
{
return strval(intval($num%2));
}
//增加箭头
function addJiantou($str)
{
$arr = explode(' > ', $str);
$count = count($arr);
// //假如最后一个不为空
$lastStr = trim($arr[$count-1]);
if(!empty($lastStr)){
return $str.' > ';
}
return $str;
}
function self_cn_substr($str,$len,$end='...')
{
if(strlen($str)<=$len){
return $str;
}
return cn_substr($str,$len).$end;
}
function getPpram($arcurl)
{
global $dsql;
$aidLength = strrpos($arcurl,'.')-strrpos($arcurl,'/')-1;
$aid = substr($arcurl,(strrpos($arcurl,'/')+1),$aidLength);
include_once (DEDEINC.'/arc.archives.class.php');
$arc = new Archives($aid);
$row = $dsql->GetOne("select channel from `#@__archives` where id='$aid'");
$channel = $row['channel'];
//不是图片类型 且不是 就业去向类型
if($channel!=18 && $channel!=19){
return $arcurl;
}
$row = $dsql->getOne("select typeid from `#@__arctiny` where id='$aid'");
$typeId = $row['typeid'];
//arcrank>=0 表示未删除
$dsql->SetQuery("select id from `#@__arctiny` where typeid='$typeId' and arcrank>=0 order by id desc");
$dsql->Execute();
$idArr = array();
while($row = $dsql->GetArray())
{
$idArr[] = $row['id'];
}
//求第一个id,并且据此求得它的arcurl
$firstId = $idArr[0];
$row = $dsql->getOne("select arc.*,tp.namerule,tp.typedir,tp.moresite,tp.siteurl,tp.sitepath from `#@__archives` arc left join `#@__arctype` tp
ON arc.typeid=tp.id where arc.id='$firstId'");
$firstArcurl = GetFileUrl($row['id'],$row['typeid'],$row['senddate'],$row['title'],$row['ismake'],
$row['arcrank'],$row['namerule'],$row['typedir'],$row['money'],$row['filename'],$row['moresite'],$row['siteurl'],$row['sitepath']);
//下面两行 第一行反转 第二行是看自己的是第几个
$idFlipArr = array_flip($idArr);
$idFlipArr[$aid] = $idFlipArr[$aid]+1;
//返回其实是此目录下的第一个文章的url后加上#p=0序号
return $firstArcurl.'#p=0'.$idFlipArr[$aid];
}
function execJs($str){
echo "<script>".$str."</script>";
}
function alert($str){
echo "<script>alert('".$str."')</script>";
}
//根据img得到src路径
function getImgSrc($str){
preg_match("/src='([^\']*)'?.*/i",$str, $matches);
return $matches[1];
}
//史平忠的裁切功能 (只是裁切) //$fullImg 是原图像路径,$thumbImg 是新图像路径
//$fix 是为了适应伍佰灵的 没有作用 $width是新图宽度 $height 是新图高度
function thumbImgCut($fullImg, $width=114 , $height=76 ,$thumbImg,$defaultWidth='100',$defaultHeight='100', $fix=5)
{
if(!is_file($fullImg)){
//echo $fullImg ."不存在<br>\n";
return ;
}
//list($w_src, $h_src) = getimagesize($fullImg);
$imgInfo=getimagesize($fullImg);
$w_src = $imgInfo[0];
$h_src = $imgInfo[1];
$imgType = $imgInfo[2];
//first change small by rate
if($width=='auto'){
$width = $height * $w_src/$h_src;
//假如自动的宽度大于默认宽度,就取默认宽度
$width>$defaultWidth && $width = $defaultWidth;
}
if($height=='auto'){
$height = $width * $h_src/$w_src;
//假如自动的高度大于默认宽度,就取默认高度
$height>$defaultHeight && $height = $defaultHeight;
}
$w_descFir = $width;
$h_descFir = $height;
$rate_descFir = $width/$height;
$rate_old = $w_src/$h_src;
//new rate >= old rate
if ($rate_descFir >= $rate_old)
{
//切掉东西的方法
$h_descFir = ceil($w_descFir / $rate_old);
}
//new rate < old rate
else if ($rate_descFir < $rate_old)
{
//切掉东西的方法
$w_descFir = ceil($h_descFir * $rate_old);
}
//source photo
//gif
if ($imgType==1)
{
$srcPho = imagecreatefromgif($fullImg);
}//jpeg(jpg)
else if ($imgType==3)
{
$srcPho = imagecreatefrompng($fullImg);
}//其余假定为 jpeg 其实是$imgType==2
else
{
$srcPho = imagecreatefromjpeg($fullImg);
}
//first new small photo
$descPhoFir = imagecreatetruecolor($w_descFir, $h_descFir);
imagecopyresampled($descPhoFir, $srcPho, 0, 0, 0, 0, $w_descFir, $h_descFir, $w_src, $h_src);
//second change small by crop
if ($rate_descFir >= $rate_old)
{
$cropX = 0;
$cropY = ceil(($h_descFir-$height)/2);
$h_descFir = $height;
}
else if ($rate_descFir < $rate_old)
{
$cropX = ceil(($w_descFir-$width)/2);
$cropY = 0;
$w_descFir = $width;
}
//second new small photo
$descPhoSec = imagecreatetruecolor($width, $height);
imagecopyresampled($descPhoSec, $descPhoFir, 0, 0, $cropX, $cropY, $width, $height, $width, $height);
//生成gif
if ($imgType==1)
{
imagegif($descPhoSec, $thumbImg);
}//生成png
else if ($imgType==3)
{
Imagepng($descPhoSec, $thumbImg);
} //其余生成jpg(jpeg)
else
{
ImageJpeg($descPhoSec, $thumbImg, 90);
}
@imagedestroy($photo);
chmod($thumbImg, 0777);
}
function endsWith($haystack, $needle)
{
$length = strlen($needle);
if($length == 0)
{
return true;
}
return (substr($haystack, -$length) === $needle);
}
function makeMulitImg($path,$get){
$pointPosition = strrpos($path, '.');
$prevPath = substr($path,0,$pointPosition);
$suffPath = strrchr($path, '.');
if(file_exists($prevPath.'_auto'.$suffPath)){
return ;
}
//感恩母校栏目页 主图
$additi ='';
$get['CKEditor']=='body' && $additi='-lp';
$thumbImg = $prevPath.$additi.'_auto_2'.$suffPath;
ImageResizeNew($path, '176', '250', $thumbImg, $issave=TRUE);
$thumbImg = $prevPath.$additi.'_auto_4'.$suffPath;
ImageResizeNew($path, '150', '215', $thumbImg, $issave=TRUE);
}
//得到img指定的(大,或中或小)路径
//值有 auto 100 160 170 180 180_2 200 215 400
//这些值指的是自定义裁切的图的宽度
function getReferImg($litpic, $width='100', $height='100', $defautWidth='100', $defaultHeight='100')
{
//error_reporting(0);
include_once(DEDEINC.'/image.func.php');
include_once('image.helper.php');
//我们是先生成,再取出图片
$pointPosition = strrpos($litpic, '.');
$prevPath = substr($litpic,0,$pointPosition);
$suffPath = strrchr($litpic, '.');
$newLitpic = $prevPath.'-'.$width.'-'.$height.$suffPath;
$newPicPath = $_SERVER['DOCUMENT_ROOT'].$newLitpic;
//看是否存在这个裁剪的新图,有的话 就直接返回
if(file_exists($newPicPath)){
return $newLitpic;
}
$oldPicPath = $_SERVER['DOCUMENT_ROOT'].$litpic;
thumbImgCut($oldPicPath,$width,$height,$newPicPath,$defautWidth,$defaultHeight);
return $newLitpic;
}
//根据宽高比与原图的宽高比 来调用 thumbImgCut
function thumbImgCutByRate($oldPicPath,$width,$height,$newPicPath)
{
$imgInfo=getimagesize($oldPicPath);
//原图宽高
$w_src = $imgInfo[0];
$h_src = $imgInfo[1];
$rateOld = $w_src/$h_src;
$rateNew = $width/$height;
//看比率 ,得到 所要的宽度 或高度
if($rateNew>$rateOld){
$width = floor($height * $rateOld);
}
else {
$height = floor($width /$rateOld);
}
thumbImgCut($oldPicPath, $width, $height,$newPicPath);
}
//根据宽高比得到指定的图形对象
function getReferImgByRate($litpic, $width='100', $height='100')
{
//error_reporting(0);
include_once(DEDEINC.'/image.func.php');
include_once('image.helper.php');
//我们是先生成,再取出图片
$pointPosition = strrpos($litpic, '.');
$prevPath = substr($litpic,0,$pointPosition);
$suffPath = strrchr($litpic, '.');
$newLitpic = $prevPath.'-'.'rate'.'-'.$width.'-'.$height.$suffPath;
$newPicPath = $_SERVER['DOCUMENT_ROOT'].$newLitpic;
//看是否存在这个裁剪的新图,有的话 就直接返回
if(file_exists($newPicPath)){
return $newLitpic;
}
$oldPicPath = $_SERVER['DOCUMENT_ROOT'].$litpic;
thumbImgCutByRate($oldPicPath,$width,$height,$newPicPath);
return $newLitpic;
}
//看是否宽度超过540,超过的话 就裁
function getReferImgWidthMax($litpic,$width='540',$height='auto')
{
//error_reporting(0);
include_once(DEDEINC.'/image.func.php');
include_once('image.helper.php');
//我们是先生成,再取出图片
$pointPosition = strrpos($litpic, '.');
$prevPath = substr($litpic,0,$pointPosition);
$suffPath = strrchr($litpic, '.');
$newLitpic = $prevPath.'-'.'widthMax'.'-'.$width.'-'.$height.$suffPath;
$newPicPath = $_SERVER['DOCUMENT_ROOT'].$newLitpic;
//看是否存在这个裁剪的新图,有的话 就直接返回
if(file_exists($newPicPath)){
return $newLitpic;
}
$oldPicPath = $_SERVER['DOCUMENT_ROOT'].$litpic;
$imgInfo=getimagesize($oldPicPath);
//原图宽
$w_src = $imgInfo[0];
//假如原图的宽度小于要设定的宽度540,返回原图
if($w_src<$width){
return $litpic;
}
$oldPicPath = $_SERVER['DOCUMENT_ROOT'].$litpic;
//给个默认的宽度和高度 ;当它的高度 高于默认的高度时 取默认高度
thumbImgCut($oldPicPath,$width,$height,$newPicPath,'5000','5000');
return $newLitpic;
}
//先去标签 ,再进行截取
function self_cn_substr_strip_tag($str,$len,$end='...')
{
//去掉标签
$str = trim(dede_strip_tags($str));
//var_dump($str);
return self_cn_substr($str, $len, $end);
}
//解析模板标签
function pasterTempletDiy($path)
{
require_once(DEDEINC."/arc.partview.class.php");
global $cfg_basedir,$cfg_templets_dir;
$tmpfile = $cfg_basedir.$cfg_templets_dir."/".$path;//模版文件的路径
$dtp = new PartView();
$dtp->SetTemplet($tmpfile);
$dtp->Display();
}