|
1
|
|
|
<?php |
|
2
|
|
|
defined('ROOT_PATH') || exit('Access denied'); |
|
3
|
|
|
/** |
|
4
|
|
|
* TNH Framework |
|
5
|
|
|
* |
|
6
|
|
|
* A simple PHP framework using HMVC architecture |
|
7
|
|
|
* |
|
8
|
|
|
* This content is released under the GNU GPL License (GPL) |
|
9
|
|
|
* |
|
10
|
|
|
* Copyright (C) 2017 Tony NGUEREZA |
|
11
|
|
|
* |
|
12
|
|
|
* This program is free software; you can redistribute it and/or |
|
13
|
|
|
* modify it under the terms of the GNU General Public License |
|
14
|
|
|
* as published by the Free Software Foundation; either version 3 |
|
15
|
|
|
* of the License, or (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* This program is distributed in the hope that it will be useful, |
|
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20
|
|
|
* GNU General Public License for more details. |
|
21
|
|
|
* |
|
22
|
|
|
* You should have received a copy of the GNU General Public License |
|
23
|
|
|
* along with this program; if not, write to the Free Software |
|
24
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
function & class_loader($class, $dir = 'libraries', $params = null){ |
|
29
|
|
|
//put the first letter of class to upper case |
|
30
|
|
|
$class = ucfirst($class); |
|
31
|
|
|
static $classes = array(); |
|
32
|
|
|
if(isset($classes[$class]) /*hack for duplicate log Logger name*/ && $class != 'Log'){ |
|
33
|
|
|
return $classes[$class]; |
|
34
|
|
|
} |
|
35
|
|
|
$found = false; |
|
36
|
|
|
foreach (array(ROOT_PATH, CORE_PATH) as $path) { |
|
37
|
|
|
$file = $path . $dir . '/' . $class . '.php'; |
|
38
|
|
|
if(file_exists($file)){ |
|
39
|
|
|
if(class_exists($class, false) == false){ |
|
|
|
|
|
|
40
|
|
|
require_once $file; |
|
41
|
|
|
} |
|
42
|
|
|
//already found |
|
43
|
|
|
$found = true; |
|
44
|
|
|
break; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
if(! $found){ |
|
48
|
|
|
//can't use show_error() at this time because some dependencies not yet loaded |
|
49
|
|
|
set_http_status_header(503); |
|
50
|
|
|
echo 'Cannot find the class [' . $class . ']'; |
|
51
|
|
|
exit(1); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/* |
|
55
|
|
|
TODO use the best method to get the Log instance |
|
56
|
|
|
*/ |
|
57
|
|
|
if($class == 'Log'){ |
|
58
|
|
|
//can't use the instruction like "return new Log()" |
|
59
|
|
|
//because we need return the reference instance of the loaded class. |
|
60
|
|
|
$log = new Log(); |
|
61
|
|
|
return $log; |
|
62
|
|
|
} |
|
63
|
|
|
//track of loaded classes |
|
64
|
|
|
class_loaded($class); |
|
65
|
|
|
|
|
66
|
|
|
//record the class instance |
|
67
|
|
|
$classes[$class] = isset($params) ? new $class($params) : new $class(); |
|
68
|
|
|
|
|
69
|
|
|
return $classes[$class]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
function & class_loaded($class = null){ |
|
74
|
|
|
static $list = array(); |
|
75
|
|
|
if($class != null){ |
|
76
|
|
|
$list[strtolower($class)] = $class; |
|
77
|
|
|
} |
|
78
|
|
|
return $list; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
function & load_configurations(array $overwrite_values = array()){ |
|
82
|
|
|
static $config; |
|
83
|
|
|
if(empty($config)){ |
|
84
|
|
|
$file = CONFIG_PATH . 'config.php'; |
|
85
|
|
|
require_once $file; |
|
86
|
|
|
|
|
87
|
|
|
foreach ($overwrite_values as $key => $value) { |
|
88
|
|
|
$config[$key] = $value; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
return $config; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @test |
|
96
|
|
|
*/ |
|
97
|
|
|
function get_config($key, $default = null){ |
|
98
|
|
|
static $cfg; |
|
99
|
|
|
if(empty($cfg)){ |
|
100
|
|
|
$cfg[0] = & load_configurations(); |
|
101
|
|
|
} |
|
102
|
|
|
return array_key_exists($key, $cfg[0]) ? $cfg[0][$key] : $default; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
function save_to_log($level, $message, $logger = null){ |
|
|
|
|
|
|
106
|
|
|
return true; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
function set_http_status_header($code = 200, $text = null){ |
|
|
|
|
|
|
111
|
|
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
function show_error($msg, $title = 'error', $logging = true){ |
|
|
|
|
|
|
116
|
|
|
//throw new RuntimeException('TNHFW Error: '.$msg); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
function is_https(){ |
|
120
|
|
|
return false; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @test |
|
125
|
|
|
*/ |
|
126
|
|
|
function is_url($url){ |
|
127
|
|
|
return preg_match('/^(http|https|ftp):\/\/(.*)/', $url); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
function php_exception_handler($ex){ |
|
131
|
|
|
throw new RuntimeException('PHP Exception : '.$ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine()); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
function php_error_handler($errno , $errstr, $errfile , $errline, array $errcontext = array()){ |
|
|
|
|
|
|
136
|
|
|
throw new RuntimeException('TNHFW Exception Error : '.$errstr.' | '.$errfile.' | '.$errline); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
function php_shudown_handler(){ |
|
140
|
|
|
return true; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @test |
|
146
|
|
|
*/ |
|
147
|
|
|
function attributes_to_string(array $attributes){ |
|
148
|
|
|
$str = ' '; |
|
149
|
|
|
//we check that the array passed as an argument is not empty. |
|
150
|
|
|
if(! empty($attributes)){ |
|
151
|
|
|
foreach($attributes as $key => $value){ |
|
152
|
|
|
$key = trim(htmlspecialchars($key)); |
|
153
|
|
|
$value = trim(htmlspecialchars($value)); |
|
154
|
|
|
/* |
|
155
|
|
|
* To predict the case where the string to convert contains the character " |
|
156
|
|
|
* we check if this is the case we add a slash to solve this problem. |
|
157
|
|
|
* For example: |
|
158
|
|
|
* $attr = array('placeholder' => 'I am a "puple"') |
|
159
|
|
|
* $str = attributes_to_string($attr); => placeholder = "I am a \"puple\"" |
|
160
|
|
|
*/ |
|
161
|
|
|
if($value && strpos('"', $value) !== false){ |
|
162
|
|
|
$value = addslashes($value); |
|
163
|
|
|
} |
|
164
|
|
|
$str .= $key.' = "'.$value.'" '; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
//remove the space after using rtrim() |
|
168
|
|
|
return rtrim($str); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
function stringfy_vars($var){ |
|
172
|
|
|
return print_r($var, true); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @test |
|
177
|
|
|
*/ |
|
178
|
|
|
function clean_input($str){ |
|
179
|
|
|
if(is_array($str)){ |
|
180
|
|
|
$str = array_map('clean_input', $str); |
|
181
|
|
|
} |
|
182
|
|
|
else if(is_object($str)){ |
|
183
|
|
|
$obj = $str; |
|
184
|
|
|
foreach ($str as $var => $value) { |
|
185
|
|
|
$obj->$var = clean_input($value); |
|
186
|
|
|
} |
|
187
|
|
|
$str = $obj; |
|
188
|
|
|
} |
|
189
|
|
|
else{ |
|
190
|
|
|
$str = htmlspecialchars(strip_tags($str), ENT_QUOTES, 'UTF-8'); |
|
191
|
|
|
} |
|
192
|
|
|
return $str; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @test |
|
197
|
|
|
*/ |
|
198
|
|
|
function string_hidden($str, $startCount = 0, $endCount = 0, $hiddenChar = '*'){ |
|
199
|
|
|
//get the string length |
|
200
|
|
|
$len = strlen($str); |
|
201
|
|
|
//if str is empty |
|
202
|
|
|
if($len <= 0){ |
|
203
|
|
|
return str_repeat($hiddenChar, 6); |
|
204
|
|
|
} |
|
205
|
|
|
//if the length is less than startCount and endCount |
|
206
|
|
|
//or the startCount and endCount length is 0 |
|
207
|
|
|
//or startCount is negative or endCount is negative |
|
208
|
|
|
//return the full string hidden |
|
209
|
|
|
|
|
210
|
|
|
if((($startCount + $endCount) > $len) || ($startCount == 0 && $endCount == 0) || ($startCount < 0 || $endCount < 0)){ |
|
211
|
|
|
return str_repeat($hiddenChar, $len); |
|
212
|
|
|
} |
|
213
|
|
|
//the start non hidden string |
|
214
|
|
|
$startNonHiddenStr = substr($str, 0, $startCount); |
|
215
|
|
|
//the end non hidden string |
|
216
|
|
|
$endNonHiddenStr = null; |
|
217
|
|
|
if($endCount > 0){ |
|
218
|
|
|
$endNonHiddenStr = substr($str, - $endCount); |
|
219
|
|
|
} |
|
220
|
|
|
//the hidden string |
|
221
|
|
|
$hiddenStr = str_repeat($hiddenChar, $len - ($startCount + $endCount)); |
|
222
|
|
|
|
|
223
|
|
|
return $startNonHiddenStr . $hiddenStr . $endNonHiddenStr; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
function set_session_config(){ |
|
227
|
|
|
return true; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
function & get_instance(){ |
|
231
|
|
|
if(! Controller::get_instance()){ |
|
232
|
|
|
$c = new Controller(); |
|
|
|
|
|
|
233
|
|
|
} |
|
234
|
|
|
return Controller::get_instance(); |
|
235
|
|
|
} |
|
236
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.