Source for file authentication.php
Documentation is available at authentication.php
* Base include file for SimpleTest
* @version $Id: authentication.php 1720 2008-04-07 02:32:43Z lastcraft $
require_once(dirname(__FILE__
) .
'/http.php');
* Represents a single security realm's identity.
* Starts with the initial entry directory.
* @param string $type Authentication type for this
* realm. Only Basic authentication
* is currently supported.
* @param SimpleUrl $url Somewhere in realm.
$this->_root =
$url->getBasePath();
* Adds another location to the realm.
* @param SimpleUrl $url Somewhere in realm.
$this->_root =
$this->_getCommonPath($this->_root, $url->getPath());
* Finds the common starting path.
* @param string $first Path to compare.
* @param string $second Path to compare.
* @return string Common directories.
function _getCommonPath($first, $second) {
if ($first[$i] !=
$second[$i]) {
* Sets the identity to try within this realm.
* @param string $username Username in authentication dialog.
* @param string $username Password in authentication dialog.
* Accessor for current identity.
* @return string Last succesful username.
* Accessor for current identity.
* @return string Last succesful password.
* Test to see if the URL is within the directory
* @param SimpleUrl $url URL to test.
* @return boolean True if subpath.
if ($this->_isIn($this->_root, $url->getBasePath())) {
if ($this->_isIn($this->_root, $url->getBasePath() .
$url->getPage() .
'/')) {
* Tests to see if one string is a substring of
* @param string $part Small bit.
* @param string $whole Big bit.
* @return boolean True if the small bit is
function _isIn($part, $whole) {
return strpos($whole, $part) ===
0;
* Manages security realms.
* Starts with no realms set up.
* Adds a new realm centered the current URL.
* Browsers vary wildly on their behaviour in this
* regard. Mozilla ignores the realm and presents
* only when challenged, wasting bandwidth. IE
* just carries on presenting until a new challenge
* occours. SimpleTest tries to follow the spirit of
* the original standards committee and treats the
* base URL as the root of a file tree shaped realm.
* @param SimpleUrl $url Base of realm.
* @param string $type Authentication type for this
* realm. Only Basic authentication
* is currently supported.
* @param string $realm Name of realm.
function addRealm($url, $type, $realm) {
* Sets the current identity to be presented
* @param string $host Server hosting realm.
* @param string $realm Name of realm.
* @param string $username Username for realm.
* @param string $password Password for realm.
if (isset
($this->_realms[$host][$realm])) {
$this->_realms[$host][$realm]->setIdentity($username, $password);
* Finds the name of the realm by comparing URLs.
* @param SimpleUrl $url URL to test.
* @return SimpleRealm Name of realm.
function _findRealmFromUrl($url) {
if (! isset
($this->_realms[$url->getHost()])) {
foreach ($this->_realms[$url->getHost()] as $name =>
$realm) {
if ($realm->isWithin($url)) {
* Presents the appropriate headers for this location.
* @param SimpleHttpRequest $request Request to modify.
* @param SimpleUrl $url Base of realm.
if ($url->getUsername() &&
$url->getPassword()) {
$username =
$url->getUsername();
$password =
$url->getPassword();
} elseif ($realm =
$this->_findRealmFromUrl($url)) {
$username =
$realm->getUsername();
$password =
$realm->getPassword();
* Presents the appropriate headers for this
* location for basic authentication.
* @param SimpleHttpRequest $request Request to modify.
* @param string $username Username for realm.
* @param string $password Password for realm.
if ($username &&
$password) {
Documentation generated on Sun, 04 May 2008 09:21:15 -0500 by phpDocumentor 1.3.0