Beispiel #1
0
 /**
  * Gets the content of the resource
  * @return string the file contents
  */
 public function getContent()
 {
     if ($this->_content === null) {
         if (is_object($this->_uploadedFile)) {
             $this->_content = file_get_contents($this->_uploadedFile->getTempName());
         } elseif (!$this->isNewRecord) {
             $this->_content = file_get_contents($this->path);
         }
     }
     return $this->_content;
 }
 /**
  * @param CUploadedFile $image
  * @return bool
  */
 public static function isAllowedType(CUploadedFile $image)
 {
     $type = CFileHelper::getMimeType($image->getTempName());
     if (!$type) {
         $type = CFileHelper::getMimeTypeByExtension($image->getName());
     }
     return in_array($type, EventsImagesConfig::get('types'));
 }
 /**
  * @param CUploadedFile $image
  * @return bool
  */
 public static function isAllowedType(CUploadedFile $image)
 {
     $type = CFileHelper::getMimeType($image->getTempName());
     if (!$type) {
         $type = CFileHelper::getMimeTypeByExtension($image->getName());
     }
     return in_array($type, array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png'));
 }
 /**
  * @param CUploadedFile $image
  * @return bool
  */
 public static function isAllowedType(CUploadedFile $image)
 {
     $type = CFileHelper::getMimeType($image->getTempName());
     if (!$type) {
         $type = CFileHelper::getMimeTypeByExtension($image->getName());
     }
     //return in_array($type, Yii::app()->params['storeImages']['types']);
     return in_array($type, StoreImagesConfig::get('types'));
 }
 /**
  * Store Upload file to S3.
  * @param CUploadedFile $uploadedFile
  * @param string $bucket The file to create the object
  * @return string url to the file.
  */
 public function store($uploadedFile, $bucket = NULL)
 {
     if ($this->config['randomPath']) {
         $filePath = $this->config['pathPrefix'] . md5(date('His')) . '/' . $uploadedFile->getName();
     } else {
         $filePath = $this->config['pathPrefix'] . $uploadedFile->getName();
     }
     if ($bucket === NULL) {
         $bucket = $this->config['defaultBucket'];
     }
     /** @var CFResponse $result */
     $result = $this->s3->create_object($bucket, $filePath, array('fileUpload' => $uploadedFile->getTempName(), 'acl' => $this->config['defaultACL']));
     if ($result->isOk()) {
         return urldecode($this->s3->get_object_url($bucket, $filePath));
     } else {
         Yii::log("STATUS:" . $result->status . "\nHEDAER:" . $result->header . "\nBODY:" . $result->body, CLogger::LEVEL_ERROR, "application");
         throw new CException($result->status);
     }
 }
Beispiel #6
0
 /**
  * Processes the uploaded image.
  * @param CUploadedFile $uploadedImg The uploaded image
  * @param string $title The title (or caption) of this image (optional)
  * @param string $description The detailed description of this image (optional)
  * @param string $albumName The name of the album to put the image in (optional)
  * @return string The ID of new uploaded image | false
  */
 private function saveImg($uploadedImg, $title = '', $description = '', $albumName = '')
 {
     // Key
     $userId = CassandraUtil::import(Yii::app()->user->getId())->__toString();
     $key = $userId . '_' . CassandraUtil::uuid1();
     // Path
     $path = realpath(Yii::app()->params['storagePath']) . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $userId;
     if (!is_dir($path)) {
         mkdir($path, 0775);
     }
     if (!empty($albumName)) {
         $path .= DIRECTORY_SEPARATOR . $albumName;
     }
     if (!is_dir($path)) {
         mkdir($path, 0775);
     }
     // Save the image information before processing the image
     $data = array('storage_path' => $uploadedImg->getTempName(), 'mime_type' => $uploadedImg->getType(), 'extension' => $uploadedImg->getExtensionName(), 'user_id' => Yii::app()->user->getId());
     if (!empty($title)) {
         $data['title'] = $title;
     }
     if (!empty($description)) {
         $data['description'] = $description;
     }
     if ($this->insert($key, $data) === false) {
         $uploadedImg->clean();
         return false;
     }
     // Render this image into different versions
     $photoTypes = Setting::model()->photo_types;
     // Get list of image types in JSON format. Decode it.
     $photoTypes = json_decode($photoTypes['value'], true);
     $img = Yii::app()->imagemod->load($data['storage_path']);
     $convertedImgs = array();
     foreach ($photoTypes as $type => $config) {
         $img->image_convert = 'jpg';
         $img->image_resize = true;
         $img->image_ratio = true;
         $img->image_x = $config['width'];
         $img->image_y = $config['height'];
         if (isset($config['suffix'])) {
             $img->file_safe_name = $key . $config['suffix'] . '.jpg';
         } else {
             $img->file_safe_name = $key . '.jpg';
         }
         $img->process($path);
         if (!$img->processed) {
             // Delete the original image
             $uploadedImg->clean();
             // Delete the record in db
             $this->delete($key);
             // Log the error
             Yii::log('Cannot resize the image ' . $data['storage_path'] . ' to ' . $path . '/' . $img->file_safe_name, 'error', 'application.modules.storage.Storage');
             // Delete all converted images
             foreach ($convertedImgs as $imgType => $imgPath) {
                 unlink($imgPath);
             }
             // Return false
             return false;
         } else {
             // Remember the path of converted image
             $convertedImgs[$type] = $img->file_dst_path;
         }
         // Update the database
         $data = array('storage_path' => $convertedImgs['original'], 'mime_type' => 'image/jpeg', 'extension' => 'jpg');
         unset($convertedImgs['original']);
         $data = array_merge($data, $convertedImgs);
         if ($this->insert($key, $data) === false) {
             // Delete the original image
             $uploadedImg->clean();
             // Delete the record in db
             $this->delete($key);
             // Log the error
             Yii::log('Cannot resize the image ' . $data['storage_path'] . ' to ' . $path . '/' . $img->file_safe_name, 'error', 'application.modules.storage.Storage');
             // Delete all converted images
             unlink($data['storage_path']);
             foreach ($convertedImgs as $imgType => $imgPath) {
                 unlink($imgPath);
             }
             // Return false
             return false;
         }
     }
     // Delete the temporary image
     $uploadedImg->clean();
     return $key;
 }
Beispiel #7
0
 /**
  * Sets a new logo image by given temp file
  *
  * @param CUploadedFile $file
  */
 public function setNew(CUploadedFile $file)
 {
     $this->delete();
     move_uploaded_file($file->getTempName(), $this->getPath());
     ImageConverter::Resize($this->getPath(), $this->getPath(), array('height' => $this->height, 'width' => 0, 'mode' => 'max', 'transparent' => $file->getExtensionName() == 'png' && ImageConverter::checkTransparent($this->getPath())));
 }
 /**
  * Internally validates a file object.
  * @param CModel $object the object being validated
  * @param string $attribute the attribute being validated
  * @param CUploadedFile $file uploaded file passed to check against a set of rules
  * @throws CException if failed to upload the file
  */
 protected function validateFile($object, $attribute, $file)
 {
     if (null === $file || ($error = $file->getError()) == UPLOAD_ERR_NO_FILE) {
         return $this->emptyAttribute($object, $attribute);
     } elseif ($error == UPLOAD_ERR_INI_SIZE || $error == UPLOAD_ERR_FORM_SIZE || $this->maxSize !== null && $file->getSize() > $this->maxSize) {
         $message = $this->tooLarge !== null ? $this->tooLarge : Yii::t('yii', 'The file "{file}" is too large. Its size cannot exceed {limit} bytes.');
         $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{limit}' => $this->getSizeLimit()));
     } elseif ($error == UPLOAD_ERR_PARTIAL) {
         throw new CException(Yii::t('yii', 'The file "{file}" was only partially uploaded.', array('{file}' => $file->getName())));
     } elseif ($error == UPLOAD_ERR_NO_TMP_DIR) {
         throw new CException(Yii::t('yii', 'Missing the temporary folder to store the uploaded file "{file}".', array('{file}' => $file->getName())));
     } elseif ($error == UPLOAD_ERR_CANT_WRITE) {
         throw new CException(Yii::t('yii', 'Failed to write the uploaded file "{file}" to disk.', array('{file}' => $file->getName())));
     } elseif (defined('UPLOAD_ERR_EXTENSION') && $error == UPLOAD_ERR_EXTENSION) {
         // available for PHP 5.2.0 or above
         throw new CException(Yii::t('yii', 'A PHP extension stopped the file upload.'));
     }
     if ($this->minSize !== null && $file->getSize() < $this->minSize) {
         $message = $this->tooSmall !== null ? $this->tooSmall : Yii::t('yii', 'The file "{file}" is too small. Its size cannot be smaller than {limit} bytes.');
         $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{limit}' => $this->minSize));
     }
     if ($this->types !== null) {
         if (is_string($this->types)) {
             $types = preg_split('/[\\s,]+/', strtolower($this->types), -1, PREG_SPLIT_NO_EMPTY);
         } else {
             $types = $this->types;
         }
         if (!in_array(strtolower($file->getExtensionName()), $types)) {
             $message = $this->wrongType !== null ? $this->wrongType : Yii::t('yii', 'The file "{file}" cannot be uploaded. Only files with these extensions are allowed: {extensions}.');
             $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{extensions}' => implode(', ', $types)));
         }
     }
     if ($this->mimeTypes !== null) {
         if (function_exists('finfo_open')) {
             $mimeType = false;
             if ($info = finfo_open(defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME)) {
                 $mimeType = finfo_file($info, $file->getTempName());
             }
         } elseif (function_exists('mime_content_type')) {
             $mimeType = mime_content_type($file->getTempName());
         } else {
             throw new CException(Yii::t('yii', 'In order to use MIME-type validation provided by CFileValidator fileinfo PECL extension should be installed.'));
         }
         if (is_string($this->mimeTypes)) {
             $mimeTypes = preg_split('/[\\s,]+/', strtolower($this->mimeTypes), -1, PREG_SPLIT_NO_EMPTY);
         } else {
             $mimeTypes = $this->mimeTypes;
         }
         if ($mimeType === false || !in_array(strtolower($mimeType), $mimeTypes)) {
             $message = $this->wrongMimeType !== null ? $this->wrongMimeType : Yii::t('yii', 'The file "{file}" cannot be uploaded. Only files of these MIME-types are allowed: {mimeTypes}.');
             $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{mimeTypes}' => implode(', ', $mimeTypes)));
         }
     }
 }
Beispiel #9
0
 /**
  * Sets a new profile image by given temp file
  *
  * @param CUploadedFile $file
  */
 public function setNew($file)
 {
     $this->delete();
     ImageConverter::TransformToJpeg($file->getTempName(), $this->getPath('_org'));
     ImageConverter::Resize($this->getPath('_org'), $this->getPath('_org'), array('width' => 850, 'mode' => 'max'));
     ImageConverter::Resize($this->getPath('_org'), $this->getPath(''), array('width' => $this->width, 'height' => $this->height));
 }
Beispiel #10
0
 /**
  * Saves given CUploadedFiles
  *
  */
 public static function store(CUploadedFile $cUploadedFile)
 {
     // Santize Filename
     $filename = $cUploadedFile->getName();
     $filename = trim($filename);
     $filename = preg_replace("/[^a-z0-9_\\-s\\.]/i", "", $filename);
     $pathInfo = pathinfo($filename);
     if (strlen($pathInfo['filename']) > 60) {
         $pathInfo['filename'] = substr($pathInfo['filename'], 0, 60);
     }
     $filename = $pathInfo['filename'];
     if (isset($pathInfo['extension'])) {
         $filename .= "." . $pathInfo['extension'];
     }
     $file = new File();
     if (!self::HasValidExtension($filename)) {
         return false;
     }
     $file->file_name = $filename;
     $file->title = $cUploadedFile->getName();
     $file->mime_type = $cUploadedFile->getType();
     #$file->size = $cUploadedFile->getSize();
     if ($file->save()) {
         // Add File to Filebase
         $file->slurp($cUploadedFile->getTempName());
         return $file;
     } else {
         return;
     }
 }
Beispiel #11
0
 private function _uploadImage(CUploadedFile $file)
 {
     $result = array('name' => $file->getName(), 'size' => $file->getSize(), 'tmpName' => $file->getTempName());
     if ($file->hasError) {
         $result['error'] = $file->getError();
     }
     return $result;
 }