@@ -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 | } |
@@ -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,13 +304,13 @@ 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"); |
| 312 | 312 | $attachment = null; |
| 313 | - if(is_resource($handle)){ |
|
| 313 | + if (is_resource($handle)) { |
|
| 314 | 314 | $attachment = fread($handle, $filesize); |
| 315 | 315 | fclose($handle); |
| 316 | 316 | } |
@@ -1,739 +1,739 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * Simple Mail |
|
| 5 | - * |
|
| 6 | - * A simple PHP wrapper class for sending email using the mail() method. |
|
| 7 | - * |
|
| 8 | - * PHP version > 5.2 |
|
| 9 | - * |
|
| 10 | - * LICENSE: This source file is subject to the MIT license, which is |
|
| 11 | - * available through the world-wide-web at the following URI: |
|
| 12 | - * http://github.com/eoghanobrien/php-simple-mail/LICENCE.txt |
|
| 13 | - * |
|
| 14 | - * @category SimpleMail |
|
| 15 | - * @package SimpleMail |
|
| 16 | - * @author Eoghan O'Brien <[email protected]> |
|
| 17 | - * @copyright 2009 - 2017 Eoghan O'Brien |
|
| 18 | - * @license http://github.com/eoghanobrien/php-simple-mail/LICENCE.txt MIT |
|
| 19 | - * @version 1.7.1 |
|
| 20 | - * @link http://github.com/eoghanobrien/php-simple-mail |
|
| 21 | - */ |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Simple Mail class. |
|
| 25 | - * |
|
| 26 | - * @category SimpleMail |
|
| 27 | - * @package SimpleMail |
|
| 28 | - * @author Eoghan O'Brien <[email protected]> |
|
| 29 | - * @copyright 2009 - 2017 Eoghan O'Brien |
|
| 30 | - * @license http://github.com/eoghanobrien/php-simple-mail/LICENCE.txt MIT |
|
| 31 | - * @version 1.7.1 |
|
| 32 | - * @link http://github.com/eoghanobrien/php-simple-mail |
|
| 33 | - */ |
|
| 34 | - class Email |
|
| 35 | - { |
|
| 36 | - /** |
|
| 37 | - * @var int $_wrap |
|
| 38 | - */ |
|
| 39 | - protected $_wrap = 78; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var array $_to |
|
| 43 | - */ |
|
| 44 | - protected $_to = array(); |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var string $_subject |
|
| 48 | - */ |
|
| 49 | - protected $_subject; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @var string $_message |
|
| 53 | - */ |
|
| 54 | - protected $_message; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @var array $_headers |
|
| 58 | - */ |
|
| 59 | - protected $_headers = array(); |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @var string $_parameters |
|
| 63 | - */ |
|
| 64 | - protected $_params; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @var array $_attachments |
|
| 68 | - */ |
|
| 69 | - protected $_attachments = array(); |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @var string $_uid |
|
| 73 | - */ |
|
| 74 | - protected $_uid; |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * Simple Mail |
|
| 5 | + * |
|
| 6 | + * A simple PHP wrapper class for sending email using the mail() method. |
|
| 7 | + * |
|
| 8 | + * PHP version > 5.2 |
|
| 9 | + * |
|
| 10 | + * LICENSE: This source file is subject to the MIT license, which is |
|
| 11 | + * available through the world-wide-web at the following URI: |
|
| 12 | + * http://github.com/eoghanobrien/php-simple-mail/LICENCE.txt |
|
| 13 | + * |
|
| 14 | + * @category SimpleMail |
|
| 15 | + * @package SimpleMail |
|
| 16 | + * @author Eoghan O'Brien <[email protected]> |
|
| 17 | + * @copyright 2009 - 2017 Eoghan O'Brien |
|
| 18 | + * @license http://github.com/eoghanobrien/php-simple-mail/LICENCE.txt MIT |
|
| 19 | + * @version 1.7.1 |
|
| 20 | + * @link http://github.com/eoghanobrien/php-simple-mail |
|
| 21 | + */ |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Simple Mail class. |
|
| 25 | + * |
|
| 26 | + * @category SimpleMail |
|
| 27 | + * @package SimpleMail |
|
| 28 | + * @author Eoghan O'Brien <[email protected]> |
|
| 29 | + * @copyright 2009 - 2017 Eoghan O'Brien |
|
| 30 | + * @license http://github.com/eoghanobrien/php-simple-mail/LICENCE.txt MIT |
|
| 31 | + * @version 1.7.1 |
|
| 32 | + * @link http://github.com/eoghanobrien/php-simple-mail |
|
| 33 | + */ |
|
| 34 | + class Email |
|
| 35 | + { |
|
| 36 | + /** |
|
| 37 | + * @var int $_wrap |
|
| 38 | + */ |
|
| 39 | + protected $_wrap = 78; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var array $_to |
|
| 43 | + */ |
|
| 44 | + protected $_to = array(); |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var string $_subject |
|
| 48 | + */ |
|
| 49 | + protected $_subject; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @var string $_message |
|
| 53 | + */ |
|
| 54 | + protected $_message; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @var array $_headers |
|
| 58 | + */ |
|
| 59 | + protected $_headers = array(); |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @var string $_parameters |
|
| 63 | + */ |
|
| 64 | + protected $_params; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @var array $_attachments |
|
| 68 | + */ |
|
| 69 | + protected $_attachments = array(); |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @var string $_uid |
|
| 73 | + */ |
|
| 74 | + protected $_uid; |
|
| 75 | 75 | |
| 76 | - /** |
|
| 76 | + /** |
|
| 77 | 77 | * The logger instance |
| 78 | 78 | * @var Log |
| 79 | 79 | */ |
| 80 | - private $logger; |
|
| 80 | + private $logger; |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * __construct |
|
| 84 | - * |
|
| 85 | - * Resets the class properties. |
|
| 86 | - */ |
|
| 87 | - public function __construct() |
|
| 88 | - { |
|
| 89 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 82 | + /** |
|
| 83 | + * __construct |
|
| 84 | + * |
|
| 85 | + * Resets the class properties. |
|
| 86 | + */ |
|
| 87 | + public function __construct() |
|
| 88 | + { |
|
| 89 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 90 | 90 | $this->logger->setLogger('Library::Email'); |
| 91 | - $this->reset(); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * reset |
|
| 96 | - * |
|
| 97 | - * Resets all properties to initial state. |
|
| 98 | - * |
|
| 99 | - * @return self |
|
| 100 | - */ |
|
| 101 | - public function reset() |
|
| 102 | - { |
|
| 103 | - $this->_to = array(); |
|
| 104 | - $this->_headers = array(); |
|
| 105 | - $this->_subject = null; |
|
| 106 | - $this->_message = null; |
|
| 107 | - $this->_wrap = 78; |
|
| 108 | - $this->_params = null; |
|
| 109 | - $this->_attachments = array(); |
|
| 110 | - $this->_uid = $this->getUniqueId(); |
|
| 111 | - return $this; |
|
| 112 | - } |
|
| 91 | + $this->reset(); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * reset |
|
| 96 | + * |
|
| 97 | + * Resets all properties to initial state. |
|
| 98 | + * |
|
| 99 | + * @return self |
|
| 100 | + */ |
|
| 101 | + public function reset() |
|
| 102 | + { |
|
| 103 | + $this->_to = array(); |
|
| 104 | + $this->_headers = array(); |
|
| 105 | + $this->_subject = null; |
|
| 106 | + $this->_message = null; |
|
| 107 | + $this->_wrap = 78; |
|
| 108 | + $this->_params = null; |
|
| 109 | + $this->_attachments = array(); |
|
| 110 | + $this->_uid = $this->getUniqueId(); |
|
| 111 | + return $this; |
|
| 112 | + } |
|
| 113 | 113 | |
| 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 | - */ |
|
| 122 | - public function setFrom($email, $name = null) |
|
| 123 | - { |
|
| 124 | - $this->addMailHeader('From', (string) $email, (string) $name); |
|
| 125 | - return $this; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * setTo |
|
| 130 | - * |
|
| 131 | - * @param string $email The email address to send to. |
|
| 132 | - * @param string $name The name of the person to send to. |
|
| 133 | - * |
|
| 134 | - * @return self |
|
| 135 | - */ |
|
| 136 | - public function setTo($email, $name = null) |
|
| 137 | - { |
|
| 138 | - $this->_to[] = $this->formatHeader((string) $email, (string) $name); |
|
| 139 | - return $this; |
|
| 140 | - } |
|
| 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 | + */ |
|
| 122 | + public function setFrom($email, $name = null) |
|
| 123 | + { |
|
| 124 | + $this->addMailHeader('From', (string) $email, (string) $name); |
|
| 125 | + return $this; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * setTo |
|
| 130 | + * |
|
| 131 | + * @param string $email The email address to send to. |
|
| 132 | + * @param string $name The name of the person to send to. |
|
| 133 | + * |
|
| 134 | + * @return self |
|
| 135 | + */ |
|
| 136 | + public function setTo($email, $name = null) |
|
| 137 | + { |
|
| 138 | + $this->_to[] = $this->formatHeader((string) $email, (string) $name); |
|
| 139 | + return $this; |
|
| 140 | + } |
|
| 141 | 141 | |
| 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 | - */ |
|
| 148 | - public function setTos(array $emails) |
|
| 149 | - { |
|
| 150 | - foreach ($emails as $name => $email) { |
|
| 151 | - if(is_numeric($name)){ |
|
| 152 | - $this->setTo($email); |
|
| 153 | - } |
|
| 154 | - else{ |
|
| 155 | - $this->setTo($email, $name); |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - return $this; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * getTo |
|
| 164 | - * |
|
| 165 | - * Return an array of formatted To addresses. |
|
| 166 | - * |
|
| 167 | - * @return array |
|
| 168 | - */ |
|
| 169 | - public function getTo() |
|
| 170 | - { |
|
| 171 | - return $this->_to; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * setCc |
|
| 177 | - * |
|
| 178 | - * @param array $pairs An array of name => email pairs. |
|
| 179 | - * |
|
| 180 | - * @return self |
|
| 181 | - */ |
|
| 182 | - public function setCc(array $pairs) |
|
| 183 | - { |
|
| 184 | - return $this->addMailHeaders('Cc', $pairs); |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * setBcc |
|
| 189 | - * |
|
| 190 | - * @param array $pairs An array of name => email pairs. |
|
| 191 | - * |
|
| 192 | - * @return self |
|
| 193 | - */ |
|
| 194 | - public function setBcc(array $pairs) |
|
| 195 | - { |
|
| 196 | - return $this->addMailHeaders('Bcc', $pairs); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * setReplyTo |
|
| 201 | - * |
|
| 202 | - * @param string $email |
|
| 203 | - * @param string $name |
|
| 204 | - * |
|
| 205 | - * @return self |
|
| 206 | - */ |
|
| 207 | - public function setReplyTo($email, $name = null) |
|
| 208 | - { |
|
| 209 | - return $this->addMailHeader('Reply-To', $email, $name); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * setHtml |
|
| 214 | - * |
|
| 215 | - * @return self |
|
| 216 | - */ |
|
| 217 | - public function setHtml() |
|
| 218 | - { |
|
| 219 | - $this->addGenericHeader( |
|
| 220 | - 'Content-Type', 'text/html; charset="utf-8"' |
|
| 221 | - ); |
|
| 222 | - return $this; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * setSubject |
|
| 227 | - * |
|
| 228 | - * @param string $subject The email subject |
|
| 229 | - * |
|
| 230 | - * @return self |
|
| 231 | - */ |
|
| 232 | - public function setSubject($subject) |
|
| 233 | - { |
|
| 234 | - $this->_subject = $this->encodeUtf8( |
|
| 235 | - $this->filterOther((string) $subject) |
|
| 236 | - ); |
|
| 237 | - return $this; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * getSubject function. |
|
| 242 | - * |
|
| 243 | - * @return string |
|
| 244 | - */ |
|
| 245 | - public function getSubject() |
|
| 246 | - { |
|
| 247 | - return $this->_subject; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * setMessage |
|
| 252 | - * |
|
| 253 | - * @param string $message The message to send. |
|
| 254 | - * |
|
| 255 | - * @return self |
|
| 256 | - */ |
|
| 257 | - public function setMessage($message) |
|
| 258 | - { |
|
| 259 | - $this->_message = str_replace("\n.", "\n..", (string) $message); |
|
| 260 | - return $this; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * getMessage |
|
| 265 | - * |
|
| 266 | - * @return string |
|
| 267 | - */ |
|
| 268 | - public function getMessage() |
|
| 269 | - { |
|
| 270 | - return $this->_message; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * addAttachment |
|
| 275 | - * |
|
| 276 | - * @param string $path The file path to the attachment. |
|
| 277 | - * @param string $filename The filename of the attachment when emailed. |
|
| 278 | - * @param string $data |
|
| 279 | - * |
|
| 280 | - * @return self |
|
| 281 | - */ |
|
| 282 | - public function addAttachment($path, $filename = null, $data = null) |
|
| 283 | - { |
|
| 284 | - if(! file_exists($path)){ |
|
| 285 | - show_error('The file [' .$path. '] does not exists.'); |
|
| 286 | - } |
|
| 287 | - $filename = empty($filename) ? basename($path) : $filename; |
|
| 288 | - $filename = $this->encodeUtf8($this->filterOther((string) $filename)); |
|
| 289 | - $data = empty($data) ? $this->getAttachmentData($path) : $data; |
|
| 290 | - $this->_attachments[] = array( |
|
| 291 | - 'path' => $path, |
|
| 292 | - 'file' => $filename, |
|
| 293 | - 'data' => chunk_split(base64_encode($data)) |
|
| 294 | - ); |
|
| 295 | - return $this; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * getAttachmentData |
|
| 300 | - * |
|
| 301 | - * @param string $path The path to the attachment file. |
|
| 302 | - * |
|
| 303 | - * @return string |
|
| 304 | - */ |
|
| 305 | - public function getAttachmentData($path) |
|
| 306 | - { |
|
| 307 | - if(! file_exists($path)){ |
|
| 308 | - show_error('The file [' .$path. '] does not exists.'); |
|
| 309 | - } |
|
| 310 | - $filesize = filesize($path); |
|
| 311 | - $handle = fopen($path, "r"); |
|
| 312 | - $attachment = null; |
|
| 313 | - if(is_resource($handle)){ |
|
| 314 | - $attachment = fread($handle, $filesize); |
|
| 315 | - fclose($handle); |
|
| 316 | - } |
|
| 317 | - return $attachment; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * addMailHeader |
|
| 322 | - * |
|
| 323 | - * @param string $header The header to add. |
|
| 324 | - * @param string $email The email to add. |
|
| 325 | - * @param string $name The name to add. |
|
| 326 | - * |
|
| 327 | - * @return self |
|
| 328 | - */ |
|
| 329 | - public function addMailHeader($header, $email, $name = null) |
|
| 330 | - { |
|
| 331 | - $address = $this->formatHeader((string) $email, (string) $name); |
|
| 332 | - $this->_headers[] = sprintf('%s: %s', (string) $header, $address); |
|
| 333 | - return $this; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * addMailHeaders |
|
| 338 | - * |
|
| 339 | - * @param string $header The header to add. |
|
| 340 | - * @param array $pairs An array of name => email pairs. |
|
| 341 | - * |
|
| 342 | - * @return self |
|
| 343 | - */ |
|
| 344 | - public function addMailHeaders($header, array $pairs) |
|
| 345 | - { |
|
| 346 | - if (count($pairs) === 0) { |
|
| 347 | - show_error('You must pass at least one name => email pair.'); |
|
| 348 | - } |
|
| 349 | - $addresses = array(); |
|
| 350 | - foreach ($pairs as $name => $email) { |
|
| 351 | - $name = is_numeric($name) ? null : $name; |
|
| 352 | - $addresses[] = $this->formatHeader($email, $name); |
|
| 353 | - } |
|
| 354 | - $this->addGenericHeader($header, implode(',', $addresses)); |
|
| 355 | - return $this; |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * addGenericHeader |
|
| 360 | - * |
|
| 361 | - * @param string $name The generic header to add. |
|
| 362 | - * @param mixed $value The value of the header. |
|
| 363 | - * |
|
| 364 | - * @return self |
|
| 365 | - */ |
|
| 366 | - public function addGenericHeader($name, $value) |
|
| 367 | - { |
|
| 368 | - $this->_headers[] = sprintf( |
|
| 369 | - '%s: %s', |
|
| 370 | - (string) $name, |
|
| 371 | - (string) $value |
|
| 372 | - ); |
|
| 373 | - return $this; |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * getHeaders |
|
| 378 | - * |
|
| 379 | - * Return the headers registered so far as an array. |
|
| 380 | - * |
|
| 381 | - * @return array |
|
| 382 | - */ |
|
| 383 | - public function getHeaders() |
|
| 384 | - { |
|
| 385 | - return $this->_headers; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * setAdditionalParameters |
|
| 390 | - * |
|
| 391 | - * Such as "[email protected] |
|
| 392 | - * |
|
| 393 | - * @param string $additionalParameters The addition mail parameter. |
|
| 394 | - * |
|
| 395 | - * @return self |
|
| 396 | - */ |
|
| 397 | - public function setParameters($additionalParameters) |
|
| 398 | - { |
|
| 399 | - $this->_params = (string) $additionalParameters; |
|
| 400 | - return $this; |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * getAdditionalParameters |
|
| 405 | - * |
|
| 406 | - * @return string |
|
| 407 | - */ |
|
| 408 | - public function getParameters() |
|
| 409 | - { |
|
| 410 | - return $this->_params; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * setWrap |
|
| 415 | - * |
|
| 416 | - * @param int $wrap The number of characters at which the message will wrap. |
|
| 417 | - * |
|
| 418 | - * @return self |
|
| 419 | - */ |
|
| 420 | - public function setWrap($wrap = 78) |
|
| 421 | - { |
|
| 422 | - $wrap = (int) $wrap; |
|
| 423 | - if ($wrap < 1) { |
|
| 424 | - $wrap = 78; |
|
| 425 | - } |
|
| 426 | - $this->_wrap = $wrap; |
|
| 427 | - return $this; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - /** |
|
| 431 | - * getWrap |
|
| 432 | - * |
|
| 433 | - * @return int |
|
| 434 | - */ |
|
| 435 | - public function getWrap() |
|
| 436 | - { |
|
| 437 | - return $this->_wrap; |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - /** |
|
| 441 | - * hasAttachments |
|
| 442 | - * |
|
| 443 | - * Checks if the email has any registered attachments. |
|
| 444 | - * |
|
| 445 | - * @return bool |
|
| 446 | - */ |
|
| 447 | - public function hasAttachments() |
|
| 448 | - { |
|
| 449 | - return !empty($this->_attachments); |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * assembleAttachment |
|
| 454 | - * |
|
| 455 | - * @return string |
|
| 456 | - */ |
|
| 457 | - public function assembleAttachmentHeaders() |
|
| 458 | - { |
|
| 459 | - $head = array(); |
|
| 460 | - $head[] = "MIME-Version: 1.0"; |
|
| 461 | - $head[] = "Content-Type: multipart/mixed; boundary=\"{$this->_uid}\""; |
|
| 462 | - |
|
| 463 | - return join(PHP_EOL, $head); |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - /** |
|
| 467 | - * assembleAttachmentBody |
|
| 468 | - * |
|
| 469 | - * @return string |
|
| 470 | - */ |
|
| 471 | - public function assembleAttachmentBody() |
|
| 472 | - { |
|
| 473 | - $body = array(); |
|
| 474 | - $body[] = "This is a multi-part message in MIME format."; |
|
| 475 | - $body[] = "--{$this->_uid}"; |
|
| 476 | - $body[] = "Content-Type: text/html; charset=\"utf-8\""; |
|
| 477 | - $body[] = "Content-Transfer-Encoding: quoted-printable"; |
|
| 478 | - $body[] = ""; |
|
| 479 | - $body[] = quoted_printable_encode($this->_message); |
|
| 480 | - $body[] = ""; |
|
| 481 | - $body[] = "--{$this->_uid}"; |
|
| 482 | - |
|
| 483 | - foreach ($this->_attachments as $attachment) { |
|
| 484 | - $body[] = $this->getAttachmentMimeTemplate($attachment); |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - return implode(PHP_EOL, $body) . '--'; |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - /** |
|
| 491 | - * getAttachmentMimeTemplate |
|
| 492 | - * |
|
| 493 | - * @param array $attachment An array containing 'file' and 'data' keys. |
|
| 494 | - * |
|
| 495 | - * @return string |
|
| 496 | - */ |
|
| 497 | - public function getAttachmentMimeTemplate($attachment) |
|
| 498 | - { |
|
| 499 | - $file = $attachment['file']; |
|
| 500 | - $data = $attachment['data']; |
|
| 501 | - |
|
| 502 | - $head = array(); |
|
| 503 | - $head[] = "Content-Type: application/octet-stream; name=\"{$file}\""; |
|
| 504 | - $head[] = "Content-Transfer-Encoding: base64"; |
|
| 505 | - $head[] = "Content-Disposition: attachment; filename=\"{$file}\""; |
|
| 506 | - $head[] = ""; |
|
| 507 | - $head[] = $data; |
|
| 508 | - $head[] = ""; |
|
| 509 | - $head[] = "--{$this->_uid}"; |
|
| 510 | - |
|
| 511 | - return implode(PHP_EOL, $head); |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * send the email |
|
| 516 | - * |
|
| 517 | - * @return boolean |
|
| 518 | - */ |
|
| 519 | - public function send() |
|
| 520 | - { |
|
| 521 | - $to = $this->getToForSend(); |
|
| 522 | - $headers = $this->getHeadersForSend(); |
|
| 523 | - |
|
| 524 | - if (empty($to)) { |
|
| 525 | - show_error('Unable to send, no To address has been set.'); |
|
| 526 | - } |
|
| 527 | - |
|
| 528 | - if ($this->hasAttachments()) { |
|
| 529 | - $message = $this->assembleAttachmentBody(); |
|
| 530 | - $headers .= PHP_EOL . $this->assembleAttachmentHeaders(); |
|
| 531 | - } else { |
|
| 532 | - $message = $this->getWrapMessage(); |
|
| 533 | - } |
|
| 534 | - $this->logger->info('Sending new mail, the information are listed below: destination: ' . $to . ', headers: ' . $headers . ', message: ' . $message); |
|
| 535 | - return mail($to, $this->_subject, $message, $headers, $this->_params); |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - /** |
|
| 539 | - * debug |
|
| 540 | - * |
|
| 541 | - * @return string |
|
| 542 | - */ |
|
| 543 | - public function debug() |
|
| 544 | - { |
|
| 545 | - return '<pre>' . print_r($this, true) . '</pre>'; |
|
| 546 | - } |
|
| 547 | - |
|
| 548 | - /** |
|
| 549 | - * magic __toString function |
|
| 550 | - * |
|
| 551 | - * @return string |
|
| 552 | - */ |
|
| 553 | - public function __toString() |
|
| 554 | - { |
|
| 555 | - return print_r($this, true); |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - /** |
|
| 559 | - * formatHeader |
|
| 560 | - * |
|
| 561 | - * Formats a display address for emails according to RFC2822 e.g. |
|
| 562 | - * Name <[email protected]> |
|
| 563 | - * |
|
| 564 | - * @param string $email The email address. |
|
| 565 | - * @param string $name The display name. |
|
| 566 | - * |
|
| 567 | - * @return string |
|
| 568 | - */ |
|
| 569 | - public function formatHeader($email, $name = null) |
|
| 570 | - { |
|
| 571 | - $email = $this->filterEmail((string) $email); |
|
| 572 | - if (empty($name)) { |
|
| 573 | - return $email; |
|
| 574 | - } |
|
| 575 | - $name = $this->encodeUtf8($this->filterName((string) $name)); |
|
| 576 | - return sprintf('"%s" <%s>', $name, $email); |
|
| 577 | - } |
|
| 578 | - |
|
| 579 | - /** |
|
| 580 | - * encodeUtf8 |
|
| 581 | - * |
|
| 582 | - * @param string $value The value to encode. |
|
| 583 | - * |
|
| 584 | - * @return string |
|
| 585 | - */ |
|
| 586 | - public function encodeUtf8($value) |
|
| 587 | - { |
|
| 588 | - $value = trim($value); |
|
| 589 | - if (preg_match('/(\s)/', $value)) { |
|
| 590 | - return $this->encodeUtf8Words($value); |
|
| 591 | - } |
|
| 592 | - return $this->encodeUtf8Word($value); |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * encodeUtf8Word |
|
| 597 | - * |
|
| 598 | - * @param string $value The word to encode. |
|
| 599 | - * |
|
| 600 | - * @return string |
|
| 601 | - */ |
|
| 602 | - public function encodeUtf8Word($value) |
|
| 603 | - { |
|
| 604 | - return sprintf('=?UTF-8?B?%s?=', base64_encode($value)); |
|
| 605 | - } |
|
| 606 | - |
|
| 607 | - /** |
|
| 608 | - * encodeUtf8Words |
|
| 609 | - * |
|
| 610 | - * @param string $value The words to encode. |
|
| 611 | - * |
|
| 612 | - * @return string |
|
| 613 | - */ |
|
| 614 | - public function encodeUtf8Words($value) |
|
| 615 | - { |
|
| 616 | - $words = explode(' ', $value); |
|
| 617 | - $encoded = array(); |
|
| 618 | - foreach ($words as $word) { |
|
| 619 | - $encoded[] = $this->encodeUtf8Word($word); |
|
| 620 | - } |
|
| 621 | - return join($this->encodeUtf8Word(' '), $encoded); |
|
| 622 | - } |
|
| 623 | - |
|
| 624 | - /** |
|
| 625 | - * filterEmail |
|
| 626 | - * |
|
| 627 | - * Removes any carriage return, line feed, tab, double quote, comma |
|
| 628 | - * and angle bracket characters before sanitizing the email address. |
|
| 629 | - * |
|
| 630 | - * @param string $email The email to filter. |
|
| 631 | - * |
|
| 632 | - * @return string |
|
| 633 | - */ |
|
| 634 | - public function filterEmail($email) |
|
| 635 | - { |
|
| 636 | - $rule = array( |
|
| 637 | - "\r" => '', |
|
| 638 | - "\n" => '', |
|
| 639 | - "\t" => '', |
|
| 640 | - '"' => '', |
|
| 641 | - ',' => '', |
|
| 642 | - '<' => '', |
|
| 643 | - '>' => '' |
|
| 644 | - ); |
|
| 645 | - $email = strtr($email, $rule); |
|
| 646 | - $email = filter_var($email, FILTER_SANITIZE_EMAIL); |
|
| 647 | - return $email; |
|
| 648 | - } |
|
| 649 | - |
|
| 650 | - /** |
|
| 651 | - * filterName |
|
| 652 | - * |
|
| 653 | - * Removes any carriage return, line feed or tab characters. Replaces |
|
| 654 | - * double quotes with single quotes and angle brackets with square |
|
| 655 | - * brackets, before sanitizing the string and stripping out html tags. |
|
| 656 | - * |
|
| 657 | - * @param string $name The name to filter. |
|
| 658 | - * |
|
| 659 | - * @return string |
|
| 660 | - */ |
|
| 661 | - public function filterName($name) |
|
| 662 | - { |
|
| 663 | - $rule = array( |
|
| 664 | - "\r" => '', |
|
| 665 | - "\n" => '', |
|
| 666 | - "\t" => '', |
|
| 667 | - '"' => "'", |
|
| 668 | - '<' => '[', |
|
| 669 | - '>' => ']', |
|
| 670 | - ); |
|
| 671 | - $filtered = filter_var( |
|
| 672 | - $name, |
|
| 673 | - FILTER_SANITIZE_STRING, |
|
| 674 | - FILTER_FLAG_NO_ENCODE_QUOTES |
|
| 675 | - ); |
|
| 676 | - return trim(strtr($filtered, $rule)); |
|
| 677 | - } |
|
| 678 | - |
|
| 679 | - /** |
|
| 680 | - * filterOther |
|
| 681 | - * |
|
| 682 | - * Removes ASCII control characters including any carriage return, line |
|
| 683 | - * feed or tab characters. |
|
| 684 | - * |
|
| 685 | - * @param string $data The data to filter. |
|
| 686 | - * |
|
| 687 | - * @return string |
|
| 688 | - */ |
|
| 689 | - public function filterOther($data) |
|
| 690 | - { |
|
| 691 | - return filter_var($data, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW); |
|
| 692 | - } |
|
| 693 | - |
|
| 694 | - /** |
|
| 695 | - * getHeadersForSend |
|
| 696 | - * |
|
| 697 | - * @return string |
|
| 698 | - */ |
|
| 699 | - public function getHeadersForSend() |
|
| 700 | - { |
|
| 701 | - if (empty($this->_headers)) { |
|
| 702 | - return ''; |
|
| 703 | - } |
|
| 704 | - return join(PHP_EOL, $this->_headers); |
|
| 705 | - } |
|
| 706 | - |
|
| 707 | - /** |
|
| 708 | - * getToForSend |
|
| 709 | - * |
|
| 710 | - * @return string |
|
| 711 | - */ |
|
| 712 | - public function getToForSend() |
|
| 713 | - { |
|
| 714 | - if (empty($this->_to)) { |
|
| 715 | - return ''; |
|
| 716 | - } |
|
| 717 | - return join(', ', $this->_to); |
|
| 718 | - } |
|
| 719 | - |
|
| 720 | - /** |
|
| 721 | - * getUniqueId |
|
| 722 | - * |
|
| 723 | - * @return string |
|
| 724 | - */ |
|
| 725 | - public function getUniqueId() |
|
| 726 | - { |
|
| 727 | - return md5(uniqid(time())); |
|
| 728 | - } |
|
| 729 | - |
|
| 730 | - /** |
|
| 731 | - * getWrapMessage |
|
| 732 | - * |
|
| 733 | - * @return string |
|
| 734 | - */ |
|
| 735 | - public function getWrapMessage() |
|
| 736 | - { |
|
| 737 | - return wordwrap($this->_message, $this->_wrap); |
|
| 738 | - } |
|
| 739 | - } |
|
| 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 | + */ |
|
| 148 | + public function setTos(array $emails) |
|
| 149 | + { |
|
| 150 | + foreach ($emails as $name => $email) { |
|
| 151 | + if(is_numeric($name)){ |
|
| 152 | + $this->setTo($email); |
|
| 153 | + } |
|
| 154 | + else{ |
|
| 155 | + $this->setTo($email, $name); |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + return $this; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * getTo |
|
| 164 | + * |
|
| 165 | + * Return an array of formatted To addresses. |
|
| 166 | + * |
|
| 167 | + * @return array |
|
| 168 | + */ |
|
| 169 | + public function getTo() |
|
| 170 | + { |
|
| 171 | + return $this->_to; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * setCc |
|
| 177 | + * |
|
| 178 | + * @param array $pairs An array of name => email pairs. |
|
| 179 | + * |
|
| 180 | + * @return self |
|
| 181 | + */ |
|
| 182 | + public function setCc(array $pairs) |
|
| 183 | + { |
|
| 184 | + return $this->addMailHeaders('Cc', $pairs); |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * setBcc |
|
| 189 | + * |
|
| 190 | + * @param array $pairs An array of name => email pairs. |
|
| 191 | + * |
|
| 192 | + * @return self |
|
| 193 | + */ |
|
| 194 | + public function setBcc(array $pairs) |
|
| 195 | + { |
|
| 196 | + return $this->addMailHeaders('Bcc', $pairs); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * setReplyTo |
|
| 201 | + * |
|
| 202 | + * @param string $email |
|
| 203 | + * @param string $name |
|
| 204 | + * |
|
| 205 | + * @return self |
|
| 206 | + */ |
|
| 207 | + public function setReplyTo($email, $name = null) |
|
| 208 | + { |
|
| 209 | + return $this->addMailHeader('Reply-To', $email, $name); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * setHtml |
|
| 214 | + * |
|
| 215 | + * @return self |
|
| 216 | + */ |
|
| 217 | + public function setHtml() |
|
| 218 | + { |
|
| 219 | + $this->addGenericHeader( |
|
| 220 | + 'Content-Type', 'text/html; charset="utf-8"' |
|
| 221 | + ); |
|
| 222 | + return $this; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * setSubject |
|
| 227 | + * |
|
| 228 | + * @param string $subject The email subject |
|
| 229 | + * |
|
| 230 | + * @return self |
|
| 231 | + */ |
|
| 232 | + public function setSubject($subject) |
|
| 233 | + { |
|
| 234 | + $this->_subject = $this->encodeUtf8( |
|
| 235 | + $this->filterOther((string) $subject) |
|
| 236 | + ); |
|
| 237 | + return $this; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * getSubject function. |
|
| 242 | + * |
|
| 243 | + * @return string |
|
| 244 | + */ |
|
| 245 | + public function getSubject() |
|
| 246 | + { |
|
| 247 | + return $this->_subject; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * setMessage |
|
| 252 | + * |
|
| 253 | + * @param string $message The message to send. |
|
| 254 | + * |
|
| 255 | + * @return self |
|
| 256 | + */ |
|
| 257 | + public function setMessage($message) |
|
| 258 | + { |
|
| 259 | + $this->_message = str_replace("\n.", "\n..", (string) $message); |
|
| 260 | + return $this; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * getMessage |
|
| 265 | + * |
|
| 266 | + * @return string |
|
| 267 | + */ |
|
| 268 | + public function getMessage() |
|
| 269 | + { |
|
| 270 | + return $this->_message; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * addAttachment |
|
| 275 | + * |
|
| 276 | + * @param string $path The file path to the attachment. |
|
| 277 | + * @param string $filename The filename of the attachment when emailed. |
|
| 278 | + * @param string $data |
|
| 279 | + * |
|
| 280 | + * @return self |
|
| 281 | + */ |
|
| 282 | + public function addAttachment($path, $filename = null, $data = null) |
|
| 283 | + { |
|
| 284 | + if(! file_exists($path)){ |
|
| 285 | + show_error('The file [' .$path. '] does not exists.'); |
|
| 286 | + } |
|
| 287 | + $filename = empty($filename) ? basename($path) : $filename; |
|
| 288 | + $filename = $this->encodeUtf8($this->filterOther((string) $filename)); |
|
| 289 | + $data = empty($data) ? $this->getAttachmentData($path) : $data; |
|
| 290 | + $this->_attachments[] = array( |
|
| 291 | + 'path' => $path, |
|
| 292 | + 'file' => $filename, |
|
| 293 | + 'data' => chunk_split(base64_encode($data)) |
|
| 294 | + ); |
|
| 295 | + return $this; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * getAttachmentData |
|
| 300 | + * |
|
| 301 | + * @param string $path The path to the attachment file. |
|
| 302 | + * |
|
| 303 | + * @return string |
|
| 304 | + */ |
|
| 305 | + public function getAttachmentData($path) |
|
| 306 | + { |
|
| 307 | + if(! file_exists($path)){ |
|
| 308 | + show_error('The file [' .$path. '] does not exists.'); |
|
| 309 | + } |
|
| 310 | + $filesize = filesize($path); |
|
| 311 | + $handle = fopen($path, "r"); |
|
| 312 | + $attachment = null; |
|
| 313 | + if(is_resource($handle)){ |
|
| 314 | + $attachment = fread($handle, $filesize); |
|
| 315 | + fclose($handle); |
|
| 316 | + } |
|
| 317 | + return $attachment; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * addMailHeader |
|
| 322 | + * |
|
| 323 | + * @param string $header The header to add. |
|
| 324 | + * @param string $email The email to add. |
|
| 325 | + * @param string $name The name to add. |
|
| 326 | + * |
|
| 327 | + * @return self |
|
| 328 | + */ |
|
| 329 | + public function addMailHeader($header, $email, $name = null) |
|
| 330 | + { |
|
| 331 | + $address = $this->formatHeader((string) $email, (string) $name); |
|
| 332 | + $this->_headers[] = sprintf('%s: %s', (string) $header, $address); |
|
| 333 | + return $this; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * addMailHeaders |
|
| 338 | + * |
|
| 339 | + * @param string $header The header to add. |
|
| 340 | + * @param array $pairs An array of name => email pairs. |
|
| 341 | + * |
|
| 342 | + * @return self |
|
| 343 | + */ |
|
| 344 | + public function addMailHeaders($header, array $pairs) |
|
| 345 | + { |
|
| 346 | + if (count($pairs) === 0) { |
|
| 347 | + show_error('You must pass at least one name => email pair.'); |
|
| 348 | + } |
|
| 349 | + $addresses = array(); |
|
| 350 | + foreach ($pairs as $name => $email) { |
|
| 351 | + $name = is_numeric($name) ? null : $name; |
|
| 352 | + $addresses[] = $this->formatHeader($email, $name); |
|
| 353 | + } |
|
| 354 | + $this->addGenericHeader($header, implode(',', $addresses)); |
|
| 355 | + return $this; |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * addGenericHeader |
|
| 360 | + * |
|
| 361 | + * @param string $name The generic header to add. |
|
| 362 | + * @param mixed $value The value of the header. |
|
| 363 | + * |
|
| 364 | + * @return self |
|
| 365 | + */ |
|
| 366 | + public function addGenericHeader($name, $value) |
|
| 367 | + { |
|
| 368 | + $this->_headers[] = sprintf( |
|
| 369 | + '%s: %s', |
|
| 370 | + (string) $name, |
|
| 371 | + (string) $value |
|
| 372 | + ); |
|
| 373 | + return $this; |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * getHeaders |
|
| 378 | + * |
|
| 379 | + * Return the headers registered so far as an array. |
|
| 380 | + * |
|
| 381 | + * @return array |
|
| 382 | + */ |
|
| 383 | + public function getHeaders() |
|
| 384 | + { |
|
| 385 | + return $this->_headers; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * setAdditionalParameters |
|
| 390 | + * |
|
| 391 | + * Such as "[email protected] |
|
| 392 | + * |
|
| 393 | + * @param string $additionalParameters The addition mail parameter. |
|
| 394 | + * |
|
| 395 | + * @return self |
|
| 396 | + */ |
|
| 397 | + public function setParameters($additionalParameters) |
|
| 398 | + { |
|
| 399 | + $this->_params = (string) $additionalParameters; |
|
| 400 | + return $this; |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + /** |
|
| 404 | + * getAdditionalParameters |
|
| 405 | + * |
|
| 406 | + * @return string |
|
| 407 | + */ |
|
| 408 | + public function getParameters() |
|
| 409 | + { |
|
| 410 | + return $this->_params; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + /** |
|
| 414 | + * setWrap |
|
| 415 | + * |
|
| 416 | + * @param int $wrap The number of characters at which the message will wrap. |
|
| 417 | + * |
|
| 418 | + * @return self |
|
| 419 | + */ |
|
| 420 | + public function setWrap($wrap = 78) |
|
| 421 | + { |
|
| 422 | + $wrap = (int) $wrap; |
|
| 423 | + if ($wrap < 1) { |
|
| 424 | + $wrap = 78; |
|
| 425 | + } |
|
| 426 | + $this->_wrap = $wrap; |
|
| 427 | + return $this; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + /** |
|
| 431 | + * getWrap |
|
| 432 | + * |
|
| 433 | + * @return int |
|
| 434 | + */ |
|
| 435 | + public function getWrap() |
|
| 436 | + { |
|
| 437 | + return $this->_wrap; |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + /** |
|
| 441 | + * hasAttachments |
|
| 442 | + * |
|
| 443 | + * Checks if the email has any registered attachments. |
|
| 444 | + * |
|
| 445 | + * @return bool |
|
| 446 | + */ |
|
| 447 | + public function hasAttachments() |
|
| 448 | + { |
|
| 449 | + return !empty($this->_attachments); |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * assembleAttachment |
|
| 454 | + * |
|
| 455 | + * @return string |
|
| 456 | + */ |
|
| 457 | + public function assembleAttachmentHeaders() |
|
| 458 | + { |
|
| 459 | + $head = array(); |
|
| 460 | + $head[] = "MIME-Version: 1.0"; |
|
| 461 | + $head[] = "Content-Type: multipart/mixed; boundary=\"{$this->_uid}\""; |
|
| 462 | + |
|
| 463 | + return join(PHP_EOL, $head); |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + /** |
|
| 467 | + * assembleAttachmentBody |
|
| 468 | + * |
|
| 469 | + * @return string |
|
| 470 | + */ |
|
| 471 | + public function assembleAttachmentBody() |
|
| 472 | + { |
|
| 473 | + $body = array(); |
|
| 474 | + $body[] = "This is a multi-part message in MIME format."; |
|
| 475 | + $body[] = "--{$this->_uid}"; |
|
| 476 | + $body[] = "Content-Type: text/html; charset=\"utf-8\""; |
|
| 477 | + $body[] = "Content-Transfer-Encoding: quoted-printable"; |
|
| 478 | + $body[] = ""; |
|
| 479 | + $body[] = quoted_printable_encode($this->_message); |
|
| 480 | + $body[] = ""; |
|
| 481 | + $body[] = "--{$this->_uid}"; |
|
| 482 | + |
|
| 483 | + foreach ($this->_attachments as $attachment) { |
|
| 484 | + $body[] = $this->getAttachmentMimeTemplate($attachment); |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + return implode(PHP_EOL, $body) . '--'; |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + /** |
|
| 491 | + * getAttachmentMimeTemplate |
|
| 492 | + * |
|
| 493 | + * @param array $attachment An array containing 'file' and 'data' keys. |
|
| 494 | + * |
|
| 495 | + * @return string |
|
| 496 | + */ |
|
| 497 | + public function getAttachmentMimeTemplate($attachment) |
|
| 498 | + { |
|
| 499 | + $file = $attachment['file']; |
|
| 500 | + $data = $attachment['data']; |
|
| 501 | + |
|
| 502 | + $head = array(); |
|
| 503 | + $head[] = "Content-Type: application/octet-stream; name=\"{$file}\""; |
|
| 504 | + $head[] = "Content-Transfer-Encoding: base64"; |
|
| 505 | + $head[] = "Content-Disposition: attachment; filename=\"{$file}\""; |
|
| 506 | + $head[] = ""; |
|
| 507 | + $head[] = $data; |
|
| 508 | + $head[] = ""; |
|
| 509 | + $head[] = "--{$this->_uid}"; |
|
| 510 | + |
|
| 511 | + return implode(PHP_EOL, $head); |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * send the email |
|
| 516 | + * |
|
| 517 | + * @return boolean |
|
| 518 | + */ |
|
| 519 | + public function send() |
|
| 520 | + { |
|
| 521 | + $to = $this->getToForSend(); |
|
| 522 | + $headers = $this->getHeadersForSend(); |
|
| 523 | + |
|
| 524 | + if (empty($to)) { |
|
| 525 | + show_error('Unable to send, no To address has been set.'); |
|
| 526 | + } |
|
| 527 | + |
|
| 528 | + if ($this->hasAttachments()) { |
|
| 529 | + $message = $this->assembleAttachmentBody(); |
|
| 530 | + $headers .= PHP_EOL . $this->assembleAttachmentHeaders(); |
|
| 531 | + } else { |
|
| 532 | + $message = $this->getWrapMessage(); |
|
| 533 | + } |
|
| 534 | + $this->logger->info('Sending new mail, the information are listed below: destination: ' . $to . ', headers: ' . $headers . ', message: ' . $message); |
|
| 535 | + return mail($to, $this->_subject, $message, $headers, $this->_params); |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + /** |
|
| 539 | + * debug |
|
| 540 | + * |
|
| 541 | + * @return string |
|
| 542 | + */ |
|
| 543 | + public function debug() |
|
| 544 | + { |
|
| 545 | + return '<pre>' . print_r($this, true) . '</pre>'; |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + /** |
|
| 549 | + * magic __toString function |
|
| 550 | + * |
|
| 551 | + * @return string |
|
| 552 | + */ |
|
| 553 | + public function __toString() |
|
| 554 | + { |
|
| 555 | + return print_r($this, true); |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + /** |
|
| 559 | + * formatHeader |
|
| 560 | + * |
|
| 561 | + * Formats a display address for emails according to RFC2822 e.g. |
|
| 562 | + * Name <[email protected]> |
|
| 563 | + * |
|
| 564 | + * @param string $email The email address. |
|
| 565 | + * @param string $name The display name. |
|
| 566 | + * |
|
| 567 | + * @return string |
|
| 568 | + */ |
|
| 569 | + public function formatHeader($email, $name = null) |
|
| 570 | + { |
|
| 571 | + $email = $this->filterEmail((string) $email); |
|
| 572 | + if (empty($name)) { |
|
| 573 | + return $email; |
|
| 574 | + } |
|
| 575 | + $name = $this->encodeUtf8($this->filterName((string) $name)); |
|
| 576 | + return sprintf('"%s" <%s>', $name, $email); |
|
| 577 | + } |
|
| 578 | + |
|
| 579 | + /** |
|
| 580 | + * encodeUtf8 |
|
| 581 | + * |
|
| 582 | + * @param string $value The value to encode. |
|
| 583 | + * |
|
| 584 | + * @return string |
|
| 585 | + */ |
|
| 586 | + public function encodeUtf8($value) |
|
| 587 | + { |
|
| 588 | + $value = trim($value); |
|
| 589 | + if (preg_match('/(\s)/', $value)) { |
|
| 590 | + return $this->encodeUtf8Words($value); |
|
| 591 | + } |
|
| 592 | + return $this->encodeUtf8Word($value); |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * encodeUtf8Word |
|
| 597 | + * |
|
| 598 | + * @param string $value The word to encode. |
|
| 599 | + * |
|
| 600 | + * @return string |
|
| 601 | + */ |
|
| 602 | + public function encodeUtf8Word($value) |
|
| 603 | + { |
|
| 604 | + return sprintf('=?UTF-8?B?%s?=', base64_encode($value)); |
|
| 605 | + } |
|
| 606 | + |
|
| 607 | + /** |
|
| 608 | + * encodeUtf8Words |
|
| 609 | + * |
|
| 610 | + * @param string $value The words to encode. |
|
| 611 | + * |
|
| 612 | + * @return string |
|
| 613 | + */ |
|
| 614 | + public function encodeUtf8Words($value) |
|
| 615 | + { |
|
| 616 | + $words = explode(' ', $value); |
|
| 617 | + $encoded = array(); |
|
| 618 | + foreach ($words as $word) { |
|
| 619 | + $encoded[] = $this->encodeUtf8Word($word); |
|
| 620 | + } |
|
| 621 | + return join($this->encodeUtf8Word(' '), $encoded); |
|
| 622 | + } |
|
| 623 | + |
|
| 624 | + /** |
|
| 625 | + * filterEmail |
|
| 626 | + * |
|
| 627 | + * Removes any carriage return, line feed, tab, double quote, comma |
|
| 628 | + * and angle bracket characters before sanitizing the email address. |
|
| 629 | + * |
|
| 630 | + * @param string $email The email to filter. |
|
| 631 | + * |
|
| 632 | + * @return string |
|
| 633 | + */ |
|
| 634 | + public function filterEmail($email) |
|
| 635 | + { |
|
| 636 | + $rule = array( |
|
| 637 | + "\r" => '', |
|
| 638 | + "\n" => '', |
|
| 639 | + "\t" => '', |
|
| 640 | + '"' => '', |
|
| 641 | + ',' => '', |
|
| 642 | + '<' => '', |
|
| 643 | + '>' => '' |
|
| 644 | + ); |
|
| 645 | + $email = strtr($email, $rule); |
|
| 646 | + $email = filter_var($email, FILTER_SANITIZE_EMAIL); |
|
| 647 | + return $email; |
|
| 648 | + } |
|
| 649 | + |
|
| 650 | + /** |
|
| 651 | + * filterName |
|
| 652 | + * |
|
| 653 | + * Removes any carriage return, line feed or tab characters. Replaces |
|
| 654 | + * double quotes with single quotes and angle brackets with square |
|
| 655 | + * brackets, before sanitizing the string and stripping out html tags. |
|
| 656 | + * |
|
| 657 | + * @param string $name The name to filter. |
|
| 658 | + * |
|
| 659 | + * @return string |
|
| 660 | + */ |
|
| 661 | + public function filterName($name) |
|
| 662 | + { |
|
| 663 | + $rule = array( |
|
| 664 | + "\r" => '', |
|
| 665 | + "\n" => '', |
|
| 666 | + "\t" => '', |
|
| 667 | + '"' => "'", |
|
| 668 | + '<' => '[', |
|
| 669 | + '>' => ']', |
|
| 670 | + ); |
|
| 671 | + $filtered = filter_var( |
|
| 672 | + $name, |
|
| 673 | + FILTER_SANITIZE_STRING, |
|
| 674 | + FILTER_FLAG_NO_ENCODE_QUOTES |
|
| 675 | + ); |
|
| 676 | + return trim(strtr($filtered, $rule)); |
|
| 677 | + } |
|
| 678 | + |
|
| 679 | + /** |
|
| 680 | + * filterOther |
|
| 681 | + * |
|
| 682 | + * Removes ASCII control characters including any carriage return, line |
|
| 683 | + * feed or tab characters. |
|
| 684 | + * |
|
| 685 | + * @param string $data The data to filter. |
|
| 686 | + * |
|
| 687 | + * @return string |
|
| 688 | + */ |
|
| 689 | + public function filterOther($data) |
|
| 690 | + { |
|
| 691 | + return filter_var($data, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW); |
|
| 692 | + } |
|
| 693 | + |
|
| 694 | + /** |
|
| 695 | + * getHeadersForSend |
|
| 696 | + * |
|
| 697 | + * @return string |
|
| 698 | + */ |
|
| 699 | + public function getHeadersForSend() |
|
| 700 | + { |
|
| 701 | + if (empty($this->_headers)) { |
|
| 702 | + return ''; |
|
| 703 | + } |
|
| 704 | + return join(PHP_EOL, $this->_headers); |
|
| 705 | + } |
|
| 706 | + |
|
| 707 | + /** |
|
| 708 | + * getToForSend |
|
| 709 | + * |
|
| 710 | + * @return string |
|
| 711 | + */ |
|
| 712 | + public function getToForSend() |
|
| 713 | + { |
|
| 714 | + if (empty($this->_to)) { |
|
| 715 | + return ''; |
|
| 716 | + } |
|
| 717 | + return join(', ', $this->_to); |
|
| 718 | + } |
|
| 719 | + |
|
| 720 | + /** |
|
| 721 | + * getUniqueId |
|
| 722 | + * |
|
| 723 | + * @return string |
|
| 724 | + */ |
|
| 725 | + public function getUniqueId() |
|
| 726 | + { |
|
| 727 | + return md5(uniqid(time())); |
|
| 728 | + } |
|
| 729 | + |
|
| 730 | + /** |
|
| 731 | + * getWrapMessage |
|
| 732 | + * |
|
| 733 | + * @return string |
|
| 734 | + */ |
|
| 735 | + public function getWrapMessage() |
|
| 736 | + { |
|
| 737 | + return wordwrap($this->_message, $this->_wrap); |
|
| 738 | + } |
|
| 739 | + } |
|
@@ -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 | } |
@@ -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(self::$logger == null){ |
|
| 41 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 39 | + private static function getLogger() { |
|
| 40 | + if (self::$logger == null) { |
|
| 41 | + self::$logger[0] = & class_loader('Log', 'classes'); |
|
| 42 | 42 | self::$logger[0]->setLogger('Library::Cookie'); |
| 43 | 43 | } |
| 44 | 44 | return self::$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 = self::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 = self::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 | |
@@ -1,112 +1,112 @@ |
||
| 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 | - */ |
|
| 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 | 26 | |
| 27 | - class Cookie{ |
|
| 27 | + class Cookie{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The logger instance |
|
| 31 | - * @var Log |
|
| 32 | - */ |
|
| 33 | - private static $logger; |
|
| 29 | + /** |
|
| 30 | + * The logger instance |
|
| 31 | + * @var Log |
|
| 32 | + */ |
|
| 33 | + private static $logger; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * The signleton of the logger |
|
| 37 | - * @return Object the Log instance |
|
| 38 | - */ |
|
| 39 | - private static function getLogger(){ |
|
| 40 | - if(self::$logger == null){ |
|
| 41 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 42 | - self::$logger[0]->setLogger('Library::Cookie'); |
|
| 43 | - } |
|
| 44 | - return self::$logger[0]; |
|
| 45 | - } |
|
| 35 | + /** |
|
| 36 | + * The signleton of the logger |
|
| 37 | + * @return Object the Log instance |
|
| 38 | + */ |
|
| 39 | + private static function getLogger(){ |
|
| 40 | + if(self::$logger == null){ |
|
| 41 | + self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 42 | + self::$logger[0]->setLogger('Library::Cookie'); |
|
| 43 | + } |
|
| 44 | + return self::$logger[0]; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Get the cookie item value |
|
| 49 | - * @param string $item the cookie item name to get |
|
| 50 | - * @param mixed $default the default value to use if can not find the cokkie item in the list |
|
| 51 | - * @return mixed the cookie value if exist or the default value |
|
| 52 | - */ |
|
| 53 | - public static function get($item, $default = null){ |
|
| 54 | - $logger = self::getLogger(); |
|
| 55 | - if(array_key_exists($item, $_COOKIE)){ |
|
| 56 | - return $_COOKIE[$item]; |
|
| 57 | - } |
|
| 58 | - $logger->warning('Cannot find cookie item [' . $item . '], using the default value [' . $default . ']'); |
|
| 59 | - return $default; |
|
| 60 | - } |
|
| 47 | + /** |
|
| 48 | + * Get the cookie item value |
|
| 49 | + * @param string $item the cookie item name to get |
|
| 50 | + * @param mixed $default the default value to use if can not find the cokkie item in the list |
|
| 51 | + * @return mixed the cookie value if exist or the default value |
|
| 52 | + */ |
|
| 53 | + public static function get($item, $default = null){ |
|
| 54 | + $logger = self::getLogger(); |
|
| 55 | + if(array_key_exists($item, $_COOKIE)){ |
|
| 56 | + return $_COOKIE[$item]; |
|
| 57 | + } |
|
| 58 | + $logger->warning('Cannot find cookie item [' . $item . '], using the default value [' . $default . ']'); |
|
| 59 | + return $default; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Set the cookie item value |
|
| 64 | - * @param string $name the cookie item name |
|
| 65 | - * @param string $value the cookie value to set |
|
| 66 | - * @param integer $expire the time to live for this cookie |
|
| 67 | - * @param string $path the path that the cookie will be available |
|
| 68 | - * @param string $domain the domain that the cookie will be available |
|
| 69 | - * @param boolean $secure if this cookie will be available on secure connection or not |
|
| 70 | - * @param boolean $httponly if this cookie will be available under HTTP protocol. |
|
| 71 | - */ |
|
| 72 | - public static function set($name, $value = '', $expire = 0, $path = '/', $domain = '', $secure = false, $httponly = false){ |
|
| 73 | - if(headers_sent()){ |
|
| 74 | - show_error('There exists a cookie that we wanted to create that we couldn\'t |
|
| 62 | + /** |
|
| 63 | + * Set the cookie item value |
|
| 64 | + * @param string $name the cookie item name |
|
| 65 | + * @param string $value the cookie value to set |
|
| 66 | + * @param integer $expire the time to live for this cookie |
|
| 67 | + * @param string $path the path that the cookie will be available |
|
| 68 | + * @param string $domain the domain that the cookie will be available |
|
| 69 | + * @param boolean $secure if this cookie will be available on secure connection or not |
|
| 70 | + * @param boolean $httponly if this cookie will be available under HTTP protocol. |
|
| 71 | + */ |
|
| 72 | + public static function set($name, $value = '', $expire = 0, $path = '/', $domain = '', $secure = false, $httponly = false){ |
|
| 73 | + if(headers_sent()){ |
|
| 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 | - } |
|
| 78 | - $timestamp = $expire; |
|
| 79 | - if($expire){ |
|
| 80 | - $timestamp = time() + $expire; |
|
| 81 | - } |
|
| 82 | - setcookie($name, $value, $timestamp, $path, $domain, $secure, $httponly); |
|
| 83 | - } |
|
| 77 | + } |
|
| 78 | + $timestamp = $expire; |
|
| 79 | + if($expire){ |
|
| 80 | + $timestamp = time() + $expire; |
|
| 81 | + } |
|
| 82 | + setcookie($name, $value, $timestamp, $path, $domain, $secure, $httponly); |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * Delete the cookie item in the list |
|
| 87 | - * @param string $item the cookie item name to be cleared |
|
| 88 | - * @return boolean true if the item exists and is deleted successfully otherwise will return false. |
|
| 89 | - */ |
|
| 90 | - public static function delete($item){ |
|
| 91 | - $logger = self::getLogger(); |
|
| 92 | - if(array_key_exists($item, $_COOKIE)){ |
|
| 93 | - $logger->info('Delete cookie item ['.$item.']'); |
|
| 94 | - unset($_COOKIE[$item]); |
|
| 95 | - return true; |
|
| 96 | - } |
|
| 97 | - else{ |
|
| 98 | - $logger->warning('Cookie item ['.$item.'] to be deleted does not exists'); |
|
| 99 | - return false; |
|
| 100 | - } |
|
| 101 | - } |
|
| 85 | + /** |
|
| 86 | + * Delete the cookie item in the list |
|
| 87 | + * @param string $item the cookie item name to be cleared |
|
| 88 | + * @return boolean true if the item exists and is deleted successfully otherwise will return false. |
|
| 89 | + */ |
|
| 90 | + public static function delete($item){ |
|
| 91 | + $logger = self::getLogger(); |
|
| 92 | + if(array_key_exists($item, $_COOKIE)){ |
|
| 93 | + $logger->info('Delete cookie item ['.$item.']'); |
|
| 94 | + unset($_COOKIE[$item]); |
|
| 95 | + return true; |
|
| 96 | + } |
|
| 97 | + else{ |
|
| 98 | + $logger->warning('Cookie item ['.$item.'] to be deleted does not exists'); |
|
| 99 | + return false; |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * Check if the given cookie item exists |
|
| 105 | - * @param string $item the cookie item name |
|
| 106 | - * @return boolean true if the cookie item is set, false or not |
|
| 107 | - */ |
|
| 108 | - public static function exists($item){ |
|
| 109 | - return array_key_exists($item, $_COOKIE); |
|
| 110 | - } |
|
| 103 | + /** |
|
| 104 | + * Check if the given cookie item exists |
|
| 105 | + * @param string $item the cookie item name |
|
| 106 | + * @return boolean true if the cookie item is set, false or not |
|
| 107 | + */ |
|
| 108 | + public static function exists($item){ |
|
| 109 | + return array_key_exists($item, $_COOKIE); |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - } |
|
| 112 | + } |
|
@@ -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'; |
@@ -1,65 +1,65 @@ |
||
| 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 | - */ |
|
| 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 | 26 | |
| 27 | - class StringHash{ |
|
| 27 | + class StringHash{ |
|
| 28 | 28 | |
| 29 | - //blowfish |
|
| 30 | - private static $algo = '$2a'; |
|
| 29 | + //blowfish |
|
| 30 | + private static $algo = '$2a'; |
|
| 31 | 31 | |
| 32 | - //cost parameter |
|
| 33 | - private static $cost = '$10'; |
|
| 32 | + //cost parameter |
|
| 33 | + private static $cost = '$10'; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Get the unique salt for the string hash |
|
| 37 | - * @return string the unique generated salt |
|
| 38 | - */ |
|
| 39 | - private static function uniqueSalt() { |
|
| 40 | - return substr(sha1(mt_rand()), 0, 22); |
|
| 41 | - } |
|
| 35 | + /** |
|
| 36 | + * Get the unique salt for the string hash |
|
| 37 | + * @return string the unique generated salt |
|
| 38 | + */ |
|
| 39 | + private static function uniqueSalt() { |
|
| 40 | + return substr(sha1(mt_rand()), 0, 22); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Hash the given string |
|
| 45 | - * @param string $value the plain string text to be hashed |
|
| 46 | - * @return string the hashed string |
|
| 47 | - */ |
|
| 48 | - public static function hash($value) { |
|
| 49 | - return crypt($value, self::$algo . |
|
| 50 | - self::$cost . |
|
| 51 | - '$' . self::uniqueSalt()); |
|
| 52 | - } |
|
| 43 | + /** |
|
| 44 | + * Hash the given string |
|
| 45 | + * @param string $value the plain string text to be hashed |
|
| 46 | + * @return string the hashed string |
|
| 47 | + */ |
|
| 48 | + public static function hash($value) { |
|
| 49 | + return crypt($value, self::$algo . |
|
| 50 | + self::$cost . |
|
| 51 | + '$' . self::uniqueSalt()); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Check if the hash and plain string is valid |
|
| 56 | - * @param string $hash the hashed string |
|
| 57 | - * @param string $plain the plain text |
|
| 58 | - * @return boolean true if is valid or false if not |
|
| 59 | - */ |
|
| 60 | - public static function check($hash, $plain) { |
|
| 61 | - $full_salt = substr($hash, 0, 29); |
|
| 62 | - $new_hash = crypt($plain, $full_salt); |
|
| 63 | - return ($hash === $new_hash); |
|
| 64 | - } |
|
| 65 | - } |
|
| 66 | 54 | \ No newline at end of file |
| 55 | + /** |
|
| 56 | + * Check if the hash and plain string is valid |
|
| 57 | + * @param string $hash the hashed string |
|
| 58 | + * @param string $plain the plain text |
|
| 59 | + * @return boolean true if is valid or false if not |
|
| 60 | + */ |
|
| 61 | + public static function check($hash, $plain) { |
|
| 62 | + $full_salt = substr($hash, 0, 29); |
|
| 63 | + $new_hash = crypt($plain, $full_salt); |
|
| 64 | + return ($hash === $new_hash); |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | 67 | \ No newline at end of file |
@@ -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 | } |
@@ -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,14 +88,14 @@ 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){ |
|
| 91 | + public static function br($nb = 1, $return = true) { |
|
| 92 | 92 | $nb = (int) $nb; |
| 93 | 93 | $str = null; |
| 94 | 94 | for ($i = 1; $i <= $nb; $i++) { |
| 95 | 95 | $str .= '<br />'; |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - if($return){ |
|
| 98 | + if ($return) { |
|
| 99 | 99 | return $str; |
| 100 | 100 | } |
| 101 | 101 | echo $str; |
@@ -109,13 +109,13 @@ discard block |
||
| 109 | 109 | * |
| 110 | 110 | * @return string|void the generated "hr" html if $return is true or display it if not. |
| 111 | 111 | */ |
| 112 | - public static function hr($nb = 1, array $attributes = array(), $return = true){ |
|
| 112 | + public static function hr($nb = 1, array $attributes = array(), $return = true) { |
|
| 113 | 113 | $nb = (int) $nb; |
| 114 | 114 | $str = null; |
| 115 | 115 | for ($i = 1; $i <= $nb; $i++) { |
| 116 | - $str .= '<hr' .attributes_to_string($attributes). ' />'; |
|
| 116 | + $str .= '<hr' . attributes_to_string($attributes) . ' />'; |
|
| 117 | 117 | } |
| 118 | - if($return){ |
|
| 118 | + if ($return) { |
|
| 119 | 119 | return $str; |
| 120 | 120 | } |
| 121 | 121 | echo $str; |
@@ -131,17 +131,17 @@ discard block |
||
| 131 | 131 | * |
| 132 | 132 | * @return string|void the generated header html if $return is true or display it if not. |
| 133 | 133 | */ |
| 134 | - public static function head($type = 1, $text = null, $nb = 1, array $attributes = array(), $return = true){ |
|
| 134 | + public static function head($type = 1, $text = null, $nb = 1, array $attributes = array(), $return = true) { |
|
| 135 | 135 | $nb = (int) $nb; |
| 136 | 136 | $type = (int) $type; |
| 137 | - if($type <= 0 || $type > 6){ |
|
| 137 | + if ($type <= 0 || $type > 6) { |
|
| 138 | 138 | $type = 1; |
| 139 | 139 | } |
| 140 | 140 | $str = null; |
| 141 | 141 | for ($i = 1; $i <= $nb; $i++) { |
| 142 | - $str .= '<h' . $type . attributes_to_string($attributes). '>' .$text. '</h' . $type . '>'; |
|
| 142 | + $str .= '<h' . $type . attributes_to_string($attributes) . '>' . $text . '</h' . $type . '>'; |
|
| 143 | 143 | } |
| 144 | - if($return){ |
|
| 144 | + if ($return) { |
|
| 145 | 145 | return $str; |
| 146 | 146 | } |
| 147 | 147 | echo $str; |
@@ -156,15 +156,15 @@ discard block |
||
| 156 | 156 | * |
| 157 | 157 | * @return string|void the generated "ul" html if $return is true or display it if not. |
| 158 | 158 | */ |
| 159 | - public static function ul($data = array(), $attributes = array(), $return = true){ |
|
| 159 | + public static function ul($data = array(), $attributes = array(), $return = true) { |
|
| 160 | 160 | $data = (array) $data; |
| 161 | 161 | $str = null; |
| 162 | - $str .= '<ul' . (! empty($attributes['ul']) ? attributes_to_string($attributes['ul']):'') . '>'; |
|
| 162 | + $str .= '<ul' . (!empty($attributes['ul']) ? attributes_to_string($attributes['ul']) : '') . '>'; |
|
| 163 | 163 | foreach ($data as $row) { |
| 164 | - $str .= '<li' . (! empty($attributes['li']) ? attributes_to_string($attributes['li']):'') .'>' .$row. '</li>'; |
|
| 164 | + $str .= '<li' . (!empty($attributes['li']) ? attributes_to_string($attributes['li']) : '') . '>' . $row . '</li>'; |
|
| 165 | 165 | } |
| 166 | 166 | $str .= '</ul>'; |
| 167 | - if($return){ |
|
| 167 | + if ($return) { |
|
| 168 | 168 | return $str; |
| 169 | 169 | } |
| 170 | 170 | echo $str; |
@@ -178,15 +178,15 @@ discard block |
||
| 178 | 178 | * @param boolean $return whether need return the generated html or just display it directly |
| 179 | 179 | * @return string|void the generated "ol" html if $return is true or display it if not. |
| 180 | 180 | */ |
| 181 | - public static function ol($data = array(), $attributes = array(), $return = true){ |
|
| 181 | + public static function ol($data = array(), $attributes = array(), $return = true) { |
|
| 182 | 182 | $data = (array) $data; |
| 183 | 183 | $str = null; |
| 184 | - $str .= '<ol' . (!empty($attributes['ol']) ? attributes_to_string($attributes['ol']):'') . '>'; |
|
| 184 | + $str .= '<ol' . (!empty($attributes['ol']) ? attributes_to_string($attributes['ol']) : '') . '>'; |
|
| 185 | 185 | foreach ($data as $row) { |
| 186 | - $str .= '<li' . (!empty($attributes['li']) ? attributes_to_string($attributes['li']):'') .'>' .$row. '</li>'; |
|
| 186 | + $str .= '<li' . (!empty($attributes['li']) ? attributes_to_string($attributes['li']) : '') . '>' . $row . '</li>'; |
|
| 187 | 187 | } |
| 188 | 188 | $str .= '</ol>'; |
| 189 | - if($return){ |
|
| 189 | + if ($return) { |
|
| 190 | 190 | return $str; |
| 191 | 191 | } |
| 192 | 192 | echo $str; |
@@ -204,46 +204,46 @@ discard block |
||
| 204 | 204 | * @param boolean $return whether need return the generated html or just display it directly |
| 205 | 205 | * @return string|void the generated "table" html if $return is true or display it if not. |
| 206 | 206 | */ |
| 207 | - public static function table($headers = array(), $body = array(), $attributes = array(), $use_footer = false, $return = true){ |
|
| 207 | + public static function table($headers = array(), $body = array(), $attributes = array(), $use_footer = false, $return = true) { |
|
| 208 | 208 | $headers = (array) $headers; |
| 209 | 209 | $body = (array) $body; |
| 210 | 210 | $str = null; |
| 211 | - $str .= '<table' . (! empty($attributes['table']) ? attributes_to_string($attributes['table']):'') . '>'; |
|
| 212 | - if(! empty($headers)){ |
|
| 213 | - $str .= '<thead' . (! empty($attributes['thead']) ? attributes_to_string($attributes['thead']):'') .'>'; |
|
| 214 | - $str .= '<tr' . (! empty($attributes['thead_tr']) ? attributes_to_string($attributes['thead_tr']):'') .'>'; |
|
| 211 | + $str .= '<table' . (!empty($attributes['table']) ? attributes_to_string($attributes['table']) : '') . '>'; |
|
| 212 | + if (!empty($headers)) { |
|
| 213 | + $str .= '<thead' . (!empty($attributes['thead']) ? attributes_to_string($attributes['thead']) : '') . '>'; |
|
| 214 | + $str .= '<tr' . (!empty($attributes['thead_tr']) ? attributes_to_string($attributes['thead_tr']) : '') . '>'; |
|
| 215 | 215 | foreach ($headers as $value) { |
| 216 | - $str .= '<th' . (! empty($attributes['thead_th']) ? attributes_to_string($attributes['thead_th']):'') .'>' .$value. '</th>'; |
|
| 216 | + $str .= '<th' . (!empty($attributes['thead_th']) ? attributes_to_string($attributes['thead_th']) : '') . '>' . $value . '</th>'; |
|
| 217 | 217 | } |
| 218 | 218 | $str .= '</tr>'; |
| 219 | 219 | $str .= '</thead>'; |
| 220 | 220 | } |
| 221 | - else{ |
|
| 221 | + else { |
|
| 222 | 222 | //no need check for footer |
| 223 | 223 | $use_footer = false; |
| 224 | 224 | } |
| 225 | - $str .= '<tbody' . (! empty($attributes['tbody']) ? attributes_to_string($attributes['tbody']):'') .'>'; |
|
| 225 | + $str .= '<tbody' . (!empty($attributes['tbody']) ? attributes_to_string($attributes['tbody']) : '') . '>'; |
|
| 226 | 226 | foreach ($body as $row) { |
| 227 | - if(is_array($row)){ |
|
| 228 | - $str .= '<tr' . (! empty($attributes['tbody_tr']) ? attributes_to_string($attributes['tbody_tr']):'') .'>'; |
|
| 227 | + if (is_array($row)) { |
|
| 228 | + $str .= '<tr' . (!empty($attributes['tbody_tr']) ? attributes_to_string($attributes['tbody_tr']) : '') . '>'; |
|
| 229 | 229 | foreach ($row as $value) { |
| 230 | - $str .= '<td' . (! empty($attributes['tbody_td']) ? attributes_to_string($attributes['tbody_td']):'') .'>' .$value. '</td>'; |
|
| 230 | + $str .= '<td' . (!empty($attributes['tbody_td']) ? attributes_to_string($attributes['tbody_td']) : '') . '>' . $value . '</td>'; |
|
| 231 | 231 | } |
| 232 | 232 | $str .= '</tr>'; |
| 233 | 233 | } |
| 234 | 234 | } |
| 235 | 235 | $str .= '</tbody>'; |
| 236 | - if($use_footer){ |
|
| 237 | - $str .= '<tfoot' . (! empty($attributes['tfoot']) ? attributes_to_string($attributes['tfoot']):'') .'>'; |
|
| 238 | - $str .= '<tr' . (! empty($attributes['tfoot_tr']) ? attributes_to_string($attributes['tfoot_tr']):'') .'>'; |
|
| 236 | + if ($use_footer) { |
|
| 237 | + $str .= '<tfoot' . (!empty($attributes['tfoot']) ? attributes_to_string($attributes['tfoot']) : '') . '>'; |
|
| 238 | + $str .= '<tr' . (!empty($attributes['tfoot_tr']) ? attributes_to_string($attributes['tfoot_tr']) : '') . '>'; |
|
| 239 | 239 | foreach ($headers as $value) { |
| 240 | - $str .= '<th' . (! empty($attributes['tfoot_th']) ? attributes_to_string($attributes['tfoot_th']):'') .'>' .$value. '</th>'; |
|
| 240 | + $str .= '<th' . (!empty($attributes['tfoot_th']) ? attributes_to_string($attributes['tfoot_th']) : '') . '>' . $value . '</th>'; |
|
| 241 | 241 | } |
| 242 | 242 | $str .= '</tr>'; |
| 243 | 243 | $str .= '</tfoot>'; |
| 244 | 244 | } |
| 245 | 245 | $str .= '</table>'; |
| 246 | - if($return){ |
|
| 246 | + if ($return) { |
|
| 247 | 247 | return $str; |
| 248 | 248 | } |
| 249 | 249 | echo $str; |
@@ -1,251 +1,251 @@ |
||
| 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 | - */ |
|
| 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 | 26 | |
| 27 | - class Html{ |
|
| 27 | + class Html{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Generate the html anchor link |
|
| 31 | - * @param string $link the href attribute value |
|
| 32 | - * @param string $anchor the displayed anchor |
|
| 33 | - * @param array $attributes the additional attributes to be added |
|
| 34 | - * @param boolean $return whether need return the generated html or just display it directly |
|
| 35 | - * |
|
| 36 | - * @return string|void the anchor link generated html if $return is true or display it if not |
|
| 37 | - */ |
|
| 38 | - public static function a($link = '', $anchor = null, array $attributes = array(), $return = true){ |
|
| 39 | - if(! is_url($link)){ |
|
| 40 | - $link = Url::site_url($link); |
|
| 41 | - } |
|
| 42 | - if(! $anchor){ |
|
| 43 | - $anchor = $link; |
|
| 44 | - } |
|
| 45 | - $str = null; |
|
| 46 | - $str .= '<a href = "'.$link.'"'; |
|
| 47 | - $str .= attributes_to_string($attributes); |
|
| 48 | - $str .= '>'; |
|
| 49 | - $str .= $anchor; |
|
| 50 | - $str .= '</a>'; |
|
| 29 | + /** |
|
| 30 | + * Generate the html anchor link |
|
| 31 | + * @param string $link the href attribute value |
|
| 32 | + * @param string $anchor the displayed anchor |
|
| 33 | + * @param array $attributes the additional attributes to be added |
|
| 34 | + * @param boolean $return whether need return the generated html or just display it directly |
|
| 35 | + * |
|
| 36 | + * @return string|void the anchor link generated html if $return is true or display it if not |
|
| 37 | + */ |
|
| 38 | + public static function a($link = '', $anchor = null, array $attributes = array(), $return = true){ |
|
| 39 | + if(! is_url($link)){ |
|
| 40 | + $link = Url::site_url($link); |
|
| 41 | + } |
|
| 42 | + if(! $anchor){ |
|
| 43 | + $anchor = $link; |
|
| 44 | + } |
|
| 45 | + $str = null; |
|
| 46 | + $str .= '<a href = "'.$link.'"'; |
|
| 47 | + $str .= attributes_to_string($attributes); |
|
| 48 | + $str .= '>'; |
|
| 49 | + $str .= $anchor; |
|
| 50 | + $str .= '</a>'; |
|
| 51 | 51 | |
| 52 | - if($return){ |
|
| 53 | - return $str; |
|
| 54 | - } |
|
| 55 | - echo $str; |
|
| 56 | - } |
|
| 52 | + if($return){ |
|
| 53 | + return $str; |
|
| 54 | + } |
|
| 55 | + echo $str; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Generate an mailto anchor link |
|
| 60 | - * @param string $link the email address |
|
| 61 | - * @param string $anchor the displayed value of the link |
|
| 62 | - * @param array $attributes the additional attributes to be added |
|
| 63 | - * @param boolean $return whether need return the generated html or just display it directly |
|
| 64 | - * |
|
| 65 | - * @return string|void the generated html for mailto link if $return is true or display it if not |
|
| 66 | - */ |
|
| 67 | - public static function mailto($link, $anchor = null, array $attributes = array(), $return = true){ |
|
| 68 | - if(! $anchor){ |
|
| 69 | - $anchor = $link; |
|
| 70 | - } |
|
| 71 | - $str = null; |
|
| 72 | - $str .= '<a href = "mailto:'.$link.'"'; |
|
| 73 | - $str .= attributes_to_string($attributes); |
|
| 74 | - $str .= '>'; |
|
| 75 | - $str .= $anchor; |
|
| 76 | - $str .= '</a>'; |
|
| 58 | + /** |
|
| 59 | + * Generate an mailto anchor link |
|
| 60 | + * @param string $link the email address |
|
| 61 | + * @param string $anchor the displayed value of the link |
|
| 62 | + * @param array $attributes the additional attributes to be added |
|
| 63 | + * @param boolean $return whether need return the generated html or just display it directly |
|
| 64 | + * |
|
| 65 | + * @return string|void the generated html for mailto link if $return is true or display it if not |
|
| 66 | + */ |
|
| 67 | + public static function mailto($link, $anchor = null, array $attributes = array(), $return = true){ |
|
| 68 | + if(! $anchor){ |
|
| 69 | + $anchor = $link; |
|
| 70 | + } |
|
| 71 | + $str = null; |
|
| 72 | + $str .= '<a href = "mailto:'.$link.'"'; |
|
| 73 | + $str .= attributes_to_string($attributes); |
|
| 74 | + $str .= '>'; |
|
| 75 | + $str .= $anchor; |
|
| 76 | + $str .= '</a>'; |
|
| 77 | 77 | |
| 78 | - if($return){ |
|
| 79 | - return $str; |
|
| 80 | - } |
|
| 81 | - echo $str; |
|
| 82 | - } |
|
| 78 | + if($return){ |
|
| 79 | + return $str; |
|
| 80 | + } |
|
| 81 | + echo $str; |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Generate the html "br" tag |
|
| 86 | - * @param integer $nb the number of generated "<br />" tag |
|
| 87 | - * @param boolean $return whether need return the generated html or just display it directly |
|
| 88 | - * |
|
| 89 | - * @return string|void the generated "br" html if $return is true or display it if not |
|
| 90 | - */ |
|
| 91 | - public static function br($nb = 1, $return = true){ |
|
| 92 | - $nb = (int) $nb; |
|
| 93 | - $str = null; |
|
| 94 | - for ($i = 1; $i <= $nb; $i++) { |
|
| 95 | - $str .= '<br />'; |
|
| 96 | - } |
|
| 84 | + /** |
|
| 85 | + * Generate the html "br" tag |
|
| 86 | + * @param integer $nb the number of generated "<br />" tag |
|
| 87 | + * @param boolean $return whether need return the generated html or just display it directly |
|
| 88 | + * |
|
| 89 | + * @return string|void the generated "br" html if $return is true or display it if not |
|
| 90 | + */ |
|
| 91 | + public static function br($nb = 1, $return = true){ |
|
| 92 | + $nb = (int) $nb; |
|
| 93 | + $str = null; |
|
| 94 | + for ($i = 1; $i <= $nb; $i++) { |
|
| 95 | + $str .= '<br />'; |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - if($return){ |
|
| 99 | - return $str; |
|
| 100 | - } |
|
| 101 | - echo $str; |
|
| 102 | - } |
|
| 98 | + if($return){ |
|
| 99 | + return $str; |
|
| 100 | + } |
|
| 101 | + echo $str; |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * Generate the html content for tag "hr" |
|
| 106 | - * @param integer $nb the number of generated "<hr />" tag |
|
| 107 | - * @param array $attributes the tag attributes |
|
| 108 | - * @param boolean $return whether need return the generated html or just display it directly |
|
| 109 | - * |
|
| 110 | - * @return string|void the generated "hr" html if $return is true or display it if not. |
|
| 111 | - */ |
|
| 112 | - public static function hr($nb = 1, array $attributes = array(), $return = true){ |
|
| 113 | - $nb = (int) $nb; |
|
| 114 | - $str = null; |
|
| 115 | - for ($i = 1; $i <= $nb; $i++) { |
|
| 116 | - $str .= '<hr' .attributes_to_string($attributes). ' />'; |
|
| 117 | - } |
|
| 118 | - if($return){ |
|
| 119 | - return $str; |
|
| 120 | - } |
|
| 121 | - echo $str; |
|
| 122 | - } |
|
| 104 | + /** |
|
| 105 | + * Generate the html content for tag "hr" |
|
| 106 | + * @param integer $nb the number of generated "<hr />" tag |
|
| 107 | + * @param array $attributes the tag attributes |
|
| 108 | + * @param boolean $return whether need return the generated html or just display it directly |
|
| 109 | + * |
|
| 110 | + * @return string|void the generated "hr" html if $return is true or display it if not. |
|
| 111 | + */ |
|
| 112 | + public static function hr($nb = 1, array $attributes = array(), $return = true){ |
|
| 113 | + $nb = (int) $nb; |
|
| 114 | + $str = null; |
|
| 115 | + for ($i = 1; $i <= $nb; $i++) { |
|
| 116 | + $str .= '<hr' .attributes_to_string($attributes). ' />'; |
|
| 117 | + } |
|
| 118 | + if($return){ |
|
| 119 | + return $str; |
|
| 120 | + } |
|
| 121 | + echo $str; |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - /** |
|
| 125 | - * Generate the html content for tag like h1, h2, h3, h4, h5 and h6 |
|
| 126 | - * @param integer $type the tag type 1 mean h1, 2 h2, etc, |
|
| 127 | - * @param string $text the display text |
|
| 128 | - * @param integer $nb the number of generated "<h{1,2,3,4,5,6}>" |
|
| 129 | - * @param array $attributes the tag attributes |
|
| 130 | - * @param boolean $return whether need return the generated html or just display it directly |
|
| 131 | - * |
|
| 132 | - * @return string|void the generated header html if $return is true or display it if not. |
|
| 133 | - */ |
|
| 134 | - public static function head($type = 1, $text = null, $nb = 1, array $attributes = array(), $return = true){ |
|
| 135 | - $nb = (int) $nb; |
|
| 136 | - $type = (int) $type; |
|
| 137 | - if($type <= 0 || $type > 6){ |
|
| 138 | - $type = 1; |
|
| 139 | - } |
|
| 140 | - $str = null; |
|
| 141 | - for ($i = 1; $i <= $nb; $i++) { |
|
| 142 | - $str .= '<h' . $type . attributes_to_string($attributes). '>' .$text. '</h' . $type . '>'; |
|
| 143 | - } |
|
| 144 | - if($return){ |
|
| 145 | - return $str; |
|
| 146 | - } |
|
| 147 | - echo $str; |
|
| 148 | - } |
|
| 124 | + /** |
|
| 125 | + * Generate the html content for tag like h1, h2, h3, h4, h5 and h6 |
|
| 126 | + * @param integer $type the tag type 1 mean h1, 2 h2, etc, |
|
| 127 | + * @param string $text the display text |
|
| 128 | + * @param integer $nb the number of generated "<h{1,2,3,4,5,6}>" |
|
| 129 | + * @param array $attributes the tag attributes |
|
| 130 | + * @param boolean $return whether need return the generated html or just display it directly |
|
| 131 | + * |
|
| 132 | + * @return string|void the generated header html if $return is true or display it if not. |
|
| 133 | + */ |
|
| 134 | + public static function head($type = 1, $text = null, $nb = 1, array $attributes = array(), $return = true){ |
|
| 135 | + $nb = (int) $nb; |
|
| 136 | + $type = (int) $type; |
|
| 137 | + if($type <= 0 || $type > 6){ |
|
| 138 | + $type = 1; |
|
| 139 | + } |
|
| 140 | + $str = null; |
|
| 141 | + for ($i = 1; $i <= $nb; $i++) { |
|
| 142 | + $str .= '<h' . $type . attributes_to_string($attributes). '>' .$text. '</h' . $type . '>'; |
|
| 143 | + } |
|
| 144 | + if($return){ |
|
| 145 | + return $str; |
|
| 146 | + } |
|
| 147 | + echo $str; |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * Generate the html "ul" tag |
|
| 152 | - * @param array $data the data to use for each "li" tag |
|
| 153 | - * @param array $attributes the "ul" properties attribute use the array index below for each tag: |
|
| 154 | - * for ul "ul", for li "li". |
|
| 155 | - * @param boolean $return whether need return the generated html or just display it directly |
|
| 156 | - * |
|
| 157 | - * @return string|void the generated "ul" html if $return is true or display it if not. |
|
| 158 | - */ |
|
| 159 | - public static function ul($data = array(), $attributes = array(), $return = true){ |
|
| 160 | - $data = (array) $data; |
|
| 161 | - $str = null; |
|
| 162 | - $str .= '<ul' . (! empty($attributes['ul']) ? attributes_to_string($attributes['ul']):'') . '>'; |
|
| 163 | - foreach ($data as $row) { |
|
| 164 | - $str .= '<li' . (! empty($attributes['li']) ? attributes_to_string($attributes['li']):'') .'>' .$row. '</li>'; |
|
| 165 | - } |
|
| 166 | - $str .= '</ul>'; |
|
| 167 | - if($return){ |
|
| 168 | - return $str; |
|
| 169 | - } |
|
| 170 | - echo $str; |
|
| 171 | - } |
|
| 150 | + /** |
|
| 151 | + * Generate the html "ul" tag |
|
| 152 | + * @param array $data the data to use for each "li" tag |
|
| 153 | + * @param array $attributes the "ul" properties attribute use the array index below for each tag: |
|
| 154 | + * for ul "ul", for li "li". |
|
| 155 | + * @param boolean $return whether need return the generated html or just display it directly |
|
| 156 | + * |
|
| 157 | + * @return string|void the generated "ul" html if $return is true or display it if not. |
|
| 158 | + */ |
|
| 159 | + public static function ul($data = array(), $attributes = array(), $return = true){ |
|
| 160 | + $data = (array) $data; |
|
| 161 | + $str = null; |
|
| 162 | + $str .= '<ul' . (! empty($attributes['ul']) ? attributes_to_string($attributes['ul']):'') . '>'; |
|
| 163 | + foreach ($data as $row) { |
|
| 164 | + $str .= '<li' . (! empty($attributes['li']) ? attributes_to_string($attributes['li']):'') .'>' .$row. '</li>'; |
|
| 165 | + } |
|
| 166 | + $str .= '</ul>'; |
|
| 167 | + if($return){ |
|
| 168 | + return $str; |
|
| 169 | + } |
|
| 170 | + echo $str; |
|
| 171 | + } |
|
| 172 | 172 | |
| 173 | - /** |
|
| 174 | - * Generate the html "ol" tag |
|
| 175 | - * @param array $data the data to use for each "li" tag |
|
| 176 | - * @param array $attributes the "ol" properties attribute use the array index below for each tag: |
|
| 177 | - * for ol "ol", for li "li". |
|
| 178 | - * @param boolean $return whether need return the generated html or just display it directly |
|
| 179 | - * @return string|void the generated "ol" html if $return is true or display it if not. |
|
| 180 | - */ |
|
| 181 | - public static function ol($data = array(), $attributes = array(), $return = true){ |
|
| 182 | - $data = (array) $data; |
|
| 183 | - $str = null; |
|
| 184 | - $str .= '<ol' . (!empty($attributes['ol']) ? attributes_to_string($attributes['ol']):'') . '>'; |
|
| 185 | - foreach ($data as $row) { |
|
| 186 | - $str .= '<li' . (!empty($attributes['li']) ? attributes_to_string($attributes['li']):'') .'>' .$row. '</li>'; |
|
| 187 | - } |
|
| 188 | - $str .= '</ol>'; |
|
| 189 | - if($return){ |
|
| 190 | - return $str; |
|
| 191 | - } |
|
| 192 | - echo $str; |
|
| 193 | - } |
|
| 173 | + /** |
|
| 174 | + * Generate the html "ol" tag |
|
| 175 | + * @param array $data the data to use for each "li" tag |
|
| 176 | + * @param array $attributes the "ol" properties attribute use the array index below for each tag: |
|
| 177 | + * for ol "ol", for li "li". |
|
| 178 | + * @param boolean $return whether need return the generated html or just display it directly |
|
| 179 | + * @return string|void the generated "ol" html if $return is true or display it if not. |
|
| 180 | + */ |
|
| 181 | + public static function ol($data = array(), $attributes = array(), $return = true){ |
|
| 182 | + $data = (array) $data; |
|
| 183 | + $str = null; |
|
| 184 | + $str .= '<ol' . (!empty($attributes['ol']) ? attributes_to_string($attributes['ol']):'') . '>'; |
|
| 185 | + foreach ($data as $row) { |
|
| 186 | + $str .= '<li' . (!empty($attributes['li']) ? attributes_to_string($attributes['li']):'') .'>' .$row. '</li>'; |
|
| 187 | + } |
|
| 188 | + $str .= '</ol>'; |
|
| 189 | + if($return){ |
|
| 190 | + return $str; |
|
| 191 | + } |
|
| 192 | + echo $str; |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - /** |
|
| 196 | - * Generate the html "table" tag |
|
| 197 | - * @param array $headers the table headers to use between (<thead>) |
|
| 198 | - * @param array $body the table body values between (<tbody>) |
|
| 199 | - * @param array $attributes the table properties attribute use the array index below for each tag: |
|
| 200 | - * for table "table", for thead "thead", for thead tr "thead_tr", |
|
| 201 | - * for thead th "thead_th", for tbody "tbody", for tbody tr "tbody_tr", for tbody td "tbody_td", for tfoot "tfoot", |
|
| 202 | - * for tfoot tr "tfoot_tr", for tfoot th "tfoot_th". |
|
| 203 | - * @param boolean $use_footer whether need to generate table footer (<tfoot>) use the $headers values |
|
| 204 | - * @param boolean $return whether need return the generated html or just display it directly |
|
| 205 | - * @return string|void the generated "table" html if $return is true or display it if not. |
|
| 206 | - */ |
|
| 207 | - public static function table($headers = array(), $body = array(), $attributes = array(), $use_footer = false, $return = true){ |
|
| 208 | - $headers = (array) $headers; |
|
| 209 | - $body = (array) $body; |
|
| 210 | - $str = null; |
|
| 211 | - $str .= '<table' . (! empty($attributes['table']) ? attributes_to_string($attributes['table']):'') . '>'; |
|
| 212 | - if(! empty($headers)){ |
|
| 213 | - $str .= '<thead' . (! empty($attributes['thead']) ? attributes_to_string($attributes['thead']):'') .'>'; |
|
| 214 | - $str .= '<tr' . (! empty($attributes['thead_tr']) ? attributes_to_string($attributes['thead_tr']):'') .'>'; |
|
| 215 | - foreach ($headers as $value) { |
|
| 216 | - $str .= '<th' . (! empty($attributes['thead_th']) ? attributes_to_string($attributes['thead_th']):'') .'>' .$value. '</th>'; |
|
| 217 | - } |
|
| 218 | - $str .= '</tr>'; |
|
| 219 | - $str .= '</thead>'; |
|
| 220 | - } |
|
| 221 | - else{ |
|
| 222 | - //no need check for footer |
|
| 223 | - $use_footer = false; |
|
| 224 | - } |
|
| 225 | - $str .= '<tbody' . (! empty($attributes['tbody']) ? attributes_to_string($attributes['tbody']):'') .'>'; |
|
| 226 | - foreach ($body as $row) { |
|
| 227 | - if(is_array($row)){ |
|
| 228 | - $str .= '<tr' . (! empty($attributes['tbody_tr']) ? attributes_to_string($attributes['tbody_tr']):'') .'>'; |
|
| 229 | - foreach ($row as $value) { |
|
| 230 | - $str .= '<td' . (! empty($attributes['tbody_td']) ? attributes_to_string($attributes['tbody_td']):'') .'>' .$value. '</td>'; |
|
| 231 | - } |
|
| 232 | - $str .= '</tr>'; |
|
| 233 | - } |
|
| 234 | - } |
|
| 235 | - $str .= '</tbody>'; |
|
| 236 | - if($use_footer){ |
|
| 237 | - $str .= '<tfoot' . (! empty($attributes['tfoot']) ? attributes_to_string($attributes['tfoot']):'') .'>'; |
|
| 238 | - $str .= '<tr' . (! empty($attributes['tfoot_tr']) ? attributes_to_string($attributes['tfoot_tr']):'') .'>'; |
|
| 239 | - foreach ($headers as $value) { |
|
| 240 | - $str .= '<th' . (! empty($attributes['tfoot_th']) ? attributes_to_string($attributes['tfoot_th']):'') .'>' .$value. '</th>'; |
|
| 241 | - } |
|
| 242 | - $str .= '</tr>'; |
|
| 243 | - $str .= '</tfoot>'; |
|
| 244 | - } |
|
| 245 | - $str .= '</table>'; |
|
| 246 | - if($return){ |
|
| 247 | - return $str; |
|
| 248 | - } |
|
| 249 | - echo $str; |
|
| 250 | - } |
|
| 251 | - } |
|
| 195 | + /** |
|
| 196 | + * Generate the html "table" tag |
|
| 197 | + * @param array $headers the table headers to use between (<thead>) |
|
| 198 | + * @param array $body the table body values between (<tbody>) |
|
| 199 | + * @param array $attributes the table properties attribute use the array index below for each tag: |
|
| 200 | + * for table "table", for thead "thead", for thead tr "thead_tr", |
|
| 201 | + * for thead th "thead_th", for tbody "tbody", for tbody tr "tbody_tr", for tbody td "tbody_td", for tfoot "tfoot", |
|
| 202 | + * for tfoot tr "tfoot_tr", for tfoot th "tfoot_th". |
|
| 203 | + * @param boolean $use_footer whether need to generate table footer (<tfoot>) use the $headers values |
|
| 204 | + * @param boolean $return whether need return the generated html or just display it directly |
|
| 205 | + * @return string|void the generated "table" html if $return is true or display it if not. |
|
| 206 | + */ |
|
| 207 | + public static function table($headers = array(), $body = array(), $attributes = array(), $use_footer = false, $return = true){ |
|
| 208 | + $headers = (array) $headers; |
|
| 209 | + $body = (array) $body; |
|
| 210 | + $str = null; |
|
| 211 | + $str .= '<table' . (! empty($attributes['table']) ? attributes_to_string($attributes['table']):'') . '>'; |
|
| 212 | + if(! empty($headers)){ |
|
| 213 | + $str .= '<thead' . (! empty($attributes['thead']) ? attributes_to_string($attributes['thead']):'') .'>'; |
|
| 214 | + $str .= '<tr' . (! empty($attributes['thead_tr']) ? attributes_to_string($attributes['thead_tr']):'') .'>'; |
|
| 215 | + foreach ($headers as $value) { |
|
| 216 | + $str .= '<th' . (! empty($attributes['thead_th']) ? attributes_to_string($attributes['thead_th']):'') .'>' .$value. '</th>'; |
|
| 217 | + } |
|
| 218 | + $str .= '</tr>'; |
|
| 219 | + $str .= '</thead>'; |
|
| 220 | + } |
|
| 221 | + else{ |
|
| 222 | + //no need check for footer |
|
| 223 | + $use_footer = false; |
|
| 224 | + } |
|
| 225 | + $str .= '<tbody' . (! empty($attributes['tbody']) ? attributes_to_string($attributes['tbody']):'') .'>'; |
|
| 226 | + foreach ($body as $row) { |
|
| 227 | + if(is_array($row)){ |
|
| 228 | + $str .= '<tr' . (! empty($attributes['tbody_tr']) ? attributes_to_string($attributes['tbody_tr']):'') .'>'; |
|
| 229 | + foreach ($row as $value) { |
|
| 230 | + $str .= '<td' . (! empty($attributes['tbody_td']) ? attributes_to_string($attributes['tbody_td']):'') .'>' .$value. '</td>'; |
|
| 231 | + } |
|
| 232 | + $str .= '</tr>'; |
|
| 233 | + } |
|
| 234 | + } |
|
| 235 | + $str .= '</tbody>'; |
|
| 236 | + if($use_footer){ |
|
| 237 | + $str .= '<tfoot' . (! empty($attributes['tfoot']) ? attributes_to_string($attributes['tfoot']):'') .'>'; |
|
| 238 | + $str .= '<tr' . (! empty($attributes['tfoot_tr']) ? attributes_to_string($attributes['tfoot_tr']):'') .'>'; |
|
| 239 | + foreach ($headers as $value) { |
|
| 240 | + $str .= '<th' . (! empty($attributes['tfoot_th']) ? attributes_to_string($attributes['tfoot_th']):'') .'>' .$value. '</th>'; |
|
| 241 | + } |
|
| 242 | + $str .= '</tr>'; |
|
| 243 | + $str .= '</tfoot>'; |
|
| 244 | + } |
|
| 245 | + $str .= '</table>'; |
|
| 246 | + if($return){ |
|
| 247 | + return $str; |
|
| 248 | + } |
|
| 249 | + echo $str; |
|
| 250 | + } |
|
| 251 | + } |
|
@@ -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 | } |
@@ -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,15 +36,15 @@ 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 | $config = array(); |
| 42 | 42 | require_once CONFIG_PATH . 'config_pagination.php'; |
| 43 | - if(empty($config) || ! is_array($config)){ |
|
| 43 | + if (empty($config) || !is_array($config)) { |
|
| 44 | 44 | show_error('No configuration found in ' . CONFIG_PATH . 'config_pagination.php'); |
| 45 | 45 | } |
| 46 | - else{ |
|
| 47 | - if(! empty($overwriteConfig)){ |
|
| 46 | + else { |
|
| 47 | + if (!empty($overwriteConfig)) { |
|
| 48 | 48 | $config = array_merge($config, $overwriteConfig); |
| 49 | 49 | } |
| 50 | 50 | $this->config = $config; |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | unset($config); |
| 54 | 54 | } |
| 55 | 55 | } |
| 56 | - else{ |
|
| 56 | + else { |
|
| 57 | 57 | show_error('Unable to find the pagination configuration file'); |
| 58 | 58 | } |
| 59 | 59 | } |
@@ -64,8 +64,8 @@ discard block |
||
| 64 | 64 | * config_pagination.php |
| 65 | 65 | * @param array $config the configuration to set |
| 66 | 66 | */ |
| 67 | - public function setConfig(array $config = array()){ |
|
| 68 | - if(! empty($config)){ |
|
| 67 | + public function setConfig(array $config = array()) { |
|
| 68 | + if (!empty($config)) { |
|
| 69 | 69 | $this->config = array_merge($this->config, $config); |
| 70 | 70 | Config::setAll($config); |
| 71 | 71 | } |
@@ -77,26 +77,26 @@ discard block |
||
| 77 | 77 | * @param int $currentPageNumber the current page number |
| 78 | 78 | * @return string the pagination link |
| 79 | 79 | */ |
| 80 | - public function getLink($totalRows, $currentPageNumber){ |
|
| 80 | + public function getLink($totalRows, $currentPageNumber) { |
|
| 81 | 81 | $pageQueryName = $this->config['page_query_string_name']; |
| 82 | 82 | $numberOfLink = $this->config['nb_link']; |
| 83 | 83 | $numberOfRowPerPage = $this->config['pagination_per_page']; |
| 84 | 84 | $queryString = Url::queryString(); |
| 85 | 85 | $currentUrl = Url::current(); |
| 86 | - if($queryString == ''){ |
|
| 86 | + if ($queryString == '') { |
|
| 87 | 87 | $query = '?' . $pageQueryName . '='; |
| 88 | 88 | } |
| 89 | - else{ |
|
| 89 | + else { |
|
| 90 | 90 | $tab = explode($pageQueryName . '=', $queryString); |
| 91 | 91 | $nb = count($tab); |
| 92 | - if($nb == 1){ |
|
| 92 | + if ($nb == 1) { |
|
| 93 | 93 | $query = '?' . $queryString . '&' . $pageQueryName . '='; |
| 94 | 94 | } |
| 95 | - else{ |
|
| 96 | - if($tab[0] == ''){ |
|
| 95 | + else { |
|
| 96 | + if ($tab[0] == '') { |
|
| 97 | 97 | $query = '?' . $pageQueryName . '='; |
| 98 | 98 | } |
| 99 | - else{ |
|
| 99 | + else { |
|
| 100 | 100 | $query = '?' . $tab[0] . '' . $pageQueryName . '='; |
| 101 | 101 | } |
| 102 | 102 | } |
@@ -106,67 +106,67 @@ discard block |
||
| 106 | 106 | $navbar = ''; |
| 107 | 107 | $numberOfPage = ceil($totalRows / $numberOfRowPerPage); |
| 108 | 108 | $currentPageNumber = (int) $currentPageNumber; |
| 109 | - if($currentPageNumber <= 0){ |
|
| 109 | + if ($currentPageNumber <= 0) { |
|
| 110 | 110 | $currentPageNumber = 1; |
| 111 | 111 | } |
| 112 | - if($numberOfPage <= 1 || $numberOfLink <= 0 || $numberOfRowPerPage <= 0 || !is_numeric($numberOfLink) || !is_numeric($numberOfRowPerPage) |
|
| 113 | - ){ |
|
| 112 | + if ($numberOfPage <= 1 || $numberOfLink <= 0 || $numberOfRowPerPage <= 0 || !is_numeric($numberOfLink) || !is_numeric($numberOfRowPerPage) |
|
| 113 | + ) { |
|
| 114 | 114 | return $navbar; |
| 115 | 115 | } |
| 116 | - if($numberOfLink % 2 == 0){ |
|
| 116 | + if ($numberOfLink % 2 == 0) { |
|
| 117 | 117 | $start = $currentPageNumber - ($numberOfLink / 2) + 1; |
| 118 | 118 | $end = $currentPageNumber + ($numberOfLink / 2); |
| 119 | 119 | } |
| 120 | - else{ |
|
| 120 | + else { |
|
| 121 | 121 | $start = $currentPageNumber - floor($numberOfLink / 2); |
| 122 | 122 | $end = $currentPageNumber + floor($numberOfLink / 2); |
| 123 | 123 | } |
| 124 | - if($start <= 1){ |
|
| 124 | + if ($start <= 1) { |
|
| 125 | 125 | $begin = 1; |
| 126 | 126 | $end = $numberOfLink; |
| 127 | 127 | } |
| 128 | - else if($start > 1 && $end < $numberOfPage){ |
|
| 128 | + else if ($start > 1 && $end < $numberOfPage) { |
|
| 129 | 129 | $begin = $start; |
| 130 | 130 | $end = $end; |
| 131 | 131 | } |
| 132 | - else{ |
|
| 132 | + else { |
|
| 133 | 133 | $begin = ($numberOfPage - $numberOfLink) + 1; |
| 134 | 134 | $end = $numberOfPage; |
| 135 | 135 | } |
| 136 | - if($numberOfPage <= $numberOfLink){ |
|
| 136 | + if ($numberOfPage <= $numberOfLink) { |
|
| 137 | 137 | $begin = 1; |
| 138 | 138 | $end = $numberOfPage; |
| 139 | 139 | } |
| 140 | - if($currentPageNumber == 1){ |
|
| 141 | - for($i = $begin; $i <= $end; $i++){ |
|
| 142 | - if($i == $currentPageNumber){ |
|
| 140 | + if ($currentPageNumber == 1) { |
|
| 141 | + for ($i = $begin; $i <= $end; $i++) { |
|
| 142 | + if ($i == $currentPageNumber) { |
|
| 143 | 143 | $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
| 144 | 144 | } |
| 145 | - else{ |
|
| 145 | + else { |
|
| 146 | 146 | $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '" ' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
| 147 | 147 | } |
| 148 | 148 | } |
| 149 | 149 | $navbar .= $this->config['next_open'] . '<a href="' . $query . ($currentPageNumber + 1) . '">' . $this->config['next_text'] . '</a>' . $this->config['next_close']; |
| 150 | 150 | } |
| 151 | - else if($currentPageNumber > 1 && $currentPageNumber < $numberOfPage){ |
|
| 151 | + else if ($currentPageNumber > 1 && $currentPageNumber < $numberOfPage) { |
|
| 152 | 152 | $navbar .= $this->config['previous_open'] . '<a href="' . $query . ($currentPageNumber - 1) . '">' . $this->config['previous_text'] . '</a>' . $this->config['previous_close']; |
| 153 | - for($i = $begin; $i <= $end; $i++){ |
|
| 154 | - if($i == $currentPageNumber){ |
|
| 153 | + for ($i = $begin; $i <= $end; $i++) { |
|
| 154 | + if ($i == $currentPageNumber) { |
|
| 155 | 155 | $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
| 156 | 156 | } |
| 157 | - else{ |
|
| 158 | - $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i .'</a>' . $this->config['digit_close']; |
|
| 157 | + else { |
|
| 158 | + $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
|
| 159 | 159 | } |
| 160 | 160 | } |
| 161 | - $navbar .= $this->config['next_open']."<a href='$query".($currentPageNumber + 1)."'>".$this->config['next_text']."</a>".$this->config['next_close']; |
|
| 161 | + $navbar .= $this->config['next_open'] . "<a href='$query" . ($currentPageNumber + 1) . "'>" . $this->config['next_text'] . "</a>" . $this->config['next_close']; |
|
| 162 | 162 | } |
| 163 | - else if($currentPageNumber == $numberOfPage){ |
|
| 163 | + else if ($currentPageNumber == $numberOfPage) { |
|
| 164 | 164 | $navbar .= $this->config['previous_open'] . '<a href="' . $query . ($currentPageNumber - 1) . '">' . $this->config['previous_text'] . '</a>' . $this->config['previous_close']; |
| 165 | - for($i = $begin; $i <= $end; $i++){ |
|
| 166 | - if($i == $currentPageNumber){ |
|
| 165 | + for ($i = $begin; $i <= $end; $i++) { |
|
| 166 | + if ($i == $currentPageNumber) { |
|
| 167 | 167 | $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
| 168 | 168 | } |
| 169 | - else{ |
|
| 169 | + else { |
|
| 170 | 170 | $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
| 171 | 171 | } |
| 172 | 172 | } |
@@ -1,32 +1,32 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 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 | - */ |
|
| 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 | 26 | |
| 27 | 27 | class Pagination{ |
| 28 | 28 | |
| 29 | - /** |
|
| 29 | + /** |
|
| 30 | 30 | * The list of loaded config |
| 31 | 31 | * @var array |
| 32 | 32 | */ |
@@ -43,15 +43,15 @@ discard block |
||
| 43 | 43 | if(empty($config) || ! is_array($config)){ |
| 44 | 44 | show_error('No configuration found in ' . CONFIG_PATH . 'config_pagination.php'); |
| 45 | 45 | } |
| 46 | - else{ |
|
| 47 | - if(! empty($overwriteConfig)){ |
|
| 48 | - $config = array_merge($config, $overwriteConfig); |
|
| 49 | - } |
|
| 50 | - $this->config = $config; |
|
| 46 | + else{ |
|
| 47 | + if(! empty($overwriteConfig)){ |
|
| 48 | + $config = array_merge($config, $overwriteConfig); |
|
| 49 | + } |
|
| 50 | + $this->config = $config; |
|
| 51 | 51 | //put it gobally |
| 52 | - Config::setAll($config); |
|
| 53 | - unset($config); |
|
| 54 | - } |
|
| 52 | + Config::setAll($config); |
|
| 53 | + unset($config); |
|
| 54 | + } |
|
| 55 | 55 | } |
| 56 | 56 | else{ |
| 57 | 57 | show_error('Unable to find the pagination configuration file'); |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | public function getLink($totalRows, $currentPageNumber){ |
| 81 | 81 | $pageQueryName = $this->config['page_query_string_name']; |
| 82 | 82 | $numberOfLink = $this->config['nb_link']; |
| 83 | - $numberOfRowPerPage = $this->config['pagination_per_page']; |
|
| 83 | + $numberOfRowPerPage = $this->config['pagination_per_page']; |
|
| 84 | 84 | $queryString = Url::queryString(); |
| 85 | 85 | $currentUrl = Url::current(); |
| 86 | 86 | if($queryString == ''){ |
@@ -106,9 +106,9 @@ discard block |
||
| 106 | 106 | $navbar = ''; |
| 107 | 107 | $numberOfPage = ceil($totalRows / $numberOfRowPerPage); |
| 108 | 108 | $currentPageNumber = (int) $currentPageNumber; |
| 109 | - if($currentPageNumber <= 0){ |
|
| 110 | - $currentPageNumber = 1; |
|
| 111 | - } |
|
| 109 | + if($currentPageNumber <= 0){ |
|
| 110 | + $currentPageNumber = 1; |
|
| 111 | + } |
|
| 112 | 112 | if($numberOfPage <= 1 || $numberOfLink <= 0 || $numberOfRowPerPage <= 0 || !is_numeric($numberOfLink) || !is_numeric($numberOfRowPerPage) |
| 113 | 113 | ){ |
| 114 | 114 | return $navbar; |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | 27 | |
| 28 | - class Form{ |
|
| 28 | + class Form { |
|
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | 31 | * Generate the form opened tag |
@@ -35,33 +35,33 @@ discard block |
||
| 35 | 35 | * @param string $enctype the form enctype like "multipart/form-data" |
| 36 | 36 | * @return string the generated form html |
| 37 | 37 | */ |
| 38 | - public static function open($path = null, array $attributes = array(), $method = 'POST', $enctype = null){ |
|
| 39 | - if($path){ |
|
| 38 | + public static function open($path = null, array $attributes = array(), $method = 'POST', $enctype = null) { |
|
| 39 | + if ($path) { |
|
| 40 | 40 | $path = Url::site_url($path); |
| 41 | 41 | } |
| 42 | 42 | $method = strtoupper($method); |
| 43 | 43 | $str = null; |
| 44 | - $str .= '<form action = "'.$path.'" method = "'.$method.'"'; |
|
| 45 | - if(! empty($enctype)){ |
|
| 46 | - $str .= ' enctype = "'.$enctype.'" '; |
|
| 44 | + $str .= '<form action = "' . $path . '" method = "' . $method . '"'; |
|
| 45 | + if (!empty($enctype)) { |
|
| 46 | + $str .= ' enctype = "' . $enctype . '" '; |
|
| 47 | 47 | } |
| 48 | - if(! isset($attributes['accept-charset'])){ |
|
| 48 | + if (!isset($attributes['accept-charset'])) { |
|
| 49 | 49 | $attributes['accept-charset'] = get_config('charset', 'utf-8'); |
| 50 | 50 | } |
| 51 | 51 | $str .= attributes_to_string($attributes); |
| 52 | 52 | $str .= '>'; |
| 53 | 53 | $checkCsrf = false; |
| 54 | 54 | //check if the user set the checking of CSRF manually |
| 55 | - if($method != 'POST' && isset($attributes['csrf'])){ |
|
| 55 | + if ($method != 'POST' && isset($attributes['csrf'])) { |
|
| 56 | 56 | $obj = & get_instance(); |
| 57 | - if(! isset($obj->formvalidation)){ |
|
| 57 | + if (!isset($obj->formvalidation)) { |
|
| 58 | 58 | Loader::library('FormValidation'); |
| 59 | 59 | } |
| 60 | 60 | $obj->formvalidation->enableCsrfCheck = true; |
| 61 | 61 | $checkCsrf = true; |
| 62 | 62 | } |
| 63 | 63 | //if CSRF enable or is set manually |
| 64 | - if((get_config('csrf_enable', false) && $method == 'POST') || ($method != 'POST' && $checkCsrf)){ |
|
| 64 | + if ((get_config('csrf_enable', false) && $method == 'POST') || ($method != 'POST' && $checkCsrf)) { |
|
| 65 | 65 | $csrfValue = Security::generateCSRF(); |
| 66 | 66 | $csrfName = get_config('csrf_key', 'csrf_key'); |
| 67 | 67 | $str .= static::hidden($csrfName, $csrfValue); |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | * @see Form::open() for more details |
| 75 | 75 | * @return string the generated multipart form html |
| 76 | 76 | */ |
| 77 | - public static function openMultipart($path = null, array $attributes = array(), $method = 'POST'){ |
|
| 77 | + public static function openMultipart($path = null, array $attributes = array(), $method = 'POST') { |
|
| 78 | 78 | return self::open($path, $attributes, $method, 'multipart/form-data'); |
| 79 | 79 | } |
| 80 | 80 | |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | * Generate the form close |
| 83 | 83 | * @return string the form close html |
| 84 | 84 | */ |
| 85 | - public static function close(){ |
|
| 85 | + public static function close() { |
|
| 86 | 86 | return '</form>'; |
| 87 | 87 | } |
| 88 | 88 | |
@@ -93,10 +93,10 @@ discard block |
||
| 93 | 93 | * @param array $legendAttributes the legend additional HTML attributes. Is used only is $legend is not empty |
| 94 | 94 | * @return string the generated fieldset value |
| 95 | 95 | */ |
| 96 | - public static function fieldset($legend = '', array $fieldsetAttributes = array(), array $legendAttributes = array()){ |
|
| 96 | + public static function fieldset($legend = '', array $fieldsetAttributes = array(), array $legendAttributes = array()) { |
|
| 97 | 97 | $str = '<fieldset' . attributes_to_string($fieldsetAttributes) . '>'; |
| 98 | - if($legend){ |
|
| 99 | - $str .= '<legend' . attributes_to_string($legendAttributes) . '>'.$legend.'</legend>'; |
|
| 98 | + if ($legend) { |
|
| 99 | + $str .= '<legend' . attributes_to_string($legendAttributes) . '>' . $legend . '</legend>'; |
|
| 100 | 100 | } |
| 101 | 101 | return $str; |
| 102 | 102 | } |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | * Generate the fieldset close tag |
| 106 | 106 | * @return string the generated html for fieldset close |
| 107 | 107 | */ |
| 108 | - public static function fieldsetClose(){ |
|
| 108 | + public static function fieldsetClose() { |
|
| 109 | 109 | return '</fieldset>'; |
| 110 | 110 | } |
| 111 | 111 | |
@@ -115,13 +115,13 @@ discard block |
||
| 115 | 115 | * @param string $name the form field name |
| 116 | 116 | * @return string the error message if exists and null if not |
| 117 | 117 | */ |
| 118 | - public static function error($name){ |
|
| 118 | + public static function error($name) { |
|
| 119 | 119 | $return = null; |
| 120 | 120 | $obj = & get_instance(); |
| 121 | - if(isset($obj->formvalidation)){ |
|
| 121 | + if (isset($obj->formvalidation)) { |
|
| 122 | 122 | $errors = $obj->formvalidation->returnErrors(); |
| 123 | - $error = isset($errors[$name]) ? $errors[$name] : null; |
|
| 124 | - if($error){ |
|
| 123 | + $error = isset($errors[$name]) ? $errors[$name] : null; |
|
| 124 | + if ($error) { |
|
| 125 | 125 | list($errorStart, $errorEnd) = $obj->formvalidation->getErrorDelimiter(); |
| 126 | 126 | $return = $errorStart . $error . $errorEnd; |
| 127 | 127 | } |
@@ -135,7 +135,7 @@ discard block |
||
| 135 | 135 | * @param mixed $default the default value if can not found the given form field name |
| 136 | 136 | * @return mixed the form field value if is set, otherwise return the default value. |
| 137 | 137 | */ |
| 138 | - public static function value($name, $default = null){ |
|
| 138 | + public static function value($name, $default = null) { |
|
| 139 | 139 | $value = get_instance()->request->query($name); |
| 140 | 140 | return $value ? $value : $default; |
| 141 | 141 | } |
@@ -147,14 +147,14 @@ discard block |
||
| 147 | 147 | * @param array $attributes the additional attributes to be added |
| 148 | 148 | * @return string the generated label html content |
| 149 | 149 | */ |
| 150 | - public static function label($label, $for = '', array $attributes = array()){ |
|
| 150 | + public static function label($label, $for = '', array $attributes = array()) { |
|
| 151 | 151 | $str = '<label'; |
| 152 | - if($for){ |
|
| 153 | - $str .= ' for = "'.$for.'"'; |
|
| 152 | + if ($for) { |
|
| 153 | + $str .= ' for = "' . $for . '"'; |
|
| 154 | 154 | } |
| 155 | 155 | $str .= attributes_to_string($attributes); |
| 156 | 156 | $str .= '>'; |
| 157 | - $str .= $label.'</label>'; |
|
| 157 | + $str .= $label . '</label>'; |
|
| 158 | 158 | return $str; |
| 159 | 159 | } |
| 160 | 160 | |
@@ -166,9 +166,9 @@ discard block |
||
| 166 | 166 | * @param string $type the type of the form field (password, text, submit, button, etc.) |
| 167 | 167 | * @return string the generated form field html content for the input |
| 168 | 168 | */ |
| 169 | - public static function input($name, $value = null, array $attributes = array(), $type = 'text'){ |
|
| 169 | + public static function input($name, $value = null, array $attributes = array(), $type = 'text') { |
|
| 170 | 170 | $str = null; |
| 171 | - $str .= '<input name = "'.$name.'" value = "'.$value.'" type = "'.$type.'"'; |
|
| 171 | + $str .= '<input name = "' . $name . '" value = "' . $value . '" type = "' . $type . '"'; |
|
| 172 | 172 | $str .= attributes_to_string($attributes); |
| 173 | 173 | $str .= '/>'; |
| 174 | 174 | return $str; |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | * Generate the form field for "text" |
| 179 | 179 | * @see Form::input() for more details |
| 180 | 180 | */ |
| 181 | - public static function text($name, $value = null, array $attributes = array()){ |
|
| 181 | + public static function text($name, $value = null, array $attributes = array()) { |
|
| 182 | 182 | return self::input($name, $value, $attributes, 'text'); |
| 183 | 183 | } |
| 184 | 184 | |
@@ -186,7 +186,7 @@ discard block |
||
| 186 | 186 | * Generate the form field for "password" |
| 187 | 187 | * @see Form::input() for more details |
| 188 | 188 | */ |
| 189 | - public static function password($name, $value = null, array $attributes = array()){ |
|
| 189 | + public static function password($name, $value = null, array $attributes = array()) { |
|
| 190 | 190 | return self::input($name, $value, $attributes, 'password'); |
| 191 | 191 | } |
| 192 | 192 | |
@@ -194,8 +194,8 @@ discard block |
||
| 194 | 194 | * Generate the form field for "radio" |
| 195 | 195 | * @see Form::input() for more details |
| 196 | 196 | */ |
| 197 | - public static function radio($name, $value = null, $checked = false, array $attributes = array()){ |
|
| 198 | - if($checked){ |
|
| 197 | + public static function radio($name, $value = null, $checked = false, array $attributes = array()) { |
|
| 198 | + if ($checked) { |
|
| 199 | 199 | $attributes['checked'] = true; |
| 200 | 200 | } |
| 201 | 201 | return self::input($name, $value, $attributes, 'radio'); |
@@ -205,8 +205,8 @@ discard block |
||
| 205 | 205 | * Generate the form field for "checkbox" |
| 206 | 206 | * @see Form::input() for more details |
| 207 | 207 | */ |
| 208 | - public static function checkbox($name, $value = null, $checked = false, array $attributes = array()){ |
|
| 209 | - if($checked){ |
|
| 208 | + public static function checkbox($name, $value = null, $checked = false, array $attributes = array()) { |
|
| 209 | + if ($checked) { |
|
| 210 | 210 | $attributes['checked'] = true; |
| 211 | 211 | } |
| 212 | 212 | return self::input($name, $value, $attributes, 'checkbox'); |
@@ -216,7 +216,7 @@ discard block |
||
| 216 | 216 | * Generate the form field for "number" |
| 217 | 217 | * @see Form::input() for more details |
| 218 | 218 | */ |
| 219 | - public static function number($name, $value = null, array $attributes = array()){ |
|
| 219 | + public static function number($name, $value = null, array $attributes = array()) { |
|
| 220 | 220 | return self::input($name, $value, $attributes, 'number'); |
| 221 | 221 | } |
| 222 | 222 | |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | * Generate the form field for "phone" |
| 225 | 225 | * @see Form::input() for more details |
| 226 | 226 | */ |
| 227 | - public static function phone($name, $value = null, array $attributes = array()){ |
|
| 227 | + public static function phone($name, $value = null, array $attributes = array()) { |
|
| 228 | 228 | return self::input($name, $value, $attributes, 'phone'); |
| 229 | 229 | } |
| 230 | 230 | |
@@ -232,7 +232,7 @@ discard block |
||
| 232 | 232 | * Generate the form field for "email" |
| 233 | 233 | * @see Form::input() for more details |
| 234 | 234 | */ |
| 235 | - public static function email($name, $value = null, array $attributes = array()){ |
|
| 235 | + public static function email($name, $value = null, array $attributes = array()) { |
|
| 236 | 236 | return self::input($name, $value, $attributes, 'email'); |
| 237 | 237 | } |
| 238 | 238 | |
@@ -240,7 +240,7 @@ discard block |
||
| 240 | 240 | * Generate the form field for "search" |
| 241 | 241 | * @see Form::input() for more details |
| 242 | 242 | */ |
| 243 | - public static function search($name, $value = null, array $attributes = array()){ |
|
| 243 | + public static function search($name, $value = null, array $attributes = array()) { |
|
| 244 | 244 | return self::input($name, $value, $attributes, 'search'); |
| 245 | 245 | } |
| 246 | 246 | |
@@ -248,7 +248,7 @@ discard block |
||
| 248 | 248 | * Generate the form field for "hidden" |
| 249 | 249 | * @see Form::input() for more details |
| 250 | 250 | */ |
| 251 | - public static function hidden($name, $value = null, array $attributes = array()){ |
|
| 251 | + public static function hidden($name, $value = null, array $attributes = array()) { |
|
| 252 | 252 | return self::input($name, $value, $attributes, 'hidden'); |
| 253 | 253 | } |
| 254 | 254 | |
@@ -256,7 +256,7 @@ discard block |
||
| 256 | 256 | * Generate the form field for "file" |
| 257 | 257 | * @see Form::input() for more details |
| 258 | 258 | */ |
| 259 | - public static function file($name, array $attributes = array()){ |
|
| 259 | + public static function file($name, array $attributes = array()) { |
|
| 260 | 260 | return self::input($name, null, $attributes, 'file'); |
| 261 | 261 | } |
| 262 | 262 | |
@@ -264,7 +264,7 @@ discard block |
||
| 264 | 264 | * Generate the form field for "button" |
| 265 | 265 | * @see Form::input() for more details |
| 266 | 266 | */ |
| 267 | - public static function button($name, $value = null, array $attributes = array()){ |
|
| 267 | + public static function button($name, $value = null, array $attributes = array()) { |
|
| 268 | 268 | return self::input($name, $value, $attributes, 'button'); |
| 269 | 269 | } |
| 270 | 270 | |
@@ -272,7 +272,7 @@ discard block |
||
| 272 | 272 | * Generate the form field for "reset" |
| 273 | 273 | * @see Form::input() for more details |
| 274 | 274 | */ |
| 275 | - public static function reset($name, $value = null, array $attributes = array()){ |
|
| 275 | + public static function reset($name, $value = null, array $attributes = array()) { |
|
| 276 | 276 | return self::input($name, $value, $attributes, 'reset'); |
| 277 | 277 | } |
| 278 | 278 | |
@@ -280,7 +280,7 @@ discard block |
||
| 280 | 280 | * Generate the form field for "submit" |
| 281 | 281 | * @see Form::input() for more details |
| 282 | 282 | */ |
| 283 | - public static function submit($name, $value = null, array $attributes = array()){ |
|
| 283 | + public static function submit($name, $value = null, array $attributes = array()) { |
|
| 284 | 284 | return self::input($name, $value, $attributes, 'submit'); |
| 285 | 285 | } |
| 286 | 286 | |
@@ -291,12 +291,12 @@ discard block |
||
| 291 | 291 | * @param array $attributes the additional attributes to be added |
| 292 | 292 | * @return string the generated textarea form html content |
| 293 | 293 | */ |
| 294 | - public static function textarea($name, $value = '', array $attributes = array()){ |
|
| 294 | + public static function textarea($name, $value = '', array $attributes = array()) { |
|
| 295 | 295 | $str = null; |
| 296 | - $str .= '<textarea name = "'.$name.'"'; |
|
| 296 | + $str .= '<textarea name = "' . $name . '"'; |
|
| 297 | 297 | $str .= attributes_to_string($attributes); |
| 298 | 298 | $str .= '>'; |
| 299 | - $str .= $value.'</textarea>'; |
|
| 299 | + $str .= $value . '</textarea>'; |
|
| 300 | 300 | return $str; |
| 301 | 301 | } |
| 302 | 302 | |
@@ -308,20 +308,20 @@ discard block |
||
| 308 | 308 | * @param array $attributes the additional attribute to be added |
| 309 | 309 | * @return string the generated form field html content for select |
| 310 | 310 | */ |
| 311 | - public static function select($name, $values = null, $selected = null, array $attributes = array()){ |
|
| 312 | - if(! is_array($values)){ |
|
| 311 | + public static function select($name, $values = null, $selected = null, array $attributes = array()) { |
|
| 312 | + if (!is_array($values)) { |
|
| 313 | 313 | $values = array('' => $values); |
| 314 | 314 | } |
| 315 | 315 | $str = null; |
| 316 | - $str .= '<select name = "'.$name.'"'; |
|
| 316 | + $str .= '<select name = "' . $name . '"'; |
|
| 317 | 317 | $str .= attributes_to_string($attributes); |
| 318 | 318 | $str .= '>'; |
| 319 | - foreach($values as $key => $val){ |
|
| 319 | + foreach ($values as $key => $val) { |
|
| 320 | 320 | $select = ''; |
| 321 | - if($key == $selected){ |
|
| 321 | + if ($key == $selected) { |
|
| 322 | 322 | $select = 'selected'; |
| 323 | 323 | } |
| 324 | - $str .= '<option value = "'.$key.'" '.$select.'>'.$val.'</option>'; |
|
| 324 | + $str .= '<option value = "' . $key . '" ' . $select . '>' . $val . '</option>'; |
|
| 325 | 325 | } |
| 326 | 326 | $str .= '</select>'; |
| 327 | 327 | return $str; |
@@ -1,330 +1,330 @@ |
||
| 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 | - */ |
|
| 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 | 26 | |
| 27 | 27 | |
| 28 | - class Form{ |
|
| 28 | + class Form{ |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Generate the form opened tag |
|
| 32 | - * @param string $path the form action path |
|
| 33 | - * @param array $attributes the additional form attributes |
|
| 34 | - * @param string $method the form method like 'GET', 'POST' |
|
| 35 | - * @param string $enctype the form enctype like "multipart/form-data" |
|
| 36 | - * @return string the generated form html |
|
| 37 | - */ |
|
| 38 | - public static function open($path = null, array $attributes = array(), $method = 'POST', $enctype = null){ |
|
| 39 | - if($path){ |
|
| 40 | - $path = Url::site_url($path); |
|
| 41 | - } |
|
| 42 | - $method = strtoupper($method); |
|
| 43 | - $str = null; |
|
| 44 | - $str .= '<form action = "'.$path.'" method = "'.$method.'"'; |
|
| 45 | - if(! empty($enctype)){ |
|
| 46 | - $str .= ' enctype = "'.$enctype.'" '; |
|
| 47 | - } |
|
| 48 | - if(! isset($attributes['accept-charset'])){ |
|
| 49 | - $attributes['accept-charset'] = get_config('charset', 'utf-8'); |
|
| 50 | - } |
|
| 51 | - $str .= attributes_to_string($attributes); |
|
| 52 | - $str .= '>'; |
|
| 53 | - $checkCsrf = false; |
|
| 54 | - //check if the user set the checking of CSRF manually |
|
| 55 | - if($method != 'POST' && isset($attributes['csrf'])){ |
|
| 56 | - $obj = & get_instance(); |
|
| 57 | - if(! isset($obj->formvalidation)){ |
|
| 58 | - Loader::library('FormValidation'); |
|
| 59 | - } |
|
| 60 | - $obj->formvalidation->enableCsrfCheck = true; |
|
| 61 | - $checkCsrf = true; |
|
| 62 | - } |
|
| 63 | - //if CSRF enable or is set manually |
|
| 64 | - if((get_config('csrf_enable', false) && $method == 'POST') || ($method != 'POST' && $checkCsrf)){ |
|
| 65 | - $csrfValue = Security::generateCSRF(); |
|
| 66 | - $csrfName = get_config('csrf_key', 'csrf_key'); |
|
| 67 | - $str .= static::hidden($csrfName, $csrfValue); |
|
| 68 | - } |
|
| 69 | - return $str; |
|
| 70 | - } |
|
| 30 | + /** |
|
| 31 | + * Generate the form opened tag |
|
| 32 | + * @param string $path the form action path |
|
| 33 | + * @param array $attributes the additional form attributes |
|
| 34 | + * @param string $method the form method like 'GET', 'POST' |
|
| 35 | + * @param string $enctype the form enctype like "multipart/form-data" |
|
| 36 | + * @return string the generated form html |
|
| 37 | + */ |
|
| 38 | + public static function open($path = null, array $attributes = array(), $method = 'POST', $enctype = null){ |
|
| 39 | + if($path){ |
|
| 40 | + $path = Url::site_url($path); |
|
| 41 | + } |
|
| 42 | + $method = strtoupper($method); |
|
| 43 | + $str = null; |
|
| 44 | + $str .= '<form action = "'.$path.'" method = "'.$method.'"'; |
|
| 45 | + if(! empty($enctype)){ |
|
| 46 | + $str .= ' enctype = "'.$enctype.'" '; |
|
| 47 | + } |
|
| 48 | + if(! isset($attributes['accept-charset'])){ |
|
| 49 | + $attributes['accept-charset'] = get_config('charset', 'utf-8'); |
|
| 50 | + } |
|
| 51 | + $str .= attributes_to_string($attributes); |
|
| 52 | + $str .= '>'; |
|
| 53 | + $checkCsrf = false; |
|
| 54 | + //check if the user set the checking of CSRF manually |
|
| 55 | + if($method != 'POST' && isset($attributes['csrf'])){ |
|
| 56 | + $obj = & get_instance(); |
|
| 57 | + if(! isset($obj->formvalidation)){ |
|
| 58 | + Loader::library('FormValidation'); |
|
| 59 | + } |
|
| 60 | + $obj->formvalidation->enableCsrfCheck = true; |
|
| 61 | + $checkCsrf = true; |
|
| 62 | + } |
|
| 63 | + //if CSRF enable or is set manually |
|
| 64 | + if((get_config('csrf_enable', false) && $method == 'POST') || ($method != 'POST' && $checkCsrf)){ |
|
| 65 | + $csrfValue = Security::generateCSRF(); |
|
| 66 | + $csrfName = get_config('csrf_key', 'csrf_key'); |
|
| 67 | + $str .= static::hidden($csrfName, $csrfValue); |
|
| 68 | + } |
|
| 69 | + return $str; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Generate the form opened tag for multipart like to send a file |
|
| 74 | - * @see Form::open() for more details |
|
| 75 | - * @return string the generated multipart form html |
|
| 76 | - */ |
|
| 77 | - public static function openMultipart($path = null, array $attributes = array(), $method = 'POST'){ |
|
| 78 | - return self::open($path, $attributes, $method, 'multipart/form-data'); |
|
| 79 | - } |
|
| 72 | + /** |
|
| 73 | + * Generate the form opened tag for multipart like to send a file |
|
| 74 | + * @see Form::open() for more details |
|
| 75 | + * @return string the generated multipart form html |
|
| 76 | + */ |
|
| 77 | + public static function openMultipart($path = null, array $attributes = array(), $method = 'POST'){ |
|
| 78 | + return self::open($path, $attributes, $method, 'multipart/form-data'); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * Generate the form close |
|
| 83 | - * @return string the form close html |
|
| 84 | - */ |
|
| 85 | - public static function close(){ |
|
| 86 | - return '</form>'; |
|
| 87 | - } |
|
| 81 | + /** |
|
| 82 | + * Generate the form close |
|
| 83 | + * @return string the form close html |
|
| 84 | + */ |
|
| 85 | + public static function close(){ |
|
| 86 | + return '</form>'; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Generate the form fieldset & legend |
|
| 91 | - * @param string $legend the legend tag value |
|
| 92 | - * @param array $fieldsetAttributes the fieldset additional HTML attributes |
|
| 93 | - * @param array $legendAttributes the legend additional HTML attributes. Is used only is $legend is not empty |
|
| 94 | - * @return string the generated fieldset value |
|
| 95 | - */ |
|
| 96 | - public static function fieldset($legend = '', array $fieldsetAttributes = array(), array $legendAttributes = array()){ |
|
| 97 | - $str = '<fieldset' . attributes_to_string($fieldsetAttributes) . '>'; |
|
| 98 | - if($legend){ |
|
| 99 | - $str .= '<legend' . attributes_to_string($legendAttributes) . '>'.$legend.'</legend>'; |
|
| 100 | - } |
|
| 101 | - return $str; |
|
| 102 | - } |
|
| 89 | + /** |
|
| 90 | + * Generate the form fieldset & legend |
|
| 91 | + * @param string $legend the legend tag value |
|
| 92 | + * @param array $fieldsetAttributes the fieldset additional HTML attributes |
|
| 93 | + * @param array $legendAttributes the legend additional HTML attributes. Is used only is $legend is not empty |
|
| 94 | + * @return string the generated fieldset value |
|
| 95 | + */ |
|
| 96 | + public static function fieldset($legend = '', array $fieldsetAttributes = array(), array $legendAttributes = array()){ |
|
| 97 | + $str = '<fieldset' . attributes_to_string($fieldsetAttributes) . '>'; |
|
| 98 | + if($legend){ |
|
| 99 | + $str .= '<legend' . attributes_to_string($legendAttributes) . '>'.$legend.'</legend>'; |
|
| 100 | + } |
|
| 101 | + return $str; |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * Generate the fieldset close tag |
|
| 106 | - * @return string the generated html for fieldset close |
|
| 107 | - */ |
|
| 108 | - public static function fieldsetClose(){ |
|
| 109 | - return '</fieldset>'; |
|
| 110 | - } |
|
| 104 | + /** |
|
| 105 | + * Generate the fieldset close tag |
|
| 106 | + * @return string the generated html for fieldset close |
|
| 107 | + */ |
|
| 108 | + public static function fieldsetClose(){ |
|
| 109 | + return '</fieldset>'; |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * Get the error message for the given form field name. |
|
| 114 | - * This use the form validation information to get the error information. |
|
| 115 | - * @param string $name the form field name |
|
| 116 | - * @return string the error message if exists and null if not |
|
| 117 | - */ |
|
| 118 | - public static function error($name){ |
|
| 119 | - $return = null; |
|
| 120 | - $obj = & get_instance(); |
|
| 121 | - if(isset($obj->formvalidation)){ |
|
| 122 | - $errors = $obj->formvalidation->returnErrors(); |
|
| 123 | - $error = isset($errors[$name]) ? $errors[$name] : null; |
|
| 124 | - if($error){ |
|
| 125 | - list($errorStart, $errorEnd) = $obj->formvalidation->getErrorDelimiter(); |
|
| 126 | - $return = $errorStart . $error . $errorEnd; |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - return $return; |
|
| 130 | - } |
|
| 112 | + /** |
|
| 113 | + * Get the error message for the given form field name. |
|
| 114 | + * This use the form validation information to get the error information. |
|
| 115 | + * @param string $name the form field name |
|
| 116 | + * @return string the error message if exists and null if not |
|
| 117 | + */ |
|
| 118 | + public static function error($name){ |
|
| 119 | + $return = null; |
|
| 120 | + $obj = & get_instance(); |
|
| 121 | + if(isset($obj->formvalidation)){ |
|
| 122 | + $errors = $obj->formvalidation->returnErrors(); |
|
| 123 | + $error = isset($errors[$name]) ? $errors[$name] : null; |
|
| 124 | + if($error){ |
|
| 125 | + list($errorStart, $errorEnd) = $obj->formvalidation->getErrorDelimiter(); |
|
| 126 | + $return = $errorStart . $error . $errorEnd; |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + return $return; |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - /** |
|
| 133 | - * Get the form field value |
|
| 134 | - * @param string $name the form field name |
|
| 135 | - * @param mixed $default the default value if can not found the given form field name |
|
| 136 | - * @return mixed the form field value if is set, otherwise return the default value. |
|
| 137 | - */ |
|
| 138 | - public static function value($name, $default = null){ |
|
| 139 | - $value = get_instance()->request->query($name); |
|
| 140 | - return $value ? $value : $default; |
|
| 141 | - } |
|
| 132 | + /** |
|
| 133 | + * Get the form field value |
|
| 134 | + * @param string $name the form field name |
|
| 135 | + * @param mixed $default the default value if can not found the given form field name |
|
| 136 | + * @return mixed the form field value if is set, otherwise return the default value. |
|
| 137 | + */ |
|
| 138 | + public static function value($name, $default = null){ |
|
| 139 | + $value = get_instance()->request->query($name); |
|
| 140 | + return $value ? $value : $default; |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - /** |
|
| 144 | - * Generate the form label html content |
|
| 145 | - * @param string $label the title of the label |
|
| 146 | - * @param string $for the value of the label "for" attribute |
|
| 147 | - * @param array $attributes the additional attributes to be added |
|
| 148 | - * @return string the generated label html content |
|
| 149 | - */ |
|
| 150 | - public static function label($label, $for = '', array $attributes = array()){ |
|
| 151 | - $str = '<label'; |
|
| 152 | - if($for){ |
|
| 153 | - $str .= ' for = "'.$for.'"'; |
|
| 154 | - } |
|
| 155 | - $str .= attributes_to_string($attributes); |
|
| 156 | - $str .= '>'; |
|
| 157 | - $str .= $label.'</label>'; |
|
| 158 | - return $str; |
|
| 159 | - } |
|
| 143 | + /** |
|
| 144 | + * Generate the form label html content |
|
| 145 | + * @param string $label the title of the label |
|
| 146 | + * @param string $for the value of the label "for" attribute |
|
| 147 | + * @param array $attributes the additional attributes to be added |
|
| 148 | + * @return string the generated label html content |
|
| 149 | + */ |
|
| 150 | + public static function label($label, $for = '', array $attributes = array()){ |
|
| 151 | + $str = '<label'; |
|
| 152 | + if($for){ |
|
| 153 | + $str .= ' for = "'.$for.'"'; |
|
| 154 | + } |
|
| 155 | + $str .= attributes_to_string($attributes); |
|
| 156 | + $str .= '>'; |
|
| 157 | + $str .= $label.'</label>'; |
|
| 158 | + return $str; |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | - /** |
|
| 162 | - * Generate the form field for input like "text", "email", "password", etc. |
|
| 163 | - * @param string $name the form field name |
|
| 164 | - * @param mixed $value the form field value to be set |
|
| 165 | - * @param array $attributes the additional attributes to be added in the form input |
|
| 166 | - * @param string $type the type of the form field (password, text, submit, button, etc.) |
|
| 167 | - * @return string the generated form field html content for the input |
|
| 168 | - */ |
|
| 169 | - public static function input($name, $value = null, array $attributes = array(), $type = 'text'){ |
|
| 170 | - $str = null; |
|
| 171 | - $str .= '<input name = "'.$name.'" value = "'.$value.'" type = "'.$type.'"'; |
|
| 172 | - $str .= attributes_to_string($attributes); |
|
| 173 | - $str .= '/>'; |
|
| 174 | - return $str; |
|
| 175 | - } |
|
| 161 | + /** |
|
| 162 | + * Generate the form field for input like "text", "email", "password", etc. |
|
| 163 | + * @param string $name the form field name |
|
| 164 | + * @param mixed $value the form field value to be set |
|
| 165 | + * @param array $attributes the additional attributes to be added in the form input |
|
| 166 | + * @param string $type the type of the form field (password, text, submit, button, etc.) |
|
| 167 | + * @return string the generated form field html content for the input |
|
| 168 | + */ |
|
| 169 | + public static function input($name, $value = null, array $attributes = array(), $type = 'text'){ |
|
| 170 | + $str = null; |
|
| 171 | + $str .= '<input name = "'.$name.'" value = "'.$value.'" type = "'.$type.'"'; |
|
| 172 | + $str .= attributes_to_string($attributes); |
|
| 173 | + $str .= '/>'; |
|
| 174 | + return $str; |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - /** |
|
| 178 | - * Generate the form field for "text" |
|
| 179 | - * @see Form::input() for more details |
|
| 180 | - */ |
|
| 181 | - public static function text($name, $value = null, array $attributes = array()){ |
|
| 182 | - return self::input($name, $value, $attributes, 'text'); |
|
| 183 | - } |
|
| 177 | + /** |
|
| 178 | + * Generate the form field for "text" |
|
| 179 | + * @see Form::input() for more details |
|
| 180 | + */ |
|
| 181 | + public static function text($name, $value = null, array $attributes = array()){ |
|
| 182 | + return self::input($name, $value, $attributes, 'text'); |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - /** |
|
| 186 | - * Generate the form field for "password" |
|
| 187 | - * @see Form::input() for more details |
|
| 188 | - */ |
|
| 189 | - public static function password($name, $value = null, array $attributes = array()){ |
|
| 190 | - return self::input($name, $value, $attributes, 'password'); |
|
| 191 | - } |
|
| 185 | + /** |
|
| 186 | + * Generate the form field for "password" |
|
| 187 | + * @see Form::input() for more details |
|
| 188 | + */ |
|
| 189 | + public static function password($name, $value = null, array $attributes = array()){ |
|
| 190 | + return self::input($name, $value, $attributes, 'password'); |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | - /** |
|
| 194 | - * Generate the form field for "radio" |
|
| 195 | - * @see Form::input() for more details |
|
| 196 | - */ |
|
| 197 | - public static function radio($name, $value = null, $checked = false, array $attributes = array()){ |
|
| 198 | - if($checked){ |
|
| 199 | - $attributes['checked'] = true; |
|
| 200 | - } |
|
| 201 | - return self::input($name, $value, $attributes, 'radio'); |
|
| 202 | - } |
|
| 193 | + /** |
|
| 194 | + * Generate the form field for "radio" |
|
| 195 | + * @see Form::input() for more details |
|
| 196 | + */ |
|
| 197 | + public static function radio($name, $value = null, $checked = false, array $attributes = array()){ |
|
| 198 | + if($checked){ |
|
| 199 | + $attributes['checked'] = true; |
|
| 200 | + } |
|
| 201 | + return self::input($name, $value, $attributes, 'radio'); |
|
| 202 | + } |
|
| 203 | 203 | |
| 204 | - /** |
|
| 205 | - * Generate the form field for "checkbox" |
|
| 206 | - * @see Form::input() for more details |
|
| 207 | - */ |
|
| 208 | - public static function checkbox($name, $value = null, $checked = false, array $attributes = array()){ |
|
| 209 | - if($checked){ |
|
| 210 | - $attributes['checked'] = true; |
|
| 211 | - } |
|
| 212 | - return self::input($name, $value, $attributes, 'checkbox'); |
|
| 213 | - } |
|
| 204 | + /** |
|
| 205 | + * Generate the form field for "checkbox" |
|
| 206 | + * @see Form::input() for more details |
|
| 207 | + */ |
|
| 208 | + public static function checkbox($name, $value = null, $checked = false, array $attributes = array()){ |
|
| 209 | + if($checked){ |
|
| 210 | + $attributes['checked'] = true; |
|
| 211 | + } |
|
| 212 | + return self::input($name, $value, $attributes, 'checkbox'); |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | - /** |
|
| 216 | - * Generate the form field for "number" |
|
| 217 | - * @see Form::input() for more details |
|
| 218 | - */ |
|
| 219 | - public static function number($name, $value = null, array $attributes = array()){ |
|
| 220 | - return self::input($name, $value, $attributes, 'number'); |
|
| 221 | - } |
|
| 215 | + /** |
|
| 216 | + * Generate the form field for "number" |
|
| 217 | + * @see Form::input() for more details |
|
| 218 | + */ |
|
| 219 | + public static function number($name, $value = null, array $attributes = array()){ |
|
| 220 | + return self::input($name, $value, $attributes, 'number'); |
|
| 221 | + } |
|
| 222 | 222 | |
| 223 | - /** |
|
| 224 | - * Generate the form field for "phone" |
|
| 225 | - * @see Form::input() for more details |
|
| 226 | - */ |
|
| 227 | - public static function phone($name, $value = null, array $attributes = array()){ |
|
| 228 | - return self::input($name, $value, $attributes, 'phone'); |
|
| 229 | - } |
|
| 223 | + /** |
|
| 224 | + * Generate the form field for "phone" |
|
| 225 | + * @see Form::input() for more details |
|
| 226 | + */ |
|
| 227 | + public static function phone($name, $value = null, array $attributes = array()){ |
|
| 228 | + return self::input($name, $value, $attributes, 'phone'); |
|
| 229 | + } |
|
| 230 | 230 | |
| 231 | - /** |
|
| 232 | - * Generate the form field for "email" |
|
| 233 | - * @see Form::input() for more details |
|
| 234 | - */ |
|
| 235 | - public static function email($name, $value = null, array $attributes = array()){ |
|
| 236 | - return self::input($name, $value, $attributes, 'email'); |
|
| 237 | - } |
|
| 231 | + /** |
|
| 232 | + * Generate the form field for "email" |
|
| 233 | + * @see Form::input() for more details |
|
| 234 | + */ |
|
| 235 | + public static function email($name, $value = null, array $attributes = array()){ |
|
| 236 | + return self::input($name, $value, $attributes, 'email'); |
|
| 237 | + } |
|
| 238 | 238 | |
| 239 | - /** |
|
| 240 | - * Generate the form field for "search" |
|
| 241 | - * @see Form::input() for more details |
|
| 242 | - */ |
|
| 243 | - public static function search($name, $value = null, array $attributes = array()){ |
|
| 244 | - return self::input($name, $value, $attributes, 'search'); |
|
| 245 | - } |
|
| 239 | + /** |
|
| 240 | + * Generate the form field for "search" |
|
| 241 | + * @see Form::input() for more details |
|
| 242 | + */ |
|
| 243 | + public static function search($name, $value = null, array $attributes = array()){ |
|
| 244 | + return self::input($name, $value, $attributes, 'search'); |
|
| 245 | + } |
|
| 246 | 246 | |
| 247 | - /** |
|
| 248 | - * Generate the form field for "hidden" |
|
| 249 | - * @see Form::input() for more details |
|
| 250 | - */ |
|
| 251 | - public static function hidden($name, $value = null, array $attributes = array()){ |
|
| 252 | - return self::input($name, $value, $attributes, 'hidden'); |
|
| 253 | - } |
|
| 247 | + /** |
|
| 248 | + * Generate the form field for "hidden" |
|
| 249 | + * @see Form::input() for more details |
|
| 250 | + */ |
|
| 251 | + public static function hidden($name, $value = null, array $attributes = array()){ |
|
| 252 | + return self::input($name, $value, $attributes, 'hidden'); |
|
| 253 | + } |
|
| 254 | 254 | |
| 255 | - /** |
|
| 256 | - * Generate the form field for "file" |
|
| 257 | - * @see Form::input() for more details |
|
| 258 | - */ |
|
| 259 | - public static function file($name, array $attributes = array()){ |
|
| 260 | - return self::input($name, null, $attributes, 'file'); |
|
| 261 | - } |
|
| 255 | + /** |
|
| 256 | + * Generate the form field for "file" |
|
| 257 | + * @see Form::input() for more details |
|
| 258 | + */ |
|
| 259 | + public static function file($name, array $attributes = array()){ |
|
| 260 | + return self::input($name, null, $attributes, 'file'); |
|
| 261 | + } |
|
| 262 | 262 | |
| 263 | - /** |
|
| 264 | - * Generate the form field for "button" |
|
| 265 | - * @see Form::input() for more details |
|
| 266 | - */ |
|
| 267 | - public static function button($name, $value = null, array $attributes = array()){ |
|
| 268 | - return self::input($name, $value, $attributes, 'button'); |
|
| 269 | - } |
|
| 263 | + /** |
|
| 264 | + * Generate the form field for "button" |
|
| 265 | + * @see Form::input() for more details |
|
| 266 | + */ |
|
| 267 | + public static function button($name, $value = null, array $attributes = array()){ |
|
| 268 | + return self::input($name, $value, $attributes, 'button'); |
|
| 269 | + } |
|
| 270 | 270 | |
| 271 | - /** |
|
| 272 | - * Generate the form field for "reset" |
|
| 273 | - * @see Form::input() for more details |
|
| 274 | - */ |
|
| 275 | - public static function reset($name, $value = null, array $attributes = array()){ |
|
| 276 | - return self::input($name, $value, $attributes, 'reset'); |
|
| 277 | - } |
|
| 271 | + /** |
|
| 272 | + * Generate the form field for "reset" |
|
| 273 | + * @see Form::input() for more details |
|
| 274 | + */ |
|
| 275 | + public static function reset($name, $value = null, array $attributes = array()){ |
|
| 276 | + return self::input($name, $value, $attributes, 'reset'); |
|
| 277 | + } |
|
| 278 | 278 | |
| 279 | - /** |
|
| 280 | - * Generate the form field for "submit" |
|
| 281 | - * @see Form::input() for more details |
|
| 282 | - */ |
|
| 283 | - public static function submit($name, $value = null, array $attributes = array()){ |
|
| 284 | - return self::input($name, $value, $attributes, 'submit'); |
|
| 285 | - } |
|
| 279 | + /** |
|
| 280 | + * Generate the form field for "submit" |
|
| 281 | + * @see Form::input() for more details |
|
| 282 | + */ |
|
| 283 | + public static function submit($name, $value = null, array $attributes = array()){ |
|
| 284 | + return self::input($name, $value, $attributes, 'submit'); |
|
| 285 | + } |
|
| 286 | 286 | |
| 287 | - /** |
|
| 288 | - * Generate the form field for textarea |
|
| 289 | - * @param string $name the name of the textarea field |
|
| 290 | - * @param string $value the textarea field value |
|
| 291 | - * @param array $attributes the additional attributes to be added |
|
| 292 | - * @return string the generated textarea form html content |
|
| 293 | - */ |
|
| 294 | - public static function textarea($name, $value = '', array $attributes = array()){ |
|
| 295 | - $str = null; |
|
| 296 | - $str .= '<textarea name = "'.$name.'"'; |
|
| 297 | - $str .= attributes_to_string($attributes); |
|
| 298 | - $str .= '>'; |
|
| 299 | - $str .= $value.'</textarea>'; |
|
| 300 | - return $str; |
|
| 301 | - } |
|
| 287 | + /** |
|
| 288 | + * Generate the form field for textarea |
|
| 289 | + * @param string $name the name of the textarea field |
|
| 290 | + * @param string $value the textarea field value |
|
| 291 | + * @param array $attributes the additional attributes to be added |
|
| 292 | + * @return string the generated textarea form html content |
|
| 293 | + */ |
|
| 294 | + public static function textarea($name, $value = '', array $attributes = array()){ |
|
| 295 | + $str = null; |
|
| 296 | + $str .= '<textarea name = "'.$name.'"'; |
|
| 297 | + $str .= attributes_to_string($attributes); |
|
| 298 | + $str .= '>'; |
|
| 299 | + $str .= $value.'</textarea>'; |
|
| 300 | + return $str; |
|
| 301 | + } |
|
| 302 | 302 | |
| 303 | - /** |
|
| 304 | - * Generate the form field for select |
|
| 305 | - * @param string $name the name of the form field |
|
| 306 | - * @param mixed|array $values the values used to populate the "option" tags |
|
| 307 | - * @param mixed $selected the selected value in the option list |
|
| 308 | - * @param array $attributes the additional attribute to be added |
|
| 309 | - * @return string the generated form field html content for select |
|
| 310 | - */ |
|
| 311 | - public static function select($name, $values = null, $selected = null, array $attributes = array()){ |
|
| 312 | - if(! is_array($values)){ |
|
| 313 | - $values = array('' => $values); |
|
| 314 | - } |
|
| 315 | - $str = null; |
|
| 316 | - $str .= '<select name = "'.$name.'"'; |
|
| 317 | - $str .= attributes_to_string($attributes); |
|
| 318 | - $str .= '>'; |
|
| 319 | - foreach($values as $key => $val){ |
|
| 320 | - $select = ''; |
|
| 321 | - if($key == $selected){ |
|
| 322 | - $select = 'selected'; |
|
| 323 | - } |
|
| 324 | - $str .= '<option value = "'.$key.'" '.$select.'>'.$val.'</option>'; |
|
| 325 | - } |
|
| 326 | - $str .= '</select>'; |
|
| 327 | - return $str; |
|
| 328 | - } |
|
| 303 | + /** |
|
| 304 | + * Generate the form field for select |
|
| 305 | + * @param string $name the name of the form field |
|
| 306 | + * @param mixed|array $values the values used to populate the "option" tags |
|
| 307 | + * @param mixed $selected the selected value in the option list |
|
| 308 | + * @param array $attributes the additional attribute to be added |
|
| 309 | + * @return string the generated form field html content for select |
|
| 310 | + */ |
|
| 311 | + public static function select($name, $values = null, $selected = null, array $attributes = array()){ |
|
| 312 | + if(! is_array($values)){ |
|
| 313 | + $values = array('' => $values); |
|
| 314 | + } |
|
| 315 | + $str = null; |
|
| 316 | + $str .= '<select name = "'.$name.'"'; |
|
| 317 | + $str .= attributes_to_string($attributes); |
|
| 318 | + $str .= '>'; |
|
| 319 | + foreach($values as $key => $val){ |
|
| 320 | + $select = ''; |
|
| 321 | + if($key == $selected){ |
|
| 322 | + $select = 'selected'; |
|
| 323 | + } |
|
| 324 | + $str .= '<option value = "'.$key.'" '.$select.'>'.$val.'</option>'; |
|
| 325 | + } |
|
| 326 | + $str .= '</select>'; |
|
| 327 | + return $str; |
|
| 328 | + } |
|
| 329 | 329 | |
| 330 | - } |
|
| 330 | + } |
|
@@ -918,27 +918,27 @@ discard block |
||
| 918 | 918 | if (isset($aresult[1])) { |
| 919 | 919 | $this->setBrowser(self::BROWSER_IE); |
| 920 | 920 | $this->setVersion(str_replace(array('(', ')', ';'), '', $aresult[1])); |
| 921 | - if(preg_match('#trident/([0-9\.]+);#i', $this->_agent, $aresult)){ |
|
| 922 | - if($aresult[1] == '3.1'){ |
|
| 921 | + if (preg_match('#trident/([0-9\.]+);#i', $this->_agent, $aresult)) { |
|
| 922 | + if ($aresult[1] == '3.1') { |
|
| 923 | 923 | $this->setVersion('7.0'); |
| 924 | 924 | } |
| 925 | - else if($aresult[1] == '4.0'){ |
|
| 925 | + else if ($aresult[1] == '4.0') { |
|
| 926 | 926 | $this->setVersion('8.0'); |
| 927 | 927 | } |
| 928 | - else if($aresult[1] == '5.0'){ |
|
| 928 | + else if ($aresult[1] == '5.0') { |
|
| 929 | 929 | $this->setVersion('9.0'); |
| 930 | 930 | } |
| 931 | - else if($aresult[1] == '6.0'){ |
|
| 931 | + else if ($aresult[1] == '6.0') { |
|
| 932 | 932 | $this->setVersion('10.0'); |
| 933 | 933 | } |
| 934 | - else if($aresult[1] == '7.0'){ |
|
| 934 | + else if ($aresult[1] == '7.0') { |
|
| 935 | 935 | $this->setVersion('11.0'); |
| 936 | 936 | } |
| 937 | - else if($aresult[1] == '8.0'){ |
|
| 937 | + else if ($aresult[1] == '8.0') { |
|
| 938 | 938 | $this->setVersion('11.0'); |
| 939 | 939 | } |
| 940 | 940 | } |
| 941 | - if(stripos($this->_agent, 'IEMobile') !== false) { |
|
| 941 | + if (stripos($this->_agent, 'IEMobile') !== false) { |
|
| 942 | 942 | $this->setBrowser(self::BROWSER_POCKET_IE); |
| 943 | 943 | $this->setMobile(true); |
| 944 | 944 | } |
@@ -1684,8 +1684,8 @@ discard block |
||
| 1684 | 1684 | $this->_platform = self::PLATFORM_ANDROID; |
| 1685 | 1685 | } elseif (stripos($this->_agent, 'Silk') !== false) { |
| 1686 | 1686 | $this->_platform = self::PLATFORM_FIRE_OS; |
| 1687 | - } elseif (stripos($this->_agent, 'linux') !== false && stripos($this->_agent, 'SMART-TV') !== false ) { |
|
| 1688 | - $this->_platform = self::PLATFORM_LINUX .'/'.self::PLATFORM_SMART_TV; |
|
| 1687 | + } elseif (stripos($this->_agent, 'linux') !== false && stripos($this->_agent, 'SMART-TV') !== false) { |
|
| 1688 | + $this->_platform = self::PLATFORM_LINUX . '/' . self::PLATFORM_SMART_TV; |
|
| 1689 | 1689 | } elseif (stripos($this->_agent, 'linux') !== false) { |
| 1690 | 1690 | $this->_platform = self::PLATFORM_LINUX; |
| 1691 | 1691 | } else if (stripos($this->_agent, 'Nokia') !== false) { |
@@ -921,20 +921,15 @@ |
||
| 921 | 921 | if(preg_match('#trident/([0-9\.]+);#i', $this->_agent, $aresult)){ |
| 922 | 922 | if($aresult[1] == '3.1'){ |
| 923 | 923 | $this->setVersion('7.0'); |
| 924 | - } |
|
| 925 | - else if($aresult[1] == '4.0'){ |
|
| 924 | + } else if($aresult[1] == '4.0'){ |
|
| 926 | 925 | $this->setVersion('8.0'); |
| 927 | - } |
|
| 928 | - else if($aresult[1] == '5.0'){ |
|
| 926 | + } else if($aresult[1] == '5.0'){ |
|
| 929 | 927 | $this->setVersion('9.0'); |
| 930 | - } |
|
| 931 | - else if($aresult[1] == '6.0'){ |
|
| 928 | + } else if($aresult[1] == '6.0'){ |
|
| 932 | 929 | $this->setVersion('10.0'); |
| 933 | - } |
|
| 934 | - else if($aresult[1] == '7.0'){ |
|
| 930 | + } else if($aresult[1] == '7.0'){ |
|
| 935 | 931 | $this->setVersion('11.0'); |
| 936 | - } |
|
| 937 | - else if($aresult[1] == '8.0'){ |
|
| 932 | + } else if($aresult[1] == '8.0'){ |
|
| 938 | 933 | $this->setVersion('11.0'); |
| 939 | 934 | } |
| 940 | 935 | } |
@@ -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 | - if(! function_exists('__')){ |
|
| 27 | + if (!function_exists('__')) { |
|
| 28 | 28 | /** |
| 29 | 29 | * function for the shortcut to Lang::get() |
| 30 | 30 | * @param string $key the language key to retrieve |
@@ -32,20 +32,20 @@ discard block |
||
| 32 | 32 | * for the given key |
| 33 | 33 | * @return string the language value |
| 34 | 34 | */ |
| 35 | - function __($key, $default = 'LANGUAGE_ERROR'){ |
|
| 35 | + function __($key, $default = 'LANGUAGE_ERROR') { |
|
| 36 | 36 | return get_instance()->lang->get($key, $default); |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | |
| 42 | - if(! function_exists('get_languages')){ |
|
| 42 | + if (!function_exists('get_languages')) { |
|
| 43 | 43 | /** |
| 44 | 44 | * function for the shortcut to Lang::getSupported() |
| 45 | 45 | * |
| 46 | 46 | * @return array all the supported languages |
| 47 | 47 | */ |
| 48 | - function get_languages(){ |
|
| 48 | + function get_languages() { |
|
| 49 | 49 | return get_instance()->lang->getSupported(); |
| 50 | 50 | } |
| 51 | 51 | |
@@ -1,52 +1,52 @@ |
||
| 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 | - */ |
|
| 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 | 26 | |
| 27 | - if(! function_exists('__')){ |
|
| 28 | - /** |
|
| 29 | - * function for the shortcut to Lang::get() |
|
| 30 | - * @param string $key the language key to retrieve |
|
| 31 | - * @param mixed $default the default value to return if can not find the value |
|
| 32 | - * for the given key |
|
| 33 | - * @return string the language value |
|
| 34 | - */ |
|
| 35 | - function __($key, $default = 'LANGUAGE_ERROR'){ |
|
| 36 | - return get_instance()->lang->get($key, $default); |
|
| 37 | - } |
|
| 27 | + if(! function_exists('__')){ |
|
| 28 | + /** |
|
| 29 | + * function for the shortcut to Lang::get() |
|
| 30 | + * @param string $key the language key to retrieve |
|
| 31 | + * @param mixed $default the default value to return if can not find the value |
|
| 32 | + * for the given key |
|
| 33 | + * @return string the language value |
|
| 34 | + */ |
|
| 35 | + function __($key, $default = 'LANGUAGE_ERROR'){ |
|
| 36 | + return get_instance()->lang->get($key, $default); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - } |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | 41 | |
| 42 | - if(! function_exists('get_languages')){ |
|
| 43 | - /** |
|
| 44 | - * function for the shortcut to Lang::getSupported() |
|
| 45 | - * |
|
| 46 | - * @return array all the supported languages |
|
| 47 | - */ |
|
| 48 | - function get_languages(){ |
|
| 49 | - return get_instance()->lang->getSupported(); |
|
| 50 | - } |
|
| 42 | + if(! function_exists('get_languages')){ |
|
| 43 | + /** |
|
| 44 | + * function for the shortcut to Lang::getSupported() |
|
| 45 | + * |
|
| 46 | + * @return array all the supported languages |
|
| 47 | + */ |
|
| 48 | + function get_languages(){ |
|
| 49 | + return get_instance()->lang->getSupported(); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - } |
|
| 53 | 52 | \ No newline at end of file |
| 53 | + } |
|
| 54 | 54 | \ No newline at end of file |
@@ -55,17 +55,13 @@ |
||
| 55 | 55 | |
| 56 | 56 | if (isset($_SERVER["HTTP_CLIENT_IP"])) { |
| 57 | 57 | $ip = $_SERVER["HTTP_CLIENT_IP"]; |
| 58 | - } |
|
| 59 | - else if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) { |
|
| 58 | + } else if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) { |
|
| 60 | 59 | $ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; |
| 61 | - } |
|
| 62 | - else if (isset($_SERVER["HTTP_X_FORWARDED"])) { |
|
| 60 | + } else if (isset($_SERVER["HTTP_X_FORWARDED"])) { |
|
| 63 | 61 | $ip = $_SERVER["HTTP_X_FORWARDED"]; |
| 64 | - } |
|
| 65 | - else if (isset($_SERVER["HTTP_FORWARDED_FOR"])) { |
|
| 62 | + } else if (isset($_SERVER["HTTP_FORWARDED_FOR"])) { |
|
| 66 | 63 | $ip = $_SERVER["HTTP_FORWARDED_FOR"]; |
| 67 | - } |
|
| 68 | - else if (isset($_SERVER["HTTP_FORWARDED"])) { |
|
| 64 | + } else if (isset($_SERVER["HTTP_FORWARDED"])) { |
|
| 69 | 65 | $ip = $_SERVER["HTTP_FORWARDED"]; |
| 70 | 66 | } |
| 71 | 67 | |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | */ |
| 42 | 42 | |
| 43 | 43 | |
| 44 | - if(! function_exists('get_ip')){ |
|
| 44 | + if (!function_exists('get_ip')) { |
|
| 45 | 45 | /** |
| 46 | 46 | * Retrieves the user's IP address |
| 47 | 47 | * |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | * |
| 51 | 51 | * @return string the IP address. |
| 52 | 52 | */ |
| 53 | - function get_ip(){ |
|
| 53 | + function get_ip() { |
|
| 54 | 54 | $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1'; |
| 55 | 55 | |
| 56 | 56 | if (isset($_SERVER["HTTP_CLIENT_IP"])) { |
@@ -1,78 +1,78 @@ |
||
| 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 | - */ |
|
| 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 | 26 | |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * @file function_user_agent.php |
|
| 30 | - * |
|
| 31 | - * Contains most of the utility functions for agent, platform, mobile, browser, and other management. |
|
| 32 | - * |
|
| 33 | - * @package core |
|
| 34 | - * @author Tony NGUEREZA |
|
| 35 | - * @copyright Copyright (c) 2017 |
|
| 36 | - * @license https://opensource.org/licenses/gpl-3.0.html GNU GPL License (GPL) |
|
| 37 | - * @link http://www.iacademy.cf |
|
| 38 | - * @version 1.0.0 |
|
| 39 | - * @since 1.0.0 |
|
| 40 | - * @filesource |
|
| 41 | - */ |
|
| 28 | + /** |
|
| 29 | + * @file function_user_agent.php |
|
| 30 | + * |
|
| 31 | + * Contains most of the utility functions for agent, platform, mobile, browser, and other management. |
|
| 32 | + * |
|
| 33 | + * @package core |
|
| 34 | + * @author Tony NGUEREZA |
|
| 35 | + * @copyright Copyright (c) 2017 |
|
| 36 | + * @license https://opensource.org/licenses/gpl-3.0.html GNU GPL License (GPL) |
|
| 37 | + * @link http://www.iacademy.cf |
|
| 38 | + * @version 1.0.0 |
|
| 39 | + * @since 1.0.0 |
|
| 40 | + * @filesource |
|
| 41 | + */ |
|
| 42 | 42 | |
| 43 | 43 | |
| 44 | - if(! function_exists('get_ip')){ |
|
| 45 | - /** |
|
| 46 | - * Retrieves the user's IP address |
|
| 47 | - * |
|
| 48 | - * This function allows to retrieve the IP address of the client |
|
| 49 | - * even if it uses a proxy, the actual IP address is retrieved. |
|
| 50 | - * |
|
| 51 | - * @return string the IP address. |
|
| 52 | - */ |
|
| 53 | - function get_ip(){ |
|
| 54 | - $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1'; |
|
| 44 | + if(! function_exists('get_ip')){ |
|
| 45 | + /** |
|
| 46 | + * Retrieves the user's IP address |
|
| 47 | + * |
|
| 48 | + * This function allows to retrieve the IP address of the client |
|
| 49 | + * even if it uses a proxy, the actual IP address is retrieved. |
|
| 50 | + * |
|
| 51 | + * @return string the IP address. |
|
| 52 | + */ |
|
| 53 | + function get_ip(){ |
|
| 54 | + $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1'; |
|
| 55 | 55 | |
| 56 | - if (isset($_SERVER["HTTP_CLIENT_IP"])) { |
|
| 57 | - $ip = $_SERVER["HTTP_CLIENT_IP"]; |
|
| 58 | - } |
|
| 59 | - else if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) { |
|
| 60 | - $ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; |
|
| 61 | - } |
|
| 62 | - else if (isset($_SERVER["HTTP_X_FORWARDED"])) { |
|
| 63 | - $ip = $_SERVER["HTTP_X_FORWARDED"]; |
|
| 64 | - } |
|
| 65 | - else if (isset($_SERVER["HTTP_FORWARDED_FOR"])) { |
|
| 66 | - $ip = $_SERVER["HTTP_FORWARDED_FOR"]; |
|
| 67 | - } |
|
| 68 | - else if (isset($_SERVER["HTTP_FORWARDED"])) { |
|
| 69 | - $ip = $_SERVER["HTTP_FORWARDED"]; |
|
| 70 | - } |
|
| 56 | + if (isset($_SERVER["HTTP_CLIENT_IP"])) { |
|
| 57 | + $ip = $_SERVER["HTTP_CLIENT_IP"]; |
|
| 58 | + } |
|
| 59 | + else if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) { |
|
| 60 | + $ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; |
|
| 61 | + } |
|
| 62 | + else if (isset($_SERVER["HTTP_X_FORWARDED"])) { |
|
| 63 | + $ip = $_SERVER["HTTP_X_FORWARDED"]; |
|
| 64 | + } |
|
| 65 | + else if (isset($_SERVER["HTTP_FORWARDED_FOR"])) { |
|
| 66 | + $ip = $_SERVER["HTTP_FORWARDED_FOR"]; |
|
| 67 | + } |
|
| 68 | + else if (isset($_SERVER["HTTP_FORWARDED"])) { |
|
| 69 | + $ip = $_SERVER["HTTP_FORWARDED"]; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - // Strip any secondary IP etc from the IP address |
|
| 73 | - if (strpos($ip, ',') > 0) { |
|
| 74 | - $ip = substr($ip, 0, strpos($ip, ',')); |
|
| 75 | - } |
|
| 76 | - return $ip; |
|
| 77 | - } |
|
| 78 | - } |
|
| 72 | + // Strip any secondary IP etc from the IP address |
|
| 73 | + if (strpos($ip, ',') > 0) { |
|
| 74 | + $ip = substr($ip, 0, strpos($ip, ',')); |
|
| 75 | + } |
|
| 76 | + return $ip; |
|
| 77 | + } |
|
| 78 | + } |
|