]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Mapstraction/map.php
Fix for ticket 2756 - Calls to OAuth endpoints are redirected to the
[quix0rs-gnu-social.git] / plugins / Mapstraction / map.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show a map of user's notices
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  Mapstraction
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  * Show a map of notices
36  *
37  * @category Mapstraction
38  * @package  StatusNet
39  * @author   Evan Prodromou <evan@status.net>
40  * @author   Craig Andrews <candrews@integralblue.com>
41  * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  */
45
46 class MapAction extends OwnerDesignAction
47 {
48     var $profile = null;
49     var $page    = null;
50     var $notices = null;
51
52     function prepare($args)
53     {
54         parent::prepare($args);
55
56         $nickname_arg = $this->arg('nickname');
57         $nickname     = common_canonical_nickname($nickname_arg);
58
59         // Permanent redirect on non-canonical nickname
60
61         if ($nickname_arg != $nickname) {
62             $args = array('nickname' => $nickname);
63             if ($this->arg('page') && $this->arg('page') != 1) {
64                 $args['page'] = $this->arg['page'];
65             }
66             common_redirect(common_local_url($this->trimmed('action'), $args), 301);
67             return false;
68         }
69
70         $this->user = User::staticGet('nickname', $nickname);
71
72         if (!$this->user) {
73             $this->clientError(_m('No such user.'), 404);
74             return false;
75         }
76
77         $this->profile = $this->user->getProfile();
78
79         if (!$this->profile) {
80             $this->serverError(_m('User has no profile.'));
81             return false;
82         }
83
84         $page = $this->trimmed('page');
85
86         if (!empty($page) && Validate::number($page)) {
87             $this->page = $page+0;
88         } else {
89             $this->page = 1;
90         }
91
92         $this->notices = empty($this->tag)
93           ? $this->user->getNotices(($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1)
94             : $this->user->getTaggedNotices($this->tag, ($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1, 0, 0, null);
95
96         return true;
97     }
98
99     function handle($args)
100     {
101         parent::handle($args);
102         $this->showPage();
103     }
104
105     function showContent()
106     {
107         $this->element('div', array('id' => 'map_canvas',
108                                     'class' => 'gray smallmap',
109                                     'style' => "width: 100%; height: 400px"));
110     }
111
112     /**
113      * Hook for adding extra JavaScript
114      *
115      * @param Action $action Action object for the page
116      *
117      * @return boolean event handler return
118      */
119
120     function showScripts()
121     {
122         parent::showScripts();
123         $jsonArray = array();
124
125         while ($this->notice->fetch()) {
126             if (!empty($this->notice->lat) && !empty($this->notice->lon)) {
127                 $jsonNotice = $this->noticeAsJson($this->notice);
128                 $jsonArray[] = $jsonNotice;
129             }
130         }
131
132         $this->inlineScript('$(document).ready(function() { '.
133                             ' var _notices = ' . json_encode($jsonArray).'; ' .
134                             'showMapstraction($("#map_canvas"), _notices); });');
135
136         return true;
137     }
138
139     function noticeAsJson($notice)
140     {
141         // FIXME: this code should be abstracted to a neutral third
142         // party, like Notice::asJson(). I'm not sure of the ethics
143         // of refactoring from within a plugin, so I'm just abusing
144         // the ApiAction method. Don't do this unless you're me!
145
146         $act = new ApiAction('/dev/null');
147
148         $arr = $act->twitterStatusArray($notice, true);
149         $arr['url'] = $notice->bestUrl();
150         $arr['html'] = $notice->rendered;
151         $arr['source'] = $arr['source'];
152
153         if (!empty($notice->reply_to)) {
154             $reply_to = Notice::staticGet('id', $notice->reply_to);
155             if (!empty($reply_to)) {
156                 $arr['in_reply_to_status_url'] = $reply_to->bestUrl();
157             }
158             $reply_to = null;
159         }
160
161         $profile = $notice->getProfile();
162         $arr['user']['profile_url'] = $profile->profileurl;
163
164         return $arr;
165     }
166 }