예제 #1
0
 /**
  * Validates file before upload
  *
  * @param ValidationEvent $event
  */
 public function onUploadValidation(ValidationEvent $event)
 {
     $file = $event->getFile();
     $extensions = $this->factory->getParameter('allowed_extensions');
     $maxSize = Asset::convertSizeToBytes($this->factory->getParameter('max_size') . 'M');
     // max size is set in MB
     if ($file !== null) {
         if ($file->getSize() > $maxSize) {
             $message = $this->translator->trans('mautic.asset.asset.error.file.size', array('%fileSize%' => round($file->getSize() / 1048576, 2), '%maxSize%' => round($maxSize / 1048576, 2)), 'validators');
             throw new ValidationException($message);
         }
         if (!in_array(strtolower($file->getExtension()), array_map('strtolower', $extensions))) {
             $message = $this->translator->trans('mautic.asset.asset.error.file.extension', array('%fileExtension%' => $file->getExtension(), '%extensions%' => implode(', ', $extensions)), 'validators');
             throw new ValidationException($message);
         }
     }
 }
예제 #2
0
 /**
  * Determine the max upload size based on PHP restrictions and config
  */
 public function getMaxUploadSize()
 {
     $maxAssetSize = Asset::convertSizeToBytes($this->factory->getParameter('max_size') . 'M');
     $maxPostSize = Asset::convertSizeToBytes(ini_get('post_max_size'));
     $maxUploadSize = Asset::convertSizeToBytes(ini_get('upload_max_filesize'));
     $memoryLimit = Asset::convertSizeToBytes(ini_get('memory_limit'));
     $maxAllowed = min(array_filter(array($maxAssetSize, $maxPostSize, $maxUploadSize, $memoryLimit)));
     return round($maxAllowed / 1048576, 2);
 }
예제 #3
0
 /**
  * Determine the max upload size based on PHP restrictions and config
  *
  * @param string     $unit              If '', determine the best unit based on the number
  * @param bool|false $humanReadable     Return as a human readable filesize
  *
  * @return float
  */
 public function getMaxUploadSize($unit = 'M', $humanReadable = false)
 {
     $maxAssetSize = $this->factory->getParameter('max_size');
     $maxAssetSize = $maxAssetSize == -1 || $maxAssetSize === 0 ? PHP_INT_MAX : Asset::convertSizeToBytes($maxAssetSize . 'M');
     $maxPostSize = Asset::getIniValue('post_max_size');
     $maxUploadSize = Asset::getIniValue('upload_max_filesize');
     $memoryLimit = Asset::getIniValue('memory_limit');
     $maxAllowed = min(array_filter(array($maxAssetSize, $maxPostSize, $maxUploadSize, $memoryLimit)));
     if ($humanReadable) {
         $number = Asset::convertBytesToHumanReadable($maxAllowed);
     } else {
         list($number, $unit) = Asset::convertBytesToUnit($maxAllowed, $unit);
     }
     return $number;
 }
