Issues (45)

src/Helpers/File.php (2 issues)

1
<?php
2
/**
3
 *  This file is part of the Simple S3 package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 */
11
12
namespace Matecat\SimpleS3\Helpers;
13
14
use RecursiveDirectoryIterator;
15
use RecursiveIteratorIterator;
16
17
class File
18
{
19
    /**
20
     * @param string $path
21
     *
22
     * @return bool
23
     */
24 1
    public static function checkIfIsADir($path)
25
    {
26 1
        if (strpos($path, DIRECTORY_SEPARATOR) !== false) {
27 1
            return true;
28
        }
29
30 1
        return false;
31
    }
32
33
    /**
34
     * @param string $string
35
     *
36
     * @return bool
37
     */
38 3
    public static function endsWith($string, $separator)
39
    {
40 3
        return substr($string, -1) === $separator;
41
    }
42
43
    /**
44
     * @param string $path
45
     *
46
     * @return string
47
     */
48 1
    public static function getBaseName($path)
49
    {
50 1
        if (false == self::checkIfIsADir($path)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
51 1
            return $path;
52
        }
53
54 1
        return self::getPathInfo($path)['basename'];
55
    }
56
57
    /**
58
     * @param string $filename
59
     *
60
     * @return string|null
61
     */
62
    public static function getExtension($filename)
63
    {
64
        $filenameArray = explode('.', $filename);
65
        $filenameArray = array_pop($filenameArray);
66
67
        if (null !== $filenameArray) {
68
            return strtolower($filenameArray);
69
        }
70
    }
71
72
    /**
73
     * @param string $filename
74
     * @param int $mode
75
     *
76
     * @return mixed|string
77
     */
78
    public static function getMimeType($filename, $mode = 0)
79
    {
80
        // mode 0 = full check
81
        // mode 1 = extension check only
82
83
        $mimetype = '';
84
85
        $mime_types = [
86
            'txt' => 'text/plain',
87
            'htm' => 'text/html',
88
            'html' => 'text/html',
89
            'php' => 'text/html',
90
            'css' => 'text/css',
91
            'js' => 'application/javascript',
92
            'json' => 'application/json',
93
            'xml' => 'application/xml',
94
            'swf' => 'application/x-shockwave-flash',
95
            'flv' => 'video/x-flv',
96
97
            // images
98
            'png' => 'image/png',
99
            'jpe' => 'image/jpeg',
100
            'jpeg' => 'image/jpeg',
101
            'jpg' => 'image/jpeg',
102
            'gif' => 'image/gif',
103
            'bmp' => 'image/bmp',
104
            'ico' => 'image/vnd.microsoft.icon',
105
            'tiff' => 'image/tiff',
106
            'tif' => 'image/tiff',
107
            'svg' => 'image/svg+xml',
108
            'svgz' => 'image/svg+xml',
109
110
            // archives
111
            'zip' => 'application/zip',
112
            'rar' => 'application/x-rar-compressed',
113
            'exe' => 'application/x-msdownload',
114
            'msi' => 'application/x-msdownload',
115
            'cab' => 'application/vnd.ms-cab-compressed',
116
117
            // audio/video
118
            'mp3' => 'audio/mpeg',
119
            'qt' => 'video/quicktime',
120
            'mov' => 'video/quicktime',
121
122
            // adobe
123
            'pdf' => 'application/pdf',
124
            'psd' => 'image/vnd.adobe.photoshop',
125
            'ai' => 'application/postscript',
126
            'eps' => 'application/postscript',
127
            'ps' => 'application/postscript',
128
129
            // ms office
130
            'doc' => 'application/msword',
131
            'rtf' => 'application/rtf',
132
            'xls' => 'application/vnd.ms-excel',
133
            'ppt' => 'application/vnd.ms-powerpoint',
134
            'docx' => 'application/msword',
135
            'xlsx' => 'application/vnd.ms-excel',
136
            'pptx' => 'application/vnd.ms-powerpoint',
137
138
139
            // open office
140
            'odt' => 'application/vnd.oasis.opendocument.text',
141
            'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
142
        ];
143
144
        if (function_exists('mime_content_type') and $mode === 0) {
145
            $mimetype = mime_content_type($filename);
146
147
            return $mimetype;
148
        }
149
150
        if (function_exists('finfo_open') and $mode === 0) {
151
            $finfo = finfo_open(FILEINFO_MIME);
152
153
            if (false !== $finfo) {
154
                $mimetype = finfo_file($finfo, $filename);
155
                finfo_close($finfo);
156
            }
157
158
            return $mimetype;
159
        }
160
161
        $ext = self::getExtension($filename);
162
163
        if (null !== $ext and array_key_exists($ext, $mime_types)) {
164
            return $mime_types[$ext];
165
        }
166
167
        return 'application/octet-stream';
168
    }
169
170
    /**
171
     * @param string $path
172
     *
173
     * @return array
174
     */
175 5
    public static function getPathInfo($path)
176
    {
177 5
        return pathinfo($path);
0 ignored issues
show
Bug Best Practice introduced by
The expression return pathinfo($path) also could return the type string which is incompatible with the documented return type array.
Loading history...
178
    }
179
180
    /**
181
     * @param string $filename
182
     *
183
     * @return false|int
184
     */
185
    public static function getSize($filename)
186
    {
187
        return filesize($filename);
188
    }
189
190
    /**
191
     * @param string $url
192
     *
193
     * @return bool|string
194
     */
195 3
    public static function loadFile($url, $sslVerify = true)
196
    {
197 3
        if (function_exists('curl_version')) {
198 3
            $ch = curl_init();
199
200 3
            $verifyPeer = (true == $sslVerify) ? 1 : 0;
201 3
            $verifyHost = (true == $sslVerify) ? 2 : 0;
202
203 3
            curl_setopt($ch, CURLOPT_HEADER, 0);
204 3
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
205 3
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $verifyHost);
206 3
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verifyPeer);
207 3
            curl_setopt($ch, CURLOPT_URL, $url);
208
209 3
            $data = curl_exec($ch);
210 3
            curl_close($ch);
211
212 3
            return $data;
213
        }
214
215
        $context = stream_context_create([
216
            'ssl' => [
217
                'verify_peer' => (($sslVerify)) ? $sslVerify : true,
218
                'verify_peer_name' => (($sslVerify)) ? $sslVerify : true,
219
            ]
220
        ]);
221
222
        return file_get_contents($url, false, $context);
223
    }
224
225
    /**
226
     * @param string $filename
227
     * @param bool $sslVerify
228
     *
229
     * @return bool|resource
230
     */
231
    public static function open($filename, $sslVerify = true)
232
    {
233
        $context = stream_context_create([
234
           'ssl' => [
235
                'verify_peer' => (($sslVerify)) ? $sslVerify : true,
236
                'verify_peer_name' => (($sslVerify)) ? $sslVerify : true,
237
           ]
238
        ]);
239
240
        return fopen($filename, 'r', false, $context);
241
    }
242
243
    /**
244
     * @param string $dir
245
     */
246
    public static function removeDir($dir)
247
    {
248
        $files = new RecursiveIteratorIterator(
249
            new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
250
            RecursiveIteratorIterator::CHILD_FIRST
251
        );
252
253
        foreach ($files as $fileinfo) {
254
            $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
255
            $todo($fileinfo->getRealPath());
256
        }
257
258
        rmdir($dir);
259
    }
260
}
261