Context.inc 7.9 KB

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