123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- <?php
- require_once(dirname(__FILE__) . '/http.php');
- class SimpleRealm {
- var $_type;
- var $_root;
- var $_username;
- var $_password;
-
- function SimpleRealm($type, $url) {
- $this->_type = $type;
- $this->_root = $url->getBasePath();
- $this->_username = false;
- $this->_password = false;
- }
-
- function stretch($url) {
- $this->_root = $this->_getCommonPath($this->_root, $url->getPath());
- }
-
- function _getCommonPath($first, $second) {
- $first = explode('/', $first);
- $second = explode('/', $second);
- for ($i = 0; $i < min(count($first), count($second)); $i++) {
- if ($first[$i] != $second[$i]) {
- return implode('/', array_slice($first, 0, $i)) . '/';
- }
- }
- return implode('/', $first) . '/';
- }
-
- function setIdentity($username, $password) {
- $this->_username = $username;
- $this->_password = $password;
- }
-
- function getUsername() {
- return $this->_username;
- }
-
- function getPassword() {
- return $this->_password;
- }
-
- function isWithin($url) {
- if ($this->_isIn($this->_root, $url->getBasePath())) {
- return true;
- }
- if ($this->_isIn($this->_root, $url->getBasePath() . $url->getPage() . '/')) {
- return true;
- }
- return false;
- }
-
- function _isIn($part, $whole) {
- return strpos($whole, $part) === 0;
- }
- }
- class SimpleAuthenticator {
- var $_realms;
-
- function SimpleAuthenticator() {
- $this->restartSession();
- }
-
- function restartSession() {
- $this->_realms = array();
- }
-
- function addRealm($url, $type, $realm) {
- $this->_realms[$url->getHost()][$realm] = new SimpleRealm($type, $url);
- }
-
- function setIdentityForRealm($host, $realm, $username, $password) {
- if (isset($this->_realms[$host][$realm])) {
- $this->_realms[$host][$realm]->setIdentity($username, $password);
- }
- }
-
- function _findRealmFromUrl($url) {
- if (! isset($this->_realms[$url->getHost()])) {
- return false;
- }
- foreach ($this->_realms[$url->getHost()] as $name => $realm) {
- if ($realm->isWithin($url)) {
- return $realm;
- }
- }
- return false;
- }
-
- function addHeaders(&$request, $url) {
- if ($url->getUsername() && $url->getPassword()) {
- $username = $url->getUsername();
- $password = $url->getPassword();
- } elseif ($realm = $this->_findRealmFromUrl($url)) {
- $username = $realm->getUsername();
- $password = $realm->getPassword();
- } else {
- return;
- }
- $this->addBasicHeaders($request, $username, $password);
- }
-
- function addBasicHeaders(&$request, $username, $password) {
- if ($username && $password) {
- $request->addHeaderLine(
- 'Authorization: Basic ' . base64_encode("$username:$password"));
- }
- }
- }
- ?>
|