PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/FSN/mediatheque/zp-core/functions-db-MySQL.php

https://gitlab.com/r.collas/site_central
PHP | 327 lines | 211 code | 35 blank | 81 comment | 35 complexity | af231a1702625bc44d0261a7a69c9ba5 MD5 | raw file
  1. <?php
  2. /**
  3. * Database core functions forthe MySQL legacy library
  4. *
  5. * Note: PHP version 5 states that the MySQL library is "Maintenance only, Long term deprecation announced."
  6. * It recommends using the PDO::MySQL or the MySQLi library instead.
  7. *
  8. * @package core
  9. */
  10. // force UTF-8 Ø
  11. define('DATABASE_SOFTWARE', 'MySQL');
  12. Define('DATABASE_MIN_VERSION', '5.0.7');
  13. Define('DATABASE_DESIRED_VERSION', '5.5.0');
  14. /**
  15. * Connect to the database server and select the database.
  16. * @param array $config the db configuration parameters
  17. * @param bool $errorstop set to false to omit error messages
  18. * @return true if successful connection
  19. */
  20. function db_connect($config, $errorstop = true) {
  21. global $_zp_DB_connection, $_zp_DB_details;
  22. $_zp_DB_details = unserialize(DB_NOT_CONNECTED);
  23. if (function_exists('mysql_connect')) {
  24. $_zp_DB_connection = @mysql_connect($config['mysql_host'], $config['mysql_user'], $config['mysql_pass']);
  25. } else {
  26. $_zp_DB_connection = NULL;
  27. }
  28. if (!$_zp_DB_connection) {
  29. if ($errorstop) {
  30. zp_error(sprintf(gettext('MySQL Error: Zenphoto received the error %s when connecting to the database server.'), mysql_error()));
  31. }
  32. return false;
  33. }
  34. $_zp_DB_details['mysql_host'] = $config['mysql_host'];
  35. if (!@mysql_select_db($config['mysql_database'])) {
  36. if ($errorstop) {
  37. zp_error(sprintf(gettext('MySQL Error: MySQL returned the error %1$s when Zenphoto tried to select the database %2$s.'), mysql_error(), $config['mysql_database']));
  38. }
  39. return false;
  40. }
  41. $_zp_DB_details = $config;
  42. if (array_key_exists('UTF-8', $config) && $config['UTF-8']) {
  43. mysql_set_charset('utf8', $_zp_DB_connection);
  44. }
  45. // set the sql_mode to relaxed (if possible)
  46. @mysql_query('SET SESSION sql_mode="";');
  47. return $_zp_DB_connection;
  48. }
  49. /**
  50. * The main query function. Runs the SQL on the connection and handles errors.
  51. * @param string $sql sql code
  52. * @param bool $errorstop set to false to supress the error message
  53. * @return results of the sql statements
  54. * @since 0.6
  55. */
  56. function query($sql, $errorstop = true) {
  57. global $_zp_DB_connection, $_zp_DB_details;
  58. if ($result = @mysql_query($sql, $_zp_DB_connection)) {
  59. return $result;
  60. }
  61. if ($errorstop) {
  62. $sql = str_replace('`' . $_zp_DB_details['mysql_prefix'], '`[' . gettext('prefix') . ']', $sql);
  63. $sql = str_replace($_zp_DB_details['mysql_database'], '[' . gettext('DB') . ']', $sql);
  64. trigger_error(sprintf(gettext('%1$s Error: ( %2$s ) failed. %1$s returned the error %3$s'), DATABASE_SOFTWARE, $sql, db_error()), E_USER_ERROR);
  65. }
  66. return false;
  67. }
  68. /**
  69. * Runs a SQL query and returns an associative array of the first row.
  70. * Doesn't handle multiple rows, so this should only be used for unique entries.
  71. * @param string $sql sql code
  72. * @param bool $errorstop set to false to supress the error message
  73. * @return results of the sql statements
  74. * @since 0.6
  75. */
  76. function query_single_row($sql, $errorstop = true) {
  77. $result = query($sql, $errorstop);
  78. if (is_resource($result)) {
  79. $row = mysql_fetch_assoc($result);
  80. mysql_free_result($result);
  81. return $row;
  82. } else {
  83. return false;
  84. }
  85. }
  86. /**
  87. * Runs a SQL query and returns an array of associative arrays of every row returned.
  88. * @param string $sql sql code
  89. * @param bool $errorstop set to false to supress the error message
  90. * @param string $key optional array index key
  91. * @return results of the sql statements
  92. * @since 0.6
  93. */
  94. function query_full_array($sql, $errorstop = true, $key = NULL) {
  95. $result = query($sql, $errorstop);
  96. if (is_resource($result)) {
  97. $allrows = array();
  98. if (is_null($key)) {
  99. while ($row = mysql_fetch_assoc($result)) {
  100. $allrows[] = $row;
  101. }
  102. } else {
  103. while ($row = mysql_fetch_assoc($result)) {
  104. $allrows[$row[$key]] = $row;
  105. }
  106. }
  107. mysql_free_result($result);
  108. return $allrows;
  109. } else {
  110. return false;
  111. }
  112. }
  113. /**
  114. * mysql_real_escape_string standin that insures the DB connection is passed.
  115. *
  116. * @param string $string
  117. * @return string
  118. */
  119. function db_quote($string, $addquotes = true) {
  120. global $_zp_DB_connection;
  121. $escaped = mysql_real_escape_string($string, $_zp_DB_connection);
  122. if ($addquotes) {
  123. return "'" . $escaped . "'";
  124. } else {
  125. return $escaped;
  126. }
  127. }
  128. /*
  129. * returns the insert id of the last database insert
  130. */
  131. function db_insert_id() {
  132. return mysql_insert_id();
  133. }
  134. /*
  135. * Fetch a result row as an associative array
  136. */
  137. function db_fetch_assoc($resource) {
  138. if ($resource) {
  139. return mysql_fetch_assoc($resource);
  140. }
  141. return false;
  142. }
  143. /*
  144. * Returns the text of the error message from previous operation
  145. */
  146. function db_error() {
  147. global $_zp_DB_connection;
  148. if (is_object($_zp_DB_connection)) {
  149. return mysql_error();
  150. }
  151. if (!$msg = mysql_error())
  152. $msg = sprintf(gettext('%s not connected'), DATABASE_SOFTWARE);
  153. return $msg;
  154. }
  155. /*
  156. * Get number of affected rows in previous operation
  157. */
  158. function db_affected_rows() {
  159. return mysql_affected_rows();
  160. }
  161. /*
  162. * Get a result row as an enumerated array
  163. */
  164. function db_fetch_row($result) {
  165. if (is_resource($result)) {
  166. return mysql_fetch_row($result);
  167. }
  168. return false;
  169. }
  170. /*
  171. * Get number of rows in result
  172. */
  173. function db_num_rows($result) {
  174. return mysql_num_rows($result);
  175. }
  176. /**
  177. * Closes the database
  178. */
  179. function db_close() {
  180. global $_zp_DB_connection;
  181. if ($_zp_DB_connection) {
  182. $rslt = mysql_close($_zp_DB_connection);
  183. } else {
  184. $rslt = true;
  185. }
  186. $_zp_DB_connection = NULL;
  187. return $rslt;
  188. }
  189. /*
  190. * report the software of the database
  191. */
  192. function db_software() {
  193. $dbversion = trim(@mysql_get_server_info());
  194. preg_match('/[0-9,\.]*/', $dbversion, $matches);
  195. return array('application' => DATABASE_SOFTWARE, 'required' => DATABASE_MIN_VERSION, 'desired' => DATABASE_DESIRED_VERSION, 'version' => $matches[0]);
  196. }
  197. /**
  198. * create the database
  199. */
  200. function db_create() {
  201. global $_zp_DB_details;
  202. $sql = 'CREATE DATABASE IF NOT EXISTS ' . '`' . $_zp_DB_details['mysql_database'] . '`' . db_collation();
  203. return query($sql, false);
  204. }
  205. /**
  206. * Returns user's permissions on the database
  207. */
  208. function db_permissions() {
  209. global $_zp_DB_details;
  210. $sql = "SHOW GRANTS FOR " . $_zp_DB_details['mysql_user'] . ";";
  211. $result = query($sql, false);
  212. if (!$result) {
  213. $result = query("SHOW GRANTS;", false);
  214. }
  215. if (is_resource($result)) {
  216. $db_results = array();
  217. while ($onerow = db_fetch_row($result)) {
  218. $db_results[] = $onerow[0];
  219. }
  220. return $db_results;
  221. } else {
  222. return false;
  223. }
  224. }
  225. /**
  226. * Sets the SQL session mode to empty
  227. */
  228. function db_setSQLmode() {
  229. return query('SET SESSION sql_mode=""', false);
  230. }
  231. /**
  232. * Queries the SQL session mode
  233. */
  234. function db_getSQLmode() {
  235. $result = query('SELECT @@SESSION.sql_mode;', false);
  236. if (is_resource($result)) {
  237. $row = db_fetch_row($result);
  238. return $row[0];
  239. }
  240. return false;
  241. }
  242. function db_collation() {
  243. $collation = ' CHARACTER SET utf8 COLLATE utf8_unicode_ci';
  244. return $collation;
  245. }
  246. function db_create_table(&$sql) {
  247. return query($sql, false);
  248. }
  249. function db_table_update(&$sql) {
  250. return query($sql, false);
  251. }
  252. function db_show($what, $aux = '') {
  253. global $_zp_DB_details;
  254. switch ($what) {
  255. case 'tables':
  256. $sql = "SHOW TABLES FROM `" . $_zp_DB_details['mysql_database'] . "` LIKE '" . db_LIKE_escape($_zp_DB_details['mysql_prefix']) . "%'";
  257. return query($sql, false);
  258. case 'columns':
  259. $sql = 'SHOW FULL COLUMNS FROM `' . $_zp_DB_details['mysql_prefix'] . $aux . '`';
  260. return query($sql, true);
  261. case 'variables':
  262. $sql = "SHOW VARIABLES LIKE '$aux'";
  263. return query_full_array($sql);
  264. case 'index':
  265. $sql = "SHOW INDEX FROM `" . $_zp_DB_details['mysql_database'] . '`.' . $aux;
  266. return query_full_array($sql);
  267. }
  268. }
  269. function db_list_fields($table) {
  270. $result = db_show('columns', $table);
  271. if (is_resource($result)) {
  272. $fields = array();
  273. while ($row = db_fetch_assoc($result)) {
  274. $fields[] = $row;
  275. }
  276. return $fields;
  277. } else {
  278. return false;
  279. }
  280. }
  281. function db_truncate_table($table) {
  282. global $_zp_DB_details;
  283. $sql = 'TRUNCATE ' . $_zp_DB_details['mysql_prefix'] . $table;
  284. return query($sql, false);
  285. }
  286. function db_LIKE_escape($str) {
  287. return strtr($str, array('_' => '\\_', '%' => '\\%'));
  288. }
  289. function db_free_result($result) {
  290. return mysql_free_result($result);
  291. }
  292. ?>