/[drupal]/drupal/modules/user/user.pages.inc
ViewVC logotype

Contents of /drupal/modules/user/user.pages.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.61 - (show annotations) (download) (as text)
Thu Oct 15 16:18:46 2009 UTC (5 weeks, 5 days ago) by webchick
Branch: MAIN
CVS Tags: DRUPAL-7-0-UNSTABLE-10, HEAD
Changes since 1.60: +2 -2 lines
File MIME type: text/x-php
#216098 by kwinters, jgoldberg, drawk, sun: Make drupal_goto() use the same parameters as url().
1 <?php
2 // $Id: user.pages.inc,v 1.60 2009/10/15 14:07:30 dries Exp $
3
4 /**
5 * @file
6 * User page callback file for the user module.
7 */
8
9 /**
10 * Menu callback; Retrieve a JSON object containing autocomplete suggestions for existing users.
11 */
12 function user_autocomplete($string = '') {
13 $matches = array();
14 if ($string) {
15 $result = db_query_range("SELECT name FROM {users} WHERE LOWER(name) LIKE LOWER(:name)", 0, 10, array(':name' => $string . '%'));
16 foreach ($result as $user) {
17 $matches[$user->name] = check_plain($user->name);
18 }
19 }
20
21 drupal_json_output($matches);
22 }
23
24 /**
25 * Form builder; Request a password reset.
26 *
27 * @ingroup forms
28 * @see user_pass_validate()
29 * @see user_pass_submit()
30 */
31 function user_pass() {
32 $form['name'] = array(
33 '#type' => 'textfield',
34 '#title' => t('Username or e-mail address'),
35 '#size' => 60,
36 '#maxlength' => max(USERNAME_MAX_LENGTH, EMAIL_MAX_LENGTH),
37 '#required' => TRUE,
38 );
39 $form['submit'] = array('#type' => 'submit', '#value' => t('E-mail new password'));
40
41 return $form;
42 }
43
44 function user_pass_validate($form, &$form_state) {
45 $name = trim($form_state['values']['name']);
46 // Try to load by email.
47 $users = user_load_multiple(array(), array('mail' => $name, 'status' => '1'));
48 $account = reset($users);
49 if (!$account) {
50 // No success, try to load by name.
51 $users = user_load_multiple(array(), array('name' => $name, 'status' => '1'));
52 $account = reset($users);
53 }
54 if (isset($account->uid)) {
55 form_set_value(array('#parents' => array('account')), $account, $form_state);
56 }
57 else {
58 form_set_error('name', t('Sorry, %name is not recognized as a user name or an e-mail address.', array('%name' => $name)));
59 }
60 }
61
62 function user_pass_submit($form, &$form_state) {
63 global $language;
64
65 $account = $form_state['values']['account'];
66 // Mail one time login URL and instructions using current language.
67 _user_mail_notify('password_reset', $account, $language);
68 watchdog('user', 'Password reset instructions mailed to %name at %email.', array('%name' => $account->name, '%email' => $account->mail));
69 drupal_set_message(t('Further instructions have been sent to your e-mail address.'));
70
71 $form_state['redirect'] = 'user';
72 return;
73 }
74
75 /**
76 * Menu callback; process one time login link and redirects to the user page on success.
77 */
78 function user_pass_reset($form, &$form_state, $uid, $timestamp, $hashed_pass, $action = NULL) {
79 global $user;
80
81 // When processing the one-time login link, we have to make sure that a user
82 // isn't already logged in.
83 if ($user->uid) {
84 // The existing user is already logged in.
85 if ($user->uid == $uid) {
86 drupal_set_message(t('You are logged in as %user. <a href="!user_edit">Change your password.</a>', array('%user' => $user->name, '!user_edit' => url("user/$user->uid/edit"))));
87 }
88 // A different user is already logged in on the computer.
89 else {
90 $reset_link_account = user_load($uid);
91 if (!empty($reset_link_account)) {
92 drupal_set_message(t('Another user (%other_user) is already logged into the site on this computer, but you tried to use a one-time link for user %resetting_user. Please <a href="!logout">logout</a> and try using the link again.',
93 array('%other_user' => $user->name, '%resetting_user' => $reset_link_account->name, '!logout' => url('user/logout'))));
94 } else {
95 // Invalid one-time link specifies an unknown user.
96 drupal_set_message(t('The one-time login link you clicked is invalid.'));
97 }
98 }
99 drupal_goto();
100 }
101 else {
102 // Time out, in seconds, until login URL expires. 24 hours = 86400 seconds.
103 $timeout = 86400;
104 $current = REQUEST_TIME;
105 // Some redundant checks for extra security ?
106 $users = user_load_multiple(array($uid), array('status' => '1'));
107 if ($timestamp <= $current && $account = reset($users)) {
108 // No time out for first time login.
109 if ($account->login && $current - $timestamp > $timeout) {
110 drupal_set_message(t('You have tried to use a one-time login link that has expired. Please request a new one using the form below.'));
111 drupal_goto('user/password');
112 }
113 elseif ($account->uid && $timestamp >= $account->login && $timestamp <= $current && $hashed_pass == user_pass_rehash($account->pass, $timestamp, $account->login)) {
114 // First stage is a confirmation form, then login
115 if ($action == 'login') {
116 watchdog('user', 'User %name used one-time login link at time %timestamp.', array('%name' => $account->name, '%timestamp' => $timestamp));
117 // Set the new user.
118 $user = $account;
119 // user_login_finalize() also updates the login timestamp of the
120 // user, which invalidates further use of the one-time login link.
121 user_login_finalize();
122 drupal_set_message(t('You have just used your one-time login link. It is no longer necessary to use this link to login. Please change your password.'));
123 drupal_goto('user/' . $user->uid . '/edit');
124 }
125 else {
126 $form['message'] = array('#markup' => t('<p>This is a one-time login for %user_name and will expire on %expiration_date.</p><p>Click on this button to login to the site and change your password.</p>', array('%user_name' => $account->name, '%expiration_date' => format_date($timestamp + $timeout))));
127 $form['help'] = array('#markup' => '<p>' . t('This login can be used only once.') . '</p>');
128 $form['submit'] = array('#type' => 'submit', '#value' => t('Log in'));
129 $form['#action'] = url("user/reset/$uid/$timestamp/$hashed_pass/login");
130 return $form;
131 }
132 }
133 else {
134 drupal_set_message(t('You have tried to use a one-time login link which has either been used or is no longer valid. Please request a new one using the form below.'));
135 drupal_goto('user/password');
136 }
137 }
138 else {
139 // Deny access, no more clues.
140 // Everything will be in the watchdog's URL for the administrator to check.
141 drupal_access_denied();
142 }
143 }
144 }
145
146 /**
147 * Menu callback; logs the current user out, and redirects to the home page.
148 */
149 function user_logout() {
150 global $user;
151
152 watchdog('user', 'Session closed for %name.', array('%name' => $user->name));
153
154 module_invoke_all('user_logout', $user);
155
156 // Destroy the current session, and reset $user to the anonymous user.
157 session_destroy();
158
159 drupal_goto();
160 }
161
162 /**
163 * Process variables for user-profile.tpl.php.
164 *
165 * The $variables array contains the following arguments:
166 * - $account
167 *
168 * @see user-profile.tpl.php
169 */
170 function template_preprocess_user_profile(&$variables) {
171 $account = $variables['elements']['#account'];
172 // Helpful $user_profile variable for templates.
173 foreach (element_children($variables['elements']) as $key) {
174 $variables['user_profile'][$key] = $variables['elements'][$key];
175 }
176 }
177
178 /**
179 * Process variables for user-profile-item.tpl.php.
180 *
181 * The $variables array contains the following arguments:
182 * - $element
183 *
184 * @see user-profile-item.tpl.php
185 */
186 function template_preprocess_user_profile_item(&$variables) {
187 $variables['title'] = $variables['element']['#title'];
188 $variables['value'] = $variables['element']['#markup'];
189 $variables['attributes'] = '';
190 if (isset($variables['element']['#attributes'])) {
191 $variables['attributes'] = drupal_attributes($variables['element']['#attributes']);
192 }
193 }
194
195 /**
196 * Process variables for user-profile-category.tpl.php.
197 *
198 * The $variables array contains the following arguments:
199 * - $element
200 *
201 * @see user-profile-category.tpl.php
202 */
203 function template_preprocess_user_profile_category(&$variables) {
204 $variables['title'] = check_plain($variables['element']['#title']);
205 $variables['profile_items'] = $variables['element']['#children'];
206 $variables['attributes'] = '';
207 if (isset($variables['element']['#attributes'])) {
208 $variables['attributes'] = drupal_attributes($variables['element']['#attributes']);
209 }
210 }
211
212 /**
213 * Form builder; edit a user account or one of their profile categories.
214 *
215 * @ingroup forms
216 * @see user_account_form()
217 * @see user_account_form_validate()
218 * @see user_account_form_submit()
219 * @see user_profile_form_validate()
220 * @see user_profile_form_submit()
221 * @see user_cancel_confirm_form_submit()
222 */
223 function user_profile_form($form, &$form_state, $account, $category = 'account') {
224 global $user;
225
226 $form['#user'] = $account;
227 $form['#user_category'] = $category;
228
229 if ($category == 'account') {
230 user_account_form($form, $form_state);
231 }
232
233 // Attach field widgets.
234 field_attach_form('user', $account, $form, $form_state);
235
236 $form['submit'] = array(
237 '#type' => 'submit',
238 '#value' => t('Save'),
239 '#weight' => 30,
240 );
241 if ($category == 'account') {
242 $form['cancel'] = array(
243 '#type' => 'submit',
244 '#value' => t('Cancel account'),
245 '#weight' => 31,
246 '#submit' => array('user_edit_cancel_submit'),
247 '#access' => ($account->uid == $user->uid && user_access('cancel account')) || user_access('administer users'),
248 );
249 }
250
251 $form['#validate'][] = 'user_profile_form_validate';
252 // Add the final user profile form submit handler.
253 $form['#submit'][] = 'user_profile_form_submit';
254
255 return $form;
256 }
257
258 /**
259 * Validation function for the user account and profile editing form.
260 */
261 function user_profile_form_validate($form, &$form_state) {
262 $edit = (object)$form_state['values'];
263 field_attach_form_validate('user', $edit, $form, $form_state);
264 }
265
266 /**
267 * Submit function for the user account and profile editing form.
268 */
269 function user_profile_form_submit($form, &$form_state) {
270 $account = $form['#user'];
271 $category = $form['#user_category'];
272 // Remove unneeded values.
273 form_state_values_clean($form_state);
274
275 $edit = (object)$form_state['values'];
276 field_attach_submit('user', $edit, $form, $form_state);
277 $edit = (array)$edit;
278
279 user_save($account, $edit, $category);
280
281 // Clear the page cache because pages can contain usernames and/or profile information:
282 cache_clear_all();
283
284 drupal_set_message(t('The changes have been saved.'));
285 }
286
287 /**
288 * Submit function for the 'Cancel account' button on the user edit form.
289 */
290 function user_edit_cancel_submit($form, &$form_state) {
291 $destination = array();
292 if (isset($_GET['destination'])) {
293 $destination = drupal_get_destination();
294 unset($_GET['destination']);
295 }
296 // Note: We redirect from user/uid/edit to user/uid/cancel to make the tabs disappear.
297 $form_state['redirect'] = array("user/" . $form['#user']->uid . "/cancel", array('query' => $destination));
298 }
299
300 /**
301 * Form builder; confirm form for cancelling user account.
302 *
303 * @ingroup forms
304 * @see user_edit_cancel_submit()
305 */
306 function user_cancel_confirm_form($form, &$form_state, $account) {
307 global $user;
308
309 $form['_account'] = array('#type' => 'value', '#value' => $account);
310
311 // Display account cancellation method selection, if allowed.
312 $default_method = variable_get('user_cancel_method', 'user_cancel_block');
313 $admin_access = user_access('administer users');
314 $can_select_method = $admin_access || user_access('select account cancellation method');
315 $form['user_cancel_method'] = array(
316 '#type' => 'item',
317 '#title' => ($account->uid == $user->uid ? t('When cancelling your account') : t('When cancelling the account')),
318 '#access' => $can_select_method,
319 );
320 $form['user_cancel_method'] += user_cancel_methods();
321
322 // Allow user administrators to skip the account cancellation confirmation
323 // mail (by default), as long as they do not attempt to cancel their own
324 // account.
325 $override_access = $admin_access && ($account->uid != $user->uid);
326 $form['user_cancel_confirm'] = array(
327 '#type' => 'checkbox',
328 '#title' => t('Require e-mail confirmation to cancel account.'),
329 '#default_value' => ($override_access ? FALSE : TRUE),
330 '#access' => $override_access,
331 '#description' => t('When enabled, the user must confirm the account cancellation via e-mail.'),
332 );
333 // Also allow to send account canceled notification mail, if enabled.
334 $default_notify = variable_get('user_mail_status_canceled_notify', FALSE);
335 $form['user_cancel_notify'] = array(
336 '#type' => 'checkbox',
337 '#title' => t('Notify user when account is canceled.'),
338 '#default_value' => ($override_access ? FALSE : $default_notify),
339 '#access' => $override_access && $default_notify,
340 '#description' => t('When enabled, the user will receive an e-mail notification after the account has been cancelled.'),
341 );
342
343 // Prepare confirmation form page title and description.
344 if ($account->uid == $user->uid) {
345 $question = t('Are you sure you want to cancel your account?');
346 }
347 else {
348 $question = t('Are you sure you want to cancel the account %name?', array('%name' => $account->name));
349 }
350 $description = '';
351 if ($can_select_method) {
352 $description = t('Select the method to cancel the account above.');
353 foreach (element_children($form['user_cancel_method']) as $element) {
354 unset($form['user_cancel_method'][$element]['#description']);
355 }
356 }
357 else {
358 // The radio button #description is used as description for the confirmation
359 // form.
360 foreach (element_children($form['user_cancel_method']) as $element) {
361 if ($form['user_cancel_method'][$element]['#default_value'] == $form['user_cancel_method'][$element]['#return_value']) {
362 $description = $form['user_cancel_method'][$element]['#description'];
363 }
364 unset($form['user_cancel_method'][$element]['#description']);
365 }
366 }
367
368 return confirm_form($form,
369 $question,
370 'user/' . $account->uid,
371 $description . ' ' . t('This action cannot be undone.'),
372 t('Cancel account'), t('Cancel'));
373 }
374
375 /**
376 * Submit handler for the account cancellation confirm form.
377 *
378 * @see user_cancel_confirm_form()
379 * @see user_multiple_cancel_confirm_submit()
380 */
381 function user_cancel_confirm_form_submit($form, &$form_state) {
382 global $user;
383 $account = $form_state['values']['_account'];
384
385 // Cancel account immediately, if the current user has administrative
386 // privileges, no confirmation mail shall be sent, and the user does not
387 // attempt to cancel the own account.
388 if (user_access('administer users') && empty($form_state['values']['user_cancel_confirm']) && $account->uid != $user->uid) {
389 user_cancel($form_state['values'], $account->uid, $form_state['values']['user_cancel_method']);
390
391 $form_state['redirect'] = 'admin/people';
392 }
393 else {
394 // Store cancelling method and whether to notify the user in $account for
395 // user_cancel_confirm().
396 $edit = array(
397 'user_cancel_method' => $form_state['values']['user_cancel_method'],
398 'user_cancel_notify' => $form_state['values']['user_cancel_notify'],
399 );
400 $account = user_save($account, $edit);
401 _user_mail_notify('cancel_confirm', $account);
402 drupal_set_message(t('A confirmation request to cancel your account has been sent to your e-mail address.'));
403 watchdog('user', 'Sent account cancellation request to %name %email.', array('%name' => $account->name, '%email' => '<' . $account->mail . '>'), WATCHDOG_NOTICE);
404
405 $form_state['redirect'] = "user/$account->uid";
406 }
407 }
408
409 /**
410 * Helper function to return available account cancellation methods.
411 *
412 * Please refer to the documentation of hook_user_cancel_methods_alter().
413 *
414 * @return
415 * An array containing all account cancellation methods as form elements.
416 *
417 * @see hook_user_cancel_methods_alter()
418 * @see user_admin_settings()
419 * @see user_cancel_confirm_form()
420 * @see user_multiple_cancel_confirm()
421 */
422 function user_cancel_methods() {
423 $methods = array(
424 'user_cancel_block' => array(
425 'title' => t('Disable the account and keep all content.'),
426 'description' => t('Your account will be blocked and you will no longer be able to log in. All of your content will remain attributed to your user name.'),
427 ),
428 'user_cancel_block_unpublish' => array(
429 'title' => t('Disable the account and unpublish all content.'),
430 'description' => t('Your account will be blocked and you will no longer be able to log in. All of your content will be hidden from everyone but administrators.'),
431 ),
432 'user_cancel_reassign' => array(
433 'title' => t('Delete the account and make all content belong to the %anonymous-name user.', array('%anonymous-name' => variable_get('anonymous', t('Anonymous')))),
434 'description' => t('Your account will be removed and all account information deleted. All of your content will be assigned to the %anonymous-name user.', array('%anonymous-name' => variable_get('anonymous', t('Anonymous')))),
435 ),
436 'user_cancel_delete' => array(
437 'title' => t('Delete the account and all content.'),
438 'description' => t('Your account will be removed and all account information deleted. All of your content will also be deleted.'),
439 'access' => user_access('administer users'),
440 ),
441 );
442 // Allow modules to customize account cancellation methods.
443 drupal_alter('user_cancel_methods', $methods);
444
445 // Turn all methods into real form elements.
446 $default_method = variable_get('user_cancel_method', 'user_cancel_block');
447 foreach ($methods as $name => $method) {
448 $form[$name] = array(
449 '#type' => 'radio',
450 '#title' => $method['title'],
451 '#description' => (isset($method['description']) ? $method['description'] : NULL),
452 '#return_value' => $name,
453 '#default_value' => $default_method,
454 '#parents' => array('user_cancel_method'),
455 '#required' => TRUE,
456 );
457 }
458 return $form;
459 }
460
461 /**
462 * Menu callback; Cancel a user account via e-mail confirmation link.
463 *
464 * @see user_cancel_confirm_form()
465 * @see user_cancel_url()
466 */
467 function user_cancel_confirm($account, $timestamp = 0, $hashed_pass = '') {
468 // Time out in seconds until cancel URL expires; 24 hours = 86400 seconds.
469 $timeout = 86400;
470 $current = REQUEST_TIME;
471
472 // Basic validation of arguments.
473 if (isset($account->user_cancel_method) && !empty($timestamp) && !empty($hashed_pass)) {
474 // Validate expiration and hashed password/login.
475 if ($timestamp <= $current && $current - $timestamp < $timeout && $account->uid && $timestamp >= $account->login && $hashed_pass == user_pass_rehash($account->pass, $timestamp, $account->login)) {
476 $edit = array(
477 'user_cancel_notify' => isset($account->user_cancel_notify) ? $account->user_cancel_notify : variable_get('user_mail_status_canceled_notify', FALSE),
478 );
479 user_cancel($edit, $account->uid, $account->user_cancel_method);
480 // Since user_cancel() is not invoked via Form API, batch processing needs
481 // to be invoked manually and should redirect to the front page after
482 // completion.
483 batch_process('');
484 }
485 else {
486 drupal_set_message(t('You have tried to use an account cancellation link that has expired. Please request a new one using the form below.'));
487 drupal_goto("user/$account->uid/cancel");
488 }
489 }
490 drupal_access_denied();
491 }
492
493 /**
494 * Access callback for path /user.
495 *
496 * Displays user profile if user is logged in, or login form for anonymous
497 * users.
498 */
499 function user_page() {
500 global $user;
501 if ($user->uid) {
502 menu_set_active_item('user/' . $user->uid);
503 return menu_execute_active_handler(NULL, FALSE);
504 }
505 else {
506 return drupal_get_form('user_login');
507 }
508 }

  ViewVC Help
Powered by ViewVC 1.1.2