DBIMysql.class.php 16 KB

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