Merge branch 'master' of https://github.com/jgoodman/Freeside
[freeside.git] / rt / lib / RT / URI.pm
index facce04..11a9e3e 100644 (file)
@@ -2,7 +2,7 @@
 #
 # COPYRIGHT:
 #
-# This software is Copyright (c) 1996-2011 Best Practical Solutions, LLC
+# This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
 #                                          <sales@bestpractical.com>
 #
 # (Except where explicitly superseded by other copyright notices)
@@ -49,6 +49,7 @@
 package RT::URI;
 
 use strict;
+use warnings;
 use base 'RT::Base';
 
 use RT::URI::base;
@@ -90,13 +91,31 @@ sub new {
     return ($self);
 }
 
+=head2 CanonicalizeURI <URI>
 
+Returns the canonical form of the given URI by calling L</FromURI> and then L</URI>.
+
+If the URI is unparseable by FromURI the passed in URI is simply returned untouched.
+
+=cut
+
+sub CanonicalizeURI {
+    my $self = shift;
+    my $uri  = shift;
+    if ($self->FromURI($uri)) {
+        my $canonical = $self->URI;
+        if ($canonical and $uri ne $canonical) {
+            RT->Logger->debug("Canonicalizing URI '$uri' to '$canonical'");
+            $uri = $canonical;
+        }
+    }
+    return $uri;
+}
 
-# {{{ FromObject
 
 =head2 FromObject <Object>
 
-Given a local object, such as an RT::Ticket or an RT::FM::Article, this routine will return a URI for
+Given a local object, such as an RT::Ticket or an RT::Article, this routine will return a URI for
 the local object
 
 =cut
@@ -109,9 +128,7 @@ sub FromObject {
     return $self->FromURI($obj->URI);
 }
 
-# }}}
 
-# {{{ FromURI
 
 =head2 FromURI <URI>
 
@@ -132,10 +149,11 @@ sub FromURI {
     # Special case: integers passed in as URIs must be ticket ids
     if ($uri =~ /^(\d+)$/) {
        $scheme = "fsck.com-rt";
-    } elsif ($uri =~ /^((?:\w|\.|-)+?):/) {
+    } elsif ($uri =~ /^((?!javascript|data)(?:\w|\.|-)+?):/i) {
        $scheme = $1;
     }
     else {
+        $self->{resolver} = RT::URI::base->new( $self->CurrentUser ); # clear resolver
         $RT::Logger->warning("Could not determine a URI scheme for $uri");
         return (undef);
     }
@@ -156,9 +174,7 @@ sub FromURI {
 
 }
 
-# }}}
 
-# {{{ _GetResolver
 
 =head2 _GetResolver <scheme>
 
@@ -183,14 +199,14 @@ sub _GetResolver {
     if ($resolver) {
         $self->{'resolver'} = $resolver;
     } else {
+        RT->Logger->warning("Failed to create new resolver object for scheme '$scheme': $@")
+            if $@ !~ m{Can't locate RT/URI/\Q$scheme\E};
         $self->{'resolver'} = RT::URI::base->new($self->CurrentUser); 
     }
 
 }
 
-# }}}
 
-# {{{ Scheme
 
 =head2 Scheme
 
@@ -204,8 +220,6 @@ sub Scheme {
     return ($self->Resolver->Scheme);
 
 }
-# }}}
-# {{{ URI
 
 =head2 URI
 
@@ -219,9 +233,7 @@ sub URI {
     return ($self->Resolver->URI);
 
 }
-# }}}
 
-# {{{ Object
 
 =head2 Object
 
@@ -237,9 +249,7 @@ sub Object {
 }
 
 
-# }}}
 
-# {{{ IsLocal
 
 =head2 IsLocal
 
@@ -253,7 +263,6 @@ sub IsLocal {
 }
 
 
-# }}}
 
 =head2 AsHREF