mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 11:06:15 +00:00
da79dff7d7
- Dispatcher sets a Controller::here variable with the real URL used to access the page, so that tag generators can that use an url (linkTo and formTag for example) use the real url, not guess it from the controller and action names which often fails - Log class works more reliably and a LogError() shortcut function was added - Nstring class added, to store string-related functions (there are just four yet, including a random password generator and an string-to-array splitter - SimpleTest library (with Rephlux) included in /vendors; I've tweaked SimpleScorer::inCli() function, because it didn't work on my setup, it should work everywhere now (it checks for empty REQUEST_METHOD, which should only be empty in CLI) git-svn-id: https://svn.cakephp.org/repo/trunk/cake@248 3807eeeb-6ff5-0310-8944-8be069107fe0
219 lines
No EOL
7.4 KiB
PHP
219 lines
No EOL
7.4 KiB
PHP
<?php
|
|
/**
|
|
* Base include file for SimpleTest
|
|
* @package SimpleTest
|
|
* @subpackage WebTester
|
|
* @version $Id$
|
|
*/
|
|
/**
|
|
* include http class
|
|
*/
|
|
require_once(dirname(__FILE__) . '/http.php');
|
|
|
|
/**
|
|
* Represents a single security realm's identity.
|
|
* @package SimpleTest
|
|
* @subpackage WebTester
|
|
*/
|
|
class SimpleRealm {
|
|
var $_type;
|
|
var $_root;
|
|
var $_username;
|
|
var $_password;
|
|
|
|
/**
|
|
* 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.
|
|
* @access public
|
|
*/
|
|
function SimpleRealm($type, $url) {
|
|
$this->_type = $type;
|
|
$this->_root = $url->getBasePath();
|
|
$this->_username = false;
|
|
$this->_password = false;
|
|
}
|
|
|
|
/**
|
|
* Adds another location to the realm.
|
|
* @param SimpleUrl $url Somewhere in realm.
|
|
* @access public
|
|
*/
|
|
function stretch($url) {
|
|
$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.
|
|
* @access private
|
|
*/
|
|
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) . '/';
|
|
}
|
|
|
|
/**
|
|
* Sets the identity to try within this realm.
|
|
* @param string $username Username in authentication dialog.
|
|
* @param string $username Password in authentication dialog.
|
|
* @access public
|
|
*/
|
|
function setIdentity($username, $password) {
|
|
$this->_username = $username;
|
|
$this->_password = $password;
|
|
}
|
|
|
|
/**
|
|
* Accessor for current identity.
|
|
* @return string Last succesful username.
|
|
* @access public
|
|
*/
|
|
function getUsername() {
|
|
return $this->_username;
|
|
}
|
|
|
|
/**
|
|
* Accessor for current identity.
|
|
* @return string Last succesful password.
|
|
* @access public
|
|
*/
|
|
function getPassword() {
|
|
return $this->_password;
|
|
}
|
|
|
|
/**
|
|
* Test to see if the URL is within the directory
|
|
* tree of the realm.
|
|
* @param SimpleUrl $url URL to test.
|
|
* @return boolean True if subpath.
|
|
* @access public
|
|
*/
|
|
function isWithin($url) {
|
|
return (strpos($url->getBasePath(), $this->_root) === 0);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Manages security realms.
|
|
* @package SimpleTest
|
|
* @subpackage WebTester
|
|
*/
|
|
class SimpleAuthenticator {
|
|
var $_realms;
|
|
|
|
/**
|
|
* Clears the realms.
|
|
* @access public
|
|
*/
|
|
function SimpleAuthenticator() {
|
|
$this->restartSession();
|
|
}
|
|
|
|
/**
|
|
* Starts with no realms set up.
|
|
* @access public
|
|
*/
|
|
function restartSession() {
|
|
$this->_realms = array();
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
* @access public
|
|
*/
|
|
function addRealm($url, $type, $realm) {
|
|
$this->_realms[$url->getHost()][$realm] = new SimpleRealm($type, $url);
|
|
}
|
|
|
|
/**
|
|
* Sets the current identity to be presented
|
|
* against that realm.
|
|
* @param string $host Server hosting realm.
|
|
* @param string $realm Name of realm.
|
|
* @param string $username Username for realm.
|
|
* @param string $password Password for realm.
|
|
* @access public
|
|
*/
|
|
function setIdentityForRealm($host, $realm, $username, $password) {
|
|
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.
|
|
* @access private
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* Presents the appropriate headers for this location.
|
|
* @param SimpleHttpRequest $request Request to modify.
|
|
* @param SimpleUrl $url Base of realm.
|
|
* @access public
|
|
*/
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
* @access public
|
|
* @static
|
|
*/
|
|
function addBasicHeaders(&$request, $username, $password) {
|
|
if ($username && $password) {
|
|
$request->addHeaderLine(
|
|
'Authorization: Basic ' . base64_encode("$username:$password"));
|
|
}
|
|
}
|
|
}
|
|
?>
|