<?

/**
 * fieldcheck.mod.php
 * @package   Modules
 */

/**
 * Module name
 */
define("MOD_FIELDCHECK",$name);

$modules[$name]= new FieldCheckModule();

/**
 * FieldCheckModule
 *
 * This module checks if the input of a field matches a particular regular
 * expression.
 * @package   Modules
 * @author    Herman Suijs
 */
class FieldCheckModule extends Module {
  var 
$name "FieldCheckModule";
  var 
$id MOD_FIELDCHECK;
  var 
$execlevel 16;
  
// Default checks
  
var $checks = array(
    
"email"    =>REGEX_MAIL,
    
"url"    =>REGEX_URL,
    
"time"  =>REGEX_TIME,
    
"date"  =>REGEX_DATE,
    
"phone:phonenr:phonenumber"    =>REGEX_PHONE,
    
"fax:faxnr:faxnumber" =>REGEX_PHONE,
    );

  
/**
   * Constructor
   *
   * Set options and call the superConstructor
   */
  
function FieldCheckModule() {
    
$this->Module();
  } 
/* function FieldCheckModule() */

  /**
   * Add a new check
   */
  
function add_check($name,$regex) {
    global 
$debug;
    
$this->checks[$name] = $regex;
    
//$debug->add_message("Added check $name",$this->name);
    
$debug->add(DB_XFLOW,$this->name,"Added check %s",$name);
  } 
/* function add_check($name,$rege... */

  /**
   * Remove a check
   */
  
function remove_check($check) {
    global 
$debug;
    
// With this function it should be possible to look for string a string
    // in the keys of the checks-array and remove the checks from the keys
    // which name was found.

    //$debug->add_message("Added check $name",$this->name);
    
$debug->add(DB_XFLOW,$this->name,"Removed check %s",$name);
  } 
/* function remove_check($check) */

  /**
   * Replace a check
   */
  
function replace_check($check,$name,$regex) {
    
$this->remove_check($check);
    
$this->add_check($name,$regex);
  } 
/* function replace_check($check,... */
  
  /**
   * Before action
   *
   * Check if there are any options, otherwise disable
   */
  
function before_action() {
    global 
$debug;
    if (empty(
$this->checks)) {
      
$this->disable();
      
$debug->add(DB_WARN,$this->name,"No checks found, module disabled");
    } 
/* if (empty($this->checks)) */
    // No options defined, so no check needed.
    //$this->check_options();
  
/* function before_action() */

  /**
   * Check field routine
   */
  
function check_field($vars,$fieldname,$value) {
    global 
$debug,$error;
    if (isset(
$vars[$fieldname])&&!empty($vars[$fieldname])) {
      
$debug->add(DB_FLOW,$this->name,"Check %s",$fieldname);
      if (!
ereg($value,$vars[$fieldname])) {
    
$error->add(ERR_INP_CONT,$this->name,"Field %s is not right",$fieldname);
      } 
/* if (!ereg($value,$vars[$fieldn... */
    
/* if (isset($vars[$fieldname])&&... */
  
/* function check_field($vars,$fi... */

  /**
   * Do action
   *
   * Call the check field routine for every check
   */
  
function do_action() {
    global 
$formHandler;
    
$this->action();

    
$vars $formHandler->get_vars();
    while (list (
$fieldname,$value) = each($this->checks)) {
      if (!
strpos($fieldname,FIELDCHECK_SEP)===false) {
    
$fieldnames explode(FIELDCHECK_SEP,$fieldname);
    while (list(,
$fieldname) = each($fieldnames)) {
      
$this->check_field($vars,$fieldname,$value);
    } 
/* while (list(,$fieldname) = eac... */
      
} else { /* if (!strpos($fieldname,FIELDCH... */
    
$this->check_field($vars,$fieldname,$value);
   } 
/* if (!strpos($fieldname,FIELDCH... */
    
/* while (list ($fieldname,$value... */

  
/* function do_action() */

  /**
   * After action
   *
   * Cleanup
   */
  
function after_action() {
    
$this->clean_up();
  } 
/* function after_action() */

/* class FieldCheckModule extends... */