Example #1
0
 /**
  * Handles POST requests for tree operations not handled in the SabreDAV parent clas
  *
  * @param RequestInterface $request
  * @param ResponseInterface $response
  * @return bool
  */
 public function httpPOSTExtra(RequestInterface $request, ResponseInterface $response)
 {
     $contentType = $request->getHeader('Content-Type');
     list($contentType) = explode(';', $contentType);
     if ($contentType !== 'application/x-www-form-urlencoded' && $contentType !== 'multipart/form-data') {
         return;
     }
     $postVars = $request->getPostData();
     if (!isset($postVars['sabreActionExtra'])) {
         return;
     }
     $uri = $request->getPath();
     switch ($postVars['sabreActionExtra']) {
         case 'del':
             if (isset($postVars['path'])) {
                 // Using basename() because we won't allow slashes
                 list(, $Name) = \Sabre\HTTP\URLUtil::splitPath(trim($postVars['path']));
                 if (!empty($Name) && $this->config->browserplugin_enable_delete === true) {
                     $this->server->tree->delete($uri . '/' . $Name);
                 }
             }
             break;
     }
     $response->setHeader('Location', $request->getUrl());
     $response->setStatus(302);
     return false;
 }
Example #2
0
 /**
  * HTTP REPORT method implementation
  *
  * Although the REPORT method is not part of the standard WebDAV spec (it's from rfc3253)
  * It's used in a lot of extensions, so it made sense to implement it into the core.
  *
  * @param RequestInterface $request
  * @param ResponseInterface $response
  * @return bool
  */
 function httpReport(RequestInterface $request, ResponseInterface $response)
 {
     $path = $request->getPath();
     $result = $this->server->xml->parse($request->getBody(), $request->getUrl(), $rootElementName);
     if ($this->server->emit('report', [$rootElementName, $result, $path])) {
         // If emit returned true, it means the report was not supported
         throw new Exception\ReportNotSupported();
     }
     // Sending back false will interupt the event chain and tell the server
     // we've handled this method.
     return false;
 }
Example #3
0
 /**
  * POST operation on Comments collections
  *
  * @param RequestInterface $request request object
  * @param ResponseInterface $response response object
  * @return null|false
  */
 public function httpPost(RequestInterface $request, ResponseInterface $response)
 {
     $path = $request->getPath();
     $node = $this->server->tree->getNodeForPath($path);
     if (!$node instanceof EntityCollection) {
         return null;
     }
     $data = $request->getBodyAsString();
     $comment = $this->createComment($node->getName(), $node->getId(), $data, $request->getHeader('Content-Type'));
     // update read marker for the current user/poster to avoid
     // having their own comments marked as unread
     $node->setReadMarker(null);
     $url = $request->getUrl() . '/' . urlencode($comment->getId());
     $response->setHeader('Content-Location', $url);
     // created
     $response->setStatus(201);
     return false;
 }
Example #4
0
 /**
  * POST operation on system tag collections
  *
  * @param RequestInterface $request request object
  * @param ResponseInterface $response response object
  * @return null|false
  */
 public function httpPost(RequestInterface $request, ResponseInterface $response)
 {
     $path = $request->getPath();
     // Making sure the node exists
     try {
         $node = $this->server->tree->getNodeForPath($path);
     } catch (NotFound $e) {
         return null;
     }
     if ($node instanceof SystemTagsByIdCollection || $node instanceof SystemTagsObjectMappingCollection) {
         $data = $request->getBodyAsString();
         $tag = $this->createTag($data, $request->getHeader('Content-Type'));
         if ($node instanceof SystemTagsObjectMappingCollection) {
             // also add to collection
             $node->createFile($tag->getId());
             $url = $request->getBaseUrl() . 'systemtags/';
         } else {
             $url = $request->getUrl();
         }
         if ($url[strlen($url) - 1] !== '/') {
             $url .= '/';
         }
         $response->setHeader('Content-Location', $url . $tag->getId());
         // created
         $response->setStatus(201);
         return false;
     }
 }
