@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
3 | 3 | /** |
4 | 4 | * TNH Framework |
5 | 5 | * |
@@ -22,188 +22,188 @@ discard block |
||
22 | 22 | * You should have received a copy of the GNU General Public License |
23 | 23 | * along with this program; if not, write to the Free Software |
24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | - */ |
|
25 | + */ |
|
26 | 26 | class Database{ |
27 | 27 | |
28 | 28 | /** |
29 | 29 | * The PDO instance |
30 | 30 | * @var object |
31 | - */ |
|
32 | - private $pdo = null; |
|
31 | + */ |
|
32 | + private $pdo = null; |
|
33 | 33 | |
34 | 34 | /** |
35 | 35 | * The database name used for the application |
36 | 36 | * @var string |
37 | - */ |
|
37 | + */ |
|
38 | 38 | private $databaseName = null; |
39 | 39 | |
40 | 40 | /** |
41 | 41 | * The SQL SELECT statment |
42 | 42 | * @var string |
43 | - */ |
|
43 | + */ |
|
44 | 44 | private $select = '*'; |
45 | 45 | |
46 | 46 | /** |
47 | 47 | * The SQL FROM statment |
48 | 48 | * @var string |
49 | - */ |
|
50 | - private $from = null; |
|
49 | + */ |
|
50 | + private $from = null; |
|
51 | 51 | |
52 | 52 | /** |
53 | 53 | * The SQL WHERE statment |
54 | 54 | * @var string |
55 | - */ |
|
56 | - private $where = null; |
|
55 | + */ |
|
56 | + private $where = null; |
|
57 | 57 | |
58 | 58 | /** |
59 | 59 | * The SQL LIMIT statment |
60 | 60 | * @var string |
61 | - */ |
|
62 | - private $limit = null; |
|
61 | + */ |
|
62 | + private $limit = null; |
|
63 | 63 | |
64 | 64 | /** |
65 | 65 | * The SQL JOIN statment |
66 | 66 | * @var string |
67 | - */ |
|
68 | - private $join = null; |
|
67 | + */ |
|
68 | + private $join = null; |
|
69 | 69 | |
70 | 70 | /** |
71 | 71 | * The SQL ORDER BY statment |
72 | 72 | * @var string |
73 | - */ |
|
74 | - private $orderBy = null; |
|
73 | + */ |
|
74 | + private $orderBy = null; |
|
75 | 75 | |
76 | 76 | /** |
77 | 77 | * The SQL GROUP BY statment |
78 | 78 | * @var string |
79 | - */ |
|
80 | - private $groupBy = null; |
|
79 | + */ |
|
80 | + private $groupBy = null; |
|
81 | 81 | |
82 | 82 | /** |
83 | 83 | * The SQL HAVING statment |
84 | 84 | * @var string |
85 | - */ |
|
86 | - private $having = null; |
|
85 | + */ |
|
86 | + private $having = null; |
|
87 | 87 | |
88 | 88 | /** |
89 | 89 | * The number of rows returned by the last query |
90 | 90 | * @var int |
91 | - */ |
|
92 | - private $numRows = 0; |
|
91 | + */ |
|
92 | + private $numRows = 0; |
|
93 | 93 | |
94 | 94 | /** |
95 | 95 | * The last insert id for the primary key column that have auto increment or sequence |
96 | 96 | * @var mixed |
97 | - */ |
|
98 | - private $insertId = null; |
|
97 | + */ |
|
98 | + private $insertId = null; |
|
99 | 99 | |
100 | 100 | /** |
101 | 101 | * The full SQL query statment after build for each command |
102 | 102 | * @var string |
103 | - */ |
|
104 | - private $query = null; |
|
103 | + */ |
|
104 | + private $query = null; |
|
105 | 105 | |
106 | 106 | /** |
107 | 107 | * The error returned for the last query |
108 | 108 | * @var string |
109 | - */ |
|
110 | - private $error = null; |
|
109 | + */ |
|
110 | + private $error = null; |
|
111 | 111 | |
112 | 112 | /** |
113 | 113 | * The result returned for the last query |
114 | 114 | * @var mixed |
115 | - */ |
|
116 | - private $result = array(); |
|
115 | + */ |
|
116 | + private $result = array(); |
|
117 | 117 | |
118 | 118 | /** |
119 | 119 | * The prefix used in each database table |
120 | 120 | * @var string |
121 | - */ |
|
122 | - private $prefix = null; |
|
121 | + */ |
|
122 | + private $prefix = null; |
|
123 | 123 | |
124 | 124 | /** |
125 | 125 | * The list of SQL valid operators |
126 | 126 | * @var array |
127 | - */ |
|
128 | - private $operatorList = array('=','!=','<','>','<=','>=','<>'); |
|
127 | + */ |
|
128 | + private $operatorList = array('=','!=','<','>','<=','>=','<>'); |
|
129 | 129 | |
130 | 130 | /** |
131 | 131 | * The cache default time to live in second. 0 means no need to use the cache feature |
132 | 132 | * @var int |
133 | - */ |
|
133 | + */ |
|
134 | 134 | private $cacheTtl = 0; |
135 | 135 | |
136 | 136 | /** |
137 | 137 | * The cache current time to live. 0 means no need to use the cache feature |
138 | 138 | * @var int |
139 | - */ |
|
140 | - private $temporaryCacheTtl = 0; |
|
139 | + */ |
|
140 | + private $temporaryCacheTtl = 0; |
|
141 | 141 | |
142 | 142 | /** |
143 | 143 | * The number of executed query for the current request |
144 | 144 | * @var int |
145 | - */ |
|
146 | - private $queryCount = 0; |
|
145 | + */ |
|
146 | + private $queryCount = 0; |
|
147 | 147 | |
148 | 148 | /** |
149 | 149 | * The default data to be used in the statments query INSERT, UPDATE |
150 | 150 | * @var array |
151 | - */ |
|
152 | - private $data = array(); |
|
151 | + */ |
|
152 | + private $data = array(); |
|
153 | 153 | |
154 | 154 | /** |
155 | 155 | * The database configuration |
156 | 156 | * @var array |
157 | - */ |
|
158 | - private $config = array(); |
|
157 | + */ |
|
158 | + private $config = array(); |
|
159 | 159 | |
160 | 160 | /** |
161 | 161 | * The logger instance |
162 | 162 | * @var Log |
163 | 163 | */ |
164 | - private $logger = null; |
|
165 | - |
|
166 | - |
|
167 | - /** |
|
168 | - * The cache instance |
|
169 | - * @var CacheInterface |
|
170 | - */ |
|
171 | - private $cacheInstance = null; |
|
172 | - |
|
173 | - /** |
|
174 | - * The benchmark instance |
|
175 | - * @var Benchmark |
|
176 | - */ |
|
177 | - private $benchmarkInstance = null; |
|
178 | - |
|
179 | - |
|
180 | - /** |
|
181 | - * Construct new database |
|
182 | - * @param array $overwriteConfig the config to overwrite with the config set in database.php |
|
183 | - * @param object $logger the log instance |
|
184 | - */ |
|
185 | - public function __construct($overwriteConfig = array(), Log $logger = null){ |
|
186 | - /** |
|
187 | - * instance of the Log class |
|
188 | - */ |
|
189 | - if(is_object($logger)){ |
|
190 | - $this->logger = $logger; |
|
191 | - } |
|
192 | - else{ |
|
193 | - $this->logger =& class_loader('Log', 'classes'); |
|
194 | - $this->logger->setLogger('Library::Database'); |
|
195 | - } |
|
196 | - |
|
197 | - $db = array(); |
|
198 | - if(file_exists(CONFIG_PATH . 'database.php')){ |
|
199 | - //here don't use require_once because somewhere user can create database instance directly |
|
200 | - require CONFIG_PATH . 'database.php'; |
|
201 | - } |
|
164 | + private $logger = null; |
|
165 | + |
|
166 | + |
|
167 | + /** |
|
168 | + * The cache instance |
|
169 | + * @var CacheInterface |
|
170 | + */ |
|
171 | + private $cacheInstance = null; |
|
172 | + |
|
173 | + /** |
|
174 | + * The benchmark instance |
|
175 | + * @var Benchmark |
|
176 | + */ |
|
177 | + private $benchmarkInstance = null; |
|
178 | + |
|
179 | + |
|
180 | + /** |
|
181 | + * Construct new database |
|
182 | + * @param array $overwriteConfig the config to overwrite with the config set in database.php |
|
183 | + * @param object $logger the log instance |
|
184 | + */ |
|
185 | + public function __construct($overwriteConfig = array(), Log $logger = null){ |
|
186 | + /** |
|
187 | + * instance of the Log class |
|
188 | + */ |
|
189 | + if(is_object($logger)){ |
|
190 | + $this->logger = $logger; |
|
191 | + } |
|
192 | + else{ |
|
193 | + $this->logger =& class_loader('Log', 'classes'); |
|
194 | + $this->logger->setLogger('Library::Database'); |
|
195 | + } |
|
196 | + |
|
197 | + $db = array(); |
|
198 | + if(file_exists(CONFIG_PATH . 'database.php')){ |
|
199 | + //here don't use require_once because somewhere user can create database instance directly |
|
200 | + require CONFIG_PATH . 'database.php'; |
|
201 | + } |
|
202 | 202 | |
203 | 203 | if(! empty($overwriteConfig)){ |
204 | 204 | $db = array_merge($db, $overwriteConfig); |
205 | 205 | } |
206 | - $config = array(); |
|
206 | + $config = array(); |
|
207 | 207 | $config['driver'] = isset($db['driver']) ? $db['driver'] : 'mysql'; |
208 | 208 | $config['username'] = isset($db['username']) ? $db['username'] : 'root'; |
209 | 209 | $config['password'] = isset($db['password']) ? $db['password'] : ''; |
@@ -212,1031 +212,1031 @@ discard block |
||
212 | 212 | $config['charset'] = isset($db['charset']) ? $db['charset'] : 'utf8'; |
213 | 213 | $config['collation'] = isset($db['collation']) ? $db['collation'] : 'utf8_general_ci'; |
214 | 214 | $config['prefix'] = isset($db['prefix']) ? $db['prefix'] : ''; |
215 | - $port = ''; |
|
216 | - if(strstr($config['hostname'], ':')){ |
|
217 | - $p = explode(':', $config['hostname']); |
|
218 | - $port = isset($p[1]) ? $p[1] : ''; |
|
219 | - $config['hostname'] = isset($p[0]) ? $p[0] : ''; |
|
220 | - } |
|
215 | + $port = ''; |
|
216 | + if(strstr($config['hostname'], ':')){ |
|
217 | + $p = explode(':', $config['hostname']); |
|
218 | + $port = isset($p[1]) ? $p[1] : ''; |
|
219 | + $config['hostname'] = isset($p[0]) ? $p[0] : ''; |
|
220 | + } |
|
221 | 221 | $config['port'] = $port; |
222 | 222 | |
223 | 223 | $this->setDatabaseConfiguration($config); |
224 | - $this->temporaryCacheTtl = $this->cacheTtl; |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * This is used to connect to database |
|
229 | - * @return bool |
|
230 | - */ |
|
231 | - public function connect(){ |
|
232 | - $config = $this->getDatabaseConfiguration(); |
|
233 | - if(! empty($config)){ |
|
234 | - try{ |
|
235 | - $driverDsnMap = array( |
|
236 | - 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
|
237 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
238 | - . 'dbname=' . $config['database'], |
|
239 | - 'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' |
|
240 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
241 | - . 'dbname=' . $config['database'], |
|
242 | - 'sqlite' => 'sqlite:' . $config['database'], |
|
243 | - 'oracle' => 'oci:dbname=' . $config['hostname'] |
|
244 | - . (($config['port']) != '' ? ':' . $config['port'] : '') |
|
245 | - . '/' . $config['database'] |
|
246 | - ); |
|
247 | - $dsn = isset($driverDsnMap[$config['driver']]) ? $driverDsnMap[$config['driver']] : ''; |
|
224 | + $this->temporaryCacheTtl = $this->cacheTtl; |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * This is used to connect to database |
|
229 | + * @return bool |
|
230 | + */ |
|
231 | + public function connect(){ |
|
232 | + $config = $this->getDatabaseConfiguration(); |
|
233 | + if(! empty($config)){ |
|
234 | + try{ |
|
235 | + $driverDsnMap = array( |
|
236 | + 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
|
237 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
238 | + . 'dbname=' . $config['database'], |
|
239 | + 'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' |
|
240 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
241 | + . 'dbname=' . $config['database'], |
|
242 | + 'sqlite' => 'sqlite:' . $config['database'], |
|
243 | + 'oracle' => 'oci:dbname=' . $config['hostname'] |
|
244 | + . (($config['port']) != '' ? ':' . $config['port'] : '') |
|
245 | + . '/' . $config['database'] |
|
246 | + ); |
|
247 | + $dsn = isset($driverDsnMap[$config['driver']]) ? $driverDsnMap[$config['driver']] : ''; |
|
248 | 248 | |
249 | - $this->pdo = new PDO($dsn, $config['username'], $config['password']); |
|
250 | - $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
|
251 | - $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
|
252 | - $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
|
253 | - return true; |
|
254 | - } |
|
255 | - catch (PDOException $e){ |
|
256 | - $this->logger->fatal($e->getMessage()); |
|
257 | - show_error('Cannot connect to Database.'); |
|
258 | - return false; |
|
259 | - } |
|
260 | - } |
|
261 | - else{ |
|
262 | - show_error('Database configuration is not set.'); |
|
263 | - return false; |
|
264 | - } |
|
265 | - } |
|
266 | - |
|
267 | - /** |
|
268 | - * Set the SQL FROM statment |
|
269 | - * @param string|array $table the table name or array of table list |
|
270 | - * @return object the current Database instance |
|
271 | - */ |
|
272 | - public function from($table){ |
|
273 | - if(is_array($table)){ |
|
274 | - $froms = ''; |
|
275 | - foreach($table as $key){ |
|
276 | - $froms .= $this->prefix . $key . ', '; |
|
277 | - } |
|
278 | - $this->from = rtrim($froms, ', '); |
|
279 | - } |
|
280 | - else{ |
|
281 | - $this->from = $this->prefix . $table; |
|
282 | - } |
|
283 | - return $this; |
|
284 | - } |
|
285 | - |
|
286 | - /** |
|
287 | - * Set the SQL SELECT statment |
|
288 | - * @param string|array $fields the field name or array of field list |
|
289 | - * @return object the current Database instance |
|
290 | - */ |
|
291 | - public function select($fields){ |
|
292 | - $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
|
293 | - $this->select = ($this->select == '*' ? $select : $this->select . ', ' . $select); |
|
294 | - return $this; |
|
295 | - } |
|
296 | - |
|
297 | - /** |
|
298 | - * Set the SQL SELECT DISTINCT statment |
|
299 | - * @param string $field the field name to distinct |
|
300 | - * @return object the current Database instance |
|
301 | - */ |
|
302 | - public function distinct($field){ |
|
303 | - $distinct = ' DISTINCT ' . $field; |
|
304 | - $this->select = ($this->select == '*' ? $distinct : $this->select . ', ' . $distinct); |
|
305 | - |
|
306 | - return $this; |
|
307 | - } |
|
308 | - |
|
309 | - /** |
|
310 | - * Set the SQL function MAX in SELECT statment |
|
311 | - * @param string $field the field name |
|
312 | - * @param string $name if is not null represent the alias used for this field in the result |
|
313 | - * @return object the current Database instance |
|
314 | - */ |
|
315 | - public function max($field, $name = null){ |
|
316 | - $func = 'MAX(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
317 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
318 | - return $this; |
|
319 | - } |
|
320 | - |
|
321 | - /** |
|
322 | - * Set the SQL function MIN in SELECT statment |
|
323 | - * @param string $field the field name |
|
324 | - * @param string $name if is not null represent the alias used for this field in the result |
|
325 | - * @return object the current Database instance |
|
326 | - */ |
|
327 | - public function min($field, $name = null){ |
|
328 | - $func = 'MIN(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
329 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
330 | - return $this; |
|
331 | - } |
|
332 | - |
|
333 | - /** |
|
334 | - * Set the SQL function SUM in SELECT statment |
|
335 | - * @param string $field the field name |
|
336 | - * @param string $name if is not null represent the alias used for this field in the result |
|
337 | - * @return object the current Database instance |
|
338 | - */ |
|
339 | - public function sum($field, $name = null){ |
|
340 | - $func = 'SUM(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
341 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
342 | - return $this; |
|
343 | - } |
|
344 | - |
|
345 | - /** |
|
346 | - * Set the SQL function COUNT in SELECT statment |
|
347 | - * @param string $field the field name |
|
348 | - * @param string $name if is not null represent the alias used for this field in the result |
|
349 | - * @return object the current Database instance |
|
350 | - */ |
|
351 | - public function count($field = '*', $name = null){ |
|
352 | - $func = 'COUNT(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
353 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
354 | - return $this; |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * Set the SQL function AVG in SELECT statment |
|
359 | - * @param string $field the field name |
|
360 | - * @param string $name if is not null represent the alias used for this field in the result |
|
361 | - * @return object the current Database instance |
|
362 | - */ |
|
363 | - public function avg($field, $name = null){ |
|
364 | - $func = 'AVG(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
365 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
366 | - return $this; |
|
367 | - } |
|
368 | - |
|
369 | - /** |
|
370 | - * Set the SQL JOIN statment |
|
371 | - * @param string $table the join table name |
|
372 | - * @param string $field1 the first field for join conditions |
|
373 | - * @param string $op the join condition operator. If is null the default will be "=" |
|
374 | - * @param string $field2 the second field for join conditions |
|
375 | - * @param string $type the type of join (INNER, LEFT, RIGHT) |
|
376 | - * @return object the current Database instance |
|
377 | - */ |
|
378 | - public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
|
379 | - $on = $field1; |
|
380 | - $table = $this->prefix . $table; |
|
381 | - if(! is_null($op)){ |
|
382 | - $on = (! in_array($op, $this->operatorList) ? $this->prefix . $field1 . ' = ' . $this->prefix . $op : $this->prefix . $field1 . ' ' . $op . ' ' . $this->prefix . $field2); |
|
383 | - } |
|
384 | - if (is_null($this->join)){ |
|
385 | - $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
386 | - } |
|
387 | - else{ |
|
388 | - $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
389 | - } |
|
390 | - return $this; |
|
391 | - } |
|
392 | - |
|
393 | - /** |
|
394 | - * Set the SQL INNER JOIN statment |
|
395 | - * @see Database::join() |
|
396 | - * @return object the current Database instance |
|
397 | - */ |
|
398 | - public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
|
399 | - return $this->join($table, $field1, $op, $field2, 'INNER '); |
|
400 | - } |
|
401 | - |
|
402 | - /** |
|
403 | - * Set the SQL LEFT JOIN statment |
|
404 | - * @see Database::join() |
|
405 | - * @return object the current Database instance |
|
406 | - */ |
|
407 | - public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
|
408 | - return $this->join($table, $field1, $op, $field2, 'LEFT '); |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * Set the SQL RIGHT JOIN statment |
|
413 | - * @see Database::join() |
|
414 | - * @return object the current Database instance |
|
415 | - */ |
|
416 | - public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
|
417 | - return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
|
418 | - } |
|
419 | - |
|
420 | - /** |
|
421 | - * Set the SQL FULL OUTER JOIN statment |
|
422 | - * @see Database::join() |
|
423 | - * @return object the current Database instance |
|
424 | - */ |
|
425 | - public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
426 | - return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
|
427 | - } |
|
428 | - |
|
429 | - /** |
|
430 | - * Set the SQL LEFT OUTER JOIN statment |
|
431 | - * @see Database::join() |
|
432 | - * @return object the current Database instance |
|
433 | - */ |
|
434 | - public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
435 | - return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
|
436 | - } |
|
437 | - |
|
438 | - /** |
|
439 | - * Set the SQL RIGHT OUTER JOIN statment |
|
440 | - * @see Database::join() |
|
441 | - * @return object the current Database instance |
|
442 | - */ |
|
443 | - public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
444 | - return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
|
445 | - } |
|
446 | - |
|
447 | - /** |
|
448 | - * Set the SQL WHERE CLAUSE for IS NULL |
|
449 | - * @param string|array $field the field name or array of field list |
|
450 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
451 | - * @return object the current Database instance |
|
452 | - */ |
|
453 | - public function whereIsNull($field, $andOr = 'AND'){ |
|
454 | - if(is_array($field)){ |
|
455 | - foreach($field as $f){ |
|
456 | - $this->whereIsNull($f, $andOr); |
|
457 | - } |
|
458 | - } |
|
459 | - else{ |
|
460 | - if (! $this->where){ |
|
461 | - $this->where = $field.' IS NULL '; |
|
462 | - } |
|
463 | - else{ |
|
464 | - $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NULL '; |
|
465 | - } |
|
466 | - } |
|
467 | - return $this; |
|
468 | - } |
|
469 | - |
|
470 | - /** |
|
471 | - * Set the SQL WHERE CLAUSE for IS NOT NULL |
|
472 | - * @param string|array $field the field name or array of field list |
|
473 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
474 | - * @return object the current Database instance |
|
475 | - */ |
|
476 | - public function whereIsNotNull($field, $andOr = 'AND'){ |
|
477 | - if(is_array($field)){ |
|
478 | - foreach($field as $f){ |
|
479 | - $this->whereIsNotNull($f, $andOr); |
|
480 | - } |
|
481 | - } |
|
482 | - else{ |
|
483 | - if (! $this->where){ |
|
484 | - $this->where = $field.' IS NOT NULL '; |
|
485 | - } |
|
486 | - else{ |
|
487 | - $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NOT NULL '; |
|
488 | - } |
|
489 | - } |
|
490 | - return $this; |
|
491 | - } |
|
249 | + $this->pdo = new PDO($dsn, $config['username'], $config['password']); |
|
250 | + $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
|
251 | + $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
|
252 | + $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
|
253 | + return true; |
|
254 | + } |
|
255 | + catch (PDOException $e){ |
|
256 | + $this->logger->fatal($e->getMessage()); |
|
257 | + show_error('Cannot connect to Database.'); |
|
258 | + return false; |
|
259 | + } |
|
260 | + } |
|
261 | + else{ |
|
262 | + show_error('Database configuration is not set.'); |
|
263 | + return false; |
|
264 | + } |
|
265 | + } |
|
266 | + |
|
267 | + /** |
|
268 | + * Set the SQL FROM statment |
|
269 | + * @param string|array $table the table name or array of table list |
|
270 | + * @return object the current Database instance |
|
271 | + */ |
|
272 | + public function from($table){ |
|
273 | + if(is_array($table)){ |
|
274 | + $froms = ''; |
|
275 | + foreach($table as $key){ |
|
276 | + $froms .= $this->prefix . $key . ', '; |
|
277 | + } |
|
278 | + $this->from = rtrim($froms, ', '); |
|
279 | + } |
|
280 | + else{ |
|
281 | + $this->from = $this->prefix . $table; |
|
282 | + } |
|
283 | + return $this; |
|
284 | + } |
|
285 | + |
|
286 | + /** |
|
287 | + * Set the SQL SELECT statment |
|
288 | + * @param string|array $fields the field name or array of field list |
|
289 | + * @return object the current Database instance |
|
290 | + */ |
|
291 | + public function select($fields){ |
|
292 | + $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
|
293 | + $this->select = ($this->select == '*' ? $select : $this->select . ', ' . $select); |
|
294 | + return $this; |
|
295 | + } |
|
296 | + |
|
297 | + /** |
|
298 | + * Set the SQL SELECT DISTINCT statment |
|
299 | + * @param string $field the field name to distinct |
|
300 | + * @return object the current Database instance |
|
301 | + */ |
|
302 | + public function distinct($field){ |
|
303 | + $distinct = ' DISTINCT ' . $field; |
|
304 | + $this->select = ($this->select == '*' ? $distinct : $this->select . ', ' . $distinct); |
|
305 | + |
|
306 | + return $this; |
|
307 | + } |
|
308 | + |
|
309 | + /** |
|
310 | + * Set the SQL function MAX in SELECT statment |
|
311 | + * @param string $field the field name |
|
312 | + * @param string $name if is not null represent the alias used for this field in the result |
|
313 | + * @return object the current Database instance |
|
314 | + */ |
|
315 | + public function max($field, $name = null){ |
|
316 | + $func = 'MAX(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
317 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
318 | + return $this; |
|
319 | + } |
|
320 | + |
|
321 | + /** |
|
322 | + * Set the SQL function MIN in SELECT statment |
|
323 | + * @param string $field the field name |
|
324 | + * @param string $name if is not null represent the alias used for this field in the result |
|
325 | + * @return object the current Database instance |
|
326 | + */ |
|
327 | + public function min($field, $name = null){ |
|
328 | + $func = 'MIN(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
329 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
330 | + return $this; |
|
331 | + } |
|
332 | + |
|
333 | + /** |
|
334 | + * Set the SQL function SUM in SELECT statment |
|
335 | + * @param string $field the field name |
|
336 | + * @param string $name if is not null represent the alias used for this field in the result |
|
337 | + * @return object the current Database instance |
|
338 | + */ |
|
339 | + public function sum($field, $name = null){ |
|
340 | + $func = 'SUM(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
341 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
342 | + return $this; |
|
343 | + } |
|
344 | + |
|
345 | + /** |
|
346 | + * Set the SQL function COUNT in SELECT statment |
|
347 | + * @param string $field the field name |
|
348 | + * @param string $name if is not null represent the alias used for this field in the result |
|
349 | + * @return object the current Database instance |
|
350 | + */ |
|
351 | + public function count($field = '*', $name = null){ |
|
352 | + $func = 'COUNT(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
353 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
354 | + return $this; |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * Set the SQL function AVG in SELECT statment |
|
359 | + * @param string $field the field name |
|
360 | + * @param string $name if is not null represent the alias used for this field in the result |
|
361 | + * @return object the current Database instance |
|
362 | + */ |
|
363 | + public function avg($field, $name = null){ |
|
364 | + $func = 'AVG(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
365 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
366 | + return $this; |
|
367 | + } |
|
368 | + |
|
369 | + /** |
|
370 | + * Set the SQL JOIN statment |
|
371 | + * @param string $table the join table name |
|
372 | + * @param string $field1 the first field for join conditions |
|
373 | + * @param string $op the join condition operator. If is null the default will be "=" |
|
374 | + * @param string $field2 the second field for join conditions |
|
375 | + * @param string $type the type of join (INNER, LEFT, RIGHT) |
|
376 | + * @return object the current Database instance |
|
377 | + */ |
|
378 | + public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
|
379 | + $on = $field1; |
|
380 | + $table = $this->prefix . $table; |
|
381 | + if(! is_null($op)){ |
|
382 | + $on = (! in_array($op, $this->operatorList) ? $this->prefix . $field1 . ' = ' . $this->prefix . $op : $this->prefix . $field1 . ' ' . $op . ' ' . $this->prefix . $field2); |
|
383 | + } |
|
384 | + if (is_null($this->join)){ |
|
385 | + $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
386 | + } |
|
387 | + else{ |
|
388 | + $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
389 | + } |
|
390 | + return $this; |
|
391 | + } |
|
392 | + |
|
393 | + /** |
|
394 | + * Set the SQL INNER JOIN statment |
|
395 | + * @see Database::join() |
|
396 | + * @return object the current Database instance |
|
397 | + */ |
|
398 | + public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
|
399 | + return $this->join($table, $field1, $op, $field2, 'INNER '); |
|
400 | + } |
|
401 | + |
|
402 | + /** |
|
403 | + * Set the SQL LEFT JOIN statment |
|
404 | + * @see Database::join() |
|
405 | + * @return object the current Database instance |
|
406 | + */ |
|
407 | + public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
|
408 | + return $this->join($table, $field1, $op, $field2, 'LEFT '); |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * Set the SQL RIGHT JOIN statment |
|
413 | + * @see Database::join() |
|
414 | + * @return object the current Database instance |
|
415 | + */ |
|
416 | + public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
|
417 | + return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
|
418 | + } |
|
419 | + |
|
420 | + /** |
|
421 | + * Set the SQL FULL OUTER JOIN statment |
|
422 | + * @see Database::join() |
|
423 | + * @return object the current Database instance |
|
424 | + */ |
|
425 | + public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
426 | + return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
|
427 | + } |
|
428 | + |
|
429 | + /** |
|
430 | + * Set the SQL LEFT OUTER JOIN statment |
|
431 | + * @see Database::join() |
|
432 | + * @return object the current Database instance |
|
433 | + */ |
|
434 | + public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
435 | + return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
|
436 | + } |
|
437 | + |
|
438 | + /** |
|
439 | + * Set the SQL RIGHT OUTER JOIN statment |
|
440 | + * @see Database::join() |
|
441 | + * @return object the current Database instance |
|
442 | + */ |
|
443 | + public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
444 | + return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
|
445 | + } |
|
446 | + |
|
447 | + /** |
|
448 | + * Set the SQL WHERE CLAUSE for IS NULL |
|
449 | + * @param string|array $field the field name or array of field list |
|
450 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
451 | + * @return object the current Database instance |
|
452 | + */ |
|
453 | + public function whereIsNull($field, $andOr = 'AND'){ |
|
454 | + if(is_array($field)){ |
|
455 | + foreach($field as $f){ |
|
456 | + $this->whereIsNull($f, $andOr); |
|
457 | + } |
|
458 | + } |
|
459 | + else{ |
|
460 | + if (! $this->where){ |
|
461 | + $this->where = $field.' IS NULL '; |
|
462 | + } |
|
463 | + else{ |
|
464 | + $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NULL '; |
|
465 | + } |
|
466 | + } |
|
467 | + return $this; |
|
468 | + } |
|
469 | + |
|
470 | + /** |
|
471 | + * Set the SQL WHERE CLAUSE for IS NOT NULL |
|
472 | + * @param string|array $field the field name or array of field list |
|
473 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
474 | + * @return object the current Database instance |
|
475 | + */ |
|
476 | + public function whereIsNotNull($field, $andOr = 'AND'){ |
|
477 | + if(is_array($field)){ |
|
478 | + foreach($field as $f){ |
|
479 | + $this->whereIsNotNull($f, $andOr); |
|
480 | + } |
|
481 | + } |
|
482 | + else{ |
|
483 | + if (! $this->where){ |
|
484 | + $this->where = $field.' IS NOT NULL '; |
|
485 | + } |
|
486 | + else{ |
|
487 | + $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NOT NULL '; |
|
488 | + } |
|
489 | + } |
|
490 | + return $this; |
|
491 | + } |
|
492 | 492 | |
493 | - /** |
|
494 | - * Set the SQL WHERE CLAUSE statment |
|
495 | - * @param string|array $where the where field or array of field list |
|
496 | - * @param string $op the condition operator. If is null the default will be "=" |
|
497 | - * @param mixed $val the where value |
|
498 | - * @param string $type the type used for this where clause (NOT, etc.) |
|
499 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
500 | - * @param boolean $escape whether to escape or not the $val |
|
501 | - * @return object the current Database instance |
|
502 | - */ |
|
503 | - public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
|
504 | - if (is_array($where)){ |
|
505 | - $_where = array(); |
|
506 | - foreach ($where as $column => $data){ |
|
507 | - if(is_null($data)){ |
|
508 | - $data = ''; |
|
509 | - } |
|
510 | - $_where[] = $type . $column . '=' . ($escape ? $this->escape($data) : $data); |
|
511 | - } |
|
512 | - $where = implode(' '.$andOr.' ', $_where); |
|
513 | - } |
|
514 | - else{ |
|
515 | - if(is_array($op)){ |
|
516 | - $x = explode('?', $where); |
|
517 | - $w = ''; |
|
518 | - foreach($x as $k => $v){ |
|
519 | - if(! empty($v)){ |
|
520 | - if(isset($op[$k]) && is_null($op[$k])){ |
|
521 | - $op[$k] = ''; |
|
522 | - } |
|
523 | - $w .= $type . $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
|
524 | - } |
|
525 | - } |
|
526 | - $where = $w; |
|
527 | - } |
|
528 | - else if (! in_array((string)$op, $this->operatorList)){ |
|
529 | - if(is_null($op)){ |
|
530 | - $op = ''; |
|
531 | - } |
|
532 | - $where = $type . $where . ' = ' . ($escape ? $this->escape($op) : $op); |
|
533 | - } |
|
534 | - else{ |
|
535 | - if(is_null($val)){ |
|
536 | - $val = ''; |
|
537 | - } |
|
538 | - $where = $type . $where . $op . ($escape ? $this->escape($val) : $val); |
|
539 | - } |
|
540 | - } |
|
541 | - if (is_null($this->where)){ |
|
542 | - $this->where = $where; |
|
543 | - } |
|
544 | - else{ |
|
545 | - if(substr($this->where, -1) == '('){ |
|
546 | - $this->where = $this->where . ' ' . $where; |
|
547 | - } |
|
548 | - else{ |
|
549 | - $this->where = $this->where . ' '.$andOr.' ' . $where; |
|
550 | - } |
|
551 | - } |
|
552 | - return $this; |
|
553 | - } |
|
554 | - |
|
555 | - /** |
|
556 | - * Set the SQL WHERE CLAUSE statment using OR |
|
557 | - * @see Database::where() |
|
558 | - * @return object the current Database instance |
|
559 | - */ |
|
560 | - public function orWhere($where, $op = null, $val = null, $escape = true){ |
|
561 | - return $this->where($where, $op, $val, '', 'OR', $escape); |
|
562 | - } |
|
563 | - |
|
564 | - |
|
565 | - /** |
|
566 | - * Set the SQL WHERE CLAUSE statment using AND and NOT |
|
567 | - * @see Database::where() |
|
568 | - * @return object the current Database instance |
|
569 | - */ |
|
570 | - public function notWhere($where, $op = null, $val = null, $escape = true){ |
|
571 | - return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
572 | - } |
|
573 | - |
|
574 | - /** |
|
575 | - * Set the SQL WHERE CLAUSE statment using OR and NOT |
|
576 | - * @see Database::where() |
|
577 | - * @return object the current Database instance |
|
578 | - */ |
|
579 | - public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
|
580 | - return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
581 | - } |
|
582 | - |
|
583 | - /** |
|
584 | - * Set the opened parenthesis for the complex SQL query |
|
585 | - * @param string $type the type of this grouped (NOT, etc.) |
|
586 | - * @param string $andOr the multiple conditions separator (AND, OR, etc.) |
|
587 | - * @return object the current Database instance |
|
588 | - */ |
|
589 | - public function groupStart($type = '', $andOr = ' AND'){ |
|
590 | - if (is_null($this->where)){ |
|
591 | - $this->where = $type . ' ('; |
|
592 | - } |
|
593 | - else{ |
|
594 | - if(substr($this->where, -1) == '('){ |
|
595 | - $this->where .= $type . ' ('; |
|
596 | - } |
|
597 | - else{ |
|
598 | - $this->where .= $andOr . ' ' . $type . ' ('; |
|
599 | - } |
|
600 | - } |
|
601 | - return $this; |
|
602 | - } |
|
603 | - |
|
604 | - /** |
|
605 | - * Set the opened parenthesis for the complex SQL query using NOT type |
|
606 | - * @see Database::groupStart() |
|
607 | - * @return object the current Database instance |
|
608 | - */ |
|
609 | - public function notGroupStart(){ |
|
610 | - return $this->groupStart('NOT'); |
|
611 | - } |
|
612 | - |
|
613 | - /** |
|
614 | - * Set the opened parenthesis for the complex SQL query using OR for separator |
|
615 | - * @see Database::groupStart() |
|
616 | - * @return object the current Database instance |
|
617 | - */ |
|
618 | - public function orGroupStart(){ |
|
619 | - return $this->groupStart('', ' OR'); |
|
620 | - } |
|
621 | - |
|
622 | - /** |
|
623 | - * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
624 | - * @see Database::groupStart() |
|
625 | - * @return object the current Database instance |
|
626 | - */ |
|
627 | - public function orNotGroupStart(){ |
|
628 | - return $this->groupStart('NOT', ' OR'); |
|
629 | - } |
|
630 | - |
|
631 | - /** |
|
632 | - * Close the parenthesis for the grouped SQL |
|
633 | - * @return object the current Database instance |
|
634 | - */ |
|
635 | - public function groupEnd(){ |
|
636 | - $this->where .= ')'; |
|
637 | - return $this; |
|
638 | - } |
|
639 | - |
|
640 | - /** |
|
641 | - * Set the SQL WHERE CLAUSE statment for IN |
|
642 | - * @param string $field the field name for IN statment |
|
643 | - * @param array $keys the list of values used |
|
644 | - * @param string $type the condition separator type (NOT) |
|
645 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
646 | - * @param boolean $escape whether to escape or not the values |
|
647 | - * @return object the current Database instance |
|
648 | - */ |
|
649 | - public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
|
650 | - if (is_array($keys)){ |
|
651 | - $_keys = array(); |
|
652 | - foreach ($keys as $k => $v){ |
|
653 | - if(is_null($v)){ |
|
654 | - $v = ''; |
|
655 | - } |
|
656 | - $_keys[] = (is_numeric($v) ? $v : ($escape ? $this->escape($v) : $v)); |
|
657 | - } |
|
658 | - $keys = implode(', ', $_keys); |
|
659 | - if (is_null($this->where)){ |
|
660 | - $this->where = $field . ' ' . $type . 'IN (' . $keys . ')'; |
|
661 | - } |
|
662 | - else{ |
|
663 | - if(substr($this->where, -1) == '('){ |
|
664 | - $this->where = $this->where . ' ' . $field . ' '.$type.'IN (' . $keys . ')'; |
|
665 | - } |
|
666 | - else{ |
|
667 | - $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' '.$type.'IN (' . $keys . ')'; |
|
668 | - } |
|
669 | - } |
|
670 | - } |
|
671 | - return $this; |
|
672 | - } |
|
673 | - |
|
674 | - /** |
|
675 | - * Set the SQL WHERE CLAUSE statment for NOT IN with AND separator |
|
676 | - * @see Database::in() |
|
677 | - * @return object the current Database instance |
|
678 | - */ |
|
679 | - public function notIn($field, array $keys, $escape = true){ |
|
680 | - return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
681 | - } |
|
682 | - |
|
683 | - /** |
|
684 | - * Set the SQL WHERE CLAUSE statment for IN with OR separator |
|
685 | - * @see Database::in() |
|
686 | - * @return object the current Database instance |
|
687 | - */ |
|
688 | - public function orIn($field, array $keys, $escape = true){ |
|
689 | - return $this->in($field, $keys, '', 'OR', $escape); |
|
690 | - } |
|
691 | - |
|
692 | - /** |
|
693 | - * Set the SQL WHERE CLAUSE statment for NOT IN with OR separator |
|
694 | - * @see Database::in() |
|
695 | - * @return object the current Database instance |
|
696 | - */ |
|
697 | - public function orNotIn($field, array $keys, $escape = true){ |
|
698 | - return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
699 | - } |
|
700 | - |
|
701 | - /** |
|
702 | - * Set the SQL WHERE CLAUSE statment for BETWEEN |
|
703 | - * @param string $field the field used for the BETWEEN statment |
|
704 | - * @param mixed $value1 the BETWEEN begin value |
|
705 | - * @param mixed $value2 the BETWEEN end value |
|
706 | - * @param string $type the condition separator type (NOT) |
|
707 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
708 | - * @param boolean $escape whether to escape or not the values |
|
709 | - * @return object the current Database instance |
|
710 | - */ |
|
711 | - public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
|
712 | - if(is_null($value1)){ |
|
713 | - $value1 = ''; |
|
714 | - } |
|
715 | - if(is_null($value2)){ |
|
716 | - $value2 = ''; |
|
717 | - } |
|
718 | - if (is_null($this->where)){ |
|
719 | - $this->where = $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
|
720 | - } |
|
721 | - else{ |
|
722 | - if(substr($this->where, -1) == '('){ |
|
723 | - $this->where = $this->where . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
|
724 | - } |
|
725 | - else{ |
|
726 | - $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
|
727 | - } |
|
728 | - } |
|
729 | - return $this; |
|
730 | - } |
|
731 | - |
|
732 | - /** |
|
733 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and AND separator |
|
734 | - * @see Database::between() |
|
735 | - * @return object the current Database instance |
|
736 | - */ |
|
737 | - public function notBetween($field, $value1, $value2, $escape = true){ |
|
738 | - return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
739 | - } |
|
740 | - |
|
741 | - /** |
|
742 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with OR separator |
|
743 | - * @see Database::between() |
|
744 | - * @return object the current Database instance |
|
745 | - */ |
|
746 | - public function orBetween($field, $value1, $value2, $escape = true){ |
|
747 | - return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
748 | - } |
|
749 | - |
|
750 | - /** |
|
751 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and OR separator |
|
752 | - * @see Database::between() |
|
753 | - * @return object the current Database instance |
|
754 | - */ |
|
755 | - public function orNotBetween($field, $value1, $value2, $escape = true){ |
|
756 | - return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
757 | - } |
|
758 | - |
|
759 | - /** |
|
760 | - * Set the SQL WHERE CLAUSE statment for LIKE |
|
761 | - * @param string $field the field name used in LIKE statment |
|
762 | - * @param string $data the LIKE value for this field including the '%', and '_' part |
|
763 | - * @param string $type the condition separator type (NOT) |
|
764 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
765 | - * @param boolean $escape whether to escape or not the values |
|
766 | - * @return object the current Database instance |
|
767 | - */ |
|
768 | - public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
|
769 | - if(is_null($data)){ |
|
770 | - $data = ''; |
|
771 | - } |
|
772 | - $like = $escape ? $this->escape($data) : $data; |
|
773 | - if (is_null($this->where)){ |
|
774 | - $this->where = $field . ' ' . $type . 'LIKE ' . $like; |
|
775 | - } |
|
776 | - else{ |
|
777 | - if(substr($this->where, -1) == '('){ |
|
778 | - $this->where = $this->where . ' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
779 | - } |
|
780 | - else{ |
|
781 | - $this->where = $this->where . ' '.$andOr.' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
782 | - } |
|
783 | - } |
|
784 | - return $this; |
|
785 | - } |
|
786 | - |
|
787 | - /** |
|
788 | - * Set the SQL WHERE CLAUSE statment for LIKE with OR separator |
|
789 | - * @see Database::like() |
|
790 | - * @return object the current Database instance |
|
791 | - */ |
|
792 | - public function orLike($field, $data, $escape = true){ |
|
793 | - return $this->like($field, $data, '', 'OR', $escape); |
|
794 | - } |
|
795 | - |
|
796 | - /** |
|
797 | - * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and AND separator |
|
798 | - * @see Database::like() |
|
799 | - * @return object the current Database instance |
|
800 | - */ |
|
801 | - public function notLike($field, $data, $escape = true){ |
|
802 | - return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
803 | - } |
|
804 | - |
|
805 | - /** |
|
806 | - * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and OR separator |
|
807 | - * @see Database::like() |
|
808 | - * @return object the current Database instance |
|
809 | - */ |
|
810 | - public function orNotLike($field, $data, $escape = true){ |
|
811 | - return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
812 | - } |
|
813 | - |
|
814 | - /** |
|
815 | - * Set the SQL LIMIT statment |
|
816 | - * @param int $limit the limit offset. If $limitEnd is null this will be the limit count |
|
817 | - * like LIMIT n; |
|
818 | - * @param int $limitEnd the limit count |
|
819 | - * @return object the current Database instance |
|
820 | - */ |
|
821 | - public function limit($limit, $limitEnd = null){ |
|
822 | - if(is_null($limit)){ |
|
823 | - return; |
|
824 | - } |
|
825 | - if (! is_null($limitEnd)){ |
|
826 | - $this->limit = $limit . ', ' . $limitEnd; |
|
827 | - } |
|
828 | - else{ |
|
829 | - $this->limit = $limit; |
|
830 | - } |
|
831 | - return $this; |
|
832 | - } |
|
833 | - |
|
834 | - /** |
|
835 | - * Set the SQL ORDER BY CLAUSE statment |
|
836 | - * @param string $orderBy the field name used for order |
|
837 | - * @param string $orderDir the order direction (ASC or DESC) |
|
838 | - * @return object the current Database instance |
|
839 | - */ |
|
840 | - public function orderBy($orderBy, $orderDir = ' ASC'){ |
|
841 | - if (! is_null($orderDir)){ |
|
842 | - $this->orderBy = ! $this->orderBy ? ($orderBy . ' ' . strtoupper($orderDir)) : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
843 | - } |
|
844 | - else{ |
|
845 | - if(stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
846 | - $this->orderBy = ! $this->orderBy ? $orderBy : $this->orderBy . ', ' . $orderBy; |
|
847 | - } |
|
848 | - else{ |
|
849 | - $this->orderBy = ! $this->orderBy ? ($orderBy . ' ASC') : $this->orderBy . ', ' . ($orderBy . ' ASC'); |
|
850 | - } |
|
851 | - } |
|
852 | - return $this; |
|
853 | - } |
|
854 | - |
|
855 | - /** |
|
856 | - * Set the SQL GROUP BY CLAUSE statment |
|
857 | - * @param string|array $field the field name used or array of field list |
|
858 | - * @return object the current Database instance |
|
859 | - */ |
|
860 | - public function groupBy($field){ |
|
861 | - if(is_array($field)){ |
|
862 | - $this->groupBy = implode(', ', $field); |
|
863 | - } |
|
864 | - else{ |
|
865 | - $this->groupBy = $field; |
|
866 | - } |
|
867 | - return $this; |
|
868 | - } |
|
869 | - |
|
870 | - /** |
|
871 | - * Set the SQL HAVING CLAUSE statment |
|
872 | - * @param string $field the field name used for HAVING statment |
|
873 | - * @param string|array $op the operator used or array |
|
874 | - * @param mixed $val the value for HAVING comparaison |
|
875 | - * @param boolean $escape whether to escape or not the values |
|
876 | - * @return object the current Database instance |
|
877 | - */ |
|
878 | - public function having($field, $op = null, $val = null, $escape = true){ |
|
879 | - if(is_array($op)){ |
|
880 | - $x = explode('?', $field); |
|
881 | - $w = ''; |
|
882 | - foreach($x as $k => $v){ |
|
883 | - if(!empty($v)){ |
|
884 | - if(isset($op[$k]) && is_null($op[$k])){ |
|
885 | - $op[$k] = ''; |
|
886 | - } |
|
887 | - $w .= $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
|
888 | - } |
|
889 | - } |
|
890 | - $this->having = $w; |
|
891 | - } |
|
892 | - else if (! in_array($op, $this->operatorList)){ |
|
893 | - if(is_null($op)){ |
|
894 | - $op = ''; |
|
895 | - } |
|
896 | - $this->having = $field . ' > ' . ($escape ? $this->escape($op) : $op); |
|
897 | - } |
|
898 | - else{ |
|
899 | - if(is_null($val)){ |
|
900 | - $val = ''; |
|
901 | - } |
|
902 | - $this->having = $field . ' ' . $op . ' ' . ($escape ? $this->escape($val) : $val); |
|
903 | - } |
|
904 | - return $this; |
|
905 | - } |
|
906 | - |
|
907 | - /** |
|
908 | - * Return the number of rows returned by the current query |
|
909 | - * @return int |
|
910 | - */ |
|
911 | - public function numRows(){ |
|
912 | - return $this->numRows; |
|
913 | - } |
|
914 | - |
|
915 | - /** |
|
916 | - * Return the last insert id value |
|
917 | - * @return mixed |
|
918 | - */ |
|
919 | - public function insertId(){ |
|
920 | - return $this->insertId; |
|
921 | - } |
|
922 | - |
|
923 | - /** |
|
924 | - * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.) |
|
925 | - */ |
|
926 | - public function error(){ |
|
493 | + /** |
|
494 | + * Set the SQL WHERE CLAUSE statment |
|
495 | + * @param string|array $where the where field or array of field list |
|
496 | + * @param string $op the condition operator. If is null the default will be "=" |
|
497 | + * @param mixed $val the where value |
|
498 | + * @param string $type the type used for this where clause (NOT, etc.) |
|
499 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
500 | + * @param boolean $escape whether to escape or not the $val |
|
501 | + * @return object the current Database instance |
|
502 | + */ |
|
503 | + public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
|
504 | + if (is_array($where)){ |
|
505 | + $_where = array(); |
|
506 | + foreach ($where as $column => $data){ |
|
507 | + if(is_null($data)){ |
|
508 | + $data = ''; |
|
509 | + } |
|
510 | + $_where[] = $type . $column . '=' . ($escape ? $this->escape($data) : $data); |
|
511 | + } |
|
512 | + $where = implode(' '.$andOr.' ', $_where); |
|
513 | + } |
|
514 | + else{ |
|
515 | + if(is_array($op)){ |
|
516 | + $x = explode('?', $where); |
|
517 | + $w = ''; |
|
518 | + foreach($x as $k => $v){ |
|
519 | + if(! empty($v)){ |
|
520 | + if(isset($op[$k]) && is_null($op[$k])){ |
|
521 | + $op[$k] = ''; |
|
522 | + } |
|
523 | + $w .= $type . $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
|
524 | + } |
|
525 | + } |
|
526 | + $where = $w; |
|
527 | + } |
|
528 | + else if (! in_array((string)$op, $this->operatorList)){ |
|
529 | + if(is_null($op)){ |
|
530 | + $op = ''; |
|
531 | + } |
|
532 | + $where = $type . $where . ' = ' . ($escape ? $this->escape($op) : $op); |
|
533 | + } |
|
534 | + else{ |
|
535 | + if(is_null($val)){ |
|
536 | + $val = ''; |
|
537 | + } |
|
538 | + $where = $type . $where . $op . ($escape ? $this->escape($val) : $val); |
|
539 | + } |
|
540 | + } |
|
541 | + if (is_null($this->where)){ |
|
542 | + $this->where = $where; |
|
543 | + } |
|
544 | + else{ |
|
545 | + if(substr($this->where, -1) == '('){ |
|
546 | + $this->where = $this->where . ' ' . $where; |
|
547 | + } |
|
548 | + else{ |
|
549 | + $this->where = $this->where . ' '.$andOr.' ' . $where; |
|
550 | + } |
|
551 | + } |
|
552 | + return $this; |
|
553 | + } |
|
554 | + |
|
555 | + /** |
|
556 | + * Set the SQL WHERE CLAUSE statment using OR |
|
557 | + * @see Database::where() |
|
558 | + * @return object the current Database instance |
|
559 | + */ |
|
560 | + public function orWhere($where, $op = null, $val = null, $escape = true){ |
|
561 | + return $this->where($where, $op, $val, '', 'OR', $escape); |
|
562 | + } |
|
563 | + |
|
564 | + |
|
565 | + /** |
|
566 | + * Set the SQL WHERE CLAUSE statment using AND and NOT |
|
567 | + * @see Database::where() |
|
568 | + * @return object the current Database instance |
|
569 | + */ |
|
570 | + public function notWhere($where, $op = null, $val = null, $escape = true){ |
|
571 | + return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
572 | + } |
|
573 | + |
|
574 | + /** |
|
575 | + * Set the SQL WHERE CLAUSE statment using OR and NOT |
|
576 | + * @see Database::where() |
|
577 | + * @return object the current Database instance |
|
578 | + */ |
|
579 | + public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
|
580 | + return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
581 | + } |
|
582 | + |
|
583 | + /** |
|
584 | + * Set the opened parenthesis for the complex SQL query |
|
585 | + * @param string $type the type of this grouped (NOT, etc.) |
|
586 | + * @param string $andOr the multiple conditions separator (AND, OR, etc.) |
|
587 | + * @return object the current Database instance |
|
588 | + */ |
|
589 | + public function groupStart($type = '', $andOr = ' AND'){ |
|
590 | + if (is_null($this->where)){ |
|
591 | + $this->where = $type . ' ('; |
|
592 | + } |
|
593 | + else{ |
|
594 | + if(substr($this->where, -1) == '('){ |
|
595 | + $this->where .= $type . ' ('; |
|
596 | + } |
|
597 | + else{ |
|
598 | + $this->where .= $andOr . ' ' . $type . ' ('; |
|
599 | + } |
|
600 | + } |
|
601 | + return $this; |
|
602 | + } |
|
603 | + |
|
604 | + /** |
|
605 | + * Set the opened parenthesis for the complex SQL query using NOT type |
|
606 | + * @see Database::groupStart() |
|
607 | + * @return object the current Database instance |
|
608 | + */ |
|
609 | + public function notGroupStart(){ |
|
610 | + return $this->groupStart('NOT'); |
|
611 | + } |
|
612 | + |
|
613 | + /** |
|
614 | + * Set the opened parenthesis for the complex SQL query using OR for separator |
|
615 | + * @see Database::groupStart() |
|
616 | + * @return object the current Database instance |
|
617 | + */ |
|
618 | + public function orGroupStart(){ |
|
619 | + return $this->groupStart('', ' OR'); |
|
620 | + } |
|
621 | + |
|
622 | + /** |
|
623 | + * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
624 | + * @see Database::groupStart() |
|
625 | + * @return object the current Database instance |
|
626 | + */ |
|
627 | + public function orNotGroupStart(){ |
|
628 | + return $this->groupStart('NOT', ' OR'); |
|
629 | + } |
|
630 | + |
|
631 | + /** |
|
632 | + * Close the parenthesis for the grouped SQL |
|
633 | + * @return object the current Database instance |
|
634 | + */ |
|
635 | + public function groupEnd(){ |
|
636 | + $this->where .= ')'; |
|
637 | + return $this; |
|
638 | + } |
|
639 | + |
|
640 | + /** |
|
641 | + * Set the SQL WHERE CLAUSE statment for IN |
|
642 | + * @param string $field the field name for IN statment |
|
643 | + * @param array $keys the list of values used |
|
644 | + * @param string $type the condition separator type (NOT) |
|
645 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
646 | + * @param boolean $escape whether to escape or not the values |
|
647 | + * @return object the current Database instance |
|
648 | + */ |
|
649 | + public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
|
650 | + if (is_array($keys)){ |
|
651 | + $_keys = array(); |
|
652 | + foreach ($keys as $k => $v){ |
|
653 | + if(is_null($v)){ |
|
654 | + $v = ''; |
|
655 | + } |
|
656 | + $_keys[] = (is_numeric($v) ? $v : ($escape ? $this->escape($v) : $v)); |
|
657 | + } |
|
658 | + $keys = implode(', ', $_keys); |
|
659 | + if (is_null($this->where)){ |
|
660 | + $this->where = $field . ' ' . $type . 'IN (' . $keys . ')'; |
|
661 | + } |
|
662 | + else{ |
|
663 | + if(substr($this->where, -1) == '('){ |
|
664 | + $this->where = $this->where . ' ' . $field . ' '.$type.'IN (' . $keys . ')'; |
|
665 | + } |
|
666 | + else{ |
|
667 | + $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' '.$type.'IN (' . $keys . ')'; |
|
668 | + } |
|
669 | + } |
|
670 | + } |
|
671 | + return $this; |
|
672 | + } |
|
673 | + |
|
674 | + /** |
|
675 | + * Set the SQL WHERE CLAUSE statment for NOT IN with AND separator |
|
676 | + * @see Database::in() |
|
677 | + * @return object the current Database instance |
|
678 | + */ |
|
679 | + public function notIn($field, array $keys, $escape = true){ |
|
680 | + return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
681 | + } |
|
682 | + |
|
683 | + /** |
|
684 | + * Set the SQL WHERE CLAUSE statment for IN with OR separator |
|
685 | + * @see Database::in() |
|
686 | + * @return object the current Database instance |
|
687 | + */ |
|
688 | + public function orIn($field, array $keys, $escape = true){ |
|
689 | + return $this->in($field, $keys, '', 'OR', $escape); |
|
690 | + } |
|
691 | + |
|
692 | + /** |
|
693 | + * Set the SQL WHERE CLAUSE statment for NOT IN with OR separator |
|
694 | + * @see Database::in() |
|
695 | + * @return object the current Database instance |
|
696 | + */ |
|
697 | + public function orNotIn($field, array $keys, $escape = true){ |
|
698 | + return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
699 | + } |
|
700 | + |
|
701 | + /** |
|
702 | + * Set the SQL WHERE CLAUSE statment for BETWEEN |
|
703 | + * @param string $field the field used for the BETWEEN statment |
|
704 | + * @param mixed $value1 the BETWEEN begin value |
|
705 | + * @param mixed $value2 the BETWEEN end value |
|
706 | + * @param string $type the condition separator type (NOT) |
|
707 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
708 | + * @param boolean $escape whether to escape or not the values |
|
709 | + * @return object the current Database instance |
|
710 | + */ |
|
711 | + public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
|
712 | + if(is_null($value1)){ |
|
713 | + $value1 = ''; |
|
714 | + } |
|
715 | + if(is_null($value2)){ |
|
716 | + $value2 = ''; |
|
717 | + } |
|
718 | + if (is_null($this->where)){ |
|
719 | + $this->where = $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
|
720 | + } |
|
721 | + else{ |
|
722 | + if(substr($this->where, -1) == '('){ |
|
723 | + $this->where = $this->where . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
|
724 | + } |
|
725 | + else{ |
|
726 | + $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
|
727 | + } |
|
728 | + } |
|
729 | + return $this; |
|
730 | + } |
|
731 | + |
|
732 | + /** |
|
733 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and AND separator |
|
734 | + * @see Database::between() |
|
735 | + * @return object the current Database instance |
|
736 | + */ |
|
737 | + public function notBetween($field, $value1, $value2, $escape = true){ |
|
738 | + return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
739 | + } |
|
740 | + |
|
741 | + /** |
|
742 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with OR separator |
|
743 | + * @see Database::between() |
|
744 | + * @return object the current Database instance |
|
745 | + */ |
|
746 | + public function orBetween($field, $value1, $value2, $escape = true){ |
|
747 | + return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
748 | + } |
|
749 | + |
|
750 | + /** |
|
751 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and OR separator |
|
752 | + * @see Database::between() |
|
753 | + * @return object the current Database instance |
|
754 | + */ |
|
755 | + public function orNotBetween($field, $value1, $value2, $escape = true){ |
|
756 | + return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
757 | + } |
|
758 | + |
|
759 | + /** |
|
760 | + * Set the SQL WHERE CLAUSE statment for LIKE |
|
761 | + * @param string $field the field name used in LIKE statment |
|
762 | + * @param string $data the LIKE value for this field including the '%', and '_' part |
|
763 | + * @param string $type the condition separator type (NOT) |
|
764 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
765 | + * @param boolean $escape whether to escape or not the values |
|
766 | + * @return object the current Database instance |
|
767 | + */ |
|
768 | + public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
|
769 | + if(is_null($data)){ |
|
770 | + $data = ''; |
|
771 | + } |
|
772 | + $like = $escape ? $this->escape($data) : $data; |
|
773 | + if (is_null($this->where)){ |
|
774 | + $this->where = $field . ' ' . $type . 'LIKE ' . $like; |
|
775 | + } |
|
776 | + else{ |
|
777 | + if(substr($this->where, -1) == '('){ |
|
778 | + $this->where = $this->where . ' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
779 | + } |
|
780 | + else{ |
|
781 | + $this->where = $this->where . ' '.$andOr.' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
782 | + } |
|
783 | + } |
|
784 | + return $this; |
|
785 | + } |
|
786 | + |
|
787 | + /** |
|
788 | + * Set the SQL WHERE CLAUSE statment for LIKE with OR separator |
|
789 | + * @see Database::like() |
|
790 | + * @return object the current Database instance |
|
791 | + */ |
|
792 | + public function orLike($field, $data, $escape = true){ |
|
793 | + return $this->like($field, $data, '', 'OR', $escape); |
|
794 | + } |
|
795 | + |
|
796 | + /** |
|
797 | + * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and AND separator |
|
798 | + * @see Database::like() |
|
799 | + * @return object the current Database instance |
|
800 | + */ |
|
801 | + public function notLike($field, $data, $escape = true){ |
|
802 | + return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
803 | + } |
|
804 | + |
|
805 | + /** |
|
806 | + * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and OR separator |
|
807 | + * @see Database::like() |
|
808 | + * @return object the current Database instance |
|
809 | + */ |
|
810 | + public function orNotLike($field, $data, $escape = true){ |
|
811 | + return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
812 | + } |
|
813 | + |
|
814 | + /** |
|
815 | + * Set the SQL LIMIT statment |
|
816 | + * @param int $limit the limit offset. If $limitEnd is null this will be the limit count |
|
817 | + * like LIMIT n; |
|
818 | + * @param int $limitEnd the limit count |
|
819 | + * @return object the current Database instance |
|
820 | + */ |
|
821 | + public function limit($limit, $limitEnd = null){ |
|
822 | + if(is_null($limit)){ |
|
823 | + return; |
|
824 | + } |
|
825 | + if (! is_null($limitEnd)){ |
|
826 | + $this->limit = $limit . ', ' . $limitEnd; |
|
827 | + } |
|
828 | + else{ |
|
829 | + $this->limit = $limit; |
|
830 | + } |
|
831 | + return $this; |
|
832 | + } |
|
833 | + |
|
834 | + /** |
|
835 | + * Set the SQL ORDER BY CLAUSE statment |
|
836 | + * @param string $orderBy the field name used for order |
|
837 | + * @param string $orderDir the order direction (ASC or DESC) |
|
838 | + * @return object the current Database instance |
|
839 | + */ |
|
840 | + public function orderBy($orderBy, $orderDir = ' ASC'){ |
|
841 | + if (! is_null($orderDir)){ |
|
842 | + $this->orderBy = ! $this->orderBy ? ($orderBy . ' ' . strtoupper($orderDir)) : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
843 | + } |
|
844 | + else{ |
|
845 | + if(stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
846 | + $this->orderBy = ! $this->orderBy ? $orderBy : $this->orderBy . ', ' . $orderBy; |
|
847 | + } |
|
848 | + else{ |
|
849 | + $this->orderBy = ! $this->orderBy ? ($orderBy . ' ASC') : $this->orderBy . ', ' . ($orderBy . ' ASC'); |
|
850 | + } |
|
851 | + } |
|
852 | + return $this; |
|
853 | + } |
|
854 | + |
|
855 | + /** |
|
856 | + * Set the SQL GROUP BY CLAUSE statment |
|
857 | + * @param string|array $field the field name used or array of field list |
|
858 | + * @return object the current Database instance |
|
859 | + */ |
|
860 | + public function groupBy($field){ |
|
861 | + if(is_array($field)){ |
|
862 | + $this->groupBy = implode(', ', $field); |
|
863 | + } |
|
864 | + else{ |
|
865 | + $this->groupBy = $field; |
|
866 | + } |
|
867 | + return $this; |
|
868 | + } |
|
869 | + |
|
870 | + /** |
|
871 | + * Set the SQL HAVING CLAUSE statment |
|
872 | + * @param string $field the field name used for HAVING statment |
|
873 | + * @param string|array $op the operator used or array |
|
874 | + * @param mixed $val the value for HAVING comparaison |
|
875 | + * @param boolean $escape whether to escape or not the values |
|
876 | + * @return object the current Database instance |
|
877 | + */ |
|
878 | + public function having($field, $op = null, $val = null, $escape = true){ |
|
879 | + if(is_array($op)){ |
|
880 | + $x = explode('?', $field); |
|
881 | + $w = ''; |
|
882 | + foreach($x as $k => $v){ |
|
883 | + if(!empty($v)){ |
|
884 | + if(isset($op[$k]) && is_null($op[$k])){ |
|
885 | + $op[$k] = ''; |
|
886 | + } |
|
887 | + $w .= $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
|
888 | + } |
|
889 | + } |
|
890 | + $this->having = $w; |
|
891 | + } |
|
892 | + else if (! in_array($op, $this->operatorList)){ |
|
893 | + if(is_null($op)){ |
|
894 | + $op = ''; |
|
895 | + } |
|
896 | + $this->having = $field . ' > ' . ($escape ? $this->escape($op) : $op); |
|
897 | + } |
|
898 | + else{ |
|
899 | + if(is_null($val)){ |
|
900 | + $val = ''; |
|
901 | + } |
|
902 | + $this->having = $field . ' ' . $op . ' ' . ($escape ? $this->escape($val) : $val); |
|
903 | + } |
|
904 | + return $this; |
|
905 | + } |
|
906 | + |
|
907 | + /** |
|
908 | + * Return the number of rows returned by the current query |
|
909 | + * @return int |
|
910 | + */ |
|
911 | + public function numRows(){ |
|
912 | + return $this->numRows; |
|
913 | + } |
|
914 | + |
|
915 | + /** |
|
916 | + * Return the last insert id value |
|
917 | + * @return mixed |
|
918 | + */ |
|
919 | + public function insertId(){ |
|
920 | + return $this->insertId; |
|
921 | + } |
|
922 | + |
|
923 | + /** |
|
924 | + * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.) |
|
925 | + */ |
|
926 | + public function error(){ |
|
927 | 927 | if($this->error){ |
928 | 928 | show_error('Query: "' . $this->query . '" Error: ' . $this->error, 'Database Error'); |
929 | 929 | } |
930 | - } |
|
931 | - |
|
932 | - /** |
|
933 | - * Get the result of one record rows returned by the current query |
|
934 | - * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
935 | - * If is string will determine the result type "array" or "object" |
|
936 | - * @return mixed the query SQL string or the record result |
|
937 | - */ |
|
938 | - public function get($returnSQLQueryOrResultType = false){ |
|
939 | - $this->limit = 1; |
|
940 | - $query = $this->getAll(true); |
|
941 | - if($returnSQLQueryOrResultType === true){ |
|
942 | - return $query; |
|
943 | - } |
|
944 | - else{ |
|
945 | - return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
946 | - } |
|
947 | - } |
|
948 | - |
|
949 | - /** |
|
950 | - * Get the result of record rows list returned by the current query |
|
951 | - * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
952 | - * If is string will determine the result type "array" or "object" |
|
953 | - * @return mixed the query SQL string or the record result |
|
954 | - */ |
|
955 | - public function getAll($returnSQLQueryOrResultType = false){ |
|
956 | - $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
|
957 | - if (! is_null($this->join)){ |
|
958 | - $query .= $this->join; |
|
959 | - } |
|
930 | + } |
|
931 | + |
|
932 | + /** |
|
933 | + * Get the result of one record rows returned by the current query |
|
934 | + * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
935 | + * If is string will determine the result type "array" or "object" |
|
936 | + * @return mixed the query SQL string or the record result |
|
937 | + */ |
|
938 | + public function get($returnSQLQueryOrResultType = false){ |
|
939 | + $this->limit = 1; |
|
940 | + $query = $this->getAll(true); |
|
941 | + if($returnSQLQueryOrResultType === true){ |
|
942 | + return $query; |
|
943 | + } |
|
944 | + else{ |
|
945 | + return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
946 | + } |
|
947 | + } |
|
948 | + |
|
949 | + /** |
|
950 | + * Get the result of record rows list returned by the current query |
|
951 | + * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
952 | + * If is string will determine the result type "array" or "object" |
|
953 | + * @return mixed the query SQL string or the record result |
|
954 | + */ |
|
955 | + public function getAll($returnSQLQueryOrResultType = false){ |
|
956 | + $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
|
957 | + if (! is_null($this->join)){ |
|
958 | + $query .= $this->join; |
|
959 | + } |
|
960 | 960 | |
961 | - if (! is_null($this->where)){ |
|
962 | - $query .= ' WHERE ' . $this->where; |
|
963 | - } |
|
961 | + if (! is_null($this->where)){ |
|
962 | + $query .= ' WHERE ' . $this->where; |
|
963 | + } |
|
964 | 964 | |
965 | - if (! is_null($this->groupBy)){ |
|
966 | - $query .= ' GROUP BY ' . $this->groupBy; |
|
967 | - } |
|
965 | + if (! is_null($this->groupBy)){ |
|
966 | + $query .= ' GROUP BY ' . $this->groupBy; |
|
967 | + } |
|
968 | 968 | |
969 | - if (! is_null($this->having)){ |
|
970 | - $query .= ' HAVING ' . $this->having; |
|
971 | - } |
|
969 | + if (! is_null($this->having)){ |
|
970 | + $query .= ' HAVING ' . $this->having; |
|
971 | + } |
|
972 | 972 | |
973 | - if (! is_null($this->orderBy)){ |
|
974 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
975 | - } |
|
973 | + if (! is_null($this->orderBy)){ |
|
974 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
975 | + } |
|
976 | 976 | |
977 | - if(! is_null($this->limit)){ |
|
978 | - $query .= ' LIMIT ' . $this->limit; |
|
979 | - } |
|
977 | + if(! is_null($this->limit)){ |
|
978 | + $query .= ' LIMIT ' . $this->limit; |
|
979 | + } |
|
980 | 980 | |
981 | 981 | if($returnSQLQueryOrResultType === true){ |
982 | - return $query; |
|
983 | - } |
|
984 | - else{ |
|
985 | - return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
986 | - } |
|
987 | - } |
|
988 | - |
|
989 | - /** |
|
990 | - * Insert new record in the database |
|
991 | - * @param array $data the record data if is empty will use the $this->data array. |
|
992 | - * @param boolean $escape whether to escape or not the values |
|
993 | - * @return mixed the insert id of the new record or null |
|
994 | - */ |
|
995 | - public function insert($data = array(), $escape = true){ |
|
996 | - $column = array(); |
|
997 | - $val = array(); |
|
998 | - if(! $data && $this->getData()){ |
|
999 | - $columns = array_keys($this->getData()); |
|
1000 | - $column = implode(',', $columns); |
|
1001 | - $val = implode(', ', $this->getData()); |
|
1002 | - } |
|
1003 | - else{ |
|
1004 | - $columns = array_keys($data); |
|
1005 | - $column = implode(',', $columns); |
|
1006 | - $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
1007 | - } |
|
1008 | - |
|
1009 | - $query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
1010 | - $query = $this->query($query); |
|
1011 | - |
|
1012 | - if ($query){ |
|
1013 | - if(! $this->pdo){ |
|
1014 | - $this->connect(); |
|
1015 | - } |
|
1016 | - $this->insertId = $this->pdo->lastInsertId(); |
|
1017 | - return $this->insertId(); |
|
1018 | - } |
|
1019 | - else{ |
|
982 | + return $query; |
|
983 | + } |
|
984 | + else{ |
|
985 | + return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
986 | + } |
|
987 | + } |
|
988 | + |
|
989 | + /** |
|
990 | + * Insert new record in the database |
|
991 | + * @param array $data the record data if is empty will use the $this->data array. |
|
992 | + * @param boolean $escape whether to escape or not the values |
|
993 | + * @return mixed the insert id of the new record or null |
|
994 | + */ |
|
995 | + public function insert($data = array(), $escape = true){ |
|
996 | + $column = array(); |
|
997 | + $val = array(); |
|
998 | + if(! $data && $this->getData()){ |
|
999 | + $columns = array_keys($this->getData()); |
|
1000 | + $column = implode(',', $columns); |
|
1001 | + $val = implode(', ', $this->getData()); |
|
1002 | + } |
|
1003 | + else{ |
|
1004 | + $columns = array_keys($data); |
|
1005 | + $column = implode(',', $columns); |
|
1006 | + $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
1007 | + } |
|
1008 | + |
|
1009 | + $query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
1010 | + $query = $this->query($query); |
|
1011 | + |
|
1012 | + if ($query){ |
|
1013 | + if(! $this->pdo){ |
|
1014 | + $this->connect(); |
|
1015 | + } |
|
1016 | + $this->insertId = $this->pdo->lastInsertId(); |
|
1017 | + return $this->insertId(); |
|
1018 | + } |
|
1019 | + else{ |
|
1020 | 1020 | return false; |
1021 | - } |
|
1022 | - } |
|
1023 | - |
|
1024 | - /** |
|
1025 | - * Update record in the database |
|
1026 | - * @param array $data the record data if is empty will use the $this->data array. |
|
1027 | - * @param boolean $escape whether to escape or not the values |
|
1028 | - * @return mixed the update status |
|
1029 | - */ |
|
1030 | - public function update($data = array(), $escape = true){ |
|
1031 | - $query = 'UPDATE ' . $this->from . ' SET '; |
|
1032 | - $values = array(); |
|
1033 | - if(! $data && $this->getData()){ |
|
1034 | - foreach ($this->getData() as $column => $val){ |
|
1035 | - $values[] = $column . ' = ' . $val; |
|
1036 | - } |
|
1037 | - } |
|
1038 | - else{ |
|
1039 | - foreach ($data as $column => $val){ |
|
1040 | - $values[] = $column . '=' . ($escape ? $this->escape($val) : $val); |
|
1041 | - } |
|
1042 | - } |
|
1043 | - $query .= (is_array($data) ? implode(', ', $values) : $data); |
|
1044 | - if (! is_null($this->where)){ |
|
1045 | - $query .= ' WHERE ' . $this->where; |
|
1046 | - } |
|
1047 | - |
|
1048 | - if (! is_null($this->orderBy)){ |
|
1049 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
1050 | - } |
|
1051 | - |
|
1052 | - if (! is_null($this->limit)){ |
|
1053 | - $query .= ' LIMIT ' . $this->limit; |
|
1054 | - } |
|
1055 | - return $this->query($query); |
|
1056 | - } |
|
1057 | - |
|
1058 | - /** |
|
1059 | - * Delete the record in database |
|
1060 | - * @return mixed the delete status |
|
1061 | - */ |
|
1062 | - public function delete(){ |
|
1063 | - $query = 'DELETE FROM ' . $this->from; |
|
1064 | - |
|
1065 | - if (! is_null($this->where)){ |
|
1066 | - $query .= ' WHERE ' . $this->where; |
|
1067 | - } |
|
1068 | - |
|
1069 | - if (! is_null($this->orderBy)){ |
|
1070 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
1071 | - } |
|
1072 | - |
|
1073 | - if (! is_null($this->limit)){ |
|
1074 | - $query .= ' LIMIT ' . $this->limit; |
|
1075 | - } |
|
1076 | - |
|
1077 | - if($query == 'DELETE FROM ' . $this->from && $this->config['driver'] != 'sqlite'){ |
|
1078 | - $query = 'TRUNCATE TABLE ' . $this->from; |
|
1079 | - } |
|
1080 | - return $this->query($query); |
|
1081 | - } |
|
1082 | - |
|
1083 | - /** |
|
1084 | - * Execute an SQL query |
|
1085 | - * @param string $query the query SQL string |
|
1086 | - * @param boolean $all whether to return all record or not |
|
1087 | - * @param boolean $array return the result as array |
|
1088 | - * @return mixed the query result |
|
1089 | - */ |
|
1090 | - public function query($query, $all = true, $array = false){ |
|
1091 | - $this->reset(); |
|
1092 | - if(is_array($all)){ |
|
1093 | - $x = explode('?', $query); |
|
1094 | - $q = ''; |
|
1095 | - foreach($x as $k => $v){ |
|
1096 | - if(! empty($v)){ |
|
1097 | - $q .= $v . (isset($all[$k]) ? $this->escape($all[$k]) : ''); |
|
1098 | - } |
|
1099 | - } |
|
1100 | - $query = $q; |
|
1101 | - } |
|
1102 | - |
|
1103 | - $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
1104 | - $sqlSELECTQuery = stristr($this->query, 'SELECT'); |
|
1105 | - $this->logger->info('Execute SQL query ['.$this->query.'], return type: ' . ($array?'ARRAY':'OBJECT') .', return as list: ' . ($all ? 'YES':'NO')); |
|
1106 | - //cache expire time |
|
1021 | + } |
|
1022 | + } |
|
1023 | + |
|
1024 | + /** |
|
1025 | + * Update record in the database |
|
1026 | + * @param array $data the record data if is empty will use the $this->data array. |
|
1027 | + * @param boolean $escape whether to escape or not the values |
|
1028 | + * @return mixed the update status |
|
1029 | + */ |
|
1030 | + public function update($data = array(), $escape = true){ |
|
1031 | + $query = 'UPDATE ' . $this->from . ' SET '; |
|
1032 | + $values = array(); |
|
1033 | + if(! $data && $this->getData()){ |
|
1034 | + foreach ($this->getData() as $column => $val){ |
|
1035 | + $values[] = $column . ' = ' . $val; |
|
1036 | + } |
|
1037 | + } |
|
1038 | + else{ |
|
1039 | + foreach ($data as $column => $val){ |
|
1040 | + $values[] = $column . '=' . ($escape ? $this->escape($val) : $val); |
|
1041 | + } |
|
1042 | + } |
|
1043 | + $query .= (is_array($data) ? implode(', ', $values) : $data); |
|
1044 | + if (! is_null($this->where)){ |
|
1045 | + $query .= ' WHERE ' . $this->where; |
|
1046 | + } |
|
1047 | + |
|
1048 | + if (! is_null($this->orderBy)){ |
|
1049 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
1050 | + } |
|
1051 | + |
|
1052 | + if (! is_null($this->limit)){ |
|
1053 | + $query .= ' LIMIT ' . $this->limit; |
|
1054 | + } |
|
1055 | + return $this->query($query); |
|
1056 | + } |
|
1057 | + |
|
1058 | + /** |
|
1059 | + * Delete the record in database |
|
1060 | + * @return mixed the delete status |
|
1061 | + */ |
|
1062 | + public function delete(){ |
|
1063 | + $query = 'DELETE FROM ' . $this->from; |
|
1064 | + |
|
1065 | + if (! is_null($this->where)){ |
|
1066 | + $query .= ' WHERE ' . $this->where; |
|
1067 | + } |
|
1068 | + |
|
1069 | + if (! is_null($this->orderBy)){ |
|
1070 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
1071 | + } |
|
1072 | + |
|
1073 | + if (! is_null($this->limit)){ |
|
1074 | + $query .= ' LIMIT ' . $this->limit; |
|
1075 | + } |
|
1076 | + |
|
1077 | + if($query == 'DELETE FROM ' . $this->from && $this->config['driver'] != 'sqlite'){ |
|
1078 | + $query = 'TRUNCATE TABLE ' . $this->from; |
|
1079 | + } |
|
1080 | + return $this->query($query); |
|
1081 | + } |
|
1082 | + |
|
1083 | + /** |
|
1084 | + * Execute an SQL query |
|
1085 | + * @param string $query the query SQL string |
|
1086 | + * @param boolean $all whether to return all record or not |
|
1087 | + * @param boolean $array return the result as array |
|
1088 | + * @return mixed the query result |
|
1089 | + */ |
|
1090 | + public function query($query, $all = true, $array = false){ |
|
1091 | + $this->reset(); |
|
1092 | + if(is_array($all)){ |
|
1093 | + $x = explode('?', $query); |
|
1094 | + $q = ''; |
|
1095 | + foreach($x as $k => $v){ |
|
1096 | + if(! empty($v)){ |
|
1097 | + $q .= $v . (isset($all[$k]) ? $this->escape($all[$k]) : ''); |
|
1098 | + } |
|
1099 | + } |
|
1100 | + $query = $q; |
|
1101 | + } |
|
1102 | + |
|
1103 | + $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
1104 | + $sqlSELECTQuery = stristr($this->query, 'SELECT'); |
|
1105 | + $this->logger->info('Execute SQL query ['.$this->query.'], return type: ' . ($array?'ARRAY':'OBJECT') .', return as list: ' . ($all ? 'YES':'NO')); |
|
1106 | + //cache expire time |
|
1107 | 1107 | $cacheExpire = $this->temporaryCacheTtl; |
1108 | 1108 | |
1109 | 1109 | //return to the initial cache time |
1110 | 1110 | $this->temporaryCacheTtl = $this->cacheTtl; |
1111 | 1111 | |
1112 | 1112 | //config for cache |
1113 | - $cacheEnable = get_config('cache_enable'); |
|
1113 | + $cacheEnable = get_config('cache_enable'); |
|
1114 | 1114 | |
1115 | 1115 | //the database cache content |
1116 | - $cacheContent = null; |
|
1116 | + $cacheContent = null; |
|
1117 | 1117 | |
1118 | 1118 | //this database query cache key |
1119 | - $cacheKey = null; |
|
1119 | + $cacheKey = null; |
|
1120 | 1120 | |
1121 | 1121 | //the cache manager instance |
1122 | - $cacheInstance = null; |
|
1122 | + $cacheInstance = null; |
|
1123 | 1123 | |
1124 | 1124 | //if can use cache feature for this query |
1125 | 1125 | $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
1126 | 1126 | |
1127 | - if ($dbCacheStatus && $sqlSELECTQuery){ |
|
1128 | - $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
1129 | - $cacheKey = md5($query . $all . $array); |
|
1130 | - if(is_object($this->cacheInstance)){ |
|
1131 | - $cacheInstance = $this->cacheInstance; |
|
1132 | - } |
|
1133 | - else{ |
|
1134 | - $obj = & get_instance(); |
|
1135 | - $cacheInstance = $obj->cache; |
|
1136 | - } |
|
1137 | - $cacheContent = $cacheInstance->get($cacheKey); |
|
1138 | - } |
|
1139 | - else{ |
|
1127 | + if ($dbCacheStatus && $sqlSELECTQuery){ |
|
1128 | + $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
1129 | + $cacheKey = md5($query . $all . $array); |
|
1130 | + if(is_object($this->cacheInstance)){ |
|
1131 | + $cacheInstance = $this->cacheInstance; |
|
1132 | + } |
|
1133 | + else{ |
|
1134 | + $obj = & get_instance(); |
|
1135 | + $cacheInstance = $obj->cache; |
|
1136 | + } |
|
1137 | + $cacheContent = $cacheInstance->get($cacheKey); |
|
1138 | + } |
|
1139 | + else{ |
|
1140 | 1140 | $this->logger->info('The cache is not enabled for this query or is not the SELECT query, get the result directly from real database'); |
1141 | - } |
|
1141 | + } |
|
1142 | 1142 | |
1143 | - if(! $this->pdo){ |
|
1144 | - $this->connect(); |
|
1145 | - } |
|
1143 | + if(! $this->pdo){ |
|
1144 | + $this->connect(); |
|
1145 | + } |
|
1146 | 1146 | |
1147 | - if (! $cacheContent && $sqlSELECTQuery){ |
|
1148 | - //for database query execution time |
|
1149 | - $benchmarkMarkerKey = md5($query . $all . $array); |
|
1150 | - $bench = null; |
|
1151 | - if(is_object($this->benchmarkInstance)){ |
|
1152 | - $bench = $this->benchmarkInstance; |
|
1153 | - } |
|
1154 | - else{ |
|
1155 | - $obj = & get_instance(); |
|
1156 | - $bench = $obj->benchmark; |
|
1157 | - } |
|
1158 | - $bench->mark('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')'); |
|
1159 | - //Now execute the query |
|
1160 | - $sqlQuery = $this->pdo->query($this->query); |
|
1147 | + if (! $cacheContent && $sqlSELECTQuery){ |
|
1148 | + //for database query execution time |
|
1149 | + $benchmarkMarkerKey = md5($query . $all . $array); |
|
1150 | + $bench = null; |
|
1151 | + if(is_object($this->benchmarkInstance)){ |
|
1152 | + $bench = $this->benchmarkInstance; |
|
1153 | + } |
|
1154 | + else{ |
|
1155 | + $obj = & get_instance(); |
|
1156 | + $bench = $obj->benchmark; |
|
1157 | + } |
|
1158 | + $bench->mark('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')'); |
|
1159 | + //Now execute the query |
|
1160 | + $sqlQuery = $this->pdo->query($this->query); |
|
1161 | 1161 | |
1162 | - //get response time for this query |
|
1163 | - $responseTime = $bench->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')'); |
|
1164 | - //TODO use the configuration value for the high response time currently is 1 second |
|
1165 | - if($responseTime >= 1 ){ |
|
1166 | - $this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.'); |
|
1167 | - } |
|
1168 | - if ($sqlQuery){ |
|
1169 | - //if need return all result like list of record |
|
1170 | - if ($all){ |
|
1171 | - $this->result = ($array == false) ? $sqlQuery->fetchAll(PDO::FETCH_OBJ) : $sqlQuery->fetchAll(PDO::FETCH_ASSOC); |
|
1172 | - } |
|
1173 | - else{ |
|
1174 | - $this->result = ($array == false) ? $sqlQuery->fetch(PDO::FETCH_OBJ) : $sqlQuery->fetch(PDO::FETCH_ASSOC); |
|
1175 | - } |
|
1176 | - //Sqlite and pgsql always return 0 when using rowCount() |
|
1177 | - if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
1178 | - $this->numRows = count($this->result); |
|
1179 | - } |
|
1180 | - else{ |
|
1181 | - $this->numRows = $sqlQuery->rowCount(); |
|
1182 | - } |
|
1183 | - |
|
1184 | - if ($dbCacheStatus && $sqlSELECTQuery){ |
|
1185 | - $this->logger->info('Save the result for query [' .$this->query. '] into cache for future use'); |
|
1186 | - $cacheInstance->set($cacheKey, $this->result, $cacheExpire); |
|
1187 | - } |
|
1188 | - } |
|
1189 | - else{ |
|
1190 | - $error = $this->pdo->errorInfo(); |
|
1191 | - $this->error = isset($error[2]) ? $error[2] : ''; |
|
1192 | - $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
|
1193 | - $this->error(); |
|
1194 | - } |
|
1195 | - } |
|
1196 | - else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){ |
|
1197 | - $queryStr = $this->pdo->query($this->query); |
|
1198 | - if($queryStr){ |
|
1199 | - //Sqlite and pgsql always return 0 when using rowCount() |
|
1200 | - if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
1201 | - $this->result = 1; //to test the result for the query like UPDATE, INSERT, DELETE |
|
1202 | - $this->numRows = 1; |
|
1203 | - } |
|
1204 | - else{ |
|
1205 | - $this->result = $queryStr->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
|
1206 | - $this->numRows = $queryStr->rowCount(); |
|
1207 | - } |
|
1208 | - } |
|
1209 | - if (! $this->result){ |
|
1210 | - $error = $this->pdo->errorInfo(); |
|
1211 | - $this->error = isset($error[2]) ? $error[2] : ''; |
|
1212 | - $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
|
1213 | - $this->error(); |
|
1214 | - } |
|
1215 | - } |
|
1216 | - else{ |
|
1217 | - $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
1218 | - $this->result = $cacheContent; |
|
1219 | - $this->numRows = count($this->result); |
|
1220 | - } |
|
1221 | - $this->queryCount++; |
|
1222 | - if(! $this->result){ |
|
1223 | - $this->logger->info('No result where found for the query [' . $query . ']'); |
|
1224 | - } |
|
1225 | - return $this->result; |
|
1226 | - } |
|
1227 | - |
|
1228 | - /** |
|
1229 | - * Set database cache time to live |
|
1230 | - * @param integer $ttl the cache time to live in second |
|
1231 | - * @return object the current Database instance |
|
1232 | - */ |
|
1233 | - public function setCache($ttl = 0){ |
|
1234 | - if($ttl > 0){ |
|
1235 | - $this->cacheTtl = $ttl; |
|
1236 | - $this->temporaryCacheTtl = $ttl; |
|
1237 | - } |
|
1238 | - return $this; |
|
1239 | - } |
|
1162 | + //get response time for this query |
|
1163 | + $responseTime = $bench->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')'); |
|
1164 | + //TODO use the configuration value for the high response time currently is 1 second |
|
1165 | + if($responseTime >= 1 ){ |
|
1166 | + $this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.'); |
|
1167 | + } |
|
1168 | + if ($sqlQuery){ |
|
1169 | + //if need return all result like list of record |
|
1170 | + if ($all){ |
|
1171 | + $this->result = ($array == false) ? $sqlQuery->fetchAll(PDO::FETCH_OBJ) : $sqlQuery->fetchAll(PDO::FETCH_ASSOC); |
|
1172 | + } |
|
1173 | + else{ |
|
1174 | + $this->result = ($array == false) ? $sqlQuery->fetch(PDO::FETCH_OBJ) : $sqlQuery->fetch(PDO::FETCH_ASSOC); |
|
1175 | + } |
|
1176 | + //Sqlite and pgsql always return 0 when using rowCount() |
|
1177 | + if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
1178 | + $this->numRows = count($this->result); |
|
1179 | + } |
|
1180 | + else{ |
|
1181 | + $this->numRows = $sqlQuery->rowCount(); |
|
1182 | + } |
|
1183 | + |
|
1184 | + if ($dbCacheStatus && $sqlSELECTQuery){ |
|
1185 | + $this->logger->info('Save the result for query [' .$this->query. '] into cache for future use'); |
|
1186 | + $cacheInstance->set($cacheKey, $this->result, $cacheExpire); |
|
1187 | + } |
|
1188 | + } |
|
1189 | + else{ |
|
1190 | + $error = $this->pdo->errorInfo(); |
|
1191 | + $this->error = isset($error[2]) ? $error[2] : ''; |
|
1192 | + $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
|
1193 | + $this->error(); |
|
1194 | + } |
|
1195 | + } |
|
1196 | + else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){ |
|
1197 | + $queryStr = $this->pdo->query($this->query); |
|
1198 | + if($queryStr){ |
|
1199 | + //Sqlite and pgsql always return 0 when using rowCount() |
|
1200 | + if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
1201 | + $this->result = 1; //to test the result for the query like UPDATE, INSERT, DELETE |
|
1202 | + $this->numRows = 1; |
|
1203 | + } |
|
1204 | + else{ |
|
1205 | + $this->result = $queryStr->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
|
1206 | + $this->numRows = $queryStr->rowCount(); |
|
1207 | + } |
|
1208 | + } |
|
1209 | + if (! $this->result){ |
|
1210 | + $error = $this->pdo->errorInfo(); |
|
1211 | + $this->error = isset($error[2]) ? $error[2] : ''; |
|
1212 | + $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
|
1213 | + $this->error(); |
|
1214 | + } |
|
1215 | + } |
|
1216 | + else{ |
|
1217 | + $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
1218 | + $this->result = $cacheContent; |
|
1219 | + $this->numRows = count($this->result); |
|
1220 | + } |
|
1221 | + $this->queryCount++; |
|
1222 | + if(! $this->result){ |
|
1223 | + $this->logger->info('No result where found for the query [' . $query . ']'); |
|
1224 | + } |
|
1225 | + return $this->result; |
|
1226 | + } |
|
1227 | + |
|
1228 | + /** |
|
1229 | + * Set database cache time to live |
|
1230 | + * @param integer $ttl the cache time to live in second |
|
1231 | + * @return object the current Database instance |
|
1232 | + */ |
|
1233 | + public function setCache($ttl = 0){ |
|
1234 | + if($ttl > 0){ |
|
1235 | + $this->cacheTtl = $ttl; |
|
1236 | + $this->temporaryCacheTtl = $ttl; |
|
1237 | + } |
|
1238 | + return $this; |
|
1239 | + } |
|
1240 | 1240 | |
1241 | 1241 | /** |
1242 | 1242 | * Enabled cache temporary for the current query not globally |
@@ -1244,186 +1244,186 @@ discard block |
||
1244 | 1244 | * @return object the current Database instance |
1245 | 1245 | */ |
1246 | 1246 | public function cached($ttl = 0){ |
1247 | - if($ttl > 0){ |
|
1248 | - $this->temporaryCacheTtl = $ttl; |
|
1249 | - } |
|
1247 | + if($ttl > 0){ |
|
1248 | + $this->temporaryCacheTtl = $ttl; |
|
1249 | + } |
|
1250 | + return $this; |
|
1251 | + } |
|
1252 | + |
|
1253 | + /** |
|
1254 | + * Escape the data before execute query useful for security. |
|
1255 | + * @param mixed $data the data to be escaped |
|
1256 | + * @return mixed the data after escaped |
|
1257 | + */ |
|
1258 | + public function escape($data){ |
|
1259 | + if(is_null($data)){ |
|
1260 | + return null; |
|
1261 | + } |
|
1262 | + if(! $this->pdo){ |
|
1263 | + $this->connect(); |
|
1264 | + } |
|
1265 | + return $this->pdo->quote(trim($data)); |
|
1266 | + } |
|
1267 | + |
|
1268 | + /** |
|
1269 | + * Return the number query executed count for the current request |
|
1270 | + * @return int |
|
1271 | + */ |
|
1272 | + public function queryCount(){ |
|
1273 | + return $this->queryCount; |
|
1274 | + } |
|
1275 | + |
|
1276 | + /** |
|
1277 | + * Return the current query SQL string |
|
1278 | + * @return string |
|
1279 | + */ |
|
1280 | + public function getQuery(){ |
|
1281 | + return $this->query; |
|
1282 | + } |
|
1283 | + |
|
1284 | + /** |
|
1285 | + * Return the application database name |
|
1286 | + * @return string |
|
1287 | + */ |
|
1288 | + public function getDatabaseName(){ |
|
1289 | + return $this->databaseName; |
|
1290 | + } |
|
1291 | + |
|
1292 | + /** |
|
1293 | + * Return the database configuration |
|
1294 | + * @return array |
|
1295 | + */ |
|
1296 | + public function getDatabaseConfiguration(){ |
|
1297 | + return $this->config; |
|
1298 | + } |
|
1299 | + |
|
1300 | + /** |
|
1301 | + * set the database configuration |
|
1302 | + * @param array $config the configuration |
|
1303 | + */ |
|
1304 | + public function setDatabaseConfiguration(array $config){ |
|
1305 | + $this->config = array_merge($this->config, $config); |
|
1306 | + $this->prefix = $this->config['prefix']; |
|
1307 | + $this->databaseName = $this->config['database']; |
|
1308 | + $this->logger->info('The database configuration are listed below: ' . stringfy_vars(array_merge($this->config, array('password' => string_hidden($this->config['password']))))); |
|
1309 | + return $this; |
|
1310 | + } |
|
1311 | + |
|
1312 | + /** |
|
1313 | + * Return the PDO instance |
|
1314 | + * @return PDO |
|
1315 | + */ |
|
1316 | + public function getPdo(){ |
|
1317 | + return $this->pdo; |
|
1318 | + } |
|
1319 | + |
|
1320 | + /** |
|
1321 | + * Set the PDO instance |
|
1322 | + * @param PDO $pdo the pdo object |
|
1323 | + */ |
|
1324 | + public function setPdo(PDO $pdo){ |
|
1325 | + $this->pdo = $pdo; |
|
1326 | + return $this; |
|
1327 | + } |
|
1328 | + |
|
1329 | + |
|
1330 | + /** |
|
1331 | + * Return the Log instance |
|
1332 | + * @return Log |
|
1333 | + */ |
|
1334 | + public function getLogger(){ |
|
1335 | + return $this->logger; |
|
1336 | + } |
|
1337 | + |
|
1338 | + /** |
|
1339 | + * Set the log instance |
|
1340 | + * @param Log $logger the log object |
|
1341 | + */ |
|
1342 | + public function setLogger($logger){ |
|
1343 | + $this->logger = $logger; |
|
1344 | + return $this; |
|
1345 | + } |
|
1346 | + |
|
1347 | + /** |
|
1348 | + * Return the cache instance |
|
1349 | + * @return CacheInterface |
|
1350 | + */ |
|
1351 | + public function getCacheInstance(){ |
|
1352 | + return $this->cacheInstance; |
|
1353 | + } |
|
1354 | + |
|
1355 | + /** |
|
1356 | + * Set the cache instance |
|
1357 | + * @param CacheInterface $cache the cache object |
|
1358 | + */ |
|
1359 | + public function setCacheInstance($cache){ |
|
1360 | + $this->cacheInstance = $cache; |
|
1361 | + return $this; |
|
1362 | + } |
|
1363 | + |
|
1364 | + /** |
|
1365 | + * Return the benchmark instance |
|
1366 | + * @return Benchmark |
|
1367 | + */ |
|
1368 | + public function getBenchmark(){ |
|
1369 | + return $this->benchmarkInstance; |
|
1370 | + } |
|
1371 | + |
|
1372 | + /** |
|
1373 | + * Set the benchmark instance |
|
1374 | + * @param Benchmark $cache the cache object |
|
1375 | + */ |
|
1376 | + public function setBenchmark($benchmark){ |
|
1377 | + $this->benchmarkInstance = $benchmark; |
|
1378 | + return $this; |
|
1379 | + } |
|
1380 | + |
|
1381 | + /** |
|
1382 | + * Return the data to be used for insert, update, etc. |
|
1383 | + * @return array |
|
1384 | + */ |
|
1385 | + public function getData(){ |
|
1386 | + return $this->data; |
|
1387 | + } |
|
1388 | + |
|
1389 | + /** |
|
1390 | + * Set the data to be used for insert, update, etc. |
|
1391 | + * @param string $key the data key identified |
|
1392 | + * @param mixed $value the data value |
|
1393 | + * @param boolean $escape whether to escape or not the $value |
|
1394 | + * @return object the current Database instance |
|
1395 | + */ |
|
1396 | + public function setData($key, $value, $escape = true){ |
|
1397 | + $this->data[$key] = $escape ? $this->escape($value) : $value; |
|
1250 | 1398 | return $this; |
1251 | - } |
|
1252 | - |
|
1253 | - /** |
|
1254 | - * Escape the data before execute query useful for security. |
|
1255 | - * @param mixed $data the data to be escaped |
|
1256 | - * @return mixed the data after escaped |
|
1257 | - */ |
|
1258 | - public function escape($data){ |
|
1259 | - if(is_null($data)){ |
|
1260 | - return null; |
|
1261 | - } |
|
1262 | - if(! $this->pdo){ |
|
1263 | - $this->connect(); |
|
1264 | - } |
|
1265 | - return $this->pdo->quote(trim($data)); |
|
1266 | - } |
|
1267 | - |
|
1268 | - /** |
|
1269 | - * Return the number query executed count for the current request |
|
1270 | - * @return int |
|
1271 | - */ |
|
1272 | - public function queryCount(){ |
|
1273 | - return $this->queryCount; |
|
1274 | - } |
|
1275 | - |
|
1276 | - /** |
|
1277 | - * Return the current query SQL string |
|
1278 | - * @return string |
|
1279 | - */ |
|
1280 | - public function getQuery(){ |
|
1281 | - return $this->query; |
|
1282 | - } |
|
1283 | - |
|
1284 | - /** |
|
1285 | - * Return the application database name |
|
1286 | - * @return string |
|
1287 | - */ |
|
1288 | - public function getDatabaseName(){ |
|
1289 | - return $this->databaseName; |
|
1290 | - } |
|
1291 | - |
|
1292 | - /** |
|
1293 | - * Return the database configuration |
|
1294 | - * @return array |
|
1295 | - */ |
|
1296 | - public function getDatabaseConfiguration(){ |
|
1297 | - return $this->config; |
|
1298 | - } |
|
1299 | - |
|
1300 | - /** |
|
1301 | - * set the database configuration |
|
1302 | - * @param array $config the configuration |
|
1303 | - */ |
|
1304 | - public function setDatabaseConfiguration(array $config){ |
|
1305 | - $this->config = array_merge($this->config, $config); |
|
1306 | - $this->prefix = $this->config['prefix']; |
|
1307 | - $this->databaseName = $this->config['database']; |
|
1308 | - $this->logger->info('The database configuration are listed below: ' . stringfy_vars(array_merge($this->config, array('password' => string_hidden($this->config['password']))))); |
|
1309 | - return $this; |
|
1310 | - } |
|
1311 | - |
|
1312 | - /** |
|
1313 | - * Return the PDO instance |
|
1314 | - * @return PDO |
|
1315 | - */ |
|
1316 | - public function getPdo(){ |
|
1317 | - return $this->pdo; |
|
1318 | - } |
|
1319 | - |
|
1320 | - /** |
|
1321 | - * Set the PDO instance |
|
1322 | - * @param PDO $pdo the pdo object |
|
1323 | - */ |
|
1324 | - public function setPdo(PDO $pdo){ |
|
1325 | - $this->pdo = $pdo; |
|
1326 | - return $this; |
|
1327 | - } |
|
1328 | - |
|
1329 | - |
|
1330 | - /** |
|
1331 | - * Return the Log instance |
|
1332 | - * @return Log |
|
1333 | - */ |
|
1334 | - public function getLogger(){ |
|
1335 | - return $this->logger; |
|
1336 | - } |
|
1337 | - |
|
1338 | - /** |
|
1339 | - * Set the log instance |
|
1340 | - * @param Log $logger the log object |
|
1341 | - */ |
|
1342 | - public function setLogger($logger){ |
|
1343 | - $this->logger = $logger; |
|
1344 | - return $this; |
|
1345 | - } |
|
1346 | - |
|
1347 | - /** |
|
1348 | - * Return the cache instance |
|
1349 | - * @return CacheInterface |
|
1350 | - */ |
|
1351 | - public function getCacheInstance(){ |
|
1352 | - return $this->cacheInstance; |
|
1353 | - } |
|
1354 | - |
|
1355 | - /** |
|
1356 | - * Set the cache instance |
|
1357 | - * @param CacheInterface $cache the cache object |
|
1358 | - */ |
|
1359 | - public function setCacheInstance($cache){ |
|
1360 | - $this->cacheInstance = $cache; |
|
1361 | - return $this; |
|
1362 | - } |
|
1363 | - |
|
1364 | - /** |
|
1365 | - * Return the benchmark instance |
|
1366 | - * @return Benchmark |
|
1367 | - */ |
|
1368 | - public function getBenchmark(){ |
|
1369 | - return $this->benchmarkInstance; |
|
1370 | - } |
|
1371 | - |
|
1372 | - /** |
|
1373 | - * Set the benchmark instance |
|
1374 | - * @param Benchmark $cache the cache object |
|
1375 | - */ |
|
1376 | - public function setBenchmark($benchmark){ |
|
1377 | - $this->benchmarkInstance = $benchmark; |
|
1378 | - return $this; |
|
1379 | - } |
|
1380 | - |
|
1381 | - /** |
|
1382 | - * Return the data to be used for insert, update, etc. |
|
1383 | - * @return array |
|
1384 | - */ |
|
1385 | - public function getData(){ |
|
1386 | - return $this->data; |
|
1387 | - } |
|
1388 | - |
|
1389 | - /** |
|
1390 | - * Set the data to be used for insert, update, etc. |
|
1391 | - * @param string $key the data key identified |
|
1392 | - * @param mixed $value the data value |
|
1393 | - * @param boolean $escape whether to escape or not the $value |
|
1394 | - * @return object the current Database instance |
|
1395 | - */ |
|
1396 | - public function setData($key, $value, $escape = true){ |
|
1397 | - $this->data[$key] = $escape ? $this->escape($value) : $value; |
|
1398 | - return $this; |
|
1399 | - } |
|
1399 | + } |
|
1400 | 1400 | |
1401 | 1401 | |
1402 | 1402 | /** |
1403 | 1403 | * Reset the database class attributs to the initail values before each query. |
1404 | 1404 | */ |
1405 | 1405 | private function reset(){ |
1406 | - $this->select = '*'; |
|
1407 | - $this->from = null; |
|
1408 | - $this->where = null; |
|
1409 | - $this->limit = null; |
|
1410 | - $this->orderBy = null; |
|
1411 | - $this->groupBy = null; |
|
1412 | - $this->having = null; |
|
1413 | - $this->join = null; |
|
1414 | - $this->numRows = 0; |
|
1415 | - $this->insertId = null; |
|
1416 | - $this->query = null; |
|
1417 | - $this->error = null; |
|
1418 | - $this->result = array(); |
|
1419 | - $this->data = array(); |
|
1406 | + $this->select = '*'; |
|
1407 | + $this->from = null; |
|
1408 | + $this->where = null; |
|
1409 | + $this->limit = null; |
|
1410 | + $this->orderBy = null; |
|
1411 | + $this->groupBy = null; |
|
1412 | + $this->having = null; |
|
1413 | + $this->join = null; |
|
1414 | + $this->numRows = 0; |
|
1415 | + $this->insertId = null; |
|
1416 | + $this->query = null; |
|
1417 | + $this->error = null; |
|
1418 | + $this->result = array(); |
|
1419 | + $this->data = array(); |
|
1420 | 1420 | } |
1421 | 1421 | |
1422 | 1422 | /** |
1423 | 1423 | * The class destructor |
1424 | 1424 | */ |
1425 | 1425 | function __destruct(){ |
1426 | - $this->pdo = null; |
|
1426 | + $this->pdo = null; |
|
1427 | 1427 | } |
1428 | 1428 | |
1429 | 1429 | } |
@@ -1,797 +1,797 @@ |
||
1 | 1 | <?php |
2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
3 | - /** |
|
4 | - * TNH Framework |
|
5 | - * |
|
6 | - * A simple PHP framework using HMVC architecture |
|
7 | - * |
|
8 | - * This content is released under the GNU GPL License (GPL) |
|
9 | - * |
|
10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
11 | - * |
|
12 | - * This program is free software; you can redistribute it and/or |
|
13 | - * modify it under the terms of the GNU General Public License |
|
14 | - * as published by the Free Software Foundation; either version 3 |
|
15 | - * of the License, or (at your option) any later version. |
|
16 | - * |
|
17 | - * This program is distributed in the hope that it will be useful, |
|
18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | - * GNU General Public License for more details. |
|
21 | - * |
|
22 | - * You should have received a copy of the GNU General Public License |
|
23 | - * along with this program; if not, write to the Free Software |
|
24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | - */ |
|
26 | - |
|
27 | - |
|
28 | - |
|
29 | - /** |
|
30 | - * Upload |
|
31 | - * |
|
32 | - * A complete class to upload files with php 5 or higher, but the best: very simple to use. |
|
33 | - * |
|
34 | - * @author Olaf Erlandsen <[email protected]> |
|
35 | - * @author http://www.webdevfreelance.com/ |
|
36 | - * |
|
37 | - * @package FileUpload |
|
38 | - * @version 1.5 |
|
39 | - */ |
|
40 | - class Upload{ |
|
41 | - |
|
42 | - /** |
|
43 | - * Version |
|
44 | - * |
|
45 | - * @since 1.5 |
|
46 | - * @version 1.0 |
|
47 | - */ |
|
48 | - const VERSION = '1.5'; |
|
49 | - |
|
50 | - /** |
|
51 | - * Upload function name |
|
52 | - * Remember: |
|
53 | - * Default function: move_uploaded_file |
|
54 | - * Native options: |
|
55 | - * - move_uploaded_file (Default and best option) |
|
56 | - * - copy |
|
57 | - * |
|
58 | - * @since 1.0 |
|
59 | - * @version 1.0 |
|
60 | - * @var mixex |
|
61 | - */ |
|
62 | - private $upload_function = 'move_uploaded_file'; |
|
63 | - |
|
64 | - /** |
|
65 | - * Array with the information obtained from the |
|
66 | - * variable $_FILES or $HTTP_POST_FILES. |
|
67 | - * |
|
68 | - * @since 1.0 |
|
69 | - * @version 1.0 |
|
70 | - * @var array |
|
71 | - */ |
|
72 | - private $file_array = array(); |
|
73 | - |
|
74 | - /** |
|
75 | - * If the file you are trying to upload already exists it will |
|
76 | - * be overwritten if you set the variable to true. |
|
77 | - * |
|
78 | - * @since 1.0 |
|
79 | - * @version 1.0 |
|
80 | - * @var boolean |
|
81 | - */ |
|
82 | - private $overwrite_file = false; |
|
83 | - |
|
84 | - /** |
|
85 | - * Input element |
|
86 | - * Example: |
|
87 | - * <input type="file" name="file" /> |
|
88 | - * Result: |
|
89 | - * FileUpload::$input = file |
|
90 | - * |
|
91 | - * @since 1.0 |
|
92 | - * @version 1.0 |
|
93 | - * @var string |
|
94 | - */ |
|
95 | - private $input; |
|
96 | - |
|
97 | - /** |
|
98 | - * Path output |
|
99 | - * |
|
100 | - * @since 1.0 |
|
101 | - * @version 1.0 |
|
102 | - * @var string |
|
103 | - */ |
|
104 | - private $destination_directory; |
|
105 | - |
|
106 | - /** |
|
107 | - * Output filename |
|
108 | - * |
|
109 | - * @since 1.0 |
|
110 | - * @version 1.0 |
|
111 | - * @var string |
|
112 | - */ |
|
113 | - private $filename; |
|
114 | - |
|
115 | - /** |
|
116 | - * Max file size |
|
117 | - * |
|
118 | - * @since 1.0 |
|
119 | - * @version 1.0 |
|
120 | - * @var float |
|
121 | - */ |
|
122 | - private $max_file_size= 0.0; |
|
123 | - |
|
124 | - /** |
|
125 | - * List of allowed mime types |
|
126 | - * |
|
127 | - * @since 1.0 |
|
128 | - * @version 1.0 |
|
129 | - * @var array |
|
130 | - */ |
|
131 | - private $allowed_mime_types = array(); |
|
132 | - |
|
133 | - /** |
|
134 | - * Callbacks |
|
135 | - * |
|
136 | - * @since 1.0 |
|
137 | - * @version 1.0 |
|
138 | - * @var array |
|
139 | - */ |
|
140 | - private $callbacks = array('before' => null, 'after' => null); |
|
141 | - |
|
142 | - /** |
|
143 | - * File object |
|
144 | - * |
|
145 | - * @since 1.0 |
|
146 | - * @version 1.0 |
|
147 | - * @var object |
|
148 | - */ |
|
149 | - private $file; |
|
150 | - |
|
151 | - /** |
|
152 | - * Helping mime types |
|
153 | - * |
|
154 | - * @since 1.0 |
|
155 | - * @version 1.0 |
|
156 | - * @var array |
|
157 | - */ |
|
158 | - private $mime_helping = array( |
|
159 | - 'text' => array('text/plain',), |
|
160 | - 'image' => array( |
|
161 | - 'image/jpeg', |
|
162 | - 'image/jpg', |
|
163 | - 'image/pjpeg', |
|
164 | - 'image/png', |
|
165 | - 'image/gif', |
|
166 | - ), |
|
167 | - 'document' => array( |
|
168 | - 'application/pdf', |
|
169 | - 'application/msword', |
|
170 | - 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
|
171 | - 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
|
172 | - 'application/vnd.ms-powerpoint', |
|
173 | - 'application/vnd.ms-excel', |
|
174 | - 'application/vnd.oasis.opendocument.spreadsheet', |
|
175 | - 'application/vnd.oasis.opendocument.presentation', |
|
176 | - ), |
|
177 | - 'video' => array( |
|
178 | - 'video/3gpp', |
|
179 | - 'video/3gpp', |
|
180 | - 'video/x-msvideo', |
|
181 | - 'video/avi', |
|
182 | - 'video/mpeg4', |
|
183 | - 'video/mp4', |
|
184 | - 'video/mpeg', |
|
185 | - 'video/mpg', |
|
186 | - 'video/quicktime', |
|
187 | - 'video/x-sgi-movie', |
|
188 | - 'video/x-ms-wmv', |
|
189 | - 'video/x-flv', |
|
190 | - ), |
|
191 | - ); |
|
192 | - |
|
193 | - /** |
|
194 | - * The upload error message |
|
195 | - * @var array |
|
196 | - */ |
|
197 | - public $error_messages = array(); |
|
198 | - |
|
199 | - /** |
|
200 | - * The upload error message |
|
201 | - * @var string |
|
202 | - */ |
|
203 | - protected $error = null; |
|
204 | - |
|
205 | - /** |
|
206 | - * The logger instance |
|
207 | - * @var Log |
|
208 | - */ |
|
209 | - private $logger; |
|
210 | - |
|
211 | - |
|
212 | - /** |
|
213 | - * Construct |
|
214 | - * |
|
215 | - * @since 0.1 |
|
216 | - * @version 1.0.1 |
|
217 | - * @return object |
|
218 | - * @method object __construct |
|
219 | - */ |
|
220 | - public function __construct(){ |
|
221 | - $this->logger =& class_loader('Log', 'classes'); |
|
222 | - $this->logger->setLogger('Library::Upload'); |
|
223 | - |
|
224 | - Loader::lang('file_upload'); |
|
225 | - $obj =& get_instance(); |
|
226 | - |
|
227 | - $this->error_messages = array( |
|
228 | - 'upload_err_ini_size' => $obj->lang->get('fu_upload_err_ini_size'), |
|
229 | - 'upload_err_form_size' => $obj->lang->get('fu_upload_err_form_size'), |
|
230 | - 'upload_err_partial' => $obj->lang->get('fu_upload_err_partial'), |
|
231 | - 'upload_err_no_file' => $obj->lang->get('fu_upload_err_no_file'), |
|
232 | - 'upload_err_no_tmp_dir' => $obj->lang->get('fu_upload_err_no_tmp_dir'), |
|
233 | - 'upload_err_cant_write' => $obj->lang->get('fu_upload_err_cant_write'), |
|
234 | - 'upload_err_extension' => $obj->lang->get('fu_upload_err_extension'), |
|
235 | - 'accept_file_types' => $obj->lang->get('fu_accept_file_types'), |
|
236 | - 'file_uploads' => $obj->lang->get('fu_file_uploads_disabled'), |
|
237 | - 'max_file_size' => $obj->lang->get('fu_max_file_size'), |
|
238 | - 'overwritten_not_allowed' => $obj->lang->get('fu_overwritten_not_allowed'), |
|
239 | - ); |
|
240 | - |
|
241 | - $this->file = array( |
|
242 | - 'status' => false, // True: success upload |
|
243 | - 'mime' => '', // Empty string |
|
244 | - 'filename' => '', // Empty string |
|
245 | - 'original' => '', // Empty string |
|
246 | - 'size' => 0, // 0 Bytes |
|
247 | - 'sizeFormated' => '0B', // 0 Bytes |
|
248 | - 'destination' => './', // Default: ./ |
|
249 | - 'allowed_mime_types' => array(), // Allowed mime types |
|
250 | - 'error' => null, // File error |
|
251 | - ); |
|
252 | - |
|
253 | - // Change dir to current dir |
|
254 | - $this->destination_directory = dirname(__FILE__) . DIRECTORY_SEPARATOR; |
|
255 | - |
|
256 | - // Set file array |
|
257 | - if (isset($_FILES) && is_array($_FILES)) { |
|
258 | - $this->file_array = $_FILES; |
|
259 | - } elseif (isset($HTTP_POST_FILES) && is_array($HTTP_POST_FILES)) { |
|
260 | - $this->file_array = $HTTP_POST_FILES; |
|
261 | - } |
|
262 | - $this->logger->info('The upload file information are : ' .stringfy_vars($this->file_array)); |
|
263 | - } |
|
264 | - /** |
|
265 | - * Set input. |
|
266 | - * If you have $_FILES["file"], you must use the key "file" |
|
267 | - * Example: |
|
268 | - * $object->setInput("file"); |
|
269 | - * |
|
270 | - * @since 1.0 |
|
271 | - * @version 1.0 |
|
272 | - * @param string $input |
|
273 | - * @return boolean |
|
274 | - * @method boolean setInput |
|
275 | - */ |
|
276 | - public function setInput($input) |
|
277 | - { |
|
278 | - if (!empty($input) && (is_string($input) || is_numeric($input) )) { |
|
279 | - $this->input = $input; |
|
280 | - } |
|
281 | - return $this; |
|
282 | - } |
|
283 | - /** |
|
284 | - * Set new filename |
|
285 | - * Example: |
|
286 | - * FileUpload::setFilename("new file.txt") |
|
287 | - * Remember: |
|
288 | - * Use %s to retrive file extension |
|
289 | - * |
|
290 | - * @since 1.0 |
|
291 | - * @version 1.0 |
|
292 | - * @param string $filename |
|
293 | - * @return boolean |
|
294 | - * @method boolean setFilename |
|
295 | - */ |
|
296 | - public function setFilename($filename) |
|
297 | - { |
|
298 | - if ($this->isFilename($filename)) { |
|
299 | - $this->filename = $filename; |
|
300 | - } |
|
301 | - return $this; |
|
302 | - } |
|
303 | - /** |
|
304 | - * Set automatic filename |
|
305 | - * |
|
306 | - * @since 1.0 |
|
307 | - * @version 1.5 |
|
308 | - * @param string $extension |
|
309 | - * @return boolean |
|
310 | - * @method boolean setAutoFilename |
|
311 | - */ |
|
312 | - public function setAutoFilename() |
|
313 | - { |
|
314 | - $this->filename = sha1(mt_rand(1, 9999).uniqid()); |
|
315 | - $this->filename .= time(); |
|
316 | - return $this; |
|
317 | - } |
|
318 | - /** |
|
319 | - * Set file size limit |
|
320 | - * |
|
321 | - * @since 1.0 |
|
322 | - * @version 1.0 |
|
323 | - * @param integer $file_size |
|
324 | - * @return boolean |
|
325 | - * @method boolean setMaxFileSize |
|
326 | - */ |
|
327 | - public function setMaxFileSize($file_size) |
|
328 | - { |
|
329 | - $file_size = $this->sizeInBytes($file_size); |
|
330 | - if (is_numeric($file_size) && $file_size > -1) { |
|
331 | - // Get php config |
|
332 | - $php_size = $this->sizeInBytes(ini_get('upload_max_filesize')); |
|
333 | - // Calculate difference |
|
334 | - if ($php_size < $file_size) { |
|
335 | - $this->logger->warning('The upload max file size you set [' .$file_size. '] is greather than the PHP configuration for upload max file size [' .$php_size. ']'); |
|
336 | - } |
|
337 | - $this->max_file_size = $file_size; |
|
338 | - } |
|
339 | - return $this; |
|
340 | - } |
|
341 | - /** |
|
342 | - * Set array mime types |
|
343 | - * |
|
344 | - * @since 1.0 |
|
345 | - * @version 1.0 |
|
346 | - * @param array $mimes |
|
347 | - * @return boolean |
|
348 | - * @method boolean setAllowedMimeTypes |
|
349 | - */ |
|
350 | - public function setAllowedMimeTypes(array $mimes) |
|
351 | - { |
|
352 | - if (count($mimes) > 0) { |
|
353 | - array_map(array($this , 'setAllowMimeType'), $mimes); |
|
354 | - } |
|
355 | - return $this; |
|
356 | - } |
|
357 | - /** |
|
358 | - * Set input callback |
|
359 | - * |
|
360 | - * @since 1.0 |
|
361 | - * @version 1.0 |
|
362 | - * @param mixed $callback |
|
363 | - * @return boolean |
|
364 | - * @method boolean setCallbackInput |
|
365 | - */ |
|
366 | - public function setCallbackInput($callback) |
|
367 | - { |
|
368 | - if (is_callable($callback, false)) { |
|
369 | - $this->callbacks['input'] = $callback; |
|
370 | - } |
|
371 | - return $this; |
|
372 | - } |
|
373 | - /** |
|
374 | - * Set output callback |
|
375 | - * |
|
376 | - * @since 1.0 |
|
377 | - * @version 1.0 |
|
378 | - * @param mixed $callback |
|
379 | - * @return boolean |
|
380 | - * @method boolean setCallbackOutput |
|
381 | - */ |
|
382 | - public function setCallbackOutput($callback) |
|
383 | - { |
|
384 | - if (is_callable($callback, false)) { |
|
385 | - $this->callbacks['output'] = $callback; |
|
386 | - } |
|
387 | - return $this; |
|
388 | - } |
|
389 | - /** |
|
390 | - * Append a mime type to allowed mime types |
|
391 | - * |
|
392 | - * @since 1.0 |
|
393 | - * @version 1.0.1 |
|
394 | - * @param string $mime |
|
395 | - * @return boolean |
|
396 | - * @method boolean setAllowMimeType |
|
397 | - */ |
|
398 | - public function setAllowMimeType($mime) |
|
399 | - { |
|
400 | - if (!empty($mime) && is_string($mime)) { |
|
401 | - $this->allowed_mime_types[] = strtolower($mime); |
|
402 | - $this->file['allowed_mime_types'][] = strtolower($mime); |
|
403 | - } |
|
404 | - return $this; |
|
405 | - } |
|
406 | - /** |
|
407 | - * Set allowed mime types from mime helping |
|
408 | - * |
|
409 | - * @since 1.0.1 |
|
410 | - * @version 1.0.1 |
|
411 | - * @return boolean |
|
412 | - * @method boolean setMimeHelping |
|
413 | - */ |
|
414 | - public function setMimeHelping($name) |
|
415 | - { |
|
416 | - if (!empty($name) && is_string($name)) { |
|
417 | - if (array_key_exists($name, $this->mime_helping)) { |
|
418 | - return $this->setAllowedMimeTypes($this->mime_helping[ $name ]); |
|
419 | - } |
|
420 | - } |
|
421 | - return $this; |
|
422 | - } |
|
423 | - /** |
|
424 | - * Set function to upload file |
|
425 | - * Examples: |
|
426 | - * 1.- FileUpload::setUploadFunction("move_uploaded_file"); |
|
427 | - * 2.- FileUpload::setUploadFunction("copy"); |
|
428 | - * |
|
429 | - * @since 1.0 |
|
430 | - * @version 1.0 |
|
431 | - * @param string $mime |
|
432 | - * @return boolean |
|
433 | - * @method boolean setUploadFunction |
|
434 | - */ |
|
435 | - public function setUploadFunction($function) |
|
436 | - { |
|
437 | - if (!empty($function) && (is_array($function) || is_string($function) )) { |
|
438 | - if (is_callable( $function)) { |
|
439 | - $this->upload_function = $function; |
|
440 | - } |
|
441 | - } |
|
442 | - return $this; |
|
443 | - } |
|
444 | - /** |
|
445 | - * Clear allowed mime types cache |
|
446 | - * |
|
447 | - * @since 1.0 |
|
448 | - * @version 1.0 |
|
449 | - * @return boolean |
|
450 | - * @method boolean clearAllowedMimeTypes |
|
451 | - */ |
|
452 | - public function clearAllowedMimeTypes() |
|
453 | - { |
|
454 | - $this->allowed_mime_types = array(); |
|
455 | - $this->file['allowed_mime_types'] = array(); |
|
456 | - return $this; |
|
457 | - } |
|
458 | - /** |
|
459 | - * Set destination output |
|
460 | - * |
|
461 | - * @since 1.0 |
|
462 | - * @version 1.0 |
|
463 | - * @param string $destination_directory Destination path |
|
464 | - * @param boolean $create_if_not_exist |
|
465 | - * @return boolean |
|
466 | - * @method boolean setDestinationDirectory |
|
467 | - */ |
|
468 | - public function setDestinationDirectory($destination_directory, $create_if_not_exist = false) { |
|
469 | - $destination_directory = realpath($destination_directory); |
|
470 | - if (substr($destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
471 | - $destination_directory .= DIRECTORY_SEPARATOR; |
|
472 | - } |
|
473 | - |
|
474 | - if ($this->isDirpath($destination_directory)) { |
|
475 | - if ($this->dirExists($destination_directory)) { |
|
476 | - $this->destination_directory = $destination_directory; |
|
477 | - if (substr($this->destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
478 | - $this->destination_directory .= DIRECTORY_SEPARATOR; |
|
479 | - } |
|
480 | - chdir($destination_directory); |
|
481 | - } elseif ($create_if_not_exist === true) { |
|
482 | - if (mkdir($destination_directory, 0775, true)) { |
|
483 | - if ($this->dirExists($destination_directory)) { |
|
484 | - $this->destination_directory = $destination_directory; |
|
485 | - if (substr($this->destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
486 | - $this->destination_directory .= DIRECTORY_SEPARATOR; |
|
487 | - } |
|
488 | - chdir($destination_directory); |
|
489 | - } |
|
490 | - } |
|
491 | - else{ |
|
492 | - $this->logger->warning('Can not create the upload directory [' .$destination_directory. ']'); |
|
493 | - } |
|
494 | - } |
|
495 | - } |
|
496 | - return $this; |
|
497 | - } |
|
498 | - /** |
|
499 | - * Check file exists |
|
500 | - * |
|
501 | - * @since 1.0 |
|
502 | - * @version 1.0.1 |
|
503 | - * @param string $file_destination |
|
504 | - * @return boolean |
|
505 | - * @method boolean fileExists |
|
506 | - */ |
|
507 | - public function fileExists($file_destination) |
|
508 | - { |
|
509 | - if ($this->isFilename($file_destination)) { |
|
510 | - return (file_exists($file_destination) && is_file($file_destination)); |
|
511 | - } |
|
512 | - return false; |
|
513 | - } |
|
514 | - /** |
|
515 | - * Check dir exists |
|
516 | - * |
|
517 | - * @since 1.0 |
|
518 | - * @version 1.0.1 |
|
519 | - * @param string $path |
|
520 | - * @return boolean |
|
521 | - * @method boolean dirExists |
|
522 | - */ |
|
523 | - public function dirExists($path) |
|
524 | - { |
|
525 | - if ($this->isDirpath($path)) { |
|
526 | - return (file_exists($path) && is_dir($path)); |
|
527 | - } |
|
528 | - return false; |
|
529 | - } |
|
530 | - /** |
|
531 | - * Check valid filename |
|
532 | - * |
|
533 | - * @since 1.0 |
|
534 | - * @version 1.0.1 |
|
535 | - * @param string $filename |
|
536 | - * @return boolean |
|
537 | - * @method boolean isFilename |
|
538 | - */ |
|
539 | - public function isFilename($filename) |
|
540 | - { |
|
541 | - $filename = basename($filename); |
|
542 | - return (!empty($filename) && (is_string( $filename) || is_numeric($filename))); |
|
543 | - } |
|
544 | - /** |
|
545 | - * Validate mime type with allowed mime types, |
|
546 | - * but if allowed mime types is empty, this method return true |
|
547 | - * |
|
548 | - * @since 1.0 |
|
549 | - * @version 1.0 |
|
550 | - * @param string $mime |
|
551 | - * @return boolean |
|
552 | - * @method boolean checkMimeType |
|
553 | - */ |
|
554 | - public function checkMimeType($mime) |
|
555 | - { |
|
556 | - if (count($this->allowed_mime_types) == 0) { |
|
557 | - return true; |
|
558 | - } |
|
559 | - return in_array(strtolower($mime), $this->allowed_mime_types); |
|
560 | - } |
|
561 | - /** |
|
562 | - * Retrive status of upload |
|
563 | - * |
|
564 | - * @since 1.0 |
|
565 | - * @version 1.0 |
|
566 | - * @return boolean |
|
567 | - * @method boolean getStatus |
|
568 | - */ |
|
569 | - public function getStatus() |
|
570 | - { |
|
571 | - return $this->file['status']; |
|
572 | - } |
|
573 | - /** |
|
574 | - * Check valid path |
|
575 | - * |
|
576 | - * @since 1.0 |
|
577 | - * @version 1.0.1 |
|
578 | - * @param string $filename |
|
579 | - * @return boolean |
|
580 | - * @method boolean isDirpath |
|
581 | - */ |
|
582 | - public function isDirpath($path) |
|
583 | - { |
|
584 | - if (!empty( $path) && (is_string( $path) || is_numeric($path) )) { |
|
585 | - if (DIRECTORY_SEPARATOR == '/') { |
|
586 | - return (preg_match( '/^[^*?"<>|:]*$/' , $path) == 1 ); |
|
587 | - } else { |
|
588 | - return (preg_match( "/^[^*?\"<>|:]*$/" , substr($path,2) ) == 1); |
|
589 | - } |
|
590 | - } |
|
591 | - return false; |
|
592 | - } |
|
593 | - /** |
|
594 | - * Allow overwriting files |
|
595 | - * |
|
596 | - * @since 1.0 |
|
597 | - * @version 1.0 |
|
598 | - * @return boolean |
|
599 | - * @method boolean allowOverwriting |
|
600 | - */ |
|
601 | - public function allowOverwriting() |
|
602 | - { |
|
603 | - $this->overwrite_file = true; |
|
604 | - return $this; |
|
605 | - } |
|
606 | - /** |
|
607 | - * File info |
|
608 | - * |
|
609 | - * @since 1.0 |
|
610 | - * @version 1.0 |
|
611 | - * @return object |
|
612 | - * @method object getInfo |
|
613 | - */ |
|
614 | - public function getInfo() |
|
615 | - { |
|
616 | - return (object)$this->file; |
|
617 | - } |
|
618 | - |
|
619 | - public function isUploaded(){ |
|
620 | - return isset($this->file_array[$this->input]) |
|
621 | - && |
|
622 | - is_uploaded_file($this->file_array[$this->input]['tmp_name']); |
|
623 | - } |
|
624 | - /** |
|
625 | - * Upload file |
|
626 | - * |
|
627 | - * @since 1.0 |
|
628 | - * @version 1.0.1 |
|
629 | - * @return boolean |
|
630 | - * @method boolean save |
|
631 | - */ |
|
632 | - public function save(){ |
|
633 | - //check if file upload is allowed in the configuration |
|
634 | - if(! ini_get('file_uploads')){ |
|
635 | - $this->setError($this->error_messages['file_uploads']); |
|
636 | - return false; |
|
637 | - } |
|
638 | - if (count($this->file_array) > 0) { |
|
639 | - if (array_key_exists($this->input, $this->file_array)) { |
|
640 | - // set original filename if not have a new name |
|
641 | - if (empty($this->filename)) { |
|
642 | - $this->filename = $this->file_array[$this->input]['name']; |
|
643 | - } |
|
644 | - else{ |
|
645 | - // Replace %s for extension in filename |
|
646 | - // Before: /[\w\d]*(.[\d\w]+)$/i |
|
647 | - // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |
|
648 | - // Support unicode(utf-8) characters |
|
649 | - // Example: "русские.jpeg" is valid; "Zhōngguó.jpeg" is valid; "Tønsberg.jpeg" is valid |
|
650 | - $extension = preg_replace( |
|
651 | - '/^[\p{L}\d\s\-\_\.\(\)]*\.([\d\w]+)$/iu', |
|
652 | - '$1', |
|
653 | - $this->file_array[$this->input]['name'] |
|
654 | - ); |
|
655 | - $this->filename = $this->filename.'.'.$extension; |
|
656 | - } |
|
657 | - |
|
658 | - // set file info |
|
659 | - $this->file['mime'] = $this->file_array[$this->input]['type']; |
|
660 | - $this->file['tmp'] = $this->file_array[$this->input]['tmp_name']; |
|
661 | - $this->file['original'] = $this->file_array[$this->input]['name']; |
|
662 | - $this->file['size'] = $this->file_array[$this->input]['size']; |
|
663 | - $this->file['sizeFormated'] = $this->sizeFormat($this->file['size']); |
|
664 | - $this->file['destination'] = $this->destination_directory . $this->filename; |
|
665 | - $this->file['filename'] = $this->filename; |
|
666 | - $this->file['error'] = $this->file_array[$this->input]['error']; |
|
667 | - |
|
668 | - $this->logger->info('The upload file information to process is : ' .stringfy_vars($this->file)); |
|
669 | - |
|
670 | - //check for php upload error |
|
671 | - if(is_numeric($this->file['error']) && $this->file['error'] > 0){ |
|
672 | - $this->setError($this->getPhpUploadErrorMessageByCode($this->file['error'])); |
|
673 | - return false; |
|
674 | - } |
|
2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
3 | + /** |
|
4 | + * TNH Framework |
|
5 | + * |
|
6 | + * A simple PHP framework using HMVC architecture |
|
7 | + * |
|
8 | + * This content is released under the GNU GPL License (GPL) |
|
9 | + * |
|
10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
11 | + * |
|
12 | + * This program is free software; you can redistribute it and/or |
|
13 | + * modify it under the terms of the GNU General Public License |
|
14 | + * as published by the Free Software Foundation; either version 3 |
|
15 | + * of the License, or (at your option) any later version. |
|
16 | + * |
|
17 | + * This program is distributed in the hope that it will be useful, |
|
18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | + * GNU General Public License for more details. |
|
21 | + * |
|
22 | + * You should have received a copy of the GNU General Public License |
|
23 | + * along with this program; if not, write to the Free Software |
|
24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | + */ |
|
26 | + |
|
27 | + |
|
28 | + |
|
29 | + /** |
|
30 | + * Upload |
|
31 | + * |
|
32 | + * A complete class to upload files with php 5 or higher, but the best: very simple to use. |
|
33 | + * |
|
34 | + * @author Olaf Erlandsen <[email protected]> |
|
35 | + * @author http://www.webdevfreelance.com/ |
|
36 | + * |
|
37 | + * @package FileUpload |
|
38 | + * @version 1.5 |
|
39 | + */ |
|
40 | + class Upload{ |
|
41 | + |
|
42 | + /** |
|
43 | + * Version |
|
44 | + * |
|
45 | + * @since 1.5 |
|
46 | + * @version 1.0 |
|
47 | + */ |
|
48 | + const VERSION = '1.5'; |
|
49 | + |
|
50 | + /** |
|
51 | + * Upload function name |
|
52 | + * Remember: |
|
53 | + * Default function: move_uploaded_file |
|
54 | + * Native options: |
|
55 | + * - move_uploaded_file (Default and best option) |
|
56 | + * - copy |
|
57 | + * |
|
58 | + * @since 1.0 |
|
59 | + * @version 1.0 |
|
60 | + * @var mixex |
|
61 | + */ |
|
62 | + private $upload_function = 'move_uploaded_file'; |
|
63 | + |
|
64 | + /** |
|
65 | + * Array with the information obtained from the |
|
66 | + * variable $_FILES or $HTTP_POST_FILES. |
|
67 | + * |
|
68 | + * @since 1.0 |
|
69 | + * @version 1.0 |
|
70 | + * @var array |
|
71 | + */ |
|
72 | + private $file_array = array(); |
|
73 | + |
|
74 | + /** |
|
75 | + * If the file you are trying to upload already exists it will |
|
76 | + * be overwritten if you set the variable to true. |
|
77 | + * |
|
78 | + * @since 1.0 |
|
79 | + * @version 1.0 |
|
80 | + * @var boolean |
|
81 | + */ |
|
82 | + private $overwrite_file = false; |
|
83 | + |
|
84 | + /** |
|
85 | + * Input element |
|
86 | + * Example: |
|
87 | + * <input type="file" name="file" /> |
|
88 | + * Result: |
|
89 | + * FileUpload::$input = file |
|
90 | + * |
|
91 | + * @since 1.0 |
|
92 | + * @version 1.0 |
|
93 | + * @var string |
|
94 | + */ |
|
95 | + private $input; |
|
96 | + |
|
97 | + /** |
|
98 | + * Path output |
|
99 | + * |
|
100 | + * @since 1.0 |
|
101 | + * @version 1.0 |
|
102 | + * @var string |
|
103 | + */ |
|
104 | + private $destination_directory; |
|
105 | + |
|
106 | + /** |
|
107 | + * Output filename |
|
108 | + * |
|
109 | + * @since 1.0 |
|
110 | + * @version 1.0 |
|
111 | + * @var string |
|
112 | + */ |
|
113 | + private $filename; |
|
114 | + |
|
115 | + /** |
|
116 | + * Max file size |
|
117 | + * |
|
118 | + * @since 1.0 |
|
119 | + * @version 1.0 |
|
120 | + * @var float |
|
121 | + */ |
|
122 | + private $max_file_size= 0.0; |
|
123 | + |
|
124 | + /** |
|
125 | + * List of allowed mime types |
|
126 | + * |
|
127 | + * @since 1.0 |
|
128 | + * @version 1.0 |
|
129 | + * @var array |
|
130 | + */ |
|
131 | + private $allowed_mime_types = array(); |
|
132 | + |
|
133 | + /** |
|
134 | + * Callbacks |
|
135 | + * |
|
136 | + * @since 1.0 |
|
137 | + * @version 1.0 |
|
138 | + * @var array |
|
139 | + */ |
|
140 | + private $callbacks = array('before' => null, 'after' => null); |
|
141 | + |
|
142 | + /** |
|
143 | + * File object |
|
144 | + * |
|
145 | + * @since 1.0 |
|
146 | + * @version 1.0 |
|
147 | + * @var object |
|
148 | + */ |
|
149 | + private $file; |
|
150 | + |
|
151 | + /** |
|
152 | + * Helping mime types |
|
153 | + * |
|
154 | + * @since 1.0 |
|
155 | + * @version 1.0 |
|
156 | + * @var array |
|
157 | + */ |
|
158 | + private $mime_helping = array( |
|
159 | + 'text' => array('text/plain',), |
|
160 | + 'image' => array( |
|
161 | + 'image/jpeg', |
|
162 | + 'image/jpg', |
|
163 | + 'image/pjpeg', |
|
164 | + 'image/png', |
|
165 | + 'image/gif', |
|
166 | + ), |
|
167 | + 'document' => array( |
|
168 | + 'application/pdf', |
|
169 | + 'application/msword', |
|
170 | + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
|
171 | + 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
|
172 | + 'application/vnd.ms-powerpoint', |
|
173 | + 'application/vnd.ms-excel', |
|
174 | + 'application/vnd.oasis.opendocument.spreadsheet', |
|
175 | + 'application/vnd.oasis.opendocument.presentation', |
|
176 | + ), |
|
177 | + 'video' => array( |
|
178 | + 'video/3gpp', |
|
179 | + 'video/3gpp', |
|
180 | + 'video/x-msvideo', |
|
181 | + 'video/avi', |
|
182 | + 'video/mpeg4', |
|
183 | + 'video/mp4', |
|
184 | + 'video/mpeg', |
|
185 | + 'video/mpg', |
|
186 | + 'video/quicktime', |
|
187 | + 'video/x-sgi-movie', |
|
188 | + 'video/x-ms-wmv', |
|
189 | + 'video/x-flv', |
|
190 | + ), |
|
191 | + ); |
|
192 | + |
|
193 | + /** |
|
194 | + * The upload error message |
|
195 | + * @var array |
|
196 | + */ |
|
197 | + public $error_messages = array(); |
|
198 | + |
|
199 | + /** |
|
200 | + * The upload error message |
|
201 | + * @var string |
|
202 | + */ |
|
203 | + protected $error = null; |
|
204 | + |
|
205 | + /** |
|
206 | + * The logger instance |
|
207 | + * @var Log |
|
208 | + */ |
|
209 | + private $logger; |
|
210 | + |
|
211 | + |
|
212 | + /** |
|
213 | + * Construct |
|
214 | + * |
|
215 | + * @since 0.1 |
|
216 | + * @version 1.0.1 |
|
217 | + * @return object |
|
218 | + * @method object __construct |
|
219 | + */ |
|
220 | + public function __construct(){ |
|
221 | + $this->logger =& class_loader('Log', 'classes'); |
|
222 | + $this->logger->setLogger('Library::Upload'); |
|
223 | + |
|
224 | + Loader::lang('file_upload'); |
|
225 | + $obj =& get_instance(); |
|
226 | + |
|
227 | + $this->error_messages = array( |
|
228 | + 'upload_err_ini_size' => $obj->lang->get('fu_upload_err_ini_size'), |
|
229 | + 'upload_err_form_size' => $obj->lang->get('fu_upload_err_form_size'), |
|
230 | + 'upload_err_partial' => $obj->lang->get('fu_upload_err_partial'), |
|
231 | + 'upload_err_no_file' => $obj->lang->get('fu_upload_err_no_file'), |
|
232 | + 'upload_err_no_tmp_dir' => $obj->lang->get('fu_upload_err_no_tmp_dir'), |
|
233 | + 'upload_err_cant_write' => $obj->lang->get('fu_upload_err_cant_write'), |
|
234 | + 'upload_err_extension' => $obj->lang->get('fu_upload_err_extension'), |
|
235 | + 'accept_file_types' => $obj->lang->get('fu_accept_file_types'), |
|
236 | + 'file_uploads' => $obj->lang->get('fu_file_uploads_disabled'), |
|
237 | + 'max_file_size' => $obj->lang->get('fu_max_file_size'), |
|
238 | + 'overwritten_not_allowed' => $obj->lang->get('fu_overwritten_not_allowed'), |
|
239 | + ); |
|
240 | + |
|
241 | + $this->file = array( |
|
242 | + 'status' => false, // True: success upload |
|
243 | + 'mime' => '', // Empty string |
|
244 | + 'filename' => '', // Empty string |
|
245 | + 'original' => '', // Empty string |
|
246 | + 'size' => 0, // 0 Bytes |
|
247 | + 'sizeFormated' => '0B', // 0 Bytes |
|
248 | + 'destination' => './', // Default: ./ |
|
249 | + 'allowed_mime_types' => array(), // Allowed mime types |
|
250 | + 'error' => null, // File error |
|
251 | + ); |
|
252 | + |
|
253 | + // Change dir to current dir |
|
254 | + $this->destination_directory = dirname(__FILE__) . DIRECTORY_SEPARATOR; |
|
255 | + |
|
256 | + // Set file array |
|
257 | + if (isset($_FILES) && is_array($_FILES)) { |
|
258 | + $this->file_array = $_FILES; |
|
259 | + } elseif (isset($HTTP_POST_FILES) && is_array($HTTP_POST_FILES)) { |
|
260 | + $this->file_array = $HTTP_POST_FILES; |
|
261 | + } |
|
262 | + $this->logger->info('The upload file information are : ' .stringfy_vars($this->file_array)); |
|
263 | + } |
|
264 | + /** |
|
265 | + * Set input. |
|
266 | + * If you have $_FILES["file"], you must use the key "file" |
|
267 | + * Example: |
|
268 | + * $object->setInput("file"); |
|
269 | + * |
|
270 | + * @since 1.0 |
|
271 | + * @version 1.0 |
|
272 | + * @param string $input |
|
273 | + * @return boolean |
|
274 | + * @method boolean setInput |
|
275 | + */ |
|
276 | + public function setInput($input) |
|
277 | + { |
|
278 | + if (!empty($input) && (is_string($input) || is_numeric($input) )) { |
|
279 | + $this->input = $input; |
|
280 | + } |
|
281 | + return $this; |
|
282 | + } |
|
283 | + /** |
|
284 | + * Set new filename |
|
285 | + * Example: |
|
286 | + * FileUpload::setFilename("new file.txt") |
|
287 | + * Remember: |
|
288 | + * Use %s to retrive file extension |
|
289 | + * |
|
290 | + * @since 1.0 |
|
291 | + * @version 1.0 |
|
292 | + * @param string $filename |
|
293 | + * @return boolean |
|
294 | + * @method boolean setFilename |
|
295 | + */ |
|
296 | + public function setFilename($filename) |
|
297 | + { |
|
298 | + if ($this->isFilename($filename)) { |
|
299 | + $this->filename = $filename; |
|
300 | + } |
|
301 | + return $this; |
|
302 | + } |
|
303 | + /** |
|
304 | + * Set automatic filename |
|
305 | + * |
|
306 | + * @since 1.0 |
|
307 | + * @version 1.5 |
|
308 | + * @param string $extension |
|
309 | + * @return boolean |
|
310 | + * @method boolean setAutoFilename |
|
311 | + */ |
|
312 | + public function setAutoFilename() |
|
313 | + { |
|
314 | + $this->filename = sha1(mt_rand(1, 9999).uniqid()); |
|
315 | + $this->filename .= time(); |
|
316 | + return $this; |
|
317 | + } |
|
318 | + /** |
|
319 | + * Set file size limit |
|
320 | + * |
|
321 | + * @since 1.0 |
|
322 | + * @version 1.0 |
|
323 | + * @param integer $file_size |
|
324 | + * @return boolean |
|
325 | + * @method boolean setMaxFileSize |
|
326 | + */ |
|
327 | + public function setMaxFileSize($file_size) |
|
328 | + { |
|
329 | + $file_size = $this->sizeInBytes($file_size); |
|
330 | + if (is_numeric($file_size) && $file_size > -1) { |
|
331 | + // Get php config |
|
332 | + $php_size = $this->sizeInBytes(ini_get('upload_max_filesize')); |
|
333 | + // Calculate difference |
|
334 | + if ($php_size < $file_size) { |
|
335 | + $this->logger->warning('The upload max file size you set [' .$file_size. '] is greather than the PHP configuration for upload max file size [' .$php_size. ']'); |
|
336 | + } |
|
337 | + $this->max_file_size = $file_size; |
|
338 | + } |
|
339 | + return $this; |
|
340 | + } |
|
341 | + /** |
|
342 | + * Set array mime types |
|
343 | + * |
|
344 | + * @since 1.0 |
|
345 | + * @version 1.0 |
|
346 | + * @param array $mimes |
|
347 | + * @return boolean |
|
348 | + * @method boolean setAllowedMimeTypes |
|
349 | + */ |
|
350 | + public function setAllowedMimeTypes(array $mimes) |
|
351 | + { |
|
352 | + if (count($mimes) > 0) { |
|
353 | + array_map(array($this , 'setAllowMimeType'), $mimes); |
|
354 | + } |
|
355 | + return $this; |
|
356 | + } |
|
357 | + /** |
|
358 | + * Set input callback |
|
359 | + * |
|
360 | + * @since 1.0 |
|
361 | + * @version 1.0 |
|
362 | + * @param mixed $callback |
|
363 | + * @return boolean |
|
364 | + * @method boolean setCallbackInput |
|
365 | + */ |
|
366 | + public function setCallbackInput($callback) |
|
367 | + { |
|
368 | + if (is_callable($callback, false)) { |
|
369 | + $this->callbacks['input'] = $callback; |
|
370 | + } |
|
371 | + return $this; |
|
372 | + } |
|
373 | + /** |
|
374 | + * Set output callback |
|
375 | + * |
|
376 | + * @since 1.0 |
|
377 | + * @version 1.0 |
|
378 | + * @param mixed $callback |
|
379 | + * @return boolean |
|
380 | + * @method boolean setCallbackOutput |
|
381 | + */ |
|
382 | + public function setCallbackOutput($callback) |
|
383 | + { |
|
384 | + if (is_callable($callback, false)) { |
|
385 | + $this->callbacks['output'] = $callback; |
|
386 | + } |
|
387 | + return $this; |
|
388 | + } |
|
389 | + /** |
|
390 | + * Append a mime type to allowed mime types |
|
391 | + * |
|
392 | + * @since 1.0 |
|
393 | + * @version 1.0.1 |
|
394 | + * @param string $mime |
|
395 | + * @return boolean |
|
396 | + * @method boolean setAllowMimeType |
|
397 | + */ |
|
398 | + public function setAllowMimeType($mime) |
|
399 | + { |
|
400 | + if (!empty($mime) && is_string($mime)) { |
|
401 | + $this->allowed_mime_types[] = strtolower($mime); |
|
402 | + $this->file['allowed_mime_types'][] = strtolower($mime); |
|
403 | + } |
|
404 | + return $this; |
|
405 | + } |
|
406 | + /** |
|
407 | + * Set allowed mime types from mime helping |
|
408 | + * |
|
409 | + * @since 1.0.1 |
|
410 | + * @version 1.0.1 |
|
411 | + * @return boolean |
|
412 | + * @method boolean setMimeHelping |
|
413 | + */ |
|
414 | + public function setMimeHelping($name) |
|
415 | + { |
|
416 | + if (!empty($name) && is_string($name)) { |
|
417 | + if (array_key_exists($name, $this->mime_helping)) { |
|
418 | + return $this->setAllowedMimeTypes($this->mime_helping[ $name ]); |
|
419 | + } |
|
420 | + } |
|
421 | + return $this; |
|
422 | + } |
|
423 | + /** |
|
424 | + * Set function to upload file |
|
425 | + * Examples: |
|
426 | + * 1.- FileUpload::setUploadFunction("move_uploaded_file"); |
|
427 | + * 2.- FileUpload::setUploadFunction("copy"); |
|
428 | + * |
|
429 | + * @since 1.0 |
|
430 | + * @version 1.0 |
|
431 | + * @param string $mime |
|
432 | + * @return boolean |
|
433 | + * @method boolean setUploadFunction |
|
434 | + */ |
|
435 | + public function setUploadFunction($function) |
|
436 | + { |
|
437 | + if (!empty($function) && (is_array($function) || is_string($function) )) { |
|
438 | + if (is_callable( $function)) { |
|
439 | + $this->upload_function = $function; |
|
440 | + } |
|
441 | + } |
|
442 | + return $this; |
|
443 | + } |
|
444 | + /** |
|
445 | + * Clear allowed mime types cache |
|
446 | + * |
|
447 | + * @since 1.0 |
|
448 | + * @version 1.0 |
|
449 | + * @return boolean |
|
450 | + * @method boolean clearAllowedMimeTypes |
|
451 | + */ |
|
452 | + public function clearAllowedMimeTypes() |
|
453 | + { |
|
454 | + $this->allowed_mime_types = array(); |
|
455 | + $this->file['allowed_mime_types'] = array(); |
|
456 | + return $this; |
|
457 | + } |
|
458 | + /** |
|
459 | + * Set destination output |
|
460 | + * |
|
461 | + * @since 1.0 |
|
462 | + * @version 1.0 |
|
463 | + * @param string $destination_directory Destination path |
|
464 | + * @param boolean $create_if_not_exist |
|
465 | + * @return boolean |
|
466 | + * @method boolean setDestinationDirectory |
|
467 | + */ |
|
468 | + public function setDestinationDirectory($destination_directory, $create_if_not_exist = false) { |
|
469 | + $destination_directory = realpath($destination_directory); |
|
470 | + if (substr($destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
471 | + $destination_directory .= DIRECTORY_SEPARATOR; |
|
472 | + } |
|
473 | + |
|
474 | + if ($this->isDirpath($destination_directory)) { |
|
475 | + if ($this->dirExists($destination_directory)) { |
|
476 | + $this->destination_directory = $destination_directory; |
|
477 | + if (substr($this->destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
478 | + $this->destination_directory .= DIRECTORY_SEPARATOR; |
|
479 | + } |
|
480 | + chdir($destination_directory); |
|
481 | + } elseif ($create_if_not_exist === true) { |
|
482 | + if (mkdir($destination_directory, 0775, true)) { |
|
483 | + if ($this->dirExists($destination_directory)) { |
|
484 | + $this->destination_directory = $destination_directory; |
|
485 | + if (substr($this->destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
486 | + $this->destination_directory .= DIRECTORY_SEPARATOR; |
|
487 | + } |
|
488 | + chdir($destination_directory); |
|
489 | + } |
|
490 | + } |
|
491 | + else{ |
|
492 | + $this->logger->warning('Can not create the upload directory [' .$destination_directory. ']'); |
|
493 | + } |
|
494 | + } |
|
495 | + } |
|
496 | + return $this; |
|
497 | + } |
|
498 | + /** |
|
499 | + * Check file exists |
|
500 | + * |
|
501 | + * @since 1.0 |
|
502 | + * @version 1.0.1 |
|
503 | + * @param string $file_destination |
|
504 | + * @return boolean |
|
505 | + * @method boolean fileExists |
|
506 | + */ |
|
507 | + public function fileExists($file_destination) |
|
508 | + { |
|
509 | + if ($this->isFilename($file_destination)) { |
|
510 | + return (file_exists($file_destination) && is_file($file_destination)); |
|
511 | + } |
|
512 | + return false; |
|
513 | + } |
|
514 | + /** |
|
515 | + * Check dir exists |
|
516 | + * |
|
517 | + * @since 1.0 |
|
518 | + * @version 1.0.1 |
|
519 | + * @param string $path |
|
520 | + * @return boolean |
|
521 | + * @method boolean dirExists |
|
522 | + */ |
|
523 | + public function dirExists($path) |
|
524 | + { |
|
525 | + if ($this->isDirpath($path)) { |
|
526 | + return (file_exists($path) && is_dir($path)); |
|
527 | + } |
|
528 | + return false; |
|
529 | + } |
|
530 | + /** |
|
531 | + * Check valid filename |
|
532 | + * |
|
533 | + * @since 1.0 |
|
534 | + * @version 1.0.1 |
|
535 | + * @param string $filename |
|
536 | + * @return boolean |
|
537 | + * @method boolean isFilename |
|
538 | + */ |
|
539 | + public function isFilename($filename) |
|
540 | + { |
|
541 | + $filename = basename($filename); |
|
542 | + return (!empty($filename) && (is_string( $filename) || is_numeric($filename))); |
|
543 | + } |
|
544 | + /** |
|
545 | + * Validate mime type with allowed mime types, |
|
546 | + * but if allowed mime types is empty, this method return true |
|
547 | + * |
|
548 | + * @since 1.0 |
|
549 | + * @version 1.0 |
|
550 | + * @param string $mime |
|
551 | + * @return boolean |
|
552 | + * @method boolean checkMimeType |
|
553 | + */ |
|
554 | + public function checkMimeType($mime) |
|
555 | + { |
|
556 | + if (count($this->allowed_mime_types) == 0) { |
|
557 | + return true; |
|
558 | + } |
|
559 | + return in_array(strtolower($mime), $this->allowed_mime_types); |
|
560 | + } |
|
561 | + /** |
|
562 | + * Retrive status of upload |
|
563 | + * |
|
564 | + * @since 1.0 |
|
565 | + * @version 1.0 |
|
566 | + * @return boolean |
|
567 | + * @method boolean getStatus |
|
568 | + */ |
|
569 | + public function getStatus() |
|
570 | + { |
|
571 | + return $this->file['status']; |
|
572 | + } |
|
573 | + /** |
|
574 | + * Check valid path |
|
575 | + * |
|
576 | + * @since 1.0 |
|
577 | + * @version 1.0.1 |
|
578 | + * @param string $filename |
|
579 | + * @return boolean |
|
580 | + * @method boolean isDirpath |
|
581 | + */ |
|
582 | + public function isDirpath($path) |
|
583 | + { |
|
584 | + if (!empty( $path) && (is_string( $path) || is_numeric($path) )) { |
|
585 | + if (DIRECTORY_SEPARATOR == '/') { |
|
586 | + return (preg_match( '/^[^*?"<>|:]*$/' , $path) == 1 ); |
|
587 | + } else { |
|
588 | + return (preg_match( "/^[^*?\"<>|:]*$/" , substr($path,2) ) == 1); |
|
589 | + } |
|
590 | + } |
|
591 | + return false; |
|
592 | + } |
|
593 | + /** |
|
594 | + * Allow overwriting files |
|
595 | + * |
|
596 | + * @since 1.0 |
|
597 | + * @version 1.0 |
|
598 | + * @return boolean |
|
599 | + * @method boolean allowOverwriting |
|
600 | + */ |
|
601 | + public function allowOverwriting() |
|
602 | + { |
|
603 | + $this->overwrite_file = true; |
|
604 | + return $this; |
|
605 | + } |
|
606 | + /** |
|
607 | + * File info |
|
608 | + * |
|
609 | + * @since 1.0 |
|
610 | + * @version 1.0 |
|
611 | + * @return object |
|
612 | + * @method object getInfo |
|
613 | + */ |
|
614 | + public function getInfo() |
|
615 | + { |
|
616 | + return (object)$this->file; |
|
617 | + } |
|
618 | + |
|
619 | + public function isUploaded(){ |
|
620 | + return isset($this->file_array[$this->input]) |
|
621 | + && |
|
622 | + is_uploaded_file($this->file_array[$this->input]['tmp_name']); |
|
623 | + } |
|
624 | + /** |
|
625 | + * Upload file |
|
626 | + * |
|
627 | + * @since 1.0 |
|
628 | + * @version 1.0.1 |
|
629 | + * @return boolean |
|
630 | + * @method boolean save |
|
631 | + */ |
|
632 | + public function save(){ |
|
633 | + //check if file upload is allowed in the configuration |
|
634 | + if(! ini_get('file_uploads')){ |
|
635 | + $this->setError($this->error_messages['file_uploads']); |
|
636 | + return false; |
|
637 | + } |
|
638 | + if (count($this->file_array) > 0) { |
|
639 | + if (array_key_exists($this->input, $this->file_array)) { |
|
640 | + // set original filename if not have a new name |
|
641 | + if (empty($this->filename)) { |
|
642 | + $this->filename = $this->file_array[$this->input]['name']; |
|
643 | + } |
|
644 | + else{ |
|
645 | + // Replace %s for extension in filename |
|
646 | + // Before: /[\w\d]*(.[\d\w]+)$/i |
|
647 | + // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |
|
648 | + // Support unicode(utf-8) characters |
|
649 | + // Example: "русские.jpeg" is valid; "Zhōngguó.jpeg" is valid; "Tønsberg.jpeg" is valid |
|
650 | + $extension = preg_replace( |
|
651 | + '/^[\p{L}\d\s\-\_\.\(\)]*\.([\d\w]+)$/iu', |
|
652 | + '$1', |
|
653 | + $this->file_array[$this->input]['name'] |
|
654 | + ); |
|
655 | + $this->filename = $this->filename.'.'.$extension; |
|
656 | + } |
|
657 | + |
|
658 | + // set file info |
|
659 | + $this->file['mime'] = $this->file_array[$this->input]['type']; |
|
660 | + $this->file['tmp'] = $this->file_array[$this->input]['tmp_name']; |
|
661 | + $this->file['original'] = $this->file_array[$this->input]['name']; |
|
662 | + $this->file['size'] = $this->file_array[$this->input]['size']; |
|
663 | + $this->file['sizeFormated'] = $this->sizeFormat($this->file['size']); |
|
664 | + $this->file['destination'] = $this->destination_directory . $this->filename; |
|
665 | + $this->file['filename'] = $this->filename; |
|
666 | + $this->file['error'] = $this->file_array[$this->input]['error']; |
|
667 | + |
|
668 | + $this->logger->info('The upload file information to process is : ' .stringfy_vars($this->file)); |
|
669 | + |
|
670 | + //check for php upload error |
|
671 | + if(is_numeric($this->file['error']) && $this->file['error'] > 0){ |
|
672 | + $this->setError($this->getPhpUploadErrorMessageByCode($this->file['error'])); |
|
673 | + return false; |
|
674 | + } |
|
675 | 675 | |
676 | - //check for mime type |
|
677 | - if (!$this->checkMimeType($this->file['mime'])) { |
|
678 | - $this->setError($this->error_messages['accept_file_types']); |
|
679 | - return false; |
|
680 | - } |
|
681 | - |
|
682 | - // Check file size |
|
683 | - if ($this->max_file_size > 0) { |
|
684 | - if ($this->max_file_size < $this->file['size']) { |
|
685 | - $this->setError(sprintf($this->error_messages['max_file_size'], $this->sizeFormat($this->max_file_size))); |
|
686 | - return false; |
|
687 | - } |
|
688 | - } |
|
689 | - |
|
690 | - // Check if exists file |
|
691 | - if ($this->fileExists($this->destination_directory . $this->filename)) { |
|
692 | - // Check if overwrite file |
|
693 | - if ($this->overwrite_file === false) { |
|
694 | - $this->setError($this->error_messages['overwritten_not_allowed']); |
|
695 | - return false; |
|
696 | - } |
|
697 | - } |
|
698 | - |
|
699 | - // Execute input callback |
|
700 | - if (!empty( $this->callbacks['input'])) { |
|
701 | - call_user_func($this->callbacks['input'], (object)$this->file); |
|
702 | - } |
|
676 | + //check for mime type |
|
677 | + if (!$this->checkMimeType($this->file['mime'])) { |
|
678 | + $this->setError($this->error_messages['accept_file_types']); |
|
679 | + return false; |
|
680 | + } |
|
681 | + |
|
682 | + // Check file size |
|
683 | + if ($this->max_file_size > 0) { |
|
684 | + if ($this->max_file_size < $this->file['size']) { |
|
685 | + $this->setError(sprintf($this->error_messages['max_file_size'], $this->sizeFormat($this->max_file_size))); |
|
686 | + return false; |
|
687 | + } |
|
688 | + } |
|
689 | + |
|
690 | + // Check if exists file |
|
691 | + if ($this->fileExists($this->destination_directory . $this->filename)) { |
|
692 | + // Check if overwrite file |
|
693 | + if ($this->overwrite_file === false) { |
|
694 | + $this->setError($this->error_messages['overwritten_not_allowed']); |
|
695 | + return false; |
|
696 | + } |
|
697 | + } |
|
698 | + |
|
699 | + // Execute input callback |
|
700 | + if (!empty( $this->callbacks['input'])) { |
|
701 | + call_user_func($this->callbacks['input'], (object)$this->file); |
|
702 | + } |
|
703 | 703 | |
704 | 704 | |
705 | - $this->file['status'] = call_user_func_array( |
|
706 | - $this->upload_function, array( |
|
707 | - $this->file_array[$this->input]['tmp_name'], |
|
708 | - $this->destination_directory . $this->filename |
|
709 | - ) |
|
710 | - ); |
|
711 | - |
|
712 | - // Execute output callback |
|
713 | - if (!empty( $this->callbacks['output'])) { |
|
714 | - call_user_func($this->callbacks['output'], (object)$this->file); |
|
715 | - } |
|
716 | - return $this->file['status']; |
|
717 | - } |
|
718 | - } |
|
719 | - } |
|
720 | - |
|
721 | - /** |
|
722 | - * File size for humans. |
|
723 | - * |
|
724 | - * @since 1.0 |
|
725 | - * @version 1.0 |
|
726 | - * @param integer $bytes |
|
727 | - * @param integer $precision |
|
728 | - * @return string |
|
729 | - * @method string sizeFormat |
|
730 | - */ |
|
731 | - public function sizeFormat($size, $precision = 2) |
|
732 | - { |
|
733 | - if($size > 0){ |
|
734 | - $base = log($size) / log(1024); |
|
735 | - $suffixes = array('B', 'K', 'M', 'G', 'T'); |
|
736 | - return round(pow(1024, $base - floor($base)), $precision) . ( isset($suffixes[floor($base)]) ? $suffixes[floor($base)] : ''); |
|
737 | - } |
|
738 | - return null; |
|
739 | - } |
|
705 | + $this->file['status'] = call_user_func_array( |
|
706 | + $this->upload_function, array( |
|
707 | + $this->file_array[$this->input]['tmp_name'], |
|
708 | + $this->destination_directory . $this->filename |
|
709 | + ) |
|
710 | + ); |
|
711 | + |
|
712 | + // Execute output callback |
|
713 | + if (!empty( $this->callbacks['output'])) { |
|
714 | + call_user_func($this->callbacks['output'], (object)$this->file); |
|
715 | + } |
|
716 | + return $this->file['status']; |
|
717 | + } |
|
718 | + } |
|
719 | + } |
|
720 | + |
|
721 | + /** |
|
722 | + * File size for humans. |
|
723 | + * |
|
724 | + * @since 1.0 |
|
725 | + * @version 1.0 |
|
726 | + * @param integer $bytes |
|
727 | + * @param integer $precision |
|
728 | + * @return string |
|
729 | + * @method string sizeFormat |
|
730 | + */ |
|
731 | + public function sizeFormat($size, $precision = 2) |
|
732 | + { |
|
733 | + if($size > 0){ |
|
734 | + $base = log($size) / log(1024); |
|
735 | + $suffixes = array('B', 'K', 'M', 'G', 'T'); |
|
736 | + return round(pow(1024, $base - floor($base)), $precision) . ( isset($suffixes[floor($base)]) ? $suffixes[floor($base)] : ''); |
|
737 | + } |
|
738 | + return null; |
|
739 | + } |
|
740 | 740 | |
741 | 741 | |
742 | - /** |
|
743 | - * Convert human file size to bytes |
|
744 | - * |
|
745 | - * @since 1.0 |
|
746 | - * @version 1.0.1 |
|
747 | - * @param integer $size |
|
748 | - * @return string |
|
749 | - * @method string sizeInBytes |
|
750 | - */ |
|
751 | - public function sizeInBytes($size) |
|
752 | - { |
|
753 | - $unit = 'B'; |
|
754 | - $units = array('B' => 0, 'K' => 1, 'M' => 2, 'G' => 3, 'T' => 4); |
|
755 | - $matches = array(); |
|
756 | - preg_match('/(?<size>[\d\.]+)\s*(?<unit>b|k|m|g|t)?/i', $size, $matches); |
|
757 | - if (array_key_exists('unit', $matches)) { |
|
758 | - $unit = strtoupper($matches['unit']); |
|
759 | - } |
|
760 | - return (floatval($matches['size']) * pow(1024, $units[$unit]) ) ; |
|
761 | - } |
|
762 | - |
|
763 | - /** |
|
764 | - * Get the upload error message |
|
765 | - * @return string |
|
766 | - */ |
|
767 | - public function getError(){ |
|
768 | - return $this->error; |
|
769 | - } |
|
770 | - |
|
771 | - /** |
|
772 | - * Set the upload error message |
|
773 | - * @param string $message the upload error message to set |
|
774 | - */ |
|
775 | - public function setError($message){ |
|
776 | - $this->logger->info('The upload got error : ' . $message); |
|
777 | - $this->error = $message; |
|
778 | - } |
|
779 | - |
|
780 | - /** |
|
781 | - * Get the PHP upload error message for the given code |
|
782 | - * @param int $code the error code |
|
783 | - * @return string the error message |
|
784 | - */ |
|
785 | - private function getPhpUploadErrorMessageByCode($code){ |
|
786 | - $codeMessageMaps = array( |
|
787 | - 1 => $this->error_messages['upload_err_ini_size'], |
|
788 | - 2 => $this->error_messages['upload_err_form_size'], |
|
789 | - 3 => $this->error_messages['upload_err_partial'], |
|
790 | - 4 => $this->error_messages['upload_err_no_file'], |
|
791 | - 6 => $this->error_messages['upload_err_no_tmp_dir'], |
|
792 | - 7 => $this->error_messages['upload_err_cant_write'], |
|
793 | - 8 => $this->error_messages['upload_err_extension'], |
|
794 | - ); |
|
795 | - return isset($codeMessageMaps[$code]) ? $codeMessageMaps[$code] : null; |
|
796 | - } |
|
797 | - } |
|
742 | + /** |
|
743 | + * Convert human file size to bytes |
|
744 | + * |
|
745 | + * @since 1.0 |
|
746 | + * @version 1.0.1 |
|
747 | + * @param integer $size |
|
748 | + * @return string |
|
749 | + * @method string sizeInBytes |
|
750 | + */ |
|
751 | + public function sizeInBytes($size) |
|
752 | + { |
|
753 | + $unit = 'B'; |
|
754 | + $units = array('B' => 0, 'K' => 1, 'M' => 2, 'G' => 3, 'T' => 4); |
|
755 | + $matches = array(); |
|
756 | + preg_match('/(?<size>[\d\.]+)\s*(?<unit>b|k|m|g|t)?/i', $size, $matches); |
|
757 | + if (array_key_exists('unit', $matches)) { |
|
758 | + $unit = strtoupper($matches['unit']); |
|
759 | + } |
|
760 | + return (floatval($matches['size']) * pow(1024, $units[$unit]) ) ; |
|
761 | + } |
|
762 | + |
|
763 | + /** |
|
764 | + * Get the upload error message |
|
765 | + * @return string |
|
766 | + */ |
|
767 | + public function getError(){ |
|
768 | + return $this->error; |
|
769 | + } |
|
770 | + |
|
771 | + /** |
|
772 | + * Set the upload error message |
|
773 | + * @param string $message the upload error message to set |
|
774 | + */ |
|
775 | + public function setError($message){ |
|
776 | + $this->logger->info('The upload got error : ' . $message); |
|
777 | + $this->error = $message; |
|
778 | + } |
|
779 | + |
|
780 | + /** |
|
781 | + * Get the PHP upload error message for the given code |
|
782 | + * @param int $code the error code |
|
783 | + * @return string the error message |
|
784 | + */ |
|
785 | + private function getPhpUploadErrorMessageByCode($code){ |
|
786 | + $codeMessageMaps = array( |
|
787 | + 1 => $this->error_messages['upload_err_ini_size'], |
|
788 | + 2 => $this->error_messages['upload_err_form_size'], |
|
789 | + 3 => $this->error_messages['upload_err_partial'], |
|
790 | + 4 => $this->error_messages['upload_err_no_file'], |
|
791 | + 6 => $this->error_messages['upload_err_no_tmp_dir'], |
|
792 | + 7 => $this->error_messages['upload_err_cant_write'], |
|
793 | + 8 => $this->error_messages['upload_err_extension'], |
|
794 | + ); |
|
795 | + return isset($codeMessageMaps[$code]) ? $codeMessageMaps[$code] : null; |
|
796 | + } |
|
797 | + } |