2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Configuration handling.
9 if (! defined('PHPMYADMIN')) {
14 * Load vendor configuration.
16 require_once './libraries/vendor_config.php';
19 * Indication for error handler (see end of this file).
21 $GLOBALS['pma_config_loading'] = false;
31 * @var string default config source
33 var $default_source = './libraries/config.default.php';
36 * @var array default configuration settings
38 var $default = array();
41 * @var array configuration settings, without user preferences applied
43 var $base_settings = array();
46 * @var array configuration settings
48 var $settings = array();
51 * @var string config source
56 * @var int source modification time
58 var $source_mtime = 0;
59 var $default_source_mtime = 0;
65 var $error_config_file = false;
70 var $error_config_default_file = false;
75 var $error_pma_uri = false;
80 var $default_server = array();
83 * @var boolean whether init is done or not
84 * set this to false to force some initial checks
85 * like checking for required functions
92 * @param string $source source to read config from
94 function __construct($source = null)
96 $this->settings
= array();
98 // functions need to refresh in case of config file changed goes in
100 $this->load($source);
102 // other settings, independent from config file, comes in
103 $this->checkSystem();
107 $this->base_settings
= $this->settings
;
111 * sets system and application settings
115 function checkSystem()
117 $this->set('PMA_VERSION', '4.2.5');
121 $this->set('PMA_THEME_VERSION', 2);
125 $this->set('PMA_THEME_GENERATION', 2);
127 $this->checkPhpVersion();
128 $this->checkWebServerOs();
129 $this->checkWebServer();
131 $this->checkClient();
132 $this->checkUpload();
133 $this->checkUploadSize();
134 $this->checkOutputCompression();
138 * whether to use gzip output compression or not
142 function checkOutputCompression()
144 // If zlib output compression is set in the php configuration file, no
145 // output buffering should be run
146 if (@ini_get
('zlib.output_compression')) {
147 $this->set('OBGzip', false);
150 // disable output-buffering (if set to 'auto') for IE6, else enable it.
151 if (strtolower($this->get('OBGzip')) == 'auto') {
152 if ($this->get('PMA_USR_BROWSER_AGENT') == 'IE'
153 && $this->get('PMA_USR_BROWSER_VER') >= 6
154 && $this->get('PMA_USR_BROWSER_VER') < 7
156 $this->set('OBGzip', false);
158 $this->set('OBGzip', true);
164 * Determines platform (OS), browser and version of the user
165 * Based on a phpBuilder article:
167 * @see http://www.phpbuilder.net/columns/tim20000821.php
171 function checkClient()
173 if (PMA_getenv('HTTP_USER_AGENT')) {
174 $HTTP_USER_AGENT = PMA_getenv('HTTP_USER_AGENT');
176 $HTTP_USER_AGENT = '';
180 if (strstr($HTTP_USER_AGENT, 'Win')) {
181 $this->set('PMA_USR_OS', 'Win');
182 } elseif (strstr($HTTP_USER_AGENT, 'Mac')) {
183 $this->set('PMA_USR_OS', 'Mac');
184 } elseif (strstr($HTTP_USER_AGENT, 'Linux')) {
185 $this->set('PMA_USR_OS', 'Linux');
186 } elseif (strstr($HTTP_USER_AGENT, 'Unix')) {
187 $this->set('PMA_USR_OS', 'Unix');
188 } elseif (strstr($HTTP_USER_AGENT, 'OS/2')) {
189 $this->set('PMA_USR_OS', 'OS/2');
191 $this->set('PMA_USR_OS', 'Other');
194 // 2. browser and version
195 // (must check everything else before Mozilla)
197 $is_mozilla = preg_match(
198 '@Mozilla/([0-9].[0-9]{1,2})@',
204 '@Opera(/| )([0-9].[0-9]{1,2})@',
208 $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
209 $this->set('PMA_USR_BROWSER_AGENT', 'OPERA');
210 } elseif (preg_match(
211 '@(MS)?IE ([0-9]{1,2}.[0-9]{1,2})@',
215 $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
216 $this->set('PMA_USR_BROWSER_AGENT', 'IE');
217 } elseif (preg_match(
222 $this->set('PMA_USR_BROWSER_VER', intval($log_version[1]) +
4);
223 $this->set('PMA_USR_BROWSER_AGENT', 'IE');
224 } elseif (preg_match(
225 '@OmniWeb/([0-9].[0-9]{1,2})@',
229 $this->set('PMA_USR_BROWSER_VER', $log_version[1]);
230 $this->set('PMA_USR_BROWSER_AGENT', 'OMNIWEB');
231 // Konqueror 2.2.2 says Konqueror/2.2.2
232 // Konqueror 3.0.3 says Konqueror/3
233 } elseif (preg_match(
234 '@(Konqueror/)(.*)(;)@',
238 $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
239 $this->set('PMA_USR_BROWSER_AGENT', 'KONQUEROR');
240 // must check Chrome before Safari
241 } elseif ($is_mozilla
242 && preg_match('@Chrome/([0-9.]*)@', $HTTP_USER_AGENT, $log_version)
244 $this->set('PMA_USR_BROWSER_VER', $log_version[1]);
245 $this->set('PMA_USR_BROWSER_AGENT', 'CHROME');
247 } elseif ($is_mozilla
248 && preg_match('@Version/(.*) Safari@', $HTTP_USER_AGENT, $log_version)
251 'PMA_USR_BROWSER_VER', $log_version[1]
253 $this->set('PMA_USR_BROWSER_AGENT', 'SAFARI');
255 } elseif ($is_mozilla
256 && preg_match('@Safari/([0-9]*)@', $HTTP_USER_AGENT, $log_version)
259 'PMA_USR_BROWSER_VER', $mozilla_version[1] . '.' . $log_version[1]
261 $this->set('PMA_USR_BROWSER_AGENT', 'SAFARI');
263 } elseif (! strstr($HTTP_USER_AGENT, 'compatible')
264 && preg_match('@Firefox/([\w.]+)@', $HTTP_USER_AGENT, $log_version)
267 'PMA_USR_BROWSER_VER', $log_version[1]
269 $this->set('PMA_USR_BROWSER_AGENT', 'FIREFOX');
270 } elseif (preg_match('@rv:1.9(.*)Gecko@', $HTTP_USER_AGENT)) {
271 $this->set('PMA_USR_BROWSER_VER', '1.9');
272 $this->set('PMA_USR_BROWSER_AGENT', 'GECKO');
273 } elseif ($is_mozilla) {
274 $this->set('PMA_USR_BROWSER_VER', $mozilla_version[1]);
275 $this->set('PMA_USR_BROWSER_AGENT', 'MOZILLA');
277 $this->set('PMA_USR_BROWSER_VER', 0);
278 $this->set('PMA_USR_BROWSER_AGENT', 'OTHER');
283 * Whether GD2 is present
289 if ($this->get('GD2Available') == 'yes') {
290 $this->set('PMA_IS_GD2', 1);
291 } elseif ($this->get('GD2Available') == 'no') {
292 $this->set('PMA_IS_GD2', 0);
294 if (!@function_exists
('imagecreatetruecolor')) {
295 $this->set('PMA_IS_GD2', 0);
297 if (@function_exists
('gd_info')) {
299 if (strstr($gd_nfo["GD Version"], '2.')) {
300 $this->set('PMA_IS_GD2', 1);
302 $this->set('PMA_IS_GD2', 0);
305 $this->set('PMA_IS_GD2', 0);
312 * Whether the Web server php is running on is IIS
316 function checkWebServer()
318 // some versions return Microsoft-IIS, some Microsoft/IIS
319 // we could use a preg_match() but it's slower
320 if (PMA_getenv('SERVER_SOFTWARE')
321 && stristr(PMA_getenv('SERVER_SOFTWARE'), 'Microsoft')
322 && stristr(PMA_getenv('SERVER_SOFTWARE'), 'IIS')
324 $this->set('PMA_IS_IIS', 1);
326 $this->set('PMA_IS_IIS', 0);
331 * Whether the os php is running on is windows or not
335 function checkWebServerOs()
337 // Default to Unix or Equiv
338 $this->set('PMA_IS_WINDOWS', 0);
339 // If PHP_OS is defined then continue
340 if (defined('PHP_OS')) {
341 if (stristr(PHP_OS
, 'win') && !stristr(PHP_OS
, 'darwin')) {
342 // Is it some version of Windows
343 $this->set('PMA_IS_WINDOWS', 1);
344 } elseif (stristr(PHP_OS
, 'OS/2')) {
345 // Is it OS/2 (No file permissions like Windows)
346 $this->set('PMA_IS_WINDOWS', 1);
352 * detects PHP version
356 function checkPhpVersion()
360 '@([0-9]{1,2}).([0-9]{1,2}).([0-9]{1,2})@',
365 '@([0-9]{1,2}).([0-9]{1,2})@',
370 if (isset($match) && ! empty($match[1])) {
371 if (! isset($match[2])) {
374 if (! isset($match[3])) {
378 'PMA_PHP_INT_VERSION',
379 (int) sprintf('%d%02d%02d', $match[1], $match[2], $match[3])
382 $this->set('PMA_PHP_INT_VERSION', 0);
384 $this->set('PMA_PHP_STR_VERSION', phpversion());
388 * detects if Git revision
392 function isGitRevision()
395 if (isset($_SESSION['is_git_revision'])) {
396 if ($_SESSION['is_git_revision']) {
397 $this->set('PMA_VERSION_GIT', 1);
399 return $_SESSION['is_git_revision'];
401 // find out if there is a .git folder
402 $git_folder = '.git';
403 if (! @file_exists
($git_folder)
404 ||
! @file_exists
($git_folder . '/config')
406 $_SESSION['is_git_revision'] = false;
409 $_SESSION['is_git_revision'] = true;
414 * detects Git revision, if running inside repo
418 function checkGitRevision()
420 // find out if there is a .git folder
421 $git_folder = '.git';
422 if (! $this->isGitRevision()) {
426 if (! $ref_head = @file_get_contents
($git_folder . '/HEAD')) {
430 // are we on any branch?
431 if (strstr($ref_head, '/')) {
432 $ref_head = substr(trim($ref_head), 5);
433 if (substr($ref_head, 0, 11) === 'refs/heads/') {
434 $branch = substr($ref_head, 11);
436 $branch = basename($ref_head);
439 $ref_file = $git_folder . '/' . $ref_head;
440 if (@file_exists
($ref_file)) {
441 $hash = @file_get_contents
($ref_file);
447 // deal with packed refs
448 $packed_refs = @file_get_contents
($git_folder . '/packed-refs');
449 if (! $packed_refs) {
452 // split file to lines
453 $ref_lines = explode("\n", $packed_refs);
454 foreach ($ref_lines as $line) {
456 if ($line[0] == '#') {
460 $parts = explode(' ', $line);
461 // care only about named refs
462 if (count($parts) != 2) {
465 // have found our ref?
466 if ($parts[1] == $ref_head) {
471 if (! isset($hash)) {
472 // Could not find ref
477 $hash = trim($ref_head);
481 if (! isset($_SESSION['PMA_VERSION_COMMITDATA_' . $hash])) {
482 $git_file_name = $git_folder . '/objects/' . substr($hash, 0, 2)
483 . '/' . substr($hash, 2);
484 if (file_exists($git_file_name) ) {
485 if (! $commit = @file_get_contents
($git_file_name)) {
488 $commit = explode("\0", gzuncompress($commit), 2);
489 $commit = explode("\n", $commit[1]);
490 $_SESSION['PMA_VERSION_COMMITDATA_' . $hash] = $commit;
492 $pack_names = array();
493 // work with packed data
494 $packs_file = $git_folder . '/objects/info/packs';
495 if (file_exists($packs_file)
496 && $packs = @file_get_contents
($packs_file)
498 // File exists. Read it, parse the file to get the names of the
499 // packs. (to look for them in .git/object/pack directory later)
500 foreach (explode("\n", $packs) as $line) {
502 if (strlen(trim($line)) == 0) {
505 // skip non pack lines
506 if ($line[0] != 'P') {
510 $pack_names[] = substr($line, 2);
513 // '.git/objects/info/packs' file can be missing
514 // (atlease in mysGit)
515 // File missing. May be we can look in the .git/object/pack
516 // directory for all the .pack files and use that list of
518 $dirIterator = new DirectoryIterator(
519 $git_folder . '/objects/pack'
521 foreach ($dirIterator as $file_info) {
522 $file_name = $file_info->getFilename();
523 // if this is a .pack file
524 if ($file_info->isFile()
525 && substr($file_name, -5) == '.pack'
527 $pack_names[] = $file_name;
531 $hash = strtolower($hash);
532 foreach ($pack_names as $pack_name) {
533 $index_name = str_replace('.pack', '.idx', $pack_name);
536 $index_data = @file_get_contents
(
537 $git_folder . '/objects/pack/' . $index_name
543 if (substr($index_data, 0, 4) != "\377tOc") {
547 $version = unpack('N', substr($index_data, 4, 4));
548 if ($version[1] != 2) {
551 // parse fanout table
552 $fanout = unpack("N*", substr($index_data, 8, 256 * 4));
554 // find where we should search
555 $firstbyte = intval(substr($hash, 0, 2), 16);
556 // array is indexed from 1 and we need to get
557 // previous entry for start
558 if ($firstbyte == 0) {
561 $start = $fanout[$firstbyte];
563 $end = $fanout[$firstbyte +
1];
565 // stupid linear search for our sha
567 $offset = 8 +
(256 * 4);
568 for ($position = $start; $position < $end; $position++
) {
572 $index_data, $offset +
($position * 20), 20
585 $offset = 8 +
(256 * 4) +
(24 * $fanout[256]);
586 $pack_offset = unpack(
587 'N', substr($index_data, $offset +
($position * 4), 4)
589 $pack_offset = $pack_offset[1];
593 $git_folder . '/objects/pack/' . $pack_name, 'rb'
595 if ($pack_file === false) {
599 fseek($pack_file, $pack_offset);
602 $header = ord(fread($pack_file, 1));
603 $type = ($header >> 4) & 7;
604 $hasnext = ($header & 128) >> 7;
605 $size = $header & 0xf;
609 $byte = ord(fread($pack_file, 1));
610 $size |
= ($byte & 0x7f) << $offset;
611 $hasnext = ($byte & 128) >> 7;
615 // we care only about commit objects
621 $commit = fread($pack_file, $size);
622 $commit = gzuncompress($commit);
623 $commit = explode("\n", $commit);
624 $_SESSION['PMA_VERSION_COMMITDATA_' . $hash] = $commit;
629 $commit = $_SESSION['PMA_VERSION_COMMITDATA_' . $hash];
632 // check if commit exists in Github
633 if ($commit !== false
634 && isset($_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash])
636 $is_remote_commit = $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash];
638 $link = 'https://api.github.com/repos/phpmyadmin/phpmyadmin/git/commits/'
640 $is_found = $this->checkHTTP($link, ! $commit);
643 $is_remote_commit = false;
644 $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = false;
647 // no remote link for now, but don't cache this as Github is down
648 $is_remote_commit = false;
651 $is_remote_commit = true;
652 $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = true;
653 if ($commit === false) {
654 // if no local commit data, try loading from Github
655 $commit_json = json_decode($is_found);
661 $is_remote_branch = false;
662 if ($is_remote_commit && $branch !== false) {
663 // check if branch exists in Github
664 if (isset($_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash])) {
665 $is_remote_branch = $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash];
667 $link = 'https://api.github.com/repos/phpmyadmin/phpmyadmin'
668 . '/git/trees/' . $branch;
669 $is_found = $this->checkHTTP($link);
672 $is_remote_branch = true;
673 $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = true;
676 $is_remote_branch = false;
677 $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = false;
680 // no remote link for now, but don't cache this as Github is down
681 $is_remote_branch = false;
687 if ($commit !== false) {
688 $author = array('name' => '', 'email' => '', 'date' => '');
689 $committer = array('name' => '', 'email' => '', 'date' => '');
692 $dataline = array_shift($commit);
693 $datalinearr = explode(' ', $dataline, 2);
694 $linetype = $datalinearr[0];
695 if (in_array($linetype, array('author', 'committer'))) {
696 $user = $datalinearr[1];
697 preg_match('/([^<]+)<([^>]+)> ([0-9]+)( [^ ]+)?/', $user, $user);
699 'name' => trim($user[1]),
700 'email' => trim($user[2]),
701 'date' => date('Y-m-d H:i:s', $user[3]));
702 if (isset($user[4])) {
703 $user2['date'] .= $user[4];
707 } while ($dataline != '');
708 $message = trim(implode(' ', $commit));
710 } elseif (isset($commit_json)) {
712 'name' => $commit_json->author
->name
,
713 'email' => $commit_json->author
->email
,
714 'date' => $commit_json->author
->date
);
716 'name' => $commit_json->committer
->name
,
717 'email' => $commit_json->committer
->email
,
718 'date' => $commit_json->committer
->date
);
719 $message = trim($commit_json->message
);
724 $this->set('PMA_VERSION_GIT', 1);
725 $this->set('PMA_VERSION_GIT_COMMITHASH', $hash);
726 $this->set('PMA_VERSION_GIT_BRANCH', $branch);
727 $this->set('PMA_VERSION_GIT_MESSAGE', $message);
728 $this->set('PMA_VERSION_GIT_AUTHOR', $author);
729 $this->set('PMA_VERSION_GIT_COMMITTER', $committer);
730 $this->set('PMA_VERSION_GIT_ISREMOTECOMMIT', $is_remote_commit);
731 $this->set('PMA_VERSION_GIT_ISREMOTEBRANCH', $is_remote_branch);
735 * Checks if given URL is 200 or 404, optionally returns data
737 * @param mixed $link curl link
738 * @param boolean $get_body whether to retrieve body of document
740 * @return string|boolean test result or data
742 function checkHTTP($link, $get_body = false)
744 if (! function_exists('curl_init')) {
747 $ch = curl_init($link);
748 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
, 0);
749 curl_setopt($ch, CURLOPT_HEADER
, 1);
750 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
751 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST
, 0);
752 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER
, 0);
753 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT
, 5);
754 curl_setopt($ch, CURLOPT_USERAGENT
, 'phpMyAdmin/' . PMA_VERSION
);
755 curl_setopt($ch, CURLOPT_TIMEOUT
, 5);
756 if (! defined('TESTSUITE')) {
757 session_write_close();
759 $data = @curl_exec
($ch);
760 if (! defined('TESTSUITE')) {
761 ini_set('session.use_only_cookies', '0');
762 ini_set('session.use_cookies', '0');
763 ini_set('session.use_trans_sid', '0');
764 ini_set('session.cache_limiter', '');
767 if ($data === false) {
770 $ok = 'HTTP/1.1 200 OK';
771 $notfound = 'HTTP/1.1 404 Not Found';
772 if (substr($data, 0, strlen($ok)) === $ok) {
773 return $get_body ?
substr($data, strpos($data, "\r\n\r\n") +
4) : true;
774 } elseif (substr($data, 0, strlen($notfound)) === $notfound) {
781 * loads default values from default source
783 * @return boolean success
785 function loadDefaults()
788 if (! file_exists($this->default_source
)) {
789 $this->error_config_default_file
= true;
792 include $this->default_source
;
794 $this->default_source_mtime
= filemtime($this->default_source
);
796 $this->default_server
= $cfg['Servers'][1];
797 unset($cfg['Servers']);
799 $this->default = $cfg;
800 $this->settings
= PMA_arrayMergeRecursive($this->settings
, $cfg);
802 $this->error_config_default_file
= false;
808 * loads configuration from $source, usually the config file
809 * should be called on object creation
811 * @param string $source config file
815 function load($source = null)
817 $this->loadDefaults();
819 if (null !== $source) {
820 $this->setSource($source);
823 if (! $this->checkConfigSource()) {
830 * Parses the configuration file, we throw away any errors or
833 $old_error_reporting = error_reporting(0);
835 $GLOBALS['pma_config_loading'] = true;
836 $eval_result = include $this->getSource();
837 $GLOBALS['pma_config_loading'] = false;
839 error_reporting($old_error_reporting);
841 if ($eval_result === false) {
842 $this->error_config_file
= true;
844 $this->error_config_file
= false;
845 $this->source_mtime
= filemtime($this->getSource());
849 * Backward compatibility code
851 if (!empty($cfg['DefaultTabTable'])) {
852 $cfg['DefaultTabTable'] = str_replace(
856 'tbl_properties.php',
858 $cfg['DefaultTabTable']
862 if (!empty($cfg['DefaultTabDatabase'])) {
863 $cfg['DefaultTabDatabase'] = str_replace(
869 $cfg['DefaultTabDatabase']
874 $this->settings
= PMA_arrayMergeRecursive($this->settings
, $cfg);
875 $this->checkPmaAbsoluteUri();
876 $this->checkFontsize();
878 // Handling of the collation must be done after merging of $cfg
879 // (from config.inc.php) so that $cfg['DefaultConnectionCollation']
880 // can have an effect. Note that the presence of collation
881 // information in a cookie has priority over what is defined
882 // in the default or user's config files.
884 * @todo check validity of $_COOKIE['pma_collation_connection']
886 if (! empty($_COOKIE['pma_collation_connection'])) {
888 'collation_connection',
889 strip_tags($_COOKIE['pma_collation_connection'])
893 'collation_connection',
894 $this->get('DefaultConnectionCollation')
897 // Now, a collation information could come from REQUEST
898 // (an example of this: the collation selector in index.php)
899 // so the following handles the setting of collation_connection
900 // and later, in common.inc.php, the cookie will be set
901 // according to this.
902 $this->checkCollationConnection();
908 * Loads user preferences and merges them with current config
909 * must be called after control connection has been estabilished
913 function loadUserPreferences()
915 // index.php should load these settings, so that phpmyadmin.css.php
916 // will have everything avaiable in session cache
917 $server = isset($GLOBALS['server'])
919 : (!empty($GLOBALS['cfg']['ServerDefault'])
920 ?
$GLOBALS['cfg']['ServerDefault']
922 $cache_key = 'server_' . $server;
923 if ($server > 0 && !defined('PMA_MINIMUM_COMMON')) {
924 $config_mtime = max($this->default_source_mtime
, $this->source_mtime
);
925 // cache user preferences, use database only when needed
926 if (! isset($_SESSION['cache'][$cache_key]['userprefs'])
927 ||
$_SESSION['cache'][$cache_key]['config_mtime'] < $config_mtime
929 // load required libraries
930 include_once './libraries/user_preferences.lib.php';
931 $prefs = PMA_loadUserprefs();
932 $_SESSION['cache'][$cache_key]['userprefs']
933 = PMA_applyUserprefs($prefs['config_data']);
934 $_SESSION['cache'][$cache_key]['userprefs_mtime'] = $prefs['mtime'];
935 $_SESSION['cache'][$cache_key]['userprefs_type'] = $prefs['type'];
936 $_SESSION['cache'][$cache_key]['config_mtime'] = $config_mtime;
938 } elseif ($server == 0
939 ||
! isset($_SESSION['cache'][$cache_key]['userprefs'])
941 $this->set('user_preferences', false);
944 $config_data = $_SESSION['cache'][$cache_key]['userprefs'];
945 // type is 'db' or 'session'
948 $_SESSION['cache'][$cache_key]['userprefs_type']
951 'user_preferences_mtime',
952 $_SESSION['cache'][$cache_key]['userprefs_mtime']
955 // backup some settings
957 if (isset($this->settings
['fontsize'])) {
958 $org_fontsize = $this->settings
['fontsize'];
961 $this->settings
= PMA_arrayMergeRecursive($this->settings
, $config_data);
962 $GLOBALS['cfg'] = PMA_arrayMergeRecursive($GLOBALS['cfg'], $config_data);
963 if (defined('PMA_MINIMUM_COMMON')) {
967 // settings below start really working on next page load, but
968 // changes are made only in index.php so everything is set when
972 $tmanager = $_SESSION['PMA_Theme_Manager'];
973 if ($tmanager->getThemeCookie() ||
isset($_REQUEST['set_theme'])) {
974 if ((! isset($config_data['ThemeDefault'])
975 && $tmanager->theme
->getId() != 'original')
976 ||
isset($config_data['ThemeDefault'])
977 && $config_data['ThemeDefault'] != $tmanager->theme
->getId()
979 // new theme was set in common.inc.php
983 $tmanager->theme
->getId(),
988 // no cookie - read default from settings
989 if ($this->settings
['ThemeDefault'] != $tmanager->theme
->getId()
990 && $tmanager->checkTheme($this->settings
['ThemeDefault'])
992 $tmanager->setActiveTheme($this->settings
['ThemeDefault']);
993 $tmanager->setThemeCookie();
998 if ((! isset($config_data['fontsize'])
999 && $org_fontsize != '82%')
1000 ||
isset($config_data['fontsize'])
1001 && $org_fontsize != $config_data['fontsize']
1003 $this->setUserValue(null, 'fontsize', $org_fontsize, '82%');
1007 if (isset($_COOKIE['pma_lang']) ||
isset($_POST['lang'])) {
1008 if ((! isset($config_data['lang'])
1009 && $GLOBALS['lang'] != 'en')
1010 ||
isset($config_data['lang'])
1011 && $GLOBALS['lang'] != $config_data['lang']
1013 $this->setUserValue(null, 'lang', $GLOBALS['lang'], 'en');
1016 // read language from settings
1017 if (isset($config_data['lang']) && PMA_langSet($config_data['lang'])) {
1018 $this->setCookie('pma_lang', $GLOBALS['lang']);
1022 // save connection collation
1024 // just to shorten the lines
1025 $collation = 'collation_connection';
1026 if (isset($_COOKIE['pma_collation_connection'])
1027 ||
isset($_POST[$collation])
1029 if ((! isset($config_data[$collation])
1030 && $GLOBALS[$collation] != 'utf8_general_ci')
1031 ||
isset($config_data[$collation])
1032 && $GLOBALS[$collation] != $config_data[$collation]
1034 $this->setUserValue(
1037 $GLOBALS[$collation],
1042 // read collation from settings
1043 if (isset($config_data['collation_connection'])) {
1044 $GLOBALS['collation_connection']
1045 = $config_data['collation_connection'];
1047 'pma_collation_connection',
1048 $GLOBALS['collation_connection']
1056 * Sets config value which is stored in user preferences (if available)
1059 * If user preferences are not yet initialized, option is applied to
1060 * global config and added to a update queue, which is processed
1061 * by {@link loadUserPreferences()}
1063 * @param string $cookie_name can be null
1064 * @param string $cfg_path configuration path
1065 * @param mixed $new_cfg_value new value
1066 * @param mixed $default_value default value
1070 function setUserValue($cookie_name, $cfg_path, $new_cfg_value,
1071 $default_value = null
1073 // use permanent user preferences if possible
1074 $prefs_type = $this->get('user_preferences');
1076 include_once './libraries/user_preferences.lib.php';
1077 if ($default_value === null) {
1078 $default_value = PMA_arrayRead($cfg_path, $this->default);
1080 PMA_persistOption($cfg_path, $new_cfg_value, $default_value);
1082 if ($prefs_type != 'db' && $cookie_name) {
1083 // fall back to cookies
1084 if ($default_value === null) {
1085 $default_value = PMA_arrayRead($cfg_path, $this->settings
);
1087 $this->setCookie($cookie_name, $new_cfg_value, $default_value);
1089 PMA_arrayWrite($cfg_path, $GLOBALS['cfg'], $new_cfg_value);
1090 PMA_arrayWrite($cfg_path, $this->settings
, $new_cfg_value);
1094 * Reads value stored by {@link setUserValue()}
1096 * @param string $cookie_name cookie name
1097 * @param mixed $cfg_value config value
1101 function getUserValue($cookie_name, $cfg_value)
1103 $cookie_exists = isset($_COOKIE) && !empty($_COOKIE[$cookie_name]);
1104 $prefs_type = $this->get('user_preferences');
1105 if ($prefs_type == 'db') {
1106 // permanent user preferences value exists, remove cookie
1107 if ($cookie_exists) {
1108 $this->removeCookie($cookie_name);
1110 } else if ($cookie_exists) {
1111 return $_COOKIE[$cookie_name];
1113 // return value from $cfg array
1120 * @param string $source source
1124 function setSource($source)
1126 $this->source
= trim($source);
1130 * check config source
1132 * @return boolean whether source is valid or not
1134 function checkConfigSource()
1136 if (! $this->getSource()) {
1137 // no configuration file set at all
1141 if (! file_exists($this->getSource())) {
1142 $this->source_mtime
= 0;
1146 if (! is_readable($this->getSource())) {
1147 // manually check if file is readable
1148 // might be bug #3059806 Supporting running from CIFS/Samba shares
1151 $handle = @fopen
($this->getSource(), 'r');
1152 if ($handle !== false) {
1153 $contents = @fread
($handle, 1); // reading 1 byte is enough to test
1156 if ($contents === false) {
1157 $this->source_mtime
= 0;
1160 function_exists('__')
1161 ?
__('Existing configuration file (%s) is not readable.')
1162 : 'Existing configuration file (%s) is not readable.',
1174 * verifies the permissions on config file (if asked by configuration)
1175 * (must be called after config.inc.php has been merged)
1179 function checkPermissions()
1181 // Check for permissions (on platforms that support it):
1182 if ($this->get('CheckConfigurationPermissions')) {
1183 $perms = @fileperms
($this->getSource());
1184 if (!($perms === false) && ($perms & 2)) {
1185 // This check is normally done after loading configuration
1186 $this->checkWebServerOs();
1187 if ($this->get('PMA_IS_WINDOWS') == 0) {
1188 $this->source_mtime
= 0;
1191 'Wrong permissions on configuration file, '
1192 . 'should not be world writable!'
1201 * returns specific config setting
1203 * @param string $setting config setting
1205 * @return mixed value
1207 function get($setting)
1209 if (isset($this->settings
[$setting])) {
1210 return $this->settings
[$setting];
1216 * sets configuration variable
1218 * @param string $setting configuration option
1219 * @param mixed $value new value for configuration option
1223 function set($setting, $value)
1225 if (! isset($this->settings
[$setting])
1226 ||
$this->settings
[$setting] !== $value
1228 $this->settings
[$setting] = $value;
1229 $this->set_mtime
= time();
1234 * returns source for current config
1236 * @return string config source
1238 function getSource()
1240 return $this->source
;
1244 * returns a unique value to force a CSS reload if either the config
1245 * or the theme changes
1246 * must also check the pma_fontsize cookie in case there is no
1249 * @return int Summary of unix timestamps and fontsize,
1250 * to be unique on theme parameters change
1252 function getThemeUniqueValue()
1254 if (null !== $this->get('fontsize')) {
1255 $fontsize = intval($this->get('fontsize'));
1256 } elseif (isset($_COOKIE['pma_fontsize'])) {
1257 $fontsize = intval($_COOKIE['pma_fontsize']);
1263 $this->source_mtime +
1264 $this->default_source_mtime +
1265 $this->get('user_preferences_mtime') +
1266 $_SESSION['PMA_Theme']->mtime_info +
1267 $_SESSION['PMA_Theme']->filesize_info
);
1271 * $cfg['PmaAbsoluteUri'] is a required directive else cookies won't be
1272 * set properly and, depending on browsers, inserting or updating a
1277 function checkPmaAbsoluteUri()
1279 // Setup a default value to let the people and lazy sysadmins work anyway,
1280 // they'll get an error if the autodetect code doesn't work
1281 $pma_absolute_uri = $this->get('PmaAbsoluteUri');
1282 $is_https = $this->detectHttps();
1284 if (strlen($pma_absolute_uri) < 5) {
1287 // If we don't have scheme, we didn't have full URL so we need to
1289 if (empty($url['scheme'])) {
1292 $url['scheme'] = 'https';
1294 $url['scheme'] = 'http';
1298 if (PMA_getenv('HTTP_HOST')) {
1299 // Prepend the scheme before using parse_url() since this
1300 // is not part of the RFC2616 Host request-header
1301 $parsed_url = parse_url(
1302 $url['scheme'] . '://' . PMA_getenv('HTTP_HOST')
1304 if (!empty($parsed_url['host'])) {
1307 $url['host'] = PMA_getenv('HTTP_HOST');
1309 } elseif (PMA_getenv('SERVER_NAME')) {
1310 $url['host'] = PMA_getenv('SERVER_NAME');
1312 $this->error_pma_uri
= true;
1316 // If we didn't set port yet...
1317 if (empty($url['port']) && PMA_getenv('SERVER_PORT')) {
1318 $url['port'] = PMA_getenv('SERVER_PORT');
1321 // And finally the path could be already set from REQUEST_URI
1322 if (empty($url['path'])) {
1323 // we got a case with nginx + php-fpm where PHP_SELF
1324 // was not set, so PMA_PHP_SELF was not set as well
1325 if (isset($GLOBALS['PMA_PHP_SELF'])) {
1326 $path = parse_url($GLOBALS['PMA_PHP_SELF']);
1328 $path = parse_url(PMA_getenv('REQUEST_URI'));
1330 $url['path'] = $path['path'];
1334 // Make url from parts we have
1335 $pma_absolute_uri = $url['scheme'] . '://';
1336 // Was there user information?
1337 if (!empty($url['user'])) {
1338 $pma_absolute_uri .= $url['user'];
1339 if (!empty($url['pass'])) {
1340 $pma_absolute_uri .= ':' . $url['pass'];
1342 $pma_absolute_uri .= '@';
1345 $pma_absolute_uri .= $url['host'];
1346 // Add port, if it not the default one
1347 if (! empty($url['port'])
1348 && (($url['scheme'] == 'http' && $url['port'] != 80)
1349 ||
($url['scheme'] == 'https' && $url['port'] != 443))
1351 $pma_absolute_uri .= ':' . $url['port'];
1353 // And finally path, without script name, the 'a' is there not to
1354 // strip our directory, when path is only /pmadir/ without filename.
1355 // Backslashes returned by Windows have to be changed.
1356 // Only replace backslashes by forward slashes if on Windows,
1357 // as the backslash could be valid on a non-Windows system.
1358 $this->checkWebServerOs();
1359 if ($this->get('PMA_IS_WINDOWS') == 1) {
1360 $path = str_replace("\\", "/", dirname($url['path'] . 'a'));
1362 $path = dirname($url['path'] . 'a');
1365 // To work correctly within javascript
1366 if (defined('PMA_PATH_TO_BASEDIR') && PMA_PATH_TO_BASEDIR
== '../') {
1367 if ($this->get('PMA_IS_WINDOWS') == 1) {
1368 $path = str_replace("\\", "/", dirname($path));
1370 $path = dirname($path);
1374 // PHP's dirname function would have returned a dot
1375 // when $path contains no slash
1379 // in vhost situations, there could be already an ending slash
1380 if (substr($path, -1) != '/') {
1383 $pma_absolute_uri .= $path;
1385 // This is to handle the case of a reverse proxy
1386 if ($this->get('ForceSSL')) {
1387 $this->set('PmaAbsoluteUri', $pma_absolute_uri);
1388 $pma_absolute_uri = $this->getSSLUri();
1392 // We used to display a warning if PmaAbsoluteUri wasn't set, but now
1393 // the autodetect code works well enough that we don't display the
1394 // warning at all. The user can still set PmaAbsoluteUri manually.
1397 // The URI is specified, however users do often specify this
1398 // wrongly, so we try to fix this.
1400 // Adds a trailing slash et the end of the phpMyAdmin uri if it
1402 if (substr($pma_absolute_uri, -1) != '/') {
1403 $pma_absolute_uri .= '/';
1406 // If URI doesn't start with http:// or https://, we will add
1408 if (substr($pma_absolute_uri, 0, 7) != 'http://'
1409 && substr($pma_absolute_uri, 0, 8) != 'https://'
1412 = ($is_https ?
'https' : 'http')
1413 . ':' . (substr($pma_absolute_uri, 0, 2) == '//' ?
'' : '//')
1414 . $pma_absolute_uri;
1417 $this->set('PmaAbsoluteUri', $pma_absolute_uri);
1421 * Converts currently used PmaAbsoluteUri to SSL based variant.
1423 * @return String witch adjusted URI
1425 function getSSLUri()
1428 $url = $this->get('PmaAbsoluteUri');
1429 // Parse current URL
1430 $parsed = parse_url($url);
1431 // In case parsing has failed do stupid string replacement
1432 if ($parsed === false) {
1433 // Replace http protocol
1434 return preg_replace('@^http:@', 'https:', $url);
1437 // Reconstruct URL using parsed parts
1438 return 'https://' . $parsed['host'] . ':443' . $parsed['path'];
1442 * check selected collation_connection
1444 * @todo check validity of $_REQUEST['collation_connection']
1448 function checkCollationConnection()
1450 if (! empty($_REQUEST['collation_connection'])) {
1452 'collation_connection',
1453 strip_tags($_REQUEST['collation_connection'])
1459 * checks for font size configuration, and sets font size as requested by user
1463 function checkFontsize()
1467 if (isset($_GET['set_fontsize'])) {
1468 $new_fontsize = $_GET['set_fontsize'];
1469 } elseif (isset($_POST['set_fontsize'])) {
1470 $new_fontsize = $_POST['set_fontsize'];
1471 } elseif (isset($_COOKIE['pma_fontsize'])) {
1472 $new_fontsize = $_COOKIE['pma_fontsize'];
1475 if (preg_match('/^[0-9.]+(px|em|pt|\%)$/', $new_fontsize)) {
1476 $this->set('fontsize', $new_fontsize);
1477 } elseif (! $this->get('fontsize')) {
1478 // 80% would correspond to the default browser font size
1479 // of 16, but use 82% to help read the monoface font
1480 $this->set('fontsize', '82%');
1483 $this->setCookie('pma_fontsize', $this->get('fontsize'), '82%');
1487 * checks if upload is enabled
1491 function checkUpload()
1493 if (ini_get('file_uploads')) {
1494 $this->set('enable_upload', true);
1495 // if set "php_admin_value file_uploads Off" in httpd.conf
1496 // ini_get() also returns the string "Off" in this case:
1497 if ('off' == strtolower(ini_get('file_uploads'))) {
1498 $this->set('enable_upload', false);
1501 $this->set('enable_upload', false);
1506 * Maximum upload size as limited by PHP
1507 * Used with permission from Moodle (http://moodle.org) by Martin Dougiamas
1509 * this section generates $max_upload_size in bytes
1513 function checkUploadSize()
1515 if (! $filesize = ini_get('upload_max_filesize')) {
1519 if ($postsize = ini_get('post_max_size')) {
1522 min(PMA_getRealSize($filesize), PMA_getRealSize($postsize))
1525 $this->set('max_upload_size', PMA_getRealSize($filesize));
1530 * Checks if protocol is https
1532 * This function checks if the https protocol is used in the PmaAbsoluteUri
1533 * configuration setting, as opposed to detectHttps() which checks if the
1534 * https protocol is used on the active connection.
1538 public function isHttps()
1541 if (null !== $this->get('is_https')) {
1542 return $this->get('is_https');
1545 $url = parse_url($this->get('PmaAbsoluteUri'));
1547 if (isset($url['scheme']) && $url['scheme'] == 'https') {
1553 $this->set('is_https', $is_https);
1559 * Detects whether https appears to be used.
1561 * This function checks if the https protocol is used in the current connection
1562 * with the webserver, based on environment variables.
1563 * Please note that this just detects what we see, so
1564 * it completely ignores things like reverse proxies.
1568 function detectHttps()
1572 // At first we try to parse REQUEST_URI, it might contain full URL,
1573 if (PMA_getenv('REQUEST_URI')) {
1574 // produces E_WARNING if it cannot get parsed, e.g. '/foobar:/'
1575 $url = @parse_url
(PMA_getenv('REQUEST_URI'));
1576 if ($url === false) {
1581 // If we don't have scheme, we didn't have full URL so we need to
1583 if (empty($url['scheme'])) {
1585 if (PMA_getenv('HTTP_SCHEME')) {
1586 $url['scheme'] = PMA_getenv('HTTP_SCHEME');
1587 } elseif (PMA_getenv('HTTPS')
1588 && strtolower(PMA_getenv('HTTPS')) == 'on'
1590 $url['scheme'] = 'https';
1591 // A10 Networks load balancer:
1592 } elseif (PMA_getenv('HTTP_HTTPS_FROM_LB')
1593 && strtolower(PMA_getenv('HTTP_HTTPS_FROM_LB')) == 'on'
1595 $url['scheme'] = 'https';
1596 } elseif (PMA_getenv('HTTP_X_FORWARDED_PROTO')) {
1597 $url['scheme'] = strtolower(PMA_getenv('HTTP_X_FORWARDED_PROTO'));
1598 } elseif (PMA_getenv('HTTP_FRONT_END_HTTPS')
1599 && strtolower(PMA_getenv('HTTP_FRONT_END_HTTPS')) == 'on'
1601 $url['scheme'] = 'https';
1603 $url['scheme'] = 'http';
1607 if (isset($url['scheme']) && $url['scheme'] == 'https') {
1617 * detect correct cookie path
1621 function checkCookiePath()
1623 $this->set('cookie_path', $this->getCookiePath());
1631 public function getCookiePath()
1633 static $cookie_path = null;
1635 if (null !== $cookie_path && !defined('TESTSUITE')) {
1636 return $cookie_path;
1639 $parsed_url = parse_url($this->get('PmaAbsoluteUri'));
1641 $cookie_path = $parsed_url['path'];
1643 return $cookie_path;
1647 * enables backward compatibility
1653 $GLOBALS['cfg'] = $this->settings
;
1654 $GLOBALS['default_server'] = $this->default_server
;
1655 unset($this->default_server
);
1656 $GLOBALS['collation_connection'] = $this->get('collation_connection');
1657 $GLOBALS['is_upload'] = $this->get('enable_upload');
1658 $GLOBALS['max_upload_size'] = $this->get('max_upload_size');
1659 $GLOBALS['cookie_path'] = $this->get('cookie_path');
1660 $GLOBALS['is_https'] = $this->get('is_https');
1664 'PMA_THEME_VERSION',
1665 'PMA_THEME_GENERATION',
1666 'PMA_PHP_STR_VERSION',
1667 'PMA_PHP_INT_VERSION',
1672 'PMA_USR_BROWSER_VER',
1673 'PMA_USR_BROWSER_AGENT'
1676 foreach ($defines as $define) {
1677 if (! defined($define)) {
1678 define($define, $this->get($define));
1684 * returns options for font size selection
1686 * @param string $current_size current selected font size with unit
1688 * @return array selectable font sizes
1692 static protected function getFontsizeOptions($current_size = '82%')
1694 $unit = preg_replace('/[0-9.]*/', '', $current_size);
1695 $value = preg_replace('/[^0-9.]*/', '', $current_size);
1699 $options["$value"] = $value . $unit;
1701 if ($unit === '%') {
1705 } elseif ($unit === 'em') {
1709 } elseif ($unit === 'pt') {
1712 } elseif ($unit === 'px') {
1717 //unknown font size unit
1725 foreach ($factors as $key => $factor) {
1726 $option_inc = $value +
$factor;
1727 $option_dec = $value - $factor;
1728 while (count($options) < 21) {
1729 $options["$option_inc"] = $option_inc . $unit;
1730 if ($option_dec > $factors[0]) {
1731 $options["$option_dec"] = $option_dec . $unit;
1733 $option_inc +
= $factor;
1734 $option_dec -= $factor;
1735 if (isset($factors[$key +
1])
1736 && $option_inc >= $value +
$factors[$key +
1]
1747 * returns html selectbox for font sizes
1751 * @return string html selectbox
1753 static protected function getFontsizeSelection()
1755 $current_size = $GLOBALS['PMA_Config']->get('fontsize');
1756 // for the case when there is no config file (this is supported)
1757 if (empty($current_size)) {
1758 if (isset($_COOKIE['pma_fontsize'])) {
1759 $current_size = $_COOKIE['pma_fontsize'];
1761 $current_size = '82%';
1764 $options = PMA_Config
::getFontsizeOptions($current_size);
1766 $return = '<label for="select_fontsize">' . __('Font size')
1767 . ':</label>' . "\n"
1768 . '<select name="set_fontsize" id="select_fontsize"'
1769 . ' class="autosubmit">' . "\n";
1770 foreach ($options as $option) {
1771 $return .= '<option value="' . $option . '"';
1772 if ($option == $current_size) {
1773 $return .= ' selected="selected"';
1775 $return .= '>' . $option . '</option>' . "\n";
1777 $return .= '</select>';
1783 * return complete font size selection form
1787 * @return string html selectbox
1789 static public function getFontsizeForm()
1791 return '<form name="form_fontsize_selection" id="form_fontsize_selection"'
1792 . ' method="get" action="index.php" class="disableAjax">' . "\n"
1793 . PMA_URL_getHiddenInputs() . "\n"
1794 . PMA_Config
::getFontsizeSelection() . "\n"
1801 * @param string $cookie name of cookie to remove
1803 * @return boolean result of setcookie()
1805 function removeCookie($cookie)
1807 if (defined('TESTSUITE')) {
1808 if (isset($_COOKIE[$cookie])) {
1809 unset($_COOKIE[$cookie]);
1817 $this->getCookiePath(),
1824 * sets cookie if value is different from current cookie value,
1825 * or removes if value is equal to default
1827 * @param string $cookie name of cookie to remove
1828 * @param mixed $value new cookie value
1829 * @param string $default default value
1830 * @param int $validity validity of cookie in seconds (default is one month)
1831 * @param bool $httponly whether cookie is only for HTTP (and not for scripts)
1833 * @return boolean result of setcookie()
1835 function setCookie($cookie, $value, $default = null, $validity = null,
1838 if (strlen($value) && null !== $default && $value === $default) {
1839 // default value is used
1840 if (isset($_COOKIE[$cookie])) {
1842 return $this->removeCookie($cookie);
1847 if (! strlen($value) && isset($_COOKIE[$cookie])) {
1848 // remove cookie, value is empty
1849 return $this->removeCookie($cookie);
1852 if (! isset($_COOKIE[$cookie]) ||
$_COOKIE[$cookie] !== $value) {
1853 // set cookie with new value
1854 /* Calculate cookie validity */
1855 if ($validity === null) {
1856 $validity = time() +
2592000;
1857 } elseif ($validity == 0) {
1860 $validity = time() +
$validity;
1862 if (defined('TESTSUITE')) {
1863 $_COOKIE[$cookie] = $value;
1870 $this->getCookiePath(),
1877 // cookie has already $value as value
1884 * Error handler to catch fatal errors when loading configuration
1889 function PMA_Config_fatalErrorHandler()
1891 if (isset($GLOBALS['pma_config_loading']) && $GLOBALS['pma_config_loading']) {
1892 $error = error_get_last();
1893 if ($error !== null) {
1896 'Failed to load phpMyAdmin configuration (%s:%s): %s',
1897 PMA_Error
::relPath($error['file']),
1906 if (!defined('TESTSUITE')) {
1907 register_shutdown_function('PMA_Config_fatalErrorHandler');