FileReaderRequest::run()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
nc 1
nop 0
1
<?php
2
namespace PHPDaemon\Applications;
3
4
/**
5
 * Class FileReaderRequest
6
 * @package PHPDaemon\Applications
7
 */
8
class FileReaderRequest extends \PHPDaemon\HTTPRequest\Generic
9
{
10
    /**
11
     * @var
12
     */
13
    public $stream;
14
15
    /**
16
     * @var
17
     */
18
    public $job;
19
20
    /**
21
     * @var
22
     */
23
    public $indexFile;
24
25
    /**
26
     * Constructor.
27
     * @return void
28
     */
29
    public function init()
30
    {
31
        if (!isset($this->attrs->server['FR_PATH'])) {
32
            $this->status(404);
33
            $this->finish();
34
            return;
35
        }
36
        $job = new \PHPDaemon\Core\ComplexJob(function ($job) {
0 ignored issues
show
Unused Code introduced by
The parameter $job is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
            $this->wakeup();
38
        });
39
        $this->job = $job;
40
        $this->sleep(5, true);
41
        $this->attrs->server['FR_PATH'] = \PHPDaemon\FS\FileSystem::sanitizePath($this->attrs->server['FR_PATH']);
42
        $job('stat', function ($name, $job) {
43
            /** @var \PHPDaemon\Core\ComplexJob $job */
44
            \PHPDaemon\FS\FileSystem::stat($this->attrs->server['FR_PATH'], function ($path, $stat) use ($job) {
45
                if ($stat === -1) {
46
                    $this->fileNotFound();
47
                    $job->setResult('stat', false);
48
                    return;
49
                }
50
                if ($stat['type'] === 'd') {
51
                    if (!\PHPDaemon\FS\FileSystem::$supported) {
52
                        $this->file(rtrim($path, '/') . '/index.html');
53
                    } else {
54
                        $job('readdir', function ($name, $job) use ($path) {
55
                            /** @var \PHPDaemon\Core\ComplexJob $job */
56
                            \PHPDaemon\FS\FileSystem::readdir(rtrim($path, '/'), function ($path, $dir) use ($job) {
57
                                $found = false;
58
                                if (is_array($dir)) {
59
                                    foreach ($dir['dents'] as $file) {
60
                                        if ($file['type'] === \EIO_DT_REG) { // is file
61
                                            if (in_array($file['name'], $this->appInstance->indexFiles)) {
0 ignored issues
show
Bug introduced by
The property indexFiles does not seem to exist in PHPDaemon\Core\AppInstance.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
62
                                                $this->file($path . '/' . $file['name']);
63
                                                $found = true;
64
                                                break;
65
                                            }
66
                                        }
67
                                    }
68
                                }
69
                                if (!$found) {
70
                                    if (isset($this->attrs->server['FR_AUTOINDEX']) && $this->attrs->server['FR_AUTOINDEX']) {
71
                                        $this->autoindex($path, $dir);
72
                                    } else {
73
                                        $this->fileNotFound();
74
                                    }
75
                                }
76
77
                                $job->setResult('readdir');
78
                            }, \EIO_READDIR_STAT_ORDER | \EIO_READDIR_DENTS);
79
                        });
80
                    }
81
                } elseif ($stat['type'] === 'f') {
82
                    $this->file($path);
83
                }
84
                $job->setResult('stat', $stat);
85
            });
86
        });
87
        $job();
88
    }
89
90
    /**
91
     * Send header 404 or, if not possible already, response "File not found"
92
     */
93
    public function fileNotFound()
94
    {
95
        try {
96
            $this->header('404 Not Found');
97
            $this->header('Content-Type: text/html');
98
        } catch (\PHPDaemon\Request\RequestHeadersAlreadySent $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
99
        }
100
        $this->out('File not found.');
101
    }
102
103
    /**
104
     * @param $path
105
     */
106
    public function file($path)
107
    {
108
        if (!\PHPDaemon\FS\FileSystem::$supported) {
109
            $this->out(file_get_contents(realpath($path)));
110
            $this->wakeup();
111
            return;
112
        }
113
        $job = $this->job;
114
        $job('readfile', function ($name, $job) use ($path) {
115
            /** @var \PHPDaemon\Core\ComplexJob $job */
116
            $this->sendfile($path, function ($file, $success) use ($job, $name) {
0 ignored issues
show
Unused Code introduced by
The parameter $file is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $success is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
117
                $job->setResult($name);
118
            });
119
        });
120
    }
121
122
    /**
123
     * @param $path
124
     * @param $dir
125
     */
126
    public function autoindex($path, $dir)
127
    {
128
129
        $this->onWakeup();
130
        ?>
131
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
132
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
133
        <head>
134
            <title>Index of /</title>
135
            <style type="text/css">
136
                a, a:active {
137
                    text-decoration: none;
138
                    color: blue;
139
                }
140
141
                a:visited {
142
                    color: #48468F;
143
                }
144
145
                a:hover, a:focus {
146
                    text-decoration: underline;
147
                    color: red;
148
                }
149
150
                body {
151
                    background-color: #F5F5F5;
152
                }
153
154
                h2 {
155
                    margin-bottom: 12px;
156
                }
157
158
                table {
159
                    margin-left: 12px;
160
                }
161
162
                th, td {
163
                    font: 90% monospace;
164
                    text-align: left;
165
                }
166
167
                th {
168
                    font-weight: bold;
169
                    padding-right: 14px;
170
                    padding-bottom: 3px;
171
                }
172
173
                td {
174
                    padding-right: 14px;
175
                }
176
177
                td.s, th.s {
178
                    text-align: right;
179
                }
180
181
                div.list {
182
                    background-color: white;
183
                    border-top: 1px solid #646464;
184
                    border-bottom: 1px solid #646464;
185
                    padding-top: 10px;
186
                    padding-bottom: 14px;
187
                }
188
189
                div.foot {
190
                    font: 90% monospace;
191
                    color: #787878;
192
                    padding-top: 4px;
193
                }
194
            </style>
195
        </head>
196
        <body>
197
        <pre class="header">Welcome!</pre>
198
        <h2>Index of /</h2>
199
200
        <div class="list">
201
            <table summary="Directory Listing" cellpadding="0" cellspacing="0">
202
                <thead>
203
                <tr>
204
                    <th class="n">Name</th>
205
                    <th class="t">Type</th>
206
                </tr>
207
                </thead>
208
                <tbody>
209
                <tr>
210
                    <td class="n"><a href="../../">Parent Directory</a>/</td>
211
                    <td class="t">Directory</td>
212
213
                </tr>
214
                <?php
215
                foreach ($dir['dents'] as $item) {
216
                    $type = $item['type'] === EIO_DT_DIR ? 'Directory' : \PHPDaemon\Utils\MIME::get($path . $item['name']);
217
                    ?>
218
                    <tr>
219
                        <td class="n"><a
220
                                href="<?php echo htmlspecialchars($item['name']) . ($type == 'Directory' ? '/' : '');
221
                                ?>"><?php echo htmlspecialchars($item['name']);
222
                                ?></a></td>
223
                        <td class="t"><?php echo $type;
224
                            ?></td>
225
                    </tr>
226
                    <?php
227
                }
228
                ?>
229
                </tbody>
230
            </table>
231
        </div>
232
        <?php if ($this->upstream->pool->config->expose->value) : ?>
233
            <div class="foot">
234
                phpDaemon/<?php echo \PHPDaemon\Core\Daemon::$version; ?>
235
            </div>
236
        <?php endif; ?>
237
        </body>
238
        </html>
239
        <?php
240
    }
241
242
    /**
243
     * Called when the request aborted.
244
     */
245
    public function onAbort()
246
    {
247
        $this->finish();
248
    }
249
250
    /**
251
     * Called when request iterated.
252
     * @return integer Status.
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
253
     */
254
    public function run()
255
    {
256
    }
257
}
258