@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | * You should have received a copy of the GNU General Public License |
23 | 23 | * along with this program; if not, write to the Free Software |
24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | - */ |
|
25 | + */ |
|
26 | 26 | |
27 | 27 | /** |
28 | 28 | * PDF library to generate PDF document using the library DOMPDF |
@@ -45,8 +45,8 @@ discard block |
||
45 | 45 | * Create PDF library instance |
46 | 46 | */ |
47 | 47 | public function __construct(){ |
48 | - $this->logger =& class_loader('Log', 'classes'); |
|
49 | - $this->logger->setLogger('Library::PDF'); |
|
48 | + $this->logger =& class_loader('Log', 'classes'); |
|
49 | + $this->logger->setLogger('Library::PDF'); |
|
50 | 50 | |
51 | 51 | require_once VENDOR_PATH.'dompdf/dompdf_config.inc.php'; |
52 | 52 | $this->dompdf = new Dompdf(); |
@@ -76,10 +76,10 @@ discard block |
||
76 | 76 | } |
77 | 77 | |
78 | 78 | /** |
79 | - * Return the instance of Dompdf |
|
80 | - * |
|
81 | - * @return object the dompdf instance |
|
82 | - */ |
|
79 | + * Return the instance of Dompdf |
|
80 | + * |
|
81 | + * @return object the dompdf instance |
|
82 | + */ |
|
83 | 83 | public function getDompdf(){ |
84 | 84 | return $this->dompdf; |
85 | 85 | } |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | /** |
28 | 28 | * PDF library to generate PDF document using the library DOMPDF |
29 | 29 | */ |
30 | - class PDF{ |
|
30 | + class PDF { |
|
31 | 31 | |
32 | 32 | /** |
33 | 33 | * The dompdf instance |
@@ -44,11 +44,11 @@ discard block |
||
44 | 44 | /** |
45 | 45 | * Create PDF library instance |
46 | 46 | */ |
47 | - public function __construct(){ |
|
48 | - $this->logger =& class_loader('Log', 'classes'); |
|
47 | + public function __construct() { |
|
48 | + $this->logger = & class_loader('Log', 'classes'); |
|
49 | 49 | $this->logger->setLogger('Library::PDF'); |
50 | 50 | |
51 | - require_once VENDOR_PATH.'dompdf/dompdf_config.inc.php'; |
|
51 | + require_once VENDOR_PATH . 'dompdf/dompdf_config.inc.php'; |
|
52 | 52 | $this->dompdf = new Dompdf(); |
53 | 53 | } |
54 | 54 | |
@@ -62,15 +62,15 @@ discard block |
||
62 | 62 | * @return string|void if $stream is true send PDF document to browser for download, else return the generated PDF |
63 | 63 | * content like to join in Email attachment of for other purpose use. |
64 | 64 | */ |
65 | - public function generate($html, $filename = 'output.pdf', $stream = true, $paper = 'A4', $orientation = 'portrait'){ |
|
66 | - $this->logger->info('Generating of PDF document: filename [' .$filename. '], stream [' .($stream ? 'TRUE':'FALSE'). '], paper [' .$paper. '], orientation [' .$orientation. ']'); |
|
65 | + public function generate($html, $filename = 'output.pdf', $stream = true, $paper = 'A4', $orientation = 'portrait') { |
|
66 | + $this->logger->info('Generating of PDF document: filename [' . $filename . '], stream [' . ($stream ? 'TRUE' : 'FALSE') . '], paper [' . $paper . '], orientation [' . $orientation . ']'); |
|
67 | 67 | $this->dompdf->load_html($html); |
68 | 68 | $this->dompdf->set_paper($paper, $orientation); |
69 | 69 | $this->dompdf->render(); |
70 | - if($stream){ |
|
70 | + if ($stream) { |
|
71 | 71 | $this->dompdf->stream($filename); |
72 | 72 | } |
73 | - else{ |
|
73 | + else { |
|
74 | 74 | return $this->dompdf->output(); |
75 | 75 | } |
76 | 76 | } |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | * |
81 | 81 | * @return object the dompdf instance |
82 | 82 | */ |
83 | - public function getDompdf(){ |
|
83 | + public function getDompdf() { |
|
84 | 84 | return $this->dompdf; |
85 | 85 | } |
86 | 86 |
@@ -69,8 +69,7 @@ |
||
69 | 69 | $this->dompdf->render(); |
70 | 70 | if($stream){ |
71 | 71 | $this->dompdf->stream($filename); |
72 | - } |
|
73 | - else{ |
|
72 | + } else{ |
|
74 | 73 | return $this->dompdf->output(); |
75 | 74 | } |
76 | 75 | } |
@@ -74,9 +74,9 @@ discard block |
||
74 | 74 | protected $_uid; |
75 | 75 | |
76 | 76 | /** |
77 | - * The logger instance |
|
78 | - * @var Log |
|
79 | - */ |
|
77 | + * The logger instance |
|
78 | + * @var Log |
|
79 | + */ |
|
80 | 80 | private $logger; |
81 | 81 | |
82 | 82 | /** |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | public function __construct() |
88 | 88 | { |
89 | 89 | $this->logger =& class_loader('Log', 'classes'); |
90 | - $this->logger->setLogger('Library::Email'); |
|
90 | + $this->logger->setLogger('Library::Email'); |
|
91 | 91 | $this->reset(); |
92 | 92 | } |
93 | 93 | |
@@ -112,13 +112,13 @@ discard block |
||
112 | 112 | } |
113 | 113 | |
114 | 114 | /** |
115 | - * setFrom |
|
116 | - * |
|
117 | - * @param string $email The email to send as from. |
|
118 | - * @param string $name The name to send as from. |
|
119 | - * |
|
120 | - * @return self |
|
121 | - */ |
|
115 | + * setFrom |
|
116 | + * |
|
117 | + * @param string $email The email to send as from. |
|
118 | + * @param string $name The name to send as from. |
|
119 | + * |
|
120 | + * @return self |
|
121 | + */ |
|
122 | 122 | public function setFrom($email, $name = null) |
123 | 123 | { |
124 | 124 | $this->addMailHeader('From', (string) $email, (string) $name); |
@@ -140,11 +140,11 @@ discard block |
||
140 | 140 | } |
141 | 141 | |
142 | 142 | /** |
143 | - * Set destination using array |
|
144 | - * @params array $emails the list of recipient. This is an associative array name => email |
|
145 | - * @example array('John Doe' => '[email protected]') |
|
146 | - * @return Object the current instance |
|
147 | - */ |
|
143 | + * Set destination using array |
|
144 | + * @params array $emails the list of recipient. This is an associative array name => email |
|
145 | + * @example array('John Doe' => '[email protected]') |
|
146 | + * @return Object the current instance |
|
147 | + */ |
|
148 | 148 | public function setTos(array $emails) |
149 | 149 | { |
150 | 150 | foreach ($emails as $name => $email) { |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | */ |
87 | 87 | public function __construct() |
88 | 88 | { |
89 | - $this->logger =& class_loader('Log', 'classes'); |
|
89 | + $this->logger = & class_loader('Log', 'classes'); |
|
90 | 90 | $this->logger->setLogger('Library::Email'); |
91 | 91 | $this->reset(); |
92 | 92 | } |
@@ -148,10 +148,10 @@ discard block |
||
148 | 148 | public function setTos(array $emails) |
149 | 149 | { |
150 | 150 | foreach ($emails as $name => $email) { |
151 | - if(is_numeric($name)){ |
|
151 | + if (is_numeric($name)) { |
|
152 | 152 | $this->setTo($email); |
153 | 153 | } |
154 | - else{ |
|
154 | + else { |
|
155 | 155 | $this->setTo($email, $name); |
156 | 156 | } |
157 | 157 | } |
@@ -281,8 +281,8 @@ discard block |
||
281 | 281 | */ |
282 | 282 | public function addAttachment($path, $filename = null, $data = null) |
283 | 283 | { |
284 | - if(! file_exists($path)){ |
|
285 | - show_error('The file [' .$path. '] does not exists.'); |
|
284 | + if (!file_exists($path)) { |
|
285 | + show_error('The file [' . $path . '] does not exists.'); |
|
286 | 286 | } |
287 | 287 | $filename = empty($filename) ? basename($path) : $filename; |
288 | 288 | $filename = $this->encodeUtf8($this->filterOther((string) $filename)); |
@@ -304,8 +304,8 @@ discard block |
||
304 | 304 | */ |
305 | 305 | public function getAttachmentData($path) |
306 | 306 | { |
307 | - if(! file_exists($path)){ |
|
308 | - show_error('The file [' .$path. '] does not exists.'); |
|
307 | + if (!file_exists($path)) { |
|
308 | + show_error('The file [' . $path . '] does not exists.'); |
|
309 | 309 | } |
310 | 310 | $filesize = filesize($path); |
311 | 311 | $handle = fopen($path, "r"); |
@@ -150,8 +150,7 @@ |
||
150 | 150 | foreach ($emails as $name => $email) { |
151 | 151 | if(is_numeric($name)){ |
152 | 152 | $this->setTo($email); |
153 | - } |
|
154 | - else{ |
|
153 | + } else{ |
|
155 | 154 | $this->setTo($email, $name); |
156 | 155 | } |
157 | 156 | } |
@@ -22,7 +22,7 @@ |
||
22 | 22 | * You should have received a copy of the GNU General Public License |
23 | 23 | * along with this program; if not, write to the Free Software |
24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | - */ |
|
25 | + */ |
|
26 | 26 | |
27 | 27 | class Cookie{ |
28 | 28 |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | 25 | */ |
26 | 26 | |
27 | - class Cookie{ |
|
27 | + class Cookie { |
|
28 | 28 | |
29 | 29 | /** |
30 | 30 | * The logger instance |
@@ -36,9 +36,9 @@ discard block |
||
36 | 36 | * The signleton of the logger |
37 | 37 | * @return Object the Log instance |
38 | 38 | */ |
39 | - private static function getLogger(){ |
|
40 | - if(static::$logger == null){ |
|
41 | - static::$logger[0] =& class_loader('Log', 'classes'); |
|
39 | + private static function getLogger() { |
|
40 | + if (static::$logger == null) { |
|
41 | + static::$logger[0] = & class_loader('Log', 'classes'); |
|
42 | 42 | static::$logger[0]->setLogger('Library::Cookie'); |
43 | 43 | } |
44 | 44 | return static::$logger[0]; |
@@ -50,9 +50,9 @@ discard block |
||
50 | 50 | * @param mixed $default the default value to use if can not find the cokkie item in the list |
51 | 51 | * @return mixed the cookie value if exist or the default value |
52 | 52 | */ |
53 | - public static function get($item, $default = null){ |
|
53 | + public static function get($item, $default = null) { |
|
54 | 54 | $logger = static::getLogger(); |
55 | - if(array_key_exists($item, $_COOKIE)){ |
|
55 | + if (array_key_exists($item, $_COOKIE)) { |
|
56 | 56 | return $_COOKIE[$item]; |
57 | 57 | } |
58 | 58 | $logger->warning('Cannot find cookie item [' . $item . '], using the default value [' . $default . ']'); |
@@ -69,14 +69,14 @@ discard block |
||
69 | 69 | * @param boolean $secure if this cookie will be available on secure connection or not |
70 | 70 | * @param boolean $httponly if this cookie will be available under HTTP protocol. |
71 | 71 | */ |
72 | - public static function set($name, $value = '', $expire = 0, $path = '/', $domain = '', $secure = false, $httponly = false){ |
|
73 | - if(headers_sent()){ |
|
72 | + public static function set($name, $value = '', $expire = 0, $path = '/', $domain = '', $secure = false, $httponly = false) { |
|
73 | + if (headers_sent()) { |
|
74 | 74 | show_error('There exists a cookie that we wanted to create that we couldn\'t |
75 | 75 | because headers was already sent. Make sure to do this first |
76 | 76 | before outputing anything.'); |
77 | 77 | } |
78 | 78 | $timestamp = $expire; |
79 | - if($expire){ |
|
79 | + if ($expire) { |
|
80 | 80 | $timestamp = time() + $expire; |
81 | 81 | } |
82 | 82 | setcookie($name, $value, $timestamp, $path, $domain, $secure, $httponly); |
@@ -87,15 +87,15 @@ discard block |
||
87 | 87 | * @param string $item the cookie item name to be cleared |
88 | 88 | * @return boolean true if the item exists and is deleted successfully otherwise will return false. |
89 | 89 | */ |
90 | - public static function delete($item){ |
|
90 | + public static function delete($item) { |
|
91 | 91 | $logger = static::getLogger(); |
92 | - if(array_key_exists($item, $_COOKIE)){ |
|
93 | - $logger->info('Delete cookie item ['.$item.']'); |
|
92 | + if (array_key_exists($item, $_COOKIE)) { |
|
93 | + $logger->info('Delete cookie item [' . $item . ']'); |
|
94 | 94 | unset($_COOKIE[$item]); |
95 | 95 | return true; |
96 | 96 | } |
97 | - else{ |
|
98 | - $logger->warning('Cookie item ['.$item.'] to be deleted does not exists'); |
|
97 | + else { |
|
98 | + $logger->warning('Cookie item [' . $item . '] to be deleted does not exists'); |
|
99 | 99 | return false; |
100 | 100 | } |
101 | 101 | } |
@@ -105,7 +105,7 @@ discard block |
||
105 | 105 | * @param string $item the cookie item name |
106 | 106 | * @return boolean true if the cookie item is set, false or not |
107 | 107 | */ |
108 | - public static function exists($item){ |
|
108 | + public static function exists($item) { |
|
109 | 109 | return array_key_exists($item, $_COOKIE); |
110 | 110 | } |
111 | 111 |
@@ -93,8 +93,7 @@ |
||
93 | 93 | $logger->info('Delete cookie item ['.$item.']'); |
94 | 94 | unset($_COOKIE[$item]); |
95 | 95 | return true; |
96 | - } |
|
97 | - else{ |
|
96 | + } else{ |
|
98 | 97 | $logger->warning('Cookie item ['.$item.'] to be deleted does not exists'); |
99 | 98 | return false; |
100 | 99 | } |
@@ -22,7 +22,7 @@ |
||
22 | 22 | * You should have received a copy of the GNU General Public License |
23 | 23 | * along with this program; if not, write to the Free Software |
24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | - */ |
|
25 | + */ |
|
26 | 26 | |
27 | 27 | /** |
28 | 28 | * @file Assets.php |
@@ -38,7 +38,7 @@ discard block |
||
38 | 38 | * @since 1.0.0 |
39 | 39 | * @filesource |
40 | 40 | */ |
41 | - class Assets{ |
|
41 | + class Assets { |
|
42 | 42 | |
43 | 43 | /** |
44 | 44 | * The logger instance |
@@ -50,10 +50,10 @@ discard block |
||
50 | 50 | * The signleton of the logger |
51 | 51 | * @return Object the Log instance |
52 | 52 | */ |
53 | - private static function getLogger(){ |
|
54 | - if(static::$logger == null){ |
|
53 | + private static function getLogger() { |
|
54 | + if (static::$logger == null) { |
|
55 | 55 | //can't assign reference to static variable |
56 | - static::$logger[0] =& class_loader('Log', 'classes'); |
|
56 | + static::$logger[0] = & class_loader('Log', 'classes'); |
|
57 | 57 | static::$logger[0]->setLogger('Library::Assets'); |
58 | 58 | } |
59 | 59 | return static::$logger[0]; |
@@ -72,13 +72,13 @@ discard block |
||
72 | 72 | * @param $asset the name of the assets file path with the extension. |
73 | 73 | * @return string|null the absolute path of the assets file, if it exists otherwise returns null if the file does not exist. |
74 | 74 | */ |
75 | - public static function path($asset){ |
|
75 | + public static function path($asset) { |
|
76 | 76 | $logger = static::getLogger(); |
77 | 77 | $path = ASSETS_PATH . $asset; |
78 | 78 | |
79 | 79 | $logger->debug('Including the Assets file [' . $path . ']'); |
80 | 80 | //Check if the file exists |
81 | - if(file_exists($path)){ |
|
81 | + if (file_exists($path)) { |
|
82 | 82 | $logger->info('Assets file [' . $path . '] included successfully'); |
83 | 83 | return Url::base_url($path); |
84 | 84 | } |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | * @param $path the name of the css file without the extension. |
99 | 99 | * @return string|null the absolute path of the css file, if it exists otherwise returns null if the file does not exist. |
100 | 100 | */ |
101 | - public static function css($path){ |
|
101 | + public static function css($path) { |
|
102 | 102 | $logger = static::getLogger(); |
103 | 103 | /* |
104 | 104 | * if the file name contains the ".css" extension, replace it with |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | |
110 | 110 | $logger->debug('Including the Assets file [' . $path . '] for CSS'); |
111 | 111 | //Check if the file exists |
112 | - if(file_exists($path)){ |
|
112 | + if (file_exists($path)) { |
|
113 | 113 | $logger->info('Assets file [' . $path . '] for CSS included successfully'); |
114 | 114 | return Url::base_url($path); |
115 | 115 | } |
@@ -129,12 +129,12 @@ discard block |
||
129 | 129 | * @param $path the name of the javascript file without the extension. |
130 | 130 | * @return string|null the absolute path of the javascript file, if it exists otherwise returns null if the file does not exist. |
131 | 131 | */ |
132 | - public static function js($path){ |
|
132 | + public static function js($path) { |
|
133 | 133 | $logger = static::getLogger(); |
134 | 134 | $path = str_ireplace('.js', '', $path); |
135 | 135 | $path = ASSETS_PATH . 'js/' . $path . '.js'; |
136 | 136 | $logger->debug('Including the Assets file [' . $path . '] for javascript'); |
137 | - if(file_exists($path)){ |
|
137 | + if (file_exists($path)) { |
|
138 | 138 | $logger->info('Assets file [' . $path . '] for Javascript included successfully'); |
139 | 139 | return Url::base_url($path); |
140 | 140 | } |
@@ -154,11 +154,11 @@ discard block |
||
154 | 154 | * @param $path the name of the image file with the extension. |
155 | 155 | * @return string|null the absolute path of the image file, if it exists otherwise returns null if the file does not exist. |
156 | 156 | */ |
157 | - public static function img($path){ |
|
157 | + public static function img($path) { |
|
158 | 158 | $logger = static::getLogger(); |
159 | 159 | $path = ASSETS_PATH . 'images/' . $path; |
160 | 160 | $logger->debug('Including the Assets file [' . $path . '] for image'); |
161 | - if(file_exists($path)){ |
|
161 | + if (file_exists($path)) { |
|
162 | 162 | $logger->info('Assets file [' . $path . '] for image included successfully'); |
163 | 163 | return Url::base_url($path); |
164 | 164 | } |
@@ -1,816 +1,816 @@ |
||
1 | 1 | <?php |
2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
3 | - /** |
|
4 | - * TNH Framework |
|
5 | - * |
|
6 | - * A simple PHP framework using HMVC architecture |
|
7 | - * |
|
8 | - * This content is released under the GNU GPL License (GPL) |
|
9 | - * |
|
10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
11 | - * |
|
12 | - * This program is free software; you can redistribute it and/or |
|
13 | - * modify it under the terms of the GNU General Public License |
|
14 | - * as published by the Free Software Foundation; either version 3 |
|
15 | - * of the License, or (at your option) any later version. |
|
16 | - * |
|
17 | - * This program is distributed in the hope that it will be useful, |
|
18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | - * GNU General Public License for more details. |
|
21 | - * |
|
22 | - * You should have received a copy of the GNU General Public License |
|
23 | - * along with this program; if not, write to the Free Software |
|
24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | - */ |
|
26 | - |
|
27 | - |
|
28 | - |
|
29 | - /** |
|
30 | - * Upload |
|
31 | - * |
|
32 | - * A complete class to upload files with php 5 or higher, but the best: very simple to use. |
|
33 | - * |
|
34 | - * @author Olaf Erlandsen <[email protected]> |
|
35 | - * @author http://www.webdevfreelance.com/ |
|
36 | - * |
|
37 | - * @package FileUpload |
|
38 | - * @version 1.5 |
|
39 | - */ |
|
40 | - class Upload{ |
|
41 | - |
|
42 | - /** |
|
43 | - * Version |
|
44 | - * |
|
45 | - * @since 1.5 |
|
46 | - * @version 1.0 |
|
47 | - */ |
|
48 | - const VERSION = '1.5'; |
|
49 | - |
|
50 | - /** |
|
51 | - * Upload function name |
|
52 | - * Remember: |
|
53 | - * Default function: move_uploaded_file |
|
54 | - * Native options: |
|
55 | - * - move_uploaded_file (Default and best option) |
|
56 | - * - copy |
|
57 | - * |
|
58 | - * @since 1.0 |
|
59 | - * @version 1.0 |
|
60 | - * @var mixex |
|
61 | - */ |
|
62 | - private $upload_function = 'move_uploaded_file'; |
|
63 | - |
|
64 | - /** |
|
65 | - * Array with the information obtained from the |
|
66 | - * variable $_FILES or $HTTP_POST_FILES. |
|
67 | - * |
|
68 | - * @since 1.0 |
|
69 | - * @version 1.0 |
|
70 | - * @var array |
|
71 | - */ |
|
72 | - private $file_array = array(); |
|
73 | - |
|
74 | - /** |
|
75 | - * If the file you are trying to upload already exists it will |
|
76 | - * be overwritten if you set the variable to true. |
|
77 | - * |
|
78 | - * @since 1.0 |
|
79 | - * @version 1.0 |
|
80 | - * @var boolean |
|
81 | - */ |
|
82 | - private $overwrite_file = false; |
|
83 | - |
|
84 | - /** |
|
85 | - * Input element |
|
86 | - * Example: |
|
87 | - * <input type="file" name="file" /> |
|
88 | - * Result: |
|
89 | - * FileUpload::$input = file |
|
90 | - * |
|
91 | - * @since 1.0 |
|
92 | - * @version 1.0 |
|
93 | - * @var string |
|
94 | - */ |
|
95 | - private $input; |
|
96 | - |
|
97 | - /** |
|
98 | - * Path output |
|
99 | - * |
|
100 | - * @since 1.0 |
|
101 | - * @version 1.0 |
|
102 | - * @var string |
|
103 | - */ |
|
104 | - private $destination_directory; |
|
105 | - |
|
106 | - /** |
|
107 | - * Output filename |
|
108 | - * |
|
109 | - * @since 1.0 |
|
110 | - * @version 1.0 |
|
111 | - * @var string |
|
112 | - */ |
|
113 | - private $filename; |
|
114 | - |
|
115 | - /** |
|
116 | - * Max file size |
|
117 | - * |
|
118 | - * @since 1.0 |
|
119 | - * @version 1.0 |
|
120 | - * @var float |
|
121 | - */ |
|
122 | - private $max_file_size= 0.0; |
|
123 | - |
|
124 | - /** |
|
125 | - * List of allowed mime types |
|
126 | - * |
|
127 | - * @since 1.0 |
|
128 | - * @version 1.0 |
|
129 | - * @var array |
|
130 | - */ |
|
131 | - private $allowed_mime_types = array(); |
|
132 | - |
|
133 | - /** |
|
134 | - * Callbacks |
|
135 | - * |
|
136 | - * @since 1.0 |
|
137 | - * @version 1.0 |
|
138 | - * @var array |
|
139 | - */ |
|
140 | - private $callbacks = array('before' => null, 'after' => null); |
|
141 | - |
|
142 | - /** |
|
143 | - * File object |
|
144 | - * |
|
145 | - * @since 1.0 |
|
146 | - * @version 1.0 |
|
147 | - * @var object |
|
148 | - */ |
|
149 | - private $file; |
|
150 | - |
|
151 | - /** |
|
152 | - * Helping mime types |
|
153 | - * |
|
154 | - * @since 1.0 |
|
155 | - * @version 1.0 |
|
156 | - * @var array |
|
157 | - */ |
|
158 | - private $mime_helping = array( |
|
159 | - 'text' => array('text/plain',), |
|
160 | - 'image' => array( |
|
161 | - 'image/jpeg', |
|
162 | - 'image/jpg', |
|
163 | - 'image/pjpeg', |
|
164 | - 'image/png', |
|
165 | - 'image/gif', |
|
166 | - ), |
|
167 | - 'document' => array( |
|
168 | - 'application/pdf', |
|
169 | - 'application/msword', |
|
170 | - 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
|
171 | - 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
|
172 | - 'application/vnd.ms-powerpoint', |
|
173 | - 'application/vnd.ms-excel', |
|
174 | - 'application/vnd.oasis.opendocument.spreadsheet', |
|
175 | - 'application/vnd.oasis.opendocument.presentation', |
|
176 | - ), |
|
177 | - 'video' => array( |
|
178 | - 'video/3gpp', |
|
179 | - 'video/3gpp', |
|
180 | - 'video/x-msvideo', |
|
181 | - 'video/avi', |
|
182 | - 'video/mpeg4', |
|
183 | - 'video/mp4', |
|
184 | - 'video/mpeg', |
|
185 | - 'video/mpg', |
|
186 | - 'video/quicktime', |
|
187 | - 'video/x-sgi-movie', |
|
188 | - 'video/x-ms-wmv', |
|
189 | - 'video/x-flv', |
|
190 | - ), |
|
191 | - ); |
|
192 | - |
|
193 | - /** |
|
194 | - * The upload error message |
|
195 | - * @var array |
|
196 | - */ |
|
197 | - public $error_messages = array(); |
|
198 | - |
|
199 | - /** |
|
200 | - * The upload error message |
|
201 | - * @var string |
|
202 | - */ |
|
203 | - protected $error = null; |
|
204 | - |
|
205 | - /** |
|
206 | - * The logger instance |
|
207 | - * @var Log |
|
208 | - */ |
|
209 | - private $logger; |
|
210 | - |
|
211 | - |
|
212 | - /** |
|
213 | - * Construct |
|
214 | - * |
|
215 | - * @since 0.1 |
|
216 | - * @version 1.0.1 |
|
217 | - * @return object |
|
218 | - * @method object __construct |
|
219 | - */ |
|
220 | - public function __construct(){ |
|
221 | - $this->logger =& class_loader('Log', 'classes'); |
|
222 | - $this->logger->setLogger('Library::Upload'); |
|
223 | - |
|
224 | - Loader::lang('file_upload'); |
|
225 | - $obj =& get_instance(); |
|
226 | - |
|
227 | - $this->error_messages = array( |
|
228 | - 'upload_err_ini_size' => $obj->lang->get('fu_upload_err_ini_size'), |
|
229 | - 'upload_err_form_size' => $obj->lang->get('fu_upload_err_form_size'), |
|
230 | - 'upload_err_partial' => $obj->lang->get('fu_upload_err_partial'), |
|
231 | - 'upload_err_no_file' => $obj->lang->get('fu_upload_err_no_file'), |
|
232 | - 'upload_err_no_tmp_dir' => $obj->lang->get('fu_upload_err_no_tmp_dir'), |
|
233 | - 'upload_err_cant_write' => $obj->lang->get('fu_upload_err_cant_write'), |
|
234 | - 'upload_err_extension' => $obj->lang->get('fu_upload_err_extension'), |
|
235 | - 'accept_file_types' => $obj->lang->get('fu_accept_file_types'), |
|
236 | - 'file_uploads' => $obj->lang->get('fu_file_uploads_disabled'), |
|
237 | - 'max_file_size' => $obj->lang->get('fu_max_file_size'), |
|
238 | - 'overwritten_not_allowed' => $obj->lang->get('fu_overwritten_not_allowed'), |
|
239 | - ); |
|
240 | - |
|
241 | - $this->file = array( |
|
242 | - 'status' => false, // True: success upload |
|
243 | - 'mime' => '', // Empty string |
|
244 | - 'filename' => '', // Empty string |
|
245 | - 'original' => '', // Empty string |
|
246 | - 'size' => 0, // 0 Bytes |
|
247 | - 'sizeFormated' => '0B', // 0 Bytes |
|
248 | - 'destination' => './', // Default: ./ |
|
249 | - 'allowed_mime_types' => array(), // Allowed mime types |
|
250 | - 'error' => null, // File error |
|
251 | - ); |
|
252 | - |
|
253 | - // Change dir to current dir |
|
254 | - $this->destination_directory = dirname(__FILE__) . DIRECTORY_SEPARATOR; |
|
255 | - |
|
256 | - // Set file array |
|
257 | - if (isset($_FILES) && is_array($_FILES)) { |
|
258 | - $this->file_array = $_FILES; |
|
259 | - } elseif (isset($HTTP_POST_FILES) && is_array($HTTP_POST_FILES)) { |
|
260 | - $this->file_array = $HTTP_POST_FILES; |
|
261 | - } |
|
262 | - $this->logger->info('The upload file information are : ' .stringfy_vars($this->file_array)); |
|
263 | - } |
|
264 | - /** |
|
265 | - * Set input. |
|
266 | - * If you have $_FILES["file"], you must use the key "file" |
|
267 | - * Example: |
|
268 | - * $object->setInput("file"); |
|
269 | - * |
|
270 | - * @since 1.0 |
|
271 | - * @version 1.0 |
|
272 | - * @param string $input |
|
273 | - * @return boolean |
|
274 | - * @method boolean setInput |
|
275 | - */ |
|
276 | - public function setInput($input) |
|
277 | - { |
|
278 | - if (!empty($input) && (is_string($input) || is_numeric($input) )) { |
|
279 | - $this->input = $input; |
|
280 | - } |
|
281 | - return $this; |
|
282 | - } |
|
283 | - /** |
|
284 | - * Set new filename |
|
285 | - * Example: |
|
286 | - * FileUpload::setFilename("new file.txt") |
|
287 | - * Remember: |
|
288 | - * Use %s to retrive file extension |
|
289 | - * |
|
290 | - * @since 1.0 |
|
291 | - * @version 1.0 |
|
292 | - * @param string $filename |
|
293 | - * @return boolean |
|
294 | - * @method boolean setFilename |
|
295 | - */ |
|
296 | - public function setFilename($filename) |
|
297 | - { |
|
298 | - if ($this->isFilename($filename)) { |
|
299 | - $this->filename = $filename; |
|
300 | - } |
|
301 | - return $this; |
|
302 | - } |
|
303 | - /** |
|
304 | - * Set automatic filename |
|
305 | - * |
|
306 | - * @since 1.0 |
|
307 | - * @version 1.5 |
|
308 | - * @param string $extension |
|
309 | - * @return boolean |
|
310 | - * @method boolean setAutoFilename |
|
311 | - */ |
|
312 | - public function setAutoFilename() |
|
313 | - { |
|
314 | - $this->filename = sha1(mt_rand(1, 9999).uniqid()); |
|
315 | - $this->filename .= time(); |
|
316 | - return $this; |
|
317 | - } |
|
318 | - /** |
|
319 | - * Set file size limit |
|
320 | - * |
|
321 | - * @since 1.0 |
|
322 | - * @version 1.0 |
|
323 | - * @param integer $file_size |
|
324 | - * @return boolean |
|
325 | - * @method boolean setMaxFileSize |
|
326 | - */ |
|
327 | - public function setMaxFileSize($file_size) |
|
328 | - { |
|
329 | - $file_size = $this->sizeInBytes($file_size); |
|
330 | - if (is_numeric($file_size) && $file_size > -1) { |
|
331 | - // Get php config |
|
332 | - $php_size = $this->sizeInBytes(ini_get('upload_max_filesize')); |
|
333 | - // Calculate difference |
|
334 | - if ($php_size < $file_size) { |
|
335 | - $this->logger->warning('The upload max file size you set [' .$file_size. '] is greather than the PHP configuration for upload max file size [' .$php_size. ']'); |
|
336 | - } |
|
337 | - $this->max_file_size = $file_size; |
|
338 | - } |
|
339 | - return $this; |
|
340 | - } |
|
341 | - /** |
|
342 | - * Set array mime types |
|
343 | - * |
|
344 | - * @since 1.0 |
|
345 | - * @version 1.0 |
|
346 | - * @param array $mimes |
|
347 | - * @return boolean |
|
348 | - * @method boolean setAllowedMimeTypes |
|
349 | - */ |
|
350 | - public function setAllowedMimeTypes(array $mimes) |
|
351 | - { |
|
352 | - if (count($mimes) > 0) { |
|
353 | - array_map(array($this , 'setAllowMimeType'), $mimes); |
|
354 | - } |
|
355 | - return $this; |
|
356 | - } |
|
357 | - /** |
|
358 | - * Set input callback |
|
359 | - * |
|
360 | - * @since 1.0 |
|
361 | - * @version 1.0 |
|
362 | - * @param mixed $callback |
|
363 | - * @return boolean |
|
364 | - * @method boolean setCallbackInput |
|
365 | - */ |
|
366 | - public function setCallbackInput($callback) |
|
367 | - { |
|
368 | - if (is_callable($callback, false)) { |
|
369 | - $this->callbacks['input'] = $callback; |
|
370 | - } |
|
371 | - return $this; |
|
372 | - } |
|
373 | - /** |
|
374 | - * Set output callback |
|
375 | - * |
|
376 | - * @since 1.0 |
|
377 | - * @version 1.0 |
|
378 | - * @param mixed $callback |
|
379 | - * @return boolean |
|
380 | - * @method boolean setCallbackOutput |
|
381 | - */ |
|
382 | - public function setCallbackOutput($callback) |
|
383 | - { |
|
384 | - if (is_callable($callback, false)) { |
|
385 | - $this->callbacks['output'] = $callback; |
|
386 | - } |
|
387 | - return $this; |
|
388 | - } |
|
389 | - /** |
|
390 | - * Append a mime type to allowed mime types |
|
391 | - * |
|
392 | - * @since 1.0 |
|
393 | - * @version 1.0.1 |
|
394 | - * @param string $mime |
|
395 | - * @return boolean |
|
396 | - * @method boolean setAllowMimeType |
|
397 | - */ |
|
398 | - public function setAllowMimeType($mime) |
|
399 | - { |
|
400 | - if (!empty($mime) && is_string($mime)) { |
|
401 | - $this->allowed_mime_types[] = strtolower($mime); |
|
402 | - $this->file['allowed_mime_types'][] = strtolower($mime); |
|
403 | - } |
|
404 | - return $this; |
|
405 | - } |
|
406 | - /** |
|
407 | - * Set allowed mime types from mime helping |
|
408 | - * |
|
409 | - * @since 1.0.1 |
|
410 | - * @version 1.0.1 |
|
411 | - * @return boolean |
|
412 | - * @method boolean setMimeHelping |
|
413 | - */ |
|
414 | - public function setMimeHelping($name) |
|
415 | - { |
|
416 | - if (!empty($name) && is_string($name)) { |
|
417 | - if (array_key_exists($name, $this->mime_helping)) { |
|
418 | - return $this->setAllowedMimeTypes($this->mime_helping[ $name ]); |
|
419 | - } |
|
420 | - } |
|
421 | - return $this; |
|
422 | - } |
|
423 | - /** |
|
424 | - * Set function to upload file |
|
425 | - * Examples: |
|
426 | - * 1.- FileUpload::setUploadFunction("move_uploaded_file"); |
|
427 | - * 2.- FileUpload::setUploadFunction("copy"); |
|
428 | - * |
|
429 | - * @since 1.0 |
|
430 | - * @version 1.0 |
|
431 | - * @param string $mime |
|
432 | - * @return boolean |
|
433 | - * @method boolean setUploadFunction |
|
434 | - */ |
|
435 | - public function setUploadFunction($function) |
|
436 | - { |
|
437 | - if (!empty($function) && (is_array($function) || is_string($function) )) { |
|
438 | - if (is_callable( $function)) { |
|
439 | - $this->upload_function = $function; |
|
440 | - } |
|
441 | - } |
|
442 | - return $this; |
|
443 | - } |
|
444 | - /** |
|
445 | - * Clear allowed mime types cache |
|
446 | - * |
|
447 | - * @since 1.0 |
|
448 | - * @version 1.0 |
|
449 | - * @return boolean |
|
450 | - * @method boolean clearAllowedMimeTypes |
|
451 | - */ |
|
452 | - public function clearAllowedMimeTypes() |
|
453 | - { |
|
454 | - $this->allowed_mime_types = array(); |
|
455 | - $this->file['allowed_mime_types'] = array(); |
|
456 | - return $this; |
|
457 | - } |
|
458 | - /** |
|
459 | - * Set destination output |
|
460 | - * |
|
461 | - * @since 1.0 |
|
462 | - * @version 1.0 |
|
463 | - * @param string $destination_directory Destination path |
|
464 | - * @param boolean $create_if_not_exist |
|
465 | - * @return boolean |
|
466 | - * @method boolean setDestinationDirectory |
|
467 | - */ |
|
468 | - public function setDestinationDirectory($destination_directory, $create_if_not_exist = false) { |
|
469 | - $destination_directory = realpath($destination_directory); |
|
470 | - if (substr($destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
471 | - $destination_directory .= DIRECTORY_SEPARATOR; |
|
472 | - } |
|
473 | - |
|
474 | - if ($this->isDirpath($destination_directory)) { |
|
475 | - if ($this->dirExists($destination_directory)) { |
|
476 | - $this->destination_directory = $destination_directory; |
|
477 | - if (substr($this->destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
478 | - $this->destination_directory .= DIRECTORY_SEPARATOR; |
|
479 | - } |
|
480 | - chdir($destination_directory); |
|
481 | - } elseif ($create_if_not_exist === true) { |
|
482 | - if (mkdir($destination_directory, $this->destination_permissions, true)) { |
|
483 | - if ($this->dirExists($destination_directory)) { |
|
484 | - $this->destination_directory = $destination_directory; |
|
485 | - if (substr($this->destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
486 | - $this->destination_directory .= DIRECTORY_SEPARATOR; |
|
487 | - } |
|
488 | - chdir($destination_directory); |
|
489 | - } |
|
490 | - } |
|
491 | - else{ |
|
492 | - $this->logger->warning('Can not create the upload directory [' .$destination_directory. ']'); |
|
493 | - } |
|
494 | - } |
|
495 | - } |
|
496 | - return $this; |
|
497 | - } |
|
498 | - /** |
|
499 | - * Check file exists |
|
500 | - * |
|
501 | - * @since 1.0 |
|
502 | - * @version 1.0.1 |
|
503 | - * @param string $file_destination |
|
504 | - * @return boolean |
|
505 | - * @method boolean fileExists |
|
506 | - */ |
|
507 | - public function fileExists($file_destination) |
|
508 | - { |
|
509 | - if ($this->isFilename($file_destination)) { |
|
510 | - return (file_exists($file_destination) && is_file($file_destination)); |
|
511 | - } |
|
512 | - return false; |
|
513 | - } |
|
514 | - /** |
|
515 | - * Check dir exists |
|
516 | - * |
|
517 | - * @since 1.0 |
|
518 | - * @version 1.0.1 |
|
519 | - * @param string $path |
|
520 | - * @return boolean |
|
521 | - * @method boolean dirExists |
|
522 | - */ |
|
523 | - public function dirExists($path) |
|
524 | - { |
|
525 | - if ($this->isDirpath($path)) { |
|
526 | - return (file_exists($path) && is_dir($path)); |
|
527 | - } |
|
528 | - return false; |
|
529 | - } |
|
530 | - /** |
|
531 | - * Check valid filename |
|
532 | - * |
|
533 | - * @since 1.0 |
|
534 | - * @version 1.0.1 |
|
535 | - * @param string $filename |
|
536 | - * @return boolean |
|
537 | - * @method boolean isFilename |
|
538 | - */ |
|
539 | - public function isFilename($filename) |
|
540 | - { |
|
541 | - $filename = basename($filename); |
|
542 | - return (!empty($filename) && (is_string( $filename) || is_numeric($filename))); |
|
543 | - } |
|
544 | - /** |
|
545 | - * Validate mime type with allowed mime types, |
|
546 | - * but if allowed mime types is empty, this method return true |
|
547 | - * |
|
548 | - * @since 1.0 |
|
549 | - * @version 1.0 |
|
550 | - * @param string $mime |
|
551 | - * @return boolean |
|
552 | - * @method boolean checkMimeType |
|
553 | - */ |
|
554 | - public function checkMimeType($mime) |
|
555 | - { |
|
556 | - if (count($this->allowed_mime_types) == 0) { |
|
557 | - return true; |
|
558 | - } |
|
559 | - return in_array(strtolower($mime), $this->allowed_mime_types); |
|
560 | - } |
|
561 | - /** |
|
562 | - * Retrive status of upload |
|
563 | - * |
|
564 | - * @since 1.0 |
|
565 | - * @version 1.0 |
|
566 | - * @return boolean |
|
567 | - * @method boolean getStatus |
|
568 | - */ |
|
569 | - public function getStatus() |
|
570 | - { |
|
571 | - return $this->file['status']; |
|
572 | - } |
|
573 | - /** |
|
574 | - * Check valid path |
|
575 | - * |
|
576 | - * @since 1.0 |
|
577 | - * @version 1.0.1 |
|
578 | - * @param string $filename |
|
579 | - * @return boolean |
|
580 | - * @method boolean isDirpath |
|
581 | - */ |
|
582 | - public function isDirpath($path) |
|
583 | - { |
|
584 | - if (!empty( $path) && (is_string( $path) || is_numeric($path) )) { |
|
585 | - if (DIRECTORY_SEPARATOR == '/') { |
|
586 | - return (preg_match( '/^[^*?"<>|:]*$/' , $path) == 1 ); |
|
587 | - } else { |
|
588 | - return (preg_match( "/^[^*?\"<>|:]*$/" , substr($path,2) ) == 1); |
|
589 | - } |
|
590 | - } |
|
591 | - return false; |
|
592 | - } |
|
593 | - /** |
|
594 | - * Allow overwriting files |
|
595 | - * |
|
596 | - * @since 1.0 |
|
597 | - * @version 1.0 |
|
598 | - * @return boolean |
|
599 | - * @method boolean allowOverwriting |
|
600 | - */ |
|
601 | - public function allowOverwriting() |
|
602 | - { |
|
603 | - $this->overwrite_file = true; |
|
604 | - return $this; |
|
605 | - } |
|
606 | - /** |
|
607 | - * File info |
|
608 | - * |
|
609 | - * @since 1.0 |
|
610 | - * @version 1.0 |
|
611 | - * @return object |
|
612 | - * @method object getInfo |
|
613 | - */ |
|
614 | - public function getInfo() |
|
615 | - { |
|
616 | - return (object)$this->file; |
|
617 | - } |
|
618 | - |
|
619 | - public function isUploaded(){ |
|
620 | - return isset($this->file_array[$this->input]) |
|
621 | - && |
|
622 | - is_uploaded_file($this->file_array[$this->input]['tmp_name']); |
|
623 | - } |
|
624 | - /** |
|
625 | - * Upload file |
|
626 | - * |
|
627 | - * @since 1.0 |
|
628 | - * @version 1.0.1 |
|
629 | - * @return boolean |
|
630 | - * @method boolean save |
|
631 | - */ |
|
632 | - public function save(){ |
|
633 | - //check if file upload is allowed in the configuration |
|
634 | - if(! ini_get('file_uploads')){ |
|
635 | - $this->setError($this->error_messages['file_uploads']); |
|
636 | - return false; |
|
637 | - } |
|
638 | - if (count($this->file_array) > 0) { |
|
639 | - if (array_key_exists($this->input, $this->file_array)) { |
|
640 | - // set original filename if not have a new name |
|
641 | - if (empty($this->filename)) { |
|
642 | - $this->filename = $this->file_array[$this->input]['name']; |
|
643 | - } |
|
644 | - else{ |
|
645 | - // Replace %s for extension in filename |
|
646 | - // Before: /[\w\d]*(.[\d\w]+)$/i |
|
647 | - // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |
|
648 | - // Support unicode(utf-8) characters |
|
649 | - // Example: "русские.jpeg" is valid; "Zhōngguó.jpeg" is valid; "Tønsberg.jpeg" is valid |
|
650 | - $extension = preg_replace( |
|
651 | - '/^[\p{L}\d\s\-\_\.\(\)]*\.([\d\w]+)$/iu', |
|
652 | - '$1', |
|
653 | - $this->file_array[$this->input]['name'] |
|
654 | - ); |
|
655 | - $this->filename = $this->filename.'.'.$extension; |
|
656 | - } |
|
657 | - |
|
658 | - // set file info |
|
659 | - $this->file['mime'] = $this->file_array[$this->input]['type']; |
|
660 | - $this->file['tmp'] = $this->file_array[$this->input]['tmp_name']; |
|
661 | - $this->file['original'] = $this->file_array[$this->input]['name']; |
|
662 | - $this->file['size'] = $this->file_array[$this->input]['size']; |
|
663 | - $this->file['sizeFormated'] = $this->sizeFormat($this->file['size']); |
|
664 | - $this->file['destination'] = $this->destination_directory . $this->filename; |
|
665 | - $this->file['filename'] = $this->filename; |
|
666 | - $this->file['error'] = $this->file_array[$this->input]['error']; |
|
667 | - |
|
668 | - $this->logger->info('The upload file information to process is : ' .stringfy_vars($this->file)); |
|
669 | - |
|
670 | - //check for php upload error |
|
671 | - if(is_numeric($this->file['error']) && $this->file['error'] > 0){ |
|
672 | - $this->setError($this->getPhpUploadErrorMessageByCode($this->file['error'])); |
|
673 | - return false; |
|
674 | - } |
|
2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
3 | + /** |
|
4 | + * TNH Framework |
|
5 | + * |
|
6 | + * A simple PHP framework using HMVC architecture |
|
7 | + * |
|
8 | + * This content is released under the GNU GPL License (GPL) |
|
9 | + * |
|
10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
11 | + * |
|
12 | + * This program is free software; you can redistribute it and/or |
|
13 | + * modify it under the terms of the GNU General Public License |
|
14 | + * as published by the Free Software Foundation; either version 3 |
|
15 | + * of the License, or (at your option) any later version. |
|
16 | + * |
|
17 | + * This program is distributed in the hope that it will be useful, |
|
18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | + * GNU General Public License for more details. |
|
21 | + * |
|
22 | + * You should have received a copy of the GNU General Public License |
|
23 | + * along with this program; if not, write to the Free Software |
|
24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | + */ |
|
26 | + |
|
27 | + |
|
28 | + |
|
29 | + /** |
|
30 | + * Upload |
|
31 | + * |
|
32 | + * A complete class to upload files with php 5 or higher, but the best: very simple to use. |
|
33 | + * |
|
34 | + * @author Olaf Erlandsen <[email protected]> |
|
35 | + * @author http://www.webdevfreelance.com/ |
|
36 | + * |
|
37 | + * @package FileUpload |
|
38 | + * @version 1.5 |
|
39 | + */ |
|
40 | + class Upload{ |
|
41 | + |
|
42 | + /** |
|
43 | + * Version |
|
44 | + * |
|
45 | + * @since 1.5 |
|
46 | + * @version 1.0 |
|
47 | + */ |
|
48 | + const VERSION = '1.5'; |
|
49 | + |
|
50 | + /** |
|
51 | + * Upload function name |
|
52 | + * Remember: |
|
53 | + * Default function: move_uploaded_file |
|
54 | + * Native options: |
|
55 | + * - move_uploaded_file (Default and best option) |
|
56 | + * - copy |
|
57 | + * |
|
58 | + * @since 1.0 |
|
59 | + * @version 1.0 |
|
60 | + * @var mixex |
|
61 | + */ |
|
62 | + private $upload_function = 'move_uploaded_file'; |
|
63 | + |
|
64 | + /** |
|
65 | + * Array with the information obtained from the |
|
66 | + * variable $_FILES or $HTTP_POST_FILES. |
|
67 | + * |
|
68 | + * @since 1.0 |
|
69 | + * @version 1.0 |
|
70 | + * @var array |
|
71 | + */ |
|
72 | + private $file_array = array(); |
|
73 | + |
|
74 | + /** |
|
75 | + * If the file you are trying to upload already exists it will |
|
76 | + * be overwritten if you set the variable to true. |
|
77 | + * |
|
78 | + * @since 1.0 |
|
79 | + * @version 1.0 |
|
80 | + * @var boolean |
|
81 | + */ |
|
82 | + private $overwrite_file = false; |
|
83 | + |
|
84 | + /** |
|
85 | + * Input element |
|
86 | + * Example: |
|
87 | + * <input type="file" name="file" /> |
|
88 | + * Result: |
|
89 | + * FileUpload::$input = file |
|
90 | + * |
|
91 | + * @since 1.0 |
|
92 | + * @version 1.0 |
|
93 | + * @var string |
|
94 | + */ |
|
95 | + private $input; |
|
96 | + |
|
97 | + /** |
|
98 | + * Path output |
|
99 | + * |
|
100 | + * @since 1.0 |
|
101 | + * @version 1.0 |
|
102 | + * @var string |
|
103 | + */ |
|
104 | + private $destination_directory; |
|
105 | + |
|
106 | + /** |
|
107 | + * Output filename |
|
108 | + * |
|
109 | + * @since 1.0 |
|
110 | + * @version 1.0 |
|
111 | + * @var string |
|
112 | + */ |
|
113 | + private $filename; |
|
114 | + |
|
115 | + /** |
|
116 | + * Max file size |
|
117 | + * |
|
118 | + * @since 1.0 |
|
119 | + * @version 1.0 |
|
120 | + * @var float |
|
121 | + */ |
|
122 | + private $max_file_size= 0.0; |
|
123 | + |
|
124 | + /** |
|
125 | + * List of allowed mime types |
|
126 | + * |
|
127 | + * @since 1.0 |
|
128 | + * @version 1.0 |
|
129 | + * @var array |
|
130 | + */ |
|
131 | + private $allowed_mime_types = array(); |
|
132 | + |
|
133 | + /** |
|
134 | + * Callbacks |
|
135 | + * |
|
136 | + * @since 1.0 |
|
137 | + * @version 1.0 |
|
138 | + * @var array |
|
139 | + */ |
|
140 | + private $callbacks = array('before' => null, 'after' => null); |
|
141 | + |
|
142 | + /** |
|
143 | + * File object |
|
144 | + * |
|
145 | + * @since 1.0 |
|
146 | + * @version 1.0 |
|
147 | + * @var object |
|
148 | + */ |
|
149 | + private $file; |
|
150 | + |
|
151 | + /** |
|
152 | + * Helping mime types |
|
153 | + * |
|
154 | + * @since 1.0 |
|
155 | + * @version 1.0 |
|
156 | + * @var array |
|
157 | + */ |
|
158 | + private $mime_helping = array( |
|
159 | + 'text' => array('text/plain',), |
|
160 | + 'image' => array( |
|
161 | + 'image/jpeg', |
|
162 | + 'image/jpg', |
|
163 | + 'image/pjpeg', |
|
164 | + 'image/png', |
|
165 | + 'image/gif', |
|
166 | + ), |
|
167 | + 'document' => array( |
|
168 | + 'application/pdf', |
|
169 | + 'application/msword', |
|
170 | + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
|
171 | + 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
|
172 | + 'application/vnd.ms-powerpoint', |
|
173 | + 'application/vnd.ms-excel', |
|
174 | + 'application/vnd.oasis.opendocument.spreadsheet', |
|
175 | + 'application/vnd.oasis.opendocument.presentation', |
|
176 | + ), |
|
177 | + 'video' => array( |
|
178 | + 'video/3gpp', |
|
179 | + 'video/3gpp', |
|
180 | + 'video/x-msvideo', |
|
181 | + 'video/avi', |
|
182 | + 'video/mpeg4', |
|
183 | + 'video/mp4', |
|
184 | + 'video/mpeg', |
|
185 | + 'video/mpg', |
|
186 | + 'video/quicktime', |
|
187 | + 'video/x-sgi-movie', |
|
188 | + 'video/x-ms-wmv', |
|
189 | + 'video/x-flv', |
|
190 | + ), |
|
191 | + ); |
|
192 | + |
|
193 | + /** |
|
194 | + * The upload error message |
|
195 | + * @var array |
|
196 | + */ |
|
197 | + public $error_messages = array(); |
|
198 | + |
|
199 | + /** |
|
200 | + * The upload error message |
|
201 | + * @var string |
|
202 | + */ |
|
203 | + protected $error = null; |
|
204 | + |
|
205 | + /** |
|
206 | + * The logger instance |
|
207 | + * @var Log |
|
208 | + */ |
|
209 | + private $logger; |
|
210 | + |
|
211 | + |
|
212 | + /** |
|
213 | + * Construct |
|
214 | + * |
|
215 | + * @since 0.1 |
|
216 | + * @version 1.0.1 |
|
217 | + * @return object |
|
218 | + * @method object __construct |
|
219 | + */ |
|
220 | + public function __construct(){ |
|
221 | + $this->logger =& class_loader('Log', 'classes'); |
|
222 | + $this->logger->setLogger('Library::Upload'); |
|
223 | + |
|
224 | + Loader::lang('file_upload'); |
|
225 | + $obj =& get_instance(); |
|
226 | + |
|
227 | + $this->error_messages = array( |
|
228 | + 'upload_err_ini_size' => $obj->lang->get('fu_upload_err_ini_size'), |
|
229 | + 'upload_err_form_size' => $obj->lang->get('fu_upload_err_form_size'), |
|
230 | + 'upload_err_partial' => $obj->lang->get('fu_upload_err_partial'), |
|
231 | + 'upload_err_no_file' => $obj->lang->get('fu_upload_err_no_file'), |
|
232 | + 'upload_err_no_tmp_dir' => $obj->lang->get('fu_upload_err_no_tmp_dir'), |
|
233 | + 'upload_err_cant_write' => $obj->lang->get('fu_upload_err_cant_write'), |
|
234 | + 'upload_err_extension' => $obj->lang->get('fu_upload_err_extension'), |
|
235 | + 'accept_file_types' => $obj->lang->get('fu_accept_file_types'), |
|
236 | + 'file_uploads' => $obj->lang->get('fu_file_uploads_disabled'), |
|
237 | + 'max_file_size' => $obj->lang->get('fu_max_file_size'), |
|
238 | + 'overwritten_not_allowed' => $obj->lang->get('fu_overwritten_not_allowed'), |
|
239 | + ); |
|
240 | + |
|
241 | + $this->file = array( |
|
242 | + 'status' => false, // True: success upload |
|
243 | + 'mime' => '', // Empty string |
|
244 | + 'filename' => '', // Empty string |
|
245 | + 'original' => '', // Empty string |
|
246 | + 'size' => 0, // 0 Bytes |
|
247 | + 'sizeFormated' => '0B', // 0 Bytes |
|
248 | + 'destination' => './', // Default: ./ |
|
249 | + 'allowed_mime_types' => array(), // Allowed mime types |
|
250 | + 'error' => null, // File error |
|
251 | + ); |
|
252 | + |
|
253 | + // Change dir to current dir |
|
254 | + $this->destination_directory = dirname(__FILE__) . DIRECTORY_SEPARATOR; |
|
255 | + |
|
256 | + // Set file array |
|
257 | + if (isset($_FILES) && is_array($_FILES)) { |
|
258 | + $this->file_array = $_FILES; |
|
259 | + } elseif (isset($HTTP_POST_FILES) && is_array($HTTP_POST_FILES)) { |
|
260 | + $this->file_array = $HTTP_POST_FILES; |
|
261 | + } |
|
262 | + $this->logger->info('The upload file information are : ' .stringfy_vars($this->file_array)); |
|
263 | + } |
|
264 | + /** |
|
265 | + * Set input. |
|
266 | + * If you have $_FILES["file"], you must use the key "file" |
|
267 | + * Example: |
|
268 | + * $object->setInput("file"); |
|
269 | + * |
|
270 | + * @since 1.0 |
|
271 | + * @version 1.0 |
|
272 | + * @param string $input |
|
273 | + * @return boolean |
|
274 | + * @method boolean setInput |
|
275 | + */ |
|
276 | + public function setInput($input) |
|
277 | + { |
|
278 | + if (!empty($input) && (is_string($input) || is_numeric($input) )) { |
|
279 | + $this->input = $input; |
|
280 | + } |
|
281 | + return $this; |
|
282 | + } |
|
283 | + /** |
|
284 | + * Set new filename |
|
285 | + * Example: |
|
286 | + * FileUpload::setFilename("new file.txt") |
|
287 | + * Remember: |
|
288 | + * Use %s to retrive file extension |
|
289 | + * |
|
290 | + * @since 1.0 |
|
291 | + * @version 1.0 |
|
292 | + * @param string $filename |
|
293 | + * @return boolean |
|
294 | + * @method boolean setFilename |
|
295 | + */ |
|
296 | + public function setFilename($filename) |
|
297 | + { |
|
298 | + if ($this->isFilename($filename)) { |
|
299 | + $this->filename = $filename; |
|
300 | + } |
|
301 | + return $this; |
|
302 | + } |
|
303 | + /** |
|
304 | + * Set automatic filename |
|
305 | + * |
|
306 | + * @since 1.0 |
|
307 | + * @version 1.5 |
|
308 | + * @param string $extension |
|
309 | + * @return boolean |
|
310 | + * @method boolean setAutoFilename |
|
311 | + */ |
|
312 | + public function setAutoFilename() |
|
313 | + { |
|
314 | + $this->filename = sha1(mt_rand(1, 9999).uniqid()); |
|
315 | + $this->filename .= time(); |
|
316 | + return $this; |
|
317 | + } |
|
318 | + /** |
|
319 | + * Set file size limit |
|
320 | + * |
|
321 | + * @since 1.0 |
|
322 | + * @version 1.0 |
|
323 | + * @param integer $file_size |
|
324 | + * @return boolean |
|
325 | + * @method boolean setMaxFileSize |
|
326 | + */ |
|
327 | + public function setMaxFileSize($file_size) |
|
328 | + { |
|
329 | + $file_size = $this->sizeInBytes($file_size); |
|
330 | + if (is_numeric($file_size) && $file_size > -1) { |
|
331 | + // Get php config |
|
332 | + $php_size = $this->sizeInBytes(ini_get('upload_max_filesize')); |
|
333 | + // Calculate difference |
|
334 | + if ($php_size < $file_size) { |
|
335 | + $this->logger->warning('The upload max file size you set [' .$file_size. '] is greather than the PHP configuration for upload max file size [' .$php_size. ']'); |
|
336 | + } |
|
337 | + $this->max_file_size = $file_size; |
|
338 | + } |
|
339 | + return $this; |
|
340 | + } |
|
341 | + /** |
|
342 | + * Set array mime types |
|
343 | + * |
|
344 | + * @since 1.0 |
|
345 | + * @version 1.0 |
|
346 | + * @param array $mimes |
|
347 | + * @return boolean |
|
348 | + * @method boolean setAllowedMimeTypes |
|
349 | + */ |
|
350 | + public function setAllowedMimeTypes(array $mimes) |
|
351 | + { |
|
352 | + if (count($mimes) > 0) { |
|
353 | + array_map(array($this , 'setAllowMimeType'), $mimes); |
|
354 | + } |
|
355 | + return $this; |
|
356 | + } |
|
357 | + /** |
|
358 | + * Set input callback |
|
359 | + * |
|
360 | + * @since 1.0 |
|
361 | + * @version 1.0 |
|
362 | + * @param mixed $callback |
|
363 | + * @return boolean |
|
364 | + * @method boolean setCallbackInput |
|
365 | + */ |
|
366 | + public function setCallbackInput($callback) |
|
367 | + { |
|
368 | + if (is_callable($callback, false)) { |
|
369 | + $this->callbacks['input'] = $callback; |
|
370 | + } |
|
371 | + return $this; |
|
372 | + } |
|
373 | + /** |
|
374 | + * Set output callback |
|
375 | + * |
|
376 | + * @since 1.0 |
|
377 | + * @version 1.0 |
|
378 | + * @param mixed $callback |
|
379 | + * @return boolean |
|
380 | + * @method boolean setCallbackOutput |
|
381 | + */ |
|
382 | + public function setCallbackOutput($callback) |
|
383 | + { |
|
384 | + if (is_callable($callback, false)) { |
|
385 | + $this->callbacks['output'] = $callback; |
|
386 | + } |
|
387 | + return $this; |
|
388 | + } |
|
389 | + /** |
|
390 | + * Append a mime type to allowed mime types |
|
391 | + * |
|
392 | + * @since 1.0 |
|
393 | + * @version 1.0.1 |
|
394 | + * @param string $mime |
|
395 | + * @return boolean |
|
396 | + * @method boolean setAllowMimeType |
|
397 | + */ |
|
398 | + public function setAllowMimeType($mime) |
|
399 | + { |
|
400 | + if (!empty($mime) && is_string($mime)) { |
|
401 | + $this->allowed_mime_types[] = strtolower($mime); |
|
402 | + $this->file['allowed_mime_types'][] = strtolower($mime); |
|
403 | + } |
|
404 | + return $this; |
|
405 | + } |
|
406 | + /** |
|
407 | + * Set allowed mime types from mime helping |
|
408 | + * |
|
409 | + * @since 1.0.1 |
|
410 | + * @version 1.0.1 |
|
411 | + * @return boolean |
|
412 | + * @method boolean setMimeHelping |
|
413 | + */ |
|
414 | + public function setMimeHelping($name) |
|
415 | + { |
|
416 | + if (!empty($name) && is_string($name)) { |
|
417 | + if (array_key_exists($name, $this->mime_helping)) { |
|
418 | + return $this->setAllowedMimeTypes($this->mime_helping[ $name ]); |
|
419 | + } |
|
420 | + } |
|
421 | + return $this; |
|
422 | + } |
|
423 | + /** |
|
424 | + * Set function to upload file |
|
425 | + * Examples: |
|
426 | + * 1.- FileUpload::setUploadFunction("move_uploaded_file"); |
|
427 | + * 2.- FileUpload::setUploadFunction("copy"); |
|
428 | + * |
|
429 | + * @since 1.0 |
|
430 | + * @version 1.0 |
|
431 | + * @param string $mime |
|
432 | + * @return boolean |
|
433 | + * @method boolean setUploadFunction |
|
434 | + */ |
|
435 | + public function setUploadFunction($function) |
|
436 | + { |
|
437 | + if (!empty($function) && (is_array($function) || is_string($function) )) { |
|
438 | + if (is_callable( $function)) { |
|
439 | + $this->upload_function = $function; |
|
440 | + } |
|
441 | + } |
|
442 | + return $this; |
|
443 | + } |
|
444 | + /** |
|
445 | + * Clear allowed mime types cache |
|
446 | + * |
|
447 | + * @since 1.0 |
|
448 | + * @version 1.0 |
|
449 | + * @return boolean |
|
450 | + * @method boolean clearAllowedMimeTypes |
|
451 | + */ |
|
452 | + public function clearAllowedMimeTypes() |
|
453 | + { |
|
454 | + $this->allowed_mime_types = array(); |
|
455 | + $this->file['allowed_mime_types'] = array(); |
|
456 | + return $this; |
|
457 | + } |
|
458 | + /** |
|
459 | + * Set destination output |
|
460 | + * |
|
461 | + * @since 1.0 |
|
462 | + * @version 1.0 |
|
463 | + * @param string $destination_directory Destination path |
|
464 | + * @param boolean $create_if_not_exist |
|
465 | + * @return boolean |
|
466 | + * @method boolean setDestinationDirectory |
|
467 | + */ |
|
468 | + public function setDestinationDirectory($destination_directory, $create_if_not_exist = false) { |
|
469 | + $destination_directory = realpath($destination_directory); |
|
470 | + if (substr($destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
471 | + $destination_directory .= DIRECTORY_SEPARATOR; |
|
472 | + } |
|
473 | + |
|
474 | + if ($this->isDirpath($destination_directory)) { |
|
475 | + if ($this->dirExists($destination_directory)) { |
|
476 | + $this->destination_directory = $destination_directory; |
|
477 | + if (substr($this->destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
478 | + $this->destination_directory .= DIRECTORY_SEPARATOR; |
|
479 | + } |
|
480 | + chdir($destination_directory); |
|
481 | + } elseif ($create_if_not_exist === true) { |
|
482 | + if (mkdir($destination_directory, $this->destination_permissions, true)) { |
|
483 | + if ($this->dirExists($destination_directory)) { |
|
484 | + $this->destination_directory = $destination_directory; |
|
485 | + if (substr($this->destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
486 | + $this->destination_directory .= DIRECTORY_SEPARATOR; |
|
487 | + } |
|
488 | + chdir($destination_directory); |
|
489 | + } |
|
490 | + } |
|
491 | + else{ |
|
492 | + $this->logger->warning('Can not create the upload directory [' .$destination_directory. ']'); |
|
493 | + } |
|
494 | + } |
|
495 | + } |
|
496 | + return $this; |
|
497 | + } |
|
498 | + /** |
|
499 | + * Check file exists |
|
500 | + * |
|
501 | + * @since 1.0 |
|
502 | + * @version 1.0.1 |
|
503 | + * @param string $file_destination |
|
504 | + * @return boolean |
|
505 | + * @method boolean fileExists |
|
506 | + */ |
|
507 | + public function fileExists($file_destination) |
|
508 | + { |
|
509 | + if ($this->isFilename($file_destination)) { |
|
510 | + return (file_exists($file_destination) && is_file($file_destination)); |
|
511 | + } |
|
512 | + return false; |
|
513 | + } |
|
514 | + /** |
|
515 | + * Check dir exists |
|
516 | + * |
|
517 | + * @since 1.0 |
|
518 | + * @version 1.0.1 |
|
519 | + * @param string $path |
|
520 | + * @return boolean |
|
521 | + * @method boolean dirExists |
|
522 | + */ |
|
523 | + public function dirExists($path) |
|
524 | + { |
|
525 | + if ($this->isDirpath($path)) { |
|
526 | + return (file_exists($path) && is_dir($path)); |
|
527 | + } |
|
528 | + return false; |
|
529 | + } |
|
530 | + /** |
|
531 | + * Check valid filename |
|
532 | + * |
|
533 | + * @since 1.0 |
|
534 | + * @version 1.0.1 |
|
535 | + * @param string $filename |
|
536 | + * @return boolean |
|
537 | + * @method boolean isFilename |
|
538 | + */ |
|
539 | + public function isFilename($filename) |
|
540 | + { |
|
541 | + $filename = basename($filename); |
|
542 | + return (!empty($filename) && (is_string( $filename) || is_numeric($filename))); |
|
543 | + } |
|
544 | + /** |
|
545 | + * Validate mime type with allowed mime types, |
|
546 | + * but if allowed mime types is empty, this method return true |
|
547 | + * |
|
548 | + * @since 1.0 |
|
549 | + * @version 1.0 |
|
550 | + * @param string $mime |
|
551 | + * @return boolean |
|
552 | + * @method boolean checkMimeType |
|
553 | + */ |
|
554 | + public function checkMimeType($mime) |
|
555 | + { |
|
556 | + if (count($this->allowed_mime_types) == 0) { |
|
557 | + return true; |
|
558 | + } |
|
559 | + return in_array(strtolower($mime), $this->allowed_mime_types); |
|
560 | + } |
|
561 | + /** |
|
562 | + * Retrive status of upload |
|
563 | + * |
|
564 | + * @since 1.0 |
|
565 | + * @version 1.0 |
|
566 | + * @return boolean |
|
567 | + * @method boolean getStatus |
|
568 | + */ |
|
569 | + public function getStatus() |
|
570 | + { |
|
571 | + return $this->file['status']; |
|
572 | + } |
|
573 | + /** |
|
574 | + * Check valid path |
|
575 | + * |
|
576 | + * @since 1.0 |
|
577 | + * @version 1.0.1 |
|
578 | + * @param string $filename |
|
579 | + * @return boolean |
|
580 | + * @method boolean isDirpath |
|
581 | + */ |
|
582 | + public function isDirpath($path) |
|
583 | + { |
|
584 | + if (!empty( $path) && (is_string( $path) || is_numeric($path) )) { |
|
585 | + if (DIRECTORY_SEPARATOR == '/') { |
|
586 | + return (preg_match( '/^[^*?"<>|:]*$/' , $path) == 1 ); |
|
587 | + } else { |
|
588 | + return (preg_match( "/^[^*?\"<>|:]*$/" , substr($path,2) ) == 1); |
|
589 | + } |
|
590 | + } |
|
591 | + return false; |
|
592 | + } |
|
593 | + /** |
|
594 | + * Allow overwriting files |
|
595 | + * |
|
596 | + * @since 1.0 |
|
597 | + * @version 1.0 |
|
598 | + * @return boolean |
|
599 | + * @method boolean allowOverwriting |
|
600 | + */ |
|
601 | + public function allowOverwriting() |
|
602 | + { |
|
603 | + $this->overwrite_file = true; |
|
604 | + return $this; |
|
605 | + } |
|
606 | + /** |
|
607 | + * File info |
|
608 | + * |
|
609 | + * @since 1.0 |
|
610 | + * @version 1.0 |
|
611 | + * @return object |
|
612 | + * @method object getInfo |
|
613 | + */ |
|
614 | + public function getInfo() |
|
615 | + { |
|
616 | + return (object)$this->file; |
|
617 | + } |
|
618 | + |
|
619 | + public function isUploaded(){ |
|
620 | + return isset($this->file_array[$this->input]) |
|
621 | + && |
|
622 | + is_uploaded_file($this->file_array[$this->input]['tmp_name']); |
|
623 | + } |
|
624 | + /** |
|
625 | + * Upload file |
|
626 | + * |
|
627 | + * @since 1.0 |
|
628 | + * @version 1.0.1 |
|
629 | + * @return boolean |
|
630 | + * @method boolean save |
|
631 | + */ |
|
632 | + public function save(){ |
|
633 | + //check if file upload is allowed in the configuration |
|
634 | + if(! ini_get('file_uploads')){ |
|
635 | + $this->setError($this->error_messages['file_uploads']); |
|
636 | + return false; |
|
637 | + } |
|
638 | + if (count($this->file_array) > 0) { |
|
639 | + if (array_key_exists($this->input, $this->file_array)) { |
|
640 | + // set original filename if not have a new name |
|
641 | + if (empty($this->filename)) { |
|
642 | + $this->filename = $this->file_array[$this->input]['name']; |
|
643 | + } |
|
644 | + else{ |
|
645 | + // Replace %s for extension in filename |
|
646 | + // Before: /[\w\d]*(.[\d\w]+)$/i |
|
647 | + // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |
|
648 | + // Support unicode(utf-8) characters |
|
649 | + // Example: "русские.jpeg" is valid; "Zhōngguó.jpeg" is valid; "Tønsberg.jpeg" is valid |
|
650 | + $extension = preg_replace( |
|
651 | + '/^[\p{L}\d\s\-\_\.\(\)]*\.([\d\w]+)$/iu', |
|
652 | + '$1', |
|
653 | + $this->file_array[$this->input]['name'] |
|
654 | + ); |
|
655 | + $this->filename = $this->filename.'.'.$extension; |
|
656 | + } |
|
657 | + |
|
658 | + // set file info |
|
659 | + $this->file['mime'] = $this->file_array[$this->input]['type']; |
|
660 | + $this->file['tmp'] = $this->file_array[$this->input]['tmp_name']; |
|
661 | + $this->file['original'] = $this->file_array[$this->input]['name']; |
|
662 | + $this->file['size'] = $this->file_array[$this->input]['size']; |
|
663 | + $this->file['sizeFormated'] = $this->sizeFormat($this->file['size']); |
|
664 | + $this->file['destination'] = $this->destination_directory . $this->filename; |
|
665 | + $this->file['filename'] = $this->filename; |
|
666 | + $this->file['error'] = $this->file_array[$this->input]['error']; |
|
667 | + |
|
668 | + $this->logger->info('The upload file information to process is : ' .stringfy_vars($this->file)); |
|
669 | + |
|
670 | + //check for php upload error |
|
671 | + if(is_numeric($this->file['error']) && $this->file['error'] > 0){ |
|
672 | + $this->setError($this->getPhpUploadErrorMessageByCode($this->file['error'])); |
|
673 | + return false; |
|
674 | + } |
|
675 | 675 | |
676 | - //check for mime type |
|
677 | - if (!$this->checkMimeType($this->file['mime'])) { |
|
678 | - $this->setError($this->error_messages['accept_file_types']); |
|
679 | - return false; |
|
680 | - } |
|
681 | - |
|
682 | - // Check file size |
|
683 | - if ($this->max_file_size > 0) { |
|
684 | - if ($this->max_file_size < $this->file['size']) { |
|
685 | - $this->setError(sprintf($this->error_messages['max_file_size'], $this->sizeFormat($this->max_file_size))); |
|
686 | - return false; |
|
687 | - } |
|
688 | - } |
|
689 | - |
|
690 | - // Check if exists file |
|
691 | - if ($this->fileExists($this->destination_directory . $this->filename)) { |
|
692 | - // Check if overwrite file |
|
693 | - if ($this->overwrite_file === false) { |
|
694 | - $this->setError($this->error_messages['overwritten_not_allowed']); |
|
695 | - return false; |
|
696 | - } |
|
697 | - } |
|
698 | - |
|
699 | - // Execute input callback |
|
700 | - if (!empty( $this->callbacks['input'])) { |
|
701 | - call_user_func($this->callbacks['input'], (object)$this->file); |
|
702 | - } |
|
676 | + //check for mime type |
|
677 | + if (!$this->checkMimeType($this->file['mime'])) { |
|
678 | + $this->setError($this->error_messages['accept_file_types']); |
|
679 | + return false; |
|
680 | + } |
|
681 | + |
|
682 | + // Check file size |
|
683 | + if ($this->max_file_size > 0) { |
|
684 | + if ($this->max_file_size < $this->file['size']) { |
|
685 | + $this->setError(sprintf($this->error_messages['max_file_size'], $this->sizeFormat($this->max_file_size))); |
|
686 | + return false; |
|
687 | + } |
|
688 | + } |
|
689 | + |
|
690 | + // Check if exists file |
|
691 | + if ($this->fileExists($this->destination_directory . $this->filename)) { |
|
692 | + // Check if overwrite file |
|
693 | + if ($this->overwrite_file === false) { |
|
694 | + $this->setError($this->error_messages['overwritten_not_allowed']); |
|
695 | + return false; |
|
696 | + } |
|
697 | + } |
|
698 | + |
|
699 | + // Execute input callback |
|
700 | + if (!empty( $this->callbacks['input'])) { |
|
701 | + call_user_func($this->callbacks['input'], (object)$this->file); |
|
702 | + } |
|
703 | 703 | |
704 | 704 | |
705 | - $this->file['status'] = call_user_func_array( |
|
706 | - $this->upload_function, array( |
|
707 | - $this->file_array[$this->input]['tmp_name'], |
|
708 | - $this->destination_directory . $this->filename |
|
709 | - ) |
|
710 | - ); |
|
711 | - |
|
712 | - // Execute output callback |
|
713 | - if (!empty( $this->callbacks['output'])) { |
|
714 | - call_user_func($this->callbacks['output'], (object)$this->file); |
|
715 | - } |
|
716 | - return $this->file['status']; |
|
717 | - } |
|
718 | - } |
|
719 | - } |
|
720 | - |
|
721 | - /** |
|
722 | - * File size for humans. |
|
723 | - * |
|
724 | - * @since 1.0 |
|
725 | - * @version 1.0 |
|
726 | - * @param integer $bytes |
|
727 | - * @param integer $precision |
|
728 | - * @return string |
|
729 | - * @method string sizeFormat |
|
730 | - */ |
|
731 | - public function sizeFormat($size, $precision = 2) |
|
732 | - { |
|
733 | - if($size > 0){ |
|
734 | - $base = log($size) / log(1024); |
|
735 | - $suffixes = array('B', 'K', 'M', 'G', 'T'); |
|
736 | - return round(pow(1024, $base - floor($base)), $precision) . ( isset($suffixes[floor($base)]) ? $suffixes[floor($base)] : ''); |
|
737 | - } |
|
738 | - return null; |
|
739 | - } |
|
705 | + $this->file['status'] = call_user_func_array( |
|
706 | + $this->upload_function, array( |
|
707 | + $this->file_array[$this->input]['tmp_name'], |
|
708 | + $this->destination_directory . $this->filename |
|
709 | + ) |
|
710 | + ); |
|
711 | + |
|
712 | + // Execute output callback |
|
713 | + if (!empty( $this->callbacks['output'])) { |
|
714 | + call_user_func($this->callbacks['output'], (object)$this->file); |
|
715 | + } |
|
716 | + return $this->file['status']; |
|
717 | + } |
|
718 | + } |
|
719 | + } |
|
720 | + |
|
721 | + /** |
|
722 | + * File size for humans. |
|
723 | + * |
|
724 | + * @since 1.0 |
|
725 | + * @version 1.0 |
|
726 | + * @param integer $bytes |
|
727 | + * @param integer $precision |
|
728 | + * @return string |
|
729 | + * @method string sizeFormat |
|
730 | + */ |
|
731 | + public function sizeFormat($size, $precision = 2) |
|
732 | + { |
|
733 | + if($size > 0){ |
|
734 | + $base = log($size) / log(1024); |
|
735 | + $suffixes = array('B', 'K', 'M', 'G', 'T'); |
|
736 | + return round(pow(1024, $base - floor($base)), $precision) . ( isset($suffixes[floor($base)]) ? $suffixes[floor($base)] : ''); |
|
737 | + } |
|
738 | + return null; |
|
739 | + } |
|
740 | 740 | |
741 | 741 | |
742 | - /** |
|
743 | - * Convert human file size to bytes |
|
744 | - * |
|
745 | - * @since 1.0 |
|
746 | - * @version 1.0.1 |
|
747 | - * @param integer $size |
|
748 | - * @return string |
|
749 | - * @method string sizeInBytes |
|
750 | - */ |
|
751 | - public function sizeInBytes($size) |
|
752 | - { |
|
753 | - $unit = 'B'; |
|
754 | - $units = array('B' => 0, 'K' => 1, 'M' => 2, 'G' => 3, 'T' => 4); |
|
755 | - $matches = array(); |
|
756 | - preg_match('/(?<size>[\d\.]+)\s*(?<unit>b|k|m|g|t)?/i', $size, $matches); |
|
757 | - if (array_key_exists('unit', $matches)) { |
|
758 | - $unit = strtoupper($matches['unit']); |
|
759 | - } |
|
760 | - return (floatval($matches['size']) * pow(1024, $units[$unit]) ) ; |
|
761 | - } |
|
762 | - |
|
763 | - /** |
|
764 | - * Get the upload error message |
|
765 | - * @return string |
|
766 | - */ |
|
767 | - public function getError(){ |
|
768 | - return $this->error; |
|
769 | - } |
|
770 | - |
|
771 | - /** |
|
772 | - * Set the upload error message |
|
773 | - * @param string $message the upload error message to set |
|
774 | - */ |
|
775 | - public function setError($message){ |
|
776 | - $this->logger->info('The upload got error : ' . $message); |
|
777 | - $this->error = $message; |
|
778 | - } |
|
779 | - |
|
780 | - /** |
|
781 | - * Get the PHP upload error message for the given code |
|
782 | - * @param int $code the error code |
|
783 | - * @return string the error message |
|
784 | - */ |
|
785 | - private function getPhpUploadErrorMessageByCode($code){ |
|
786 | - if(! is_int($code) || $code <= 0){ |
|
787 | - return null; |
|
788 | - } |
|
789 | - switch ($code) { |
|
790 | - case 1: |
|
791 | - return $this->error_messages['upload_err_ini_size']; |
|
792 | - break; |
|
793 | - case 2: |
|
794 | - return $this->error_messages['upload_err_form_size']; |
|
795 | - break; |
|
796 | - case 3: |
|
797 | - return $this->error_messages['upload_err_partial']; |
|
798 | - break; |
|
799 | - case 4: |
|
800 | - return $this->error_messages['upload_err_no_file']; |
|
801 | - break; |
|
802 | - case 6: |
|
803 | - return $this->error_messages['upload_err_no_tmp_dir']; |
|
804 | - break; |
|
805 | - case 7: |
|
806 | - return $this->error_messages['upload_err_cant_write']; |
|
807 | - break; |
|
808 | - case 8: |
|
809 | - return $this->error_messages['upload_err_extension']; |
|
810 | - break; |
|
811 | - default: |
|
812 | - return null; |
|
813 | - break; |
|
814 | - } |
|
815 | - } |
|
816 | - } |
|
742 | + /** |
|
743 | + * Convert human file size to bytes |
|
744 | + * |
|
745 | + * @since 1.0 |
|
746 | + * @version 1.0.1 |
|
747 | + * @param integer $size |
|
748 | + * @return string |
|
749 | + * @method string sizeInBytes |
|
750 | + */ |
|
751 | + public function sizeInBytes($size) |
|
752 | + { |
|
753 | + $unit = 'B'; |
|
754 | + $units = array('B' => 0, 'K' => 1, 'M' => 2, 'G' => 3, 'T' => 4); |
|
755 | + $matches = array(); |
|
756 | + preg_match('/(?<size>[\d\.]+)\s*(?<unit>b|k|m|g|t)?/i', $size, $matches); |
|
757 | + if (array_key_exists('unit', $matches)) { |
|
758 | + $unit = strtoupper($matches['unit']); |
|
759 | + } |
|
760 | + return (floatval($matches['size']) * pow(1024, $units[$unit]) ) ; |
|
761 | + } |
|
762 | + |
|
763 | + /** |
|
764 | + * Get the upload error message |
|
765 | + * @return string |
|
766 | + */ |
|
767 | + public function getError(){ |
|
768 | + return $this->error; |
|
769 | + } |
|
770 | + |
|
771 | + /** |
|
772 | + * Set the upload error message |
|
773 | + * @param string $message the upload error message to set |
|
774 | + */ |
|
775 | + public function setError($message){ |
|
776 | + $this->logger->info('The upload got error : ' . $message); |
|
777 | + $this->error = $message; |
|
778 | + } |
|
779 | + |
|
780 | + /** |
|
781 | + * Get the PHP upload error message for the given code |
|
782 | + * @param int $code the error code |
|
783 | + * @return string the error message |
|
784 | + */ |
|
785 | + private function getPhpUploadErrorMessageByCode($code){ |
|
786 | + if(! is_int($code) || $code <= 0){ |
|
787 | + return null; |
|
788 | + } |
|
789 | + switch ($code) { |
|
790 | + case 1: |
|
791 | + return $this->error_messages['upload_err_ini_size']; |
|
792 | + break; |
|
793 | + case 2: |
|
794 | + return $this->error_messages['upload_err_form_size']; |
|
795 | + break; |
|
796 | + case 3: |
|
797 | + return $this->error_messages['upload_err_partial']; |
|
798 | + break; |
|
799 | + case 4: |
|
800 | + return $this->error_messages['upload_err_no_file']; |
|
801 | + break; |
|
802 | + case 6: |
|
803 | + return $this->error_messages['upload_err_no_tmp_dir']; |
|
804 | + break; |
|
805 | + case 7: |
|
806 | + return $this->error_messages['upload_err_cant_write']; |
|
807 | + break; |
|
808 | + case 8: |
|
809 | + return $this->error_messages['upload_err_extension']; |
|
810 | + break; |
|
811 | + default: |
|
812 | + return null; |
|
813 | + break; |
|
814 | + } |
|
815 | + } |
|
816 | + } |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | * @package FileUpload |
38 | 38 | * @version 1.5 |
39 | 39 | */ |
40 | - class Upload{ |
|
40 | + class Upload { |
|
41 | 41 | |
42 | 42 | /** |
43 | 43 | * Version |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | * @version 1.0 |
70 | 70 | * @var array |
71 | 71 | */ |
72 | - private $file_array = array(); |
|
72 | + private $file_array = array(); |
|
73 | 73 | |
74 | 74 | /** |
75 | 75 | * If the file you are trying to upload already exists it will |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | * @version 1.0 |
120 | 120 | * @var float |
121 | 121 | */ |
122 | - private $max_file_size= 0.0; |
|
122 | + private $max_file_size = 0.0; |
|
123 | 123 | |
124 | 124 | /** |
125 | 125 | * List of allowed mime types |
@@ -217,12 +217,12 @@ discard block |
||
217 | 217 | * @return object |
218 | 218 | * @method object __construct |
219 | 219 | */ |
220 | - public function __construct(){ |
|
221 | - $this->logger =& class_loader('Log', 'classes'); |
|
220 | + public function __construct() { |
|
221 | + $this->logger = & class_loader('Log', 'classes'); |
|
222 | 222 | $this->logger->setLogger('Library::Upload'); |
223 | 223 | |
224 | 224 | Loader::lang('file_upload'); |
225 | - $obj =& get_instance(); |
|
225 | + $obj = & get_instance(); |
|
226 | 226 | |
227 | 227 | $this->error_messages = array( |
228 | 228 | 'upload_err_ini_size' => $obj->lang->get('fu_upload_err_ini_size'), |
@@ -239,15 +239,15 @@ discard block |
||
239 | 239 | ); |
240 | 240 | |
241 | 241 | $this->file = array( |
242 | - 'status' => false, // True: success upload |
|
243 | - 'mime' => '', // Empty string |
|
244 | - 'filename' => '', // Empty string |
|
245 | - 'original' => '', // Empty string |
|
246 | - 'size' => 0, // 0 Bytes |
|
247 | - 'sizeFormated' => '0B', // 0 Bytes |
|
248 | - 'destination' => './', // Default: ./ |
|
249 | - 'allowed_mime_types' => array(), // Allowed mime types |
|
250 | - 'error' => null, // File error |
|
242 | + 'status' => false, // True: success upload |
|
243 | + 'mime' => '', // Empty string |
|
244 | + 'filename' => '', // Empty string |
|
245 | + 'original' => '', // Empty string |
|
246 | + 'size' => 0, // 0 Bytes |
|
247 | + 'sizeFormated' => '0B', // 0 Bytes |
|
248 | + 'destination' => './', // Default: ./ |
|
249 | + 'allowed_mime_types' => array(), // Allowed mime types |
|
250 | + 'error' => null, // File error |
|
251 | 251 | ); |
252 | 252 | |
253 | 253 | // Change dir to current dir |
@@ -259,7 +259,7 @@ discard block |
||
259 | 259 | } elseif (isset($HTTP_POST_FILES) && is_array($HTTP_POST_FILES)) { |
260 | 260 | $this->file_array = $HTTP_POST_FILES; |
261 | 261 | } |
262 | - $this->logger->info('The upload file information are : ' .stringfy_vars($this->file_array)); |
|
262 | + $this->logger->info('The upload file information are : ' . stringfy_vars($this->file_array)); |
|
263 | 263 | } |
264 | 264 | /** |
265 | 265 | * Set input. |
@@ -275,7 +275,7 @@ discard block |
||
275 | 275 | */ |
276 | 276 | public function setInput($input) |
277 | 277 | { |
278 | - if (!empty($input) && (is_string($input) || is_numeric($input) )) { |
|
278 | + if (!empty($input) && (is_string($input) || is_numeric($input))) { |
|
279 | 279 | $this->input = $input; |
280 | 280 | } |
281 | 281 | return $this; |
@@ -311,7 +311,7 @@ discard block |
||
311 | 311 | */ |
312 | 312 | public function setAutoFilename() |
313 | 313 | { |
314 | - $this->filename = sha1(mt_rand(1, 9999).uniqid()); |
|
314 | + $this->filename = sha1(mt_rand(1, 9999) . uniqid()); |
|
315 | 315 | $this->filename .= time(); |
316 | 316 | return $this; |
317 | 317 | } |
@@ -332,7 +332,7 @@ discard block |
||
332 | 332 | $php_size = $this->sizeInBytes(ini_get('upload_max_filesize')); |
333 | 333 | // Calculate difference |
334 | 334 | if ($php_size < $file_size) { |
335 | - $this->logger->warning('The upload max file size you set [' .$file_size. '] is greather than the PHP configuration for upload max file size [' .$php_size. ']'); |
|
335 | + $this->logger->warning('The upload max file size you set [' . $file_size . '] is greather than the PHP configuration for upload max file size [' . $php_size . ']'); |
|
336 | 336 | } |
337 | 337 | $this->max_file_size = $file_size; |
338 | 338 | } |
@@ -350,7 +350,7 @@ discard block |
||
350 | 350 | public function setAllowedMimeTypes(array $mimes) |
351 | 351 | { |
352 | 352 | if (count($mimes) > 0) { |
353 | - array_map(array($this , 'setAllowMimeType'), $mimes); |
|
353 | + array_map(array($this, 'setAllowMimeType'), $mimes); |
|
354 | 354 | } |
355 | 355 | return $this; |
356 | 356 | } |
@@ -415,7 +415,7 @@ discard block |
||
415 | 415 | { |
416 | 416 | if (!empty($name) && is_string($name)) { |
417 | 417 | if (array_key_exists($name, $this->mime_helping)) { |
418 | - return $this->setAllowedMimeTypes($this->mime_helping[ $name ]); |
|
418 | + return $this->setAllowedMimeTypes($this->mime_helping[$name]); |
|
419 | 419 | } |
420 | 420 | } |
421 | 421 | return $this; |
@@ -434,8 +434,8 @@ discard block |
||
434 | 434 | */ |
435 | 435 | public function setUploadFunction($function) |
436 | 436 | { |
437 | - if (!empty($function) && (is_array($function) || is_string($function) )) { |
|
438 | - if (is_callable( $function)) { |
|
437 | + if (!empty($function) && (is_array($function) || is_string($function))) { |
|
438 | + if (is_callable($function)) { |
|
439 | 439 | $this->upload_function = $function; |
440 | 440 | } |
441 | 441 | } |
@@ -488,8 +488,8 @@ discard block |
||
488 | 488 | chdir($destination_directory); |
489 | 489 | } |
490 | 490 | } |
491 | - else{ |
|
492 | - $this->logger->warning('Can not create the upload directory [' .$destination_directory. ']'); |
|
491 | + else { |
|
492 | + $this->logger->warning('Can not create the upload directory [' . $destination_directory . ']'); |
|
493 | 493 | } |
494 | 494 | } |
495 | 495 | } |
@@ -539,7 +539,7 @@ discard block |
||
539 | 539 | public function isFilename($filename) |
540 | 540 | { |
541 | 541 | $filename = basename($filename); |
542 | - return (!empty($filename) && (is_string( $filename) || is_numeric($filename))); |
|
542 | + return (!empty($filename) && (is_string($filename) || is_numeric($filename))); |
|
543 | 543 | } |
544 | 544 | /** |
545 | 545 | * Validate mime type with allowed mime types, |
@@ -581,11 +581,11 @@ discard block |
||
581 | 581 | */ |
582 | 582 | public function isDirpath($path) |
583 | 583 | { |
584 | - if (!empty( $path) && (is_string( $path) || is_numeric($path) )) { |
|
584 | + if (!empty($path) && (is_string($path) || is_numeric($path))) { |
|
585 | 585 | if (DIRECTORY_SEPARATOR == '/') { |
586 | - return (preg_match( '/^[^*?"<>|:]*$/' , $path) == 1 ); |
|
586 | + return (preg_match('/^[^*?"<>|:]*$/', $path) == 1); |
|
587 | 587 | } else { |
588 | - return (preg_match( "/^[^*?\"<>|:]*$/" , substr($path,2) ) == 1); |
|
588 | + return (preg_match("/^[^*?\"<>|:]*$/", substr($path, 2)) == 1); |
|
589 | 589 | } |
590 | 590 | } |
591 | 591 | return false; |
@@ -613,10 +613,10 @@ discard block |
||
613 | 613 | */ |
614 | 614 | public function getInfo() |
615 | 615 | { |
616 | - return (object)$this->file; |
|
616 | + return (object) $this->file; |
|
617 | 617 | } |
618 | 618 | |
619 | - public function isUploaded(){ |
|
619 | + public function isUploaded() { |
|
620 | 620 | return isset($this->file_array[$this->input]) |
621 | 621 | && |
622 | 622 | is_uploaded_file($this->file_array[$this->input]['tmp_name']); |
@@ -629,9 +629,9 @@ discard block |
||
629 | 629 | * @return boolean |
630 | 630 | * @method boolean save |
631 | 631 | */ |
632 | - public function save(){ |
|
632 | + public function save() { |
|
633 | 633 | //check if file upload is allowed in the configuration |
634 | - if(! ini_get('file_uploads')){ |
|
634 | + if (!ini_get('file_uploads')) { |
|
635 | 635 | $this->setError($this->error_messages['file_uploads']); |
636 | 636 | return false; |
637 | 637 | } |
@@ -641,7 +641,7 @@ discard block |
||
641 | 641 | if (empty($this->filename)) { |
642 | 642 | $this->filename = $this->file_array[$this->input]['name']; |
643 | 643 | } |
644 | - else{ |
|
644 | + else { |
|
645 | 645 | // Replace %s for extension in filename |
646 | 646 | // Before: /[\w\d]*(.[\d\w]+)$/i |
647 | 647 | // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |
@@ -652,7 +652,7 @@ discard block |
||
652 | 652 | '$1', |
653 | 653 | $this->file_array[$this->input]['name'] |
654 | 654 | ); |
655 | - $this->filename = $this->filename.'.'.$extension; |
|
655 | + $this->filename = $this->filename . '.' . $extension; |
|
656 | 656 | } |
657 | 657 | |
658 | 658 | // set file info |
@@ -665,10 +665,10 @@ discard block |
||
665 | 665 | $this->file['filename'] = $this->filename; |
666 | 666 | $this->file['error'] = $this->file_array[$this->input]['error']; |
667 | 667 | |
668 | - $this->logger->info('The upload file information to process is : ' .stringfy_vars($this->file)); |
|
668 | + $this->logger->info('The upload file information to process is : ' . stringfy_vars($this->file)); |
|
669 | 669 | |
670 | 670 | //check for php upload error |
671 | - if(is_numeric($this->file['error']) && $this->file['error'] > 0){ |
|
671 | + if (is_numeric($this->file['error']) && $this->file['error'] > 0) { |
|
672 | 672 | $this->setError($this->getPhpUploadErrorMessageByCode($this->file['error'])); |
673 | 673 | return false; |
674 | 674 | } |
@@ -697,8 +697,8 @@ discard block |
||
697 | 697 | } |
698 | 698 | |
699 | 699 | // Execute input callback |
700 | - if (!empty( $this->callbacks['input'])) { |
|
701 | - call_user_func($this->callbacks['input'], (object)$this->file); |
|
700 | + if (!empty($this->callbacks['input'])) { |
|
701 | + call_user_func($this->callbacks['input'], (object) $this->file); |
|
702 | 702 | } |
703 | 703 | |
704 | 704 | |
@@ -710,8 +710,8 @@ discard block |
||
710 | 710 | ); |
711 | 711 | |
712 | 712 | // Execute output callback |
713 | - if (!empty( $this->callbacks['output'])) { |
|
714 | - call_user_func($this->callbacks['output'], (object)$this->file); |
|
713 | + if (!empty($this->callbacks['output'])) { |
|
714 | + call_user_func($this->callbacks['output'], (object) $this->file); |
|
715 | 715 | } |
716 | 716 | return $this->file['status']; |
717 | 717 | } |
@@ -730,10 +730,10 @@ discard block |
||
730 | 730 | */ |
731 | 731 | public function sizeFormat($size, $precision = 2) |
732 | 732 | { |
733 | - if($size > 0){ |
|
733 | + if ($size > 0) { |
|
734 | 734 | $base = log($size) / log(1024); |
735 | 735 | $suffixes = array('B', 'K', 'M', 'G', 'T'); |
736 | - return round(pow(1024, $base - floor($base)), $precision) . ( isset($suffixes[floor($base)]) ? $suffixes[floor($base)] : ''); |
|
736 | + return round(pow(1024, $base - floor($base)), $precision) . (isset($suffixes[floor($base)]) ? $suffixes[floor($base)] : ''); |
|
737 | 737 | } |
738 | 738 | return null; |
739 | 739 | } |
@@ -757,14 +757,14 @@ discard block |
||
757 | 757 | if (array_key_exists('unit', $matches)) { |
758 | 758 | $unit = strtoupper($matches['unit']); |
759 | 759 | } |
760 | - return (floatval($matches['size']) * pow(1024, $units[$unit]) ) ; |
|
760 | + return (floatval($matches['size']) * pow(1024, $units[$unit])); |
|
761 | 761 | } |
762 | 762 | |
763 | 763 | /** |
764 | 764 | * Get the upload error message |
765 | 765 | * @return string |
766 | 766 | */ |
767 | - public function getError(){ |
|
767 | + public function getError() { |
|
768 | 768 | return $this->error; |
769 | 769 | } |
770 | 770 | |
@@ -772,7 +772,7 @@ discard block |
||
772 | 772 | * Set the upload error message |
773 | 773 | * @param string $message the upload error message to set |
774 | 774 | */ |
775 | - public function setError($message){ |
|
775 | + public function setError($message) { |
|
776 | 776 | $this->logger->info('The upload got error : ' . $message); |
777 | 777 | $this->error = $message; |
778 | 778 | } |
@@ -782,8 +782,8 @@ discard block |
||
782 | 782 | * @param int $code the error code |
783 | 783 | * @return string the error message |
784 | 784 | */ |
785 | - private function getPhpUploadErrorMessageByCode($code){ |
|
786 | - if(! is_int($code) || $code <= 0){ |
|
785 | + private function getPhpUploadErrorMessageByCode($code) { |
|
786 | + if (!is_int($code) || $code <= 0) { |
|
787 | 787 | return null; |
788 | 788 | } |
789 | 789 | switch ($code) { |
@@ -487,8 +487,7 @@ discard block |
||
487 | 487 | } |
488 | 488 | chdir($destination_directory); |
489 | 489 | } |
490 | - } |
|
491 | - else{ |
|
490 | + } else{ |
|
492 | 491 | $this->logger->warning('Can not create the upload directory [' .$destination_directory. ']'); |
493 | 492 | } |
494 | 493 | } |
@@ -640,8 +639,7 @@ discard block |
||
640 | 639 | // set original filename if not have a new name |
641 | 640 | if (empty($this->filename)) { |
642 | 641 | $this->filename = $this->file_array[$this->input]['name']; |
643 | - } |
|
644 | - else{ |
|
642 | + } else{ |
|
645 | 643 | // Replace %s for extension in filename |
646 | 644 | // Before: /[\w\d]*(.[\d\w]+)$/i |
647 | 645 | // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |
@@ -1,918 +1,918 @@ |
||
1 | 1 | <?php |
2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
3 | - /** |
|
4 | - * TNH Framework |
|
5 | - * |
|
6 | - * A simple PHP framework using HMVC architecture |
|
7 | - * |
|
8 | - * This content is released under the GNU GPL License (GPL) |
|
9 | - * |
|
10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
11 | - * |
|
12 | - * This program is free software; you can redistribute it and/or |
|
13 | - * modify it under the terms of the GNU General Public License |
|
14 | - * as published by the Free Software Foundation; either version 3 |
|
15 | - * of the License, or (at your option) any later version. |
|
16 | - * |
|
17 | - * This program is distributed in the hope that it will be useful, |
|
18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | - * GNU General Public License for more details. |
|
21 | - * |
|
22 | - * You should have received a copy of the GNU General Public License |
|
23 | - * along with this program; if not, write to the Free Software |
|
24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | - */ |
|
26 | - |
|
27 | - |
|
28 | - class FormValidation{ |
|
2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
3 | + /** |
|
4 | + * TNH Framework |
|
5 | + * |
|
6 | + * A simple PHP framework using HMVC architecture |
|
7 | + * |
|
8 | + * This content is released under the GNU GPL License (GPL) |
|
9 | + * |
|
10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
11 | + * |
|
12 | + * This program is free software; you can redistribute it and/or |
|
13 | + * modify it under the terms of the GNU General Public License |
|
14 | + * as published by the Free Software Foundation; either version 3 |
|
15 | + * of the License, or (at your option) any later version. |
|
16 | + * |
|
17 | + * This program is distributed in the hope that it will be useful, |
|
18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | + * GNU General Public License for more details. |
|
21 | + * |
|
22 | + * You should have received a copy of the GNU General Public License |
|
23 | + * along with this program; if not, write to the Free Software |
|
24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | + */ |
|
26 | + |
|
27 | + |
|
28 | + class FormValidation{ |
|
29 | 29 | |
30 | - /** |
|
31 | - * The form validation status |
|
32 | - * @var boolean |
|
33 | - */ |
|
34 | - protected $_success = false; |
|
35 | - |
|
36 | - /** |
|
37 | - * The list of errors messages |
|
38 | - * @var array |
|
39 | - */ |
|
40 | - protected $_errorsMessages = array(); |
|
30 | + /** |
|
31 | + * The form validation status |
|
32 | + * @var boolean |
|
33 | + */ |
|
34 | + protected $_success = false; |
|
35 | + |
|
36 | + /** |
|
37 | + * The list of errors messages |
|
38 | + * @var array |
|
39 | + */ |
|
40 | + protected $_errorsMessages = array(); |
|
41 | 41 | |
42 | - // Array of rule sets, fieldName => PIPE seperated ruleString |
|
43 | - protected $_rules = array(); |
|
42 | + // Array of rule sets, fieldName => PIPE seperated ruleString |
|
43 | + protected $_rules = array(); |
|
44 | 44 | |
45 | - // Array of errors, niceName => Error Message |
|
46 | - protected $_errors = array(); |
|
45 | + // Array of errors, niceName => Error Message |
|
46 | + protected $_errors = array(); |
|
47 | 47 | |
48 | - // Array of post Key => Nice name labels |
|
49 | - protected $_labels = array(); |
|
48 | + // Array of post Key => Nice name labels |
|
49 | + protected $_labels = array(); |
|
50 | 50 | |
51 | - /** |
|
52 | - * The errors delimiters |
|
53 | - * @var array |
|
54 | - */ |
|
55 | - protected $_allErrorsDelimiter = array('<div class="error">', '</div>'); |
|
56 | - |
|
57 | - /** |
|
58 | - * The each error delimiter |
|
59 | - * @var array |
|
60 | - */ |
|
61 | - protected $_eachErrorDelimiter = array('<p class="error">', '</p>'); |
|
51 | + /** |
|
52 | + * The errors delimiters |
|
53 | + * @var array |
|
54 | + */ |
|
55 | + protected $_allErrorsDelimiter = array('<div class="error">', '</div>'); |
|
56 | + |
|
57 | + /** |
|
58 | + * The each error delimiter |
|
59 | + * @var array |
|
60 | + */ |
|
61 | + protected $_eachErrorDelimiter = array('<p class="error">', '</p>'); |
|
62 | 62 | |
63 | 63 | /** |
64 | - * Indicated if need force the validation to be failed |
|
65 | - * @var boolean |
|
66 | - */ |
|
67 | - protected $_forceFail = false; |
|
68 | - |
|
69 | - /** |
|
70 | - * The list of the error messages overrides by the original |
|
71 | - * @var array |
|
72 | - */ |
|
73 | - protected $_errorPhraseOverrides = array(); |
|
74 | - |
|
75 | - /** |
|
76 | - * The logger instance |
|
77 | - * @var Log |
|
78 | - */ |
|
79 | - private $logger; |
|
80 | - |
|
81 | - /** |
|
82 | - * The data to be validated, the default is to use $_POST |
|
83 | - * @var array |
|
84 | - */ |
|
85 | - private $data = array(); |
|
86 | - |
|
87 | - /** |
|
88 | - * Whether to check the CSRF. This attribute is just a way to allow custom change of the |
|
64 | + * Indicated if need force the validation to be failed |
|
65 | + * @var boolean |
|
66 | + */ |
|
67 | + protected $_forceFail = false; |
|
68 | + |
|
69 | + /** |
|
70 | + * The list of the error messages overrides by the original |
|
71 | + * @var array |
|
72 | + */ |
|
73 | + protected $_errorPhraseOverrides = array(); |
|
74 | + |
|
75 | + /** |
|
76 | + * The logger instance |
|
77 | + * @var Log |
|
78 | + */ |
|
79 | + private $logger; |
|
80 | + |
|
81 | + /** |
|
82 | + * The data to be validated, the default is to use $_POST |
|
83 | + * @var array |
|
84 | + */ |
|
85 | + private $data = array(); |
|
86 | + |
|
87 | + /** |
|
88 | + * Whether to check the CSRF. This attribute is just a way to allow custom change of the |
|
89 | 89 | * CSRF global configuration |
90 | 90 | * |
91 | - * @var boolean |
|
92 | - */ |
|
93 | - public $enableCsrfCheck = false; |
|
94 | - |
|
95 | - /** |
|
96 | - * Set all errors and rule sets empty, and sets success to false. |
|
97 | - * |
|
98 | - * @return void |
|
99 | - */ |
|
100 | - public function __construct() { |
|
101 | - $this->logger =& class_loader('Log', 'classes'); |
|
102 | - $this->logger->setLogger('Library::FormValidation'); |
|
91 | + * @var boolean |
|
92 | + */ |
|
93 | + public $enableCsrfCheck = false; |
|
94 | + |
|
95 | + /** |
|
96 | + * Set all errors and rule sets empty, and sets success to false. |
|
97 | + * |
|
98 | + * @return void |
|
99 | + */ |
|
100 | + public function __construct() { |
|
101 | + $this->logger =& class_loader('Log', 'classes'); |
|
102 | + $this->logger->setLogger('Library::FormValidation'); |
|
103 | 103 | |
104 | 104 | //Load form validation language message |
105 | - Loader::lang('form_validation'); |
|
106 | - $obj = & get_instance(); |
|
107 | - $this->_errorsMessages = array( |
|
108 | - 'required' => $obj->lang->get('fv_required'), |
|
109 | - 'min_length' => $obj->lang->get('fv_min_length'), |
|
110 | - 'max_length' => $obj->lang->get('fv_max_length'), |
|
111 | - 'exact_length' => $obj->lang->get('fv_exact_length'), |
|
112 | - 'less_than' => $obj->lang->get('fv_less_than'), |
|
113 | - 'greater_than' => $obj->lang->get('fv_greater_than'), |
|
114 | - 'matches' => $obj->lang->get('fv_matches'), |
|
115 | - 'valid_email' => $obj->lang->get('fv_valid_email'), |
|
116 | - 'not_equal' => array( |
|
117 | - 'post:key' => $obj->lang->get('fv_not_equal_post_key'), |
|
118 | - 'string' => $obj->lang->get('fv_not_equal_string') |
|
119 | - ), |
|
120 | - 'depends' => $obj->lang->get('fv_depends'), |
|
121 | - 'is_unique' => $obj->lang->get('fv_is_unique'), |
|
122 | - 'is_unique_update' => $obj->lang->get('fv_is_unique_update'), |
|
123 | - 'exists' => $obj->lang->get('fv_exists'), |
|
124 | - 'regex' => $obj->lang->get('fv_regex'), |
|
125 | - 'in_list' => $obj->lang->get('fv_in_list'), |
|
126 | - 'numeric' => $obj->lang->get('fv_numeric'), |
|
127 | - 'callback' => $obj->lang->get('fv_callback'), |
|
128 | - ); |
|
129 | - $this->_resetValidation(); |
|
130 | - $this->setData($obj->request->post(null)); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Reset the form validation instance |
|
135 | - */ |
|
136 | - protected function _resetValidation() { |
|
137 | - $this->_rules = array(); |
|
138 | - $this->_labels = array(); |
|
139 | - $this->_errorPhraseOverrides = array(); |
|
140 | - $this->_errors = array(); |
|
141 | - $this->_success = false; |
|
142 | - $this->_forceFail = false; |
|
143 | - $this->data = array(); |
|
105 | + Loader::lang('form_validation'); |
|
106 | + $obj = & get_instance(); |
|
107 | + $this->_errorsMessages = array( |
|
108 | + 'required' => $obj->lang->get('fv_required'), |
|
109 | + 'min_length' => $obj->lang->get('fv_min_length'), |
|
110 | + 'max_length' => $obj->lang->get('fv_max_length'), |
|
111 | + 'exact_length' => $obj->lang->get('fv_exact_length'), |
|
112 | + 'less_than' => $obj->lang->get('fv_less_than'), |
|
113 | + 'greater_than' => $obj->lang->get('fv_greater_than'), |
|
114 | + 'matches' => $obj->lang->get('fv_matches'), |
|
115 | + 'valid_email' => $obj->lang->get('fv_valid_email'), |
|
116 | + 'not_equal' => array( |
|
117 | + 'post:key' => $obj->lang->get('fv_not_equal_post_key'), |
|
118 | + 'string' => $obj->lang->get('fv_not_equal_string') |
|
119 | + ), |
|
120 | + 'depends' => $obj->lang->get('fv_depends'), |
|
121 | + 'is_unique' => $obj->lang->get('fv_is_unique'), |
|
122 | + 'is_unique_update' => $obj->lang->get('fv_is_unique_update'), |
|
123 | + 'exists' => $obj->lang->get('fv_exists'), |
|
124 | + 'regex' => $obj->lang->get('fv_regex'), |
|
125 | + 'in_list' => $obj->lang->get('fv_in_list'), |
|
126 | + 'numeric' => $obj->lang->get('fv_numeric'), |
|
127 | + 'callback' => $obj->lang->get('fv_callback'), |
|
128 | + ); |
|
129 | + $this->_resetValidation(); |
|
130 | + $this->setData($obj->request->post(null)); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Reset the form validation instance |
|
135 | + */ |
|
136 | + protected function _resetValidation() { |
|
137 | + $this->_rules = array(); |
|
138 | + $this->_labels = array(); |
|
139 | + $this->_errorPhraseOverrides = array(); |
|
140 | + $this->_errors = array(); |
|
141 | + $this->_success = false; |
|
142 | + $this->_forceFail = false; |
|
143 | + $this->data = array(); |
|
144 | 144 | $this->enableCsrfCheck = false; |
145 | - } |
|
145 | + } |
|
146 | 146 | |
147 | - /** |
|
148 | - * Set the form validation data |
|
149 | - * @param array $data the values to be validated |
|
147 | + /** |
|
148 | + * Set the form validation data |
|
149 | + * @param array $data the values to be validated |
|
150 | 150 | * |
151 | - * @return FormValidation Current instance of object. |
|
152 | - */ |
|
153 | - public function setData(array $data){ |
|
154 | - $this->logger->debug('Setting the form validation data, the values are: ' . stringfy_vars($data)); |
|
155 | - $this->data = $data; |
|
151 | + * @return FormValidation Current instance of object. |
|
152 | + */ |
|
153 | + public function setData(array $data){ |
|
154 | + $this->logger->debug('Setting the form validation data, the values are: ' . stringfy_vars($data)); |
|
155 | + $this->data = $data; |
|
156 | 156 | return $this; |
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Get the form validation data |
|
161 | - * @return array the form validation data to be validated |
|
162 | - */ |
|
163 | - public function getData(){ |
|
164 | - return $this->data; |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Get the validation function name to validate a rule |
|
169 | - * |
|
170 | - * @return string the function name |
|
171 | - */ |
|
172 | - protected function _toCallCase($funcName, $prefix='_validate') { |
|
173 | - $funcName = strtolower($funcName); |
|
174 | - $finalFuncName = $prefix; |
|
175 | - foreach (explode('_', $funcName) as $funcNamePart) { |
|
176 | - $finalFuncName .= strtoupper($funcNamePart[0]) . substr($funcNamePart, 1); |
|
177 | - } |
|
178 | - return $finalFuncName; |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Returns the boolean of the data status success. It goes by the simple |
|
183 | - * |
|
184 | - * @return boolean Whether or not the data validation has succeeded |
|
185 | - */ |
|
186 | - public function isSuccess() { |
|
187 | - return $this->_success; |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Checks if the request method is POST or the Data to be validated is set |
|
192 | - * |
|
193 | - * @return boolean Whether or not the form has been submitted or the data is available for validation. |
|
194 | - */ |
|
195 | - public function canDoValidation() { |
|
196 | - return get_instance()->request->method() === 'POST' || ! empty($this->data); |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Runs _run once POST data has been submitted or data is set manually. |
|
201 | - * |
|
202 | - * @return boolean |
|
203 | - */ |
|
204 | - public function run() { |
|
205 | - if ($this->canDoValidation()) { |
|
206 | - $this->logger->info('The data to validate are listed below: ' . stringfy_vars($this->getData())); |
|
207 | - $this->_run(); |
|
208 | - } |
|
209 | - return $this->isSuccess(); |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * Takes and trims each data, if it has any rules, we parse the rule string and run |
|
214 | - * each rule against the data value. Sets _success to true if there are no errors |
|
215 | - * afterwards. |
|
216 | - */ |
|
217 | - protected function _run() { |
|
218 | - if(get_instance()->request->method() == 'POST' || $this->enableCsrfCheck){ |
|
219 | - $this->logger->debug('Check if CSRF is enabled in configuration'); |
|
220 | - //first check for CSRF |
|
221 | - if( get_config('csrf_enable', false) || $this->enableCsrfCheck){ |
|
222 | - $this->logger->info('Check the CSRF value if is valid'); |
|
223 | - if(! Security::validateCSRF()){ |
|
224 | - show_error('Invalide data, Cross Site Request Forgery do his job, the data to validate is corrupted.'); |
|
225 | - } |
|
226 | - } |
|
227 | - else{ |
|
228 | - $this->logger->info('CSRF is not enabled in configuration or not set manully, no need to check it'); |
|
229 | - } |
|
230 | - } |
|
231 | - ///////////////////////////////////////////// |
|
232 | - $this->_forceFail = false; |
|
233 | - |
|
234 | - foreach ($this->getData() as $inputName => $inputVal) { |
|
235 | - if(is_array($this->data[$inputName])){ |
|
236 | - $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
|
237 | - } |
|
238 | - else{ |
|
239 | - $this->data[$inputName] = trim($this->data[$inputName]); |
|
240 | - } |
|
241 | - |
|
242 | - if (array_key_exists($inputName, $this->_rules)) { |
|
243 | - foreach ($this->_parseRuleString($this->_rules[$inputName]) as $eachRule) { |
|
244 | - $this->_validateRule($inputName, $this->data[$inputName], $eachRule); |
|
245 | - } |
|
246 | - } |
|
247 | - } |
|
248 | - |
|
249 | - if (empty($this->_errors) && $this->_forceFail === false) { |
|
250 | - $this->_success = true; |
|
251 | - } |
|
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * Adds a rule to a form data validation field. |
|
256 | - * |
|
257 | - * @param string $inputField Name of the field or the data key to add a rule to |
|
258 | - * @param string $ruleSets PIPE seperated string of rules |
|
259 | - * |
|
260 | - * @return FormValidation Current instance of object. |
|
261 | - */ |
|
262 | - public function setRule($inputField, $inputLabel, $ruleSets) { |
|
263 | - $this->_rules[$inputField] = $ruleSets; |
|
264 | - $this->_labels[$inputField] = $inputLabel; |
|
265 | - $this->logger->info('Set the field rule: name [' .$inputField. '], label [' .$inputLabel. '], rules [' .$ruleSets. ']'); |
|
266 | - return $this; |
|
267 | - } |
|
268 | - |
|
269 | - /** |
|
270 | - * Takes an array of rules and uses setRule() to set them, accepts an array |
|
271 | - * of rule names rather than a pipe-delimited string as well. |
|
272 | - * @param array $ruleSets |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Get the form validation data |
|
161 | + * @return array the form validation data to be validated |
|
162 | + */ |
|
163 | + public function getData(){ |
|
164 | + return $this->data; |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Get the validation function name to validate a rule |
|
169 | + * |
|
170 | + * @return string the function name |
|
171 | + */ |
|
172 | + protected function _toCallCase($funcName, $prefix='_validate') { |
|
173 | + $funcName = strtolower($funcName); |
|
174 | + $finalFuncName = $prefix; |
|
175 | + foreach (explode('_', $funcName) as $funcNamePart) { |
|
176 | + $finalFuncName .= strtoupper($funcNamePart[0]) . substr($funcNamePart, 1); |
|
177 | + } |
|
178 | + return $finalFuncName; |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Returns the boolean of the data status success. It goes by the simple |
|
183 | + * |
|
184 | + * @return boolean Whether or not the data validation has succeeded |
|
185 | + */ |
|
186 | + public function isSuccess() { |
|
187 | + return $this->_success; |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Checks if the request method is POST or the Data to be validated is set |
|
192 | + * |
|
193 | + * @return boolean Whether or not the form has been submitted or the data is available for validation. |
|
194 | + */ |
|
195 | + public function canDoValidation() { |
|
196 | + return get_instance()->request->method() === 'POST' || ! empty($this->data); |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Runs _run once POST data has been submitted or data is set manually. |
|
201 | + * |
|
202 | + * @return boolean |
|
203 | + */ |
|
204 | + public function run() { |
|
205 | + if ($this->canDoValidation()) { |
|
206 | + $this->logger->info('The data to validate are listed below: ' . stringfy_vars($this->getData())); |
|
207 | + $this->_run(); |
|
208 | + } |
|
209 | + return $this->isSuccess(); |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * Takes and trims each data, if it has any rules, we parse the rule string and run |
|
214 | + * each rule against the data value. Sets _success to true if there are no errors |
|
215 | + * afterwards. |
|
216 | + */ |
|
217 | + protected function _run() { |
|
218 | + if(get_instance()->request->method() == 'POST' || $this->enableCsrfCheck){ |
|
219 | + $this->logger->debug('Check if CSRF is enabled in configuration'); |
|
220 | + //first check for CSRF |
|
221 | + if( get_config('csrf_enable', false) || $this->enableCsrfCheck){ |
|
222 | + $this->logger->info('Check the CSRF value if is valid'); |
|
223 | + if(! Security::validateCSRF()){ |
|
224 | + show_error('Invalide data, Cross Site Request Forgery do his job, the data to validate is corrupted.'); |
|
225 | + } |
|
226 | + } |
|
227 | + else{ |
|
228 | + $this->logger->info('CSRF is not enabled in configuration or not set manully, no need to check it'); |
|
229 | + } |
|
230 | + } |
|
231 | + ///////////////////////////////////////////// |
|
232 | + $this->_forceFail = false; |
|
233 | + |
|
234 | + foreach ($this->getData() as $inputName => $inputVal) { |
|
235 | + if(is_array($this->data[$inputName])){ |
|
236 | + $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
|
237 | + } |
|
238 | + else{ |
|
239 | + $this->data[$inputName] = trim($this->data[$inputName]); |
|
240 | + } |
|
241 | + |
|
242 | + if (array_key_exists($inputName, $this->_rules)) { |
|
243 | + foreach ($this->_parseRuleString($this->_rules[$inputName]) as $eachRule) { |
|
244 | + $this->_validateRule($inputName, $this->data[$inputName], $eachRule); |
|
245 | + } |
|
246 | + } |
|
247 | + } |
|
248 | + |
|
249 | + if (empty($this->_errors) && $this->_forceFail === false) { |
|
250 | + $this->_success = true; |
|
251 | + } |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * Adds a rule to a form data validation field. |
|
256 | + * |
|
257 | + * @param string $inputField Name of the field or the data key to add a rule to |
|
258 | + * @param string $ruleSets PIPE seperated string of rules |
|
273 | 259 | * |
274 | 260 | * @return FormValidation Current instance of object. |
275 | - */ |
|
276 | - public function setRules(array $ruleSets) { |
|
277 | - foreach ($ruleSets as $ruleSet) { |
|
278 | - $pipeDelimitedRules = null; |
|
279 | - if (is_array($ruleSet['rules'])) { |
|
280 | - $pipeDelimitedRules = implode('|', $ruleSet['rules']); |
|
281 | - } else { |
|
282 | - $pipeDelimitedRules = $ruleSet['rules']; |
|
283 | - } |
|
284 | - $this->setRule($ruleSet['name'], $ruleSet['label'], $pipeDelimitedRules); |
|
285 | - } |
|
286 | - return $this; |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * This method creates the global errors delimiter, each argument occurs once, at the beginning, and |
|
291 | - * end of the errors block respectively. |
|
292 | - * |
|
293 | - * @param string $start Before block of errors gets displayed, HTML allowed. |
|
294 | - * @param string $end After the block of errors gets displayed, HTML allowed. |
|
295 | - * |
|
261 | + */ |
|
262 | + public function setRule($inputField, $inputLabel, $ruleSets) { |
|
263 | + $this->_rules[$inputField] = $ruleSets; |
|
264 | + $this->_labels[$inputField] = $inputLabel; |
|
265 | + $this->logger->info('Set the field rule: name [' .$inputField. '], label [' .$inputLabel. '], rules [' .$ruleSets. ']'); |
|
266 | + return $this; |
|
267 | + } |
|
268 | + |
|
269 | + /** |
|
270 | + * Takes an array of rules and uses setRule() to set them, accepts an array |
|
271 | + * of rule names rather than a pipe-delimited string as well. |
|
272 | + * @param array $ruleSets |
|
273 | + * |
|
296 | 274 | * @return FormValidation Current instance of object. |
297 | - */ |
|
298 | - public function setErrorsDelimiter($start, $end) { |
|
299 | - $this->_allErrorsDelimiter[0] = $start; |
|
300 | - $this->_allErrorsDelimiter[1] = $end; |
|
301 | - return $this; |
|
302 | - } |
|
303 | - |
|
304 | - /** |
|
305 | - * This is the individual error delimiter, each argument occurs once before and after |
|
306 | - * each individual error listed. |
|
307 | - * |
|
308 | - * @param string $start Displayed before each error. |
|
309 | - * @param string $end Displayed after each error. |
|
310 | - * |
|
275 | + */ |
|
276 | + public function setRules(array $ruleSets) { |
|
277 | + foreach ($ruleSets as $ruleSet) { |
|
278 | + $pipeDelimitedRules = null; |
|
279 | + if (is_array($ruleSet['rules'])) { |
|
280 | + $pipeDelimitedRules = implode('|', $ruleSet['rules']); |
|
281 | + } else { |
|
282 | + $pipeDelimitedRules = $ruleSet['rules']; |
|
283 | + } |
|
284 | + $this->setRule($ruleSet['name'], $ruleSet['label'], $pipeDelimitedRules); |
|
285 | + } |
|
286 | + return $this; |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * This method creates the global errors delimiter, each argument occurs once, at the beginning, and |
|
291 | + * end of the errors block respectively. |
|
292 | + * |
|
293 | + * @param string $start Before block of errors gets displayed, HTML allowed. |
|
294 | + * @param string $end After the block of errors gets displayed, HTML allowed. |
|
295 | + * |
|
311 | 296 | * @return FormValidation Current instance of object. |
312 | - */ |
|
313 | - public function setErrorDelimiter($start, $end) { |
|
314 | - $this->_eachErrorDelimiter[0] = $start; |
|
315 | - $this->_eachErrorDelimiter[1] = $end; |
|
316 | - return $this; |
|
317 | - } |
|
318 | - |
|
319 | - /** |
|
320 | - * Get the each errors delimiters |
|
321 | - * |
|
322 | - * @return array |
|
323 | - */ |
|
324 | - public function getErrorDelimiter() { |
|
325 | - return $this->_eachErrorDelimiter; |
|
326 | - } |
|
327 | - |
|
328 | - /** |
|
329 | - * Get the all errors delimiters |
|
330 | - * |
|
331 | - * @return array |
|
332 | - */ |
|
333 | - public function getErrorsDelimiter() { |
|
334 | - return $this->_allErrorsDelimiter; |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * This sets a custom error message that can override the default error phrase provided |
|
339 | - * by FormValidation, it can be used in the format of setMessage('rule', 'error phrase') |
|
340 | - * which will globally change the error phrase of that rule, or in the format of: |
|
341 | - * setMessage('rule', 'fieldname', 'error phrase') - which will only change the error phrase for |
|
342 | - * that rule, applied on that field. |
|
343 | - * |
|
344 | - * @return boolean True on success, false on failure. |
|
345 | - */ |
|
346 | - public function setMessage() { |
|
347 | - $numArgs = func_num_args(); |
|
348 | - switch ($numArgs) { |
|
349 | - default: |
|
350 | - return false; |
|
351 | - break; |
|
352 | - // A global rule error message |
|
353 | - case 2: |
|
354 | - foreach ($this->post(null) as $key => $val) { |
|
355 | - $this->_errorPhraseOverrides[$key][func_get_arg(0)] = func_get_arg(1); |
|
356 | - } |
|
357 | - break; |
|
358 | - // Field specific rule error message |
|
359 | - case 3: |
|
360 | - $this->_errorPhraseOverrides[func_get_arg(1)][func_get_arg(0)] = func_get_arg(2); |
|
361 | - break; |
|
362 | - } |
|
363 | - return true; |
|
364 | - } |
|
365 | - |
|
366 | - /** |
|
367 | - * Adds a custom error message in the errorSet array, that will |
|
368 | - * forcibly display it. |
|
369 | - * |
|
370 | - * @param string $inputName The form input name or data key |
|
371 | - * @param string $errorMessage Error to display |
|
372 | - * |
|
373 | - * @return formValidation Current instance of the object |
|
374 | - */ |
|
375 | - public function setCustomError($inputName, $errorMessage) { |
|
376 | - $errorMessage = str_replace('%1', $this->_labels[$inputName], $errorMessage); |
|
377 | - $this->_errors[$inputName] = $errorMessage; |
|
378 | - return $this; |
|
379 | - } |
|
380 | - |
|
381 | - /** |
|
382 | - * Allows for an accesor to any/all post values, if a value of null is passed as the key, it |
|
383 | - * will recursively find all keys/values of the $_POST array or data array. It also automatically trims |
|
384 | - * all values. |
|
385 | - * |
|
386 | - * @param string $key Key of $this->data to be found, pass null for all Key => Val pairs. |
|
387 | - * @param boolean $trim Defaults to true, trims all $this->data values. |
|
388 | - * @return string/array Array of post or data values if null is passed as key, string if only one key is desired. |
|
389 | - */ |
|
390 | - public function post($key = null, $trim = true) { |
|
391 | - $returnValue = null; |
|
392 | - if (is_null($key)) { |
|
393 | - $returnValue = array(); |
|
394 | - foreach ($this->getData() as $key => $val) { |
|
395 | - $returnValue[$key] = $this->post($key, $trim); |
|
396 | - } |
|
397 | - } else { |
|
398 | - $returnValue = (array_key_exists($key, $this->getData())) ? (($trim) ? trim($this->data[$key]) : $this->data[$key]) : false; |
|
399 | - } |
|
400 | - return $returnValue; |
|
401 | - } |
|
402 | - |
|
403 | - /** |
|
404 | - * Gets all errors from errorSet and displays them, can be echo out from the |
|
405 | - * function or just returned. |
|
406 | - * |
|
407 | - * @param boolean $limit number of error to display or return |
|
408 | - * @param boolean $echo Whether or not the values are to be returned or displayed |
|
409 | - * |
|
410 | - * @return string Errors formatted for output |
|
411 | - */ |
|
412 | - public function displayErrors($limit = null, $echo = true) { |
|
413 | - list($errorsStart, $errorsEnd) = $this->_allErrorsDelimiter; |
|
414 | - list($errorStart, $errorEnd) = $this->_eachErrorDelimiter; |
|
415 | - $errorOutput = $errorsStart; |
|
416 | - $i = 0; |
|
417 | - if (!empty($this->_errors)) { |
|
418 | - foreach ($this->_errors as $fieldName => $error) { |
|
419 | - if ($i === $limit) { |
|
420 | - break; |
|
421 | - } |
|
422 | - $errorOutput .= $errorStart; |
|
423 | - $errorOutput .= $error; |
|
424 | - $errorOutput .= $errorEnd; |
|
425 | - $i++; |
|
426 | - } |
|
427 | - } |
|
428 | - $errorOutput .= $errorsEnd; |
|
429 | - echo ($echo) ? $errorOutput : ''; |
|
430 | - return (! $echo) ? $errorOutput : null; |
|
431 | - } |
|
432 | - |
|
433 | - /** |
|
434 | - * Returns raw array of errors in no format instead of displaying them |
|
435 | - * formatted. |
|
436 | - * |
|
437 | - * @return array |
|
438 | - */ |
|
439 | - public function returnErrors() { |
|
440 | - return $this->_errors; |
|
441 | - } |
|
442 | - |
|
443 | - /** |
|
444 | - * Breaks up a PIPE seperated string of rules, and puts them into an array. |
|
445 | - * |
|
446 | - * @param string $ruleString String to be parsed. |
|
447 | - * |
|
448 | - * @return array Array of each value in original string. |
|
449 | - */ |
|
450 | - protected function _parseRuleString($ruleString) { |
|
451 | - $ruleSets = array(); |
|
452 | - /* |
|
297 | + */ |
|
298 | + public function setErrorsDelimiter($start, $end) { |
|
299 | + $this->_allErrorsDelimiter[0] = $start; |
|
300 | + $this->_allErrorsDelimiter[1] = $end; |
|
301 | + return $this; |
|
302 | + } |
|
303 | + |
|
304 | + /** |
|
305 | + * This is the individual error delimiter, each argument occurs once before and after |
|
306 | + * each individual error listed. |
|
307 | + * |
|
308 | + * @param string $start Displayed before each error. |
|
309 | + * @param string $end Displayed after each error. |
|
310 | + * |
|
311 | + * @return FormValidation Current instance of object. |
|
312 | + */ |
|
313 | + public function setErrorDelimiter($start, $end) { |
|
314 | + $this->_eachErrorDelimiter[0] = $start; |
|
315 | + $this->_eachErrorDelimiter[1] = $end; |
|
316 | + return $this; |
|
317 | + } |
|
318 | + |
|
319 | + /** |
|
320 | + * Get the each errors delimiters |
|
321 | + * |
|
322 | + * @return array |
|
323 | + */ |
|
324 | + public function getErrorDelimiter() { |
|
325 | + return $this->_eachErrorDelimiter; |
|
326 | + } |
|
327 | + |
|
328 | + /** |
|
329 | + * Get the all errors delimiters |
|
330 | + * |
|
331 | + * @return array |
|
332 | + */ |
|
333 | + public function getErrorsDelimiter() { |
|
334 | + return $this->_allErrorsDelimiter; |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * This sets a custom error message that can override the default error phrase provided |
|
339 | + * by FormValidation, it can be used in the format of setMessage('rule', 'error phrase') |
|
340 | + * which will globally change the error phrase of that rule, or in the format of: |
|
341 | + * setMessage('rule', 'fieldname', 'error phrase') - which will only change the error phrase for |
|
342 | + * that rule, applied on that field. |
|
343 | + * |
|
344 | + * @return boolean True on success, false on failure. |
|
345 | + */ |
|
346 | + public function setMessage() { |
|
347 | + $numArgs = func_num_args(); |
|
348 | + switch ($numArgs) { |
|
349 | + default: |
|
350 | + return false; |
|
351 | + break; |
|
352 | + // A global rule error message |
|
353 | + case 2: |
|
354 | + foreach ($this->post(null) as $key => $val) { |
|
355 | + $this->_errorPhraseOverrides[$key][func_get_arg(0)] = func_get_arg(1); |
|
356 | + } |
|
357 | + break; |
|
358 | + // Field specific rule error message |
|
359 | + case 3: |
|
360 | + $this->_errorPhraseOverrides[func_get_arg(1)][func_get_arg(0)] = func_get_arg(2); |
|
361 | + break; |
|
362 | + } |
|
363 | + return true; |
|
364 | + } |
|
365 | + |
|
366 | + /** |
|
367 | + * Adds a custom error message in the errorSet array, that will |
|
368 | + * forcibly display it. |
|
369 | + * |
|
370 | + * @param string $inputName The form input name or data key |
|
371 | + * @param string $errorMessage Error to display |
|
372 | + * |
|
373 | + * @return formValidation Current instance of the object |
|
374 | + */ |
|
375 | + public function setCustomError($inputName, $errorMessage) { |
|
376 | + $errorMessage = str_replace('%1', $this->_labels[$inputName], $errorMessage); |
|
377 | + $this->_errors[$inputName] = $errorMessage; |
|
378 | + return $this; |
|
379 | + } |
|
380 | + |
|
381 | + /** |
|
382 | + * Allows for an accesor to any/all post values, if a value of null is passed as the key, it |
|
383 | + * will recursively find all keys/values of the $_POST array or data array. It also automatically trims |
|
384 | + * all values. |
|
385 | + * |
|
386 | + * @param string $key Key of $this->data to be found, pass null for all Key => Val pairs. |
|
387 | + * @param boolean $trim Defaults to true, trims all $this->data values. |
|
388 | + * @return string/array Array of post or data values if null is passed as key, string if only one key is desired. |
|
389 | + */ |
|
390 | + public function post($key = null, $trim = true) { |
|
391 | + $returnValue = null; |
|
392 | + if (is_null($key)) { |
|
393 | + $returnValue = array(); |
|
394 | + foreach ($this->getData() as $key => $val) { |
|
395 | + $returnValue[$key] = $this->post($key, $trim); |
|
396 | + } |
|
397 | + } else { |
|
398 | + $returnValue = (array_key_exists($key, $this->getData())) ? (($trim) ? trim($this->data[$key]) : $this->data[$key]) : false; |
|
399 | + } |
|
400 | + return $returnValue; |
|
401 | + } |
|
402 | + |
|
403 | + /** |
|
404 | + * Gets all errors from errorSet and displays them, can be echo out from the |
|
405 | + * function or just returned. |
|
406 | + * |
|
407 | + * @param boolean $limit number of error to display or return |
|
408 | + * @param boolean $echo Whether or not the values are to be returned or displayed |
|
409 | + * |
|
410 | + * @return string Errors formatted for output |
|
411 | + */ |
|
412 | + public function displayErrors($limit = null, $echo = true) { |
|
413 | + list($errorsStart, $errorsEnd) = $this->_allErrorsDelimiter; |
|
414 | + list($errorStart, $errorEnd) = $this->_eachErrorDelimiter; |
|
415 | + $errorOutput = $errorsStart; |
|
416 | + $i = 0; |
|
417 | + if (!empty($this->_errors)) { |
|
418 | + foreach ($this->_errors as $fieldName => $error) { |
|
419 | + if ($i === $limit) { |
|
420 | + break; |
|
421 | + } |
|
422 | + $errorOutput .= $errorStart; |
|
423 | + $errorOutput .= $error; |
|
424 | + $errorOutput .= $errorEnd; |
|
425 | + $i++; |
|
426 | + } |
|
427 | + } |
|
428 | + $errorOutput .= $errorsEnd; |
|
429 | + echo ($echo) ? $errorOutput : ''; |
|
430 | + return (! $echo) ? $errorOutput : null; |
|
431 | + } |
|
432 | + |
|
433 | + /** |
|
434 | + * Returns raw array of errors in no format instead of displaying them |
|
435 | + * formatted. |
|
436 | + * |
|
437 | + * @return array |
|
438 | + */ |
|
439 | + public function returnErrors() { |
|
440 | + return $this->_errors; |
|
441 | + } |
|
442 | + |
|
443 | + /** |
|
444 | + * Breaks up a PIPE seperated string of rules, and puts them into an array. |
|
445 | + * |
|
446 | + * @param string $ruleString String to be parsed. |
|
447 | + * |
|
448 | + * @return array Array of each value in original string. |
|
449 | + */ |
|
450 | + protected function _parseRuleString($ruleString) { |
|
451 | + $ruleSets = array(); |
|
452 | + /* |
|
453 | 453 | //////////////// hack for regex rule that can contain "|" |
454 | 454 | */ |
455 | - if(strpos($ruleString, 'regex') !== false){ |
|
456 | - $regexRule = array(); |
|
457 | - $rule = '#regex\[\/(.*)\/([a-zA-Z0-9]?)\]#'; |
|
458 | - preg_match($rule, $ruleString, $regexRule); |
|
459 | - $ruleStringTemp = preg_replace($rule, '', $ruleString); |
|
460 | - if(isset($regexRule[0]) && !empty($regexRule[0])){ |
|
461 | - $ruleSets[] = $regexRule[0]; |
|
462 | - } |
|
463 | - $ruleStringRegex = explode('|', $ruleStringTemp); |
|
464 | - if(is_array($ruleStringRegex)){ |
|
465 | - foreach ($ruleStringRegex as $rule) { |
|
466 | - $rule = trim($rule); |
|
467 | - if($rule){ |
|
468 | - $ruleSets[] = $rule; |
|
469 | - } |
|
470 | - } |
|
471 | - } |
|
472 | - } |
|
473 | - /***********************************/ |
|
474 | - else{ |
|
475 | - if (strpos($ruleString, '|') !== FALSE) { |
|
476 | - $ruleSets = explode('|', $ruleString); |
|
477 | - } else { |
|
478 | - $ruleSets[] = $ruleString; |
|
479 | - } |
|
480 | - } |
|
481 | - return $ruleSets; |
|
482 | - } |
|
483 | - |
|
484 | - /** |
|
485 | - * Returns whether or not a field obtains the rule "required". |
|
486 | - * |
|
487 | - * @param string $fieldName Field to check if required. |
|
488 | - * |
|
489 | - * @return boolean Whether or not the field is required. |
|
490 | - */ |
|
491 | - protected function _fieldIsRequired($fieldName) { |
|
492 | - $rules = $this->_parseRuleString($this->_rules[$fieldName]); |
|
493 | - return (in_array('required', $rules)); |
|
494 | - } |
|
495 | - |
|
496 | - /** |
|
497 | - * Takes a data input name, it's value, and the rule it's being validated against (ex: max_length[16]) |
|
498 | - * and adds an error to the errorSet if it fails validation of the rule. |
|
499 | - * |
|
500 | - * @param string $inputName Name or key of the validation data |
|
501 | - * @param string $inputVal Value of the validation data |
|
502 | - * @param string $ruleName Rule to be validated against, including args (exact_length[5]) |
|
503 | - * @return void |
|
504 | - */ |
|
505 | - protected function _validateRule($inputName, $inputVal, $ruleName) { |
|
506 | - $this->logger->debug('Rule validation of field [' .$inputName. '], value [' .$inputVal. '], rule [' .$ruleName. ']'); |
|
507 | - // Array to store args |
|
508 | - $ruleArgs = array(); |
|
509 | - |
|
510 | - // Get the rule arguments, realRule is just the base rule name |
|
511 | - // Like min_length instead of min_length[3] |
|
512 | - $realRule = preg_match('/\[(.*)\]/', $ruleName, $ruleArgs); |
|
513 | - $ruleName = preg_replace('/\[(.*)\]/', '', $ruleName); |
|
455 | + if(strpos($ruleString, 'regex') !== false){ |
|
456 | + $regexRule = array(); |
|
457 | + $rule = '#regex\[\/(.*)\/([a-zA-Z0-9]?)\]#'; |
|
458 | + preg_match($rule, $ruleString, $regexRule); |
|
459 | + $ruleStringTemp = preg_replace($rule, '', $ruleString); |
|
460 | + if(isset($regexRule[0]) && !empty($regexRule[0])){ |
|
461 | + $ruleSets[] = $regexRule[0]; |
|
462 | + } |
|
463 | + $ruleStringRegex = explode('|', $ruleStringTemp); |
|
464 | + if(is_array($ruleStringRegex)){ |
|
465 | + foreach ($ruleStringRegex as $rule) { |
|
466 | + $rule = trim($rule); |
|
467 | + if($rule){ |
|
468 | + $ruleSets[] = $rule; |
|
469 | + } |
|
470 | + } |
|
471 | + } |
|
472 | + } |
|
473 | + /***********************************/ |
|
474 | + else{ |
|
475 | + if (strpos($ruleString, '|') !== FALSE) { |
|
476 | + $ruleSets = explode('|', $ruleString); |
|
477 | + } else { |
|
478 | + $ruleSets[] = $ruleString; |
|
479 | + } |
|
480 | + } |
|
481 | + return $ruleSets; |
|
482 | + } |
|
483 | + |
|
484 | + /** |
|
485 | + * Returns whether or not a field obtains the rule "required". |
|
486 | + * |
|
487 | + * @param string $fieldName Field to check if required. |
|
488 | + * |
|
489 | + * @return boolean Whether or not the field is required. |
|
490 | + */ |
|
491 | + protected function _fieldIsRequired($fieldName) { |
|
492 | + $rules = $this->_parseRuleString($this->_rules[$fieldName]); |
|
493 | + return (in_array('required', $rules)); |
|
494 | + } |
|
495 | + |
|
496 | + /** |
|
497 | + * Takes a data input name, it's value, and the rule it's being validated against (ex: max_length[16]) |
|
498 | + * and adds an error to the errorSet if it fails validation of the rule. |
|
499 | + * |
|
500 | + * @param string $inputName Name or key of the validation data |
|
501 | + * @param string $inputVal Value of the validation data |
|
502 | + * @param string $ruleName Rule to be validated against, including args (exact_length[5]) |
|
503 | + * @return void |
|
504 | + */ |
|
505 | + protected function _validateRule($inputName, $inputVal, $ruleName) { |
|
506 | + $this->logger->debug('Rule validation of field [' .$inputName. '], value [' .$inputVal. '], rule [' .$ruleName. ']'); |
|
507 | + // Array to store args |
|
508 | + $ruleArgs = array(); |
|
509 | + |
|
510 | + // Get the rule arguments, realRule is just the base rule name |
|
511 | + // Like min_length instead of min_length[3] |
|
512 | + $realRule = preg_match('/\[(.*)\]/', $ruleName, $ruleArgs); |
|
513 | + $ruleName = preg_replace('/\[(.*)\]/', '', $ruleName); |
|
514 | 514 | |
515 | - if (method_exists($this, $this->_toCallCase($ruleName))) { |
|
516 | - $methodToCall = $this->_toCallCase($ruleName); |
|
517 | - @call_user_func(array($this, $methodToCall), $inputName, $ruleName, $ruleArgs); |
|
518 | - } |
|
519 | - |
|
520 | - return; |
|
521 | - } |
|
522 | - |
|
523 | - /** |
|
524 | - * Set error for the given field or key |
|
525 | - * |
|
526 | - * @param string $inputName the input or key name |
|
527 | - * @param string $ruleName the rule name |
|
528 | - * @param array $replacements |
|
529 | - */ |
|
530 | - protected function _setError($inputName, $ruleName, $replacements = array()) { |
|
531 | - $rulePhraseKeyParts = explode(',', $ruleName); |
|
532 | - foreach ($rulePhraseKeyParts as $rulePhraseKeyPart) { |
|
533 | - if (array_key_exists($rulePhraseKeyPart, $this->_errorsMessages)) { |
|
534 | - $rulePhrase = $this->_errorsMessages[$rulePhraseKeyPart]; |
|
535 | - } else { |
|
536 | - $rulePhrase = $rulePhrase[$rulePhraseKeyPart]; |
|
537 | - } |
|
538 | - } |
|
539 | - // Any overrides? |
|
540 | - if (array_key_exists($inputName, $this->_errorPhraseOverrides) && array_key_exists($ruleName, $this->_errorPhraseOverrides[$inputName])) { |
|
541 | - $rulePhrase = $this->_errorPhraseOverrides[$inputName][$ruleName]; |
|
542 | - } |
|
543 | - // Type cast to array in case it's a string |
|
544 | - $replacements = (array) $replacements; |
|
515 | + if (method_exists($this, $this->_toCallCase($ruleName))) { |
|
516 | + $methodToCall = $this->_toCallCase($ruleName); |
|
517 | + @call_user_func(array($this, $methodToCall), $inputName, $ruleName, $ruleArgs); |
|
518 | + } |
|
519 | + |
|
520 | + return; |
|
521 | + } |
|
522 | + |
|
523 | + /** |
|
524 | + * Set error for the given field or key |
|
525 | + * |
|
526 | + * @param string $inputName the input or key name |
|
527 | + * @param string $ruleName the rule name |
|
528 | + * @param array $replacements |
|
529 | + */ |
|
530 | + protected function _setError($inputName, $ruleName, $replacements = array()) { |
|
531 | + $rulePhraseKeyParts = explode(',', $ruleName); |
|
532 | + foreach ($rulePhraseKeyParts as $rulePhraseKeyPart) { |
|
533 | + if (array_key_exists($rulePhraseKeyPart, $this->_errorsMessages)) { |
|
534 | + $rulePhrase = $this->_errorsMessages[$rulePhraseKeyPart]; |
|
535 | + } else { |
|
536 | + $rulePhrase = $rulePhrase[$rulePhraseKeyPart]; |
|
537 | + } |
|
538 | + } |
|
539 | + // Any overrides? |
|
540 | + if (array_key_exists($inputName, $this->_errorPhraseOverrides) && array_key_exists($ruleName, $this->_errorPhraseOverrides[$inputName])) { |
|
541 | + $rulePhrase = $this->_errorPhraseOverrides[$inputName][$ruleName]; |
|
542 | + } |
|
543 | + // Type cast to array in case it's a string |
|
544 | + $replacements = (array) $replacements; |
|
545 | 545 | $replacementCount = count($replacements); |
546 | - for ($i = 1; $i <= $replacementCount; $i++) { |
|
547 | - $key = $i - 1; |
|
548 | - $rulePhrase = str_replace('%' . $i, $replacements[$key], $rulePhrase); |
|
549 | - } |
|
550 | - if (! array_key_exists($inputName, $this->_errors)) { |
|
551 | - $this->_errors[$inputName] = $rulePhrase; |
|
552 | - } |
|
553 | - } |
|
554 | - |
|
555 | - /** |
|
556 | - * Used to run a callback for the callback rule, as well as pass in a default |
|
557 | - * argument of the post value. For example the username field having a rule: |
|
558 | - * callback[userExists] will eval userExists(data[username]) - Note the use |
|
559 | - * of eval over call_user_func is in case the function is not user defined. |
|
560 | - * |
|
561 | - * @param type $inputArg |
|
562 | - * @param string $callbackFunc |
|
563 | - * |
|
564 | - * @return anything |
|
565 | - */ |
|
566 | - protected function _runCallback($inputArg, $callbackFunc) { |
|
546 | + for ($i = 1; $i <= $replacementCount; $i++) { |
|
547 | + $key = $i - 1; |
|
548 | + $rulePhrase = str_replace('%' . $i, $replacements[$key], $rulePhrase); |
|
549 | + } |
|
550 | + if (! array_key_exists($inputName, $this->_errors)) { |
|
551 | + $this->_errors[$inputName] = $rulePhrase; |
|
552 | + } |
|
553 | + } |
|
554 | + |
|
555 | + /** |
|
556 | + * Used to run a callback for the callback rule, as well as pass in a default |
|
557 | + * argument of the post value. For example the username field having a rule: |
|
558 | + * callback[userExists] will eval userExists(data[username]) - Note the use |
|
559 | + * of eval over call_user_func is in case the function is not user defined. |
|
560 | + * |
|
561 | + * @param type $inputArg |
|
562 | + * @param string $callbackFunc |
|
563 | + * |
|
564 | + * @return anything |
|
565 | + */ |
|
566 | + protected function _runCallback($inputArg, $callbackFunc) { |
|
567 | 567 | return eval('return ' . $callbackFunc . '("' . $inputArg . '");'); |
568 | - } |
|
569 | - |
|
570 | - /** |
|
571 | - * Used for applying a rule only if the empty callback evaluates to true, |
|
572 | - * for example required[funcName] - This runs funcName without passing any |
|
573 | - * arguments. |
|
574 | - * |
|
575 | - * @param string $callbackFunc |
|
576 | - * |
|
577 | - * @return anything |
|
578 | - */ |
|
579 | - protected function _runEmptyCallback($callbackFunc) { |
|
580 | - return eval('return ' . $callbackFunc . '();'); |
|
581 | - } |
|
582 | - |
|
583 | - /** |
|
584 | - * Gets a specific label of a specific field input name. |
|
585 | - * |
|
586 | - * @param string $inputName |
|
587 | - * |
|
588 | - * @return string |
|
589 | - */ |
|
590 | - protected function _getLabel($inputName) { |
|
591 | - return (array_key_exists($inputName, $this->_labels)) ? $this->_labels[$inputName] : $inputName; |
|
592 | - } |
|
568 | + } |
|
569 | + |
|
570 | + /** |
|
571 | + * Used for applying a rule only if the empty callback evaluates to true, |
|
572 | + * for example required[funcName] - This runs funcName without passing any |
|
573 | + * arguments. |
|
574 | + * |
|
575 | + * @param string $callbackFunc |
|
576 | + * |
|
577 | + * @return anything |
|
578 | + */ |
|
579 | + protected function _runEmptyCallback($callbackFunc) { |
|
580 | + return eval('return ' . $callbackFunc . '();'); |
|
581 | + } |
|
582 | + |
|
583 | + /** |
|
584 | + * Gets a specific label of a specific field input name. |
|
585 | + * |
|
586 | + * @param string $inputName |
|
587 | + * |
|
588 | + * @return string |
|
589 | + */ |
|
590 | + protected function _getLabel($inputName) { |
|
591 | + return (array_key_exists($inputName, $this->_labels)) ? $this->_labels[$inputName] : $inputName; |
|
592 | + } |
|
593 | 593 | |
594 | - /** |
|
595 | - * Peform validation for the rule "required" |
|
596 | - * @param string $inputName the form field or data key name used |
|
597 | - * @param string $ruleName the rule name for this validation ("required") |
|
598 | - * @param array $ruleArgs the rules argument |
|
599 | - */ |
|
594 | + /** |
|
595 | + * Peform validation for the rule "required" |
|
596 | + * @param string $inputName the form field or data key name used |
|
597 | + * @param string $ruleName the rule name for this validation ("required") |
|
598 | + * @param array $ruleArgs the rules argument |
|
599 | + */ |
|
600 | 600 | protected function _validateRequired($inputName, $ruleName, array $ruleArgs) { |
601 | - $inputVal = $this->post($inputName); |
|
602 | - if(array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
|
603 | - $callbackReturn = $this->_runEmptyCallback($ruleArgs[1]); |
|
604 | - if ($inputVal == '' && $callbackReturn == true) { |
|
605 | - $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
606 | - } |
|
607 | - } |
|
601 | + $inputVal = $this->post($inputName); |
|
602 | + if(array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
|
603 | + $callbackReturn = $this->_runEmptyCallback($ruleArgs[1]); |
|
604 | + if ($inputVal == '' && $callbackReturn == true) { |
|
605 | + $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
606 | + } |
|
607 | + } |
|
608 | 608 | else if($inputVal == '') { |
609 | 609 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
610 | - } |
|
611 | - } |
|
612 | - |
|
613 | - /** |
|
614 | - * Perform validation for the honey pot so means for the validation to be failed |
|
615 | - * @param string $inputName the form field or data key name used |
|
616 | - * @param string $ruleName the rule name for this validation |
|
617 | - * @param array $ruleArgs the rules argument |
|
618 | - */ |
|
619 | - protected function _validateHoneypot($inputName, $ruleName, array $ruleArgs) { |
|
620 | - if ($this->data[$inputName] != '') { |
|
621 | - $this->_forceFail = true; |
|
622 | - } |
|
623 | - } |
|
624 | - |
|
625 | - /** |
|
626 | - * Peform validation for the rule "callback" |
|
627 | - * @param string $inputName the form field or data key name used |
|
628 | - * @param string $ruleName the rule name for this validation ("callback") |
|
629 | - * @param array $ruleArgs the rules argument |
|
630 | - */ |
|
631 | - protected function _validateCallback($inputName, $ruleName, array $ruleArgs) { |
|
632 | - if (function_exists($ruleArgs[1]) && !empty($this->data[$inputName])) { |
|
610 | + } |
|
611 | + } |
|
612 | + |
|
613 | + /** |
|
614 | + * Perform validation for the honey pot so means for the validation to be failed |
|
615 | + * @param string $inputName the form field or data key name used |
|
616 | + * @param string $ruleName the rule name for this validation |
|
617 | + * @param array $ruleArgs the rules argument |
|
618 | + */ |
|
619 | + protected function _validateHoneypot($inputName, $ruleName, array $ruleArgs) { |
|
620 | + if ($this->data[$inputName] != '') { |
|
621 | + $this->_forceFail = true; |
|
622 | + } |
|
623 | + } |
|
624 | + |
|
625 | + /** |
|
626 | + * Peform validation for the rule "callback" |
|
627 | + * @param string $inputName the form field or data key name used |
|
628 | + * @param string $ruleName the rule name for this validation ("callback") |
|
629 | + * @param array $ruleArgs the rules argument |
|
630 | + */ |
|
631 | + protected function _validateCallback($inputName, $ruleName, array $ruleArgs) { |
|
632 | + if (function_exists($ruleArgs[1]) && !empty($this->data[$inputName])) { |
|
633 | 633 | $result = $this->_runCallback($this->data[$inputName], $ruleArgs[1]); |
634 | 634 | if(! $result){ |
635 | 635 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
636 | 636 | } |
637 | - } |
|
638 | - } |
|
639 | - |
|
640 | - /** |
|
641 | - * Peform validation for the rule "depends" |
|
642 | - * @param string $inputName the form field or data key name used |
|
643 | - * @param string $ruleName the rule name for this validation ("depends") |
|
644 | - * @param array $ruleArgs the rules argument |
|
645 | - */ |
|
646 | - protected function _validateDepends($inputName, $ruleName, array $ruleArgs) { |
|
647 | - if (array_key_exists($ruleArgs[1], $this->_errors)) { |
|
648 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
649 | - } |
|
650 | - } |
|
651 | - |
|
652 | - /** |
|
653 | - * Peform validation for the rule "not_equal" |
|
654 | - * @param string $inputName the form field or data key name used |
|
655 | - * @param string $ruleName the rule name for this validation ("not_equal") |
|
656 | - * @param array $ruleArgs the rules argument |
|
657 | - */ |
|
658 | - protected function _validateNotEqual($inputName, $ruleName, array $ruleArgs) { |
|
659 | - $canNotEqual = explode(',', $ruleArgs[1]); |
|
660 | - foreach ($canNotEqual as $doNotEqual) { |
|
661 | - $inputVal = $this->post($inputName); |
|
662 | - if (preg_match('/post:(.*)/', $doNotEqual)) { |
|
663 | - if ($inputVal == $this->data[str_replace('post:', '', $doNotEqual)]) { |
|
664 | - $this->_setError($inputName, $ruleName . ',post:key', array($this->_getLabel($inputName), $this->_getLabel(str_replace('post:', '', $doNotEqual)))); |
|
665 | - continue; |
|
666 | - } |
|
667 | - } |
|
637 | + } |
|
638 | + } |
|
639 | + |
|
640 | + /** |
|
641 | + * Peform validation for the rule "depends" |
|
642 | + * @param string $inputName the form field or data key name used |
|
643 | + * @param string $ruleName the rule name for this validation ("depends") |
|
644 | + * @param array $ruleArgs the rules argument |
|
645 | + */ |
|
646 | + protected function _validateDepends($inputName, $ruleName, array $ruleArgs) { |
|
647 | + if (array_key_exists($ruleArgs[1], $this->_errors)) { |
|
648 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
649 | + } |
|
650 | + } |
|
651 | + |
|
652 | + /** |
|
653 | + * Peform validation for the rule "not_equal" |
|
654 | + * @param string $inputName the form field or data key name used |
|
655 | + * @param string $ruleName the rule name for this validation ("not_equal") |
|
656 | + * @param array $ruleArgs the rules argument |
|
657 | + */ |
|
658 | + protected function _validateNotEqual($inputName, $ruleName, array $ruleArgs) { |
|
659 | + $canNotEqual = explode(',', $ruleArgs[1]); |
|
660 | + foreach ($canNotEqual as $doNotEqual) { |
|
661 | + $inputVal = $this->post($inputName); |
|
662 | + if (preg_match('/post:(.*)/', $doNotEqual)) { |
|
663 | + if ($inputVal == $this->data[str_replace('post:', '', $doNotEqual)]) { |
|
664 | + $this->_setError($inputName, $ruleName . ',post:key', array($this->_getLabel($inputName), $this->_getLabel(str_replace('post:', '', $doNotEqual)))); |
|
665 | + continue; |
|
666 | + } |
|
667 | + } |
|
668 | 668 | else{ |
669 | - if ($inputVal == $doNotEqual) { |
|
670 | - $this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
|
671 | - continue; |
|
672 | - } |
|
673 | - } |
|
674 | - } |
|
675 | - } |
|
676 | - |
|
677 | - /** |
|
678 | - * Peform validation for the rule "matches" |
|
679 | - * @param string $inputName the form field or data key name used |
|
680 | - * @param string $ruleName the rule name for this validation ("matches") |
|
681 | - * @param array $ruleArgs the rules argument |
|
682 | - */ |
|
683 | - protected function _validateMatches($inputName, $ruleName, array $ruleArgs) { |
|
684 | - $inputVal = $this->post($inputName); |
|
685 | - if ($inputVal != $this->data[$ruleArgs[1]]) { |
|
686 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
687 | - } |
|
688 | - } |
|
689 | - |
|
690 | - /** |
|
691 | - * Peform validation for the rule "valid_email" |
|
692 | - * @param string $inputName the form field or data key name used |
|
693 | - * @param string $ruleName the rule name for this validation ("valid_email") |
|
694 | - * @param array $ruleArgs the rules argument |
|
695 | - */ |
|
696 | - protected function _validateValidEmail($inputName, $ruleName, array $ruleArgs) { |
|
697 | - $inputVal = $this->post($inputName); |
|
698 | - if (! preg_match("/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i", $inputVal)) { |
|
699 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
700 | - return; |
|
701 | - } |
|
702 | - $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
703 | - } |
|
704 | - } |
|
705 | - |
|
706 | - /** |
|
707 | - * Peform validation for the rule "exact_length" |
|
708 | - * @param string $inputName the form field or data key name used |
|
709 | - * @param string $ruleName the rule name for this validation ("exact_length") |
|
710 | - * @param array $ruleArgs the rules argument |
|
711 | - */ |
|
712 | - protected function _validateExactLength($inputName, $ruleName, array $ruleArgs) { |
|
713 | - $inputVal = $this->post($inputName); |
|
714 | - if (strlen($inputVal) != $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
715 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
716 | - return; |
|
717 | - } |
|
718 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
719 | - } |
|
720 | - } |
|
721 | - |
|
722 | - /** |
|
723 | - * Peform validation for the rule "max_length" |
|
724 | - * @param string $inputName the form field or data key name used |
|
725 | - * @param string $ruleName the rule name for this validation ("max_length") |
|
726 | - * @param array $ruleArgs the rules argument |
|
727 | - */ |
|
728 | - protected function _validateMaxLength($inputName, $ruleName, array $ruleArgs) { |
|
729 | - $inputVal = $this->post($inputName); |
|
730 | - if (strlen($inputVal) > $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
731 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
732 | - return; |
|
733 | - } |
|
734 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
735 | - } |
|
736 | - } |
|
737 | - |
|
738 | - /** |
|
739 | - * Peform validation for the rule "min_length" |
|
740 | - * @param string $inputName the form field or data key name used |
|
741 | - * @param string $ruleName the rule name for this validation ("min_length") |
|
742 | - * @param array $ruleArgs the rules argument |
|
743 | - */ |
|
744 | - protected function _validateMinLength($inputName, $ruleName, array $ruleArgs) { |
|
745 | - $inputVal = $this->post($inputName); |
|
746 | - if (strlen($inputVal) < $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
747 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
748 | - return; |
|
749 | - } |
|
750 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
751 | - } |
|
752 | - } |
|
669 | + if ($inputVal == $doNotEqual) { |
|
670 | + $this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
|
671 | + continue; |
|
672 | + } |
|
673 | + } |
|
674 | + } |
|
675 | + } |
|
676 | + |
|
677 | + /** |
|
678 | + * Peform validation for the rule "matches" |
|
679 | + * @param string $inputName the form field or data key name used |
|
680 | + * @param string $ruleName the rule name for this validation ("matches") |
|
681 | + * @param array $ruleArgs the rules argument |
|
682 | + */ |
|
683 | + protected function _validateMatches($inputName, $ruleName, array $ruleArgs) { |
|
684 | + $inputVal = $this->post($inputName); |
|
685 | + if ($inputVal != $this->data[$ruleArgs[1]]) { |
|
686 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
687 | + } |
|
688 | + } |
|
689 | + |
|
690 | + /** |
|
691 | + * Peform validation for the rule "valid_email" |
|
692 | + * @param string $inputName the form field or data key name used |
|
693 | + * @param string $ruleName the rule name for this validation ("valid_email") |
|
694 | + * @param array $ruleArgs the rules argument |
|
695 | + */ |
|
696 | + protected function _validateValidEmail($inputName, $ruleName, array $ruleArgs) { |
|
697 | + $inputVal = $this->post($inputName); |
|
698 | + if (! preg_match("/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i", $inputVal)) { |
|
699 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
700 | + return; |
|
701 | + } |
|
702 | + $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
703 | + } |
|
704 | + } |
|
705 | + |
|
706 | + /** |
|
707 | + * Peform validation for the rule "exact_length" |
|
708 | + * @param string $inputName the form field or data key name used |
|
709 | + * @param string $ruleName the rule name for this validation ("exact_length") |
|
710 | + * @param array $ruleArgs the rules argument |
|
711 | + */ |
|
712 | + protected function _validateExactLength($inputName, $ruleName, array $ruleArgs) { |
|
713 | + $inputVal = $this->post($inputName); |
|
714 | + if (strlen($inputVal) != $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
715 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
716 | + return; |
|
717 | + } |
|
718 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
719 | + } |
|
720 | + } |
|
721 | + |
|
722 | + /** |
|
723 | + * Peform validation for the rule "max_length" |
|
724 | + * @param string $inputName the form field or data key name used |
|
725 | + * @param string $ruleName the rule name for this validation ("max_length") |
|
726 | + * @param array $ruleArgs the rules argument |
|
727 | + */ |
|
728 | + protected function _validateMaxLength($inputName, $ruleName, array $ruleArgs) { |
|
729 | + $inputVal = $this->post($inputName); |
|
730 | + if (strlen($inputVal) > $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
731 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
732 | + return; |
|
733 | + } |
|
734 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
735 | + } |
|
736 | + } |
|
737 | + |
|
738 | + /** |
|
739 | + * Peform validation for the rule "min_length" |
|
740 | + * @param string $inputName the form field or data key name used |
|
741 | + * @param string $ruleName the rule name for this validation ("min_length") |
|
742 | + * @param array $ruleArgs the rules argument |
|
743 | + */ |
|
744 | + protected function _validateMinLength($inputName, $ruleName, array $ruleArgs) { |
|
745 | + $inputVal = $this->post($inputName); |
|
746 | + if (strlen($inputVal) < $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
747 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
748 | + return; |
|
749 | + } |
|
750 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
751 | + } |
|
752 | + } |
|
753 | 753 | |
754 | - /** |
|
755 | - * Peform validation for the rule "less_than" |
|
756 | - * @param string $inputName the form field or data key name used |
|
757 | - * @param string $ruleName the rule name for this validation ("less_than") |
|
758 | - * @param array $ruleArgs the rules argument |
|
759 | - */ |
|
760 | - protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
|
761 | - $inputVal = $this->post($inputName); |
|
762 | - if ($inputVal >= $ruleArgs[1]) { |
|
763 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
764 | - return; |
|
765 | - } |
|
766 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
767 | - } |
|
768 | - } |
|
754 | + /** |
|
755 | + * Peform validation for the rule "less_than" |
|
756 | + * @param string $inputName the form field or data key name used |
|
757 | + * @param string $ruleName the rule name for this validation ("less_than") |
|
758 | + * @param array $ruleArgs the rules argument |
|
759 | + */ |
|
760 | + protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
|
761 | + $inputVal = $this->post($inputName); |
|
762 | + if ($inputVal >= $ruleArgs[1]) { |
|
763 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
764 | + return; |
|
765 | + } |
|
766 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
767 | + } |
|
768 | + } |
|
769 | 769 | |
770 | - /** |
|
771 | - * Peform validation for the rule "greater_than" |
|
772 | - * @param string $inputName the form field or data key name used |
|
773 | - * @param string $ruleName the rule name for this validation ("greater_than") |
|
774 | - * @param array $ruleArgs the rules argument |
|
775 | - */ |
|
776 | - protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
|
777 | - $inputVal = $this->post($inputName); |
|
778 | - if ($inputVal <= $ruleArgs[1]) { |
|
779 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
780 | - return; |
|
781 | - } |
|
782 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
783 | - } |
|
784 | - } |
|
770 | + /** |
|
771 | + * Peform validation for the rule "greater_than" |
|
772 | + * @param string $inputName the form field or data key name used |
|
773 | + * @param string $ruleName the rule name for this validation ("greater_than") |
|
774 | + * @param array $ruleArgs the rules argument |
|
775 | + */ |
|
776 | + protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
|
777 | + $inputVal = $this->post($inputName); |
|
778 | + if ($inputVal <= $ruleArgs[1]) { |
|
779 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
780 | + return; |
|
781 | + } |
|
782 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
783 | + } |
|
784 | + } |
|
785 | 785 | |
786 | - /** |
|
787 | - * Peform validation for the rule "numeric" |
|
788 | - * @param string $inputName the form field or data key name used |
|
789 | - * @param string $ruleName the rule name for this validation ("numeric") |
|
790 | - * @param array $ruleArgs the rules argument |
|
791 | - */ |
|
792 | - protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
|
793 | - $inputVal = $this->post($inputName); |
|
794 | - if (! is_numeric($inputVal)) { |
|
795 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
796 | - return; |
|
797 | - } |
|
798 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
799 | - } |
|
800 | - } |
|
786 | + /** |
|
787 | + * Peform validation for the rule "numeric" |
|
788 | + * @param string $inputName the form field or data key name used |
|
789 | + * @param string $ruleName the rule name for this validation ("numeric") |
|
790 | + * @param array $ruleArgs the rules argument |
|
791 | + */ |
|
792 | + protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
|
793 | + $inputVal = $this->post($inputName); |
|
794 | + if (! is_numeric($inputVal)) { |
|
795 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
796 | + return; |
|
797 | + } |
|
798 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
799 | + } |
|
800 | + } |
|
801 | 801 | |
802 | - /** |
|
803 | - * Peform validation for the rule "exists" |
|
804 | - * @param string $inputName the form field or data key name used |
|
805 | - * @param string $ruleName the rule name for this validation ("exists") |
|
806 | - * @param array $ruleArgs the rules argument |
|
807 | - */ |
|
802 | + /** |
|
803 | + * Peform validation for the rule "exists" |
|
804 | + * @param string $inputName the form field or data key name used |
|
805 | + * @param string $ruleName the rule name for this validation ("exists") |
|
806 | + * @param array $ruleArgs the rules argument |
|
807 | + */ |
|
808 | 808 | protected function _validateExists($inputName, $ruleName, array $ruleArgs) { |
809 | - $inputVal = $this->post($inputName); |
|
810 | - $obj = & get_instance(); |
|
811 | - if(! isset($obj->database)){ |
|
812 | - return; |
|
813 | - } |
|
814 | - list($table, $column) = explode('.', $ruleArgs[1]); |
|
815 | - $obj->database->from($table) |
|
816 | - ->where($column, $inputVal) |
|
817 | - ->get(); |
|
818 | - $nb = $obj->database->numRows(); |
|
819 | - if ($nb == 0) { |
|
820 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
821 | - return; |
|
822 | - } |
|
823 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
824 | - } |
|
825 | - } |
|
826 | - |
|
827 | - /** |
|
828 | - * Peform validation for the rule "is_unique" |
|
829 | - * @param string $inputName the form field or data key name used |
|
830 | - * @param string $ruleName the rule name for this validation ("is_unique") |
|
831 | - * @param array $ruleArgs the rules argument |
|
832 | - */ |
|
833 | - protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
|
834 | - $inputVal = $this->post($inputName); |
|
835 | - $obj = & get_instance(); |
|
836 | - if(! isset($obj->database)){ |
|
837 | - return; |
|
838 | - } |
|
839 | - list($table, $column) = explode('.', $ruleArgs[1]); |
|
840 | - $obj->database->from($table) |
|
841 | - ->where($column, $inputVal) |
|
842 | - ->get(); |
|
843 | - $nb = $obj->database->numRows(); |
|
844 | - if ($nb != 0) { |
|
845 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
846 | - return; |
|
847 | - } |
|
848 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
849 | - } |
|
850 | - } |
|
809 | + $inputVal = $this->post($inputName); |
|
810 | + $obj = & get_instance(); |
|
811 | + if(! isset($obj->database)){ |
|
812 | + return; |
|
813 | + } |
|
814 | + list($table, $column) = explode('.', $ruleArgs[1]); |
|
815 | + $obj->database->from($table) |
|
816 | + ->where($column, $inputVal) |
|
817 | + ->get(); |
|
818 | + $nb = $obj->database->numRows(); |
|
819 | + if ($nb == 0) { |
|
820 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
821 | + return; |
|
822 | + } |
|
823 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
824 | + } |
|
825 | + } |
|
826 | + |
|
827 | + /** |
|
828 | + * Peform validation for the rule "is_unique" |
|
829 | + * @param string $inputName the form field or data key name used |
|
830 | + * @param string $ruleName the rule name for this validation ("is_unique") |
|
831 | + * @param array $ruleArgs the rules argument |
|
832 | + */ |
|
833 | + protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
|
834 | + $inputVal = $this->post($inputName); |
|
835 | + $obj = & get_instance(); |
|
836 | + if(! isset($obj->database)){ |
|
837 | + return; |
|
838 | + } |
|
839 | + list($table, $column) = explode('.', $ruleArgs[1]); |
|
840 | + $obj->database->from($table) |
|
841 | + ->where($column, $inputVal) |
|
842 | + ->get(); |
|
843 | + $nb = $obj->database->numRows(); |
|
844 | + if ($nb != 0) { |
|
845 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
846 | + return; |
|
847 | + } |
|
848 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
849 | + } |
|
850 | + } |
|
851 | 851 | |
852 | - /** |
|
853 | - * Peform validation for the rule "is_unique_update" |
|
854 | - * @param string $inputName the form field or data key name used |
|
855 | - * @param string $ruleName the rule name for this validation ("is_unique_update") |
|
856 | - * @param array $ruleArgs the rules argument |
|
857 | - */ |
|
858 | - protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
|
859 | - $inputVal = $this->post($inputName); |
|
860 | - $obj = & get_instance(); |
|
861 | - if(! isset($obj->database)){ |
|
862 | - return; |
|
863 | - } |
|
864 | - $data = explode(',', $ruleArgs[1]); |
|
865 | - if(count($data) < 2){ |
|
866 | - return; |
|
867 | - } |
|
868 | - list($table, $column) = explode('.', $data[0]); |
|
869 | - list($field, $val) = explode('=', $data[1]); |
|
870 | - $obj->database->from($table) |
|
871 | - ->where($column, $inputVal) |
|
872 | - ->where($field, '!=', trim($val)) |
|
873 | - ->get(); |
|
874 | - $nb = $obj->database->numRows(); |
|
875 | - if ($nb != 0) { |
|
876 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
877 | - return; |
|
878 | - } |
|
879 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
880 | - } |
|
881 | - } |
|
882 | - |
|
883 | - /** |
|
884 | - * Peform validation for the rule "in_list" |
|
885 | - * @param string $inputName the form field or data key name used |
|
886 | - * @param string $ruleName the rule name for this validation ("in_list") |
|
887 | - * @param array $ruleArgs the rules argument |
|
888 | - */ |
|
889 | - protected function _validateInList($inputName, $ruleName, array $ruleArgs) { |
|
890 | - $inputVal = $this->post($inputName); |
|
891 | - $list = explode(',', $ruleArgs[1]); |
|
892 | - $list = array_map('trim', $list); |
|
893 | - if (! in_array($inputVal, $list)) { |
|
894 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
895 | - return; |
|
896 | - } |
|
897 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
898 | - } |
|
899 | - } |
|
900 | - |
|
901 | - /** |
|
902 | - * Peform validation for the rule "regex" |
|
903 | - * @param string $inputName the form field or data key name used |
|
904 | - * @param string $ruleName the rule name for this validation ("regex") |
|
905 | - * @param array $ruleArgs the rules argument |
|
906 | - */ |
|
907 | - protected function _validateRegex($inputName, $ruleName, array $ruleArgs) { |
|
908 | - $inputVal = $this->post($inputName); |
|
909 | - $regex = $ruleArgs[1]; |
|
910 | - if (! preg_match($regex, $inputVal)) { |
|
911 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
912 | - return; |
|
913 | - } |
|
914 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
915 | - } |
|
916 | - } |
|
852 | + /** |
|
853 | + * Peform validation for the rule "is_unique_update" |
|
854 | + * @param string $inputName the form field or data key name used |
|
855 | + * @param string $ruleName the rule name for this validation ("is_unique_update") |
|
856 | + * @param array $ruleArgs the rules argument |
|
857 | + */ |
|
858 | + protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
|
859 | + $inputVal = $this->post($inputName); |
|
860 | + $obj = & get_instance(); |
|
861 | + if(! isset($obj->database)){ |
|
862 | + return; |
|
863 | + } |
|
864 | + $data = explode(',', $ruleArgs[1]); |
|
865 | + if(count($data) < 2){ |
|
866 | + return; |
|
867 | + } |
|
868 | + list($table, $column) = explode('.', $data[0]); |
|
869 | + list($field, $val) = explode('=', $data[1]); |
|
870 | + $obj->database->from($table) |
|
871 | + ->where($column, $inputVal) |
|
872 | + ->where($field, '!=', trim($val)) |
|
873 | + ->get(); |
|
874 | + $nb = $obj->database->numRows(); |
|
875 | + if ($nb != 0) { |
|
876 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
877 | + return; |
|
878 | + } |
|
879 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
880 | + } |
|
881 | + } |
|
882 | + |
|
883 | + /** |
|
884 | + * Peform validation for the rule "in_list" |
|
885 | + * @param string $inputName the form field or data key name used |
|
886 | + * @param string $ruleName the rule name for this validation ("in_list") |
|
887 | + * @param array $ruleArgs the rules argument |
|
888 | + */ |
|
889 | + protected function _validateInList($inputName, $ruleName, array $ruleArgs) { |
|
890 | + $inputVal = $this->post($inputName); |
|
891 | + $list = explode(',', $ruleArgs[1]); |
|
892 | + $list = array_map('trim', $list); |
|
893 | + if (! in_array($inputVal, $list)) { |
|
894 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
895 | + return; |
|
896 | + } |
|
897 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
898 | + } |
|
899 | + } |
|
900 | + |
|
901 | + /** |
|
902 | + * Peform validation for the rule "regex" |
|
903 | + * @param string $inputName the form field or data key name used |
|
904 | + * @param string $ruleName the rule name for this validation ("regex") |
|
905 | + * @param array $ruleArgs the rules argument |
|
906 | + */ |
|
907 | + protected function _validateRegex($inputName, $ruleName, array $ruleArgs) { |
|
908 | + $inputVal = $this->post($inputName); |
|
909 | + $regex = $ruleArgs[1]; |
|
910 | + if (! preg_match($regex, $inputVal)) { |
|
911 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
912 | + return; |
|
913 | + } |
|
914 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
915 | + } |
|
916 | + } |
|
917 | 917 | |
918 | - } |
|
918 | + } |
@@ -25,13 +25,13 @@ discard block |
||
25 | 25 | */ |
26 | 26 | |
27 | 27 | |
28 | - class FormValidation{ |
|
28 | + class FormValidation { |
|
29 | 29 | |
30 | 30 | /** |
31 | 31 | * The form validation status |
32 | 32 | * @var boolean |
33 | 33 | */ |
34 | - protected $_success = false; |
|
34 | + protected $_success = false; |
|
35 | 35 | |
36 | 36 | /** |
37 | 37 | * The list of errors messages |
@@ -40,31 +40,31 @@ discard block |
||
40 | 40 | protected $_errorsMessages = array(); |
41 | 41 | |
42 | 42 | // Array of rule sets, fieldName => PIPE seperated ruleString |
43 | - protected $_rules = array(); |
|
43 | + protected $_rules = array(); |
|
44 | 44 | |
45 | 45 | // Array of errors, niceName => Error Message |
46 | - protected $_errors = array(); |
|
46 | + protected $_errors = array(); |
|
47 | 47 | |
48 | 48 | // Array of post Key => Nice name labels |
49 | - protected $_labels = array(); |
|
49 | + protected $_labels = array(); |
|
50 | 50 | |
51 | 51 | /** |
52 | 52 | * The errors delimiters |
53 | 53 | * @var array |
54 | 54 | */ |
55 | - protected $_allErrorsDelimiter = array('<div class="error">', '</div>'); |
|
55 | + protected $_allErrorsDelimiter = array('<div class="error">', '</div>'); |
|
56 | 56 | |
57 | 57 | /** |
58 | 58 | * The each error delimiter |
59 | 59 | * @var array |
60 | 60 | */ |
61 | - protected $_eachErrorDelimiter = array('<p class="error">', '</p>'); |
|
61 | + protected $_eachErrorDelimiter = array('<p class="error">', '</p>'); |
|
62 | 62 | |
63 | 63 | /** |
64 | 64 | * Indicated if need force the validation to be failed |
65 | 65 | * @var boolean |
66 | 66 | */ |
67 | - protected $_forceFail = false; |
|
67 | + protected $_forceFail = false; |
|
68 | 68 | |
69 | 69 | /** |
70 | 70 | * The list of the error messages overrides by the original |
@@ -98,13 +98,13 @@ discard block |
||
98 | 98 | * @return void |
99 | 99 | */ |
100 | 100 | public function __construct() { |
101 | - $this->logger =& class_loader('Log', 'classes'); |
|
101 | + $this->logger = & class_loader('Log', 'classes'); |
|
102 | 102 | $this->logger->setLogger('Library::FormValidation'); |
103 | 103 | |
104 | 104 | //Load form validation language message |
105 | 105 | Loader::lang('form_validation'); |
106 | 106 | $obj = & get_instance(); |
107 | - $this->_errorsMessages = array( |
|
107 | + $this->_errorsMessages = array( |
|
108 | 108 | 'required' => $obj->lang->get('fv_required'), |
109 | 109 | 'min_length' => $obj->lang->get('fv_min_length'), |
110 | 110 | 'max_length' => $obj->lang->get('fv_max_length'), |
@@ -141,7 +141,7 @@ discard block |
||
141 | 141 | $this->_success = false; |
142 | 142 | $this->_forceFail = false; |
143 | 143 | $this->data = array(); |
144 | - $this->enableCsrfCheck = false; |
|
144 | + $this->enableCsrfCheck = false; |
|
145 | 145 | } |
146 | 146 | |
147 | 147 | /** |
@@ -150,7 +150,7 @@ discard block |
||
150 | 150 | * |
151 | 151 | * @return FormValidation Current instance of object. |
152 | 152 | */ |
153 | - public function setData(array $data){ |
|
153 | + public function setData(array $data) { |
|
154 | 154 | $this->logger->debug('Setting the form validation data, the values are: ' . stringfy_vars($data)); |
155 | 155 | $this->data = $data; |
156 | 156 | return $this; |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | * Get the form validation data |
161 | 161 | * @return array the form validation data to be validated |
162 | 162 | */ |
163 | - public function getData(){ |
|
163 | + public function getData() { |
|
164 | 164 | return $this->data; |
165 | 165 | } |
166 | 166 | |
@@ -169,7 +169,7 @@ discard block |
||
169 | 169 | * |
170 | 170 | * @return string the function name |
171 | 171 | */ |
172 | - protected function _toCallCase($funcName, $prefix='_validate') { |
|
172 | + protected function _toCallCase($funcName, $prefix = '_validate') { |
|
173 | 173 | $funcName = strtolower($funcName); |
174 | 174 | $finalFuncName = $prefix; |
175 | 175 | foreach (explode('_', $funcName) as $funcNamePart) { |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | * @return boolean Whether or not the form has been submitted or the data is available for validation. |
194 | 194 | */ |
195 | 195 | public function canDoValidation() { |
196 | - return get_instance()->request->method() === 'POST' || ! empty($this->data); |
|
196 | + return get_instance()->request->method() === 'POST' || !empty($this->data); |
|
197 | 197 | } |
198 | 198 | |
199 | 199 | /** |
@@ -215,16 +215,16 @@ discard block |
||
215 | 215 | * afterwards. |
216 | 216 | */ |
217 | 217 | protected function _run() { |
218 | - if(get_instance()->request->method() == 'POST' || $this->enableCsrfCheck){ |
|
218 | + if (get_instance()->request->method() == 'POST' || $this->enableCsrfCheck) { |
|
219 | 219 | $this->logger->debug('Check if CSRF is enabled in configuration'); |
220 | 220 | //first check for CSRF |
221 | - if( get_config('csrf_enable', false) || $this->enableCsrfCheck){ |
|
221 | + if (get_config('csrf_enable', false) || $this->enableCsrfCheck) { |
|
222 | 222 | $this->logger->info('Check the CSRF value if is valid'); |
223 | - if(! Security::validateCSRF()){ |
|
223 | + if (!Security::validateCSRF()) { |
|
224 | 224 | show_error('Invalide data, Cross Site Request Forgery do his job, the data to validate is corrupted.'); |
225 | 225 | } |
226 | 226 | } |
227 | - else{ |
|
227 | + else { |
|
228 | 228 | $this->logger->info('CSRF is not enabled in configuration or not set manully, no need to check it'); |
229 | 229 | } |
230 | 230 | } |
@@ -232,10 +232,10 @@ discard block |
||
232 | 232 | $this->_forceFail = false; |
233 | 233 | |
234 | 234 | foreach ($this->getData() as $inputName => $inputVal) { |
235 | - if(is_array($this->data[$inputName])){ |
|
235 | + if (is_array($this->data[$inputName])) { |
|
236 | 236 | $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
237 | 237 | } |
238 | - else{ |
|
238 | + else { |
|
239 | 239 | $this->data[$inputName] = trim($this->data[$inputName]); |
240 | 240 | } |
241 | 241 | |
@@ -262,7 +262,7 @@ discard block |
||
262 | 262 | public function setRule($inputField, $inputLabel, $ruleSets) { |
263 | 263 | $this->_rules[$inputField] = $ruleSets; |
264 | 264 | $this->_labels[$inputField] = $inputLabel; |
265 | - $this->logger->info('Set the field rule: name [' .$inputField. '], label [' .$inputLabel. '], rules [' .$ruleSets. ']'); |
|
265 | + $this->logger->info('Set the field rule: name [' . $inputField . '], label [' . $inputLabel . '], rules [' . $ruleSets . ']'); |
|
266 | 266 | return $this; |
267 | 267 | } |
268 | 268 | |
@@ -427,7 +427,7 @@ discard block |
||
427 | 427 | } |
428 | 428 | $errorOutput .= $errorsEnd; |
429 | 429 | echo ($echo) ? $errorOutput : ''; |
430 | - return (! $echo) ? $errorOutput : null; |
|
430 | + return (!$echo) ? $errorOutput : null; |
|
431 | 431 | } |
432 | 432 | |
433 | 433 | /** |
@@ -452,26 +452,26 @@ discard block |
||
452 | 452 | /* |
453 | 453 | //////////////// hack for regex rule that can contain "|" |
454 | 454 | */ |
455 | - if(strpos($ruleString, 'regex') !== false){ |
|
455 | + if (strpos($ruleString, 'regex') !== false) { |
|
456 | 456 | $regexRule = array(); |
457 | 457 | $rule = '#regex\[\/(.*)\/([a-zA-Z0-9]?)\]#'; |
458 | 458 | preg_match($rule, $ruleString, $regexRule); |
459 | 459 | $ruleStringTemp = preg_replace($rule, '', $ruleString); |
460 | - if(isset($regexRule[0]) && !empty($regexRule[0])){ |
|
460 | + if (isset($regexRule[0]) && !empty($regexRule[0])) { |
|
461 | 461 | $ruleSets[] = $regexRule[0]; |
462 | 462 | } |
463 | 463 | $ruleStringRegex = explode('|', $ruleStringTemp); |
464 | - if(is_array($ruleStringRegex)){ |
|
464 | + if (is_array($ruleStringRegex)) { |
|
465 | 465 | foreach ($ruleStringRegex as $rule) { |
466 | 466 | $rule = trim($rule); |
467 | - if($rule){ |
|
467 | + if ($rule) { |
|
468 | 468 | $ruleSets[] = $rule; |
469 | 469 | } |
470 | 470 | } |
471 | 471 | } |
472 | 472 | } |
473 | 473 | /***********************************/ |
474 | - else{ |
|
474 | + else { |
|
475 | 475 | if (strpos($ruleString, '|') !== FALSE) { |
476 | 476 | $ruleSets = explode('|', $ruleString); |
477 | 477 | } else { |
@@ -503,7 +503,7 @@ discard block |
||
503 | 503 | * @return void |
504 | 504 | */ |
505 | 505 | protected function _validateRule($inputName, $inputVal, $ruleName) { |
506 | - $this->logger->debug('Rule validation of field [' .$inputName. '], value [' .$inputVal. '], rule [' .$ruleName. ']'); |
|
506 | + $this->logger->debug('Rule validation of field [' . $inputName . '], value [' . $inputVal . '], rule [' . $ruleName . ']'); |
|
507 | 507 | // Array to store args |
508 | 508 | $ruleArgs = array(); |
509 | 509 | |
@@ -547,7 +547,7 @@ discard block |
||
547 | 547 | $key = $i - 1; |
548 | 548 | $rulePhrase = str_replace('%' . $i, $replacements[$key], $rulePhrase); |
549 | 549 | } |
550 | - if (! array_key_exists($inputName, $this->_errors)) { |
|
550 | + if (!array_key_exists($inputName, $this->_errors)) { |
|
551 | 551 | $this->_errors[$inputName] = $rulePhrase; |
552 | 552 | } |
553 | 553 | } |
@@ -599,13 +599,13 @@ discard block |
||
599 | 599 | */ |
600 | 600 | protected function _validateRequired($inputName, $ruleName, array $ruleArgs) { |
601 | 601 | $inputVal = $this->post($inputName); |
602 | - if(array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
|
602 | + if (array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
|
603 | 603 | $callbackReturn = $this->_runEmptyCallback($ruleArgs[1]); |
604 | 604 | if ($inputVal == '' && $callbackReturn == true) { |
605 | 605 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
606 | 606 | } |
607 | 607 | } |
608 | - else if($inputVal == '') { |
|
608 | + else if ($inputVal == '') { |
|
609 | 609 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
610 | 610 | } |
611 | 611 | } |
@@ -631,7 +631,7 @@ discard block |
||
631 | 631 | protected function _validateCallback($inputName, $ruleName, array $ruleArgs) { |
632 | 632 | if (function_exists($ruleArgs[1]) && !empty($this->data[$inputName])) { |
633 | 633 | $result = $this->_runCallback($this->data[$inputName], $ruleArgs[1]); |
634 | - if(! $result){ |
|
634 | + if (!$result) { |
|
635 | 635 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
636 | 636 | } |
637 | 637 | } |
@@ -665,7 +665,7 @@ discard block |
||
665 | 665 | continue; |
666 | 666 | } |
667 | 667 | } |
668 | - else{ |
|
668 | + else { |
|
669 | 669 | if ($inputVal == $doNotEqual) { |
670 | 670 | $this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
671 | 671 | continue; |
@@ -695,8 +695,8 @@ discard block |
||
695 | 695 | */ |
696 | 696 | protected function _validateValidEmail($inputName, $ruleName, array $ruleArgs) { |
697 | 697 | $inputVal = $this->post($inputName); |
698 | - if (! preg_match("/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i", $inputVal)) { |
|
699 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
698 | + if (!preg_match("/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i", $inputVal)) { |
|
699 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
700 | 700 | return; |
701 | 701 | } |
702 | 702 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
@@ -712,7 +712,7 @@ discard block |
||
712 | 712 | protected function _validateExactLength($inputName, $ruleName, array $ruleArgs) { |
713 | 713 | $inputVal = $this->post($inputName); |
714 | 714 | if (strlen($inputVal) != $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
715 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
715 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
716 | 716 | return; |
717 | 717 | } |
718 | 718 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -728,7 +728,7 @@ discard block |
||
728 | 728 | protected function _validateMaxLength($inputName, $ruleName, array $ruleArgs) { |
729 | 729 | $inputVal = $this->post($inputName); |
730 | 730 | if (strlen($inputVal) > $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
731 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
731 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
732 | 732 | return; |
733 | 733 | } |
734 | 734 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -744,7 +744,7 @@ discard block |
||
744 | 744 | protected function _validateMinLength($inputName, $ruleName, array $ruleArgs) { |
745 | 745 | $inputVal = $this->post($inputName); |
746 | 746 | if (strlen($inputVal) < $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
747 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
747 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
748 | 748 | return; |
749 | 749 | } |
750 | 750 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -760,7 +760,7 @@ discard block |
||
760 | 760 | protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
761 | 761 | $inputVal = $this->post($inputName); |
762 | 762 | if ($inputVal >= $ruleArgs[1]) { |
763 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
763 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
764 | 764 | return; |
765 | 765 | } |
766 | 766 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -776,7 +776,7 @@ discard block |
||
776 | 776 | protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
777 | 777 | $inputVal = $this->post($inputName); |
778 | 778 | if ($inputVal <= $ruleArgs[1]) { |
779 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
779 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
780 | 780 | return; |
781 | 781 | } |
782 | 782 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -791,8 +791,8 @@ discard block |
||
791 | 791 | */ |
792 | 792 | protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
793 | 793 | $inputVal = $this->post($inputName); |
794 | - if (! is_numeric($inputVal)) { |
|
795 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
794 | + if (!is_numeric($inputVal)) { |
|
795 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
796 | 796 | return; |
797 | 797 | } |
798 | 798 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
@@ -808,7 +808,7 @@ discard block |
||
808 | 808 | protected function _validateExists($inputName, $ruleName, array $ruleArgs) { |
809 | 809 | $inputVal = $this->post($inputName); |
810 | 810 | $obj = & get_instance(); |
811 | - if(! isset($obj->database)){ |
|
811 | + if (!isset($obj->database)) { |
|
812 | 812 | return; |
813 | 813 | } |
814 | 814 | list($table, $column) = explode('.', $ruleArgs[1]); |
@@ -817,7 +817,7 @@ discard block |
||
817 | 817 | ->get(); |
818 | 818 | $nb = $obj->database->numRows(); |
819 | 819 | if ($nb == 0) { |
820 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
820 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
821 | 821 | return; |
822 | 822 | } |
823 | 823 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
@@ -833,7 +833,7 @@ discard block |
||
833 | 833 | protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
834 | 834 | $inputVal = $this->post($inputName); |
835 | 835 | $obj = & get_instance(); |
836 | - if(! isset($obj->database)){ |
|
836 | + if (!isset($obj->database)) { |
|
837 | 837 | return; |
838 | 838 | } |
839 | 839 | list($table, $column) = explode('.', $ruleArgs[1]); |
@@ -842,7 +842,7 @@ discard block |
||
842 | 842 | ->get(); |
843 | 843 | $nb = $obj->database->numRows(); |
844 | 844 | if ($nb != 0) { |
845 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
845 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
846 | 846 | return; |
847 | 847 | } |
848 | 848 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
@@ -858,11 +858,11 @@ discard block |
||
858 | 858 | protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
859 | 859 | $inputVal = $this->post($inputName); |
860 | 860 | $obj = & get_instance(); |
861 | - if(! isset($obj->database)){ |
|
861 | + if (!isset($obj->database)) { |
|
862 | 862 | return; |
863 | 863 | } |
864 | 864 | $data = explode(',', $ruleArgs[1]); |
865 | - if(count($data) < 2){ |
|
865 | + if (count($data) < 2) { |
|
866 | 866 | return; |
867 | 867 | } |
868 | 868 | list($table, $column) = explode('.', $data[0]); |
@@ -873,7 +873,7 @@ discard block |
||
873 | 873 | ->get(); |
874 | 874 | $nb = $obj->database->numRows(); |
875 | 875 | if ($nb != 0) { |
876 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
876 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
877 | 877 | return; |
878 | 878 | } |
879 | 879 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
@@ -890,8 +890,8 @@ discard block |
||
890 | 890 | $inputVal = $this->post($inputName); |
891 | 891 | $list = explode(',', $ruleArgs[1]); |
892 | 892 | $list = array_map('trim', $list); |
893 | - if (! in_array($inputVal, $list)) { |
|
894 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
893 | + if (!in_array($inputVal, $list)) { |
|
894 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
895 | 895 | return; |
896 | 896 | } |
897 | 897 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -907,8 +907,8 @@ discard block |
||
907 | 907 | protected function _validateRegex($inputName, $ruleName, array $ruleArgs) { |
908 | 908 | $inputVal = $this->post($inputName); |
909 | 909 | $regex = $ruleArgs[1]; |
910 | - if (! preg_match($regex, $inputVal)) { |
|
911 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
910 | + if (!preg_match($regex, $inputVal)) { |
|
911 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
912 | 912 | return; |
913 | 913 | } |
914 | 914 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
@@ -223,8 +223,7 @@ discard block |
||
223 | 223 | if(! Security::validateCSRF()){ |
224 | 224 | show_error('Invalide data, Cross Site Request Forgery do his job, the data to validate is corrupted.'); |
225 | 225 | } |
226 | - } |
|
227 | - else{ |
|
226 | + } else{ |
|
228 | 227 | $this->logger->info('CSRF is not enabled in configuration or not set manully, no need to check it'); |
229 | 228 | } |
230 | 229 | } |
@@ -234,8 +233,7 @@ discard block |
||
234 | 233 | foreach ($this->getData() as $inputName => $inputVal) { |
235 | 234 | if(is_array($this->data[$inputName])){ |
236 | 235 | $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
237 | - } |
|
238 | - else{ |
|
236 | + } else{ |
|
239 | 237 | $this->data[$inputName] = trim($this->data[$inputName]); |
240 | 238 | } |
241 | 239 | |
@@ -604,8 +602,7 @@ discard block |
||
604 | 602 | if ($inputVal == '' && $callbackReturn == true) { |
605 | 603 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
606 | 604 | } |
607 | - } |
|
608 | - else if($inputVal == '') { |
|
605 | + } else if($inputVal == '') { |
|
609 | 606 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
610 | 607 | } |
611 | 608 | } |
@@ -664,8 +661,7 @@ discard block |
||
664 | 661 | $this->_setError($inputName, $ruleName . ',post:key', array($this->_getLabel($inputName), $this->_getLabel(str_replace('post:', '', $doNotEqual)))); |
665 | 662 | continue; |
666 | 663 | } |
667 | - } |
|
668 | - else{ |
|
664 | + } else{ |
|
669 | 665 | if ($inputVal == $doNotEqual) { |
670 | 666 | $this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
671 | 667 | continue; |
@@ -22,7 +22,7 @@ |
||
22 | 22 | * You should have received a copy of the GNU General Public License |
23 | 23 | * along with this program; if not, write to the Free Software |
24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | - */ |
|
25 | + */ |
|
26 | 26 | |
27 | 27 | class StringHash{ |
28 | 28 |
@@ -24,7 +24,7 @@ |
||
24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | 25 | */ |
26 | 26 | |
27 | - class StringHash{ |
|
27 | + class StringHash { |
|
28 | 28 | |
29 | 29 | //blowfish |
30 | 30 | private static $algo = '$2a'; |
@@ -22,7 +22,7 @@ |
||
22 | 22 | * You should have received a copy of the GNU General Public License |
23 | 23 | * along with this program; if not, write to the Free Software |
24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | - */ |
|
25 | + */ |
|
26 | 26 | |
27 | 27 | class Html{ |
28 | 28 |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | 25 | */ |
26 | 26 | |
27 | - class Html{ |
|
27 | + class Html { |
|
28 | 28 | |
29 | 29 | /** |
30 | 30 | * Generate the html anchor link |
@@ -35,21 +35,21 @@ discard block |
||
35 | 35 | * |
36 | 36 | * @return string|void the anchor link generated html if $return is true or display it if not |
37 | 37 | */ |
38 | - public static function a($link = '', $anchor = null, array $attributes = array(), $return = true){ |
|
39 | - if(! is_url($link)){ |
|
38 | + public static function a($link = '', $anchor = null, array $attributes = array(), $return = true) { |
|
39 | + if (!is_url($link)) { |
|
40 | 40 | $link = Url::site_url($link); |
41 | 41 | } |
42 | - if(! $anchor){ |
|
42 | + if (!$anchor) { |
|
43 | 43 | $anchor = $link; |
44 | 44 | } |
45 | 45 | $str = null; |
46 | - $str .= '<a href = "'.$link.'"'; |
|
46 | + $str .= '<a href = "' . $link . '"'; |
|
47 | 47 | $str .= attributes_to_string($attributes); |
48 | 48 | $str .= '>'; |
49 | 49 | $str .= $anchor; |
50 | 50 | $str .= '</a>'; |
51 | 51 | |
52 | - if($return){ |
|
52 | + if ($return) { |
|
53 | 53 | return $str; |
54 | 54 | } |
55 | 55 | echo $str; |
@@ -64,18 +64,18 @@ discard block |
||
64 | 64 | * |
65 | 65 | * @return string|void the generated html for mailto link if $return is true or display it if not |
66 | 66 | */ |
67 | - public static function mailto($link, $anchor = null, array $attributes = array(), $return = true){ |
|
68 | - if(! $anchor){ |
|
67 | + public static function mailto($link, $anchor = null, array $attributes = array(), $return = true) { |
|
68 | + if (!$anchor) { |
|
69 | 69 | $anchor = $link; |
70 | 70 | } |
71 | 71 | $str = null; |
72 | - $str .= '<a href = "mailto:'.$link.'"'; |
|
72 | + $str .= '<a href = "mailto:' . $link . '"'; |
|
73 | 73 | $str .= attributes_to_string($attributes); |
74 | 74 | $str .= '>'; |
75 | 75 | $str .= $anchor; |
76 | 76 | $str .= '</a>'; |
77 | 77 | |
78 | - if($return){ |
|
78 | + if ($return) { |
|
79 | 79 | return $str; |
80 | 80 | } |
81 | 81 | echo $str; |
@@ -88,8 +88,8 @@ discard block |
||
88 | 88 | * |
89 | 89 | * @return string|void the generated "br" html if $return is true or display it if not |
90 | 90 | */ |
91 | - public static function br($nb = 1, $return = true){ |
|
92 | - if(! is_numeric($nb) || $nb <= 0){ |
|
91 | + public static function br($nb = 1, $return = true) { |
|
92 | + if (!is_numeric($nb) || $nb <= 0) { |
|
93 | 93 | $nb = 1; |
94 | 94 | } |
95 | 95 | $str = null; |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | $str .= '<br />'; |
98 | 98 | } |
99 | 99 | |
100 | - if($return){ |
|
100 | + if ($return) { |
|
101 | 101 | return $str; |
102 | 102 | } |
103 | 103 | echo $str; |
@@ -111,15 +111,15 @@ discard block |
||
111 | 111 | * |
112 | 112 | * @return string|void the generated "hr" html if $return is true or display it if not. |
113 | 113 | */ |
114 | - public static function hr($nb = 1, array $attributes = array(), $return = true){ |
|
115 | - if(! is_numeric($nb) || $nb <= 0){ |
|
114 | + public static function hr($nb = 1, array $attributes = array(), $return = true) { |
|
115 | + if (!is_numeric($nb) || $nb <= 0) { |
|
116 | 116 | $nb = 1; |
117 | 117 | } |
118 | 118 | $str = null; |
119 | 119 | for ($i = 1; $i <= $nb; $i++) { |
120 | - $str .= '<hr' .attributes_to_string($attributes). ' />'; |
|
120 | + $str .= '<hr' . attributes_to_string($attributes) . ' />'; |
|
121 | 121 | } |
122 | - if($return){ |
|
122 | + if ($return) { |
|
123 | 123 | return $str; |
124 | 124 | } |
125 | 125 | echo $str; |
@@ -135,18 +135,18 @@ discard block |
||
135 | 135 | * |
136 | 136 | * @return string|void the generated header html if $return is true or display it if not. |
137 | 137 | */ |
138 | - public static function head($type = 1, $text = null, $nb = 1, array $attributes = array(), $return = true){ |
|
139 | - if(! is_numeric($nb) || $nb <= 0){ |
|
138 | + public static function head($type = 1, $text = null, $nb = 1, array $attributes = array(), $return = true) { |
|
139 | + if (!is_numeric($nb) || $nb <= 0) { |
|
140 | 140 | $nb = 1; |
141 | 141 | } |
142 | - if(! is_numeric($type) || $type <= 0 || $type > 6){ |
|
142 | + if (!is_numeric($type) || $type <= 0 || $type > 6) { |
|
143 | 143 | $type = 1; |
144 | 144 | } |
145 | 145 | $str = null; |
146 | 146 | for ($i = 1; $i <= $nb; $i++) { |
147 | - $str .= '<h' . $type . attributes_to_string($attributes). '>' .$text. '</h' . $type . '>'; |
|
147 | + $str .= '<h' . $type . attributes_to_string($attributes) . '>' . $text . '</h' . $type . '>'; |
|
148 | 148 | } |
149 | - if($return){ |
|
149 | + if ($return) { |
|
150 | 150 | return $str; |
151 | 151 | } |
152 | 152 | echo $str; |
@@ -161,15 +161,15 @@ discard block |
||
161 | 161 | * |
162 | 162 | * @return string|void the generated "ul" html if $return is true or display it if not. |
163 | 163 | */ |
164 | - public static function ul($data = array(), $attributes = array(), $return = true){ |
|
164 | + public static function ul($data = array(), $attributes = array(), $return = true) { |
|
165 | 165 | $data = (array) $data; |
166 | 166 | $str = null; |
167 | - $str .= '<ul' . (! empty($attributes['ul']) ? attributes_to_string($attributes['ul']):'') . '>'; |
|
167 | + $str .= '<ul' . (!empty($attributes['ul']) ? attributes_to_string($attributes['ul']) : '') . '>'; |
|
168 | 168 | foreach ($data as $row) { |
169 | - $str .= '<li' . (! empty($attributes['li']) ? attributes_to_string($attributes['li']):'') .'>' .$row. '</li>'; |
|
169 | + $str .= '<li' . (!empty($attributes['li']) ? attributes_to_string($attributes['li']) : '') . '>' . $row . '</li>'; |
|
170 | 170 | } |
171 | 171 | $str .= '</ul>'; |
172 | - if($return){ |
|
172 | + if ($return) { |
|
173 | 173 | return $str; |
174 | 174 | } |
175 | 175 | echo $str; |
@@ -183,15 +183,15 @@ discard block |
||
183 | 183 | * @param boolean $return whether need return the generated html or just display it directly |
184 | 184 | * @return string|void the generated "ol" html if $return is true or display it if not. |
185 | 185 | */ |
186 | - public static function ol($data = array(), $attributes = array(), $return = true){ |
|
186 | + public static function ol($data = array(), $attributes = array(), $return = true) { |
|
187 | 187 | $data = (array) $data; |
188 | 188 | $str = null; |
189 | - $str .= '<ol' . (!empty($attributes['ol']) ? attributes_to_string($attributes['ol']):'') . '>'; |
|
189 | + $str .= '<ol' . (!empty($attributes['ol']) ? attributes_to_string($attributes['ol']) : '') . '>'; |
|
190 | 190 | foreach ($data as $row) { |
191 | - $str .= '<li' . (!empty($attributes['li']) ? attributes_to_string($attributes['li']):'') .'>' .$row. '</li>'; |
|
191 | + $str .= '<li' . (!empty($attributes['li']) ? attributes_to_string($attributes['li']) : '') . '>' . $row . '</li>'; |
|
192 | 192 | } |
193 | 193 | $str .= '</ol>'; |
194 | - if($return){ |
|
194 | + if ($return) { |
|
195 | 195 | return $str; |
196 | 196 | } |
197 | 197 | echo $str; |
@@ -209,46 +209,46 @@ discard block |
||
209 | 209 | * @param boolean $return whether need return the generated html or just display it directly |
210 | 210 | * @return string|void the generated "table" html if $return is true or display it if not. |
211 | 211 | */ |
212 | - public static function table($headers = array(), $body = array(), $attributes = array(), $use_footer = false, $return = true){ |
|
212 | + public static function table($headers = array(), $body = array(), $attributes = array(), $use_footer = false, $return = true) { |
|
213 | 213 | $headers = (array) $headers; |
214 | 214 | $body = (array) $body; |
215 | 215 | $str = null; |
216 | - $str .= '<table' . (! empty($attributes['table']) ? attributes_to_string($attributes['table']):'') . '>'; |
|
217 | - if(! empty($headers)){ |
|
218 | - $str .= '<thead' . (! empty($attributes['thead']) ? attributes_to_string($attributes['thead']):'') .'>'; |
|
219 | - $str .= '<tr' . (! empty($attributes['thead_tr']) ? attributes_to_string($attributes['thead_tr']):'') .'>'; |
|
216 | + $str .= '<table' . (!empty($attributes['table']) ? attributes_to_string($attributes['table']) : '') . '>'; |
|
217 | + if (!empty($headers)) { |
|
218 | + $str .= '<thead' . (!empty($attributes['thead']) ? attributes_to_string($attributes['thead']) : '') . '>'; |
|
219 | + $str .= '<tr' . (!empty($attributes['thead_tr']) ? attributes_to_string($attributes['thead_tr']) : '') . '>'; |
|
220 | 220 | foreach ($headers as $value) { |
221 | - $str .= '<th' . (! empty($attributes['thead_th']) ? attributes_to_string($attributes['thead_th']):'') .'>' .$value. '</th>'; |
|
221 | + $str .= '<th' . (!empty($attributes['thead_th']) ? attributes_to_string($attributes['thead_th']) : '') . '>' . $value . '</th>'; |
|
222 | 222 | } |
223 | 223 | $str .= '</tr>'; |
224 | 224 | $str .= '</thead>'; |
225 | 225 | } |
226 | - else{ |
|
226 | + else { |
|
227 | 227 | //no need check for footer |
228 | 228 | $use_footer = false; |
229 | 229 | } |
230 | - $str .= '<tbody' . (! empty($attributes['tbody']) ? attributes_to_string($attributes['tbody']):'') .'>'; |
|
230 | + $str .= '<tbody' . (!empty($attributes['tbody']) ? attributes_to_string($attributes['tbody']) : '') . '>'; |
|
231 | 231 | foreach ($body as $row) { |
232 | - if(is_array($row)){ |
|
233 | - $str .= '<tr' . (! empty($attributes['tbody_tr']) ? attributes_to_string($attributes['tbody_tr']):'') .'>'; |
|
232 | + if (is_array($row)) { |
|
233 | + $str .= '<tr' . (!empty($attributes['tbody_tr']) ? attributes_to_string($attributes['tbody_tr']) : '') . '>'; |
|
234 | 234 | foreach ($row as $value) { |
235 | - $str .= '<td' . (! empty($attributes['tbody_td']) ? attributes_to_string($attributes['tbody_td']):'') .'>' .$value. '</td>'; |
|
235 | + $str .= '<td' . (!empty($attributes['tbody_td']) ? attributes_to_string($attributes['tbody_td']) : '') . '>' . $value . '</td>'; |
|
236 | 236 | } |
237 | 237 | $str .= '</tr>'; |
238 | 238 | } |
239 | 239 | } |
240 | 240 | $str .= '</tbody>'; |
241 | - if($use_footer){ |
|
242 | - $str .= '<tfoot' . (! empty($attributes['tfoot']) ? attributes_to_string($attributes['tfoot']):'') .'>'; |
|
243 | - $str .= '<tr' . (! empty($attributes['tfoot_tr']) ? attributes_to_string($attributes['tfoot_tr']):'') .'>'; |
|
241 | + if ($use_footer) { |
|
242 | + $str .= '<tfoot' . (!empty($attributes['tfoot']) ? attributes_to_string($attributes['tfoot']) : '') . '>'; |
|
243 | + $str .= '<tr' . (!empty($attributes['tfoot_tr']) ? attributes_to_string($attributes['tfoot_tr']) : '') . '>'; |
|
244 | 244 | foreach ($headers as $value) { |
245 | - $str .= '<th' . (! empty($attributes['tfoot_th']) ? attributes_to_string($attributes['tfoot_th']):'') .'>' .$value. '</th>'; |
|
245 | + $str .= '<th' . (!empty($attributes['tfoot_th']) ? attributes_to_string($attributes['tfoot_th']) : '') . '>' . $value . '</th>'; |
|
246 | 246 | } |
247 | 247 | $str .= '</tr>'; |
248 | 248 | $str .= '</tfoot>'; |
249 | 249 | } |
250 | 250 | $str .= '</table>'; |
251 | - if($return){ |
|
251 | + if ($return) { |
|
252 | 252 | return $str; |
253 | 253 | } |
254 | 254 | echo $str; |
@@ -222,8 +222,7 @@ |
||
222 | 222 | } |
223 | 223 | $str .= '</tr>'; |
224 | 224 | $str .= '</thead>'; |
225 | - } |
|
226 | - else{ |
|
225 | + } else{ |
|
227 | 226 | //no need check for footer |
228 | 227 | $use_footer = false; |
229 | 228 | } |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
3 | 3 | /** |
4 | 4 | * TNH Framework |
5 | 5 | * |
@@ -22,26 +22,26 @@ discard block |
||
22 | 22 | * You should have received a copy of the GNU General Public License |
23 | 23 | * along with this program; if not, write to the Free Software |
24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | - */ |
|
25 | + */ |
|
26 | 26 | |
27 | - class Pagination{ |
|
27 | + class Pagination{ |
|
28 | 28 | |
29 | 29 | /** |
30 | - * The list of loaded config |
|
31 | - * @var array |
|
32 | - */ |
|
33 | - private $config = array(); |
|
30 | + * The list of loaded config |
|
31 | + * @var array |
|
32 | + */ |
|
33 | + private $config = array(); |
|
34 | 34 | |
35 | - /** |
|
36 | - * Create an instance of pagination |
|
37 | - * @param array $overwriteConfig the list of configuration to overwrite the defined configuration in config_pagination.php |
|
38 | - */ |
|
39 | - public function __construct($overwriteConfig = array()){ |
|
40 | - if(file_exists(CONFIG_PATH . 'config_pagination.php')){ |
|
41 | - require_once CONFIG_PATH . 'config_pagination.php'; |
|
42 | - if(empty($config) || ! is_array($config)){ |
|
43 | - show_error('No configuration found in ' . CONFIG_PATH . 'config_pagination.php'); |
|
44 | - } |
|
35 | + /** |
|
36 | + * Create an instance of pagination |
|
37 | + * @param array $overwriteConfig the list of configuration to overwrite the defined configuration in config_pagination.php |
|
38 | + */ |
|
39 | + public function __construct($overwriteConfig = array()){ |
|
40 | + if(file_exists(CONFIG_PATH . 'config_pagination.php')){ |
|
41 | + require_once CONFIG_PATH . 'config_pagination.php'; |
|
42 | + if(empty($config) || ! is_array($config)){ |
|
43 | + show_error('No configuration found in ' . CONFIG_PATH . 'config_pagination.php'); |
|
44 | + } |
|
45 | 45 | else{ |
46 | 46 | if(! empty($overwriteConfig)){ |
47 | 47 | $config = array_merge($config, $overwriteConfig); |
@@ -50,125 +50,125 @@ discard block |
||
50 | 50 | Config::setAll($config); |
51 | 51 | unset($config); |
52 | 52 | } |
53 | - } |
|
54 | - else{ |
|
55 | - show_error('Unable to find the pagination configuration file'); |
|
56 | - } |
|
57 | - } |
|
53 | + } |
|
54 | + else{ |
|
55 | + show_error('Unable to find the pagination configuration file'); |
|
56 | + } |
|
57 | + } |
|
58 | 58 | |
59 | 59 | |
60 | - /** |
|
61 | - * Set the pagination custom configuration to overwrite the default configuration in |
|
62 | - * config_pagination.php |
|
63 | - * @param array $config the configuration to set |
|
64 | - */ |
|
65 | - public function setConfig(array $config = array()){ |
|
66 | - if(! empty($config)){ |
|
67 | - $this->config = array_merge($this->config, $config); |
|
68 | - Config::setAll($config); |
|
69 | - } |
|
70 | - } |
|
60 | + /** |
|
61 | + * Set the pagination custom configuration to overwrite the default configuration in |
|
62 | + * config_pagination.php |
|
63 | + * @param array $config the configuration to set |
|
64 | + */ |
|
65 | + public function setConfig(array $config = array()){ |
|
66 | + if(! empty($config)){ |
|
67 | + $this->config = array_merge($this->config, $config); |
|
68 | + Config::setAll($config); |
|
69 | + } |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Generate the pagination link |
|
74 | - * @param int $totalRows the total number of data |
|
75 | - * @param int $currentPageNumber the current page number |
|
76 | - * @return string the pagination link |
|
77 | - */ |
|
78 | - public function getLink($totalRows, $currentPageNumber){ |
|
79 | - $pageQueryName = $this->config['page_query_string_name']; |
|
80 | - $numberOfLink = $this->config['nb_link']; |
|
72 | + /** |
|
73 | + * Generate the pagination link |
|
74 | + * @param int $totalRows the total number of data |
|
75 | + * @param int $currentPageNumber the current page number |
|
76 | + * @return string the pagination link |
|
77 | + */ |
|
78 | + public function getLink($totalRows, $currentPageNumber){ |
|
79 | + $pageQueryName = $this->config['page_query_string_name']; |
|
80 | + $numberOfLink = $this->config['nb_link']; |
|
81 | 81 | $numberOfRowPerPage = $this->config['pagination_per_page']; |
82 | - $queryString = Url::queryString(); |
|
83 | - $currentUrl = Url::current(); |
|
84 | - if($queryString == ''){ |
|
85 | - $query = '?' . $pageQueryName . '='; |
|
86 | - } |
|
87 | - else{ |
|
88 | - $tab = explode($pageQueryName . '=', $queryString); |
|
89 | - $nb = count($tab); |
|
90 | - if($nb == 1){ |
|
91 | - $query = '?' . $queryString . '&' . $pageQueryName . '='; |
|
92 | - } |
|
93 | - else{ |
|
94 | - if($tab[0] == ''){ |
|
95 | - $query = '?' . $pageQueryName . '='; |
|
96 | - } |
|
97 | - else{ |
|
98 | - $query = '?' . $tab[0] . '' . $pageQueryName . '='; |
|
99 | - } |
|
100 | - } |
|
101 | - } |
|
102 | - $temp = explode('?', $currentUrl); |
|
103 | - $query = $temp[0] . $query; |
|
104 | - $navbar = ''; |
|
105 | - $numberOfPage = ceil($totalRows / $numberOfRowPerPage); |
|
82 | + $queryString = Url::queryString(); |
|
83 | + $currentUrl = Url::current(); |
|
84 | + if($queryString == ''){ |
|
85 | + $query = '?' . $pageQueryName . '='; |
|
86 | + } |
|
87 | + else{ |
|
88 | + $tab = explode($pageQueryName . '=', $queryString); |
|
89 | + $nb = count($tab); |
|
90 | + if($nb == 1){ |
|
91 | + $query = '?' . $queryString . '&' . $pageQueryName . '='; |
|
92 | + } |
|
93 | + else{ |
|
94 | + if($tab[0] == ''){ |
|
95 | + $query = '?' . $pageQueryName . '='; |
|
96 | + } |
|
97 | + else{ |
|
98 | + $query = '?' . $tab[0] . '' . $pageQueryName . '='; |
|
99 | + } |
|
100 | + } |
|
101 | + } |
|
102 | + $temp = explode('?', $currentUrl); |
|
103 | + $query = $temp[0] . $query; |
|
104 | + $navbar = ''; |
|
105 | + $numberOfPage = ceil($totalRows / $numberOfRowPerPage); |
|
106 | 106 | if(! is_numeric ($currentPageNumber) || $currentPageNumber <= 0){ |
107 | 107 | $currentPageNumber = 1; |
108 | 108 | } |
109 | - if($numberOfPage <= 1 || $numberOfLink <= 0 || $numberOfRowPerPage <= 0 || !is_numeric($numberOfLink) || !is_numeric($numberOfRowPerPage) |
|
110 | - ){ |
|
111 | - return $navbar; |
|
112 | - } |
|
113 | - if($numberOfLink % 2 == 0){ |
|
114 | - $start = $currentPageNumber - ($numberOfLink / 2) + 1; |
|
115 | - $end = $currentPageNumber + ($numberOfLink / 2); |
|
116 | - } |
|
117 | - else{ |
|
118 | - $start = $currentPageNumber - floor($numberOfLink / 2); |
|
119 | - $end = $currentPageNumber + floor($numberOfLink / 2); |
|
120 | - } |
|
121 | - if($start <= 1){ |
|
122 | - $begin = 1; |
|
123 | - $end = $numberOfLink; |
|
124 | - } |
|
125 | - else if($start > 1 && $end < $numberOfPage){ |
|
126 | - $begin = $start; |
|
127 | - $end = $end; |
|
128 | - } |
|
129 | - else{ |
|
130 | - $begin = ($numberOfPage - $numberOfLink) + 1; |
|
131 | - $end = $numberOfPage; |
|
132 | - } |
|
133 | - if($numberOfPage <= $numberOfLink){ |
|
134 | - $begin = 1; |
|
135 | - $end = $numberOfPage; |
|
136 | - } |
|
137 | - if($currentPageNumber == 1){ |
|
138 | - for($i = $begin; $i <= $end; $i++){ |
|
139 | - if($i == $currentPageNumber){ |
|
140 | - $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
|
141 | - } |
|
142 | - else{ |
|
143 | - $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '" ' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
|
144 | - } |
|
145 | - } |
|
146 | - $navbar .= $this->config['next_open'] . '<a href="' . $query . ($currentPageNumber + 1) . '">' . $this->config['next_text'] . '</a>' . $this->config['next_close']; |
|
147 | - } |
|
148 | - else if($currentPageNumber > 1 && $currentPageNumber < $numberOfPage){ |
|
149 | - $navbar .= $this->config['previous_open'] . '<a href="' . $query . ($currentPageNumber - 1) . '">' . $this->config['previous_text'] . '</a>' . $this->config['previous_close']; |
|
150 | - for($i = $begin; $i <= $end; $i++){ |
|
151 | - if($i == $currentPageNumber){ |
|
152 | - $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
|
153 | - } |
|
154 | - else{ |
|
155 | - $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i .'</a>' . $this->config['digit_close']; |
|
156 | - } |
|
157 | - } |
|
158 | - $navbar .= $this->config['next_open']."<a href='$query".($currentPageNumber + 1)."'>".$this->config['next_text']."</a>".$this->config['next_close']; |
|
159 | - } |
|
160 | - else if($currentPageNumber == $numberOfPage){ |
|
161 | - $navbar .= $this->config['previous_open'] . '<a href="' . $query . ($currentPageNumber - 1) . '">' . $this->config['previous_text'] . '</a>' . $this->config['previous_close']; |
|
162 | - for($i = $begin; $i <= $end; $i++){ |
|
163 | - if($i == $currentPageNumber){ |
|
164 | - $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
|
165 | - } |
|
166 | - else{ |
|
167 | - $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
|
168 | - } |
|
169 | - } |
|
170 | - } |
|
171 | - $navbar = $this->config['pagination_open'] . $navbar . $this->config['pagination_close']; |
|
172 | - return $navbar; |
|
173 | - } |
|
174 | - } |
|
175 | 109 | \ No newline at end of file |
110 | + if($numberOfPage <= 1 || $numberOfLink <= 0 || $numberOfRowPerPage <= 0 || !is_numeric($numberOfLink) || !is_numeric($numberOfRowPerPage) |
|
111 | + ){ |
|
112 | + return $navbar; |
|
113 | + } |
|
114 | + if($numberOfLink % 2 == 0){ |
|
115 | + $start = $currentPageNumber - ($numberOfLink / 2) + 1; |
|
116 | + $end = $currentPageNumber + ($numberOfLink / 2); |
|
117 | + } |
|
118 | + else{ |
|
119 | + $start = $currentPageNumber - floor($numberOfLink / 2); |
|
120 | + $end = $currentPageNumber + floor($numberOfLink / 2); |
|
121 | + } |
|
122 | + if($start <= 1){ |
|
123 | + $begin = 1; |
|
124 | + $end = $numberOfLink; |
|
125 | + } |
|
126 | + else if($start > 1 && $end < $numberOfPage){ |
|
127 | + $begin = $start; |
|
128 | + $end = $end; |
|
129 | + } |
|
130 | + else{ |
|
131 | + $begin = ($numberOfPage - $numberOfLink) + 1; |
|
132 | + $end = $numberOfPage; |
|
133 | + } |
|
134 | + if($numberOfPage <= $numberOfLink){ |
|
135 | + $begin = 1; |
|
136 | + $end = $numberOfPage; |
|
137 | + } |
|
138 | + if($currentPageNumber == 1){ |
|
139 | + for($i = $begin; $i <= $end; $i++){ |
|
140 | + if($i == $currentPageNumber){ |
|
141 | + $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
|
142 | + } |
|
143 | + else{ |
|
144 | + $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '" ' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
|
145 | + } |
|
146 | + } |
|
147 | + $navbar .= $this->config['next_open'] . '<a href="' . $query . ($currentPageNumber + 1) . '">' . $this->config['next_text'] . '</a>' . $this->config['next_close']; |
|
148 | + } |
|
149 | + else if($currentPageNumber > 1 && $currentPageNumber < $numberOfPage){ |
|
150 | + $navbar .= $this->config['previous_open'] . '<a href="' . $query . ($currentPageNumber - 1) . '">' . $this->config['previous_text'] . '</a>' . $this->config['previous_close']; |
|
151 | + for($i = $begin; $i <= $end; $i++){ |
|
152 | + if($i == $currentPageNumber){ |
|
153 | + $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
|
154 | + } |
|
155 | + else{ |
|
156 | + $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i .'</a>' . $this->config['digit_close']; |
|
157 | + } |
|
158 | + } |
|
159 | + $navbar .= $this->config['next_open']."<a href='$query".($currentPageNumber + 1)."'>".$this->config['next_text']."</a>".$this->config['next_close']; |
|
160 | + } |
|
161 | + else if($currentPageNumber == $numberOfPage){ |
|
162 | + $navbar .= $this->config['previous_open'] . '<a href="' . $query . ($currentPageNumber - 1) . '">' . $this->config['previous_text'] . '</a>' . $this->config['previous_close']; |
|
163 | + for($i = $begin; $i <= $end; $i++){ |
|
164 | + if($i == $currentPageNumber){ |
|
165 | + $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
|
166 | + } |
|
167 | + else{ |
|
168 | + $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
|
169 | + } |
|
170 | + } |
|
171 | + } |
|
172 | + $navbar = $this->config['pagination_open'] . $navbar . $this->config['pagination_close']; |
|
173 | + return $navbar; |
|
174 | + } |
|
175 | + } |
|
176 | 176 | \ No newline at end of file |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | 25 | */ |
26 | 26 | |
27 | - class Pagination{ |
|
27 | + class Pagination { |
|
28 | 28 | |
29 | 29 | /** |
30 | 30 | * The list of loaded config |
@@ -36,14 +36,14 @@ discard block |
||
36 | 36 | * Create an instance of pagination |
37 | 37 | * @param array $overwriteConfig the list of configuration to overwrite the defined configuration in config_pagination.php |
38 | 38 | */ |
39 | - public function __construct($overwriteConfig = array()){ |
|
40 | - if(file_exists(CONFIG_PATH . 'config_pagination.php')){ |
|
39 | + public function __construct($overwriteConfig = array()) { |
|
40 | + if (file_exists(CONFIG_PATH . 'config_pagination.php')) { |
|
41 | 41 | require_once CONFIG_PATH . 'config_pagination.php'; |
42 | - if(empty($config) || ! is_array($config)){ |
|
42 | + if (empty($config) || !is_array($config)) { |
|
43 | 43 | show_error('No configuration found in ' . CONFIG_PATH . 'config_pagination.php'); |
44 | 44 | } |
45 | - else{ |
|
46 | - if(! empty($overwriteConfig)){ |
|
45 | + else { |
|
46 | + if (!empty($overwriteConfig)) { |
|
47 | 47 | $config = array_merge($config, $overwriteConfig); |
48 | 48 | } |
49 | 49 | $this->config = $config; |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | unset($config); |
52 | 52 | } |
53 | 53 | } |
54 | - else{ |
|
54 | + else { |
|
55 | 55 | show_error('Unable to find the pagination configuration file'); |
56 | 56 | } |
57 | 57 | } |
@@ -62,8 +62,8 @@ discard block |
||
62 | 62 | * config_pagination.php |
63 | 63 | * @param array $config the configuration to set |
64 | 64 | */ |
65 | - public function setConfig(array $config = array()){ |
|
66 | - if(! empty($config)){ |
|
65 | + public function setConfig(array $config = array()) { |
|
66 | + if (!empty($config)) { |
|
67 | 67 | $this->config = array_merge($this->config, $config); |
68 | 68 | Config::setAll($config); |
69 | 69 | } |
@@ -75,26 +75,26 @@ discard block |
||
75 | 75 | * @param int $currentPageNumber the current page number |
76 | 76 | * @return string the pagination link |
77 | 77 | */ |
78 | - public function getLink($totalRows, $currentPageNumber){ |
|
78 | + public function getLink($totalRows, $currentPageNumber) { |
|
79 | 79 | $pageQueryName = $this->config['page_query_string_name']; |
80 | 80 | $numberOfLink = $this->config['nb_link']; |
81 | 81 | $numberOfRowPerPage = $this->config['pagination_per_page']; |
82 | 82 | $queryString = Url::queryString(); |
83 | 83 | $currentUrl = Url::current(); |
84 | - if($queryString == ''){ |
|
84 | + if ($queryString == '') { |
|
85 | 85 | $query = '?' . $pageQueryName . '='; |
86 | 86 | } |
87 | - else{ |
|
87 | + else { |
|
88 | 88 | $tab = explode($pageQueryName . '=', $queryString); |
89 | 89 | $nb = count($tab); |
90 | - if($nb == 1){ |
|
90 | + if ($nb == 1) { |
|
91 | 91 | $query = '?' . $queryString . '&' . $pageQueryName . '='; |
92 | 92 | } |
93 | - else{ |
|
94 | - if($tab[0] == ''){ |
|
93 | + else { |
|
94 | + if ($tab[0] == '') { |
|
95 | 95 | $query = '?' . $pageQueryName . '='; |
96 | 96 | } |
97 | - else{ |
|
97 | + else { |
|
98 | 98 | $query = '?' . $tab[0] . '' . $pageQueryName . '='; |
99 | 99 | } |
100 | 100 | } |
@@ -103,67 +103,67 @@ discard block |
||
103 | 103 | $query = $temp[0] . $query; |
104 | 104 | $navbar = ''; |
105 | 105 | $numberOfPage = ceil($totalRows / $numberOfRowPerPage); |
106 | - if(! is_numeric ($currentPageNumber) || $currentPageNumber <= 0){ |
|
106 | + if (!is_numeric($currentPageNumber) || $currentPageNumber <= 0) { |
|
107 | 107 | $currentPageNumber = 1; |
108 | 108 | } |
109 | - if($numberOfPage <= 1 || $numberOfLink <= 0 || $numberOfRowPerPage <= 0 || !is_numeric($numberOfLink) || !is_numeric($numberOfRowPerPage) |
|
110 | - ){ |
|
109 | + if ($numberOfPage <= 1 || $numberOfLink <= 0 || $numberOfRowPerPage <= 0 || !is_numeric($numberOfLink) || !is_numeric($numberOfRowPerPage) |
|
110 | + ) { |
|
111 | 111 | return $navbar; |
112 | 112 | } |
113 | - if($numberOfLink % 2 == 0){ |
|
113 | + if ($numberOfLink % 2 == 0) { |
|
114 | 114 | $start = $currentPageNumber - ($numberOfLink / 2) + 1; |
115 | 115 | $end = $currentPageNumber + ($numberOfLink / 2); |
116 | 116 | } |
117 | - else{ |
|
117 | + else { |
|
118 | 118 | $start = $currentPageNumber - floor($numberOfLink / 2); |
119 | 119 | $end = $currentPageNumber + floor($numberOfLink / 2); |
120 | 120 | } |
121 | - if($start <= 1){ |
|
121 | + if ($start <= 1) { |
|
122 | 122 | $begin = 1; |
123 | 123 | $end = $numberOfLink; |
124 | 124 | } |
125 | - else if($start > 1 && $end < $numberOfPage){ |
|
125 | + else if ($start > 1 && $end < $numberOfPage) { |
|
126 | 126 | $begin = $start; |
127 | 127 | $end = $end; |
128 | 128 | } |
129 | - else{ |
|
129 | + else { |
|
130 | 130 | $begin = ($numberOfPage - $numberOfLink) + 1; |
131 | 131 | $end = $numberOfPage; |
132 | 132 | } |
133 | - if($numberOfPage <= $numberOfLink){ |
|
133 | + if ($numberOfPage <= $numberOfLink) { |
|
134 | 134 | $begin = 1; |
135 | 135 | $end = $numberOfPage; |
136 | 136 | } |
137 | - if($currentPageNumber == 1){ |
|
138 | - for($i = $begin; $i <= $end; $i++){ |
|
139 | - if($i == $currentPageNumber){ |
|
137 | + if ($currentPageNumber == 1) { |
|
138 | + for ($i = $begin; $i <= $end; $i++) { |
|
139 | + if ($i == $currentPageNumber) { |
|
140 | 140 | $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
141 | 141 | } |
142 | - else{ |
|
142 | + else { |
|
143 | 143 | $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '" ' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
144 | 144 | } |
145 | 145 | } |
146 | 146 | $navbar .= $this->config['next_open'] . '<a href="' . $query . ($currentPageNumber + 1) . '">' . $this->config['next_text'] . '</a>' . $this->config['next_close']; |
147 | 147 | } |
148 | - else if($currentPageNumber > 1 && $currentPageNumber < $numberOfPage){ |
|
148 | + else if ($currentPageNumber > 1 && $currentPageNumber < $numberOfPage) { |
|
149 | 149 | $navbar .= $this->config['previous_open'] . '<a href="' . $query . ($currentPageNumber - 1) . '">' . $this->config['previous_text'] . '</a>' . $this->config['previous_close']; |
150 | - for($i = $begin; $i <= $end; $i++){ |
|
151 | - if($i == $currentPageNumber){ |
|
150 | + for ($i = $begin; $i <= $end; $i++) { |
|
151 | + if ($i == $currentPageNumber) { |
|
152 | 152 | $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
153 | 153 | } |
154 | - else{ |
|
155 | - $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i .'</a>' . $this->config['digit_close']; |
|
154 | + else { |
|
155 | + $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
|
156 | 156 | } |
157 | 157 | } |
158 | - $navbar .= $this->config['next_open']."<a href='$query".($currentPageNumber + 1)."'>".$this->config['next_text']."</a>".$this->config['next_close']; |
|
158 | + $navbar .= $this->config['next_open'] . "<a href='$query" . ($currentPageNumber + 1) . "'>" . $this->config['next_text'] . "</a>" . $this->config['next_close']; |
|
159 | 159 | } |
160 | - else if($currentPageNumber == $numberOfPage){ |
|
160 | + else if ($currentPageNumber == $numberOfPage) { |
|
161 | 161 | $navbar .= $this->config['previous_open'] . '<a href="' . $query . ($currentPageNumber - 1) . '">' . $this->config['previous_text'] . '</a>' . $this->config['previous_close']; |
162 | - for($i = $begin; $i <= $end; $i++){ |
|
163 | - if($i == $currentPageNumber){ |
|
162 | + for ($i = $begin; $i <= $end; $i++) { |
|
163 | + if ($i == $currentPageNumber) { |
|
164 | 164 | $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
165 | 165 | } |
166 | - else{ |
|
166 | + else { |
|
167 | 167 | $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
168 | 168 | } |
169 | 169 | } |
@@ -41,8 +41,7 @@ discard block |
||
41 | 41 | require_once CONFIG_PATH . 'config_pagination.php'; |
42 | 42 | if(empty($config) || ! is_array($config)){ |
43 | 43 | show_error('No configuration found in ' . CONFIG_PATH . 'config_pagination.php'); |
44 | - } |
|
45 | - else{ |
|
44 | + } else{ |
|
46 | 45 | if(! empty($overwriteConfig)){ |
47 | 46 | $config = array_merge($config, $overwriteConfig); |
48 | 47 | } |
@@ -50,8 +49,7 @@ discard block |
||
50 | 49 | Config::setAll($config); |
51 | 50 | unset($config); |
52 | 51 | } |
53 | - } |
|
54 | - else{ |
|
52 | + } else{ |
|
55 | 53 | show_error('Unable to find the pagination configuration file'); |
56 | 54 | } |
57 | 55 | } |
@@ -83,18 +81,15 @@ discard block |
||
83 | 81 | $currentUrl = Url::current(); |
84 | 82 | if($queryString == ''){ |
85 | 83 | $query = '?' . $pageQueryName . '='; |
86 | - } |
|
87 | - else{ |
|
84 | + } else{ |
|
88 | 85 | $tab = explode($pageQueryName . '=', $queryString); |
89 | 86 | $nb = count($tab); |
90 | 87 | if($nb == 1){ |
91 | 88 | $query = '?' . $queryString . '&' . $pageQueryName . '='; |
92 | - } |
|
93 | - else{ |
|
89 | + } else{ |
|
94 | 90 | if($tab[0] == ''){ |
95 | 91 | $query = '?' . $pageQueryName . '='; |
96 | - } |
|
97 | - else{ |
|
92 | + } else{ |
|
98 | 93 | $query = '?' . $tab[0] . '' . $pageQueryName . '='; |
99 | 94 | } |
100 | 95 | } |
@@ -113,20 +108,17 @@ discard block |
||
113 | 108 | if($numberOfLink % 2 == 0){ |
114 | 109 | $start = $currentPageNumber - ($numberOfLink / 2) + 1; |
115 | 110 | $end = $currentPageNumber + ($numberOfLink / 2); |
116 | - } |
|
117 | - else{ |
|
111 | + } else{ |
|
118 | 112 | $start = $currentPageNumber - floor($numberOfLink / 2); |
119 | 113 | $end = $currentPageNumber + floor($numberOfLink / 2); |
120 | 114 | } |
121 | 115 | if($start <= 1){ |
122 | 116 | $begin = 1; |
123 | 117 | $end = $numberOfLink; |
124 | - } |
|
125 | - else if($start > 1 && $end < $numberOfPage){ |
|
118 | + } else if($start > 1 && $end < $numberOfPage){ |
|
126 | 119 | $begin = $start; |
127 | 120 | $end = $end; |
128 | - } |
|
129 | - else{ |
|
121 | + } else{ |
|
130 | 122 | $begin = ($numberOfPage - $numberOfLink) + 1; |
131 | 123 | $end = $numberOfPage; |
132 | 124 | } |
@@ -138,32 +130,27 @@ discard block |
||
138 | 130 | for($i = $begin; $i <= $end; $i++){ |
139 | 131 | if($i == $currentPageNumber){ |
140 | 132 | $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
141 | - } |
|
142 | - else{ |
|
133 | + } else{ |
|
143 | 134 | $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '" ' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
144 | 135 | } |
145 | 136 | } |
146 | 137 | $navbar .= $this->config['next_open'] . '<a href="' . $query . ($currentPageNumber + 1) . '">' . $this->config['next_text'] . '</a>' . $this->config['next_close']; |
147 | - } |
|
148 | - else if($currentPageNumber > 1 && $currentPageNumber < $numberOfPage){ |
|
138 | + } else if($currentPageNumber > 1 && $currentPageNumber < $numberOfPage){ |
|
149 | 139 | $navbar .= $this->config['previous_open'] . '<a href="' . $query . ($currentPageNumber - 1) . '">' . $this->config['previous_text'] . '</a>' . $this->config['previous_close']; |
150 | 140 | for($i = $begin; $i <= $end; $i++){ |
151 | 141 | if($i == $currentPageNumber){ |
152 | 142 | $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
153 | - } |
|
154 | - else{ |
|
143 | + } else{ |
|
155 | 144 | $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i .'</a>' . $this->config['digit_close']; |
156 | 145 | } |
157 | 146 | } |
158 | 147 | $navbar .= $this->config['next_open']."<a href='$query".($currentPageNumber + 1)."'>".$this->config['next_text']."</a>".$this->config['next_close']; |
159 | - } |
|
160 | - else if($currentPageNumber == $numberOfPage){ |
|
148 | + } else if($currentPageNumber == $numberOfPage){ |
|
161 | 149 | $navbar .= $this->config['previous_open'] . '<a href="' . $query . ($currentPageNumber - 1) . '">' . $this->config['previous_text'] . '</a>' . $this->config['previous_close']; |
162 | 150 | for($i = $begin; $i <= $end; $i++){ |
163 | 151 | if($i == $currentPageNumber){ |
164 | 152 | $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
165 | - } |
|
166 | - else{ |
|
153 | + } else{ |
|
167 | 154 | $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
168 | 155 | } |
169 | 156 | } |