From c1e4f4c5338be96bc060a05d2a68f8e76012a3b7 Mon Sep 17 00:00:00 2001 From: Garvin Hicking Date: Thu, 22 Sep 2016 12:35:48 +0200 Subject: [PATCH] Add serendipity_request_url() --- docs/NEWS | 4 ++ include/functions.inc.php | 77 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/docs/NEWS b/docs/NEWS index b627d55e..7bb70546 100644 --- a/docs/NEWS +++ b/docs/NEWS @@ -1,6 +1,10 @@ Version 2.1-beta1 (June 8th, 2016) ------------------------------------------------------------------------ + * Added new API wrapper serendipity_request_url() to request URLs. + Currently uses HTTP_Request2, might change to curl or others in + the future, but irrelevant to plugins using this function. + * Removed outdated themes blue, carl_contest, kubrick and wp. They live on Spartacus now. diff --git a/include/functions.inc.php b/include/functions.inc.php index bc2e55d2..a1710fb1 100644 --- a/include/functions.inc.php +++ b/include/functions.inc.php @@ -1120,6 +1120,83 @@ function serendipity_request_end() { return true; } +/* Request the contents of an URL, API wrapper + * @param $uri string The URL to fetch + * @param $method string HTTP method (GET/POST/PUT/OPTIONS...) + * @param $contenttype string optional HTTP content type + * @param $contenttype string optional extra data (i.e. POST body) + * @return $content string The URL contents + */ + +function serendipity_request_url($uri, $method = 'GET', $contenttype = null, $data = null) { + require_once S9Y_PEAR_PATH . 'HTTP/Request2.php'; + $options = array('follow_redirects' => true, 'max_redirects' => 5); + serendipity_plugin_api::hook_event('backend_http_request', $options, 'trackback_send'); + serendipity_request_start(); + if (version_compare(PHP_VERSION, '5.6.0', '<')) { + // On earlier PHP versions, the certificate validation fails. We deactivate it on them to restore the functionality we had with HTTP/Request1 + $options['ssl_verify_peer'] = false; + } + + switch(strtoupper($method)) { + case 'GET': + $http_method = HTTP_Request2::METHOD_GET; + break; + + case 'PUT': + $http_method = HTTP_Request2::METHOD_PUT; + break; + + case 'OPTIONS': + $http_method = HTTP_Request2::METHOD_OPTIONS; + break; + + case 'HEAD': + $http_method = HTTP_Request2::METHOD_HEAD; + break; + + case 'DELETE': + $http_method = HTTP_Request2::METHOD_DELETE; + break; + + case 'TRACE': + $http_method = HTTP_Request2::METHOD_TRACE; + break; + + case 'CONNECT': + $http_method = HTTP_Request2::METHOD_CONNECT; + break; + + default: + case 'POST': + $http_method = HTTP_Request2::METHOD_POST; + break; + + } + + $req = new HTTP_Request2($uri, $http_method, $options); + if (isset($contenttype)) { + $req->setHeader('Content-Type', $contenttype); + } + + if ($data != null) { + $req->setBody($data); + } + + try { + $res = $req->send(); + } catch (HTTP_Request2_Exception $e) { + serendipity_request_end(); + return false; + } + + + $fContent = $res->getBody(); + serendipity_request_end(); + return $fContent; +} + + if (!function_exists('microtime_float')) { /** * Get current timestamp as microseconds