]> projects.mako.cc - scuttle/blob - register.php
updated readme with information on a series of bugs I know exist
[scuttle] / register.php
1 <?php
2 /***************************************************************************
3 Copyright (c) 2004 - 2010 Marcus Campbell
4 http://scuttle.org/
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 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 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 ***************************************************************************/
20
21 require_once 'header.inc.php';
22 $sf = new ServiceFactory();
23 $userservice     =& $sf->getServiceInstance('UserService');
24 $templateservice =& $sf->getServiceInstance('TemplateService');
25
26 $tplVars   = array();
27 $completed = FALSE;
28
29 if ($_POST['submitted']) {
30   if (!$completed) {  
31     $posteduser = trim(utf8_strtolower($_POST['username']));
32     $postedpass = trim($_POST['password']);
33     $postedconf = trim($_POST['passconf']);
34     $postedmail = trim($_POST['email']);
35
36     // Check token
37     if (!isset($_SESSION['token']) || $_POST['token'] != $_SESSION['token']) {
38       $tplVars['error'] = T_('Form could not be authenticated. Please try again.');
39     }
40
41     // Check elapsed time
42     if (!isset($_SESSION['token_time']) || time() - $_SESSION['token_time'] < 1) {
43       $tplVars['error'] = T_('Form was submitted too quickly. Please wait before trying again.');
44     }
45
46     // Check if form is incomplete
47     elseif (!$posteduser || !$postedpass || !$postedmail) {
48       $tplVars['error'] = T_('You <em>must</em> enter a username, password and e-mail address.');
49     }
50
51     // Check if username is reserved
52     elseif ($userservice->isReserved($posteduser)) {
53       $tplVars['error'] = T_('This username has been reserved, please make another choice.');
54     }
55
56     // Check if username already exists
57     elseif ($userservice->getUserByUsername($posteduser)) {
58       $tplVars['error'] = T_('This username already exists, please make another choice.');
59     }
60     
61     // Check that password is long enough
62     elseif ($postedpass != '' && strlen($postedpass) < 6) {
63       $tplVars['error'] = T_('Password must be at least 6 characters long.');       
64     }
65
66     // Check if password matches confirmation
67     elseif ($postedpass != $postedconf) {
68       $tplVars['error'] = T_('Password and confirmation do not match.');
69     }
70
71     // Check if e-mail address is blocked
72     elseif ($userservice->isBlockedEmail($postedmail)) {
73       $tplVars['error'] = T_('This e-mail address is not permitted.');
74     }
75
76     // Check if e-mail address is valid
77     elseif (!$userservice->isValidEmail($postedmail)) {
78       $tplVars['error'] = T_('E-mail address is not valid. Please try again.');
79     }
80
81     // Register details
82     elseif ($userservice->addUser($posteduser, $_POST['password'], $postedmail)) {
83       // Log in with new username
84       $login = $userservice->login($posteduser, $_POST['password']);
85       if ($login) {
86         header('Location: '. createURL('bookmarks', $posteduser));
87       }
88       $tplVars['msg'] = T_('You have successfully registered. Enjoy!');
89     }
90     else {
91       $tplVars['error'] = T_('Registration failed. Please try again.');
92     }
93   }
94   else {
95     $tplVars['msg'] = T_('Woah there, go easy on the Register button! Your registration was successful. Check your e-mail for instructions on how to verify your account.');  
96   }
97 }
98
99 // Generate anti-CSRF token
100 $token = md5(uniqid(rand(), TRUE));
101 $_SESSION['token']      = $token;
102 $_SESSION['token_time'] = time();
103
104 $tplVars['loadjs']     = TRUE;
105 $tplVars['subtitle']   = T_('Register');
106 $tplVars['formaction'] = createURL('register');
107 $tplVars['token']      = $token;
108 $templateservice->loadTemplate('register.tpl', $tplVars);

Benjamin Mako Hill || Want to submit a patch?