Skip to content

Commit 25e8352

Browse files
committed
Added Basic Authentication
Moved all HTTP response header settings to HTTP_Response
1 parent c8a4080 commit 25e8352

File tree

4 files changed

+136
-26
lines changed

4 files changed

+136
-26
lines changed

lib/Sabre.includes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/* Utilities */
44
include 'Sabre/PHP/Exception.php';
55
include 'Sabre/HTTP/Response.php';
6+
include 'Sabre/HTTP/BasicAuth.php';
67

78
/* Basics */
89
include 'Sabre/DAV/Lock.php';

lib/Sabre/DAV/Server.php

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ public function setBaseUri($uri) {
111111
*/
112112
protected function httpOptions() {
113113

114-
$this->addHeader('Allow',strtoupper(implode(' ',$this->getAllowedMethods())));
114+
$this->httpResponse->setHeader('Allow',strtoupper(implode(' ',$this->getAllowedMethods())));
115115
if ($this->tree->supportsLocks()) {
116-
$this->addHeader('DAV','1,2,3');
116+
$this->httpResponse->setHeader('DAV','1,2,3');
117117
} else {
118-
$this->addHeader('DAV','1,3');
118+
$this->httpResponse->setHeader('DAV','1,3');
119119
}
120-
$this->addHeader('MS-Author-Via','DAV');
120+
$this->httpResponse->setHeader('MS-Author-Via','DAV');
121121

122122
}
123123

@@ -132,9 +132,9 @@ protected function httpGet() {
132132

133133
$nodeInfo = $this->tree->getNodeInfo($this->getRequestUri(),0);
134134

135-
if ($nodeInfo[0]['size']) $this->addHeader('Content-Length',$nodeInfo[0]['size']);
135+
if ($nodeInfo[0]['size']) $this->httpResponse->setHeader('Content-Length',$nodeInfo[0]['size']);
136136

137-
$this->addHeader('Content-Type', 'application/octet-stream');
137+
$this->httpResponse->setHeader('Content-Type', 'application/octet-stream');
138138
echo $this->tree->get($this->getRequestUri());
139139

140140
}
@@ -151,8 +151,8 @@ protected function httpGet() {
151151
protected function httpHead() {
152152

153153
$nodeInfo = $this->tree->getNodeInfo($this->getRequestUri(),0);
154-
if ($nodeInfo[0]['size']) $this->addHeader('Content-Length',$nodeInfo[0]['size']);
155-
$this->addHeader('Content-Type', 'application/octet-stream');
154+
if ($nodeInfo[0]['size']) $this->httpResponse->setHeader('Content-Length',$nodeInfo[0]['size']);
155+
$this->httpResponse->setHeader('Content-Type', 'application/octet-stream');
156156

157157
}
158158

@@ -219,7 +219,7 @@ protected function httpPropfind() {
219219

220220
// This is a multi-status response
221221
$this->httpResponse->sendStatus(207);
222-
$this->addHeader('Content-Type','text/xml; charset="utf-8"');
222+
$this->httpResponse->setHeader('Content-Type','text/xml; charset="utf-8"');
223223
$data = $this->generatePropfindResponse($fileList,$properties);
224224
echo $data;
225225

@@ -323,7 +323,7 @@ protected function httpPOST() {
323323
}
324324

325325
// We assume > 5.1.2, which has the header injection attack prevention
326-
if (isset($_POST['redirectUrl']) && is_string($_POST['redirectUrl'])) header('Location: ' . $_POST['redirectUrl']);
326+
if (isset($_POST['redirectUrl']) && is_string($_POST['redirectUrl'])) $this->httpResponse->setHeader('Location', $_POST['redirectUrl']);
327327

328328
}
329329

@@ -478,7 +478,7 @@ protected function httpLock() {
478478
}
479479

480480
$this->tree->lockNode($uri,$lockInfo);
481-
$this->addHeader('Lock-Token','opaquelocktoken:' . $lockInfo->token);
481+
$this->httpResponse->setHeader('Lock-Token','opaquelocktoken:' . $lockInfo->token);
482482
echo $this->generateLockResponse($lockInfo);
483483

484484
}
@@ -561,19 +561,6 @@ protected function getAllowedMethods() {
561561

562562
}
563563

564-
/**
565-
* Adds an HTTP response header
566-
*
567-
* @param string $name
568-
* @param string $value
569-
* @return void
570-
*/
571-
protected function addHeader($name,$value) {
572-
573-
header($name . ': ' . str_replace(array("\n","\r"),array('\n','\r'),$value));
574-
575-
}
576-
577564
/**
578565
* Gets the uri for the request, keeping the base uri into consideration
579566
*

lib/Sabre/HTTP/BasicAuth.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
/**
4+
* HTTP Basic Authentication handler
5+
*
6+
* Use this class for easy http authentication setup
7+
*
8+
* @package Sabre
9+
* @subpackage HTTP
10+
* @version $Id$
11+
* @copyright Copyright (C) 2009 Rooftop Solutions. All rights reserved.
12+
* @author Evert Pot (http://www.rooftopsolutions.nl/)
13+
* @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
14+
*/
15+
class Sabre_HTTP_BasicAuth {
16+
17+
/**
18+
* The realm will be displayed in the dialog boxes
19+
*
20+
* This identifier can be changed through setRealm()
21+
*
22+
* @var string
23+
*/
24+
protected $realm = 'SabreDAV';
25+
26+
/**
27+
* HTTP response helper
28+
*
29+
* @var Sabre_HTTP_Response
30+
*/
31+
protected $httpResponse;
32+
33+
/**
34+
* __construct
35+
*
36+
* @return void
37+
*/
38+
public function __construct() {
39+
40+
$this->httpResponse = new Sabre_HTTP_Response();
41+
42+
}
43+
44+
/**
45+
* Returns the supplied username and password.
46+
*
47+
* The returned array has two values:
48+
* * 0 - username
49+
* * 1 - password
50+
*
51+
* If nothing was supplied, 'false' will be returned
52+
*
53+
* @return mixed
54+
*/
55+
public function getUserPass() {
56+
57+
// Apache
58+
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
59+
60+
$username = $_SERVER['PHP_AUTH_USER'];
61+
$password = $_SERVER['PHP_AUTH_PW'];
62+
63+
return array($username,$password);
64+
65+
}
66+
67+
// IIS
68+
if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
69+
70+
return explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
71+
72+
}
73+
74+
return false;
75+
76+
}
77+
78+
/**
79+
* Sets the realm
80+
*
81+
* The realm is often displayed in authentication dialog boxes
82+
* Commonly an application name displayed here
83+
*
84+
* @param mixed $realm
85+
* @return void
86+
*/
87+
public function setRealm($realm) {
88+
89+
$this->realm = $realm;
90+
91+
}
92+
93+
/**
94+
* Returns an HTTP 401 header, forcing login
95+
*
96+
* This should be called when username and password are incorrect, or not supplied at all
97+
*
98+
* @return void
99+
*/
100+
public function requireLogin() {
101+
102+
$this->httpResponse->setHeader('WWW-Authenticate','Basic realm="' . $this->realm . '"');
103+
$this->httpResponse->sendStatus(401);
104+
105+
}
106+
107+
}

lib/Sabre/HTTP/Response.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
* Sabre_HTTP_Response
55
*
66
* @package Sabre
7+
* @subpackage DAV
78
* @version $Id$
8-
* @copyright Copyright (C) 2007 Rooftop Solutions. All rights reserved.
9+
* @copyright Copyright (C) 2007-2009 Rooftop Solutions. All rights reserved.
910
* @author Evert Pot (http://www.rooftopsolutions.nl/)
10-
* @license licence http://www.freebsd.org/copyright/license.html BSD License (4 Clause)
11+
* @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
1112
*/
1213
class Sabre_HTTP_Response {
1314

@@ -54,4 +55,18 @@ public function sendStatus($code) {
5455

5556
}
5657

58+
/**
59+
* Sets an HTTP header for the response
60+
*
61+
* @param string $name
62+
* @param string $value
63+
* @return void
64+
*/
65+
public function setHeader($name, $value) {
66+
67+
$value = str_replace(array("\r","\n"),array('\r','\n'),$value);
68+
header($name . ': ' . $value);
69+
70+
}
71+
5772
}

0 commit comments

Comments
 (0)