@@ -1,215 +1,215 @@ |
||
| 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 ApcCache implements CacheInterface{ |
|
| 27 | + class ApcCache implements CacheInterface{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The logger instance |
|
| 31 | - * @var Log |
|
| 32 | - */ |
|
| 33 | - private $logger; |
|
| 29 | + /** |
|
| 30 | + * The logger instance |
|
| 31 | + * @var Log |
|
| 32 | + */ |
|
| 33 | + private $logger; |
|
| 34 | 34 | |
| 35 | 35 | |
| 36 | - public function __construct(Log $logger = null){ |
|
| 37 | - if(! $this->isSupported()){ |
|
| 38 | - show_error('The cache for APC[u] driver is not available. Check if APC[u] extension is loaded and enabled.'); |
|
| 39 | - } |
|
| 36 | + public function __construct(Log $logger = null){ |
|
| 37 | + if(! $this->isSupported()){ |
|
| 38 | + show_error('The cache for APC[u] driver is not available. Check if APC[u] extension is loaded and enabled.'); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * instance of the Log class |
|
| 43 | - */ |
|
| 44 | - if(is_object($logger)){ |
|
| 45 | - $this->logger = $logger; |
|
| 46 | - } |
|
| 47 | - else{ |
|
| 48 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 49 | - $this->logger->setLogger('Library::ApcCache'); |
|
| 50 | - } |
|
| 51 | - } |
|
| 41 | + /** |
|
| 42 | + * instance of the Log class |
|
| 43 | + */ |
|
| 44 | + if(is_object($logger)){ |
|
| 45 | + $this->logger = $logger; |
|
| 46 | + } |
|
| 47 | + else{ |
|
| 48 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 49 | + $this->logger->setLogger('Library::ApcCache'); |
|
| 50 | + } |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * This is used to get the cache data using the key |
|
| 55 | - * @param string $key the key to identify the cache data |
|
| 56 | - * @return mixed the cache data if exists else return false |
|
| 57 | - */ |
|
| 58 | - public function get($key){ |
|
| 59 | - $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 60 | - $success = false; |
|
| 61 | - $data = apc_fetch($key, $success); |
|
| 62 | - if($success === false){ |
|
| 63 | - $this->logger->info('No cache found for the key ['. $key .'], return false'); |
|
| 64 | - return false; |
|
| 65 | - } |
|
| 66 | - else{ |
|
| 67 | - $cacheInfo = $this->_getCacheInfo($key); |
|
| 68 | - $expire = time(); |
|
| 69 | - if($cacheInfo){ |
|
| 70 | - $expire = $cacheInfo['creation_time'] + $cacheInfo['ttl']; |
|
| 71 | - } |
|
| 72 | - $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 73 | - return $data; |
|
| 74 | - } |
|
| 75 | - } |
|
| 53 | + /** |
|
| 54 | + * This is used to get the cache data using the key |
|
| 55 | + * @param string $key the key to identify the cache data |
|
| 56 | + * @return mixed the cache data if exists else return false |
|
| 57 | + */ |
|
| 58 | + public function get($key){ |
|
| 59 | + $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 60 | + $success = false; |
|
| 61 | + $data = apc_fetch($key, $success); |
|
| 62 | + if($success === false){ |
|
| 63 | + $this->logger->info('No cache found for the key ['. $key .'], return false'); |
|
| 64 | + return false; |
|
| 65 | + } |
|
| 66 | + else{ |
|
| 67 | + $cacheInfo = $this->_getCacheInfo($key); |
|
| 68 | + $expire = time(); |
|
| 69 | + if($cacheInfo){ |
|
| 70 | + $expire = $cacheInfo['creation_time'] + $cacheInfo['ttl']; |
|
| 71 | + } |
|
| 72 | + $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 73 | + return $data; |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Save data to the cache |
|
| 80 | - * @param string $key the key to identify this cache data |
|
| 81 | - * @param mixed $data the cache data to be saved |
|
| 82 | - * @param integer $ttl the cache life time |
|
| 83 | - * @return boolean true if success otherwise will return false |
|
| 84 | - */ |
|
| 85 | - public function set($key, $data, $ttl = 0){ |
|
| 86 | - $expire = time() + $ttl; |
|
| 87 | - $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 88 | - $result = apc_store($key, $data, $ttl); |
|
| 89 | - if($result === false){ |
|
| 90 | - $this->logger->error('Can not write cache data for the key ['. $key .'], return false'); |
|
| 91 | - return false; |
|
| 92 | - } |
|
| 93 | - else{ |
|
| 94 | - $this->logger->info('Cache data saved for the key ['. $key .']'); |
|
| 95 | - return true; |
|
| 96 | - } |
|
| 97 | - } |
|
| 78 | + /** |
|
| 79 | + * Save data to the cache |
|
| 80 | + * @param string $key the key to identify this cache data |
|
| 81 | + * @param mixed $data the cache data to be saved |
|
| 82 | + * @param integer $ttl the cache life time |
|
| 83 | + * @return boolean true if success otherwise will return false |
|
| 84 | + */ |
|
| 85 | + public function set($key, $data, $ttl = 0){ |
|
| 86 | + $expire = time() + $ttl; |
|
| 87 | + $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 88 | + $result = apc_store($key, $data, $ttl); |
|
| 89 | + if($result === false){ |
|
| 90 | + $this->logger->error('Can not write cache data for the key ['. $key .'], return false'); |
|
| 91 | + return false; |
|
| 92 | + } |
|
| 93 | + else{ |
|
| 94 | + $this->logger->info('Cache data saved for the key ['. $key .']'); |
|
| 95 | + return true; |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * Delete the cache data for given key |
|
| 102 | - * @param string $key the key for cache to be deleted |
|
| 103 | - * @return boolean true if the cache is deleted, false if can't delete |
|
| 104 | - * the cache or the cache with the given key not exist |
|
| 105 | - */ |
|
| 106 | - public function delete($key){ |
|
| 107 | - $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 108 | - $cacheInfo = $this->_getCacheInfo($key); |
|
| 109 | - if($cacheInfo === false){ |
|
| 110 | - $this->logger->info('This cache data does not exists skipping'); |
|
| 111 | - return false; |
|
| 112 | - } |
|
| 113 | - else{ |
|
| 114 | - $this->logger->info('Found cache data for the key [' .$key. '] remove it'); |
|
| 115 | - return apc_delete($key) === true; |
|
| 116 | - } |
|
| 117 | - } |
|
| 100 | + /** |
|
| 101 | + * Delete the cache data for given key |
|
| 102 | + * @param string $key the key for cache to be deleted |
|
| 103 | + * @return boolean true if the cache is deleted, false if can't delete |
|
| 104 | + * the cache or the cache with the given key not exist |
|
| 105 | + */ |
|
| 106 | + public function delete($key){ |
|
| 107 | + $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 108 | + $cacheInfo = $this->_getCacheInfo($key); |
|
| 109 | + if($cacheInfo === false){ |
|
| 110 | + $this->logger->info('This cache data does not exists skipping'); |
|
| 111 | + return false; |
|
| 112 | + } |
|
| 113 | + else{ |
|
| 114 | + $this->logger->info('Found cache data for the key [' .$key. '] remove it'); |
|
| 115 | + return apc_delete($key) === true; |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - /** |
|
| 120 | - * Get the cache information for given key |
|
| 121 | - * @param string $key the key for cache to get the information for |
|
| 122 | - * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 123 | - * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 124 | - * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 125 | - * 'ttl' => the time to live of the cache in second |
|
| 126 | - */ |
|
| 127 | - public function getInfo($key){ |
|
| 128 | - $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 129 | - $cacheInfos = $this->_getCacheInfo($key); |
|
| 130 | - if($cacheInfos){ |
|
| 131 | - $data = array( |
|
| 132 | - 'mtime' => $cacheInfos['creation_time'], |
|
| 133 | - 'expire' => $cacheInfos['creation_time'] + $cacheInfos['ttl'], |
|
| 134 | - 'ttl' => $cacheInfos['ttl'] |
|
| 135 | - ); |
|
| 136 | - return $data; |
|
| 137 | - } |
|
| 138 | - else{ |
|
| 139 | - $this->logger->info('This cache does not exists skipping'); |
|
| 140 | - return false; |
|
| 141 | - } |
|
| 142 | - } |
|
| 119 | + /** |
|
| 120 | + * Get the cache information for given key |
|
| 121 | + * @param string $key the key for cache to get the information for |
|
| 122 | + * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 123 | + * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 124 | + * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 125 | + * 'ttl' => the time to live of the cache in second |
|
| 126 | + */ |
|
| 127 | + public function getInfo($key){ |
|
| 128 | + $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 129 | + $cacheInfos = $this->_getCacheInfo($key); |
|
| 130 | + if($cacheInfos){ |
|
| 131 | + $data = array( |
|
| 132 | + 'mtime' => $cacheInfos['creation_time'], |
|
| 133 | + 'expire' => $cacheInfos['creation_time'] + $cacheInfos['ttl'], |
|
| 134 | + 'ttl' => $cacheInfos['ttl'] |
|
| 135 | + ); |
|
| 136 | + return $data; |
|
| 137 | + } |
|
| 138 | + else{ |
|
| 139 | + $this->logger->info('This cache does not exists skipping'); |
|
| 140 | + return false; |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | 144 | |
| 145 | - /** |
|
| 146 | - * Used to delete expired cache data |
|
| 147 | - */ |
|
| 148 | - public function deleteExpiredCache(){ |
|
| 149 | - //for APC[u] is done automatically |
|
| 150 | - return true; |
|
| 151 | - } |
|
| 145 | + /** |
|
| 146 | + * Used to delete expired cache data |
|
| 147 | + */ |
|
| 148 | + public function deleteExpiredCache(){ |
|
| 149 | + //for APC[u] is done automatically |
|
| 150 | + return true; |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * Remove all cache data |
|
| 155 | - */ |
|
| 156 | - public function clean(){ |
|
| 157 | - $this->logger->debug('Deleting of all cache data'); |
|
| 158 | - $cacheInfos = apc_cache_info('user'); |
|
| 159 | - if(empty($cacheInfos['cache_list'])){ |
|
| 160 | - $this->logger->info('No cache data were found skipping'); |
|
| 161 | - return false; |
|
| 162 | - } |
|
| 163 | - else{ |
|
| 164 | - $this->logger->info('Found [' . count($cacheInfos) . '] cache data to remove'); |
|
| 165 | - return apc_clear_cache('user'); |
|
| 166 | - } |
|
| 167 | - } |
|
| 153 | + /** |
|
| 154 | + * Remove all cache data |
|
| 155 | + */ |
|
| 156 | + public function clean(){ |
|
| 157 | + $this->logger->debug('Deleting of all cache data'); |
|
| 158 | + $cacheInfos = apc_cache_info('user'); |
|
| 159 | + if(empty($cacheInfos['cache_list'])){ |
|
| 160 | + $this->logger->info('No cache data were found skipping'); |
|
| 161 | + return false; |
|
| 162 | + } |
|
| 163 | + else{ |
|
| 164 | + $this->logger->info('Found [' . count($cacheInfos) . '] cache data to remove'); |
|
| 165 | + return apc_clear_cache('user'); |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | 169 | |
| 170 | - /** |
|
| 171 | - * Check whether the cache feature for the handle is supported |
|
| 172 | - * |
|
| 173 | - * @return bool |
|
| 174 | - */ |
|
| 175 | - public function isSupported(){ |
|
| 176 | - return (extension_loaded('apc') || extension_loaded('apcu')) && ini_get('apc.enabled'); |
|
| 177 | - } |
|
| 170 | + /** |
|
| 171 | + * Check whether the cache feature for the handle is supported |
|
| 172 | + * |
|
| 173 | + * @return bool |
|
| 174 | + */ |
|
| 175 | + public function isSupported(){ |
|
| 176 | + return (extension_loaded('apc') || extension_loaded('apcu')) && ini_get('apc.enabled'); |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | - /** |
|
| 180 | - * Return the Log instance |
|
| 181 | - * @return Log |
|
| 182 | - */ |
|
| 183 | - public function getLogger(){ |
|
| 184 | - return $this->logger; |
|
| 185 | - } |
|
| 179 | + /** |
|
| 180 | + * Return the Log instance |
|
| 181 | + * @return Log |
|
| 182 | + */ |
|
| 183 | + public function getLogger(){ |
|
| 184 | + return $this->logger; |
|
| 185 | + } |
|
| 186 | 186 | |
| 187 | - /** |
|
| 188 | - * Set the log instance |
|
| 189 | - * @param Log $logger the log object |
|
| 190 | - */ |
|
| 191 | - public function setLogger(Log $logger){ |
|
| 192 | - $this->logger = $logger; |
|
| 193 | - return $this; |
|
| 194 | - } |
|
| 187 | + /** |
|
| 188 | + * Set the log instance |
|
| 189 | + * @param Log $logger the log object |
|
| 190 | + */ |
|
| 191 | + public function setLogger(Log $logger){ |
|
| 192 | + $this->logger = $logger; |
|
| 193 | + return $this; |
|
| 194 | + } |
|
| 195 | 195 | |
| 196 | - /** |
|
| 197 | - * Return the array of cache information |
|
| 198 | - * |
|
| 199 | - * @param string $key the cache key to get the cache information |
|
| 200 | - * @return boolean|array |
|
| 201 | - */ |
|
| 202 | - private function _getCacheInfo($key){ |
|
| 203 | - $caches = apc_cache_info('user'); |
|
| 204 | - if(! empty($caches['cache_list'])){ |
|
| 205 | - $cacheLists = $caches['cache_list']; |
|
| 206 | - foreach ($cacheLists as $c){ |
|
| 207 | - if(isset($c['info']) && $c['info'] === $key){ |
|
| 208 | - return $c; |
|
| 209 | - } |
|
| 210 | - } |
|
| 196 | + /** |
|
| 197 | + * Return the array of cache information |
|
| 198 | + * |
|
| 199 | + * @param string $key the cache key to get the cache information |
|
| 200 | + * @return boolean|array |
|
| 201 | + */ |
|
| 202 | + private function _getCacheInfo($key){ |
|
| 203 | + $caches = apc_cache_info('user'); |
|
| 204 | + if(! empty($caches['cache_list'])){ |
|
| 205 | + $cacheLists = $caches['cache_list']; |
|
| 206 | + foreach ($cacheLists as $c){ |
|
| 207 | + if(isset($c['info']) && $c['info'] === $key){ |
|
| 208 | + return $c; |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | 211 | |
| 212 | - } |
|
| 213 | - return false; |
|
| 214 | - } |
|
| 215 | - } |
|
| 212 | + } |
|
| 213 | + return false; |
|
| 214 | + } |
|
| 215 | + } |
|
@@ -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,182 +1,182 @@ |
||
| 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{ |
|
| 32 | + class EventDispatcher{ |
|
| 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 | - /** |
|
| 42 | - * The logger instance |
|
| 43 | - * @var Log |
|
| 44 | - */ |
|
| 45 | - private $logger; |
|
| 41 | + /** |
|
| 42 | + * The logger instance |
|
| 43 | + * @var Log |
|
| 44 | + */ |
|
| 45 | + private $logger; |
|
| 46 | 46 | |
| 47 | - public function __construct(){ |
|
| 48 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 49 | - $this->logger->setLogger('Library::EventDispatcher'); |
|
| 50 | - } |
|
| 47 | + public function __construct(){ |
|
| 48 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 49 | + $this->logger->setLogger('Library::EventDispatcher'); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Register new listener |
|
| 54 | - * @param string $eventName the name of the event to register for |
|
| 55 | - * @param callable $listener the function or class method to receive the event information after dispatch |
|
| 56 | - */ |
|
| 57 | - public function addListener($eventName, callable $listener){ |
|
| 58 | - $this->logger->debug('Adding new event listener for the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
| 59 | - if(! isset($this->listeners[$eventName])){ |
|
| 60 | - $this->logger->info('This event does not have the registered event listener before, adding new one'); |
|
| 61 | - $this->listeners[$eventName] = array(); |
|
| 62 | - } |
|
| 63 | - else{ |
|
| 64 | - $this->logger->info('This event already have the registered listener, add this listener to the list'); |
|
| 65 | - } |
|
| 66 | - $this->listeners[$eventName][] = $listener; |
|
| 67 | - } |
|
| 52 | + /** |
|
| 53 | + * Register new listener |
|
| 54 | + * @param string $eventName the name of the event to register for |
|
| 55 | + * @param callable $listener the function or class method to receive the event information after dispatch |
|
| 56 | + */ |
|
| 57 | + public function addListener($eventName, callable $listener){ |
|
| 58 | + $this->logger->debug('Adding new event listener for the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
| 59 | + if(! isset($this->listeners[$eventName])){ |
|
| 60 | + $this->logger->info('This event does not have the registered event listener before, adding new one'); |
|
| 61 | + $this->listeners[$eventName] = array(); |
|
| 62 | + } |
|
| 63 | + else{ |
|
| 64 | + $this->logger->info('This event already have the registered listener, add this listener to the list'); |
|
| 65 | + } |
|
| 66 | + $this->listeners[$eventName][] = $listener; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Remove the event listener from list |
|
| 71 | - * @param string $eventName the event name |
|
| 72 | - * @param callable $listener the listener callback |
|
| 73 | - */ |
|
| 74 | - public function removeListener($eventName, callable $listener){ |
|
| 75 | - $this->logger->debug('Removing of the event listener, the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
| 76 | - if(isset($this->listeners[$eventName])){ |
|
| 77 | - $this->logger->info('This event have the listeners, check if this listener exists'); |
|
| 78 | - if(false !== $index = array_search($listener, $this->listeners[$eventName], true)){ |
|
| 79 | - $this->logger->info('Found the listener at index [' .$index. '] remove it'); |
|
| 80 | - unset($this->listeners[$eventName][$index]); |
|
| 81 | - } |
|
| 82 | - else{ |
|
| 83 | - $this->logger->info('Cannot found this listener in the event listener list'); |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - else{ |
|
| 87 | - $this->logger->info('This event does not have this listener ignore remove'); |
|
| 88 | - } |
|
| 89 | - } |
|
| 69 | + /** |
|
| 70 | + * Remove the event listener from list |
|
| 71 | + * @param string $eventName the event name |
|
| 72 | + * @param callable $listener the listener callback |
|
| 73 | + */ |
|
| 74 | + public function removeListener($eventName, callable $listener){ |
|
| 75 | + $this->logger->debug('Removing of the event listener, the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
| 76 | + if(isset($this->listeners[$eventName])){ |
|
| 77 | + $this->logger->info('This event have the listeners, check if this listener exists'); |
|
| 78 | + if(false !== $index = array_search($listener, $this->listeners[$eventName], true)){ |
|
| 79 | + $this->logger->info('Found the listener at index [' .$index. '] remove it'); |
|
| 80 | + unset($this->listeners[$eventName][$index]); |
|
| 81 | + } |
|
| 82 | + else{ |
|
| 83 | + $this->logger->info('Cannot found this listener in the event listener list'); |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + else{ |
|
| 87 | + $this->logger->info('This event does not have this listener ignore remove'); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * Remove all the event listener. If event name is null will remove all listeners, else will just |
|
| 93 | - * remove all listeners for this event |
|
| 94 | - * @param string $eventName the event name |
|
| 95 | - */ |
|
| 96 | - public function removeAllListener($eventName = null){ |
|
| 97 | - $this->logger->debug('Removing of all event listener, the event name [' .$eventName. ']'); |
|
| 98 | - if($eventName !== null && isset($this->listeners[$eventName])){ |
|
| 99 | - $this->logger->info('The event name is set of exist in the listener just remove all event listener for this event'); |
|
| 100 | - unset($this->listeners[$eventName]); |
|
| 101 | - } |
|
| 102 | - else{ |
|
| 103 | - $this->logger->info('The event name is not set or does not exist in the listener, so remove all event listener'); |
|
| 104 | - $this->listeners = array(); |
|
| 105 | - } |
|
| 106 | - } |
|
| 91 | + /** |
|
| 92 | + * Remove all the event listener. If event name is null will remove all listeners, else will just |
|
| 93 | + * remove all listeners for this event |
|
| 94 | + * @param string $eventName the event name |
|
| 95 | + */ |
|
| 96 | + public function removeAllListener($eventName = null){ |
|
| 97 | + $this->logger->debug('Removing of all event listener, the event name [' .$eventName. ']'); |
|
| 98 | + if($eventName !== null && isset($this->listeners[$eventName])){ |
|
| 99 | + $this->logger->info('The event name is set of exist in the listener just remove all event listener for this event'); |
|
| 100 | + unset($this->listeners[$eventName]); |
|
| 101 | + } |
|
| 102 | + else{ |
|
| 103 | + $this->logger->info('The event name is not set or does not exist in the listener, so remove all event listener'); |
|
| 104 | + $this->listeners = array(); |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * Get the list of listener for this event |
|
| 110 | - * @param string $eventName the event name |
|
| 111 | - * @return array the listeners for this event or empty array if this event does not contain any listener |
|
| 112 | - */ |
|
| 113 | - public function getListeners($eventName){ |
|
| 114 | - return isset($this->listeners[$eventName]) ? $this->listeners[$eventName] : array(); |
|
| 115 | - } |
|
| 108 | + /** |
|
| 109 | + * Get the list of listener for this event |
|
| 110 | + * @param string $eventName the event name |
|
| 111 | + * @return array the listeners for this event or empty array if this event does not contain any listener |
|
| 112 | + */ |
|
| 113 | + public function getListeners($eventName){ |
|
| 114 | + return isset($this->listeners[$eventName]) ? $this->listeners[$eventName] : array(); |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * Dispatch the event to the registered listeners. |
|
| 119 | - * @param mixed|object $event the event information |
|
| 120 | - * @return void|object if event need return, will return the final EventInfo object. |
|
| 121 | - */ |
|
| 122 | - public function dispatch($event){ |
|
| 123 | - if(! $event || !$event instanceof EventInfo){ |
|
| 124 | - $this->logger->info('The event is not set or is not an instance of "EventInfo" create the default "EventInfo" object to use instead of.'); |
|
| 125 | - $event = new EventInfo((string) $event); |
|
| 126 | - } |
|
| 127 | - $this->logger->debug('Dispatch to the event listener, the event [' .stringfy_vars($event). ']'); |
|
| 128 | - if(isset($event->stop) && $event->stop){ |
|
| 129 | - $this->logger->info('This event need stopped, no need call any listener'); |
|
| 130 | - return; |
|
| 131 | - } |
|
| 132 | - if($event->returnBack){ |
|
| 133 | - $this->logger->info('This event need return back, return the result for future use'); |
|
| 134 | - return $this->dispatchToListerners($event); |
|
| 135 | - } |
|
| 136 | - else{ |
|
| 137 | - $this->logger->info('This event no need return back the result, just dispatch it'); |
|
| 138 | - $this->dispatchToListerners($event); |
|
| 139 | - } |
|
| 140 | - } |
|
| 117 | + /** |
|
| 118 | + * Dispatch the event to the registered listeners. |
|
| 119 | + * @param mixed|object $event the event information |
|
| 120 | + * @return void|object if event need return, will return the final EventInfo object. |
|
| 121 | + */ |
|
| 122 | + public function dispatch($event){ |
|
| 123 | + if(! $event || !$event instanceof EventInfo){ |
|
| 124 | + $this->logger->info('The event is not set or is not an instance of "EventInfo" create the default "EventInfo" object to use instead of.'); |
|
| 125 | + $event = new EventInfo((string) $event); |
|
| 126 | + } |
|
| 127 | + $this->logger->debug('Dispatch to the event listener, the event [' .stringfy_vars($event). ']'); |
|
| 128 | + if(isset($event->stop) && $event->stop){ |
|
| 129 | + $this->logger->info('This event need stopped, no need call any listener'); |
|
| 130 | + return; |
|
| 131 | + } |
|
| 132 | + if($event->returnBack){ |
|
| 133 | + $this->logger->info('This event need return back, return the result for future use'); |
|
| 134 | + return $this->dispatchToListerners($event); |
|
| 135 | + } |
|
| 136 | + else{ |
|
| 137 | + $this->logger->info('This event no need return back the result, just dispatch it'); |
|
| 138 | + $this->dispatchToListerners($event); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - /** |
|
| 143 | - * Dispatch the event to the registered listeners. |
|
| 144 | - * @param object EventInfo $event the event information |
|
| 145 | - * @return void|object if event need return, will return the final EventInfo instance. |
|
| 146 | - */ |
|
| 147 | - private function dispatchToListerners(EventInfo $event){ |
|
| 148 | - $eBackup = $event; |
|
| 149 | - $list = $this->getListeners($event->name); |
|
| 150 | - if(empty($list)){ |
|
| 151 | - $this->logger->info('No event listener is registered for the event [' .$event->name. '] skipping.'); |
|
| 152 | - if($event->returnBack){ |
|
| 153 | - return $event; |
|
| 154 | - } |
|
| 155 | - return; |
|
| 156 | - } |
|
| 157 | - else{ |
|
| 158 | - $this->logger->info('Found the registered event listener for the event [' .$event->name. '] the list are: ' . stringfy_vars($list)); |
|
| 159 | - } |
|
| 160 | - foreach($list as $listener){ |
|
| 161 | - if($eBackup->returnBack){ |
|
| 162 | - $returnedEvent = call_user_func_array($listener, array($event)); |
|
| 163 | - if($returnedEvent instanceof EventInfo){ |
|
| 164 | - $event = $returnedEvent; |
|
| 165 | - } |
|
| 166 | - else{ |
|
| 167 | - show_error('This event [' .$event->name. '] need you return the event object after processing'); |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - else{ |
|
| 171 | - call_user_func_array($listener, array($event)); |
|
| 172 | - } |
|
| 173 | - if($event->stop){ |
|
| 174 | - break; |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - //only test for original event may be during the flow some listeners change this parameter |
|
| 178 | - if($eBackup->returnBack){ |
|
| 179 | - return $event; |
|
| 180 | - } |
|
| 181 | - } |
|
| 182 | - } |
|
| 142 | + /** |
|
| 143 | + * Dispatch the event to the registered listeners. |
|
| 144 | + * @param object EventInfo $event the event information |
|
| 145 | + * @return void|object if event need return, will return the final EventInfo instance. |
|
| 146 | + */ |
|
| 147 | + private function dispatchToListerners(EventInfo $event){ |
|
| 148 | + $eBackup = $event; |
|
| 149 | + $list = $this->getListeners($event->name); |
|
| 150 | + if(empty($list)){ |
|
| 151 | + $this->logger->info('No event listener is registered for the event [' .$event->name. '] skipping.'); |
|
| 152 | + if($event->returnBack){ |
|
| 153 | + return $event; |
|
| 154 | + } |
|
| 155 | + return; |
|
| 156 | + } |
|
| 157 | + else{ |
|
| 158 | + $this->logger->info('Found the registered event listener for the event [' .$event->name. '] the list are: ' . stringfy_vars($list)); |
|
| 159 | + } |
|
| 160 | + foreach($list as $listener){ |
|
| 161 | + if($eBackup->returnBack){ |
|
| 162 | + $returnedEvent = call_user_func_array($listener, array($event)); |
|
| 163 | + if($returnedEvent instanceof EventInfo){ |
|
| 164 | + $event = $returnedEvent; |
|
| 165 | + } |
|
| 166 | + else{ |
|
| 167 | + show_error('This event [' .$event->name. '] need you return the event object after processing'); |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + else{ |
|
| 171 | + call_user_func_array($listener, array($event)); |
|
| 172 | + } |
|
| 173 | + if($event->stop){ |
|
| 174 | + break; |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + //only test for original event may be during the flow some listeners change this parameter |
|
| 178 | + if($eBackup->returnBack){ |
|
| 179 | + return $event; |
|
| 180 | + } |
|
| 181 | + } |
|
| 182 | + } |
|
@@ -1,39 +1,39 @@ discard block |
||
| 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 | - /** |
|
| 28 | - * DB session handler class |
|
| 29 | - */ |
|
| 30 | - abstract class DBSessionHandlerModel extends Model { |
|
| 27 | + /** |
|
| 28 | + * DB session handler class |
|
| 29 | + */ |
|
| 30 | + abstract class DBSessionHandlerModel extends Model { |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * The session table columns to use |
|
| 34 | - * @var array |
|
| 35 | - * @example |
|
| 36 | - * array( |
|
| 32 | + /** |
|
| 33 | + * The session table columns to use |
|
| 34 | + * @var array |
|
| 35 | + * @example |
|
| 36 | + * array( |
|
| 37 | 37 | 'sid' => '', //VARCHAR(255) Note: this a primary key |
| 38 | 38 | 'sdata' => '', //TEXT |
| 39 | 39 | 'stime' => '', //unix timestamp (INT|BIGINT) |
@@ -42,41 +42,41 @@ discard block |
||
| 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,208 +1,208 @@ |
||
| 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{ |
|
| 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{ |
|
| 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 | - * The logger instance |
|
| 63 | - * @var Log |
|
| 64 | - */ |
|
| 65 | - private $logger; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Construct new Lang instance |
|
| 69 | - */ |
|
| 70 | - public function __construct(){ |
|
| 71 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 72 | - $this->logger->setLogger('Library::Lang'); |
|
| 73 | - |
|
| 74 | - $this->default = get_config('default_language', 'en'); |
|
| 75 | - $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 | + * The logger instance |
|
| 63 | + * @var Log |
|
| 64 | + */ |
|
| 65 | + private $logger; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Construct new Lang instance |
|
| 69 | + */ |
|
| 70 | + public function __construct(){ |
|
| 71 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 72 | + $this->logger->setLogger('Library::Lang'); |
|
| 73 | + |
|
| 74 | + $this->default = get_config('default_language', 'en'); |
|
| 75 | + $this->logger->debug('Setting the supported languages'); |
|
| 76 | 76 | |
| 77 | - //add the supported languages ('key', 'display name') |
|
| 78 | - $languages = get_config('languages', null); |
|
| 79 | - if(! empty($languages)){ |
|
| 80 | - foreach($languages as $key => $displayName){ |
|
| 81 | - $this->addLang($key, $displayName); |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - unset($languages); |
|
| 85 | - |
|
| 86 | - //if the language exists in cookie use it |
|
| 87 | - $cfgKey = get_config('language_cookie_name'); |
|
| 88 | - $this->logger->debug('Getting current language from cookie [' .$cfgKey. ']'); |
|
| 89 | - $objCookie = & class_loader('Cookie'); |
|
| 90 | - $cookieLang = $objCookie->get($cfgKey); |
|
| 91 | - if($cookieLang && $this->isValid($cookieLang)){ |
|
| 92 | - $this->current = $cookieLang; |
|
| 93 | - $this->logger->info('Language from cookie [' .$cfgKey. '] is valid so we will set the language using the cookie value [' .$cookieLang. ']'); |
|
| 94 | - } |
|
| 95 | - else{ |
|
| 96 | - $this->logger->info('Language from cookie [' .$cfgKey. '] is not set, use the default value [' .$this->getDefault(). ']'); |
|
| 97 | - $this->current = $this->getDefault(); |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Get the all languages messages |
|
| 103 | - * |
|
| 104 | - * @return array the language message list |
|
| 105 | - */ |
|
| 106 | - public function getAll(){ |
|
| 107 | - return $this->languages; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Set the language message |
|
| 112 | - * |
|
| 113 | - * @param string $key the language key to identify |
|
| 114 | - * @param string $value the language message value |
|
| 115 | - */ |
|
| 116 | - public function set($key, $value){ |
|
| 117 | - $this->languages[$key] = $value; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Get the language message for the given key. If can't find return the default value |
|
| 122 | - * |
|
| 123 | - * @param string $key the message language key |
|
| 124 | - * @param string $default the default value to return if can not found the language message key |
|
| 125 | - * |
|
| 126 | - * @return string the language message value |
|
| 127 | - */ |
|
| 128 | - public function get($key, $default = 'LANGUAGE_ERROR'){ |
|
| 129 | - if(isset($this->languages[$key])){ |
|
| 130 | - return $this->languages[$key]; |
|
| 131 | - } |
|
| 132 | - $this->logger->warning('Language key [' .$key. '] does not exist use the default value [' .$default. ']'); |
|
| 133 | - return $default; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Check whether the language file for given name exists |
|
| 138 | - * |
|
| 139 | - * @param string $language the language name like "fr", "en", etc. |
|
| 140 | - * |
|
| 141 | - * @return boolean true if the language directory exists, false or not |
|
| 142 | - */ |
|
| 143 | - public function isValid($language){ |
|
| 144 | - $searchDir = array(CORE_LANG_PATH, APP_LANG_PATH); |
|
| 145 | - foreach($searchDir as $dir){ |
|
| 146 | - if(file_exists($dir . $language) && is_dir($dir . $language)){ |
|
| 147 | - return true; |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - return false; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Get the default language value like "en" , "fr", etc. |
|
| 155 | - * |
|
| 156 | - * @return string the default language |
|
| 157 | - */ |
|
| 158 | - public function getDefault(){ |
|
| 159 | - return $this->default; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Get the current language defined by cookie or the default value |
|
| 164 | - * |
|
| 165 | - * @return string the current language |
|
| 166 | - */ |
|
| 167 | - public function getCurrent(){ |
|
| 168 | - return $this->current; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Add new supported or available language |
|
| 173 | - * |
|
| 174 | - * @param string $name the short language name like "en", "fr". |
|
| 175 | - * @param string $description the human readable description of this language |
|
| 176 | - */ |
|
| 177 | - public function addLang($name, $description){ |
|
| 178 | - if(isset($this->availables[$name])){ |
|
| 179 | - return; //already added cost in performance |
|
| 180 | - } |
|
| 181 | - if($this->isValid($name)){ |
|
| 182 | - $this->availables[$name] = $description; |
|
| 183 | - } |
|
| 184 | - else{ |
|
| 185 | - show_error('The language [' . $name . '] is not valid or does not exists.'); |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Get the list of the application supported language |
|
| 191 | - * |
|
| 192 | - * @return array the list of the application language |
|
| 193 | - */ |
|
| 194 | - public function getSupported(){ |
|
| 195 | - return $this->availables; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Add new language messages |
|
| 200 | - * |
|
| 201 | - * @param array $langs the languages array of the messages to be added |
|
| 202 | - */ |
|
| 203 | - public function addLangMessages(array $langs){ |
|
| 204 | - foreach ($langs as $key => $value) { |
|
| 205 | - $this->set($key, $value); |
|
| 206 | - } |
|
| 207 | - } |
|
| 208 | - } |
|
| 77 | + //add the supported languages ('key', 'display name') |
|
| 78 | + $languages = get_config('languages', null); |
|
| 79 | + if(! empty($languages)){ |
|
| 80 | + foreach($languages as $key => $displayName){ |
|
| 81 | + $this->addLang($key, $displayName); |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + unset($languages); |
|
| 85 | + |
|
| 86 | + //if the language exists in cookie use it |
|
| 87 | + $cfgKey = get_config('language_cookie_name'); |
|
| 88 | + $this->logger->debug('Getting current language from cookie [' .$cfgKey. ']'); |
|
| 89 | + $objCookie = & class_loader('Cookie'); |
|
| 90 | + $cookieLang = $objCookie->get($cfgKey); |
|
| 91 | + if($cookieLang && $this->isValid($cookieLang)){ |
|
| 92 | + $this->current = $cookieLang; |
|
| 93 | + $this->logger->info('Language from cookie [' .$cfgKey. '] is valid so we will set the language using the cookie value [' .$cookieLang. ']'); |
|
| 94 | + } |
|
| 95 | + else{ |
|
| 96 | + $this->logger->info('Language from cookie [' .$cfgKey. '] is not set, use the default value [' .$this->getDefault(). ']'); |
|
| 97 | + $this->current = $this->getDefault(); |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Get the all languages messages |
|
| 103 | + * |
|
| 104 | + * @return array the language message list |
|
| 105 | + */ |
|
| 106 | + public function getAll(){ |
|
| 107 | + return $this->languages; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Set the language message |
|
| 112 | + * |
|
| 113 | + * @param string $key the language key to identify |
|
| 114 | + * @param string $value the language message value |
|
| 115 | + */ |
|
| 116 | + public function set($key, $value){ |
|
| 117 | + $this->languages[$key] = $value; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Get the language message for the given key. If can't find return the default value |
|
| 122 | + * |
|
| 123 | + * @param string $key the message language key |
|
| 124 | + * @param string $default the default value to return if can not found the language message key |
|
| 125 | + * |
|
| 126 | + * @return string the language message value |
|
| 127 | + */ |
|
| 128 | + public function get($key, $default = 'LANGUAGE_ERROR'){ |
|
| 129 | + if(isset($this->languages[$key])){ |
|
| 130 | + return $this->languages[$key]; |
|
| 131 | + } |
|
| 132 | + $this->logger->warning('Language key [' .$key. '] does not exist use the default value [' .$default. ']'); |
|
| 133 | + return $default; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Check whether the language file for given name exists |
|
| 138 | + * |
|
| 139 | + * @param string $language the language name like "fr", "en", etc. |
|
| 140 | + * |
|
| 141 | + * @return boolean true if the language directory exists, false or not |
|
| 142 | + */ |
|
| 143 | + public function isValid($language){ |
|
| 144 | + $searchDir = array(CORE_LANG_PATH, APP_LANG_PATH); |
|
| 145 | + foreach($searchDir as $dir){ |
|
| 146 | + if(file_exists($dir . $language) && is_dir($dir . $language)){ |
|
| 147 | + return true; |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + return false; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Get the default language value like "en" , "fr", etc. |
|
| 155 | + * |
|
| 156 | + * @return string the default language |
|
| 157 | + */ |
|
| 158 | + public function getDefault(){ |
|
| 159 | + return $this->default; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Get the current language defined by cookie or the default value |
|
| 164 | + * |
|
| 165 | + * @return string the current language |
|
| 166 | + */ |
|
| 167 | + public function getCurrent(){ |
|
| 168 | + return $this->current; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Add new supported or available language |
|
| 173 | + * |
|
| 174 | + * @param string $name the short language name like "en", "fr". |
|
| 175 | + * @param string $description the human readable description of this language |
|
| 176 | + */ |
|
| 177 | + public function addLang($name, $description){ |
|
| 178 | + if(isset($this->availables[$name])){ |
|
| 179 | + return; //already added cost in performance |
|
| 180 | + } |
|
| 181 | + if($this->isValid($name)){ |
|
| 182 | + $this->availables[$name] = $description; |
|
| 183 | + } |
|
| 184 | + else{ |
|
| 185 | + show_error('The language [' . $name . '] is not valid or does not exists.'); |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Get the list of the application supported language |
|
| 191 | + * |
|
| 192 | + * @return array the list of the application language |
|
| 193 | + */ |
|
| 194 | + public function getSupported(){ |
|
| 195 | + return $this->availables; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Add new language messages |
|
| 200 | + * |
|
| 201 | + * @param array $langs the languages array of the messages to be added |
|
| 202 | + */ |
|
| 203 | + public function addLangMessages(array $langs){ |
|
| 204 | + foreach ($langs as $key => $value) { |
|
| 205 | + $this->set($key, $value); |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | + } |
|
@@ -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,739 +1,739 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * Simple Mail |
|
| 5 | - * |
|
| 6 | - * A simple PHP wrapper class for sending email using the mail() method. |
|
| 7 | - * |
|
| 8 | - * PHP version > 5.2 |
|
| 9 | - * |
|
| 10 | - * LICENSE: This source file is subject to the MIT license, which is |
|
| 11 | - * available through the world-wide-web at the following URI: |
|
| 12 | - * http://github.com/eoghanobrien/php-simple-mail/LICENCE.txt |
|
| 13 | - * |
|
| 14 | - * @category SimpleMail |
|
| 15 | - * @package SimpleMail |
|
| 16 | - * @author Eoghan O'Brien <[email protected]> |
|
| 17 | - * @copyright 2009 - 2017 Eoghan O'Brien |
|
| 18 | - * @license http://github.com/eoghanobrien/php-simple-mail/LICENCE.txt MIT |
|
| 19 | - * @version 1.7.1 |
|
| 20 | - * @link http://github.com/eoghanobrien/php-simple-mail |
|
| 21 | - */ |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Simple Mail class. |
|
| 25 | - * |
|
| 26 | - * @category SimpleMail |
|
| 27 | - * @package SimpleMail |
|
| 28 | - * @author Eoghan O'Brien <[email protected]> |
|
| 29 | - * @copyright 2009 - 2017 Eoghan O'Brien |
|
| 30 | - * @license http://github.com/eoghanobrien/php-simple-mail/LICENCE.txt MIT |
|
| 31 | - * @version 1.7.1 |
|
| 32 | - * @link http://github.com/eoghanobrien/php-simple-mail |
|
| 33 | - */ |
|
| 34 | - class Email |
|
| 35 | - { |
|
| 36 | - /** |
|
| 37 | - * @var int $_wrap |
|
| 38 | - */ |
|
| 39 | - protected $_wrap = 78; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var array $_to |
|
| 43 | - */ |
|
| 44 | - protected $_to = array(); |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var string $_subject |
|
| 48 | - */ |
|
| 49 | - protected $_subject; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @var string $_message |
|
| 53 | - */ |
|
| 54 | - protected $_message; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @var array $_headers |
|
| 58 | - */ |
|
| 59 | - protected $_headers = array(); |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @var string $_parameters |
|
| 63 | - */ |
|
| 64 | - protected $_params; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @var array $_attachments |
|
| 68 | - */ |
|
| 69 | - protected $_attachments = array(); |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @var string $_uid |
|
| 73 | - */ |
|
| 74 | - protected $_uid; |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * Simple Mail |
|
| 5 | + * |
|
| 6 | + * A simple PHP wrapper class for sending email using the mail() method. |
|
| 7 | + * |
|
| 8 | + * PHP version > 5.2 |
|
| 9 | + * |
|
| 10 | + * LICENSE: This source file is subject to the MIT license, which is |
|
| 11 | + * available through the world-wide-web at the following URI: |
|
| 12 | + * http://github.com/eoghanobrien/php-simple-mail/LICENCE.txt |
|
| 13 | + * |
|
| 14 | + * @category SimpleMail |
|
| 15 | + * @package SimpleMail |
|
| 16 | + * @author Eoghan O'Brien <[email protected]> |
|
| 17 | + * @copyright 2009 - 2017 Eoghan O'Brien |
|
| 18 | + * @license http://github.com/eoghanobrien/php-simple-mail/LICENCE.txt MIT |
|
| 19 | + * @version 1.7.1 |
|
| 20 | + * @link http://github.com/eoghanobrien/php-simple-mail |
|
| 21 | + */ |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Simple Mail class. |
|
| 25 | + * |
|
| 26 | + * @category SimpleMail |
|
| 27 | + * @package SimpleMail |
|
| 28 | + * @author Eoghan O'Brien <[email protected]> |
|
| 29 | + * @copyright 2009 - 2017 Eoghan O'Brien |
|
| 30 | + * @license http://github.com/eoghanobrien/php-simple-mail/LICENCE.txt MIT |
|
| 31 | + * @version 1.7.1 |
|
| 32 | + * @link http://github.com/eoghanobrien/php-simple-mail |
|
| 33 | + */ |
|
| 34 | + class Email |
|
| 35 | + { |
|
| 36 | + /** |
|
| 37 | + * @var int $_wrap |
|
| 38 | + */ |
|
| 39 | + protected $_wrap = 78; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var array $_to |
|
| 43 | + */ |
|
| 44 | + protected $_to = array(); |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var string $_subject |
|
| 48 | + */ |
|
| 49 | + protected $_subject; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @var string $_message |
|
| 53 | + */ |
|
| 54 | + protected $_message; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @var array $_headers |
|
| 58 | + */ |
|
| 59 | + protected $_headers = array(); |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @var string $_parameters |
|
| 63 | + */ |
|
| 64 | + protected $_params; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @var array $_attachments |
|
| 68 | + */ |
|
| 69 | + protected $_attachments = array(); |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @var string $_uid |
|
| 73 | + */ |
|
| 74 | + protected $_uid; |
|
| 75 | 75 | |
| 76 | - /** |
|
| 76 | + /** |
|
| 77 | 77 | * The logger instance |
| 78 | 78 | * @var Log |
| 79 | 79 | */ |
| 80 | - private $logger; |
|
| 80 | + private $logger; |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * __construct |
|
| 84 | - * |
|
| 85 | - * Resets the class properties. |
|
| 86 | - */ |
|
| 87 | - public function __construct() |
|
| 88 | - { |
|
| 89 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 82 | + /** |
|
| 83 | + * __construct |
|
| 84 | + * |
|
| 85 | + * Resets the class properties. |
|
| 86 | + */ |
|
| 87 | + public function __construct() |
|
| 88 | + { |
|
| 89 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 90 | 90 | $this->logger->setLogger('Library::Email'); |
| 91 | - $this->reset(); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * reset |
|
| 96 | - * |
|
| 97 | - * Resets all properties to initial state. |
|
| 98 | - * |
|
| 99 | - * @return self |
|
| 100 | - */ |
|
| 101 | - public function reset() |
|
| 102 | - { |
|
| 103 | - $this->_to = array(); |
|
| 104 | - $this->_headers = array(); |
|
| 105 | - $this->_subject = null; |
|
| 106 | - $this->_message = null; |
|
| 107 | - $this->_wrap = 78; |
|
| 108 | - $this->_params = null; |
|
| 109 | - $this->_attachments = array(); |
|
| 110 | - $this->_uid = $this->getUniqueId(); |
|
| 111 | - return $this; |
|
| 112 | - } |
|
| 91 | + $this->reset(); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * reset |
|
| 96 | + * |
|
| 97 | + * Resets all properties to initial state. |
|
| 98 | + * |
|
| 99 | + * @return self |
|
| 100 | + */ |
|
| 101 | + public function reset() |
|
| 102 | + { |
|
| 103 | + $this->_to = array(); |
|
| 104 | + $this->_headers = array(); |
|
| 105 | + $this->_subject = null; |
|
| 106 | + $this->_message = null; |
|
| 107 | + $this->_wrap = 78; |
|
| 108 | + $this->_params = null; |
|
| 109 | + $this->_attachments = array(); |
|
| 110 | + $this->_uid = $this->getUniqueId(); |
|
| 111 | + return $this; |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * setFrom |
|
| 116 | - * |
|
| 117 | - * @param string $email The email to send as from. |
|
| 118 | - * @param string $name The name to send as from. |
|
| 119 | - * |
|
| 120 | - * @return self |
|
| 121 | - */ |
|
| 122 | - public function setFrom($email, $name = null) |
|
| 123 | - { |
|
| 124 | - $this->addMailHeader('From', (string) $email, (string) $name); |
|
| 125 | - return $this; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * setTo |
|
| 130 | - * |
|
| 131 | - * @param string $email The email address to send to. |
|
| 132 | - * @param string $name The name of the person to send to. |
|
| 133 | - * |
|
| 134 | - * @return self |
|
| 135 | - */ |
|
| 136 | - public function setTo($email, $name = null) |
|
| 137 | - { |
|
| 138 | - $this->_to[] = $this->formatHeader((string) $email, (string) $name); |
|
| 139 | - return $this; |
|
| 140 | - } |
|
| 114 | + /** |
|
| 115 | + * setFrom |
|
| 116 | + * |
|
| 117 | + * @param string $email The email to send as from. |
|
| 118 | + * @param string $name The name to send as from. |
|
| 119 | + * |
|
| 120 | + * @return self |
|
| 121 | + */ |
|
| 122 | + public function setFrom($email, $name = null) |
|
| 123 | + { |
|
| 124 | + $this->addMailHeader('From', (string) $email, (string) $name); |
|
| 125 | + return $this; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * setTo |
|
| 130 | + * |
|
| 131 | + * @param string $email The email address to send to. |
|
| 132 | + * @param string $name The name of the person to send to. |
|
| 133 | + * |
|
| 134 | + * @return self |
|
| 135 | + */ |
|
| 136 | + public function setTo($email, $name = null) |
|
| 137 | + { |
|
| 138 | + $this->_to[] = $this->formatHeader((string) $email, (string) $name); |
|
| 139 | + return $this; |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - /** |
|
| 143 | - * Set destination using array |
|
| 144 | - * @params array $emails the list of recipient. This is an associative array name => email |
|
| 145 | - * @example array('John Doe' => '[email protected]') |
|
| 146 | - * @return Object the current instance |
|
| 147 | - */ |
|
| 148 | - public function setTos(array $emails) |
|
| 149 | - { |
|
| 150 | - foreach ($emails as $name => $email) { |
|
| 151 | - if(is_numeric($name)){ |
|
| 152 | - $this->setTo($email); |
|
| 153 | - } |
|
| 154 | - else{ |
|
| 155 | - $this->setTo($email, $name); |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - return $this; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * getTo |
|
| 164 | - * |
|
| 165 | - * Return an array of formatted To addresses. |
|
| 166 | - * |
|
| 167 | - * @return array |
|
| 168 | - */ |
|
| 169 | - public function getTo() |
|
| 170 | - { |
|
| 171 | - return $this->_to; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * setCc |
|
| 177 | - * |
|
| 178 | - * @param array $pairs An array of name => email pairs. |
|
| 179 | - * |
|
| 180 | - * @return self |
|
| 181 | - */ |
|
| 182 | - public function setCc(array $pairs) |
|
| 183 | - { |
|
| 184 | - return $this->addMailHeaders('Cc', $pairs); |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * setBcc |
|
| 189 | - * |
|
| 190 | - * @param array $pairs An array of name => email pairs. |
|
| 191 | - * |
|
| 192 | - * @return self |
|
| 193 | - */ |
|
| 194 | - public function setBcc(array $pairs) |
|
| 195 | - { |
|
| 196 | - return $this->addMailHeaders('Bcc', $pairs); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * setReplyTo |
|
| 201 | - * |
|
| 202 | - * @param string $email |
|
| 203 | - * @param string $name |
|
| 204 | - * |
|
| 205 | - * @return self |
|
| 206 | - */ |
|
| 207 | - public function setReplyTo($email, $name = null) |
|
| 208 | - { |
|
| 209 | - return $this->addMailHeader('Reply-To', $email, $name); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * setHtml |
|
| 214 | - * |
|
| 215 | - * @return self |
|
| 216 | - */ |
|
| 217 | - public function setHtml() |
|
| 218 | - { |
|
| 219 | - $this->addGenericHeader( |
|
| 220 | - 'Content-Type', 'text/html; charset="utf-8"' |
|
| 221 | - ); |
|
| 222 | - return $this; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * setSubject |
|
| 227 | - * |
|
| 228 | - * @param string $subject The email subject |
|
| 229 | - * |
|
| 230 | - * @return self |
|
| 231 | - */ |
|
| 232 | - public function setSubject($subject) |
|
| 233 | - { |
|
| 234 | - $this->_subject = $this->encodeUtf8( |
|
| 235 | - $this->filterOther((string) $subject) |
|
| 236 | - ); |
|
| 237 | - return $this; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * getSubject function. |
|
| 242 | - * |
|
| 243 | - * @return string |
|
| 244 | - */ |
|
| 245 | - public function getSubject() |
|
| 246 | - { |
|
| 247 | - return $this->_subject; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * setMessage |
|
| 252 | - * |
|
| 253 | - * @param string $message The message to send. |
|
| 254 | - * |
|
| 255 | - * @return self |
|
| 256 | - */ |
|
| 257 | - public function setMessage($message) |
|
| 258 | - { |
|
| 259 | - $this->_message = str_replace("\n.", "\n..", (string) $message); |
|
| 260 | - return $this; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * getMessage |
|
| 265 | - * |
|
| 266 | - * @return string |
|
| 267 | - */ |
|
| 268 | - public function getMessage() |
|
| 269 | - { |
|
| 270 | - return $this->_message; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * addAttachment |
|
| 275 | - * |
|
| 276 | - * @param string $path The file path to the attachment. |
|
| 277 | - * @param string $filename The filename of the attachment when emailed. |
|
| 278 | - * @param string $data |
|
| 279 | - * |
|
| 280 | - * @return self |
|
| 281 | - */ |
|
| 282 | - public function addAttachment($path, $filename = null, $data = null) |
|
| 283 | - { |
|
| 284 | - if(! file_exists($path)){ |
|
| 285 | - show_error('The file [' .$path. '] does not exists.'); |
|
| 286 | - } |
|
| 287 | - $filename = empty($filename) ? basename($path) : $filename; |
|
| 288 | - $filename = $this->encodeUtf8($this->filterOther((string) $filename)); |
|
| 289 | - $data = empty($data) ? $this->getAttachmentData($path) : $data; |
|
| 290 | - $this->_attachments[] = array( |
|
| 291 | - 'path' => $path, |
|
| 292 | - 'file' => $filename, |
|
| 293 | - 'data' => chunk_split(base64_encode($data)) |
|
| 294 | - ); |
|
| 295 | - return $this; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * getAttachmentData |
|
| 300 | - * |
|
| 301 | - * @param string $path The path to the attachment file. |
|
| 302 | - * |
|
| 303 | - * @return string |
|
| 304 | - */ |
|
| 305 | - public function getAttachmentData($path) |
|
| 306 | - { |
|
| 307 | - if(! file_exists($path)){ |
|
| 308 | - show_error('The file [' .$path. '] does not exists.'); |
|
| 309 | - } |
|
| 310 | - $filesize = filesize($path); |
|
| 311 | - $handle = fopen($path, "r"); |
|
| 312 | - $attachment = null; |
|
| 313 | - if(is_resource($handle)){ |
|
| 314 | - $attachment = fread($handle, $filesize); |
|
| 315 | - fclose($handle); |
|
| 316 | - } |
|
| 317 | - return $attachment; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * addMailHeader |
|
| 322 | - * |
|
| 323 | - * @param string $header The header to add. |
|
| 324 | - * @param string $email The email to add. |
|
| 325 | - * @param string $name The name to add. |
|
| 326 | - * |
|
| 327 | - * @return self |
|
| 328 | - */ |
|
| 329 | - public function addMailHeader($header, $email, $name = null) |
|
| 330 | - { |
|
| 331 | - $address = $this->formatHeader((string) $email, (string) $name); |
|
| 332 | - $this->_headers[] = sprintf('%s: %s', (string) $header, $address); |
|
| 333 | - return $this; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * addMailHeaders |
|
| 338 | - * |
|
| 339 | - * @param string $header The header to add. |
|
| 340 | - * @param array $pairs An array of name => email pairs. |
|
| 341 | - * |
|
| 342 | - * @return self |
|
| 343 | - */ |
|
| 344 | - public function addMailHeaders($header, array $pairs) |
|
| 345 | - { |
|
| 346 | - if (count($pairs) === 0) { |
|
| 347 | - show_error('You must pass at least one name => email pair.'); |
|
| 348 | - } |
|
| 349 | - $addresses = array(); |
|
| 350 | - foreach ($pairs as $name => $email) { |
|
| 351 | - $name = is_numeric($name) ? null : $name; |
|
| 352 | - $addresses[] = $this->formatHeader($email, $name); |
|
| 353 | - } |
|
| 354 | - $this->addGenericHeader($header, implode(',', $addresses)); |
|
| 355 | - return $this; |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * addGenericHeader |
|
| 360 | - * |
|
| 361 | - * @param string $name The generic header to add. |
|
| 362 | - * @param mixed $value The value of the header. |
|
| 363 | - * |
|
| 364 | - * @return self |
|
| 365 | - */ |
|
| 366 | - public function addGenericHeader($name, $value) |
|
| 367 | - { |
|
| 368 | - $this->_headers[] = sprintf( |
|
| 369 | - '%s: %s', |
|
| 370 | - (string) $name, |
|
| 371 | - (string) $value |
|
| 372 | - ); |
|
| 373 | - return $this; |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * getHeaders |
|
| 378 | - * |
|
| 379 | - * Return the headers registered so far as an array. |
|
| 380 | - * |
|
| 381 | - * @return array |
|
| 382 | - */ |
|
| 383 | - public function getHeaders() |
|
| 384 | - { |
|
| 385 | - return $this->_headers; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * setAdditionalParameters |
|
| 390 | - * |
|
| 391 | - * Such as "[email protected] |
|
| 392 | - * |
|
| 393 | - * @param string $additionalParameters The addition mail parameter. |
|
| 394 | - * |
|
| 395 | - * @return self |
|
| 396 | - */ |
|
| 397 | - public function setParameters($additionalParameters) |
|
| 398 | - { |
|
| 399 | - $this->_params = (string) $additionalParameters; |
|
| 400 | - return $this; |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * getAdditionalParameters |
|
| 405 | - * |
|
| 406 | - * @return string |
|
| 407 | - */ |
|
| 408 | - public function getParameters() |
|
| 409 | - { |
|
| 410 | - return $this->_params; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * setWrap |
|
| 415 | - * |
|
| 416 | - * @param int $wrap The number of characters at which the message will wrap. |
|
| 417 | - * |
|
| 418 | - * @return self |
|
| 419 | - */ |
|
| 420 | - public function setWrap($wrap = 78) |
|
| 421 | - { |
|
| 422 | - $wrap = (int) $wrap; |
|
| 423 | - if ($wrap < 1) { |
|
| 424 | - $wrap = 78; |
|
| 425 | - } |
|
| 426 | - $this->_wrap = $wrap; |
|
| 427 | - return $this; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - /** |
|
| 431 | - * getWrap |
|
| 432 | - * |
|
| 433 | - * @return int |
|
| 434 | - */ |
|
| 435 | - public function getWrap() |
|
| 436 | - { |
|
| 437 | - return $this->_wrap; |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - /** |
|
| 441 | - * hasAttachments |
|
| 442 | - * |
|
| 443 | - * Checks if the email has any registered attachments. |
|
| 444 | - * |
|
| 445 | - * @return bool |
|
| 446 | - */ |
|
| 447 | - public function hasAttachments() |
|
| 448 | - { |
|
| 449 | - return !empty($this->_attachments); |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * assembleAttachment |
|
| 454 | - * |
|
| 455 | - * @return string |
|
| 456 | - */ |
|
| 457 | - public function assembleAttachmentHeaders() |
|
| 458 | - { |
|
| 459 | - $head = array(); |
|
| 460 | - $head[] = "MIME-Version: 1.0"; |
|
| 461 | - $head[] = "Content-Type: multipart/mixed; boundary=\"{$this->_uid}\""; |
|
| 462 | - |
|
| 463 | - return join(PHP_EOL, $head); |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - /** |
|
| 467 | - * assembleAttachmentBody |
|
| 468 | - * |
|
| 469 | - * @return string |
|
| 470 | - */ |
|
| 471 | - public function assembleAttachmentBody() |
|
| 472 | - { |
|
| 473 | - $body = array(); |
|
| 474 | - $body[] = "This is a multi-part message in MIME format."; |
|
| 475 | - $body[] = "--{$this->_uid}"; |
|
| 476 | - $body[] = "Content-Type: text/html; charset=\"utf-8\""; |
|
| 477 | - $body[] = "Content-Transfer-Encoding: quoted-printable"; |
|
| 478 | - $body[] = ""; |
|
| 479 | - $body[] = quoted_printable_encode($this->_message); |
|
| 480 | - $body[] = ""; |
|
| 481 | - $body[] = "--{$this->_uid}"; |
|
| 482 | - |
|
| 483 | - foreach ($this->_attachments as $attachment) { |
|
| 484 | - $body[] = $this->getAttachmentMimeTemplate($attachment); |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - return implode(PHP_EOL, $body) . '--'; |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - /** |
|
| 491 | - * getAttachmentMimeTemplate |
|
| 492 | - * |
|
| 493 | - * @param array $attachment An array containing 'file' and 'data' keys. |
|
| 494 | - * |
|
| 495 | - * @return string |
|
| 496 | - */ |
|
| 497 | - public function getAttachmentMimeTemplate($attachment) |
|
| 498 | - { |
|
| 499 | - $file = $attachment['file']; |
|
| 500 | - $data = $attachment['data']; |
|
| 501 | - |
|
| 502 | - $head = array(); |
|
| 503 | - $head[] = "Content-Type: application/octet-stream; name=\"{$file}\""; |
|
| 504 | - $head[] = "Content-Transfer-Encoding: base64"; |
|
| 505 | - $head[] = "Content-Disposition: attachment; filename=\"{$file}\""; |
|
| 506 | - $head[] = ""; |
|
| 507 | - $head[] = $data; |
|
| 508 | - $head[] = ""; |
|
| 509 | - $head[] = "--{$this->_uid}"; |
|
| 510 | - |
|
| 511 | - return implode(PHP_EOL, $head); |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * send the email |
|
| 516 | - * |
|
| 517 | - * @return boolean |
|
| 518 | - */ |
|
| 519 | - public function send() |
|
| 520 | - { |
|
| 521 | - $to = $this->getToForSend(); |
|
| 522 | - $headers = $this->getHeadersForSend(); |
|
| 523 | - |
|
| 524 | - if (empty($to)) { |
|
| 525 | - show_error('Unable to send, no To address has been set.'); |
|
| 526 | - } |
|
| 527 | - |
|
| 528 | - if ($this->hasAttachments()) { |
|
| 529 | - $message = $this->assembleAttachmentBody(); |
|
| 530 | - $headers .= PHP_EOL . $this->assembleAttachmentHeaders(); |
|
| 531 | - } else { |
|
| 532 | - $message = $this->getWrapMessage(); |
|
| 533 | - } |
|
| 534 | - $this->logger->info('Sending new mail, the information are listed below: destination: ' . $to . ', headers: ' . $headers . ', message: ' . $message); |
|
| 535 | - return mail($to, $this->_subject, $message, $headers, $this->_params); |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - /** |
|
| 539 | - * debug |
|
| 540 | - * |
|
| 541 | - * @return string |
|
| 542 | - */ |
|
| 543 | - public function debug() |
|
| 544 | - { |
|
| 545 | - return '<pre>' . print_r($this, true) . '</pre>'; |
|
| 546 | - } |
|
| 547 | - |
|
| 548 | - /** |
|
| 549 | - * magic __toString function |
|
| 550 | - * |
|
| 551 | - * @return string |
|
| 552 | - */ |
|
| 553 | - public function __toString() |
|
| 554 | - { |
|
| 555 | - return print_r($this, true); |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - /** |
|
| 559 | - * formatHeader |
|
| 560 | - * |
|
| 561 | - * Formats a display address for emails according to RFC2822 e.g. |
|
| 562 | - * Name <[email protected]> |
|
| 563 | - * |
|
| 564 | - * @param string $email The email address. |
|
| 565 | - * @param string $name The display name. |
|
| 566 | - * |
|
| 567 | - * @return string |
|
| 568 | - */ |
|
| 569 | - public function formatHeader($email, $name = null) |
|
| 570 | - { |
|
| 571 | - $email = $this->filterEmail((string) $email); |
|
| 572 | - if (empty($name)) { |
|
| 573 | - return $email; |
|
| 574 | - } |
|
| 575 | - $name = $this->encodeUtf8($this->filterName((string) $name)); |
|
| 576 | - return sprintf('"%s" <%s>', $name, $email); |
|
| 577 | - } |
|
| 578 | - |
|
| 579 | - /** |
|
| 580 | - * encodeUtf8 |
|
| 581 | - * |
|
| 582 | - * @param string $value The value to encode. |
|
| 583 | - * |
|
| 584 | - * @return string |
|
| 585 | - */ |
|
| 586 | - public function encodeUtf8($value) |
|
| 587 | - { |
|
| 588 | - $value = trim($value); |
|
| 589 | - if (preg_match('/(\s)/', $value)) { |
|
| 590 | - return $this->encodeUtf8Words($value); |
|
| 591 | - } |
|
| 592 | - return $this->encodeUtf8Word($value); |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * encodeUtf8Word |
|
| 597 | - * |
|
| 598 | - * @param string $value The word to encode. |
|
| 599 | - * |
|
| 600 | - * @return string |
|
| 601 | - */ |
|
| 602 | - public function encodeUtf8Word($value) |
|
| 603 | - { |
|
| 604 | - return sprintf('=?UTF-8?B?%s?=', base64_encode($value)); |
|
| 605 | - } |
|
| 606 | - |
|
| 607 | - /** |
|
| 608 | - * encodeUtf8Words |
|
| 609 | - * |
|
| 610 | - * @param string $value The words to encode. |
|
| 611 | - * |
|
| 612 | - * @return string |
|
| 613 | - */ |
|
| 614 | - public function encodeUtf8Words($value) |
|
| 615 | - { |
|
| 616 | - $words = explode(' ', $value); |
|
| 617 | - $encoded = array(); |
|
| 618 | - foreach ($words as $word) { |
|
| 619 | - $encoded[] = $this->encodeUtf8Word($word); |
|
| 620 | - } |
|
| 621 | - return join($this->encodeUtf8Word(' '), $encoded); |
|
| 622 | - } |
|
| 623 | - |
|
| 624 | - /** |
|
| 625 | - * filterEmail |
|
| 626 | - * |
|
| 627 | - * Removes any carriage return, line feed, tab, double quote, comma |
|
| 628 | - * and angle bracket characters before sanitizing the email address. |
|
| 629 | - * |
|
| 630 | - * @param string $email The email to filter. |
|
| 631 | - * |
|
| 632 | - * @return string |
|
| 633 | - */ |
|
| 634 | - public function filterEmail($email) |
|
| 635 | - { |
|
| 636 | - $rule = array( |
|
| 637 | - "\r" => '', |
|
| 638 | - "\n" => '', |
|
| 639 | - "\t" => '', |
|
| 640 | - '"' => '', |
|
| 641 | - ',' => '', |
|
| 642 | - '<' => '', |
|
| 643 | - '>' => '' |
|
| 644 | - ); |
|
| 645 | - $email = strtr($email, $rule); |
|
| 646 | - $email = filter_var($email, FILTER_SANITIZE_EMAIL); |
|
| 647 | - return $email; |
|
| 648 | - } |
|
| 649 | - |
|
| 650 | - /** |
|
| 651 | - * filterName |
|
| 652 | - * |
|
| 653 | - * Removes any carriage return, line feed or tab characters. Replaces |
|
| 654 | - * double quotes with single quotes and angle brackets with square |
|
| 655 | - * brackets, before sanitizing the string and stripping out html tags. |
|
| 656 | - * |
|
| 657 | - * @param string $name The name to filter. |
|
| 658 | - * |
|
| 659 | - * @return string |
|
| 660 | - */ |
|
| 661 | - public function filterName($name) |
|
| 662 | - { |
|
| 663 | - $rule = array( |
|
| 664 | - "\r" => '', |
|
| 665 | - "\n" => '', |
|
| 666 | - "\t" => '', |
|
| 667 | - '"' => "'", |
|
| 668 | - '<' => '[', |
|
| 669 | - '>' => ']', |
|
| 670 | - ); |
|
| 671 | - $filtered = filter_var( |
|
| 672 | - $name, |
|
| 673 | - FILTER_SANITIZE_STRING, |
|
| 674 | - FILTER_FLAG_NO_ENCODE_QUOTES |
|
| 675 | - ); |
|
| 676 | - return trim(strtr($filtered, $rule)); |
|
| 677 | - } |
|
| 678 | - |
|
| 679 | - /** |
|
| 680 | - * filterOther |
|
| 681 | - * |
|
| 682 | - * Removes ASCII control characters including any carriage return, line |
|
| 683 | - * feed or tab characters. |
|
| 684 | - * |
|
| 685 | - * @param string $data The data to filter. |
|
| 686 | - * |
|
| 687 | - * @return string |
|
| 688 | - */ |
|
| 689 | - public function filterOther($data) |
|
| 690 | - { |
|
| 691 | - return filter_var($data, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW); |
|
| 692 | - } |
|
| 693 | - |
|
| 694 | - /** |
|
| 695 | - * getHeadersForSend |
|
| 696 | - * |
|
| 697 | - * @return string |
|
| 698 | - */ |
|
| 699 | - public function getHeadersForSend() |
|
| 700 | - { |
|
| 701 | - if (empty($this->_headers)) { |
|
| 702 | - return ''; |
|
| 703 | - } |
|
| 704 | - return join(PHP_EOL, $this->_headers); |
|
| 705 | - } |
|
| 706 | - |
|
| 707 | - /** |
|
| 708 | - * getToForSend |
|
| 709 | - * |
|
| 710 | - * @return string |
|
| 711 | - */ |
|
| 712 | - public function getToForSend() |
|
| 713 | - { |
|
| 714 | - if (empty($this->_to)) { |
|
| 715 | - return ''; |
|
| 716 | - } |
|
| 717 | - return join(', ', $this->_to); |
|
| 718 | - } |
|
| 719 | - |
|
| 720 | - /** |
|
| 721 | - * getUniqueId |
|
| 722 | - * |
|
| 723 | - * @return string |
|
| 724 | - */ |
|
| 725 | - public function getUniqueId() |
|
| 726 | - { |
|
| 727 | - return md5(uniqid(time())); |
|
| 728 | - } |
|
| 729 | - |
|
| 730 | - /** |
|
| 731 | - * getWrapMessage |
|
| 732 | - * |
|
| 733 | - * @return string |
|
| 734 | - */ |
|
| 735 | - public function getWrapMessage() |
|
| 736 | - { |
|
| 737 | - return wordwrap($this->_message, $this->_wrap); |
|
| 738 | - } |
|
| 739 | - } |
|
| 142 | + /** |
|
| 143 | + * Set destination using array |
|
| 144 | + * @params array $emails the list of recipient. This is an associative array name => email |
|
| 145 | + * @example array('John Doe' => '[email protected]') |
|
| 146 | + * @return Object the current instance |
|
| 147 | + */ |
|
| 148 | + public function setTos(array $emails) |
|
| 149 | + { |
|
| 150 | + foreach ($emails as $name => $email) { |
|
| 151 | + if(is_numeric($name)){ |
|
| 152 | + $this->setTo($email); |
|
| 153 | + } |
|
| 154 | + else{ |
|
| 155 | + $this->setTo($email, $name); |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + return $this; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * getTo |
|
| 164 | + * |
|
| 165 | + * Return an array of formatted To addresses. |
|
| 166 | + * |
|
| 167 | + * @return array |
|
| 168 | + */ |
|
| 169 | + public function getTo() |
|
| 170 | + { |
|
| 171 | + return $this->_to; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * setCc |
|
| 177 | + * |
|
| 178 | + * @param array $pairs An array of name => email pairs. |
|
| 179 | + * |
|
| 180 | + * @return self |
|
| 181 | + */ |
|
| 182 | + public function setCc(array $pairs) |
|
| 183 | + { |
|
| 184 | + return $this->addMailHeaders('Cc', $pairs); |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * setBcc |
|
| 189 | + * |
|
| 190 | + * @param array $pairs An array of name => email pairs. |
|
| 191 | + * |
|
| 192 | + * @return self |
|
| 193 | + */ |
|
| 194 | + public function setBcc(array $pairs) |
|
| 195 | + { |
|
| 196 | + return $this->addMailHeaders('Bcc', $pairs); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * setReplyTo |
|
| 201 | + * |
|
| 202 | + * @param string $email |
|
| 203 | + * @param string $name |
|
| 204 | + * |
|
| 205 | + * @return self |
|
| 206 | + */ |
|
| 207 | + public function setReplyTo($email, $name = null) |
|
| 208 | + { |
|
| 209 | + return $this->addMailHeader('Reply-To', $email, $name); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * setHtml |
|
| 214 | + * |
|
| 215 | + * @return self |
|
| 216 | + */ |
|
| 217 | + public function setHtml() |
|
| 218 | + { |
|
| 219 | + $this->addGenericHeader( |
|
| 220 | + 'Content-Type', 'text/html; charset="utf-8"' |
|
| 221 | + ); |
|
| 222 | + return $this; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * setSubject |
|
| 227 | + * |
|
| 228 | + * @param string $subject The email subject |
|
| 229 | + * |
|
| 230 | + * @return self |
|
| 231 | + */ |
|
| 232 | + public function setSubject($subject) |
|
| 233 | + { |
|
| 234 | + $this->_subject = $this->encodeUtf8( |
|
| 235 | + $this->filterOther((string) $subject) |
|
| 236 | + ); |
|
| 237 | + return $this; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * getSubject function. |
|
| 242 | + * |
|
| 243 | + * @return string |
|
| 244 | + */ |
|
| 245 | + public function getSubject() |
|
| 246 | + { |
|
| 247 | + return $this->_subject; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * setMessage |
|
| 252 | + * |
|
| 253 | + * @param string $message The message to send. |
|
| 254 | + * |
|
| 255 | + * @return self |
|
| 256 | + */ |
|
| 257 | + public function setMessage($message) |
|
| 258 | + { |
|
| 259 | + $this->_message = str_replace("\n.", "\n..", (string) $message); |
|
| 260 | + return $this; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * getMessage |
|
| 265 | + * |
|
| 266 | + * @return string |
|
| 267 | + */ |
|
| 268 | + public function getMessage() |
|
| 269 | + { |
|
| 270 | + return $this->_message; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * addAttachment |
|
| 275 | + * |
|
| 276 | + * @param string $path The file path to the attachment. |
|
| 277 | + * @param string $filename The filename of the attachment when emailed. |
|
| 278 | + * @param string $data |
|
| 279 | + * |
|
| 280 | + * @return self |
|
| 281 | + */ |
|
| 282 | + public function addAttachment($path, $filename = null, $data = null) |
|
| 283 | + { |
|
| 284 | + if(! file_exists($path)){ |
|
| 285 | + show_error('The file [' .$path. '] does not exists.'); |
|
| 286 | + } |
|
| 287 | + $filename = empty($filename) ? basename($path) : $filename; |
|
| 288 | + $filename = $this->encodeUtf8($this->filterOther((string) $filename)); |
|
| 289 | + $data = empty($data) ? $this->getAttachmentData($path) : $data; |
|
| 290 | + $this->_attachments[] = array( |
|
| 291 | + 'path' => $path, |
|
| 292 | + 'file' => $filename, |
|
| 293 | + 'data' => chunk_split(base64_encode($data)) |
|
| 294 | + ); |
|
| 295 | + return $this; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * getAttachmentData |
|
| 300 | + * |
|
| 301 | + * @param string $path The path to the attachment file. |
|
| 302 | + * |
|
| 303 | + * @return string |
|
| 304 | + */ |
|
| 305 | + public function getAttachmentData($path) |
|
| 306 | + { |
|
| 307 | + if(! file_exists($path)){ |
|
| 308 | + show_error('The file [' .$path. '] does not exists.'); |
|
| 309 | + } |
|
| 310 | + $filesize = filesize($path); |
|
| 311 | + $handle = fopen($path, "r"); |
|
| 312 | + $attachment = null; |
|
| 313 | + if(is_resource($handle)){ |
|
| 314 | + $attachment = fread($handle, $filesize); |
|
| 315 | + fclose($handle); |
|
| 316 | + } |
|
| 317 | + return $attachment; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * addMailHeader |
|
| 322 | + * |
|
| 323 | + * @param string $header The header to add. |
|
| 324 | + * @param string $email The email to add. |
|
| 325 | + * @param string $name The name to add. |
|
| 326 | + * |
|
| 327 | + * @return self |
|
| 328 | + */ |
|
| 329 | + public function addMailHeader($header, $email, $name = null) |
|
| 330 | + { |
|
| 331 | + $address = $this->formatHeader((string) $email, (string) $name); |
|
| 332 | + $this->_headers[] = sprintf('%s: %s', (string) $header, $address); |
|
| 333 | + return $this; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * addMailHeaders |
|
| 338 | + * |
|
| 339 | + * @param string $header The header to add. |
|
| 340 | + * @param array $pairs An array of name => email pairs. |
|
| 341 | + * |
|
| 342 | + * @return self |
|
| 343 | + */ |
|
| 344 | + public function addMailHeaders($header, array $pairs) |
|
| 345 | + { |
|
| 346 | + if (count($pairs) === 0) { |
|
| 347 | + show_error('You must pass at least one name => email pair.'); |
|
| 348 | + } |
|
| 349 | + $addresses = array(); |
|
| 350 | + foreach ($pairs as $name => $email) { |
|
| 351 | + $name = is_numeric($name) ? null : $name; |
|
| 352 | + $addresses[] = $this->formatHeader($email, $name); |
|
| 353 | + } |
|
| 354 | + $this->addGenericHeader($header, implode(',', $addresses)); |
|
| 355 | + return $this; |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * addGenericHeader |
|
| 360 | + * |
|
| 361 | + * @param string $name The generic header to add. |
|
| 362 | + * @param mixed $value The value of the header. |
|
| 363 | + * |
|
| 364 | + * @return self |
|
| 365 | + */ |
|
| 366 | + public function addGenericHeader($name, $value) |
|
| 367 | + { |
|
| 368 | + $this->_headers[] = sprintf( |
|
| 369 | + '%s: %s', |
|
| 370 | + (string) $name, |
|
| 371 | + (string) $value |
|
| 372 | + ); |
|
| 373 | + return $this; |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * getHeaders |
|
| 378 | + * |
|
| 379 | + * Return the headers registered so far as an array. |
|
| 380 | + * |
|
| 381 | + * @return array |
|
| 382 | + */ |
|
| 383 | + public function getHeaders() |
|
| 384 | + { |
|
| 385 | + return $this->_headers; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * setAdditionalParameters |
|
| 390 | + * |
|
| 391 | + * Such as "[email protected] |
|
| 392 | + * |
|
| 393 | + * @param string $additionalParameters The addition mail parameter. |
|
| 394 | + * |
|
| 395 | + * @return self |
|
| 396 | + */ |
|
| 397 | + public function setParameters($additionalParameters) |
|
| 398 | + { |
|
| 399 | + $this->_params = (string) $additionalParameters; |
|
| 400 | + return $this; |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + /** |
|
| 404 | + * getAdditionalParameters |
|
| 405 | + * |
|
| 406 | + * @return string |
|
| 407 | + */ |
|
| 408 | + public function getParameters() |
|
| 409 | + { |
|
| 410 | + return $this->_params; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + /** |
|
| 414 | + * setWrap |
|
| 415 | + * |
|
| 416 | + * @param int $wrap The number of characters at which the message will wrap. |
|
| 417 | + * |
|
| 418 | + * @return self |
|
| 419 | + */ |
|
| 420 | + public function setWrap($wrap = 78) |
|
| 421 | + { |
|
| 422 | + $wrap = (int) $wrap; |
|
| 423 | + if ($wrap < 1) { |
|
| 424 | + $wrap = 78; |
|
| 425 | + } |
|
| 426 | + $this->_wrap = $wrap; |
|
| 427 | + return $this; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + /** |
|
| 431 | + * getWrap |
|
| 432 | + * |
|
| 433 | + * @return int |
|
| 434 | + */ |
|
| 435 | + public function getWrap() |
|
| 436 | + { |
|
| 437 | + return $this->_wrap; |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + /** |
|
| 441 | + * hasAttachments |
|
| 442 | + * |
|
| 443 | + * Checks if the email has any registered attachments. |
|
| 444 | + * |
|
| 445 | + * @return bool |
|
| 446 | + */ |
|
| 447 | + public function hasAttachments() |
|
| 448 | + { |
|
| 449 | + return !empty($this->_attachments); |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * assembleAttachment |
|
| 454 | + * |
|
| 455 | + * @return string |
|
| 456 | + */ |
|
| 457 | + public function assembleAttachmentHeaders() |
|
| 458 | + { |
|
| 459 | + $head = array(); |
|
| 460 | + $head[] = "MIME-Version: 1.0"; |
|
| 461 | + $head[] = "Content-Type: multipart/mixed; boundary=\"{$this->_uid}\""; |
|
| 462 | + |
|
| 463 | + return join(PHP_EOL, $head); |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + /** |
|
| 467 | + * assembleAttachmentBody |
|
| 468 | + * |
|
| 469 | + * @return string |
|
| 470 | + */ |
|
| 471 | + public function assembleAttachmentBody() |
|
| 472 | + { |
|
| 473 | + $body = array(); |
|
| 474 | + $body[] = "This is a multi-part message in MIME format."; |
|
| 475 | + $body[] = "--{$this->_uid}"; |
|
| 476 | + $body[] = "Content-Type: text/html; charset=\"utf-8\""; |
|
| 477 | + $body[] = "Content-Transfer-Encoding: quoted-printable"; |
|
| 478 | + $body[] = ""; |
|
| 479 | + $body[] = quoted_printable_encode($this->_message); |
|
| 480 | + $body[] = ""; |
|
| 481 | + $body[] = "--{$this->_uid}"; |
|
| 482 | + |
|
| 483 | + foreach ($this->_attachments as $attachment) { |
|
| 484 | + $body[] = $this->getAttachmentMimeTemplate($attachment); |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + return implode(PHP_EOL, $body) . '--'; |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + /** |
|
| 491 | + * getAttachmentMimeTemplate |
|
| 492 | + * |
|
| 493 | + * @param array $attachment An array containing 'file' and 'data' keys. |
|
| 494 | + * |
|
| 495 | + * @return string |
|
| 496 | + */ |
|
| 497 | + public function getAttachmentMimeTemplate($attachment) |
|
| 498 | + { |
|
| 499 | + $file = $attachment['file']; |
|
| 500 | + $data = $attachment['data']; |
|
| 501 | + |
|
| 502 | + $head = array(); |
|
| 503 | + $head[] = "Content-Type: application/octet-stream; name=\"{$file}\""; |
|
| 504 | + $head[] = "Content-Transfer-Encoding: base64"; |
|
| 505 | + $head[] = "Content-Disposition: attachment; filename=\"{$file}\""; |
|
| 506 | + $head[] = ""; |
|
| 507 | + $head[] = $data; |
|
| 508 | + $head[] = ""; |
|
| 509 | + $head[] = "--{$this->_uid}"; |
|
| 510 | + |
|
| 511 | + return implode(PHP_EOL, $head); |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * send the email |
|
| 516 | + * |
|
| 517 | + * @return boolean |
|
| 518 | + */ |
|
| 519 | + public function send() |
|
| 520 | + { |
|
| 521 | + $to = $this->getToForSend(); |
|
| 522 | + $headers = $this->getHeadersForSend(); |
|
| 523 | + |
|
| 524 | + if (empty($to)) { |
|
| 525 | + show_error('Unable to send, no To address has been set.'); |
|
| 526 | + } |
|
| 527 | + |
|
| 528 | + if ($this->hasAttachments()) { |
|
| 529 | + $message = $this->assembleAttachmentBody(); |
|
| 530 | + $headers .= PHP_EOL . $this->assembleAttachmentHeaders(); |
|
| 531 | + } else { |
|
| 532 | + $message = $this->getWrapMessage(); |
|
| 533 | + } |
|
| 534 | + $this->logger->info('Sending new mail, the information are listed below: destination: ' . $to . ', headers: ' . $headers . ', message: ' . $message); |
|
| 535 | + return mail($to, $this->_subject, $message, $headers, $this->_params); |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + /** |
|
| 539 | + * debug |
|
| 540 | + * |
|
| 541 | + * @return string |
|
| 542 | + */ |
|
| 543 | + public function debug() |
|
| 544 | + { |
|
| 545 | + return '<pre>' . print_r($this, true) . '</pre>'; |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + /** |
|
| 549 | + * magic __toString function |
|
| 550 | + * |
|
| 551 | + * @return string |
|
| 552 | + */ |
|
| 553 | + public function __toString() |
|
| 554 | + { |
|
| 555 | + return print_r($this, true); |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + /** |
|
| 559 | + * formatHeader |
|
| 560 | + * |
|
| 561 | + * Formats a display address for emails according to RFC2822 e.g. |
|
| 562 | + * Name <[email protected]> |
|
| 563 | + * |
|
| 564 | + * @param string $email The email address. |
|
| 565 | + * @param string $name The display name. |
|
| 566 | + * |
|
| 567 | + * @return string |
|
| 568 | + */ |
|
| 569 | + public function formatHeader($email, $name = null) |
|
| 570 | + { |
|
| 571 | + $email = $this->filterEmail((string) $email); |
|
| 572 | + if (empty($name)) { |
|
| 573 | + return $email; |
|
| 574 | + } |
|
| 575 | + $name = $this->encodeUtf8($this->filterName((string) $name)); |
|
| 576 | + return sprintf('"%s" <%s>', $name, $email); |
|
| 577 | + } |
|
| 578 | + |
|
| 579 | + /** |
|
| 580 | + * encodeUtf8 |
|
| 581 | + * |
|
| 582 | + * @param string $value The value to encode. |
|
| 583 | + * |
|
| 584 | + * @return string |
|
| 585 | + */ |
|
| 586 | + public function encodeUtf8($value) |
|
| 587 | + { |
|
| 588 | + $value = trim($value); |
|
| 589 | + if (preg_match('/(\s)/', $value)) { |
|
| 590 | + return $this->encodeUtf8Words($value); |
|
| 591 | + } |
|
| 592 | + return $this->encodeUtf8Word($value); |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * encodeUtf8Word |
|
| 597 | + * |
|
| 598 | + * @param string $value The word to encode. |
|
| 599 | + * |
|
| 600 | + * @return string |
|
| 601 | + */ |
|
| 602 | + public function encodeUtf8Word($value) |
|
| 603 | + { |
|
| 604 | + return sprintf('=?UTF-8?B?%s?=', base64_encode($value)); |
|
| 605 | + } |
|
| 606 | + |
|
| 607 | + /** |
|
| 608 | + * encodeUtf8Words |
|
| 609 | + * |
|
| 610 | + * @param string $value The words to encode. |
|
| 611 | + * |
|
| 612 | + * @return string |
|
| 613 | + */ |
|
| 614 | + public function encodeUtf8Words($value) |
|
| 615 | + { |
|
| 616 | + $words = explode(' ', $value); |
|
| 617 | + $encoded = array(); |
|
| 618 | + foreach ($words as $word) { |
|
| 619 | + $encoded[] = $this->encodeUtf8Word($word); |
|
| 620 | + } |
|
| 621 | + return join($this->encodeUtf8Word(' '), $encoded); |
|
| 622 | + } |
|
| 623 | + |
|
| 624 | + /** |
|
| 625 | + * filterEmail |
|
| 626 | + * |
|
| 627 | + * Removes any carriage return, line feed, tab, double quote, comma |
|
| 628 | + * and angle bracket characters before sanitizing the email address. |
|
| 629 | + * |
|
| 630 | + * @param string $email The email to filter. |
|
| 631 | + * |
|
| 632 | + * @return string |
|
| 633 | + */ |
|
| 634 | + public function filterEmail($email) |
|
| 635 | + { |
|
| 636 | + $rule = array( |
|
| 637 | + "\r" => '', |
|
| 638 | + "\n" => '', |
|
| 639 | + "\t" => '', |
|
| 640 | + '"' => '', |
|
| 641 | + ',' => '', |
|
| 642 | + '<' => '', |
|
| 643 | + '>' => '' |
|
| 644 | + ); |
|
| 645 | + $email = strtr($email, $rule); |
|
| 646 | + $email = filter_var($email, FILTER_SANITIZE_EMAIL); |
|
| 647 | + return $email; |
|
| 648 | + } |
|
| 649 | + |
|
| 650 | + /** |
|
| 651 | + * filterName |
|
| 652 | + * |
|
| 653 | + * Removes any carriage return, line feed or tab characters. Replaces |
|
| 654 | + * double quotes with single quotes and angle brackets with square |
|
| 655 | + * brackets, before sanitizing the string and stripping out html tags. |
|
| 656 | + * |
|
| 657 | + * @param string $name The name to filter. |
|
| 658 | + * |
|
| 659 | + * @return string |
|
| 660 | + */ |
|
| 661 | + public function filterName($name) |
|
| 662 | + { |
|
| 663 | + $rule = array( |
|
| 664 | + "\r" => '', |
|
| 665 | + "\n" => '', |
|
| 666 | + "\t" => '', |
|
| 667 | + '"' => "'", |
|
| 668 | + '<' => '[', |
|
| 669 | + '>' => ']', |
|
| 670 | + ); |
|
| 671 | + $filtered = filter_var( |
|
| 672 | + $name, |
|
| 673 | + FILTER_SANITIZE_STRING, |
|
| 674 | + FILTER_FLAG_NO_ENCODE_QUOTES |
|
| 675 | + ); |
|
| 676 | + return trim(strtr($filtered, $rule)); |
|
| 677 | + } |
|
| 678 | + |
|
| 679 | + /** |
|
| 680 | + * filterOther |
|
| 681 | + * |
|
| 682 | + * Removes ASCII control characters including any carriage return, line |
|
| 683 | + * feed or tab characters. |
|
| 684 | + * |
|
| 685 | + * @param string $data The data to filter. |
|
| 686 | + * |
|
| 687 | + * @return string |
|
| 688 | + */ |
|
| 689 | + public function filterOther($data) |
|
| 690 | + { |
|
| 691 | + return filter_var($data, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW); |
|
| 692 | + } |
|
| 693 | + |
|
| 694 | + /** |
|
| 695 | + * getHeadersForSend |
|
| 696 | + * |
|
| 697 | + * @return string |
|
| 698 | + */ |
|
| 699 | + public function getHeadersForSend() |
|
| 700 | + { |
|
| 701 | + if (empty($this->_headers)) { |
|
| 702 | + return ''; |
|
| 703 | + } |
|
| 704 | + return join(PHP_EOL, $this->_headers); |
|
| 705 | + } |
|
| 706 | + |
|
| 707 | + /** |
|
| 708 | + * getToForSend |
|
| 709 | + * |
|
| 710 | + * @return string |
|
| 711 | + */ |
|
| 712 | + public function getToForSend() |
|
| 713 | + { |
|
| 714 | + if (empty($this->_to)) { |
|
| 715 | + return ''; |
|
| 716 | + } |
|
| 717 | + return join(', ', $this->_to); |
|
| 718 | + } |
|
| 719 | + |
|
| 720 | + /** |
|
| 721 | + * getUniqueId |
|
| 722 | + * |
|
| 723 | + * @return string |
|
| 724 | + */ |
|
| 725 | + public function getUniqueId() |
|
| 726 | + { |
|
| 727 | + return md5(uniqid(time())); |
|
| 728 | + } |
|
| 729 | + |
|
| 730 | + /** |
|
| 731 | + * getWrapMessage |
|
| 732 | + * |
|
| 733 | + * @return string |
|
| 734 | + */ |
|
| 735 | + public function getWrapMessage() |
|
| 736 | + { |
|
| 737 | + return wordwrap($this->_message, $this->_wrap); |
|
| 738 | + } |
|
| 739 | + } |
|
@@ -1,112 +1,112 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - class Cookie{ |
|
| 27 | + class Cookie{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The logger instance |
|
| 31 | - * @var Log |
|
| 32 | - */ |
|
| 33 | - private static $logger; |
|
| 29 | + /** |
|
| 30 | + * The logger instance |
|
| 31 | + * @var Log |
|
| 32 | + */ |
|
| 33 | + private static $logger; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * The signleton of the logger |
|
| 37 | - * @return Object the Log instance |
|
| 38 | - */ |
|
| 39 | - private static function getLogger(){ |
|
| 40 | - if(self::$logger == null){ |
|
| 41 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 42 | - self::$logger[0]->setLogger('Library::Cookie'); |
|
| 43 | - } |
|
| 44 | - return self::$logger[0]; |
|
| 45 | - } |
|
| 35 | + /** |
|
| 36 | + * The signleton of the logger |
|
| 37 | + * @return Object the Log instance |
|
| 38 | + */ |
|
| 39 | + private static function getLogger(){ |
|
| 40 | + if(self::$logger == null){ |
|
| 41 | + self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 42 | + self::$logger[0]->setLogger('Library::Cookie'); |
|
| 43 | + } |
|
| 44 | + return self::$logger[0]; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Get the cookie item value |
|
| 49 | - * @param string $item the cookie item name to get |
|
| 50 | - * @param mixed $default the default value to use if can not find the cokkie item in the list |
|
| 51 | - * @return mixed the cookie value if exist or the default value |
|
| 52 | - */ |
|
| 53 | - public static function get($item, $default = null){ |
|
| 54 | - $logger = self::getLogger(); |
|
| 55 | - if(array_key_exists($item, $_COOKIE)){ |
|
| 56 | - return $_COOKIE[$item]; |
|
| 57 | - } |
|
| 58 | - $logger->warning('Cannot find cookie item [' . $item . '], using the default value [' . $default . ']'); |
|
| 59 | - return $default; |
|
| 60 | - } |
|
| 47 | + /** |
|
| 48 | + * Get the cookie item value |
|
| 49 | + * @param string $item the cookie item name to get |
|
| 50 | + * @param mixed $default the default value to use if can not find the cokkie item in the list |
|
| 51 | + * @return mixed the cookie value if exist or the default value |
|
| 52 | + */ |
|
| 53 | + public static function get($item, $default = null){ |
|
| 54 | + $logger = self::getLogger(); |
|
| 55 | + if(array_key_exists($item, $_COOKIE)){ |
|
| 56 | + return $_COOKIE[$item]; |
|
| 57 | + } |
|
| 58 | + $logger->warning('Cannot find cookie item [' . $item . '], using the default value [' . $default . ']'); |
|
| 59 | + return $default; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Set the cookie item value |
|
| 64 | - * @param string $name the cookie item name |
|
| 65 | - * @param string $value the cookie value to set |
|
| 66 | - * @param integer $expire the time to live for this cookie |
|
| 67 | - * @param string $path the path that the cookie will be available |
|
| 68 | - * @param string $domain the domain that the cookie will be available |
|
| 69 | - * @param boolean $secure if this cookie will be available on secure connection or not |
|
| 70 | - * @param boolean $httponly if this cookie will be available under HTTP protocol. |
|
| 71 | - */ |
|
| 72 | - public static function set($name, $value = '', $expire = 0, $path = '/', $domain = '', $secure = false, $httponly = false){ |
|
| 73 | - if(headers_sent()){ |
|
| 74 | - show_error('There exists a cookie that we wanted to create that we couldn\'t |
|
| 62 | + /** |
|
| 63 | + * Set the cookie item value |
|
| 64 | + * @param string $name the cookie item name |
|
| 65 | + * @param string $value the cookie value to set |
|
| 66 | + * @param integer $expire the time to live for this cookie |
|
| 67 | + * @param string $path the path that the cookie will be available |
|
| 68 | + * @param string $domain the domain that the cookie will be available |
|
| 69 | + * @param boolean $secure if this cookie will be available on secure connection or not |
|
| 70 | + * @param boolean $httponly if this cookie will be available under HTTP protocol. |
|
| 71 | + */ |
|
| 72 | + public static function set($name, $value = '', $expire = 0, $path = '/', $domain = '', $secure = false, $httponly = false){ |
|
| 73 | + if(headers_sent()){ |
|
| 74 | + show_error('There exists a cookie that we wanted to create that we couldn\'t |
|
| 75 | 75 | because headers was already sent. Make sure to do this first |
| 76 | 76 | before outputing anything.'); |
| 77 | - } |
|
| 78 | - $timestamp = $expire; |
|
| 79 | - if($expire){ |
|
| 80 | - $timestamp = time() + $expire; |
|
| 81 | - } |
|
| 82 | - setcookie($name, $value, $timestamp, $path, $domain, $secure, $httponly); |
|
| 83 | - } |
|
| 77 | + } |
|
| 78 | + $timestamp = $expire; |
|
| 79 | + if($expire){ |
|
| 80 | + $timestamp = time() + $expire; |
|
| 81 | + } |
|
| 82 | + setcookie($name, $value, $timestamp, $path, $domain, $secure, $httponly); |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * Delete the cookie item in the list |
|
| 87 | - * @param string $item the cookie item name to be cleared |
|
| 88 | - * @return boolean true if the item exists and is deleted successfully otherwise will return false. |
|
| 89 | - */ |
|
| 90 | - public static function delete($item){ |
|
| 91 | - $logger = self::getLogger(); |
|
| 92 | - if(array_key_exists($item, $_COOKIE)){ |
|
| 93 | - $logger->info('Delete cookie item ['.$item.']'); |
|
| 94 | - unset($_COOKIE[$item]); |
|
| 95 | - return true; |
|
| 96 | - } |
|
| 97 | - else{ |
|
| 98 | - $logger->warning('Cookie item ['.$item.'] to be deleted does not exists'); |
|
| 99 | - return false; |
|
| 100 | - } |
|
| 101 | - } |
|
| 85 | + /** |
|
| 86 | + * Delete the cookie item in the list |
|
| 87 | + * @param string $item the cookie item name to be cleared |
|
| 88 | + * @return boolean true if the item exists and is deleted successfully otherwise will return false. |
|
| 89 | + */ |
|
| 90 | + public static function delete($item){ |
|
| 91 | + $logger = self::getLogger(); |
|
| 92 | + if(array_key_exists($item, $_COOKIE)){ |
|
| 93 | + $logger->info('Delete cookie item ['.$item.']'); |
|
| 94 | + unset($_COOKIE[$item]); |
|
| 95 | + return true; |
|
| 96 | + } |
|
| 97 | + else{ |
|
| 98 | + $logger->warning('Cookie item ['.$item.'] to be deleted does not exists'); |
|
| 99 | + return false; |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * Check if the given cookie item exists |
|
| 105 | - * @param string $item the cookie item name |
|
| 106 | - * @return boolean true if the cookie item is set, false or not |
|
| 107 | - */ |
|
| 108 | - public static function exists($item){ |
|
| 109 | - return array_key_exists($item, $_COOKIE); |
|
| 110 | - } |
|
| 103 | + /** |
|
| 104 | + * Check if the given cookie item exists |
|
| 105 | + * @param string $item the cookie item name |
|
| 106 | + * @return boolean true if the cookie item is set, false or not |
|
| 107 | + */ |
|
| 108 | + public static function exists($item){ |
|
| 109 | + return array_key_exists($item, $_COOKIE); |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - } |
|
| 112 | + } |
|
@@ -1,65 +1,65 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - class StringHash{ |
|
| 27 | + class StringHash{ |
|
| 28 | 28 | |
| 29 | - //blowfish |
|
| 30 | - private static $algo = '$2a'; |
|
| 29 | + //blowfish |
|
| 30 | + private static $algo = '$2a'; |
|
| 31 | 31 | |
| 32 | - //cost parameter |
|
| 33 | - private static $cost = '$10'; |
|
| 32 | + //cost parameter |
|
| 33 | + private static $cost = '$10'; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Get the unique salt for the string hash |
|
| 37 | - * @return string the unique generated salt |
|
| 38 | - */ |
|
| 39 | - private static function uniqueSalt() { |
|
| 40 | - return substr(sha1(mt_rand()), 0, 22); |
|
| 41 | - } |
|
| 35 | + /** |
|
| 36 | + * Get the unique salt for the string hash |
|
| 37 | + * @return string the unique generated salt |
|
| 38 | + */ |
|
| 39 | + private static function uniqueSalt() { |
|
| 40 | + return substr(sha1(mt_rand()), 0, 22); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Hash the given string |
|
| 45 | - * @param string $value the plain string text to be hashed |
|
| 46 | - * @return string the hashed string |
|
| 47 | - */ |
|
| 48 | - public static function hash($value) { |
|
| 49 | - return crypt($value, self::$algo . |
|
| 50 | - self::$cost . |
|
| 51 | - '$' . self::uniqueSalt()); |
|
| 52 | - } |
|
| 43 | + /** |
|
| 44 | + * Hash the given string |
|
| 45 | + * @param string $value the plain string text to be hashed |
|
| 46 | + * @return string the hashed string |
|
| 47 | + */ |
|
| 48 | + public static function hash($value) { |
|
| 49 | + return crypt($value, self::$algo . |
|
| 50 | + self::$cost . |
|
| 51 | + '$' . self::uniqueSalt()); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Check if the hash and plain string is valid |
|
| 56 | - * @param string $hash the hashed string |
|
| 57 | - * @param string $plain the plain text |
|
| 58 | - * @return boolean true if is valid or false if not |
|
| 59 | - */ |
|
| 60 | - public static function check($hash, $plain) { |
|
| 61 | - $full_salt = substr($hash, 0, 29); |
|
| 62 | - $new_hash = crypt($plain, $full_salt); |
|
| 63 | - return ($hash === $new_hash); |
|
| 64 | - } |
|
| 65 | - } |
|
| 66 | 54 | \ No newline at end of file |
| 55 | + /** |
|
| 56 | + * Check if the hash and plain string is valid |
|
| 57 | + * @param string $hash the hashed string |
|
| 58 | + * @param string $plain the plain text |
|
| 59 | + * @return boolean true if is valid or false if not |
|
| 60 | + */ |
|
| 61 | + public static function check($hash, $plain) { |
|
| 62 | + $full_salt = substr($hash, 0, 29); |
|
| 63 | + $new_hash = crypt($plain, $full_salt); |
|
| 64 | + return ($hash === $new_hash); |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | 67 | \ No newline at end of file |