Completed
Push — master ( 7e42fe...9ab714 )
by Jacob
04:00
created

Router::requires_trailing_slash()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 8.8571
cc 5
eloc 7
nc 5
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
	'ImageOld',
5
	'Request',
6
	'URLDecode'));
7
8
abstract class Router
9
{
10
11
	public function __construct() {}
12
13
	public static function instance()
14
	{
15
		$router_name = self::get_router_name();
16
		$router = Loader::loadNew('router', $router_name);
17
		$router->route();
18
	}
19
20
	private static function get_router_name()
21
	{
22
		if(Request::isAJAX())
23
			return 'AJAXRouter';
24
		if(URLDecode::getURI() == '/sitemap.xml')
25
			return 'SitemapRouter';
26
		if(URLDecode::getURI() == '/rss/')
27
			return 'RSSRouter';
28
		if(URLDecode::getExtension() == 'jpg')
29
			return 'ImageRouter';
30
		if(URLDecode::getExtension() == 'png')
31
			return 'ImageRouter';
32
		
33
		switch(URLDecode::getSite())
34
		{
35
			case 'ajax' :
36
				return 'AjaxRouter';
37
			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...
38
			case 'blog' :
39
				return 'BlogRouter';
40
			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...
41
			case 'home' :
42
				return 'HomeRouter';
43
			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...
44
			case 'images' :
45
				return 'ImageRouter';
46
			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...
47
			case 'lifestream' :
48
				return 'LifestreamRouter';
49
			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...
50
			case 'portfolio' :
51
				return 'PortfolioRouter';
52
			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...
53
			case 'site' :
54
				return 'SiteRouter';
55
			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...
56
			case 'waterfalls' :
57
				return 'WaterfallRouter';
58
			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...
59
		}
60
		
61
		Debugger::logMessage("The router for " . URLDecode::getSite() . " was not loaded.");
62
		Loader::loadNew('controller', '/Error404Controller')->activate();
63
	}
64
65
	protected function route()
66
	{
67
		$uri = URLDecode::getURI();
68
		
69
		$this->check_for_redirect($uri);
70
		
71
		$controller = $this->get_controller($uri);
72
		Loader::loadNew('controller', $controller)
73
			->activate();
74
	}
75
76
	abstract protected function get_redirect_array();
77
	abstract protected function get_direct_array();
78
79
	final protected function check_for_redirect($redirect_uri)
80
	{
81
		foreach($this->get_redirect_array() as $check)
82
		{
83
			$redirect_uri = preg_replace($check->pattern, $check->replace, $redirect_uri);
84
		}
85
		
86
		$redirect_uri = $this->check_for_special_redirect($redirect_uri);
87
		
88
		if($this->requires_trailing_slash() && substr($redirect_uri, -1) != '/')
89
			$redirect_uri .= '/';
90
		
91
        if (URLDecode::getHost() == 'waterfalls.jacobemerick.com') {
92
            $redirect_uri = 'http://' . (!Loader::isLive() ? 'dev' : 'www') . '.waterfallsofthekeweenaw.com' . $redirect_uri;
93
        }
94
        
95
		if($redirect_uri == URLDecode::getURI())
96
			return;
97
		
98
		$controller_check = $redirect_uri;
99
		if(substr($redirect_uri, 0, 4) == 'http')
100
			$controller_check = preg_replace('@^http://([a-z\.]+)@', '', $redirect_uri);
101
		
102
		$controller = $this->get_controller($controller_check);
103
		if($controller == '/Error404Controller')
104
		{
105
			Loader::loadNew('controller', '/Error404Controller')
106
				->activate();
107
			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...
108
		}
109
110
		if($this->get_primary_folder() == 'images') {
111
			$file = $controller_check;//URLDecode::getURI();
112
113 View Code Duplication
			if (
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
				URLDecode::getSite() == 'images' ||
115
				URLDecode::getExtension() == 'ico') {
116
				$file = "/css/{$file}";
117
			} else if (URLDecode::getSite() == 'portfolio') {
118
				$file = "/portfolio/{$file}";
119
			} else if (substr($file, 0, 7) == '/photo/') {
120
				$file = '/photo/processed/' . substr($file, 7);
121
			}
122
123
			$image = new ImageOld($file);
124
        	        if(!$image->isValid()) {
125
                	        Loader::loadNew('controller', '/Error404Controller')->activate();
126
				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...
127
			}
128
		}
129
		
130
		if(substr($redirect_uri, 0, 4) != 'http')
131
		{
132
			$redirect_uri = substr($redirect_uri, 1);
133
			$redirect_uri = URLDecode::getBase() . $redirect_uri;
134
		}
135
		
136
		Loader::loadNew('controller', '/Error301Controller', (array) $redirect_uri)
137
			->activate();
138
	}
139
140
	protected function check_for_special_redirect($uri)
141
	{
142
		return $uri;
143
	}
144
145
	final private function get_controller($uri)
146
	{
147
		foreach($this->get_direct_array() as $check)
148
		{
149
			if($uri == $check->match)
150
				return "{$this->get_primary_folder()}/{$check->controller}";
151
			
152
			if(preg_match("@^{$check->match}$@", $uri))
153
				return "{$this->get_primary_folder()}/{$check->controller}";
154
		}
155
		
156
		return '/Error404Controller';
157
	}
158
159
	final private function get_primary_folder()
160
	{
161
		if(Request::isAjax())
162
			return 'ajax';
163
		if(URLDecode::getURI() == '/sitemap.xml')
164
			return 'sitemap';
165
		if(URLDecode::getURI() == '/rss/')
166
			return 'rss';
167
		if(
168
			URLDecode::getExtension() == 'jpg' ||
169
			URLDecode::getExtension() == 'png')
170
			return 'images';
171
		
172
		return URLDecode::getSite();
173
	}
174
175
	private function requires_trailing_slash()
176
	{
177
		return (
178
			URLDecode::getURI() != '/sitemap.xml' &&
179
			URLDecode::getExtension() != 'json' &&
180
			URLDecode::getExtension() != 'jpg' &&
181
			URLDecode::getExtension() != 'png' &&
182
            strstr(URLDecode::getURI(), '#') === false);
183
	}
184
185
}
186