Translated using Weblate.
[phpmyadmin.git] / libraries / Config.class.php
blob7f25ab67ee5404817df9e016bb4504472ce657ea
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 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 * @return nothing
99 function checkSystem()
101 $this->set('PMA_VERSION', '3.5.0-rc1');
103 * @deprecated
105 $this->set('PMA_THEME_VERSION', 2);
107 * @deprecated
109 $this->set('PMA_THEME_GENERATION', 2);
111 $this->checkPhpVersion();
112 $this->checkWebServerOs();
113 $this->checkWebServer();
114 $this->checkGd2();
115 $this->checkClient();
116 $this->checkUpload();
117 $this->checkUploadSize();
118 $this->checkOutputCompression();
122 * whether to use gzip output compression or not
124 * @return nothing
126 function checkOutputCompression()
128 // If zlib output compression is set in the php configuration file, no
129 // output buffering should be run
130 if (@ini_get('zlib.output_compression')) {
131 $this->set('OBGzip', false);
134 // disable output-buffering (if set to 'auto') for IE6, else enable it.
135 if (strtolower($this->get('OBGzip')) == 'auto') {
136 if ($this->get('PMA_USR_BROWSER_AGENT') == 'IE'
137 && $this->get('PMA_USR_BROWSER_VER') >= 6
138 && $this->get('PMA_USR_BROWSER_VER') < 7
140 $this->set('OBGzip', false);
141 } else {
142 $this->set('OBGzip', true);
148 * Determines platform (OS), browser and version of the user
149 * Based on a phpBuilder article:
151 * @see http://www.phpbuilder.net/columns/tim20000821.php
153 * @return nothing
155 function checkClient()
157 if (PMA_getenv('HTTP_USER_AGENT')) {
158 $HTTP_USER_AGENT = PMA_getenv('HTTP_USER_AGENT');
159 } elseif (! isset($HTTP_USER_AGENT)) {
160 $HTTP_USER_AGENT = '';
163 // 1. Platform
164 if (strstr($HTTP_USER_AGENT, 'Win')) {
165 $this->set('PMA_USR_OS', 'Win');
166 } elseif (strstr($HTTP_USER_AGENT, 'Mac')) {
167 $this->set('PMA_USR_OS', 'Mac');
168 } elseif (strstr($HTTP_USER_AGENT, 'Linux')) {
169 $this->set('PMA_USR_OS', 'Linux');
170 } elseif (strstr($HTTP_USER_AGENT, 'Unix')) {
171 $this->set('PMA_USR_OS', 'Unix');
172 } elseif (strstr($HTTP_USER_AGENT, 'OS/2')) {
173 $this->set('PMA_USR_OS', 'OS/2');
174 } else {
175 $this->set('PMA_USR_OS', 'Other');
178 // 2. browser and version
179 // (must check everything else before Mozilla)
181 if (preg_match(
182 '@Opera(/| )([0-9].[0-9]{1,2})@',
183 $HTTP_USER_AGENT,
184 $log_version
185 )) {
186 $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
187 $this->set('PMA_USR_BROWSER_AGENT', 'OPERA');
188 } elseif (preg_match(
189 '@MSIE ([0-9].[0-9]{1,2})@',
190 $HTTP_USER_AGENT,
191 $log_version
192 )) {
193 $this->set('PMA_USR_BROWSER_VER', $log_version[1]);
194 $this->set('PMA_USR_BROWSER_AGENT', 'IE');
195 } elseif (preg_match(
196 '@OmniWeb/([0-9].[0-9]{1,2})@',
197 $HTTP_USER_AGENT,
198 $log_version
199 )) {
200 $this->set('PMA_USR_BROWSER_VER', $log_version[1]);
201 $this->set('PMA_USR_BROWSER_AGENT', 'OMNIWEB');
202 // Konqueror 2.2.2 says Konqueror/2.2.2
203 // Konqueror 3.0.3 says Konqueror/3
204 } elseif (preg_match(
205 '@(Konqueror/)(.*)(;)@',
206 $HTTP_USER_AGENT,
207 $log_version
208 )) {
209 $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
210 $this->set('PMA_USR_BROWSER_AGENT', 'KONQUEROR');
211 } elseif (preg_match(
212 '@Mozilla/([0-9].[0-9]{1,2})@',
213 $HTTP_USER_AGENT,
214 $log_version)
215 && preg_match('@Safari/([0-9]*)@', $HTTP_USER_AGENT, $log_version2)
217 $this->set('PMA_USR_BROWSER_VER', $log_version[1] . '.' . $log_version2[1]);
218 $this->set('PMA_USR_BROWSER_AGENT', 'SAFARI');
219 } elseif (preg_match('@rv:1.9(.*)Gecko@', $HTTP_USER_AGENT)) {
220 $this->set('PMA_USR_BROWSER_VER', '1.9');
221 $this->set('PMA_USR_BROWSER_AGENT', 'GECKO');
222 } elseif (
223 preg_match('@Mozilla/([0-9].[0-9]{1,2})@',
224 $HTTP_USER_AGENT,
225 $log_version
226 )) {
227 $this->set('PMA_USR_BROWSER_VER', $log_version[1]);
228 $this->set('PMA_USR_BROWSER_AGENT', 'MOZILLA');
229 } else {
230 $this->set('PMA_USR_BROWSER_VER', 0);
231 $this->set('PMA_USR_BROWSER_AGENT', 'OTHER');
236 * Whether GD2 is present
238 * @return nothing
240 function checkGd2()
242 if ($this->get('GD2Available') == 'yes') {
243 $this->set('PMA_IS_GD2', 1);
244 } elseif ($this->get('GD2Available') == 'no') {
245 $this->set('PMA_IS_GD2', 0);
246 } else {
247 if (!@function_exists('imagecreatetruecolor')) {
248 $this->set('PMA_IS_GD2', 0);
249 } else {
250 if (@function_exists('gd_info')) {
251 $gd_nfo = gd_info();
252 if (strstr($gd_nfo["GD Version"], '2.')) {
253 $this->set('PMA_IS_GD2', 1);
254 } else {
255 $this->set('PMA_IS_GD2', 0);
257 } else {
258 /* We must do hard way... but almost no chance to execute this */
259 ob_start();
260 phpinfo(INFO_MODULES); /* Only modules */
261 $a = strip_tags(ob_get_contents());
262 ob_end_clean();
263 /* Get GD version string from phpinfo output */
264 if (preg_match('@GD Version[[:space:]]*\(.*\)@', $a, $v)) {
265 if (strstr($v, '2.')) {
266 $this->set('PMA_IS_GD2', 1);
267 } else {
268 $this->set('PMA_IS_GD2', 0);
270 } else {
271 $this->set('PMA_IS_GD2', 0);
279 * Whether the Web server php is running on is IIS
281 * @return nothing
283 function checkWebServer()
285 if (PMA_getenv('SERVER_SOFTWARE')
286 // some versions return Microsoft-IIS, some Microsoft/IIS
287 // we could use a preg_match() but it's slower
288 && stristr(PMA_getenv('SERVER_SOFTWARE'), 'Microsoft')
289 && stristr(PMA_getenv('SERVER_SOFTWARE'), 'IIS')
291 $this->set('PMA_IS_IIS', 1);
292 } else {
293 $this->set('PMA_IS_IIS', 0);
298 * Whether the os php is running on is windows or not
300 * @return nothing
302 function checkWebServerOs()
304 // Default to Unix or Equiv
305 $this->set('PMA_IS_WINDOWS', 0);
306 // If PHP_OS is defined then continue
307 if (defined('PHP_OS')) {
308 if (stristr(PHP_OS, 'win')) {
309 // Is it some version of Windows
310 $this->set('PMA_IS_WINDOWS', 1);
311 } elseif (stristr(PHP_OS, 'OS/2')) {
312 // Is it OS/2 (No file permissions like Windows)
313 $this->set('PMA_IS_WINDOWS', 1);
319 * detects PHP version
321 * @return nothing
323 function checkPhpVersion()
325 $match = array();
326 if (! preg_match(
327 '@([0-9]{1,2}).([0-9]{1,2}).([0-9]{1,2})@',
328 phpversion(),
329 $match
330 )) {
331 preg_match(
332 '@([0-9]{1,2}).([0-9]{1,2})@',
333 phpversion(),
334 $match
337 if (isset($match) && ! empty($match[1])) {
338 if (! isset($match[2])) {
339 $match[2] = 0;
341 if (! isset($match[3])) {
342 $match[3] = 0;
344 $this->set(
345 'PMA_PHP_INT_VERSION',
346 (int) sprintf('%d%02d%02d', $match[1], $match[2], $match[3])
348 } else {
349 $this->set('PMA_PHP_INT_VERSION', 0);
351 $this->set('PMA_PHP_STR_VERSION', phpversion());
355 * loads default values from default source
357 * @return boolean success
359 function loadDefaults()
361 $cfg = array();
362 if (! file_exists($this->default_source)) {
363 $this->error_config_default_file = true;
364 return false;
366 include $this->default_source;
368 $this->default_source_mtime = filemtime($this->default_source);
370 $this->default_server = $cfg['Servers'][1];
371 unset($cfg['Servers']);
373 $this->default = $cfg;
374 $this->settings = PMA_array_merge_recursive($this->settings, $cfg);
376 $this->error_config_default_file = false;
378 return true;
382 * loads configuration from $source, usally the config file
383 * should be called on object creation
385 * @param string $source config file
387 * @return bool
389 function load($source = null)
391 $this->loadDefaults();
393 if (null !== $source) {
394 $this->setSource($source);
397 if (! $this->checkConfigSource()) {
398 return false;
401 $cfg = array();
404 * Parses the configuration file, the eval is used here to avoid
405 * problems with trailing whitespace, what is often a problem.
407 $old_error_reporting = error_reporting(0);
408 $eval_result = eval('?' . '>' . trim(file_get_contents($this->getSource())));
409 error_reporting($old_error_reporting);
411 if ($eval_result === false) {
412 $this->error_config_file = true;
413 } else {
414 $this->error_config_file = false;
415 $this->source_mtime = filemtime($this->getSource());
419 * Backward compatibility code
421 if (!empty($cfg['DefaultTabTable'])) {
422 $cfg['DefaultTabTable'] = str_replace(
423 '_properties',
425 str_replace(
426 'tbl_properties.php',
427 'tbl_sql.php',
428 $cfg['DefaultTabTable']
432 if (!empty($cfg['DefaultTabDatabase'])) {
433 $cfg['DefaultTabDatabase'] = str_replace(
434 '_details',
436 str_replace(
437 'db_details.php',
438 'db_sql.php',
439 $cfg['DefaultTabDatabase']
444 $this->settings = PMA_array_merge_recursive($this->settings, $cfg);
445 $this->checkPmaAbsoluteUri();
446 $this->checkFontsize();
448 $this->checkPermissions();
450 // Handling of the collation must be done after merging of $cfg
451 // (from config.inc.php) so that $cfg['DefaultConnectionCollation']
452 // can have an effect. Note that the presence of collation
453 // information in a cookie has priority over what is defined
454 // in the default or user's config files.
456 * @todo check validity of $_COOKIE['pma_collation_connection']
458 if (! empty($_COOKIE['pma_collation_connection'])) {
459 $this->set(
460 'collation_connection',
461 strip_tags($_COOKIE['pma_collation_connection'])
463 } else {
464 $this->set(
465 'collation_connection',
466 $this->get('DefaultConnectionCollation')
469 // Now, a collation information could come from REQUEST
470 // (an example of this: the collation selector in main.php)
471 // so the following handles the setting of collation_connection
472 // and later, in common.inc.php, the cookie will be set
473 // according to this.
474 $this->checkCollationConnection();
476 return true;
480 * Loads user preferences and merges them with current config
481 * must be called after control connection has been estabilished
483 * @return boolean
485 function loadUserPreferences()
487 // index.php should load these settings, so that phpmyadmin.css.php
488 // will have everything avaiable in session cache
489 $server = isset($GLOBALS['server'])
490 ? $GLOBALS['server']
491 : (!empty($GLOBALS['cfg']['ServerDefault'])
492 ? $GLOBALS['cfg']['ServerDefault']
493 : 0);
494 $cache_key = 'server_' . $server;
495 if ($server > 0 && !defined('PMA_MINIMUM_COMMON')) {
496 $config_mtime = max($this->default_source_mtime, $this->source_mtime);
497 // cache user preferences, use database only when needed
498 if (! isset($_SESSION['cache'][$cache_key]['userprefs'])
499 || $_SESSION['cache'][$cache_key]['config_mtime'] < $config_mtime
501 // load required libraries
502 include_once './libraries/user_preferences.lib.php';
503 $prefs = PMA_load_userprefs();
504 $_SESSION['cache'][$cache_key]['userprefs']
505 = PMA_apply_userprefs($prefs['config_data']);
506 $_SESSION['cache'][$cache_key]['userprefs_mtime'] = $prefs['mtime'];
507 $_SESSION['cache'][$cache_key]['userprefs_type'] = $prefs['type'];
508 $_SESSION['cache'][$cache_key]['config_mtime'] = $config_mtime;
510 } elseif ($server == 0
511 || ! isset($_SESSION['cache'][$cache_key]['userprefs'])
513 $this->set('user_preferences', false);
514 return;
516 $config_data = $_SESSION['cache'][$cache_key]['userprefs'];
517 // type is 'db' or 'session'
518 $this->set(
519 'user_preferences',
520 $_SESSION['cache'][$cache_key]['userprefs_type']
522 $this->set(
523 'user_preferences_mtime',
524 $_SESSION['cache'][$cache_key]['userprefs_mtime']
527 // backup some settings
528 $org_fontsize = $this->settings['fontsize'];
529 // load config array
530 $this->settings = PMA_array_merge_recursive($this->settings, $config_data);
531 $GLOBALS['cfg'] = PMA_array_merge_recursive($GLOBALS['cfg'], $config_data);
532 if (defined('PMA_MINIMUM_COMMON')) {
533 return;
536 // settings below start really working on next page load, but
537 // changes are made only in index.php so everything is set when
538 // in frames
540 // save theme
541 $tmanager = $_SESSION['PMA_Theme_Manager'];
542 if ($tmanager->getThemeCookie() || isset($_REQUEST['set_theme'])) {
543 if ((! isset($config_data['ThemeDefault'])
544 && $tmanager->theme->getId() != 'original')
545 || isset($config_data['ThemeDefault'])
546 && $config_data['ThemeDefault'] != $tmanager->theme->getId()
548 // new theme was set in common.inc.php
549 $this->setUserValue(
550 null,
551 'ThemeDefault',
552 $tmanager->theme->getId(),
553 'original'
556 } else {
557 // no cookie - read default from settings
558 if ($this->settings['ThemeDefault'] != $tmanager->theme->getId()
559 && $tmanager->checkTheme($this->settings['ThemeDefault'])
561 $tmanager->setActiveTheme($this->settings['ThemeDefault']);
562 $tmanager->setThemeCookie();
566 // save font size
567 if ((! isset($config_data['fontsize'])
568 && $org_fontsize != '82%')
569 || isset($config_data['fontsize'])
570 && $org_fontsize != $config_data['fontsize']
572 $this->setUserValue(null, 'fontsize', $org_fontsize, '82%');
575 // save language
576 if (isset($_COOKIE['pma_lang']) || isset($_POST['lang'])) {
577 if ((! isset($config_data['lang'])
578 && $GLOBALS['lang'] != 'en')
579 || isset($config_data['lang'])
580 && $GLOBALS['lang'] != $config_data['lang']
582 $this->setUserValue(null, 'lang', $GLOBALS['lang'], 'en');
584 } else {
585 // read language from settings
586 if (isset($config_data['lang']) && PMA_langSet($config_data['lang'])) {
587 $this->setCookie('pma_lang', $GLOBALS['lang']);
591 // save connection collation
592 if (isset($_COOKIE['pma_collation_connection'])
593 || isset($_POST['collation_connection'])
595 if ((! isset($config_data['collation_connection'])
596 && $GLOBALS['collation_connection'] != 'utf8_general_ci')
597 || isset($config_data['collation_connection'])
598 && $GLOBALS['collation_connection']
599 != $config_data['collation_connection']
601 $this->setUserValue(
602 null,
603 'collation_connection',
604 $GLOBALS['collation_connection'],
605 'utf8_general_ci'
608 } else {
609 // read collation from settings
610 if (isset($config_data['collation_connection'])) {
611 $GLOBALS['collation_connection']
612 = $config_data['collation_connection'];
613 $this->setCookie(
614 'pma_collation_connection',
615 $GLOBALS['collation_connection']
622 * Sets config value which is stored in user preferences (if available)
623 * or in a cookie.
625 * If user preferences are not yet initialized, option is applied to
626 * global config and added to a update queue, which is processed
627 * by {@link loadUserPreferences()}
629 * @param string $cookie_name can be null
630 * @param string $cfg_path
631 * @param mixed $new_cfg_value new value
632 * @param mixed $default_value default value
634 * @return nothing
636 function setUserValue($cookie_name, $cfg_path, $new_cfg_value, $default_value = null)
638 // use permanent user preferences if possible
639 $prefs_type = $this->get('user_preferences');
640 if ($prefs_type) {
641 include_once './libraries/user_preferences.lib.php';
642 if ($default_value === null) {
643 $default_value = PMA_array_read($cfg_path, $this->default);
645 PMA_persist_option($cfg_path, $new_cfg_value, $default_value);
647 if ($prefs_type != 'db' && $cookie_name) {
648 // fall back to cookies
649 if ($default_value === null) {
650 $default_value = PMA_array_read($cfg_path, $this->settings);
652 $this->setCookie($cookie_name, $new_cfg_value, $default_value);
654 PMA_array_write($cfg_path, $GLOBALS['cfg'], $new_cfg_value);
655 PMA_array_write($cfg_path, $this->settings, $new_cfg_value);
659 * Reads value stored by {@link setUserValue()}
661 * @param string $cookie_name cookie name
662 * @param mixed $cfg_value config value
664 * @return mixed
666 function getUserValue($cookie_name, $cfg_value)
668 $cookie_exists = isset($_COOKIE) && !empty($_COOKIE[$cookie_name]);
669 $prefs_type = $this->get('user_preferences');
670 if ($prefs_type == 'db') {
671 // permanent user preferences value exists, remove cookie
672 if ($cookie_exists) {
673 $this->removeCookie($cookie_name);
675 } else if ($cookie_exists) {
676 return $_COOKIE[$cookie_name];
678 // return value from $cfg array
679 return $cfg_value;
683 * set source
685 * @param string $source
687 * @return nothing
689 function setSource($source)
691 $this->source = trim($source);
695 * checks if the config folder still exists and terminates app if true
697 * @return nothing
699 function checkConfigFolder()
701 // Refuse to work while there still might be some world writable dir:
702 if (is_dir('./config')) {
703 die(__('Remove "./config" directory before using phpMyAdmin!'));
708 * check config source
710 * @return boolean whether source is valid or not
712 function checkConfigSource()
714 if (! $this->getSource()) {
715 // no configuration file set at all
716 return false;
719 if (! file_exists($this->getSource())) {
720 $this->source_mtime = 0;
721 return false;
724 if (! is_readable($this->getSource())) {
725 $this->source_mtime = 0;
726 die(
727 sprintf(__('Existing configuration file (%s) is not readable.'),
728 $this->getSource()
733 return true;
737 * verifies the permissions on config file (if asked by configuration)
738 * (must be called after config.inc.php has been merged)
740 * @return nothing
742 function checkPermissions()
744 // Check for permissions (on platforms that support it):
745 if ($this->get('CheckConfigurationPermissions')) {
746 $perms = @fileperms($this->getSource());
747 if (!($perms === false) && ($perms & 2)) {
748 // This check is normally done after loading configuration
749 $this->checkWebServerOs();
750 if ($this->get('PMA_IS_WINDOWS') == 0) {
751 $this->source_mtime = 0;
752 die(__('Wrong permissions on configuration file, should not be world writable!'));
759 * returns specific config setting
761 * @param string $setting config setting
763 * @return mixed value
765 function get($setting)
767 if (isset($this->settings[$setting])) {
768 return $this->settings[$setting];
770 return null;
774 * sets configuration variable
776 * @param string $setting configuration option
777 * @param string $value new value for configuration option
779 * @return nothing
781 function set($setting, $value)
783 if (! isset($this->settings[$setting])
784 || $this->settings[$setting] != $value
786 $this->settings[$setting] = $value;
787 $this->set_mtime = time();
792 * returns source for current config
794 * @return string config source
796 function getSource()
798 return $this->source;
802 * returns a unique value to force a CSS reload if either the config
803 * or the theme changes
804 * must also check the pma_fontsize cookie in case there is no
805 * config file
807 * @return int Summary of unix timestamps and fontsize,
808 * to be unique on theme parameters change
810 function getThemeUniqueValue()
812 if (null !== $this->get('fontsize')) {
813 $fontsize = intval($this->get('fontsize'));
814 } elseif (isset($_COOKIE['pma_fontsize'])) {
815 $fontsize = intval($_COOKIE['pma_fontsize']);
816 } else {
817 $fontsize = 0;
819 return (
820 $fontsize +
821 $this->source_mtime +
822 $this->default_source_mtime +
823 $this->get('user_preferences_mtime') +
824 $_SESSION['PMA_Theme']->mtime_info +
825 $_SESSION['PMA_Theme']->filesize_info);
829 * $cfg['PmaAbsoluteUri'] is a required directive else cookies won't be
830 * set properly and, depending on browsers, inserting or updating a
831 * record might fail
833 * @return bool
835 function checkPmaAbsoluteUri()
837 // Setup a default value to let the people and lazy sysadmins work anyway,
838 // they'll get an error if the autodetect code doesn't work
839 $pma_absolute_uri = $this->get('PmaAbsoluteUri');
840 $is_https = $this->detectHttps();
842 if (strlen($pma_absolute_uri) < 5) {
843 $url = array();
845 // If we don't have scheme, we didn't have full URL so we need to
846 // dig deeper
847 if (empty($url['scheme'])) {
848 // Scheme
849 if ($is_https) {
850 $url['scheme'] = 'https';
851 } else {
852 $url['scheme'] = 'http';
855 // Host and port
856 if (PMA_getenv('HTTP_HOST')) {
857 // Prepend the scheme before using parse_url() since this
858 // is not part of the RFC2616 Host request-header
859 $parsed_url = parse_url(
860 $url['scheme'] . '://' . PMA_getenv('HTTP_HOST')
862 if (!empty($parsed_url['host'])) {
863 $url = $parsed_url;
864 } else {
865 $url['host'] = PMA_getenv('HTTP_HOST');
867 } elseif (PMA_getenv('SERVER_NAME')) {
868 $url['host'] = PMA_getenv('SERVER_NAME');
869 } else {
870 $this->error_pma_uri = true;
871 return false;
874 // If we didn't set port yet...
875 if (empty($url['port']) && PMA_getenv('SERVER_PORT')) {
876 $url['port'] = PMA_getenv('SERVER_PORT');
879 // And finally the path could be already set from REQUEST_URI
880 if (empty($url['path'])) {
881 $path = parse_url($GLOBALS['PMA_PHP_SELF']);
882 $url['path'] = $path['path'];
886 // Make url from parts we have
887 $pma_absolute_uri = $url['scheme'] . '://';
888 // Was there user information?
889 if (!empty($url['user'])) {
890 $pma_absolute_uri .= $url['user'];
891 if (!empty($url['pass'])) {
892 $pma_absolute_uri .= ':' . $url['pass'];
894 $pma_absolute_uri .= '@';
896 // Add hostname
897 $pma_absolute_uri .= $url['host'];
898 // Add port, if it not the default one
899 if (! empty($url['port'])
900 && (($url['scheme'] == 'http' && $url['port'] != 80)
901 || ($url['scheme'] == 'https' && $url['port'] != 443))
903 $pma_absolute_uri .= ':' . $url['port'];
905 // And finally path, without script name, the 'a' is there not to
906 // strip our directory, when path is only /pmadir/ without filename.
907 // Backslashes returned by Windows have to be changed.
908 // Only replace backslashes by forward slashes if on Windows,
909 // as the backslash could be valid on a non-Windows system.
910 $this->checkWebServerOs();
911 if ($this->get('PMA_IS_WINDOWS') == 1) {
912 $path = str_replace("\\", "/", dirname($url['path'] . 'a'));
913 } else {
914 $path = dirname($url['path'] . 'a');
917 // To work correctly within transformations overview:
918 if (defined('PMA_PATH_TO_BASEDIR') && PMA_PATH_TO_BASEDIR == '../../') {
919 if ($this->get('PMA_IS_WINDOWS') == 1) {
920 $path = str_replace("\\", "/", dirname(dirname($path)));
921 } else {
922 $path = dirname(dirname($path));
926 // PHP's dirname function would have returned a dot
927 // when $path contains no slash
928 if ($path == '.') {
929 $path = '';
931 // in vhost situations, there could be already an ending slash
932 if (substr($path, -1) != '/') {
933 $path .= '/';
935 $pma_absolute_uri .= $path;
937 // We used to display a warning if PmaAbsoluteUri wasn't set, but now
938 // the autodetect code works well enough that we don't display the
939 // warning at all. The user can still set PmaAbsoluteUri manually.
940 // See
941 // http://sf.net/tracker/?func=detail&aid=1257134&group_id=23067&atid=377411
943 } else {
944 // The URI is specified, however users do often specify this
945 // wrongly, so we try to fix this.
947 // Adds a trailing slash et the end of the phpMyAdmin uri if it
948 // does not exist.
949 if (substr($pma_absolute_uri, -1) != '/') {
950 $pma_absolute_uri .= '/';
953 // If URI doesn't start with http:// or https://, we will add
954 // this.
955 if (substr($pma_absolute_uri, 0, 7) != 'http://'
956 && substr($pma_absolute_uri, 0, 8) != 'https://'
958 $pma_absolute_uri
959 = ($is_https ? 'https' : 'http')
960 . ':' . (substr($pma_absolute_uri, 0, 2) == '//' ? '' : '//')
961 . $pma_absolute_uri;
964 $this->set('PmaAbsoluteUri', $pma_absolute_uri);
968 * check selected collation_connection
970 * @todo check validity of $_REQUEST['collation_connection']
972 * @return nothing
974 function checkCollationConnection()
976 if (! empty($_REQUEST['collation_connection'])) {
977 $this->set(
978 'collation_connection',
979 strip_tags($_REQUEST['collation_connection'])
985 * checks for font size configuration, and sets font size as requested by user
987 * @return nothing
989 function checkFontsize()
991 $new_fontsize = '';
993 if (isset($_GET['set_fontsize'])) {
994 $new_fontsize = $_GET['set_fontsize'];
995 } elseif (isset($_POST['set_fontsize'])) {
996 $new_fontsize = $_POST['set_fontsize'];
997 } elseif (isset($_COOKIE['pma_fontsize'])) {
998 $new_fontsize = $_COOKIE['pma_fontsize'];
1001 if (preg_match('/^[0-9.]+(px|em|pt|\%)$/', $new_fontsize)) {
1002 $this->set('fontsize', $new_fontsize);
1003 } elseif (! $this->get('fontsize')) {
1004 // 80% would correspond to the default browser font size
1005 // of 16, but use 82% to help read the monoface font
1006 $this->set('fontsize', '82%');
1009 $this->setCookie('pma_fontsize', $this->get('fontsize'), '82%');
1013 * checks if upload is enabled
1015 * @return nothing
1017 function checkUpload()
1019 if (ini_get('file_uploads')) {
1020 $this->set('enable_upload', true);
1021 // if set "php_admin_value file_uploads Off" in httpd.conf
1022 // ini_get() also returns the string "Off" in this case:
1023 if ('off' == strtolower(ini_get('file_uploads'))) {
1024 $this->set('enable_upload', false);
1026 } else {
1027 $this->set('enable_upload', false);
1032 * Maximum upload size as limited by PHP
1033 * Used with permission from Moodle (http://moodle.org) by Martin Dougiamas
1035 * this section generates $max_upload_size in bytes
1037 * @return nothing
1039 function checkUploadSize()
1041 if (! $filesize = ini_get('upload_max_filesize')) {
1042 $filesize = "5M";
1045 if ($postsize = ini_get('post_max_size')) {
1046 $this->set(
1047 'max_upload_size',
1048 min(PMA_get_real_size($filesize), PMA_get_real_size($postsize))
1050 } else {
1051 $this->set('max_upload_size', PMA_get_real_size($filesize));
1056 * check for https
1058 * @return nothing
1060 function checkIsHttps()
1062 $this->set('is_https', $this->isHttps());
1066 * Checks if protocol is https
1068 * This function checks if the https protocol is used in the PmaAbsoluteUri
1069 * configuration setting, as opposed to detectHttps() which checks if the
1070 * https protocol is used on the active connection.
1072 * @return bool
1074 public function isHttps()
1076 static $is_https = null;
1078 if (null !== $is_https) {
1079 return $is_https;
1082 $url = parse_url($this->get('PmaAbsoluteUri'));
1084 if (isset($url['scheme']) && $url['scheme'] == 'https') {
1085 $is_https = true;
1086 } else {
1087 $is_https = false;
1090 return $is_https;
1094 * Detects whether https appears to be used.
1096 * This function checks if the https protocol is used in the current connection
1097 * with the webserver, based on environment variables.
1098 * Please note that this just detects what we see, so
1099 * it completely ignores things like reverse proxies.
1101 * @return bool
1103 function detectHttps()
1105 $is_https = false;
1107 $url = array();
1109 // At first we try to parse REQUEST_URI, it might contain full URL,
1110 if (PMA_getenv('REQUEST_URI')) {
1111 // produces E_WARNING if it cannot get parsed, e.g. '/foobar:/'
1112 $url = @parse_url(PMA_getenv('REQUEST_URI'));
1113 if ($url === false) {
1114 $url = array();
1118 // If we don't have scheme, we didn't have full URL so we need to
1119 // dig deeper
1120 if (empty($url['scheme'])) {
1121 // Scheme
1122 if (PMA_getenv('HTTP_SCHEME')) {
1123 $url['scheme'] = PMA_getenv('HTTP_SCHEME');
1124 } elseif (PMA_getenv('HTTPS') && strtolower(PMA_getenv('HTTPS')) == 'on') {
1125 $url['scheme'] = 'https';
1126 } elseif (PMA_getenv('HTTP_X_FORWARDED_PROTO')) {
1127 $url['scheme'] = strtolower(PMA_getenv('HTTP_X_FORWARDED_PROTO'));
1128 } elseif (PMA_getenv('HTTP_FRONT_END_HTTPS') && strtolower(PMA_getenv('HTTP_FRONT_END_HTTPS')) == 'on') {
1129 $url['scheme'] = 'https';
1130 } else {
1131 $url['scheme'] = 'http';
1135 if (isset($url['scheme']) && $url['scheme'] == 'https') {
1136 $is_https = true;
1137 } else {
1138 $is_https = false;
1141 return $is_https;
1145 * detect correct cookie path
1147 * @return nothing
1149 function checkCookiePath()
1151 $this->set('cookie_path', $this->getCookiePath());
1155 * Get cookie path
1157 * @return string
1159 public function getCookiePath()
1161 static $cookie_path = null;
1163 if (null !== $cookie_path) {
1164 return $cookie_path;
1167 $parsed_url = parse_url($this->get('PmaAbsoluteUri'));
1169 $cookie_path = $parsed_url['path'];
1171 return $cookie_path;
1175 * enables backward compatibility
1177 * @return nothing
1179 function enableBc()
1181 $GLOBALS['cfg'] = $this->settings;
1182 $GLOBALS['default_server'] = $this->default_server;
1183 unset($this->default_server);
1184 $GLOBALS['collation_connection'] = $this->get('collation_connection');
1185 $GLOBALS['is_upload'] = $this->get('enable_upload');
1186 $GLOBALS['max_upload_size'] = $this->get('max_upload_size');
1187 $GLOBALS['cookie_path'] = $this->get('cookie_path');
1188 $GLOBALS['is_https'] = $this->get('is_https');
1190 $defines = array(
1191 'PMA_VERSION',
1192 'PMA_THEME_VERSION',
1193 'PMA_THEME_GENERATION',
1194 'PMA_PHP_STR_VERSION',
1195 'PMA_PHP_INT_VERSION',
1196 'PMA_IS_WINDOWS',
1197 'PMA_IS_IIS',
1198 'PMA_IS_GD2',
1199 'PMA_USR_OS',
1200 'PMA_USR_BROWSER_VER',
1201 'PMA_USR_BROWSER_AGENT'
1204 foreach ($defines as $define) {
1205 if (! defined($define)) {
1206 define($define, $this->get($define));
1212 * @todo finish
1214 * @return nothing
1216 function save()
1221 * returns options for font size selection
1223 * @static
1224 * @param string $current_size current selected font size with unit
1226 * @return array selectable font sizes
1228 static protected function _getFontsizeOptions($current_size = '82%')
1230 $unit = preg_replace('/[0-9.]*/', '', $current_size);
1231 $value = preg_replace('/[^0-9.]*/', '', $current_size);
1233 $factors = array();
1234 $options = array();
1235 $options["$value"] = $value . $unit;
1237 if ($unit === '%') {
1238 $factors[] = 1;
1239 $factors[] = 5;
1240 $factors[] = 10;
1241 } elseif ($unit === 'em') {
1242 $factors[] = 0.05;
1243 $factors[] = 0.2;
1244 $factors[] = 1;
1245 } elseif ($unit === 'pt') {
1246 $factors[] = 0.5;
1247 $factors[] = 2;
1248 } elseif ($unit === 'px') {
1249 $factors[] = 1;
1250 $factors[] = 5;
1251 $factors[] = 10;
1252 } else {
1253 //unknown font size unit
1254 $factors[] = 0.05;
1255 $factors[] = 0.2;
1256 $factors[] = 1;
1257 $factors[] = 5;
1258 $factors[] = 10;
1261 foreach ($factors as $key => $factor) {
1262 $option_inc = $value + $factor;
1263 $option_dec = $value - $factor;
1264 while (count($options) < 21) {
1265 $options["$option_inc"] = $option_inc . $unit;
1266 if ($option_dec > $factors[0]) {
1267 $options["$option_dec"] = $option_dec . $unit;
1269 $option_inc += $factor;
1270 $option_dec -= $factor;
1271 if (isset($factors[$key + 1])
1272 && $option_inc >= $value + $factors[$key + 1]
1274 break;
1278 ksort($options);
1279 return $options;
1283 * returns html selectbox for font sizes
1285 * @static
1286 * @param string $current_size currently slected font size with unit
1288 * @return string html selectbox
1290 static protected function _getFontsizeSelection()
1292 $current_size = $GLOBALS['PMA_Config']->get('fontsize');
1293 // for the case when there is no config file (this is supported)
1294 if (empty($current_size)) {
1295 if (isset($_COOKIE['pma_fontsize'])) {
1296 $current_size = $_COOKIE['pma_fontsize'];
1297 } else {
1298 $current_size = '82%';
1301 $options = PMA_Config::_getFontsizeOptions($current_size);
1303 $return = '<label for="select_fontsize">' . __('Font size') . ':</label>' . "\n";
1304 $return .= '<select name="set_fontsize" id="select_fontsize" class="autosubmit">' . "\n";
1305 foreach ($options as $option) {
1306 $return .= '<option value="' . $option . '"';
1307 if ($option == $current_size) {
1308 $return .= ' selected="selected"';
1310 $return .= '>' . $option . '</option>' . "\n";
1312 $return .= '</select>';
1314 return $return;
1318 * return complete font size selection form
1320 * @static
1321 * @param string $current_size currently slected font size with unit
1323 * @return string html selectbox
1325 static public function getFontsizeForm()
1327 return '<form name="form_fontsize_selection" id="form_fontsize_selection"'
1328 . ' method="post" action="index.php" target="_parent">' . "\n"
1329 . PMA_generate_common_hidden_inputs() . "\n"
1330 . PMA_Config::_getFontsizeSelection() . "\n"
1331 . '<noscript>' . "\n"
1332 . '<input type="submit" value="' . __('Go') . '" />' . "\n"
1333 . '</noscript>' . "\n"
1334 . '</form>';
1338 * removes cookie
1340 * @param string $cookie name of cookie to remove
1342 * @return boolean result of setcookie()
1344 function removeCookie($cookie)
1346 return setcookie(
1347 $cookie,
1349 time() - 3600,
1350 $this->getCookiePath(),
1352 $this->isHttps()
1357 * sets cookie if value is different from current cokkie value,
1358 * or removes if value is equal to default
1360 * @param string $cookie name of cookie to remove
1361 * @param mixed $value new cookie value
1362 * @param string $default default value
1363 * @param int $validity validity of cookie in seconds (default is one month)
1364 * @param bool $httponly whether cookie is only for HTTP (and not for scripts)
1366 * @return boolean result of setcookie()
1368 function setCookie($cookie, $value, $default = null, $validity = null, $httponly = true)
1370 if ($validity == null) {
1371 $validity = 2592000;
1373 if (strlen($value) && null !== $default && $value === $default) {
1374 // default value is used
1375 if (isset($_COOKIE[$cookie])) {
1376 // remove cookie
1377 return $this->removeCookie($cookie);
1379 return false;
1382 if (! strlen($value) && isset($_COOKIE[$cookie])) {
1383 // remove cookie, value is empty
1384 return $this->removeCookie($cookie);
1387 if (! isset($_COOKIE[$cookie]) || $_COOKIE[$cookie] !== $value) {
1388 // set cookie with new value
1389 /* Calculate cookie validity */
1390 if ($validity == 0) {
1391 $v = 0;
1392 } else {
1393 $v = time() + $validity;
1395 return setcookie(
1396 $cookie,
1397 $value,
1399 $this->getCookiePath(),
1401 $this->isHttps(),
1402 $httponly
1406 // cookie has already $value as value
1407 return true;