PHP工具类,开发系统必备的类: 表单验证类 验证码类 日志类 分页类 无限极分类类
-
class Lib_Form
- {
- private $typeArr=array('isNotEmpty' , 'isInt' , 'isStr' , 'isEmail' , 'isTel' , 'isOnlyNum' , 'hasSet', 'isOnlyChar' , 'isNumAndChar' , 'checkLength');
- private $msg = array();
- private $code = 0;
-
- public function validata($post)
- {
- if(!is_array($post))
- {
- $this->msg[] = 'data is not array';
- }
- else
- {
- foreach ($post as $field=>$value)
- {
- $func = $post[$field]['valid'];
- $value = $post[$field]['value'];
-
- $checkLength = 'checkLength';
- if($pos = stripos($func , $checkLength)!==false)
- {
- $condition = substr($func, strlen($checkLength));
- $func = $checkLength;
- $lengthArr = explode('-', $condition);
- self::$func($value , $field , $lengthArr[0] , $lengthArr[1]);
- }
- else
- {
- if(!in_array($func , $this->typeArr))
- {
- $this->msg = $func.' isNotExists';
- break;
- }
- self::$func($value , $field);
- }
- }
- }
- return $this->showRestult();
- }
-
- private function showRestult()
- {
- if($this->msg && is_array($this->msg))
- {
- $this->code = 1;
- $msg = implode(',', $this->msg);
- $ret = array('code'=>$this->code , 'msg'=>$msg);
- return $ret;
- }
- return array('code'=>$this->code , 'msg'=>'success');
- }
-
- private function isNotEmpty($value,$field)
- {
- if(!$this->hasSet($value, $field)) return false;
- $value = trim($value);
- if(empty($value))
- {
- $this->msg[] = $field.' isEmpty';
- return false;
- }
- return true;
- }
-
- private function isInt($value,$field)
- {
- if(!$this->isNotEmpty($value,$field)) return false;
- $value = trim($value);
- if(!is_int($value))
- {
- $this->msg[] = $field.' isNotInt';
- return false;
- }
- return true;
- }
-
- private function isStr($value,$field)
- {
- if(!$this->isNotEmpty($value,$field)) return false;
- $value = trim($value);
- if(!is_string($value))
- {
- $this->msg[] = $field.' isNotStr';
- return false;
- }
- return true;
- }
-
- private function hasSet($value , $field)
- {
- if(!isset($value))
- {
- $this->msg[] = $field.' isNotSet';
- return false;
- }
- return true;
- }
-
- private function isEmail($value,$field)
- {
- if(!$this->isNotEmpty($value,$field)) return false;
- $value = trim($value);
- $pattern = "/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)$/";
- if(!preg_match($pattern, $value))
- {
- $this->msg[] = $field.' isNotEmail';
- return false;
- }
- return true;
- }
-
- private function isTel($value,$field)
- {
- if(!$this->isNotEmpty($value,$field)) return false;
- $value = trim($value);
- $pattern = '/^[0-9]{7,11}$/';
- if (!preg_match($pattern, $value))
- {
- $this->msg[] = $field.' isNotTel';
- return false;
- }
- return true;
- }
-
- private function isOnlyNum($value,$field)
- {
- if(!$this->isNotEmpty($value,$field)) return false;
- $value = trim($value);
- $pattern = "/^[0-9]{1,}$/";
- if(!preg_match($pattern, $value))
- {
- $this->msg[] = $field.' isNotOnlyNum';
- return false;
- }
- return true;
- }
-
- private function isOnlyChar($value,$field)
- {
- if(!$this->isNotEmpty($value,$field)) return false;
- $value = trim($value);
- $pattern = "/^[a-zA-Z]{1,}$/";
- if(!preg_match($pattern, $value))
- {
- $this->msg[] = $field.' isNotOnlyChar';
- return false;
- }
- return true;
- }
-
- private function isNumAndChar($value,$field)
- {
- if(!$this->isNotEmpty($value,$field)) return false;
- $value = trim($value);
- $pattern = "/^[a-zA-z0-9]{1,}$/";
- if(!preg_match($pattern , $value))
- {
- $this->msg[] = $field.' isNotNumAndChar';
- return false;
- }
- return true;
- }
-
- private function checkLength($value , $field , $minLength , $maxLength)
- {
- if(!$this->isNotEmpty($value,$field)) return false;
- $value = trim($value);
- $length = (strlen($value) + mb_strlen($value,'UTF8')) / 2;
- if($length $maxLength)
- {
- $this->msg[] = $field.' isNotInLength';
- return false;
- }
- return true;
- }
- }
-
- if($_POST['submit'])
- {
- $form = new Lib_Form();
- $post['name'] = array('value'=>$_POST['name'] , 'valid'=>'checkLength6-12');
- $post['pwd'] = array('value'=>$_POST['pwd'] , 'valid'=>'checkLength4-12');
- $post['sex'] = array('value'=>$_POST['sex'] , 'valid'=>'hasSet');
-
- $ret = $form->validata($post);
- if($ret['code'])
- {
- echo $ret['msg'];
- }
- }
- ?>
-
-
复制代码
-
class Lib_Image
- {
- private $height = 0;
- private $width = 0;
-
- public function __construct($height , $width)
- {
- $this->height = $height;
- $this->width = $width;
- }
-
- private function genCode($num)
- {
- for($i=0;$i {
- switch(rand(0,2))
- {
- case 0:$code[$i]=chr(rand(48,57));break;//数字
- case 1:$code[$i]=chr(rand(65,90));break;//大写字母
- case 2:$code[$i]=chr(rand(97,122));break;//小写字母
- }
- }
- $_SESSION["VerifyCode"]=$code;
- return $code;
- }
-
- private function genOther($image)
- {
- for($i=0;$i {
- $dis_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
- imagesetpixel($image,rand(1,$this->width),rand(1,$this->height),$dis_color);
- }
- }
-
- public function veryCode()
- {
-
- $image=imagecreate($this->width,$this->height);
- imagecolorallocate($image,255,255,255);
- //$this->genOther($image);
-
- $num = 4;
- $code = $this->genCode($num);
- for($i=0;$i {
- $char_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
- imagechar($image,60,($this->width/$num)*$i,rand(0,5),$code[$i],$char_color);
- }
-
- header("Content-type:image/png");
- imagepng($image);//输出图像到浏览器
- imagedestroy($image);//释放资源
- }
- }
-
- $image = new Lib_Image(25, 65);
- $image->veryCode();
- ?>
复制代码
-
class Lib_Log
- {
- private $logError = 0;
- private $logWarn = 1;
- private $logDebug = 2;
- private $logDir = 'log/';
- private $logFile = 'log';
- private $fileExt = '.txt';
- private $fileHander = null;
-
- public function __construct()
- {
- if(!is_dir($this->logDir)){
- mkdir($this->logDir,0777);
- }
- $this->logFile .= date('Y-m-d').$this->fileExt;
- if(!$this->fileHander = @fopen($this->logDir.$this->logFile, 'a+')){
- die('the log file can not be open!');
- }
- }
-
- public function writeLog($message)
- {
- $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0';
- $debug = debug_backtrace(true);
- $string = date('Y-m-d H:i:s')."\t";
- $string .= $ip."\t";
- $string .=$debug[0]['file']."\t";
- $string .= "\tline" . $debug[0]['line']."\t";
- $string .= json_encode($message)."\r\n";
- if(!fwrite($this->fileHander, $string)){
- die('the log file can not be written!');
- }
- }
-
- public function __destruct()
- {
- if($this->fileHander!=null){
- fclose($this->fileHander);
- }
- }
-
- }
-
- $log = new Lib_Log();
- $log->writeLog('the error debug!');
- echo "";
-
- ?>
复制代码
-
class Lib_Page
- {
- public $currentPage=0; //当前页数
- private $totalPage=0; //总页数
- private $totalNums=0; //总记录数
- private $perNums=0; //每页显示的记录数
- private $type = 0; //显示类型
-
- public function __construct($totalNums , $perNums,$type=0)
- {
- $this->totalNums = intval($totalNums);
- $this->perNums = intval($perNums);
- $this->totalPage = intval(ceil($this->totalNums / $this->perNums));
- $this->currentPage = min(max(1 , $_REQUEST['p']) , $this->totalPage);
- $this->type = intval($type);
- }
-
- private function first()
- {
- if ($this->currentPage==1) return false;
- return "首页 ";
- }
-
- private function last()
- {
- if ($this->currentPage==$this->totalPage) return false;
- return "尾页 ";
- }
-
- private function next()
- {
- $p = min($this->currentPage+1 , $this->totalPage);
- if ($p==$this->totalPage) return false;
- return "下一页 ";
- }
-
- private function prev()
- {
- $p = max(1 , $this->currentPage - 1);
- if($p==1) return false;
- return "上一页 ";
- }
-
- private function total()
- {
- return "共 {$this->totalPage} 页 | {$this->totalNums} 条记录 | 当前第 {$this->currentPage} 页";
- }
-
- private function page()
- {
- $show = "";
- for ($i=1; $itotalPage; $i++){
- if ($i==$this->currentPage)
- $show .= "{$i} ";
- else
- $show .= "{$i} ";
- }
- return $show;
- }
-
- public function show()
- {
- if ($this->type==1) {
- return $this->total().' '.$this->page();
- }else if($this->type==2){
- return $this->total().' '.$this->first().' '.$this->prev().' '.$this->next().' '.$this->last();
- }elseif ($this->type==0){
- return $this->total().' '.$this->first().' '.$this->prev().' '.$this->page().' '.$this->next().' '.$this->last();
- }
- }
-
- }
-
- $totalNums = 80;
- $perNums = 10;
- $page = new Lib_Page($totalNums, $perNums);
- echo $page->show();
- ?>
复制代码
-
class Lib_Tree
- {
- private $items = array();
- private $icon = array(
- '├',
- ' ├',
- ' ├',
- ' ├',
- ' ├',
- ' └',
- );
- private $field = array('id','name');
- public $ret = '
类名 | 操作 |
';-
- public function __construct($items)
- {
- $this->items = $items;
- }
-
- public function setIcon($icon)
- {
- $this->icon = $icon;
- }
-
- public function getChildren($pid)
- {
- foreach ($this->items as $item)
- {
- if($item['pid']==$pid)
- {
- $children[] = $item;
- }
- }
- return $children && is_array($children) ? $children : false;
- }
-
- public function getParent($id)
- {
- return $this->items[$this->items[$id]['pid']];
- }
-
- public function show($pid)
- {
- $children = $this->getChildren($pid);
- if(!$children) return false;
- foreach ($children as $child)
- {
- $this->ret.='
';- $this->ret.='
'.$this->icon[$child['level']].$child['name'].' | ';
- $this->ret.='
删除 添加 修改
| ';
- $this->ret.='
';
- $this->show($child['id']);
- }
- }
-
-
- }
-
- $items = array(
- array('id'=>1 , 'name'=>'湖北', 'pid'=>0, 'level'=>0),
- array('id'=>2 , 'name'=>'武汉', 'pid'=>1, 'level'=>1),
- array('id'=>3 , 'name'=>'孝感', 'pid'=>1, 'level'=>1),
- array('id'=>4 , 'name'=>'广东', 'pid'=>0, 'level'=>0),
- array('id'=>5 , 'name'=>'广州', 'pid'=>4, 'level'=>1),
- array('id'=>6 , 'name'=>'深圳', 'pid'=>4, 'level'=>1),
- array('id'=>7 , 'name'=>'东莞', 'pid'=>4, 'level'=>1),
- array('id'=>8 , 'name'=>'宜昌', 'pid'=>1, 'level'=>1),
- array('id'=>9 , 'name'=>'云梦', 'pid'=>3, 'level'=>2),
- array('id'=>10 , 'name'=>'南山区', 'pid'=>6, 'level'=>2),
- array('id'=>11 , 'name'=>'宝安全', 'pid'=>6, 'level'=>2),
- array('id'=>12 , 'name'=>'倒店', 'pid'=>9, 'level'=>3),
- array('id'=>13 , 'name'=>'罗范大队', 'pid'=>12, 'level'=>4),
- array('id'=>14 , 'name'=>'下范存', 'pid'=>13, 'level'=>5),
- );
-
- $tree = new Lib_Tree($items);
- $tree->show(0);
- echo $tree->ret;
-
- ?>复制代码
|