/**
  * Returns the HTTP 405 Method Not Allowed status code
  * 
  * This will only be called when an unknown/unsupported HTTP method is used. So
  * We'll return the correct status code and explain which methods are allowed.
  * 
  * @param DAV_Resource $resource
  * @return void
  * @throws DAV_Status
  */
 public function handle($resource)
 {
     $allow = implode(', ', self::$ALLOWED_METHODS);
     DAV::header("Allow: {$allow}");
     $status = new DAV_Status(DAV::HTTP_METHOD_NOT_ALLOWED, "Allowed methods: {$allow}");
     $status->output();
 }
Example #2
0
    public function testOutput()
    {
        $_SERVER['SERVER_NAME'] = 'example.org';
        $_SERVER['SERVER_PORT'] = 80;
        $status300 = new DAV_Status(300, 'http://example.org/some/new/path');
        ob_start();
        $status300->output();
        $output300 = ob_get_clean();
        $this->assertSame(<<<EOS
Content-Type: text/plain; charset=US-ASCII
Location: http://example.org/some/new/path
HTTP/1.1 300 Multiple Choices
http://example.org/some/new/path
EOS
, $output300, 'DAV_Status with code 300 should generate the right output');
        $status400 = new DAV_Status(400, 'some free error text');
        ob_start();
        $status400->output();
        $output400 = ob_get_clean();
        // Not the double status line; the first is a header, the second is part of the body!
        $this->assertSame(<<<EOS
Content-Type: text/plain; charset="UTF-8"
HTTP/1.1 400 Bad Request
HTTP/1.1 400 Bad Request
some free error text
EOS
, $output400, 'DAV_Status with code 400 should generate the right output');
        $status409 = new DAV_Status(409, array(DAV::COND_ALLOWED_PRINCIPAL => '<tag>someXML?</tag>'));
        ob_start();
        $status409->output();
        $output409 = ob_get_clean();
        // Not the double status line; the first is a header, the second is part of the body!
        $this->assertSame(<<<EOS
Content-Type: application/xml; charset="UTF-8"
HTTP/1.1 409 Conflict
<?xml version="1.0" encoding="utf-8"?>
<D:error xmlns:D="DAV:">
<D:allowed-principal><tag>someXML?</tag></D:allowed-principal>
</D:error>
EOS
, $output409, 'DAV_Status with code 409 should generate the right output');
    }
Example #3
0
 /**
  * The default exception handler, so we always output nice errors if an exception is uncaught
  * @param   exception  $exception  The (uncaught) exception
  * @return  void
  */
 public static function exception_handler($exception)
 {
     if (!$exception instanceof DAV_Status) {
         trigger_error(strval($exception), E_USER_WARNING);
         $exception = new DAV_Status(DAV::HTTP_INTERNAL_SERVER_ERROR, "Unexpected error occured at " . date('r'));
     }
     $exception->output();
 }
Example #4
0
 /**
  * Serve WebDAV HTTP request.
  * @param DAV_Registry $registry
  */
 public function handleRequest()
 {
     // We want to catch every exception thrown in this code, and report about it
     // to the user appropriately.
     $shallow_lock = false;
     try {
         $shallow_lock = $this->check_if_headers();
         $resource = DAV::$REGISTRY->resource(DAV::getPath());
         if (!$resource || !$resource->isVisible() and in_array($_SERVER['REQUEST_METHOD'], array('ACL', 'COPY', 'DELETE', 'GET', 'HEAD', 'MOVE', 'OPTIONS', 'POST', 'PROPFIND', 'PROPPATCH', 'REPORT', 'UNLOCK'))) {
             throw new DAV_Status(DAV::HTTP_NOT_FOUND);
         }
         if ('/' !== substr(DAV::getPath(), -1) && ($resource && $resource instanceof DAV_Collection || 'MKCOL' === $_SERVER['REQUEST_METHOD'])) {
             $newPath = DAV::getPath() . '/';
             DAV::setPath(DAV::encodeURIFullPath($newPath));
             DAV::header(array('Content-Location' => DAV::path2uri($newPath)));
         }
         $this->handle($resource);
     } catch (Exception $e) {
         if (!$e instanceof DAV_Status) {
             $e = new DAV_Status(DAV::HTTP_INTERNAL_SERVER_ERROR, "{$e}");
         }
         $e->output();
     }
     if ($shallow_lock) {
         DAV::$REGISTRY->shallowUnlock();
     }
     if (DAV_Multistatus::active()) {
         DAV_Multistatus::inst()->close();
     }
 }
Example #5
0
            call_user_func(DAV::$UNAUTHORIZED);
            return;
        } elseif (!empty($this->conditions)) {
            $headers = array('status' => $status, 'Content-Type' => 'application/xml; charset="UTF-8"');
            if ($this->location) {
                $headers['Location'] = DAV::encodeURIFullPath($this->location);
            }
            DAV::header($headers);
            echo DAV::xml_header() . '<D:error xmlns:D="DAV:">';
            foreach ($this->conditions as $condition => $xml) {
                echo "\n<D:" . $condition;
                echo $xml ? ">{$xml}</D:{$condition}>" : "/>";
            }
            echo "\n</D:error>";
        } elseif ($this->location) {
            DAV::redirect($status, $this->location);
        } else {
            if (self::$RESPONSE_GENERATOR && in_array($_SERVER['REQUEST_METHOD'], array('GET', 'POST'))) {
                DAV::header(array('status' => $status));
                call_user_func(self::$RESPONSE_GENERATOR, $status, $this->getMessage());
            } else {
                DAV::header(array('status' => $status, 'Content-Type' => 'text/plain; charset="UTF-8"'));
                echo "HTTP/1.1 " . DAV::status_code($status) . "\n" . $this->getMessage();
            }
        }
    }
}
// class DAV_Status
DAV_Status::$OK = new DAV_Status(DAV::HTTP_OK);
DAV_Status::$NOT_FOUND = new DAV_Status(DAV::HTTP_NOT_FOUND);