Failed Conditions
Push — newinternal ( 216d62...410e59 )
by Simon
05:28 queued 13s
created
includes/Router/ApiRequestRouter.php 1 patch
Indentation   +36 added lines, -36 removed lines patch added patch discarded remove patch
@@ -20,43 +20,43 @@
 block discarded – undo
20 20
 
21 21
 class ApiRequestRouter implements IRequestRouter
22 22
 {
23
-    /**
24
-     * @return string[]
25
-     */
26
-    public static function getActionList()
27
-    {
28
-        return array("count", "status", "stats", "help", "monitor");
29
-    }
23
+	/**
24
+	 * @return string[]
25
+	 */
26
+	public static function getActionList()
27
+	{
28
+		return array("count", "status", "stats", "help", "monitor");
29
+	}
30 30
 
31
-    /**
32
-     * @return IRoutedTask
33
-     * @throws Exception
34
-     */
35
-    public function route()
36
-    {
37
-        $requestAction = WebRequest::getString('action');
31
+	/**
32
+	 * @return IRoutedTask
33
+	 * @throws Exception
34
+	 */
35
+	public function route()
36
+	{
37
+		$requestAction = WebRequest::getString('action');
38 38
 
39
-        switch ($requestAction) {
40
-            case "count":
41
-                $result = new CountAction();
42
-                break;
43
-            case "status":
44
-                $result = new StatusAction();
45
-                break;
46
-            case "stats":
47
-                $result = new StatsAction();
48
-                break;
49
-            case "help":
50
-                $result = new HelpAction();
51
-                break;
52
-            case "monitor":
53
-                $result = new MonitorAction();
54
-                break;
55
-            default:
56
-                $result = new UnknownAction();
57
-                break;
58
-        }
39
+		switch ($requestAction) {
40
+			case "count":
41
+				$result = new CountAction();
42
+				break;
43
+			case "status":
44
+				$result = new StatusAction();
45
+				break;
46
+			case "stats":
47
+				$result = new StatsAction();
48
+				break;
49
+			case "help":
50
+				$result = new HelpAction();
51
+				break;
52
+			case "monitor":
53
+				$result = new MonitorAction();
54
+				break;
55
+			default:
56
+				$result = new UnknownAction();
57
+				break;
58
+		}
59 59
 
60
-        return $result;
61
-    }
60
+		return $result;
61
+	}
62 62
 }
63 63
\ No newline at end of file
Please login to merge, or discard this patch.
includes/Router/RequestRouter.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -358,7 +358,7 @@
 block discarded – undo
358 358
         $routeMap = $this->routePathSegments($classSegment, $requestedAction);
359 359
 
360 360
         if ($routeMap[0] === Page404::class) {
361
-            $routeMap = $this->routeSinglePathSegment($classSegment . '/' . $requestedAction);
361
+            $routeMap = $this->routeSinglePathSegment($classSegment.'/'.$requestedAction);
362 362
         }
363 363
 
364 364
         return $routeMap;
Please login to merge, or discard this patch.
Indentation   +434 added lines, -434 removed lines patch added patch discarded remove patch
@@ -62,438 +62,438 @@
 block discarded – undo
62 62
  */
63 63
 class RequestRouter implements IRequestRouter
