]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SubMirror/actions/basemirror.php
SubMirror: check feel-url discovery if profile-url discovery failed; should help...
[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
49 abstract class BaseMirrorAction extends Action
50 {
51     var $user;
52     var $profile;
53
54     /**
55      * Check pre-requisites and instantiate attributes
56      *
57      * @param Array $args array of arguments (URL, GET, POST)
58      *
59      * @return boolean success flag
60      */
61
62     function prepare($args)
63     {
64         parent::prepare($args);
65         return $this->sharedBoilerplate();
66     }
67
68     protected function validateFeedUrl($url)
69     {
70         if (common_valid_http_url($url)) {
71             return $url;
72         } else {
73             $this->clientError(_m("Invalid feed 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, but we were unable to resolve the given URL to a working feed.
85         $this->clientError(_m("Invalid profile for mirroring."));
86     }
87
88     /**
89      *
90      * @param string $url
91      * @return Profile
92      */
93     protected function profileForFeed($url)
94     {
95         try {
96             // Maybe we got a web page?
97             $oprofile = Ostatus_profile::ensureProfileURL($url);
98         } catch (Exception $e) {
99             // Direct feed URL?
100             $oprofile = Ostatus_profile::ensureFeedURL($url);
101         }
102         if ($oprofile->isGroup()) {
103             $this->clientError(_m("Can't mirror a StatusNet group at this time."));
104         }
105         $this->oprofile = $oprofile; // @fixme ugly side effect :D
106         return $oprofile->localProfile();
107     }
108
109     /**
110      * @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
117         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
118             $this->clientError(_('This action only accepts POST requests.'));
119             return false;
120         }
121
122         // CSRF protection
123
124         $token = $this->trimmed('token');
125
126         if (!$token || $token != common_session_token()) {
127             $this->clientError(_('There was a problem with your session token.'.
128                                  ' Try again, please.'));
129             return false;
130         }
131
132         // Only for logged-in users
133
134         $this->user = common_current_user();
135
136         if (empty($this->user)) {
137             $this->clientError(_('Not logged in.'));
138             return false;
139         }
140         return true;
141     }
142
143     /**
144      * Handle request
145      *
146      * Does the subscription and returns results.
147      *
148      * @param Array $args unused.
149      *
150      * @return void
151      */
152
153     function handle($args)
154     {
155         // Throws exception on error
156         $this->saveMirror();
157
158         if ($this->boolean('ajax')) {
159             $this->startHTML('text/xml;charset=utf-8');
160             $this->elementStart('head');
161             $this->element('title', null, _('Subscribed'));
162             $this->elementEnd('head');
163             $this->elementStart('body');
164             $unsubscribe = new EditMirrorForm($this, $this->profile);
165             $unsubscribe->show();
166             $this->elementEnd('body');
167             $this->elementEnd('html');
168         } else {
169             $url = common_local_url('mirrorsettings');
170             common_redirect($url, 303);
171         }
172     }
173
174     abstract function saveMirror();
175 }