Odoo中文网|Odoo实施培训

 找回密码
 立即注册
搜索
热搜: Odoo OpenERP 实施
查看: 6452|回复: 0
打印 上一主题 下一主题

我的工具类库

[复制链接]

16

主题

40

帖子

148

积分

版主

Rank: 7Rank: 7Rank: 7

积分
148
跳转到指定楼层
楼主
发表于 2015-11-24 14:57:16 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. <?php  
  2. /**   
  3. * 常用工具类   
  4. * author Lee.   
  5. * Last modify $Date: 2012-8-23
  6. */  
  7. class Tool {  
  8.     /**
  9.      * js 弹窗并且跳转
  10.      * @param string $_info
  11.      * @param string $_url
  12.      * @return js
  13.      */  
  14.     static public function alertLocation($_info, $_url) {  
  15.         echo "<script type='text/javascript'>alert('$_info');location.href='$_url';</script>";  
  16.         exit();  
  17.     }  
  18.       
  19.     /**
  20.      * js 弹窗返回
  21.      * @param string $_info
  22.      * @return js
  23.      */  
  24.     static public function alertBack($_info) {  
  25.         echo "<script type='text/javascript'>alert('$_info');history.back();</script>";  
  26.         exit();  
  27.     }  
  28.       
  29.     /**
  30.      * 页面跳转
  31.      * @param string $url
  32.      * @return js
  33.      */  
  34.     static public function headerUrl($url) {  
  35.         echo "<script type='text/javascript'>location.href='{$url}';</script>";  
  36.         exit();  
  37.     }  
  38.       
  39.     /**
  40.      * 弹窗关闭
  41.      * @param string $_info
  42.      * @return js
  43.      */  
  44.     static public function alertClose($_info) {  
  45.         echo "<script type='text/javascript'>alert('$_info');close();</script>";  
  46.         exit();  
  47.     }  
  48.       
  49.     /**
  50.      * 弹窗
  51.      * @param string $_info
  52.      * @return js
  53.      */  
  54.     static public function alert($_info) {  
  55.         echo "<script type='text/javascript'>alert('$_info');</script>";  
  56.         exit();  
  57.     }  
  58.       
  59.     /**
  60.      * 系统基本参数上传图片专用
  61.      * @param string $_path
  62.      * @return null
  63.      */  
  64.     static public function sysUploadImg($_path) {  
  65.         echo '<script type="text/javascript">document.getElementById("logo").value="'.$_path.'";</script>';  
  66.         echo '<script type="text/javascript">document.getElementById("pic").src="'.$_path.'";</script>';  
  67.         echo '<script type="text/javascript">$("#loginpop1").hide();</script>';  
  68.         echo '<script type="text/javascript">$("#bgloginpop2").hide();</script>';  
  69.     }  
  70.       
  71.     /**
  72.      * html过滤
  73.      * @param array|object $_date
  74.      * @return string
  75.      */  
  76.     static public function htmlString($_date) {  
  77.         if (is_array($_date)) {  
  78.             foreach ($_date as $_key=>$_value) {  
  79.                 $_string[$_key] = Tool::htmlString($_value);  //递归  
  80.             }  
  81.         } elseif (is_object($_date)) {  
  82.             foreach ($_date as $_key=>$_value) {  
  83.                 $_string->$_key = Tool::htmlString($_value);  //递归  
  84.             }  
  85.         } else {  
  86.             $_string = htmlspecialchars($_date);  
  87.         }  
  88.         return $_string;  
  89.     }  
  90.       
  91.     /**
  92.      * 数据库输入过滤
  93.      * @param string $_data
  94.      * @return string
  95.      */  
  96.     static public function mysqlString($_data) {  
  97.         $_data = trim($_data);  
  98.         return !GPC ? addcslashes($_data) : $_data;  
  99.     }  
  100.       
  101.     /**
  102.      * 清理session
  103.      */  
  104.     static public function unSession() {  
  105.         if (session_start()) {  
  106.             session_destroy();  
  107.         }  
  108.     }  
  109.       
  110.     /**
  111.      * 验证是否为空
  112.      * @param string $str
  113.      * @param string $name
  114.      * @return bool (true or false)
  115.      */  
  116.     static function validateEmpty($str, $name) {  
  117.         if (empty($str)) {  
  118.             self::alertBack('警告:' .$name . '不能为空!');  
  119.         }  
  120.     }  
  121.       
  122.     /**
  123.      * 验证是否相同
  124.      * @param string $str1
  125.      * @param string $str2
  126.      * @param string $alert
  127.      * @return JS  
  128.      */  
  129.     static function validateAll($str1, $str2, $alert) {  
  130.         if ($str1 != $str2) self::alertBack('警告:' .$alert);  
  131.     }  
  132.       
  133.     /**
  134.      * 验证ID
  135.      * @param Number $id
  136.      * @return JS
  137.      */  
  138.     static function validateId($id) {  
  139.         if (empty($id) || !is_numeric($id)) self::alertBack('警告:参数错误!');  
  140.     }  
  141.       
  142.     /**
  143.      * 格式化字符串
  144.      * @param string $str
  145.      * @return string
  146.      */  
  147.     static public function formatStr($str) {  
  148.         $arr = array(' ', ' ', '&', '@', '#', '%',  '\'', '"', '\\', '/', '.', ',', '
  149. , '^', '*', '(', ')', '[', ']', '{', '}', '|', '~', '`', '?', '!', ';', ':', '-', '_', '+', '=');  
  150.         foreach ($arr as $v) {  
  151.             $str = str_replace($v, '', $str);  
  152.         }  
  153.         return $str;  
  154.     }  
  155.       
  156.     /**
  157.      * 格式化时间
  158.      * @param int $time 时间戳
  159.      * @return string
  160.      */  
  161.     static public function formatDate($time='default') {  
  162.         $date = $time == 'default' ? date('Y-m-d H:i:s', time()) : date('Y-m-d H:i:s', $time);  
  163.         return $date;  
  164.     }  
  165.       
  166.     /**   
  167.     * 获得真实IP地址   
  168.     * @return string   
  169.     */  
  170.     static public function realIp() {     
  171.         static $realip = NULL;     
  172.         if ($realip !== NULL) return $realip;   
  173.         if (isset($_SERVER)) {   
  174.             if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {     
  175.                 $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);   
  176.                 foreach ($arr AS $ip) {   
  177.                     $ip = trim($ip);   
  178.                     if ($ip != 'unknown') {     
  179.                         $realip = $ip;     
  180.                         break;     
  181.                     }     
  182.                 }     
  183.             } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {     
  184.                 $realip = $_SERVER['HTTP_CLIENT_IP'];   
  185.             } else {     
  186.                 if (isset($_SERVER['REMOTE_ADDR'])) {     
  187.                     $realip = $_SERVER['REMOTE_ADDR'];     
  188.                 } else {     
  189.                     $realip = '0.0.0.0';     
  190.                 }   
  191.             }   
  192.         } else {   
  193.             if (getenv('HTTP_X_FORWARDED_FOR')) {   
  194.                 $realip = getenv('HTTP_X_FORWARDED_FOR');   
  195.             } elseif (getenv('HTTP_CLIENT_IP')) {   
  196.                 $realip = getenv('HTTP_CLIENT_IP');   
  197.             } else {   
  198.                 $realip = getenv('REMOTE_ADDR');   
  199.             }   
  200.         }  
  201.         preg_match('/[\d\.]{7,15}/', $realip, $onlineip);   
  202.         $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';   
  203.         return $realip;   
  204.     }  
  205.       
  206.     /**
  207.      * 加载 Smarty 模板
  208.      * @param string $html
  209.      * @return null;
  210.      */  
  211.     static public function display() {  
  212.         global $tpl;$html = null;  
  213.         $htmlArr = explode('/', $_SERVER[SCRIPT_NAME]);  
  214.         $html = str_ireplace('.php', '.html', $htmlArr[count($htmlArr)-1]);  
  215.         $dir = dirname($_SERVER[SCRIPT_NAME]);  
  216.         $firstStr = substr($dir, 0, 1);  
  217.         $endStr = substr($dir, strlen($dir)-1, 1);  
  218.         if ($firstStr == '/' || $firstStr == '\\') $dir = substr($dir, 1);  
  219.         if ($endStr != '/' || $endStr != '\\') $dir = $dir . '/';  
  220.         $tpl->display($dir.$html);  
  221.     }  
  222.       
  223.     /**
  224.      * 创建目录
  225.      * @param string $dir
  226.      */  
  227.     static public function createDir($dir) {  
  228.         if (!is_dir($dir)) {  
  229.             mkdir($dir, 0777);  
  230.         }  
  231.     }  
  232.       
  233.     /**
  234.      * 创建文件(默认为空)
  235.      * @param unknown_type $filename
  236.      */  
  237.     static public function createFile($filename) {  
  238.         if (!is_file($filename)) touch($filename);  
  239.     }  
  240.       
  241.     /**
  242.      * 正确获取变量
  243.      * @param string $param
  244.      * @param string $type
  245.      * @return string
  246.      */  
  247.     static public function getData($param, $type='post') {  
  248.         $type = strtolower($type);  
  249.         if ($type=='post') {  
  250.             return Tool::mysqlString(trim($_POST[$param]));  
  251.         } elseif ($type=='get') {  
  252.             return Tool::mysqlString(trim($_GET[$param]));  
  253.         }  
  254.     }  
  255.       
  256.     /**
  257.      * 删除文件
  258.      * @param string $filename
  259.      */  
  260.     static public function delFile($filename) {  
  261.         if (file_exists($filename)) unlink($filename);  
  262.     }  
  263.       
  264.     /**
  265.      * 删除目录
  266.      * @param string $path
  267.      */  
  268.     static public function delDir($path) {  
  269.         if (is_dir($path)) rmdir($path);  
  270.     }  
  271.       
  272.     /**
  273.      * 删除目录及地下的全部文件
  274.      * @param string $dir
  275.      * @return bool
  276.      */  
  277.     static public function delDirOfAll($dir) {  
  278.         //先删除目录下的文件:  
  279.         if (is_dir($dir)) {  
  280.             $dh=opendir($dir);  
  281.             while (!!$file=readdir($dh)) {  
  282.                 if($file!="." && $file!="..") {  
  283.                     $fullpath=$dir."/".$file;  
  284.                     if(!is_dir($fullpath)) {  
  285.                         unlink($fullpath);  
  286.                     } else {  
  287.                         self::delDirOfAll($fullpath);  
  288.                     }  
  289.                 }  
  290.             }  
  291.             closedir($dh);  
  292.             //删除当前文件夹:  
  293.             if(rmdir($dir)) {  
  294.                 return true;  
  295.             } else {  
  296.                 return false;  
  297.             }  
  298.         }  
  299.     }  
  300.   
  301.     /**
  302.      * 验证登陆
  303.      */  
  304.     static public function validateLogin() {  
  305.         if (empty($_SESSION['admin']['user'])) header('Location:/admin/');  
  306.     }  
  307.       
  308.     /**
  309.      * 给已经存在的图片添加水印
  310.      * @param string $file_path
  311.      * @return bool
  312.      */  
  313.     static public function addMark($file_path) {  
  314.         if (file_exists($file_path) && file_exists(MARK)) {  
  315.             //求出上传图片的名称后缀  
  316.             $ext_name = strtolower(substr($file_path, strrpos($file_path, '.'), strlen($file_path)));  
  317.             //$new_name='jzy_' . time() . rand(1000,9999) . $ext_name ;  
  318.             $store_path = ROOT_PATH . UPDIR;  
  319.             //求上传图片高宽  
  320.             $imginfo = getimagesize($file_path);  
  321.             $width = $imginfo[0];  
  322.             $height = $imginfo[1];  
  323.              //添加图片水印               
  324.             switch($ext_name) {  
  325.                 case '.gif':  
  326.                     $dst_im = imagecreatefromgif($file_path);  
  327.                     break;  
  328.                 case '.jpg':  
  329.                     $dst_im = imagecreatefromjpeg($file_path);  
  330.                     break;  
  331.                 case '.png':  
  332.                     $dst_im = imagecreatefrompng($file_path);  
  333.                     break;  
  334.             }  
  335.             $src_im = imagecreatefrompng(MARK);  
  336.             //求水印图片高宽  
  337.             $src_imginfo = getimagesize(MARK);  
  338.             $src_width = $src_imginfo[0];  
  339.             $src_height = $src_imginfo[1];  
  340.             //求出水印图片的实际生成位置  
  341.             $src_x = $width - $src_width - 10;  
  342.             $src_y = $height - $src_height - 10;  
  343.             //新建一个真彩色图像  
  344.             $nimage = imagecreatetruecolor($width, $height);                 
  345.             //拷贝上传图片到真彩图像  
  346.             imagecopy($nimage, $dst_im, 0, 0, 0, 0, $width, $height);            
  347.             //按坐标位置拷贝水印图片到真彩图像上  
  348.             imagecopy($nimage, $src_im, $src_x, $src_y, 0, 0, $src_width, $src_height);  
  349.             //分情况输出生成后的水印图片  
  350.             switch($ext_name) {  
  351.                 case '.gif':  
  352.                     imagegif($nimage, $file_path);  
  353.                     break;  
  354.                 case '.jpg':  
  355.                     imagejpeg($nimage, $file_path);  
  356.                     break;  
  357.                 case '.png':  
  358.                     imagepng($nimage, $file_path);  
  359.                     break;      
  360.             }  
  361.             //释放资源   
  362.             imagedestroy($dst_im);  
  363.             imagedestroy($src_im);  
  364.             unset($imginfo);  
  365.             unset($src_imginfo);  
  366.             //移动生成后的图片  
  367.             @move_uploaded_file($file_path, ROOT_PATH.UPDIR . $file_path);  
  368.         }  
  369.     }  
  370.       
  371.     /**
  372.     *  中文截取2,单字节截取模式
  373.     * @access public
  374.     * @param string $str  需要截取的字符串
  375.     * @param int $slen  截取的长度
  376.     * @param int $startdd  开始标记处
  377.     * @return string
  378.     */  
  379.     static public function cn_substr($str, $slen, $startdd=0){  
  380.         $cfg_soft_lang = PAGECHARSET;  
  381.         if($cfg_soft_lang=='utf-8') {  
  382.             return self::cn_substr_utf8($str, $slen, $startdd);  
  383.         }  
  384.         $restr = '';  
  385.         $c = '';  
  386.         $str_len = strlen($str);  
  387.         if($str_len < $startdd+1) {  
  388.             return '';  
  389.         }  
  390.         if($str_len < $startdd + $slen || $slen==0) {  
  391.             $slen = $str_len - $startdd;  
  392.         }  
  393.         $enddd = $startdd + $slen - 1;  
  394.         for($i=0;$i<$str_len;$i++) {  
  395.             if($startdd==0) {  
  396.                 $restr .= $c;  
  397.             } elseif($i > $startdd) {  
  398.                 $restr .= $c;  
  399.             }  
  400.             if(ord($str[$i])>0x80) {  
  401.                 if($str_len>$i+1) {  
  402.                     $c = $str[$i].$str[$i+1];  
  403.                 }  
  404.                 $i++;  
  405.             } else {  
  406.                 $c = $str[$i];  
  407.             }  
  408.             if($i >= $enddd) {  
  409.                 if(strlen($restr)+strlen($c)>$slen) {  
  410.                     break;  
  411.                 } else {  
  412.                     $restr .= $c;  
  413.                     break;  
  414.                 }  
  415.             }  
  416.         }  
  417.         return $restr;  
  418.     }  
  419.   
  420.     /**
  421.     *  utf-8中文截取,单字节截取模式
  422.     *
  423.     * @access public
  424.     * @param string $str 需要截取的字符串
  425.     * @param int $slen 截取的长度
  426.     * @param int $startdd 开始标记处
  427.     * @return string
  428.     */  
  429.     static public function cn_substr_utf8($str, $length, $start=0) {  
  430.         if(strlen($str) < $start+1) {  
  431.             return '';  
  432.         }  
  433.         preg_match_all("/./su", $str, $ar);  
  434.         $str = '';  
  435.         $tstr = '';  
  436.         //为了兼容mysql4.1以下版本,与数据库varchar一致,这里使用按字节截取  
  437.         for($i=0; isset($ar[0][$i]); $i++) {  
  438.             if(strlen($tstr) < $start) {  
  439.                 $tstr .= $ar[0][$i];  
  440.             } else {  
  441.                 if(strlen($str) < $length + strlen($ar[0][$i]) ) {  
  442.                     $str .= $ar[0][$i];  
  443.                 } else {  
  444.                     break;  
  445.                 }  
  446.             }  
  447.         }  
  448.         return $str;  
  449.     }  
  450.       
  451.     /**
  452.      * 删除图片,根据图片ID
  453.      * @param int $image_id
  454.      */  
  455.     static function delPicByImageId($image_id) {  
  456.         $db_name = PREFIX . 'images i';  
  457.         $m = new Model();  
  458.         $data = $m->getOne($db_name, "i.id={$image_id}", "i.path as p, i.big_img as b, i.small_img as s");  
  459.         foreach ($data as $v) {  
  460.             @self::delFile(ROOT_PATH . $v['p']);  
  461.             @self::delFile(ROOT_PATH . $v['b']);  
  462.             @self::delFile(ROOT_PATH . $v['s']);  
  463.         }  
  464.         $m->del(PREFIX . 'images', "id={$image_id}");  
  465.         unset($m);  
  466.     }  
  467.       
  468.     /**
  469.      * 图片等比例缩放
  470.      * @param resource $im    新建图片资源(imagecreatefromjpeg/imagecreatefrompng/imagecreatefromgif)
  471.      * @param int $maxwidth   生成图像宽
  472.      * @param int $maxheight  生成图像高
  473.      * @param string $name    生成图像名称
  474.      * @param string $filetype文件类型(.jpg/.gif/.png)
  475.      */  
  476.     static public function resizeImage($im, $maxwidth, $maxheight, $name, $filetype) {  
  477.         $pic_width = imagesx($im);  
  478.         $pic_height = imagesy($im);  
  479.         if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {  
  480.             if($maxwidth && $pic_width>$maxwidth) {  
  481.                 $widthratio = $maxwidth/$pic_width;  
  482.                 $resizewidth_tag = true;  
  483.             }  
  484.             if($maxheight && $pic_height>$maxheight) {  
  485.                 $heightratio = $maxheight/$pic_height;  
  486.                 $resizeheight_tag = true;  
  487.             }  
  488.             if($resizewidth_tag && $resizeheight_tag) {  
  489.                 if($widthratio<$heightratio)  
  490.                     $ratio = $widthratio;  
  491.                 else  
  492.                     $ratio = $heightratio;  
  493.             }  
  494.             if($resizewidth_tag && !$resizeheight_tag)  
  495.                 $ratio = $widthratio;  
  496.             if($resizeheight_tag && !$resizewidth_tag)  
  497.                 $ratio = $heightratio;  
  498.             $newwidth = $pic_width * $ratio;  
  499.             $newheight = $pic_height * $ratio;  
  500.             if(function_exists("imagecopyresampled")) {  
  501.                 $newim = imagecreatetruecolor($newwidth,$newheight);  
  502.                 imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);  
  503.             } else {  
  504.                 $newim = imagecreate($newwidth,$newheight);  
  505.                 imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);  
  506.             }  
  507.             $name = $name.$filetype;  
  508.             imagejpeg($newim,$name);  
  509.             imagedestroy($newim);  
  510.         } else {  
  511.             $name = $name.$filetype;  
  512.             imagejpeg($im,$name);  
  513.         }  
  514.     }  
  515.   
  516.     /**
  517.      * 下载文件
  518.      * @param string $file_path 绝对路径
  519.      */  
  520.     static public function downFile($file_path) {  
  521.         //判断文件是否存在  
  522.         $file_path = iconv('utf-8', 'gb2312', $file_path); //对可能出现的中文名称进行转码  
  523.         if (!file_exists($file_path)) {  
  524.             exit('文件不存在!');  
  525.         }  
  526.         $file_name = basename($file_path); //获取文件名称  
  527.         $file_size = filesize($file_path); //获取文件大小  
  528.         $fp = fopen($file_path, 'r'); //以只读的方式打开文件  
  529.         header("Content-type: application/octet-stream");  
  530.         header("Accept-Ranges: bytes");  
  531.         header("Accept-Length: {$file_size}");  
  532.         header("Content-Disposition: attachment;filename={$file_name}");  
  533.         $buffer = 1024;  
  534.         $file_count = 0;  
  535.         //判断文件是否结束  
  536.         while (!feof($fp) && ($file_size-$file_count>0)) {  
  537.             $file_data = fread($fp, $buffer);  
  538.             $file_count += $buffer;  
  539.             echo $file_data;  
  540.         }  
  541.         fclose($fp); //关闭文件  
  542.     }  
  543. }  
  544. ?>
复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|技术支持|开发手册|Odoo中文网-远鼎旗下odoo培训网站 ( 苏ICP备15039516号 )

GMT+8, 2024-5-8 19:27 , Processed in 0.013006 second(s), 9 queries , Xcache On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表