comparison modules/user/user.pages.inc @ 1:c1f4ac30525a 6.0

Drupal 6.0
author Franck Deroche <webmaster@defr.org>
date Tue, 23 Dec 2008 14:28:28 +0100
parents
children 589fb7c02327
comparison
equal deleted inserted replaced
0:5a113a1c4740 1:c1f4ac30525a
1 <?php
2 // $Id: user.pages.inc,v 1.11 2008/01/08 10:35:43 goba 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('%s%%')", $string, 0, 10);
16 while ($user = db_fetch_object($result)) {
17 $matches[$user->name] = check_plain($user->name);
18 }
19 }
20
21 drupal_json($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 $account = user_load(array('mail' => $name, 'status' => 1));
48 if (!$account) {
49 // No success, try to load by name.
50 $account = user_load(array('name' => $name, 'status' => 1));
51 }
52 if (isset($account->uid)) {
53 form_set_value(array('#parents' => array('account')), $account, $form_state);
54 }
55 else {
56 form_set_error('name', t('Sorry, %name is not recognized as a user name or an e-mail address.', array('%name' => $name)));
57 }
58 }
59
60 function user_pass_submit($form, &$form_state) {
61 global $language;
62
63 $account = $form_state['values']['account'];
64 // Mail one time login URL and instructions using current language.
65 _user_mail_notify('password_reset', $account, $language);
66 watchdog('user', 'Password reset instructions mailed to %name at %email.', array('%name' => $account->name, '%email' => $account->mail));
67 drupal_set_message(t('Further instructions have been sent to your e-mail address.'));
68
69 $form_state['redirect'] = 'user';
70 return;
71 }
72
73 /**
74 * Menu callback; process one time login link and redirects to the user page on success.
75 */
76 function user_pass_reset(&$form_state, $uid, $timestamp, $hashed_pass, $action = NULL) {
77 global $user;
78
79 // Check if the user is already logged in. The back button is often the culprit here.
80 if ($user->uid) {
81 drupal_set_message(t('You have already used this one-time login link. It is not necessary to use this link to login anymore. You are already logged in.'));
82 drupal_goto();
83 }
84 else {
85 // Time out, in seconds, until login URL expires. 24 hours = 86400 seconds.
86 $timeout = 86400;
87 $current = time();
88 // Some redundant checks for extra security ?
89 if ($timestamp < $current && $account = user_load(array('uid' => $uid, 'status' => 1)) ) {
90 // No time out for first time login.
91 if ($account->login && $current - $timestamp > $timeout) {
92 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.'));
93 drupal_goto('user/password');
94 }
95 else if ($account->uid && $timestamp > $account->login && $timestamp < $current && $hashed_pass == user_pass_rehash($account->pass, $timestamp, $account->login)) {
96 // First stage is a confirmation form, then login
97 if ($action == 'login') {
98 watchdog('user', 'User %name used one-time login link at time %timestamp.', array('%name' => $account->name, '%timestamp' => $timestamp));
99 // Set the new user.
100 $user = $account;
101 // user_authenticate_finalize() also updates the login timestamp of the
102 // user, which invalidates further use of the one-time login link.
103 user_authenticate_finalize($form_state['values']);
104 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.'));
105 drupal_goto('user/'. $user->uid .'/edit');
106 }
107 else {
108 $form['message'] = array('#value' => 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))));
109 $form['help'] = array('#value' => '<p>'. t('This login can be used only once.') .'</p>');
110 $form['submit'] = array('#type' => 'submit', '#value' => t('Log in'));
111 $form['#action'] = url("user/reset/$uid/$timestamp/$hashed_pass/login");
112 return $form;
113 }
114 }
115 else {
116 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.'));
117 drupal_goto('user/password');
118 }
119 }
120 else {
121 // Deny access, no more clues.
122 // Everything will be in the watchdog's URL for the administrator to check.
123 drupal_access_denied();
124 }
125 }
126 }
127
128 /**
129 * Menu callback; logs the current user out, and redirects to the home page.
130 */
131 function user_logout() {
132 global $user;
133
134 watchdog('user', 'Session closed for %name.', array('%name' => $user->name));
135
136 // Destroy the current session:
137 session_destroy();
138 module_invoke_all('user', 'logout', NULL, $user);
139
140 // Load the anonymous user
141 $user = drupal_anonymous_user();
142
143 drupal_goto();
144 }
145
146 /**
147 * Menu callback; Displays a user or user profile page.
148 */
149 function user_view($account) {
150 drupal_set_title(check_plain($account->name));
151 // Retrieve all profile fields and attach to $account->content.
152 user_build_content($account);
153
154 // To theme user profiles, copy modules/user/user_profile.tpl.php
155 // to your theme directory, and edit it as instructed in that file's comments.
156 return theme('user_profile', $account);
157 }
158
159 /**
160 * Process variables for user-profile.tpl.php.
161 *
162 * The $variables array contains the following arguments:
163 * - $account
164 *
165 * @see user-picture.tpl.php
166 */
167 function template_preprocess_user_profile(&$variables) {
168 $variables['profile'] = array();
169 // Sort sections by weight
170 uasort($variables['account']->content, 'element_sort');
171 // Provide keyed variables so themers can print each section independantly.
172 foreach (element_children($variables['account']->content) as $key) {
173 $variables['profile'][$key] = drupal_render($variables['account']->content[$key]);
174 }
175 // Collect all profiles to make it easier to print all items at once.
176 $variables['user_profile'] = implode($variables['profile']);
177 }
178
179 /**
180 * Process variables for user-profile-item.tpl.php.
181 *
182 * The $variables array contains the following arguments:
183 * - $element
184 *
185 * @see user-profile-item.tpl.php
186 */
187 function template_preprocess_user_profile_item(&$variables) {
188 $variables['title'] = $variables['element']['#title'];
189 $variables['value'] = $variables['element']['#value'];
190 $variables['attributes'] = '';
191 if (isset($variables['element']['#attributes'])) {
192 $variables['attributes'] = drupal_attributes($variables['element']['#attributes']);
193 }
194 }
195
196 /**
197 * Process variables for user-profile-category.tpl.php.
198 *
199 * The $variables array contains the following arguments:
200 * - $element
201 *
202 * @see user-profile-category.tpl.php
203 */
204 function template_preprocess_user_profile_category(&$variables) {
205 $variables['title'] = check_plain($variables['element']['#title']);
206 $variables['profile_items'] = $variables['element']['#children'];
207 $variables['attributes'] = '';
208 if (isset($variables['element']['#attributes'])) {
209 $variables['attributes'] = drupal_attributes($variables['element']['#attributes']);
210 }
211 }
212
213 /**
214 * Form builder; Present the form to edit a given user or profile category.
215 *
216 * @ingroup forms
217 * @see user_edit_validate()
218 * @see user_edit_submit()
219 */
220 function user_edit($account, $category = 'account') {
221 drupal_set_title(check_plain($account->name));
222 return drupal_get_form('user_profile_form', $account, $category);
223 }
224
225 /**
226 * Form builder; edit a user account or one of their profile categories.
227 *
228 * @ingroup forms
229 * @see user_profile_form_validate()
230 * @see user_profile_form_submit()
231 * @see user_edit_delete_submit()
232 */
233 function user_profile_form($form_state, $account, $category = 'account') {
234
235 $edit = (empty($form_state['values'])) ? (array)$account : $form_state['values'];
236
237 $form = _user_forms($edit, $account, $category);
238 $form['_category'] = array('#type' => 'value', '#value' => $category);
239 $form['_account'] = array('#type' => 'value', '#value' => $account);
240 $form['submit'] = array('#type' => 'submit', '#value' => t('Save'), '#weight' => 30);
241 if (user_access('administer users')) {
242 $form['delete'] = array(
243 '#type' => 'submit',
244 '#value' => t('Delete'),
245 '#weight' => 31,
246 '#submit' => array('user_edit_delete_submit'),
247 );
248 }
249 $form['#attributes']['enctype'] = 'multipart/form-data';
250
251 return $form;
252 }
253
254 /**
255 * Validation function for the user account and profile editing form.
256 */
257 function user_profile_form_validate($form, &$form_state) {
258 user_module_invoke('validate', $form_state['values'], $form_state['values']['_account'], $form_state['values']['_category']);
259 // Validate input to ensure that non-privileged users can't alter protected data.
260 if ((!user_access('administer users') && array_intersect(array_keys($form_state['values']), array('uid', 'init', 'session'))) || (!user_access('administer permissions') && isset($form_state['values']['roles']))) {
261 watchdog('security', 'Detected malicious attempt to alter protected user fields.', array(), WATCHDOG_WARNING);
262 // set this to a value type field
263 form_set_error('category', t('Detected malicious attempt to alter protected user fields.'));
264 }
265 }
266
267 /**
268 * Submit function for the user account and profile editing form.
269 */
270 function user_profile_form_submit($form, &$form_state) {
271 $account = $form_state['values']['_account'];
272 $category = $form_state['values']['_category'];
273 unset($form_state['values']['_account'], $form_state['values']['op'], $form_state['values']['submit'], $form_state['values']['delete'], $form_state['values']['form_token'], $form_state['values']['form_id'], $form_state['values']['_category']);
274 user_module_invoke('submit', $form_state['values'], $account, $category);
275 user_save($account, $form_state['values'], $category);
276
277 // Clear the page cache because pages can contain usernames and/or profile information:
278 cache_clear_all();
279
280 drupal_set_message(t('The changes have been saved.'));
281 return;
282 }
283
284 /**
285 * Submit function for the 'Delete' button on the user edit form.
286 */
287 function user_edit_delete_submit($form, &$form_state) {
288 $destination = '';
289 if (isset($_REQUEST['destination'])) {
290 $destination = drupal_get_destination();
291 unset($_REQUEST['destination']);
292 }
293 // Note: We redirect from user/uid/edit to user/uid/delete to make the tabs disappear.
294 $form_state['redirect'] = array("user/". $form_state['values']['_account']->uid ."/delete", $destination);
295 }
296
297 /**
298 * Form builder; confirm form for user deletion.
299 *
300 * @ingroup forms
301 * @see user_confirm_delete_submit()
302 */
303 function user_confirm_delete(&$form_state, $account) {
304
305 $form['_account'] = array('#type' => 'value', '#value' => $account);
306
307 return confirm_form($form,
308 t('Are you sure you want to delete the account %name?', array('%name' => $account->name)),
309 'user/'. $account->uid,
310 t('All submissions made by this user will be attributed to the anonymous account. This action cannot be undone.'),
311 t('Delete'), t('Cancel'));
312 }
313
314 /**
315 * Submit function for the confirm form for user deletion.
316 */
317 function user_confirm_delete_submit($form, &$form_state) {
318 user_delete($form_state['values'], $form_state['values']['_account']->uid);
319 drupal_set_message(t('%name has been deleted.', array('%name' => $form_state['values']['_account']->name)));
320
321 if (!isset($_REQUEST['destination'])) {
322 $form_state['redirect'] = 'admin/user/user';
323 }
324 }
325
326 function user_edit_validate($form, &$form_state) {
327 user_module_invoke('validate', $form_state['values'], $form_state['values']['_account'], $form_state['values']['_category']);
328 // Validate input to ensure that non-privileged users can't alter protected data.
329 if ((!user_access('administer users') && array_intersect(array_keys($form_state['values']), array('uid', 'init', 'session'))) || (!user_access('administer permissions') && isset($form_state['values']['roles']))) {
330 watchdog('security', 'Detected malicious attempt to alter protected user fields.', array(), WATCHDOG_WARNING);
331 // set this to a value type field
332 form_set_error('category', t('Detected malicious attempt to alter protected user fields.'));
333 }
334 }
335
336 function user_edit_submit($form, &$form_state) {
337 $account = $form_state['values']['_account'];
338 $category = $form_state['values']['_category'];
339 unset($form_state['values']['_account'], $form_state['values']['op'], $form_state['values']['submit'], $form_state['values']['delete'], $form_state['values']['form_token'], $form_state['values']['form_id'], $form_state['values']['_category']);
340 user_module_invoke('submit', $form_state['values'], $account, $category);
341 user_save($account, $form_state['values'], $category);
342
343 // Clear the page cache because pages can contain usernames and/or profile information:
344 cache_clear_all();
345
346 drupal_set_message(t('The changes have been saved.'));
347 return;
348 }
349
350 /**
351 * Access callback for path /user.
352 *
353 * Displays user profile if user is logged in, or login form for anonymous
354 * users.
355 */
356 function user_page() {
357 global $user;
358 if ($user->uid) {
359 menu_set_active_item('user/'. $user->uid);
360 return menu_execute_active_handler();
361 }
362 else {
363 return drupal_get_form('user_login');
364 }
365 }