public function testStatus_code() { $this->assertSame('207 Multi-Status', DAV::status_code(DAV::HTTP_MULTI_STATUS), 'DAV::status_code() should return \'207 Multi-Status\' with a 207 parameter'); $this->assertSame('414 Request-URI Too Long', DAV::status_code(DAV::HTTP_REQUEST_URI_TOO_LONG), 'DAV::status_code() should return \'414 Request-URI Too Long\' with a 414 parameter'); }
/** * Serializes this object to XML. * Must only be called by DAV_Multistatus. * @return string XML */ public function toXML() { // Set the default status to 200: foreach (array_keys($this->properties) as $p) { if (!isset($this->status[$p])) { $this->status[$p] = DAV_Status::$OK; } } // Rearrange by status: $hashed_statusses = array(); $hashed_properties = array(); foreach ($this->status as $p => $s) { $hash = md5($s->getCode() . "\t" . $s->getMessage() . "\t" . implode("\t", $s->conditions) . $s->location); $hashed_statusses[$hash] = $s; $hashed_properties[$hash][] = $p; } // Start generating some XML: $xml = "\n<D:response><D:href>" . DAV::xmlescape(DAV::encodeURIFullPath($this->path)) . "</D:href>"; // Each defined status gets its own <D:propstat> element: foreach ($hashed_statusses as $hash => $status) { $xml .= "\n<D:propstat><D:prop>"; foreach ($hashed_properties[$hash] as $prop) { list($namespaceURI, $localName) = explode(' ', $prop); $xml .= "\n<"; switch ($namespaceURI) { case 'DAV:': $xml .= "D:{$localName}"; break; case '': $xml .= "{$localName}"; break; default: $xml .= "ns:{$localName} xmlns:ns=\"{$namespaceURI}\""; } if (isset($this->properties[$prop])) { $xml .= '>' . $this->properties[$prop] . '</'; switch ($namespaceURI) { case 'DAV:': $xml .= "D:"; break; case '': break; default: $xml .= "ns:"; } $xml .= "{$localName}>"; } else { $xml .= '/>'; } } // And give the status itself! $xml .= "\n</D:prop>\n<D:status>HTTP/1.1 " . DAV::status_code($status->getCode()) . '</D:status>'; if (!empty($status->conditions)) { $xml .= "\n<D:error>"; foreach ($status->conditions as $condition) { $xml .= @DAV::$CONDITIONS[$condition]; } $xml .= "</D:error>"; } $message = $status->getMessage(); if (!empty($message)) { $xml .= "\n<D:responsedescription>" . DAV::xmlescape($message) . '</D:responsedescription>'; } $xml .= "\n</D:propstat>"; } $xml .= "\n</D:response>"; return $xml; }
/** * Sends this status to client. * @return void */ public function output() { $status = $this->getCode(); if ($status < 300) { throw new DAV_Status(DAV::HTTP_INTERNAL_SERVER_ERROR, "DAV_Status object with status {$status} " . var_export($this->getMessage(), true)); } if (DAV::HTTP_UNAUTHORIZED === $status && DAV::$UNAUTHORIZED) { 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(); } } }