You are here

fancy_login.module in Fancy Login 6

File

fancy_login.module
View source
<?php

/**
 * Implementation of hook_init()
 * Loads the javascript on all pages except user/* when the user is not logged in
 */
function fancy_login_init() {
  if (user_is_anonymous()) {
    $path = drupal_get_path('module', 'fancy_login');
    $login_path = drupal_get_path_alias('user/login');
    $fl_settings = array(
      'screenFadeColor' => variable_get('screen_fade_color', 'white'),
      'screenFadeZIndex' => variable_get('screen_fade_z_index', '10'),
      'loginBoxHeight' => variable_get('login_box_height', 'auto'),
      'loginBoxWidth' => variable_get('login_box_width', '175px'),
      'loginBoxBackgroundColor' => variable_get('login_box_background_color', 'white'),
      'loginBoxTextColor' => variable_get('login_box_text_color', 'black'),
      'loginBoxBorderColor' => variable_get('login_box_border_color', 'black'),
      'loginBoxBorderWidth' => variable_get('login_box_border_width', '3px'),
      'loginBoxBorderStyle' => variable_get('login_box_border_style', 'solid'),
      'loginPath' => $login_path,
      'dimFadeSpeed' => (int) variable_get('fancy_login_dim_fade_speed', 500),
      'boxFadeSpeed' => (int) variable_get('fancy_login_box_fade_speed', 1000),
      'hideObjects' => variable_get('fancy_login_hide_objects', 0),
    );
    if (variable_get('fancy_login_no_redirect', 0)) {
      $fl_settings['destination'] = variable_get('clean_url', 0) ? '?' : '&';
      $fl_settings['destination'] .= 'destination=' . $_GET['q'];
    }
    if (variable_get('fancy_login_https', FALSE)) {
      global $base_root;
      $fl_settings['httpsRoot'] = preg_replace('/http:\\/\\//', 'https://', $base_root);
    }
    $fl_settings['requestDestination'] = variable_get('clean_url', 0) ? '?' : '&';
    $fl_settings['requestDestination'] .= 'destination=' . $_GET['q'];
    drupal_add_js(array(
      'fancyLogin' => $fl_settings,
    ), 'setting');
    drupal_add_js($path . '/scripts/fancy_login.js');
    drupal_add_css($path . '/css/fancy_login.css');
  }
}

/**
 * Implementation of hook_menu()
 */
function fancy_login_menu() {
  $menu['admin/settings/fancy_login'] = array(
    'title' => 'Fancy Login',
    'description' => 'Settings for Fancy Login Page',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'fancy_login_settings',
    ),
    'access arguments' => array(
      'Administer fancy login',
    ),
  );
  return $menu;
}

/**
 * Implmentation of hook_perm()
 */
function fancy_login_perm() {
  return array(
    'Administer fancy login',
  );
}

/**
 * Implementation of hook_footer()
 */
function fancy_login_footer() {
  if (user_is_anonymous()) {
    return '<div id="fancy_login_dim_screen"></div>' . fancy_login_get_form();
  }
}

/**
 * Function that is displayed in the popup to allow the user to log in
 */
