]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SubMirror/actions/basemirror.php
Merge branch 'threaded_replies_nightly' into 'nightly'
[quix0rs-gnu-social.git] / plugins / SubMirror / actions / basemirror.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  * PHP version 5
20  *
21  * @category  Action
22  * @package   StatusNet
23  * @author    Brion Vibber <brion@status.net>
24  * @copyright 2010 StatusNet, Inc.
25  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
26  * @link      http://status.net/
27  */
28
29 if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
30
31 /**
32  * Takes parameters:
33  *
34  *    - feed: a profile ID
35  *    - token: session token to prevent CSRF attacks
36  *    - ajax: boolean; whether to return Ajax or full-browser results
37  *
38  * Only works if the current user is logged in.
39  *
40  * @category  Action
41  * @package   StatusNet
42  * @copyright 2010 StatusNet, Inc.
43  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
44  * @link      http://status.net/
45  */
46 abstract class BaseMirrorAction extends Action
47 {
48     var $user;
49     var $profile;
50
51     /**
52      * Check pre-requisites and instantiate attributes
53      *
54      * @param Array $args array of arguments (URL, GET, POST)
55      *
56      * @return boolean success flag
57      */
58     protected function prepare(array $args=array())
59     {
60         parent::prepare($args);
61         return $this->sharedBoilerplate();
62     }
63
64     protected function validateFeedUrl($url)
65     {
66         if (common_valid_http_url($url)) {
67             return $url;
68         } else {
69             // TRANS: Client error displayed when entering an invalid URL for a feed.
70             // TRANS: %s is the invalid feed URL.
71             $this->clientError(sprintf(_m("Invalid feed URL: %s."), $url));
72         }
73     }
74
75     protected function validateProfile($id)
76     {
77         $id = intval($id);
78         $profile = Profile::getKV('id', $id);
79         if ($profile && $profile->id != $this->user->id) {
80             return $profile;
81         }
82         // TRANS: Error message returned to user when setting up feed mirroring,
83         // TRANS: but we were unable to resolve the given URL to a working feed.
84         $this->clientError(_m('Invalid profile for mirroring.'));
85     }
86
87     /**
88      *
89      * @param string $url
90      * @return Profile
91      */
92     protected function profileForFeed($url)
93     {
94         try {
95             // Maybe we got a web page?
96             $oprofile = Ostatus_profile::ensureProfileURL($url);
97         } catch (Exception $e) {
98             // Direct feed URL?
99             $oprofile = Ostatus_profile::ensureFeedURL($url);
100         }
101         if ($oprofile->isGroup()) {
102             // TRANS: Client error displayed when trying to mirror a GNU social group feed.
103             $this->clientError(_m('Cannot mirror a GNU social group at this time.'));
104         }
105         $this->oprofile = $oprofile; // @todo FIXME: ugly side effect :D
106         return $oprofile->localProfile();
107     }
108
109     /**
110      * @todo FIXME: none of this belongs in end classes
111      * this stuff belongs in shared code!
112      */
113     function sharedBoilerplate()
114     {
115         // Only allow POST requests
116         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
117             // TRANS: Client error displayed when trying to use another method than POST.
118             $this->clientError(_m('This action only accepts POST requests.'));
119         }
120
121         // CSRF protection
122         $token = $this->trimmed('token');
123
124         if (!$token || $token != common_session_token()) {
125             // TRANS: Client error displayed when the session token does not match or is not given.
126             $this->clientError(_m('There was a problem with your session token.'.
127                                  ' Try again, please.'));
128         }
129
130         // Only for logged-in users
131
132         $this->user = common_current_user();
133
134         if (empty($this->user)) {
135             // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
136             $this->clientError(_m('Not logged in.'));
137         }
138         return true;
139     }
140
141     /**
142      * Handle request
143      *
144      * Does the subscription and returns results.
145      *
146      * @param Array $args unused.
147      *
148      * @return void
149      */
150     protected function handle()
151     {
152         // Throws exception on error
153         $this->saveMirror();
154
155         if ($this->boolean('ajax')) {
156             $this->startHTML('text/xml;charset=utf-8');
157             $this->elementStart('head');
158             // TRANS: Page title for subscribed feed mirror.
159             $this->element('title', null, _m('Subscribed'));
160             $this->elementEnd('head');
161             $this->elementStart('body');
162             $unsubscribe = new EditMirrorForm($this, $this->profile);
163             $unsubscribe->show();
164             $this->elementEnd('body');
165             $this->endHTML();
166         } else {
167             $url = common_local_url('mirrorsettings');
168             common_redirect($url, 303);
169         }
170     }
171
172     abstract protected function saveMirror();
173 }