PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/bin/phpMyAdminForPHP5/libraries/dbi/mysql.dbi.lib.php

https://github.com/racontemoi/chi_mac
PHP | 407 lines | 259 code | 49 blank | 99 comment | 63 complexity | 99580f65d82977bb621bfa88a3ddec8e MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, AGPL-1.0, BSD-3-Clause, LGPL-2.1, BSD-2-Clause, GPL-2.0
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Interface to the classic MySQL extension
  5. *
  6. * @package phpMyAdmin-DBI-MySQL
  7. * @version $Id: mysql.dbi.lib.php 12283 2009-03-03 16:20:41Z nijel $
  8. */
  9. if (! defined('PHPMYADMIN')) {
  10. exit;
  11. }
  12. require_once './libraries/logging.lib.php';
  13. /**
  14. * MySQL client API
  15. */
  16. if (! defined('PMA_MYSQL_CLIENT_API')) {
  17. $client_api = explode('.', mysql_get_client_info());
  18. define('PMA_MYSQL_CLIENT_API', (int)sprintf('%d%02d%02d', $client_api[0], $client_api[1], intval($client_api[2])));
  19. unset($client_api);
  20. }
  21. function PMA_DBI_real_connect($server, $user, $password, $client_flags)
  22. {
  23. global $cfg;
  24. if (empty($client_flags)) {
  25. if ($cfg['PersistentConnections']) {
  26. $link = @mysql_pconnect($server, $user, $password);
  27. } else {
  28. $link = @mysql_connect($server, $user, $password);
  29. }
  30. } else {
  31. if ($cfg['PersistentConnections']) {
  32. $link = @mysql_pconnect($server, $user, $password, $client_flags);
  33. } else {
  34. $link = @mysql_connect($server, $user, $password, false, $client_flags);
  35. }
  36. }
  37. return $link;
  38. }
  39. function PMA_DBI_connect($user, $password, $is_controluser = false)
  40. {
  41. global $cfg, $php_errormsg;
  42. $server_port = (empty($cfg['Server']['port']))
  43. ? ''
  44. : ':' . $cfg['Server']['port'];
  45. if (strtolower($cfg['Server']['connect_type']) == 'tcp') {
  46. $cfg['Server']['socket'] = '';
  47. }
  48. $server_socket = (empty($cfg['Server']['socket']))
  49. ? ''
  50. : ':' . $cfg['Server']['socket'];
  51. $client_flags = 0;
  52. // always use CLIENT_LOCAL_FILES as defined in mysql_com.h
  53. // for the case where the client library was not compiled
  54. // with --enable-local-infile
  55. $client_flags |= 128;
  56. /* Optionally compress connection */
  57. if (defined('MYSQL_CLIENT_COMPRESS') && $cfg['Server']['compress']) {
  58. $client_flags |= MYSQL_CLIENT_COMPRESS;
  59. }
  60. /* Optionally enable SSL */
  61. if (defined('MYSQL_CLIENT_SSL') && $cfg['Server']['ssl']) {
  62. $client_flags |= MYSQL_CLIENT_SSL;
  63. }
  64. $link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, $password, empty($client_flags) ? NULL : $client_flags);
  65. // Retry with empty password if we're allowed to
  66. if (empty($link) && $cfg['Server']['nopassword'] && !$is_controluser) {
  67. $link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, '', empty($client_flags) ? NULL : $client_flags);
  68. }
  69. if (empty($link)) {
  70. if ($is_controluser) {
  71. trigger_error($GLOBALS['strControluserFailed'], E_USER_WARNING);
  72. return false;
  73. }
  74. PMA_log_user($user, 'mysql-denied');
  75. PMA_auth_fails();
  76. } // end if
  77. PMA_DBI_postConnect($link, $is_controluser);
  78. return $link;
  79. }
  80. /**
  81. * select a db
  82. *
  83. * @param string $dbname name of db to select
  84. * @param resource $link mysql link resource
  85. * @return boolean success
  86. */
  87. function PMA_DBI_select_db($dbname, $link = null)
  88. {
  89. if (empty($link)) {
  90. if (isset($GLOBALS['userlink'])) {
  91. $link = $GLOBALS['userlink'];
  92. } else {
  93. return false;
  94. }
  95. }
  96. return mysql_select_db($dbname, $link);
  97. }
  98. /**
  99. * runs a query and returns the result
  100. *
  101. * @param string $query query to run
  102. * @param resource $link mysql link resource
  103. * @param integer $options
  104. * @return mixed
  105. */
  106. function PMA_DBI_try_query($query, $link = null, $options = 0)
  107. {
  108. if (empty($link)) {
  109. if (isset($GLOBALS['userlink'])) {
  110. $link = $GLOBALS['userlink'];
  111. } else {
  112. return false;
  113. }
  114. }
  115. if ($GLOBALS['cfg']['DBG']['sql']) {
  116. $time = microtime(true);
  117. }
  118. if ($options == ($options | PMA_DBI_QUERY_STORE)) {
  119. $r = mysql_query($query, $link);
  120. } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED)) {
  121. $r = mysql_unbuffered_query($query, $link);
  122. } else {
  123. $r = mysql_query($query, $link);
  124. }
  125. if ($GLOBALS['cfg']['DBG']['sql']) {
  126. $time = microtime(true) - $time;
  127. $hash = md5($query);
  128. if (isset($_SESSION['debug']['queries'][$hash])) {
  129. $_SESSION['debug']['queries'][$hash]['count']++;
  130. } else {
  131. $_SESSION['debug']['queries'][$hash] = array();
  132. $_SESSION['debug']['queries'][$hash]['count'] = 1;
  133. $_SESSION['debug']['queries'][$hash]['query'] = $query;
  134. $_SESSION['debug']['queries'][$hash]['time'] = $time;
  135. }
  136. $trace = array();
  137. foreach (debug_backtrace() as $trace_step) {
  138. $trace[] = PMA_Error::relPath($trace_step['file']) . '#'
  139. . $trace_step['line'] . ': '
  140. . (isset($trace_step['class']) ? $trace_step['class'] : '')
  141. //. (isset($trace_step['object']) ? get_class($trace_step['object']) : '')
  142. . (isset($trace_step['type']) ? $trace_step['type'] : '')
  143. . (isset($trace_step['function']) ? $trace_step['function'] : '')
  144. . '('
  145. . (isset($trace_step['params']) ? implode(', ', $trace_step['params']) : '')
  146. . ')'
  147. ;
  148. }
  149. $_SESSION['debug']['queries'][$hash]['trace'][] = $trace;
  150. }
  151. return $r;
  152. }
  153. function PMA_DBI_fetch_array($result)
  154. {
  155. return mysql_fetch_array($result, MYSQL_BOTH);
  156. }
  157. function PMA_DBI_fetch_assoc($result) {
  158. return mysql_fetch_array($result, MYSQL_ASSOC);
  159. }
  160. function PMA_DBI_fetch_row($result)
  161. {
  162. return mysql_fetch_array($result, MYSQL_NUM);
  163. }
  164. /*
  165. * Adjusts the result pointer to an arbitrary row in the result
  166. *
  167. * @uses mysql_data_seek()
  168. * @param $result
  169. * @param $offset
  170. * @return boolean true on success, false on failure
  171. */
  172. function PMA_DBI_data_seek($result, $offset)
  173. {
  174. return mysql_data_seek($result, $offset);
  175. }
  176. /**
  177. * Frees the memory associated with the results
  178. *
  179. * @param result $result,... one or more mysql result resources
  180. */
  181. function PMA_DBI_free_result()
  182. {
  183. foreach (func_get_args() as $result) {
  184. if (is_resource($result)
  185. && get_resource_type($result) === 'mysql result') {
  186. mysql_free_result($result);
  187. }
  188. }
  189. }
  190. /**
  191. * Returns a string representing the type of connection used
  192. * @uses mysql_get_host_info()
  193. * @uses $GLOBALS['userlink'] as default for $link
  194. * @param resource $link mysql link
  195. * @return string type of connection used
  196. */
  197. function PMA_DBI_get_host_info($link = null)
  198. {
  199. if (null === $link) {
  200. if (isset($GLOBALS['userlink'])) {
  201. $link = $GLOBALS['userlink'];
  202. } else {
  203. return false;
  204. }
  205. }
  206. return mysql_get_host_info($link);
  207. }
  208. /**
  209. * Returns the version of the MySQL protocol used
  210. * @uses mysql_get_proto_info()
  211. * @uses $GLOBALS['userlink'] as default for $link
  212. * @param resource $link mysql link
  213. * @return integer version of the MySQL protocol used
  214. */
  215. function PMA_DBI_get_proto_info($link = null)
  216. {
  217. if (null === $link) {
  218. if (isset($GLOBALS['userlink'])) {
  219. $link = $GLOBALS['userlink'];
  220. } else {
  221. return false;
  222. }
  223. }
  224. return mysql_get_proto_info($link);
  225. }
  226. /**
  227. * returns a string that represents the client library version
  228. * @uses mysql_get_client_info()
  229. * @return string MySQL client library version
  230. */
  231. function PMA_DBI_get_client_info()
  232. {
  233. return mysql_get_client_info();
  234. }
  235. /**
  236. * returns last error message or false if no errors occured
  237. *
  238. * @uses PMA_DBI_convert_message()
  239. * @uses $GLOBALS['errno']
  240. * @uses $GLOBALS['userlink']
  241. * @uses $GLOBALS['strServerNotResponding']
  242. * @uses $GLOBALS['strSocketProblem']
  243. * @uses $GLOBALS['strDetails']
  244. * @uses mysql_errno()
  245. * @uses mysql_error()
  246. * @uses defined()
  247. * @uses PMA_generate_common_url()
  248. * @param resource $link mysql link
  249. * @return string|boolean $error or false
  250. */
  251. function PMA_DBI_getError($link = null)
  252. {
  253. $GLOBALS['errno'] = 0;
  254. if (null === $link && isset($GLOBALS['userlink'])) {
  255. $link =& $GLOBALS['userlink'];
  256. // Do not stop now. On the initial connection, we don't have a $link,
  257. // we don't have a $GLOBALS['userlink'], but we can catch the error code
  258. // } else {
  259. // return false;
  260. }
  261. if (null !== $link && false !== $link) {
  262. $error_number = mysql_errno($link);
  263. $error_message = mysql_error($link);
  264. } else {
  265. $error_number = mysql_errno();
  266. $error_message = mysql_error();
  267. }
  268. if (0 == $error_number) {
  269. return false;
  270. }
  271. // keep the error number for further check after the call to PMA_DBI_getError()
  272. $GLOBALS['errno'] = $error_number;
  273. if (! empty($error_message)) {
  274. $error_message = PMA_DBI_convert_message($error_message);
  275. }
  276. // Some errors messages cannot be obtained by mysql_error()
  277. if ($error_number == 2002) {
  278. $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'] . ' ' . $GLOBALS['strSocketProblem'];
  279. } elseif ($error_number == 2003) {
  280. $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'];
  281. } elseif ($error_number == 1005) {
  282. /* InnoDB contraints, see
  283. * http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html
  284. */
  285. $error = '#' . ((string) $error_number) . ' - ' . $error_message .
  286. ' (<a href="server_engines.php' . PMA_generate_common_url(array('engine' => 'InnoDB', 'page' => 'Status')).
  287. '">' . $GLOBALS['strDetails'] . '</a>)';
  288. } else {
  289. $error = '#' . ((string) $error_number) . ' - ' . $error_message;
  290. }
  291. return $error;
  292. }
  293. function PMA_DBI_num_rows($result)
  294. {
  295. if (!is_bool($result)) {
  296. return mysql_num_rows($result);
  297. } else {
  298. return 0;
  299. }
  300. }
  301. function PMA_DBI_insert_id($link = null)
  302. {
  303. if (empty($link)) {
  304. if (isset($GLOBALS['userlink'])) {
  305. $link = $GLOBALS['userlink'];
  306. } else {
  307. return false;
  308. }
  309. }
  310. //$insert_id = mysql_insert_id($link);
  311. // if the primary key is BIGINT we get an incorrect result
  312. // (sometimes negative, sometimes positive)
  313. // and in the present function we don't know if the PK is BIGINT
  314. // so better play safe and use LAST_INSERT_ID()
  315. //
  316. // by the way, no problem with mysqli_insert_id()
  317. return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link);
  318. }
  319. function PMA_DBI_affected_rows($link = null)
  320. {
  321. if (empty($link)) {
  322. if (isset($GLOBALS['userlink'])) {
  323. $link = $GLOBALS['userlink'];
  324. } else {
  325. return false;
  326. }
  327. }
  328. return mysql_affected_rows($link);
  329. }
  330. /**
  331. * @todo add missing keys like in from mysqli_query (orgname, orgtable, flags, decimals)
  332. */
  333. function PMA_DBI_get_fields_meta($result)
  334. {
  335. $fields = array();
  336. $num_fields = mysql_num_fields($result);
  337. for ($i = 0; $i < $num_fields; $i++) {
  338. $fields[] = mysql_fetch_field($result, $i);
  339. }
  340. return $fields;
  341. }
  342. function PMA_DBI_num_fields($result)
  343. {
  344. return mysql_num_fields($result);
  345. }
  346. function PMA_DBI_field_len($result, $i)
  347. {
  348. return mysql_field_len($result, $i);
  349. }
  350. function PMA_DBI_field_name($result, $i)
  351. {
  352. return mysql_field_name($result, $i);
  353. }
  354. function PMA_DBI_field_flags($result, $i)
  355. {
  356. return mysql_field_flags($result, $i);
  357. }
  358. ?>