@@ -1,251 +1,251 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - class Html{ |
|
| 27 | + class Html{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Generate the html anchor link |
|
| 31 | - * @param string $link the href attribute value |
|
| 32 | - * @param string $anchor the displayed anchor |
|
| 33 | - * @param array $attributes the additional attributes to be added |
|
| 34 | - * @param boolean $return whether need return the generated html or just display it directly |
|
| 35 | - * |
|
| 36 | - * @return string|void the anchor link generated html if $return is true or display it if not |
|
| 37 | - */ |
|
| 38 | - public static function a($link = '', $anchor = null, array $attributes = array(), $return = true){ |
|
| 39 | - if(! is_url($link)){ |
|
| 40 | - $link = Url::site_url($link); |
|
| 41 | - } |
|
| 42 | - if(! $anchor){ |
|
| 43 | - $anchor = $link; |
|
| 44 | - } |
|
| 45 | - $str = null; |
|
| 46 | - $str .= '<a href = "'.$link.'"'; |
|
| 47 | - $str .= attributes_to_string($attributes); |
|
| 48 | - $str .= '>'; |
|
| 49 | - $str .= $anchor; |
|
| 50 | - $str .= '</a>'; |
|
| 29 | + /** |
|
| 30 | + * Generate the html anchor link |
|
| 31 | + * @param string $link the href attribute value |
|
| 32 | + * @param string $anchor the displayed anchor |
|
| 33 | + * @param array $attributes the additional attributes to be added |
|
| 34 | + * @param boolean $return whether need return the generated html or just display it directly |
|
| 35 | + * |
|
| 36 | + * @return string|void the anchor link generated html if $return is true or display it if not |
|
| 37 | + */ |
|
| 38 | + public static function a($link = '', $anchor = null, array $attributes = array(), $return = true){ |
|
| 39 | + if(! is_url($link)){ |
|
| 40 | + $link = Url::site_url($link); |
|
| 41 | + } |
|
| 42 | + if(! $anchor){ |
|
| 43 | + $anchor = $link; |
|
| 44 | + } |
|
| 45 | + $str = null; |
|
| 46 | + $str .= '<a href = "'.$link.'"'; |
|
| 47 | + $str .= attributes_to_string($attributes); |
|
| 48 | + $str .= '>'; |
|
| 49 | + $str .= $anchor; |
|
| 50 | + $str .= '</a>'; |
|
| 51 | 51 | |
| 52 | - if($return){ |
|
| 53 | - return $str; |
|
| 54 | - } |
|
| 55 | - echo $str; |
|
| 56 | - } |
|
| 52 | + if($return){ |
|
| 53 | + return $str; |
|
| 54 | + } |
|
| 55 | + echo $str; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Generate an mailto anchor link |
|
| 60 | - * @param string $link the email address |
|
| 61 | - * @param string $anchor the displayed value of the link |
|
| 62 | - * @param array $attributes the additional attributes to be added |
|
| 63 | - * @param boolean $return whether need return the generated html or just display it directly |
|
| 64 | - * |
|
| 65 | - * @return string|void the generated html for mailto link if $return is true or display it if not |
|
| 66 | - */ |
|
| 67 | - public static function mailto($link, $anchor = null, array $attributes = array(), $return = true){ |
|
| 68 | - if(! $anchor){ |
|
| 69 | - $anchor = $link; |
|
| 70 | - } |
|
| 71 | - $str = null; |
|
| 72 | - $str .= '<a href = "mailto:'.$link.'"'; |
|
| 73 | - $str .= attributes_to_string($attributes); |
|
| 74 | - $str .= '>'; |
|
| 75 | - $str .= $anchor; |
|
| 76 | - $str .= '</a>'; |
|
| 58 | + /** |
|
| 59 | + * Generate an mailto anchor link |
|
| 60 | + * @param string $link the email address |
|
| 61 | + * @param string $anchor the displayed value of the link |
|
| 62 | + * @param array $attributes the additional attributes to be added |
|
| 63 | + * @param boolean $return whether need return the generated html or just display it directly |
|
| 64 | + * |
|
| 65 | + * @return string|void the generated html for mailto link if $return is true or display it if not |
|
| 66 | + */ |
|
| 67 | + public static function mailto($link, $anchor = null, array $attributes = array(), $return = true){ |
|
| 68 | + if(! $anchor){ |
|
| 69 | + $anchor = $link; |
|
| 70 | + } |
|
| 71 | + $str = null; |
|
| 72 | + $str .= '<a href = "mailto:'.$link.'"'; |
|
| 73 | + $str .= attributes_to_string($attributes); |
|
| 74 | + $str .= '>'; |
|
| 75 | + $str .= $anchor; |
|
| 76 | + $str .= '</a>'; |
|
| 77 | 77 | |
| 78 | - if($return){ |
|
| 79 | - return $str; |
|
| 80 | - } |
|
| 81 | - echo $str; |
|
| 82 | - } |
|
| 78 | + if($return){ |
|
| 79 | + return $str; |
|
| 80 | + } |
|
| 81 | + echo $str; |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Generate the html "br" tag |
|
| 86 | - * @param integer $nb the number of generated "<br />" tag |
|
| 87 | - * @param boolean $return whether need return the generated html or just display it directly |
|
| 88 | - * |
|
| 89 | - * @return string|void the generated "br" html if $return is true or display it if not |
|
| 90 | - */ |
|
| 91 | - public static function br($nb = 1, $return = true){ |
|
| 92 | - $nb = (int) $nb; |
|
| 93 | - $str = null; |
|
| 94 | - for ($i = 1; $i <= $nb; $i++) { |
|
| 95 | - $str .= '<br />'; |
|
| 96 | - } |
|
| 84 | + /** |
|
| 85 | + * Generate the html "br" tag |
|
| 86 | + * @param integer $nb the number of generated "<br />" tag |
|
| 87 | + * @param boolean $return whether need return the generated html or just display it directly |
|
| 88 | + * |
|
| 89 | + * @return string|void the generated "br" html if $return is true or display it if not |
|
| 90 | + */ |
|
| 91 | + public static function br($nb = 1, $return = true){ |
|
| 92 | + $nb = (int) $nb; |
|
| 93 | + $str = null; |
|
| 94 | + for ($i = 1; $i <= $nb; $i++) { |
|
| 95 | + $str .= '<br />'; |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - if($return){ |
|
| 99 | - return $str; |
|
| 100 | - } |
|
| 101 | - echo $str; |
|
| 102 | - } |
|
| 98 | + if($return){ |
|
| 99 | + return $str; |
|
| 100 | + } |
|
| 101 | + echo $str; |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * Generate the html content for tag "hr" |
|
| 106 | - * @param integer $nb the number of generated "<hr />" tag |
|
| 107 | - * @param array $attributes the tag attributes |
|
| 108 | - * @param boolean $return whether need return the generated html or just display it directly |
|
| 109 | - * |
|
| 110 | - * @return string|void the generated "hr" html if $return is true or display it if not. |
|
| 111 | - */ |
|
| 112 | - public static function hr($nb = 1, array $attributes = array(), $return = true){ |
|
| 113 | - $nb = (int) $nb; |
|
| 114 | - $str = null; |
|
| 115 | - for ($i = 1; $i <= $nb; $i++) { |
|
| 116 | - $str .= '<hr' .attributes_to_string($attributes). ' />'; |
|
| 117 | - } |
|
| 118 | - if($return){ |
|
| 119 | - return $str; |
|
| 120 | - } |
|
| 121 | - echo $str; |
|
| 122 | - } |
|
| 104 | + /** |
|
| 105 | + * Generate the html content for tag "hr" |
|
| 106 | + * @param integer $nb the number of generated "<hr />" tag |
|
| 107 | + * @param array $attributes the tag attributes |
|
| 108 | + * @param boolean $return whether need return the generated html or just display it directly |
|
| 109 | + * |
|
| 110 | + * @return string|void the generated "hr" html if $return is true or display it if not. |
|
| 111 | + */ |
|
| 112 | + public static function hr($nb = 1, array $attributes = array(), $return = true){ |
|
| 113 | + $nb = (int) $nb; |
|
| 114 | + $str = null; |
|
| 115 | + for ($i = 1; $i <= $nb; $i++) { |
|
| 116 | + $str .= '<hr' .attributes_to_string($attributes). ' />'; |
|
| 117 | + } |
|
| 118 | + if($return){ |
|
| 119 | + return $str; |
|
| 120 | + } |
|
| 121 | + echo $str; |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - /** |
|
| 125 | - * Generate the html content for tag like h1, h2, h3, h4, h5 and h6 |
|
| 126 | - * @param integer $type the tag type 1 mean h1, 2 h2, etc, |
|
| 127 | - * @param string $text the display text |
|
| 128 | - * @param integer $nb the number of generated "<h{1,2,3,4,5,6}>" |
|
| 129 | - * @param array $attributes the tag attributes |
|
| 130 | - * @param boolean $return whether need return the generated html or just display it directly |
|
| 131 | - * |
|
| 132 | - * @return string|void the generated header html if $return is true or display it if not. |
|
| 133 | - */ |
|
| 134 | - public static function head($type = 1, $text = null, $nb = 1, array $attributes = array(), $return = true){ |
|
| 135 | - $nb = (int) $nb; |
|
| 136 | - $type = (int) $type; |
|
| 137 | - if($type <= 0 || $type > 6){ |
|
| 138 | - $type = 1; |
|
| 139 | - } |
|
| 140 | - $str = null; |
|
| 141 | - for ($i = 1; $i <= $nb; $i++) { |
|
| 142 | - $str .= '<h' . $type . attributes_to_string($attributes). '>' .$text. '</h' . $type . '>'; |
|
| 143 | - } |
|
| 144 | - if($return){ |
|
| 145 | - return $str; |
|
| 146 | - } |
|
| 147 | - echo $str; |
|
| 148 | - } |
|
| 124 | + /** |
|
| 125 | + * Generate the html content for tag like h1, h2, h3, h4, h5 and h6 |
|
| 126 | + * @param integer $type the tag type 1 mean h1, 2 h2, etc, |
|
| 127 | + * @param string $text the display text |
|
| 128 | + * @param integer $nb the number of generated "<h{1,2,3,4,5,6}>" |
|
| 129 | + * @param array $attributes the tag attributes |
|
| 130 | + * @param boolean $return whether need return the generated html or just display it directly |
|
| 131 | + * |
|
| 132 | + * @return string|void the generated header html if $return is true or display it if not. |
|
| 133 | + */ |
|
| 134 | + public static function head($type = 1, $text = null, $nb = 1, array $attributes = array(), $return = true){ |
|
| 135 | + $nb = (int) $nb; |
|
| 136 | + $type = (int) $type; |
|
| 137 | + if($type <= 0 || $type > 6){ |
|
| 138 | + $type = 1; |
|
| 139 | + } |
|
| 140 | + $str = null; |
|
| 141 | + for ($i = 1; $i <= $nb; $i++) { |
|
| 142 | + $str .= '<h' . $type . attributes_to_string($attributes). '>' .$text. '</h' . $type . '>'; |
|
| 143 | + } |
|
| 144 | + if($return){ |
|
| 145 | + return $str; |
|
| 146 | + } |
|
| 147 | + echo $str; |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * Generate the html "ul" tag |
|
| 152 | - * @param array $data the data to use for each "li" tag |
|
| 153 | - * @param array $attributes the "ul" properties attribute use the array index below for each tag: |
|
| 154 | - * for ul "ul", for li "li". |
|
| 155 | - * @param boolean $return whether need return the generated html or just display it directly |
|
| 156 | - * |
|
| 157 | - * @return string|void the generated "ul" html if $return is true or display it if not. |
|
| 158 | - */ |
|
| 159 | - public static function ul($data = array(), $attributes = array(), $return = true){ |
|
| 160 | - $data = (array) $data; |
|
| 161 | - $str = null; |
|
| 162 | - $str .= '<ul' . (! empty($attributes['ul']) ? attributes_to_string($attributes['ul']):'') . '>'; |
|
| 163 | - foreach ($data as $row) { |
|
| 164 | - $str .= '<li' . (! empty($attributes['li']) ? attributes_to_string($attributes['li']):'') .'>' .$row. '</li>'; |
|
| 165 | - } |
|
| 166 | - $str .= '</ul>'; |
|
| 167 | - if($return){ |
|
| 168 | - return $str; |
|
| 169 | - } |
|
| 170 | - echo $str; |
|
| 171 | - } |
|
| 150 | + /** |
|
| 151 | + * Generate the html "ul" tag |
|
| 152 | + * @param array $data the data to use for each "li" tag |
|
| 153 | + * @param array $attributes the "ul" properties attribute use the array index below for each tag: |
|
| 154 | + * for ul "ul", for li "li". |
|
| 155 | + * @param boolean $return whether need return the generated html or just display it directly |
|
| 156 | + * |
|
| 157 | + * @return string|void the generated "ul" html if $return is true or display it if not. |
|
| 158 | + */ |
|
| 159 | + public static function ul($data = array(), $attributes = array(), $return = true){ |
|
| 160 | + $data = (array) $data; |
|
| 161 | + $str = null; |
|
| 162 | + $str .= '<ul' . (! empty($attributes['ul']) ? attributes_to_string($attributes['ul']):'') . '>'; |
|
| 163 | + foreach ($data as $row) { |
|
| 164 | + $str .= '<li' . (! empty($attributes['li']) ? attributes_to_string($attributes['li']):'') .'>' .$row. '</li>'; |
|
| 165 | + } |
|
| 166 | + $str .= '</ul>'; |
|
| 167 | + if($return){ |
|
| 168 | + return $str; |
|
| 169 | + } |
|
| 170 | + echo $str; |
|
| 171 | + } |
|
| 172 | 172 | |
| 173 | - /** |
|
| 174 | - * Generate the html "ol" tag |
|
| 175 | - * @param array $data the data to use for each "li" tag |
|
| 176 | - * @param array $attributes the "ol" properties attribute use the array index below for each tag: |
|
| 177 | - * for ol "ol", for li "li". |
|
| 178 | - * @param boolean $return whether need return the generated html or just display it directly |
|
| 179 | - * @return string|void the generated "ol" html if $return is true or display it if not. |
|
| 180 | - */ |
|
| 181 | - public static function ol($data = array(), $attributes = array(), $return = true){ |
|
| 182 | - $data = (array) $data; |
|
| 183 | - $str = null; |
|
| 184 | - $str .= '<ol' . (!empty($attributes['ol']) ? attributes_to_string($attributes['ol']):'') . '>'; |
|
| 185 | - foreach ($data as $row) { |
|
| 186 | - $str .= '<li' . (!empty($attributes['li']) ? attributes_to_string($attributes['li']):'') .'>' .$row. '</li>'; |
|
| 187 | - } |
|
| 188 | - $str .= '</ol>'; |
|
| 189 | - if($return){ |
|
| 190 | - return $str; |
|
| 191 | - } |
|
| 192 | - echo $str; |
|
| 193 | - } |
|
| 173 | + /** |
|
| 174 | + * Generate the html "ol" tag |
|
| 175 | + * @param array $data the data to use for each "li" tag |
|
| 176 | + * @param array $attributes the "ol" properties attribute use the array index below for each tag: |
|
| 177 | + * for ol "ol", for li "li". |
|
| 178 | + * @param boolean $return whether need return the generated html or just display it directly |
|
| 179 | + * @return string|void the generated "ol" html if $return is true or display it if not. |
|
| 180 | + */ |
|
| 181 | + public static function ol($data = array(), $attributes = array(), $return = true){ |
|
| 182 | + $data = (array) $data; |
|
| 183 | + $str = null; |
|
| 184 | + $str .= '<ol' . (!empty($attributes['ol']) ? attributes_to_string($attributes['ol']):'') . '>'; |
|
| 185 | + foreach ($data as $row) { |
|
| 186 | + $str .= '<li' . (!empty($attributes['li']) ? attributes_to_string($attributes['li']):'') .'>' .$row. '</li>'; |
|
| 187 | + } |
|
| 188 | + $str .= '</ol>'; |
|
| 189 | + if($return){ |
|
| 190 | + return $str; |
|
| 191 | + } |
|
| 192 | + echo $str; |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - /** |
|
| 196 | - * Generate the html "table" tag |
|
| 197 | - * @param array $headers the table headers to use between (<thead>) |
|
| 198 | - * @param array $body the table body values between (<tbody>) |
|
| 199 | - * @param array $attributes the table properties attribute use the array index below for each tag: |
|
| 200 | - * for table "table", for thead "thead", for thead tr "thead_tr", |
|
| 201 | - * for thead th "thead_th", for tbody "tbody", for tbody tr "tbody_tr", for tbody td "tbody_td", for tfoot "tfoot", |
|
| 202 | - * for tfoot tr "tfoot_tr", for tfoot th "tfoot_th". |
|
| 203 | - * @param boolean $use_footer whether need to generate table footer (<tfoot>) use the $headers values |
|
| 204 | - * @param boolean $return whether need return the generated html or just display it directly |
|
| 205 | - * @return string|void the generated "table" html if $return is true or display it if not. |
|
| 206 | - */ |
|
| 207 | - public static function table($headers = array(), $body = array(), $attributes = array(), $use_footer = false, $return = true){ |
|
| 208 | - $headers = (array) $headers; |
|
| 209 | - $body = (array) $body; |
|
| 210 | - $str = null; |
|
| 211 | - $str .= '<table' . (! empty($attributes['table']) ? attributes_to_string($attributes['table']):'') . '>'; |
|
| 212 | - if(! empty($headers)){ |
|
| 213 | - $str .= '<thead' . (! empty($attributes['thead']) ? attributes_to_string($attributes['thead']):'') .'>'; |
|
| 214 | - $str .= '<tr' . (! empty($attributes['thead_tr']) ? attributes_to_string($attributes['thead_tr']):'') .'>'; |
|
| 215 | - foreach ($headers as $value) { |
|
| 216 | - $str .= '<th' . (! empty($attributes['thead_th']) ? attributes_to_string($attributes['thead_th']):'') .'>' .$value. '</th>'; |
|
| 217 | - } |
|
| 218 | - $str .= '</tr>'; |
|
| 219 | - $str .= '</thead>'; |
|
| 220 | - } |
|
| 221 | - else{ |
|
| 222 | - //no need check for footer |
|
| 223 | - $use_footer = false; |
|
| 224 | - } |
|
| 225 | - $str .= '<tbody' . (! empty($attributes['tbody']) ? attributes_to_string($attributes['tbody']):'') .'>'; |
|
| 226 | - foreach ($body as $row) { |
|
| 227 | - if(is_array($row)){ |
|
| 228 | - $str .= '<tr' . (! empty($attributes['tbody_tr']) ? attributes_to_string($attributes['tbody_tr']):'') .'>'; |
|
| 229 | - foreach ($row as $value) { |
|
| 230 | - $str .= '<td' . (! empty($attributes['tbody_td']) ? attributes_to_string($attributes['tbody_td']):'') .'>' .$value. '</td>'; |
|
| 231 | - } |
|
| 232 | - $str .= '</tr>'; |
|
| 233 | - } |
|
| 234 | - } |
|
| 235 | - $str .= '</tbody>'; |
|
| 236 | - if($use_footer){ |
|
| 237 | - $str .= '<tfoot' . (! empty($attributes['tfoot']) ? attributes_to_string($attributes['tfoot']):'') .'>'; |
|
| 238 | - $str .= '<tr' . (! empty($attributes['tfoot_tr']) ? attributes_to_string($attributes['tfoot_tr']):'') .'>'; |
|
| 239 | - foreach ($headers as $value) { |
|
| 240 | - $str .= '<th' . (! empty($attributes['tfoot_th']) ? attributes_to_string($attributes['tfoot_th']):'') .'>' .$value. '</th>'; |
|
| 241 | - } |
|
| 242 | - $str .= '</tr>'; |
|
| 243 | - $str .= '</tfoot>'; |
|
| 244 | - } |
|
| 245 | - $str .= '</table>'; |
|
| 246 | - if($return){ |
|
| 247 | - return $str; |
|
| 248 | - } |
|
| 249 | - echo $str; |
|
| 250 | - } |
|
| 251 | - } |
|
| 195 | + /** |
|
| 196 | + * Generate the html "table" tag |
|
| 197 | + * @param array $headers the table headers to use between (<thead>) |
|
| 198 | + * @param array $body the table body values between (<tbody>) |
|
| 199 | + * @param array $attributes the table properties attribute use the array index below for each tag: |
|
| 200 | + * for table "table", for thead "thead", for thead tr "thead_tr", |
|
| 201 | + * for thead th "thead_th", for tbody "tbody", for tbody tr "tbody_tr", for tbody td "tbody_td", for tfoot "tfoot", |
|
| 202 | + * for tfoot tr "tfoot_tr", for tfoot th "tfoot_th". |
|
| 203 | + * @param boolean $use_footer whether need to generate table footer (<tfoot>) use the $headers values |
|
| 204 | + * @param boolean $return whether need return the generated html or just display it directly |
|
| 205 | + * @return string|void the generated "table" html if $return is true or display it if not. |
|
| 206 | + */ |
|
| 207 | + public static function table($headers = array(), $body = array(), $attributes = array(), $use_footer = false, $return = true){ |
|
| 208 | + $headers = (array) $headers; |
|
| 209 | + $body = (array) $body; |
|
| 210 | + $str = null; |
|
| 211 | + $str .= '<table' . (! empty($attributes['table']) ? attributes_to_string($attributes['table']):'') . '>'; |
|
| 212 | + if(! empty($headers)){ |
|
| 213 | + $str .= '<thead' . (! empty($attributes['thead']) ? attributes_to_string($attributes['thead']):'') .'>'; |
|
| 214 | + $str .= '<tr' . (! empty($attributes['thead_tr']) ? attributes_to_string($attributes['thead_tr']):'') .'>'; |
|
| 215 | + foreach ($headers as $value) { |
|
| 216 | + $str .= '<th' . (! empty($attributes['thead_th']) ? attributes_to_string($attributes['thead_th']):'') .'>' .$value. '</th>'; |
|
| 217 | + } |
|
| 218 | + $str .= '</tr>'; |
|
| 219 | + $str .= '</thead>'; |
|
| 220 | + } |
|
| 221 | + else{ |
|
| 222 | + //no need check for footer |
|
| 223 | + $use_footer = false; |
|
| 224 | + } |
|
| 225 | + $str .= '<tbody' . (! empty($attributes['tbody']) ? attributes_to_string($attributes['tbody']):'') .'>'; |
|
| 226 | + foreach ($body as $row) { |
|
| 227 | + if(is_array($row)){ |
|
| 228 | + $str .= '<tr' . (! empty($attributes['tbody_tr']) ? attributes_to_string($attributes['tbody_tr']):'') .'>'; |
|
| 229 | + foreach ($row as $value) { |
|
| 230 | + $str .= '<td' . (! empty($attributes['tbody_td']) ? attributes_to_string($attributes['tbody_td']):'') .'>' .$value. '</td>'; |
|
| 231 | + } |
|
| 232 | + $str .= '</tr>'; |
|
| 233 | + } |
|
| 234 | + } |
|
| 235 | + $str .= '</tbody>'; |
|
| 236 | + if($use_footer){ |
|
| 237 | + $str .= '<tfoot' . (! empty($attributes['tfoot']) ? attributes_to_string($attributes['tfoot']):'') .'>'; |
|
| 238 | + $str .= '<tr' . (! empty($attributes['tfoot_tr']) ? attributes_to_string($attributes['tfoot_tr']):'') .'>'; |
|
| 239 | + foreach ($headers as $value) { |
|
| 240 | + $str .= '<th' . (! empty($attributes['tfoot_th']) ? attributes_to_string($attributes['tfoot_th']):'') .'>' .$value. '</th>'; |
|
| 241 | + } |
|
| 242 | + $str .= '</tr>'; |
|
| 243 | + $str .= '</tfoot>'; |
|
| 244 | + } |
|
| 245 | + $str .= '</table>'; |
|
| 246 | + if($return){ |
|
| 247 | + return $str; |
|
| 248 | + } |
|
| 249 | + echo $str; |
|
| 250 | + } |
|
| 251 | + } |
|
@@ -1,330 +1,330 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | 27 | |
| 28 | - class Form{ |
|
| 28 | + class Form{ |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Generate the form opened tag |
|
| 32 | - * @param string $path the form action path |
|
| 33 | - * @param array $attributes the additional form attributes |
|
| 34 | - * @param string $method the form method like 'GET', 'POST' |
|
| 35 | - * @param string $enctype the form enctype like "multipart/form-data" |
|
| 36 | - * @return string the generated form html |
|
| 37 | - */ |
|
| 38 | - public static function open($path = null, array $attributes = array(), $method = 'POST', $enctype = null){ |
|
| 39 | - if($path){ |
|
| 40 | - $path = Url::site_url($path); |
|
| 41 | - } |
|
| 42 | - $method = strtoupper($method); |
|
| 43 | - $str = null; |
|
| 44 | - $str .= '<form action = "'.$path.'" method = "'.$method.'"'; |
|
| 45 | - if(! empty($enctype)){ |
|
| 46 | - $str .= ' enctype = "'.$enctype.'" '; |
|
| 47 | - } |
|
| 48 | - if(! isset($attributes['accept-charset'])){ |
|
| 49 | - $attributes['accept-charset'] = get_config('charset', 'utf-8'); |
|
| 50 | - } |
|
| 51 | - $str .= attributes_to_string($attributes); |
|
| 52 | - $str .= '>'; |
|
| 53 | - $checkCsrf = false; |
|
| 54 | - //check if the user set the checking of CSRF manually |
|
| 55 | - if($method != 'POST' && isset($attributes['csrf'])){ |
|
| 56 | - $obj = & get_instance(); |
|
| 57 | - if(! isset($obj->formvalidation)){ |
|
| 58 | - Loader::library('FormValidation'); |
|
| 59 | - } |
|
| 60 | - $obj->formvalidation->enableCsrfCheck = true; |
|
| 61 | - $checkCsrf = true; |
|
| 62 | - } |
|
| 63 | - //if CSRF enable or is set manually |
|
| 64 | - if((get_config('csrf_enable', false) && $method == 'POST') || ($method != 'POST' && $checkCsrf)){ |
|
| 65 | - $csrfValue = Security::generateCSRF(); |
|
| 66 | - $csrfName = get_config('csrf_key', 'csrf_key'); |
|
| 67 | - $str .= static::hidden($csrfName, $csrfValue); |
|
| 68 | - } |
|
| 69 | - return $str; |
|
| 70 | - } |
|
| 30 | + /** |
|
| 31 | + * Generate the form opened tag |
|
| 32 | + * @param string $path the form action path |
|
| 33 | + * @param array $attributes the additional form attributes |
|
| 34 | + * @param string $method the form method like 'GET', 'POST' |
|
| 35 | + * @param string $enctype the form enctype like "multipart/form-data" |
|
| 36 | + * @return string the generated form html |
|
| 37 | + */ |
|
| 38 | + public static function open($path = null, array $attributes = array(), $method = 'POST', $enctype = null){ |
|
| 39 | + if($path){ |
|
| 40 | + $path = Url::site_url($path); |
|
| 41 | + } |
|
| 42 | + $method = strtoupper($method); |
|
| 43 | + $str = null; |
|
| 44 | + $str .= '<form action = "'.$path.'" method = "'.$method.'"'; |
|
| 45 | + if(! empty($enctype)){ |
|
| 46 | + $str .= ' enctype = "'.$enctype.'" '; |
|
| 47 | + } |
|
| 48 | + if(! isset($attributes['accept-charset'])){ |
|
| 49 | + $attributes['accept-charset'] = get_config('charset', 'utf-8'); |
|
| 50 | + } |
|
| 51 | + $str .= attributes_to_string($attributes); |
|
| 52 | + $str .= '>'; |
|
| 53 | + $checkCsrf = false; |
|
| 54 | + //check if the user set the checking of CSRF manually |
|
| 55 | + if($method != 'POST' && isset($attributes['csrf'])){ |
|
| 56 | + $obj = & get_instance(); |
|
| 57 | + if(! isset($obj->formvalidation)){ |
|
| 58 | + Loader::library('FormValidation'); |
|
| 59 | + } |
|
| 60 | + $obj->formvalidation->enableCsrfCheck = true; |
|
| 61 | + $checkCsrf = true; |
|
| 62 | + } |
|
| 63 | + //if CSRF enable or is set manually |
|
| 64 | + if((get_config('csrf_enable', false) && $method == 'POST') || ($method != 'POST' && $checkCsrf)){ |
|
| 65 | + $csrfValue = Security::generateCSRF(); |
|
| 66 | + $csrfName = get_config('csrf_key', 'csrf_key'); |
|
| 67 | + $str .= static::hidden($csrfName, $csrfValue); |
|
| 68 | + } |
|
| 69 | + return $str; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Generate the form opened tag for multipart like to send a file |
|
| 74 | - * @see Form::open() for more details |
|
| 75 | - * @return string the generated multipart form html |
|
| 76 | - */ |
|
| 77 | - public static function openMultipart($path = null, array $attributes = array(), $method = 'POST'){ |
|
| 78 | - return self::open($path, $attributes, $method, 'multipart/form-data'); |
|
| 79 | - } |
|
| 72 | + /** |
|
| 73 | + * Generate the form opened tag for multipart like to send a file |
|
| 74 | + * @see Form::open() for more details |
|
| 75 | + * @return string the generated multipart form html |
|
| 76 | + */ |
|
| 77 | + public static function openMultipart($path = null, array $attributes = array(), $method = 'POST'){ |
|
| 78 | + return self::open($path, $attributes, $method, 'multipart/form-data'); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * Generate the form close |
|
| 83 | - * @return string the form close html |
|
| 84 | - */ |
|
| 85 | - public static function close(){ |
|
| 86 | - return '</form>'; |
|
| 87 | - } |
|
| 81 | + /** |
|
| 82 | + * Generate the form close |
|
| 83 | + * @return string the form close html |
|
| 84 | + */ |
|
| 85 | + public static function close(){ |
|
| 86 | + return '</form>'; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Generate the form fieldset & legend |
|
| 91 | - * @param string $legend the legend tag value |
|
| 92 | - * @param array $fieldsetAttributes the fieldset additional HTML attributes |
|
| 93 | - * @param array $legendAttributes the legend additional HTML attributes. Is used only is $legend is not empty |
|
| 94 | - * @return string the generated fieldset value |
|
| 95 | - */ |
|
| 96 | - public static function fieldset($legend = '', array $fieldsetAttributes = array(), array $legendAttributes = array()){ |
|
| 97 | - $str = '<fieldset' . attributes_to_string($fieldsetAttributes) . '>'; |
|
| 98 | - if($legend){ |
|
| 99 | - $str .= '<legend' . attributes_to_string($legendAttributes) . '>'.$legend.'</legend>'; |
|
| 100 | - } |
|
| 101 | - return $str; |
|
| 102 | - } |
|
| 89 | + /** |
|
| 90 | + * Generate the form fieldset & legend |
|
| 91 | + * @param string $legend the legend tag value |
|
| 92 | + * @param array $fieldsetAttributes the fieldset additional HTML attributes |
|
| 93 | + * @param array $legendAttributes the legend additional HTML attributes. Is used only is $legend is not empty |
|
| 94 | + * @return string the generated fieldset value |
|
| 95 | + */ |
|
| 96 | + public static function fieldset($legend = '', array $fieldsetAttributes = array(), array $legendAttributes = array()){ |
|
| 97 | + $str = '<fieldset' . attributes_to_string($fieldsetAttributes) . '>'; |
|
| 98 | + if($legend){ |
|
| 99 | + $str .= '<legend' . attributes_to_string($legendAttributes) . '>'.$legend.'</legend>'; |
|
| 100 | + } |
|
| 101 | + return $str; |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * Generate the fieldset close tag |
|
| 106 | - * @return string the generated html for fieldset close |
|
| 107 | - */ |
|
| 108 | - public static function fieldsetClose(){ |
|
| 109 | - return '</fieldset>'; |
|
| 110 | - } |
|
| 104 | + /** |
|
| 105 | + * Generate the fieldset close tag |
|
| 106 | + * @return string the generated html for fieldset close |
|
| 107 | + */ |
|
| 108 | + public static function fieldsetClose(){ |
|
| 109 | + return '</fieldset>'; |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * Get the error message for the given form field name. |
|
| 114 | - * This use the form validation information to get the error information. |
|
| 115 | - * @param string $name the form field name |
|
| 116 | - * @return string the error message if exists and null if not |
|
| 117 | - */ |
|
| 118 | - public static function error($name){ |
|
| 119 | - $return = null; |
|
| 120 | - $obj = & get_instance(); |
|
| 121 | - if(isset($obj->formvalidation)){ |
|
| 122 | - $errors = $obj->formvalidation->returnErrors(); |
|
| 123 | - $error = isset($errors[$name]) ? $errors[$name] : null; |
|
| 124 | - if($error){ |
|
| 125 | - list($errorStart, $errorEnd) = $obj->formvalidation->getErrorDelimiter(); |
|
| 126 | - $return = $errorStart . $error . $errorEnd; |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - return $return; |
|
| 130 | - } |
|
| 112 | + /** |
|
| 113 | + * Get the error message for the given form field name. |
|
| 114 | + * This use the form validation information to get the error information. |
|
| 115 | + * @param string $name the form field name |
|
| 116 | + * @return string the error message if exists and null if not |
|
| 117 | + */ |
|
| 118 | + public static function error($name){ |
|
| 119 | + $return = null; |
|
| 120 | + $obj = & get_instance(); |
|
| 121 | + if(isset($obj->formvalidation)){ |
|
| 122 | + $errors = $obj->formvalidation->returnErrors(); |
|
| 123 | + $error = isset($errors[$name]) ? $errors[$name] : null; |
|
| 124 | + if($error){ |
|
| 125 | + list($errorStart, $errorEnd) = $obj->formvalidation->getErrorDelimiter(); |
|
| 126 | + $return = $errorStart . $error . $errorEnd; |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + return $return; |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - /** |
|
| 133 | - * Get the form field value |
|
| 134 | - * @param string $name the form field name |
|
| 135 | - * @param mixed $default the default value if can not found the given form field name |
|
| 136 | - * @return mixed the form field value if is set, otherwise return the default value. |
|
| 137 | - */ |
|
| 138 | - public static function value($name, $default = null){ |
|
| 139 | - $value = get_instance()->request->query($name); |
|
| 140 | - return $value ? $value : $default; |
|
| 141 | - } |
|
| 132 | + /** |
|
| 133 | + * Get the form field value |
|
| 134 | + * @param string $name the form field name |
|
| 135 | + * @param mixed $default the default value if can not found the given form field name |
|
| 136 | + * @return mixed the form field value if is set, otherwise return the default value. |
|
| 137 | + */ |
|
| 138 | + public static function value($name, $default = null){ |
|
| 139 | + $value = get_instance()->request->query($name); |
|
| 140 | + return $value ? $value : $default; |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - /** |
|
| 144 | - * Generate the form label html content |
|
| 145 | - * @param string $label the title of the label |
|
| 146 | - * @param string $for the value of the label "for" attribute |
|
| 147 | - * @param array $attributes the additional attributes to be added |
|
| 148 | - * @return string the generated label html content |
|
| 149 | - */ |
|
| 150 | - public static function label($label, $for = '', array $attributes = array()){ |
|
| 151 | - $str = '<label'; |
|
| 152 | - if($for){ |
|
| 153 | - $str .= ' for = "'.$for.'"'; |
|
| 154 | - } |
|
| 155 | - $str .= attributes_to_string($attributes); |
|
| 156 | - $str .= '>'; |
|
| 157 | - $str .= $label.'</label>'; |
|
| 158 | - return $str; |
|
| 159 | - } |
|
| 143 | + /** |
|
| 144 | + * Generate the form label html content |
|
| 145 | + * @param string $label the title of the label |
|
| 146 | + * @param string $for the value of the label "for" attribute |
|
| 147 | + * @param array $attributes the additional attributes to be added |
|
| 148 | + * @return string the generated label html content |
|
| 149 | + */ |
|
| 150 | + public static function label($label, $for = '', array $attributes = array()){ |
|
| 151 | + $str = '<label'; |
|
| 152 | + if($for){ |
|
| 153 | + $str .= ' for = "'.$for.'"'; |
|
| 154 | + } |
|
| 155 | + $str .= attributes_to_string($attributes); |
|
| 156 | + $str .= '>'; |
|
| 157 | + $str .= $label.'</label>'; |
|
| 158 | + return $str; |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | - /** |
|
| 162 | - * Generate the form field for input like "text", "email", "password", etc. |
|
| 163 | - * @param string $name the form field name |
|
| 164 | - * @param mixed $value the form field value to be set |
|
| 165 | - * @param array $attributes the additional attributes to be added in the form input |
|
| 166 | - * @param string $type the type of the form field (password, text, submit, button, etc.) |
|
| 167 | - * @return string the generated form field html content for the input |
|
| 168 | - */ |
|
| 169 | - public static function input($name, $value = null, array $attributes = array(), $type = 'text'){ |
|
| 170 | - $str = null; |
|
| 171 | - $str .= '<input name = "'.$name.'" value = "'.$value.'" type = "'.$type.'"'; |
|
| 172 | - $str .= attributes_to_string($attributes); |
|
| 173 | - $str .= '/>'; |
|
| 174 | - return $str; |
|
| 175 | - } |
|
| 161 | + /** |
|
| 162 | + * Generate the form field for input like "text", "email", "password", etc. |
|
| 163 | + * @param string $name the form field name |
|
| 164 | + * @param mixed $value the form field value to be set |
|
| 165 | + * @param array $attributes the additional attributes to be added in the form input |
|
| 166 | + * @param string $type the type of the form field (password, text, submit, button, etc.) |
|
| 167 | + * @return string the generated form field html content for the input |
|
| 168 | + */ |
|
| 169 | + public static function input($name, $value = null, array $attributes = array(), $type = 'text'){ |
|
| 170 | + $str = null; |
|
| 171 | + $str .= '<input name = "'.$name.'" value = "'.$value.'" type = "'.$type.'"'; |
|
| 172 | + $str .= attributes_to_string($attributes); |
|
| 173 | + $str .= '/>'; |
|
| 174 | + return $str; |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - /** |
|
| 178 | - * Generate the form field for "text" |
|
| 179 | - * @see Form::input() for more details |
|
| 180 | - */ |
|
| 181 | - public static function text($name, $value = null, array $attributes = array()){ |
|
| 182 | - return self::input($name, $value, $attributes, 'text'); |
|
| 183 | - } |
|
| 177 | + /** |
|
| 178 | + * Generate the form field for "text" |
|
| 179 | + * @see Form::input() for more details |
|
| 180 | + */ |
|
| 181 | + public static function text($name, $value = null, array $attributes = array()){ |
|
| 182 | + return self::input($name, $value, $attributes, 'text'); |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - /** |
|
| 186 | - * Generate the form field for "password" |
|
| 187 | - * @see Form::input() for more details |
|
| 188 | - */ |
|
| 189 | - public static function password($name, $value = null, array $attributes = array()){ |
|
| 190 | - return self::input($name, $value, $attributes, 'password'); |
|
| 191 | - } |
|
| 185 | + /** |
|
| 186 | + * Generate the form field for "password" |
|
| 187 | + * @see Form::input() for more details |
|
| 188 | + */ |
|
| 189 | + public static function password($name, $value = null, array $attributes = array()){ |
|
| 190 | + return self::input($name, $value, $attributes, 'password'); |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | - /** |
|
| 194 | - * Generate the form field for "radio" |
|
| 195 | - * @see Form::input() for more details |
|
| 196 | - */ |
|
| 197 | - public static function radio($name, $value = null, $checked = false, array $attributes = array()){ |
|
| 198 | - if($checked){ |
|
| 199 | - $attributes['checked'] = true; |
|
| 200 | - } |
|
| 201 | - return self::input($name, $value, $attributes, 'radio'); |
|
| 202 | - } |
|
| 193 | + /** |
|
| 194 | + * Generate the form field for "radio" |
|
| 195 | + * @see Form::input() for more details |
|
| 196 | + */ |
|
| 197 | + public static function radio($name, $value = null, $checked = false, array $attributes = array()){ |
|
| 198 | + if($checked){ |
|
| 199 | + $attributes['checked'] = true; |
|
| 200 | + } |
|
| 201 | + return self::input($name, $value, $attributes, 'radio'); |
|
| 202 | + } |
|
| 203 | 203 | |
| 204 | - /** |
|
| 205 | - * Generate the form field for "checkbox" |
|
| 206 | - * @see Form::input() for more details |
|
| 207 | - */ |
|
| 208 | - public static function checkbox($name, $value = null, $checked = false, array $attributes = array()){ |
|
| 209 | - if($checked){ |
|
| 210 | - $attributes['checked'] = true; |
|
| 211 | - } |
|
| 212 | - return self::input($name, $value, $attributes, 'checkbox'); |
|
| 213 | - } |
|
| 204 | + /** |
|
| 205 | + * Generate the form field for "checkbox" |
|
| 206 | + * @see Form::input() for more details |
|
| 207 | + */ |
|
| 208 | + public static function checkbox($name, $value = null, $checked = false, array $attributes = array()){ |
|
| 209 | + if($checked){ |
|
| 210 | + $attributes['checked'] = true; |
|
| 211 | + } |
|
| 212 | + return self::input($name, $value, $attributes, 'checkbox'); |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | - /** |
|
| 216 | - * Generate the form field for "number" |
|
| 217 | - * @see Form::input() for more details |
|
| 218 | - */ |
|
| 219 | - public static function number($name, $value = null, array $attributes = array()){ |
|
| 220 | - return self::input($name, $value, $attributes, 'number'); |
|
| 221 | - } |
|
| 215 | + /** |
|
| 216 | + * Generate the form field for "number" |
|
| 217 | + * @see Form::input() for more details |
|
| 218 | + */ |
|
| 219 | + public static function number($name, $value = null, array $attributes = array()){ |
|
| 220 | + return self::input($name, $value, $attributes, 'number'); |
|
| 221 | + } |
|
| 222 | 222 | |
| 223 | - /** |
|
| 224 | - * Generate the form field for "phone" |
|
| 225 | - * @see Form::input() for more details |
|
| 226 | - */ |
|
| 227 | - public static function phone($name, $value = null, array $attributes = array()){ |
|
| 228 | - return self::input($name, $value, $attributes, 'phone'); |
|
| 229 | - } |
|
| 223 | + /** |
|
| 224 | + * Generate the form field for "phone" |
|
| 225 | + * @see Form::input() for more details |
|
| 226 | + */ |
|
| 227 | + public static function phone($name, $value = null, array $attributes = array()){ |
|
| 228 | + return self::input($name, $value, $attributes, 'phone'); |
|
| 229 | + } |
|
| 230 | 230 | |
| 231 | - /** |
|
| 232 | - * Generate the form field for "email" |
|
| 233 | - * @see Form::input() for more details |
|
| 234 | - */ |
|
| 235 | - public static function email($name, $value = null, array $attributes = array()){ |
|
| 236 | - return self::input($name, $value, $attributes, 'email'); |
|
| 237 | - } |
|
| 231 | + /** |
|
| 232 | + * Generate the form field for "email" |
|
| 233 | + * @see Form::input() for more details |
|
| 234 | + */ |
|
| 235 | + public static function email($name, $value = null, array $attributes = array()){ |
|
| 236 | + return self::input($name, $value, $attributes, 'email'); |
|
| 237 | + } |
|
| 238 | 238 | |
| 239 | - /** |
|
| 240 | - * Generate the form field for "search" |
|
| 241 | - * @see Form::input() for more details |
|
| 242 | - */ |
|
| 243 | - public static function search($name, $value = null, array $attributes = array()){ |
|
| 244 | - return self::input($name, $value, $attributes, 'search'); |
|
| 245 | - } |
|
| 239 | + /** |
|
| 240 | + * Generate the form field for "search" |
|
| 241 | + * @see Form::input() for more details |
|
| 242 | + */ |
|
| 243 | + public static function search($name, $value = null, array $attributes = array()){ |
|
| 244 | + return self::input($name, $value, $attributes, 'search'); |
|
| 245 | + } |
|
| 246 | 246 | |
| 247 | - /** |
|
| 248 | - * Generate the form field for "hidden" |
|
| 249 | - * @see Form::input() for more details |
|
| 250 | - */ |
|
| 251 | - public static function hidden($name, $value = null, array $attributes = array()){ |
|
| 252 | - return self::input($name, $value, $attributes, 'hidden'); |
|
| 253 | - } |
|
| 247 | + /** |
|
| 248 | + * Generate the form field for "hidden" |
|
| 249 | + * @see Form::input() for more details |
|
| 250 | + */ |
|
| 251 | + public static function hidden($name, $value = null, array $attributes = array()){ |
|
| 252 | + return self::input($name, $value, $attributes, 'hidden'); |
|
| 253 | + } |
|
| 254 | 254 | |
| 255 | - /** |
|
| 256 | - * Generate the form field for "file" |
|
| 257 | - * @see Form::input() for more details |
|
| 258 | - */ |
|
| 259 | - public static function file($name, array $attributes = array()){ |
|
| 260 | - return self::input($name, null, $attributes, 'file'); |
|
| 261 | - } |
|
| 255 | + /** |
|
| 256 | + * Generate the form field for "file" |
|
| 257 | + * @see Form::input() for more details |
|
| 258 | + */ |
|
| 259 | + public static function file($name, array $attributes = array()){ |
|
| 260 | + return self::input($name, null, $attributes, 'file'); |
|
| 261 | + } |
|
| 262 | 262 | |
| 263 | - /** |
|
| 264 | - * Generate the form field for "button" |
|
| 265 | - * @see Form::input() for more details |
|
| 266 | - */ |
|
| 267 | - public static function button($name, $value = null, array $attributes = array()){ |
|
| 268 | - return self::input($name, $value, $attributes, 'button'); |
|
| 269 | - } |
|
| 263 | + /** |
|
| 264 | + * Generate the form field for "button" |
|
| 265 | + * @see Form::input() for more details |
|
| 266 | + */ |
|
| 267 | + public static function button($name, $value = null, array $attributes = array()){ |
|
| 268 | + return self::input($name, $value, $attributes, 'button'); |
|
| 269 | + } |
|
| 270 | 270 | |
| 271 | - /** |
|
| 272 | - * Generate the form field for "reset" |
|
| 273 | - * @see Form::input() for more details |
|
| 274 | - */ |
|
| 275 | - public static function reset($name, $value = null, array $attributes = array()){ |
|
| 276 | - return self::input($name, $value, $attributes, 'reset'); |
|
| 277 | - } |
|
| 271 | + /** |
|
| 272 | + * Generate the form field for "reset" |
|
| 273 | + * @see Form::input() for more details |
|
| 274 | + */ |
|
| 275 | + public static function reset($name, $value = null, array $attributes = array()){ |
|
| 276 | + return self::input($name, $value, $attributes, 'reset'); |
|
| 277 | + } |
|
| 278 | 278 | |
| 279 | - /** |
|
| 280 | - * Generate the form field for "submit" |
|
| 281 | - * @see Form::input() for more details |
|
| 282 | - */ |
|
| 283 | - public static function submit($name, $value = null, array $attributes = array()){ |
|
| 284 | - return self::input($name, $value, $attributes, 'submit'); |
|
| 285 | - } |
|
| 279 | + /** |
|
| 280 | + * Generate the form field for "submit" |
|
| 281 | + * @see Form::input() for more details |
|
| 282 | + */ |
|
| 283 | + public static function submit($name, $value = null, array $attributes = array()){ |
|
| 284 | + return self::input($name, $value, $attributes, 'submit'); |
|
| 285 | + } |
|
| 286 | 286 | |
| 287 | - /** |
|
| 288 | - * Generate the form field for textarea |
|
| 289 | - * @param string $name the name of the textarea field |
|
| 290 | - * @param string $value the textarea field value |
|
| 291 | - * @param array $attributes the additional attributes to be added |
|
| 292 | - * @return string the generated textarea form html content |
|
| 293 | - */ |
|
| 294 | - public static function textarea($name, $value = '', array $attributes = array()){ |
|
| 295 | - $str = null; |
|
| 296 | - $str .= '<textarea name = "'.$name.'"'; |
|
| 297 | - $str .= attributes_to_string($attributes); |
|
| 298 | - $str .= '>'; |
|
| 299 | - $str .= $value.'</textarea>'; |
|
| 300 | - return $str; |
|
| 301 | - } |
|
| 287 | + /** |
|
| 288 | + * Generate the form field for textarea |
|
| 289 | + * @param string $name the name of the textarea field |
|
| 290 | + * @param string $value the textarea field value |
|
| 291 | + * @param array $attributes the additional attributes to be added |
|
| 292 | + * @return string the generated textarea form html content |
|
| 293 | + */ |
|
| 294 | + public static function textarea($name, $value = '', array $attributes = array()){ |
|
| 295 | + $str = null; |
|
| 296 | + $str .= '<textarea name = "'.$name.'"'; |
|
| 297 | + $str .= attributes_to_string($attributes); |
|
| 298 | + $str .= '>'; |
|
| 299 | + $str .= $value.'</textarea>'; |
|
| 300 | + return $str; |
|
| 301 | + } |
|
| 302 | 302 | |
| 303 | - /** |
|
| 304 | - * Generate the form field for select |
|
| 305 | - * @param string $name the name of the form field |
|
| 306 | - * @param mixed|array $values the values used to populate the "option" tags |
|
| 307 | - * @param mixed $selected the selected value in the option list |
|
| 308 | - * @param array $attributes the additional attribute to be added |
|
| 309 | - * @return string the generated form field html content for select |
|
| 310 | - */ |
|
| 311 | - public static function select($name, $values = null, $selected = null, array $attributes = array()){ |
|
| 312 | - if(! is_array($values)){ |
|
| 313 | - $values = array('' => $values); |
|
| 314 | - } |
|
| 315 | - $str = null; |
|
| 316 | - $str .= '<select name = "'.$name.'"'; |
|
| 317 | - $str .= attributes_to_string($attributes); |
|
| 318 | - $str .= '>'; |
|
| 319 | - foreach($values as $key => $val){ |
|
| 320 | - $select = ''; |
|
| 321 | - if($key == $selected){ |
|
| 322 | - $select = 'selected'; |
|
| 323 | - } |
|
| 324 | - $str .= '<option value = "'.$key.'" '.$select.'>'.$val.'</option>'; |
|
| 325 | - } |
|
| 326 | - $str .= '</select>'; |
|
| 327 | - return $str; |
|
| 328 | - } |
|
| 303 | + /** |
|
| 304 | + * Generate the form field for select |
|
| 305 | + * @param string $name the name of the form field |
|
| 306 | + * @param mixed|array $values the values used to populate the "option" tags |
|
| 307 | + * @param mixed $selected the selected value in the option list |
|
| 308 | + * @param array $attributes the additional attribute to be added |
|
| 309 | + * @return string the generated form field html content for select |
|
| 310 | + */ |
|
| 311 | + public static function select($name, $values = null, $selected = null, array $attributes = array()){ |
|
| 312 | + if(! is_array($values)){ |
|
| 313 | + $values = array('' => $values); |
|
| 314 | + } |
|
| 315 | + $str = null; |
|
| 316 | + $str .= '<select name = "'.$name.'"'; |
|
| 317 | + $str .= attributes_to_string($attributes); |
|
| 318 | + $str .= '>'; |
|
| 319 | + foreach($values as $key => $val){ |
|
| 320 | + $select = ''; |
|
| 321 | + if($key == $selected){ |
|
| 322 | + $select = 'selected'; |
|
| 323 | + } |
|
| 324 | + $str .= '<option value = "'.$key.'" '.$select.'>'.$val.'</option>'; |
|
| 325 | + } |
|
| 326 | + $str .= '</select>'; |
|
| 327 | + return $str; |
|
| 328 | + } |
|
| 329 | 329 | |
| 330 | - } |
|
| 330 | + } |
|
@@ -1,195 +1,195 @@ |
||
| 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 bootstrap.php |
|
| 29 | - * |
|
| 30 | - * Contains the loading process: loading of constants, common functions, libraries |
|
| 31 | - * configurations, etc |
|
| 32 | - * verification of the environment and the routing of the request. |
|
| 33 | - * |
|
| 34 | - * @package core |
|
| 35 | - * @author Tony NGUEREZA |
|
| 36 | - * @copyright Copyright (c) 2017 |
|
| 37 | - * @license https://opensource.org/licenses/gpl-3.0.html GNU GPL License (GPL) |
|
| 38 | - * @link http://www.iacademy.cf |
|
| 39 | - * @version 1.0.0 |
|
| 40 | - * @filesource |
|
| 41 | - */ |
|
| 27 | + /** |
|
| 28 | + * @file bootstrap.php |
|
| 29 | + * |
|
| 30 | + * Contains the loading process: loading of constants, common functions, libraries |
|
| 31 | + * configurations, etc |
|
| 32 | + * verification of the environment and the routing of the request. |
|
| 33 | + * |
|
| 34 | + * @package core |
|
| 35 | + * @author Tony NGUEREZA |
|
| 36 | + * @copyright Copyright (c) 2017 |
|
| 37 | + * @license https://opensource.org/licenses/gpl-3.0.html GNU GPL License (GPL) |
|
| 38 | + * @link http://www.iacademy.cf |
|
| 39 | + * @version 1.0.0 |
|
| 40 | + * @filesource |
|
| 41 | + */ |
|
| 42 | 42 | |
| 43 | - //if the application is running in CLI mode $_SESSION global variable is not available |
|
| 44 | - if(IS_CLI){ |
|
| 45 | - $_SESSION = array(); |
|
| 46 | - } |
|
| 43 | + //if the application is running in CLI mode $_SESSION global variable is not available |
|
| 44 | + if(IS_CLI){ |
|
| 45 | + $_SESSION = array(); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * inclusion of global constants of the environment that contain : name of the framework, |
|
| 50 | - * version, release date, version of PHP required, etc. |
|
| 51 | - */ |
|
| 52 | - require_once CORE_PATH . 'constants.php'; |
|
| 48 | + /** |
|
| 49 | + * inclusion of global constants of the environment that contain : name of the framework, |
|
| 50 | + * version, release date, version of PHP required, etc. |
|
| 51 | + */ |
|
| 52 | + require_once CORE_PATH . 'constants.php'; |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * include file containing commons functions used in the framework such: show_error, |
|
| 56 | - * php_exception_handler, php_error_handler, get_instance, etc. |
|
| 57 | - */ |
|
| 58 | - require_once CORE_PATH . 'common.php'; |
|
| 54 | + /** |
|
| 55 | + * include file containing commons functions used in the framework such: show_error, |
|
| 56 | + * php_exception_handler, php_error_handler, get_instance, etc. |
|
| 57 | + */ |
|
| 58 | + require_once CORE_PATH . 'common.php'; |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * The Benchmark class |
|
| 62 | - */ |
|
| 63 | - $BENCHMARK =& class_loader('Benchmark'); |
|
| 60 | + /** |
|
| 61 | + * The Benchmark class |
|
| 62 | + */ |
|
| 63 | + $BENCHMARK =& class_loader('Benchmark'); |
|
| 64 | 64 | |
| 65 | - $BENCHMARK->mark('APP_EXECUTION_START'); |
|
| 65 | + $BENCHMARK->mark('APP_EXECUTION_START'); |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * instance of the Log class |
|
| 69 | - */ |
|
| 67 | + /** |
|
| 68 | + * instance of the Log class |
|
| 69 | + */ |
|
| 70 | 70 | $LOGGER =& class_loader('Log', 'classes'); |
| 71 | 71 | |
| 72 | 72 | $LOGGER->setLogger('ApplicationBootstrap'); |
| 73 | 73 | |
| 74 | 74 | $LOGGER->debug('Checking PHP version ...'); |
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * Verification of the PHP environment: minimum and maximum version |
|
| 78 | - */ |
|
| 79 | - if (version_compare(phpversion(), TNH_REQUIRED_PHP_MIN_VERSION, '<')){ |
|
| 80 | - show_error('Your PHP Version [' . phpversion() . '] is less than [' . TNH_REQUIRED_PHP_MIN_VERSION . '], please install a new version or update your PHP to the latest.', 'PHP Error environment'); |
|
| 81 | - } |
|
| 82 | - else if(version_compare(phpversion(), TNH_REQUIRED_PHP_MAX_VERSION, '>')){ |
|
| 83 | - show_error('Your PHP Version [' . phpversion() . '] is greather than [' . TNH_REQUIRED_PHP_MAX_VERSION . '] please install a PHP version that is compatible.', 'PHP Error environment'); |
|
| 84 | - } |
|
| 85 | - $LOGGER->info('PHP version [' . phpversion() . '] is OK [REQUIRED MINIMUM: ' . TNH_REQUIRED_PHP_MIN_VERSION . ', REQUIRED MAXIMUM: ' . TNH_REQUIRED_PHP_MAX_VERSION . '], application can work without any issue'); |
|
| 76 | + /** |
|
| 77 | + * Verification of the PHP environment: minimum and maximum version |
|
| 78 | + */ |
|
| 79 | + if (version_compare(phpversion(), TNH_REQUIRED_PHP_MIN_VERSION, '<')){ |
|
| 80 | + show_error('Your PHP Version [' . phpversion() . '] is less than [' . TNH_REQUIRED_PHP_MIN_VERSION . '], please install a new version or update your PHP to the latest.', 'PHP Error environment'); |
|
| 81 | + } |
|
| 82 | + else if(version_compare(phpversion(), TNH_REQUIRED_PHP_MAX_VERSION, '>')){ |
|
| 83 | + show_error('Your PHP Version [' . phpversion() . '] is greather than [' . TNH_REQUIRED_PHP_MAX_VERSION . '] please install a PHP version that is compatible.', 'PHP Error environment'); |
|
| 84 | + } |
|
| 85 | + $LOGGER->info('PHP version [' . phpversion() . '] is OK [REQUIRED MINIMUM: ' . TNH_REQUIRED_PHP_MIN_VERSION . ', REQUIRED MAXIMUM: ' . TNH_REQUIRED_PHP_MAX_VERSION . '], application can work without any issue'); |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * Setting of the PHP error message handling function |
|
| 89 | - */ |
|
| 90 | - set_error_handler('php_error_handler'); |
|
| 87 | + /** |
|
| 88 | + * Setting of the PHP error message handling function |
|
| 89 | + */ |
|
| 90 | + set_error_handler('php_error_handler'); |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Setting of the PHP error exception handling function |
|
| 94 | - */ |
|
| 95 | - set_exception_handler('php_exception_handler'); |
|
| 92 | + /** |
|
| 93 | + * Setting of the PHP error exception handling function |
|
| 94 | + */ |
|
| 95 | + set_exception_handler('php_exception_handler'); |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * Setting of the PHP shutdown handling function |
|
| 99 | - */ |
|
| 100 | - register_shutdown_function('php_shudown_handler'); |
|
| 97 | + /** |
|
| 98 | + * Setting of the PHP shutdown handling function |
|
| 99 | + */ |
|
| 100 | + register_shutdown_function('php_shudown_handler'); |
|
| 101 | 101 | |
| 102 | - //if user have some composer packages |
|
| 103 | - $LOGGER->debug('Check for composer autoload'); |
|
| 104 | - if(file_exists(VENDOR_PATH . 'autoload.php')){ |
|
| 105 | - $LOGGER->info('The composer autoload file exists include it'); |
|
| 106 | - require_once VENDOR_PATH . 'autoload.php'; |
|
| 107 | - } |
|
| 108 | - else{ |
|
| 109 | - $LOGGER->info('The composer autoload file does not exist skipping'); |
|
| 110 | - } |
|
| 102 | + //if user have some composer packages |
|
| 103 | + $LOGGER->debug('Check for composer autoload'); |
|
| 104 | + if(file_exists(VENDOR_PATH . 'autoload.php')){ |
|
| 105 | + $LOGGER->info('The composer autoload file exists include it'); |
|
| 106 | + require_once VENDOR_PATH . 'autoload.php'; |
|
| 107 | + } |
|
| 108 | + else{ |
|
| 109 | + $LOGGER->info('The composer autoload file does not exist skipping'); |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - $LOGGER->debug('Begin to load the required resources'); |
|
| 112 | + $LOGGER->debug('Begin to load the required resources'); |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * Load the EventInfo class file |
|
| 116 | - */ |
|
| 117 | - require_once CORE_CLASSES_PATH . 'EventInfo.php'; |
|
| 114 | + /** |
|
| 115 | + * Load the EventInfo class file |
|
| 116 | + */ |
|
| 117 | + require_once CORE_CLASSES_PATH . 'EventInfo.php'; |
|
| 118 | 118 | |
| 119 | 119 | |
| 120 | - $BENCHMARK->mark('CONFIG_INIT_START'); |
|
| 121 | - /** |
|
| 122 | - * Load configurations and using the |
|
| 123 | - * static method "init()" to initialize the Config class . |
|
| 124 | - */ |
|
| 125 | - $CONFIG =& class_loader('Config', 'classes'); |
|
| 126 | - $CONFIG->init(); |
|
| 127 | - $BENCHMARK->mark('CONFIG_INIT_END'); |
|
| 120 | + $BENCHMARK->mark('CONFIG_INIT_START'); |
|
| 121 | + /** |
|
| 122 | + * Load configurations and using the |
|
| 123 | + * static method "init()" to initialize the Config class . |
|
| 124 | + */ |
|
| 125 | + $CONFIG =& class_loader('Config', 'classes'); |
|
| 126 | + $CONFIG->init(); |
|
| 127 | + $BENCHMARK->mark('CONFIG_INIT_END'); |
|
| 128 | 128 | |
| 129 | - $BENCHMARK->mark('MODULE_INIT_START'); |
|
| 130 | - /** |
|
| 131 | - * Load modules and using the |
|
| 132 | - * static method "init()" to initialize the Module class. |
|
| 133 | - */ |
|
| 134 | - $MODULE =& class_loader('Module', 'classes'); |
|
| 135 | - $MODULE->init(); |
|
| 136 | - $BENCHMARK->mark('MODULE_INIT_END'); |
|
| 129 | + $BENCHMARK->mark('MODULE_INIT_START'); |
|
| 130 | + /** |
|
| 131 | + * Load modules and using the |
|
| 132 | + * static method "init()" to initialize the Module class. |
|
| 133 | + */ |
|
| 134 | + $MODULE =& class_loader('Module', 'classes'); |
|
| 135 | + $MODULE->init(); |
|
| 136 | + $BENCHMARK->mark('MODULE_INIT_END'); |
|
| 137 | 137 | |
| 138 | - $LOGGER->debug('Loading Base Controller ...'); |
|
| 139 | - /** |
|
| 140 | - * Include of the file containing the Base Controller class |
|
| 141 | - */ |
|
| 142 | - require_once CORE_CLASSES_PATH . 'Controller.php'; |
|
| 143 | - $LOGGER->info('Base Controller loaded successfully'); |
|
| 138 | + $LOGGER->debug('Loading Base Controller ...'); |
|
| 139 | + /** |
|
| 140 | + * Include of the file containing the Base Controller class |
|
| 141 | + */ |
|
| 142 | + require_once CORE_CLASSES_PATH . 'Controller.php'; |
|
| 143 | + $LOGGER->info('Base Controller loaded successfully'); |
|
| 144 | 144 | |
| 145 | - /** |
|
| 146 | - * Register controllers autoload function |
|
| 147 | - */ |
|
| 148 | - spl_autoload_register('autoload_controller'); |
|
| 145 | + /** |
|
| 146 | + * Register controllers autoload function |
|
| 147 | + */ |
|
| 148 | + spl_autoload_register('autoload_controller'); |
|
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * Loading Security class |
|
| 152 | - */ |
|
| 153 | - $SECURITY =& class_loader('Security', 'classes'); |
|
| 154 | - $SECURITY->checkWhiteListIpAccess(); |
|
| 150 | + /** |
|
| 151 | + * Loading Security class |
|
| 152 | + */ |
|
| 153 | + $SECURITY =& class_loader('Security', 'classes'); |
|
| 154 | + $SECURITY->checkWhiteListIpAccess(); |
|
| 155 | 155 | |
| 156 | - /** |
|
| 157 | - * Loading Url class |
|
| 158 | - */ |
|
| 159 | - $URL =& class_loader('Url', 'classes'); |
|
| 156 | + /** |
|
| 157 | + * Loading Url class |
|
| 158 | + */ |
|
| 159 | + $URL =& class_loader('Url', 'classes'); |
|
| 160 | 160 | |
| 161 | - if(get_config('cache_enable', false)){ |
|
| 162 | - /** |
|
| 163 | - * Load Cache interface file |
|
| 164 | - */ |
|
| 165 | - require_once CORE_CLASSES_CACHE_PATH . 'CacheInterface.php'; |
|
| 166 | - $cacheHandler = get_config('cache_handler'); |
|
| 167 | - if(! $cacheHandler){ |
|
| 168 | - show_error('The cache feature is enabled in the configuration but the cache handler class is not set.'); |
|
| 169 | - } |
|
| 170 | - $CACHE = null; |
|
| 171 | - //first check if the cache handler is the system driver |
|
| 172 | - if(file_exists(CORE_CLASSES_CACHE_PATH . $cacheHandler . '.php')){ |
|
| 173 | - $CACHE =& class_loader($cacheHandler, 'classes/cache'); |
|
| 174 | - } |
|
| 175 | - else{ |
|
| 176 | - //it's not a system driver use user library |
|
| 177 | - $CACHE =& class_loader($cacheHandler); |
|
| 178 | - } |
|
| 179 | - //check if the page already cached |
|
| 180 | - if(! empty($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'get'){ |
|
| 181 | - $RESPONSE = & class_loader('Response', 'classes'); |
|
| 182 | - $RESPONSE->renderFinalPageFromCache($CACHE); |
|
| 183 | - } |
|
| 184 | - } |
|
| 161 | + if(get_config('cache_enable', false)){ |
|
| 162 | + /** |
|
| 163 | + * Load Cache interface file |
|
| 164 | + */ |
|
| 165 | + require_once CORE_CLASSES_CACHE_PATH . 'CacheInterface.php'; |
|
| 166 | + $cacheHandler = get_config('cache_handler'); |
|
| 167 | + if(! $cacheHandler){ |
|
| 168 | + show_error('The cache feature is enabled in the configuration but the cache handler class is not set.'); |
|
| 169 | + } |
|
| 170 | + $CACHE = null; |
|
| 171 | + //first check if the cache handler is the system driver |
|
| 172 | + if(file_exists(CORE_CLASSES_CACHE_PATH . $cacheHandler . '.php')){ |
|
| 173 | + $CACHE =& class_loader($cacheHandler, 'classes/cache'); |
|
| 174 | + } |
|
| 175 | + else{ |
|
| 176 | + //it's not a system driver use user library |
|
| 177 | + $CACHE =& class_loader($cacheHandler); |
|
| 178 | + } |
|
| 179 | + //check if the page already cached |
|
| 180 | + if(! empty($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'get'){ |
|
| 181 | + $RESPONSE = & class_loader('Response', 'classes'); |
|
| 182 | + $RESPONSE->renderFinalPageFromCache($CACHE); |
|
| 183 | + } |
|
| 184 | + } |
|
| 185 | 185 | |
| 186 | - //load model class |
|
| 187 | - require_once CORE_CLASSES_MODEL_PATH . 'Model.php'; |
|
| 186 | + //load model class |
|
| 187 | + require_once CORE_CLASSES_MODEL_PATH . 'Model.php'; |
|
| 188 | 188 | |
| 189 | - $LOGGER->info('Everything is OK load Router library and dispatch the request to the corresponding controller'); |
|
| 190 | - /** |
|
| 191 | - * Routing |
|
| 192 | - * instantiation of the "Router" class and user request routing processing. |
|
| 193 | - */ |
|
| 194 | - $ROUTER = & class_loader('Router', 'classes'); |
|
| 195 | - $ROUTER->run(); |
|
| 189 | + $LOGGER->info('Everything is OK load Router library and dispatch the request to the corresponding controller'); |
|
| 190 | + /** |
|
| 191 | + * Routing |
|
| 192 | + * instantiation of the "Router" class and user request routing processing. |
|
| 193 | + */ |
|
| 194 | + $ROUTER = & class_loader('Router', 'classes'); |
|
| 195 | + $ROUTER->run(); |
|
@@ -1,81 +1,81 @@ |
||
| 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 constants.php |
|
| 29 | - * |
|
| 30 | - * This file contains the declaration of most of the constants used in the system, |
|
| 31 | - * for example: the version, the name of the framework, etc. |
|
| 32 | - * |
|
| 33 | - * @package core |
|
| 34 | - * @author Tony NGUEREZA |
|
| 35 | - * @copyright Copyright (c) 2017 |
|
| 36 | - * @license https://opensource.org/licenses/gpl-3.0.html GNU GPL License (GPL) |
|
| 37 | - * @link http://www.iacademy.cf |
|
| 38 | - * @version 1.0.0 |
|
| 39 | - * @filesource |
|
| 40 | - */ |
|
| 27 | + /** |
|
| 28 | + * @file constants.php |
|
| 29 | + * |
|
| 30 | + * This file contains the declaration of most of the constants used in the system, |
|
| 31 | + * for example: the version, the name of the framework, etc. |
|
| 32 | + * |
|
| 33 | + * @package core |
|
| 34 | + * @author Tony NGUEREZA |
|
| 35 | + * @copyright Copyright (c) 2017 |
|
| 36 | + * @license https://opensource.org/licenses/gpl-3.0.html GNU GPL License (GPL) |
|
| 37 | + * @link http://www.iacademy.cf |
|
| 38 | + * @version 1.0.0 |
|
| 39 | + * @filesource |
|
| 40 | + */ |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * The framework name |
|
| 44 | - */ |
|
| 45 | - define('TNH_NAME', 'TNH Framework'); |
|
| 42 | + /** |
|
| 43 | + * The framework name |
|
| 44 | + */ |
|
| 45 | + define('TNH_NAME', 'TNH Framework'); |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * The version of the framework in X.Y.Z format (Major, minor and bugs). |
|
| 49 | - * If there is the presence of the word "dev", it means that |
|
| 50 | - * it is a version under development. |
|
| 51 | - */ |
|
| 52 | - define('TNH_VERSION', '1.0.0-dev'); |
|
| 47 | + /** |
|
| 48 | + * The version of the framework in X.Y.Z format (Major, minor and bugs). |
|
| 49 | + * If there is the presence of the word "dev", it means that |
|
| 50 | + * it is a version under development. |
|
| 51 | + */ |
|
| 52 | + define('TNH_VERSION', '1.0.0-dev'); |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * The date of publication or release of the framework |
|
| 56 | - */ |
|
| 57 | - define('TNH_RELEASE_DATE', '2017/02/05'); |
|
| 54 | + /** |
|
| 55 | + * The date of publication or release of the framework |
|
| 56 | + */ |
|
| 57 | + define('TNH_RELEASE_DATE', '2017/02/05'); |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * The author of the framework, the person who developed the framework. |
|
| 61 | - */ |
|
| 62 | - define('TNH_AUTHOR', 'Tony NGUEREZA'); |
|
| 59 | + /** |
|
| 60 | + * The author of the framework, the person who developed the framework. |
|
| 61 | + */ |
|
| 62 | + define('TNH_AUTHOR', 'Tony NGUEREZA'); |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Email address of the author of the framework. |
|
| 66 | - */ |
|
| 67 | - define('TNH_AUTHOR_EMAIL', '[email protected]'); |
|
| 64 | + /** |
|
| 65 | + * Email address of the author of the framework. |
|
| 66 | + */ |
|
| 67 | + define('TNH_AUTHOR_EMAIL', '[email protected]'); |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * The minimum PHP version required to use the framework. |
|
| 71 | - * If the version of PHP installed is lower, then the application will not work. |
|
| 72 | - * Note: we use the PHP version_compare function to compare the required version with |
|
| 73 | - * the version installed on your system. |
|
| 74 | - */ |
|
| 75 | - define('TNH_REQUIRED_PHP_MIN_VERSION', '5.4'); |
|
| 69 | + /** |
|
| 70 | + * The minimum PHP version required to use the framework. |
|
| 71 | + * If the version of PHP installed is lower, then the application will not work. |
|
| 72 | + * Note: we use the PHP version_compare function to compare the required version with |
|
| 73 | + * the version installed on your system. |
|
| 74 | + */ |
|
| 75 | + define('TNH_REQUIRED_PHP_MIN_VERSION', '5.4'); |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * The maximum version of PHP required to use the framework. |
|
| 79 | - * If the version of PHP installed is higher than the required one, then the application will not work. |
|
| 80 | - */ |
|
| 81 | - define('TNH_REQUIRED_PHP_MAX_VERSION', '7.1'); |
|
| 82 | 77 | \ No newline at end of file |
| 78 | + /** |
|
| 79 | + * The maximum version of PHP required to use the framework. |
|
| 80 | + * If the version of PHP installed is higher than the required one, then the application will not work. |
|
| 81 | + */ |
|
| 82 | + define('TNH_REQUIRED_PHP_MAX_VERSION', '7.1'); |
|
| 83 | 83 | \ No newline at end of file |
@@ -1,585 +1,585 @@ |
||
| 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 common.php |
|
| 29 | - * |
|
| 30 | - * Contains most of the commons functions used by the system |
|
| 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 | - * @filesource |
|
| 39 | - */ |
|
| 27 | + /** |
|
| 28 | + * @file common.php |
|
| 29 | + * |
|
| 30 | + * Contains most of the commons functions used by the system |
|
| 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 | + * @filesource |
|
| 39 | + */ |
|
| 40 | 40 | |
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * This function is the class loader helper is used if the library "Loader" not yet loaded |
|
| 44 | - * he load the class once |
|
| 45 | - * @param string $class the class name to be loaded |
|
| 46 | - * @param string $dir the directory where to find the class |
|
| 47 | - * @param mixed $params the parameter to pass as argument to the constructor of the class |
|
| 48 | - * @codeCoverageIgnore |
|
| 49 | - * |
|
| 50 | - * @return object the instance of the loaded class |
|
| 51 | - */ |
|
| 52 | - function & class_loader($class, $dir = 'libraries', $params = null){ |
|
| 53 | - //put the first letter of class to upper case |
|
| 54 | - $class = ucfirst($class); |
|
| 55 | - static $classes = array(); |
|
| 56 | - if (isset($classes[$class]) /*hack for duplicate log Logger name*/ && $class != 'Log'){ |
|
| 57 | - return $classes[$class]; |
|
| 58 | - } |
|
| 59 | - $found = false; |
|
| 60 | - foreach (array(ROOT_PATH, CORE_PATH) as $path) { |
|
| 61 | - $file = $path . $dir . '/' . $class . '.php'; |
|
| 62 | - if (file_exists($file)){ |
|
| 63 | - if (class_exists($class, false) === false){ |
|
| 64 | - require_once $file; |
|
| 65 | - } |
|
| 66 | - //already found |
|
| 67 | - $found = true; |
|
| 68 | - break; |
|
| 69 | - } |
|
| 70 | - } |
|
| 71 | - if (! $found){ |
|
| 72 | - //can't use show_error() at this time because some dependencies not yet loaded |
|
| 73 | - set_http_status_header(503); |
|
| 74 | - echo 'Cannot find the class [' . $class . ']'; |
|
| 75 | - die(); |
|
| 76 | - } |
|
| 42 | + /** |
|
| 43 | + * This function is the class loader helper is used if the library "Loader" not yet loaded |
|
| 44 | + * he load the class once |
|
| 45 | + * @param string $class the class name to be loaded |
|
| 46 | + * @param string $dir the directory where to find the class |
|
| 47 | + * @param mixed $params the parameter to pass as argument to the constructor of the class |
|
| 48 | + * @codeCoverageIgnore |
|
| 49 | + * |
|
| 50 | + * @return object the instance of the loaded class |
|
| 51 | + */ |
|
| 52 | + function & class_loader($class, $dir = 'libraries', $params = null){ |
|
| 53 | + //put the first letter of class to upper case |
|
| 54 | + $class = ucfirst($class); |
|
| 55 | + static $classes = array(); |
|
| 56 | + if (isset($classes[$class]) /*hack for duplicate log Logger name*/ && $class != 'Log'){ |
|
| 57 | + return $classes[$class]; |
|
| 58 | + } |
|
| 59 | + $found = false; |
|
| 60 | + foreach (array(ROOT_PATH, CORE_PATH) as $path) { |
|
| 61 | + $file = $path . $dir . '/' . $class . '.php'; |
|
| 62 | + if (file_exists($file)){ |
|
| 63 | + if (class_exists($class, false) === false){ |
|
| 64 | + require_once $file; |
|
| 65 | + } |
|
| 66 | + //already found |
|
| 67 | + $found = true; |
|
| 68 | + break; |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | + if (! $found){ |
|
| 72 | + //can't use show_error() at this time because some dependencies not yet loaded |
|
| 73 | + set_http_status_header(503); |
|
| 74 | + echo 'Cannot find the class [' . $class . ']'; |
|
| 75 | + die(); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /* |
|
| 78 | + /* |
|
| 79 | 79 | TODO use the best method to get the Log instance |
| 80 | 80 | */ |
| 81 | - if ($class == 'Log'){ |
|
| 82 | - //can't use the instruction like "return new Log()" |
|
| 83 | - //because we need return the reference instance of the loaded class. |
|
| 84 | - $log = new Log(); |
|
| 85 | - return $log; |
|
| 86 | - } |
|
| 87 | - //track of loaded classes |
|
| 88 | - class_loaded($class); |
|
| 81 | + if ($class == 'Log'){ |
|
| 82 | + //can't use the instruction like "return new Log()" |
|
| 83 | + //because we need return the reference instance of the loaded class. |
|
| 84 | + $log = new Log(); |
|
| 85 | + return $log; |
|
| 86 | + } |
|
| 87 | + //track of loaded classes |
|
| 88 | + class_loaded($class); |
|
| 89 | 89 | |
| 90 | - //record the class instance |
|
| 91 | - $classes[$class] = isset($params) ? new $class($params) : new $class(); |
|
| 90 | + //record the class instance |
|
| 91 | + $classes[$class] = isset($params) ? new $class($params) : new $class(); |
|
| 92 | 92 | |
| 93 | - return $classes[$class]; |
|
| 94 | - } |
|
| 93 | + return $classes[$class]; |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * This function is the helper to record the loaded classes |
|
| 98 | - * @param string $class the loaded class name |
|
| 99 | - * @codeCoverageIgnore |
|
| 100 | - * |
|
| 101 | - * @return array the list of the loaded classes |
|
| 102 | - */ |
|
| 103 | - function & class_loaded($class = null){ |
|
| 104 | - static $list = array(); |
|
| 105 | - if ($class !== null){ |
|
| 106 | - $list[strtolower($class)] = $class; |
|
| 107 | - } |
|
| 108 | - return $list; |
|
| 109 | - } |
|
| 96 | + /** |
|
| 97 | + * This function is the helper to record the loaded classes |
|
| 98 | + * @param string $class the loaded class name |
|
| 99 | + * @codeCoverageIgnore |
|
| 100 | + * |
|
| 101 | + * @return array the list of the loaded classes |
|
| 102 | + */ |
|
| 103 | + function & class_loaded($class = null){ |
|
| 104 | + static $list = array(); |
|
| 105 | + if ($class !== null){ |
|
| 106 | + $list[strtolower($class)] = $class; |
|
| 107 | + } |
|
| 108 | + return $list; |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * This function is used to load the configurations in the case the "Config" library not yet loaded |
|
| 113 | - * @param array $overwrite_values if need overwrite the existing configuration |
|
| 114 | - * @codeCoverageIgnore |
|
| 115 | - * |
|
| 116 | - * @return array the configurations values |
|
| 117 | - */ |
|
| 118 | - function & load_configurations(array $overwrite_values = array()){ |
|
| 119 | - static $config; |
|
| 120 | - if (empty($config)){ |
|
| 121 | - $file = CONFIG_PATH . 'config.php'; |
|
| 122 | - $found = false; |
|
| 123 | - if (file_exists($file)){ |
|
| 124 | - require_once $file; |
|
| 125 | - $found = true; |
|
| 126 | - } |
|
| 127 | - if (! $found){ |
|
| 128 | - set_http_status_header(503); |
|
| 129 | - echo 'Unable to find the configuration file [' . $file . ']'; |
|
| 130 | - die(); |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - foreach ($overwrite_values as $key => $value) { |
|
| 134 | - $config[$key] = $value; |
|
| 135 | - } |
|
| 136 | - return $config; |
|
| 137 | - } |
|
| 111 | + /** |
|
| 112 | + * This function is used to load the configurations in the case the "Config" library not yet loaded |
|
| 113 | + * @param array $overwrite_values if need overwrite the existing configuration |
|
| 114 | + * @codeCoverageIgnore |
|
| 115 | + * |
|
| 116 | + * @return array the configurations values |
|
| 117 | + */ |
|
| 118 | + function & load_configurations(array $overwrite_values = array()){ |
|
| 119 | + static $config; |
|
| 120 | + if (empty($config)){ |
|
| 121 | + $file = CONFIG_PATH . 'config.php'; |
|
| 122 | + $found = false; |
|
| 123 | + if (file_exists($file)){ |
|
| 124 | + require_once $file; |
|
| 125 | + $found = true; |
|
| 126 | + } |
|
| 127 | + if (! $found){ |
|
| 128 | + set_http_status_header(503); |
|
| 129 | + echo 'Unable to find the configuration file [' . $file . ']'; |
|
| 130 | + die(); |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + foreach ($overwrite_values as $key => $value) { |
|
| 134 | + $config[$key] = $value; |
|
| 135 | + } |
|
| 136 | + return $config; |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - /** |
|
| 140 | - * This function is the helper to get the config value in case the "Config" library not yet loaded |
|
| 141 | - * @param string $key the config item to get the vale |
|
| 142 | - * @param mixed $default the default value to return if can't find the config item in the configuration |
|
| 143 | - * @test |
|
| 144 | - * |
|
| 145 | - * @return mixed the config value |
|
| 146 | - */ |
|
| 147 | - function get_config($key, $default = null){ |
|
| 148 | - static $cfg; |
|
| 149 | - if (empty($cfg)){ |
|
| 150 | - $cfg[0] = & load_configurations(); |
|
| 151 | - } |
|
| 152 | - return array_key_exists($key, $cfg[0]) ? $cfg[0][$key] : $default; |
|
| 153 | - } |
|
| 139 | + /** |
|
| 140 | + * This function is the helper to get the config value in case the "Config" library not yet loaded |
|
| 141 | + * @param string $key the config item to get the vale |
|
| 142 | + * @param mixed $default the default value to return if can't find the config item in the configuration |
|
| 143 | + * @test |
|
| 144 | + * |
|
| 145 | + * @return mixed the config value |
|
| 146 | + */ |
|
| 147 | + function get_config($key, $default = null){ |
|
| 148 | + static $cfg; |
|
| 149 | + if (empty($cfg)){ |
|
| 150 | + $cfg[0] = & load_configurations(); |
|
| 151 | + } |
|
| 152 | + return array_key_exists($key, $cfg[0]) ? $cfg[0][$key] : $default; |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | - /** |
|
| 156 | - * This function is a helper to logging message |
|
| 157 | - * @param string $level the log level "ERROR", "DEBUG", "INFO", etc. |
|
| 158 | - * @param string $message the log message to be saved |
|
| 159 | - * @param string $logger the logger to use if is set |
|
| 160 | - * |
|
| 161 | - * @codeCoverageIgnore |
|
| 162 | - */ |
|
| 163 | - function save_to_log($level, $message, $logger = null){ |
|
| 164 | - $log =& class_loader('Log', 'classes'); |
|
| 165 | - if ($logger){ |
|
| 166 | - $log->setLogger($logger); |
|
| 167 | - } |
|
| 168 | - $log->writeLog($message, $level); |
|
| 169 | - } |
|
| 155 | + /** |
|
| 156 | + * This function is a helper to logging message |
|
| 157 | + * @param string $level the log level "ERROR", "DEBUG", "INFO", etc. |
|
| 158 | + * @param string $message the log message to be saved |
|
| 159 | + * @param string $logger the logger to use if is set |
|
| 160 | + * |
|
| 161 | + * @codeCoverageIgnore |
|
| 162 | + */ |
|
| 163 | + function save_to_log($level, $message, $logger = null){ |
|
| 164 | + $log =& class_loader('Log', 'classes'); |
|
| 165 | + if ($logger){ |
|
| 166 | + $log->setLogger($logger); |
|
| 167 | + } |
|
| 168 | + $log->writeLog($message, $level); |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - /** |
|
| 172 | - * Set the HTTP status header |
|
| 173 | - * @param integer $code the HTTP status code |
|
| 174 | - * @param string $text the HTTP status text |
|
| 175 | - * |
|
| 176 | - * @codeCoverageIgnore |
|
| 177 | - */ |
|
| 178 | - function set_http_status_header($code = 200, $text = null){ |
|
| 179 | - if (empty($text)){ |
|
| 180 | - $http_status = array( |
|
| 181 | - 100 => 'Continue', |
|
| 182 | - 101 => 'Switching Protocols', |
|
| 171 | + /** |
|
| 172 | + * Set the HTTP status header |
|
| 173 | + * @param integer $code the HTTP status code |
|
| 174 | + * @param string $text the HTTP status text |
|
| 175 | + * |
|
| 176 | + * @codeCoverageIgnore |
|
| 177 | + */ |
|
| 178 | + function set_http_status_header($code = 200, $text = null){ |
|
| 179 | + if (empty($text)){ |
|
| 180 | + $http_status = array( |
|
| 181 | + 100 => 'Continue', |
|
| 182 | + 101 => 'Switching Protocols', |
|
| 183 | 183 | |
| 184 | - 200 => 'OK', |
|
| 185 | - 201 => 'Created', |
|
| 186 | - 202 => 'Accepted', |
|
| 187 | - 203 => 'Non-Authoritative Information', |
|
| 188 | - 204 => 'No Content', |
|
| 189 | - 205 => 'Reset Content', |
|
| 190 | - 206 => 'Partial Content', |
|
| 184 | + 200 => 'OK', |
|
| 185 | + 201 => 'Created', |
|
| 186 | + 202 => 'Accepted', |
|
| 187 | + 203 => 'Non-Authoritative Information', |
|
| 188 | + 204 => 'No Content', |
|
| 189 | + 205 => 'Reset Content', |
|
| 190 | + 206 => 'Partial Content', |
|
| 191 | 191 | |
| 192 | - 300 => 'Multiple Choices', |
|
| 193 | - 301 => 'Moved Permanently', |
|
| 194 | - 302 => 'Found', |
|
| 195 | - 303 => 'See Other', |
|
| 196 | - 304 => 'Not Modified', |
|
| 197 | - 305 => 'Use Proxy', |
|
| 198 | - 307 => 'Temporary Redirect', |
|
| 192 | + 300 => 'Multiple Choices', |
|
| 193 | + 301 => 'Moved Permanently', |
|
| 194 | + 302 => 'Found', |
|
| 195 | + 303 => 'See Other', |
|
| 196 | + 304 => 'Not Modified', |
|
| 197 | + 305 => 'Use Proxy', |
|
| 198 | + 307 => 'Temporary Redirect', |
|
| 199 | 199 | |
| 200 | - 400 => 'Bad Request', |
|
| 201 | - 401 => 'Unauthorized', |
|
| 202 | - 402 => 'Payment Required', |
|
| 203 | - 403 => 'Forbidden', |
|
| 204 | - 404 => 'Not Found', |
|
| 205 | - 405 => 'Method Not Allowed', |
|
| 206 | - 406 => 'Not Acceptable', |
|
| 207 | - 407 => 'Proxy Authentication Required', |
|
| 208 | - 408 => 'Request Timeout', |
|
| 209 | - 409 => 'Conflict', |
|
| 210 | - 410 => 'Gone', |
|
| 211 | - 411 => 'Length Required', |
|
| 212 | - 412 => 'Precondition Failed', |
|
| 213 | - 413 => 'Request Entity Too Large', |
|
| 214 | - 414 => 'Request-URI Too Long', |
|
| 215 | - 415 => 'Unsupported Media Type', |
|
| 216 | - 416 => 'Requested Range Not Satisfiable', |
|
| 217 | - 417 => 'Expectation Failed', |
|
| 218 | - 418 => 'I\'m a teapot', |
|
| 200 | + 400 => 'Bad Request', |
|
| 201 | + 401 => 'Unauthorized', |
|
| 202 | + 402 => 'Payment Required', |
|
| 203 | + 403 => 'Forbidden', |
|
| 204 | + 404 => 'Not Found', |
|
| 205 | + 405 => 'Method Not Allowed', |
|
| 206 | + 406 => 'Not Acceptable', |
|
| 207 | + 407 => 'Proxy Authentication Required', |
|
| 208 | + 408 => 'Request Timeout', |
|
| 209 | + 409 => 'Conflict', |
|
| 210 | + 410 => 'Gone', |
|
| 211 | + 411 => 'Length Required', |
|
| 212 | + 412 => 'Precondition Failed', |
|
| 213 | + 413 => 'Request Entity Too Large', |
|
| 214 | + 414 => 'Request-URI Too Long', |
|
| 215 | + 415 => 'Unsupported Media Type', |
|
| 216 | + 416 => 'Requested Range Not Satisfiable', |
|
| 217 | + 417 => 'Expectation Failed', |
|
| 218 | + 418 => 'I\'m a teapot', |
|
| 219 | 219 | |
| 220 | - 500 => 'Internal Server Error', |
|
| 221 | - 501 => 'Not Implemented', |
|
| 222 | - 502 => 'Bad Gateway', |
|
| 223 | - 503 => 'Service Unavailable', |
|
| 224 | - 504 => 'Gateway Timeout', |
|
| 225 | - 505 => 'HTTP Version Not Supported', |
|
| 226 | - ); |
|
| 227 | - if (isset($http_status[$code])){ |
|
| 228 | - $text = $http_status[$code]; |
|
| 229 | - } |
|
| 230 | - else{ |
|
| 231 | - show_error('No HTTP status text found for your code please check it.'); |
|
| 232 | - } |
|
| 233 | - } |
|
| 220 | + 500 => 'Internal Server Error', |
|
| 221 | + 501 => 'Not Implemented', |
|
| 222 | + 502 => 'Bad Gateway', |
|
| 223 | + 503 => 'Service Unavailable', |
|
| 224 | + 504 => 'Gateway Timeout', |
|
| 225 | + 505 => 'HTTP Version Not Supported', |
|
| 226 | + ); |
|
| 227 | + if (isset($http_status[$code])){ |
|
| 228 | + $text = $http_status[$code]; |
|
| 229 | + } |
|
| 230 | + else{ |
|
| 231 | + show_error('No HTTP status text found for your code please check it.'); |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | 234 | |
| 235 | - if (strpos(php_sapi_name(), 'cgi') === 0){ |
|
| 236 | - header('Status: ' . $code . ' ' . $text, TRUE); |
|
| 237 | - } |
|
| 238 | - else{ |
|
| 239 | - $proto = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'; |
|
| 240 | - header($proto . ' ' . $code . ' ' . $text, TRUE, $code); |
|
| 241 | - } |
|
| 242 | - } |
|
| 235 | + if (strpos(php_sapi_name(), 'cgi') === 0){ |
|
| 236 | + header('Status: ' . $code . ' ' . $text, TRUE); |
|
| 237 | + } |
|
| 238 | + else{ |
|
| 239 | + $proto = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'; |
|
| 240 | + header($proto . ' ' . $code . ' ' . $text, TRUE, $code); |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | 243 | |
| 244 | - /** |
|
| 245 | - * This function displays an error message to the user and ends the execution of the script. |
|
| 246 | - * |
|
| 247 | - * @param string $msg the message to display |
|
| 248 | - * @param string $title the message title: "error", "info", "warning", etc. |
|
| 249 | - * @param boolean $logging either to save error in log |
|
| 250 | - * |
|
| 251 | - * @codeCoverageIgnore |
|
| 252 | - */ |
|
| 253 | - function show_error($msg, $title = 'error', $logging = true){ |
|
| 254 | - $title = strtoupper($title); |
|
| 255 | - $data = array(); |
|
| 256 | - $data['error'] = $msg; |
|
| 257 | - $data['title'] = $title; |
|
| 258 | - if ($logging){ |
|
| 259 | - save_to_log('error', '['.$title.'] '.strip_tags($msg), 'GLOBAL::ERROR'); |
|
| 260 | - } |
|
| 261 | - $response = & class_loader('Response', 'classes'); |
|
| 262 | - $response->sendError($data); |
|
| 263 | - die(); |
|
| 264 | - } |
|
| 244 | + /** |
|
| 245 | + * This function displays an error message to the user and ends the execution of the script. |
|
| 246 | + * |
|
| 247 | + * @param string $msg the message to display |
|
| 248 | + * @param string $title the message title: "error", "info", "warning", etc. |
|
| 249 | + * @param boolean $logging either to save error in log |
|
| 250 | + * |
|
| 251 | + * @codeCoverageIgnore |
|
| 252 | + */ |
|
| 253 | + function show_error($msg, $title = 'error', $logging = true){ |
|
| 254 | + $title = strtoupper($title); |
|
| 255 | + $data = array(); |
|
| 256 | + $data['error'] = $msg; |
|
| 257 | + $data['title'] = $title; |
|
| 258 | + if ($logging){ |
|
| 259 | + save_to_log('error', '['.$title.'] '.strip_tags($msg), 'GLOBAL::ERROR'); |
|
| 260 | + } |
|
| 261 | + $response = & class_loader('Response', 'classes'); |
|
| 262 | + $response->sendError($data); |
|
| 263 | + die(); |
|
| 264 | + } |
|
| 265 | 265 | |
| 266 | - /** |
|
| 267 | - * Check whether the protocol used is "https" or not |
|
| 268 | - * That is, the web server is configured to use a secure connection. |
|
| 269 | - * @codeCoverageIgnore |
|
| 270 | - * |
|
| 271 | - * @return boolean true if the web server uses the https protocol, false if not. |
|
| 272 | - */ |
|
| 273 | - function is_https(){ |
|
| 274 | - /* |
|
| 266 | + /** |
|
| 267 | + * Check whether the protocol used is "https" or not |
|
| 268 | + * That is, the web server is configured to use a secure connection. |
|
| 269 | + * @codeCoverageIgnore |
|
| 270 | + * |
|
| 271 | + * @return boolean true if the web server uses the https protocol, false if not. |
|
| 272 | + */ |
|
| 273 | + function is_https(){ |
|
| 274 | + /* |
|
| 275 | 275 | * some servers pass the "HTTPS" parameter in the server variable, |
| 276 | 276 | * if is the case, check if the value is "on", "true", "1". |
| 277 | 277 | */ |
| 278 | - if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off'){ |
|
| 279 | - return true; |
|
| 280 | - } |
|
| 281 | - else if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https'){ |
|
| 282 | - return true; |
|
| 283 | - } |
|
| 284 | - else if (isset($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off'){ |
|
| 285 | - return true; |
|
| 286 | - } |
|
| 287 | - return false; |
|
| 288 | - } |
|
| 278 | + if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off'){ |
|
| 279 | + return true; |
|
| 280 | + } |
|
| 281 | + else if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https'){ |
|
| 282 | + return true; |
|
| 283 | + } |
|
| 284 | + else if (isset($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off'){ |
|
| 285 | + return true; |
|
| 286 | + } |
|
| 287 | + return false; |
|
| 288 | + } |
|
| 289 | 289 | |
| 290 | - /** |
|
| 291 | - * This function is used to check the URL format of the given string argument. |
|
| 292 | - * The address is valid if the protocol is http, https, ftp, etc. |
|
| 293 | - * |
|
| 294 | - * @param string $url the URL address to check |
|
| 295 | - * @test |
|
| 296 | - * |
|
| 297 | - * @return boolean true if is a valid URL address or false. |
|
| 298 | - */ |
|
| 299 | - function is_url($url){ |
|
| 300 | - return preg_match('/^(http|https|ftp):\/\/(.*)/', $url) == 1; |
|
| 301 | - } |
|
| 290 | + /** |
|
| 291 | + * This function is used to check the URL format of the given string argument. |
|
| 292 | + * The address is valid if the protocol is http, https, ftp, etc. |
|
| 293 | + * |
|
| 294 | + * @param string $url the URL address to check |
|
| 295 | + * @test |
|
| 296 | + * |
|
| 297 | + * @return boolean true if is a valid URL address or false. |
|
| 298 | + */ |
|
| 299 | + function is_url($url){ |
|
| 300 | + return preg_match('/^(http|https|ftp):\/\/(.*)/', $url) == 1; |
|
| 301 | + } |
|
| 302 | 302 | |
| 303 | - /** |
|
| 304 | - * Function defined to load controller |
|
| 305 | - * |
|
| 306 | - * @param string $controllerClass the controller class name to be loaded |
|
| 307 | - * @codeCoverageIgnore |
|
| 308 | - */ |
|
| 309 | - function autoload_controller($controllerClass){ |
|
| 310 | - if (file_exists($path = APPS_CONTROLLER_PATH . $controllerClass . '.php')){ |
|
| 311 | - require_once $path; |
|
| 312 | - } |
|
| 313 | - } |
|
| 303 | + /** |
|
| 304 | + * Function defined to load controller |
|
| 305 | + * |
|
| 306 | + * @param string $controllerClass the controller class name to be loaded |
|
| 307 | + * @codeCoverageIgnore |
|
| 308 | + */ |
|
| 309 | + function autoload_controller($controllerClass){ |
|
| 310 | + if (file_exists($path = APPS_CONTROLLER_PATH . $controllerClass . '.php')){ |
|
| 311 | + require_once $path; |
|
| 312 | + } |
|
| 313 | + } |
|
| 314 | 314 | |
| 315 | - /** |
|
| 316 | - * Function defined for handling PHP exception error message, |
|
| 317 | - * it displays an error message using the function "show_error" |
|
| 318 | - * |
|
| 319 | - * @param object $ex instance of the "Exception" class or a derived class |
|
| 320 | - * @codeCoverageIgnore |
|
| 321 | - * |
|
| 322 | - * @return boolean |
|
| 323 | - */ |
|
| 324 | - function php_exception_handler($ex){ |
|
| 325 | - if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))){ |
|
| 326 | - show_error('An exception is occured in file '. $ex->getFile() .' at line ' . $ex->getLine() . ' raison : ' . $ex->getMessage(), 'PHP Exception #' . $ex->getCode()); |
|
| 327 | - } |
|
| 328 | - else{ |
|
| 329 | - save_to_log('error', 'An exception is occured in file ' . $ex->getFile() . ' at line ' . $ex->getLine() . ' raison : ' . $ex->getMessage(), 'PHP Exception'); |
|
| 330 | - } |
|
| 331 | - return true; |
|
| 332 | - } |
|
| 315 | + /** |
|
| 316 | + * Function defined for handling PHP exception error message, |
|
| 317 | + * it displays an error message using the function "show_error" |
|
| 318 | + * |
|
| 319 | + * @param object $ex instance of the "Exception" class or a derived class |
|
| 320 | + * @codeCoverageIgnore |
|
| 321 | + * |
|
| 322 | + * @return boolean |
|
| 323 | + */ |
|
| 324 | + function php_exception_handler($ex){ |
|
| 325 | + if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))){ |
|
| 326 | + show_error('An exception is occured in file '. $ex->getFile() .' at line ' . $ex->getLine() . ' raison : ' . $ex->getMessage(), 'PHP Exception #' . $ex->getCode()); |
|
| 327 | + } |
|
| 328 | + else{ |
|
| 329 | + save_to_log('error', 'An exception is occured in file ' . $ex->getFile() . ' at line ' . $ex->getLine() . ' raison : ' . $ex->getMessage(), 'PHP Exception'); |
|
| 330 | + } |
|
| 331 | + return true; |
|
| 332 | + } |
|
| 333 | 333 | |
| 334 | - /** |
|
| 335 | - * Function defined for PHP error message handling |
|
| 336 | - * |
|
| 337 | - * @param int $errno the type of error for example: E_USER_ERROR, E_USER_WARNING, etc. |
|
| 338 | - * @param string $errstr the error message |
|
| 339 | - * @param string $errfile the file where the error occurred |
|
| 340 | - * @param int $errline the line number where the error occurred |
|
| 341 | - * @codeCoverageIgnore |
|
| 342 | - * |
|
| 343 | - * @return boolean |
|
| 344 | - */ |
|
| 345 | - function php_error_handler($errno , $errstr, $errfile , $errline){ |
|
| 346 | - $isError = (((E_ERROR | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR) & $errno) === $errno); |
|
| 347 | - if ($isError){ |
|
| 348 | - set_http_status_header(500); |
|
| 349 | - } |
|
| 350 | - if (! (error_reporting() & $errno)) { |
|
| 351 | - save_to_log('error', 'An error is occurred in the file ' . $errfile . ' at line ' . $errline . ' raison : ' . $errstr, 'PHP ERROR'); |
|
| 352 | - return; |
|
| 353 | - } |
|
| 354 | - if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))){ |
|
| 355 | - $errorType = 'error'; |
|
| 356 | - switch ($errno) { |
|
| 357 | - case E_USER_ERROR: |
|
| 358 | - $errorType = 'error'; |
|
| 359 | - break; |
|
| 360 | - case E_USER_WARNING: |
|
| 361 | - $errorType = 'warning'; |
|
| 362 | - break; |
|
| 363 | - case E_USER_NOTICE: |
|
| 364 | - $errorType = 'notice'; |
|
| 365 | - break; |
|
| 366 | - default: |
|
| 367 | - $errorType = 'error'; |
|
| 368 | - break; |
|
| 369 | - } |
|
| 370 | - show_error('An error is occurred in the file <b>' . $errfile . '</b> at line <b>' . $errline .'</b> raison : ' . $errstr, 'PHP ' . $errorType); |
|
| 371 | - } |
|
| 372 | - if ($isError){ |
|
| 373 | - die(); |
|
| 374 | - } |
|
| 375 | - return true; |
|
| 376 | - } |
|
| 334 | + /** |
|
| 335 | + * Function defined for PHP error message handling |
|
| 336 | + * |
|
| 337 | + * @param int $errno the type of error for example: E_USER_ERROR, E_USER_WARNING, etc. |
|
| 338 | + * @param string $errstr the error message |
|
| 339 | + * @param string $errfile the file where the error occurred |
|
| 340 | + * @param int $errline the line number where the error occurred |
|
| 341 | + * @codeCoverageIgnore |
|
| 342 | + * |
|
| 343 | + * @return boolean |
|
| 344 | + */ |
|
| 345 | + function php_error_handler($errno , $errstr, $errfile , $errline){ |
|
| 346 | + $isError = (((E_ERROR | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR) & $errno) === $errno); |
|
| 347 | + if ($isError){ |
|
| 348 | + set_http_status_header(500); |
|
| 349 | + } |
|
| 350 | + if (! (error_reporting() & $errno)) { |
|
| 351 | + save_to_log('error', 'An error is occurred in the file ' . $errfile . ' at line ' . $errline . ' raison : ' . $errstr, 'PHP ERROR'); |
|
| 352 | + return; |
|
| 353 | + } |
|
| 354 | + if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))){ |
|
| 355 | + $errorType = 'error'; |
|
| 356 | + switch ($errno) { |
|
| 357 | + case E_USER_ERROR: |
|
| 358 | + $errorType = 'error'; |
|
| 359 | + break; |
|
| 360 | + case E_USER_WARNING: |
|
| 361 | + $errorType = 'warning'; |
|
| 362 | + break; |
|
| 363 | + case E_USER_NOTICE: |
|
| 364 | + $errorType = 'notice'; |
|
| 365 | + break; |
|
| 366 | + default: |
|
| 367 | + $errorType = 'error'; |
|
| 368 | + break; |
|
| 369 | + } |
|
| 370 | + show_error('An error is occurred in the file <b>' . $errfile . '</b> at line <b>' . $errline .'</b> raison : ' . $errstr, 'PHP ' . $errorType); |
|
| 371 | + } |
|
| 372 | + if ($isError){ |
|
| 373 | + die(); |
|
| 374 | + } |
|
| 375 | + return true; |
|
| 376 | + } |
|
| 377 | 377 | |
| 378 | - /** |
|
| 379 | - * This function is used to run in shutdown situation of the script |
|
| 380 | - * @codeCoverageIgnore |
|
| 381 | - */ |
|
| 382 | - function php_shudown_handler(){ |
|
| 383 | - $lastError = error_get_last(); |
|
| 384 | - if (isset($lastError) && |
|
| 385 | - ($lastError['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING))){ |
|
| 386 | - php_error_handler($lastError['type'], $lastError['message'], $lastError['file'], $lastError['line']); |
|
| 387 | - } |
|
| 388 | - } |
|
| 378 | + /** |
|
| 379 | + * This function is used to run in shutdown situation of the script |
|
| 380 | + * @codeCoverageIgnore |
|
| 381 | + */ |
|
| 382 | + function php_shudown_handler(){ |
|
| 383 | + $lastError = error_get_last(); |
|
| 384 | + if (isset($lastError) && |
|
| 385 | + ($lastError['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING))){ |
|
| 386 | + php_error_handler($lastError['type'], $lastError['message'], $lastError['file'], $lastError['line']); |
|
| 387 | + } |
|
| 388 | + } |
|
| 389 | 389 | |
| 390 | 390 | |
| 391 | - /** |
|
| 392 | - * Convert array attributes to string |
|
| 393 | - * |
|
| 394 | - * This function converts an associative array into HTML attributes. |
|
| 395 | - * For example : |
|
| 396 | - * $a = array('name' => 'Foo', 'type' => 'text'); => produces the following string: |
|
| 397 | - * name = "Foo" type = "text" |
|
| 398 | - * |
|
| 399 | - * @param array $attributes associative array to convert to a string attribute. |
|
| 400 | - * |
|
| 401 | - * @return string string of the HTML attribute. |
|
| 402 | - */ |
|
| 403 | - function attributes_to_string(array $attributes){ |
|
| 404 | - $str = ' '; |
|
| 405 | - //we check that the array passed as an argument is not empty. |
|
| 406 | - if (! empty($attributes)){ |
|
| 407 | - foreach($attributes as $key => $value){ |
|
| 408 | - $key = trim(htmlspecialchars($key)); |
|
| 409 | - $value = trim(htmlspecialchars($value)); |
|
| 410 | - /* |
|
| 391 | + /** |
|
| 392 | + * Convert array attributes to string |
|
| 393 | + * |
|
| 394 | + * This function converts an associative array into HTML attributes. |
|
| 395 | + * For example : |
|
| 396 | + * $a = array('name' => 'Foo', 'type' => 'text'); => produces the following string: |
|
| 397 | + * name = "Foo" type = "text" |
|
| 398 | + * |
|
| 399 | + * @param array $attributes associative array to convert to a string attribute. |
|
| 400 | + * |
|
| 401 | + * @return string string of the HTML attribute. |
|
| 402 | + */ |
|
| 403 | + function attributes_to_string(array $attributes){ |
|
| 404 | + $str = ' '; |
|
| 405 | + //we check that the array passed as an argument is not empty. |
|
| 406 | + if (! empty($attributes)){ |
|
| 407 | + foreach($attributes as $key => $value){ |
|
| 408 | + $key = trim(htmlspecialchars($key)); |
|
| 409 | + $value = trim(htmlspecialchars($value)); |
|
| 410 | + /* |
|
| 411 | 411 | * To predict the case where the string to convert contains the character " |
| 412 | 412 | * we check if this is the case we add a slash to solve this problem. |
| 413 | 413 | * For example: |
| 414 | 414 | * $attr = array('placeholder' => 'I am a "puple"') |
| 415 | 415 | * $str = attributes_to_string($attr); => placeholder = "I am a \"puple\"" |
| 416 | 416 | */ |
| 417 | - if ($value && strpos('"', $value) !== false){ |
|
| 418 | - $value = addslashes($value); |
|
| 419 | - } |
|
| 420 | - $str .= $key.' = "'.$value.'" '; |
|
| 421 | - } |
|
| 422 | - } |
|
| 423 | - //remove the space after using rtrim() |
|
| 424 | - return rtrim($str); |
|
| 425 | - } |
|
| 417 | + if ($value && strpos('"', $value) !== false){ |
|
| 418 | + $value = addslashes($value); |
|
| 419 | + } |
|
| 420 | + $str .= $key.' = "'.$value.'" '; |
|
| 421 | + } |
|
| 422 | + } |
|
| 423 | + //remove the space after using rtrim() |
|
| 424 | + return rtrim($str); |
|
| 425 | + } |
|
| 426 | 426 | |
| 427 | 427 | |
| 428 | - /** |
|
| 429 | - * Function to stringfy PHP variable, useful in debug situation |
|
| 430 | - * |
|
| 431 | - * @param mixed $var the variable to stringfy |
|
| 432 | - * @codeCoverageIgnore |
|
| 433 | - * |
|
| 434 | - * @return string the stringfy value |
|
| 435 | - */ |
|
| 436 | - function stringfy_vars($var){ |
|
| 437 | - return print_r($var, true); |
|
| 438 | - } |
|
| 428 | + /** |
|
| 429 | + * Function to stringfy PHP variable, useful in debug situation |
|
| 430 | + * |
|
| 431 | + * @param mixed $var the variable to stringfy |
|
| 432 | + * @codeCoverageIgnore |
|
| 433 | + * |
|
| 434 | + * @return string the stringfy value |
|
| 435 | + */ |
|
| 436 | + function stringfy_vars($var){ |
|
| 437 | + return print_r($var, true); |
|
| 438 | + } |
|
| 439 | 439 | |
| 440 | - /** |
|
| 441 | - * Clean the user input |
|
| 442 | - * @param mixed $str the value to clean |
|
| 443 | - * @test |
|
| 444 | - * |
|
| 445 | - * @return mixed the sanitize value |
|
| 446 | - */ |
|
| 447 | - function clean_input($str){ |
|
| 448 | - if (is_array($str)){ |
|
| 449 | - $str = array_map('clean_input', $str); |
|
| 450 | - } |
|
| 451 | - else if (is_object($str)){ |
|
| 452 | - $obj = $str; |
|
| 453 | - foreach ($str as $var => $value) { |
|
| 454 | - $obj->$var = clean_input($value); |
|
| 455 | - } |
|
| 456 | - $str = $obj; |
|
| 457 | - } |
|
| 458 | - else{ |
|
| 459 | - $str = htmlspecialchars(strip_tags($str), ENT_QUOTES, 'UTF-8'); |
|
| 460 | - } |
|
| 461 | - return $str; |
|
| 462 | - } |
|
| 440 | + /** |
|
| 441 | + * Clean the user input |
|
| 442 | + * @param mixed $str the value to clean |
|
| 443 | + * @test |
|
| 444 | + * |
|
| 445 | + * @return mixed the sanitize value |
|
| 446 | + */ |
|
| 447 | + function clean_input($str){ |
|
| 448 | + if (is_array($str)){ |
|
| 449 | + $str = array_map('clean_input', $str); |
|
| 450 | + } |
|
| 451 | + else if (is_object($str)){ |
|
| 452 | + $obj = $str; |
|
| 453 | + foreach ($str as $var => $value) { |
|
| 454 | + $obj->$var = clean_input($value); |
|
| 455 | + } |
|
| 456 | + $str = $obj; |
|
| 457 | + } |
|
| 458 | + else{ |
|
| 459 | + $str = htmlspecialchars(strip_tags($str), ENT_QUOTES, 'UTF-8'); |
|
| 460 | + } |
|
| 461 | + return $str; |
|
| 462 | + } |
|
| 463 | 463 | |
| 464 | - /** |
|
| 465 | - * This function is used to hidden some part of the given string. Helpful if you need hide some confidential |
|
| 466 | - * Information like credit card number, password, etc. |
|
| 467 | - * |
|
| 468 | - * @param string $str the string you want to hide some part |
|
| 469 | - * @param int $startCount the length of non hidden for the beginning char |
|
| 470 | - * @param int $endCount the length of non hidden for the ending char |
|
| 471 | - * @param string $hiddenChar the char used to hide the given string |
|
| 472 | - * @test |
|
| 473 | - * |
|
| 474 | - * @return string the string with the hidden part. |
|
| 475 | - */ |
|
| 476 | - function string_hidden($str, $startCount = 0, $endCount = 0, $hiddenChar = '*'){ |
|
| 477 | - //get the string length |
|
| 478 | - $len = strlen($str); |
|
| 479 | - //if str is empty |
|
| 480 | - if ($len <= 0){ |
|
| 481 | - return str_repeat($hiddenChar, 6); |
|
| 482 | - } |
|
| 483 | - //if the length is less than startCount and endCount |
|
| 484 | - //or the startCount and endCount length is 0 |
|
| 485 | - //or startCount is negative or endCount is negative |
|
| 486 | - //return the full string hidden |
|
| 464 | + /** |
|
| 465 | + * This function is used to hidden some part of the given string. Helpful if you need hide some confidential |
|
| 466 | + * Information like credit card number, password, etc. |
|
| 467 | + * |
|
| 468 | + * @param string $str the string you want to hide some part |
|
| 469 | + * @param int $startCount the length of non hidden for the beginning char |
|
| 470 | + * @param int $endCount the length of non hidden for the ending char |
|
| 471 | + * @param string $hiddenChar the char used to hide the given string |
|
| 472 | + * @test |
|
| 473 | + * |
|
| 474 | + * @return string the string with the hidden part. |
|
| 475 | + */ |
|
| 476 | + function string_hidden($str, $startCount = 0, $endCount = 0, $hiddenChar = '*'){ |
|
| 477 | + //get the string length |
|
| 478 | + $len = strlen($str); |
|
| 479 | + //if str is empty |
|
| 480 | + if ($len <= 0){ |
|
| 481 | + return str_repeat($hiddenChar, 6); |
|
| 482 | + } |
|
| 483 | + //if the length is less than startCount and endCount |
|
| 484 | + //or the startCount and endCount length is 0 |
|
| 485 | + //or startCount is negative or endCount is negative |
|
| 486 | + //return the full string hidden |
|
| 487 | 487 | |
| 488 | - if ((($startCount + $endCount) > $len) || ($startCount == 0 && $endCount == 0) || ($startCount < 0 || $endCount < 0)){ |
|
| 489 | - return str_repeat($hiddenChar, $len); |
|
| 490 | - } |
|
| 491 | - //the start non hidden string |
|
| 492 | - $startNonHiddenStr = substr($str, 0, $startCount); |
|
| 493 | - //the end non hidden string |
|
| 494 | - $endNonHiddenStr = null; |
|
| 495 | - if ($endCount > 0){ |
|
| 496 | - $endNonHiddenStr = substr($str, - $endCount); |
|
| 497 | - } |
|
| 498 | - //the hidden string |
|
| 499 | - $hiddenStr = str_repeat($hiddenChar, $len - ($startCount + $endCount)); |
|
| 488 | + if ((($startCount + $endCount) > $len) || ($startCount == 0 && $endCount == 0) || ($startCount < 0 || $endCount < 0)){ |
|
| 489 | + return str_repeat($hiddenChar, $len); |
|
| 490 | + } |
|
| 491 | + //the start non hidden string |
|
| 492 | + $startNonHiddenStr = substr($str, 0, $startCount); |
|
| 493 | + //the end non hidden string |
|
| 494 | + $endNonHiddenStr = null; |
|
| 495 | + if ($endCount > 0){ |
|
| 496 | + $endNonHiddenStr = substr($str, - $endCount); |
|
| 497 | + } |
|
| 498 | + //the hidden string |
|
| 499 | + $hiddenStr = str_repeat($hiddenChar, $len - ($startCount + $endCount)); |
|
| 500 | 500 | |
| 501 | - return $startNonHiddenStr . $hiddenStr . $endNonHiddenStr; |
|
| 502 | - } |
|
| 501 | + return $startNonHiddenStr . $hiddenStr . $endNonHiddenStr; |
|
| 502 | + } |
|
| 503 | 503 | |
| 504 | - /** |
|
| 505 | - * This function is used to set the initial session config regarding the configuration |
|
| 506 | - * @codeCoverageIgnore |
|
| 507 | - */ |
|
| 508 | - function set_session_config(){ |
|
| 509 | - //$_SESSION is not available on cli mode |
|
| 510 | - if (! IS_CLI){ |
|
| 511 | - $logger =& class_loader('Log', 'classes'); |
|
| 512 | - $logger->setLogger('PHPSession'); |
|
| 513 | - //set session params |
|
| 514 | - $sessionHandler = get_config('session_handler', 'files'); //the default is to store in the files |
|
| 515 | - $sessionName = get_config('session_name'); |
|
| 516 | - if ($sessionName){ |
|
| 517 | - session_name($sessionName); |
|
| 518 | - } |
|
| 519 | - $logger->info('Session handler: ' . $sessionHandler); |
|
| 520 | - $logger->info('Session name: ' . $sessionName); |
|
| 504 | + /** |
|
| 505 | + * This function is used to set the initial session config regarding the configuration |
|
| 506 | + * @codeCoverageIgnore |
|
| 507 | + */ |
|
| 508 | + function set_session_config(){ |
|
| 509 | + //$_SESSION is not available on cli mode |
|
| 510 | + if (! IS_CLI){ |
|
| 511 | + $logger =& class_loader('Log', 'classes'); |
|
| 512 | + $logger->setLogger('PHPSession'); |
|
| 513 | + //set session params |
|
| 514 | + $sessionHandler = get_config('session_handler', 'files'); //the default is to store in the files |
|
| 515 | + $sessionName = get_config('session_name'); |
|
| 516 | + if ($sessionName){ |
|
| 517 | + session_name($sessionName); |
|
| 518 | + } |
|
| 519 | + $logger->info('Session handler: ' . $sessionHandler); |
|
| 520 | + $logger->info('Session name: ' . $sessionName); |
|
| 521 | 521 | |
| 522 | - if ($sessionHandler == 'files'){ |
|
| 523 | - $sessionSavePath = get_config('session_save_path'); |
|
| 524 | - if ($sessionSavePath){ |
|
| 525 | - if (! is_dir($sessionSavePath)){ |
|
| 526 | - mkdir($sessionSavePath, 1773); |
|
| 527 | - } |
|
| 528 | - session_save_path($sessionSavePath); |
|
| 529 | - $logger->info('Session save path: ' . $sessionSavePath); |
|
| 530 | - } |
|
| 531 | - } |
|
| 532 | - else if ($sessionHandler == 'database'){ |
|
| 533 | - //load database session handle library |
|
| 534 | - //Database Session handler Model |
|
| 535 | - require_once CORE_CLASSES_MODEL_PATH . 'DBSessionHandlerModel.php'; |
|
| 522 | + if ($sessionHandler == 'files'){ |
|
| 523 | + $sessionSavePath = get_config('session_save_path'); |
|
| 524 | + if ($sessionSavePath){ |
|
| 525 | + if (! is_dir($sessionSavePath)){ |
|
| 526 | + mkdir($sessionSavePath, 1773); |
|
| 527 | + } |
|
| 528 | + session_save_path($sessionSavePath); |
|
| 529 | + $logger->info('Session save path: ' . $sessionSavePath); |
|
| 530 | + } |
|
| 531 | + } |
|
| 532 | + else if ($sessionHandler == 'database'){ |
|
| 533 | + //load database session handle library |
|
| 534 | + //Database Session handler Model |
|
| 535 | + require_once CORE_CLASSES_MODEL_PATH . 'DBSessionHandlerModel.php'; |
|
| 536 | 536 | |
| 537 | - $DBS =& class_loader('DBSessionHandler', 'classes'); |
|
| 538 | - session_set_save_handler($DBS, true); |
|
| 539 | - $logger->info('session save path: ' . get_config('session_save_path')); |
|
| 540 | - } |
|
| 541 | - else{ |
|
| 542 | - show_error('Invalid session handler configuration'); |
|
| 543 | - } |
|
| 544 | - $lifetime = get_config('session_cookie_lifetime', 0); |
|
| 545 | - $path = get_config('session_cookie_path', '/'); |
|
| 546 | - $domain = get_config('session_cookie_domain', ''); |
|
| 547 | - $secure = get_config('session_cookie_secure', false); |
|
| 548 | - session_set_cookie_params( |
|
| 549 | - $lifetime, |
|
| 550 | - $path, |
|
| 551 | - $domain, |
|
| 552 | - $secure, |
|
| 553 | - $httponly = true /*for security for access to cookie via javascript or XSS attack*/ |
|
| 554 | - ); |
|
| 555 | - //to prevent attack of Session Fixation |
|
| 556 | - //thank to https://www.phparch.com/2018/01/php-sessions-in-depth/ |
|
| 557 | - ini_set('session.use_strict_mode ', 1); |
|
| 558 | - ini_set('session.use_only_cookies', 1); |
|
| 559 | - ini_set('session.use_trans_sid ', 0); |
|
| 537 | + $DBS =& class_loader('DBSessionHandler', 'classes'); |
|
| 538 | + session_set_save_handler($DBS, true); |
|
| 539 | + $logger->info('session save path: ' . get_config('session_save_path')); |
|
| 540 | + } |
|
| 541 | + else{ |
|
| 542 | + show_error('Invalid session handler configuration'); |
|
| 543 | + } |
|
| 544 | + $lifetime = get_config('session_cookie_lifetime', 0); |
|
| 545 | + $path = get_config('session_cookie_path', '/'); |
|
| 546 | + $domain = get_config('session_cookie_domain', ''); |
|
| 547 | + $secure = get_config('session_cookie_secure', false); |
|
| 548 | + session_set_cookie_params( |
|
| 549 | + $lifetime, |
|
| 550 | + $path, |
|
| 551 | + $domain, |
|
| 552 | + $secure, |
|
| 553 | + $httponly = true /*for security for access to cookie via javascript or XSS attack*/ |
|
| 554 | + ); |
|
| 555 | + //to prevent attack of Session Fixation |
|
| 556 | + //thank to https://www.phparch.com/2018/01/php-sessions-in-depth/ |
|
| 557 | + ini_set('session.use_strict_mode ', 1); |
|
| 558 | + ini_set('session.use_only_cookies', 1); |
|
| 559 | + ini_set('session.use_trans_sid ', 0); |
|
| 560 | 560 | |
| 561 | - $logger->info('Session lifetime: ' . $lifetime); |
|
| 562 | - $logger->info('Session cookie path: ' . $path); |
|
| 563 | - $logger->info('Session domain: ' . $domain); |
|
| 564 | - $logger->info('Session is secure: ' . ($secure ? 'TRUE':'FALSE')); |
|
| 561 | + $logger->info('Session lifetime: ' . $lifetime); |
|
| 562 | + $logger->info('Session cookie path: ' . $path); |
|
| 563 | + $logger->info('Session domain: ' . $domain); |
|
| 564 | + $logger->info('Session is secure: ' . ($secure ? 'TRUE':'FALSE')); |
|
| 565 | 565 | |
| 566 | - if ((function_exists('session_status') && session_status() !== PHP_SESSION_ACTIVE) || !session_id()){ |
|
| 567 | - $logger->info('Session not yet start, start it now'); |
|
| 568 | - session_start(); |
|
| 569 | - } |
|
| 570 | - } |
|
| 571 | - } |
|
| 566 | + if ((function_exists('session_status') && session_status() !== PHP_SESSION_ACTIVE) || !session_id()){ |
|
| 567 | + $logger->info('Session not yet start, start it now'); |
|
| 568 | + session_start(); |
|
| 569 | + } |
|
| 570 | + } |
|
| 571 | + } |
|
| 572 | 572 | |
| 573 | - /** |
|
| 574 | - * This function is very useful, it allows to recover the instance of the global controller. |
|
| 575 | - * Note this function always returns the address of the super instance. |
|
| 576 | - * For example : |
|
| 577 | - * $obj = & get_instance(); |
|
| 578 | - * |
|
| 579 | - * @codeCoverageIgnore |
|
| 580 | - * |
|
| 581 | - * @return object the instance of the "Controller" class |
|
| 582 | - */ |
|
| 583 | - function & get_instance(){ |
|
| 584 | - return Controller::get_instance(); |
|
| 585 | - } |
|
| 573 | + /** |
|
| 574 | + * This function is very useful, it allows to recover the instance of the global controller. |
|
| 575 | + * Note this function always returns the address of the super instance. |
|
| 576 | + * For example : |
|
| 577 | + * $obj = & get_instance(); |
|
| 578 | + * |
|
| 579 | + * @codeCoverageIgnore |
|
| 580 | + * |
|
| 581 | + * @return object the instance of the "Controller" class |
|
| 582 | + */ |
|
| 583 | + function & get_instance(){ |
|
| 584 | + return Controller::get_instance(); |
|
| 585 | + } |
|
@@ -1,9 +1,9 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | defined('ROOT_PATH') || exit('Access denied'); |
| 3 | - /** |
|
| 4 | - * Form validation language message (English) |
|
| 5 | - */ |
|
| 6 | - $lang['fv_required'] = 'Field %1 is required.'; |
|
| 3 | + /** |
|
| 4 | + * Form validation language message (English) |
|
| 5 | + */ |
|
| 6 | + $lang['fv_required'] = 'Field %1 is required.'; |
|
| 7 | 7 | $lang['fv_min_length'] = 'Field %1 must contain at least %2 characters.'; |
| 8 | 8 | $lang['fv_max_length'] = 'Field %1 must contain at most %2 characters.'; |
| 9 | 9 | $lang['fv_exact_length'] = 'Field %1 must contain exactly %2 characters.'; |
@@ -14,8 +14,8 @@ discard block |
||
| 14 | 14 | $lang['fv_not_equal_post_key'] = 'Field %1 must not be the same as field %2.'; |
| 15 | 15 | $lang['fv_not_equal_string'] = 'Field %1 must not contain the value %2.'; |
| 16 | 16 | $lang['fv_depends'] = 'Field %1 depends on field %2 which is not valid.'; |
| 17 | - $lang['fv_is_unique'] = 'The value of field %1 already exists.'; |
|
| 18 | - $lang['fv_is_unique_update'] = 'The value of field %1 already exists for another record.'; |
|
| 17 | + $lang['fv_is_unique'] = 'The value of field %1 already exists.'; |
|
| 18 | + $lang['fv_is_unique_update'] = 'The value of field %1 already exists for another record.'; |
|
| 19 | 19 | $lang['fv_exists'] = 'The value of the field %1 does not exist.'; |
| 20 | 20 | $lang['fv_regex'] = 'The value of the field %1 does not use the correct format.'; |
| 21 | 21 | $lang['fv_in_list'] = 'The value of field %1 must be one of the list (%2).'; |
@@ -1,9 +1,9 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | defined('ROOT_PATH') || exit('Access denied'); |
| 3 | - /** |
|
| 4 | - * File upload language messages (English) |
|
| 5 | - */ |
|
| 6 | - $lang['fu_upload_err_ini_size'] = 'The uploaded file exceeds the upload_max_filesize directive in php.ini.'; |
|
| 3 | + /** |
|
| 4 | + * File upload language messages (English) |
|
| 5 | + */ |
|
| 6 | + $lang['fu_upload_err_ini_size'] = 'The uploaded file exceeds the upload_max_filesize directive in php.ini.'; |
|
| 7 | 7 | $lang['fu_upload_err_form_size'] = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.'; |
| 8 | 8 | $lang['fu_upload_err_partial'] = 'The uploaded file was only partially uploaded.'; |
| 9 | 9 | $lang['fu_upload_err_no_file'] = 'No file was choosed. Please select one.'; |
@@ -1,90 +1,90 @@ 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 DatabaseQueryRunner{ |
|
| 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 DatabaseQueryRunner{ |
|
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | - * The logger instance |
|
| 30 | - * @var object |
|
| 31 | - */ |
|
| 29 | + * The logger instance |
|
| 30 | + * @var object |
|
| 31 | + */ |
|
| 32 | 32 | private $logger = null; |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * The last query result |
|
| 36 | - * @var object |
|
| 37 | - */ |
|
| 38 | - private $queryResult = null; |
|
| 34 | + /** |
|
| 35 | + * The last query result |
|
| 36 | + * @var object |
|
| 37 | + */ |
|
| 38 | + private $queryResult = null; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * The benchmark instance |
|
| 42 | - * @var object |
|
| 43 | - */ |
|
| 40 | + /** |
|
| 41 | + * The benchmark instance |
|
| 42 | + * @var object |
|
| 43 | + */ |
|
| 44 | 44 | private $benchmarkInstance = null; |
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | - * The SQL query statment to execute |
|
| 48 | - * @var string |
|
| 49 | - */ |
|
| 47 | + * The SQL query statment to execute |
|
| 48 | + * @var string |
|
| 49 | + */ |
|
| 50 | 50 | private $query = null; |
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | - * Indicate if we need return result as list (boolean) |
|
| 53 | + * Indicate if we need return result as list (boolean) |
|
| 54 | 54 | * or the data used to replace the placeholder (array) |
| 55 | - * @var array|boolean |
|
| 56 | - */ |
|
| 57 | - private $returnAsList = true; |
|
| 55 | + * @var array|boolean |
|
| 56 | + */ |
|
| 57 | + private $returnAsList = true; |
|
| 58 | 58 | |
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Indicate if we need return result as array or not |
|
| 62 | - * @var boolean |
|
| 63 | - */ |
|
| 64 | - private $returnAsArray = true; |
|
| 60 | + /** |
|
| 61 | + * Indicate if we need return result as array or not |
|
| 62 | + * @var boolean |
|
| 63 | + */ |
|
| 64 | + private $returnAsArray = true; |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * The last PDOStatment instance |
|
| 68 | - * @var object |
|
| 69 | - */ |
|
| 70 | - private $pdoStatment = null; |
|
| 66 | + /** |
|
| 67 | + * The last PDOStatment instance |
|
| 68 | + * @var object |
|
| 69 | + */ |
|
| 70 | + private $pdoStatment = null; |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * The error returned for the last query |
|
| 74 | - * @var string |
|
| 75 | - */ |
|
| 76 | - private $error = null; |
|
| 72 | + /** |
|
| 73 | + * The error returned for the last query |
|
| 74 | + * @var string |
|
| 75 | + */ |
|
| 76 | + private $error = null; |
|
| 77 | 77 | |
| 78 | 78 | /** |
| 79 | 79 | * The PDO instance |
| 80 | 80 | * @var object |
| 81 | - */ |
|
| 81 | + */ |
|
| 82 | 82 | private $pdo = null; |
| 83 | 83 | |
| 84 | 84 | /** |
| 85 | 85 | * The database driver name used |
| 86 | 86 | * @var string |
| 87 | - */ |
|
| 87 | + */ |
|
| 88 | 88 | private $driver = null; |
| 89 | 89 | |
| 90 | 90 | |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | */ |
| 99 | 99 | public function __construct(PDO $pdo = null, $query = null, $returnAsList = true, $returnAsArray = false){ |
| 100 | 100 | if (is_object($pdo)){ |
| 101 | - $this->pdo = $pdo; |
|
| 101 | + $this->pdo = $pdo; |
|
| 102 | 102 | } |
| 103 | 103 | $this->query = $query; |
| 104 | 104 | $this->returnAsList = $returnAsList; |
@@ -116,10 +116,10 @@ discard block |
||
| 116 | 116 | //reset instance |
| 117 | 117 | $this->reset(); |
| 118 | 118 | |
| 119 | - //for database query execution time |
|
| 119 | + //for database query execution time |
|
| 120 | 120 | $benchmarkMarkerKey = $this->getBenchmarkKey(); |
| 121 | 121 | if (! is_object($this->benchmarkInstance)){ |
| 122 | - $this->benchmarkInstance = & class_loader('Benchmark'); |
|
| 122 | + $this->benchmarkInstance = & class_loader('Benchmark'); |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | $this->logger->info('Execute SQL query [' . $this->query . ']'); |
@@ -132,101 +132,101 @@ discard block |
||
| 132 | 132 | $responseTime = $this->benchmarkInstance->elapsedTime( |
| 133 | 133 | 'DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', |
| 134 | 134 | 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')' |
| 135 | - ); |
|
| 136 | - //TODO use the configuration value for the high response time currently is 1 second |
|
| 135 | + ); |
|
| 136 | + //TODO use the configuration value for the high response time currently is 1 second |
|
| 137 | 137 | if ($responseTime >= 1 ){ |
| 138 | 138 | $this->logger->warning( |
| 139 | 139 | 'High response time while processing database query [' . $this->query . ']. |
| 140 | 140 | The response time is [' .$responseTime. '] sec.' |
| 141 | - ); |
|
| 141 | + ); |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | if ($this->pdoStatment !== false){ |
| 145 | - $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
|
| 146 | - if($isSqlSELECTQuery){ |
|
| 147 | - $this->setResultForSelect(); |
|
| 148 | - } |
|
| 149 | - else{ |
|
| 150 | - $this->setResultForNonSelect(); |
|
| 151 | - } |
|
| 152 | - return $this->queryResult; |
|
| 145 | + $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
|
| 146 | + if($isSqlSELECTQuery){ |
|
| 147 | + $this->setResultForSelect(); |
|
| 148 | + } |
|
| 149 | + else{ |
|
| 150 | + $this->setResultForNonSelect(); |
|
| 151 | + } |
|
| 152 | + return $this->queryResult; |
|
| 153 | 153 | } |
| 154 | 154 | $this->setQueryRunnerError(); |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | - /** |
|
| 158 | - * Return the result for SELECT query |
|
| 159 | - * @see DatabaseQueryRunner::execute |
|
| 160 | - */ |
|
| 157 | + /** |
|
| 158 | + * Return the result for SELECT query |
|
| 159 | + * @see DatabaseQueryRunner::execute |
|
| 160 | + */ |
|
| 161 | 161 | protected function setResultForSelect(){ |
| 162 | - //if need return all result like list of record |
|
| 163 | - $result = null; |
|
| 164 | - $numRows = 0; |
|
| 165 | - $fetchMode = PDO::FETCH_OBJ; |
|
| 166 | - if($this->returnAsArray){ |
|
| 162 | + //if need return all result like list of record |
|
| 163 | + $result = null; |
|
| 164 | + $numRows = 0; |
|
| 165 | + $fetchMode = PDO::FETCH_OBJ; |
|
| 166 | + if($this->returnAsArray){ |
|
| 167 | 167 | $fetchMode = PDO::FETCH_ASSOC; |
| 168 | - } |
|
| 169 | - if ($this->returnAsList){ |
|
| 170 | - $result = $this->pdoStatment->fetchAll($fetchMode); |
|
| 171 | - } |
|
| 172 | - else{ |
|
| 173 | - $result = $this->pdoStatment->fetch($fetchMode); |
|
| 174 | - } |
|
| 175 | - //Sqlite and pgsql always return 0 when using rowCount() |
|
| 176 | - if (in_array($this->driver, array('sqlite', 'pgsql'))){ |
|
| 168 | + } |
|
| 169 | + if ($this->returnAsList){ |
|
| 170 | + $result = $this->pdoStatment->fetchAll($fetchMode); |
|
| 171 | + } |
|
| 172 | + else{ |
|
| 173 | + $result = $this->pdoStatment->fetch($fetchMode); |
|
| 174 | + } |
|
| 175 | + //Sqlite and pgsql always return 0 when using rowCount() |
|
| 176 | + if (in_array($this->driver, array('sqlite', 'pgsql'))){ |
|
| 177 | 177 | $numRows = count($result); |
| 178 | - } |
|
| 179 | - else{ |
|
| 178 | + } |
|
| 179 | + else{ |
|
| 180 | 180 | $numRows = $this->pdoStatment->rowCount(); |
| 181 | - } |
|
| 182 | - if(! is_object($this->queryResult)){ |
|
| 183 | - $this->queryResult = & class_loader('DatabaseQueryResult', 'classes/database'); |
|
| 184 | - } |
|
| 185 | - $this->queryResult->setResult($result); |
|
| 186 | - $this->queryResult->setNumRows($numRows); |
|
| 181 | + } |
|
| 182 | + if(! is_object($this->queryResult)){ |
|
| 183 | + $this->queryResult = & class_loader('DatabaseQueryResult', 'classes/database'); |
|
| 184 | + } |
|
| 185 | + $this->queryResult->setResult($result); |
|
| 186 | + $this->queryResult->setNumRows($numRows); |
|
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | /** |
| 190 | - * Return the result for non SELECT query |
|
| 191 | - * @see DatabaseQueryRunner::execute |
|
| 192 | - */ |
|
| 190 | + * Return the result for non SELECT query |
|
| 191 | + * @see DatabaseQueryRunner::execute |
|
| 192 | + */ |
|
| 193 | 193 | protected function setResultForNonSelect(){ |
| 194 | - //Sqlite and pgsql always return 0 when using rowCount() |
|
| 195 | - $result = false; |
|
| 196 | - $numRows = 0; |
|
| 197 | - if (in_array($this->driver, array('sqlite', 'pgsql'))){ |
|
| 194 | + //Sqlite and pgsql always return 0 when using rowCount() |
|
| 195 | + $result = false; |
|
| 196 | + $numRows = 0; |
|
| 197 | + if (in_array($this->driver, array('sqlite', 'pgsql'))){ |
|
| 198 | 198 | $result = true; //to test the result for the query like UPDATE, INSERT, DELETE |
| 199 | 199 | $numRows = 1; //TODO use the correct method to get the exact affected row |
| 200 | - } |
|
| 201 | - else{ |
|
| 202 | - //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 203 | - $result = $this->pdoStatment->rowCount() >= 0; |
|
| 204 | - $numRows = $this->pdoStatment->rowCount(); |
|
| 205 | - } |
|
| 206 | - if(! is_object($this->queryResult)){ |
|
| 207 | - $this->queryResult = & class_loader('DatabaseQueryResult', 'classes/database'); |
|
| 208 | - } |
|
| 209 | - $this->queryResult->setResult($result); |
|
| 210 | - $this->queryResult->setNumRows($numRows); |
|
| 200 | + } |
|
| 201 | + else{ |
|
| 202 | + //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 203 | + $result = $this->pdoStatment->rowCount() >= 0; |
|
| 204 | + $numRows = $this->pdoStatment->rowCount(); |
|
| 205 | + } |
|
| 206 | + if(! is_object($this->queryResult)){ |
|
| 207 | + $this->queryResult = & class_loader('DatabaseQueryResult', 'classes/database'); |
|
| 208 | + } |
|
| 209 | + $this->queryResult->setResult($result); |
|
| 210 | + $this->queryResult->setNumRows($numRows); |
|
| 211 | 211 | } |
| 212 | 212 | |
| 213 | 213 | |
| 214 | - /** |
|
| 214 | + /** |
|
| 215 | 215 | * Return the benchmark instance |
| 216 | 216 | * @return Benchmark |
| 217 | 217 | */ |
| 218 | 218 | public function getBenchmark(){ |
| 219 | - return $this->benchmarkInstance; |
|
| 219 | + return $this->benchmarkInstance; |
|
| 220 | 220 | } |
| 221 | 221 | |
| 222 | 222 | /** |
| 223 | 223 | * Set the benchmark instance |
| 224 | 224 | * @param Benchmark $benchmark the benchmark object |
| 225 | - * @return object DatabaseQueryRunner |
|
| 225 | + * @return object DatabaseQueryRunner |
|
| 226 | 226 | */ |
| 227 | 227 | public function setBenchmark($benchmark){ |
| 228 | - $this->benchmarkInstance = $benchmark; |
|
| 229 | - return $this; |
|
| 228 | + $this->benchmarkInstance = $benchmark; |
|
| 229 | + return $this; |
|
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | /** |
@@ -235,18 +235,18 @@ discard block |
||
| 235 | 235 | * @return object DatabaseQueryResult |
| 236 | 236 | */ |
| 237 | 237 | public function getQueryResult(){ |
| 238 | - return $this->queryResult; |
|
| 238 | + return $this->queryResult; |
|
| 239 | 239 | } |
| 240 | 240 | |
| 241 | 241 | /** |
| 242 | 242 | * Set the database query result instance |
| 243 | 243 | * @param object $queryResult the query result |
| 244 | 244 | * |
| 245 | - * @return object DatabaseQueryRunner |
|
| 245 | + * @return object DatabaseQueryRunner |
|
| 246 | 246 | */ |
| 247 | 247 | public function setQueryResult(DatabaseQueryResult $queryResult){ |
| 248 | - $this->queryResult = $queryResult; |
|
| 249 | - return $this; |
|
| 248 | + $this->queryResult = $queryResult; |
|
| 249 | + return $this; |
|
| 250 | 250 | } |
| 251 | 251 | |
| 252 | 252 | /** |
@@ -254,17 +254,17 @@ discard block |
||
| 254 | 254 | * @return Log |
| 255 | 255 | */ |
| 256 | 256 | public function getLogger(){ |
| 257 | - return $this->logger; |
|
| 257 | + return $this->logger; |
|
| 258 | 258 | } |
| 259 | 259 | |
| 260 | 260 | /** |
| 261 | 261 | * Set the log instance |
| 262 | 262 | * @param Log $logger the log object |
| 263 | - * @return object DatabaseQueryRunner |
|
| 263 | + * @return object DatabaseQueryRunner |
|
| 264 | 264 | */ |
| 265 | 265 | public function setLogger($logger){ |
| 266 | - $this->logger = $logger; |
|
| 267 | - return $this; |
|
| 266 | + $this->logger = $logger; |
|
| 267 | + return $this; |
|
| 268 | 268 | } |
| 269 | 269 | |
| 270 | 270 | /** |
@@ -272,7 +272,7 @@ discard block |
||
| 272 | 272 | * @return string |
| 273 | 273 | */ |
| 274 | 274 | public function getQuery(){ |
| 275 | - return $this->query; |
|
| 275 | + return $this->query; |
|
| 276 | 276 | } |
| 277 | 277 | |
| 278 | 278 | /** |
@@ -281,8 +281,8 @@ discard block |
||
| 281 | 281 | * @return object DatabaseQueryRunner |
| 282 | 282 | */ |
| 283 | 283 | public function setQuery($query){ |
| 284 | - $this->query = $query; |
|
| 285 | - return $this; |
|
| 284 | + $this->query = $query; |
|
| 285 | + return $this; |
|
| 286 | 286 | } |
| 287 | 287 | |
| 288 | 288 | /** |
@@ -291,8 +291,8 @@ discard block |
||
| 291 | 291 | * @return object DatabaseQueryRunner |
| 292 | 292 | */ |
| 293 | 293 | public function setReturnType($returnType){ |
| 294 | - $this->returnAsList = $returnType; |
|
| 295 | - return $this; |
|
| 294 | + $this->returnAsList = $returnType; |
|
| 295 | + return $this; |
|
| 296 | 296 | } |
| 297 | 297 | |
| 298 | 298 | /** |
@@ -301,8 +301,8 @@ discard block |
||
| 301 | 301 | * @return object DatabaseQueryRunner |
| 302 | 302 | */ |
| 303 | 303 | public function setReturnAsArray($status = true){ |
| 304 | - $this->returnAsArray = $status; |
|
| 305 | - return $this; |
|
| 304 | + $this->returnAsArray = $status; |
|
| 305 | + return $this; |
|
| 306 | 306 | } |
| 307 | 307 | |
| 308 | 308 | /** |
@@ -310,7 +310,7 @@ discard block |
||
| 310 | 310 | * @return string |
| 311 | 311 | */ |
| 312 | 312 | public function getQueryError(){ |
| 313 | - return $this->error; |
|
| 313 | + return $this->error; |
|
| 314 | 314 | } |
| 315 | 315 | |
| 316 | 316 | /** |
@@ -318,7 +318,7 @@ discard block |
||
| 318 | 318 | * @return object |
| 319 | 319 | */ |
| 320 | 320 | public function getPdo(){ |
| 321 | - return $this->pdo; |
|
| 321 | + return $this->pdo; |
|
| 322 | 322 | } |
| 323 | 323 | |
| 324 | 324 | /** |
@@ -327,16 +327,16 @@ discard block |
||
| 327 | 327 | * @return object DatabaseQueryRunner |
| 328 | 328 | */ |
| 329 | 329 | public function setPdo(PDO $pdo = null){ |
| 330 | - $this->pdo = $pdo; |
|
| 331 | - return $this; |
|
| 330 | + $this->pdo = $pdo; |
|
| 331 | + return $this; |
|
| 332 | 332 | } |
| 333 | 333 | |
| 334 | - /** |
|
| 335 | - * Return the database driver |
|
| 336 | - * @return string |
|
| 337 | - */ |
|
| 334 | + /** |
|
| 335 | + * Return the database driver |
|
| 336 | + * @return string |
|
| 337 | + */ |
|
| 338 | 338 | public function getDriver(){ |
| 339 | - return $this->driver; |
|
| 339 | + return $this->driver; |
|
| 340 | 340 | } |
| 341 | 341 | |
| 342 | 342 | /** |
@@ -345,8 +345,8 @@ discard block |
||
| 345 | 345 | * @return object DatabaseQueryRunner |
| 346 | 346 | */ |
| 347 | 347 | public function setDriver($driver){ |
| 348 | - $this->driver = $driver; |
|
| 349 | - return $this; |
|
| 348 | + $this->driver = $driver; |
|
| 349 | + return $this; |
|
| 350 | 350 | } |
| 351 | 351 | |
| 352 | 352 | /** |
@@ -355,18 +355,18 @@ discard block |
||
| 355 | 355 | * @return string |
| 356 | 356 | */ |
| 357 | 357 | protected function getBenchmarkKey(){ |
| 358 | - return md5($this->query . $this->returnAsList . $this->returnAsArray); |
|
| 358 | + return md5($this->query . $this->returnAsList . $this->returnAsArray); |
|
| 359 | 359 | } |
| 360 | 360 | |
| 361 | 361 | /** |
| 362 | 362 | * Set error for database query execution |
| 363 | 363 | */ |
| 364 | 364 | protected function setQueryRunnerError(){ |
| 365 | - $error = $this->pdo->errorInfo(); |
|
| 366 | - $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 367 | - $this->logger->error('The database query execution got an error: ' . stringfy_vars($error)); |
|
| 368 | - //show error message |
|
| 369 | - show_error('Query: "' . $this->query . '" Error: ' . $this->error, 'Database Error'); |
|
| 365 | + $error = $this->pdo->errorInfo(); |
|
| 366 | + $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 367 | + $this->logger->error('The database query execution got an error: ' . stringfy_vars($error)); |
|
| 368 | + //show error message |
|
| 369 | + show_error('Query: "' . $this->query . '" Error: ' . $this->error, 'Database Error'); |
|
| 370 | 370 | } |
| 371 | 371 | |
| 372 | 372 | /** |
@@ -374,19 +374,19 @@ discard block |
||
| 374 | 374 | * @param object $logger the Log instance if not null |
| 375 | 375 | */ |
| 376 | 376 | protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
| 377 | - if ($logger !== null){ |
|
| 377 | + if ($logger !== null){ |
|
| 378 | 378 | $this->logger = $logger; |
| 379 | - } |
|
| 380 | - else{ |
|
| 381 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 382 | - $this->logger->setLogger('Library::DatabaseQueryRunner'); |
|
| 383 | - } |
|
| 379 | + } |
|
| 380 | + else{ |
|
| 381 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 382 | + $this->logger->setLogger('Library::DatabaseQueryRunner'); |
|
| 383 | + } |
|
| 384 | 384 | } |
| 385 | 385 | |
| 386 | 386 | |
| 387 | 387 | /** |
| 388 | - * Reset the instance before run each query |
|
| 389 | - */ |
|
| 388 | + * Reset the instance before run each query |
|
| 389 | + */ |
|
| 390 | 390 | private function reset(){ |
| 391 | 391 | $this->error = null; |
| 392 | 392 | $this->pdoStatment = null; |
@@ -1,119 +1,119 @@ 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 Database{ |
|
| 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 Database{ |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * The PDO instance |
|
| 30 | - * @var object |
|
| 31 | - */ |
|
| 28 | + /** |
|
| 29 | + * The PDO instance |
|
| 30 | + * @var object |
|
| 31 | + */ |
|
| 32 | 32 | private $pdo = null; |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * The database name used for the application |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - private $databaseName = null; |
|
| 34 | + /** |
|
| 35 | + * The database name used for the application |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + private $databaseName = null; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * The number of rows returned by the last query |
|
| 42 | - * @var int |
|
| 43 | - */ |
|
| 40 | + /** |
|
| 41 | + * The number of rows returned by the last query |
|
| 42 | + * @var int |
|
| 43 | + */ |
|
| 44 | 44 | private $numRows = 0; |
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * The last insert id for the primary key column that have auto increment or sequence |
|
| 48 | - * @var mixed |
|
| 49 | - */ |
|
| 46 | + /** |
|
| 47 | + * The last insert id for the primary key column that have auto increment or sequence |
|
| 48 | + * @var mixed |
|
| 49 | + */ |
|
| 50 | 50 | private $insertId = null; |
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * The full SQL query statment after build for each command |
|
| 54 | - * @var string |
|
| 55 | - */ |
|
| 52 | + /** |
|
| 53 | + * The full SQL query statment after build for each command |
|
| 54 | + * @var string |
|
| 55 | + */ |
|
| 56 | 56 | private $query = null; |
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * The result returned for the last query |
|
| 60 | - * @var mixed |
|
| 61 | - */ |
|
| 58 | + /** |
|
| 59 | + * The result returned for the last query |
|
| 60 | + * @var mixed |
|
| 61 | + */ |
|
| 62 | 62 | private $result = array(); |
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * The cache default time to live in second. 0 means no need to use the cache feature |
|
| 66 | - * @var int |
|
| 67 | - */ |
|
| 68 | - private $cacheTtl = 0; |
|
| 64 | + /** |
|
| 65 | + * The cache default time to live in second. 0 means no need to use the cache feature |
|
| 66 | + * @var int |
|
| 67 | + */ |
|
| 68 | + private $cacheTtl = 0; |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * The cache current time to live. 0 means no need to use the cache feature |
|
| 72 | - * @var int |
|
| 73 | - */ |
|
| 70 | + /** |
|
| 71 | + * The cache current time to live. 0 means no need to use the cache feature |
|
| 72 | + * @var int |
|
| 73 | + */ |
|
| 74 | 74 | private $temporaryCacheTtl = 0; |
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * The number of executed query for the current request |
|
| 78 | - * @var int |
|
| 79 | - */ |
|
| 76 | + /** |
|
| 77 | + * The number of executed query for the current request |
|
| 78 | + * @var int |
|
| 79 | + */ |
|
| 80 | 80 | private $queryCount = 0; |
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * The default data to be used in the statments query INSERT, UPDATE |
|
| 84 | - * @var array |
|
| 85 | - */ |
|
| 82 | + /** |
|
| 83 | + * The default data to be used in the statments query INSERT, UPDATE |
|
| 84 | + * @var array |
|
| 85 | + */ |
|
| 86 | 86 | private $data = array(); |
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * The database configuration |
|
| 90 | - * @var array |
|
| 91 | - */ |
|
| 88 | + /** |
|
| 89 | + * The database configuration |
|
| 90 | + * @var array |
|
| 91 | + */ |
|
| 92 | 92 | private $config = array(); |
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * The logger instance |
|
| 96 | - * @var object |
|
| 97 | - */ |
|
| 94 | + /** |
|
| 95 | + * The logger instance |
|
| 96 | + * @var object |
|
| 97 | + */ |
|
| 98 | 98 | private $logger = null; |
| 99 | 99 | |
| 100 | 100 | /** |
| 101 | - * The cache instance |
|
| 102 | - * @var object |
|
| 103 | - */ |
|
| 101 | + * The cache instance |
|
| 102 | + * @var object |
|
| 103 | + */ |
|
| 104 | 104 | private $cacheInstance = null; |
| 105 | 105 | |
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * The DatabaseQueryBuilder instance |
|
| 109 | - * @var object |
|
| 110 | - */ |
|
| 107 | + /** |
|
| 108 | + * The DatabaseQueryBuilder instance |
|
| 109 | + * @var object |
|
| 110 | + */ |
|
| 111 | 111 | private $queryBuilder = null; |
| 112 | 112 | |
| 113 | 113 | /** |
| 114 | - * The DatabaseQueryRunner instance |
|
| 115 | - * @var object |
|
| 116 | - */ |
|
| 114 | + * The DatabaseQueryRunner instance |
|
| 115 | + * @var object |
|
| 116 | + */ |
|
| 117 | 117 | private $queryRunner = null; |
| 118 | 118 | |
| 119 | 119 | |
@@ -125,8 +125,8 @@ discard block |
||
| 125 | 125 | //Set Log instance to use |
| 126 | 126 | $this->setLoggerFromParamOrCreateNewInstance(null); |
| 127 | 127 | |
| 128 | - //Set DatabaseQueryBuilder instance to use |
|
| 129 | - $this->setQueryBuilderFromParamOrCreateNewInstance(null); |
|
| 128 | + //Set DatabaseQueryBuilder instance to use |
|
| 129 | + $this->setQueryBuilderFromParamOrCreateNewInstance(null); |
|
| 130 | 130 | |
| 131 | 131 | //Set DatabaseQueryRunner instance to use |
| 132 | 132 | $this->setQueryRunnerFromParamOrCreateNewInstance(null); |
@@ -143,22 +143,22 @@ discard block |
||
| 143 | 143 | * @return bool |
| 144 | 144 | */ |
| 145 | 145 | public function connect(){ |
| 146 | - $config = $this->getDatabaseConfiguration(); |
|
| 147 | - if (! empty($config)){ |
|
| 146 | + $config = $this->getDatabaseConfiguration(); |
|
| 147 | + if (! empty($config)){ |
|
| 148 | 148 | try{ |
| 149 | 149 | $this->pdo = new PDO($this->getDsnFromDriver(), $config['username'], $config['password']); |
| 150 | 150 | $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
| 151 | 151 | $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
| 152 | 152 | $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
| 153 | 153 | return true; |
| 154 | - } |
|
| 155 | - catch (PDOException $e){ |
|
| 154 | + } |
|
| 155 | + catch (PDOException $e){ |
|
| 156 | 156 | $this->logger->fatal($e->getMessage()); |
| 157 | 157 | show_error('Cannot connect to Database.'); |
| 158 | 158 | return false; |
| 159 | - } |
|
| 160 | - } |
|
| 161 | - return false; |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + return false; |
|
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | |
@@ -167,7 +167,7 @@ discard block |
||
| 167 | 167 | * @return int |
| 168 | 168 | */ |
| 169 | 169 | public function numRows(){ |
| 170 | - return $this->numRows; |
|
| 170 | + return $this->numRows; |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | /** |
@@ -175,7 +175,7 @@ discard block |
||
| 175 | 175 | * @return mixed |
| 176 | 176 | */ |
| 177 | 177 | public function insertId(){ |
| 178 | - return $this->insertId; |
|
| 178 | + return $this->insertId; |
|
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | |
@@ -186,13 +186,13 @@ discard block |
||
| 186 | 186 | * @return mixed the query SQL string or the record result |
| 187 | 187 | */ |
| 188 | 188 | public function get($returnSQLQueryOrResultType = false){ |
| 189 | - $this->getQueryBuilder()->limit(1); |
|
| 190 | - $query = $this->getAll(true); |
|
| 191 | - if ($returnSQLQueryOrResultType === true){ |
|
| 189 | + $this->getQueryBuilder()->limit(1); |
|
| 190 | + $query = $this->getAll(true); |
|
| 191 | + if ($returnSQLQueryOrResultType === true){ |
|
| 192 | 192 | return $query; |
| 193 | - } else { |
|
| 193 | + } else { |
|
| 194 | 194 | return $this->query($query, false, $returnSQLQueryOrResultType == 'array'); |
| 195 | - } |
|
| 195 | + } |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | /** |
@@ -202,11 +202,11 @@ discard block |
||
| 202 | 202 | * @return mixed the query SQL string or the record result |
| 203 | 203 | */ |
| 204 | 204 | public function getAll($returnSQLQueryOrResultType = false){ |
| 205 | - $query = $this->getQueryBuilder()->getQuery(); |
|
| 206 | - if ($returnSQLQueryOrResultType === true){ |
|
| 207 | - return $query; |
|
| 208 | - } |
|
| 209 | - return $this->query($query, true, $returnSQLQueryOrResultType == 'array'); |
|
| 205 | + $query = $this->getQueryBuilder()->getQuery(); |
|
| 206 | + if ($returnSQLQueryOrResultType === true){ |
|
| 207 | + return $query; |
|
| 208 | + } |
|
| 209 | + return $this->query($query, true, $returnSQLQueryOrResultType == 'array'); |
|
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | /** |
@@ -216,19 +216,19 @@ discard block |
||
| 216 | 216 | * @return mixed the insert id of the new record or null |
| 217 | 217 | */ |
| 218 | 218 | public function insert($data = array(), $escape = true){ |
| 219 | - if (empty($data) && $this->getData()){ |
|
| 219 | + if (empty($data) && $this->getData()){ |
|
| 220 | 220 | //as when using $this->setData() may be the data already escaped |
| 221 | 221 | $escape = false; |
| 222 | 222 | $data = $this->getData(); |
| 223 | - } |
|
| 224 | - $query = $this->getQueryBuilder()->insert($data, $escape)->getQuery(); |
|
| 225 | - $result = $this->query($query); |
|
| 226 | - if ($result){ |
|
| 223 | + } |
|
| 224 | + $query = $this->getQueryBuilder()->insert($data, $escape)->getQuery(); |
|
| 225 | + $result = $this->query($query); |
|
| 226 | + if ($result){ |
|
| 227 | 227 | $this->insertId = $this->pdo->lastInsertId(); |
| 228 | - //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
|
| 228 | + //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
|
| 229 | 229 | return ! $this->insertId() ? true : $this->insertId(); |
| 230 | - } |
|
| 231 | - return false; |
|
| 230 | + } |
|
| 231 | + return false; |
|
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | /** |
@@ -238,13 +238,13 @@ discard block |
||
| 238 | 238 | * @return mixed the update status |
| 239 | 239 | */ |
| 240 | 240 | public function update($data = array(), $escape = true){ |
| 241 | - if (empty($data) && $this->getData()){ |
|
| 241 | + if (empty($data) && $this->getData()){ |
|
| 242 | 242 | //as when using $this->setData() may be the data already escaped |
| 243 | 243 | $escape = false; |
| 244 | 244 | $data = $this->getData(); |
| 245 | - } |
|
| 246 | - $query = $this->getQueryBuilder()->update($data, $escape)->getQuery(); |
|
| 247 | - return $this->query($query); |
|
| 245 | + } |
|
| 246 | + $query = $this->getQueryBuilder()->update($data, $escape)->getQuery(); |
|
| 247 | + return $this->query($query); |
|
| 248 | 248 | } |
| 249 | 249 | |
| 250 | 250 | /** |
@@ -252,8 +252,8 @@ discard block |
||
| 252 | 252 | * @return mixed the delete status |
| 253 | 253 | */ |
| 254 | 254 | public function delete(){ |
| 255 | - $query = $this->getQueryBuilder()->delete()->getQuery(); |
|
| 256 | - return $this->query($query); |
|
| 255 | + $query = $this->getQueryBuilder()->delete()->getQuery(); |
|
| 256 | + return $this->query($query); |
|
| 257 | 257 | } |
| 258 | 258 | |
| 259 | 259 | /** |
@@ -262,21 +262,21 @@ discard block |
||
| 262 | 262 | * @return object the current Database instance |
| 263 | 263 | */ |
| 264 | 264 | public function setCache($ttl = 0){ |
| 265 | - if ($ttl > 0){ |
|
| 265 | + if ($ttl > 0){ |
|
| 266 | 266 | $this->cacheTtl = $ttl; |
| 267 | 267 | $this->temporaryCacheTtl = $ttl; |
| 268 | - } |
|
| 269 | - return $this; |
|
| 268 | + } |
|
| 269 | + return $this; |
|
| 270 | 270 | } |
| 271 | 271 | |
| 272 | - /** |
|
| 273 | - * Enabled cache temporary for the current query not globally |
|
| 274 | - * @param integer $ttl the cache time to live in second |
|
| 275 | - * @return object the current Database instance |
|
| 276 | - */ |
|
| 277 | - public function cached($ttl = 0){ |
|
| 272 | + /** |
|
| 273 | + * Enabled cache temporary for the current query not globally |
|
| 274 | + * @param integer $ttl the cache time to live in second |
|
| 275 | + * @return object the current Database instance |
|
| 276 | + */ |
|
| 277 | + public function cached($ttl = 0){ |
|
| 278 | 278 | if ($ttl > 0){ |
| 279 | - $this->temporaryCacheTtl = $ttl; |
|
| 279 | + $this->temporaryCacheTtl = $ttl; |
|
| 280 | 280 | } |
| 281 | 281 | return $this; |
| 282 | 282 | } |
@@ -288,7 +288,7 @@ discard block |
||
| 288 | 288 | * @return mixed the data after escaped or the same data if not |
| 289 | 289 | */ |
| 290 | 290 | public function escape($data, $escaped = true){ |
| 291 | - return $escaped ? |
|
| 291 | + return $escaped ? |
|
| 292 | 292 | $this->pdo->quote(trim($data)) |
| 293 | 293 | : $data; |
| 294 | 294 | } |
@@ -298,7 +298,7 @@ discard block |
||
| 298 | 298 | * @return int |
| 299 | 299 | */ |
| 300 | 300 | public function queryCount(){ |
| 301 | - return $this->queryCount; |
|
| 301 | + return $this->queryCount; |
|
| 302 | 302 | } |
| 303 | 303 | |
| 304 | 304 | /** |
@@ -306,7 +306,7 @@ discard block |
||
| 306 | 306 | * @return string |
| 307 | 307 | */ |
| 308 | 308 | public function getQuery(){ |
| 309 | - return $this->query; |
|
| 309 | + return $this->query; |
|
| 310 | 310 | } |
| 311 | 311 | |
| 312 | 312 | /** |
@@ -314,7 +314,7 @@ discard block |
||
| 314 | 314 | * @return string |
| 315 | 315 | */ |
| 316 | 316 | public function getDatabaseName(){ |
| 317 | - return $this->databaseName; |
|
| 317 | + return $this->databaseName; |
|
| 318 | 318 | } |
| 319 | 319 | |
| 320 | 320 | /** |
@@ -322,17 +322,17 @@ discard block |
||
| 322 | 322 | * @return object |
| 323 | 323 | */ |
| 324 | 324 | public function getPdo(){ |
| 325 | - return $this->pdo; |
|
| 325 | + return $this->pdo; |
|
| 326 | 326 | } |
| 327 | 327 | |
| 328 | 328 | /** |
| 329 | 329 | * Set the PDO instance |
| 330 | 330 | * @param object $pdo the pdo object |
| 331 | - * @return object Database |
|
| 331 | + * @return object Database |
|
| 332 | 332 | */ |
| 333 | 333 | public function setPdo(PDO $pdo){ |
| 334 | - $this->pdo = $pdo; |
|
| 335 | - return $this; |
|
| 334 | + $this->pdo = $pdo; |
|
| 335 | + return $this; |
|
| 336 | 336 | } |
| 337 | 337 | |
| 338 | 338 | |
@@ -341,44 +341,44 @@ discard block |
||
| 341 | 341 | * @return Log |
| 342 | 342 | */ |
| 343 | 343 | public function getLogger(){ |
| 344 | - return $this->logger; |
|
| 344 | + return $this->logger; |
|
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | /** |
| 348 | 348 | * Set the log instance |
| 349 | 349 | * @param Log $logger the log object |
| 350 | - * @return object Database |
|
| 350 | + * @return object Database |
|
| 351 | 351 | */ |
| 352 | 352 | public function setLogger($logger){ |
| 353 | - $this->logger = $logger; |
|
| 354 | - return $this; |
|
| 353 | + $this->logger = $logger; |
|
| 354 | + return $this; |
|
| 355 | 355 | } |
| 356 | 356 | |
| 357 | - /** |
|
| 358 | - * Return the cache instance |
|
| 359 | - * @return CacheInterface |
|
| 360 | - */ |
|
| 357 | + /** |
|
| 358 | + * Return the cache instance |
|
| 359 | + * @return CacheInterface |
|
| 360 | + */ |
|
| 361 | 361 | public function getCacheInstance(){ |
| 362 | - return $this->cacheInstance; |
|
| 362 | + return $this->cacheInstance; |
|
| 363 | 363 | } |
| 364 | 364 | |
| 365 | 365 | /** |
| 366 | 366 | * Set the cache instance |
| 367 | 367 | * @param CacheInterface $cache the cache object |
| 368 | - * @return object Database |
|
| 368 | + * @return object Database |
|
| 369 | 369 | */ |
| 370 | 370 | public function setCacheInstance($cache){ |
| 371 | - $this->cacheInstance = $cache; |
|
| 372 | - return $this; |
|
| 371 | + $this->cacheInstance = $cache; |
|
| 372 | + return $this; |
|
| 373 | 373 | } |
| 374 | 374 | |
| 375 | 375 | |
| 376 | - /** |
|
| 377 | - * Return the DatabaseQueryBuilder instance |
|
| 378 | - * @return object DatabaseQueryBuilder |
|
| 379 | - */ |
|
| 376 | + /** |
|
| 377 | + * Return the DatabaseQueryBuilder instance |
|
| 378 | + * @return object DatabaseQueryBuilder |
|
| 379 | + */ |
|
| 380 | 380 | public function getQueryBuilder(){ |
| 381 | - return $this->queryBuilder; |
|
| 381 | + return $this->queryBuilder; |
|
| 382 | 382 | } |
| 383 | 383 | |
| 384 | 384 | /** |
@@ -386,8 +386,8 @@ discard block |
||
| 386 | 386 | * @param object DatabaseQueryBuilder $queryBuilder the DatabaseQueryBuilder object |
| 387 | 387 | */ |
| 388 | 388 | public function setQueryBuilder(DatabaseQueryBuilder $queryBuilder){ |
| 389 | - $this->queryBuilder = $queryBuilder; |
|
| 390 | - return $this; |
|
| 389 | + $this->queryBuilder = $queryBuilder; |
|
| 390 | + return $this; |
|
| 391 | 391 | } |
| 392 | 392 | |
| 393 | 393 | /** |
@@ -395,7 +395,7 @@ discard block |
||
| 395 | 395 | * @return object DatabaseQueryRunner |
| 396 | 396 | */ |
| 397 | 397 | public function getQueryRunner(){ |
| 398 | - return $this->queryRunner; |
|
| 398 | + return $this->queryRunner; |
|
| 399 | 399 | } |
| 400 | 400 | |
| 401 | 401 | /** |
@@ -403,8 +403,8 @@ discard block |
||
| 403 | 403 | * @param object DatabaseQueryRunner $queryRunner the DatabaseQueryRunner object |
| 404 | 404 | */ |
| 405 | 405 | public function setQueryRunner(DatabaseQueryRunner $queryRunner){ |
| 406 | - $this->queryRunner = $queryRunner; |
|
| 407 | - return $this; |
|
| 406 | + $this->queryRunner = $queryRunner; |
|
| 407 | + return $this; |
|
| 408 | 408 | } |
| 409 | 409 | |
| 410 | 410 | /** |
@@ -412,7 +412,7 @@ discard block |
||
| 412 | 412 | * @return array |
| 413 | 413 | */ |
| 414 | 414 | public function getData(){ |
| 415 | - return $this->data; |
|
| 415 | + return $this->data; |
|
| 416 | 416 | } |
| 417 | 417 | |
| 418 | 418 | /** |
@@ -423,51 +423,51 @@ discard block |
||
| 423 | 423 | * @return object the current Database instance |
| 424 | 424 | */ |
| 425 | 425 | public function setData($key, $value = null, $escape = true){ |
| 426 | - if(is_array($key)){ |
|
| 427 | - foreach($key as $k => $v){ |
|
| 428 | - $this->setData($k, $v, $escape); |
|
| 429 | - } |
|
| 430 | - } else { |
|
| 426 | + if(is_array($key)){ |
|
| 427 | + foreach($key as $k => $v){ |
|
| 428 | + $this->setData($k, $v, $escape); |
|
| 429 | + } |
|
| 430 | + } else { |
|
| 431 | 431 | $this->data[$key] = $this->escape($value, $escape); |
| 432 | - } |
|
| 433 | - return $this; |
|
| 432 | + } |
|
| 433 | + return $this; |
|
| 434 | 434 | } |
| 435 | 435 | |
| 436 | - /** |
|
| 437 | - * Execute an SQL query |
|
| 438 | - * @param string $query the query SQL string |
|
| 439 | - * @param boolean $returnAsList indicate whether to return all record or just one row |
|
| 440 | - * @param boolean $returnAsArray return the result as array or not |
|
| 441 | - * @return mixed the query result |
|
| 442 | - */ |
|
| 436 | + /** |
|
| 437 | + * Execute an SQL query |
|
| 438 | + * @param string $query the query SQL string |
|
| 439 | + * @param boolean $returnAsList indicate whether to return all record or just one row |
|
| 440 | + * @param boolean $returnAsArray return the result as array or not |
|
| 441 | + * @return mixed the query result |
|
| 442 | + */ |
|
| 443 | 443 | public function query($query, $returnAsList = true, $returnAsArray = false){ |
| 444 | - $this->reset(); |
|
| 445 | - $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 446 | - //If is the SELECT query |
|
| 447 | - $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
|
| 444 | + $this->reset(); |
|
| 445 | + $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 446 | + //If is the SELECT query |
|
| 447 | + $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
|
| 448 | 448 | |
| 449 | - //cache expire time |
|
| 450 | - $cacheExpire = $this->temporaryCacheTtl; |
|
| 449 | + //cache expire time |
|
| 450 | + $cacheExpire = $this->temporaryCacheTtl; |
|
| 451 | 451 | |
| 452 | - //return to the initial cache time |
|
| 453 | - $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 452 | + //return to the initial cache time |
|
| 453 | + $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 454 | 454 | |
| 455 | - //config for cache |
|
| 456 | - $cacheEnable = get_config('cache_enable'); |
|
| 455 | + //config for cache |
|
| 456 | + $cacheEnable = get_config('cache_enable'); |
|
| 457 | 457 | |
| 458 | - //the database cache content |
|
| 459 | - $cacheContent = null; |
|
| 458 | + //the database cache content |
|
| 459 | + $cacheContent = null; |
|
| 460 | 460 | |
| 461 | - //if can use cache feature for this query |
|
| 462 | - $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
|
| 461 | + //if can use cache feature for this query |
|
| 462 | + $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
|
| 463 | 463 | |
| 464 | - if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
| 465 | - $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 466 | - $cacheContent = $this->getCacheContentForQuery($query, $returnAsList, $returnAsArray); |
|
| 467 | - } |
|
| 464 | + if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
| 465 | + $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 466 | + $cacheContent = $this->getCacheContentForQuery($query, $returnAsList, $returnAsArray); |
|
| 467 | + } |
|
| 468 | 468 | |
| 469 | - if ( !$cacheContent){ |
|
| 470 | - //count the number of query execution to server |
|
| 469 | + if ( !$cacheContent){ |
|
| 470 | + //count the number of query execution to server |
|
| 471 | 471 | $this->queryCount++; |
| 472 | 472 | |
| 473 | 473 | $this->queryRunner->setQuery($query); |
@@ -482,33 +482,33 @@ discard block |
||
| 482 | 482 | $key = $this->getCacheKeyForQuery($this->query, $returnAsList, $returnAsArray); |
| 483 | 483 | $this->setCacheContentForQuery($this->query, $key, $this->result, $cacheExpire); |
| 484 | 484 | if (! $this->result){ |
| 485 | - $this->logger->info('No result where found for the query [' . $query . ']'); |
|
| 485 | + $this->logger->info('No result where found for the query [' . $query . ']'); |
|
| 486 | 486 | } |
| 487 | - } |
|
| 487 | + } |
|
| 488 | + } |
|
| 489 | + } else if ($isSqlSELECTQuery){ |
|
| 490 | + $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 491 | + $this->result = $cacheContent; |
|
| 492 | + $this->numRows = count($this->result); |
|
| 488 | 493 | } |
| 489 | - } else if ($isSqlSELECTQuery){ |
|
| 490 | - $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 491 | - $this->result = $cacheContent; |
|
| 492 | - $this->numRows = count($this->result); |
|
| 493 | - } |
|
| 494 | - return $this->result; |
|
| 494 | + return $this->result; |
|
| 495 | 495 | } |
| 496 | 496 | |
| 497 | 497 | |
| 498 | - /** |
|
| 499 | - * Return the database configuration |
|
| 500 | - * @return array |
|
| 501 | - */ |
|
| 502 | - public function getDatabaseConfiguration(){ |
|
| 503 | - return $this->config; |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - /** |
|
| 507 | - * Setting the database configuration using the configuration file and additional configuration from param |
|
| 508 | - * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
| 509 | - * @param boolean $useConfigFile whether to use database configuration file |
|
| 510 | - * @return object Database |
|
| 511 | - */ |
|
| 498 | + /** |
|
| 499 | + * Return the database configuration |
|
| 500 | + * @return array |
|
| 501 | + */ |
|
| 502 | + public function getDatabaseConfiguration(){ |
|
| 503 | + return $this->config; |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + /** |
|
| 507 | + * Setting the database configuration using the configuration file and additional configuration from param |
|
| 508 | + * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
| 509 | + * @param boolean $useConfigFile whether to use database configuration file |
|
| 510 | + * @return object Database |
|
| 511 | + */ |
|
| 512 | 512 | public function setDatabaseConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
| 513 | 513 | $db = array(); |
| 514 | 514 | if ($useConfigFile && file_exists(CONFIG_PATH . 'database.php')){ |
@@ -521,50 +521,50 @@ discard block |
||
| 521 | 521 | |
| 522 | 522 | //default configuration |
| 523 | 523 | $config = array( |
| 524 | - 'driver' => 'mysql', |
|
| 525 | - 'username' => 'root', |
|
| 526 | - 'password' => '', |
|
| 527 | - 'database' => '', |
|
| 528 | - 'hostname' => 'localhost', |
|
| 529 | - 'charset' => 'utf8', |
|
| 530 | - 'collation' => 'utf8_general_ci', |
|
| 531 | - 'prefix' => '', |
|
| 532 | - 'port' => '' |
|
| 524 | + 'driver' => 'mysql', |
|
| 525 | + 'username' => 'root', |
|
| 526 | + 'password' => '', |
|
| 527 | + 'database' => '', |
|
| 528 | + 'hostname' => 'localhost', |
|
| 529 | + 'charset' => 'utf8', |
|
| 530 | + 'collation' => 'utf8_general_ci', |
|
| 531 | + 'prefix' => '', |
|
| 532 | + 'port' => '' |
|
| 533 | 533 | ); |
| 534 | 534 | |
| 535 | - $config = array_merge($config, $db); |
|
| 536 | - //determine the port using the hostname like localhost:3307 |
|
| 537 | - //hostname will be "localhost", and port "3307" |
|
| 538 | - $p = explode(':', $config['hostname']); |
|
| 539 | - if (count($p) >= 2){ |
|
| 540 | - $config['hostname'] = $p[0]; |
|
| 541 | - $config['port'] = $p[1]; |
|
| 542 | - } |
|
| 535 | + $config = array_merge($config, $db); |
|
| 536 | + //determine the port using the hostname like localhost:3307 |
|
| 537 | + //hostname will be "localhost", and port "3307" |
|
| 538 | + $p = explode(':', $config['hostname']); |
|
| 539 | + if (count($p) >= 2){ |
|
| 540 | + $config['hostname'] = $p[0]; |
|
| 541 | + $config['port'] = $p[1]; |
|
| 542 | + } |
|
| 543 | 543 | |
| 544 | - $this->databaseName = $config['database']; |
|
| 545 | - $this->config = $config; |
|
| 546 | - $this->logger->info( |
|
| 547 | - 'The database configuration are listed below: ' |
|
| 548 | - . stringfy_vars(array_merge( |
|
| 549 | - $this->config, |
|
| 550 | - array('password' => string_hidden($this->config['password'])) |
|
| 551 | - )) |
|
| 552 | - ); |
|
| 544 | + $this->databaseName = $config['database']; |
|
| 545 | + $this->config = $config; |
|
| 546 | + $this->logger->info( |
|
| 547 | + 'The database configuration are listed below: ' |
|
| 548 | + . stringfy_vars(array_merge( |
|
| 549 | + $this->config, |
|
| 550 | + array('password' => string_hidden($this->config['password'])) |
|
| 551 | + )) |
|
| 552 | + ); |
|
| 553 | 553 | |
| 554 | - //Now connect to the database |
|
| 555 | - $this->connect(); |
|
| 554 | + //Now connect to the database |
|
| 555 | + $this->connect(); |
|
| 556 | 556 | |
| 557 | - //do update of QueryRunner and Builder |
|
| 558 | - $this->updateQueryBuilderAndRunnerProperties(); |
|
| 557 | + //do update of QueryRunner and Builder |
|
| 558 | + $this->updateQueryBuilderAndRunnerProperties(); |
|
| 559 | 559 | |
| 560 | - return $this; |
|
| 560 | + return $this; |
|
| 561 | 561 | } |
| 562 | 562 | |
| 563 | 563 | /** |
| 564 | 564 | * Close the connexion |
| 565 | 565 | */ |
| 566 | 566 | public function close(){ |
| 567 | - $this->pdo = null; |
|
| 567 | + $this->pdo = null; |
|
| 568 | 568 | } |
| 569 | 569 | |
| 570 | 570 | /** |
@@ -572,18 +572,18 @@ discard block |
||
| 572 | 572 | * @return void |
| 573 | 573 | */ |
| 574 | 574 | protected function updateQueryBuilderAndRunnerProperties(){ |
| 575 | - //update queryBuilder with some properties needed |
|
| 576 | - if(is_object($this->queryBuilder)){ |
|
| 575 | + //update queryBuilder with some properties needed |
|
| 576 | + if(is_object($this->queryBuilder)){ |
|
| 577 | 577 | $this->queryBuilder->setDriver($this->config['driver']); |
| 578 | 578 | $this->queryBuilder->setPrefix($this->config['prefix']); |
| 579 | 579 | $this->queryBuilder->setPdo($this->pdo); |
| 580 | - } |
|
| 580 | + } |
|
| 581 | 581 | |
| 582 | - //update queryRunner with some properties needed |
|
| 583 | - if(is_object($this->queryRunner)){ |
|
| 582 | + //update queryRunner with some properties needed |
|
| 583 | + if(is_object($this->queryRunner)){ |
|
| 584 | 584 | $this->queryRunner->setDriver($this->config['driver']); |
| 585 | 585 | $this->queryRunner->setPdo($this->pdo); |
| 586 | - } |
|
| 586 | + } |
|
| 587 | 587 | } |
| 588 | 588 | |
| 589 | 589 | |
@@ -592,24 +592,24 @@ discard block |
||
| 592 | 592 | * @return string the DSN string |
| 593 | 593 | */ |
| 594 | 594 | protected function getDsnFromDriver(){ |
| 595 | - $config = $this->getDatabaseConfiguration(); |
|
| 596 | - if (! empty($config)){ |
|
| 595 | + $config = $this->getDatabaseConfiguration(); |
|
| 596 | + if (! empty($config)){ |
|
| 597 | 597 | $driver = $config['driver']; |
| 598 | 598 | $driverDsnMap = array( |
| 599 | - 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
|
| 600 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 601 | - . 'dbname=' . $config['database'], |
|
| 602 | - 'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' |
|
| 603 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 604 | - . 'dbname=' . $config['database'], |
|
| 605 | - 'sqlite' => 'sqlite:' . $config['database'], |
|
| 606 | - 'oracle' => 'oci:dbname=' . $config['hostname'] |
|
| 599 | + 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
|
| 600 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 601 | + . 'dbname=' . $config['database'], |
|
| 602 | + 'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' |
|
| 603 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 604 | + . 'dbname=' . $config['database'], |
|
| 605 | + 'sqlite' => 'sqlite:' . $config['database'], |
|
| 606 | + 'oracle' => 'oci:dbname=' . $config['hostname'] |
|
| 607 | 607 | . (($config['port']) != '' ? ':' . $config['port'] : '') |
| 608 | 608 | . '/' . $config['database'] |
| 609 | - ); |
|
| 609 | + ); |
|
| 610 | 610 | return isset($driverDsnMap[$driver]) ? $driverDsnMap[$driver] : ''; |
| 611 | - } |
|
| 612 | - return null; |
|
| 611 | + } |
|
| 612 | + return null; |
|
| 613 | 613 | } |
| 614 | 614 | |
| 615 | 615 | /** |
@@ -621,11 +621,11 @@ discard block |
||
| 621 | 621 | protected function getCacheContentForQuery($query, $returnAsList, $returnAsArray){ |
| 622 | 622 | $cacheKey = $this->getCacheKeyForQuery($query, $returnAsList, $returnAsArray); |
| 623 | 623 | if (! is_object($this->cacheInstance)){ |
| 624 | - //can not call method with reference in argument |
|
| 625 | - //like $this->setCacheInstance(& get_instance()->cache); |
|
| 626 | - //use temporary variable |
|
| 627 | - $instance = & get_instance()->cache; |
|
| 628 | - $this->cacheInstance = $instance; |
|
| 624 | + //can not call method with reference in argument |
|
| 625 | + //like $this->setCacheInstance(& get_instance()->cache); |
|
| 626 | + //use temporary variable |
|
| 627 | + $instance = & get_instance()->cache; |
|
| 628 | + $this->cacheInstance = $instance; |
|
| 629 | 629 | } |
| 630 | 630 | return $this->cacheInstance->get($cacheKey); |
| 631 | 631 | } |
@@ -637,87 +637,87 @@ discard block |
||
| 637 | 637 | * @param mixed $result the query result to save |
| 638 | 638 | * @param int $expire the cache TTL |
| 639 | 639 | */ |
| 640 | - protected function setCacheContentForQuery($query, $key, $result, $expire){ |
|
| 640 | + protected function setCacheContentForQuery($query, $key, $result, $expire){ |
|
| 641 | 641 | $this->logger->info('Save the result for query [' .$query. '] into cache for future use'); |
| 642 | 642 | if (! is_object($this->cacheInstance)){ |
| 643 | - //can not call method with reference in argument |
|
| 644 | - //like $this->setCacheInstance(& get_instance()->cache); |
|
| 645 | - //use temporary variable |
|
| 646 | - $instance = & get_instance()->cache; |
|
| 647 | - $this->cacheInstance = $instance; |
|
| 648 | - } |
|
| 643 | + //can not call method with reference in argument |
|
| 644 | + //like $this->setCacheInstance(& get_instance()->cache); |
|
| 645 | + //use temporary variable |
|
| 646 | + $instance = & get_instance()->cache; |
|
| 647 | + $this->cacheInstance = $instance; |
|
| 648 | + } |
|
| 649 | 649 | $this->cacheInstance->set($key, $result, $expire); |
| 650 | - } |
|
| 650 | + } |
|
| 651 | 651 | |
| 652 | 652 | |
| 653 | - /** |
|
| 654 | - * Return the cache key for the given query |
|
| 655 | - * @see Database::query |
|
| 656 | - * |
|
| 657 | - * @return string |
|
| 658 | - */ |
|
| 653 | + /** |
|
| 654 | + * Return the cache key for the given query |
|
| 655 | + * @see Database::query |
|
| 656 | + * |
|
| 657 | + * @return string |
|
| 658 | + */ |
|
| 659 | 659 | protected function getCacheKeyForQuery($query, $returnAsList, $returnAsArray){ |
| 660 | - return md5($query . $returnAsList . $returnAsArray); |
|
| 660 | + return md5($query . $returnAsList . $returnAsArray); |
|
| 661 | 661 | } |
| 662 | 662 | |
| 663 | - /** |
|
| 664 | - * Set the Log instance using argument or create new instance |
|
| 665 | - * @param object $logger the Log instance if not null |
|
| 666 | - */ |
|
| 663 | + /** |
|
| 664 | + * Set the Log instance using argument or create new instance |
|
| 665 | + * @param object $logger the Log instance if not null |
|
| 666 | + */ |
|
| 667 | 667 | protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
| 668 | - if ($logger !== null){ |
|
| 668 | + if ($logger !== null){ |
|
| 669 | 669 | $this->logger = $logger; |
| 670 | - } |
|
| 671 | - else{ |
|
| 672 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 673 | - $this->logger->setLogger('Library::Database'); |
|
| 674 | - } |
|
| 670 | + } |
|
| 671 | + else{ |
|
| 672 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 673 | + $this->logger->setLogger('Library::Database'); |
|
| 674 | + } |
|
| 675 | 675 | } |
| 676 | 676 | |
| 677 | - /** |
|
| 678 | - * Set the DatabaseQueryBuilder instance using argument or create new instance |
|
| 679 | - * @param object $queryBuilder the DatabaseQueryBuilder instance if not null |
|
| 680 | - */ |
|
| 681 | - protected function setQueryBuilderFromParamOrCreateNewInstance(DatabaseQueryBuilder $queryBuilder = null){ |
|
| 682 | - if ($queryBuilder !== null){ |
|
| 677 | + /** |
|
| 678 | + * Set the DatabaseQueryBuilder instance using argument or create new instance |
|
| 679 | + * @param object $queryBuilder the DatabaseQueryBuilder instance if not null |
|
| 680 | + */ |
|
| 681 | + protected function setQueryBuilderFromParamOrCreateNewInstance(DatabaseQueryBuilder $queryBuilder = null){ |
|
| 682 | + if ($queryBuilder !== null){ |
|
| 683 | 683 | $this->queryBuilder = $queryBuilder; |
| 684 | - } |
|
| 685 | - else{ |
|
| 686 | - $this->queryBuilder =& class_loader('DatabaseQueryBuilder', 'classes/database'); |
|
| 687 | - } |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - /** |
|
| 691 | - * Set the DatabaseQueryRunner instance using argument or create new instance |
|
| 692 | - * @param object $queryRunner the DatabaseQueryRunner instance if not null |
|
| 693 | - */ |
|
| 694 | - protected function setQueryRunnerFromParamOrCreateNewInstance(DatabaseQueryRunner $queryRunner = null){ |
|
| 684 | + } |
|
| 685 | + else{ |
|
| 686 | + $this->queryBuilder =& class_loader('DatabaseQueryBuilder', 'classes/database'); |
|
| 687 | + } |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + /** |
|
| 691 | + * Set the DatabaseQueryRunner instance using argument or create new instance |
|
| 692 | + * @param object $queryRunner the DatabaseQueryRunner instance if not null |
|
| 693 | + */ |
|
| 694 | + protected function setQueryRunnerFromParamOrCreateNewInstance(DatabaseQueryRunner $queryRunner = null){ |
|
| 695 | 695 | if ($queryRunner !== null){ |
| 696 | 696 | $this->queryRunner = $queryRunner; |
| 697 | 697 | } |
| 698 | 698 | else{ |
| 699 | - $this->queryRunner =& class_loader('DatabaseQueryRunner', 'classes/database'); |
|
| 699 | + $this->queryRunner =& class_loader('DatabaseQueryRunner', 'classes/database'); |
|
| 700 | + } |
|
| 700 | 701 | } |
| 701 | - } |
|
| 702 | 702 | |
| 703 | 703 | /** |
| 704 | 704 | * Reset the database class attributs to the initail values before each query. |
| 705 | 705 | */ |
| 706 | 706 | private function reset(){ |
| 707 | - //query builder reset |
|
| 708 | - $this->getQueryBuilder()->reset(); |
|
| 709 | - $this->numRows = 0; |
|
| 710 | - $this->insertId = null; |
|
| 711 | - $this->query = null; |
|
| 712 | - $this->result = array(); |
|
| 713 | - $this->data = array(); |
|
| 707 | + //query builder reset |
|
| 708 | + $this->getQueryBuilder()->reset(); |
|
| 709 | + $this->numRows = 0; |
|
| 710 | + $this->insertId = null; |
|
| 711 | + $this->query = null; |
|
| 712 | + $this->result = array(); |
|
| 713 | + $this->data = array(); |
|
| 714 | 714 | } |
| 715 | 715 | |
| 716 | 716 | /** |
| 717 | 717 | * The class destructor |
| 718 | 718 | */ |
| 719 | 719 | public function __destruct(){ |
| 720 | - $this->pdo = null; |
|
| 720 | + $this->pdo = null; |
|
| 721 | 721 | } |
| 722 | 722 | |
| 723 | 723 | } |