PHP Tool Classes

Source: Internet
Author: User
PHP tool classes, developing prerequisites for the class:
Form Validation Class
Verification Code Class
Log class
Pagination class
Infinite Pole Classification class
  1. Class Lib_form
  2. {
  3. Private $typeArr =array (' isnotempty ', ' isint ', ' isstr ', ' isemail ', ' Istel ', ' isonlynum ', ' hasset ', ' Isonlychar ', ' is Numandchar ', ' checklength ');
  4. Private $msg = Array ();
  5. Private $code = 0;
  6. Public Function Validata ($post)
  7. {
  8. if (!is_array ($post))
  9. {
  10. $this->msg[] = ' data is not array ';
  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 = Stripos ($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. Private Function 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. Private Function Isnotempty ($value, $field)
  51. {
  52. if (! $this->hasset ($value, $field)) return false;
  53. $value = Trim ($value);
  54. if (empty ($value))
  55. {
  56. $this->msg[] = $field. ' IsEmpty ';
  57. return false;
  58. }
  59. return true;
  60. }
  61. Private Function 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. Private Function Isstr ($value, $field)
  73. {
  74. if (! $this->isnotempty ($value, $field)) return false;
  75. $value = Trim ($value);
  76. if (!is_string ($value))
  77. {
  78. $this->msg[] = $field. ' Isnotstr ';
  79. return false;
  80. }
  81. return true;
  82. }
  83. Private Function Hasset ($value, $field)
  84. {
  85. if (!isset ($value))
  86. {
  87. $this->msg[] = $field. ' Isnotset ';
  88. return false;
  89. }
  90. return true;
  91. }
  92. Private Function Isemail ($value, $field)
  93. {
  94. if (! $this->isnotempty ($value, $field)) return false;
  95. $value = Trim ($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. Private Function Istel ($value, $field)
  105. {
  106. if (! $this->isnotempty ($value, $field)) return false;
  107. $value = Trim ($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. Private Function Isonlynum ($value, $field)
  117. {
  118. if (! $this->isnotempty ($value, $field)) return false;
  119. $value = Trim ($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. Private Function Isonlychar ($value, $field)
  129. {
  130. if (! $this->isnotempty ($value, $field)) return false;
  131. $value = Trim ($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. Private Function Isnumandchar ($value, $field)
  141. {
  142. if (! $this->isnotempty ($value, $field)) return false;
  143. $value = Trim ($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. Private Function 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. ?>
Copy Code
  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. Private Function Gencode ($num)
  11. {
  12. for ($i =0; $i < $num; $i + +)//Generate Verification code
  13. {
  14. Switch (rand (0,2))
  15. {
  16. Case 0: $code [$i]=CHR (rand (48,57)); break;//number
  17. Case 1: $code [$i]=CHR (rand (65,90)); break;//Capital Letter
  18. Case 2: $code [$i]=CHR (rand (97,122)); break;//Small Letter
  19. }
  20. }
  21. $_session["Verifycode"]= $code;
  22. return $code;
  23. }
  24. Private Function Genother ($image)
  25. {
  26. for ($i =0; $i <80; $i + +)//Generate interference pixels
  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 < $num; $i + +)//print character to image
  40. {
  41. $char _color=imagecolorallocate ($image, Rand (0,2555), Rand (0,255), Rand (0,255));
  42. Imagechar ($image, Max, ($this->width/$num) * $i, Rand (0,5), $code [$i], $char _color);
  43. }
  44. Header ("Content-type:image/png");
  45. Imagepng ($image);//output image to browser
  46. Imagedestroy ($image);//Release resources
  47. }
  48. }
  49. $image = new Lib_image (25, 65);
  50. $image->verycode ();
  51. ?>
Copy Code
  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. Private $fileExt = '. txt ';
  9. Private $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 (' The log file can is not open! ');
  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). " \ r \ n ";
  29. if (!fwrite ($this->filehander, $string)) {
  30. Die (' The log file can is not written! ');
  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 (' the error debug! ');
  42. echo "";
  43. ?>
Copy Code
  1. Class Lib_page
  2. {
  3. Public $currentPage =0;//Current page
  4. Private $totalPage =0;//Total pages
  5. Private $totalNums = 0; Total Record Count
  6. Private $perNums =0;//The number of records displayed per page
  7. Private $type = 0;//Display Type
  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. Private Function First ()
  17. {
  18. if ($this->currentpage==1) return false;
  19. Return "Home";
  20. }
  21. Private Function Last ()
  22. {
  23. if ($this->currentpage== $this->totalpage) return false;
  24. Return "Totalpage} ' > last";
  25. }
  26. Private Function Next ()
  27. {
  28. $p = min ($this->currentpage+1, $this->totalpage);
  29. if ($p = = $this->totalpage) return false;
  30. Return "Next page";
  31. }
  32. Private Function prev ()
  33. {
  34. $p = max (1, $this->currentpage-1);
  35. if ($p ==1) return false;
  36. Return "Previous page";
  37. }
  38. Private Function Total ()
  39. {
  40. Return " Total {$this->totalpage} page | {$this->totalnums} records | Current {$this->currentpage} page ";
  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. ?>
Copy Code
  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 = '
  14. $this->ret.= '
  15. $this->ret.= '
  16. $this->ret.= '
  17. $this->show ($child [' id ']);
  18. ';
  19. Public function __construct ($items)
  20. {
  21. $this->items = $items;
  22. }
  23. Public Function SetIcon ($icon)
  24. {
  25. $this->icon = $icon;
  26. }
  27. Public Function GetChildren ($pid)
  28. {
  29. foreach ($this->items as $item)
  30. {
  31. if ($item [' pid ']== $pid)
  32. {
  33. $children [] = $item;
  34. }
  35. }
  36. return $children && Is_array ($children)? $children: false;
  37. }
  38. Public Function getParent ($id)
  39. {
  40. return $this->items[$this->items[$id] [' pid '];
  41. }
  42. Public function Show ($PID)
  43. {
  44. $children = $this->getchildren ($pid);
  45. if (! $children) return false;
  46. foreach ($children as $child)
  47. {
  48. $this->ret.= '
  49. '; '; '; ';
  50. }
  51. }
  52. }
  53. $items = Array (
  54. Array (' ID ' =>1, ' name ' = ' Hubei ', ' pid ' =>0, ' Level ' =>0),
  55. Array (' ID ' =>2, ' name ' = ' Wuhan ', ' pid ' =>1, ' Level ' =>1),
  56. Array (' ID ' =>3, ' name ' = ' Xiaogan ', ' pid ' =>1, ' Level ' =>1),
  57. Array (' ID ' =>4, ' name ' = ' Guangdong ', ' pid ' =>0, ' Level ' =>0),
  58. Array (' ID ' =>5, ' name ' = ' Guangzhou ', ' pid ' =>4, ' Level ' =>1),
  59. Array (' ID ' =>6, ' name ' = ' Shenzhen ', ' pid ' =>4, ' Level ' =>1),
  60. Array (' ID ' =>7, ' name ' = ' Dongguan ', ' pid ' =>4, ' Level ' =>1),
  61. Array (' ID ' =>8, ' name ' = ' Yichang ', ' pid ' =>1, ' Level ' =>1),
  62. Array (' ID ' =>9, ' name ' = ' Yunmeng ', ' pid ' =>3, ' Level ' =>2),
  63. Array (' ID ' =>10, ' name ' = ' Nanshan ', ' pid ' =>6, ' Level ' =>2),
  64. Array (' ID ' =>11, ' name ' = ' bao security ', ' pid ' =>6, ' Level ' =>2),
  65. Array (' ID ' =>12, ' name ' = = ' inverted shop ', ' pid ' =>9, ' Level ' =>3),
  66. Array (' ID ' =>13, ' name ' = ' Mrs Law Brigade ', ' pid ' =>12, ' Level ' =>4),
  67. Array (' ID ' =>14, ' name ' = ' + ' under Fan Cun ', ' pid ' =>13, ' Level ' =>5),
  68. );
  69. $tree = new Lib_tree ($items);
  70. $tree->show (0);
  71. Echo $tree->ret;
  72. ?> Copy Code
  73. class name Operation
    '. $this->icon[$child [' Level ']. $child [' name ']. 'Remove Add modifications
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.