From fe5f1d41372b0871120e6d2b4ebc9e2c1fd62726 Mon Sep 17 00:00:00 2001 From: khoff Date: Sat, 10 Mar 2007 00:11:50 +0000 Subject: [PATCH] Added ut_coord and ut_coordn for FS::svc_broadband. --- FS/FS/Record.pm | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++ FS/FS/svc_broadband.pm | 11 ++----- 2 files changed, 88 insertions(+), 9 deletions(-) diff --git a/FS/FS/Record.pm b/FS/FS/Record.pm index 94cc356e5..e43829417 100644 --- a/FS/FS/Record.pm +++ b/FS/FS/Record.pm @@ -1612,6 +1612,92 @@ sub ut_ipn { } } +=item ut_coord COLUMN [ LOWER [ UPPER ] ] + +Check/untaint coordinates. +Accepts the following forms: +DDD.DDDDD +-DDD.DDDDD +DDD MM.MMM +-DDD MM.MMM +DDD MM SS +-DDD MM SS +DDD MM MMM +-DDD MM MMM + +The "DDD MM SS" and "DDD MM MMM" are potentially ambiguous. +The latter form (that is, the MMM are thousands of minutes) is +assumed if the "MMM" is exactly three digits or two digits > 59. + +To be safe, just use the DDD.DDDDD form. + +If LOWER or UPPER are specified, then the coordinate is checked +for lower and upper bounds, respectively. + +=cut + +sub ut_coord { + + my ($self, $field) = (shift, shift); + + my $lower = shift if scalar(@_); + my $upper = shift if scalar(@_); + my $coord = $self->getfield($field); + my $neg = $coord =~ s/^(-)//; + + my ($d, $m, $s) = (0, 0, 0); + + if ( + (($d) = ($coord =~ /^(\s*\d{1,3}(?:\.\d+)?)\s*$/)) || + (($d, $m) = ($coord =~ /^(\s*\d{1,3})\s+(\d{1,2}(?:\.\d+))\s*$/)) || + (($d, $m, $s) = ($coord =~ /^(\s*\d{1,3})\s+(\d{1,2})\s+(\d{1,3})\s*$/)) + ) { + $s = (((($s =~ /^\d{3}$/) or $s > 59) ? ($s / 1000) : ($s / 60)) / 60); + $m = $m / 60; + if ($m > 59) { + return "Invalid (coordinate with minutes > 59) $field: " + . $self->getfield($field); + } + + $coord = ($neg ? -1 : 1) * sprintf('%.8f', $d + $m + $s); + + if (defined($lower) and ($coord < $lower)) { + return "Invalid (coordinate < $lower) $field: " + . $self->getfield($field);; + } + + if (defined($upper) and ($coord > $upper)) { + return "Invalid (coordinate > $upper) $field: " + . $self->getfield($field);; + } + + $self->setfield($field, $coord); + return ''; + } + + return "Invalid (coordinate) $field: " . $self->getfield($field); + +} + +=item ut_coordn COLUMN [ LOWER [ UPPER ] ] + +Same as ut_coord, except optionally null. + +=cut + +sub ut_coordn { + + my ($self, $field) = (shift, shift); + + if ($self->getfield($field) =~ /^$/) { + return ''; + } else { + return $self->ut_coord($field, @_); + } + +} + + =item ut_domain COLUMN Check/untaint host and domain names. diff --git a/FS/FS/svc_broadband.pm b/FS/FS/svc_broadband.pm index 07821f9f5..473cd5705 100755 --- a/FS/FS/svc_broadband.pm +++ b/FS/FS/svc_broadband.pm @@ -200,8 +200,8 @@ sub check { || $self->ut_ipn('ip_addr') || $self->ut_hexn('mac_addr') || $self->ut_hexn('auth_key') - || $self->ut_sfloatn('latitude') - || $self->ut_sfloatn('longitude') + || $self->ut_coordn('latitude', -90, 90) + || $self->ut_coordn('longitude', -180, 180) || $self->ut_sfloatn('altitude') || $self->ut_textn('vlan_profile') ; @@ -210,13 +210,6 @@ sub check { if($self->speed_up < 0) { return 'speed_up must be positive'; } if($self->speed_down < 0) { return 'speed_down must be positive'; } - if($self->latitude < -90 || $self->latitude > 90) { - return 'latitude must be between -90 and 90'; - } - if($self->longitude < -180 || $self->longitude > 180) { - return 'longitude must be between -180 and 180'; - } - if (not($self->ip_addr) or $self->ip_addr eq '0.0.0.0') { my $next_addr = $self->addr_block->next_free_addr; if ($next_addr) { -- 2.11.0