Ejemplo n.º 1
0
 /**
  * Locks an uri
  *
  * The WebDAV lock request can be operated to either create a new lock on a file, or to refresh an existing lock
  * If a new lock is created, a full XML body should be supplied, containing information about the lock such as the type
  * of lock (shared or exclusive) and the owner of the lock
  *
  * If a lock is to be refreshed, no body should be supplied and there should be a valid If header containing the lock
  *
  * Additionally, a lock can be requested for a non-existent file. In these case we're obligated to create an empty file as per RFC4918:S7.3
  *
  * @param RequestInterface $request
  * @param ResponseInterface $response
  * @return bool
  */
 function httpLock(RequestInterface $request, ResponseInterface $response)
 {
     $uri = $request->getPath();
     $existingLocks = $this->getLocks($uri);
     if ($body = $request->getBodyAsString()) {
         // This is a new lock request
         $existingLock = null;
         // Checking if there's already non-shared locks on the uri.
         foreach ($existingLocks as $existingLock) {
             if ($existingLock->scope === LockInfo::EXCLUSIVE) {
                 throw new DAV\Exception\ConflictingLock($existingLock);
             }
         }
         $lockInfo = $this->parseLockRequest($body);
         $lockInfo->depth = $this->server->getHTTPDepth();
         $lockInfo->uri = $uri;
         if ($existingLock && $lockInfo->scope != LockInfo::SHARED) {
             throw new DAV\Exception\ConflictingLock($existingLock);
         }
     } else {
         // Gonna check if this was a lock refresh.
         $existingLocks = $this->getLocks($uri);
         $conditions = $this->server->getIfConditions($request);
         $found = null;
         foreach ($existingLocks as $existingLock) {
             foreach ($conditions as $condition) {
                 foreach ($condition['tokens'] as $token) {
                     if ($token['token'] === 'opaquelocktoken:' . $existingLock->token) {
                         $found = $existingLock;
                         break 3;
                     }
                 }
             }
         }
         // If none were found, this request is in error.
         if (is_null($found)) {
             if ($existingLocks) {
                 throw new DAV\Exception\Locked(reset($existingLocks));
             } else {
                 throw new DAV\Exception\BadRequest('An xml body is required for lock requests');
             }
         }
         // This must have been a lock refresh
         $lockInfo = $found;
         // The resource could have been locked through another uri.
         if ($uri != $lockInfo->uri) {
             $uri = $lockInfo->uri;
         }
     }
     if ($timeout = $this->getTimeoutHeader()) {
         $lockInfo->timeout = $timeout;
     }
     $newFile = false;
     // If we got this far.. we should go check if this node actually exists. If this is not the case, we need to create it first
     try {
         $this->server->tree->getNodeForPath($uri);
         // We need to call the beforeWriteContent event for RFC3744
         // Edit: looks like this is not used, and causing problems now.
         //
         // See Issue 222
         // $this->server->emit('beforeWriteContent',array($uri));
     } catch (DAV\Exception\NotFound $e) {
         // It didn't, lets create it
         $this->server->createFile($uri, fopen('php://memory', 'r'));
         $newFile = true;
     }
     $this->lockNode($uri, $lockInfo);
     $response->setHeader('Content-Type', 'application/xml; charset=utf-8');
     $response->setHeader('Lock-Token', '<opaquelocktoken:' . $lockInfo->token . '>');
     $response->setStatus($newFile ? 201 : 200);
     $response->setBody($this->generateLockResponse($lockInfo));
     // Returning false will interupt the event chain and mark this method
     // as 'handled'.
     return false;
 }