Esempio n. 1
0
 /**
  * Gets the extension from the given content type.
  * @param String $content_type
  * @return String
  */
 private function getExt($content_type)
 {
     return array_search($content_type, FileTypes::getMap());
 }
 /**
  * Validates the attachment headers.
  * @param [String => Mixed] $headers
  */
 private static function validateHeaders(array $headers, array $sha_hashes)
 {
     if (!isset($headers['content-type'])) {
         throw new Exceptions\Exception('You need to set a content type for your attachments.');
     }
     //get the correct ext if valid
     $ext = array_search($headers['content-type'], FileTypes::getMap());
     if ($ext === false) {
         throw new Exceptions\Exception('This file type cannot be supported');
     }
     //check X-Experience-API-Hash is set, otherwise reject @todo
     if (!isset($headers['x-experience-api-hash']) || $headers['x-experience-api-hash'] == '') {
         throw new Exceptions\Exception('Attachments require an api hash.');
     }
 }
Esempio n. 3
0
 /**
  * Handle content storage
  * @param Mixed $content          The content passed in the request
  */
 public function setContent($content_info, $method)
 {
     $content = $content_info['content'];
     $contentType = $content_info['contentType'];
     $contentTypeArr = explode(";", $contentType);
     if (sizeof($contentTypeArr) >= 1) {
         $mimeType = $contentTypeArr[0];
     } else {
         $mimeType = $contentType;
     }
     switch ($mimeType) {
         case "application/json":
             $request_content = json_decode($content, TRUE);
             if (!$this->exists) {
                 //if we are adding a new piece of content...
                 $this->content = $request_content;
             } else {
                 //if existing content, check that it is also JSON
                 switch ($method) {
                     case 'PUT':
                         //overwrite content
                         if (is_null($request_content)) {
                             \App::abort(400, 'JSON is invalid.');
                         }
                         $this->content = $request_content;
                         break;
                     case 'POST':
                         //merge variables
                         if (!(is_array($request_content) && $this->isAssoc($request_content))) {
                             \App::abort(400, 'JSON must contain an object at the top level.');
                         } else {
                             if ($this->contentType !== $mimeType) {
                                 \App::abort(400, 'JSON document content may not be merged with that of another type');
                             }
                         }
                         $this->content = array_merge($this->content, $request_content);
                         break;
                     default:
                         \App::abort(400, 'Only PUT AND POST methods may amend content');
                         break;
                 }
             }
             break;
         case "text/plain":
             if (!$this->exists) {
                 $this->content = $content;
             } else {
                 if ($method === 'PUT') {
                     $this->content = $content;
                 } else {
                     \App::abort(400, sprintf('Cannot amend existing %s document with a string', $this->contentType));
                 }
             }
             break;
         default:
             if (!$this->exists) {
                 // check we are adding a new document
                 //HANDLE FILE SAVES???
                 $dir = $this->getContentDir();
                 if ($content instanceof UploadedFile) {
                     $origname = $content->getClientOriginalName();
                     $parts = pathinfo($origname);
                     $filename = Str::slug(Str::lower($parts['filename'])) . '-' . time() . '.' . $parts['extension'];
                     $content->move($dir, $filename);
                 } else {
                     $ext = array_search($mimeType, FileTypes::getMap());
                     if ($ext === false) {
                         \App::abort(400, 'This file type cannot be supported');
                     }
                     // @todo check for allowed filetypes?
                     $filename = time() . "." . $ext;
                     $size = file_put_contents($dir . $filename, $content);
                     if ($size === false) {
                         \App::abort(400, 'There was an issue saving the content');
                     }
                 }
                 $this->content = $filename;
             } else {
                 \App::abort(400, sprintf('Cannot amend existing %s document with a file', $this->contentType));
             }
             break;
     }
     $this->contentType = $mimeType;
 }
 private function saveDocument($content, $contentType)
 {
     $dir = $this->getContentDir();
     if ($content instanceof UploadedFile) {
         $origname = $content->getClientOriginalName();
         $parts = pathinfo($origname);
         $filename = Str::slug(Str::lower($parts['filename'])) . '-' . time() . '.' . $parts['extension'];
         $content->move($dir, $filename);
     } else {
         $ext = array_search($contentType, FileTypes::getMap());
         $filename = time() . '_' . mt_rand(0, 1000) . ($ext !== false ? '.' . $ext : '');
         $size = file_put_contents($dir . $filename, $content);
         if ($size === false) {
             throw new Exceptions\Exception('There was an issue saving the content');
         }
     }
     $this->content = $filename;
     $this->setSha($content);
 }
Esempio n. 5
0
 private function saveDocument($content, $contentType)
 {
     $dir = $this->getContentDir();
     if ($content instanceof UploadedFile) {
         $origname = $content->getClientOriginalName();
         $parts = pathinfo($origname);
         $filename = Str::slug(Str::lower($parts['filename'])) . '-' . time() . '.' . $parts['extension'];
         // Stores the file in a temporary location before writing it to the FileRepository.
         $tmp_location = __DIR__ . '/../../uploads/tmp';
         $content->move($tmp_location, $filename);
         $data = file_get_contents($tmp_location . '/' . $filename);
         FileFactory::create()->update($dir . $filename, ['content' => $data], []);
         unlink($tmp_location . '/' . $filename);
     } else {
         $ext = array_search($contentType, FileTypes::getMap());
         $filename = time() . '_' . mt_rand(0, 1000) . ($ext !== false ? '.' . $ext : '');
         $size = FileFactory::create()->update($dir . $filename, ['content' => $content], []);
         if ($size === false) {
             throw new Exceptions\Exception('There was an issue saving the content');
         }
     }
     $this->content = $filename;
     $this->setSha($content);
 }
 /**
  * Store any attachments
  *
  **/
 private function storeAttachments($attachments, $lrs)
 {
     foreach ($attachments as $a) {
         // Separate body contents from headers
         $a = ltrim($a, "\n");
         list($raw_headers, $body) = explode("\n\n", $a, 2);
         // Parse headers and separate so we can access
         $raw_headers = explode("\n", $raw_headers);
         $headers = array();
         foreach ($raw_headers as $header) {
             list($name, $value) = explode(':', $header);
             $headers[strtolower($name)] = ltrim($value, ' ');
         }
         //get the correct ext if valid
         $ext = array_search($headers['content-type'], FileTypes::getMap());
         if ($ext === false) {
             \App::abort(400, 'This file type cannot be supported');
         }
         $filename = str_random(12) . "." . $ext;
         //create directory if it doesn't exist
         if (!\File::exists(base_path() . '/uploads/' . $lrs . '/attachments/' . $headers['x-experience-api-hash'] . '/')) {
             \File::makeDirectory(base_path() . '/uploads/' . $lrs . '/attachments/' . $headers['x-experience-api-hash'] . '/', 0775, true);
         }
         $destinationPath = base_path() . '/uploads/' . $lrs . '/attachments/' . $headers['x-experience-api-hash'] . '/';
         $filename = $destinationPath . $filename;
         $file = fopen($filename, 'wb');
         //opens the file for writing with a BINARY (b) fla
         $size = fwrite($file, $body);
         //write the data to the file
         fclose($file);
         if ($size === false) {
             \App::abort(400, 'There was an issue saving the attachment');
         }
     }
 }