Cheetah
ChWsbMenu.php
Go to the documentation of this file.
1 <?php
2 
8 require_once(CH_DIRECTORY_PATH_INC . 'profiles.inc.php');
9 ch_import('ChWsbPermalinks');
10 
11 define('CH_PROFILE_MENU_ID', 9);
12 
13 class ChWsbMenu
14 {
15  var $aTopMenu;
16  var $aMenuInfo = array();
18  var $sCode = '';
19  //var $iDivide; //divider or top items
22  var $aNotShowSubsFor = array( );
24  var $aCustomBreadcrumbs = array ();
25 
26  function __construct()
27  {
29 
30  $this->oPermalinks = new ChWsbPermalinks();
31 
32  $this->oTemplConfig = &$oTemplConfig;
33 
34  //$this->iDivide = (int)getParam( 'topmenu_items_perline' );
35 
36  if( !$this->load() )
37  $this->aTopMenu = array();
38  }
39 
40  function load()
41  {
42  $oCache = $GLOBALS['MySQL']->getDbCacheObject();
43  $this->aTopMenu = $oCache->getData($GLOBALS['MySQL']->genDbCacheKey('sys_menu_top'));
44 
45  if (null === $this->aTopMenu) {
46 
47  if (!$this->compile())
48  return false;
49 
50  $this->aTopMenu = $oCache->getData($GLOBALS['MySQL']->genDbCacheKey('sys_menu_top'));
51  }
52 
53  if( !$this->aTopMenu or !is_array( $this->aTopMenu ) ) {
54  echo '<b>Warning!</b> Cannot evaluate Menu Cache.';
55  return false;
56  }
57 
58  if (ChWsbRequest::serviceExists('pageac', 'menu_items_filter')) {
59  ChWsbService::call('pageac', 'menu_items_filter', array('top', &$this->aTopMenu));
60  }
61 
62  return true;
63  }
64 
65  function setCurrentProfileID($iProfileID = 0)
66  {
67  $iProfileID = (int)$iProfileID;
68  $sProfileUserName = getUsername($iProfileID);
69  $sProfileNickName = getNickName($iProfileID);
70 
71  if($iProfileID > 0 && !empty($sProfileUserName) && !empty($sProfileNickName)) {
72  $this->aMenuInfo['profileID'] = $iProfileID;
73  $this->aMenuInfo['profileUsername'] = $sProfileUserName;
74  $this->aMenuInfo['profileNick'] = $sProfileNickName;
75  $this->aMenuInfo['profileLink'] = getProfileLink($iProfileID);
76  } else {
77  $this->aMenuInfo['profileID'] = 0;
78  $this->aMenuInfo['profileUsername'] = '';
79  $this->aMenuInfo['profileNick'] = '';
80  $this->aMenuInfo['profileLink'] = '';
81  }
82  }
83 
84  function setCustomVar($sVar, $sVal)
85  {
86  $this->aMenuInfo[$sVar] = $sVal;
87  }
88 
89  function unsetCustomVar($sVar, $sVal)
90  {
91  $this->aMenuInfo[$sVar] = $sVal;
92  }
93 
94  function setCurrentProfileNickName($sNickName = '')
95  {
96  $sNickName = trim($sNickName);
97  $iProfileID = getID($sNickName);
98  $this->setCurrentProfileID($iProfileID);
99  }
100 
101  function getMenuInfo()
102  {
103  global $p_arr;
104 
105  $aSiteUrl = parse_url( CH_WSB_URL_ROOT );
106  $this->sRequestUriFile = substr($_SERVER['REQUEST_URI'], strlen($aSiteUrl['path']));
107  $this->sSelfFile = substr(ch_html_attribute($_SERVER['PHP_SELF']), strlen($aSiteUrl['path']));
108 
109  if (isMember()) {
110  $this->aMenuInfo['memberID'] = (int)$_COOKIE['memberID'];
111  $this->aMenuInfo['memberUsername'] = getUsername( $this->aMenuInfo['memberID'] );
112  $this->aMenuInfo['memberNick'] = getNickName( $this->aMenuInfo['memberID'] );
113  $this->aMenuInfo['memberLink'] = getProfileLink( $this->aMenuInfo['memberID'] );
114  $this->aMenuInfo['visible'] = 'memb';
115  } else {
116  $this->aMenuInfo['memberID'] = 0;
117  $this->aMenuInfo['memberUsername'] = '';
118  $this->aMenuInfo['memberNick'] = '';
119  $this->aMenuInfo['memberLink'] = '';
120  $this->aMenuInfo['visible'] = 'non';
121  }
122 
123  // if profile ID is not defined yet by script (using setCurrentProfileID)
124  if (empty($this->aMenuInfo['profileID'])) {
125  //get viewed profile ID (cherez jopu)
126 
127  $selfFile = basename( $_SERVER['PHP_SELF'] );
128 
129  if (isset($p_arr) and isset($p_arr['ID']))
130  $iProfileID = (int)$p_arr['ID'];
131 
132  // known modules
133  elseif ($selfFile == 'browseMedia.php')
134  $iProfileID = (int)$_GET['userID'];
135  elseif ($selfFile == 'viewFriends.php')
136  $iProfileID = (int)$_GET['iUser'];
137 
138  // unknown modules. f*ck me! %-[
139  elseif (isset($_REQUEST['iUser']))
140  $iProfileID = (int)$_REQUEST['iUser'];
141  elseif (isset($_REQUEST['userID']))
142  $iProfileID = (int)$_REQUEST['userID'];
143  elseif (isset($_REQUEST['profileID']))
144  $iProfileID = (int)$_REQUEST['profileID'];
145  elseif (isset($_REQUEST['ownerID']))
146  $iProfileID = (int)$_REQUEST['ownerID'];
147  // Have more variants? Please add them. Do not hesitate. It is ugly anyway. ;)
148 
149  // not found
150  else
151  $iProfileID = 0;
152 
153  $this->setCurrentProfileID($iProfileID);
154  }
155 
156  // detect current menu
157  $this->aMenuInfo['currentCustom'] = -1;
158  $this->aMenuInfo['currentTop'] = 0;
159  $this->aMenuInfo['currentTopLink'] = 0;
160 
161  $aPossibleItems = array();
162  foreach( $this->aTopMenu as $iItemID => $aItem ) {
163 
164  if( $aItem['Type'] == 'top' and $this->aMenuInfo['currentTop'] and $this->aMenuInfo['currentTop'] != $iItemID )
165  break;
166 
167  // if profile ID isn't defined, then profile menu submenus can't be currently selected
168  if((!isset($this->aMenuInfo['profileID']) || !$this->aMenuInfo['profileID']) && CH_PROFILE_MENU_ID == $aItem['Parent'] )
169  continue;
170 
171  $this->aMenuInfo['currentTopLink'] = $aItem['Link'];
172 
173  $aItemUris = explode( '|', $aItem['Link'] );
174  foreach ($aItemUris as $sItemUri) {
175 
176  if( empty($this->aMenuInfo['memberID'] )) {
177  unset($this->aMenuInfo['memberID']);
178  unset($this->aMenuInfo['memberUsername']);
179  unset($this->aMenuInfo['memberNick']);
180  unset($this->aMenuInfo['memberLink']);
181  }
182 
183  if( empty($this->aMenuInfo['profileID']) ) {
184  unset ($this->aMenuInfo['profileID']);
185  unset ($this->aMenuInfo['profileUsername']);
186  unset ($this->aMenuInfo['profileNick']);
187  unset ($this->aMenuInfo['profileLink']);
188  }
189 
190  foreach ($this->aMenuInfo as $k => $v)
191  $sItemUri = str_replace('{'.$k.'}', $v, $sItemUri);
192 
193  $sItemUriPermalink = $this->oPermalinks->permalink($sItemUri);
194 
195  if (0 === strcasecmp($sItemUri, $this->sRequestUriFile) || 0 === strcasecmp($sItemUriPermalink, $this->sRequestUriFile) || 0 === strncasecmp(rawurldecode($this->sRequestUriFile), $sItemUri, strlen($sItemUri)) || 0 === strncasecmp($this->sRequestUriFile, $sItemUriPermalink, strlen($sItemUriPermalink))) {
196 
197  if ((isset($aPossibleItems[$sItemUriPermalink]) && $aPossibleItems[$sItemUriPermalink]['Type'] == "custom" && $aItem['Type'] == "top")
198  ||
199  (isset($this->aTopMenu[$aItem['Parent']]) && !$this->checkToShow($this->aTopMenu[$aItem['Parent']]))
200  ||
201  !$this->checkToShow($aItem))
202  continue;
203  $aItem['ID'] = $iItemID;
204  $aPossibleItems[$sItemUriPermalink] = $aItem;
205  }
206  }
207 
208  }
209  $aPossibleItemsKeys = array_keys($aPossibleItems);
210  if (!empty($aPossibleItemsKeys)) {
211  $sMaxUri = $aPossibleItemsKeys[0];
212  for($i=1; $i<count($aPossibleItemsKeys); $i++)
213  if(strlen($aPossibleItemsKeys[$i]) > strlen($sMaxUri))
214  $sMaxUri = $aPossibleItemsKeys[$i];
215  }
216 
217  if(count($aPossibleItems) > 0) {
218  $aItem = $aPossibleItems[$sMaxUri];
219  if( $aItem['Type'] == 'custom' ) {
220  $this->aMenuInfo['currentCustom'] = $aItem['ID'];
221  $this->aMenuInfo['currentTop'] = (int)$aItem['Parent'];
222  $this->aMenuInfo['currentTopName'] = (int)$aItem['Parent'];
223  } else { //top or system
224  if( $this->aMenuInfo['currentTop'] and $this->aMenuInfo['currentTop'] != $aItem['ID'] ) {
225  } else {
226  $this->aMenuInfo['currentTop'] = $aItem['ID'];
227  }
228  }
229  }
230  // if( $this->aMenuInfo['currentCustom'] )
231  // break;
232  if(!$this->aMenuInfo['currentTop']) {
233  $this->aMenuInfo['currentCustom'] = -1;
234  $this->aMenuInfo['currentTop'] = -1;
235  $this->aMenuInfo['currentTopLink'] = -1;
236  }
237  }
238 
239  // check if to show current sub menu
240  function checkShowCurSub()
241  {
242  foreach( $this->aNotShowSubsFor as $sExcep )
243  if( $this->sSelfFile == $sExcep )
244  return false;
245  return true;
246  }
247 
248  function checkToShow( $aItem )
249  {
250  if( !$this->checkVisible( $aItem['Visible'] ) )
251  return false;
252 
253  if( !$this->checkCond( $aItem['Check'] ) )
254  return false;
255 
256  return true;
257  }
258 
259  function checkVisible( $sVisible )
260  {
261  return ( strpos( $sVisible, $this->aMenuInfo['visible'] ) !== false );
262  }
263 
264  function checkCond( $sCheck )
265  {
266  if( !$sCheck )
267  return true;
268 
269  return eval($sCheck);
270  }
271 
272  function genSubItems( $iTItemID = 0 )
273  {
274  if( !$iTItemID )
275  $iTItemID = $this->aMenuInfo['currentTop'];
276 
277  foreach( $this->aTopMenu as $iItemID => $aItem ) {
278  if( $aItem['Type'] != 'custom' )
279  continue;
280 
281  if( $aItem['Parent'] != $iTItemID )
282  continue;
283 
284  if( !$this->checkToShow( $aItem ) )
285  continue;
286 
287  //generate
288  list( $aItem['Link'] ) = explode( '|', $aItem['Link'] );
289 
290  $aItem['Link'] = $this->replaceMetas( $aItem['Link'] );
291  $aItem['Onclick'] = $this->replaceMetas( $aItem['Onclick'] );
292 
293  $bActive = ( $iItemID == $this->aMenuInfo['currentCustom'] );
294 
295  $this->genSubItem( _t( $aItem['Caption'] ), $aItem['Link'], $aItem['Target'], $aItem['Onclick'], $bActive );
296  }
297  }
298 
299  function replaceMetas( $sLink )
300  {
301  $sLink = str_replace( '{memberPass}', empty($this->aMenuInfo['memberID']) ? '' : getPassword( $this->aMenuInfo['memberID'] ), $sLink );
302 
303  foreach ($this->aMenuInfo as $k => $v)
304  $sLink = str_replace('{'.$k.'}', $v, $sLink);
305 
306  return $sLink;
307  }
308 
309  function compile()
310  {
311  $sEval = "return array(\n";
312  $aFields = array( 'Type','Caption','Link','Visible','Target','Onclick','Check','Parent','Picture','Icon','BQuickLink', 'Statistics', 'Name' );
313 
314  $sQuery = "
315  SELECT
316  `ID`,
317  `" . implode('`,
318  `', $aFields ) . "`
319  FROM `sys_menu_top`
320  WHERE
321  `Active` = 1 AND
322  ( `Type` = 'system' OR `Type` = 'top' )
323  ORDER BY `Type`,`Order`
324  ";
325 
326  $rMenu = db_res( $sQuery );
327  while( $aMenuItem = $rMenu ->fetch() ) {
328  $sEval .= " " . str_pad( $aMenuItem['ID'], 2 ) . " => array(\n";
329 
330  foreach( $aFields as $sKey => $sField ) {
331  $sCont = $aMenuItem[$sField];
332 
333  if( $sField == 'Link' )
334  $sCont = $this->getCurrLink($sCont);
335 
336  $sCont = str_replace( '\\', '\\\\', $sCont );
337  $sCont = str_replace( '"', '\\"', $sCont );
338  $sCont = str_replace( '$', '\\$', $sCont );
339 
340  $sCont = str_replace( "\n", '', $sCont );
341  $sCont = str_replace( "\r", '', $sCont );
342  $sCont = str_replace( "\t", '', $sCont );
343 
344  $sEval .= " " . str_pad( "'$sField'", 11 ) . " => \"$sCont\",\n";
345  }
346 
347  $sEval .= " ),\n";
348 
349  // write it's children
350  $sQuery = "
351  SELECT
352  `ID`,
353  `" . implode('`,
354  `', $aFields ) . "`
355  FROM `sys_menu_top`
356  WHERE
357  `Active` = 1 AND
358  `Type` = 'custom' AND
359  `Parent` = {$aMenuItem['ID']}
360  ORDER BY `Order`
361  ";
362 
363  $rCMenu = db_res( $sQuery );
364  while( $aMenuItem = $rCMenu ->fetch() ) {
365  $sEval .= " " . str_pad( $aMenuItem['ID'], 2 ) . " => array(\n";
366 
367  foreach( $aFields as $sKey => $sField ) {
368  $sCont = $aMenuItem[$sField];
369 
370  if( $sField == 'Link' )
371  $sCont = $this->getCurrLink($sCont);
372 
373  $sCont = str_replace( '\\', '\\\\', $sCont );
374  $sCont = str_replace( '"', '\\"', $sCont );
375  $sCont = str_replace( '$', '\\$', $sCont );
376 
377  $sCont = str_replace( "\n", '', $sCont );
378  $sCont = str_replace( "\r", '', $sCont );
379  $sCont = str_replace( "\t", '', $sCont );
380 
381  $sEval .= " " . str_pad( "'$sField'", 11 ) . " => \"$sCont\",\n";
382  }
383 
384  $sEval .= " ),\n";
385  }
386  }
387 
388  $sEval .= ");\n";
389  $aResult = eval($sEval);
390 
391  // view my profile and view other member's profile must be in particular order
392  $aMenuProfileView = $aResult[9];
393  $aMenuMyProfile = $aResult[4];
394  unset($aResult[9]);
395  unset($aResult[4]);
396  $aResult[9] = $aMenuProfileView;
397  $aResult[4] = $aMenuMyProfile;
398 
399  $oCache = $GLOBALS['MySQL']->getDbCacheObject();
400  return $oCache->setData ($GLOBALS['MySQL']->genDbCacheKey('sys_menu_top'), $aResult);
401  }
402 
406  function getCurrLink($sCont)
407  {
408  $aCurrLink = explode('|', $sCont);
409  $aCurrLink[0] = $this->oPermalinks->permalink($aCurrLink[0]);
410  $sCont = implode( '|', $aCurrLink );
411 
412  return $sCont;
413  }
414 
424  function setCustomBreadcrumbs ($a)
425  {
426  $this->aCustomBreadcrumbs = $a;
427  }
428 }
ChWsbMenu\genSubItems
genSubItems( $iTItemID=0)
Definition: ChWsbMenu.php:272
ChWsbRequest\serviceExists
static serviceExists($mixedModule, $sMethod, $sClass="Module")
Definition: ChWsbRequest.php:70
ChWsbMenu\$sRequestUriFile
$sRequestUriFile
Definition: ChWsbMenu.php:20
ChWsbMenu\setCurrentProfileID
setCurrentProfileID($iProfileID=0)
Definition: ChWsbMenu.php:65
ch_import
ch_import($sClassName, $aModule=array())
Definition: utils.inc.php:1218
$aResult
$aResult
Definition: index.php:19
$sCont
$sCont
Definition: get_rss_feed.php:36
ChWsbMenu\load
load()
Definition: ChWsbMenu.php:40
getPassword
getPassword( $ID='')
Definition: profiles.inc.php:475
php
ChWsbMenu\$aCustomBreadcrumbs
$aCustomBreadcrumbs
Definition: ChWsbMenu.php:24
ChWsbMenu\unsetCustomVar
unsetCustomVar($sVar, $sVal)
Definition: ChWsbMenu.php:89
ChWsbMenu\getCurrLink
getCurrLink($sCont)
Definition: ChWsbMenu.php:406
ChWsbMenu\$sCode
$sCode
Definition: ChWsbMenu.php:18
$oCache
$oCache
Definition: prof.inc.php:10
CH_PROFILE_MENU_ID
const CH_PROFILE_MENU_ID
Definition: ChWsbMenu.php:11
ch_html_attribute
ch_html_attribute($mixedInput)
Definition: utils.inc.php:1324
ChWsbMenu\$sSelfFile
$sSelfFile
Definition: ChWsbMenu.php:21
$_GET
$_GET['debug']
Definition: index.php:67
and
and
Definition: license.txt:18
$aFields
$aFields
Definition: preValues.php:19
ChWsbMenu\checkVisible
checkVisible( $sVisible)
Definition: ChWsbMenu.php:259
getNickName
getNickName( $ID='')
Definition: profiles.inc.php:461
$_REQUEST
$_REQUEST['action']
Definition: cmd.php:11
getUsername
getUsername( $ID='')
Definition: profiles.inc.php:443
ChWsbMenu\$oPermalinks
$oPermalinks
Definition: ChWsbMenu.php:23
global
if(!defined("GLOBAL_MODULE")) define("GLOBAL_MODULE" global
Definition: header.inc.php:25
_t
_t($key, $arg0="", $arg1="", $arg2="")
Definition: languages.inc.php:509
ChWsbMenu\__construct
__construct()
Definition: ChWsbMenu.php:26
getProfileLink
getProfileLink( $iID, $sLinkAdd='')
Definition: profiles.inc.php:484
ChWsbMenu\$oTemplConfig
$oTemplConfig
Definition: ChWsbMenu.php:17
ChWsbMenu\$aTopMenu
$aTopMenu
Definition: ChWsbMenu.php:15
ChWsbMenu\checkToShow
checkToShow( $aItem)
Definition: ChWsbMenu.php:248
db_res
db_res($query, $bindings=[])
Definition: db.inc.php:39
ChWsbMenu\getMenuInfo
getMenuInfo()
Definition: ChWsbMenu.php:101
ChWsbMenu\checkCond
checkCond( $sCheck)
Definition: ChWsbMenu.php:264
ChWsbMenu\setCustomVar
setCustomVar($sVar, $sVal)
Definition: ChWsbMenu.php:84
ChWsbMenu\checkShowCurSub
checkShowCurSub()
Definition: ChWsbMenu.php:240
getID
getID( $str, $with_email=1)
Definition: admin.inc.php:139
ChWsbMenu\$aNotShowSubsFor
$aNotShowSubsFor
Definition: ChWsbMenu.php:22
ChWsbMenu\compile
compile()
Definition: ChWsbMenu.php:309
$p_arr
$p_arr
Definition: profile.php:68
empty
Attr AllowedRel this is empty
Definition: Attr.AllowedRel.txt:7
ChWsbService\call
static call($mixed, $sMethod, $aParams=array(), $sClass='Module')
Definition: ChWsbService.php:32
ChWsbMenu\replaceMetas
replaceMetas( $sLink)
Definition: ChWsbMenu.php:299
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
ChWsbMenu\setCurrentProfileNickName
setCurrentProfileNickName($sNickName='')
Definition: ChWsbMenu.php:94
$GLOBALS
$GLOBALS['iAdminPage']
Definition: advanced_settings.php:10
or
Voluntary License Schemes The Licensor waives the right to collect whether individually or
Definition: license.txt:37
isMember
isMember($iId=0)
Definition: profiles.inc.php:44
ChWsbMenu
Definition: ChWsbMenu.php:14
ChWsbMenu\setCustomBreadcrumbs
setCustomBreadcrumbs($a)
Definition: ChWsbMenu.php:424
ChWsbMenu\$aMenuInfo
$aMenuInfo
Definition: ChWsbMenu.php:16