|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00011 require_once(dirname(__FILE__) . '/http.php'); 00012 00018 class SimpleRealm { 00019 var $_type; 00020 var $_root; 00021 var $_username; 00022 var $_password; 00023 00032 function SimpleRealm($type, $url) { 00033 $this->_type = $type; 00034 $this->_root = $url->getBasePath(); 00035 $this->_username = false; 00036 $this->_password = false; 00037 } 00038 00044 function stretch($url) { 00045 $this->_root = $this->_getCommonPath($this->_root, $url->getPath()); 00046 } 00047 00055 function _getCommonPath($first, $second) { 00056 $first = explode('/', $first); 00057 $second = explode('/', $second); 00058 for ($i = 0; $i < min(count($first), count($second)); $i++) { 00059 if ($first[$i] != $second[$i]) { 00060 return implode('/', array_slice($first, 0, $i)) . '/'; 00061 } 00062 } 00063 return implode('/', $first) . '/'; 00064 } 00065 00072 function setIdentity($username, $password) { 00073 $this->_username = $username; 00074 $this->_password = $password; 00075 } 00076 00082 function getUsername() { 00083 return $this->_username; 00084 } 00085 00091 function getPassword() { 00092 return $this->_password; 00093 } 00094 00102 function isWithin($url) { 00103 if ($this->_isIn($this->_root, $url->getBasePath())) { 00104 return true; 00105 } 00106 if ($this->_isIn($this->_root, $url->getBasePath() . $url->getPage() . '/')) { 00107 return true; 00108 } 00109 return false; 00110 } 00111 00121 function _isIn($part, $whole) { 00122 return strpos($whole, $part) === 0; 00123 } 00124 } 00125 00131 class SimpleAuthenticator { 00132 var $_realms; 00133 00138 function SimpleAuthenticator() { 00139 $this->restartSession(); 00140 } 00141 00146 function restartSession() { 00147 $this->_realms = array(); 00148 } 00149 00166 function addRealm($url, $type, $realm) { 00167 $this->_realms[$url->getHost()][$realm] = new SimpleRealm($type, $url); 00168 } 00169 00179 function setIdentityForRealm($host, $realm, $username, $password) { 00180 if (isset($this->_realms[$host][$realm])) { 00181 $this->_realms[$host][$realm]->setIdentity($username, $password); 00182 } 00183 } 00184 00191 function _findRealmFromUrl($url) { 00192 if (! isset($this->_realms[$url->getHost()])) { 00193 return false; 00194 } 00195 foreach ($this->_realms[$url->getHost()] as $name => $realm) { 00196 if ($realm->isWithin($url)) { 00197 return $realm; 00198 } 00199 } 00200 return false; 00201 } 00202 00209 function addHeaders(&$request, $url) { 00210 if ($url->getUsername() && $url->getPassword()) { 00211 $username = $url->getUsername(); 00212 $password = $url->getPassword(); 00213 } elseif ($realm = $this->_findRealmFromUrl($url)) { 00214 $username = $realm->getUsername(); 00215 $password = $realm->getPassword(); 00216 } else { 00217 return; 00218 } 00219 $this->addBasicHeaders($request, $username, $password); 00220 } 00221 00231 function addBasicHeaders(&$request, $username, $password) { 00232 if ($username && $password) { 00233 $request->addHeaderLine( 00234 'Authorization: Basic ' . base64_encode("$username:$password")); 00235 } 00236 } 00237 } 00238 ?>