752 lines
16 KiB
PHP
752 lines
16 KiB
PHP
<?php
|
|
namespace ContentNation\ActivityPub\Common;
|
|
|
|
class APObject {
|
|
// actor | bto | current | first | id | instrument | last | items | oneOf |
|
|
// anyOf | closed | origin | next | object | prev | result | target |
|
|
// type | accuracy | altitude | content | duration | height | href | hreflang |
|
|
// partOf | latitude | longitude | endTime | radius | rel |
|
|
// startIndex | totalItems | units | width | subject | relationship | describes |
|
|
// formerType | deleted
|
|
/**
|
|
* unique id
|
|
* @var string
|
|
*/
|
|
private $id = '';
|
|
|
|
/**
|
|
* child object
|
|
* @var APObject|null
|
|
*/
|
|
private $object = null;
|
|
|
|
/**
|
|
* type
|
|
* @var string
|
|
*/
|
|
private $type = 'Object';
|
|
|
|
/**
|
|
* content
|
|
* @var string
|
|
*/
|
|
private $content = '';
|
|
|
|
/**
|
|
* duration in seconds
|
|
* @var \DateInterval|false
|
|
*/
|
|
private $duration = false;
|
|
|
|
/**
|
|
* height
|
|
* @var int
|
|
*/
|
|
private $height = -1;
|
|
|
|
/**
|
|
* href
|
|
* @var string
|
|
*/
|
|
private $href = '';
|
|
|
|
/**
|
|
* end time
|
|
* @var int
|
|
*/
|
|
private $endTime = 0;
|
|
|
|
/**
|
|
* width
|
|
* @var int
|
|
*/
|
|
private $width = -1;
|
|
|
|
|
|
// fiels are attachment | attributedTo | audience | content | context | name | endTime | generator |
|
|
// icon | image | inReplyTo | location | preview | published | replies | startTime | summary | tag |
|
|
// updated | url | to | bto | cc | bcc | mediaType | duration
|
|
|
|
/**
|
|
* attachements
|
|
* @var APObject[]
|
|
*/
|
|
private $attachment = [];
|
|
|
|
/**
|
|
* attributed to
|
|
* @var string
|
|
*/
|
|
private $attributedTo = '';
|
|
|
|
// audience
|
|
// content
|
|
// context
|
|
/**
|
|
* name
|
|
* @var string
|
|
*/
|
|
private $name = '';
|
|
// endTime
|
|
// generator
|
|
/**
|
|
* images
|
|
* @var Image[]
|
|
*/
|
|
private $icon = array();
|
|
|
|
/**
|
|
* images
|
|
* @var Image[]
|
|
*/
|
|
private $image = array();
|
|
|
|
/**
|
|
* reply id
|
|
*
|
|
* @var string
|
|
*/
|
|
private $inReplyTo = "";
|
|
|
|
// location
|
|
// preview
|
|
|
|
/**
|
|
* published timestamp
|
|
* @var int
|
|
*/
|
|
private $published = 0;
|
|
|
|
// startTime
|
|
/**
|
|
* summary
|
|
* @var string
|
|
*/
|
|
private $summary = "";
|
|
|
|
/**
|
|
* tags
|
|
* @var Tag[]
|
|
*/
|
|
private $tag = array();
|
|
|
|
/**
|
|
* updated timestamp
|
|
* @var int
|
|
*/
|
|
private $updated = 0;
|
|
/**
|
|
* url
|
|
* @var string
|
|
*/
|
|
private $url = "";
|
|
|
|
/**
|
|
* list of to ids
|
|
* @var array<string>
|
|
*/
|
|
private $to = array();
|
|
|
|
// bto
|
|
/**
|
|
* list of cc ids
|
|
* @var array<string>
|
|
*/
|
|
private $cc = array();
|
|
|
|
// bcc
|
|
/**
|
|
* media type
|
|
* @var string
|
|
*/
|
|
private $mediaType = "";
|
|
|
|
// duration
|
|
|
|
/**
|
|
* list of contexts
|
|
* @var array<string|array<string,mixed>>
|
|
*/
|
|
private $context = [];
|
|
|
|
/**
|
|
* atom URI
|
|
* @var string
|
|
*/
|
|
private $atomURI = '';
|
|
|
|
/**
|
|
* reply to atom URI
|
|
* @var ?string
|
|
*/
|
|
private $replyAtomURI = null;
|
|
|
|
// found items
|
|
/**
|
|
* Content Nation generated unique id
|
|
* @var string
|
|
*/
|
|
private $uuid = "";
|
|
|
|
/**
|
|
* sensitive flag
|
|
* @var ?bool
|
|
*/
|
|
private $sensitive = null;
|
|
/**
|
|
* blur hash
|
|
* @var string
|
|
*/
|
|
private $blurhash = "";
|
|
|
|
/**
|
|
* conversation id
|
|
* @var string
|
|
*/
|
|
private $conversation = '';
|
|
|
|
public function __construct(string $type) {
|
|
$this->type = $type;
|
|
}
|
|
|
|
|
|
/**
|
|
* set note content
|
|
* @param string $content note content
|
|
* @return APObject current instance
|
|
*/
|
|
public function setContent(string $content) : APObject {
|
|
$this->content = $content;
|
|
return $this;
|
|
}
|
|
|
|
public function getContent() : string {
|
|
return $this->content;
|
|
}
|
|
|
|
/**
|
|
* add context to list
|
|
* @param string|array<string,mixed> $context new context
|
|
*/
|
|
final public function addContext($context) : void {
|
|
if (!in_array($context, $this->context)) {
|
|
$this->context[] = $context;
|
|
}
|
|
}
|
|
/**
|
|
* set id
|
|
* @param string $id new id
|
|
*/
|
|
final public function setID(string $id) {
|
|
$this->id = $id;
|
|
}
|
|
|
|
final public function getID() : string {
|
|
return $this->id;
|
|
}
|
|
|
|
/**
|
|
* set href
|
|
* @param string $href href
|
|
*/
|
|
public function setHref(string $href) : void {
|
|
$this->href = $href;
|
|
}
|
|
|
|
/**
|
|
* set child object
|
|
* @param APObject $object
|
|
*/
|
|
public function setObject($object) : void {
|
|
$this->object = $object;
|
|
}
|
|
/**
|
|
* get child object
|
|
* @return APObject | false child object
|
|
*/
|
|
public function getObject() {
|
|
return $this->object;
|
|
}
|
|
|
|
/**
|
|
* set type
|
|
* @param string $type type
|
|
*/
|
|
public function setType(string $type) : void {
|
|
$this->type = $type;
|
|
}
|
|
|
|
public function getType() : string {
|
|
return $this->type;
|
|
}
|
|
|
|
/**
|
|
* set attachments
|
|
* @param APObject[] $attachment
|
|
*/
|
|
public function setAttachment($attachment) : void {
|
|
$this->attachment = $attachment;
|
|
}
|
|
|
|
public function addAttachment(APObject $attachment) : void {
|
|
$this->attachment[] = $attachment;
|
|
}
|
|
|
|
/**
|
|
* get attachments
|
|
* @return APObject[] attachments
|
|
*/
|
|
public function getAttachment() {
|
|
return $this->attachment;
|
|
}
|
|
|
|
public function getAttachmentsAsJson() : string {
|
|
if ($this->attachment === []) {
|
|
return "{}";
|
|
}
|
|
$obj = array();
|
|
foreach($this->attachment as $a) {
|
|
$obj[] = $a->toObject();
|
|
}
|
|
return json_encode($obj);
|
|
}
|
|
|
|
|
|
/**
|
|
* set attributed to
|
|
* @param string $to attribute to
|
|
*/
|
|
public function setAttributedTo(string $to) : void {
|
|
$this->attributedTo = $to;
|
|
}
|
|
|
|
public function getAttributedTo() : string {
|
|
return $this->attributedTo;
|
|
}
|
|
|
|
/**
|
|
* set name
|
|
* @param string $name name
|
|
*/
|
|
public function setName(string $name) : void {
|
|
$this->name = $name;
|
|
}
|
|
|
|
|
|
/**
|
|
* add Image
|
|
* @param Image $image image to add
|
|
*/
|
|
public function addImage(Image $image) : void {
|
|
$this->image[] = $image;
|
|
}
|
|
|
|
public function setInReplyTo(string $reply) : void {
|
|
$this->inReplyTo = $reply;
|
|
}
|
|
|
|
public function getInReplyTo() : string {
|
|
return $this->inReplyTo ?? "";
|
|
}
|
|
|
|
/**
|
|
* set published timestamp
|
|
* @param int $published published timestamp
|
|
*/
|
|
public function setPublished(int $published) : void {
|
|
$this->published = $published;
|
|
}
|
|
|
|
public function getPublished() : int {
|
|
return $this->published;
|
|
}
|
|
|
|
/**
|
|
* add Tag
|
|
* @param Tag $tag tag to add
|
|
*/
|
|
public function addTag(Tag $tag) : void {
|
|
$this->tag[] = $tag;
|
|
}
|
|
|
|
/**
|
|
* set url
|
|
* @param string $url URL
|
|
*/
|
|
public function setURL(string $url) : void {
|
|
$this->url = $url;
|
|
}
|
|
|
|
/**
|
|
* get URL
|
|
* @return string URL
|
|
*/
|
|
public function getURL() : string {
|
|
return $this->url;
|
|
}
|
|
|
|
/**
|
|
* add to
|
|
* @param string $to additional to address
|
|
*/
|
|
public function addTo(string $to) : void {
|
|
$this->to[] = $to;
|
|
}
|
|
|
|
/**
|
|
* get to
|
|
* @return array<string>
|
|
*/
|
|
public function getTo() {
|
|
return $this->to;
|
|
}
|
|
/**
|
|
* add cc
|
|
* @param string $cc additional cc address
|
|
*/
|
|
public function addCC(string $cc) : void {
|
|
$this->cc[] = $cc;
|
|
}
|
|
/**
|
|
* get cc
|
|
* @return array<string>
|
|
*/
|
|
public function getCC() {
|
|
return $this->cc;
|
|
}
|
|
|
|
public function getMediaType() : string {
|
|
return $this->mediaType;
|
|
}
|
|
/**
|
|
* set atom URI
|
|
* @param string $uri atom URI
|
|
*/
|
|
public function setAtomURI(string $uri) : void {
|
|
$this->atomURI = $uri;
|
|
}
|
|
|
|
/**
|
|
* set reply atom URI
|
|
* @param string $uri reply atom URI
|
|
*/
|
|
public function setReplyAtomURI(string $uri) : void {
|
|
$this->replyAtomURI = $uri;
|
|
}
|
|
|
|
/**
|
|
* set sensitive
|
|
* @param bool $sensitive status
|
|
*/
|
|
public function setSensitive(bool $sensitive) : void {
|
|
$this->sensitive = $sensitive;
|
|
}
|
|
|
|
/**
|
|
* set conversation id
|
|
* @param string $conversation conversation ID
|
|
*/
|
|
public function setConversation(string $conversation) : void {
|
|
$this->conversation = $conversation;
|
|
}
|
|
|
|
public function getConversation() : ?string {
|
|
return $this->conversation;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $json input
|
|
*/
|
|
public function fromJson($json) : bool {
|
|
if (!is_array($json)) {
|
|
error_log("fromJson called with ".gettype($json). " => ". debug_backtrace()[1]['function'] . " json: " . print_r($json, true));
|
|
return false;
|
|
}
|
|
if (array_key_exists('id', $json)) {
|
|
$this->id = $json['id'];
|
|
}
|
|
if (array_key_exists('content', $json)) {
|
|
$this->content = $json['content'];
|
|
}
|
|
if (array_key_exists('duration', $json)) {
|
|
try {
|
|
$this->duration = new \DateInterval($json['duration']);
|
|
} catch (\Exception $unused_e) {
|
|
error_log("error parsing duration ". $json['duration']);
|
|
}
|
|
}
|
|
if (array_key_exists('height', $json)) {
|
|
$this->height = intval($json['height'], 10);
|
|
}
|
|
if (array_key_exists('href', $json)) {
|
|
$this->href = $json['href'];
|
|
}
|
|
if (array_key_exists('endTime', $json) && $json['endTime'] !== null) {
|
|
$this->endTime = $this->parseDateTime($json['endTime']);
|
|
}
|
|
if (array_key_exists('width', $json)) {
|
|
$this->width = intval($json['width'], 10);
|
|
}
|
|
if (array_key_exists('attachment', $json) && $json['attachment'] !== null) {
|
|
$attachment = [];
|
|
foreach($json['attachment'] as $a) {
|
|
$att = \ContentNation\ActivityPub\Factory::newFromJson($a, "");
|
|
if ($att !== false) {
|
|
$attachment[] = $att;
|
|
}
|
|
}
|
|
$this->attachment = $attachment;
|
|
}
|
|
if (array_key_exists('attributedTo', $json)) {
|
|
if (is_array($json['attributedTo']) && array_key_exists(0, $json['attributedTo'])) {
|
|
// error_log("attributedTo is array" . print_r($json['attributedTo'], true));
|
|
if (is_array($json['attributedTo'][0])) {
|
|
$this->attributedTo = $json['attributedTo'][0]['id'];
|
|
} else {
|
|
$this->attributedTo = $json['attributedTo'][0];
|
|
}
|
|
} else {
|
|
$this->attributedTo = $json['attributedTo'];
|
|
}
|
|
}
|
|
if (array_key_exists('name', $json)) {
|
|
$this->name = $json['name'];
|
|
}
|
|
if (array_key_exists('icon',$json)) {
|
|
if (array_key_exists('type', $json['icon'])) {
|
|
$image = \ContentNation\ActivityPub\Factory::newImage();
|
|
$image->fromJson($json['icon']);
|
|
$this->icon[] = $image;
|
|
} else {
|
|
foreach($json['icon'] as $icon) {
|
|
$image = \ContentNation\ActivityPub\Factory::newImage();
|
|
$image->fromJson($icon);
|
|
$this->icon[] = $image;
|
|
}
|
|
}
|
|
}
|
|
if (array_key_exists('image',$json)) {
|
|
$image = \ContentNation\ActivityPub\Factory::newImage();
|
|
$image->fromJson($json['image']);
|
|
$this->image[] = $image;
|
|
}
|
|
if (array_key_exists('inReplyTo', $json)) {
|
|
$this->inReplyTo = $json['inReplyTo'];
|
|
}
|
|
if (array_key_exists('published', $json)) {
|
|
$this->published = $this->parseDateTime($json['published']);
|
|
}
|
|
if (array_key_exists('summary', $json)) {
|
|
$this->summary = $json['summary'];
|
|
}
|
|
if (array_key_exists('tag', $json)) {
|
|
require_once("Tag.php");
|
|
$tags = [];
|
|
foreach($json['tag'] as $t) {
|
|
$tag = new Tag();
|
|
$tag->fromJson($t);
|
|
$tags[] = $tag;
|
|
}
|
|
$this->tag = $tags;
|
|
}
|
|
if (array_key_exists('updated', $json)) {
|
|
$this->updated = $this->parseDateTime($json['updated']);
|
|
}
|
|
if (array_key_exists('url', $json)) {
|
|
$this->url = $json['url'];
|
|
}
|
|
if (array_key_exists('to', $json)) {
|
|
if (is_array($json['to'])) {
|
|
foreach($json['to'] as $to) {
|
|
$this->to[] = $to;
|
|
}
|
|
} else {
|
|
$this->to[] = $json['to'];
|
|
}
|
|
}
|
|
if (array_key_exists('cc', $json)) {
|
|
if (is_array($json['cc'])) {
|
|
foreach($json['cc'] as $cc) {
|
|
$this->cc[] = $cc;
|
|
}
|
|
} else {
|
|
$this->cc[] = $json['cc'];
|
|
}
|
|
}
|
|
if (array_key_exists('mediaType', $json)) {
|
|
$this->mediaType = $json['mediaType'];
|
|
}
|
|
if (array_key_exists('object', $json)) {
|
|
$this->object = \ContentNation\ActivityPub\Factory::newFromJson($json['object'], "");
|
|
}
|
|
if (array_key_exists('sensitive', $json)) {
|
|
$this->sensitive = $json['sensitive'];
|
|
}
|
|
if (array_key_exists('blurhash', $json)) {
|
|
$this->blurhash = $json['blurhash'];
|
|
}
|
|
if (array_key_exists('uuid', $json)) {
|
|
$this->uuid = $json['uuid'];
|
|
}
|
|
if (array_key_exists('conversation', $json)) {
|
|
$this->conversation = $json['conversation'];
|
|
}
|
|
return true;
|
|
}
|
|
/**
|
|
* convert internal state to php array
|
|
* @return array<string,mixed>
|
|
*/
|
|
public function toObject() {
|
|
$return = [];
|
|
if (sizeof($this->context) == 1) {
|
|
$return['@context'] = array_values($this->context)[0];
|
|
} else if (sizeof($this->context) > 1) {
|
|
$c = [];
|
|
foreach(array_values($this->context) as $context) {
|
|
$c[] = $context;
|
|
}
|
|
$return['@context'] = $c;
|
|
}
|
|
if ($this->id !== "") {
|
|
$return['id'] = $this->id;
|
|
}
|
|
$return['type'] = $this->type;
|
|
|
|
if ($this->content !== "") {
|
|
$return['content'] = $this->content;
|
|
}
|
|
|
|
if ($this->duration !== false) {
|
|
$return['duration'] = $this->duration->format("P%yY%mM%dDT%hH%iM%sS");
|
|
}
|
|
if ($this->height != -1) {
|
|
$return['height'] = $this->height;
|
|
}
|
|
if ($this->href !== "") {
|
|
$return['href'] = $this->href;
|
|
}
|
|
if ($this->endTime > 0) {
|
|
$return['endTime'] = gmdate("Y-m-d\TH:i:s\Z", $this->endTime);
|
|
}
|
|
if ($this->width != -1) {
|
|
$return['width'] = $this->width;
|
|
}
|
|
if (sizeof($this->attachment) > 0) {
|
|
$attachment = [];
|
|
foreach($this->attachment as $a) {
|
|
$attachment[] = $a->toObject();
|
|
}
|
|
$return['attachment'] = $attachment;
|
|
}
|
|
if ($this->attributedTo !== "") {
|
|
$return['attributedTo'] = $this->attributedTo;
|
|
}
|
|
if ($this->name !== "") {
|
|
$return['name'] = $this->name;
|
|
}
|
|
if (sizeof($this->icon) > 0 ) {
|
|
if (sizeof($this->icon) > 1) {
|
|
$icons = [];
|
|
foreach($this->icon as $icon) {
|
|
$icons[] = $icon->toObject();
|
|
}
|
|
$return['icon'] = $icons;
|
|
} else {
|
|
$return['icon'] = $this->icon[0]->toObject();
|
|
}
|
|
}
|
|
if (sizeof($this->image) > 0 ) {
|
|
$images = [];
|
|
foreach($this->image as $image) {
|
|
$images[] = $image->toObject();
|
|
}
|
|
$return['image'] = $images;
|
|
}
|
|
if ($this->inReplyTo !== "") {
|
|
$return['inReplyTo'] = $this->inReplyTo;
|
|
}
|
|
if ($this->published > 0) {
|
|
$return['published'] = gmdate("Y-m-d\TH:i:s\Z", $this->published);
|
|
}
|
|
if ($this->summary !== "") {
|
|
$return['summary'] = $this->summary;
|
|
}
|
|
if (sizeof($this->tag) > 0) {
|
|
$tags = [];
|
|
foreach($this->tag as $tag) {
|
|
$tags[] = $tag->toObject();
|
|
}
|
|
$return['tag'] = $tags;
|
|
}
|
|
if ($this->updated > 0) {
|
|
$return['updated'] = gmdate("Y-m-d\TH:i:S\Z", $this->updated);
|
|
}
|
|
if ($this->url !== '') {
|
|
$return['url'] = $this->url;
|
|
}
|
|
if (sizeof($this->to) > 0) {
|
|
$return['to'] = $this->to;
|
|
}
|
|
if (sizeof($this->cc) > 0) {
|
|
$return['cc'] = $this->cc;
|
|
}
|
|
if ($this->mediaType !== '') {
|
|
$return['mediaType'] = $this->mediaType;
|
|
}
|
|
if ($this->object !== null) {
|
|
$return['object'] = $this->object->toObject();
|
|
}
|
|
if ($this->atomURI !== '') {
|
|
$return['atomUri'] = $this->atomURI;
|
|
}
|
|
if ($this->replyAtomURI !== null) {
|
|
$return['inReplyTo'] = $this->replyAtomURI;
|
|
$return['inReplyToAtomUri'] = $this->replyAtomURI;
|
|
}
|
|
if ($this->sensitive !== null) {
|
|
$return['sensitive'] = $this->sensitive;
|
|
}
|
|
if ($this->blurhash !== '') {
|
|
$return['blurhash'] = $this->blurhash;
|
|
}
|
|
if ($this->conversation !== '') {
|
|
$return['conversation'] = $this->conversation;
|
|
}
|
|
if ($this->uuid !== '') {
|
|
$return['uuid'] = $this->uuid;
|
|
}
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* set uuid
|
|
* @param string $id
|
|
*/
|
|
public function setUUID(string $id) : void {
|
|
$this->uuid = $id;
|
|
}
|
|
|
|
public function getUUID() : string {
|
|
return $this->uuid;
|
|
}
|
|
public static function parseDateTime(string $input) : int {
|
|
$timestamp = 0;
|
|
if (strpos($input, "T")!== false) {
|
|
$date = \DateTime::createFromFormat('Y-m-d\TH:i:sT', $input);
|
|
if ($date === false) {
|
|
$date = \DateTime::createFromFormat('Y-m-d\TH:i:s+', $input);
|
|
}
|
|
if ($date !== false) {
|
|
$timestamp = $date->getTimestamp();
|
|
} else {
|
|
error_log("date parsing error ". $input);
|
|
}
|
|
} else {
|
|
$timestamp = intval($input, 10);
|
|
}
|
|
return $timestamp;
|
|
}
|
|
|
|
}
|