@@ -1,255 +1,255 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - class Request { |
|
| 27 | + class Request { |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The value for the super global $_GET |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - public $get = null; |
|
| 29 | + /** |
|
| 30 | + * The value for the super global $_GET |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + public $get = null; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * The value for the super global $_POST |
|
| 37 | - * @var array |
|
| 38 | - */ |
|
| 39 | - public $post = null; |
|
| 35 | + /** |
|
| 36 | + * The value for the super global $_POST |
|
| 37 | + * @var array |
|
| 38 | + */ |
|
| 39 | + public $post = null; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * The value for the super global $_SERVER |
|
| 43 | - * @var array |
|
| 44 | - */ |
|
| 45 | - public $server = null; |
|
| 41 | + /** |
|
| 42 | + * The value for the super global $_SERVER |
|
| 43 | + * @var array |
|
| 44 | + */ |
|
| 45 | + public $server = null; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * The value for the super global $_COOKIE |
|
| 49 | - * @var array |
|
| 50 | - */ |
|
| 51 | - public $cookie = null; |
|
| 47 | + /** |
|
| 48 | + * The value for the super global $_COOKIE |
|
| 49 | + * @var array |
|
| 50 | + */ |
|
| 51 | + public $cookie = null; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * The value for the super global $_FILES |
|
| 55 | - * @var array |
|
| 56 | - */ |
|
| 57 | - public $file = null; |
|
| 53 | + /** |
|
| 54 | + * The value for the super global $_FILES |
|
| 55 | + * @var array |
|
| 56 | + */ |
|
| 57 | + public $file = null; |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * The value for the super global $_REQUEST |
|
| 61 | - * @var array |
|
| 62 | - */ |
|
| 63 | - public $query = null; |
|
| 59 | + /** |
|
| 60 | + * The value for the super global $_REQUEST |
|
| 61 | + * @var array |
|
| 62 | + */ |
|
| 63 | + public $query = null; |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * The session instance |
|
| 67 | - * @var Session |
|
| 68 | - */ |
|
| 69 | - public $session = null; |
|
| 65 | + /** |
|
| 66 | + * The session instance |
|
| 67 | + * @var Session |
|
| 68 | + */ |
|
| 69 | + public $session = null; |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * The request headers |
|
| 73 | - * @var array |
|
| 74 | - */ |
|
| 75 | - public $header = null; |
|
| 71 | + /** |
|
| 72 | + * The request headers |
|
| 73 | + * @var array |
|
| 74 | + */ |
|
| 75 | + public $header = null; |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * The current request method 'GET', 'POST', 'PUT', etc. |
|
| 79 | - * @var null |
|
| 80 | - */ |
|
| 81 | - private $method = null; |
|
| 77 | + /** |
|
| 78 | + * The current request method 'GET', 'POST', 'PUT', etc. |
|
| 79 | + * @var null |
|
| 80 | + */ |
|
| 81 | + private $method = null; |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * The current request URI |
|
| 85 | - * @var string |
|
| 86 | - */ |
|
| 87 | - private $requestUri = null; |
|
| 83 | + /** |
|
| 84 | + * The current request URI |
|
| 85 | + * @var string |
|
| 86 | + */ |
|
| 87 | + private $requestUri = null; |
|
| 88 | 88 | |
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Construct new request instance |
|
| 92 | - */ |
|
| 93 | - public function __construct(){ |
|
| 94 | - $this->get = $_GET; |
|
| 95 | - $this->post = $_POST; |
|
| 96 | - $this->server = $_SERVER; |
|
| 97 | - $this->query = $_REQUEST; |
|
| 98 | - $this->cookie = $_COOKIE; |
|
| 99 | - $this->file = $_FILES; |
|
| 100 | - $this->session =& class_loader('Session', 'classes'); |
|
| 101 | - $this->method = $this->server('REQUEST_METHOD'); |
|
| 102 | - $this->requestUri = $this->server('REQUEST_URI'); |
|
| 103 | - $this->header = array(); |
|
| 104 | - if(function_exists('apache_request_headers')){ |
|
| 105 | - $this->header = apache_request_headers(); |
|
| 106 | - } else if(function_exists('getallheaders')){ |
|
| 107 | - $this->header = getallheaders(); |
|
| 108 | - } |
|
| 109 | - } |
|
| 90 | + /** |
|
| 91 | + * Construct new request instance |
|
| 92 | + */ |
|
| 93 | + public function __construct(){ |
|
| 94 | + $this->get = $_GET; |
|
| 95 | + $this->post = $_POST; |
|
| 96 | + $this->server = $_SERVER; |
|
| 97 | + $this->query = $_REQUEST; |
|
| 98 | + $this->cookie = $_COOKIE; |
|
| 99 | + $this->file = $_FILES; |
|
| 100 | + $this->session =& class_loader('Session', 'classes'); |
|
| 101 | + $this->method = $this->server('REQUEST_METHOD'); |
|
| 102 | + $this->requestUri = $this->server('REQUEST_URI'); |
|
| 103 | + $this->header = array(); |
|
| 104 | + if(function_exists('apache_request_headers')){ |
|
| 105 | + $this->header = apache_request_headers(); |
|
| 106 | + } else if(function_exists('getallheaders')){ |
|
| 107 | + $this->header = getallheaders(); |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * Get the request method |
|
| 113 | - * @return string |
|
| 114 | - */ |
|
| 115 | - public function method() { |
|
| 116 | - return $this->method; |
|
| 117 | - } |
|
| 111 | + /** |
|
| 112 | + * Get the request method |
|
| 113 | + * @return string |
|
| 114 | + */ |
|
| 115 | + public function method() { |
|
| 116 | + return $this->method; |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - /** |
|
| 120 | - * Get the request URI |
|
| 121 | - * @return string |
|
| 122 | - */ |
|
| 123 | - public function requestUri() { |
|
| 124 | - return $this->requestUri; |
|
| 125 | - } |
|
| 119 | + /** |
|
| 120 | + * Get the request URI |
|
| 121 | + * @return string |
|
| 122 | + */ |
|
| 123 | + public function requestUri() { |
|
| 124 | + return $this->requestUri; |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - /** |
|
| 128 | - * Get the value from $_REQUEST for given key. if the key is empty will return the all values |
|
| 129 | - * @param string $key the item key to be fetched |
|
| 130 | - * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 131 | - * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 132 | - */ |
|
| 133 | - public function query($key = null, $xss = true) { |
|
| 134 | - if (empty($key)) { |
|
| 135 | - //return all |
|
| 136 | - return $xss ? clean_input($this->query) : $this->query; |
|
| 137 | - } |
|
| 138 | - $query = array_key_exists($key, $this->query) ? $this->query[$key] : null; |
|
| 139 | - if ($xss) { |
|
| 140 | - $query = clean_input($query); |
|
| 141 | - } |
|
| 142 | - return $query; |
|
| 143 | - } |
|
| 127 | + /** |
|
| 128 | + * Get the value from $_REQUEST for given key. if the key is empty will return the all values |
|
| 129 | + * @param string $key the item key to be fetched |
|
| 130 | + * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 131 | + * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 132 | + */ |
|
| 133 | + public function query($key = null, $xss = true) { |
|
| 134 | + if (empty($key)) { |
|
| 135 | + //return all |
|
| 136 | + return $xss ? clean_input($this->query) : $this->query; |
|
| 137 | + } |
|
| 138 | + $query = array_key_exists($key, $this->query) ? $this->query[$key] : null; |
|
| 139 | + if ($xss) { |
|
| 140 | + $query = clean_input($query); |
|
| 141 | + } |
|
| 142 | + return $query; |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - /** |
|
| 146 | - * Get the value from $_GET for given key. if the key is empty will return the all values |
|
| 147 | - * @param string $key the item key to be fetched |
|
| 148 | - * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 149 | - * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 150 | - */ |
|
| 151 | - public function get($key = null, $xss = true) { |
|
| 152 | - if (empty($key)) { |
|
| 153 | - //return all |
|
| 154 | - return $xss ? clean_input($this->get) : $this->get; |
|
| 155 | - } |
|
| 156 | - $get = array_key_exists($key, $this->get) ? $this->get[$key] : null; |
|
| 157 | - if ($xss) { |
|
| 158 | - $get = clean_input($get); |
|
| 159 | - } |
|
| 160 | - return $get; |
|
| 161 | - } |
|
| 145 | + /** |
|
| 146 | + * Get the value from $_GET for given key. if the key is empty will return the all values |
|
| 147 | + * @param string $key the item key to be fetched |
|
| 148 | + * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 149 | + * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 150 | + */ |
|
| 151 | + public function get($key = null, $xss = true) { |
|
| 152 | + if (empty($key)) { |
|
| 153 | + //return all |
|
| 154 | + return $xss ? clean_input($this->get) : $this->get; |
|
| 155 | + } |
|
| 156 | + $get = array_key_exists($key, $this->get) ? $this->get[$key] : null; |
|
| 157 | + if ($xss) { |
|
| 158 | + $get = clean_input($get); |
|
| 159 | + } |
|
| 160 | + return $get; |
|
| 161 | + } |
|
| 162 | 162 | |
| 163 | - /** |
|
| 164 | - * Get the value from $_POST for given key. if the key is empty will return the all values |
|
| 165 | - * @param string $key the item key to be fetched |
|
| 166 | - * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 167 | - * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 168 | - */ |
|
| 169 | - public function post($key = null, $xss = true) { |
|
| 170 | - if (empty($key)) { |
|
| 171 | - //return all |
|
| 172 | - return $xss ? clean_input($this->post) : $this->post; |
|
| 173 | - } |
|
| 174 | - $post = array_key_exists($key, $this->post) ? $this->post[$key] : null; |
|
| 175 | - if ($xss) { |
|
| 176 | - $post = clean_input($post); |
|
| 177 | - } |
|
| 178 | - return $post; |
|
| 179 | - } |
|
| 163 | + /** |
|
| 164 | + * Get the value from $_POST for given key. if the key is empty will return the all values |
|
| 165 | + * @param string $key the item key to be fetched |
|
| 166 | + * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 167 | + * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 168 | + */ |
|
| 169 | + public function post($key = null, $xss = true) { |
|
| 170 | + if (empty($key)) { |
|
| 171 | + //return all |
|
| 172 | + return $xss ? clean_input($this->post) : $this->post; |
|
| 173 | + } |
|
| 174 | + $post = array_key_exists($key, $this->post) ? $this->post[$key] : null; |
|
| 175 | + if ($xss) { |
|
| 176 | + $post = clean_input($post); |
|
| 177 | + } |
|
| 178 | + return $post; |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | - /** |
|
| 182 | - * Get the value from $_SERVER for given key. if the key is empty will return the all values |
|
| 183 | - * @param string $key the item key to be fetched |
|
| 184 | - * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 185 | - * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 186 | - */ |
|
| 187 | - public function server($key = null, $xss = true) { |
|
| 188 | - if (empty($key)) { |
|
| 189 | - //return all |
|
| 190 | - return $xss ? clean_input($this->server) : $this->server; |
|
| 191 | - } |
|
| 192 | - $server = array_key_exists($key, $this->server) ? $this->server[$key] : null; |
|
| 193 | - if ($xss) { |
|
| 194 | - $server = clean_input($server); |
|
| 195 | - } |
|
| 196 | - return $server; |
|
| 197 | - } |
|
| 181 | + /** |
|
| 182 | + * Get the value from $_SERVER for given key. if the key is empty will return the all values |
|
| 183 | + * @param string $key the item key to be fetched |
|
| 184 | + * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 185 | + * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 186 | + */ |
|
| 187 | + public function server($key = null, $xss = true) { |
|
| 188 | + if (empty($key)) { |
|
| 189 | + //return all |
|
| 190 | + return $xss ? clean_input($this->server) : $this->server; |
|
| 191 | + } |
|
| 192 | + $server = array_key_exists($key, $this->server) ? $this->server[$key] : null; |
|
| 193 | + if ($xss) { |
|
| 194 | + $server = clean_input($server); |
|
| 195 | + } |
|
| 196 | + return $server; |
|
| 197 | + } |
|
| 198 | 198 | |
| 199 | - /** |
|
| 200 | - * Get the value from $_COOKIE for given key. if the key is empty will return the all values |
|
| 201 | - * @param string $key the item key to be fetched |
|
| 202 | - * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 203 | - * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 204 | - */ |
|
| 205 | - public function cookie($key = null, $xss = true) { |
|
| 206 | - if (empty($key)) { |
|
| 207 | - //return all |
|
| 208 | - return $xss ? clean_input($this->cookie) : $this->cookie; |
|
| 209 | - } |
|
| 210 | - $cookie = array_key_exists($key, $this->cookie) ? $this->cookie[$key] : null; |
|
| 211 | - if ($xss) { |
|
| 212 | - $cookie = clean_input($cookie); |
|
| 213 | - } |
|
| 214 | - return $cookie; |
|
| 215 | - } |
|
| 199 | + /** |
|
| 200 | + * Get the value from $_COOKIE for given key. if the key is empty will return the all values |
|
| 201 | + * @param string $key the item key to be fetched |
|
| 202 | + * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 203 | + * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 204 | + */ |
|
| 205 | + public function cookie($key = null, $xss = true) { |
|
| 206 | + if (empty($key)) { |
|
| 207 | + //return all |
|
| 208 | + return $xss ? clean_input($this->cookie) : $this->cookie; |
|
| 209 | + } |
|
| 210 | + $cookie = array_key_exists($key, $this->cookie) ? $this->cookie[$key] : null; |
|
| 211 | + if ($xss) { |
|
| 212 | + $cookie = clean_input($cookie); |
|
| 213 | + } |
|
| 214 | + return $cookie; |
|
| 215 | + } |
|
| 216 | 216 | |
| 217 | - /** |
|
| 218 | - * Get the value from $_FILES for given key. if the key is empty will return the all values |
|
| 219 | - * @param string $key the item key to be fetched |
|
| 220 | - * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 221 | - */ |
|
| 222 | - public function file($key) { |
|
| 223 | - $file = array_key_exists($key, $this->file) ? $this->file[$key] : null; |
|
| 224 | - return $file; |
|
| 225 | - } |
|
| 217 | + /** |
|
| 218 | + * Get the value from $_FILES for given key. if the key is empty will return the all values |
|
| 219 | + * @param string $key the item key to be fetched |
|
| 220 | + * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 221 | + */ |
|
| 222 | + public function file($key) { |
|
| 223 | + $file = array_key_exists($key, $this->file) ? $this->file[$key] : null; |
|
| 224 | + return $file; |
|
| 225 | + } |
|
| 226 | 226 | |
| 227 | - /** |
|
| 228 | - * Get the value from $_SESSION for given key. if the key is empty will return the all values |
|
| 229 | - * @param string $key the item key to be fetched |
|
| 230 | - * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 231 | - * @return array|mixed the item value if the key exists or null if the key does not exists |
|
| 232 | - */ |
|
| 233 | - public function session($key, $xss = true) { |
|
| 234 | - $session = $this->session->get($key); |
|
| 235 | - if ($xss) { |
|
| 236 | - $session = clean_input($session); |
|
| 237 | - } |
|
| 238 | - return $session; |
|
| 239 | - } |
|
| 227 | + /** |
|
| 228 | + * Get the value from $_SESSION for given key. if the key is empty will return the all values |
|
| 229 | + * @param string $key the item key to be fetched |
|
| 230 | + * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 231 | + * @return array|mixed the item value if the key exists or null if the key does not exists |
|
| 232 | + */ |
|
| 233 | + public function session($key, $xss = true) { |
|
| 234 | + $session = $this->session->get($key); |
|
| 235 | + if ($xss) { |
|
| 236 | + $session = clean_input($session); |
|
| 237 | + } |
|
| 238 | + return $session; |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | - /** |
|
| 242 | - * Get the value from header array for given key. |
|
| 243 | - * @param string $key the item key to be fetched |
|
| 244 | - * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 245 | - * @return mixed the item value if the key exists or null if the key does not exists |
|
| 246 | - */ |
|
| 247 | - public function header($key, $xss = true) { |
|
| 248 | - $header = array_key_exists($key, $this->header) ? $this->header[$key] : null; |
|
| 249 | - if ($xss) { |
|
| 250 | - $header = clean_input($header); |
|
| 251 | - } |
|
| 252 | - return $header; |
|
| 253 | - } |
|
| 241 | + /** |
|
| 242 | + * Get the value from header array for given key. |
|
| 243 | + * @param string $key the item key to be fetched |
|
| 244 | + * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 245 | + * @return mixed the item value if the key exists or null if the key does not exists |
|
| 246 | + */ |
|
| 247 | + public function header($key, $xss = true) { |
|
| 248 | + $header = array_key_exists($key, $this->header) ? $this->header[$key] : null; |
|
| 249 | + if ($xss) { |
|
| 250 | + $header = clean_input($header); |
|
| 251 | + } |
|
| 252 | + return $header; |
|
| 253 | + } |
|
| 254 | 254 | |
| 255 | - } |
|
| 255 | + } |
|
@@ -1,84 +1,84 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - interface CacheInterface { |
|
| 27 | + interface CacheInterface { |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * This is used to get the cache data using the key |
|
| 31 | - * @param string $key the key to identify the cache data |
|
| 32 | - * @return mixed the cache data if exists else return false |
|
| 33 | - */ |
|
| 34 | - public function get($key); |
|
| 29 | + /** |
|
| 30 | + * This is used to get the cache data using the key |
|
| 31 | + * @param string $key the key to identify the cache data |
|
| 32 | + * @return mixed the cache data if exists else return false |
|
| 33 | + */ |
|
| 34 | + public function get($key); |
|
| 35 | 35 | |
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Save data to the cache |
|
| 39 | - * @param string $key the key to identify this cache data |
|
| 40 | - * @param mixed $data the cache data to be saved |
|
| 41 | - * @param integer $ttl the cache life time |
|
| 42 | - * @return boolean true if success otherwise will return false |
|
| 43 | - */ |
|
| 44 | - public function set($key, $data, $ttl = 0); |
|
| 37 | + /** |
|
| 38 | + * Save data to the cache |
|
| 39 | + * @param string $key the key to identify this cache data |
|
| 40 | + * @param mixed $data the cache data to be saved |
|
| 41 | + * @param integer $ttl the cache life time |
|
| 42 | + * @return boolean true if success otherwise will return false |
|
| 43 | + */ |
|
| 44 | + public function set($key, $data, $ttl = 0); |
|
| 45 | 45 | |
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Delete the cache data for given key |
|
| 49 | - * @param string $key the key for cache to be deleted |
|
| 50 | - * @return boolean true if the cache is deleted, false if can't delete |
|
| 51 | - * the cache or the cache with the given key not exist |
|
| 52 | - */ |
|
| 53 | - public function delete($key); |
|
| 47 | + /** |
|
| 48 | + * Delete the cache data for given key |
|
| 49 | + * @param string $key the key for cache to be deleted |
|
| 50 | + * @return boolean true if the cache is deleted, false if can't delete |
|
| 51 | + * the cache or the cache with the given key not exist |
|
| 52 | + */ |
|
| 53 | + public function delete($key); |
|
| 54 | 54 | |
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Get the cache information for given key |
|
| 58 | - * @param string $key the key for cache to get the information for |
|
| 59 | - * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 60 | - * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 61 | - * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 62 | - * 'ttl' => the time to live of the cache in second |
|
| 63 | - */ |
|
| 64 | - public function getInfo($key); |
|
| 56 | + /** |
|
| 57 | + * Get the cache information for given key |
|
| 58 | + * @param string $key the key for cache to get the information for |
|
| 59 | + * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 60 | + * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 61 | + * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 62 | + * 'ttl' => the time to live of the cache in second |
|
| 63 | + */ |
|
| 64 | + public function getInfo($key); |
|
| 65 | 65 | |
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Used to delete expired cache data |
|
| 69 | - */ |
|
| 70 | - public function deleteExpiredCache(); |
|
| 67 | + /** |
|
| 68 | + * Used to delete expired cache data |
|
| 69 | + */ |
|
| 70 | + public function deleteExpiredCache(); |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Remove all cache data |
|
| 74 | - */ |
|
| 75 | - public function clean(); |
|
| 72 | + /** |
|
| 73 | + * Remove all cache data |
|
| 74 | + */ |
|
| 75 | + public function clean(); |
|
| 76 | 76 | |
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Check whether the cache feature for the handle is supported |
|
| 80 | - * |
|
| 81 | - * @return bool |
|
| 82 | - */ |
|
| 83 | - public function isSupported(); |
|
| 84 | - } |
|
| 78 | + /** |
|
| 79 | + * Check whether the cache feature for the handle is supported |
|
| 80 | + * |
|
| 81 | + * @return bool |
|
| 82 | + */ |
|
| 83 | + public function isSupported(); |
|
| 84 | + } |
|
@@ -1,167 +1,167 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * This class represent the event dispatcher management, permit to record the listener and |
|
| 29 | - * also to dispatch the event |
|
| 30 | - */ |
|
| 27 | + /** |
|
| 28 | + * This class represent the event dispatcher management, permit to record the listener and |
|
| 29 | + * also to dispatch the event |
|
| 30 | + */ |
|
| 31 | 31 | |
| 32 | - class EventDispatcher extends BaseClass { |
|
| 32 | + class EventDispatcher extends BaseClass { |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * The list of the registered listeners |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - private $listeners = array(); |
|
| 34 | + /** |
|
| 35 | + * The list of the registered listeners |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + private $listeners = array(); |
|
| 39 | 39 | |
| 40 | 40 | |
| 41 | - public function __construct() { |
|
| 42 | - parent::__construct(); |
|
| 43 | - } |
|
| 41 | + public function __construct() { |
|
| 42 | + parent::__construct(); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Register new listener |
|
| 47 | - * @param string $eventName the name of the event to register for |
|
| 48 | - * @param callable $listener the function or class method to receive the event information after dispatch |
|
| 49 | - */ |
|
| 50 | - public function addListener($eventName, callable $listener){ |
|
| 51 | - $this->logger->debug('Adding new event listener for the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
| 52 | - if(! isset($this->listeners[$eventName])){ |
|
| 53 | - $this->logger->info('This event does not have the registered event listener before, adding new one'); |
|
| 54 | - $this->listeners[$eventName] = array(); |
|
| 55 | - } else{ |
|
| 56 | - $this->logger->info('This event already have the registered listener, add this listener to the list'); |
|
| 57 | - } |
|
| 58 | - $this->listeners[$eventName][] = $listener; |
|
| 59 | - } |
|
| 45 | + /** |
|
| 46 | + * Register new listener |
|
| 47 | + * @param string $eventName the name of the event to register for |
|
| 48 | + * @param callable $listener the function or class method to receive the event information after dispatch |
|
| 49 | + */ |
|
| 50 | + public function addListener($eventName, callable $listener){ |
|
| 51 | + $this->logger->debug('Adding new event listener for the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
| 52 | + if(! isset($this->listeners[$eventName])){ |
|
| 53 | + $this->logger->info('This event does not have the registered event listener before, adding new one'); |
|
| 54 | + $this->listeners[$eventName] = array(); |
|
| 55 | + } else{ |
|
| 56 | + $this->logger->info('This event already have the registered listener, add this listener to the list'); |
|
| 57 | + } |
|
| 58 | + $this->listeners[$eventName][] = $listener; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Remove the event listener from list |
|
| 63 | - * @param string $eventName the event name |
|
| 64 | - * @param callable $listener the listener callback |
|
| 65 | - */ |
|
| 66 | - public function removeListener($eventName, callable $listener){ |
|
| 67 | - $this->logger->debug('Removing of the event listener, the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
| 68 | - if(isset($this->listeners[$eventName])){ |
|
| 69 | - $this->logger->info('This event have the listeners, check if this listener exists'); |
|
| 70 | - if(false !== $index = array_search($listener, $this->listeners[$eventName], true)){ |
|
| 71 | - $this->logger->info('Found the listener at index [' .$index. '] remove it'); |
|
| 72 | - unset($this->listeners[$eventName][$index]); |
|
| 73 | - } else{ |
|
| 74 | - $this->logger->info('Cannot found this listener in the event listener list'); |
|
| 75 | - } |
|
| 76 | - } else{ |
|
| 77 | - $this->logger->info('This event does not have this listener ignore remove'); |
|
| 78 | - } |
|
| 79 | - } |
|
| 61 | + /** |
|
| 62 | + * Remove the event listener from list |
|
| 63 | + * @param string $eventName the event name |
|
| 64 | + * @param callable $listener the listener callback |
|
| 65 | + */ |
|
| 66 | + public function removeListener($eventName, callable $listener){ |
|
| 67 | + $this->logger->debug('Removing of the event listener, the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
| 68 | + if(isset($this->listeners[$eventName])){ |
|
| 69 | + $this->logger->info('This event have the listeners, check if this listener exists'); |
|
| 70 | + if(false !== $index = array_search($listener, $this->listeners[$eventName], true)){ |
|
| 71 | + $this->logger->info('Found the listener at index [' .$index. '] remove it'); |
|
| 72 | + unset($this->listeners[$eventName][$index]); |
|
| 73 | + } else{ |
|
| 74 | + $this->logger->info('Cannot found this listener in the event listener list'); |
|
| 75 | + } |
|
| 76 | + } else{ |
|
| 77 | + $this->logger->info('This event does not have this listener ignore remove'); |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * Remove all the event listener. If event name is null will remove all listeners, else will just |
|
| 83 | - * remove all listeners for this event |
|
| 84 | - * @param string $eventName the event name |
|
| 85 | - */ |
|
| 86 | - public function removeAllListener($eventName = null){ |
|
| 87 | - $this->logger->debug('Removing of all event listener, the event name [' .$eventName. ']'); |
|
| 88 | - if($eventName !== null && isset($this->listeners[$eventName])){ |
|
| 89 | - $this->logger->info('The event name is set of exist in the listener just remove all event listener for this event'); |
|
| 90 | - unset($this->listeners[$eventName]); |
|
| 91 | - } else{ |
|
| 92 | - $this->logger->info('The event name is not set or does not exist in the listener, so remove all event listener'); |
|
| 93 | - $this->listeners = array(); |
|
| 94 | - } |
|
| 95 | - } |
|
| 81 | + /** |
|
| 82 | + * Remove all the event listener. If event name is null will remove all listeners, else will just |
|
| 83 | + * remove all listeners for this event |
|
| 84 | + * @param string $eventName the event name |
|
| 85 | + */ |
|
| 86 | + public function removeAllListener($eventName = null){ |
|
| 87 | + $this->logger->debug('Removing of all event listener, the event name [' .$eventName. ']'); |
|
| 88 | + if($eventName !== null && isset($this->listeners[$eventName])){ |
|
| 89 | + $this->logger->info('The event name is set of exist in the listener just remove all event listener for this event'); |
|
| 90 | + unset($this->listeners[$eventName]); |
|
| 91 | + } else{ |
|
| 92 | + $this->logger->info('The event name is not set or does not exist in the listener, so remove all event listener'); |
|
| 93 | + $this->listeners = array(); |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * Get the list of listener for this event |
|
| 99 | - * @param string $eventName the event name |
|
| 100 | - * @return array the listeners for this event or empty array if this event does not contain any listener |
|
| 101 | - */ |
|
| 102 | - public function getListeners($eventName) { |
|
| 103 | - return isset($this->listeners[$eventName]) ? $this->listeners[$eventName] : array(); |
|
| 104 | - } |
|
| 97 | + /** |
|
| 98 | + * Get the list of listener for this event |
|
| 99 | + * @param string $eventName the event name |
|
| 100 | + * @return array the listeners for this event or empty array if this event does not contain any listener |
|
| 101 | + */ |
|
| 102 | + public function getListeners($eventName) { |
|
| 103 | + return isset($this->listeners[$eventName]) ? $this->listeners[$eventName] : array(); |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * Dispatch the event to the registered listeners. |
|
| 108 | - * @param mixed|object $event the event information |
|
| 109 | - * @return void|object if event need return, will return the final EventInfo object. |
|
| 110 | - */ |
|
| 111 | - public function dispatch($event){ |
|
| 112 | - if(! $event || !$event instanceof EventInfo){ |
|
| 113 | - $this->logger->info('The event is not set or is not an instance of "EventInfo" create the default "EventInfo" object to use instead of.'); |
|
| 114 | - $event = new EventInfo((string) $event); |
|
| 115 | - } |
|
| 116 | - $this->logger->debug('Dispatch to the event listener, the event [' .stringfy_vars($event). ']'); |
|
| 117 | - if(isset($event->stop) && $event->stop){ |
|
| 118 | - $this->logger->info('This event need stopped, no need call any listener'); |
|
| 119 | - return; |
|
| 120 | - } |
|
| 121 | - if($event->returnBack){ |
|
| 122 | - $this->logger->info('This event need return back, return the result for future use'); |
|
| 123 | - return $this->dispatchToListerners($event); |
|
| 124 | - } else{ |
|
| 125 | - $this->logger->info('This event no need return back the result, just dispatch it'); |
|
| 126 | - $this->dispatchToListerners($event); |
|
| 127 | - } |
|
| 128 | - } |
|
| 106 | + /** |
|
| 107 | + * Dispatch the event to the registered listeners. |
|
| 108 | + * @param mixed|object $event the event information |
|
| 109 | + * @return void|object if event need return, will return the final EventInfo object. |
|
| 110 | + */ |
|
| 111 | + public function dispatch($event){ |
|
| 112 | + if(! $event || !$event instanceof EventInfo){ |
|
| 113 | + $this->logger->info('The event is not set or is not an instance of "EventInfo" create the default "EventInfo" object to use instead of.'); |
|
| 114 | + $event = new EventInfo((string) $event); |
|
| 115 | + } |
|
| 116 | + $this->logger->debug('Dispatch to the event listener, the event [' .stringfy_vars($event). ']'); |
|
| 117 | + if(isset($event->stop) && $event->stop){ |
|
| 118 | + $this->logger->info('This event need stopped, no need call any listener'); |
|
| 119 | + return; |
|
| 120 | + } |
|
| 121 | + if($event->returnBack){ |
|
| 122 | + $this->logger->info('This event need return back, return the result for future use'); |
|
| 123 | + return $this->dispatchToListerners($event); |
|
| 124 | + } else{ |
|
| 125 | + $this->logger->info('This event no need return back the result, just dispatch it'); |
|
| 126 | + $this->dispatchToListerners($event); |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - /** |
|
| 131 | - * Dispatch the event to the registered listeners. |
|
| 132 | - * @param object EventInfo $event the event information |
|
| 133 | - * @return void|object if event need return, will return the final EventInfo instance. |
|
| 134 | - */ |
|
| 135 | - private function dispatchToListerners(EventInfo $event){ |
|
| 136 | - $eBackup = $event; |
|
| 137 | - $list = $this->getListeners($event->name); |
|
| 138 | - if(empty($list)){ |
|
| 139 | - $this->logger->info('No event listener is registered for the event [' .$event->name. '] skipping.'); |
|
| 140 | - if($event->returnBack){ |
|
| 141 | - return $event; |
|
| 142 | - } |
|
| 143 | - return; |
|
| 144 | - } else{ |
|
| 145 | - $this->logger->info('Found the registered event listener for the event [' .$event->name. '] the list are: ' . stringfy_vars($list)); |
|
| 146 | - } |
|
| 147 | - foreach($list as $listener){ |
|
| 148 | - if($eBackup->returnBack){ |
|
| 149 | - $returnedEvent = call_user_func_array($listener, array($event)); |
|
| 150 | - if($returnedEvent instanceof EventInfo){ |
|
| 151 | - $event = $returnedEvent; |
|
| 152 | - } else{ |
|
| 153 | - show_error('This event [' .$event->name. '] need you return the event object after processing'); |
|
| 154 | - } |
|
| 155 | - } else{ |
|
| 156 | - call_user_func_array($listener, array($event)); |
|
| 157 | - } |
|
| 158 | - if($event->stop){ |
|
| 159 | - break; |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - //only test for original event may be during the flow some listeners change this parameter |
|
| 163 | - if($eBackup->returnBack){ |
|
| 164 | - return $event; |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - } |
|
| 130 | + /** |
|
| 131 | + * Dispatch the event to the registered listeners. |
|
| 132 | + * @param object EventInfo $event the event information |
|
| 133 | + * @return void|object if event need return, will return the final EventInfo instance. |
|
| 134 | + */ |
|
| 135 | + private function dispatchToListerners(EventInfo $event){ |
|
| 136 | + $eBackup = $event; |
|
| 137 | + $list = $this->getListeners($event->name); |
|
| 138 | + if(empty($list)){ |
|
| 139 | + $this->logger->info('No event listener is registered for the event [' .$event->name. '] skipping.'); |
|
| 140 | + if($event->returnBack){ |
|
| 141 | + return $event; |
|
| 142 | + } |
|
| 143 | + return; |
|
| 144 | + } else{ |
|
| 145 | + $this->logger->info('Found the registered event listener for the event [' .$event->name. '] the list are: ' . stringfy_vars($list)); |
|
| 146 | + } |
|
| 147 | + foreach($list as $listener){ |
|
| 148 | + if($eBackup->returnBack){ |
|
| 149 | + $returnedEvent = call_user_func_array($listener, array($event)); |
|
| 150 | + if($returnedEvent instanceof EventInfo){ |
|
| 151 | + $event = $returnedEvent; |
|
| 152 | + } else{ |
|
| 153 | + show_error('This event [' .$event->name. '] need you return the event object after processing'); |
|
| 154 | + } |
|
| 155 | + } else{ |
|
| 156 | + call_user_func_array($listener, array($event)); |
|
| 157 | + } |
|
| 158 | + if($event->stop){ |
|
| 159 | + break; |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + //only test for original event may be during the flow some listeners change this parameter |
|
| 163 | + if($eBackup->returnBack){ |
|
| 164 | + return $event; |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + } |
|
@@ -1,612 +1,612 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 26 | - class Loader extends BaseStaticClass { |
|
| 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 | + class Loader extends BaseStaticClass { |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * List of loaded resources |
|
| 30 | - * @var array |
|
| 31 | - */ |
|
| 32 | - public static $loaded = array(); |
|
| 28 | + /** |
|
| 29 | + * List of loaded resources |
|
| 30 | + * @var array |
|
| 31 | + */ |
|
| 32 | + public static $loaded = array(); |
|
| 33 | 33 | |
| 34 | 34 | |
| 35 | - public function __construct() { |
|
| 36 | - //add the resources already loaded during application bootstrap |
|
| 37 | - //in the list to prevent duplicate or loading the resources again. |
|
| 38 | - static::$loaded = class_loaded(); |
|
| 35 | + public function __construct() { |
|
| 36 | + //add the resources already loaded during application bootstrap |
|
| 37 | + //in the list to prevent duplicate or loading the resources again. |
|
| 38 | + static::$loaded = class_loaded(); |
|
| 39 | 39 | |
| 40 | - //Load resources from autoload configuration |
|
| 41 | - $this->loadResourcesFromAutoloadConfig(); |
|
| 42 | - } |
|
| 40 | + //Load resources from autoload configuration |
|
| 41 | + $this->loadResourcesFromAutoloadConfig(); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Load the model class |
|
| 47 | - * |
|
| 48 | - * @param string $class the class name to be loaded |
|
| 49 | - * @param string $instance the name of the instance to use in super object |
|
| 50 | - * |
|
| 51 | - * @return void |
|
| 52 | - */ |
|
| 53 | - public static function model($class, $instance = null) { |
|
| 54 | - $logger = static::getLogger(); |
|
| 55 | - $class = str_ireplace('.php', '', $class); |
|
| 56 | - $class = trim($class, '/\\'); |
|
| 57 | - $file = ucfirst($class) . '.php'; |
|
| 58 | - $logger->debug('Loading model [' . $class . '] ...'); |
|
| 59 | - //************ |
|
| 60 | - if (!$instance) { |
|
| 61 | - $instance = self::getModelLibraryInstanceName($class); |
|
| 62 | - } |
|
| 63 | - //**************** |
|
| 64 | - if (isset(static::$loaded[$instance])) { |
|
| 65 | - $logger->info('Model [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
| 66 | - return; |
|
| 67 | - } |
|
| 68 | - $classFilePath = APPS_MODEL_PATH . $file; |
|
| 69 | - //first check if this model is in the module |
|
| 70 | - $logger->debug('Checking model [' . $class . '] from module list ...'); |
|
| 71 | - //check if the request class contains module name |
|
| 72 | - $moduleInfo = self::getModuleInfoForModelLibrary($class); |
|
| 73 | - $module = $moduleInfo['module']; |
|
| 74 | - $class = $moduleInfo['class']; |
|
| 45 | + /** |
|
| 46 | + * Load the model class |
|
| 47 | + * |
|
| 48 | + * @param string $class the class name to be loaded |
|
| 49 | + * @param string $instance the name of the instance to use in super object |
|
| 50 | + * |
|
| 51 | + * @return void |
|
| 52 | + */ |
|
| 53 | + public static function model($class, $instance = null) { |
|
| 54 | + $logger = static::getLogger(); |
|
| 55 | + $class = str_ireplace('.php', '', $class); |
|
| 56 | + $class = trim($class, '/\\'); |
|
| 57 | + $file = ucfirst($class) . '.php'; |
|
| 58 | + $logger->debug('Loading model [' . $class . '] ...'); |
|
| 59 | + //************ |
|
| 60 | + if (!$instance) { |
|
| 61 | + $instance = self::getModelLibraryInstanceName($class); |
|
| 62 | + } |
|
| 63 | + //**************** |
|
| 64 | + if (isset(static::$loaded[$instance])) { |
|
| 65 | + $logger->info('Model [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
| 66 | + return; |
|
| 67 | + } |
|
| 68 | + $classFilePath = APPS_MODEL_PATH . $file; |
|
| 69 | + //first check if this model is in the module |
|
| 70 | + $logger->debug('Checking model [' . $class . '] from module list ...'); |
|
| 71 | + //check if the request class contains module name |
|
| 72 | + $moduleInfo = self::getModuleInfoForModelLibrary($class); |
|
| 73 | + $module = $moduleInfo['module']; |
|
| 74 | + $class = $moduleInfo['class']; |
|
| 75 | 75 | |
| 76 | - $moduleModelFilePath = Module::findModelFullPath($class, $module); |
|
| 77 | - if ($moduleModelFilePath){ |
|
| 78 | - $logger->info('Found model [' . $class . '] from module [' .$module. '], the file path is [' .$moduleModelFilePath. '] we will used it'); |
|
| 79 | - $classFilePath = $moduleModelFilePath; |
|
| 80 | - } else{ |
|
| 81 | - $logger->info('Cannot find model [' . $class . '] from modules using the default location'); |
|
| 82 | - } |
|
| 83 | - $logger->info('The model file path to be loaded is [' . $classFilePath . ']'); |
|
| 84 | - if (file_exists($classFilePath)){ |
|
| 85 | - require_once $classFilePath; |
|
| 86 | - if (class_exists($class)){ |
|
| 87 | - $c = new $class(); |
|
| 88 | - $obj = & get_instance(); |
|
| 89 | - $obj->{$instance} = $c; |
|
| 90 | - static::$loaded[$instance] = $class; |
|
| 91 | - $logger->info('Model [' . $class . '] --> ' . $classFilePath . ' loaded successfully.'); |
|
| 92 | - } else{ |
|
| 93 | - show_error('The file '.$classFilePath.' exists but does not contain the class ['. $class . ']'); |
|
| 94 | - } |
|
| 95 | - } else{ |
|
| 96 | - show_error('Unable to find the model [' . $class . ']'); |
|
| 97 | - } |
|
| 98 | - } |
|
| 76 | + $moduleModelFilePath = Module::findModelFullPath($class, $module); |
|
| 77 | + if ($moduleModelFilePath){ |
|
| 78 | + $logger->info('Found model [' . $class . '] from module [' .$module. '], the file path is [' .$moduleModelFilePath. '] we will used it'); |
|
| 79 | + $classFilePath = $moduleModelFilePath; |
|
| 80 | + } else{ |
|
| 81 | + $logger->info('Cannot find model [' . $class . '] from modules using the default location'); |
|
| 82 | + } |
|
| 83 | + $logger->info('The model file path to be loaded is [' . $classFilePath . ']'); |
|
| 84 | + if (file_exists($classFilePath)){ |
|
| 85 | + require_once $classFilePath; |
|
| 86 | + if (class_exists($class)){ |
|
| 87 | + $c = new $class(); |
|
| 88 | + $obj = & get_instance(); |
|
| 89 | + $obj->{$instance} = $c; |
|
| 90 | + static::$loaded[$instance] = $class; |
|
| 91 | + $logger->info('Model [' . $class . '] --> ' . $classFilePath . ' loaded successfully.'); |
|
| 92 | + } else{ |
|
| 93 | + show_error('The file '.$classFilePath.' exists but does not contain the class ['. $class . ']'); |
|
| 94 | + } |
|
| 95 | + } else{ |
|
| 96 | + show_error('Unable to find the model [' . $class . ']'); |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * Load the library class |
|
| 103 | - * |
|
| 104 | - * @param string $class the library class name to be loaded |
|
| 105 | - * @param string $instance the instance name to use in super object |
|
| 106 | - * @param mixed $params the arguments to pass to the constructor |
|
| 107 | - * |
|
| 108 | - * @return void |
|
| 109 | - */ |
|
| 110 | - public static function library($class, $instance = null, array $params = array()) { |
|
| 111 | - $logger = static::getLogger(); |
|
| 112 | - $class = str_ireplace('.php', '', $class); |
|
| 113 | - $class = trim($class, '/\\'); |
|
| 114 | - $file = ucfirst($class) . '.php'; |
|
| 115 | - $logger->debug('Loading library [' . $class . '] ...'); |
|
| 116 | - if (!$instance) { |
|
| 117 | - $instance = self::getModelLibraryInstanceName($class); |
|
| 118 | - } |
|
| 119 | - if (isset(static::$loaded[$instance])) { |
|
| 120 | - $logger->info('Library [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
| 121 | - return; |
|
| 122 | - } |
|
| 123 | - $obj = & get_instance(); |
|
| 124 | - //Check and load Database library |
|
| 125 | - if (strtolower($class) == 'database') { |
|
| 126 | - $logger->info('This is the Database library ...'); |
|
| 127 | - $obj->{$instance} = & class_loader('Database', 'classes/database', $params); |
|
| 128 | - static::$loaded[$instance] = $class; |
|
| 129 | - $logger->info('Library Database loaded successfully.'); |
|
| 130 | - return; |
|
| 131 | - } |
|
| 132 | - $libraryFilePath = null; |
|
| 133 | - $logger->debug('Check if this is a system library ...'); |
|
| 134 | - if (file_exists(CORE_LIBRARY_PATH . $file)){ |
|
| 135 | - $libraryFilePath = CORE_LIBRARY_PATH . $file; |
|
| 136 | - $class = ucfirst($class); |
|
| 137 | - $logger->info('This library is a system library'); |
|
| 138 | - } else{ |
|
| 139 | - $logger->info('This library is not a system library'); |
|
| 140 | - //first check if this library is in the module |
|
| 141 | - $libraryFilePath = self::getLibraryPathUsingModuleInfo($class); |
|
| 142 | - //*************** |
|
| 143 | - } |
|
| 144 | - if (! $libraryFilePath && file_exists(LIBRARY_PATH . $file)){ |
|
| 145 | - $libraryFilePath = LIBRARY_PATH . $file; |
|
| 146 | - } |
|
| 147 | - $logger->info('The library file path to be loaded is [' . $libraryFilePath . ']'); |
|
| 148 | - //************************* |
|
| 149 | - self::loadLibrary($libraryFilePath, $class, $instance, $params); |
|
| 150 | - } |
|
| 101 | + /** |
|
| 102 | + * Load the library class |
|
| 103 | + * |
|
| 104 | + * @param string $class the library class name to be loaded |
|
| 105 | + * @param string $instance the instance name to use in super object |
|
| 106 | + * @param mixed $params the arguments to pass to the constructor |
|
| 107 | + * |
|
| 108 | + * @return void |
|
| 109 | + */ |
|
| 110 | + public static function library($class, $instance = null, array $params = array()) { |
|
| 111 | + $logger = static::getLogger(); |
|
| 112 | + $class = str_ireplace('.php', '', $class); |
|
| 113 | + $class = trim($class, '/\\'); |
|
| 114 | + $file = ucfirst($class) . '.php'; |
|
| 115 | + $logger->debug('Loading library [' . $class . '] ...'); |
|
| 116 | + if (!$instance) { |
|
| 117 | + $instance = self::getModelLibraryInstanceName($class); |
|
| 118 | + } |
|
| 119 | + if (isset(static::$loaded[$instance])) { |
|
| 120 | + $logger->info('Library [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
| 121 | + return; |
|
| 122 | + } |
|
| 123 | + $obj = & get_instance(); |
|
| 124 | + //Check and load Database library |
|
| 125 | + if (strtolower($class) == 'database') { |
|
| 126 | + $logger->info('This is the Database library ...'); |
|
| 127 | + $obj->{$instance} = & class_loader('Database', 'classes/database', $params); |
|
| 128 | + static::$loaded[$instance] = $class; |
|
| 129 | + $logger->info('Library Database loaded successfully.'); |
|
| 130 | + return; |
|
| 131 | + } |
|
| 132 | + $libraryFilePath = null; |
|
| 133 | + $logger->debug('Check if this is a system library ...'); |
|
| 134 | + if (file_exists(CORE_LIBRARY_PATH . $file)){ |
|
| 135 | + $libraryFilePath = CORE_LIBRARY_PATH . $file; |
|
| 136 | + $class = ucfirst($class); |
|
| 137 | + $logger->info('This library is a system library'); |
|
| 138 | + } else{ |
|
| 139 | + $logger->info('This library is not a system library'); |
|
| 140 | + //first check if this library is in the module |
|
| 141 | + $libraryFilePath = self::getLibraryPathUsingModuleInfo($class); |
|
| 142 | + //*************** |
|
| 143 | + } |
|
| 144 | + if (! $libraryFilePath && file_exists(LIBRARY_PATH . $file)){ |
|
| 145 | + $libraryFilePath = LIBRARY_PATH . $file; |
|
| 146 | + } |
|
| 147 | + $logger->info('The library file path to be loaded is [' . $libraryFilePath . ']'); |
|
| 148 | + //************************* |
|
| 149 | + self::loadLibrary($libraryFilePath, $class, $instance, $params); |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - /** |
|
| 153 | - * Load the helper |
|
| 154 | - * |
|
| 155 | - * @param string $function the helper name to be loaded |
|
| 156 | - * |
|
| 157 | - * @return void |
|
| 158 | - */ |
|
| 159 | - public static function functions($function) { |
|
| 160 | - $logger = static::getLogger(); |
|
| 161 | - $function = str_ireplace('.php', '', $function); |
|
| 162 | - $function = trim($function, '/\\'); |
|
| 163 | - $function = str_ireplace('function_', '', $function); |
|
| 164 | - $file = 'function_' . $function . '.php'; |
|
| 165 | - $logger->debug('Loading helper [' . $function . '] ...'); |
|
| 166 | - if (isset(static::$loaded['function_' . $function])) { |
|
| 167 | - $logger->info('Helper [' . $function . '] already loaded no need to load it again, cost in performance'); |
|
| 168 | - return; |
|
| 169 | - } |
|
| 170 | - $functionFilePath = null; |
|
| 171 | - //first check if this helper is in the module |
|
| 172 | - $logger->debug('Checking helper [' . $function . '] from module list ...'); |
|
| 173 | - $moduleInfo = self::getModuleInfoForFunction($function); |
|
| 174 | - $module = $moduleInfo['module']; |
|
| 175 | - $function = $moduleInfo['function']; |
|
| 176 | - if(! empty($moduleInfo['file'])){ |
|
| 177 | - $file = $moduleInfo['file']; |
|
| 178 | - } |
|
| 179 | - $moduleFunctionPath = Module::findFunctionFullPath($function, $module); |
|
| 180 | - if ($moduleFunctionPath){ |
|
| 181 | - $logger->info('Found helper [' . $function . '] from module [' .$module. '], the file path is [' .$moduleFunctionPath. '] we will used it'); |
|
| 182 | - $functionFilePath = $moduleFunctionPath; |
|
| 183 | - } else{ |
|
| 184 | - $logger->info('Cannot find helper [' . $function . '] from modules using the default location'); |
|
| 185 | - } |
|
| 186 | - if (! $functionFilePath){ |
|
| 187 | - $searchDir = array(FUNCTIONS_PATH, CORE_FUNCTIONS_PATH); |
|
| 188 | - foreach($searchDir as $dir){ |
|
| 189 | - $filePath = $dir . $file; |
|
| 190 | - if (file_exists($filePath)){ |
|
| 191 | - $functionFilePath = $filePath; |
|
| 192 | - //is already found not to continue |
|
| 193 | - break; |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - $logger->info('The helper file path to be loaded is [' . $functionFilePath . ']'); |
|
| 198 | - if ($functionFilePath){ |
|
| 199 | - require_once $functionFilePath; |
|
| 200 | - static::$loaded['function_' . $function] = $functionFilePath; |
|
| 201 | - $logger->info('Helper [' . $function . '] --> ' . $functionFilePath . ' loaded successfully.'); |
|
| 202 | - } else{ |
|
| 203 | - show_error('Unable to find helper file [' . $file . ']'); |
|
| 204 | - } |
|
| 205 | - } |
|
| 152 | + /** |
|
| 153 | + * Load the helper |
|
| 154 | + * |
|
| 155 | + * @param string $function the helper name to be loaded |
|
| 156 | + * |
|
| 157 | + * @return void |
|
| 158 | + */ |
|
| 159 | + public static function functions($function) { |
|
| 160 | + $logger = static::getLogger(); |
|
| 161 | + $function = str_ireplace('.php', '', $function); |
|
| 162 | + $function = trim($function, '/\\'); |
|
| 163 | + $function = str_ireplace('function_', '', $function); |
|
| 164 | + $file = 'function_' . $function . '.php'; |
|
| 165 | + $logger->debug('Loading helper [' . $function . '] ...'); |
|
| 166 | + if (isset(static::$loaded['function_' . $function])) { |
|
| 167 | + $logger->info('Helper [' . $function . '] already loaded no need to load it again, cost in performance'); |
|
| 168 | + return; |
|
| 169 | + } |
|
| 170 | + $functionFilePath = null; |
|
| 171 | + //first check if this helper is in the module |
|
| 172 | + $logger->debug('Checking helper [' . $function . '] from module list ...'); |
|
| 173 | + $moduleInfo = self::getModuleInfoForFunction($function); |
|
| 174 | + $module = $moduleInfo['module']; |
|
| 175 | + $function = $moduleInfo['function']; |
|
| 176 | + if(! empty($moduleInfo['file'])){ |
|
| 177 | + $file = $moduleInfo['file']; |
|
| 178 | + } |
|
| 179 | + $moduleFunctionPath = Module::findFunctionFullPath($function, $module); |
|
| 180 | + if ($moduleFunctionPath){ |
|
| 181 | + $logger->info('Found helper [' . $function . '] from module [' .$module. '], the file path is [' .$moduleFunctionPath. '] we will used it'); |
|
| 182 | + $functionFilePath = $moduleFunctionPath; |
|
| 183 | + } else{ |
|
| 184 | + $logger->info('Cannot find helper [' . $function . '] from modules using the default location'); |
|
| 185 | + } |
|
| 186 | + if (! $functionFilePath){ |
|
| 187 | + $searchDir = array(FUNCTIONS_PATH, CORE_FUNCTIONS_PATH); |
|
| 188 | + foreach($searchDir as $dir){ |
|
| 189 | + $filePath = $dir . $file; |
|
| 190 | + if (file_exists($filePath)){ |
|
| 191 | + $functionFilePath = $filePath; |
|
| 192 | + //is already found not to continue |
|
| 193 | + break; |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + $logger->info('The helper file path to be loaded is [' . $functionFilePath . ']'); |
|
| 198 | + if ($functionFilePath){ |
|
| 199 | + require_once $functionFilePath; |
|
| 200 | + static::$loaded['function_' . $function] = $functionFilePath; |
|
| 201 | + $logger->info('Helper [' . $function . '] --> ' . $functionFilePath . ' loaded successfully.'); |
|
| 202 | + } else{ |
|
| 203 | + show_error('Unable to find helper file [' . $file . ']'); |
|
| 204 | + } |
|
| 205 | + } |
|
| 206 | 206 | |
| 207 | - /** |
|
| 208 | - * Load the configuration file |
|
| 209 | - * |
|
| 210 | - * @param string $filename the configuration filename located at CONFIG_PATH or MODULE_PATH/config |
|
| 211 | - * |
|
| 212 | - * @return void |
|
| 213 | - */ |
|
| 214 | - public static function config($filename) { |
|
| 215 | - $logger = static::getLogger(); |
|
| 216 | - $filename = str_ireplace('.php', '', $filename); |
|
| 217 | - $filename = trim($filename, '/\\'); |
|
| 218 | - $filename = str_ireplace('config_', '', $filename); |
|
| 219 | - $file = 'config_' . $filename . '.php'; |
|
| 220 | - $logger->debug('Loading configuration [' . $filename . '] ...'); |
|
| 221 | - if (isset(static::$loaded['config_' . $filename])) { |
|
| 222 | - $logger->info('Configuration [' . $file . '] already loaded no need to load it again, cost in performance'); |
|
| 223 | - return; |
|
| 224 | - } |
|
| 225 | - $configFilePath = CONFIG_PATH . $file; |
|
| 226 | - //first check if this config is in the module |
|
| 227 | - $logger->debug('Checking config [' . $filename . '] from module list ...'); |
|
| 228 | - $moduleInfo = self::getModuleInfoForConfig($filename); |
|
| 229 | - $module = $moduleInfo['module']; |
|
| 230 | - $filename = $moduleInfo['filename']; |
|
| 231 | - $moduleConfigPath = Module::findConfigFullPath($filename, $module); |
|
| 232 | - if ($moduleConfigPath){ |
|
| 233 | - $logger->info('Found config [' . $filename . '] from module [' .$module. '], the file path is [' .$moduleConfigPath. '] we will used it'); |
|
| 234 | - $configFilePath = $moduleConfigPath; |
|
| 235 | - } else{ |
|
| 236 | - $logger->info('Cannot find config [' . $filename . '] from modules using the default location'); |
|
| 237 | - } |
|
| 238 | - $logger->info('The config file path to be loaded is [' . $configFilePath . ']'); |
|
| 239 | - $config = array(); |
|
| 240 | - if (file_exists($configFilePath)){ |
|
| 241 | - require_once $configFilePath; |
|
| 242 | - if (! empty($config) && is_array($config)){ |
|
| 243 | - Config::setAll($config); |
|
| 244 | - static::$loaded['config_' . $filename] = $configFilePath; |
|
| 245 | - $logger->info('Configuration [' . $configFilePath . '] loaded successfully.'); |
|
| 246 | - $logger->info('The custom application configuration loaded are listed below: ' . stringfy_vars($config)); |
|
| 247 | - unset($config); |
|
| 248 | - } |
|
| 249 | - } else{ |
|
| 250 | - show_error('Unable to find config file ['. $configFilePath . ']'); |
|
| 251 | - } |
|
| 252 | - } |
|
| 207 | + /** |
|
| 208 | + * Load the configuration file |
|
| 209 | + * |
|
| 210 | + * @param string $filename the configuration filename located at CONFIG_PATH or MODULE_PATH/config |
|
| 211 | + * |
|
| 212 | + * @return void |
|
| 213 | + */ |
|
| 214 | + public static function config($filename) { |
|
| 215 | + $logger = static::getLogger(); |
|
| 216 | + $filename = str_ireplace('.php', '', $filename); |
|
| 217 | + $filename = trim($filename, '/\\'); |
|
| 218 | + $filename = str_ireplace('config_', '', $filename); |
|
| 219 | + $file = 'config_' . $filename . '.php'; |
|
| 220 | + $logger->debug('Loading configuration [' . $filename . '] ...'); |
|
| 221 | + if (isset(static::$loaded['config_' . $filename])) { |
|
| 222 | + $logger->info('Configuration [' . $file . '] already loaded no need to load it again, cost in performance'); |
|
| 223 | + return; |
|
| 224 | + } |
|
| 225 | + $configFilePath = CONFIG_PATH . $file; |
|
| 226 | + //first check if this config is in the module |
|
| 227 | + $logger->debug('Checking config [' . $filename . '] from module list ...'); |
|
| 228 | + $moduleInfo = self::getModuleInfoForConfig($filename); |
|
| 229 | + $module = $moduleInfo['module']; |
|
| 230 | + $filename = $moduleInfo['filename']; |
|
| 231 | + $moduleConfigPath = Module::findConfigFullPath($filename, $module); |
|
| 232 | + if ($moduleConfigPath){ |
|
| 233 | + $logger->info('Found config [' . $filename . '] from module [' .$module. '], the file path is [' .$moduleConfigPath. '] we will used it'); |
|
| 234 | + $configFilePath = $moduleConfigPath; |
|
| 235 | + } else{ |
|
| 236 | + $logger->info('Cannot find config [' . $filename . '] from modules using the default location'); |
|
| 237 | + } |
|
| 238 | + $logger->info('The config file path to be loaded is [' . $configFilePath . ']'); |
|
| 239 | + $config = array(); |
|
| 240 | + if (file_exists($configFilePath)){ |
|
| 241 | + require_once $configFilePath; |
|
| 242 | + if (! empty($config) && is_array($config)){ |
|
| 243 | + Config::setAll($config); |
|
| 244 | + static::$loaded['config_' . $filename] = $configFilePath; |
|
| 245 | + $logger->info('Configuration [' . $configFilePath . '] loaded successfully.'); |
|
| 246 | + $logger->info('The custom application configuration loaded are listed below: ' . stringfy_vars($config)); |
|
| 247 | + unset($config); |
|
| 248 | + } |
|
| 249 | + } else{ |
|
| 250 | + show_error('Unable to find config file ['. $configFilePath . ']'); |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | 253 | |
| 254 | 254 | |
| 255 | - /** |
|
| 256 | - * Load the language |
|
| 257 | - * |
|
| 258 | - * @param string $language the language name to be loaded |
|
| 259 | - * |
|
| 260 | - * @return void |
|
| 261 | - */ |
|
| 262 | - public static function lang($language) { |
|
| 263 | - $logger = static::getLogger(); |
|
| 264 | - $language = str_ireplace('.php', '', $language); |
|
| 265 | - $language = trim($language, '/\\'); |
|
| 266 | - $language = str_ireplace('lang_', '', $language); |
|
| 267 | - $file = 'lang_' . $language . '.php'; |
|
| 268 | - $logger->debug('Loading language [' . $language . '] ...'); |
|
| 269 | - if (isset(static::$loaded['lang_' . $language])) { |
|
| 270 | - $logger->info('Language [' . $language . '] already loaded no need to load it again, cost in performance'); |
|
| 271 | - return; |
|
| 272 | - } |
|
| 273 | - //get the current language |
|
| 274 | - $appLang = self::getAppLang(); |
|
| 275 | - $languageFilePath = null; |
|
| 276 | - //first check if this language is in the module |
|
| 277 | - $logger->debug('Checking language [' . $language . '] from module list ...'); |
|
| 278 | - $moduleInfo = self::getModuleInfoForLanguage($language); |
|
| 279 | - $module = $moduleInfo['module']; |
|
| 280 | - $language = $moduleInfo['language']; |
|
| 281 | - if(! empty($moduleInfo['file'])){ |
|
| 282 | - $file = $moduleInfo['file']; |
|
| 283 | - } |
|
| 284 | - $moduleLanguagePath = Module::findLanguageFullPath($language, $appLang, $module); |
|
| 285 | - if ($moduleLanguagePath){ |
|
| 286 | - $logger->info('Found language [' . $language . '] from module [' .$module. '], the file path is [' .$moduleLanguagePath. '] we will used it'); |
|
| 287 | - $languageFilePath = $moduleLanguagePath; |
|
| 288 | - } else{ |
|
| 289 | - $logger->info('Cannot find language [' . $language . '] from modules using the default location'); |
|
| 290 | - } |
|
| 291 | - if (! $languageFilePath){ |
|
| 292 | - $searchDir = array(APP_LANG_PATH, CORE_LANG_PATH); |
|
| 293 | - foreach($searchDir as $dir){ |
|
| 294 | - $filePath = $dir . $appLang . DS . $file; |
|
| 295 | - if (file_exists($filePath)){ |
|
| 296 | - $languageFilePath = $filePath; |
|
| 297 | - //already found no need continue |
|
| 298 | - break; |
|
| 299 | - } |
|
| 300 | - } |
|
| 301 | - } |
|
| 302 | - $logger->info('The language file path to be loaded is [' . $languageFilePath . ']'); |
|
| 303 | - self::loadLanguage($languageFilePath, $language); |
|
| 304 | - } |
|
| 255 | + /** |
|
| 256 | + * Load the language |
|
| 257 | + * |
|
| 258 | + * @param string $language the language name to be loaded |
|
| 259 | + * |
|
| 260 | + * @return void |
|
| 261 | + */ |
|
| 262 | + public static function lang($language) { |
|
| 263 | + $logger = static::getLogger(); |
|
| 264 | + $language = str_ireplace('.php', '', $language); |
|
| 265 | + $language = trim($language, '/\\'); |
|
| 266 | + $language = str_ireplace('lang_', '', $language); |
|
| 267 | + $file = 'lang_' . $language . '.php'; |
|
| 268 | + $logger->debug('Loading language [' . $language . '] ...'); |
|
| 269 | + if (isset(static::$loaded['lang_' . $language])) { |
|
| 270 | + $logger->info('Language [' . $language . '] already loaded no need to load it again, cost in performance'); |
|
| 271 | + return; |
|
| 272 | + } |
|
| 273 | + //get the current language |
|
| 274 | + $appLang = self::getAppLang(); |
|
| 275 | + $languageFilePath = null; |
|
| 276 | + //first check if this language is in the module |
|
| 277 | + $logger->debug('Checking language [' . $language . '] from module list ...'); |
|
| 278 | + $moduleInfo = self::getModuleInfoForLanguage($language); |
|
| 279 | + $module = $moduleInfo['module']; |
|
| 280 | + $language = $moduleInfo['language']; |
|
| 281 | + if(! empty($moduleInfo['file'])){ |
|
| 282 | + $file = $moduleInfo['file']; |
|
| 283 | + } |
|
| 284 | + $moduleLanguagePath = Module::findLanguageFullPath($language, $appLang, $module); |
|
| 285 | + if ($moduleLanguagePath){ |
|
| 286 | + $logger->info('Found language [' . $language . '] from module [' .$module. '], the file path is [' .$moduleLanguagePath. '] we will used it'); |
|
| 287 | + $languageFilePath = $moduleLanguagePath; |
|
| 288 | + } else{ |
|
| 289 | + $logger->info('Cannot find language [' . $language . '] from modules using the default location'); |
|
| 290 | + } |
|
| 291 | + if (! $languageFilePath){ |
|
| 292 | + $searchDir = array(APP_LANG_PATH, CORE_LANG_PATH); |
|
| 293 | + foreach($searchDir as $dir){ |
|
| 294 | + $filePath = $dir . $appLang . DS . $file; |
|
| 295 | + if (file_exists($filePath)){ |
|
| 296 | + $languageFilePath = $filePath; |
|
| 297 | + //already found no need continue |
|
| 298 | + break; |
|
| 299 | + } |
|
| 300 | + } |
|
| 301 | + } |
|
| 302 | + $logger->info('The language file path to be loaded is [' . $languageFilePath . ']'); |
|
| 303 | + self::loadLanguage($languageFilePath, $language); |
|
| 304 | + } |
|
| 305 | 305 | |
| 306 | - /** |
|
| 307 | - * Return the current app language by default will use the value from cookie |
|
| 308 | - * if can not found will use the default value from configuration |
|
| 309 | - * @return string the app language like "en", "fr" |
|
| 310 | - */ |
|
| 311 | - protected static function getAppLang() { |
|
| 312 | - //determine the current language |
|
| 313 | - $appLang = get_config('default_language'); |
|
| 314 | - //if the language exists in the cookie use it |
|
| 315 | - $cfgKey = get_config('language_cookie_name'); |
|
| 316 | - $objCookie = & class_loader('Cookie'); |
|
| 317 | - $cookieLang = $objCookie->get($cfgKey); |
|
| 318 | - if ($cookieLang) { |
|
| 319 | - $appLang = $cookieLang; |
|
| 320 | - } |
|
| 321 | - return $appLang; |
|
| 322 | - } |
|
| 323 | - /** |
|
| 324 | - * Get the module information for the model and library to load |
|
| 325 | - * @param string $class the full class name like moduleName/className, className, |
|
| 326 | - * @return array the module information |
|
| 327 | - * array( |
|
| 328 | - * 'module'=> 'module_name' |
|
| 329 | - * 'class' => 'class_name' |
|
| 330 | - * ) |
|
| 331 | - */ |
|
| 332 | - protected static function getModuleInfoForModelLibrary($class){ |
|
| 333 | - $module = null; |
|
| 334 | - $obj = & get_instance(); |
|
| 335 | - if (strpos($class, '/') !== false){ |
|
| 336 | - $path = explode('/', $class); |
|
| 337 | - if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 338 | - $module = $path[0]; |
|
| 339 | - $class = ucfirst($path[1]); |
|
| 340 | - } |
|
| 341 | - } else{ |
|
| 342 | - $class = ucfirst($class); |
|
| 343 | - } |
|
| 344 | - if (! $module && !empty($obj->moduleName)){ |
|
| 345 | - $module = $obj->moduleName; |
|
| 346 | - } |
|
| 347 | - return array( |
|
| 348 | - 'class' => $class, |
|
| 349 | - 'module' => $module |
|
| 350 | - ); |
|
| 351 | - } |
|
| 306 | + /** |
|
| 307 | + * Return the current app language by default will use the value from cookie |
|
| 308 | + * if can not found will use the default value from configuration |
|
| 309 | + * @return string the app language like "en", "fr" |
|
| 310 | + */ |
|
| 311 | + protected static function getAppLang() { |
|
| 312 | + //determine the current language |
|
| 313 | + $appLang = get_config('default_language'); |
|
| 314 | + //if the language exists in the cookie use it |
|
| 315 | + $cfgKey = get_config('language_cookie_name'); |
|
| 316 | + $objCookie = & class_loader('Cookie'); |
|
| 317 | + $cookieLang = $objCookie->get($cfgKey); |
|
| 318 | + if ($cookieLang) { |
|
| 319 | + $appLang = $cookieLang; |
|
| 320 | + } |
|
| 321 | + return $appLang; |
|
| 322 | + } |
|
| 323 | + /** |
|
| 324 | + * Get the module information for the model and library to load |
|
| 325 | + * @param string $class the full class name like moduleName/className, className, |
|
| 326 | + * @return array the module information |
|
| 327 | + * array( |
|
| 328 | + * 'module'=> 'module_name' |
|
| 329 | + * 'class' => 'class_name' |
|
| 330 | + * ) |
|
| 331 | + */ |
|
| 332 | + protected static function getModuleInfoForModelLibrary($class){ |
|
| 333 | + $module = null; |
|
| 334 | + $obj = & get_instance(); |
|
| 335 | + if (strpos($class, '/') !== false){ |
|
| 336 | + $path = explode('/', $class); |
|
| 337 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 338 | + $module = $path[0]; |
|
| 339 | + $class = ucfirst($path[1]); |
|
| 340 | + } |
|
| 341 | + } else{ |
|
| 342 | + $class = ucfirst($class); |
|
| 343 | + } |
|
| 344 | + if (! $module && !empty($obj->moduleName)){ |
|
| 345 | + $module = $obj->moduleName; |
|
| 346 | + } |
|
| 347 | + return array( |
|
| 348 | + 'class' => $class, |
|
| 349 | + 'module' => $module |
|
| 350 | + ); |
|
| 351 | + } |
|
| 352 | 352 | |
| 353 | - /** |
|
| 354 | - * Get the module information for the function to load |
|
| 355 | - * @param string $function the function name like moduleName/functionName, functionName, |
|
| 356 | - * @return array the module information |
|
| 357 | - * array( |
|
| 358 | - * 'module'=> 'module_name' |
|
| 359 | - * 'function' => 'function' |
|
| 360 | - * 'file' => 'file' |
|
| 361 | - * ) |
|
| 362 | - */ |
|
| 363 | - protected static function getModuleInfoForFunction($function) { |
|
| 364 | - $module = null; |
|
| 365 | - $file = null; |
|
| 366 | - $obj = & get_instance(); |
|
| 367 | - //check if the request class contains module name |
|
| 368 | - if (strpos($function, '/') !== false) { |
|
| 369 | - $path = explode('/', $function); |
|
| 370 | - if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
| 371 | - $module = $path[0]; |
|
| 372 | - $function = 'function_' . $path[1]; |
|
| 373 | - $file = $path[0] . DS . $function . '.php'; |
|
| 374 | - } |
|
| 375 | - } |
|
| 376 | - if (!$module && !empty($obj->moduleName)) { |
|
| 377 | - $module = $obj->moduleName; |
|
| 378 | - } |
|
| 379 | - return array( |
|
| 380 | - 'function' => $function, |
|
| 381 | - 'module' => $module, |
|
| 382 | - 'file' => $file |
|
| 383 | - ); |
|
| 384 | - } |
|
| 353 | + /** |
|
| 354 | + * Get the module information for the function to load |
|
| 355 | + * @param string $function the function name like moduleName/functionName, functionName, |
|
| 356 | + * @return array the module information |
|
| 357 | + * array( |
|
| 358 | + * 'module'=> 'module_name' |
|
| 359 | + * 'function' => 'function' |
|
| 360 | + * 'file' => 'file' |
|
| 361 | + * ) |
|
| 362 | + */ |
|
| 363 | + protected static function getModuleInfoForFunction($function) { |
|
| 364 | + $module = null; |
|
| 365 | + $file = null; |
|
| 366 | + $obj = & get_instance(); |
|
| 367 | + //check if the request class contains module name |
|
| 368 | + if (strpos($function, '/') !== false) { |
|
| 369 | + $path = explode('/', $function); |
|
| 370 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
| 371 | + $module = $path[0]; |
|
| 372 | + $function = 'function_' . $path[1]; |
|
| 373 | + $file = $path[0] . DS . $function . '.php'; |
|
| 374 | + } |
|
| 375 | + } |
|
| 376 | + if (!$module && !empty($obj->moduleName)) { |
|
| 377 | + $module = $obj->moduleName; |
|
| 378 | + } |
|
| 379 | + return array( |
|
| 380 | + 'function' => $function, |
|
| 381 | + 'module' => $module, |
|
| 382 | + 'file' => $file |
|
| 383 | + ); |
|
| 384 | + } |
|
| 385 | 385 | |
| 386 | - /** |
|
| 387 | - * Get the module information for the language to load |
|
| 388 | - * @param string $language the language name like moduleName/languageName, languageName, |
|
| 389 | - * @return array the module information |
|
| 390 | - * array( |
|
| 391 | - * 'module'=> 'module_name' |
|
| 392 | - * 'language' => 'language' |
|
| 393 | - * 'file' => 'file' |
|
| 394 | - * ) |
|
| 395 | - */ |
|
| 396 | - protected static function getModuleInfoForLanguage($language) { |
|
| 397 | - $module = null; |
|
| 398 | - $file = null; |
|
| 399 | - $obj = & get_instance(); |
|
| 400 | - //check if the request class contains module name |
|
| 401 | - if (strpos($language, '/') !== false) { |
|
| 402 | - $path = explode('/', $language); |
|
| 403 | - if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
| 404 | - $module = $path[0]; |
|
| 405 | - $language = 'lang_' . $path[1] . '.php'; |
|
| 406 | - $file = $path[0] . DS . $language; |
|
| 407 | - } |
|
| 408 | - } |
|
| 409 | - if (!$module && !empty($obj->moduleName)) { |
|
| 410 | - $module = $obj->moduleName; |
|
| 411 | - } |
|
| 412 | - return array( |
|
| 413 | - 'language' => $language, |
|
| 414 | - 'module' => $module, |
|
| 415 | - 'file' => $file |
|
| 416 | - ); |
|
| 417 | - } |
|
| 386 | + /** |
|
| 387 | + * Get the module information for the language to load |
|
| 388 | + * @param string $language the language name like moduleName/languageName, languageName, |
|
| 389 | + * @return array the module information |
|
| 390 | + * array( |
|
| 391 | + * 'module'=> 'module_name' |
|
| 392 | + * 'language' => 'language' |
|
| 393 | + * 'file' => 'file' |
|
| 394 | + * ) |
|
| 395 | + */ |
|
| 396 | + protected static function getModuleInfoForLanguage($language) { |
|
| 397 | + $module = null; |
|
| 398 | + $file = null; |
|
| 399 | + $obj = & get_instance(); |
|
| 400 | + //check if the request class contains module name |
|
| 401 | + if (strpos($language, '/') !== false) { |
|
| 402 | + $path = explode('/', $language); |
|
| 403 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
| 404 | + $module = $path[0]; |
|
| 405 | + $language = 'lang_' . $path[1] . '.php'; |
|
| 406 | + $file = $path[0] . DS . $language; |
|
| 407 | + } |
|
| 408 | + } |
|
| 409 | + if (!$module && !empty($obj->moduleName)) { |
|
| 410 | + $module = $obj->moduleName; |
|
| 411 | + } |
|
| 412 | + return array( |
|
| 413 | + 'language' => $language, |
|
| 414 | + 'module' => $module, |
|
| 415 | + 'file' => $file |
|
| 416 | + ); |
|
| 417 | + } |
|
| 418 | 418 | |
| 419 | 419 | |
| 420 | - /** |
|
| 421 | - * Get the module information for the config to load |
|
| 422 | - * @param string $filename the filename of the configuration file, |
|
| 423 | - * @return array the module information |
|
| 424 | - * array( |
|
| 425 | - * 'module'=> 'module_name' |
|
| 426 | - * 'filename' => 'filename' |
|
| 427 | - * ) |
|
| 428 | - */ |
|
| 429 | - protected static function getModuleInfoForConfig($filename) { |
|
| 430 | - $module = null; |
|
| 431 | - $obj = & get_instance(); |
|
| 432 | - //check if the request class contains module name |
|
| 433 | - if (strpos($filename, '/') !== false) { |
|
| 434 | - $path = explode('/', $filename); |
|
| 435 | - if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
| 436 | - $module = $path[0]; |
|
| 437 | - $filename = $path[1] . '.php'; |
|
| 438 | - } |
|
| 439 | - } |
|
| 440 | - if (!$module && !empty($obj->moduleName)) { |
|
| 441 | - $module = $obj->moduleName; |
|
| 442 | - } |
|
| 443 | - return array( |
|
| 444 | - 'filename' => $filename, |
|
| 445 | - 'module' => $module |
|
| 446 | - ); |
|
| 447 | - } |
|
| 420 | + /** |
|
| 421 | + * Get the module information for the config to load |
|
| 422 | + * @param string $filename the filename of the configuration file, |
|
| 423 | + * @return array the module information |
|
| 424 | + * array( |
|
| 425 | + * 'module'=> 'module_name' |
|
| 426 | + * 'filename' => 'filename' |
|
| 427 | + * ) |
|
| 428 | + */ |
|
| 429 | + protected static function getModuleInfoForConfig($filename) { |
|
| 430 | + $module = null; |
|
| 431 | + $obj = & get_instance(); |
|
| 432 | + //check if the request class contains module name |
|
| 433 | + if (strpos($filename, '/') !== false) { |
|
| 434 | + $path = explode('/', $filename); |
|
| 435 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
| 436 | + $module = $path[0]; |
|
| 437 | + $filename = $path[1] . '.php'; |
|
| 438 | + } |
|
| 439 | + } |
|
| 440 | + if (!$module && !empty($obj->moduleName)) { |
|
| 441 | + $module = $obj->moduleName; |
|
| 442 | + } |
|
| 443 | + return array( |
|
| 444 | + 'filename' => $filename, |
|
| 445 | + 'module' => $module |
|
| 446 | + ); |
|
| 447 | + } |
|
| 448 | 448 | |
| 449 | - /** |
|
| 450 | - * Get the name of model or library instance if is null |
|
| 451 | - * @param string $class the class name to determine the instance |
|
| 452 | - * @return string the instance name |
|
| 453 | - */ |
|
| 454 | - protected static function getModelLibraryInstanceName($class){ |
|
| 455 | - //for module |
|
| 456 | - $instance = null; |
|
| 457 | - if (strpos($class, '/') !== false){ |
|
| 458 | - $path = explode('/', $class); |
|
| 459 | - if (isset($path[1])){ |
|
| 460 | - $instance = strtolower($path[1]); |
|
| 461 | - } |
|
| 462 | - } else{ |
|
| 463 | - $instance = strtolower($class); |
|
| 464 | - } |
|
| 465 | - return $instance; |
|
| 466 | - } |
|
| 449 | + /** |
|
| 450 | + * Get the name of model or library instance if is null |
|
| 451 | + * @param string $class the class name to determine the instance |
|
| 452 | + * @return string the instance name |
|
| 453 | + */ |
|
| 454 | + protected static function getModelLibraryInstanceName($class){ |
|
| 455 | + //for module |
|
| 456 | + $instance = null; |
|
| 457 | + if (strpos($class, '/') !== false){ |
|
| 458 | + $path = explode('/', $class); |
|
| 459 | + if (isset($path[1])){ |
|
| 460 | + $instance = strtolower($path[1]); |
|
| 461 | + } |
|
| 462 | + } else{ |
|
| 463 | + $instance = strtolower($class); |
|
| 464 | + } |
|
| 465 | + return $instance; |
|
| 466 | + } |
|
| 467 | 467 | |
| 468 | - /** |
|
| 469 | - * Get the library file path using the module information |
|
| 470 | - * @param string $class the class name |
|
| 471 | - * @return string|null the library file path otherwise null will be returned |
|
| 472 | - */ |
|
| 473 | - protected static function getLibraryPathUsingModuleInfo($class) { |
|
| 474 | - $logger = static::getLogger(); |
|
| 475 | - $libraryFilePath = null; |
|
| 476 | - $logger->debug('Checking library [' . $class . '] from module list ...'); |
|
| 477 | - $moduleInfo = self::getModuleInfoForModelLibrary($class); |
|
| 478 | - $module = $moduleInfo['module']; |
|
| 479 | - $class = $moduleInfo['class']; |
|
| 480 | - $moduleLibraryPath = Module::findLibraryFullPath($class, $module); |
|
| 481 | - if ($moduleLibraryPath){ |
|
| 482 | - $logger->info('Found library [' . $class . '] from module [' .$module. '], the file path is [' .$moduleLibraryPath. '] we will used it'); |
|
| 483 | - $libraryFilePath = $moduleLibraryPath; |
|
| 484 | - } else{ |
|
| 485 | - $logger->info('Cannot find library [' . $class . '] from modules using the default location'); |
|
| 486 | - } |
|
| 487 | - return $libraryFilePath; |
|
| 488 | - } |
|
| 468 | + /** |
|
| 469 | + * Get the library file path using the module information |
|
| 470 | + * @param string $class the class name |
|
| 471 | + * @return string|null the library file path otherwise null will be returned |
|
| 472 | + */ |
|
| 473 | + protected static function getLibraryPathUsingModuleInfo($class) { |
|
| 474 | + $logger = static::getLogger(); |
|
| 475 | + $libraryFilePath = null; |
|
| 476 | + $logger->debug('Checking library [' . $class . '] from module list ...'); |
|
| 477 | + $moduleInfo = self::getModuleInfoForModelLibrary($class); |
|
| 478 | + $module = $moduleInfo['module']; |
|
| 479 | + $class = $moduleInfo['class']; |
|
| 480 | + $moduleLibraryPath = Module::findLibraryFullPath($class, $module); |
|
| 481 | + if ($moduleLibraryPath){ |
|
| 482 | + $logger->info('Found library [' . $class . '] from module [' .$module. '], the file path is [' .$moduleLibraryPath. '] we will used it'); |
|
| 483 | + $libraryFilePath = $moduleLibraryPath; |
|
| 484 | + } else{ |
|
| 485 | + $logger->info('Cannot find library [' . $class . '] from modules using the default location'); |
|
| 486 | + } |
|
| 487 | + return $libraryFilePath; |
|
| 488 | + } |
|
| 489 | 489 | |
| 490 | - /** |
|
| 491 | - * Load the library |
|
| 492 | - * @param string $libraryFilePath the file path of the library to load |
|
| 493 | - * @param string $class the class name |
|
| 494 | - * @param string $instance the instance |
|
| 495 | - * @param array $params the parameter to use |
|
| 496 | - * @return void |
|
| 497 | - */ |
|
| 498 | - protected static function loadLibrary($libraryFilePath, $class, $instance, $params = array()){ |
|
| 499 | - if ($libraryFilePath){ |
|
| 500 | - $logger = static::getLogger(); |
|
| 501 | - require_once $libraryFilePath; |
|
| 502 | - if (class_exists($class)){ |
|
| 503 | - $c = $params ? new $class($params) : new $class(); |
|
| 504 | - $obj = & get_instance(); |
|
| 505 | - $obj->{$instance} = $c; |
|
| 506 | - static::$loaded[$instance] = $class; |
|
| 507 | - $logger->info('Library [' . $class . '] --> ' . $libraryFilePath . ' loaded successfully.'); |
|
| 508 | - } else{ |
|
| 509 | - show_error('The file '.$libraryFilePath.' exists but does not contain the class '.$class); |
|
| 510 | - } |
|
| 511 | - } else{ |
|
| 512 | - show_error('Unable to find library class [' . $class . ']'); |
|
| 513 | - } |
|
| 514 | - } |
|
| 490 | + /** |
|
| 491 | + * Load the library |
|
| 492 | + * @param string $libraryFilePath the file path of the library to load |
|
| 493 | + * @param string $class the class name |
|
| 494 | + * @param string $instance the instance |
|
| 495 | + * @param array $params the parameter to use |
|
| 496 | + * @return void |
|
| 497 | + */ |
|
| 498 | + protected static function loadLibrary($libraryFilePath, $class, $instance, $params = array()){ |
|
| 499 | + if ($libraryFilePath){ |
|
| 500 | + $logger = static::getLogger(); |
|
| 501 | + require_once $libraryFilePath; |
|
| 502 | + if (class_exists($class)){ |
|
| 503 | + $c = $params ? new $class($params) : new $class(); |
|
| 504 | + $obj = & get_instance(); |
|
| 505 | + $obj->{$instance} = $c; |
|
| 506 | + static::$loaded[$instance] = $class; |
|
| 507 | + $logger->info('Library [' . $class . '] --> ' . $libraryFilePath . ' loaded successfully.'); |
|
| 508 | + } else{ |
|
| 509 | + show_error('The file '.$libraryFilePath.' exists but does not contain the class '.$class); |
|
| 510 | + } |
|
| 511 | + } else{ |
|
| 512 | + show_error('Unable to find library class [' . $class . ']'); |
|
| 513 | + } |
|
| 514 | + } |
|
| 515 | 515 | |
| 516 | - /** |
|
| 517 | - * Load the language |
|
| 518 | - * @param string $languageFilePath the file path of the language to load |
|
| 519 | - * @param string $language the language name |
|
| 520 | - * @return void |
|
| 521 | - */ |
|
| 522 | - protected static function loadLanguage($languageFilePath, $language) { |
|
| 523 | - if ($languageFilePath) { |
|
| 524 | - $logger = static::getLogger(); |
|
| 525 | - $lang = array(); |
|
| 526 | - require_once $languageFilePath; |
|
| 527 | - if (!empty($lang) && is_array($lang)) { |
|
| 528 | - $logger->info('Language file [' . $languageFilePath . '] contains the valid languages keys add them to language list'); |
|
| 529 | - //Note: may be here the class 'Lang' not yet loaded |
|
| 530 | - $langObj = & class_loader('Lang', 'classes'); |
|
| 531 | - $langObj->addLangMessages($lang); |
|
| 532 | - //free the memory |
|
| 533 | - unset($lang); |
|
| 534 | - } |
|
| 535 | - static::$loaded['lang_' . $language] = $languageFilePath; |
|
| 536 | - $logger->info('Language [' . $language . '] --> ' . $languageFilePath . ' loaded successfully.'); |
|
| 537 | - } else{ |
|
| 538 | - show_error('Unable to find language [' . $language . ']'); |
|
| 539 | - } |
|
| 540 | - } |
|
| 516 | + /** |
|
| 517 | + * Load the language |
|
| 518 | + * @param string $languageFilePath the file path of the language to load |
|
| 519 | + * @param string $language the language name |
|
| 520 | + * @return void |
|
| 521 | + */ |
|
| 522 | + protected static function loadLanguage($languageFilePath, $language) { |
|
| 523 | + if ($languageFilePath) { |
|
| 524 | + $logger = static::getLogger(); |
|
| 525 | + $lang = array(); |
|
| 526 | + require_once $languageFilePath; |
|
| 527 | + if (!empty($lang) && is_array($lang)) { |
|
| 528 | + $logger->info('Language file [' . $languageFilePath . '] contains the valid languages keys add them to language list'); |
|
| 529 | + //Note: may be here the class 'Lang' not yet loaded |
|
| 530 | + $langObj = & class_loader('Lang', 'classes'); |
|
| 531 | + $langObj->addLangMessages($lang); |
|
| 532 | + //free the memory |
|
| 533 | + unset($lang); |
|
| 534 | + } |
|
| 535 | + static::$loaded['lang_' . $language] = $languageFilePath; |
|
| 536 | + $logger->info('Language [' . $language . '] --> ' . $languageFilePath . ' loaded successfully.'); |
|
| 537 | + } else{ |
|
| 538 | + show_error('Unable to find language [' . $language . ']'); |
|
| 539 | + } |
|
| 540 | + } |
|
| 541 | 541 | |
| 542 | - /** |
|
| 543 | - * Get all the autoload using the configuration file |
|
| 544 | - * @return array |
|
| 545 | - */ |
|
| 546 | - private function getResourcesFromAutoloadConfig() { |
|
| 547 | - $autoloads = array(); |
|
| 548 | - $autoloads['config'] = array(); |
|
| 549 | - $autoloads['languages'] = array(); |
|
| 550 | - $autoloads['libraries'] = array(); |
|
| 551 | - $autoloads['models'] = array(); |
|
| 552 | - $autoloads['functions'] = array(); |
|
| 553 | - //loading of the resources from autoload configuration file |
|
| 554 | - if (file_exists(CONFIG_PATH . 'autoload.php')) { |
|
| 555 | - $autoload = array(); |
|
| 556 | - require_once CONFIG_PATH . 'autoload.php'; |
|
| 557 | - if (!empty($autoload) && is_array($autoload)) { |
|
| 558 | - $autoloads = array_merge($autoloads, $autoload); |
|
| 559 | - unset($autoload); |
|
| 560 | - } |
|
| 561 | - } |
|
| 562 | - //loading autoload configuration for modules |
|
| 563 | - $modulesAutoloads = Module::getModulesAutoloadConfig(); |
|
| 564 | - if (!empty($modulesAutoloads) && is_array($modulesAutoloads)) { |
|
| 565 | - $autoloads = array_merge_recursive($autoloads, $modulesAutoloads); |
|
| 566 | - } |
|
| 567 | - return $autoloads; |
|
| 568 | - } |
|
| 542 | + /** |
|
| 543 | + * Get all the autoload using the configuration file |
|
| 544 | + * @return array |
|
| 545 | + */ |
|
| 546 | + private function getResourcesFromAutoloadConfig() { |
|
| 547 | + $autoloads = array(); |
|
| 548 | + $autoloads['config'] = array(); |
|
| 549 | + $autoloads['languages'] = array(); |
|
| 550 | + $autoloads['libraries'] = array(); |
|
| 551 | + $autoloads['models'] = array(); |
|
| 552 | + $autoloads['functions'] = array(); |
|
| 553 | + //loading of the resources from autoload configuration file |
|
| 554 | + if (file_exists(CONFIG_PATH . 'autoload.php')) { |
|
| 555 | + $autoload = array(); |
|
| 556 | + require_once CONFIG_PATH . 'autoload.php'; |
|
| 557 | + if (!empty($autoload) && is_array($autoload)) { |
|
| 558 | + $autoloads = array_merge($autoloads, $autoload); |
|
| 559 | + unset($autoload); |
|
| 560 | + } |
|
| 561 | + } |
|
| 562 | + //loading autoload configuration for modules |
|
| 563 | + $modulesAutoloads = Module::getModulesAutoloadConfig(); |
|
| 564 | + if (!empty($modulesAutoloads) && is_array($modulesAutoloads)) { |
|
| 565 | + $autoloads = array_merge_recursive($autoloads, $modulesAutoloads); |
|
| 566 | + } |
|
| 567 | + return $autoloads; |
|
| 568 | + } |
|
| 569 | 569 | |
| 570 | - /** |
|
| 571 | - * Load the autoload configuration |
|
| 572 | - * @return void |
|
| 573 | - */ |
|
| 574 | - private function loadResourcesFromAutoloadConfig() { |
|
| 575 | - $autoloads = array(); |
|
| 576 | - $autoloads['config'] = array(); |
|
| 577 | - $autoloads['languages'] = array(); |
|
| 578 | - $autoloads['libraries'] = array(); |
|
| 579 | - $autoloads['models'] = array(); |
|
| 580 | - $autoloads['functions'] = array(); |
|
| 570 | + /** |
|
| 571 | + * Load the autoload configuration |
|
| 572 | + * @return void |
|
| 573 | + */ |
|
| 574 | + private function loadResourcesFromAutoloadConfig() { |
|
| 575 | + $autoloads = array(); |
|
| 576 | + $autoloads['config'] = array(); |
|
| 577 | + $autoloads['languages'] = array(); |
|
| 578 | + $autoloads['libraries'] = array(); |
|
| 579 | + $autoloads['models'] = array(); |
|
| 580 | + $autoloads['functions'] = array(); |
|
| 581 | 581 | |
| 582 | - $list = $this->getResourcesFromAutoloadConfig(); |
|
| 583 | - $autoloads = array_merge($autoloads, $list); |
|
| 582 | + $list = $this->getResourcesFromAutoloadConfig(); |
|
| 583 | + $autoloads = array_merge($autoloads, $list); |
|
| 584 | 584 | |
| 585 | - //config autoload |
|
| 586 | - $this->loadAutoloadResourcesArray('config', $autoloads['config']); |
|
| 585 | + //config autoload |
|
| 586 | + $this->loadAutoloadResourcesArray('config', $autoloads['config']); |
|
| 587 | 587 | |
| 588 | - //languages autoload |
|
| 589 | - $this->loadAutoloadResourcesArray('lang', $autoloads['languages']); |
|
| 588 | + //languages autoload |
|
| 589 | + $this->loadAutoloadResourcesArray('lang', $autoloads['languages']); |
|
| 590 | 590 | |
| 591 | - //libraries autoload |
|
| 592 | - $this->loadAutoloadResourcesArray('library', $autoloads['libraries']); |
|
| 591 | + //libraries autoload |
|
| 592 | + $this->loadAutoloadResourcesArray('library', $autoloads['libraries']); |
|
| 593 | 593 | |
| 594 | - //models autoload |
|
| 595 | - $this->loadAutoloadResourcesArray('model', $autoloads['models']); |
|
| 594 | + //models autoload |
|
| 595 | + $this->loadAutoloadResourcesArray('model', $autoloads['models']); |
|
| 596 | 596 | |
| 597 | - //functions autoload |
|
| 598 | - $this->loadAutoloadResourcesArray('functions', $autoloads['functions']); |
|
| 599 | - } |
|
| 597 | + //functions autoload |
|
| 598 | + $this->loadAutoloadResourcesArray('functions', $autoloads['functions']); |
|
| 599 | + } |
|
| 600 | 600 | |
| 601 | - /** |
|
| 602 | - * Load the resources autoload array |
|
| 603 | - * @param string $method this object method name to call |
|
| 604 | - * @param array $resources the resource to load |
|
| 605 | - * @return void |
|
| 606 | - */ |
|
| 607 | - private function loadAutoloadResourcesArray($method, array $resources) { |
|
| 608 | - foreach ($resources as $name) { |
|
| 609 | - $this->{$method}($name); |
|
| 610 | - } |
|
| 611 | - } |
|
| 612 | - } |
|
| 601 | + /** |
|
| 602 | + * Load the resources autoload array |
|
| 603 | + * @param string $method this object method name to call |
|
| 604 | + * @param array $resources the resource to load |
|
| 605 | + * @return void |
|
| 606 | + */ |
|
| 607 | + private function loadAutoloadResourcesArray($method, array $resources) { |
|
| 608 | + foreach ($resources as $name) { |
|
| 609 | + $this->{$method}($name); |
|
| 610 | + } |
|
| 611 | + } |
|
| 612 | + } |
|
@@ -42,41 +42,41 @@ |
||
| 42 | 42 | 'sbrowser' => '', //VARCHAR(255) |
| 43 | 43 | 'skey' => '' //VARCHAR(255) |
| 44 | 44 | ); |
| 45 | - */ |
|
| 46 | - protected $sessionTableColumns = array(); |
|
| 45 | + */ |
|
| 46 | + protected $sessionTableColumns = array(); |
|
| 47 | 47 | |
| 48 | - public function __construct(Database $db = null) { |
|
| 49 | - parent::__construct($db); |
|
| 50 | - } |
|
| 48 | + public function __construct(Database $db = null) { |
|
| 49 | + parent::__construct($db); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Return the session database table columns |
|
| 54 | - * @return array |
|
| 55 | - */ |
|
| 56 | - public function getSessionTableColumns() { |
|
| 57 | - return $this->sessionTableColumns; |
|
| 58 | - } |
|
| 52 | + /** |
|
| 53 | + * Return the session database table columns |
|
| 54 | + * @return array |
|
| 55 | + */ |
|
| 56 | + public function getSessionTableColumns() { |
|
| 57 | + return $this->sessionTableColumns; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Set the session database table columns |
|
| 62 | - * @param array $columns the columns definition |
|
| 63 | - */ |
|
| 64 | - public function setSessionTableColumns(array $columns) { |
|
| 65 | - $this->sessionTableColumns = $columns; |
|
| 66 | - return $this; |
|
| 67 | - } |
|
| 60 | + /** |
|
| 61 | + * Set the session database table columns |
|
| 62 | + * @param array $columns the columns definition |
|
| 63 | + */ |
|
| 64 | + public function setSessionTableColumns(array $columns) { |
|
| 65 | + $this->sessionTableColumns = $columns; |
|
| 66 | + return $this; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Delete the expire session |
|
| 71 | - * @param int $time the unix timestamp |
|
| 72 | - * @return int affected rows |
|
| 73 | - */ |
|
| 74 | - abstract public function deleteByTime($time); |
|
| 69 | + /** |
|
| 70 | + * Delete the expire session |
|
| 71 | + * @param int $time the unix timestamp |
|
| 72 | + * @return int affected rows |
|
| 73 | + */ |
|
| 74 | + abstract public function deleteByTime($time); |
|
| 75 | 75 | |
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * How to get the value of the table column key. Generally is the session key |
|
| 79 | - * @return mixed the key value like used to identify the data |
|
| 80 | - */ |
|
| 81 | - abstract public function getKeyValue(); |
|
| 82 | - } |
|
| 77 | + /** |
|
| 78 | + * How to get the value of the table column key. Generally is the session key |
|
| 79 | + * @return mixed the key value like used to identify the data |
|
| 80 | + */ |
|
| 81 | + abstract public function getKeyValue(); |
|
| 82 | + } |
|
@@ -1,199 +1,199 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * For application languages management |
|
| 29 | - */ |
|
| 30 | - class Lang extends BaseClass { |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * For application languages management |
|
| 29 | + */ |
|
| 30 | + class Lang extends BaseClass { |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * The supported available language for this application. |
|
| 34 | - * @example "en" => "english" |
|
| 35 | - * @see Lang::addLang() |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - protected $availables = array(); |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * The all messages language |
|
| 42 | - * @var array |
|
| 43 | - */ |
|
| 44 | - protected $languages = array(); |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The default language to use if can not |
|
| 48 | - * determine the client language |
|
| 49 | - * |
|
| 50 | - * @example $default = 'en' |
|
| 51 | - * @var string |
|
| 52 | - */ |
|
| 53 | - protected $default = null; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * The current client language |
|
| 57 | - * @var string |
|
| 58 | - */ |
|
| 59 | - protected $current = null; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Construct new Lang instance |
|
| 63 | - */ |
|
| 64 | - public function __construct() { |
|
| 65 | - parent::__construct(); |
|
| 66 | - |
|
| 67 | - $this->default = get_config('default_language', 'en'); |
|
| 68 | - $this->logger->debug('Setting the supported languages'); |
|
| 32 | + /** |
|
| 33 | + * The supported available language for this application. |
|
| 34 | + * @example "en" => "english" |
|
| 35 | + * @see Lang::addLang() |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + protected $availables = array(); |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * The all messages language |
|
| 42 | + * @var array |
|
| 43 | + */ |
|
| 44 | + protected $languages = array(); |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The default language to use if can not |
|
| 48 | + * determine the client language |
|
| 49 | + * |
|
| 50 | + * @example $default = 'en' |
|
| 51 | + * @var string |
|
| 52 | + */ |
|
| 53 | + protected $default = null; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * The current client language |
|
| 57 | + * @var string |
|
| 58 | + */ |
|
| 59 | + protected $current = null; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Construct new Lang instance |
|
| 63 | + */ |
|
| 64 | + public function __construct() { |
|
| 65 | + parent::__construct(); |
|
| 66 | + |
|
| 67 | + $this->default = get_config('default_language', 'en'); |
|
| 68 | + $this->logger->debug('Setting the supported languages'); |
|
| 69 | 69 | |
| 70 | - //add the supported languages ('key', 'display name') |
|
| 71 | - $languages = get_config('languages', null); |
|
| 72 | - if (!empty($languages)) { |
|
| 73 | - foreach ($languages as $key => $displayName) { |
|
| 74 | - $this->addLang($key, $displayName); |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - unset($languages); |
|
| 78 | - |
|
| 79 | - //if the language exists in cookie use it |
|
| 80 | - $cfgKey = get_config('language_cookie_name'); |
|
| 81 | - $this->logger->debug('Getting current language from cookie [' .$cfgKey. ']'); |
|
| 82 | - $objCookie = & class_loader('Cookie'); |
|
| 83 | - $cookieLang = $objCookie->get($cfgKey); |
|
| 84 | - if($cookieLang && $this->isValid($cookieLang)){ |
|
| 85 | - $this->current = $cookieLang; |
|
| 86 | - $this->logger->info('Language from cookie [' .$cfgKey. '] is valid so we will set the language using the cookie value [' .$cookieLang. ']'); |
|
| 87 | - } else{ |
|
| 88 | - $this->logger->info('Language from cookie [' .$cfgKey. '] is not set, use the default value [' .$this->getDefault(). ']'); |
|
| 89 | - $this->current = $this->getDefault(); |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Get the all languages messages |
|
| 95 | - * |
|
| 96 | - * @return array the language message list |
|
| 97 | - */ |
|
| 98 | - public function getAll() { |
|
| 99 | - return $this->languages; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Set the language message |
|
| 104 | - * |
|
| 105 | - * @param string $key the language key to identify |
|
| 106 | - * @param string $value the language message value |
|
| 107 | - */ |
|
| 108 | - public function set($key, $value) { |
|
| 109 | - $this->languages[$key] = $value; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Get the language message for the given key. If can't find return the default value |
|
| 114 | - * |
|
| 115 | - * @param string $key the message language key |
|
| 116 | - * @param string $default the default value to return if can not found the language message key |
|
| 117 | - * |
|
| 118 | - * @return string the language message value |
|
| 119 | - */ |
|
| 120 | - public function get($key, $default = 'LANGUAGE_ERROR') { |
|
| 121 | - if (isset($this->languages[$key])) { |
|
| 122 | - return $this->languages[$key]; |
|
| 123 | - } |
|
| 124 | - $this->logger->warning('Language key [' . $key . '] does not exist use the default value [' . $default . ']'); |
|
| 125 | - return $default; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Check whether the language file for given name exists |
|
| 130 | - * |
|
| 131 | - * @param string $language the language name like "fr", "en", etc. |
|
| 132 | - * |
|
| 133 | - * @return boolean true if the language directory exists, false or not |
|
| 134 | - */ |
|
| 135 | - public function isValid($language) { |
|
| 136 | - $searchDir = array(CORE_LANG_PATH, APP_LANG_PATH); |
|
| 137 | - foreach ($searchDir as $dir) { |
|
| 138 | - if (file_exists($dir . $language) && is_dir($dir . $language)) { |
|
| 139 | - return true; |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - return false; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Get the default language value like "en" , "fr", etc. |
|
| 147 | - * |
|
| 148 | - * @return string the default language |
|
| 149 | - */ |
|
| 150 | - public function getDefault() { |
|
| 151 | - return $this->default; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Get the current language defined by cookie or the default value |
|
| 156 | - * |
|
| 157 | - * @return string the current language |
|
| 158 | - */ |
|
| 159 | - public function getCurrent() { |
|
| 160 | - return $this->current; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Add new supported or available language |
|
| 165 | - * |
|
| 166 | - * @param string $name the short language name like "en", "fr". |
|
| 167 | - * @param string $description the human readable description of this language |
|
| 168 | - */ |
|
| 169 | - public function addLang($name, $description){ |
|
| 170 | - if(isset($this->availables[$name])){ |
|
| 171 | - return; //already added cost in performance |
|
| 172 | - } |
|
| 173 | - if($this->isValid($name)){ |
|
| 174 | - $this->availables[$name] = $description; |
|
| 175 | - } else{ |
|
| 176 | - show_error('The language [' . $name . '] is not valid or does not exists.'); |
|
| 177 | - } |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Get the list of the application supported language |
|
| 182 | - * |
|
| 183 | - * @return array the list of the application language |
|
| 184 | - */ |
|
| 185 | - public function getSupported() { |
|
| 186 | - return $this->availables; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Add new language messages |
|
| 191 | - * |
|
| 192 | - * @param array $langs the languages array of the messages to be added |
|
| 193 | - */ |
|
| 194 | - public function addLangMessages(array $langs) { |
|
| 195 | - foreach ($langs as $key => $value) { |
|
| 196 | - $this->set($key, $value); |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - } |
|
| 70 | + //add the supported languages ('key', 'display name') |
|
| 71 | + $languages = get_config('languages', null); |
|
| 72 | + if (!empty($languages)) { |
|
| 73 | + foreach ($languages as $key => $displayName) { |
|
| 74 | + $this->addLang($key, $displayName); |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + unset($languages); |
|
| 78 | + |
|
| 79 | + //if the language exists in cookie use it |
|
| 80 | + $cfgKey = get_config('language_cookie_name'); |
|
| 81 | + $this->logger->debug('Getting current language from cookie [' .$cfgKey. ']'); |
|
| 82 | + $objCookie = & class_loader('Cookie'); |
|
| 83 | + $cookieLang = $objCookie->get($cfgKey); |
|
| 84 | + if($cookieLang && $this->isValid($cookieLang)){ |
|
| 85 | + $this->current = $cookieLang; |
|
| 86 | + $this->logger->info('Language from cookie [' .$cfgKey. '] is valid so we will set the language using the cookie value [' .$cookieLang. ']'); |
|
| 87 | + } else{ |
|
| 88 | + $this->logger->info('Language from cookie [' .$cfgKey. '] is not set, use the default value [' .$this->getDefault(). ']'); |
|
| 89 | + $this->current = $this->getDefault(); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Get the all languages messages |
|
| 95 | + * |
|
| 96 | + * @return array the language message list |
|
| 97 | + */ |
|
| 98 | + public function getAll() { |
|
| 99 | + return $this->languages; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Set the language message |
|
| 104 | + * |
|
| 105 | + * @param string $key the language key to identify |
|
| 106 | + * @param string $value the language message value |
|
| 107 | + */ |
|
| 108 | + public function set($key, $value) { |
|
| 109 | + $this->languages[$key] = $value; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Get the language message for the given key. If can't find return the default value |
|
| 114 | + * |
|
| 115 | + * @param string $key the message language key |
|
| 116 | + * @param string $default the default value to return if can not found the language message key |
|
| 117 | + * |
|
| 118 | + * @return string the language message value |
|
| 119 | + */ |
|
| 120 | + public function get($key, $default = 'LANGUAGE_ERROR') { |
|
| 121 | + if (isset($this->languages[$key])) { |
|
| 122 | + return $this->languages[$key]; |
|
| 123 | + } |
|
| 124 | + $this->logger->warning('Language key [' . $key . '] does not exist use the default value [' . $default . ']'); |
|
| 125 | + return $default; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Check whether the language file for given name exists |
|
| 130 | + * |
|
| 131 | + * @param string $language the language name like "fr", "en", etc. |
|
| 132 | + * |
|
| 133 | + * @return boolean true if the language directory exists, false or not |
|
| 134 | + */ |
|
| 135 | + public function isValid($language) { |
|
| 136 | + $searchDir = array(CORE_LANG_PATH, APP_LANG_PATH); |
|
| 137 | + foreach ($searchDir as $dir) { |
|
| 138 | + if (file_exists($dir . $language) && is_dir($dir . $language)) { |
|
| 139 | + return true; |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + return false; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Get the default language value like "en" , "fr", etc. |
|
| 147 | + * |
|
| 148 | + * @return string the default language |
|
| 149 | + */ |
|
| 150 | + public function getDefault() { |
|
| 151 | + return $this->default; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Get the current language defined by cookie or the default value |
|
| 156 | + * |
|
| 157 | + * @return string the current language |
|
| 158 | + */ |
|
| 159 | + public function getCurrent() { |
|
| 160 | + return $this->current; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Add new supported or available language |
|
| 165 | + * |
|
| 166 | + * @param string $name the short language name like "en", "fr". |
|
| 167 | + * @param string $description the human readable description of this language |
|
| 168 | + */ |
|
| 169 | + public function addLang($name, $description){ |
|
| 170 | + if(isset($this->availables[$name])){ |
|
| 171 | + return; //already added cost in performance |
|
| 172 | + } |
|
| 173 | + if($this->isValid($name)){ |
|
| 174 | + $this->availables[$name] = $description; |
|
| 175 | + } else{ |
|
| 176 | + show_error('The language [' . $name . '] is not valid or does not exists.'); |
|
| 177 | + } |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Get the list of the application supported language |
|
| 182 | + * |
|
| 183 | + * @return array the list of the application language |
|
| 184 | + */ |
|
| 185 | + public function getSupported() { |
|
| 186 | + return $this->availables; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Add new language messages |
|
| 191 | + * |
|
| 192 | + * @param array $langs the languages array of the messages to be added |
|
| 193 | + */ |
|
| 194 | + public function addLangMessages(array $langs) { |
|
| 195 | + foreach ($langs as $key => $value) { |
|
| 196 | + $this->set($key, $value); |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + } |
|
@@ -1,158 +1,158 @@ |
||
| 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 Config extends BaseStaticClass { |
|
| 27 | + class Config extends BaseStaticClass { |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The list of loaded configuration |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - private static $config = array(); |
|
| 29 | + /** |
|
| 30 | + * The list of loaded configuration |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + private static $config = array(); |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Initialize the configuration by loading all the configuration from config file |
|
| 37 | - */ |
|
| 38 | - public static function init() { |
|
| 39 | - $logger = self::getLogger(); |
|
| 40 | - $logger->debug('Initialization of the configuration'); |
|
| 41 | - self::$config = & load_configurations(); |
|
| 42 | - self::setBaseUrlUsingServerVar(); |
|
| 43 | - if (ENVIRONMENT == 'production' && in_array(strtolower(self::$config['log_level']), array('debug', 'info', 'all'))) { |
|
| 44 | - $logger->warning('You are in production environment, please set log level to WARNING, ERROR, FATAL to increase the application performance'); |
|
| 45 | - } |
|
| 46 | - $logger->info('Configuration initialized successfully'); |
|
| 47 | - $logger->info('The application configuration are listed below: ' . stringfy_vars(self::$config)); |
|
| 48 | - } |
|
| 35 | + /** |
|
| 36 | + * Initialize the configuration by loading all the configuration from config file |
|
| 37 | + */ |
|
| 38 | + public static function init() { |
|
| 39 | + $logger = self::getLogger(); |
|
| 40 | + $logger->debug('Initialization of the configuration'); |
|
| 41 | + self::$config = & load_configurations(); |
|
| 42 | + self::setBaseUrlUsingServerVar(); |
|
| 43 | + if (ENVIRONMENT == 'production' && in_array(strtolower(self::$config['log_level']), array('debug', 'info', 'all'))) { |
|
| 44 | + $logger->warning('You are in production environment, please set log level to WARNING, ERROR, FATAL to increase the application performance'); |
|
| 45 | + } |
|
| 46 | + $logger->info('Configuration initialized successfully'); |
|
| 47 | + $logger->info('The application configuration are listed below: ' . stringfy_vars(self::$config)); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Get the configuration item value |
|
| 52 | - * @param string $item the configuration item name to get |
|
| 53 | - * @param mixed $default the default value to use if can not find the config item in the list |
|
| 54 | - * @return mixed the config value if exist or the default value |
|
| 55 | - */ |
|
| 56 | - public static function get($item, $default = null) { |
|
| 57 | - $logger = self::getLogger(); |
|
| 58 | - if (array_key_exists($item, self::$config)) { |
|
| 59 | - return self::$config[$item]; |
|
| 60 | - } |
|
| 61 | - $logger->warning('Cannot find config item [' . $item . '] using the default value [' . $default . ']'); |
|
| 62 | - return $default; |
|
| 63 | - } |
|
| 50 | + /** |
|
| 51 | + * Get the configuration item value |
|
| 52 | + * @param string $item the configuration item name to get |
|
| 53 | + * @param mixed $default the default value to use if can not find the config item in the list |
|
| 54 | + * @return mixed the config value if exist or the default value |
|
| 55 | + */ |
|
| 56 | + public static function get($item, $default = null) { |
|
| 57 | + $logger = self::getLogger(); |
|
| 58 | + if (array_key_exists($item, self::$config)) { |
|
| 59 | + return self::$config[$item]; |
|
| 60 | + } |
|
| 61 | + $logger->warning('Cannot find config item [' . $item . '] using the default value [' . $default . ']'); |
|
| 62 | + return $default; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Set the configuration item value |
|
| 67 | - * @param string $item the config item name to set |
|
| 68 | - * @param mixed $value the config item value |
|
| 69 | - */ |
|
| 70 | - public static function set($item, $value) { |
|
| 71 | - self::$config[$item] = $value; |
|
| 72 | - } |
|
| 65 | + /** |
|
| 66 | + * Set the configuration item value |
|
| 67 | + * @param string $item the config item name to set |
|
| 68 | + * @param mixed $value the config item value |
|
| 69 | + */ |
|
| 70 | + public static function set($item, $value) { |
|
| 71 | + self::$config[$item] = $value; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Get all the configuration values |
|
| 76 | - * @return array the config values |
|
| 77 | - */ |
|
| 78 | - public static function getAll() { |
|
| 79 | - return self::$config; |
|
| 80 | - } |
|
| 74 | + /** |
|
| 75 | + * Get all the configuration values |
|
| 76 | + * @return array the config values |
|
| 77 | + */ |
|
| 78 | + public static function getAll() { |
|
| 79 | + return self::$config; |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * Set the configuration values bu merged with the existing configuration |
|
| 84 | - * @param array $config the config values to add in the configuration list |
|
| 85 | - */ |
|
| 86 | - public static function setAll(array $config = array()) { |
|
| 87 | - self::$config = array_merge(self::$config, $config); |
|
| 88 | - } |
|
| 82 | + /** |
|
| 83 | + * Set the configuration values bu merged with the existing configuration |
|
| 84 | + * @param array $config the config values to add in the configuration list |
|
| 85 | + */ |
|
| 86 | + public static function setAll(array $config = array()) { |
|
| 87 | + self::$config = array_merge(self::$config, $config); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Delete the configuration item in the list |
|
| 92 | - * @param string $item the config item name to be deleted |
|
| 93 | - * @return boolean true if the item exists and is deleted successfully otherwise will return false. |
|
| 94 | - */ |
|
| 95 | - public static function delete($item){ |
|
| 96 | - $logger = self::getLogger(); |
|
| 97 | - if(array_key_exists($item, self::$config)){ |
|
| 98 | - $logger->info('Delete config item ['.$item.']'); |
|
| 99 | - unset(self::$config[$item]); |
|
| 100 | - return true; |
|
| 101 | - } else{ |
|
| 102 | - $logger->warning('Config item ['.$item.'] to be deleted does not exists'); |
|
| 103 | - return false; |
|
| 104 | - } |
|
| 105 | - } |
|
| 90 | + /** |
|
| 91 | + * Delete the configuration item in the list |
|
| 92 | + * @param string $item the config item name to be deleted |
|
| 93 | + * @return boolean true if the item exists and is deleted successfully otherwise will return false. |
|
| 94 | + */ |
|
| 95 | + public static function delete($item){ |
|
| 96 | + $logger = self::getLogger(); |
|
| 97 | + if(array_key_exists($item, self::$config)){ |
|
| 98 | + $logger->info('Delete config item ['.$item.']'); |
|
| 99 | + unset(self::$config[$item]); |
|
| 100 | + return true; |
|
| 101 | + } else{ |
|
| 102 | + $logger->warning('Config item ['.$item.'] to be deleted does not exists'); |
|
| 103 | + return false; |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * Load the configuration file. This an alias to Loader::config() |
|
| 109 | - * @param string $config the config name to be loaded |
|
| 110 | - */ |
|
| 111 | - public static function load($config) { |
|
| 112 | - Loader::config($config); |
|
| 113 | - } |
|
| 107 | + /** |
|
| 108 | + * Load the configuration file. This an alias to Loader::config() |
|
| 109 | + * @param string $config the config name to be loaded |
|
| 110 | + */ |
|
| 111 | + public static function load($config) { |
|
| 112 | + Loader::config($config); |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * Set the configuration for "base_url" if is not set in the configuration |
|
| 117 | - */ |
|
| 118 | - private static function setBaseUrlUsingServerVar() { |
|
| 119 | - $logger = self::getLogger(); |
|
| 120 | - if (!isset(self::$config['base_url']) || !is_url(self::$config['base_url'])) { |
|
| 121 | - if (ENVIRONMENT == 'production') { |
|
| 122 | - $logger->warning('Application base URL is not set or invalid, please set application base URL to increase the application loading time'); |
|
| 123 | - } |
|
| 124 | - $baseUrl = null; |
|
| 125 | - $protocol = 'http'; |
|
| 126 | - if (is_https()) { |
|
| 127 | - $protocol = 'https'; |
|
| 128 | - } |
|
| 129 | - $protocol .= '://'; |
|
| 115 | + /** |
|
| 116 | + * Set the configuration for "base_url" if is not set in the configuration |
|
| 117 | + */ |
|
| 118 | + private static function setBaseUrlUsingServerVar() { |
|
| 119 | + $logger = self::getLogger(); |
|
| 120 | + if (!isset(self::$config['base_url']) || !is_url(self::$config['base_url'])) { |
|
| 121 | + if (ENVIRONMENT == 'production') { |
|
| 122 | + $logger->warning('Application base URL is not set or invalid, please set application base URL to increase the application loading time'); |
|
| 123 | + } |
|
| 124 | + $baseUrl = null; |
|
| 125 | + $protocol = 'http'; |
|
| 126 | + if (is_https()) { |
|
| 127 | + $protocol = 'https'; |
|
| 128 | + } |
|
| 129 | + $protocol .= '://'; |
|
| 130 | 130 | |
| 131 | - if (isset($_SERVER['SERVER_ADDR'])) { |
|
| 132 | - $baseUrl = $_SERVER['SERVER_ADDR']; |
|
| 133 | - //check if the server is running under IPv6 |
|
| 134 | - if (strpos($_SERVER['SERVER_ADDR'], ':') !== FALSE) { |
|
| 135 | - $baseUrl = '[' . $_SERVER['SERVER_ADDR'] . ']'; |
|
| 136 | - } |
|
| 137 | - $serverPort = 80; |
|
| 138 | - if (isset($_SERVER['SERVER_PORT'])) { |
|
| 139 | - $serverPort = $_SERVER['SERVER_PORT']; |
|
| 140 | - } |
|
| 141 | - $port = ''; |
|
| 142 | - if ($serverPort && ((is_https() && $serverPort != 443) || (!is_https() && $serverPort != 80))) { |
|
| 143 | - $port = ':' . $serverPort; |
|
| 144 | - } |
|
| 145 | - $baseUrl = $protocol . $baseUrl . $port . substr( |
|
| 146 | - $_SERVER['SCRIPT_NAME'], |
|
| 147 | - 0, |
|
| 148 | - strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME'])) |
|
| 149 | - ); |
|
| 150 | - } else{ |
|
| 151 | - $logger->warning('Can not determine the application base URL automatically, use http://localhost as default'); |
|
| 152 | - $baseUrl = 'http://localhost/'; |
|
| 153 | - } |
|
| 154 | - self::set('base_url', $baseUrl); |
|
| 155 | - } |
|
| 156 | - self::$config['base_url'] = rtrim(self::$config['base_url'], '/') .'/'; |
|
| 157 | - } |
|
| 158 | - } |
|
| 131 | + if (isset($_SERVER['SERVER_ADDR'])) { |
|
| 132 | + $baseUrl = $_SERVER['SERVER_ADDR']; |
|
| 133 | + //check if the server is running under IPv6 |
|
| 134 | + if (strpos($_SERVER['SERVER_ADDR'], ':') !== FALSE) { |
|
| 135 | + $baseUrl = '[' . $_SERVER['SERVER_ADDR'] . ']'; |
|
| 136 | + } |
|
| 137 | + $serverPort = 80; |
|
| 138 | + if (isset($_SERVER['SERVER_PORT'])) { |
|
| 139 | + $serverPort = $_SERVER['SERVER_PORT']; |
|
| 140 | + } |
|
| 141 | + $port = ''; |
|
| 142 | + if ($serverPort && ((is_https() && $serverPort != 443) || (!is_https() && $serverPort != 80))) { |
|
| 143 | + $port = ':' . $serverPort; |
|
| 144 | + } |
|
| 145 | + $baseUrl = $protocol . $baseUrl . $port . substr( |
|
| 146 | + $_SERVER['SCRIPT_NAME'], |
|
| 147 | + 0, |
|
| 148 | + strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME'])) |
|
| 149 | + ); |
|
| 150 | + } else{ |
|
| 151 | + $logger->warning('Can not determine the application base URL automatically, use http://localhost as default'); |
|
| 152 | + $baseUrl = 'http://localhost/'; |
|
| 153 | + } |
|
| 154 | + self::set('base_url', $baseUrl); |
|
| 155 | + } |
|
| 156 | + self::$config['base_url'] = rtrim(self::$config['base_url'], '/') .'/'; |
|
| 157 | + } |
|
| 158 | + } |
|
@@ -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 |
@@ -1,732 +1,732 @@ |
||
| 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 extends BaseClass |
|
| 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 extends BaseClass |
|
| 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 | - /** |
|
| 78 | - * __construct |
|
| 79 | - * |
|
| 80 | - * Resets the class properties. |
|
| 81 | - */ |
|
| 82 | - public function __construct() |
|
| 83 | - { |
|
| 84 | - parent::__construct(); |
|
| 85 | - $this->reset(); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * reset |
|
| 90 | - * |
|
| 91 | - * Resets all properties to initial state. |
|
| 92 | - * |
|
| 93 | - * @return self |
|
| 94 | - */ |
|
| 95 | - public function reset() |
|
| 96 | - { |
|
| 97 | - $this->_to = array(); |
|
| 98 | - $this->_headers = array(); |
|
| 99 | - $this->_subject = null; |
|
| 100 | - $this->_message = null; |
|
| 101 | - $this->_wrap = 78; |
|
| 102 | - $this->_params = null; |
|
| 103 | - $this->_attachments = array(); |
|
| 104 | - $this->_uid = $this->getUniqueId(); |
|
| 105 | - return $this; |
|
| 106 | - } |
|
| 77 | + /** |
|
| 78 | + * __construct |
|
| 79 | + * |
|
| 80 | + * Resets the class properties. |
|
| 81 | + */ |
|
| 82 | + public function __construct() |
|
| 83 | + { |
|
| 84 | + parent::__construct(); |
|
| 85 | + $this->reset(); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * reset |
|
| 90 | + * |
|
| 91 | + * Resets all properties to initial state. |
|
| 92 | + * |
|
| 93 | + * @return self |
|
| 94 | + */ |
|
| 95 | + public function reset() |
|
| 96 | + { |
|
| 97 | + $this->_to = array(); |
|
| 98 | + $this->_headers = array(); |
|
| 99 | + $this->_subject = null; |
|
| 100 | + $this->_message = null; |
|
| 101 | + $this->_wrap = 78; |
|
| 102 | + $this->_params = null; |
|
| 103 | + $this->_attachments = array(); |
|
| 104 | + $this->_uid = $this->getUniqueId(); |
|
| 105 | + return $this; |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * setFrom |
|
| 110 | - * |
|
| 111 | - * @param string $email The email to send as from. |
|
| 112 | - * @param string $name The name to send as from. |
|
| 113 | - * |
|
| 114 | - * @return self |
|
| 115 | - */ |
|
| 116 | - public function setFrom($email, $name = null) |
|
| 117 | - { |
|
| 118 | - $this->addMailHeader('From', (string) $email, (string) $name); |
|
| 119 | - return $this; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * setTo |
|
| 124 | - * |
|
| 125 | - * @param string $email The email address to send to. |
|
| 126 | - * @param string $name The name of the person to send to. |
|
| 127 | - * |
|
| 128 | - * @return self |
|
| 129 | - */ |
|
| 130 | - public function setTo($email, $name = null) |
|
| 131 | - { |
|
| 132 | - $this->_to[] = $this->formatHeader((string) $email, (string) $name); |
|
| 133 | - return $this; |
|
| 134 | - } |
|
| 108 | + /** |
|
| 109 | + * setFrom |
|
| 110 | + * |
|
| 111 | + * @param string $email The email to send as from. |
|
| 112 | + * @param string $name The name to send as from. |
|
| 113 | + * |
|
| 114 | + * @return self |
|
| 115 | + */ |
|
| 116 | + public function setFrom($email, $name = null) |
|
| 117 | + { |
|
| 118 | + $this->addMailHeader('From', (string) $email, (string) $name); |
|
| 119 | + return $this; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * setTo |
|
| 124 | + * |
|
| 125 | + * @param string $email The email address to send to. |
|
| 126 | + * @param string $name The name of the person to send to. |
|
| 127 | + * |
|
| 128 | + * @return self |
|
| 129 | + */ |
|
| 130 | + public function setTo($email, $name = null) |
|
| 131 | + { |
|
| 132 | + $this->_to[] = $this->formatHeader((string) $email, (string) $name); |
|
| 133 | + return $this; |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - /** |
|
| 137 | - * Set destination using array |
|
| 138 | - * @params array $emails the list of recipient. This is an associative array name => email |
|
| 139 | - * @example array('John Doe' => '[email protected]') |
|
| 140 | - * @return Object the current instance |
|
| 141 | - */ |
|
| 142 | - public function setTos(array $emails) |
|
| 143 | - { |
|
| 144 | - foreach ($emails as $name => $email) { |
|
| 145 | - if(is_numeric($name)){ |
|
| 146 | - $this->setTo($email); |
|
| 147 | - } else{ |
|
| 148 | - $this->setTo($email, $name); |
|
| 149 | - } |
|
| 150 | - } |
|
| 151 | - return $this; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * getTo |
|
| 157 | - * |
|
| 158 | - * Return an array of formatted To addresses. |
|
| 159 | - * |
|
| 160 | - * @return array |
|
| 161 | - */ |
|
| 162 | - public function getTo() |
|
| 163 | - { |
|
| 164 | - return $this->_to; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * setCc |
|
| 170 | - * |
|
| 171 | - * @param array $pairs An array of name => email pairs. |
|
| 172 | - * |
|
| 173 | - * @return self |
|
| 174 | - */ |
|
| 175 | - public function setCc(array $pairs) |
|
| 176 | - { |
|
| 177 | - return $this->addMailHeaders('Cc', $pairs); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * setBcc |
|
| 182 | - * |
|
| 183 | - * @param array $pairs An array of name => email pairs. |
|
| 184 | - * |
|
| 185 | - * @return self |
|
| 186 | - */ |
|
| 187 | - public function setBcc(array $pairs) |
|
| 188 | - { |
|
| 189 | - return $this->addMailHeaders('Bcc', $pairs); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * setReplyTo |
|
| 194 | - * |
|
| 195 | - * @param string $email |
|
| 196 | - * @param string $name |
|
| 197 | - * |
|
| 198 | - * @return self |
|
| 199 | - */ |
|
| 200 | - public function setReplyTo($email, $name = null) |
|
| 201 | - { |
|
| 202 | - return $this->addMailHeader('Reply-To', $email, $name); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * setHtml |
|
| 207 | - * |
|
| 208 | - * @return self |
|
| 209 | - */ |
|
| 210 | - public function setHtml() |
|
| 211 | - { |
|
| 212 | - $this->addGenericHeader( |
|
| 213 | - 'Content-Type', 'text/html; charset="utf-8"' |
|
| 214 | - ); |
|
| 215 | - return $this; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * setSubject |
|
| 220 | - * |
|
| 221 | - * @param string $subject The email subject |
|
| 222 | - * |
|
| 223 | - * @return self |
|
| 224 | - */ |
|
| 225 | - public function setSubject($subject) |
|
| 226 | - { |
|
| 227 | - $this->_subject = $this->encodeUtf8( |
|
| 228 | - $this->filterOther((string) $subject) |
|
| 229 | - ); |
|
| 230 | - return $this; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * getSubject function. |
|
| 235 | - * |
|
| 236 | - * @return string |
|
| 237 | - */ |
|
| 238 | - public function getSubject() |
|
| 239 | - { |
|
| 240 | - return $this->_subject; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * setMessage |
|
| 245 | - * |
|
| 246 | - * @param string $message The message to send. |
|
| 247 | - * |
|
| 248 | - * @return self |
|
| 249 | - */ |
|
| 250 | - public function setMessage($message) |
|
| 251 | - { |
|
| 252 | - $this->_message = str_replace("\n.", "\n..", (string) $message); |
|
| 253 | - return $this; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * getMessage |
|
| 258 | - * |
|
| 259 | - * @return string |
|
| 260 | - */ |
|
| 261 | - public function getMessage() |
|
| 262 | - { |
|
| 263 | - return $this->_message; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * addAttachment |
|
| 268 | - * |
|
| 269 | - * @param string $path The file path to the attachment. |
|
| 270 | - * @param string $filename The filename of the attachment when emailed. |
|
| 271 | - * @param string $data |
|
| 272 | - * |
|
| 273 | - * @return self |
|
| 274 | - */ |
|
| 275 | - public function addAttachment($path, $filename = null, $data = null) |
|
| 276 | - { |
|
| 277 | - if (!file_exists($path)) { |
|
| 278 | - show_error('The file [' . $path . '] does not exists.'); |
|
| 279 | - } |
|
| 280 | - $filename = empty($filename) ? basename($path) : $filename; |
|
| 281 | - $filename = $this->encodeUtf8($this->filterOther((string) $filename)); |
|
| 282 | - $data = empty($data) ? $this->getAttachmentData($path) : $data; |
|
| 283 | - $this->_attachments[] = array( |
|
| 284 | - 'path' => $path, |
|
| 285 | - 'file' => $filename, |
|
| 286 | - 'data' => chunk_split(base64_encode($data)) |
|
| 287 | - ); |
|
| 288 | - return $this; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * getAttachmentData |
|
| 293 | - * |
|
| 294 | - * @param string $path The path to the attachment file. |
|
| 295 | - * |
|
| 296 | - * @return string |
|
| 297 | - */ |
|
| 298 | - public function getAttachmentData($path) |
|
| 299 | - { |
|
| 300 | - if (!file_exists($path)) { |
|
| 301 | - show_error('The file [' . $path . '] does not exists.'); |
|
| 302 | - } |
|
| 303 | - $filesize = filesize($path); |
|
| 304 | - $handle = fopen($path, "r"); |
|
| 305 | - $attachment = null; |
|
| 306 | - if (is_resource($handle)) { |
|
| 307 | - $attachment = fread($handle, $filesize); |
|
| 308 | - fclose($handle); |
|
| 309 | - } |
|
| 310 | - return $attachment; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * addMailHeader |
|
| 315 | - * |
|
| 316 | - * @param string $header The header to add. |
|
| 317 | - * @param string $email The email to add. |
|
| 318 | - * @param string $name The name to add. |
|
| 319 | - * |
|
| 320 | - * @return self |
|
| 321 | - */ |
|
| 322 | - public function addMailHeader($header, $email, $name = null) |
|
| 323 | - { |
|
| 324 | - $address = $this->formatHeader((string) $email, (string) $name); |
|
| 325 | - $this->_headers[] = sprintf('%s: %s', (string) $header, $address); |
|
| 326 | - return $this; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * addMailHeaders |
|
| 331 | - * |
|
| 332 | - * @param string $header The header to add. |
|
| 333 | - * @param array $pairs An array of name => email pairs. |
|
| 334 | - * |
|
| 335 | - * @return self |
|
| 336 | - */ |
|
| 337 | - public function addMailHeaders($header, array $pairs) |
|
| 338 | - { |
|
| 339 | - if (count($pairs) === 0) { |
|
| 340 | - show_error('You must pass at least one name => email pair.'); |
|
| 341 | - } |
|
| 342 | - $addresses = array(); |
|
| 343 | - foreach ($pairs as $name => $email) { |
|
| 344 | - $name = is_numeric($name) ? null : $name; |
|
| 345 | - $addresses[] = $this->formatHeader($email, $name); |
|
| 346 | - } |
|
| 347 | - $this->addGenericHeader($header, implode(',', $addresses)); |
|
| 348 | - return $this; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - /** |
|
| 352 | - * addGenericHeader |
|
| 353 | - * |
|
| 354 | - * @param string $name The generic header to add. |
|
| 355 | - * @param mixed $value The value of the header. |
|
| 356 | - * |
|
| 357 | - * @return self |
|
| 358 | - */ |
|
| 359 | - public function addGenericHeader($name, $value) |
|
| 360 | - { |
|
| 361 | - $this->_headers[] = sprintf( |
|
| 362 | - '%s: %s', |
|
| 363 | - (string) $name, |
|
| 364 | - (string) $value |
|
| 365 | - ); |
|
| 366 | - return $this; |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * getHeaders |
|
| 371 | - * |
|
| 372 | - * Return the headers registered so far as an array. |
|
| 373 | - * |
|
| 374 | - * @return array |
|
| 375 | - */ |
|
| 376 | - public function getHeaders() |
|
| 377 | - { |
|
| 378 | - return $this->_headers; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * setAdditionalParameters |
|
| 383 | - * |
|
| 384 | - * Such as "[email protected] |
|
| 385 | - * |
|
| 386 | - * @param string $additionalParameters The addition mail parameter. |
|
| 387 | - * |
|
| 388 | - * @return self |
|
| 389 | - */ |
|
| 390 | - public function setParameters($additionalParameters) |
|
| 391 | - { |
|
| 392 | - $this->_params = (string) $additionalParameters; |
|
| 393 | - return $this; |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * getAdditionalParameters |
|
| 398 | - * |
|
| 399 | - * @return string |
|
| 400 | - */ |
|
| 401 | - public function getParameters() |
|
| 402 | - { |
|
| 403 | - return $this->_params; |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * setWrap |
|
| 408 | - * |
|
| 409 | - * @param int $wrap The number of characters at which the message will wrap. |
|
| 410 | - * |
|
| 411 | - * @return self |
|
| 412 | - */ |
|
| 413 | - public function setWrap($wrap = 78) |
|
| 414 | - { |
|
| 415 | - $wrap = (int) $wrap; |
|
| 416 | - if ($wrap < 1) { |
|
| 417 | - $wrap = 78; |
|
| 418 | - } |
|
| 419 | - $this->_wrap = $wrap; |
|
| 420 | - return $this; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * getWrap |
|
| 425 | - * |
|
| 426 | - * @return int |
|
| 427 | - */ |
|
| 428 | - public function getWrap() |
|
| 429 | - { |
|
| 430 | - return $this->_wrap; |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - /** |
|
| 434 | - * hasAttachments |
|
| 435 | - * |
|
| 436 | - * Checks if the email has any registered attachments. |
|
| 437 | - * |
|
| 438 | - * @return bool |
|
| 439 | - */ |
|
| 440 | - public function hasAttachments() |
|
| 441 | - { |
|
| 442 | - return !empty($this->_attachments); |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - /** |
|
| 446 | - * assembleAttachment |
|
| 447 | - * |
|
| 448 | - * @return string |
|
| 449 | - */ |
|
| 450 | - public function assembleAttachmentHeaders() |
|
| 451 | - { |
|
| 452 | - $head = array(); |
|
| 453 | - $head[] = "MIME-Version: 1.0"; |
|
| 454 | - $head[] = "Content-Type: multipart/mixed; boundary=\"{$this->_uid}\""; |
|
| 455 | - |
|
| 456 | - return join(PHP_EOL, $head); |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - /** |
|
| 460 | - * assembleAttachmentBody |
|
| 461 | - * |
|
| 462 | - * @return string |
|
| 463 | - */ |
|
| 464 | - public function assembleAttachmentBody() |
|
| 465 | - { |
|
| 466 | - $body = array(); |
|
| 467 | - $body[] = "This is a multi-part message in MIME format."; |
|
| 468 | - $body[] = "--{$this->_uid}"; |
|
| 469 | - $body[] = "Content-Type: text/html; charset=\"utf-8\""; |
|
| 470 | - $body[] = "Content-Transfer-Encoding: quoted-printable"; |
|
| 471 | - $body[] = ""; |
|
| 472 | - $body[] = quoted_printable_encode($this->_message); |
|
| 473 | - $body[] = ""; |
|
| 474 | - $body[] = "--{$this->_uid}"; |
|
| 475 | - |
|
| 476 | - foreach ($this->_attachments as $attachment) { |
|
| 477 | - $body[] = $this->getAttachmentMimeTemplate($attachment); |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - return implode(PHP_EOL, $body) . '--'; |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - /** |
|
| 484 | - * getAttachmentMimeTemplate |
|
| 485 | - * |
|
| 486 | - * @param array $attachment An array containing 'file' and 'data' keys. |
|
| 487 | - * |
|
| 488 | - * @return string |
|
| 489 | - */ |
|
| 490 | - public function getAttachmentMimeTemplate($attachment) |
|
| 491 | - { |
|
| 492 | - $file = $attachment['file']; |
|
| 493 | - $data = $attachment['data']; |
|
| 494 | - |
|
| 495 | - $head = array(); |
|
| 496 | - $head[] = "Content-Type: application/octet-stream; name=\"{$file}\""; |
|
| 497 | - $head[] = "Content-Transfer-Encoding: base64"; |
|
| 498 | - $head[] = "Content-Disposition: attachment; filename=\"{$file}\""; |
|
| 499 | - $head[] = ""; |
|
| 500 | - $head[] = $data; |
|
| 501 | - $head[] = ""; |
|
| 502 | - $head[] = "--{$this->_uid}"; |
|
| 503 | - |
|
| 504 | - return implode(PHP_EOL, $head); |
|
| 505 | - } |
|
| 506 | - |
|
| 507 | - /** |
|
| 508 | - * send the email |
|
| 509 | - * |
|
| 510 | - * @return boolean |
|
| 511 | - */ |
|
| 512 | - public function send() |
|
| 513 | - { |
|
| 514 | - $to = $this->getToForSend(); |
|
| 515 | - $headers = $this->getHeadersForSend(); |
|
| 516 | - |
|
| 517 | - if (empty($to)) { |
|
| 518 | - show_error('Unable to send, no To address has been set.'); |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - if ($this->hasAttachments()) { |
|
| 522 | - $message = $this->assembleAttachmentBody(); |
|
| 523 | - $headers .= PHP_EOL . $this->assembleAttachmentHeaders(); |
|
| 524 | - } else { |
|
| 525 | - $message = $this->getWrapMessage(); |
|
| 526 | - } |
|
| 527 | - $this->logger->info('Sending new mail, the information are listed below: destination: ' . $to . ', headers: ' . $headers . ', message: ' . $message); |
|
| 528 | - return mail($to, $this->_subject, $message, $headers, $this->_params); |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - /** |
|
| 532 | - * debug |
|
| 533 | - * |
|
| 534 | - * @return string |
|
| 535 | - */ |
|
| 536 | - public function debug() |
|
| 537 | - { |
|
| 538 | - return '<pre>' . print_r($this, true) . '</pre>'; |
|
| 539 | - } |
|
| 540 | - |
|
| 541 | - /** |
|
| 542 | - * magic __toString function |
|
| 543 | - * |
|
| 544 | - * @return string |
|
| 545 | - */ |
|
| 546 | - public function __toString() |
|
| 547 | - { |
|
| 548 | - return print_r($this, true); |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - /** |
|
| 552 | - * formatHeader |
|
| 553 | - * |
|
| 554 | - * Formats a display address for emails according to RFC2822 e.g. |
|
| 555 | - * Name <[email protected]> |
|
| 556 | - * |
|
| 557 | - * @param string $email The email address. |
|
| 558 | - * @param string $name The display name. |
|
| 559 | - * |
|
| 560 | - * @return string |
|
| 561 | - */ |
|
| 562 | - public function formatHeader($email, $name = null) |
|
| 563 | - { |
|
| 564 | - $email = $this->filterEmail((string) $email); |
|
| 565 | - if (empty($name)) { |
|
| 566 | - return $email; |
|
| 567 | - } |
|
| 568 | - $name = $this->encodeUtf8($this->filterName((string) $name)); |
|
| 569 | - return sprintf('"%s" <%s>', $name, $email); |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - /** |
|
| 573 | - * encodeUtf8 |
|
| 574 | - * |
|
| 575 | - * @param string $value The value to encode. |
|
| 576 | - * |
|
| 577 | - * @return string |
|
| 578 | - */ |
|
| 579 | - public function encodeUtf8($value) |
|
| 580 | - { |
|
| 581 | - $value = trim($value); |
|
| 582 | - if (preg_match('/(\s)/', $value)) { |
|
| 583 | - return $this->encodeUtf8Words($value); |
|
| 584 | - } |
|
| 585 | - return $this->encodeUtf8Word($value); |
|
| 586 | - } |
|
| 587 | - |
|
| 588 | - /** |
|
| 589 | - * encodeUtf8Word |
|
| 590 | - * |
|
| 591 | - * @param string $value The word to encode. |
|
| 592 | - * |
|
| 593 | - * @return string |
|
| 594 | - */ |
|
| 595 | - public function encodeUtf8Word($value) |
|
| 596 | - { |
|
| 597 | - return sprintf('=?UTF-8?B?%s?=', base64_encode($value)); |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - /** |
|
| 601 | - * encodeUtf8Words |
|
| 602 | - * |
|
| 603 | - * @param string $value The words to encode. |
|
| 604 | - * |
|
| 605 | - * @return string |
|
| 606 | - */ |
|
| 607 | - public function encodeUtf8Words($value) |
|
| 608 | - { |
|
| 609 | - $words = explode(' ', $value); |
|
| 610 | - $encoded = array(); |
|
| 611 | - foreach ($words as $word) { |
|
| 612 | - $encoded[] = $this->encodeUtf8Word($word); |
|
| 613 | - } |
|
| 614 | - return join($this->encodeUtf8Word(' '), $encoded); |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - /** |
|
| 618 | - * filterEmail |
|
| 619 | - * |
|
| 620 | - * Removes any carriage return, line feed, tab, double quote, comma |
|
| 621 | - * and angle bracket characters before sanitizing the email address. |
|
| 622 | - * |
|
| 623 | - * @param string $email The email to filter. |
|
| 624 | - * |
|
| 625 | - * @return string |
|
| 626 | - */ |
|
| 627 | - public function filterEmail($email) |
|
| 628 | - { |
|
| 629 | - $rule = array( |
|
| 630 | - "\r" => '', |
|
| 631 | - "\n" => '', |
|
| 632 | - "\t" => '', |
|
| 633 | - '"' => '', |
|
| 634 | - ',' => '', |
|
| 635 | - '<' => '', |
|
| 636 | - '>' => '' |
|
| 637 | - ); |
|
| 638 | - $email = strtr($email, $rule); |
|
| 639 | - $email = filter_var($email, FILTER_SANITIZE_EMAIL); |
|
| 640 | - return $email; |
|
| 641 | - } |
|
| 642 | - |
|
| 643 | - /** |
|
| 644 | - * filterName |
|
| 645 | - * |
|
| 646 | - * Removes any carriage return, line feed or tab characters. Replaces |
|
| 647 | - * double quotes with single quotes and angle brackets with square |
|
| 648 | - * brackets, before sanitizing the string and stripping out html tags. |
|
| 649 | - * |
|
| 650 | - * @param string $name The name to filter. |
|
| 651 | - * |
|
| 652 | - * @return string |
|
| 653 | - */ |
|
| 654 | - public function filterName($name) |
|
| 655 | - { |
|
| 656 | - $rule = array( |
|
| 657 | - "\r" => '', |
|
| 658 | - "\n" => '', |
|
| 659 | - "\t" => '', |
|
| 660 | - '"' => "'", |
|
| 661 | - '<' => '[', |
|
| 662 | - '>' => ']', |
|
| 663 | - ); |
|
| 664 | - $filtered = filter_var( |
|
| 665 | - $name, |
|
| 666 | - FILTER_SANITIZE_STRING, |
|
| 667 | - FILTER_FLAG_NO_ENCODE_QUOTES |
|
| 668 | - ); |
|
| 669 | - return trim(strtr($filtered, $rule)); |
|
| 670 | - } |
|
| 671 | - |
|
| 672 | - /** |
|
| 673 | - * filterOther |
|
| 674 | - * |
|
| 675 | - * Removes ASCII control characters including any carriage return, line |
|
| 676 | - * feed or tab characters. |
|
| 677 | - * |
|
| 678 | - * @param string $data The data to filter. |
|
| 679 | - * |
|
| 680 | - * @return string |
|
| 681 | - */ |
|
| 682 | - public function filterOther($data) |
|
| 683 | - { |
|
| 684 | - return filter_var($data, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW); |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - /** |
|
| 688 | - * getHeadersForSend |
|
| 689 | - * |
|
| 690 | - * @return string |
|
| 691 | - */ |
|
| 692 | - public function getHeadersForSend() |
|
| 693 | - { |
|
| 694 | - if (empty($this->_headers)) { |
|
| 695 | - return ''; |
|
| 696 | - } |
|
| 697 | - return join(PHP_EOL, $this->_headers); |
|
| 698 | - } |
|
| 699 | - |
|
| 700 | - /** |
|
| 701 | - * getToForSend |
|
| 702 | - * |
|
| 703 | - * @return string |
|
| 704 | - */ |
|
| 705 | - public function getToForSend() |
|
| 706 | - { |
|
| 707 | - if (empty($this->_to)) { |
|
| 708 | - return ''; |
|
| 709 | - } |
|
| 710 | - return join(', ', $this->_to); |
|
| 711 | - } |
|
| 712 | - |
|
| 713 | - /** |
|
| 714 | - * getUniqueId |
|
| 715 | - * |
|
| 716 | - * @return string |
|
| 717 | - */ |
|
| 718 | - public function getUniqueId() |
|
| 719 | - { |
|
| 720 | - return md5(uniqid(time())); |
|
| 721 | - } |
|
| 722 | - |
|
| 723 | - /** |
|
| 724 | - * getWrapMessage |
|
| 725 | - * |
|
| 726 | - * @return string |
|
| 727 | - */ |
|
| 728 | - public function getWrapMessage() |
|
| 729 | - { |
|
| 730 | - return wordwrap($this->_message, $this->_wrap); |
|
| 731 | - } |
|
| 732 | - } |
|
| 136 | + /** |
|
| 137 | + * Set destination using array |
|
| 138 | + * @params array $emails the list of recipient. This is an associative array name => email |
|
| 139 | + * @example array('John Doe' => '[email protected]') |
|
| 140 | + * @return Object the current instance |
|
| 141 | + */ |
|
| 142 | + public function setTos(array $emails) |
|
| 143 | + { |
|
| 144 | + foreach ($emails as $name => $email) { |
|
| 145 | + if(is_numeric($name)){ |
|
| 146 | + $this->setTo($email); |
|
| 147 | + } else{ |
|
| 148 | + $this->setTo($email, $name); |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | + return $this; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * getTo |
|
| 157 | + * |
|
| 158 | + * Return an array of formatted To addresses. |
|
| 159 | + * |
|
| 160 | + * @return array |
|
| 161 | + */ |
|
| 162 | + public function getTo() |
|
| 163 | + { |
|
| 164 | + return $this->_to; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * setCc |
|
| 170 | + * |
|
| 171 | + * @param array $pairs An array of name => email pairs. |
|
| 172 | + * |
|
| 173 | + * @return self |
|
| 174 | + */ |
|
| 175 | + public function setCc(array $pairs) |
|
| 176 | + { |
|
| 177 | + return $this->addMailHeaders('Cc', $pairs); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * setBcc |
|
| 182 | + * |
|
| 183 | + * @param array $pairs An array of name => email pairs. |
|
| 184 | + * |
|
| 185 | + * @return self |
|
| 186 | + */ |
|
| 187 | + public function setBcc(array $pairs) |
|
| 188 | + { |
|
| 189 | + return $this->addMailHeaders('Bcc', $pairs); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * setReplyTo |
|
| 194 | + * |
|
| 195 | + * @param string $email |
|
| 196 | + * @param string $name |
|
| 197 | + * |
|
| 198 | + * @return self |
|
| 199 | + */ |
|
| 200 | + public function setReplyTo($email, $name = null) |
|
| 201 | + { |
|
| 202 | + return $this->addMailHeader('Reply-To', $email, $name); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * setHtml |
|
| 207 | + * |
|
| 208 | + * @return self |
|
| 209 | + */ |
|
| 210 | + public function setHtml() |
|
| 211 | + { |
|
| 212 | + $this->addGenericHeader( |
|
| 213 | + 'Content-Type', 'text/html; charset="utf-8"' |
|
| 214 | + ); |
|
| 215 | + return $this; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * setSubject |
|
| 220 | + * |
|
| 221 | + * @param string $subject The email subject |
|
| 222 | + * |
|
| 223 | + * @return self |
|
| 224 | + */ |
|
| 225 | + public function setSubject($subject) |
|
| 226 | + { |
|
| 227 | + $this->_subject = $this->encodeUtf8( |
|
| 228 | + $this->filterOther((string) $subject) |
|
| 229 | + ); |
|
| 230 | + return $this; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * getSubject function. |
|
| 235 | + * |
|
| 236 | + * @return string |
|
| 237 | + */ |
|
| 238 | + public function getSubject() |
|
| 239 | + { |
|
| 240 | + return $this->_subject; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * setMessage |
|
| 245 | + * |
|
| 246 | + * @param string $message The message to send. |
|
| 247 | + * |
|
| 248 | + * @return self |
|
| 249 | + */ |
|
| 250 | + public function setMessage($message) |
|
| 251 | + { |
|
| 252 | + $this->_message = str_replace("\n.", "\n..", (string) $message); |
|
| 253 | + return $this; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * getMessage |
|
| 258 | + * |
|
| 259 | + * @return string |
|
| 260 | + */ |
|
| 261 | + public function getMessage() |
|
| 262 | + { |
|
| 263 | + return $this->_message; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * addAttachment |
|
| 268 | + * |
|
| 269 | + * @param string $path The file path to the attachment. |
|
| 270 | + * @param string $filename The filename of the attachment when emailed. |
|
| 271 | + * @param string $data |
|
| 272 | + * |
|
| 273 | + * @return self |
|
| 274 | + */ |
|
| 275 | + public function addAttachment($path, $filename = null, $data = null) |
|
| 276 | + { |
|
| 277 | + if (!file_exists($path)) { |
|
| 278 | + show_error('The file [' . $path . '] does not exists.'); |
|
| 279 | + } |
|
| 280 | + $filename = empty($filename) ? basename($path) : $filename; |
|
| 281 | + $filename = $this->encodeUtf8($this->filterOther((string) $filename)); |
|
| 282 | + $data = empty($data) ? $this->getAttachmentData($path) : $data; |
|
| 283 | + $this->_attachments[] = array( |
|
| 284 | + 'path' => $path, |
|
| 285 | + 'file' => $filename, |
|
| 286 | + 'data' => chunk_split(base64_encode($data)) |
|
| 287 | + ); |
|
| 288 | + return $this; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * getAttachmentData |
|
| 293 | + * |
|
| 294 | + * @param string $path The path to the attachment file. |
|
| 295 | + * |
|
| 296 | + * @return string |
|
| 297 | + */ |
|
| 298 | + public function getAttachmentData($path) |
|
| 299 | + { |
|
| 300 | + if (!file_exists($path)) { |
|
| 301 | + show_error('The file [' . $path . '] does not exists.'); |
|
| 302 | + } |
|
| 303 | + $filesize = filesize($path); |
|
| 304 | + $handle = fopen($path, "r"); |
|
| 305 | + $attachment = null; |
|
| 306 | + if (is_resource($handle)) { |
|
| 307 | + $attachment = fread($handle, $filesize); |
|
| 308 | + fclose($handle); |
|
| 309 | + } |
|
| 310 | + return $attachment; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * addMailHeader |
|
| 315 | + * |
|
| 316 | + * @param string $header The header to add. |
|
| 317 | + * @param string $email The email to add. |
|
| 318 | + * @param string $name The name to add. |
|
| 319 | + * |
|
| 320 | + * @return self |
|
| 321 | + */ |
|
| 322 | + public function addMailHeader($header, $email, $name = null) |
|
| 323 | + { |
|
| 324 | + $address = $this->formatHeader((string) $email, (string) $name); |
|
| 325 | + $this->_headers[] = sprintf('%s: %s', (string) $header, $address); |
|
| 326 | + return $this; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * addMailHeaders |
|
| 331 | + * |
|
| 332 | + * @param string $header The header to add. |
|
| 333 | + * @param array $pairs An array of name => email pairs. |
|
| 334 | + * |
|
| 335 | + * @return self |
|
| 336 | + */ |
|
| 337 | + public function addMailHeaders($header, array $pairs) |
|
| 338 | + { |
|
| 339 | + if (count($pairs) === 0) { |
|
| 340 | + show_error('You must pass at least one name => email pair.'); |
|
| 341 | + } |
|
| 342 | + $addresses = array(); |
|
| 343 | + foreach ($pairs as $name => $email) { |
|
| 344 | + $name = is_numeric($name) ? null : $name; |
|
| 345 | + $addresses[] = $this->formatHeader($email, $name); |
|
| 346 | + } |
|
| 347 | + $this->addGenericHeader($header, implode(',', $addresses)); |
|
| 348 | + return $this; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + /** |
|
| 352 | + * addGenericHeader |
|
| 353 | + * |
|
| 354 | + * @param string $name The generic header to add. |
|
| 355 | + * @param mixed $value The value of the header. |
|
| 356 | + * |
|
| 357 | + * @return self |
|
| 358 | + */ |
|
| 359 | + public function addGenericHeader($name, $value) |
|
| 360 | + { |
|
| 361 | + $this->_headers[] = sprintf( |
|
| 362 | + '%s: %s', |
|
| 363 | + (string) $name, |
|
| 364 | + (string) $value |
|
| 365 | + ); |
|
| 366 | + return $this; |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * getHeaders |
|
| 371 | + * |
|
| 372 | + * Return the headers registered so far as an array. |
|
| 373 | + * |
|
| 374 | + * @return array |
|
| 375 | + */ |
|
| 376 | + public function getHeaders() |
|
| 377 | + { |
|
| 378 | + return $this->_headers; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * setAdditionalParameters |
|
| 383 | + * |
|
| 384 | + * Such as "[email protected] |
|
| 385 | + * |
|
| 386 | + * @param string $additionalParameters The addition mail parameter. |
|
| 387 | + * |
|
| 388 | + * @return self |
|
| 389 | + */ |
|
| 390 | + public function setParameters($additionalParameters) |
|
| 391 | + { |
|
| 392 | + $this->_params = (string) $additionalParameters; |
|
| 393 | + return $this; |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * getAdditionalParameters |
|
| 398 | + * |
|
| 399 | + * @return string |
|
| 400 | + */ |
|
| 401 | + public function getParameters() |
|
| 402 | + { |
|
| 403 | + return $this->_params; |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + /** |
|
| 407 | + * setWrap |
|
| 408 | + * |
|
| 409 | + * @param int $wrap The number of characters at which the message will wrap. |
|
| 410 | + * |
|
| 411 | + * @return self |
|
| 412 | + */ |
|
| 413 | + public function setWrap($wrap = 78) |
|
| 414 | + { |
|
| 415 | + $wrap = (int) $wrap; |
|
| 416 | + if ($wrap < 1) { |
|
| 417 | + $wrap = 78; |
|
| 418 | + } |
|
| 419 | + $this->_wrap = $wrap; |
|
| 420 | + return $this; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * getWrap |
|
| 425 | + * |
|
| 426 | + * @return int |
|
| 427 | + */ |
|
| 428 | + public function getWrap() |
|
| 429 | + { |
|
| 430 | + return $this->_wrap; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + /** |
|
| 434 | + * hasAttachments |
|
| 435 | + * |
|
| 436 | + * Checks if the email has any registered attachments. |
|
| 437 | + * |
|
| 438 | + * @return bool |
|
| 439 | + */ |
|
| 440 | + public function hasAttachments() |
|
| 441 | + { |
|
| 442 | + return !empty($this->_attachments); |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + /** |
|
| 446 | + * assembleAttachment |
|
| 447 | + * |
|
| 448 | + * @return string |
|
| 449 | + */ |
|
| 450 | + public function assembleAttachmentHeaders() |
|
| 451 | + { |
|
| 452 | + $head = array(); |
|
| 453 | + $head[] = "MIME-Version: 1.0"; |
|
| 454 | + $head[] = "Content-Type: multipart/mixed; boundary=\"{$this->_uid}\""; |
|
| 455 | + |
|
| 456 | + return join(PHP_EOL, $head); |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + /** |
|
| 460 | + * assembleAttachmentBody |
|
| 461 | + * |
|
| 462 | + * @return string |
|
| 463 | + */ |
|
| 464 | + public function assembleAttachmentBody() |
|
| 465 | + { |
|
| 466 | + $body = array(); |
|
| 467 | + $body[] = "This is a multi-part message in MIME format."; |
|
| 468 | + $body[] = "--{$this->_uid}"; |
|
| 469 | + $body[] = "Content-Type: text/html; charset=\"utf-8\""; |
|
| 470 | + $body[] = "Content-Transfer-Encoding: quoted-printable"; |
|
| 471 | + $body[] = ""; |
|
| 472 | + $body[] = quoted_printable_encode($this->_message); |
|
| 473 | + $body[] = ""; |
|
| 474 | + $body[] = "--{$this->_uid}"; |
|
| 475 | + |
|
| 476 | + foreach ($this->_attachments as $attachment) { |
|
| 477 | + $body[] = $this->getAttachmentMimeTemplate($attachment); |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + return implode(PHP_EOL, $body) . '--'; |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + /** |
|
| 484 | + * getAttachmentMimeTemplate |
|
| 485 | + * |
|
| 486 | + * @param array $attachment An array containing 'file' and 'data' keys. |
|
| 487 | + * |
|
| 488 | + * @return string |
|
| 489 | + */ |
|
| 490 | + public function getAttachmentMimeTemplate($attachment) |
|
| 491 | + { |
|
| 492 | + $file = $attachment['file']; |
|
| 493 | + $data = $attachment['data']; |
|
| 494 | + |
|
| 495 | + $head = array(); |
|
| 496 | + $head[] = "Content-Type: application/octet-stream; name=\"{$file}\""; |
|
| 497 | + $head[] = "Content-Transfer-Encoding: base64"; |
|
| 498 | + $head[] = "Content-Disposition: attachment; filename=\"{$file}\""; |
|
| 499 | + $head[] = ""; |
|
| 500 | + $head[] = $data; |
|
| 501 | + $head[] = ""; |
|
| 502 | + $head[] = "--{$this->_uid}"; |
|
| 503 | + |
|
| 504 | + return implode(PHP_EOL, $head); |
|
| 505 | + } |
|
| 506 | + |
|
| 507 | + /** |
|
| 508 | + * send the email |
|
| 509 | + * |
|
| 510 | + * @return boolean |
|
| 511 | + */ |
|
| 512 | + public function send() |
|
| 513 | + { |
|
| 514 | + $to = $this->getToForSend(); |
|
| 515 | + $headers = $this->getHeadersForSend(); |
|
| 516 | + |
|
| 517 | + if (empty($to)) { |
|
| 518 | + show_error('Unable to send, no To address has been set.'); |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + if ($this->hasAttachments()) { |
|
| 522 | + $message = $this->assembleAttachmentBody(); |
|
| 523 | + $headers .= PHP_EOL . $this->assembleAttachmentHeaders(); |
|
| 524 | + } else { |
|
| 525 | + $message = $this->getWrapMessage(); |
|
| 526 | + } |
|
| 527 | + $this->logger->info('Sending new mail, the information are listed below: destination: ' . $to . ', headers: ' . $headers . ', message: ' . $message); |
|
| 528 | + return mail($to, $this->_subject, $message, $headers, $this->_params); |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + /** |
|
| 532 | + * debug |
|
| 533 | + * |
|
| 534 | + * @return string |
|
| 535 | + */ |
|
| 536 | + public function debug() |
|
| 537 | + { |
|
| 538 | + return '<pre>' . print_r($this, true) . '</pre>'; |
|
| 539 | + } |
|
| 540 | + |
|
| 541 | + /** |
|
| 542 | + * magic __toString function |
|
| 543 | + * |
|
| 544 | + * @return string |
|
| 545 | + */ |
|
| 546 | + public function __toString() |
|
| 547 | + { |
|
| 548 | + return print_r($this, true); |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + /** |
|
| 552 | + * formatHeader |
|
| 553 | + * |
|
| 554 | + * Formats a display address for emails according to RFC2822 e.g. |
|
| 555 | + * Name <[email protected]> |
|
| 556 | + * |
|
| 557 | + * @param string $email The email address. |
|
| 558 | + * @param string $name The display name. |
|
| 559 | + * |
|
| 560 | + * @return string |
|
| 561 | + */ |
|
| 562 | + public function formatHeader($email, $name = null) |
|
| 563 | + { |
|
| 564 | + $email = $this->filterEmail((string) $email); |
|
| 565 | + if (empty($name)) { |
|
| 566 | + return $email; |
|
| 567 | + } |
|
| 568 | + $name = $this->encodeUtf8($this->filterName((string) $name)); |
|
| 569 | + return sprintf('"%s" <%s>', $name, $email); |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + /** |
|
| 573 | + * encodeUtf8 |
|
| 574 | + * |
|
| 575 | + * @param string $value The value to encode. |
|
| 576 | + * |
|
| 577 | + * @return string |
|
| 578 | + */ |
|
| 579 | + public function encodeUtf8($value) |
|
| 580 | + { |
|
| 581 | + $value = trim($value); |
|
| 582 | + if (preg_match('/(\s)/', $value)) { |
|
| 583 | + return $this->encodeUtf8Words($value); |
|
| 584 | + } |
|
| 585 | + return $this->encodeUtf8Word($value); |
|
| 586 | + } |
|
| 587 | + |
|
| 588 | + /** |
|
| 589 | + * encodeUtf8Word |
|
| 590 | + * |
|
| 591 | + * @param string $value The word to encode. |
|
| 592 | + * |
|
| 593 | + * @return string |
|
| 594 | + */ |
|
| 595 | + public function encodeUtf8Word($value) |
|
| 596 | + { |
|
| 597 | + return sprintf('=?UTF-8?B?%s?=', base64_encode($value)); |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + /** |
|
| 601 | + * encodeUtf8Words |
|
| 602 | + * |
|
| 603 | + * @param string $value The words to encode. |
|
| 604 | + * |
|
| 605 | + * @return string |
|
| 606 | + */ |
|
| 607 | + public function encodeUtf8Words($value) |
|
| 608 | + { |
|
| 609 | + $words = explode(' ', $value); |
|
| 610 | + $encoded = array(); |
|
| 611 | + foreach ($words as $word) { |
|
| 612 | + $encoded[] = $this->encodeUtf8Word($word); |
|
| 613 | + } |
|
| 614 | + return join($this->encodeUtf8Word(' '), $encoded); |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + /** |
|
| 618 | + * filterEmail |
|
| 619 | + * |
|
| 620 | + * Removes any carriage return, line feed, tab, double quote, comma |
|
| 621 | + * and angle bracket characters before sanitizing the email address. |
|
| 622 | + * |
|
| 623 | + * @param string $email The email to filter. |
|
| 624 | + * |
|
| 625 | + * @return string |
|
| 626 | + */ |
|
| 627 | + public function filterEmail($email) |
|
| 628 | + { |
|
| 629 | + $rule = array( |
|
| 630 | + "\r" => '', |
|
| 631 | + "\n" => '', |
|
| 632 | + "\t" => '', |
|
| 633 | + '"' => '', |
|
| 634 | + ',' => '', |
|
| 635 | + '<' => '', |
|
| 636 | + '>' => '' |
|
| 637 | + ); |
|
| 638 | + $email = strtr($email, $rule); |
|
| 639 | + $email = filter_var($email, FILTER_SANITIZE_EMAIL); |
|
| 640 | + return $email; |
|
| 641 | + } |
|
| 642 | + |
|
| 643 | + /** |
|
| 644 | + * filterName |
|
| 645 | + * |
|
| 646 | + * Removes any carriage return, line feed or tab characters. Replaces |
|
| 647 | + * double quotes with single quotes and angle brackets with square |
|
| 648 | + * brackets, before sanitizing the string and stripping out html tags. |
|
| 649 | + * |
|
| 650 | + * @param string $name The name to filter. |
|
| 651 | + * |
|
| 652 | + * @return string |
|
| 653 | + */ |
|
| 654 | + public function filterName($name) |
|
| 655 | + { |
|
| 656 | + $rule = array( |
|
| 657 | + "\r" => '', |
|
| 658 | + "\n" => '', |
|
| 659 | + "\t" => '', |
|
| 660 | + '"' => "'", |
|
| 661 | + '<' => '[', |
|
| 662 | + '>' => ']', |
|
| 663 | + ); |
|
| 664 | + $filtered = filter_var( |
|
| 665 | + $name, |
|
| 666 | + FILTER_SANITIZE_STRING, |
|
| 667 | + FILTER_FLAG_NO_ENCODE_QUOTES |
|
| 668 | + ); |
|
| 669 | + return trim(strtr($filtered, $rule)); |
|
| 670 | + } |
|
| 671 | + |
|
| 672 | + /** |
|
| 673 | + * filterOther |
|
| 674 | + * |
|
| 675 | + * Removes ASCII control characters including any carriage return, line |
|
| 676 | + * feed or tab characters. |
|
| 677 | + * |
|
| 678 | + * @param string $data The data to filter. |
|
| 679 | + * |
|
| 680 | + * @return string |
|
| 681 | + */ |
|
| 682 | + public function filterOther($data) |
|
| 683 | + { |
|
| 684 | + return filter_var($data, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW); |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + /** |
|
| 688 | + * getHeadersForSend |
|
| 689 | + * |
|
| 690 | + * @return string |
|
| 691 | + */ |
|
| 692 | + public function getHeadersForSend() |
|
| 693 | + { |
|
| 694 | + if (empty($this->_headers)) { |
|
| 695 | + return ''; |
|
| 696 | + } |
|
| 697 | + return join(PHP_EOL, $this->_headers); |
|
| 698 | + } |
|
| 699 | + |
|
| 700 | + /** |
|
| 701 | + * getToForSend |
|
| 702 | + * |
|
| 703 | + * @return string |
|
| 704 | + */ |
|
| 705 | + public function getToForSend() |
|
| 706 | + { |
|
| 707 | + if (empty($this->_to)) { |
|
| 708 | + return ''; |
|
| 709 | + } |
|
| 710 | + return join(', ', $this->_to); |
|
| 711 | + } |
|
| 712 | + |
|
| 713 | + /** |
|
| 714 | + * getUniqueId |
|
| 715 | + * |
|
| 716 | + * @return string |
|
| 717 | + */ |
|
| 718 | + public function getUniqueId() |
|
| 719 | + { |
|
| 720 | + return md5(uniqid(time())); |
|
| 721 | + } |
|
| 722 | + |
|
| 723 | + /** |
|
| 724 | + * getWrapMessage |
|
| 725 | + * |
|
| 726 | + * @return string |
|
| 727 | + */ |
|
| 728 | + public function getWrapMessage() |
|
| 729 | + { |
|
| 730 | + return wordwrap($this->_message, $this->_wrap); |
|
| 731 | + } |
|
| 732 | + } |
|