aboutsummaryrefslogtreecommitdiffstats
path: root/include/network.php
diff options
context:
space:
mode:
Diffstat (limited to 'include/network.php')
-rw-r--r--include/network.php117
1 files changed, 68 insertions, 49 deletions
diff --git a/include/network.php b/include/network.php
index 84c1c17f8..0ee7f044f 100644
--- a/include/network.php
+++ b/include/network.php
@@ -1,38 +1,43 @@
-<?php /** @file */
-
-
+<?php
+/**
+ * @file include/network.php
+ */
+/**
+ * @brief Returns path to CA file.
+ *
+ * @return string
+ */
function get_capath() {
return appdirpath() . '/library/cacert.pem';
}
/**
- * @function z_fetch_url
+ * @brief fetches an URL.
+ *
* @param string $url
* URL to fetch
- * @param boolean $binary = false
+ * @param boolean $binary default false
* TRUE if asked to return binary results (file download)
- * @param int $redirects = 0
+ * @param int $redirects default 0
* internal use, recursion counter
- * @param array $opts (optional parameters)
- * 'accept_content' => supply Accept: header with 'accept_content' as the value
- * 'timeout' => int seconds, default system config value or 60 seconds
- * 'http_auth' => username:password
- * 'novalidate' => do not validate SSL certs, default is to validate using our CA list
- *
- * @returns array
- * 'return_code' => HTTP return code or 0 if timeout or failure
- * 'success' => boolean true (if HTTP 2xx result) or false
- * 'header' => HTTP headers
- * 'body' => fetched content
+ * @param array $opts (optional parameters) assoziative array with:
+ * * \b accept_content => supply Accept: header with 'accept_content' as the value
+ * * \b timeout => int seconds, default system config value or 60 seconds
+ * * \b http_auth => username:password
+ * * \b novalidate => do not validate SSL certs, default is to validate using our CA list
+ * * \b nobody => only return the header
+ *
+ * @return array an assoziative array with:
+ * * \e int \b return_code => HTTP return code or 0 if timeout or failure
+ * * \e boolean \b success => boolean true (if HTTP 2xx result) or false
+ * * \e string \b header => HTTP headers
+ * * \e string \b body => fetched content
*/
-
function z_fetch_url($url, $binary = false, $redirects = 0, $opts = array()) {
$ret = array('return_code' => 0, 'success' => false, 'header' => "", 'body' => "");
- $a = get_app();
-
$ch = @curl_init($url);
if(($redirects > 8) || (! $ch))
return false;
@@ -51,6 +56,9 @@ function z_fetch_url($url, $binary = false, $redirects = 0, $opts = array()) {
if(x($opts,'headers'))
@curl_setopt($ch, CURLOPT_HTTPHEADER, $opts['headers']);
+ if(x($opts,'nobody'))
+ @curl_setopt($ch, CURLOPT_NOBODY, $opts['nobody']);
+
if(x($opts,'timeout') && intval($opts['timeout'])) {
@curl_setopt($ch, CURLOPT_TIMEOUT, $opts['timeout']);
}
@@ -67,7 +75,6 @@ function z_fetch_url($url, $binary = false, $redirects = 0, $opts = array()) {
@curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,
((x($opts,'novalidate') && intval($opts['novalidate'])) ? false : true));
-
$prx = get_config('system','proxy');
if(strlen($prx)) {
@curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
@@ -109,7 +116,7 @@ function z_fetch_url($url, $binary = false, $redirects = 0, $opts = array()) {
$url_parsed = @parse_url($newurl);
if (isset($url_parsed)) {
@curl_close($ch);
- return z_fetch_url($newurl,$binary,$redirects++,$opts);
+ return z_fetch_url($newurl,$binary,++$redirects,$opts);
}
}
@@ -128,14 +135,15 @@ function z_fetch_url($url, $binary = false, $redirects = 0, $opts = array()) {
if(x($opts,'debug')) {
$ret['debug'] = $curl_info;
}
-
+
@curl_close($ch);
return($ret);
}
/**
- * @function z_post_url
+ * @brief
+ *
* @param string $url
* URL to post
* @param mixed $params
@@ -150,17 +158,15 @@ function z_fetch_url($url, $binary = false, $redirects = 0, $opts = array()) {
* 'timeout' => int seconds, default system config value or 60 seconds
* 'http_auth' => username:password
* 'novalidate' => do not validate SSL certs, default is to validate using our CA list
- *
- * @returns array
- * 'return_code' => HTTP return code or 0 if timeout or failure
- * 'success' => boolean true (if HTTP 2xx result) or false
- * 'header' => HTTP headers
- * 'body' => fetched content
+ * @return array an assoziative array with:
+ * * \e int \b return_code => HTTP return code or 0 if timeout or failure
+ * * \e boolean \b success => boolean true (if HTTP 2xx result) or false
+ * * \e string \b header => HTTP headers
+ * * \e string \b body => content
+ * * \e string \b debug => from curl_info()
*/
-
-
function z_post_url($url,$params, $redirects = 0, $opts = array()) {
-
+
$ret = array('return_code' => 0, 'success' => false, 'header' => "", 'body' => "");
$ch = curl_init($url);
@@ -182,6 +188,9 @@ function z_post_url($url,$params, $redirects = 0, $opts = array()) {
if(x($opts,'headers'))
@curl_setopt($ch, CURLOPT_HTTPHEADER, $opts['headers']);
+ if(x($opts,'nobody'))
+ @curl_setopt($ch, CURLOPT_NOBODY, $opts['nobody']);
+
if(x($opts,'timeout') && intval($opts['timeout'])) {
@curl_setopt($ch, CURLOPT_TIMEOUT, $opts['timeout']);
}
@@ -239,7 +248,7 @@ function z_post_url($url,$params, $redirects = 0, $opts = array()) {
if($http_code == 303) {
return z_fetch_url($newurl,false,$redirects++,$opts);
} else {
- return z_post_url($newurl,$params,$redirects++,$opts);
+ return z_post_url($newurl,$params,++$redirects,$opts);
}
}
}
@@ -253,24 +262,35 @@ function z_post_url($url,$params, $redirects = 0, $opts = array()) {
logger('z_post_url: debug: ' . print_r($curl_info,true), LOGGER_DATA);
}
- $ret['body'] = substr($s,strlen($header));
+ $ret['body'] = substr($s, strlen($header));
$ret['header'] = $header;
if(x($opts,'debug')) {
$ret['debug'] = $curl_info;
}
-
curl_close($ch);
return($ret);
}
+/**
+ * @brief Like z_post_url() but with an application/json HTTP header.
+ *
+ * Add a "Content-Type: application/json" HTTP-header to $opts and call z_post_url().
+ *
+ * @see z_post_url()
+ *
+ * @param string $url
+ * @param array $params
+ * @param number $redirects default 0
+ * @param array $opts (optional) curl options
+ * @return z_post_url()
+ */
+function z_post_url_json($url, $params, $redirects = 0, $opts = array()) {
-function z_post_url_json($url,$params,$redirects = 0, $opts = array()) {
+ $opts = array_merge($opts, array('headers' => array('Content-Type: application/json')));
- $opts = array_merge($opts,array('headers' => array('Content-Type: application/json')));
return z_post_url($url,json_encode($params),$redirects,$opts);
-
}
@@ -301,22 +321,19 @@ function xml_status($st, $message = '') {
}
/**
- * @function http_status_exit
- *
- * Send HTTP status header and exit
+ * @brief Send HTTP status header and exit.
+ *
* @param int $val
* integer HTTP status result value
* @param string $msg
* optional message
* @returns (does not return, process is terminated)
*/
+function http_status_exit($val, $msg = '') {
-function http_status_exit($val,$msg = '') {
-
- $err = '';
- if($val >= 400)
+ if ($val >= 400)
$msg = (($msg) ? $msg : 'Error');
- if($val >= 200 && $val < 300)
+ if ($val >= 200 && $val < 300)
$msg = (($msg) ? $msg : 'OK');
logger('http_status_exit ' . $val . ' ' . $msg);
@@ -511,6 +528,7 @@ function allowed_email($email) {
function avatar_img($email) {
+ $avatar = array();
$a = get_app();
$avatar['size'] = 175;
@@ -520,10 +538,11 @@ function avatar_img($email) {
call_hooks('avatar_lookup', $avatar);
- if(! $avatar['success'])
+ if (! $avatar['success'])
$avatar['url'] = $a->get_baseurl() . '/' . get_default_profile_photo();
logger('Avatar: ' . $avatar['email'] . ' ' . $avatar['url'], LOGGER_DEBUG);
+
return $avatar['url'];
}
@@ -1530,7 +1549,7 @@ function service_plink($contact, $guid) {
$m = parse_url($contact['xchan_url']);
if($m) {
- $url = $scheme . '://' . $m['host'] . (($m['port']) ? ':' . $m['port'] : '');
+ $url = $m['scheme'] . '://' . $m['host'] . (($m['port']) ? ':' . $m['port'] : '');
}
else
$url = 'https://' . substr($contact['xchan_addr'],strpos($contact['xchan_addr'],'@')+1);