コード例 #1
0
 public function testGetSetError()
 {
     $file = new FileUpload();
     $this->assertEquals(UPLOAD_ERR_OK, $file->getError(), 'Default is ERR_OK');
     $this->assertFalse($file->hasError(), 'ERR_OK is not an error');
     $this->assertEquals($file, $file->setError(UPLOAD_ERR_INI_SIZE), 'Fluent API');
     $this->assertEquals(UPLOAD_ERR_INI_SIZE, $file->getError());
     $this->assertTrue($file->hasError());
 }
コード例 #2
0
ファイル: MediaManager.php プロジェクト: Aziz-JH/core
 /**
  * Validate the upload
  * @return string
  */
 public function validateUpload()
 {
     \Message::reset();
     $objUploader = new \FileUpload();
     $objUploader->setName($this->strName);
     $uploadFolder = $this->strTempFolder;
     // Convert the $_FILES array to Contao format
     if (!empty($_FILES[$this->strName])) {
         $pathinfo = pathinfo(strtolower($_FILES[$this->strName]['name']));
         $strCacheName = standardize($pathinfo['filename']) . '.' . $pathinfo['extension'];
         $uploadFolder = $this->strTempFolder . '/' . substr($strCacheName, 0, 1);
         if (is_file(TL_ROOT . '/' . $uploadFolder . '/' . $strCacheName) && md5_file(TL_ROOT . '/' . $uploadFolder . '/' . $_FILES[$this->strName]['name']) != md5_file(TL_ROOT . '/' . $uploadFolder . '/' . $strCacheName)) {
             $strCacheName = standardize($pathinfo['filename']) . '-' . substr(md5_file(TL_ROOT . '/' . $uploadFolder . '/' . $_FILES[$this->strName]['name']), 0, 8) . '.' . $pathinfo['extension'];
             $uploadFolder = $this->strTempFolder . '/' . substr($strCacheName, 0, 1);
         }
         \Haste\Haste::mkdirr($uploadFolder);
         $arrFallback = $this->getFallbackData();
         // Check that image is not assigned in fallback language
         if (is_array($arrFallback) && in_array($strCacheName, $arrFallback)) {
             $this->addError($GLOBALS['TL_LANG']['ERR']['imageInFallback']);
         }
         $_FILES[$this->strName] = array('name' => array($strCacheName), 'type' => array($_FILES[$this->strName]['type']), 'tmp_name' => array($_FILES[$this->strName]['tmp_name']), 'error' => array($_FILES[$this->strName]['error']), 'size' => array($_FILES[$this->strName]['size']));
     }
     $varInput = '';
     try {
         $varInput = $objUploader->uploadTo($uploadFolder);
     } catch (\Exception $e) {
         $this->addError($e->getMessage());
     }
     if ($objUploader->hasError()) {
         foreach ($_SESSION['TL_ERROR'] as $strError) {
             $this->addError($strError);
         }
     }
     \Message::reset();
     if (!is_array($varInput) || empty($varInput)) {
         $this->addError($GLOBALS['TL_LANG']['MSC']['mmUnknownError']);
     }
     return $varInput[0];
 }
コード例 #3
0
 /**
  * Validate the upload
  * @return string
  */
 public function validateUpload()
 {
     \Message::reset();
     $strTempName = $this->strName . '_fineuploader';
     $objUploader = new \FileUpload();
     $objUploader->setName($this->strName);
     $blnIsChunk = isset($_POST['qqpartindex']);
     // Convert the $_FILES array to Contao format
     if (!empty($_FILES[$strTempName])) {
         $arrFile = array('name' => array($_FILES[$strTempName]['name']), 'type' => array($_FILES[$strTempName]['type']), 'tmp_name' => array($_FILES[$strTempName]['tmp_name']), 'error' => array($_FILES[$strTempName]['error']), 'size' => array($_FILES[$strTempName]['size']));
         // Set the UUID as the filename
         if ($blnIsChunk) {
             $arrFile['name'][0] = \Input::post('qquuid') . '.chunk';
         }
         // Check if the file exists
         if (file_exists(TL_ROOT . '/' . $this->strTemporaryPath . '/' . $arrFile['name'][0])) {
             $arrFile['name'][0] = $this->getFileName($arrFile['name'][0], $this->strTemporaryPath);
         }
         $_FILES[$this->strName] = $arrFile;
         unset($_FILES[$strTempName]);
         // Unset the temporary file
     }
     $varInput = '';
     $extensions = null;
     $maxlength = null;
     // Add the "chunk" extension to upload types
     if ($blnIsChunk) {
         $extensions = $GLOBALS['TL_CONFIG']['uploadTypes'];
         $GLOBALS['TL_CONFIG']['uploadTypes'] .= ',chunk';
     }
     // Override the default maxlength value
     if (isset($this->arrConfiguration['maxlength'])) {
         $maxlength = $GLOBALS['TL_CONFIG']['maxFileSize'];
         $GLOBALS['TL_CONFIG']['maxFileSize'] = $this->arrConfiguration['maxlength'];
     }
     try {
         $varInput = $objUploader->uploadTo($this->strTemporaryPath);
         if ($objUploader->hasError()) {
             foreach ($_SESSION['TL_ERROR'] as $strError) {
                 $this->addError($strError);
             }
         }
         \Message::reset();
     } catch (\Exception $e) {
         $this->addError($e->getMessage());
     }
     // Restore the default maxlength value
     if ($maxlength !== null) {
         $GLOBALS['TL_CONFIG']['maxFileSize'] = $maxlength;
     }
     // Restore the default extensions value
     if ($extensions !== null) {
         $GLOBALS['TL_CONFIG']['uploadTypes'] = $extensions;
     }
     if (!is_array($varInput) || empty($varInput)) {
         $this->addError($GLOBALS['TL_LANG']['MSC']['fineuploader_error']);
     }
     $varInput = $varInput[0];
     // Store the chunk in the session for further merge
     if ($blnIsChunk) {
         $_SESSION[$this->strName . '_FINEUPLOADER_CHUNKS'][\Input::post('qqfilename')][] = $varInput;
         // This is the last chunking request, merge the chunks and create the final file
         if (\Input::post('qqpartindex') == \Input::post('qqtotalparts') - 1) {
             $strFileName = \Input::post('qqfilename');
             // Get the new file name
             if (file_exists(TL_ROOT . '/' . $this->strTemporaryPath . '/' . $strFileName)) {
                 $strFileName = $this->getFileName($strFileName, $this->strTemporaryPath);
             }
             $objFile = new \File($this->strTemporaryPath . '/' . $strFileName);
             // Merge the chunks
             foreach ($_SESSION[$this->strName . '_FINEUPLOADER_CHUNKS'][\Input::post('qqfilename')] as $strChunk) {
                 $objFile->append(file_get_contents(TL_ROOT . '/' . $strChunk), '');
                 // Delete the file
                 \Files::getInstance()->delete($strChunk);
             }
             $objFile->close();
             $varInput = $objFile->path;
         }
     }
     return $varInput;
 }
