XoopsModules25x /
xoopstube
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
|
0 ignored issues
–
show
|
|||
| 2 | /** |
||
| 3 | * Module: XoopsTube |
||
| 4 | * |
||
| 5 | * You may not change or alter any portion of this comment or credits |
||
| 6 | * of supporting developers from this source code or any supporting source code |
||
| 7 | * which is considered copyrighted (c) material of the original comment or credit authors. |
||
| 8 | * |
||
| 9 | * PHP version 5 |
||
| 10 | * |
||
| 11 | * @category Module |
||
| 12 | * @package Xoopstube |
||
| 13 | * @author XOOPS Development Team |
||
| 14 | * @copyright 2001-2013 The XOOPS Project |
||
| 15 | * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
||
| 16 | * @version $Id$ |
||
| 17 | * @link http://sourceforge.net/projects/xoops/ |
||
| 18 | * @since 1.0.6 |
||
| 19 | */ |
||
| 20 | |||
| 21 | mt_srand((double)microtime() * 1000000); |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Class XoopsMediaUploader |
||
| 25 | */ |
||
| 26 | class XoopsMediaUploader |
||
| 27 | { |
||
| 28 | public $mediaName; |
||
| 29 | public $mediaType; |
||
| 30 | public $mediaSize; |
||
| 31 | public $mediaTmpName; |
||
| 32 | public $mediaError; |
||
| 33 | public $uploadDir = ''; |
||
| 34 | public $allowedMimeTypes = array(); |
||
| 35 | public $maxFileSize = 0; |
||
| 36 | public $maxWidth; |
||
| 37 | public $maxHeight; |
||
| 38 | public $targetFileName; |
||
| 39 | public $prefix; |
||
| 40 | public $ext; |
||
| 41 | public $dimension; |
||
| 42 | public $errors = array(); |
||
| 43 | public $savedDestination; |
||
| 44 | public $savedFileName; |
||
| 45 | /** |
||
| 46 | * No admin check for uploads |
||
| 47 | */ |
||
| 48 | public $noAdminSizeCheck; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Constructor |
||
| 52 | * |
||
| 53 | * @param string $uploadDir |
||
| 54 | * @param array|int $allowedMimeTypes |
||
| 55 | * @param int $maxFileSize |
||
| 56 | * @param int $maxWidth |
||
| 57 | * @param int $maxHeight |
||
| 58 | * |
||
| 59 | * @internal param int $cmodvalue |
||
| 60 | */ |
||
| 61 | public function __construct($uploadDir, $allowedMimeTypes = 0, $maxFileSize, $maxWidth = 0, $maxHeight = 0) |
||
| 62 | { |
||
| 63 | if (is_array($allowedMimeTypes)) { |
||
| 64 | $this->allowedMimeTypes = & $allowedMimeTypes; |
||
| 65 | } |
||
| 66 | $this->uploadDir = $uploadDir; |
||
| 67 | $this->maxFileSize = intval($maxFileSize); |
||
| 68 | if (isset($maxWidth)) { |
||
| 69 | $this->maxWidth = intval($maxWidth); |
||
| 70 | } |
||
| 71 | if (isset($maxHeight)) { |
||
| 72 | $this->maxHeight = intval($maxHeight); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param $value |
||
| 78 | */ |
||
| 79 | public function noAdminSizeCheck($value) |
||
| 80 | { |
||
| 81 | $this->noAdminSizeCheck = $value; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Fetch the uploaded file |
||
| 86 | * |
||
| 87 | * @param string $media_name Name of the file field |
||
| 88 | * @param int $index Index of the file (if more than one uploaded under that name) |
||
| 89 | * |
||
| 90 | * @global $HTTP_POST_FILES |
||
| 91 | * @return bool |
||
| 92 | */ |
||
| 93 | public function fetchMedia($media_name, $index = null) |
||
| 94 | { |
||
| 95 | global $_FILES; |
||
| 96 | |||
| 97 | if (!isset($_FILES[$media_name])) { |
||
| 98 | $this->setErrors(_AM_XOOPSTUBE_READWRITEERROR); |
||
| 99 | |||
| 100 | return false; |
||
| 101 | } elseif (is_array($_FILES[$media_name]['name']) && isset($index)) { |
||
| 102 | $index = intval($index); |
||
| 103 | $this->mediaName = (get_magic_quotes_gpc()) ? stripslashes( |
||
| 104 | $_FILES[$media_name]['name'][$index] |
||
| 105 | ) : $_FILES[$media_name]['name'][$index]; |
||
| 106 | $this->mediaType = $_FILES[$media_name]['type'][$index]; |
||
| 107 | $this->mediaSize = $_FILES[$media_name]['size'][$index]; |
||
| 108 | $this->mediaTmpName = $_FILES[$media_name]['tmp_name'][$index]; |
||
| 109 | $this->mediaError = !empty($_FILES[$media_name]['error'][$index]) ? $_FILES[$media_name]['error'][$index] : 0; |
||
| 110 | } else { |
||
| 111 | $media_name = @$_FILES[$media_name]; |
||
| 112 | $this->mediaName = (get_magic_quotes_gpc()) ? stripslashes($media_name['name']) : $media_name['name']; |
||
| 113 | $this->mediaName = $media_name['name']; |
||
| 114 | $this->mediaType = $media_name['type']; |
||
| 115 | $this->mediaSize = $media_name['size']; |
||
| 116 | $this->mediaTmpName = $media_name['tmp_name']; |
||
| 117 | $this->mediaError = !empty($media_name['error']) ? $media_name['error'] : 0; |
||
| 118 | } |
||
| 119 | $this->dimension = getimagesize($this->mediaTmpName); |
||
| 120 | |||
| 121 | $this->errors = array(); |
||
| 122 | |||
| 123 | if (intval($this->mediaSize) < 0) { |
||
| 124 | $this->setErrors(_AM_XOOPSTUBE_INVALIDFILESIZE); |
||
| 125 | |||
| 126 | return false; |
||
| 127 | } |
||
| 128 | if ($this->mediaName == '') { |
||
| 129 | $this->setErrors(_AM_XOOPSTUBE_FILENAMEEMPTY); |
||
| 130 | |||
| 131 | return false; |
||
| 132 | } |
||
| 133 | |||
| 134 | if ($this->mediaTmpName == 'none') { |
||
| 135 | $this->setErrors(_AM_XOOPSTUBE_NOFILEUPLOAD); |
||
| 136 | |||
| 137 | return false; |
||
| 138 | } |
||
| 139 | |||
| 140 | if (!is_uploaded_file($this->mediaTmpName)) { |
||
| 141 | switch ($this->mediaError) { |
||
| 142 | case 0: // no error; possible file attack! |
||
| 143 | $this->setErrors(_AM_XOOPSTUBE_UPLOADERRORZERO); |
||
| 144 | break; |
||
| 145 | case 1: // uploaded file exceeds the upload_max_filesize directive in php.ini |
||
| 146 | $this->setErrors(_AM_XOOPSTUBE_UPLOADERRORONE); |
||
| 147 | break; |
||
| 148 | case 2: // uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form |
||
| 149 | $this->setErrors(_AM_XOOPSTUBE_UPLOADERRORTWO); |
||
| 150 | break; |
||
| 151 | case 3: // uploaded file was only partially uploaded |
||
| 152 | $this->setErrors(_AM_XOOPSTUBE_UPLOADERRORTHREE); |
||
| 153 | break; |
||
| 154 | case 4: // no file was uploaded |
||
| 155 | $this->setErrors(_AM_XOOPSTUBE_UPLOADERRORFOUR); |
||
| 156 | break; |
||
| 157 | default: // a default error, just in case! :) |
||
|
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
36% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. Loading history...
|
|||
| 158 | $this->setErrors(_AM_XOOPSTUBE_UPLOADERRORFIVE); |
||
| 159 | break; |
||
| 160 | } |
||
| 161 | |||
| 162 | return false; |
||
| 163 | } |
||
| 164 | |||
| 165 | return true; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Set the target filename |
||
| 170 | * |
||
| 171 | * @param string $value |
||
| 172 | */ |
||
| 173 | public function setTargetFileName($value) |
||
| 174 | { |
||
| 175 | $this->targetFileName = strval(trim($value)); |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Set the prefix |
||
| 180 | * |
||
| 181 | * @param string $value |
||
| 182 | */ |
||
| 183 | public function setPrefix($value) |
||
| 184 | { |
||
| 185 | $this->prefix = strval(trim($value)); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get the uploaded filename |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public function getMediaName() |
||
| 194 | { |
||
| 195 | return $this->mediaName; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get the type of the uploaded file |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | public function getMediaType() |
||
| 204 | { |
||
| 205 | return $this->mediaType; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get the size of the uploaded file |
||
| 210 | * |
||
| 211 | * @return int |
||
| 212 | */ |
||
| 213 | public function getMediaSize() |
||
| 214 | { |
||
| 215 | return $this->mediaSize; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get the temporary name that the uploaded file was stored under |
||
| 220 | * |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | public function getMediaTmpName() |
||
| 224 | { |
||
| 225 | return $this->mediaTmpName; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get the saved filename |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public function getSavedFileName() |
||
| 234 | { |
||
| 235 | return $this->savedFileName; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get the destination the file is saved to |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public function getSavedDestination() |
||
| 244 | { |
||
| 245 | return $this->savedDestination; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Check the file and copy it to the destination |
||
| 250 | * |
||
| 251 | * @param int $chmod |
||
| 252 | * |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | public function upload($chmod = 0644) |
||
| 256 | { |
||
| 257 | if ($this->uploadDir == '') { |
||
| 258 | $this->setErrors(_AM_XOOPSTUBE_NOUPLOADDIR); |
||
| 259 | |||
| 260 | return false; |
||
| 261 | } |
||
| 262 | |||
| 263 | if (!is_dir($this->uploadDir)) { |
||
| 264 | $this->setErrors(_AM_XOOPSTUBE_FAILOPENDIR . $this->uploadDir); |
||
| 265 | } |
||
| 266 | |||
| 267 | if (!is_writeable($this->uploadDir)) { |
||
| 268 | $this->setErrors(_AM_XOOPSTUBE_FAILOPENDIRWRITEPERM . $this->uploadDir); |
||
| 269 | } |
||
| 270 | |||
| 271 | if (!$this->checkMaxFileSize()) { |
||
| 272 | $this->setErrors(sprintf(_AM_XOOPSTUBE_FILESIZEMAXSIZE, $this->mediaSize, $this->maxFileSize)); |
||
| 273 | } |
||
| 274 | |||
| 275 | if (is_array($this->dimension)) { |
||
| 276 | if (!$this->checkMaxWidth($this->dimension[0])) { |
||
| 277 | $this->setErrors(sprintf(_AM_XOOPSTUBE_FILESIZEMAXWIDTH, $this->dimension[0], $this->maxWidth)); |
||
| 278 | } |
||
| 279 | if (!$this->checkMaxHeight($this->dimension[1])) { |
||
| 280 | $this->setErrors(sprintf(_AM_XOOPSTUBE_FILESIZEMAXHEIGHT, $this->dimension[1], $this->maxHeight)); |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | if (!$this->checkMimeType()) { |
||
| 285 | $this->setErrors(_AM_XOOPSTUBE_MIMENOTALLOW . $this->mediaType); |
||
| 286 | } |
||
| 287 | |||
| 288 | if (!$this->_copyFile($chmod)) { |
||
| 289 | $this->setErrors(_AM_XOOPSTUBE_FAILEDUPLOADING . $this->mediaName); |
||
| 290 | } |
||
| 291 | |||
| 292 | if (count($this->errors) > 0) { |
||
| 293 | return false; |
||
| 294 | } |
||
| 295 | |||
| 296 | return true; |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Copy the file to its destination |
||
| 301 | * |
||
| 302 | * @param $chmod |
||
| 303 | * |
||
| 304 | * @return bool |
||
| 305 | */ |
||
| 306 | public function _copyFile($chmod) |
||
| 307 | { |
||
| 308 | $matched = array(); |
||
| 309 | if (!preg_match("/\.([a-zA-Z0-9]+)$/", $this->mediaName, $matched)) { |
||
| 310 | return false; |
||
| 311 | } |
||
| 312 | if (isset($this->targetFileName)) { |
||
| 313 | $this->savedFileName = $this->targetFileName; |
||
| 314 | } elseif (isset($this->prefix)) { |
||
| 315 | $this->savedFileName = uniqid($this->prefix) . '.' . strtolower($matched[1]); |
||
| 316 | } else { |
||
| 317 | $this->savedFileName = strtolower($this->mediaName); |
||
| 318 | } |
||
| 319 | $this->savedFileName = preg_replace('!\s+!', '_', $this->savedFileName); |
||
| 320 | $this->savedDestination = $this->uploadDir . $this->savedFileName; |
||
| 321 | if (is_file($this->savedDestination) && !!is_dir($this->savedDestination)) { |
||
| 322 | $this->setErrors(_AM_XOOPSTUBE_FILE . $this->mediaName . _AM_XOOPSTUBE_ALREADYEXISTTRYAGAIN); |
||
| 323 | |||
| 324 | return false; |
||
| 325 | } |
||
| 326 | if (!move_uploaded_file($this->mediaTmpName, $this->savedDestination)) { |
||
| 327 | return false; |
||
| 328 | } |
||
| 329 | @chmod($this->savedDestination, $chmod); |
||
| 330 | |||
| 331 | return true; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Is the file the right size? |
||
| 336 | * |
||
| 337 | * @return bool |
||
| 338 | */ |
||
| 339 | public function checkMaxFileSize() |
||
| 340 | { |
||
| 341 | if ($this->noAdminSizeCheck) { |
||
| 342 | return true; |
||
| 343 | } |
||
| 344 | if ($this->mediaSize > $this->maxFileSize) { |
||
| 345 | return false; |
||
| 346 | } |
||
| 347 | |||
| 348 | return true; |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Is the picture the right width? |
||
| 353 | * |
||
| 354 | * @param $dimension |
||
| 355 | * |
||
| 356 | * @return bool |
||
| 357 | */ |
||
| 358 | View Code Duplication | public function checkMaxWidth($dimension) |
|
| 359 | { |
||
| 360 | if (!isset($this->maxWidth)) { |
||
| 361 | return true; |
||
| 362 | } |
||
| 363 | if ($dimension > $this->maxWidth) { |
||
| 364 | return false; |
||
| 365 | } |
||
| 366 | |||
| 367 | return true; |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Is the picture the right height? |
||
| 372 | * |
||
| 373 | * @param $dimension |
||
| 374 | * |
||
| 375 | * @return bool |
||
| 376 | */ |
||
| 377 | View Code Duplication | public function checkMaxHeight($dimension) |
|
| 378 | { |
||
| 379 | if (!isset($this->maxHeight)) { |
||
| 380 | return true; |
||
| 381 | } |
||
| 382 | if ($dimension > $this->maxWidth) { |
||
| 383 | return false; |
||
| 384 | } |
||
| 385 | |||
| 386 | return true; |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Is the file the right Mime type |
||
| 391 | * |
||
| 392 | * (is there a right type of mime? ;-) |
||
| 393 | * |
||
| 394 | * @return bool |
||
| 395 | */ |
||
| 396 | public function checkMimeType() |
||
| 397 | { |
||
| 398 | if (count($this->allowedMimeTypes) > 0 && !in_array($this->mediaType, $this->allowedMimeTypes)) { |
||
| 399 | return false; |
||
| 400 | } else { |
||
| 401 | return true; |
||
| 402 | } |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Add an error |
||
| 407 | * |
||
| 408 | * @param string $error |
||
| 409 | */ |
||
| 410 | public function setErrors($error) |
||
| 411 | { |
||
| 412 | $this->errors[] = trim($error); |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Get generated errors |
||
| 417 | * |
||
| 418 | * @param bool $ashtml Format using HTML? |
||
| 419 | * |
||
| 420 | * @return array |string Array of array messages OR HTML string |
||
| 421 | */ |
||
| 422 | function &getErrors($ashtml = true) |
||
| 423 | { |
||
| 424 | if (!$ashtml) { |
||
| 425 | return $this->errors; |
||
| 426 | } else { |
||
| 427 | $ret = ''; |
||
| 428 | if (count($this->errors) > 0) { |
||
| 429 | $ret = _AM_XOOPSTUBE_ERRORSRETURNUPLOAD; |
||
| 430 | foreach ($this->errors as $error) { |
||
| 431 | $ret .= $error . '<br />'; |
||
| 432 | } |
||
| 433 | } |
||
| 434 | |||
| 435 | return $ret; |
||
| 436 | } |
||
| 437 | } |
||
| 438 | } |
||
| 439 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.