Cheetah
ChWsbPermalinks.php
Go to the documentation of this file.
1 <?php
2 
8 ch_import('ChWsbDb');
9 
33 {
34  public $sCacheFile;
35  public $aLinks;
36  protected $oDb;
37 
38  function __construct()
39  {
40  $this->oDb = ChWsbDb::getInstance();
41  $oCache = $this->oDb->getDbCacheObject();
42  $this->aLinks = $oCache->getData($this->oDb->genDbCacheKey('sys_permalinks'));
43  if (null === $this->aLinks) {
44  if (!$this->cache()) {
45  $this->aLinks = array();
46  }
47  }
48  }
49 
50  function getInstance()
51  {
52  if (!isset($GLOBALS['chWsbClasses']['ChWsbPermalinks'])) {
53  $GLOBALS['chWsbClasses']['ChWsbPermalinks'] = new ChWsbPermalinks();
54  }
55 
56  return $GLOBALS['chWsbClasses']['ChWsbPermalinks'];
57  }
58 
59  function cache()
60  {
61  $aLinks = $this->oDb->getAll("SELECT * FROM `sys_permalinks`");
62 
63  $aResult = array();
64  foreach ($aLinks as $aLink) {
65  $aResult[$aLink['standard']] = array(
66  'permalink' => $aLink['permalink'],
67  'check' => $aLink['check'],
68  'enabled' => $this->oDb->getParam($aLink['check']) == 'on'
69  );
70  }
71 
72  $oCache = $this->oDb->getDbCacheObject();
73  if ($oCache->setData($this->oDb->genDbCacheKey('sys_permalinks'), $aResult)) {
74  $this->aLinks = $aResult;
75 
76  return true;
77  }
78 
79  return false;
80  }
81 
82  function permalink($sLink)
83  {
84  if (strpos($sLink, 'modules/?r=') === false && strpos($sLink, 'modules/index.php?r=') === false) {
85  // check for exact match
86  if ($this->_isEnabled($sLink)) {
87  return $this->aLinks[$sLink]['permalink'];
88  }
89 
90  // check permalinks with numeric id or parameter at the end
91  $aMatch = array();
92  preg_match('/([\d]+)$/', $sLink, $aMatch);
93  if (!isset($aMatch[1])) {
94  preg_match('/(\{[a-zA-Z0-9_]+\})$/', $sLink, $aMatch);
95  }
96  if (!isset($aMatch[1])) {
97  return $sLink;
98  } // no id at the end and no exact match, return unmodified link
99 
100  // process links with id or parameter at the end
101  $sLink = substr($sLink, 0, -strlen($aMatch[1]));
102 
103  return $this->_isEnabled($sLink) ? $this->aLinks[$sLink]['permalink'] . $aMatch[1] : $sLink . $aMatch[1];
104  }
105 
106  // modules permalinks
107 
108  $aMatch = array();
109  preg_match('/^.*(modules\/(index.php)?\?r=[A-Za-z0-9_-]+\/).*$/', $sLink, $aMatch);
110 
111  if (!isset($aMatch[1])) {
112  return $this->_isEnabled($sLink) ? $this->aLinks[$sLink]['permalink'] : $sLink;
113  }
114 
115  $sBase = $aMatch[1];
116  if ($this->_isEnabled($sBase)) {
117  return str_replace($sBase, $this->aLinks[$sBase]['permalink'], $sLink);
118  }
119 
120  $sBaseShort = str_replace('index.php', '', $sBase);
121 
122  return $this->_isEnabled($sBaseShort) ? str_replace($sBase, $this->aLinks[$sBaseShort]['permalink'],
123  $sLink) : $sLink;
124  }
125 
126  function _isEnabled($sLink)
127  {
128  return array_key_exists($sLink, $this->aLinks) && $this->aLinks[$sLink]['enabled'];
129  }
130 
135  function redirectIfNecessary($aSkip = array())
136  {
137  $sCurrentUrl = $_SERVER['PHP_SELF'] . '?' . ch_encode_url_params($_GET, $aSkip);
138 
139  if (!preg_match('/modules\/index.php\?r=(\w+)(.*)/', $sCurrentUrl, $m)) {
140  return false;
141  }
142 
143  $sStandardLink = 'modules/?r=' . $m[1] . '/';
144  $sPermalink = $this->permalink($sStandardLink);
145 
146  if (false !== strpos($sCurrentUrl, $sPermalink)) {
147  return false;
148  }
149 
150  header("HTTP/1.1 301 Moved Permanently");
151  header('Location:' . CH_WSB_URL_ROOT . $sPermalink . rtrim(trim(urldecode($m[2]), '/'), '&'));
153 
154  return true;
155  }
156 }
header
</code > Be careful enabling this directive if you have a redirector script that does not use the< code > Location</code > HTTP header
Definition: URI.MungeResources.txt:10
ch_import
ch_import($sClassName, $aModule=array())
Definition: utils.inc.php:1218
$aResult
$aResult
Definition: index.php:19
php
send_headers_page_changed
send_headers_page_changed()
Definition: design.inc.php:99
$oCache
$oCache
Definition: prof.inc.php:10
$_GET
$_GET['debug']
Definition: index.php:67
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
ChWsbDb\getInstance
static getInstance()
Definition: ChWsbDb.php:82
$GLOBALS
$GLOBALS['iAdminPage']
Definition: advanced_settings.php:10
ch_encode_url_params
ch_encode_url_params($a, $aExcludeKeys=array(), $aOnlyKeys=false)
Definition: utils.inc.php:1675