ホームページ  >  記事  >  バックエンド開発  >  PHPツール

PHPツール

WBOY
WBOYオリジナル
2016-07-25 08:48:392822ブラウズ
PHPツールクラス、システム開発に必須のクラス:
フォーム検証クラス
検証コードクラス
ログクラス
ページングクラス
無制限极分类类
  1. class Lib_Form
  2. {
  3. private $typeArr=array('isNotEmpty' , 'isInt' , 'isStr' , 'isEmail' , 'isTel' , 'isOnlyNum' , 'hasSet', 'isOnlyChar ' , 'isNumAndChar' , 'checkLength');
  4. private $msg = array();
  5. private $code = 0;
  6. public function validata($post)
  7. {
  8. if(!is_array($post))
  9. {
  10. $this->msg[] = 'データは配列ではありません';
  11. }
  12. else
  13. {
  14. foreach ($post as $field=>$value)
  15. {
  16. $func = $post[$field][ 'valid'];
  17. $value = $post[$field]['value'];
  18. $checkLength = 'checkLength';
  19. if($pos =tripos($func , $checkLength)!=​​=false)
  20. {
  21. $condition = substr($func, strlen($checkLength));
  22. $func = $checkLength;
  23. $lengthArr =explode('-', $condition);
  24. self::$func($value , $field , $lengthArr[0] , $lengthArr[1]);
  25. }
  26. else
  27. {
  28. if(!in_array($func , $this->typeArr))
  29. {
  30. $this->msg = $func. ' isNotExists';
  31. break;
  32. }
  33. self::$func($value , $field);
  34. }
  35. }
  36. }
  37. return $this->showRestult();
  38. }
  39. プライベート関数 showRestult()
  40. {
  41. if($this->msg && is_array($this->msg))
  42. {
  43. $this->code = 1;
  44. $msg = implode(',', $this->msg);
  45. $ret = array('code'=>$this->code , 'msg'=>$msg);
  46. return $ret;
  47. }
  48. return array('code'=>$this-> ;code , 'msg'=>'success');
  49. }
  50. プライベート関数 isNotEmpty($value,$field)
  51. {
  52. if(!$this->hasSet($value, $field)) return false ;
  53. $value = トリム($value);
  54. if(empty($value))
  55. {
  56. $this->msg[] = $field.' isEmpty';
  57. return false;
  58. }
  59. return true;
  60. }
  61. プライベート関数 isInt($value,$field)
  62. {
  63. if(!$this->isNotEmpty($value,$field)) return false;
  64. $value = trim($value);
  65. if(!is_int($value))
  66. {
  67. $this->msg[] = $field.' isNotInt';
  68. return false;
  69. }
  70. return true;
  71. }
  72. プライベート関数 isStr($value,$field)
  73. {
  74. if(!$this->isNotEmpty($value,$field)) return false;
  75. $value = トリム($value);
  76. if(!is_string($value))
  77. {
  78. $this->msg[] = $field.' isNotStr';
  79. return false;
  80. }
  81. return true;
  82. }
  83. プライベート関数 hasSet($value , $field)
  84. {
  85. if(!isset($value))
  86. {
  87. $this->msg[] = $フィールド。 isNotSet';
  88. return false;
  89. }
  90. return true;
  91. }
  92. プライベート関数 isEmail($value,$field)
  93. {
  94. if(!$this->isNotEmpty($value,$field)) return false;
  95. $value = トリム($value);
  96. $pattern = "/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_ -]+)$/";
  97. if(!preg_match($pattern, $value))
  98. {
  99. $this->msg[] = $field.' isNotEmail';
  100. return false;
  101. }
  102. return true;
  103. }
  104. プライベート関数 isTel($value,$field)
  105. {
  106. if(!$this->isNotEmpty($value,$field)) return false;
  107. $value = トリム($value);
  108. $pattern = '/^[0-9]{7,11}$/';
  109. if (!preg_match($pattern, $value))
  110. {
  111. $this- >msg[] = $field.' isNotTel';
  112. return false;
  113. }
  114. return true;
  115. }
  116. プライベート関数 isOnlyNum($value,$field)
  117. {
  118. if(!$this->isNotEmpty($value,$field)) return false;
  119. $value = トリム($value);
  120. $pattern = "/^[0-9]{1,}$/";
  121. if(!preg_match($pattern, $value))
  122. {
  123. $this-> ;msg[] = $field.' isNotOnlyNum';
  124. return false;
  125. }
  126. return true;
  127. }
  128. プライベート関数 isOnlyChar($value,$field)
  129. {
  130. if(!$this->isNotEmpty($value,$field)) return false;
  131. $value = トリム($value);
  132. $pattern = "/^[a-zA-Z]{1,}$/";
  133. if(!preg_match($pattern, $value))
  134. {
  135. $this ->msg[] = $field.' isNotOnlyChar';
  136. return false;
  137. }
  138. return true;
  139. }
  140. プライベート関数 isNumAndChar($value,$field)
  141. {
  142. if(!$this->isNotEmpty($value,$field)) return false;
  143. $value = トリム($value);
  144. $pattern = "/^[a-zA-z0-9]{1,}$/";
  145. if(!preg_match($pattern , $value))
  146. {
  147. $this->msg[] = $field.' isNotNumAndChar';
  148. return false;
  149. }
  150. return true;
  151. }
  152. プライベート関数 checkLength($value , $field , $minLength , $maxLength)
  153. {
  154. if(!$this->isNotEmpty($value,$field)) return false;
  155. $value = trim($value) ;
  156. $length = (strlen($value) + mb_strlen($value,'UTF8')) / 2;
  157. if($length < $minLength || $length > $maxLength)
  158. {
  159. $this->msg[] = $field.' isNotInLength';
  160. return false;
  161. }
  162. return true;
  163. }
  164. }
  165. if($_POST['submit'])
  166. {
  167. $form = new Lib_Form();
  168. $post['name'] = array ('value'=>$_POST['name'] , 'valid'=>'checkLength6-12');
  169. $post['pwd'] = array('value'=>$_POST['pwd '] , 'valid'=>'checkLength4-12');
  170. $post['sex'] = array('value'=>$_POST['sex'] , 'valid'=>'hasSet' );
  171. $ret = $form->validata($post);
  172. if($ret['code'])
  173. {
  174. echo $ret['msg'];
  175. }
  176. }
  177. ?>
