logintoboggan.module 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. <?php
  2. /**
  3. * @file
  4. * LoginToboggan module
  5. *
  6. * This module enhances the configuration abilities of Drupal's default login system.
  7. */
  8. /**
  9. * @todo
  10. *
  11. */
  12. /**
  13. * @wishlist
  14. *
  15. */
  16. /**
  17. * @defgroup logintoboggan_core Core drupal hooks
  18. */
  19. /**
  20. * Implement hook_cron().
  21. */
  22. function logintoboggan_cron() {
  23. // If set password is enabled, and a purge interval is set, check for
  24. // unvalidated users to purge.
  25. if (($purge_interval = variable_get('logintoboggan_purge_unvalidated_user_interval', 0)) && !variable_get('user_email_verification', TRUE)) {
  26. $validating_id = logintoboggan_validating_id();
  27. // As a safety check, make sure that we have a non-core role as the
  28. // pre-auth role -- otherwise skip.
  29. if (!in_array($validating_id, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
  30. $purge_time = REQUEST_TIME - $purge_interval;
  31. $accounts = db_query("SELECT u.uid, u.name FROM {users} u INNER JOIN {users_roles} ur ON u.uid = ur.uid WHERE ur.rid = :rid AND u.created < :created", array(
  32. ':rid' => $validating_id,
  33. ':created' => $purge_time,
  34. ));
  35. $purged_users = array();
  36. $uids = array();
  37. foreach ($accounts as $account) {
  38. $uids[] = $account->uid;
  39. $purged_users[] = check_plain($account->name);
  40. }
  41. // Delete the users from the system.
  42. user_delete_multiple($uids);
  43. // Log the purged users.
  44. if (!empty($purged_users)) {
  45. watchdog('logintoboggan', 'Purged the following unvalidated users: !purged_users', array('!purged_users' => theme('item_list', array('items' => $purged_users))));
  46. }
  47. }
  48. }
  49. }
  50. /**
  51. * Implement hook_help().
  52. */
  53. function logintoboggan_help($path, $arg) {
  54. switch ($path) {
  55. case 'admin/help#logintoboggan':
  56. $output = t("<p>The Login Toboggan module improves the Drupal login system by offering the following features:
  57. <ol>
  58. <li>Allow users to login using either their username OR their e-mail address.</li>
  59. <li>Allow users to login immediately.</li>
  60. <li>Provide a login form on Access Denied pages for non-logged-in (anonymous) users.</li>
  61. <li>The module provides two login block options: One uses JavaScript to display the form within the block immediately upon clicking 'log in'. The other brings the user to a separate page, but returns the user to their original page upon login.</li>
  62. <li>Customize the registration form with two e-mail fields to ensure accuracy.</li>
  63. <li>Optionally redirect the user to a specific page when using the 'Immediate login' feature.</li>
  64. <li>Optionally redirect the user to a specific page upon validation of their e-mail address.</li>
  65. <li>Optionally display a user message indicating a successful login.</li>
  66. <li>Optionally combine both the login and registration form on one page.</li>
  67. <li>Optionally display an 'Request new password' link on the user login form.</li>
  68. <li>Optionally have unvalidated users purged from the system at a pre-defined interval (please read the CAVEATS section of INSTALL.txt for important information on configuring this feature!).</li>
  69. </ol>
  70. These features may be turned on or off in the Login Toboggan <a href=\"!url\">settings</a>.</p>
  71. <p>Because this module completely reorients the Drupal login process you will probably want to edit the welcome e-mail on the <a href=\"!user_settings\">user settings</a> page. Note when the 'Set password' option is enabled, the [user:validate-url] token becomes a verification url that the user MUST visit in order to enable authenticated status). The following is an example welcome e-mail:</p>
  72. ", array('!url' => url('admin/config/system/logintoboggan'), '!user_settings' => url('admin/config/people/accounts')));
  73. $example_help_form = drupal_get_form('logintoboggan_example_help');
  74. $output .= drupal_render($example_help_form);
  75. $output .= t("<p>Note that if you have set the 'Visitors can create accounts but administrator approval is required' option for account approval, and are also using the 'Set password' feature of LoginToboggan, the user will immediately receive the permissions of the pre-authorized user role. LoginToboggan prevents the pre-authorized role from automatically inheriting the authorized role permissions. You may wish to create a pre-authorized role with the exact same permissions as the anonymous user if you wish the newly created user to only have anonymous permissions.</p><p>In order for a site administrator to unblock a user who is awaiting administrator approval, they must either click on the validation link they receive in their notification e-mail, or manually remove the user from the site's pre-authorized role -- afterwards the user will then receive 'authenticated user' permissions. In either case, the user will receive an account activated e-mail if it's enabled on the user settings page -- it's recommended that you edit the default text of the activation e-mail to match LoginToboggan's workflow as described. </p><p>If you are using the 'Visitors can create accounts and no administrator approval is required' option, removal of the pre-authorized role will happen automatically when the user validates their account via e-mail.</p><p>Also be aware that LoginToboggan only affects registrations initiated by users--any user account created by an administrator will not use any LoginToboggan functionality.");
  76. return $output;
  77. break;
  78. case 'admin/config/system/logintoboggan':
  79. if (module_exists('help')) {
  80. $help_text = t("More help can be found at <a href=\"!help\">LoginToboggan help</a>.", array('!help' => url('admin/help/logintoboggan')));
  81. }
  82. else {
  83. $help_text = '';
  84. }
  85. $output = "<p>" . t("Customize your login and registration system.") . " $help_text</p>";
  86. return $output;
  87. }
  88. }
  89. /**
  90. * Helper function for example user e-mail textfield.
  91. */
  92. function logintoboggan_example_help() {
  93. $example = t('
  94. [user:name],
  95. Thank you for registering.
  96. IMPORTANT:
  97. For full site access, you will need to click on this link or copy and paste it in your browser:
  98. [user:validate-url]
  99. This will verify your account and log you into the site. In the future you will be able to log in to [site:login-url] using the username and password that you created during registration.
  100. ');
  101. $form['foo'] = array(
  102. '#type' => 'textarea',
  103. '#default_value' => $example,
  104. '#rows' => 15,
  105. );
  106. return $form;
  107. }
  108. /**
  109. * Implement hook_form_block_admin_configure_alter().
  110. *
  111. * @ingroup logintoboggan_core
  112. */
  113. function logintoboggan_form_block_admin_configure_alter(&$form, &$form_state) {
  114. if (($form['module']['#value'] == 'user') && ($form['delta']['#value'] == 'login')) {
  115. $form['#submit'][] = 'logintoboggan_user_block_admin_configure_submit';
  116. $form['settings']['title']['#description'] .= '<div id="logintoboggan-block-title-description">'. t('<strong>Note:</strong> Logintoboggan module is installed. If you are using one of the custom login block types below, it is recommended that you set this to <em>&lt;none&gt;</em>.') .'</div>';
  117. $form['settings']['logintoboggan_login_block_type'] = array(
  118. '#type' => 'radios',
  119. '#title' => t('Block type'),
  120. '#default_value' => variable_get('logintoboggan_login_block_type', 0),
  121. '#options' => array(t('Standard'), t('Link'), t('Collapsible form')),
  122. '#description' => t("'Standard' is a standard login block, 'Link' is a login link that returns the user to the original page after logging in, 'Collapsible form' is a javascript collaspible login form."),
  123. );
  124. $form['settings']['logintoboggan_login_block_message'] = array(
  125. '#type' => 'textarea',
  126. '#title' => t('Set a custom message to appear at the top of the login block'),
  127. '#default_value' => variable_get('logintoboggan_login_block_message', ''),
  128. );
  129. }
  130. }
  131. /**
  132. * Implement hook_form_user_profile_form_alter().
  133. *
  134. * @ingroup logintoboggan_core
  135. */
  136. function logintoboggan_form_user_profile_form_alter(&$form, &$form_state) {
  137. if ($form['#user_category'] == 'account') {
  138. $account = $form['#user'];
  139. $form['#validate'][] = 'logintoboggan_user_edit_validate';
  140. $id = logintoboggan_validating_id();
  141. // User is editing their own account settings, or user admin
  142. // is editing their account.
  143. if ($GLOBALS['user']->uid == $account->uid || user_access('administer users')) {
  144. // Display link to re-send validation e-mail.
  145. // Re-validate link appears if:
  146. // 1. Users can create their own password.
  147. // 2. User is still in the validating role.
  148. // 3. Users can create accounts without admin approval.
  149. // 4. The validating role is not the authorized user role.
  150. if (!variable_get('user_email_verification', TRUE) && array_key_exists($id, $account->roles) && (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS) && ($id > DRUPAL_AUTHENTICATED_RID)) {
  151. $form['revalidate'] = array(
  152. '#type' => 'fieldset',
  153. '#title' => t('Account validation'),
  154. '#weight' => -10,
  155. );
  156. $form['revalidate']['revalidate_link'] = array(
  157. '#markup' => l(t('re-send validation e-mail'), 'toboggan/revalidate/'. $account->uid),
  158. );
  159. }
  160. }
  161. $pre_auth = !variable_get('user_email_verification', TRUE) && $id != DRUPAL_AUTHENTICATED_RID;
  162. $in_pre_auth_role = in_array($id, array_keys($account->roles));
  163. // Messages are only necessary for user admins, and aren't necessary if
  164. // there's no valid pre-auth role.
  165. if (user_access('administer users') && isset($form['account']['roles']) && $pre_auth) {
  166. // User is still in the pre-auth role, so let the admin know.
  167. if ($in_pre_auth_role) {
  168. // To reduce UI confusion, remove the disabled checkbox for the
  169. // authenticated user role.
  170. unset($form['account']['roles'][DRUPAL_AUTHENTICATED_RID]);
  171. // This form element is necessary as a placeholder for the user's
  172. // pre-auth setting on form load. It's used to compare against the
  173. // submitted form values to see if the pre-auth role has been unchecked.
  174. $form['logintoboggan_pre_auth_check'] = array(
  175. '#type' => 'hidden',
  176. '#value' => '1',
  177. );
  178. if (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) {
  179. $form['account']['status']['#description'] = t('If this user was created using the "Immediate Login" feature of LoginToboggan, and they are also awaiting adminstrator approval on their account, you must remove them from the site\'s pre-authorized role in the "Roles" section below, or they will not receive authenticated user permissions!');
  180. }
  181. $form['account']['roles']['#description'] = t("The user is assigned LoginToboggan's pre-authorized role, and is not currently receiving authenticated user permissions.");
  182. }
  183. // User is no longer in the pre-auth role, so remove the option to add
  184. // them back.
  185. else {
  186. unset($form['account']['roles']['#options'][$id]);
  187. }
  188. }
  189. }
  190. }
  191. /**
  192. * Implement hook_form_user_register_form_alter().
  193. *
  194. * @ingroup logintoboggan_core
  195. */
  196. function logintoboggan_form_user_register_form_alter(&$form, &$form_state) {
  197. // Admin created accounts are only validated by the module.
  198. if (user_access('administer users')) {
  199. $form['#validate'][] = 'logintoboggan_user_register_validate';
  200. return;
  201. }
  202. $mail = variable_get('logintoboggan_confirm_email_at_registration', 0);
  203. $pass = !variable_get('user_email_verification', TRUE);
  204. // Ensure a valid submit array.
  205. $form['#submit'] = is_array($form['#submit']) ? $form['#submit'] : array();
  206. // Replace core's registration function with LT's registration function.
  207. // Put the LT submit handler first, so other submit handlers have a valid
  208. // user to work with upon registration.
  209. $key = array_search('user_register_submit', $form['#submit']);
  210. if ($key !== FALSE) {
  211. unset($form['#submit'][$key]);
  212. }
  213. array_unshift($form['#submit'],'logintoboggan_user_register_submit');
  214. if ($mail || $pass) {
  215. $form['#validate'][] = 'logintoboggan_user_register_validate';
  216. //Display a confirm e-mail address box if option is enabled.
  217. if ($mail) {
  218. $form['account']['conf_mail'] = array(
  219. '#type' => 'textfield',
  220. '#title' => t('Confirm e-mail address'),
  221. '#weight' => -28,
  222. '#maxlength' => 64,
  223. '#description' => t('Please re-type your e-mail address to confirm it is accurate.'),
  224. '#required' => TRUE,
  225. );
  226. // Weight things properly so that the order is name, mail, conf_mail.
  227. $form['account']['name']['#weight'] = -30;
  228. $form['account']['mail']['#weight'] = -29;
  229. }
  230. $min_pass = variable_get('logintoboggan_minimum_password_length', 0);
  231. if ($pass && $min_pass > 0) {
  232. $form['account']['pass']['#description'] = isset($form['account']['pass']['#description']) ? $form['account']['pass']['#description'] . " " : "";
  233. $form['account']['pass']['#description'] .= t('Password must be at least %length characters.', array('%length' => $min_pass));
  234. }
  235. }
  236. }
  237. /**
  238. * Implement hook_form_user_admin_account_alter().
  239. *
  240. * @ingroup logintoboggan_core
  241. */
  242. function logintoboggan_form_user_admin_account_alter(&$form, &$form_state) {
  243. // Unset the ability to add the pre-auth role in the user admin interface.
  244. $id = logintoboggan_validating_id();
  245. // Test here for a valid pre-auth -- we only remove this role if one exists.
  246. $pre_auth = !variable_get('user_email_verification', TRUE) && $id != DRUPAL_AUTHENTICATED_RID;
  247. $add = t('Add a role to the selected users');
  248. if ($pre_auth && isset($form['options']['operation']['#options'][$add]["add_role-$id"])) {
  249. unset($form['options']['operation']['#options'][$add]["add_role-$id"]);
  250. }
  251. }
  252. /**
  253. * Implement hook_form_user_pass_reset_alter().
  254. *
  255. * @ingroup logintoboggan_core
  256. */
  257. function logintoboggan_form_user_pass_reset_alter(&$form, &$form_state) {
  258. // Password resets count as validating an email address, so remove the user
  259. // from the pre-auth role if they are still in it. We only want to run this
  260. // code when the user first hits the reset login form.
  261. if (arg(5) != 'login' && ($uid = (int) arg(2))) {
  262. if ($account = user_load($uid)) {
  263. $id = logintoboggan_validating_id();
  264. $in_pre_auth_role = in_array($id, array_keys($account->roles));
  265. if ($in_pre_auth_role) {
  266. _logintoboggan_process_validation($account);
  267. drupal_set_message(t('You have successfully validated your e-mail address.'));
  268. }
  269. }
  270. }
  271. }
  272. /**
  273. * Implement hook_form_alter().
  274. *
  275. * @ingroup logintoboggan_core
  276. */
  277. function logintoboggan_form_alter(&$form, &$form_state, $form_id) {
  278. switch ($form_id) {
  279. case 'user_login':
  280. case 'user_login_block':
  281. // Grab the message from settings for display at the top of the login block.
  282. if ($login_msg = variable_get('logintoboggan_login_block_message', '')) {
  283. $form['message'] = array(
  284. '#markup' => filter_xss_admin($login_msg),
  285. '#weight' => -50,
  286. );
  287. }
  288. if (variable_get('logintoboggan_login_with_email', 0)) {
  289. // Ensure a valid validate array.
  290. $form['#validate'] = is_array($form['#validate']) ? $form['#validate'] : array();
  291. // LT's validation function must run first.
  292. array_unshift($form['#validate'],'logintoboggan_user_login_validate');
  293. // Use theme functions to print the username field's textual labels.
  294. $form['name']['#title'] = theme('lt_username_title', array('form_id' => $form_id));
  295. $form['name']['#description'] = theme('lt_username_description', array('form_id' => $form_id));
  296. // Use theme functions to print the password field's textual labels.
  297. $form['pass']['#title'] = theme('lt_password_title', array('form_id' => $form_id));
  298. $form['pass']['#description'] = theme('lt_password_description', array('form_id' => $form_id));
  299. }
  300. if (($form_id == 'user_login_block')) {
  301. $block_type = variable_get('logintoboggan_login_block_type', 0);
  302. if ($block_type == 1) {
  303. // What would really be nice here is to start with a clean form, but
  304. // we can't really do that, because drupal_prepare_form() has already
  305. // been run, and we can't run it again in the _alter() hook, or we'll
  306. // get into and endless loop. Since we don't know exactly what's in
  307. // the form, strip out all regular form elements and the handlers.
  308. foreach (element_children($form) as $element) {
  309. unset($form[$element]);
  310. // OpenID expects this key, so provide it to prevent notices.
  311. if (module_exists("openid")) {
  312. $form['name']['#size'] = 0;
  313. }
  314. }
  315. unset($form['#validate'], $form['#submit']);
  316. $form['logintoboggan_login_link'] = array(
  317. '#markup' => l(theme('lt_login_link'), 'user/login', array('query' => drupal_get_destination())),
  318. );
  319. }
  320. elseif ($block_type == 2) {
  321. $form = _logintoboggan_toggleboggan($form);
  322. }
  323. }
  324. else {
  325. if (variable_get('logintoboggan_unified_login', 0)) {
  326. $form['lost_password'] = array(
  327. '#markup' => '<div class="login-forgot">' . l(t('Request new password'), 'user/password') . '</div>',
  328. );
  329. }
  330. }
  331. break;
  332. case 'user_admin_settings':
  333. // Disable the checkbox at the Account settings page which controls
  334. // whether e-mail verification is required upon registration or not.
  335. // The LoginToboggan module implements e-mail verification functionality
  336. // differently than core, and will control wether e-mail verification is
  337. // required or not.
  338. $form['registration_cancellation']['user_email_verification']['#disabled'] = true;
  339. $form['registration_cancellation']['user_email_verification']['#description'] = t('This setting has been locked by the LoginToboggan module. You can change this setting by modifying the <strong>Set password</strong> checkbox at <a href="!link">LoginToboggan settings page</a>.', array('!link' => url('admin/config/system/logintoboggan')));
  340. break;
  341. }
  342. }
  343. /**
  344. * Implement hook_js_alter().
  345. */
  346. function logintoboggan_js_alter(&$javascript) {
  347. // Look for the user permissions js.
  348. if (isset($javascript['modules/user/user.permissions.js'])) {
  349. $id = logintoboggan_validating_id();
  350. // If the pre-auth user isn't the auth user, then swap out core's user
  351. // permissions js with LT's custom implementation. This is necessary to
  352. // prevent the pre-auth role's checkboxes from being automatically disabled
  353. // when the auth user's checkboxes are checked.
  354. if ($id != DRUPAL_AUTHENTICATED_RID) {
  355. $javascript['settings']['data'][] = array('LoginToboggan' => array('preAuthID' => $id));
  356. $file = drupal_get_path('module', 'logintoboggan') . '/logintoboggan.permissions.js';
  357. $javascript[$file] = drupal_js_defaults($file);
  358. $javascript[$file]['weight'] = 999;
  359. }
  360. }
  361. }
  362. /**
  363. * Implement hook_page_alter().
  364. */
  365. function logintoboggan_page_alter(&$page) {
  366. // Remove blocks on access denied pages.
  367. if (isset($page['#logintoboggan_denied'])) {
  368. drupal_set_message(t('Access denied. You may need to login below or register to access this page.'), 'error');
  369. // Allow overriding the removal of the sidebars, since there's no way to
  370. // override this in the theme.
  371. if (variable_get('logintoboggan_denied_remove_sidebars', TRUE)) {
  372. unset($page['sidebar_first'], $page['sidebar_second']);
  373. }
  374. }
  375. }
  376. /**
  377. * Custom submit function for user registration form
  378. *
  379. * @ingroup logintoboggan_form
  380. */
  381. function logintoboggan_user_register_submit($form, &$form_state) {
  382. $reg_pass_set = !variable_get('user_email_verification', TRUE);
  383. // Test here for a valid pre-auth -- if the pre-auth is set to the auth user, we
  384. // handle things a bit differently.
  385. $pre_auth = logintoboggan_validating_id() != DRUPAL_AUTHENTICATED_RID;
  386. // If we are allowing user selected passwords then skip the auto-generate function
  387. // The new user's status will be 1 (visitors can create own accounts) if reg_pass_set == 1
  388. // Immediate login, we are going to assign a pre-auth role, until email validation completed
  389. if ($reg_pass_set) {
  390. $pass = $form_state['values']['pass'];
  391. $status = 1;
  392. }
  393. else {
  394. $pass = user_password();
  395. $status = variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS;
  396. }
  397. // The unset below is needed to prevent these form values from being saved as
  398. // user data.
  399. form_state_values_clean($form_state);
  400. // Set the roles for the new user -- add the pre-auth role if they can pick their own password,
  401. // and the pre-auth role isn't anon or auth user.
  402. $validating_id = logintoboggan_validating_id();
  403. $roles = isset($form_state['values']['roles']) ? array_filter($form_state['values']['roles']) : array();
  404. if ($reg_pass_set && ($validating_id > DRUPAL_AUTHENTICATED_RID)) {
  405. $roles[$validating_id] = 1;
  406. }
  407. $form_state['values']['pass'] = $pass;
  408. $form_state['values']['init'] = $form_state['values']['mail'];
  409. $form_state['values']['roles'] = $roles;
  410. $form_state['values']['status'] = $status;
  411. $account = $form['#user'];
  412. entity_form_submit_build_entity('user', $account, $form, $form_state);
  413. // Populate $edit with the properties of $account, which have been edited on
  414. // this form by taking over all values, which appear in the form values too.
  415. $edit = array_intersect_key((array) $account, $form_state['values']);
  416. $account = user_save($account, $edit);
  417. // Terminate if an error occurred during user_save().
  418. if (!$account) {
  419. drupal_set_message(t("Error saving user account."), 'error');
  420. $form_state['redirect'] = '';
  421. return;
  422. }
  423. $form_state['user'] = $account;
  424. $form_state['values']['uid'] = $account->uid;
  425. watchdog('user', 'New user: %name (%email).', array('%name' => $form_state['values']['name'], '%email' => $form_state['values']['mail']), WATCHDOG_NOTICE, l(t('edit'), 'user/' . $account->uid . '/edit'));
  426. // Add plain text password into user account to generate mail tokens.
  427. $account->password = $pass;
  428. // Compose the appropriate user message. Validation emails are only sent if:
  429. // 1. Users can set their own password.
  430. // 2. The pre-auth role isn't the auth user.
  431. // 3. Visitors can create their own accounts.
  432. $message = t('Further instructions have been sent to your e-mail address.');
  433. if($reg_pass_set && $pre_auth && variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS) {
  434. $message = t('A validation e-mail has been sent to your e-mail address. You will need to follow the instructions in that message in order to gain full access to the site.');
  435. }
  436. if (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS) {
  437. // Create new user account, no administrator approval required.
  438. $mailkey = 'register_no_approval_required';
  439. } elseif (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) {
  440. // Create new user account, administrator approval required.
  441. $mailkey = 'register_pending_approval';
  442. $message = t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.<br />Once it has been approved, you will receive an e-mail containing further instructions.');
  443. }
  444. // Mail the user.
  445. _user_mail_notify($mailkey, $account);
  446. drupal_set_message($message);
  447. // where do we need to redirect after registration?
  448. $redirect = _logintoboggan_process_redirect(variable_get('logintoboggan_redirect_on_register', ''), $account);
  449. // Log the user in if they created the account and immediate login is enabled.
  450. if($reg_pass_set && variable_get('logintoboggan_immediate_login_on_register', TRUE)) {
  451. $form_state['redirect'] = logintoboggan_process_login($account, $form_state['values'], $redirect);
  452. }
  453. else {
  454. // Redirect to the appropriate page.
  455. $form_state['redirect'] = $redirect;
  456. }
  457. }
  458. /**
  459. * Custom validation for user login form
  460. *
  461. * @ingroup logintoboggan_form
  462. */
  463. function logintoboggan_user_login_validate($form, &$form_state) {
  464. if (isset($form_state['values']['name']) && $form_state['values']['name']) {
  465. if ($name = db_query("SELECT name FROM {users} WHERE LOWER(mail) = LOWER(:name)", array(
  466. ':name' => $form_state['values']['name'],
  467. ))->fetchField()) {
  468. form_set_value($form['name'], $name, $form_state);
  469. }
  470. }
  471. }
  472. /**
  473. * Custom validation function for user registration form
  474. *
  475. * @ingroup logintoboggan_form
  476. */
  477. function logintoboggan_user_register_validate($form, &$form_state) {
  478. //Check to see whether our username matches any email address currently in the system.
  479. if($mail = db_query("SELECT mail FROM {users} WHERE LOWER(:name) = LOWER(mail)", array(
  480. ':name' => $form_state['values']['name'],
  481. ))->fetchField()) {
  482. form_set_error('name', t('This e-mail has already been taken by another user.'));
  483. }
  484. //Check to see whether our e-mail address matches the confirm address if enabled.
  485. if (variable_get('logintoboggan_confirm_email_at_registration', 0) && isset($form_state['values']['conf_mail'])) {
  486. if (trim($form_state['values']['mail']) != trim($form_state['values']['conf_mail'])) {
  487. form_set_error('conf_mail', t('Your e-mail address and confirmed e-mail address must match.'));
  488. }
  489. }
  490. //Do some password validation if password selection is enabled.
  491. if (!variable_get('user_email_verification', TRUE)) {
  492. $pass_err = logintoboggan_validate_pass($form_state['values']['pass']);
  493. if ($pass_err) {
  494. form_set_error('pass', $pass_err);
  495. }
  496. }
  497. }
  498. /**
  499. * Custom validation function for user edit form
  500. *
  501. * @ingroup logintoboggan_form
  502. */
  503. function logintoboggan_user_edit_validate($form, &$form_state) {
  504. $account = $form['#user'];
  505. $edit = $form_state['values'];
  506. // If login with mail is enabled...
  507. if (variable_get('logintoboggan_login_with_email', 0)) {
  508. $uid = isset($account->uid) ? $account->uid : 0;
  509. // Check that no user is using this name for their email address.
  510. if (isset($edit['name']) && db_query("SELECT uid FROM {users} WHERE LOWER(mail) = LOWER(:mail) AND uid <> :uid", array(
  511. ':mail' => $edit['name'],
  512. ':uid' => $uid,
  513. ))->fetchField()) {
  514. form_set_error('name', t('This name has already been taken by another user.'));
  515. }
  516. // Check that no user is using this email address for their name.
  517. if (isset($edit['mail']) && db_query("SELECT uid FROM {users} WHERE LOWER(name) = LOWER(:name) AND uid <> :uid", array(
  518. ':name' => $edit['mail'],
  519. ':uid' => $uid,
  520. ))->fetchField()) {
  521. form_set_error('mail', t('This e-mail has already been taken by another user.'));
  522. }
  523. }
  524. if (!empty($edit['pass'])) {
  525. // if we're changing the password, validate it
  526. $pass_err = logintoboggan_validate_pass($edit['pass']);
  527. if ($pass_err) {
  528. form_set_error('pass', $pass_err);
  529. }
  530. }
  531. }
  532. /**
  533. * Implement hook_menu_get_item_alter()
  534. *
  535. * @ingroup logintoboggan_core
  536. *
  537. * This is currently the best place to dynamically remove the authenticated role
  538. * from the user object, hook_boot() allows us to act on the user object before
  539. * any access checks are performed.
  540. */
  541. function logintoboggan_boot() {
  542. global $user;
  543. // Make sure any user with pre-auth role doesn't have authenticated user role
  544. _logintoboggan_user_roles_alter($user);
  545. }
  546. /**
  547. * Alter user roles for loaded user account.
  548. *
  549. * If user is not an anonymous user, and the user has the pre-auth role, and the pre-auth role
  550. * isn't also the auth role, then unset the auth role for this user--they haven't validated yet.
  551. *
  552. * This alteration is required because sess_read() and user_load() automatically set the
  553. * authenticated user role for all non-anonymous users (see http://drupal.org/node/92361).
  554. *
  555. * @param &$account
  556. * User account to have roles adjusted.
  557. */
  558. function _logintoboggan_user_roles_alter($account) {
  559. $id = logintoboggan_validating_id();
  560. $in_pre_auth_role = in_array($id, array_keys($account->roles));
  561. if ($account->uid && $in_pre_auth_role) {
  562. if ($id != DRUPAL_AUTHENTICATED_RID) {
  563. unset($account->roles[DRUPAL_AUTHENTICATED_RID]);
  564. // Reset the permissions cache.
  565. drupal_static_reset('user_access');
  566. }
  567. }
  568. }
  569. /**
  570. * Implement hook_menu()
  571. *
  572. * @ingroup logintoboggan_core
  573. */
  574. function logintoboggan_menu() {
  575. $items = array();
  576. // Settings page.
  577. $items['admin/config/system/logintoboggan'] = array(
  578. 'title' => 'LoginToboggan',
  579. 'description' => 'Set up custom login options like instant login, login redirects, pre-authorized validation roles, etc.',
  580. 'page callback' => 'drupal_get_form',
  581. 'page arguments' => array('logintoboggan_main_settings'),
  582. 'access callback' => 'user_access',
  583. 'access arguments' => array('administer site configuration'),
  584. 'file' => 'logintoboggan.admin.inc',
  585. );
  586. // Callback for user validate routine.
  587. $items['user/validate/%user/%/%'] = array(
  588. 'title' => 'Validate e-mail address',
  589. 'page callback' => 'logintoboggan_validate_email',
  590. 'page arguments' => array(2, 3, 4),
  591. 'access callback' => 'logintoboggan_validate_email_access',
  592. 'access arguments' => array(2, 3),
  593. 'type' => MENU_CALLBACK,
  594. 'file' => 'logintoboggan.validation.inc',
  595. );
  596. // Callback for re-sending validation e-mail
  597. $items['toboggan/revalidate/%user'] = array(
  598. 'title' => 'Re-send validation e-mail',
  599. 'page callback' => 'logintoboggan_resend_validation',
  600. 'page arguments' => array(2),
  601. 'access callback' => 'logintoboggan_revalidate_access',
  602. 'access arguments' => array(2),
  603. 'type' => MENU_CALLBACK,
  604. 'file' => 'logintoboggan.validation.inc',
  605. );
  606. // Callback for handling access denied redirection.
  607. $items['toboggan/denied'] = array(
  608. 'access callback' => TRUE,
  609. 'page callback' => 'logintoboggan_denied',
  610. 'title' => 'Access denied',
  611. 'type' => MENU_CALLBACK,
  612. );
  613. return $items;
  614. }
  615. /**
  616. * Implementation of hook_menu_alter().
  617. */
  618. function logintoboggan_menu_alter(&$callbacks) {
  619. if (variable_get('logintoboggan_unified_login', 0)) {
  620. // Kill the tabs on the login pages.
  621. $callbacks['user/login']['type'] = MENU_NORMAL_ITEM;
  622. $callbacks['user/login']['page callback'] = 'logintoboggan_unified_login_page';
  623. $callbacks['user/register']['type'] = MENU_CALLBACK;
  624. $callbacks['user/register']['page callback'] = 'logintoboggan_unified_login_page';
  625. $callbacks['user/register']['page arguments'] = array('register');
  626. $callbacks['user/password']['type'] = MENU_CALLBACK;
  627. $callbacks['user']['page callback'] = 'logintoboggan_unified_login_page';
  628. }
  629. }
  630. /**
  631. * Access check for user e-mail validation.
  632. */
  633. function logintoboggan_validate_email_access($account, $timestamp) {
  634. return $account->uid && $timestamp < REQUEST_TIME;
  635. }
  636. /**
  637. * Access check for user revalidation.
  638. */
  639. function logintoboggan_revalidate_access($account) {
  640. return $GLOBALS['user']->uid && ($GLOBALS['user']->uid == $account->uid || user_access('administer users'));
  641. }
  642. /**
  643. * Menu callback for user/login
  644. * creates a unified login/registration form (without tabs)
  645. *
  646. * @param $active_form
  647. * Which form to display, should be 'login' or 'register'.
  648. */
  649. function logintoboggan_unified_login_page($active_form = 'login') {
  650. // Sanitise the $active_form text as it comes direct from the url.
  651. // It should only ever be 'login' or 'register', so default to 'login'.
  652. if ($active_form != 'login' && $active_form != 'register') {
  653. $active_form = 'login';
  654. }
  655. global $user;
  656. if ($user->uid) {
  657. menu_set_active_item('user/' . $user->uid);
  658. return menu_execute_active_handler(NULL, FALSE);
  659. }
  660. else {
  661. $output = logintoboggan_get_authentication_form($active_form);
  662. return $output;
  663. }
  664. }
  665. /**
  666. * Implemenation of hook_theme().
  667. *
  668. * @ingroup logintoboggan_core
  669. */
  670. function logintoboggan_theme($existing, $type, $theme, $path) {
  671. return array(
  672. 'lt_username_title' => array(
  673. 'variables' => array('form_id' => NULL),
  674. ),
  675. 'lt_username_description' => array(
  676. 'variables' => array('form_id' => NULL),
  677. ),
  678. 'lt_password_title' => array(
  679. 'variables' => array('form_id' => NULL),
  680. ),
  681. 'lt_password_description' => array(
  682. 'variables' => array('form_id' => NULL),
  683. ),
  684. 'lt_access_denied' => array(
  685. 'variables' => array(),
  686. ),
  687. 'lt_loggedinblock' => array(
  688. 'variables' => array('account' => NULL),
  689. ),
  690. 'lt_login_link' => array(
  691. 'variables' => array(),
  692. ),
  693. 'lt_login_successful_message' => array(
  694. 'variables' => array('account' => NULL),
  695. ),
  696. 'lt_unified_login_page' => array(
  697. 'variables' => array(
  698. 'login_form' => NULL,
  699. 'register_form' => NULL,
  700. 'active_form' => NULL,
  701. ),
  702. ),
  703. );
  704. }
  705. /**
  706. * @defgroup logintoboggan_block Functions for LoginToboggan blocks.
  707. */
  708. function logintoboggan_user_block_admin_configure_submit($form, &$form_state) {
  709. variable_set('logintoboggan_login_block_type', $form_state['values']['logintoboggan_login_block_type']);
  710. variable_set('logintoboggan_login_block_message', $form_state['values']['logintoboggan_login_block_message']);
  711. }
  712. /**
  713. * Implement hook_block_view().
  714. *
  715. * @ingroup logintoboggan_core
  716. */
  717. function logintoboggan_block_view($delta = '') {
  718. global $user;
  719. $block = array();
  720. switch ($delta) {
  721. case 'logintoboggan_logged_in':
  722. if ($user->uid) {
  723. $block['content'] = array(
  724. '#theme' => 'lt_loggedinblock',
  725. '#account' => $user,
  726. );
  727. }
  728. break;
  729. }
  730. return $block;
  731. }
  732. /**
  733. * Implement hook_block_info().
  734. *
  735. * @ingroup logintoboggan_core
  736. */
  737. function logintoboggan_block_info() {
  738. $blocks = array();
  739. $blocks['logintoboggan_logged_in'] = array(
  740. 'info' => t('LoginToboggan logged in block'),
  741. 'cache' => DRUPAL_NO_CACHE,
  742. );
  743. return $blocks;
  744. }
  745. /**
  746. * User login block with JavaScript to expand
  747. *
  748. * this should really be themed
  749. *
  750. * @return array
  751. * the reconstituted user login block
  752. */
  753. function _logintoboggan_toggleboggan ($form) {
  754. $form['#attached']['js'][] = drupal_get_path('module', 'logintoboggan') .'/logintoboggan.js';
  755. $pre = '<div id="toboggan-container" class="toboggan-container">';
  756. $options = array(
  757. 'attributes' => array(
  758. 'id' => 'toboggan-login-link',
  759. 'class' => array('toboggan-login-link'),
  760. ),
  761. 'query' => drupal_get_destination(),
  762. );
  763. $pre .= '<div id="toboggan-login-link-container" class="toboggan-login-link-container">';
  764. $pre .= l(theme('lt_login_link'), 'user/login', $options);
  765. $pre .= '</div>';
  766. //the block that will be toggled
  767. $pre .= '<div id="toboggan-login" class="user-login-block">';
  768. $form['pre'] = array('#markup' => $pre, '#weight' => -300);
  769. $form['post'] = array('#markup' => '</div></div>', '#weight' => 300);
  770. return $form;
  771. }
  772. /**
  773. * Builds a unified login form.
  774. *
  775. * @param $active_form
  776. * Which form to display, should be 'login' or 'register'.
  777. */
  778. function logintoboggan_unified_login_form($active_form = 'login') {
  779. $login_form = drupal_get_form('user_login');
  780. $login_form['#attached']['js'][] = drupal_get_path('module', 'logintoboggan') .'/logintoboggan.unifiedlogin.js';
  781. $login_form['#attached']['js'][] = array(
  782. 'data' => array(
  783. 'LoginToboggan' => array(
  784. 'unifiedLoginActiveForm' => $active_form,
  785. ),
  786. ),
  787. 'type' => 'setting',
  788. );
  789. $register_form = drupal_get_form('user_register_form');
  790. $rendered_login_form = drupal_render($login_form);
  791. $rendered_register_form = drupal_render($register_form);
  792. $variables = array(
  793. 'login_form' => $rendered_login_form,
  794. 'register_form' => $rendered_register_form,
  795. 'active_form' => $active_form,
  796. );
  797. $output = theme('lt_unified_login_page', $variables);
  798. return $output;
  799. }
  800. /**
  801. * Returns an appropriate authentication form based on the configuration.
  802. *
  803. * @param $active_form
  804. * Which form to display, should be 'login' or 'register'.
  805. */
  806. function logintoboggan_get_authentication_form($active_form = 'login') {
  807. $output = '';
  808. if (variable_get('logintoboggan_unified_login', 0)) {
  809. $output = logintoboggan_unified_login_form($active_form);
  810. }
  811. elseif ($active_form == 'login') {
  812. $output = drupal_get_form('user_login');
  813. }
  814. elseif ($active_form == 'register') {
  815. $output = drupal_get_form('user_register_form');
  816. }
  817. return $output;
  818. }
  819. function logintoboggan_denied() {
  820. if ($GLOBALS['user']->uid == 0) {
  821. drupal_set_title(t('Access Denied / User log in'));
  822. // Output the user login form.
  823. $output = logintoboggan_get_authentication_form('login');
  824. drupal_set_page_content($output);
  825. // Return page attributes, hide blocks.
  826. $page = element_info('page');
  827. $page['#logintoboggan_denied'] = TRUE;
  828. }
  829. else {
  830. drupal_set_title(t('Access Denied'));
  831. $page = theme('lt_access_denied');
  832. }
  833. return $page;
  834. }
  835. /**
  836. * Modified version of user_validate_name
  837. * - validates user submitted passwords have a certain length and only contain letters, numbers or punctuation (graph character class in regex)
  838. */
  839. function logintoboggan_validate_pass($pass) {
  840. if (!strlen($pass)) return t('You must enter a password.');
  841. if (preg_match('/[\x{80}-\x{A0}'. // Non-printable ISO-8859-1 + NBSP
  842. '\x{A1}-\x{F7}'. // Latin punctuations
  843. '\x{AD}'. // Soft-hyphen
  844. '\x{2000}-\x{200F}'. // Various space characters
  845. '\x{2028}-\x{202F}'. // Bidirectional text overrides
  846. '\x{205F}-\x{206F}'. // Various text hinting characters
  847. '\x{FEFF}'. // Byte order mark
  848. '\x{FF01}-\x{FF60}'. // Full-width latin
  849. '\x{FFF9}-\x{FFFD}]/u', // Replacement characters
  850. $pass)) {
  851. return t('The password contains an illegal character.');
  852. }
  853. $min_pass_length = variable_get('logintoboggan_minimum_password_length', 0);
  854. if ($min_pass_length && strlen($pass) < $min_pass_length) return t("The password is too short: it must be at least %min_length characters.", array('%min_length' => $min_pass_length));
  855. }
  856. /**
  857. * Modified version of $DRUPAL_AUTHENTICATED_RID
  858. * - gets the role id for the "validating" user role.
  859. */
  860. function logintoboggan_validating_id() {
  861. return variable_get('logintoboggan_pre_auth_role', DRUPAL_AUTHENTICATED_RID);
  862. }
  863. function _logintoboggan_process_validation($account) {
  864. // Test here for a valid pre-auth -- if the pre-auth is set to the auth user, we
  865. // handle things a bit differently.
  866. $validating_id = logintoboggan_validating_id();
  867. $pre_auth = !variable_get('user_email_verification', TRUE) && $validating_id != DRUPAL_AUTHENTICATED_RID;
  868. // Remove the pre-auth role from the user, unless they haven't been approved yet.
  869. if ($account->status) {
  870. if ($pre_auth) {
  871. db_delete('users_roles')
  872. ->condition('uid', $account->uid)
  873. ->condition('rid', $validating_id)
  874. ->execute();
  875. }
  876. }
  877. // Reload the user object freshly, since the cached value may have stale
  878. // roles, and to prepare for the possible user_save() below.
  879. $account = user_load($account->uid, TRUE);
  880. // Allow other modules to react to email validation by invoking the user update hook.
  881. // This should only be triggered if LT's custom validation is active.
  882. if (!variable_get('user_email_verification', TRUE)) {
  883. $edit = array();
  884. $account->logintoboggan_email_validated = TRUE;
  885. user_module_invoke('update', $edit, $account);
  886. }
  887. }
  888. /**
  889. * Actually log the user on
  890. *
  891. * @param object $account
  892. * The user object.
  893. * @param array $edit
  894. * An array of form values if a form has been submitted.
  895. * @param array $redirect
  896. * An array of key/value pairs describing a redirect location, in the form
  897. * that drupal_goto() will understand. Defaults to:
  898. * 'path' => 'user/'. $user->uid
  899. * 'query' => NULL
  900. * 'fragment' => NULL
  901. */
  902. function logintoboggan_process_login($account, &$edit, $redirect = array()){
  903. global $user;
  904. $user = user_load($account->uid);
  905. user_login_finalize($edit);
  906. // In the special case where a user is validating but they did not create their
  907. // own password, show a user message letting them know to change their password.
  908. if (variable_get('user_email_verification', TRUE)) {
  909. watchdog('user', 'User %name used one-time login link at time %timestamp.', array('%name' => $user->name, '%timestamp' => REQUEST_TIME));
  910. drupal_set_message(t('You have just used your one-time login link. It is no longer possible to use this link to login. Please change your password.'));
  911. }
  912. if (isset($redirect[0]) && $redirect[0] != '') {
  913. return $redirect;
  914. }
  915. return array(
  916. 'user/'. $user->uid,
  917. array(
  918. 'query' => array(),
  919. 'fragment' => '',
  920. ),
  921. );
  922. }
  923. function logintoboggan_eml_validate_url($account, $url_options){
  924. $timestamp = REQUEST_TIME;
  925. return url("user/validate/$account->uid/$timestamp/". logintoboggan_eml_rehash($account->pass, $timestamp, $account->mail, $account->uid), $url_options);
  926. }
  927. function logintoboggan_eml_rehash($password, $timestamp, $mail, $uid) {
  928. return user_pass_rehash($password, $timestamp, $mail, $uid);
  929. }
  930. /**
  931. * Implement hook_user_login().
  932. */
  933. function logintoboggan_user_login(&$edit, $account) {
  934. if (variable_get('logintoboggan_login_successful_message', 0)) {
  935. drupal_set_message(theme('lt_login_successful_message', array('account' => $account)));
  936. }
  937. }
  938. /**
  939. * Implement hook_user_load().
  940. */
  941. function logintoboggan_user_load($users) {
  942. foreach ($users as $account) {
  943. // If the user has the pre-auth role, unset the authenticated role
  944. _logintoboggan_user_roles_alter($account);
  945. }
  946. }
  947. /**
  948. * Implement hook_user_update().
  949. */
  950. function logintoboggan_user_update(&$edit, $account, $category) {
  951. // Only perform this check if an admin is editing the account.
  952. if (user_access('administer users') && isset($edit['roles'])) {
  953. // Check to see if roles present, and the pre-auth role was present when
  954. // the form was initially displayed.
  955. if (isset($edit['logintoboggan_pre_auth_check'])) {
  956. // If the pre-auth is set to the auth user, then no further checking is
  957. // necessary.
  958. $validating_id = logintoboggan_validating_id();
  959. $pre_auth = !variable_get('user_email_verification', TRUE) && $validating_id != DRUPAL_AUTHENTICATED_RID;
  960. if ($pre_auth) {
  961. // Check to see if an admin has manually removed the pre-auth role from
  962. // the user. If so, send the account activation email.
  963. if (!isset($edit['roles'][$validating_id]) || !$edit['roles'][$validating_id]) {
  964. // Mail the user, letting them know their account now has auth user perms.
  965. _user_mail_notify('status_activated', $account);
  966. }
  967. }
  968. }
  969. }
  970. }
  971. function _logintoboggan_protocol() {
  972. return ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http');
  973. }
  974. /**
  975. * Transforms a URL fragment into a redirect array understood by drupal_goto().
  976. *
  977. * @param $redirect
  978. * The redirect string.
  979. * @param $account
  980. * The user account object associated with the redirect.
  981. */
  982. function _logintoboggan_process_redirect($redirect, $account) {
  983. $variables = array('%uid' => $account->uid);
  984. $redirect = parse_url(urldecode(strtr($redirect, $variables)));
  985. // If there's a path set, override the destination parameter if necessary.
  986. if ($redirect['path'] && variable_get('logintoboggan_override_destination_parameter', 1)) {
  987. unset($_GET['destination']);
  988. }
  989. // Explicitly create query and fragment elements if not present already.
  990. $query = isset($redirect['query']) ? $redirect['query'] : array();
  991. $fragment = isset($redirect['fragment']) ? $redirect['fragment'] : '';
  992. return array(
  993. $redirect['path'],
  994. array(
  995. 'query' => $query,
  996. 'fragment' => $fragment,
  997. ),
  998. );
  999. }
  1000. /**
  1001. * Implement hook_mail_alter().
  1002. */
  1003. function logintoboggan_mail_alter(&$message) {
  1004. if ($message['id'] == 'user_register_pending_approval_admin') {
  1005. $reg_pass_set = !variable_get('user_email_verification', TRUE);
  1006. if ($reg_pass_set) {
  1007. $account = $message['params']['account'];
  1008. $url_options = array('absolute' => TRUE);
  1009. $language = $message['language'];
  1010. $langcode = isset($language) ? $language->language : NULL;
  1011. $message['body'][] = t("\n\nThe user has automatically received the permissions of the LoginToboggan validating role. To give the user full site permissions, click the link below:\n\n!validation_url/admin\n\nAlternatively, you may visit their user account listed above and remove them from the validating role.", array('!validation_url' => logintoboggan_eml_validate_url($account, $url_options)), array('langcode' => $langcode));
  1012. }
  1013. }
  1014. }
  1015. /**
  1016. *
  1017. * THEME FUNCTIONS!
  1018. *
  1019. * You may override and change any of these custom HTML output functions
  1020. * by copy/pasting them into your template.php file, at which point you can
  1021. * customize anything, provided you are using the default phptemplate engine.
  1022. *
  1023. * For more info on overriding theme functions, see http://drupal.org/node/55126
  1024. */
  1025. /**
  1026. * Theme the username title of the user login form
  1027. * and the user login block.
  1028. */
  1029. function theme_lt_username_title($variables) {
  1030. switch ($variables['form_id']) {
  1031. case 'user_login':
  1032. // Label text for the username field on the /user/login page.
  1033. return t('Username or e-mail address');
  1034. break;
  1035. case 'user_login_block':
  1036. // Label text for the username field when shown in a block.
  1037. return t('Username or e-mail');
  1038. break;
  1039. }
  1040. }
  1041. /**
  1042. * Theme the username description of the user login form
  1043. * and the user login block.
  1044. */
  1045. function theme_lt_username_description($variables) {
  1046. switch ($variables['form_id']) {
  1047. case 'user_login':
  1048. // The username field's description when shown on the /user/login page.
  1049. return t('You may login with either your assigned username or your e-mail address.');
  1050. break;
  1051. case 'user_login_block':
  1052. return '';
  1053. break;
  1054. }
  1055. }
  1056. /**
  1057. * Theme the password title of the user login form
  1058. * and the user login block.
  1059. */
  1060. function theme_lt_password_title($variables) {
  1061. // Label text for the password field.
  1062. return t('Password');
  1063. }
  1064. /**
  1065. * Theme the password description of the user login form
  1066. * and the user login block.
  1067. */
  1068. function theme_lt_password_description($variables) {
  1069. switch ($variables['form_id']) {
  1070. case 'user_login':
  1071. // The password field's description on the /user/login page.
  1072. return t('The password field is case sensitive.');
  1073. break;
  1074. case 'user_login_block':
  1075. // If showing the login form in a block, don't print any descriptive text.
  1076. return '';
  1077. break;
  1078. }
  1079. }
  1080. /**
  1081. * Theme the Access Denied message.
  1082. */
  1083. function theme_lt_access_denied($variables) {
  1084. return t('You are not authorized to access this page.');
  1085. }
  1086. /**
  1087. * Theme the loggedinblock that shows for logged-in users.
  1088. */
  1089. function theme_lt_loggedinblock($variables){
  1090. return theme('username', array('account' => $variables['account'])) .' | ' . l(t('Log out'), 'user/logout');
  1091. }
  1092. /**
  1093. * Custom theme function for the login/register link.
  1094. */
  1095. function theme_lt_login_link($variables) {
  1096. // Only display register text if registration is allowed.
  1097. if (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)) {
  1098. return t('Log in/Register');
  1099. }
  1100. else {
  1101. return t('Log in');
  1102. }
  1103. }
  1104. /**
  1105. * Theme the login successful message.
  1106. *
  1107. * @param $account
  1108. * A user object representing the user being logged in.
  1109. */
  1110. function theme_lt_login_successful_message($variables) {
  1111. return t('Log in successful for %name.', array('%name' => format_username($variables['account'])));
  1112. }
  1113. /**
  1114. * Theme function for unified login page.
  1115. *
  1116. * @ingroup themable
  1117. */
  1118. function theme_lt_unified_login_page($variables) {
  1119. $login_form = $variables['login_form'];
  1120. $register_form = $variables['register_form'];
  1121. $active_form = $variables['active_form'];
  1122. $output = '';
  1123. $output .= '<div class="toboggan-unified ' . $active_form . '">';
  1124. // Create the initial message and links that people can click on.
  1125. $output .= '<div id="login-message">' . t('You are not logged in.') . '</div>';
  1126. $output .= '<div id="login-links">';
  1127. $output .= l(t('I have an account'), 'user/login', array('attributes' => array('class' => array('login-link'), 'id' => 'login-link')));
  1128. $output .= ' ';
  1129. $output .= l(t('I want to create an account'), 'user/register', array('attributes' => array('class' => array('login-link'), 'id' => 'register-link')));
  1130. $output .= '</div>';
  1131. // Add the login and registration forms in.
  1132. $output .= '<div id="login-form">' . $login_form . '</div>';
  1133. $output .= '<div id="register-form">' . $register_form . '</div>';
  1134. $output .= '</div>';
  1135. return $output;
  1136. }