64 64
 {
65
-    /**
66
-     * This is the core routing table for the application. The basic idea is:
67
-     *
68
-     *      array(
69
-     *          "foo" =>
70
-     *              array(
71
-     *                  "class"   => PageFoo::class,
72
-     *                  "actions" => array("bar", "other")
73
-     *              ),
74
-     * );
75
-     *
76
-     * Things to note:
77
-     *     - If no page is requested, we go to PageMain. PageMain can't have actions defined.
78
-     *
79
-     *     - If a page is defined and requested, but no action is requested, go to that page's main() method
80
-     *     - If a page is defined and requested, and an action is defined and requested, go to that action's method.
81
-     *     - If a page is defined and requested, and an action NOT defined and requested, go to Page404 and it's main()
82
-     *       method.
83
-     *     - If a page is NOT defined and requested, go to Page404 and it's main() method.
84
-     *
85
-     *     - Query parameters are ignored.
86
-     *
87
-     * The key point here is request routing with validation that this is allowed, before we start hitting the
88
-     * filesystem through the AutoLoader, and opening random files. Also, so that we validate the action requested
89
-     * before we start calling random methods through the web UI.
90
-     *
91
-     * Examples:
92
-     * /internal.php                => returns instance of PageMain, routed to main()
93
-     * /internal.php?query          => returns instance of PageMain, routed to main()
94
-     * /internal.php/foo            => returns instance of PageFoo, routed to main()
95
-     * /internal.php/foo?query      => returns instance of PageFoo, routed to main()
96
-     * /internal.php/foo/bar        => returns instance of PageFoo, routed to bar()
97
-     * /internal.php/foo/bar?query  => returns instance of PageFoo, routed to bar()
98
-     * /internal.php/foo/baz        => returns instance of Page404, routed to main()
99
-     * /internal.php/foo/baz?query  => returns instance of Page404, routed to main()
100
-     * /internal.php/bar            => returns instance of Page404, routed to main()
101
-     * /internal.php/bar?query      => returns instance of Page404, routed to main()
102
-     * /internal.php/bar/baz        => returns instance of Page404, routed to main()
103
-     * /internal.php/bar/baz?query  => returns instance of Page404, routed to main()
104
-     *
105
-     * Take care when changing this - a lot of places rely on the array key for redirects and other links. If you need
106
-     * to change the key, then you'll likely have to update a lot of files.
107
-     *
108
-     * @var array
109
-     */
110
-    private $routeMap = array(
111
-
112
-        //////////////////////////////////////////////////////////////////////////////////////////////////
113
-        // Login and registration
114
-        'logout'                      =>
115
-            array(
116
-                'class'   => PageLogout::class,
117
-                'actions' => array(),
118
-            ),
119
-        'login'                       =>
120
-            array(
121
-                'class'   => PagePasswordLogin::class,
122
-                'actions' => array(),
123
-            ),
124
-        'login/otp'                   =>
125
-            array(
126
-                'class'   => PageOtpLogin::class,
127
-                'actions' => array(),
128
-            ),
129
-        'login/u2f'                   =>
130
-            array(
131
-                'class'   => PageU2FLogin::class,
132
-                'actions' => array(),
133
-            ),
134
-        'forgotPassword'              =>
135
-            array(
136
-                'class'   => PageForgotPassword::class,
137
-                'actions' => array('reset'),
138
-            ),
139
-        'register'                    =>
140
-            array(
141
-                'class'   => PageRegisterOption::class,
142
-                'actions' => array(),
143
-            ),
144
-        'register/standard'           =>
145
-            array(
146
-                'class'   => PageRegisterStandard::class,
147
-                'actions' => array('done'),
148
-            ),
149
-
150
-        //////////////////////////////////////////////////////////////////////////////////////////////////
151
-        // Discovery
152
-        'search'                      =>
153
-            array(
154
-                'class'   => PageSearch::class,
155
-                'actions' => array(),
156
-            ),
157
-        'logs'                        =>
158
-            array(
159
-                'class'   => PageLog::class,
160
-                'actions' => array(),
161
-            ),
162
-
163
-        //////////////////////////////////////////////////////////////////////////////////////////////////
164
-        // Administration
165
-        'bans'                        =>
166
-            array(
167
-                'class'   => PageBan::class,
168
-                'actions' => array('set', 'remove'),
169
-            ),
170
-        'userManagement'              =>
171
-            array(
172
-                'class'   => PageUserManagement::class,
173
-                'actions' => array(
174
-                    'approve',
175
-                    'decline',
176
-                    'rename',
177
-                    'editUser',
178
-                    'suspend',
179
-                    'editRoles',
180
-                ),
181
-            ),
182
-        'siteNotice'                  =>
183
-            array(
184
-                'class'   => PageSiteNotice::class,
185
-                'actions' => array(),
186
-            ),
187
-        'emailManagement'             =>
188
-            array(
189
-                'class'   => PageEmailManagement::class,
190
-                'actions' => array('create', 'edit', 'view'),
191
-            ),
192
-        'jobQueue'                    =>
193
-            array(
194
-                'class'   => PageJobQueue::class,
195
-                'actions' => array('acknowledge', 'requeue', 'view', 'all'),
196
-            ),
197
-
198
-        //////////////////////////////////////////////////////////////////////////////////////////////////
199
-        // Personal preferences
200
-        'preferences'                 =>
201
-            array(
202
-                'class'   => PagePreferences::class,
203
-                'actions' => array(),
204
-            ),
205
-        'changePassword'              =>
206
-            array(
207
-                'class'   => PageChangePassword::class,
208
-                'actions' => array(),
209
-            ),
210
-        'multiFactor'                 =>
211
-            array(
212
-                'class'   => PageMultiFactor::class,
213
-                'actions' => array(
214
-                    'scratch',
215
-                    'enableYubikeyOtp',
216
-                    'disableYubikeyOtp',
217
-                    'enableTotp',
218
-                    'disableTotp',
219
-                    'enableU2F',
220
-                    'disableU2F',
221
-                ),
222
-            ),
223
-        'oauth'                       =>
224
-            array(
225
-                'class'   => PageOAuth::class,
226
-                'actions' => array('detach', 'attach'),
227
-            ),
228
-        'oauth/callback'              =>
229
-            array(
230
-                'class'   => PageOAuthCallback::class,
231
-                'actions' => array('authorise', 'create'),
232
-            ),
233
-
234
-        //////////////////////////////////////////////////////////////////////////////////////////////////
235
-        // Welcomer configuration
236
-        'welcomeTemplates'            =>
237
-            array(
238
-                'class'   => PageWelcomeTemplateManagement::class,
239
-                'actions' => array('select', 'edit', 'delete', 'add', 'view'),
240
-            ),
241
-
242
-        //////////////////////////////////////////////////////////////////////////////////////////////////
243
-        // Statistics
244
-        'statistics'                  =>
245
-            array(
246
-                'class'   => StatsMain::class,
247
-                'actions' => array(),
248
-            ),
249
-        'statistics/fastCloses'       =>
250
-            array(
251
-                'class'   => StatsFastCloses::class,
252
-                'actions' => array(),
253
-            ),
254
-        'statistics/inactiveUsers'    =>
255
-            array(
256
-                'class'   => StatsInactiveUsers::class,
257
-                'actions' => array(),
258
-            ),
259
-        'statistics/monthlyStats'     =>
260
-            array(
261
-                'class'   => StatsMonthlyStats::class,
262
-                'actions' => array(),
263
-            ),
264
-        'statistics/reservedRequests' =>
265
-            array(
266
-                'class'   => StatsReservedRequests::class,
267
-                'actions' => array(),
268
-            ),
269
-        'statistics/templateStats'    =>
270
-            array(
271
-                'class'   => StatsTemplateStats::class,
272
-                'actions' => array(),
273
-            ),
274
-        'statistics/topCreators'      =>
275
-            array(
276
-                'class'   => StatsTopCreators::class,
277
-                'actions' => array(),
278
-            ),
279
-        'statistics/users'            =>
280
-            array(
281
-                'class'   => StatsUsers::class,
282
-                'actions' => array('detail'),
283
-            ),
284
-
285
-        //////////////////////////////////////////////////////////////////////////////////////////////////
286
-        // Zoom page
287
-        'viewRequest'                 =>
288
-            array(
289
-                'class'   => PageViewRequest::class,
290
-                'actions' => array(),
291
-            ),
292
-        'viewRequest/reserve'         =>
293
-            array(
294
-                'class'   => PageReservation::class,
295
-                'actions' => array(),
296
-            ),
297
-        'viewRequest/breakReserve'    =>
298
-            array(
299
-                'class'   => PageBreakReservation::class,
300
-                'actions' => array(),
301
-            ),
302
-        'viewRequest/defer'           =>
303
-            array(
304
-                'class'   => PageDeferRequest::class,
305
-                'actions' => array(),
306
-            ),
307
-        'viewRequest/comment'         =>
308
-            array(
309
-                'class'   => PageComment::class,
310
-                'actions' => array(),
311
-            ),
312
-        'viewRequest/sendToUser'      =>
313
-            array(
314
-                'class'   => PageSendToUser::class,
315
-                'actions' => array(),
316
-            ),
317
-        'viewRequest/close'           =>
318
-            array(
319
-                'class'   => PageCloseRequest::class,
320
-                'actions' => array(),
321
-            ),
322
-        'viewRequest/create'          =>
323
-            array(
324
-                'class'   => PageCreateRequest::class,
325
-                'actions' => array(),
326
-            ),
327
-        'viewRequest/drop'            =>
328
-            array(
329
-                'class'   => PageDropRequest::class,
330
-                'actions' => array(),
331
-            ),
332
-        'viewRequest/custom'          =>
333
-            array(
334
-                'class'   => PageCustomClose::class,
335
-                'actions' => array(),
336
-            ),
337
-        'editComment'                 =>
338
-            array(
339
-                'class'   => PageEditComment::class,
340
-                'actions' => array(),
341
-            ),
342
-
343
-        //////////////////////////////////////////////////////////////////////////////////////////////////
344
-        // Misc stuff
345
-        'team'                        =>
346
-            array(
347
-                'class'   => PageTeam::class,
348
-                'actions' => array(),
349
-            ),
350
-        'requestList'                 =>
351
-            array(
352
-                'class'   => PageExpandedRequestList::class,
353
-                'actions' => array(),
354
-            ),
355
-    );
356
-
357
-    /**
358
-     * @return IRoutedTask
359
-     * @throws Exception
360
-     */
361
-    final public function route()
362
-    {
363
-        $pathInfo = WebRequest::pathInfo();
364
-
365
-        list($pageClass, $action) = $this->getRouteFromPath($pathInfo);
366
-
367
-        /** @var IRoutedTask $page */
368
-        $page = new $pageClass();
369
-
370
-        // Dynamic creation, so we've got to be careful here. We can't use built-in language type protection, so
371
-        // let's use our own.
372
-        if (!($page instanceof IRoutedTask)) {
373
-            throw new Exception('Expected a page, but this is not a page.');
374
-        }
375
-
376
-        // OK, I'm happy at this point that we know we're running a page, and we know it's probably what we want if it
377
-        // inherits PageBase and has been created from the routing map.
378
-        $page->setRoute($action);
379
-
380
-        return $page;
381
-    }
382
-
383
-    /**
384
-     * @param $pathInfo
385
-     *
386
-     * @return array
387
-     */
388
-    protected function getRouteFromPath($pathInfo)
389
-    {
390
-        if (count($pathInfo) === 0) {
391
-            // No pathInfo, so no page to load. Load the main page.
392
-            return $this->getDefaultRoute();
393
-        }
394
-        elseif (count($pathInfo) === 1) {
395
-            // Exactly one path info segment, it's got to be a page.
396
-            $classSegment = $pathInfo[0];
397
-
398
-            return $this->routeSinglePathSegment($classSegment);
399
-        }
400
-
401
-        // OK, we have two or more segments now.
402
-        if (count($pathInfo) > 2) {
403
-            // Let's handle more than two, and collapse it down into two.
404
-            $requestedAction = array_pop($pathInfo);
405
-            $classSegment = implode('/', $pathInfo);
406
-        }
407
-        else {
408
-            // Two path info segments.
409
-            $classSegment = $pathInfo[0];
410
-            $requestedAction = $pathInfo[1];
411
-        }
412
-
413
-        $routeMap = $this->routePathSegments($classSegment, $requestedAction);
414
-
415
-        if ($routeMap[0] === Page404::class) {
416
-            $routeMap = $this->routeSinglePathSegment($classSegment . '/' . $requestedAction);
417
-        }
418
-
419
-        return $routeMap;
420
-    }
421
-
422
-    /**
423
-     * @param $classSegment
424
-     *
425
-     * @return array
426
-     */
427
-    final protected function routeSinglePathSegment($classSegment)
428
-    {
429
-        $routeMap = $this->getRouteMap();
430
-        if (array_key_exists($classSegment, $routeMap)) {
431
-            // Route exists, but we don't have an action in path info, so default to main.
432
-            $pageClass = $routeMap[$classSegment]['class'];
433
-            $action = 'main';
434
-
435
-            return array($pageClass, $action);
436
-        }
437
-        else {
438
-            // Doesn't exist in map. Fall back to 404
439
-            $pageClass = Page404::class;
440
-            $action = "main";
441
-
442
-            return array($pageClass, $action);
443
-        }
444
-    }
445
-
446
-    /**
447
-     * @param $classSegment
448
-     * @param $requestedAction
449
-     *
450
-     * @return array
451
-     */
452
-    final protected function routePathSegments($classSegment, $requestedAction)
453
-    {
454
-        $routeMap = $this->getRouteMap();
455
-        if (array_key_exists($classSegment, $routeMap)) {
456
-            // Route exists, but we don't have an action in path info, so default to main.
457
-
458
-            if (isset($routeMap[$classSegment]['actions'])
459
-                && array_search($requestedAction, $routeMap[$classSegment]['actions']) !== false
460
-            ) {
461
-                // Action exists in allowed action list. Allow both the page and the action
462
-                $pageClass = $routeMap[$classSegment]['class'];
463
-                $action = $requestedAction;
464
-
465
-                return array($pageClass, $action);
466
-            }
467
-            else {
468
-                // Valid page, invalid action. 404 our way out.
469
-                $pageClass = Page404::class;
470
-                $action = 'main';
471
-
472
-                return array($pageClass, $action);
473
-            }
474
-        }
475
-        else {
476
-            // Class doesn't exist in map. Fall back to 404
477
-            $pageClass = Page404::class;
478
-            $action = 'main';
479
-
480
-            return array($pageClass, $action);
481
-        }
482
-    }
483
-
484
-    /**
485
-     * @return array
486
-     */
487
-    protected function getRouteMap()
488
-    {
489
-        return $this->routeMap;
490
-    }
491
-
492
-    /**
493
-     * @return callable
494
-     */
495
-    protected function getDefaultRoute()
496
-    {
497
-        return array(PageMain::class, "main");
498
-    }
65
+	/**
66
+	 * This is the core routing table for the application. The basic idea is:
67
+	 *
68
+	 *      array(
69
+	 *          "foo" =>
70
+	 *              array(
71
+	 *                  "class"   => PageFoo::class,
72
+	 *                  "actions" => array("bar", "other")
73
+	 *              ),
74
+	 * );
75
+	 *
76
+	 * Things to note:
77
+	 *     - If no page is requested, we go to PageMain. PageMain can't have actions defined.
78
+	 *
79
+	 *     - If a page is defined and requested, but no action is requested, go to that page's main() method
80
+	 *     - If a page is defined and requested, and an action is defined and requested, go to that action's method.
81
+	 *     - If a page is defined and requested, and an action NOT defined and requested, go to Page404 and it's main()
82
+	 *       method.
83
+	 *     - If a page is NOT defined and requested, go to Page404 and it's main() method.
84
+	 *
85
+	 *     - Query parameters are ignored.
86
+	 *
87
+	 * The key point here is request routing with validation that this is allowed, before we start hitting the
88
+	 * filesystem through the AutoLoader, and opening random files. Also, so that we validate the action requested
89
+	 * before we start calling random methods through the web UI.
90
+	 *
91
+	 * Examples:
92
+	 * /internal.php                => returns instance of PageMain, routed to main()
93
+	 * /internal.php?query          => returns instance of PageMain, routed to main()
94
+	 * /internal.php/foo            => returns instance of PageFoo, routed to main()
95
+	 * /internal.php/foo?query      => returns instance of PageFoo, routed to main()
96
+	 * /internal.php/foo/bar        => returns instance of PageFoo, routed to bar()
97
+	 * /internal.php/foo/bar?query  => returns instance of PageFoo, routed to bar()
98
+	 * /internal.php/foo/baz        => returns instance of Page404, routed to main()
99
+	 * /internal.php/foo/baz?query  => returns instance of Page404, routed to main()
100
+	 * /internal.php/bar            => returns instance of Page404, routed to main()
101
+	 * /internal.php/bar?query      => returns instance of Page404, routed to main()
102
+	 * /internal.php/bar/baz        => returns instance of Page404, routed to main()
103
+	 * /internal.php/bar/baz?query  => returns instance of Page404, routed to main()
104
+	 *
105
+	 * Take care when changing this - a lot of places rely on the array key for redirects and other links. If you need
106
+	 * to change the key, then you'll likely have to update a lot of files.
107
+	 *
108
+	 * @var array
109
+	 */
110
+	private $routeMap = array(
111
+
112
+		//////////////////////////////////////////////////////////////////////////////////////////////////
113
+		// Login and registration
114
+		'logout'                      =>
115
+			array(
116
+				'class'   => PageLogout::class,
117
+				'actions' => array(),
118
+			),
119
+		'login'                       =>
120
+			array(
121
+				'class'   => PagePasswordLogin::class,
122
+				'actions' => array(),
123
+			),
124
+		'login/otp'                   =>
125
+			array(
126
+				'class'   => PageOtpLogin::class,
127
+				'actions' => array(),
128
+			),
129
+		'login/u2f'                   =>
130
+			array(
131
+				'class'   => PageU2FLogin::class,
132
+				'actions' => array(),
133
+			),
134
+		'forgotPassword'              =>
135
+			array(
136
+				'class'   => PageForgotPassword::class,
137
+				'actions' => array('reset'),
138
+			),
139
+		'register'                    =>
140
+			array(
141
+				'class'   => PageRegisterOption::class,
142
+				'actions' => array(),
143
+			),
144
+		'register/standard'           =>
145
+			array(
146
+				'class'   => PageRegisterStandard::class,
147
+				'actions' => array('done'),
148
+			),
149
+
150
+		//////////////////////////////////////////////////////////////////////////////////////////////////
151
+		// Discovery
152
+		'search'                      =>
153
+			array(
154
+				'class'   => PageSearch::class,
155
+				'actions' => array(),
156
+			),
157
+		'logs'                        =>
158
+			array(
159
+				'class'   => PageLog::class,
160
+				'actions' => array(),
161
+			),
162
+
163
+		//////////////////////////////////////////////////////////////////////////////////////////////////
164
+		// Administration
165
+		'bans'                        =>
166
+			array(
167
+				'class'   => PageBan::class,
168
+				'actions' => array('set', 'remove'),
169
+			),
170
+		'userManagement'              =>
171
+			array(
172
+				'class'   => PageUserManagement::class,
173
+				'actions' => array(
174
+					'approve',
175
+					'decline',
176
+					'rename',
177
+					'editUser',
178
+					'suspend',
179
+					'editRoles',
180
+				),
181
+			),
182
+		'siteNotice'                  =>
183
+			array(
184
+				'class'   => PageSiteNotice::class,
185
+				'actions' => array(),
186
+			),
187
+		'emailManagement'             =>
188
+			array(
189
+				'class'   => PageEmailManagement::class,
190
+				'actions' => array('create', 'edit', 'view'),
191
+			),
192
+		'jobQueue'                    =>
193
+			array(
194
+				'class'   => PageJobQueue::class,
195
+				'actions' => array('acknowledge', 'requeue', 'view', 'all'),
196
+			),
197
+
198
+		//////////////////////////////////////////////////////////////////////////////////////////////////
199
+		// Personal preferences
200
+		'preferences'                 =>
201
+			array(
202
+				'class'   => PagePreferences::class,
203
+				'actions' => array(),
204
+			),
205
+		'changePassword'              =>
206
+			array(
207
+				'class'   => PageChangePassword::class,
208
+				'actions' => array(),
209
+			),
210
+		'multiFactor'                 =>
211
+			array(
212
+				'class'   => PageMultiFactor::class,
213
+				'actions' => array(
214
+					'scratch',
215
+					'enableYubikeyOtp',
216
+					'disableYubikeyOtp',
217
+					'enableTotp',
218
+					'disableTotp',
219
+					'enableU2F',
220
+					'disableU2F',
221
+				),
222
+			),
223
+		'oauth'                       =>
224
+			array(
225
+				'class'   => PageOAuth::class,
226
+				'actions' => array('detach', 'attach'),
227
+			),
228
+		'oauth/callback'              =>
229
+			array(
230
+				'class'   => PageOAuthCallback::class,
231
+				'actions' => array('authorise', 'create'),
232
+			),
233
+
234
+		//////////////////////////////////////////////////////////////////////////////////////////////////
235
+		// Welcomer configuration
236
+		'welcomeTemplates'            =>
237
+			array(
238
+				'class'   => PageWelcomeTemplateManagement::class,
239
+				'actions' => array('select', 'edit', 'delete', 'add', 'view'),
240
+			),
241
+
242
+		//////////////////////////////////////////////////////////////////////////////////////////////////
243
+		// Statistics
244
+		'statistics'                  =>
245
+			array(
246
+				'class'   => StatsMain::class,
247
+				'actions' => array(),
248
+			),
249
+		'statistics/fastCloses'       =>
250
+			array(
251
+				'class'   => StatsFastCloses::class,
252
+				'actions' => array(),
253
+			),
254
+		'statistics/inactiveUsers'    =>
255
+			array(
256
+				'class'   => StatsInactiveUsers::class,
257
+				'actions' => array(),
258
+			),
259
+		'statistics/monthlyStats'     =>
260
+			array(
261
+				'class'   => StatsMonthlyStats::class,
262
+				'actions' => array(),
263
+			),
264
+		'statistics/reservedRequests' =>
265
+			array(
266
+				'class'   => StatsReservedRequests::class,
267
+				'actions' => array(),
268
+			),
269
+		'statistics/templateStats'    =>
270
+			array(
271
+				'class'   => StatsTemplateStats::class,
272
+				'actions' => array(),
273
+			),
274
+		'statistics/topCreators'      =>
275
+			array(
276
+				'class'   => StatsTopCreators::class,
277
+				'actions' => array(),
278
+			),
279
+		'statistics/users'            =>
280
+			array(
281
+				'class'   => StatsUsers::class,
282
+				'actions' => array('detail'),
283
+			),
284
+
285
+		//////////////////////////////////////////////////////////////////////////////////////////////////
286
+		// Zoom page
287
+		'viewRequest'                 =>
288
+			array(
289
+				'class'   => PageViewRequest::class,
290
+				'actions' => array(),
291
+			),
292
+		'viewRequest/reserve'         =>
293
+			array(
294
+				'class'   => PageReservation::class,
295
+				'actions' => array(),
296
+			),
297
+		'viewRequest/breakReserve'    =>
298
+			array(
299
+				'class'   => PageBreakReservation::class,
300
+				'actions' => array(),
301
+			),
302
+		'viewRequest/defer'           =>
303
+			array(
304
+				'class'   => PageDeferRequest::class,
305
+				'actions' => array(),
306
+			),
307
+		'viewRequest/comment'         =>
308
+			array(
309
+				'class'   => PageComment::class,
310
+				'actions' => array(),
311
+			),
312
+		'viewRequest/sendToUser'      =>
313
+			array(
314
+				'class'   => PageSendToUser::class,
315
+				'actions' => array(),
316
+			),
317
+		'viewRequest/close'           =>
318
+			array(
319
+				'class'   => PageCloseRequest::class,
320
+				'actions' => array(),
321
+			),
322
+		'viewRequest/create'          =>
323
+			array(
324
+				'class'   => PageCreateRequest::class,
325
+				'actions' => array(),
326
+			),
327
+		'viewRequest/drop'            =>
328
+			array(
329
+				'class'   => PageDropRequest::class,
330
+				'actions' => array(),
331
+			),
332
+		'viewRequest/custom'          =>
333
+			array(
334
+				'class'   => PageCustomClose::class,
335
+				'actions' => array(),
336
+			),
337
+		'editComment'                 =>
338
+			array(
339
+				'class'   => PageEditComment::class,
340
+				'actions' => array(),
341
+			),
342
+
343
+		//////////////////////////////////////////////////////////////////////////////////////////////////
344
+		// Misc stuff
345
+		'team'                        =>
346
+			array(
347
+				'class'   => PageTeam::class,
348
+				'actions' => array(),
349
+			),
350
+		'requestList'                 =>
351
+			array(
352
+				'class'   => PageExpandedRequestList::class,
353
+				'actions' => array(),
354
+			),
355
+	);
356
+
357
+	/**
358
+	 * @return IRoutedTask
359
+	 * @throws Exception
360
+	 */
361
+	final public function route()
362
+	{
363
+		$pathInfo = WebRequest::pathInfo();
364
+
365
+		list($pageClass, $action) = $this->getRouteFromPath($pathInfo);
366
+
367
+		/** @var IRoutedTask $page */
368
+		$page = new $pageClass();
369
+
370
+		// Dynamic creation, so we've got to be careful here. We can't use built-in language type protection, so
371
+		// let's use our own.
372
+		if (!($page instanceof IRoutedTask)) {
373
+			throw new Exception('Expected a page, but this is not a page.');
374
+		}
375
+
376
+		// OK, I'm happy at this point that we know we're running a page, and we know it's probably what we want if it
377
+		// inherits PageBase and has been created from the routing map.
378
+		$page->setRoute($action);
379
+
380
+		return $page;
381
+	}
382
+
383
+	/**
384
+	 * @param $pathInfo
385
+	 *
386
+	 * @return array
387
+	 */
388
+	protected function getRouteFromPath($pathInfo)
389
+	{
390
+		if (count($pathInfo) === 0) {
391
+			// No pathInfo, so no page to load. Load the main page.
392
+			return $this->getDefaultRoute();
393
+		}
394
+		elseif (count($pathInfo) === 1) {
395
+			// Exactly one path info segment, it's got to be a page.
396
+			$classSegment = $pathInfo[0];
397
+
398
+			return $this->routeSinglePathSegment($classSegment);
399
+		}
400
+
401
+		// OK, we have two or more segments now.
402
+		if (count($pathInfo) > 2) {
403
+			// Let's handle more than two, and collapse it down into two.
404
+			$requestedAction = array_pop($pathInfo);
405
+			$classSegment = implode('/', $pathInfo);
406
+		}
407
+		else {
408
+			// Two path info segments.
409
+			$classSegment = $pathInfo[0];
410
+			$requestedAction = $pathInfo[1];
411
+		}
412
+
413
+		$routeMap = $this->routePathSegments($classSegment, $requestedAction);
414
+
415
+		if ($routeMap[0] === Page404::class) {
416
+			$routeMap = $this->routeSinglePathSegment($classSegment . '/' . $requestedAction);
417
+		}
418
+
419
+		return $routeMap;
420
+	}
421
+
422
+	/**
423
+	 * @param $classSegment
424
+	 *
425
+	 * @return array
426
+	 */
427
+	final protected function routeSinglePathSegment($classSegment)
428
+	{
429
+		$routeMap = $this->getRouteMap();
430
+		if (array_key_exists($classSegment, $routeMap)) {
431
+			// Route exists, but we don't have an action in path info, so default to main.
432
+			$pageClass = $routeMap[$classSegment]['class'];
433
+			$action = 'main';
434
+
435
+			return array($pageClass, $action);
436
+		}
437
+		else {
438
+			// Doesn't exist in map. Fall back to 404
439
+			$pageClass = Page404::class;
440
+			$action = "main";
441
+
442
+			return array($pageClass, $action);
443
+		}
444
+	}
445
+
446
+	/**
447
+	 * @param $classSegment
448
+	 * @param $requestedAction
449
+	 *
450
+	 * @return array
451
+	 */
452
+	final protected function routePathSegments($classSegment, $requestedAction)
453
+	{
454
+		$routeMap = $this->getRouteMap();
455
+		if (array_key_exists($classSegment, $routeMap)) {
456
+			// Route exists, but we don't have an action in path info, so default to main.
457
+
458
+			if (isset($routeMap[$classSegment]['actions'])
459
+				&& array_search($requestedAction, $routeMap[$classSegment]['actions']) !== false
460
+			) {
461
+				// Action exists in allowed action list. Allow both the page and the action
462
+				$pageClass = $routeMap[$classSegment]['class'];
463
+				$action = $requestedAction;
464
+
465
+				return array($pageClass, $action);
466
+			}
467
+			else {
468
+				// Valid page, invalid action. 404 our way out.
469
+				$pageClass = Page404::class;
470
+				$action = 'main';
471
+
472
+				return array($pageClass, $action);
473
+			}
474
+		}
475
+		else {
476
+			// Class doesn't exist in map. Fall back to 404
477
+			$pageClass = Page404::class;
478
+			$action = 'main';
479
+
480
+			return array($pageClass, $action);
481
+		}
482
+	}
483
+
484
+	/**
485
+	 * @return array
486
+	 */
487
+	protected function getRouteMap()
488
+	{
489
+		return $this->routeMap;
490
+	}
491
+
492
+	/**
493
+	 * @return callable
494
+	 */
495
+	protected function getDefaultRoute()
496
+	{
497
+		return array(PageMain::class, "main");
498
+	}
499 499
 }
