Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / Config.class.php
blob57b643ea3dfc601f588401dff64f385f015f501d
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->checkIsHttps();
97 /**
98 * sets system and application settings
100 * @return void
102 function checkSystem()
104 $this->set('PMA_VERSION', '4.0.4');
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 if (preg_match(
185 '@Opera(/| )([0-9].[0-9]{1,2})@',
186 $HTTP_USER_AGENT,
187 $log_version
188 )) {
189 $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
190 $this->set('PMA_USR_BROWSER_AGENT', 'OPERA');
191 } elseif (preg_match(
192 '@MSIE ([0-9].[0-9]{1,2})@',
193 $HTTP_USER_AGENT,
194 $log_version
195 )) {
196 $this->set('PMA_USR_BROWSER_VER', $log_version[1]);
197 $this->set('PMA_USR_BROWSER_AGENT', 'IE');
198 } elseif (preg_match(
199 '@OmniWeb/([0-9].[0-9]{1,2})@',
200 $HTTP_USER_AGENT,
201 $log_version
202 )) {
203 $this->set('PMA_USR_BROWSER_VER', $log_version[1]);
204 $this->set('PMA_USR_BROWSER_AGENT', 'OMNIWEB');
205 // Konqueror 2.2.2 says Konqueror/2.2.2
206 // Konqueror 3.0.3 says Konqueror/3
207 } elseif (preg_match(
208 '@(Konqueror/)(.*)(;)@',
209 $HTTP_USER_AGENT,
210 $log_version
211 )) {
212 $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
213 $this->set('PMA_USR_BROWSER_AGENT', 'KONQUEROR');
214 // must check Chrome before Safari
215 } elseif (preg_match(
216 '@Mozilla/([0-9].[0-9]{1,2})@',
217 $HTTP_USER_AGENT,
218 $log_version)
219 && preg_match('@Chrome/([0-9]*)@', $HTTP_USER_AGENT, $log_version2)
221 $this->set('PMA_USR_BROWSER_VER', $log_version[1] . '.' . $log_version2[1]);
222 $this->set('PMA_USR_BROWSER_AGENT', 'CHROME');
223 // newer Safari
224 } elseif (preg_match(
225 '@Mozilla/([0-9].[0-9]{1,2})@',
226 $HTTP_USER_AGENT,
227 $log_version)
228 && preg_match('@Version/(.*) Safari@', $HTTP_USER_AGENT, $log_version2)
230 $this->set(
231 'PMA_USR_BROWSER_VER', $log_version2[1]
233 $this->set('PMA_USR_BROWSER_AGENT', 'SAFARI');
234 // older Safari
235 } elseif (preg_match(
236 '@Mozilla/([0-9].[0-9]{1,2})@',
237 $HTTP_USER_AGENT,
238 $log_version)
239 && preg_match('@Safari/([0-9]*)@', $HTTP_USER_AGENT, $log_version2)
241 $this->set(
242 'PMA_USR_BROWSER_VER', $log_version[1] . '.' . $log_version2[1]
244 $this->set('PMA_USR_BROWSER_AGENT', 'SAFARI');
245 } elseif (preg_match('@rv:1.9(.*)Gecko@', $HTTP_USER_AGENT)) {
246 $this->set('PMA_USR_BROWSER_VER', '1.9');
247 $this->set('PMA_USR_BROWSER_AGENT', 'GECKO');
248 } elseif (preg_match('@Mozilla/([0-9].[0-9]{1,2})@', $HTTP_USER_AGENT, $log_version)) {
249 $this->set('PMA_USR_BROWSER_VER', $log_version[1]);
250 $this->set('PMA_USR_BROWSER_AGENT', 'MOZILLA');
251 } else {
252 $this->set('PMA_USR_BROWSER_VER', 0);
253 $this->set('PMA_USR_BROWSER_AGENT', 'OTHER');
258 * Whether GD2 is present
260 * @return void
262 function checkGd2()
264 if ($this->get('GD2Available') == 'yes') {
265 $this->set('PMA_IS_GD2', 1);
266 } elseif ($this->get('GD2Available') == 'no') {
267 $this->set('PMA_IS_GD2', 0);
268 } else {
269 if (!@function_exists('imagecreatetruecolor')) {
270 $this->set('PMA_IS_GD2', 0);
271 } else {
272 if (@function_exists('gd_info')) {
273 $gd_nfo = gd_info();
274 if (strstr($gd_nfo["GD Version"], '2.')) {
275 $this->set('PMA_IS_GD2', 1);
276 } else {
277 $this->set('PMA_IS_GD2', 0);
279 } else {
280 $this->set('PMA_IS_GD2', 0);
287 * Whether the Web server php is running on is IIS
289 * @return void
291 function checkWebServer()
293 // some versions return Microsoft-IIS, some Microsoft/IIS
294 // we could use a preg_match() but it's slower
295 if (PMA_getenv('SERVER_SOFTWARE')
296 && stristr(PMA_getenv('SERVER_SOFTWARE'), 'Microsoft')
297 && stristr(PMA_getenv('SERVER_SOFTWARE'), 'IIS')
299 $this->set('PMA_IS_IIS', 1);
300 } else {
301 $this->set('PMA_IS_IIS', 0);
306 * Whether the os php is running on is windows or not
308 * @return void
310 function checkWebServerOs()
312 // Default to Unix or Equiv
313 $this->set('PMA_IS_WINDOWS', 0);
314 // If PHP_OS is defined then continue
315 if (defined('PHP_OS')) {
316 if (stristr(PHP_OS, 'win')) {
317 // Is it some version of Windows
318 $this->set('PMA_IS_WINDOWS', 1);
319 } elseif (stristr(PHP_OS, 'OS/2')) {
320 // Is it OS/2 (No file permissions like Windows)
321 $this->set('PMA_IS_WINDOWS', 1);
327 * detects PHP version
329 * @return void
331 function checkPhpVersion()
333 $match = array();
334 if (! preg_match(
335 '@([0-9]{1,2}).([0-9]{1,2}).([0-9]{1,2})@',
336 phpversion(),
337 $match
338 )) {
339 preg_match(
340 '@([0-9]{1,2}).([0-9]{1,2})@',
341 phpversion(),
342 $match
345 if (isset($match) && ! empty($match[1])) {
346 if (! isset($match[2])) {
347 $match[2] = 0;
349 if (! isset($match[3])) {
350 $match[3] = 0;
352 $this->set(
353 'PMA_PHP_INT_VERSION',
354 (int) sprintf('%d%02d%02d', $match[1], $match[2], $match[3])
356 } else {
357 $this->set('PMA_PHP_INT_VERSION', 0);
359 $this->set('PMA_PHP_STR_VERSION', phpversion());
363 * detects if Git revision
365 * @return boolean
367 function isGitRevision()
369 // caching
370 if (isset($_SESSION['is_git_revision'])) {
371 if ($_SESSION['is_git_revision']) {
372 $this->set('PMA_VERSION_GIT', 1);
374 return $_SESSION['is_git_revision'];
376 // find out if there is a .git folder
377 $git_folder = '.git';
378 if (! @file_exists($git_folder)
379 || ! @file_exists($git_folder . '/config')
381 $_SESSION['is_git_revision'] = false;
382 return false;
384 $_SESSION['is_git_revision'] = true;
385 return true;
389 * detects Git revision, if running inside repo
391 * @return void
393 function checkGitRevision()
395 // find out if there is a .git folder
396 $git_folder = '.git';
397 if (! $this->isGitRevision()) {
398 return;
401 if (! $ref_head = @file_get_contents($git_folder . '/HEAD')) {
402 return;
404 $branch = false;
405 // are we on any branch?
406 if (strstr($ref_head, '/')) {
407 $ref_head = substr(trim($ref_head), 5);
408 if (substr($ref_head, 0, 11) === 'refs/heads/') {
409 $branch = substr($ref_head, 11);
410 } else {
411 $branch = basename($ref_head);
414 $ref_file = $git_folder . '/' . $ref_head;
415 if (@file_exists($ref_file)) {
416 if (! $hash = @file_get_contents($ref_file)) {
417 return;
419 $hash = trim($hash);
420 } else {
421 // deal with packed refs
422 if (! $packed_refs = @file_get_contents($git_folder . '/packed-refs')) {
423 return;
425 // split file to lines
426 $ref_lines = explode("\n", $packed_refs);
427 foreach ($ref_lines as $line) {
428 // skip comments
429 if ($line[0] == '#') {
430 continue;
432 // parse line
433 $parts = explode(' ', $line);
434 // care only about named refs
435 if (count($parts) != 2) {
436 continue;
438 // have found our ref?
439 if ($parts[1] == $ref_head) {
440 $hash = $parts[0];
441 break;
444 if (! isset($hash)) {
445 // Could not find ref
446 return;
449 } else {
450 $hash = trim($ref_head);
453 $commit = false;
454 if ( !isset($_SESSION['PMA_VERSION_COMMITDATA_' . $hash])) {
455 $git_file_name = $git_folder . '/objects/' . substr($hash, 0, 2)
456 . '/' . substr($hash, 2);
457 if (file_exists($git_file_name) ) {
458 if (! $commit = @file_get_contents($git_file_name)) {
459 return;
461 $commit = explode("\0", gzuncompress($commit), 2);
462 $commit = explode("\n", $commit[1]);
463 $_SESSION['PMA_VERSION_COMMITDATA_' . $hash] = $commit;
464 } else {
465 $pack_names = array();
466 // work with packed data
467 if ($packs = @file_get_contents($git_folder . '/objects/info/packs')) {
468 // File exists. Read it, parse the file to get the names of the
469 // packs. (to look for them in .git/object/pack directory later)
470 foreach (explode("\n", $packs) as $line) {
471 // skip blank lines
472 if (strlen(trim($line)) == 0) {
473 continue;
475 // skip non pack lines
476 if ($line[0] != 'P') {
477 continue;
479 // parse names
480 $pack_names[] = substr($line, 2);
482 } else {
483 // '.git/objects/info/packs' file can be missing
484 // (atlease in mysGit)
485 // File missing. May be we can look in the .git/object/pack
486 // directory for all the .pack files and use that list of
487 // files instead
488 $it = new DirectoryIterator($git_folder . '/objects/pack');
489 foreach ($it as $file_info) {
490 $file_name = $file_info->getFilename();
491 // if this is a .pack file
492 if ($file_info->isFile()
493 && substr($file_name, -5) == '.pack'
495 $pack_names[] = $file_name;
499 $hash = strtolower($hash);
500 foreach ($pack_names as $pack_name) {
501 $index_name = str_replace('.pack', '.idx', $pack_name);
503 // load index
504 if (! $index_data = @file_get_contents($git_folder . '/objects/pack/' . $index_name)) {
505 continue;
507 // check format
508 if (substr($index_data, 0, 4) != "\377tOc") {
509 continue;
511 // check version
512 $version = unpack('N', substr($index_data, 4, 4));
513 if ($version[1] != 2) {
514 continue;
516 // parse fanout table
517 $fanout = unpack("N*", substr($index_data, 8, 256 * 4));
519 // find where we should search
520 $firstbyte = intval(substr($hash, 0, 2), 16);
521 // array is indexed from 1 and we need to get
522 // previous entry for start
523 if ($firstbyte == 0) {
524 $start = 0;
525 } else {
526 $start = $fanout[$firstbyte];
528 $end = $fanout[$firstbyte + 1];
530 // stupid linear search for our sha
531 $position = $start;
532 $found = false;
533 $offset = 8 + (256 * 4);
534 for ($position = $start; $position < $end; $position++) {
535 $sha = strtolower(
536 bin2hex(
537 substr(
538 $index_data, $offset + ($position * 20), 20
542 if ($sha == $hash) {
543 $found = true;
544 break;
547 if (! $found) {
548 continue;
550 // read pack offset
551 $offset = 8 + (256 * 4) + (24 * $fanout[256]);
552 $pack_offset = unpack(
553 'N', substr($index_data, $offset + ($position * 4), 4)
555 $pack_offset = $pack_offset[1];
557 // open pack file
558 $pack_file = fopen(
559 $git_folder . '/objects/pack/' . $pack_name, 'rb'
561 if ($pack_file === false) {
562 continue;
564 // seek to start
565 fseek($pack_file, $pack_offset);
567 // parse header
568 $header = ord(fread($pack_file, 1));
569 $type = ($header >> 4) & 7;
570 $hasnext = ($header & 128) >> 7;
571 $size = $header & 0xf;
572 $offset = 4;
574 while ($hasnext) {
575 $byte = ord(fread($pack_file, 1));
576 $size |= ($byte & 0x7f) << $offset;
577 $hasnext = ($byte & 128) >> 7;
578 $offset += 7;
581 // we care only about commit objects
582 if ($type != 1) {
583 continue;
586 // read data
587 $commit = fread($pack_file, $size);
588 $commit = gzuncompress($commit);
589 $commit = explode("\n", $commit);
590 $_SESSION['PMA_VERSION_COMMITDATA_' . $hash] = $commit;
591 fclose($pack_file);
594 } else {
595 $commit = $_SESSION['PMA_VERSION_COMMITDATA_' . $hash];
598 // check if commit exists in Github
599 $is_remote_commit = false;
600 if ($commit !== false
601 && isset($_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash])
603 $is_remote_commit = $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash];
604 } else {
605 $link = 'https://api.github.com/repos/phpmyadmin/phpmyadmin/git/commits/'
606 . $hash;
607 $is_found = $this->checkHTTP($link, !$commit);
608 switch($is_found) {
609 case false:
610 $is_remote_commit = false;
611 $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = false;
612 break;
613 case null:
614 // no remote link for now, but don't cache this as Github is down
615 $is_remote_commit = false;
616 break;
617 default:
618 $is_remote_commit = true;
619 $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = true;
620 if ($commit === false) {
621 // if no local commit data, try loading from Github
622 $commit_json = json_decode($is_found);
624 break;
628 $is_remote_branch = false;
629 if ($is_remote_commit && $branch !== false) {
630 // check if branch exists in Github
631 if (isset($_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash])) {
632 $is_remote_branch = $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash];
633 } else {
634 $link = 'https://api.github.com/repos/phpmyadmin/phpmyadmin'
635 . '/git/trees/' . $branch;
636 $is_found = $this->checkHTTP($link);
637 switch($is_found) {
638 case true:
639 $is_remote_branch = true;
640 $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = true;
641 break;
642 case false:
643 $is_remote_branch = false;
644 $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = false;
645 break;
646 case null:
647 // no remote link for now, but don't cache this as Github is down
648 $is_remote_branch = false;
649 break;
654 if ($commit !== false) {
655 $author = array('name' => '', 'email' => '', 'date' => '');
656 $committer = array('name' => '', 'email' => '', 'date' => '');
658 do {
659 $dataline = array_shift($commit);
660 $datalinearr = explode(' ', $dataline, 2);
661 $linetype = $datalinearr[0];
662 if (in_array($linetype, array('author', 'committer'))) {
663 $user = $datalinearr[1];
664 preg_match('/([^<]+)<([^>]+)> ([0-9]+)( [^ ]+)?/', $user, $user);
665 $user2 = array(
666 'name' => trim($user[1]),
667 'email' => trim($user[2]),
668 'date' => date('Y-m-d H:i:s', $user[3]));
669 if (isset($user[4])) {
670 $user2['date'] .= $user[4];
672 $$linetype = $user2;
674 } while ($dataline != '');
675 $message = trim(implode(' ', $commit));
677 } elseif (isset($commit_json)) {
678 $author = array(
679 'name' => $commit_json->author->name,
680 'email' => $commit_json->author->email,
681 'date' => $commit_json->author->date);
682 $committer = array(
683 'name' => $commit_json->committer->name,
684 'email' => $commit_json->committer->email,
685 'date' => $commit_json->committer->date);
686 $message = trim($commit_json->message);
687 } else {
688 return;
691 $this->set('PMA_VERSION_GIT', 1);
692 $this->set('PMA_VERSION_GIT_COMMITHASH', $hash);
693 $this->set('PMA_VERSION_GIT_BRANCH', $branch);
694 $this->set('PMA_VERSION_GIT_MESSAGE', $message);
695 $this->set('PMA_VERSION_GIT_AUTHOR', $author);
696 $this->set('PMA_VERSION_GIT_COMMITTER', $committer);
697 $this->set('PMA_VERSION_GIT_ISREMOTECOMMIT', $is_remote_commit);
698 $this->set('PMA_VERSION_GIT_ISREMOTEBRANCH', $is_remote_branch);
702 * Checks if given URL is 200 or 404, optionally returns data
704 * @param mixed $link curl link
705 * @param boolean $get_body whether to retrieve body of document
707 * @return test result or data
709 function checkHTTP($link, $get_body = false)
711 if (! function_exists('curl_init')) {
712 return null;
714 $ch = curl_init($link);
715 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
716 curl_setopt($ch, CURLOPT_HEADER, 1);
717 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
718 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
719 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
720 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
721 curl_setopt($ch, CURLOPT_USERAGENT, 'phpMyAdmin/' . PMA_VERSION);
722 curl_setopt($ch, CURLOPT_TIMEOUT, 5);
723 $data = @curl_exec($ch);
724 if ($data === false) {
725 return null;
727 $ok = 'HTTP/1.1 200 OK';
728 $notfound = 'HTTP/1.1 404 Not Found';
729 if (substr($data, 0, strlen($ok)) === $ok) {
730 return $get_body ? substr($data, strpos($data, "\r\n\r\n") + 4) : true;
731 } elseif (substr($data, 0, strlen($notfound)) === $notfound) {
732 return false;
734 return null;
738 * loads default values from default source
740 * @return boolean success
742 function loadDefaults()
744 $cfg = array();
745 if (! file_exists($this->default_source)) {
746 $this->error_config_default_file = true;
747 return false;
749 include $this->default_source;
751 $this->default_source_mtime = filemtime($this->default_source);
753 $this->default_server = $cfg['Servers'][1];
754 unset($cfg['Servers']);
756 $this->default = $cfg;
757 $this->settings = PMA_arrayMergeRecursive($this->settings, $cfg);
759 $this->error_config_default_file = false;
761 return true;
765 * loads configuration from $source, usally the config file
766 * should be called on object creation
768 * @param string $source config file
770 * @return bool
772 function load($source = null)
774 $this->loadDefaults();
776 if (null !== $source) {
777 $this->setSource($source);
780 if (! $this->checkConfigSource()) {
781 return false;
784 $cfg = array();
787 * Parses the configuration file, the eval is used here to avoid
788 * problems with trailing whitespace, what is often a problem.
790 $old_error_reporting = error_reporting(0);
791 $eval_result = eval('?' . '>' . trim(file_get_contents($this->getSource())));
792 error_reporting($old_error_reporting);
794 if ($eval_result === false) {
795 $this->error_config_file = true;
796 } else {
797 $this->error_config_file = false;
798 $this->source_mtime = filemtime($this->getSource());
802 * Backward compatibility code
804 if (!empty($cfg['DefaultTabTable'])) {
805 $cfg['DefaultTabTable'] = str_replace(
806 '_properties',
808 str_replace(
809 'tbl_properties.php',
810 'tbl_sql.php',
811 $cfg['DefaultTabTable']
815 if (!empty($cfg['DefaultTabDatabase'])) {
816 $cfg['DefaultTabDatabase'] = str_replace(
817 '_details',
819 str_replace(
820 'db_details.php',
821 'db_sql.php',
822 $cfg['DefaultTabDatabase']
827 $this->settings = PMA_arrayMergeRecursive($this->settings, $cfg);
828 $this->checkPmaAbsoluteUri();
829 $this->checkFontsize();
831 // Handling of the collation must be done after merging of $cfg
832 // (from config.inc.php) so that $cfg['DefaultConnectionCollation']
833 // can have an effect. Note that the presence of collation
834 // information in a cookie has priority over what is defined
835 // in the default or user's config files.
837 * @todo check validity of $_COOKIE['pma_collation_connection']
839 if (! empty($_COOKIE['pma_collation_connection'])) {
840 $this->set(
841 'collation_connection',
842 strip_tags($_COOKIE['pma_collation_connection'])
844 } else {
845 $this->set(
846 'collation_connection',
847 $this->get('DefaultConnectionCollation')
850 // Now, a collation information could come from REQUEST
851 // (an example of this: the collation selector in index.php)
852 // so the following handles the setting of collation_connection
853 // and later, in common.inc.php, the cookie will be set
854 // according to this.
855 $this->checkCollationConnection();
857 return true;
861 * Loads user preferences and merges them with current config
862 * must be called after control connection has been estabilished
864 * @return boolean
866 function loadUserPreferences()
868 // index.php should load these settings, so that phpmyadmin.css.php
869 // will have everything avaiable in session cache
870 $server = isset($GLOBALS['server'])
871 ? $GLOBALS['server']
872 : (!empty($GLOBALS['cfg']['ServerDefault'])
873 ? $GLOBALS['cfg']['ServerDefault']
874 : 0);
875 $cache_key = 'server_' . $server;
876 if ($server > 0 && !defined('PMA_MINIMUM_COMMON')) {
877 $config_mtime = max($this->default_source_mtime, $this->source_mtime);
878 // cache user preferences, use database only when needed
879 if (! isset($_SESSION['cache'][$cache_key]['userprefs'])
880 || $_SESSION['cache'][$cache_key]['config_mtime'] < $config_mtime
882 // load required libraries
883 include_once './libraries/user_preferences.lib.php';
884 $prefs = PMA_loadUserprefs();
885 $_SESSION['cache'][$cache_key]['userprefs']
886 = PMA_applyUserprefs($prefs['config_data']);
887 $_SESSION['cache'][$cache_key]['userprefs_mtime'] = $prefs['mtime'];
888 $_SESSION['cache'][$cache_key]['userprefs_type'] = $prefs['type'];
889 $_SESSION['cache'][$cache_key]['config_mtime'] = $config_mtime;
891 } elseif ($server == 0
892 || ! isset($_SESSION['cache'][$cache_key]['userprefs'])
894 $this->set('user_preferences', false);
895 return;
897 $config_data = $_SESSION['cache'][$cache_key]['userprefs'];
898 // type is 'db' or 'session'
899 $this->set(
900 'user_preferences',
901 $_SESSION['cache'][$cache_key]['userprefs_type']
903 $this->set(
904 'user_preferences_mtime',
905 $_SESSION['cache'][$cache_key]['userprefs_mtime']
908 // backup some settings
909 $org_fontsize = '';
910 if (isset($this->settings['fontsize'])) {
911 $org_fontsize = $this->settings['fontsize'];
913 // load config array
914 $this->settings = PMA_arrayMergeRecursive($this->settings, $config_data);
915 $GLOBALS['cfg'] = PMA_arrayMergeRecursive($GLOBALS['cfg'], $config_data);
916 if (defined('PMA_MINIMUM_COMMON')) {
917 return;
920 // settings below start really working on next page load, but
921 // changes are made only in index.php so everything is set when
922 // in frames
924 // save theme
925 $tmanager = $_SESSION['PMA_Theme_Manager'];
926 if ($tmanager->getThemeCookie() || isset($_REQUEST['set_theme'])) {
927 if ((! isset($config_data['ThemeDefault'])
928 && $tmanager->theme->getId() != 'original')
929 || isset($config_data['ThemeDefault'])
930 && $config_data['ThemeDefault'] != $tmanager->theme->getId()
932 // new theme was set in common.inc.php
933 $this->setUserValue(
934 null,
935 'ThemeDefault',
936 $tmanager->theme->getId(),
937 'original'
940 } else {
941 // no cookie - read default from settings
942 if ($this->settings['ThemeDefault'] != $tmanager->theme->getId()
943 && $tmanager->checkTheme($this->settings['ThemeDefault'])
945 $tmanager->setActiveTheme($this->settings['ThemeDefault']);
946 $tmanager->setThemeCookie();
950 // save font size
951 if ((! isset($config_data['fontsize'])
952 && $org_fontsize != '82%')
953 || isset($config_data['fontsize'])
954 && $org_fontsize != $config_data['fontsize']
956 $this->setUserValue(null, 'fontsize', $org_fontsize, '82%');
959 // save language
960 if (isset($_COOKIE['pma_lang']) || isset($_POST['lang'])) {
961 if ((! isset($config_data['lang'])
962 && $GLOBALS['lang'] != 'en')
963 || isset($config_data['lang'])
964 && $GLOBALS['lang'] != $config_data['lang']
966 $this->setUserValue(null, 'lang', $GLOBALS['lang'], 'en');
968 } else {
969 // read language from settings
970 if (isset($config_data['lang']) && PMA_langSet($config_data['lang'])) {
971 $this->setCookie('pma_lang', $GLOBALS['lang']);
975 // save connection collation
976 if (isset($_COOKIE['pma_collation_connection'])
977 || isset($_POST['collation_connection'])
979 if ((! isset($config_data['collation_connection'])
980 && $GLOBALS['collation_connection'] != 'utf8_general_ci')
981 || isset($config_data['collation_connection'])
982 && $GLOBALS['collation_connection'] != $config_data['collation_connection']
984 $this->setUserValue(
985 null,
986 'collation_connection',
987 $GLOBALS['collation_connection'],
988 'utf8_general_ci'
991 } else {
992 // read collation from settings
993 if (isset($config_data['collation_connection'])) {
994 $GLOBALS['collation_connection']
995 = $config_data['collation_connection'];
996 $this->setCookie(
997 'pma_collation_connection',
998 $GLOBALS['collation_connection']
1005 * Sets config value which is stored in user preferences (if available)
1006 * or in a cookie.
1008 * If user preferences are not yet initialized, option is applied to
1009 * global config and added to a update queue, which is processed
1010 * by {@link loadUserPreferences()}
1012 * @param string $cookie_name can be null
1013 * @param string $cfg_path configuration path
1014 * @param mixed $new_cfg_value new value
1015 * @param mixed $default_value default value
1017 * @return void
1019 function setUserValue($cookie_name, $cfg_path, $new_cfg_value,
1020 $default_value = null
1022 // use permanent user preferences if possible
1023 $prefs_type = $this->get('user_preferences');
1024 if ($prefs_type) {
1025 include_once './libraries/user_preferences.lib.php';
1026 if ($default_value === null) {
1027 $default_value = PMA_arrayRead($cfg_path, $this->default);
1029 PMA_persistOption($cfg_path, $new_cfg_value, $default_value);
1031 if ($prefs_type != 'db' && $cookie_name) {
1032 // fall back to cookies
1033 if ($default_value === null) {
1034 $default_value = PMA_arrayRead($cfg_path, $this->settings);
1036 $this->setCookie($cookie_name, $new_cfg_value, $default_value);
1038 PMA_arrayWrite($cfg_path, $GLOBALS['cfg'], $new_cfg_value);
1039 PMA_arrayWrite($cfg_path, $this->settings, $new_cfg_value);
1043 * Reads value stored by {@link setUserValue()}
1045 * @param string $cookie_name cookie name
1046 * @param mixed $cfg_value config value
1048 * @return mixed
1050 function getUserValue($cookie_name, $cfg_value)
1052 $cookie_exists = isset($_COOKIE) && !empty($_COOKIE[$cookie_name]);
1053 $prefs_type = $this->get('user_preferences');
1054 if ($prefs_type == 'db') {
1055 // permanent user preferences value exists, remove cookie
1056 if ($cookie_exists) {
1057 $this->removeCookie($cookie_name);
1059 } else if ($cookie_exists) {
1060 return $_COOKIE[$cookie_name];
1062 // return value from $cfg array
1063 return $cfg_value;
1067 * set source
1069 * @param string $source source
1071 * @return void
1073 function setSource($source)
1075 $this->source = trim($source);
1079 * check config source
1081 * @return boolean whether source is valid or not
1083 function checkConfigSource()
1085 if (! $this->getSource()) {
1086 // no configuration file set at all
1087 return false;
1090 if (! file_exists($this->getSource())) {
1091 $this->source_mtime = 0;
1092 return false;
1095 if (! is_readable($this->getSource())) {
1096 // manually check if file is readable
1097 // might be bug #3059806 Supporting running from CIFS/Samba shares
1099 $contents = false;
1100 $handle = @fopen($this->getSource(), 'r');
1101 if ($handle !== false) {
1102 $contents = @fread($handle, 1); // reading 1 byte is enough to test
1103 @fclose($handle);
1105 if ($contents === false) {
1106 $this->source_mtime = 0;
1107 PMA_fatalError(
1108 sprintf(
1109 function_exists('__')
1110 ? __('Existing configuration file (%s) is not readable.')
1111 : 'Existing configuration file (%s) is not readable.',
1112 $this->getSource()
1115 return false;
1119 return true;
1123 * verifies the permissions on config file (if asked by configuration)
1124 * (must be called after config.inc.php has been merged)
1126 * @return void
1128 function checkPermissions()
1130 // Check for permissions (on platforms that support it):
1131 if ($this->get('CheckConfigurationPermissions')) {
1132 $perms = @fileperms($this->getSource());
1133 if (!($perms === false) && ($perms & 2)) {
1134 // This check is normally done after loading configuration
1135 $this->checkWebServerOs();
1136 if ($this->get('PMA_IS_WINDOWS') == 0) {
1137 $this->source_mtime = 0;
1138 /* Gettext is possibly still not loaded */
1139 if (function_exists('__')) {
1140 $msg = __('Wrong permissions on configuration file, should not be world writable!');
1141 } else {
1142 $msg = 'Wrong permissions on configuration file, should not be world writable!';
1144 PMA_fatalError($msg);
1151 * returns specific config setting
1153 * @param string $setting config setting
1155 * @return mixed value
1157 function get($setting)
1159 if (isset($this->settings[$setting])) {
1160 return $this->settings[$setting];
1162 return null;
1166 * sets configuration variable
1168 * @param string $setting configuration option
1169 * @param string $value new value for configuration option
1171 * @return void
1173 function set($setting, $value)
1175 if (! isset($this->settings[$setting])
1176 || $this->settings[$setting] != $value
1178 $this->settings[$setting] = $value;
1179 $this->set_mtime = time();
1184 * returns source for current config
1186 * @return string config source
1188 function getSource()
1190 return $this->source;
1194 * returns a unique value to force a CSS reload if either the config
1195 * or the theme changes
1196 * must also check the pma_fontsize cookie in case there is no
1197 * config file
1199 * @return int Summary of unix timestamps and fontsize,
1200 * to be unique on theme parameters change
1202 function getThemeUniqueValue()
1204 if (null !== $this->get('fontsize')) {
1205 $fontsize = intval($this->get('fontsize'));
1206 } elseif (isset($_COOKIE['pma_fontsize'])) {
1207 $fontsize = intval($_COOKIE['pma_fontsize']);
1208 } else {
1209 $fontsize = 0;
1211 return (
1212 $fontsize +
1213 $this->source_mtime +
1214 $this->default_source_mtime +
1215 $this->get('user_preferences_mtime') +
1216 $_SESSION['PMA_Theme']->mtime_info +
1217 $_SESSION['PMA_Theme']->filesize_info);
1221 * $cfg['PmaAbsoluteUri'] is a required directive else cookies won't be
1222 * set properly and, depending on browsers, inserting or updating a
1223 * record might fail
1225 * @return bool
1227 function checkPmaAbsoluteUri()
1229 // Setup a default value to let the people and lazy sysadmins work anyway,
1230 // they'll get an error if the autodetect code doesn't work
1231 $pma_absolute_uri = $this->get('PmaAbsoluteUri');
1232 $is_https = $this->detectHttps();
1234 if (strlen($pma_absolute_uri) < 5) {
1235 $url = array();
1237 // If we don't have scheme, we didn't have full URL so we need to
1238 // dig deeper
1239 if (empty($url['scheme'])) {
1240 // Scheme
1241 if ($is_https) {
1242 $url['scheme'] = 'https';
1243 } else {
1244 $url['scheme'] = 'http';
1247 // Host and port
1248 if (PMA_getenv('HTTP_HOST')) {
1249 // Prepend the scheme before using parse_url() since this
1250 // is not part of the RFC2616 Host request-header
1251 $parsed_url = parse_url(
1252 $url['scheme'] . '://' . PMA_getenv('HTTP_HOST')
1254 if (!empty($parsed_url['host'])) {
1255 $url = $parsed_url;
1256 } else {
1257 $url['host'] = PMA_getenv('HTTP_HOST');
1259 } elseif (PMA_getenv('SERVER_NAME')) {
1260 $url['host'] = PMA_getenv('SERVER_NAME');
1261 } else {
1262 $this->error_pma_uri = true;
1263 return false;
1266 // If we didn't set port yet...
1267 if (empty($url['port']) && PMA_getenv('SERVER_PORT')) {
1268 $url['port'] = PMA_getenv('SERVER_PORT');
1271 // And finally the path could be already set from REQUEST_URI
1272 if (empty($url['path'])) {
1273 $path = parse_url($GLOBALS['PMA_PHP_SELF']);
1274 $url['path'] = $path['path'];
1278 // Make url from parts we have
1279 $pma_absolute_uri = $url['scheme'] . '://';
1280 // Was there user information?
1281 if (!empty($url['user'])) {
1282 $pma_absolute_uri .= $url['user'];
1283 if (!empty($url['pass'])) {
1284 $pma_absolute_uri .= ':' . $url['pass'];
1286 $pma_absolute_uri .= '@';
1288 // Add hostname
1289 $pma_absolute_uri .= $url['host'];
1290 // Add port, if it not the default one
1291 if (! empty($url['port'])
1292 && (($url['scheme'] == 'http' && $url['port'] != 80)
1293 || ($url['scheme'] == 'https' && $url['port'] != 443))
1295 $pma_absolute_uri .= ':' . $url['port'];
1297 // And finally path, without script name, the 'a' is there not to
1298 // strip our directory, when path is only /pmadir/ without filename.
1299 // Backslashes returned by Windows have to be changed.
1300 // Only replace backslashes by forward slashes if on Windows,
1301 // as the backslash could be valid on a non-Windows system.
1302 $this->checkWebServerOs();
1303 if ($this->get('PMA_IS_WINDOWS') == 1) {
1304 $path = str_replace("\\", "/", dirname($url['path'] . 'a'));
1305 } else {
1306 $path = dirname($url['path'] . 'a');
1309 // To work correctly within transformations overview:
1310 if (defined('PMA_PATH_TO_BASEDIR') && PMA_PATH_TO_BASEDIR == '../../') {
1311 if ($this->get('PMA_IS_WINDOWS') == 1) {
1312 $path = str_replace("\\", "/", dirname(dirname($path)));
1313 } else {
1314 $path = dirname(dirname($path));
1318 // PHP's dirname function would have returned a dot
1319 // when $path contains no slash
1320 if ($path == '.') {
1321 $path = '';
1323 // in vhost situations, there could be already an ending slash
1324 if (substr($path, -1) != '/') {
1325 $path .= '/';
1327 $pma_absolute_uri .= $path;
1329 // We used to display a warning if PmaAbsoluteUri wasn't set, but now
1330 // the autodetect code works well enough that we don't display the
1331 // warning at all. The user can still set PmaAbsoluteUri manually.
1333 } else {
1334 // The URI is specified, however users do often specify this
1335 // wrongly, so we try to fix this.
1337 // Adds a trailing slash et the end of the phpMyAdmin uri if it
1338 // does not exist.
1339 if (substr($pma_absolute_uri, -1) != '/') {
1340 $pma_absolute_uri .= '/';
1343 // If URI doesn't start with http:// or https://, we will add
1344 // this.
1345 if (substr($pma_absolute_uri, 0, 7) != 'http://'
1346 && substr($pma_absolute_uri, 0, 8) != 'https://'
1348 $pma_absolute_uri
1349 = ($is_https ? 'https' : 'http')
1350 . ':' . (substr($pma_absolute_uri, 0, 2) == '//' ? '' : '//')
1351 . $pma_absolute_uri;
1354 $this->set('PmaAbsoluteUri', $pma_absolute_uri);
1358 * Converts currently used PmaAbsoluteUri to SSL based variant.
1360 * @return String witch adjusted URI
1362 function getSSLUri()
1364 // grab current URL
1365 $url = $this->get('PmaAbsoluteUri');
1366 // Parse current URL
1367 $parsed = parse_url($url);
1368 // In case parsing has failed do stupid string replacement
1369 if ($parsed === false) {
1370 // Replace http protocol
1371 return preg_replace('@^http:@', 'https:', $url);
1374 // Reconstruct URL using parsed parts
1375 if ($this->get('SSLPort')) {
1376 $port_number = $this->get('SSLPort');
1377 } else {
1378 $port_number = 443;
1380 return 'https://' . $parsed['host'] . ':' . $port_number . $parsed['path'];
1384 * check selected collation_connection
1386 * @todo check validity of $_REQUEST['collation_connection']
1388 * @return void
1390 function checkCollationConnection()
1392 if (! empty($_REQUEST['collation_connection'])) {
1393 $this->set(
1394 'collation_connection',
1395 strip_tags($_REQUEST['collation_connection'])
1401 * checks for font size configuration, and sets font size as requested by user
1403 * @return void
1405 function checkFontsize()
1407 $new_fontsize = '';
1409 if (isset($_GET['set_fontsize'])) {
1410 $new_fontsize = $_GET['set_fontsize'];
1411 } elseif (isset($_POST['set_fontsize'])) {
1412 $new_fontsize = $_POST['set_fontsize'];
1413 } elseif (isset($_COOKIE['pma_fontsize'])) {
1414 $new_fontsize = $_COOKIE['pma_fontsize'];
1417 if (preg_match('/^[0-9.]+(px|em|pt|\%)$/', $new_fontsize)) {
1418 $this->set('fontsize', $new_fontsize);
1419 } elseif (! $this->get('fontsize')) {
1420 // 80% would correspond to the default browser font size
1421 // of 16, but use 82% to help read the monoface font
1422 $this->set('fontsize', '82%');
1425 $this->setCookie('pma_fontsize', $this->get('fontsize'), '82%');
1429 * checks if upload is enabled
1431 * @return void
1433 function checkUpload()
1435 if (ini_get('file_uploads')) {
1436 $this->set('enable_upload', true);
1437 // if set "php_admin_value file_uploads Off" in httpd.conf
1438 // ini_get() also returns the string "Off" in this case:
1439 if ('off' == strtolower(ini_get('file_uploads'))) {
1440 $this->set('enable_upload', false);
1442 } else {
1443 $this->set('enable_upload', false);
1448 * Maximum upload size as limited by PHP
1449 * Used with permission from Moodle (http://moodle.org) by Martin Dougiamas
1451 * this section generates $max_upload_size in bytes
1453 * @return void
1455 function checkUploadSize()
1457 if (! $filesize = ini_get('upload_max_filesize')) {
1458 $filesize = "5M";
1461 if ($postsize = ini_get('post_max_size')) {
1462 $this->set(
1463 'max_upload_size',
1464 min(PMA_getRealSize($filesize), PMA_getRealSize($postsize))
1466 } else {
1467 $this->set('max_upload_size', PMA_getRealSize($filesize));
1472 * check for https
1474 * @return void
1476 function checkIsHttps()
1478 $this->set('is_https', $this->isHttps());
1482 * Checks if protocol is https
1484 * This function checks if the https protocol is used in the PmaAbsoluteUri
1485 * configuration setting, as opposed to detectHttps() which checks if the
1486 * https protocol is used on the active connection.
1488 * @return bool
1490 public function isHttps()
1492 static $is_https = null;
1494 if (null !== $is_https) {
1495 return $is_https;
1498 $url = parse_url($this->get('PmaAbsoluteUri'));
1500 if (isset($url['scheme']) && $url['scheme'] == 'https') {
1501 $is_https = true;
1502 } else {
1503 $is_https = false;
1506 return $is_https;
1510 * Detects whether https appears to be used.
1512 * This function checks if the https protocol is used in the current connection
1513 * with the webserver, based on environment variables.
1514 * Please note that this just detects what we see, so
1515 * it completely ignores things like reverse proxies.
1517 * @return bool
1519 function detectHttps()
1521 $is_https = false;
1523 $url = array();
1525 // At first we try to parse REQUEST_URI, it might contain full URL,
1526 if (PMA_getenv('REQUEST_URI')) {
1527 // produces E_WARNING if it cannot get parsed, e.g. '/foobar:/'
1528 $url = @parse_url(PMA_getenv('REQUEST_URI'));
1529 if ($url === false) {
1530 $url = array();
1534 // If we don't have scheme, we didn't have full URL so we need to
1535 // dig deeper
1536 if (empty($url['scheme'])) {
1537 // Scheme
1538 if (PMA_getenv('HTTP_SCHEME')) {
1539 $url['scheme'] = PMA_getenv('HTTP_SCHEME');
1540 } elseif (PMA_getenv('HTTPS')
1541 && strtolower(PMA_getenv('HTTPS')) == 'on'
1543 $url['scheme'] = 'https';
1544 } elseif (PMA_getenv('HTTP_X_FORWARDED_PROTO')) {
1545 $url['scheme'] = strtolower(PMA_getenv('HTTP_X_FORWARDED_PROTO'));
1546 } elseif (PMA_getenv('HTTP_FRONT_END_HTTPS')
1547 && strtolower(PMA_getenv('HTTP_FRONT_END_HTTPS')) == 'on'
1549 $url['scheme'] = 'https';
1550 } else {
1551 $url['scheme'] = 'http';
1555 if (isset($url['scheme']) && $url['scheme'] == 'https') {
1556 $is_https = true;
1557 } else {
1558 $is_https = false;
1561 return $is_https;
1565 * detect correct cookie path
1567 * @return void
1569 function checkCookiePath()
1571 $this->set('cookie_path', $this->getCookiePath());
1575 * Get cookie path
1577 * @return string
1579 public function getCookiePath()
1581 static $cookie_path = null;
1583 if (null !== $cookie_path && !defined('TESTSUITE')) {
1584 return $cookie_path;
1587 $parsed_url = parse_url($this->get('PmaAbsoluteUri'));
1589 $cookie_path = $parsed_url['path'];
1591 return $cookie_path;
1595 * enables backward compatibility
1597 * @return void
1599 function enableBc()
1601 $GLOBALS['cfg'] = $this->settings;
1602 $GLOBALS['default_server'] = $this->default_server;
1603 unset($this->default_server);
1604 $GLOBALS['collation_connection'] = $this->get('collation_connection');
1605 $GLOBALS['is_upload'] = $this->get('enable_upload');
1606 $GLOBALS['max_upload_size'] = $this->get('max_upload_size');
1607 $GLOBALS['cookie_path'] = $this->get('cookie_path');
1608 $GLOBALS['is_https'] = $this->get('is_https');
1610 $defines = array(
1611 'PMA_VERSION',
1612 'PMA_THEME_VERSION',
1613 'PMA_THEME_GENERATION',
1614 'PMA_PHP_STR_VERSION',
1615 'PMA_PHP_INT_VERSION',
1616 'PMA_IS_WINDOWS',
1617 'PMA_IS_IIS',
1618 'PMA_IS_GD2',
1619 'PMA_USR_OS',
1620 'PMA_USR_BROWSER_VER',
1621 'PMA_USR_BROWSER_AGENT'
1624 foreach ($defines as $define) {
1625 if (! defined($define)) {
1626 define($define, $this->get($define));
1632 * returns options for font size selection
1634 * @param string $current_size current selected font size with unit
1636 * @return array selectable font sizes
1638 * @static
1640 static protected function getFontsizeOptions($current_size = '82%')
1642 $unit = preg_replace('/[0-9.]*/', '', $current_size);
1643 $value = preg_replace('/[^0-9.]*/', '', $current_size);
1645 $factors = array();
1646 $options = array();
1647 $options["$value"] = $value . $unit;
1649 if ($unit === '%') {
1650 $factors[] = 1;
1651 $factors[] = 5;
1652 $factors[] = 10;
1653 } elseif ($unit === 'em') {
1654 $factors[] = 0.05;
1655 $factors[] = 0.2;
1656 $factors[] = 1;
1657 } elseif ($unit === 'pt') {
1658 $factors[] = 0.5;
1659 $factors[] = 2;
1660 } elseif ($unit === 'px') {
1661 $factors[] = 1;
1662 $factors[] = 5;
1663 $factors[] = 10;
1664 } else {
1665 //unknown font size unit
1666 $factors[] = 0.05;
1667 $factors[] = 0.2;
1668 $factors[] = 1;
1669 $factors[] = 5;
1670 $factors[] = 10;
1673 foreach ($factors as $key => $factor) {
1674 $option_inc = $value + $factor;
1675 $option_dec = $value - $factor;
1676 while (count($options) < 21) {
1677 $options["$option_inc"] = $option_inc . $unit;
1678 if ($option_dec > $factors[0]) {
1679 $options["$option_dec"] = $option_dec . $unit;
1681 $option_inc += $factor;
1682 $option_dec -= $factor;
1683 if (isset($factors[$key + 1])
1684 && $option_inc >= $value + $factors[$key + 1]
1686 break;
1690 ksort($options);
1691 return $options;
1695 * returns html selectbox for font sizes
1697 * @static
1699 * @return string html selectbox
1701 static protected function getFontsizeSelection()
1703 $current_size = $GLOBALS['PMA_Config']->get('fontsize');
1704 // for the case when there is no config file (this is supported)
1705 if (empty($current_size)) {
1706 if (isset($_COOKIE['pma_fontsize'])) {
1707 $current_size = $_COOKIE['pma_fontsize'];
1708 } else {
1709 $current_size = '82%';
1712 $options = PMA_Config::getFontsizeOptions($current_size);
1714 $return = '<label for="select_fontsize">' . __('Font size')
1715 . ':</label>' . "\n"
1716 . '<select name="set_fontsize" id="select_fontsize"'
1717 . ' class="autosubmit">' . "\n";
1718 foreach ($options as $option) {
1719 $return .= '<option value="' . $option . '"';
1720 if ($option == $current_size) {
1721 $return .= ' selected="selected"';
1723 $return .= '>' . $option . '</option>' . "\n";
1725 $return .= '</select>';
1727 return $return;
1731 * return complete font size selection form
1733 * @static
1735 * @return string html selectbox
1737 static public function getFontsizeForm()
1739 return '<form name="form_fontsize_selection" id="form_fontsize_selection"'
1740 . ' method="get" action="index.php" class="disableAjax">' . "\n"
1741 . PMA_generate_common_hidden_inputs() . "\n"
1742 . PMA_Config::getFontsizeSelection() . "\n"
1743 . '</form>';
1747 * removes cookie
1749 * @param string $cookie name of cookie to remove
1751 * @return boolean result of setcookie()
1753 function removeCookie($cookie)
1755 if (defined('TESTSUITE')) {
1756 if (isset($_COOKIE[$cookie])) {
1757 unset($_COOKIE[$cookie]);
1759 return true;
1761 return setcookie(
1762 $cookie,
1764 time() - 3600,
1765 $this->getCookiePath(),
1767 $this->isHttps()
1772 * sets cookie if value is different from current cookie value,
1773 * or removes if value is equal to default
1775 * @param string $cookie name of cookie to remove
1776 * @param mixed $value new cookie value
1777 * @param string $default default value
1778 * @param int $validity validity of cookie in seconds (default is one month)
1779 * @param bool $httponly whether cookie is only for HTTP (and not for scripts)
1781 * @return boolean result of setcookie()
1783 function setCookie($cookie, $value, $default = null, $validity = null,
1784 $httponly = true
1786 if (strlen($value) && null !== $default && $value === $default) {
1787 // default value is used
1788 if (isset($_COOKIE[$cookie])) {
1789 // remove cookie
1790 return $this->removeCookie($cookie);
1792 return false;
1795 if (! strlen($value) && isset($_COOKIE[$cookie])) {
1796 // remove cookie, value is empty
1797 return $this->removeCookie($cookie);
1800 if (! isset($_COOKIE[$cookie]) || $_COOKIE[$cookie] !== $value) {
1801 // set cookie with new value
1802 /* Calculate cookie validity */
1803 if ($validity === null) {
1804 $validity = time() + 2592000;
1805 } elseif ($validity == 0) {
1806 $validity = 0;
1807 } else {
1808 $validity = time() + $validity;
1810 if (defined('TESTSUITE')) {
1811 $_COOKIE[$cookie] = $value;
1812 return true;
1814 return setcookie(
1815 $cookie,
1816 $value,
1817 $validity,
1818 $this->getCookiePath(),
1820 $this->isHttps(),
1821 $httponly
1825 // cookie has already $value as value
1826 return true;