PageRenderTime 27ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/sitemanager/phpmyadmin/libraries/dbi/mysql.dbi.lib.php

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