コード例 #1
0
ファイル: project.php プロジェクト: phpsource/CrowdFunding
 /**
  * Upload a pitch image.
  *
  * @param  array $image
  *
  * @throws Exception
  *
  * @return array
  */
 public function uploadPitchImage($image)
 {
     $app = JFactory::getApplication();
     /** @var $app JApplicationSite */
     $uploadedFile = JArrayHelper::getValue($image, 'tmp_name');
     $uploadedName = JArrayHelper::getValue($image, 'name');
     $errorCode = JArrayHelper::getValue($image, 'error');
     // Load parameters.
     $params = JComponentHelper::getParams($this->option);
     /** @var  $params Joomla\Registry\Registry */
     $destFolder = JPath::clean(JPATH_ROOT . DIRECTORY_SEPARATOR . $params->get("images_directory", "images/crowdfunding"));
     $tmpFolder = $app->get("tmp_path");
     // Joomla! media extension parameters
     $mediaParams = JComponentHelper::getParams("com_media");
     /** @var  $mediaParams Joomla\Registry\Registry */
     jimport("itprism.file");
     jimport("itprism.file.uploader.local");
     jimport("itprism.file.validator.size");
     jimport("itprism.file.validator.image");
     jimport("itprism.file.validator.server");
     $file = new ITPrismFile();
     // Prepare size validator.
     $KB = 1024 * 1024;
     $fileSize = (int) $app->input->server->get('CONTENT_LENGTH');
     $uploadMaxSize = $mediaParams->get("upload_maxsize") * $KB;
     $sizeValidator = new ITPrismFileValidatorSize($fileSize, $uploadMaxSize);
     // Prepare server validator.
     $serverValidator = new ITPrismFileValidatorServer($errorCode, array(UPLOAD_ERR_NO_FILE));
     // Prepare image validator.
     $imageValidator = new ITPrismFileValidatorImage($uploadedFile, $uploadedName);
     // Get allowed mime types from media manager options
     $mimeTypes = explode(",", $mediaParams->get("upload_mime"));
     $imageValidator->setMimeTypes($mimeTypes);
     // Get allowed image extensions from media manager options
     $imageExtensions = explode(",", $mediaParams->get("image_extensions"));
     $imageValidator->setImageExtensions($imageExtensions);
     $file->addValidator($sizeValidator)->addValidator($imageValidator)->addValidator($serverValidator);
     // Validate the file
     if (!$file->isValid()) {
         throw new RuntimeException($file->getError());
     }
     // Generate temporary file name
     $ext = JString::strtolower(JFile::makeSafe(JFile::getExt($image['name'])));
     jimport("itprism.string");
     $generatedName = new ITPrismString();
     $generatedName->generateRandomString(32);
     $tmpDestFile = $tmpFolder . DIRECTORY_SEPARATOR . $generatedName . "." . $ext;
     // Prepare uploader object.
     $uploader = new ITPrismFileUploaderLocal($uploadedFile);
     $uploader->setDestination($tmpDestFile);
     // Upload temporary file
     $file->setUploader($uploader);
     $file->upload();
     // Get file
     $tmpDestFile = $file->getFile();
     if (!is_file($tmpDestFile)) {
         throw new Exception('COM_CROWDFUNDING_ERROR_FILE_CANT_BE_UPLOADED');
     }
     // Resize image
     $image = new JImage();
     $image->loadFile($tmpDestFile);
     if (!$image->isLoaded()) {
         throw new Exception(JText::sprintf('COM_CROWDFUNDING_ERROR_FILE_NOT_FOUND', $tmpDestFile));
     }
     $imageName = $generatedName . "_pimage.png";
     $imageFile = JPath::clean($destFolder . DIRECTORY_SEPARATOR . $imageName);
     // Create main image
     $width = $params->get("pitch_image_width", 600);
     $height = $params->get("pitch_image_height", 400);
     $image->resize($width, $height, false);
     $image->toFile($imageFile, IMAGETYPE_PNG);
     // Remove the temporary
     if (is_file($tmpDestFile)) {
         JFile::delete($tmpDestFile);
     }
     return $imageName;
 }
