Translated using Weblate (Slovenian)
[phpmyadmin.git] / libraries / Config.php
blob548405a7ebbd169803e10cd2c307e1f9276b8767
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Configuration handling.
6 * @package PhpMyAdmin
7 */
8 namespace PMA\libraries;
10 /**
11 * Load vendor configuration.
13 use DirectoryIterator;
15 require_once './libraries/vendor_config.php';
17 /**
18 * Indication for error handler (see end of this file).
20 $GLOBALS['pma_config_loading'] = false;
22 /**
23 * Configuration class
25 * @package PhpMyAdmin
27 class Config
29 /**
30 * @var string default config source
32 var $default_source = './libraries/config.default.php';
34 /**
35 * @var array default configuration settings
37 var $default = array();
39 /**
40 * @var array configuration settings, without user preferences applied
42 var $base_settings = array();
44 /**
45 * @var array configuration settings
47 var $settings = array();
49 /**
50 * @var string config source
52 var $source = '';
54 /**
55 * @var int source modification time
57 var $source_mtime = 0;
58 var $default_source_mtime = 0;
59 var $set_mtime = 0;
61 /**
62 * @var boolean
64 var $error_config_file = false;
66 /**
67 * @var boolean
69 var $error_config_default_file = false;
71 /**
72 * @var array
74 var $default_server = array();
76 /**
77 * @var boolean whether init is done or not
78 * set this to false to force some initial checks
79 * like checking for required functions
81 var $done = false;
83 /**
84 * constructor
86 * @param string $source source to read config from
88 public function __construct($source = null)
90 $this->settings = array();
92 // functions need to refresh in case of config file changed goes in
93 // PMA\libraries\Config::load()
94 $this->load($source);
96 // other settings, independent from config file, comes in
97 $this->checkSystem();
99 $this->base_settings = $this->settings;
103 * sets system and application settings
105 * @return void
107 public function checkSystem()
109 $this->set('PMA_VERSION', '4.6.0-dev');
111 * @deprecated
113 $this->set('PMA_THEME_VERSION', 2);
115 * @deprecated
117 $this->set('PMA_THEME_GENERATION', 2);
119 $this->checkWebServerOs();
120 $this->checkWebServer();
121 $this->checkGd2();
122 $this->checkClient();
123 $this->checkUpload();
124 $this->checkUploadSize();
125 $this->checkOutputCompression();
129 * whether to use gzip output compression or not
131 * @return void
133 public function checkOutputCompression()
135 // If zlib output compression is set in the php configuration file, no
136 // output buffering should be run
137 if (@ini_get('zlib.output_compression')) {
138 $this->set('OBGzip', false);
141 // disable output-buffering (if set to 'auto') for IE6, else enable it.
142 if (strtolower($this->get('OBGzip')) == 'auto') {
143 if ($this->get('PMA_USR_BROWSER_AGENT') == 'IE'
144 && $this->get('PMA_USR_BROWSER_VER') >= 6
145 && $this->get('PMA_USR_BROWSER_VER') < 7
147 $this->set('OBGzip', false);
148 } else {
149 $this->set('OBGzip', true);
155 * Sets the client platform based on user agent
157 * @param string $user_agent the user agent
159 * @return void
161 private function _setClientPlatform($user_agent)
163 if (mb_strstr($user_agent, 'Win')) {
164 $this->set('PMA_USR_OS', 'Win');
165 } elseif (mb_strstr($user_agent, 'Mac')) {
166 $this->set('PMA_USR_OS', 'Mac');
167 } elseif (mb_strstr($user_agent, 'Linux')) {
168 $this->set('PMA_USR_OS', 'Linux');
169 } elseif (mb_strstr($user_agent, 'Unix')) {
170 $this->set('PMA_USR_OS', 'Unix');
171 } elseif (mb_strstr($user_agent, 'OS/2')) {
172 $this->set('PMA_USR_OS', 'OS/2');
173 } else {
174 $this->set('PMA_USR_OS', 'Other');
179 * Determines platform (OS), browser and version of the user
180 * Based on a phpBuilder article:
182 * @see http://www.phpbuilder.net/columns/tim20000821.php
184 * @return void
186 public function checkClient()
188 if (PMA_getenv('HTTP_USER_AGENT')) {
189 $HTTP_USER_AGENT = PMA_getenv('HTTP_USER_AGENT');
190 } else {
191 $HTTP_USER_AGENT = '';
194 // 1. Platform
195 $this->_setClientPlatform($HTTP_USER_AGENT);
197 // 2. browser and version
198 // (must check everything else before Mozilla)
200 $is_mozilla = preg_match(
201 '@Mozilla/([0-9].[0-9]{1,2})@',
202 $HTTP_USER_AGENT,
203 $mozilla_version
206 if (preg_match(
207 '@Opera(/| )([0-9].[0-9]{1,2})@',
208 $HTTP_USER_AGENT,
209 $log_version
210 )) {
211 $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
212 $this->set('PMA_USR_BROWSER_AGENT', 'OPERA');
213 } elseif (preg_match(
214 '@(MS)?IE ([0-9]{1,2}.[0-9]{1,2})@',
215 $HTTP_USER_AGENT,
216 $log_version
217 )) {
218 $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
219 $this->set('PMA_USR_BROWSER_AGENT', 'IE');
220 } elseif (preg_match(
221 '@Trident/(7)\.0@',
222 $HTTP_USER_AGENT,
223 $log_version
224 )) {
225 $this->set('PMA_USR_BROWSER_VER', intval($log_version[1]) + 4);
226 $this->set('PMA_USR_BROWSER_AGENT', 'IE');
227 } elseif (preg_match(
228 '@OmniWeb/([0-9].[0-9]{1,2})@',
229 $HTTP_USER_AGENT,
230 $log_version
231 )) {
232 $this->set('PMA_USR_BROWSER_VER', $log_version[1]);
233 $this->set('PMA_USR_BROWSER_AGENT', 'OMNIWEB');
234 // Konqueror 2.2.2 says Konqueror/2.2.2
235 // Konqueror 3.0.3 says Konqueror/3
236 } elseif (preg_match(
237 '@(Konqueror/)(.*)(;)@',
238 $HTTP_USER_AGENT,
239 $log_version
240 )) {
241 $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
242 $this->set('PMA_USR_BROWSER_AGENT', 'KONQUEROR');
243 // must check Chrome before Safari
244 } elseif ($is_mozilla
245 && preg_match('@Chrome/([0-9.]*)@', $HTTP_USER_AGENT, $log_version)
247 $this->set('PMA_USR_BROWSER_VER', $log_version[1]);
248 $this->set('PMA_USR_BROWSER_AGENT', 'CHROME');
249 // newer Safari
250 } elseif ($is_mozilla
251 && preg_match('@Version/(.*) Safari@', $HTTP_USER_AGENT, $log_version)
253 $this->set(
254 'PMA_USR_BROWSER_VER', $log_version[1]
256 $this->set('PMA_USR_BROWSER_AGENT', 'SAFARI');
257 // older Safari
258 } elseif ($is_mozilla
259 && preg_match('@Safari/([0-9]*)@', $HTTP_USER_AGENT, $log_version)
261 $this->set(
262 'PMA_USR_BROWSER_VER', $mozilla_version[1] . '.' . $log_version[1]
264 $this->set('PMA_USR_BROWSER_AGENT', 'SAFARI');
265 // Firefox
266 } elseif (! mb_strstr($HTTP_USER_AGENT, 'compatible')
267 && preg_match('@Firefox/([\w.]+)@', $HTTP_USER_AGENT, $log_version)
269 $this->set(
270 'PMA_USR_BROWSER_VER', $log_version[1]
272 $this->set('PMA_USR_BROWSER_AGENT', 'FIREFOX');
273 } elseif (preg_match('@rv:1.9(.*)Gecko@', $HTTP_USER_AGENT)) {
274 $this->set('PMA_USR_BROWSER_VER', '1.9');
275 $this->set('PMA_USR_BROWSER_AGENT', 'GECKO');
276 } elseif ($is_mozilla) {
277 $this->set('PMA_USR_BROWSER_VER', $mozilla_version[1]);
278 $this->set('PMA_USR_BROWSER_AGENT', 'MOZILLA');
279 } else {
280 $this->set('PMA_USR_BROWSER_VER', 0);
281 $this->set('PMA_USR_BROWSER_AGENT', 'OTHER');
286 * Whether GD2 is present
288 * @return void
290 public function checkGd2()
292 if ($this->get('GD2Available') == 'yes') {
293 $this->set('PMA_IS_GD2', 1);
294 return;
297 if ($this->get('GD2Available') == 'no') {
298 $this->set('PMA_IS_GD2', 0);
299 return;
302 if (!@function_exists('imagecreatetruecolor')) {
303 $this->set('PMA_IS_GD2', 0);
304 return;
307 if (@function_exists('gd_info')) {
308 $gd_nfo = gd_info();
309 if (mb_strstr($gd_nfo["GD Version"], '2.')) {
310 $this->set('PMA_IS_GD2', 1);
311 } else {
312 $this->set('PMA_IS_GD2', 0);
314 } else {
315 $this->set('PMA_IS_GD2', 0);
320 * Whether the Web server php is running on is IIS
322 * @return void
324 public function checkWebServer()
326 // some versions return Microsoft-IIS, some Microsoft/IIS
327 // we could use a preg_match() but it's slower
328 if (PMA_getenv('SERVER_SOFTWARE')
329 && stristr(PMA_getenv('SERVER_SOFTWARE'), 'Microsoft')
330 && stristr(PMA_getenv('SERVER_SOFTWARE'), 'IIS')
332 $this->set('PMA_IS_IIS', 1);
333 } else {
334 $this->set('PMA_IS_IIS', 0);
339 * Whether the os php is running on is windows or not
341 * @return void
343 public function checkWebServerOs()
345 // Default to Unix or Equiv
346 $this->set('PMA_IS_WINDOWS', 0);
347 // If PHP_OS is defined then continue
348 if (defined('PHP_OS')) {
349 if (stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin')) {
350 // Is it some version of Windows
351 $this->set('PMA_IS_WINDOWS', 1);
352 } elseif (stristr(PHP_OS, 'OS/2')) {
353 // Is it OS/2 (No file permissions like Windows)
354 $this->set('PMA_IS_WINDOWS', 1);
360 * detects if Git revision
362 * @return boolean
364 public function isGitRevision()
366 if (!$this->get('ShowGitRevision')) {
367 return false;
370 // caching
371 if (isset($_SESSION['is_git_revision'])) {
372 if ($_SESSION['is_git_revision']) {
373 $this->set('PMA_VERSION_GIT', 1);
375 return $_SESSION['is_git_revision'];
377 // find out if there is a .git folder
378 $git_folder = '.git';
379 if (! @file_exists($git_folder)
380 || ! @file_exists($git_folder . '/config')
382 $_SESSION['is_git_revision'] = false;
383 return false;
385 $_SESSION['is_git_revision'] = true;
386 return true;
390 * detects Git revision, if running inside repo
392 * @return void
394 public function checkGitRevision()
396 // find out if there is a .git folder
397 $git_folder = '.git';
398 if (! $this->isGitRevision()) {
399 return;
402 if (! $ref_head = @file_get_contents($git_folder . '/HEAD')) {
403 return;
406 $branch = false;
407 // are we on any branch?
408 if (mb_strstr($ref_head, '/')) {
409 $ref_head = mb_substr(trim($ref_head), 5);
410 if (substr($ref_head, 0, 11) === 'refs/heads/') {
411 $branch = mb_substr($ref_head, 11);
412 } else {
413 $branch = basename($ref_head);
416 $ref_file = $git_folder . '/' . $ref_head;
417 if (@file_exists($ref_file)) {
418 $hash = @file_get_contents($ref_file);
419 if (! $hash) {
420 return;
422 $hash = trim($hash);
423 } else {
424 // deal with packed refs
425 $packed_refs = @file_get_contents($git_folder . '/packed-refs');
426 if (! $packed_refs) {
427 return;
429 // split file to lines
430 $ref_lines = explode("\n", $packed_refs);
431 foreach ($ref_lines as $line) {
432 // skip comments
433 if ($line[0] == '#') {
434 continue;
436 // parse line
437 $parts = explode(' ', $line);
438 // care only about named refs
439 if (count($parts) != 2) {
440 continue;
442 // have found our ref?
443 if ($parts[1] == $ref_head) {
444 $hash = $parts[0];
445 break;
448 if (! isset($hash)) {
449 // Could not find ref
450 return;
453 } else {
454 $hash = trim($ref_head);
457 $commit = false;
458 if (isset($_SESSION['PMA_VERSION_COMMITDATA_' . $hash])) {
459 $commit = $_SESSION['PMA_VERSION_COMMITDATA_' . $hash];
460 } elseif (function_exists('gzuncompress')) {
461 $git_file_name = $git_folder . '/objects/'
462 . substr($hash, 0, 2) . '/' . substr($hash, 2);
463 if (file_exists($git_file_name) ) {
464 if (! $commit = @file_get_contents($git_file_name)) {
465 return;
467 $commit = explode("\0", gzuncompress($commit), 2);
468 $commit = explode("\n", $commit[1]);
469 $_SESSION['PMA_VERSION_COMMITDATA_' . $hash] = $commit;
470 } else {
471 $pack_names = array();
472 // work with packed data
473 $packs_file = $git_folder . '/objects/info/packs';
474 if (file_exists($packs_file)
475 && $packs = @file_get_contents($packs_file)
477 // File exists. Read it, parse the file to get the names of the
478 // packs. (to look for them in .git/object/pack directory later)
479 foreach (explode("\n", $packs) as $line) {
480 // skip blank lines
481 if (strlen(trim($line)) == 0) {
482 continue;
484 // skip non pack lines
485 if ($line[0] != 'P') {
486 continue;
488 // parse names
489 $pack_names[] = substr($line, 2);
491 } else {
492 // '.git/objects/info/packs' file can be missing
493 // (atlease in mysGit)
494 // File missing. May be we can look in the .git/object/pack
495 // directory for all the .pack files and use that list of
496 // files instead
497 $dirIterator = new DirectoryIterator(
498 $git_folder . '/objects/pack'
500 foreach ($dirIterator as $file_info) {
501 $file_name = $file_info->getFilename();
502 // if this is a .pack file
503 if ($file_info->isFile() && substr($file_name, -5) == '.pack'
505 $pack_names[] = $file_name;
509 $hash = strtolower($hash);
510 foreach ($pack_names as $pack_name) {
511 $index_name = str_replace('.pack', '.idx', $pack_name);
513 // load index
514 $index_data = @file_get_contents(
515 $git_folder . '/objects/pack/' . $index_name
517 if (! $index_data) {
518 continue;
520 // check format
521 if (substr($index_data, 0, 4) != "\377tOc") {
522 continue;
524 // check version
525 $version = unpack('N', substr($index_data, 4, 4));
526 if ($version[1] != 2) {
527 continue;
529 // parse fanout table
530 $fanout = unpack(
531 "N*",
532 substr($index_data, 8, 256 * 4)
535 // find where we should search
536 $firstbyte = intval(substr($hash, 0, 2), 16);
537 // array is indexed from 1 and we need to get
538 // previous entry for start
539 if ($firstbyte == 0) {
540 $start = 0;
541 } else {
542 $start = $fanout[$firstbyte];
544 $end = $fanout[$firstbyte + 1];
546 // stupid linear search for our sha
547 $found = false;
548 $offset = 8 + (256 * 4);
549 for ($position = $start; $position < $end; $position++) {
550 $sha = strtolower(
551 bin2hex(
552 substr($index_data, $offset + ($position * 20), 20)
555 if ($sha == $hash) {
556 $found = true;
557 break;
560 if (! $found) {
561 continue;
563 // read pack offset
564 $offset = 8 + (256 * 4) + (24 * $fanout[256]);
565 $pack_offset = unpack(
566 'N',
567 substr($index_data, $offset + ($position * 4), 4)
569 $pack_offset = $pack_offset[1];
571 // open pack file
572 $pack_file = fopen(
573 $git_folder . '/objects/pack/' . $pack_name, 'rb'
575 if ($pack_file === false) {
576 continue;
578 // seek to start
579 fseek($pack_file, $pack_offset);
581 // parse header
582 $header = ord(fread($pack_file, 1));
583 $type = ($header >> 4) & 7;
584 $hasnext = ($header & 128) >> 7;
585 $size = $header & 0xf;
586 $offset = 4;
588 while ($hasnext) {
589 $byte = ord(fread($pack_file, 1));
590 $size |= ($byte & 0x7f) << $offset;
591 $hasnext = ($byte & 128) >> 7;
592 $offset += 7;
595 // we care only about commit objects
596 if ($type != 1) {
597 continue;
600 // read data
601 $commit = fread($pack_file, $size);
602 $commit = gzuncompress($commit);
603 $commit = explode("\n", $commit);
604 $_SESSION['PMA_VERSION_COMMITDATA_' . $hash] = $commit;
605 fclose($pack_file);
610 // check if commit exists in Github
611 if ($commit !== false
612 && isset($_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash])
614 $is_remote_commit = $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash];
615 } else {
616 $link = 'https://api.github.com/repos/phpmyadmin/phpmyadmin/git/commits/'
617 . $hash;
618 $is_found = $this->checkHTTP($link, ! $commit);
619 switch($is_found) {
620 case false:
621 $is_remote_commit = false;
622 $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = false;
623 break;
624 case null:
625 // no remote link for now, but don't cache this as Github is down
626 $is_remote_commit = false;
627 break;
628 default:
629 $is_remote_commit = true;
630 $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = true;
631 if ($commit === false) {
632 // if no local commit data, try loading from Github
633 $commit_json = json_decode($is_found);
635 break;
639 $is_remote_branch = false;
640 if ($is_remote_commit && $branch !== false) {
641 // check if branch exists in Github
642 if (isset($_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash])) {
643 $is_remote_branch = $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash];
644 } else {
645 $link = 'https://api.github.com/repos/phpmyadmin/phpmyadmin'
646 . '/git/trees/' . $branch;
647 $is_found = $this->checkHTTP($link);
648 switch($is_found) {
649 case true:
650 $is_remote_branch = true;
651 $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = true;
652 break;
653 case false:
654 $is_remote_branch = false;
655 $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = false;
656 break;
657 case null:
658 // no remote link for now, but don't cache this as Github is down
659 $is_remote_branch = false;
660 break;
665 if ($commit !== false) {
666 $author = array('name' => '', 'email' => '', 'date' => '');
667 $committer = array('name' => '', 'email' => '', 'date' => '');
669 do {
670 $dataline = array_shift($commit);
671 $datalinearr = explode(' ', $dataline, 2);
672 $linetype = $datalinearr[0];
673 if (in_array($linetype, array('author', 'committer'))) {
674 $user = $datalinearr[1];
675 preg_match('/([^<]+)<([^>]+)> ([0-9]+)( [^ ]+)?/', $user, $user);
676 $user2 = array(
677 'name' => trim($user[1]),
678 'email' => trim($user[2]),
679 'date' => date('Y-m-d H:i:s', $user[3]));
680 if (isset($user[4])) {
681 $user2['date'] .= $user[4];
683 $$linetype = $user2;
685 } while ($dataline != '');
686 $message = trim(implode(' ', $commit));
688 } elseif (isset($commit_json)) {
689 $author = array(
690 'name' => $commit_json->author->name,
691 'email' => $commit_json->author->email,
692 'date' => $commit_json->author->date);
693 $committer = array(
694 'name' => $commit_json->committer->name,
695 'email' => $commit_json->committer->email,
696 'date' => $commit_json->committer->date);
697 $message = trim($commit_json->message);
698 } else {
699 return;
702 $this->set('PMA_VERSION_GIT', 1);
703 $this->set('PMA_VERSION_GIT_COMMITHASH', $hash);
704 $this->set('PMA_VERSION_GIT_BRANCH', $branch);
705 $this->set('PMA_VERSION_GIT_MESSAGE', $message);
706 $this->set('PMA_VERSION_GIT_AUTHOR', $author);
707 $this->set('PMA_VERSION_GIT_COMMITTER', $committer);
708 $this->set('PMA_VERSION_GIT_ISREMOTECOMMIT', $is_remote_commit);
709 $this->set('PMA_VERSION_GIT_ISREMOTEBRANCH', $is_remote_branch);
713 * Checks if given URL is 200 or 404, optionally returns data
715 * @param string $link the URL to check
716 * @param boolean $get_body whether to retrieve body of document
718 * @return string|boolean test result or data
720 public function checkHTTP($link, $get_body = false)
722 if (! function_exists('curl_init')) {
723 return null;
725 $handle = curl_init($link);
726 if ($handle === false) {
727 return null;
729 Util::configureCurl($handle);
730 curl_setopt($handle, CURLOPT_FOLLOWLOCATION, 0);
731 curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
732 curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, 0);
733 curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, 0);
734 curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
735 curl_setopt($handle, CURLOPT_TIMEOUT, 5);
736 curl_setopt($handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
737 if (! defined('TESTSUITE')) {
738 session_write_close();
740 $data = @curl_exec($handle);
741 if (! defined('TESTSUITE')) {
742 ini_set('session.use_only_cookies', '0');
743 ini_set('session.use_cookies', '0');
744 ini_set('session.use_trans_sid', '0');
745 ini_set('session.cache_limiter', 'nocache');
746 session_start();
748 if ($data === false) {
749 return null;
751 $http_status = curl_getinfo($handle, CURLINFO_HTTP_CODE);
753 if ($http_status == 200) {
754 return $get_body ? $data : true;
757 if ($http_status == 404) {
758 return false;
760 return null;
764 * loads default values from default source
766 * @return boolean success
768 public function loadDefaults()
770 $cfg = array();
771 if (! file_exists($this->default_source)) {
772 $this->error_config_default_file = true;
773 return false;
775 include $this->default_source;
777 $this->default_source_mtime = filemtime($this->default_source);
779 $this->default_server = $cfg['Servers'][1];
780 unset($cfg['Servers']);
782 $this->default = $cfg;
783 $this->settings = array_replace_recursive($this->settings, $cfg);
785 $this->error_config_default_file = false;
787 return true;
791 * loads configuration from $source, usually the config file
792 * should be called on object creation
794 * @param string $source config file
796 * @return bool
798 public function load($source = null)
800 $this->loadDefaults();
802 if (null !== $source) {
803 $this->setSource($source);
807 * We check and set the font size at this point, to make the font size
808 * selector work also for users without a config.inc.php
810 $this->checkFontsize();
812 if (! $this->checkConfigSource()) {
813 // even if no config file, set collation_connection
814 $this->checkCollationConnection();
815 return false;
818 $cfg = array();
821 * Parses the configuration file, we throw away any errors or
822 * output.
824 $old_error_reporting = error_reporting(0);
825 ob_start();
826 $GLOBALS['pma_config_loading'] = true;
827 $eval_result = include $this->getSource();
828 $GLOBALS['pma_config_loading'] = false;
829 ob_end_clean();
830 error_reporting($old_error_reporting);
832 if ($eval_result === false) {
833 $this->error_config_file = true;
834 } else {
835 $this->error_config_file = false;
836 $this->source_mtime = filemtime($this->getSource());
840 * Backward compatibility code
842 if (!empty($cfg['DefaultTabTable'])) {
843 $cfg['DefaultTabTable'] = str_replace(
844 '_properties',
846 str_replace(
847 'tbl_properties.php',
848 'tbl_sql.php',
849 $cfg['DefaultTabTable']
853 if (!empty($cfg['DefaultTabDatabase'])) {
854 $cfg['DefaultTabDatabase'] = str_replace(
855 '_details',
857 str_replace(
858 'db_details.php',
859 'db_sql.php',
860 $cfg['DefaultTabDatabase']
865 $this->settings = array_replace_recursive($this->settings, $cfg);
867 // Handling of the collation must be done after merging of $cfg
868 // (from config.inc.php) so that $cfg['DefaultConnectionCollation']
869 // can have an effect.
870 $this->checkCollationConnection();
872 return true;
876 * Saves the connection collation
878 * @param array $config_data configuration data from user preferences
880 * @return void
882 private function _saveConnectionCollation($config_data)
884 // just to shorten the lines
885 $collation = 'collation_connection';
886 if (isset($GLOBALS[$collation])
887 && (isset($_COOKIE['pma_collation_connection'])
888 || isset($_POST[$collation]))
890 if ((! isset($config_data[$collation])
891 && $GLOBALS[$collation] != 'utf8_general_ci')
892 || isset($config_data[$collation])
893 && $GLOBALS[$collation] != $config_data[$collation]
895 $this->setUserValue(
896 null,
897 $collation,
898 $GLOBALS[$collation],
899 'utf8_general_ci'
902 } else {
903 // read collation from settings
904 if (isset($config_data[$collation])) {
905 $GLOBALS[$collation]
906 = $config_data[$collation];
907 $this->setCookie(
908 'pma_collation_connection',
909 $GLOBALS[$collation]
916 * Loads user preferences and merges them with current config
917 * must be called after control connection has been established
919 * @return void
921 public function loadUserPreferences()
923 // index.php should load these settings, so that phpmyadmin.css.php
924 // will have everything available in session cache
925 $server = isset($GLOBALS['server'])
926 ? $GLOBALS['server']
927 : (!empty($GLOBALS['cfg']['ServerDefault'])
928 ? $GLOBALS['cfg']['ServerDefault']
929 : 0);
930 $cache_key = 'server_' . $server;
931 if ($server > 0 && !defined('PMA_MINIMUM_COMMON')) {
932 $config_mtime = max($this->default_source_mtime, $this->source_mtime);
933 // cache user preferences, use database only when needed
934 if (! isset($_SESSION['cache'][$cache_key]['userprefs'])
935 || $_SESSION['cache'][$cache_key]['config_mtime'] < $config_mtime
937 // load required libraries
938 include_once './libraries/user_preferences.lib.php';
939 $prefs = PMA_loadUserprefs();
940 $_SESSION['cache'][$cache_key]['userprefs']
941 = PMA_applyUserprefs($prefs['config_data']);
942 $_SESSION['cache'][$cache_key]['userprefs_mtime'] = $prefs['mtime'];
943 $_SESSION['cache'][$cache_key]['userprefs_type'] = $prefs['type'];
944 $_SESSION['cache'][$cache_key]['config_mtime'] = $config_mtime;
946 } elseif ($server == 0
947 || ! isset($_SESSION['cache'][$cache_key]['userprefs'])
949 $this->set('user_preferences', false);
950 return;
952 $config_data = $_SESSION['cache'][$cache_key]['userprefs'];
953 // type is 'db' or 'session'
954 $this->set(
955 'user_preferences',
956 $_SESSION['cache'][$cache_key]['userprefs_type']
958 $this->set(
959 'user_preferences_mtime',
960 $_SESSION['cache'][$cache_key]['userprefs_mtime']
963 // backup some settings
964 $org_fontsize = '';
965 if (isset($this->settings['fontsize'])) {
966 $org_fontsize = $this->settings['fontsize'];
968 // load config array
969 $this->settings = array_replace_recursive($this->settings, $config_data);
970 $GLOBALS['cfg'] = array_replace_recursive($GLOBALS['cfg'], $config_data);
971 if (defined('PMA_MINIMUM_COMMON')) {
972 return;
975 // settings below start really working on next page load, but
976 // changes are made only in index.php so everything is set when
977 // in frames
979 // save theme
980 /** @var ThemeManager $tmanager */
981 $tmanager = $_SESSION['PMA_Theme_Manager'];
982 if ($tmanager->getThemeCookie() || isset($_REQUEST['set_theme'])) {
983 if ((! isset($config_data['ThemeDefault'])
984 && $tmanager->theme->getId() != 'original')
985 || isset($config_data['ThemeDefault'])
986 && $config_data['ThemeDefault'] != $tmanager->theme->getId()
988 // new theme was set in common.inc.php
989 $this->setUserValue(
990 null,
991 'ThemeDefault',
992 $tmanager->theme->getId(),
993 'original'
996 } else {
997 // no cookie - read default from settings
998 if ($this->settings['ThemeDefault'] != $tmanager->theme->getId()
999 && $tmanager->checkTheme($this->settings['ThemeDefault'])
1001 $tmanager->setActiveTheme($this->settings['ThemeDefault']);
1002 $tmanager->setThemeCookie();
1006 // save font size
1007 if ((! isset($config_data['fontsize'])
1008 && $org_fontsize != '82%')
1009 || isset($config_data['fontsize'])
1010 && $org_fontsize != $config_data['fontsize']
1012 $this->setUserValue(null, 'fontsize', $org_fontsize, '82%');
1015 // save language
1016 if (isset($_COOKIE['pma_lang']) || isset($_POST['lang'])) {
1017 if ((! isset($config_data['lang'])
1018 && $GLOBALS['lang'] != 'en')
1019 || isset($config_data['lang'])
1020 && $GLOBALS['lang'] != $config_data['lang']
1022 $this->setUserValue(null, 'lang', $GLOBALS['lang'], 'en');
1024 } else {
1025 // read language from settings
1026 if (isset($config_data['lang']) && PMA_langSet($config_data['lang'])) {
1027 $this->setCookie('pma_lang', $GLOBALS['lang']);
1031 // save connection collation
1032 $this->_saveConnectionCollation($config_data);
1036 * Sets config value which is stored in user preferences (if available)
1037 * or in a cookie.
1039 * If user preferences are not yet initialized, option is applied to
1040 * global config and added to a update queue, which is processed
1041 * by {@link loadUserPreferences()}
1043 * @param string $cookie_name can be null
1044 * @param string $cfg_path configuration path
1045 * @param mixed $new_cfg_value new value
1046 * @param mixed $default_value default value
1048 * @return void
1050 public function setUserValue($cookie_name, $cfg_path, $new_cfg_value,
1051 $default_value = null
1053 // use permanent user preferences if possible
1054 $prefs_type = $this->get('user_preferences');
1055 if ($prefs_type) {
1056 include_once './libraries/user_preferences.lib.php';
1057 if ($default_value === null) {
1058 $default_value = PMA_arrayRead($cfg_path, $this->default);
1060 PMA_persistOption($cfg_path, $new_cfg_value, $default_value);
1062 if ($prefs_type != 'db' && $cookie_name) {
1063 // fall back to cookies
1064 if ($default_value === null) {
1065 $default_value = PMA_arrayRead($cfg_path, $this->settings);
1067 $this->setCookie($cookie_name, $new_cfg_value, $default_value);
1069 PMA_arrayWrite($cfg_path, $GLOBALS['cfg'], $new_cfg_value);
1070 PMA_arrayWrite($cfg_path, $this->settings, $new_cfg_value);
1074 * Reads value stored by {@link setUserValue()}
1076 * @param string $cookie_name cookie name
1077 * @param mixed $cfg_value config value
1079 * @return mixed
1081 public function getUserValue($cookie_name, $cfg_value)
1083 $cookie_exists = isset($_COOKIE) && !empty($_COOKIE[$cookie_name]);
1084 $prefs_type = $this->get('user_preferences');
1085 if ($prefs_type == 'db') {
1086 // permanent user preferences value exists, remove cookie
1087 if ($cookie_exists) {
1088 $this->removeCookie($cookie_name);
1090 } else if ($cookie_exists) {
1091 return $_COOKIE[$cookie_name];
1093 // return value from $cfg array
1094 return $cfg_value;
1098 * set source
1100 * @param string $source source
1102 * @return void
1104 public function setSource($source)
1106 $this->source = trim($source);
1110 * check config source
1112 * @return boolean whether source is valid or not
1114 public function checkConfigSource()
1116 if (! $this->getSource()) {
1117 // no configuration file set at all
1118 return false;
1121 if (! file_exists($this->getSource())) {
1122 $this->source_mtime = 0;
1123 return false;
1126 if (! is_readable($this->getSource())) {
1127 // manually check if file is readable
1128 // might be bug #3059806 Supporting running from CIFS/Samba shares
1130 $contents = false;
1131 $handle = @fopen($this->getSource(), 'r');
1132 if ($handle !== false) {
1133 $contents = @fread($handle, 1); // reading 1 byte is enough to test
1134 fclose($handle);
1136 if ($contents === false) {
1137 $this->source_mtime = 0;
1138 PMA_fatalError(
1139 sprintf(
1140 function_exists('__')
1141 ? __('Existing configuration file (%s) is not readable.')
1142 : 'Existing configuration file (%s) is not readable.',
1143 $this->getSource()
1146 return false;
1150 return true;
1154 * verifies the permissions on config file (if asked by configuration)
1155 * (must be called after config.inc.php has been merged)
1157 * @return void
1159 public function checkPermissions()
1161 // Check for permissions (on platforms that support it):
1162 if ($this->get('CheckConfigurationPermissions')) {
1163 $perms = @fileperms($this->getSource());
1164 if (!($perms === false) && ($perms & 2)) {
1165 // This check is normally done after loading configuration
1166 $this->checkWebServerOs();
1167 if ($this->get('PMA_IS_WINDOWS') == 0) {
1168 $this->source_mtime = 0;
1169 PMA_fatalError(
1171 'Wrong permissions on configuration file, '
1172 . 'should not be world writable!'
1181 * returns specific config setting
1183 * @param string $setting config setting
1185 * @return mixed value
1187 public function get($setting)
1189 if (isset($this->settings[$setting])) {
1190 return $this->settings[$setting];
1192 return null;
1196 * sets configuration variable
1198 * @param string $setting configuration option
1199 * @param mixed $value new value for configuration option
1201 * @return void
1203 public function set($setting, $value)
1205 if (! isset($this->settings[$setting])
1206 || $this->settings[$setting] !== $value
1208 $this->settings[$setting] = $value;
1209 $this->set_mtime = time();
1214 * returns source for current config
1216 * @return string config source
1218 public function getSource()
1220 return $this->source;
1224 * returns a unique value to force a CSS reload if either the config
1225 * or the theme changes
1226 * must also check the pma_fontsize cookie in case there is no
1227 * config file
1229 * @return int Summary of unix timestamps and fontsize,
1230 * to be unique on theme parameters change
1232 public function getThemeUniqueValue()
1234 if (null !== $this->get('fontsize')) {
1235 $fontsize = intval($this->get('fontsize'));
1236 } elseif (isset($_COOKIE['pma_fontsize'])) {
1237 $fontsize = intval($_COOKIE['pma_fontsize']);
1238 } else {
1239 $fontsize = 0;
1241 return (
1242 $fontsize +
1243 $this->source_mtime +
1244 $this->default_source_mtime +
1245 $this->get('user_preferences_mtime') +
1246 $_SESSION['PMA_Theme']->mtime_info +
1247 $_SESSION['PMA_Theme']->filesize_info);
1251 * Sets collation_connection based on user preference. First is checked
1252 * value from request, then cookies with fallback to default.
1254 * After setting it here, cookie is set in common.inc.php to persist
1255 * the selection.
1257 * @todo check validity of collation string
1259 * @return void
1261 public function checkCollationConnection()
1263 if (! empty($_REQUEST['collation_connection'])) {
1264 $collation = strip_tags($_REQUEST['collation_connection']);
1265 } elseif (! empty($_COOKIE['pma_collation_connection'])) {
1266 $collation = strip_tags($_COOKIE['pma_collation_connection']);
1267 } else {
1268 $collation = $this->get('DefaultConnectionCollation');
1270 $this->set('collation_connection', $collation);
1274 * checks for font size configuration, and sets font size as requested by user
1276 * @return void
1278 public function checkFontsize()
1280 $new_fontsize = '';
1282 if (isset($_GET['set_fontsize'])) {
1283 $new_fontsize = $_GET['set_fontsize'];
1284 } elseif (isset($_POST['set_fontsize'])) {
1285 $new_fontsize = $_POST['set_fontsize'];
1286 } elseif (isset($_COOKIE['pma_fontsize'])) {
1287 $new_fontsize = $_COOKIE['pma_fontsize'];
1290 if (preg_match('/^[0-9.]+(px|em|pt|\%)$/', $new_fontsize)) {
1291 $this->set('fontsize', $new_fontsize);
1292 } elseif (! $this->get('fontsize')) {
1293 // 80% would correspond to the default browser font size
1294 // of 16, but use 82% to help read the monoface font
1295 $this->set('fontsize', '82%');
1298 $this->setCookie('pma_fontsize', $this->get('fontsize'), '82%');
1302 * checks if upload is enabled
1304 * @return void
1306 public function checkUpload()
1308 if (!ini_get('file_uploads')) {
1309 $this->set('enable_upload', false);
1310 return;
1313 $this->set('enable_upload', true);
1314 // if set "php_admin_value file_uploads Off" in httpd.conf
1315 // ini_get() also returns the string "Off" in this case:
1316 if ('off' == strtolower(ini_get('file_uploads'))) {
1317 $this->set('enable_upload', false);
1322 * Maximum upload size as limited by PHP
1323 * Used with permission from Moodle (http://moodle.org) by Martin Dougiamas
1325 * this section generates $max_upload_size in bytes
1327 * @return void
1329 public function checkUploadSize()
1331 if (! $filesize = ini_get('upload_max_filesize')) {
1332 $filesize = "5M";
1335 if ($postsize = ini_get('post_max_size')) {
1336 $this->set(
1337 'max_upload_size',
1338 min(PMA_getRealSize($filesize), PMA_getRealSize($postsize))
1340 } else {
1341 $this->set('max_upload_size', PMA_getRealSize($filesize));
1346 * Checks if protocol is https
1348 * This function checks if the https protocol on the active connection.
1350 * @return bool
1352 public function isHttps()
1355 if (null !== $this->get('is_https')) {
1356 return $this->get('is_https');
1359 $is_https = false;
1360 if (strtolower(PMA_getenv('HTTP_SCHEME')) == 'https') {
1361 $is_https = true;
1362 } elseif (strtolower(PMA_getenv('HTTPS')) == 'on') {
1363 $is_https = true;
1364 } elseif (substr(strtolower(PMA_getenv('REQUEST_URI')), 0, 6) == 'https:') {
1365 $is_https = true;
1366 } elseif (strtolower(PMA_getenv('HTTP_HTTPS_FROM_LB')) == 'on') {
1367 // A10 Networks load balancer
1368 $is_https = true;
1369 } elseif (strtolower(PMA_getenv('HTTP_FRONT_END_HTTPS')) == 'on') {
1370 $is_https = true;
1371 } elseif (strtolower(PMA_getenv('HTTP_X_FORWARDED_PROTO')) == 'https') {
1372 $is_https = true;
1373 } elseif (PMA_getenv('SERVER_PORT') == 443) {
1374 $is_https = true;
1377 $this->set('is_https', $is_https);
1379 return $is_https;
1383 * Get cookie path
1385 * @return string
1387 public function getCookiePath()
1389 static $cookie_path = null;
1391 if (null !== $cookie_path && !defined('TESTSUITE')) {
1392 return $cookie_path;
1395 if (isset($GLOBALS['PMA_PHP_SELF'])) {
1396 $parsed_url = parse_url($GLOBALS['PMA_PHP_SELF']);
1397 } else {
1398 $parsed_url = parse_url(PMA_getenv('REQUEST_URI'));
1401 $cookie_path = $parsed_url['path'];
1403 /* Remove filename */
1404 if (substr($cookie_path, -4) == '.php') {
1405 $cookie_path = dirname($cookie_path);
1408 /* Remove extra path from javascript calls */
1409 if (defined('PMA_PATH_TO_BASEDIR')) {
1410 $cookie_path = dirname($cookie_path);
1413 if (substr($cookie_path, -1) != '/') {
1414 $cookie_path = $cookie_path . '/';
1417 return $cookie_path;
1421 * enables backward compatibility
1423 * @return void
1425 public function enableBc()
1427 $GLOBALS['cfg'] = $this->settings;
1428 $GLOBALS['default_server'] = $this->default_server;
1429 unset($this->default_server);
1430 $GLOBALS['collation_connection'] = $this->get('collation_connection');
1431 $GLOBALS['is_upload'] = $this->get('enable_upload');
1432 $GLOBALS['max_upload_size'] = $this->get('max_upload_size');
1433 $GLOBALS['is_https'] = $this->get('is_https');
1435 $defines = array(
1436 'PMA_VERSION',
1437 'PMA_THEME_VERSION',
1438 'PMA_THEME_GENERATION',
1439 'PMA_IS_WINDOWS',
1440 'PMA_IS_IIS',
1441 'PMA_IS_GD2',
1442 'PMA_USR_OS',
1443 'PMA_USR_BROWSER_VER',
1444 'PMA_USR_BROWSER_AGENT'
1447 foreach ($defines as $define) {
1448 if (! defined($define)) {
1449 define($define, $this->get($define));
1455 * returns options for font size selection
1457 * @param string $current_size current selected font size with unit
1459 * @return array selectable font sizes
1461 protected static function getFontsizeOptions($current_size = '82%')
1463 $unit = preg_replace('/[0-9.]*/', '', $current_size);
1464 $value = preg_replace('/[^0-9.]*/', '', $current_size);
1466 $factors = array();
1467 $options = array();
1468 $options["$value"] = $value . $unit;
1470 if ($unit === '%') {
1471 $factors[] = 1;
1472 $factors[] = 5;
1473 $factors[] = 10;
1474 } elseif ($unit === 'em') {
1475 $factors[] = 0.05;
1476 $factors[] = 0.2;
1477 $factors[] = 1;
1478 } elseif ($unit === 'pt') {
1479 $factors[] = 0.5;
1480 $factors[] = 2;
1481 } elseif ($unit === 'px') {
1482 $factors[] = 1;
1483 $factors[] = 5;
1484 $factors[] = 10;
1485 } else {
1486 //unknown font size unit
1487 $factors[] = 0.05;
1488 $factors[] = 0.2;
1489 $factors[] = 1;
1490 $factors[] = 5;
1491 $factors[] = 10;
1494 foreach ($factors as $key => $factor) {
1495 $option_inc = $value + $factor;
1496 $option_dec = $value - $factor;
1497 while (count($options) < 21) {
1498 $options["$option_inc"] = $option_inc . $unit;
1499 if ($option_dec > $factors[0]) {
1500 $options["$option_dec"] = $option_dec . $unit;
1502 $option_inc += $factor;
1503 $option_dec -= $factor;
1504 if (isset($factors[$key + 1])
1505 && $option_inc >= $value + $factors[$key + 1]
1507 break;
1511 ksort($options);
1512 return $options;
1516 * returns html selectbox for font sizes
1518 * @return string html selectbox
1520 protected static function getFontsizeSelection()
1522 $current_size = $GLOBALS['PMA_Config']->get('fontsize');
1523 // for the case when there is no config file (this is supported)
1524 if (empty($current_size)) {
1525 if (isset($_COOKIE['pma_fontsize'])) {
1526 $current_size = htmlspecialchars($_COOKIE['pma_fontsize']);
1527 } else {
1528 $current_size = '82%';
1531 $options = Config::getFontsizeOptions($current_size);
1533 $return = '<label for="select_fontsize">' . __('Font size')
1534 . ':</label>' . "\n"
1535 . '<select name="set_fontsize" id="select_fontsize"'
1536 . ' class="autosubmit">' . "\n";
1537 foreach ($options as $option) {
1538 $return .= '<option value="' . $option . '"';
1539 if ($option == $current_size) {
1540 $return .= ' selected="selected"';
1542 $return .= '>' . $option . '</option>' . "\n";
1544 $return .= '</select>';
1546 return $return;
1550 * return complete font size selection form
1552 * @return string html selectbox
1554 public static function getFontsizeForm()
1556 return '<form name="form_fontsize_selection" id="form_fontsize_selection"'
1557 . ' method="get" action="index.php" class="disableAjax">' . "\n"
1558 . PMA_URL_getHiddenInputs() . "\n"
1559 . Config::getFontsizeSelection() . "\n"
1560 . '</form>';
1564 * removes cookie
1566 * @param string $cookie name of cookie to remove
1568 * @return boolean result of setcookie()
1570 public function removeCookie($cookie)
1572 if (defined('TESTSUITE')) {
1573 if (isset($_COOKIE[$cookie])) {
1574 unset($_COOKIE[$cookie]);
1576 return true;
1578 return setcookie(
1579 $cookie,
1581 time() - 3600,
1582 $this->getCookiePath(),
1584 $this->isHttps()
1589 * sets cookie if value is different from current cookie value,
1590 * or removes if value is equal to default
1592 * @param string $cookie name of cookie to remove
1593 * @param mixed $value new cookie value
1594 * @param string $default default value
1595 * @param int $validity validity of cookie in seconds (default is one month)
1596 * @param bool $httponly whether cookie is only for HTTP (and not for scripts)
1598 * @return boolean result of setcookie()
1600 public function setCookie($cookie, $value, $default = null,
1601 $validity = null, $httponly = true
1603 if (mb_strlen($value) && null !== $default && $value === $default
1605 // default value is used
1606 if (isset($_COOKIE[$cookie])) {
1607 // remove cookie
1608 return $this->removeCookie($cookie);
1610 return false;
1613 if (!mb_strlen($value) && isset($_COOKIE[$cookie])) {
1614 // remove cookie, value is empty
1615 return $this->removeCookie($cookie);
1618 if (! isset($_COOKIE[$cookie]) || $_COOKIE[$cookie] !== $value) {
1619 // set cookie with new value
1620 /* Calculate cookie validity */
1621 if ($validity === null) {
1622 $validity = time() + 2592000;
1623 } elseif ($validity == 0) {
1624 $validity = 0;
1625 } else {
1626 $validity = time() + $validity;
1628 if (defined('TESTSUITE')) {
1629 $_COOKIE[$cookie] = $value;
1630 return true;
1632 return setcookie(
1633 $cookie,
1634 $value,
1635 $validity,
1636 $this->getCookiePath(),
1638 $this->isHttps(),
1639 $httponly
1643 // cookie has already $value as value
1644 return true;
1649 * Error handler to catch fatal errors when loading configuration
1650 * file
1653 * PMA_Config_fatalErrorHandler
1654 * @return void
1656 public static function fatalErrorHandler()
1658 if (!isset($GLOBALS['pma_config_loading'])
1659 || !$GLOBALS['pma_config_loading']
1661 return;
1664 $error = error_get_last();
1665 if ($error === null) {
1666 return;
1669 PMA_fatalError(
1670 sprintf(
1671 'Failed to load phpMyAdmin configuration (%s:%s): %s',
1672 Error::relPath($error['file']),
1673 $error['line'],
1674 $error['message']
1680 if (!defined('TESTSUITE')) {
1681 register_shutdown_function(array('PMA\libraries\Config', 'fatalErrorHandler'));