Please login to merge, or discard this patch.
includes/Router/PublicRequestRouter.php 1 patch
Indentation   +37 added lines, -37 removed lines patch added patch discarded remove patch
@@ -15,42 +15,42 @@
 block discarded – undo
15 15
 
16 16
 class PublicRequestRouter extends RequestRouter
17 17
 {
18
-    /**
19
-     * Gets the route map to be used by this request router.
20
-     *
21
-     * @return array
22
-     */
23
-    protected function getRouteMap()
24
-    {
25
-        return array(
26
-            // Page showing a message stating the request has been submitted to our internal queues
27
-            'requestSubmitted'          =>
28
-                array(
29
-                    'class'   => PageRequestSubmitted::class,
30
-                    'actions' => array(),
31
-                ),
32
-            // Page showing a message stating that email confirmation is required to continue
33
-            'emailConfirmationRequired' =>
34
-                array(
35
-                    'class'   => PageEmailConfirmationRequired::class,
36
-                    'actions' => array(),
37
-                ),
38
-            // Action page which handles email confirmation
39
-            'confirmEmail'              =>
40
-                array(
41
-                    'class'   => PageConfirmEmail::class,
42
-                    'actions' => array(),
43
-                ),
44
-        );
45
-    }
18
+	/**
19
+	 * Gets the route map to be used by this request router.
20
+	 *
21
+	 * @return array
22
+	 */
23
+	protected function getRouteMap()
24
+	{
25
+		return array(
26
+			// Page showing a message stating the request has been submitted to our internal queues
27
+			'requestSubmitted'          =>
28
+				array(
29
+					'class'   => PageRequestSubmitted::class,
30
+					'actions' => array(),
31
+				),
32
+			// Page showing a message stating that email confirmation is required to continue
33
+			'emailConfirmationRequired' =>
34
+				array(
35
+					'class'   => PageEmailConfirmationRequired::class,
36
+					'actions' => array(),
37
+				),
38
+			// Action page which handles email confirmation
39
+			'confirmEmail'              =>
40
+				array(
41
+					'class'   => PageConfirmEmail::class,
42
+					'actions' => array(),
43
+				),
44
+		);
45
+	}
46 46
 
47
-    /**
48
-     * Gets the default route if no explicit route is requested.
49
-     *
50
-     * @return callable
51
-     */
52
-    protected function getDefaultRoute()
53
-    {
54
-        return array(PageRequestAccount::class, 'main');
55
-    }
47
+	/**
48
+	 * Gets the default route if no explicit route is requested.
49
+	 *
50
+	 * @return callable
51
+	 */
52
+	protected function getDefaultRoute()
53
+	{
54
+		return array(PageRequestAccount::class, 'main');
55
+	}
56 56
 }
