Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
page_redirect.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/page/page.inc';
19 
31 class Page_Redirect extends Page
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  parent::__construct($assetid);
44 
45  }//end constructor
46 
47 
54  function lockTypes()
55  {
56  $lock_types = parent::lockTypes();
57  $lock_types['redirect'] = ($lock_types['attributes'] | $lock_types['links']);
58  return $lock_types;
59 
60  }//end lockTypes()
61 
62 
70  function _getAllowedLinks()
71  {
72  $page_links = parent::_getAllowedLinks();
73  $page_links[SQ_LINK_NOTICE]['asset'] = Array('card' => 'M', 'exclusive' => FALSE);
74  return $page_links;
75 
76  }//end _getAllowedLinks()
77 
78 
85  function &getRedirectAsset()
86  {
87  $res = NULL;
88  if ($this->useSystemVersion()) {
89  $notice_links = unserialize(file_to_string($this->data_path.'/.sq_notice_links'));
90  foreach ($notice_links as $link) {
91  if ($link['value'] == 'redirect_asset') {
92  $redirect_link = $link;
93  break;
94  }
95  }
96  } else {
97  if (!empty($this->id)) {
98  $link = $GLOBALS['SQ_SYSTEM']->am->getLink($this->id, SQ_LINK_NOTICE, '', TRUE, 'redirect_asset');
99  }
100  }
101  if (!empty($link)) {
102  $res = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
103  }
104 
105  return $res;
106 
107  }//end getRedirectAsset()
108 
109 
118  function setRedirectAsset(&$asset)
119  {
120  $old_link = $GLOBALS['SQ_SYSTEM']->am->getLink($this->id, SQ_LINK_NOTICE, '', TRUE, 'redirect_asset');
121  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
122  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
123  if (empty($old_link)) {
124  // if there is not existing asset, and we aren't setting on, we are done here
125  if (is_null($asset)) {
126  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
127  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
128  return TRUE;
129  }
130 
131  // else if there is a link, and we are about to set an asset, but it is the same as we already have
132  } else if (!is_null($asset) && $old_link['minorid'] == $asset->id) {
133  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
134  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
135  return TRUE;
136 
137  // else something different, delete the old link
138  } else {
139  if (!$this->deleteLink($old_link['linkid'])) {
140  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
141  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
142  return FALSE;
143  }
144  }
145 
146  if (!is_null($asset)) {
147  if (!$this->createLink($asset, SQ_LINK_NOTICE, 'redirect_asset')) {
148  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
149  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
150  return FALSE;
151  }
152  }
153 
154  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
155  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
156 
157  return TRUE;
158 
159  }//end setRedirectAsset()
160 
161 
170  function printFrontend()
171  {
172  $GLOBALS['SQ_SYSTEM']->setGlobalDefine('SQ_REPLACE_MYSOURCE_LEVEL_KEYWORDS', FALSE);
173 
174  // check that the user has read access to the site
175  if (!$this->readAccess()) {
176  $GLOBALS['SQ_SYSTEM']->paintLogin('Login', translate('cannot_access_asset', $this->name));
177  return;
178  }//end if
179 
180  if (SQ_IN_LIMBO) {
181  // if we are in LIMBO then do not redirect
182  $this->printFrontendAsset($this);
183  return;
184  }//end if
185 
186  $url = $this->_getRedirectURL();
187  if (empty($url)) return;
188 
189  // Set the Session variables
190  $pmap = $this->getAttribute('session_variables');
191  $pmap_value = $pmap->getParameters();
192  $return_value = '';
193  foreach ($pmap_value as $index => $value) {
194  if (isset($_SESSION[SQ_SESSION_SANDBOX_INDEX][$value])) {
195  unset($_SESSION[SQ_SESSION_SANDBOX_INDEX][$value]);
196  }//end if
197 
198  // Get the value
199  $return_value = $pmap->getParameterValue($value);
200 
201  // Replace global keywords if required
202  replace_global_keywords($return_value);
203 
204  // Set the SESSION
205  $_SESSION[SQ_SESSION_SANDBOX_INDEX][$value] = $return_value;
206  }//end foreach
207 
208  //if redirect permanently, change location now
209  if($this->attr('permanent_redirect') && !headers_sent()) {
210  header('HTTP/1.1 301 Moved Permanently');
211  header('Location: '.$url);
212  return;
213  }
214 
215  $timeout = $this->attr('timeout');
216  if (($timeout > 0) || headers_sent()) {
217  if (!isset($this->_tmp['parent_already_called'])) {
218  $this->_tmp['parent_already_called'] = TRUE;
219  parent::printFrontend();
220  }
221  flush();
223  }
224 
225  if ($this->attr('new_window')) {
226  $options = $this->attr('new_window_options');
227  $params = 'location='.((strpos($options, 'hide_location' ) !== FALSE) ? '0' : '1').','
228  .'menubar='.((strpos($options, 'hide_menubar' ) !== FALSE) ? '0' : '1').','
229  .'scrollbars='.((strpos($options, 'hide_scrollbars') !== FALSE) ? '0' : '1').','
230  .'statusbar='.((strpos($options, 'hide_status' ) !== FALSE) ? '0' : '1').','
231  .'toolbar='.((strpos($options, 'hide_toolbar' ) !== FALSE) ? '0' : '1').','
232  .'resizable='.((strpos($options, 'not_resizable' ) !== FALSE) ? '0' : '1');
233  if ($w = $this->attr('new_window_width')) {
234  $params .= ',width='.$w;
235  }
236  if ($h = $this->attr('new_window_height')) {
237  $params .= ',height='.$h;
238  }
239 
240  // if the headers have been sent then other stuff
241  $wrap_script = !headers_sent();
242  if ($wrap_script) {
243  ?><html><body><?php
244  }
245  ?>
246  <script language="JavaScript" type="text/javascript">
247  var page = window.open('<?php echo addslashes($url); ?>','<?php echo time(); ?>','<?php echo addslashes($params);?>');
248  history.go(-1);
249  </script>
250  <?php
251  if ($wrap_script) ?></body></html><?php
252 
253  // not in a new window, just relocate them
254  } else {
255  if (!headers_sent()) header('Location: '.$url);
256  }
257 
258  }//end printFrontend()
259 
260 
268  {
269  $keywords = parent::getAvailableKeywords();
270 
271  $asset = new Asset();
272 
273  foreach ($asset->getAvailableKeywords() as $keyword => $desc) {
274  $keywords['referring_'.$keyword] = 'Referring Asset: '.$desc;
275  }
276 
277  $keywords['javascript_redirect'] = 'JavaScript Redirect Code';
278  $keywords['redirect_url'] = 'Redirect URL';
279  $keywords['redirect_assetid'] = 'Redirect Asset Id';
280 
281  return $keywords;
282 
283  }//end getAvailableKeywords()
284 
285 
300  function getKeywordReplacement($keyword)
301  {
302  $replacement = NULL;
303  $original_keyword = $keyword;
304  $keyword = parse_keyword($keyword, $modifiers);
305 
306  if (preg_match('|^referring\_(.*)|', $keyword, $matches)) {
307  $referring_asset = $this->getReferringAsset();
308  $replacement = empty($referring_asset) ? '' : $referring_asset->getKeywordReplacement($matches[1]);
309  } else if ($keyword === 'redirect_url') {
310  $replacement = $this->_getRedirectURL();
311  } else if ($keyword === 'redirect_assetid') {
312  $redirect_asset = $this->getRedirectAsset();
313  $replacement = empty($redirect_asset) ? '' : $redirect_asset->id;
314  } else {
315  return parent::getKeywordReplacement($original_keyword);
316  }
317 
318  if (!is_null($replacement)) {
319  apply_keyword_modifiers($replacement, $modifiers, Array('assetid' => $this->id));
320  }
321 
322  return $replacement;
323 
324  }//end getKeywordReplacement()
325 
326 
333  function &getReferringAsset()
334  {
335  $url = array_get_index($_SERVER, 'HTTP_REFERER', FALSE);
336 
337  // strip protocol, strip query string
338  $url = preg_replace('|^.*://|', '', $url);
339  $url = preg_replace('|\?.*$|', '', $url);
340 
341  $asset = $GLOBALS['SQ_SYSTEM']->am->getAssetFromURL(NULL, $url, FALSE, TRUE);
342 
343  return $asset;
344 
345  }//end getReferringAsset()
346 
347 
354  function printBody()
355  {
356  echo $this->printFrontend();
357 
358  }//end printBody()
359 
360 
367  function _getRedirectURL()
368  {
369  // See if we are given a query string value to look at
370  $qrystr_param = $this->attr('use_querystring_parameter');
371  $url = trim($this->attr('redirect_url'));
372  if ($qrystr_param && isset($_GET[$qrystr_param]) && trim($_GET[$qrystr_param]) != '') {
373  $url = trim($_GET[$qrystr_param]);
374 
375  // See if there is a URL entered manually in the redirect_url attribute field
376  } else if ($url != '') {
377  // Replace global keywords
378  replace_global_keywords($url);
379 
380  // if there is no url, see if we have an asset selected
381  } else {
382  $redirect_asset = $this->getRedirectAsset();
383  if (!is_null($redirect_asset)) {
384  if (($url = $redirect_asset->getURL()) == '') {
385  $url = current_url().'?a='.$redirect_asset->id;
386  }
387  }
388  }//end if
389 
390  if ($url == '') {
391  trigger_localised_error('CMS0037', E_USER_WARNING, $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($this->type(), 'name'), $this->name, $this->id);
392  return '';
393  }
394 
395  $url_suffix = trim($this->attr('url_suffix'));
396 
397  // Check if URL suffix attribute has a value
398  if ($url_suffix != '') {
399 
400  // Replace globals keywords on URL suffix
401  replace_global_keywords($url_suffix);
402 
403  // Append the suffix to the URL
404  $url .= $url_suffix;
405  }
406 
407  // See if we need to append the query string and there is a query string
408  if ($this->attr('append_querystring') && !empty($_SERVER['QUERY_STRING'])) {
409  $url .= ((strpos($url, '?') === FALSE) ? '?' : '&').$_SERVER['QUERY_STRING'];
410  }
411 
412  // Strip newlines (and other dodgy characters) from the $url that may have
413  // been inadvertently added by keywords or modifiers. They're not allowed in
414  // the 'Location' header and will cause an error.
415  $stripchars = array();
416  for ($i = 0; $i < 32; $stripchars[] = chr($i), $i++);
417  $url = str_replace($stripchars, '', $url);
418 
419  return $url;
420 
421  }//end _getRedirectURL()
422 
423 
431  {
432  static $fn_called = FALSE;
433 
434  if ($fn_called) return '';
435  $fn_called = TRUE;
436 
437  $timeout = $this->attr('timeout');
438  $url = $this->_getRedirectURL();
439  if (empty($url)) return;
440 
441  ob_start();
442  if (headers_sent()) {
443  ?>
444  <script language="JavaScript" type="text/javascript">
445  setTimeout("self.location = '<?php echo addslashes($url); ?>'", <?php echo $timeout * 1000 ?>);
446  </script>
447  <?php
448  }
449  return ob_get_clean();
450 
451  }//end getJavascriptRedirectKeywordReplacement()
452 
453 
454 }//end class
455 
456 ?>