Guest User

Untitled

a guest
Dec 11th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.45 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. |_ |_ ._ o _| |_ _ _.._ _|
  5. | |\/|_)| |(_| |_)(_)(_|| (_|
  6. /
  7. */
  8.  
  9. /*
  10. @HTTPRequest
  11. */
  12. class HTTPRequest {
  13.  
  14. /*
  15. @strDomain
  16. */
  17. private $strDomain;
  18.  
  19. /*
  20. @strFile
  21. */
  22. private $strFile;
  23.  
  24. /*
  25. @intPort
  26. */
  27. private $intPort = 80;
  28.  
  29. /*
  30. @resFile
  31. */
  32. private $resFile = null;
  33.  
  34. /*
  35. @intType
  36. */
  37. private $intType = 1;
  38.  
  39. /*
  40. @strHeader
  41. */
  42. private $strHeader = '';
  43.  
  44. /*
  45. @boolSended
  46. */
  47. private $boolSended = false;
  48.  
  49. /*
  50. @strResponseHeaders
  51. */
  52. private $strResponseHeaders = false;
  53.  
  54. /*
  55. @POST
  56. */
  57. const POST = 1;
  58.  
  59. /*
  60. @GET
  61. */
  62. const GET = 2;
  63.  
  64. /*
  65. @HTTPSTATUSCODE
  66. */
  67. const HTTPSTATUSCODE = 3;
  68.  
  69. /*
  70. @HTTPSTATUS
  71. */
  72. const HTTPSTATUS = 4;
  73.  
  74. /*
  75. @CONTENTTYPE
  76. */
  77. const CONTENTTYPE = 5;
  78.  
  79. /*
  80. @CONTENTLENGTH
  81. */
  82. const CONTENTLENGTH = 6;
  83.  
  84. /*
  85. @CONNECTIONSTATUS
  86. */
  87. const CONNECTIONSTATUS = 7;
  88.  
  89. /*
  90. @CRLF
  91. */
  92. const CRLF = "\r\n";
  93.  
  94. /*
  95. @strResponse
  96. */
  97. private $strResponse = false;
  98.  
  99. /*
  100. @intHTTPStatusCode
  101. */
  102. private $intHTTPStatusCode = 0;
  103.  
  104. /*
  105. @strHTTPStatus
  106. */
  107. private $strHTTPStatus = '';
  108.  
  109. /*
  110. @strContentType
  111. */
  112. private $strContentType = '';
  113.  
  114. /*
  115. @intContentLength
  116. */
  117. private $intContentLength = 0;
  118.  
  119. /*
  120. @strConnectionStatus
  121. */
  122. private $strConnectionStatus = 'unknown';
  123.  
  124. /*
  125. @arrResponseHeaders
  126. */
  127. private $arrResponseHeaders = array();
  128.  
  129. /*
  130. @__construct
  131. */
  132. public function __construct($strDomain, $strFile = '', $intPort = 80) {
  133. // check domain
  134. if ($strDomain == gethostbyname($strDomain)) {
  135. return false;
  136. }
  137.  
  138. // set vars
  139. $this->strDomain = $strDomain;
  140. $this->strFile = $strFile;
  141. $this->intPort = (int)$intPort;
  142.  
  143. // connect
  144. return $this->connect();
  145. }
  146.  
  147. /*
  148. @connect
  149. */
  150. private function connect() {
  151. $this->resFile = @fsockopen($this->strDomain, $this->intPort);
  152.  
  153. if ($this->resFile == false) {
  154. return false;
  155. }
  156.  
  157. return true;
  158. }
  159.  
  160. /*
  161. @setDefaultHeader
  162. */
  163. public function setDefaultHeader() {
  164. // set default header
  165. $this->strHeader = (($this->intType == 1) ? 'POST' : 'GET').' /'.$this->strFile.' HTTP/1.1'.self::CRLF;
  166. }
  167.  
  168. /*
  169. @addCustomHeader
  170. */
  171. public function addCustomHeader($strHeader, $strValue) {
  172. // add header
  173. $this->strHeader .= $strHeader.((!empty($strHeader) && $strHeader != self::CRLF) ? ': ' : '').$strValue.self::CRLF;
  174. }
  175.  
  176. /*
  177. @getHeaders
  178. */
  179. public function getHeaders() {
  180. return $this->strHeader;
  181. }
  182.  
  183. /*
  184. @send
  185. */
  186. public function send($arrData = null, $intType = 2) {
  187. //
  188. $strData = '';
  189.  
  190. $this->intType = $intType;
  191.  
  192. if (is_array($arrData)) {
  193. switch ($this->intType) {
  194. // POST
  195. case 1:
  196. // set counter to 0
  197. $intCounter = 0;
  198.  
  199. // prepare data
  200. foreach ($arrData as $strKey => $strValue) {
  201. $intCounter++;
  202.  
  203. // add
  204. $strData .= $strKey.'='.urlencode($strValue).'&';
  205. }
  206.  
  207. // remove last &
  208. $strData = substr($strData, 0, (strlen($strData) - 1));
  209.  
  210. // save
  211. $this->strData = $strData;
  212. break;
  213. // GET
  214. case 2:
  215. // look for get parameters
  216. if (stristr($this->strFile, '?') === false) {
  217. $this->strFile .= '?';
  218. } else {
  219. $this->strFile .= '&';
  220. }
  221.  
  222. // set counter to 0
  223. $intCounter = 0;
  224.  
  225. // prepare data
  226. foreach ($arrData as $strKey => $strValue) {
  227. $intCounter++;
  228.  
  229. // add
  230. $strData .= $strKey.'='.urlencode($strValue).'&';
  231. }
  232.  
  233. // remove last &
  234. $strData = substr($strData, 0, (strlen($strData) - 1));
  235.  
  236. // add to file
  237. $this->strFile .= $strData;
  238. break;
  239. }
  240. } else {
  241. $this->strData = '';
  242. }
  243.  
  244. // prepare headers
  245. $this->setDefaultHeader();
  246.  
  247. $this->addCustomHeader('Host', $this->strDomain);
  248.  
  249. // post headers
  250. if ($this->intType == 1 && !empty($this->strData)) {
  251. // post header
  252. $this->addCustomHeader('Content-Type', 'application/x-www-form-urlencoded');
  253. }
  254.  
  255.  
  256.  
  257. if ($this->intType == 1 && !empty($this->strData)) {
  258. // content length
  259. $this->addCustomHeader('Content-Length', strlen($this->strData));
  260.  
  261. // data
  262. $this->addCustomHeader(self::CRLF, $this->strData);
  263. }
  264.  
  265. // connection
  266. $this->addCustomHeader(self::CRLF.'Connection', 'close');
  267.  
  268. // send
  269. if (false !== fwrite($this->resFile, $this->getHeaders())) {
  270. $this->boolSended = true;
  271.  
  272. return true;
  273. } else {
  274. return false;
  275. }
  276. }
  277.  
  278. /*
  279. @getStatus
  280. */
  281. public function getStatus() {
  282. return $this->boolSended;
  283. }
  284.  
  285. /*
  286. @unchunkResponse
  287. */
  288. public function unchunkResponse($strResponse) {
  289. return preg_replace_callback('/(?:(?:\r\n|\n)|^)([0-9A-F]+)(?:\r\n|\n){1,2}(.*?)((?:\r\n|\n)(?:[0-9A-F]+(?:\r\n|\n))|$)/si', create_function('$arrMatch', 'return (hexdec($arrMatch[1]) == strlen($arrMatch[2]) ? $arrMatch[2] : $arrMatch[0]);'), $strResponse);
  290. }
  291.  
  292. /*
  293. @saveHTTPStatus
  294. */
  295. private function saveHTTPStatus($arrMatch) {
  296. $this->intHTTPStatusCode = (int)$arrMatch[2];
  297. $this->strHTTPStatus = $arrMatch[3];
  298. }
  299.  
  300. /*
  301. @saveContentType
  302. */
  303. private function saveContentType($arrMatch) {
  304. $this->strContentType = $arrMatch[1];
  305. }
  306.  
  307. /*
  308. @saveContentLength
  309. */
  310. private function saveContentLength($arrMatch) {
  311. $this->intContentLength = (int)$arrMatch[1];
  312. }
  313.  
  314. /*
  315. @saveConnectionStatus
  316. */
  317. private function saveConnectionStatus($arrMatch) {
  318. $this->strConnectionStatus = $arrMatch[1];
  319. }
  320.  
  321. /*
  322. @getResponse
  323. */
  324. public function getResponse() {
  325. if ($this->getStatus() == false) {
  326. return false;
  327. }
  328.  
  329. if ($this->strResponse == false) {
  330. $strResponse = '';
  331.  
  332. // get response
  333. while ($strLine = fgets($this->resFile, 128)) {
  334. $strResponse .= $strLine;
  335. }
  336.  
  337. $this->strResponse = $strResponse;
  338. }
  339.  
  340. // split response
  341. $arrSplitted = explode(self::CRLF.self::CRLF, $this->strResponse);
  342.  
  343. unset($arrSplitted[1]);
  344.  
  345. // replace
  346. $strContent = str_replace($arrSplitted, '', $this->strResponse);
  347.  
  348. // save headers
  349. $this->strResponseHeaders = implode(self::CRLF.self::CRLF, $arrSplitted);
  350.  
  351. return utf8_encode($this->unchunkResponse($strContent));
  352. }
  353.  
  354. /*
  355. @getResponseHeaders
  356. */
  357. public function getResponseHeaders() {
  358. // check string
  359. if ($this->strResponseHeaders == false) {
  360. $this->getResponse();
  361. }
  362.  
  363. // get status code
  364. preg_replace_callback('#HTTP\/(1.0|1.1) ([0-9]+) ([A-Za-z]+)#', array($this, 'saveHTTPStatus'), $this->strResponseHeaders);
  365.  
  366. // get content type
  367. preg_replace_callback('#Content\-Type\: (.*)#', array($this, 'saveContentType'), $this->strResponseHeaders);
  368.  
  369. // get content length
  370. preg_replace_callback('#Content\-Length\: ([0-9]+)#', array($this, 'saveContentLength'), $this->strResponseHeaders);
  371.  
  372. // get connection status
  373. preg_replace_callback('#Connection\: ([a-z]+)#', array($this, 'saveConnectionStatus'), $this->strResponseHeaders);
  374.  
  375. // set array
  376. $this->arrResponseHeaders = array(
  377. 'httpStatus' => array(
  378. 'code' => $this->intHTTPStatusCode,
  379. 'text' => $this->strHTTPStatus
  380. ),
  381. 'contentType' => $this->strContentType,
  382. 'contentLength' => $this->intContentLength,
  383. 'connection' => $this->strConnectionStatus
  384. );
  385.  
  386. return $this->arrResponseHeaders;
  387. }
  388.  
  389. /*
  390. @getResponseHeader
  391. */
  392. public function getResponseHeader($strHeader) {
  393. // check headers
  394. if ($this->arrResponseHeaders == false) {
  395. $this->getResponseHeaders();
  396. }
  397.  
  398. switch ($strHeader) {
  399. // status code
  400. case 3:
  401. return $this->arrResponseHeaders['httpStatus']['code'];
  402. break;
  403.  
  404. // status
  405. case 4:
  406. return $this->arrResponseHeaders['httpStatus']['text'];
  407. break;
  408.  
  409. // content type
  410. case 5:
  411. return $this->arrResponseHeaders['contentType'];
  412. break;
  413.  
  414. // content length
  415. case 6:
  416. return $this->arrResponseHeaders['contentLength'];
  417. break;
  418.  
  419. // connection status
  420. case 7:
  421. return $this->arrResponseHeaders['connection'];
  422. break;
  423.  
  424. default:
  425. return null;
  426. }
  427. }
  428.  
  429. }
  430.  
  431. /*
  432.  
  433. usage:
  434.  
  435. new http request:
  436. $HTTPRequest = new HTTPRequest('domain.com', 'file.php');
  437.  
  438. send data:
  439. $HTTPRequest->send(array('key' => 'value'), HTTPRequest::POST); // HTTPRequest::POST // HTTPRequest::GET
  440.  
  441. get data:
  442. $content = $HTTPRequest->getResponse();
  443.  
  444. get header:
  445. $status = $HTTPRequest->getResponseHeader(HTTPRequest::HTTPSTATUSCODE); // 200 // 403 etc.
  446. $statustext = $HTTPRequest->getResponseHeader(HTTPRequest::HTTPSTATUS); // OK // forbidden etc.
  447.  
  448. */
  449.  
  450. ?>
Add Comment
Please, Sign In to add comment