57 57
\ No newline at end of file
Please login to merge, or discard this patch.
includes/Router/IRequestRouter.php 1 patch
Indentation   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -18,9 +18,9 @@
 block discarded – undo
18 18
  */
19 19
 interface IRequestRouter
20 20
 {
21
-    /**
22
-     * @return IRoutedTask
23
-     * @throws Exception
24
-     */
25
-    public function route();
21
+	/**
22
+	 * @return IRoutedTask
23
+	 * @throws Exception
24
+	 */
25
+	public function route();
26 26
 }
27 27
\ No newline at end of file
Please login to merge, or discard this patch.
includes/AutoLoader.php 2 patches
Indentation   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -13,36 +13,36 @@
 block discarded – undo
13 13
  */
14 14
 class AutoLoader
15 15
 {
16
-    public static function load($class)
17
-    {
18
-        // handle namespaces sensibly
19
-        if (strpos($class, "Waca") !== false) {
20
-            // strip off the initial namespace
21
-            $class = str_replace("Waca\\", "", $class);
16
+	public static function load($class)
17
+	{
18
+		// handle namespaces sensibly
19
+		if (strpos($class, "Waca") !== false) {
20
+			// strip off the initial namespace
21
+			$class = str_replace("Waca\\", "", $class);
22 22
 
23
-            // swap backslashes for forward slashes to map to directory names
24
-            $class = str_replace("\\", "/", $class);
25
-        }
23
+			// swap backslashes for forward slashes to map to directory names
24
+			$class = str_replace("\\", "/", $class);
25
+		}
26 26
 
27
-        $paths = array(
28
-            __DIR__ . '/' . $class . ".php",
29
-            __DIR__ . '/DataObjects/' . $class . ".php",
30
-            __DIR__ . '/Providers/' . $class . ".php",
31
-            __DIR__ . '/Providers/Interfaces/' . $class . ".php",
32
-            __DIR__ . '/Validation/' . $class . ".php",
33
-            __DIR__ . '/Helpers/' . $class . ".php",
34
-            __DIR__ . '/Helpers/Interfaces/' . $class . ".php",
35
-            __DIR__ . '/' . $class . ".php",
36
-        );
27
+		$paths = array(
28
+			__DIR__ . '/' . $class . ".php",
29
+			__DIR__ . '/DataObjects/' . $class . ".php",
30
+			__DIR__ . '/Providers/' . $class . ".php",
31
+			__DIR__ . '/Providers/Interfaces/' . $class . ".php",
32
+			__DIR__ . '/Validation/' . $class . ".php",
33
+			__DIR__ . '/Helpers/' . $class . ".php",
34
+			__DIR__ . '/Helpers/Interfaces/' . $class . ".php",
35
+			__DIR__ . '/' . $class . ".php",
36
+		);
37 37
 
38
-        foreach ($paths as $file) {
39
-            if (file_exists($file)) {
40
-                require_once($file);
41
-            }
38
+		foreach ($paths as $file) {
39
+			if (file_exists($file)) {
40
+				require_once($file);
41
+			}
42 42
 
43
-            if (class_exists($class)) {
44
-                return;
45
-            }
46
-        }
47
-    }
43
+			if (class_exists($class)) {
44
+				return;
45
+			}
46
+		}
47
+	}
48 48
 }
