database.lib.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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. ]
  142. );
  143. $params['charset'] = 'utf8';
  144. $entityManager = EntityManager::create($params, $config);
  145. $sysPath = !empty($sysPath) ? $sysPath : api_get_path(SYS_PATH);
  146. // Registering Constraints
  147. /*AnnotationRegistry::registerAutoloadNamespace(
  148. 'Symfony\Component',
  149. $sysPath."vendor/"
  150. );*/
  151. AnnotationRegistry::registerLoader(
  152. function ($class) use ($sysPath) {
  153. $file = str_replace("\\", DIRECTORY_SEPARATOR, $class).".php";
  154. $file = str_replace('Symfony/Component/Validator', '', $file);
  155. $file = str_replace('Symfony\Component\Validator', '', $file);
  156. $fileToInclude = $sysPath.'vendor/symfony/validator/'.$file;
  157. if (file_exists($fileToInclude)) {
  158. // file exists makes sure that the loader fails silently
  159. require_once $fileToInclude;
  160. return true;
  161. }
  162. $fileToInclude = $sysPath.'vendor/symfony/validator/Constraints/'.$file;
  163. if (file_exists($fileToInclude)) {
  164. // file exists makes sure that the loader fails silently
  165. require_once $fileToInclude;
  166. return true;
  167. }
  168. }
  169. );
  170. AnnotationRegistry::registerFile(
  171. $sysPath."vendor/symfony/doctrine-bridge/Validator/Constraints/UniqueEntity.php"
  172. );
  173. // Registering gedmo extensions
  174. AnnotationRegistry::registerAutoloadNamespace(
  175. 'Gedmo\Mapping\Annotation',
  176. $sysPath."vendor/gedmo/doctrine-extensions/lib"
  177. );
  178. Type::overrideType(
  179. Type::DATETIME,
  180. self::getUTCDateTimeTypeClass()
  181. );
  182. $listener = new \Gedmo\Timestampable\TimestampableListener();
  183. $entityManager->getEventManager()->addEventSubscriber($listener);
  184. $listener = new \Gedmo\Tree\TreeListener();
  185. $entityManager->getEventManager()->addEventSubscriber($listener);
  186. $listener = new \Gedmo\Sortable\SortableListener();
  187. $entityManager->getEventManager()->addEventSubscriber($listener);
  188. $connection = $entityManager->getConnection();
  189. $connection->executeQuery('SET sql_mode = "";');
  190. if ($returnConnection) {
  191. return $connection;
  192. }
  193. if ($returnManager) {
  194. return $entityManager;
  195. }
  196. $this->setConnection($connection);
  197. $this->setManager($entityManager);
  198. }
  199. /**
  200. * Escape MySQL wildchars _ and % in LIKE search.
  201. *
  202. * @param string $text The string to escape
  203. *
  204. * @return string The escaped string
  205. */
  206. public static function escape_sql_wildcards($text)
  207. {
  208. $text = api_preg_replace("/_/", "\_", $text);
  209. $text = api_preg_replace("/%/", "\%", $text);
  210. return $text;
  211. }
  212. /**
  213. * Escapes a string to insert into the database as text.
  214. *
  215. * @param string $string
  216. *
  217. * @return string
  218. */
  219. public static function escape_string($string)
  220. {
  221. $string = self::getManager()->getConnection()->quote($string);
  222. // The quote method from PDO also adds quotes around the string, which
  223. // is not how the legacy mysql_real_escape_string() was used in
  224. // Chamilo, so we need to remove the quotes around. Using trim will
  225. // remove more than one quote if they are sequenced, generating
  226. // broken queries and SQL injection risks
  227. return substr($string, 1, -1);
  228. }
  229. /**
  230. * Gets the array from a SQL result (as returned by Database::query).
  231. *
  232. * @param Statement $result
  233. * @param string $option Optional: "ASSOC","NUM" or "BOTH"
  234. *
  235. * @return array|mixed
  236. */
  237. public static function fetch_array(Statement $result, $option = 'BOTH')
  238. {
  239. if ($result === false) {
  240. return [];
  241. }
  242. return $result->fetch(self::customOptionToDoctrineOption($option));
  243. }
  244. /**
  245. * Gets an associative array from a SQL result (as returned by Database::query).
  246. *
  247. * @param Statement $result
  248. *
  249. * @return array
  250. */
  251. public static function fetch_assoc(Statement $result)
  252. {
  253. return $result->fetch(PDO::FETCH_ASSOC);
  254. }
  255. /**
  256. * Gets the next row of the result of the SQL query
  257. * (as returned by Database::query) in an object form.
  258. *
  259. * @param Statement $result
  260. *
  261. * @return mixed
  262. */
  263. public static function fetch_object(Statement $result)
  264. {
  265. return $result->fetch(PDO::FETCH_OBJ);
  266. }
  267. /**
  268. * Gets the array from a SQL result (as returned by Database::query)
  269. * help achieving database independence.
  270. *
  271. * @param Statement $result
  272. *
  273. * @return mixed
  274. */
  275. public static function fetch_row(Statement $result)
  276. {
  277. if ($result === false) {
  278. return [];
  279. }
  280. return $result->fetch(PDO::FETCH_NUM);
  281. }
  282. /**
  283. * Frees all the memory associated with the provided result identifier.
  284. *
  285. * @return bool|null Returns TRUE on success or FALSE on failure.
  286. * Notes: Use this method if you are concerned about how much memory is being used for queries that return large result sets.
  287. * Anyway, all associated result memory is automatically freed at the end of the script's execution.
  288. */
  289. public static function free_result(Statement $result)
  290. {
  291. $result->closeCursor();
  292. }
  293. /**
  294. * Gets the ID of the last item inserted into the database.
  295. *
  296. * @return string
  297. */
  298. public static function insert_id()
  299. {
  300. return self::getManager()->getConnection()->lastInsertId();
  301. }
  302. /**
  303. * @param Statement $result
  304. *
  305. * @return int
  306. */
  307. public static function num_rows(Statement $result)
  308. {
  309. if ($result === false) {
  310. return 0;
  311. }
  312. return $result->rowCount();
  313. }
  314. /**
  315. * Acts as the relative *_result() function of most DB drivers and fetches a
  316. * specific line and a field.
  317. *
  318. * @param Statement $resource
  319. * @param int $row
  320. * @param string $field
  321. *
  322. * @return mixed
  323. */
  324. public static function result(Statement $resource, $row, $field = '')
  325. {
  326. if ($resource->rowCount() > 0) {
  327. $result = $resource->fetchAll(PDO::FETCH_BOTH);
  328. return $result[$row][$field];
  329. }
  330. }
  331. /**
  332. * @param string $query
  333. *
  334. * @return Statement
  335. */
  336. public static function query($query)
  337. {
  338. $connection = self::getManager()->getConnection();
  339. $result = null;
  340. try {
  341. $result = $connection->executeQuery($query);
  342. } catch (Exception $e) {
  343. $debug = api_get_setting('server_type') == 'test';
  344. if ($debug) {
  345. // We use Symfony exception handler for better error information
  346. $handler = new ExceptionHandler();
  347. $handler->handle($e);
  348. exit;
  349. } else {
  350. error_log($e->getMessage());
  351. api_not_allowed(false, get_lang('GeneralError'));
  352. }
  353. }
  354. return $result;
  355. }
  356. /**
  357. * @param string $option
  358. *
  359. * @return int
  360. */
  361. public static function customOptionToDoctrineOption($option)
  362. {
  363. switch ($option) {
  364. case 'ASSOC':
  365. return PDO::FETCH_ASSOC;
  366. break;
  367. case 'NUM':
  368. return PDO::FETCH_NUM;
  369. break;
  370. case 'BOTH':
  371. default:
  372. return PDO::FETCH_BOTH;
  373. break;
  374. }
  375. }
  376. /**
  377. * Stores a query result into an array.
  378. *
  379. * @author Olivier Brouckaert
  380. *
  381. * @param Statement $result - the return value of the query
  382. * @param string $option BOTH, ASSOC, or NUM
  383. *
  384. * @return array - the value returned by the query
  385. */
  386. public static function store_result(Statement $result, $option = 'BOTH')
  387. {
  388. return $result->fetchAll(self::customOptionToDoctrineOption($option));
  389. }
  390. /**
  391. * Database insert.
  392. *
  393. * @param string $table_name
  394. * @param array $attributes
  395. * @param bool $show_query
  396. *
  397. * @return false|int
  398. */
  399. public static function insert($table_name, $attributes, $show_query = false)
  400. {
  401. if (empty($attributes) || empty($table_name)) {
  402. return false;
  403. }
  404. $params = array_keys($attributes);
  405. if (!empty($params)) {
  406. $sql = 'INSERT INTO '.$table_name.' ('.implode(',', $params).')
  407. VALUES (:'.implode(', :', $params).')';
  408. $statement = self::getManager()->getConnection()->prepare($sql);
  409. $result = $statement->execute($attributes);
  410. if ($show_query) {
  411. var_dump($sql);
  412. error_log($sql);
  413. }
  414. if ($result) {
  415. return (int) self::getManager()->getConnection()->lastInsertId();
  416. }
  417. }
  418. return false;
  419. }
  420. /**
  421. * @param string $tableName use Database::get_main_table
  422. * @param array $attributes Values to updates
  423. * Example: $params['name'] = 'Julio'; $params['lastname'] = 'Montoya';
  424. * @param array $whereConditions where conditions i.e array('id = ?' =>'4')
  425. * @param bool $showQuery
  426. *
  427. * @return bool|int
  428. */
  429. public static function update(
  430. $tableName,
  431. $attributes,
  432. $whereConditions = [],
  433. $showQuery = false
  434. ) {
  435. if (!empty($tableName) && !empty($attributes)) {
  436. $updateSql = '';
  437. $count = 1;
  438. foreach ($attributes as $key => $value) {
  439. if ($showQuery) {
  440. echo $key.': '.$value.PHP_EOL;
  441. }
  442. $updateSql .= "$key = :$key ";
  443. if ($count < count($attributes)) {
  444. $updateSql .= ', ';
  445. }
  446. $count++;
  447. }
  448. if (!empty($updateSql)) {
  449. //Parsing and cleaning the where conditions
  450. $whereReturn = self::parse_where_conditions($whereConditions);
  451. $sql = "UPDATE $tableName SET $updateSql $whereReturn ";
  452. $statement = self::getManager()->getConnection()->prepare($sql);
  453. $result = $statement->execute($attributes);
  454. if ($showQuery) {
  455. var_dump($sql);
  456. var_dump($attributes);
  457. var_dump($whereConditions);
  458. }
  459. if ($result) {
  460. return $statement->rowCount();
  461. }
  462. }
  463. }
  464. return false;
  465. }
  466. /**
  467. * Experimental useful database finder.
  468. *
  469. * @todo lot of stuff to do here
  470. * @todo known issues, it doesn't work when using LIKE conditions
  471. *
  472. * @example array('where'=> array('course_code LIKE "?%"'))
  473. * @example array('where'=> array('type = ? AND category = ?' => array('setting', 'Plugins'))
  474. * @example array('where'=> array('name = "Julio" AND lastname = "montoya"'))
  475. *
  476. * @param array $columns
  477. * @param string $table_name
  478. * @param array $conditions
  479. * @param string $type_result
  480. * @param string $option
  481. * @param bool $debug
  482. *
  483. * @return array
  484. */
  485. public static function select(
  486. $columns,
  487. $table_name,
  488. $conditions = [],
  489. $type_result = 'all',
  490. $option = 'ASSOC',
  491. $debug = false
  492. ) {
  493. $conditions = self::parse_conditions($conditions);
  494. //@todo we could do a describe here to check the columns ...
  495. if (is_array($columns)) {
  496. $clean_columns = implode(',', $columns);
  497. } else {
  498. if ($columns == '*') {
  499. $clean_columns = '*';
  500. } else {
  501. $clean_columns = (string) $columns;
  502. }
  503. }
  504. $sql = "SELECT $clean_columns FROM $table_name $conditions";
  505. if ($debug) {
  506. var_dump($sql);
  507. }
  508. $result = self::query($sql);
  509. $array = [];
  510. if ($type_result === 'all') {
  511. while ($row = self::fetch_array($result, $option)) {
  512. if (isset($row['id'])) {
  513. $array[$row['id']] = $row;
  514. } else {
  515. $array[] = $row;
  516. }
  517. }
  518. } else {
  519. $array = self::fetch_array($result, $option);
  520. }
  521. return $array;
  522. }
  523. /**
  524. * Parses WHERE/ORDER conditions i.e array('where'=>array('id = ?' =>'4'), 'order'=>'id DESC').
  525. *
  526. * @todo known issues, it doesn't work when using
  527. * LIKE conditions example: array('where'=>array('course_code LIKE "?%"'))
  528. *
  529. * @param array $conditions
  530. *
  531. * @return string Partial SQL string to add to longer query
  532. */
  533. public static function parse_conditions($conditions)
  534. {
  535. if (empty($conditions)) {
  536. return '';
  537. }
  538. $return_value = $where_return = '';
  539. foreach ($conditions as $type_condition => $condition_data) {
  540. if ($condition_data == false) {
  541. continue;
  542. }
  543. $type_condition = strtolower($type_condition);
  544. switch ($type_condition) {
  545. case 'where':
  546. foreach ($condition_data as $condition => $value_array) {
  547. if (is_array($value_array)) {
  548. $clean_values = [];
  549. foreach ($value_array as $item) {
  550. $item = self::escape_string($item);
  551. $clean_values[] = $item;
  552. }
  553. } else {
  554. $value_array = self::escape_string($value_array);
  555. $clean_values = $value_array;
  556. }
  557. if (!empty($condition) && $clean_values != '') {
  558. $condition = str_replace('%', "'@percentage@'", $condition); //replace "%"
  559. $condition = str_replace("'?'", "%s", $condition);
  560. $condition = str_replace("?", "%s", $condition);
  561. $condition = str_replace("@%s@", "@-@", $condition);
  562. $condition = str_replace("%s", "'%s'", $condition);
  563. $condition = str_replace("@-@", "@%s@", $condition);
  564. // Treat conditions as string
  565. $condition = vsprintf($condition, $clean_values);
  566. $condition = str_replace('@percentage@', '%', $condition); //replace "%"
  567. $where_return .= $condition;
  568. }
  569. }
  570. if (!empty($where_return)) {
  571. $return_value = " WHERE $where_return";
  572. }
  573. break;
  574. case 'order':
  575. $order_array = $condition_data;
  576. if (!empty($order_array)) {
  577. // 'order' => 'id desc, name desc'
  578. $order_array = self::escape_string($order_array, null, false);
  579. $new_order_array = explode(',', $order_array);
  580. $temp_value = [];
  581. foreach ($new_order_array as $element) {
  582. $element = explode(' ', $element);
  583. $element = array_filter($element);
  584. $element = array_values($element);
  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. return $return_value;
  615. }
  616. /**
  617. * @param array $conditions
  618. *
  619. * @return string
  620. */
  621. public static function parse_where_conditions($conditions)
  622. {
  623. return self::parse_conditions(['where' => $conditions]);
  624. }
  625. /**
  626. * @param string $table_name
  627. * @param array $where_conditions
  628. * @param bool $show_query
  629. *
  630. * @return int
  631. */
  632. public static function delete($table_name, $where_conditions, $show_query = false)
  633. {
  634. $where_return = self::parse_where_conditions($where_conditions);
  635. $sql = "DELETE FROM $table_name $where_return ";
  636. if ($show_query) {
  637. echo $sql;
  638. echo '<br />';
  639. }
  640. $result = self::query($sql);
  641. $affected_rows = self::affected_rows($result);
  642. //@todo should return affected_rows for
  643. return $affected_rows;
  644. }
  645. /**
  646. * Get Doctrine configuration.
  647. *
  648. * @param string $path
  649. *
  650. * @return \Doctrine\ORM\Configuration
  651. */
  652. public static function getDoctrineConfig($path)
  653. {
  654. $isDevMode = true; // Forces doctrine to use ArrayCache instead of apc/xcache/memcache/redis
  655. $isSimpleMode = false; // related to annotations @Entity
  656. $cache = null;
  657. $path = !empty($path) ? $path : api_get_path(SYS_PATH);
  658. $paths = [
  659. //$path.'src/Chamilo/ClassificationBundle/Entity',
  660. //$path.'src/Chamilo/MediaBundle/Entity',
  661. //$path.'src/Chamilo/PageBundle/Entity',
  662. $path.'src/Chamilo/CoreBundle/Entity',
  663. $path.'src/Chamilo/UserBundle/Entity',
  664. $path.'src/Chamilo/CourseBundle/Entity',
  665. $path.'src/Chamilo/TicketBundle/Entity',
  666. $path.'src/Chamilo/SkillBundle/Entity',
  667. $path.'src/Chamilo/PluginBundle/Entity',
  668. //$path.'vendor/sonata-project/user-bundle/Entity',
  669. //$path.'vendor/sonata-project/user-bundle/Model',
  670. //$path.'vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Entity',
  671. ];
  672. $proxyDir = $path.'app/cache/';
  673. $config = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(
  674. $paths,
  675. $isDevMode,
  676. $proxyDir,
  677. $cache,
  678. $isSimpleMode
  679. );
  680. return $config;
  681. }
  682. /**
  683. * @param string $table
  684. *
  685. * @return bool
  686. */
  687. public static function tableExists($table)
  688. {
  689. return self::getManager()->getConnection()->getSchemaManager()->tablesExist($table);
  690. }
  691. /**
  692. * @param string $table
  693. *
  694. * @return \Doctrine\DBAL\Schema\Column[]
  695. */
  696. public static function listTableColumns($table)
  697. {
  698. return self::getManager()->getConnection()->getSchemaManager()->listTableColumns($table);
  699. }
  700. }