Merge pull request #431 from xmujay/0609_monitor
[phpmyadmin/aamir.git] / libraries / Config.class.php
blob9a576ebd6c8f4c0bdcd6c429b67a6047d14dcb80
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Configuration handling.
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * Load vendor configuration.
15 require_once './libraries/vendor_config.php';
17 /**
18 * Configuration class
20 * @package PhpMyAdmin
22 class PMA_Config
24 /**
25 * @var string default config source
27 var $default_source = './libraries/config.default.php';
29 /**
30 * @var array default configuration settings
32 var $default = array();
34 /**
35 * @var array configuration settings
37 var $settings = array();
39 /**
40 * @var string config source
42 var $source = '';
44 /**
45 * @var int source modification time
47 var $source_mtime = 0;
48 var $default_source_mtime = 0;
49 var $set_mtime = 0;
51 /**
52 * @var boolean
54 var $error_config_file = false;
56 /**
57 * @var boolean
59 var $error_config_default_file = false;
61 /**
62 * @var boolean
64 var $error_pma_uri = false;
66 /**
67 * @var array
69 var $default_server = array();
71 /**
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
76 var $done = false;
78 /**
79 * constructor
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
88 // PMA_Config::load()
89 $this->load($source);
91 // other settings, independent from config file, comes in
92 $this->checkSystem();
94 $this->isHttps();
97 /**
98 * sets system and application settings
100 * @return void
102 function checkSystem()
104 $this->set('PMA_VERSION', '4.1-dev');
106 * @deprecated
108 $this->set('PMA_THEME_VERSION', 2);
110 * @deprecated
112 $this->set('PMA_THEME_GENERATION', 2);
114 $this->checkPhpVersion();
115 $this->checkWebServerOs();
116 $this->checkWebServer();
117 $this->checkGd2();
118 $this->checkClient();
119 $this->checkUpload();
120 $this->checkUploadSize();
121 $this->checkOutputCompression();
125 * whether to use gzip output compression or not
127 * @return void
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);
144 } else {
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
156 * @return void
158 function checkClient()
160 if (PMA_getenv('HTTP_USER_AGENT')) {
161 $HTTP_USER_AGENT = PMA_getenv('HTTP_USER_AGENT');
162 } else {
163 $HTTP_USER_AGENT = '';
166 // 1. Platform
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');
177 } else {
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})@',
186 $HTTP_USER_AGENT,
187 $mozilla_version
190 if (preg_match(
191 '@Opera(/| )([0-9].[0-9]{1,2})@',
192 $HTTP_USER_AGENT,
193 $log_version
194 )) {
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})@',
199 $HTTP_USER_AGENT,
200 $log_version
201 )) {
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})@',
206 $HTTP_USER_AGENT,
207 $log_version
208 )) {
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/)(.*)(;)@',
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', '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');
226 // newer Safari
227 } elseif ($is_mozilla
228 && preg_match('@Version/(.*) Safari@', $HTTP_USER_AGENT, $log_version)
230 $this->set(
231 'PMA_USR_BROWSER_VER', $log_version[1]
233 $this->set('PMA_USR_BROWSER_AGENT', 'SAFARI');
234 // older Safari
235 } elseif ($is_mozilla
236 && preg_match('@Safari/([0-9]*)@', $HTTP_USER_AGENT, $log_version)
238 $this->set(
239 'PMA_USR_BROWSER_VER', $mozilla_version[1] . '.' . $log_version[1]
241 $this->set('PMA_USR_BROWSER_AGENT', 'SAFARI');
242 // Firefox
243 } elseif (! strstr($HTTP_USER_AGENT, 'compatible')
244 && preg_match('@Firefox/([\w.]+)@', $HTTP_USER_AGENT, $log_version)
246 $this->set(
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');
256 } else {
257 $this->set('PMA_USR_BROWSER_VER', 0);
258 $this->set('PMA_USR_BROWSER_AGENT', 'OTHER');
263 * Whether GD2 is present
265 * @return void
267 function checkGd2()
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);
273 } else {
274 if (!@function_exists('imagecreatetruecolor')) {
275 $this->set('PMA_IS_GD2', 0);
276 } else {
277 if (@function_exists('gd_info')) {
278 $gd_nfo = gd_info();
279 if (strstr($gd_nfo["GD Version"], '2.')) {
280 $this->set('PMA_IS_GD2', 1);
281 } else {
282 $this->set('PMA_IS_GD2', 0);
284 } else {
285 $this->set('PMA_IS_GD2', 0);
292 * Whether the Web server php is running on is IIS
294 * @return void
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);
305 } else {
306 $this->set('PMA_IS_IIS', 0);
311 * Whether the os php is running on is windows or not
313 * @return void
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
334 * @return void
336 function checkPhpVersion()
338 $match = array();
339 if (! preg_match(
340 '@([0-9]{1,2}).([0-9]{1,2}).([0-9]{1,2})@',
341 phpversion(),
342 $match
343 )) {
344 preg_match(
345 '@([0-9]{1,2}).([0-9]{1,2})@',
346 phpversion(),
347 $match
350 if (isset($match) && ! empty($match[1])) {
351 if (! isset($match[2])) {
352 $match[2] = 0;
354 if (! isset($match[3])) {
355 $match[3] = 0;
357 $this->set(
358 'PMA_PHP_INT_VERSION',
359 (int) sprintf('%d%02d%02d', $match[1], $match[2], $match[3])
361 } else {
362 $this->set('PMA_PHP_INT_VERSION', 0);
364 $this->set('PMA_PHP_STR_VERSION', phpversion());
368 * detects if Git revision
370 * @return boolean
372 function isGitRevision()
374 // caching
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;
387 return false;
389 $_SESSION['is_git_revision'] = true;
390 return true;
394 * detects Git revision, if running inside repo
396 * @return void
398 function checkGitRevision()
400 // find out if there is a .git folder
401 $git_folder = '.git';
402 if (! $this->isGitRevision()) {
403 return;
406 if (! $ref_head = @file_get_contents($git_folder . '/HEAD')) {
407 return;
409 $branch = false;
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);
415 } else {
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)) {
422 return;
424 $hash = trim($hash);
425 } else {
426 // deal with packed refs
427 if (! $packed_refs = @file_get_contents($git_folder . '/packed-refs')) {
428 return;
430 // split file to lines
431 $ref_lines = explode("\n", $packed_refs);
432 foreach ($ref_lines as $line) {
433 // skip comments
434 if ($line[0] == '#') {
435 continue;
437 // parse line
438 $parts = explode(' ', $line);
439 // care only about named refs
440 if (count($parts) != 2) {
441 continue;
443 // have found our ref?
444 if ($parts[1] == $ref_head) {
445 $hash = $parts[0];
446 break;
449 if (! isset($hash)) {
450 // Could not find ref
451 return;
454 } else {
455 $hash = trim($ref_head);
458 $commit = false;
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)) {
464 return;
466 $commit = explode("\0", gzuncompress($commit), 2);
467 $commit = explode("\n", $commit[1]);
468 $_SESSION['PMA_VERSION_COMMITDATA_' . $hash] = $commit;
469 } else {
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) {
476 // skip blank lines
477 if (strlen(trim($line)) == 0) {
478 continue;
480 // skip non pack lines
481 if ($line[0] != 'P') {
482 continue;
484 // parse names
485 $pack_names[] = substr($line, 2);
487 } else {
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
492 // files instead
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);
508 // load index
509 if (! $index_data = @file_get_contents($git_folder . '/objects/pack/' . $index_name)) {
510 continue;
512 // check format
513 if (substr($index_data, 0, 4) != "\377tOc") {
514 continue;
516 // check version
517 $version = unpack('N', substr($index_data, 4, 4));
518 if ($version[1] != 2) {
519 continue;
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) {
529 $start = 0;
530 } else {
531 $start = $fanout[$firstbyte];
533 $end = $fanout[$firstbyte + 1];
535 // stupid linear search for our sha
536 $position = $start;
537 $found = false;
538 $offset = 8 + (256 * 4);
539 for ($position = $start; $position < $end; $position++) {
540 $sha = strtolower(
541 bin2hex(
542 substr(
543 $index_data, $offset + ($position * 20), 20
547 if ($sha == $hash) {
548 $found = true;
549 break;
552 if (! $found) {
553 continue;
555 // read pack offset
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];
562 // open pack file
563 $pack_file = fopen(
564 $git_folder . '/objects/pack/' . $pack_name, 'rb'
566 if ($pack_file === false) {
567 continue;
569 // seek to start
570 fseek($pack_file, $pack_offset);
572 // parse header
573 $header = ord(fread($pack_file, 1));
574 $type = ($header >> 4) & 7;
575 $hasnext = ($header & 128) >> 7;
576 $size = $header & 0xf;
577 $offset = 4;
579 while ($hasnext) {
580 $byte = ord(fread($pack_file, 1));
581 $size |= ($byte & 0x7f) << $offset;
582 $hasnext = ($byte & 128) >> 7;
583 $offset += 7;
586 // we care only about commit objects
587 if ($type != 1) {
588 continue;
591 // read data
592 $commit = fread($pack_file, $size);
593 $commit = gzuncompress($commit);
594 $commit = explode("\n", $commit);
595 $_SESSION['PMA_VERSION_COMMITDATA_' . $hash] = $commit;
596 fclose($pack_file);
599 } else {
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];
609 } else {
610 $link = 'https://api.github.com/repos/phpmyadmin/phpmyadmin/git/commits/'
611 . $hash;
612 $is_found = $this->checkHTTP($link, ! $commit);
613 switch($is_found) {
614 case false:
615 $is_remote_commit = false;
616 $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = false;
617 break;
618 case null:
619 // no remote link for now, but don't cache this as Github is down
620 $is_remote_commit = false;
621 break;
622 default:
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);
629 break;
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];
638 } else {
639 $link = 'https://api.github.com/repos/phpmyadmin/phpmyadmin'
640 . '/git/trees/' . $branch;
641 $is_found = $this->checkHTTP($link);
642 switch($is_found) {
643 case true:
644 $is_remote_branch = true;
645 $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = true;
646 break;
647 case false:
648 $is_remote_branch = false;
649 $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = false;
650 break;
651 case null:
652 // no remote link for now, but don't cache this as Github is down
653 $is_remote_branch = false;
654 break;
659 if ($commit !== false) {
660 $author = array('name' => '', 'email' => '', 'date' => '');
661 $committer = array('name' => '', 'email' => '', 'date' => '');
663 do {
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);
670 $user2 = array(
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];
677 $$linetype = $user2;
679 } while ($dataline != '');
680 $message = trim(implode(' ', $commit));
682 } elseif (isset($commit_json)) {
683 $author = array(
684 'name' => $commit_json->author->name,
685 'email' => $commit_json->author->email,
686 'date' => $commit_json->author->date);
687 $committer = array(
688 'name' => $commit_json->committer->name,
689 'email' => $commit_json->committer->email,
690 'date' => $commit_json->committer->date);
691 $message = trim($commit_json->message);
692 } else {
693 return;
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')) {
717 return null;
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) {
730 return null;
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) {
737 return false;
739 return null;
743 * loads default values from default source
745 * @return boolean success
747 function loadDefaults()
749 $cfg = array();
750 if (! file_exists($this->default_source)) {
751 $this->error_config_default_file = true;
752 return false;
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;
766 return true;
770 * loads configuration from $source, usally the config file
771 * should be called on object creation
773 * @param string $source config file
775 * @return bool
777 function load($source = null)
779 $this->loadDefaults();
781 if (null !== $source) {
782 $this->setSource($source);
785 if (! $this->checkConfigSource()) {
786 return false;
789 $cfg = array();
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;
801 } else {
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(
811 '_properties',
813 str_replace(
814 'tbl_properties.php',
815 'tbl_sql.php',
816 $cfg['DefaultTabTable']
820 if (!empty($cfg['DefaultTabDatabase'])) {
821 $cfg['DefaultTabDatabase'] = str_replace(
822 '_details',
824 str_replace(
825 'db_details.php',
826 'db_sql.php',
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'])) {
845 $this->set(
846 'collation_connection',
847 strip_tags($_COOKIE['pma_collation_connection'])
849 } else {
850 $this->set(
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();
862 return true;
866 * Loads user preferences and merges them with current config
867 * must be called after control connection has been estabilished
869 * @return boolean
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'])
876 ? $GLOBALS['server']
877 : (!empty($GLOBALS['cfg']['ServerDefault'])
878 ? $GLOBALS['cfg']['ServerDefault']
879 : 0);
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);
900 return;
902 $config_data = $_SESSION['cache'][$cache_key]['userprefs'];
903 // type is 'db' or 'session'
904 $this->set(
905 'user_preferences',
906 $_SESSION['cache'][$cache_key]['userprefs_type']
908 $this->set(
909 'user_preferences_mtime',
910 $_SESSION['cache'][$cache_key]['userprefs_mtime']
913 // backup some settings
914 $org_fontsize = '';
915 if (isset($this->settings['fontsize'])) {
916 $org_fontsize = $this->settings['fontsize'];
918 // load config array
919 $this->settings = PMA_arrayMergeRecursive($this->settings, $config_data);
920 $GLOBALS['cfg'] = PMA_arrayMergeRecursive($GLOBALS['cfg'], $config_data);
921 if (defined('PMA_MINIMUM_COMMON')) {
922 return;
925 // settings below start really working on next page load, but
926 // changes are made only in index.php so everything is set when
927 // in frames
929 // save theme
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
938 $this->setUserValue(
939 null,
940 'ThemeDefault',
941 $tmanager->theme->getId(),
942 'original'
945 } else {
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();
955 // save font size
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%');
964 // save language
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');
973 } else {
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']
989 $this->setUserValue(
990 null,
991 'collation_connection',
992 $GLOBALS['collation_connection'],
993 'utf8_general_ci'
996 } else {
997 // read collation from settings
998 if (isset($config_data['collation_connection'])) {
999 $GLOBALS['collation_connection']
1000 = $config_data['collation_connection'];
1001 $this->setCookie(
1002 'pma_collation_connection',
1003 $GLOBALS['collation_connection']
1010 * Sets config value which is stored in user preferences (if available)
1011 * or in a cookie.
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
1022 * @return void
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');
1029 if ($prefs_type) {
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
1053 * @return mixed
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
1068 return $cfg_value;
1072 * set source
1074 * @param string $source source
1076 * @return void
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
1092 return false;
1095 if (! file_exists($this->getSource())) {
1096 $this->source_mtime = 0;
1097 return false;
1100 if (! is_readable($this->getSource())) {
1101 // manually check if file is readable
1102 // might be bug #3059806 Supporting running from CIFS/Samba shares
1104 $contents = false;
1105 $handle = @fopen($this->getSource(), 'r');
1106 if ($handle !== false) {
1107 $contents = @fread($handle, 1); // reading 1 byte is enough to test
1108 @fclose($handle);
1110 if ($contents === false) {
1111 $this->source_mtime = 0;
1112 PMA_fatalError(
1113 sprintf(
1114 function_exists('__')
1115 ? __('Existing configuration file (%s) is not readable.')
1116 : 'Existing configuration file (%s) is not readable.',
1117 $this->getSource()
1120 return false;
1124 return true;
1128 * verifies the permissions on config file (if asked by configuration)
1129 * (must be called after config.inc.php has been merged)
1131 * @return void
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!');
1146 } else {
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];
1167 return null;
1171 * sets configuration variable
1173 * @param string $setting configuration option
1174 * @param mixed $value new value for configuration option
1176 * @return void
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
1202 * config file
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']);
1213 } else {
1214 $fontsize = 0;
1216 return (
1217 $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
1228 * record might fail
1230 * @return bool
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) {
1240 $url = array();
1242 // If we don't have scheme, we didn't have full URL so we need to
1243 // dig deeper
1244 if (empty($url['scheme'])) {
1245 // Scheme
1246 if ($is_https) {
1247 $url['scheme'] = 'https';
1248 } else {
1249 $url['scheme'] = 'http';
1252 // Host and port
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'])) {
1260 $url = $parsed_url;
1261 } else {
1262 $url['host'] = PMA_getenv('HTTP_HOST');
1264 } elseif (PMA_getenv('SERVER_NAME')) {
1265 $url['host'] = PMA_getenv('SERVER_NAME');
1266 } else {
1267 $this->error_pma_uri = true;
1268 return false;
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 .= '@';
1293 // Add hostname
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'));
1310 } else {
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)));
1318 } else {
1319 $path = dirname(dirname($path));
1323 // PHP's dirname function would have returned a dot
1324 // when $path contains no slash
1325 if ($path == '.') {
1326 $path = '';
1328 // in vhost situations, there could be already an ending slash
1329 if (substr($path, -1) != '/') {
1330 $path .= '/';
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.
1338 } else {
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
1343 // does not exist.
1344 if (substr($pma_absolute_uri, -1) != '/') {
1345 $pma_absolute_uri .= '/';
1348 // If URI doesn't start with http:// or https://, we will add
1349 // this.
1350 if (substr($pma_absolute_uri, 0, 7) != 'http://'
1351 && substr($pma_absolute_uri, 0, 8) != 'https://'
1353 $pma_absolute_uri
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()
1369 // grab current URL
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');
1382 } else {
1383 $port_number = 443;
1385 return 'https://' . $parsed['host'] . ':' . $port_number . $parsed['path'];
1389 * check selected collation_connection
1391 * @todo check validity of $_REQUEST['collation_connection']
1393 * @return void
1395 function checkCollationConnection()
1397 if (! empty($_REQUEST['collation_connection'])) {
1398 $this->set(
1399 'collation_connection',
1400 strip_tags($_REQUEST['collation_connection'])
1406 * checks for font size configuration, and sets font size as requested by user
1408 * @return void
1410 function checkFontsize()
1412 $new_fontsize = '';
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
1436 * @return void
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);
1447 } else {
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
1458 * @return void
1460 function checkUploadSize()
1462 if (! $filesize = ini_get('upload_max_filesize')) {
1463 $filesize = "5M";
1466 if ($postsize = ini_get('post_max_size')) {
1467 $this->set(
1468 'max_upload_size',
1469 min(PMA_getRealSize($filesize), PMA_getRealSize($postsize))
1471 } else {
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.
1483 * @return bool
1485 public function isHttps()
1488 if (null !== $this->get('is_https')) {
1489 return $this->get('is_https');
1492 $url = parse_url($this->get('PmaAbsoluteUri'));
1493 $is_https = null;
1495 if (isset($url['scheme']) && $url['scheme'] == 'https') {
1496 $is_https = true;
1497 } else {
1498 $is_https = false;
1501 $this->set('is_https', $is_https);
1503 return $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.
1514 * @return bool
1516 function detectHttps()
1518 $is_https = false;
1520 $url = array();
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) {
1527 $url = array();
1531 // If we don't have scheme, we didn't have full URL so we need to
1532 // dig deeper
1533 if (empty($url['scheme'])) {
1534 // 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';
1547 } else {
1548 $url['scheme'] = 'http';
1552 if (isset($url['scheme']) && $url['scheme'] == 'https') {
1553 $is_https = true;
1554 } else {
1555 $is_https = false;
1558 return $is_https;
1562 * detect correct cookie path
1564 * @return void
1566 function checkCookiePath()
1568 $this->set('cookie_path', $this->getCookiePath());
1572 * Get cookie path
1574 * @return string
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
1594 * @return void
1596 function enableBc()
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');
1607 $defines = array(
1608 'PMA_VERSION',
1609 'PMA_THEME_VERSION',
1610 'PMA_THEME_GENERATION',
1611 'PMA_PHP_STR_VERSION',
1612 'PMA_PHP_INT_VERSION',
1613 'PMA_IS_WINDOWS',
1614 'PMA_IS_IIS',
1615 'PMA_IS_GD2',
1616 'PMA_USR_OS',
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
1635 * @static
1637 static protected function getFontsizeOptions($current_size = '82%')
1639 $unit = preg_replace('/[0-9.]*/', '', $current_size);
1640 $value = preg_replace('/[^0-9.]*/', '', $current_size);
1642 $factors = array();
1643 $options = array();
1644 $options["$value"] = $value . $unit;
1646 if ($unit === '%') {
1647 $factors[] = 1;
1648 $factors[] = 5;
1649 $factors[] = 10;
1650 } elseif ($unit === 'em') {
1651 $factors[] = 0.05;
1652 $factors[] = 0.2;
1653 $factors[] = 1;
1654 } elseif ($unit === 'pt') {
1655 $factors[] = 0.5;
1656 $factors[] = 2;
1657 } elseif ($unit === 'px') {
1658 $factors[] = 1;
1659 $factors[] = 5;
1660 $factors[] = 10;
1661 } else {
1662 //unknown font size unit
1663 $factors[] = 0.05;
1664 $factors[] = 0.2;
1665 $factors[] = 1;
1666 $factors[] = 5;
1667 $factors[] = 10;
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]
1683 break;
1687 ksort($options);
1688 return $options;
1692 * returns html selectbox for font sizes
1694 * @static
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'];
1705 } else {
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>';
1724 return $return;
1728 * return complete font size selection form
1730 * @static
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"
1740 . '</form>';
1744 * removes cookie
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]);
1756 return true;
1758 return setcookie(
1759 $cookie,
1761 time() - 3600,
1762 $this->getCookiePath(),
1764 $this->isHttps()
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,
1781 $httponly = true
1783 if (strlen($value) && null !== $default && $value === $default) {
1784 // default value is used
1785 if (isset($_COOKIE[$cookie])) {
1786 // remove cookie
1787 return $this->removeCookie($cookie);
1789 return false;
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) {
1803 $validity = 0;
1804 } else {
1805 $validity = time() + $validity;
1807 if (defined('TESTSUITE')) {
1808 $_COOKIE[$cookie] = $value;
1809 return true;
1811 return setcookie(
1812 $cookie,
1813 $value,
1814 $validity,
1815 $this->getCookiePath(),
1817 $this->isHttps(),
1818 $httponly
1822 // cookie has already $value as value
1823 return true;