Exemplo n.º 1
0
 public function uploadFiles($files, $options)
 {
     $result = array();
     $destination = JArrayHelper::getValue($options, "destination");
     $maxSize = JArrayHelper::getValue($options, "max_size");
     $legalExtensions = JArrayHelper::getValue($options, "legal_extensions");
     $legalFileTypes = JArrayHelper::getValue($options, "legal_types");
     // check for error
     foreach ($files as $fileData) {
         // Upload image
         if (!empty($fileData['name'])) {
             $uploadedFile = JArrayHelper::getValue($fileData, 'tmp_name');
             $uploadedName = JArrayHelper::getValue($fileData, 'name');
             $errorCode = JArrayHelper::getValue($fileData, 'error');
             $file = new Prism\File\File();
             // Prepare size validator.
             $KB = 1024 * 1024;
             $fileSize = JArrayHelper::getValue($fileData, "size");
             $uploadMaxSize = $maxSize * $KB;
             // Prepare file size validator
             $sizeValidator = new Prism\File\Validator\Size($fileSize, $uploadMaxSize);
             // Prepare server validator.
             $serverValidator = new Prism\File\Validator\Server($errorCode, array(UPLOAD_ERR_NO_FILE));
             // Prepare image validator.
             $typeValidator = new Prism\File\Validator\Type($uploadedFile, $uploadedName);
             // Get allowed MIME types.
             $mimeTypes = explode(",", $legalFileTypes);
             $mimeTypes = array_map('trim', $mimeTypes);
             $typeValidator->setMimeTypes($mimeTypes);
             // Get allowed file extensions.
             $fileExtensions = explode(",", $legalExtensions);
             $fileExtensions = array_map('trim', $fileExtensions);
             $typeValidator->setLegalExtensions($fileExtensions);
             $file->addValidator($sizeValidator)->addValidator($typeValidator)->addValidator($serverValidator);
             // Validate the file
             if (!$file->isValid()) {
                 throw new RuntimeException($file->getError());
             }
             // Generate file name
             $baseName = JString::strtolower(JFile::makeSafe(basename($fileData['name'])));
             $ext = JFile::getExt($baseName);
             $generatedName = new Prism\String();
             $generatedName->generateRandomString(6);
             $destinationFile = $destination . DIRECTORY_SEPARATOR . $generatedName . "." . $ext;
             // Prepare uploader object.
             $uploader = new Prism\File\Uploader\Local($uploadedFile);
             $uploader->setDestination($destinationFile);
             // Upload temporary file
             $file->setUploader($uploader);
             $file->upload();
             // Get file
             $fileSource = $file->getFile();
             if (!JFile::exists($fileSource)) {
                 throw new RuntimeException(JText::_("COM_CROWDFUNDING_ERROR_FILE_CANT_BE_UPLOADED"));
             }
             $result[] = array("title" => $baseName, "filename" => basename($fileSource));
         }
     }
     return $result;
 }
Exemplo n.º 2
0
 /**
  * Crop the image and generates smaller ones.
  *
  * @param string $file
  * @param array $options
  *
  * @throws Exception
  *
  * @return array
  */
 public function cropImage($file, $options)
 {
     // Resize image
     $image = new JImage();
     $image->loadFile($file);
     if (!$image->isLoaded()) {
         throw new Exception(JText::sprintf('COM_CROWDFUNDING_ERROR_FILE_NOT_FOUND', $file));
     }
     $destinationFolder = Joomla\Utilities\ArrayHelper::getValue($options, "destination");
     // Generate temporary file name
     $generatedName = new Prism\String();
     $generatedName->generateRandomString(32);
     $imageName = $generatedName . "_image.png";
     $smallName = $generatedName . "_small.png";
     $squareName = $generatedName . "_square.png";
     $imageFile = $destinationFolder . DIRECTORY_SEPARATOR . $imageName;
     $smallFile = $destinationFolder . DIRECTORY_SEPARATOR . $smallName;
     $squareFile = $destinationFolder . DIRECTORY_SEPARATOR . $squareName;
     // Create main image
     $width = Joomla\Utilities\ArrayHelper::getValue($options, "width", 200);
     $width = $width < 25 ? 50 : $width;
     $height = Joomla\Utilities\ArrayHelper::getValue($options, "height", 200);
     $height = $height < 25 ? 50 : $height;
     $left = Joomla\Utilities\ArrayHelper::getValue($options, "x", 0);
     $top = Joomla\Utilities\ArrayHelper::getValue($options, "y", 0);
     $image->crop($width, $height, $left, $top, false);
     // Resize to general size.
     $width = Joomla\Utilities\ArrayHelper::getValue($options, "resize_width", 200);
     $width = $width < 25 ? 50 : $width;
     $height = Joomla\Utilities\ArrayHelper::getValue($options, "resize_height", 200);
     $height = $height < 25 ? 50 : $height;
     $image->resize($width, $height, false);
     // Store to file.
     $image->toFile($imageFile, IMAGETYPE_PNG);
     // Load parameters.
     $params = JComponentHelper::getParams($this->option);
     /** @var  $params Joomla\Registry\Registry */
     // Create small image
     $width = $params->get("image_small_width", 100);
     $height = $params->get("image_small_height", 100);
     $image->resize($width, $height, false);
     $image->toFile($smallFile, IMAGETYPE_PNG);
     // Create square image
     $width = $params->get("image_square_width", 50);
     $height = $params->get("image_square_height", 50);
     $image->resize($width, $height, false);
     $image->toFile($squareFile, IMAGETYPE_PNG);
     $names = array("image" => $imageName, "image_small" => $smallName, "image_square" => $squareName);
     // Remove the temporary file.
     if (is_file($file)) {
         JFile::delete($file);
     }
     return $names;
 }