Please login to merge, or discard this patch.
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -25,14 +25,14 @@
 block discarded – undo
25 25
         }
26 26
 
27 27
         $paths = array(
28
-            __DIR__ . '/' . $class . ".php",
29
-            __DIR__ . '/DataObjects/' . $class . ".php",
30
-            __DIR__ . '/Providers/' . $class . ".php",
31
-            __DIR__ . '/Providers/Interfaces/' . $class . ".php",
32
-            __DIR__ . '/Validation/' . $class . ".php",
33
-            __DIR__ . '/Helpers/' . $class . ".php",
34
-            __DIR__ . '/Helpers/Interfaces/' . $class . ".php",
35
-            __DIR__ . '/' . $class . ".php",
28
+            __DIR__.'/'.$class.".php",
29
+            __DIR__.'/DataObjects/'.$class.".php",
30
+            __DIR__.'/Providers/'.$class.".php",
31
+            __DIR__.'/Providers/Interfaces/'.$class.".php",
32
+            __DIR__.'/Validation/'.$class.".php",
33
+            __DIR__.'/Helpers/'.$class.".php",
34
+            __DIR__.'/Helpers/Interfaces/'.$class.".php",
35
+            __DIR__.'/'.$class.".php",
36 36
         );
37 37
 
38 38
         foreach ($paths as $file) {
Please login to merge, or discard this patch.
includes/StringFunctions.php 2 patches
Indentation   +64 added lines, -64 removed lines patch added patch discarded remove patch
@@ -10,77 +10,77 @@
 block discarded – undo
10 10
 
11 11
 class StringFunctions
12 12
 {
13
-    /**
14
-     * Formats a string to be used as a username.
15
-     *
16
-     * @param $username
17
-     *
18
-     * @return string
19
-     */
20
-    public function formatAsUsername($username)
21
-    {
22
-        // trim whitespace from the ends
23
-        $uname = mb_ereg_replace("^[ \t]+|[ \t]+$", "", $username);
13
+	/**
14
+	 * Formats a string to be used as a username.
15
+	 *
16
+	 * @param $username
17
+	 *
18
+	 * @return string
19
+	 */
20
+	public function formatAsUsername($username)
21
+	{
22
+		// trim whitespace from the ends
23
+		$uname = mb_ereg_replace("^[ \t]+|[ \t]+$", "", $username);
24 24
 
25
-        // convert first char to uppercase
26
-        $uname = $this->ucfirst($uname);
25
+		// convert first char to uppercase
26
+		$uname = $this->ucfirst($uname);
27 27
 
28
-        // replace spaces with underscores
29
-        $uname = mb_ereg_replace("[ ]+", "_", $uname);
28
+		// replace spaces with underscores
29
+		$uname = mb_ereg_replace("[ ]+", "_", $uname);
30 30
 
31
-        // trim underscores from the end
32
-        $uname = mb_ereg_replace("[_]+$", "", $uname);
31
+		// trim underscores from the end
32
+		$uname = mb_ereg_replace("[_]+$", "", $uname);
33 33
 
34
-        return $uname;
35
-    }
34
+		return $uname;
35
+	}
36 36
 
37
-    /**
38
-     * Formats a string to be used as an email (specifically strips whitespace
39
-     * from the beginning/end of the Email, as well as immediately before/after
40
-     * the @ in the Email).
41
-     *
42
-     * @param $email
43
-     *
44
-     * @return string
45
-     */
46
-    public static function formatAsEmail($email)
47
-    {
48
-        // trim whitespace from the ends
49
-        $newemail = mb_ereg_replace("^[ \t]+|[ \t]+$", "", $email);
37
+	/**
38
+	 * Formats a string to be used as an email (specifically strips whitespace
39
+	 * from the beginning/end of the Email, as well as immediately before/after
40
+	 * the @ in the Email).
41
+	 *
42
+	 * @param $email
43
+	 *
44
+	 * @return string
45
+	 */
46
+	public static function formatAsEmail($email)
47
+	{
48
+		// trim whitespace from the ends
49
+		$newemail = mb_ereg_replace("^[ \t]+|[ \t]+$", "", $email);
50 50
 
51
-        // trim whitespace from around the email address
52
-        $newemail = mb_ereg_replace("[ \t]+@", "@", $newemail);
53
-        $newemail = mb_ereg_replace("@[ \t]+", "@", $newemail);
51
+		// trim whitespace from around the email address
52
+		$newemail = mb_ereg_replace("[ \t]+@", "@", $newemail);
53
+		$newemail = mb_ereg_replace("@[ \t]+", "@", $newemail);
54 54
 
55
-        return $newemail;
56
-    }
55
+		return $newemail;
56
+	}
57 57
 
58
-    /**
59
-     * Returns true if a string is a multibyte string
60
-     *
61
-     * @param string $string
62
-     *
63
-     * @return bool
64
-     */
65
-    public function isMultibyte($string)
66
-    {
67
-        return strlen($string) !== mb_strlen($string);
68
-    }
58
+	/**
59
+	 * Returns true if a string is a multibyte string
60
+	 *
61
+	 * @param string $string
62
+	 *
63
+	 * @return bool
64
+	 */
65
+	public function isMultibyte($string)
66
+	{
67
+		return strlen($string) !== mb_strlen($string);
68
+	}
69 69
 
70
-    /**
71
-     * Make a string's first character uppercase
72
-     *
73
-     * @param string $string
74
-     *
75
-     * @return string
76
-     */
77
-    public function ucfirst($string)
78
-    {
79
-        if (ord($string) < 128) {
80
-            return ucfirst($string);
81
-        }
82
-        else {
83
-            return mb_strtoupper(mb_substr($string, 0, 1)) . mb_substr($string, 1);
84
-        }
85
-    }
70
+	/**
71
+	 * Make a string's first character uppercase
72
+	 *
73
+	 * @param string $string
74
+	 *
75
+	 * @return string
76
+	 */
77
+	public function ucfirst($string)
78
+	{
79
+		if (ord($string) < 128) {
80
+			return ucfirst($string);
81
+		}
82
+		else {
83
+			return mb_strtoupper(mb_substr($string, 0, 1)) . mb_substr($string, 1);
84
+		}
85
+	}
86 86
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -80,7 +80,7 @@
 block discarded – undo
80 80
             return ucfirst($string);
81 81
         }
82 82
         else {
83
-            return mb_strtoupper(mb_substr($string, 0, 1)) . mb_substr($string, 1);
83
+            return mb_strtoupper(mb_substr($string, 0, 1)).mb_substr($string, 1);
84 84
         }
85 85
     }
