Skip to content
This repository
Browse code

Ensure IRI::get_iri() returns a valid URI

Technically, this is a bad idea. IRI::get_iri() should always return a
valid normalized IRI. By converting it to a URI, this causes it to be
non-normalized. However, since we're only ever using it in a URI
context, this is something I'm willing to live with.

See also 9c28bdc for `master`. Unlike
there, we can't use IRI::get_uri() instead, as that would be a break in
backwards compatibility.

Also see http://core.trac.wordpress.org/ticket/20999 for discussion
  • Loading branch information...
commit 90a771f21985e01cb0133c89631debfa951f6171 1 parent 155ff32
Ryan McCue authored July 03, 2012

Showing 1 changed file with 26 additions and 1 deletion. Show diff stats Hide diff stats

  1. 27  simplepie.inc
27  simplepie.inc
@@ -12486,7 +12486,7 @@ class SimplePie_IRI
12486 12486
 
12487 12487
 		if ($iri !== '')
12488 12488
 		{
12489  
-			return $iri;
  12489
+			return $this->to_uri($iri);
12490 12490
 		}
12491 12491
 		else
12492 12492
 		{
@@ -12495,6 +12495,31 @@ class SimplePie_IRI
12495 12495
 	}
12496 12496
 
12497 12497
 	/**
  12498
+	 * Convert an IRI to a URI (or parts thereof)
  12499
+	 *
  12500
+	 * @return string
  12501
+	 */
  12502
+	function to_uri($string)
  12503
+	{
  12504
+		static $non_ascii;
  12505
+		if (!$non_ascii)
  12506
+		{
  12507
+			$non_ascii = implode('', range("\x80", "\xFF"));
  12508
+		}
  12509
+
  12510
+		$position = 0;
  12511
+		$strlen = strlen($string);
  12512
+		while (($position += strcspn($string, $non_ascii, $position)) < $strlen)
  12513
+		{
  12514
+			$string = substr_replace($string, sprintf('%%%02X', ord($string[$position])), $position, 1);
  12515
+			$position += 3;
  12516
+			$strlen += 2;
  12517
+		}
  12518
+
  12519
+		return $string;
  12520
+	}
  12521
+
  12522
+	/**
12498 12523
 	 * Get the scheme
12499 12524
 	 *
12500 12525
 	 * @access public

0 notes on commit 90a771f

Please sign in to comment.
Something went wrong with that request. Please try again.