[1128] | 1 | <?php |
---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
| 3 | # |
---|
| 4 | # This file is part of Dotclear 2. |
---|
| 5 | # |
---|
| 6 | # Copyright (c) 2003-2011 Olivier Meunier & Association Dotclear |
---|
| 7 | # Licensed under the GPL version 2.0 license. |
---|
| 8 | # See LICENSE file or |
---|
| 9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
| 10 | # |
---|
| 11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
| 12 | if (!defined('DC_RC_PATH')) { return; } |
---|
| 13 | |
---|
| 14 | /** |
---|
| 15 | * @ingroup DC_CORE |
---|
| 16 | * @brief Template extension for public context |
---|
| 17 | * |
---|
| 18 | * This extends template environment with tools required in context. |
---|
| 19 | * Admin context and public context should extend this class. |
---|
| 20 | */ |
---|
| 21 | class dcContext extends Twig_Extension |
---|
| 22 | { |
---|
| 23 | protected $core; |
---|
| 24 | protected $globals = array(); |
---|
| 25 | protected $protected_globals = array(); |
---|
| 26 | protected $memory = array(); |
---|
[1138] | 27 | protected $timestamp_messages = true; |
---|
[1128] | 28 | |
---|
| 29 | public function __construct($core) |
---|
| 30 | { |
---|
| 31 | $this->core = $core; |
---|
| 32 | |
---|
| 33 | # Globals editable via context |
---|
| 34 | $this->globals = array(); |
---|
| 35 | |
---|
| 36 | # Globals not editable via context |
---|
| 37 | $this->protected_globals = array( |
---|
| 38 | 'messages' => array( |
---|
| 39 | 'static' => array(), |
---|
| 40 | 'lists' => array(), |
---|
| 41 | 'alert' => '', |
---|
| 42 | 'errors' => array() |
---|
| 43 | ), |
---|
| 44 | |
---|
| 45 | 'version' => DC_VERSION, |
---|
| 46 | 'vendor_name' => DC_VENDOR_NAME, |
---|
| 47 | |
---|
| 48 | 'safe_mode' => isset($_SESSION['sess_safe_mode']) && $_SESSION['sess_safe_mode'], |
---|
| 49 | 'debug_mode' => DC_DEBUG |
---|
| 50 | ); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | /** |
---|
| 54 | * Prevent call crash from template on method that return this class |
---|
| 55 | */ |
---|
| 56 | public function __toString() |
---|
| 57 | { |
---|
| 58 | return ''; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * Test a global variable |
---|
| 63 | * |
---|
| 64 | * @param string $name Name of the variable to test |
---|
| 65 | * @return boolean |
---|
| 66 | */ |
---|
| 67 | public function __isset($name) |
---|
| 68 | { |
---|
| 69 | return isset($this->globals[$name]); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | * Add a global variable |
---|
| 74 | * |
---|
| 75 | * @param string $name Name of the variable |
---|
| 76 | * @param mixed $value Value of the variable |
---|
| 77 | */ |
---|
| 78 | public function __set($name,$value) |
---|
| 79 | { |
---|
| 80 | /* |
---|
| 81 | # Overload protect |
---|
| 82 | if ($value === null && isset($this->globals[$name])) { |
---|
| 83 | unset($this->globals[$name]); |
---|
| 84 | } |
---|
| 85 | elseif (!isset($this->globals[$name])) { |
---|
| 86 | throw new Exception('Modification of overloaded globals has no effect'); |
---|
| 87 | } |
---|
| 88 | //*/ |
---|
| 89 | $this->globals[$name] = $value; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | * Get a global variable |
---|
| 94 | * |
---|
| 95 | * @param string $name Name of the variable |
---|
| 96 | * @return mixed Value of the variable or null |
---|
| 97 | */ |
---|
| 98 | public function __get($name) |
---|
| 99 | { |
---|
| 100 | return isset($this->globals[$name]) ? $this->globals[$name] : null; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | * Returns a list of filters to add to the existing list. |
---|
| 105 | * |
---|
| 106 | * @return array An array of filters |
---|
| 107 | */ |
---|
| 108 | public function getFilters() |
---|
| 109 | { |
---|
| 110 | return array( |
---|
| 111 | 'trans' => new Twig_Filter_Function("__", array('is_safe' => array('html'))) |
---|
| 112 | ); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | /** |
---|
| 116 | * Returns a list of functions to add to the existing list. |
---|
| 117 | * |
---|
| 118 | * @return array An array of functions |
---|
| 119 | */ |
---|
| 120 | public function getFunctions() |
---|
| 121 | { |
---|
| 122 | return array( |
---|
| 123 | '__' => new Twig_Function_Function("__", array('is_safe' => array('html'))), |
---|
| 124 | 'debug_info' => new Twig_Function_Method($this, 'getDebugInfo', array('is_safe' => array('html'))), |
---|
| 125 | 'memorize' => new Twig_Function_Method($this, 'setMemory', array('is_safe' => array('html'))), |
---|
| 126 | 'memorized' => new Twig_Function_Method($this, 'getMemory', array('is_safe' => array('html'))) |
---|
| 127 | ); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /** |
---|
| 131 | * Returns a list of global variables to add to the existing list. |
---|
| 132 | * |
---|
| 133 | * This merges overloaded variables with defined variables. |
---|
| 134 | * |
---|
| 135 | * @return array An array of global variables |
---|
| 136 | */ |
---|
| 137 | public function getGlobals() |
---|
| 138 | { |
---|
| 139 | # Keep protected globals safe |
---|
| 140 | return array_merge($this->globals,$this->protected_globals); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | /** |
---|
| 144 | * Returns the name of the extension. |
---|
| 145 | * |
---|
| 146 | * @return string The extension name |
---|
| 147 | */ |
---|
| 148 | public function getName() |
---|
| 149 | { |
---|
| 150 | return 'dcContext'; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | |
---|
| 154 | /** |
---|
| 155 | * Add an informational message |
---|
| 156 | * |
---|
| 157 | * @param string $message A message |
---|
| 158 | * @return object self |
---|
| 159 | */ |
---|
| 160 | public function setSafeMode($safe_mode) |
---|
| 161 | { |
---|
| 162 | $this->protected_globals['safe_mode'] = (boolean) $safe_mode; |
---|
| 163 | return $this; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | /** |
---|
| 167 | * Add an informational message |
---|
| 168 | * |
---|
| 169 | * @param string $message A message |
---|
| 170 | * @return object self |
---|
| 171 | */ |
---|
| 172 | public function addMessageStatic($message) |
---|
| 173 | { |
---|
[1138] | 174 | $this->protected_globals['messages']['static'][] = $this->timeStamp($message); |
---|
[1128] | 175 | return $this; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | /** |
---|
| 179 | * Add a list of informational messages |
---|
| 180 | * |
---|
| 181 | * @param string $message A title |
---|
| 182 | * @param array $message A list of messages |
---|
| 183 | * @return object self |
---|
| 184 | */ |
---|
| 185 | public function addMessagesList($title,$messages) |
---|
| 186 | { |
---|
[1138] | 187 | $this->protected_globals['messages']['lists'][$title] = $this->timeStamp($messages); |
---|
[1128] | 188 | return $this; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | /** |
---|
| 192 | * Set an important message |
---|
| 193 | * |
---|
| 194 | * @param string $message A message |
---|
| 195 | * @return object self |
---|
| 196 | */ |
---|
| 197 | public function setAlert($message) |
---|
| 198 | { |
---|
[1138] | 199 | $this->protected_globals['messages']['alert'] = $this->timeStamp($message); |
---|
[1128] | 200 | return $this; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | /** |
---|
| 204 | * Add an error message |
---|
| 205 | * |
---|
| 206 | * @param string Error message |
---|
| 207 | * @return object self |
---|
| 208 | */ |
---|
| 209 | public function addError($error) |
---|
| 210 | { |
---|
[1138] | 211 | $this->protected_globals['messages']['errors'][] = $this->timeStamp($error); |
---|
[1128] | 212 | return $this; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | /** |
---|
| 216 | * Check if there is an error message |
---|
| 217 | * |
---|
| 218 | * @return boolean |
---|
| 219 | */ |
---|
| 220 | public function hasError() |
---|
| 221 | { |
---|
| 222 | return !empty($this->protected_globals['messages']['errors']); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | /** |
---|
[1138] | 226 | * Set use of timestamp on messages |
---|
| 227 | * |
---|
| 228 | * @param boolean $add Add timestamp to messages |
---|
| 229 | * @return object self |
---|
| 230 | */ |
---|
| 231 | public function setTimestamp($add=true) |
---|
| 232 | { |
---|
| 233 | $this->timestamp_messages; |
---|
| 234 | return $this; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | /** |
---|
| 238 | * Prepend message with a timestamp |
---|
| 239 | * |
---|
| 240 | * @param string|array $message Message(s) |
---|
| 241 | * @return string|array Timestamp message(s) |
---|
| 242 | */ |
---|
| 243 | public function timeStamp($message) |
---|
| 244 | { |
---|
| 245 | if (!$this->timestamp_messages) { |
---|
| 246 | return $message; |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | $tz = @$this->core->auth->getInfo('user_tz'); |
---|
| 250 | if (!$tz) { |
---|
| 251 | $tz = $this->core->blog->settings->system->blog_timezone; |
---|
| 252 | } |
---|
| 253 | $dt = dt::str(__('%H:%M:%S:'),null,$tz); |
---|
| 254 | |
---|
| 255 | if (!is_array($message)) { |
---|
| 256 | return $dt.' '.$message; |
---|
| 257 | } else { |
---|
| 258 | foreach($message as $k => $v) |
---|
| 259 | { |
---|
| 260 | $message[$k] = $dt.' '.$v; |
---|
| 261 | } |
---|
| 262 | return $message; |
---|
| 263 | } |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | /** |
---|
[1128] | 267 | * Get an array of debug/dev infos |
---|
| 268 | */ |
---|
| 269 | public function getDebugInfo() |
---|
| 270 | { |
---|
| 271 | if (!DC_DEBUG) { |
---|
| 272 | return array(); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | $di = array( |
---|
| 276 | 'global_vars' => implode(', ',array_keys($GLOBALS)), |
---|
| 277 | 'memory' => array( |
---|
| 278 | 'usage' => memory_get_usage(), |
---|
| 279 | 'size' => files::size(memory_get_usage()) |
---|
| 280 | ), |
---|
| 281 | 'xdebug' => array() |
---|
| 282 | ); |
---|
| 283 | |
---|
| 284 | if (function_exists('xdebug_get_profiler_filename')) { |
---|
| 285 | |
---|
| 286 | $url = http::getSelfURI(); |
---|
| 287 | $url .= strpos($url,'?') === false ? '?' : '&'; |
---|
| 288 | $url .= 'XDEBUG_PROFILE'; |
---|
| 289 | |
---|
| 290 | $di['xdebug'] = array( |
---|
| 291 | 'elapse_time' => xdebug_time_index(), |
---|
| 292 | 'profiler_file' => xdebug_get_profiler_filename(), |
---|
| 293 | 'profiler_url' => $url |
---|
| 294 | ); |
---|
| 295 | |
---|
| 296 | /* xdebug configuration: |
---|
| 297 | zend_extension = /.../xdebug.so |
---|
| 298 | xdebug.auto_trace = On |
---|
| 299 | xdebug.trace_format = 0 |
---|
| 300 | xdebug.trace_options = 1 |
---|
| 301 | xdebug.show_mem_delta = On |
---|
| 302 | xdebug.profiler_enable = 0 |
---|
| 303 | xdebug.profiler_enable_trigger = 1 |
---|
| 304 | xdebug.profiler_output_dir = /tmp |
---|
| 305 | xdebug.profiler_append = 0 |
---|
| 306 | xdebug.profiler_output_name = timestamp |
---|
| 307 | */ |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | return $di; |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | /** |
---|
| 314 | * Add a value in a namespace memory |
---|
| 315 | * |
---|
| 316 | * This help keep variable when recalling Twig macros |
---|
| 317 | * |
---|
| 318 | * @param string $ns A namespace |
---|
| 319 | * @param string $str A value to memorize in this namespace |
---|
| 320 | */ |
---|
| 321 | public function setMemory($ns,$str) |
---|
| 322 | { |
---|
| 323 | if (!array_key_exists($ns,$this->memory) || !in_array($str,$this->memory[$ns])) { |
---|
| 324 | $this->memory[$ns][] = $str; |
---|
| 325 | } |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | /** |
---|
| 329 | * Check if a value is previously memorized in a namespace |
---|
| 330 | * |
---|
| 331 | * @param string $ns A namespace |
---|
| 332 | * @param string $str A value to search in this namespace |
---|
| 333 | * @return array True if exists |
---|
| 334 | */ |
---|
| 335 | public function getMemory($ns,$str) |
---|
| 336 | { |
---|
| 337 | return array_key_exists($ns,$this->memory) && in_array($str,$this->memory[$ns]); |
---|
| 338 | } |
---|
| 339 | } |
---|
| 340 | ?> |
---|