/** * Parses a piece of XML with <D:href> pieces * * @param string $hrefs * @return DAV_Element_href * @throws DAV_Status */ public static function parse_hrefs($hrefs) { $href = new DAV_Element_href(); if (!preg_match('@^\\s*(?:<D:href(?:\\s+[^>]*)?>\\s*[^\\s<]+\\s*</D:href>\\s*)*$@', $hrefs)) { return $href; } preg_match_all('@<D:href(?:\\s+[^>]*)?>\\s*([^\\s<]+)\\s*</D:href>@', $hrefs, $matches); foreach ($matches[1] as $match) { $href->addURI(DAV::parseURI($match, false)); } return $href; }
/** * @expectedException PHPUnit_Framework_Error_Warning */ public function testParseURI() { $this->assertSame(DAV::parseURI('https://webdav.org/some/path/to/a/file.txt'), '/some/path/to/a/file.txt', 'DAV::parseURI() should return the correct path for a regular uri'); try { DAV::parseURI('https://non-webdav.org/some/path/to/a/file.txt'); $this->assertTrue(false, 'DAV::parseURI() should throw an DAV_Status exception when the uri is out of scope of this server'); } catch (DAV_Status $exception) { $this->assertSame(400, $exception->getCode(), 'DAV::parseURI() should throw an DAV_Status exception with code 400 when the uri is out of scope of this server'); } try { $this->assertSame('/some/path/to/a/file.txt', DAV::parseURI('https://non-webdav.org/some/path/to/a/file.txt', false), 'DAV::parseURI() should return the correct path when it is allowed to have the uri out of scope of this server'); } catch (DAV_Status $exception) { $this->assertTrue(false, 'DAV::parseURI() should not throw an DAV_Status exception when it is allowed to have the uri out of scope of this server'); } $_SERVER['PHP_AUTH_USER'] = '******'; $this->assertSame(DAV::parseURI('https://niek@webdav.org/some/path/to/a/file.txt'), '/some/path/to/a/file.txt', 'DAV::parseURI() should return the correct path for an uri with username'); }
/** * Parses the If: header. * Puts its results into $this->if_header. * @return void * @throws DAV_Status if there's a parse error. */ private function init_if_header() { if (!isset($_SERVER['HTTP_IF'])) { return; } $pos = 0; // Outer parser loop. Iterates over (No-)Tag-Lists while ($token = self::if_header_lexer($pos)) { $path = DAV::getPath(); // check for URI if ($token[0] === 'URI') { // It's a tagged list! $path = DAV::parseURI($token[1]); // May throw an exception if (!($token = self::if_header_lexer($pos))) { throw new DAV_Status(DAV::HTTP_BAD_REQUEST, "Unexpected end of If: header: {$_SERVER['HTTP_IF']}"); } } // sanity check if ($token[0] !== "CHAR" || $token[1] !== '(') { throw new DAV_Status(DAV::HTTP_BAD_REQUEST, "Error while parsing If: header: Found '{$token[1]}' where '(' was expected."); } // Initialize inner parser loop: $etag = null; $notetags = $locks = $notlocks = array(); // Inner parser loop: while (($token = self::if_header_lexer($pos)) && !($token[0] === 'CHAR' && $token[1] === ')')) { // Initialize $bool: if ($token[0] === 'NOT') { $bool = false; if (!($token = self::if_header_lexer($pos))) { throw new DAV_Status(DAV::HTTP_BAD_REQUEST, "Unexpected end header If: {$_SERVER['HTTP_IF']}"); } } else { $bool = true; } switch ($token[0]) { case 'URI': DAV::$SUBMITTEDTOKENS[$token[1]] = $token[1]; if ($bool) { $locks[$token[1]] = $token[1]; } else { $notlocks[$token[1]] = $token[1]; } break; case 'ETAG': if ($bool && $etag) { throw new DAV_Status(DAV::HTTP_BAD_REQUEST, 'Multiple etags required on resource.'); } if ($bool) { $etag = $token[1]; } else { $notetags[$token[1]] = $token[1]; } break; default: throw new DAV_Status(DAV::HTTP_BAD_REQUEST, <<<EOS Error while parsing If: header: Found "{$token[1]}" where "<" or "[" was expected. EOS ); } // switch($token[0]) } // while // Shared locks are not supported, so any request with multiple lock tokens // for one URI can never succeed. if (1 < count($locks)) { throw new DAV_Status(DAV::HTTP_PRECONDITION_FAILED, DAV::COND_LOCK_TOKEN_MATCHES_REQUEST_URI); } $this->if_header[$path] = array('etag' => $etag, 'notetags' => $notetags, 'lock' => array_shift($locks), 'notlocks' => $notlocks); } // while }
/** * Set the (requested) path * @param string $urlencodedPath The URL encoded path * @return void */ public static function setPath($urlencodedPath) { $cache = DAV_Cache::inst('DAV'); $cache->set('path', DAV::parseURI($urlencodedPath, true)); }