1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
6
|
|
|
use Doctrine\Common\Proxy\AbstractProxyFactory; |
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
use Doctrine\DBAL\Driver\Statement; |
9
|
|
|
use Doctrine\DBAL\Types\Type; |
10
|
|
|
use Doctrine\ORM\EntityManager; |
11
|
|
|
use Doctrine\ORM\Tools\Setup; |
12
|
|
|
use Symfony\Component\Debug\ExceptionHandler; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Database. |
16
|
|
|
*/ |
17
|
|
|
class Database |
18
|
|
|
{ |
19
|
|
|
public static $utcDateTimeClass; |
20
|
|
|
/** |
21
|
|
|
* @var EntityManager |
22
|
|
|
*/ |
23
|
|
|
private static $em; |
24
|
|
|
private static $connection; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Set the DB manager instance. |
28
|
|
|
* |
29
|
|
|
* @param EntityManager $em |
30
|
|
|
*/ |
31
|
|
|
public function setManager($em) |
32
|
|
|
{ |
33
|
|
|
self::$em = $em; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set the DB connection instance. |
38
|
|
|
*/ |
39
|
|
|
public function setConnection(Connection $connection) |
40
|
|
|
{ |
41
|
|
|
self::$connection = $connection; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the DB connection instance. |
46
|
|
|
* |
47
|
|
|
* @return Connection |
48
|
|
|
*/ |
49
|
|
|
public function getConnection() |
50
|
|
|
{ |
51
|
|
|
return self::$connection; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the DB manager instance. |
56
|
|
|
* |
57
|
|
|
* @return EntityManager |
58
|
|
|
*/ |
59
|
|
|
public static function getManager() |
60
|
|
|
{ |
61
|
|
|
return self::$em; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns the name of the main database. |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public static function get_main_database() |
70
|
|
|
{ |
71
|
|
|
return self::getManager()->getConnection()->getDatabase(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get main table. |
76
|
|
|
* |
77
|
|
|
* @param string $table |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public static function get_main_table($table) |
82
|
|
|
{ |
83
|
|
|
return $table; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get course table. |
88
|
|
|
* |
89
|
|
|
* @param string $table |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public static function get_course_table($table) |
94
|
|
|
{ |
95
|
|
|
return DB_COURSE_PREFIX.$table; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Counts the number of rows in a table. |
100
|
|
|
* |
101
|
|
|
* @param string $table The table of which the rows should be counted |
102
|
|
|
* |
103
|
|
|
* @return int the number of rows in the given table |
104
|
|
|
* |
105
|
|
|
* @deprecated |
106
|
|
|
*/ |
107
|
|
|
public static function count_rows($table) |
108
|
|
|
{ |
109
|
|
|
$obj = self::fetch_object(self::query("SELECT COUNT(*) AS n FROM $table")); |
110
|
|
|
|
111
|
|
|
return $obj->n; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns the number of affected rows in the last database operation. |
116
|
|
|
* |
117
|
|
|
* @return int |
118
|
|
|
*/ |
119
|
|
|
public static function affected_rows($result) |
120
|
|
|
{ |
121
|
|
|
return $result->rowCount(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public static function getUTCDateTimeTypeClass() |
128
|
|
|
{ |
129
|
|
|
return isset(self::$utcDateTimeClass) ? self::$utcDateTimeClass : 'Application\DoctrineExtensions\DBAL\Types\UTCDateTimeType'; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Connect to the database sets the entity manager. |
134
|
|
|
* |
135
|
|
|
* @param array $params |
136
|
|
|
* @param string $sysPath |
137
|
|
|
* @param string $entityRootPath |
138
|
|
|
* @param bool $returnConnection |
139
|
|
|
* @param bool $returnManager |
140
|
|
|
* |
141
|
|
|
* @throws \Doctrine\ORM\ORMException |
142
|
|
|
* |
143
|
|
|
* @return |
144
|
|
|
*/ |
145
|
|
|
public function connect( |
146
|
|
|
$params = [], |
147
|
|
|
$sysPath = '', |
148
|
|
|
$entityRootPath = '', |
149
|
|
|
$returnConnection = false, |
150
|
|
|
$returnManager = false |
151
|
|
|
) { |
152
|
|
|
$config = self::getDoctrineConfig($entityRootPath); |
153
|
|
|
|
154
|
|
|
$params['charset'] = 'utf8'; |
155
|
|
|
$entityManager = EntityManager::create($params, $config); |
156
|
|
|
$sysPath = !empty($sysPath) ? $sysPath : api_get_path(SYS_PATH); |
157
|
|
|
|
158
|
|
|
// Registering Constraints |
159
|
|
|
/*AnnotationRegistry::registerAutoloadNamespace( |
160
|
|
|
'Symfony\Component', |
161
|
|
|
$sysPath."vendor/" |
162
|
|
|
);*/ |
163
|
|
|
|
164
|
|
|
AnnotationRegistry::registerLoader( |
165
|
|
|
function ($class) use ($sysPath) { |
166
|
|
|
$file = str_replace("\\", DIRECTORY_SEPARATOR, $class).".php"; |
167
|
|
|
$file = str_replace('Symfony/Component/Validator', '', $file); |
168
|
|
|
$file = str_replace('Symfony\Component\Validator', '', $file); |
169
|
|
|
$fileToInclude = $sysPath.'vendor/symfony/validator/'.$file; |
170
|
|
|
|
171
|
|
|
if (file_exists($fileToInclude)) { |
172
|
|
|
// file exists makes sure that the loader fails silently |
173
|
|
|
require_once $fileToInclude; |
174
|
|
|
|
175
|
|
|
return true; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$fileToInclude = $sysPath.'vendor/symfony/validator/Constraints/'.$file; |
179
|
|
|
if (file_exists($fileToInclude)) { |
180
|
|
|
// file exists makes sure that the loader fails silently |
181
|
|
|
require_once $fileToInclude; |
182
|
|
|
|
183
|
|
|
return true; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
); |
187
|
|
|
|
188
|
|
|
AnnotationRegistry::registerFile( |
189
|
|
|
$sysPath."vendor/symfony/doctrine-bridge/Validator/Constraints/UniqueEntity.php" |
190
|
|
|
); |
191
|
|
|
|
192
|
|
|
// Registering gedmo extensions |
193
|
|
|
AnnotationRegistry::registerAutoloadNamespace( |
194
|
|
|
'Gedmo\Mapping\Annotation', |
195
|
|
|
$sysPath."vendor/gedmo/doctrine-extensions/lib" |
196
|
|
|
); |
197
|
|
|
|
198
|
|
|
Type::overrideType( |
199
|
|
|
Type::DATETIME, |
200
|
|
|
self::getUTCDateTimeTypeClass() |
201
|
|
|
); |
202
|
|
|
|
203
|
|
|
$listener = new \Gedmo\Timestampable\TimestampableListener(); |
204
|
|
|
$entityManager->getEventManager()->addEventSubscriber($listener); |
205
|
|
|
|
206
|
|
|
$listener = new \Gedmo\Tree\TreeListener(); |
207
|
|
|
$entityManager->getEventManager()->addEventSubscriber($listener); |
208
|
|
|
|
209
|
|
|
$listener = new \Gedmo\Sortable\SortableListener(); |
210
|
|
|
$entityManager->getEventManager()->addEventSubscriber($listener); |
211
|
|
|
$connection = $entityManager->getConnection(); |
212
|
|
|
$connection->executeQuery('SET sql_mode = "";'); |
213
|
|
|
$connection->executeQuery('SET SESSION sql_mode = ""'); |
214
|
|
|
|
215
|
|
|
if ($returnConnection) { |
216
|
|
|
return $connection; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if ($returnManager) { |
220
|
|
|
return $entityManager; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$this->setConnection($connection); |
224
|
|
|
$this->setManager($entityManager); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Escape MySQL wildcards _ and % in LIKE search. |
229
|
|
|
* |
230
|
|
|
* @param string $text The string to escape |
231
|
|
|
* |
232
|
|
|
* @return string The escaped string |
233
|
|
|
*/ |
234
|
|
|
public static function escape_sql_wildcards($text) |
235
|
|
|
{ |
236
|
|
|
$text = api_preg_replace("/_/", "\_", $text); |
237
|
|
|
$text = api_preg_replace("/%/", "\%", $text); |
238
|
|
|
|
239
|
|
|
return $text; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Escapes a string to insert into the database as text. |
244
|
|
|
* |
245
|
|
|
* @param string $string |
246
|
|
|
* |
247
|
|
|
* @return string |
248
|
|
|
*/ |
249
|
|
|
public static function escape_string($string) |
250
|
|
|
{ |
251
|
|
|
$string = self::getManager()->getConnection()->quote($string); |
252
|
|
|
// The quote method from PDO also adds quotes around the string, which |
253
|
|
|
// is not how the legacy mysql_real_escape_string() was used in |
254
|
|
|
// Chamilo, so we need to remove the quotes around. Using trim will |
255
|
|
|
// remove more than one quote if they are sequenced, generating |
256
|
|
|
// broken queries and SQL injection risks |
257
|
|
|
return substr($string, 1, -1); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Gets the array from a SQL result (as returned by Database::query). |
262
|
|
|
* |
263
|
|
|
* @param string $option Optional: "ASSOC","NUM" or "BOTH" |
264
|
|
|
* |
265
|
|
|
* @return array|mixed |
266
|
|
|
*/ |
267
|
|
|
public static function fetch_array($result, $option = 'BOTH') |
268
|
|
|
{ |
269
|
|
|
if ($result === false) { |
270
|
|
|
return []; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return $result->fetch(self::customOptionToDoctrineOption($option)); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Gets an associative array from a SQL result (as returned by Database::query). |
278
|
|
|
* |
279
|
|
|
* @return array |
280
|
|
|
*/ |
281
|
|
|
public static function fetch_assoc($result) |
282
|
|
|
{ |
283
|
|
|
return $result->fetch(PDO::FETCH_ASSOC); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Gets the next row of the result of the SQL query |
288
|
|
|
* (as returned by Database::query) in an object form. |
289
|
|
|
* |
290
|
|
|
* @return mixed |
291
|
|
|
*/ |
292
|
|
|
public static function fetch_object($result) |
293
|
|
|
{ |
294
|
|
|
return $result->fetch(PDO::FETCH_OBJ); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Gets the array from a SQL result (as returned by Database::query) |
299
|
|
|
* help achieving database independence. |
300
|
|
|
* |
301
|
|
|
* @return mixed |
302
|
|
|
*/ |
303
|
|
|
public static function fetch_row($result) |
304
|
|
|
{ |
305
|
|
|
if ($result === false) { |
306
|
|
|
return []; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
return $result->fetch(PDO::FETCH_NUM); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Frees all the memory associated with the provided result identifier. |
314
|
|
|
* |
315
|
|
|
* @return bool|null Returns TRUE on success or FALSE on failure. |
316
|
|
|
* Notes: Use this method if you are concerned about how much memory is being used for queries that return large result sets. |
317
|
|
|
* Anyway, all associated result memory is automatically freed at the end of the script's execution. |
318
|
|
|
*/ |
319
|
|
|
public static function free_result($result) |
320
|
|
|
{ |
321
|
|
|
$result->closeCursor(); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Gets the ID of the last item inserted into the database. |
326
|
|
|
* |
327
|
|
|
* @return string |
328
|
|
|
*/ |
329
|
|
|
public static function insert_id() |
330
|
|
|
{ |
331
|
|
|
return self::getManager()->getConnection()->lastInsertId(); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Wrapper for rowCount(). |
336
|
|
|
* |
337
|
|
|
* @return int |
338
|
|
|
*/ |
339
|
|
|
public static function num_rows($result) |
340
|
|
|
{ |
341
|
|
|
if ($result === false) { |
342
|
|
|
return 0; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
return $result->rowCount(); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Acts as the relative *_result() function of most DB drivers and fetches a |
350
|
|
|
* specific line and a field. |
351
|
|
|
* |
352
|
|
|
* @param int $row |
353
|
|
|
* @param string $field |
354
|
|
|
* |
355
|
|
|
* @return mixed |
356
|
|
|
*/ |
357
|
|
|
public static function result($resource, $row, $field = '') |
358
|
|
|
{ |
359
|
|
|
if ($resource->rowCount() > 0) { |
360
|
|
|
$result = $resource->fetchAll(PDO::FETCH_BOTH); |
361
|
|
|
|
362
|
|
|
return $result[$row][$field]; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
return false; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Wrapper for executeQuery(). |
370
|
|
|
* |
371
|
|
|
* @param string $query |
372
|
|
|
* |
373
|
|
|
* @return Statement |
374
|
|
|
*/ |
375
|
|
|
public static function query($query) |
376
|
|
|
{ |
377
|
|
|
$connection = self::getManager()->getConnection(); |
378
|
|
|
$result = null; |
379
|
|
|
try { |
380
|
|
|
$result = $connection->executeQuery($query); |
381
|
|
|
} catch (Exception $e) { |
382
|
|
|
self::handleError($e); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
return $result; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Deal with exceptions from the database extension. |
390
|
|
|
* |
391
|
|
|
* @param Exception $e |
392
|
|
|
*/ |
393
|
|
|
public static function handleError($e) |
394
|
|
|
{ |
395
|
|
|
$debug = api_get_setting('server_type') == 'test'; |
396
|
|
|
if ($debug) { |
397
|
|
|
// We use Symfony exception handler for better error information |
398
|
|
|
$handler = new ExceptionHandler(); |
399
|
|
|
$handler->handle($e); |
400
|
|
|
exit; |
|
|
|
|
401
|
|
|
} else { |
402
|
|
|
$msg = $e->getMessage(); |
403
|
|
|
if (preg_match('/Serialization failure:/', $msg)) { |
404
|
|
|
//do nothing except from logging |
405
|
|
|
error_log($msg.' - Reported but otherwise ignored'); |
406
|
|
|
} else { |
407
|
|
|
error_log($msg); |
408
|
|
|
api_not_allowed(false, get_lang('GeneralError')); |
409
|
|
|
exit; |
|
|
|
|
410
|
|
|
} |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Transform an SQL option from Chamilo to PDO syntax. |
416
|
|
|
* |
417
|
|
|
* @param string $option |
418
|
|
|
* |
419
|
|
|
* @return int |
420
|
|
|
*/ |
421
|
|
|
public static function customOptionToDoctrineOption($option) |
422
|
|
|
{ |
423
|
|
|
switch ($option) { |
424
|
|
|
case 'ASSOC': |
425
|
|
|
return PDO::FETCH_ASSOC; |
426
|
|
|
break; |
|
|
|
|
427
|
|
|
case 'NUM': |
428
|
|
|
return PDO::FETCH_NUM; |
429
|
|
|
break; |
430
|
|
|
case 'BOTH': |
431
|
|
|
default: |
432
|
|
|
return PDO::FETCH_BOTH; |
433
|
|
|
break; |
434
|
|
|
} |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* Stores a query result into an array. |
439
|
|
|
* |
440
|
|
|
* @author Olivier Brouckaert |
441
|
|
|
* |
442
|
|
|
* @param Statement $result - the return value of the query |
443
|
|
|
* @param string $option BOTH, ASSOC, or NUM |
444
|
|
|
* |
445
|
|
|
* @return array - the value returned by the query |
446
|
|
|
*/ |
447
|
|
|
public static function store_result($result, $option = 'BOTH') |
448
|
|
|
{ |
449
|
|
|
return $result->fetchAll(self::customOptionToDoctrineOption($option)); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* Build an insert query. |
454
|
|
|
* |
455
|
|
|
* @param string $table_name |
456
|
|
|
* @param array $attributes |
457
|
|
|
* @param bool $show_query |
458
|
|
|
* |
459
|
|
|
* @return false|int |
460
|
|
|
*/ |
461
|
|
|
public static function insert($table_name, $attributes, $show_query = false) |
462
|
|
|
{ |
463
|
|
|
if (empty($attributes) || empty($table_name)) { |
464
|
|
|
return false; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
$params = array_keys($attributes); |
468
|
|
|
|
469
|
|
|
if (!empty($params)) { |
470
|
|
|
$sql = 'INSERT INTO '.$table_name.' ('.implode(',', $params).') |
471
|
|
|
VALUES (:'.implode(', :', $params).')'; |
472
|
|
|
|
473
|
|
|
$statement = self::getManager()->getConnection()->prepare($sql); |
474
|
|
|
$result = $statement->execute($attributes); |
475
|
|
|
|
476
|
|
|
if ($show_query) { |
477
|
|
|
var_dump($sql); |
|
|
|
|
478
|
|
|
error_log($sql); |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
if ($result) { |
482
|
|
|
return (int) self::getManager()->getConnection()->lastInsertId(); |
483
|
|
|
} |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
return false; |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
/** |
490
|
|
|
* Build an update query. |
491
|
|
|
* |
492
|
|
|
* @param string $tableName use Database::get_main_table |
493
|
|
|
* @param array $attributes Values to updates |
494
|
|
|
* Example: $params['name'] = 'Julio'; $params['lastname'] = 'Montoya'; |
495
|
|
|
* @param array $whereConditions where conditions i.e array('id = ?' =>'4') |
496
|
|
|
* @param bool $showQuery |
497
|
|
|
* |
498
|
|
|
* @return bool|int |
499
|
|
|
*/ |
500
|
|
|
public static function update( |
501
|
|
|
$tableName, |
502
|
|
|
$attributes, |
503
|
|
|
$whereConditions = [], |
504
|
|
|
$showQuery = false |
505
|
|
|
) { |
506
|
|
|
if (!empty($tableName) && !empty($attributes)) { |
507
|
|
|
$updateSql = ''; |
508
|
|
|
$count = 1; |
509
|
|
|
|
510
|
|
|
foreach ($attributes as $key => $value) { |
511
|
|
|
if ($showQuery) { |
512
|
|
|
echo $key.': '.$value.PHP_EOL; |
513
|
|
|
} |
514
|
|
|
$updateSql .= "$key = :$key "; |
515
|
|
|
if ($count < count($attributes)) { |
516
|
|
|
$updateSql .= ', '; |
517
|
|
|
} |
518
|
|
|
$count++; |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
if (!empty($updateSql)) { |
522
|
|
|
//Parsing and cleaning the where conditions |
523
|
|
|
$whereReturn = self::parse_where_conditions($whereConditions); |
524
|
|
|
|
525
|
|
|
$sql = "UPDATE $tableName SET $updateSql $whereReturn "; |
526
|
|
|
|
527
|
|
|
$statement = self::getManager()->getConnection()->prepare($sql); |
528
|
|
|
|
529
|
|
|
$result = $statement->execute($attributes); |
530
|
|
|
|
531
|
|
|
if ($showQuery) { |
532
|
|
|
var_dump($sql); |
|
|
|
|
533
|
|
|
var_dump($attributes); |
534
|
|
|
var_dump($whereConditions); |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
if ($result) { |
538
|
|
|
return $statement->rowCount(); |
539
|
|
|
} |
540
|
|
|
} |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
return false; |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
/** |
547
|
|
|
* Experimental useful database finder. |
548
|
|
|
* |
549
|
|
|
* @todo lot of stuff to do here |
550
|
|
|
* @todo known issues, it doesn't work when using LIKE conditions |
551
|
|
|
* |
552
|
|
|
* @example array('where'=> array('course_code LIKE "?%"')) |
553
|
|
|
* @example array('where'=> array('type = ? AND category = ?' => array('setting', 'Plugins')) |
554
|
|
|
* @example array('where'=> array('name = "Julio" AND lastname = "montoya"')) |
555
|
|
|
* |
556
|
|
|
* @param mixed $columns array (or string if only one column) |
557
|
|
|
* @param string $table_name |
558
|
|
|
* @param array $conditions |
559
|
|
|
* @param string $type_result all|first|count |
560
|
|
|
* @param string $option |
561
|
|
|
* @param bool $debug |
562
|
|
|
* |
563
|
|
|
* @return array |
564
|
|
|
*/ |
565
|
|
|
public static function select( |
566
|
|
|
$columns, |
567
|
|
|
$table_name, |
568
|
|
|
$conditions = [], |
569
|
|
|
$type_result = 'all', |
570
|
|
|
$option = 'ASSOC', |
571
|
|
|
$debug = false |
572
|
|
|
) { |
573
|
|
|
if ($type_result === 'count') { |
574
|
|
|
$conditions['LIMIT'] = null; |
575
|
|
|
$conditions['limit'] = null; |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
$conditions = self::parse_conditions($conditions); |
579
|
|
|
|
580
|
|
|
//@todo we could do a describe here to check the columns ... |
581
|
|
|
if (is_array($columns)) { |
582
|
|
|
$clean_columns = implode(',', $columns); |
583
|
|
|
} else { |
584
|
|
|
if ($columns === '*') { |
585
|
|
|
$clean_columns = '*'; |
586
|
|
|
} else { |
587
|
|
|
$clean_columns = (string) $columns; |
588
|
|
|
} |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
if ($type_result === 'count') { |
592
|
|
|
$clean_columns = ' count(*) count '; |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
$sql = "SELECT $clean_columns FROM $table_name $conditions"; |
596
|
|
|
if ($debug) { |
597
|
|
|
var_dump($sql); |
|
|
|
|
598
|
|
|
} |
599
|
|
|
$result = self::query($sql); |
600
|
|
|
|
601
|
|
|
if ($type_result === 'count') { |
602
|
|
|
$row = self::fetch_array($result, $option); |
603
|
|
|
if ($row) { |
604
|
|
|
return (int) $row['count']; |
605
|
|
|
} |
606
|
|
|
|
607
|
|
|
return 0; |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
$array = []; |
611
|
|
|
if ($type_result === 'all') { |
612
|
|
|
while ($row = self::fetch_array($result, $option)) { |
613
|
|
|
if (isset($row['iid'])) { |
614
|
|
|
$array[$row['iid']] = $row; |
615
|
|
|
} elseif (isset($row['id'])) { |
616
|
|
|
$array[$row['id']] = $row; |
617
|
|
|
} else { |
618
|
|
|
$array[] = $row; |
619
|
|
|
} |
620
|
|
|
} |
621
|
|
|
} else { |
622
|
|
|
$array = self::fetch_array($result, $option); |
623
|
|
|
} |
624
|
|
|
|
625
|
|
|
return $array; |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
/** |
629
|
|
|
* Parses WHERE/ORDER conditions i.e array('where'=>array('id = ?' =>'4'), 'order'=>'id DESC'). |
630
|
|
|
* |
631
|
|
|
* @todo known issues, it doesn't work when using |
632
|
|
|
* LIKE conditions example: array('where'=>array('course_code LIKE "?%"')) |
633
|
|
|
* |
634
|
|
|
* @param array $conditions |
635
|
|
|
* |
636
|
|
|
* @return string Partial SQL string to add to longer query |
637
|
|
|
*/ |
638
|
|
|
public static function parse_conditions($conditions) |
639
|
|
|
{ |
640
|
|
|
if (empty($conditions)) { |
641
|
|
|
return ''; |
642
|
|
|
} |
643
|
|
|
$return_value = $where_return = ''; |
644
|
|
|
foreach ($conditions as $type_condition => $condition_data) { |
645
|
|
|
if ($condition_data == false) { |
646
|
|
|
continue; |
647
|
|
|
} |
648
|
|
|
$type_condition = strtolower($type_condition); |
649
|
|
|
switch ($type_condition) { |
650
|
|
|
case 'where': |
651
|
|
|
foreach ($condition_data as $condition => $value_array) { |
652
|
|
|
if (is_array($value_array)) { |
653
|
|
|
$clean_values = []; |
654
|
|
|
foreach ($value_array as $item) { |
655
|
|
|
$item = self::escape_string($item); |
656
|
|
|
$clean_values[] = $item; |
657
|
|
|
} |
658
|
|
|
} else { |
659
|
|
|
$value_array = self::escape_string($value_array); |
660
|
|
|
$clean_values = [$value_array]; |
661
|
|
|
} |
662
|
|
|
|
663
|
|
|
if (!empty($condition) && $clean_values != '') { |
664
|
|
|
$condition = str_replace('%', "'@percentage@'", $condition); //replace "%" |
665
|
|
|
$condition = str_replace("'?'", "%s", $condition); |
666
|
|
|
$condition = str_replace("?", "%s", $condition); |
667
|
|
|
|
668
|
|
|
$condition = str_replace("@%s@", "@-@", $condition); |
669
|
|
|
$condition = str_replace("%s", "'%s'", $condition); |
670
|
|
|
$condition = str_replace("@-@", "@%s@", $condition); |
671
|
|
|
|
672
|
|
|
// Treat conditions as string |
673
|
|
|
$condition = vsprintf($condition, $clean_values); |
674
|
|
|
$condition = str_replace('@percentage@', '%', $condition); //replace "%" |
675
|
|
|
$where_return .= $condition; |
676
|
|
|
} |
677
|
|
|
} |
678
|
|
|
|
679
|
|
|
if (!empty($where_return)) { |
680
|
|
|
$return_value = " WHERE $where_return"; |
681
|
|
|
} |
682
|
|
|
break; |
683
|
|
|
case 'order': |
684
|
|
|
$order_array = $condition_data; |
685
|
|
|
|
686
|
|
|
if (!empty($order_array)) { |
687
|
|
|
// 'order' => 'id desc, name desc' |
688
|
|
|
$order_array = self::escape_string($order_array); |
689
|
|
|
$new_order_array = explode(',', $order_array); |
690
|
|
|
$temp_value = []; |
691
|
|
|
|
692
|
|
|
foreach ($new_order_array as $element) { |
693
|
|
|
$element = explode(' ', $element); |
694
|
|
|
$element = array_filter($element); |
695
|
|
|
$element = array_values($element); |
696
|
|
|
|
697
|
|
|
if (!empty($element[1])) { |
698
|
|
|
$element[1] = strtolower($element[1]); |
699
|
|
|
$order = 'DESC'; |
700
|
|
|
if (in_array($element[1], ['desc', 'asc'])) { |
701
|
|
|
$order = $element[1]; |
702
|
|
|
} |
703
|
|
|
$temp_value[] = ' `'.$element[0].'` '.$order.' '; |
704
|
|
|
} else { |
705
|
|
|
//by default DESC |
706
|
|
|
$temp_value[] = ' `'.$element[0].'` DESC '; |
707
|
|
|
} |
708
|
|
|
} |
709
|
|
|
if (!empty($temp_value)) { |
710
|
|
|
$return_value .= ' ORDER BY '.implode(', ', $temp_value); |
711
|
|
|
} |
712
|
|
|
} |
713
|
|
|
break; |
714
|
|
|
case 'limit': |
715
|
|
|
$limit_array = explode(',', $condition_data); |
716
|
|
|
if (!empty($limit_array)) { |
717
|
|
|
if (count($limit_array) > 1) { |
718
|
|
|
$return_value .= ' LIMIT '.intval($limit_array[0]).' , '.intval($limit_array[1]); |
719
|
|
|
} else { |
720
|
|
|
$return_value .= ' LIMIT '.intval($limit_array[0]); |
721
|
|
|
} |
722
|
|
|
} |
723
|
|
|
break; |
724
|
|
|
} |
725
|
|
|
} |
726
|
|
|
|
727
|
|
|
return $return_value; |
728
|
|
|
} |
729
|
|
|
|
730
|
|
|
/** |
731
|
|
|
* @param array $conditions |
732
|
|
|
* |
733
|
|
|
* @return string |
734
|
|
|
*/ |
735
|
|
|
public static function parse_where_conditions($conditions) |
736
|
|
|
{ |
737
|
|
|
return self::parse_conditions(['where' => $conditions]); |
738
|
|
|
} |
739
|
|
|
|
740
|
|
|
/** |
741
|
|
|
* Build a delete query. |
742
|
|
|
* |
743
|
|
|
* @param string $table_name |
744
|
|
|
* @param array $where_conditions |
745
|
|
|
* @param bool $show_query |
746
|
|
|
* |
747
|
|
|
* @return int |
748
|
|
|
*/ |
749
|
|
|
public static function delete($table_name, $where_conditions, $show_query = false) |
750
|
|
|
{ |
751
|
|
|
$where_return = self::parse_where_conditions($where_conditions); |
752
|
|
|
$sql = "DELETE FROM $table_name $where_return "; |
753
|
|
|
if ($show_query) { |
754
|
|
|
echo $sql; |
755
|
|
|
echo '<br />'; |
756
|
|
|
} |
757
|
|
|
$result = self::query($sql); |
758
|
|
|
$affected_rows = self::affected_rows($result); |
759
|
|
|
//@todo should return affected_rows for |
760
|
|
|
return $affected_rows; |
761
|
|
|
} |
762
|
|
|
|
763
|
|
|
/** |
764
|
|
|
* Get Doctrine configuration. |
765
|
|
|
* |
766
|
|
|
* @param string $path |
767
|
|
|
* |
768
|
|
|
* @return \Doctrine\ORM\Configuration |
769
|
|
|
*/ |
770
|
|
|
public static function getDoctrineConfig($path) |
771
|
|
|
{ |
772
|
|
|
$isDevMode = true; // Forces doctrine to use ArrayCache instead of apc/xcache/memcache/redis |
773
|
|
|
$isSimpleMode = false; // related to annotations @Entity |
774
|
|
|
$cache = null; |
775
|
|
|
$path = !empty($path) ? $path : api_get_path(SYS_PATH); |
776
|
|
|
|
777
|
|
|
$paths = [ |
778
|
|
|
//$path.'src/Chamilo/ClassificationBundle/Entity', |
779
|
|
|
//$path.'src/Chamilo/MediaBundle/Entity', |
780
|
|
|
//$path.'src/Chamilo/PageBundle/Entity', |
781
|
|
|
$path.'src/Chamilo/CoreBundle/Entity', |
782
|
|
|
$path.'src/Chamilo/UserBundle/Entity', |
783
|
|
|
$path.'src/Chamilo/CourseBundle/Entity', |
784
|
|
|
$path.'src/Chamilo/TicketBundle/Entity', |
785
|
|
|
$path.'src/Chamilo/SkillBundle/Entity', |
786
|
|
|
$path.'src/Chamilo/PluginBundle/Entity', |
787
|
|
|
//$path.'vendor/sonata-project/user-bundle/Entity', |
788
|
|
|
//$path.'vendor/sonata-project/user-bundle/Model', |
789
|
|
|
//$path.'vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Entity', |
790
|
|
|
]; |
791
|
|
|
|
792
|
|
|
$proxyDir = $path.'app/cache/'; |
793
|
|
|
|
794
|
|
|
$config = Setup::createAnnotationMetadataConfiguration( |
795
|
|
|
$paths, |
796
|
|
|
$isDevMode, |
797
|
|
|
$proxyDir, |
798
|
|
|
$cache, |
799
|
|
|
$isSimpleMode |
800
|
|
|
); |
801
|
|
|
|
802
|
|
|
$config->setAutoGenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS); |
803
|
|
|
|
804
|
|
|
$config->setEntityNamespaces( |
805
|
|
|
[ |
806
|
|
|
'ChamiloUserBundle' => 'Chamilo\UserBundle\Entity', |
807
|
|
|
'ChamiloCoreBundle' => 'Chamilo\CoreBundle\Entity', |
808
|
|
|
'ChamiloCourseBundle' => 'Chamilo\CourseBundle\Entity', |
809
|
|
|
'ChamiloSkillBundle' => 'Chamilo\SkillBundle\Entity', |
810
|
|
|
'ChamiloTicketBundle' => 'Chamilo\TicketBundle\Entity', |
811
|
|
|
'ChamiloPluginBundle' => 'Chamilo\PluginBundle\Entity', |
812
|
|
|
] |
813
|
|
|
); |
814
|
|
|
|
815
|
|
|
return $config; |
816
|
|
|
} |
817
|
|
|
|
818
|
|
|
/** |
819
|
|
|
* Check if a given table exists. |
820
|
|
|
* |
821
|
|
|
* @param string $table |
822
|
|
|
* |
823
|
|
|
* @return bool |
824
|
|
|
*/ |
825
|
|
|
public static function tableExists($table) |
826
|
|
|
{ |
827
|
|
|
return self::getManager()->getConnection()->getSchemaManager()->tablesExist($table); |
828
|
|
|
} |
829
|
|
|
|
830
|
|
|
/** |
831
|
|
|
* List the columns of a given table. |
832
|
|
|
* |
833
|
|
|
* @param string $table |
834
|
|
|
* |
835
|
|
|
* @return \Doctrine\DBAL\Schema\Column[] |
836
|
|
|
*/ |
837
|
|
|
public static function listTableColumns($table) |
838
|
|
|
{ |
839
|
|
|
return self::getManager()->getConnection()->getSchemaManager()->listTableColumns($table); |
840
|
|
|
} |
841
|
|
|
|
842
|
|
|
public static function escapeField($field) |
843
|
|
|
{ |
844
|
|
|
return self::escape_string(preg_replace("/[^a-zA-Z0-9_.]/", '', $field)); |
845
|
|
|
} |
846
|
|
|
|
847
|
|
|
public static function clearDatabaseName(string $dbName): string |
848
|
|
|
{ |
849
|
|
|
return preg_replace('/[^a-zA-Z0-9_\-]/', '', $dbName); |
850
|
|
|
} |
851
|
|
|
} |
852
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.