/[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.9, Wed Apr 8 17:42:22 2009 UTC revision 1.10, Tue Apr 21 12:31:44 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: ipauth.module,v 1.8 2009/03/17 14:21:02 jonfrancisskydiver Exp $  // $Id$
3  /**  /**
4   * @file   * @file
5   * Module code for an IP based authenticator   * Module code for an IP based authenticator
# Line 18  function ipauth_boot() { Line 18  function ipauth_boot() {
18    
19    // 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
20    // a new uid has to be assigned.    // a new uid has to be assigned.
21    if (!$user->uid || in_array($user->uid, ipauth_get_uids())) {    if (!$user->uid || in_array($user->uid, ipauth_get_uids("enabled"))) {
22    
23      // 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.
24      $result = ipauth_get_ip_authenticators($_SERVER['REMOTE_ADDR'], "uid");      $result = ipauth_get_ip_authenticators($_SERVER['REMOTE_ADDR'], "uid");
# Line 61  function ipauth_boot() { Line 61  function ipauth_boot() {
61      }      }
62    }    }
63    
64    if (in_array($user->uid, ipauth_get_uids())) {    if (in_array($user->uid, ipauth_get_uids('enabled'))) {
65      // It's one of the special ip_auth users, take the authenticated user role away.      // It's one of the special ip_auth users, take the authenticated user role away.
66      unset($user->roles[DRUPAL_AUTHENTICATED_RID]);      unset($user->roles[DRUPAL_AUTHENTICATED_RID]);
67      $user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user';      $user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user';
# Line 264  function _ipauth_path_allowed() { Line 264  function _ipauth_path_allowed() {
264    
265  function ipauth_import_export($form_state) {  function ipauth_import_export($form_state) {
266    $form['#attributes']['enctype'] = 'multipart/form-data';    $form['#attributes']['enctype'] = 'multipart/form-data';
267      /*
268    $form['export_fieldset'] = array(    $form['export_fieldset'] = array(
269      '#type' => 'fieldset',      '#type' => 'fieldset',
270      '#title' => t('Export IP Authenticators to a CSV file'),      '#title' => t('Export IP Authenticators to a CSV file'),
271      '#description' => t('This will cause you to download a CSV file containing all IP Authenticators.'),      '#description' => t('This will cause you to download a CSV file containing all IP Authenticators.'),
272      '#weight' => 1,      '#weight' => 1,
273    );    );
274    $form['export_fieldset']['export'] = array(     $form['export_fieldset']['export'] = array(
275      '#value' => l('Export', 'admin/user/ip_authenticator/data/export'),      '#value' => l('Export', 'admin/user/ip_authenticator/data/export'),
276      '#weight' => 1      '#weight' => 1
277    );    );
278      */
279    $form['import_fieldset'] = array(    $form['import_fieldset'] = array(
280      '#type' => 'fieldset',      '#type' => 'fieldset',
281      '#title' => t('Import IP Authenticators from a CSV file'),      '#title' => t('Import IP Authenticators from a CSV file'),
# Line 289  function ipauth_import_export($form_stat Line 290  function ipauth_import_export($form_stat
290      '#description' => t('This will import all the IP Authenticator\'s in the CSV file to the database.')      '#description' => t('This will import all the IP Authenticator\'s in the CSV file to the database.')
291    );    );
292    $form['import_fieldset']['belowmarkup'] = array(    $form['import_fieldset']['belowmarkup'] = array(
293      'value' => t('The format of the CSV file is as follows:') .'<Br/>' .      '#value' => t('The format of the CSV file is as follows:') .'<Br/><ul><li>' .
294                 t('"Row ID","Drupal User ID","Unsigned Int of IP","Unsigned Int of IP","description","0 for disabledm, or 1 for enabled","unix timestamp"') ."<br/>",                 t('"Row ID","Drupal User ID","IP Address","IP Address","description","0 for disabled, or 1 for enabled","unix timestamp"') ."</li></ul><br/>",
295    
296    );    );
297    $form['import_fieldset']['submit'] = array(    $form['import_fieldset']['submit'] = array(
# Line 343  function ipauth_import_export_submit($fo Line 344  function ipauth_import_export_submit($fo
344        array_walk($row, '_ipauth_strip_quotes');        array_walk($row, '_ipauth_strip_quotes');
345        if (_ipauth_check_import($row)) {        if (_ipauth_check_import($row)) {
346          $sql = "INSERT INTO {ip_authenticator} (uid, ip1, ip2, description, enabled, created) VALUES ('%s','%s','%s','%s', '%d', '%s')";          $sql = "INSERT INTO {ip_authenticator} (uid, ip1, ip2, description, enabled, created) VALUES ('%s','%s','%s','%s', '%d', '%s')";
347          db_query($sql, $row["uid"], $row["ip1"], $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"]);
348        }        }
349      }      }
350    
# Line 363  function ipauth_admin_settings($form_sta Line 364  function ipauth_admin_settings($form_sta
364    
365    $form['ip_list'] = array(    $form['ip_list'] = array(
366      '#type' => 'fieldset',      '#type' => 'fieldset',
367      '#title' => t('Current IP Authenticators'),      '#title' => t('Modify the IP Authenticators'),
368      '#description' => "<b>". t("Instructions:") ."</b><br/> &nbsp;".      '#description' => "<b>". t("Instructions:") ."</b><br/> &nbsp;".
369                        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; ".
370                        t(" - Click on a user name to edit the account.") ."<br/> &nbsp; ".                        t(" - Click on a user name to edit the account.") ."<br/> &nbsp; ".
371                        t(" - The title fields are sortable.") ."<br/><br/>".                        t(" - The title fields are sortable."),
                       "<b>". t("Your IP: @s", array('@s' => $_SERVER['REMOTE_ADDR'])) ."</b>".  
                       '<br /><br/>'.  
                       t('Please note that users authenticated via IP authenticator do not get permissions of the <em>authenticated user</em> role.'),  
372      '#collapsible' => TRUE,      '#collapsible' => TRUE,
373      '#collapsed' => FALSE,      '#collapsed' => FALSE,
374      '#weight' => 0      '#weight' => 0
375    );    );
376    
377    $form['ip_list']['import-export'] = array(    $form['ip_list']['import-export'] = array(
378      '#value' => l("Import/Export", "admin/user/ip_authenticator/data"),      '#value' => l("Export", "admin/user/ip_authenticator/data/export", array('attributes'=>array('class'=>'button'))),
379      '#prefix' => '<div id="import-export">',      '#prefix' => '<div id="import-export">',
380      '#suffix' => '</div>',      '#suffix' => '</div>',
381      '#weight' => 10,      '#weight' => 0,
382    );    );
383    
384    $non_blocked_users = array();    $non_blocked_users = array();
# Line 411  function ipauth_admin_settings($form_sta Line 409  function ipauth_admin_settings($form_sta
409    //and incorporated into this ipauth module.    //and incorporated into this ipauth module.
410    $form['ip_list']['ipauth_table'] = array(    $form['ip_list']['ipauth_table'] = array(
411        '#type' => 'formtable',        '#type' => 'formtable',
412        '#header' => array(        '#header' => array(
                           l(t('Active'), $_GET["q"], array('query' => array('sort_field' => 'enabled', 'order' => $order_toggle[$order]))) . ipauth_arrow($sort_field, 'enabled', $order),  
413                            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),
414                              l(t('Active'), $_GET["q"], array('query' => array('sort_field' => 'enabled', 'order' => $order_toggle[$order]))) . ipauth_arrow($sort_field, 'enabled', $order),
415                            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),
416                            l(t('Description'), $_GET["q"], array('query' => array('sort_field' => 'description', 'order' => $order_toggle[$order]))). ipauth_arrow($sort_field, 'description', $order),                            l(t('Description'), $_GET["q"], array('query' => array('sort_field' => 'description', 'order' => $order_toggle[$order]))). ipauth_arrow($sort_field, 'description', $order),
417                            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),)
# Line 448  function ipauth_admin_settings($form_sta Line 446  function ipauth_admin_settings($form_sta
446          '#attributes' => array('class' => ($evenodd_counter%2==1) ? "even" : "odd"),          '#attributes' => array('class' => ($evenodd_counter%2==1) ? "even" : "odd"),
447        );        );
448    
449          $form['ip_list']['ipauth_table']['row_' .$row->id]['iprange'] = array(
450              '#value' =>  l($ip_title, "admin/user/ip_authenticator/edit/". $row->id),
451              '#prefix' => '<td>',
452              '#suffix' => '</td>'
453          );
454        $form['ip_list']['ipauth_table']['row_' .$row->id]['checkbox_' .$row->id] = array(        $form['ip_list']['ipauth_table']['row_' .$row->id]['checkbox_' .$row->id] = array(
455          '#prefix' => '<td>',          '#prefix' => '<td>',
456          '#suffix' => '</td>',          '#suffix' => '</td>',
457          '#type' => 'checkbox',          '#type' => 'checkbox',
458          '#default_value' => ($row->enabled == TRUE) ? 1 : 0,          '#default_value' => ($row->enabled == TRUE) ? 1 : 0,
459        );        );
   
       $form['ip_list']['ipauth_table']['row_' .$row->id]['iprange'] = array(  
           '#value' =>  l($ip_title, "admin/user/ip_authenticator/edit/". $row->id),  
           '#prefix' => '<td>',  
           '#suffix' => '</td>'  
       );  