复制代
  1. class Lib_Image
  2. {
  3. private $height = 0;
  4. private $width = 0;
  5. public function __construct($height , $width)
  6. {
  7. $this->height = $ height;
  8. $this->width = $width;
  9. }
  10. プライベート関数 genCode($num)
  11. {
  12. for($i=0;$i<$num;$i++)// 生成验证码
  13. {
  14. switch(rand(0,2))
  15. {
  16. case 0:$code[$i]=chr(rand(48,57));break;// 数字
  17. case 1:$code[$i]=chr (rand(65,90));break;//大写字母
  18. case 2:$code[$i]=chr(rand(97,122));break;//小写字母
  19. }
  20. }
  21. $_SESSION["VerifyCode "]=$コード;
  22. $code を返します;
  23. }
  24. private function genOther($image)
  25. {
  26. for($i=0;$i<80;$i++)// 干扰像素生成
  27. {
  28. $dis_color=imagecolorallocate($image,rand(0,2555) ),rand(0,255),rand(0,255));
  29. imagesetpixel($image,rand(1,$this->width),rand(1,$this->height),$dis_color);
  30. }
  31. }
  32. public function VeryCode()
  33. {
  34. $image=imagecreate($this->width,$this->height);
  35. imagecolorallocate($image,255,255,255);
  36. //$this->genOther ($image);
  37. $num = 4;
  38. $code = $this->genCode($num);
  39. for($i=0;$i {
  40. $char_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
  41. imagechar($image,60,($this->width/$num)*$ i,rand(0,5),$code[$i],$char_color);
  42. }
  43. header("Content-type:image/png");
  44. imagepng($image);//输出图像到浏览器
  45. imagedestroy($image);//释放资源
  46. }
  47. }
  48. $image = new Lib_Image(25, 65);
  49. $image->veryCode();
  50. ?>
复制代
  1. class Lib_Log
  2. {
  3. private $logError = 0;
  4. private $logWarn = 1;
  5. private $logDebug = 2;
  6. private $logDir = 'log/';
  7. private $logFile = ' log';
  8. プライベート $fileExt = '.txt';
  9. プライベート $fileHander = null;
  10. public function __construct()
  11. {
  12. if(!is_dir($this->logDir)){
  13. mkdir($this->logDir,0777);
  14. }
  15. $this->logFile .= date( 'Y-m-d').$this->fileExt;
  16. if(!$this->fileHander = @fopen($this->logDir.$this->logFile, 'a+')){
  17. die ('ログ ファイルを開くことができません!');
  18. }
  19. }
  20. public function writeLog($message)
  21. {
  22. $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0';
  23. $debug = debug_backtrace(true);
  24. $string = date('Y-m-d H:i:s')."t";
  25. $string .= $ip ."t";
  26. $string .=$debug[0]['file']."t";
  27. $string .= "tline" . $debug[0]['line']."t";
  28. $string .= json_encode($message)."rn";
  29. if(!fwrite($this->fileHander, $string)){
  30. die ('ログ ファイルに書き込めません!');
  31. }
  32. }
  33. public function __destruct()
  34. {
  35. if($this->fileHander!=null){
  36. fclose($this->fileHander) ;
  37. }
  38. }
  39. }
  40. $log = new Lib_Log();
  41. $log->writeLog('エラー デバッグ!');
  42. echo "";
  43. ?>