Example #5
0
 /**
  * We intercept this to handle POST requests on calendars.
  *
  * @param RequestInterface $request
  * @param ResponseInterface $response
  * @return null|bool
  */
 function httpPost(RequestInterface $request, ResponseInterface $response)
 {
     $path = $request->getPath();
     // Only handling xml
     $contentType = $request->getHeader('Content-Type');
     if (strpos($contentType, 'application/xml') === false && strpos($contentType, 'text/xml') === false) {
         return;
     }
     // Making sure the node exists
     try {
         $node = $this->server->tree->getNodeForPath($path);
     } catch (DAV\Exception\NotFound $e) {
         return;
     }
     $requestBody = $request->getBodyAsString();
     // If this request handler could not deal with this POST request, it
     // will return 'null' and other plugins get a chance to handle the
     // request.
     //
     // However, we already requested the full body. This is a problem,
     // because a body can only be read once. This is why we preemptively
     // re-populated the request body with the existing data.
     $request->setBody($requestBody);
     $message = $this->server->xml->parse($requestBody, $request->getUrl(), $documentType);
     switch ($documentType) {
         // Dealing with the 'share' document, which modified invitees on a
         // calendar.
         case '{' . Plugin::NS_CALENDARSERVER . '}share':
             // We can only deal with IShareableCalendar objects
             if (!$node instanceof IShareableCalendar) {
                 return;
             }
             $this->server->transactionType = 'post-calendar-share';
             // Getting ACL info
             $acl = $this->server->getPlugin('acl');
             // If there's no ACL support, we allow everything
             if ($acl) {
                 $acl->checkPrivileges($path, '{DAV:}write');
             }
             $node->updateShares($message->set, $message->remove);
             $response->setStatus(200);
             // Adding this because sending a response body may cause issues,
             // and I wanted some type of indicator the response was handled.
             $response->setHeader('X-Sabre-Status', 'everything-went-well');
             // Breaking the event chain
             return false;
             // The invite-reply document is sent when the user replies to an
             // invitation of a calendar share.
         // The invite-reply document is sent when the user replies to an
         // invitation of a calendar share.
         case '{' . Plugin::NS_CALENDARSERVER . '}invite-reply':
             // This only works on the calendar-home-root node.
             if (!$node instanceof CalendarHome) {
                 return;
             }
             $this->server->transactionType = 'post-invite-reply';
             // Getting ACL info
             $acl = $this->server->getPlugin('acl');
             // If there's no ACL support, we allow everything
             if ($acl) {
                 $acl->checkPrivileges($path, '{DAV:}write');
             }
             $url = $node->shareReply($message->href, $message->status, $message->calendarUri, $message->inReplyTo, $message->summary);
             $response->setStatus(200);
             // Adding this because sending a response body may cause issues,
             // and I wanted some type of indicator the response was handled.
             $response->setHeader('X-Sabre-Status', 'everything-went-well');
             if ($url) {
                 $writer = $this->server->xml->getWriter($this->server->getBaseUri());
                 $writer->openMemory();
                 $writer->startDocument();
                 $writer->startElement('{' . Plugin::NS_CALENDARSERVER . '}shared-as');
                 $writer->write(new Href($url));
                 $writer->endElement();
                 $response->setHeader('Content-Type', 'application/xml');
                 $response->setBody($writer->outputMemory());
             }
             // Breaking the event chain
             return false;
         case '{' . Plugin::NS_CALENDARSERVER . '}publish-calendar':
             // We can only deal with IShareableCalendar objects
             if (!$node instanceof IShareableCalendar) {
                 return;
             }
             $this->server->transactionType = 'post-publish-calendar';
             // Getting ACL info
             $acl = $this->server->getPlugin('acl');
             // If there's no ACL support, we allow everything
             if ($acl) {
                 $acl->checkPrivileges($path, '{DAV:}write');
             }
             $node->setPublishStatus(true);
             // iCloud sends back the 202, so we will too.
             $response->setStatus(202);
             // Adding this because sending a response body may cause issues,
             // and I wanted some type of indicator the response was handled.
             $response->setHeader('X-Sabre-Status', 'everything-went-well');
             // Breaking the event chain
             return false;
         case '{' . Plugin::NS_CALENDARSERVER . '}unpublish-calendar':
             // We can only deal with IShareableCalendar objects
             if (!$node instanceof IShareableCalendar) {
                 return;
             }
             $this->server->transactionType = 'post-unpublish-calendar';
             // Getting ACL info
             $acl = $this->server->getPlugin('acl');
             // If there's no ACL support, we allow everything
             if ($acl) {
                 $acl->checkPrivileges($path, '{DAV:}write');
             }
             $node->setPublishStatus(false);
             $response->setStatus(200);
             // Adding this because sending a response body may cause issues,
             // and I wanted some type of indicator the response was handled.
             $response->setHeader('X-Sabre-Status', 'everything-went-well');
             // Breaking the event chain
             return false;
     }
 }
