]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activitycontext.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / lib / activitycontext.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 class ActivityContext
36 {
37     public $replyToID;
38     public $replyToUrl;
39     public $location;
40     public $attention = array();
41     public $conversation;
42
43     const THR     = 'http://purl.org/syndication/thread/1.0';
44     const GEORSS  = 'http://www.georss.org/georss';
45     const OSTATUS = 'http://ostatus.org/schema/1.0';
46
47     const INREPLYTO = 'in-reply-to';
48     const REF       = 'ref';
49     const HREF      = 'href';
50
51     const POINT     = 'point';
52
53     const ATTENTION    = 'ostatus:attention';
54     const CONVERSATION = 'ostatus:conversation';
55
56     function __construct($element)
57     {
58         $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
59
60         if (!empty($replyToEl)) {
61             $this->replyToID  = $replyToEl->getAttribute(self::REF);
62             $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
63         }
64
65         $this->location = $this->getLocation($element);
66
67         $this->conversation = ActivityUtils::getLink($element, self::CONVERSATION);
68
69         // Multiple attention links allowed
70
71         $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
72
73         for ($i = 0; $i < $links->length; $i++) {
74
75             $link = $links->item($i);
76
77             $linkRel = $link->getAttribute(ActivityUtils::REL);
78
79             if ($linkRel == self::ATTENTION) {
80                 $this->attention[] = $link->getAttribute(self::HREF);
81             }
82         }
83     }
84
85     /**
86      * Parse location given as a GeoRSS-simple point, if provided.
87      * http://www.georss.org/simple
88      *
89      * @param feed item $entry
90      * @return mixed Location or false
91      */
92     function getLocation($dom)
93     {
94         $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
95
96         for ($i = 0; $i < $points->length; $i++) {
97             $point = $points->item($i)->textContent;
98             return self::locationFromPoint($point);
99         }
100
101         return null;
102     }
103
104     // XXX: Move to ActivityUtils or Location?
105     static function locationFromPoint($point)
106     {
107         $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
108         $point = preg_replace('/\s+/', ' ', $point);
109         $point = trim($point);
110         $coords = explode(' ', $point);
111         if (count($coords) == 2) {
112             list($lat, $lon) = $coords;
113             if (is_numeric($lat) && is_numeric($lon)) {
114                 common_log(LOG_INFO, "Looking up location for $lat $lon from georss point");
115                 return Location::fromLatLon($lat, $lon);
116             }
117         }
118         common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
119         return null;
120     }
121 }