コード例 #2
0
ファイル: proof.php プロジェクト: xop32/Proof-of-Identity
 /**
  * Upload the file.
  *
  * @param array $tmpFile
  *
  * @throws Exception
  *
  * @return array
  */
 public function uploadFile($tmpFile)
 {
     jimport("joomla.filesystem.folder");
     jimport("joomla.filesystem.file");
     $app = JFactory::getApplication();
     /** @var $app JApplicationSite */
     $uploadedFile = JArrayHelper::getValue($tmpFile, 'tmp_name');
     $uploadedName = JArrayHelper::getValue($tmpFile, 'name');
     $errorCode = JArrayHelper::getValue($tmpFile, 'error');
     // Load parameters.
     $params = JComponentHelper::getParams($this->option);
     /** @var  $params Joomla\Registry\Registry */
     // Generate a folder name.
     jimport("itprism.string");
     $generatedName = new ITPrismString();
     $generatedName->generateRandomString();
     $destFolder = JPath::clean($app->get("tmp_path") . DIRECTORY_SEPARATOR . (string) $generatedName);
     // Create a temporary folder.
     if (!JFolder::create($destFolder)) {
         throw new RuntimeException(JText::_('COM_IDENTITYPROOF_ERROR_FILE_CANT_BE_UPLOADED'));
     }
     // Create .htaccess file to deny the access for that folder.
     $htaccessFile = JPath::clean($destFolder . DIRECTORY_SEPARATOR . ".htaccess");
     $fileContent = "Deny from all";
     if (!JFile::write($htaccessFile, $fileContent)) {
         throw new RuntimeException(JText::_('COM_IDENTITYPROOF_ERROR_FILE_CANT_BE_UPLOADED'));
     }
     jimport("itprism.file");
     jimport("itprism.file.uploader.local");
     jimport("itprism.file.validator.size");
     jimport("itprism.file.validator.type");
     jimport("itprism.file.validator.server");
     $file = new ITPrismFile();
     // Prepare size validator.
     $KB = 1024 * 1024;
     $fileSize = (int) $app->input->server->get('CONTENT_LENGTH');
     $uploadMaxSize = $params->get("max_size") * $KB;
     // Prepare file size validator
     $sizeValidator = new ITPrismFileValidatorSize($fileSize, $uploadMaxSize);
     // Prepare server validator.
     $serverValidator = new ITPrismFileValidatorServer($errorCode, array(UPLOAD_ERR_NO_FILE));
     // Prepare image validator.
     $typeValidator = new ITPrismFileValidatorType($uploadedFile, $uploadedName);
     // Get allowed mime types from media manager options
     $mimeTypes = explode(",", $params->get("legal_types"));
     $mimeTypes = array_map("trim", $mimeTypes);
     $typeValidator->setMimeTypes($mimeTypes);
     // Get allowed image extensions from media manager options.
     $legalExtensions = explode(",", $params->get("legal_extensions"));
     $legalExtensions = array_map("trim", $legalExtensions);
     $typeValidator->setLegalExtensions($legalExtensions);
     $file->addValidator($sizeValidator)->addValidator($typeValidator)->addValidator($serverValidator);
     // Validate the file
     if (!$file->isValid()) {
         throw new RuntimeException($file->getError());
     }
     // Generate temporary file name
     $ext = JFile::makeSafe(JFile::getExt($tmpFile['name']));
     // Generate a file name.
     $generatedName = new ITPrismString();
     $generatedName->generateRandomString();
     $tmpDestFile = JPath::clean($destFolder . DIRECTORY_SEPARATOR . $generatedName . "." . $ext);
     // Prepare uploader object.
     $uploader = new ITPrismFileUploaderLocal($uploadedFile);
     $uploader->setDestination($tmpDestFile);
     // Upload temporary file
     $file->setUploader($uploader);
     $file->upload();
     // Get file
     $tmpDestFile = JPath::clean($file->getFile());
     if (!is_file($tmpDestFile)) {
         throw new RuntimeException(JText::_('COM_IDENTITYPROOF_ERROR_FILE_CANT_BE_UPLOADED'));
     }
     // Set new permissions for the file.
     chmod($tmpDestFile, 0600);
     // Remove the temporary file that came from the form.
     if (JFile::exists($uploadedFile)) {
         JFile::delete($uploadedFile);
     }
     return $tmpDestFile;
 }
