]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SubMirror/actions/editmirror.php
Removed plugin Google-Analytics as this is free/libre and decentralized
[quix0rs-gnu-social.git] / plugins / SubMirror / actions / editmirror.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 class EditMirrorAction extends BaseMirrorAction
47 {
48     /**
49      * Check pre-requisites and instantiate attributes
50      *
51      * @param Array $args array of arguments (URL, GET, POST)
52      *
53      * @return boolean success flag
54      */
55     protected function prepare(array $args=array())
56     {
57         parent::prepare($args);
58
59         $this->profile = $this->validateProfile($this->trimmed('profile'));
60
61         $this->mirror = SubMirror::pkeyGet(array('subscriber' => $this->user->id,
62                                                  'subscribed' => $this->profile->id));
63
64         if (!$this->mirror instanceof SubMirror) {
65             // TRANS: Client error displayed when trying to edit an object that is not a feed mirror.
66             $this->clientError(_m('Requested invalid profile to edit.'));
67         }
68
69         $this->style = $this->validateStyle($this->trimmed('style'));
70
71         // DO NOT change to $this->boolean(), it will be wrong.
72         // We're checking for the presence of the setting, not its value.
73         $this->delete = (bool)$this->arg('delete');
74
75         return true;
76     }
77
78     protected function validateStyle($style)
79     {
80         $allowed = array('repeat', 'copy');
81         if (in_array($style, $allowed)) {
82             return $style;
83         } else {
84             // TRANS: Client error displayed when providing invalid input when editing a mirror.
85             $this->clientError(_m('Bad form data.'));
86         }
87     }
88
89     protected function saveMirror()
90     {
91         $mirror = SubMirror::getMirror($this->user, $this->profile);
92         if (!$mirror) {
93             // TRANS: Client error thrown when a mirror request is made and no result is retrieved.
94             $this->clientError(_m('The mirror request failed, because no result was retrieved.'));
95         }
96
97         if ($this->delete) {
98             $mirror->delete();
99             $oprofile = Ostatus_profile::getKV('profile_id', $this->profile->id);
100             if ($oprofile) {
101                 $oprofile->garbageCollect();
102             }
103         } else if ($this->style != $mirror->style) {
104             $orig = clone($mirror);
105             $mirror->style = $this->style;
106             $mirror->modified = common_sql_now();
107             $mirror->update($orig);
108         }
109     }
110 }