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
|
/*
+----------------------------------------------------------------------+
| PHP Version 5 / Imagick |
+----------------------------------------------------------------------+
| Copyright (c) 2006-2009 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_defs.h"
#include "php_imagick_macros.h"
#ifdef PHP_WIN32
# include "TSRM/tsrm_virtual_cwd.h"
#endif
/* {{{ Indicates whether image filename has a page. For example test.pdf[0] */
zend_bool php_imagick_has_page(char *filename, int filename_len TSRMLS_DC) {
/* Too short */
if (filename_len < 3) {
return 0;
}
/* If these conditions match it's safe to assume the filename contains a page */
if ((filename[filename_len - 1] == ']') &&
(filename[filename_len - 2] >= 48) &&
(filename[filename_len - 2] <= 57)) {
return 1;
}
return 0;
}
/* }}} */
/* {{{ url format? */
zend_bool php_imagick_is_url(char *filename, int filename_len TSRMLS_DC)
{
char *path_for_open;
if (php_stream_locate_url_wrapper(filename, &path_for_open, STREAM_LOCATE_WRAPPERS_ONLY TSRMLS_CC)) {
return 1;
}
return 0;
}
/* }}} */
/* {{{ Does the filename have a format indicator?
Returns format length if found, -1 on fail */
int php_imagick_format_indicator(char *filename, int filename_len TSRMLS_DC)
{
#ifdef PHP_WIN32
if (IS_ABSOLUTE_PATH(filename, filename_len)) {
if (count_occurences_of(':', filename TSRMLS_CC) == 1) {
return -1;
}
}
#endif
if (count_occurences_of(':', filename TSRMLS_CC) == 0) {
return -1;
}
return php_imagick_recognized_format(filename, filename_len TSRMLS_CC);
}
/* }}} */
/* {{{ Is the format virtual ? */
zend_bool php_imagick_is_virtual_format(char *filename, int filename_len TSRMLS_DC) {
#ifdef PHP_WIN32
const int elements = 18;
int i = 0;
const char *no_basedir_fmt[] = { "CAPTION:", "CLIPBOARD:", "FRACTAL:", "GRADIENT:", "LABEL:", "MATTE:",
"NULL:", "PLASMA:", "PRINT:", "SCAN:", "RADIAL_GRADIENT:", "SCANX:",
"WIN:", "XC:", "MAGICK:", "GRANITE:", "LOGO:", "NETSCAPE:", "ROSE:" };
#else
const int elements = 19;
int i = 0;
const char *no_basedir_fmt[] = { "CAPTION:", "CLIPBOARD:", "FRACTAL:", "GRADIENT:", "LABEL:", "MATTE:",
"NULL:", "PLASMA:", "PRINT:", "SCAN:", "RADIAL_GRADIENT:", "SCANX:",
"WIN:", "X:", "XC:", "MAGICK:", "GRANITE:", "LOGO:", "NETSCAPE:", "ROSE:" };
#endif
for (i = 0; i <= elements; i++) {
if (strncasecmp(filename, no_basedir_fmt[i], strlen(no_basedir_fmt[i])) == 0) {
return 1;
}
}
return 0;
}
/* }}} */
/* {{{ check if recognised format */
int php_imagick_recognized_format(char *filename, int filename_len TSRMLS_DC)
{
char **formats, *format, *pch;
unsigned long chr_pos, i, num_formats = 0;
if ((pch = strchr(filename, ':')) == NULL) {
return -1;
}
chr_pos = pch - filename;
format = estrndup(filename, chr_pos);
/* I know this! */
if (php_imagick_is_virtual_format(filename, filename_len TSRMLS_CC)) {
efree(format);
return chr_pos;
}
formats = MagickQueryFormats(php_strtoupper(format, chr_pos), &num_formats);
efree(format);
if (num_formats == 0) {
chr_pos = -1;
}
for (i = 0 ; i < num_formats ; i++) {
IMAGICK_FREE_MEMORY(char *, formats[i]);
}
IMAGICK_FREE_MEMORY(char **, formats);
return chr_pos;
}
/* }}} */
/* {{{ Gets absolute filename of the file.
Returns null if format is virtual
Filename can be passed with or without format indicator */
char *php_imagick_get_absolute_filename(char *filename, int filename_len TSRMLS_DC)
{
int chr_pos = php_imagick_format_indicator(filename, filename_len TSRMLS_CC);
if (chr_pos == -1) {
/* There was no recognised format in the filename, try to access as is */
return expand_filepath(filename, NULL TSRMLS_CC);
}
/* the was a format but is it virtual?? */
if (php_imagick_is_virtual_format(filename, filename_len TSRMLS_CC)) {
/* virtual format has no filename */
return NULL;
}
return expand_filepath(filename + chr_pos + 1, NULL TSRMLS_CC);
}
/* }}} */
/* {{{ Whether php stream should be used to read the image */
zend_bool php_imagick_use_stream(char *filename, int filename_len TSRMLS_DC)
{
/* Read urls using php */
if (php_imagick_is_url(filename, filename_len TSRMLS_CC)) {
return 1;
}
/* Explicit format or page indicator -> use imagemagick */
if ((php_imagick_format_indicator(filename, filename_len TSRMLS_CC) > 0) ||
(php_imagick_has_page(filename, filename_len TSRMLS_CC))) {
return 0;
}
/* The rest using php streams */
return 1;
}
/* }}} */
int php_imagick_safety_check(char *filename, int filename_len TSRMLS_DC)
{
int status = IMAGICK_READ_WRITE_NO_ERROR;
if (PG(open_basedir) || PG(safe_mode)) {
char *absolute = php_imagick_get_absolute_filename(filename, filename_len TSRMLS_CC);
if (absolute) {
if (PG(safe_mode) && (!php_checkuid_ex(absolute, NULL, CHECKUID_CHECK_FILE_AND_DIR, CHECKUID_NO_ERRORS))) {
status = IMAGICK_READ_WRITE_SAFE_MODE_ERROR;
}
if (PG(open_basedir) && php_check_open_basedir_ex(absolute, 0 TSRMLS_CC)) {
status = IMAGICK_READ_WRITE_OPEN_BASEDIR_ERROR;
}
efree(absolute);
}
}
return status;
}
/* {{{ }}} */
MagickBooleanType php_imagick_read_image_using_imagemagick(php_imagick_object *intern, int type, char *filename, int filename_len TSRMLS_DC)
{
int pos = 0;
char *absolute = NULL;
absolute = php_imagick_get_absolute_filename(filename, filename_len TSRMLS_CC);
if (absolute) {
int status = php_imagick_safety_check(absolute, strlen(absolute) TSRMLS_CC);
if (status != IMAGICK_READ_WRITE_NO_ERROR) {
efree(absolute);
return status;
}
}
if (type == 1) {
if (MagickReadImage(intern->magick_wand, filename) == MagickFalse) {
goto general_error;
}
} else {
if (MagickPingImage(intern->magick_wand, filename) == MagickFalse) {
goto general_error;
}
}
if (!absolute) {
MagickSetImageFilename(intern->magick_wand, "");
} else {
MagickSetImageFilename(intern->magick_wand, absolute);
efree(absolute);
}
IMAGICK_CORRECT_ITERATOR_POSITION(intern);
return IMAGICK_READ_WRITE_NO_ERROR;
general_error:
if (absolute) efree(absolute);
return IMAGICK_READ_WRITE_UNDERLYING_LIBRARY;
}
int php_imagick_read_image_using_php_streams(php_imagick_object *intern, int type, char *filename, int filename_len TSRMLS_DC)
{
php_stream *stream;
MagickBooleanType status;
FILE *fp;
#if ZEND_MODULE_API_NO > 20060613
zend_error_handling error_handling;
#endif
#if ZEND_MODULE_API_NO > 20060613
zend_replace_error_handling(EH_THROW, php_imagick_exception_class_entry, &error_handling TSRMLS_CC);
#else
php_set_error_handling(EH_THROW, php_imagick_exception_class_entry TSRMLS_CC);
#endif
stream = php_stream_open_wrapper(filename, "rb", (ENFORCE_SAFE_MODE|IGNORE_PATH) & ~REPORT_ERRORS, NULL);
if (!stream) {
goto return_error;
}
if (php_stream_can_cast(stream, PHP_STREAM_AS_STDIO|PHP_STREAM_CAST_INTERNAL) == FAILURE) {
goto return_error;
}
if (php_stream_cast(stream, PHP_STREAM_AS_STDIO|PHP_STREAM_CAST_INTERNAL, (void*)&fp, 0) == FAILURE) {
goto return_error;
}
#if ZEND_MODULE_API_NO > 20060613
zend_restore_error_handling(&error_handling TSRMLS_CC);
#else
php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
#endif
if (type == 1) {
status = MagickReadImageFile(intern->magick_wand, fp);
} else {
status = MagickPingImageFile(intern->magick_wand, fp);
}
if (status == MagickFalse) {
php_stream_close(stream);
return IMAGICK_READ_WRITE_UNDERLYING_LIBRARY;
}
if (php_stream_is(stream, PHP_STREAM_IS_STDIO)) {
char *absolute = expand_filepath(filename, NULL TSRMLS_CC);
MagickSetImageFilename(intern->magick_wand, absolute);
efree(absolute);
} else {
/* Set to empty filename, otherwise it will point to MAGICK_TEMP/magick-XXXXX */
MagickSetImageFilename(intern->magick_wand, "");
}
php_stream_close(stream);
if (status == MagickFalse) {
return IMAGICK_READ_WRITE_UNDERLYING_LIBRARY;
}
IMAGICK_CORRECT_ITERATOR_POSITION(intern);
return IMAGICK_READ_WRITE_NO_ERROR;
return_error:
#if ZEND_MODULE_API_NO > 20060613
zend_restore_error_handling(&error_handling TSRMLS_CC);
#else
php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
#endif
if (stream) php_stream_close(stream);
return IMAGICK_READ_WRITE_UNDERLYING_LIBRARY;
}
int read_image_into_magickwand(php_imagick_object *intern, int type, char *filename, int filename_len TSRMLS_DC)
{
if (!filename || filename_len == 0) {
return IMAGICK_READ_WRITE_NO_ERROR;
}
/* Use traditional imagemagick method or php streams (?) */
if (php_imagick_use_stream(filename, filename_len TSRMLS_CC)) {
return php_imagick_read_image_using_php_streams(intern, type, filename, filename_len TSRMLS_CC);
} else {
return php_imagick_read_image_using_imagemagick(intern, type, filename, filename_len TSRMLS_CC);
}
}
|