]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SubMirror/actions/basemirror.php
Merge branch 'master' into testing
[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('STATUSNET')) {
30     exit(1);
31 }
32
33 /**
34  * Takes parameters:
35  *
36  *    - feed: a profile ID
37  *    - token: session token to prevent CSRF attacks
38  *    - ajax: boolean; whether to return Ajax or full-browser results
39  *
40  * Only works if the current user is logged in.
41  *
42  * @category  Action
43  * @package   StatusNet
44  * @copyright 2010 StatusNet, Inc.
45  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
46  * @link      http://status.net/
47  */
48 abstract class BaseMirrorAction extends Action
49 {
50     var $user;
51     var $profile;
52
53     /**
54      * Check pre-requisites and instantiate attributes
55      *
56      * @param Array $args array of arguments (URL, GET, POST)
57      *
58      * @return boolean success flag
59      */
60     function prepare($args)
61     {
62         parent::prepare($args);
63         return $this->sharedBoilerplate();
64     }
65
66     protected function validateFeedUrl($url)
67     {
68         if (common_valid_http_url($url)) {
69             return $url;
70         } else {
71             // TRANS: Client error displayed when entering an invalid URL for a feed.
72             // TRANS: %s is the invalid feed URL.
73             $this->clientError(sprintf(_m("Invalid feed URL: %s."), $url));
74         }
75     }
76
77     protected function validateProfile($id)
78     {
79         $id = intval($id);
80         $profile = Profile::staticGet('id', $id);
81         if ($profile && $profile->id != $this->user->id) {
82             return $profile;
83         }
84         // TRANS: Error message returned to user when setting up feed mirroring,
85         // TRANS: but we were unable to resolve the given URL to a working feed.
86         $this->clientError(_m('Invalid profile for mirroring.'));
87     }
88
89     /**
90      *
91      * @param string $url
92      * @return Profile
93      */
94     protected function profileForFeed($url)
95     {
96         try {
97             // Maybe we got a web page?
98             $oprofile = Ostatus_profile::ensureProfileURL($url);
99         } catch (Exception $e) {
100             // Direct feed URL?
101             $oprofile = Ostatus_profile::ensureFeedURL($url);
102         }
103         if ($oprofile->isGroup()) {
104             // TRANS: Client error displayed when trying to mirror a StatusNet group feed.
105             $this->clientError(_m('Cannot mirror a StatusNet group at this time.'));
106         }
107         $this->oprofile = $oprofile; // @todo FIXME: ugly side effect :D
108         return $oprofile->localProfile();
109     }
110
111     /**
112      * @todo FIXME: none of this belongs in end classes
113      * this stuff belongs in shared code!
114      */
115     function sharedBoilerplate()
116     {
117         // Only allow POST requests
118         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
119             // TRANS: Client error displayed when trying to use another method than POST.
120             $this->clientError(_m('This action only accepts POST requests.'));
121             return false;
122         }
123
124         // CSRF protection
125         $token = $this->trimmed('token');
126
127         if (!$token || $token != common_session_token()) {
128             // TRANS: Client error displayed when the session token does not match or is not given.
129             $this->clientError(_m('There was a problem with your session token.'.
130                                  ' Try again, please.'));
131             return false;
132         }
133
134         // Only for logged-in users
135
136         $this->user = common_current_user();
137
138         if (empty($this->user)) {
139             // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
140             $this->clientError(_m('Not logged in.'));
141             return false;
142         }
143         return true;
144     }
145
146     /**
147      * Handle request
148      *
149      * Does the subscription and returns results.
150      *
151      * @param Array $args unused.
152      *
153      * @return void
154      */
155     function handle($args)
156     {
157         // Throws exception on error
158         $this->saveMirror();
159
160         if ($this->boolean('ajax')) {
161             $this->startHTML('text/xml;charset=utf-8');
162             $this->elementStart('head');
163             // TRANS: Page title for subscribed feed mirror.
164             $this->element('title', null, _m('Subscribed'));
165             $this->elementEnd('head');
166             $this->elementStart('body');
167             $unsubscribe = new EditMirrorForm($this, $this->profile);
168             $unsubscribe->show();
169             $this->elementEnd('body');
170             $this->elementEnd('html');
171         } else {
172             $url = common_local_url('mirrorsettings');
173             common_redirect($url, 303);
174         }
175     }
176
177     abstract function saveMirror();
178 }