@@ -1,320 +1,320 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | 27 | |
| 28 | - class Form{ |
|
| 28 | + class Form{ |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Generate the form opened tag |
|
| 32 | - * @param string $path the form action path |
|
| 33 | - * @param array $attributes the additional form attributes |
|
| 34 | - * @param string $method the form method like 'GET', 'POST' |
|
| 35 | - * @param string $enctype the form enctype like "multipart/form-data" |
|
| 36 | - * @return string the generated form html |
|
| 37 | - */ |
|
| 38 | - public static function open($path = null, array $attributes = array(), $method = 'POST', $enctype = null){ |
|
| 39 | - if($path){ |
|
| 40 | - $path = Url::site_url($path); |
|
| 41 | - } |
|
| 42 | - $method = strtoupper($method); |
|
| 43 | - $str = null; |
|
| 44 | - $str .= '<form action = "'.$path.'" method = "'.$method.'"'; |
|
| 45 | - if(! empty($enctype)){ |
|
| 46 | - $str .= ' enctype = "'.$enctype.'" '; |
|
| 47 | - } |
|
| 48 | - if(! isset($attributes['accept-charset'])){ |
|
| 49 | - $attributes['accept-charset'] = get_config('charset', 'utf-8'); |
|
| 50 | - } |
|
| 51 | - $str .= attributes_to_string($attributes); |
|
| 52 | - $str .= '>'; |
|
| 53 | - //if CSRF is enabled in the configuration |
|
| 54 | - if(get_config('csrf_enable', false) && $method == 'POST'){ |
|
| 55 | - $csrfValue = Security::generateCSRF(); |
|
| 56 | - $csrfName = get_config('csrf_key', 'csrf_key'); |
|
| 57 | - $str .= static::hidden($csrfName, $csrfValue); |
|
| 58 | - } |
|
| 59 | - return $str; |
|
| 60 | - } |
|
| 30 | + /** |
|
| 31 | + * Generate the form opened tag |
|
| 32 | + * @param string $path the form action path |
|
| 33 | + * @param array $attributes the additional form attributes |
|
| 34 | + * @param string $method the form method like 'GET', 'POST' |
|
| 35 | + * @param string $enctype the form enctype like "multipart/form-data" |
|
| 36 | + * @return string the generated form html |
|
| 37 | + */ |
|
| 38 | + public static function open($path = null, array $attributes = array(), $method = 'POST', $enctype = null){ |
|
| 39 | + if($path){ |
|
| 40 | + $path = Url::site_url($path); |
|
| 41 | + } |
|
| 42 | + $method = strtoupper($method); |
|
| 43 | + $str = null; |
|
| 44 | + $str .= '<form action = "'.$path.'" method = "'.$method.'"'; |
|
| 45 | + if(! empty($enctype)){ |
|
| 46 | + $str .= ' enctype = "'.$enctype.'" '; |
|
| 47 | + } |
|
| 48 | + if(! isset($attributes['accept-charset'])){ |
|
| 49 | + $attributes['accept-charset'] = get_config('charset', 'utf-8'); |
|
| 50 | + } |
|
| 51 | + $str .= attributes_to_string($attributes); |
|
| 52 | + $str .= '>'; |
|
| 53 | + //if CSRF is enabled in the configuration |
|
| 54 | + if(get_config('csrf_enable', false) && $method == 'POST'){ |
|
| 55 | + $csrfValue = Security::generateCSRF(); |
|
| 56 | + $csrfName = get_config('csrf_key', 'csrf_key'); |
|
| 57 | + $str .= static::hidden($csrfName, $csrfValue); |
|
| 58 | + } |
|
| 59 | + return $str; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Generate the form opened tag for multipart like to send a file |
|
| 64 | - * @see Form::open() for more details |
|
| 65 | - * @return string the generated multipart form html |
|
| 66 | - */ |
|
| 67 | - public static function openMultipart($path = null, array $attributes = array(), $method = 'POST'){ |
|
| 68 | - return self::open($path, $attributes, $method, 'multipart/form-data'); |
|
| 69 | - } |
|
| 62 | + /** |
|
| 63 | + * Generate the form opened tag for multipart like to send a file |
|
| 64 | + * @see Form::open() for more details |
|
| 65 | + * @return string the generated multipart form html |
|
| 66 | + */ |
|
| 67 | + public static function openMultipart($path = null, array $attributes = array(), $method = 'POST'){ |
|
| 68 | + return self::open($path, $attributes, $method, 'multipart/form-data'); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Generate the form close |
|
| 73 | - * @return string the form close html |
|
| 74 | - */ |
|
| 75 | - public static function close(){ |
|
| 76 | - return '</form>'; |
|
| 77 | - } |
|
| 71 | + /** |
|
| 72 | + * Generate the form close |
|
| 73 | + * @return string the form close html |
|
| 74 | + */ |
|
| 75 | + public static function close(){ |
|
| 76 | + return '</form>'; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * Generate the form fieldset & legend |
|
| 81 | - * @param string $legend the legend tag value |
|
| 82 | - * @param array $fieldsetAttributes the fieldset additional HTML attributes |
|
| 83 | - * @param array $legendAttributes the legend additional HTML attributes. Is used only is $legend is not empty |
|
| 84 | - * @return string the generated fieldset value |
|
| 85 | - */ |
|
| 86 | - public static function fieldset($legend = '', array $fieldsetAttributes = array(), array $legendAttributes = array()){ |
|
| 87 | - $str = '<fieldset' . attributes_to_string($fieldsetAttributes) . '>'; |
|
| 88 | - if($legend){ |
|
| 89 | - $str .= '<legend' . attributes_to_string($legendAttributes) . '>'.$legend.'</legend>'; |
|
| 90 | - } |
|
| 91 | - return $str; |
|
| 92 | - } |
|
| 79 | + /** |
|
| 80 | + * Generate the form fieldset & legend |
|
| 81 | + * @param string $legend the legend tag value |
|
| 82 | + * @param array $fieldsetAttributes the fieldset additional HTML attributes |
|
| 83 | + * @param array $legendAttributes the legend additional HTML attributes. Is used only is $legend is not empty |
|
| 84 | + * @return string the generated fieldset value |
|
| 85 | + */ |
|
| 86 | + public static function fieldset($legend = '', array $fieldsetAttributes = array(), array $legendAttributes = array()){ |
|
| 87 | + $str = '<fieldset' . attributes_to_string($fieldsetAttributes) . '>'; |
|
| 88 | + if($legend){ |
|
| 89 | + $str .= '<legend' . attributes_to_string($legendAttributes) . '>'.$legend.'</legend>'; |
|
| 90 | + } |
|
| 91 | + return $str; |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Generate the fieldset close tag |
|
| 96 | - * @return string the generated html for fieldset close |
|
| 97 | - */ |
|
| 98 | - public static function fieldsetClose(){ |
|
| 99 | - return '</fieldset>'; |
|
| 100 | - } |
|
| 94 | + /** |
|
| 95 | + * Generate the fieldset close tag |
|
| 96 | + * @return string the generated html for fieldset close |
|
| 97 | + */ |
|
| 98 | + public static function fieldsetClose(){ |
|
| 99 | + return '</fieldset>'; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Get the error message for the given form field name. |
|
| 104 | - * This use the form validation information to get the error information. |
|
| 105 | - * @param string $name the form field name |
|
| 106 | - * @return string the error message if exists and null if not |
|
| 107 | - */ |
|
| 108 | - public static function error($name){ |
|
| 109 | - $return = null; |
|
| 110 | - $obj = & get_instance(); |
|
| 111 | - if(isset($obj->formvalidation)){ |
|
| 112 | - $errors = $obj->formvalidation->returnErrors(); |
|
| 113 | - $error = isset($errors[$name]) ? $errors[$name] : null; |
|
| 114 | - if($error){ |
|
| 115 | - list($errorStart, $errorEnd) = $obj->formvalidation->getErrorDelimiter(); |
|
| 116 | - $return = $errorStart . $error . $errorEnd; |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - return $return; |
|
| 120 | - } |
|
| 102 | + /** |
|
| 103 | + * Get the error message for the given form field name. |
|
| 104 | + * This use the form validation information to get the error information. |
|
| 105 | + * @param string $name the form field name |
|
| 106 | + * @return string the error message if exists and null if not |
|
| 107 | + */ |
|
| 108 | + public static function error($name){ |
|
| 109 | + $return = null; |
|
| 110 | + $obj = & get_instance(); |
|
| 111 | + if(isset($obj->formvalidation)){ |
|
| 112 | + $errors = $obj->formvalidation->returnErrors(); |
|
| 113 | + $error = isset($errors[$name]) ? $errors[$name] : null; |
|
| 114 | + if($error){ |
|
| 115 | + list($errorStart, $errorEnd) = $obj->formvalidation->getErrorDelimiter(); |
|
| 116 | + $return = $errorStart . $error . $errorEnd; |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + return $return; |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - /** |
|
| 123 | - * Get the form field value |
|
| 124 | - * @param string $name the form field name |
|
| 125 | - * @param mixed $default the default value if can not found the given form field name |
|
| 126 | - * @return mixed the form field value if is set, otherwise return the default value. |
|
| 127 | - */ |
|
| 128 | - public static function value($name, $default = null){ |
|
| 129 | - $value = get_instance()->request->query($name); |
|
| 130 | - return $value ? $value : $default; |
|
| 131 | - } |
|
| 122 | + /** |
|
| 123 | + * Get the form field value |
|
| 124 | + * @param string $name the form field name |
|
| 125 | + * @param mixed $default the default value if can not found the given form field name |
|
| 126 | + * @return mixed the form field value if is set, otherwise return the default value. |
|
| 127 | + */ |
|
| 128 | + public static function value($name, $default = null){ |
|
| 129 | + $value = get_instance()->request->query($name); |
|
| 130 | + return $value ? $value : $default; |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - /** |
|
| 134 | - * Generate the form label html content |
|
| 135 | - * @param string $label the title of the label |
|
| 136 | - * @param string $for the value of the label "for" attribute |
|
| 137 | - * @param array $attributes the additional attributes to be added |
|
| 138 | - * @return string the generated label html content |
|
| 139 | - */ |
|
| 140 | - public static function label($label, $for = '', array $attributes = array()){ |
|
| 141 | - $str = '<label'; |
|
| 142 | - if($for){ |
|
| 143 | - $str .= ' for = "'.$for.'"'; |
|
| 144 | - } |
|
| 145 | - $str .= attributes_to_string($attributes); |
|
| 146 | - $str .= '>'; |
|
| 147 | - $str .= $label.'</label>'; |
|
| 148 | - return $str; |
|
| 149 | - } |
|
| 133 | + /** |
|
| 134 | + * Generate the form label html content |
|
| 135 | + * @param string $label the title of the label |
|
| 136 | + * @param string $for the value of the label "for" attribute |
|
| 137 | + * @param array $attributes the additional attributes to be added |
|
| 138 | + * @return string the generated label html content |
|
| 139 | + */ |
|
| 140 | + public static function label($label, $for = '', array $attributes = array()){ |
|
| 141 | + $str = '<label'; |
|
| 142 | + if($for){ |
|
| 143 | + $str .= ' for = "'.$for.'"'; |
|
| 144 | + } |
|
| 145 | + $str .= attributes_to_string($attributes); |
|
| 146 | + $str .= '>'; |
|
| 147 | + $str .= $label.'</label>'; |
|
| 148 | + return $str; |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - /** |
|
| 152 | - * Generate the form field for input like "text", "email", "password", etc. |
|
| 153 | - * @param string $name the form field name |
|
| 154 | - * @param mixed $value the form field value to be set |
|
| 155 | - * @param array $attributes the additional attributes to be added in the form input |
|
| 156 | - * @param string $type the type of the form field (password, text, submit, button, etc.) |
|
| 157 | - * @return string the generated form field html content for the input |
|
| 158 | - */ |
|
| 159 | - public static function input($name, $value = null, array $attributes = array(), $type = 'text'){ |
|
| 160 | - $str = null; |
|
| 161 | - $str .= '<input name = "'.$name.'" value = "'.$value.'" type = "'.$type.'"'; |
|
| 162 | - $str .= attributes_to_string($attributes); |
|
| 163 | - $str .= '/>'; |
|
| 164 | - return $str; |
|
| 165 | - } |
|
| 151 | + /** |
|
| 152 | + * Generate the form field for input like "text", "email", "password", etc. |
|
| 153 | + * @param string $name the form field name |
|
| 154 | + * @param mixed $value the form field value to be set |
|
| 155 | + * @param array $attributes the additional attributes to be added in the form input |
|
| 156 | + * @param string $type the type of the form field (password, text, submit, button, etc.) |
|
| 157 | + * @return string the generated form field html content for the input |
|
| 158 | + */ |
|
| 159 | + public static function input($name, $value = null, array $attributes = array(), $type = 'text'){ |
|
| 160 | + $str = null; |
|
| 161 | + $str .= '<input name = "'.$name.'" value = "'.$value.'" type = "'.$type.'"'; |
|
| 162 | + $str .= attributes_to_string($attributes); |
|
| 163 | + $str .= '/>'; |
|
| 164 | + return $str; |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * Generate the form field for "text" |
|
| 169 | - * @see Form::input() for more details |
|
| 170 | - */ |
|
| 171 | - public static function text($name, $value = null, array $attributes = array()){ |
|
| 172 | - return self::input($name, $value, $attributes, 'text'); |
|
| 173 | - } |
|
| 167 | + /** |
|
| 168 | + * Generate the form field for "text" |
|
| 169 | + * @see Form::input() for more details |
|
| 170 | + */ |
|
| 171 | + public static function text($name, $value = null, array $attributes = array()){ |
|
| 172 | + return self::input($name, $value, $attributes, 'text'); |
|
| 173 | + } |
|
| 174 | 174 | |
| 175 | - /** |
|
| 176 | - * Generate the form field for "password" |
|
| 177 | - * @see Form::input() for more details |
|
| 178 | - */ |
|
| 179 | - public static function password($name, $value = null, array $attributes = array()){ |
|
| 180 | - return self::input($name, $value, $attributes, 'password'); |
|
| 181 | - } |
|
| 175 | + /** |
|
| 176 | + * Generate the form field for "password" |
|
| 177 | + * @see Form::input() for more details |
|
| 178 | + */ |
|
| 179 | + public static function password($name, $value = null, array $attributes = array()){ |
|
| 180 | + return self::input($name, $value, $attributes, 'password'); |
|
| 181 | + } |
|
| 182 | 182 | |
| 183 | - /** |
|
| 184 | - * Generate the form field for "radio" |
|
| 185 | - * @see Form::input() for more details |
|
| 186 | - */ |
|
| 187 | - public static function radio($name, $value = null, $checked = false, array $attributes = array()){ |
|
| 188 | - if($checked){ |
|
| 189 | - $attributes['checked'] = true; |
|
| 190 | - } |
|
| 191 | - return self::input($name, $value, $attributes, 'radio'); |
|
| 192 | - } |
|
| 183 | + /** |
|
| 184 | + * Generate the form field for "radio" |
|
| 185 | + * @see Form::input() for more details |
|
| 186 | + */ |
|
| 187 | + public static function radio($name, $value = null, $checked = false, array $attributes = array()){ |
|
| 188 | + if($checked){ |
|
| 189 | + $attributes['checked'] = true; |
|
| 190 | + } |
|
| 191 | + return self::input($name, $value, $attributes, 'radio'); |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | - /** |
|
| 195 | - * Generate the form field for "checkbox" |
|
| 196 | - * @see Form::input() for more details |
|
| 197 | - */ |
|
| 198 | - public static function checkbox($name, $value = null, $checked = false, array $attributes = array()){ |
|
| 199 | - if($checked){ |
|
| 200 | - $attributes['checked'] = true; |
|
| 201 | - } |
|
| 202 | - return self::input($name, $value, $attributes, 'checkbox'); |
|
| 203 | - } |
|
| 194 | + /** |
|
| 195 | + * Generate the form field for "checkbox" |
|
| 196 | + * @see Form::input() for more details |
|
| 197 | + */ |
|
| 198 | + public static function checkbox($name, $value = null, $checked = false, array $attributes = array()){ |
|
| 199 | + if($checked){ |
|
| 200 | + $attributes['checked'] = true; |
|
| 201 | + } |
|
| 202 | + return self::input($name, $value, $attributes, 'checkbox'); |
|
| 203 | + } |
|
| 204 | 204 | |
| 205 | - /** |
|
| 206 | - * Generate the form field for "number" |
|
| 207 | - * @see Form::input() for more details |
|
| 208 | - */ |
|
| 209 | - public static function number($name, $value = null, array $attributes = array()){ |
|
| 210 | - return self::input($name, $value, $attributes, 'number'); |
|
| 211 | - } |
|
| 205 | + /** |
|
| 206 | + * Generate the form field for "number" |
|
| 207 | + * @see Form::input() for more details |
|
| 208 | + */ |
|
| 209 | + public static function number($name, $value = null, array $attributes = array()){ |
|
| 210 | + return self::input($name, $value, $attributes, 'number'); |
|
| 211 | + } |
|
| 212 | 212 | |
| 213 | - /** |
|
| 214 | - * Generate the form field for "phone" |
|
| 215 | - * @see Form::input() for more details |
|
| 216 | - */ |
|
| 217 | - public static function phone($name, $value = null, array $attributes = array()){ |
|
| 218 | - return self::input($name, $value, $attributes, 'phone'); |
|
| 219 | - } |
|
| 213 | + /** |
|
| 214 | + * Generate the form field for "phone" |
|
| 215 | + * @see Form::input() for more details |
|
| 216 | + */ |
|
| 217 | + public static function phone($name, $value = null, array $attributes = array()){ |
|
| 218 | + return self::input($name, $value, $attributes, 'phone'); |
|
| 219 | + } |
|
| 220 | 220 | |
| 221 | - /** |
|
| 222 | - * Generate the form field for "email" |
|
| 223 | - * @see Form::input() for more details |
|
| 224 | - */ |
|
| 225 | - public static function email($name, $value = null, array $attributes = array()){ |
|
| 226 | - return self::input($name, $value, $attributes, 'email'); |
|
| 227 | - } |
|
| 221 | + /** |
|
| 222 | + * Generate the form field for "email" |
|
| 223 | + * @see Form::input() for more details |
|
| 224 | + */ |
|
| 225 | + public static function email($name, $value = null, array $attributes = array()){ |
|
| 226 | + return self::input($name, $value, $attributes, 'email'); |
|
| 227 | + } |
|
| 228 | 228 | |
| 229 | - /** |
|
| 230 | - * Generate the form field for "search" |
|
| 231 | - * @see Form::input() for more details |
|
| 232 | - */ |
|
| 233 | - public static function search($name, $value = null, array $attributes = array()){ |
|
| 234 | - return self::input($name, $value, $attributes, 'search'); |
|
| 235 | - } |
|
| 229 | + /** |
|
| 230 | + * Generate the form field for "search" |
|
| 231 | + * @see Form::input() for more details |
|
| 232 | + */ |
|
| 233 | + public static function search($name, $value = null, array $attributes = array()){ |
|
| 234 | + return self::input($name, $value, $attributes, 'search'); |
|
| 235 | + } |
|
| 236 | 236 | |
| 237 | - /** |
|
| 238 | - * Generate the form field for "hidden" |
|
| 239 | - * @see Form::input() for more details |
|
| 240 | - */ |
|
| 241 | - public static function hidden($name, $value = null, array $attributes = array()){ |
|
| 242 | - return self::input($name, $value, $attributes, 'hidden'); |
|
| 243 | - } |
|
| 237 | + /** |
|
| 238 | + * Generate the form field for "hidden" |
|
| 239 | + * @see Form::input() for more details |
|
| 240 | + */ |
|
| 241 | + public static function hidden($name, $value = null, array $attributes = array()){ |
|
| 242 | + return self::input($name, $value, $attributes, 'hidden'); |
|
| 243 | + } |
|
| 244 | 244 | |
| 245 | - /** |
|
| 246 | - * Generate the form field for "file" |
|
| 247 | - * @see Form::input() for more details |
|
| 248 | - */ |
|
| 249 | - public static function file($name, array $attributes = array()){ |
|
| 250 | - return self::input($name, null, $attributes, 'file'); |
|
| 251 | - } |
|
| 245 | + /** |
|
| 246 | + * Generate the form field for "file" |
|
| 247 | + * @see Form::input() for more details |
|
| 248 | + */ |
|
| 249 | + public static function file($name, array $attributes = array()){ |
|
| 250 | + return self::input($name, null, $attributes, 'file'); |
|
| 251 | + } |
|
| 252 | 252 | |
| 253 | - /** |
|
| 254 | - * Generate the form field for "button" |
|
| 255 | - * @see Form::input() for more details |
|
| 256 | - */ |
|
| 257 | - public static function button($name, $value = null, array $attributes = array()){ |
|
| 258 | - return self::input($name, $value, $attributes, 'button'); |
|
| 259 | - } |
|
| 253 | + /** |
|
| 254 | + * Generate the form field for "button" |
|
| 255 | + * @see Form::input() for more details |
|
| 256 | + */ |
|
| 257 | + public static function button($name, $value = null, array $attributes = array()){ |
|
| 258 | + return self::input($name, $value, $attributes, 'button'); |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | - /** |
|
| 262 | - * Generate the form field for "reset" |
|
| 263 | - * @see Form::input() for more details |
|
| 264 | - */ |
|
| 265 | - public static function reset($name, $value = null, array $attributes = array()){ |
|
| 266 | - return self::input($name, $value, $attributes, 'reset'); |
|
| 267 | - } |
|
| 261 | + /** |
|
| 262 | + * Generate the form field for "reset" |
|
| 263 | + * @see Form::input() for more details |
|
| 264 | + */ |
|
| 265 | + public static function reset($name, $value = null, array $attributes = array()){ |
|
| 266 | + return self::input($name, $value, $attributes, 'reset'); |
|
| 267 | + } |
|
| 268 | 268 | |
| 269 | - /** |
|
| 270 | - * Generate the form field for "submit" |
|
| 271 | - * @see Form::input() for more details |
|
| 272 | - */ |
|
| 273 | - public static function submit($name, $value = null, array $attributes = array()){ |
|
| 274 | - return self::input($name, $value, $attributes, 'submit'); |
|
| 275 | - } |
|
| 269 | + /** |
|
| 270 | + * Generate the form field for "submit" |
|
| 271 | + * @see Form::input() for more details |
|
| 272 | + */ |
|
| 273 | + public static function submit($name, $value = null, array $attributes = array()){ |
|
| 274 | + return self::input($name, $value, $attributes, 'submit'); |
|
| 275 | + } |
|
| 276 | 276 | |
| 277 | - /** |
|
| 278 | - * Generate the form field for textarea |
|
| 279 | - * @param string $name the name of the textarea field |
|
| 280 | - * @param string $value the textarea field value |
|
| 281 | - * @param array $attributes the additional attributes to be added |
|
| 282 | - * @return string the generated textarea form html content |
|
| 283 | - */ |
|
| 284 | - public static function textarea($name, $value = '', array $attributes = array()){ |
|
| 285 | - $str = null; |
|
| 286 | - $str .= '<textarea name = "'.$name.'"'; |
|
| 287 | - $str .= attributes_to_string($attributes); |
|
| 288 | - $str .= '>'; |
|
| 289 | - $str .= $value.'</textarea>'; |
|
| 290 | - return $str; |
|
| 291 | - } |
|
| 277 | + /** |
|
| 278 | + * Generate the form field for textarea |
|
| 279 | + * @param string $name the name of the textarea field |
|
| 280 | + * @param string $value the textarea field value |
|
| 281 | + * @param array $attributes the additional attributes to be added |
|
| 282 | + * @return string the generated textarea form html content |
|
| 283 | + */ |
|
| 284 | + public static function textarea($name, $value = '', array $attributes = array()){ |
|
| 285 | + $str = null; |
|
| 286 | + $str .= '<textarea name = "'.$name.'"'; |
|
| 287 | + $str .= attributes_to_string($attributes); |
|
| 288 | + $str .= '>'; |
|
| 289 | + $str .= $value.'</textarea>'; |
|
| 290 | + return $str; |
|
| 291 | + } |
|
| 292 | 292 | |
| 293 | - /** |
|
| 294 | - * Generate the form field for select |
|
| 295 | - * @param string $name the name of the form field |
|
| 296 | - * @param mixed|array $values the values used to populate the "option" tags |
|
| 297 | - * @param mixed $selected the selected value in the option list |
|
| 298 | - * @param array $attributes the additional attribute to be added |
|
| 299 | - * @return string the generated form field html content for select |
|
| 300 | - */ |
|
| 301 | - public static function select($name, $values = null, $selected = null, array $attributes = array()){ |
|
| 302 | - if(! is_array($values)){ |
|
| 303 | - $values = array('' => $values); |
|
| 304 | - } |
|
| 305 | - $str = null; |
|
| 306 | - $str .= '<select name = "'.$name.'"'; |
|
| 307 | - $str .= attributes_to_string($attributes); |
|
| 308 | - $str .= '>'; |
|
| 309 | - foreach($values as $key => $val){ |
|
| 310 | - $select = ''; |
|
| 311 | - if($key == $selected){ |
|
| 312 | - $select = 'selected'; |
|
| 313 | - } |
|
| 314 | - $str .= '<option value = "'.$key.'" '.$select.'>'.$val.'</option>'; |
|
| 315 | - } |
|
| 316 | - $str .= '</select>'; |
|
| 317 | - return $str; |
|
| 318 | - } |
|
| 293 | + /** |
|
| 294 | + * Generate the form field for select |
|
| 295 | + * @param string $name the name of the form field |
|
| 296 | + * @param mixed|array $values the values used to populate the "option" tags |
|
| 297 | + * @param mixed $selected the selected value in the option list |
|
| 298 | + * @param array $attributes the additional attribute to be added |
|
| 299 | + * @return string the generated form field html content for select |
|
| 300 | + */ |
|
| 301 | + public static function select($name, $values = null, $selected = null, array $attributes = array()){ |
|
| 302 | + if(! is_array($values)){ |
|
| 303 | + $values = array('' => $values); |
|
| 304 | + } |
|
| 305 | + $str = null; |
|
| 306 | + $str .= '<select name = "'.$name.'"'; |
|
| 307 | + $str .= attributes_to_string($attributes); |
|
| 308 | + $str .= '>'; |
|
| 309 | + foreach($values as $key => $val){ |
|
| 310 | + $select = ''; |
|
| 311 | + if($key == $selected){ |
|
| 312 | + $select = 'selected'; |
|
| 313 | + } |
|
| 314 | + $str .= '<option value = "'.$key.'" '.$select.'>'.$val.'</option>'; |
|
| 315 | + } |
|
| 316 | + $str .= '</select>'; |
|
| 317 | + return $str; |
|
| 318 | + } |
|
| 319 | 319 | |
| 320 | - } |
|
| 320 | + } |
|
@@ -1,312 +1,312 @@ |
||
| 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 FileCache implements CacheInterface{ |
|
| 27 | + class FileCache implements CacheInterface{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Whether to enable compression of the cache data file. |
|
| 31 | - * @var boolean |
|
| 32 | - */ |
|
| 33 | - private $compressCacheData = true; |
|
| 29 | + /** |
|
| 30 | + * Whether to enable compression of the cache data file. |
|
| 31 | + * @var boolean |
|
| 32 | + */ |
|
| 33 | + private $compressCacheData = true; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * The logger instance |
|
| 37 | - * @var Log |
|
| 38 | - */ |
|
| 39 | - private $logger; |
|
| 35 | + /** |
|
| 36 | + * The logger instance |
|
| 37 | + * @var Log |
|
| 38 | + */ |
|
| 39 | + private $logger; |
|
| 40 | 40 | |
| 41 | 41 | |
| 42 | - public function __construct(Log $logger = null){ |
|
| 43 | - if(! $this->isSupported()){ |
|
| 44 | - show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
|
| 45 | - } |
|
| 46 | - /** |
|
| 47 | - * instance of the Log class |
|
| 48 | - */ |
|
| 49 | - if(is_object($logger)){ |
|
| 50 | - $this->logger = $logger; |
|
| 51 | - } |
|
| 52 | - else{ |
|
| 53 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 54 | - $this->logger->setLogger('Library::FileCache'); |
|
| 55 | - } |
|
| 42 | + public function __construct(Log $logger = null){ |
|
| 43 | + if(! $this->isSupported()){ |
|
| 44 | + show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
|
| 45 | + } |
|
| 46 | + /** |
|
| 47 | + * instance of the Log class |
|
| 48 | + */ |
|
| 49 | + if(is_object($logger)){ |
|
| 50 | + $this->logger = $logger; |
|
| 51 | + } |
|
| 52 | + else{ |
|
| 53 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 54 | + $this->logger->setLogger('Library::FileCache'); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - //if Zlib extension is not loaded set compressCacheData to false |
|
| 58 | - if(! extension_loaded('zlib')){ |
|
| 59 | - $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
| 60 | - $this->compressCacheData = false; |
|
| 61 | - } |
|
| 62 | - } |
|
| 57 | + //if Zlib extension is not loaded set compressCacheData to false |
|
| 58 | + if(! extension_loaded('zlib')){ |
|
| 59 | + $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
| 60 | + $this->compressCacheData = false; |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * This is used to get the cache data using the key |
|
| 66 | - * @param string $key the key to identify the cache data |
|
| 67 | - * @return mixed the cache data if exists else return false |
|
| 68 | - */ |
|
| 69 | - public function get($key){ |
|
| 70 | - $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 71 | - $filePath = $this->getFilePath($key); |
|
| 72 | - if(! file_exists($filePath)){ |
|
| 73 | - $this->logger->info('No cache file found for the key ['. $key .'], return false'); |
|
| 74 | - return false; |
|
| 75 | - } |
|
| 76 | - $this->logger->info('The cache file [' .$filePath. '] for the key ['. $key .'] exists, check if the cache data is valid'); |
|
| 77 | - $handle = fopen($filePath,'r'); |
|
| 78 | - if(! is_resource($handle)){ |
|
| 79 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 80 | - return false; |
|
| 81 | - } |
|
| 82 | - // Getting a shared lock |
|
| 83 | - flock($handle, LOCK_SH); |
|
| 84 | - $data = file_get_contents($filePath); |
|
| 85 | - fclose($handle); |
|
| 86 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 87 | - if (! $data) { |
|
| 88 | - $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 89 | - // If unserializing somehow didn't work out, we'll delete the file |
|
| 90 | - unlink($filePath); |
|
| 91 | - return false; |
|
| 92 | - } |
|
| 93 | - if (time() > $data['expire']) { |
|
| 94 | - $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 95 | - // Unlinking when the file was expired |
|
| 96 | - unlink($filePath); |
|
| 97 | - return false; |
|
| 98 | - } |
|
| 99 | - else{ |
|
| 100 | - $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', $data['expire']) . ']'); |
|
| 101 | - return $data['data']; |
|
| 102 | - } |
|
| 103 | - } |
|
| 64 | + /** |
|
| 65 | + * This is used to get the cache data using the key |
|
| 66 | + * @param string $key the key to identify the cache data |
|
| 67 | + * @return mixed the cache data if exists else return false |
|
| 68 | + */ |
|
| 69 | + public function get($key){ |
|
| 70 | + $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 71 | + $filePath = $this->getFilePath($key); |
|
| 72 | + if(! file_exists($filePath)){ |
|
| 73 | + $this->logger->info('No cache file found for the key ['. $key .'], return false'); |
|
| 74 | + return false; |
|
| 75 | + } |
|
| 76 | + $this->logger->info('The cache file [' .$filePath. '] for the key ['. $key .'] exists, check if the cache data is valid'); |
|
| 77 | + $handle = fopen($filePath,'r'); |
|
| 78 | + if(! is_resource($handle)){ |
|
| 79 | + $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 80 | + return false; |
|
| 81 | + } |
|
| 82 | + // Getting a shared lock |
|
| 83 | + flock($handle, LOCK_SH); |
|
| 84 | + $data = file_get_contents($filePath); |
|
| 85 | + fclose($handle); |
|
| 86 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 87 | + if (! $data) { |
|
| 88 | + $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 89 | + // If unserializing somehow didn't work out, we'll delete the file |
|
| 90 | + unlink($filePath); |
|
| 91 | + return false; |
|
| 92 | + } |
|
| 93 | + if (time() > $data['expire']) { |
|
| 94 | + $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 95 | + // Unlinking when the file was expired |
|
| 96 | + unlink($filePath); |
|
| 97 | + return false; |
|
| 98 | + } |
|
| 99 | + else{ |
|
| 100 | + $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', $data['expire']) . ']'); |
|
| 101 | + return $data['data']; |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * Save data to the cache |
|
| 108 | - * @param string $key the key to identify this cache data |
|
| 109 | - * @param mixed $data the cache data |
|
| 110 | - * @param integer $ttl the cache life time |
|
| 111 | - * @return boolean true if success otherwise will return false |
|
| 112 | - */ |
|
| 113 | - public function set($key, $data, $ttl = 0){ |
|
| 114 | - $expire = time() + $ttl; |
|
| 115 | - $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 116 | - $filePath = $this->getFilePath($key); |
|
| 117 | - $handle = fopen($filePath,'w'); |
|
| 118 | - if(! is_resource($handle)){ |
|
| 119 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 120 | - return false; |
|
| 121 | - } |
|
| 122 | - flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
|
| 123 | - //Serializing along with the TTL |
|
| 124 | - $cacheData = serialize(array( |
|
| 125 | - 'mtime' => time(), |
|
| 126 | - 'expire' => $expire, |
|
| 127 | - 'data' => $data, |
|
| 128 | - 'ttl' => $ttl |
|
| 129 | - ) |
|
| 130 | - ); |
|
| 131 | - $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
|
| 132 | - if(! $result){ |
|
| 133 | - $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 134 | - fclose($handle); |
|
| 135 | - return false; |
|
| 136 | - } |
|
| 137 | - else{ |
|
| 138 | - $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 139 | - fclose($handle); |
|
| 140 | - chmod($filePath, 0640); |
|
| 141 | - return true; |
|
| 142 | - } |
|
| 143 | - } |
|
| 106 | + /** |
|
| 107 | + * Save data to the cache |
|
| 108 | + * @param string $key the key to identify this cache data |
|
| 109 | + * @param mixed $data the cache data |
|
| 110 | + * @param integer $ttl the cache life time |
|
| 111 | + * @return boolean true if success otherwise will return false |
|
| 112 | + */ |
|
| 113 | + public function set($key, $data, $ttl = 0){ |
|
| 114 | + $expire = time() + $ttl; |
|
| 115 | + $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 116 | + $filePath = $this->getFilePath($key); |
|
| 117 | + $handle = fopen($filePath,'w'); |
|
| 118 | + if(! is_resource($handle)){ |
|
| 119 | + $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 120 | + return false; |
|
| 121 | + } |
|
| 122 | + flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
|
| 123 | + //Serializing along with the TTL |
|
| 124 | + $cacheData = serialize(array( |
|
| 125 | + 'mtime' => time(), |
|
| 126 | + 'expire' => $expire, |
|
| 127 | + 'data' => $data, |
|
| 128 | + 'ttl' => $ttl |
|
| 129 | + ) |
|
| 130 | + ); |
|
| 131 | + $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
|
| 132 | + if(! $result){ |
|
| 133 | + $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 134 | + fclose($handle); |
|
| 135 | + return false; |
|
| 136 | + } |
|
| 137 | + else{ |
|
| 138 | + $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 139 | + fclose($handle); |
|
| 140 | + chmod($filePath, 0640); |
|
| 141 | + return true; |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | 145 | |
| 146 | - /** |
|
| 147 | - * Delete the cache data for given key |
|
| 148 | - * @param string $key the key for cache to be deleted |
|
| 149 | - * @return boolean true if the cache is delete, false if can't delete |
|
| 150 | - * the cache or the cache with the given key not exist |
|
| 151 | - */ |
|
| 152 | - public function delete($key){ |
|
| 153 | - $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 154 | - $filePath = $this->getFilePath($key); |
|
| 155 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 156 | - if(! file_exists($filePath)){ |
|
| 157 | - $this->logger->info('This cache file does not exists skipping'); |
|
| 158 | - return false; |
|
| 159 | - } |
|
| 160 | - else{ |
|
| 161 | - $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
|
| 162 | - unlink($filePath); |
|
| 163 | - return true; |
|
| 164 | - } |
|
| 165 | - } |
|
| 146 | + /** |
|
| 147 | + * Delete the cache data for given key |
|
| 148 | + * @param string $key the key for cache to be deleted |
|
| 149 | + * @return boolean true if the cache is delete, false if can't delete |
|
| 150 | + * the cache or the cache with the given key not exist |
|
| 151 | + */ |
|
| 152 | + public function delete($key){ |
|
| 153 | + $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 154 | + $filePath = $this->getFilePath($key); |
|
| 155 | + $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 156 | + if(! file_exists($filePath)){ |
|
| 157 | + $this->logger->info('This cache file does not exists skipping'); |
|
| 158 | + return false; |
|
| 159 | + } |
|
| 160 | + else{ |
|
| 161 | + $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
|
| 162 | + unlink($filePath); |
|
| 163 | + return true; |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * Get the cache information for given key |
|
| 169 | - * @param string $key the key for cache to get the information for |
|
| 170 | - * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 171 | - * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 172 | - * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 173 | - * 'ttl' => the time to live of the cache in second |
|
| 174 | - */ |
|
| 175 | - public function getInfo($key){ |
|
| 176 | - $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 177 | - $filePath = $this->getFilePath($key); |
|
| 178 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 179 | - if(! file_exists($filePath)){ |
|
| 180 | - $this->logger->info('This cache file does not exists skipping'); |
|
| 181 | - return false; |
|
| 182 | - } |
|
| 183 | - $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
|
| 184 | - $data = file_get_contents($filePath); |
|
| 185 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 186 | - if(! $data){ |
|
| 187 | - $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
|
| 188 | - return false; |
|
| 189 | - } |
|
| 190 | - $this->logger->info('This cache data is OK check for expire'); |
|
| 191 | - if(isset($data['expire']) && $data['expire'] > time()){ |
|
| 192 | - $this->logger->info('This cache not yet expired return cache informations'); |
|
| 193 | - $info = array( |
|
| 194 | - 'mtime' => $data['mtime'], |
|
| 195 | - 'expire' => $data['expire'], |
|
| 196 | - 'ttl' => $data['ttl'] |
|
| 197 | - ); |
|
| 198 | - return $info; |
|
| 199 | - } |
|
| 200 | - $this->logger->info('This cache already expired return false'); |
|
| 201 | - return false; |
|
| 202 | - } |
|
| 167 | + /** |
|
| 168 | + * Get the cache information for given key |
|
| 169 | + * @param string $key the key for cache to get the information for |
|
| 170 | + * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 171 | + * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 172 | + * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 173 | + * 'ttl' => the time to live of the cache in second |
|
| 174 | + */ |
|
| 175 | + public function getInfo($key){ |
|
| 176 | + $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 177 | + $filePath = $this->getFilePath($key); |
|
| 178 | + $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 179 | + if(! file_exists($filePath)){ |
|
| 180 | + $this->logger->info('This cache file does not exists skipping'); |
|
| 181 | + return false; |
|
| 182 | + } |
|
| 183 | + $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
|
| 184 | + $data = file_get_contents($filePath); |
|
| 185 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 186 | + if(! $data){ |
|
| 187 | + $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
|
| 188 | + return false; |
|
| 189 | + } |
|
| 190 | + $this->logger->info('This cache data is OK check for expire'); |
|
| 191 | + if(isset($data['expire']) && $data['expire'] > time()){ |
|
| 192 | + $this->logger->info('This cache not yet expired return cache informations'); |
|
| 193 | + $info = array( |
|
| 194 | + 'mtime' => $data['mtime'], |
|
| 195 | + 'expire' => $data['expire'], |
|
| 196 | + 'ttl' => $data['ttl'] |
|
| 197 | + ); |
|
| 198 | + return $info; |
|
| 199 | + } |
|
| 200 | + $this->logger->info('This cache already expired return false'); |
|
| 201 | + return false; |
|
| 202 | + } |
|
| 203 | 203 | |
| 204 | 204 | |
| 205 | - /** |
|
| 206 | - * Used to delete expired cache data |
|
| 207 | - */ |
|
| 208 | - public function deleteExpiredCache(){ |
|
| 209 | - $this->logger->debug('Deleting of expired cache files'); |
|
| 210 | - $list = glob(CACHE_PATH . '*.cache'); |
|
| 211 | - if(! $list){ |
|
| 212 | - $this->logger->info('No cache files were found skipping'); |
|
| 213 | - } |
|
| 214 | - else{ |
|
| 215 | - $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
|
| 216 | - foreach ($list as $file) { |
|
| 217 | - $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
| 218 | - $data = file_get_contents($file); |
|
| 219 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 220 | - if(! $data){ |
|
| 221 | - $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
|
| 222 | - } |
|
| 223 | - else if(time() > $data['expire']){ |
|
| 224 | - $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
|
| 225 | - unlink($file); |
|
| 226 | - } |
|
| 227 | - else{ |
|
| 228 | - $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
|
| 229 | - } |
|
| 230 | - } |
|
| 231 | - } |
|
| 232 | - } |
|
| 205 | + /** |
|
| 206 | + * Used to delete expired cache data |
|
| 207 | + */ |
|
| 208 | + public function deleteExpiredCache(){ |
|
| 209 | + $this->logger->debug('Deleting of expired cache files'); |
|
| 210 | + $list = glob(CACHE_PATH . '*.cache'); |
|
| 211 | + if(! $list){ |
|
| 212 | + $this->logger->info('No cache files were found skipping'); |
|
| 213 | + } |
|
| 214 | + else{ |
|
| 215 | + $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
|
| 216 | + foreach ($list as $file) { |
|
| 217 | + $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
| 218 | + $data = file_get_contents($file); |
|
| 219 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 220 | + if(! $data){ |
|
| 221 | + $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
|
| 222 | + } |
|
| 223 | + else if(time() > $data['expire']){ |
|
| 224 | + $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
|
| 225 | + unlink($file); |
|
| 226 | + } |
|
| 227 | + else{ |
|
| 228 | + $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
|
| 229 | + } |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | - /** |
|
| 235 | - * Remove all file from cache folder |
|
| 236 | - */ |
|
| 237 | - public function clean(){ |
|
| 238 | - $this->logger->debug('Deleting of all cache files'); |
|
| 239 | - $list = glob(CACHE_PATH . '*.cache'); |
|
| 240 | - if(! $list){ |
|
| 241 | - $this->logger->info('No cache files were found skipping'); |
|
| 242 | - } |
|
| 243 | - else{ |
|
| 244 | - $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
|
| 245 | - foreach ($list as $file) { |
|
| 246 | - $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
| 247 | - unlink($file); |
|
| 248 | - } |
|
| 249 | - } |
|
| 250 | - } |
|
| 234 | + /** |
|
| 235 | + * Remove all file from cache folder |
|
| 236 | + */ |
|
| 237 | + public function clean(){ |
|
| 238 | + $this->logger->debug('Deleting of all cache files'); |
|
| 239 | + $list = glob(CACHE_PATH . '*.cache'); |
|
| 240 | + if(! $list){ |
|
| 241 | + $this->logger->info('No cache files were found skipping'); |
|
| 242 | + } |
|
| 243 | + else{ |
|
| 244 | + $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
|
| 245 | + foreach ($list as $file) { |
|
| 246 | + $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
| 247 | + unlink($file); |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | + } |
|
| 251 | 251 | |
| 252 | - /** |
|
| 253 | - * @return boolean |
|
| 254 | - */ |
|
| 255 | - public function isCompressCacheData(){ |
|
| 256 | - return $this->compressCacheData; |
|
| 257 | - } |
|
| 252 | + /** |
|
| 253 | + * @return boolean |
|
| 254 | + */ |
|
| 255 | + public function isCompressCacheData(){ |
|
| 256 | + return $this->compressCacheData; |
|
| 257 | + } |
|
| 258 | 258 | |
| 259 | - /** |
|
| 260 | - * @param boolean $compressCacheData |
|
| 261 | - * |
|
| 262 | - * @return object |
|
| 263 | - */ |
|
| 264 | - public function setCompressCacheData($status = true){ |
|
| 265 | - //if Zlib extension is not loaded set compressCacheData to false |
|
| 266 | - if($status === true && ! extension_loaded('zlib')){ |
|
| 259 | + /** |
|
| 260 | + * @param boolean $compressCacheData |
|
| 261 | + * |
|
| 262 | + * @return object |
|
| 263 | + */ |
|
| 264 | + public function setCompressCacheData($status = true){ |
|
| 265 | + //if Zlib extension is not loaded set compressCacheData to false |
|
| 266 | + if($status === true && ! extension_loaded('zlib')){ |
|
| 267 | 267 | |
| 268 | - $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
| 269 | - $this->compressCacheData = false; |
|
| 270 | - } |
|
| 271 | - else{ |
|
| 272 | - $this->compressCacheData = $status; |
|
| 273 | - } |
|
| 274 | - return $this; |
|
| 275 | - } |
|
| 268 | + $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
| 269 | + $this->compressCacheData = false; |
|
| 270 | + } |
|
| 271 | + else{ |
|
| 272 | + $this->compressCacheData = $status; |
|
| 273 | + } |
|
| 274 | + return $this; |
|
| 275 | + } |
|
| 276 | 276 | |
| 277 | - /** |
|
| 278 | - * Check whether the cache feature for the handle is supported |
|
| 279 | - * |
|
| 280 | - * @return bool |
|
| 281 | - */ |
|
| 282 | - public function isSupported(){ |
|
| 283 | - return CACHE_PATH && is_dir(CACHE_PATH) && is_writable(CACHE_PATH); |
|
| 284 | - } |
|
| 277 | + /** |
|
| 278 | + * Check whether the cache feature for the handle is supported |
|
| 279 | + * |
|
| 280 | + * @return bool |
|
| 281 | + */ |
|
| 282 | + public function isSupported(){ |
|
| 283 | + return CACHE_PATH && is_dir(CACHE_PATH) && is_writable(CACHE_PATH); |
|
| 284 | + } |
|
| 285 | 285 | |
| 286 | - /** |
|
| 287 | - * Return the Log instance |
|
| 288 | - * @return object |
|
| 289 | - */ |
|
| 290 | - public function getLogger(){ |
|
| 291 | - return $this->logger; |
|
| 292 | - } |
|
| 286 | + /** |
|
| 287 | + * Return the Log instance |
|
| 288 | + * @return object |
|
| 289 | + */ |
|
| 290 | + public function getLogger(){ |
|
| 291 | + return $this->logger; |
|
| 292 | + } |
|
| 293 | 293 | |
| 294 | - /** |
|
| 295 | - * Set the log instance |
|
| 296 | - * @param Log $logger the log object |
|
| 297 | - */ |
|
| 298 | - public function setLogger(Log $logger){ |
|
| 299 | - $this->logger = $logger; |
|
| 300 | - return $this; |
|
| 301 | - } |
|
| 294 | + /** |
|
| 295 | + * Set the log instance |
|
| 296 | + * @param Log $logger the log object |
|
| 297 | + */ |
|
| 298 | + public function setLogger(Log $logger){ |
|
| 299 | + $this->logger = $logger; |
|
| 300 | + return $this; |
|
| 301 | + } |
|
| 302 | 302 | |
| 303 | - /** |
|
| 304 | - * Get the cache file full path for the given key |
|
| 305 | - * |
|
| 306 | - * @param string $key the cache item key |
|
| 307 | - * @return string the full cache file path for this key |
|
| 308 | - */ |
|
| 309 | - private function getFilePath($key){ |
|
| 310 | - return CACHE_PATH . md5($key) . '.cache'; |
|
| 311 | - } |
|
| 312 | - } |
|
| 303 | + /** |
|
| 304 | + * Get the cache file full path for the given key |
|
| 305 | + * |
|
| 306 | + * @param string $key the cache item key |
|
| 307 | + * @return string the full cache file path for this key |
|
| 308 | + */ |
|
| 309 | + private function getFilePath($key){ |
|
| 310 | + return CACHE_PATH . md5($key) . '.cache'; |
|
| 311 | + } |
|
| 312 | + } |
|
@@ -1,624 +1,624 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 26 | - |
|
| 27 | - class Router { |
|
| 28 | - /** |
|
| 29 | - * @var array $pattern: The list of URIs to validate against |
|
| 30 | - */ |
|
| 31 | - private $pattern = array(); |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var array $callback: The list of callback to call |
|
| 35 | - */ |
|
| 36 | - private $callback = array(); |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @var string $uriTrim: The char to remove from the URIs |
|
| 40 | - */ |
|
| 41 | - protected $uriTrim = '/\^$'; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @var string $uri: The route URI to use |
|
| 45 | - */ |
|
| 46 | - protected $uri = ''; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * The module name of the current request |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 52 | - protected $module = null; |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | + |
|
| 27 | + class Router { |
|
| 28 | + /** |
|
| 29 | + * @var array $pattern: The list of URIs to validate against |
|
| 30 | + */ |
|
| 31 | + private $pattern = array(); |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var array $callback: The list of callback to call |
|
| 35 | + */ |
|
| 36 | + private $callback = array(); |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @var string $uriTrim: The char to remove from the URIs |
|
| 40 | + */ |
|
| 41 | + protected $uriTrim = '/\^$'; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @var string $uri: The route URI to use |
|
| 45 | + */ |
|
| 46 | + protected $uri = ''; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * The module name of the current request |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | + protected $module = null; |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * The controller name of the current request |
|
| 56 | - * @var string |
|
| 57 | - */ |
|
| 58 | - protected $controller = null; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * The controller path |
|
| 62 | - * @var string |
|
| 63 | - */ |
|
| 64 | - protected $controllerPath = null; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * The method name. The default value is "index" |
|
| 68 | - * @var string |
|
| 69 | - */ |
|
| 70 | - protected $method = 'index'; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * List of argument to pass to the method |
|
| 74 | - * @var array |
|
| 75 | - */ |
|
| 76 | - protected $args = array(); |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * List of routes configurations |
|
| 80 | - * @var array |
|
| 81 | - */ |
|
| 82 | - protected $routes = array(); |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * The segments array for the current request |
|
| 86 | - * @var array |
|
| 87 | - */ |
|
| 88 | - protected $segments = array(); |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * The logger instance |
|
| 92 | - * @var Log |
|
| 93 | - */ |
|
| 94 | - private $logger; |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Construct the new Router instance |
|
| 98 | - */ |
|
| 99 | - public function __construct(){ |
|
| 100 | - $this->setLoggerFromParamOrCreateNewInstance(null); |
|
| 54 | + /** |
|
| 55 | + * The controller name of the current request |
|
| 56 | + * @var string |
|
| 57 | + */ |
|
| 58 | + protected $controller = null; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * The controller path |
|
| 62 | + * @var string |
|
| 63 | + */ |
|
| 64 | + protected $controllerPath = null; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * The method name. The default value is "index" |
|
| 68 | + * @var string |
|
| 69 | + */ |
|
| 70 | + protected $method = 'index'; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * List of argument to pass to the method |
|
| 74 | + * @var array |
|
| 75 | + */ |
|
| 76 | + protected $args = array(); |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * List of routes configurations |
|
| 80 | + * @var array |
|
| 81 | + */ |
|
| 82 | + protected $routes = array(); |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * The segments array for the current request |
|
| 86 | + * @var array |
|
| 87 | + */ |
|
| 88 | + protected $segments = array(); |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * The logger instance |
|
| 92 | + * @var Log |
|
| 93 | + */ |
|
| 94 | + private $logger; |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Construct the new Router instance |
|
| 98 | + */ |
|
| 99 | + public function __construct(){ |
|
| 100 | + $this->setLoggerFromParamOrCreateNewInstance(null); |
|
| 101 | 101 | |
| 102 | - //loading routes for module |
|
| 103 | - $moduleRouteList = array(); |
|
| 104 | - $modulesRoutes = Module::getModulesRoutes(); |
|
| 105 | - if($modulesRoutes && is_array($modulesRoutes)){ |
|
| 106 | - $moduleRouteList = $modulesRoutes; |
|
| 107 | - unset($modulesRoutes); |
|
| 108 | - } |
|
| 109 | - $this->setRouteConfiguration($moduleRouteList); |
|
| 110 | - $this->logger->info('The routes configuration are listed below: ' . stringfy_vars($this->routes)); |
|
| 111 | - |
|
| 112 | - //Set route informations |
|
| 113 | - $this->setRouteConfigurationInfos(); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Get the route patterns |
|
| 118 | - * @return array |
|
| 119 | - */ |
|
| 120 | - public function getPattern(){ |
|
| 121 | - return $this->pattern; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Get the route callbacks |
|
| 126 | - * @return array |
|
| 127 | - */ |
|
| 128 | - public function getCallback(){ |
|
| 129 | - return $this->callback; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Get the module name |
|
| 134 | - * @return string |
|
| 135 | - */ |
|
| 136 | - public function getModule(){ |
|
| 137 | - return $this->module; |
|
| 138 | - } |
|
| 102 | + //loading routes for module |
|
| 103 | + $moduleRouteList = array(); |
|
| 104 | + $modulesRoutes = Module::getModulesRoutes(); |
|
| 105 | + if($modulesRoutes && is_array($modulesRoutes)){ |
|
| 106 | + $moduleRouteList = $modulesRoutes; |
|
| 107 | + unset($modulesRoutes); |
|
| 108 | + } |
|
| 109 | + $this->setRouteConfiguration($moduleRouteList); |
|
| 110 | + $this->logger->info('The routes configuration are listed below: ' . stringfy_vars($this->routes)); |
|
| 111 | + |
|
| 112 | + //Set route informations |
|
| 113 | + $this->setRouteConfigurationInfos(); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Get the route patterns |
|
| 118 | + * @return array |
|
| 119 | + */ |
|
| 120 | + public function getPattern(){ |
|
| 121 | + return $this->pattern; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Get the route callbacks |
|
| 126 | + * @return array |
|
| 127 | + */ |
|
| 128 | + public function getCallback(){ |
|
| 129 | + return $this->callback; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Get the module name |
|
| 134 | + * @return string |
|
| 135 | + */ |
|
| 136 | + public function getModule(){ |
|
| 137 | + return $this->module; |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - /** |
|
| 141 | - * Get the controller name |
|
| 142 | - * @return string |
|
| 143 | - */ |
|
| 144 | - public function getController(){ |
|
| 145 | - return $this->controller; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Get the controller file path |
|
| 150 | - * @return string |
|
| 151 | - */ |
|
| 152 | - public function getControllerPath(){ |
|
| 153 | - return $this->controllerPath; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Get the controller method |
|
| 158 | - * @return string |
|
| 159 | - */ |
|
| 160 | - public function getMethod(){ |
|
| 161 | - return $this->method; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Get the request arguments |
|
| 166 | - * @return array |
|
| 167 | - */ |
|
| 168 | - public function getArgs(){ |
|
| 169 | - return $this->args; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Get the URL segments array |
|
| 174 | - * @return array |
|
| 175 | - */ |
|
| 176 | - public function getSegments(){ |
|
| 177 | - return $this->segments; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Return the Log instance |
|
| 182 | - * @return Log |
|
| 183 | - */ |
|
| 184 | - public function getLogger(){ |
|
| 185 | - return $this->logger; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Set the log instance |
|
| 190 | - * @param Log $logger the log object |
|
| 191 | - * @return object |
|
| 192 | - */ |
|
| 193 | - public function setLogger($logger){ |
|
| 194 | - $this->logger = $logger; |
|
| 195 | - return $this; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Get the route URI |
|
| 200 | - * @return string |
|
| 201 | - */ |
|
| 202 | - public function getRouteUri(){ |
|
| 203 | - return $this->uri; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Add the URI and callback to the list of URIs to validate |
|
| 208 | - * |
|
| 209 | - * @param string $uri the request URI |
|
| 210 | - * @param string $callback the callback function |
|
| 211 | - * |
|
| 212 | - * @return object the current instance |
|
| 213 | - */ |
|
| 214 | - public function add($uri, $callback) { |
|
| 215 | - $uri = trim($uri, $this->uriTrim); |
|
| 216 | - if(in_array($uri, $this->pattern)){ |
|
| 217 | - $this->logger->warning('The route [' . $uri . '] already added, may be adding again can have route conflict'); |
|
| 218 | - } |
|
| 219 | - $this->pattern[] = $uri; |
|
| 220 | - $this->callback[] = $callback; |
|
| 221 | - return $this; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Remove the route configuration |
|
| 226 | - * |
|
| 227 | - * @param string $uri the URI |
|
| 228 | - * |
|
| 229 | - * @return object the current instance |
|
| 230 | - */ |
|
| 231 | - public function removeRoute($uri) { |
|
| 232 | - $index = array_search($uri, $this->pattern, true); |
|
| 233 | - if($index !== false){ |
|
| 234 | - $this->logger->info('Remove route for uri [' . $uri . '] from the configuration'); |
|
| 235 | - unset($this->pattern[$index]); |
|
| 236 | - unset($this->callback[$index]); |
|
| 237 | - } |
|
| 238 | - return $this; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * Remove all the routes from the configuration |
|
| 244 | - * |
|
| 245 | - * @return object the current instance |
|
| 246 | - */ |
|
| 247 | - public function removeAllRoute() { |
|
| 248 | - $this->logger->info('Remove all routes from the configuration'); |
|
| 249 | - $this->pattern = array(); |
|
| 250 | - $this->callback = array(); |
|
| 251 | - $this->routes = array(); |
|
| 252 | - return $this; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * Set the route URI to use later |
|
| 258 | - * @param string $uri the route URI, if is empty will determine automatically |
|
| 259 | - * @return object |
|
| 260 | - */ |
|
| 261 | - public function setRouteUri($uri = ''){ |
|
| 262 | - $routeUri = ''; |
|
| 263 | - if(! empty($uri)){ |
|
| 264 | - $routeUri = $uri; |
|
| 265 | - } |
|
| 266 | - //if the application is running in CLI mode use the first argument |
|
| 267 | - else if(IS_CLI && isset($_SERVER['argv'][1])){ |
|
| 268 | - $routeUri = $_SERVER['argv'][1]; |
|
| 269 | - } |
|
| 270 | - else if(isset($_SERVER['REQUEST_URI'])){ |
|
| 271 | - $routeUri = $_SERVER['REQUEST_URI']; |
|
| 272 | - } |
|
| 273 | - $this->logger->debug('Check if URL suffix is enabled in the configuration'); |
|
| 274 | - //remove url suffix from the request URI |
|
| 275 | - $suffix = get_config('url_suffix'); |
|
| 276 | - if ($suffix) { |
|
| 277 | - $this->logger->info('URL suffix is enabled in the configuration, the value is [' . $suffix . ']' ); |
|
| 278 | - $routeUri = str_ireplace($suffix, '', $routeUri); |
|
| 279 | - } |
|
| 280 | - if (strpos($routeUri, '?') !== false){ |
|
| 281 | - $routeUri = substr($routeUri, 0, strpos($routeUri, '?')); |
|
| 282 | - } |
|
| 283 | - $this->uri = trim($routeUri, $this->uriTrim); |
|
| 284 | - return $this; |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * Set the route segments informations |
|
| 289 | - * @param array $segements the route segments information |
|
| 290 | - * |
|
| 291 | - * @return object |
|
| 292 | - */ |
|
| 293 | - public function setRouteSegments(array $segments = array()){ |
|
| 294 | - if(! empty($segments)){ |
|
| 295 | - $this->segments = $segments; |
|
| 296 | - } else if (!empty($this->uri)) { |
|
| 297 | - $this->segments = explode('/', $this->uri); |
|
| 298 | - } |
|
| 299 | - $segment = $this->segments; |
|
| 300 | - $baseUrl = get_config('base_url'); |
|
| 301 | - //check if the app is not in DOCUMENT_ROOT |
|
| 302 | - if(isset($segment[0]) && stripos($baseUrl, $segment[0]) !== false){ |
|
| 303 | - array_shift($segment); |
|
| 304 | - $this->segments = $segment; |
|
| 305 | - } |
|
| 306 | - $this->logger->debug('Check if the request URI contains the front controller'); |
|
| 307 | - if(isset($segment[0]) && $segment[0] == SELF){ |
|
| 308 | - $this->logger->info('The request URI contains the front controller'); |
|
| 309 | - array_shift($segment); |
|
| 310 | - $this->segments = $segment; |
|
| 311 | - } |
|
| 312 | - return $this; |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * Setting the route parameters like module, controller, method, argument |
|
| 317 | - * @return object the current instance |
|
| 318 | - */ |
|
| 319 | - public function determineRouteParamsInformation() { |
|
| 320 | - $this->logger->debug('Routing process start ...'); |
|
| 140 | + /** |
|
| 141 | + * Get the controller name |
|
| 142 | + * @return string |
|
| 143 | + */ |
|
| 144 | + public function getController(){ |
|
| 145 | + return $this->controller; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Get the controller file path |
|
| 150 | + * @return string |
|
| 151 | + */ |
|
| 152 | + public function getControllerPath(){ |
|
| 153 | + return $this->controllerPath; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Get the controller method |
|
| 158 | + * @return string |
|
| 159 | + */ |
|
| 160 | + public function getMethod(){ |
|
| 161 | + return $this->method; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Get the request arguments |
|
| 166 | + * @return array |
|
| 167 | + */ |
|
| 168 | + public function getArgs(){ |
|
| 169 | + return $this->args; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Get the URL segments array |
|
| 174 | + * @return array |
|
| 175 | + */ |
|
| 176 | + public function getSegments(){ |
|
| 177 | + return $this->segments; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Return the Log instance |
|
| 182 | + * @return Log |
|
| 183 | + */ |
|
| 184 | + public function getLogger(){ |
|
| 185 | + return $this->logger; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Set the log instance |
|
| 190 | + * @param Log $logger the log object |
|
| 191 | + * @return object |
|
| 192 | + */ |
|
| 193 | + public function setLogger($logger){ |
|
| 194 | + $this->logger = $logger; |
|
| 195 | + return $this; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Get the route URI |
|
| 200 | + * @return string |
|
| 201 | + */ |
|
| 202 | + public function getRouteUri(){ |
|
| 203 | + return $this->uri; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Add the URI and callback to the list of URIs to validate |
|
| 208 | + * |
|
| 209 | + * @param string $uri the request URI |
|
| 210 | + * @param string $callback the callback function |
|
| 211 | + * |
|
| 212 | + * @return object the current instance |
|
| 213 | + */ |
|
| 214 | + public function add($uri, $callback) { |
|
| 215 | + $uri = trim($uri, $this->uriTrim); |
|
| 216 | + if(in_array($uri, $this->pattern)){ |
|
| 217 | + $this->logger->warning('The route [' . $uri . '] already added, may be adding again can have route conflict'); |
|
| 218 | + } |
|
| 219 | + $this->pattern[] = $uri; |
|
| 220 | + $this->callback[] = $callback; |
|
| 221 | + return $this; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Remove the route configuration |
|
| 226 | + * |
|
| 227 | + * @param string $uri the URI |
|
| 228 | + * |
|
| 229 | + * @return object the current instance |
|
| 230 | + */ |
|
| 231 | + public function removeRoute($uri) { |
|
| 232 | + $index = array_search($uri, $this->pattern, true); |
|
| 233 | + if($index !== false){ |
|
| 234 | + $this->logger->info('Remove route for uri [' . $uri . '] from the configuration'); |
|
| 235 | + unset($this->pattern[$index]); |
|
| 236 | + unset($this->callback[$index]); |
|
| 237 | + } |
|
| 238 | + return $this; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * Remove all the routes from the configuration |
|
| 244 | + * |
|
| 245 | + * @return object the current instance |
|
| 246 | + */ |
|
| 247 | + public function removeAllRoute() { |
|
| 248 | + $this->logger->info('Remove all routes from the configuration'); |
|
| 249 | + $this->pattern = array(); |
|
| 250 | + $this->callback = array(); |
|
| 251 | + $this->routes = array(); |
|
| 252 | + return $this; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * Set the route URI to use later |
|
| 258 | + * @param string $uri the route URI, if is empty will determine automatically |
|
| 259 | + * @return object |
|
| 260 | + */ |
|
| 261 | + public function setRouteUri($uri = ''){ |
|
| 262 | + $routeUri = ''; |
|
| 263 | + if(! empty($uri)){ |
|
| 264 | + $routeUri = $uri; |
|
| 265 | + } |
|
| 266 | + //if the application is running in CLI mode use the first argument |
|
| 267 | + else if(IS_CLI && isset($_SERVER['argv'][1])){ |
|
| 268 | + $routeUri = $_SERVER['argv'][1]; |
|
| 269 | + } |
|
| 270 | + else if(isset($_SERVER['REQUEST_URI'])){ |
|
| 271 | + $routeUri = $_SERVER['REQUEST_URI']; |
|
| 272 | + } |
|
| 273 | + $this->logger->debug('Check if URL suffix is enabled in the configuration'); |
|
| 274 | + //remove url suffix from the request URI |
|
| 275 | + $suffix = get_config('url_suffix'); |
|
| 276 | + if ($suffix) { |
|
| 277 | + $this->logger->info('URL suffix is enabled in the configuration, the value is [' . $suffix . ']' ); |
|
| 278 | + $routeUri = str_ireplace($suffix, '', $routeUri); |
|
| 279 | + } |
|
| 280 | + if (strpos($routeUri, '?') !== false){ |
|
| 281 | + $routeUri = substr($routeUri, 0, strpos($routeUri, '?')); |
|
| 282 | + } |
|
| 283 | + $this->uri = trim($routeUri, $this->uriTrim); |
|
| 284 | + return $this; |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * Set the route segments informations |
|
| 289 | + * @param array $segements the route segments information |
|
| 290 | + * |
|
| 291 | + * @return object |
|
| 292 | + */ |
|
| 293 | + public function setRouteSegments(array $segments = array()){ |
|
| 294 | + if(! empty($segments)){ |
|
| 295 | + $this->segments = $segments; |
|
| 296 | + } else if (!empty($this->uri)) { |
|
| 297 | + $this->segments = explode('/', $this->uri); |
|
| 298 | + } |
|
| 299 | + $segment = $this->segments; |
|
| 300 | + $baseUrl = get_config('base_url'); |
|
| 301 | + //check if the app is not in DOCUMENT_ROOT |
|
| 302 | + if(isset($segment[0]) && stripos($baseUrl, $segment[0]) !== false){ |
|
| 303 | + array_shift($segment); |
|
| 304 | + $this->segments = $segment; |
|
| 305 | + } |
|
| 306 | + $this->logger->debug('Check if the request URI contains the front controller'); |
|
| 307 | + if(isset($segment[0]) && $segment[0] == SELF){ |
|
| 308 | + $this->logger->info('The request URI contains the front controller'); |
|
| 309 | + array_shift($segment); |
|
| 310 | + $this->segments = $segment; |
|
| 311 | + } |
|
| 312 | + return $this; |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * Setting the route parameters like module, controller, method, argument |
|
| 317 | + * @return object the current instance |
|
| 318 | + */ |
|
| 319 | + public function determineRouteParamsInformation() { |
|
| 320 | + $this->logger->debug('Routing process start ...'); |
|
| 321 | 321 | |
| 322 | - //determine route parameters using the config |
|
| 323 | - $this->determineRouteParamsFromConfig(); |
|
| 322 | + //determine route parameters using the config |
|
| 323 | + $this->determineRouteParamsFromConfig(); |
|
| 324 | 324 | |
| 325 | - //if can not determine the module/controller/method via the defined routes configuration we will use |
|
| 326 | - //the URL like http://domain.com/module/controller/method/arg1/arg2 |
|
| 327 | - if(! $this->controller){ |
|
| 328 | - $this->logger->info('Cannot determine the routing information using the predefined routes configuration, will use the request URI parameters'); |
|
| 329 | - //determine route parameters using the REQUEST_URI param |
|
| 330 | - $this->determineRouteParamsFromRequestUri(); |
|
| 331 | - } |
|
| 332 | - //Set the controller file path if not yet set |
|
| 333 | - $this->setControllerFilePath(); |
|
| 334 | - $this->logger->debug('Routing process end.'); |
|
| 335 | - |
|
| 336 | - return $this; |
|
| 337 | - } |
|
| 325 | + //if can not determine the module/controller/method via the defined routes configuration we will use |
|
| 326 | + //the URL like http://domain.com/module/controller/method/arg1/arg2 |
|
| 327 | + if(! $this->controller){ |
|
| 328 | + $this->logger->info('Cannot determine the routing information using the predefined routes configuration, will use the request URI parameters'); |
|
| 329 | + //determine route parameters using the REQUEST_URI param |
|
| 330 | + $this->determineRouteParamsFromRequestUri(); |
|
| 331 | + } |
|
| 332 | + //Set the controller file path if not yet set |
|
| 333 | + $this->setControllerFilePath(); |
|
| 334 | + $this->logger->debug('Routing process end.'); |
|
| 335 | + |
|
| 336 | + return $this; |
|
| 337 | + } |
|
| 338 | 338 | |
| 339 | - /** |
|
| 340 | - * Routing the request to the correspondant module/controller/method if exists |
|
| 341 | - * otherwise send 404 error. |
|
| 342 | - */ |
|
| 343 | - public function processRequest(){ |
|
| 344 | - //Setting the route URI |
|
| 345 | - $this->setRouteUri(); |
|
| 346 | - |
|
| 347 | - //setting route segments |
|
| 348 | - $this->setRouteSegments(); |
|
| 349 | - |
|
| 350 | - $this->logger->info('The final Request URI is [' . implode('/', $this->segments) . ']' ); |
|
| 351 | - |
|
| 352 | - //determine the route parameters information |
|
| 353 | - $this->determineRouteParamsInformation(); |
|
| 354 | - |
|
| 355 | - $e404 = false; |
|
| 356 | - $classFilePath = $this->controllerPath; |
|
| 357 | - $controller = ucfirst($this->controller); |
|
| 358 | - $this->logger->info('The routing information are: module [' . $this->module . '], controller [' . $controller . '], method [' . $this->method . '], args [' . stringfy_vars($this->args) . ']'); |
|
| 359 | - $this->logger->debug('Loading controller [' . $controller . '], the file path is [' . $classFilePath . ']...'); |
|
| 339 | + /** |
|
| 340 | + * Routing the request to the correspondant module/controller/method if exists |
|
| 341 | + * otherwise send 404 error. |
|
| 342 | + */ |
|
| 343 | + public function processRequest(){ |
|
| 344 | + //Setting the route URI |
|
| 345 | + $this->setRouteUri(); |
|
| 346 | + |
|
| 347 | + //setting route segments |
|
| 348 | + $this->setRouteSegments(); |
|
| 349 | + |
|
| 350 | + $this->logger->info('The final Request URI is [' . implode('/', $this->segments) . ']' ); |
|
| 351 | + |
|
| 352 | + //determine the route parameters information |
|
| 353 | + $this->determineRouteParamsInformation(); |
|
| 354 | + |
|
| 355 | + $e404 = false; |
|
| 356 | + $classFilePath = $this->controllerPath; |
|
| 357 | + $controller = ucfirst($this->controller); |
|
| 358 | + $this->logger->info('The routing information are: module [' . $this->module . '], controller [' . $controller . '], method [' . $this->method . '], args [' . stringfy_vars($this->args) . ']'); |
|
| 359 | + $this->logger->debug('Loading controller [' . $controller . '], the file path is [' . $classFilePath . ']...'); |
|
| 360 | 360 | |
| 361 | - if(file_exists($classFilePath)){ |
|
| 362 | - require_once $classFilePath; |
|
| 363 | - if(! class_exists($controller, false)){ |
|
| 364 | - $e404 = true; |
|
| 365 | - $this->logger->warning('The controller file [' .$classFilePath. '] exists but does not contain the class [' . $controller . ']'); |
|
| 366 | - } |
|
| 367 | - else{ |
|
| 368 | - $controllerInstance = new $controller(); |
|
| 369 | - $controllerMethod = $this->getMethod(); |
|
| 370 | - if(! method_exists($controllerInstance, $controllerMethod)){ |
|
| 371 | - $e404 = true; |
|
| 372 | - $this->logger->warning('The controller [' . $controller . '] exist but does not contain the method [' . $controllerMethod . ']'); |
|
| 373 | - } |
|
| 374 | - else{ |
|
| 375 | - $this->logger->info('Routing data is set correctly now GO!'); |
|
| 376 | - call_user_func_array(array($controllerInstance, $controllerMethod), $this->args); |
|
| 377 | - //render the final page to user |
|
| 378 | - $this->logger->info('Render the final output to the browser'); |
|
| 379 | - get_instance()->response->renderFinalPage(); |
|
| 380 | - } |
|
| 381 | - } |
|
| 382 | - } |
|
| 383 | - else{ |
|
| 384 | - $this->logger->info('The controller file path [' . $classFilePath . '] does not exist'); |
|
| 385 | - $e404 = true; |
|
| 386 | - } |
|
| 387 | - if($e404){ |
|
| 388 | - if(IS_CLI){ |
|
| 389 | - set_http_status_header(404); |
|
| 390 | - echo 'Error 404: page not found.'; |
|
| 391 | - } else { |
|
| 392 | - $response =& class_loader('Response', 'classes'); |
|
| 393 | - $response->send404(); |
|
| 394 | - } |
|
| 395 | - } |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - |
|
| 399 | - /** |
|
| 400 | - * Setting the route configuration using the configuration file and additional configuration from param |
|
| 401 | - * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
| 402 | - * @param boolean $useConfigFile whether to use route configuration file |
|
| 403 | - * @return object |
|
| 404 | - */ |
|
| 405 | - public function setRouteConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
|
| 406 | - $route = array(); |
|
| 407 | - if ($useConfigFile && file_exists(CONFIG_PATH . 'routes.php')){ |
|
| 408 | - require_once CONFIG_PATH . 'routes.php'; |
|
| 409 | - } |
|
| 410 | - $route = array_merge($route, $overwriteConfig); |
|
| 411 | - $this->routes = $route; |
|
| 412 | - //if route is empty remove all configuration |
|
| 413 | - if(empty($route)){ |
|
| 414 | - $this->removeAllRoute(); |
|
| 415 | - } |
|
| 416 | - return $this; |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - /** |
|
| 420 | - * Get the route configuration |
|
| 421 | - * @return array |
|
| 422 | - */ |
|
| 423 | - public function getRouteConfiguration(){ |
|
| 424 | - return $this->routes; |
|
| 425 | - } |
|
| 361 | + if(file_exists($classFilePath)){ |
|
| 362 | + require_once $classFilePath; |
|
| 363 | + if(! class_exists($controller, false)){ |
|
| 364 | + $e404 = true; |
|
| 365 | + $this->logger->warning('The controller file [' .$classFilePath. '] exists but does not contain the class [' . $controller . ']'); |
|
| 366 | + } |
|
| 367 | + else{ |
|
| 368 | + $controllerInstance = new $controller(); |
|
| 369 | + $controllerMethod = $this->getMethod(); |
|
| 370 | + if(! method_exists($controllerInstance, $controllerMethod)){ |
|
| 371 | + $e404 = true; |
|
| 372 | + $this->logger->warning('The controller [' . $controller . '] exist but does not contain the method [' . $controllerMethod . ']'); |
|
| 373 | + } |
|
| 374 | + else{ |
|
| 375 | + $this->logger->info('Routing data is set correctly now GO!'); |
|
| 376 | + call_user_func_array(array($controllerInstance, $controllerMethod), $this->args); |
|
| 377 | + //render the final page to user |
|
| 378 | + $this->logger->info('Render the final output to the browser'); |
|
| 379 | + get_instance()->response->renderFinalPage(); |
|
| 380 | + } |
|
| 381 | + } |
|
| 382 | + } |
|
| 383 | + else{ |
|
| 384 | + $this->logger->info('The controller file path [' . $classFilePath . '] does not exist'); |
|
| 385 | + $e404 = true; |
|
| 386 | + } |
|
| 387 | + if($e404){ |
|
| 388 | + if(IS_CLI){ |
|
| 389 | + set_http_status_header(404); |
|
| 390 | + echo 'Error 404: page not found.'; |
|
| 391 | + } else { |
|
| 392 | + $response =& class_loader('Response', 'classes'); |
|
| 393 | + $response->send404(); |
|
| 394 | + } |
|
| 395 | + } |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + |
|
| 399 | + /** |
|
| 400 | + * Setting the route configuration using the configuration file and additional configuration from param |
|
| 401 | + * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
| 402 | + * @param boolean $useConfigFile whether to use route configuration file |
|
| 403 | + * @return object |
|
| 404 | + */ |
|
| 405 | + public function setRouteConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
|
| 406 | + $route = array(); |
|
| 407 | + if ($useConfigFile && file_exists(CONFIG_PATH . 'routes.php')){ |
|
| 408 | + require_once CONFIG_PATH . 'routes.php'; |
|
| 409 | + } |
|
| 410 | + $route = array_merge($route, $overwriteConfig); |
|
| 411 | + $this->routes = $route; |
|
| 412 | + //if route is empty remove all configuration |
|
| 413 | + if(empty($route)){ |
|
| 414 | + $this->removeAllRoute(); |
|
| 415 | + } |
|
| 416 | + return $this; |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + /** |
|
| 420 | + * Get the route configuration |
|
| 421 | + * @return array |
|
| 422 | + */ |
|
| 423 | + public function getRouteConfiguration(){ |
|
| 424 | + return $this->routes; |
|
| 425 | + } |
|
| 426 | 426 | |
| 427 | 427 | |
| 428 | - /** |
|
| 429 | - * Set the controller file path if is not set |
|
| 430 | - * @param string $path the file path if is null will using the route |
|
| 431 | - * information |
|
| 432 | - * |
|
| 433 | - * @return object the current instance |
|
| 434 | - */ |
|
| 435 | - public function setControllerFilePath($path = null){ |
|
| 436 | - if($path !== null){ |
|
| 437 | - $this->controllerPath = $path; |
|
| 438 | - return $this; |
|
| 439 | - } |
|
| 440 | - //did we set the controller, so set the controller path |
|
| 441 | - if($this->controller && ! $this->controllerPath){ |
|
| 442 | - $this->logger->debug('Setting the file path for the controller [' . $this->controller . ']'); |
|
| 443 | - $controllerPath = APPS_CONTROLLER_PATH . ucfirst($this->controller) . '.php'; |
|
| 444 | - //if the controller is in module |
|
| 445 | - if($this->module){ |
|
| 446 | - $path = Module::findControllerFullPath(ucfirst($this->controller), $this->module); |
|
| 447 | - if($path !== false){ |
|
| 448 | - $controllerPath = $path; |
|
| 449 | - } |
|
| 450 | - } |
|
| 451 | - $this->controllerPath = $controllerPath; |
|
| 452 | - } |
|
| 453 | - return $this; |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - /** |
|
| 457 | - * Determine the route parameters from route configuration |
|
| 458 | - * @return void |
|
| 459 | - */ |
|
| 460 | - protected function determineRouteParamsFromConfig(){ |
|
| 461 | - $uri = implode('/', $this->segments); |
|
| 462 | - /* |
|
| 428 | + /** |
|
| 429 | + * Set the controller file path if is not set |
|
| 430 | + * @param string $path the file path if is null will using the route |
|
| 431 | + * information |
|
| 432 | + * |
|
| 433 | + * @return object the current instance |
|
| 434 | + */ |
|
| 435 | + public function setControllerFilePath($path = null){ |
|
| 436 | + if($path !== null){ |
|
| 437 | + $this->controllerPath = $path; |
|
| 438 | + return $this; |
|
| 439 | + } |
|
| 440 | + //did we set the controller, so set the controller path |
|
| 441 | + if($this->controller && ! $this->controllerPath){ |
|
| 442 | + $this->logger->debug('Setting the file path for the controller [' . $this->controller . ']'); |
|
| 443 | + $controllerPath = APPS_CONTROLLER_PATH . ucfirst($this->controller) . '.php'; |
|
| 444 | + //if the controller is in module |
|
| 445 | + if($this->module){ |
|
| 446 | + $path = Module::findControllerFullPath(ucfirst($this->controller), $this->module); |
|
| 447 | + if($path !== false){ |
|
| 448 | + $controllerPath = $path; |
|
| 449 | + } |
|
| 450 | + } |
|
| 451 | + $this->controllerPath = $controllerPath; |
|
| 452 | + } |
|
| 453 | + return $this; |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + /** |
|
| 457 | + * Determine the route parameters from route configuration |
|
| 458 | + * @return void |
|
| 459 | + */ |
|
| 460 | + protected function determineRouteParamsFromConfig(){ |
|
| 461 | + $uri = implode('/', $this->segments); |
|
| 462 | + /* |
|
| 463 | 463 | * Generics routes patterns |
| 464 | 464 | */ |
| 465 | - $pattern = array(':num', ':alpha', ':alnum', ':any'); |
|
| 466 | - $replace = array('[0-9]+', '[a-zA-Z]+', '[a-zA-Z0-9]+', '.*'); |
|
| 467 | - |
|
| 468 | - $this->logger->debug( |
|
| 469 | - 'Begin to loop in the predefined routes configuration ' |
|
| 470 | - . 'to check if the current request match' |
|
| 471 | - ); |
|
| 472 | - |
|
| 473 | - // Cycle through the URIs stored in the array |
|
| 474 | - foreach ($this->pattern as $index => $uriList) { |
|
| 475 | - $uriList = str_ireplace($pattern, $replace, $uriList); |
|
| 476 | - // Check for an existant matching URI |
|
| 477 | - if (preg_match("#^$uriList$#", $uri, $args)) { |
|
| 478 | - $this->logger->info( |
|
| 479 | - 'Route found for request URI [' . $uri . '] using the predefined configuration ' |
|
| 480 | - . ' [' . $this->pattern[$index] . '] --> [' . $this->callback[$index] . ']' |
|
| 481 | - ); |
|
| 482 | - array_shift($args); |
|
| 483 | - //check if this contains an module |
|
| 484 | - $moduleControllerMethod = explode('#', $this->callback[$index]); |
|
| 485 | - if(is_array($moduleControllerMethod) && count($moduleControllerMethod) >= 2){ |
|
| 486 | - $this->logger->info('The current request use the module [' . $moduleControllerMethod[0] . ']'); |
|
| 487 | - $this->module = $moduleControllerMethod[0]; |
|
| 488 | - $moduleControllerMethod = explode('@', $moduleControllerMethod[1]); |
|
| 489 | - } |
|
| 490 | - else{ |
|
| 491 | - $this->logger->info('The current request does not use the module'); |
|
| 492 | - $moduleControllerMethod = explode('@', $this->callback[$index]); |
|
| 493 | - } |
|
| 494 | - if(is_array($moduleControllerMethod)){ |
|
| 495 | - if(isset($moduleControllerMethod[0])){ |
|
| 496 | - $this->controller = $moduleControllerMethod[0]; |
|
| 497 | - } |
|
| 498 | - if(isset($moduleControllerMethod[1])){ |
|
| 499 | - $this->method = $moduleControllerMethod[1]; |
|
| 500 | - } |
|
| 501 | - $this->args = $args; |
|
| 502 | - } |
|
| 503 | - // stop here |
|
| 504 | - break; |
|
| 505 | - } |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - //first if the controller is not set and the module is set use the module name as the controller |
|
| 509 | - if(! $this->controller && $this->module){ |
|
| 510 | - $this->logger->info( |
|
| 511 | - 'After loop in predefined routes configuration, |
|
| 465 | + $pattern = array(':num', ':alpha', ':alnum', ':any'); |
|
| 466 | + $replace = array('[0-9]+', '[a-zA-Z]+', '[a-zA-Z0-9]+', '.*'); |
|
| 467 | + |
|
| 468 | + $this->logger->debug( |
|
| 469 | + 'Begin to loop in the predefined routes configuration ' |
|
| 470 | + . 'to check if the current request match' |
|
| 471 | + ); |
|
| 472 | + |
|
| 473 | + // Cycle through the URIs stored in the array |
|
| 474 | + foreach ($this->pattern as $index => $uriList) { |
|
| 475 | + $uriList = str_ireplace($pattern, $replace, $uriList); |
|
| 476 | + // Check for an existant matching URI |
|
| 477 | + if (preg_match("#^$uriList$#", $uri, $args)) { |
|
| 478 | + $this->logger->info( |
|
| 479 | + 'Route found for request URI [' . $uri . '] using the predefined configuration ' |
|
| 480 | + . ' [' . $this->pattern[$index] . '] --> [' . $this->callback[$index] . ']' |
|
| 481 | + ); |
|
| 482 | + array_shift($args); |
|
| 483 | + //check if this contains an module |
|
| 484 | + $moduleControllerMethod = explode('#', $this->callback[$index]); |
|
| 485 | + if(is_array($moduleControllerMethod) && count($moduleControllerMethod) >= 2){ |
|
| 486 | + $this->logger->info('The current request use the module [' . $moduleControllerMethod[0] . ']'); |
|
| 487 | + $this->module = $moduleControllerMethod[0]; |
|
| 488 | + $moduleControllerMethod = explode('@', $moduleControllerMethod[1]); |
|
| 489 | + } |
|
| 490 | + else{ |
|
| 491 | + $this->logger->info('The current request does not use the module'); |
|
| 492 | + $moduleControllerMethod = explode('@', $this->callback[$index]); |
|
| 493 | + } |
|
| 494 | + if(is_array($moduleControllerMethod)){ |
|
| 495 | + if(isset($moduleControllerMethod[0])){ |
|
| 496 | + $this->controller = $moduleControllerMethod[0]; |
|
| 497 | + } |
|
| 498 | + if(isset($moduleControllerMethod[1])){ |
|
| 499 | + $this->method = $moduleControllerMethod[1]; |
|
| 500 | + } |
|
| 501 | + $this->args = $args; |
|
| 502 | + } |
|
| 503 | + // stop here |
|
| 504 | + break; |
|
| 505 | + } |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + //first if the controller is not set and the module is set use the module name as the controller |
|
| 509 | + if(! $this->controller && $this->module){ |
|
| 510 | + $this->logger->info( |
|
| 511 | + 'After loop in predefined routes configuration, |
|
| 512 | 512 | the module name is set but the controller is not set, |
| 513 | 513 | so we will use module as the controller' |
| 514 | - ); |
|
| 515 | - $this->controller = $this->module; |
|
| 516 | - } |
|
| 517 | - } |
|
| 518 | - |
|
| 519 | - /** |
|
| 520 | - * Determine the route parameters using the server variable "REQUEST_URI" |
|
| 521 | - * @return void |
|
| 522 | - */ |
|
| 523 | - protected function determineRouteParamsFromRequestUri(){ |
|
| 524 | - $segment = $this->segments; |
|
| 525 | - $nbSegment = count($segment); |
|
| 526 | - //if segment is null so means no need to perform |
|
| 527 | - if($nbSegment > 0){ |
|
| 528 | - //get the module list |
|
| 529 | - $modules = Module::getModuleList(); |
|
| 530 | - //first check if no module |
|
| 531 | - if(empty($modules)){ |
|
| 532 | - $this->logger->info('No module was loaded will skip the module checking'); |
|
| 533 | - //the application don't use module |
|
| 534 | - //controller |
|
| 535 | - if(isset($segment[0])){ |
|
| 536 | - $this->controller = $segment[0]; |
|
| 537 | - array_shift($segment); |
|
| 538 | - } |
|
| 539 | - //method |
|
| 540 | - if(isset($segment[0])){ |
|
| 541 | - $this->method = $segment[0]; |
|
| 542 | - array_shift($segment); |
|
| 543 | - } |
|
| 544 | - //args |
|
| 545 | - $this->args = $segment; |
|
| 546 | - } |
|
| 547 | - else{ |
|
| 548 | - $this->logger->info('The application contains a loaded module will check if the current request is found in the module list'); |
|
| 549 | - if(in_array($segment[0], $modules)){ |
|
| 550 | - $this->logger->info('Found, the current request use the module [' . $segment[0] . ']'); |
|
| 551 | - $this->module = $segment[0]; |
|
| 552 | - array_shift($segment); |
|
| 553 | - //check if the second arg is the controller from module |
|
| 554 | - if(isset($segment[0])){ |
|
| 555 | - $this->controller = $segment[0]; |
|
| 556 | - //check if the request use the same module name and controller |
|
| 557 | - $path = Module::findControllerFullPath(ucfirst($this->controller), $this->module); |
|
| 558 | - if(! $path){ |
|
| 559 | - $this->logger->info('The controller [' . $this->controller . '] not found in the module, may be will use the module [' . $this->module . '] as controller'); |
|
| 560 | - $this->controller = $this->module; |
|
| 561 | - } |
|
| 562 | - else{ |
|
| 563 | - $this->controllerPath = $path; |
|
| 564 | - array_shift($segment); |
|
| 565 | - } |
|
| 566 | - } |
|
| 567 | - //check for method |
|
| 568 | - if(isset($segment[0])){ |
|
| 569 | - $this->method = $segment[0]; |
|
| 570 | - array_shift($segment); |
|
| 571 | - } |
|
| 572 | - //the remaining is for args |
|
| 573 | - $this->args = $segment; |
|
| 574 | - } |
|
| 575 | - else{ |
|
| 576 | - $this->logger->info('The current request information is not found in the module list'); |
|
| 577 | - //controller |
|
| 578 | - if(isset($segment[0])){ |
|
| 579 | - $this->controller = $segment[0]; |
|
| 580 | - array_shift($segment); |
|
| 581 | - } |
|
| 582 | - //method |
|
| 583 | - if(isset($segment[0])){ |
|
| 584 | - $this->method = $segment[0]; |
|
| 585 | - array_shift($segment); |
|
| 586 | - } |
|
| 587 | - //args |
|
| 588 | - $this->args = $segment; |
|
| 589 | - } |
|
| 590 | - } |
|
| 591 | - if(! $this->controller && $this->module){ |
|
| 592 | - $this->logger->info('After using the request URI the module name is set but the controller is not set so we will use module as the controller'); |
|
| 593 | - $this->controller = $this->module; |
|
| 594 | - } |
|
| 595 | - } |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - /** |
|
| 599 | - * Set the route informations using the configuration |
|
| 600 | - * |
|
| 601 | - * @return object the current instance |
|
| 602 | - */ |
|
| 603 | - protected function setRouteConfigurationInfos(){ |
|
| 604 | - //adding route |
|
| 605 | - foreach($this->routes as $pattern => $callback){ |
|
| 606 | - $this->add($pattern, $callback); |
|
| 607 | - } |
|
| 608 | - return $this; |
|
| 609 | - } |
|
| 610 | - |
|
| 611 | - /** |
|
| 612 | - * Set the Log instance using argument or create new instance |
|
| 613 | - * @param object $logger the Log instance if not null |
|
| 614 | - */ |
|
| 615 | - protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 616 | - if ($logger !== null){ |
|
| 617 | - $this->logger = $logger; |
|
| 618 | - } |
|
| 619 | - else{ |
|
| 620 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 621 | - $this->logger->setLogger('Library::Router'); |
|
| 622 | - } |
|
| 623 | - } |
|
| 624 | - } |
|
| 514 | + ); |
|
| 515 | + $this->controller = $this->module; |
|
| 516 | + } |
|
| 517 | + } |
|
| 518 | + |
|
| 519 | + /** |
|
| 520 | + * Determine the route parameters using the server variable "REQUEST_URI" |
|
| 521 | + * @return void |
|
| 522 | + */ |
|
| 523 | + protected function determineRouteParamsFromRequestUri(){ |
|
| 524 | + $segment = $this->segments; |
|
| 525 | + $nbSegment = count($segment); |
|
| 526 | + //if segment is null so means no need to perform |
|
| 527 | + if($nbSegment > 0){ |
|
| 528 | + //get the module list |
|
| 529 | + $modules = Module::getModuleList(); |
|
| 530 | + //first check if no module |
|
| 531 | + if(empty($modules)){ |
|
| 532 | + $this->logger->info('No module was loaded will skip the module checking'); |
|
| 533 | + //the application don't use module |
|
| 534 | + //controller |
|
| 535 | + if(isset($segment[0])){ |
|
| 536 | + $this->controller = $segment[0]; |
|
| 537 | + array_shift($segment); |
|
| 538 | + } |
|
| 539 | + //method |
|
| 540 | + if(isset($segment[0])){ |
|
| 541 | + $this->method = $segment[0]; |
|
| 542 | + array_shift($segment); |
|
| 543 | + } |
|
| 544 | + //args |
|
| 545 | + $this->args = $segment; |
|
| 546 | + } |
|
| 547 | + else{ |
|
| 548 | + $this->logger->info('The application contains a loaded module will check if the current request is found in the module list'); |
|
| 549 | + if(in_array($segment[0], $modules)){ |
|
| 550 | + $this->logger->info('Found, the current request use the module [' . $segment[0] . ']'); |
|
| 551 | + $this->module = $segment[0]; |
|
| 552 | + array_shift($segment); |
|
| 553 | + //check if the second arg is the controller from module |
|
| 554 | + if(isset($segment[0])){ |
|
| 555 | + $this->controller = $segment[0]; |
|
| 556 | + //check if the request use the same module name and controller |
|
| 557 | + $path = Module::findControllerFullPath(ucfirst($this->controller), $this->module); |
|
| 558 | + if(! $path){ |
|
| 559 | + $this->logger->info('The controller [' . $this->controller . '] not found in the module, may be will use the module [' . $this->module . '] as controller'); |
|
| 560 | + $this->controller = $this->module; |
|
| 561 | + } |
|
| 562 | + else{ |
|
| 563 | + $this->controllerPath = $path; |
|
| 564 | + array_shift($segment); |
|
| 565 | + } |
|
| 566 | + } |
|
| 567 | + //check for method |
|
| 568 | + if(isset($segment[0])){ |
|
| 569 | + $this->method = $segment[0]; |
|
| 570 | + array_shift($segment); |
|
| 571 | + } |
|
| 572 | + //the remaining is for args |
|
| 573 | + $this->args = $segment; |
|
| 574 | + } |
|
| 575 | + else{ |
|
| 576 | + $this->logger->info('The current request information is not found in the module list'); |
|
| 577 | + //controller |
|
| 578 | + if(isset($segment[0])){ |
|
| 579 | + $this->controller = $segment[0]; |
|
| 580 | + array_shift($segment); |
|
| 581 | + } |
|
| 582 | + //method |
|
| 583 | + if(isset($segment[0])){ |
|
| 584 | + $this->method = $segment[0]; |
|
| 585 | + array_shift($segment); |
|
| 586 | + } |
|
| 587 | + //args |
|
| 588 | + $this->args = $segment; |
|
| 589 | + } |
|
| 590 | + } |
|
| 591 | + if(! $this->controller && $this->module){ |
|
| 592 | + $this->logger->info('After using the request URI the module name is set but the controller is not set so we will use module as the controller'); |
|
| 593 | + $this->controller = $this->module; |
|
| 594 | + } |
|
| 595 | + } |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + /** |
|
| 599 | + * Set the route informations using the configuration |
|
| 600 | + * |
|
| 601 | + * @return object the current instance |
|
| 602 | + */ |
|
| 603 | + protected function setRouteConfigurationInfos(){ |
|
| 604 | + //adding route |
|
| 605 | + foreach($this->routes as $pattern => $callback){ |
|
| 606 | + $this->add($pattern, $callback); |
|
| 607 | + } |
|
| 608 | + return $this; |
|
| 609 | + } |
|
| 610 | + |
|
| 611 | + /** |
|
| 612 | + * Set the Log instance using argument or create new instance |
|
| 613 | + * @param object $logger the Log instance if not null |
|
| 614 | + */ |
|
| 615 | + protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 616 | + if ($logger !== null){ |
|
| 617 | + $this->logger = $logger; |
|
| 618 | + } |
|
| 619 | + else{ |
|
| 620 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 621 | + $this->logger->setLogger('Library::Router'); |
|
| 622 | + } |
|
| 623 | + } |
|
| 624 | + } |
|
@@ -1,157 +1,157 @@ |
||
| 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 Security{ |
|
| 27 | + class Security{ |
|
| 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 | - * Get the logger singleton instance |
|
| 37 | - * @return Log the logger 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::Security'); |
|
| 43 | - } |
|
| 44 | - return self::$logger[0]; |
|
| 45 | - } |
|
| 35 | + /** |
|
| 36 | + * Get the logger singleton instance |
|
| 37 | + * @return Log the logger 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::Security'); |
|
| 43 | + } |
|
| 44 | + return self::$logger[0]; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * This method is used to generate the CSRF token |
|
| 50 | - * @return string the generated CSRF token |
|
| 51 | - */ |
|
| 52 | - public static function generateCSRF(){ |
|
| 53 | - $logger = self::getLogger(); |
|
| 54 | - $logger->debug('Generation of CSRF ...'); |
|
| 48 | + /** |
|
| 49 | + * This method is used to generate the CSRF token |
|
| 50 | + * @return string the generated CSRF token |
|
| 51 | + */ |
|
| 52 | + public static function generateCSRF(){ |
|
| 53 | + $logger = self::getLogger(); |
|
| 54 | + $logger->debug('Generation of CSRF ...'); |
|
| 55 | 55 | |
| 56 | - $key = get_config('csrf_key', 'csrf_key'); |
|
| 57 | - $expire = get_config('csrf_expire', 60); |
|
| 58 | - $keyExpire = 'csrf_expire'; |
|
| 59 | - $currentTime = time(); |
|
| 60 | - if(Session::exists($key) && Session::exists($keyExpire) && Session::get($keyExpire) > $currentTime){ |
|
| 61 | - $logger->info('The CSRF token not yet expire just return it'); |
|
| 62 | - return Session::get($key); |
|
| 63 | - } |
|
| 64 | - else{ |
|
| 65 | - $newTime = $currentTime + $expire; |
|
| 66 | - $token = sha1(uniqid()) . sha1(uniqid()); |
|
| 67 | - $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. '], token [' .$token. ']'); |
|
| 68 | - Session::set($keyExpire, $newTime); |
|
| 69 | - Session::set($key, $token); |
|
| 70 | - return Session::get($key); |
|
| 71 | - } |
|
| 72 | - } |
|
| 56 | + $key = get_config('csrf_key', 'csrf_key'); |
|
| 57 | + $expire = get_config('csrf_expire', 60); |
|
| 58 | + $keyExpire = 'csrf_expire'; |
|
| 59 | + $currentTime = time(); |
|
| 60 | + if(Session::exists($key) && Session::exists($keyExpire) && Session::get($keyExpire) > $currentTime){ |
|
| 61 | + $logger->info('The CSRF token not yet expire just return it'); |
|
| 62 | + return Session::get($key); |
|
| 63 | + } |
|
| 64 | + else{ |
|
| 65 | + $newTime = $currentTime + $expire; |
|
| 66 | + $token = sha1(uniqid()) . sha1(uniqid()); |
|
| 67 | + $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. '], token [' .$token. ']'); |
|
| 68 | + Session::set($keyExpire, $newTime); |
|
| 69 | + Session::set($key, $token); |
|
| 70 | + return Session::get($key); |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * This method is used to check the CSRF if is valid, not yet expire, etc. |
|
| 76 | - * @return boolean true if valid, false if not valid |
|
| 77 | - */ |
|
| 78 | - public static function validateCSRF(){ |
|
| 79 | - $logger = self::getLogger(); |
|
| 80 | - $logger->debug('Validation of CSRF ...'); |
|
| 74 | + /** |
|
| 75 | + * This method is used to check the CSRF if is valid, not yet expire, etc. |
|
| 76 | + * @return boolean true if valid, false if not valid |
|
| 77 | + */ |
|
| 78 | + public static function validateCSRF(){ |
|
| 79 | + $logger = self::getLogger(); |
|
| 80 | + $logger->debug('Validation of CSRF ...'); |
|
| 81 | 81 | |
| 82 | - $key = get_config('csrf_key', 'csrf_key'); |
|
| 83 | - $expire = get_config('csrf_expire', 60); |
|
| 84 | - $keyExpire = 'csrf_expire'; |
|
| 85 | - $currentTime = time(); |
|
| 86 | - $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. ']'); |
|
| 87 | - if(! Session::exists($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 88 | - $logger->warning('The CSRF session data is not valide'); |
|
| 89 | - return false; |
|
| 90 | - } |
|
| 91 | - //perform form data |
|
| 92 | - //need use request->query() for best retrieve |
|
| 93 | - //super instance |
|
| 94 | - $obj = & get_instance(); |
|
| 95 | - $token = $obj->request->query($key); |
|
| 96 | - if(! $token || $token !== Session::get($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 97 | - $logger->warning('The CSRF data [' .$token. '] is not valide may be attacker do his job'); |
|
| 98 | - return false; |
|
| 99 | - } |
|
| 100 | - $logger->info('The CSRF data [' .$token. '] is valide the form data is safe continue'); |
|
| 101 | - //remove the token from session |
|
| 102 | - Session::clear($key); |
|
| 103 | - Session::clear($keyExpire); |
|
| 104 | - return true; |
|
| 105 | - } |
|
| 82 | + $key = get_config('csrf_key', 'csrf_key'); |
|
| 83 | + $expire = get_config('csrf_expire', 60); |
|
| 84 | + $keyExpire = 'csrf_expire'; |
|
| 85 | + $currentTime = time(); |
|
| 86 | + $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. ']'); |
|
| 87 | + if(! Session::exists($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 88 | + $logger->warning('The CSRF session data is not valide'); |
|
| 89 | + return false; |
|
| 90 | + } |
|
| 91 | + //perform form data |
|
| 92 | + //need use request->query() for best retrieve |
|
| 93 | + //super instance |
|
| 94 | + $obj = & get_instance(); |
|
| 95 | + $token = $obj->request->query($key); |
|
| 96 | + if(! $token || $token !== Session::get($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 97 | + $logger->warning('The CSRF data [' .$token. '] is not valide may be attacker do his job'); |
|
| 98 | + return false; |
|
| 99 | + } |
|
| 100 | + $logger->info('The CSRF data [' .$token. '] is valide the form data is safe continue'); |
|
| 101 | + //remove the token from session |
|
| 102 | + Session::clear($key); |
|
| 103 | + Session::clear($keyExpire); |
|
| 104 | + return true; |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * This method is used to check the whitelist IP address access |
|
| 109 | - */ |
|
| 110 | - public static function checkWhiteListIpAccess(){ |
|
| 111 | - $logger = self::getLogger(); |
|
| 112 | - $logger->debug('Validation of the IP address access ...'); |
|
| 113 | - $logger->debug('Check if whitelist IP access is enabled in the configuration ...'); |
|
| 114 | - $isEnable = get_config('white_list_ip_enable', false); |
|
| 115 | - if($isEnable){ |
|
| 116 | - $logger->info('Whitelist IP access is enabled in the configuration'); |
|
| 117 | - $list = get_config('white_list_ip_addresses', array()); |
|
| 118 | - if(! empty($list)){ |
|
| 119 | - //Can't use Loader::functions() at this time because teh "Loader" library is loader after the security prossessing |
|
| 120 | - require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 121 | - $ip = get_ip(); |
|
| 122 | - if((count($list) == 1 && $list[0] == '*') || in_array($ip, $list)){ |
|
| 123 | - $logger->info('IP address ' . $ip . ' allowed using the wildcard "*" or the full IP'); |
|
| 124 | - //wildcard to access all ip address |
|
| 125 | - return; |
|
| 126 | - } |
|
| 127 | - else{ |
|
| 128 | - // go through all whitelisted ips |
|
| 129 | - foreach ($list as $ipaddr) { |
|
| 130 | - // find the wild card * in whitelisted ip (f.e. find position in "127.0.*" or "127*") |
|
| 131 | - $wildcardPosition = strpos($ipaddr, '*'); |
|
| 132 | - if ($wildcardPosition === false) { |
|
| 133 | - // no wild card in whitelisted ip --continue searching |
|
| 134 | - continue; |
|
| 135 | - } |
|
| 136 | - // cut ip at the position where we got the wild card on the whitelisted ip |
|
| 137 | - // and add the wold card to get the same pattern |
|
| 138 | - if (substr($ip, 0, $wildcardPosition) . '*' === $ipaddr) { |
|
| 139 | - // f.e. we got |
|
| 140 | - // ip "127.0.0.1" |
|
| 141 | - // whitelisted ip "127.0.*" |
|
| 142 | - // then we compared "127.0.*" with "127.0.*" |
|
| 143 | - // return success |
|
| 144 | - $logger->info('IP address ' . $ip . ' allowed using the wildcard like "x.x.x.*"'); |
|
| 145 | - return; |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - $logger->warning('IP address ' . $ip . ' is not allowed to access to this application'); |
|
| 149 | - show_error('Access to this application is not allowed'); |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - else{ |
|
| 154 | - $logger->info('Whitelist IP access is not enabled in the configuration, ignore checking'); |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - } |
|
| 107 | + /** |
|
| 108 | + * This method is used to check the whitelist IP address access |
|
| 109 | + */ |
|
| 110 | + public static function checkWhiteListIpAccess(){ |
|
| 111 | + $logger = self::getLogger(); |
|
| 112 | + $logger->debug('Validation of the IP address access ...'); |
|
| 113 | + $logger->debug('Check if whitelist IP access is enabled in the configuration ...'); |
|
| 114 | + $isEnable = get_config('white_list_ip_enable', false); |
|
| 115 | + if($isEnable){ |
|
| 116 | + $logger->info('Whitelist IP access is enabled in the configuration'); |
|
| 117 | + $list = get_config('white_list_ip_addresses', array()); |
|
| 118 | + if(! empty($list)){ |
|
| 119 | + //Can't use Loader::functions() at this time because teh "Loader" library is loader after the security prossessing |
|
| 120 | + require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 121 | + $ip = get_ip(); |
|
| 122 | + if((count($list) == 1 && $list[0] == '*') || in_array($ip, $list)){ |
|
| 123 | + $logger->info('IP address ' . $ip . ' allowed using the wildcard "*" or the full IP'); |
|
| 124 | + //wildcard to access all ip address |
|
| 125 | + return; |
|
| 126 | + } |
|
| 127 | + else{ |
|
| 128 | + // go through all whitelisted ips |
|
| 129 | + foreach ($list as $ipaddr) { |
|
| 130 | + // find the wild card * in whitelisted ip (f.e. find position in "127.0.*" or "127*") |
|
| 131 | + $wildcardPosition = strpos($ipaddr, '*'); |
|
| 132 | + if ($wildcardPosition === false) { |
|
| 133 | + // no wild card in whitelisted ip --continue searching |
|
| 134 | + continue; |
|
| 135 | + } |
|
| 136 | + // cut ip at the position where we got the wild card on the whitelisted ip |
|
| 137 | + // and add the wold card to get the same pattern |
|
| 138 | + if (substr($ip, 0, $wildcardPosition) . '*' === $ipaddr) { |
|
| 139 | + // f.e. we got |
|
| 140 | + // ip "127.0.0.1" |
|
| 141 | + // whitelisted ip "127.0.*" |
|
| 142 | + // then we compared "127.0.*" with "127.0.*" |
|
| 143 | + // return success |
|
| 144 | + $logger->info('IP address ' . $ip . ' allowed using the wildcard like "x.x.x.*"'); |
|
| 145 | + return; |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | + $logger->warning('IP address ' . $ip . ' is not allowed to access to this application'); |
|
| 149 | + show_error('Access to this application is not allowed'); |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + else{ |
|
| 154 | + $logger->info('Whitelist IP access is not enabled in the configuration, ignore checking'); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + } |
|
@@ -22,10 +22,10 @@ discard block |
||
| 22 | 22 | * You should have received a copy of the GNU General Public License |
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | - */ |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | 27 | |
| 28 | - class FormValidation{ |
|
| 28 | + class FormValidation{ |
|
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | 31 | * The form validation status |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | */ |
| 61 | 61 | protected $_eachErrorDelimiter = array('<p class="error">', '</p>'); |
| 62 | 62 | |
| 63 | - /** |
|
| 63 | + /** |
|
| 64 | 64 | * Indicated if need force the validation to be failed |
| 65 | 65 | * @var boolean |
| 66 | 66 | */ |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | $this->logger =& class_loader('Log', 'classes'); |
| 101 | 101 | $this->logger->setLogger('Library::FormValidation'); |
| 102 | 102 | |
| 103 | - //Load form validation language message |
|
| 103 | + //Load form validation language message |
|
| 104 | 104 | Loader::lang('form_validation'); |
| 105 | 105 | $obj = & get_instance(); |
| 106 | 106 | $this->_errorsMessages = array( |
@@ -162,13 +162,13 @@ discard block |
||
| 162 | 162 | /** |
| 163 | 163 | * Set the form validation data |
| 164 | 164 | * @param array $data the values to be validated |
| 165 | - * |
|
| 165 | + * |
|
| 166 | 166 | * @return FormValidation Current instance of object. |
| 167 | 167 | */ |
| 168 | 168 | public function setData(array $data){ |
| 169 | 169 | $this->logger->debug('Setting the form validation data, the values are: ' . stringfy_vars($data)); |
| 170 | 170 | $this->data = $data; |
| 171 | - return $this; |
|
| 171 | + return $this; |
|
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | /** |
@@ -179,11 +179,11 @@ discard block |
||
| 179 | 179 | return $this->data; |
| 180 | 180 | } |
| 181 | 181 | |
| 182 | - /** |
|
| 183 | - * Get the validation function name to validate a rule |
|
| 184 | - * |
|
| 185 | - * @return string the function name |
|
| 186 | - */ |
|
| 182 | + /** |
|
| 183 | + * Get the validation function name to validate a rule |
|
| 184 | + * |
|
| 185 | + * @return string the function name |
|
| 186 | + */ |
|
| 187 | 187 | protected function _toCallCase($funcName, $prefix='_validate') { |
| 188 | 188 | $funcName = strtolower($funcName); |
| 189 | 189 | $finalFuncName = $prefix; |
@@ -253,12 +253,12 @@ discard block |
||
| 253 | 253 | $this->_forceFail = false; |
| 254 | 254 | |
| 255 | 255 | foreach ($this->getData() as $inputName => $inputVal) { |
| 256 | - if(is_array($this->data[$inputName])){ |
|
| 257 | - $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
|
| 258 | - } |
|
| 259 | - else{ |
|
| 260 | - $this->data[$inputName] = trim($this->data[$inputName]); |
|
| 261 | - } |
|
| 256 | + if(is_array($this->data[$inputName])){ |
|
| 257 | + $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
|
| 258 | + } |
|
| 259 | + else{ |
|
| 260 | + $this->data[$inputName] = trim($this->data[$inputName]); |
|
| 261 | + } |
|
| 262 | 262 | |
| 263 | 263 | if (array_key_exists($inputName, $this->_rules)) { |
| 264 | 264 | foreach ($this->_parseRuleString($this->_rules[$inputName]) as $eachRule) { |
@@ -274,7 +274,7 @@ discard block |
||
| 274 | 274 | * |
| 275 | 275 | * @param string $inputField Name of the field or the data key to add a rule to |
| 276 | 276 | * @param string $ruleSets PIPE seperated string of rules |
| 277 | - * |
|
| 277 | + * |
|
| 278 | 278 | * @return FormValidation Current instance of object. |
| 279 | 279 | */ |
| 280 | 280 | public function setRule($inputField, $inputLabel, $ruleSets) { |
@@ -288,8 +288,8 @@ discard block |
||
| 288 | 288 | * Takes an array of rules and uses setRule() to set them, accepts an array |
| 289 | 289 | * of rule names rather than a pipe-delimited string as well. |
| 290 | 290 | * @param array $ruleSets |
| 291 | - * |
|
| 292 | - * @return FormValidation Current instance of object. |
|
| 291 | + * |
|
| 292 | + * @return FormValidation Current instance of object. |
|
| 293 | 293 | */ |
| 294 | 294 | public function setRules(array $ruleSets) { |
| 295 | 295 | foreach ($ruleSets as $ruleSet) { |
@@ -311,7 +311,7 @@ discard block |
||
| 311 | 311 | * @param string $start Before block of errors gets displayed, HTML allowed. |
| 312 | 312 | * @param string $end After the block of errors gets displayed, HTML allowed. |
| 313 | 313 | * |
| 314 | - * @return FormValidation Current instance of object. |
|
| 314 | + * @return FormValidation Current instance of object. |
|
| 315 | 315 | */ |
| 316 | 316 | public function setErrorsDelimiter($start, $end) { |
| 317 | 317 | $this->_allErrorsDelimiter[0] = $start; |
@@ -326,7 +326,7 @@ discard block |
||
| 326 | 326 | * @param string $start Displayed before each error. |
| 327 | 327 | * @param string $end Displayed after each error. |
| 328 | 328 | * |
| 329 | - * @return FormValidation Current instance of object. |
|
| 329 | + * @return FormValidation Current instance of object. |
|
| 330 | 330 | */ |
| 331 | 331 | public function setErrorDelimiter($start, $end) { |
| 332 | 332 | $this->_eachErrorDelimiter[0] = $start; |
@@ -334,21 +334,21 @@ discard block |
||
| 334 | 334 | return $this; |
| 335 | 335 | } |
| 336 | 336 | |
| 337 | - /** |
|
| 338 | - * Get the each errors delimiters |
|
| 339 | - * |
|
| 340 | - * @return array |
|
| 341 | - */ |
|
| 342 | - public function getErrorDelimiter() { |
|
| 337 | + /** |
|
| 338 | + * Get the each errors delimiters |
|
| 339 | + * |
|
| 340 | + * @return array |
|
| 341 | + */ |
|
| 342 | + public function getErrorDelimiter() { |
|
| 343 | 343 | return $this->_eachErrorDelimiter; |
| 344 | 344 | } |
| 345 | 345 | |
| 346 | - /** |
|
| 347 | - * Get the all errors delimiters |
|
| 348 | - * |
|
| 349 | - * @return array |
|
| 350 | - */ |
|
| 351 | - public function getErrorsDelimiter() { |
|
| 346 | + /** |
|
| 347 | + * Get the all errors delimiters |
|
| 348 | + * |
|
| 349 | + * @return array |
|
| 350 | + */ |
|
| 351 | + public function getErrorsDelimiter() { |
|
| 352 | 352 | return $this->_allErrorsDelimiter; |
| 353 | 353 | } |
| 354 | 354 | |
@@ -386,7 +386,7 @@ discard block |
||
| 386 | 386 | * |
| 387 | 387 | * @param string $inputName The form input name or data key |
| 388 | 388 | * @param string $errorMessage Error to display |
| 389 | - * |
|
| 389 | + * |
|
| 390 | 390 | * @return formValidation Current instance of the object |
| 391 | 391 | */ |
| 392 | 392 | public function setCustomError($inputName, $errorMessage) { |
@@ -423,17 +423,17 @@ discard block |
||
| 423 | 423 | * |
| 424 | 424 | * @param boolean $limit number of error to display or return |
| 425 | 425 | * @param boolean $echo Whether or not the values are to be returned or displayed |
| 426 | - * |
|
| 426 | + * |
|
| 427 | 427 | * @return string Errors formatted for output |
| 428 | 428 | */ |
| 429 | 429 | public function displayErrors($limit = null, $echo = true) { |
| 430 | 430 | list($errorsStart, $errorsEnd) = $this->_allErrorsDelimiter; |
| 431 | 431 | list($errorStart, $errorEnd) = $this->_eachErrorDelimiter; |
| 432 | 432 | $errorOutput = $errorsStart; |
| 433 | - $i = 0; |
|
| 433 | + $i = 0; |
|
| 434 | 434 | if (!empty($this->_errors)) { |
| 435 | 435 | foreach ($this->_errors as $fieldName => $error) { |
| 436 | - if ($i === $limit) { |
|
| 436 | + if ($i === $limit) { |
|
| 437 | 437 | break; |
| 438 | 438 | } |
| 439 | 439 | $errorOutput .= $errorStart; |
@@ -461,7 +461,7 @@ discard block |
||
| 461 | 461 | * Breaks up a PIPE seperated string of rules, and puts them into an array. |
| 462 | 462 | * |
| 463 | 463 | * @param string $ruleString String to be parsed. |
| 464 | - * |
|
| 464 | + * |
|
| 465 | 465 | * @return array Array of each value in original string. |
| 466 | 466 | */ |
| 467 | 467 | protected function _parseRuleString($ruleString) { |
@@ -474,10 +474,10 @@ discard block |
||
| 474 | 474 | $rule = '#regex\[\/(.*)\/([a-zA-Z0-9]?)\]#'; |
| 475 | 475 | preg_match($rule, $ruleString, $regexRule); |
| 476 | 476 | $ruleStringTemp = preg_replace($rule, '', $ruleString); |
| 477 | - if(!empty($regexRule[0])){ |
|
| 478 | - $ruleSets[] = $regexRule[0]; |
|
| 479 | - } |
|
| 480 | - $ruleStringRegex = explode('|', $ruleStringTemp); |
|
| 477 | + if(!empty($regexRule[0])){ |
|
| 478 | + $ruleSets[] = $regexRule[0]; |
|
| 479 | + } |
|
| 480 | + $ruleStringRegex = explode('|', $ruleStringTemp); |
|
| 481 | 481 | foreach ($ruleStringRegex as $rule) { |
| 482 | 482 | $rule = trim($rule); |
| 483 | 483 | if($rule){ |
@@ -493,7 +493,7 @@ discard block |
||
| 493 | 493 | } else { |
| 494 | 494 | $ruleSets[] = $ruleString; |
| 495 | 495 | } |
| 496 | - } |
|
| 496 | + } |
|
| 497 | 497 | return $ruleSets; |
| 498 | 498 | } |
| 499 | 499 | |
@@ -501,7 +501,7 @@ discard block |
||
| 501 | 501 | * Returns whether or not a field obtains the rule "required". |
| 502 | 502 | * |
| 503 | 503 | * @param string $fieldName Field to check if required. |
| 504 | - * |
|
| 504 | + * |
|
| 505 | 505 | * @return boolean Whether or not the field is required. |
| 506 | 506 | */ |
| 507 | 507 | protected function _fieldIsRequired($fieldName) { |
@@ -536,13 +536,13 @@ discard block |
||
| 536 | 536 | return; |
| 537 | 537 | } |
| 538 | 538 | |
| 539 | - /** |
|
| 540 | - * Set error for the given field or key |
|
| 541 | - * |
|
| 542 | - * @param string $inputName the input or key name |
|
| 543 | - * @param string $ruleName the rule name |
|
| 544 | - * @param array|string $replacements |
|
| 545 | - */ |
|
| 539 | + /** |
|
| 540 | + * Set error for the given field or key |
|
| 541 | + * |
|
| 542 | + * @param string $inputName the input or key name |
|
| 543 | + * @param string $ruleName the rule name |
|
| 544 | + * @param array|string $replacements |
|
| 545 | + */ |
|
| 546 | 546 | protected function _setError($inputName, $ruleName, $replacements = array()) { |
| 547 | 547 | $rulePhraseKeyParts = explode(',', $ruleName); |
| 548 | 548 | $rulePhrase = null; |
@@ -559,7 +559,7 @@ discard block |
||
| 559 | 559 | } |
| 560 | 560 | // Type cast to array in case it's a string |
| 561 | 561 | $replacements = (array) $replacements; |
| 562 | - $replacementCount = count($replacements); |
|
| 562 | + $replacementCount = count($replacements); |
|
| 563 | 563 | for ($i = 1; $i <= $replacementCount; $i++) { |
| 564 | 564 | $key = $i - 1; |
| 565 | 565 | $rulePhrase = str_replace('%' . $i, $replacements[$key], $rulePhrase); |
@@ -577,11 +577,11 @@ discard block |
||
| 577 | 577 | * |
| 578 | 578 | * @param type $inputArg |
| 579 | 579 | * @param string $callbackFunc |
| 580 | - * |
|
| 580 | + * |
|
| 581 | 581 | * @return mixed |
| 582 | 582 | */ |
| 583 | 583 | protected function _runCallback($inputArg, $callbackFunc) { |
| 584 | - return eval('return ' . $callbackFunc . '("' . $inputArg . '");'); |
|
| 584 | + return eval('return ' . $callbackFunc . '("' . $inputArg . '");'); |
|
| 585 | 585 | } |
| 586 | 586 | |
| 587 | 587 | /** |
@@ -590,7 +590,7 @@ discard block |
||
| 590 | 590 | * arguments. |
| 591 | 591 | * |
| 592 | 592 | * @param string $callbackFunc |
| 593 | - * |
|
| 593 | + * |
|
| 594 | 594 | * @return mixed |
| 595 | 595 | */ |
| 596 | 596 | protected function _runEmptyCallback($callbackFunc) { |
@@ -601,7 +601,7 @@ discard block |
||
| 601 | 601 | * Gets a specific label of a specific field input name. |
| 602 | 602 | * |
| 603 | 603 | * @param string $inputName |
| 604 | - * |
|
| 604 | + * |
|
| 605 | 605 | * @return string |
| 606 | 606 | */ |
| 607 | 607 | protected function _getLabel($inputName) { |
@@ -614,7 +614,7 @@ discard block |
||
| 614 | 614 | * @param string $ruleName the rule name for this validation ("required") |
| 615 | 615 | * @param array $ruleArgs the rules argument |
| 616 | 616 | */ |
| 617 | - protected function _validateRequired($inputName, $ruleName, array $ruleArgs) { |
|
| 617 | + protected function _validateRequired($inputName, $ruleName, array $ruleArgs) { |
|
| 618 | 618 | $inputVal = $this->post($inputName); |
| 619 | 619 | if(array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
| 620 | 620 | $callbackReturn = $this->_runEmptyCallback($ruleArgs[1]); |
@@ -622,8 +622,8 @@ discard block |
||
| 622 | 622 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
| 623 | 623 | } |
| 624 | 624 | } |
| 625 | - else if($inputVal == '') { |
|
| 626 | - $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 625 | + else if($inputVal == '') { |
|
| 626 | + $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 627 | 627 | } |
| 628 | 628 | } |
| 629 | 629 | |
@@ -647,10 +647,10 @@ discard block |
||
| 647 | 647 | */ |
| 648 | 648 | protected function _validateCallback($inputName, $ruleName, array $ruleArgs) { |
| 649 | 649 | if (function_exists($ruleArgs[1]) && !empty($this->data[$inputName])) { |
| 650 | - $result = $this->_runCallback($this->data[$inputName], $ruleArgs[1]); |
|
| 651 | - if(! $result){ |
|
| 652 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 653 | - } |
|
| 650 | + $result = $this->_runCallback($this->data[$inputName], $ruleArgs[1]); |
|
| 651 | + if(! $result){ |
|
| 652 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 653 | + } |
|
| 654 | 654 | } |
| 655 | 655 | } |
| 656 | 656 | |
@@ -682,7 +682,7 @@ discard block |
||
| 682 | 682 | continue; |
| 683 | 683 | } |
| 684 | 684 | } |
| 685 | - else{ |
|
| 685 | + else{ |
|
| 686 | 686 | if ($inputVal == $doNotEqual) { |
| 687 | 687 | $this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
| 688 | 688 | continue; |
@@ -774,7 +774,7 @@ discard block |
||
| 774 | 774 | * @param string $ruleName the rule name for this validation ("less_than") |
| 775 | 775 | * @param array $ruleArgs the rules argument |
| 776 | 776 | */ |
| 777 | - protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
|
| 777 | + protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
|
| 778 | 778 | $inputVal = $this->post($inputName); |
| 779 | 779 | if ($inputVal >= $ruleArgs[1]) { |
| 780 | 780 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
@@ -790,7 +790,7 @@ discard block |
||
| 790 | 790 | * @param string $ruleName the rule name for this validation ("greater_than") |
| 791 | 791 | * @param array $ruleArgs the rules argument |
| 792 | 792 | */ |
| 793 | - protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
|
| 793 | + protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
|
| 794 | 794 | $inputVal = $this->post($inputName); |
| 795 | 795 | if ($inputVal <= $ruleArgs[1]) { |
| 796 | 796 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
@@ -806,7 +806,7 @@ discard block |
||
| 806 | 806 | * @param string $ruleName the rule name for this validation ("numeric") |
| 807 | 807 | * @param array $ruleArgs the rules argument |
| 808 | 808 | */ |
| 809 | - protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
|
| 809 | + protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
|
| 810 | 810 | $inputVal = $this->post($inputName); |
| 811 | 811 | if (! is_numeric($inputVal)) { |
| 812 | 812 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
@@ -822,18 +822,18 @@ discard block |
||
| 822 | 822 | * @param string $ruleName the rule name for this validation ("exists") |
| 823 | 823 | * @param array $ruleArgs the rules argument |
| 824 | 824 | */ |
| 825 | - protected function _validateExists($inputName, $ruleName, array $ruleArgs) { |
|
| 825 | + protected function _validateExists($inputName, $ruleName, array $ruleArgs) { |
|
| 826 | 826 | $inputVal = $this->post($inputName); |
| 827 | - if (! is_object($this->databaseInstance)){ |
|
| 827 | + if (! is_object($this->databaseInstance)){ |
|
| 828 | 828 | $obj = & get_instance(); |
| 829 | 829 | if(isset($obj->database)){ |
| 830 | 830 | $this->databaseInstance = $obj->database; |
| 831 | 831 | } |
| 832 | 832 | } |
| 833 | - list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 834 | - $this->databaseInstance->getQueryBuilder()->from($table) |
|
| 835 | - ->where($column, $inputVal); |
|
| 836 | - $this->databaseInstance->get(); |
|
| 833 | + list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 834 | + $this->databaseInstance->getQueryBuilder()->from($table) |
|
| 835 | + ->where($column, $inputVal); |
|
| 836 | + $this->databaseInstance->get(); |
|
| 837 | 837 | if ($this->databaseInstance->numRows() <= 0) { |
| 838 | 838 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
| 839 | 839 | return; |
@@ -848,7 +848,7 @@ discard block |
||
| 848 | 848 | * @param string $ruleName the rule name for this validation ("is_unique") |
| 849 | 849 | * @param array $ruleArgs the rules argument |
| 850 | 850 | */ |
| 851 | - protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
|
| 851 | + protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
|
| 852 | 852 | $inputVal = $this->post($inputName); |
| 853 | 853 | if (! is_object($this->databaseInstance)){ |
| 854 | 854 | $obj = & get_instance(); |
@@ -856,11 +856,11 @@ discard block |
||
| 856 | 856 | $this->databaseInstance = $obj->database; |
| 857 | 857 | } |
| 858 | 858 | } |
| 859 | - list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 860 | - $this->databaseInstance->getQueryBuilder()->from($table) |
|
| 861 | - ->where($column, $inputVal); |
|
| 862 | - $this->databaseInstance->get(); |
|
| 863 | - if ($this->databaseInstance->numRows() > 0) { |
|
| 859 | + list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 860 | + $this->databaseInstance->getQueryBuilder()->from($table) |
|
| 861 | + ->where($column, $inputVal); |
|
| 862 | + $this->databaseInstance->get(); |
|
| 863 | + if ($this->databaseInstance->numRows() > 0) { |
|
| 864 | 864 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
| 865 | 865 | return; |
| 866 | 866 | } |
@@ -874,25 +874,25 @@ discard block |
||
| 874 | 874 | * @param string $ruleName the rule name for this validation ("is_unique_update") |
| 875 | 875 | * @param array $ruleArgs the rules argument |
| 876 | 876 | */ |
| 877 | - protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
|
| 877 | + protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
|
| 878 | 878 | $inputVal = $this->post($inputName); |
| 879 | - if (! is_object($this->databaseInstance)){ |
|
| 879 | + if (! is_object($this->databaseInstance)){ |
|
| 880 | 880 | $obj = & get_instance(); |
| 881 | 881 | if(isset($obj->database)){ |
| 882 | 882 | $this->databaseInstance = $obj->database; |
| 883 | 883 | } |
| 884 | 884 | } |
| 885 | - $data = explode(',', $ruleArgs[1]); |
|
| 886 | - if(count($data) < 2){ |
|
| 887 | - return; |
|
| 888 | - } |
|
| 889 | - list($table, $column) = explode('.', $data[0]); |
|
| 890 | - list($field, $val) = explode('=', $data[1]); |
|
| 891 | - $this->databaseInstance->getQueryBuilder()->from($table) |
|
| 892 | - ->where($column, $inputVal) |
|
| 893 | - ->where($field, '!=', trim($val)); |
|
| 885 | + $data = explode(',', $ruleArgs[1]); |
|
| 886 | + if(count($data) < 2){ |
|
| 887 | + return; |
|
| 888 | + } |
|
| 889 | + list($table, $column) = explode('.', $data[0]); |
|
| 890 | + list($field, $val) = explode('=', $data[1]); |
|
| 891 | + $this->databaseInstance->getQueryBuilder()->from($table) |
|
| 892 | + ->where($column, $inputVal) |
|
| 893 | + ->where($field, '!=', trim($val)); |
|
| 894 | 894 | $this->databaseInstance->get(); |
| 895 | - if ($this->databaseInstance->numRows() > 0) { |
|
| 895 | + if ($this->databaseInstance->numRows() > 0) { |
|
| 896 | 896 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
| 897 | 897 | return; |
| 898 | 898 | } |
@@ -908,7 +908,7 @@ discard block |
||
| 908 | 908 | */ |
| 909 | 909 | protected function _validateInList($inputName, $ruleName, array $ruleArgs) { |
| 910 | 910 | $inputVal = $this->post($inputName); |
| 911 | - $list = explode(',', $ruleArgs[1]); |
|
| 911 | + $list = explode(',', $ruleArgs[1]); |
|
| 912 | 912 | $list = array_map('trim', $list); |
| 913 | 913 | if (! in_array($inputVal, $list)) { |
| 914 | 914 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
@@ -926,7 +926,7 @@ discard block |
||
| 926 | 926 | */ |
| 927 | 927 | protected function _validateRegex($inputName, $ruleName, array $ruleArgs) { |
| 928 | 928 | $inputVal = $this->post($inputName); |
| 929 | - $regex = $ruleArgs[1]; |
|
| 929 | + $regex = $ruleArgs[1]; |
|
| 930 | 930 | if (! preg_match($regex, $inputVal)) { |
| 931 | 931 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
| 932 | 932 | return; |
@@ -1669,34 +1669,34 @@ |
||
| 1669 | 1669 | protected function checkPlatform() |
| 1670 | 1670 | { |
| 1671 | 1671 | $platformMaps = array( |
| 1672 | - 'windows' => self::PLATFORM_WINDOWS, |
|
| 1673 | - 'iPad' => self::PLATFORM_IPAD, |
|
| 1674 | - 'iPod' => self::PLATFORM_IPOD, |
|
| 1675 | - 'iPhone' => self::PLATFORM_IPHONE, |
|
| 1676 | - 'mac' => self::PLATFORM_APPLE, |
|
| 1677 | - 'android' => self::PLATFORM_ANDROID, |
|
| 1678 | - 'Silk' => self::PLATFORM_FIRE_OS, |
|
| 1679 | - 'linux_smarttv' => self::PLATFORM_LINUX .'/'.self::PLATFORM_SMART_TV, |
|
| 1680 | - 'linux' => self::PLATFORM_LINUX, |
|
| 1681 | - 'Nokia' => self::PLATFORM_NOKIA, |
|
| 1682 | - 'BlackBerry' => self::PLATFORM_BLACKBERRY, |
|
| 1683 | - 'FreeBSD' => self::PLATFORM_FREEBSD, |
|
| 1684 | - 'OpenBSD' => self::PLATFORM_OPENBSD, |
|
| 1685 | - 'NetBSD' => self::PLATFORM_NETBSD, |
|
| 1686 | - 'OpenSolaris' => self::PLATFORM_OPENSOLARIS, |
|
| 1687 | - 'SunOS' => self::PLATFORM_SUNOS, |
|
| 1688 | - 'OS\/2' => self::PLATFORM_OS2, |
|
| 1689 | - 'BeOS' => self::PLATFORM_BEOS, |
|
| 1690 | - 'win' => self::PLATFORM_WINDOWS, |
|
| 1691 | - 'Playstation' => self::PLATFORM_PLAYSTATION, |
|
| 1692 | - 'Roku' => self::PLATFORM_ROKU, |
|
| 1693 | - 'iOS' => self::PLATFORM_IPHONE . '/' . self::PLATFORM_IPAD, |
|
| 1694 | - 'tvOS' => self::PLATFORM_APPLE_TV, |
|
| 1695 | - 'curl' => self::PLATFORM_TERMINAL, |
|
| 1696 | - 'CrOS' => self::PLATFORM_CHROME_OS, |
|
| 1697 | - 'okhttp' => self::PLATFORM_JAVA_ANDROID, |
|
| 1698 | - 'PostmanRuntime' => self::PLATFORM_POSTMAN, |
|
| 1699 | - 'Iframely' => self::PLATFORM_I_FRAME |
|
| 1672 | + 'windows' => self::PLATFORM_WINDOWS, |
|
| 1673 | + 'iPad' => self::PLATFORM_IPAD, |
|
| 1674 | + 'iPod' => self::PLATFORM_IPOD, |
|
| 1675 | + 'iPhone' => self::PLATFORM_IPHONE, |
|
| 1676 | + 'mac' => self::PLATFORM_APPLE, |
|
| 1677 | + 'android' => self::PLATFORM_ANDROID, |
|
| 1678 | + 'Silk' => self::PLATFORM_FIRE_OS, |
|
| 1679 | + 'linux_smarttv' => self::PLATFORM_LINUX .'/'.self::PLATFORM_SMART_TV, |
|
| 1680 | + 'linux' => self::PLATFORM_LINUX, |
|
| 1681 | + 'Nokia' => self::PLATFORM_NOKIA, |
|
| 1682 | + 'BlackBerry' => self::PLATFORM_BLACKBERRY, |
|
| 1683 | + 'FreeBSD' => self::PLATFORM_FREEBSD, |
|
| 1684 | + 'OpenBSD' => self::PLATFORM_OPENBSD, |
|
| 1685 | + 'NetBSD' => self::PLATFORM_NETBSD, |
|
| 1686 | + 'OpenSolaris' => self::PLATFORM_OPENSOLARIS, |
|
| 1687 | + 'SunOS' => self::PLATFORM_SUNOS, |
|
| 1688 | + 'OS\/2' => self::PLATFORM_OS2, |
|
| 1689 | + 'BeOS' => self::PLATFORM_BEOS, |
|
| 1690 | + 'win' => self::PLATFORM_WINDOWS, |
|
| 1691 | + 'Playstation' => self::PLATFORM_PLAYSTATION, |
|
| 1692 | + 'Roku' => self::PLATFORM_ROKU, |
|
| 1693 | + 'iOS' => self::PLATFORM_IPHONE . '/' . self::PLATFORM_IPAD, |
|
| 1694 | + 'tvOS' => self::PLATFORM_APPLE_TV, |
|
| 1695 | + 'curl' => self::PLATFORM_TERMINAL, |
|
| 1696 | + 'CrOS' => self::PLATFORM_CHROME_OS, |
|
| 1697 | + 'okhttp' => self::PLATFORM_JAVA_ANDROID, |
|
| 1698 | + 'PostmanRuntime' => self::PLATFORM_POSTMAN, |
|
| 1699 | + 'Iframely' => self::PLATFORM_I_FRAME |
|
| 1700 | 1700 | ); |
| 1701 | 1701 | |
| 1702 | 1702 | foreach ($platformMaps as $name => $value) { |
@@ -1,71 +1,71 @@ |
||
| 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 | - * @file function_string.php |
|
| 29 | - * |
|
| 30 | - * This file contains the definition of the functions relating to the processing of strings characters. |
|
| 31 | - * |
|
| 32 | - * @package core |
|
| 33 | - * @author Tony NGUEREZA |
|
| 34 | - * @copyright Copyright (c) 2017 |
|
| 35 | - * @license https://opensource.org/licenses/gpl-3.0.html GNU GPL License (GPL) |
|
| 36 | - * @link http://www.iacademy.cf |
|
| 37 | - * @version 1.0.0 |
|
| 38 | - * @since 1.0.0 |
|
| 39 | - * @filesource |
|
| 40 | - */ |
|
| 27 | + /** |
|
| 28 | + * @file function_string.php |
|
| 29 | + * |
|
| 30 | + * This file contains the definition of the functions relating to the processing of strings characters. |
|
| 31 | + * |
|
| 32 | + * @package core |
|
| 33 | + * @author Tony NGUEREZA |
|
| 34 | + * @copyright Copyright (c) 2017 |
|
| 35 | + * @license https://opensource.org/licenses/gpl-3.0.html GNU GPL License (GPL) |
|
| 36 | + * @link http://www.iacademy.cf |
|
| 37 | + * @version 1.0.0 |
|
| 38 | + * @since 1.0.0 |
|
| 39 | + * @filesource |
|
| 40 | + */ |
|
| 41 | 41 | |
| 42 | - if(! function_exists('get_random_string')){ |
|
| 43 | - /** |
|
| 44 | - * Generate a random string |
|
| 45 | - * @param string $type the type of generation. It can take the values: "alpha" for alphabetic characters, |
|
| 46 | - * "alnum" for alpha-numeric characters and "num" for numbers. |
|
| 47 | - * By default it is "alnum". |
|
| 48 | - * @param integer $length the length of the string to generate. By default it is 10. |
|
| 49 | - * @param boolean $lower if we return the generated string in lowercase (true). By default it's false. |
|
| 50 | - * @return string the generated string. |
|
| 51 | - */ |
|
| 52 | - function get_random_string($type = 'alnum', $length = 10, $lower = false){ |
|
| 53 | - $chars = array( |
|
| 54 | - 'alpha' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
|
| 55 | - 'alnum' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', |
|
| 56 | - 'num' => '1234567890' |
|
| 57 | - ); |
|
| 58 | - $str = null; |
|
| 59 | - if(isset($chars[$type])){ |
|
| 60 | - $str = $chars[$type]; |
|
| 61 | - } |
|
| 62 | - $random = null; |
|
| 63 | - for($i = 0; $i < $length; $i++){ |
|
| 64 | - $random .= $str[mt_rand() % strlen($str)]; |
|
| 65 | - } |
|
| 66 | - if($lower){ |
|
| 67 | - $random = strtolower($random); |
|
| 68 | - } |
|
| 69 | - return $random; |
|
| 70 | - } |
|
| 71 | - } |
|
| 42 | + if(! function_exists('get_random_string')){ |
|
| 43 | + /** |
|
| 44 | + * Generate a random string |
|
| 45 | + * @param string $type the type of generation. It can take the values: "alpha" for alphabetic characters, |
|
| 46 | + * "alnum" for alpha-numeric characters and "num" for numbers. |
|
| 47 | + * By default it is "alnum". |
|
| 48 | + * @param integer $length the length of the string to generate. By default it is 10. |
|
| 49 | + * @param boolean $lower if we return the generated string in lowercase (true). By default it's false. |
|
| 50 | + * @return string the generated string. |
|
| 51 | + */ |
|
| 52 | + function get_random_string($type = 'alnum', $length = 10, $lower = false){ |
|
| 53 | + $chars = array( |
|
| 54 | + 'alpha' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
|
| 55 | + 'alnum' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', |
|
| 56 | + 'num' => '1234567890' |
|
| 57 | + ); |
|
| 58 | + $str = null; |
|
| 59 | + if(isset($chars[$type])){ |
|
| 60 | + $str = $chars[$type]; |
|
| 61 | + } |
|
| 62 | + $random = null; |
|
| 63 | + for($i = 0; $i < $length; $i++){ |
|
| 64 | + $random .= $str[mt_rand() % strlen($str)]; |
|
| 65 | + } |
|
| 66 | + if($lower){ |
|
| 67 | + $random = strtolower($random); |
|
| 68 | + } |
|
| 69 | + return $random; |
|
| 70 | + } |
|
| 71 | + } |
|
@@ -1,108 +1,108 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | defined('ROOT_PATH') || exit('Access denied'); |
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 26 | - class DatabaseQueryBuilder{ |
|
| 27 | - /** |
|
| 28 | - * The SQL SELECT statment |
|
| 29 | - * @var string |
|
| 30 | - */ |
|
| 31 | - private $select = '*'; |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | + class DatabaseQueryBuilder{ |
|
| 27 | + /** |
|
| 28 | + * The SQL SELECT statment |
|
| 29 | + * @var string |
|
| 30 | + */ |
|
| 31 | + private $select = '*'; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * The SQL FROM statment |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - private $from = null; |
|
| 33 | + /** |
|
| 34 | + * The SQL FROM statment |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + private $from = null; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * The SQL WHERE statment |
|
| 41 | - * @var string |
|
| 42 | - */ |
|
| 43 | - private $where = null; |
|
| 39 | + /** |
|
| 40 | + * The SQL WHERE statment |
|
| 41 | + * @var string |
|
| 42 | + */ |
|
| 43 | + private $where = null; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * The SQL LIMIT statment |
|
| 47 | - * @var string |
|
| 48 | - */ |
|
| 49 | - private $limit = null; |
|
| 45 | + /** |
|
| 46 | + * The SQL LIMIT statment |
|
| 47 | + * @var string |
|
| 48 | + */ |
|
| 49 | + private $limit = null; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * The SQL JOIN statment |
|
| 53 | - * @var string |
|
| 54 | - */ |
|
| 55 | - private $join = null; |
|
| 51 | + /** |
|
| 52 | + * The SQL JOIN statment |
|
| 53 | + * @var string |
|
| 54 | + */ |
|
| 55 | + private $join = null; |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * The SQL ORDER BY statment |
|
| 59 | - * @var string |
|
| 60 | - */ |
|
| 61 | - private $orderBy = null; |
|
| 57 | + /** |
|
| 58 | + * The SQL ORDER BY statment |
|
| 59 | + * @var string |
|
| 60 | + */ |
|
| 61 | + private $orderBy = null; |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * The SQL GROUP BY statment |
|
| 65 | - * @var string |
|
| 66 | - */ |
|
| 67 | - private $groupBy = null; |
|
| 63 | + /** |
|
| 64 | + * The SQL GROUP BY statment |
|
| 65 | + * @var string |
|
| 66 | + */ |
|
| 67 | + private $groupBy = null; |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * The SQL HAVING statment |
|
| 71 | - * @var string |
|
| 72 | - */ |
|
| 73 | - private $having = null; |
|
| 69 | + /** |
|
| 70 | + * The SQL HAVING statment |
|
| 71 | + * @var string |
|
| 72 | + */ |
|
| 73 | + private $having = null; |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * The full SQL query statment after build for each command |
|
| 77 | - * @var string |
|
| 78 | - */ |
|
| 79 | - private $query = null; |
|
| 75 | + /** |
|
| 76 | + * The full SQL query statment after build for each command |
|
| 77 | + * @var string |
|
| 78 | + */ |
|
| 79 | + private $query = null; |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * The list of SQL valid operators |
|
| 83 | - * @var array |
|
| 84 | - */ |
|
| 81 | + /** |
|
| 82 | + * The list of SQL valid operators |
|
| 83 | + * @var array |
|
| 84 | + */ |
|
| 85 | 85 | private $operatorList = array('=','!=','<','>','<=','>=','<>'); |
| 86 | 86 | |
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * The prefix used in each database table |
|
| 90 | - * @var string |
|
| 91 | - */ |
|
| 88 | + /** |
|
| 89 | + * The prefix used in each database table |
|
| 90 | + * @var string |
|
| 91 | + */ |
|
| 92 | 92 | private $prefix = null; |
| 93 | 93 | |
| 94 | 94 | |
| 95 | 95 | /** |
| 96 | - * The PDO instance |
|
| 97 | - * @var object |
|
| 98 | - */ |
|
| 96 | + * The PDO instance |
|
| 97 | + * @var object |
|
| 98 | + */ |
|
| 99 | 99 | private $pdo = null; |
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * The database driver name used |
|
| 103 | - * @var string |
|
| 104 | - */ |
|
| 105 | - private $driver = null; |
|
| 101 | + /** |
|
| 102 | + * The database driver name used |
|
| 103 | + * @var string |
|
| 104 | + */ |
|
| 105 | + private $driver = null; |
|
| 106 | 106 | |
| 107 | 107 | |
| 108 | 108 | /** |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | */ |
| 112 | 112 | public function __construct(PDO $pdo = null){ |
| 113 | 113 | if (is_object($pdo)){ |
| 114 | - $this->setPdo($pdo); |
|
| 114 | + $this->setPdo($pdo); |
|
| 115 | 115 | } |
| 116 | 116 | } |
| 117 | 117 | |
@@ -121,16 +121,16 @@ discard block |
||
| 121 | 121 | * @return object the current DatabaseQueryBuilder instance |
| 122 | 122 | */ |
| 123 | 123 | public function from($table){ |
| 124 | - if (is_array($table)){ |
|
| 124 | + if (is_array($table)){ |
|
| 125 | 125 | $froms = ''; |
| 126 | 126 | foreach($table as $key){ |
| 127 | - $froms .= $this->getPrefix() . $key . ', '; |
|
| 127 | + $froms .= $this->getPrefix() . $key . ', '; |
|
| 128 | 128 | } |
| 129 | 129 | $this->from = rtrim($froms, ', '); |
| 130 | - } else { |
|
| 130 | + } else { |
|
| 131 | 131 | $this->from = $this->getPrefix() . $table; |
| 132 | - } |
|
| 133 | - return $this; |
|
| 132 | + } |
|
| 133 | + return $this; |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | /** |
@@ -139,9 +139,9 @@ discard block |
||
| 139 | 139 | * @return object the current DatabaseQueryBuilder instance |
| 140 | 140 | */ |
| 141 | 141 | public function select($fields){ |
| 142 | - $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
|
| 143 | - $this->select = (($this->select == '*' || empty($this->select)) ? $select : $this->select . ', ' . $select); |
|
| 144 | - return $this; |
|
| 142 | + $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
|
| 143 | + $this->select = (($this->select == '*' || empty($this->select)) ? $select : $this->select . ', ' . $select); |
|
| 144 | + return $this; |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | /** |
@@ -150,19 +150,19 @@ discard block |
||
| 150 | 150 | * @return object the current DatabaseQueryBuilder instance |
| 151 | 151 | */ |
| 152 | 152 | public function distinct($field){ |
| 153 | - $distinct = ' DISTINCT ' . $field; |
|
| 154 | - $this->select = (($this->select == '*' || empty($this->select)) ? $distinct : $this->select . ', ' . $distinct); |
|
| 155 | - return $this; |
|
| 153 | + $distinct = ' DISTINCT ' . $field; |
|
| 154 | + $this->select = (($this->select == '*' || empty($this->select)) ? $distinct : $this->select . ', ' . $distinct); |
|
| 155 | + return $this; |
|
| 156 | 156 | } |
| 157 | 157 | |
| 158 | - /** |
|
| 159 | - * Set the SQL function COUNT in SELECT statment |
|
| 160 | - * @param string $field the field name |
|
| 161 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 162 | - * @return object the current DatabaseQueryBuilder instance |
|
| 163 | - */ |
|
| 158 | + /** |
|
| 159 | + * Set the SQL function COUNT in SELECT statment |
|
| 160 | + * @param string $field the field name |
|
| 161 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 162 | + * @return object the current DatabaseQueryBuilder instance |
|
| 163 | + */ |
|
| 164 | 164 | public function count($field = '*', $name = null){ |
| 165 | - return $this->select_min_max_sum_count_avg('COUNT', $field, $name); |
|
| 165 | + return $this->select_min_max_sum_count_avg('COUNT', $field, $name); |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | /** |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | * @return object the current DatabaseQueryBuilder instance |
| 173 | 173 | */ |
| 174 | 174 | public function min($field, $name = null){ |
| 175 | - return $this->select_min_max_sum_count_avg('MIN', $field, $name); |
|
| 175 | + return $this->select_min_max_sum_count_avg('MIN', $field, $name); |
|
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | /** |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | * @return object the current DatabaseQueryBuilder instance |
| 183 | 183 | */ |
| 184 | 184 | public function max($field, $name = null){ |
| 185 | - return $this->select_min_max_sum_count_avg('MAX', $field, $name); |
|
| 185 | + return $this->select_min_max_sum_count_avg('MAX', $field, $name); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | /** |
@@ -192,7 +192,7 @@ discard block |
||
| 192 | 192 | * @return object the current DatabaseQueryBuilder instance |
| 193 | 193 | */ |
| 194 | 194 | public function sum($field, $name = null){ |
| 195 | - return $this->select_min_max_sum_count_avg('SUM', $field, $name); |
|
| 195 | + return $this->select_min_max_sum_count_avg('SUM', $field, $name); |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | /** |
@@ -202,7 +202,7 @@ discard block |
||
| 202 | 202 | * @return object the current DatabaseQueryBuilder instance |
| 203 | 203 | */ |
| 204 | 204 | public function avg($field, $name = null){ |
| 205 | - return $this->select_min_max_sum_count_avg('AVG', $field, $name); |
|
| 205 | + return $this->select_min_max_sum_count_avg('AVG', $field, $name); |
|
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | |
@@ -216,20 +216,20 @@ discard block |
||
| 216 | 216 | * @return object the current DatabaseQueryBuilder instance |
| 217 | 217 | */ |
| 218 | 218 | public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
| 219 | - $on = $field1; |
|
| 220 | - $table = $this->getPrefix() . $table; |
|
| 221 | - if (! is_null($op)){ |
|
| 219 | + $on = $field1; |
|
| 220 | + $table = $this->getPrefix() . $table; |
|
| 221 | + if (! is_null($op)){ |
|
| 222 | 222 | $on = (! in_array($op, $this->operatorList) |
| 223 | - ? ($this->getPrefix() . $field1 . ' = ' . $this->getPrefix() . $op) |
|
| 224 | - : ($this->getPrefix() . $field1 . ' ' . $op . ' ' . $this->getPrefix() . $field2)); |
|
| 225 | - } |
|
| 226 | - if (empty($this->join)){ |
|
| 223 | + ? ($this->getPrefix() . $field1 . ' = ' . $this->getPrefix() . $op) |
|
| 224 | + : ($this->getPrefix() . $field1 . ' ' . $op . ' ' . $this->getPrefix() . $field2)); |
|
| 225 | + } |
|
| 226 | + if (empty($this->join)){ |
|
| 227 | 227 | $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 228 | - } |
|
| 229 | - else{ |
|
| 228 | + } |
|
| 229 | + else{ |
|
| 230 | 230 | $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 231 | - } |
|
| 232 | - return $this; |
|
| 231 | + } |
|
| 232 | + return $this; |
|
| 233 | 233 | } |
| 234 | 234 | |
| 235 | 235 | /** |
@@ -238,7 +238,7 @@ discard block |
||
| 238 | 238 | * @return object the current DatabaseQueryBuilder instance |
| 239 | 239 | */ |
| 240 | 240 | public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
| 241 | - return $this->join($table, $field1, $op, $field2, 'INNER '); |
|
| 241 | + return $this->join($table, $field1, $op, $field2, 'INNER '); |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | /** |
@@ -247,16 +247,16 @@ discard block |
||
| 247 | 247 | * @return object the current DatabaseQueryBuilder instance |
| 248 | 248 | */ |
| 249 | 249 | public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
| 250 | - return $this->join($table, $field1, $op, $field2, 'LEFT '); |
|
| 251 | - } |
|
| 250 | + return $this->join($table, $field1, $op, $field2, 'LEFT '); |
|
| 251 | + } |
|
| 252 | 252 | |
| 253 | - /** |
|
| 253 | + /** |
|
| 254 | 254 | * Set the SQL RIGHT JOIN statment |
| 255 | 255 | * @see DatabaseQueryBuilder::join() |
| 256 | 256 | * @return object the current DatabaseQueryBuilder instance |
| 257 | 257 | */ |
| 258 | 258 | public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
| 259 | - return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
|
| 259 | + return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
|
| 260 | 260 | } |
| 261 | 261 | |
| 262 | 262 | /** |
@@ -265,7 +265,7 @@ discard block |
||
| 265 | 265 | * @return object the current DatabaseQueryBuilder instance |
| 266 | 266 | */ |
| 267 | 267 | public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
| 268 | - return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
|
| 268 | + return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
|
| 269 | 269 | } |
| 270 | 270 | |
| 271 | 271 | /** |
@@ -274,7 +274,7 @@ discard block |
||
| 274 | 274 | * @return object the current DatabaseQueryBuilder instance |
| 275 | 275 | */ |
| 276 | 276 | public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
| 277 | - return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
|
| 277 | + return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
|
| 278 | 278 | } |
| 279 | 279 | |
| 280 | 280 | /** |
@@ -283,7 +283,7 @@ discard block |
||
| 283 | 283 | * @return object the current DatabaseQueryBuilder instance |
| 284 | 284 | */ |
| 285 | 285 | public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
| 286 | - return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
|
| 286 | + return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
|
| 287 | 287 | } |
| 288 | 288 | |
| 289 | 289 | /** |
@@ -293,14 +293,14 @@ discard block |
||
| 293 | 293 | * @return object the current DatabaseQueryBuilder instance |
| 294 | 294 | */ |
| 295 | 295 | public function whereIsNull($field, $andOr = 'AND'){ |
| 296 | - if (is_array($field)){ |
|
| 296 | + if (is_array($field)){ |
|
| 297 | 297 | foreach($field as $f){ |
| 298 | - $this->whereIsNull($f, $andOr); |
|
| 298 | + $this->whereIsNull($f, $andOr); |
|
| 299 | 299 | } |
| 300 | - } else { |
|
| 301 | - $this->setWhereStr($field.' IS NULL ', $andOr); |
|
| 302 | - } |
|
| 303 | - return $this; |
|
| 300 | + } else { |
|
| 301 | + $this->setWhereStr($field.' IS NULL ', $andOr); |
|
| 302 | + } |
|
| 303 | + return $this; |
|
| 304 | 304 | } |
| 305 | 305 | |
| 306 | 306 | /** |
@@ -310,14 +310,14 @@ discard block |
||
| 310 | 310 | * @return object the current DatabaseQueryBuilder instance |
| 311 | 311 | */ |
| 312 | 312 | public function whereIsNotNull($field, $andOr = 'AND'){ |
| 313 | - if (is_array($field)){ |
|
| 313 | + if (is_array($field)){ |
|
| 314 | 314 | foreach($field as $f){ |
| 315 | - $this->whereIsNotNull($f, $andOr); |
|
| 315 | + $this->whereIsNotNull($f, $andOr); |
|
| 316 | + } |
|
| 317 | + } else { |
|
| 318 | + $this->setWhereStr($field.' IS NOT NULL ', $andOr); |
|
| 316 | 319 | } |
| 317 | - } else { |
|
| 318 | - $this->setWhereStr($field.' IS NOT NULL ', $andOr); |
|
| 319 | - } |
|
| 320 | - return $this; |
|
| 320 | + return $this; |
|
| 321 | 321 | } |
| 322 | 322 | |
| 323 | 323 | /** |
@@ -331,19 +331,19 @@ discard block |
||
| 331 | 331 | * @return object the current DatabaseQueryBuilder instance |
| 332 | 332 | */ |
| 333 | 333 | public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
| 334 | - $whereStr = ''; |
|
| 335 | - if (is_array($where)){ |
|
| 334 | + $whereStr = ''; |
|
| 335 | + if (is_array($where)){ |
|
| 336 | 336 | $whereStr = $this->getWhereStrIfIsArray($where, $type, $andOr, $escape); |
| 337 | - } |
|
| 338 | - else{ |
|
| 337 | + } |
|
| 338 | + else{ |
|
| 339 | 339 | if (is_array($op)){ |
| 340 | - $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
|
| 340 | + $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
|
| 341 | 341 | } else { |
| 342 | - $whereStr = $this->getWhereStrForOperator($where, $op, $val, $type, $escape); |
|
| 342 | + $whereStr = $this->getWhereStrForOperator($where, $op, $val, $type, $escape); |
|
| 343 | + } |
|
| 343 | 344 | } |
| 344 | - } |
|
| 345 | - $this->setWhereStr($whereStr, $andOr); |
|
| 346 | - return $this; |
|
| 345 | + $this->setWhereStr($whereStr, $andOr); |
|
| 346 | + return $this; |
|
| 347 | 347 | } |
| 348 | 348 | |
| 349 | 349 | /** |
@@ -352,7 +352,7 @@ discard block |
||
| 352 | 352 | * @return object the current DatabaseQueryBuilder instance |
| 353 | 353 | */ |
| 354 | 354 | public function orWhere($where, $op = null, $val = null, $escape = true){ |
| 355 | - return $this->where($where, $op, $val, '', 'OR', $escape); |
|
| 355 | + return $this->where($where, $op, $val, '', 'OR', $escape); |
|
| 356 | 356 | } |
| 357 | 357 | |
| 358 | 358 | |
@@ -362,7 +362,7 @@ discard block |
||
| 362 | 362 | * @return object the current DatabaseQueryBuilder instance |
| 363 | 363 | */ |
| 364 | 364 | public function notWhere($where, $op = null, $val = null, $escape = true){ |
| 365 | - return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
| 365 | + return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
| 366 | 366 | } |
| 367 | 367 | |
| 368 | 368 | /** |
@@ -371,7 +371,7 @@ discard block |
||
| 371 | 371 | * @return object the current DatabaseQueryBuilder instance |
| 372 | 372 | */ |
| 373 | 373 | public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
| 374 | - return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
| 374 | + return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
| 375 | 375 | } |
| 376 | 376 | |
| 377 | 377 | /** |
@@ -381,16 +381,16 @@ discard block |
||
| 381 | 381 | * @return object the current DatabaseQueryBuilder instance |
| 382 | 382 | */ |
| 383 | 383 | public function groupStart($type = '', $andOr = ' AND'){ |
| 384 | - if (empty($this->where)){ |
|
| 384 | + if (empty($this->where)){ |
|
| 385 | 385 | $this->where = $type . ' ('; |
| 386 | - } else { |
|
| 387 | - if (substr(trim($this->where), -1) == '('){ |
|
| 386 | + } else { |
|
| 387 | + if (substr(trim($this->where), -1) == '('){ |
|
| 388 | 388 | $this->where .= $type . ' ('; |
| 389 | - } else { |
|
| 390 | - $this->where .= $andOr . ' ' . $type . ' ('; |
|
| 391 | - } |
|
| 392 | - } |
|
| 393 | - return $this; |
|
| 389 | + } else { |
|
| 390 | + $this->where .= $andOr . ' ' . $type . ' ('; |
|
| 391 | + } |
|
| 392 | + } |
|
| 393 | + return $this; |
|
| 394 | 394 | } |
| 395 | 395 | |
| 396 | 396 | /** |
@@ -399,7 +399,7 @@ discard block |
||
| 399 | 399 | * @return object the current DatabaseQueryBuilder instance |
| 400 | 400 | */ |
| 401 | 401 | public function notGroupStart(){ |
| 402 | - return $this->groupStart('NOT'); |
|
| 402 | + return $this->groupStart('NOT'); |
|
| 403 | 403 | } |
| 404 | 404 | |
| 405 | 405 | /** |
@@ -408,16 +408,16 @@ discard block |
||
| 408 | 408 | * @return object the current DatabaseQueryBuilder instance |
| 409 | 409 | */ |
| 410 | 410 | public function orGroupStart(){ |
| 411 | - return $this->groupStart('', ' OR'); |
|
| 411 | + return $this->groupStart('', ' OR'); |
|
| 412 | 412 | } |
| 413 | 413 | |
| 414 | - /** |
|
| 415 | - * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
| 416 | - * @see DatabaseQueryBuilder::groupStart() |
|
| 417 | - * @return object the current DatabaseQueryBuilder instance |
|
| 418 | - */ |
|
| 414 | + /** |
|
| 415 | + * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
| 416 | + * @see DatabaseQueryBuilder::groupStart() |
|
| 417 | + * @return object the current DatabaseQueryBuilder instance |
|
| 418 | + */ |
|
| 419 | 419 | public function orNotGroupStart(){ |
| 420 | - return $this->groupStart('NOT', ' OR'); |
|
| 420 | + return $this->groupStart('NOT', ' OR'); |
|
| 421 | 421 | } |
| 422 | 422 | |
| 423 | 423 | /** |
@@ -425,8 +425,8 @@ discard block |
||
| 425 | 425 | * @return object the current DatabaseQueryBuilder instance |
| 426 | 426 | */ |
| 427 | 427 | public function groupEnd(){ |
| 428 | - $this->where .= ')'; |
|
| 429 | - return $this; |
|
| 428 | + $this->where .= ')'; |
|
| 429 | + return $this; |
|
| 430 | 430 | } |
| 431 | 431 | |
| 432 | 432 | /** |
@@ -439,17 +439,17 @@ discard block |
||
| 439 | 439 | * @return object the current DatabaseQueryBuilder instance |
| 440 | 440 | */ |
| 441 | 441 | public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
| 442 | - $_keys = array(); |
|
| 443 | - foreach ($keys as $k => $v){ |
|
| 442 | + $_keys = array(); |
|
| 443 | + foreach ($keys as $k => $v){ |
|
| 444 | 444 | if (is_null($v)){ |
| 445 | - $v = ''; |
|
| 445 | + $v = ''; |
|
| 446 | 446 | } |
| 447 | 447 | $_keys[] = (is_numeric($v) ? $v : $this->escape($v, $escape)); |
| 448 | - } |
|
| 449 | - $keys = implode(', ', $_keys); |
|
| 450 | - $whereStr = $field . ' ' . $type . ' IN (' . $keys . ')'; |
|
| 451 | - $this->setWhereStr($whereStr, $andOr); |
|
| 452 | - return $this; |
|
| 448 | + } |
|
| 449 | + $keys = implode(', ', $_keys); |
|
| 450 | + $whereStr = $field . ' ' . $type . ' IN (' . $keys . ')'; |
|
| 451 | + $this->setWhereStr($whereStr, $andOr); |
|
| 452 | + return $this; |
|
| 453 | 453 | } |
| 454 | 454 | |
| 455 | 455 | /** |
@@ -458,7 +458,7 @@ discard block |
||
| 458 | 458 | * @return object the current DatabaseQueryBuilder instance |
| 459 | 459 | */ |
| 460 | 460 | public function notIn($field, array $keys, $escape = true){ |
| 461 | - return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
| 461 | + return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
| 462 | 462 | } |
| 463 | 463 | |
| 464 | 464 | /** |
@@ -467,7 +467,7 @@ discard block |
||
| 467 | 467 | * @return object the current DatabaseQueryBuilder instance |
| 468 | 468 | */ |
| 469 | 469 | public function orIn($field, array $keys, $escape = true){ |
| 470 | - return $this->in($field, $keys, '', 'OR', $escape); |
|
| 470 | + return $this->in($field, $keys, '', 'OR', $escape); |
|
| 471 | 471 | } |
| 472 | 472 | |
| 473 | 473 | /** |
@@ -476,7 +476,7 @@ discard block |
||
| 476 | 476 | * @return object the current DatabaseQueryBuilder instance |
| 477 | 477 | */ |
| 478 | 478 | public function orNotIn($field, array $keys, $escape = true){ |
| 479 | - return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
| 479 | + return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
| 480 | 480 | } |
| 481 | 481 | |
| 482 | 482 | /** |
@@ -490,15 +490,15 @@ discard block |
||
| 490 | 490 | * @return object the current DatabaseQueryBuilder instance |
| 491 | 491 | */ |
| 492 | 492 | public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
| 493 | - if (is_null($value1)){ |
|
| 493 | + if (is_null($value1)){ |
|
| 494 | 494 | $value1 = ''; |
| 495 | - } |
|
| 496 | - if (is_null($value2)){ |
|
| 495 | + } |
|
| 496 | + if (is_null($value2)){ |
|
| 497 | 497 | $value2 = ''; |
| 498 | - } |
|
| 499 | - $whereStr = $field . ' ' . $type . ' BETWEEN ' . $this->escape($value1, $escape) . ' AND ' . $this->escape($value2, $escape); |
|
| 500 | - $this->setWhereStr($whereStr, $andOr); |
|
| 501 | - return $this; |
|
| 498 | + } |
|
| 499 | + $whereStr = $field . ' ' . $type . ' BETWEEN ' . $this->escape($value1, $escape) . ' AND ' . $this->escape($value2, $escape); |
|
| 500 | + $this->setWhereStr($whereStr, $andOr); |
|
| 501 | + return $this; |
|
| 502 | 502 | } |
| 503 | 503 | |
| 504 | 504 | /** |
@@ -507,7 +507,7 @@ discard block |
||
| 507 | 507 | * @return object the current DatabaseQueryBuilder instance |
| 508 | 508 | */ |
| 509 | 509 | public function notBetween($field, $value1, $value2, $escape = true){ |
| 510 | - return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
| 510 | + return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
| 511 | 511 | } |
| 512 | 512 | |
| 513 | 513 | /** |
@@ -516,7 +516,7 @@ discard block |
||
| 516 | 516 | * @return object the current DatabaseQueryBuilder instance |
| 517 | 517 | */ |
| 518 | 518 | public function orBetween($field, $value1, $value2, $escape = true){ |
| 519 | - return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
| 519 | + return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
| 520 | 520 | } |
| 521 | 521 | |
| 522 | 522 | /** |
@@ -525,7 +525,7 @@ discard block |
||
| 525 | 525 | * @return object the current DatabaseQueryBuilder instance |
| 526 | 526 | */ |
| 527 | 527 | public function orNotBetween($field, $value1, $value2, $escape = true){ |
| 528 | - return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
| 528 | + return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
| 529 | 529 | } |
| 530 | 530 | |
| 531 | 531 | /** |
@@ -538,11 +538,11 @@ discard block |
||
| 538 | 538 | * @return object the current DatabaseQueryBuilder instance |
| 539 | 539 | */ |
| 540 | 540 | public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
| 541 | - if (empty($data)){ |
|
| 541 | + if (empty($data)){ |
|
| 542 | 542 | $data = ''; |
| 543 | - } |
|
| 544 | - $this->setWhereStr($field . ' ' . $type . ' LIKE ' . ($this->escape($data, $escape)), $andOr); |
|
| 545 | - return $this; |
|
| 543 | + } |
|
| 544 | + $this->setWhereStr($field . ' ' . $type . ' LIKE ' . ($this->escape($data, $escape)), $andOr); |
|
| 545 | + return $this; |
|
| 546 | 546 | } |
| 547 | 547 | |
| 548 | 548 | /** |
@@ -551,7 +551,7 @@ discard block |
||
| 551 | 551 | * @return object the current DatabaseQueryBuilder instance |
| 552 | 552 | */ |
| 553 | 553 | public function orLike($field, $data, $escape = true){ |
| 554 | - return $this->like($field, $data, '', 'OR', $escape); |
|
| 554 | + return $this->like($field, $data, '', 'OR', $escape); |
|
| 555 | 555 | } |
| 556 | 556 | |
| 557 | 557 | /** |
@@ -560,7 +560,7 @@ discard block |
||
| 560 | 560 | * @return object the current DatabaseQueryBuilder instance |
| 561 | 561 | */ |
| 562 | 562 | public function notLike($field, $data, $escape = true){ |
| 563 | - return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
| 563 | + return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
| 564 | 564 | } |
| 565 | 565 | |
| 566 | 566 | /** |
@@ -569,7 +569,7 @@ discard block |
||
| 569 | 569 | * @return object the current DatabaseQueryBuilder instance |
| 570 | 570 | */ |
| 571 | 571 | public function orNotLike($field, $data, $escape = true){ |
| 572 | - return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
| 572 | + return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
| 573 | 573 | } |
| 574 | 574 | |
| 575 | 575 | /** |
@@ -580,16 +580,16 @@ discard block |
||
| 580 | 580 | * @return object the current DatabaseQueryBuilder instance |
| 581 | 581 | */ |
| 582 | 582 | public function limit($limit, $limitEnd = null){ |
| 583 | - if (empty($limit)){ |
|
| 583 | + if (empty($limit)){ |
|
| 584 | 584 | $limit = 0; |
| 585 | - } |
|
| 586 | - if (! is_null($limitEnd)){ |
|
| 585 | + } |
|
| 586 | + if (! is_null($limitEnd)){ |
|
| 587 | 587 | $this->limit = $limit . ', ' . $limitEnd; |
| 588 | - } |
|
| 589 | - else{ |
|
| 588 | + } |
|
| 589 | + else{ |
|
| 590 | 590 | $this->limit = $limit; |
| 591 | - } |
|
| 592 | - return $this; |
|
| 591 | + } |
|
| 592 | + return $this; |
|
| 593 | 593 | } |
| 594 | 594 | |
| 595 | 595 | /** |
@@ -599,15 +599,15 @@ discard block |
||
| 599 | 599 | * @return object the current DatabaseQueryBuilder instance |
| 600 | 600 | */ |
| 601 | 601 | public function orderBy($orderBy, $orderDir = ' ASC'){ |
| 602 | - if (stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 602 | + if (stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 603 | 603 | $this->orderBy = empty($this->orderBy) ? $orderBy : $this->orderBy . ', ' . $orderBy; |
| 604 | - } |
|
| 605 | - else{ |
|
| 604 | + } |
|
| 605 | + else{ |
|
| 606 | 606 | $this->orderBy = empty($this->orderBy) |
| 607 | - ? ($orderBy . ' ' . strtoupper($orderDir)) |
|
| 608 | - : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
| 609 | - } |
|
| 610 | - return $this; |
|
| 607 | + ? ($orderBy . ' ' . strtoupper($orderDir)) |
|
| 608 | + : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
| 609 | + } |
|
| 610 | + return $this; |
|
| 611 | 611 | } |
| 612 | 612 | |
| 613 | 613 | /** |
@@ -616,13 +616,13 @@ discard block |
||
| 616 | 616 | * @return object the current DatabaseQueryBuilder instance |
| 617 | 617 | */ |
| 618 | 618 | public function groupBy($field){ |
| 619 | - if (is_array($field)){ |
|
| 619 | + if (is_array($field)){ |
|
| 620 | 620 | $this->groupBy = implode(', ', $field); |
| 621 | - } |
|
| 622 | - else{ |
|
| 621 | + } |
|
| 622 | + else{ |
|
| 623 | 623 | $this->groupBy = $field; |
| 624 | - } |
|
| 625 | - return $this; |
|
| 624 | + } |
|
| 625 | + return $this; |
|
| 626 | 626 | } |
| 627 | 627 | |
| 628 | 628 | /** |
@@ -634,22 +634,22 @@ discard block |
||
| 634 | 634 | * @return object the current DatabaseQueryBuilder instance |
| 635 | 635 | */ |
| 636 | 636 | public function having($field, $op = null, $val = null, $escape = true){ |
| 637 | - if (is_array($op)){ |
|
| 637 | + if (is_array($op)){ |
|
| 638 | 638 | $this->having = $this->getHavingStrIfOperatorIsArray($field, $op, $escape); |
| 639 | - } |
|
| 640 | - else if (! in_array($op, $this->operatorList)){ |
|
| 639 | + } |
|
| 640 | + else if (! in_array($op, $this->operatorList)){ |
|
| 641 | 641 | if (is_null($op)){ |
| 642 | - $op = ''; |
|
| 642 | + $op = ''; |
|
| 643 | 643 | } |
| 644 | 644 | $this->having = $field . ' > ' . ($this->escape($op, $escape)); |
| 645 | - } |
|
| 646 | - else{ |
|
| 645 | + } |
|
| 646 | + else{ |
|
| 647 | 647 | if (is_null($val)){ |
| 648 | - $val = ''; |
|
| 648 | + $val = ''; |
|
| 649 | 649 | } |
| 650 | 650 | $this->having = $field . ' ' . $op . ' ' . ($this->escape($val, $escape)); |
| 651 | - } |
|
| 652 | - return $this; |
|
| 651 | + } |
|
| 652 | + return $this; |
|
| 653 | 653 | } |
| 654 | 654 | |
| 655 | 655 | /** |
@@ -659,12 +659,12 @@ discard block |
||
| 659 | 659 | * @return object the current DatabaseQueryBuilder instance |
| 660 | 660 | */ |
| 661 | 661 | public function insert($data = array(), $escape = true){ |
| 662 | - $columns = array_keys($data); |
|
| 663 | - $column = implode(', ', $columns); |
|
| 664 | - $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
| 662 | + $columns = array_keys($data); |
|
| 663 | + $column = implode(', ', $columns); |
|
| 664 | + $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
| 665 | 665 | |
| 666 | - $this->query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
| 667 | - return $this; |
|
| 666 | + $this->query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
| 667 | + return $this; |
|
| 668 | 668 | } |
| 669 | 669 | |
| 670 | 670 | /** |
@@ -674,25 +674,25 @@ discard block |
||
| 674 | 674 | * @return object the current DatabaseQueryBuilder instance |
| 675 | 675 | */ |
| 676 | 676 | public function update($data = array(), $escape = true){ |
| 677 | - $query = 'UPDATE ' . $this->from . ' SET '; |
|
| 678 | - $values = array(); |
|
| 679 | - foreach ($data as $column => $val){ |
|
| 677 | + $query = 'UPDATE ' . $this->from . ' SET '; |
|
| 678 | + $values = array(); |
|
| 679 | + foreach ($data as $column => $val){ |
|
| 680 | 680 | $values[] = $column . ' = ' . ($this->escape($val, $escape)); |
| 681 | - } |
|
| 682 | - $query .= implode(', ', $values); |
|
| 683 | - if (! empty($this->where)){ |
|
| 681 | + } |
|
| 682 | + $query .= implode(', ', $values); |
|
| 683 | + if (! empty($this->where)){ |
|
| 684 | 684 | $query .= ' WHERE ' . $this->where; |
| 685 | - } |
|
| 685 | + } |
|
| 686 | 686 | |
| 687 | - if (! empty($this->orderBy)){ |
|
| 687 | + if (! empty($this->orderBy)){ |
|
| 688 | 688 | $query .= ' ORDER BY ' . $this->orderBy; |
| 689 | - } |
|
| 689 | + } |
|
| 690 | 690 | |
| 691 | - if (! empty($this->limit)){ |
|
| 691 | + if (! empty($this->limit)){ |
|
| 692 | 692 | $query .= ' LIMIT ' . $this->limit; |
| 693 | - } |
|
| 694 | - $this->query = $query; |
|
| 695 | - return $this; |
|
| 693 | + } |
|
| 694 | + $this->query = $query; |
|
| 695 | + return $this; |
|
| 696 | 696 | } |
| 697 | 697 | |
| 698 | 698 | /** |
@@ -700,25 +700,25 @@ discard block |
||
| 700 | 700 | * @return object the current DatabaseQueryBuilder instance |
| 701 | 701 | */ |
| 702 | 702 | public function delete(){ |
| 703 | - $query = 'DELETE FROM ' . $this->from; |
|
| 704 | - $isTruncate = $query; |
|
| 705 | - if (! empty($this->where)){ |
|
| 706 | - $query .= ' WHERE ' . $this->where; |
|
| 707 | - } |
|
| 703 | + $query = 'DELETE FROM ' . $this->from; |
|
| 704 | + $isTruncate = $query; |
|
| 705 | + if (! empty($this->where)){ |
|
| 706 | + $query .= ' WHERE ' . $this->where; |
|
| 707 | + } |
|
| 708 | 708 | |
| 709 | - if (! empty($this->orderBy)){ |
|
| 710 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 711 | - } |
|
| 709 | + if (! empty($this->orderBy)){ |
|
| 710 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 711 | + } |
|
| 712 | 712 | |
| 713 | - if (! empty($this->limit)){ |
|
| 714 | - $query .= ' LIMIT ' . $this->limit; |
|
| 715 | - } |
|
| 713 | + if (! empty($this->limit)){ |
|
| 714 | + $query .= ' LIMIT ' . $this->limit; |
|
| 715 | + } |
|
| 716 | 716 | |
| 717 | - if ($isTruncate == $query && $this->driver != 'sqlite'){ |
|
| 718 | - $query = 'TRUNCATE TABLE ' . $this->from; |
|
| 719 | - } |
|
| 720 | - $this->query = $query; |
|
| 721 | - return $this; |
|
| 717 | + if ($isTruncate == $query && $this->driver != 'sqlite'){ |
|
| 718 | + $query = 'TRUNCATE TABLE ' . $this->from; |
|
| 719 | + } |
|
| 720 | + $this->query = $query; |
|
| 721 | + return $this; |
|
| 722 | 722 | } |
| 723 | 723 | |
| 724 | 724 | /** |
@@ -728,11 +728,11 @@ discard block |
||
| 728 | 728 | * @return mixed the data after escaped or the same data if not |
| 729 | 729 | */ |
| 730 | 730 | public function escape($data, $escaped = true){ |
| 731 | - $data = trim($data); |
|
| 732 | - if($escaped){ |
|
| 731 | + $data = trim($data); |
|
| 732 | + if($escaped){ |
|
| 733 | 733 | return $this->pdo->quote($data); |
| 734 | - } |
|
| 735 | - return $data; |
|
| 734 | + } |
|
| 735 | + return $data; |
|
| 736 | 736 | } |
| 737 | 737 | |
| 738 | 738 | |
@@ -741,126 +741,126 @@ discard block |
||
| 741 | 741 | * @return string |
| 742 | 742 | */ |
| 743 | 743 | public function getQuery(){ |
| 744 | - //INSERT, UPDATE, DELETE already set it, if is the SELECT we need set it now |
|
| 745 | - if(empty($this->query)){ |
|
| 746 | - $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
|
| 747 | - if (! empty($this->join)){ |
|
| 748 | - $query .= $this->join; |
|
| 749 | - } |
|
| 744 | + //INSERT, UPDATE, DELETE already set it, if is the SELECT we need set it now |
|
| 745 | + if(empty($this->query)){ |
|
| 746 | + $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
|
| 747 | + if (! empty($this->join)){ |
|
| 748 | + $query .= $this->join; |
|
| 749 | + } |
|
| 750 | 750 | |
| 751 | - if (! empty($this->where)){ |
|
| 752 | - $query .= ' WHERE ' . $this->where; |
|
| 753 | - } |
|
| 751 | + if (! empty($this->where)){ |
|
| 752 | + $query .= ' WHERE ' . $this->where; |
|
| 753 | + } |
|
| 754 | 754 | |
| 755 | - if (! empty($this->groupBy)){ |
|
| 756 | - $query .= ' GROUP BY ' . $this->groupBy; |
|
| 757 | - } |
|
| 755 | + if (! empty($this->groupBy)){ |
|
| 756 | + $query .= ' GROUP BY ' . $this->groupBy; |
|
| 757 | + } |
|
| 758 | 758 | |
| 759 | - if (! empty($this->having)){ |
|
| 760 | - $query .= ' HAVING ' . $this->having; |
|
| 761 | - } |
|
| 759 | + if (! empty($this->having)){ |
|
| 760 | + $query .= ' HAVING ' . $this->having; |
|
| 761 | + } |
|
| 762 | 762 | |
| 763 | - if (! empty($this->orderBy)){ |
|
| 764 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 765 | - } |
|
| 763 | + if (! empty($this->orderBy)){ |
|
| 764 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 765 | + } |
|
| 766 | 766 | |
| 767 | - if (! empty($this->limit)){ |
|
| 768 | - $query .= ' LIMIT ' . $this->limit; |
|
| 769 | - } |
|
| 770 | - $this->query = $query; |
|
| 771 | - } |
|
| 772 | - return $this->query; |
|
| 767 | + if (! empty($this->limit)){ |
|
| 768 | + $query .= ' LIMIT ' . $this->limit; |
|
| 769 | + } |
|
| 770 | + $this->query = $query; |
|
| 771 | + } |
|
| 772 | + return $this->query; |
|
| 773 | 773 | } |
| 774 | 774 | |
| 775 | 775 | |
| 776 | - /** |
|
| 777 | - * Return the PDO instance |
|
| 778 | - * @return object |
|
| 779 | - */ |
|
| 776 | + /** |
|
| 777 | + * Return the PDO instance |
|
| 778 | + * @return object |
|
| 779 | + */ |
|
| 780 | 780 | public function getPdo(){ |
| 781 | - return $this->pdo; |
|
| 781 | + return $this->pdo; |
|
| 782 | 782 | } |
| 783 | 783 | |
| 784 | 784 | /** |
| 785 | 785 | * Set the PDO instance |
| 786 | 786 | * @param PDO $pdo the pdo object |
| 787 | - * @return object DatabaseQueryBuilder |
|
| 787 | + * @return object DatabaseQueryBuilder |
|
| 788 | 788 | */ |
| 789 | 789 | public function setPdo(PDO $pdo = null){ |
| 790 | - $this->pdo = $pdo; |
|
| 791 | - return $this; |
|
| 790 | + $this->pdo = $pdo; |
|
| 791 | + return $this; |
|
| 792 | 792 | } |
| 793 | 793 | |
| 794 | - /** |
|
| 795 | - * Return the database table prefix |
|
| 796 | - * @return string |
|
| 797 | - */ |
|
| 794 | + /** |
|
| 795 | + * Return the database table prefix |
|
| 796 | + * @return string |
|
| 797 | + */ |
|
| 798 | 798 | public function getPrefix(){ |
| 799 | - return $this->prefix; |
|
| 799 | + return $this->prefix; |
|
| 800 | 800 | } |
| 801 | 801 | |
| 802 | 802 | /** |
| 803 | 803 | * Set the database table prefix |
| 804 | 804 | * @param string $prefix the new prefix |
| 805 | - * @return object DatabaseQueryBuilder |
|
| 805 | + * @return object DatabaseQueryBuilder |
|
| 806 | 806 | */ |
| 807 | 807 | public function setPrefix($prefix){ |
| 808 | - $this->prefix = $prefix; |
|
| 809 | - return $this; |
|
| 808 | + $this->prefix = $prefix; |
|
| 809 | + return $this; |
|
| 810 | 810 | } |
| 811 | 811 | |
| 812 | - /** |
|
| 813 | - * Return the database driver |
|
| 814 | - * @return string |
|
| 815 | - */ |
|
| 812 | + /** |
|
| 813 | + * Return the database driver |
|
| 814 | + * @return string |
|
| 815 | + */ |
|
| 816 | 816 | public function getDriver(){ |
| 817 | - return $this->driver; |
|
| 817 | + return $this->driver; |
|
| 818 | 818 | } |
| 819 | 819 | |
| 820 | 820 | /** |
| 821 | 821 | * Set the database driver |
| 822 | 822 | * @param string $driver the new driver |
| 823 | - * @return object DatabaseQueryBuilder |
|
| 823 | + * @return object DatabaseQueryBuilder |
|
| 824 | 824 | */ |
| 825 | 825 | public function setDriver($driver){ |
| 826 | - $this->driver = $driver; |
|
| 827 | - return $this; |
|
| 826 | + $this->driver = $driver; |
|
| 827 | + return $this; |
|
| 828 | 828 | } |
| 829 | 829 | |
| 830 | - /** |
|
| 831 | - * Reset the DatabaseQueryBuilder class attributs to the initial values before each query. |
|
| 832 | - * @return object the current DatabaseQueryBuilder instance |
|
| 833 | - */ |
|
| 830 | + /** |
|
| 831 | + * Reset the DatabaseQueryBuilder class attributs to the initial values before each query. |
|
| 832 | + * @return object the current DatabaseQueryBuilder instance |
|
| 833 | + */ |
|
| 834 | 834 | public function reset(){ |
| 835 | - $this->select = '*'; |
|
| 836 | - $this->from = null; |
|
| 837 | - $this->where = null; |
|
| 838 | - $this->limit = null; |
|
| 839 | - $this->orderBy = null; |
|
| 840 | - $this->groupBy = null; |
|
| 841 | - $this->having = null; |
|
| 842 | - $this->join = null; |
|
| 843 | - $this->query = null; |
|
| 844 | - return $this; |
|
| 845 | - } |
|
| 846 | - |
|
| 847 | - /** |
|
| 848 | - * Get the SQL HAVING clause when operator argument is an array |
|
| 849 | - * @see DatabaseQueryBuilder::having |
|
| 850 | - * |
|
| 851 | - * @return string |
|
| 852 | - */ |
|
| 835 | + $this->select = '*'; |
|
| 836 | + $this->from = null; |
|
| 837 | + $this->where = null; |
|
| 838 | + $this->limit = null; |
|
| 839 | + $this->orderBy = null; |
|
| 840 | + $this->groupBy = null; |
|
| 841 | + $this->having = null; |
|
| 842 | + $this->join = null; |
|
| 843 | + $this->query = null; |
|
| 844 | + return $this; |
|
| 845 | + } |
|
| 846 | + |
|
| 847 | + /** |
|
| 848 | + * Get the SQL HAVING clause when operator argument is an array |
|
| 849 | + * @see DatabaseQueryBuilder::having |
|
| 850 | + * |
|
| 851 | + * @return string |
|
| 852 | + */ |
|
| 853 | 853 | protected function getHavingStrIfOperatorIsArray($field, $op = null, $escape = true){ |
| 854 | 854 | $x = explode('?', $field); |
| 855 | 855 | $w = ''; |
| 856 | 856 | foreach($x as $k => $v){ |
| 857 | - if (!empty($v)){ |
|
| 857 | + if (!empty($v)){ |
|
| 858 | 858 | if (! isset($op[$k])){ |
| 859 | - $op[$k] = ''; |
|
| 859 | + $op[$k] = ''; |
|
| 860 | + } |
|
| 861 | + $w .= $v . (isset($op[$k]) ? $this->escape($op[$k], $escape) : ''); |
|
| 862 | + } |
|
| 860 | 863 | } |
| 861 | - $w .= $v . (isset($op[$k]) ? $this->escape($op[$k], $escape) : ''); |
|
| 862 | - } |
|
| 863 | - } |
|
| 864 | 864 | return $w; |
| 865 | 865 | } |
| 866 | 866 | |
@@ -872,35 +872,35 @@ discard block |
||
| 872 | 872 | * @return string |
| 873 | 873 | */ |
| 874 | 874 | protected function getWhereStrIfIsArray(array $where, $type = '', $andOr = 'AND', $escape = true){ |
| 875 | - $_where = array(); |
|
| 876 | - foreach ($where as $column => $data){ |
|
| 875 | + $_where = array(); |
|
| 876 | + foreach ($where as $column => $data){ |
|
| 877 | 877 | if (is_null($data)){ |
| 878 | - $data = ''; |
|
| 878 | + $data = ''; |
|
| 879 | 879 | } |
| 880 | 880 | $_where[] = $type . $column . ' = ' . ($this->escape($data, $escape)); |
| 881 | - } |
|
| 882 | - $where = implode(' '.$andOr.' ', $_where); |
|
| 883 | - return $where; |
|
| 881 | + } |
|
| 882 | + $where = implode(' '.$andOr.' ', $_where); |
|
| 883 | + return $where; |
|
| 884 | 884 | } |
| 885 | 885 | |
| 886 | - /** |
|
| 887 | - * Get the SQL WHERE clause when operator argument is an array |
|
| 888 | - * @see DatabaseQueryBuilder::where |
|
| 889 | - * |
|
| 890 | - * @return string |
|
| 891 | - */ |
|
| 886 | + /** |
|
| 887 | + * Get the SQL WHERE clause when operator argument is an array |
|
| 888 | + * @see DatabaseQueryBuilder::where |
|
| 889 | + * |
|
| 890 | + * @return string |
|
| 891 | + */ |
|
| 892 | 892 | protected function getWhereStrIfOperatorIsArray($where, array $op, $type = '', $escape = true){ |
| 893 | - $x = explode('?', $where); |
|
| 894 | - $w = ''; |
|
| 895 | - foreach($x as $k => $v){ |
|
| 893 | + $x = explode('?', $where); |
|
| 894 | + $w = ''; |
|
| 895 | + foreach($x as $k => $v){ |
|
| 896 | 896 | if (! empty($v)){ |
| 897 | 897 | if (isset($op[$k]) && is_null($op[$k])){ |
| 898 | - $op[$k] = ''; |
|
| 898 | + $op[$k] = ''; |
|
| 899 | 899 | } |
| 900 | 900 | $w .= $type . $v . (isset($op[$k]) ? ($this->escape($op[$k], $escape)) : ''); |
| 901 | 901 | } |
| 902 | - } |
|
| 903 | - return $w; |
|
| 902 | + } |
|
| 903 | + return $w; |
|
| 904 | 904 | } |
| 905 | 905 | |
| 906 | 906 | /** |
@@ -910,53 +910,53 @@ discard block |
||
| 910 | 910 | * @return string |
| 911 | 911 | */ |
| 912 | 912 | protected function getWhereStrForOperator($where, $op = null, $val = null, $type = '', $escape = true){ |
| 913 | - $w = ''; |
|
| 914 | - if (! in_array((string)$op, $this->operatorList)){ |
|
| 915 | - if (is_null($op)){ |
|
| 913 | + $w = ''; |
|
| 914 | + if (! in_array((string)$op, $this->operatorList)){ |
|
| 915 | + if (is_null($op)){ |
|
| 916 | 916 | $op = ''; |
| 917 | - } |
|
| 918 | - $w = $type . $where . ' = ' . ($this->escape($op, $escape)); |
|
| 917 | + } |
|
| 918 | + $w = $type . $where . ' = ' . ($this->escape($op, $escape)); |
|
| 919 | 919 | } else { |
| 920 | - if (is_null($val)){ |
|
| 920 | + if (is_null($val)){ |
|
| 921 | 921 | $val = ''; |
| 922 | - } |
|
| 923 | - $w = $type . $where . $op . ($this->escape($val, $escape)); |
|
| 922 | + } |
|
| 923 | + $w = $type . $where . $op . ($this->escape($val, $escape)); |
|
| 924 | 924 | } |
| 925 | 925 | return $w; |
| 926 | - } |
|
| 927 | - |
|
| 928 | - /** |
|
| 929 | - * Set the $this->where property |
|
| 930 | - * @param string $whereStr the WHERE clause string |
|
| 931 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 932 | - */ |
|
| 933 | - protected function setWhereStr($whereStr, $andOr = 'AND'){ |
|
| 926 | + } |
|
| 927 | + |
|
| 928 | + /** |
|
| 929 | + * Set the $this->where property |
|
| 930 | + * @param string $whereStr the WHERE clause string |
|
| 931 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 932 | + */ |
|
| 933 | + protected function setWhereStr($whereStr, $andOr = 'AND'){ |
|
| 934 | 934 | if (empty($this->where)){ |
| 935 | - $this->where = $whereStr; |
|
| 935 | + $this->where = $whereStr; |
|
| 936 | 936 | } else { |
| 937 | - if (substr(trim($this->where), -1) == '('){ |
|
| 937 | + if (substr(trim($this->where), -1) == '('){ |
|
| 938 | 938 | $this->where = $this->where . ' ' . $whereStr; |
| 939 | - } else { |
|
| 939 | + } else { |
|
| 940 | 940 | $this->where = $this->where . ' '.$andOr.' ' . $whereStr; |
| 941 | - } |
|
| 941 | + } |
|
| 942 | + } |
|
| 942 | 943 | } |
| 943 | - } |
|
| 944 | 944 | |
| 945 | 945 | |
| 946 | - /** |
|
| 947 | - * Set the SQL SELECT for function MIN, MAX, SUM, AVG, COUNT, AVG |
|
| 948 | - * @param string $clause the clause type like MIN, MAX, etc. |
|
| 949 | - * @see DatabaseQueryBuilder::min |
|
| 950 | - * @see DatabaseQueryBuilder::max |
|
| 951 | - * @see DatabaseQueryBuilder::sum |
|
| 952 | - * @see DatabaseQueryBuilder::count |
|
| 953 | - * @see DatabaseQueryBuilder::avg |
|
| 954 | - * @return object |
|
| 955 | - */ |
|
| 946 | + /** |
|
| 947 | + * Set the SQL SELECT for function MIN, MAX, SUM, AVG, COUNT, AVG |
|
| 948 | + * @param string $clause the clause type like MIN, MAX, etc. |
|
| 949 | + * @see DatabaseQueryBuilder::min |
|
| 950 | + * @see DatabaseQueryBuilder::max |
|
| 951 | + * @see DatabaseQueryBuilder::sum |
|
| 952 | + * @see DatabaseQueryBuilder::count |
|
| 953 | + * @see DatabaseQueryBuilder::avg |
|
| 954 | + * @return object |
|
| 955 | + */ |
|
| 956 | 956 | protected function select_min_max_sum_count_avg($clause, $field, $name = null){ |
| 957 | - $clause = strtoupper($clause); |
|
| 958 | - $func = $clause . '(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 959 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 960 | - return $this; |
|
| 957 | + $clause = strtoupper($clause); |
|
| 958 | + $func = $clause . '(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 959 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 960 | + return $this; |
|
| 961 | 961 | } |
| 962 | 962 | } |
@@ -1,146 +1,146 @@ |
||
| 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 Controller{ |
|
| 27 | + class Controller{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The name of the module if this controller belong to an module |
|
| 31 | - * @var string |
|
| 32 | - */ |
|
| 33 | - public $moduleName = null; |
|
| 29 | + /** |
|
| 30 | + * The name of the module if this controller belong to an module |
|
| 31 | + * @var string |
|
| 32 | + */ |
|
| 33 | + public $moduleName = null; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * The singleton of the super object |
|
| 37 | - * @var Controller |
|
| 38 | - */ |
|
| 39 | - private static $instance; |
|
| 35 | + /** |
|
| 36 | + * The singleton of the super object |
|
| 37 | + * @var Controller |
|
| 38 | + */ |
|
| 39 | + private static $instance; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * The logger instance |
|
| 43 | - * @var Log |
|
| 44 | - */ |
|
| 45 | - protected $logger; |
|
| 41 | + /** |
|
| 42 | + * The logger instance |
|
| 43 | + * @var Log |
|
| 44 | + */ |
|
| 45 | + protected $logger; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Class constructor |
|
| 49 | - * @param object $logger the Log instance to use if is null will create one |
|
| 50 | - */ |
|
| 51 | - public function __construct(Log $logger = null){ |
|
| 52 | - //setting the Log instance |
|
| 53 | - $this->setLoggerFromParamOrCreateNewInstance($logger); |
|
| 47 | + /** |
|
| 48 | + * Class constructor |
|
| 49 | + * @param object $logger the Log instance to use if is null will create one |
|
| 50 | + */ |
|
| 51 | + public function __construct(Log $logger = null){ |
|
| 52 | + //setting the Log instance |
|
| 53 | + $this->setLoggerFromParamOrCreateNewInstance($logger); |
|
| 54 | 54 | |
| 55 | - //instance of the super object |
|
| 56 | - self::$instance = & $this; |
|
| 55 | + //instance of the super object |
|
| 56 | + self::$instance = & $this; |
|
| 57 | 57 | |
| 58 | - //Load the resources loaded during the application bootstrap |
|
| 59 | - $this->logger->debug('Adding the loaded classes to the super instance'); |
|
| 60 | - foreach (class_loaded() as $var => $class){ |
|
| 61 | - $this->$var =& class_loader($class); |
|
| 62 | - } |
|
| 58 | + //Load the resources loaded during the application bootstrap |
|
| 59 | + $this->logger->debug('Adding the loaded classes to the super instance'); |
|
| 60 | + foreach (class_loaded() as $var => $class){ |
|
| 61 | + $this->$var =& class_loader($class); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - //set module using the router |
|
| 65 | - $this->setModuleNameFromRouter(); |
|
| 64 | + //set module using the router |
|
| 65 | + $this->setModuleNameFromRouter(); |
|
| 66 | 66 | |
| 67 | - //load the required resources |
|
| 68 | - $this->loadRequiredResources(); |
|
| 67 | + //load the required resources |
|
| 68 | + $this->loadRequiredResources(); |
|
| 69 | 69 | |
| 70 | - //set the cache using the configuration |
|
| 71 | - $this->setCacheFromParamOrConfig(null); |
|
| 70 | + //set the cache using the configuration |
|
| 71 | + $this->setCacheFromParamOrConfig(null); |
|
| 72 | 72 | |
| 73 | - //set application session configuration |
|
| 74 | - $this->logger->debug('Setting PHP application session handler'); |
|
| 75 | - set_session_config(); |
|
| 73 | + //set application session configuration |
|
| 74 | + $this->logger->debug('Setting PHP application session handler'); |
|
| 75 | + set_session_config(); |
|
| 76 | 76 | |
| 77 | - //dispatch the loaded instance of super controller event |
|
| 78 | - $this->eventdispatcher->dispatch('SUPER_CONTROLLER_CREATED'); |
|
| 79 | - } |
|
| 77 | + //dispatch the loaded instance of super controller event |
|
| 78 | + $this->eventdispatcher->dispatch('SUPER_CONTROLLER_CREATED'); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * This is a very useful method it's used to get the super object instance |
|
| 84 | - * @return Controller the super object instance |
|
| 85 | - */ |
|
| 86 | - public static function &get_instance(){ |
|
| 87 | - return self::$instance; |
|
| 88 | - } |
|
| 82 | + /** |
|
| 83 | + * This is a very useful method it's used to get the super object instance |
|
| 84 | + * @return Controller the super object instance |
|
| 85 | + */ |
|
| 86 | + public static function &get_instance(){ |
|
| 87 | + return self::$instance; |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * This method is used to set the module name |
|
| 92 | - */ |
|
| 93 | - protected function setModuleNameFromRouter(){ |
|
| 94 | - //set the module using the router instance |
|
| 95 | - if(isset($this->router) && $this->router->getModule()){ |
|
| 96 | - $this->moduleName = $this->router->getModule(); |
|
| 97 | - } |
|
| 98 | - } |
|
| 90 | + /** |
|
| 91 | + * This method is used to set the module name |
|
| 92 | + */ |
|
| 93 | + protected function setModuleNameFromRouter(){ |
|
| 94 | + //set the module using the router instance |
|
| 95 | + if(isset($this->router) && $this->router->getModule()){ |
|
| 96 | + $this->moduleName = $this->router->getModule(); |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * Set the cache using the argument otherwise will use the configuration |
|
| 102 | - * @param CacheInterface $cache the implementation of CacheInterface if null will use the configured |
|
| 103 | - */ |
|
| 104 | - protected function setCacheFromParamOrConfig(CacheInterface $cache = null){ |
|
| 105 | - $this->logger->debug('Setting the cache handler instance'); |
|
| 106 | - //set cache handler instance |
|
| 107 | - if(get_config('cache_enable', false)){ |
|
| 108 | - if ($cache !== null){ |
|
| 109 | - $this->cache = $cache; |
|
| 110 | - } else if (isset($this->{strtolower(get_config('cache_handler'))})){ |
|
| 111 | - $this->cache = $this->{strtolower(get_config('cache_handler'))}; |
|
| 112 | - unset($this->{strtolower(get_config('cache_handler'))}); |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - } |
|
| 100 | + /** |
|
| 101 | + * Set the cache using the argument otherwise will use the configuration |
|
| 102 | + * @param CacheInterface $cache the implementation of CacheInterface if null will use the configured |
|
| 103 | + */ |
|
| 104 | + protected function setCacheFromParamOrConfig(CacheInterface $cache = null){ |
|
| 105 | + $this->logger->debug('Setting the cache handler instance'); |
|
| 106 | + //set cache handler instance |
|
| 107 | + if(get_config('cache_enable', false)){ |
|
| 108 | + if ($cache !== null){ |
|
| 109 | + $this->cache = $cache; |
|
| 110 | + } else if (isset($this->{strtolower(get_config('cache_handler'))})){ |
|
| 111 | + $this->cache = $this->{strtolower(get_config('cache_handler'))}; |
|
| 112 | + unset($this->{strtolower(get_config('cache_handler'))}); |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * Set the Log instance using argument or create new instance |
|
| 119 | - * @param object $logger the Log instance if not null |
|
| 120 | - */ |
|
| 121 | - protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 122 | - if($logger !== null){ |
|
| 123 | - $this->logger = $logger; |
|
| 124 | - } |
|
| 125 | - else{ |
|
| 126 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 127 | - $this->logger->setLogger('MainController'); |
|
| 128 | - } |
|
| 129 | - } |
|
| 117 | + /** |
|
| 118 | + * Set the Log instance using argument or create new instance |
|
| 119 | + * @param object $logger the Log instance if not null |
|
| 120 | + */ |
|
| 121 | + protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 122 | + if($logger !== null){ |
|
| 123 | + $this->logger = $logger; |
|
| 124 | + } |
|
| 125 | + else{ |
|
| 126 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 127 | + $this->logger->setLogger('MainController'); |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | - /** |
|
| 132 | - * This method is used to load the required resources for framework to work |
|
| 133 | - * @return void |
|
| 134 | - */ |
|
| 135 | - private function loadRequiredResources(){ |
|
| 136 | - $this->logger->debug('Loading the required classes into super instance'); |
|
| 137 | - $this->eventdispatcher =& class_loader('EventDispatcher', 'classes'); |
|
| 138 | - $this->loader =& class_loader('Loader', 'classes'); |
|
| 139 | - $this->lang =& class_loader('Lang', 'classes'); |
|
| 140 | - $this->request =& class_loader('Request', 'classes'); |
|
| 141 | - //dispatch the request instance created event |
|
| 142 | - $this->eventdispatcher->dispatch('REQUEST_CREATED'); |
|
| 143 | - $this->response =& class_loader('Response', 'classes', 'classes'); |
|
| 144 | - } |
|
| 131 | + /** |
|
| 132 | + * This method is used to load the required resources for framework to work |
|
| 133 | + * @return void |
|
| 134 | + */ |
|
| 135 | + private function loadRequiredResources(){ |
|
| 136 | + $this->logger->debug('Loading the required classes into super instance'); |
|
| 137 | + $this->eventdispatcher =& class_loader('EventDispatcher', 'classes'); |
|
| 138 | + $this->loader =& class_loader('Loader', 'classes'); |
|
| 139 | + $this->lang =& class_loader('Lang', 'classes'); |
|
| 140 | + $this->request =& class_loader('Request', 'classes'); |
|
| 141 | + //dispatch the request instance created event |
|
| 142 | + $this->eventdispatcher->dispatch('REQUEST_CREATED'); |
|
| 143 | + $this->response =& class_loader('Response', 'classes', 'classes'); |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - } |
|
| 146 | + } |
|