Context.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. * Logging level, as per RFC5424
  19. *
  20. * @link http://php.net/network.constants.php
  21. *
  22. * @var integer
  23. */
  24. protected $logLevel = NULL;
  25. /**
  26. * Messages for display.
  27. *
  28. * @var array
  29. */
  30. protected $messages = array();
  31. /**
  32. * Requested path: <$PHP_SELF>?q=a/b/c
  33. *
  34. * @var string
  35. */
  36. protected $path = NULL;
  37. /**
  38. * Tidy the output ?
  39. *
  40. * @var boolean
  41. */
  42. protected $tidy = NULL;
  43. /**
  44. * User information: logged or not ?
  45. *
  46. * @var boolean
  47. */
  48. protected $user = FALSE;
  49. function __construct() {
  50. $this->getTidy(); // Needed to check extension
  51. }
  52. function __destruct() {
  53. if (!empty($this->messages)) {
  54. $ret = (string) new Element('pre', array('class' => array('messages')),
  55. implode("\n", $this->getMessage(TRUE)));
  56. echo $ret;
  57. }
  58. }
  59. function __toString() {
  60. $ret = '<pre>' . print_r($this, TRUE) . '</pre>';
  61. return $ret;
  62. }
  63. /**
  64. * Get the base path where the script is located.
  65. *
  66. * This is helpful to locate other files using paths relative to the install.
  67. */
  68. public function getBase() {
  69. if (!isset($this->base)) {
  70. $this->base = dirname($_SERVER['SCRIPT_NAME']);
  71. if ($this->base == '/') {
  72. $this->base = '';
  73. }
  74. }
  75. return $this->base;
  76. }
  77. public function getLogLevel() {
  78. if (!isset($this->logLevel)) {
  79. $usLogLevel = NULL;
  80. foreach ($_GET as $key => $value) {
  81. if (strtolower($key) === 'loglevel') {
  82. $usLogLevel = (int) $value;
  83. break;
  84. }
  85. }
  86. if (!isset($usLogLevel)) {
  87. $usLogLevel = LOG_NOTICE;
  88. }
  89. if ($usLogLevel < LOG_EMERG) {
  90. $this->logLevel = LOG_EMERG;
  91. }
  92. elseif ($usLogLevel > LOG_DEBUG) {
  93. $this->logLevel = LOG_DEBUG;
  94. }
  95. else {
  96. $this->logLevel = $usLogLevel; // We now know it to be safe
  97. }
  98. }
  99. return $this->logLevel;
  100. }
  101. public function getLogLevelClass($logLevel) {
  102. if (!isset($this->logLevelClasses)) {
  103. $this->logLevelClasses = array(
  104. LOG_EMERG => 'error',
  105. LOG_ALERT => 'error',
  106. LOG_CRIT => 'error',
  107. LOG_ERR => 'error',
  108. LOG_WARNING => 'warning',
  109. LOG_NOTICE => 'warning',
  110. LOG_INFO => 'status',
  111. LOG_DEBUG => 'status',
  112. );
  113. }
  114. if ($logLevel < LOG_EMERG) {
  115. $logLevel = LOG_EMERG;
  116. }
  117. elseif ($logLevel > LOG_DEBUG) {
  118. $logLevel = LOG_DEBUG;
  119. }
  120. return $this->logLevelClasses[$logLevel];
  121. }
  122. public function getMessage($clear = FALSE) {
  123. $ret = $this->messages;
  124. if ($clear) {
  125. $this->messages = array();
  126. }
  127. return $ret;
  128. }
  129. /**
  130. * Return the requested path.
  131. *
  132. * @param string $path
  133. */
  134. public function getPath() {
  135. if (!isset($this->path)) {
  136. $this->path = empty($_GET['q']) ? '' : $_GET['q'];
  137. }
  138. return $this->path;
  139. }
  140. /**
  141. * Return the "tidy" status.
  142. *
  143. * Will only be TRUE if requested (possibly by default) and extension is
  144. * loaded. A warning will be generated if tidy is requested but extension is
  145. * not loaded.
  146. */
  147. public function getTidy() {
  148. static $notified = FALSE;
  149. if (!isset($this->tidy)) {
  150. $this->tidy = TRUE;
  151. foreach ($_GET as $key => $value) {
  152. if (strtolower($key) === 'tidy') {
  153. $this->tidy = !!$value;
  154. break;
  155. }
  156. }
  157. if (!$notified && $this->tidy && !extension_loaded('tidy')) {
  158. $this->setMessage(strtr('Extension @tidy requested but missing: skipping', array(
  159. '@tidy' => 'tidy',
  160. )), LOG_WARNING);
  161. $notified = TRUE;
  162. $this->tidy = FALSE;
  163. }
  164. }
  165. return $this->tidy;
  166. }
  167. /**
  168. * Add a message to the messages list if it is above the current logging level.
  169. *
  170. * @param string $text
  171. * @param integer $logLevel
  172. *
  173. * @return void
  174. */
  175. public function setMessage($text, $logLevel = LOG_NOTICE) {
  176. if ($logLevel <= $this->getlogLevel()) {
  177. if (is_array($text) || (is_object($text) && !method_exists($text, '__toString'))) {
  178. $this->messages[] = array('<pre>' . print_r($text, TRUE) . '</pre>', $logLevel);
  179. }
  180. else {
  181. $this->messages[] = array((string) $text, $logLevel);
  182. }
  183. }
  184. }
  185. }
  186. }