Skip to content

Instantly share code, notes, and snippets.

@sillygwailo
Created February 19, 2011 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sillygwailo/834889 to your computer and use it in GitHub Desktop.
Save sillygwailo/834889 to your computer and use it in GitHub Desktop.
name = Chroma-Hash
description = "Implements Mattt Thompson's Chroma-Hash jQuery plugin on password form elements"
core = 7.x
<?php
/**
* @file
* Modifies password form elements by adding Chroma-Hash bars
*
* Chroma-Hash is a jQuery plugin by Mattt Thompson that
* adds a visual indicator as to the accuracy of the password
* one types in. If the person who's typing in a password
* knows that the visual clue is similar to what they remember,
* then they can be confident they've typed in the right password.
*
* @see https://github.com/mattt/Chroma-Hash
*/
/**
* Implements hook_init().
*/
function chroma_hash_init() {
global $user;
$password_paths = (arg(0) == "user" && arg(2) == 'edit') || (arg(0) == 'admin' && arg(1) == 'people' && 'create');
if ((!$user->uid) || ($password_paths)) {
drupal_add_js(drupal_get_path('module', 'chroma_hash') . '/js/chroma-hash.js');
drupal_add_js(
array(
'chroma_hash' => array(
'bars' => variable_get('chroma_hash_bars', 1),
'salt' => variable_get('chroma_hash_salt', ''),
'minimum' => variable_get('chroma_hash_minimum', 3),
),
),
'setting'
);
drupal_add_js('jQuery(document).ready(function() {
jQuery("input:password").chromaHash(Drupal.settings.chroma_hash);
});',
array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)
);
}
}
/**
* Implements hook_menu().
*/
function chroma_hash_menu() {
$items = array();
$items['admin/config/user-interface/chroma-hash'] = array(
'title' => 'Chroma Hash',
'description' => 'Configure the number of bars.',
'page callback' => 'drupal_get_form',
'page arguments' => array('chroma_hash_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Configure the number of bars shown on login
*/
function chroma_hash_admin_settings() {
$form = array();
$form['chroma_hash_bars'] = array(
'#title' => t('Number of bars to show on password form.'),
'#type' => 'select',
'#default_value' => variable_get('chroma_hash_bars', 1),
'#options' => drupal_map_assoc(range(1, 4)), // the JavaScript library only accepts 1 to 4
'#validate' => array('chroma_hash_admin_settings_validate'),
);
$form['chroma_hash_salt'] = array(
'#title' => t('Salt'),
'#description' => t('Value to be appended when calculating hash function.'),
'#type' => 'textfield',
'#default_value' => variable_get('chroma_hash_salt', ''),
);
$form['chroma_hash_minimum'] = array(
'#title' => t('Minimum characters'),
'#descrption' => t('Minimum number of characters needed for grayscale bars to be displayed in color.'),
'#type' => 'select',
'#default_value' => variable_get('chroma_hash_minimum', 3),
'#options' => drupal_map_assoc(range(1, 10)),
);
return system_settings_form($form);
}
function chroma_hash_admin_settings_validate($form, &$form_state) {
if ($form['chroma_hash_bars']['#value'] < 1 || $form['chroma_hash_bars']['#value'] > 4) {
form_set_error($name = 'chroma_hash_bars', $message = 'Invalid form input. Values can only be between 1 and 4 inclusive.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment