PageRenderTime 130ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/simpletest/user_agent.php

https://bitbucket.org/valmy/openx
PHP | 333 lines | 150 code | 25 blank | 158 comment | 19 complexity | a95d41105e6f834463f31771675e88e4 MD5 | raw file
  1. <?php
  2. /**
  3. * Base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage WebTester
  6. * @version $Id$
  7. */
  8. /**#@+
  9. * include other SimpleTest class files
  10. */
  11. require_once(dirname(__FILE__) . '/cookies.php');
  12. require_once(dirname(__FILE__) . '/http.php');
  13. require_once(dirname(__FILE__) . '/encoding.php');
  14. require_once(dirname(__FILE__) . '/authentication.php');
  15. /**#@-*/
  16. if (! defined('DEFAULT_MAX_REDIRECTS')) {
  17. define('DEFAULT_MAX_REDIRECTS', 3);
  18. }
  19. if (! defined('DEFAULT_CONNECTION_TIMEOUT')) {
  20. define('DEFAULT_CONNECTION_TIMEOUT', 15);
  21. }
  22. /**
  23. * Fetches web pages whilst keeping track of
  24. * cookies and authentication.
  25. * @package SimpleTest
  26. * @subpackage WebTester
  27. */
  28. class SimpleUserAgent {
  29. var $_cookie_jar;
  30. var $_cookies_enabled = true;
  31. var $_authenticator;
  32. var $_max_redirects = DEFAULT_MAX_REDIRECTS;
  33. var $_proxy = false;
  34. var $_proxy_username = false;
  35. var $_proxy_password = false;
  36. var $_connection_timeout = DEFAULT_CONNECTION_TIMEOUT;
  37. var $_additional_headers = array();
  38. /**
  39. * Starts with no cookies, realms or proxies.
  40. * @access public
  41. */
  42. function SimpleUserAgent() {
  43. $this->_cookie_jar = &new SimpleCookieJar();
  44. $this->_authenticator = &new SimpleAuthenticator();
  45. }
  46. /**
  47. * Removes expired and temporary cookies as if
  48. * the browser was closed and re-opened. Authorisation
  49. * has to be obtained again as well.
  50. * @param string/integer $date Time when session restarted.
  51. * If omitted then all persistent
  52. * cookies are kept.
  53. * @access public
  54. */
  55. function restart($date = false) {
  56. $this->_cookie_jar->restartSession($date);
  57. $this->_authenticator->restartSession();
  58. }
  59. /**
  60. * Adds a header to every fetch.
  61. * @param string $header Header line to add to every
  62. * request until cleared.
  63. * @access public
  64. */
  65. function addHeader($header) {
  66. $this->_additional_headers[] = $header;
  67. }
  68. /**
  69. * Ages the cookies by the specified time.
  70. * @param integer $interval Amount in seconds.
  71. * @access public
  72. */
  73. function ageCookies($interval) {
  74. $this->_cookie_jar->agePrematurely($interval);
  75. }
  76. /**
  77. * Sets an additional cookie. If a cookie has
  78. * the same name and path it is replaced.
  79. * @param string $name Cookie key.
  80. * @param string $value Value of cookie.
  81. * @param string $host Host upon which the cookie is valid.
  82. * @param string $path Cookie path if not host wide.
  83. * @param string $expiry Expiry date.
  84. * @access public
  85. */
  86. function setCookie($name, $value, $host = false, $path = '/', $expiry = false) {
  87. $this->_cookie_jar->setCookie($name, $value, $host, $path, $expiry);
  88. }
  89. /**
  90. * Reads the most specific cookie value from the
  91. * browser cookies.
  92. * @param string $host Host to search.
  93. * @param string $path Applicable path.
  94. * @param string $name Name of cookie to read.
  95. * @return string False if not present, else the
  96. * value as a string.
  97. * @access public
  98. */
  99. function getCookieValue($host, $path, $name) {
  100. return $this->_cookie_jar->getCookieValue($host, $path, $name);
  101. }
  102. /**
  103. * Reads the current cookies within the base URL.
  104. * @param string $name Key of cookie to find.
  105. * @param SimpleUrl $base Base URL to search from.
  106. * @return string/boolean Null if there is no base URL, false
  107. * if the cookie is not set.
  108. * @access public
  109. */
  110. function getBaseCookieValue($name, $base) {
  111. if (! $base) {
  112. return null;
  113. }
  114. return $this->getCookieValue($base->getHost(), $base->getPath(), $name);
  115. }
  116. /**
  117. * Switches off cookie sending and recieving.
  118. * @access public
  119. */
  120. function ignoreCookies() {
  121. $this->_cookies_enabled = false;
  122. }
  123. /**
  124. * Switches back on the cookie sending and recieving.
  125. * @access public
  126. */
  127. function useCookies() {
  128. $this->_cookies_enabled = true;
  129. }
  130. /**
  131. * Sets the socket timeout for opening a connection.
  132. * @param integer $timeout Maximum time in seconds.
  133. * @access public
  134. */
  135. function setConnectionTimeout($timeout) {
  136. $this->_connection_timeout = $timeout;
  137. }
  138. /**
  139. * Sets the maximum number of redirects before
  140. * a page will be loaded anyway.
  141. * @param integer $max Most hops allowed.
  142. * @access public
  143. */
  144. function setMaximumRedirects($max) {
  145. $this->_max_redirects = $max;
  146. }
  147. /**
  148. * Sets proxy to use on all requests for when
  149. * testing from behind a firewall. Set URL
  150. * to false to disable.
  151. * @param string $proxy Proxy URL.
  152. * @param string $username Proxy username for authentication.
  153. * @param string $password Proxy password for authentication.
  154. * @access public
  155. */
  156. function useProxy($proxy, $username, $password) {
  157. if (! $proxy) {
  158. $this->_proxy = false;
  159. return;
  160. }
  161. if ((strncmp($proxy, 'http://', 7) != 0) && (strncmp($proxy, 'https://', 8) != 0)) {
  162. $proxy = 'http://'. $proxy;
  163. }
  164. $this->_proxy = &new SimpleUrl($proxy);
  165. $this->_proxy_username = $username;
  166. $this->_proxy_password = $password;
  167. }
  168. /**
  169. * Test to see if the redirect limit is passed.
  170. * @param integer $redirects Count so far.
  171. * @return boolean True if over.
  172. * @access private
  173. */
  174. function _isTooManyRedirects($redirects) {
  175. return ($redirects > $this->_max_redirects);
  176. }
  177. /**
  178. * Sets the identity for the current realm.
  179. * @param string $host Host to which realm applies.
  180. * @param string $realm Full name of realm.
  181. * @param string $username Username for realm.
  182. * @param string $password Password for realm.
  183. * @access public
  184. */
  185. function setIdentity($host, $realm, $username, $password) {
  186. $this->_authenticator->setIdentityForRealm($host, $realm, $username, $password);
  187. }
  188. /**
  189. * Fetches a URL as a response object. Will keep trying if redirected.
  190. * It will also collect authentication realm information.
  191. * @param string/SimpleUrl $url Target to fetch.
  192. * @param SimpleEncoding $encoding Additional parameters for request.
  193. * @return SimpleHttpResponse Hopefully the target page.
  194. * @access public
  195. */
  196. function &fetchResponse($url, $encoding) {
  197. if ($encoding->getMethod() != 'POST') {
  198. $url->addRequestParameters($encoding);
  199. $encoding->clear();
  200. }
  201. $response = &$this->_fetchWhileRedirected($url, $encoding);
  202. if ($headers = $response->getHeaders()) {
  203. if ($headers->isChallenge()) {
  204. $this->_authenticator->addRealm(
  205. $url,
  206. $headers->getAuthentication(),
  207. $headers->getRealm());
  208. }
  209. }
  210. return $response;
  211. }
  212. /**
  213. * Fetches the page until no longer redirected or
  214. * until the redirect limit runs out.
  215. * @param SimpleUrl $url Target to fetch.
  216. * @param SimpelFormEncoding $encoding Additional parameters for request.
  217. * @return SimpleHttpResponse Hopefully the target page.
  218. * @access private
  219. */
  220. function &_fetchWhileRedirected($url, $encoding) {
  221. $redirects = 0;
  222. do {
  223. $response = &$this->_fetch($url, $encoding);
  224. if ($response->isError()) {
  225. return $response;
  226. }
  227. $headers = $response->getHeaders();
  228. $location = new SimpleUrl($headers->getLocation());
  229. $url = $location->makeAbsolute($url);
  230. if ($this->_cookies_enabled) {
  231. $headers->writeCookiesToJar($this->_cookie_jar, $url);
  232. }
  233. if (! $headers->isRedirect()) {
  234. break;
  235. }
  236. $encoding = new SimpleGetEncoding();
  237. } while (! $this->_isTooManyRedirects(++$redirects));
  238. return $response;
  239. }
  240. /**
  241. * Actually make the web request.
  242. * @param SimpleUrl $url Target to fetch.
  243. * @param SimpleFormEncoding $encoding Additional parameters for request.
  244. * @return SimpleHttpResponse Headers and hopefully content.
  245. * @access protected
  246. */
  247. function &_fetch($url, $encoding) {
  248. $request = &$this->_createRequest($url, $encoding);
  249. $response = &$request->fetch($this->_connection_timeout);
  250. return $response;
  251. }
  252. /**
  253. * Creates a full page request.
  254. * @param SimpleUrl $url Target to fetch as url object.
  255. * @param SimpleFormEncoding $encoding POST/GET parameters.
  256. * @return SimpleHttpRequest New request.
  257. * @access private
  258. */
  259. function &_createRequest($url, $encoding) {
  260. $request = &$this->_createHttpRequest($url, $encoding);
  261. $this->_addAdditionalHeaders($request);
  262. if ($this->_cookies_enabled) {
  263. $request->readCookiesFromJar($this->_cookie_jar, $url);
  264. }
  265. $this->_authenticator->addHeaders($request, $url);
  266. return $request;
  267. }
  268. /**
  269. * Builds the appropriate HTTP request object.
  270. * @param SimpleUrl $url Target to fetch as url object.
  271. * @param SimpleFormEncoding $parameters POST/GET parameters.
  272. * @return SimpleHttpRequest New request object.
  273. * @access protected
  274. */
  275. function &_createHttpRequest($url, $encoding) {
  276. $request = &new SimpleHttpRequest($this->_createRoute($url), $encoding);
  277. return $request;
  278. }
  279. /**
  280. * Sets up either a direct route or via a proxy.
  281. * @param SimpleUrl $url Target to fetch as url object.
  282. * @return SimpleRoute Route to take to fetch URL.
  283. * @access protected
  284. */
  285. function &_createRoute($url) {
  286. if ($this->_proxy) {
  287. $route = &new SimpleProxyRoute(
  288. $url,
  289. $this->_proxy,
  290. $this->_proxy_username,
  291. $this->_proxy_password);
  292. } else {
  293. $route = &new SimpleRoute($url);
  294. }
  295. return $route;
  296. }
  297. /**
  298. * Adds additional manual headers.
  299. * @param SimpleHttpRequest $request Outgoing request.
  300. * @access private
  301. */
  302. function _addAdditionalHeaders(&$request) {
  303. foreach ($this->_additional_headers as $header) {
  304. $request->addHeaderLine($header);
  305. }
  306. }
  307. }
  308. ?>