]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SubMirror/actions/basemirror.php
Merge branch 'jqueryui' into 1.0.x
[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             $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::staticGet('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, but we were unable to resolve the given URL to a working feed.
83         $this->clientError(_m("Invalid profile for mirroring."));
84     }
85
86     /**
87      *
88      * @param string $url
89      * @return Profile
90      */
91     protected function profileForFeed($url)
92     {
93         try {
94             // Maybe we got a web page?
95             $oprofile = Ostatus_profile::ensureProfileURL($url);
96         } catch (Exception $e) {
97             // Direct feed URL?
98             $oprofile = Ostatus_profile::ensureFeedURL($url);
99         }
100         if ($oprofile->isGroup()) {
101             $this->clientError(_m("Can't mirror a StatusNet group at this time."));
102         }
103         $this->oprofile = $oprofile; // @fixme ugly side effect :D
104         return $oprofile->localProfile();
105     }
106
107     /**
108      * @todo FIXME: none of this belongs in end classes
109      * this stuff belongs in shared code!
110      */
111     function sharedBoilerplate()
112     {
113         // Only allow POST requests
114         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
115             $this->clientError(_m('This action only accepts POST requests.'));
116             return false;
117         }
118
119         // CSRF protection
120         $token = $this->trimmed('token');
121
122         if (!$token || $token != common_session_token()) {
123             $this->clientError(_m('There was a problem with your session token.'.
124                                  ' Try again, please.'));
125             return false;
126         }
127
128         // Only for logged-in users
129
130         $this->user = common_current_user();
131
132         if (empty($this->user)) {
133             $this->clientError(_m('Not logged in.'));
134             return false;
135         }
136         return true;
137     }
138
139     /**
140      * Handle request
141      *
142      * Does the subscription and returns results.
143      *
144      * @param Array $args unused.
145      *
146      * @return void
147      */
148     function handle($args)
149     {
150         // Throws exception on error
151         $this->saveMirror();
152
153         if ($this->boolean('ajax')) {
154             $this->startHTML('text/xml;charset=utf-8');
155             $this->elementStart('head');
156             $this->element('title', null, _m('Subscribed'));
157             $this->elementEnd('head');
158             $this->elementStart('body');
159             $unsubscribe = new EditMirrorForm($this, $this->profile);
160             $unsubscribe->show();
161             $this->elementEnd('body');
162             $this->elementEnd('html');
163         } else {
164             $url = common_local_url('mirrorsettings');
165             common_redirect($url, 303);
166         }
167     }
168
169     abstract function saveMirror();
170 }