3 /* GNU FM -- a free network service for sharing your music listening habits
5 Copyright (C) 2009 Free Software Foundation, Inc
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Affero General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Affero General Public License for more details.
17 You should have received a copy of the GNU Affero General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 require_once('database.php');
23 require_once('templating.php');
24 require_once('utils/EmailAddressValidator.php');
26 if ($logged_in == true) {
27 header('Location: index.php');
31 if ($registration_disabled == true) {
32 displayError("Registration disabled", "Registration has been disabled by the site owner, sorry!");
35 function sendEmail($to, $subject, $message) {
37 $foo = parse_url($base_url);
39 $domain = $foo['host'];
41 $headers = 'From: do-not-reply@' . $domain . "\r\n" .
42 'Reply-To: do-not-reply@' . $domain . "\r\n" .
45 mail($to, $subject, $message, $headers);
48 if (isset($_GET['auth'])) {
49 $authcode = $_GET['auth'];
50 $adodb->SetFetchMode(ADODB_FETCH_ASSOC);
52 $row = $adodb->GetRow('SELECT * FROM AccountActivation WHERE authcode = ' . $adodb->qstr($authcode));
53 } catch (Exception $e) {
54 displayError("Error", "Unknown activation code.");
57 $sql_update = 'UPDATE Users SET active = 1 WHERE username = ' . $adodb->qstr($row['username']);
58 $sql_delete = 'DELETE FROM AccountActivation WHERE authcode = ' . $adodb->qstr($authcode);
60 $res = $adodb->Execute($sql_update);
61 $res = $adodb->Execute($sql_delete);
62 } catch (Exception $e) {
63 displayError("Error", $e->getMessage());
65 $smarty->assign('activated', true);
68 if (isset($_POST['register'])) {
71 $username = $_POST['username'];
72 $password = $_POST['password'];
73 $passwordrepeat = $_POST['password-repeat'];
74 $fullname = $_POST['fullname'];
75 $email = $_POST['email'];
76 $location = $_POST['location'];
81 if (!preg_match('/^[a-zA-Z0-9][a-zA-Z0-9_-]{1,14}[a-zA-Z0-9]$/', $username)) {
82 $errors .= 'Your username must be at least 3 characters in length (max 16) and only consist of <i>a-z, A-Z, 0-9</i> and _ (underscore), and may not begin or end with an underscore.<br />';
84 if (empty($password)) {
85 $errors .= 'You must enter a password.<br />';
87 if ($password != $passwordrepeat) {
88 $errors .= 'Your passwords do not match.<br />';
91 $errors .= 'You must enter an e-mail address.<br />';
93 $validator = new EmailAddressValidator();
94 if (!$validator->check_email_address($email)) {
95 $errors .= 'You must provide a valid email address!<br />';
99 //Check this username is available
101 $res = $adodb->GetOne('SELECT username FROM Users WHERE lower(username) = lower(' . $adodb->qstr($username) . ')');
102 } catch (Exception $e) {
103 $errors .= 'Database error.<br />';
106 $errors .= 'Sorry, that username is already registered.<br />';
109 if (empty($errors)) {
111 $sql = 'INSERT INTO Users (username, password, email, fullname, bio, location, created, active) VALUES ('
112 . $adodb->qstr($username) . ', '
113 . $adodb->qstr(md5($password)) . ', '
114 . $adodb->qstr($email) . ', '
115 . $adodb->qstr($fullname) . ', '
116 . $adodb->qstr($bio) . ', '
117 . $adodb->qstr($location) . ', '
120 $insert = $adodb->Execute($sql);
121 } catch (Exception $e) {
122 reportError('Create user, insert, register.php', $e->getMessage());
123 displayError("Error", $e->getMessage());
126 $code = md5($username . time());
127 $sql = 'INSERT INTO AccountActivation (username, authcode, expires) VALUES('
128 . $adodb->qstr($username) . ', '
129 . $adodb->qstr($code) . ', '
130 . (time() + (86400 * 2)) . ')';
132 $res = $adodb->Execute($sql);
133 } catch (Exception $e) {
134 reportError('AccountActivation, insert, register.php', $e->getMessage());
135 displayError("Error", $e->getMessage());
138 $url = $base_url . '/register.php?auth=' . $code;
139 $content = "Hi!\n\nSomeone registered an account "
140 . "at " . $base_url . ". If this was you, please visit the webpage specified below to activate "
141 . "your account within 48 hours, after which time all information provided by you and "
142 . "your activation code will be permanently deleted from our database. If you do not want to activate your account, "
143 . "please disregard this email.\n\n" . $url . "\n\n- The " . $site_name . " Team";
144 $subject = $site_name . ' Account Activation - Action needed!';
145 sendEmail($email, $subject, $content);
147 $smarty->assign('registered', true);
149 $smarty->assign('username', $username);
150 $smarty->assign('fullname', $fullname);
151 $smarty->assign('email', $email);
152 $smarty->assign('location', $location);
153 $smarty->assign('bio', $bio);
154 $smarty->assign('errors', $errors);
158 $smarty->display('register.tpl');