streams/Zotlabs/Lib/ActivityStreams.php

273 lines
5.7 KiB
PHP
Raw Normal View History

2017-07-17 05:28:28 +00:00
<?php
namespace Zotlabs\Lib;
/**
* @brief ActivityStreams class.
*
* Parses an ActivityStream JSON string.
*/
class ActivityStreams {
2017-07-17 05:28:28 +00:00
public $data;
public $valid = false;
public $id = '';
public $type = '';
public $actor = null;
public $obj = null;
public $tgt = null;
public $origin = null;
public $owner = null;
2017-09-12 01:56:17 +00:00
public $signer = null;
public $ldsig = null;
public $sigok = false;
2017-08-28 04:46:10 +00:00
public $recips = null;
2017-09-18 01:40:32 +00:00
public $raw_recips = null;
2017-08-28 04:46:10 +00:00
/**
* @brief Constructor for ActivityStreams.
*
* Takes a JSON string as parameter, decodes it and sets up this object.
*
* @param string $string
*/
2017-07-17 05:28:28 +00:00
function __construct($string) {
$this->data = json_decode($string, true);
2017-07-17 05:28:28 +00:00
if($this->data) {
$this->valid = true;
}
if($this->is_valid()) {
$this->id = $this->get_property_obj('id');
$this->type = $this->get_primary_type();
$this->actor = $this->get_compound_property('actor');
$this->obj = $this->get_compound_property('object');
$this->tgt = $this->get_compound_property('target');
$this->origin = $this->get_compound_property('origin');
2017-08-28 04:46:10 +00:00
$this->recips = $this->collect_recips();
2017-09-12 01:56:17 +00:00
$this->ldsig = $this->get_compound_property('signature');
if($this->ldsig) {
$this->signer = $this->get_compound_property('creator',$this->ldsig);
if($this->signer && $this->signer['publicKey'] && $this->signer['publicKey']['publicKeyPem']) {
$this->sigok = \Zotlabs\Lib\LDSignatures::verify($this->data,$this->signer['publicKey']['publicKeyPem']);
}
}
if(($this->type === 'Note') && (! $this->obj)) {
$this->obj = $this->data;
$this->type = 'Create';
}
2017-07-17 05:28:28 +00:00
}
}
/**
* @brief Return if instantiated ActivityStream is valid.
*
* @return boolean Return true if the JSON string could be decoded.
*/
2017-07-17 05:28:28 +00:00
function is_valid() {
return $this->valid;
}
2017-09-18 01:40:32 +00:00
function set_recips($arr) {
$this->saved_recips = $arr;
}
/**
* @brief Collects all recipients.
*
* @param string $base
* @param string $namespace (optional) default empty
* @return array
*/
function collect_recips($base = '', $namespace = '') {
2017-08-28 04:46:10 +00:00
$x = [];
$fields = [ 'to', 'cc', 'bto', 'bcc', 'audience'];
2017-08-28 04:46:10 +00:00
foreach($fields as $f) {
$y = $this->get_compound_property($f, $base, $namespace);
2017-09-18 01:40:32 +00:00
if($y) {
$x = array_merge($x, $y);
2017-09-18 01:40:32 +00:00
if(! is_array($this->raw_recips))
$this->raw_recips = [];
2017-09-18 01:40:32 +00:00
$this->raw_recips[$f] = $x;
}
}
2017-08-28 04:46:10 +00:00
// not yet ready for prime time
// $x = $this->expand($x,$base,$namespace);
return $x;
}
2017-09-20 23:26:33 +00:00
function expand($arr,$base = '',$namespace = '') {
2017-08-28 04:46:10 +00:00
$ret = [];
// right now use a hardwired recursion depth of 5
for($z = 0; $z < 5; $z ++) {
if(is_array($arr) && $arr) {
foreach($arr as $a) {
if(is_array($a)) {
$ret[] = $a;
}
else {
$x = $this->get_compound_property($a,$base,$namespace);
if($x) {
$ret = array_merge($ret,$x);
}
}
}
}
}
/// @fixme de-duplicate
2017-08-28 04:46:10 +00:00
return $ret;
}
/**
* @brief
*
* @param array $base
* @param string $namespace if not set return empty string
* @return string|NULL
*/
function get_namespace($base, $namespace) {
2017-09-20 23:26:33 +00:00
if(! $namespace)
return '';
$key = null;
foreach( [ $this->data, $base ] as $b ) {
if(! $b)
continue;
if(array_key_exists('@context', $b)) {
if(is_array($b['@context'])) {
foreach($b['@context'] as $ns) {
if(is_array($ns)) {
foreach($ns as $k => $v) {
if($namespace === $v)
$key = $k;
}
}
else {
if($namespace === $ns) {
$key = '';
}
}
}
}
else {
if($namespace === $b['@context']) {
$key = '';
}
}
}
}
return $key;
}
/**
* @brief
*
* @param string $property
* @param array $base (optional)
* @param string $namespace (optional) default empty
* @return NULL|mixed
*/
function get_property_obj($property, $base = '', $namespace = '') {
$prefix = $this->get_namespace($base, $namespace);
if($prefix === null)
return null;
$base = (($base) ? $base : $this->data);
$propname = (($prefix) ? $prefix . ':' : '') . $property;
return ((array_key_exists($propname, $base)) ? $base[$propname] : null);
2017-07-17 05:28:28 +00:00
}
/**
* @brief Fetches a property from an URL.
*
* @param string $url
* @return NULL|mixed
*/
2017-07-17 05:28:28 +00:00
function fetch_property($url) {
$redirects = 0;
if(! check_siteallowed($url)) {
logger('blacklisted: ' . $url);
return null;
}
$x = z_fetch_url($url, true, $redirects,
2017-09-21 06:42:57 +00:00
['headers' => [ 'Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams", application/activity+json' ]]);
2017-07-17 05:28:28 +00:00
if($x['success'])
return json_decode($x['body'], true);
2017-07-17 05:28:28 +00:00
return null;
}
/**
* @brief
*
* @param string $property
* @param array $base
* @param string $namespace (optional) default empty
* @return NULL|mixed
*/
function get_compound_property($property, $base = '', $namespace = '') {
$x = $this->get_property_obj($property, $base, $namespace);
2017-07-17 05:28:28 +00:00
if($this->is_url($x)) {
$x = $this->fetch_property($x);
2017-07-17 05:28:28 +00:00
}
2017-07-17 05:28:28 +00:00
return $x;
}
/**
* @brief Check if string starts with http.
*
* @param string $url
* @return boolean
*/
2017-07-17 05:28:28 +00:00
function is_url($url) {
if(($url) && (! is_array($url)) && (strpos($url, 'http') === 0)) {
2017-07-17 05:28:28 +00:00
return true;
}
2017-07-17 05:28:28 +00:00
return false;
}
/**
* @brief Gets the type property.
*
* @param array $base
* @param string $namespace (optional) default empty
* @return NULL|mixed
*/
function get_primary_type($base = '', $namespace = '') {
2017-07-17 05:28:28 +00:00
if(! $base)
$base = $this->data;
$x = $this->get_property_obj('type', $base, $namespace);
2017-07-17 05:28:28 +00:00
if(is_array($x)) {
foreach($x as $y) {
if(strpos($y, ':') === false) {
2017-07-17 05:28:28 +00:00
return $y;
}
}
}
2017-07-17 05:28:28 +00:00
return $x;
}
function debug() {
$x = var_export($this, true);
2017-07-17 05:28:28 +00:00
return $x;
}
}