Dotclear

source: inc/admin/class.dc.favorites.php @ 3298:97a04b84afc9

Revision 3298:97a04b84afc9, 14.6 KB checked in by franck <carnet.franck.paul@…>, 9 years ago (diff)

Cope with trailing spaces in permissions, fixes #2194

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2013 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* dcFavorites -- Favorites handling facilities
16*
17*/
18class dcFavorites
19{
20     /** @var dcCore dotclear core instance */
21     protected $core;
22
23     /** @var array list of favorite definitions  */
24     protected $fav_defs;
25
26     /** @var dcWorkspace current favorite landing workspace */
27     protected $ws;
28
29     /** @var array list of user-defined favorite ids */
30     protected $local_prefs;
31
32     /** @var array list of globally-defined favorite ids */
33     protected $global_prefs;
34
35     /** @var array list of user preferences (either one of the 2 above, or not!) */
36     protected $user_prefs;
37
38    /**
39     * Class constructor
40     *
41     * @param mixed  $core   dotclear core
42     *
43     * @access public
44     *
45     * @return mixed Value.
46     */
47      public function __construct($core) {
48          $this->core = $core;
49          $this->fav_defs = new ArrayObject();
50          $this->ws = $core->auth->user_prefs->addWorkspace('dashboard');
51          $this->user_prefs = array();
52
53          if ($this->ws->prefExists('favorites')) {
54               $this->local_prefs = $this->ws->getLocal('favorites');
55               $this->global_prefs = $this->ws->getGlobal('favorites');
56               // Since we never know what user puts through user:preferences ...
57               if (!is_array($this->local_prefs)) {
58                    $this->local_prefs = array();
59               }
60               if (!is_array($this->global_prefs)) {
61                    $this->global_prefs = array();
62               }
63          } else {
64               // No favorite defined ? Huhu, let's go for a migration
65               $this->migrateFavorites();
66          }
67     }
68
69
70    /**
71     * setup - sets up favorites, fetch user favorites (against his permissions)
72     *              This method is to be called after loading plugins
73      *
74     * @access public
75      *
76     */
77      public function setup() {
78          defaultFavorites::initDefaultFavorites($this);
79          $this->legacyFavorites();
80          $this->core->callBehavior('adminDashboardFavorites', $this->core, $this);
81          $this->setUserPrefs();
82     }
83
84   /**
85     * getFavorite - retrieves a favorite (complete description) from its id.
86     *
87     * @param string  $id   the favorite id, or an array having 1 key 'name' set to id, ther keys are merged to favorite.
88     *
89     * @access public
90     *
91     * @return array the favorite, false if not found (or not permitted)
92     */
93     public function getFavorite($p) {
94          if (is_array($p)) {
95               $fname=$p['name'];
96               if (!isset($this->fav_defs[$fname])) {
97                    return false;
98               }
99               $fattr = $p;
100               unset($fattr['name']);
101               $fattr = array_merge($this->fav_defs[$fname],$fattr);
102          } else {
103               if (!isset($this->fav_defs[$p])) {
104                    return false;
105               }
106               $fattr = $this->fav_defs[$p];
107          }
108          $fattr = array_merge(array('id' => null,'class' => null),$fattr);
109          if (isset($fattr['permissions'])) {
110               if (is_bool($fattr['permissions']) && !$fattr['permissions'] ) {
111                    return false;
112               }
113               if (!$this->core->auth->check($fattr['permissions'],$this->core->blog->id)) {
114                    return false;
115               }
116          }
117          return $fattr;
118     }
119
120   /**
121     * getFavorites - retrieves a list of favorites.
122     *
123     * @param string  $ids   an array of ids, as defined in getFavorite.
124     *
125     * @access public
126     *
127     * @return array array of favorites, can be empty if ids are not found (or not permitted)
128     */
129     public function getFavorites($ids) {
130          $prefs = array();
131          foreach ($ids as $id) {
132               $f = $this->getFavorite($id);
133               if ($f !== false) {
134                    $prefs[$id]=$f;
135               }
136          }
137          return $prefs;
138     }
139
140   /**
141     * setUserPrefs - get user favorites from settings. These are complete favorites, not ids only
142     *                   returned favorites are the first non-empty list from :
143      *                   * user-defined favorites
144      *                   * globally-defined favorites
145      *                   * a failback list "new post" (shall never be empty)
146      *                  This method is called by ::setup()
147     * @access protected
148     *
149     */
150     protected function setUserPrefs() {
151          $this->user_prefs = $this->getFavorites($this->local_prefs);
152          if (!count($this->user_prefs)) {
153               $this->user_prefs = $this->getFavorites($this->global_prefs);
154          }
155          if (!count($this->user_prefs)) {
156               $this->user_prefs = $this->getFavorites(array('new_post'));
157          }
158          $u = explode('?',$_SERVER['REQUEST_URI']);
159          // Loop over prefs to enable active favorites
160          foreach ($this->user_prefs as $k => &$v) {
161               if (isset($v['active_cb']) && is_callable($v['active_cb'])) {
162                    // Use callback if defined to match whether favorite is active or not
163                    $v['active'] = call_user_func($v['active_cb'],$u[0],$_REQUEST);
164               } else {
165                    // Failback active detection. We test against URI name & parameters
166                    $v['active'] = true; // true until something proves it is false
167                    $u = explode('?',$v['url'],2);
168                    if (!preg_match('/'.preg_quote($u[0],"/").'/',$_SERVER['REQUEST_URI'])) {
169                         $v['active'] = false; // no URI match
170                    }
171                    if (count($u) == 2) {
172                         parse_str($u[1],$p);
173                         // test against each request parameter.
174                         foreach ($p as $k2 => $v2) {
175                              if (!isset($_REQUEST[$k2]) || $_REQUEST[$k2] !== $v2) {
176                                   $v['active'] = false;
177                              }
178                         }
179                    }
180               }
181          }
182
183     }
184
185   /**
186     * migrateFavorites - migrate dc < 2.6 favorites to new format
187      *
188     * @access protected
189     *
190     */
191     protected function migrateFavorites() {
192          $fav_ws = $this->core->auth->user_prefs->addWorkspace('favorites');
193          $this->local_prefs=array();
194          $this->global_prefs=array();
195          foreach ($fav_ws->dumpPrefs() as $k => $v) {
196               $fav = @unserialize($v['value']);
197               if (is_array($fav)) {
198                    if ($v['global']) {
199                         $this->global_prefs[] = $fav['name'];
200                    } else {
201                         $this->local_prefs[] = $fav['name'];
202                    }
203               }
204          }
205          $this->ws->put('favorites',$this->global_prefs,'array','User favorites',true,true);
206          $this->ws->put('favorites',$this->local_prefs);
207          $this->user_prefs = $this->getFavorites($this->local_prefs);
208     }
209
210
211
212   /**
213     * legacyFavorites - handle legacy favorites using adminDashboardFavs behavior
214      *
215     * @access protected
216     *
217     */
218     protected function legacyFavorites() {
219          $f = new ArrayObject();
220          $this->core->callBehavior('adminDashboardFavs', $this->core, $f);
221          foreach ($f as $k => $v) {
222               $fav = array (
223                    'title' => __($v[1]),
224                    'url' => $v[2],
225                    'small-icon' => $v[3],
226                    'large-icon' => $v[4],
227                    'permissions' => $v[5],
228                    'id' => $v[6],
229                    'class' => $v[7]
230               );
231               $this->register ($v[0], $fav);
232          }
233
234     }
235
236   /**
237     * getUserFavorites - returns favorites that correspond to current user
238      *   (may be local, global, or failback favorites)
239      *
240     * @access public
241     *
242     * @return array array of favorites (enriched)
243     */
244     public function getUserFavorites() {
245          return $this->user_prefs;
246     }
247
248
249   /**
250     * getFavoriteIDs - returns user-defined or global favorites ids list
251      *                       shall not be called outside preferences.php...
252      *
253     * @param boolean  $global   if true, retrieve global favs, user favs otherwise
254      *
255     * @access public
256     *
257     * @return array array of favorites ids (only ids, not enriched)
258     */
259     public function getFavoriteIDs ($global=false) {
260          return $global?$this->global_prefs:$this->local_prefs;
261     }
262
263   /**
264     * setFavoriteIDs - stores user-defined or global favorites ids list
265      *                       shall not be called outside preferences.php...
266      *
267     * @param array  $ids   list of fav ids
268     * @param boolean  $global   if true, retrieve global favs, user favs otherwise
269      *
270     * @access public
271     */
272     public function setFavoriteIDs($ids,$global=false) {
273          $this->ws->put('favorites',$ids,'array',null,true,$global);
274     }
275
276   /**
277     * getAvailableFavoritesIDs - returns all available fav ids
278      *
279     * @access public
280     *
281     * @return array array of favorites ids (only ids, not enriched)
282     */
283     public function getAvailableFavoritesIDs () {
284          return array_keys($this->fav_defs->getArrayCopy());
285     }
286
287   /**
288     * appendMenuTitle - adds favorites section title to sidebar menu
289      *                       shall not be called outside admin/prepend.php...
290      *
291     * @param dcMenu  $menu   admin menu instance
292      *
293     * @access public
294     */
295     public function appendMenuTitle($menu) {
296          $menu['Favorites'] = new dcMenu('favorites-menu','My favorites');
297          $menu['Favorites']->title = __('My favorites');
298     }
299
300   /**
301     * appendMenu - adds favorites items title to sidebar menu
302      *                       shall not be called outside admin/prepend.php...
303      *
304     * @param dcMenu  $menu   admin menu instance
305      *
306     * @access public
307     */
308     public function appendMenu($menu) {
309          foreach ($this->user_prefs as $k => $v) {
310               $menu['Favorites']->addItem(
311                    $v['title'],
312                    $v['url'],
313                    $v['small-icon'],
314                    $v['active'],
315                    true,
316                    $v['id'],
317                    $v['class'],
318                    true
319               );
320          }
321     }
322
323   /**
324     * appendDashboardIcons - adds favorites icons to index page
325      *                       shall not be called outside admin/index.php...
326      *
327     * @param array  $icons   dashboard icon list to enrich
328      *
329     * @access public
330     */
331     public function appendDashboardIcons($icons) {
332          foreach ($this->user_prefs as $k => $v) {
333               if (isset($v['dashboard_cb']) && is_callable($v['dashboard_cb'])) {
334                    $v = new ArrayObject($v);
335                    call_user_func($v['dashboard_cb'],$this->core,$v);
336               }
337               $icons[$k]=new ArrayObject(array($v['title'],$v['url'],$v['large-icon']));
338               $this->core->callBehavior('adminDashboardFavsIcon',$this->core,$k,$icons[$k]);
339          }
340     }
341
342   /**
343     * register - registers a new favorite definition
344      *
345     * @param string  $id   favorite id
346      * @param array  $data favorite information. Array keys are :
347      *   'title' => favorite title (localized)
348      *   'url' => favorite URL,
349      *   'small-icon' => favorite small icon (for menu)
350      *   'large-icon' => favorite large icon (for dashboard)
351      *   'permissions' => (optional) comma-separated list of permissions for thie fav, if not set : no restriction
352      *   'dashboard_cb' => (optional) callback to modify title if dynamic, if not set : title is taken as is
353      *   'active_cb' => (optional) callback to tell whether current page matches favorite or not, for complex pages
354      *
355     * @access public
356     */
357     public function register($id,$data) {
358          $this->fav_defs[$id] = $data;
359          return $this;
360     }
361
362   /**
363     * registerMultiple - registers a list of favorites definition
364      *
365     * @param array an array defining all favorites key is the id, value is the data.
366      *                  see register method for data format
367     * @access public
368     */
369      public function registerMultiple($data) {
370          foreach ($data as $k => $v) {
371               $this->register($k,$v);
372          }
373          return $this;
374     }
375
376   /**
377     * exists - tells whether a fav definition exists or not
378      *
379     * @param string $id : the fav id to test
380      *
381     * @access public
382      *
383      * @return true if the fav definition exists, false otherwise
384     */
385     public function exists($id) {
386          return isset($this->fav_defs[$id]);
387     }
388
389}
390
391
392/**
393* defaultFavorites -- default favorites definition
394*
395*/
396class defaultFavorites
397{
398     public static function initDefaultFavorites($favs) {
399          $core =& $GLOBALS['core'];
400          $favs->registerMultiple(array(
401               'prefs' => array(
402                    'title' => __('My preferences'),
403                    'url' => $core->adminurl->get("admin.user.preferences"),
404                    'small-icon' => 'images/menu/user-pref.png',
405                    'large-icon' => 'images/menu/user-pref-b.png'),
406               'new_post' => array(
407                    'title' => __('New entry'),
408                    'url' => $core->adminurl->get("admin.post"),
409                    'small-icon' => 'images/menu/edit.png',
410                    'large-icon' => 'images/menu/edit-b.png',
411                    'permissions' =>'usage,contentadmin'),
412               'posts' => array(
413                    'title' => __('Entries'),
414                    'url' => $core->adminurl->get("admin.posts"),
415                    'small-icon' => 'images/menu/entries.png',
416                    'large-icon' => 'images/menu/entries-b.png',
417                    'permissions' => 'usage,contentadmin',
418                    'dashboard_cb' => array('defaultFavorites','postsDashboard')),
419               'comments' => array(
420                    'title' => __('Comments'),
421                    'url' => $core->adminurl->get("admin.comments"),
422                    'small-icon' => 'images/menu/comments.png',
423                    'large-icon' => 'images/menu/comments-b.png',
424                    'permissions' => 'usage,contentadmin',
425                    'dashboard_cb' => array('defaultFavorites','commentsDashboard')),
426               'search' => array(
427                    'title' => __('Search'),
428                    'url' => $core->adminurl->get("admin.search"),
429                    'small-icon' => 'images/menu/search.png',
430                    'large-icon' => 'images/menu/search-b.png',
431                    'permissions' => 'usage,contentadmin'),
432               'categories' => array(
433                    'title' => __('Categories'),
434                    'url' => $core->adminurl->get("admin.categories"),
435                    'small-icon' => 'images/menu/categories.png',
436                    'large-icon' => 'images/menu/categories-b.png',
437                    'permissions' =>'categories'),
438               'media' => array(
439                    'title' => __('Media manager'),
440                    'url' => $core->adminurl->get("admin.media"),
441                    'small-icon' => 'images/menu/media.png',
442                    'large-icon' => 'images/menu/media-b.png',
443                    'permissions' => 'media,media_admin'),
444               'blog_pref' => array(
445                    'title' => __('Blog settings'),
446                    'url' => $core->adminurl->get("admin.blog.pref"),
447                    'small-icon' => 'images/menu/blog-pref.png',
448                    'large-icon' => 'images/menu/blog-pref-b.png',
449                    'permissions' => 'admin'),
450               'blog_theme' => array(
451                    'title' => __('Blog appearance'),
452                    'url' => $core->adminurl->get("admin.blog.theme"),
453                    'small-icon' => 'images/menu/themes.png',
454                    'large-icon' => 'images/menu/blog-theme-b.png',
455                    'permissions' => 'admin'),
456               'blogs' => array(
457                    'title' => __('Blogs'),
458                    'url' => $core->adminurl->get("admin.blogs"),
459                    'small-icon' => 'images/menu/blogs.png',
460                    'large-icon' => 'images/menu/blogs-b.png',
461                    'permissions' =>'usage,contentadmin'),
462               'users' => array(
463                    'title' => __('Users'),
464                    'url' => $core->adminurl->get("admin.users"),
465                    'small-icon' => 'images/menu/users.png',
466                    'large-icon' => 'images/menu/users-b.png'),
467               'plugins' => array(
468                    'title' => __('Plugins management'),
469                    'url' => $core->adminurl->get("admin.plugins"),
470                    'small-icon' => 'images/menu/plugins.png',
471                    'large-icon' => 'images/menu/plugins-b.png'),
472               'langs' => array(
473                    'title' => __('Languages'),
474                    'url' => $core->adminurl->get("admin.langs"),
475                    'small-icon' => 'images/menu/langs.png',
476                    'large-icon' => 'images/menu/langs-b.png'),
477               'help' => array(
478                    'title' => __('Global help'),
479                    'url' => $core->adminurl->get("admin.help"),
480                    'small-icon' => 'images/menu/help.png',
481                    'large-icon' => 'images/menu/help-b.png')
482          ));
483     }
484
485     public static function postsDashboard($core,$v)
486     {
487          $post_count = $core->blog->getPosts(array(),true)->f(0);
488          $str_entries = __('%d entry', '%d entries',$post_count);
489          $v['title'] = sprintf($str_entries,$post_count);
490     }
491
492     public static function commentsDashboard($core,$v)
493     {
494          $comment_count = $core->blog->getComments(array(),true)->f(0);
495          $str_comments = __('%d comment', '%d comments',$comment_count);
496          $v['title']= sprintf($str_comments,$comment_count);
497     }
498}
Note: See TracBrowser for help on using the repository browser.

Sites map