Translation update done using Pootle.
[phpmyadmin.git] / libraries / Config.class.php
blobe655d9b0708eb63d541843e060e900775147f179
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-dev');
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 (PMA_getenv('HTTP_SCHEME')) {
850 $url['scheme'] = PMA_getenv('HTTP_SCHEME');
851 } else {
852 $url['scheme'] = PMA_getenv('HTTPS')
853 && strtolower(PMA_getenv('HTTPS')) != 'off'
854 ? 'https'
855 : 'http';
858 // Host and port
859 if (PMA_getenv('HTTP_HOST')) {
860 // Prepend the scheme before using parse_url() since this
861 // is not part of the RFC2616 Host request-header
862 $parsed_url = parse_url(
863 $url['scheme'] . '://' . PMA_getenv('HTTP_HOST')
865 if (!empty($parsed_url['host'])) {
866 $url = $parsed_url;
867 } else {
868 $url['host'] = PMA_getenv('HTTP_HOST');
870 } elseif (PMA_getenv('SERVER_NAME')) {
871 $url['host'] = PMA_getenv('SERVER_NAME');
872 } else {
873 $this->error_pma_uri = true;
874 return false;
877 // If we didn't set port yet...
878 if (empty($url['port']) && PMA_getenv('SERVER_PORT')) {
879 $url['port'] = PMA_getenv('SERVER_PORT');
882 // And finally the path could be already set from REQUEST_URI
883 if (empty($url['path'])) {
884 $path = parse_url($GLOBALS['PMA_PHP_SELF']);
885 $url['path'] = $path['path'];
889 // Make url from parts we have
890 $pma_absolute_uri = $url['scheme'] . '://';
891 // Was there user information?
892 if (!empty($url['user'])) {
893 $pma_absolute_uri .= $url['user'];
894 if (!empty($url['pass'])) {
895 $pma_absolute_uri .= ':' . $url['pass'];
897 $pma_absolute_uri .= '@';
899 // Add hostname
900 $pma_absolute_uri .= $url['host'];
901 // Add port, if it not the default one
902 if (! empty($url['port'])
903 && (($url['scheme'] == 'http' && $url['port'] != 80)
904 || ($url['scheme'] == 'https' && $url['port'] != 443))
906 $pma_absolute_uri .= ':' . $url['port'];
908 // And finally path, without script name, the 'a' is there not to
909 // strip our directory, when path is only /pmadir/ without filename.
910 // Backslashes returned by Windows have to be changed.
911 // Only replace backslashes by forward slashes if on Windows,
912 // as the backslash could be valid on a non-Windows system.
913 $this->checkWebServerOs();
914 if ($this->get('PMA_IS_WINDOWS') == 1) {
915 $path = str_replace("\\", "/", dirname($url['path'] . 'a'));
916 } else {
917 $path = dirname($url['path'] . 'a');
920 // To work correctly within transformations overview:
921 if (defined('PMA_PATH_TO_BASEDIR') && PMA_PATH_TO_BASEDIR == '../../') {
922 if ($this->get('PMA_IS_WINDOWS') == 1) {
923 $path = str_replace("\\", "/", dirname(dirname($path)));
924 } else {
925 $path = dirname(dirname($path));
929 // PHP's dirname function would have returned a dot
930 // when $path contains no slash
931 if ($path == '.') {
932 $path = '';
934 // in vhost situations, there could be already an ending slash
935 if (substr($path, -1) != '/') {
936 $path .= '/';
938 $pma_absolute_uri .= $path;
940 // We used to display a warning if PmaAbsoluteUri wasn't set, but now
941 // the autodetect code works well enough that we don't display the
942 // warning at all. The user can still set PmaAbsoluteUri manually.
943 // See
944 // http://sf.net/tracker/?func=detail&aid=1257134&group_id=23067&atid=377411
946 } else {
947 // The URI is specified, however users do often specify this
948 // wrongly, so we try to fix this.
950 // Adds a trailing slash et the end of the phpMyAdmin uri if it
951 // does not exist.
952 if (substr($pma_absolute_uri, -1) != '/') {
953 $pma_absolute_uri .= '/';
956 // If URI doesn't start with http:// or https://, we will add
957 // this.
958 if (substr($pma_absolute_uri, 0, 7) != 'http://'
959 && substr($pma_absolute_uri, 0, 8) != 'https://'
961 $pma_absolute_uri =
962 ($is_https ? 'https' : 'http')
963 . ':' . (substr($pma_absolute_uri, 0, 2) == '//' ? '' : '//')
964 . $pma_absolute_uri;
967 $this->set('PmaAbsoluteUri', $pma_absolute_uri);
971 * check selected collation_connection
973 * @todo check validity of $_REQUEST['collation_connection']
975 * @return nothing
977 function checkCollationConnection()
979 if (! empty($_REQUEST['collation_connection'])) {
980 $this->set(
981 'collation_connection',
982 strip_tags($_REQUEST['collation_connection'])
988 * checks for font size configuration, and sets font size as requested by user
990 * @return nothing
992 function checkFontsize()
994 $new_fontsize = '';
996 if (isset($_GET['set_fontsize'])) {
997 $new_fontsize = $_GET['set_fontsize'];
998 } elseif (isset($_POST['set_fontsize'])) {
999 $new_fontsize = $_POST['set_fontsize'];
1000 } elseif (isset($_COOKIE['pma_fontsize'])) {
1001 $new_fontsize = $_COOKIE['pma_fontsize'];
1004 if (preg_match('/^[0-9.]+(px|em|pt|\%)$/', $new_fontsize)) {
1005 $this->set('fontsize', $new_fontsize);
1006 } elseif (! $this->get('fontsize')) {
1007 // 80% would correspond to the default browser font size
1008 // of 16, but use 82% to help read the monoface font
1009 $this->set('fontsize', '82%');
1012 $this->setCookie('pma_fontsize', $this->get('fontsize'), '82%');
1016 * checks if upload is enabled
1018 * @return nothing
1020 function checkUpload()
1022 if (ini_get('file_uploads')) {
1023 $this->set('enable_upload', true);
1024 // if set "php_admin_value file_uploads Off" in httpd.conf
1025 // ini_get() also returns the string "Off" in this case:
1026 if ('off' == strtolower(ini_get('file_uploads'))) {
1027 $this->set('enable_upload', false);
1029 } else {
1030 $this->set('enable_upload', false);
1035 * Maximum upload size as limited by PHP
1036 * Used with permission from Moodle (http://moodle.org) by Martin Dougiamas
1038 * this section generates $max_upload_size in bytes
1040 * @return nothing
1042 function checkUploadSize()
1044 if (! $filesize = ini_get('upload_max_filesize')) {
1045 $filesize = "5M";
1048 if ($postsize = ini_get('post_max_size')) {
1049 $this->set(
1050 'max_upload_size',
1051 min(PMA_get_real_size($filesize), PMA_get_real_size($postsize))
1053 } else {
1054 $this->set('max_upload_size', PMA_get_real_size($filesize));
1059 * check for https
1061 * @return nothing
1063 function checkIsHttps()
1065 $this->set('is_https', $this->isHttps());
1069 * Checks if protocol is https
1071 * @return bool
1073 public function isHttps()
1075 static $is_https = null;
1077 if (null !== $is_https) {
1078 return $is_https;
1081 $url = parse_url($this->get('PmaAbsoluteUri'));
1083 if (isset($url['scheme']) && $url['scheme'] == 'https') {
1084 $is_https = true;
1085 } else {
1086 $is_https = false;
1089 return $is_https;
1093 * Detects whether https appears to be used.
1095 * Please note that this just detects what we see, so
1096 * it completely ignores things like reverse proxies.
1098 * @return bool
1100 function detectHttps()
1102 $is_https = false;
1104 $url = array();
1106 // At first we try to parse REQUEST_URI, it might contain full URL,
1107 if (PMA_getenv('REQUEST_URI')) {
1108 // produces E_WARNING if it cannot get parsed, e.g. '/foobar:/'
1109 $url = @parse_url(PMA_getenv('REQUEST_URI'));
1110 if ($url === false) {
1111 $url = array();
1115 // If we don't have scheme, we didn't have full URL so we need to
1116 // dig deeper
1117 if (empty($url['scheme'])) {
1118 // Scheme
1119 if (PMA_getenv('HTTP_SCHEME')) {
1120 $url['scheme'] = PMA_getenv('HTTP_SCHEME');
1121 } else {
1122 $url['scheme'] =
1123 PMA_getenv('HTTPS') && strtolower(PMA_getenv('HTTPS')) != 'off'
1124 ? 'https'
1125 : 'http';
1129 if (isset($url['scheme']) && $url['scheme'] == 'https') {
1130 $is_https = true;
1131 } else {
1132 $is_https = false;
1135 return $is_https;
1139 * detect correct cookie path
1141 * @return nothing
1143 function checkCookiePath()
1145 $this->set('cookie_path', $this->getCookiePath());
1149 * Get cookie path
1151 * @return string
1153 public function getCookiePath()
1155 static $cookie_path = null;
1157 if (null !== $cookie_path) {
1158 return $cookie_path;
1161 $parsed_url = parse_url($this->get('PmaAbsoluteUri'));
1163 $cookie_path = $parsed_url['path'];
1165 return $cookie_path;
1169 * enables backward compatibility
1171 * @return nothing
1173 function enableBc()
1175 $GLOBALS['cfg'] = $this->settings;
1176 $GLOBALS['default_server'] = $this->default_server;
1177 unset($this->default_server);
1178 $GLOBALS['collation_connection'] = $this->get('collation_connection');
1179 $GLOBALS['is_upload'] = $this->get('enable_upload');
1180 $GLOBALS['max_upload_size'] = $this->get('max_upload_size');
1181 $GLOBALS['cookie_path'] = $this->get('cookie_path');
1182 $GLOBALS['is_https'] = $this->get('is_https');
1184 $defines = array(
1185 'PMA_VERSION',
1186 'PMA_THEME_VERSION',
1187 'PMA_THEME_GENERATION',
1188 'PMA_PHP_STR_VERSION',
1189 'PMA_PHP_INT_VERSION',
1190 'PMA_IS_WINDOWS',
1191 'PMA_IS_IIS',
1192 'PMA_IS_GD2',
1193 'PMA_USR_OS',
1194 'PMA_USR_BROWSER_VER',
1195 'PMA_USR_BROWSER_AGENT'
1198 foreach ($defines as $define) {
1199 if (! defined($define)) {
1200 define($define, $this->get($define));
1206 * @todo finish
1208 * @return nothing
1210 function save()
1215 * returns options for font size selection
1217 * @static
1218 * @param string $current_size current selected font size with unit
1220 * @return array selectable font sizes
1222 static protected function _getFontsizeOptions($current_size = '82%')
1224 $unit = preg_replace('/[0-9.]*/', '', $current_size);
1225 $value = preg_replace('/[^0-9.]*/', '', $current_size);
1227 $factors = array();
1228 $options = array();
1229 $options["$value"] = $value . $unit;
1231 if ($unit === '%') {
1232 $factors[] = 1;
1233 $factors[] = 5;
1234 $factors[] = 10;
1235 } elseif ($unit === 'em') {
1236 $factors[] = 0.05;
1237 $factors[] = 0.2;
1238 $factors[] = 1;
1239 } elseif ($unit === 'pt') {
1240 $factors[] = 0.5;
1241 $factors[] = 2;
1242 } elseif ($unit === 'px') {
1243 $factors[] = 1;
1244 $factors[] = 5;
1245 $factors[] = 10;
1246 } else {
1247 //unknown font size unit
1248 $factors[] = 0.05;
1249 $factors[] = 0.2;
1250 $factors[] = 1;
1251 $factors[] = 5;
1252 $factors[] = 10;
1255 foreach ($factors as $key => $factor) {
1256 $option_inc = $value + $factor;
1257 $option_dec = $value - $factor;
1258 while (count($options) < 21) {
1259 $options["$option_inc"] = $option_inc . $unit;
1260 if ($option_dec > $factors[0]) {
1261 $options["$option_dec"] = $option_dec . $unit;
1263 $option_inc += $factor;
1264 $option_dec -= $factor;
1265 if (isset($factors[$key + 1])
1266 && $option_inc >= $value + $factors[$key + 1]
1268 break;
1272 ksort($options);
1273 return $options;
1277 * returns html selectbox for font sizes
1279 * @static
1280 * @param string $current_size currently slected font size with unit
1282 * @return string html selectbox
1284 static protected function _getFontsizeSelection()
1286 $current_size = $GLOBALS['PMA_Config']->get('fontsize');
1287 // for the case when there is no config file (this is supported)
1288 if (empty($current_size)) {
1289 if (isset($_COOKIE['pma_fontsize'])) {
1290 $current_size = $_COOKIE['pma_fontsize'];
1291 } else {
1292 $current_size = '82%';
1295 $options = PMA_Config::_getFontsizeOptions($current_size);
1297 $return = '<label for="select_fontsize">' . __('Font size') . ':</label>' . "\n";
1298 $return .= '<select name="set_fontsize" id="select_fontsize" class="autosubmit">' . "\n";
1299 foreach ($options as $option) {
1300 $return .= '<option value="' . $option . '"';
1301 if ($option == $current_size) {
1302 $return .= ' selected="selected"';
1304 $return .= '>' . $option . '</option>' . "\n";
1306 $return .= '</select>';
1308 return $return;
1312 * return complete font size selection form
1314 * @static
1315 * @param string $current_size currently slected font size with unit
1317 * @return string html selectbox
1319 static public function getFontsizeForm()
1321 return '<form name="form_fontsize_selection" id="form_fontsize_selection"'
1322 . ' method="post" action="index.php" target="_parent">' . "\n"
1323 . PMA_generate_common_hidden_inputs() . "\n"
1324 . PMA_Config::_getFontsizeSelection() . "\n"
1325 . '<noscript>' . "\n"
1326 . '<input type="submit" value="' . __('Go') . '" />' . "\n"
1327 . '</noscript>' . "\n"
1328 . '</form>';
1332 * removes cookie
1334 * @param string $cookie name of cookie to remove
1336 * @return boolean result of setcookie()
1338 function removeCookie($cookie)
1340 return setcookie(
1341 $cookie,
1343 time() - 3600,
1344 $this->getCookiePath(),
1346 $this->isHttps()
1351 * sets cookie if value is different from current cokkie value,
1352 * or removes if value is equal to default
1354 * @param string $cookie name of cookie to remove
1355 * @param mixed $value new cookie value
1356 * @param string $default default value
1357 * @param int $validity validity of cookie in seconds (default is one month)
1358 * @param bool $httponly whether cookie is only for HTTP (and not for scripts)
1360 * @return boolean result of setcookie()
1362 function setCookie($cookie, $value, $default = null, $validity = null, $httponly = true)
1364 if ($validity == null) {
1365 $validity = 2592000;
1367 if (strlen($value) && null !== $default && $value === $default) {
1368 // default value is used
1369 if (isset($_COOKIE[$cookie])) {
1370 // remove cookie
1371 return $this->removeCookie($cookie);
1373 return false;
1376 if (! strlen($value) && isset($_COOKIE[$cookie])) {
1377 // remove cookie, value is empty
1378 return $this->removeCookie($cookie);
1381 if (! isset($_COOKIE[$cookie]) || $_COOKIE[$cookie] !== $value) {
1382 // set cookie with new value
1383 /* Calculate cookie validity */
1384 if ($validity == 0) {
1385 $v = 0;
1386 } else {
1387 $v = time() + $validity;
1389 return setcookie(
1390 $cookie,
1391 $value,
1393 $this->getCookiePath(),
1395 $this->isHttps(),
1396 $httponly
1400 // cookie has already $value as value
1401 return true;