Completed
Push — master ( 18cdc2...7b9bde )
by Simon
03:22
created

zoompage.php ➔ zoomPage()   F

Complexity

Conditions 35
Paths > 20000

Size

Total Lines 270
Code Lines 155

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 35
eloc 155
nc 23809
nop 2
dl 0
loc 270
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**************************************************************************
3
 **********      English Wikipedia Account Request Interface      **********
4
 ***************************************************************************
5
 ** Wikipedia Account Request Graphic Design by Charles Melbye,           **
6
 ** which is licensed under a Creative Commons                            **
7
 ** Attribution-Noncommercial-Share Alike 3.0 United States License.      **
8
 **                                                                       **
9
 ** All other code are released under the Public Domain                   **
10
 ** by the ACC Development Team.                                          **
11
 **                                                                       **
12
 ** See CREDITS for the list of developers.                               **
13
 ***************************************************************************/
14
15
function zoomPage($id, $urlhash)
0 ignored issues
show
Coding Style introduced by
zoomPage uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
16
{
17
	global $session, $availableRequestStates, $createdid;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
18
	global $smarty, $locationProvider, $rdnsProvider, $antispoofProvider;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
19
	global $xffTrustProvider, $enableEmailConfirm;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
20
    
21
	$database = gGetDb();
22
	$request = Request::getById($id, $database);
23
	if ($request == false) {
24
		// Notifies the user and stops the script.
25
		BootstrapSkin::displayAlertBox("Could not load the requested request!", "alert-error", "Error", true, false);
26
		BootstrapSkin::displayInternalFooter();
27
		die();
0 ignored issues
show
Coding Style Compatibility introduced by
The function zoomPage() 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...
28
	}
29
    
30
	$smarty->assign('ecenable', $enableEmailConfirm);
31
32
	if (isset($_GET['ecoverride']) && User::getCurrent()->isAdmin()) {
33
		$smarty->assign('ecoverride', true);
34
	}
35
	else {
36
		$smarty->assign('ecoverride', false);
37
	}
38
        
39
	$smarty->assign('request', $request);    
40
    
41
	$smarty->assign("usernamerawunicode", html_entity_decode($request->getName()));
42
    
43
	$smarty->assign("iplocation", $locationProvider->getIpLocation($request->getTrustedIp()));
44
        
45
	$createdreason = EmailTemplate::getById($createdid, gGetDb());
46
	$smarty->assign("createdEmailTemplate", $createdreason);
47
48
	#region setup whether data is viewable or not
49
	
50
	$viewableDataStatement = $database->prepare(<<<SQL
51
        SELECT COUNT(*) 
52
        FROM request 
53
        WHERE 
54
            (
55
                email = :email 
56
                OR ip = :trustedIp 
57
                OR forwardedip LIKE :trustedProxy
58
            ) 
59
            AND reserved = :reserved 
60
            AND emailconfirm = 'Confirmed' 
61
            AND status != 'Closed';
62
SQL
63
	);
64
    
65
	$viewableDataStatement->bindValue(":email", $request->getEmail());
66
	$viewableDataStatement->bindValue(":reserved", User::getCurrent()->getId());
67
	$viewableDataStatement->bindValue(":trustedIp", $request->getTrustedIp());
68
	$viewableDataStatement->bindValue(":trustedProxy", '%' . $request->getTrustedIp() . '%');
69
    
70
	$viewableDataStatement->execute();
71
    
72
	$viewableData = $viewableDataStatement->fetchColumn();
73
	$viewableDataStatement->closeCursor();
74
    
75
	$hideinfo = ($viewableData == 0);
76
    
77
	#endregion
78
	
79
	if ($request->getStatus() == "Closed") {
80
		$hash = md5($request->getId() . $request->getEmail() . $request->getTrustedIp() . microtime()); //If the request is closed, change the hash based on microseconds similar to the checksums.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 189 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
81
		$smarty->assign("isclosed", true);
82
	}
83
	else {
84
		$hash = md5($request->getId() . $request->getEmail() . $request->getTrustedIp());
85
		$smarty->assign("isclosed", false);
86
	}
87
	$smarty->assign("hash", $hash);
88
	if ($hash == $urlhash) {
89
		$correcthash = true;
90
	}
91
	else {
92
		$correcthash = false;
93
	}
94
	
95
	$smarty->assign("showinfo", false);
96
	if ($hideinfo == false || $correcthash == true || User::getCurrent()->isAdmin() || User::getCurrent()->isCheckuser()) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
97
		$smarty->assign("showinfo", true);
98
	}
