|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Cecil. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Cecil\Util; |
|
15
|
|
|
|
|
16
|
|
|
use Cecil\Exception\RuntimeException; |
|
17
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
18
|
|
|
|
|
19
|
|
|
class File |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var Filesystem */ |
|
22
|
|
|
protected static $fs; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Returns a Symfony\Component\Filesystem instance. |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function getFS(): Filesystem |
|
28
|
|
|
{ |
|
29
|
|
|
if (!self::$fs instanceof Filesystem) { |
|
30
|
|
|
self::$fs = new Filesystem(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return self::$fs; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* file_get_contents() function with error handler. |
|
38
|
|
|
* |
|
39
|
|
|
* @return string|false |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function fileGetContents(string $filename, bool $userAgent = false) |
|
42
|
|
|
{ |
|
43
|
|
|
if (empty($filename)) { |
|
44
|
|
|
return false; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
set_error_handler( |
|
48
|
|
|
function ($severity, $message, $file, $line) { |
|
49
|
|
|
throw new \ErrorException($message, 0, $severity, $file, $line, null); |
|
50
|
|
|
} |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
|
|
try { |
|
54
|
|
|
if ($userAgent) { |
|
55
|
|
|
$options = [ |
|
56
|
|
|
'http' => [ |
|
57
|
|
|
'method' => 'GET', |
|
58
|
|
|
'header' => 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.47 Safari/537.36', |
|
59
|
|
|
'follow_location' => true, |
|
60
|
|
|
], |
|
61
|
|
|
]; |
|
62
|
|
|
|
|
63
|
|
|
return file_get_contents($filename, false, stream_context_create($options)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return file_get_contents($filename); |
|
67
|
|
|
} catch (\ErrorException) { |
|
68
|
|
|
return false; |
|
69
|
|
|
} finally { |
|
70
|
|
|
restore_error_handler(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Returns the content type and subtype of a file. |
|
76
|
|
|
* |
|
77
|
|
|
* ie: ['text', 'text/plain'] |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function getMediaType(string $filename): array |
|
80
|
|
|
{ |
|
81
|
|
|
if (false === $subtype = mime_content_type($filename)) { |
|
82
|
|
|
throw new RuntimeException(\sprintf('Can\'t get content type of "%s".', $filename)); |
|
83
|
|
|
} |
|
84
|
|
|
$type = explode('/', $subtype)[0]; |
|
85
|
|
|
|
|
86
|
|
|
return [ |
|
87
|
|
|
$type, |
|
88
|
|
|
$subtype, |
|
89
|
|
|
]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* exif_read_data() function with error handler. |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function readExif(string $filename): array |
|
96
|
|
|
{ |
|
97
|
|
|
if (empty($filename)) { |
|
98
|
|
|
return []; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
set_error_handler( |
|
102
|
|
|
function ($severity, $message, $file, $line) { |
|
103
|
|
|
throw new \ErrorException($message, 0, $severity, $file, $line, null); |
|
104
|
|
|
} |
|
105
|
|
|
); |
|
106
|
|
|
|
|
107
|
|
|
try { |
|
108
|
|
|
if (!\function_exists('exif_read_data')) { |
|
109
|
|
|
throw new \ErrorException('`exif` extension is not available.'); |
|
110
|
|
|
} |
|
111
|
|
|
$exif = exif_read_data($filename, null, true); |
|
112
|
|
|
if ($exif === false) { |
|
113
|
|
|
return []; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $exif; |
|
117
|
|
|
} catch (\ErrorException) { |
|
118
|
|
|
return []; |
|
119
|
|
|
} finally { |
|
120
|
|
|
restore_error_handler(); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|