예제 #4
0
 /**
  * Generates edit form and processes post data
  *
  * @param int  $objectId
  * @param bool $ignorePost
  *
  * @return JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
  */
 public function editAction($objectId, $ignorePost = false)
 {
     /** @var \Mautic\AssetBundle\Model\AssetModel $model */
     $model = $this->factory->getModel('asset.asset');
     /** @var \Mautic\AssetBundle\Entity\Asset $entity */
     $entity = $model->getEntity($objectId);
     $entity->setMaxSize(Asset::convertSizeToBytes($this->factory->getParameter('max_size') . 'M'));
     // convert from MB to B
     $session = $this->factory->getSession();
     $page = $this->factory->getSession()->get('mautic.asset.page', 1);
     $method = $this->request->getMethod();
     $maxSize = $model->getMaxUploadSize();
     $extensions = '.' . implode(', .', $this->factory->getParameter('allowed_extensions'));
     $maxSizeError = $this->get('translator')->trans('mautic.asset.asset.error.file.size', array('%fileSize%' => '{{filesize}}', '%maxSize%' => '{{maxFilesize}}'), 'validators');
     $extensionError = $this->get('translator')->trans('mautic.asset.asset.error.file.extension.js', array('%extensions%' => $extensions), 'validators');
     //set the return URL
     $returnUrl = $this->generateUrl('mautic_asset_index', array('page' => $page));
     // Get upload folder
     $uploaderHelper = $this->container->get('oneup_uploader.templating.uploader_helper');
     $uploadEndpoint = $uploaderHelper->endpoint('asset');
     $postActionVars = array('returnUrl' => $returnUrl, 'viewParameters' => array('page' => $page), 'contentTemplate' => 'MauticAssetBundle:Asset:index', 'passthroughVars' => array('activeLink' => 'mautic_asset_index', 'mauticContent' => 'asset'));
     //not found
     if ($entity === null) {
         return $this->postActionRedirect(array_merge($postActionVars, array('flashes' => array(array('type' => 'error', 'msg' => 'mautic.asset.asset.error.notfound', 'msgVars' => array('%id%' => $objectId))))));
     } elseif (!$this->factory->getSecurity()->hasEntityAccess('asset:assets:viewown', 'asset:assets:viewother', $entity->getCreatedBy())) {
         return $this->accessDenied();
     } elseif ($model->isLocked($entity)) {
         //deny access if the entity is locked
         return $this->isLocked($postActionVars, $entity, 'asset.asset');
     }
     // Create temporary asset ID
     $tempId = $method == 'POST' ? $this->request->request->get('asset[tempId]', '', true) : uniqid('tmp_');
     $entity->setTempId($tempId);
     //Create the form
     $action = $this->generateUrl('mautic_asset_action', array('objectAction' => 'edit', 'objectId' => $objectId));
     $form = $model->createForm($entity, $this->get('form.factory'), $action);
     ///Check for a submitted form and process it
     if (!$ignorePost && $method == 'POST') {
         $valid = false;
         if (!($cancelled = $this->isFormCancelled($form))) {
             if ($valid = $this->isFormValid($form)) {
                 $entity->setUploadDir($this->factory->getParameter('upload_dir'));
                 $entity->preUpload();
                 $entity->upload();
                 //form is valid so process the data
                 $model->saveEntity($entity, $form->get('buttons')->get('save')->isClicked());
                 //remove the asset from request
                 $this->request->files->remove('asset');
                 $this->addFlash('mautic.core.notice.updated', array('%name%' => $entity->getTitle(), '%menu_link%' => 'mautic_asset_index', '%url%' => $this->generateUrl('mautic_asset_action', array('objectAction' => 'edit', 'objectId' => $entity->getId()))));
                 $returnUrl = $this->generateUrl('mautic_asset_action', array('objectAction' => 'view', 'objectId' => $entity->getId()));
                 $viewParams = array('objectId' => $entity->getId());
                 $template = 'MauticAssetBundle:Asset:view';
             }
         } else {
             //clear any modified content
             $session->remove('mautic.asestbuilder.' . $objectId . '.content');
             //unlock the entity
             $model->unlockEntity($entity);
             $returnUrl = $this->generateUrl('mautic_asset_index', array('page' => $page));
             $viewParams = array('page' => $page);
             $template = 'MauticAssetBundle:Asset:index';
         }
         if ($cancelled || $valid && $form->get('buttons')->get('save')->isClicked()) {
             return $this->postActionRedirect(array_merge($postActionVars, array('returnUrl' => $returnUrl, 'viewParameters' => $viewParams, 'contentTemplate' => $template)));
         }
     } else {
         //lock the entity
         $model->lockEntity($entity);
     }
     // Check for integrations to cloud providers
     /** @var \Mautic\PluginBundle\Helper\IntegrationHelper $integrationHelper */
     $integrationHelper = $this->factory->getHelper('integration');
     $integrations = $integrationHelper->getIntegrationObjects(null, array('cloud_storage'));
     return $this->delegateView(array('viewParameters' => array('form' => $form->createView(), 'activeAsset' => $entity, 'assetDownloadUrl' => $model->generateUrl($entity), 'integrations' => $integrations, 'startOnLocal' => $entity->getStorageLocation() == 'local', 'uploadEndpoint' => $uploadEndpoint, 'maxSize' => $maxSize, 'maxSizeError' => $maxSizeError, 'extensions' => $extensions, 'extensionError' => $extensionError), 'contentTemplate' => 'MauticAssetBundle:Asset:form.html.php', 'passthroughVars' => array('activeLink' => '#mautic_asset_index', 'mauticContent' => 'asset', 'route' => $this->generateUrl('mautic_asset_action', array('objectAction' => 'edit', 'objectId' => $entity->getId())))));
 }