Changeset 7833

Show
Ignore:
Timestamp:
06/20/09 09:26:09 (3 years ago)
Author:
p_lindheimer
Message:

added some validations, not done, need to create error div for display and test

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • modules/branches/2.6/sipsettings/functions.inc.php

    r7828 r7833  
    2929define('VIDEO_CODEC','2'); 
    3030define('CUSTOM','9'); 
     31 
     32class sipsettings_validate { 
     33  var $errors = array(); 
     34 
     35  /* checks if value is an integer */ 
     36  function is_int($value, $item, $message) { 
     37    $value = trim($value); 
     38    if ($value != '' && !ctype_digit($value) || $value < 0) { 
     39      $this->errors[] = array('id' => $item, 'value' => $value, 'message' => $message); 
     40    } 
     41    return $value; 
     42  } 
     43 
     44  /* checks if value is valid port between 1024 - 6 65535 */ 
     45  function is_ip_port($value, $item, $message) { 
     46    $value = trim($value); 
     47    if ($value != '' && ($value < 1024 || $value > 65535)) { 
     48      $this->errors[] = array('id' => $item, 'value' => $value, 'message' => $message); 
     49    } 
     50    return $value; 
     51  } 
     52 
     53  /* checks if value is valid ip format */ 
     54  function is_ip($value, $item, $message) { 
     55    $value = trim($value); 
     56    if ($value != '' && !preg_match('|^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$|',$value,$matches)) { 
     57      $this->errors[] = array('id' => $item, 'value' => $value, 'message' => $message); 
     58    } 
     59    return $value; 
     60  } 
     61 
     62  /* checks if value is valid ip netmask format */ 
     63  function is_netmask($value, $item, $message) { 
     64    $value = trim($value); 
     65    if ($value != '' && !(preg_match('|^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$|',$value,$matches) || ($value >= 0 && $value <= 24))) { 
     66      $this->errors[] = array('id' => $item, 'value' => $value, 'message' => $message); 
     67    } 
     68    return $value; 
     69  } 
     70 
     71  /* checks if value is valid alpha numeric format */ 
     72  function is_alphanumeric($value, $item, $message) { 
     73    $value = trim($value); 
     74    if ($value != '' && !preg_match("/^\s*([a-zA-Z0-9 .&-@=_!<>!\"\']+)\s*$/",$value,$matches)) { 
     75      $this->errors[] = array('id' => $item, 'value' => $value, 'message' => $message); 
     76    } 
     77    return $value; 
     78  } 
     79 
     80  /* trigger a validation error to be appended to this class */ 
     81  function log_error($value, $item, $message) { 
     82    $this->errors[] = array('id' => $item, 'value' => $value, 'message' => $message); 
     83    return $value; 
     84  } 
     85} 
    3186 
    3287function sipsettings_hookGet_config($engine) { 
     
    261316  global $db; 
    262317  $save_settings = array(); 
     318  $vd = new  sipsettings_validate(); 
    263319 
    264320  $codecs = $sip_settings['codecs']; 
     
    272328    switch ($key) { 
    273329      case 'bindaddr': 
    274         // ip validate this and store 
    275         $save_settings[] = array($key,$db->escapeSimple($val),'0',NORMAL); 
     330        $msg = _("Bind Address (bindaddr) must be a properly formated IP address."); 
     331        $save_settings[] = array($key,$db->escapeSimple($vd->is_ip($val,$key,$msg)),'0',NORMAL); 
    276332      break; 
    277333 
    278334      case 'bindport': 
    279         // validate-ip-port 
    280         $save_settings[] = array($key,$db->escapeSimple($val),'0',NORMAL); 
     335        $msg = _("Bind Port (bindport) must be a proper IP port between 1024 and 65536 inclusive, typically 5060."); 
     336        $save_settings[] = array($key,$db->escapeSimple($vd->is_ip_port($val, $key, $msg)),'0',NORMAL); 
    281337      break; 
    282338 
    283339      case 'rtpholdtimeout': 
    284340        // validation: must be > $sip_settings['rtptimeout'] (and of course a proper number) 
    285         $save_settings[] = array($key,$db->escapeSimple($val),'0',NORMAL); 
     341        //$vd->log_error(); 
     342        if ($val < $sip_settings['rtptimeout']) { 
     343          $msg = _("The rtpholdtimeout must be bigger than the rtptimeout"); 
     344          $vd->log_error($val, $key, $msg); 
     345        } 
     346        $msg = _("The rtptimeout settng must be a non-negative interger value of seconds"); 
     347        $save_settings[] = array($key,$db->escapeSimple($vd->is_int($val, $key, $msg)),'0',NORMAL); 
    286348      break; 
    287349 
    288350      case 'externrefresh': 
    289       case 'maxcallbitrate': 
    290351      case 'rtptimeout': 
    291352      case 'rtpkeepalive': 
    292353      case 'checkmwi': 
    293354      case 'registertimeout': 
    294       case 'registerattempts': 
    295355      case 'minexpiry': 
    296356      case 'maxexpiry': 
    297357      case 'defaultexpiry': 
     358        $msg = sprintf(_("The Asterisk setting: %s must be a non-negative interger value in seconds."),$key); 
     359        $save_settings[] = array($key,$db->escapeSimple($vd->is_int($val,$key,$msg)),'0',NORMAL); 
     360      break; 
     361 
     362      case 'maxcallbitrate': 
     363      case 'registerattempts': 
    298364      case 'jbmaxsize': 
    299365      case 'jbresyncthreshold': 
    300         // validate-int: 
    301         $save_settings[] = array($key,$db->escapeSimple($val),'0',NORMAL); 
     366        $msg = sprintf(_("The Asterisk setting: %s must be a non-negative interger value."),$key); 
     367        $save_settings[] = array($key,$db->escapeSimple($vd->is_int($val,$key,$msg)),'0',NORMAL); 
    302368      break; 
    303369 
    304370      case 'sip_language': 
    305         // validate-alphanumeric 
    306         $save_settings[] = array($key,$db->escapeSimple($val),'0',NORMAL); 
     371        $msg = sprintf(_("The language value must be alphanumeric and should be a supported and installed language."),$key); 
     372        $save_settings[] = array($key,$db->escapeSimple($vd->is_alphanumeric($val,$key,$msg)),'0',NORMAL); 
    307373      break; 
    308374 
     
    330396        // ip validate this and store 
    331397        $seq = substr($key,9); 
    332         $save_settings[] = array($key,$db->escapeSimple($val),$seq,NORMAL);  
     398        $msg = _("Localnet setting must be a valid IP address format."); 
     399        $save_settings[] = array($key,$db->escapeSimple($vd->is_ip($val,$key,$msg)),$seq,NORMAL);  
    333400      } else if (substr($key,0,8) == "netmask_") { 
    334401        // ip validate this and store 
    335402        $seq = substr($key,8); 
    336         $save_settings[] = array($key,$db->escapeSimple($val),$seq,NORMAL); 
     403        $msg = _("Localnet mask must be in a proper netmask format such as 255.255.255.0 or 24."); 
     404        $save_settings[] = array($key,$db->escapeSimple($vd->is_netmask($val,$key,$msg)),$seq,NORMAL);  
    337405      } else if (substr($key,0,15) == "sip_custom_key_") { 
    338406        $seq = substr($key,15); 
     
    345413    } 
    346414  } 
    347   $seq = 0; 
    348   foreach ($codecs as $key => $val) { 
    349     $save_settings[] = array($db->escapeSimple($key),$db->escapeSimple($val),$seq++,CODEC); 
    350   } 
    351   $seq = 0; 
    352   foreach ($video_codecs as $key => $val) { 
    353     $save_settings[] = array($db->escapeSimple($key),$db->escapeSimple($val),$seq++,VIDEO_CODEC);  
    354   } 
    355  
    356   // TODO: shouldn't do DELETE/INSERT (Joel doesn't like it) but for now ... 
    357   // 
    358   sql("DELETE FROM `sipsettings` WHERE 1"); 
    359  
    360   $compiled = $db->prepare('INSERT INTO `sipsettings` (`keyword`, `data`, `seq`, `type`) VALUES (?,?,?,?)'); 
    361   $result = $db->executeMultiple($compiled,$save_settings); 
    362   if(DB::IsError($result)) { 
    363     die_freepbx($result->getDebugInfo()."<br><br>".'error adding to sipsettings table');  } 
    364   return true; 
     415 
     416  /* if there were any validation errors, we will return them and not proceed with saving */ 
     417  if (count($vd->errors)) { 
     418    return $vd->errors; 
     419  } else { 
     420    $seq = 0; 
     421    foreach ($codecs as $key => $val) { 
     422      $save_settings[] = array($db->escapeSimple($key),$db->escapeSimple($val),$seq++,CODEC); 
     423    } 
     424    $seq = 0; 
     425    foreach ($video_codecs as $key => $val) { 
     426      $save_settings[] = array($db->escapeSimple($key),$db->escapeSimple($val),$seq++,VIDEO_CODEC);  
     427    } 
     428 
     429    // TODO: normally don't like doing delete/insert but otherwise we would have do update for each 
     430    //       individual setting and then an insert if there was nothing to update. So this is cleaner 
     431    //       this time around. 
     432    // 
     433    sql("DELETE FROM `sipsettings` WHERE 1"); 
     434    $compiled = $db->prepare('INSERT INTO `sipsettings` (`keyword`, `data`, `seq`, `type`) VALUES (?,?,?,?)'); 
     435    $result = $db->executeMultiple($compiled,$save_settings); 
     436    if(DB::IsError($result)) { 
     437      die_freepbx($result->getDebugInfo()."<br><br>".'error adding to sipsettings table');  } 
     438    return true; 
     439  } 
    365440} 
    366441 
  • modules/branches/2.6/sipsettings/page.sipsettings.php

    r7832 r7833  
    2020 
    2121  /* Determines how many columns per row for the codecs and formats the table */ 
    22   $cols_per_row  = 4; 
    23   $width         = (100.0 / $cols_per_row); 
    24   $tabindex      = 0; 
    25   $dispnum       = "sipsettings"; 
     22  $cols_per_row   = 4; 
     23  $width          = (100.0 / $cols_per_row); 
     24  $tabindex       = 0; 
     25  $dispnum        = "sipsettings"; 
     26  $error_displays = array(); 
    2627 
    2728  $action                            = isset($_POST['action'])?$_POST['action']:''; 
     
    3536  $p_idx = 0; 
    3637  $n_idx = 0; 
    37   while (isset($_POST["localnet-$p_idx"])) { 
    38     if ($_POST["localnet-$p_idx"] != '') { 
    39       $sip_settings["localnet_$n_idx"] = htmlspecialchars($_POST["localnet-$p_idx"]); 
    40       $sip_settings["netmask_$n_idx"]  = htmlspecialchars($_POST["netmask-$p_idx"]); 
     38  while (isset($_POST["localnet_$p_idx"])) { 
     39    if ($_POST["localnet_$p_idx"] != '') { 
     40      $sip_settings["localnet_$n_idx"] = htmlspecialchars($_POST["localnet_$p_idx"]); 
     41      $sip_settings["netmask_$n_idx"]  = htmlspecialchars($_POST["netmask_$p_idx"]); 
    4142      $n_idx++; 
    4243    }  
     
    112113  $p_idx = 0; 
    113114  $n_idx = 0; 
    114   while (isset($_POST["sip-custom-key-$p_idx"])) { 
    115     if ($_POST["sip-custom-key-$p_idx"] != '') { 
    116       $sip_settings["sip_custom_key_$n_idx"] = htmlspecialchars($_POST["sip-custom-key-$p_idx"]); 
    117       $sip_settings["sip_custom_val_$n_idx"] = htmlspecialchars($_POST["sip-custom-val-$p_idx"]); 
     115  while (isset($_POST["sip_custom_key_$p_idx"])) { 
     116    if ($_POST["sip_custom_key_$p_idx"] != '') { 
     117      $sip_settings["sip_custom_key_$n_idx"] = htmlspecialchars($_POST["sip_custom_key_$p_idx"]); 
     118      $sip_settings["sip_custom_val_$n_idx"] = htmlspecialchars($_POST["sip_custom_val_$p_idx"]); 
    118119      $n_idx++; 
    119120    }  
     
    124125  case "edit":  //just delete and re-add 
    125126    if (($errors = sipsettings_edit($sip_settings)) !== true) { 
    126       sippsettings_process_errors($errors); 
     127      $error_displays = process_errors($errors); 
    127128    } else { 
    128129      needreload(); 
     
    131132  break; 
    132133  default: 
     134    /* only get them if first time load, if they pressed submit, use values from POST */ 
    133135    $sip_settings = sipsettings_get(); 
    134136} 
     
    140142  <h2><?php echo _("Edit Settings"); ?></h2> 
    141143<?php 
     144  if (!empty($error_displays)) { 
     145?> 
     146    <div class="sip-errors"> 
     147<?php 
     148    foreach ($error_displays as $div_disp) { 
     149      echo "<p>".$div_disp['message']."</p>"; 
     150    } 
     151?> 
     152    </div> 
     153<?php 
     154  } 
    142155 
    143156  /* We massaged these above or they came from sipsettings_get() if this is not 
     
    232245    </td> 
    233246    <td> 
    234       <input type="text" id="localnet-0" name="localnet-0" class="localnet validate=ip" value="<?php echo $localnet_0 ?>" tabindex="<?php echo ++$tabindex;?>"> / 
    235       <input type="text" id="netmask-0" name="netmask-0" class="validate-netmask" value="<?php echo $netmask_0 ?>" tabindex="<?php echo ++$tabindex;?>"> 
     247      <input type="text" id="localnet_0" name="localnet_0" class="localnet validate=ip" value="<?php echo $localnet_0 ?>" tabindex="<?php echo ++$tabindex;?>"> / 
     248      <input type="text" id="netmask_0" name="netmask_0" class="validate-netmask" value="<?php echo $netmask_0 ?>" tabindex="<?php echo ++$tabindex;?>"> 
    236249    </td> 
    237250  </tr> 
     
    249262    </td> 
    250263    <td> 
    251       <input type="text" id="localnet-$idx" name="localnet-$idx" class="localnet validate-ip" value="{$$var_localnet}" tabindex="$tabindex"> / 
     264      <input type="text" id="localnet_$idx" name="localnet_$idx" class="localnet validate-ip" value="{$$var_localnet}" tabindex="$tabindex"> / 
    252265END; 
    253266      $tabindex++; 
    254267      echo <<< END 
    255       <input type="text" id="netmask-$idx" name="netmask-$idx" class="validate-netmask" value="{$$var_netmask}" tabindex="$tabindex"> 
     268      <input type="text" id="netmask_$idx" name="netmask_$idx" class="validate-netmask" value="{$$var_netmask}" tabindex="$tabindex"> 
    256269    </td> 
    257270  </tr> 
     
    712725    </td> 
    713726    <td> 
    714       <input type="text" id="sip-custom-key-0" name="sip-custom-key-0" class="sip-custom" value="<?php echo $sip_custom_key_0 ?>" tabindex="<?php echo ++$tabindex;?>"> = 
    715       <input type="text" id="sip-custom-val-0" name="sip-custom-val-0" value="<?php echo $sip_custom_val_0 ?>" tabindex="<?php echo ++$tabindex;?>"> 
     727      <input type="text" id="sip_custom_key_0" name="sip_custom_key_0" class="sip-custom" value="<?php echo $sip_custom_key_0 ?>" tabindex="<?php echo ++$tabindex;?>"> = 
     728      <input type="text" id="sip_custom_val_0" name="sip_custom_val_0" value="<?php echo $sip_custom_val_0 ?>" tabindex="<?php echo ++$tabindex;?>"> 
    716729    </td> 
    717730  </tr> 
     
    729742    </td> 
    730743    <td> 
    731       <input type="text" id="sip-custom-key-$idx" name="sip-custom-key-$idx" class="sip-custom" value="{$$var_sip_custom_key}" tabindex="$tabindex"> = 
     744      <input type="text" id="sip_custom_key_$idx" name="sip_custom_key_$idx" class="sip-custom" value="{$$var_sip_custom_key}" tabindex="$tabindex"> = 
    732745END; 
    733746      $tabindex++; 
    734747      echo <<< END 
    735       <input type="text" id="sip-custom-val-$idx" name="sip-custom-val-$idx" value="{$$var_sip_custom_val}" tabindex="$tabindex"> 
     748      <input type="text" id="sip_custom_val_$idx" name="sip_custom_val_$idx" value="{$$var_sip_custom_val}" tabindex="$tabindex"> 
    736749    </td> 
    737750  </tr> 
     
    777790          $.each(data.localnet, function(loc,mask){ 
    778791            if (cnt < fields) { 
    779               $('#localnet-'+cnt).attr("value",loc); 
    780               $('#netmask-'+cnt).attr("value",mask); 
     792              $('#localnet_'+cnt).attr("value",loc); 
     793              $('#netmask_'+cnt).attr("value",mask); 
    781794            } else { 
    782795              addLocalnet(loc,mask); 
     
    846859    $(".jitter-buffer").hide(); 
    847860  }); 
     861<?php 
     862  /* this will insert the addClass jquery calls to all id's in error */ 
     863  if (!empty($error_displays)) { 
     864    foreach ($error_displays as $js_disp) { 
     865      echo "  ".$js_disp['js']; 
     866    } 
     867  } 
     868?> 
    848869}); 
    849870 
     
    854875  var idx = $(".localnet").size(); 
    855876  var idxp = idx - 1; 
    856   var tabindex = parseInt($("#netmask-"+idxp).attr('tabindex')) + 1; 
     877  var tabindex = parseInt($("#netmask_"+idxp).attr('tabindex')) + 1; 
    857878  var tabindexp = tabindex + 1; 
    858879 
     
    862883    </td>\ 
    863884    <td>\ 
    864       <input type="text" id="localnet-'+idx+'" name="localnet-'+idx+'" class="localnet" value="'+localnet+'" tabindex="'+tabindex+'"> /\ 
    865       <input type="text" id="netmask-'+idx+'" name="netmask-'+idx+'" value="'+netmask+'" tabindex="'+tabindexp+'">\ 
     885      <input type="text" id="localnet_'+idx+'" name="localnet_'+idx+'" class="localnet" value="'+localnet+'" tabindex="'+tabindex+'"> /\ 
     886      <input type="text" id="netmask_'+idx+'" name="netmask_'+idx+'" value="'+netmask+'" tabindex="'+tabindexp+'">\ 
    866887    </td>\ 
    867888  </tr>\ 
     
    873894  var idx = $(".sip-custom").size(); 
    874895  var idxp = idx - 1; 
    875   var tabindex = parseInt($("#sip-custom-val-"+idxp).attr('tabindex')) + 1; 
     896  var tabindex = parseInt($("#sip_custom_val_"+idxp).attr('tabindex')) + 1; 
    876897  var tabindexp = tabindex + 1; 
    877898 
     
    881902    </td>\ 
    882903    <td>\ 
    883       <input type="text" id="sip-custom-key-'+idx+'" name="sip-custom-key-'+idx+'" class="sip-custom" value="'+key+'" tabindex="'+tabindex+'"> =\ 
    884       <input type="text" id="sip-custom-val-'+idx+'" name="sip-custom-val-'+idx+'" value="'+val+'" tabindex="'+tabindexp+'">\ 
     904      <input type="text" id="sip_custom_key_'+idx+'" name="sip_custom_key_'+idx+'" class="sip-custom" value="'+key+'" tabindex="'+tabindex+'"> =\ 
     905      <input type="text" id="sip_custom_val_'+idx+'" name="sip_custom_val_'+idx+'" value="'+val+'" tabindex="'+tabindexp+'">\ 
    885906    </td>\ 
    886907  </tr>\ 
     
    951972<?php    
    952973     
    953 function sippsettings_process_errors($errors) { 
    954   /* TODO: process the array of errors and show issues somewhere */ 
     974function process_errors($errors) { 
     975  foreach($errors as $error) { 
     976    $error_display[] = array( 
     977      'js' => "$('#".$error['id']."').addClass('validation-error');\n", 
     978      'div' => $error['message'], 
     979    ); 
     980  } 
     981  return $error_display; 
    955982} 
    956983?>