Issues (80)

code/AdvertController.php (20 issues)

1
<?php
2
    class Advert_Controller extends Controller
0 ignored issues
show
Line indented incorrectly; expected 0 spaces, found 4
Loading history...
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...
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
3
    {
4
        /**
5
         * @var int Size of output chunks in kb while in PHP fread mode.
6
         */
7
        protected static $chunck_size_kb = 32;
0 ignored issues
show
Line indented incorrectly; expected 4 spaces, found 8
Loading history...
8
9
        /**
10
         * @var bool Flag use X-Sendfile header mode instead of PHP fread mode.
11
         */
12
        protected static $use_x_sendfile = false;
0 ignored issues
show
Line indented incorrectly; expected 4 spaces, found 8
Loading history...
13
14
        /**
15
         * @var bool Flag use SilverStripe send file method.
16
         */
17
        protected static $use_ss_sendfile = false;
0 ignored issues
show
Line indented incorrectly; expected 4 spaces, found 8
Loading history...
18
19
        /*
20
        When a URL of the following form is clicked,
21
        http://SERVER/advert/26b0c28f4566dc54cc66770bdda86017851bd3dbbd4ac5868c63e003903dc8dc9b88b209bba54f1b5e6f4c3cf1295e58298e7d5a58b6c8ff02df9130ffd4ac30
22
        a search is made for the digital signature in the adverts (this is indexed in the database)
23
24
        If a record is found it's link is obtained and a redirection made, firstly recording the clickthrough in the database.
25
26
        If a record is not found a 404 is returned
27
        */
28
        public function click($args)
0 ignored issues
show
Line indented incorrectly; expected 4 spaces, found 8
Loading history...
29
        {
30
            $params = $args->allParams();
31
            $digsig = $params['DigitalSignature'];
32
33
            $advert = Advert::get()->filter('DigitalSignature', $digsig)->first(); // should only be the one but make sure
34
35
            if (!$advert) {
0 ignored issues
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
36
                $this->httpError(404, 'Advert "'.$digsig.'"" not found');
37
            } else {
38
                // record the click
39
                $advert->Clickthroughs = $advert->Clickthroughs + 1;
40
                $advert->write();
41
                // documentation here states temporary directs are used, http://doc.silverstripe.org/framework/en/topics/controller
42
                // this means the browser wont store the redirect and thus bypass the clickthrough recording
43
                return $this->redirect($advert->WebsiteLink);
44
            }
45
        }
46
47
        public function image($args)
48
        {
49
            $params = $args->allParams();
50
            $digsig = $params['DigitalSignature'];
51
52
            $advert = Advert::get()->filter('DigitalSignature', $digsig)->first(); // should only be the one but make sure
53
            if (!$advert) {
54
                $this->httpError(404, 'Advert "'.$digsig.'"" not found');
55
            } else {
56
                // record the click
57
                $advert->Impressions = $advert->Impressions + 1;
58
                $advert->write();
59
                // documentation here states temporary directs are used, http://doc.silverstripe.org/framework/en/topics/controller
60
                // this means the browser wont store the redirect and thus bypass the clickthrough recording
61
62
                return $this->fileFound($advert->AdvertImage());
63
            }
64
        }
65
66
    /**
67
     * Use X-Sendfile headers to send files to the browser.
68
     * This is quicker than pushing files through PHP but
69
     * requires either Lighttpd or mod_xsendfile for Apache.
70
     *
71
     * @link http://tn123.ath.cx/mod_xsendfile/
72
     */
73
    public static function use_x_sendfile_method()
0 ignored issues
show
Method name "Advert_Controller::use_x_sendfile_method" is not in camel caps format
Loading history...
Line indented incorrectly; expected 8 spaces, found 4
Loading history...
74
    {
0 ignored issues
show
It seems like the identation of this line is off (expected at least 8 spaces, but found 4).
Loading history...
75
        self::use_default_sendfile_method();
0 ignored issues
show
It seems like the identation of this line is off (expected at least 12 spaces, but found 8).
Loading history...
76
        self::$use_x_sendfile = true;
0 ignored issues
show
It seems like the identation of this line is off (expected at least 12 spaces, but found 8).
Loading history...
77
    }
78
79
    /**
80
     * Use internal SilverStripe to send files to the browser.
81
     * This is the least efficient method but is useful for
82
     * testing. Not recommend for production
83
     * environments.
84
     */
85
    public static function use_ss_sendfile_method()
0 ignored issues
show
Method name "Advert_Controller::use_ss_sendfile_method" is not in camel caps format
Loading history...
86
    {
87
        self::use_default_sendfile_method();
88
        self::$use_ss_sendfile = true;
89
    }
90
91
    /**
92
     * Use the default chuncked file method to send files to the browser.
93
     * This is the default method.
94
     */
95
    public static function use_default_sendfile_method()
0 ignored issues
show
Method name "Advert_Controller::use_default_sendfile_method" is not in camel caps format
Loading history...
96
    {
97
        self::$use_ss_sendfile = false;
98
        self::$use_x_sendfile = false;
99
    }
100
101
    /**
102
     * Set the size of upload chunk in bytes.
103
     *
104
     * @param int $kilobytes
105
     */
106
    public static function set_chunk_size($kilobytes)
0 ignored issues
show
Method name "Advert_Controller::set_chunk_size" is not in camel caps format
Loading history...
107
    {
108
        $kilobytes = max(0, (int) $kilobytes);
109
        if (!$kilobytes) {
110
            user_error('Invalid download chunk size', E_USER_ERROR);
111
        }
112
        self::$chunck_size_kb = $kilobytes;
113
    }
114
115
    /**
116
     * Set the Apache access file name (.htaccess by default)
117
     * as determined by the AccessFileName Apache directive.
118
     *
119
     * @param string $filename
120
     */
121
    public static function set_access_filename($filename)
0 ignored issues
show
Method name "Advert_Controller::set_access_filename" is not in camel caps format
Loading history...
122
    {
123
        self::$htaccess_file = $filename;
124
    }
125
126
    /**
127
     * Get the Apache access file name.
128
     *
129
     * @return string
130
     */
131
    public static function get_access_filename()
0 ignored issues
show
Method name "Advert_Controller::get_access_filename" is not in camel caps format
Loading history...
132
    {
133
        return self::$htaccess_file;
134
    }
135
136
    /**
137
     * File found response.
138
     *
139
     * @param $file File to send
140
     * @param $alternate_path string If supplied, return the file from this path instead, for
141
     * example, resampled images.
142
     */
143
    public function fileFound(File $file, $alternate_path = null)
144
    {
145
146
        // File properties
147
        $file_name = $file->Name;
148
        $file_path = Director::getAbsFile($alternate_path ? $alternate_path : $file->FullPath);
149
        $file_size = filesize($file_path);
150
151
        // Testing mode - return an HTTPResponse
152
        if (self::$use_ss_sendfile) {
153
            if (ClassInfo::exists('SS_HTTPRequest')) {
154
                return SS_HTTPRequest::send_file(file_get_contents($file_path), $file_name);
155
            } else {
156
                return HTTPRequest::send_file(file_get_contents($file_path), $file_name);
157
            }
158
        }
159
160
        // Normal operation:
161
        $mimeType = HTTP::get_mime_type($file_name);
162
        header("Content-Type: {$mimeType}; name=\"".addslashes($file_name).'"');
163
        header('Content-Disposition: attachment; filename='.addslashes($file_name));
164
        header('Cache-Control: max-age=1, private');
165
        header("Content-Length: {$file_size}");
166
        header('Pragma: ');
167
168
        if (self::$use_x_sendfile) {
169
            session_write_close();
170
            header('X-Sendfile: '.$file_path);
171
            exit();
172
        } elseif ($filePointer = @fopen($file_path, 'rb')) {
173
            session_write_close();
174
            $this->flush();
175
            // Push the file while not EOF and connection exists
176
            while (!feof($filePointer) && !connection_aborted()) {
177
                echo fread($filePointer, 1024 * self::$chunck_size_kb);
178
                $this->flush();
179
            }
180
            fclose($filePointer);
181
            exit();
182
        } else {
183
            // Edge case - either not found anymore or can't read
184
            return $this->fileNotFound();
185
        }
186
    }
187
188
    /**
189
     * Flush the output buffer to the server (if possible).
190
     *
191
     * @see http://nz.php.net/manual/en/function.flush.php#93531
192
     */
193
    public function flush()
194
    {
195
        if (ob_get_length()) {
196
            @ob_flush();
197
            @flush();
198
            @ob_end_flush();
199
        }
200
        @ob_start();
201
    }
202
}
0 ignored issues
show
Line indented incorrectly; expected 4 spaces, found 0
Loading history...
Closing brace indented incorrectly; expected 4 spaces, found 0
Loading history...
203