diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Control.pm b/scripts/ZoneMinder/lib/ZoneMinder/Control.pm index 57bcd2c3a..90540edfd 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Control.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Control.pm @@ -379,6 +379,59 @@ sub credentials { @$self{'username', 'password'} = @_; } +sub guess_credentials { + my $self = shift; + + require URI; + + # Extract the username/password host/port from ControlAddress + if ($self->{Monitor}{ControlAddress} + and + $self->{Monitor}{ControlAddress} ne 'user:pass@ip' + and + $self->{Monitor}{ControlAddress} ne 'user:port@ip' + ) { + Debug("Using ControlAddress for credentials: $self->{Monitor}{ControlAddress}"); + $uri = URI->new($self->{Monitor}->{ControlAddress}); + $uri = URI->new('http://'.$self->{Monitor}->{ControlAddress}) if ref($uri) eq 'URI::_foreign'; + $$self{host} = $uri->host(); + if ( $uri->userinfo()) { + @$self{'username','password'} = $uri->userinfo() =~ /^(.*):(.*)$/; + } else { + $$self{username} = $self->{Monitor}->{User}; + $$self{password} = $self->{Monitor}->{Pass}; + } + # Check if it is a host and port or just a host + if ( $$self{host} =~ /([^:]+):(.+)/ ) { + $$self{host} = $1; + $$self{port} = $2 ? $2 : $$self{port}; + } + $$self{uri} = $uri; + } elsif ($self->{Monitor}{Path}) { + Debug("Using Path for credentials: $self->{Monitor}{Path}"); + if (($self->{Monitor}->{Path} =~ /^(?(https?|rtsp):\/\/)?(?[^:@]+)?:?(?[^\/@]+)?@(?
[^:\/]+)/)) { + $$self{username} = $+{USERNAME} if $+{USERNAME} and !$$self{username}; + $$self{password} = $+{PASSWORD} if $+{PASSWORD} and !$$self{password}; + $$self{host} = $+{ADDRESS} if $+{ADDRESS}; + } elsif (($self->{Monitor}->{Path} =~ /^(?(https?|rtsp):\/\/)?(?
[^:\/]+)/)) { + $$self{host} = $+{ADDRESS} if $+{ADDRESS}; + $$self{username} = $self->{Monitor}->{User} if $self->{Monitor}->{User} and !$$self{username}; + $$self{password} = $self->{Monitor}->{Pass} if $self->{Monitor}->{Pass} and !$$self{password}; + } else { + $$self{username}= $self->{Monitor}->{User} if $self->{Monitor}->{User} and !$$self{username}; + $$self{password} = $self->{Monitor}->{Pass} if $self->{Monitor}->{Pass} and !$$self{password}; + } + $uri = URI->new($self->{Monitor}->{Path}); + $uri->scheme('http'); + $uri->port(80); + $uri->path(''); + $$self{host} = $uri->host(); + $$self{uri} = $uri; + } else { + Debug('Unable to guess credentials'); + } +} + sub get_realm { my $self = shift; my $url = shift;