EmailRegistrationPlugin.php 5.76 KB
Newer Older
1 2 3
<?php
/**
 * StatusNet - the distributed open-source microblogging tool
4
 * Copyright (C) 2011, StatusNet, Inc.
5
 *
6
 * Email-based registration, as on the StatusNet OnDemand service
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * PHP version 5
 *
 * 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/>.
 *
23
 * @category  Email registration
24 25
 * @package   StatusNet
 * @author    Evan Prodromou <evan@status.net>
26
 * @copyright 2011 StatusNet, Inc.
27 28 29 30 31 32 33 34 35 36 37
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
 * @link      http://status.net/
 */

if (!defined('STATUSNET')) {
    // This check helps protect against security problems;
    // your code file can't be executed directly from the web.
    exit(1);
}

/**
38
 * Email based registration plugin
39
 *
40
 * @category  Email registration
41 42 43
 * @package   StatusNet
 * @author    Brion Vibber <brionv@status.net>
 * @author    Evan Prodromou <evan@status.net>
44
 * @copyright 2011 StatusNet, Inc.
45 46 47
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
 * @link      http://status.net/
 */
48 49
class EmailRegistrationPlugin extends Plugin
{
50 51
    const CONFIRMTYPE = 'register';

52
    function onArgsInitialize(&$args)
53
    {
54 55 56
        if (array_key_exists('action', $args) && $args['action'] == 'register') {
            // YOINK!
            $args['action'] = 'emailregister';
57
        }
58 59 60
        return true;
    }

61 62 63 64 65 66 67 68 69
    function onLoginAction($action, &$login)
    {
        if ($action == 'emailregister') {
            $login = true;
            return false;
        }
        return true;
    }

70 71 72 73
    function onStartLoadDoc(&$title, &$output)
    {
        $dir = dirname(__FILE__);

74
        // @todo FIXME: i18n issue.
75 76 77 78 79 80 81 82 83 84
        $docFile = DocFile::forTitle($title, $dir.'/doc-src/');

        if (!empty($docFile)) {
            $output = $docFile->toHTML();
            return false;
        }

        return true;
    }

85 86
    static function registerEmail($email)
    {
87
        $old = User::getKV('email', $email);
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

        if (!empty($old)) {
            // TRANS: Error text when trying to register with an already registered e-mail address.
            // TRANS: %s is the URL to recover password at.
            throw new ClientException(sprintf(_m('A user with that email address already exists. You can use the '.
                                                 '<a href="%s">password recovery</a> tool to recover a missing password.'),
                                              common_local_url('recoverpassword')));
        }

        $valid = false;

        if (Event::handle('StartValidateUserEmail', array(null, $email, &$valid))) {
            $valid = Validate::email($email, common_config('email', 'check_domain'));
            Event::handle('EndValidateUserEmail', array(null, $email, &$valid));
        }

        if (!$valid) {
            // TRANS: Error text when trying to register with an invalid e-mail address.
            throw new ClientException(_m('Not a valid email address.'));
        }

        $confirm = Confirm_address::getAddress($email, self::CONFIRMTYPE);

        if (empty($confirm)) {
            $confirm = Confirm_address::saveNew(null, $email, 'register');
        }

        return $confirm;
    }

    static function nicknameFromEmail($email)
    {
        $parts = explode('@', $email);

        $nickname = $parts[0];

        $nickname = preg_replace('/[^A-Za-z0-9]/', '', $nickname);

        $nickname = Nickname::normalize($nickname);

        $original = $nickname;

        $n = 0;

132
        while (User::getKV('nickname', $nickname)) {
133 134 135 136 137 138 139
            $n++;
            $nickname = $original . $n;
        }

        return $nickname;
    }

140
    static function sendConfirmEmail($confirm, $title=null)
141 142 143 144 145 146 147 148 149
    {
        $sitename = common_config('site', 'name');

        $recipients = array($confirm->address);

        $headers['From'] = mail_notify_from();
        $headers['To'] = trim($confirm->address);
         // TRANS: Subject for confirmation e-mail.
         // TRANS: %s is the StatusNet sitename.
150 151
        $headers['Subject'] = sprintf(_m('Welcome to %s'), $sitename);
        $headers['Content-Type'] = 'text/html; charset=UTF-8';
152 153 154

        $confirmUrl = common_local_url('register', array('code' => $confirm->code));

155 156 157 158 159 160 161
        if (empty($title)) {
            $title = 'confirmemailreg';
        }

        $confirmTemplate = DocFile::forTitle($title, DocFile::mailPaths());

        $body = $confirmTemplate->toHTML(array('confirmurl' => $confirmUrl));
162 163 164 165

        mail_send($recipients, $headers, $body);
    }

166 167 168 169 170 171 172 173 174
    function onEndDocFileForTitle($title, $paths, &$filename)
    {
        if ($title == 'confirmemailreg' && empty($filename)) {
            $filename = dirname(__FILE__).'/mail-src/'.$title;
            return false;
        }
        return true;
    }

175
    function onPluginVersion(array &$versions)
176
    {
177
        $versions[] = array('name' => 'EmailRegistration',
178
                            'version' => GNUSOCIAL_VERSION,
179 180
                            'author' => 'Evan Prodromou',
                            'homepage' => 'http://status.net/wiki/Plugin:EmailRegistration',
181
                            'rawdescription' =>
182 183
                            // TRANS: Plugin description.
                            _m('Use email only for registration.'));
184 185 186
        return true;
    }
}