database.mysqli.lib.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This is a special version of the main database library for Chamilo focused
  5. * on using the MySQLi driver instead of the MySQL driver.
  6. * To use it, make a backup copy of your database.lib.php file and replace
  7. * database.lib.php by this file, then rename MySQLIDatabase into Database.
  8. * Include/require it in your code to use its functionality.
  9. * Because this library contains all the basic database calls, it could be
  10. * replaced by another library for say, PostgreSQL, to actually use Chamilo
  11. * with another database (this is not ready yet because a lot of code still
  12. * uses the MySQL database functions extensively).
  13. *
  14. * @package chamilo.library
  15. */
  16. /* Loading constants */
  17. require_once 'database.constants.inc.php';
  18. /**
  19. * DATABASE CLASS
  20. * The class and its methods
  21. * @package chamilo.library
  22. */
  23. class MySQLIDatabase {
  24. /* Variable use only in the installation process to log errors. See the Database::query function */
  25. static $log_queries = false;
  26. /*
  27. Accessor methods
  28. Usually, you won't need these directly but instead
  29. rely on of the get_xxx_table methods.
  30. */
  31. /**
  32. * Returns the name of the main database.
  33. */
  34. public static function get_main_database()
  35. {
  36. global $_configuration;
  37. return $_configuration['main_database'];
  38. }
  39. /**
  40. * Returns the name of the statistics database.
  41. */
  42. public static function get_statistic_database()
  43. {
  44. global $_configuration;
  45. return $_configuration['statistics_database'];
  46. }
  47. /**
  48. * Returns the name of the SCORM database.
  49. * @deprecated
  50. */
  51. public static function get_scorm_database()
  52. {
  53. global $_configuration;
  54. return $_configuration['scorm_database'];
  55. }
  56. /**
  57. * Returns the name of the database where all the personal stuff of the user is stored
  58. */
  59. public static function get_user_personal_database()
  60. {
  61. global $_configuration;
  62. return $_configuration['user_personal_database'];
  63. }
  64. /**
  65. * Returns the name of the current course database.
  66. * @return mixed Glued database name of false if undefined
  67. */
  68. public static function get_current_course_database()
  69. {
  70. $course_info = api_get_course_info();
  71. if (empty($course_info['dbName'])) {
  72. return false;
  73. }
  74. return $course_info['dbName'];
  75. }
  76. /**
  77. * Returns the glued name of the current course database.
  78. * @return mixed Glued database name of false if undefined
  79. */
  80. public static function get_current_course_glued_database()
  81. {
  82. $course_info = api_get_course_info();
  83. if (empty($course_info['dbNameGlu'])) {
  84. return false;
  85. }
  86. return $course_info['dbNameGlu'];
  87. }
  88. /**
  89. * The glue is the string needed between database and table.
  90. * The trick is: in multiple databases, this is a period (with backticks).
  91. * In single database, this can be e.g. an underscore so we just fake
  92. * there are multiple databases and the code can be written independent
  93. * of the single / multiple database setting.
  94. */
  95. public static function get_database_glue()
  96. {
  97. global $_configuration;
  98. return $_configuration['db_glue'];
  99. }
  100. /**
  101. * Returns the database prefix.
  102. * All created COURSE databases are prefixed with this string.
  103. *
  104. * TIP: This can be convenient e.g. if you have multiple system installations
  105. * on the same physical server.
  106. */
  107. public static function get_database_name_prefix()
  108. {
  109. global $_configuration;
  110. return $_configuration['db_prefix'];
  111. }
  112. /**
  113. * Returns the course table prefix for single database.
  114. * Not certain exactly when this is used.
  115. * Do research.
  116. * It's used in local.inc.php.
  117. */
  118. public static function get_course_table_prefix()
  119. {
  120. global $_configuration;
  121. return $_configuration['table_prefix'];
  122. }
  123. /*
  124. Table name methods
  125. Use these methods to get table names for queries,
  126. instead of constructing them yourself.
  127. Backticks automatically surround the result,
  128. e.g. `COURSE_NAME`.`link`
  129. so the queries can look cleaner.
  130. Example:
  131. $table = Database::get_course_table(TABLE_DOCUMENT);
  132. $sql_query = "SELECT * FROM $table WHERE $condition";
  133. $sql_result = Database::query($sql_query);
  134. $result = Database::fetch_array($sql_result);
  135. */
  136. /**
  137. * A more generic method than the other get_main_xxx_table methods,
  138. * This one returns the correct complete name of any table of the main database of which you pass
  139. * the short name as a parameter.
  140. * Please, define table names as constants in this library and use them
  141. * instead of directly using magic words in your tool code.
  142. *
  143. * @param string $short_table_name, the name of the table
  144. * @return string The full name of the requested table from the main DB
  145. */
  146. public static function get_main_table($short_table_name)
  147. {
  148. return self::format_table_name(self::get_main_database(), $short_table_name);
  149. }
  150. /**
  151. * A more generic method than the older get_course_xxx_table methods,
  152. * This one can return the correct complete name of any course table of which you pass
  153. * the short name as a parameter.
  154. * Please, define table names as constants in this library and use them
  155. * instead of directly using magic words in your tool code.
  156. *
  157. * @param string $short_table_name, the name of the table
  158. * @param string $database_name, optional, name of the course database
  159. * - if you don't specify this, you work on the current course.
  160. */
  161. public static function get_course_table($short_table_name, $database_name = '')
  162. {
  163. return self::format_glued_course_table_name(self::fix_database_parameter($database_name), $short_table_name);
  164. }
  165. /**
  166. * This generic method returns the correct and complete name of any statistic table
  167. * of which you pass the short name as a parameter.
  168. * Please, define table names as constants in this library and use them
  169. * instead of directly using magic words in your tool code.
  170. *
  171. * @param string $short_table_name, the name of the table
  172. * @return string The full name of the requested stats table
  173. */
  174. public static function get_statistic_table($short_table_name)
  175. {
  176. return self::format_table_name(self::get_statistic_database(), $short_table_name);
  177. }
  178. /**
  179. * This generic method returns the correct and complete name of any scorm
  180. * table of which you pass the short name as a parameter. Please, define
  181. * table names as constants in this library and use them instead of directly
  182. * using magic words in your tool code.
  183. *
  184. * @param string $short_table_name, the name of the table
  185. */
  186. public static function get_user_personal_table($short_table_name)
  187. {
  188. return self::format_table_name(self::get_user_personal_database(), $short_table_name);
  189. }
  190. public static function get_course_chat_connected_table($database_name = '')
  191. {
  192. return self::format_glued_course_table_name(self::fix_database_parameter($database_name), TABLE_CHAT_CONNECTED);
  193. }
  194. /*
  195. Query methods
  196. These methods execute a query and return the result(s).
  197. */
  198. /**
  199. * Returns a full list of the contents of the course table as a PHP table
  200. * @return a list (array) of all courses.
  201. * @todo shouldn't this be in the course.lib.php script?
  202. */
  203. public static function get_course_list()
  204. {
  205. $table = self::get_main_table(TABLE_MAIN_COURSE);
  206. return self::store_result(self::query("SELECT * FROM $table"));
  207. }
  208. /**
  209. * Returns an array with all database fields for the specified course.
  210. *
  211. * @param string $course_code The real (system) code of the course (ID from inside the main course table)
  212. * @return array Course info from the course table
  213. * @todo shouldn't this be in the course.lib.php script?
  214. */
  215. public static function get_course_info($course_code)
  216. {
  217. $course_code = self::escape_string($course_code);
  218. $table = self::get_main_table(TABLE_MAIN_COURSE);
  219. $result = self::generate_abstract_course_field_names(
  220. self::fetch_array(self::query("SELECT * FROM $table WHERE code = '$course_code'"))
  221. );
  222. return $result === false ? array('db_name' => '') : $result;
  223. }
  224. /**
  225. * @param $user_id (integer): the id of the user
  226. * @return $user_info (array): user_id, lastname, firstname, username, email, ...
  227. * @author Patrick Cool <patrick.cool@UGent.be>, expanded to get info for any user
  228. * @author Roan Embrechts, first version + converted to Database API
  229. * @version 30 September 2004
  230. * @desc find all the information about a specified user. Without parameter this is the current user.
  231. * @todo shouldn't this be in the user.lib.php script?
  232. */
  233. public static function get_user_info_from_id($user_id = '')
  234. {
  235. if (empty($user_id)) {
  236. return $GLOBALS['_user'];
  237. }
  238. $table = self::get_main_table(TABLE_MAIN_USER);
  239. $user_id = self::escape_string($user_id);
  240. return self::generate_abstract_user_field_names(
  241. self::fetch_array(self::query("SELECT * FROM $table WHERE user_id = '$user_id'")));
  242. }
  243. /**
  244. * Returns course code from a given gradebook category's id
  245. * @param int Category ID
  246. * @return string Course code
  247. * @todo move this function in a gradebook-related library
  248. */
  249. public static function get_course_by_category($category_id)
  250. {
  251. $category_id = intval($category_id);
  252. $info = self::fetch_array(self::query('SELECT course_code FROM '.self::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY).' WHERE id='.$category_id), 'ASSOC');
  253. return $info ? $info['course_code'] : false;
  254. }
  255. /**
  256. * This method creates an abstraction layer between database field names
  257. * and field names expected in code.
  258. *
  259. * This approach helps when changing database names.
  260. * It's also useful now to get rid of the 'franglais'.
  261. *
  262. * @todo add more array entries to abstract course info from field names
  263. * @author Roan Embrechts
  264. *
  265. * @todo What's the use of this method. I think this is better removed.
  266. * There should be consistency in the variable names and the use throughout the scripts
  267. * for the database name we should consistently use or db_name or database (db_name probably being the better one)
  268. */
  269. public static function generate_abstract_course_field_names($result_array)
  270. {
  271. $visual_code = isset($result_array['visual_code']) ? $result_array['visual_code'] : null;
  272. $code = isset($result_array['code']) ? $result_array['code'] : null;
  273. $title = isset($result_array['title']) ? $result_array['title'] : null;
  274. $db_name = isset($result_array['db_name']) ? $result_array['db_name'] : null;
  275. $category_code = isset($result_array['category_code']) ? $result_array['category_code'] : null;
  276. $result_array['official_code'] = $visual_code;
  277. $result_array['visual_code'] = $visual_code;
  278. $result_array['real_code'] = $code;
  279. $result_array['system_code'] = $code;
  280. $result_array['title'] = $title;
  281. $result_array['database'] = $db_name;
  282. $result_array['faculty'] = $category_code;
  283. //$result_array['directory'] = $result_array['directory'];
  284. /*
  285. still to do: (info taken from local.inc.php)
  286. $_course['id' ] = $cData['cours_id' ]; //auto-assigned integer
  287. $_course['name' ] = $cData['title' ];
  288. $_course['official_code'] = $cData['visual_code' ]; // use in echo
  289. $_course['sysCode' ] = $cData['code' ]; // use as key in db
  290. $_course['path' ] = $cData['directory' ]; // use as key in path
  291. $_course['dbName' ] = $cData['db_name' ]; // use as key in db list
  292. $_course['dbNameGlu' ] = $_configuration['table_prefix'] . $cData['dbName'] . $_configuration['db_glue']; // use in all queries
  293. $_course['titular' ] = $cData['tutor_name' ];
  294. $_course['language' ] = $cData['course_language' ];
  295. $_course['extLink' ]['url' ] = $cData['department_url' ];
  296. $_course['extLink' ]['name'] = $cData['department_name'];
  297. $_course['categoryCode'] = $cData['faCode' ];
  298. $_course['categoryName'] = $cData['faName' ];
  299. $_course['visibility' ] = (bool) ($cData['visibility'] == 2 || $cData['visibility'] == 3);
  300. $_course['registrationAllowed'] = (bool) ($cData['visibility'] == 1 || $cData['visibility'] == 2);
  301. */
  302. return $result_array;
  303. }
  304. /**
  305. * This method creates an abstraction layer between database field names
  306. * and field names expected in code.
  307. *
  308. * This helps when changing database names.
  309. * It's also useful now to get rid of the 'franglais'.
  310. *
  311. * @todo add more array entries to abstract user info from field names
  312. * @author Roan Embrechts
  313. * @author Patrick Cool
  314. *
  315. * @todo what's the use of this function. I think this is better removed.
  316. * There should be consistency in the variable names and the use throughout the scripts
  317. */
  318. public static function generate_abstract_user_field_names($result_array)
  319. {
  320. $result_array['firstName'] = $result_array['firstname'];
  321. $result_array['lastName'] = $result_array['lastname'];
  322. $result_array['mail'] = $result_array['email'];
  323. #$result_array['picture_uri'] = $result_array['picture_uri'];
  324. #$result_array ['user_id'] = $result_array['user_id'];
  325. return $result_array;
  326. }
  327. /**
  328. * Counts the number of rows in a table
  329. * @param string $table The table of which the rows should be counted
  330. * @return int The number of rows in the given table.
  331. */
  332. public static function count_rows($table)
  333. {
  334. $obj = self::fetch_object(self::query("SELECT COUNT(*) AS n FROM $table"));
  335. return $obj->n;
  336. }
  337. /*
  338. An intermediate API-layer between the system and the dabase server.
  339. */
  340. /**
  341. * Returns the number of affected rows in the last database operation.
  342. * @param resource $connection The database server connection, for detailed description see the method query().
  343. * @return int Returns the number of affected rows on success, and -1 if the last query failed.
  344. */
  345. public static function affected_rows($connection = null)
  346. {
  347. global $database_connection;
  348. return $database_connection->affected_rows;
  349. }
  350. /**
  351. * Closes non-persistent database connection.
  352. * @param resource $connection (optional) The database server connection, for detailed description see the method query().
  353. * @return bool Returns TRUE on success or FALSE on failure.
  354. */
  355. public static function close($connection = null)
  356. {
  357. return self::use_default_connection($connection) ? mysqli::close() : mysqli::close($connection);
  358. }
  359. /**
  360. * Opens a connection to a database server.
  361. * @param array $parameters (optional) An array that contains the necessary parameters for accessing the server.
  362. * @return resource/boolean Returns a database connection on success or FALSE on failure.
  363. * Note: Currently the array could contain MySQL-specific parameters:
  364. * $parameters['server'], $parameters['username'], $parameters['password'],
  365. * $parameters['new_link'], $parameters['client_flags'], $parameters['persistent'].
  366. * For details see documentation about the functions mysql_connect() and mysql_pconnect().
  367. * @link http://php.net/manual/en/function.mysql-connect.php
  368. * @link http://php.net/manual/en/function.mysql-pconnect.php
  369. */
  370. public static function connect($parameters = array())
  371. {
  372. global $database_connection;
  373. // A MySQL-specific implementation.
  374. if (!isset($parameters['server'])) {
  375. $parameters['server'] = @ini_get('mysqli.default_host');
  376. if (empty($parameters['server'])) {
  377. $parameters['server'] = 'localhost:3306';
  378. }
  379. }
  380. if (!isset($parameters['username'])) {
  381. $parameters['username'] = @ini_get('mysqli.default_user');
  382. }
  383. if (!isset($parameters['password'])) {
  384. $parameters['password'] = @ini_get('mysqli.default_pw');
  385. }
  386. $database_connection = $parameters['persistent']
  387. ? new mysqli('p:'.$parameters['server'], $parameters['username'], $parameters['password'])
  388. : new mysqli($parameters['server'], $parameters['username'], $parameters['password']);
  389. if ($database_connection->connect_errno) {
  390. return false;
  391. } else {
  392. return true;
  393. }
  394. }
  395. /**
  396. * Returns the error number from the last operation done on the database server.
  397. * @param resource $connection (optional) The database server connection, for detailed description see the method query().
  398. * @return int Returns the error number from the last database (operation, or 0 (zero) if no error occurred.
  399. */
  400. public static function errno($connection = null)
  401. {
  402. return self::use_default_connection($connection) ? mysqli_connect_error() : mysqli_connect_error($connection);
  403. }
  404. /**
  405. * Returns the error text from the last operation done on the database server.
  406. * @param resource $connection (optional) The database server connection, for detailed description see the method query().
  407. * @return string Returns the error text from the last database operation, or '' (empty string) if no error occurred.
  408. */
  409. public static function error($connection = null)
  410. {
  411. return self::use_default_connection($connection) ? mysqli_connect_error() : mysqli_connect_error($connection);
  412. }
  413. /**
  414. * Escapes a string to insert into the database as text
  415. * @param string The string to escape
  416. * @param resource $connection (optional) The database server connection, for detailed description see the method query().
  417. * @return string The escaped string
  418. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  419. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  420. */
  421. public static function escape_string($string, $connection = null)
  422. {
  423. global $database_connection;
  424. return get_magic_quotes_gpc()
  425. ? ( $database_connection->escape_string(stripslashes($string)))
  426. : ( $database_connection->escape_string($string));
  427. }
  428. /**
  429. * Gets the array from a SQL result (as returned by Database::query) - help achieving database independence
  430. * @param resource The result from a call to sql_query (e.g. Database::query)
  431. * @param string Optional: "ASSOC","NUM" or "BOTH", as the constant used in mysqli_fetch_array.
  432. * @return array Array of results as returned by php
  433. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  434. */
  435. public static function fetch_array($result, $option = 'BOTH')
  436. {
  437. return ($option == 'ASSOC') ? $result->fetch_array(MYSQLI_ASSOC) : ($option == 'NUM' ? $result->fetch_array(MYSQLI_NUM) : $result->fetch_array());
  438. }
  439. /**
  440. * Gets an associative array from a SQL result (as returned by Database::query).
  441. * This method is equivalent to calling Database::fetch_array() with 'ASSOC' value for the optional second parameter.
  442. * @param resource $result The result from a call to sql_query (e.g. Database::query).
  443. * @return array Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead.
  444. */
  445. public static function fetch_assoc($result)
  446. {
  447. return $result->fetch_assoc();
  448. }
  449. /**
  450. * Gets the next row of the result of the SQL query (as returned by Database::query) in an object form
  451. * @param mysqli $result The result from a call to sql_query (e.g. Database::query)
  452. * @param string $class Optional class name to instantiate
  453. * @param array $params Optional array of parameters
  454. * @return resource Object of class StdClass or the required class, containing the query result row
  455. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  456. */
  457. public static function fetch_object($result, $class = null, $params = null)
  458. {
  459. return !empty($class) ? (is_array($params) ? $result->fetch_object($class, $params) : $result->fetch_object($class)) : $result->fetch_object();
  460. }
  461. /**
  462. * Gets the array from a SQL result (as returned by Database::query) - help achieving database independence
  463. * @param resource The result from a call to sql_query (see Database::query()).
  464. * @return array Array of results as returned by php (mysql_fetch_row)
  465. */
  466. public static function fetch_row($result)
  467. {
  468. return $result->fetch_row();
  469. }
  470. /**
  471. * Frees all the memory associated with the provided result identifier.
  472. * @return bool Returns TRUE on success or FALSE on failure.
  473. * Notes: Use this method if you are concerned about how much memory is being used for queries that return large result sets.
  474. * Anyway, all associated result memory is automatically freed at the end of the script's execution.
  475. */
  476. public static function free_result($result)
  477. {
  478. return $result->free_result();
  479. }
  480. /**
  481. * Returns the database client library version.
  482. * @return string String that represents the client library version.
  483. */
  484. public function get_client_info()
  485. {
  486. return mysqli_get_client_info();
  487. }
  488. /**
  489. * Returns a list of databases created on the server. The list may contain all of the
  490. * available database names or filtered database names by using a pattern.
  491. * @param string $pattern A pattern for filtering database names as if it was needed for the SQL's LIKE clause, for example 'chamilo_%'.
  492. * @param resource $connection The database server connection, for detailed description see the method query().
  493. * @return array Returns in an array the retrieved list of database names.
  494. */
  495. public static function get_databases($pattern = '', $connection = null)
  496. {
  497. $result = array();
  498. $query_result = Database::query(!empty($pattern) ?
  499. "SHOW DATABASES LIKE '".self::escape_string($pattern, $connection)."'" :
  500. "SHOW DATABASES", $connection);
  501. while ($row = Database::fetch_row($query_result)) {
  502. $result[] = $row[0];
  503. }
  504. return $result;
  505. }
  506. /**
  507. * Returns a list of the fields that a given table contains. The list may contain all of the available field names or filtered field names by using a pattern.
  508. * By using a special option, this method is able to return an indexed list of fields' properties, where field names are keys.
  509. * @param string $table This is the examined table.
  510. * @param string $pattern (optional) A pattern for filtering field names as if it was needed for the SQL LIKE clause, for example 'column_%'.
  511. * @param string $database (optional) The name of the targeted database. If it is omitted, the current database is assumed, see Database::select_db().
  512. * @param bool $including_properties (optional) When this option is true, the returned result has the following format:
  513. * array(field_name_1 => array(0 => property_1, 1 => property_2, ...), field_name_2 => array(0 => property_1, ...), ...)
  514. * @param resource $connection (optional) The database server connection, for detailed description see the method query().
  515. * @return array Returns in an array the retrieved list of field names.
  516. */
  517. public static function get_fields($table, $pattern = '', $database = '', $including_properties = false, $connection = null)
  518. {
  519. $result = array();
  520. $query = "SHOW COLUMNS FROM `".self::escape_string($table, $connection)."`";
  521. if (!empty($database)) {
  522. $query .= " FROM `".self::escape_string($database, $connection)."`";
  523. }
  524. if (!empty($pattern)) {
  525. $query .= " LIKE '".self::escape_string($pattern, $connection)."'";
  526. }
  527. $query_result = Database::query($query, $connection);
  528. if ($including_properties) {
  529. // Making an indexed list of the fields and their properties.
  530. while ($row = Database::fetch_row($query_result)) {
  531. $result[$row[0]] = $row;
  532. }
  533. } else {
  534. // Making a plain, flat list.
  535. while ($row = Database::fetch_row($query_result)) {
  536. $result[] = $row[0];
  537. }
  538. }
  539. return $result;
  540. }
  541. /**
  542. * Returns information about the type of the current connection and the server host name.
  543. * @param resource $connection (optional) The database server connection, for detailed description see the method query().
  544. * @return string/boolean Returns string data on success or FALSE on failure.
  545. */
  546. public function get_host_info($connection = null)
  547. {
  548. return self::use_default_connection($connection) ? mysqli_get_host_info() : mysqli_get_host_info($connection);
  549. }
  550. /**
  551. * Retrieves database client/server protocol version.
  552. * @param resource $connection (optional) The database server connection, for detailed description see the method query().
  553. * @return int/boolean Returns the protocol version on success or FALSE on failure.
  554. */
  555. public function get_proto_info($connection = null)
  556. {
  557. return self::use_default_connection($connection) ? mysqli_get_proto_info() : mysqli_get_proto_info($connection);
  558. }
  559. /**
  560. * Retrieves the database server version.
  561. * @param resource $connection (optional) The database server connection, for detailed description see the method query().
  562. * @return string/boolean Returns the MySQL server version on success or FALSE on failure.
  563. */
  564. public function get_server_info($connection = null)
  565. {
  566. return self::use_default_connection($connection) ? mysqli_get_server_info() : mysqli_get_server_info($connection);
  567. }
  568. /**
  569. * Returns a list of tables within a database. The list may contain all of the
  570. * available table names or filtered table names by using a pattern.
  571. * @param string $database (optional) The name of the examined database. If it is omitted, the current database is assumed, see Database::select_db().
  572. * @param string $pattern (optional) A pattern for filtering table names as if it was needed for the SQL LIKE clause, for example 'access_%'.
  573. * @param resource $connection (optional) The database server connection, for detailed description see the method query().
  574. * @return array Returns in an array the retrieved list of table names.
  575. */
  576. public static function get_tables($database = '', $pattern = '', $connection = null)
  577. {
  578. $result = array();
  579. $query = "SHOW TABLES";
  580. if (!empty($database)) {
  581. $query .= " FROM `".self::escape_string($database, $connection)."`";
  582. }
  583. if (!empty($pattern)) {
  584. $query .= " LIKE '".self::escape_string($pattern, $connection)."'";
  585. }
  586. $query_result = Database::query($query, $connection);
  587. while ($row = Database::fetch_row($query_result)) {
  588. $result[] = $row[0];
  589. }
  590. return $result;
  591. }
  592. /**
  593. * Gets the ID of the last item inserted into the database
  594. * @param resource $connection (optional) The database server connection, for detailed description see the method query().
  595. * @return int The last ID as returned by the DB function
  596. * @comment This should be updated to use ADODB at some point
  597. */
  598. public static function insert_id($connection = null)
  599. {
  600. global $database_connection;
  601. return $database_connection->insert_id;
  602. }
  603. /**
  604. * Gets the number of rows from the last query result - help achieving database independence
  605. * @param resource The result
  606. * @return integer The number of rows contained in this result
  607. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  608. **/
  609. public static function num_rows($result)
  610. {
  611. return is_a($result,'mysqli_result') ? $result->num_rows : false;
  612. }
  613. /**
  614. * Acts as the relative *_result() function of most DB drivers and fetches a
  615. * specific line and a field
  616. * @param resource The database resource to get data from
  617. * @param integer The row number
  618. * @param string Optional field name or number
  619. * @result mixed One cell of the result, or FALSE on error
  620. */
  621. public static function result(&$resource, $row, $field = '')
  622. {
  623. if (self::num_rows($resource) > 0) {
  624. if (!empty($field)) {
  625. $r = mysqli_data_seek($resource, $row);
  626. return $r[$field];
  627. } else {
  628. return mysqli_data_seek($resource, $row);
  629. }
  630. } else { return null; }
  631. }
  632. /**
  633. * This method returns a resource
  634. * Documentation has been added by Arthur Portugal
  635. * Some adaptations have been implemented by Ivan Tcholakov, 2009, 2010
  636. * @author Olivier Brouckaert
  637. * @param string $query The SQL query
  638. * @param resource $connection (optional) The database server (MySQL) connection.
  639. * If it is not specified, the connection opened by mysql_connect() is assumed.
  640. * If no connection is found, the server will try to create one as if mysql_connect() was called with no arguments.
  641. * If no connection is found or established, an E_WARNING level error is generated.
  642. * @param string $file (optional) On error it shows the file in which the error has been triggered (use the "magic" constant __FILE__ as input parameter)
  643. * @param string $line (optional) On error it shows the line in which the error has been triggered (use the "magic" constant __LINE__ as input parameter)
  644. * @return resource The returned result from the query
  645. * Note: The parameter $connection could be skipped. Here are examples of this method usage:
  646. * Database::query($query);
  647. * $result = Database::query($query);
  648. * Database::query($query, $connection);
  649. * $result = Database::query($query, $connection);
  650. * The following ways for calling this method are obsolete:
  651. * Database::query($query, __FILE__, __LINE__);
  652. * $result = Database::query($query, __FILE__, __LINE__);
  653. * Database::query($query, $connection, __FILE__, __LINE__);
  654. * $result = Database::query($query, $connection, __FILE__, __LINE__);
  655. */
  656. public static function query($query, $connection = null, $file = null, $line = null)
  657. {
  658. global $database_connection;
  659. $result = @$database_connection->query($query);
  660. if ($database_connection->errno) {
  661. $backtrace = debug_backtrace(); // Retrieving information about the caller statement.
  662. if (isset($backtrace[0])) {
  663. $caller = & $backtrace[0];
  664. } else {
  665. $caller = array();
  666. }
  667. if (isset($backtrace[1])) {
  668. $owner = & $backtrace[1];
  669. } else {
  670. $owner = array();
  671. }
  672. if (empty($file)) {
  673. $file = $caller['file'];
  674. }
  675. if (empty($line) && $line !== false) {
  676. $line = $caller['line'];
  677. }
  678. $type = $owner['type'];
  679. $function = $owner['function'];
  680. $class = $owner['class'];
  681. $server_type = api_get_setting('server_type');
  682. if (!empty($line) && !empty($server_type) && $server_type != 'production') {
  683. $info = '<pre>' .
  684. '<strong>DATABASE ERROR #'.self::errno($connection).':</strong><br /> ' .
  685. self::remove_XSS(self::error($connection)) . '<br />' .
  686. '<strong>QUERY :</strong><br /> ' .
  687. self::remove_XSS($query) . '<br />' .
  688. '<strong>FILE :</strong><br /> ' .
  689. (empty($file) ? ' unknown ' : $file) . '<br />' .
  690. '<strong>LINE :</strong><br /> ' .
  691. (empty($line) ? ' unknown ' : $line) . '<br />';
  692. if (empty($type)) {
  693. if (!empty($function)) {
  694. $info .= '<strong>FUNCTION :</strong><br /> ' . $function;
  695. }
  696. } else {
  697. if (!empty($class) && !empty($function)) {
  698. $info .= '<strong>CLASS :</strong><br /> ' . $class . '<br />';
  699. $info .= '<strong>METHOD :</strong><br /> ' . $function;
  700. }
  701. }
  702. $info .= '</pre>';
  703. echo $info;
  704. }
  705. if (isset(self::$log_queries) && self::$log_queries) {
  706. error_log("---------------- SQL error ---------------- ");
  707. error_log($query);
  708. error_log('error #'.self::errno($connection));
  709. error_log('error: '.self::error($connection));
  710. $info = 'FILE: ' .(empty($file) ? ' unknown ' : $file);
  711. error_log($info);
  712. $info = 'LINE: '.(empty($line) ? ' unknown ' : $line);
  713. error_log($info);
  714. if (empty($type)) {
  715. if (!empty($function)) {
  716. $info = 'FUNCTION: ' . $function;
  717. error_log($info);
  718. }
  719. } else {
  720. if (!empty($class) && !empty($function)) {
  721. $info .= 'CLASS: ' . $class;
  722. error_log($info);
  723. $info .= 'METHOD: ' . $function;
  724. error_log($info);
  725. }
  726. }
  727. error_log("---------------- end ----------------");
  728. }
  729. }
  730. return $result;
  731. }
  732. /**
  733. * Selects a database.
  734. * @param string $database_name The name of the database that is to be selected.
  735. * @param resource $connection (optional) The database server connection, for detailed description see the method query().
  736. * @return bool Returns TRUE on success or FALSE on failure.
  737. */
  738. public static function select_db($database_name, $connection = null)
  739. {
  740. global $database_connection;
  741. $database_connection->select_db($database_name);
  742. return !$database_connection->errno;
  743. //return self::use_default_connection($connection) ? mysqli_select_db($connection, $database_name) : mysqli_select_db($connection, $database_name);
  744. }
  745. /**
  746. * Stores a query result into an array.
  747. *
  748. * @author Olivier Brouckaert
  749. * @param resource $result - the return value of the query
  750. * @param option BOTH, ASSOC, or NUM
  751. * @return array - the value returned by the query
  752. */
  753. public static function store_result($result, $option = 'BOTH')
  754. {
  755. $array = array();
  756. if ($result !== false) { // For isolation from database engine's behaviour.
  757. while ($row = self::fetch_array($result, $option)) {
  758. $array[] = $row;
  759. }
  760. }
  761. return $array;
  762. }
  763. /*
  764. Encodings and collations supported by MySQL database server
  765. */
  766. /**
  767. * Checks whether a given encoding is supported by the database server.
  768. * @param string $encoding The encoding (a system conventional id, for example 'UTF-8') to be checked.
  769. * @return bool Returns a boolean value as a check-result.
  770. * @author Ivan Tcholakov
  771. */
  772. public static function is_encoding_supported($encoding)
  773. {
  774. static $supported = array();
  775. if (!isset($supported[$encoding])) {
  776. $supported[$encoding] = false;
  777. if (strlen($db_encoding = self::to_db_encoding($encoding)) > 0) {
  778. if (self::num_rows(self::query("SHOW CHARACTER SET WHERE Charset = '".self::escape_string($db_encoding)."';")) > 0) {
  779. $supported[$encoding] = true;
  780. }
  781. }
  782. }
  783. return $supported[$encoding];
  784. }
  785. /**
  786. * Constructs a SQL clause about default character set and default collation for newly created databases and tables.
  787. * Example: Database::make_charset_clause('UTF-8', 'bulgarian') returns
  788. * DEFAULT CHARACTER SET `utf8` DEFAULT COLLATE `utf8_general_ci`
  789. * @param string $encoding (optional) The default database/table encoding (a system conventional id) to be used.
  790. * @param string $language (optional) Language (a system conventional id) used for choosing language sensitive collation (if it is possible).
  791. * @return string Returns the constructed SQL clause or empty string if $encoding is not correct or is not supported.
  792. * @author Ivan Tcholakov
  793. */
  794. public static function make_charset_clause($encoding = null, $language = null)
  795. {
  796. if (empty($encoding)) {
  797. $encoding = api_get_system_encoding();
  798. }
  799. if (empty($language)) {
  800. $language = api_get_interface_language();
  801. }
  802. $charset_clause = '';
  803. if (self::is_encoding_supported($encoding)) {
  804. $db_encoding = Database::to_db_encoding($encoding);
  805. $charset_clause .= " DEFAULT CHARACTER SET `".$db_encoding."`";
  806. $db_collation = Database::to_db_collation($encoding, $language);
  807. if (!empty($db_collation)) {
  808. $charset_clause .= " DEFAULT COLLATE `".$db_collation."`";
  809. }
  810. }
  811. return $charset_clause;
  812. }
  813. /**
  814. * Converts an encoding identificator to MySQL-specific encoding identifier,
  815. * i.e. 'UTF-8' --> 'utf8'.
  816. * @param string $encoding The conventional encoding identifier.
  817. * @return string Returns the corresponding MySQL-specific encoding identifier if any, otherwise returns NULL.
  818. * @author Ivan Tcholakov
  819. */
  820. public static function to_db_encoding($encoding)
  821. {
  822. static $result = array();
  823. if (!isset($result[$encoding])) {
  824. $result[$encoding] = null;
  825. $encoding_map = & self::get_db_encoding_map();
  826. foreach ($encoding_map as $key => $value) {
  827. if (api_equal_encodings($encoding, $key)) {
  828. $result[$encoding] = $value;
  829. break;
  830. }
  831. }
  832. }
  833. return $result[$encoding];
  834. }
  835. /**
  836. * Converts a MySQL-specific encoding identifier to conventional encoding identifier,
  837. * i.e. 'utf8' --> 'UTF-8'.
  838. * @param string $encoding The MySQL-specific encoding identifier.
  839. * @return string Returns the corresponding conventional encoding identifier if any, otherwise returns NULL.
  840. * @author Ivan Tcholakov
  841. */
  842. public static function from_db_encoding($db_encoding)
  843. {
  844. static $result = array();
  845. if (!isset($result[$db_encoding])) {
  846. $result[$db_encoding] = null;
  847. $encoding_map = & self::get_db_encoding_map();
  848. foreach ($encoding_map as $key => $value) {
  849. if (strtolower($db_encoding) == $value) {
  850. $result[$db_encoding] = $key;
  851. break;
  852. }
  853. }
  854. }
  855. return $result[$db_encoding];
  856. }
  857. /**
  858. * Chooses the default MySQL-specific collation from given encoding and language.
  859. * @param string $encoding A conventional encoding id, i.e. 'UTF-8'
  860. * @param string $language (optional) A conventional for the system language id, i.e. 'bulgarian'. If it is empty, the chosen collation is the default server value corresponding to the given encoding.
  861. * @return string Returns a suitable default collation, for example 'utf8_general_ci', or NULL if collation was not found.
  862. * @author Ivan Tcholakov
  863. */
  864. public static function to_db_collation($encoding, $language = null)
  865. {
  866. static $result = array();
  867. if (!isset($result[$encoding][$language])) {
  868. $result[$encoding][$language] = null;
  869. if (self::is_encoding_supported($encoding)) {
  870. $db_encoding = self::to_db_encoding($encoding);
  871. if (!empty($language)) {
  872. $lang = api_purify_language_id($language);
  873. $res = self::check_db_collation($db_encoding, $lang);
  874. if (empty($res)) {
  875. $db_collation_map = & self::get_db_collation_map();
  876. if (isset($db_collation_map[$lang])) {
  877. $res = self::check_db_collation($db_encoding, $db_collation_map[$lang]);
  878. }
  879. }
  880. if (empty($res)) {
  881. $res = self::check_db_collation($db_encoding, null);
  882. }
  883. $result[$encoding][$language] = $res;
  884. } else {
  885. $result[$encoding][$language] = self::check_db_collation($db_encoding, null);
  886. }
  887. }
  888. }
  889. return $result[$encoding][$language];
  890. }
  891. /*
  892. Private methods
  893. You should not access these from outside the class
  894. No effort is made to keep the names / results the same.
  895. */
  896. /**
  897. * Glues a course database.
  898. * glue format from local.inc.php.
  899. */
  900. private static function glue_course_database_name($database_name)
  901. {
  902. return self::get_course_table_prefix().$database_name.self::get_database_glue();
  903. }
  904. /**
  905. * @param string $database_name, can be empty to use current course db
  906. *
  907. * @return the glued parameter if it is not empty,
  908. * or the current course database (glued) if the parameter is empty.
  909. */
  910. private static function fix_database_parameter($database_name)
  911. {
  912. if (empty($database_name)) {
  913. $course_info = api_get_course_info();
  914. return $course_info['dbNameGlu'];
  915. }
  916. return self::glue_course_database_name($database_name);
  917. }
  918. /**
  919. * Structures a course database and table name to ready them
  920. * for querying. The course database parameter is considered glued:
  921. * e.g. COURSE001`.`
  922. */
  923. private static function format_glued_course_table_name($database_name_with_glue, $table)
  924. {
  925. return '`'.$database_name_with_glue.$table.'`';
  926. }
  927. /**
  928. * Structures a database and table name to ready them
  929. * for querying. The database parameter is considered not glued,
  930. * just plain e.g. COURSE001
  931. */
  932. private static function format_table_name($database, $table)
  933. {
  934. return '`'.$database.'`.`'.$table.'`';
  935. }
  936. /**
  937. * This private method is to be used by the other methods in this class for
  938. * checking whether the input parameter $connection actually has been provided.
  939. * If the input parameter connection is not a resource or if it is not FALSE (in case of error)
  940. * then the default opened connection should be used by the called method.
  941. * @param resource/boolean $connection The checked parameter $connection.
  942. * @return boolean TRUE means that calling method should use the default connection.
  943. * FALSE means that (valid) parameter $connection has been provided and it should be used.
  944. */
  945. private static function use_default_connection($connection)
  946. {
  947. return !is_resource($connection) && $connection !== false;
  948. }
  949. /**
  950. * This private method tackles the XSS injections. It is similar to Security::remove_XSS() and works always,
  951. * including the time of initialization when the class Security has not been loaded yet.
  952. * @param string The input variable to be filtered from XSS, in this class it is expected to be a string.
  953. * @return string Returns the filtered string as a result.
  954. */
  955. private static function remove_XSS(& $var)
  956. {
  957. return class_exists('Security') ? Security::remove_XSS($var) : @htmlspecialchars($var, ENT_QUOTES, api_get_system_encoding());
  958. }
  959. /**
  960. * This private method encapsulates a table with relations between
  961. * conventional and MuSQL-specific encoding identificators.
  962. * @author Ivan Tcholakov
  963. */
  964. private static function & get_db_encoding_map()
  965. {
  966. static $encoding_map = array(
  967. 'ARMSCII-8' => 'armscii8',
  968. 'BIG5' => 'big5',
  969. 'BINARY' => 'binary',
  970. 'CP866' => 'cp866',
  971. 'EUC-JP' => 'ujis',
  972. 'EUC-KR' => 'euckr',
  973. 'GB2312' => 'gb2312',
  974. 'GBK' => 'gbk',
  975. 'ISO-8859-1' => 'latin1',
  976. 'ISO-8859-2' => 'latin2',
  977. 'ISO-8859-7' => 'greek',
  978. 'ISO-8859-8' => 'hebrew',
  979. 'ISO-8859-9' => 'latin5',
  980. 'ISO-8859-13' => 'latin7',
  981. 'ISO-8859-15' => 'latin1',
  982. 'KOI8-R' => 'koi8r',
  983. 'KOI8-U' => 'koi8u',
  984. 'SHIFT-JIS' => 'sjis',
  985. 'TIS-620' => 'tis620',
  986. 'US-ASCII' => 'ascii',
  987. 'UTF-8' => 'utf8',
  988. 'WINDOWS-1250' => 'cp1250',
  989. 'WINDOWS-1251' => 'cp1251',
  990. 'WINDOWS-1252' => 'latin1',
  991. 'WINDOWS-1256' => 'cp1256',
  992. 'WINDOWS-1257' => 'cp1257'
  993. );
  994. return $encoding_map;
  995. }
  996. /**
  997. * A helper language id translation table for choosing some collations.
  998. * @author Ivan Tcholakov
  999. */
  1000. private static function & get_db_collation_map()
  1001. {
  1002. static $db_collation_map = array(
  1003. 'german' => 'german2',
  1004. 'simpl_chinese' => 'chinese',
  1005. 'trad_chinese' => 'chinese',
  1006. 'turkce' => 'turkish'
  1007. );
  1008. return $db_collation_map;
  1009. }
  1010. /**
  1011. * Constructs a MySQL-specific collation and checks whether it is supported by the database server.
  1012. * @param string $db_encoding A MySQL-specific encoding id, i.e. 'utf8'
  1013. * @param string $language A MySQL-compatible language id, i.e. 'bulgarian'
  1014. * @return string Returns a suitable default collation, for example 'utf8_general_ci', or NULL if collation was not found.
  1015. * @author Ivan Tcholakov
  1016. */
  1017. private static function check_db_collation($db_encoding, $language)
  1018. {
  1019. if (empty($db_encoding)) {
  1020. return null;
  1021. }
  1022. if (empty($language)) {
  1023. $result = self::fetch_array(self::query("SHOW COLLATION WHERE Charset = '".self::escape_string($db_encoding)."' AND `Default` = 'Yes';"), 'NUM');
  1024. return $result ? $result[0] : null;
  1025. }
  1026. $collation = $db_encoding.'_'.$language.'_ci';
  1027. $query_result = self::query("SHOW COLLATION WHERE Charset = '".self::escape_string($db_encoding)."';");
  1028. while ($result = self::fetch_array($query_result, 'NUM')) {
  1029. if ($result[0] == $collation) {
  1030. return $collation;
  1031. }
  1032. }
  1033. return null;
  1034. }
  1035. /*
  1036. New useful DB functions
  1037. */
  1038. /**
  1039. * Experimental useful database insert
  1040. * @todo lot of stuff to do here
  1041. */
  1042. public static function insert($table_name, $attributes)
  1043. {
  1044. if (empty($attributes) || empty($table_name)) {
  1045. return false;
  1046. }
  1047. $filtred_attributes = array();
  1048. foreach($attributes as $key => $value) {
  1049. $filtred_attributes[$key] = "'".self::escape_string($value)."'";
  1050. }
  1051. $params = array_keys($filtred_attributes); //@todo check if the field exists in the table we should use a describe of that table
  1052. $values = array_values($filtred_attributes);
  1053. if (!empty($params) && !empty($values)) {
  1054. $sql = 'INSERT INTO '.$table_name.' ('.implode(',',$params).') VALUES ('.implode(',',$values).')';
  1055. $result = self::query($sql);
  1056. return self::get_last_insert_id();
  1057. }
  1058. return false;
  1059. }
  1060. /**
  1061. * Experimental useful database finder
  1062. * @todo lot of stuff to do here
  1063. */
  1064. public static function select($columns, $table_name, $conditions = array(), $type_result = 'all', $option = 'ASSOC')
  1065. {
  1066. $conditions = self::parse_conditions($conditions);
  1067. //@todo we could do a describe here to check the columns ...
  1068. $clean_columns = '';
  1069. if (is_array($columns)) {
  1070. $clean_columns = implode(',', $columns);
  1071. } else {
  1072. if ($columns == '*') {
  1073. $clean_columns = '*';
  1074. } else {
  1075. $clean_columns = (string)$columns;
  1076. }
  1077. }
  1078. $sql = "SELECT $clean_columns FROM $table_name $conditions";
  1079. $result = self::query($sql);
  1080. $array = array();
  1081. //if (self::num_rows($result) > 0 ) {
  1082. if ($type_result == 'all') {
  1083. while ($row = self::fetch_array($result, $option)) {
  1084. if (isset($row['id'])) {
  1085. $array[$row['id']] = $row;
  1086. } else {
  1087. $array[] = $row;
  1088. }
  1089. }
  1090. } else {
  1091. $array = self::fetch_array($result, $option);
  1092. }
  1093. return $array;
  1094. }
  1095. /**
  1096. * Parses WHERE/ORDER conditions i.e array('where'=>array('id = ?' =>'4'), 'order'=>'id DESC'))
  1097. * @param array
  1098. * @todo lot of stuff to do here
  1099. */
  1100. static function parse_conditions($conditions)
  1101. {
  1102. if (empty($conditions)) {
  1103. return '';
  1104. }
  1105. $return_value = '';
  1106. foreach ($conditions as $type_condition => $condition_data) {
  1107. $type_condition = strtolower($type_condition);
  1108. switch($type_condition) {
  1109. case 'where':
  1110. $where_return = '';
  1111. foreach ($condition_data as $condition => $value_array) {
  1112. if (is_array($value_array)) {
  1113. $clean_values = array();
  1114. foreach($value_array as $item) {
  1115. $item = Database::escape_string($item);
  1116. $clean_values[]= $item;
  1117. }
  1118. } else {
  1119. $value_array = Database::escape_string($value_array);
  1120. $clean_values = $value_array;
  1121. }
  1122. if (!empty($condition) && $clean_values != '') {
  1123. $condition = str_replace('%',"'@percentage@'", $condition); //replace "%"
  1124. $condition = str_replace("'?'","%s", $condition);
  1125. $condition = str_replace("?","%s", $condition);
  1126. $condition = str_replace("@%s@","@-@", $condition);
  1127. $condition = str_replace("%s","'%s'", $condition);
  1128. $condition = str_replace("@-@","@%s@", $condition);
  1129. //Treat conditons as string
  1130. $condition = vsprintf($condition, $clean_values);
  1131. $condition = str_replace('@percentage@','%', $condition); //replace "%"
  1132. $where_return .= $condition;
  1133. }
  1134. }
  1135. if (!empty($where_return)) {
  1136. $return_value = " WHERE $where_return" ;
  1137. }
  1138. break;
  1139. case 'order':
  1140. $order_array = $condition_data;
  1141. if (!empty($order_array)) {
  1142. // 'order' => 'id desc, name desc'
  1143. $order_array = self::escape_string($order_array);
  1144. $new_order_array = explode(',', $order_array);
  1145. $temp_value = array();
  1146. foreach($new_order_array as $element) {
  1147. $element = explode(' ', $element);
  1148. $element = array_filter($element);
  1149. $element = array_values($element);
  1150. if (!empty($element[1])) {
  1151. $element[1] = strtolower($element[1]);
  1152. $order = 'DESC';
  1153. if (in_array($element[1], array('desc', 'asc'))) {
  1154. $order = $element[1];
  1155. }
  1156. $temp_value[]= $element[0].' '.$order.' ';
  1157. } else {
  1158. //by default DESC
  1159. $temp_value[]= $element[0].' DESC ';
  1160. }
  1161. }
  1162. if (!empty($temp_value)) {
  1163. $return_value .= ' ORDER BY '.implode(', ', $temp_value);
  1164. } else {
  1165. //$return_value .= '';
  1166. }
  1167. }
  1168. break;
  1169. case 'limit':
  1170. $limit_array = explode(',', $condition_data);
  1171. if (!empty($limit_array)) {
  1172. if (count($limit_array) > 1) {
  1173. $return_value .= ' LIMIT '.intval($limit_array[0]).' , '.intval($limit_array[1]);
  1174. } else {
  1175. $return_value .= ' LIMIT '.intval($limit_array[0]);
  1176. }
  1177. }
  1178. break;
  1179. }
  1180. }
  1181. return $return_value;
  1182. }
  1183. public static function parse_where_conditions($coditions)
  1184. {
  1185. return self::parse_conditions(array('where'=>$coditions));
  1186. }
  1187. /**
  1188. * Experimental useful database update
  1189. * @todo lot of stuff to do here
  1190. */
  1191. public static function delete($table_name, $where_conditions)
  1192. {
  1193. $result = false;
  1194. $where_return = self::parse_where_conditions($where_conditions);
  1195. $sql = "DELETE FROM $table_name $where_return ";
  1196. $result = self::query($sql);
  1197. $affected_rows = self::affected_rows();
  1198. //@todo should return affected_rows for
  1199. return $affected_rows;
  1200. }
  1201. /**
  1202. * Experimental useful database update
  1203. * @todo lot of stuff to do here
  1204. */
  1205. public static function update($table_name, $attributes, $where_conditions = array())
  1206. {
  1207. if (!empty($table_name) && !empty($attributes)) {
  1208. $update_sql = '';
  1209. //Cleaning attributes
  1210. $count = 1;
  1211. foreach ($attributes as $key=>$value) {
  1212. $value = self::escape_string($value);
  1213. $update_sql .= "$key = '$value' ";
  1214. if ($count < count($attributes)) {
  1215. $update_sql.=', ';
  1216. }
  1217. $count++;
  1218. }
  1219. if (!empty($update_sql)) {
  1220. //Parsing and cleaning the where conditions
  1221. $where_return = self::parse_where_conditions($where_conditions);
  1222. $sql = "UPDATE $table_name SET $update_sql $where_return ";
  1223. //echo $sql; exit;
  1224. $result = self::query($sql);
  1225. $affected_rows = self::affected_rows();
  1226. return $affected_rows;
  1227. }
  1228. }
  1229. return false;
  1230. }
  1231. /*
  1232. DEPRECATED METHODS
  1233. */
  1234. /**
  1235. * @deprecated Use api_get_language_isocode($language) instead.
  1236. */
  1237. public static function get_language_isocode($language)
  1238. {
  1239. return api_get_language_isocode($language);
  1240. }
  1241. /**
  1242. * @deprecated Use Database::insert_id() instead.
  1243. */
  1244. public static function get_last_insert_id()
  1245. {
  1246. global $database_connection;
  1247. return $database_connection->insert_id($database_connection);
  1248. }
  1249. }
  1250. //end class Database