3.4.0-beta2
[phpmyadmin/crack.git] / libraries / Config.class.php
blobde7e61a3da3d6c3f09783b30f10f6b9f52aeef65
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
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.4.0-beta2');
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 * @uses file_exists()
309 * @uses $this->default_source
310 * @uses $this->error_config_default_file
311 * @uses $this->settings
312 * @return boolean success
314 function loadDefaults()
316 $cfg = array();
317 if (! file_exists($this->default_source)) {
318 $this->error_config_default_file = true;
319 return false;
321 include $this->default_source;
323 $this->default_source_mtime = filemtime($this->default_source);
325 $this->default_server = $cfg['Servers'][1];
326 unset($cfg['Servers']);
328 $this->default = $cfg;
329 $this->settings = PMA_array_merge_recursive($this->settings, $cfg);
331 $this->error_config_default_file = false;
333 return true;
337 * loads configuration from $source, usally the config file
338 * should be called on object creation
340 * @param string $source config file
342 function load($source = null)
344 $this->loadDefaults();
346 if (null !== $source) {
347 $this->setSource($source);
350 if (! $this->checkConfigSource()) {
351 return false;
354 $cfg = array();
357 * Parses the configuration file
359 $old_error_reporting = error_reporting(0);
360 if (function_exists('file_get_contents')) {
361 $eval_result =
362 eval('?' . '>' . trim(file_get_contents($this->getSource())));
363 } else {
364 $eval_result =
365 eval('?' . '>' . trim(implode("\n", file($this->getSource()))));
367 error_reporting($old_error_reporting);
369 if ($eval_result === false) {
370 $this->error_config_file = true;
371 } else {
372 $this->error_config_file = false;
373 $this->source_mtime = filemtime($this->getSource());
377 * Backward compatibility code
379 if (!empty($cfg['DefaultTabTable'])) {
380 $cfg['DefaultTabTable'] = str_replace('_properties', '', str_replace('tbl_properties.php', 'tbl_sql.php', $cfg['DefaultTabTable']));
382 if (!empty($cfg['DefaultTabDatabase'])) {
383 $cfg['DefaultTabDatabase'] = str_replace('_details', '', str_replace('db_details.php', 'db_sql.php', $cfg['DefaultTabDatabase']));
386 $this->settings = PMA_array_merge_recursive($this->settings, $cfg);
387 $this->checkPmaAbsoluteUri();
388 $this->checkFontsize();
390 $this->checkPermissions();
392 // Handling of the collation must be done after merging of $cfg
393 // (from config.inc.php) so that $cfg['DefaultConnectionCollation']
394 // can have an effect. Note that the presence of collation
395 // information in a cookie has priority over what is defined
396 // in the default or user's config files.
398 * @todo check validity of $_COOKIE['pma_collation_connection']
400 if (! empty($_COOKIE['pma_collation_connection'])) {
401 $this->set('collation_connection',
402 strip_tags($_COOKIE['pma_collation_connection']));
403 } else {
404 $this->set('collation_connection',
405 $this->get('DefaultConnectionCollation'));
407 // Now, a collation information could come from REQUEST
408 // (an example of this: the collation selector in main.php)
409 // so the following handles the setting of collation_connection
410 // and later, in common.inc.php, the cookie will be set
411 // according to this.
412 $this->checkCollationConnection();
414 return true;
418 * Loads user preferences and merges them with current config
419 * must be called after control connection has been estabilished
421 * @uses $GLOBALS['cfg']
422 * @uses $GLOBALS['collation_connection']
423 * @uses $GLOBALS['lang']
424 * @uses $_SESSION['cache']['server_$server']['config_mtime']
425 * @uses $_SESSION['cache']['server_$server']['userprefs']
426 * @uses $_SESSION['cache']['server_$server']['userprefs_mtime']
427 * @uses $_SESSION['PMA_Theme_Manager']
428 * @uses PMA_apply_userprefs()
429 * @uses PMA_array_merge_recursive()
430 * @uses PMA_load_userprefs()
431 * @return boolean
433 function loadUserPreferences()
435 // index.php should load these settings, so that phpmyadmin.css.php
436 // will have everything avaiable in session cache
437 $server = isset($GLOBALS['server'])
438 ? $GLOBALS['server']
439 : (!empty($GLOBALS['cfg']['ServerDefault']) ? $GLOBALS['cfg']['ServerDefault'] : 0);
440 $cache_key = 'server_' . $server;
441 if ($server > 0 && !defined('PMA_MINIMUM_COMMON')) {
442 $config_mtime = max($this->default_source_mtime, $this->source_mtime);
443 // cache user preferences, use database only when needed
444 if (!isset($_SESSION['cache'][$cache_key]['userprefs'])
445 || $_SESSION['cache'][$cache_key]['config_mtime'] < $config_mtime) {
446 // load required libraries
447 require_once './libraries/user_preferences.lib.php';
448 $prefs = PMA_load_userprefs();
449 $_SESSION['cache'][$cache_key]['userprefs'] = PMA_apply_userprefs($prefs['config_data']);
450 $_SESSION['cache'][$cache_key]['userprefs_mtime'] = $prefs['mtime'];
451 $_SESSION['cache'][$cache_key]['userprefs_type'] = $prefs['type'];
452 $_SESSION['cache'][$cache_key]['config_mtime'] = $config_mtime;
454 } else if ($server == 0 || !isset($_SESSION['cache'][$cache_key]['userprefs'])) {
455 $this->set('user_preferences', false);
456 return;
458 $config_data = $_SESSION['cache'][$cache_key]['userprefs'];
459 // type is 'db' or 'session'
460 $this->set('user_preferences', $_SESSION['cache'][$cache_key]['userprefs_type']);
461 $this->set('user_preferences_mtime', $_SESSION['cache'][$cache_key]['userprefs_mtime']);
463 // backup some settings
464 $org_fontsize = $this->settings['fontsize'];
465 // load config array
466 $this->settings = PMA_array_merge_recursive($this->settings, $config_data);
467 $GLOBALS['cfg'] = PMA_array_merge_recursive($GLOBALS['cfg'], $config_data);
468 if (defined('PMA_MINIMUM_COMMON')) {
469 return;
472 // settings below start really working on next page load, but
473 // changes are made only in index.php so everything is set when
474 // in frames
476 // save theme
477 $tmanager = $_SESSION['PMA_Theme_Manager'];
478 if ($tmanager->getThemeCookie() || isset($_REQUEST['set_theme'])) {
479 if ((!isset($config_data['ThemeDefault']) && $tmanager->theme->getId() != 'original')
480 || isset($config_data['ThemeDefault']) && $config_data['ThemeDefault'] != $tmanager->theme->getId()) {
481 // new theme was set in common.inc.php
482 $this->setUserValue(null, 'ThemeDefault', $tmanager->theme->getId(), 'original');
484 } else {
485 // no cookie - read default from settings
486 if ($this->settings['ThemeDefault'] != $tmanager->theme->getId()
487 && $tmanager->checkTheme($this->settings['ThemeDefault'])) {
488 $tmanager->setActiveTheme($this->settings['ThemeDefault']);
489 $tmanager->setThemeCookie();
493 // save font size
494 if ((!isset($config_data['fontsize']) && $org_fontsize != '82%')
495 || isset($config_data['fontsize']) && $org_fontsize != $config_data['fontsize']) {
496 $this->setUserValue(null, 'fontsize', $org_fontsize, '82%');
499 // save language
500 if (isset($_COOKIE['pma_lang']) || isset($_POST['lang'])) {
501 if ((!isset($config_data['lang']) && $GLOBALS['lang'] != 'en')
502 || isset($config_data['lang']) && $GLOBALS['lang'] != $config_data['lang']) {
503 $this->setUserValue(null, 'lang', $GLOBALS['lang'], 'en');
505 } else {
506 // read language from settings
507 if (isset($config_data['lang']) && PMA_langSet($config_data['lang'])) {
508 $this->setCookie('pma_lang', $GLOBALS['lang']);
512 // save connection collation
513 if (isset($_COOKIE['pma_collation_connection']) || isset($_POST['collation_connection'])) {
514 if ((!isset($config_data['collation_connection']) && $GLOBALS['collation_connection'] != 'utf8_general_ci')
515 || isset($config_data['collation_connection']) && $GLOBALS['collation_connection'] != $config_data['collation_connection']) {
516 $this->setUserValue(null, 'collation_connection', $GLOBALS['collation_connection'], 'utf8_general_ci');
518 } else {
519 // read collation from settings
520 if (isset($config_data['collation_connection'])) {
521 $GLOBALS['collation_connection'] = $config_data['collation_connection'];
522 $this->setCookie('pma_collation_connection', $GLOBALS['collation_connection']);
528 * Sets config value which is stored in user preferences (if available) or in a cookie.
530 * If user preferences are not yet initialized, option is applied to global config and
531 * added to a update queue, which is processed by {@link loadUserPreferences()}
533 * @uses $GLOBALS['cfg']
534 * @uses PMA_array_read()
535 * @uses PMA_array_write()
536 * @uses PMA_persist_option()
537 * @param string $cookie_name can be null
538 * @param string $cfg_path
539 * @param mixed $new_cfg_value
540 * @param mixed $default_value
542 function setUserValue($cookie_name, $cfg_path, $new_cfg_value, $default_value = null)
544 // use permanent user preferences if possible
545 $prefs_type = $this->get('user_preferences');
546 if ($prefs_type) {
547 require_once './libraries/user_preferences.lib.php';
548 if ($default_value === null) {
549 $default_value = PMA_array_read($cfg_path, $this->default);
551 PMA_persist_option($cfg_path, $new_cfg_value, $default_value);
553 if ($prefs_type != 'db' && $cookie_name) {
554 // fall back to cookies
555 if ($default_value === null) {
556 $default_value = PMA_array_read($cfg_path, $this->settings);
558 $this->setCookie($cookie_name, $new_cfg_value, $default_value);
560 PMA_array_write($cfg_path, $GLOBALS['cfg'], $new_cfg_value);
561 PMA_array_write($cfg_path, $this->settings, $new_cfg_value);
565 * Reads value stored by {@link setUserValue()}
567 * @param string $cookie_name
568 * @param mixed $cfg_value
569 * @return mixed
571 function getUserValue($cookie_name, $cfg_value)
573 $cookie_exists = isset($_COOKIE) && !empty($_COOKIE[$cookie_name]);
574 $prefs_type = $this->get('user_preferences');
575 if ($prefs_type == 'db') {
576 // permanent user preferences value exists, remove cookie
577 if ($cookie_exists) {
578 $this->removeCookie($cookie_name);
580 } else if ($cookie_exists) {
581 return $_COOKIE[$cookie_name];
583 // return value from $cfg array
584 return $cfg_value;
588 * set source
589 * @param string $source
591 function setSource($source)
593 $this->source = trim($source);
597 * checks if the config folder still exists and terminates app if true
599 function checkConfigFolder()
601 // Refuse to work while there still might be some world writable dir:
602 if (is_dir('./config')) {
603 die('Remove "./config" directory before using phpMyAdmin!');
608 * check config source
610 * @return boolean whether source is valid or not
612 function checkConfigSource()
614 if (! $this->getSource()) {
615 // no configuration file set at all
616 return false;
619 if (! file_exists($this->getSource())) {
620 // do not trigger error here
621 // https://sf.net/tracker/?func=detail&aid=1370269&group_id=23067&atid=377408
623 trigger_error(
624 'phpMyAdmin-ERROR: unkown configuration source: ' . $source,
625 E_USER_WARNING);
627 $this->source_mtime = 0;
628 return false;
631 if (! is_readable($this->getSource())) {
632 $this->source_mtime = 0;
633 die('Existing configuration file (' . $this->getSource() . ') is not readable.');
636 return true;
640 * verifies the permissions on config file (if asked by configuration)
641 * (must be called after config.inc.php has been merged)
643 function checkPermissions()
645 // Check for permissions (on platforms that support it):
646 if ($this->get('CheckConfigurationPermissions')) {
647 $perms = @fileperms($this->getSource());
648 if (!($perms === false) && ($perms & 2)) {
649 // This check is normally done after loading configuration
650 $this->checkWebServerOs();
651 if ($this->get('PMA_IS_WINDOWS') == 0) {
652 $this->source_mtime = 0;
653 die('Wrong permissions on configuration file, should not be world writable!');
660 * returns specific config setting
661 * @param string $setting
662 * @return mixed value
664 function get($setting)
666 if (isset($this->settings[$setting])) {
667 return $this->settings[$setting];
669 return null;
673 * sets configuration variable
675 * @uses $this->settings
676 * @param string $setting configuration option
677 * @param string $value new value for configuration option
679 function set($setting, $value)
681 if (!isset($this->settings[$setting]) || $this->settings[$setting] != $value) {
682 $this->settings[$setting] = $value;
683 $this->set_mtime = time();
688 * returns source for current config
689 * @return string config source
691 function getSource()
693 return $this->source;
697 * returns a unique value to force a CSS reload if either the config
698 * or the theme changes
699 * must also check the pma_fontsize cookie in case there is no
700 * config file
701 * @return int Unix timestamp
703 function getThemeUniqueValue()
705 if (null !== $this->get('fontsize')) {
706 $fontsize = intval($this->get('fontsize'));
707 } elseif (isset($_COOKIE['pma_fontsize'])) {
708 $fontsize = intval($_COOKIE['pma_fontsize']);
709 } else {
710 $fontsize = 0;
712 return (
713 $fontsize +
714 $this->source_mtime +
715 $this->default_source_mtime +
716 $this->get('user_preferences_mtime') +
717 $_SESSION['PMA_Theme']->mtime_info +
718 $_SESSION['PMA_Theme']->filesize_info)
719 . (isset($_SESSION['tmp_user_values']['custom_color']) ? substr($_SESSION['tmp_user_values']['custom_color'],1,6) : '');
723 * $cfg['PmaAbsoluteUri'] is a required directive else cookies won't be
724 * set properly and, depending on browsers, inserting or updating a
725 * record might fail
727 function checkPmaAbsoluteUri()
729 // Setup a default value to let the people and lazy sysadmins work anyway,
730 // they'll get an error if the autodetect code doesn't work
731 $pma_absolute_uri = $this->get('PmaAbsoluteUri');
732 $is_https = $this->detectHttps();
734 if (strlen($pma_absolute_uri) < 5) {
735 $url = array();
737 // At first we try to parse REQUEST_URI, it might contain full URL
739 * REQUEST_URI contains PATH_INFO too, this is not what we want
740 * script-php/pathinfo/
741 if (PMA_getenv('REQUEST_URI')) {
742 $url = @parse_url(PMA_getenv('REQUEST_URI')); // produces E_WARNING if it cannot get parsed, e.g. '/foobar:/'
743 if ($url === false) {
744 $url = array('path' => $_SERVER['REQUEST_URI']);
749 // If we don't have scheme, we didn't have full URL so we need to
750 // dig deeper
751 if (empty($url['scheme'])) {
752 // Scheme
753 if (PMA_getenv('HTTP_SCHEME')) {
754 $url['scheme'] = PMA_getenv('HTTP_SCHEME');
755 } else {
756 $url['scheme'] =
757 PMA_getenv('HTTPS') && strtolower(PMA_getenv('HTTPS')) != 'off'
758 ? 'https'
759 : 'http';
762 // Host and port
763 if (PMA_getenv('HTTP_HOST')) {
764 // Prepend the scheme before using parse_url() since this is not part of the RFC2616 Host request-header
765 $parsed_url = parse_url($url['scheme'] . '://' . PMA_getenv('HTTP_HOST'));
766 if (!empty($parsed_url['host'])) {
767 $url = $parsed_url;
768 } else {
769 $url['host'] = PMA_getenv('HTTP_HOST');
771 } elseif (PMA_getenv('SERVER_NAME')) {
772 $url['host'] = PMA_getenv('SERVER_NAME');
773 } else {
774 $this->error_pma_uri = true;
775 return false;
778 // If we didn't set port yet...
779 if (empty($url['port']) && PMA_getenv('SERVER_PORT')) {
780 $url['port'] = PMA_getenv('SERVER_PORT');
783 // And finally the path could be already set from REQUEST_URI
784 if (empty($url['path'])) {
786 * REQUEST_URI contains PATH_INFO too, this is not what we want
787 * script-php/pathinfo/
788 if (PMA_getenv('PATH_INFO')) {
789 $path = parse_url(PMA_getenv('PATH_INFO'));
790 } else {
791 // PHP_SELF in CGI often points to cgi executable, so use it
792 // as last choice
794 $path = parse_url($GLOBALS['PMA_PHP_SELF']);
796 $url['path'] = $path['path'];
800 // Make url from parts we have
801 $pma_absolute_uri = $url['scheme'] . '://';
802 // Was there user information?
803 if (!empty($url['user'])) {
804 $pma_absolute_uri .= $url['user'];
805 if (!empty($url['pass'])) {
806 $pma_absolute_uri .= ':' . $url['pass'];
808 $pma_absolute_uri .= '@';
810 // Add hostname
811 $pma_absolute_uri .= $url['host'];
812 // Add port, if it not the default one
813 if (! empty($url['port'])
814 && (($url['scheme'] == 'http' && $url['port'] != 80)
815 || ($url['scheme'] == 'https' && $url['port'] != 443))) {
816 $pma_absolute_uri .= ':' . $url['port'];
818 // And finally path, without script name, the 'a' is there not to
819 // strip our directory, when path is only /pmadir/ without filename.
820 // Backslashes returned by Windows have to be changed.
821 // Only replace backslashes by forward slashes if on Windows,
822 // as the backslash could be valid on a non-Windows system.
823 $this->checkWebServerOs();
824 if ($this->get('PMA_IS_WINDOWS') == 1) {
825 $path = str_replace("\\", "/", dirname($url['path'] . 'a'));
826 } else {
827 $path = dirname($url['path'] . 'a');
830 // To work correctly within transformations overview:
831 if (defined('PMA_PATH_TO_BASEDIR') && PMA_PATH_TO_BASEDIR == '../../') {
832 if ($this->get('PMA_IS_WINDOWS') == 1) {
833 $path = str_replace("\\", "/", dirname(dirname($path)));
834 } else {
835 $path = dirname(dirname($path));
839 // PHP's dirname function would have returned a dot when $path contains no slash
840 if ($path == '.') {
841 $path = '';
843 // in vhost situations, there could be already an ending slash
844 if (substr($path, -1) != '/') {
845 $path .= '/';
847 $pma_absolute_uri .= $path;
849 // We used to display a warning if PmaAbsoluteUri wasn't set, but now
850 // the autodetect code works well enough that we don't display the
851 // warning at all. The user can still set PmaAbsoluteUri manually.
852 // See
853 // http://sf.net/tracker/?func=detail&aid=1257134&group_id=23067&atid=377411
855 } else {
856 // The URI is specified, however users do often specify this
857 // wrongly, so we try to fix this.
859 // Adds a trailing slash et the end of the phpMyAdmin uri if it
860 // does not exist.
861 if (substr($pma_absolute_uri, -1) != '/') {
862 $pma_absolute_uri .= '/';
865 // If URI doesn't start with http:// or https://, we will add
866 // this.
867 if (substr($pma_absolute_uri, 0, 7) != 'http://'
868 && substr($pma_absolute_uri, 0, 8) != 'https://') {
869 $pma_absolute_uri =
870 ($is_https ? 'https' : 'http')
871 . ':' . (substr($pma_absolute_uri, 0, 2) == '//' ? '' : '//')
872 . $pma_absolute_uri;
875 $this->set('PmaAbsoluteUri', $pma_absolute_uri);
879 * check selected collation_connection
880 * @todo check validity of $_REQUEST['collation_connection']
882 function checkCollationConnection()
884 if (! empty($_REQUEST['collation_connection'])) {
885 $this->set('collation_connection',
886 strip_tags($_REQUEST['collation_connection']));
891 * checks for font size configuration, and sets font size as requested by user
893 * @uses $_GET
894 * @uses $_POST
895 * @uses $_COOKIE
896 * @uses preg_match()
897 * @uses function_exists()
898 * @uses PMA_Config::set()
899 * @uses PMA_Config::get()
900 * @uses PMA_Config::setCookie()
902 function checkFontsize()
904 $new_fontsize = '';
906 if (isset($_GET['set_fontsize'])) {
907 $new_fontsize = $_GET['set_fontsize'];
908 } elseif (isset($_POST['set_fontsize'])) {
909 $new_fontsize = $_POST['set_fontsize'];
910 } elseif (isset($_COOKIE['pma_fontsize'])) {
911 $new_fontsize = $_COOKIE['pma_fontsize'];
914 if (preg_match('/^[0-9.]+(px|em|pt|\%)$/', $new_fontsize)) {
915 $this->set('fontsize', $new_fontsize);
916 } elseif (! $this->get('fontsize')) {
917 // 80% would correspond to the default browser font size
918 // of 16, but use 82% to help read the monoface font
919 $this->set('fontsize', '82%');
922 $this->setCookie('pma_fontsize', $this->get('fontsize'), '82%');
926 * checks if upload is enabled
930 function checkUpload()
932 if (ini_get('file_uploads')) {
933 $this->set('enable_upload', true);
934 // if set "php_admin_value file_uploads Off" in httpd.conf
935 // ini_get() also returns the string "Off" in this case:
936 if ('off' == strtolower(ini_get('file_uploads'))) {
937 $this->set('enable_upload', false);
939 } else {
940 $this->set('enable_upload', false);
945 * Maximum upload size as limited by PHP
946 * Used with permission from Moodle (http://moodle.org) by Martin Dougiamas
948 * this section generates $max_upload_size in bytes
950 function checkUploadSize()
952 if (! $filesize = ini_get('upload_max_filesize')) {
953 $filesize = "5M";
956 if ($postsize = ini_get('post_max_size')) {
957 $this->set('max_upload_size',
958 min(PMA_get_real_size($filesize), PMA_get_real_size($postsize)));
959 } else {
960 $this->set('max_upload_size', PMA_get_real_size($filesize));
965 * check for https
967 function checkIsHttps()
969 $this->set('is_https', $this->isHttps());
973 * @static
975 public function isHttps()
977 static $is_https = null;
979 if (null !== $is_https) {
980 return $is_https;
983 $url = parse_url($this->get('PmaAbsoluteUri'));
985 if (isset($url['scheme'])
986 && $url['scheme'] == 'https') {
987 $is_https = true;
988 } else {
989 $is_https = false;
992 return $is_https;
996 * Detects whether https appears to be used.
998 * Please note that this just detects what we see, so
999 * it completely ignores things like reverse proxies.
1001 function detectHttps()
1003 $is_https = false;
1005 $url = array();
1007 // At first we try to parse REQUEST_URI, it might contain full URL,
1008 if (PMA_getenv('REQUEST_URI')) {
1009 $url = @parse_url(PMA_getenv('REQUEST_URI')); // produces E_WARNING if it cannot get parsed, e.g. '/foobar:/'
1010 if($url === false) {
1011 $url = array();
1015 // If we don't have scheme, we didn't have full URL so we need to
1016 // dig deeper
1017 if (empty($url['scheme'])) {
1018 // Scheme
1019 if (PMA_getenv('HTTP_SCHEME')) {
1020 $url['scheme'] = PMA_getenv('HTTP_SCHEME');
1021 } else {
1022 $url['scheme'] =
1023 PMA_getenv('HTTPS') && strtolower(PMA_getenv('HTTPS')) != 'off'
1024 ? 'https'
1025 : 'http';
1029 if (isset($url['scheme'])
1030 && $url['scheme'] == 'https') {
1031 $is_https = true;
1032 } else {
1033 $is_https = false;
1036 return $is_https;
1040 * detect correct cookie path
1042 function checkCookiePath()
1044 $this->set('cookie_path', $this->getCookiePath());
1048 * @static
1050 public function getCookiePath()
1052 static $cookie_path = null;
1054 if (null !== $cookie_path) {
1055 return $cookie_path;
1058 $parsed_url = parse_url($this->get('PmaAbsoluteUri'));
1060 $cookie_path = $parsed_url['path'];
1062 return $cookie_path;
1066 * enables backward compatibility
1068 function enableBc()
1070 $GLOBALS['cfg'] = $this->settings;
1071 $GLOBALS['default_server'] = $this->default_server;
1072 unset($this->default_server);
1073 $GLOBALS['collation_connection'] = $this->get('collation_connection');
1074 $GLOBALS['is_upload'] = $this->get('enable_upload');
1075 $GLOBALS['max_upload_size'] = $this->get('max_upload_size');
1076 $GLOBALS['cookie_path'] = $this->get('cookie_path');
1077 $GLOBALS['is_https'] = $this->get('is_https');
1079 $defines = array(
1080 'PMA_VERSION',
1081 'PMA_THEME_VERSION',
1082 'PMA_THEME_GENERATION',
1083 'PMA_PHP_STR_VERSION',
1084 'PMA_PHP_INT_VERSION',
1085 'PMA_IS_WINDOWS',
1086 'PMA_IS_IIS',
1087 'PMA_IS_GD2',
1088 'PMA_USR_OS',
1089 'PMA_USR_BROWSER_VER',
1090 'PMA_USR_BROWSER_AGENT'
1093 foreach ($defines as $define) {
1094 if (! defined($define)) {
1095 define($define, $this->get($define));
1101 * @todo finish
1103 function save() {}
1106 * returns options for font size selection
1108 * @uses preg_replace()
1109 * @uses ksort()
1110 * @static
1111 * @param string $current_size current selected font size with unit
1112 * @return array selectable font sizes
1114 static protected function _getFontsizeOptions($current_size = '82%')
1116 $unit = preg_replace('/[0-9.]*/', '', $current_size);
1117 $value = preg_replace('/[^0-9.]*/', '', $current_size);
1119 $factors = array();
1120 $options = array();
1121 $options["$value"] = $value . $unit;
1123 if ($unit === '%') {
1124 $factors[] = 1;
1125 $factors[] = 5;
1126 $factors[] = 10;
1127 } elseif ($unit === 'em') {
1128 $factors[] = 0.05;
1129 $factors[] = 0.2;
1130 $factors[] = 1;
1131 } elseif ($unit === 'pt') {
1132 $factors[] = 0.5;
1133 $factors[] = 2;
1134 } elseif ($unit === 'px') {
1135 $factors[] = 1;
1136 $factors[] = 5;
1137 $factors[] = 10;
1138 } else {
1139 //unknown font size unit
1140 $factors[] = 0.05;
1141 $factors[] = 0.2;
1142 $factors[] = 1;
1143 $factors[] = 5;
1144 $factors[] = 10;
1147 foreach ($factors as $key => $factor) {
1148 $option_inc = $value + $factor;
1149 $option_dec = $value - $factor;
1150 while (count($options) < 21) {
1151 $options["$option_inc"] = $option_inc . $unit;
1152 if ($option_dec > $factors[0]) {
1153 $options["$option_dec"] = $option_dec . $unit;
1155 $option_inc += $factor;
1156 $option_dec -= $factor;
1157 if (isset($factors[$key + 1])
1158 && $option_inc >= $value + $factors[$key + 1]) {
1159 break;
1163 ksort($options);
1164 return $options;
1168 * returns html selectbox for font sizes
1170 * @uses $GLOBALS['PMA_Config']
1171 * @uses PMA_Config::get()
1172 * @uses PMA_Config::_getFontsizeOptions()
1173 * @static
1174 * @param string $current_size currently slected font size with unit
1175 * @return string html selectbox
1177 static protected function _getFontsizeSelection()
1179 $current_size = $GLOBALS['PMA_Config']->get('fontsize');
1180 // for the case when there is no config file (this is supported)
1181 if (empty($current_size)) {
1182 if (isset($_COOKIE['pma_fontsize'])) {
1183 $current_size = $_COOKIE['pma_fontsize'];
1184 } else {
1185 $current_size = '82%';
1188 $options = PMA_Config::_getFontsizeOptions($current_size);
1190 $return = '<label for="select_fontsize">' . __('Font size') . ':</label>' . "\n";
1191 $return .= '<select name="set_fontsize" id="select_fontsize" onchange="this.form.submit();">' . "\n";
1192 foreach ($options as $option) {
1193 $return .= '<option value="' . $option . '"';
1194 if ($option == $current_size) {
1195 $return .= ' selected="selected"';
1197 $return .= '>' . $option . '</option>' . "\n";
1199 $return .= '</select>';
1201 return $return;
1205 * return complete font size selection form
1207 * @uses PMA_generate_common_hidden_inputs()
1208 * @uses PMA_Config::_getFontsizeSelection()
1209 * @static
1210 * @param string $current_size currently slected font size with unit
1211 * @return string html selectbox
1213 static public function getFontsizeForm()
1215 return '<form name="form_fontsize_selection" id="form_fontsize_selection"'
1216 . ' method="post" action="index.php" target="_parent">' . "\n"
1217 . PMA_generate_common_hidden_inputs() . "\n"
1218 . PMA_Config::_getFontsizeSelection() . "\n"
1219 . '<noscript>' . "\n"
1220 . '<input type="submit" value="' . __('Go') . '" />' . "\n"
1221 . '</noscript>' . "\n"
1222 . '</form>';
1226 * removes cookie
1228 * @uses PMA_Config::isHttps()
1229 * @uses PMA_Config::getCookiePath()
1230 * @uses setcookie()
1231 * @uses time()
1232 * @param string $cookie name of cookie to remove
1233 * @return boolean result of setcookie()
1235 function removeCookie($cookie)
1237 return setcookie($cookie, '', time() - 3600,
1238 $this->getCookiePath(), '', $this->isHttps());
1242 * sets cookie if value is different from current cokkie value,
1243 * or removes if value is equal to default
1245 * @uses PMA_Config::isHttps()
1246 * @uses PMA_Config::getCookiePath()
1247 * @uses $_COOKIE
1248 * @uses PMA_Config::removeCookie()
1249 * @uses setcookie()
1250 * @uses time()
1251 * @param string $cookie name of cookie to remove
1252 * @param mixed $value new cookie value
1253 * @param string $default default value
1254 * @param int $validity validity of cookie in seconds (default is one month)
1255 * @param bool $httponlt whether cookie is only for HTTP (and not for scripts)
1256 * @return boolean result of setcookie()
1258 function setCookie($cookie, $value, $default = null, $validity = null, $httponly = true)
1260 if ($validity == null) {
1261 $validity = 2592000;
1263 if (strlen($value) && null !== $default && $value === $default) {
1264 // default value is used
1265 if (isset($_COOKIE[$cookie])) {
1266 // remove cookie
1267 return $this->removeCookie($cookie);
1269 return false;
1272 if (! strlen($value) && isset($_COOKIE[$cookie])) {
1273 // remove cookie, value is empty
1274 return $this->removeCookie($cookie);
1277 if (! isset($_COOKIE[$cookie]) || $_COOKIE[$cookie] !== $value) {
1278 // set cookie with new value
1279 /* Calculate cookie validity */
1280 if ($validity == 0) {
1281 $v = 0;
1282 } else {
1283 $v = time() + $validity;
1285 return setcookie($cookie, $value, $v,
1286 $this->getCookiePath(), '', $this->isHttps(), $httponly);
1289 // cookie has already $value as value
1290 return true;