PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/fianetfraud/fianet/classes/fianet_socket.php

http://marocmall.googlecode.com/
PHP | 172 lines | 154 code | 14 blank | 4 comment | 24 complexity | d1656682167279bb2eb202a36e7fd017 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. class fianet_socket
  3. {
  4. var $host;
  5. var $port;
  6. var $is_ssl = false;
  7. var $method = 'GET';
  8. var $data = array();
  9. var $path;
  10. function fianet_socket($fianet_url, $fianet_file, $method = 'GET', $data = array())
  11. {
  12. if (is_array($data))
  13. {
  14. $this->data = $data;
  15. }
  16. $this->parse_fianet_url($fianet_url, $fianet_file);
  17. if (strtoupper($method) == 'GET' || strtoupper($method) == 'POST')
  18. {
  19. $this->method = strtoupper($method);
  20. }
  21. }
  22. function parse_fianet_url($fianet_url, $fianet_file)
  23. {
  24. if (ereg("^http://", $fianet_url))
  25. {
  26. $this->is_ssl = false;
  27. $this->host = eregi_replace("http://([^/]+)/.+", "\\1", $fianet_url);
  28. $this->port = 80;
  29. $this->path = eregi_replace("http://[^/]+/", "/", $fianet_url) . $fianet_file;
  30. }
  31. if (ereg("^https://", $fianet_url))
  32. {
  33. $this->is_ssl = true;
  34. $this->host = eregi_replace("https://([^/]+)/.+", "\\1", $fianet_url);
  35. $this->port = 443;
  36. $this->path = eregi_replace("https://[^/]+/", "/", $fianet_url) . $fianet_file;
  37. }
  38. //debug($this->path);
  39. }
  40. function determine_boundary()
  41. {
  42. srand((double)microtime() * 1000000);
  43. return ("---------------------".substr(md5(rand(0,32000)),0,10));
  44. }
  45. function build_header($boundary, $data)
  46. {
  47. if ($this->method == 'POST')
  48. {
  49. $header = "POST ".$this->path." HTTP/1.0\r\n";
  50. $header .= "Host: ".$this->host."\r\n";
  51. $header .= "Content-type: application/x-www-form-urlencoded; boundary=$boundary\r\n";
  52. $header .= "Content-length: " . strlen($data) . "\r\n\r\n";
  53. }
  54. elseif ($this->method == 'GET')
  55. {
  56. if ($data != '')
  57. {
  58. if (strlen($this->path."?".$data) > 2048)
  59. {
  60. fianet_insert_log("fianet_socket.php - build_header() <br />\nMaximum length in get method reached(".strlen($this->path."?".$data).") : <br />\n".$this->path."?".$data);
  61. }
  62. $header = "GET ".$this->path."?".$data." HTTP/1.1\r\n";
  63. }
  64. else
  65. {
  66. $header = "GET ".$this->path." HTTP/1.1\r\n";
  67. }
  68. $header .= "Host: ".$this->host."\r\n";
  69. $header .= "Connection: close\r\n\r\n";
  70. }
  71. return ($header);
  72. }
  73. function build_data($boundary)
  74. {
  75. $data = "";
  76. foreach($this->data as $index => $value)
  77. {
  78. if ($data == '')
  79. {
  80. $data .= $index . '='. urlencode($value);
  81. }
  82. else
  83. {
  84. $data .= '&'.$index . '='. urlencode($value);
  85. }
  86. }
  87. return ($data);
  88. }
  89. function send()
  90. {
  91. $boundary = $this->determine_boundary();
  92. $data = $this->build_data($boundary);
  93. $header = $this->build_header($boundary, $data);
  94. //debug($header, 'header');
  95. //debug(htmlentities($data), 'data');
  96. $response = $this->connect($header, $data);
  97. if ($response != false)
  98. {
  99. $d['header'] = $response['header'];
  100. $d['data'] = htmlentities($response['data']);
  101. //debug($d, 'RĂ©ponse');
  102. }
  103. return ($response);
  104. }
  105. function connect($header, $data)
  106. {
  107. $res['header'] = "";
  108. $res['data'] = "";
  109. if ($this->is_ssl)
  110. {
  111. $host = 'ssl://'.$this->host . ':'.$this->port;
  112. $socket = fsockopen ('ssl://'.$this->host, $this->port);
  113. }
  114. else
  115. {
  116. $host = $this->host . ':' . $this->port;
  117. $socket = fsockopen ($this->host, $this->port);
  118. }
  119. if ($socket !== false)
  120. {
  121. $data_reached = false;
  122. if (@fputs($socket, $header.$data))
  123. {
  124. while (!feof($socket))
  125. {
  126. $line = fgets($socket);
  127. if (!$data_reached)
  128. {
  129. if ($line == "\r\n")
  130. {
  131. $data_reached = true;
  132. }
  133. else
  134. {
  135. $res['header'] .= $line;
  136. }
  137. }
  138. else
  139. {
  140. $res['data'] .= $line;
  141. }
  142. }
  143. }
  144. else
  145. {
  146. fianet_insert_log("fianet_socket.php - connect() <br />\nEnvoie des données impossible sur : <br />\n".$host);
  147. $res = false;
  148. }
  149. fclose($socket);
  150. }
  151. else
  152. {
  153. fianet_insert_log("fianet_socket.php - connect() <br />\nConnexion socket impossible : <br />\n".$host);
  154. $res = false;
  155. }
  156. return ($res);
  157. }
  158. }