function fancy_login_form() {
  global $base_url;
  $form_id = 'user_login';
  $form = array();
  $icon_position = FALSE;
  if (variable_get('fancy_login_https', 0) && ($icon_position = variable_get('fancy_login_icon_position', 0))) {
    $icon = theme('ssl_icon', $base_url);
  }
  if ($icon_position == 1) {
    $form['ssl_logo'] = array(
      '#value' => $icon,
    );
  }
  $form['name'] = array(
    '#title' => t('Username'),
    '#type' => 'textfield',
    '#maxlength' => USERNAME_MAX_LENGTH,
    '#size' => 15,
    '#required' => TRUE,
    '#attributes' => array(
      'tabindex' => '1',
    ),
  );
  $form['pass'] = array(
    '#title' => t('Password'),
    '#type' => 'password',
    '#required' => TRUE,
    '#size' => 15,
    '#attributes' => array(
      'tabindex' => '2',
    ),
  );
  if ($icon_position == 2) {
    $form['ssl_logo'] = array(
      '#value' => $icon,
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Log in'),
    '#weight' => 2,
    '#attributes' => array(
      'tabindex' => '3',
    ),
  );
  $form['#validate'] = user_login_default_validators();
  $form['#build_id'] = sprintf('form-%s', md5(uniqid(mt_rand(), TRUE)));
  $form_state = array();
  drupal_prepare_form($form_id, $form, $form_state);
  drupal_process_form($form_id, $form, $form_state);
  $out = new stdClass();
  if (variable_get('fancy_login_https', 0)) {
    $post_url = preg_replace('/^http:\\/\\//', 'https://', url('user/login', array(
      'absolute' => TRUE,
    )));
  }
  else {
    $post_url = url('user/login');
  }
  $out->form_start = sprintf('<form id="user-login" method="post" accept-charset="UTF-8" action="%s"><div>', $post_url);
  if ($icon_position == 1) {
    $out->form_start = str_replace('<form ', '<form class="ssl_icon_above" ', $out->form_start);
  }
  elseif ($icon_position == 2) {
    $out->form_start = str_replace('<form ', '<form class="ssl_icon_below" ', $out->form_start);
  }
  $out->form_end = "</div></form>";
  foreach (element_children($form) as $key) {
    if (!in_array($key, array(
      'submit',
      'form_build_id',
      'form_id',
    ))) {
      $out->{$key} = drupal_render($form[$key]);
    }
  }
  $items = array();
  if (variable_get('user_register', 1)) {
    $items[] = l(t('Create new account'), 'user/register', array(
      'attributes' => array(
        'title' => t('Create a new user account.'),
      ),
    ));
  }
  if (!module_exists('noreqnewpass') || !variable_get('noreqnewpass_disabled', FALSE)) {
    $items[] = l(t('Request new password'), 'user/password', array(
      'attributes' => array(
        'title' => t('Request new password via e-mail.'),
      ),
    ));
  }
  $form['links'] = array(
    '#value' => theme('item_list', $items),
  );
  $out->links = drupal_render($form['links']);
  $out->submit = drupal_render($form['form_id']) . drupal_render($form['form_build_id']) . drupal_render($form['submit']);
  return $out;
}

/**
 * Function that is called to create the popup that allows the user to log in
 */
function fancy_login_get_form() {
  global $base_url;
  $path = drupal_get_path('module', 'fancy_login');
  $image_path = $base_url . '/' . $path . '/images/ajax-loader.gif';
  $login_form = fancy_login_form();
  $output = '<div id="fancy_login_login_box">';
  $output .= '<div id="fancy_login_form_contents">';
  $output .= '<a href="#" id="fancy_login_close_button">X</a>';
  $output .= $login_form->form_start;
  foreach ($login_form as $key => $value) {
    if (!in_array($key, array(
      'form_start',
      'form_end',
      'links',
    ))) {
      $output .= $login_form->{$key};
    }
  }
  $output .= $login_form->form_end;
  $output .= $login_form->links;
  $output .= '</div>';
  $output .= '<div id="fancy_login_ajax_loader"><img src="' . $image_path . '" alt="' . t('Loading') . '" /></div>';
  $output .= '</div>';
  return $output;
}

/**
 * Implementation of hook_block()
 */
function fancy_login_block($op = 'list', $delta = 0, $edit = array()) {
  global $user;
  if ($op == "list") {
    $block = array();
    $block[0]['info'] = t('Fancy Login Link');
    return $block;
  }
  elseif ($op == 'view' && !$user->uid && !$user->uid && !(arg(0) == 'user' && !is_numeric(arg(1)))) {
    $destination = drupal_get_destination();
    $block['subject'] = t('Login');
    $block['content'] = l(t('Login'), 'user/login');
    return $block;
  }
}

/**
 * Callback function for admin/settings/fancy_login
 * Allows the user to set various CSS settings related to the display of the popup window
 */
function fancy_login_settings($form_state) {
  $form['display'] = array(
    '#type' => 'fieldset',
    '#title' => t('Display'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['display']['explanation'] = array(
    '#value' => '<p>' . t('All settings on this page must be valid CSS settings for the item that they will modify. For information on what types of values are valid, check the links included in the descriptions underneath the inputs.') . '</p>',
  );
  $form['display']['screen_fade_color'] = array(
    '#title' => t('Screen Fade Color'),
    '#type' => 'textfield',
    '#maxlength' => 7,
    '#size' => 8,
    '#default_value' => variable_get('screen_fade_color', 'white'),
    '#description' => t('This is the color that the screen fades to when the login box is activated. This should generally be black, white, or the same color as the background of your site. CSS property: !url.', array(
      '!url' => '<a href="http://www.devguru.com/technologies/css2/8047.asp">background-color</a>',
    )),
  );
  $form['display']['screen_fade_z_index'] = array(
    '#title' => t('Screen Fade z-index'),
    '#type' => 'textfield',
    '#maxlength' => 4,
    '#size' => 8,
    '#default_value' => variable_get('screen_fade_z_index', '10'),
    '#description' => t('This is the z-index of the faded screen. If you find elements on your layout are appearing over top of the faded out part of your screen, you can increase this number, but you should probably not touch it otherwise. CSS propery !url.', array(
      '!url' => '<a href="http://www.devguru.com/technologies/css2/8139.asp">z-index</a>',
    )),
  );
  $form['display']['login_box_height'] = array(
    '#title' => t('Login Box Height'),
    '#type' => 'textfield',
    '#maxlength' => 7,
    '#size' => 8,
    '#default_value' => variable_get('login_box_height', 'auto'),
    '#description' => t('This is the height of the login box. CSS property: !url.', array(
      '!url' => '<a href="http://www.devguru.com/technologies/css2/8096.asp">height</a>',
    )),
  );
  $form['display']['login_box_width'] = array(
    '#title' => t('Login Box Width'),
    '#type' => 'textfield',
    '#maxlength' => 7,
    '#size' => 8,
    '#default_value' => variable_get('login_box_width', '175px'),
    '#description' => t('This is the width of the login box. CSS property: !url.', array(
      '!url' => '<a href="http://www.devguru.com/technologies/css2/8137.asp">width</a>',
    )),
  );
  $form['display']['login_box_background_color'] = array(
    '#title' => t('Login Box Background Color'),
    '#type' => 'textfield',
    '#maxlength' => 7,
    '#size' => 8,
    '#default_value' => variable_get('login_box_background_color', 'white'),
    '#description' => t('This is the background color of the login box. CSS property: !url.', array(
      '!url' => '<a href="http://www.devguru.com/technologies/css2/8047.asp">background-color</a>',
    )),
  );
  $form['display']['login_box_text_color'] = array(
    '#title' => t('Login Box Text Color'),
    '#type' => 'textfield',
    '#maxlength' => 7,
    '#size' => 8,
    '#default_value' => variable_get('login_box_text_color', 'black'),
    '#description' => t('This is the color of the text inside the login box. CSS property: !url.', array(
      '!url' => '<a href="http://www.devguru.com/technologies/css2/8077.asp">color</a>',
    )),
  );
  $form['display']['login_box_border_color'] = array(
    '#title' => t('Login Box Border Color'),
    '#type' => 'textfield',
    '#maxlength' => 7,
    '#size' => 8,
    '#default_value' => variable_get('login_box_border_color', 'black'),
    '#description' => t('This is the color of the border around the login box. CSS property: !url.', array(
      '!url' => '<a href="http://www.devguru.com/technologies/css2/8057.asp">border-color</a>',
    )),
  );
  $form['display']['login_box_border_width'] = array(
    '#title' => t('Login Box Border Width'),
    '#type' => 'textfield',
    '#maxlength' => 7,
    '#size' => 8,
    '#default_value' => variable_get('login_box_border_width', '3px'),
    '#description' => t('This is the width of the border around the login box. CSS property: !url.', array(
      '!url' => '<a href="http://www.devguru.com/technologies/css2/8072.asp">border-width</a>',
    )),
  );
  $form['display']['login_box_border_style'] = array(
    '#title' => t('Login Box Border Style'),
    '#type' => 'textfield',
    '#maxlength' => 7,
    '#size' => 8,
    '#default_value' => variable_get('login_box_border_style', 'solid'),
    '#description' => t('This is the style (eg: solid, dotted) of the border around the login box. CSS property: !url.', array(
      '!url' => '<a href="http://www.devguru.com/technologies/css2/8067.asp">border-style</a>',
    )),
  );
  $form['display']['fancy_login_hide_objects'] = array(
    '#title' => t('Hide Objects'),
    '#type' => 'checkbox',
    '#default_value' => variable_get('fancy_login_hide_objects', 0),
    '#description' => t('If you are having issues where the fancy login box is being hidden behind videos or other flash objects, check this box to hide the objects while the login box is being shown'),
  );
  $form['display']['fancy_login_dim_fade_speed'] = array(
    '#title' => t('Background Fade Speed'),
    '#type' => 'textfield',
    '#default_value' => variable_get('fancy_login_dim_fade_speed', 500),
    '#maxlength' => 4,
    '#size' => 8,
    '#description' => t('This is the number of milliseconds it will take for the fullscreen background color to fade in/out. The higher the number, the slower the fade process will be. The lower the number, the faster the fade.'),
  );
  $form['display']['fancy_login_box_fade_speed'] = array(
    '#title' => t('Login Box Fade Speed'),
    '#type' => 'textfield',
    '#default_value' => variable_get('fancy_login_box_fade_speed', 1000),
    '#maxlength' => 4,
    '#size' => 8,
    '#description' => t('This is the number of milliseconds it will take for the login box to fade in/out. The higher the number, the slower the fade process will be. The lower the number, the faster the fade.'),
  );
  $form['fancy_login_no_redirect'] = array(
    '#title' => t('Keep User on Same Page'),
    '#type' => 'checkbox',
    '#description' => t('If this box is checked, the user will not be redirected upon login, and will stay on the page from which the login link was clicked. If this box is unchecked, the user will be redirected according to the Drupal system settings'),
    '#default_value' => variable_get('fancy_login_no_redirect', 0),
  );
  $form['ssl'] = array(
    '#type' => 'fieldset',
    '#title' => t('SSL (Secure Login)'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['ssl']['fancy_login_https'] = array(
    '#title' => t('Enable SSL (HTTPS)'),
    '#type' => 'checkbox',
    '#description' => t('If this box is checked, the form will be posted as encrypted data (HTTPS/SSL). Only use this if you have already set up an SSL certificate on your site, and have set up the login page as an encrypted page.'),
    '#default_value' => variable_get('fancy_login_https', 0),
  );
  $form['ssl']['fancy_login_icon_position'] = array(
    '#type' => 'radios',
    '#title' => t('Secure login icon position'),
    '#options' => array(
      t("Don't show icon"),
      t('Above the form'),
      t('Below the form'),
    ),
    '#default_value' => variable_get('fancy_login_icon_position', 0),
    '#description' => t("If SSL is turned on, turning this option on will display an icon indicating that the login is secure"),
  );
  return system_settings_form($form);
}

/**
 * Validation function for the settings page
 */
function fancy_login_settings_validate($form, &$form_state) {
  $test_values = array(
    'screen_fade_color' => 'Screen Fade Color',
    'screen_fade_z_index' => 'Screen Fade z-index',
    'login_box_height' => 'Login Box Height',
    'login_box_width' => 'Login Box Width',
    'login_box_background_color' => 'login_box_background_color',
    'login_box_text_color' => 'login_box_text_color',
    'login_box_border_color' => 'login_box_border_color',
    'login_box_border_width' => 'login_box_border_width',
    'login_box_border_style' => 'Login Box Border Style',
  );
  foreach ($test_values as $machine => $human) {
    if ($form_state['values'][$machine] == '') {
      form_set_error($machine, t('!field must contain a value.', array(
        '!field' => $human,
      )));
    }
  }
  if (!preg_match('/^[0-9]+$/', $form_state['values']['fancy_login_dim_fade_speed'])) {
    form_set_error('fancy_login_dim_fade_speed', t('Background Fade Speed must contain a numeric value'));
  }
  if (!preg_match('/^[0-9]+$/', $form_state['values']['fancy_login_box_fade_speed'])) {
    form_set_error('fancy_login_box_fade_speed', t('Login Box Fade Speed must contain a numeric value'));
  }
}
function fancy_login_theme() {
  $path = drupal_get_path('module', 'fancy_login');
  return array(
    'ssl_icon' => array(
      'arguments' => array(
        'base_url' => NULL,
      ),
      'path' => $path . '/templates',
      'template' => 'ssl-icon',
    ),
  );
}

Functions

Namesort descending Description
fancy_login_block Implementation of hook_block()
fancy_login_footer Implementation of hook_footer()
fancy_login_form Function that is displayed in the popup to allow the user to log in
fancy_login_get_form Function that is called to create the popup that allows the user to log in
fancy_login_init Implementation of hook_init() Loads the javascript on all pages except user/* when the user is not logged in
fancy_login_menu Implementation of hook_menu()
fancy_login_perm Implmentation of hook_perm()
fancy_login_settings Callback function for admin/settings/fancy_login Allows the user to set various CSS settings related to the display of the popup window
fancy_login_settings_validate Validation function for the settings page
fancy_login_theme