nudge.php 4.22 KB
Newer Older
csarven's avatar
csarven committed
1
<?php
Robin Millette's avatar
Robin Millette committed
2 3 4 5 6 7 8

/**
 * User by ID action class.
 *
 * PHP version 5
 *
 * @category Action
9
 * @package  StatusNet
10 11
 * @author   Evan Prodromou <evan@status.net>
 * @author   Robin Millette <millette@status.net>
Robin Millette's avatar
Robin Millette committed
12
 * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
13
 * @link     http://status.net/
Robin Millette's avatar
Robin Millette committed
14
 *
15
 * StatusNet - the distributed open-source microblogging tool
16
 * Copyright (C) 2008, 2009, StatusNet, Inc.
csarven's avatar
csarven committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

32
if (!defined('STATUSNET') && !defined('LACONICA')) {
Robin Millette's avatar
Robin Millette committed
33 34
    exit(1);
}
csarven's avatar
csarven committed
35

Robin Millette's avatar
Robin Millette committed
36
require_once INSTALLDIR.'/lib/mail.php';
csarven's avatar
csarven committed
37

Robin Millette's avatar
Robin Millette committed
38 39 40 41
/**
 * Nudge a user action class.
 *
 * @category Action
42
 * @package  StatusNet
43 44 45
 * @author   Evan Prodromou <evan@status.net>
 * @author   Robin Millette <millette@status.net>
 * @author   Sarven Capadisli <csarven@status.net>
Robin Millette's avatar
Robin Millette committed
46
 * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
47
 * @link     http://status.net/
Robin Millette's avatar
Robin Millette committed
48
 */
49 50
class NudgeAction extends Action
{
Robin Millette's avatar
Robin Millette committed
51 52
     /**
     * Class handler.
53
     *
Robin Millette's avatar
Robin Millette committed
54 55 56 57
     * @param array $args array of arguments
     *
     * @return nothing
     */
58 59
    function handle($args)
    {
60
        parent::handle($args);
csarven's avatar
csarven committed
61

62
        if (!common_logged_in()) {
63
            // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
64
            $this->clientError(_('Not logged in.'));
65 66
            return;
        }
csarven's avatar
csarven committed
67

Robin Millette's avatar
Robin Millette committed
68
        $user  = common_current_user();
69
        $other = User::staticGet('nickname', $this->arg('nickname'));
millette's avatar
millette committed
70

71
        if ($_SERVER['REQUEST_METHOD'] != 'POST') {
Robin Millette's avatar
Robin Millette committed
72 73
            common_redirect(common_local_url('showstream',
                array('nickname' => $other->nickname)));
74 75
            return;
        }
millette's avatar
millette committed
76

Robin Millette's avatar
Robin Millette committed
77
        // CSRF protection
78
        $token = $this->trimmed('token');
79

80
        if (!$token || $token != common_session_token()) {
81
            // TRANS: Client error displayed when the session token does not match or is not given.
82
            $this->clientError(_('There was a problem with your session token. Try again, please.'));
83 84
            return;
        }
millette's avatar
millette committed
85 86

        if (!$other->email || !$other->emailnotifynudge) {
87
            // TRANS: Client error displayed trying to nudge a user that cannot be nudged.
88
            $this->clientError(_('This user doesn\'t allow nudges or hasn\'t confirmed or set their email address yet.'));
millette's avatar
millette committed
89 90 91
            return;
        }

92
        $this->notify($user, $other);
csarven's avatar
csarven committed
93

94
        if ($this->boolean('ajax')) {
95
            $this->startHTML('text/xml;charset=utf-8');
96
            $this->elementStart('head');
97
            // TRANS: Page title after sending a nudge.
98 99 100
            $this->element('title', null, _('Nudge sent'));
            $this->elementEnd('head');
            $this->elementStart('body');
101
            // TRANS: Confirmation text after sending a nudge.
102
            $this->element('p', array('id' => 'nudge_response'), _('Nudge sent!'));
103 104
            $this->elementEnd('body');
            $this->elementEnd('html');
105
        } else {
106
            // display a confirmation to the user
107
            common_redirect(common_local_url('showstream',
108 109
                                             array('nickname' => $other->nickname)),
                            303);
110 111
        }
    }
csarven's avatar
csarven committed
112

Robin Millette's avatar
Robin Millette committed
113 114 115 116 117 118 119 120
     /**
     * Do the actual notification
     *
     * @param class $user  nudger
     * @param class $other nudgee
     *
     * @return nothing
     */
121 122
    function notify($user, $other)
    {
123 124 125 126
        if ($other->id != $user->id) {
            if ($other->email && $other->emailnotifynudge) {
                mail_notify_nudge($user, $other);
            }
Robin Millette's avatar
Robin Millette committed
127 128
            // XXX: notify by IM
            // XXX: notify by SMS
129 130
        }
    }
131

132
    function isReadOnly($args)
133 134 135
    {
        return true;
    }
csarven's avatar
csarven committed
136
}