123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- <?php
- namespace Memcache_UI\Core {
- class Context {
- protected $logLevelClasses = NULL;
- /**
- * Base URL for the script.
- *
- * @var string
- */
- protected $base;
- /**
- * Graphics context
- *
- * @var GraphicsContext
- */
- protected $gc = NULL;
- /**
- * Locale with encoding
- *
- * This is a static because a page runs for only one locale.
- */
- protected static $locale = 'en_US.UTF8';
- /**
- * Logging level, as per RFC5424
- *
- * @link http://php.net/network.constants.php
- *
- * @var integer
- */
- protected $logLevel = NULL;
- /**
- * Messages for display.
- *
- * @var array
- */
- protected $messages = array();
- /**
- * Requested path: <$PHP_SELF>?q=a/b/c
- *
- * @var string
- */
- protected $path = NULL;
- /**
- * Tidy the output ?
- *
- * @var boolean
- */
- protected $tidy = NULL;
- /**
- * User information: logged or not ?
- *
- * @var boolean
- */
- protected $user = FALSE;
- function __construct() {
- $this->initLocale(); // Check extension and initialize locale
- $this->getTidy(); // Needed to check optional extension
- }
- function __destruct() {
- if (!empty($this->messages)) {
- $ret = (string) new Element('pre', array('class' => array('messages')),
- implode("\n", $this->getMessage(TRUE)));
- echo $ret;
- }
- }
- function __toString() {
- $ret = '<pre>' . print_r($this, TRUE) . '</pre>';
- return $ret;
- }
- /**
- * Get the base path where the script is located.
- *
- * This is helpful to locate other files using paths relative to the install.
- */
- public function getBase() {
- if (!isset($this->base)) {
- $this->base = dirname($_SERVER['SCRIPT_NAME']);
- if ($this->base == '/') {
- $this->base = '';
- }
- }
- return $this->base;
- }
- public function getLogLevel() {
- if (!isset($this->logLevel)) {
- $usLogLevel = NULL;
- foreach ($_GET as $key => $value) {
- if (strtolower($key) === 'loglevel') {
- $usLogLevel = (int) $value;
- break;
- }
- }
- if (!isset($usLogLevel)) {
- $usLogLevel = LOG_NOTICE;
- }
- if ($usLogLevel < LOG_EMERG) {
- $this->logLevel = LOG_EMERG;
- }
- elseif ($usLogLevel > LOG_DEBUG) {
- $this->logLevel = LOG_DEBUG;
- }
- else {
- $this->logLevel = $usLogLevel; // We now know it to be safe
- }
- }
- return $this->logLevel;
- }
- public function getLogLevelClass($logLevel) {
- if (!isset($this->logLevelClasses)) {
- $this->logLevelClasses = array(
- LOG_EMERG => 'error',
- LOG_ALERT => 'error',
- LOG_CRIT => 'error',
- LOG_ERR => 'error',
- LOG_WARNING => 'warning',
- LOG_NOTICE => 'warning',
- LOG_INFO => 'status',
- LOG_DEBUG => 'status',
- );
- }
- if ($logLevel < LOG_EMERG) {
- $logLevel = LOG_EMERG;
- }
- elseif ($logLevel > LOG_DEBUG) {
- $logLevel = LOG_DEBUG;
- }
- return $this->logLevelClasses[$logLevel];
- }
- public function getMessage($clear = FALSE) {
- $ret = $this->messages;
- if ($clear) {
- $this->messages = array();
- }
- return $ret;
- }
- /**
- * Return the requested path.
- *
- * @param string $path
- */
- public function getPath() {
- if (!isset($this->path)) {
- $this->path = empty($_GET['q']) ? '' : $_GET['q'];
- }
- return $this->path;
- }
- /**
- * Return the "tidy" status.
- *
- * Will only be TRUE if requested (possibly by default) and extension is
- * loaded. A warning will be generated if tidy is requested but extension is
- * not loaded.
- */
- public function getTidy() {
- static $notified = FALSE;
- if (!isset($this->tidy)) {
- $this->tidy = TRUE;
- foreach ($_GET as $key => $value) {
- if (strtolower($key) === 'tidy') {
- $this->tidy = !!$value;
- break;
- }
- }
- if (!$notified && $this->tidy && !extension_loaded('tidy')) {
- $this->setMessage(t('Extension @tidy requested but missing: output formatting unavailable.', array(
- '@tidy' => 'tidy',
- )), LOG_WARNING);
- $notified = TRUE;
- $this->tidy = FALSE;
- }
- }
- return $this->tidy;
- }
- /**
- * Initialize the locale based on the user Accept-Language header.
- *
- * @TODO support "xx" form, and not just "xx_YY".
- * @TODO support more platforms. UNIX and MacOS X do not handle locales like Linux
- *
- * @link @link http://www.php.net/manual/fr/function.bind-textdomain-codeset.php#42631
- *
- * @return void
- */
- protected function initLocale() {
- static $notified = FALSE;
- if (!$notified && !extension_loaded('intl')) {
- // Do not invoke t() before initializing locale
- $this->setMessage(strtr('Extension @intl requested but missing: translations unavailable.', array(
- '@intl' => 'intl',
- )), LOG_WARNING);
- }
- else {
- $locale = \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
- $this->setMessage(strtr('Requested locale: @locale', array('@locale' => $locale)), LOG_DEBUG);
- if (!empty($locale)) {
- $matches = array();
- $count = preg_match('/^([a-z]{2}_[A-Z]{2})(\.(\w+))*/', $locale, $matches);
- if ($count) {
- $count = count($matches);
- }
- $locale = $count >= 2
- ? $matches[1]
- : 'en_US';
- $codeset = $count >= 4
- ? $matches[3]
- : 'UTF8';
- self::$locale = $locale . '.' . $codeset;
- setlocale(LC_ALL, self::$locale);
- $domain = 'messages';
- $translation_path = 'locale';
- bindtextdomain($domain, $translation_path);
- textdomain($domain);
- bind_textdomain_codeset($domain, 'UTF8');
- $this->setMessage(self::t('Locale: @locale', array('@locale' => self::$locale)), LOG_DEBUG);
- }
- }
- }
- /**
- * Add a message to the messages list if it is above the current logging level.
- *
- * @param string $text
- * @param integer $logLevel
- *
- * @return void
- */
- public function setMessage($text, $logLevel = LOG_NOTICE) {
- if ($logLevel <= $this->getlogLevel()) {
- if (is_array($text) || (is_object($text) && !method_exists($text, '__toString'))) {
- $this->messages[] = array('<pre>' . print_r($text, TRUE) . '</pre>', $logLevel);
- }
- else {
- $this->messages[] = array((string) $text, $logLevel);
- }
- }
- }
- /**
- * Wrapper to combine gettext translation and parameter substitution.
- *
- * @param string $message
- * @param array $args
- *
- * @return string
- */
- static function t($message, $args = array()) {
- return strtr(gettext($message), $args);
- }
- }
- }
|