XOOPS  2.6.0
decorator.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  */
11 
21 {
22  protected $user;
23 
24  protected $owner;
25 
26  protected $user_groups;
27 
28  protected $user_uid;
29 
30  protected $get_uid;
31 
32  function start()
33  {
35  $member_handler = $xoops->getHandlerMember();
36 
37  if (!$xoops->isUser()) {
38  $user = $member_handler->createUser();
39  $user->setVar('uid', 0);
40  $user->setVar('uname', $GLOBALS['xoopsConfig']['anonymous']);
41  } else {
42  $user = $xoops->user;
43  }
44 
45  $ownerid = isset($_GET['uid']) ? intval($_GET['uid']) : null;
46  $owner = $member_handler->getUser($ownerid);
47  //if uid > 0 but user does not exists
48  if (!is_object($owner)) {
49  //create new user
50  $owner = $member_handler->createUser();
51  $owner->setVar('uid', 0);
52  $owner->setVar('uname', $xoops->getConfig('anonymous'));
53  }
54  $this->user = $user->getValues();
55  $this->owner = $owner->getValues();
56  $this->user_groups = $xoops->getUserGroups();
57  $this->user_uid = $xoops->isUser() ? $xoops->user->getVar('uid') : 0;
58  $this->get_uid = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
59  }
60 
61  function accessFilter(&$access_filter)
62  {
63  $access_filter['is_owner']['name'] = _PL_MENUS_MENUS_ISOWNER;
64  $access_filter['is_owner']['method'] = 'isOwner';
65  $access_filter['is_not_owner']['name'] = _PL_MENUS_MENUS_ISNOTOWNER;
66  $access_filter['is_not_owner']['method'] = 'isNotOwner';
67  }
68 
69  function decorateMenu(&$menu)
70  {
71  $decorations = array('link', 'title', 'alt_title');
72  foreach ($decorations as $decoration) {
73  if ($decoration == 'alt_title' && empty($menu['alt_title'])) {
74  $menu['alt_title'] = $menu['title'];
75  }
76  $menu[$decoration] = self::_doDecoration($menu[$decoration]);
77  if ($decoration == 'link') {
78  if (!preg_match('/mailto:/i', $menu['link']) && !preg_match('#://#i', $menu['link'])) {
79  $menu['link'] = \XoopsBaseConfig::get('url') . '/' . $menu['link']; //Do not do this in other decorators
80  }
81  }
82  }
83  }
84 
85  function end(&$menus)
86  {
87  // TODO: Implement end() method.
88  }
89 
90  function hasAccess($menu, &$hasAccess)
91  {
93  if ($menu['visible'] == 0 || !array_intersect($menu['groups'], $groups)) {
94  $hasAccess = false;
95  return;
96  }
97 
98  $hooks = array_intersect($menu['hooks'], get_class_methods(__CLASS__));
99 
100  foreach ($hooks as $method) {
101  if (!self::$method()) {
102  $hasAccess = false;
103  return;
104  }
105  }
106  }
107 
108  function _doDecoration($string)
109  {
110  if (!preg_match('/{(.*\|.*)}/i', $string, $reg)) {
111  return $string;
112  }
113 
114  $expression = $reg[0];
115  list($validator, $value) = array_map('strtolower', explode('|', $reg[1]));
116 
117  //just to prevent any bad admin to get easy passwords
118  if ($value == 'pass') {
119  return $string;
120  }
121 
122  if ($validator == 'user') {
123  $value = isset($this->user[$value]) ? $this->user[$value] : self::getExtraValue('user', $value);
124  $string = str_replace($expression, $value, $string);
125  }
126 
127  if ($validator == 'uri') {
128  $value = isset($_GET[$value]) ? $_GET[$value] : 0;
129  $string = str_replace($expression, $value, $string);
130  }
131 
132  if ($validator == 'owner') {
133  $value = isset($this->owner[$value]) ? $this->owner[$value] : self::getExtraValue('owner', $value);
134  $string = str_replace($expression, $value, $string);
135  }
136 
137  return $string;
138  }
139 
140  function isOwner()
141  {
142  return ($this->user_uid != 0 && ($this->user_uid == $this->get_uid)) ? true : false;
143  }
144 
145  function isNotOwner()
146  {
147  return !self::isOwner();
148  }
149 
150  function getExtraValue($type = 'user', $value)
151  {
153  $ret = 0;
154  $values = array('pm_new', 'pm_readed', 'pm_total');
155  if (!in_array($value, $values)) {
156  return $ret;
157  }
158 
159  $entry = $this->$type;
160  if (empty($entry)) {
161  return $ret;
162  }
163 
164  $pm_handler = $xoops->getHandlerPrivmessage();
165 
166  $criteria = new CriteriaCompo();
167  if ($value == 'pm_new') {
168  $criteria->add(new Criteria('read_msg', 0));
169  $criteria->add(new Criteria('to_userid', $entry['uid']));
170  }
171 
172  if ($value == 'pm_readed') {
173  $criteria->add(new Criteria('read_msg', 1));
174  $criteria->add(new Criteria('to_userid', $entry['uid']));
175  }
176 
177  if ($value == 'pm_total') {
178  $criteria->add(new Criteria('to_userid', $entry['uid']));
179  }
180 
181  $entry[$value] = $pm_handler->getCount($criteria);
182 
183  $this->$type = $entry;
184  unset($criteria);
185 
186  return $entry[$value];
187  }
188 }
if($uname== ''||$pass== '') $member_handler
Definition: checklogin.php:44
static getInstance()
Definition: Xoops.php:160
hasAccess($menu, &$hasAccess)
Definition: decorator.php:90
getExtraValue($type= 'user', $value)
Definition: decorator.php:150
$xoops
Definition: admin.php:25
const _PL_MENUS_MENUS_ISNOTOWNER
Definition: decorator.php:8
accessFilter(&$access_filter)
Definition: decorator.php:61
$menu
static get($name)
$type
Definition: misc.php:33
$groups
$GLOBALS['xoops']
Definition: common.php:33
$criteria
$pm_handler
Definition: readpmsg.php:37
const _PL_MENUS_MENUS_ISOWNER
Definition: decorator.php:7