PageRenderTime 48ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/manage/phpmyadminlite/libraries/dbi/mysql.dbi.lib.php

https://gitlab.com/albert925/lading-ach
PHP | 450 lines | 292 code | 47 blank | 111 comment | 79 complexity | 1607d948373b812650be275f10daeb8c MD5 | raw file
  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$
  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, $persistant=false)
  22. {
  23. global $cfg;
  24. if (empty($client_flags)) {
  25. if ($cfg['PersistentConnections'] || $persistant) {
  26. $link = @mysql_pconnect($server, $user, $password);
  27. } else {
  28. $link = @mysql_connect($server, $user, $password);
  29. }
  30. } else {
  31. if ($cfg['PersistentConnections'] || $persistant) {
  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. /**
  40. * @param string $user mysql user name
  41. * @param string $password mysql user password
  42. * @param boolean $is_controluser
  43. * @param array $server host/port/socket/persistant
  44. * @param boolean $auxiliary_connection (when true, don't go back to login if connection fails)
  45. * @return mixed false on error or a mysqli object on success
  46. */
  47. function PMA_DBI_connect($user, $password, $is_controluser = false, $server = null, $auxiliary_connection = false)
  48. {
  49. global $cfg, $php_errormsg;
  50. if ($server) {
  51. $server_port = (empty($server['port']))
  52. ? ''
  53. : ':' . (int)$server['port'];
  54. $server_socket = (empty($server['socket']))
  55. ? ''
  56. : ':' . $server['socket'];
  57. $server_persistant = (empty($server['persistant']))
  58. ? false
  59. : true;
  60. } else {
  61. $server_port = (empty($cfg['Server']['port']))
  62. ? ''
  63. : ':' . (int)$cfg['Server']['port'];
  64. $server_socket = (empty($cfg['Server']['socket']))
  65. ? ''
  66. : ':' . $cfg['Server']['socket'];
  67. }
  68. if (strtolower($cfg['Server']['connect_type']) == 'tcp') {
  69. $cfg['Server']['socket'] = '';
  70. }
  71. $client_flags = 0;
  72. // always use CLIENT_LOCAL_FILES as defined in mysql_com.h
  73. // for the case where the client library was not compiled
  74. // with --enable-local-infile
  75. $client_flags |= 128;
  76. /* Optionally compress connection */
  77. if (defined('MYSQL_CLIENT_COMPRESS') && $cfg['Server']['compress']) {
  78. $client_flags |= MYSQL_CLIENT_COMPRESS;
  79. }
  80. /* Optionally enable SSL */
  81. if (defined('MYSQL_CLIENT_SSL') && $cfg['Server']['ssl']) {
  82. $client_flags |= MYSQL_CLIENT_SSL;
  83. }
  84. if (!$server) {
  85. $link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, $password, empty($client_flags) ? NULL : $client_flags);
  86. // Retry with empty password if we're allowed to
  87. if (empty($link) && $cfg['Server']['nopassword'] && !$is_controluser) {
  88. $link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, '', empty($client_flags) ? NULL : $client_flags);
  89. }
  90. } else {
  91. if (!isset($server['host'])) {
  92. $link = PMA_DBI_real_connect($server_socket, $user, $password, NULL, $server_persistant);
  93. } else {
  94. $link = PMA_DBI_real_connect($server['host'] . $server_port . $server_socket, $user, $password, NULL, $server_persistant);
  95. }
  96. }
  97. if (empty($link)) {
  98. if ($is_controluser) {
  99. trigger_error($GLOBALS['strControluserFailed'], E_USER_WARNING);
  100. return false;
  101. }
  102. // we could be calling PMA_DBI_connect() to connect to another
  103. // server, for example in the Synchronize feature, so do not
  104. // go back to main login if it fails
  105. if (! $auxiliary_connection) {
  106. PMA_log_user($user, 'mysql-denied');
  107. PMA_auth_fails();
  108. } else {
  109. return false;
  110. }
  111. } // end if
  112. if (! $server) {
  113. PMA_DBI_postConnect($link, $is_controluser);
  114. }
  115. return $link;
  116. }
  117. /**
  118. * select a db
  119. *
  120. * @param string $dbname name of db to select
  121. * @param resource $link mysql link resource
  122. * @return boolean success
  123. */
  124. function PMA_DBI_select_db($dbname, $link = null)
  125. {
  126. if (empty($link)) {
  127. if (isset($GLOBALS['userlink'])) {
  128. $link = $GLOBALS['userlink'];
  129. } else {
  130. return false;
  131. }
  132. }
  133. return mysql_select_db($dbname, $link);
  134. }
  135. /**
  136. * runs a query and returns the result
  137. *
  138. * @param string $query query to run
  139. * @param resource $link mysql link resource
  140. * @param integer $options
  141. * @return mixed
  142. */
  143. function PMA_DBI_try_query($query, $link = null, $options = 0)
  144. {
  145. if (empty($link)) {
  146. if (isset($GLOBALS['userlink'])) {
  147. $link = $GLOBALS['userlink'];
  148. } else {
  149. return false;
  150. }
  151. }
  152. if ($GLOBALS['cfg']['DBG']['sql']) {
  153. $time = microtime(true);
  154. }
  155. if ($options == ($options | PMA_DBI_QUERY_STORE)) {
  156. $r = mysql_query($query, $link);
  157. } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED)) {
  158. $r = mysql_unbuffered_query($query, $link);
  159. } else {
  160. $r = mysql_query($query, $link);
  161. }
  162. if ($GLOBALS['cfg']['DBG']['sql']) {
  163. $time = microtime(true) - $time;
  164. $hash = md5($query);
  165. if (isset($_SESSION['debug']['queries'][$hash])) {
  166. $_SESSION['debug']['queries'][$hash]['count']++;
  167. } else {
  168. $_SESSION['debug']['queries'][$hash] = array();
  169. $_SESSION['debug']['queries'][$hash]['count'] = 1;
  170. $_SESSION['debug']['queries'][$hash]['query'] = $query;
  171. $_SESSION['debug']['queries'][$hash]['time'] = $time;
  172. }
  173. $trace = array();
  174. foreach (debug_backtrace() as $trace_step) {
  175. $trace[] = PMA_Error::relPath($trace_step['file']) . '#'
  176. . $trace_step['line'] . ': '
  177. . (isset($trace_step['class']) ? $trace_step['class'] : '')
  178. //. (isset($trace_step['object']) ? get_class($trace_step['object']) : '')
  179. . (isset($trace_step['type']) ? $trace_step['type'] : '')
  180. . (isset($trace_step['function']) ? $trace_step['function'] : '')
  181. . '('
  182. . (isset($trace_step['params']) ? implode(', ', $trace_step['params']) : '')
  183. . ')'
  184. ;
  185. }
  186. $_SESSION['debug']['queries'][$hash]['trace'][] = $trace;
  187. }
  188. if ($r != FALSE && PMA_Tracker::isActive() == TRUE ) {
  189. PMA_Tracker::handleQuery($query);
  190. }
  191. return $r;
  192. }
  193. function PMA_DBI_fetch_array($result)
  194. {
  195. return mysql_fetch_array($result, MYSQL_BOTH);
  196. }
  197. function PMA_DBI_fetch_assoc($result) {
  198. return mysql_fetch_array($result, MYSQL_ASSOC);
  199. }
  200. function PMA_DBI_fetch_row($result)
  201. {
  202. return mysql_fetch_array($result, MYSQL_NUM);
  203. }
  204. /*
  205. * Adjusts the result pointer to an arbitrary row in the result
  206. *
  207. * @uses mysql_data_seek()
  208. * @param $result
  209. * @param $offset
  210. * @return boolean true on success, false on failure
  211. */
  212. function PMA_DBI_data_seek($result, $offset)
  213. {
  214. return mysql_data_seek($result, $offset);
  215. }
  216. /**
  217. * Frees the memory associated with the results
  218. *
  219. * @param result $result,... one or more mysql result resources
  220. */
  221. function PMA_DBI_free_result()
  222. {
  223. foreach (func_get_args() as $result) {
  224. if (is_resource($result)
  225. && get_resource_type($result) === 'mysql result') {
  226. mysql_free_result($result);
  227. }
  228. }
  229. }
  230. /**
  231. * Returns a string representing the type of connection used
  232. * @uses mysql_get_host_info()
  233. * @uses $GLOBALS['userlink'] as default for $link
  234. * @param resource $link mysql link
  235. * @return string type of connection used
  236. */
  237. function PMA_DBI_get_host_info($link = null)
  238. {
  239. if (null === $link) {
  240. if (isset($GLOBALS['userlink'])) {
  241. $link = $GLOBALS['userlink'];
  242. } else {
  243. return false;
  244. }
  245. }
  246. return mysql_get_host_info($link);
  247. }
  248. /**
  249. * Returns the version of the MySQL protocol used
  250. * @uses mysql_get_proto_info()
  251. * @uses $GLOBALS['userlink'] as default for $link
  252. * @param resource $link mysql link
  253. * @return integer version of the MySQL protocol used
  254. */
  255. function PMA_DBI_get_proto_info($link = null)
  256. {
  257. if (null === $link) {
  258. if (isset($GLOBALS['userlink'])) {
  259. $link = $GLOBALS['userlink'];
  260. } else {
  261. return false;
  262. }
  263. }
  264. return mysql_get_proto_info($link);
  265. }
  266. /**
  267. * returns a string that represents the client library version
  268. * @uses mysql_get_client_info()
  269. * @return string MySQL client library version
  270. */
  271. function PMA_DBI_get_client_info()
  272. {
  273. return mysql_get_client_info();
  274. }
  275. /**
  276. * returns last error message or false if no errors occured
  277. *
  278. * @uses PMA_DBI_convert_message()
  279. * @uses $GLOBALS['errno']
  280. * @uses $GLOBALS['userlink']
  281. * @uses $GLOBALS['strServerNotResponding']
  282. * @uses $GLOBALS['strSocketProblem']
  283. * @uses $GLOBALS['strDetails']
  284. * @uses mysql_errno()
  285. * @uses mysql_error()
  286. * @uses defined()
  287. * @uses PMA_generate_common_url()
  288. * @param resource $link mysql link
  289. * @return string|boolean $error or false
  290. */
  291. function PMA_DBI_getError($link = null)
  292. {
  293. $GLOBALS['errno'] = 0;
  294. /* Treat false same as null because of controllink */
  295. if ($link === false) {
  296. $link = null;
  297. }
  298. if (null === $link && isset($GLOBALS['userlink'])) {
  299. $link =& $GLOBALS['userlink'];
  300. // Do not stop now. On the initial connection, we don't have a $link,
  301. // we don't have a $GLOBALS['userlink'], but we can catch the error code
  302. // } else {
  303. // return false;
  304. }
  305. if (null !== $link && false !== $link) {
  306. $error_number = mysql_errno($link);
  307. $error_message = mysql_error($link);
  308. } else {
  309. $error_number = mysql_errno();
  310. $error_message = mysql_error();
  311. }
  312. if (0 == $error_number) {
  313. return false;
  314. }
  315. // keep the error number for further check after the call to PMA_DBI_getError()
  316. $GLOBALS['errno'] = $error_number;
  317. if (! empty($error_message)) {
  318. $error_message = PMA_DBI_convert_message($error_message);
  319. }
  320. $error_message = htmlspecialchars($error_message);
  321. // Some errors messages cannot be obtained by mysql_error()
  322. if ($error_number == 2002) {
  323. $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'] . ' ' . $GLOBALS['strSocketProblem'];
  324. } elseif ($error_number == 2003) {
  325. $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'];
  326. } elseif ($error_number == 1005) {
  327. /* InnoDB contraints, see
  328. * http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html
  329. */
  330. $error = '#' . ((string) $error_number) . ' - ' . $error_message .
  331. ' (<a href="server_engines.php' . PMA_generate_common_url(array('engine' => 'InnoDB', 'page' => 'Status')).
  332. '">' . $GLOBALS['strDetails'] . '</a>)';
  333. } else {
  334. $error = '#' . ((string) $error_number) . ' - ' . $error_message;
  335. }
  336. return $error;
  337. }
  338. function PMA_DBI_num_rows($result)
  339. {
  340. if (!is_bool($result)) {
  341. return mysql_num_rows($result);
  342. } else {
  343. return 0;
  344. }
  345. }
  346. function PMA_DBI_insert_id($link = null)
  347. {
  348. if (empty($link)) {
  349. if (isset($GLOBALS['userlink'])) {
  350. $link = $GLOBALS['userlink'];
  351. } else {
  352. return false;
  353. }
  354. }
  355. //$insert_id = mysql_insert_id($link);
  356. // if the primary key is BIGINT we get an incorrect result
  357. // (sometimes negative, sometimes positive)
  358. // and in the present function we don't know if the PK is BIGINT
  359. // so better play safe and use LAST_INSERT_ID()
  360. //
  361. // by the way, no problem with mysqli_insert_id()
  362. return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link);
  363. }
  364. function PMA_DBI_affected_rows($link = null)
  365. {
  366. if (empty($link)) {
  367. if (isset($GLOBALS['userlink'])) {
  368. $link = $GLOBALS['userlink'];
  369. } else {
  370. return false;
  371. }
  372. }
  373. return mysql_affected_rows($link);
  374. }
  375. /**
  376. * @todo add missing keys like in from mysqli_query (orgname, orgtable, flags, decimals)
  377. */
  378. function PMA_DBI_get_fields_meta($result)
  379. {
  380. $fields = array();
  381. $num_fields = mysql_num_fields($result);
  382. for ($i = 0; $i < $num_fields; $i++) {
  383. $fields[] = mysql_fetch_field($result, $i);
  384. }
  385. return $fields;
  386. }
  387. function PMA_DBI_num_fields($result)
  388. {
  389. return mysql_num_fields($result);
  390. }
  391. function PMA_DBI_field_len($result, $i)
  392. {
  393. return mysql_field_len($result, $i);
  394. }
  395. function PMA_DBI_field_name($result, $i)
  396. {
  397. return mysql_field_name($result, $i);
  398. }
  399. function PMA_DBI_field_flags($result, $i)
  400. {
  401. return mysql_field_flags($result, $i);
  402. }
  403. ?>