UPDATE 4.4.0.0
[phpmyadmin.git] / libraries / Config.class.php
blob1f8b7d2cbf56ca37ecebd4933d28ab8191c5a3b9
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Configuration handling.
6 * @package PhpMyAdmin
7 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
14 * Load vendor configuration.
16 require_once './libraries/vendor_config.php';
18 /**
19 * Indication for error handler (see end of this file).
21 $GLOBALS['pma_config_loading'] = false;
23 /**
24 * Configuration class
26 * @package PhpMyAdmin
28 class PMA_Config
30 /**
31 * @var string default config source
33 var $default_source = './libraries/config.default.php';
35 /**
36 * @var array default configuration settings
38 var $default = array();
40 /**
41 * @var array configuration settings, without user preferences applied
43 var $base_settings = array();
45 /**
46 * @var array configuration settings
48 var $settings = array();
50 /**
51 * @var string config source
53 var $source = '';
55 /**
56 * @var int source modification time
58 var $source_mtime = 0;
59 var $default_source_mtime = 0;
60 var $set_mtime = 0;
62 /**
63 * @var boolean
65 var $error_config_file = false;
67 /**
68 * @var boolean
70 var $error_config_default_file = false;
72 /**
73 * @var boolean
75 var $error_pma_uri = false;
77 /**
78 * @var array
80 var $default_server = array();
82 /**
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
87 var $done = false;
89 /**
90 * constructor
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
99 // PMA_Config::load()
100 $this->load($source);
102 // other settings, independent from config file, comes in
103 $this->checkSystem();
105 $this->isHttps();
107 $this->base_settings = $this->settings;
111 * sets system and application settings
113 * @return void
115 function checkSystem()
117 $this->set('PMA_VERSION', '4.4.0');
119 * @deprecated
121 $this->set('PMA_THEME_VERSION', 2);
123 * @deprecated
125 $this->set('PMA_THEME_GENERATION', 2);
127 $this->checkPhpVersion();
128 $this->checkWebServerOs();
129 $this->checkWebServer();
130 $this->checkGd2();
131 $this->checkClient();
132 $this->checkUpload();
133 $this->checkUploadSize();
134 $this->checkOutputCompression();
138 * whether to use gzip output compression or not
140 * @return void
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);
157 } else {
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
169 * @return void
171 function checkClient()
173 if (PMA_getenv('HTTP_USER_AGENT')) {
174 $HTTP_USER_AGENT = PMA_getenv('HTTP_USER_AGENT');
175 } else {
176 $HTTP_USER_AGENT = '';
179 // 1. Platform
180 if (/*overload*/mb_strstr($HTTP_USER_AGENT, 'Win')) {
181 $this->set('PMA_USR_OS', 'Win');
182 } elseif (/*overload*/mb_strstr($HTTP_USER_AGENT, 'Mac')) {
183 $this->set('PMA_USR_OS', 'Mac');
184 } elseif (/*overload*/mb_strstr($HTTP_USER_AGENT, 'Linux')) {
185 $this->set('PMA_USR_OS', 'Linux');
186 } elseif (/*overload*/mb_strstr($HTTP_USER_AGENT, 'Unix')) {
187 $this->set('PMA_USR_OS', 'Unix');
188 } elseif (/*overload*/mb_strstr($HTTP_USER_AGENT, 'OS/2')) {
189 $this->set('PMA_USR_OS', 'OS/2');
190 } else {
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})@',
199 $HTTP_USER_AGENT,
200 $mozilla_version
203 if (preg_match(
204 '@Opera(/| )([0-9].[0-9]{1,2})@',
205 $HTTP_USER_AGENT,
206 $log_version
207 )) {
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})@',
212 $HTTP_USER_AGENT,
213 $log_version
214 )) {
215 $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
216 $this->set('PMA_USR_BROWSER_AGENT', 'IE');
217 } elseif (preg_match(
218 '@Trident/(7)\.0@',
219 $HTTP_USER_AGENT,
220 $log_version
221 )) {
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})@',
226 $HTTP_USER_AGENT,
227 $log_version
228 )) {
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/)(.*)(;)@',
235 $HTTP_USER_AGENT,
236 $log_version
237 )) {
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');
246 // newer Safari
247 } elseif ($is_mozilla
248 && preg_match('@Version/(.*) Safari@', $HTTP_USER_AGENT, $log_version)
250 $this->set(
251 'PMA_USR_BROWSER_VER', $log_version[1]
253 $this->set('PMA_USR_BROWSER_AGENT', 'SAFARI');
254 // older Safari
255 } elseif ($is_mozilla
256 && preg_match('@Safari/([0-9]*)@', $HTTP_USER_AGENT, $log_version)
258 $this->set(
259 'PMA_USR_BROWSER_VER', $mozilla_version[1] . '.' . $log_version[1]
261 $this->set('PMA_USR_BROWSER_AGENT', 'SAFARI');
262 // Firefox
263 } elseif (! /*overload*/mb_strstr($HTTP_USER_AGENT, 'compatible')
264 && preg_match('@Firefox/([\w.]+)@', $HTTP_USER_AGENT, $log_version)
266 $this->set(
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');
276 } else {
277 $this->set('PMA_USR_BROWSER_VER', 0);
278 $this->set('PMA_USR_BROWSER_AGENT', 'OTHER');
283 * Whether GD2 is present
285 * @return void
287 function checkGd2()
289 if ($this->get('GD2Available') == 'yes') {
290 $this->set('PMA_IS_GD2', 1);
291 return;
294 if ($this->get('GD2Available') == 'no') {
295 $this->set('PMA_IS_GD2', 0);
296 return;
299 if (!@function_exists('imagecreatetruecolor')) {
300 $this->set('PMA_IS_GD2', 0);
301 return;
304 if (@function_exists('gd_info')) {
305 $gd_nfo = gd_info();
306 if (/*overload*/mb_strstr($gd_nfo["GD Version"], '2.')) {
307 $this->set('PMA_IS_GD2', 1);
308 } else {
309 $this->set('PMA_IS_GD2', 0);
311 } else {
312 $this->set('PMA_IS_GD2', 0);
317 * Whether the Web server php is running on is IIS
319 * @return void
321 function checkWebServer()
323 // some versions return Microsoft-IIS, some Microsoft/IIS
324 // we could use a preg_match() but it's slower
325 if (PMA_getenv('SERVER_SOFTWARE')
326 && stristr(PMA_getenv('SERVER_SOFTWARE'), 'Microsoft')
327 && stristr(PMA_getenv('SERVER_SOFTWARE'), 'IIS')
329 $this->set('PMA_IS_IIS', 1);
330 } else {
331 $this->set('PMA_IS_IIS', 0);
336 * Whether the os php is running on is windows or not
338 * @return void
340 function checkWebServerOs()
342 // Default to Unix or Equiv
343 $this->set('PMA_IS_WINDOWS', 0);
344 // If PHP_OS is defined then continue
345 if (defined('PHP_OS')) {
346 if (stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin')) {
347 // Is it some version of Windows
348 $this->set('PMA_IS_WINDOWS', 1);
349 } elseif (stristr(PHP_OS, 'OS/2')) {
350 // Is it OS/2 (No file permissions like Windows)
351 $this->set('PMA_IS_WINDOWS', 1);
357 * detects PHP version
359 * @return void
361 function checkPhpVersion()
363 $match = array();
364 if (! preg_match(
365 '@([0-9]{1,2}).([0-9]{1,2}).([0-9]{1,2})@',
366 phpversion(),
367 $match
368 )) {
369 preg_match(
370 '@([0-9]{1,2}).([0-9]{1,2})@',
371 phpversion(),
372 $match
375 if (isset($match) && ! empty($match[1])) {
376 if (! isset($match[2])) {
377 $match[2] = 0;
379 if (! isset($match[3])) {
380 $match[3] = 0;
382 $this->set(
383 'PMA_PHP_INT_VERSION',
384 (int) sprintf('%d%02d%02d', $match[1], $match[2], $match[3])
386 } else {
387 $this->set('PMA_PHP_INT_VERSION', 0);
389 $this->set('PMA_PHP_STR_VERSION', phpversion());
393 * detects if Git revision
395 * @return boolean
397 function isGitRevision()
399 // caching
400 if (isset($_SESSION['is_git_revision'])) {
401 if ($_SESSION['is_git_revision']) {
402 $this->set('PMA_VERSION_GIT', 1);
404 return $_SESSION['is_git_revision'];
406 // find out if there is a .git folder
407 $git_folder = '.git';
408 if (! @file_exists($git_folder)
409 || ! @file_exists($git_folder . '/config')
411 $_SESSION['is_git_revision'] = false;
412 return false;
414 $_SESSION['is_git_revision'] = true;
415 return true;
419 * detects Git revision, if running inside repo
421 * @return void
423 function checkGitRevision()
425 // find out if there is a .git folder
426 $git_folder = '.git';
427 if (! $this->isGitRevision()) {
428 return;
431 if (! $ref_head = @file_get_contents($git_folder . '/HEAD')) {
432 return;
435 $branch = false;
436 // are we on any branch?
437 if (/*overload*/mb_strstr($ref_head, '/')) {
438 $ref_head = /*overload*/mb_substr(trim($ref_head), 5);
439 if (substr($ref_head, 0, 11) === 'refs/heads/') {
440 $branch = /*overload*/mb_substr($ref_head, 11);
441 } else {
442 $branch = basename($ref_head);
445 $ref_file = $git_folder . '/' . $ref_head;
446 if (@file_exists($ref_file)) {
447 $hash = @file_get_contents($ref_file);
448 if (! $hash) {
449 return;
451 $hash = trim($hash);
452 } else {
453 // deal with packed refs
454 $packed_refs = @file_get_contents($git_folder . '/packed-refs');
455 if (! $packed_refs) {
456 return;
458 // split file to lines
459 $ref_lines = explode("\n", $packed_refs);
460 foreach ($ref_lines as $line) {
461 // skip comments
462 if ($line[0] == '#') {
463 continue;
465 // parse line
466 $parts = explode(' ', $line);
467 // care only about named refs
468 if (count($parts) != 2) {
469 continue;
471 // have found our ref?
472 if ($parts[1] == $ref_head) {
473 $hash = $parts[0];
474 break;
477 if (! isset($hash)) {
478 // Could not find ref
479 return;
482 } else {
483 $hash = trim($ref_head);
486 $commit = false;
487 if (! isset($_SESSION['PMA_VERSION_COMMITDATA_' . $hash])) {
488 $git_file_name = $git_folder . '/objects/'
489 . substr($hash, 0, 2) . '/' . substr($hash, 2);
490 if (file_exists($git_file_name) ) {
491 if (! $commit = @file_get_contents($git_file_name)) {
492 return;
494 $commit = explode("\0", gzuncompress($commit), 2);
495 $commit = explode("\n", $commit[1]);
496 $_SESSION['PMA_VERSION_COMMITDATA_' . $hash] = $commit;
497 } else {
498 $pack_names = array();
499 // work with packed data
500 $packs_file = $git_folder . '/objects/info/packs';
501 if (file_exists($packs_file)
502 && $packs = @file_get_contents($packs_file)
504 // File exists. Read it, parse the file to get the names of the
505 // packs. (to look for them in .git/object/pack directory later)
506 foreach (explode("\n", $packs) as $line) {
507 // skip blank lines
508 if (strlen(trim($line)) == 0) {
509 continue;
511 // skip non pack lines
512 if ($line[0] != 'P') {
513 continue;
515 // parse names
516 $pack_names[] = substr($line, 2);
518 } else {
519 // '.git/objects/info/packs' file can be missing
520 // (atlease in mysGit)
521 // File missing. May be we can look in the .git/object/pack
522 // directory for all the .pack files and use that list of
523 // files instead
524 $dirIterator = new DirectoryIterator(
525 $git_folder . '/objects/pack'
527 foreach ($dirIterator as $file_info) {
528 $file_name = $file_info->getFilename();
529 // if this is a .pack file
530 if ($file_info->isFile() && substr($file_name, -5) == '.pack'
532 $pack_names[] = $file_name;
536 $hash = strtolower($hash);
537 foreach ($pack_names as $pack_name) {
538 $index_name = str_replace('.pack', '.idx', $pack_name);
540 // load index
541 $index_data = @file_get_contents(
542 $git_folder . '/objects/pack/' . $index_name
544 if (! $index_data) {
545 continue;
547 // check format
548 if (substr($index_data, 0, 4) != "\377tOc") {
549 continue;
551 // check version
552 $version = unpack('N', substr($index_data, 4, 4));
553 if ($version[1] != 2) {
554 continue;
556 // parse fanout table
557 $fanout = unpack(
558 "N*",
559 substr($index_data, 8, 256 * 4)
562 // find where we should search
563 $firstbyte = intval(substr($hash, 0, 2), 16);
564 // array is indexed from 1 and we need to get
565 // previous entry for start
566 if ($firstbyte == 0) {
567 $start = 0;
568 } else {
569 $start = $fanout[$firstbyte];
571 $end = $fanout[$firstbyte + 1];
573 // stupid linear search for our sha
574 $found = false;
575 $offset = 8 + (256 * 4);
576 for ($position = $start; $position < $end; $position++) {
577 $sha = strtolower(
578 bin2hex(
579 substr($index_data, $offset + ($position * 20), 20)
582 if ($sha == $hash) {
583 $found = true;
584 break;
587 if (! $found) {
588 continue;
590 // read pack offset
591 $offset = 8 + (256 * 4) + (24 * $fanout[256]);
592 $pack_offset = unpack(
593 'N',
594 substr($index_data, $offset + ($position * 4), 4)
596 $pack_offset = $pack_offset[1];
598 // open pack file
599 $pack_file = fopen(
600 $git_folder . '/objects/pack/' . $pack_name, 'rb'
602 if ($pack_file === false) {
603 continue;
605 // seek to start
606 fseek($pack_file, $pack_offset);
608 // parse header
609 $header = ord(fread($pack_file, 1));
610 $type = ($header >> 4) & 7;
611 $hasnext = ($header & 128) >> 7;
612 $size = $header & 0xf;
613 $offset = 4;
615 while ($hasnext) {
616 $byte = ord(fread($pack_file, 1));
617 $size |= ($byte & 0x7f) << $offset;
618 $hasnext = ($byte & 128) >> 7;
619 $offset += 7;
622 // we care only about commit objects
623 if ($type != 1) {
624 continue;
627 // read data
628 $commit = fread($pack_file, $size);
629 $commit = gzuncompress($commit);
630 $commit = explode("\n", $commit);
631 $_SESSION['PMA_VERSION_COMMITDATA_' . $hash] = $commit;
632 fclose($pack_file);
635 } else {
636 $commit = $_SESSION['PMA_VERSION_COMMITDATA_' . $hash];
639 // check if commit exists in Github
640 if ($commit !== false
641 && isset($_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash])
643 $is_remote_commit = $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash];
644 } else {
645 $link = 'https://api.github.com/repos/phpmyadmin/phpmyadmin/git/commits/'
646 . $hash;
647 $is_found = $this->checkHTTP($link, ! $commit);
648 switch($is_found) {
649 case false:
650 $is_remote_commit = false;
651 $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = false;
652 break;
653 case null:
654 // no remote link for now, but don't cache this as Github is down
655 $is_remote_commit = false;
656 break;
657 default:
658 $is_remote_commit = true;
659 $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = true;
660 if ($commit === false) {
661 // if no local commit data, try loading from Github
662 $commit_json = json_decode($is_found);
664 break;
668 $is_remote_branch = false;
669 if ($is_remote_commit && $branch !== false) {
670 // check if branch exists in Github
671 if (isset($_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash])) {
672 $is_remote_branch = $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash];
673 } else {
674 $link = 'https://api.github.com/repos/phpmyadmin/phpmyadmin'
675 . '/git/trees/' . $branch;
676 $is_found = $this->checkHTTP($link);
677 switch($is_found) {
678 case true:
679 $is_remote_branch = true;
680 $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = true;
681 break;
682 case false:
683 $is_remote_branch = false;
684 $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = false;
685 break;
686 case null:
687 // no remote link for now, but don't cache this as Github is down
688 $is_remote_branch = false;
689 break;
694 if ($commit !== false) {
695 $author = array('name' => '', 'email' => '', 'date' => '');
696 $committer = array('name' => '', 'email' => '', 'date' => '');
698 do {
699 $dataline = array_shift($commit);
700 $datalinearr = explode(' ', $dataline, 2);
701 $linetype = $datalinearr[0];
702 if (in_array($linetype, array('author', 'committer'))) {
703 $user = $datalinearr[1];
704 preg_match('/([^<]+)<([^>]+)> ([0-9]+)( [^ ]+)?/', $user, $user);
705 $user2 = array(
706 'name' => trim($user[1]),
707 'email' => trim($user[2]),
708 'date' => date('Y-m-d H:i:s', $user[3]));
709 if (isset($user[4])) {
710 $user2['date'] .= $user[4];
712 $$linetype = $user2;
714 } while ($dataline != '');
715 $message = trim(implode(' ', $commit));
717 } elseif (isset($commit_json)) {
718 $author = array(
719 'name' => $commit_json->author->name,
720 'email' => $commit_json->author->email,
721 'date' => $commit_json->author->date);
722 $committer = array(
723 'name' => $commit_json->committer->name,
724 'email' => $commit_json->committer->email,
725 'date' => $commit_json->committer->date);
726 $message = trim($commit_json->message);
727 } else {
728 return;
731 $this->set('PMA_VERSION_GIT', 1);
732 $this->set('PMA_VERSION_GIT_COMMITHASH', $hash);
733 $this->set('PMA_VERSION_GIT_BRANCH', $branch);
734 $this->set('PMA_VERSION_GIT_MESSAGE', $message);
735 $this->set('PMA_VERSION_GIT_AUTHOR', $author);
736 $this->set('PMA_VERSION_GIT_COMMITTER', $committer);
737 $this->set('PMA_VERSION_GIT_ISREMOTECOMMIT', $is_remote_commit);
738 $this->set('PMA_VERSION_GIT_ISREMOTEBRANCH', $is_remote_branch);
742 * Checks if given URL is 200 or 404, optionally returns data
744 * @param string $link the URL to check
745 * @param boolean $get_body whether to retrieve body of document
747 * @return string|boolean test result or data
749 function checkHTTP($link, $get_body = false)
751 if (! function_exists('curl_init')) {
752 return null;
754 $ch = curl_init($link);
755 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
756 curl_setopt($ch, CURLOPT_HEADER, 1);
757 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
758 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
759 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
760 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
761 curl_setopt($ch, CURLOPT_USERAGENT, 'phpMyAdmin/' . PMA_VERSION);
762 curl_setopt($ch, CURLOPT_TIMEOUT, 5);
763 if (! defined('TESTSUITE')) {
764 session_write_close();
766 $data = @curl_exec($ch);
767 if (! defined('TESTSUITE')) {
768 ini_set('session.use_only_cookies', '0');
769 ini_set('session.use_cookies', '0');
770 ini_set('session.use_trans_sid', '0');
771 ini_set('session.cache_limiter', 'nocache');
772 session_start();
774 if ($data === false) {
775 return null;
777 $httpOk = 'HTTP/1.1 200 OK';
778 $httpNotFound = 'HTTP/1.1 404 Not Found';
780 if (substr($data, 0, strlen($httpOk)) === $httpOk) {
781 return $get_body
782 ? /*overload*/mb_substr(
783 $data,
784 /*overload*/mb_strpos($data, "\r\n\r\n") + 4
786 : true;
789 $httpNOK = substr(
790 $data,
792 strlen($httpNotFound)
794 if ($httpNOK === $httpNotFound) {
795 return false;
797 return null;
801 * loads default values from default source
803 * @return boolean success
805 function loadDefaults()
807 $cfg = array();
808 if (! file_exists($this->default_source)) {
809 $this->error_config_default_file = true;
810 return false;
812 include $this->default_source;
814 $this->default_source_mtime = filemtime($this->default_source);
816 $this->default_server = $cfg['Servers'][1];
817 unset($cfg['Servers']);
819 $this->default = $cfg;
820 $this->settings = PMA_arrayMergeRecursive($this->settings, $cfg);
822 $this->error_config_default_file = false;
824 return true;
828 * loads configuration from $source, usually the config file
829 * should be called on object creation
831 * @param string $source config file
833 * @return bool
835 function load($source = null)
837 $this->loadDefaults();
839 if (null !== $source) {
840 $this->setSource($source);
844 * We check and set the font size at this point, to make the font size
845 * selector work also for users without a config.inc.php
847 $this->checkFontsize();
849 if (! $this->checkConfigSource()) {
850 // even if no config file, set collation_connection
851 $this->checkCollationConnection();
852 return false;
855 $cfg = array();
858 * Parses the configuration file, we throw away any errors or
859 * output.
861 $old_error_reporting = error_reporting(0);
862 ob_start();
863 $GLOBALS['pma_config_loading'] = true;
864 $eval_result = include $this->getSource();
865 $GLOBALS['pma_config_loading'] = false;
866 ob_end_clean();
867 error_reporting($old_error_reporting);
869 if ($eval_result === false) {
870 $this->error_config_file = true;
871 } else {
872 $this->error_config_file = false;
873 $this->source_mtime = filemtime($this->getSource());
877 * Backward compatibility code
879 if (!empty($cfg['DefaultTabTable'])) {
880 $cfg['DefaultTabTable'] = str_replace(
881 '_properties',
883 str_replace(
884 'tbl_properties.php',
885 'tbl_sql.php',
886 $cfg['DefaultTabTable']
890 if (!empty($cfg['DefaultTabDatabase'])) {
891 $cfg['DefaultTabDatabase'] = str_replace(
892 '_details',
894 str_replace(
895 'db_details.php',
896 'db_sql.php',
897 $cfg['DefaultTabDatabase']
902 $this->settings = PMA_arrayMergeRecursive($this->settings, $cfg);
903 $this->checkPmaAbsoluteUri();
905 // Handling of the collation must be done after merging of $cfg
906 // (from config.inc.php) so that $cfg['DefaultConnectionCollation']
907 // can have an effect.
908 $this->checkCollationConnection();
910 return true;
914 * Loads user preferences and merges them with current config
915 * must be called after control connection has been established
917 * @return void
919 function loadUserPreferences()
921 // index.php should load these settings, so that phpmyadmin.css.php
922 // will have everything available in session cache
923 $server = isset($GLOBALS['server'])
924 ? $GLOBALS['server']
925 : (!empty($GLOBALS['cfg']['ServerDefault'])
926 ? $GLOBALS['cfg']['ServerDefault']
927 : 0);
928 $cache_key = 'server_' . $server;
929 if ($server > 0 && !defined('PMA_MINIMUM_COMMON')) {
930 $config_mtime = max($this->default_source_mtime, $this->source_mtime);
931 // cache user preferences, use database only when needed
932 if (! isset($_SESSION['cache'][$cache_key]['userprefs'])
933 || $_SESSION['cache'][$cache_key]['config_mtime'] < $config_mtime
935 // load required libraries
936 include_once './libraries/user_preferences.lib.php';
937 $prefs = PMA_loadUserprefs();
938 $_SESSION['cache'][$cache_key]['userprefs']
939 = PMA_applyUserprefs($prefs['config_data']);
940 $_SESSION['cache'][$cache_key]['userprefs_mtime'] = $prefs['mtime'];
941 $_SESSION['cache'][$cache_key]['userprefs_type'] = $prefs['type'];
942 $_SESSION['cache'][$cache_key]['config_mtime'] = $config_mtime;
944 } elseif ($server == 0
945 || ! isset($_SESSION['cache'][$cache_key]['userprefs'])
947 $this->set('user_preferences', false);
948 return;
950 $config_data = $_SESSION['cache'][$cache_key]['userprefs'];
951 // type is 'db' or 'session'
952 $this->set(
953 'user_preferences',
954 $_SESSION['cache'][$cache_key]['userprefs_type']
956 $this->set(
957 'user_preferences_mtime',
958 $_SESSION['cache'][$cache_key]['userprefs_mtime']
961 // backup some settings
962 $org_fontsize = '';
963 if (isset($this->settings['fontsize'])) {
964 $org_fontsize = $this->settings['fontsize'];
966 // load config array
967 $this->settings = PMA_arrayMergeRecursive($this->settings, $config_data);
968 $GLOBALS['cfg'] = PMA_arrayMergeRecursive($GLOBALS['cfg'], $config_data);
969 if (defined('PMA_MINIMUM_COMMON')) {
970 return;
973 // settings below start really working on next page load, but
974 // changes are made only in index.php so everything is set when
975 // in frames
977 // save theme
978 $tmanager = $_SESSION['PMA_Theme_Manager'];
979 if ($tmanager->getThemeCookie() || isset($_REQUEST['set_theme'])) {
980 if ((! isset($config_data['ThemeDefault'])
981 && $tmanager->theme->getId() != 'original')
982 || isset($config_data['ThemeDefault'])
983 && $config_data['ThemeDefault'] != $tmanager->theme->getId()
985 // new theme was set in common.inc.php
986 $this->setUserValue(
987 null,
988 'ThemeDefault',
989 $tmanager->theme->getId(),
990 'original'
993 } else {
994 // no cookie - read default from settings
995 if ($this->settings['ThemeDefault'] != $tmanager->theme->getId()
996 && $tmanager->checkTheme($this->settings['ThemeDefault'])
998 $tmanager->setActiveTheme($this->settings['ThemeDefault']);
999 $tmanager->setThemeCookie();
1003 // save font size
1004 if ((! isset($config_data['fontsize'])
1005 && $org_fontsize != '82%')
1006 || isset($config_data['fontsize'])
1007 && $org_fontsize != $config_data['fontsize']
1009 $this->setUserValue(null, 'fontsize', $org_fontsize, '82%');
1012 // save language
1013 if (isset($_COOKIE['pma_lang']) || isset($_POST['lang'])) {
1014 if ((! isset($config_data['lang'])
1015 && $GLOBALS['lang'] != 'en')
1016 || isset($config_data['lang'])
1017 && $GLOBALS['lang'] != $config_data['lang']
1019 $this->setUserValue(null, 'lang', $GLOBALS['lang'], 'en');
1021 } else {
1022 // read language from settings
1023 if (isset($config_data['lang']) && PMA_langSet($config_data['lang'])) {
1024 $this->setCookie('pma_lang', $GLOBALS['lang']);
1028 // save connection collation
1029 if (!PMA_DRIZZLE) {
1030 // just to shorten the lines
1031 $collation = 'collation_connection';
1032 if (isset($_COOKIE['pma_collation_connection'])
1033 || isset($_POST[$collation])
1035 if ((! isset($config_data[$collation])
1036 && $GLOBALS[$collation] != 'utf8_general_ci')
1037 || isset($config_data[$collation])
1038 && $GLOBALS[$collation] != $config_data[$collation]
1040 $this->setUserValue(
1041 null,
1042 $collation,
1043 $GLOBALS[$collation],
1044 'utf8_general_ci'
1047 } else {
1048 // read collation from settings
1049 if (isset($config_data['collation_connection'])) {
1050 $GLOBALS['collation_connection']
1051 = $config_data['collation_connection'];
1052 $this->setCookie(
1053 'pma_collation_connection',
1054 $GLOBALS['collation_connection']
1062 * Sets config value which is stored in user preferences (if available)
1063 * or in a cookie.
1065 * If user preferences are not yet initialized, option is applied to
1066 * global config and added to a update queue, which is processed
1067 * by {@link loadUserPreferences()}
1069 * @param string $cookie_name can be null
1070 * @param string $cfg_path configuration path
1071 * @param mixed $new_cfg_value new value
1072 * @param mixed $default_value default value
1074 * @return void
1076 function setUserValue($cookie_name, $cfg_path, $new_cfg_value,
1077 $default_value = null
1079 // use permanent user preferences if possible
1080 $prefs_type = $this->get('user_preferences');
1081 if ($prefs_type) {
1082 include_once './libraries/user_preferences.lib.php';
1083 if ($default_value === null) {
1084 $default_value = PMA_arrayRead($cfg_path, $this->default);
1086 PMA_persistOption($cfg_path, $new_cfg_value, $default_value);
1088 if ($prefs_type != 'db' && $cookie_name) {
1089 // fall back to cookies
1090 if ($default_value === null) {
1091 $default_value = PMA_arrayRead($cfg_path, $this->settings);
1093 $this->setCookie($cookie_name, $new_cfg_value, $default_value);
1095 PMA_arrayWrite($cfg_path, $GLOBALS['cfg'], $new_cfg_value);
1096 PMA_arrayWrite($cfg_path, $this->settings, $new_cfg_value);
1100 * Reads value stored by {@link setUserValue()}
1102 * @param string $cookie_name cookie name
1103 * @param mixed $cfg_value config value
1105 * @return mixed
1107 function getUserValue($cookie_name, $cfg_value)
1109 $cookie_exists = isset($_COOKIE) && !empty($_COOKIE[$cookie_name]);
1110 $prefs_type = $this->get('user_preferences');
1111 if ($prefs_type == 'db') {
1112 // permanent user preferences value exists, remove cookie
1113 if ($cookie_exists) {
1114 $this->removeCookie($cookie_name);
1116 } else if ($cookie_exists) {
1117 return $_COOKIE[$cookie_name];
1119 // return value from $cfg array
1120 return $cfg_value;
1124 * set source
1126 * @param string $source source
1128 * @return void
1130 function setSource($source)
1132 $this->source = trim($source);
1136 * check config source
1138 * @return boolean whether source is valid or not
1140 function checkConfigSource()
1142 if (! $this->getSource()) {
1143 // no configuration file set at all
1144 return false;
1147 if (! file_exists($this->getSource())) {
1148 $this->source_mtime = 0;
1149 return false;
1152 if (! is_readable($this->getSource())) {
1153 // manually check if file is readable
1154 // might be bug #3059806 Supporting running from CIFS/Samba shares
1156 $contents = false;
1157 $handle = @fopen($this->getSource(), 'r');
1158 if ($handle !== false) {
1159 $contents = @fread($handle, 1); // reading 1 byte is enough to test
1160 @fclose($handle);
1162 if ($contents === false) {
1163 $this->source_mtime = 0;
1164 PMA_fatalError(
1165 sprintf(
1166 function_exists('__')
1167 ? __('Existing configuration file (%s) is not readable.')
1168 : 'Existing configuration file (%s) is not readable.',
1169 $this->getSource()
1172 return false;
1176 return true;
1180 * verifies the permissions on config file (if asked by configuration)
1181 * (must be called after config.inc.php has been merged)
1183 * @return void
1185 function checkPermissions()
1187 // Check for permissions (on platforms that support it):
1188 if ($this->get('CheckConfigurationPermissions')) {
1189 $perms = @fileperms($this->getSource());
1190 if (!($perms === false) && ($perms & 2)) {
1191 // This check is normally done after loading configuration
1192 $this->checkWebServerOs();
1193 if ($this->get('PMA_IS_WINDOWS') == 0) {
1194 $this->source_mtime = 0;
1195 PMA_fatalError(
1197 'Wrong permissions on configuration file, '
1198 . 'should not be world writable!'
1207 * returns specific config setting
1209 * @param string $setting config setting
1211 * @return mixed value
1213 function get($setting)
1215 if (isset($this->settings[$setting])) {
1216 return $this->settings[$setting];
1218 return null;
1222 * sets configuration variable
1224 * @param string $setting configuration option
1225 * @param mixed $value new value for configuration option
1227 * @return void
1229 function set($setting, $value)
1231 if (! isset($this->settings[$setting])
1232 || $this->settings[$setting] !== $value
1234 $this->settings[$setting] = $value;
1235 $this->set_mtime = time();
1240 * returns source for current config
1242 * @return string config source
1244 function getSource()
1246 return $this->source;
1250 * returns a unique value to force a CSS reload if either the config
1251 * or the theme changes
1252 * must also check the pma_fontsize cookie in case there is no
1253 * config file
1255 * @return int Summary of unix timestamps and fontsize,
1256 * to be unique on theme parameters change
1258 function getThemeUniqueValue()
1260 if (null !== $this->get('fontsize')) {
1261 $fontsize = intval($this->get('fontsize'));
1262 } elseif (isset($_COOKIE['pma_fontsize'])) {
1263 $fontsize = intval($_COOKIE['pma_fontsize']);
1264 } else {
1265 $fontsize = 0;
1267 return (
1268 $fontsize +
1269 $this->source_mtime +
1270 $this->default_source_mtime +
1271 $this->get('user_preferences_mtime') +
1272 $_SESSION['PMA_Theme']->mtime_info +
1273 $_SESSION['PMA_Theme']->filesize_info);
1277 * $cfg['PmaAbsoluteUri'] is a required directive else cookies won't be
1278 * set properly and, depending on browsers, inserting or updating a
1279 * record might fail
1281 * @return void
1283 function checkPmaAbsoluteUri()
1285 // Setup a default value to let the people and lazy sysadmins work anyway,
1286 // they'll get an error if the autodetect code doesn't work
1287 $pma_absolute_uri = $this->get('PmaAbsoluteUri');
1288 $is_https = $this->detectHttps();
1290 if (/*overload*/mb_strlen($pma_absolute_uri) < 5) {
1291 $url = array();
1293 // If we don't have scheme, we didn't have full URL so we need to
1294 // dig deeper
1295 if (empty($url['scheme'])) {
1296 // Scheme
1297 if ($is_https) {
1298 $url['scheme'] = 'https';
1299 } else {
1300 $url['scheme'] = 'http';
1303 // Host and port
1304 if (PMA_getenv('HTTP_HOST')) {
1305 // Prepend the scheme before using parse_url() since this
1306 // is not part of the RFC2616 Host request-header
1307 $parsed_url = parse_url(
1308 $url['scheme'] . '://' . PMA_getenv('HTTP_HOST')
1310 if (!empty($parsed_url['host'])) {
1311 $url = $parsed_url;
1312 } else {
1313 $url['host'] = PMA_getenv('HTTP_HOST');
1315 } elseif (PMA_getenv('SERVER_NAME')) {
1316 $url['host'] = PMA_getenv('SERVER_NAME');
1317 } else {
1318 $this->error_pma_uri = true;
1319 return;
1322 // If we didn't set port yet...
1323 if (empty($url['port']) && PMA_getenv('SERVER_PORT')) {
1324 $url['port'] = PMA_getenv('SERVER_PORT');
1327 // And finally the path could be already set from REQUEST_URI
1328 if (empty($url['path'])) {
1329 // we got a case with nginx + php-fpm where PHP_SELF
1330 // was not set, so PMA_PHP_SELF was not set as well
1331 if (isset($GLOBALS['PMA_PHP_SELF'])) {
1332 $path = parse_url($GLOBALS['PMA_PHP_SELF']);
1333 } else {
1334 $path = parse_url(PMA_getenv('REQUEST_URI'));
1336 $url['path'] = $path['path'];
1340 // Make url from parts we have
1341 $pma_absolute_uri = $url['scheme'] . '://';
1342 // Was there user information?
1343 if (!empty($url['user'])) {
1344 $pma_absolute_uri .= $url['user'];
1345 if (!empty($url['pass'])) {
1346 $pma_absolute_uri .= ':' . $url['pass'];
1348 $pma_absolute_uri .= '@';
1350 // Add hostname
1351 $pma_absolute_uri .= $url['host'];
1352 // Add port, if it not the default one
1353 if (! empty($url['port'])
1354 && (($url['scheme'] == 'http' && $url['port'] != 80)
1355 || ($url['scheme'] == 'https' && $url['port'] != 443))
1357 $pma_absolute_uri .= ':' . $url['port'];
1359 // And finally path, without script name, the 'a' is there not to
1360 // strip our directory, when path is only /pmadir/ without filename.
1361 // Backslashes returned by Windows have to be changed.
1362 // Only replace backslashes by forward slashes if on Windows,
1363 // as the backslash could be valid on a non-Windows system.
1364 $this->checkWebServerOs();
1365 if ($this->get('PMA_IS_WINDOWS') == 1) {
1366 $path = str_replace("\\", "/", dirname($url['path'] . 'a'));
1367 } else {
1368 $path = dirname($url['path'] . 'a');
1371 // To work correctly within javascript
1372 if (defined('PMA_PATH_TO_BASEDIR') && PMA_PATH_TO_BASEDIR == '../') {
1373 if ($this->get('PMA_IS_WINDOWS') == 1) {
1374 $path = str_replace("\\", "/", dirname($path));
1375 } else {
1376 $path = dirname($path);
1380 // PHP's dirname function would have returned a dot
1381 // when $path contains no slash
1382 if ($path == '.') {
1383 $path = '';
1385 // in vhost situations, there could be already an ending slash
1386 if (/*overload*/mb_substr($path, -1) != '/') {
1387 $path .= '/';
1389 $pma_absolute_uri .= $path;
1391 // This is to handle the case of a reverse proxy
1392 if ($this->get('ForceSSL')) {
1393 $this->set('PmaAbsoluteUri', $pma_absolute_uri);
1394 $pma_absolute_uri = $this->getSSLUri();
1395 $this->isHttps();
1398 // We used to display a warning if PmaAbsoluteUri wasn't set, but now
1399 // the autodetect code works well enough that we don't display the
1400 // warning at all. The user can still set PmaAbsoluteUri manually.
1402 } else {
1403 // The URI is specified, however users do often specify this
1404 // wrongly, so we try to fix this.
1406 // Adds a trailing slash et the end of the phpMyAdmin uri if it
1407 // does not exist.
1408 if (/*overload*/mb_substr($pma_absolute_uri, -1) != '/') {
1409 $pma_absolute_uri .= '/';
1412 // If URI doesn't start with http:// or https://, we will add
1413 // this.
1414 if (/*overload*/mb_substr($pma_absolute_uri, 0, 7) != 'http://'
1415 && /*overload*/mb_substr($pma_absolute_uri, 0, 8) != 'https://'
1417 $pma_absolute_uri
1418 = ($is_https ? 'https' : 'http')
1419 . ':'
1421 /*overload*/mb_substr($pma_absolute_uri, 0, 2) == '//'
1422 ? ''
1423 : '//'
1425 . $pma_absolute_uri;
1428 $this->set('PmaAbsoluteUri', $pma_absolute_uri);
1432 * Converts currently used PmaAbsoluteUri to SSL based variant.
1434 * @return String witch adjusted URI
1436 function getSSLUri()
1438 // grab current URL
1439 $url = $this->get('PmaAbsoluteUri');
1440 // Parse current URL
1441 $parsed = parse_url($url);
1442 // In case parsing has failed do stupid string replacement
1443 if ($parsed === false) {
1444 // Replace http protocol
1445 return preg_replace('@^http:@', 'https:', $url);
1448 // Reconstruct URL using parsed parts
1449 return 'https://' . $parsed['host'] . ':443' . $parsed['path'];
1453 * Sets collation_connection based on user preference. First is checked
1454 * value from request, then cookies with fallback to default.
1456 * After setting it here, cookie is set in common.inc.php to persist
1457 * the selection.
1459 * @todo check validity of collation string
1461 * @return void
1463 function checkCollationConnection()
1465 if (! empty($_REQUEST['collation_connection'])) {
1466 $collation = strip_tags($_REQUEST['collation_connection']);
1467 } elseif (! empty($_COOKIE['pma_collation_connection'])) {
1468 $collation = strip_tags($_COOKIE['pma_collation_connection']);
1469 } else {
1470 $collation = $this->get('DefaultConnectionCollation');
1472 $this->set('collation_connection', $collation);
1476 * checks for font size configuration, and sets font size as requested by user
1478 * @return void
1480 function checkFontsize()
1482 $new_fontsize = '';
1484 if (isset($_GET['set_fontsize'])) {
1485 $new_fontsize = $_GET['set_fontsize'];
1486 } elseif (isset($_POST['set_fontsize'])) {
1487 $new_fontsize = $_POST['set_fontsize'];
1488 } elseif (isset($_COOKIE['pma_fontsize'])) {
1489 $new_fontsize = $_COOKIE['pma_fontsize'];
1492 if (preg_match('/^[0-9.]+(px|em|pt|\%)$/', $new_fontsize)) {
1493 $this->set('fontsize', $new_fontsize);
1494 } elseif (! $this->get('fontsize')) {
1495 // 80% would correspond to the default browser font size
1496 // of 16, but use 82% to help read the monoface font
1497 $this->set('fontsize', '82%');
1500 $this->setCookie('pma_fontsize', $this->get('fontsize'), '82%');
1504 * checks if upload is enabled
1506 * @return void
1508 function checkUpload()
1510 if (!ini_get('file_uploads')) {
1511 $this->set('enable_upload', false);
1512 return;
1515 $this->set('enable_upload', true);
1516 // if set "php_admin_value file_uploads Off" in httpd.conf
1517 // ini_get() also returns the string "Off" in this case:
1518 if ('off' == strtolower(ini_get('file_uploads'))) {
1519 $this->set('enable_upload', false);
1524 * Maximum upload size as limited by PHP
1525 * Used with permission from Moodle (http://moodle.org) by Martin Dougiamas
1527 * this section generates $max_upload_size in bytes
1529 * @return void
1531 function checkUploadSize()
1533 if (! $filesize = ini_get('upload_max_filesize')) {
1534 $filesize = "5M";
1537 if ($postsize = ini_get('post_max_size')) {
1538 $this->set(
1539 'max_upload_size',
1540 min(PMA_getRealSize($filesize), PMA_getRealSize($postsize))
1542 } else {
1543 $this->set('max_upload_size', PMA_getRealSize($filesize));
1548 * Checks if protocol is https
1550 * This function checks if the https protocol is used in the PmaAbsoluteUri
1551 * configuration setting, as opposed to detectHttps() which checks if the
1552 * https protocol is used on the active connection.
1554 * @return bool
1556 public function isHttps()
1559 if (null !== $this->get('is_https')) {
1560 return $this->get('is_https');
1563 $url = parse_url($this->get('PmaAbsoluteUri'));
1565 $is_https = (isset($url['scheme']) && $url['scheme'] == 'https');
1567 $this->set('is_https', $is_https);
1569 return $is_https;
1573 * Detects whether https appears to be used.
1575 * This function checks if the https protocol is used in the current connection
1576 * with the webserver, based on environment variables.
1577 * Please note that this just detects what we see, so
1578 * it completely ignores things like reverse proxies.
1580 * @return bool
1582 function detectHttps()
1584 $url = array();
1586 // At first we try to parse REQUEST_URI, it might contain full URL,
1587 if (PMA_getenv('REQUEST_URI')) {
1588 // produces E_WARNING if it cannot get parsed, e.g. '/foobar:/'
1589 $url = @parse_url(PMA_getenv('REQUEST_URI'));
1590 if ($url === false) {
1591 $url = array();
1595 // If we don't have scheme, we didn't have full URL so we need to
1596 // dig deeper
1597 if (empty($url['scheme'])) {
1598 // Scheme
1599 if (PMA_getenv('HTTP_SCHEME')) {
1600 $url['scheme'] = PMA_getenv('HTTP_SCHEME');
1601 } elseif (PMA_getenv('HTTPS')
1602 && strtolower(PMA_getenv('HTTPS')) == 'on'
1604 $url['scheme'] = 'https';
1605 // A10 Networks load balancer:
1606 } elseif (PMA_getenv('HTTP_HTTPS_FROM_LB')
1607 && strtolower(PMA_getenv('HTTP_HTTPS_FROM_LB')) == 'on'
1609 $url['scheme'] = 'https';
1610 } elseif (PMA_getenv('HTTP_X_FORWARDED_PROTO')) {
1611 $url['scheme'] = /*overload*/mb_strtolower(
1612 PMA_getenv('HTTP_X_FORWARDED_PROTO')
1614 } elseif (PMA_getenv('HTTP_FRONT_END_HTTPS')
1615 && strtolower(PMA_getenv('HTTP_FRONT_END_HTTPS')) == 'on'
1617 $url['scheme'] = 'https';
1618 } else {
1619 $url['scheme'] = 'http';
1623 if (isset($url['scheme']) && $url['scheme'] == 'https') {
1624 $is_https = true;
1625 } else {
1626 $is_https = false;
1629 return $is_https;
1633 * detect correct cookie path
1635 * @return void
1637 function checkCookiePath()
1639 $this->set('cookie_path', $this->getCookiePath());
1643 * Get cookie path
1645 * @return string
1647 public function getCookiePath()
1649 static $cookie_path = null;
1651 if (null !== $cookie_path && !defined('TESTSUITE')) {
1652 return $cookie_path;
1655 $parsed_url = parse_url($this->get('PmaAbsoluteUri'));
1657 $cookie_path = $parsed_url['path'];
1659 return $cookie_path;
1663 * enables backward compatibility
1665 * @return void
1667 function enableBc()
1669 $GLOBALS['cfg'] = $this->settings;
1670 $GLOBALS['default_server'] = $this->default_server;
1671 unset($this->default_server);
1672 $GLOBALS['collation_connection'] = $this->get('collation_connection');
1673 $GLOBALS['is_upload'] = $this->get('enable_upload');
1674 $GLOBALS['max_upload_size'] = $this->get('max_upload_size');
1675 $GLOBALS['cookie_path'] = $this->get('cookie_path');
1676 $GLOBALS['is_https'] = $this->get('is_https');
1678 $defines = array(
1679 'PMA_VERSION',
1680 'PMA_THEME_VERSION',
1681 'PMA_THEME_GENERATION',
1682 'PMA_PHP_STR_VERSION',
1683 'PMA_PHP_INT_VERSION',
1684 'PMA_IS_WINDOWS',
1685 'PMA_IS_IIS',
1686 'PMA_IS_GD2',
1687 'PMA_USR_OS',
1688 'PMA_USR_BROWSER_VER',
1689 'PMA_USR_BROWSER_AGENT'
1692 foreach ($defines as $define) {
1693 if (! defined($define)) {
1694 define($define, $this->get($define));
1700 * returns options for font size selection
1702 * @param string $current_size current selected font size with unit
1704 * @return array selectable font sizes
1706 * @static
1708 static protected function getFontsizeOptions($current_size = '82%')
1710 $unit = preg_replace('/[0-9.]*/', '', $current_size);
1711 $value = preg_replace('/[^0-9.]*/', '', $current_size);
1713 $factors = array();
1714 $options = array();
1715 $options["$value"] = $value . $unit;
1717 if ($unit === '%') {
1718 $factors[] = 1;
1719 $factors[] = 5;
1720 $factors[] = 10;
1721 } elseif ($unit === 'em') {
1722 $factors[] = 0.05;
1723 $factors[] = 0.2;
1724 $factors[] = 1;
1725 } elseif ($unit === 'pt') {
1726 $factors[] = 0.5;
1727 $factors[] = 2;
1728 } elseif ($unit === 'px') {
1729 $factors[] = 1;
1730 $factors[] = 5;
1731 $factors[] = 10;
1732 } else {
1733 //unknown font size unit
1734 $factors[] = 0.05;
1735 $factors[] = 0.2;
1736 $factors[] = 1;
1737 $factors[] = 5;
1738 $factors[] = 10;
1741 foreach ($factors as $key => $factor) {
1742 $option_inc = $value + $factor;
1743 $option_dec = $value - $factor;
1744 while (count($options) < 21) {
1745 $options["$option_inc"] = $option_inc . $unit;
1746 if ($option_dec > $factors[0]) {
1747 $options["$option_dec"] = $option_dec . $unit;
1749 $option_inc += $factor;
1750 $option_dec -= $factor;
1751 if (isset($factors[$key + 1])
1752 && $option_inc >= $value + $factors[$key + 1]
1754 break;
1758 ksort($options);
1759 return $options;
1763 * returns html selectbox for font sizes
1765 * @static
1767 * @return string html selectbox
1769 static protected function getFontsizeSelection()
1771 $current_size = $GLOBALS['PMA_Config']->get('fontsize');
1772 // for the case when there is no config file (this is supported)
1773 if (empty($current_size)) {
1774 if (isset($_COOKIE['pma_fontsize'])) {
1775 $current_size = htmlspecialchars($_COOKIE['pma_fontsize']);
1776 } else {
1777 $current_size = '82%';
1780 $options = PMA_Config::getFontsizeOptions($current_size);
1782 $return = '<label for="select_fontsize">' . __('Font size')
1783 . ':</label>' . "\n"
1784 . '<select name="set_fontsize" id="select_fontsize"'
1785 . ' class="autosubmit">' . "\n";
1786 foreach ($options as $option) {
1787 $return .= '<option value="' . $option . '"';
1788 if ($option == $current_size) {
1789 $return .= ' selected="selected"';
1791 $return .= '>' . $option . '</option>' . "\n";
1793 $return .= '</select>';
1795 return $return;
1799 * return complete font size selection form
1801 * @static
1803 * @return string html selectbox
1805 static public function getFontsizeForm()
1807 return '<form name="form_fontsize_selection" id="form_fontsize_selection"'
1808 . ' method="get" action="index.php" class="disableAjax">' . "\n"
1809 . PMA_URL_getHiddenInputs() . "\n"
1810 . PMA_Config::getFontsizeSelection() . "\n"
1811 . '</form>';
1815 * removes cookie
1817 * @param string $cookie name of cookie to remove
1819 * @return boolean result of setcookie()
1821 function removeCookie($cookie)
1823 if (defined('TESTSUITE')) {
1824 if (isset($_COOKIE[$cookie])) {
1825 unset($_COOKIE[$cookie]);
1827 return true;
1829 return setcookie(
1830 $cookie,
1832 time() - 3600,
1833 $this->getCookiePath(),
1835 $this->isHttps()
1840 * sets cookie if value is different from current cookie value,
1841 * or removes if value is equal to default
1843 * @param string $cookie name of cookie to remove
1844 * @param mixed $value new cookie value
1845 * @param string $default default value
1846 * @param int $validity validity of cookie in seconds (default is one month)
1847 * @param bool $httponly whether cookie is only for HTTP (and not for scripts)
1849 * @return boolean result of setcookie()
1851 function setCookie($cookie, $value, $default = null, $validity = null,
1852 $httponly = true
1854 if (/*overload*/mb_strlen($value) && null !== $default && $value === $default
1856 // default value is used
1857 if (isset($_COOKIE[$cookie])) {
1858 // remove cookie
1859 return $this->removeCookie($cookie);
1861 return false;
1864 if (!/*overload*/mb_strlen($value) && isset($_COOKIE[$cookie])) {
1865 // remove cookie, value is empty
1866 return $this->removeCookie($cookie);
1869 if (! isset($_COOKIE[$cookie]) || $_COOKIE[$cookie] !== $value) {
1870 // set cookie with new value
1871 /* Calculate cookie validity */
1872 if ($validity === null) {
1873 $validity = time() + 2592000;
1874 } elseif ($validity == 0) {
1875 $validity = 0;
1876 } else {
1877 $validity = time() + $validity;
1879 if (defined('TESTSUITE')) {
1880 $_COOKIE[$cookie] = $value;
1881 return true;
1883 return setcookie(
1884 $cookie,
1885 $value,
1886 $validity,
1887 $this->getCookiePath(),
1889 $this->isHttps(),
1890 $httponly
1894 // cookie has already $value as value
1895 return true;
1901 * Error handler to catch fatal errors when loading configuration
1902 * file
1904 * @return void
1906 function PMA_Config_fatalErrorHandler()
1908 if (isset($GLOBALS['pma_config_loading']) && $GLOBALS['pma_config_loading']) {
1909 $error = error_get_last();
1910 if ($error !== null) {
1911 PMA_fatalError(
1912 sprintf(
1913 'Failed to load phpMyAdmin configuration (%s:%s): %s',
1914 PMA_Error::relPath($error['file']),
1915 $error['line'],
1916 $error['message']
1923 if (!defined('TESTSUITE')) {
1924 register_shutdown_function('PMA_Config_fatalErrorHandler');