Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.48 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. |_ |_ ._ o _| |_ _ _.._ _|
  5. | |\/|_)| |(_| |_)(_)(_|| (_|
  6. /
  7. */
  8.  
  9. if (!defined('CRLF')) define('CRLF', "\r\n");
  10.  
  11. /*
  12. @Request
  13. */
  14. class Request {
  15.  
  16. /*
  17. @strSite
  18. */
  19. private $strSite;
  20.  
  21. /*
  22. @strFile
  23. */
  24. private $strFile;
  25.  
  26. /*
  27. @intPort
  28. */
  29. private $intPort = 80;
  30.  
  31. /*
  32. @resFile
  33. */
  34. private $resFile = null;
  35.  
  36. /*
  37. @strType
  38. */
  39. private $strType = 'POST';
  40.  
  41. /*
  42. @arrData
  43. */
  44. private $arrData = array();
  45.  
  46. /*
  47. @boolSended
  48. */
  49. private $boolSended = false;
  50.  
  51. /*
  52. @intStatusCode
  53. */
  54. private $intStatusCode = 0;
  55.  
  56. /*
  57. @intContentLength
  58. */
  59. private $intContentLength = 0;
  60.  
  61. /*
  62. @arrCustomHeaders
  63. */
  64. private $arrCustomHeaders = array();
  65.  
  66. /*
  67. @__construct
  68. */
  69. public function __construct($strSite, $strFile, $intPort = 80) {
  70. // check site
  71. if ($strSite == gethostbyname($strSite)) {
  72. // failed
  73. return false;
  74. }
  75.  
  76. // set vars
  77. $this->strSite = $strSite;
  78. $this->intPort = $intPort;
  79.  
  80. // connect
  81. return $this->connect();
  82. }
  83.  
  84. /*
  85. @connect
  86. */
  87. private function connect() {
  88. $this->resFile = fsockopen($this->strSite, $this->intPort);
  89.  
  90. if (false == $this->resFile) {
  91. return false;
  92. } else {
  93. return true;
  94. }
  95. }
  96.  
  97. /*
  98. @setData
  99. */
  100. public function setData($arrData, $strType = 'POST') {
  101. // set type
  102. $this->strType = strtoupper($strType);
  103.  
  104. if ($this->strType == 'POST' || $this->strType == 'GET') {
  105. // save parameters
  106. if (is_array($arrData)) {
  107. foreach ($arrData as $strName => $strValue) {
  108. $this->arrData[$strName] = urlencode($strValue);
  109. }
  110. } else {
  111. return false;
  112. }
  113. }
  114.  
  115. return true;
  116. }
  117.  
  118. /*
  119. @addHeader
  120. */
  121. public function addHeader($strHeader, $strValue) {
  122. $this->arrCustomHeaders[$strHeader] = $strValue;
  123. }
  124.  
  125. /*
  126. @removeHeader
  127. */
  128. public function removeHeader($strHeader) {
  129. if (isset($this->arrCustomHeaders[$strHeader])) {
  130. unset($this->arrCustomHeaders[$strHeader]);
  131.  
  132. return true;
  133. } else {
  134. return false;
  135. }
  136. }
  137.  
  138. /*
  139. @send
  140. */
  141. public function send() {
  142. if (false == $this->resFile) {
  143. return false;
  144. }
  145.  
  146. // read data
  147. if (is_array($this->arrData)) {
  148. $intCounter = 0;
  149. $strData = '';
  150.  
  151. foreach ($this->arrData as $strKey => $strValue) {
  152. $strData .= $strKey.'='.$strValue.(($intCounter + 1 != count($this->arrData)) ? '&' : '');
  153.  
  154. $intCounter++;
  155. }
  156. }
  157.  
  158. // set header
  159. $strHeader = '';
  160. $strHeader .= $this->strType.' /'.$this->strFile.' HTTP/1.1'.CRLF;
  161. $strHeader .= 'Host: '.$this->strSite.CRLF;
  162. $strHeader .= 'Content-Type: application/x-www-form-urlencoded'.CRLF;
  163. $strHeader .= 'Content-Length: '.strlen($strData).CRLF.CRLF;
  164. $strHeader .= $strData.CRLF;
  165.  
  166. // add custom header
  167. foreach ($this->arrCustomHeaders as $strCustomHeader => $strCustomHeaderValue) {
  168. $strHeader .= $strCustomHeader.': '.$strCustomHeaderValue.CRLF;
  169. }
  170.  
  171. if (false !== fwrite($this->resFile, $strHeader)) {
  172. $this->boolSended = true;
  173.  
  174. return true;
  175. } else {
  176. return false;
  177. }
  178. }
  179.  
  180. /*
  181. @saveStatusCode
  182. */
  183. private function saveStatusCode($arrMatch) {
  184. $this->intStatusCode = $arrMatch[2];
  185. }
  186.  
  187. /*
  188. @saveContentLength
  189. */
  190. private function saveContentLength($arrMatch) {
  191. $this->intContentLength = $arrMatch[1];
  192. }
  193.  
  194. /*
  195. @unchunkHTTP
  196. */
  197. public function unchunkHTTP($strData) {
  198. $intCounter = 0;
  199. $strOutData = '';
  200.  
  201. while ($intCounter < strlen($strData)) {
  202. $intRawNum = substr($strData, $intCounter, strpos(substr($strData, $intCounter), CRLF) + 2);
  203. $intNum = hexdec(trim($intRawNum));
  204. $intCounter += strlen($intRawNum);
  205. $strChunk = substr($strData, $intCounter, $intNum);
  206. $strOutData .= $strChunk;
  207. $intCounter += strlen($strChunk);
  208. }
  209.  
  210. return $strOutData;
  211. }
  212.  
  213. /*
  214. @get
  215. */
  216. public function get($strKey = 'Content') {
  217. // check if http request was sended
  218. if (false == $this->resFile || false == $this->boolSended) {
  219. return false;
  220. }
  221.  
  222. $strContent = '';
  223. // get content
  224. while (!feof($this->resFile)) {
  225. // save content
  226. $strContent .= fread($this->resFile, 1024);
  227. }
  228.  
  229. $arrHeaders = array();
  230.  
  231. // status code
  232. preg_replace_callback('#HTTP\/(1\.0|1\1.) ([0-9]{0,4})#', array($this, 'saveStatusCode'), $strContent);
  233.  
  234. // add status code
  235. $arrHeaders['Status'] = $this->intStatusCode;
  236.  
  237. // content Length
  238. preg_replace_callback('#Content\-Length\: (.*)#', array($this, 'saveContentLength'), $strContent);
  239.  
  240. // add content length
  241. $arrHeaders['Length'] = $this->intContentLength;
  242.  
  243. $arrContent = explode(CRLF.CRLF, $strContent);
  244. $strContent = str_replace(array(CLRF, BR), '', $arrContent[1]);
  245.  
  246. // add content
  247. $arrHeaders['Content'] = $this->unchunkHTTP((($arrContent[1] == 0) ? '' : $strContent));
  248.  
  249. return $arrHeaders[$strKey];
  250. }
  251.  
  252. }
  253.  
  254.  
  255. /*
  256. usage:
  257.  
  258. $objRequest = new Request('domain.com', 'file.php', 80);
  259.  
  260. $objRequest->setData(array(
  261. 'key' => 'value'
  262. ), 'POST');
  263.  
  264. $objRequest->send();
  265.  
  266. $statusCode = $objRequest->get('Status');
  267.  
  268. echo $objRequest->get('Content'); // or $objRequest->get();
  269.  
  270. */
  271.  
  272. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement