Completed
Push — master ( 20b4d3...f28f70 )
by Julito
09:12 queued 12s
created

Database::escape_sql_wildcards()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Doctrine\Common\Annotations\AnnotationRegistry;
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Driver\Statement;
7
use Doctrine\DBAL\Types\Type;
8
use Doctrine\ORM\EntityManager;
9
use Symfony\Component\Debug\ExceptionHandler;
10
11
/**
12
 * Class Database.
13
 */
14
class Database
15
{
16
    /**
17
     * @var EntityManager
18
     */
19
    private static $em;
20
    private static $connection;
21
22
    /**
23
     * Only used by the installer.
24
     *
25
     * @param array  $params
26
     * @param string $entityRootPath
27
     *
28
     * @throws \Doctrine\ORM\ORMException
29
     */
30
    public function connect(
31
        $params = [],
32
        $entityRootPath = ''
33
    ) {
34
        $config = self::getDoctrineConfig($entityRootPath);
35
        $config->setAutoGenerateProxyClasses(true);
36
37
        $config->setEntityNamespaces(
38
            [
39
                'ChamiloUserBundle' => 'Chamilo\UserBundle\Entity',
40
                'ChamiloCoreBundle' => 'Chamilo\CoreBundle\Entity',
41
                'ChamiloCourseBundle' => 'Chamilo\CourseBundle\Entity',
42
                'ChamiloSkillBundle' => 'Chamilo\SkillBundle\Entity',
43
                'ChamiloTicketBundle' => 'Chamilo\TicketBundle\Entity',
44
                'ChamiloPluginBundle' => 'Chamilo\PluginBundle\Entity',
45
            ]
46
        );
47
48
        $params['charset'] = 'utf8';
49
        $entityManager = EntityManager::create($params, $config);
50
        $connection = $entityManager->getConnection();
51
52
        $sysPath = !empty($sysPath) ? $sysPath : api_get_path(SYS_PATH);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sysPath seems to never exist and therefore empty should always be true.
Loading history...
53
        AnnotationRegistry::registerFile(
54
            $sysPath."vendor/symfony/doctrine-bridge/Validator/Constraints/UniqueEntity.php"
55
        );
56
57
        // Registering gedmo extensions
58
        AnnotationRegistry::registerAutoloadNamespace(
59
            'Gedmo\Mapping\Annotation',
60
            $sysPath."vendor/gedmo/doctrine-extensions/lib"
61
        );
62
63
        $this->setConnection($connection);
64
        $this->setManager($entityManager);
65
    }
66
67
    /**
68
     * @param EntityManager $em
69
     */
70
    public static function setManager($em)
71
    {
72
        self::$em = $em;
73
    }
74
75
    /**
76
     * @param Connection $connection
77
     */
78
    public static function setConnection(Connection $connection)
79
    {
80
        self::$connection = $connection;
81
    }
82
83
    /**
84
     * @return Connection
85
     */
86
    public static function getConnection()
87
    {
88
        return self::$connection;
89
    }
90
91
    /**
92
     * @return EntityManager
93
     */
94
    public static function getManager()
95
    {
96
        return self::$em;
97
    }
98
99
    /**
100
     * Returns the name of the main database.
101
     *
102
     * @return string
103
     */
104
    public static function get_main_database()
105
    {
106
        return self::getManager()->getConnection()->getDatabase();
107
    }
108
109
    /**
110
     * Get main table.
111
     *
112
     * @param string $table
113
     *
114
     * @return string
115
     */
116
    public static function get_main_table($table)
117
    {
118
        return $table;
119
    }
120
121
    /**
122
     * Get course table.
123
     *
124
     * @param string $table
125
     *
126
     * @return string
127
     */
128
    public static function get_course_table($table)
129
    {
130
        return DB_COURSE_PREFIX.$table;
131
    }
132
133
    /**
134
     * Counts the number of rows in a table.
135
     *
136
     * @param string $table The table of which the rows should be counted
137
     *
138
     * @return int the number of rows in the given table
139
     *
140
     * @deprecated
141
     */
142
    public static function count_rows($table)
143
    {
144
        $obj = self::fetch_object(self::query("SELECT COUNT(*) AS n FROM $table"));
145
146
        return $obj->n;
147
    }
148
149
    /**
150
     * Returns the number of affected rows in the last database operation.
151
     *
152
     * @param Statement $result
153
     *
154
     * @return int
155
     */
156
    public static function affected_rows(Statement $result)
157
    {
158
        return $result->rowCount();
159
    }
160
161
    /**
162
     * Escapes a string to insert into the database as text.
163
     *
164
     * @param string $string
165
     *
166
     * @return string
167
     */
168
    public static function escape_string($string)
169
    {
170
        $string = self::getManager()->getConnection()->quote($string);
171
        // The quote method from PDO also adds quotes around the string, which
172
        // is not how the legacy mysql_real_escape_string() was used in
173
        // Chamilo, so we need to remove the quotes around. Using trim will
174
        // remove more than one quote if they are sequenced, generating
175
        // broken queries and SQL injection risks
176
        return substr($string, 1, -1);
177
    }
178
179
    /**
180
     * Gets the array from a SQL result (as returned by Database::query).
181
     *
182
     * @param Statement $result
183
     * @param string    $option Optional: "ASSOC","NUM" or "BOTH"
184
     *
185
     * @return array|mixed
186
     */
187
    public static function fetch_array(Statement $result, $option = 'BOTH')
188
    {
189
        if ($result === false) {
190
            return [];
191
        }
192
193
        return $result->fetch(self::customOptionToDoctrineOption($option));
194
    }
195
196
    /**
197
     * Gets an associative array from a SQL result (as returned by Database::query).
198
     *
199
     * @param Statement $result
200
     *
201
     * @return array
202
     */
203
    public static function fetch_assoc(Statement $result)
204
    {
205
        return $result->fetch(PDO::FETCH_ASSOC);
206
    }
207
208
    /**
209
     * Gets the next row of the result of the SQL query
210
     * (as returned by Database::query) in an object form.
211
     *
212
     * @param Statement $result
213
     *
214
     * @return mixed
215
     */
216
    public static function fetch_object(Statement $result)
217
    {
218
        return $result->fetch(PDO::FETCH_OBJ);
219
    }
220
221
    /**
222
     * Gets the array from a SQL result (as returned by Database::query)
223
     * help achieving database independence.
224
     *
225
     * @param Statement $result
226
     *
227
     * @return mixed
228
     */
229
    public static function fetch_row(Statement $result)
230
    {
231
        if ($result === false) {
232
            return [];
233
        }
234
235
        return $result->fetch(PDO::FETCH_NUM);
236
    }
237
238
    /**
239
     * Gets the ID of the last item inserted into the database.
240
     *
241
     * @return string
242
     */
243
    public static function insert_id()
244
    {
245
        return self::getManager()->getConnection()->lastInsertId();
246
    }
247
248
    /**
249
     * @param Statement $result
250
     *
251
     * @return int
252
     */
253
    public static function num_rows(Statement $result)
254
    {
255
        if ($result === false) {
256
            return 0;
257
        }
258
259
        return $result->rowCount();
260
    }
261
262
    /**
263
     * Acts as the relative *_result() function of most DB drivers and fetches a
264
     * specific line and a field.
265
     *
266
     * @param Statement $resource
267
     * @param int       $row
268
     * @param string    $field
269
     *
270
     * @return mixed
271
     */
272
    public static function result(Statement $resource, $row, $field = '')
273
    {
274
        if ($resource->rowCount() > 0) {
275
            $result = $resource->fetchAll(PDO::FETCH_BOTH);
276
277
            return $result[$row][$field];
278
        }
279
280
        return false;
281
    }
282
283
    /**
284
     * @param string $query
285
     *
286
     * @return Statement
287
     */
288
    public static function query($query)
289
    {
290
        $connection = self::getManager()->getConnection();
291
        $result = null;
292
        try {
293
            $result = $connection->executeQuery($query);
294
        } catch (Exception $e) {
295
            self::handleError($e);
296
        }
297
298
        return $result;
299
    }
300
301
    /**
302
     * @param Exception $e
303
     */
304
    public static function handleError($e)
305
    {
306
        $debug = api_get_setting('server_type') == 'test';
307
        if ($debug) {
308
            // We use Symfony exception handler for better error information
309
            $handler = new ExceptionHandler();
310
            $handler->handle($e);
311
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
312
        } else {
313
            error_log($e->getMessage());
314
            api_not_allowed(false, get_lang('GeneralError'));
315
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
316
        }
317
    }
318
319
    /**
320
     * @param string $option
321
     *
322
     * @return int
323
     */
324
    public static function customOptionToDoctrineOption($option)
325
    {
326
        switch ($option) {
327
            case 'ASSOC':
328
                return PDO::FETCH_ASSOC;
329
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
330
            case 'NUM':
331
                return PDO::FETCH_NUM;
332
                break;
333
            case 'BOTH':
334
            default:
335
                return PDO::FETCH_BOTH;
336
                break;
337
        }
338
    }
339
340
    /**
341
     * Stores a query result into an array.
342
     *
343
     * @author Olivier Brouckaert
344
     *
345
     * @param Statement $result - the return value of the query
346
     * @param string    $option BOTH, ASSOC, or NUM
347
     *
348
     * @return array - the value returned by the query
349
     */
350
    public static function store_result(Statement $result, $option = 'BOTH')
351
    {
352
        return $result->fetchAll(self::customOptionToDoctrineOption($option));
353
    }
354
355
    /**
356
     * Database insert.
357
     *
358
     * @param string $table_name
359
     * @param array  $attributes
360
     * @param bool   $show_query
361
     *
362
     * @return false|int
363
     */
364
    public static function insert($table_name, $attributes, $show_query = false)
365
    {
366
        if (empty($attributes) || empty($table_name)) {
367
            return false;
368
        }
369
370
        $params = array_keys($attributes);
371
372
        if (!empty($params)) {
373
            $sql = 'INSERT INTO '.$table_name.' ('.implode(',', $params).')
374
                    VALUES (:'.implode(', :', $params).')';
375
376
            if ($show_query) {
377
                var_dump($sql);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($sql) looks like debug code. Are you sure you do not want to remove it?
Loading history...
378
                error_log($sql);
379
            }
380
381
            $result = false;
382
            try {
383
                $statement = self::getConnection()->prepare($sql);
384
                $result = $statement->execute($attributes);
385
            } catch (Exception $e) {
386
                self::handleError($e);
387
            }
388
389
            if ($result) {
390
                return (int) self::getManager()->getConnection()->lastInsertId();
391
            }
392
        }
393
394
        return false;
395
    }
396
397
    /**
398
     * @param string $tableName       use Database::get_main_table
399
     * @param array  $attributes      Values to updates
400
     *                                Example: $params['name'] = 'Julio'; $params['lastname'] = 'Montoya';
401
     * @param array  $whereConditions where conditions i.e array('id = ?' =>'4')
402
     * @param bool   $showQuery
403
     *
404
     * @return bool|int
405
     */
406
    public static function update(
407
        $tableName,
408
        $attributes,
409
        $whereConditions = [],
410
        $showQuery = false
411
    ) {
412
        if (!empty($tableName) && !empty($attributes)) {
413
            $updateSql = '';
414
            $count = 1;
415
416
            foreach ($attributes as $key => $value) {
417
                if ($showQuery) {
418
                    echo $key.': '.$value.PHP_EOL;
419
                }
420
                $updateSql .= "$key = :$key ";
421
                if ($count < count($attributes)) {
422
                    $updateSql .= ', ';
423
                }
424
                $count++;
425
            }
426
427
            if (!empty($updateSql)) {
428
                // Parsing and cleaning the where conditions
429
                $whereReturn = self::parse_where_conditions($whereConditions);
430
                $sql = "UPDATE $tableName SET $updateSql $whereReturn ";
431
432
                try {
433
                    $statement = self::getManager()->getConnection()->prepare($sql);
434
                    $result = $statement->execute($attributes);
435
                } catch (Exception $e) {
436
                    self::handleError($e);
437
                }
438
439
                if ($showQuery) {
440
                    var_dump($sql);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($sql) looks like debug code. Are you sure you do not want to remove it?
Loading history...
441
                    var_dump($attributes);
442
                    var_dump($whereConditions);
443
                }
444
445
                if ($result && $statement) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.
Loading history...
446
                    return $statement->rowCount();
447
                }
448
            }
449
        }
450
451
        return false;
452
    }
