CDNRewriteRequestFilter::preRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
class CDNRewriteRequestFilter implements RequestFilter
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
6
    /**
7
     * Enable rewriting of asset urls
8
     * @var bool
9
     */
10
    private static $cdn_rewrite = false;
0 ignored issues
show
Unused Code introduced by
The property $cdn_rewrite is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
11
12
    /**
13
     * The cdn domain incl. protocol
14
     * @var string
15
     */
16
    private static $cdn_domain = 'http://cdn.mysite.com';
0 ignored issues
show
Unused Code introduced by
The property $cdn_domain is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
17
18
    /**
19
     * Enable rewrite in admin area
20
     * @var bool
21
     */
22
    private static $enable_in_backend = false;
0 ignored issues
show
Unused Code introduced by
The property $enable_in_backend is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
23
24
    /**
25
     * Enable rewrite in dev mode
26
     * @var bool
27
     */
28
    private static $enable_in_dev = false;
0 ignored issues
show
Unused Code introduced by
The property $enable_in_dev is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
29
30
31
    /**
32
     * should assets be rewritten?
33
     * @var bool
34
     */
35
    private static $rewrite_assets = true;
0 ignored issues
show
Unused Code introduced by
The property $rewrite_assets is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
36
37
    /**
38
     * should themes also be rewritten?
39
     * @var bool
40
     */
41
    private static $rewrite_themes = false;
0 ignored issues
show
Unused Code introduced by
The property $rewrite_themes is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
42
43
    /**
44
     * Filter executed before a request processes
45
     *
46
     * @param SS_HTTPRequest $request Request container object
47
     * @param Session $session Request session
48
     * @param DataModel $model Current DataModel
49
     * @return boolean Whether to continue processing other filters. Null or true will continue processing (optional)
50
     */
51
    public function preRequest(SS_HTTPRequest $request, Session $session, DataModel $model)
52
    {
53
        return true;
54
    }
55
56
    /**
57
     * Filter executed AFTER a request
58
     *
59
     * @param SS_HTTPRequest $request Request container object
60
     * @param SS_HTTPResponse $response Response output object
61
     * @param DataModel $model Current DataModel
62
     * @return boolean Whether to continue processing other filters. Null or true will continue processing (optional)
63
     */
64
    public function postRequest(SS_HTTPRequest $request, SS_HTTPResponse $response, DataModel $model)
65
    {
66
        if (!self::isEnabled()) {
67
            return true;
68
        }
69
70
        $body = $response->getBody();
71
        $response->setBody(self::replaceCDN($body));
72
73
        return true;
74
    }
75
76
    /**
77
     * Checks if cdn rewrite is enabled
78
     * @return bool
79
     */
80
    public static function isEnabled()
81
    {
82
        $general = Config::inst()->get('CDNRewriteRequestFilter', 'cdn_rewrite');
83
        $notDev = !Director::isDev() || Config::inst()->get('CDNRewriteRequestFilter', 'enable_in_dev');
84
        $notBackend = !self::isBackend() ||  Config::inst()->get('CDNRewriteRequestFilter', 'enable_in_backend');
85
86
        return $general && $notDev && $notBackend;
87
    }
88
89
    /**
90
     * Helper method to check if we're in backend (LeftAndMain) or frontend
91
     * Controller::curr() doesn't return anything, so i cannot check it...
92
     * @return bool
93
     */
94
    public static function isBackend()
0 ignored issues
show
Coding Style introduced by
isBackend 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...
95
    {
96
        $url = array_key_exists('url', $_GET) ? $_GET['url'] : '';
97
        return !Config::inst()->get('SSViewer', 'theme_enabled') || strpos($url, 'admin') === 1;
98
    }
99
100
    /**
101
     * replaces links to assets in src and href attributes to point to a given cdn domain
102
     *
103
     * @param $body
104
     * @return mixed|void
105
     */
106
    public static function replaceCDN($body)
107
    {
108
        $cdn = Config::inst()->get('CDNRewriteRequestFilter', 'cdn_domain');
109
110
        if (Config::inst()->get('CDNRewriteRequestFilter', 'rewrite_assets')) {
111
            $body = str_replace('src="assets/', 'src="' . $cdn . '/assets/', $body);
112
            $body = str_replace('src="/assets/', 'src="' . $cdn . '/assets/', $body);
113
            $body = str_replace('src=\"/assets/', 'src=\"' . $cdn . '/assets/', $body);
114
115
            $body = str_replace('href="/assets/', 'href="' . $cdn . '/assets/', $body);
116
            $body = str_replace(Director::absoluteBaseURL() . 'assets/', $cdn . '/assets/', $body);
117
        }
118
119
        if (Config::inst()->get('CDNRewriteRequestFilter', 'rewrite_themes')) {
120
            $body = str_replace('src="/themes/', 'src="' . $cdn . '/themes/', $body);
121
            $body = str_replace('src="' . Director::absoluteBaseURL() . 'themes/', 'src="' . $cdn . '/themes/', $body);
122
123
            $body = str_replace('href="/themes/', 'href="' . $cdn . '/themes/', $body);
124
            $body = str_replace('href="' . Director::absoluteBaseURL() . 'themes/', 'href="' . $cdn . '/themes/', $body);
125
        }
126
127
        return $body;
128
    }
129
}
130