Example #6
0
 /**
  * Handles POST requests for tree operations.
  *
  * @param RequestInterface $request
  * @param ResponseInterface $response
  * @return bool
  */
 function httpPOST(RequestInterface $request, ResponseInterface $response)
 {
     $contentType = $request->getHeader('Content-Type');
     list($contentType) = explode(';', $contentType);
     if ($contentType !== 'application/x-www-form-urlencoded' && $contentType !== 'multipart/form-data') {
         return;
     }
     $postVars = $request->getPostData();
     if (!isset($postVars['sabreAction'])) {
         return;
     }
     $uri = $request->getPath();
     if ($this->server->emit('onBrowserPostAction', [$uri, $postVars['sabreAction'], $postVars])) {
         switch ($postVars['sabreAction']) {
             case 'mkcol':
                 if (isset($postVars['name']) && trim($postVars['name'])) {
                     // Using basename() because we won't allow slashes
                     list(, $folderName) = URLUtil::splitPath(trim($postVars['name']));
                     if (isset($postVars['resourceType'])) {
                         $resourceType = explode(',', $postVars['resourceType']);
                     } else {
                         $resourceType = ['{DAV:}collection'];
                     }
                     $properties = [];
                     foreach ($postVars as $varName => $varValue) {
                         // Any _POST variable in clark notation is treated
                         // like a property.
                         if ($varName[0] === '{') {
                             // PHP will convert any dots to underscores.
                             // This leaves us with no way to differentiate
                             // the two.
                             // Therefore we replace the string *DOT* with a
                             // real dot. * is not allowed in uris so we
                             // should be good.
                             $varName = str_replace('*DOT*', '.', $varName);
                             $properties[$varName] = $varValue;
                         }
                     }
                     $mkCol = new MkCol($resourceType, $properties);
                     $this->server->createCollection($uri . '/' . $folderName, $mkCol);
                 }
                 break;
                 // @codeCoverageIgnoreStart
             // @codeCoverageIgnoreStart
             case 'put':
                 if ($_FILES) {
                     $file = current($_FILES);
                 } else {
                     break;
                 }
                 list(, $newName) = URLUtil::splitPath(trim($file['name']));
                 if (isset($postVars['name']) && trim($postVars['name'])) {
                     $newName = trim($postVars['name']);
                 }
                 // Making sure we only have a 'basename' component
                 list(, $newName) = URLUtil::splitPath($newName);
                 if (is_uploaded_file($file['tmp_name'])) {
                     $this->server->createFile($uri . '/' . $newName, fopen($file['tmp_name'], 'r'));
                 }
                 break;
                 // @codeCoverageIgnoreEnd
         }
     }
     $response->setHeader('Location', $request->getUrl());
     $response->setStatus(302);
     return false;
 }
