Dotclear


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • inc/admin/class.dc.admincontext.php

    r1128 r1153  
    1212if (!defined('DC_RC_PATH')) { return; } 
    1313 
     14 
     15class dcProxy { 
     16     protected $object; 
     17     protected $attributes; 
     18     protected $methods; 
     19     protected $default; 
     20     protected $denyfirst; 
     21 
     22    /** 
     23     * valuesToArray - converts a list of strings to an array having these strings as keys. 
     24     *  
     25     * @param mixed $val the list to convert. 
     26     * @access protected 
     27     * @return mixed Value The resulting array 
     28     */ 
     29     protected function valuesToArray($val) { 
     30          $arr = array(); 
     31          foreach ($val as $k) { 
     32               $arr[$k]=true; 
     33          } 
     34          return $arr; 
     35     } 
     36 
     37     protected function isAllowed ($name,$list) { 
     38          if ($this->denyfirst) { 
     39               return isset($list[$name]); 
     40          } else { 
     41               return !isset($list[$name]); 
     42          } 
     43     } 
     44 
     45     public function __construct($object,$rights,$default='',$denyfirst=true) { 
     46          $this->object = $object; 
     47          $this->attributes = array(); 
     48          $this->methods = array(); 
     49          $this->denyfirst = $denyfirst; 
     50          if (isset($rights['attr'])) { 
     51               $this->attributes = $this->valuesToArray($rights['attr']); 
     52          } 
     53          if (isset($rights['methods'])) { 
     54               $this->methods = $this->valuesToArray($rights['methods']); 
     55          } 
     56     } 
     57 
     58     public function __get($name) { 
     59          if ($this->isAllowed($name,$this->attributes)) { 
     60               return $this->object->$name; 
     61          } else { 
     62               return $this->default; 
     63          } 
     64     } 
     65 
     66     public function __call($name,$args) { 
     67          if ($this->isAllowed($name,$this->methods) && 
     68               is_callable(array($this->object,$name))) { 
     69               return call_user_func_array(array($this->object,$name),$args); 
     70          } else { 
     71               return $this->default; 
     72          } 
     73 
     74     } 
     75} 
     76 
     77class dcArrayProxy extends dcProxy implements ArrayAccess { 
     78     public function offsetExists ($offset) { 
     79          return (isset($this->value[$offset])); 
     80     } 
     81     public function offsetGet ($offset) { 
     82          return new ProxyValue($this->object[$offset],$this->rights); 
     83     } 
     84     public function offsetSet ($offset ,$value ) { 
     85          // Do nothing, we are read only 
     86     } 
     87     public function offsetUnset ($offset) { 
     88          // Do nothing, we are read only 
     89     } 
     90} 
     91 
     92 
    1493/** 
    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 dcContext 
     94@ingroup DC_CORE 
     95@brief Template extension for admin context 
     96 
     97This extends template environment with tools required in admin context. 
     98*/ 
     99class dcAdminContext extends Twig_Extension 
    21100{ 
     101     protected $core; 
     102     protected $globals = array(); 
     103     protected $protected_globals = array(); 
     104     protected $memory = array(); 
     105      
    22106     public function __construct($core) 
    23107     { 
    24           parent::__construct($core); 
    25            
    26           $this->protected_globals = array_merge($this->protected_globals,array( 
     108          $this->core = $core; 
     109           
     110          # Globals editable via context 
     111          $this->globals = array(); 
     112           
     113          # Globals not editable via context 
     114          $this->protected_globals = array( 
     115               'messages' => array( 
     116                    'static' => array(), 
     117                    'lists' => array(), 
     118                    'alert' => '', 
     119                    'errors' => array() 
     120               ), 
     121                
    27122               'page_title'   => array(), 
    28123               'page_global'  => false, 
    29124                
    30125               'admin_url'    => DC_ADMIN_URL, 
    31                'theme_url'    => DC_ADMIN_URL.'index.php?tf=', 
     126               'theme_url'    => '', 
    32127               'plugin_url'   => DC_ADMIN_URL.'index.php?pf=', 
    33           )); 
    34      } 
    35       
    36      /** 
    37       * Returns a list of global variables to add to the existing list. 
    38       * 
    39       * This merges overloaded variables with defined variables. 
    40       *  
    41       * @return array An array of global variables 
    42       */ 
     128                
     129               'version'           => DC_VERSION, 
     130               'vendor_name'  => DC_VENDOR_NAME, 
     131                
     132               'safe_mode'    => isset($_SESSION['sess_safe_mode']) && $_SESSION['sess_safe_mode'], 
     133               'debug_mode'   => DC_DEBUG 
     134          ); 
     135     } 
     136      
     137     /** 
     138     Prevent call crash from template on method that return this class 
     139     */ 
     140     public function __toString() 
     141     { 
     142          return ''; 
     143     } 
     144      
     145     /** 
     146     Test a global variable 
     147      
     148     @param string $name Name of the variable to test 
     149     @return boolean 
     150     */ 
     151     public function __isset($name) 
     152     { 
     153          return isset($this->globals[$name]); 
     154     } 
     155      
     156     /** 
     157     Add a global variable 
     158      
     159     @param string $name Name of the variable 
     160     @param mixed $value Value of the variable 
     161     */ 
     162     public function __set($name,$value) 
     163     { 
     164/* 
     165          # Overload protect 
     166          if ($value === null && isset($this->globals[$name])) { 
     167               unset($this->globals[$name]); 
     168          } 
     169          elseif (!isset($this->globals[$name])) { 
     170               throw new Exception('Modification of overloaded globals has no effect'); 
     171          } 
     172//*/ 
     173          $this->globals[$name] = $value; 
     174     } 
     175      
     176     /** 
     177     Get a global variable 
     178      
     179     @param string $name Name of the variable 
     180     @return mixed Value of the variable or null 
     181     */ 
     182     public function __get($name) 
     183     { 
     184          return isset($this->globals[$name]) ? $this->globals[$name] : null; 
     185     } 
     186      
     187     /** 
     188     Returns a list of filters to add to the existing list. 
     189      
     190     @return array An array of filters 
     191     */ 
     192     public function getFilters() 
     193     { 
     194          return array( 
     195               'trans' => new Twig_Filter_Function("__", array('is_safe' => array('html'))) 
     196          ); 
     197     } 
     198      
     199     /** 
     200     Returns a list of functions to add to the existing list. 
     201      
     202     @return array An array of functions 
     203     */ 
     204     public function getFunctions() 
     205     { 
     206          return array( 
     207               '__'           => new Twig_Function_Function("__", array('is_safe' => array('html'))), 
     208               'debug_info' => new Twig_Function_Method($this, 'getDebugInfo', array('is_safe' => array('html'))), 
     209               'memorize' => new Twig_Function_Method($this, 'setMemory', array('is_safe' => array('html'))), 
     210               'memorized' => new Twig_Function_Method($this, 'getMemory', array('is_safe' => array('html'))) 
     211          ); 
     212     } 
     213      
     214     /** 
     215     Returns a list of global variables to add to the existing list. 
     216      
     217     This merges overloaded variables with defined variables. 
     218      
     219     @return array An array of global variables 
     220     */ 
    43221     public function getGlobals() 
    44222     { 
     
    65243      
    66244     /** 
    67       * Fill the page title. 
    68       * 
    69       * $title can be:  
    70       * - a string for page title part or  
    71       * - TRUE to add blog name at the begining of title or 
    72       * - NULL to empty/reset title 
    73       * 
    74       * @param mixed $title A title part 
    75       * @param boolean $url Link of the title part 
    76       * @return object self 
    77       */ 
     245     Returns the name of the extension. 
     246      
     247     @return string The extension name 
     248     */ 
     249     public function getName() 
     250     { 
     251          return 'AdminContext'; 
     252     } 
     253      
     254      
     255     /** 
     256     Add an informational message 
     257      
     258     @param string $message A message 
     259     @return object self 
     260     */ 
     261     public function setSafeMode($safe_mode) 
     262     { 
     263          $this->protected_globals['safe_mode'] = (boolean) $safe_mode; 
     264          return $this; 
     265     } 
     266      
     267     /** 
     268     Add an informational message 
     269      
     270     @param string $message A message 
     271     @return object self 
     272     */ 
     273     public function addMessageStatic($message) 
     274     { 
     275          $this->protected_globals['messages']['static'][] = $message; 
     276          return $this; 
     277     } 
     278      
     279     /** 
     280     Add a list of informational messages 
     281      
     282     @param string $message A title 
     283     @param array $message A list of messages 
     284     @return object self 
     285     */ 
     286     public function addMessagesList($title,$messages) 
     287     { 
     288          $this->protected_globals['messages']['lists'][$title] = $messages; 
     289          return $this; 
     290     } 
     291      
     292     /** 
     293     Set an important message 
     294      
     295     @param string $message A message 
     296     @return object self 
     297     */ 
     298     public function setAlert($message) 
     299     { 
     300          $this->protected_globals['messages']['alert'] = $message; 
     301          return $this; 
     302     } 
     303      
     304     /** 
     305     Add an error message 
     306      
     307     @param string Error message 
     308     @return object self 
     309     */ 
     310     public function addError($error) 
     311     { 
     312          $this->protected_globals['messages']['errors'][] = $error; 
     313          return $this; 
     314     } 
     315      
     316     /** 
     317     Check if there is an error message 
     318      
     319     @return boolean 
     320     */ 
     321     public function hasError() 
     322     { 
     323          return !empty($this->protected_globals['messages']['errors']); 
     324     } 
     325      
     326     /** 
     327     Fill the page title 
     328      
     329     $title can be:  
     330     a string for page title part or  
     331     TRUE to add blog name at the begining of title or 
     332     NULL to empty/reset title 
     333      
     334     @param mixed $title A title part 
     335     @param boolean $url Link of the title part 
     336     @return object self 
     337     */ 
    78338     public function fillPageTitle($title,$url='') 
    79339     { 
     
    95355      
    96356     /** 
    97       * Check if a page title is set 
    98       */ 
     357     Check if a page title is set 
     358     */ 
    99359     public function hasPageTitle() 
    100360     { 
     
    103363      
    104364     /** 
    105       * Get list of blogs 
    106       */ 
     365     Get list of blogs 
     366     */ 
    107367     protected function getBlogs() 
    108368     { 
     
    116376               while ($rs_blogs->fetch()) { 
    117377                    $blogs[$rs_blogs->blog_id] = $rs_blogs->blog_name.' - '.$rs_blogs->blog_url; 
    118                     $this->protected_globals['blogs'][$rs_blogs->blog_id] = array( 
    119                          'id'      => $rs_blogs->blog_id, 
    120                          'name'    => $rs_blogs->blog_name, 
    121                          'desc'    => $rs_blogs->blog_desc, 
    122                          'url'     => $rs_blogs->blog_url, 
    123                          'creadt'  => $rs_blogs->blog_creadt, 
    124                          'upddt'   => $rs_blogs->blog_upddt 
    125                     ); 
     378                    $this->protected_globals['blogs'][$rs_blogs->blog_id] =  
     379                    new dcArrayProxy($rs_blogs, array( 
     380                         'blog_id','blog_name','blog_desc','blog_url','blog_creadt','blog_upddt')); 
    126381               } 
    127382          } 
     
    140395      
    141396     /** 
    142       * Get current blog information 
    143       */ 
     397     Get current blog information 
     398     */ 
    144399     protected function getCurrentBlog() 
    145400     { 
    146401          $this->protected_globals['current_blog'] = $this->core->auth->blog_count ? 
    147                array( 
    148                     'id'      => $this->core->blog->id, 
    149                     'name'    => $this->core->blog->name, 
    150                     'desc'    => $this->core->blog->desc, 
    151                     'url'     => $this->core->blog->url, 
    152                     'host'    => $this->core->blog->host, 
    153                     'creadt'  => $this->core->blog->creadt, 
    154                     'upddt'   => $this->core->blog->upddt 
    155                ) : array( 
     402               new dcProxy($this->core->blog,array( 
     403                    'id','name','desc','url','host','creadt','upddt' 
     404               )) : array( 
    156405                    'id'      => '', 
    157406                    'name'    => '', 
     
    165414      
    166415     /** 
    167       * Get current user information 
    168       */ 
     416     Get current user information 
     417     */ 
    169418     protected function getCurrentUser() 
    170419     { 
     
    214463      
    215464     /** 
    216       * Get sidebar menus 
    217       */ 
     465     Get sidebar menus 
     466     */ 
    218467     protected function getMenus() 
    219468     { 
     
    235484          } 
    236485     } 
     486      
     487     /** 
     488     Get an array of debug/dev infos 
     489     */ 
     490     public function getDebugInfo() 
     491     { 
     492          if (!DC_DEBUG) { 
     493               return array(); 
     494          } 
     495           
     496          $di = array( 
     497               'global_vars' => implode(', ',array_keys($GLOBALS)), 
     498               'memory' => array( 
     499                    'usage' => memory_get_usage(), 
     500                    'size' => files::size(memory_get_usage()) 
     501               ), 
     502               'xdebug' => array() 
     503          ); 
     504           
     505          if (function_exists('xdebug_get_profiler_filename')) { 
     506           
     507               $url = http::getSelfURI(); 
     508               $url .= strpos($url,'?') === false ? '?' : '&'; 
     509               $url .= 'XDEBUG_PROFILE'; 
     510                
     511               $di['xdebug'] = array( 
     512                    'elapse_time' => xdebug_time_index(), 
     513                    'profiler_file' => xdebug_get_profiler_filename(), 
     514                    'profiler_url' =>  $url 
     515               ); 
     516                
     517               /* xdebug configuration: 
     518               zend_extension = /.../xdebug.so 
     519               xdebug.auto_trace = On 
     520               xdebug.trace_format = 0 
     521               xdebug.trace_options = 1 
     522               xdebug.show_mem_delta = On 
     523               xdebug.profiler_enable = 0 
     524               xdebug.profiler_enable_trigger = 1 
     525               xdebug.profiler_output_dir = /tmp 
     526               xdebug.profiler_append = 0 
     527               xdebug.profiler_output_name = timestamp 
     528               */ 
     529          } 
     530           
     531          return $di; 
     532     } 
     533      
     534     /** 
     535     Add a value in a namespace memory 
     536      
     537     This help keep variable when recalling Twig macros 
     538      
     539     @param string $ns A namespace 
     540     @param string $str A value to memorize in this namespace 
     541     */ 
     542     public function setMemory($ns,$str) 
     543     { 
     544          if (!array_key_exists($ns,$this->memory) || !in_array($str,$this->memory[$ns])) { 
     545               $this->memory[$ns][] = $str; 
     546          } 
     547     } 
     548      
     549     /** 
     550     Check if a value is previously memorized in a namespace 
     551      
     552     @param string $ns A namespace 
     553     @param string $str A value to search in this namespace 
     554     @return array True if exists 
     555     */ 
     556     public function getMemory($ns,$str) 
     557     { 
     558          return array_key_exists($ns,$this->memory) && in_array($str,$this->memory[$ns]); 
     559     } 
    237560} 
    238561?> 
Note: See TracChangeset for help on using the changeset viewer.

Sites map