api.class.php 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  1. <?php
  2. /**
  3. * The api library of zentaopms.
  4. *
  5. * @copyright Copyright 2009-2015 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  6. * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Chunsheng Wang <chunsheng@cnezsoft.com>
  8. * @package API
  9. * @version $Id: api.class.php 4132 2013-01-18 02:30:05Z wwccss $
  10. * @link http://www.zentao.net
  11. */
  12. class ztclient
  13. {
  14. public $configFile;
  15. public $zentao;
  16. public $agent;
  17. public $session;
  18. /**
  19. * The construce function.
  20. *
  21. * @param string $zentaoRoot the zentao root url
  22. * @param string $account the login account
  23. * @param string $password the passwod
  24. * @access public
  25. * @return void
  26. */
  27. public function __construct($zentaoRoot = '', $account = '', $password = '')
  28. {
  29. $this->agent = new snoopy();
  30. /* Assign to $this->zentao. */
  31. $this->zentao->root = rtrim($zentaoRoot, '/') . '/';
  32. $this->zentao->account = $account;
  33. $this->zentao->password = md5($password);
  34. /* Load the remote settings. */
  35. $config = $this->getRemoteConfig();
  36. foreach($config as $key => $value) $this->zentao->$key = $value;
  37. $this->startSession();
  38. $this->login();
  39. }
  40. /**
  41. * Get the settings through getconfig api from remote.
  42. *
  43. * @access private
  44. * @return object
  45. */
  46. private function getRemoteConfig()
  47. {
  48. $url = $this->zentao->root . 'index.php?mode=getconfig';
  49. $this->agent->fetchText($url);
  50. $config = json_decode($this->agent->results);
  51. return $config;
  52. }
  53. /**
  54. * Set the session api.
  55. *
  56. * @access private
  57. * @return string the session api url.
  58. */
  59. private function setSessionAPI()
  60. {
  61. return $this->setControlAPI('api', 'getsessionid');
  62. }
  63. /**
  64. * Set the login api.
  65. *
  66. * @access private
  67. * @return string the login api url.
  68. */
  69. private function setLoginAPI()
  70. {
  71. return $this->setControlAPI('user', 'login');
  72. }
  73. /**
  74. * Set the method api.
  75. *
  76. * @param string $module the module name
  77. * @param string $method the methhod name
  78. * @param string $vars the vars to passwd
  79. * @access private
  80. * @return string the control api string.
  81. */
  82. private function setControlAPI($module, $method, $vars = '')
  83. {
  84. if($this->zentao->requestType == 'GET')
  85. {
  86. $controlAPI = $this->appendSession($this->zentao->root);
  87. $controlAPI .= "&{$this->zentao->moduleVar}=$module&{$this->zentao->methodVar}=$method&{$this->zentao->viewVar}=json$vars";
  88. }
  89. elseif($this->zentao->requestType == 'PATH_INFO')
  90. {
  91. $controlAPI = $this->zentao->root . $module . $this->zentao->requestFix . $method . $this->zentao->requestFix;
  92. if($vars)
  93. {
  94. parse_str($vars, $vars);
  95. foreach($vars as $var) $controlAPI .= $var . $this->zentao->requestFix;
  96. }
  97. $controlAPI = rtrim($controlAPI, $this->zentao->requestFix) . '.json';
  98. $controlAPI = $this->appendSession($controlAPI);
  99. }
  100. return $controlAPI;
  101. }
  102. /**
  103. * Set the method api.
  104. *
  105. * @param string $module the module name
  106. * @param string $method the methhod name
  107. * @param string $vars the vars to passed
  108. * @access private
  109. * @return string the model api string.
  110. */
  111. private function setModelAPI($module, $method, $vars = '')
  112. {
  113. $vars = str_replace('&', ',', $vars);
  114. if($this->zentao->requestType == 'GET')
  115. {
  116. $modelAPI = $this->appendSession($this->zentao->root);
  117. $modelAPI .= "&{$this->zentao->moduleVar}=api&{$this->zentao->methodVar}=getModel&{$this->zentao->viewVar}=json";
  118. $modelAPI .="&module=$module&method=$method&params=$vars";
  119. }
  120. elseif($this->zentao->requestType == 'PATH_INFO')
  121. {
  122. $modelAPI = $this->zentao->root . 'api' . $this->zentao->requestFix . 'getmodel' . $this->zentao->requestFix;
  123. $modelAPI .= $module . $this->zentao->requestFix . $method . $this->zentao->requestFix;
  124. $modelAPI .= $vars;
  125. $modelAPI = rtrim($modelAPI, $this->zentao->requestFix) . '.json';
  126. $modelAPI = $this->appendSession($modelAPI);
  127. }
  128. return $modelAPI;
  129. }
  130. /**
  131. * Start session
  132. *
  133. * @access public
  134. * @return void
  135. */
  136. public function startSession()
  137. {
  138. $this->session = null;
  139. $this->session = $this->httpGet($this->setSessionAPI());
  140. }
  141. /**
  142. * Login.
  143. *
  144. * @access public
  145. * @return void
  146. */
  147. public function login()
  148. {
  149. $loginAPI = $this->setLoginAPI();
  150. $authHash = md5($this->zentao->password . $this->session->rand);
  151. $loginAPI .= "&account={$this->zentao->account}&password=$authHash";
  152. $this->httpGet($loginAPI);
  153. }
  154. /**
  155. * Fetch one method of a module's control.
  156. *
  157. * @param string $module the module name
  158. * @param string $method the methhod name
  159. * @param string $vars the vars to passwd
  160. * @access public
  161. * @return void
  162. */
  163. public function fetch($module, $method = 'index', $vars = '')
  164. {
  165. return $this->httpGet($this->setControlAPI($module, $method, $vars));
  166. }
  167. /**
  168. * Fetch one method of a module's model.
  169. *
  170. * @param string $module the module name
  171. * @param string $method the methhod name
  172. * @param string $vars the vars to passwd
  173. * @access public
  174. * @return void
  175. */
  176. public function fetchModel($module, $method, $vars = '')
  177. {
  178. return $this->httpGet($this->setModelAPI($module, $method, $vars));
  179. }
  180. /**
  181. * Get a api and check it.
  182. *
  183. * @param string $url
  184. * @access private
  185. * @return bool
  186. */
  187. private function httpGet($url)
  188. {
  189. $this->agent->fetch($url);
  190. $result = json_decode($this->agent->results);
  191. if($result->status != 'success') return false;
  192. if(isset($result->data) and md5($result->data) != $result->md5) return false;
  193. if(isset($result->data)) return json_decode($result->data);
  194. return true;
  195. }
  196. /**
  197. * Fetch one method of a module's control.
  198. *
  199. * @param string $module the module name
  200. * @param string $method the methhod name
  201. * @param array $vars the vars to passwd
  202. * @access public
  203. * @return void
  204. */
  205. public function post($module, $method = 'index', $vars = array())
  206. {
  207. return $this->httpPost($this->setControlAPI($module, $method), $vars);
  208. }
  209. /**
  210. * Post.
  211. *
  212. * @param string $url
  213. * @access private
  214. * @return bool
  215. */
  216. private function httpPost($url, $vars)
  217. {
  218. $this->agent->submit($url, $vars);
  219. $result = json_decode($this->agent->results);
  220. if($result->status != 'success') return false;
  221. if(isset($result->data) and md5($result->data) != $result->md5) return false;
  222. if(isset($result->data)) return json_decode($result->data);
  223. return true;
  224. }
  225. /**
  226. * Append session param to the url.
  227. *
  228. * @param string $url
  229. * @access private
  230. * @return string
  231. */
  232. private function appendSession($url)
  233. {
  234. if(strrpos($url, '&') === false) $url .= '?';
  235. if($this->session) $url .= '&' . $this->session->sessionName . '=' . $this->session->sessionID;
  236. return str_replace('?&', '?', $url);
  237. }
  238. }
  239. ?>
  240. <?php
  241. /*************************************************
  242. Snoopy - the PHP net client
  243. Author: Monte Ohrt <monte@ispi.net>
  244. Copyright (c): 1999-2008 New Digital Group, all rights reserved
  245. Version: 1.2.4
  246. * This library is free software; you can redistribute it and/or
  247. * modify it under the terms of the GNU Lesser General Public
  248. * License as published by the Free Software Foundation; either
  249. * version 2.1 of the License, or (at your option) any later version.
  250. *
  251. * This library is distributed in the hope that it will be useful,
  252. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  253. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  254. * Lesser General Public License for more details.
  255. *
  256. * You should have received a copy of the GNU Lesser General Public
  257. * License along with this library; if not, write to the Free Software
  258. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  259. You may contact the author of Snoopy by e-mail at:
  260. monte@ohrt.com
  261. The latest version of Snoopy can be obtained from:
  262. http://snoopy.sourceforge.net/
  263. *************************************************/
  264. class Snoopy
  265. {
  266. /**** Public variables ****/
  267. /* user definable vars */
  268. var $host = "www.php.net"; // host name we are connecting to
  269. var $port = 80; // port we are connecting to
  270. var $proxy_host = ""; // proxy host to use
  271. var $proxy_port = ""; // proxy port to use
  272. var $proxy_user = ""; // proxy user to use
  273. var $proxy_pass = ""; // proxy password to use
  274. var $agent = "Snoopy v1.2.4"; // agent we masquerade as
  275. var $referer = ""; // referer info to pass
  276. var $cookies = array(); // array of cookies to pass
  277. // $cookies["username"]="joe";
  278. var $rawheaders = array(); // array of raw headers to send
  279. // $rawheaders["Content-type"]="text/html";
  280. var $maxredirs = 5; // http redirection depth maximum. 0 = disallow
  281. var $lastredirectaddr = ""; // contains address of last redirected address
  282. var $offsiteok = true; // allows redirection off-site
  283. var $maxframes = 0; // frame content depth maximum. 0 = disallow
  284. var $expandlinks = true; // expand links to fully qualified URLs.
  285. // this only applies to fetchlinks()
  286. // submitlinks(), and submittext()
  287. var $passcookies = true; // pass set cookies back through redirects
  288. // NOTE: this currently does not respect
  289. // dates, domains or paths.
  290. var $user = ""; // user for http authentication
  291. var $pass = ""; // password for http authentication
  292. // http accept types
  293. var $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
  294. var $results = ""; // where the content is put
  295. var $error = ""; // error messages sent here
  296. var $response_code = ""; // response code returned from server
  297. var $headers = array(); // headers returned from server sent here
  298. var $maxlength = 500000; // max return data length (body)
  299. var $read_timeout = 0; // timeout on read operations, in seconds
  300. // supported only since PHP 4 Beta 4
  301. // set to 0 to disallow timeouts
  302. var $timed_out = false; // if a read operation timed out
  303. var $status = 0; // http request status
  304. var $temp_dir = "/tmp"; // temporary directory that the webserver
  305. // has permission to write to.
  306. // under Windows, this should be C:\temp
  307. var $curl_path = "/usr/local/bin/curl";
  308. // Snoopy will use cURL for fetching
  309. // SSL content if a full system path to
  310. // the cURL binary is supplied here.
  311. // set to false if you do not have
  312. // cURL installed. See http://curl.haxx.se
  313. // for details on installing cURL.
  314. // Snoopy does *not* use the cURL
  315. // library functions built into php,
  316. // as these functions are not stable
  317. // as of this Snoopy release.
  318. /**** Private variables ****/
  319. var $_maxlinelen = 4096; // max line length (headers)
  320. var $_httpmethod = "GET"; // default http request method
  321. var $_httpversion = "HTTP/1.0"; // default http request version
  322. var $_submit_method = "POST"; // default submit method
  323. var $_submit_type = "application/x-www-form-urlencoded"; // default submit type
  324. var $_mime_boundary = ""; // MIME boundary for multipart/form-data submit type
  325. var $_redirectaddr = false; // will be set if page fetched is a redirect
  326. var $_redirectdepth = 0; // increments on an http redirect
  327. var $_frameurls = array(); // frame src urls
  328. var $_framedepth = 0; // increments on frame depth
  329. var $_isproxy = false; // set if using a proxy server
  330. var $_fp_timeout = 30; // timeout for socket connection
  331. /*======================================================================*\
  332. Function: fetch
  333. Purpose: fetch the contents of a web page
  334. (and possibly other protocols in the
  335. future like ftp, nntp, gopher, etc.)
  336. Input: $URI the location of the page to fetch
  337. Output: $this->results the output text from the fetch
  338. \*======================================================================*/
  339. function fetch($URI)
  340. {
  341. //preg_match("|^([^:]+)://([^:/]+)(:[\d]+)*(.*)|",$URI,$URI_PARTS);
  342. $URI_PARTS = parse_url($URI);
  343. if (!empty($URI_PARTS["user"]))
  344. $this->user = $URI_PARTS["user"];
  345. if (!empty($URI_PARTS["pass"]))
  346. $this->pass = $URI_PARTS["pass"];
  347. if (empty($URI_PARTS["query"]))
  348. $URI_PARTS["query"] = '';
  349. if (empty($URI_PARTS["path"]))
  350. $URI_PARTS["path"] = '';
  351. switch(strtolower($URI_PARTS["scheme"]))
  352. {
  353. case "http":
  354. $this->host = $URI_PARTS["host"];
  355. if(!empty($URI_PARTS["port"]))
  356. $this->port = $URI_PARTS["port"];
  357. if($this->_connect($fp))
  358. {
  359. if($this->_isproxy)
  360. {
  361. // using proxy, send entire URI
  362. $this->_httprequest($URI,$fp,$URI,$this->_httpmethod);
  363. }
  364. else
  365. {
  366. $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : "");
  367. // no proxy, send only the path
  368. $this->_httprequest($path, $fp, $URI, $this->_httpmethod);
  369. }
  370. $this->_disconnect($fp);
  371. if($this->_redirectaddr)
  372. {
  373. /* url was redirected, check if we've hit the max depth */
  374. if($this->maxredirs > $this->_redirectdepth)
  375. {
  376. // only follow redirect if it's on this site, or offsiteok is true
  377. if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
  378. {
  379. /* follow the redirect */
  380. $this->_redirectdepth++;
  381. $this->lastredirectaddr=$this->_redirectaddr;
  382. $this->fetch($this->_redirectaddr);
  383. }
  384. }
  385. }
  386. if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
  387. {
  388. $frameurls = $this->_frameurls;
  389. $this->_frameurls = array();
  390. foreach($frameurls as $frameurl)
  391. {
  392. if($this->_framedepth >= $this->maxframes) break;
  393. $this->fetch($frameurl);
  394. $this->_framedepth++;
  395. }
  396. }
  397. }
  398. else
  399. {
  400. return false;
  401. }
  402. return true;
  403. break;
  404. case "https":
  405. if(!$this->curl_path)
  406. return false;
  407. if(function_exists("is_executable"))
  408. if (!is_executable($this->curl_path))
  409. return false;
  410. $this->host = $URI_PARTS["host"];
  411. if(!empty($URI_PARTS["port"]))
  412. $this->port = $URI_PARTS["port"];
  413. if($this->_isproxy)
  414. {
  415. // using proxy, send entire URI
  416. $this->_httpsrequest($URI,$URI,$this->_httpmethod);
  417. }
  418. else
  419. {
  420. $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : "");
  421. // no proxy, send only the path
  422. $this->_httpsrequest($path, $URI, $this->_httpmethod);
  423. }
  424. if($this->_redirectaddr)
  425. {
  426. /* url was redirected, check if we've hit the max depth */
  427. if($this->maxredirs > $this->_redirectdepth)
  428. {
  429. // only follow redirect if it's on this site, or offsiteok is true
  430. if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
  431. {
  432. /* follow the redirect */
  433. $this->_redirectdepth++;
  434. $this->lastredirectaddr=$this->_redirectaddr;
  435. $this->fetch($this->_redirectaddr);
  436. }
  437. }
  438. }
  439. if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
  440. {
  441. $frameurls = $this->_frameurls;
  442. $this->_frameurls = array();
  443. foreach($frameurls as $frameurl)
  444. {
  445. if($this->_framedepth < $this->maxframes)
  446. {
  447. $this->fetch($frameurl);
  448. $this->_framedepth++;
  449. }
  450. else
  451. {
  452. break;
  453. }
  454. }
  455. }
  456. return true;
  457. break;
  458. default:
  459. // not a valid protocol
  460. $this->error = 'Invalid protocol "'.$URI_PARTS["scheme"].'"\n';
  461. return false;
  462. break;
  463. }
  464. return true;
  465. }
  466. /*======================================================================*\
  467. Function: submit
  468. Purpose: submit an http form
  469. Input: $URI the location to post the data
  470. $formvars the formvars to use.
  471. format: $formvars["var"] = "val";
  472. $formfiles an array of files to submit
  473. format: $formfiles["var"] = "/dir/filename.ext";
  474. Output: $this->results the text output from the post
  475. \*======================================================================*/
  476. function submit($URI, $formvars="", $formfiles="")
  477. {
  478. unset($postdata);
  479. $postdata = $this->_prepare_post_body($formvars, $formfiles);
  480. $URI_PARTS = parse_url($URI);
  481. if (!empty($URI_PARTS["user"]))
  482. $this->user = $URI_PARTS["user"];
  483. if (!empty($URI_PARTS["pass"]))
  484. $this->pass = $URI_PARTS["pass"];
  485. if (empty($URI_PARTS["query"]))
  486. $URI_PARTS["query"] = '';
  487. if (empty($URI_PARTS["path"]))
  488. $URI_PARTS["path"] = '';
  489. switch(strtolower($URI_PARTS["scheme"]))
  490. {
  491. case "http":
  492. $this->host = $URI_PARTS["host"];
  493. if(!empty($URI_PARTS["port"]))
  494. $this->port = $URI_PARTS["port"];
  495. if($this->_connect($fp))
  496. {
  497. if($this->_isproxy)
  498. {
  499. // using proxy, send entire URI
  500. $this->_httprequest($URI,$fp,$URI,$this->_submit_method,$this->_submit_type,$postdata);
  501. }
  502. else
  503. {
  504. $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : "");
  505. // no proxy, send only the path
  506. $this->_httprequest($path, $fp, $URI, $this->_submit_method, $this->_submit_type, $postdata);
  507. }
  508. $this->_disconnect($fp);
  509. if($this->_redirectaddr)
  510. {
  511. /* url was redirected, check if we've hit the max depth */
  512. if($this->maxredirs > $this->_redirectdepth)
  513. {
  514. if(!preg_match("|^".$URI_PARTS["scheme"]."://|", $this->_redirectaddr))
  515. $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]);
  516. // only follow redirect if it's on this site, or offsiteok is true
  517. if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
  518. {
  519. /* follow the redirect */
  520. $this->_redirectdepth++;
  521. $this->lastredirectaddr=$this->_redirectaddr;
  522. if( strpos( $this->_redirectaddr, "?" ) > 0 )
  523. $this->fetch($this->_redirectaddr); // the redirect has changed the request method from post to get
  524. else
  525. $this->submit($this->_redirectaddr,$formvars, $formfiles);
  526. }
  527. }
  528. }
  529. if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
  530. {
  531. $frameurls = $this->_frameurls;
  532. $this->_frameurls = array();
  533. foreach($frameurls as $frameurl)
  534. {
  535. if($this->_framedepth < $this->maxframes)
  536. {
  537. $this->fetch($frameurl);
  538. $this->_framedepth++;
  539. }
  540. else
  541. break;
  542. }
  543. }
  544. }
  545. else
  546. {
  547. return false;
  548. }
  549. return true;
  550. break;
  551. case "https":
  552. if(!$this->curl_path)
  553. return false;
  554. if(function_exists("is_executable"))
  555. if (!is_executable($this->curl_path))
  556. return false;
  557. $this->host = $URI_PARTS["host"];
  558. if(!empty($URI_PARTS["port"]))
  559. $this->port = $URI_PARTS["port"];
  560. if($this->_isproxy)
  561. {
  562. // using proxy, send entire URI
  563. $this->_httpsrequest($URI, $URI, $this->_submit_method, $this->_submit_type, $postdata);
  564. }
  565. else
  566. {
  567. $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : "");
  568. // no proxy, send only the path
  569. $this->_httpsrequest($path, $URI, $this->_submit_method, $this->_submit_type, $postdata);
  570. }
  571. if($this->_redirectaddr)
  572. {
  573. /* url was redirected, check if we've hit the max depth */
  574. if($this->maxredirs > $this->_redirectdepth)
  575. {
  576. if(!preg_match("|^".$URI_PARTS["scheme"]."://|", $this->_redirectaddr))
  577. $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]);
  578. // only follow redirect if it's on this site, or offsiteok is true
  579. if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
  580. {
  581. /* follow the redirect */
  582. $this->_redirectdepth++;
  583. $this->lastredirectaddr=$this->_redirectaddr;
  584. if( strpos( $this->_redirectaddr, "?" ) > 0 )
  585. $this->fetch($this->_redirectaddr); // the redirect has changed the request method from post to get
  586. else
  587. $this->submit($this->_redirectaddr,$formvars, $formfiles);
  588. }
  589. }
  590. }
  591. if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
  592. {
  593. $frameurls = $this->_frameurls;
  594. $this->_frameurls = array();
  595. foreach($frameurls as $frameurl)
  596. {
  597. if($this->_framedepth < $this->maxframes)
  598. {
  599. $this->fetch($frameurl);
  600. $this->_framedepth++;
  601. }
  602. else
  603. break;
  604. }
  605. }
  606. return true;
  607. break;
  608. default:
  609. // not a valid protocol
  610. $this->error = 'Invalid protocol "'.$URI_PARTS["scheme"].'"\n';
  611. return false;
  612. break;
  613. }
  614. return true;
  615. }
  616. /*======================================================================*\
  617. Function: fetchlinks
  618. Purpose: fetch the links from a web page
  619. Input: $URI where you are fetching from
  620. Output: $this->results an array of the URLs
  621. \*======================================================================*/
  622. function fetchlinks($URI)
  623. {
  624. if ($this->fetch($URI))
  625. {
  626. if($this->lastredirectaddr)
  627. $URI = $this->lastredirectaddr;
  628. if(is_array($this->results))
  629. {
  630. for($x=0;$x<count($this->results);$x++)
  631. $this->results[$x] = $this->_striplinks($this->results[$x]);
  632. }
  633. else
  634. $this->results = $this->_striplinks($this->results);
  635. if($this->expandlinks)
  636. $this->results = $this->_expandlinks($this->results, $URI);
  637. return true;
  638. }
  639. else
  640. return false;
  641. }
  642. /*======================================================================*\
  643. Function: fetchform
  644. Purpose: fetch the form elements from a web page
  645. Input: $URI where you are fetching from
  646. Output: $this->results the resulting html form
  647. \*======================================================================*/
  648. function fetchform($URI)
  649. {
  650. if ($this->fetch($URI))
  651. {
  652. if(is_array($this->results))
  653. {
  654. for($x=0;$x<count($this->results);$x++)
  655. $this->results[$x] = $this->_stripform($this->results[$x]);
  656. }
  657. else
  658. $this->results = $this->_stripform($this->results);
  659. return true;
  660. }
  661. else
  662. return false;
  663. }
  664. /*======================================================================*\
  665. Function: fetchtext
  666. Purpose: fetch the text from a web page, stripping the links
  667. Input: $URI where you are fetching from
  668. Output: $this->results the text from the web page
  669. \*======================================================================*/
  670. function fetchtext($URI)
  671. {
  672. if($this->fetch($URI))
  673. {
  674. if(is_array($this->results))
  675. {
  676. for($x=0;$x<count($this->results);$x++)
  677. $this->results[$x] = $this->_striptext($this->results[$x]);
  678. }
  679. else
  680. $this->results = $this->_striptext($this->results);
  681. return true;
  682. }
  683. else
  684. return false;
  685. }
  686. /*======================================================================*\
  687. Function: submitlinks
  688. Purpose: grab links from a form submission
  689. Input: $URI where you are submitting from
  690. Output: $this->results an array of the links from the post
  691. \*======================================================================*/
  692. function submitlinks($URI, $formvars="", $formfiles="")
  693. {
  694. if($this->submit($URI,$formvars, $formfiles))
  695. {
  696. if($this->lastredirectaddr)
  697. $URI = $this->lastredirectaddr;
  698. if(is_array($this->results))
  699. {
  700. for($x=0;$x<count($this->results);$x++)
  701. {
  702. $this->results[$x] = $this->_striplinks($this->results[$x]);
  703. if($this->expandlinks)
  704. $this->results[$x] = $this->_expandlinks($this->results[$x],$URI);
  705. }
  706. }
  707. else
  708. {
  709. $this->results = $this->_striplinks($this->results);
  710. if($this->expandlinks)
  711. $this->results = $this->_expandlinks($this->results,$URI);
  712. }
  713. return true;
  714. }
  715. else
  716. return false;
  717. }
  718. /*======================================================================*\
  719. Function: submittext
  720. Purpose: grab text from a form submission
  721. Input: $URI where you are submitting from
  722. Output: $this->results the text from the web page
  723. \*======================================================================*/
  724. function submittext($URI, $formvars = "", $formfiles = "")
  725. {
  726. if($this->submit($URI,$formvars, $formfiles))
  727. {
  728. if($this->lastredirectaddr)
  729. $URI = $this->lastredirectaddr;
  730. if(is_array($this->results))
  731. {
  732. for($x=0;$x<count($this->results);$x++)
  733. {
  734. $this->results[$x] = $this->_striptext($this->results[$x]);
  735. if($this->expandlinks)
  736. $this->results[$x] = $this->_expandlinks($this->results[$x],$URI);
  737. }
  738. }
  739. else
  740. {
  741. $this->results = $this->_striptext($this->results);
  742. if($this->expandlinks)
  743. $this->results = $this->_expandlinks($this->results,$URI);
  744. }
  745. return true;
  746. }
  747. else
  748. return false;
  749. }
  750. /*======================================================================*\
  751. Function: set_submit_multipart
  752. Purpose: Set the form submission content type to
  753. multipart/form-data
  754. \*======================================================================*/
  755. function set_submit_multipart()
  756. {
  757. $this->_submit_type = "multipart/form-data";
  758. }
  759. /*======================================================================*\
  760. Function: set_submit_normal
  761. Purpose: Set the form submission content type to
  762. application/x-www-form-urlencoded
  763. \*======================================================================*/
  764. function set_submit_normal()
  765. {
  766. $this->_submit_type = "application/x-www-form-urlencoded";
  767. }
  768. /*======================================================================*\
  769. Private functions
  770. \*======================================================================*/
  771. /*======================================================================*\
  772. Function: _striplinks
  773. Purpose: strip the hyperlinks from an html document
  774. Input: $document document to strip.
  775. Output: $match an array of the links
  776. \*======================================================================*/
  777. function _striplinks($document)
  778. {
  779. preg_match_all("'<\s*a\s.*?href\s*=\s* # find <a href=
  780. ([\"\'])? # find single or double quote
  781. (?(1) (.*?)\\1 | ([^\s\>]+)) # if quote found, match up to next matching
  782. # quote, otherwise match up to next space
  783. 'isx",$document,$links);
  784. // catenate the non-empty matches from the conditional subpattern
  785. foreach($links[2] as $key => $val)
  786. {
  787. if(!empty($val))
  788. $match[] = $val;
  789. }
  790. foreach($links[3] as $key => $val)
  791. {
  792. if(!empty($val))
  793. $match[] = $val;
  794. }
  795. // return the links
  796. return $match;
  797. }
  798. /*======================================================================*\
  799. Function: _stripform
  800. Purpose: strip the form elements from an html document
  801. Input: $document document to strip.
  802. Output: $match an array of the links
  803. \*======================================================================*/
  804. function _stripform($document)
  805. {
  806. preg_match_all("'<\/?(FORM|INPUT|SELECT|TEXTAREA|(OPTION))[^<>]*>(?(2)(.*(?=<\/?(option|select)[^<>]*>[\r\n]*)|(?=[\r\n]*))|(?=[\r\n]*))'Usi",$document,$elements);
  807. // catenate the matches
  808. $match = implode("\r\n",$elements[0]);
  809. // return the links
  810. return $match;
  811. }
  812. /*======================================================================*\
  813. Function: _striptext
  814. Purpose: strip the text from an html document
  815. Input: $document document to strip.
  816. Output: $text the resulting text
  817. \*======================================================================*/
  818. function _striptext($document)
  819. {
  820. // I didn't use preg eval (//e) since that is only available in PHP 4.0.
  821. // so, list your entities one by one here. I included some of the
  822. // more common ones.
  823. $search = array("'<script[^>]*?>.*?</script>'si", // strip out javascript
  824. "'<[\/\!]*?[^<>]*?>'si", // strip out html tags
  825. "'([\r\n])[\s]+'", // strip out white space
  826. "'&(quot|#34|#034|#x22);'i", // replace html entities
  827. "'&(amp|#38|#038|#x26);'i", // added hexadecimal values
  828. "'&(lt|#60|#060|#x3c);'i",
  829. "'&(gt|#62|#062|#x3e);'i",
  830. "'&(nbsp|#160|#xa0);'i",
  831. "'&(iexcl|#161);'i",
  832. "'&(cent|#162);'i",
  833. "'&(pound|#163);'i",
  834. "'&(copy|#169);'i",
  835. "'&(reg|#174);'i",
  836. "'&(deg|#176);'i",
  837. "'&(#39|#039|#x27);'",
  838. "'&(euro|#8364);'i", // europe
  839. "'&a(uml|UML);'", // german
  840. "'&o(uml|UML);'",
  841. "'&u(uml|UML);'",
  842. "'&A(uml|UML);'",
  843. "'&O(uml|UML);'",
  844. "'&U(uml|UML);'",
  845. "'&szlig;'i",
  846. );
  847. $replace = array( "",
  848. "",
  849. "\\1",
  850. "\"",
  851. "&",
  852. "<",
  853. ">",
  854. " ",
  855. chr(161),
  856. chr(162),
  857. chr(163),
  858. chr(169),
  859. chr(174),
  860. chr(176),
  861. chr(39),
  862. chr(128),
  863. "?",
  864. "?",
  865. "?",
  866. "?",
  867. "?",
  868. "?",
  869. "?",
  870. );
  871. $text = preg_replace($search,$replace,$document);
  872. return $text;
  873. }
  874. /*======================================================================*\
  875. Function: _expandlinks
  876. Purpose: expand each link into a fully qualified URL
  877. Input: $links the links to qualify
  878. $URI the full URI to get the base from
  879. Output: $expandedLinks the expanded links
  880. \*======================================================================*/
  881. function _expandlinks($links,$URI)
  882. {
  883. preg_match("/^[^\?]+/",$URI,$match);
  884. $match = preg_replace("|/[^\/\.]+\.[^\/\.]+$|","",$match[0]);
  885. $match = preg_replace("|/$|","",$match);
  886. $match_part = parse_url($match);
  887. $match_root =
  888. $match_part["scheme"]."://".$match_part["host"];
  889. $search = array( "|^http://".preg_quote($this->host)."|i",
  890. "|^(\/)|i",
  891. "|^(?!http://)(?!mailto:)|i",
  892. "|/\./|",
  893. "|/[^\/]+/\.\./|"
  894. );
  895. $replace = array( "",
  896. $match_root."/",
  897. $match."/",
  898. "/",
  899. "/"
  900. );
  901. $expandedLinks = preg_replace($search,$replace,$links);
  902. return $expandedLinks;
  903. }
  904. /*======================================================================*\
  905. Function: _httprequest
  906. Purpose: go get the http data from the server
  907. Input: $url the url to fetch
  908. $fp the current open file pointer
  909. $URI the full URI
  910. $body body contents to send if any (POST)
  911. Output:
  912. \*======================================================================*/
  913. function _httprequest($url,$fp,$URI,$http_method,$content_type="",$body="")
  914. {
  915. $cookie_headers = '';
  916. if($this->passcookies && $this->_redirectaddr)
  917. $this->setcookies();
  918. $URI_PARTS = parse_url($URI);
  919. if(empty($url))
  920. $url = "/";
  921. $headers = $http_method." ".$url." ".$this->_httpversion."\r\n";
  922. if(!empty($this->agent))
  923. $headers .= "User-Agent: ".$this->agent."\r\n";
  924. if(!empty($this->host) && !isset($this->rawheaders['Host'])) {
  925. $headers .= "Host: ".$this->host;
  926. if(!empty($this->port))
  927. $headers .= ":".$this->port;
  928. $headers .= "\r\n";
  929. }
  930. if(!empty($this->accept))
  931. $headers .= "Accept: ".$this->accept."\r\n";
  932. if(!empty($this->referer))
  933. $headers .= "Referer: ".$this->referer."\r\n";
  934. if(!empty($this->cookies))
  935. {
  936. if(!is_array($this->cookies))
  937. $this->cookies = (array)$this->cookies;
  938. reset($this->cookies);
  939. if ( count($this->cookies) > 0 ) {
  940. $cookie_headers .= 'Cookie: ';
  941. foreach ( $this->cookies as $cookieKey => $cookieVal ) {
  942. $cookie_headers .= $cookieKey."=".urlencode($cookieVal)."; ";
  943. }
  944. $headers .= substr($cookie_headers,0,-2) . "\r\n";
  945. }
  946. }
  947. if(!empty($this->rawheaders))
  948. {
  949. if(!is_array($this->rawheaders))
  950. {
  951. $this->rawheaders = (array)$this->rawheaders;
  952. }
  953. foreach($this->rawheaders as $headerKey => $headerVal)
  954. {
  955. $headers .= $headerKey.": ".$headerVal."\r\n";
  956. }
  957. }
  958. if(!empty($content_type)) {
  959. $headers .= "Content-type: $content_type";
  960. if ($content_type == "multipart/form-data")
  961. $headers .= "; boundary=".$this->_mime_boundary;
  962. $headers .= "\r\n";
  963. }
  964. if(!empty($body))
  965. $headers .= "Content-length: ".strlen($body)."\r\n";
  966. if(!empty($this->user) || !empty($this->pass))
  967. $headers .= "Authorization: Basic ".base64_encode($this->user.":".$this->pass)."\r\n";
  968. //add proxy auth headers
  969. if(!empty($this->proxy_user))
  970. $headers .= 'Proxy-Authorization: ' . 'Basic ' . base64_encode($this->proxy_user . ':' . $this->proxy_pass)."\r\n";
  971. $headers .= "\r\n";
  972. // set the read timeout if needed
  973. if ($this->read_timeout > 0)
  974. socket_set_timeout($fp, $this->read_timeout);
  975. $this->timed_out = false;
  976. fwrite($fp,$headers.$body,strlen($headers.$body));
  977. $this->_redirectaddr = false;
  978. unset($this->headers);
  979. while($currentHeader = fgets($fp,$this->_maxlinelen))
  980. {
  981. if ($this->read_timeout > 0 && $this->_check_timeout($fp))
  982. {
  983. $this->status=-100;
  984. return false;
  985. }
  986. if($currentHeader == "\r\n")
  987. break;
  988. // if a header begins with Location: or URI:, set the redirect
  989. if(preg_match("/^(Location:|URI:)/i",$currentHeader))
  990. {
  991. // get URL portion of the redirect
  992. preg_match("/^(Location:|URI:)[ ]+(.*)/i",chop($currentHeader),$matches);
  993. // look for :// in the Location header to see if hostname is included
  994. if(!preg_match("|\:\/\/|",$matches[2]))
  995. {
  996. // no host in the path, so prepend
  997. $this->_redirectaddr = $URI_PARTS["scheme"]."://".$this->host.":".$this->port;
  998. // eliminate double slash
  999. if(!preg_match("|^/|",$matches[2]))
  1000. $this->_redirectaddr .= "/".$matches[2];
  1001. else
  1002. $this->_redirectaddr .= $matches[2];
  1003. }
  1004. else
  1005. $this->_redirectaddr = $matches[2];
  1006. }
  1007. if(preg_match("|^HTTP/|",$currentHeader))
  1008. {
  1009. if(preg_match("|^HTTP/[^\s]*\s(.*?)\s|",$currentHeader, $status))
  1010. {
  1011. $this->status= $status[1];
  1012. }
  1013. $this->response_code = $currentHeader;
  1014. }
  1015. $this->headers[] = $currentHeader;
  1016. }
  1017. $results = '';
  1018. do {
  1019. $_data = fread($fp, $this->maxlength);
  1020. if (strlen($_data) == 0) {
  1021. break;
  1022. }
  1023. $results .= $_data;
  1024. } while(true);
  1025. if ($this->read_timeout > 0 && $this->_check_timeout($fp))
  1026. {
  1027. $this->status=-100;
  1028. return false;
  1029. }
  1030. // check if there is a a redirect meta tag
  1031. if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match))
  1032. {
  1033. $this->_redirectaddr = $this->_expandlinks($match[1],$URI);
  1034. }
  1035. // have we hit our frame depth and is there frame src to fetch?
  1036. if(($this->_framedepth < $this->maxframes) && preg_match_all("'<frame\s+.*src[\s]*=[\'\"]?([^\'\"\>]+)'i",$results,$match))
  1037. {
  1038. $this->results[] = $results;
  1039. for($x=0; $x<count($match[1]); $x++)
  1040. $this->_frameurls[] = $this->_expandlinks($match[1][$x],$URI_PARTS["scheme"]."://".$this->host);
  1041. }
  1042. // have we already fetched framed content?
  1043. elseif(is_array($this->results))
  1044. $this->results[] = $results;
  1045. // no framed content
  1046. else
  1047. $this->results = $results;
  1048. return true;
  1049. }
  1050. /*======================================================================*\
  1051. Function: _httpsrequest
  1052. Purpose: go get the https data from the server using curl
  1053. Input: $url the url to fetch
  1054. $URI the full URI
  1055. $body body contents to send if any (POST)
  1056. Output:
  1057. \*======================================================================*/
  1058. function _httpsrequest($url,$URI,$http_method,$content_type="",$body="")
  1059. {
  1060. if($this->passcookies && $this->_redirectaddr)
  1061. $this->setcookies();
  1062. $headers = array();
  1063. $URI_PARTS = parse_url($URI);
  1064. if(empty($url))
  1065. $url = "/";
  1066. // GET ... header not needed for curl
  1067. //$headers[] = $http_method." ".$url." ".$this->_httpversion;
  1068. if(!empty($this->agent))
  1069. $headers[] = "User-Agent: ".$this->agent;
  1070. if(!empty($this->host))
  1071. if(!empty($this->port))
  1072. $headers[] = "Host: ".$this->host.":".$this->port;
  1073. else
  1074. $headers[] = "Host: ".$this->host;
  1075. if(!empty($this->accept))
  1076. $headers[] = "Accept: ".$this->accept;
  1077. if(!empty($this->referer))
  1078. $headers[] = "Referer: ".$this->referer;
  1079. if(!empty($this->cookies))
  1080. {
  1081. if(!is_array($this->cookies))
  1082. $this->cookies = (array)$this->cookies;
  1083. reset($this->cookies);
  1084. if ( count($this->cookies) > 0 ) {
  1085. $cookie_str = 'Cookie: ';
  1086. foreach ( $this->cookies as $cookieKey => $cookieVal ) {
  1087. $cookie_str .= $cookieKey."=".urlencode($cookieVal)."; ";
  1088. }
  1089. $headers[] = substr($cookie_str,0,-2);
  1090. }
  1091. }
  1092. if(!empty($this->rawheaders))
  1093. {
  1094. if(!is_array($this->rawheaders))
  1095. {
  1096. $this->rawheaders = (array)$this->rawheaders;
  1097. }
  1098. foreach($this->rawheaders as $headerKey => $headerVal)
  1099. {
  1100. $headers[] = $headerKey.": ".$headerVal;
  1101. }
  1102. }
  1103. if(!empty($content_type)) {
  1104. if ($content_type == "multipart/form-data")
  1105. $headers[] = "Content-type: $content_type; boundary=".$this->_mime_boundary;
  1106. else
  1107. $headers[] = "Content-type: $content_type";
  1108. }
  1109. if(!empty($body))
  1110. $headers[] = "Content-length: ".strlen($body);
  1111. if(!empty($this->user) || !empty($this->pass))
  1112. $headers[] = "Authorization: BASIC ".base64_encode($this->user.":".$this->pass);
  1113. for($curr_header = 0; $curr_header < count($headers); $curr_header++) {
  1114. $safer_header = strtr( $headers[$curr_header], "\"", " " );
  1115. $cmdline_params .= " -H \"".$safer_header."\"";
  1116. }
  1117. if(!empty($body))
  1118. $cmdline_params .= " -d \"$body\"";
  1119. if($this->read_timeout > 0)
  1120. $cmdline_params .= " -m ".$this->read_timeout;
  1121. $headerfile = tempnam($temp_dir, "sno");
  1122. exec($this->curl_path." -k -D \"$headerfile\"".$cmdline_params." \"".escapeshellcmd($URI)."\"",$results,$return);
  1123. if($return)
  1124. {
  1125. $this->error = "Error: cURL could not retrieve the document, error $return.";
  1126. return false;
  1127. }
  1128. $results = implode("\r\n",$results);
  1129. $result_headers = file("$headerfile");
  1130. $this->_redirectaddr = false;
  1131. unset($this->headers);
  1132. for($currentHeader = 0; $currentHeader < count($result_headers); $currentHeader++)
  1133. {
  1134. // if a header begins with Location: or URI:, set the redirect
  1135. if(preg_match("/^(Location: |URI: )/i",$result_headers[$currentHeader]))
  1136. {
  1137. // get URL portion of the redirect
  1138. preg_match("/^(Location: |URI:)\s+(.*)/",chop($result_headers[$currentHeader]),$matches);
  1139. // look for :// in the Location header to see if hostname is included
  1140. if(!preg_match("|\:\/\/|",$matches[2]))
  1141. {
  1142. // no host in the path, so prepend
  1143. $this->_redirectaddr = $URI_PARTS["scheme"]."://".$this->host.":".$this->port;
  1144. // eliminate double slash
  1145. if(!preg_match("|^/|",$matches[2]))
  1146. $this->_redirectaddr .= "/".$matches[2];
  1147. else
  1148. $this->_redirectaddr .= $matches[2];
  1149. }
  1150. else
  1151. $this->_redirectaddr = $matches[2];
  1152. }
  1153. if(preg_match("|^HTTP/|",$result_headers[$currentHeader]))
  1154. $this->response_code = $result_headers[$currentHeader];
  1155. $this->headers[] = $result_headers[$currentHeader];
  1156. }
  1157. // check if there is a a redirect meta tag
  1158. if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match))
  1159. {
  1160. $this->_redirectaddr = $this->_expandlinks($match[1],$URI);
  1161. }
  1162. // have we hit our frame depth and is there frame src to fetch?
  1163. if(($this->_framedepth < $this->maxframes) && preg_match_all("'<frame\s+.*src[\s]*=[\'\"]?([^\'\"\>]+)'i",$results,$match))
  1164. {
  1165. $this->results[] = $results;
  1166. for($x=0; $x<count($match[1]); $x++)
  1167. $this->_frameurls[] = $this->_expandlinks($match[1][$x],$URI_PARTS["scheme"]."://".$this->host);
  1168. }
  1169. // have we already fetched framed content?
  1170. elseif(is_array($this->results))
  1171. $this->results[] = $results;
  1172. // no framed content
  1173. else
  1174. $this->results = $results;
  1175. unlink("$headerfile");
  1176. return true;
  1177. }
  1178. /*======================================================================*\
  1179. Function: setcookies()
  1180. Purpose: set cookies for a redirection
  1181. \*======================================================================*/
  1182. function setcookies()
  1183. {
  1184. for($x=0; $x<count($this->headers); $x++)
  1185. {
  1186. if(preg_match('/^set-cookie:[\s]+([^=]+)=([^;]+)/i', $this->headers[$x],$match))
  1187. $this->cookies[$match[1]] = urldecode($match[2]);
  1188. }
  1189. }
  1190. /*======================================================================*\
  1191. Function: _check_timeout
  1192. Purpose: checks whether timeout has occurred
  1193. Input: $fp file pointer
  1194. \*======================================================================*/
  1195. function _check_timeout($fp)
  1196. {
  1197. if ($this->read_timeout > 0) {
  1198. $fp_status = socket_get_status($fp);
  1199. if ($fp_status["timed_out"]) {
  1200. $this->timed_out = true;
  1201. return true;
  1202. }
  1203. }
  1204. return false;
  1205. }
  1206. /*======================================================================*\
  1207. Function: _connect
  1208. Purpose: make a socket connection
  1209. Input: $fp file pointer
  1210. \*======================================================================*/
  1211. function _connect(&$fp)
  1212. {
  1213. if(!empty($this->proxy_host) && !empty($this->proxy_port))
  1214. {
  1215. $this->_isproxy = true;
  1216. $host = $this->proxy_host;
  1217. $port = $this->proxy_port;
  1218. }
  1219. else
  1220. {
  1221. $host = $this->host;
  1222. $port = $this->port;
  1223. }
  1224. $this->status = 0;
  1225. if($fp = fsockopen(
  1226. $host,
  1227. $port,
  1228. $errno,
  1229. $errstr,
  1230. $this->_fp_timeout
  1231. ))
  1232. {
  1233. // socket connection succeeded
  1234. return true;
  1235. }
  1236. else
  1237. {
  1238. // socket connection failed
  1239. $this->status = $errno;
  1240. switch($errno)
  1241. {
  1242. case -3:
  1243. $this->error="socket creation failed (-3)";
  1244. case -4:
  1245. $this->error="dns lookup failure (-4)";
  1246. case -5:
  1247. $this->error="connection refused or timed out (-5)";
  1248. default:
  1249. $this->error="connection failed (".$errno.")";
  1250. }
  1251. return false;
  1252. }
  1253. }
  1254. /*======================================================================*\
  1255. Function: _disconnect
  1256. Purpose: disconnect a socket connection
  1257. Input: $fp file pointer
  1258. \*======================================================================*/
  1259. function _disconnect($fp)
  1260. {
  1261. return(fclose($fp));
  1262. }
  1263. /*======================================================================*\
  1264. Function: _prepare_post_body
  1265. Purpose: Prepare post body according to encoding type
  1266. Input: $formvars - form variables
  1267. $formfiles - form upload files
  1268. Output: post body
  1269. \*======================================================================*/
  1270. function _prepare_post_body($formvars, $formfiles)
  1271. {
  1272. settype($formvars, "array");
  1273. settype($formfiles, "array");
  1274. $postdata = '';
  1275. if (count($formvars) == 0 && count($formfiles) == 0)
  1276. return;
  1277. switch ($this->_submit_type) {
  1278. case "application/x-www-form-urlencoded":
  1279. reset($formvars);
  1280. foreach($formvars as $key => $val)
  1281. {
  1282. if (is_array($val) || is_object($val)) {
  1283. foreach($val as $cur_key => $cur_val)
  1284. {
  1285. $postdata .= urlencode($key)."[]=".urlencode($cur_val)."&";
  1286. }
  1287. } else
  1288. {
  1289. $postdata .= urlencode($key)."=".urlencode($val)."&";
  1290. }
  1291. }
  1292. break;
  1293. case "multipart/form-data":
  1294. $this->_mime_boundary = "Snoopy".md5(uniqid(microtime()));
  1295. reset($formvars);
  1296. foreach($formvars as $key => $val)
  1297. {
  1298. if (is_array($val) || is_object($val)) {
  1299. foreach($val as $cur_key => $cur_val) {
  1300. $postdata .= "--".$this->_mime_boundary."\r\n";
  1301. $postdata .= "Content-Disposition: form-data; name=\"$key\[\]\"\r\n\r\n";
  1302. $postdata .= "$cur_val\r\n";
  1303. }
  1304. } else {
  1305. $postdata .= "--".$this->_mime_boundary."\r\n";
  1306. $postdata .= "Content-Disposition: form-data; name=\"$key\"\r\n\r\n";
  1307. $postdata .= "$val\r\n";
  1308. }
  1309. }
  1310. reset($formfiles);
  1311. foreach($formfiles as $field_name => $file_names) {
  1312. settype($file_names, "array");
  1313. foreach($file_names as $file_name) {
  1314. if (!is_readable($file_name)) continue;
  1315. $fp = fopen($file_name, "r");
  1316. $file_content = fread($fp, filesize($file_name));
  1317. fclose($fp);
  1318. $base_name = basename($file_name);
  1319. $postdata .= "--".$this->_mime_boundary."\r\n";
  1320. $postdata .= "Content-Disposition: form-data; name=\"$field_name\"; filename=\"$base_name\"\r\n\r\n";
  1321. $postdata .= "$file_content\r\n";
  1322. }
  1323. }
  1324. $postdata .= "--".$this->_mime_boundary."--\r\n";
  1325. break;
  1326. }
  1327. return $postdata;
  1328. }
  1329. }
  1330. ?>