Exemplo n.º 3
0
 protected function prepareRewards(&$paymentSession)
 {
     // Create payment session ID.
     $sessionId = new Prism\String();
     $sessionId->generateRandomString(32);
     $paymentSession->session_id = (string) $sessionId;
     // Get selected reward ID
     $this->rewardId = $this->state->get("reward_id");
     // If it has been selected another reward, set the old one to 0.
     if ($this->rewardId != $paymentSession->rewardId) {
         $paymentSession->rewardId = 0;
         $paymentSession->step1 = false;
     }
     // Get amount from session
     $this->rewardAmount = !$paymentSession->amount ? 0 : $paymentSession->amount;
     // Get rewards
     $this->rewards = new Crowdfunding\Rewards(JFactory::getDbo());
     $this->rewards->load(array("project_id" => $this->item->id, "state" => 1));
     // Compare amount with the amount of reward, that is selected.
     // If the amount of selected reward is larger than amount from session,
     // use the amount of selected reward.
     if (!empty($this->rewardId)) {
         foreach ($this->rewards as $reward) {
             if ($this->rewardId == $reward["id"]) {
                 if ($this->rewardAmount < $reward["amount"]) {
                     $this->rewardAmount = $reward["amount"];
                     $paymentSession->step1 = false;
                 }
                 break;
             }
         }
     }
     // Store the new values of the payment process to the user session.
     $this->app->setUserState($this->paymentSessionContext, $paymentSession);
     if (!$this->fourSteps) {
         $this->secondStepTask = "backing.process";
     } else {
         $this->secondStepTask = "backing.step2";
     }
 }
