From 16cba8fdc8c7b8dc8934c73697b152886b0d7828 Mon Sep 17 00:00:00 2001 From: danij Date: Fri, 15 Jun 2012 18:49:47 +0100 Subject: [PATCH] Homepage|Url: Fixed Url parsing and added setScheme() and setHost() methods --- web/classes/url.class.php | 37 +++++++++++++++++++++++++++++++--- web/includes/utilities.inc.php | 12 ++++++++++- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/web/classes/url.class.php b/web/classes/url.class.php index 8a91254512..737d84d93f 100644 --- a/web/classes/url.class.php +++ b/web/classes/url.class.php @@ -65,7 +65,7 @@ public function __construct($str=NULL) private function parse($str) { - $u = parse_url($str); + $u = parse_url(trim($str)); if(isset($u['scheme'])) $this->scheme = $u['scheme']; @@ -106,6 +106,37 @@ private function parse($str) $this->args = $a; } + public function setScheme($scheme='') + { + if(!strcasecmp(gettype($scheme), 'string') && strlen($scheme)) + { + $this->scheme = $scheme; + } + else + { + unset($this->scheme); + } + return $this; + } + + public function &scheme() + { + return $this->scheme; + } + + public function setHost($host='') + { + if(!strcasecmp(gettype($host), 'string') && strlen($host)) + { + $this->host = $host; + } + else + { + unset($this->host); + } + return $this; + } + public function &host() { return $this->host; @@ -126,7 +157,7 @@ public function &args() * * [scheme]://[user]:[pass]@[host]/[path]?[query]#[fragment] */ - public function toString($sep, $encode=true) + public function toString($sep='&', $encode=true) { $str = ''; if(isset($this->scheme)) @@ -150,7 +181,7 @@ public function toString($sep, $encode=true) { $str .= $this->path; - if(isset($this->args)) + if(isset($this->args) && count($this->args)) { if($encode) { diff --git a/web/includes/utilities.inc.php b/web/includes/utilities.inc.php index df1f12ed25..c4b1b66115 100644 --- a/web/includes/utilities.inc.php +++ b/web/includes/utilities.inc.php @@ -23,6 +23,8 @@ * @author Copyright © 2009-2012 Daniel Swanson */ +require_once(DIR_CLASSES.'/url.class.php'); + includeGuard('Utils'); function checkImagePath($path, $formats) @@ -230,7 +232,15 @@ function json_encode_clean(&$array, $flags=0, $indent_level=0) function generateHyperlinkHTML($uri, $maxLength=40, $cssClass=NULL) { - $uri = strval($uri); + if($uri instanceof Url) + { + $uri = $uri->toString(); + } + else + { + $uri = strval($uri); + } + $maxLength = (integer)$maxLength; if($maxLength < 0) $maxLength = 0; if(!is_null($cssClass))