PageRenderTime 46ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

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