2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Configuration handling.
8 if (! defined('PHPMYADMIN')) {
13 * Load vendor configuration.
15 require_once './libraries/vendor_config.php';
25 * @var string default config source
27 var $default_source = './libraries/config.default.php';
30 * @var array default configuration settings
32 var $default = array();
35 * @var array configuration settings
37 var $settings = array();
40 * @var string config source
45 * @var int source modification time
47 var $source_mtime = 0;
48 var $default_source_mtime = 0;
54 var $error_config_file = false;
59 var $error_config_default_file = false;
64 var $error_pma_uri = false;
69 var $default_server = array();
72 * @var boolean whether init is done or not
73 * set this to false to force some initial checks
74 * like checking for required functions
81 * @param string $source source to read config from
83 function __construct($source = null)
85 $this->settings
= array();
87 // functions need to refresh in case of config file changed goes in
91 // other settings, independent from config file, comes in
98 * sets system and application settings
102 function checkSystem()
104 $this->set('PMA_VERSION', '4.1-dev');
108 $this->set('PMA_THEME_VERSION', 2);
112 $this->set('PMA_THEME_GENERATION', 2);
114 $this->checkPhpVersion();
115 $this->checkWebServerOs();
116 $this->checkWebServer();
118 $this->checkClient();
119 $this->checkUpload();
120 $this->checkUploadSize();
121 $this->checkOutputCompression();
125 * whether to use gzip output compression or not
129 function checkOutputCompression()
131 // If zlib output compression is set in the php configuration file, no
132 // output buffering should be run
133 if (@ini_get
('zlib.output_compression')) {
134 $this->set('OBGzip', false);
137 // disable output-buffering (if set to 'auto') for IE6, else enable it.
138 if (strtolower($this->get('OBGzip')) == 'auto') {
139 if ($this->get('PMA_USR_BROWSER_AGENT') == 'IE'
140 && $this->get('PMA_USR_BROWSER_VER') >= 6
141 && $this->get('PMA_USR_BROWSER_VER') < 7
143 $this->set('OBGzip', false);
145 $this->set('OBGzip', true);
151 * Determines platform (OS), browser and version of the user
152 * Based on a phpBuilder article:
154 * @see http://www.phpbuilder.net/columns/tim20000821.php
158 function checkClient()
160 if (PMA_getenv('HTTP_USER_AGENT')) {
161 $HTTP_USER_AGENT = PMA_getenv('HTTP_USER_AGENT');
163 $HTTP_USER_AGENT = '';
167 if (strstr($HTTP_USER_AGENT, 'Win')) {
168 $this->set('PMA_USR_OS', 'Win');
169 } elseif (strstr($HTTP_USER_AGENT, 'Mac')) {
170 $this->set('PMA_USR_OS', 'Mac');
171 } elseif (strstr($HTTP_USER_AGENT, 'Linux')) {
172 $this->set('PMA_USR_OS', 'Linux');
173 } elseif (strstr($HTTP_USER_AGENT, 'Unix')) {
174 $this->set('PMA_USR_OS', 'Unix');
175 } elseif (strstr($HTTP_USER_AGENT, 'OS/2')) {
176 $this->set('PMA_USR_OS', 'OS/2');
178 $this->set('PMA_USR_OS', 'Other');
181 // 2. browser and version
182 // (must check everything else before Mozilla)
184 $is_mozilla = preg_match(
185 '@Mozilla/([0-9].[0-9]{1,2})@',
191 '@Opera(/| )([0-9].[0-9]{1,2})@',
195 $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
196 $this->set('PMA_USR_BROWSER_AGENT', 'OPERA');
197 } elseif (preg_match(
198 '@(MS)?IE ([0-9]{1,2}.[0-9]{1,2})@',
202 $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
203 $this->set('PMA_USR_BROWSER_AGENT', 'IE');
204 } elseif (preg_match(
205 '@OmniWeb/([0-9].[0-9]{1,2})@',
209 $this->set('PMA_USR_BROWSER_VER', $log_version[1]);
210 $this->set('PMA_USR_BROWSER_AGENT', 'OMNIWEB');
211 // Konqueror 2.2.2 says Konqueror/2.2.2
212 // Konqueror 3.0.3 says Konqueror/3
213 } elseif (preg_match(
214 '@(Konqueror/)(.*)(;)@',
218 $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
219 $this->set('PMA_USR_BROWSER_AGENT', 'KONQUEROR');
220 // must check Chrome before Safari
221 } elseif ($is_mozilla
222 && preg_match('@Chrome/([0-9.]*)@', $HTTP_USER_AGENT, $log_version)
224 $this->set('PMA_USR_BROWSER_VER', $log_version[1]);
225 $this->set('PMA_USR_BROWSER_AGENT', 'CHROME');
227 } elseif ($is_mozilla
228 && preg_match('@Version/(.*) Safari@', $HTTP_USER_AGENT, $log_version)
231 'PMA_USR_BROWSER_VER', $log_version[1]
233 $this->set('PMA_USR_BROWSER_AGENT', 'SAFARI');
235 } elseif ($is_mozilla
236 && preg_match('@Safari/([0-9]*)@', $HTTP_USER_AGENT, $log_version)
239 'PMA_USR_BROWSER_VER', $mozilla_version[1] . '.' . $log_version[1]
241 $this->set('PMA_USR_BROWSER_AGENT', 'SAFARI');
243 } elseif (! strstr($HTTP_USER_AGENT, 'compatible')
244 && preg_match('@Firefox/([\w.]+)@', $HTTP_USER_AGENT, $log_version)
247 'PMA_USR_BROWSER_VER', $log_version[1]
249 $this->set('PMA_USR_BROWSER_AGENT', 'FIREFOX');
250 } elseif (preg_match('@rv:1.9(.*)Gecko@', $HTTP_USER_AGENT)) {
251 $this->set('PMA_USR_BROWSER_VER', '1.9');
252 $this->set('PMA_USR_BROWSER_AGENT', 'GECKO');
253 } elseif ($is_mozilla) {
254 $this->set('PMA_USR_BROWSER_VER', $mozilla_version[1]);
255 $this->set('PMA_USR_BROWSER_AGENT', 'MOZILLA');
257 $this->set('PMA_USR_BROWSER_VER', 0);
258 $this->set('PMA_USR_BROWSER_AGENT', 'OTHER');
263 * Whether GD2 is present
269 if ($this->get('GD2Available') == 'yes') {
270 $this->set('PMA_IS_GD2', 1);
271 } elseif ($this->get('GD2Available') == 'no') {
272 $this->set('PMA_IS_GD2', 0);
274 if (!@function_exists
('imagecreatetruecolor')) {
275 $this->set('PMA_IS_GD2', 0);
277 if (@function_exists
('gd_info')) {
279 if (strstr($gd_nfo["GD Version"], '2.')) {
280 $this->set('PMA_IS_GD2', 1);
282 $this->set('PMA_IS_GD2', 0);
285 $this->set('PMA_IS_GD2', 0);
292 * Whether the Web server php is running on is IIS
296 function checkWebServer()
298 // some versions return Microsoft-IIS, some Microsoft/IIS
299 // we could use a preg_match() but it's slower
300 if (PMA_getenv('SERVER_SOFTWARE')
301 && stristr(PMA_getenv('SERVER_SOFTWARE'), 'Microsoft')
302 && stristr(PMA_getenv('SERVER_SOFTWARE'), 'IIS')
304 $this->set('PMA_IS_IIS', 1);
306 $this->set('PMA_IS_IIS', 0);
311 * Whether the os php is running on is windows or not
315 function checkWebServerOs()
317 // Default to Unix or Equiv
318 $this->set('PMA_IS_WINDOWS', 0);
319 // If PHP_OS is defined then continue
320 if (defined('PHP_OS')) {
321 if (stristr(PHP_OS
, 'win') && !stristr(PHP_OS
, 'darwin')) {
322 // Is it some version of Windows
323 $this->set('PMA_IS_WINDOWS', 1);
324 } elseif (stristr(PHP_OS
, 'OS/2')) {
325 // Is it OS/2 (No file permissions like Windows)
326 $this->set('PMA_IS_WINDOWS', 1);
332 * detects PHP version
336 function checkPhpVersion()
340 '@([0-9]{1,2}).([0-9]{1,2}).([0-9]{1,2})@',
345 '@([0-9]{1,2}).([0-9]{1,2})@',
350 if (isset($match) && ! empty($match[1])) {
351 if (! isset($match[2])) {
354 if (! isset($match[3])) {
358 'PMA_PHP_INT_VERSION',
359 (int) sprintf('%d%02d%02d', $match[1], $match[2], $match[3])
362 $this->set('PMA_PHP_INT_VERSION', 0);
364 $this->set('PMA_PHP_STR_VERSION', phpversion());
368 * detects if Git revision
372 function isGitRevision()
375 if (isset($_SESSION['is_git_revision'])) {
376 if ($_SESSION['is_git_revision']) {
377 $this->set('PMA_VERSION_GIT', 1);
379 return $_SESSION['is_git_revision'];
381 // find out if there is a .git folder
382 $git_folder = '.git';
383 if (! @file_exists
($git_folder)
384 ||
! @file_exists
($git_folder . '/config')
386 $_SESSION['is_git_revision'] = false;
389 $_SESSION['is_git_revision'] = true;
394 * detects Git revision, if running inside repo
398 function checkGitRevision()
400 // find out if there is a .git folder
401 $git_folder = '.git';
402 if (! $this->isGitRevision()) {
406 if (! $ref_head = @file_get_contents
($git_folder . '/HEAD')) {
410 // are we on any branch?
411 if (strstr($ref_head, '/')) {
412 $ref_head = substr(trim($ref_head), 5);
413 if (substr($ref_head, 0, 11) === 'refs/heads/') {
414 $branch = substr($ref_head, 11);
416 $branch = basename($ref_head);
419 $ref_file = $git_folder . '/' . $ref_head;
420 if (@file_exists
($ref_file)) {
421 if (! $hash = @file_get_contents
($ref_file)) {
426 // deal with packed refs
427 if (! $packed_refs = @file_get_contents
($git_folder . '/packed-refs')) {
430 // split file to lines
431 $ref_lines = explode("\n", $packed_refs);
432 foreach ($ref_lines as $line) {
434 if ($line[0] == '#') {
438 $parts = explode(' ', $line);
439 // care only about named refs
440 if (count($parts) != 2) {
443 // have found our ref?
444 if ($parts[1] == $ref_head) {
449 if (! isset($hash)) {
450 // Could not find ref
455 $hash = trim($ref_head);
459 if ( !isset($_SESSION['PMA_VERSION_COMMITDATA_' . $hash])) {
460 $git_file_name = $git_folder . '/objects/' . substr($hash, 0, 2)
461 . '/' . substr($hash, 2);
462 if (file_exists($git_file_name) ) {
463 if (! $commit = @file_get_contents
($git_file_name)) {
466 $commit = explode("\0", gzuncompress($commit), 2);
467 $commit = explode("\n", $commit[1]);
468 $_SESSION['PMA_VERSION_COMMITDATA_' . $hash] = $commit;
470 $pack_names = array();
471 // work with packed data
472 if ($packs = @file_get_contents
($git_folder . '/objects/info/packs')) {
473 // File exists. Read it, parse the file to get the names of the
474 // packs. (to look for them in .git/object/pack directory later)
475 foreach (explode("\n", $packs) as $line) {
477 if (strlen(trim($line)) == 0) {
480 // skip non pack lines
481 if ($line[0] != 'P') {
485 $pack_names[] = substr($line, 2);
488 // '.git/objects/info/packs' file can be missing
489 // (atlease in mysGit)
490 // File missing. May be we can look in the .git/object/pack
491 // directory for all the .pack files and use that list of
493 $it = new DirectoryIterator($git_folder . '/objects/pack');
494 foreach ($it as $file_info) {
495 $file_name = $file_info->getFilename();
496 // if this is a .pack file
497 if ($file_info->isFile()
498 && substr($file_name, -5) == '.pack'
500 $pack_names[] = $file_name;
504 $hash = strtolower($hash);
505 foreach ($pack_names as $pack_name) {
506 $index_name = str_replace('.pack', '.idx', $pack_name);
509 if (! $index_data = @file_get_contents
($git_folder . '/objects/pack/' . $index_name)) {
513 if (substr($index_data, 0, 4) != "\377tOc") {
517 $version = unpack('N', substr($index_data, 4, 4));
518 if ($version[1] != 2) {
521 // parse fanout table
522 $fanout = unpack("N*", substr($index_data, 8, 256 * 4));
524 // find where we should search
525 $firstbyte = intval(substr($hash, 0, 2), 16);
526 // array is indexed from 1 and we need to get
527 // previous entry for start
528 if ($firstbyte == 0) {
531 $start = $fanout[$firstbyte];
533 $end = $fanout[$firstbyte +
1];
535 // stupid linear search for our sha
538 $offset = 8 +
(256 * 4);
539 for ($position = $start; $position < $end; $position++
) {
543 $index_data, $offset +
($position * 20), 20
556 $offset = 8 +
(256 * 4) +
(24 * $fanout[256]);
557 $pack_offset = unpack(
558 'N', substr($index_data, $offset +
($position * 4), 4)
560 $pack_offset = $pack_offset[1];
564 $git_folder . '/objects/pack/' . $pack_name, 'rb'
566 if ($pack_file === false) {
570 fseek($pack_file, $pack_offset);
573 $header = ord(fread($pack_file, 1));
574 $type = ($header >> 4) & 7;
575 $hasnext = ($header & 128) >> 7;
576 $size = $header & 0xf;
580 $byte = ord(fread($pack_file, 1));
581 $size |
= ($byte & 0x7f) << $offset;
582 $hasnext = ($byte & 128) >> 7;
586 // we care only about commit objects
592 $commit = fread($pack_file, $size);
593 $commit = gzuncompress($commit);
594 $commit = explode("\n", $commit);
595 $_SESSION['PMA_VERSION_COMMITDATA_' . $hash] = $commit;
600 $commit = $_SESSION['PMA_VERSION_COMMITDATA_' . $hash];
603 // check if commit exists in Github
604 $is_remote_commit = false;
605 if ($commit !== false
606 && isset($_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash])
608 $is_remote_commit = $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash];
610 $link = 'https://api.github.com/repos/phpmyadmin/phpmyadmin/git/commits/'
612 $is_found = $this->checkHTTP($link, ! $commit);
615 $is_remote_commit = false;
616 $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = false;
619 // no remote link for now, but don't cache this as Github is down
620 $is_remote_commit = false;
623 $is_remote_commit = true;
624 $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = true;
625 if ($commit === false) {
626 // if no local commit data, try loading from Github
627 $commit_json = json_decode($is_found);
633 $is_remote_branch = false;
634 if ($is_remote_commit && $branch !== false) {
635 // check if branch exists in Github
636 if (isset($_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash])) {
637 $is_remote_branch = $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash];
639 $link = 'https://api.github.com/repos/phpmyadmin/phpmyadmin'
640 . '/git/trees/' . $branch;
641 $is_found = $this->checkHTTP($link);
644 $is_remote_branch = true;
645 $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = true;
648 $is_remote_branch = false;
649 $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = false;
652 // no remote link for now, but don't cache this as Github is down
653 $is_remote_branch = false;
659 if ($commit !== false) {
660 $author = array('name' => '', 'email' => '', 'date' => '');
661 $committer = array('name' => '', 'email' => '', 'date' => '');
664 $dataline = array_shift($commit);
665 $datalinearr = explode(' ', $dataline, 2);
666 $linetype = $datalinearr[0];
667 if (in_array($linetype, array('author', 'committer'))) {
668 $user = $datalinearr[1];
669 preg_match('/([^<]+)<([^>]+)> ([0-9]+)( [^ ]+)?/', $user, $user);
671 'name' => trim($user[1]),
672 'email' => trim($user[2]),
673 'date' => date('Y-m-d H:i:s', $user[3]));
674 if (isset($user[4])) {
675 $user2['date'] .= $user[4];
679 } while ($dataline != '');
680 $message = trim(implode(' ', $commit));
682 } elseif (isset($commit_json)) {
684 'name' => $commit_json->author
->name
,
685 'email' => $commit_json->author
->email
,
686 'date' => $commit_json->author
->date
);
688 'name' => $commit_json->committer
->name
,
689 'email' => $commit_json->committer
->email
,
690 'date' => $commit_json->committer
->date
);
691 $message = trim($commit_json->message
);
696 $this->set('PMA_VERSION_GIT', 1);
697 $this->set('PMA_VERSION_GIT_COMMITHASH', $hash);
698 $this->set('PMA_VERSION_GIT_BRANCH', $branch);
699 $this->set('PMA_VERSION_GIT_MESSAGE', $message);
700 $this->set('PMA_VERSION_GIT_AUTHOR', $author);
701 $this->set('PMA_VERSION_GIT_COMMITTER', $committer);
702 $this->set('PMA_VERSION_GIT_ISREMOTECOMMIT', $is_remote_commit);
703 $this->set('PMA_VERSION_GIT_ISREMOTEBRANCH', $is_remote_branch);
707 * Checks if given URL is 200 or 404, optionally returns data
709 * @param mixed $link curl link
710 * @param boolean $get_body whether to retrieve body of document
712 * @return test result or data
714 function checkHTTP($link, $get_body = false)
716 if (! function_exists('curl_init')) {
719 $ch = curl_init($link);
720 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
, 0);
721 curl_setopt($ch, CURLOPT_HEADER
, 1);
722 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
723 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST
, 0);
724 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER
, 0);
725 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT
, 5);
726 curl_setopt($ch, CURLOPT_USERAGENT
, 'phpMyAdmin/' . PMA_VERSION
);
727 curl_setopt($ch, CURLOPT_TIMEOUT
, 5);
728 $data = @curl_exec
($ch);
729 if ($data === false) {
732 $ok = 'HTTP/1.1 200 OK';
733 $notfound = 'HTTP/1.1 404 Not Found';
734 if (substr($data, 0, strlen($ok)) === $ok) {
735 return $get_body ?
substr($data, strpos($data, "\r\n\r\n") +
4) : true;
736 } elseif (substr($data, 0, strlen($notfound)) === $notfound) {
743 * loads default values from default source
745 * @return boolean success
747 function loadDefaults()
750 if (! file_exists($this->default_source
)) {
751 $this->error_config_default_file
= true;
754 include $this->default_source
;
756 $this->default_source_mtime
= filemtime($this->default_source
);
758 $this->default_server
= $cfg['Servers'][1];
759 unset($cfg['Servers']);
761 $this->default = $cfg;
762 $this->settings
= PMA_arrayMergeRecursive($this->settings
, $cfg);
764 $this->error_config_default_file
= false;
770 * loads configuration from $source, usally the config file
771 * should be called on object creation
773 * @param string $source config file
777 function load($source = null)
779 $this->loadDefaults();
781 if (null !== $source) {
782 $this->setSource($source);
785 if (! $this->checkConfigSource()) {
792 * Parses the configuration file, the eval is used here to avoid
793 * problems with trailing whitespace, what is often a problem.
795 $old_error_reporting = error_reporting(0);
796 $eval_result = eval('?' . '>' . trim(file_get_contents($this->getSource())));
797 error_reporting($old_error_reporting);
799 if ($eval_result === false) {
800 $this->error_config_file
= true;
802 $this->error_config_file
= false;
803 $this->source_mtime
= filemtime($this->getSource());
807 * Backward compatibility code
809 if (!empty($cfg['DefaultTabTable'])) {
810 $cfg['DefaultTabTable'] = str_replace(
814 'tbl_properties.php',
816 $cfg['DefaultTabTable']
820 if (!empty($cfg['DefaultTabDatabase'])) {
821 $cfg['DefaultTabDatabase'] = str_replace(
827 $cfg['DefaultTabDatabase']
832 $this->settings
= PMA_arrayMergeRecursive($this->settings
, $cfg);
833 $this->checkPmaAbsoluteUri();
834 $this->checkFontsize();
836 // Handling of the collation must be done after merging of $cfg
837 // (from config.inc.php) so that $cfg['DefaultConnectionCollation']
838 // can have an effect. Note that the presence of collation
839 // information in a cookie has priority over what is defined
840 // in the default or user's config files.
842 * @todo check validity of $_COOKIE['pma_collation_connection']
844 if (! empty($_COOKIE['pma_collation_connection'])) {
846 'collation_connection',
847 strip_tags($_COOKIE['pma_collation_connection'])
851 'collation_connection',
852 $this->get('DefaultConnectionCollation')
855 // Now, a collation information could come from REQUEST
856 // (an example of this: the collation selector in index.php)
857 // so the following handles the setting of collation_connection
858 // and later, in common.inc.php, the cookie will be set
859 // according to this.
860 $this->checkCollationConnection();
866 * Loads user preferences and merges them with current config
867 * must be called after control connection has been estabilished
871 function loadUserPreferences()
873 // index.php should load these settings, so that phpmyadmin.css.php
874 // will have everything avaiable in session cache
875 $server = isset($GLOBALS['server'])
877 : (!empty($GLOBALS['cfg']['ServerDefault'])
878 ?
$GLOBALS['cfg']['ServerDefault']
880 $cache_key = 'server_' . $server;
881 if ($server > 0 && !defined('PMA_MINIMUM_COMMON')) {
882 $config_mtime = max($this->default_source_mtime
, $this->source_mtime
);
883 // cache user preferences, use database only when needed
884 if (! isset($_SESSION['cache'][$cache_key]['userprefs'])
885 ||
$_SESSION['cache'][$cache_key]['config_mtime'] < $config_mtime
887 // load required libraries
888 include_once './libraries/user_preferences.lib.php';
889 $prefs = PMA_loadUserprefs();
890 $_SESSION['cache'][$cache_key]['userprefs']
891 = PMA_applyUserprefs($prefs['config_data']);
892 $_SESSION['cache'][$cache_key]['userprefs_mtime'] = $prefs['mtime'];
893 $_SESSION['cache'][$cache_key]['userprefs_type'] = $prefs['type'];
894 $_SESSION['cache'][$cache_key]['config_mtime'] = $config_mtime;
896 } elseif ($server == 0
897 ||
! isset($_SESSION['cache'][$cache_key]['userprefs'])
899 $this->set('user_preferences', false);
902 $config_data = $_SESSION['cache'][$cache_key]['userprefs'];
903 // type is 'db' or 'session'
906 $_SESSION['cache'][$cache_key]['userprefs_type']
909 'user_preferences_mtime',
910 $_SESSION['cache'][$cache_key]['userprefs_mtime']
913 // backup some settings
915 if (isset($this->settings
['fontsize'])) {
916 $org_fontsize = $this->settings
['fontsize'];
919 $this->settings
= PMA_arrayMergeRecursive($this->settings
, $config_data);
920 $GLOBALS['cfg'] = PMA_arrayMergeRecursive($GLOBALS['cfg'], $config_data);
921 if (defined('PMA_MINIMUM_COMMON')) {
925 // settings below start really working on next page load, but
926 // changes are made only in index.php so everything is set when
930 $tmanager = $_SESSION['PMA_Theme_Manager'];
931 if ($tmanager->getThemeCookie() ||
isset($_REQUEST['set_theme'])) {
932 if ((! isset($config_data['ThemeDefault'])
933 && $tmanager->theme
->getId() != 'original')
934 ||
isset($config_data['ThemeDefault'])
935 && $config_data['ThemeDefault'] != $tmanager->theme
->getId()
937 // new theme was set in common.inc.php
941 $tmanager->theme
->getId(),
946 // no cookie - read default from settings
947 if ($this->settings
['ThemeDefault'] != $tmanager->theme
->getId()
948 && $tmanager->checkTheme($this->settings
['ThemeDefault'])
950 $tmanager->setActiveTheme($this->settings
['ThemeDefault']);
951 $tmanager->setThemeCookie();
956 if ((! isset($config_data['fontsize'])
957 && $org_fontsize != '82%')
958 ||
isset($config_data['fontsize'])
959 && $org_fontsize != $config_data['fontsize']
961 $this->setUserValue(null, 'fontsize', $org_fontsize, '82%');
965 if (isset($_COOKIE['pma_lang']) ||
isset($_POST['lang'])) {
966 if ((! isset($config_data['lang'])
967 && $GLOBALS['lang'] != 'en')
968 ||
isset($config_data['lang'])
969 && $GLOBALS['lang'] != $config_data['lang']
971 $this->setUserValue(null, 'lang', $GLOBALS['lang'], 'en');
974 // read language from settings
975 if (isset($config_data['lang']) && PMA_langSet($config_data['lang'])) {
976 $this->setCookie('pma_lang', $GLOBALS['lang']);
980 // save connection collation
981 if (isset($_COOKIE['pma_collation_connection'])
982 ||
isset($_POST['collation_connection'])
984 if ((! isset($config_data['collation_connection'])
985 && $GLOBALS['collation_connection'] != 'utf8_general_ci')
986 ||
isset($config_data['collation_connection'])
987 && $GLOBALS['collation_connection'] != $config_data['collation_connection']
991 'collation_connection',
992 $GLOBALS['collation_connection'],
997 // read collation from settings
998 if (isset($config_data['collation_connection'])) {
999 $GLOBALS['collation_connection']
1000 = $config_data['collation_connection'];
1002 'pma_collation_connection',
1003 $GLOBALS['collation_connection']
1010 * Sets config value which is stored in user preferences (if available)
1013 * If user preferences are not yet initialized, option is applied to
1014 * global config and added to a update queue, which is processed
1015 * by {@link loadUserPreferences()}
1017 * @param string $cookie_name can be null
1018 * @param string $cfg_path configuration path
1019 * @param mixed $new_cfg_value new value
1020 * @param mixed $default_value default value
1024 function setUserValue($cookie_name, $cfg_path, $new_cfg_value,
1025 $default_value = null
1027 // use permanent user preferences if possible
1028 $prefs_type = $this->get('user_preferences');
1030 include_once './libraries/user_preferences.lib.php';
1031 if ($default_value === null) {
1032 $default_value = PMA_arrayRead($cfg_path, $this->default);
1034 PMA_persistOption($cfg_path, $new_cfg_value, $default_value);
1036 if ($prefs_type != 'db' && $cookie_name) {
1037 // fall back to cookies
1038 if ($default_value === null) {
1039 $default_value = PMA_arrayRead($cfg_path, $this->settings
);
1041 $this->setCookie($cookie_name, $new_cfg_value, $default_value);
1043 PMA_arrayWrite($cfg_path, $GLOBALS['cfg'], $new_cfg_value);
1044 PMA_arrayWrite($cfg_path, $this->settings
, $new_cfg_value);
1048 * Reads value stored by {@link setUserValue()}
1050 * @param string $cookie_name cookie name
1051 * @param mixed $cfg_value config value
1055 function getUserValue($cookie_name, $cfg_value)
1057 $cookie_exists = isset($_COOKIE) && !empty($_COOKIE[$cookie_name]);
1058 $prefs_type = $this->get('user_preferences');
1059 if ($prefs_type == 'db') {
1060 // permanent user preferences value exists, remove cookie
1061 if ($cookie_exists) {
1062 $this->removeCookie($cookie_name);
1064 } else if ($cookie_exists) {
1065 return $_COOKIE[$cookie_name];
1067 // return value from $cfg array
1074 * @param string $source source
1078 function setSource($source)
1080 $this->source
= trim($source);
1084 * check config source
1086 * @return boolean whether source is valid or not
1088 function checkConfigSource()
1090 if (! $this->getSource()) {
1091 // no configuration file set at all
1095 if (! file_exists($this->getSource())) {
1096 $this->source_mtime
= 0;
1100 if (! is_readable($this->getSource())) {
1101 // manually check if file is readable
1102 // might be bug #3059806 Supporting running from CIFS/Samba shares
1105 $handle = @fopen
($this->getSource(), 'r');
1106 if ($handle !== false) {
1107 $contents = @fread
($handle, 1); // reading 1 byte is enough to test
1110 if ($contents === false) {
1111 $this->source_mtime
= 0;
1114 function_exists('__')
1115 ?
__('Existing configuration file (%s) is not readable.')
1116 : 'Existing configuration file (%s) is not readable.',
1128 * verifies the permissions on config file (if asked by configuration)
1129 * (must be called after config.inc.php has been merged)
1133 function checkPermissions()
1135 // Check for permissions (on platforms that support it):
1136 if ($this->get('CheckConfigurationPermissions')) {
1137 $perms = @fileperms
($this->getSource());
1138 if (!($perms === false) && ($perms & 2)) {
1139 // This check is normally done after loading configuration
1140 $this->checkWebServerOs();
1141 if ($this->get('PMA_IS_WINDOWS') == 0) {
1142 $this->source_mtime
= 0;
1143 /* Gettext is possibly still not loaded */
1144 if (function_exists('__')) {
1145 $msg = __('Wrong permissions on configuration file, should not be world writable!');
1147 $msg = 'Wrong permissions on configuration file, should not be world writable!';
1149 PMA_fatalError($msg);
1156 * returns specific config setting
1158 * @param string $setting config setting
1160 * @return mixed value
1162 function get($setting)
1164 if (isset($this->settings
[$setting])) {
1165 return $this->settings
[$setting];
1171 * sets configuration variable
1173 * @param string $setting configuration option
1174 * @param mixed $value new value for configuration option
1178 function set($setting, $value)
1180 if (! isset($this->settings
[$setting])
1181 ||
$this->settings
[$setting] !== $value
1183 $this->settings
[$setting] = $value;
1184 $this->set_mtime
= time();
1189 * returns source for current config
1191 * @return string config source
1193 function getSource()
1195 return $this->source
;
1199 * returns a unique value to force a CSS reload if either the config
1200 * or the theme changes
1201 * must also check the pma_fontsize cookie in case there is no
1204 * @return int Summary of unix timestamps and fontsize,
1205 * to be unique on theme parameters change
1207 function getThemeUniqueValue()
1209 if (null !== $this->get('fontsize')) {
1210 $fontsize = intval($this->get('fontsize'));
1211 } elseif (isset($_COOKIE['pma_fontsize'])) {
1212 $fontsize = intval($_COOKIE['pma_fontsize']);
1218 $this->source_mtime +
1219 $this->default_source_mtime +
1220 $this->get('user_preferences_mtime') +
1221 $_SESSION['PMA_Theme']->mtime_info +
1222 $_SESSION['PMA_Theme']->filesize_info
);
1226 * $cfg['PmaAbsoluteUri'] is a required directive else cookies won't be
1227 * set properly and, depending on browsers, inserting or updating a
1232 function checkPmaAbsoluteUri()
1234 // Setup a default value to let the people and lazy sysadmins work anyway,
1235 // they'll get an error if the autodetect code doesn't work
1236 $pma_absolute_uri = $this->get('PmaAbsoluteUri');
1237 $is_https = $this->detectHttps();
1239 if (strlen($pma_absolute_uri) < 5) {
1242 // If we don't have scheme, we didn't have full URL so we need to
1244 if (empty($url['scheme'])) {
1247 $url['scheme'] = 'https';
1249 $url['scheme'] = 'http';
1253 if (PMA_getenv('HTTP_HOST')) {
1254 // Prepend the scheme before using parse_url() since this
1255 // is not part of the RFC2616 Host request-header
1256 $parsed_url = parse_url(
1257 $url['scheme'] . '://' . PMA_getenv('HTTP_HOST')
1259 if (!empty($parsed_url['host'])) {
1262 $url['host'] = PMA_getenv('HTTP_HOST');
1264 } elseif (PMA_getenv('SERVER_NAME')) {
1265 $url['host'] = PMA_getenv('SERVER_NAME');
1267 $this->error_pma_uri
= true;
1271 // If we didn't set port yet...
1272 if (empty($url['port']) && PMA_getenv('SERVER_PORT')) {
1273 $url['port'] = PMA_getenv('SERVER_PORT');
1276 // And finally the path could be already set from REQUEST_URI
1277 if (empty($url['path'])) {
1278 $path = parse_url($GLOBALS['PMA_PHP_SELF']);
1279 $url['path'] = $path['path'];
1283 // Make url from parts we have
1284 $pma_absolute_uri = $url['scheme'] . '://';
1285 // Was there user information?
1286 if (!empty($url['user'])) {
1287 $pma_absolute_uri .= $url['user'];
1288 if (!empty($url['pass'])) {
1289 $pma_absolute_uri .= ':' . $url['pass'];
1291 $pma_absolute_uri .= '@';
1294 $pma_absolute_uri .= $url['host'];
1295 // Add port, if it not the default one
1296 if (! empty($url['port'])
1297 && (($url['scheme'] == 'http' && $url['port'] != 80)
1298 ||
($url['scheme'] == 'https' && $url['port'] != 443))
1300 $pma_absolute_uri .= ':' . $url['port'];
1302 // And finally path, without script name, the 'a' is there not to
1303 // strip our directory, when path is only /pmadir/ without filename.
1304 // Backslashes returned by Windows have to be changed.
1305 // Only replace backslashes by forward slashes if on Windows,
1306 // as the backslash could be valid on a non-Windows system.
1307 $this->checkWebServerOs();
1308 if ($this->get('PMA_IS_WINDOWS') == 1) {
1309 $path = str_replace("\\", "/", dirname($url['path'] . 'a'));
1311 $path = dirname($url['path'] . 'a');
1314 // To work correctly within transformations overview:
1315 if (defined('PMA_PATH_TO_BASEDIR') && PMA_PATH_TO_BASEDIR
== '../../') {
1316 if ($this->get('PMA_IS_WINDOWS') == 1) {
1317 $path = str_replace("\\", "/", dirname(dirname($path)));
1319 $path = dirname(dirname($path));
1323 // PHP's dirname function would have returned a dot
1324 // when $path contains no slash
1328 // in vhost situations, there could be already an ending slash
1329 if (substr($path, -1) != '/') {
1332 $pma_absolute_uri .= $path;
1334 // We used to display a warning if PmaAbsoluteUri wasn't set, but now
1335 // the autodetect code works well enough that we don't display the
1336 // warning at all. The user can still set PmaAbsoluteUri manually.
1339 // The URI is specified, however users do often specify this
1340 // wrongly, so we try to fix this.
1342 // Adds a trailing slash et the end of the phpMyAdmin uri if it
1344 if (substr($pma_absolute_uri, -1) != '/') {
1345 $pma_absolute_uri .= '/';
1348 // If URI doesn't start with http:// or https://, we will add
1350 if (substr($pma_absolute_uri, 0, 7) != 'http://'
1351 && substr($pma_absolute_uri, 0, 8) != 'https://'
1354 = ($is_https ?
'https' : 'http')
1355 . ':' . (substr($pma_absolute_uri, 0, 2) == '//' ?
'' : '//')
1356 . $pma_absolute_uri;
1359 $this->set('PmaAbsoluteUri', $pma_absolute_uri);
1363 * Converts currently used PmaAbsoluteUri to SSL based variant.
1365 * @return String witch adjusted URI
1367 function getSSLUri()
1370 $url = $this->get('PmaAbsoluteUri');
1371 // Parse current URL
1372 $parsed = parse_url($url);
1373 // In case parsing has failed do stupid string replacement
1374 if ($parsed === false) {
1375 // Replace http protocol
1376 return preg_replace('@^http:@', 'https:', $url);
1379 // Reconstruct URL using parsed parts
1380 if ($this->get('SSLPort')) {
1381 $port_number = $this->get('SSLPort');
1385 return 'https://' . $parsed['host'] . ':' . $port_number . $parsed['path'];
1389 * check selected collation_connection
1391 * @todo check validity of $_REQUEST['collation_connection']
1395 function checkCollationConnection()
1397 if (! empty($_REQUEST['collation_connection'])) {
1399 'collation_connection',
1400 strip_tags($_REQUEST['collation_connection'])
1406 * checks for font size configuration, and sets font size as requested by user
1410 function checkFontsize()
1414 if (isset($_GET['set_fontsize'])) {
1415 $new_fontsize = $_GET['set_fontsize'];
1416 } elseif (isset($_POST['set_fontsize'])) {
1417 $new_fontsize = $_POST['set_fontsize'];
1418 } elseif (isset($_COOKIE['pma_fontsize'])) {
1419 $new_fontsize = $_COOKIE['pma_fontsize'];
1422 if (preg_match('/^[0-9.]+(px|em|pt|\%)$/', $new_fontsize)) {
1423 $this->set('fontsize', $new_fontsize);
1424 } elseif (! $this->get('fontsize')) {
1425 // 80% would correspond to the default browser font size
1426 // of 16, but use 82% to help read the monoface font
1427 $this->set('fontsize', '82%');
1430 $this->setCookie('pma_fontsize', $this->get('fontsize'), '82%');
1434 * checks if upload is enabled
1438 function checkUpload()
1440 if (ini_get('file_uploads')) {
1441 $this->set('enable_upload', true);
1442 // if set "php_admin_value file_uploads Off" in httpd.conf
1443 // ini_get() also returns the string "Off" in this case:
1444 if ('off' == strtolower(ini_get('file_uploads'))) {
1445 $this->set('enable_upload', false);
1448 $this->set('enable_upload', false);
1453 * Maximum upload size as limited by PHP
1454 * Used with permission from Moodle (http://moodle.org) by Martin Dougiamas
1456 * this section generates $max_upload_size in bytes
1460 function checkUploadSize()
1462 if (! $filesize = ini_get('upload_max_filesize')) {
1466 if ($postsize = ini_get('post_max_size')) {
1469 min(PMA_getRealSize($filesize), PMA_getRealSize($postsize))
1472 $this->set('max_upload_size', PMA_getRealSize($filesize));
1477 * Checks if protocol is https
1479 * This function checks if the https protocol is used in the PmaAbsoluteUri
1480 * configuration setting, as opposed to detectHttps() which checks if the
1481 * https protocol is used on the active connection.
1485 public function isHttps()
1488 if (null !== $this->get('is_https')) {
1489 return $this->get('is_https');
1492 $url = parse_url($this->get('PmaAbsoluteUri'));
1495 if (isset($url['scheme']) && $url['scheme'] == 'https') {
1501 $this->set('is_https', $is_https);
1507 * Detects whether https appears to be used.
1509 * This function checks if the https protocol is used in the current connection
1510 * with the webserver, based on environment variables.
1511 * Please note that this just detects what we see, so
1512 * it completely ignores things like reverse proxies.
1516 function detectHttps()
1522 // At first we try to parse REQUEST_URI, it might contain full URL,
1523 if (PMA_getenv('REQUEST_URI')) {
1524 // produces E_WARNING if it cannot get parsed, e.g. '/foobar:/'
1525 $url = @parse_url
(PMA_getenv('REQUEST_URI'));
1526 if ($url === false) {
1531 // If we don't have scheme, we didn't have full URL so we need to
1533 if (empty($url['scheme'])) {
1535 if (PMA_getenv('HTTP_SCHEME')) {
1536 $url['scheme'] = PMA_getenv('HTTP_SCHEME');
1537 } elseif (PMA_getenv('HTTPS')
1538 && strtolower(PMA_getenv('HTTPS')) == 'on'
1540 $url['scheme'] = 'https';
1541 } elseif (PMA_getenv('HTTP_X_FORWARDED_PROTO')) {
1542 $url['scheme'] = strtolower(PMA_getenv('HTTP_X_FORWARDED_PROTO'));
1543 } elseif (PMA_getenv('HTTP_FRONT_END_HTTPS')
1544 && strtolower(PMA_getenv('HTTP_FRONT_END_HTTPS')) == 'on'
1546 $url['scheme'] = 'https';
1548 $url['scheme'] = 'http';
1552 if (isset($url['scheme']) && $url['scheme'] == 'https') {
1562 * detect correct cookie path
1566 function checkCookiePath()
1568 $this->set('cookie_path', $this->getCookiePath());
1576 public function getCookiePath()
1578 static $cookie_path = null;
1580 if (null !== $cookie_path && !defined('TESTSUITE')) {
1581 return $cookie_path;
1584 $parsed_url = parse_url($this->get('PmaAbsoluteUri'));
1586 $cookie_path = $parsed_url['path'];
1588 return $cookie_path;
1592 * enables backward compatibility
1598 $GLOBALS['cfg'] = $this->settings
;
1599 $GLOBALS['default_server'] = $this->default_server
;
1600 unset($this->default_server
);
1601 $GLOBALS['collation_connection'] = $this->get('collation_connection');
1602 $GLOBALS['is_upload'] = $this->get('enable_upload');
1603 $GLOBALS['max_upload_size'] = $this->get('max_upload_size');
1604 $GLOBALS['cookie_path'] = $this->get('cookie_path');
1605 $GLOBALS['is_https'] = $this->get('is_https');
1609 'PMA_THEME_VERSION',
1610 'PMA_THEME_GENERATION',
1611 'PMA_PHP_STR_VERSION',
1612 'PMA_PHP_INT_VERSION',
1617 'PMA_USR_BROWSER_VER',
1618 'PMA_USR_BROWSER_AGENT'
1621 foreach ($defines as $define) {
1622 if (! defined($define)) {
1623 define($define, $this->get($define));
1629 * returns options for font size selection
1631 * @param string $current_size current selected font size with unit
1633 * @return array selectable font sizes
1637 static protected function getFontsizeOptions($current_size = '82%')
1639 $unit = preg_replace('/[0-9.]*/', '', $current_size);
1640 $value = preg_replace('/[^0-9.]*/', '', $current_size);
1644 $options["$value"] = $value . $unit;
1646 if ($unit === '%') {
1650 } elseif ($unit === 'em') {
1654 } elseif ($unit === 'pt') {
1657 } elseif ($unit === 'px') {
1662 //unknown font size unit
1670 foreach ($factors as $key => $factor) {
1671 $option_inc = $value +
$factor;
1672 $option_dec = $value - $factor;
1673 while (count($options) < 21) {
1674 $options["$option_inc"] = $option_inc . $unit;
1675 if ($option_dec > $factors[0]) {
1676 $options["$option_dec"] = $option_dec . $unit;
1678 $option_inc +
= $factor;
1679 $option_dec -= $factor;
1680 if (isset($factors[$key +
1])
1681 && $option_inc >= $value +
$factors[$key +
1]
1692 * returns html selectbox for font sizes
1696 * @return string html selectbox
1698 static protected function getFontsizeSelection()
1700 $current_size = $GLOBALS['PMA_Config']->get('fontsize');
1701 // for the case when there is no config file (this is supported)
1702 if (empty($current_size)) {
1703 if (isset($_COOKIE['pma_fontsize'])) {
1704 $current_size = $_COOKIE['pma_fontsize'];
1706 $current_size = '82%';
1709 $options = PMA_Config
::getFontsizeOptions($current_size);
1711 $return = '<label for="select_fontsize">' . __('Font size')
1712 . ':</label>' . "\n"
1713 . '<select name="set_fontsize" id="select_fontsize"'
1714 . ' class="autosubmit">' . "\n";
1715 foreach ($options as $option) {
1716 $return .= '<option value="' . $option . '"';
1717 if ($option == $current_size) {
1718 $return .= ' selected="selected"';
1720 $return .= '>' . $option . '</option>' . "\n";
1722 $return .= '</select>';
1728 * return complete font size selection form
1732 * @return string html selectbox
1734 static public function getFontsizeForm()
1736 return '<form name="form_fontsize_selection" id="form_fontsize_selection"'
1737 . ' method="get" action="index.php" class="disableAjax">' . "\n"
1738 . PMA_generate_common_hidden_inputs() . "\n"
1739 . PMA_Config
::getFontsizeSelection() . "\n"
1746 * @param string $cookie name of cookie to remove
1748 * @return boolean result of setcookie()
1750 function removeCookie($cookie)
1752 if (defined('TESTSUITE')) {
1753 if (isset($_COOKIE[$cookie])) {
1754 unset($_COOKIE[$cookie]);
1762 $this->getCookiePath(),
1769 * sets cookie if value is different from current cookie value,
1770 * or removes if value is equal to default
1772 * @param string $cookie name of cookie to remove
1773 * @param mixed $value new cookie value
1774 * @param string $default default value
1775 * @param int $validity validity of cookie in seconds (default is one month)
1776 * @param bool $httponly whether cookie is only for HTTP (and not for scripts)
1778 * @return boolean result of setcookie()
1780 function setCookie($cookie, $value, $default = null, $validity = null,
1783 if (strlen($value) && null !== $default && $value === $default) {
1784 // default value is used
1785 if (isset($_COOKIE[$cookie])) {
1787 return $this->removeCookie($cookie);
1792 if (! strlen($value) && isset($_COOKIE[$cookie])) {
1793 // remove cookie, value is empty
1794 return $this->removeCookie($cookie);
1797 if (! isset($_COOKIE[$cookie]) ||
$_COOKIE[$cookie] !== $value) {
1798 // set cookie with new value
1799 /* Calculate cookie validity */
1800 if ($validity === null) {
1801 $validity = time() +
2592000;
1802 } elseif ($validity == 0) {
1805 $validity = time() +
$validity;
1807 if (defined('TESTSUITE')) {
1808 $_COOKIE[$cookie] = $value;
1815 $this->getCookiePath(),
1822 // cookie has already $value as value