コード例 #4
0
 /**
  * Validate the upload
  * @return string
  */
 public function validateUpload()
 {
     \Message::reset();
     $strTempName = $this->strName . '_upload';
     $objUploader = new \FileUpload();
     $objUploader->setName($this->strName);
     // Convert the $_FILES array to Contao format
     if (!empty($_FILES[$strTempName])) {
         $arrFile = array('name' => array($_FILES[$strTempName]['name']), 'type' => array($_FILES[$strTempName]['type']), 'tmp_name' => array($_FILES[$strTempName]['tmp_name']), 'error' => array($_FILES[$strTempName]['error']), 'size' => array($_FILES[$strTempName]['size']));
         // Check if the file exists
         if (file_exists(TL_ROOT . '/' . $this->strTemporaryPath . '/' . $arrFile['name'][0])) {
             $arrFile['name'][0] = $this->getFileName($arrFile['name'][0], $this->strTemporaryPath);
         }
         $_FILES[$this->strName] = $arrFile;
         unset($_FILES[$strTempName]);
         // Unset the temporary file
     }
     $varInput = '';
     $maxlength = null;
     // Override the default maxlength value
     if (isset($this->arrConfiguration['maxlength'])) {
         $maxlength = $GLOBALS['TL_CONFIG']['maxFileSize'];
         $GLOBALS['TL_CONFIG']['maxFileSize'] = $this->getMaximumFileSize();
     }
     try {
         $varInput = $objUploader->uploadTo($this->strTemporaryPath);
         if ($objUploader->hasError()) {
             foreach ($_SESSION['TL_ERROR'] as $strError) {
                 $this->addError($strError);
             }
         }
         \Message::reset();
     } catch (\Exception $e) {
         $this->addError($e->getMessage());
     }
     // Restore the default maxlength value
     if ($maxlength !== null) {
         $GLOBALS['TL_CONFIG']['maxFileSize'] = $maxlength;
     }
     if (!is_array($varInput) || empty($varInput)) {
         $this->addError($GLOBALS['TL_LANG']['MSC']['avatar_error']);
     }
     $varInput = $varInput[0];
     $strExtension = pathinfo($varInput, PATHINFO_EXTENSION);
     $arrAllowedTypes = trimsplit(',', strtolower($this->getAllowedExtensions()));
     // File type not allowed
     if (!in_array(strtolower($strExtension), $arrAllowedTypes)) {
         $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['filetype'], $strExtension));
     }
     // Check image size
     if (($arrImageSize = @getimagesize(TL_ROOT . '/' . $varInput)) !== false) {
         // Image exceeds maximum image width
         if ($arrImageSize[0] > $GLOBALS['TL_CONFIG']['imageWidth']) {
             $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['filewidth'], '', $GLOBALS['TL_CONFIG']['imageWidth']));
         }
         // Image exceeds maximum image height
         if ($arrImageSize[1] > $GLOBALS['TL_CONFIG']['imageHeight']) {
             $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['fileheight'], '', $GLOBALS['TL_CONFIG']['imageHeight']));
         }
         // Image exceeds minimum image width
         if ($arrImageSize[0] < $this->arrAvatarSize[0]) {
             $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['avatar_width'], $this->arrAvatarSize[0]));
         }
         // Image exceeds minimum image height
         if ($arrImageSize[1] < $this->arrAvatarSize[1]) {
             $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['avatar_height'], $this->arrAvatarSize[1]));
         }
     }
     return $varInput;
 }