public function indexAction()
 {
     $type = $this->params()->fromRoute('type');
     $id = $this->params()->fromRoute('id');
     $appServiceLoader = $this->recoverAppServiceLoader();
     $configurations = $appServiceLoader->recoverService('configurations');
     $wrapper = new AttachmentsGetterWrapper(new AttachmentsGetter($this->getServiceLocator()->get('doctrine.entitymanager.orm_default')));
     $wrapper->setInput(array('id' => $id, 'limit' => 1));
     $wrapper->setupQueryBuilder();
     $attachmentRecord = $wrapper->getRecords();
     if (empty($attachmentRecord)) {
         return $this->redirect()->toRoute('notfound', array('lang' => 'it'));
     }
     $bucketDir = $type . '/';
     $filename = $attachmentRecord[0]['name'];
     $mimetype = $attachmentRecord[0]['mimetype'];
     $s3 = new S3($configurations['amazon_s3_accesskey'], $configurations['amazon_s3_secretkey']);
     $sthreeFile = $s3->getObject($configurations['amazon_s3_bucket'], $bucketDir . $filename);
     if (empty($sthreeFile->body)) {
         return $this->redirect()->toRoute('notfound', array('lang' => 'it'));
     }
     $response = $this->getResponse();
     $response->setContent($sthreeFile->body);
     $response->getHeaders()->addHeaderLine('Content-Type', 'public')->addHeaderLine('Content-Description', 'File Transfer')->addHeaderLine('Content-Disposition', 'attachment; filename=' . $filename)->addHeaderLine('Content-Type', $mimetype);
     return $response;
 }
 public function indexAction()
 {
     $id = $this->params()->fromPost('id');
     /**
      * @var \Doctrine\ORM\EntityManager $em
      */
     $em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
     /**
      * @var \Doctrine\DBAL\Connection $connection
      */
     $connection = $em->getConnection();
     $request = $this->getRequest();
     $post = array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray());
     if (!($request->isXmlHttpRequest() or $request->isPost())) {
         return $this->redirect()->toRoute('main');
     }
     $mainLayout = $this->initializeAdminArea();
     $userDetails = $this->recoverUserDetails();
     $helper = new AttachmentsControllerHelper();
     $helper->setConnection($connection);
     $helper->getConnection()->beginTransaction();
     try {
         $helper->setLoggedUser($userDetails);
         $attachmentRecord = $helper->recoverWrapperRecordsById(new AttachmentsGetterWrapper(new AttachmentsGetter($em)), array('id' => $post['deleteId'], 'limit' => 1), $post['deleteId']);
         $helper->checkRecords($attachmentRecord, 'Dati file allegato non trovati');
         $configurations = $this->layout()->getVariable('configurations');
         $s3 = new S3($configurations['amazon_s3_accesskey'], $configurations['amazon_s3_secretkey']);
         $s3->deleteObject($configurations['amazon_s3_bucket'], $this->params()->fromRoute('modulename') . '/' . $attachmentRecord[0]['name']);
         $helper->deleteAttachments($post['deleteId']);
         $helper->deleteAttachmentsRelations($post['deleteId']);
         $logWriter = new LogWriter($connection);
         $logWriter->writeLog(array('user_id' => $userDetails->id, 'module_id' => ModulesContainer::recoverIdFromModuleCode($this->params()->fromRoute('modulename')), 'message' => "Eliminato file allegato ", 'type' => 'info', 'reference_id' => $id, 'backend' => 1));
         $helper->getConnection()->commit();
         $referer = $this->getRequest()->getHeader('Referer');
         if (is_object($referer)) {
             return $this->redirect()->toUrl($referer->getUri());
         }
     } catch (\Exception $e) {
         try {
             $helper->getConnection()->rollBack();
         } catch (\Doctrine\DBAL\ConnectionException $dbEx) {
         }
         $logWriter = new LogWriter($connection);
         $logWriter->writeLog(array('user_id' => $userDetails->id, 'module_id' => ModulesContainer::contenuti_id, 'message' => "Errore eliminazione file allegato", 'type' => 'error', 'description' => $e->getMessage(), 'reference_id' => $id, 'backend' => 1));
         $referer = $this->getRequest()->getHeader('Referer');
         if (is_object($referer)) {
             $refererLink = $referer->getUri();
         }
         $this->layout()->setVariables(array('messageType' => 'danger', 'messageTitle' => 'Errore eliminazione file allegato', 'messageText' => $e->getMessage(), 'previousPageLink' => isset($refererLink) ? $refererLink : null, 'previousPageLabel' => "Torna all'elenco allegati", 'templatePartial' => "message.phtml"));
     }
     $this->layout()->setTemplate($mainLayout);
 }
