Passed
Push — phpunit8 ( f24111 )
by Sam
08:30
created

URLSpecialsMiddleware::buildImpactRedirect()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 17
rs 9.9332
1
<?php
2
3
namespace SilverStripe\Control\Middleware;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Control\Middleware\URLSpecialsMiddleware\FlushScheduler;
8
use SilverStripe\Control\Middleware\URLSpecialsMiddleware\SessionEnvTypeSwitcher;
9
use SilverStripe\Control\HTTPRequest;
10
use SilverStripe\Control\HTTPResponse;
11
use SilverStripe\Security\RandomGenerator;
12
13
/**
14
 * Check the request for the URL special variables.
15
 * Performs authorisation, confirmation and actions for some of those.
16
 *
17
 * WARNING: Bypasses only disable authorisation and confirmation, but not actions nor redirects
18
 *
19
 * The rules are:
20
 *  - flush GET parameter
21
 *  - isDev GET parameter
22
 *  - isTest GET parameter
23
 *  - dev/build URL
24
 *
25
 * @see https://docs.silverstripe.org/en/4/developer_guides/debugging/url_variable_tools/ special variables docs
26
 *
27
 * {@inheritdoc}
28
 */
29
class URLSpecialsMiddleware extends PermissionAwareConfirmationMiddleware
30
{
31
    use FlushScheduler;
32
    use SessionEnvTypeSwitcher;
33
34
    /**
35
     * Initializes the middleware with the required rules
36
     */
37
    public function __construct()
38
    {
39
        parent::__construct(
40
            new ConfirmationMiddleware\GetParameter("flush"),
41
            new ConfirmationMiddleware\GetParameter("isDev"),
42
            new ConfirmationMiddleware\GetParameter("isTest"),
43
            new ConfirmationMiddleware\UrlPathStartswith("dev/build")
44
        );
45
    }
46
47
    /**
48
     * Looks up for the special flags passed in the request
49
     * and schedules the changes accordingly for the next request.
50
     * Returns a redirect to the same page (with a random token) if
51
     * there are changes introduced by the flags.
52
     * Returns null if there is no impact introduced by the flags.
53
     *
54
     * @param HTTPRequest $request
55
     *
56
     * @return null|HTTPResponse redirect to the same url
57
     */
58
    public function buildImpactRedirect(HTTPRequest $request)
59
    {
60
        $flush = $this->scheduleFlush($request);
61
        $env_type = $this->setSessionEnvType($request);
62
63
        if ($flush || $env_type) {
64
            // the token only purpose is to invalidate browser/proxy cache
65
            $request['urlspecialstoken'] = bin2hex(random_bytes(4));
66
67
            $result = new HTTPResponse();
68
            $result->redirect(
69
                Controller::join_links(
70
                    Director::baseURL(),
71
                    $request->getURL(true)
72
                )
73
            );
74
            return $result;
75
        }
76
    }
77
78
    protected function confirmedEffect(HTTPRequest $request)
79
    {
80
        if ($response = $this->buildImpactRedirect($request)) {
81
            HTTPCacheControlMiddleware::singleton()->disableCache(true);
82
            return $response;
83
        }
84
    }
85
}
86