Example #7
0
 /**
  * Handles POST requests for tree operations.
  *
  * @param RequestInterface $request
  * @param ResponseInterface $response
  * @return bool
  */
 function httpPOST(RequestInterface $request, ResponseInterface $response)
 {
     $contentType = $request->getHeader('Content-Type');
     list($contentType) = explode(';', $contentType);
     if ($contentType !== 'application/x-www-form-urlencoded' && $contentType !== 'multipart/form-data') {
         return;
     }
     $postVars = $request->getPostData();
     if (!isset($postVars['sabreAction'])) {
         return;
     }
     $uri = $request->getPath();
     if ($this->server->emit('onBrowserPostAction', [$uri, $postVars['sabreAction'], $postVars])) {
         switch ($postVars['sabreAction']) {
             case 'mkcol':
                 if (isset($postVars['name']) && trim($postVars['name'])) {
                     // Using basename() because we won't allow slashes
                     list(, $folderName) = URLUtil::splitPath(trim($postVars['name']));
                     $this->server->createDirectory($uri . '/' . $folderName);
                 }
                 break;
                 // @codeCoverageIgnoreStart
             // @codeCoverageIgnoreStart
             case 'put':
                 if ($_FILES) {
                     $file = current($_FILES);
                 } else {
                     break;
                 }
                 list(, $newName) = URLUtil::splitPath(trim($file['name']));
                 if (isset($postVars['name']) && trim($postVars['name'])) {
                     $newName = trim($postVars['name']);
                 }
                 // Making sure we only have a 'basename' component
                 list(, $newName) = URLUtil::splitPath($newName);
                 if (is_uploaded_file($file['tmp_name'])) {
                     $this->server->createFile($uri . '/' . $newName, fopen($file['tmp_name'], 'r'));
                 }
                 break;
                 // @codeCoverageIgnoreEnd
         }
     }
     $response->setHeader('Location', $request->getUrl());
     $response->setStatus(302);
     return false;
 }
Example #8
0
 /**
  * We intercept this to handle POST requests on calendars.
  *
  * @param RequestInterface $request
  * @param ResponseInterface $response
  * @return null|false
  */
 function httpPost(RequestInterface $request, ResponseInterface $response)
 {
     $path = $request->getPath();
     // Only handling xml
     $contentType = $request->getHeader('Content-Type');
     if (strpos($contentType, 'application/xml') === false && strpos($contentType, 'text/xml') === false) {
         return;
     }
     // Making sure the node exists
     try {
         $node = $this->server->tree->getNodeForPath($path);
     } catch (NotFound $e) {
         return;
     }
     // CSRF protection
     $this->protectAgainstCSRF();
     $requestBody = $request->getBodyAsString();
     // If this request handler could not deal with this POST request, it
     // will return 'null' and other plugins get a chance to handle the
     // request.
     //
     // However, we already requested the full body. This is a problem,
     // because a body can only be read once. This is why we preemptively
     // re-populated the request body with the existing data.
     $request->setBody($requestBody);
     $message = $this->server->xml->parse($requestBody, $request->getUrl(), $documentType);
     switch ($documentType) {
         // Dealing with the 'share' document, which modified invitees on a
         // calendar.
         case '{' . self::NS_OWNCLOUD . '}share':
             // We can only deal with IShareableCalendar objects
             if (!$node instanceof IShareableAddressBook) {
                 return;
             }
             $this->server->transactionType = 'post-oc-addressbook-share';
             // Getting ACL info
             $acl = $this->server->getPlugin('acl');
             // If there's no ACL support, we allow everything
             if ($acl) {
                 /** @var \Sabre\DAVACL\Plugin $acl */
                 $acl->checkPrivileges($path, '{DAV:}write');
             }
             $node->updateShares($message->set, $message->remove);
             $response->setStatus(200);
             // Adding this because sending a response body may cause issues,
             // and I wanted some type of indicator the response was handled.
             $response->setHeader('X-Sabre-Status', 'everything-went-well');
             // Breaking the event chain
             return false;
     }
 }