460        $form['ip_list']['ipauth_table']['row_' .$row->id]['user'] = array(        $form['ip_list']['ipauth_table']['row_' .$row->id]['user'] = array(
461            '#value' =>  $user_title,            '#value' =>  $user_title,
462            '#prefix' => '<td>',            '#prefix' => '<td>',
# Line 495  function ipauth_admin_settings($form_sta Line 492  function ipauth_admin_settings($form_sta
492    
493    $form['ips'] = array(    $form['ips'] = array(
494      '#type' => 'fieldset',      '#type' => 'fieldset',
495      '#title' => t('IP address assignment') ."<br/>",      '#title' => t('Add an IP Authenticator') ."<br/>",
496        '#description' =>
497                          /*"<div class='ipauth_ip'><b>". t("Your IP: @s", array('@s' => $_SERVER['REMOTE_ADDR'])) ."</b></div>".
498                          '<br /><br/>'. */
499                          t('Please note that users authenticated via IP authenticator do not get permissions of the <em>authenticated user</em> role.'),
500      '#collapsible' => FALSE,      '#collapsible' => FALSE,
501      '#collapsed' => FALSE,      '#collapsed' => FALSE,
502      '#weight' => 0      '#weight' => -1
503    );    );
504    $form['ips']['checkbox_enabled'] = array(    $form['ips']['checkbox_enabled'] = array(
505      '#prefix' => '<div class="floatLeft checkbox form-item"><label>'. t('Active') .'</label>',      '#prefix' => '<div class="floatLeft checkbox form-item"><label>'. t('Active') .'</label>',
# Line 513  function ipauth_admin_settings($form_sta Line 514  function ipauth_admin_settings($form_sta
514      '#type' => 'textfield',      '#type' => 'textfield',
515      '#title' => 'IP 1',      '#title' => 'IP 1',
516      '#size' => '15',      '#size' => '15',
517        '#description' => "<div class='ipauth_ip'>". t('Your IP: ') . '<a href="javascript:void(0);" class="ip">'. $_SERVER["REMOTE_ADDR"] ."</a></div>",
518      /* '#required' => TRUE, */      /* '#required' => TRUE, */
519      '#suffix' => '</div><div class="floatLeft" id="formSeperator">&nbsp;&nbsp; - &nbsp;&nbsp;</div>'      '#suffix' => '</div><div class="floatLeft" id="formSeperator">&nbsp;&nbsp; - &nbsp;&nbsp;</div>'
520    );    );
# Line 542  function ipauth_admin_settings($form_sta Line 544  function ipauth_admin_settings($form_sta
544      '#title' => t('Description of Authenticator'),      '#title' => t('Description of Authenticator'),
545      '#description' => t("Enter a description to better identify this authenticator"),      '#description' => t("Enter a description to better identify this authenticator"),
546    );    );
547      $form['ips']['submit1'] = array(
548        '#type' => 'submit',
549        '#value' => 'Add IP Authenticator',
550        '#weight' => 1,
551      );
552      $form['ips']['import-export1'] = array(
553        '#value' => l("Import", "admin/user/ip_authenticator/data", array('attributes'=>array('class'=>'button'))),
554        '#prefix' => '<div id="import-export1">',
555        '#suffix' => '</div>',
556        '#weight' => 1,
557      );
558    $form['#validate'][] = 'ipauth_admin_settings_validate';    $form['#validate'][] = 'ipauth_admin_settings_validate';
559    $form['#submit'][] = 'ipauth_admin_settings_submit';    $form['#submit'][] = 'ipauth_admin_settings_submit';
560    
561    return system_settings_form($form);    $form['submit'] = array(
562        '#type' => 'submit',
563        '#value' => 'Save Configuration',
564        '#weight' => 1,
565      );
566    
567      return $form; //system_settings_form($form);
568  } // end function ipauth_admin_settings  } // end function ipauth_admin_settings
569    
570  /**  /**
# Line 719  function ipauth_admin_edit($form_state) Line 737  function ipauth_admin_edit($form_state)
737      '#value' => $row["description"],      '#value' => $row["description"],
738      '#description' => t("Enter a description to better identify this authenticator"),      '#description' => t("Enter a description to better identify this authenticator"),
739    );    );
740    
741    $form['id'] = array(    $form['id'] = array(
742      '#type' => 'hidden',      '#type' => 'hidden',
743      '#value' => $args[1],      '#value' => $args[1],
# Line 852  function ipauth_block($op = 'list', $del Line 871  function ipauth_block($op = 'list', $del
871          // For usability's sake, avoid showing two login forms on one page.          // For usability's sake, avoid showing two login forms on one page.
872          // In contrast to the user.module login block this block is shown when          // In contrast to the user.module login block this block is shown when
873          // an ip_auth authenticated user is online.          // an ip_auth authenticated user is online.
874          if (in_array($user->uid, ipauth_get_uids()) && !(arg(0) == 'user' && !is_numeric(arg(1)))) {          if (in_array($user->uid, ipauth_get_uids('enabled')) && !(arg(0) == 'user' && !is_numeric(arg(1)))) {
875            $block['subject'] = t('User login');            $block['subject'] = t('User login');
876            $block['content'] = drupal_get_form('user_login_block');            $block['content'] = drupal_get_form('user_login_block');
877          }          }
# Line 898  function ipauth_get_ip_authenticators($i Line 917  function ipauth_get_ip_authenticators($i
917   * Get an array of all uids used by ip authenticator   * Get an array of all uids used by ip authenticator
918   * @return array List of all uids   * @return array List of all uids
919   */   */
920  function ipauth_get_uids() {  function ipauth_get_uids($which = 'all') {
921    static $uids;    static $uids;
922    if (!isset($uids)) {    static $which_cached;
923    
924      if (!isset($uids) || !isset($which_cached[$which]) ) {
925      $uids = array();      $uids = array();
926      $sql = "SELECT DISTINCT uid FROM {ip_authenticator} WHERE 1";      $which_cached[$which] = TRUE;
927        if ($which == 'enabled') {
928          $where = 'enabled = 1';
929        } else {
930          $where = '1';
931        }
932        $sql = "SELECT DISTINCT uid FROM {ip_authenticator} WHERE " .$where;
933    
934      $result = db_query($sql);      $result = db_query($sql);
935      while ($row = db_fetch_array($result)) {      while ($row = db_fetch_array($result)) {
936        $uids[] = $row['uid'];        $uids[] = $row['uid'];
# Line 998  function ipauth_export($which_ones) { Line 1026  function ipauth_export($which_ones) {
1026    $result = ipauth_get_ip_authenticators(NULL, "id, uid, ip1, ip2, description, enabled, UNIX_TIMESTAMP(created) as created");    $result = ipauth_get_ip_authenticators(NULL, "id, uid, ip1, ip2, description, enabled, UNIX_TIMESTAMP(created) as created");
1027    while ($row = db_fetch_array($result)) {    while ($row = db_fetch_array($result)) {
1028      printf('"%s","%s","%s","%s","%s","%s","%s"      printf('"%s","%s","%s","%s","%s","%s","%s"
1029  ', $row["id"],$row["uid"],$row["ip1"],$row["ip2"],$row["description"],$row["enabled"],$row["created"]);  ', $row["id"],$row["uid"],long2ip($row["ip1"]),long2ip($row["ip2"]),$row["description"],$row["enabled"],$row["created"]);
1030    }    }
1031    
1032    module_invoke_all('exit', $url);    module_invoke_all('exit', $url);

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.10

  ViewVC Help
Powered by ViewVC 1.1.2