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: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540: 541: 542: 543: 544: 545: 546: 547: 548: 549: 550: 551: 552: 553: 554: 555: 556: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566: 567: 568: 569: 570: 571: 572: 573: 574: 575: 576: 577: 578: 579: 580: 581: 582: 583: 584: 585: 586: 587: 588: 589: 590: 591: 592: 593: 594: 595: 596: 597: 598:
<?php
class icms_file_MediaUploadHandler {
private $allowUnknownTypes = false;
private $mediaName;
private $mediaType;
private $mediaSize;
private $mediaTmpName;
private $mediaError;
private $mediaRealType = '';
private $uploadDir = '';
private $allowedMimeTypes = array();
private $deniedMimeTypes = array(
"application/x-httpd-php"
);
private $maxFileSize = 0;
private $maxWidth;
private $maxHeight;
private $targetFileName;
private $prefix;
private $errors = array();
private $savedDestination;
private $savedFileName;
private $extensionToMime = array();
private $checkImageType = true;
private $extensionsToBeSanitized = array(
'php',
'phtml',
'phtm',
'php3',
'php4',
'cgi',
'pl',
'asp',
'php5'
);
private $imageExtensions = array(
1 => 'gif',
2 => 'jpg',
3 => 'png',
4 => 'swf',
5 => 'psd',
6 => 'bmp',
7 => 'tif',
8 => 'tif',
9 => 'jpc',
10 => 'jp2',
11 => 'jpx',
12 => 'jb2',
13 => 'swc',
14 => 'iff',
15 => 'wbmp',
16 => 'xbm'
);
public function __construct($uploadDir, $allowedMimeTypes, $maxFileSize = 0, $maxWidth = null, $maxHeight = null) {
$this->extensionToMime = icms_Utils::mimetypes() ;
if (!is_array($this->extensionToMime)) {
$this->extensionToMime = array();
return false;
}
if (is_array($allowedMimeTypes)) {
$this->allowedMimeTypes = & $allowedMimeTypes;
}
$this->uploadDir = $uploadDir;
$this->maxFileSize = (int) $maxFileSize;
if (isset($maxWidth)) {
$this->maxWidth = (int) $maxWidth;
}
if (isset($maxHeight)) {
$this->maxHeight = (int) $maxHeight;
}
icms_loadLanguageFile('core', 'uploader');
}
public function fetchMedia($media_name, $index = null) {
if (empty($this->extensionToMime)) {
self::setErrors(_ER_UP_MIMETYPELOAD);
return false;
}
if (!isset($_FILES[$media_name])) {
self::setErrors(_ER_UP_FILENOTFOUND);
return false;
} elseif (is_array($_FILES[$media_name]['name']) && isset($index)) {
$index = (int) ($index);
$this->mediaName = $_FILES[$media_name]['name'][$index];
$this->mediaType = $_FILES[$media_name]['type'][$index];
$this->mediaSize = $_FILES[$media_name]['size'][$index];
$this->mediaTmpName = $_FILES[$media_name]['tmp_name'][$index];
$this->mediaError = !empty($_FILES[$media_name]['error'][$index]) ? $_FILES[$media_name]['error'][$index] : 0;
} else {
$media_name = & $_FILES[$media_name];
$this->mediaName = $media_name['name'];
$this->mediaType = $media_name['type'];
$this->mediaSize = $media_name['size'];
$this->mediaTmpName = $media_name['tmp_name'];
$this->mediaError = !empty($media_name['error']) ? $media_name['error'] : 0;
}
if (($ext = strrpos($this->mediaName, '.')) !== false) {
$ext = strtolower(substr($this->mediaName, $ext +1));
if (isset($this->extensionToMime[$ext])) {
$this->mediaRealType = $this->extensionToMime[$ext];
}
}
$this->errors = array();
if ( (int) ($this->mediaSize) < 0) {
self::setErrors(_ER_UP_INVALIDFILESIZE);
return false;
}
if ($this->mediaName == '') {
self::setErrors(_ER_UP_FILENAMEEMPTY);
return false;
}
if ($this->mediaTmpName == 'none' || !is_uploaded_file($this->mediaTmpName)) {
self::setErrors($this->getUploadErrorText($media_name['error']));
return false;
}
if ($this->mediaError > 0) {
self::setErrors(sprintf(_ER_UP_ERROROCCURRED, $this->mediaError));
return false;
}
return true;
}
private function getUploadErrorText($err) {
switch ($err) {
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
$err = _ER_UP_INVALIDFILESIZE;
break;
case UPLOAD_ERR_PARTIAL:
$err = _ER_UP_PARTIALLY;
break;
case UPLOAD_ERR_NO_FILE:
$err = _ER_UP_NOFILEUPLOADED;
break;
case UPLOAD_ERR_NO_TMP_DIR:
$err = _ER_UP_NO_TMP_DIR;
break;
case UPLOAD_ERR_CANT_WRITE:
$err = _ER_UP_CANT_WRITE;
break;
case UPLOAD_ERR_EXTENSION:
$err = _ER_UP_NOFILEUPLOADED;
break;
}
return $err;
}
public function setTargetFileName($value) {
$this->targetFileName = (string) (trim($value));
}
public function setPrefix($value, $unique = true) {
if (isset($value) && $value !== '') {
if (!isset($unique) || (isset($unique) && $unique !== true)) {
$this->prefix = (string) trim($value);
} elseif (isset($unique) && $unique == true) {
$this->prefix = (string) (trim($value)) . '_' . uniqid(rand(0, 32767));
}
} elseif (!isset($value) || $value == '') {
if (!isset($unique) || (isset($unique) && $unique !== true)) {
$this->prefix = '';
} elseif (isset($unique) && $unique == true) {
$this->prefix = uniqid(rand(0, 32767));
}
}
}
public function getMediaName() {
return $this->mediaName;
}
public function getMediaType() {
return $this->mediaType;
}
public function getMediaSize() {
return $this->mediaSize;
}
public function getMediaTmpName() {
return $this->mediaTmpName;
}
public function getSavedFileName() {
return $this->savedFileName;
}
public function getSavedDestination() {
return $this->savedDestination;
}
public function upload($chmod = 0644) {
if ($this->uploadDir == '') {
self::setErrors(_ER_UP_UPLOADDIRNOTSET);
return false;
}
if (!is_dir($this->uploadDir)) {
self::setErrors(sprintf(_ER_UP_FAILEDOPENDIR, $this->uploadDir));
return false;
}
if (!is_writeable($this->uploadDir)) {
self::setErrors(sprintf(_ER_UP_FAILEDOPENDIRWRITE, $this->uploadDir));
return false;
}
self::sanitizeMultipleExtensions();
if (!self::checkMaxFileSize()) {
return false;
}
if (!self::checkMimeType()) {
return false;
}
if (!self::checkImageType()) {
return false;
}
if (!self::checkMaxWidth()) {
return false;
}
if (!self::checkMaxHeight()) {
return false;
}
if (count($this->errors) > 0) {
return false;
}
return self::_copyFile($chmod);
}
private function _copyFile($chmod) {
$matched = array();
if (!preg_match("/\.([a-zA-Z0-9]+)$/", $this->mediaName, $matched)) {
self::setErrors(sprintf(_ER_UP_INVALIDFILENAME, $this->mediaName));
return false;
}
if (isset($this->targetFileName)) {
$this->savedFileName = $this->targetFileName;
} elseif (isset($this->prefix) && $this->prefix !== '') {
$this->savedFileName = $this->prefix . '.' . strtolower($matched[1]);
} else {
$this->savedFileName = strtolower($this->mediaName);
}
$this->savedDestination = $this->uploadDir . '/' . $this->savedFileName;
if (!move_uploaded_file($this->mediaTmpName, $this->savedDestination)) {
self::setErrors(sprintf(_ER_UP_FAILEDSAVEFILE, $this->savedDestination));
return false;
}
$ext = strtolower(substr(strrchr($this->savedDestination, '.'), 1));
if (in_array($ext, $this->imageExtensions)) {
$info = @ getimagesize($this->savedDestination);
if ($info === false || $this->imageExtensions[(int) $info[2]] != $ext) {
self::setErrors(_ER_UP_SUSPICIOUSREFUSED);
@ unlink($this->savedDestination);
return false;
}
}
@ chmod($this->savedDestination, $chmod);
return true;
}
public function checkMaxFileSize() {
if (!isset($this->maxFileSize)) {
return true;
}
if ($this->mediaSize > $this->maxFileSize) {
self::setErrors(sprintf(_ER_UP_FILESIZETOOLARGE, $this->maxFileSize, $this->mediaSize));
return false;
}
return true;
}
public function checkMaxWidth() {
if (!isset($this->maxWidth)) {
return true;
}
if (false !== $dimension = getimagesize($this->mediaTmpName)) {
if ($dimension[0] > $this->maxWidth) {
self::setErrors(sprintf(_ER_UP_FILEWIDTHTOOLARGE, $this->maxWidth, $dimension[0]));
return false;
}
} else {
trigger_error(sprintf(_ER_UP_FAILEDFETCHIMAGESIZE, $this->mediaTmpName), E_USER_WARNING);
}
return true;
}
public function checkMaxHeight() {
if (!isset($this->maxHeight)) {
return true;
}
if (false !== $dimension = getimagesize($this->mediaTmpName)) {
if ($dimension[1] > $this->maxHeight) {
self::setErrors(sprintf(_ER_UP_FILEHEIGHTTOOLARGE, $this->maxHeight, $dimension[1]));
return false;
}
} else {
trigger_error(sprintf(_ER_UP_FAILEDFETCHIMAGESIZE, $this->mediaTmpName), E_USER_WARNING);
}
return true;
}
public function checkMimeType() {
global $icmsModule;
$mimetypeHandler = icms_getModulehandler('mimetype', 'system');
$modulename = (isset($icmsModule) && is_object($icmsModule)) ? $icmsModule->getVar('dirname') : 'system';
if (empty($this->mediaRealType) && empty($this->allowUnknownTypes)) {
self::setErrors(_ER_UP_UNKNOWNFILETYPEREJECTED);
return false;
}
$AllowedMimeTypes = $mimetypeHandler->AllowedModules($this->mediaRealType, $modulename);
if ((!empty($this->allowedMimeTypes) && !in_array($this->mediaRealType, $this->allowedMimeTypes))
|| (!empty($this->deniedMimeTypes) && in_array($this->mediaRealType, $this->deniedMimeTypes))
|| (empty($this->allowedMimeTypes) && !$AllowedMimeTypes))
{
self::setErrors(sprintf(_ER_UP_MIMETYPENOTALLOWED, $this->mediaType));
return false;
}
return true;
}
public function checkImageType() {
if (empty($this->checkImageType)) {
return true;
}
if (("image" == substr($this->mediaType, 0, strpos($this->mediaType, "/")))
|| (!empty($this->mediaRealType) && "image" == substr($this->mediaRealType, 0, strpos($this->mediaRealType, "/")))
) {
if (!($info = @ getimagesize($this->mediaTmpName))) {
self::setErrors(_ER_UP_INVALIDIMAGEFILE);
return false;
}
}
return true;
}
public function sanitizeMultipleExtensions() {
if (empty($this->extensionsToBeSanitized)) {
return;
}
$patterns = array();
$replaces = array();
foreach ($this->extensionsToBeSanitized as $ext) {
$patterns[] = "/\." . preg_quote($ext) . "\./i";
$replaces[] = "_" . $ext . ".";
}
$this->mediaName = preg_replace($patterns, $replaces, $this->mediaName);
}
public function setErrors($error) {
$this->errors[] = trim($error);
}
public function getErrors($ashtml = true) {
if (!$ashtml) {
return $this->errors;
} else {
$ret = '';
if (count($this->errors) > 0) {
$ret = '<h4>' . _ER_UP_ERRORSRETURNED . '</h4>';
foreach ($this->errors as $error) {
$ret .= $error . '<br />';
}
}
return $ret;
}
}
}