复制代
  1. class Lib_Page
  2. {
  3. public $currentPage=0; //当前页数
  4. private $totalPage=0; //总页数
  5. private $totalNums=0; //总记录数
  6. private $perNums=0; // 表示される记录数
  7. private $type = 0; //表示型
  8. public function __construct($totalNums , $perNums,$type=0)
  9. {
  10. $this->totalNums = intval($totalNums);
  11. $this->perNums = intval($perNums) ;
  12. $this->totalPage = intval(ceil($this->totalNums / $this->perNums));
  13. $this->currentPage = min(max(1 , $_REQUEST['p'] ) , $this->totalPage);
  14. $this->type = intval($type);
  15. }
  16. プライベート関数 first()
  17. {
  18. if ($this->currentPage==1) return false ;
  19. return "首页  ";
  20. }
  21. プライベート関数 last()
  22. {
  23. if ($this->currentPage= =$this->totalPage) return false;
  24. return "尾页  ";
  25. }
  26. プライベート関数 next()
  27. {
  28. $p = min($this->currentPage+1 , $this->totalPage);
  29. if ($p==$this->totalPage) return false;
  30. return "下一页  ";
  31. }
  32. プライベート関数 prev()
  33. {
  34. $p = max(1 , $this->currentPage - 1);
  35. if($p==1) return false;
  36. return "上一页  ; ";
  37. }
  38. プライベート関数 total()
  39. {
  40. return "共 {$this->totalPage} 页 | {$this->totalNums} 条记录 | 当前第 {$this->currentPage} 页";
  41. }
  42. private function page()
  43. {
  44. $show = "";
  45. for ($i=1; $i< ;=$this->totalPage; $i++){
  46. if ($i==$this->currentPage)
  47. $show .= "{$i}  ";
  48. else
  49. $show .= "{$i}  ";
  50. }
  51. return $show;
  52. }
  53. public function show()
  54. {
  55. if ($this->type==1) {
  56. return $this->total() 。」 '.$this->page();
  57. }else if($this->type==2){
  58. return $this->total().' '.$this->first().' '.$this->prev().' '.$this->next().' '.$this->last();
  59. }elseif ($this->type==0){
  60. return $this->total().' '.$this->first().' '.$this->prev().' '.$this->page().' '.$this->next().' '.$this->last();
  61. }
  62. }
  63. }
  64. $totalNums = 80;
  65. $perNums = 10;
  66. $page = new Lib_Page($totalNums, $perNums);
  67. echo $page- >show();
  68. ?>
复制代
  1. class Lib_Tree
  2. {
  3. private $items = array();
  4. private $icon = array(
  5. '§',
  6. ' §',
  7. '   ; §',
  8. ' §',
  9. ' §',
  10. ' └',
  11. );
  12. private $field = array('id','name');
  13. public $ret = '< tr>';
  14. public function __construct($items)
  15. {
  16. $this->items = $items;
  17. }
  18. public function setIcon($icon)
  19. {
  20. $this->icon = $icon;
  21. }
  22. public function getChildren($pid)
  23. {
  24. foreach ($this->items as $item)
  25. {
  26. if($item['pid' ]==$pid)
  27. {
  28. $children[] = $item;
  29. }
  30. }
  31. return $children && is_array($children) ? $children : false;
  32. }
  33. public function getParent($id)
  34. {
  35. return $this->items[$this->items[$id]['pid']];
  36. }
  37. public function show($pid)
  38. {
  39. $children = $this->getChildren($pid);
  40. if(!$children) return false;
  41. foreach ($children as $child)
  42. {
  43. $this->ret .='
  44. ';
  45. $this->ret.='
  46. ';
  47. $this->ret.='
  48. ';
  49. $this->ret.='
  50. ';
  51. $this->show($child['id']);
  52. }
  53. }
  54. }
  55. $items = array(
  56. array('id'=>1 , 'name'=>'湖北', 'pid'=>0, 'level'=>0),
  57. array('id'=>2 , 'name'=>'武汉', 'pid '=>1, 'level'=>1),
  58. array('id'=>3 , 'name'=>'孝感', 'pid'=>1, 'level'=> 1),
  59. array('id'=>4 , 'name'=>'广东', 'pid'=>0, 'level'=>0),
  60. array('id'=> 5 , 'name'=>'广州', 'pid'=>4, 'level'=>1),
  61. array('id'=>6 , 'name'=>'深圳', 'pid'=>4, 'level'=>1),
  62. array('id'=>7 , 'name'=>'东莞', 'pid'=>4, 'level'= >1),
  63. array('id'=>8 , 'name'=>宜昌', 'pid'=>1, 'level'=>1),
  64. array('id'= >9 , 'name'=>'云梦', 'pid'=>3, 'level'=>2),
  65. array('id'=>10 , 'name'=>'南山区', 'pid'=>6, 'level'=>2),
  66. array('id'=>11 , 'name'=>'宝安全', 'pid'=>6 , 'level'=>2),
  67. array('id'=>12 , 'name'=>'倒店', 'pid'=>9, 'level'=>3),
  68. array('id'=>13 , 'name'=>'罗范大队', 'pid'=>12, 'level'=>4),
  69. array('id'=>14 , 'name'=>'下范存', 'pid'=>13, 'level'=>5),
  70. );
  71. $tree = new Lib_Tree($items);
  72. $tree-> show(0);
  73. echo $tree->ret;
  74. ?>
  75. 复制代码
    名前 操作
    '.$this->icon[$child['level']].$child['name'].' 删除< ;/a> 追加 修正


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。