]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activity.php
Merge branch '1.0.x' of gitorious.org:statusnet/mainline into 1.0.x
[quix0rs-gnu-social.git] / lib / activity.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * An activity
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Feed
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Zach Copley <zach@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * An activity in the ActivityStrea.ms world
37  *
38  * An activity is kind of like a sentence: someone did something
39  * to something else.
40  *
41  * 'someone' is the 'actor'; 'did something' is the verb;
42  * 'something else' is the object.
43  *
44  * @category  OStatus
45  * @package   StatusNet
46  * @author    Evan Prodromou <evan@status.net>
47  * @copyright 2010 StatusNet, Inc.
48  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
49  * @link      http://status.net/
50  */
51 class Activity
52 {
53     const SPEC   = 'http://activitystrea.ms/spec/1.0/';
54     const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
55     const MEDIA  = 'http://purl.org/syndication/atommedia';
56
57     const VERB       = 'verb';
58     const OBJECT     = 'object';
59     const ACTOR      = 'actor';
60     const SUBJECT    = 'subject';
61     const OBJECTTYPE = 'object-type';
62     const CONTEXT    = 'context';
63     const TARGET     = 'target';
64
65     const ATOM = 'http://www.w3.org/2005/Atom';
66
67     const AUTHOR    = 'author';
68     const PUBLISHED = 'published';
69     const UPDATED   = 'updated';
70
71     const RSS = null; // no namespace!
72
73     const PUBDATE     = 'pubDate';
74     const DESCRIPTION = 'description';
75     const GUID        = 'guid';
76     const SELF        = 'self';
77     const IMAGE       = 'image';
78     const URL         = 'url';
79
80     const DC = 'http://purl.org/dc/elements/1.1/';
81
82     const CREATOR = 'creator';
83
84     const CONTENTNS = 'http://purl.org/rss/1.0/modules/content/';
85     const ENCODED = 'encoded';
86
87     public $actor;   // an ActivityObject
88     public $verb;    // a string (the URL)
89     public $objects = array();  // an array of ActivityObjects
90     public $target;  // an ActivityObject
91     public $context; // an ActivityObject
92     public $time;    // Time of the activity
93     public $link;    // an ActivityObject
94     public $entry;   // the source entry
95     public $feed;    // the source feed
96
97     public $summary; // summary of activity
98     public $content; // HTML content of activity
99     public $id;      // ID of the activity
100     public $title;   // title of the activity
101     public $categories = array(); // list of AtomCategory objects
102     public $enclosures = array(); // list of enclosure URL references
103
104     public $extra = array(); // extra elements as array(tag, attrs, content)
105     public $source;  // ActivitySource object representing 'home feed'
106     public $selfLink; // <link rel='self' type='application/atom+xml'>
107     public $editLink; // <link rel='edit' type='application/atom+xml'>
108
109     /**
110      * Turns a regular old Atom <entry> into a magical activity
111      *
112      * @param DOMElement $entry Atom entry to poke at
113      * @param DOMElement $feed  Atom feed, for context
114      */
115     function __construct($entry = null, $feed = null)
116     {
117         if (is_null($entry)) {
118             return;
119         }
120
121         // Insist on a feed's root DOMElement; don't allow a DOMDocument
122         if ($feed instanceof DOMDocument) {
123             throw new ClientException(
124                 // TRANS: Client exception thrown when a feed instance is a DOMDocument.
125                 _('Expecting a root feed element but got a whole XML document.')
126             );
127         }
128
129         $this->entry = $entry;
130         $this->feed  = $feed;
131
132         if ($entry->namespaceURI == Activity::ATOM &&
133             $entry->localName == 'entry') {
134             $this->_fromAtomEntry($entry, $feed);
135         } else if ($entry->namespaceURI == Activity::RSS &&
136                    $entry->localName == 'item') {
137             $this->_fromRssItem($entry, $feed);
138         } else if ($entry->namespaceURI == Activity::SPEC &&
139                    $entry->localName == 'object') {
140             $this->_fromAtomEntry($entry, $feed);
141         } else {
142             // Low level exception. No need for i18n.
143             throw new Exception("Unknown DOM element: {$entry->namespaceURI} {$entry->localName}");
144         }
145     }
146
147     function _fromAtomEntry($entry, $feed)
148     {
149         $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
150
151         if (!empty($pubEl)) {
152             $this->time = strtotime($pubEl->textContent);
153         } else {
154             // XXX technically an error; being liberal. Good idea...?
155             $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
156             if (!empty($updateEl)) {
157                 $this->time = strtotime($updateEl->textContent);
158             } else {
159                 $this->time = null;
160             }
161         }
162
163         $this->link = ActivityUtils::getPermalink($entry);
164
165         $verbEl = $this->_child($entry, self::VERB);
166
167         if (!empty($verbEl)) {
168             $this->verb = trim($verbEl->textContent);
169         } else {
170             $this->verb = ActivityVerb::POST;
171             // XXX: do other implied stuff here
172         }
173
174         // get immediate object children
175
176         $objectEls = ActivityUtils::children($entry, self::OBJECT, self::SPEC);
177
178         if (count($objectEls) > 0) {
179             foreach ($objectEls as $objectEl) {
180                 // Special case for embedded activities
181                 $objectType = ActivityUtils::childContent($objectEl, self::OBJECTTYPE, self::SPEC);
182                 if (!empty($objectType) && $objectType == ActivityObject::ACTIVITY) {
183                     $this->objects[] = new Activity($objectEl);
184                 } else {
185                     $this->objects[] = new ActivityObject($objectEl);
186                 }
187             }
188         } else {
189             // XXX: really?
190             $this->objects[] = new ActivityObject($entry);
191         }
192
193         $actorEl = $this->_child($entry, self::ACTOR);
194
195         if (!empty($actorEl)) {
196             // Standalone <activity:actor> elements are a holdover from older
197             // versions of ActivityStreams. Newer feeds should have this data
198             // integrated straight into <atom:author>.
199
200             $this->actor = new ActivityObject($actorEl);
201
202             // Cliqset has bad actor IDs (just nickname of user). We
203             // work around it by getting the author data and using its
204             // id instead
205
206             if (!preg_match('/^\w+:/', $this->actor->id)) {
207                 $authorEl = ActivityUtils::child($entry, 'author');
208                 if (!empty($authorEl)) {
209                     $authorObj = new ActivityObject($authorEl);
210                     $this->actor->id = $authorObj->id;
211                 }
212             }
213         } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
214
215             // An <atom:author> in the entry overrides any author info on
216             // the surrounding feed.
217             $this->actor = new ActivityObject($authorEl);
218
219         } else if (!empty($feed) &&
220                    $subjectEl = $this->_child($feed, self::SUBJECT)) {
221
222             // Feed subject is used for things like groups.
223             // Should actually possibly not be interpreted as an actor...?
224             $this->actor = new ActivityObject($subjectEl);
225
226         } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
227                                                               self::ATOM)) {
228
229             // If there's no <atom:author> on the entry, it's safe to assume
230             // the containing feed's authorship info applies.
231             $this->actor = new ActivityObject($authorEl);
232         }
233
234         $contextEl = $this->_child($entry, self::CONTEXT);
235
236         if (!empty($contextEl)) {
237             $this->context = new ActivityContext($contextEl);
238         } else {
239             $this->context = new ActivityContext($entry);
240         }
241
242         $targetEl = $this->_child($entry, self::TARGET);
243
244         if (!empty($targetEl)) {
245             $this->target = new ActivityObject($targetEl);
246         }
247
248         $this->summary = ActivityUtils::childContent($entry, 'summary');
249         $this->id      = ActivityUtils::childContent($entry, 'id');
250         $this->content = ActivityUtils::getContent($entry);
251
252         $catEls = $entry->getElementsByTagNameNS(self::ATOM, 'category');
253         if ($catEls) {
254             for ($i = 0; $i < $catEls->length; $i++) {
255                 $catEl = $catEls->item($i);
256                 $this->categories[] = new AtomCategory($catEl);
257             }
258         }
259
260         foreach (ActivityUtils::getLinks($entry, 'enclosure') as $link) {
261             $this->enclosures[] = $link->getAttribute('href');
262         }
263
264         // From APP. Might be useful.
265
266         $this->selfLink = ActivityUtils::getLink($entry, 'self', 'application/atom+xml');
267         $this->editLink = ActivityUtils::getLink($entry, 'edit', 'application/atom+xml');
268     }
269
270     function _fromRssItem($item, $channel)
271     {
272         $verbEl = $this->_child($item, self::VERB);
273
274         if (!empty($verbEl)) {
275             $this->verb = trim($verbEl->textContent);
276         } else {
277             $this->verb = ActivityVerb::POST;
278             // XXX: do other implied stuff here
279         }
280
281         $pubDateEl = $this->_child($item, self::PUBDATE, self::RSS);
282
283         if (!empty($pubDateEl)) {
284             $this->time = strtotime($pubDateEl->textContent);
285         }
286
287         if ($authorEl = $this->_child($item, self::AUTHOR, self::RSS)) {
288             $this->actor = ActivityObject::fromRssAuthor($authorEl);
289         } else if ($dcCreatorEl = $this->_child($item, self::CREATOR, self::DC)) {
290             $this->actor = ActivityObject::fromDcCreator($dcCreatorEl);
291         } else if ($posterousEl = $this->_child($item, ActivityObject::AUTHOR, ActivityObject::POSTEROUS)) {
292             // Special case for Posterous.com
293             $this->actor = ActivityObject::fromPosterousAuthor($posterousEl);
294         } else if (!empty($channel)) {
295             $this->actor = ActivityObject::fromRssChannel($channel);
296         } else {
297             // No actor!
298         }
299
300         $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, self::RSS);
301
302         $contentEl = ActivityUtils::child($item, self::ENCODED, self::CONTENTNS);
303
304         if (!empty($contentEl)) {
305             // <content:encoded> XML node's text content is HTML; no further processing needed.
306             $this->content = $contentEl->textContent;
307         } else {
308             $descriptionEl = ActivityUtils::child($item, self::DESCRIPTION, self::RSS);
309             if (!empty($descriptionEl)) {
310                 // Per spec, <description> must be plaintext.
311                 // In practice, often there's HTML... but these days good
312                 // feeds are using <content:encoded> which is explicitly
313                 // real HTML.
314                 // We'll treat this following spec, and do HTML escaping
315                 // to convert from plaintext to HTML.
316                 $this->content = htmlspecialchars($descriptionEl->textContent);
317             }
318         }
319
320         $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, self::RSS);
321
322         // @fixme enclosures
323         // @fixme thumbnails... maybe
324
325         $guidEl = ActivityUtils::child($item, self::GUID, self::RSS);
326
327         if (!empty($guidEl)) {
328             $this->id = $guidEl->textContent;
329
330             if ($guidEl->hasAttribute('isPermaLink') && $guidEl->getAttribute('isPermaLink') != 'false') {
331                 // overwrites <link>
332                 $this->link = $this->id;
333             }
334         }
335
336         $this->objects[] = new ActivityObject($item);
337         $this->context   = new ActivityContext($item);
338     }
339
340     /**
341      * Returns an Atom <entry> based on this activity
342      *
343      * @return DOMElement Atom entry
344      */
345
346     function toAtomEntry()
347     {
348         return null;
349     }
350
351     /**
352      * Returns an array based on this activity suitable
353      * for encoding as a JSON object
354      *
355      * @return array $activity
356      */
357
358     function asArray()
359     {
360         $activity = array();
361
362         // actor
363         $activity['actor'] = $this->actor->asArray();
364
365         // body
366         $activity['body'] = $this->content;
367
368         // generator <-- We could use this when we know a notice is created
369         //               locally. Or if we know the upstream Generator.
370
371         // icon <-- I've decided to use the posting user's stream avatar here
372         //          for now (also included in the avatarLinks extension)
373
374
375         // object
376         if ($this->verb == ActivityVerb::POST && count($this->objects) == 1) {
377             $activity['object'] = $this->objects[0]->asArray();
378
379             // Context stuff. For now I'm just sticking most of it
380             // in a property called "context"
381
382             if (!empty($this->context)) {
383
384                 if (!empty($this->context->location)) {
385                     $loc = $this->context->location;
386
387                     // GeoJSON
388
389                     $activity['geopoint'] = array(
390                         'type'        => 'Point',
391                         'coordinates' => array($loc->lat, $loc->lon)
392                     );
393
394                 }
395
396                 $activity['to']      = $this->context->getToArray();
397                 $activity['context'] = $this->context->asArray();
398             }
399
400             // Instead of adding enclosures as an extension to JSON
401             // Activities, it seems like we should be using the
402             // attachedObjects property of ActivityObject
403
404             $attachedObjects = array();
405
406             // XXX: OK, this is kinda cheating. We should probably figure out
407             // what kind of objects these are based on mime-type and then
408             // create specific object types. Right now this rely on
409             // duck-typing.  Also, we should include an embed code for
410             // video attachments.
411
412             foreach ($this->enclosures as $enclosure) {
413
414                 if (is_string($enclosure)) {
415
416                     $attachedObjects[]['id']  = $enclosure;
417
418                 } else {
419
420                     $attachedObjects[]['id']  = $enclosure->url;
421
422                     $mediaLink = new ActivityStreamsMediaLink(
423                         $enclosure->url,
424                         null,
425                         null,
426                         $enclosure->mimetype
427                         // XXX: Add 'size' as an extension to MediaLink?
428                     );
429
430                     $attachedObjects[]['mediaLink'] = $mediaLink->asArray(); // extension
431
432                     if ($enclosure->title) {
433                         $attachedObjects[]['displayName'] = $enclosure->title;
434                     }
435                }
436             }
437
438             if (!empty($attachedObjects)) {
439                 $activity['object']['attachedObjects'] = $attachedObjects;
440             }
441
442         } else {
443             $activity['object'] = array();
444             foreach($this->objects as $object) {
445                 $oa = $object->asArray();
446                 if ($object instanceof Activity) {
447                     // throw in a type
448                     // XXX: hackety-hack
449                     $oa['objectType'] = 'activity';
450                 }
451                 $activity['object'][] = $oa;
452             }
453         }
454
455         $activity['postedTime'] = self::iso8601Date($this->time); // Change to exactly be RFC3339?
456
457         // provider
458         $provider = array(
459             'objectType' => 'service',
460             'displayName' => common_config('site', 'name'),
461             'url' => common_root_url()
462         );
463
464         $activity['provider'] = $provider;
465
466         // target
467         if (!empty($this->target)) {
468             $activity['target'] = $this->target->asArray();
469         }
470
471         // title
472         $activity['title'] = $this->title;
473
474         // updatedTime <-- Should we use this to indicate the time we received
475         //                 a remote notice? Probably not.
476
477         // verb
478         //
479         // We can probably use the whole schema URL here but probably the
480         // relative simple name is easier to parse
481         $activity['verb'] = substr($this->verb, strrpos($this->verb, '/') + 1);
482
483         /* Purely extensions hereafter */
484
485         $tags = array();
486
487         // Use an Activity Object for term? Which object? Note?
488         foreach ($this->categories as $cat) {
489             $tags[] = $cat->term;
490         }
491
492         $activity['tags'] = $tags;
493
494         // XXX: a bit of a hack... Since JSON isn't namespaced we probably
495         // shouldn't be using 'statusnet:notice_info', but this will work
496         // for the moment.
497
498         foreach ($this->extra as $e) {
499             list($objectName, $props, $txt) = $e;
500             if (!empty($objectName)) {
501                 $activity[$objectName] = $props;
502             }
503         }
504
505         return array_filter($activity);
506     }
507
508     function asString($namespace=false, $author=true, $source=false)
509     {
510         $xs = new XMLStringer(true);
511         $this->outputTo($xs, $namespace, $author, $source);
512         return $xs->getString();
513     }
514
515     function outputTo($xs, $namespace=false, $author=true, $source=false, $tag='entry')
516     {
517         if ($namespace) {
518             $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
519                            'xmlns:thr' => 'http://purl.org/syndication/thread/1.0',
520                            'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
521                            'xmlns:georss' => 'http://www.georss.org/georss',
522                            'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
523                            'xmlns:poco' => 'http://portablecontacts.net/spec/1.0',
524                            'xmlns:media' => 'http://purl.org/syndication/atommedia',
525                            'xmlns:statusnet' => 'http://status.net/schema/api/1/');
526         } else {
527             $attrs = array();
528         }
529
530         $xs->elementStart($tag, $attrs);
531
532         if ($tag != 'entry') {
533             $xs->element('activity:object-type', null, ActivityObject::ACTIVITY);
534         }
535
536         if ($this->verb == ActivityVerb::POST && count($this->objects) == 1 && $tag == 'entry') {
537
538             $obj = $this->objects[0];
539                         $obj->outputTo($xs, null);
540
541         } else {
542             $xs->element('id', null, $this->id);
543             $xs->element('title', null, $this->title);
544
545             $xs->element('content', array('type' => 'html'), $this->content);
546
547             if (!empty($this->summary)) {
548                 $xs->element('summary', null, $this->summary);
549             }
550
551             if (!empty($this->link)) {
552                 $xs->element('link', array('rel' => 'alternate',
553                                            'type' => 'text/html'),
554                              $this->link);
555             }
556
557         }
558
559         $xs->element('activity:verb', null, $this->verb);
560
561         $published = self::iso8601Date($this->time);
562
563         $xs->element('published', null, $published);
564         $xs->element('updated', null, $published);
565
566         if ($author) {
567             $this->actor->outputTo($xs, 'author');
568
569             // XXX: Remove <activity:actor> ASAP! Author information
570             // has been moved to the author element in the Activity
571             // Streams spec. We're outputting actor only for backward
572             // compatibility with clients that can only parse
573             // activities based on older versions of the spec.
574
575             $depMsg = 'Deprecation warning: activity:actor is present '
576                 . 'only for backward compatibility. It will be '
577                 . 'removed in the next version of StatusNet.';
578             $xs->comment($depMsg);
579             $this->actor->outputTo($xs, 'activity:actor');
580         }
581
582         if ($this->verb != ActivityVerb::POST || count($this->objects) != 1 || $tag != 'entry') {
583             foreach($this->objects as $object) {
584                 if ($object instanceof Activity) {
585                     $object->outputTo($xs, false, true, true, 'activity:object');
586                 } else {
587                     $object->outputTo($xs, 'activity:object');
588                 }
589             }
590         }
591
592         if (!empty($this->context)) {
593
594             if (!empty($this->context->replyToID)) {
595                 if (!empty($this->context->replyToUrl)) {
596                     $xs->element('thr:in-reply-to',
597                                  array('ref' => $this->context->replyToID,
598                                        'href' => $this->context->replyToUrl));
599                 } else {
600                     $xs->element('thr:in-reply-to',
601                                  array('ref' => $this->context->replyToID));
602                 }
603             }
604
605             if (!empty($this->context->replyToUrl)) {
606                 $xs->element('link', array('rel' => 'related',
607                                            'href' => $this->context->replyToUrl));
608             }
609
610             if (!empty($this->context->conversation)) {
611                 $xs->element('link', array('rel' => 'ostatus:conversation',
612                                            'href' => $this->context->conversation));
613             }
614
615             foreach ($this->context->attention as $attnURI) {
616                 $xs->element('link', array('rel' => 'ostatus:attention',
617                                            'href' => $attnURI));
618                 $xs->element('link', array('rel' => 'mentioned',
619                                            'href' => $attnURI));
620             }
621
622             // XXX: shoulda used ActivityVerb::SHARE
623
624             if (!empty($this->context->forwardID)) {
625                 if (!empty($this->context->forwardUrl)) {
626                     $xs->element('ostatus:forward',
627                                  array('ref' => $this->context->forwardID,
628                                        'href' => $this->context->forwardUrl));
629                 } else {
630                     $xs->element('ostatus:forward',
631                                  array('ref' => $this->context->forwardID));
632                 }
633             }
634
635             if (!empty($this->context->location)) {
636                 $loc = $this->context->location;
637                 $xs->element('georss:point', null, $loc->lat . ' ' . $loc->lon);
638             }
639         }
640
641         if ($this->target) {
642             $this->target->outputTo($xs, 'activity:target');
643         }
644
645         foreach ($this->categories as $cat) {
646             $cat->outputTo($xs);
647         }
648
649         // can be either URLs or enclosure objects
650
651         foreach ($this->enclosures as $enclosure) {
652             if (is_string($enclosure)) {
653                 $xs->element('link', array('rel' => 'enclosure',
654                                            'href' => $enclosure));
655             } else {
656                 $attributes = array('rel' => 'enclosure',
657                                     'href' => $enclosure->url,
658                                     'type' => $enclosure->mimetype,
659                                     'length' => $enclosure->size);
660                 if ($enclosure->title) {
661                     $attributes['title'] = $enclosure->title;
662                 }
663                 $xs->element('link', $attributes);
664             }
665         }
666
667         // Info on the source feed
668
669         if ($source && !empty($this->source)) {
670             $xs->elementStart('source');
671
672             $xs->element('id', null, $this->source->id);
673             $xs->element('title', null, $this->source->title);
674
675             if (array_key_exists('alternate', $this->source->links)) {
676                 $xs->element('link', array('rel' => 'alternate',
677                                            'type' => 'text/html',
678                                            'href' => $this->source->links['alternate']));
679             }
680
681             if (array_key_exists('self', $this->source->links)) {
682                 $xs->element('link', array('rel' => 'self',
683                                            'type' => 'application/atom+xml',
684                                            'href' => $this->source->links['self']));
685             }
686
687             if (array_key_exists('license', $this->source->links)) {
688                 $xs->element('link', array('rel' => 'license',
689                                            'href' => $this->source->links['license']));
690             }
691
692             if (!empty($this->source->icon)) {
693                 $xs->element('icon', null, $this->source->icon);
694             }
695
696             if (!empty($this->source->updated)) {
697                 $xs->element('updated', null, $this->source->updated);
698             }
699
700             $xs->elementEnd('source');
701         }
702
703         if (!empty($this->selfLink)) {
704             $xs->element('link', array('rel' => 'self',
705                                        'type' => 'application/atom+xml',
706                                        'href' => $this->selfLink));
707         }
708
709         if (!empty($this->editLink)) {
710             $xs->element('link', array('rel' => 'edit',
711                                        'type' => 'application/atom+xml',
712                                        'href' => $this->editLink));
713         }
714
715         // For throwing in extra elements; used for statusnet:notice_info
716
717         foreach ($this->extra as $el) {
718             list($tag, $attrs, $content) = $el;
719             $xs->element($tag, $attrs, $content);
720         }
721
722         $xs->elementEnd($tag);
723
724         return;
725     }
726
727     private function _child($element, $tag, $namespace=self::SPEC)
728     {
729         return ActivityUtils::child($element, $tag, $namespace);
730     }
731
732     /**
733      * For consistency, we'll always output UTC rather than local time.
734      * Note that clients *should* accept any timezone we give them as long
735      * as it's properly formatted.
736      *
737      * @param int $tm Unix timestamp
738      * @return string
739      */
740     static function iso8601Date($tm)
741     {
742         $dateStr = date('d F Y H:i:s', $tm);
743         $d = new DateTime($dateStr, new DateTimeZone('UTC'));
744         return $d->format('c');
745     }
746 }