2 /** @file user.register
3 * User registration process
4 * Part of the user module, helper functions for the user registration process
7 function user_edit_form(&$u = NULL){
9 $form['edit_user']['username'] = array(
11 'text' => 'User Name',
12 'value' => (isset($u) ? $u['name'] : ''),
13 'attributes' => (isset($u) && (user_access('edit own username') || user_access('edit user accounts')) ? '' : 'readonly'),
17 $form['edit_user']['mail'] = array(
20 'help' => 'You will receive a confirmation email here.',
21 'value' => (isset($u) ? $u['mail'] : ''),
25 if(core_variable_get('user_sigs', 'no', 'user') == 'yes' && (core_variable_get('user_show_sig_register', 'no', 'user') == 'yes' || isset($u))){
26 $form['edit_user']['signature'] = array(
28 'text' => 'Signature',
29 'value' => (isset($u) ? $u['signature'] : ''),
35 if(user_access('edit user accounts')){
37 $form['edit_user']['active'] = array(
39 'text' =>'Acount is Active',
41 'checked' => ($u['status'] == 1 ? 'checked' : '')
44 foreach(user_roles() as $role){
46 $form['edit_user']['role_'.str_replace(' ', '_', $role['name'])] = array(
48 'text' => $role['name'],
49 'value' => $role['rid'],
50 'checked' => (in_array($role['name'], $u['roles']) ? 'checked' : ''),
57 $form['edit_user']['password_1'] = array(
60 'required' => (isset($u) ? 0 : 1)
63 $form['edit_user']['password_2'] = array(
65 'text' =>'Confirm Password',
66 'required' => (isset($u) ? 0 : 1)
69 $form['edit_user']['submit'] = array(
71 'value' => (isset($u) ? 'Submit Changes' : 'Register'),
74 if(user_access('users delete')){
75 $form['edit_user']['form_delete'] = array(
77 'value' => 'Delete User'
84 function _user_register_verify(&$the_user = NULL){
86 global $form_status, $db, $u;
88 if($_POST['password_1'] != $_POST['password_2']){
89 $form_status = 'with_errors';
90 system_warnings("The passwords don't match", 'error');
93 $user = mysql_fetch_assoc(mysql_query("SELECT name FROM ".$db['pre']."users WHERE name = '".$_POST['username']."'"));
96 //When creating a new account
97 if(!isset($the_user)){
98 $form_status = 'with_errors';
99 system_warnings("This user name is already in use, sorry :(", 'error');
101 //When modifying an account
102 elseif($the_user['name'] != $_POST['username']){
103 $form_status = 'with_errors';
104 system_warnings("This user name is already in use, sorry :(", 'error');
110 function _user_register(){
112 db_query("INSERT INTO {PRE_}users
113 (name, pass, mail, signature, signature_format, created, access, login, status, timezone, language, picture, data)
114 values ('".$_POST['username']."', '".md5($_POST['password_1'])."', '".$_POST['mail']."', '".$_POST['signature']."', 0, ".time().", 0, 0, 1, 0, '', '', '')");
116 path_redirect('user/login');
119 function user_register($mode = 'user'){
121 global $form_status, $user, $skin;
123 $skin['page_title'] = 'Register a new user';
125 $form = user_edit_form();
127 if(isset($_POST['form_id'])){
128 $u = _user_register_verify();
131 $content = form_form($form);
133 if($form_status == 'verified'){
134 //Do the actual user log in and get the roles
141 function user_modify(&$the_user){
143 global $user, $db, $x;
147 if(isset($_POST['password_1']) && $_POST['password_1'] != ''){
148 $pass = "pass = '".md5($_POST['password_1'])."',";
151 foreach(user_roles() as $r){
153 if(isset($_POST['role_'.$r['name']]))
154 $user_roles[] = $r['name'];
158 if(count($user_roles) > 0){
159 $user_roles = implode(',', $user_roles);
165 db_query("UPDATE {PRE_}users SET
166 name = '".$_POST['username']."',
168 mail = '".$_POST['mail']."',
169 signature = '".$_POST['signature']."',
170 status = ".(isset($_POST['active']) ? 1 : 0).",
171 roles = '".$user_roles."'
172 WHERE uid = ".$the_user['uid']."");
174 //Reload account if needed
175 if($user['uid'] == $the_user['uid']){
176 $u = user_load($user['uid']);
183 function user_delete(&$the_user){
187 if($the_user['uid'] == $user['uid']){
188 system_warnings('You can\'t delete your own account');
189 return 'Action not permited';
192 if($the_user['uid'] == 0 || $the_user['uid'] == 1){
193 system_warnings('This account can\'t be deleted');
194 return 'Action not permited';
197 db_query("DELETE FROM {PRE_}users WHERE uid = ".$the_user['uid']."");
199 path_redirect("user/admin");
203 function user_edit($user_id = NULL, $cat = 'account'){
205 global $form_status, $user, $skin, $the_user, $user_content;
207 //To edit other accounts
208 //We will work on $the_user to avoid problems
209 if(isset($user_id) && user_access('edit user accounts')){
210 $the_user = user_load($user_id);
214 elseif(user_access('edit own account')){
218 if($the_user['name'] == ''){
219 return '<h2>User account does not exist</h2>';
222 $skin['page_title'] = 'Edit Account for: ' . $the_user['name'];
224 user_tabs($the_user['uid']);
227 $user_content['cat'] = $cat;
228 hooks_invoke('user_edit');
230 if(isset($_POST['form_id'])){
231 _user_register_verify($the_user);
234 if($cat == 'account'){
235 $user_content['content'] = form_form(user_edit_form($the_user));
237 if($form_status == 'verified'){
238 user_modify($the_user);
241 if($form_status == 'delete_confirmed'){
242 return user_delete($the_user);
246 return $user_content['content'];