From c1e4f4c5338be96bc060a05d2a68f8e76012a3b7 Mon Sep 17 00:00:00 2001 From: Garvin Hicking Date: Thu, 22 Sep 2016 12:35:48 +0200 Subject: [PATCH 1/5] 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 From 80f3b39502549328c738a872c33e58bb8126f807 Mon Sep 17 00:00:00 2001 From: Garvin Hicking Date: Thu, 22 Sep 2016 14:26:59 +0200 Subject: [PATCH 2/5] forward compatibility to serendipity_request_url --- include/functions.inc.php | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/include/functions.inc.php b/include/functions.inc.php index a1710fb1..12dc33d6 100644 --- a/include/functions.inc.php +++ b/include/functions.inc.php @@ -1125,13 +1125,23 @@ function serendipity_request_end() { * @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) + * @param $extra_options array Extra options + * @param $addData string possible extra event addData declaration for 'backend_http_request' hook * @return $content string The URL contents */ -function serendipity_request_url($uri, $method = 'GET', $contenttype = null, $data = null) { +function serendipity_request_url($uri, $method = 'GET', $contenttype = null, $data = null, $extra_options = null, $addData = null) { + global $serendipity; + 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'); + + if (is_array($extra_options)) { + foreach($extra_options AS $okey => $oval) { + $options[$okey] = $oval; + } + } + serendipity_plugin_api::hook_event('backend_http_request', $options, $addData); 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 @@ -1192,6 +1202,17 @@ function serendipity_request_url($uri, $method = 'GET', $contenttype = null, $da $fContent = $res->getBody(); + $serendipity['last_http_request'] = array( + 'responseCode' => $res->getStatus(), + 'effectiveUrl' => $res->getEffectiveUrl(), + 'reasonPhrase' => $res->getReasonPhrase(), + 'isRedirect' => $res->isRedirect(), + 'cookies' => $res->getCookies(), + 'version' => $res->getVersion(), + + 'object' => $res // forward compatibility for possible other checks + ); + serendipity_request_end(); return $fContent; } From 20ade83792ffe6f04e8a7381a4f3dfd1dc605141 Mon Sep 17 00:00:00 2001 From: Garvin Hicking Date: Thu, 22 Sep 2016 14:33:52 +0200 Subject: [PATCH 3/5] allow arrays --- include/functions.inc.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/functions.inc.php b/include/functions.inc.php index 12dc33d6..c682bde1 100644 --- a/include/functions.inc.php +++ b/include/functions.inc.php @@ -1124,7 +1124,7 @@ function serendipity_request_end() { * @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) + * @param $contenttype mixed optional extra data (i.e. POST body), can be an array * @param $extra_options array Extra options * @param $addData string possible extra event addData declaration for 'backend_http_request' hook * @return $content string The URL contents @@ -1190,7 +1190,11 @@ function serendipity_request_url($uri, $method = 'GET', $contenttype = null, $da } if ($data != null) { - $req->setBody($data); + if (is_array($data)) { + $req->addPostParameter($data); + } else { + $req->setBody($data); + } } try { From d60a7da9c316455412621c9a88ffe04a6416cf16 Mon Sep 17 00:00:00 2001 From: Garvin Hicking Date: Thu, 22 Sep 2016 15:34:27 +0200 Subject: [PATCH 4/5] support http auth --- include/functions.inc.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/include/functions.inc.php b/include/functions.inc.php index c682bde1..7f4d2ba4 100644 --- a/include/functions.inc.php +++ b/include/functions.inc.php @@ -1125,12 +1125,13 @@ function serendipity_request_end() { * @param $method string HTTP method (GET/POST/PUT/OPTIONS...) * @param $contenttype string optional HTTP content type * @param $contenttype mixed optional extra data (i.e. POST body), can be an array - * @param $extra_options array Extra options + * @param $extra_options array Extra options for HTTP_Request $options array (can override defaults) * @param $addData string possible extra event addData declaration for 'backend_http_request' hook + * @param $auth array Array with 'user' and 'pass' for HTTP Auth * @return $content string The URL contents */ -function serendipity_request_url($uri, $method = 'GET', $contenttype = null, $data = null, $extra_options = null, $addData = null) { +function serendipity_request_url($uri, $method = 'GET', $contenttype = null, $data = null, $extra_options = null, $addData = null, $auth = null) { global $serendipity; require_once S9Y_PEAR_PATH . 'HTTP/Request2.php'; @@ -1185,9 +1186,13 @@ function serendipity_request_url($uri, $method = 'GET', $contenttype = null, $da } $req = new HTTP_Request2($uri, $http_method, $options); - if (isset($contenttype)) { + if (isset($contenttype) && $contenttype !== null) { $req->setHeader('Content-Type', $contenttype); } + + if (is_array($auth)) { + $req->setAuth($auth['user'], $auth['pass']); + } if ($data != null) { if (is_array($data)) { From 4aaa9845eb24766c8afe08b7531a0a030622b224 Mon Sep 17 00:00:00 2001 From: Garvin Hicking Date: Thu, 22 Sep 2016 15:38:12 +0200 Subject: [PATCH 5/5] Add header API --- include/functions.inc.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/functions.inc.php b/include/functions.inc.php index 7f4d2ba4..c274cae9 100644 --- a/include/functions.inc.php +++ b/include/functions.inc.php @@ -1217,7 +1217,8 @@ function serendipity_request_url($uri, $method = 'GET', $contenttype = null, $da 'reasonPhrase' => $res->getReasonPhrase(), 'isRedirect' => $res->isRedirect(), 'cookies' => $res->getCookies(), - 'version' => $res->getVersion(), + 'version' => $res->getVersion(), + 'header' => $res->getHeader(), 'object' => $res // forward compatibility for possible other checks );