86 86
 }
Please login to merge, or discard this patch.
includes/ConsoleTasks/UpdateTorExitTask.php 1 patch
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -13,14 +13,14 @@
 block discarded – undo
13 13
 
14 14
 class UpdateTorExitTask extends ConsoleTaskBase
15 15
 {
16
-    /**
17
-     * @return void
18
-     */
19
-    public function execute()
20
-    {
21
-        TorExitProvider::regenerate(
22
-            $this->getDatabase(),
23
-            $this->getHttpHelper(),
24
-            $this->getSiteConfiguration()->getTorExitPaths());
25
-    }
16
+	/**
17
+	 * @return void
18
+	 */
19
+	public function execute()
20
+	{
21
+		TorExitProvider::regenerate(
22
+			$this->getDatabase(),
23
+			$this->getHttpHelper(),
24
+			$this->getSiteConfiguration()->getTorExitPaths());
25
+	}
26 26
 }
27 27
\ No newline at end of file
Please login to merge, or discard this patch.
includes/ConsoleTasks/RecreateTrustedIpTableTask.php 2 patches
Indentation   +148 added lines, -148 removed lines patch added patch discarded remove patch
@@ -16,152 +16,152 @@
 block discarded – undo
16 16
 
17 17
 class RecreateTrustedIpTableTask extends ConsoleTaskBase
