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 admin context |
---|
17 | |
---|
18 | This extends template environment with tools required in admin context. |
---|
19 | */ |
---|
20 | class dcAdminContext extends Twig_Extension |
---|
21 | { |
---|
22 | protected $core; |
---|
23 | protected $globals = array(); |
---|
24 | protected $protected_globals = array(); |
---|
25 | |
---|
26 | public function __construct($core) |
---|
27 | { |
---|
28 | $this->core = $core; |
---|
29 | |
---|
30 | # Globals editable via context |
---|
31 | $this->globals = array(); |
---|
32 | |
---|
33 | # Globals not editable via context |
---|
34 | $this->protected_globals = array( |
---|
35 | 'page_message' => '', |
---|
36 | 'page_errors' => array(), |
---|
37 | 'page_title' => '', |
---|
38 | |
---|
39 | 'admin_url' => DC_ADMIN_URL, |
---|
40 | 'theme_url' => DC_ADMIN_URL.'index.php?tf=', |
---|
41 | 'plugin_url' => DC_ADMIN_URL.'index.php?pf=', |
---|
42 | |
---|
43 | 'version' => DC_VERSION, |
---|
44 | 'vendor_name' => DC_VENDOR_NAME, |
---|
45 | |
---|
46 | 'safe_mode' => isset($_SESSION['sess_safe_mode']) && $_SESSION['sess_safe_mode'], |
---|
47 | 'debug_mode' => DC_DEBUG |
---|
48 | ); |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | Prevent call crash from template on method that return this class |
---|
53 | */ |
---|
54 | public function __toString() |
---|
55 | { |
---|
56 | return ''; |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | Test a global variable |
---|
61 | |
---|
62 | @param string $name Name of the variable to test |
---|
63 | @return boolean |
---|
64 | */ |
---|
65 | public function __isset($name) |
---|
66 | { |
---|
67 | return isset($this->globals[$name]); |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | Add a global variable |
---|
72 | |
---|
73 | @param string $name Name of the variable |
---|
74 | @param mixed $value Value of the variable |
---|
75 | */ |
---|
76 | public function __set($name,$value) |
---|
77 | { |
---|
78 | /* |
---|
79 | # Overload protect |
---|
80 | if ($value === null && isset($this->globals[$name])) { |
---|
81 | unset($this->globals[$name]); |
---|
82 | } |
---|
83 | elseif (!isset($this->globals[$name])) { |
---|
84 | throw new Exception('Modification of overloaded globals has no effect'); |
---|
85 | } |
---|
86 | //*/ |
---|
87 | $this->globals[$name] = $value; |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | Get a global variable |
---|
92 | |
---|
93 | @param string $name Name of the variable |
---|
94 | @return mixed Value of the variable or null |
---|
95 | */ |
---|
96 | public function __get($name) |
---|
97 | { |
---|
98 | return isset($this->globals[$name]) ? $this->globals[$name] : null; |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | Returns a list of filters to add to the existing list. |
---|
103 | |
---|
104 | @return array An array of filters |
---|
105 | */ |
---|
106 | public function getFilters() |
---|
107 | { |
---|
108 | return array( |
---|
109 | 'trans' => new Twig_Filter_Function("__", array('is_safe' => array('html'))) |
---|
110 | ); |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | Returns a list of functions to add to the existing list. |
---|
115 | |
---|
116 | @return array An array of functions |
---|
117 | */ |
---|
118 | public function getFunctions() |
---|
119 | { |
---|
120 | return array( |
---|
121 | '__' => new Twig_Function_Function("__", array('is_safe' => array('html'))), |
---|
122 | 'debug_info' => new Twig_Function_Method($this, 'getDebugInfo', array('is_safe' => array('html'))) |
---|
123 | //,'page_menu' => new Twig_Function_Method($this, 'pageMenu', array('is_safe' => array('html'))) |
---|
124 | ); |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | Returns a list of global variables to add to the existing list. |
---|
129 | |
---|
130 | This merges overloaded variables with defined variables. |
---|
131 | |
---|
132 | @return array An array of global variables |
---|
133 | */ |
---|
134 | public function getGlobals() |
---|
135 | { |
---|
136 | $this->getBlogs(); |
---|
137 | $this->getCurrentBlog(); |
---|
138 | $this->getCurrentUser(); |
---|
139 | $this->getMenus(); |
---|
140 | |
---|
141 | # Additional globals |
---|
142 | $p = path::info($_SERVER['REQUEST_URI']); |
---|
143 | $this->protected_globals['current_page'] = $p['base']; |
---|
144 | $this->protected_globals['blog_count'] = $this->core->auth->blog_count; |
---|
145 | |
---|
146 | # Keep protected globals safe |
---|
147 | return array_merge($this->globals,$this->protected_globals); |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * Returns the name of the extension. |
---|
152 | * |
---|
153 | * @return string The extension name |
---|
154 | */ |
---|
155 | public function getName() |
---|
156 | { |
---|
157 | return 'AdminContext'; |
---|
158 | } |
---|
159 | |
---|
160 | public function setSafeMode($safe_mode) |
---|
161 | { |
---|
162 | $this->protected_globals['safe_mode'] = (boolean) $safe_mode; |
---|
163 | return $this; |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | Set information message |
---|
168 | |
---|
169 | @param string $message A message |
---|
170 | @return object self |
---|
171 | */ |
---|
172 | public function setMessage($message) |
---|
173 | { |
---|
174 | $this->protected_globals['page_message'] = $message; |
---|
175 | return $this; |
---|
176 | } |
---|
177 | |
---|
178 | /** |
---|
179 | Add an error message |
---|
180 | |
---|
181 | @param string Error message |
---|
182 | @return object self |
---|
183 | */ |
---|
184 | public function addError($error) |
---|
185 | { |
---|
186 | $this->protected_globals['page_errors'][] = $error; |
---|
187 | return $this; |
---|
188 | } |
---|
189 | |
---|
190 | /** |
---|
191 | Check if there is an error message |
---|
192 | |
---|
193 | @return boolean |
---|
194 | */ |
---|
195 | public function hasError() |
---|
196 | { |
---|
197 | return !empty($this->protected_globals['page_errors']); |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | Add page title |
---|
202 | */ |
---|
203 | public function setPageTitle($title) |
---|
204 | { |
---|
205 | $this->protected_globals['page_title'] = $title; |
---|
206 | } |
---|
207 | |
---|
208 | /** |
---|
209 | Check if a page title is set |
---|
210 | */ |
---|
211 | public function hasPageTitle() |
---|
212 | { |
---|
213 | return !empty($this->protected_globals['page_title']); |
---|
214 | } |
---|
215 | |
---|
216 | /** |
---|
217 | * Get list of blogs |
---|
218 | */ |
---|
219 | protected function getBlogs() |
---|
220 | { |
---|
221 | $blog_id = ''; |
---|
222 | |
---|
223 | # Blogs list |
---|
224 | $blogs = array(); |
---|
225 | if ($this->core->auth->blog_count > 1 && $this->core->auth->blog_count < 20) { |
---|
226 | $blog_id = $this->core->blog->id; |
---|
227 | $rs_blogs = $this->core->getBlogs(array('order'=>'LOWER(blog_name)','limit'=>20)); |
---|
228 | while ($rs_blogs->fetch()) { |
---|
229 | $blogs[$rs_blogs->blog_id] = $rs_blogs->blog_name.' - '.$rs_blogs->blog_url; |
---|
230 | $this->protected_globals['blogs'][$rs_blogs->blog_id] = array( |
---|
231 | 'id' => $rs_blogs->blog_id, |
---|
232 | 'name' => $rs_blogs->blog_name, |
---|
233 | 'desc' => $rs_blogs->blog_desc, |
---|
234 | 'url' => $rs_blogs->blog_url, |
---|
235 | 'creadt' => $rs_blogs->blog_creadt, |
---|
236 | 'upddt' => $rs_blogs->blog_upddt |
---|
237 | ); |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | # Switch blog form |
---|
242 | $form = new dcForm($this->core,'switchblog_menu','index.php'); |
---|
243 | $form |
---|
244 | ->addField( |
---|
245 | new dcFieldCombo('switchblog',$blog_id,$blogs,array( |
---|
246 | 'label' => __('Blogs:')))) |
---|
247 | ->addField( |
---|
248 | new dcFieldSubmit('switchblog_submit',__('ok'),array( |
---|
249 | 'action' => 'switchblog'))) |
---|
250 | ->setup(); |
---|
251 | } |
---|
252 | |
---|
253 | /** |
---|
254 | * Get current blog information |
---|
255 | */ |
---|
256 | protected function getCurrentBlog() |
---|
257 | { |
---|
258 | $this->protected_globals['current_blog'] = $this->core->auth->blog_count ? |
---|
259 | array( |
---|
260 | 'id' => $this->core->blog->id, |
---|
261 | 'name' => $this->core->blog->name, |
---|
262 | 'desc' => $this->core->blog->desc, |
---|
263 | 'url' => $this->core->blog->url, |
---|
264 | 'host' => $this->core->blog->host, |
---|
265 | 'creadt' => $this->core->blog->creadt, |
---|
266 | 'upddt' => $this->core->blog->upddt |
---|
267 | ) : array( |
---|
268 | 'id' => '', |
---|
269 | 'name' => '', |
---|
270 | 'desc' => '', |
---|
271 | 'url' => '', |
---|
272 | 'host' => '', |
---|
273 | 'creadt' => '', |
---|
274 | 'upddt' => '' |
---|
275 | ); |
---|
276 | } |
---|
277 | |
---|
278 | /** |
---|
279 | * Get current user information |
---|
280 | */ |
---|
281 | protected function getCurrentUser() |
---|
282 | { |
---|
283 | $this->protected_globals['current_user'] = $this->core->auth->userID() ? |
---|
284 | array( |
---|
285 | 'id' => $this->core->auth->userID(), |
---|
286 | 'admin' => $this->core->auth->getInfo('user_admin'), |
---|
287 | 'name' => $this->core->auth->getInfo('user_name'), |
---|
288 | 'firstname' => $this->core->auth->getInfo('user_firstname'), |
---|
289 | 'displayname' => $this->core->auth->getInfo('user_displayname'), |
---|
290 | 'url' => $this->core->auth->getInfo('user_url'), |
---|
291 | 'blog' => $this->core->auth->getInfo('user_default_blog'), |
---|
292 | 'lang' => $this->core->auth->getInfo('user_lang'), |
---|
293 | 'tz' => $this->core->auth->getInfo('user_tz'), |
---|
294 | 'creadt' => $this->core->auth->getInfo('user_creadt'), |
---|
295 | 'cn' => $this->core->auth->getInfo('user_cn') |
---|
296 | ) : |
---|
297 | array( |
---|
298 | 'id' => '', |
---|
299 | 'admin' => '', |
---|
300 | 'name' => '', |
---|
301 | 'firstname' => '', |
---|
302 | 'displayname' => '', |
---|
303 | 'url' => '', |
---|
304 | 'blog' => '', |
---|
305 | 'lang' => 'en', |
---|
306 | 'tz' => '', |
---|
307 | 'creadt' => '', |
---|
308 | 'cn' => '', |
---|
309 | ); |
---|
310 | } |
---|
311 | |
---|
312 | protected function getMenus() |
---|
313 | { |
---|
314 | global $_menu; |
---|
315 | |
---|
316 | $this->protected_globals['menus'] = array(); |
---|
317 | |
---|
318 | if (!isset($_menu)) { |
---|
319 | return; |
---|
320 | } |
---|
321 | |
---|
322 | foreach($_menu as $m) { |
---|
323 | $this->protected_globals['menus'][] = array( |
---|
324 | 'id' => $m->getID(), |
---|
325 | 'title' => $m->getTitle(), |
---|
326 | 'separator' => $m->getSeparator(), |
---|
327 | 'items' => $m->getItems() |
---|
328 | ); |
---|
329 | } |
---|
330 | } |
---|
331 | |
---|
332 | /** |
---|
333 | Get an array of debug/dev infos |
---|
334 | */ |
---|
335 | public function getDebugInfo() |
---|
336 | { |
---|
337 | if (!DC_DEBUG) { |
---|
338 | return array(); |
---|
339 | } |
---|
340 | |
---|
341 | $di = array( |
---|
342 | 'global_vars' => implode(', ',array_keys($GLOBALS)), |
---|
343 | 'memory' => array( |
---|
344 | 'usage' => memory_get_usage(), |
---|
345 | 'size' => files::size(memory_get_usage()) |
---|
346 | ), |
---|
347 | 'xdebug' => array() |
---|
348 | ); |
---|
349 | |
---|
350 | if (function_exists('xdebug_get_profiler_filename')) { |
---|
351 | |
---|
352 | $url = http::getSelfURI(); |
---|
353 | $url .= strpos($url,'?') === false ? '?' : '&'; |
---|
354 | $url .= 'XDEBUG_PROFILE'; |
---|
355 | |
---|
356 | $di['xdebug'] = array( |
---|
357 | 'elapse_time' => xdebug_time_index(), |
---|
358 | 'profiler_file' => xdebug_get_profiler_filename(), |
---|
359 | 'profiler_url' => $url |
---|
360 | ); |
---|
361 | |
---|
362 | /* xdebug configuration: |
---|
363 | zend_extension = /.../xdebug.so |
---|
364 | xdebug.auto_trace = On |
---|
365 | xdebug.trace_format = 0 |
---|
366 | xdebug.trace_options = 1 |
---|
367 | xdebug.show_mem_delta = On |
---|
368 | xdebug.profiler_enable = 0 |
---|
369 | xdebug.profiler_enable_trigger = 1 |
---|
370 | xdebug.profiler_output_dir = /tmp |
---|
371 | xdebug.profiler_append = 0 |
---|
372 | xdebug.profiler_output_name = timestamp |
---|
373 | */ |
---|
374 | } |
---|
375 | |
---|
376 | return $di; |
---|
377 | } |
---|
378 | } |
---|
379 | ?> |
---|