* @license PHP License * @package WB * @subpackage content */ /** * Load base class */ WBClass::load('WBContent' , 'WBString'); /** * Content component: Redirect * * @version 1.3.0 * @package WB * @subpackage content */ class WBContent_Redirect extends WBContent { /** * My Parameter List * * - map: * - langmap like de => 'https://www.example.com/de/' * - session: use variable from session as redirect URL * * @var array */ protected $config = array( 'url' => '[[SELF]]', 'status' => 301, 'withpath' => 1, 'withget' => 1, 'langmap' => array(), 'map' => array(), 'session' => '' ); /** * Redirect * * Redirect to other page * * @return array parameter list * @cation this method does not return */ public function run() { $path = ''; if (!empty($this->config['__path'])) { $path = implode('/', $this->config['__path']); } $rUrl = $this->findTarget4Session(); if (empty($rUrl)) { $rUrl = $this->findTarget4Path($path); } if (empty($rUrl)) { $rUrl = $this->findLangUrl(); if (0 < intval($this->config['withpath']) && !empty($this->config['__path'])) { $rUrl .= $path; } } // detect cycle if ('[[SELF]]' . $path == $rUrl) { $rUrl = '[[SELF]]'; } $res = WBClass::create('WBResponse'); $status = $this->config['status']; $get = $this->req->export(); if (0 < intval($this->config['withget']) && !empty($get)) { $get = http_build_query($get); $rUrl .= '?' . $get; } $rUrl = WBString::replaceSuperPlaceholders($rUrl); if (1 > WBParam::get('wb/debug', 0)) { $res->addHeader('Location', $rUrl); $res->setStatus($status); } else if (9 < WBParam::get('wb/debug', 0)) { $res->add("

Debug Redirect

\n"); $res->add("
" . print_r($this->config, true) . "
\n"); } $data = str_replace('{URL}', $rUrl, 'Please continue at {URL}. Thank you.'); $res->add($data); $res->send($this->req); exit(0); } private function findLangUrl() { $url = $this->config['url']; if (!class_exists('patI18n', false)) { return $url; } $lang = patI18n::getLocale(patI18n::LOCALE_TYPE_SHORT); if (empty($this->config['langmap'][$lang])) { return $url; } return $this->config['langmap'][$lang]; } private function findTarget4Path($path) { if (empty($this->config['map'])) { return ''; } foreach ($this->config['map'] as $l) { // exact match if ($l['path'] == $path) { return $l['url']; } } return ''; } private function findTarget4Session() { if (empty($this->config['session'])) { return ''; } if (!$this->sess->has($this->config['session'])) { return ''; } $url = $this->sess->get($this->config['session']); $this->sess->clear($this->config['session']); return trim($url); } }