Completed
Push — master ( 99f0cc...43937d )
by Jacob
03:09
created

Router::requires_trailing_slash()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4286
cc 3
eloc 5
nc 3
nop 0
1
<?
0 ignored issues
show
Security Best Practice introduced by
It is not recommend to use PHP's short opening tag <?, better use <?php, or <?= in case of outputting.

Short opening tags are disabled in PHP’s default configuration. In such a case, all content of this file is output verbatim to the browser without being parsed, or executed.

As a precaution to avoid these problems better use the long opening tag <?php.

Loading history...
2
3
Loader::load('utility', array(
4
	'Request',
5
	'URLDecode'));
6
7
abstract class Router
8
{
9
10
	public function __construct() {}
11
12
	public static function instance()
13
	{
14
		$router_name = self::get_router_name();
15
		$router = Loader::loadNew('router', $router_name);
16
		$router->route();
17
	}
18
19
	private static function get_router_name()
20
	{
21
		if(Request::isAJAX())
22
			return 'AJAXRouter';
23
		if(URLDecode::getURI() == '/sitemap.xml')
24
			return 'SitemapRouter';
25
		
26
		switch(URLDecode::getSite())
27
		{
28
			case 'ajax' :
29
				return 'AjaxRouter';
30
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
31
			case 'blog' :
32
				return 'BlogRouter';
33
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
34
			case 'home' :
35
				return 'HomeRouter';
36
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
37
			case 'lifestream' :
38
				return 'LifestreamRouter';
39
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
40
			case 'portfolio' :
41
				return 'PortfolioRouter';
42
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
43
			case 'site' :
44
				return 'SiteRouter';
45
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
46
			case 'waterfalls' :
47
				return 'WaterfallRouter';
48
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
49
		}
50
		
51
		Debugger::logMessage("The router for " . URLDecode::getSite() . " was not loaded.");
52
		Loader::loadNew('controller', '/Error404Controller')->activate();
53
	}
54
55
	protected function route()
56
	{
57
		$uri = URLDecode::getURI();
58
		
59
		$this->check_for_redirect($uri);
60
		
61
		$controller = $this->get_controller($uri);
62
		Loader::loadNew('controller', $controller)
63
			->activate();
64
	}
65
66
	abstract protected function get_redirect_array();
67
	abstract protected function get_direct_array();
68
69
	final protected function check_for_redirect($redirect_uri)
70
	{
71
		foreach($this->get_redirect_array() as $check)
72
		{
73
			$redirect_uri = preg_replace($check->pattern, $check->replace, $redirect_uri);
74
		}
75
		
76
		$redirect_uri = $this->check_for_special_redirect($redirect_uri);
77
		
78
		if($this->requires_trailing_slash() && substr($redirect_uri, -1) != '/')
79
			$redirect_uri .= '/';
80
		
81
        if (URLDecode::getHost() == 'waterfalls.jacobemerick.com') {
82
            $redirect_uri = 'http://' . (!Loader::isLive() ? 'dev' : 'www') . '.waterfallsofthekeweenaw.com' . $redirect_uri;
83
        }
84
        
85
		if($redirect_uri == URLDecode::getURI())
86
			return;
87
		
88
		$controller_check = $redirect_uri;
89
		if(substr($redirect_uri, 0, 4) == 'http')
90
			$controller_check = preg_replace('@^http://([a-z\.]+)@', '', $redirect_uri);
91
		
92
		$controller = $this->get_controller($controller_check);
93
		if($controller == '/Error404Controller')
94
		{
95
			Loader::loadNew('controller', '/Error404Controller')
96
				->activate();
97
			exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method check_for_redirect() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
98
		}
99
100
		if(substr($redirect_uri, 0, 4) != 'http')
101
		{
102
			$redirect_uri = substr($redirect_uri, 1);
103
			$redirect_uri = URLDecode::getBase() . $redirect_uri;
104
		}
105
		
106
		Loader::loadNew('controller', '/Error301Controller', (array) $redirect_uri)
107
			->activate();
108
	}
109
110
	protected function check_for_special_redirect($uri)
111
	{
112
		return $uri;
113
	}
114
115
	final private function get_controller($uri)
116
	{
117
		foreach($this->get_direct_array() as $check)
118
		{
119
			if($uri == $check->match)
120
				return "{$this->get_primary_folder()}/{$check->controller}";
121
			
122
			if(preg_match("@^{$check->match}$@", $uri))
123
				return "{$this->get_primary_folder()}/{$check->controller}";
124
		}
125
		
126
		return '/Error404Controller';
127
	}
128
129
	final private function get_primary_folder()
130
	{
131
		if(Request::isAjax())
132
			return 'ajax';
133
		if(URLDecode::getURI() == '/sitemap.xml')
134
			return 'sitemap';
135
		
136
		return URLDecode::getSite();
137
	}
138
139
	private function requires_trailing_slash()
140
	{
141
		return (
142
			URLDecode::getURI() != '/sitemap.xml' &&
143
			URLDecode::getExtension() != 'json' &&
144
            strstr(URLDecode::getURI(), '#') === false);
145
	}
146
147
}
148