PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/demo/scalr_newui/app/src/LibWebta/library/NET/HTTP/class.HTTPClient.php

https://github.com/kennethjiang/Wolke
PHP | 532 lines | 179 code | 81 blank | 272 comment | 24 complexity | 20fd66150348f677beb9d15648b7b57a MD5 | raw file
  1. <?
  2. /**
  3. * This file is a part of LibWebta, PHP class library.
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to version 2 of the GPL license,
  8. * that is bundled with this package in the file license.txt and is
  9. * available through the world-wide-web at the following url:
  10. * http://www.gnu.org/copyleft/gpl.html
  11. *
  12. * @category LibWebta
  13. * @package NET
  14. * @subpackage HTTP
  15. * @copyright Copyright (c) 2003-2007 Webta Inc, http://www.gnu.org/licenses/gpl.html
  16. * @license http://www.gnu.org/licenses/gpl.html
  17. */
  18. /**
  19. * @name HTTPClient
  20. * @category LibWebta
  21. * @package NET
  22. * @subpackage HTTP
  23. * @todo Enable in HTTP Client socket connections if curl functions are disabled
  24. * @author Sergey Koksharov <http://webta.net/company.html>
  25. * @author Igor Savchenko <http://webta.net/company.html>
  26. */
  27. class HTTPClient extends Core
  28. {
  29. /**
  30. * Default user agent for HTTP Client
  31. *
  32. */
  33. const USER_AGENT = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4';
  34. /**
  35. * Default cookie file (when cookies enabled)
  36. *
  37. */
  38. const COOKIE_FILE = '/tmp/cookies.txt';
  39. /**
  40. * The number of seconds to wait whilst trying to connect. Use 0 to wait
  41. * indefinitely
  42. *
  43. */
  44. const CONNECT_TIMEOUT = 60;
  45. /**
  46. * The maximum number of seconds to allow CURL functions to execute.
  47. *
  48. */
  49. const CURL_TIMEOUT = 60;
  50. /**
  51. * Max redirects
  52. *
  53. */
  54. const MAX_REDIRECTS = 3;
  55. /**
  56. * Default debug file (when debugging enabled)
  57. *
  58. */
  59. const DEBUG_FILE = "/tmp/HTTPClient_curl_verbose.log";
  60. /**
  61. * CURL handler
  62. *
  63. * @var resource
  64. * @access protected
  65. */
  66. protected $Curl;
  67. /**
  68. * Parameters query string
  69. *
  70. * @var array
  71. * @access private
  72. */
  73. private $Params;
  74. /**
  75. * User agent for curl query
  76. *
  77. * @var string
  78. * @access private
  79. */
  80. private $UserAgent;
  81. /**
  82. * Result of HTTP query
  83. *
  84. * @var string
  85. * @access public
  86. */
  87. public $Result;
  88. /**
  89. * HTTP headers for request
  90. *
  91. * @var array
  92. * @access private
  93. */
  94. private $Headers;
  95. /**
  96. * Connection status
  97. *
  98. * @var bool
  99. * @access public
  100. */
  101. public $Connected;
  102. /**
  103. * Debug mode (true if enabled)
  104. *
  105. * @var bool
  106. * @access private
  107. */
  108. private $Debug;
  109. /**
  110. * Path to file for debugging
  111. *
  112. * @var string
  113. * @access private
  114. */
  115. private $DebugFile;
  116. /**
  117. * File handler for debug file
  118. *
  119. * @var resource
  120. * @access private
  121. */
  122. private $DebugHandler;
  123. /**
  124. * Timeout for curl connection
  125. *
  126. * @var integer
  127. * @access private
  128. */
  129. private $ConnectTimeout;
  130. /**
  131. * Timeout for curl function execute
  132. *
  133. * @vars integer
  134. * @access private
  135. */
  136. private $CurlTimeout;
  137. /**
  138. * Usage of redirects (true when enabled)
  139. *
  140. * @var bool
  141. * @access private
  142. */
  143. private $UseRedirects;
  144. /**
  145. * Max allowed redirects
  146. *
  147. * @var integer
  148. * @access private
  149. */
  150. private $MaxRedirects;
  151. /**
  152. * Cookies for http query header
  153. *
  154. * @var string
  155. * @access private
  156. */
  157. private $Cookies;
  158. /**
  159. * Receive headers from server or not
  160. *
  161. * @var bool
  162. * @access private
  163. */
  164. private $NeedHeaders;
  165. /**
  166. * Use cookies while redirecting
  167. *
  168. * @var bool
  169. * @access private
  170. */
  171. private $UseCookies;
  172. /**
  173. * Path to cookie file
  174. *
  175. * @var string
  176. * @access private
  177. */
  178. private $CookiePath;
  179. /**
  180. * Ignore curl errors while execute query
  181. *
  182. * @var bool
  183. * @access private
  184. */
  185. private $IgnoreErrors;
  186. /**
  187. * User authentication data in format [login]:[pass]
  188. *
  189. * @var string
  190. * @access private
  191. */
  192. private $Credentials;
  193. /**
  194. * Class constructor. HTTP client uses CURL functions for connection.
  195. *
  196. * @param resource $curl Opened curl handler
  197. */
  198. function __construct($curl = null)
  199. {
  200. parent::__construct();
  201. $this->SetUserAgent(self::USER_AGENT);
  202. $this->SetTimeouts(self::CONNECT_TIMEOUT, self::CURL_TIMEOUT);
  203. $this->UseRedirects(false, self::MAX_REDIRECTS);
  204. $this->Curl = (is_resource($curl)) ? $curl : curl_init();
  205. if ($this->UserAgent)
  206. curl_setopt($this->Curl, CURLOPT_USERAGENT, $this->UserAgent);
  207. }
  208. /**
  209. * Connecting to server by given URL with given parameters and fetching
  210. * content
  211. *
  212. * @param string $url URL to the server
  213. * @param array $params Params to send in the query
  214. * @param bool $post If variable value is true then query method will
  215. * be POST. Otherwise uses GET method
  216. * @return string Result content if success
  217. * @access public
  218. */
  219. public function Fetch($url, $params = array(), $post = false)
  220. {
  221. if (!$this->Curl)
  222. return false;
  223. $chunks = parse_url($url);
  224. if ($this->Params && is_array($params))
  225. $this->Params = array_merge($this->Params, $params);
  226. else
  227. $this->SetParams($params);
  228. if ($chunks['scheme'] == 'https')
  229. {
  230. curl_setopt($this->Curl, CURLOPT_SSL_VERIFYPEER, false);
  231. curl_setopt($this->Curl, CURLOPT_SSL_VERIFYHOST,1);
  232. }
  233. curl_setopt($this->Curl, CURLOPT_RETURNTRANSFER,1);
  234. curl_setopt($this->Curl, CURLOPT_URL, $url);
  235. if ($this->Headers)
  236. curl_setopt($this->Curl, CURLOPT_HTTPHEADER, $this->Headers);
  237. if ($this->Credentials)
  238. curl_setopt($this->Curl, CURLOPT_USERPWD, $this->Credentials);
  239. if ($post)
  240. {
  241. curl_setopt($this->Curl, CURLOPT_POST,1);
  242. if (count($this->Params) > 0)
  243. curl_setopt($this->Curl, CURLOPT_POSTFIELDS, http_build_query($this->Params));
  244. }
  245. if ($this->Cookies)
  246. curl_setopt($this->Curl, CURLOPT_COOKIE, $this->Cookies);
  247. if ($this->UseCookies)
  248. curl_setopt($this->Curl, CURLOPT_COOKIEFILE, $this->CookiePath);
  249. if ($this->ConnectTimeout)
  250. curl_setopt($this->Curl, CURLOPT_CONNECTTIMEOUT, $this->ConnectTimeout);
  251. if ($this->CurlTimeout)
  252. curl_setopt($this->Curl, CURLOPT_TIMEOUT, $this->CurlTimeout);
  253. if ($this->UseRedirects)
  254. {
  255. curl_setopt($this->Curl, CURLOPT_FOLLOWLOCATION, 1);
  256. curl_setopt($this->Curl, CURLOPT_MAXREDIRS, $this->MaxRedirects);
  257. }
  258. else
  259. curl_setopt($this->Curl, CURLOPT_FOLLOWLOCATION, 0);
  260. if ($this->NeedHeaders)
  261. curl_setopt($this->Curl, CURLOPT_HEADER, 1);
  262. if ($this->Debug)
  263. {
  264. curl_setopt($this->Curl, CURLOPT_VERBOSE, 1);
  265. curl_setopt($this->Curl, CURLOPT_STDERR, $this->DebugHandler);
  266. }
  267. $this->Result = trim(curl_exec($this->Curl));
  268. if (curl_error($this->Curl) && !$this->IgnoreErrors)
  269. {
  270. Core::RaiseWarning(curl_error($this->Curl));
  271. $this->Result = '';
  272. return false;
  273. }
  274. return $this->Result;
  275. }
  276. /**
  277. * Set user agent for query
  278. *
  279. * @param string $agent Name of the user agent
  280. * @access public
  281. */
  282. public function SetUserAgent($agent)
  283. {
  284. $this->UserAgent = $agent;
  285. }
  286. /**
  287. * Set query parameters
  288. *
  289. * @param array $params Array of query parameters
  290. * @access public
  291. */
  292. public function SetParams($params)
  293. {
  294. if (is_array($params))
  295. $this->Params = $params;
  296. }
  297. /**
  298. * Set HTTP headers
  299. *
  300. * @param array $headers Array of HTTP headers
  301. * @access public
  302. */
  303. public function SetHeaders($headers)
  304. {
  305. if (is_array($headers))
  306. $this->Headers = $headers;
  307. }
  308. /**
  309. * Set max timeout for connect and cURL function execution
  310. *
  311. * @param integer $timeout Timemout for curl connection
  312. * @param integer $func_timeout Timeout for curl function execution
  313. * @access public
  314. */
  315. public function SetTimeouts($timeout, $func_timeout)
  316. {
  317. $this->ConnectTimeout = $timeout;
  318. $this->CurlTimeout = $func_timeout;
  319. }
  320. /**
  321. * Set credentials for user autentication
  322. *
  323. * @param string $login User name/login
  324. * @param string $pass User password
  325. * @access public
  326. */
  327. public function SetCredentials($login, $pass)
  328. {
  329. $this->Credentials = "{$login}:{$pass}";
  330. }
  331. /**
  332. * Usage redirects while query
  333. *
  334. * @param bool $use True if needed to use redirects, otherwise - false.
  335. * By default this variable value is true.
  336. * @param integer $max_redir Count of max allowed redirects. If not
  337. * determined then used MAX_REDIRECTS constant
  338. * @uses MAX_REDIRECTS Class constant for max allowed redirects
  339. * @access public
  340. */
  341. public function UseRedirects($use = true, $max_redir = null)
  342. {
  343. $this->MaxRedirects = (is_numeric($max_redir)) ? $max_redir : self::MAX_REDIRECTS;
  344. $this->UseRedirects = (bool)$use;
  345. }
  346. /**
  347. * Set cookies function
  348. *
  349. * @param string|array $cookies Cookies for query. If variable type is
  350. * array then used http_build_query function
  351. * @access public
  352. */
  353. public function SetCookies($cookies)
  354. {
  355. $this->Cookies = is_array($cookies) ? http_build_query($cookies) : $cookies;
  356. }
  357. /**
  358. * Receiving headers in result content
  359. *
  360. * @param bool $receive Boolean variable for indication requirement of
  361. * headers in result fetching string
  362. * @access public
  363. */
  364. public function ReceiveHeaders($receive = true)
  365. {
  366. $this->NeedHeaders = $receive;
  367. }
  368. /**
  369. * Get cookies from result. Parse result string and return all cookies
  370. * in headers.
  371. *
  372. * @return array Array of cookies in result headers
  373. * @access public
  374. */
  375. public function GetCookies()
  376. {
  377. if (!$this->Result)
  378. return '';
  379. preg_match("/^Set\-Cookie: (.*)\n/m", $this->Result, $matches);
  380. return $matches[1];
  381. }
  382. /**
  383. * Usage of cookies
  384. *
  385. * @param bool $use Use cookies while sending query
  386. * @param string $path Path to cookie file. If not defined then path
  387. * will be constant COOKIE_FILE value
  388. * @uses COOKIE_FILE Default cookie file path
  389. * @access public
  390. */
  391. public function UseCookies($use = true, $path = null)
  392. {
  393. $this->UseCookies = $use;
  394. $this->CookiePath = $path ? $path : self::COOKIE_FILE;
  395. }
  396. /**
  397. * Switch debug mode
  398. *
  399. * @param bool $turnon Enable debug mode
  400. * @param string $filename Name of the debug file. If not defined then
  401. * used DEBUG_FILE constant
  402. * @uses DEBUG_FILE Default debug filename
  403. * @access public
  404. */
  405. public function Debug($turnon = false, $filename = false)
  406. {
  407. $this->Debug = $turnon;
  408. $this->DebugFile = $filename ? $filename : self::DEBUG_FILE;
  409. @touch($this->DebugFile);
  410. @chmod($this->DebugFile, 0777);
  411. if ($turnon)
  412. $this->DebugHandler = fopen($this->DebugFile, "a+");
  413. else
  414. {
  415. @fclose($this->DebugHandler);
  416. $this->DebugHandler = null;
  417. }
  418. }
  419. /**
  420. * Ignoring errors in result content
  421. *
  422. * @param bool $turnon Ignoring errors flag. True by default
  423. * @access public
  424. */
  425. public function IgnoreErrors($turnon = true)
  426. {
  427. $this->IgnoreErrors = $turnon;
  428. }
  429. /**
  430. * Class desctructor. Clearing curl handler and unlink cookie file
  431. *
  432. */
  433. function __destruct()
  434. {
  435. $this->Curl = null;
  436. if ($this->CookiePath) @unlink($this->CookiePath);
  437. $this->Debug(false);
  438. }
  439. public static function HTTPDigestParse($txt)
  440. {
  441. // protect against missing data
  442. $needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);
  443. $data = array();
  444. preg_match_all('/(\w+)=[\'"]*(.*?)[\'"]*,/si', $txt, $matches, PREG_SET_ORDER);
  445. foreach ($matches as $m) {
  446. $data[$m[1]] = $m[2];
  447. unset($needed_parts[$m[1]]);
  448. }
  449. return $needed_parts ? false : $data;
  450. }
  451. }