/lib/configonlylib.php

https://github.com/nigeldaley/moodle · PHP · 118 lines · 47 code · 13 blank · 58 comment · 8 complexity · 8a43301816ba98f6aeb971f30899f093 MD5 · raw file

  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Minimalistic library, usable even when no other moodle libs are loaded.
  18. *
  19. * The only library that gets loaded if you define ABORT_AFTER_CONFIG
  20. * before including main config.php. You can resume normal script operation
  21. * if you define ABORT_AFTER_CONFIG_CANCEL and require the setup.php
  22. *
  23. * @package core
  24. * @copyright 2009 Petr Skoda (skodak)
  25. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26. */
  27. /**
  28. * Minimalistic parameter validation function.
  29. * Can not use optional param because moodlelib.php is not loaded yet
  30. * @param string $name
  31. * @param mixed $default
  32. * @param string $type
  33. * @return mixed
  34. */
  35. function min_optional_param($name, $default, $type) {
  36. $value = $default;
  37. if (isset($_GET[$name])) {
  38. $value = $_GET[$name];
  39. } else if (isset($_GET['amp;'.$name])) {
  40. // very, very, very ugly hack, unfortunately $OUTPUT->pix_url() is not used properly in javascript code :-(
  41. $value = $_GET['amp;'.$name];
  42. }
  43. return min_clean_param($value, $type);
  44. }
  45. /**
  46. * Minimalistic parameter cleaning function.
  47. * Can not use optional param because moodlelib.php is not loaded yet
  48. * @param string $name
  49. * @param mixed $default
  50. * @param string $type
  51. * @return mixed
  52. */
  53. function min_clean_param($value, $type) {
  54. switch($type) {
  55. case 'RAW': $value = iconv('UTF-8', 'UTF-8//IGNORE', $value);
  56. break;
  57. case 'INT': $value = (int)$value;
  58. break;
  59. case 'SAFEDIR': $value = preg_replace('/[^a-zA-Z0-9_-]/', '', $value);
  60. break;
  61. case 'SAFEPATH': $value = preg_replace('/[^a-zA-Z0-9\/\._-]/', '', $value);
  62. $value = preg_replace('/\.+/', '.', $value);
  63. $value = preg_replace('#/+#', '/', $value);
  64. break;
  65. default: die("Coding error: incorrect parameter type specified ($type).");
  66. }
  67. return $value;
  68. }
  69. /**
  70. * This method tries to enable output compression if possible.
  71. * This function must be called before any output or headers.
  72. *
  73. * (IE6 is not supported at all.)
  74. *
  75. * @return boolean, true if compression enabled
  76. */
  77. function min_enable_zlib_compression() {
  78. if (headers_sent()) {
  79. return false;
  80. }
  81. // zlib.output_compression is preferred over ob_gzhandler()
  82. if (!empty($_SERVER['HTTP_USER_AGENT'])) {
  83. $agent = $_SERVER['HTTP_USER_AGENT'];
  84. // try to detect IE6 and prevent gzip because it is extremely buggy browser
  85. $parts = explode(';', $agent);
  86. if (isset($parts[1])) {
  87. $parts = explode(' ', trim($parts[1]));
  88. if (count($parts) > 1) {
  89. if ($parts[0] === 'MSIE' and (float)$parts[1] < 7) {
  90. @ini_set('zlib.output_compression', '0');
  91. return false;
  92. }
  93. }
  94. }
  95. }
  96. @ini_set('output_handler', '');
  97. /*
  98. * docs clearly say 'on' means enable and number means size of buffer,
  99. * but unfortunately some PHP version break when we set 'on' here.
  100. * 1 probably sets chunk size to 4096. our CSS and JS scripts are much bigger,
  101. * so let's try some bigger sizes.
  102. */
  103. @ini_set('zlib.output_compression', 65536);
  104. return true;
  105. }