HttpRequest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Hold the PhpMyAdmin\Utils\HttpRequest class
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PhpMyAdmin\Utils;
  9. /**
  10. * Handles HTTP requests
  11. *
  12. * @package PhpMyAdmin
  13. */
  14. class HttpRequest
  15. {
  16. private $proxyUrl;
  17. private $proxyUser;
  18. private $proxyPass;
  19. /**
  20. * Constructor
  21. */
  22. public function __construct()
  23. {
  24. global $cfg;
  25. $this->proxyUrl = $cfg['ProxyUrl'];
  26. $this->proxyUser = $cfg['ProxyUser'];
  27. $this->proxyPass = $cfg['ProxyPass'];
  28. }
  29. /**
  30. * Returns information with regards to handling the http request
  31. *
  32. * @param array $context Data about the context for which
  33. * to http request is sent
  34. *
  35. * @return array of updated context information
  36. */
  37. private function handleContext(array $context)
  38. {
  39. if (strlen($this->proxyUrl) > 0) {
  40. $context['http'] = array(
  41. 'proxy' => $this->proxyUrl,
  42. 'request_fulluri' => true
  43. );
  44. if (strlen($this->proxyUser) > 0) {
  45. $auth = base64_encode(
  46. $this->proxyUser . ':' . $this->proxyPass
  47. );
  48. $context['http']['header'] .= 'Proxy-Authorization: Basic '
  49. . $auth . "\r\n";
  50. }
  51. }
  52. return $context;
  53. }
  54. /**
  55. * Creates HTTP request using curl
  56. *
  57. * @param mixed $response HTTP response
  58. * @param int $httpStatus HTTP response status code
  59. * @param bool $returnOnlyStatus If set to true, the method would only return response status
  60. *
  61. * @return string|null|bool
  62. */
  63. private function response(
  64. $response,
  65. $httpStatus,
  66. $returnOnlyStatus
  67. ) {
  68. if ($httpStatus == 404) {
  69. return false;
  70. }
  71. if ($httpStatus != 200) {
  72. return null;
  73. }
  74. if ($returnOnlyStatus) {
  75. return true;
  76. }
  77. return $response;
  78. }
  79. /**
  80. * Creates HTTP request using curl
  81. *
  82. * @param string $url Url to send the request
  83. * @param string $method HTTP request method (GET, POST, PUT, DELETE, etc)
  84. * @param bool $returnOnlyStatus If set to true, the method would only return response status
  85. * @param mixed $content Content to be sent with HTTP request
  86. * @param string $header Header to be set for the HTTP request
  87. * @param int $ssl SSL mode to use
  88. *
  89. * @return string|null|bool
  90. */
  91. private function curl(
  92. $url,
  93. $method,
  94. $returnOnlyStatus = false,
  95. $content = null,
  96. $header = '',
  97. $ssl = 0
  98. ) {
  99. $curlHandle = curl_init($url);
  100. if ($curlHandle === false) {
  101. return null;
  102. }
  103. $curlStatus = true;
  104. if (strlen($this->proxyUrl) > 0) {
  105. $curlStatus &= curl_setopt($curlHandle, CURLOPT_PROXY, $this->proxyUrl);
  106. if (strlen($this->proxyUser) > 0) {
  107. $curlStatus &= curl_setopt(
  108. $curlHandle,
  109. CURLOPT_PROXYUSERPWD,
  110. $this->proxyUser . ':' . $this->proxyPass
  111. );
  112. }
  113. }
  114. $curlStatus &= curl_setopt($curlHandle, CURLOPT_USERAGENT, 'phpMyAdmin');
  115. if ($method != "GET") {
  116. $curlStatus &= curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, $method);
  117. }
  118. if ($header) {
  119. $curlStatus &= curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array($header));
  120. }
  121. if ($method == "POST") {
  122. $curlStatus &= curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $content);
  123. }
  124. $curlStatus &= curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST, '2');
  125. $curlStatus &= curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, '1');
  126. /**
  127. * Configure ISRG Root X1 to be able to verify Let's Encrypt SSL
  128. * certificates even without properly configured curl in PHP.
  129. *
  130. * See https://letsencrypt.org/certificates/
  131. */
  132. $certsDir = dirname(__file__) . '/../../certs/';
  133. /* See code below for logic */
  134. if ($ssl == CURLOPT_CAPATH) {
  135. $curlStatus &= curl_setopt($curlHandle, CURLOPT_CAPATH, $certsDir);
  136. } elseif ($ssl == CURLOPT_CAINFO) {
  137. $curlStatus &= curl_setopt($curlHandle, CURLOPT_CAINFO, $certsDir . 'cacert.pem');
  138. }
  139. $curlStatus &= curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
  140. $curlStatus &= curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, 0);
  141. $curlStatus &= curl_setopt($curlHandle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  142. $curlStatus &= curl_setopt($curlHandle, CURLOPT_TIMEOUT, 10);
  143. $curlStatus &= curl_setopt($curlHandle, CURLOPT_CONNECTTIMEOUT, 10);
  144. if (! $curlStatus) {
  145. return null;
  146. }
  147. $response = @curl_exec($curlHandle);
  148. if ($response === false) {
  149. /*
  150. * In case of SSL verification failure let's try configuring curl
  151. * certificate verification. Unfortunately it is tricky as setting
  152. * options incompatible with PHP build settings can lead to failure.
  153. *
  154. * So let's rather try the options one by one.
  155. *
  156. * 1. Try using system SSL storage.
  157. * 2. Try setting CURLOPT_CAINFO.
  158. * 3. Try setting CURLOPT_CAPATH.
  159. * 4. Fail.
  160. */
  161. if (curl_getinfo($curlHandle, CURLINFO_SSL_VERIFYRESULT) != 0) {
  162. if ($ssl == 0) {
  163. $this->curl($url, $method, $returnOnlyStatus, $content, $header, CURLOPT_CAINFO);
  164. } elseif ($ssl == CURLOPT_CAINFO) {
  165. $this->curl($url, $method, $returnOnlyStatus, $content, $header, CURLOPT_CAPATH);
  166. }
  167. }
  168. return null;
  169. }
  170. $httpStatus = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
  171. return $this->response($response, $httpStatus, $returnOnlyStatus);
  172. }
  173. /**
  174. * Creates HTTP request using file_get_contents
  175. *
  176. * @param string $url Url to send the request
  177. * @param string $method HTTP request method (GET, POST, PUT, DELETE, etc)
  178. * @param bool $returnOnlyStatus If set to true, the method would only return response status
  179. * @param mixed $content Content to be sent with HTTP request
  180. * @param string $header Header to be set for the HTTP request
  181. *
  182. * @return string|null|bool
  183. */
  184. private function fopen(
  185. $url,
  186. $method,
  187. $returnOnlyStatus = false,
  188. $content = null,
  189. $header = ''
  190. ) {
  191. $context = array(
  192. 'http' => array(
  193. 'method' => $method,
  194. 'request_fulluri' => true,
  195. 'timeout' => 10,
  196. 'user_agent' => 'phpMyAdmin',
  197. 'header' => "Accept: */*",
  198. )
  199. );
  200. if ($header) {
  201. $context['http']['header'] .= "\n" . $header;
  202. }
  203. if ($method == "POST") {
  204. $context['http']['content'] = $content;
  205. }
  206. $context = $this->handleContext($context);
  207. $response = @file_get_contents(
  208. $url,
  209. false,
  210. stream_context_create($context)
  211. );
  212. if (isset($http_response_header)) {
  213. preg_match("#HTTP/[0-9\.]+\s+([0-9]+)#", $http_response_header[0], $out);
  214. $httpStatus = intval($out[1]);
  215. return $this->response($response, $httpStatus, $returnOnlyStatus);
  216. }
  217. return null;
  218. }
  219. /**
  220. * Creates HTTP request
  221. *
  222. * @param string $url Url to send the request
  223. * @param string $method HTTP request method (GET, POST, PUT, DELETE, etc)
  224. * @param bool $returnOnlyStatus If set to true, the method would only return response status
  225. * @param mixed $content Content to be sent with HTTP request
  226. * @param string $header Header to be set for the HTTP request
  227. *
  228. * @return string|null|bool
  229. */
  230. public function create(
  231. $url,
  232. $method,
  233. $returnOnlyStatus = false,
  234. $content = null,
  235. $header = ''
  236. ) {
  237. if (function_exists('curl_init')) {
  238. return $this->curl($url, $method, $returnOnlyStatus, $content, $header);
  239. } elseif (ini_get('allow_url_fopen')) {
  240. return $this->fopen($url, $method, $returnOnlyStatus, $content, $header);
  241. }
  242. return null;
  243. }
  244. }