DbiMysql.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. namespace PhpMyAdmin\Dbi;
  10. use PhpMyAdmin\Core;
  11. use PhpMyAdmin\DatabaseInterface;
  12. if (! defined('PHPMYADMIN')) {
  13. exit;
  14. }
  15. if (! extension_loaded('mysql')) {
  16. // The old MySQL extension is deprecated as of PHP 5.5.0, and will be
  17. // removed in the future. Instead, the `MySQLi` or `PDO_MySQL` extension
  18. // should be used.
  19. return;
  20. }
  21. /**
  22. * Interface to the classic MySQL extension
  23. *
  24. * @package PhpMyAdmin-DBI
  25. * @subpackage MySQL
  26. */
  27. class DbiMysql implements DbiExtension
  28. {
  29. /**
  30. * Helper function for connecting to the database server
  31. *
  32. * @param string $server host/port/socket
  33. * @param string $user mysql user name
  34. * @param string $password mysql user password
  35. * @param int $client_flags client flags of connection
  36. * @param bool $persistent whether to use persistent connection
  37. *
  38. * @return mixed false on error or a mysql connection resource on success
  39. */
  40. private function _realConnect($server, $user, $password, $client_flags,
  41. $persistent = false
  42. ) {
  43. global $cfg;
  44. if (ini_get('mysql.allow_local_infile')) {
  45. Core::fatalError(__('Please disable mysql.allow_local_infile in your PHP configuration or install the mysqli extension.'));
  46. }
  47. if (empty($client_flags)) {
  48. if ($cfg['PersistentConnections'] || $persistent) {
  49. $link = @mysql_pconnect($server, $user, $password);
  50. } else {
  51. $link = @mysql_connect($server, $user, $password);
  52. }
  53. } else {
  54. if ($cfg['PersistentConnections'] || $persistent) {
  55. $link = @mysql_pconnect($server, $user, $password, $client_flags);
  56. } else {
  57. $link = @mysql_connect(
  58. $server, $user, $password, false, $client_flags
  59. );
  60. }
  61. }
  62. return $link;
  63. }
  64. /**
  65. * Run the multi query and output the results
  66. *
  67. * @param mysqli $link mysqli object
  68. * @param string $query multi query statement to execute
  69. *
  70. * @return boolean false always false since mysql extension not support
  71. * for multi query executions
  72. */
  73. public function realMultiQuery($link, $query)
  74. {
  75. // N.B.: PHP's 'mysql' extension does not support
  76. // multi_queries so this function will always
  77. // return false. Use the 'mysqli' extension, if
  78. // you need support for multi_queries.
  79. return false;
  80. }
  81. /**
  82. * connects to the database server
  83. *
  84. * @param string $user mysql user name
  85. * @param string $password mysql user password
  86. * @param array $server host/port/socket/persistent
  87. *
  88. * @return mixed false on error or a mysqli object on success
  89. */
  90. public function connect(
  91. $user, $password, array $server
  92. ) {
  93. if ($server['port'] === 0) {
  94. $server_port = '';
  95. } else {
  96. $server_port = ':' . $server['port'];
  97. }
  98. if (is_null($server['socket'])) {
  99. $server_socket = '';
  100. } else {
  101. $server_socket = ':' . $server['socket'];
  102. }
  103. $client_flags = 0;
  104. if (defined('PMA_ENABLE_LDI')) {
  105. // use CLIENT_LOCAL_FILES as defined in mysql_com.h
  106. // for the case where the client library was not compiled
  107. // with --enable-local-infile
  108. $client_flags |= 128;
  109. }
  110. /* Optionally compress connection */
  111. if (defined('MYSQL_CLIENT_COMPRESS') && $server['compress']) {
  112. $client_flags |= MYSQL_CLIENT_COMPRESS;
  113. }
  114. /* Optionally enable SSL */
  115. if (defined('MYSQL_CLIENT_SSL') && $server['ssl']) {
  116. $client_flags |= MYSQL_CLIENT_SSL;
  117. }
  118. if (!isset($server['host'])) {
  119. $link = $this->_realConnect($server_socket, $user, $password, null);
  120. } else {
  121. $link = $this->_realConnect(
  122. $server['host'] . $server_port . $server_socket,
  123. $user, $password, null
  124. );
  125. }
  126. return $link;
  127. }
  128. /**
  129. * selects given database
  130. *
  131. * @param string $dbname name of db to select
  132. * @param resource|null $link mysql link resource
  133. *
  134. * @return bool
  135. */
  136. public function selectDb($dbname, $link)
  137. {
  138. return mysql_select_db($dbname, $link);
  139. }
  140. /**
  141. * runs a query and returns the result
  142. *
  143. * @param string $query query to run
  144. * @param resource|null $link mysql link resource
  145. * @param int $options query options
  146. *
  147. * @return mixed
  148. */
  149. public function realQuery($query, $link, $options)
  150. {
  151. if ($options == ($options | DatabaseInterface::QUERY_STORE)) {
  152. return mysql_query($query, $link);
  153. } elseif ($options == ($options | DatabaseInterface::QUERY_UNBUFFERED)) {
  154. return mysql_unbuffered_query($query, $link);
  155. }
  156. return mysql_query($query, $link);
  157. }
  158. /**
  159. * returns array of rows with associative and numeric keys from $result
  160. *
  161. * @param resource $result result MySQL result
  162. *
  163. * @return array
  164. */
  165. public function fetchArray($result)
  166. {
  167. return mysql_fetch_array($result, MYSQL_BOTH);
  168. }
  169. /**
  170. * returns array of rows with associative keys from $result
  171. *
  172. * @param resource $result MySQL result
  173. *
  174. * @return array
  175. */
  176. public function fetchAssoc($result)
  177. {
  178. return mysql_fetch_array($result, MYSQL_ASSOC);
  179. }
  180. /**
  181. * returns array of rows with numeric keys from $result
  182. *
  183. * @param resource $result MySQL result
  184. *
  185. * @return array
  186. */
  187. public function fetchRow($result)
  188. {
  189. return mysql_fetch_array($result, MYSQL_NUM);
  190. }
  191. /**
  192. * Adjusts the result pointer to an arbitrary row in the result
  193. *
  194. * @param resource $result database result
  195. * @param integer $offset offset to seek
  196. *
  197. * @return bool true on success, false on failure
  198. */
  199. public function dataSeek($result, $offset)
  200. {
  201. return mysql_data_seek($result, $offset);
  202. }
  203. /**
  204. * Frees memory associated with the result
  205. *
  206. * @param resource $result database result
  207. *
  208. * @return void
  209. */
  210. public function freeResult($result)
  211. {
  212. if (is_resource($result) && get_resource_type($result) === 'mysql result') {
  213. mysql_free_result($result);
  214. }
  215. }
  216. /**
  217. * Check if there are any more query results from a multi query
  218. *
  219. * @param resource $link the connection object
  220. *
  221. * @return bool false
  222. */
  223. public function moreResults($link)
  224. {
  225. // N.B.: PHP's 'mysql' extension does not support
  226. // multi_queries so this function will always
  227. // return false. Use the 'mysqli' extension, if
  228. // you need support for multi_queries.
  229. return false;
  230. }
  231. /**
  232. * Prepare next result from multi_query
  233. *
  234. * @param resource $link the connection object
  235. *
  236. * @return boolean false
  237. */
  238. public function nextResult($link)
  239. {
  240. // N.B.: PHP's 'mysql' extension does not support
  241. // multi_queries so this function will always
  242. // return false. Use the 'mysqli' extension, if
  243. // you need support for multi_queries.
  244. return false;
  245. }
  246. /**
  247. * Returns a string representing the type of connection used
  248. *
  249. * @param resource|null $link mysql link
  250. *
  251. * @return string type of connection used
  252. */
  253. public function getHostInfo($link)
  254. {
  255. return mysql_get_host_info($link);
  256. }
  257. /**
  258. * Returns the version of the MySQL protocol used
  259. *
  260. * @param resource|null $link mysql link
  261. *
  262. * @return int version of the MySQL protocol used
  263. */
  264. public function getProtoInfo($link)
  265. {
  266. return mysql_get_proto_info($link);
  267. }
  268. /**
  269. * returns a string that represents the client library version
  270. *
  271. * @return string MySQL client library version
  272. */
  273. public function getClientInfo()
  274. {
  275. return mysql_get_client_info();
  276. }
  277. /**
  278. * returns last error message or false if no errors occurred
  279. *
  280. * @param resource|null $link mysql link
  281. *
  282. * @return string|bool $error or false
  283. */
  284. public function getError($link)
  285. {
  286. $GLOBALS['errno'] = 0;
  287. if (null !== $link && false !== $link) {
  288. $error_number = mysql_errno($link);
  289. $error_message = mysql_error($link);
  290. } else {
  291. $error_number = mysql_errno();
  292. $error_message = mysql_error();
  293. }
  294. if (0 == $error_number) {
  295. return false;
  296. }
  297. // keep the error number for further check after
  298. // the call to getError()
  299. $GLOBALS['errno'] = $error_number;
  300. return $GLOBALS['dbi']->formatError($error_number, $error_message);
  301. }
  302. /**
  303. * returns the number of rows returned by last query
  304. *
  305. * @param resource $result MySQL result
  306. *
  307. * @return string|int
  308. */
  309. public function numRows($result)
  310. {
  311. if (is_bool($result)) {
  312. return 0;
  313. }
  314. return mysql_num_rows($result);
  315. }
  316. /**
  317. * returns the number of rows affected by last query
  318. *
  319. * @param resource|null $link the mysql object
  320. *
  321. * @return int
  322. */
  323. public function affectedRows($link)
  324. {
  325. return mysql_affected_rows($link);
  326. }
  327. /**
  328. * returns metainfo for fields in $result
  329. *
  330. * @param resource $result MySQL result
  331. *
  332. * @return array meta info for fields in $result
  333. *
  334. * @todo add missing keys like in mysqli_query (decimals)
  335. */
  336. public function getFieldsMeta($result)
  337. {
  338. $fields = array();
  339. $num_fields = mysql_num_fields($result);
  340. for ($i = 0; $i < $num_fields; $i++) {
  341. $field = mysql_fetch_field($result, $i);
  342. $field->flags = mysql_field_flags($result, $i);
  343. $field->orgtable = mysql_field_table($result, $i);
  344. $field->orgname = mysql_field_name($result, $i);
  345. $fields[] = $field;
  346. }
  347. return $fields;
  348. }
  349. /**
  350. * return number of fields in given $result
  351. *
  352. * @param resource $result MySQL result
  353. *
  354. * @return int field count
  355. */
  356. public function numFields($result)
  357. {
  358. return mysql_num_fields($result);
  359. }
  360. /**
  361. * returns the length of the given field $i in $result
  362. *
  363. * @param resource $result MySQL result
  364. * @param int $i field
  365. *
  366. * @return int length of field
  367. */
  368. public function fieldLen($result, $i)
  369. {
  370. return mysql_field_len($result, $i);
  371. }
  372. /**
  373. * returns name of $i. field in $result
  374. *
  375. * @param resource $result MySQL result
  376. * @param int $i field
  377. *
  378. * @return string name of $i. field in $result
  379. */
  380. public function fieldName($result, $i)
  381. {
  382. return mysql_field_name($result, $i);
  383. }
  384. /**
  385. * returns concatenated string of human readable field flags
  386. *
  387. * @param resource $result MySQL result
  388. * @param int $i field
  389. *
  390. * @return string field flags
  391. */
  392. public function fieldFlags($result, $i)
  393. {
  394. return mysql_field_flags($result, $i);
  395. }
  396. /**
  397. * Store the result returned from multi query
  398. *
  399. * @param resource $result MySQL result
  400. *
  401. * @return false
  402. */
  403. public function storeResult($result)
  404. {
  405. return false;
  406. }
  407. /**
  408. * returns properly escaped string for use in MySQL queries
  409. *
  410. * @param mixed $link database link
  411. * @param string $str string to be escaped
  412. *
  413. * @return string a MySQL escaped string
  414. */
  415. public function escapeString($link, $str)
  416. {
  417. return mysql_real_escape_string($str, $link);
  418. }
  419. }