2.2.4
[phpmyadmin/arisferyanto.git] / libraries / common.lib.php3
blob1db194ed783d1fdb91559f003d42d3ab596b9253
1 <?php
2 /* $Id$ */
5 /**
6 * Misc stuff and functions used by almost all the scripts.
7 * Among other things, it contains the advanced authentification work.
8 */
12 if (!defined('PMA_COMMON_LIB_INCLUDED')){
13 define('PMA_COMMON_LIB_INCLUDED', 1);
15 /**
16 * Order of sections for common.lib.php3:
18 * in PHP3, functions and constants must be physically defined
19 * before they are referenced
21 * some functions need the constants of libraries/defines.lib.php3
23 * the PMA_setFontSizes() function must be before the call to the
24 * libraries/auth/cookie.auth.lib.php3 library
26 * the include of libraries/defines.lib.php3 must be after the connection
27 * to db to get the MySql version
29 * the PMA_sqlAddslashes() function must be before the connection to db
31 * the authentication libraries must be before the connection to db but
32 * after the PMA_isInto() function
34 * the PMA_mysqlDie() function must be before the connection to db but after
35 * mysql extension has been loaded
37 * ... so the required order is:
39 * - parsing of the configuration file
40 * - first load of the libraries/define.lib.php3 library (won't get the
41 * MySQL release number)
42 * - load of mysql extension (if necessary)
43 * - definition of PMA_sqlAddslashes()
44 * - definition of PMA_mysqlDie()
45 * - definition of PMA_isInto()
46 * - definition of PMA_setFontSizes()
47 * - loading of an authentication library
48 * - db connection
49 * - authentication work
50 * - second load of the libraries/define.lib.php3 library to get the MySQL
51 * release number)
52 * - other functions, respecting dependencies
56 /**
57 * Avoids undefined variables in PHP3
59 if (!isset($use_backquotes)) {
60 $use_backquotes = 0;
62 if (!isset($pos)) {
63 $pos = 0;
67 /**
68 * Parses the configuration file and gets some constants used to define
69 * versions of phpMyAdmin/php/mysql...
71 include('./config.inc.php3');
73 // For compatibility with old config.inc.php3
74 if (!isset($cfgExecTimeLimit)) {
75 $cfgExecTimeLimit = 300; // 5 minuts
77 if (!isset($cfgShowStats)) {
78 $cfgShowStats = TRUE;
80 if (!isset($cfgLoginCookieRecall)) {
81 $cfgLoginCookieRecall = TRUE;
83 if (!isset($cfgShowTooltip)) {
84 $cfgShowTooltip = TRUE;
86 if (!isset($cfgShowMysqlInfo)) {
87 $cfgShowMysqlInfo = FALSE;
89 if (!isset($cfgShowMysqlVars)) {
90 $cfgShowMysqlVars = FALSE;
92 if (!isset($cfgShowPhpInfo)) {
93 $cfgShowPhpInfo = FALSE;
95 if (!isset($cfgShowAll)) {
96 $cfgShowAll = FALSE;
98 if (!isset($cfgNavigationBarIconic)) {
99 $cfgNavigationBarIconic = TRUE;
101 if (!isset($cfgProtectBinary)) {
102 if (isset($cfgProtectBlob)) {
103 $cfgProtectBinary = ($cfgProtectBlob ? 'blob' : FALSE);
104 unset($cfgProtectBlob);
105 } else {
106 $cfgProtectBinary = 'blob';
109 if (!isset($cfgShowFunctionFields)) {
110 $cfgShowFunctionFields = TRUE;
112 if (!isset($cfgZipDump)) {
113 $cfgZipDump = (isset($cfgGZipDump) ? $cfgGZipDump : TRUE);
115 if (!isset($cfgLeftBgColor)) {
116 $cfgLeftBgColor = '#D0DCE0';
118 if (!isset($cfgLeftPointerColor)) {
119 $cfgLeftPointerColor = '';
121 if (!isset($cfgRightBgColor)) {
122 $cfgRightBgColor = '#F5F5F5';
124 if (!isset($cfgBrowsePointerColor)) {
125 $cfgBrowsePointerColor = '';
127 if (!isset($cfgBrowseMarkRow)) {
128 $cfgBrowseMarkRow = 0;
130 if (!isset($cfgTextareaCols)) {
131 $cfgTextareaCols = 40;
133 if (!isset($cfgTextareaRows)) {
134 $cfgTextareaRows = 7;
136 if (!isset($cfgDefaultDisplay)) {
137 $cfgDefaultDisplay = 'horizontal';
139 if (!isset($cfgRepeatCells)) {
140 $cfgRepeatCells = 100;
142 if (!isset($cfgLeftFrameLight)) {
143 $cfgLeftFrameLight = TRUE;
146 // Adds a trailing slash et the end of the phpMyAdmin uri if it does not
147 // exist
148 if ($cfgPmaAbsoluteUri != '' && substr($cfgPmaAbsoluteUri, -1) != '/') {
149 $cfgPmaAbsoluteUri .= '/';
152 // Gets some constants
153 include('./libraries/defines.lib.php3');
155 // If zlib output compression is set in the php configuration file, no
156 // output buffering should be run
157 if (PMA_PHP_INT_VERSION < 40000
158 || (PMA_PHP_INT_VERSION >= 40005 && @ini_get('zlib.output_compression'))) {
159 $cfgOBGzip = FALSE;
164 * Loads the mysql extensions if it is not loaded yet
165 * staybyte - 26. June 2001
167 if (((PMA_PHP_INT_VERSION >= 40000 && !@ini_get('safe_mode') && @ini_get('enable_dl'))
168 || (PMA_PHP_INT_VERSION > 30009 && !@get_cfg_var('safe_mode')))
169 && @function_exists('dl')) {
170 if (PMA_PHP_INT_VERSION < 40000) {
171 $extension = 'MySQL';
172 } else {
173 $extension = 'mysql';
175 if (PMA_IS_WINDOWS) {
176 $suffix = '.dll';
177 } else {
178 $suffix = '.so';
180 if (!@extension_loaded($extension)) {
181 @dl($extension.$suffix);
183 if (!@extension_loaded($extension)) {
184 echo $strCantLoadMySQL;
185 exit();
187 } // end load mysql extension
191 * Add slashes before "'" and "\" characters so a value containing them can
192 * be used in a sql comparison.
194 * @param string the string to slash
195 * @param boolean whether the string will be used in a 'LIKE' clause
196 * (it then requires two more escaped sequences) or not
198 * @return string the slashed string
200 * @access public
202 function PMA_sqlAddslashes($a_string = '', $is_like = FALSE)
204 if ($is_like) {
205 $a_string = str_replace('\\', '\\\\\\\\', $a_string);
206 } else {
207 $a_string = str_replace('\\', '\\\\', $a_string);
209 $a_string = str_replace('\'', '\\\'', $a_string);
211 return $a_string;
212 } // end of the 'PMA_sqlAddslashes()' function
216 * Displays a MySQL error message in the right frame.
218 * @param string the error mesage
219 * @param string the sql query that failed
220 * @param boolean whether to show a "modify" link or not
221 * @param string the "back" link url (full path is not required)
223 * @access public
225 function PMA_mysqlDie($error_message = '', $the_query = '',
226 $is_modify_link = TRUE, $back_url = '')
228 if (!$error_message) {
229 $error_message = mysql_error();
231 if (!$the_query && !empty($GLOBALS['sql_query'])) {
232 $the_query = $GLOBALS['sql_query'];
235 echo '<b>'. $GLOBALS['strError'] . '</b>' . "\n";
236 // if the config password is wrong, or the MySQL server does not
237 // respond, do not show the query that would reveal the
238 // username/password
239 if (!empty($the_query) && !strstr($the_query, 'connect')) {
240 $query_base = htmlspecialchars($the_query);
241 $query_base = ereg_replace("((\015\012)|(\015)|(\012)){3,}", "\n\n", $query_base);
242 echo '<p>' . "\n";
243 echo ' ' . $GLOBALS['strSQLQuery'] . '&nbsp;:&nbsp;' . "\n";
244 if ($is_modify_link) {
245 echo ' ['
246 . '<a href="db_details.php3?lang=' . $GLOBALS['lang'] . '&amp;server=' . urlencode($GLOBALS['server']) . '&amp;db=' . urlencode($GLOBALS['db']) . '&amp;sql_query=' . urlencode($the_query) . '&amp;show_query=y">' . $GLOBALS['strEdit'] . '</a>'
247 . ']' . "\n";
248 } // end if
249 echo '<pre>' . "\n" . $query_base . "\n" . '</pre>' . "\n";
250 echo '</p>' . "\n";
251 } // end if
252 if (!empty($error_message)) {
253 $error_message = htmlspecialchars($error_message);
254 $error_message = ereg_replace("((\015\012)|(\015)|(\012)){3,}", "\n\n", $error_message);
256 echo '<p>' . "\n";
257 echo ' ' . $GLOBALS['strMySQLSaid'] . '<br />' . "\n";
258 echo '<pre>' . "\n" . $error_message . "\n" . '</pre>' . "\n";
259 echo '</p>' . "\n";
260 if (!empty($back_url)) {
261 echo '<a href="' . $back_url . '">' . $GLOBALS['strBack'] . '</a>';
263 echo "\n";
265 include('./footer.inc.php3');
266 exit();
267 } // end of the 'PMA_mysqlDie()' function
271 * Defines whether a string exists inside an array or not
273 * @param string string to search for
274 * @param mixed array to search into
276 * @return integer the rank of the $toFind string in the array or '-1' if
277 * it hasn't been found
279 * @access public
281 function PMA_isInto($toFind = '', &$in)
283 $max = count($in);
284 for ($i = 0; $i < $max && ($toFind != $in[$i]); $i++) {
285 // void();
288 return ($i < $max) ? $i : -1;
289 } // end of the 'PMA_isInto()' function
293 * Determines the font sizes to use depending on the os and browser of the
294 * user.
296 * This function is based on an article from phpBuilder (see
297 * http://www.phpbuilder.net/columns/tim20000821.php3).
299 * @return boolean always true
301 * @global string the standard font size
302 * @global string the font size for titles
303 * @global string the small font size
304 * @global string the smallest font size
306 * @access public
308 * @version 1.1
310 function PMA_setFontSizes()
312 global $font_size, $font_bigger, $font_smaller, $font_smallest;
314 // IE (<6)/Opera for win case: needs smaller fonts than anyone else
315 if (PMA_USR_OS == 'Win'
316 && ((PMA_USR_BROWSER_AGENT == 'IE' && PMA_USR_BROWSER_VER < 6) || PMA_USR_BROWSER_AGENT == 'OPERA')) {
317 $font_size = 'x-small';
318 $font_bigger = 'large';
319 $font_smaller = '90%';
320 $font_smallest = '7pt';
322 // IE6 and other browsers for win case
323 else if (PMA_USR_OS == 'Win') {
324 $font_size = 'small';
325 $font_bigger = 'large';
326 $font_smaller = (PMA_USR_BROWSER_AGENT == 'IE')
327 ? '90%'
328 : 'x-small';
329 $font_smallest = 'x-small';
331 // Some mac browsers need also smaller default fonts size (OmniWeb &
332 // Opera)...
333 else if (PMA_USR_OS == 'Mac'
334 && (PMA_USR_BROWSER_AGENT == 'OMNIWEB' || PMA_USR_BROWSER_AGENT == 'OPERA')) {
335 $font_size = 'x-small';
336 $font_bigger = 'large';
337 $font_smaller = '90%';
338 $font_smallest = '7pt';
340 // ... but most of them (except IE 5+ & NS 6+) need bigger fonts
341 else if (PMA_USR_OS == 'Mac'
342 && ((PMA_USR_BROWSER_AGENT != 'IE' && PMA_USR_BROWSER_AGENT != 'MOZILLA')
343 || PMA_USR_BROWSER_VER < 5)) {
344 $font_size = 'medium';
345 $font_bigger = 'x-large';
346 $font_smaller = 'small';
347 $font_smallest = 'x-small';
349 // OS/2 browser
350 else if (PMA_USR_OS == 'OS/2'
351 && PMA_USR_BROWSER_AGENT == 'OPERA') {
352 $font_size = 'small';
353 $font_bigger = 'medium';
354 $font_smaller = 'x-small';
355 $font_smallest = 'x-small';
357 else {
358 $font_size = 'small';
359 $font_bigger = 'large';
360 $font_smaller = 'x-small';
361 $font_smallest = 'x-small';
364 return true;
365 } // end of the 'PMA_setFontSizes()' function
369 * Use mysql_connect() or mysql_pconnect()?
371 $connect_func = ($cfgPersistentConnections) ? 'mysql_pconnect' : 'mysql_connect';
372 $dblist = array();
376 * Gets the valid servers list and parameters
378 reset($cfgServers);
379 while (list($key, $val) = each($cfgServers)) {
380 // Don't use servers with no hostname
381 if (empty($val['host'])) {
382 unset($cfgServers[$key]);
386 if (empty($server) || !isset($cfgServers[$server]) || !is_array($cfgServers[$server])) {
387 $server = $cfgServerDefault;
392 * If no server is selected, make sure that $cfgServer is empty (so that
393 * nothing will work), and skip server authentication.
394 * We do NOT exit here, but continue on without logging into any server.
395 * This way, the welcome page will still come up (with no server info) and
396 * present a choice of servers in the case that there are multiple servers
397 * and '$cfgServerDefault = 0' is set.
399 if ($server == 0) {
400 $cfgServer = array();
404 * Otherwise, set up $cfgServer and do the usual login stuff.
406 else if (isset($cfgServers[$server])) {
407 $cfgServer = $cfgServers[$server];
409 // Check how the config says to connect to the server
410 $server_port = (empty($cfgServer['port']))
411 ? ''
412 : ':' . $cfgServer['port'];
413 if (strtolower($cfgServer['connect_type']) == 'tcp') {
414 $cfgServer['socket'] = '';
416 $server_socket = (empty($cfgServer['socket']) || PMA_PHP_INT_VERSION < 30010)
417 ? ''
418 : ':' . $cfgServer['socket'];
420 // Ensures compatibility with old config files
421 if (!isset($cfgServer['auth_type'])) {
422 $cfgServer['auth_type'] = (isset($cfgServer['adv_auth']) && $cfgServer['adv_auth'])
423 ? 'http'
424 : 'config';
427 // Gets the authentication library that fits the cfgServer settings
428 // and run authentication
429 include('./libraries/auth/' . $cfgServer['auth_type'] . '.auth.lib.php3');
430 if (!PMA_auth_check()) {
431 PMA_auth();
432 } else {
433 PMA_auth_set_user();
436 // The user can work with only some databases
437 if (isset($cfgServer['only_db']) && $cfgServer['only_db'] != '') {
438 if (is_array($cfgServer['only_db'])) {
439 $dblist = $cfgServer['only_db'];
440 } else {
441 $dblist[] = $cfgServer['only_db'];
443 } // end if
445 if (PMA_PHP_INT_VERSION >= 40000) {
446 $bkp_track_err = @ini_set('track_errors', 1);
449 // Try to connect MySQL with the standard user profile (will be used to
450 // get the privileges list for the current user but the true user link
451 // must be open after this one so it would be default one for all the
452 // scripts)
453 if ($cfgServer['stduser'] != '') {
454 $dbh = @$connect_func(
455 $cfgServer['host'] . $server_port . $server_socket,
456 $cfgServer['stduser'],
457 $cfgServer['stdpass']
459 if ($dbh == FALSE) {
460 if (mysql_error()) {
461 $conn_error = mysql_error();
462 } else if (isset($php_errormsg)) {
463 $conn_error = $php_errormsg;
464 } else {
465 $conn_error = 'Cannot connect: invalid settings.';
467 $local_query = $connect_func . '('
468 . $cfgServer['host'] . $server_port . $server_socket . ', '
469 . $cfgServer['stduser'] . ', '
470 . $cfgServer['stdpass'] . ')';
471 PMA_mysqlDie($conn_error, $local_query, FALSE);
472 } // end if
473 } // end if
475 // Connects to the server (validates user's login)
476 $userlink = @$connect_func(
477 $cfgServer['host'] . $server_port . $server_socket,
478 $cfgServer['user'],
479 $cfgServer['password']
481 if ($userlink == FALSE) {
482 PMA_auth_fails();
483 } // end if
485 if (PMA_PHP_INT_VERSION >= 40000) {
486 @ini_set('track_errors', $bkp_track_err);
489 // If stduser isn't defined, use the current user settings to get his
490 // rights
491 if ($cfgServer['stduser'] == '') {
492 $dbh = $userlink;
495 // if 'only_db' is set for the current user, there is no need to check for
496 // available databases in the "mysql" db
497 $dblist_cnt = count($dblist);
498 if ($dblist_cnt) {
499 $true_dblist = array();
500 $is_show_dbs = TRUE;
501 for ($i = 0; $i < $dblist_cnt; $i++) {
502 if ($is_show_dbs && ereg('(^|[^\])(_|%)', $dblist[$i])) {
503 $local_query = 'SHOW DATABASES LIKE \'' . $dblist[$i] . '\'';
504 $rs = mysql_query($local_query, $dbh);
505 // "SHOW DATABASES" statement is disabled
506 if ($i == 0
507 && (mysql_error() && mysql_errno() == 1045)) {
508 $true_dblist[] = str_replace('\\_', '_', str_replace('\\%', '%', $dblist[$i]));
509 $is_show_dbs = FALSE;
511 // Debug
512 // else if (mysql_error()) {
513 // PMA_mysqlDie('', $local_query, FALSE);
514 // }
515 while ($row = @mysql_fetch_row($rs)) {
516 $true_dblist[] = $row[0];
517 } // end while
518 if ($rs) {
519 mysql_free_result($rs);
521 } else {
522 $true_dblist[] = str_replace('\\_', '_', str_replace('\\%', '%', $dblist[$i]));
523 } // end if... else...
524 } // end for
525 $dblist = $true_dblist;
526 unset($true_dblist);
527 } // end if
529 // 'only_db' is empty for the current user -> checks for available
530 // databases in the "mysql" db
531 else {
532 $auth_query = 'SELECT User, Select_priv '
533 . 'FROM mysql.user '
534 . 'WHERE User = \'' . PMA_sqlAddslashes($cfgServer['user']) . '\'';
535 $rs = mysql_query($auth_query, $dbh); // Debug: or PMA_mysqlDie('', $auth_query, FALSE);
536 } // end if
538 // Access to "mysql" db allowed -> gets the usable db list
539 if (!$dblist_cnt && @mysql_numrows($rs)) {
540 $row = mysql_fetch_array($rs);
541 mysql_free_result($rs);
542 // Correction uva 19991215
543 // Previous code assumed database "mysql" admin table "db" column
544 // "db" contains literal name of user database, and works if so.
545 // Mysql usage generally (and uva usage specifically) allows this
546 // column to contain regular expressions (we have all databases
547 // owned by a given student/faculty/staff beginning with user i.d.
548 // and governed by default by a single set of privileges with
549 // regular expression as key). This breaks previous code.
550 // This maintenance is to fix code to work correctly for regular
551 // expressions.
552 if ($row['Select_priv'] != 'Y') {
554 // 1. get allowed dbs from the "mysql.db" table
555 // lem9: User can be blank (anonymous user)
556 $local_query = 'SELECT DISTINCT Db FROM mysql.db WHERE Select_priv = \'Y\' AND (User = \'' . PMA_sqlAddslashes($cfgServer['user']) . '\' OR User = \'\')';
557 $rs = mysql_query($local_query, $dbh); // Debug: or PMA_mysqlDie('', $local_query, FALSE);
558 if (@mysql_numrows($rs)) {
559 // Will use as associative array of the following 2 code
560 // lines:
561 // the 1st is the only line intact from before
562 // correction,
563 // the 2nd replaces $dblist[] = $row['Db'];
564 $uva_mydbs = array();
565 // Code following those 2 lines in correction continues
566 // populating $dblist[], as previous code did. But it is
567 // now populated with actual database names instead of
568 // with regular expressions.
569 while ($row = mysql_fetch_array($rs)) {
570 // loic1: all databases cases - part 1
571 if (empty($row['Db']) || $row['Db'] == '%') {
572 $uva_mydbs['%'] = 1;
573 break;
575 // loic1: avoid multiple entries for dbs
576 if (!isset($uva_mydbs[$row['Db']])) {
577 $uva_mydbs[$row['Db']] = 1;
579 } // end while
580 mysql_free_result($rs);
581 $uva_alldbs = mysql_list_dbs($dbh);
582 // loic1: all databases cases - part 2
583 if (isset($uva_mydbs['%'])) {
584 while ($uva_row = mysql_fetch_array($uva_alldbs)) {
585 $dblist[] = $uva_row[0];
586 } // end while
587 } // end if
588 else {
589 while ($uva_row = mysql_fetch_array($uva_alldbs)) {
590 $uva_db = $uva_row[0];
591 if (isset($uva_mydbs[$uva_db]) && $uva_mydbs[$uva_db] == 1) {
592 $dblist[] = $uva_db;
593 $uva_mydbs[$uva_db] = 0;
594 } else if (!isset($dblist[$uva_db])) {
595 reset($uva_mydbs);
596 while (list($uva_matchpattern, $uva_value) = each($uva_mydbs)) {
597 // loic1: fixed bad regexp
598 // TODO: db names may contain characters
599 // that are regexp instructions
600 $re = '(^|(\\\\\\\\)+|[^\])';
601 $uva_regex = ereg_replace($re . '%', '\\1.*', ereg_replace($re . '_', '\\1.{1}', $uva_matchpattern));
602 // Fixed db name matching
603 // 2000-08-28 -- Benjamin Gandon
604 if (ereg('^' . $uva_regex . '$', $uva_db)) {
605 $dblist[] = $uva_db;
606 break;
608 } // end while
609 } // end if ... else if....
610 } // end while
611 } // end else
612 mysql_free_result($uva_alldbs);
613 unset($uva_mydbs);
614 } // end if
616 // 2. get allowed dbs from the "mysql.tables_priv" table
617 $local_query = 'SELECT DISTINCT Db FROM mysql.tables_priv WHERE Table_priv LIKE \'%Select%\' AND User = \'' . PMA_sqlAddslashes($cfgServer['user']) . '\'';
618 $rs = mysql_query($local_query, $dbh); // Debug: or PMA_mysqlDie('', $local_query, FALSE);
619 if (@mysql_numrows($rs)) {
620 while ($row = mysql_fetch_array($rs)) {
621 if (PMA_isInto($row['Db'], $dblist) == -1) {
622 $dblist[] = $row['Db'];
624 } // end while
625 mysql_free_result($rs);
626 } // end if
627 } // end if
628 } // end building available dbs from the "mysql" db
630 } // end server connecting
633 * Missing server hostname
635 else {
636 echo $strHostEmpty;
641 * Get the list and number of available databases.
643 * @param string the url to go back to in case of error
645 * @return boolean always true
647 * @global array the list of available databases
648 * @global integer the number of available databases
650 function PMA_availableDatabases($error_url = '')
652 global $dblist;
653 global $num_dbs;
655 $num_dbs = count($dblist);
657 // 1. A list of allowed databases has already been defined by the
658 // authentification process -> gets the available databases list
659 if ($num_dbs) {
660 $true_dblist = array();
661 for ($i = 0; $i < $num_dbs; $i++) {
662 $dblink = @mysql_select_db($dblist[$i]);
663 if ($dblink) {
664 $true_dblist[] = $dblist[$i];
665 } // end if
666 } // end for
667 $dblist = array();
668 $dblist = $true_dblist;
669 unset($true_dblist);
670 $num_dbs = count($dblist);
671 } // end if
673 // 2. Allowed database list is empty -> gets the list of all databases
674 // on the server
675 else {
676 $dbs = mysql_list_dbs() or PMA_mysqlDie('', 'mysql_list_dbs()', FALSE, $error_url);
677 $num_dbs = @mysql_num_rows($dbs);
678 $real_num_dbs = 0;
679 for ($i = 0; $i < $num_dbs; $i++) {
680 $db_name_tmp = mysql_dbname($dbs, $i);
681 $dblink = @mysql_select_db($db_name_tmp);
682 if ($dblink) {
683 $dblist[] = $db_name_tmp;
684 $real_num_dbs++;
686 } // end for
687 mysql_free_result($dbs);
688 $num_dbs = $real_num_dbs;
689 } // end else
691 return TRUE;
692 } // end of the 'PMA_availableDatabases()' function
696 * Gets constants that defines the PHP, MySQL... releases.
697 * This include must be located physically before any code that needs to
698 * reference the constants, else PHP 3.0.16 won't be happy; and must be
699 * located after we are connected to db to get the MySql version.
701 include('./libraries/defines.lib.php3');
705 /* ----------------------- Set of misc functions ----------------------- */
709 * Adds backquotes on both sides of a database, table or field name.
710 * Since MySQL 3.23.6 this allows to use non-alphanumeric characters in
711 * these names.
713 * @param string the database, table or field name to "backquote"
714 * @param boolean a flag to bypass this function (used by dump functions)
716 * @return string the "backquoted" database, table or field name if the
717 * current MySQL release is >= 3.23.6, the original one
718 * else
720 * @access public
722 function PMA_backquote($a_name, $do_it = TRUE)
724 if ($do_it
725 && PMA_MYSQL_INT_VERSION >= 32306
726 && !empty($a_name) && $a_name != '*') {
727 return '`' . $a_name . '`';
728 } else {
729 return $a_name;
731 } // end of the 'PMA_backquote()' function
735 * Format a string so it can be passed to a javascript function.
736 * This function is used to displays a javascript confirmation box for
737 * "DROP/DELETE/ALTER" queries.
739 * @param string the string to format
740 * @param boolean whether to add backquotes to the string or not
742 * @return string the formated string
744 * @access public
746 function PMA_jsFormat($a_string = '', $add_backquotes = TRUE)
748 if (is_string($a_string)) {
749 $a_string = str_replace('"', '&quot;', $a_string);
750 $a_string = str_replace('\\', '\\\\', $a_string);
751 $a_string = str_replace('\'', '\\\'', $a_string);
752 $a_string = str_replace('#', '\\#', $a_string);
753 $a_string = str_replace("\012", '\\\\n', $a_string);
754 $a_string = str_replace("\015", '\\\\r', $a_string);
757 return (($add_backquotes) ? PMA_backquote($a_string) : $a_string);
758 } // end of the 'PMA_jsFormat()' function
762 * Defines the <CR><LF> value depending on the user OS.
764 * @return string the <CR><LF> value to use
766 * @access public
768 function PMA_whichCrlf()
770 $the_crlf = "\n";
772 // The 'PMA_USR_OS' constant is defined in "./libraries/defines.lib.php3"
773 // Win case
774 if (PMA_USR_OS == 'Win') {
775 $the_crlf = "\r\n";
777 // Mac case
778 else if (PMA_USR_OS == 'Mac') {
779 $the_crlf = "\r";
781 // Others
782 else {
783 $the_crlf = "\n";
786 return $the_crlf;
787 } // end of the 'PMA_whichCrlf()' function
791 * Counts and displays the number of records in a table
793 * Last revision 13 July 2001: Patch for limiting dump size from
794 * vinay@sanisoft.com & girish@sanisoft.com
796 * @param string the current database name
797 * @param string the current table name
798 * @param boolean whether to retain or to displays the result
800 * @return mixed the number of records if retain is required, true else
802 * @access public
804 function PMA_countRecords($db, $table, $ret = FALSE)
806 $result = mysql_query('SELECT COUNT(*) AS num FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table));
807 $num = mysql_result($result, 0, 'num');
808 mysql_free_result($result);
809 if ($ret) {
810 return $num;
811 } else {
812 echo number_format($num, 0, $GLOBALS['number_decimal_separator'], $GLOBALS['number_thousands_separator']);
813 return TRUE;
815 } // end of the 'PMA_countRecords()' function
819 * Displays a message at the top of the "main" (right) frame
821 * @param string the message to display
823 * @access public
825 function PMA_showMessage($message)
827 // Reloads the navigation frame via JavaScript if required
828 if (isset($GLOBALS['reload']) && $GLOBALS['reload']) {
829 echo "\n";
830 $reload_url = './left.php3'
831 . '?lang=' . $GLOBALS['lang']
832 . '&server=' . $GLOBALS['server']
833 . ((!empty($GLOBALS['db'])) ? '&db=' . urlencode($GLOBALS['db']) : '');
835 <script type="text/javascript" language="javascript1.2">
836 <!--
837 window.parent.frames['nav'].location.replace('<?php echo $reload_url; ?>');
838 //-->
839 </script>
840 <?php
842 echo "\n";
844 <div align="<?php echo $GLOBALS['cell_align_left']; ?>">
845 <table border="<?php echo $GLOBALS['cfgBorder']; ?>" cellpadding="5">
846 <tr>
847 <td bgcolor="<?php echo $GLOBALS['cfgThBgcolor']; ?>">
848 <b><?php echo get_magic_quotes_gpc() ? stripslashes($message) : $message; ?></b><br />
849 </td>
850 </tr>
851 <?php
852 if ($GLOBALS['cfgShowSQL'] == TRUE && !empty($GLOBALS['sql_query'])) {
853 echo "\n";
855 <tr>
856 <td bgcolor="<?php echo $GLOBALS['cfgBgcolorOne']; ?>">
857 <?php
858 echo "\n";
859 // The nl2br function isn't used because its result isn't a valid
860 // xhtml1.0 statement before php4.0.5 ("<br>" and not "<br />")
861 $new_line = '<br />' . "\n" . ' ';
862 $query_base = htmlspecialchars($GLOBALS['sql_query']);
863 $query_base = ereg_replace("((\015\012)|(\015)|(\012))+", $new_line, $query_base);
864 if (!isset($GLOBALS['show_query']) || $GLOBALS['show_query'] != 'y') {
865 if (!isset($GLOBALS['goto'])) {
866 $edit_target = (isset($GLOBALS['table'])) ? 'tbl_properties.php3' : 'db_details.php3';
867 } else if ($GLOBALS['goto'] != 'main.php3') {
868 $edit_target = $GLOBALS['goto'];
869 } else {
870 $edit_target = '';
872 if ($edit_target == 'tbl_properties.php3') {
873 $edit_link = '<a href="tbl_properties.php3?lang=' . $GLOBALS['lang'] . '&amp;server=' . urlencode($GLOBALS['server']) . '&amp;db=' . urlencode($GLOBALS['db']) . '&amp;table=' . urlencode($GLOBALS['table']) . '&amp;sql_query=' . urlencode($GLOBALS['sql_query']) . '&amp;show_query=y#querybox">' . $GLOBALS['strEdit'] . '</a>';
874 } else if ($edit_target != '') {
875 $edit_link = '<a href="db_details.php3?lang=' . $GLOBALS['lang'] . '&amp;server=' . urlencode($GLOBALS['server']) . '&amp;db=' . urlencode($GLOBALS['db']) . '&amp;sql_query=' . urlencode($GLOBALS['sql_query']) . '&amp;show_query=y#querybox">' . $GLOBALS['strEdit'] . '</a>';
878 if (!empty($edit_target)) {
879 echo ' ' . $GLOBALS['strSQLQuery'] . '&nbsp;:&nbsp;[' . $edit_link . ']<br />' . "\n";
880 } else {
881 echo ' ' . $GLOBALS['strSQLQuery'] . '&nbsp;:<br />' . "\n";
883 echo ' ' . $query_base;
884 // If a 'LIMIT' clause has been programatically added to the query
885 // displays it
886 if (!empty($GLOBALS['sql_limit_to_append'])) {
887 echo $GLOBALS['sql_limit_to_append'];
889 echo "\n";
891 </td>
892 </tr>
893 <?php
895 echo "\n";
897 </table>
898 </div><br />
899 <?php
900 } // end of the 'PMA_showMessage()' function
904 * Displays a link to the official MySQL documentation (short)
906 * @param string an anchor to move to
908 * @return string the html link
910 * @access public
912 function PMA_showDocuShort($link)
914 if (!empty($GLOBALS['cfgManualBaseShort'])) {
915 return '[<a href="' . $GLOBALS['cfgManualBaseShort'] . '/' . $link .'" target="mysql_doc">' . $GLOBALS['strDocu'] . '</a>]';
917 } // end of the 'PMA_showDocuShort()' function
921 * Formats $value to byte view
923 * @param double the value to format
924 * @param integer the sensitiveness
925 * @param integer the number of decimals to retain
927 * @return array the formatted value and its unit
929 * @access public
931 * @author staybyte
932 * @version 1.1 - 07 July 2001
934 function PMA_formatByteDown($value, $limes = 6, $comma = 0)
936 $dh = pow(10, $comma);
937 $li = pow(10, $limes);
938 $return_value = $value;
939 $unit = $GLOBALS['byteUnits'][0];
941 if ($value >= $li*1000000) {
942 $value = round($value/(1073741824/$dh))/$dh;
943 $unit = $GLOBALS['byteUnits'][3];
945 else if ($value >= $li*1000) {
946 $value = round($value/(1048576/$dh))/$dh;
947 $unit = $GLOBALS['byteUnits'][2];
949 else if ($value >= $li) {
950 $value = round($value/(1024/$dh))/$dh;
951 $unit = $GLOBALS['byteUnits'][1];
953 if ($unit != $GLOBALS['byteUnits'][0]) {
954 $return_value = number_format($value, $comma, $GLOBALS['number_decimal_separator'], $GLOBALS['number_thousands_separator']);
955 } else {
956 $return_value = number_format($value, 0, $GLOBALS['number_decimal_separator'], $GLOBALS['number_thousands_separator']);
959 return array($return_value, $unit);
960 } // end of the 'PMA_formatByteDown' function
964 * Ensures a database/table/field's name is not a reserved word (for MySQL
965 * releases < 3.23.6)
967 * @param string the name to check
968 * @param string the url to go back in case of error
970 * @return boolean true if the name is valid (no return else)
972 * @access public
974 * @author Dell'Aiera Pol; Olivier Blin
976 function PMA_checkReservedWords($the_name, $error_url)
978 // The name contains caracters <> a-z, A-Z and "_" -> not a reserved
979 // word
980 if (!ereg('^[a-zA-Z_]+$', $the_name)) {
981 return true;
984 // Else do the work
985 $filename = 'badwords.txt';
986 if (file_exists($filename)) {
987 // Builds the reserved words array
988 $fd = fopen($filename, 'r');
989 $contents = fread($fd, filesize($filename) - 1);
990 fclose ($fd);
991 $word_list = explode("\n", $contents);
993 // Do the checking
994 $word_cnt = count($word_list);
995 for ($i = 0; $i < $word_cnt; $i++) {
996 if (strtolower($the_name) == $word_list[$i]) {
997 PMA_mysqlDie(sprintf($GLOBALS['strInvalidName'], $the_name), '', FALSE, $error_url);
998 } // end if
999 } // end for
1000 } // end if
1001 } // end of the 'PMA_checkReservedWords' function
1005 * Writes localised date
1007 * @param string the current timestamp
1009 * @return string the formatted date
1011 * @access public
1013 function PMA_localisedDate($timestamp = -1)
1015 global $datefmt, $month, $day_of_week;
1017 if ($timestamp == -1) {
1018 $timestamp = time();
1021 $date = ereg_replace('%[aA]', $day_of_week[(int)strftime('%w', $timestamp)], $datefmt);
1022 $date = ereg_replace('%[bB]', $month[(int)strftime('%m', $timestamp)-1], $date);
1024 return strftime($date, $timestamp);
1025 } // end of the 'PMA_localisedDate()' function
1027 } // $__PMA_COMMON_LIB__