Merge remote-tracking branch 'origin/master' into drizzle
[phpmyadmin.git] / libraries / Config.class.php
blob0130252e871f16d6ee1d40ca7ca3f6d44a21b1f4
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Configuration handling.
6 * @package phpMyAdmin
7 */
9 /**
10 * Load vendor configuration.
12 require('./libraries/vendor_config.php');
14 /**
15 * Configuration class
17 * @package phpMyAdmin
19 class PMA_Config
21 /**
22 * @var string default config source
24 var $default_source = './libraries/config.default.php';
26 /**
27 * @var array default configuration settings
29 var $default = array();
31 /**
32 * @var array configuration settings
34 var $settings = array();
36 /**
37 * @var string config source
39 var $source = '';
41 /**
42 * @var int source modification time
44 var $source_mtime = 0;
45 var $default_source_mtime = 0;
46 var $set_mtime = 0;
48 /**
49 * @var boolean
51 var $error_config_file = false;
53 /**
54 * @var boolean
56 var $error_config_default_file = false;
58 /**
59 * @var boolean
61 var $error_pma_uri = false;
63 /**
64 * @var array
66 var $default_server = array();
68 /**
69 * @var boolean whether init is done or not
70 * set this to false to force some initial checks
71 * like checking for required functions
73 var $done = false;
75 /**
76 * constructor
78 * @param string source to read config from
80 function __construct($source = null)
82 $this->settings = array();
84 // functions need to refresh in case of config file changed goes in
85 // PMA_Config::load()
86 $this->load($source);
88 // other settings, independent from config file, comes in
89 $this->checkSystem();
91 $this->checkIsHttps();
94 /**
95 * sets system and application settings
97 function checkSystem()
99 $this->set('PMA_VERSION', '3.5.0-dev');
101 * @deprecated
103 $this->set('PMA_THEME_VERSION', 2);
105 * @deprecated
107 $this->set('PMA_THEME_GENERATION', 2);
109 $this->checkPhpVersion();
110 $this->checkWebServerOs();
111 $this->checkWebServer();
112 $this->checkGd2();
113 $this->checkClient();
114 $this->checkUpload();
115 $this->checkUploadSize();
116 $this->checkOutputCompression();
120 * whether to use gzip output compression or not
122 function checkOutputCompression()
124 // If zlib output compression is set in the php configuration file, no
125 // output buffering should be run
126 if (@ini_get('zlib.output_compression')) {
127 $this->set('OBGzip', false);
130 // disable output-buffering (if set to 'auto') for IE6, else enable it.
131 if (strtolower($this->get('OBGzip')) == 'auto') {
132 if ($this->get('PMA_USR_BROWSER_AGENT') == 'IE'
133 && $this->get('PMA_USR_BROWSER_VER') >= 6
134 && $this->get('PMA_USR_BROWSER_VER') < 7) {
135 $this->set('OBGzip', false);
136 } else {
137 $this->set('OBGzip', true);
143 * Determines platform (OS), browser and version of the user
144 * Based on a phpBuilder article:
145 * @see http://www.phpbuilder.net/columns/tim20000821.php
147 function checkClient()
149 if (PMA_getenv('HTTP_USER_AGENT')) {
150 $HTTP_USER_AGENT = PMA_getenv('HTTP_USER_AGENT');
151 } elseif (! isset($HTTP_USER_AGENT)) {
152 $HTTP_USER_AGENT = '';
155 // 1. Platform
156 if (strstr($HTTP_USER_AGENT, 'Win')) {
157 $this->set('PMA_USR_OS', 'Win');
158 } elseif (strstr($HTTP_USER_AGENT, 'Mac')) {
159 $this->set('PMA_USR_OS', 'Mac');
160 } elseif (strstr($HTTP_USER_AGENT, 'Linux')) {
161 $this->set('PMA_USR_OS', 'Linux');
162 } elseif (strstr($HTTP_USER_AGENT, 'Unix')) {
163 $this->set('PMA_USR_OS', 'Unix');
164 } elseif (strstr($HTTP_USER_AGENT, 'OS/2')) {
165 $this->set('PMA_USR_OS', 'OS/2');
166 } else {
167 $this->set('PMA_USR_OS', 'Other');
170 // 2. browser and version
171 // (must check everything else before Mozilla)
173 if (preg_match('@Opera(/| )([0-9].[0-9]{1,2})@', $HTTP_USER_AGENT, $log_version)) {
174 $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
175 $this->set('PMA_USR_BROWSER_AGENT', 'OPERA');
176 } elseif (preg_match('@MSIE ([0-9].[0-9]{1,2})@', $HTTP_USER_AGENT, $log_version)) {
177 $this->set('PMA_USR_BROWSER_VER', $log_version[1]);
178 $this->set('PMA_USR_BROWSER_AGENT', 'IE');
179 } elseif (preg_match('@OmniWeb/([0-9].[0-9]{1,2})@', $HTTP_USER_AGENT, $log_version)) {
180 $this->set('PMA_USR_BROWSER_VER', $log_version[1]);
181 $this->set('PMA_USR_BROWSER_AGENT', 'OMNIWEB');
182 // Konqueror 2.2.2 says Konqueror/2.2.2
183 // Konqueror 3.0.3 says Konqueror/3
184 } elseif (preg_match('@(Konqueror/)(.*)(;)@', $HTTP_USER_AGENT, $log_version)) {
185 $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
186 $this->set('PMA_USR_BROWSER_AGENT', 'KONQUEROR');
187 } elseif (preg_match('@Mozilla/([0-9].[0-9]{1,2})@', $HTTP_USER_AGENT, $log_version)
188 && preg_match('@Safari/([0-9]*)@', $HTTP_USER_AGENT, $log_version2)) {
189 $this->set('PMA_USR_BROWSER_VER', $log_version[1] . '.' . $log_version2[1]);
190 $this->set('PMA_USR_BROWSER_AGENT', 'SAFARI');
191 } elseif (preg_match('@rv:1.9(.*)Gecko@', $HTTP_USER_AGENT)) {
192 $this->set('PMA_USR_BROWSER_VER', '1.9');
193 $this->set('PMA_USR_BROWSER_AGENT', 'GECKO');
194 } elseif (preg_match('@Mozilla/([0-9].[0-9]{1,2})@', $HTTP_USER_AGENT, $log_version)) {
195 $this->set('PMA_USR_BROWSER_VER', $log_version[1]);
196 $this->set('PMA_USR_BROWSER_AGENT', 'MOZILLA');
197 } else {
198 $this->set('PMA_USR_BROWSER_VER', 0);
199 $this->set('PMA_USR_BROWSER_AGENT', 'OTHER');
204 * Whether GD2 is present
206 function checkGd2()
208 if ($this->get('GD2Available') == 'yes') {
209 $this->set('PMA_IS_GD2', 1);
210 } elseif ($this->get('GD2Available') == 'no') {
211 $this->set('PMA_IS_GD2', 0);
212 } else {
213 if (!@function_exists('imagecreatetruecolor')) {
214 $this->set('PMA_IS_GD2', 0);
215 } else {
216 if (@function_exists('gd_info')) {
217 $gd_nfo = gd_info();
218 if (strstr($gd_nfo["GD Version"], '2.')) {
219 $this->set('PMA_IS_GD2', 1);
220 } else {
221 $this->set('PMA_IS_GD2', 0);
223 } else {
224 /* We must do hard way... but almost no chance to execute this */
225 ob_start();
226 phpinfo(INFO_MODULES); /* Only modules */
227 $a = strip_tags(ob_get_contents());
228 ob_end_clean();
229 /* Get GD version string from phpinfo output */
230 if (preg_match('@GD Version[[:space:]]*\(.*\)@', $a, $v)) {
231 if (strstr($v, '2.')) {
232 $this->set('PMA_IS_GD2', 1);
233 } else {
234 $this->set('PMA_IS_GD2', 0);
236 } else {
237 $this->set('PMA_IS_GD2', 0);
245 * Whether the Web server php is running on is IIS
247 function checkWebServer()
249 if (PMA_getenv('SERVER_SOFTWARE')
250 // some versions return Microsoft-IIS, some Microsoft/IIS
251 // we could use a preg_match() but it's slower
252 && stristr(PMA_getenv('SERVER_SOFTWARE'), 'Microsoft')
253 && stristr(PMA_getenv('SERVER_SOFTWARE'), 'IIS')) {
254 $this->set('PMA_IS_IIS', 1);
255 } else {
256 $this->set('PMA_IS_IIS', 0);
261 * Whether the os php is running on is windows or not
263 function checkWebServerOs()
265 // Default to Unix or Equiv
266 $this->set('PMA_IS_WINDOWS', 0);
267 // If PHP_OS is defined then continue
268 if (defined('PHP_OS')) {
269 if (stristr(PHP_OS, 'win')) {
270 // Is it some version of Windows
271 $this->set('PMA_IS_WINDOWS', 1);
272 } elseif (stristr(PHP_OS, 'OS/2')) {
273 // Is it OS/2 (No file permissions like Windows)
274 $this->set('PMA_IS_WINDOWS', 1);
280 * detects PHP version
282 function checkPhpVersion()
284 $match = array();
285 if (! preg_match('@([0-9]{1,2}).([0-9]{1,2}).([0-9]{1,2})@',
286 phpversion(), $match)) {
287 $result = preg_match('@([0-9]{1,2}).([0-9]{1,2})@',
288 phpversion(), $match);
290 if (isset($match) && ! empty($match[1])) {
291 if (! isset($match[2])) {
292 $match[2] = 0;
294 if (! isset($match[3])) {
295 $match[3] = 0;
297 $this->set('PMA_PHP_INT_VERSION',
298 (int) sprintf('%d%02d%02d', $match[1], $match[2], $match[3]));
299 } else {
300 $this->set('PMA_PHP_INT_VERSION', 0);
302 $this->set('PMA_PHP_STR_VERSION', phpversion());
306 * loads default values from default source
308 * @return boolean success
310 function loadDefaults()
312 $cfg = array();
313 if (! file_exists($this->default_source)) {
314 $this->error_config_default_file = true;
315 return false;
317 include $this->default_source;
319 $this->default_source_mtime = filemtime($this->default_source);
321 $this->default_server = $cfg['Servers'][1];
322 unset($cfg['Servers']);
324 $this->default = $cfg;
325 $this->settings = PMA_array_merge_recursive($this->settings, $cfg);
327 $this->error_config_default_file = false;
329 return true;
333 * loads configuration from $source, usally the config file
334 * should be called on object creation
336 * @param string $source config file
338 function load($source = null)
340 $this->loadDefaults();
342 if (null !== $source) {
343 $this->setSource($source);
346 if (! $this->checkConfigSource()) {
347 return false;
350 $cfg = array();
353 * Parses the configuration file, the eval is used here to avoid
354 * problems with trailing whitespace, what is often a problem.
356 $old_error_reporting = error_reporting(0);
357 $eval_result = eval('?' . '>' . trim(file_get_contents($this->getSource())));
358 error_reporting($old_error_reporting);
360 if ($eval_result === false) {
361 $this->error_config_file = true;
362 } else {
363 $this->error_config_file = false;
364 $this->source_mtime = filemtime($this->getSource());
368 * Backward compatibility code
370 if (!empty($cfg['DefaultTabTable'])) {
371 $cfg['DefaultTabTable'] = str_replace('_properties', '', str_replace('tbl_properties.php', 'tbl_sql.php', $cfg['DefaultTabTable']));
373 if (!empty($cfg['DefaultTabDatabase'])) {
374 $cfg['DefaultTabDatabase'] = str_replace('_details', '', str_replace('db_details.php', 'db_sql.php', $cfg['DefaultTabDatabase']));
377 $this->settings = PMA_array_merge_recursive($this->settings, $cfg);
378 $this->checkPmaAbsoluteUri();
379 $this->checkFontsize();
381 $this->checkPermissions();
383 // Handling of the collation must be done after merging of $cfg
384 // (from config.inc.php) so that $cfg['DefaultConnectionCollation']
385 // can have an effect. Note that the presence of collation
386 // information in a cookie has priority over what is defined
387 // in the default or user's config files.
389 * @todo check validity of $_COOKIE['pma_collation_connection']
391 if (! empty($_COOKIE['pma_collation_connection'])) {
392 $this->set('collation_connection',
393 strip_tags($_COOKIE['pma_collation_connection']));
394 } else {
395 $this->set('collation_connection',
396 $this->get('DefaultConnectionCollation'));
398 // Now, a collation information could come from REQUEST
399 // (an example of this: the collation selector in main.php)
400 // so the following handles the setting of collation_connection
401 // and later, in common.inc.php, the cookie will be set
402 // according to this.
403 $this->checkCollationConnection();
405 return true;
409 * Loads user preferences and merges them with current config
410 * must be called after control connection has been estabilished
412 * @return boolean
414 function loadUserPreferences()
416 // index.php should load these settings, so that phpmyadmin.css.php
417 // will have everything avaiable in session cache
418 $server = isset($GLOBALS['server'])
419 ? $GLOBALS['server']
420 : (!empty($GLOBALS['cfg']['ServerDefault']) ? $GLOBALS['cfg']['ServerDefault'] : 0);
421 $cache_key = 'server_' . $server;
422 if ($server > 0 && !defined('PMA_MINIMUM_COMMON')) {
423 $config_mtime = max($this->default_source_mtime, $this->source_mtime);
424 // cache user preferences, use database only when needed
425 if (! isset($_SESSION['cache'][$cache_key]['userprefs'])
426 || $_SESSION['cache'][$cache_key]['config_mtime'] < $config_mtime) {
427 // load required libraries
428 require_once './libraries/user_preferences.lib.php';
429 $prefs = PMA_load_userprefs();
430 $_SESSION['cache'][$cache_key]['userprefs'] = PMA_apply_userprefs($prefs['config_data']);
431 $_SESSION['cache'][$cache_key]['userprefs_mtime'] = $prefs['mtime'];
432 $_SESSION['cache'][$cache_key]['userprefs_type'] = $prefs['type'];
433 $_SESSION['cache'][$cache_key]['config_mtime'] = $config_mtime;
435 } else if ($server == 0 || ! isset($_SESSION['cache'][$cache_key]['userprefs'])) {
436 $this->set('user_preferences', false);
437 return;
439 $config_data = $_SESSION['cache'][$cache_key]['userprefs'];
440 // type is 'db' or 'session'
441 $this->set('user_preferences', $_SESSION['cache'][$cache_key]['userprefs_type']);
442 $this->set('user_preferences_mtime', $_SESSION['cache'][$cache_key]['userprefs_mtime']);
444 // backup some settings
445 $org_fontsize = $this->settings['fontsize'];
446 // load config array
447 $this->settings = PMA_array_merge_recursive($this->settings, $config_data);
448 $GLOBALS['cfg'] = PMA_array_merge_recursive($GLOBALS['cfg'], $config_data);
449 if (defined('PMA_MINIMUM_COMMON')) {
450 return;
453 // settings below start really working on next page load, but
454 // changes are made only in index.php so everything is set when
455 // in frames
457 // save theme
458 $tmanager = $_SESSION['PMA_Theme_Manager'];
459 if ($tmanager->getThemeCookie() || isset($_REQUEST['set_theme'])) {
460 if ((! isset($config_data['ThemeDefault']) && $tmanager->theme->getId() != 'original')
461 || isset($config_data['ThemeDefault']) && $config_data['ThemeDefault'] != $tmanager->theme->getId()) {
462 // new theme was set in common.inc.php
463 $this->setUserValue(null, 'ThemeDefault', $tmanager->theme->getId(), 'original');
465 } else {
466 // no cookie - read default from settings
467 if ($this->settings['ThemeDefault'] != $tmanager->theme->getId()
468 && $tmanager->checkTheme($this->settings['ThemeDefault'])) {
469 $tmanager->setActiveTheme($this->settings['ThemeDefault']);
470 $tmanager->setThemeCookie();
474 // save font size
475 if ((! isset($config_data['fontsize']) && $org_fontsize != '82%')
476 || isset($config_data['fontsize']) && $org_fontsize != $config_data['fontsize']) {
477 $this->setUserValue(null, 'fontsize', $org_fontsize, '82%');
480 // save language
481 if (isset($_COOKIE['pma_lang']) || isset($_POST['lang'])) {
482 if ((! isset($config_data['lang']) && $GLOBALS['lang'] != 'en')
483 || isset($config_data['lang']) && $GLOBALS['lang'] != $config_data['lang']) {
484 $this->setUserValue(null, 'lang', $GLOBALS['lang'], 'en');
486 } else {
487 // read language from settings
488 if (isset($config_data['lang']) && PMA_langSet($config_data['lang'])) {
489 $this->setCookie('pma_lang', $GLOBALS['lang']);
493 // save connection collation
494 if (isset($_COOKIE['pma_collation_connection']) || isset($_POST['collation_connection'])) {
495 if ((! isset($config_data['collation_connection']) && $GLOBALS['collation_connection'] != 'utf8_general_ci')
496 || isset($config_data['collation_connection']) && $GLOBALS['collation_connection'] != $config_data['collation_connection']) {
497 $this->setUserValue(null, 'collation_connection', $GLOBALS['collation_connection'], 'utf8_general_ci');
499 } else {
500 // read collation from settings
501 if (isset($config_data['collation_connection'])) {
502 $GLOBALS['collation_connection'] = $config_data['collation_connection'];
503 $this->setCookie('pma_collation_connection', $GLOBALS['collation_connection']);
509 * Sets config value which is stored in user preferences (if available) or in a cookie.
511 * If user preferences are not yet initialized, option is applied to global config and
512 * added to a update queue, which is processed by {@link loadUserPreferences()}
514 * @param string $cookie_name can be null
515 * @param string $cfg_path
516 * @param mixed $new_cfg_value
517 * @param mixed $default_value
519 function setUserValue($cookie_name, $cfg_path, $new_cfg_value, $default_value = null)
521 // use permanent user preferences if possible
522 $prefs_type = $this->get('user_preferences');
523 if ($prefs_type) {
524 require_once './libraries/user_preferences.lib.php';
525 if ($default_value === null) {
526 $default_value = PMA_array_read($cfg_path, $this->default);
528 PMA_persist_option($cfg_path, $new_cfg_value, $default_value);
530 if ($prefs_type != 'db' && $cookie_name) {
531 // fall back to cookies
532 if ($default_value === null) {
533 $default_value = PMA_array_read($cfg_path, $this->settings);
535 $this->setCookie($cookie_name, $new_cfg_value, $default_value);
537 PMA_array_write($cfg_path, $GLOBALS['cfg'], $new_cfg_value);
538 PMA_array_write($cfg_path, $this->settings, $new_cfg_value);
542 * Reads value stored by {@link setUserValue()}
544 * @param string $cookie_name
545 * @param mixed $cfg_value
546 * @return mixed
548 function getUserValue($cookie_name, $cfg_value)
550 $cookie_exists = isset($_COOKIE) && !empty($_COOKIE[$cookie_name]);
551 $prefs_type = $this->get('user_preferences');
552 if ($prefs_type == 'db') {
553 // permanent user preferences value exists, remove cookie
554 if ($cookie_exists) {
555 $this->removeCookie($cookie_name);
557 } else if ($cookie_exists) {
558 return $_COOKIE[$cookie_name];
560 // return value from $cfg array
561 return $cfg_value;
565 * set source
566 * @param string $source
568 function setSource($source)
570 $this->source = trim($source);
574 * checks if the config folder still exists and terminates app if true
576 function checkConfigFolder()
578 // Refuse to work while there still might be some world writable dir:
579 if (is_dir('./config')) {
580 die('Remove "./config" directory before using phpMyAdmin!');
585 * check config source
587 * @return boolean whether source is valid or not
589 function checkConfigSource()
591 if (! $this->getSource()) {
592 // no configuration file set at all
593 return false;
596 if (! file_exists($this->getSource())) {
597 // do not trigger error here
598 // https://sf.net/tracker/?func=detail&aid=1370269&group_id=23067&atid=377408
600 trigger_error(
601 'phpMyAdmin-ERROR: unkown configuration source: ' . $source,
602 E_USER_WARNING);
604 $this->source_mtime = 0;
605 return false;
608 if (! is_readable($this->getSource())) {
609 $this->source_mtime = 0;
610 die('Existing configuration file (' . $this->getSource() . ') is not readable.');
613 return true;
617 * verifies the permissions on config file (if asked by configuration)
618 * (must be called after config.inc.php has been merged)
620 function checkPermissions()
622 // Check for permissions (on platforms that support it):
623 if ($this->get('CheckConfigurationPermissions')) {
624 $perms = @fileperms($this->getSource());
625 if (!($perms === false) && ($perms & 2)) {
626 // This check is normally done after loading configuration
627 $this->checkWebServerOs();
628 if ($this->get('PMA_IS_WINDOWS') == 0) {
629 $this->source_mtime = 0;
630 die('Wrong permissions on configuration file, should not be world writable!');
637 * returns specific config setting
638 * @param string $setting
639 * @return mixed value
641 function get($setting)
643 if (isset($this->settings[$setting])) {
644 return $this->settings[$setting];
646 return null;
650 * sets configuration variable
652 * @param string $setting configuration option
653 * @param string $value new value for configuration option
655 function set($setting, $value)
657 if (! isset($this->settings[$setting]) || $this->settings[$setting] != $value) {
658 $this->settings[$setting] = $value;
659 $this->set_mtime = time();
664 * returns source for current config
665 * @return string config source
667 function getSource()
669 return $this->source;
673 * returns a unique value to force a CSS reload if either the config
674 * or the theme changes
675 * must also check the pma_fontsize cookie in case there is no
676 * config file
677 * @return int Summary of unix timestamps and fontsize, to be unique on theme parameters change
679 function getThemeUniqueValue()
681 if (null !== $this->get('fontsize')) {
682 $fontsize = intval($this->get('fontsize'));
683 } elseif (isset($_COOKIE['pma_fontsize'])) {
684 $fontsize = intval($_COOKIE['pma_fontsize']);
685 } else {
686 $fontsize = 0;
688 return (
689 $fontsize +
690 $this->source_mtime +
691 $this->default_source_mtime +
692 $this->get('user_preferences_mtime') +
693 $_SESSION['PMA_Theme']->mtime_info +
694 $_SESSION['PMA_Theme']->filesize_info);
698 * $cfg['PmaAbsoluteUri'] is a required directive else cookies won't be
699 * set properly and, depending on browsers, inserting or updating a
700 * record might fail
702 function checkPmaAbsoluteUri()
704 // Setup a default value to let the people and lazy sysadmins work anyway,
705 // they'll get an error if the autodetect code doesn't work
706 $pma_absolute_uri = $this->get('PmaAbsoluteUri');
707 $is_https = $this->detectHttps();
709 if (strlen($pma_absolute_uri) < 5) {
710 $url = array();
712 // At first we try to parse REQUEST_URI, it might contain full URL
714 * REQUEST_URI contains PATH_INFO too, this is not what we want
715 * script-php/pathinfo/
716 if (PMA_getenv('REQUEST_URI')) {
717 $url = @parse_url(PMA_getenv('REQUEST_URI')); // produces E_WARNING if it cannot get parsed, e.g. '/foobar:/'
718 if ($url === false) {
719 $url = array('path' => $_SERVER['REQUEST_URI']);
724 // If we don't have scheme, we didn't have full URL so we need to
725 // dig deeper
726 if (empty($url['scheme'])) {
727 // Scheme
728 if (PMA_getenv('HTTP_SCHEME')) {
729 $url['scheme'] = PMA_getenv('HTTP_SCHEME');
730 } else {
731 $url['scheme'] =
732 PMA_getenv('HTTPS') && strtolower(PMA_getenv('HTTPS')) != 'off'
733 ? 'https'
734 : 'http';
737 // Host and port
738 if (PMA_getenv('HTTP_HOST')) {
739 // Prepend the scheme before using parse_url() since this is not part of the RFC2616 Host request-header
740 $parsed_url = parse_url($url['scheme'] . '://' . PMA_getenv('HTTP_HOST'));
741 if (!empty($parsed_url['host'])) {
742 $url = $parsed_url;
743 } else {
744 $url['host'] = PMA_getenv('HTTP_HOST');
746 } elseif (PMA_getenv('SERVER_NAME')) {
747 $url['host'] = PMA_getenv('SERVER_NAME');
748 } else {
749 $this->error_pma_uri = true;
750 return false;
753 // If we didn't set port yet...
754 if (empty($url['port']) && PMA_getenv('SERVER_PORT')) {
755 $url['port'] = PMA_getenv('SERVER_PORT');
758 // And finally the path could be already set from REQUEST_URI
759 if (empty($url['path'])) {
761 * REQUEST_URI contains PATH_INFO too, this is not what we want
762 * script-php/pathinfo/
763 if (PMA_getenv('PATH_INFO')) {
764 $path = parse_url(PMA_getenv('PATH_INFO'));
765 } else {
766 // PHP_SELF in CGI often points to cgi executable, so use it
767 // as last choice
769 $path = parse_url($GLOBALS['PMA_PHP_SELF']);
771 $url['path'] = $path['path'];
775 // Make url from parts we have
776 $pma_absolute_uri = $url['scheme'] . '://';
777 // Was there user information?
778 if (!empty($url['user'])) {
779 $pma_absolute_uri .= $url['user'];
780 if (!empty($url['pass'])) {
781 $pma_absolute_uri .= ':' . $url['pass'];
783 $pma_absolute_uri .= '@';
785 // Add hostname
786 $pma_absolute_uri .= $url['host'];
787 // Add port, if it not the default one
788 if (! empty($url['port'])
789 && (($url['scheme'] == 'http' && $url['port'] != 80)
790 || ($url['scheme'] == 'https' && $url['port'] != 443))) {
791 $pma_absolute_uri .= ':' . $url['port'];
793 // And finally path, without script name, the 'a' is there not to
794 // strip our directory, when path is only /pmadir/ without filename.
795 // Backslashes returned by Windows have to be changed.
796 // Only replace backslashes by forward slashes if on Windows,
797 // as the backslash could be valid on a non-Windows system.
798 $this->checkWebServerOs();
799 if ($this->get('PMA_IS_WINDOWS') == 1) {
800 $path = str_replace("\\", "/", dirname($url['path'] . 'a'));
801 } else {
802 $path = dirname($url['path'] . 'a');
805 // To work correctly within transformations overview:
806 if (defined('PMA_PATH_TO_BASEDIR') && PMA_PATH_TO_BASEDIR == '../../') {
807 if ($this->get('PMA_IS_WINDOWS') == 1) {
808 $path = str_replace("\\", "/", dirname(dirname($path)));
809 } else {
810 $path = dirname(dirname($path));
814 // PHP's dirname function would have returned a dot when $path contains no slash
815 if ($path == '.') {
816 $path = '';
818 // in vhost situations, there could be already an ending slash
819 if (substr($path, -1) != '/') {
820 $path .= '/';
822 $pma_absolute_uri .= $path;
824 // We used to display a warning if PmaAbsoluteUri wasn't set, but now
825 // the autodetect code works well enough that we don't display the
826 // warning at all. The user can still set PmaAbsoluteUri manually.
827 // See
828 // http://sf.net/tracker/?func=detail&aid=1257134&group_id=23067&atid=377411
830 } else {
831 // The URI is specified, however users do often specify this
832 // wrongly, so we try to fix this.
834 // Adds a trailing slash et the end of the phpMyAdmin uri if it
835 // does not exist.
836 if (substr($pma_absolute_uri, -1) != '/') {
837 $pma_absolute_uri .= '/';
840 // If URI doesn't start with http:// or https://, we will add
841 // this.
842 if (substr($pma_absolute_uri, 0, 7) != 'http://'
843 && substr($pma_absolute_uri, 0, 8) != 'https://') {
844 $pma_absolute_uri =
845 ($is_https ? 'https' : 'http')
846 . ':' . (substr($pma_absolute_uri, 0, 2) == '//' ? '' : '//')
847 . $pma_absolute_uri;
850 $this->set('PmaAbsoluteUri', $pma_absolute_uri);
854 * check selected collation_connection
855 * @todo check validity of $_REQUEST['collation_connection']
857 function checkCollationConnection()
859 if (! empty($_REQUEST['collation_connection'])) {
860 $this->set('collation_connection',
861 strip_tags($_REQUEST['collation_connection']));
866 * checks for font size configuration, and sets font size as requested by user
869 function checkFontsize()
871 $new_fontsize = '';
873 if (isset($_GET['set_fontsize'])) {
874 $new_fontsize = $_GET['set_fontsize'];
875 } elseif (isset($_POST['set_fontsize'])) {
876 $new_fontsize = $_POST['set_fontsize'];
877 } elseif (isset($_COOKIE['pma_fontsize'])) {
878 $new_fontsize = $_COOKIE['pma_fontsize'];
881 if (preg_match('/^[0-9.]+(px|em|pt|\%)$/', $new_fontsize)) {
882 $this->set('fontsize', $new_fontsize);
883 } elseif (! $this->get('fontsize')) {
884 // 80% would correspond to the default browser font size
885 // of 16, but use 82% to help read the monoface font
886 $this->set('fontsize', '82%');
889 $this->setCookie('pma_fontsize', $this->get('fontsize'), '82%');
893 * checks if upload is enabled
897 function checkUpload()
899 if (ini_get('file_uploads')) {
900 $this->set('enable_upload', true);
901 // if set "php_admin_value file_uploads Off" in httpd.conf
902 // ini_get() also returns the string "Off" in this case:
903 if ('off' == strtolower(ini_get('file_uploads'))) {
904 $this->set('enable_upload', false);
906 } else {
907 $this->set('enable_upload', false);
912 * Maximum upload size as limited by PHP
913 * Used with permission from Moodle (http://moodle.org) by Martin Dougiamas
915 * this section generates $max_upload_size in bytes
917 function checkUploadSize()
919 if (! $filesize = ini_get('upload_max_filesize')) {
920 $filesize = "5M";
923 if ($postsize = ini_get('post_max_size')) {
924 $this->set('max_upload_size',
925 min(PMA_get_real_size($filesize), PMA_get_real_size($postsize)));
926 } else {
927 $this->set('max_upload_size', PMA_get_real_size($filesize));
932 * check for https
934 function checkIsHttps()
936 $this->set('is_https', $this->isHttps());
940 * @static
942 public function isHttps()
944 static $is_https = null;
946 if (null !== $is_https) {
947 return $is_https;
950 $url = parse_url($this->get('PmaAbsoluteUri'));
952 if (isset($url['scheme'])
953 && $url['scheme'] == 'https') {
954 $is_https = true;
955 } else {
956 $is_https = false;
959 return $is_https;
963 * Detects whether https appears to be used.
965 * Please note that this just detects what we see, so
966 * it completely ignores things like reverse proxies.
968 function detectHttps()
970 $is_https = false;
972 $url = array();
974 // At first we try to parse REQUEST_URI, it might contain full URL,
975 if (PMA_getenv('REQUEST_URI')) {
976 $url = @parse_url(PMA_getenv('REQUEST_URI')); // produces E_WARNING if it cannot get parsed, e.g. '/foobar:/'
977 if ($url === false) {
978 $url = array();
982 // If we don't have scheme, we didn't have full URL so we need to
983 // dig deeper
984 if (empty($url['scheme'])) {
985 // Scheme
986 if (PMA_getenv('HTTP_SCHEME')) {
987 $url['scheme'] = PMA_getenv('HTTP_SCHEME');
988 } else {
989 $url['scheme'] =
990 PMA_getenv('HTTPS') && strtolower(PMA_getenv('HTTPS')) != 'off'
991 ? 'https'
992 : 'http';
996 if (isset($url['scheme'])
997 && $url['scheme'] == 'https') {
998 $is_https = true;
999 } else {
1000 $is_https = false;
1003 return $is_https;
1007 * detect correct cookie path
1009 function checkCookiePath()
1011 $this->set('cookie_path', $this->getCookiePath());
1015 * @static
1017 public function getCookiePath()
1019 static $cookie_path = null;
1021 if (null !== $cookie_path) {
1022 return $cookie_path;
1025 $parsed_url = parse_url($this->get('PmaAbsoluteUri'));
1027 $cookie_path = $parsed_url['path'];
1029 return $cookie_path;
1033 * enables backward compatibility
1035 function enableBc()
1037 $GLOBALS['cfg'] = $this->settings;
1038 $GLOBALS['default_server'] = $this->default_server;
1039 unset($this->default_server);
1040 $GLOBALS['collation_connection'] = $this->get('collation_connection');
1041 $GLOBALS['is_upload'] = $this->get('enable_upload');
1042 $GLOBALS['max_upload_size'] = $this->get('max_upload_size');
1043 $GLOBALS['cookie_path'] = $this->get('cookie_path');
1044 $GLOBALS['is_https'] = $this->get('is_https');
1046 $defines = array(
1047 'PMA_VERSION',
1048 'PMA_THEME_VERSION',
1049 'PMA_THEME_GENERATION',
1050 'PMA_PHP_STR_VERSION',
1051 'PMA_PHP_INT_VERSION',
1052 'PMA_IS_WINDOWS',
1053 'PMA_IS_IIS',
1054 'PMA_IS_GD2',
1055 'PMA_USR_OS',
1056 'PMA_USR_BROWSER_VER',
1057 'PMA_USR_BROWSER_AGENT'
1060 foreach ($defines as $define) {
1061 if (! defined($define)) {
1062 define($define, $this->get($define));
1068 * @todo finish
1070 function save()
1075 * returns options for font size selection
1077 * @static
1078 * @param string $current_size current selected font size with unit
1079 * @return array selectable font sizes
1081 static protected function _getFontsizeOptions($current_size = '82%')
1083 $unit = preg_replace('/[0-9.]*/', '', $current_size);
1084 $value = preg_replace('/[^0-9.]*/', '', $current_size);
1086 $factors = array();
1087 $options = array();
1088 $options["$value"] = $value . $unit;
1090 if ($unit === '%') {
1091 $factors[] = 1;
1092 $factors[] = 5;
1093 $factors[] = 10;
1094 } elseif ($unit === 'em') {
1095 $factors[] = 0.05;
1096 $factors[] = 0.2;
1097 $factors[] = 1;
1098 } elseif ($unit === 'pt') {
1099 $factors[] = 0.5;
1100 $factors[] = 2;
1101 } elseif ($unit === 'px') {
1102 $factors[] = 1;
1103 $factors[] = 5;
1104 $factors[] = 10;
1105 } else {
1106 //unknown font size unit
1107 $factors[] = 0.05;
1108 $factors[] = 0.2;
1109 $factors[] = 1;
1110 $factors[] = 5;
1111 $factors[] = 10;
1114 foreach ($factors as $key => $factor) {
1115 $option_inc = $value + $factor;
1116 $option_dec = $value - $factor;
1117 while (count($options) < 21) {
1118 $options["$option_inc"] = $option_inc . $unit;
1119 if ($option_dec > $factors[0]) {
1120 $options["$option_dec"] = $option_dec . $unit;
1122 $option_inc += $factor;
1123 $option_dec -= $factor;
1124 if (isset($factors[$key + 1])
1125 && $option_inc >= $value + $factors[$key + 1]) {
1126 break;
1130 ksort($options);
1131 return $options;
1135 * returns html selectbox for font sizes
1137 * @static
1138 * @param string $current_size currently slected font size with unit
1139 * @return string html selectbox
1141 static protected function _getFontsizeSelection()
1143 $current_size = $GLOBALS['PMA_Config']->get('fontsize');
1144 // for the case when there is no config file (this is supported)
1145 if (empty($current_size)) {
1146 if (isset($_COOKIE['pma_fontsize'])) {
1147 $current_size = $_COOKIE['pma_fontsize'];
1148 } else {
1149 $current_size = '82%';
1152 $options = PMA_Config::_getFontsizeOptions($current_size);
1154 $return = '<label for="select_fontsize">' . __('Font size') . ':</label>' . "\n";
1155 $return .= '<select name="set_fontsize" id="select_fontsize" onchange="this.form.submit();">' . "\n";
1156 foreach ($options as $option) {
1157 $return .= '<option value="' . $option . '"';
1158 if ($option == $current_size) {
1159 $return .= ' selected="selected"';
1161 $return .= '>' . $option . '</option>' . "\n";
1163 $return .= '</select>';
1165 return $return;
1169 * return complete font size selection form
1171 * @static
1172 * @param string $current_size currently slected font size with unit
1173 * @return string html selectbox
1175 static public function getFontsizeForm()
1177 return '<form name="form_fontsize_selection" id="form_fontsize_selection"'
1178 . ' method="post" action="index.php" target="_parent">' . "\n"
1179 . PMA_generate_common_hidden_inputs() . "\n"
1180 . PMA_Config::_getFontsizeSelection() . "\n"
1181 . '<noscript>' . "\n"
1182 . '<input type="submit" value="' . __('Go') . '" />' . "\n"
1183 . '</noscript>' . "\n"
1184 . '</form>';
1188 * removes cookie
1190 * @param string $cookie name of cookie to remove
1191 * @return boolean result of setcookie()
1193 function removeCookie($cookie)
1195 return setcookie($cookie, '', time() - 3600,
1196 $this->getCookiePath(), '', $this->isHttps());
1200 * sets cookie if value is different from current cokkie value,
1201 * or removes if value is equal to default
1203 * @param string $cookie name of cookie to remove
1204 * @param mixed $value new cookie value
1205 * @param string $default default value
1206 * @param int $validity validity of cookie in seconds (default is one month)
1207 * @param bool $httponlt whether cookie is only for HTTP (and not for scripts)
1208 * @return boolean result of setcookie()
1210 function setCookie($cookie, $value, $default = null, $validity = null, $httponly = true)
1212 if ($validity == null) {
1213 $validity = 2592000;
1215 if (strlen($value) && null !== $default && $value === $default) {
1216 // default value is used
1217 if (isset($_COOKIE[$cookie])) {
1218 // remove cookie
1219 return $this->removeCookie($cookie);
1221 return false;
1224 if (! strlen($value) && isset($_COOKIE[$cookie])) {
1225 // remove cookie, value is empty
1226 return $this->removeCookie($cookie);
1229 if (! isset($_COOKIE[$cookie]) || $_COOKIE[$cookie] !== $value) {
1230 // set cookie with new value
1231 /* Calculate cookie validity */
1232 if ($validity == 0) {
1233 $v = 0;
1234 } else {
1235 $v = time() + $validity;
1237 return setcookie($cookie, $value, $v,
1238 $this->getCookiePath(), '', $this->isHttps(), $httponly);
1241 // cookie has already $value as value
1242 return true;