99
    
100
	// force to not show, overriden later
101
	$smarty->assign("proxyip", "");
102
103
	if ($hideinfo == false || $correcthash == true || User::getCurrent()->isAdmin() || User::getCurrent()->isCheckuser()) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
104
		$smarty->assign("proxyip", $request->getForwardedIp());
105
		if ($request->getForwardedIp()) {
106
			$smartyproxies = array(); // Initialize array to store data to be output in Smarty template.
107
			$smartyproxiesindex = 0;
108
			
109
			$proxies = explode(",", $request->getForwardedIp());
110
			$proxies[] = $request->getIp();
111
			
112
			$origin = $proxies[0];
113
			$smarty->assign("origin", $origin);
114
			
115
			$proxies = array_reverse($proxies);
116
			$trust = true;
117
			global $rfc1918ips;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
118
119
			foreach ($proxies as $proxynum => $p) {
120
				$p2 = trim($p);
121
				$smartyproxies[$smartyproxiesindex]['ip'] = $p2;
122
123
				// get data on this IP.
124
				$trusted = $xffTrustProvider->isTrusted($p2);
125
				$ipisprivate = ipInRange($rfc1918ips, $p2);
126
                
127
				if (!$ipisprivate) {
128
					$iprdns = $rdnsProvider->getRdns($p2);
129
					$iplocation = $locationProvider->getIpLocation($p2);
130
				}
131
				else {
132
					// this is going to fail, so why bother trying?
133
					$iprdns = false;
134
					$iplocation = false;
135
				}
136
                
137
				// current trust chain status BEFORE this link
138
				$pretrust = $trust;
139
				
140
				// is *this* link trusted?
141
				$smartyproxies[$smartyproxiesindex]['trustedlink'] = $trusted;
142
                
143
				// current trust chain status AFTER this link
144
				$trust = $trust & $trusted;
145
				if ($pretrust && $p2 == $origin) {
146
					$trust = true;   
147
				}
148
				$smartyproxies[$smartyproxiesindex]['trust'] = $trust;
149
				
150
				$smartyproxies[$smartyproxiesindex]['rdnsfailed'] = $iprdns === false;
151
				$smartyproxies[$smartyproxiesindex]['rdns'] = $iprdns;
152
				$smartyproxies[$smartyproxiesindex]['routable'] = !$ipisprivate;
153
				
154
				$smartyproxies[$smartyproxiesindex]['location'] = $iplocation;
155
				
156
				if ($iprdns == $p2 && $ipisprivate == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
157
					$smartyproxies[$smartyproxiesindex]['rdns'] = null;
158
				}
159
                
160
				$smartyproxies[$smartyproxiesindex]['showlinks'] = (!$trust || $p2 == $origin) && !$ipisprivate;
161
                
162
				$smartyproxiesindex++;
163
			}
164
			
165
			$smarty->assign("proxies", $smartyproxies);
166
		}
167
	}
168
169
	global $defaultRequestStateKey;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
170
	
171
	// TODO: remove me and replace with call in the template directly
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
172
	$smarty->assign("isprotected", $request->isProtected());
173
    
174
	$smarty->assign("defaultstate", $defaultRequestStateKey);
175
	$smarty->assign("requeststates", $availableRequestStates);
176
		
177
	try {
178
		$spoofs = $antispoofProvider->getSpoofs($request->getName());
179
	}
180
	catch (Exception $ex) {
181
		$spoofs = $ex->getMessage();   
182
	}
183
    
184
	$smarty->assign("spoofs", $spoofs);
185
	
186
	// START LOG DISPLAY
187
	