18 18
 {
19
-    public function execute()
20
-    {
21
-
22
-        echo "Fetching file...\n";
23
-
24
-        $htmlfile = file($this->getSiteConfiguration()->getXffTrustedHostsFile(),
25
-            FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
26
-
27
-        $ip = array();
28
-        $iprange = array();
29
-        $dnsdomain = array();
30
-
31
-        echo "Sorting file...\n";
32
-        $this->readFile($htmlfile, $iprange, $ip, $dnsdomain);
33
-
34
-        echo "Exploding CIDRs...\n";
35
-        $this->explodeCidrs($iprange, $ip);
36
-
37
-        echo "Resolving DNS...\n";
38
-        $this->resolveDns($dnsdomain, $ip);
39
-
40
-        echo "Uniq-ing array...\n";
41
-
42
-        $ip = array_unique($ip);
43
-
44
-        $database = $this->getDatabase();
45
-
46
-        $database->exec('DELETE FROM xfftrustcache;');
47
-
48
-        $insert = $database->prepare('INSERT INTO xfftrustcache (ip) VALUES (:ip);');
49
-
50
-        $this->doInserts($ip, $insert);
51
-    }
52
-
53
-    /**
54
-     * @param string[] $dnsDomains  the DNS domains to resolve
55
-     * @param string[] $ipAddresses existing array of IPs to add to
56
-     */
57
-    protected function resolveDns($dnsDomains, &$ipAddresses)
58
-    {
59
-        foreach ($dnsDomains as $domain) {
60
-            $ipList = gethostbynamel($domain);
61
-
62
-            if ($ipList === false) {
63
-                echo "Invalid DNS name $domain\n";
64
-                continue;
65
-            }
66
-
67
-            foreach ($ipList as $ipAddress) {
68
-                $ipAddresses[] = $ipAddress;
69
-            }
70
-
71
-            // don't DoS
72
-            usleep(10000);
73
-        }
74
-    }
75
-
76
-    /**
77
-     * @param $iprange
78
-     * @param $ip
79
-     */
80
-    protected function explodeCidrs($iprange, &$ip)
81
-    {
82
-        foreach ($iprange as $r) {
83
-            $ips = $this->getXffTrustProvider()->explodeCidr($r);
84
-
85
-            foreach ($ips as $i) {
86
-                $ip[] = $i;
87
-            }
88
-        }
89
-    }
90
-
91
-    /**
92
-     * @param $htmlfile
93
-     * @param $iprange
94
-     * @param $ip
95
-     * @param $dnsdomain
96
-     */
97
-    protected function readFile($htmlfile, &$iprange, &$ip, &$dnsdomain)
98
-    {
99
-        foreach ($htmlfile as $line_num => $rawline) {
100
-            // remove the comments
101
-            $hashPos = strpos($rawline, '#');
102
-            if ($hashPos !== false) {
103
-                $line = substr($rawline, 0, $hashPos);
104
-            }
105
-            else {
106
-                $line = $rawline;
107
-            }
108
-
109
-            $line = trim($line);
110
-
111
-            // this was a comment or empty line...
112
-            if ($line == "") {
113
-                continue;
114
-            }
115
-
116
-            // match a regex of an CIDR range:
117
-            $ipcidr = '@' . RegexConstants::IPV4 . RegexConstants::IPV4_CIDR . '@';
118
-            if (preg_match($ipcidr, $line) === 1) {
119
-                $iprange[] = $line;
120
-                continue;
121
-            }
122
-
123
-            $ipnoncidr = '@' . RegexConstants::IPV4 . '@';
124
-            if (preg_match($ipnoncidr, $line) === 1) {
125
-                $ip[] = $line;
126
-                continue;
127
-            }
128
-
129
-            // it's probably a DNS name.
130
-            $dnsdomain[] = $line;
131
-        }
132
-    }
133
-
134
-    /**
135
-     * @param array        $ip
136
-     * @param PDOStatement $insert
137
-     *
138
-     * @throws Exception
139
-     */
140
-    protected function doInserts($ip, PDOStatement $insert)
141
-    {
142
-        $successful = true;
143
-
144
-        foreach ($ip as $i) {
145
-            if (count($i) > 15) {
146
-                echo "Rejected $i\n";
147
-                $successful = false;
148
-
149
-                continue;
150
-            }
151
-
152
-            try {
153
-                $insert->execute(array(':ip' => $i));
154
-            }
155
-            catch (PDOException $ex) {
156
-                echo "Exception on $i :\n";
157
-                echo $ex->getMessage();
158
-                $successful = false;
159
-                break;
160
-            }
161
-        }
162
-
163
-        if (!$successful) {
164
-            throw new Exception('Encountered errors during transaction processing');
165
-        }
166
-    }
19
+	public function execute()
20
+	{
21
+
22
+		echo "Fetching file...\n";
23
+
24
+		$htmlfile = file($this->getSiteConfiguration()->getXffTrustedHostsFile(),
25
+			FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
26
+
27
+		$ip = array();
28
+		$iprange = array();
29
+		$dnsdomain = array();
30
+
31
+		echo "Sorting file...\n";
32
+		$this->readFile($htmlfile, $iprange, $ip, $dnsdomain);
33
+
34
+		echo "Exploding CIDRs...\n";
35
+		$this->explodeCidrs($iprange, $ip);
36
+
37
+		echo "Resolving DNS...\n";
38
+		$this->resolveDns($dnsdomain, $ip);
39
+
40
+		echo "Uniq-ing array...\n";
41
+
42
+		$ip = array_unique($ip);
43
+
44
+		$database = $this->getDatabase();
45
+
46
+		$database->exec('DELETE FROM xfftrustcache;');
47
+
48
+		$insert = $database->prepare('INSERT INTO xfftrustcache (ip) VALUES (:ip);');
49
+
50
+		$this->doInserts($ip, $insert);
51
+	}
52
+
53
+	/**
54
+	 * @param string[] $dnsDomains  the DNS domains to resolve
55
+	 * @param string[] $ipAddresses existing array of IPs to add to
56
+	 */
57
+	protected function resolveDns($dnsDomains, &$ipAddresses)
58
+	{
59
+		foreach ($dnsDomains as $domain) {
60
+			$ipList = gethostbynamel($domain);
61
+
62
+			if ($ipList === false) {
63
+				echo "Invalid DNS name $domain\n";
64
+				continue;
65
+			}
66
+
67
+			foreach ($ipList as $ipAddress) {
68
+				$ipAddresses[] = $ipAddress;
69
+			}
70
+
71
+			// don't DoS
72
+			usleep(10000);
73
+		}
74
+	}
75
+
76
+	/**
77
+	 * @param $iprange
78
+	 * @param $ip
79
+	 */
80
+	protected function explodeCidrs($iprange, &$ip)
81
+	{
82
+		foreach ($iprange as $r) {
83
+			$ips = $this->getXffTrustProvider()->explodeCidr($r);
84
+
85
+			foreach ($ips as $i) {
86
+				$ip[] = $i;
87
+			}
88
+		}
89
+	}
90
+
91
+	/**
92
+	 * @param $htmlfile
93
+	 * @param $iprange
94
+	 * @param $ip
95
+	 * @param $dnsdomain
96
+	 */
97
+	protected function readFile($htmlfile, &$iprange, &$ip, &$dnsdomain)
98
+	{
99
+		foreach ($htmlfile as $line_num => $rawline) {
100
+			// remove the comments
101
+			$hashPos = strpos($rawline, '#');
102
+			if ($hashPos !== false) {
103
+				$line = substr($rawline, 0, $hashPos);
104
+			}
105
+			else {
106
+				$line = $rawline;
107
+			}
108
+
109
+			$line = trim($line);
110
+
111
+			// this was a comment or empty line...
112
+			if ($line == "") {
113
+				continue;
114
+			}
115
+
116
+			// match a regex of an CIDR range:
117
+			$ipcidr = '@' . RegexConstants::IPV4 . RegexConstants::IPV4_CIDR . '@';
118
+			if (preg_match($ipcidr, $line) === 1) {
119
+				$iprange[] = $line;
120
+				continue;
121
+			}
122
+
123
+			$ipnoncidr = '@' . RegexConstants::IPV4 . '@';
124
+			if (preg_match($ipnoncidr, $line) === 1) {
125
+				$ip[] = $line;
126
+				continue;
127
+			}
128
+
129
+			// it's probably a DNS name.
130
+			$dnsdomain[] = $line;
131
+		}
132
+	}
133
+
134
+	/**
135
+	 * @param array        $ip
136
+	 * @param PDOStatement $insert
137
+	 *
138
+	 * @throws Exception
139
+	 */
140
+	protected function doInserts($ip, PDOStatement $insert)
141
+	{
142
+		$successful = true;
143
+
144
+		foreach ($ip as $i) {
145
+			if (count($i) > 15) {
146
+				echo "Rejected $i\n";
147
+				$successful = false;
148
+
149
+				continue;
150
+			}
151
+
152
+			try {
153
+				$insert->execute(array(':ip' => $i));
154
+			}
155
+			catch (PDOException $ex) {
156
+				echo "Exception on $i :\n";
157
+				echo $ex->getMessage();
158
+				$successful = false;
159
+				break;
160
+			}
161
+		}
162
+
163
+		if (!$successful) {
164
+			throw new Exception('Encountered errors during transaction processing');
165
+		}
166
+	}
167 167
 }
168 168
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -114,13 +114,13 @@
 block discarded – undo
114 114
             }
115 115
 
116 116
             // match a regex of an CIDR range:
117
-            $ipcidr = '@' . RegexConstants::IPV4 . RegexConstants::IPV4_CIDR . '@';
117
+            $ipcidr = '@'.RegexConstants::IPV4.RegexConstants::IPV4_CIDR.'@';
118 118
             if (preg_match($ipcidr, $line) === 1) {
119 119
                 $iprange[] = $line;
120 120
                 continue;
121 121
             }
122 122
 
123
-            $ipnoncidr = '@' . RegexConstants::IPV4 . '@';
123
+            $ipnoncidr = '@'.RegexConstants::IPV4.'@';
124 124
             if (preg_match($ipnoncidr, $line) === 1) {
125 125
                 $ip[] = $line;
126 126
                 continue;
Please login to merge, or discard this patch.
includes/ConsoleTasks/ClearExpiredIdentificationData.php 1 patch
Indentation   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -13,11 +13,11 @@
 block discarded – undo
13 13
 
14 14
 class ClearExpiredIdentificationData extends ConsoleTaskBase
15 15
 {
16
-    /**
17
-     * @return void
18
-     */
19
-    public function execute()
20
-    {
21
-        IdentificationVerifier::clearExpiredCacheEntries($this->getSiteConfiguration(), $this->getDatabase());
22
-    }
16
+	/**
17
+	 * @return void
18
+	 */
19
+	public function execute()
20
+	{
21
+		IdentificationVerifier::clearExpiredCacheEntries($this->getSiteConfiguration(), $this->getDatabase());
22
+	}
23 23
 }
Please login to merge, or discard this patch.