]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GeoURL/GeoURLPlugin.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / GeoURL / GeoURLPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugin to add ICBM metadata to HTML pages and report data to GeoURL.org
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  Action
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2009 StatusNet Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * Plugin to add ICBM metadata to HTML pages and report data to GeoURL.org
36  *
37  * Adds metadata to notice and profile pages that geourl.org and others
38  * understand. Also, pings geourl.org when a new notice is saved or
39  * a profile is changed.
40  *
41  * @category Plugin
42  * @package  StatusNet
43  * @author   Evan Prodromou <evan@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
46  *
47  * @seeAlso  Location
48  */
49 class GeoURLPlugin extends Plugin
50 {
51     public $ping = 'http://geourl.org/ping/';
52
53     /**
54      * Add extra <meta> headers for certain pages that geourl.org understands
55      *
56      * @param Action $action page being shown
57      *
58      * @return boolean event handler flag
59      */
60     function onEndShowHeadElements(Action $action)
61     {
62         $name = $action->trimmed('action');
63
64         $location = null;
65
66         if ($action instanceof ShowstreamAction) {
67             $profile = $action->getTarget();
68             if (!empty($profile->lat) && !empty($profile->lon)) {
69                 $location = $profile->lat . ', ' . $profile->lon;
70             }
71         } elseif ($action instanceof ShownoticeAction) {
72             // FIXME: getNotice in ShownoticeAction will do a new lookup, we should fix those classes
73             // so they can reliably just get a pre-stored notice object which was fetched in Shownotice prepare()...
74             $notice = $action->notice;
75             if ($notice instanceof Notice && !empty($notice->lat) && !empty($notice->lon)) {
76                 $location = $notice->lat . ', ' . $notice->lon;
77             }
78         }
79
80         if (!is_null($location)) {
81             $action->element('meta', array('name' => 'ICBM',
82                                            'content' => $location));
83             $action->element('meta', array('name' => 'DC.title',
84                                            'content' => $action->title()));
85         }
86
87         return true;
88     }
89
90     /**
91      * Report local notices to GeoURL.org when they're created
92      *
93      * @param Notice &$notice queued notice
94      *
95      * @return boolean event handler flag
96      */
97     function onHandleQueuedNotice(&$notice)
98     {
99         if (intval($notice->is_local) === Notice::LOCAL_PUBLIC) {
100
101             $request = HTTPClient::start();
102
103             $url = common_local_url('shownotice',
104                                     array('notice' => $notice->id));
105
106             try {
107                 $request->post($this->ping,
108                                null,
109                                array('p' => $url));
110             } catch (HTTP_Request2_Exception $e) {
111                 common_log(LOG_WARNING,
112                            "GeoURL.org ping failed for '$url' ($this->ping)");
113             }
114         }
115
116         return true;
117     }
118
119     function onPluginVersion(array &$versions)
120     {
121         $versions[] = array('name' => 'GeoURL',
122                             'version' => GNUSOCIAL_VERSION,
123                             'author' => 'Evan Prodromou',
124                             'homepage' => 'http://status.net/wiki/Plugin:GeoURL',
125                             'rawdescription' =>
126                             // TRANS: Plugin description.
127                             _m('Ping <a href="http://geourl.org/">GeoURL</a> when '.
128                                'new geolocation-enhanced notices are posted.'));
129         return true;
130     }
131 }