]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SubMirror/actions/addmirror.php
df6939491fdbf595ba8a5b9c51e96933f087e55d
[quix0rs-gnu-social.git] / plugins / SubMirror / actions / addmirror.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 class AddMirrorAction extends Action
50 {
51     var $user;
52     var $feedurl;
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         $ok = $this->sharedBoilerplate();
66         if ($ok) {
67             // and now do something useful!
68             $this->profile = $this->validateProfile($this->trimmed('profile'));
69             return true;
70         } else {
71             return $ok;
72         }
73     }
74
75     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      * @fixme none of this belongs in end classes
88      * this stuff belongs in shared code!
89      */
90     function sharedBoilerplate()
91     {
92         // Only allow POST requests
93
94         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
95             $this->clientError(_('This action only accepts POST requests.'));
96             return false;
97         }
98
99         // CSRF protection
100
101         $token = $this->trimmed('token');
102
103         if (!$token || $token != common_session_token()) {
104             $this->clientError(_('There was a problem with your session token.'.
105                                  ' Try again, please.'));
106             return false;
107         }
108
109         // Only for logged-in users
110
111         $this->user = common_current_user();
112
113         if (empty($this->user)) {
114             $this->clientError(_('Not logged in.'));
115             return false;
116         }
117         return true;
118     }
119
120     /**
121      * Handle request
122      *
123      * Does the subscription and returns results.
124      *
125      * @param Array $args unused.
126      *
127      * @return void
128      */
129
130     function handle($args)
131     {
132         // Throws exception on error
133         $this->saveMirror();
134
135         if ($this->boolean('ajax')) {
136             $this->startHTML('text/xml;charset=utf-8');
137             $this->elementStart('head');
138             $this->element('title', null, _('Subscribed'));
139             $this->elementEnd('head');
140             $this->elementStart('body');
141             $unsubscribe = new EditMirrorForm($this, $this->profile);
142             $unsubscribe->show();
143             $this->elementEnd('body');
144             $this->elementEnd('html');
145         } else {
146             $url = common_local_url('mirrorsettings');
147             common_redirect($url, 303);
148         }
149     }
150
151     function saveMirror()
152     {
153         SubMirror::saveMirror($this->user, $this->profile);
154     }
155 }