]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/favor.php
Merge branch 'master' into groups
[quix0rs-gnu-social.git] / actions / favor.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, 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
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/mail.php');
23 require_once INSTALLDIR.'/lib/disfavorform.php';
24
25 class FavorAction extends Action
26 {
27
28     function handle($args)
29     {
30         parent::handle($args);
31
32         if (!common_logged_in()) {
33             $this->clientError(_('Not logged in.'));
34             return;
35         }
36
37         $user = common_current_user();
38
39         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
40             common_redirect(common_local_url('showfavorites', array('nickname' => $user->nickname)));
41             return;
42         }
43
44         $id = $this->trimmed('notice');
45
46         $notice = Notice::staticGet($id);
47
48         # CSRF protection
49
50         $token = $this->trimmed('token-'.$notice->id);
51         if (!$token || $token != common_session_token()) {
52             $this->clientError(_("There was a problem with your session token. Try again, please."));
53             return;
54         }
55
56         if ($user->hasFave($notice)) {
57             $this->clientError(_('This notice is already a favorite!'));
58             return;
59         }
60
61         $fave = Fave::addNew($user, $notice);
62
63         if (!$fave) {
64             $this->serverError(_('Could not create favorite.'));
65             return;
66         }
67
68         $this->notify($fave, $notice, $user);
69         $user->blowFavesCache();
70         
71         if ($this->boolean('ajax')) {
72             $this->startHTML('text/xml;charset=utf-8', true);
73             $this->elementStart('head');
74             $this->element('title', null, _('Disfavor favorite'));
75             $this->elementEnd('head');
76             $this->elementStart('body');
77             $disfavor = new DisFavorForm($this, $notice);
78             $disfavor->show();
79             $this->elementEnd('body');
80             $this->elementEnd('html');
81         } else {
82             common_redirect(common_local_url('showfavorites',
83                                              array('nickname' => $user->nickname)));
84         }
85     }
86
87     function notify($fave, $notice, $user)
88     {
89         $other = User::staticGet('id', $notice->profile_id);
90         if ($other && $other->id != $user->id) {
91             if ($other->email && $other->emailnotifyfav) {
92                 mail_notify_fave($other, $user, $notice);
93             }
94             # XXX: notify by IM
95             # XXX: notify by SMS
96         }
97     }
98
99 }