Example #9
0
 /**
  * Turns a RequestInterface object into an array with settings that can be
  * fed to curl_setopt
  *
  * @param RequestInterface $request
  * @return array
  */
 protected function createCurlSettingsArray(RequestInterface $request)
 {
     $settings = $this->curlSettings;
     switch ($request->getMethod()) {
         case 'HEAD':
             $settings[CURLOPT_NOBODY] = true;
             $settings[CURLOPT_CUSTOMREQUEST] = 'HEAD';
             $settings[CURLOPT_POSTFIELDS] = '';
             $settings[CURLOPT_PUT] = false;
             break;
         case 'GET':
             $settings[CURLOPT_CUSTOMREQUEST] = 'GET';
             $settings[CURLOPT_POSTFIELDS] = '';
             $settings[CURLOPT_PUT] = false;
             break;
         default:
             $body = $request->getBody();
             if (is_resource($body)) {
                 // This needs to be set to PUT, regardless of the actual
                 // method used. Without it, INFILE will be ignored for some
                 // reason.
                 $settings[CURLOPT_PUT] = true;
                 $settings[CURLOPT_INFILE] = $request->getBody();
             } else {
                 // For security we cast this to a string. If somehow an array could
                 // be passed here, it would be possible for an attacker to use @ to
                 // post local files.
                 $settings[CURLOPT_POSTFIELDS] = (string) $body;
             }
             $settings[CURLOPT_CUSTOMREQUEST] = $request->getMethod();
             break;
     }
     $nHeaders = [];
     foreach ($request->getHeaders() as $key => $values) {
         foreach ($values as $value) {
             $nHeaders[] = $key . ': ' . $value;
         }
     }
     $settings[CURLOPT_HTTPHEADER] = $nHeaders;
     $settings[CURLOPT_URL] = $request->getUrl();
     // FIXME: CURLOPT_PROTOCOLS is currently unsupported by HHVM
     if (defined('CURLOPT_PROTOCOLS')) {
         $settings[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
     }
     // FIXME: CURLOPT_REDIR_PROTOCOLS is currently unsupported by HHVM
     if (defined('CURLOPT_REDIR_PROTOCOLS')) {
         $settings[CURLOPT_REDIR_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
     }
     return $settings;
 }
Example #10
0
 /**
  * We intercept this to handle POST requests on shared resources
  *
  * @param RequestInterface $request
  * @param ResponseInterface $response
  * @return null|bool
  */
 function httpPost(RequestInterface $request, ResponseInterface $response)
 {
     $path = $request->getPath();
     $contentType = $request->getHeader('Content-Type');
     // We're only interested in the davsharing content type.
     if (strpos($contentType, 'application/davsharing+xml') === false) {
         return;
     }
     $message = $this->server->xml->parse($request->getBody(), $request->getUrl(), $documentType);
     switch ($documentType) {
         case '{DAV:}share-resource':
             $this->shareResource($path, $message->sharees);
             $response->setStatus(200);
             // Adding this because sending a response body may cause issues,
             // and I wanted some type of indicator the response was handled.
             $response->setHeader('X-Sabre-Status', 'everything-went-well');
             // Breaking the event chain
             return false;
         default:
             throw new BadRequest('Unexpected document type: ' . $documentType . ' for this Content-Type');
     }
 }
Example #11
0
 /**
  * Sends a request to a HTTP server, and returns a response.
  *
  * Switches request URL for absolute URL
  *
  * @param RequestInterface $request
  * @return ResponseInterface
  */
 public function send(HTTP\RequestInterface $request)
 {
     $url = $request->getUrl();
     $absoluteUrl = $this->getAbsoluteUrl($url);
     $request->setUrl($absoluteUrl);
     return parent::send($request);
 }