forked from h5p/h5p-editor-php-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh5peditor-file.class.php
200 lines (166 loc) · 5.5 KB
/
h5peditor-file.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
class H5peditorFile {
private $result, $field, $files_directory, $interface;
public $type, $name, $path, $mime, $size;
function __construct($interface, $files_directory) {
$field = filter_input(INPUT_POST, 'field', FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
// Check for file upload.
if ($field === NULL || empty($_FILES) || !isset($_FILES['file'])) {
return;
}
$this->interface = $interface;
// Create a new result object.
$this->result = new stdClass();
// Set directory.
$this->files_directory = $files_directory;
// Create the temporary directory if it doesn't exist.
$dirs = array ('', '/files', '/images', '/videos', '/audios');
foreach ($dirs as $dir) {
$dir = $this->files_directory . $dir;
if (!is_dir($dir)) {
if (!mkdir($dir)) {
// TODO: Move all t-s out of here.
$this->result->error = $this->interface->t('Unable to create directory.');
return;
}
}
}
// Get the field.
$this->field = json_decode($field);
if (function_exists('finfo_file')) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$this->type = finfo_file($finfo, $_FILES['file']['tmp_name']);
finfo_close($finfo);
}
elseif (function_exists('mime_content_type')) {
// Deprecated, only when finfo isn't available.
$this->type = mime_content_type($_FILES['file']['tmp_name']);
}
else {
$this->type = $_FILES['file']['type'];
}
$this->extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
$this->size = $_FILES['file']['size'];
}
/**
*
* @return boolean
*/
public function isLoaded() {
return is_object($this->result);
}
/**
* Check current file up agains mime types and extensions in the given list.
*
* @param array $mimes List to check against.
* @return boolean
*/
public function check($mimes) {
foreach ($mimes as $mime => $extension) {
// TODO: Either remove everything that has to do with mime types, or make it work
// Currently we're experiencing trouble with mime types on different servers...
if (/*$this->type === $mime && */strtolower($this->extension) === $extension) {
$this->type = $mime;
return TRUE;
}
}
return FALSE;
}
/**
* Validate the file.
*
* @return boolean
*/
public function validate() {
if (isset($this->result->error)) {
return FALSE;
}
// Check for field type.
if (!isset($this->field->type)) {
$this->result->error = $this->interface->t('Unable to get field type.');
return FALSE;
}
// Check if mime type is allowed.
if ((isset($this->field->mimes) && !in_array($this->type, $this->field->mimes)) || substr($this->extension, 0, 3) === 'php') {
$this->result->error = $this->interface->t("File type isn't allowed.");
return FALSE;
}
// Type specific validations.
switch ($this->field->type) {
default:
$this->result->error = $this->interface->t('Invalid field type.');
return FALSE;
case 'image':
$allowed = array(
'image/png' => 'png',
'image/jpeg' => 'jpg',
'image/gif' => 'gif',
);
if (!$this->check($allowed)) {
$this->result->error = $this->interface->t('Invalid image file format. Use jpg, png or gif.');
return FALSE;
}
$image = @getimagesize($_FILES['file']['tmp_name']);
if (!$image) {
$this->result->error = $this->interface->t('File is not an image.');
return FALSE;
}
$this->result->width = $image[0];
$this->result->height = $image[1];
$this->result->mime = $this->type;
break;
case 'audio':
$allowed = array(
'audio/mpeg' => 'mp3',
'audio/mp3' => 'mp3',
'audio/x-wav' => 'wav',
'audio/wav' => 'wav',
//'application/ogg' => 'ogg',
'audio/ogg' => 'ogg',
//'video/ogg' => 'ogg',
);
if (!$this->check($allowed)) {
$this->result->error = $this->interface->t('Invalid audio file format. Use mp3 or wav.');
return FALSE;
}
$this->result->mime = $this->type;
break;
case 'video':
$allowed = array(
'video/mp4' => 'mp4',
'video/webm' => 'webm',
// 'application/ogg' => 'ogv',
'video/ogg' => 'ogv',
);
if (!$this->check($allowed)) {
$this->result->error = $this->interface->t('Invalid video file format. Use mp4 or webm.');
return FALSE;
}
$this->result->mime = $this->type;
break;
case 'file':
// TODO: Try to get file extension for type and check that it matches the current extension.
$this->result->mime = $this->type;
}
return TRUE;
}
public function copy() {
$matches = array();
preg_match('/([a-z0-9]{1,})$/i', $_FILES['file']['name'], $matches);
$this->name = uniqid($this->field->name . '-');
if (isset($matches[0])) {
$this->name .= '.' . $matches[0];
}
$this->name = $this->field->type . 's/' . $this->name;
$this->path = $this->files_directory . '/' . $this->name;
if (!copy($_FILES['file']['tmp_name'], $this->path)) {
$this->result->error = $this->interface->t('Could not copy file.');
return FALSE;
}
$this->result->path = $this->name;
return TRUE;
}
public function getResult() {
return json_encode($this->result);
}
}