]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/favor.php
change Controlez-Vous to Control Yourself
[quix0rs-gnu-social.git] / actions / favor.php
1 <?php
2
3 /**
4  * Favor action.
5  *
6  * PHP version 5
7  *
8  * @category Action
9  * @package  Laconica
10  * @author   Evan Prodromou <evan@controlyourself.ca>
11  * @author   Robin Millette <millette@controlyourself.ca>
12  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
13  * @link     http://laconi.ca/
14  *
15
16 /*
17  * Laconica - a distributed open-source microblogging tool
18  * Copyright (C) 2008, Control Yourself, Inc.
19  *
20  * This program is free software: you can redistribute it and/or modify
21  * it under the terms of the GNU Affero General Public License as published by
22  * the Free Software Foundation, either version 3 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU Affero General Public License for more details.
29  *
30  * You should have received a copy of the GNU Affero General Public License
31  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32  */
33
34 if (!defined('LACONICA')) {
35     exit(1);
36 }
37
38 require_once INSTALLDIR.'/lib/mail.php';
39 require_once INSTALLDIR.'/lib/disfavorform.php';
40
41 /**
42  * Favor class.
43  *
44  * @category Action
45  * @package  Laconica
46  * @author   Evan Prodromou <evan@controlyourself.ca>
47  * @author   Robin Millette <millette@controlyourself.ca>
48  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
49  * @link     http://laconi.ca/
50  */
51 class FavorAction extends Action
52 {
53     /**
54      * Class handler.
55      *
56      * @param array $args query arguments
57      *
58      * @return void
59      */
60     function handle($args)
61     {
62         parent::handle($args);
63         if (!common_logged_in()) {
64             $this->clientError(_('Not logged in.'));
65             return;
66         }
67         $user = common_current_user();
68         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
69             common_redirect(common_local_url('showfavorites',
70                 array('nickname' => $user->nickname)));
71             return;
72         }
73         $id     = $this->trimmed('notice');
74         $notice = Notice::staticGet($id);
75         $token  = $this->trimmed('token-'.$notice->id);
76         if (!$token || $token != common_session_token()) {
77             $this->clientError(_("There was a problem with your session token. Try again, please."));
78             return;
79         }
80         if ($user->hasFave($notice)) {
81             $this->clientError(_('This notice is already a favorite!'));
82             return;
83         }
84         $fave = Fave::addNew($user, $notice);
85         if (!$fave) {
86             $this->serverError(_('Could not create favorite.'));
87             return;
88         }
89         $this->notify($notice, $user);
90         $user->blowFavesCache();
91         if ($this->boolean('ajax')) {
92             $this->startHTML('text/xml;charset=utf-8');
93             $this->elementStart('head');
94             $this->element('title', null, _('Disfavor favorite'));
95             $this->elementEnd('head');
96             $this->elementStart('body');
97             $disfavor = new DisFavorForm($this, $notice);
98             $disfavor->show();
99             $this->elementEnd('body');
100             $this->elementEnd('html');
101         } else {
102             common_redirect(common_local_url('showfavorites',
103                                              array('nickname' => $user->nickname)),
104                             303);
105         }
106     }
107
108     /**
109      * Notifies a user when his notice is favorited.
110      *
111      * @param class $notice favorited notice
112      * @param class $user   user declaring a favorite
113      *
114      * @return void
115      */
116     function notify($notice, $user)
117     {
118         $other = User::staticGet('id', $notice->profile_id);
119         if ($other && $other->id != $user->id) {
120             if ($other->email && $other->emailnotifyfav) {
121                 mail_notify_fave($other, $user, $notice);
122             }
123             // XXX: notify by IM
124             // XXX: notify by SMS
125         }
126     }
127 }
128