Dotclear

source: inc/admin/class.dc.admincontext.php @ 1061:f522c5a73b4e

Revision 1061:f522c5a73b4e, 7.4 KB checked in by JcDenis, 13 years ago (diff)
  • Prepare safe_mode
Line 
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 -----------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14/**
15@ingroup DC_CORE
16@brief Template extension for admin context
17
18This extends template environment with tools required in admin context.
19*/
20class 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               
42               'version'           => DC_VERSION,
43               'vendor_name'  => DC_VENDOR_NAME,
44               
45               'safe_mode'    => isset($_SESSION['sess_safe_mode']) && $_SESSION['sess_safe_mode']
46          );
47     }
48     
49     /**
50     Prevent call crash from template on method that return this class
51     */
52     public function __toString()
53     {
54          return '';
55     }
56     
57     /**
58     Test a global variable
59     
60     @param string $name Name of the variable to test
61     @return boolean
62     */
63     public function __isset($name)
64     {
65          return isset($this->globals[$name]);
66     }
67     
68     /**
69     Add a global variable
70     
71    @param string $name Name of the variable
72    @param mixed $value Value of the variable
73     */
74     public function __set($name,$value)
75     {
76/*
77          # Overload protect
78          if ($value === null && isset($this->globals[$name])) {
79               unset($this->globals[$name]);
80          }
81          elseif (!isset($this->globals[$name])) {
82               throw new Exception('Modification of overloaded globals has no effect');
83          }
84//*/
85          $this->globals[$name] = $value;
86     }
87     
88     /**
89     Get a global variable
90     
91     @param string $name Name of the variable
92    @return mixed Value of the variable or null
93     */
94     public function __get($name)
95     {
96          return isset($this->globals[$name]) ? $this->globals[$name] : null;
97     }
98     
99    /**
100    Returns a list of filters to add to the existing list.
101   
102     @return array An array of filters
103    */
104     public function getFilters()
105     {
106          return array(
107               'trans' => new Twig_Filter_Function("__", array('is_safe' => array('html')))
108          );
109     }
110     
111    /**
112    Returns a list of functions to add to the existing list.
113   
114    @return array An array of functions
115    */
116     public function getFunctions()
117     {
118          return array(
119               '__'           => new Twig_Function_Function("__", array('is_safe' => array('html'))),
120               'page_menu' => new Twig_Function_Method($this, 'pageMenu', array('is_safe' => array('html')))
121          );
122     }
123     
124    /**
125    Returns a list of global variables to add to the existing list.
126     
127     This merges overloaded variables with defined variables.
128   
129    @return array An array of global variables
130    */
131     public function getGlobals()
132     {
133          $this->getBlogs();
134          $this->getCurrentBlog();
135          $this->getCurrentUser();
136         
137          # Additional globals
138          $p = path::info($_SERVER['REQUEST_URI']);
139          $this->protected_globals['current_page'] = $p['base'];
140          $this->protected_globals['blog_count'] = $this->core->auth->blog_count;
141         
142          # Keep protected globals safe
143          return array_merge($this->globals,$this->protected_globals);
144     }
145     
146    /**
147     * Returns the name of the extension.
148     *
149     * @return string The extension name
150     */
151     public function getName()
152     {
153          return 'AdminContext';
154     }
155     
156     public function setSafeMode($safe_mode)
157     {
158          $this->protected_globals['safe_mode'] = (boolean) $safe_mode;
159          return $this;
160     }
161     
162     /**
163     Set information message
164     
165     @param string $message A message
166     @return object self
167     */
168     public function setMessage($message)
169     {
170          $this->protected_globals['page_message'] = $message;
171          return $this;
172     }
173
174     /**
175     Add an error message
176     
177     @param string Error message
178     @return object self
179     */
180     public function addError($error)
181     {
182          $this->protected_globals['page_errors'][] = $error;
183          return $this;
184     }
185     
186     /**
187     Check if there is an error message
188     
189     @return boolean
190     */
191     public function hasError()
192     {
193          return !empty($this->protected_globals['page_errors']);
194     }
195     
196     /**
197     Add page title
198     */
199     public function setPageTitle($title)
200     {
201          $this->protected_globals['page_title'] = $title;
202     }
203     
204     /**
205     pageMenu
206     */
207     public function pageMenu()
208     {
209          $menu =& $GLOBALS['_menu'];
210          foreach ($menu as $k => $v) {
211               echo $menu[$k]->draw();
212          }
213     }
214     
215     /**
216      * Get list of blogs
217      */
218     protected function getBlogs()
219     {
220          $blog_id = '';
221         
222          # Blogs list
223          $blogs = array();
224          if ($this->core->auth->blog_count > 1 && $this->core->auth->blog_count < 20) {
225               $blog_id = $this->core->blog->id;
226               $rs_blogs = $this->core->getBlogs(array('order'=>'LOWER(blog_name)','limit'=>20));
227               while ($rs_blogs->fetch()) {
228                    $blogs[$rs_blogs->blog_id] = $rs_blogs->blog_name.' - '.$rs_blogs->blog_url;
229                    $this->protected_globals['blogs'][$rs_blogs->blog_id] = array(
230                         'id'      => $rs_blogs->blog_id,
231                         'name'    => $rs_blogs->blog_name,
232                         'desc'    => $rs_blogs->blog_desc,
233                         'url'     => $rs_blogs->blog_url,
234                         'creadt'  => $rs_blogs->blog_creadt,
235                         'upddt'   => $rs_blogs->blog_upddt
236                    );
237               }
238          }
239         
240          # Switch blog form
241          $form = new dcForm($this->core,'switchblog_menu','index.php');
242          $form
243               ->addField(
244                    new dcFieldCombo('switchblog',$blog_id,$blogs,array(
245                    'label' => __('Blogs:'))))
246               ->addField(
247                    new dcFieldSubmit('switchblog_submit',__('ok'),array(
248                    'action' => 'switchblog')))
249               ->setup();
250     }
251     
252     /**
253      * Get current blog information
254      */
255     protected function getCurrentBlog()
256     {
257          $this->protected_globals['current_blog'] = $this->core->auth->blog_count ?
258               array(
259                    'id'      => $this->core->blog->id,
260                    'name'    => $this->core->blog->name,
261                    'desc'    => $this->core->blog->desc,
262                    'url'     => $this->core->blog->url,
263                    'host'    => $this->core->blog->host,
264                    'creadt'  => $this->core->blog->creadt,
265                    'upddt'   => $this->core->blog->upddt
266               ) : array(
267                    'id'      => '',
268                    'name'    => '',
269                    'desc'    => '',
270                    'url'     => '',
271                    'host'    => '',
272                    'creadt'  => '',
273                    'upddt'   => ''
274               );
275     }
276     
277     /**
278      * Get current user information
279      */
280     protected function getCurrentUser()
281     {
282          $this->protected_globals['current_user'] = $this->core->auth->userID() ?
283               array(
284                    'id'      => $this->core->auth->userID(),
285                    'admin'   => $this->core->auth->getInfo('user_admin'),
286                    'name'    => $this->core->auth->getInfo('user_name'),
287                    'firstname'    => $this->core->auth->getInfo('user_firstname'),
288                    'displayname'  => $this->core->auth->getInfo('user_displayname'),
289                    'url'     => $this->core->auth->getInfo('user_url'),
290                    'blog'    => $this->core->auth->getInfo('user_default_blog'),
291                    'lang'    => $this->core->auth->getInfo('user_lang'),
292                    'tz'      => $this->core->auth->getInfo('user_tz'),
293                    'creadt'  => $this->core->auth->getInfo('user_creadt'),
294                    'cn'      => $this->core->auth->getInfo('user_cn')
295               ) :
296               array(
297                    'id'      => '',
298                    'admin'   => '',
299                    'name'    => '',
300                    'firstname'    => '',
301                    'displayname'  => '',
302                    'url'     => '',
303                    'blog'    => '',
304                    'lang'    => 'en',
305                    'tz'      => '',
306                    'creadt'  => '',
307                    'cn'      => '',
308               );
309     }
310}
311?>
Note: See TracBrowser for help on using the repository browser.

Sites map