Use PMA_DBI_get_columns in odt export
[phpmyadmin.git] / libraries / Config.class.php
blob9c7ab4804db32df764cdb13ff6a082cc7877e827
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.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
355 $old_error_reporting = error_reporting(0);
356 if (function_exists('file_get_contents')) {
357 $eval_result =
358 eval('?' . '>' . trim(file_get_contents($this->getSource())));
359 } else {
360 $eval_result =
361 eval('?' . '>' . trim(implode("\n", file($this->getSource()))));
363 error_reporting($old_error_reporting);
365 if ($eval_result === false) {
366 $this->error_config_file = true;
367 } else {
368 $this->error_config_file = false;
369 $this->source_mtime = filemtime($this->getSource());
373 * Backward compatibility code
375 if (!empty($cfg['DefaultTabTable'])) {
376 $cfg['DefaultTabTable'] = str_replace('_properties', '', str_replace('tbl_properties.php', 'tbl_sql.php', $cfg['DefaultTabTable']));
378 if (!empty($cfg['DefaultTabDatabase'])) {
379 $cfg['DefaultTabDatabase'] = str_replace('_details', '', str_replace('db_details.php', 'db_sql.php', $cfg['DefaultTabDatabase']));
382 $this->settings = PMA_array_merge_recursive($this->settings, $cfg);
383 $this->checkPmaAbsoluteUri();
384 $this->checkFontsize();
386 $this->checkPermissions();
388 // Handling of the collation must be done after merging of $cfg
389 // (from config.inc.php) so that $cfg['DefaultConnectionCollation']
390 // can have an effect. Note that the presence of collation
391 // information in a cookie has priority over what is defined
392 // in the default or user's config files.
394 * @todo check validity of $_COOKIE['pma_collation_connection']
396 if (! empty($_COOKIE['pma_collation_connection'])) {
397 $this->set('collation_connection',
398 strip_tags($_COOKIE['pma_collation_connection']));
399 } else {
400 $this->set('collation_connection',
401 $this->get('DefaultConnectionCollation'));
403 // Now, a collation information could come from REQUEST
404 // (an example of this: the collation selector in main.php)
405 // so the following handles the setting of collation_connection
406 // and later, in common.inc.php, the cookie will be set
407 // according to this.
408 $this->checkCollationConnection();
410 return true;
414 * Loads user preferences and merges them with current config
415 * must be called after control connection has been estabilished
417 * @return boolean
419 function loadUserPreferences()
421 // index.php should load these settings, so that phpmyadmin.css.php
422 // will have everything avaiable in session cache
423 $server = isset($GLOBALS['server'])
424 ? $GLOBALS['server']
425 : (!empty($GLOBALS['cfg']['ServerDefault']) ? $GLOBALS['cfg']['ServerDefault'] : 0);
426 $cache_key = 'server_' . $server;
427 if ($server > 0 && !defined('PMA_MINIMUM_COMMON')) {
428 $config_mtime = max($this->default_source_mtime, $this->source_mtime);
429 // cache user preferences, use database only when needed
430 if (! isset($_SESSION['cache'][$cache_key]['userprefs'])
431 || $_SESSION['cache'][$cache_key]['config_mtime'] < $config_mtime) {
432 // load required libraries
433 require_once './libraries/user_preferences.lib.php';
434 $prefs = PMA_load_userprefs();
435 $_SESSION['cache'][$cache_key]['userprefs'] = PMA_apply_userprefs($prefs['config_data']);
436 $_SESSION['cache'][$cache_key]['userprefs_mtime'] = $prefs['mtime'];
437 $_SESSION['cache'][$cache_key]['userprefs_type'] = $prefs['type'];
438 $_SESSION['cache'][$cache_key]['config_mtime'] = $config_mtime;
440 } else if ($server == 0 || ! isset($_SESSION['cache'][$cache_key]['userprefs'])) {
441 $this->set('user_preferences', false);
442 return;
444 $config_data = $_SESSION['cache'][$cache_key]['userprefs'];
445 // type is 'db' or 'session'
446 $this->set('user_preferences', $_SESSION['cache'][$cache_key]['userprefs_type']);
447 $this->set('user_preferences_mtime', $_SESSION['cache'][$cache_key]['userprefs_mtime']);
449 // backup some settings
450 $org_fontsize = $this->settings['fontsize'];
451 // load config array
452 $this->settings = PMA_array_merge_recursive($this->settings, $config_data);
453 $GLOBALS['cfg'] = PMA_array_merge_recursive($GLOBALS['cfg'], $config_data);
454 if (defined('PMA_MINIMUM_COMMON')) {
455 return;
458 // settings below start really working on next page load, but
459 // changes are made only in index.php so everything is set when
460 // in frames
462 // save theme
463 $tmanager = $_SESSION['PMA_Theme_Manager'];
464 if ($tmanager->getThemeCookie() || isset($_REQUEST['set_theme'])) {
465 if ((! isset($config_data['ThemeDefault']) && $tmanager->theme->getId() != 'original')
466 || isset($config_data['ThemeDefault']) && $config_data['ThemeDefault'] != $tmanager->theme->getId()) {
467 // new theme was set in common.inc.php
468 $this->setUserValue(null, 'ThemeDefault', $tmanager->theme->getId(), 'original');
470 } else {
471 // no cookie - read default from settings
472 if ($this->settings['ThemeDefault'] != $tmanager->theme->getId()
473 && $tmanager->checkTheme($this->settings['ThemeDefault'])) {
474 $tmanager->setActiveTheme($this->settings['ThemeDefault']);
475 $tmanager->setThemeCookie();
479 // save font size
480 if ((! isset($config_data['fontsize']) && $org_fontsize != '82%')
481 || isset($config_data['fontsize']) && $org_fontsize != $config_data['fontsize']) {
482 $this->setUserValue(null, 'fontsize', $org_fontsize, '82%');
485 // save language
486 if (isset($_COOKIE['pma_lang']) || isset($_POST['lang'])) {
487 if ((! isset($config_data['lang']) && $GLOBALS['lang'] != 'en')
488 || isset($config_data['lang']) && $GLOBALS['lang'] != $config_data['lang']) {
489 $this->setUserValue(null, 'lang', $GLOBALS['lang'], 'en');
491 } else {
492 // read language from settings
493 if (isset($config_data['lang']) && PMA_langSet($config_data['lang'])) {
494 $this->setCookie('pma_lang', $GLOBALS['lang']);
498 // save connection collation
499 if (isset($_COOKIE['pma_collation_connection']) || isset($_POST['collation_connection'])) {
500 if ((! isset($config_data['collation_connection']) && $GLOBALS['collation_connection'] != 'utf8_general_ci')
501 || isset($config_data['collation_connection']) && $GLOBALS['collation_connection'] != $config_data['collation_connection']) {
502 $this->setUserValue(null, 'collation_connection', $GLOBALS['collation_connection'], 'utf8_general_ci');
504 } else {
505 // read collation from settings
506 if (isset($config_data['collation_connection'])) {
507 $GLOBALS['collation_connection'] = $config_data['collation_connection'];
508 $this->setCookie('pma_collation_connection', $GLOBALS['collation_connection']);
514 * Sets config value which is stored in user preferences (if available) or in a cookie.
516 * If user preferences are not yet initialized, option is applied to global config and
517 * added to a update queue, which is processed by {@link loadUserPreferences()}
519 * @param string $cookie_name can be null
520 * @param string $cfg_path
521 * @param mixed $new_cfg_value
522 * @param mixed $default_value
524 function setUserValue($cookie_name, $cfg_path, $new_cfg_value, $default_value = null)
526 // use permanent user preferences if possible
527 $prefs_type = $this->get('user_preferences');
528 if ($prefs_type) {
529 require_once './libraries/user_preferences.lib.php';
530 if ($default_value === null) {
531 $default_value = PMA_array_read($cfg_path, $this->default);
533 PMA_persist_option($cfg_path, $new_cfg_value, $default_value);
535 if ($prefs_type != 'db' && $cookie_name) {
536 // fall back to cookies
537 if ($default_value === null) {
538 $default_value = PMA_array_read($cfg_path, $this->settings);
540 $this->setCookie($cookie_name, $new_cfg_value, $default_value);
542 PMA_array_write($cfg_path, $GLOBALS['cfg'], $new_cfg_value);
543 PMA_array_write($cfg_path, $this->settings, $new_cfg_value);
547 * Reads value stored by {@link setUserValue()}
549 * @param string $cookie_name
550 * @param mixed $cfg_value
551 * @return mixed
553 function getUserValue($cookie_name, $cfg_value)
555 $cookie_exists = isset($_COOKIE) && !empty($_COOKIE[$cookie_name]);
556 $prefs_type = $this->get('user_preferences');
557 if ($prefs_type == 'db') {
558 // permanent user preferences value exists, remove cookie
559 if ($cookie_exists) {
560 $this->removeCookie($cookie_name);
562 } else if ($cookie_exists) {
563 return $_COOKIE[$cookie_name];
565 // return value from $cfg array
566 return $cfg_value;
570 * set source
571 * @param string $source
573 function setSource($source)
575 $this->source = trim($source);
579 * checks if the config folder still exists and terminates app if true
581 function checkConfigFolder()
583 // Refuse to work while there still might be some world writable dir:
584 if (is_dir('./config')) {
585 die('Remove "./config" directory before using phpMyAdmin!');
590 * check config source
592 * @return boolean whether source is valid or not
594 function checkConfigSource()
596 if (! $this->getSource()) {
597 // no configuration file set at all
598 return false;
601 if (! file_exists($this->getSource())) {
602 // do not trigger error here
603 // https://sf.net/tracker/?func=detail&aid=1370269&group_id=23067&atid=377408
605 trigger_error(
606 'phpMyAdmin-ERROR: unkown configuration source: ' . $source,
607 E_USER_WARNING);
609 $this->source_mtime = 0;
610 return false;
613 if (! is_readable($this->getSource())) {
614 $this->source_mtime = 0;
615 die('Existing configuration file (' . $this->getSource() . ') is not readable.');
618 return true;
622 * verifies the permissions on config file (if asked by configuration)
623 * (must be called after config.inc.php has been merged)
625 function checkPermissions()
627 // Check for permissions (on platforms that support it):
628 if ($this->get('CheckConfigurationPermissions')) {
629 $perms = @fileperms($this->getSource());
630 if (!($perms === false) && ($perms & 2)) {
631 // This check is normally done after loading configuration
632 $this->checkWebServerOs();
633 if ($this->get('PMA_IS_WINDOWS') == 0) {
634 $this->source_mtime = 0;
635 die('Wrong permissions on configuration file, should not be world writable!');
642 * returns specific config setting
643 * @param string $setting
644 * @return mixed value
646 function get($setting)
648 if (isset($this->settings[$setting])) {
649 return $this->settings[$setting];
651 return null;
655 * sets configuration variable
657 * @param string $setting configuration option
658 * @param string $value new value for configuration option
660 function set($setting, $value)
662 if (! isset($this->settings[$setting]) || $this->settings[$setting] != $value) {
663 $this->settings[$setting] = $value;
664 $this->set_mtime = time();
669 * returns source for current config
670 * @return string config source
672 function getSource()
674 return $this->source;
678 * returns a unique value to force a CSS reload if either the config
679 * or the theme changes
680 * must also check the pma_fontsize cookie in case there is no
681 * config file
682 * @return int Unix timestamp
684 function getThemeUniqueValue()
686 if (null !== $this->get('fontsize')) {
687 $fontsize = intval($this->get('fontsize'));
688 } elseif (isset($_COOKIE['pma_fontsize'])) {
689 $fontsize = intval($_COOKIE['pma_fontsize']);
690 } else {
691 $fontsize = 0;
693 return (
694 $fontsize +
695 $this->source_mtime +
696 $this->default_source_mtime +
697 $this->get('user_preferences_mtime') +
698 $_SESSION['PMA_Theme']->mtime_info +
699 $_SESSION['PMA_Theme']->filesize_info)
700 . (isset($_SESSION['tmp_user_values']['custom_color']) ? substr($_SESSION['tmp_user_values']['custom_color'],1,6) : '');
704 * $cfg['PmaAbsoluteUri'] is a required directive else cookies won't be
705 * set properly and, depending on browsers, inserting or updating a
706 * record might fail
708 function checkPmaAbsoluteUri()
710 // Setup a default value to let the people and lazy sysadmins work anyway,
711 // they'll get an error if the autodetect code doesn't work
712 $pma_absolute_uri = $this->get('PmaAbsoluteUri');
713 $is_https = $this->detectHttps();
715 if (strlen($pma_absolute_uri) < 5) {
716 $url = array();
718 // At first we try to parse REQUEST_URI, it might contain full URL
720 * REQUEST_URI contains PATH_INFO too, this is not what we want
721 * script-php/pathinfo/
722 if (PMA_getenv('REQUEST_URI')) {
723 $url = @parse_url(PMA_getenv('REQUEST_URI')); // produces E_WARNING if it cannot get parsed, e.g. '/foobar:/'
724 if ($url === false) {
725 $url = array('path' => $_SERVER['REQUEST_URI']);
730 // If we don't have scheme, we didn't have full URL so we need to
731 // dig deeper
732 if (empty($url['scheme'])) {
733 // Scheme
734 if (PMA_getenv('HTTP_SCHEME')) {
735 $url['scheme'] = PMA_getenv('HTTP_SCHEME');
736 } else {
737 $url['scheme'] =
738 PMA_getenv('HTTPS') && strtolower(PMA_getenv('HTTPS')) != 'off'
739 ? 'https'
740 : 'http';
743 // Host and port
744 if (PMA_getenv('HTTP_HOST')) {
745 // Prepend the scheme before using parse_url() since this is not part of the RFC2616 Host request-header
746 $parsed_url = parse_url($url['scheme'] . '://' . PMA_getenv('HTTP_HOST'));
747 if (!empty($parsed_url['host'])) {
748 $url = $parsed_url;
749 } else {
750 $url['host'] = PMA_getenv('HTTP_HOST');
752 } elseif (PMA_getenv('SERVER_NAME')) {
753 $url['host'] = PMA_getenv('SERVER_NAME');
754 } else {
755 $this->error_pma_uri = true;
756 return false;
759 // If we didn't set port yet...
760 if (empty($url['port']) && PMA_getenv('SERVER_PORT')) {
761 $url['port'] = PMA_getenv('SERVER_PORT');
764 // And finally the path could be already set from REQUEST_URI
765 if (empty($url['path'])) {
767 * REQUEST_URI contains PATH_INFO too, this is not what we want
768 * script-php/pathinfo/
769 if (PMA_getenv('PATH_INFO')) {
770 $path = parse_url(PMA_getenv('PATH_INFO'));
771 } else {
772 // PHP_SELF in CGI often points to cgi executable, so use it
773 // as last choice
775 $path = parse_url($GLOBALS['PMA_PHP_SELF']);
777 $url['path'] = $path['path'];
781 // Make url from parts we have
782 $pma_absolute_uri = $url['scheme'] . '://';
783 // Was there user information?
784 if (!empty($url['user'])) {
785 $pma_absolute_uri .= $url['user'];
786 if (!empty($url['pass'])) {
787 $pma_absolute_uri .= ':' . $url['pass'];
789 $pma_absolute_uri .= '@';
791 // Add hostname
792 $pma_absolute_uri .= $url['host'];
793 // Add port, if it not the default one
794 if (! empty($url['port'])
795 && (($url['scheme'] == 'http' && $url['port'] != 80)
796 || ($url['scheme'] == 'https' && $url['port'] != 443))) {
797 $pma_absolute_uri .= ':' . $url['port'];
799 // And finally path, without script name, the 'a' is there not to
800 // strip our directory, when path is only /pmadir/ without filename.
801 // Backslashes returned by Windows have to be changed.
802 // Only replace backslashes by forward slashes if on Windows,
803 // as the backslash could be valid on a non-Windows system.
804 $this->checkWebServerOs();
805 if ($this->get('PMA_IS_WINDOWS') == 1) {
806 $path = str_replace("\\", "/", dirname($url['path'] . 'a'));
807 } else {
808 $path = dirname($url['path'] . 'a');
811 // To work correctly within transformations overview:
812 if (defined('PMA_PATH_TO_BASEDIR') && PMA_PATH_TO_BASEDIR == '../../') {
813 if ($this->get('PMA_IS_WINDOWS') == 1) {
814 $path = str_replace("\\", "/", dirname(dirname($path)));
815 } else {
816 $path = dirname(dirname($path));
820 // PHP's dirname function would have returned a dot when $path contains no slash
821 if ($path == '.') {
822 $path = '';
824 // in vhost situations, there could be already an ending slash
825 if (substr($path, -1) != '/') {
826 $path .= '/';
828 $pma_absolute_uri .= $path;
830 // We used to display a warning if PmaAbsoluteUri wasn't set, but now
831 // the autodetect code works well enough that we don't display the
832 // warning at all. The user can still set PmaAbsoluteUri manually.
833 // See
834 // http://sf.net/tracker/?func=detail&aid=1257134&group_id=23067&atid=377411
836 } else {
837 // The URI is specified, however users do often specify this
838 // wrongly, so we try to fix this.
840 // Adds a trailing slash et the end of the phpMyAdmin uri if it
841 // does not exist.
842 if (substr($pma_absolute_uri, -1) != '/') {
843 $pma_absolute_uri .= '/';
846 // If URI doesn't start with http:// or https://, we will add
847 // this.
848 if (substr($pma_absolute_uri, 0, 7) != 'http://'
849 && substr($pma_absolute_uri, 0, 8) != 'https://') {
850 $pma_absolute_uri =
851 ($is_https ? 'https' : 'http')
852 . ':' . (substr($pma_absolute_uri, 0, 2) == '//' ? '' : '//')
853 . $pma_absolute_uri;
856 $this->set('PmaAbsoluteUri', $pma_absolute_uri);
860 * check selected collation_connection
861 * @todo check validity of $_REQUEST['collation_connection']
863 function checkCollationConnection()
865 if (! empty($_REQUEST['collation_connection'])) {
866 $this->set('collation_connection',
867 strip_tags($_REQUEST['collation_connection']));
872 * checks for font size configuration, and sets font size as requested by user
875 function checkFontsize()
877 $new_fontsize = '';
879 if (isset($_GET['set_fontsize'])) {
880 $new_fontsize = $_GET['set_fontsize'];
881 } elseif (isset($_POST['set_fontsize'])) {
882 $new_fontsize = $_POST['set_fontsize'];
883 } elseif (isset($_COOKIE['pma_fontsize'])) {
884 $new_fontsize = $_COOKIE['pma_fontsize'];
887 if (preg_match('/^[0-9.]+(px|em|pt|\%)$/', $new_fontsize)) {
888 $this->set('fontsize', $new_fontsize);
889 } elseif (! $this->get('fontsize')) {
890 // 80% would correspond to the default browser font size
891 // of 16, but use 82% to help read the monoface font
892 $this->set('fontsize', '82%');
895 $this->setCookie('pma_fontsize', $this->get('fontsize'), '82%');
899 * checks if upload is enabled
903 function checkUpload()
905 if (ini_get('file_uploads')) {
906 $this->set('enable_upload', true);
907 // if set "php_admin_value file_uploads Off" in httpd.conf
908 // ini_get() also returns the string "Off" in this case:
909 if ('off' == strtolower(ini_get('file_uploads'))) {
910 $this->set('enable_upload', false);
912 } else {
913 $this->set('enable_upload', false);
918 * Maximum upload size as limited by PHP
919 * Used with permission from Moodle (http://moodle.org) by Martin Dougiamas
921 * this section generates $max_upload_size in bytes
923 function checkUploadSize()
925 if (! $filesize = ini_get('upload_max_filesize')) {
926 $filesize = "5M";
929 if ($postsize = ini_get('post_max_size')) {
930 $this->set('max_upload_size',
931 min(PMA_get_real_size($filesize), PMA_get_real_size($postsize)));
932 } else {
933 $this->set('max_upload_size', PMA_get_real_size($filesize));
938 * check for https
940 function checkIsHttps()
942 $this->set('is_https', $this->isHttps());
946 * @static
948 public function isHttps()
950 static $is_https = null;
952 if (null !== $is_https) {
953 return $is_https;
956 $url = parse_url($this->get('PmaAbsoluteUri'));
958 if (isset($url['scheme'])
959 && $url['scheme'] == 'https') {
960 $is_https = true;
961 } else {
962 $is_https = false;
965 return $is_https;
969 * Detects whether https appears to be used.
971 * Please note that this just detects what we see, so
972 * it completely ignores things like reverse proxies.
974 function detectHttps()
976 $is_https = false;
978 $url = array();
980 // At first we try to parse REQUEST_URI, it might contain full URL,
981 if (PMA_getenv('REQUEST_URI')) {
982 $url = @parse_url(PMA_getenv('REQUEST_URI')); // produces E_WARNING if it cannot get parsed, e.g. '/foobar:/'
983 if($url === false) {
984 $url = array();
988 // If we don't have scheme, we didn't have full URL so we need to
989 // dig deeper
990 if (empty($url['scheme'])) {
991 // Scheme
992 if (PMA_getenv('HTTP_SCHEME')) {
993 $url['scheme'] = PMA_getenv('HTTP_SCHEME');
994 } else {
995 $url['scheme'] =
996 PMA_getenv('HTTPS') && strtolower(PMA_getenv('HTTPS')) != 'off'
997 ? 'https'
998 : 'http';
1002 if (isset($url['scheme'])
1003 && $url['scheme'] == 'https') {
1004 $is_https = true;
1005 } else {
1006 $is_https = false;
1009 return $is_https;
1013 * detect correct cookie path
1015 function checkCookiePath()
1017 $this->set('cookie_path', $this->getCookiePath());
1021 * @static
1023 public function getCookiePath()
1025 static $cookie_path = null;
1027 if (null !== $cookie_path) {
1028 return $cookie_path;
1031 $parsed_url = parse_url($this->get('PmaAbsoluteUri'));
1033 $cookie_path = $parsed_url['path'];
1035 return $cookie_path;
1039 * enables backward compatibility
1041 function enableBc()
1043 $GLOBALS['cfg'] = $this->settings;
1044 $GLOBALS['default_server'] = $this->default_server;
1045 unset($this->default_server);
1046 $GLOBALS['collation_connection'] = $this->get('collation_connection');
1047 $GLOBALS['is_upload'] = $this->get('enable_upload');
1048 $GLOBALS['max_upload_size'] = $this->get('max_upload_size');
1049 $GLOBALS['cookie_path'] = $this->get('cookie_path');
1050 $GLOBALS['is_https'] = $this->get('is_https');
1052 $defines = array(
1053 'PMA_VERSION',
1054 'PMA_THEME_VERSION',
1055 'PMA_THEME_GENERATION',
1056 'PMA_PHP_STR_VERSION',
1057 'PMA_PHP_INT_VERSION',
1058 'PMA_IS_WINDOWS',
1059 'PMA_IS_IIS',
1060 'PMA_IS_GD2',
1061 'PMA_USR_OS',
1062 'PMA_USR_BROWSER_VER',
1063 'PMA_USR_BROWSER_AGENT'
1066 foreach ($defines as $define) {
1067 if (! defined($define)) {
1068 define($define, $this->get($define));
1074 * @todo finish
1076 function save() {}
1079 * returns options for font size selection
1081 * @static
1082 * @param string $current_size current selected font size with unit
1083 * @return array selectable font sizes
1085 static protected function _getFontsizeOptions($current_size = '82%')
1087 $unit = preg_replace('/[0-9.]*/', '', $current_size);
1088 $value = preg_replace('/[^0-9.]*/', '', $current_size);
1090 $factors = array();
1091 $options = array();
1092 $options["$value"] = $value . $unit;
1094 if ($unit === '%') {
1095 $factors[] = 1;
1096 $factors[] = 5;
1097 $factors[] = 10;
1098 } elseif ($unit === 'em') {
1099 $factors[] = 0.05;
1100 $factors[] = 0.2;
1101 $factors[] = 1;
1102 } elseif ($unit === 'pt') {
1103 $factors[] = 0.5;
1104 $factors[] = 2;
1105 } elseif ($unit === 'px') {
1106 $factors[] = 1;
1107 $factors[] = 5;
1108 $factors[] = 10;
1109 } else {
1110 //unknown font size unit
1111 $factors[] = 0.05;
1112 $factors[] = 0.2;
1113 $factors[] = 1;
1114 $factors[] = 5;
1115 $factors[] = 10;
1118 foreach ($factors as $key => $factor) {
1119 $option_inc = $value + $factor;
1120 $option_dec = $value - $factor;
1121 while (count($options) < 21) {
1122 $options["$option_inc"] = $option_inc . $unit;
1123 if ($option_dec > $factors[0]) {
1124 $options["$option_dec"] = $option_dec . $unit;
1126 $option_inc += $factor;
1127 $option_dec -= $factor;
1128 if (isset($factors[$key + 1])
1129 && $option_inc >= $value + $factors[$key + 1]) {
1130 break;
1134 ksort($options);
1135 return $options;
1139 * returns html selectbox for font sizes
1141 * @static
1142 * @param string $current_size currently slected font size with unit
1143 * @return string html selectbox
1145 static protected function _getFontsizeSelection()
1147 $current_size = $GLOBALS['PMA_Config']->get('fontsize');
1148 // for the case when there is no config file (this is supported)
1149 if (empty($current_size)) {
1150 if (isset($_COOKIE['pma_fontsize'])) {
1151 $current_size = $_COOKIE['pma_fontsize'];
1152 } else {
1153 $current_size = '82%';
1156 $options = PMA_Config::_getFontsizeOptions($current_size);
1158 $return = '<label for="select_fontsize">' . __('Font size') . ':</label>' . "\n";
1159 $return .= '<select name="set_fontsize" id="select_fontsize" onchange="this.form.submit();">' . "\n";
1160 foreach ($options as $option) {
1161 $return .= '<option value="' . $option . '"';
1162 if ($option == $current_size) {
1163 $return .= ' selected="selected"';
1165 $return .= '>' . $option . '</option>' . "\n";
1167 $return .= '</select>';
1169 return $return;
1173 * return complete font size selection form
1175 * @static
1176 * @param string $current_size currently slected font size with unit
1177 * @return string html selectbox
1179 static public function getFontsizeForm()
1181 return '<form name="form_fontsize_selection" id="form_fontsize_selection"'
1182 . ' method="post" action="index.php" target="_parent">' . "\n"
1183 . PMA_generate_common_hidden_inputs() . "\n"
1184 . PMA_Config::_getFontsizeSelection() . "\n"
1185 . '<noscript>' . "\n"
1186 . '<input type="submit" value="' . __('Go') . '" />' . "\n"
1187 . '</noscript>' . "\n"
1188 . '</form>';
1192 * removes cookie
1194 * @param string $cookie name of cookie to remove
1195 * @return boolean result of setcookie()
1197 function removeCookie($cookie)
1199 return setcookie($cookie, '', time() - 3600,
1200 $this->getCookiePath(), '', $this->isHttps());
1204 * sets cookie if value is different from current cokkie value,
1205 * or removes if value is equal to default
1207 * @param string $cookie name of cookie to remove
1208 * @param mixed $value new cookie value
1209 * @param string $default default value
1210 * @param int $validity validity of cookie in seconds (default is one month)
1211 * @param bool $httponlt whether cookie is only for HTTP (and not for scripts)
1212 * @return boolean result of setcookie()
1214 function setCookie($cookie, $value, $default = null, $validity = null, $httponly = true)
1216 if ($validity == null) {
1217 $validity = 2592000;
1219 if (strlen($value) && null !== $default && $value === $default) {
1220 // default value is used
1221 if (isset($_COOKIE[$cookie])) {
1222 // remove cookie
1223 return $this->removeCookie($cookie);
1225 return false;
1228 if (! strlen($value) && isset($_COOKIE[$cookie])) {
1229 // remove cookie, value is empty
1230 return $this->removeCookie($cookie);
1233 if (! isset($_COOKIE[$cookie]) || $_COOKIE[$cookie] !== $value) {
1234 // set cookie with new value
1235 /* Calculate cookie validity */
1236 if ($validity == 0) {
1237 $v = 0;
1238 } else {
1239 $v = time() + $validity;
1241 return setcookie($cookie, $value, $v,
1242 $this->getCookiePath(), '', $this->isHttps(), $httponly);
1245 // cookie has already $value as value
1246 return true;