]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activitycontext.php
*** Privacy Leak fixed: ***
[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();    // 'uri' => 'type'
41     public $conversation;
42     public $conversation_url;
43     public $scope;
44
45     const THR     = 'http://purl.org/syndication/thread/1.0';
46     const GEORSS  = 'http://www.georss.org/georss';
47     const OSTATUS = 'http://ostatus.org/schema/1.0';
48
49     const INREPLYTO  = 'in-reply-to';
50     const REF        = 'ref';
51     const HREF       = 'href';
52
53     // OStatus element names with prefixes
54     const OBJECTTYPE = 'ostatus:object-type';   // FIXME: Undocumented!
55     const CONVERSATION = 'conversation';
56
57     const POINT     = 'point';
58
59     const MENTIONED    = 'mentioned';
60
61     const ATTN_PUBLIC  = 'http://activityschema.org/collection/public';
62
63     function __construct($element = null)
64     {
65         if (empty($element)) {
66             return;
67         }
68
69         $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
70
71         if (!empty($replyToEl)) {
72             $this->replyToID  = $replyToEl->getAttribute(self::REF);
73             $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
74         }
75
76         $this->location = $this->getLocation($element);
77
78         foreach ($element->getElementsByTagNameNS(self::OSTATUS, self::CONVERSATION) as $conv) {
79             if ($conv->hasAttribute('ref')) {
80                 $this->conversation = $conv->getAttribute('ref');
81                 if ($conv->hasAttribute('href')) {
82                     $this->conversation_url = $conv->getAttribute('href');
83                 }
84             } else {
85                 $this->conversation = $conv->textContent;
86             }
87             if (!empty($this->conversation)) {
88                 break;
89             }
90         }
91         if (empty($this->conversation)) {
92             // fallback to the atom:link rel="ostatus:conversation" element
93             $this->conversation = ActivityUtils::getLink($element, 'ostatus:'.self::CONVERSATION);
94         }
95
96         // Multiple attention links allowed
97
98         $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
99
100         for ($i = 0; $i < $links->length; $i++) {
101             $link = $links->item($i);
102
103             $linkRel  = $link->getAttribute(ActivityUtils::REL);
104             $linkHref = $link->getAttribute(self::HREF);
105             if ($linkRel == self::MENTIONED && $linkHref !== '') {
106                 $this->attention[$linkHref] = $link->getAttribute(ActivityContext::OBJECTTYPE);
107             }
108         }
109     }
110
111     /**
112      * Parse location given as a GeoRSS-simple point, if provided.
113      * http://www.georss.org/simple
114      *
115      * @param feed item $entry
116      * @return mixed Location or false
117      */
118     function getLocation($dom)
119     {
120         $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
121
122         for ($i = 0; $i < $points->length; $i++) {
123             $point = $points->item($i)->textContent;
124             return self::locationFromPoint($point);
125         }
126
127         return null;
128     }
129
130     // XXX: Move to ActivityUtils or Location?
131     static function locationFromPoint($point)
132     {
133         $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
134         $point = preg_replace('/\s+/', ' ', $point);
135         $point = trim($point);
136         $coords = explode(' ', $point);
137         if (count($coords) == 2) {
138             list($lat, $lon) = $coords;
139             if (is_numeric($lat) && is_numeric($lon)) {
140                 common_log(LOG_INFO, "Looking up location for $lat $lon from georss point");
141                 return Location::fromLatLon($lat, $lon);
142             }
143         }
144         common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
145         return null;
146     }
147
148     /**
149      * Returns context (StatusNet stuff) as an array suitable for serializing
150      * in JSON. Right now context stuff is an extension to Activity.
151      *
152      * @return array the context
153      */
154
155     function asArray()
156     {
157         $context = array();
158
159         $context['inReplyTo']    = $this->getInReplyToArray();
160         $context['conversation'] = $this->conversation;
161         $context['conversation_url'] = $this->conversation_url;
162
163         return array_filter($context);
164     }
165
166     /**
167      * Returns an array of arrays representing Activity Objects (intended to be
168      * serialized in JSON) that represent WHO the Activity is supposed to
169      * be received by. This is not really specified but appears in an example
170      * of the current spec as an extension. We might want to figure out a JSON
171      * serialization for OStatus and use that to express mentions instead.
172      *
173      * XXX: People's ideas on how to do this are all over the place
174      *
175      * @return array the array of recipients
176      */
177
178     function getToArray()
179     {
180         $tos = array();
181
182         foreach ($this->attention as $attnUrl => $attnType) {
183             $to = array(
184                 'objectType' => $attnType,  // can be empty
185                 'id'         => $attnUrl,
186             );
187             $tos[] = $to;
188         }
189
190         return $tos;
191     }
192
193     /**
194      * Return an array for the notices this notice is a reply to 
195      * suitable for serializing as JSON note objects.
196      *
197      * @return array the array of notes
198      */
199
200      function getInReplyToArray()
201      {
202          if (empty($this->replyToID) && empty($this->replyToUrl)) {
203              return null;
204          }
205
206          $replyToObj = array('objectType' => 'note');
207
208          // XXX: Possibly shorten this to just the numeric ID?
209          //      Currently, it's the full URI of the notice.
210          if (!empty($this->replyToID)) {
211              $replyToObj['id'] = $this->replyToID;
212          }
213          if (!empty($this->replyToUrl)) {
214              $replyToObj['url'] = $this->replyToUrl;
215          }
216
217          return $replyToObj;
218      }
219
220 }
221