PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/public/phpmyadmin/libraries/dbi/DBIMysql.class.php

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