/[drupal]/contributions/modules/ipAuthenticator/ipauth.module
ViewVC logotype

Diff of /contributions/modules/ipAuthenticator/ipauth.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.11, Tue Apr 21 13:11:26 2009 UTC revision 1.12, Fri May 15 19:55:00 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: ipauth.module,v 1.10 2009/04/21 12:31:44 jonfrancisskydiver Exp $  // $Id: ipauth.module,v 1.11 2009/04/21 13:11:26 jonfrancisskydiver Exp $
3  /**  /**
4   * @file   * @file
5   * Module code for an IP based authenticator   * Module code for an IP based authenticator
6   * @author C/S Group - Jonathan T. Francis   * @author C/S Group - Jonathan T. Francis
7   */   */
8    
 /*******  
  * HOOKS  
  */  
   
9  /**  /**
10   * Detect which uid to apply based on the viewer's IP address   * Detect which uid to apply based on the viewer's IP address
11   */   */
# Line 19  function ipauth_boot() { Line 15  function ipauth_boot() {
15    // Also check ip auth users to see if they have changed the network so that    // Also check ip auth users to see if they have changed the network so that
16    // a new uid has to be assigned.    // a new uid has to be assigned.
17    if (!$user->uid || in_array($user->uid, ipauth_get_uids("enabled"))) {    if (!$user->uid || in_array($user->uid, ipauth_get_uids("enabled"))) {
18    
19      // Get the first result from the database, we can only assign one single uid.      // Get the first result from the database, we can only assign one single uid.
20      $result = ipauth_get_ip_authenticators($_SERVER['REMOTE_ADDR'], "uid");      $result = ipauth_get_ip_authenticators($_SERVER['REMOTE_ADDR'], "uid");
21    
22      if ($row = db_fetch_array($result)) {      if ($row = db_fetch_array($result)) {
23        // we found an entry in the table, so load the user        // we found an entry in the table, so load the user
24        drupal_load("module", "user");        drupal_load("module", "user");
# Line 35  function ipauth_boot() { Line 31  function ipauth_boot() {
31      // code if the user has changed      // code if the user has changed
32      if (isset($account) && $account && $account->uid != $user->uid) {      if (isset($account) && $account && $account->uid != $user->uid) {
33        $user = $account;        $user = $account;
34    
35        // Regenerate the session ID to prevent against session fixation attacks.        // Regenerate the session ID to prevent against session fixation attacks.
36        sess_regenerate();        sess_regenerate();
37    
38        if (variable_get('cache', CACHE_DISABLED) != CACHE_DISABLED && !isset($_GET['ipauth_no_cache'])) {        if (variable_get('cache', CACHE_DISABLED) != CACHE_DISABLED && !isset($_GET['ipauth_no_cache'])) {
39          // Reload the page, the query string ensures that there will be a page          // Reload the page, the query string ensures that there will be a page
40          // cache miss and thus a fresh generated page is served.          // cache miss and thus a fresh generated page is served.
41    
42          $url = url($_GET["q"], 'ipauth_no_cache='.md5(time()), NULL, TRUE);          $url = url($_GET["q"], 'ipauth_no_cache='.md5(time()), NULL, TRUE);
43    
44          // Remove newlines from the URL to avoid header injection attacks.          // Remove newlines from the URL to avoid header injection attacks.
45          $url = str_replace(array("\n", "\r"), '', $url);          $url = str_replace(array("\n", "\r"), '', $url);
46    
47          // Before the redirect, allow modules to react to the end of the page request.          // Before the redirect, allow modules to react to the end of the page request.
48          module_invoke_all('exit', $url);          module_invoke_all('exit', $url);
49    
50          // Even though session_write_close() is registered as a shutdown function, we          // Even though session_write_close() is registered as a shutdown function, we
51          // need all session data written to the database before redirecting.          // need all session data written to the database before redirecting.
52          session_write_close();          session_write_close();
53    
54          header('Location: '. $url, TRUE, 302);          header('Location: '. $url, TRUE, 302);
55          exit();          exit();
56        }        }
57      }      }
# Line 78  function ipauth_boot() { Line 74  function ipauth_boot() {
74      if ($_GET['q'] == 'user') {      if ($_GET['q'] == 'user') {
75        $_GET['q'] = 'user/login';        $_GET['q'] = 'user/login';
76      }      }
77    
78      // Logout if password reset page is requested      // Logout if password reset page is requested
79      if (substr($_GET['q'], 0, 11) == 'user/reset/') {      if (substr($_GET['q'], 0, 11) == 'user/reset/') {
80        $user = drupal_anonymous_user();        $user = drupal_anonymous_user();
# Line 112  function ipauth_perm() { Line 108  function ipauth_perm() {
108    return array(    return array(
109    'access ip_authenticator content',    'access ip_authenticator content',
110    'administer ip_authenticator');    'administer ip_authenticator');
111  } // end function ipauth_perm  } // end function ipauth_perm
112    
113  /**  /**
114   * insert the administration menu   * insert the administration menu
# Line 135  function ipauth_menu() { Line 131  function ipauth_menu() {
131      'title' => t('IP Authenticator -- Modify'),      'title' => t('IP Authenticator -- Modify'),
132      'page callback' => 'drupal_get_form',      'page callback' => 'drupal_get_form',
133      'page arguments' => array('ipauth_admin_edit'),      'page arguments' => array('ipauth_admin_edit'),
134      'access callback' => 'user_access',      'access callback' => 'user_access',
135      'access arguments' => Array('administer ip_authenticator'),      'access arguments' => Array('administer ip_authenticator'),
136      'file' => 'ipauth.admin.inc',      'file' => 'ipauth.admin.inc',
137      'type' => MENU_CALLBACK      'type' => MENU_CALLBACK
# Line 144  function ipauth_menu() { Line 140  function ipauth_menu() {
140      'title' => t('IP Authenticator -- Delete'),      'title' => t('IP Authenticator -- Delete'),
141      'page callback' => 'drupal_get_form',      'page callback' => 'drupal_get_form',
142      'page arguments' => array('ipauth_admin_delete'),      'page arguments' => array('ipauth_admin_delete'),
143      'access callback' => 'user_access',      'access callback' => 'user_access',
144      'access arguments' => Array('administer ip_authenticator'),      'access arguments' => Array('administer ip_authenticator'),
145      'file' => 'ipauth.admin.inc',      'file' => 'ipauth.admin.inc',
146      'type' => MENU_CALLBACK      'type' => MENU_CALLBACK
# Line 158  function ipauth_menu() { Line 154  function ipauth_menu() {
154      'file' => 'ipauth.admin.inc',      'file' => 'ipauth.admin.inc',
155      'type' => MENU_CALLBACK      'type' => MENU_CALLBACK
156    );    );
157      $items['admin/user/ip_authenticator/data/get_auths/%'] = array(
158        'title' => t('IP Authenticator -- ajax'),
159        'page callback' => 'ipauth_ajax',
160        'page arguments' => array(5),
161        'access callback' => 'user_access',
162        'access arguments' => Array('administer ip_authenticator'),
163        'file' => 'ipauth.admin.inc',
164        'type' => MENU_DYNAMIC_ITEM,
165      );
166    $items['admin/user/ip_authenticator/data/export'] = array(    $items['admin/user/ip_authenticator/data/export'] = array(
167      'title' => t('IP Authenticator -- Import/Export'),      'title' => t('IP Authenticator -- Import/Export'),
168      'page callback' => 'ipauth_export',      'page callback' => 'ipauth_export',
# Line 187  function ipauth_menu() { Line 192  function ipauth_menu() {
192      'type' => MENU_CALLBACK,      'type' => MENU_CALLBACK,
193      'file' => '../../../../modules/user/user.pages.inc',      'file' => '../../../../modules/user/user.pages.inc',
194    );    );
195    
196    $items['user/login'] = array(    $items['user/login'] = array(
197      'title' => 'Log in',      'title' => 'Log in',
198      'access callback' => 'is_ipauth_user',      'access callback' => 'is_ipauth_user',
# Line 201  function ipauth_menu() { Line 206  function ipauth_menu() {
206      'page callback' => 'drupal_get_form',      'page callback' => 'drupal_get_form',
207      'page arguments' => array('ipauth_user_register'),      'page arguments' => array('ipauth_user_register'),
208      'access callback' => 'is_ipauth_user',      'access callback' => 'is_ipauth_user',
209      'access arguments' => array(FALSE, variable_get('user_register', 1), TRUE),      'access arguments' => array(FALSE, variable_get('user_register', 1), TRUE),
210      'type' => MENU_LOCAL_TASK,      'type' => MENU_LOCAL_TASK,
211      'file' => '../../../../modules/user/user.pages.inc',      'file' => '../../../../modules/user/user.pages.inc',
212    );    );
# Line 215  function ipauth_menu() { Line 220  function ipauth_menu() {
220      'file' => '../../../../modules/user/user.pages.inc',      'file' => '../../../../modules/user/user.pages.inc',
221    );    );
222    
223    // Your personal page    // Your personal page
224    $items['user/%user_uid_optional'] = array(    $items['user/%user_uid_optional'] = array(
225      'title' => 'My account',      'title' => 'My account',
226      'title callback' => 'user_page_title',      'title callback' => 'user_page_title',
# Line 232  function ipauth_menu() { Line 237  function ipauth_menu() {
237      'type' => MENU_DEFAULT_LOCAL_TASK,      'type' => MENU_DEFAULT_LOCAL_TASK,
238      'weight' => -10,      'weight' => -10,
239    );    );
240    
241    $items['logout'] = array(    $items['logout'] = array(
242     'title' => 'Log out',     'title' => 'Log out',
243     'page callback' => 'user_logout',     'page callback' => 'user_logout',
# Line 251  function ipauth_menu() { Line 256  function ipauth_menu() {
256   */   */
257  function _ipauth_path_allowed() {  function _ipauth_path_allowed() {
258    $path = $_GET['q'];    $path = $_GET['q'];
259    $args = explode('/', $path);    $args = explode('/', $path);
260    // Deny access to all "/user" pages of the currently logged in ipauth user    // Deny access to all "/user" pages of the currently logged in ipauth user
261    // except if he is an admin.    // except if he is an admin.
262    return !( ($args[0] == 'user' && $args[1] == $GLOBALS['user']->uid) && !user_access('administer users') );    return !( ($args[0] == 'user' && $args[1] == $GLOBALS['user']->uid) && !user_access('administer users') );
# Line 342  function ipauth_import_export_submit($fo Line 347  function ipauth_import_export_submit($fo
347      foreach ($CSV_file AS $CSV_file_line) {      foreach ($CSV_file AS $CSV_file_line) {
348        list($row["uid"], $row["ip1"], $row["ip2"], $row["description"], $row["enabled"], $row["created"]) = split(",", $CSV_file_line);        list($row["uid"], $row["ip1"], $row["ip2"], $row["description"], $row["enabled"], $row["created"]) = split(",", $CSV_file_line);
349        array_walk($row, '_ipauth_strip_quotes');        array_walk($row, '_ipauth_strip_quotes');
350        $row["created"] = preg_replace("/\n|\"/","",$row["created"]);        $row["created"] = preg_replace("/\n|\"|\r/","",$row["created"]);
351          if ($row["created"] == "") {
352            $row["created"] = time();
353          }
354        if (_ipauth_check_import($row)) {        if (_ipauth_check_import($row)) {
355          $sql = "INSERT INTO {ip_authenticator} (uid, ip1, ip2, description, enabled, created) VALUES ('%s', '%s','%s','%s', '%d', FROM_UNIXTIME(%s))";          $sql = "INSERT INTO {ip_authenticator} (uid, ip1, ip2, description, enabled, created) VALUES ('%s', '%s','%s','%s', '%d', FROM_UNIXTIME(%s))";
356          db_query($sql, $row["uid"], sprintf("%u", ip2long(trim($row['ip1']))), sprintf("%u", ip2long(trim($row['ip2']))), $row["description"], $row["enabled"], $row["created"]);          db_query($sql, $row["uid"], sprintf("%u", ip2long(trim($row['ip1']))), sprintf("%u", ip2long(trim($row['ip2']))), $row["description"], $row["enabled"], $row["created"]);
# Line 369  function ipauth_admin_settings($form_sta Line 377  function ipauth_admin_settings($form_sta
377      '#description' => "<b>". t("Instructions:") ."</b><br/> &nbsp;".      '#description' => "<b>". t("Instructions:") ."</b><br/> &nbsp;".
378                        t(" - Click on an ip range to modify the authenticator assignment.") ."<br/> &nbsp; ".                        t(" - Click on an ip range to modify the authenticator assignment.") ."<br/> &nbsp; ".
379                        t(" - Click on a user name to edit the account.") ."<br/> &nbsp; ".                        t(" - Click on a user name to edit the account.") ."<br/> &nbsp; ".
380                        t(" - The title fields are sortable."),                        t(" - The title fields are sortable."),
381      '#collapsible' => TRUE,      '#collapsible' => TRUE,
382      '#collapsed' => FALSE,      '#collapsed' => FALSE,
383      '#weight' => 0      '#weight' => 0
# Line 378  function ipauth_admin_settings($form_sta Line 386  function ipauth_admin_settings($form_sta
386    $form['ip_list']['import-export'] = array(    $form['ip_list']['import-export'] = array(
387      '#value' => l("Export", "admin/user/ip_authenticator/data/export", array('attributes'=>array('class'=>'button'))),      '#value' => l("Export", "admin/user/ip_authenticator/data/export", array('attributes'=>array('class'=>'button'))),
388      '#prefix' => '<div id="import-export">',      '#prefix' => '<div id="import-export">',
389      '#suffix' => '</div>',      '#suffix' => '',
390      '#weight' => 0,      '#weight' => 0,
391    );    );
392    
393    $non_blocked_users = array();          $form['ip_list']['user_filter'] = array(
394                    '#type'                                 => 'textfield',
395                    '#title'                                => 'User Filter',
396                    '#description'  => t('Username filtering'),
397                    '#id'                                           => 'user_filter',
398                    '#suffix'                               => '</div>',
399                    '#size'                                 => 15,
400            );
401    
402    
403    
404            ipauth_get_ipauths_form($form);
405    
406    
407    
408    
409      $form['ips'] = array(
410        '#type' => 'fieldset',
411        '#title' => t('Add an IP Authenticator') ."<br/>",
412        '#description' =>
413                          /*"<div class='ipauth_ip'><b>". t("Your IP: @s", array('@s' => $_SERVER['REMOTE_ADDR'])) ."</b></div>".
414                          '<br /><br/>'. */
415                          t('Please note that users authenticated via IP authenticator do not get permissions of the <em>authenticated user</em> role.'),
416        '#collapsible' => FALSE,
417        '#collapsed' => FALSE,
418        '#weight' => -1
419      );
420      $form['ips']['checkbox_enabled'] = array(
421        '#prefix' => '<div class="floatLeft checkbox form-item"><label>'. t('Active') .'</label>',
422        '#suffix' => '</div>',
423        '#type' => 'checkbox',
424        '#description' => 'Enable/Disable',
425        '#id' => 'enable_disable_checkbox',
426        '#default_value' => '0',
427      );
428      $form['ips']['ip1'] = array(
429        '#prefix' => '<div class="floatLeft">',
430        '#type' => 'textfield',
431        '#title' => 'IP 1',
432        '#size' => '15',
433        '#description' => "<div class='ipauth_ip'>". t('Your IP: ') . '<a href="javascript:void(0);" class="ip">'. $_SERVER["REMOTE_ADDR"] ."</a></div>",
434        /* '#required' => TRUE, */
435        '#suffix' => '</div><div class="floatLeft" id="formSeperator">&nbsp;&nbsp; - &nbsp;&nbsp;</div>'
436      );
437    
438      $form['ips']['ip2'] = array(
439        '#prefix' => '<div class="floatLeft">',
440        '#type' => 'textfield',
441        '#title' => 'IP 2',
442        '#description' => t('Leave blank for<br/>individual IPs'),
443        '#size' => '15',
444        '#suffix' => '</div><div class="floatLeft" id="formSeperator">&nbsp; &nbsp; &nbsp; &nbsp;</div>',
445      );
446    
447      $form['ips']['username'] = array(
448        '#prefix' => '<div class="floatLeft">',
449        '#type' => 'textfield',
450        '#maxlength' => 60,
451        '#title' => t('Assign to User'),
452        '#autocomplete_path' => 'user/autocomplete',
453        '#description' => t("Select a user to assign the IPs to"),
454        /* '#required' => TRUE,*/
455        '#suffix' => '</div>',
456      );
457      $form['ips']['ipauth_desc'] = array(
458        '#prefix' => '<br style="clear:both" />',
459        '#type' => 'textfield',
460        '#title' => t('Description of Authenticator'),
461        '#description' => t("Enter a description to better identify this authenticator"),
462      );
463      $form['ips']['submit1'] = array(
464        '#type' => 'submit',
465        '#value' => 'Add IP Authenticator',
466        '#weight' => 1,
467      );
468      $form['ips']['import-export1'] = array(
469        '#value' => l("Import", "admin/user/ip_authenticator/data", array('attributes'=>array('class'=>'button'))),
470        '#prefix' => '<div id="import-export1">',
471        '#suffix' => '</div>',
472        '#weight' => 1,
473      );
474      $form['#validate'][] = 'ipauth_admin_settings_validate';
475      $form['#submit'][] = 'ipauth_admin_settings_submit';
476    
477      $form['submit'] = array(
478        '#type' => 'submit',
479        '#value' => 'Save Configuration',
480        '#weight' => 1,
481      );
482    
483      return $form; //system_settings_form($form);
484    } // end function ipauth_admin_settings
485    
486    function ipauth_ajax($user_filter = "") {
487            $form = array();
488            echo drupal_get_form("ipauth_ajax_authenticators", $user_filter);
489            exit;
490    }
491    
492    function ipauth_ajax_authenticators($state, $name) {
493            ipauth_get_ipauths_form($form, $name);
494    
495            return $form;
496    }
497    
498    function ipauth_get_ipauths_form(&$form, $name="") {
499    
500            $non_blocked_users = array();
501    
502    // Figure out which field to sort by.    // Figure out which field to sort by.
503    if (isset($_GET["sort_field"]) && preg_match("!^[a-zA-Z0-9_]+$!", $_GET["sort_field"])) {    if (isset($_GET["sort_field"]) && preg_match("!^[a-zA-Z0-9_]+$!", $_GET["sort_field"])) {
504      $sort_field = $_GET["sort_field"];      $sort_field = $_GET["sort_field"];
505    } else {    } else {
506      $sort_field = "created";      $sort_field = "created";
507    }    }
508    
509    // How are we ordering the fields, Asc or Desc?    // How are we ordering the fields, Asc or Desc?
510    if (isset($_GET["order"]) && preg_match("!^asc|desc$!", $_GET["order"]))    if (isset($_GET["order"]) && preg_match("!^asc|desc$!", $_GET["order"]))
511      $order = $_GET["order"];      $order = $_GET["order"];
512    else    else
513      $order= "asc";      $order= "asc";
514    
515    // Used to toggle between asc and desc.    // Used to toggle between asc and desc.
516    $order_toggle["asc"] = "desc";    $order_toggle["asc"] = "desc";
517    $order_toggle["desc"] = "asc";    $order_toggle["desc"] = "asc";
518    
519    // Get all of the IP Authenticators in the specified order.    // Get all of the IP Authenticators in the specified order.
520    $result = ipauth_get_ip_authenticators(NULL, "id, uid, ip1, ip2, description, enabled, DATE_FORMAT(created,'%m/%%d/%Y %l:%%i%p') as created", $sort_field, $order);          if ($name == "" || $name=='all')
521                    $result = ipauth_get_ip_authenticators(NULL, "id, uid, ip1, ip2, description, enabled, DATE_FORMAT(created,\"%%m/%%d/%%Y %%l:%%i%%p\") as created", $sort_field, $order);
522            else
523                    $result = ipauth_get_auths_by_name($name, "ipauth.id, ipauth.uid, ipauth.ip1, ipauth.ip2, ipauth.description, ipauth.enabled, DATE_FORMAT(ipauth.created,\"%%m/%%d/%%Y %%l:%%i%%p\") as created", "ipauth.".$sort_field, $order);
524    
525    //The theming function used to theme the table with the FAPI element is inspired from:          //The theming function used to theme the table with the FAPI element is inspired from:
526    //  http://drupal.org/project/formtable.    //  http://drupal.org/project/formtable.
527    //That project currently only supports a Drupal 5 Module; That module has been ported    //That project currently only supports a Drupal 5 Module; That module has been ported
528    //and incorporated into this ipauth module.    //and incorporated into this ipauth module.
529    $form['ip_list']['ipauth_table'] = array(    $form['ip_list']['ipauth_table'] = array(
530        '#type' => 'formtable',        '#type' => 'formtable',
531        '#header' => array(        '#header' => array(
532                            l(t('IP Range'), $_GET["q"], array('query' => array('sort_field' => 'ip1', 'order' => $order_toggle[$order]))). ipauth_arrow($sort_field, 'ip1', $order),                            l(t('IP Range'), $_GET["q"], array('query' => array('sort_field' => 'ip1', 'order' => $order_toggle[$order]))). ipauth_arrow($sort_field, 'ip1', $order),
533                            l(t('Active'), $_GET["q"], array('query' => array('sort_field' => 'enabled', 'order' => $order_toggle[$order]))) . ipauth_arrow($sort_field, 'enabled', $order),                            l(t('Active'), $_GET["q"], array('query' => array('sort_field' => 'enabled', 'order' => $order_toggle[$order]))) . ipauth_arrow($sort_field, 'enabled', $order),
534                            l(t('User'), $_GET["q"], array('query' => array('sort_field' => 'uid', 'order' => $order_toggle[$order]))). ipauth_arrow($sort_field, 'uid', $order),                            l(t('User'), $_GET["q"], array('query' => array('sort_field' => 'uid', 'order' => $order_toggle[$order]))). ipauth_arrow($sort_field, 'uid', $order),
# Line 418  function ipauth_admin_settings($form_sta Line 536  function ipauth_admin_settings($form_sta
536                            l(t('Last Updated'), $_GET["q"], array('query' => array('sort_field' => 'created', 'order' => $order_toggle[$order]))). ipauth_arrow($sort_field, 'created', $order),)                            l(t('Last Updated'), $_GET["q"], array('query' => array('sort_field' => 'created', 'order' => $order_toggle[$order]))). ipauth_arrow($sort_field, 'created', $order),)
537    );    );
538    
539    
540    $row = db_fetch_object($result); // Get the next IP Authenticator    $row = db_fetch_object($result); // Get the next IP Authenticator
541    $evenodd_counter=0;  // used for the table stripping (even and odd CSS classes).    $evenodd_counter=0;  // used for the table stripping (even and odd CSS classes).
542    
543    if ($row) {    if ($row) {
544      do {      do {
545        $evenodd_counter++;        $evenodd_counter++;
# Line 437  function ipauth_admin_settings($form_sta Line 555  function ipauth_admin_settings($form_sta
555        else {        else {
556          $user_title = t('The user account does not exist. Please delete this rule.');          $user_title = t('The user account does not exist. Please delete this rule.');
557        }        }
558    
559        if (!empty($account) && !user_is_blocked($account->name) && !in_array($account->name, $non_blocked_users)) {        if (!empty($account) && !user_is_blocked($account->name) && !in_array($account->name, $non_blocked_users)) {
560          $non_blocked_users[] = array('name' => $account->name, 'uid' => $account->uid);          $non_blocked_users[] = array('name' => $account->name, 'uid' => $account->uid);
561        }        }
562    
563        $form['ip_list']['ipauth_table']['row_' .$row->id] = array(        $form['ip_list']['ipauth_table']['row_' .$row->id] = array(
564          '#type' => 'formrow',          '#type' => 'formrow',
565          '#attributes' => array('class' => ($evenodd_counter%2==1) ? "even" : "odd"),          '#attributes' => array('class' => ($evenodd_counter%2==1) ? "even" : "odd"),
566        );        );
567    
568        $form['ip_list']['ipauth_table']['row_' .$row->id]['iprange'] = array(        $form['ip_list']['ipauth_table']['row_' .$row->id]['iprange'] = array(
569            '#value' =>  l($ip_title, "admin/user/ip_authenticator/edit/". $row->id),            '#value' =>  l($ip_title, "admin/user/ip_authenticator/edit/". $row->id),
570            '#prefix' => '<td>',            '#prefix' => '<td>',
# Line 462  function ipauth_admin_settings($form_sta Line 580  function ipauth_admin_settings($form_sta
580            '#value' =>  $user_title,            '#value' =>  $user_title,
581            '#prefix' => '<td>',            '#prefix' => '<td>',
582            '#suffix' => '</td>'            '#suffix' => '</td>'
583        );        );
584        $form['ip_list']['ipauth_table']['row_' .$row->id]['description'] = array(        $form['ip_list']['ipauth_table']['row_' .$row->id]['description'] = array(
585            '#value' =>  ($row->description=="") ? "&nbsp;" : $row->description,            '#value' =>  ($row->description=="") ? "&nbsp;" : $row->description,
586            '#prefix' => '<td>',            '#prefix' => '<td>',
# Line 472  function ipauth_admin_settings($form_sta Line 590  function ipauth_admin_settings($form_sta
590            '#value' =>  $row->created,            '#value' =>  $row->created,
591            '#prefix' => '<td>',            '#prefix' => '<td>',
592            '#suffix' => '</td>'            '#suffix' => '</td>'
593        );        );
594      } while($row = db_fetch_object($result));// END while ($row = db_fetch_object($result))      } while($row = db_fetch_object($result));// END while ($row = db_fetch_object($result))
595    } else {    } else {
596      $form['ip_list']['no_authenticators'] = array(      $form['ip_list']['no_authenticators'] = array(
597        '#value' => t("There are no IP Authenticators to list."),        '#value' => t("There are no IP Authenticators to list."),
# Line 491  function ipauth_admin_settings($form_sta Line 609  function ipauth_admin_settings($form_sta
609      drupal_set_message($error_text, 'error');      drupal_set_message($error_text, 'error');
610    }    }
611    
   $form['ips'] = array(  
     '#type' => 'fieldset',  
     '#title' => t('Add an IP Authenticator') ."<br/>",  
     '#description' =>  
                       /*"<div class='ipauth_ip'><b>". t("Your IP: @s", array('@s' => $_SERVER['REMOTE_ADDR'])) ."</b></div>".  
                       '<br /><br/>'. */  
                       t('Please note that users authenticated via IP authenticator do not get permissions of the <em>authenticated user</em> role.'),  
     '#collapsible' => FALSE,  
     '#collapsed' => FALSE,  
     '#weight' => -1  
   );  
   $form['ips']['checkbox_enabled'] = array(  
     '#prefix' => '<div class="floatLeft checkbox form-item"><label>'. t('Active') .'</label>',  
     '#suffix' => '</div>',  
     '#type' => 'checkbox',  
     '#description' => 'Enable/Disable',  
     '#id' => 'enable_disable_checkbox',  
     '#default_value' => '0',  
   );  
   $form['ips']['ip1'] = array(  
     '#prefix' => '<div class="floatLeft">',  
     '#type' => 'textfield',  
     '#title' => 'IP 1',  
     '#size' => '15',  
     '#description' => "<div class='ipauth_ip'>". t('Your IP: ') . '<a href="javascript:void(0);" class="ip">'. $_SERVER["REMOTE_ADDR"] ."</a></div>",  
     /* '#required' => TRUE, */  
     '#suffix' => '</div><div class="floatLeft" id="formSeperator">&nbsp;&nbsp; - &nbsp;&nbsp;</div>'  
   );  
   
   $form['ips']['ip2'] = array(  
     '#prefix' => '<div class="floatLeft">',  
     '#type' => 'textfield',  
     '#title' => 'IP 2',  
     '#description' => t('Leave blank for<br/>individual IPs'),  
     '#size' => '15',  
     '#suffix' => '</div><div class="floatLeft" id="formSeperator">&nbsp; &nbsp; &nbsp; &nbsp;</div>',  
   );  
   
   $form['ips']['username'] = array(  
     '#prefix' => '<div class="floatLeft">',  
     '#type' => 'textfield',  
     '#maxlength' => 60,  
     '#title' => t('Assign to User'),  
     '#autocomplete_path' => 'user/autocomplete',  
     '#description' => t("Select a user to assign the IPs to"),  
     /* '#required' => TRUE,*/  
     '#suffix' => '</div>',  
   );  
   $form['ips']['ipauth_desc'] = array(  
     '#prefix' => '<br style="clear:both" />',  
     '#type' => 'textfield',  
     '#title' => t('Description of Authenticator'),  
     '#description' => t("Enter a description to better identify this authenticator"),  
   );  
   $form['ips']['submit1'] = array(  
     '#type' => 'submit',  
     '#value' => 'Add IP Authenticator',  
     '#weight' => 1,  
   );  
   $form['ips']['import-export1'] = array(  
     '#value' => l("Import", "admin/user/ip_authenticator/data", array('attributes'=>array('class'=>'button'))),  
     '#prefix' => '<div id="import-export1">',  
     '#suffix' => '</div>',  
     '#weight' => 1,  
   );  
   $form['#validate'][] = 'ipauth_admin_settings_validate';  
   $form['#submit'][] = 'ipauth_admin_settings_submit';  
612    
613    $form['submit'] = array(  }
     '#type' => 'submit',  
     '#value' => 'Save Configuration',  
     '#weight' => 1,  
   );  
614    
   return $form; //system_settings_form($form);  
 } // end function ipauth_admin_settings  
615    
616  /**  /**
617   * Validate our admin settings   * Validate our admin settings
# Line 582  function ipauth_admin_settings_validate( Line 627  function ipauth_admin_settings_validate(
627    if ((trim($form_state['values']['ip2']) != "") && (!preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\$/x", trim($form_state['values']['ip2'])))) {    if ((trim($form_state['values']['ip2']) != "") && (!preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\$/x", trim($form_state['values']['ip2'])))) {
628      form_set_error('ip2', t("You must enter a valid IP address in the second field, or leave the second field blank."));      form_set_error('ip2', t("You must enter a valid IP address in the second field, or leave the second field blank."));
629    }    }
630    
631    // if IP1 contains a value and the user name is blank or the user is not blocked, then    // if IP1 contains a value and the user name is blank or the user is not blocked, then
632    if ( trim($form_state['values']['ip1']) != "" && (trim($form_state['values']['username']) == "" || !user_is_blocked($form_state['values']['username']))) {    if ( trim($form_state['values']['ip1']) != "" && (trim($form_state['values']['username']) == "" || !user_is_blocked($form_state['values']['username']))) {
633    
634      if (trim($form_state['values']['username']) != "") {      if (trim($form_state['values']['username']) != "") {
635        $account = user_load(array('name' => $form_state['values']['username']));        $account = user_load(array('name' => $form_state['values']['username']));
# Line 599  function ipauth_admin_settings_validate( Line 644  function ipauth_admin_settings_validate(
644        form_set_error('username', t('The username must contain a value.'));        form_set_error('username', t('The username must contain a value.'));
645      }      }
646    }    }
647    
648    $checkbox_keys = array_keys($form_state['values']);    $checkbox_keys = array_keys($form_state['values']);
649    foreach ($checkbox_keys as $key => $value) {    foreach ($checkbox_keys as $key => $value) {
650      if (strpos($key, 'checkbox_') !== FALSE && ($value != 0 && $value != 1 ) ) {      if (strpos($key, 'checkbox_') !== FALSE && ($value != 0 && $value != 1 ) ) {
651        form_set_error($key, t('Checkbox value must be either checked or not checked.'));        form_set_error($key, t('Checkbox value must be either checked or not checked.'));
652      }      }
653    }    }
654    
655  } //end function ipauth_admin_settings_validate  } //end function ipauth_admin_settings_validate
656    
657  /**  /**
# Line 620  function ipauth_admin_settings_submit($f Line 665  function ipauth_admin_settings_submit($f
665      else {      else {
666        $ip2 = sprintf("%u", ip2long(trim($form_state['values']['ip2'])));        $ip2 = sprintf("%u", ip2long(trim($form_state['values']['ip2'])));
667      }      }
668    
669      $sql = "INSERT INTO {ip_authenticator} (ip1, ip2, uid, description, enabled, created) VALUES ('%s','%s','%d','%s', '%d', NOW())";      $sql = "INSERT INTO {ip_authenticator} (ip1, ip2, uid, description, enabled, created) VALUES ('%s','%s','%d','%s', '%d', NOW())";
670      $account = user_load(array('name' => $form_state['values']['username']));      $account = user_load(array('name' => $form_state['values']['username']));
671    
672      if ($account) {      if ($account) {
673        db_query($sql, sprintf("%u", ip2long(trim($form_state['values']['ip1']))), $ip2, $account->uid, $form_state['values']['ipauth_desc'], $form_state['values']['checkbox_enabled']);        db_query($sql, sprintf("%u", ip2long(trim($form_state['values']['ip1']))), $ip2, $account->uid, $form_state['values']['ipauth_desc'], $form_state['values']['checkbox_enabled']);
674        if (db_affected_rows() == 0) {        if (db_affected_rows() == 0) {
# Line 635  function ipauth_admin_settings_submit($f Line 680  function ipauth_admin_settings_submit($f
680      else {      else {
681        drupal_set_message(t('Error while loading the user'), 'error');        drupal_set_message(t('Error while loading the user'), 'error');
682      }      }
683    
684      ipauth_update_checkboxes($form_state);      ipauth_update_checkboxes($form_state);
685    } else {    } else {
686      ipauth_update_checkboxes($form_state);      ipauth_update_checkboxes($form_state);
687    }    }
# Line 647  function ipauth_admin_settings_submit($f Line 692  function ipauth_admin_settings_submit($f
692   * checkbox field status' in the database.   * checkbox field status' in the database.
693   */   */
694  function ipauth_update_checkboxes(&$form_state) {  function ipauth_update_checkboxes(&$form_state) {
695    
696    // Get a list of all input field names    // Get a list of all input field names
697    $checkbox_keys = array_keys($form_state['values']);    $checkbox_keys = array_keys($form_state['values']);
698    
699    //loop through all input fields    //loop through all input fields
700    foreach ($checkbox_keys as $key) {    foreach ($checkbox_keys as $key) {
701    
# Line 660  function ipauth_update_checkboxes(&$form Line 705  function ipauth_update_checkboxes(&$form
705        //update sql statement        //update sql statement
706        $sql = "UPDATE {ip_authenticator} SET enabled = '%d' WHERE id = '%s'";        $sql = "UPDATE {ip_authenticator} SET enabled = '%d' WHERE id = '%s'";
707        $id = substr($key, strrpos($key, '_')+1);        $id = substr($key, strrpos($key, '_')+1);
708    
709        db_query($sql, $form_state['values'][$key], $id);        db_query($sql, $form_state['values'][$key], $id);
710      }      }
711    }    }
# Line 699  function ipauth_admin_edit($form_state) Line 744  function ipauth_admin_edit($form_state)
744      '#description' => 'Enable/Disable',      '#description' => 'Enable/Disable',
745      '#id' => 'enable_disable_checkbox',      '#id' => 'enable_disable_checkbox',
746      '#default_value' => ($row["enabled"] == TRUE) ? 1 : 0,      '#default_value' => ($row["enabled"] == TRUE) ? 1 : 0,
747    );    );
748    $form['ips']['ip1'] = array(    $form['ips']['ip1'] = array(
749      '#prefix' => '<div class="floatLeft">',      '#prefix' => '<div class="floatLeft">',
750      '#type' => 'textfield',      '#type' => 'textfield',
# Line 727  function ipauth_admin_edit($form_state) Line 772  function ipauth_admin_edit($form_state)
772      '#title' => t('Assign to User'),      '#title' => t('Assign to User'),
773      '#autocomplete_path' => 'user/autocomplete',      '#autocomplete_path' => 'user/autocomplete',
774      '#default_value' => $username,      '#default_value' => $username,
775      '#required' => TRUE,      '#required' => TRUE,
776      '#description' => t("Select a user to assign the IPs to"),      '#description' => t("Select a user to assign the IPs to"),
777      '#suffix' => '</div><div class="floatLeft" id="deleteLink">&nbsp; &nbsp; &nbsp; &nbsp;'. l("delete", "admin/user/ip_authenticator/delete/". $args[1]) .'</div>',      '#suffix' => '</div><div class="floatLeft" id="deleteLink">&nbsp; &nbsp; &nbsp; &nbsp;'. l("delete", "admin/user/ip_authenticator/delete/". $args[1]) .'</div>',
778    );    );
# Line 902  function ipauth_get_ip_uid_info($id) { Line 947  function ipauth_get_ip_uid_info($id) {
947   * Queries the database to see if any IP based role changes are found.   * Queries the database to see if any IP based role changes are found.
948   * @return $result - a database result set.   * @return $result - a database result set.
949   */   */
950  function ipauth_get_ip_authenticators($ip = "", $return_fields = "uid", $sort_field = NULL, $sort_order = NULL) {  function ipauth_get_ip_authenticators($ip = "", $return_fields = "uid", $sort_field = NULL, $sort_order = NULL) {
951    $sql_where_clause = ("ALL" == $ip || "all" == $ip || "*" == $ip || "" == $ip) ? "1=1" : "((ip1 = '%s') OR (ip1 <> 0 AND ip1 <= '%s' AND ip2 >= '%s')) AND enabled <> 0";    $sql_where_clause = ("ALL" == $ip || "all" == $ip || "*" == $ip || "" == $ip) ? "1=1" : "((ip1 = '%s') OR (ip1 <> 0 AND ip1 <= '%s' AND ip2 >= '%s')) AND enabled <> 0";
952    $sql_order_by = ($sort_field != "") ? " ORDER BY ".$sort_field." ".$sort_order : "";    $sql_order_by = ($sort_field != "") ? " ORDER BY ".$sort_field." ".$sort_order : "";
953    
954    $sql = "SELECT ". $return_fields .", id FROM {ip_authenticator} WHERE ". $sql_where_clause . $sql_order_by;    $sql = "SELECT ". $return_fields .", id FROM {ip_authenticator} WHERE ". $sql_where_clause . $sql_order_by;
955    
956    // use the php functions and not the mysql function INET_ATON and INET_NTOA functions. This will provide greater database functionality.    // use the php functions and not the mysql function INET_ATON and INET_NTOA functions. This will provide greater database functionality.
957    $long_ip = sprintf("%u", ip2long($ip));    $long_ip = sprintf("%u", ip2long($ip));
958    return db_query($sql, $long_ip, $long_ip, $long_ip);    return db_query($sql, $long_ip, $long_ip, $long_ip);
959  } // end function ipauth_get_ip_authenticators  } // end function ipauth_get_ip_authenticators
960    
961    function ipauth_get_auths_by_name($name="", $return_fields = "uid", $sort_field = NULL, $sort_order = NULL) {
962      $sql_where_clause = ("ALL" == $name || "all" == $name || "*" == $name || "" == $name) ? "1=1" : "((users.`name` LIKE '%s') /*AND ipauth.`enabled` <> 0*/)";
963      $sql_order_by = ($sort_field != "") ? " ORDER BY ".$sort_field." ".$sort_order : "";
964    
965      $sql = "SELECT ". $return_fields .", id FROM {ip_authenticator} ipauth INNER JOIN {users} users ON ipauth.`uid` = users.`uid` WHERE ". $sql_where_clause . $sql_order_by;
966            //printf($sql, '%'.$name.'%');
967      // use the php functions and not the mysql function INET_ATON and INET_NTOA functions. This will provide greater database functionality.
968    
969            return db_query($sql, '%'.$name.'%');
970    
971    }
972  /**  /**
973   * Get an array of all uids used by ip authenticator   * Get an array of all uids used by ip authenticator
974   * @return array List of all uids   * @return array List of all uids
# Line 953  function ipauth_user_page() { Line 1008  function ipauth_user_page() {
1008    $user_login_form = drupal_get_form('user_login');    $user_login_form = drupal_get_form('user_login');
1009    $user->uid = $ipauth_uid;    $user->uid = $ipauth_uid;
1010    //$form['#validate'] = array('user_login_validate' => array());    //$form['#validate'] = array('user_login_validate' => array());
1011    //$form['#submit'] = array('user_login_submit' => array());    //$form['#submit'] = array('user_login_submit' => array());
1012    return $user_login_form;    return $user_login_form;
1013  }  }
1014    
# Line 970  function ipauth_user_register() { Line 1025  function ipauth_user_register() {
1025    $form = user_register();    $form = user_register();
1026    $user->uid = $uid;    $user->uid = $uid;
1027    $form['#validate'] = array('user_register_validate' => array());    $form['#validate'] = array('user_register_validate' => array());
1028    $form['#submit'] = array('user_register_submit' => array());    $form['#submit'] = array('user_register_submit' => array());
1029    return $form;    return $form;
1030  }  }
1031    
# Line 988  function ipauth_user_register() { Line 1043  function ipauth_user_register() {
1043  function is_ipauth_user($not = FALSE, $and = TRUE, $test_uid = FALSE, $test_uid_not = FALSE, $debug_text=NULL) {  function is_ipauth_user($not = FALSE, $and = TRUE, $test_uid = FALSE, $test_uid_not = FALSE, $debug_text=NULL) {
1044    static $ipauth_uids;    static $ipauth_uids;
1045    static $result;    static $result;
1046    
1047    $uid = $GLOBALS['user']->uid;    $uid = $GLOBALS['user']->uid;
1048    
1049    if (!isset($ipauth_uids)) {    if (!isset($ipauth_uids)) {
1050       $ipauth_uids = ipauth_get_uids();       $ipauth_uids = ipauth_get_uids();
1051    }    }
# Line 1009  function is_ipauth_user($not = FALSE, $a Line 1064  function is_ipauth_user($not = FALSE, $a
1064    if ($not) {    if ($not) {
1065      return !$result && $and;      return !$result && $and;
1066    }    }
1067    
1068    return $result && $and;    return $result && $and;
1069  }  }
1070    
# Line 1060  function ipauth_arrow($sort_field, $acti Line 1115  function ipauth_arrow($sort_field, $acti
1115    if ($sort_field == $active_field) {    if ($sort_field == $active_field) {
1116      if ($order == "desc") {      if ($order == "desc") {
1117        if (!$image_down){        if (!$image_down){
1118          $image_down = " ". theme("image", 'misc/arrow-asc.png','asc','asc');          $image_down = " ". theme("image", 'misc/arrow-asc.png','asc','asc');
1119        }        }
1120        return $image_down;        return $image_down;
1121      } else {      } else {
1122        if (!$image_up) {        if (!$image_up) {
1123          $image_up = " ". theme("image", 'misc/arrow-desc.png','desc','desc');          $image_up = " ". theme("image", 'misc/arrow-desc.png','desc','desc');
1124        }        }
1125        return $image_up;        return $image_up;
1126      }      }
1127    }    }
1128    return "";    return "";
1129  }  }
1130    
1131    
1132  /************  /************
1133   * These are theming function to enhance drupal 6.x FAPI   * These are theming function to enhance drupal 6.x FAPI
1134   * to include form elements inside a form api table structure.   * to include form elements inside a form api table structure.
1135   *   *
1136   */   */
1137    
1138  /**  /**
# Line 1085  function ipauth_arrow($sort_field, $acti Line 1140  function ipauth_arrow($sort_field, $acti
1140   */   */
1141  function formtable_elements() {  function formtable_elements() {
1142    $types = array();    $types = array();
1143    
1144    $types['formtable'] = array(    $types['formtable'] = array(
1145      '#input' => TRUE,      '#input' => TRUE,
1146    );    );
1147    
1148    $types['formrow'] = array(    $types['formrow'] = array(
1149      '#input' => TRUE,      '#input' => TRUE,
1150    );    );
1151    
1152    return $types;    return $types;
1153  }  }
1154    
# Line 1121  function ipauth_theme() { Line 1176  function ipauth_theme() {
1176   *   A themed HTML string representing the form table.   *   A themed HTML string representing the form table.
1177   */   */
1178  function theme_formtable($element) {  function theme_formtable($element) {
1179    
1180    //(theme_table doesn't work here because $element['#children'] is already rendered as HTML)    //(theme_table doesn't work here because $element['#children'] is already rendered as HTML)
1181    
1182    $output = "<div class=\"form-table\">\n";    $output = "<div class=\"form-table\">\n";
1183    if ($element['#title']) {    if ($element['#title']) {
1184      $output .= "<span class=\"table-title\">". $element['#title'] .':</span>';      $output .= "<span class=\"table-title\">". $element['#title'] .':</span>';
1185    }    }
1186    
1187    if ($element['#description']) {    if ($element['#description']) {
1188      $output .= "\n<div class=\"description\">". $element['#description'] ."</div>\n";      $output .= "\n<div class=\"description\">". $element['#description'] ."</div>\n";
1189    }    }
# Line 1136  function theme_formtable($element) { Line 1191  function theme_formtable($element) {
1191    $output .= "\n<table id=\"". $element['#id'] .'"'. drupal_attributes($element['#attributes']) .">\n";    $output .= "\n<table id=\"". $element['#id'] .'"'. drupal_attributes($element['#attributes']) .">\n";
1192    if ($element['#caption']) {    if ($element['#caption']) {
1193      $output .= "\t<caption>". $element['#caption'] ."</caption>\n";      $output .= "\t<caption>". $element['#caption'] ."</caption>\n";
1194    }    }
1195    
1196    if ($element['#header']) {    if ($element['#header']) {
1197    
1198      $output .= "\t<thead>\n\t\t<tr>\n";      $output .= "\t<thead>\n\t\t<tr>\n";
1199      foreach ($element['#header'] as $col_header) {      foreach ($element['#header'] as $col_header) {
1200        $output .= "\t\t\t<th>". $col_header ."</th>\n";        $output .= "\t\t\t<th>". $col_header ."</th>\n";
1201      }      }
1202      $output .= "\t\t</tr>\n\t</thead>\n";      $output .= "\t\t</tr>\n\t</thead>\n";
1203    
1204    }    }
1205    $output .= "\t<tbody>\n";    $output .= "\t<tbody>\n";
1206    $output .= $element['#children'];    $output .= $element['#children'];
1207    $output .= "\t</tbody>\n";    $output .= "\t</tbody>\n";
1208    $output .= "</table>\n</div>\n";    $output .= "</table>\n</div>\n";
1209    
1210    return $output;    return $output;
1211    
1212  }  }
1213    
1214  /**  /**
# Line 1162  function theme_formtable($element) { Line 1217  function theme_formtable($element) {
1217   * @param $element   * @param $element
1218   *   An associative array containing the properties of the element.   *   An associative array containing the properties of the element.
1219   *   Properties used: attributes, children.   *   Properties used: attributes, children.
1220   *   *
1221   *   Set 'class' key in the attributes array to 'even' or 'odd' for zebra tables.   *   Set 'class' key in the attributes array to 'even' or 'odd' for zebra tables.
1222   *   *
1223   * @return   * @return
1224   *   A themed HTML string representing the form row.   *   A themed HTML string representing the form row.
1225   */   */

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.12

  ViewVC Help
Powered by ViewVC 1.1.2