188
	$logs = Logger::getRequestLogsWithComments($request->getId(), $request->getDatabase());
189
	$requestLogs = array();
190
	
191
	if (trim($request->getComment()) !== "") {
192
		$requestLogs[] = array(
193
			'type' => 'comment',
194
			'security' => 'user',
195
			'userid' => null,
196
			'user' => $request->getName(),
197
			'entry' => null,
198
			'time' => $request->getDate(),
199
			'canedit' => false,
200
			'id' => $request->getId(),
201
			'comment' => $request->getComment(),
202
		);
203
	}
204
	
205
	$namecache = array();
206
	
207
	$editableComments = false;
208
	if (User::getCurrent()->isAdmin() || User::getCurrent()->isCheckuser()) {
209
		$editableComments = true;
210
	}
211
	
212
	foreach ($logs as $entry) {
213
		// both log and comment have a 'user' field
214
		if (!array_key_exists($entry->getUser(), $namecache)) {
215
			$namecache[$entry->getUser()] = $entry->getUserObject();
216
		}
217
		
218
		if ($entry instanceof Comment) {
219
			$requestLogs[] = array(
220
				'type' => 'comment',
221
				'security' => $entry->getVisibility(), 
222
				'user' => $namecache[$entry->getUser()]->getUsername(),
223
				'userid' => $entry->getUser() == -1 ? null : $entry->getUser(),
224
				'entry' => null,
225
				'time' => $entry->getTime(),
226
				'canedit' => ($editableComments || $entry->getUser() == User::getCurrent()->getId()),
227
				'id' => $entry->getId(),
228
				'comment' => $entry->getComment(),
229
			);
230
		}
231
		
232
		if ($entry instanceof Log) {
233
			$requestLogs[] = array(
234
				'type' => 'log',
235
				'security' => 'user',
236
				'userid' => $entry->getUser() == -1 ? null : $entry->getUser(),
237
				'user' => $namecache[$entry->getUser()]->getUsername(),
238
				'entry' => Logger::getLogDescription($entry),
239
				'time' => $entry->getTimestamp(),
240
				'canedit' => false,
241
				'id' => $entry->getId(),
242
				'comment' => $entry->getComment(),
243
			);
244
		}
245
	}
246
	
247
	$smarty->assign("requestLogs", $requestLogs);
248
	
249
250
	// START OTHER REQUESTS BY IP AND EMAIL STUFF
251
	
252
	// Displays other requests from this ip.
253
254
	// assign to user
255
	$userListQuery = "SELECT username FROM user WHERE status = 'User' or status = 'Admin';";
256
	$userListResult = gGetDb()->query($userListQuery);
257
	$userListData = $userListResult->fetchAll(PDO::FETCH_COLUMN);
258
	$userListProcessedData = array();
259
	foreach ($userListData as $userListItem) {
260
		$userListProcessedData[] = "\"" . htmlentities($userListItem) . "\"";
261
	}
262
    
263
	$userList = '[' . implode(",", $userListProcessedData) . ']';	
264
	$smarty->assign("jsuserlist", $userList);
265
	// end: assign to user
266
    
267
	// TODO: refactor this!
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
268
	$createreasons = EmailTemplate::getActiveTemplates(EmailTemplate::CREATED);
269
	$smarty->assign("createreasons", $createreasons);
270
	
271
	$declinereasons = EmailTemplate::getActiveTemplates(EmailTemplate::NOT_CREATED);
272
	$smarty->assign("declinereasons", $declinereasons);
273
    
274
	$allcreatereasons = EmailTemplate::getAllActiveTemplates(EmailTemplate::CREATED);
275
	$smarty->assign("allcreatereasons", $allcreatereasons);
276
	
277
	$alldeclinereasons = EmailTemplate::getAllActiveTemplates(EmailTemplate::NOT_CREATED);
278
	$smarty->assign("alldeclinereasons", $alldeclinereasons);
279
280
	$allotherreasons = EmailTemplate::getAllActiveTemplates(false);
281
	$smarty->assign("allotherreasons", $allotherreasons);
282
	
283
	return $smarty->fetch("request-zoom.tpl");
284
}
285