453
454
    /**
455
     * Experimental useful database finder.
456
     *
457
     * @todo lot of stuff to do here
458
     * @todo known issues, it doesn't work when using LIKE conditions
459
     *
460
     * @example array('where'=> array('course_code LIKE "?%"'))
461
     * @example array('where'=> array('type = ? AND category = ?' => array('setting', 'Plugins'))
462
     * @example array('where'=> array('name = "Julio" AND lastname = "montoya"'))
463
     *
464
     * @param array  $columns
465
     * @param string $table_name
466
     * @param array  $conditions
467
     * @param string $type_result
468
     * @param string $option
469
     * @param bool   $debug
470
     *
471
     * @return array
472
     */
473
    public static function select(
474
        $columns,
475
        $table_name,
476
        $conditions = [],
477
        $type_result = 'all',
478
        $option = 'ASSOC',
479
        $debug = false
480
    ) {
481
        $conditions = self::parse_conditions($conditions);
482
483
        //@todo we could do a describe here to check the columns ...
484
        if (is_array($columns)) {
485
            $clean_columns = implode(',', $columns);
486
        } else {
487
            if ($columns == '*') {
488
                $clean_columns = '*';
489
            } else {
490
                $clean_columns = (string) $columns;
491
            }
492
        }
493
494
        $sql = "SELECT $clean_columns FROM $table_name $conditions";
495
        if ($debug) {
496
            var_dump($sql);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($sql) looks like debug code. Are you sure you do not want to remove it?
Loading history...
497
        }
498
        $result = self::query($sql);
499
        $array = [];
500
501
        if ($type_result === 'all') {
502
            while ($row = self::fetch_array($result, $option)) {
503
                if (isset($row['id'])) {
504
                    $array[$row['id']] = $row;
505
                } else {
506
                    $array[] = $row;
507
                }
508
            }
509
        } else {
510
            $array = self::fetch_array($result, $option);
511
        }
512
513
        return $array;
514
    }