コード例 #3
0
ファイル: story.php プロジェクト: phpsource/CrowdFunding
 public function uploadExtraImages($files, $options)
 {
     $images = array();
     $destination = JArrayHelper::getValue($options, "destination", "images/crowdfunding");
     jimport("itprism.file");
     jimport("itprism.file.image");
     jimport("itprism.file.uploader.local");
     jimport("itprism.file.validator.size");
     jimport("itprism.file.validator.image");
     jimport("itprism.file.validator.server");
     jimport("itprism.string");
     // Joomla! media extension parameters
     $mediaParams = JComponentHelper::getParams("com_media");
     /** @var  $mediaParams Joomla\Registry\Registry */
     // check for error
     foreach ($files as $image) {
         // Upload image
         if (!empty($image['name'])) {
             $uploadedFile = JArrayHelper::getValue($image, 'tmp_name');
             $uploadedName = JArrayHelper::getValue($image, 'name');
             $errorCode = JArrayHelper::getValue($image, 'error');
             $file = new ITPrismFile();
             // Prepare size validator.
             $KB = 1024 * 1024;
             $fileSize = JArrayHelper::getValue($image, "size");
             $uploadMaxSize = $mediaParams->get("upload_maxsize") * $KB;
             // Prepare file size validator
             $sizeValidator = new ITPrismFileValidatorSize($fileSize, $uploadMaxSize);
             // Prepare server validator.
             $serverValidator = new ITPrismFileValidatorServer($errorCode, array(UPLOAD_ERR_NO_FILE));
             // Prepare image validator.
             $imageValidator = new ITPrismFileValidatorImage($uploadedFile, $uploadedName);
             // Get allowed mime types from media manager options
             $mimeTypes = explode(",", $mediaParams->get("upload_mime"));
             $imageValidator->setMimeTypes($mimeTypes);
             // Get allowed image extensions from media manager options
             $imageExtensions = explode(",", $mediaParams->get("image_extensions"));
             $imageValidator->setImageExtensions($imageExtensions);
             $file->addValidator($sizeValidator)->addValidator($imageValidator)->addValidator($serverValidator);
             // Validate the file
             if (!$file->isValid()) {
                 throw new RuntimeException($file->getError());
             }
             // Generate file name
             $ext = JString::strtolower(JFile::makeSafe(JFile::getExt($image['name'])));
             $generatedName = new ITPrismString();
             $generatedName->generateRandomString(6);
             $tmpDestFile = $destination . DIRECTORY_SEPARATOR . $generatedName . "_extra." . $ext;
             // Prepare uploader object.
             $uploader = new ITPrismFileUploaderLocal($uploadedFile);
             $uploader->setDestination($tmpDestFile);
             // Upload temporary file
             $file->setUploader($uploader);
             $file->upload();
             // Get file
             $imageSource = $file->getFile();
             if (!JFile::exists($imageSource)) {
                 throw new RuntimeException(JText::_("COM_CROWDFUNDING_ERROR_FILE_CANT_BE_UPLOADED"));
             }
             // Create thumbnail
             $fileImage = new ITPrismFileImage($imageSource);
             $options["destination"] = $destination . DIRECTORY_SEPARATOR . $generatedName . "_extra_thumb." . $ext;
             $thumbSource = $fileImage->createThumbnail($options);
             $names = array("image" => "", "thumb" => "");
             $names['image'] = basename($imageSource);
             $names["thumb"] = basename($thumbSource);
             $images[] = $names;
         }
     }
     return $images;
 }
コード例 #4
0
ファイル: import.php プロジェクト: phpsource/CrowdFunding
 public function uploadFile($fileData, $type)
 {
     $app = JFactory::getApplication();
     /** @var $app JApplicationAdministrator */
     jimport('joomla.filesystem.archive');
     jimport('itprism.file');
     jimport('itprism.file.uploader.local');
     jimport('itprism.file.validator.size');
     jimport('itprism.file.validator.server');
     $uploadedFile = JArrayHelper::getValue($fileData, 'tmp_name');
     $uploadedName = JArrayHelper::getValue($fileData, 'name');
     $errorCode = JArrayHelper::getValue($fileData, 'error');
     $destination = JPath::clean($app->get("tmp_path")) . DIRECTORY_SEPARATOR . JFile::makeSafe($uploadedName);
     $file = new ITPrismFile();
     // Prepare size validator.
     $KB = 1024 * 1024;
     $fileSize = (int) $app->input->server->get('CONTENT_LENGTH');
     $mediaParams = JComponentHelper::getParams("com_media");
     /** @var $mediaParams Joomla\Registry\Registry */
     $uploadMaxSize = $mediaParams->get("upload_maxsize") * $KB;
     // Prepare size validator.
     $sizeValidator = new ITPrismFileValidatorSize($fileSize, $uploadMaxSize);
     // Prepare server validator.
     $serverValidator = new ITPrismFileValidatorServer($errorCode, array(UPLOAD_ERR_NO_FILE));
     $file->addValidator($sizeValidator);
     $file->addValidator($serverValidator);
     // Validate the file
     if (!$file->isValid()) {
         throw new RuntimeException($file->getError());
     }
     // Prepare uploader object.
     $uploader = new ITPrismFileUploaderLocal($uploadedFile);
     $uploader->setDestination($destination);
     // Upload the file
     $file->setUploader($uploader);
     $file->upload();
     $fileName = basename($destination);
     // Extract file if it is archive
     $ext = JString::strtolower(JFile::getExt($fileName));
     if (strcmp($ext, "zip") == 0) {
         $destFolder = JPath::clean($app->get("tmp_path")) . "/" . $type;
         if (is_dir($destFolder)) {
             JFolder::delete($destFolder);
         }
         $filePath = $this->extractFile($destination, $destFolder);
     } else {
         $filePath = $destination;
     }
     return $filePath;
 }