PHP tool classes, developing prerequisites for the class: Form Validation Class Verification Code Class Log class Pagination class Infinite Pole Classification class
- Class Lib_form
- {
- Private $typeArr =array (' isnotempty ', ' isint ', ' isstr ', ' isemail ', ' Istel ', ' isonlynum ', ' hasset ', ' Isonlychar ', ' is Numandchar ', ' 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 < $minLength | | $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 '];
- }
- }
- ?>
Copy Code
- 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 < $num; $i + +)//Generate Verification code
- {
- Switch (rand (0,2))
- {
- Case 0: $code [$i]=CHR (rand (48,57)); break;//number
- Case 1: $code [$i]=CHR (rand (65,90)); break;//Capital Letter
- Case 2: $code [$i]=CHR (rand (97,122)); break;//Small Letter
- }
- }
- $_session["Verifycode"]= $code;
- return $code;
- }
- Private Function Genother ($image)
- {
- for ($i =0; $i <80; $i + +)//Generate interference pixels
- {
- $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 < $num; $i + +)//print character to image
- {
- $char _color=imagecolorallocate ($image, Rand (0,2555), Rand (0,255), Rand (0,255));
- Imagechar ($image, Max, ($this->width/$num) * $i, Rand (0,5), $code [$i], $char _color);
- }
- Header ("Content-type:image/png");
- Imagepng ($image);//output image to browser
- Imagedestroy ($image);//Release resources
- }
- }
- $image = new Lib_image (25, 65);
- $image->verycode ();
- ?>
Copy Code
- 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 is not 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 is not written! ');
- }
- }
- Public Function __destruct ()
- {
- if ($this->filehander!=null) {
- Fclose ($this->filehander);
- }
- }
- }
- $log = new Lib_log ();
- $log->writelog (' the error debug! ');
- echo "";
- ?>
Copy Code
- Class Lib_page
- {
- Public $currentPage =0;//Current page
- Private $totalPage =0;//Total pages
- Private $totalNums = 0; Total Record Count
- Private $perNums =0;//The number of records displayed per page
- Private $type = 0;//Display Type
- 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 "Home";
- }
- Private Function Last ()
- {
- if ($this->currentpage== $this->totalpage) return false;
- Return "Totalpage} ' > last";
- }
- Private Function Next ()
- {
- $p = min ($this->currentpage+1, $this->totalpage);
- if ($p = = $this->totalpage) return false;
- Return "Next page";
- }
- Private Function prev ()
- {
- $p = max (1, $this->currentpage-1);
- if ($p ==1) return false;
- Return "Previous page";
- }
- Private Function Total ()
- {
- Return " Total {$this->totalpage} page | {$this->totalnums} records | Current {$this->currentpage} page ";
- }
- Private Function page ()
- {
- $show = "";
- for ($i =1; $i <= $this->totalpage; $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 ();
- ?>
Copy Code
- Class Lib_tree
- {
- Private $items = Array ();
- Private $icon = Array (
- ' ├ ',
- ' ├ ',
- ' ├ ',
- ' ├ ',
- ' ├ ',
- ' └ ',
- );
- Private $field = array (' id ', ' name ');
- Public $ret = '
- $this->ret.= '
- $this->ret.= '
- $this->ret.= '
- $this->show ($child [' id ']);
class name |
Operation |
';
- 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->icon[$child [' Level ']. $child [' name ']. ' | ';
Remove Add modifications | ';
';
- }
- }
- }
- $items = Array (
- Array (' ID ' =>1, ' name ' = ' Hubei ', ' pid ' =>0, ' Level ' =>0),
- Array (' ID ' =>2, ' name ' = ' Wuhan ', ' pid ' =>1, ' Level ' =>1),
- Array (' ID ' =>3, ' name ' = ' Xiaogan ', ' pid ' =>1, ' Level ' =>1),
- Array (' ID ' =>4, ' name ' = ' Guangdong ', ' pid ' =>0, ' Level ' =>0),
- Array (' ID ' =>5, ' name ' = ' Guangzhou ', ' pid ' =>4, ' Level ' =>1),
- Array (' ID ' =>6, ' name ' = ' Shenzhen ', ' pid ' =>4, ' Level ' =>1),
- Array (' ID ' =>7, ' name ' = ' Dongguan ', ' pid ' =>4, ' Level ' =>1),
- Array (' ID ' =>8, ' name ' = ' Yichang ', ' pid ' =>1, ' Level ' =>1),
- Array (' ID ' =>9, ' name ' = ' Yunmeng ', ' pid ' =>3, ' Level ' =>2),
- Array (' ID ' =>10, ' name ' = ' Nanshan ', ' pid ' =>6, ' Level ' =>2),
- Array (' ID ' =>11, ' name ' = ' bao security ', ' pid ' =>6, ' Level ' =>2),
- Array (' ID ' =>12, ' name ' = = ' inverted shop ', ' pid ' =>9, ' Level ' =>3),
- Array (' ID ' =>13, ' name ' = ' Mrs Law Brigade ', ' pid ' =>12, ' Level ' =>4),
- Array (' ID ' =>14, ' name ' = ' + ' under Fan Cun ', ' pid ' =>13, ' Level ' =>5),
- );
- $tree = new Lib_tree ($items);
- $tree->show (0);
- Echo $tree->ret;
- ?> Copy Code
-
|