Exemple #1
0
 public function onPost()
 {
     if ($this->user->hasRight('user_activity_add')) {
         try {
             $contentType = Base::getRequestHeader('Content-Type');
             switch ($contentType) {
                 case Writer\Atom::$mime:
                     $atom = new Atom();
                     $atom->import($this->getRequest(ReaderInterface::DOM));
                     foreach ($atom as $entry) {
                         try {
                             $this->insertEntry($entry);
                         } catch (\Exception $e) {
                         }
                     }
                     break;
                 default:
                     throw new Exception('Invalid content type');
                     break;
             }
             $msg = new Data\Message('You have successful create a message', true);
             $this->setResponse($msg, WriterInterface::XML);
         } catch (Exception $e) {
             $msg = new Data\Message($e->getMessage(), false);
             $this->setResponse($msg, WriterInterface::XML);
         }
     } else {
         $msg = new Data\Message('Access not allowed', false);
         $this->setResponse($msg, WriterInterface::XML, $this->user->isAnonymous() ? 401 : 403);
     }
 }
Exemple #2
0
 /**
  * Outputs the raw media item
  *
  * @httpMethod GET
  * @path /{mediaId}
  * @nickname doServe
  * @responseClass PSX_Data_Message
  */
 public function doServe()
 {
     try {
         // get id
         $mediaId = $this->getUriFragments('mediaId');
         if (strlen($mediaId) == 36) {
             $media = $this->getHandler()->getOneByGlobalId($mediaId);
         } else {
             $media = $this->getHandler()->getOneById($mediaId);
         }
         // get media item
         if (!empty($media)) {
             // remove caching header
             header_remove('Expires');
             header_remove('Last-Modified');
             header_remove('Cache-Control');
             header_remove('Pragma');
             // check right
             if (!empty($media['rightId']) && !$this->user->hasRightId($media['rightId'])) {
                 throw new Exception('Access not allowed');
             }
             // send header
             switch ($media['mimeType']) {
                 case 'application/octet-stream':
                     header('Content-Type: ' . $media['mimeType']);
                     header('Content-Disposition: attachment; filename="' . $media['name'] . '"');
                     break;
                 default:
                     header('Content-Type: ' . $media['mimeType']);
                     break;
             }
             // read content
             if ($media['path'][0] == '/' || $media['path'][1] == ':') {
                 // absolute path
                 $path = $media['path'];
             } else {
                 // relative path
                 $path = $this->registry['media.path'] . '/' . $media['path'];
             }
             if (!is_file($path)) {
                 throw new Exception('File not found', 404);
             }
             $response = file_get_contents($path);
             // caching header
             $etag = md5($response);
             $match = Base::getRequestHeader('If-None-Match');
             $match = $match !== false ? trim($match, '"') : '';
             header('Etag: "' . $etag . '"');
             if ($match != $etag) {
                 echo $response;
             } else {
                 header('HTTP/1.1 304 Not Modified');
             }
             exit;
         } else {
             throw new Exception('Invalid media id');
         }
     } catch (\Exception $e) {
         $msg = new Message($e->getMessage(), false);
         $this->setResponse($msg, null, 404);
     }
 }