515
516
    /**
517
     * Parses WHERE/ORDER conditions i.e array('where'=>array('id = ?' =>'4'), 'order'=>'id DESC').
518
     *
519
     * @todo known issues, it doesn't work when using
520
     * LIKE conditions example: array('where'=>array('course_code LIKE "?%"'))
521
     *
522
     * @param array $conditions
523
     *
524
     * @return string Partial SQL string to add to longer query
525
     */
526
    public static function parse_conditions($conditions)
527
    {
528
        if (empty($conditions)) {
529
            return '';
530
        }
531
        $return_value = $where_return = '';
532
        foreach ($conditions as $type_condition => $condition_data) {
533
            if ($condition_data == false) {
534
                continue;
535
            }
536
            $type_condition = strtolower($type_condition);
537
            switch ($type_condition) {
538
                case 'where':
539
                    foreach ($condition_data as $condition => $value_array) {
540
                        if (is_array($value_array)) {
541
                            $clean_values = [];
542
                            foreach ($value_array as $item) {
543
                                $item = self::escape_string($item);
544
                                $clean_values[] = $item;
545
                            }
546
                        } else {
547
                            $value_array = self::escape_string($value_array);
548
                            $clean_values = $value_array;
549
                        }
550
551
                        if (!empty($condition) && $clean_values != '') {
552
                            $condition = str_replace('%', "'@percentage@'", $condition); //replace "%"
553
                            $condition = str_replace("'?'", "%s", $condition);
554
                            $condition = str_replace("?", "%s", $condition);
555
556
                            $condition = str_replace("@%s@", "@-@", $condition);
557
                            $condition = str_replace("%s", "'%s'", $condition);
558
                            $condition = str_replace("@-@", "@%s@", $condition);
559
560
                            // Treat conditions as string
561
                            $condition = vsprintf($condition, $clean_values);
0 ignored issues
show
Bug introduced by
It seems like $clean_values can also be of type string; however, parameter $args of vsprintf() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

561
                            $condition = vsprintf($condition, /** @scrutinizer ignore-type */ $clean_values);
Loading history...
562
                            $condition = str_replace('@percentage@', '%', $condition); //replace "%"
563
                            $where_return .= $condition;
564
                        }
565
                    }
566
567
                    if (!empty($where_return)) {
568
                        $return_value = " WHERE $where_return";
569
                    }
570
                    break;
571
                case 'order':
572
                    $order_array = $condition_data;
573
574
                    if (!empty($order_array)) {
575
                        // 'order' => 'id desc, name desc'
576
                        $order_array = self::escape_string($order_array, null, false);
577
                        $new_order_array = explode(',', $order_array);
578
                        $temp_value = [];
579
580
                        foreach ($new_order_array as $element) {
581
                            $element = explode(' ', $element);
582
                            $element = array_filter($element);
583
                            $element = array_values($element);
584
585
                            if (!empty($element[1])) {
586
                                $element[1] = strtolower($element[1]);
587
                                $order = 'DESC';
588
                                if (in_array($element[1], ['desc', 'asc'])) {
589
                                    $order = $element[1];
590
                                }
591
                                $temp_value[] = $element[0].' '.$order.' ';
592
                            } else {
593
                                //by default DESC
594
                                $temp_value[] = $element[0].' DESC ';
595
                            }
596
                        }
597
                        if (!empty($temp_value)) {
598
                            $return_value .= ' ORDER BY '.implode(', ', $temp_value);
599
                        }
600
                    }
601
                    break;
602
                case 'limit':
603
                    $limit_array = explode(',', $condition_data);
604
                    if (!empty($limit_array)) {
605
                        if (count($limit_array) > 1) {
606
                            $return_value .= ' LIMIT '.intval($limit_array[0]).' , '.intval($limit_array[1]);
607
                        } else {
608
                            $return_value .= ' LIMIT '.intval($limit_array[0]);
609
                        }
610
                    }
611
                    break;
612
            }
613
        }
614
615
        return $return_value;
616
    }