Exemplo n.º 4
0
 public function process()
 {
     $app = JFactory::getApplication();
     /** @var $app JApplicationSite */
     // Check for request forgeries.
     $requestMethod = $app->input->getMethod();
     if (strcmp("POST", $requestMethod) == 0) {
         JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
     } else {
         JSession::checkToken("get") or jexit(JText::_('JINVALID_TOKEN'));
     }
     // Get params
     $params = JComponentHelper::getParams("com_crowdfunding");
     /** @var  $params Joomla\Registry\Registry */
     // Get the data from the form
     $itemId = $this->input->getInt('id', 0);
     $rewardId = $this->input->getInt('rid', 0);
     // Get amount
     $amount = CrowdfundingHelper::parseAmount($this->input->getString("amount"));
     // Get user ID
     $user = JFactory::getUser();
     $userId = (int) $user->get("id");
     // Anonymous user ID
     $aUserId = "";
     $model = $this->getModel();
     /** @var $model CrowdfundingModelBacking */
     // Get the item
     $item = $model->getItem($itemId);
     $returnUrl = CrowdfundingHelperRoute::getBackingRoute($item->slug, $item->catslug);
     // Authorise the user
     if (!$user->authorise("crowdfunding.donate", "com_crowdfunding")) {
         $this->setRedirect(JRoute::_($returnUrl, false), JText::_('COM_CROWDFUNDING_ERROR_NO_PERMISSIONS'), "notice");
         return;
     }
     // Check for valid project
     if (empty($item->id)) {
         $this->setRedirect(JRoute::_(CrowdfundingHelperRoute::getDiscoverRoute()), JText::_('COM_CROWDFUNDING_ERROR_INVALID_PROJECT'), "notice");
         return;
     }
     // Check for maintenance (debug) state.
     if ($params->get("debug_payment_disabled", 0)) {
         $msg = Joomla\String\String::trim($params->get("debug_disabled_functionality_msg"));
         if (!$msg) {
             $msg = JText::_("COM_CROWDFUNDING_DEBUG_MODE_DEFAULT_MSG");
         }
         $this->setRedirect(JRoute::_($returnUrl, false), $msg, "notice");
         return;
     }
     // Check for agreed conditions from the user.
     if ($params->get("backing_terms", 0)) {
         $terms = $this->input->get("terms", 0, "int");
         if (!$terms) {
             $this->setRedirect(JRoute::_($returnUrl, false), JText::_("COM_CROWDFUNDING_ERROR_TERMS_NOT_ACCEPTED"), "notice");
             return;
         }
     }
     // Check for valid amount.
     if (!$amount) {
         $this->setRedirect(JRoute::_($returnUrl, false), JText::_("COM_CROWDFUNDING_ERROR_INVALID_AMOUNT"), "notice");
         return;
     }
     // Store payment process data
     // Get the payment process object and
     // store the selected data from the user.
     $paymentSessionContext = Crowdfunding\Constants::PAYMENT_SESSION_CONTEXT . $item->id;
     $paymentSessionLocal = $app->getUserState($paymentSessionContext);
     $paymentSessionLocal->step1 = true;
     $paymentSessionLocal->amount = $amount;
     $paymentSessionLocal->rewardId = $rewardId;
     $app->setUserState($paymentSessionContext, $paymentSessionLocal);
     // Generate hash user ID used for anonymous payment.
     if (!$userId) {
         $aUserId = $app->getUserState("auser_id");
         if (!$aUserId) {
             // Generate a hash ID for anonymous user.
             $anonymousUserId = new Prism\String();
             $anonymousUserId->generateRandomString(32);
             $aUserId = (string) $anonymousUserId;
             $app->setUserState("auser_id", $aUserId);
         }
     }
     $date = new JDate();
     // Create an intention record.
     $intentionId = 0;
     if (!empty($userId)) {
         $intentionKeys = array("user_id" => $userId, "project_id" => $item->id);
         $intention = new Crowdfunding\Intention(JFactory::getDbo());
         $intention->load($intentionKeys);
         $intentionData = array("user_id" => $userId, "project_id" => $item->id, "reward_id" => $rewardId, "record_date" => $date->toSql());
         $intention->bind($intentionData);
         $intention->store();
         $intentionId = $intention->getId();
     }
     // Create a payment session.
     $paymentSessionDatabase = new Crowdfunding\Payment\Session(JFactory::getDbo());
     $paymentSessionData = array("user_id" => $userId, "auser_id" => $aUserId, "project_id" => $item->id, "reward_id" => $rewardId, "record_date" => $date->toSql(), "session_id" => $paymentSessionLocal->session_id, "intention_id" => $intentionId);
     $paymentSessionDatabase->bind($paymentSessionData);
     $paymentSessionDatabase->store();
     // Redirect to next page
     $link = CrowdfundingHelperRoute::getBackingRoute($item->slug, $item->catslug, "payment");
     $this->setRedirect(JRoute::_($link, false));
 }
Exemplo n.º 5
0
 /**
  * Upload images.
  *
  * @param  array $files
  * @param  string $destFolder
  * @param  array $rewardsIds
  *
  * @return array
  */
 public function uploadImages($files, $destFolder, $rewardsIds)
 {
     // Load parameters.
     $params = JComponentHelper::getParams($this->option);
     /** @var  $params Joomla\Registry\Registry */
     // Joomla! media extension parameters
     $mediaParams = JComponentHelper::getParams("com_media");
     /** @var  $mediaParams Joomla\Registry\Registry */
     $KB = 1024 * 1024;
     $uploadMaxSize = $mediaParams->get("upload_maxsize") * $KB;
     $mimeTypes = explode(",", $mediaParams->get("upload_mime"));
     $imageExtensions = explode(",", $mediaParams->get("image_extensions"));
     $images = array();
     foreach ($files as $rewardId => $image) {
         // If the image is set to not valid reward, continue to next one.
         // It is impossible to store image to missed reward.
         if (!in_array($rewardId, $rewardsIds)) {
             continue;
         }
         $uploadedFile = Joomla\Utilities\ArrayHelper::getValue($image, 'tmp_name');
         $uploadedName = Joomla\String\String::trim(Joomla\Utilities\ArrayHelper::getValue($image, 'name'));
         $errorCode = Joomla\Utilities\ArrayHelper::getValue($image, 'error');
         $file = new Prism\File\Image();
         if (!empty($uploadedName)) {
             // Prepare size validator.
             $fileSize = (int) Joomla\Utilities\ArrayHelper::getValue($image, 'size');
             // Prepare file size validator.
             $sizeValidator = new Prism\File\Validator\Size($fileSize, $uploadMaxSize);
             // Prepare server validator.
             $serverValidator = new Prism\File\Validator\Server($errorCode, array(UPLOAD_ERR_NO_FILE));
             // Prepare image validator.
             $imageValidator = new Prism\File\Validator\Image($uploadedFile, $uploadedName);
             // Get allowed mime types from media manager options
             $imageValidator->setMimeTypes($mimeTypes);
             // Get allowed image extensions from media manager options
             $imageValidator->setImageExtensions($imageExtensions);
             $file->addValidator($sizeValidator)->addValidator($imageValidator)->addValidator($serverValidator);
             // Validate the file
             if (!$file->isValid()) {
                 continue;
             }
             // Generate temporary file name
             $ext = Joomla\String\String::strtolower(JFile::makeSafe(JFile::getExt($image['name'])));
             $generatedName = new Prism\String();
             $generatedName->generateRandomString(12, "reward_");
             $destFile = JPath::clean($destFolder . DIRECTORY_SEPARATOR . $generatedName . "." . $ext);
             // Prepare uploader object.
             $uploader = new Prism\File\Uploader\Local($uploadedFile);
             $uploader->setDestination($destFile);
             // Upload temporary file
             $file->setUploader($uploader);
             $file->upload();
             // Get file
             $imageSource = $file->getFile();
             if (!is_file($imageSource)) {
                 continue;
             }
             // Generate thumbnails.
             // Create thumbnail.
             $generatedName->generateRandomString(12, "reward_thumb_");
             $options = array("width" => $params->get("rewards_image_thumb_width", 200), "height" => $params->get("rewards_image_thumb_height", 200), "scale" => $params->get("image_resizing_scale", JImage::SCALE_INSIDE), "destination" => JPath::clean($destFolder . DIRECTORY_SEPARATOR . $generatedName . "." . $ext));
             $thumbSource = $file->createThumbnail($options);
             // Create square image.
             $generatedName->generateRandomString(12, "reward_square_");
             $options = array("width" => $params->get("rewards_image_square_width", 50), "height" => $params->get("rewards_image_square_height", 50), "scale" => $params->get("image_resizing_scale", JImage::SCALE_INSIDE), "destination" => JPath::clean($destFolder . DIRECTORY_SEPARATOR . $generatedName . "." . $ext));
             $squareSource = $file->createThumbnail($options);
             $names = array("image" => "", "thumb" => "", "square" => "");
             $names['image'] = basename($imageSource);
             $names["thumb"] = basename($thumbSource);
             $names["square"] = basename($squareSource);
             $images[$rewardId] = $names;
         }
     }
     return $images;
 }
