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 public 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 public function checkSystem()
117 $this->set('PMA_VERSION', '4.5.4.1');
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 public 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 * Sets the client platform based on user agent
166 * @param string $user_agent the user agent
170 private function _setClientPlatform($user_agent)
172 if (/*overload*/mb_strstr($user_agent, 'Win')) {
173 $this->set('PMA_USR_OS', 'Win');
174 } elseif (/*overload*/mb_strstr($user_agent, 'Mac')) {
175 $this->set('PMA_USR_OS', 'Mac');
176 } elseif (/*overload*/mb_strstr($user_agent, 'Linux')) {
177 $this->set('PMA_USR_OS', 'Linux');
178 } elseif (/*overload*/mb_strstr($user_agent, 'Unix')) {
179 $this->set('PMA_USR_OS', 'Unix');
180 } elseif (/*overload*/mb_strstr($user_agent, 'OS/2')) {
181 $this->set('PMA_USR_OS', 'OS/2');
183 $this->set('PMA_USR_OS', 'Other');
188 * Determines platform (OS), browser and version of the user
189 * Based on a phpBuilder article:
191 * @see http://www.phpbuilder.net/columns/tim20000821.php
195 public function checkClient()
197 if (PMA_getenv('HTTP_USER_AGENT')) {
198 $HTTP_USER_AGENT = PMA_getenv('HTTP_USER_AGENT');
200 $HTTP_USER_AGENT = '';
204 $this->_setClientPlatform($HTTP_USER_AGENT);
206 // 2. browser and version
207 // (must check everything else before Mozilla)
209 $is_mozilla = preg_match(
210 '@Mozilla/([0-9].[0-9]{1,2})@',
216 '@Opera(/| )([0-9].[0-9]{1,2})@',
220 $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
221 $this->set('PMA_USR_BROWSER_AGENT', 'OPERA');
222 } elseif (preg_match(
223 '@(MS)?IE ([0-9]{1,2}.[0-9]{1,2})@',
227 $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
228 $this->set('PMA_USR_BROWSER_AGENT', 'IE');
229 } elseif (preg_match(
234 $this->set('PMA_USR_BROWSER_VER', intval($log_version[1]) +
4);
235 $this->set('PMA_USR_BROWSER_AGENT', 'IE');
236 } elseif (preg_match(
237 '@OmniWeb/([0-9].[0-9]{1,2})@',
241 $this->set('PMA_USR_BROWSER_VER', $log_version[1]);
242 $this->set('PMA_USR_BROWSER_AGENT', 'OMNIWEB');
243 // Konqueror 2.2.2 says Konqueror/2.2.2
244 // Konqueror 3.0.3 says Konqueror/3
245 } elseif (preg_match(
246 '@(Konqueror/)(.*)(;)@',
250 $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
251 $this->set('PMA_USR_BROWSER_AGENT', 'KONQUEROR');
252 // must check Chrome before Safari
253 } elseif ($is_mozilla
254 && preg_match('@Chrome/([0-9.]*)@', $HTTP_USER_AGENT, $log_version)
256 $this->set('PMA_USR_BROWSER_VER', $log_version[1]);
257 $this->set('PMA_USR_BROWSER_AGENT', 'CHROME');
259 } elseif ($is_mozilla
260 && preg_match('@Version/(.*) Safari@', $HTTP_USER_AGENT, $log_version)
263 'PMA_USR_BROWSER_VER', $log_version[1]
265 $this->set('PMA_USR_BROWSER_AGENT', 'SAFARI');
267 } elseif ($is_mozilla
268 && preg_match('@Safari/([0-9]*)@', $HTTP_USER_AGENT, $log_version)
271 'PMA_USR_BROWSER_VER', $mozilla_version[1] . '.' . $log_version[1]
273 $this->set('PMA_USR_BROWSER_AGENT', 'SAFARI');
275 } elseif (! /*overload*/mb_strstr($HTTP_USER_AGENT, 'compatible')
276 && preg_match('@Firefox/([\w.]+)@', $HTTP_USER_AGENT, $log_version)
279 'PMA_USR_BROWSER_VER', $log_version[1]
281 $this->set('PMA_USR_BROWSER_AGENT', 'FIREFOX');
282 } elseif (preg_match('@rv:1.9(.*)Gecko@', $HTTP_USER_AGENT)) {
283 $this->set('PMA_USR_BROWSER_VER', '1.9');
284 $this->set('PMA_USR_BROWSER_AGENT', 'GECKO');
285 } elseif ($is_mozilla) {
286 $this->set('PMA_USR_BROWSER_VER', $mozilla_version[1]);
287 $this->set('PMA_USR_BROWSER_AGENT', 'MOZILLA');
289 $this->set('PMA_USR_BROWSER_VER', 0);
290 $this->set('PMA_USR_BROWSER_AGENT', 'OTHER');
295 * Whether GD2 is present
299 public function checkGd2()
301 if ($this->get('GD2Available') == 'yes') {
302 $this->set('PMA_IS_GD2', 1);
306 if ($this->get('GD2Available') == 'no') {
307 $this->set('PMA_IS_GD2', 0);
311 if (!@function_exists
('imagecreatetruecolor')) {
312 $this->set('PMA_IS_GD2', 0);
316 if (@function_exists
('gd_info')) {
318 if (/*overload*/mb_strstr($gd_nfo["GD Version"], '2.')) {
319 $this->set('PMA_IS_GD2', 1);
321 $this->set('PMA_IS_GD2', 0);
324 $this->set('PMA_IS_GD2', 0);
329 * Whether the Web server php is running on is IIS
333 public function checkWebServer()
335 // some versions return Microsoft-IIS, some Microsoft/IIS
336 // we could use a preg_match() but it's slower
337 if (PMA_getenv('SERVER_SOFTWARE')
338 && stristr(PMA_getenv('SERVER_SOFTWARE'), 'Microsoft')
339 && stristr(PMA_getenv('SERVER_SOFTWARE'), 'IIS')
341 $this->set('PMA_IS_IIS', 1);
343 $this->set('PMA_IS_IIS', 0);
348 * Whether the os php is running on is windows or not
352 public function checkWebServerOs()
354 // Default to Unix or Equiv
355 $this->set('PMA_IS_WINDOWS', 0);
356 // If PHP_OS is defined then continue
357 if (defined('PHP_OS')) {
358 if (stristr(PHP_OS
, 'win') && !stristr(PHP_OS
, 'darwin')) {
359 // Is it some version of Windows
360 $this->set('PMA_IS_WINDOWS', 1);
361 } elseif (stristr(PHP_OS
, 'OS/2')) {
362 // Is it OS/2 (No file permissions like Windows)
363 $this->set('PMA_IS_WINDOWS', 1);
369 * detects PHP version
373 public function checkPhpVersion()
377 '@([0-9]{1,2}).([0-9]{1,2}).([0-9]{1,2})@',
382 '@([0-9]{1,2}).([0-9]{1,2})@',
387 if (isset($match) && ! empty($match[1])) {
388 if (! isset($match[2])) {
391 if (! isset($match[3])) {
395 'PMA_PHP_INT_VERSION',
396 (int) sprintf('%d%02d%02d', $match[1], $match[2], $match[3])
399 $this->set('PMA_PHP_INT_VERSION', 0);
401 $this->set('PMA_PHP_STR_VERSION', phpversion());
405 * detects if Git revision
409 public function isGitRevision()
411 if (!$this->get('ShowGitRevision')) {
416 if (isset($_SESSION['is_git_revision'])) {
417 if ($_SESSION['is_git_revision']) {
418 $this->set('PMA_VERSION_GIT', 1);
420 return $_SESSION['is_git_revision'];
422 // find out if there is a .git folder
423 $git_folder = '.git';
424 if (! @file_exists
($git_folder)
425 ||
! @file_exists
($git_folder . '/config')
427 $_SESSION['is_git_revision'] = false;
430 $_SESSION['is_git_revision'] = true;
435 * detects Git revision, if running inside repo
439 public function checkGitRevision()
441 // find out if there is a .git folder
442 $git_folder = '.git';
443 if (! $this->isGitRevision()) {
447 if (! $ref_head = @file_get_contents
($git_folder . '/HEAD')) {
452 // are we on any branch?
453 if (/*overload*/mb_strstr($ref_head, '/')) {
454 $ref_head = /*overload*/mb_substr(trim($ref_head), 5);
455 if (substr($ref_head, 0, 11) === 'refs/heads/') {
456 $branch = /*overload*/mb_substr($ref_head, 11);
458 $branch = basename($ref_head);
461 $ref_file = $git_folder . '/' . $ref_head;
462 if (@file_exists
($ref_file)) {
463 $hash = @file_get_contents
($ref_file);
469 // deal with packed refs
470 $packed_refs = @file_get_contents
($git_folder . '/packed-refs');
471 if (! $packed_refs) {
474 // split file to lines
475 $ref_lines = explode("\n", $packed_refs);
476 foreach ($ref_lines as $line) {
478 if ($line[0] == '#') {
482 $parts = explode(' ', $line);
483 // care only about named refs
484 if (count($parts) != 2) {
487 // have found our ref?
488 if ($parts[1] == $ref_head) {
493 if (! isset($hash)) {
494 // Could not find ref
499 $hash = trim($ref_head);
503 if (isset($_SESSION['PMA_VERSION_COMMITDATA_' . $hash])) {
504 $commit = $_SESSION['PMA_VERSION_COMMITDATA_' . $hash];
505 } elseif (function_exists('gzuncompress')) {
506 $git_file_name = $git_folder . '/objects/'
507 . substr($hash, 0, 2) . '/' . substr($hash, 2);
508 if (file_exists($git_file_name) ) {
509 if (! $commit = @file_get_contents
($git_file_name)) {
512 $commit = explode("\0", gzuncompress($commit), 2);
513 $commit = explode("\n", $commit[1]);
514 $_SESSION['PMA_VERSION_COMMITDATA_' . $hash] = $commit;
516 $pack_names = array();
517 // work with packed data
518 $packs_file = $git_folder . '/objects/info/packs';
519 if (file_exists($packs_file)
520 && $packs = @file_get_contents
($packs_file)
522 // File exists. Read it, parse the file to get the names of the
523 // packs. (to look for them in .git/object/pack directory later)
524 foreach (explode("\n", $packs) as $line) {
526 if (strlen(trim($line)) == 0) {
529 // skip non pack lines
530 if ($line[0] != 'P') {
534 $pack_names[] = substr($line, 2);
537 // '.git/objects/info/packs' file can be missing
538 // (atlease in mysGit)
539 // File missing. May be we can look in the .git/object/pack
540 // directory for all the .pack files and use that list of
542 $dirIterator = new DirectoryIterator(
543 $git_folder . '/objects/pack'
545 foreach ($dirIterator as $file_info) {
546 $file_name = $file_info->getFilename();
547 // if this is a .pack file
548 if ($file_info->isFile() && substr($file_name, -5) == '.pack'
550 $pack_names[] = $file_name;
554 $hash = strtolower($hash);
555 foreach ($pack_names as $pack_name) {
556 $index_name = str_replace('.pack', '.idx', $pack_name);
559 $index_data = @file_get_contents
(
560 $git_folder . '/objects/pack/' . $index_name
566 if (substr($index_data, 0, 4) != "\377tOc") {
570 $version = unpack('N', substr($index_data, 4, 4));
571 if ($version[1] != 2) {
574 // parse fanout table
577 substr($index_data, 8, 256 * 4)
580 // find where we should search
581 $firstbyte = intval(substr($hash, 0, 2), 16);
582 // array is indexed from 1 and we need to get
583 // previous entry for start
584 if ($firstbyte == 0) {
587 $start = $fanout[$firstbyte];
589 $end = $fanout[$firstbyte +
1];
591 // stupid linear search for our sha
593 $offset = 8 +
(256 * 4);
594 for ($position = $start; $position < $end; $position++
) {
597 substr($index_data, $offset +
($position * 20), 20)
609 $offset = 8 +
(256 * 4) +
(24 * $fanout[256]);
610 $pack_offset = unpack(
612 substr($index_data, $offset +
($position * 4), 4)
614 $pack_offset = $pack_offset[1];
618 $git_folder . '/objects/pack/' . $pack_name, 'rb'
620 if ($pack_file === false) {
624 fseek($pack_file, $pack_offset);
627 $header = ord(fread($pack_file, 1));
628 $type = ($header >> 4) & 7;
629 $hasnext = ($header & 128) >> 7;
630 $size = $header & 0xf;
634 $byte = ord(fread($pack_file, 1));
635 $size |
= ($byte & 0x7f) << $offset;
636 $hasnext = ($byte & 128) >> 7;
640 // we care only about commit objects
646 $commit = fread($pack_file, $size);
647 $commit = gzuncompress($commit);
648 $commit = explode("\n", $commit);
649 $_SESSION['PMA_VERSION_COMMITDATA_' . $hash] = $commit;
655 // check if commit exists in Github
656 if ($commit !== false
657 && isset($_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash])
659 $is_remote_commit = $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash];
661 $link = 'https://api.github.com/repos/phpmyadmin/phpmyadmin/git/commits/'
663 $is_found = $this->checkHTTP($link, ! $commit);
666 $is_remote_commit = false;
667 $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = false;
670 // no remote link for now, but don't cache this as Github is down
671 $is_remote_commit = false;
674 $is_remote_commit = true;
675 $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = true;
676 if ($commit === false) {
677 // if no local commit data, try loading from Github
678 $commit_json = json_decode($is_found);
684 $is_remote_branch = false;
685 if ($is_remote_commit && $branch !== false) {
686 // check if branch exists in Github
687 if (isset($_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash])) {
688 $is_remote_branch = $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash];
690 $link = 'https://api.github.com/repos/phpmyadmin/phpmyadmin'
691 . '/git/trees/' . $branch;
692 $is_found = $this->checkHTTP($link);
695 $is_remote_branch = true;
696 $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = true;
699 $is_remote_branch = false;
700 $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = false;
703 // no remote link for now, but don't cache this as Github is down
704 $is_remote_branch = false;
710 if ($commit !== false) {
711 $author = array('name' => '', 'email' => '', 'date' => '');
712 $committer = array('name' => '', 'email' => '', 'date' => '');
715 $dataline = array_shift($commit);
716 $datalinearr = explode(' ', $dataline, 2);
717 $linetype = $datalinearr[0];
718 if (in_array($linetype, array('author', 'committer'))) {
719 $user = $datalinearr[1];
720 preg_match('/([^<]+)<([^>]+)> ([0-9]+)( [^ ]+)?/', $user, $user);
722 'name' => trim($user[1]),
723 'email' => trim($user[2]),
724 'date' => date('Y-m-d H:i:s', $user[3]));
725 if (isset($user[4])) {
726 $user2['date'] .= $user[4];
730 } while ($dataline != '');
731 $message = trim(implode(' ', $commit));
733 } elseif (isset($commit_json)) {
735 'name' => $commit_json->author
->name
,
736 'email' => $commit_json->author
->email
,
737 'date' => $commit_json->author
->date
);
739 'name' => $commit_json->committer
->name
,
740 'email' => $commit_json->committer
->email
,
741 'date' => $commit_json->committer
->date
);
742 $message = trim($commit_json->message
);
747 $this->set('PMA_VERSION_GIT', 1);
748 $this->set('PMA_VERSION_GIT_COMMITHASH', $hash);
749 $this->set('PMA_VERSION_GIT_BRANCH', $branch);
750 $this->set('PMA_VERSION_GIT_MESSAGE', $message);
751 $this->set('PMA_VERSION_GIT_AUTHOR', $author);
752 $this->set('PMA_VERSION_GIT_COMMITTER', $committer);
753 $this->set('PMA_VERSION_GIT_ISREMOTECOMMIT', $is_remote_commit);
754 $this->set('PMA_VERSION_GIT_ISREMOTEBRANCH', $is_remote_branch);
758 * Checks if given URL is 200 or 404, optionally returns data
760 * @param string $link the URL to check
761 * @param boolean $get_body whether to retrieve body of document
763 * @return string|boolean test result or data
765 public function checkHTTP($link, $get_body = false)
767 if (! function_exists('curl_init')) {
770 $handle = curl_init($link);
771 if ($handle === false) {
774 PMA_Util
::configureCurl($handle);
775 curl_setopt($handle, CURLOPT_FOLLOWLOCATION
, 0);
776 curl_setopt($handle, CURLOPT_RETURNTRANSFER
, 1);
777 curl_setopt($handle, CURLOPT_SSL_VERIFYHOST
, 0);
778 curl_setopt($handle, CURLOPT_SSL_VERIFYPEER
, 0);
779 curl_setopt($handle, CURLOPT_CONNECTTIMEOUT
, 5);
780 curl_setopt($handle, CURLOPT_TIMEOUT
, 5);
781 curl_setopt($handle, CURLOPT_IPRESOLVE
, CURL_IPRESOLVE_V4
);
782 if (! defined('TESTSUITE')) {
783 session_write_close();
785 $data = @curl_exec
($handle);
786 if (! defined('TESTSUITE')) {
787 ini_set('session.use_only_cookies', '0');
788 ini_set('session.use_cookies', '0');
789 ini_set('session.use_trans_sid', '0');
790 ini_set('session.cache_limiter', 'nocache');
793 if ($data === false) {
796 $http_status = curl_getinfo($handle, CURLINFO_HTTP_CODE
);
798 if ($http_status == 200) {
799 return $get_body ?
$data : true;
802 if ($http_status == 404) {
809 * loads default values from default source
811 * @return boolean success
813 public function loadDefaults()
816 if (! file_exists($this->default_source
)) {
817 $this->error_config_default_file
= true;
820 include $this->default_source
;
822 $this->default_source_mtime
= filemtime($this->default_source
);
824 $this->default_server
= $cfg['Servers'][1];
825 unset($cfg['Servers']);
827 $this->default = $cfg;
828 $this->settings
= PMA_arrayMergeRecursive($this->settings
, $cfg);
830 $this->error_config_default_file
= false;
836 * loads configuration from $source, usually the config file
837 * should be called on object creation
839 * @param string $source config file
843 public function load($source = null)
845 $this->loadDefaults();
847 if (null !== $source) {
848 $this->setSource($source);
852 * We check and set the font size at this point, to make the font size
853 * selector work also for users without a config.inc.php
855 $this->checkFontsize();
857 if (! $this->checkConfigSource()) {
858 // even if no config file, set collation_connection
859 $this->checkCollationConnection();
866 * Parses the configuration file, we throw away any errors or
869 $old_error_reporting = error_reporting(0);
871 $GLOBALS['pma_config_loading'] = true;
872 $eval_result = include $this->getSource();
873 $GLOBALS['pma_config_loading'] = false;
875 error_reporting($old_error_reporting);
877 if ($eval_result === false) {
878 $this->error_config_file
= true;
880 $this->error_config_file
= false;
881 $this->source_mtime
= filemtime($this->getSource());
885 * Backward compatibility code
887 if (!empty($cfg['DefaultTabTable'])) {
888 $cfg['DefaultTabTable'] = str_replace(
892 'tbl_properties.php',
894 $cfg['DefaultTabTable']
898 if (!empty($cfg['DefaultTabDatabase'])) {
899 $cfg['DefaultTabDatabase'] = str_replace(
905 $cfg['DefaultTabDatabase']
910 $this->settings
= PMA_arrayMergeRecursive($this->settings
, $cfg);
911 $this->checkPmaAbsoluteUri();
913 // Handling of the collation must be done after merging of $cfg
914 // (from config.inc.php) so that $cfg['DefaultConnectionCollation']
915 // can have an effect.
916 $this->checkCollationConnection();
922 * Saves the connection collation
924 * @param array $config_data configuration data from user preferences
928 private function _saveConnectionCollation($config_data)
931 // just to shorten the lines
932 $collation = 'collation_connection';
933 if (isset($GLOBALS[$collation])
934 && (isset($_COOKIE['pma_collation_connection'])
935 ||
isset($_POST[$collation]))
937 if ((! isset($config_data[$collation])
938 && $GLOBALS[$collation] != 'utf8_general_ci')
939 ||
isset($config_data[$collation])
940 && $GLOBALS[$collation] != $config_data[$collation]
945 $GLOBALS[$collation],
950 // read collation from settings
951 if (isset($config_data[$collation])) {
953 = $config_data[$collation];
955 'pma_collation_connection',
964 * Loads user preferences and merges them with current config
965 * must be called after control connection has been established
969 public function loadUserPreferences()
971 // index.php should load these settings, so that phpmyadmin.css.php
972 // will have everything available in session cache
973 $server = isset($GLOBALS['server'])
975 : (!empty($GLOBALS['cfg']['ServerDefault'])
976 ?
$GLOBALS['cfg']['ServerDefault']
978 $cache_key = 'server_' . $server;
979 if ($server > 0 && !defined('PMA_MINIMUM_COMMON')) {
980 $config_mtime = max($this->default_source_mtime
, $this->source_mtime
);
981 // cache user preferences, use database only when needed
982 if (! isset($_SESSION['cache'][$cache_key]['userprefs'])
983 ||
$_SESSION['cache'][$cache_key]['config_mtime'] < $config_mtime
985 // load required libraries
986 include_once './libraries/user_preferences.lib.php';
987 $prefs = PMA_loadUserprefs();
988 $_SESSION['cache'][$cache_key]['userprefs']
989 = PMA_applyUserprefs($prefs['config_data']);
990 $_SESSION['cache'][$cache_key]['userprefs_mtime'] = $prefs['mtime'];
991 $_SESSION['cache'][$cache_key]['userprefs_type'] = $prefs['type'];
992 $_SESSION['cache'][$cache_key]['config_mtime'] = $config_mtime;
994 } elseif ($server == 0
995 ||
! isset($_SESSION['cache'][$cache_key]['userprefs'])
997 $this->set('user_preferences', false);
1000 $config_data = $_SESSION['cache'][$cache_key]['userprefs'];
1001 // type is 'db' or 'session'
1004 $_SESSION['cache'][$cache_key]['userprefs_type']
1007 'user_preferences_mtime',
1008 $_SESSION['cache'][$cache_key]['userprefs_mtime']
1011 // backup some settings
1013 if (isset($this->settings
['fontsize'])) {
1014 $org_fontsize = $this->settings
['fontsize'];
1016 // load config array
1017 $this->settings
= PMA_arrayMergeRecursive($this->settings
, $config_data);
1018 $GLOBALS['cfg'] = PMA_arrayMergeRecursive($GLOBALS['cfg'], $config_data);
1019 if (defined('PMA_MINIMUM_COMMON')) {
1023 // settings below start really working on next page load, but
1024 // changes are made only in index.php so everything is set when
1028 /** @var PMA_Theme_Manager $tmanager */
1029 $tmanager = $_SESSION['PMA_Theme_Manager'];
1030 if ($tmanager->getThemeCookie() ||
isset($_REQUEST['set_theme'])) {
1031 if ((! isset($config_data['ThemeDefault'])
1032 && $tmanager->theme
->getId() != 'original')
1033 ||
isset($config_data['ThemeDefault'])
1034 && $config_data['ThemeDefault'] != $tmanager->theme
->getId()
1036 // new theme was set in common.inc.php
1037 $this->setUserValue(
1040 $tmanager->theme
->getId(),
1045 // no cookie - read default from settings
1046 if ($this->settings
['ThemeDefault'] != $tmanager->theme
->getId()
1047 && $tmanager->checkTheme($this->settings
['ThemeDefault'])
1049 $tmanager->setActiveTheme($this->settings
['ThemeDefault']);
1050 $tmanager->setThemeCookie();
1055 if ((! isset($config_data['fontsize'])
1056 && $org_fontsize != '82%')
1057 ||
isset($config_data['fontsize'])
1058 && $org_fontsize != $config_data['fontsize']
1060 $this->setUserValue(null, 'fontsize', $org_fontsize, '82%');
1064 if (isset($_COOKIE['pma_lang']) ||
isset($_POST['lang'])) {
1065 if ((! isset($config_data['lang'])
1066 && $GLOBALS['lang'] != 'en')
1067 ||
isset($config_data['lang'])
1068 && $GLOBALS['lang'] != $config_data['lang']
1070 $this->setUserValue(null, 'lang', $GLOBALS['lang'], 'en');
1073 // read language from settings
1074 if (isset($config_data['lang']) && PMA_langSet($config_data['lang'])) {
1075 $this->setCookie('pma_lang', $GLOBALS['lang']);
1079 // save connection collation
1080 $this->_saveConnectionCollation($config_data);
1084 * Sets config value which is stored in user preferences (if available)
1087 * If user preferences are not yet initialized, option is applied to
1088 * global config and added to a update queue, which is processed
1089 * by {@link loadUserPreferences()}
1091 * @param string $cookie_name can be null
1092 * @param string $cfg_path configuration path
1093 * @param mixed $new_cfg_value new value
1094 * @param mixed $default_value default value
1098 public function setUserValue($cookie_name, $cfg_path, $new_cfg_value,
1099 $default_value = null
1101 // use permanent user preferences if possible
1102 $prefs_type = $this->get('user_preferences');
1104 include_once './libraries/user_preferences.lib.php';
1105 if ($default_value === null) {
1106 $default_value = PMA_arrayRead($cfg_path, $this->default);
1108 PMA_persistOption($cfg_path, $new_cfg_value, $default_value);
1110 if ($prefs_type != 'db' && $cookie_name) {
1111 // fall back to cookies
1112 if ($default_value === null) {
1113 $default_value = PMA_arrayRead($cfg_path, $this->settings
);
1115 $this->setCookie($cookie_name, $new_cfg_value, $default_value);
1117 PMA_arrayWrite($cfg_path, $GLOBALS['cfg'], $new_cfg_value);
1118 PMA_arrayWrite($cfg_path, $this->settings
, $new_cfg_value);
1122 * Reads value stored by {@link setUserValue()}
1124 * @param string $cookie_name cookie name
1125 * @param mixed $cfg_value config value
1129 public function getUserValue($cookie_name, $cfg_value)
1131 $cookie_exists = isset($_COOKIE) && !empty($_COOKIE[$cookie_name]);
1132 $prefs_type = $this->get('user_preferences');
1133 if ($prefs_type == 'db') {
1134 // permanent user preferences value exists, remove cookie
1135 if ($cookie_exists) {
1136 $this->removeCookie($cookie_name);
1138 } else if ($cookie_exists) {
1139 return $_COOKIE[$cookie_name];
1141 // return value from $cfg array
1148 * @param string $source source
1152 public function setSource($source)
1154 $this->source
= trim($source);
1158 * check config source
1160 * @return boolean whether source is valid or not
1162 public function checkConfigSource()
1164 if (! $this->getSource()) {
1165 // no configuration file set at all
1169 if (! file_exists($this->getSource())) {
1170 $this->source_mtime
= 0;
1174 if (! is_readable($this->getSource())) {
1175 // manually check if file is readable
1176 // might be bug #3059806 Supporting running from CIFS/Samba shares
1179 $handle = @fopen
($this->getSource(), 'r');
1180 if ($handle !== false) {
1181 $contents = @fread
($handle, 1); // reading 1 byte is enough to test
1184 if ($contents === false) {
1185 $this->source_mtime
= 0;
1188 function_exists('__')
1189 ?
__('Existing configuration file (%s) is not readable.')
1190 : 'Existing configuration file (%s) is not readable.',
1202 * verifies the permissions on config file (if asked by configuration)
1203 * (must be called after config.inc.php has been merged)
1207 public function checkPermissions()
1209 // Check for permissions (on platforms that support it):
1210 if ($this->get('CheckConfigurationPermissions')) {
1211 $perms = @fileperms
($this->getSource());
1212 if (!($perms === false) && ($perms & 2)) {
1213 // This check is normally done after loading configuration
1214 $this->checkWebServerOs();
1215 if ($this->get('PMA_IS_WINDOWS') == 0) {
1216 $this->source_mtime
= 0;
1219 'Wrong permissions on configuration file, '
1220 . 'should not be world writable!'
1229 * returns specific config setting
1231 * @param string $setting config setting
1233 * @return mixed value
1235 public function get($setting)
1237 if (isset($this->settings
[$setting])) {
1238 return $this->settings
[$setting];
1244 * sets configuration variable
1246 * @param string $setting configuration option
1247 * @param mixed $value new value for configuration option
1251 public function set($setting, $value)
1253 if (! isset($this->settings
[$setting])
1254 ||
$this->settings
[$setting] !== $value
1256 $this->settings
[$setting] = $value;
1257 $this->set_mtime
= time();
1262 * returns source for current config
1264 * @return string config source
1266 public function getSource()
1268 return $this->source
;
1272 * returns a unique value to force a CSS reload if either the config
1273 * or the theme changes
1274 * must also check the pma_fontsize cookie in case there is no
1277 * @return int Summary of unix timestamps and fontsize,
1278 * to be unique on theme parameters change
1280 public function getThemeUniqueValue()
1282 if (null !== $this->get('fontsize')) {
1283 $fontsize = intval($this->get('fontsize'));
1284 } elseif (isset($_COOKIE['pma_fontsize'])) {
1285 $fontsize = intval($_COOKIE['pma_fontsize']);
1291 $this->source_mtime +
1292 $this->default_source_mtime +
1293 $this->get('user_preferences_mtime') +
1294 $_SESSION['PMA_Theme']->mtime_info +
1295 $_SESSION['PMA_Theme']->filesize_info
);
1299 * $cfg['PmaAbsoluteUri'] is a required directive else cookies won't be
1300 * set properly and, depending on browsers, inserting or updating a
1305 public function checkPmaAbsoluteUri()
1307 // Setup a default value to let the people and lazy sysadmins work anyway,
1308 // they'll get an error if the autodetect code doesn't work
1309 $pma_absolute_uri = $this->get('PmaAbsoluteUri');
1310 $is_https = $this->detectHttps();
1312 if (/*overload*/mb_strlen($pma_absolute_uri) < 5) {
1315 // If we don't have scheme, we didn't have full URL so we need to
1317 if (empty($url['scheme'])) {
1320 $url['scheme'] = 'https';
1322 $url['scheme'] = 'http';
1326 if (PMA_getenv('HTTP_HOST')) {
1327 // Prepend the scheme before using parse_url() since this
1328 // is not part of the RFC2616 Host request-header
1329 $parsed_url = parse_url(
1330 $url['scheme'] . '://' . PMA_getenv('HTTP_HOST')
1332 if (!empty($parsed_url['host'])) {
1335 $url['host'] = PMA_getenv('HTTP_HOST');
1337 } elseif (PMA_getenv('SERVER_NAME')) {
1338 $url['host'] = PMA_getenv('SERVER_NAME');
1340 $this->error_pma_uri
= true;
1344 // If we didn't set port yet...
1345 if (empty($url['port']) && PMA_getenv('SERVER_PORT')) {
1346 $url['port'] = PMA_getenv('SERVER_PORT');
1349 // And finally the path could be already set from REQUEST_URI
1350 if (empty($url['path'])) {
1351 // we got a case with nginx + php-fpm where PHP_SELF
1352 // was not set, so PMA_PHP_SELF was not set as well
1353 if (isset($GLOBALS['PMA_PHP_SELF'])) {
1354 $path = parse_url($GLOBALS['PMA_PHP_SELF']);
1356 $path = parse_url(PMA_getenv('REQUEST_URI'));
1358 $url['path'] = $path['path'];
1362 // Make url from parts we have
1363 $pma_absolute_uri = $url['scheme'] . '://';
1364 // Was there user information?
1365 if (!empty($url['user'])) {
1366 $pma_absolute_uri .= $url['user'];
1367 if (!empty($url['pass'])) {
1368 $pma_absolute_uri .= ':' . $url['pass'];
1370 $pma_absolute_uri .= '@';
1373 $pma_absolute_uri .= $url['host'];
1374 // Add port, if it not the default one
1375 // (or 80 for https which is most likely a bug)
1376 if (! empty($url['port'])
1377 && (($url['scheme'] == 'http' && $url['port'] != 80)
1378 ||
($url['scheme'] == 'https' && $url['port'] != 80)
1379 ||
($url['scheme'] == 'https' && $url['port'] != 443))
1381 $pma_absolute_uri .= ':' . $url['port'];
1383 // And finally path, without script name, the 'a' is there not to
1384 // strip our directory, when path is only /pmadir/ without filename.
1385 // Backslashes returned by Windows have to be changed.
1386 // Only replace backslashes by forward slashes if on Windows,
1387 // as the backslash could be valid on a non-Windows system.
1388 $this->checkWebServerOs();
1389 if ($this->get('PMA_IS_WINDOWS') == 1) {
1390 $path = str_replace("\\", "/", dirname($url['path'] . 'a'));
1392 $path = dirname($url['path'] . 'a');
1395 // To work correctly within javascript
1396 if (defined('PMA_PATH_TO_BASEDIR') && PMA_PATH_TO_BASEDIR
== '../') {
1397 if ($this->get('PMA_IS_WINDOWS') == 1) {
1398 $path = str_replace("\\", "/", dirname($path));
1400 $path = dirname($path);
1404 // PHP's dirname function would have returned a dot
1405 // when $path contains no slash
1409 // in vhost situations, there could be already an ending slash
1410 if (/*overload*/mb_substr($path, -1) != '/') {
1413 $pma_absolute_uri .= $path;
1415 // This is to handle the case of a reverse proxy
1416 if ($this->get('ForceSSL')) {
1417 $this->set('PmaAbsoluteUri', $pma_absolute_uri);
1418 $pma_absolute_uri = $this->getSSLUri();
1422 // We used to display a warning if PmaAbsoluteUri wasn't set, but now
1423 // the autodetect code works well enough that we don't display the
1424 // warning at all. The user can still set PmaAbsoluteUri manually.
1427 // The URI is specified, however users do often specify this
1428 // wrongly, so we try to fix this.
1430 // Adds a trailing slash et the end of the phpMyAdmin uri if it
1432 if (/*overload*/mb_substr($pma_absolute_uri, -1) != '/') {
1433 $pma_absolute_uri .= '/';
1436 // If URI doesn't start with http:// or https://, we will add
1438 if (/*overload*/mb_substr($pma_absolute_uri, 0, 7) != 'http://'
1439 && /*overload*/mb_substr($pma_absolute_uri, 0, 8) != 'https://'
1442 = ($is_https ?
'https' : 'http')
1445 /*overload*/mb_substr($pma_absolute_uri, 0, 2) == '//'
1449 . $pma_absolute_uri;
1452 $this->set('PmaAbsoluteUri', $pma_absolute_uri);
1456 * Converts currently used PmaAbsoluteUri to SSL based variant.
1458 * @return String witch adjusted URI
1460 public function getSSLUri()
1463 $url = $this->get('PmaAbsoluteUri');
1464 // Parse current URL
1465 $parsed = parse_url($url);
1466 // In case parsing has failed do stupid string replacement
1467 if ($parsed === false) {
1468 // Replace http protocol
1469 return preg_replace('@^http:@', 'https:', $url);
1472 // Reconstruct URL using parsed parts
1473 return 'https://' . $parsed['host'] . ':443' . $parsed['path'];
1477 * Sets collation_connection based on user preference. First is checked
1478 * value from request, then cookies with fallback to default.
1480 * After setting it here, cookie is set in common.inc.php to persist
1483 * @todo check validity of collation string
1487 public function checkCollationConnection()
1489 if (! empty($_REQUEST['collation_connection'])) {
1490 $collation = strip_tags($_REQUEST['collation_connection']);
1491 } elseif (! empty($_COOKIE['pma_collation_connection'])) {
1492 $collation = strip_tags($_COOKIE['pma_collation_connection']);
1494 $collation = $this->get('DefaultConnectionCollation');
1496 $this->set('collation_connection', $collation);
1500 * checks for font size configuration, and sets font size as requested by user
1504 public function checkFontsize()
1508 if (isset($_GET['set_fontsize'])) {
1509 $new_fontsize = $_GET['set_fontsize'];
1510 } elseif (isset($_POST['set_fontsize'])) {
1511 $new_fontsize = $_POST['set_fontsize'];
1512 } elseif (isset($_COOKIE['pma_fontsize'])) {
1513 $new_fontsize = $_COOKIE['pma_fontsize'];
1516 if (preg_match('/^[0-9.]+(px|em|pt|\%)$/', $new_fontsize)) {
1517 $this->set('fontsize', $new_fontsize);
1518 } elseif (! $this->get('fontsize')) {
1519 // 80% would correspond to the default browser font size
1520 // of 16, but use 82% to help read the monoface font
1521 $this->set('fontsize', '82%');
1524 $this->setCookie('pma_fontsize', $this->get('fontsize'), '82%');
1528 * checks if upload is enabled
1532 public function checkUpload()
1534 if (!ini_get('file_uploads')) {
1535 $this->set('enable_upload', false);
1539 $this->set('enable_upload', true);
1540 // if set "php_admin_value file_uploads Off" in httpd.conf
1541 // ini_get() also returns the string "Off" in this case:
1542 if ('off' == strtolower(ini_get('file_uploads'))) {
1543 $this->set('enable_upload', false);
1548 * Maximum upload size as limited by PHP
1549 * Used with permission from Moodle (http://moodle.org) by Martin Dougiamas
1551 * this section generates $max_upload_size in bytes
1555 public function checkUploadSize()
1557 if (! $filesize = ini_get('upload_max_filesize')) {
1561 if ($postsize = ini_get('post_max_size')) {
1564 min(PMA_getRealSize($filesize), PMA_getRealSize($postsize))
1567 $this->set('max_upload_size', PMA_getRealSize($filesize));
1572 * Checks if protocol is https
1574 * This function checks if the https protocol is used in the PmaAbsoluteUri
1575 * configuration setting, as opposed to detectHttps() which checks if the
1576 * https protocol is used on the active connection.
1580 public function isHttps()
1583 if (null !== $this->get('is_https')) {
1584 return $this->get('is_https');
1587 $url = parse_url($this->get('PmaAbsoluteUri'));
1589 $is_https = (isset($url['scheme']) && $url['scheme'] == 'https');
1591 $this->set('is_https', $is_https);
1597 * Detects whether https appears to be used.
1599 * This function checks if the https protocol is used in the current connection
1600 * with the webserver, based on environment variables.
1601 * Please note that this just detects what we see, so
1602 * it completely ignores things like reverse proxies.
1606 public function detectHttps()
1610 // At first we try to parse REQUEST_URI, it might contain full URL,
1611 if (PMA_getenv('REQUEST_URI')) {
1612 // produces E_WARNING if it cannot get parsed, e.g. '/foobar:/'
1613 $url = @parse_url
(PMA_getenv('REQUEST_URI'));
1614 if ($url === false) {
1619 // If we don't have scheme, we didn't have full URL so we need to
1621 if (empty($url['scheme'])) {
1623 if (PMA_getenv('HTTP_SCHEME')) {
1624 $url['scheme'] = PMA_getenv('HTTP_SCHEME');
1625 } elseif (PMA_getenv('HTTPS')
1626 && strtolower(PMA_getenv('HTTPS')) == 'on'
1628 $url['scheme'] = 'https';
1629 // A10 Networks load balancer:
1630 } elseif (PMA_getenv('HTTP_HTTPS_FROM_LB')
1631 && strtolower(PMA_getenv('HTTP_HTTPS_FROM_LB')) == 'on'
1633 $url['scheme'] = 'https';
1634 } elseif (PMA_getenv('HTTP_X_FORWARDED_PROTO')) {
1635 $url['scheme'] = /*overload*/mb_strtolower(
1636 PMA_getenv('HTTP_X_FORWARDED_PROTO')
1638 } elseif (PMA_getenv('HTTP_FRONT_END_HTTPS')
1639 && strtolower(PMA_getenv('HTTP_FRONT_END_HTTPS')) == 'on'
1641 $url['scheme'] = 'https';
1643 $url['scheme'] = 'http';
1647 if (isset($url['scheme']) && $url['scheme'] == 'https') {
1657 * detect correct cookie path
1661 public function checkCookiePath()
1663 $this->set('cookie_path', $this->getCookiePath());
1671 public function getCookiePath()
1673 static $cookie_path = null;
1675 if (null !== $cookie_path && !defined('TESTSUITE')) {
1676 return $cookie_path;
1679 $parsed_url = parse_url($this->get('PmaAbsoluteUri'));
1681 $cookie_path = $parsed_url['path'];
1683 return $cookie_path;
1687 * enables backward compatibility
1691 public function enableBc()
1693 $GLOBALS['cfg'] = $this->settings
;
1694 $GLOBALS['default_server'] = $this->default_server
;
1695 unset($this->default_server
);
1696 $GLOBALS['collation_connection'] = $this->get('collation_connection');
1697 $GLOBALS['is_upload'] = $this->get('enable_upload');
1698 $GLOBALS['max_upload_size'] = $this->get('max_upload_size');
1699 $GLOBALS['cookie_path'] = $this->get('cookie_path');
1700 $GLOBALS['is_https'] = $this->get('is_https');
1704 'PMA_THEME_VERSION',
1705 'PMA_THEME_GENERATION',
1706 'PMA_PHP_STR_VERSION',
1707 'PMA_PHP_INT_VERSION',
1712 'PMA_USR_BROWSER_VER',
1713 'PMA_USR_BROWSER_AGENT'
1716 foreach ($defines as $define) {
1717 if (! defined($define)) {
1718 define($define, $this->get($define));
1724 * returns options for font size selection
1726 * @param string $current_size current selected font size with unit
1728 * @return array selectable font sizes
1730 protected static function getFontsizeOptions($current_size = '82%')
1732 $unit = preg_replace('/[0-9.]*/', '', $current_size);
1733 $value = preg_replace('/[^0-9.]*/', '', $current_size);
1737 $options["$value"] = $value . $unit;
1739 if ($unit === '%') {
1743 } elseif ($unit === 'em') {
1747 } elseif ($unit === 'pt') {
1750 } elseif ($unit === 'px') {
1755 //unknown font size unit
1763 foreach ($factors as $key => $factor) {
1764 $option_inc = $value +
$factor;
1765 $option_dec = $value - $factor;
1766 while (count($options) < 21) {
1767 $options["$option_inc"] = $option_inc . $unit;
1768 if ($option_dec > $factors[0]) {
1769 $options["$option_dec"] = $option_dec . $unit;
1771 $option_inc +
= $factor;
1772 $option_dec -= $factor;
1773 if (isset($factors[$key +
1])
1774 && $option_inc >= $value +
$factors[$key +
1]
1785 * returns html selectbox for font sizes
1787 * @return string html selectbox
1789 protected static function getFontsizeSelection()
1791 $current_size = $GLOBALS['PMA_Config']->get('fontsize');
1792 // for the case when there is no config file (this is supported)
1793 if (empty($current_size)) {
1794 if (isset($_COOKIE['pma_fontsize'])) {
1795 $current_size = htmlspecialchars($_COOKIE['pma_fontsize']);
1797 $current_size = '82%';
1800 $options = PMA_Config
::getFontsizeOptions($current_size);
1802 $return = '<label for="select_fontsize">' . __('Font size')
1803 . ':</label>' . "\n"
1804 . '<select name="set_fontsize" id="select_fontsize"'
1805 . ' class="autosubmit">' . "\n";
1806 foreach ($options as $option) {
1807 $return .= '<option value="' . $option . '"';
1808 if ($option == $current_size) {
1809 $return .= ' selected="selected"';
1811 $return .= '>' . $option . '</option>' . "\n";
1813 $return .= '</select>';
1819 * return complete font size selection form
1821 * @return string html selectbox
1823 public static function getFontsizeForm()
1825 return '<form name="form_fontsize_selection" id="form_fontsize_selection"'
1826 . ' method="get" action="index.php" class="disableAjax">' . "\n"
1827 . PMA_URL_getHiddenInputs() . "\n"
1828 . PMA_Config
::getFontsizeSelection() . "\n"
1835 * @param string $cookie name of cookie to remove
1837 * @return boolean result of setcookie()
1839 public function removeCookie($cookie)
1841 if (defined('TESTSUITE')) {
1842 if (isset($_COOKIE[$cookie])) {
1843 unset($_COOKIE[$cookie]);
1851 $this->getCookiePath(),
1858 * sets cookie if value is different from current cookie value,
1859 * or removes if value is equal to default
1861 * @param string $cookie name of cookie to remove
1862 * @param mixed $value new cookie value
1863 * @param string $default default value
1864 * @param int $validity validity of cookie in seconds (default is one month)
1865 * @param bool $httponly whether cookie is only for HTTP (and not for scripts)
1867 * @return boolean result of setcookie()
1869 public function setCookie($cookie, $value, $default = null,
1870 $validity = null, $httponly = true
1872 if (/*overload*/mb_strlen($value) && null !== $default && $value === $default
1874 // default value is used
1875 if (isset($_COOKIE[$cookie])) {
1877 return $this->removeCookie($cookie);
1882 if (!/*overload*/mb_strlen($value) && isset($_COOKIE[$cookie])) {
1883 // remove cookie, value is empty
1884 return $this->removeCookie($cookie);
1887 if (! isset($_COOKIE[$cookie]) ||
$_COOKIE[$cookie] !== $value) {
1888 // set cookie with new value
1889 /* Calculate cookie validity */
1890 if ($validity === null) {
1891 $validity = time() +
2592000;
1892 } elseif ($validity == 0) {
1895 $validity = time() +
$validity;
1897 if (defined('TESTSUITE')) {
1898 $_COOKIE[$cookie] = $value;
1905 $this->getCookiePath(),
1912 // cookie has already $value as value
1919 * Error handler to catch fatal errors when loading configuration
1924 function PMA_Config_fatalErrorHandler()
1926 if (isset($GLOBALS['pma_config_loading']) && $GLOBALS['pma_config_loading']) {
1927 $error = error_get_last();
1928 if ($error !== null) {
1931 'Failed to load phpMyAdmin configuration (%s:%s): %s',
1932 PMA_Error
::relPath($error['file']),
1941 if (!defined('TESTSUITE')) {
1942 register_shutdown_function('PMA_Config_fatalErrorHandler');