mysql.dbi.lib.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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
  7. * @subpackage MySQL
  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(
  19. 'PMA_MYSQL_CLIENT_API',
  20. (int)sprintf(
  21. '%d%02d%02d',
  22. $client_api[0], $client_api[1], intval($client_api[2])
  23. )
  24. );
  25. unset($client_api);
  26. }
  27. /**
  28. * Helper function for connecting to the database server
  29. *
  30. * @param array $server host/port/socket
  31. * @param string $user mysql user name
  32. * @param string $password mysql user password
  33. * @param int $client_flags client flags of connection
  34. * @param bool $persistent whether to use peristent connection
  35. *
  36. * @return mixed false on error or a mysql connection resource on success
  37. */
  38. function PMA_DBI_real_connect($server, $user, $password, $client_flags,
  39. $persistent = false
  40. ) {
  41. global $cfg;
  42. if (empty($client_flags)) {
  43. if ($cfg['PersistentConnections'] || $persistent) {
  44. $link = @mysql_pconnect($server, $user, $password);
  45. } else {
  46. $link = @mysql_connect($server, $user, $password);
  47. }
  48. } else {
  49. if ($cfg['PersistentConnections'] || $persistent) {
  50. $link = @mysql_pconnect($server, $user, $password, $client_flags);
  51. } else {
  52. $link = @mysql_connect($server, $user, $password, false, $client_flags);
  53. }
  54. }
  55. return $link;
  56. }
  57. /**
  58. * Run the multi query and output the results
  59. *
  60. * @param mysqli $link mysqli object
  61. * @param string $query multi query statement to execute
  62. *
  63. * @return boolean false always false since mysql extention not support
  64. * for multi query executions
  65. */
  66. function PMA_DBI_real_multi_query($link, $query)
  67. {
  68. // N.B.: PHP's 'mysql' extension does not support
  69. // multi_queries so this function will always
  70. // return false. Use the 'mysqli' extension, if
  71. // you need support for multi_queries.
  72. return false;
  73. }
  74. /**
  75. * connects to the database server
  76. *
  77. * @param string $user mysql user name
  78. * @param string $password mysql user password
  79. * @param bool $is_controluser whether this is a control user connection
  80. * @param array $server host/port/socket/persistent
  81. * @param bool $auxiliary_connection (when true, don't go back to login if
  82. * connection fails)
  83. *
  84. * @return mixed false on error or a mysqli object on success
  85. */
  86. function PMA_DBI_connect(
  87. $user, $password, $is_controluser = false, $server = null,
  88. $auxiliary_connection = false
  89. ) {
  90. global $cfg;
  91. if ($server) {
  92. $server_port = (empty($server['port']))
  93. ? ''
  94. : ':' . (int)$server['port'];
  95. $server_socket = (empty($server['socket']))
  96. ? ''
  97. : ':' . $server['socket'];
  98. } else {
  99. $server_port = (empty($cfg['Server']['port']))
  100. ? ''
  101. : ':' . (int)$cfg['Server']['port'];
  102. $server_socket = (empty($cfg['Server']['socket']))
  103. ? ''
  104. : ':' . $cfg['Server']['socket'];
  105. }
  106. $client_flags = 0;
  107. if (defined('PMA_ENABLE_LDI')) {
  108. // use CLIENT_LOCAL_FILES as defined in mysql_com.h
  109. // for the case where the client library was not compiled
  110. // with --enable-local-infile
  111. $client_flags |= 128;
  112. }
  113. /* Optionally compress connection */
  114. if (defined('MYSQL_CLIENT_COMPRESS') && $cfg['Server']['compress']) {
  115. $client_flags |= MYSQL_CLIENT_COMPRESS;
  116. }
  117. /* Optionally enable SSL */
  118. if (defined('MYSQL_CLIENT_SSL') && $cfg['Server']['ssl']) {
  119. $client_flags |= MYSQL_CLIENT_SSL;
  120. }
  121. if (!$server) {
  122. $link = PMA_DBI_real_connect(
  123. $cfg['Server']['host'] . $server_port . $server_socket,
  124. $user, $password, empty($client_flags) ? null : $client_flags
  125. );
  126. // Retry with empty password if we're allowed to
  127. if (empty($link) && $cfg['Server']['nopassword'] && !$is_controluser) {
  128. $link = PMA_DBI_real_connect(
  129. $cfg['Server']['host'] . $server_port . $server_socket,
  130. $user, '', empty($client_flags) ? null : $client_flags
  131. );
  132. }
  133. } else {
  134. if (!isset($server['host'])) {
  135. $link = PMA_DBI_real_connect($server_socket, $user, $password, null);
  136. } else {
  137. $link = PMA_DBI_real_connect(
  138. $server['host'] . $server_port . $server_socket,
  139. $user, $password, null
  140. );
  141. }
  142. }
  143. if (empty($link)) {
  144. if ($is_controluser) {
  145. trigger_error(
  146. __(
  147. 'Connection for controluser as defined'
  148. . ' in your configuration failed.'
  149. ),
  150. E_USER_WARNING
  151. );
  152. return false;
  153. }
  154. // we could be calling PMA_DBI_connect() to connect to another
  155. // server, for example in the Synchronize feature, so do not
  156. // go back to main login if it fails
  157. if (! $auxiliary_connection) {
  158. PMA_log_user($user, 'mysql-denied');
  159. global $auth_plugin;
  160. $auth_plugin->authFails();
  161. } else {
  162. return false;
  163. }
  164. } // end if
  165. if (! $server) {
  166. PMA_DBI_postConnect($link, $is_controluser);
  167. }
  168. return $link;
  169. }
  170. /**
  171. * selects given database
  172. *
  173. * @param string $dbname name of db to select
  174. * @param resource $link mysql link resource
  175. *
  176. * @return bool
  177. */
  178. function PMA_DBI_select_db($dbname, $link = null)
  179. {
  180. if (empty($link)) {
  181. if (isset($GLOBALS['userlink'])) {
  182. $link = $GLOBALS['userlink'];
  183. } else {
  184. return false;
  185. }
  186. }
  187. return mysql_select_db($dbname, $link);
  188. }
  189. /**
  190. * runs a query and returns the result
  191. *
  192. * @param string $query query to run
  193. * @param resource $link mysql link resource
  194. * @param int $options query options
  195. *
  196. * @return mixed
  197. */
  198. function PMA_DBI_real_query($query, $link, $options)
  199. {
  200. if ($options == ($options | PMA_DBI_QUERY_STORE)) {
  201. return mysql_query($query, $link);
  202. } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED)) {
  203. return mysql_unbuffered_query($query, $link);
  204. } else {
  205. return mysql_query($query, $link);
  206. }
  207. }
  208. /**
  209. * returns array of rows with associative and numeric keys from $result
  210. *
  211. * @param resource $result result MySQL result
  212. *
  213. * @return array
  214. */
  215. function PMA_DBI_fetch_array($result)
  216. {
  217. return mysql_fetch_array($result, MYSQL_BOTH);
  218. }
  219. /**
  220. * returns array of rows with associative keys from $result
  221. *
  222. * @param resource $result MySQL result
  223. *
  224. * @return array
  225. */
  226. function PMA_DBI_fetch_assoc($result)
  227. {
  228. return mysql_fetch_array($result, MYSQL_ASSOC);
  229. }
  230. /**
  231. * returns array of rows with numeric keys from $result
  232. *
  233. * @param resource $result MySQL result
  234. *
  235. * @return array
  236. */
  237. function PMA_DBI_fetch_row($result)
  238. {
  239. return mysql_fetch_array($result, MYSQL_NUM);
  240. }
  241. /**
  242. * Adjusts the result pointer to an arbitrary row in the result
  243. *
  244. * @param resource $result database result
  245. * @param integer $offset offset to seek
  246. *
  247. * @return bool true on success, false on failure
  248. */
  249. function PMA_DBI_data_seek($result, $offset)
  250. {
  251. return mysql_data_seek($result, $offset);
  252. }
  253. /**
  254. * Frees memory associated with the result
  255. *
  256. * @param resource $result database result
  257. *
  258. * @return void
  259. */
  260. function PMA_DBI_free_result($result)
  261. {
  262. if (is_resource($result) && get_resource_type($result) === 'mysql result') {
  263. mysql_free_result($result);
  264. }
  265. }
  266. /**
  267. * Check if there are any more query results from a multi query
  268. *
  269. * @return bool false
  270. */
  271. function PMA_DBI_more_results()
  272. {
  273. // N.B.: PHP's 'mysql' extension does not support
  274. // multi_queries so this function will always
  275. // return false. Use the 'mysqli' extension, if
  276. // you need support for multi_queries.
  277. return false;
  278. }
  279. /**
  280. * Prepare next result from multi_query
  281. *
  282. * @return boo false
  283. */
  284. function PMA_DBI_next_result()
  285. {
  286. // N.B.: PHP's 'mysql' extension does not support
  287. // multi_queries so this function will always
  288. // return false. Use the 'mysqli' extension, if
  289. // you need support for multi_queries.
  290. return false;
  291. }
  292. /**
  293. * Returns a string representing the type of connection used
  294. *
  295. * @param resource $link mysql link
  296. *
  297. * @return string type of connection used
  298. */
  299. function PMA_DBI_get_host_info($link = null)
  300. {
  301. if (null === $link) {
  302. if (isset($GLOBALS['userlink'])) {
  303. $link = $GLOBALS['userlink'];
  304. } else {
  305. return false;
  306. }
  307. }
  308. return mysql_get_host_info($link);
  309. }
  310. /**
  311. * Returns the version of the MySQL protocol used
  312. *
  313. * @param resource $link mysql link
  314. *
  315. * @return int version of the MySQL protocol used
  316. */
  317. function PMA_DBI_get_proto_info($link = null)
  318. {
  319. if (null === $link) {
  320. if (isset($GLOBALS['userlink'])) {
  321. $link = $GLOBALS['userlink'];
  322. } else {
  323. return false;
  324. }
  325. }
  326. return mysql_get_proto_info($link);
  327. }
  328. /**
  329. * returns a string that represents the client library version
  330. *
  331. * @return string MySQL client library version
  332. */
  333. function PMA_DBI_get_client_info()
  334. {
  335. return mysql_get_client_info();
  336. }
  337. /**
  338. * returns last error message or false if no errors occured
  339. *
  340. * @param resource $link mysql link
  341. *
  342. * @return string|bool $error or false
  343. */
  344. function PMA_DBI_getError($link = null)
  345. {
  346. $GLOBALS['errno'] = 0;
  347. /* Treat false same as null because of controllink */
  348. if ($link === false) {
  349. $link = null;
  350. }
  351. if (null === $link && isset($GLOBALS['userlink'])) {
  352. $link =& $GLOBALS['userlink'];
  353. // Do not stop now. On the initial connection, we don't have a $link,
  354. // we don't have a $GLOBALS['userlink'], but we can catch the error code
  355. // } else {
  356. // return false;
  357. }
  358. if (null !== $link && false !== $link) {
  359. $error_number = mysql_errno($link);
  360. $error_message = mysql_error($link);
  361. } else {
  362. $error_number = mysql_errno();
  363. $error_message = mysql_error();
  364. }
  365. if (0 == $error_number) {
  366. return false;
  367. }
  368. // keep the error number for further check after the call to PMA_DBI_getError()
  369. $GLOBALS['errno'] = $error_number;
  370. return PMA_DBI_formatError($error_number, $error_message);
  371. }
  372. /**
  373. * returns the number of rows returned by last query
  374. *
  375. * @param resource $result MySQL result
  376. *
  377. * @return string|int
  378. */
  379. function PMA_DBI_num_rows($result)
  380. {
  381. if (!is_bool($result)) {
  382. return mysql_num_rows($result);
  383. } else {
  384. return 0;
  385. }
  386. }
  387. /**
  388. * returns last inserted auto_increment id for given $link or $GLOBALS['userlink']
  389. *
  390. * @param resource $link the mysql object
  391. *
  392. * @return string|int
  393. */
  394. function PMA_DBI_insert_id($link = null)
  395. {
  396. if (empty($link)) {
  397. if (isset($GLOBALS['userlink'])) {
  398. $link = $GLOBALS['userlink'];
  399. } else {
  400. return false;
  401. }
  402. }
  403. // If the primary key is BIGINT we get an incorrect result
  404. // (sometimes negative, sometimes positive)
  405. // and in the present function we don't know if the PK is BIGINT
  406. // so better play safe and use LAST_INSERT_ID()
  407. //
  408. return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link);
  409. }
  410. /**
  411. * returns the number of rows affected by last query
  412. *
  413. * @param resource $link the mysql object
  414. * @param bool $get_from_cache whether to retrieve from cache
  415. *
  416. * @return string|int
  417. */
  418. function PMA_DBI_affected_rows($link = null, $get_from_cache = true)
  419. {
  420. if (empty($link)) {
  421. if (isset($GLOBALS['userlink'])) {
  422. $link = $GLOBALS['userlink'];
  423. } else {
  424. return false;
  425. }
  426. }
  427. if ($get_from_cache) {
  428. return $GLOBALS['cached_affected_rows'];
  429. } else {
  430. return mysql_affected_rows($link);
  431. }
  432. }
  433. /**
  434. * returns metainfo for fields in $result
  435. *
  436. * @param resource $result MySQL result
  437. *
  438. * @return array meta info for fields in $result
  439. *
  440. * @todo add missing keys like in mysqli_query (decimals)
  441. */
  442. function PMA_DBI_get_fields_meta($result)
  443. {
  444. $fields = array();
  445. $num_fields = mysql_num_fields($result);
  446. for ($i = 0; $i < $num_fields; $i++) {
  447. $field = mysql_fetch_field($result, $i);
  448. $field->flags = mysql_field_flags($result, $i);
  449. $field->orgtable = mysql_field_table($result, $i);
  450. $field->orgname = mysql_field_name($result, $i);
  451. $fields[] = $field;
  452. }
  453. return $fields;
  454. }
  455. /**
  456. * return number of fields in given $result
  457. *
  458. * @param resource $result MySQL result
  459. *
  460. * @return int field count
  461. */
  462. function PMA_DBI_num_fields($result)
  463. {
  464. return mysql_num_fields($result);
  465. }
  466. /**
  467. * returns the length of the given field $i in $result
  468. *
  469. * @param resource $result MySQL result
  470. * @param int $i field
  471. *
  472. * @return int length of field
  473. */
  474. function PMA_DBI_field_len($result, $i)
  475. {
  476. return mysql_field_len($result, $i);
  477. }
  478. /**
  479. * returns name of $i. field in $result
  480. *
  481. * @param resource $result MySQL result
  482. * @param int $i field
  483. *
  484. * @return string name of $i. field in $result
  485. */
  486. function PMA_DBI_field_name($result, $i)
  487. {
  488. return mysql_field_name($result, $i);
  489. }
  490. /**
  491. * returns concatenated string of human readable field flags
  492. *
  493. * @param resource $result MySQL result
  494. * @param int $i field
  495. *
  496. * @return string field flags
  497. */
  498. function PMA_DBI_field_flags($result, $i)
  499. {
  500. return mysql_field_flags($result, $i);
  501. }
  502. /**
  503. * Store the result returned from multi query
  504. *
  505. * @return false
  506. */
  507. function PMA_DBI_store_result()
  508. {
  509. return false;
  510. }
  511. ?>