Changeset 7833
- Timestamp:
- 06/20/09 09:26:09 (3 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
modules/branches/2.6/sipsettings/functions.inc.php
r7828 r7833 29 29 define('VIDEO_CODEC','2'); 30 30 define('CUSTOM','9'); 31 32 class 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 } 31 86 32 87 function sipsettings_hookGet_config($engine) { … … 261 316 global $db; 262 317 $save_settings = array(); 318 $vd = new sipsettings_validate(); 263 319 264 320 $codecs = $sip_settings['codecs']; … … 272 328 switch ($key) { 273 329 case 'bindaddr': 274 // ip validate this and store275 $save_settings[] = array($key,$db->escapeSimple($v al),'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); 276 332 break; 277 333 278 334 case 'bindport': 279 // validate-ip-port280 $save_settings[] = array($key,$db->escapeSimple($v al),'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); 281 337 break; 282 338 283 339 case 'rtpholdtimeout': 284 340 // 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); 286 348 break; 287 349 288 350 case 'externrefresh': 289 case 'maxcallbitrate':290 351 case 'rtptimeout': 291 352 case 'rtpkeepalive': 292 353 case 'checkmwi': 293 354 case 'registertimeout': 294 case 'registerattempts':295 355 case 'minexpiry': 296 356 case 'maxexpiry': 297 357 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': 298 364 case 'jbmaxsize': 299 365 case 'jbresyncthreshold': 300 // validate-int:301 $save_settings[] = array($key,$db->escapeSimple($v al),'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); 302 368 break; 303 369 304 370 case 'sip_language': 305 // validate-alphanumeric306 $save_settings[] = array($key,$db->escapeSimple($v al),'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); 307 373 break; 308 374 … … 330 396 // ip validate this and store 331 397 $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); 333 400 } else if (substr($key,0,8) == "netmask_") { 334 401 // ip validate this and store 335 402 $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); 337 405 } else if (substr($key,0,15) == "sip_custom_key_") { 338 406 $seq = substr($key,15); … … 345 413 } 346 414 } 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 } 365 440 } 366 441 modules/branches/2.6/sipsettings/page.sipsettings.php
r7832 r7833 20 20 21 21 /* 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(); 26 27 27 28 $action = isset($_POST['action'])?$_POST['action']:''; … … 35 36 $p_idx = 0; 36 37 $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"]); 41 42 $n_idx++; 42 43 } … … 112 113 $p_idx = 0; 113 114 $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"]); 118 119 $n_idx++; 119 120 } … … 124 125 case "edit": //just delete and re-add 125 126 if (($errors = sipsettings_edit($sip_settings)) !== true) { 126 sippsettings_process_errors($errors);127 $error_displays = process_errors($errors); 127 128 } else { 128 129 needreload(); … … 131 132 break; 132 133 default: 134 /* only get them if first time load, if they pressed submit, use values from POST */ 133 135 $sip_settings = sipsettings_get(); 134 136 } … … 140 142 <h2><?php echo _("Edit Settings"); ?></h2> 141 143 <?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 } 142 155 143 156 /* We massaged these above or they came from sipsettings_get() if this is not … … 232 245 </td> 233 246 <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;?>"> 236 249 </td> 237 250 </tr> … … 249 262 </td> 250 263 <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"> / 252 265 END; 253 266 $tabindex++; 254 267 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"> 256 269 </td> 257 270 </tr> … … 712 725 </td> 713 726 <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;?>"> 716 729 </td> 717 730 </tr> … … 729 742 </td> 730 743 <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"> = 732 745 END; 733 746 $tabindex++; 734 747 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"> 736 749 </td> 737 750 </tr> … … 777 790 $.each(data.localnet, function(loc,mask){ 778 791 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); 781 794 } else { 782 795 addLocalnet(loc,mask); … … 846 859 $(".jitter-buffer").hide(); 847 860 }); 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 ?> 848 869 }); 849 870 … … 854 875 var idx = $(".localnet").size(); 855 876 var idxp = idx - 1; 856 var tabindex = parseInt($("#netmask -"+idxp).attr('tabindex')) + 1;877 var tabindex = parseInt($("#netmask_"+idxp).attr('tabindex')) + 1; 857 878 var tabindexp = tabindex + 1; 858 879 … … 862 883 </td>\ 863 884 <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+'">\ 866 887 </td>\ 867 888 </tr>\ … … 873 894 var idx = $(".sip-custom").size(); 874 895 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; 876 897 var tabindexp = tabindex + 1; 877 898 … … 881 902 </td>\ 882 903 <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+'">\ 885 906 </td>\ 886 907 </tr>\ … … 951 972 <?php 952 973 953 function sippsettings_process_errors($errors) { 954 /* TODO: process the array of errors and show issues somewhere */ 974 function 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; 955 982 } 956 983 ?>
