forked from Imagick/imagick
-
Notifications
You must be signed in to change notification settings - Fork 1
/
imagick_file.c
369 lines (309 loc) Β· 10.4 KB
/
imagick_file.c
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
+----------------------------------------------------------------------+
| PHP Version 5 / Imagick |
+----------------------------------------------------------------------+
| Copyright (c) 2006-2013 Mikko Koppanen, Scott MacVicar |
| ImageMagick (c) ImageMagick Studio LLC |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Mikko Kopppanen <mkoppanen@php.net> |
| Scott MacVicar <scottmac@php.net> |
+----------------------------------------------------------------------+
*/
#include "php_imagick.h"
#include "php_imagick_file.h"
#include "php_imagick_macros.h"
#include "php_imagick_defs.h"
#if ZEND_MODULE_API_NO > 20060613
# define IMAGICK_INIT_ERROR_HANDLING zend_error_handling error_handling
# define IMAGICK_SET_ERROR_HANDLING_THROW zend_replace_error_handling(EH_THROW, php_imagick_exception_class_entry, &error_handling TSRMLS_CC)
# define IMAGICK_RESTORE_ERROR_HANDLING zend_restore_error_handling(&error_handling TSRMLS_CC)
#else
# define IMAGICK_INIT_ERROR_HANDLING
# define IMAGICK_SET_ERROR_HANDLING_THROW php_set_error_handling(EH_THROW, php_imagick_exception_class_entry TSRMLS_CC)
# define IMAGICK_RESTORE_ERROR_HANDLING php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC)
#endif
#ifndef S_ISDIR
# define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR)
#endif
static
zend_bool php_imagick_is_virtual_format(const char *format)
{
int i, elements;
const char *virtual_fmt[] = {
"CANVAS",
"CAPTION",
"CLIPBOARD",
"FRACTAL",
"GRADIENT",
"GRANITE",
"HALD", // Identity Hald CLUT Image Select levels like this: hald:[8] for level 8.
"INLINE", // Base64-encoded inline image
"LABEL",
"LOGO",
"MAGICK",
"MAP", // Colormap intensities and indices Set -depth to set the sample size of the intensities; indices are 16-bit if colors > 256.
"MASK", // Image mask
"MATTE",
"NETSCAPE",
"NULL",
"PANGO", // Image caption
"PLASMA",
"PRINT",
"RADIAL-GRADIENT",
"RADIAL_GRADIENT",
"ROSE"
"SCAN",
"SCANX",
"TILE", // Tiled image
"UNIQUE",
"WIN",
#ifndef PHP_WIN32
"X",
#endif
"XC",
};
elements = sizeof (virtual_fmt) / sizeof (virtual_fmt [0]);
for (i = 0; i < elements; i++) {
if (strcasecmp(format, virtual_fmt[i]) == 0) {
return 1;
}
}
return 0;
}
static
zend_bool php_imagick_is_url(const char *filename TSRMLS_DC)
{
const char *path_for_open;
if (php_stream_locate_url_wrapper(filename, &path_for_open, STREAM_LOCATE_WRAPPERS_ONLY TSRMLS_CC)) {
return 1;
}
return 0;
}
zend_bool php_imagick_file_init(struct php_imagick_file_t *file, const char *filename, size_t filename_len TSRMLS_DC)
{
char magick_path[MaxTextExtent], head_path[MaxTextExtent], tail_path[MaxTextExtent], buffer[MaxTextExtent];
if (!filename_len) {
return 0;
}
/* Undefined for now */
file->type = ImagickUndefinedType;
if (filename_len >= MaxTextExtent) {
return 0;
}
/* Take a copy of the original string */
strlcpy(file->filename, filename, MaxTextExtent);
file->filename_len = filename_len;
/* Break the path into pieces */
memset(magick_path, 0, MaxTextExtent);
GetPathComponent(file->filename, MagickPath, magick_path);
/* The path has a format identifier, check for url and virtual format */
if (strlen(magick_path) > 0) {
/* Virtual format? */
if (php_imagick_is_virtual_format(magick_path)) {
file->type = ImagickVirtualFormat;
file->absolute_path = estrdup("");
return 1;
}
/* Is it an url? */
else if (php_imagick_is_url(filename TSRMLS_CC)) {
file->type = ImagickUri;
file->absolute_path = estrdup("");
return 1;
}
}
/* This is a normal file path */
file->type = ImagickFile;
memset(head_path, 0, MaxTextExtent);
memset(tail_path, 0, MaxTextExtent);
GetPathComponent(file->filename, HeadPath, head_path);
GetPathComponent(file->filename, TailPath, tail_path);
(void) snprintf(buffer, MaxTextExtent, "%s/%s", head_path, tail_path);
/* The full path to the file */
file->absolute_path = expand_filepath(buffer, NULL TSRMLS_CC);
/* Failed to resolve absolute path */
if (!file->absolute_path) {
file->absolute_path = estrdup("");
}
return 1;
}
void php_imagick_file_deinit(struct php_imagick_file_t *file)
{
if (file->absolute_path) {
efree(file->absolute_path);
file->absolute_path = NULL;
}
}
static
int php_imagick_read_image_using_imagemagick(php_imagick_object *intern, struct php_imagick_file_t *file, ImagickOperationType type TSRMLS_DC)
{
#if PHP_VERSION_ID < 70000
#if PHP_VERSION_ID >= 50600
#ifdef ZTS
// This suppresses an 'unused parameter' warning.
(void)tsrm_ls;
#endif
#endif
#endif
if (type == ImagickReadImage) {
if (MagickReadImage(intern->magick_wand, file->filename) == MagickFalse) {
#if PHP_VERSION_ID >= 70000
zend_stat_t st;
#else
struct stat st;
#endif
/* Resolved to a filename. Check that it's not a dir */
if (php_sys_stat(file->absolute_path, &st) == 0 && S_ISDIR(st.st_mode)) {
return IMAGICK_RW_PATH_IS_DIR;
}
return IMAGICK_RW_UNDERLYING_LIBRARY;
}
} else if (type == ImagickPingImage){
if (MagickPingImage(intern->magick_wand, file->filename) == MagickFalse) {
return IMAGICK_RW_UNDERLYING_LIBRARY;
}
} else {
return IMAGICK_RW_UNDERLYING_LIBRARY;
}
MagickSetImageFilename(intern->magick_wand, file->absolute_path);
MagickSetLastIterator(intern->magick_wand);
return IMAGICK_RW_OK;
}
static
int php_imagick_read_image_using_php_streams(php_imagick_object *intern, struct php_imagick_file_t *file, ImagickOperationType type TSRMLS_DC)
{
php_stream *stream;
MagickBooleanType status;
FILE *fp;
IMAGICK_INIT_ERROR_HANDLING;
IMAGICK_SET_ERROR_HANDLING_THROW;
#if PHP_VERSION_ID >= 70000
stream = php_stream_open_wrapper(file->filename, "rb", (IGNORE_PATH) & ~REPORT_ERRORS, NULL);
#else
stream = php_stream_open_wrapper(file->filename, "rb", (ENFORCE_SAFE_MODE|IGNORE_PATH) & ~REPORT_ERRORS, NULL);
#endif
if (!stream) {
IMAGICK_RESTORE_ERROR_HANDLING;
return IMAGICK_RW_UNDERLYING_LIBRARY;
}
if (php_stream_can_cast(stream, PHP_STREAM_AS_STDIO|PHP_STREAM_CAST_INTERNAL) == FAILURE ||
php_stream_cast(stream, PHP_STREAM_AS_STDIO|PHP_STREAM_CAST_INTERNAL, (void*)&fp, 0) == FAILURE) {
php_stream_close(stream);
IMAGICK_RESTORE_ERROR_HANDLING;
return IMAGICK_RW_UNDERLYING_LIBRARY;
}
IMAGICK_RESTORE_ERROR_HANDLING;
if (type == ImagickReadImage) {
status = MagickReadImageFile(intern->magick_wand, fp);
} else if (type == ImagickPingImage){
status = MagickPingImageFile(intern->magick_wand, fp);
} else {
php_stream_close(stream);
return IMAGICK_RW_UNDERLYING_LIBRARY;
}
if (status == MagickFalse) {
php_stream_close(stream);
return IMAGICK_RW_UNDERLYING_LIBRARY;
}
MagickSetImageFilename(intern->magick_wand, file->absolute_path);
php_stream_close(stream);
if (status == MagickFalse) {
return IMAGICK_RW_UNDERLYING_LIBRARY;
}
MagickSetLastIterator(intern->magick_wand);
return IMAGICK_RW_OK;
}
int php_imagick_safe_mode_check(const char *filename TSRMLS_DC)
{
#if defined(CHECKUID_CHECK_FILE_AND_DIR)
if (PG(safe_mode) && (!php_checkuid_ex(filename, NULL, CHECKUID_CHECK_FILE_AND_DIR, CHECKUID_NO_ERRORS))) {
return IMAGICK_RW_SAFE_MODE_ERROR;
}
#endif
if (PG(open_basedir) && php_check_open_basedir_ex(filename, 0 TSRMLS_CC)) {
return IMAGICK_RW_OPEN_BASEDIR_ERROR;
}
return IMAGICK_RW_OK;
}
php_imagick_rw_result_t php_imagick_read_file(php_imagick_object *intern, struct php_imagick_file_t *file, ImagickOperationType type TSRMLS_DC)
{
php_imagick_rw_result_t rc;
if (file->type == ImagickFile) {
rc = php_imagick_safe_mode_check(file->absolute_path TSRMLS_CC);
if (rc != IMAGICK_RW_OK) {
return rc;
}
}
if (file->type == ImagickUri) {
return php_imagick_read_image_using_php_streams(intern, file, type TSRMLS_CC);
} else {
return php_imagick_read_image_using_imagemagick(intern, file, type TSRMLS_CC);
}
}
php_imagick_rw_result_t php_imagick_write_file(php_imagick_object *intern, struct php_imagick_file_t *file, ImagickOperationType type, zend_bool adjoin TSRMLS_DC)
{
php_imagick_rw_result_t rc;
MagickBooleanType status = MagickFalse;
if (file->type == ImagickFile) {
rc = php_imagick_safe_mode_check(file->absolute_path TSRMLS_CC);
if (rc != IMAGICK_RW_OK) {
return rc;
}
}
if (type == ImagickWriteImage) {
status = MagickWriteImage(intern->magick_wand, file->filename);
} else if (type == ImagickWriteImages) {
status = MagickWriteImages(intern->magick_wand, file->filename, adjoin);
}
/* Write succeded ? */
if (status == MagickFalse) {
return IMAGICK_RW_UNDERLYING_LIBRARY;
}
/* All went well it seems */
return IMAGICK_RW_OK;
}
zend_bool php_imagick_stream_handler(php_imagick_object *intern, php_stream *stream, ImagickOperationType type TSRMLS_DC)
{
FILE *fp;
MagickBooleanType status = MagickFalse;
IMAGICK_INIT_ERROR_HANDLING;
IMAGICK_SET_ERROR_HANDLING_THROW;
if (php_stream_can_cast(stream, PHP_STREAM_AS_STDIO | PHP_STREAM_CAST_INTERNAL) == FAILURE ||
php_stream_cast(stream, PHP_STREAM_AS_STDIO | PHP_STREAM_CAST_INTERNAL, (void **)&fp, 0) == FAILURE) {
IMAGICK_RESTORE_ERROR_HANDLING;
return 0;
}
IMAGICK_RESTORE_ERROR_HANDLING;
/* php_stream_cast returns warning on some streams but still does not return FAILURE */
if (EG(exception)) {
return 0;
}
switch (type) {
case ImagickWriteImageFile:
status = MagickWriteImageFile(intern->magick_wand, fp);
break;
case ImagickWriteImagesFile:
status = MagickWriteImagesFile(intern->magick_wand, fp);
break;
case ImagickReadImageFile:
status = MagickReadImageFile(intern->magick_wand, fp);
break;
case ImagickPingImageFile:
status = MagickPingImageFile(intern->magick_wand, fp);
break;
default:
return 0;
break;
}
if (status == MagickFalse) {
return 0;
}
return 1;
}