Exemplo n.º 6
0
 /**
  * Check for duplication of session ID.
  * If the session ID exists, generate new one.
  *
  * @param object $item
  */
 protected function prepareSessionId(&$item)
 {
     // Get the payment session object and session ID.
     $paymentSessionContext = Crowdfunding\Constants::PAYMENT_SESSION_CONTEXT . $item->id;
     $paymentSession = $this->app->getUserState($paymentSessionContext);
     $db = JFactory::getDbo();
     $query = $db->getQuery(true);
     $query->select("COUNT(*)")->from($db->quoteName("#__cfdata_records", "a"))->where("a.session_id = " . $db->quote($paymentSession->session_id));
     $db->setQuery($query, 0, 1);
     $result = $db->loadResult();
     if (!empty($result)) {
         // Create payment session ID.
         $sessionId = new Prism\String();
         $sessionId->generateRandomString(32);
         $paymentSession->session_id = (string) $sessionId;
         $this->app->setUserState($paymentSessionContext, $paymentSession);
     }
 }
Exemplo n.º 7
0
 /**
  * Upload a pitch image.
  *
  * @param  array $image
  *
  * @throws Exception
  *
  * @return array
  */
 public function uploadPitchImage($image)
 {
     $app = JFactory::getApplication();
     /** @var $app JApplicationSite */
     $uploadedFile = Joomla\Utilities\ArrayHelper::getValue($image, 'tmp_name');
     $uploadedName = Joomla\Utilities\ArrayHelper::getValue($image, 'name');
     $errorCode = Joomla\Utilities\ArrayHelper::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 */
     $file = new Prism\File\File();
     // Prepare size validator.
     $KB = 1024 * 1024;
     $fileSize = (int) $app->input->server->get('CONTENT_LENGTH');
     $uploadMaxSize = $mediaParams->get("upload_maxsize") * $KB;
     $sizeValidator = new Prism\File\Validator\Size($fileSize, $uploadMaxSize);
     // Prepare server validator.
     $serverValidator = new Prism\File\Validator\Server($errorCode, array(UPLOAD_ERR_NO_FILE));
     // Prepare image validator.
     $imageValidator = new Prism\File\Validator\Image($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 = Joomla\String\String::strtolower(JFile::makeSafe(JFile::getExt($image['name'])));
     $generatedName = new Prism\String();
     $generatedName->generateRandomString(32);
     $tmpDestFile = $tmpFolder . DIRECTORY_SEPARATOR . $generatedName . "." . $ext;
     // Prepare uploader object.
     $uploader = new Prism\File\Uploader\Local($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;
 }
Exemplo n.º 8
0
 /**
  * Upload an image.
  *
  * @param  array $image
  * @param  string $destFolder
  *
  * @throws RuntimeException
  *
  * @return array
  */
 public function uploadImage($image, $destFolder)
 {
     // Load parameters.
     $params = JComponentHelper::getParams($this->option);
     /** @var  $params Joomla\Registry\Registry */
     // Joomla! media extension parameters
     $mediaParams = JComponentHelper::getParams("com_media");
     /** @var  $mediaParams Joomla\Registry\Registry */
     $names = array("image" => "", "thumb" => "", "square" => "");
     $KB = 1024 * 1024;
     $uploadMaxSize = $mediaParams->get("upload_maxsize") * $KB;
     $mimeTypes = explode(",", $mediaParams->get("upload_mime"));
     $imageExtensions = explode(",", $mediaParams->get("image_extensions"));
     $uploadedFile = JArrayHelper::getValue($image, 'tmp_name');
     $uploadedName = Joomla\String\String::trim(JArrayHelper::getValue($image, 'name'));
     $errorCode = JArrayHelper::getValue($image, 'error');
     $file = new Prism\File\Image();
     if (!empty($uploadedName)) {
         // Prepare size validator.
         $fileSize = (int) JArrayHelper::getValue($image, 'size');
         // Prepare file size validator.
         $sizeValidator = new Prism\File\Validator\Size($fileSize, $uploadMaxSize);
         // Prepare server validator.
         $serverValidator = new Prism\File\Validator\Server($errorCode, array(UPLOAD_ERR_NO_FILE));
         // Prepare image validator.
         $imageValidator = new Prism\File\Validator\Image($uploadedFile, $uploadedName);
         // Get allowed mime types from media manager options
         $imageValidator->setMimeTypes($mimeTypes);
         // Get allowed image extensions from media manager options
         $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 = Joomla\String\String::strtolower(JFile::makeSafe(JFile::getExt($image['name'])));
         $generatedName = new Prism\String();
         $generatedName->generateRandomString(12, "reward_");
         $destFile = JPath::clean($destFolder . DIRECTORY_SEPARATOR . $generatedName . "." . $ext);
         // Prepare uploader object.
         $uploader = new Prism\File\Uploader\Local($uploadedFile);
         $uploader->setDestination($destFile);
         // Upload temporary file
         $file->setUploader($uploader);
         $file->upload();
         // Get file
         $imageSource = $file->getFile();
         if (!is_file($imageSource)) {
             throw new RuntimeException(JText::_("COM_CROWDFUNDING_ERROR_FILE_CANT_BE_UPLOADED"));
         }
         // Generate thumbnails.
         // Create thumbnail.
         $generatedName->generateRandomString(12, "reward_thumb_");
         $options = array("width" => $params->get("rewards_image_thumb_width", 200), "height" => $params->get("rewards_image_thumb_height", 200), "destination" => JPath::clean($destFolder . DIRECTORY_SEPARATOR . $generatedName . "." . $ext));
         $thumbSource = $file->createThumbnail($options);
         // Create square image.
         $generatedName->generateRandomString(12, "reward_square_");
         $options = array("width" => $params->get("rewards_image_square_width", 50), "height" => $params->get("rewards_image_square_height", 50), "destination" => JPath::clean($destFolder . DIRECTORY_SEPARATOR . $generatedName . "." . $ext));
         $squareSource = $file->createThumbnail($options);
         $names['image'] = basename($imageSource);
         $names["thumb"] = basename($thumbSource);
         $names["square"] = basename($squareSource);
     }
     return $names;
 }
Exemplo n.º 9
0
 /**
  * Store the file in a folder of the extension.
  *
  * @param array $image
  *
  * @return string
  */
 public function uploadImage($image)
 {
     $app = JFactory::getApplication();
     /** @var $app JApplicationSite */
     $uploadedFile = Joomla\Utilities\ArrayHelper::getValue($image, 'tmp_name');
     $uploadedName = Joomla\Utilities\ArrayHelper::getValue($image, 'name');
     $errorCode = Joomla\Utilities\ArrayHelper::getValue($image, 'error');
     $params = JComponentHelper::getParams($this->option);
     /** @var  $params Joomla\Registry\Registry */
     $destinationFolder = JPath::clean(JPATH_ROOT . DIRECTORY_SEPARATOR . $params->get("images_directory", "images/gamification"));
     // Joomla! media extension parameters
     $mediaParams = JComponentHelper::getParams("com_media");
     /** @var $mediaParams Joomla\Registry\Registry */
     $file = new Prism\File\File();
     // Prepare size validator.
     $KB = 1024 * 1024;
     $fileSize = (int) $app->input->server->get('CONTENT_LENGTH');
     $uploadMaxSize = $mediaParams->get("upload_maxsize") * $KB;
     // Prepare file validators.
     $sizeValidator = new Prism\File\Validator\Size($fileSize, $uploadMaxSize);
     $serverValidator = new Prism\File\Validator\Server($errorCode, array(UPLOAD_ERR_NO_FILE));
     $imageValidator = new Prism\File\Validator\Image($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($serverValidator)->addValidator($imageValidator);
     // Validate the file
     if (!$file->isValid()) {
         throw new RuntimeException($file->getError());
     }
     // Generate temporary file name
     $ext = Joomla\String\String::strtolower(JFile::makeSafe(JFile::getExt($image['name'])));
     $generatedName = new Prism\String();
     $generatedName->generateRandomString(16);
     $imageName = $generatedName . "_badge." . $ext;
     $destination = JPath::clean($destinationFolder . DIRECTORY_SEPARATOR . $imageName);
     // Prepare uploader object.
     $uploader = new Prism\File\Uploader\Local($uploadedFile);
     $uploader->setDestination($destination);
     // Upload temporary file
     $file->setUploader($uploader);
     $file->upload();
     $source = $file->getFile();
     return basename($source);
 }
Exemplo n.º 10
0
 /**
  * Upload an image
  *
  * @param array $image Array with information about uploaded file.
  *
  * @throws RuntimeException
  * @return array
  */
 public function uploadImage($image)
 {
     $app = JFactory::getApplication();
     /** @var $app JApplicationAdministrator */
     $uploadedFile = Joomla\Utilities\ArrayHelper::getValue($image, 'tmp_name');
     $uploadedName = Joomla\Utilities\ArrayHelper::getValue($image, 'name');
     $errorCode = Joomla\Utilities\ArrayHelper::getValue($image, 'error');
     $tmpFolder = $app->get("tmp_path");
     /** @var  $params Joomla\Registry\Registry */
     $params = JComponentHelper::getParams($this->option);
     $destFolder = JPath::clean(JPATH_ROOT . DIRECTORY_SEPARATOR . $params->get("images_directory", "/images/profiles"));
     $options = array("width" => $params->get("image_width"), "height" => $params->get("image_height"), "small_width" => $params->get("image_small_width"), "small_height" => $params->get("image_small_height"), "square_width" => $params->get("image_square_width"), "square_height" => $params->get("image_square_height"), "icon_width" => $params->get("image_icon_width"), "icon_height" => $params->get("image_icon_height"));
     // Joomla! media extension parameters
     /** @var  $mediaParams Joomla\Registry\Registry */
     $mediaParams = JComponentHelper::getParams("com_media");
     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 Prism\File\File();
     // Prepare size validator.
     $KB = 1024 * 1024;
     $fileSize = (int) $app->input->server->get('CONTENT_LENGTH');
     $uploadMaxSize = $mediaParams->get("upload_maxsize") * $KB;
     // Prepare file size validator
     $sizeValidator = new Prism\File\Validator\Size($fileSize, $uploadMaxSize);
     // Prepare server validator.
     $serverValidator = new Prism\File\Validator\Server($errorCode, array(UPLOAD_ERR_NO_FILE));
     // Prepare image validator.
     $imageValidator = new Prism\File\Validator\Image($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 = JFile::makeSafe(JFile::getExt($image['name']));
     $generatedName = new Prism\String();
     $generatedName->generateRandomString(32);
     $tmpDestFile = $tmpFolder . DIRECTORY_SEPARATOR . $generatedName . "." . $ext;
     // Prepare uploader object.
     $uploader = new Prism\File\Uploader\Local($uploadedFile);
     $uploader->setDestination($tmpDestFile);
     // Upload temporary file
     $file->setUploader($uploader);
     $file->upload();
     // Get file
     $tmpSourceFile = $file->getFile();
     if (!is_file($tmpSourceFile)) {
         throw new RuntimeException('COM_SOCIALCOMMUNITY_ERROR_FILE_CANT_BE_UPLOADED');
     }
     // Generate file names for the image files.
     $generatedName->generateRandomString(32);
     $imageName = $generatedName . "_image.png";
     $smallName = $generatedName . "_small.png";
     $squareName = $generatedName . "_square.png";
     $iconName = $generatedName . "_icon.png";
     // Resize image
     $image = new JImage();
     $image->loadFile($tmpSourceFile);
     if (!$image->isLoaded()) {
         throw new RuntimeException(JText::sprintf('COM_SOCIALCOMMUNITY_ERROR_FILE_NOT_FOUND', $tmpSourceFile));
     }
     $imageFile = $destFolder . DIRECTORY_SEPARATOR . $imageName;
     $smallFile = $destFolder . DIRECTORY_SEPARATOR . $smallName;
     $squareFile = $destFolder . DIRECTORY_SEPARATOR . $squareName;
     $iconFile = $destFolder . DIRECTORY_SEPARATOR . $iconName;
     // Create profile picture
     $width = Joomla\Utilities\ArrayHelper::getValue($options, "image_width", 200);
     $height = Joomla\Utilities\ArrayHelper::getValue($options, "image_height", 200);
     $image->resize($width, $height, false);
     $image->toFile($imageFile, IMAGETYPE_PNG);
     // Create small profile picture
     $width = Joomla\Utilities\ArrayHelper::getValue($options, "small_width", 100);
     $height = Joomla\Utilities\ArrayHelper::getValue($options, "small_height", 100);
     $image->resize($width, $height, false);
     $image->toFile($smallFile, IMAGETYPE_PNG);
     // Create square picture
     $width = Joomla\Utilities\ArrayHelper::getValue($options, "square_width", 50);
     $height = Joomla\Utilities\ArrayHelper::getValue($options, "square_height", 50);
     $image->resize($width, $height, false);
     $image->toFile($squareFile, IMAGETYPE_PNG);
     // Create icon picture
     $width = Joomla\Utilities\ArrayHelper::getValue($options, "icon_width", 24);
     $height = Joomla\Utilities\ArrayHelper::getValue($options, "icon_height", 24);
     $image->resize($width, $height, false);
     $image->toFile($iconFile, IMAGETYPE_PNG);
     // Remove the temporary file
     if (JFile::exists($tmpSourceFile)) {
         JFile::delete($tmpSourceFile);
     }
     return $names = array("image" => $imageName, "image_small" => $smallName, "image_icon" => $iconName, "image_square" => $squareName);
 }
Exemplo n.º 11
0
 /**
  * Process PAY notification data from PayPal.
  * This method updates transaction record.
  *
  * @param array $result
  * @param string $url  The parameters of the component
  * @param bool $loadCertificate
  * @param Joomla\Registry\Registry $params  The parameters of the component
  *
  * @return null|array
  */
 protected function processPay(&$result, $url, $loadCertificate, &$params)
 {
     // Get raw post data and parse it.
     $rowPostString = file_get_contents("php://input");
     $string = new Prism\String($rowPostString);
     $rawPost = $string->parseNameValue();
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_RESPONSE"), $this->debugType, $_POST) : null;
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_RESPONSE_INPUT"), $this->debugType, $rawPost) : null;
     $paypalIpn = new Prism\Payment\PayPal\Ipn($url, $rawPost);
     $paypalIpn->verify($loadCertificate);
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_IPN_OBJECT"), $this->debugType, $paypalIpn) : null;
     if ($paypalIpn->isVerified()) {
         // Parse raw post transaction data.
         $rawPostTransaction = $paypalIpn->getTransactionData();
         if (!empty($rawPostTransaction)) {
             $_POST["transaction"] = $this->filterRawPostTransaction($rawPostTransaction);
         }
         JDEBUG ? $this->log->add(JText::_("PLG_CROWDFUNDINGPAYMENT_PAYPALADAPTIVE_DEBUG_FILTERED_RAW_POST"), $this->debugType, $_POST) : null;
         unset($rawPostTransaction);
         unset($rawPost);
         $preApprovalKey = Joomla\Utilities\ArrayHelper::getValue($_POST, "preapproval_key");
         // Validate transaction data
         $this->updateTransactionDataOnPay($_POST, $preApprovalKey);
     } else {
         // Log error
         $this->log->add(JText::_($this->textPrefix . "_ERROR_INVALID_TRANSACTION_DATA"), $this->debugType, array("error message" => $paypalIpn->getError(), "paypalIPN" => $paypalIpn, "_POST" => $_POST, "RAW POST" => $rawPost));
     }
     return $result;
 }
Exemplo n.º 12
0
 /**
  * This method performs the transaction.
  *
  * @param string $context
  * @param Joomla\Registry\Registry $params
  *
  * @return null|array
  */
 public function onPaymentNotify($context, &$params)
 {
     if (strcmp("com_crowdfunding.notify.banktransfer", $context) != 0) {
         return null;
     }
     if ($this->app->isAdmin()) {
         return null;
     }
     $doc = JFactory::getDocument();
     /**  @var $doc JDocumentHtml */
     // Check document type
     $docType = $doc->getType();
     if (strcmp("raw", $docType) != 0) {
         return null;
     }
     $projectId = $this->app->input->getInt("pid");
     $amount = $this->app->input->getFloat("amount");
     // Prepare the array that will be returned by this method
     $result = array("project" => null, "reward" => null, "transaction" => null, "payment_session" => null, "redirect_url" => null, "message" => null);
     // Get project
     $project = new Crowdfunding\Project(JFactory::getDbo());
     $project->load($projectId);
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_PROJECT_OBJECT"), $this->debugType, $project->getProperties()) : null;
     // Check for valid project
     if (!$project->getId()) {
         // Log data in the database
         $this->log->add(JText::_($this->textPrefix . "_ERROR_INVALID_PROJECT"), $this->debugType, array("PROJECT OBJECT" => $project->getProperties(), "REQUEST METHOD" => $this->app->input->getMethod(), "_REQUEST" => $_REQUEST));
         return null;
     }
     $currencyId = $params->get("project_currency");
     $currency = Crowdfunding\Currency::getInstance(JFactory::getDbo(), $currencyId, $params);
     // Prepare return URL
     $result["redirect_url"] = Joomla\String\String::trim($this->params->get('return_url'));
     if (!$result["redirect_url"]) {
         $filter = JFilterInput::getInstance();
         $uri = JUri::getInstance();
         $domain = $filter->clean($uri->toString(array("scheme", "host")));
         $result["redirect_url"] = $domain . JRoute::_(CrowdfundingHelperRoute::getBackingRoute($project->getSlug(), $project->getCatslug(), "share"), false);
     }
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_RETURN_URL"), $this->debugType, $result["redirect_url"]) : null;
     // Payment Session
     $userId = JFactory::getUser()->get("id");
     $aUserId = $this->app->getUserState("auser_id");
     // Reset anonymous user hash ID,
     // because the payment session based in it will be removed when transaction completes.
     if (!empty($aUserId)) {
         $this->app->setUserState("auser_id", "");
     }
     $paymentSessionContext = Crowdfunding\Constants::PAYMENT_SESSION_CONTEXT . $project->getId();
     $paymentSessionLocal = $this->app->getUserState($paymentSessionContext);
     $paymentSession = $this->getPaymentSession(array("session_id" => $paymentSessionLocal->session_id));
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_PAYMENT_SESSION_OBJECT"), $this->debugType, $paymentSession->getProperties()) : null;
     // Validate payment session record.
     if (!$paymentSession->getId()) {
         // Log data in the database
         $this->log->add(JText::_($this->textPrefix . "_ERROR_INVALID_PAYMENT_SESSION"), $this->debugType, $paymentSession->getProperties());
         // Send response to the browser
         $response = array("success" => false, "title" => JText::_($this->textPrefix . "_FAIL"), "text" => JText::_($this->textPrefix . "_ERROR_INVALID_PROJECT"));
         return $response;
     }
     // Validate a reward and update the number of distributed ones.
     // If the user is anonymous, the system will store 0 for reward ID.
     // The anonymous users can't select rewards.
     $rewardId = $paymentSession->isAnonymous() ? 0 : (int) $paymentSession->getRewardId();
     if (!empty($rewardId)) {
         $validData = array("reward_id" => $rewardId, "project_id" => $projectId, "txn_amount" => $amount);
         $reward = $this->updateReward($validData);
         // Validate the reward.
         if (!$reward) {
             $rewardId = 0;
         }
     }
     // Prepare transaction data
     $transactionId = new Prism\String();
     $transactionId->generateRandomString(12, "BT");
     $transactionId = Joomla\String\String::strtoupper($transactionId);
     $transactionData = array("txn_amount" => $amount, "txn_currency" => $currency->getCode(), "txn_status" => "pending", "txn_id" => $transactionId, "project_id" => $projectId, "reward_id" => $rewardId, "investor_id" => (int) $userId, "receiver_id" => (int) $project->getUserId(), "service_provider" => "Bank Transfer");
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_TRANSACTION_DATA"), $this->debugType, $transactionData) : null;
     // Auto complete transaction
     if ($this->params->get("auto_complete", 0)) {
         $transactionData["txn_status"] = "completed";
         $project->addFunds($amount);
         $project->storeFunds();
     }
     // Store transaction data
     $transaction = new Crowdfunding\Transaction(JFactory::getDbo());
     $transaction->bind($transactionData);
     $transaction->store();
     // Generate object of data, based on the transaction properties.
     $properties = $transaction->getProperties();
     $result["transaction"] = Joomla\Utilities\ArrayHelper::toObject($properties);
     // Generate object of data, based on the project properties.
     $properties = $project->getProperties();
     $result["project"] = Joomla\Utilities\ArrayHelper::toObject($properties);
     // Generate object of data, based on the reward properties.
     if (!empty($reward)) {
         $properties = $reward->getProperties();
         $result["reward"] = Joomla\Utilities\ArrayHelper::toObject($properties);
     }
     // Generate data object, based on the payment session properties.
     $properties = $paymentSession->getProperties();
     $result["payment_session"] = Joomla\Utilities\ArrayHelper::toObject($properties);
     // Set message to the user.
     $result["message"] = JText::sprintf($this->textPrefix . "_TRANSACTION_REGISTERED", $transaction->getTransactionId(), $transaction->getTransactionId());
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_RESULT_DATA"), $this->debugType, $result) : null;
     // Close payment session and remove payment session record.
     $txnStatus = isset($result["transaction"]->txn_status) ? $result["transaction"]->txn_status : null;
     $this->closePaymentSession($paymentSession, $txnStatus);
     return $result;
 }