Context.inc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace Memcache_UI\Core {
  3. class Context {
  4. protected $logLevelClasses = NULL;
  5. /**
  6. * Base URL for the script.
  7. *
  8. * @var string
  9. */
  10. protected $base;
  11. /**
  12. * Graphics context
  13. *
  14. * @var GraphicsContext
  15. */
  16. protected $gc = NULL;
  17. /**
  18. * Locale with encoding
  19. *
  20. * This is a static because a page runs for only one locale.
  21. */
  22. protected static $locale = 'en_US.UTF8';
  23. /**
  24. * Logging level, as per RFC5424
  25. *
  26. * @link http://php.net/network.constants.php
  27. *
  28. * @var integer
  29. */
  30. protected $logLevel = NULL;
  31. /**
  32. * Messages for display.
  33. *
  34. * @var array
  35. */
  36. protected $messages = array();
  37. /**
  38. * Requested path: <$PHP_SELF>?q=a/b/c
  39. *
  40. * @var string
  41. */
  42. protected $path = NULL;
  43. /**
  44. * Tidy the output ?
  45. *
  46. * @var boolean
  47. */
  48. protected $tidy = NULL;
  49. /**
  50. * User information: logged or not ?
  51. *
  52. * @var boolean
  53. */
  54. protected $user = FALSE;
  55. function __construct() {
  56. $this->initLocale(); // Check extension and initialize locale
  57. $this->getTidy(); // Needed to check optional extension
  58. }
  59. function __destruct() {
  60. if (!empty($this->messages)) {
  61. $ret = (string) new Element('pre', array('class' => array('messages')),
  62. implode("\n", $this->getMessage(TRUE)));
  63. echo $ret;
  64. }
  65. }
  66. function __toString() {
  67. $ret = '<pre>' . print_r($this, TRUE) . '</pre>';
  68. return $ret;
  69. }
  70. /**
  71. * Get the base path where the script is located.
  72. *
  73. * This is helpful to locate other files using paths relative to the install.
  74. */
  75. public function getBase() {
  76. if (!isset($this->base)) {
  77. $this->base = dirname($_SERVER['SCRIPT_NAME']);
  78. if ($this->base == '/') {
  79. $this->base = '';
  80. }
  81. }
  82. return $this->base;
  83. }
  84. public function getLogLevel() {
  85. if (!isset($this->logLevel)) {
  86. $usLogLevel = NULL;
  87. foreach ($_GET as $key => $value) {
  88. if (strtolower($key) === 'loglevel') {
  89. $usLogLevel = (int) $value;
  90. break;
  91. }
  92. }
  93. if (!isset($usLogLevel)) {
  94. $usLogLevel = LOG_NOTICE;
  95. }
  96. if ($usLogLevel < LOG_EMERG) {
  97. $this->logLevel = LOG_EMERG;
  98. }
  99. elseif ($usLogLevel > LOG_DEBUG) {
  100. $this->logLevel = LOG_DEBUG;
  101. }
  102. else {
  103. $this->logLevel = $usLogLevel; // We now know it to be safe
  104. }
  105. }
  106. return $this->logLevel;
  107. }
  108. public function getLogLevelClass($logLevel) {
  109. if (!isset($this->logLevelClasses)) {
  110. $this->logLevelClasses = array(
  111. LOG_EMERG => 'error',
  112. LOG_ALERT => 'error',
  113. LOG_CRIT => 'error',
  114. LOG_ERR => 'error',
  115. LOG_WARNING => 'warning',
  116. LOG_NOTICE => 'warning',
  117. LOG_INFO => 'status',
  118. LOG_DEBUG => 'status',
  119. );
  120. }
  121. if ($logLevel < LOG_EMERG) {
  122. $logLevel = LOG_EMERG;
  123. }
  124. elseif ($logLevel > LOG_DEBUG) {
  125. $logLevel = LOG_DEBUG;
  126. }
  127. return $this->logLevelClasses[$logLevel];
  128. }
  129. public function getMessage($clear = FALSE) {
  130. $ret = $this->messages;
  131. if ($clear) {
  132. $this->messages = array();
  133. }
  134. return $ret;
  135. }
  136. /**
  137. * Return the requested path.
  138. *
  139. * @param string $path
  140. */
  141. public function getPath() {
  142. if (!isset($this->path)) {
  143. $this->path = empty($_GET['q']) ? '' : $_GET['q'];
  144. }
  145. return $this->path;
  146. }
  147. /**
  148. * Return the "tidy" status.
  149. *
  150. * Will only be TRUE if requested (possibly by default) and extension is
  151. * loaded. A warning will be generated if tidy is requested but extension is
  152. * not loaded.
  153. */
  154. public function getTidy() {
  155. static $notified = FALSE;
  156. if (!isset($this->tidy)) {
  157. $this->tidy = TRUE;
  158. foreach ($_GET as $key => $value) {
  159. if (strtolower($key) === 'tidy') {
  160. $this->tidy = !!$value;
  161. break;
  162. }
  163. }
  164. if (!$notified && $this->tidy && !extension_loaded('tidy')) {
  165. $this->setMessage(t('Extension @tidy requested but missing: output formatting unavailable.', array(
  166. '@tidy' => 'tidy',
  167. )), LOG_WARNING);
  168. $notified = TRUE;
  169. $this->tidy = FALSE;
  170. }
  171. }
  172. return $this->tidy;
  173. }
  174. /**
  175. * Initialize the locale based on the user Accept-Language header.
  176. *
  177. * @TODO support "xx" form, and not just "xx_YY".
  178. * @TODO support more platforms. UNIX and MacOS X do not handle locales like Linux
  179. *
  180. * @link @link http://www.php.net/manual/fr/function.bind-textdomain-codeset.php#42631
  181. *
  182. * @return void
  183. */
  184. protected function initLocale() {
  185. static $notified = FALSE;
  186. if (!$notified && !extension_loaded('intl')) {
  187. // Do not invoke t() before initializing locale
  188. $this->setMessage(strtr('Extension @intl requested but missing: translations unavailable.', array(
  189. '@intl' => 'intl',
  190. )), LOG_WARNING);
  191. }
  192. else {
  193. $locale = \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
  194. $this->setMessage(strtr('Requested locale: @locale', array('@locale' => $locale)), LOG_DEBUG);
  195. if (!empty($locale)) {
  196. $matches = array();
  197. $count = preg_match('/^([a-z]{2}_[A-Z]{2})(\.(\w+))*/', $locale, $matches);
  198. if ($count) {
  199. $count = count($matches);
  200. }
  201. $locale = $count >= 2
  202. ? $matches[1]
  203. : 'en_US';
  204. $codeset = $count >= 4
  205. ? $matches[3]
  206. : 'UTF8';
  207. self::$locale = $locale . '.' . $codeset;
  208. setlocale(LC_ALL, self::$locale);
  209. $domain = 'messages';
  210. $translation_path = 'locale';
  211. bindtextdomain($domain, $translation_path);
  212. textdomain($domain);
  213. bind_textdomain_codeset($domain, 'UTF8');
  214. $this->setMessage(self::t('Locale: @locale', array('@locale' => self::$locale)), LOG_DEBUG);
  215. }
  216. }
  217. }
  218. /**
  219. * Add a message to the messages list if it is above the current logging level.
  220. *
  221. * @param string $text
  222. * @param integer $logLevel
  223. *
  224. * @return void
  225. */
  226. public function setMessage($text, $logLevel = LOG_NOTICE) {
  227. if ($logLevel <= $this->getlogLevel()) {
  228. if (is_array($text) || (is_object($text) && !method_exists($text, '__toString'))) {
  229. $this->messages[] = array('<pre>' . print_r($text, TRUE) . '</pre>', $logLevel);
  230. }
  231. else {
  232. $this->messages[] = array((string) $text, $logLevel);
  233. }
  234. }
  235. }
  236. /**
  237. * Wrapper to combine gettext translation and parameter substitution.
  238. *
  239. * @param string $message
  240. * @param array $args
  241. *
  242. * @return string
  243. */
  244. static function t($message, $args = array()) {
  245. return strtr(gettext($message), $args);
  246. }
  247. }
  248. }