HTTPHelper.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC;
  25. use OCP\Http\Client\IClientService;
  26. use OCP\IConfig;
  27. /**
  28. * Class HTTPHelper
  29. *
  30. * @package OC
  31. * @deprecated Use \OCP\Http\Client\IClientService
  32. */
  33. class HTTPHelper {
  34. const USER_AGENT = 'ownCloud Server Crawler';
  35. /** @var \OCP\IConfig */
  36. private $config;
  37. /** @var IClientService */
  38. private $clientService;
  39. /**
  40. * @param IConfig $config
  41. * @param IClientService $clientService
  42. */
  43. public function __construct(IConfig $config,
  44. IClientService $clientService) {
  45. $this->config = $config;
  46. $this->clientService = $clientService;
  47. }
  48. /**
  49. * Get URL content
  50. * @param string $url Url to get content
  51. * @throws \Exception If the URL does not start with http:// or https://
  52. * @return string of the response or false on error
  53. * This function get the content of a page via curl, if curl is enabled.
  54. * If not, file_get_contents is used.
  55. * @deprecated Use \OCP\Http\Client\IClientService
  56. */
  57. public function getUrlContent($url) {
  58. try {
  59. $client = $this->clientService->newClient();
  60. $response = $client->get($url);
  61. return $response->getBody();
  62. } catch (\Exception $e) {
  63. return false;
  64. }
  65. }
  66. /**
  67. * Returns the response headers of a HTTP URL without following redirects
  68. * @param string $location Needs to be a HTTPS or HTTP URL
  69. * @return array
  70. * @deprecated Use \OCP\Http\Client\IClientService
  71. */
  72. public function getHeaders($location) {
  73. $client = $this->clientService->newClient();
  74. $response = $client->get($location);
  75. return $response->getHeaders();
  76. }
  77. /**
  78. * Checks whether the supplied URL begins with HTTPS:// or HTTP:// (case insensitive)
  79. * @param string $url
  80. * @return bool
  81. */
  82. public function isHTTPURL($url) {
  83. return stripos($url, 'https://') === 0 || stripos($url, 'http://') === 0;
  84. }
  85. /**
  86. * send http post request
  87. *
  88. * @param string $url
  89. * @param array $fields data send by the request
  90. * @return array
  91. * @deprecated Use \OCP\Http\Client\IClientService
  92. */
  93. public function post($url, array $fields) {
  94. $client = $this->clientService->newClient();
  95. try {
  96. $response = $client->post(
  97. $url,
  98. [
  99. 'body' => $fields,
  100. 'connect_timeout' => 10,
  101. ]
  102. );
  103. } catch (\Exception $e) {
  104. return ['success' => false, 'result' => $e->getMessage()];
  105. }
  106. return ['success' => true, 'result' => $response->getBody()];
  107. }
  108. }