예제 #3
0
 /**
  * @param $fileData
  * @param $filename
  * @param $lastId
  * @return bool
  * @throws NullException
  */
 public function upload($fileData, $filename)
 {
     $this->assertAccessKey();
     $this->assertSecretKey();
     $this->assertBucket();
     $this->assertS3Directory();
     $this->assertS3();
     return $this->getS3()->putObject(S3::inputFile($fileData, false), $this->bucket, $this->s3Directory . '/' . $filename, S3::ACL_PUBLIC_READ);
 }
예제 #4
0
 /**
  * Get the S3 response
  *
  * @return object | false
  */
 public function getResponse()
 {
     $query = '';
     if (sizeof($this->parameters) > 0) {
         $query = substr($this->uri, -1) !== '?' ? '?' : '&';
         foreach ($this->parameters as $var => $value) {
             if ($value == null || $value == '') {
                 $query .= $var . '&';
             } else {
                 $query .= $var . '=' . rawurlencode($value) . '&';
             }
         }
         $query = substr($query, 0, -1);
         $this->uri .= $query;
         if (array_key_exists('acl', $this->parameters) || array_key_exists('location', $this->parameters) || array_key_exists('torrent', $this->parameters) || array_key_exists('website', $this->parameters) || array_key_exists('logging', $this->parameters)) {
             $this->resource .= $query;
         }
     }
     $url = (S3::$useSSL ? 'https://' : 'http://') . ($this->headers['Host'] !== '' ? $this->headers['Host'] : $this->endpoint) . $this->uri;
     //var_dump('bucket: ' . $this->bucket, 'uri: ' . $this->uri, 'resource: ' . $this->resource, 'url: ' . $url);
     // Basic setup
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_USERAGENT, 'S3/php');
     if (S3::$useSSL) {
         // SSL Validation can now be optional for those with broken OpenSSL installations
         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, S3::$useSSLValidation ? 2 : 0);
         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, S3::$useSSLValidation ? 1 : 0);
         if (S3::$sslKey !== null) {
             curl_setopt($curl, CURLOPT_SSLKEY, S3::$sslKey);
         }
         if (S3::$sslCert !== null) {
             curl_setopt($curl, CURLOPT_SSLCERT, S3::$sslCert);
         }
         if (S3::$sslCACert !== null) {
             curl_setopt($curl, CURLOPT_CAINFO, S3::$sslCACert);
         }
     }
     curl_setopt($curl, CURLOPT_URL, $url);
     if (S3::$proxy != null && isset(S3::$proxy['host'])) {
         curl_setopt($curl, CURLOPT_PROXY, S3::$proxy['host']);
         curl_setopt($curl, CURLOPT_PROXYTYPE, S3::$proxy['type']);
         if (isset(S3::$proxy['user'], S3::$proxy['pass']) && S3::$proxy['user'] != null && S3::$proxy['pass'] != null) {
             curl_setopt($curl, CURLOPT_PROXYUSERPWD, sprintf('%s:%s', S3::$proxy['user'], S3::$proxy['pass']));
         }
     }
     // Headers
     $headers = array();
     $amz = array();
     foreach ($this->amzHeaders as $header => $value) {
         if (strlen($value) > 0) {
             $headers[] = $header . ': ' . $value;
         }
     }
     foreach ($this->headers as $header => $value) {
         if (strlen($value) > 0) {
             $headers[] = $header . ': ' . $value;
         }
     }
     // Collect AMZ headers for signature
     foreach ($this->amzHeaders as $header => $value) {
         if (strlen($value) > 0) {
             $amz[] = strtolower($header) . ':' . $value;
         }
     }
     // AMZ headers must be sorted
     if (sizeof($amz) > 0) {
         //sort($amz);
         usort($amz, array(&$this, '__sortMetaHeadersCmp'));
         $amz = "\n" . implode("\n", $amz);
     } else {
         $amz = '';
     }
     if (S3::hasAuth()) {
         // Authorization string (CloudFront stringToSign should only contain a date)
         if ($this->headers['Host'] == 'cloudfront.amazonaws.com') {
             $headers[] = 'Authorization: ' . S3::__getSignature($this->headers['Date']);
         } else {
             $headers[] = 'Authorization: ' . S3::__getSignature($this->verb . "\n" . $this->headers['Content-MD5'] . "\n" . $this->headers['Content-Type'] . "\n" . $this->headers['Date'] . $amz . "\n" . $this->resource);
         }
     }
     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($curl, CURLOPT_HEADER, false);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
     curl_setopt($curl, CURLOPT_WRITEFUNCTION, array(&$this, '__responseWriteCallback'));
     curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this, '__responseHeaderCallback'));
     // curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); THIS GENERATES A WARNING!
     // Request types
     switch ($this->verb) {
         case 'GET':
             break;
         case 'PUT':
         case 'POST':
             // POST only used for CloudFront
             if ($this->fp !== false) {
                 curl_setopt($curl, CURLOPT_PUT, true);
                 curl_setopt($curl, CURLOPT_INFILE, $this->fp);
                 if ($this->size >= 0) {
                     curl_setopt($curl, CURLOPT_INFILESIZE, $this->size);
                 }
             } elseif ($this->data !== false) {
                 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->verb);
                 curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data);
             } else {
                 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->verb);
             }
             break;
         case 'HEAD':
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD');
             curl_setopt($curl, CURLOPT_NOBODY, true);
             break;
         case 'DELETE':
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
             break;
         default:
             break;
     }
     // Execute, grab errors
     if (curl_exec($curl)) {
         $this->response->code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     } else {
         $this->response->error = array('code' => curl_errno($curl), 'message' => curl_error($curl), 'resource' => $this->resource);
     }
     @curl_close($curl);
     // Parse body into XML
     if ($this->response->error === false && isset($this->response->headers['type']) && $this->response->headers['type'] == 'application/xml' && isset($this->response->body)) {
         $this->response->body = simplexml_load_string($this->response->body);
         // Grab S3 errors
         if (!in_array($this->response->code, array(200, 204, 206)) && isset($this->response->body->Code, $this->response->body->Message)) {
             $this->response->error = array('code' => (string) $this->response->body->Code, 'message' => (string) $this->response->body->Message);
             if (isset($this->response->body->Resource)) {
                 $this->response->error['resource'] = (string) $this->response->body->Resource;
             }
             unset($this->response->body);
         }
     }
     // Clean up file resources
     if ($this->fp !== false && is_resource($this->fp)) {
         fclose($this->fp);
     }
     return $this->response;
 }
 public function indexAction()
 {
     $id = $this->params()->fromPost('id');
     /**
      * @var \Doctrine\ORM\EntityManager $em
      */
     $em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
     /**
      * @var \Doctrine\DBAL\Connection $connection
      */
     $connection = $em->getConnection();
     $request = $this->getRequest();
     $post = array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray());
     if (!($request->isXmlHttpRequest() or $request->isPost())) {
         return $this->redirect()->toRoute('main');
     }
     $mainLayout = $this->initializeAdminArea();
     $configurations = $this->layout()->getVariable('configurations');
     $userDetails = $this->recoverUserDetails();
     $moduleId = $this->params()->fromRoute('modulename') == 'contenuti' ? ModulesContainer::contenuti_id : ModulesContainer::amministrazione_trasparente_id;
     $helper = new ContenutiControllerHelper();
     try {
         $contentRecord = $helper->recoverWrapperRecordsById(new ContenutiGetterWrapper(new ContenutiGetter($em)), array('id' => $post['deleteId'], 'limit' => 1), $post['deleteId']);
         $helper->checkRecords($contentRecord, 'Articolo da eliminare non trovato');
         $helper->setConnection($connection);
         $helper->getConnection()->beginTransaction();
         $helper->delete($post['deleteId']);
         $helper->getConnection()->commit();
         /* Delte Attachments files */
         $attachmentsHelper = new AttachmentsControllerHelper();
         $attachmentsHelper->setConnection($connection);
         $attachmentsRecords = $helper->recoverWrapperRecords(new AttachmentsGetterWrapper(new AttachmentsGetter($em)), array('referenceId' => $post['deleteId'], 'moduleId' => $moduleId));
         if (!empty($attachmentsRecords)) {
             foreach ($attachmentsRecords as $attachmentRecord) {
                 $attachmentsHelper->deleteAttachments($attachmentsRecords['id']);
                 $attachmentsHelper->deleteAttachmentsRelations($attachmentsRecords['id']);
                 $s3 = new S3($configurations['amazon_s3_accesskey'], $configurations['amazon_s3_secretkey']);
                 $s3->deleteObject($configurations['amazon_s3_bucket'], $this->params()->fromRoute('modulename') . '/' . $attachmentRecord[0]['name']);
             }
         }
         /* Delete from home page */
         $homeHelper = new HomePagePutRemoveControllerHelper();
         $homePageRecords = $homeHelper->recoverWrapperRecords(new HomePageGetterWrapper(new HomePageGetter($em)), array('referenceId' => $post['deleteId'], 'moduleId' => $moduleId, 'moduleCode' => $this->params()->fromRoute('modulename')));
         if (!empty($homePageRecords)) {
             $homePageBlocksRecords = $helper->recoverWrapperRecords(new HomePageBlocksGetterWrapper(new HomePageBlocksGetter($em)), array('fields' => 'homePageBlocks.id', 'moduleId' => $moduleId, 'limit' => 1));
             $helper->checkRecords($homePageBlocksRecords, 'Impossibile recuperare i dati relativi al modulo in home page');
             $homeHelper->setConnection($connection);
             $homeHelper->getConnection()->beginTransaction();
             $homeHelper->deleteFromHomePage($post['deleteId'], $homePageBlocksRecords[0]['id']);
             $homeHelper->getConnection()->commit();
         }
         $logWriter = new LogWriter($connection);
         $logWriter->writeLog(array('user_id' => $userDetails->id, 'module_id' => ModulesContainer::recoverIdFromModuleCode($this->params()->fromRoute('modulename')), 'message' => "Eliminato articolo " . $contentRecord[0]['titolo'], 'type' => 'info', 'reference_id' => $id, 'backend' => 1));
         $referer = $this->getRequest()->getHeader('Referer');
         if (is_object($referer)) {
             return $this->redirect()->toUrl($referer->getUri());
         }
     } catch (\Exception $e) {
         try {
             // $helper->getConnection()->rollBack();
         } catch (\Doctrine\DBAL\ConnectionException $dbEx) {
         }
         $logWriter = new LogWriter($connection);
         $logWriter->writeLog(array('user_id' => $userDetails->id, 'module_id' => ModulesContainer::contenuti_id, 'message' => "Errore eliminazione file articolo ", 'type' => 'error', 'description' => $e->getMessage(), 'reference_id' => $id, 'backend' => 1));
         $referer = $this->getRequest()->getHeader('Referer');
         if (is_object($referer)) {
             $refererLink = $referer->getUri();
         }
         $this->layout()->setVariables(array('messageType' => 'danger', 'messageTitle' => 'Errore eliminazione contenuto', 'messageText' => $e->getMessage(), 'previousPageLink' => isset($refererLink) ? $refererLink : null, 'previousPageLabel' => "Torna all'elenco", 'templatePartial' => "message.phtml"));
     }
     $this->layout()->setTemplate($mainLayout);
 }