aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/network.php90
-rw-r--r--include/zot.php50
-rw-r--r--mod/post.php11
3 files changed, 147 insertions, 4 deletions
diff --git a/include/network.php b/include/network.php
index 6b9557234..e987c5fb4 100644
--- a/include/network.php
+++ b/include/network.php
@@ -182,6 +182,96 @@ function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0)
return($body);
}}
+if(! function_exists('z_fetch_url')) {
+function z_fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_content=Null) {
+
+ $ret = array('return_code' => 0, 'success' => false, 'header' => "", 'body' => "");
+
+ $a = get_app();
+
+ $ch = @curl_init($url);
+ if(($redirects > 8) || (! $ch))
+ return false;
+
+ @curl_setopt($ch, CURLOPT_HEADER, true);
+
+ if (!is_null($accept_content)){
+ curl_setopt($ch,CURLOPT_HTTPHEADER, array (
+ "Accept: " . $accept_content
+ ));
+ }
+
+ @curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
+ @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Friendica)");
+
+
+ if(intval($timeout)) {
+ @curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
+ }
+ else {
+ $curl_time = intval(get_config('system','curl_timeout'));
+ @curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
+ }
+ // by default we will allow self-signed certs
+ // but you can override this
+
+ $check_cert = get_config('system','verifyssl');
+ @curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
+
+ $prx = get_config('system','proxy');
+ if(strlen($prx)) {
+ @curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
+ @curl_setopt($ch, CURLOPT_PROXY, $prx);
+ $prxusr = @get_config('system','proxyuser');
+ if(strlen($prxusr))
+ @curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
+ }
+ if($binary)
+ @curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
+
+
+ // don't let curl abort the entire application
+ // if it throws any errors.
+
+ $s = @curl_exec($ch);
+
+ $base = $s;
+ $curl_info = @curl_getinfo($ch);
+ $http_code = $curl_info['http_code'];
+// logger('fetch_url:' . $http_code . ' data: ' . $s);
+ $header = '';
+
+ // Pull out multiple headers, e.g. proxy and continuation headers
+ // allow for HTTP/2.x without fixing code
+
+ while(preg_match('/^HTTP\/[1-2].+? [1-5][0-9][0-9]/',$base)) {
+ $chunk = substr($base,0,strpos($base,"\r\n\r\n")+4);
+ $header .= $chunk;
+ $base = substr($base,strlen($chunk));
+ }
+
+ if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) {
+ $matches = array();
+ preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
+ $newurl = trim(array_pop($matches));
+ if(strpos($newurl,'/') === 0)
+ $newurl = $url . $newurl;
+ $url_parsed = @parse_url($newurl);
+ if (isset($url_parsed)) {
+ $redirects++;
+ @curl_close($ch);
+ return z_fetch_url($newurl,$binary,$redirects,$timeout,$accpt_content);
+ }
+ }
+
+ $rc = intval($http_code);
+ $ret['return_code'] = $rc;
+ $ret['success'] = (($rc >= 200 && $rc <= 299) ? true : false);
+ $ret['body'] = substr($s,strlen($header));
+ $ret['header'] = $header;
+ @curl_close($ch);
+ return($ret);
+}}
diff --git a/include/zot.php b/include/zot.php
index f978971ea..dd5d39ef2 100644
--- a/include/zot.php
+++ b/include/zot.php
@@ -22,7 +22,7 @@ function zot_new_uid($entity_id) {
*
*/
-function zot_get_hubloc($arr,$primary) {
+function zot_get_hubloc($arr,$primary = false) {
$tmp = '';
@@ -38,7 +38,7 @@ function zot_get_hubloc($arr,$primary) {
return array();
$sql_extra = (($primary) ? " and hubloc_primary = 1 " : "" );
- return q("select * from hubloc where hubloc_zuid in ( $tmp ) $sql_extra order by hubloc_url");
+ return q("select * from hubloc where hubloc_guid in ( $tmp ) $sql_extra order by hubloc_url");
}
@@ -60,11 +60,53 @@ function zot_verify(&$item,$identity) {
function zot_notify($entity,$url) {
$x = z_post_url($url, array(
'type' => 'notify',
- 'guid' => $entity['entity_global_id'],
- 'callback' => z_root() . '/post',
+ 'guid' => $entity['entity_global_id'],
+ 'hub' => z_root(),
+ 'callback' => '/post',
'spec' => ZOT_REVISION)
);
return($x);
}
+function zot_gethub($arr) {
+
+ if((x($arr,'hub')) && (x($arr,'guid'))) {
+ $r = q("select * from hubloc
+ where hubloc_guid = '%s' and hubloc_url = '%s'
+ limit 1",
+ dbesc($arr['guid']),
+ dbesc($arr['hub'])
+ );
+ if($r && count($r))
+ return $r[0];
+ }
+ return null;
+}
+
+function zot_register_hub($arr) {
+ if((x($arr,'hub')) && (x($arr,'guid'))) {
+ $x = z_fetch_url($arr['hub'] . '/.well-known/zot-guid/' . $arr['guid']);
+ if($x['success']) {
+ $record = json_decode($x['body']);
+ if($record->guid === $arr['guid'] && $record->url === $arr['hub']) {
+ $r = q("insert into hubloc (hubloc_guid, hubloc_primary, hubloc_url,
+ hubloc_callback, hubloc_sitekey, hubloc_key)
+ values ( '%s', %d, '%s', '%s', '%s', '%s' )",
+ dbesc($arr['guid']),
+ intval($record->primary),
+ dbesc($record->url),
+ dbesc($record->callback),
+ dbesc($record->sitekey),
+ dbesc($record->key)
+ );
+
+ // return the discovery record so we can further process
+
+ if($r)
+ return $record;
+ }
+ }
+ }
+ return false;
+} \ No newline at end of file
diff --git a/mod/post.php b/mod/post.php
index c5430d46e..c44e6114d 100644
--- a/mod/post.php
+++ b/mod/post.php
@@ -15,6 +15,17 @@ function post_post(&$a) {
if($msgtype === 'notify') {
+ $hub = zot_gethub($_REQUEST['guid']);
+ if(! $hub) {
+ $result = zot_register_hub($_REQUEST);
+ if(! $result) {
+ $ret['message'] = 'Hub not available.';
+ json_return_and_dir($ret);
+ }
+ }
+ // check which hub is primary and take action if mismatched
+
+
// add to receive queue
// qreceive_add($_REQUEST);