database.lib.php 23 KB

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