logintoboggan.module 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  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_user_admin_permissions_alter().
  274. *
  275. * @ingroup logintoboggan_core
  276. */
  277. function logintoboggan_form_user_admin_permissions_alter(&$form, &$form_state) {
  278. // If the pre-auth role isn't the auth user, then add it as a setting.
  279. $id = logintoboggan_validating_id();
  280. if ($id != DRUPAL_AUTHENTICATED_RID) {
  281. $form['#attached']['js'][] = array(
  282. 'data' => array(
  283. 'LoginToboggan' => array(
  284. 'preAuthID' => $id,
  285. ),
  286. ),
  287. 'type' => 'setting',
  288. );
  289. }
  290. }
  291. /**
  292. * Implement hook_form_alter().
  293. *
  294. * @ingroup logintoboggan_core
  295. */
  296. function logintoboggan_form_alter(&$form, &$form_state, $form_id) {
  297. switch ($form_id) {
  298. case 'user_login':
  299. case 'user_login_block':
  300. // Grab the message from settings for display at the top of the login block.
  301. if ($login_msg = variable_get('logintoboggan_login_block_message', '')) {
  302. $form['message'] = array(
  303. '#markup' => filter_xss_admin($login_msg),
  304. '#weight' => -50,
  305. );
  306. }
  307. if (variable_get('logintoboggan_login_with_email', 0)) {
  308. // Ensure a valid validate array.
  309. $form['#validate'] = is_array($form['#validate']) ? $form['#validate'] : array();
  310. // LT's validation function must run first.
  311. array_unshift($form['#validate'],'logintoboggan_user_login_validate');
  312. // Use theme functions to print the username field's textual labels.
  313. $form['name']['#title'] = theme('lt_username_title', array('form_id' => $form_id));
  314. $form['name']['#description'] = theme('lt_username_description', array('form_id' => $form_id));
  315. // Use theme functions to print the password field's textual labels.
  316. $form['pass']['#title'] = theme('lt_password_title', array('form_id' => $form_id));
  317. $form['pass']['#description'] = theme('lt_password_description', array('form_id' => $form_id));
  318. }
  319. if (($form_id == 'user_login_block')) {
  320. $block_type = variable_get('logintoboggan_login_block_type', 0);
  321. if ($block_type == 1) {
  322. // What would really be nice here is to start with a clean form, but
  323. // we can't really do that, because drupal_prepare_form() has already
  324. // been run, and we can't run it again in the _alter() hook, or we'll
  325. // get into and endless loop. Since we don't know exactly what's in
  326. // the form, strip out all regular form elements and the handlers.
  327. foreach (element_children($form) as $element) {
  328. unset($form[$element]);
  329. // OpenID expects this key, so provide it to prevent notices.
  330. if (module_exists("openid")) {
  331. $form['name']['#size'] = 0;
  332. }
  333. }
  334. unset($form['#validate'], $form['#submit']);
  335. $form['logintoboggan_login_link'] = array(
  336. '#markup' => l(theme('lt_login_link'), 'user/login', array('query' => drupal_get_destination())),
  337. );
  338. }
  339. elseif ($block_type == 2) {
  340. $form = _logintoboggan_toggleboggan($form);
  341. }
  342. }
  343. else {
  344. if (variable_get('logintoboggan_unified_login', 0)) {
  345. $form['lost_password'] = array(
  346. '#markup' => '<div class="login-forgot">' . l(t('Request new password'), 'user/password') . '</div>',
  347. );
  348. }
  349. }
  350. break;
  351. case 'user_admin_settings':
  352. // Disable the checkbox at the Account settings page which controls
  353. // whether e-mail verification is required upon registration or not.
  354. // The LoginToboggan module implements e-mail verification functionality
  355. // differently than core, and will control wether e-mail verification is
  356. // required or not.
  357. $form['registration_cancellation']['user_email_verification']['#disabled'] = true;
  358. $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')));
  359. break;
  360. }
  361. }
  362. /**
  363. * Implement hook_js_alter().
  364. */
  365. function logintoboggan_js_alter(&$javascript) {
  366. // Look for the user permissions js.
  367. if (isset($javascript['modules/user/user.permissions.js'])) {
  368. $id = logintoboggan_validating_id();
  369. // If the pre-auth user isn't the auth user, then swap out core's user
  370. // permissions js with LT's custom implementation. This is necessary to
  371. // prevent the pre-auth role's checkboxes from being automatically disabled
  372. // when the auth user's checkboxes are checked.
  373. if ($id != DRUPAL_AUTHENTICATED_RID) {
  374. $javascript['modules/user/user.permissions.js']['data'] = drupal_get_path('module', 'logintoboggan') . '/logintoboggan.permissions.js';
  375. }
  376. }
  377. }
  378. /**
  379. * Implement hook_page_alter().
  380. */
  381. function logintoboggan_page_alter(&$page) {
  382. // Remove blocks on access denied pages.
  383. if (isset($page['#logintoboggan_denied'])) {
  384. drupal_set_message(t('Access denied. You may need to login below or register to access this page.'), 'error');
  385. // Allow overriding the removal of the sidebars, since there's no way to
  386. // override this in the theme.
  387. if (variable_get('logintoboggan_denied_remove_sidebars', TRUE)) {
  388. unset($page['sidebar_first'], $page['sidebar_second']);
  389. }
  390. }
  391. }
  392. /**
  393. * Custom submit function for user registration form
  394. *
  395. * @ingroup logintoboggan_form
  396. */
  397. function logintoboggan_user_register_submit($form, &$form_state) {
  398. $reg_pass_set = !variable_get('user_email_verification', TRUE);
  399. // Test here for a valid pre-auth -- if the pre-auth is set to the auth user, we
  400. // handle things a bit differently.
  401. $pre_auth = logintoboggan_validating_id() != DRUPAL_AUTHENTICATED_RID;
  402. // If we are allowing user selected passwords then skip the auto-generate function
  403. // The new user's status will be 1 (visitors can create own accounts) if reg_pass_set == 1
  404. // Immediate login, we are going to assign a pre-auth role, until email validation completed
  405. if ($reg_pass_set) {
  406. $pass = $form_state['values']['pass'];
  407. $status = 1;
  408. }
  409. else {
  410. $pass = user_password();
  411. $status = variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS;
  412. }
  413. // The unset below is needed to prevent these form values from being saved as
  414. // user data.
  415. form_state_values_clean($form_state);
  416. // Set the roles for the new user -- add the pre-auth role if they can pick their own password,
  417. // and the pre-auth role isn't anon or auth user.
  418. $validating_id = logintoboggan_validating_id();
  419. $roles = isset($form_state['values']['roles']) ? array_filter($form_state['values']['roles']) : array();
  420. if ($reg_pass_set && ($validating_id > DRUPAL_AUTHENTICATED_RID)) {
  421. $roles[$validating_id] = 1;
  422. }
  423. $form_state['values']['pass'] = $pass;
  424. $form_state['values']['init'] = $form_state['values']['mail'];
  425. $form_state['values']['roles'] = $roles;
  426. $form_state['values']['status'] = $status;
  427. $account = $form['#user'];
  428. entity_form_submit_build_entity('user', $account, $form, $form_state);
  429. // Populate $edit with the properties of $account, which have been edited on
  430. // this form by taking over all values, which appear in the form values too.
  431. $edit = array_intersect_key((array) $account, $form_state['values']);
  432. $account = user_save($account, $edit);
  433. // Terminate if an error occurred during user_save().
  434. if (!$account) {
  435. drupal_set_message(t("Error saving user account."), 'error');
  436. $form_state['redirect'] = '';
  437. return;
  438. }
  439. $form_state['user'] = $account;
  440. $form_state['values']['uid'] = $account->uid;
  441. 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'));
  442. // Add plain text password into user account to generate mail tokens.
  443. $account->password = $pass;
  444. // Compose the appropriate user message. Validation emails are only sent if:
  445. // 1. Users can set their own password.
  446. // 2. The pre-auth role isn't the auth user.
  447. // 3. Visitors can create their own accounts.
  448. $message = t('Further instructions have been sent to your e-mail address.');
  449. if($reg_pass_set && $pre_auth && variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS) {
  450. $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.');
  451. }
  452. if (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS) {
  453. // Create new user account, no administrator approval required.
  454. $mailkey = 'register_no_approval_required';
  455. } elseif (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) {
  456. // Create new user account, administrator approval required.
  457. $mailkey = 'register_pending_approval';
  458. $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.');
  459. }
  460. // Mail the user.
  461. _user_mail_notify($mailkey, $account);
  462. drupal_set_message($message);
  463. // where do we need to redirect after registration?
  464. $redirect = _logintoboggan_process_redirect(variable_get('logintoboggan_redirect_on_register', ''), $account);
  465. // Log the user in if they created the account and immediate login is enabled.
  466. if($reg_pass_set && variable_get('logintoboggan_immediate_login_on_register', TRUE)) {
  467. $form_state['redirect'] = logintoboggan_process_login($account, $form_state['values'], $redirect);
  468. }
  469. else {
  470. // Redirect to the appropriate page.
  471. $form_state['redirect'] = $redirect;
  472. }
  473. }
  474. /**
  475. * Custom validation for user login form
  476. *
  477. * @ingroup logintoboggan_form
  478. */
  479. function logintoboggan_user_login_validate($form, &$form_state) {
  480. if (isset($form_state['values']['name']) && $form_state['values']['name']) {
  481. if ($name = db_query("SELECT name FROM {users} WHERE LOWER(mail) = LOWER(:name)", array(
  482. ':name' => $form_state['values']['name'],
  483. ))->fetchField()) {
  484. form_set_value($form['name'], $name, $form_state);
  485. }
  486. }
  487. }
  488. /**
  489. * Custom validation function for user registration form
  490. *
  491. * @ingroup logintoboggan_form
  492. */
  493. function logintoboggan_user_register_validate($form, &$form_state) {
  494. //Check to see whether our username matches any email address currently in the system.
  495. if($mail = db_query("SELECT mail FROM {users} WHERE LOWER(:name) = LOWER(mail)", array(
  496. ':name' => $form_state['values']['name'],
  497. ))->fetchField()) {
  498. form_set_error('name', t('This e-mail has already been taken by another user.'));
  499. }
  500. //Check to see whether our e-mail address matches the confirm address if enabled.
  501. if (variable_get('logintoboggan_confirm_email_at_registration', 0) && isset($form_state['values']['conf_mail'])) {
  502. if (trim($form_state['values']['mail']) != trim($form_state['values']['conf_mail'])) {
  503. form_set_error('conf_mail', t('Your e-mail address and confirmed e-mail address must match.'));
  504. }
  505. }
  506. //Do some password validation if password selection is enabled.
  507. if (!variable_get('user_email_verification', TRUE)) {
  508. $pass_err = logintoboggan_validate_pass($form_state['values']['pass']);
  509. if ($pass_err) {
  510. form_set_error('pass', $pass_err);
  511. }
  512. }
  513. }
  514. /**
  515. * Custom validation function for user edit form
  516. *
  517. * @ingroup logintoboggan_form
  518. */
  519. function logintoboggan_user_edit_validate($form, &$form_state) {
  520. $account = $form['#user'];
  521. $edit = $form_state['values'];
  522. // If login with mail is enabled...
  523. if (variable_get('logintoboggan_login_with_email', 0)) {
  524. $uid = isset($account->uid) ? $account->uid : 0;
  525. // Check that no user is using this name for their email address.
  526. if (isset($edit['name']) && db_query("SELECT uid FROM {users} WHERE LOWER(mail) = LOWER(:mail) AND uid <> :uid", array(
  527. ':mail' => $edit['name'],
  528. ':uid' => $uid,
  529. ))->fetchField()) {
  530. form_set_error('name', t('This name has already been taken by another user.'));
  531. }
  532. // Check that no user is using this email address for their name.
  533. if (isset($edit['mail']) && db_query("SELECT uid FROM {users} WHERE LOWER(name) = LOWER(:name) AND uid <> :uid", array(
  534. ':name' => $edit['mail'],
  535. ':uid' => $uid,
  536. ))->fetchField()) {
  537. form_set_error('mail', t('This e-mail has already been taken by another user.'));
  538. }
  539. }
  540. if (!empty($edit['pass'])) {
  541. // if we're changing the password, validate it
  542. $pass_err = logintoboggan_validate_pass($edit['pass']);
  543. if ($pass_err) {
  544. form_set_error('pass', $pass_err);
  545. }
  546. }
  547. }
  548. /**
  549. * Implement hook_menu_get_item_alter()
  550. *
  551. * @ingroup logintoboggan_core
  552. *
  553. * This is currently the best place to dynamically remove the authenticated role
  554. * from the user object, hook_boot() allows us to act on the user object before
  555. * any access checks are performed.
  556. */
  557. function logintoboggan_boot() {
  558. global $user;
  559. // Make sure any user with pre-auth role doesn't have authenticated user role
  560. _logintoboggan_user_roles_alter($user);
  561. }
  562. /**
  563. * Alter user roles for loaded user account.
  564. *
  565. * If user is not an anonymous user, and the user has the pre-auth role, and the pre-auth role
  566. * isn't also the auth role, then unset the auth role for this user--they haven't validated yet.
  567. *
  568. * This alteration is required because sess_read() and user_load() automatically set the
  569. * authenticated user role for all non-anonymous users (see http://drupal.org/node/92361).
  570. *
  571. * @param &$account
  572. * User account to have roles adjusted.
  573. */
  574. function _logintoboggan_user_roles_alter($account) {
  575. $id = logintoboggan_validating_id();
  576. $in_pre_auth_role = in_array($id, array_keys($account->roles));
  577. if ($account->uid && $in_pre_auth_role) {
  578. if ($id != DRUPAL_AUTHENTICATED_RID) {
  579. unset($account->roles[DRUPAL_AUTHENTICATED_RID]);
  580. // Reset the permissions cache.
  581. drupal_static_reset('user_access');
  582. }
  583. }
  584. }
  585. /**
  586. * Implement hook_menu()
  587. *
  588. * @ingroup logintoboggan_core
  589. */
  590. function logintoboggan_menu() {
  591. $items = array();
  592. // Settings page.
  593. $items['admin/config/system/logintoboggan'] = array(
  594. 'title' => 'LoginToboggan',
  595. 'description' => 'Set up custom login options like instant login, login redirects, pre-authorized validation roles, etc.',
  596. 'page callback' => 'drupal_get_form',
  597. 'page arguments' => array('logintoboggan_main_settings'),
  598. 'access callback' => 'user_access',
  599. 'access arguments' => array('administer site configuration'),
  600. 'file' => 'logintoboggan.admin.inc',
  601. );
  602. // Callback for user validate routine.
  603. $items['user/validate/%user/%/%'] = array(
  604. 'title' => 'Validate e-mail address',
  605. 'page callback' => 'logintoboggan_validate_email',
  606. 'page arguments' => array(2, 3, 4),
  607. 'access callback' => 'logintoboggan_validate_email_access',
  608. 'access arguments' => array(2, 3),
  609. 'type' => MENU_CALLBACK,
  610. 'file' => 'logintoboggan.validation.inc',
  611. );
  612. // Callback for re-sending validation e-mail
  613. $items['toboggan/revalidate/%user'] = array(
  614. 'title' => 'Re-send validation e-mail',
  615. 'page callback' => 'logintoboggan_resend_validation',
  616. 'page arguments' => array(2),
  617. 'access callback' => 'logintoboggan_revalidate_access',
  618. 'access arguments' => array(2),
  619. 'type' => MENU_CALLBACK,
  620. 'file' => 'logintoboggan.validation.inc',
  621. );
  622. // Callback for handling access denied redirection.
  623. $items['toboggan/denied'] = array(
  624. 'access callback' => TRUE,
  625. 'page callback' => 'logintoboggan_denied',
  626. 'title' => 'Access denied',
  627. 'type' => MENU_CALLBACK,
  628. );
  629. return $items;
  630. }
  631. /**
  632. * Implementation of hook_menu_alter().
  633. */
  634. function logintoboggan_menu_alter(&$callbacks) {
  635. if (variable_get('logintoboggan_unified_login', 0)) {
  636. // Kill the tabs on the login pages.
  637. $callbacks['user/login']['type'] = MENU_NORMAL_ITEM;
  638. $callbacks['user/login']['page callback'] = 'logintoboggan_unified_login_page';
  639. $callbacks['user/register']['type'] = MENU_CALLBACK;
  640. $callbacks['user/register']['page callback'] = 'logintoboggan_unified_login_page';
  641. $callbacks['user/register']['page arguments'] = array('register');
  642. $callbacks['user/password']['type'] = MENU_CALLBACK;
  643. $callbacks['user']['page callback'] = 'logintoboggan_unified_login_page';
  644. }
  645. }
  646. /**
  647. * Access check for user e-mail validation.
  648. */
  649. function logintoboggan_validate_email_access($account, $timestamp) {
  650. return $account->uid && $timestamp < REQUEST_TIME;
  651. }
  652. /**
  653. * Access check for user revalidation.
  654. */
  655. function logintoboggan_revalidate_access($account) {
  656. return $GLOBALS['user']->uid && ($GLOBALS['user']->uid == $account->uid || user_access('administer users'));
  657. }
  658. /**
  659. * Menu callback for user/login
  660. * creates a unified login/registration form (without tabs)
  661. *
  662. * @param $active_form
  663. * Which form to display, should be 'login' or 'register'.
  664. */
  665. function logintoboggan_unified_login_page($active_form = 'login') {
  666. // Sanitise the $active_form text as it comes direct from the url.
  667. // It should only ever be 'login' or 'register', so default to 'login'.
  668. if ($active_form != 'login' && $active_form != 'register') {
  669. $active_form = 'login';
  670. }
  671. global $user;
  672. if ($user->uid) {
  673. menu_set_active_item('user/' . $user->uid);
  674. return menu_execute_active_handler(NULL, FALSE);
  675. }
  676. else {
  677. $output = logintoboggan_get_authentication_form($active_form);
  678. return $output;
  679. }
  680. }
  681. /**
  682. * Implemenation of hook_theme().
  683. *
  684. * @ingroup logintoboggan_core
  685. */
  686. function logintoboggan_theme($existing, $type, $theme, $path) {
  687. return array(
  688. 'lt_username_title' => array(
  689. 'variables' => array('form_id' => NULL),
  690. ),
  691. 'lt_username_description' => array(
  692. 'variables' => array('form_id' => NULL),
  693. ),
  694. 'lt_password_title' => array(
  695. 'variables' => array('form_id' => NULL),
  696. ),
  697. 'lt_password_description' => array(
  698. 'variables' => array('form_id' => NULL),
  699. ),
  700. 'lt_access_denied' => array(
  701. 'variables' => array(),
  702. ),
  703. 'lt_loggedinblock' => array(
  704. 'variables' => array('account' => NULL),
  705. ),
  706. 'lt_login_link' => array(
  707. 'variables' => array(),
  708. ),
  709. 'lt_login_successful_message' => array(
  710. 'variables' => array('account' => NULL),
  711. ),
  712. 'lt_unified_login_page' => array(
  713. 'variables' => array(
  714. 'login_form' => NULL,
  715. 'register_form' => NULL,
  716. 'active_form' => NULL,
  717. ),
  718. ),
  719. );
  720. }
  721. /**
  722. * @defgroup logintoboggan_block Functions for LoginToboggan blocks.
  723. */
  724. function logintoboggan_user_block_admin_configure_submit($form, &$form_state) {
  725. variable_set('logintoboggan_login_block_type', $form_state['values']['logintoboggan_login_block_type']);
  726. variable_set('logintoboggan_login_block_message', $form_state['values']['logintoboggan_login_block_message']);
  727. }
  728. /**
  729. * Implement hook_block_view().
  730. *
  731. * @ingroup logintoboggan_core
  732. */
  733. function logintoboggan_block_view($delta = '') {
  734. global $user;
  735. $block = array();
  736. switch ($delta) {
  737. case 'logintoboggan_logged_in':
  738. if ($user->uid) {
  739. $block['content'] = array(
  740. '#theme' => 'lt_loggedinblock',
  741. '#account' => $user,
  742. );
  743. }
  744. break;
  745. }
  746. return $block;
  747. }
  748. /**
  749. * Implement hook_block_info().
  750. *
  751. * @ingroup logintoboggan_core
  752. */
  753. function logintoboggan_block_info() {
  754. $blocks = array();
  755. $blocks['logintoboggan_logged_in'] = array(
  756. 'info' => t('LoginToboggan logged in block'),
  757. 'cache' => DRUPAL_NO_CACHE,
  758. );
  759. return $blocks;
  760. }
  761. /**
  762. * User login block with JavaScript to expand
  763. *
  764. * this should really be themed
  765. *
  766. * @return array
  767. * the reconstituted user login block
  768. */
  769. function _logintoboggan_toggleboggan ($form) {
  770. $form['#attached']['js'][] = drupal_get_path('module', 'logintoboggan') .'/logintoboggan.js';
  771. $pre = '<div id="toboggan-container" class="toboggan-container">';
  772. $options = array(
  773. 'attributes' => array(
  774. 'id' => 'toboggan-login-link',
  775. 'class' => array('toboggan-login-link'),
  776. ),
  777. 'query' => drupal_get_destination(),
  778. );
  779. $pre .= '<div id="toboggan-login-link-container" class="toboggan-login-link-container">';
  780. $pre .= l(theme('lt_login_link'), 'user/login', $options);
  781. $pre .= '</div>';
  782. //the block that will be toggled
  783. $pre .= '<div id="toboggan-login" class="user-login-block">';
  784. $form['pre'] = array('#markup' => $pre, '#weight' => -300);
  785. $form['post'] = array('#markup' => '</div></div>', '#weight' => 300);
  786. return $form;
  787. }
  788. /**
  789. * Builds a unified login form.
  790. *
  791. * @param $active_form
  792. * Which form to display, should be 'login' or 'register'.
  793. */
  794. function logintoboggan_unified_login_form($active_form = 'login') {
  795. $login_form = drupal_get_form('user_login');
  796. $login_form['#attached']['js'][] = drupal_get_path('module', 'logintoboggan') .'/logintoboggan.unifiedlogin.js';
  797. $login_form['#attached']['js'][] = array(
  798. 'data' => array(
  799. 'LoginToboggan' => array(
  800. 'unifiedLoginActiveForm' => $active_form,
  801. ),
  802. ),
  803. 'type' => 'setting',
  804. );
  805. $register_form = drupal_get_form('user_register_form');
  806. $rendered_login_form = drupal_render($login_form);
  807. $rendered_register_form = drupal_render($register_form);
  808. $variables = array(
  809. 'login_form' => $rendered_login_form,
  810. 'register_form' => $rendered_register_form,
  811. 'active_form' => $active_form,
  812. );
  813. $output = theme('lt_unified_login_page', $variables);
  814. return $output;
  815. }
  816. /**
  817. * Returns an appropriate authentication form based on the configuration.
  818. *
  819. * @param $active_form
  820. * Which form to display, should be 'login' or 'register'.
  821. */
  822. function logintoboggan_get_authentication_form($active_form = 'login') {
  823. $output = '';
  824. if (variable_get('logintoboggan_unified_login', 0)) {
  825. $output = logintoboggan_unified_login_form($active_form);
  826. }
  827. elseif ($active_form == 'login') {
  828. $output = drupal_get_form('user_login');
  829. }
  830. elseif ($active_form == 'register') {
  831. $output = drupal_get_form('user_register_form');
  832. }
  833. return $output;
  834. }
  835. function logintoboggan_denied() {
  836. if ($GLOBALS['user']->uid == 0) {
  837. drupal_set_title(t('Access Denied / User log in'));
  838. // Output the user login form.
  839. $output = logintoboggan_get_authentication_form('login');
  840. drupal_set_page_content($output);
  841. // Return page attributes, hide blocks.
  842. $page = element_info('page');
  843. $page['#logintoboggan_denied'] = TRUE;
  844. }
  845. else {
  846. drupal_set_title(t('Access Denied'));
  847. $page = theme('lt_access_denied');
  848. }
  849. return $page;
  850. }
  851. /**
  852. * Modified version of user_validate_name
  853. * - validates user submitted passwords have a certain length and only contain letters, numbers or punctuation (graph character class in regex)
  854. */
  855. function logintoboggan_validate_pass($pass) {
  856. if (!strlen($pass)) return t('You must enter a password.');
  857. if (preg_match('/[\x{80}-\x{A0}'. // Non-printable ISO-8859-1 + NBSP
  858. '\x{A1}-\x{F7}'. // Latin punctuations
  859. '\x{AD}'. // Soft-hyphen
  860. '\x{2000}-\x{200F}'. // Various space characters
  861. '\x{2028}-\x{202F}'. // Bidirectional text overrides
  862. '\x{205F}-\x{206F}'. // Various text hinting characters
  863. '\x{FEFF}'. // Byte order mark
  864. '\x{FF01}-\x{FF60}'. // Full-width latin
  865. '\x{FFF9}-\x{FFFD}]/u', // Replacement characters
  866. $pass)) {
  867. return t('The password contains an illegal character.');
  868. }
  869. $min_pass_length = variable_get('logintoboggan_minimum_password_length', 0);
  870. 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));
  871. }
  872. /**
  873. * Modified version of $DRUPAL_AUTHENTICATED_RID
  874. * - gets the role id for the "validating" user role.
  875. */
  876. function logintoboggan_validating_id() {
  877. return variable_get('logintoboggan_pre_auth_role', DRUPAL_AUTHENTICATED_RID);
  878. }
  879. function _logintoboggan_process_validation($account) {
  880. // Test here for a valid pre-auth -- if the pre-auth is set to the auth user, we
  881. // handle things a bit differently.
  882. $validating_id = logintoboggan_validating_id();
  883. $pre_auth = !variable_get('user_email_verification', TRUE) && $validating_id != DRUPAL_AUTHENTICATED_RID;
  884. // Remove the pre-auth role from the user, unless they haven't been approved yet.
  885. if ($account->status) {
  886. if ($pre_auth) {
  887. db_delete('users_roles')
  888. ->condition('uid', $account->uid)
  889. ->condition('rid', $validating_id)
  890. ->execute();
  891. }
  892. }
  893. // Reload the user object freshly, since the cached value may have stale
  894. // roles, and to prepare for the possible user_save() below.
  895. $account = user_load($account->uid, TRUE);
  896. // Allow other modules to react to email validation by invoking the user update hook.
  897. // This should only be triggered if LT's custom validation is active.
  898. if (!variable_get('user_email_verification', TRUE)) {
  899. $edit = array();
  900. $account->logintoboggan_email_validated = TRUE;
  901. user_module_invoke('update', $edit, $account);
  902. }
  903. }
  904. /**
  905. * Actually log the user on
  906. *
  907. * @param object $account
  908. * The user object.
  909. * @param array $edit
  910. * An array of form values if a form has been submitted.
  911. * @param array $redirect
  912. * An array of key/value pairs describing a redirect location, in the form
  913. * that drupal_goto() will understand. Defaults to:
  914. * 'path' => 'user/'. $user->uid
  915. * 'query' => NULL
  916. * 'fragment' => NULL
  917. */
  918. function logintoboggan_process_login($account, &$edit, $redirect = array()){
  919. global $user;
  920. $user = user_load($account->uid);
  921. user_login_finalize($edit);
  922. // In the special case where a user is validating but they did not create their
  923. // own password, show a user message letting them know to change their password.
  924. if (variable_get('user_email_verification', TRUE)) {
  925. watchdog('user', 'User %name used one-time login link at time %timestamp.', array('%name' => $user->name, '%timestamp' => REQUEST_TIME));
  926. 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.'));
  927. }
  928. if (isset($redirect[0]) && $redirect[0] != '') {
  929. return $redirect;
  930. }
  931. return array(
  932. 'user/'. $user->uid,
  933. array(
  934. 'query' => array(),
  935. 'fragment' => '',
  936. ),
  937. );
  938. }
  939. function logintoboggan_eml_validate_url($account, $url_options){
  940. $timestamp = REQUEST_TIME;
  941. return url("user/validate/$account->uid/$timestamp/". logintoboggan_eml_rehash($account->pass, $timestamp, $account->mail, $account->uid), $url_options);
  942. }
  943. function logintoboggan_eml_rehash($password, $timestamp, $mail, $uid) {
  944. return user_pass_rehash($password, $timestamp, $mail, $uid);
  945. }
  946. /**
  947. * Implement hook_user_login().
  948. */
  949. function logintoboggan_user_login(&$edit, $account) {
  950. if (variable_get('logintoboggan_login_successful_message', 0)) {
  951. drupal_set_message(theme('lt_login_successful_message', array('account' => $account)));
  952. }
  953. }
  954. /**
  955. * Implement hook_user_load().
  956. */
  957. function logintoboggan_user_load($users) {
  958. foreach ($users as $account) {
  959. // If the user has the pre-auth role, unset the authenticated role
  960. _logintoboggan_user_roles_alter($account);
  961. }
  962. }
  963. /**
  964. * Implement hook_user_update().
  965. */
  966. function logintoboggan_user_update(&$edit, $account, $category) {
  967. // Only perform this check if an admin is editing the account.
  968. if (user_access('administer users') && isset($edit['roles'])) {
  969. // Check to see if roles present, and the pre-auth role was present when
  970. // the form was initially displayed.
  971. if (isset($edit['logintoboggan_pre_auth_check'])) {
  972. // If the pre-auth is set to the auth user, then no further checking is
  973. // necessary.
  974. $validating_id = logintoboggan_validating_id();
  975. $pre_auth = !variable_get('user_email_verification', TRUE) && $validating_id != DRUPAL_AUTHENTICATED_RID;
  976. if ($pre_auth) {
  977. // Check to see if an admin has manually removed the pre-auth role from
  978. // the user. If so, send the account activation email.
  979. if (!isset($edit['roles'][$validating_id]) || !$edit['roles'][$validating_id]) {
  980. // Mail the user, letting them know their account now has auth user perms.
  981. _user_mail_notify('status_activated', $account);
  982. }
  983. }
  984. }
  985. }
  986. }
  987. function _logintoboggan_protocol() {
  988. return ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http');
  989. }
  990. /**
  991. * Transforms a URL fragment into a redirect array understood by drupal_goto().
  992. *
  993. * @param $redirect
  994. * The redirect string.
  995. * @param $account
  996. * The user account object associated with the redirect.
  997. */
  998. function _logintoboggan_process_redirect($redirect, $account) {
  999. $variables = array('%uid' => $account->uid);
  1000. $redirect = parse_url(urldecode(strtr($redirect, $variables)));
  1001. // If there's a path set, override the destination parameter if necessary.
  1002. if ($redirect['path'] && variable_get('logintoboggan_override_destination_parameter', 1)) {
  1003. unset($_GET['destination']);
  1004. }
  1005. // Explicitly create query and fragment elements if not present already.
  1006. $query = isset($redirect['query']) ? $redirect['query'] : array();
  1007. $fragment = isset($redirect['fragment']) ? $redirect['fragment'] : '';
  1008. return array(
  1009. $redirect['path'],
  1010. array(
  1011. 'query' => $query,
  1012. 'fragment' => $fragment,
  1013. ),
  1014. );
  1015. }
  1016. /**
  1017. * Implement hook_mail_alter().
  1018. */
  1019. function logintoboggan_mail_alter(&$message) {
  1020. if ($message['id'] == 'user_register_pending_approval_admin') {
  1021. $reg_pass_set = !variable_get('user_email_verification', TRUE);
  1022. if ($reg_pass_set) {
  1023. $account = $message['params']['account'];
  1024. $url_options = array('absolute' => TRUE);
  1025. $language = $message['language'];
  1026. $langcode = isset($language) ? $language->language : NULL;
  1027. $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));
  1028. }
  1029. }
  1030. }
  1031. /**
  1032. *
  1033. * THEME FUNCTIONS!
  1034. *
  1035. * You may override and change any of these custom HTML output functions
  1036. * by copy/pasting them into your template.php file, at which point you can
  1037. * customize anything, provided you are using the default phptemplate engine.
  1038. *
  1039. * For more info on overriding theme functions, see http://drupal.org/node/55126
  1040. */
  1041. /**
  1042. * Theme the username title of the user login form
  1043. * and the user login block.
  1044. */
  1045. function theme_lt_username_title($variables) {
  1046. switch ($variables['form_id']) {
  1047. case 'user_login':
  1048. // Label text for the username field on the /user/login page.
  1049. return t('Username or e-mail address');
  1050. break;
  1051. case 'user_login_block':
  1052. // Label text for the username field when shown in a block.
  1053. return t('Username or e-mail');
  1054. break;
  1055. }
  1056. }
  1057. /**
  1058. * Theme the username description of the user login form
  1059. * and the user login block.
  1060. */
  1061. function theme_lt_username_description($variables) {
  1062. switch ($variables['form_id']) {
  1063. case 'user_login':
  1064. // The username field's description when shown on the /user/login page.
  1065. return t('You may login with either your assigned username or your e-mail address.');
  1066. break;
  1067. case 'user_login_block':
  1068. return '';
  1069. break;
  1070. }
  1071. }
  1072. /**
  1073. * Theme the password title of the user login form
  1074. * and the user login block.
  1075. */
  1076. function theme_lt_password_title($variables) {
  1077. // Label text for the password field.
  1078. return t('Password');
  1079. }
  1080. /**
  1081. * Theme the password description of the user login form
  1082. * and the user login block.
  1083. */
  1084. function theme_lt_password_description($variables) {
  1085. switch ($variables['form_id']) {
  1086. case 'user_login':
  1087. // The password field's description on the /user/login page.
  1088. return t('The password field is case sensitive.');
  1089. break;
  1090. case 'user_login_block':
  1091. // If showing the login form in a block, don't print any descriptive text.
  1092. return '';
  1093. break;
  1094. }
  1095. }
  1096. /**
  1097. * Theme the Access Denied message.
  1098. */
  1099. function theme_lt_access_denied($variables) {
  1100. return t('You are not authorized to access this page.');
  1101. }
  1102. /**
  1103. * Theme the loggedinblock that shows for logged-in users.
  1104. */
  1105. function theme_lt_loggedinblock($variables){
  1106. return theme('username', array('account' => $variables['account'])) .' | ' . l(t('Log out'), 'user/logout');
  1107. }
  1108. /**
  1109. * Custom theme function for the login/register link.
  1110. */
  1111. function theme_lt_login_link($variables) {
  1112. // Only display register text if registration is allowed.
  1113. if (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)) {
  1114. return t('Log in/Register');
  1115. }
  1116. else {
  1117. return t('Log in');
  1118. }
  1119. }
  1120. /**
  1121. * Theme the login successful message.
  1122. *
  1123. * @param $account
  1124. * A user object representing the user being logged in.
  1125. */
  1126. function theme_lt_login_successful_message($variables) {
  1127. return t('Log in successful for %name.', array('%name' => format_username($variables['account'])));
  1128. }
  1129. /**
  1130. * Theme function for unified login page.
  1131. *
  1132. * @ingroup themable
  1133. */
  1134. function theme_lt_unified_login_page($variables) {
  1135. $login_form = $variables['login_form'];
  1136. $register_form = $variables['register_form'];
  1137. $active_form = $variables['active_form'];
  1138. $output = '';
  1139. $output .= '<div class="toboggan-unified ' . $active_form . '">';
  1140. // Create the initial message and links that people can click on.
  1141. $output .= '<div id="login-message">' . t('You are not logged in.') . '</div>';
  1142. $output .= '<div id="login-links">';
  1143. $output .= l(t('I have an account'), 'user/login', array('attributes' => array('class' => array('login-link'), 'id' => 'login-link')));
  1144. $output .= ' ';
  1145. $output .= l(t('I want to create an account'), 'user/register', array('attributes' => array('class' => array('login-link'), 'id' => 'register-link')));
  1146. $output .= '</div>';
  1147. // Add the login and registration forms in.
  1148. $output .= '<div id="login-form">' . $login_form . '</div>';
  1149. $output .= '<div id="register-form">' . $register_form . '</div>';
  1150. $output .= '</div>';
  1151. return $output;
  1152. }