617
618
    /**
619
     * @param array $conditions
620
     *
621
     * @return string
622
     */
623
    public static function parse_where_conditions($conditions)
624
    {
625
        return self::parse_conditions(['where' => $conditions]);
626
    }
627
628
    /**
629
     * @param string $table_name
630
     * @param array  $where_conditions
631
     * @param bool   $show_query
632
     *
633
     * @return int
634
     */
635
    public static function delete($table_name, $where_conditions, $show_query = false)
636
    {
637
        $where_return = self::parse_where_conditions($where_conditions);
638
        $sql = "DELETE FROM $table_name $where_return ";
639
        if ($show_query) {
640
            echo $sql;
641
            echo '<br />';
642
        }
643
        $result = self::query($sql);
644
        $affected_rows = self::affected_rows($result);
645
        //@todo should return affected_rows for
646
        return $affected_rows;
647
    }
648
649
    /**
650
     * Get Doctrine configuration.
651
     *
652
     * @param string $path
653
     *
654
     * @return \Doctrine\ORM\Configuration
655
     */
656
    public static function getDoctrineConfig($path)
657
    {
658
        $isDevMode = true; // Forces doctrine to use ArrayCache instead of apc/xcache/memcache/redis
659
        $isSimpleMode = false; // related to annotations @Entity
660
        $cache = null;
661
        $path = !empty($path) ? $path : api_get_path(SYS_PATH);
662
663
        $paths = [
664
            //$path.'src/Chamilo/ClassificationBundle/Entity',
665
            //$path.'src/Chamilo/MediaBundle/Entity',
666
            //$path.'src/Chamilo/PageBundle/Entity',
667
            $path.'src/Chamilo/CoreBundle/Entity',
668
            $path.'src/Chamilo/UserBundle/Entity',
669
            $path.'src/Chamilo/CourseBundle/Entity',
670
            $path.'src/Chamilo/TicketBundle/Entity',
671
            $path.'src/Chamilo/SkillBundle/Entity',
672
            $path.'src/Chamilo/PluginBundle/Entity',
673
            //$path.'vendor/sonata-project/user-bundle/Entity',
674
            //$path.'vendor/sonata-project/user-bundle/Model',
675
            //$path.'vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Entity',
676
        ];
677
678
        $proxyDir = $path.'var/cache/';
679
680
        $config = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(
681
            $paths,
682
            $isDevMode,
683
            $proxyDir,
684
            $cache,
685
            $isSimpleMode
686
        );
687
688
        return $config;
689
    }
690
691
    /**
692
     * @param string $table
693
     *
694
     * @return bool
695
     */
696
    public static function tableExists($table)
697
    {
698
        return self::getManager()->getConnection()->getSchemaManager()->tablesExist($table);
699
    }
700
701
    /**
702
     * @param string $table
703
     *
704
     * @return \Doctrine\DBAL\Schema\Column[]
705
     */
706
    public static function listTableColumns($table)
707
    {
708
        return self::getManager()->getConnection()->getSchemaManager()->listTableColumns($table);
709
    }
710
}
711