3 use dns\system\cache\builder\ControllerCacheBuilder
;
6 * @author Jan Altensen (Stricted)
7 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
8 * @copyright 2013-2015 Jan Altensen (Stricted)
10 class RequestHandler
{
11 protected $pattern = "";
12 protected $routeData = array();
13 protected $controllers = array();
18 public function __construct ($module = '') {
19 $this->pattern
= '~/?(?:(?P<controller>[A-Za-z0-9\-]+)(?:/(?P<id>\d+)(?:-(?P<title>[^/]+))?)?)?~x';
20 $this->getControllers($module);
22 if (DNS
::getSession()->username
!== null) {
23 DNS
::getTPL()->assign(array("username" => DNS
::getSession()->username
));
26 DNS
::getTPL()->assign(array("username" => ''));
30 if (!empty($_SERVER['QUERY_STRING'])) {
31 $this->matches($_SERVER['QUERY_STRING']);
32 $this->registerRouteData();
35 $className = '\\dns'.(empty($module) ?
'' : '\\'.$module).'\\page\\IndexPage';
38 if (isset($this->routeData
['controller']) && !empty($this->routeData
['controller'])) {
39 $controller = strtolower($this->routeData
['controller']);
40 if (isset($this->controllers
[$controller]) && !empty($this->controllers
[$controller])) {
41 $className = $this->controllers
[$controller];
44 @header
('HTTP/1.0 404 Not Found');
45 DNS
::getTPL()->assign(array("activeMenuItem" => '', "error" => 'The link you are trying to reach is no longer available or invalid.'));
46 DNS
::getTPL()->display('error.tpl');
51 if (!User
::isLoggedIn() && $className != '\dns\page\LoginPage' && $className != '\dns\page\ApiPage') {
52 DNS
::getTPL()->display('login.tpl');
56 // handle offline mode
57 if (defined('OFFLINE') && OFFLINE
) {
58 $admin = User
::isAdmin();
61 if (defined($className . '::AVAILABLE_DURING_OFFLINE_MODE') && constant($className . '::AVAILABLE_DURING_OFFLINE_MODE')) {
65 if (!$admin && !$available) {
66 @header
('HTTP/1.1 503 Service Unavailable');
67 DNS
::getTPL()->display('offline.tpl');
75 catch (\Exception
$e) {
76 if ($e->getCode() == 404) {
77 @header
('HTTP/1.0 404 Not Found');
79 else if ($e->getCode() == 403) {
80 @header
('HTTP/1.0 403 Forbidden');
84 DNS
::getTPL()->assign(array("activeMenuItem" => '', "error" => $e->getMessage()));
85 DNS
::getTPL()->display('error.tpl');
91 * Registers route data within $_GET and $_REQUEST.
93 protected function registerRouteData() {
94 foreach ($this->routeData
as $key => $value) {
96 $_REQUEST[$key] = $value;
100 public function getControllers ($module) {
102 $this->controllers
= ControllerCacheBuilder
::getInstance()->getData(array('module' => $module));
104 $pages = glob(DNS_DIR.'/lib/'.(empty($module) ? '' : $module.'/').'page/*Page.class.php');
106 foreach ($pages as $page) {
107 $page = str_replace('Page.class.php', '', basename($page));
109 $class = "\\dns".(empty($module) ? '' : "\\".$module)."\\page\\".$page."Page";
110 if (class_exists($class) && is_subclass_of($class, '\\dns\\page\\AbstractPage')) {
111 $this->controllers[strtolower($page)] = $class;
117 public function matches($requestURL) {
118 if (preg_match($this->pattern
, $requestURL, $matches)) {
119 foreach ($matches as $key => $value) {
120 if (!is_numeric($key)) {
121 $this->routeData
[$key] = $value;