* $geturl = new GetString ('foo.php') * $geturl->addVar('why?','because'); * // Outputs foo.php?why=because" * print $geturl->getURL()'; * * * * @author Aaron DeVore * @license http://creativecommons.org/licenses/by-sa/2.5/ Creative Commons Attribution-ShareAlike 2.5 */ class GetString { var $RELATIVE = 'relative'; var $ABSOLUTE = 'absolute'; /** * Constructor; sets target * * @param string URL of target script * @param string type of URL (relative, absolute) */ function GetString ($target,$type='relative') { $result = $this->setTarget($target, $type); $this->getvars = array(); if (!$result) { return FALSE; } } /** * Add a variable to be included in the URL * * Values will be URL encoded, so don't worry about illegal characters * * @param string name of the variable * @param string value of the variable */ function addVar($name, $value) { $this->getvars[$name] = $value; } /** * Add all name => value variables in an array to the URL * * @param array Variables to be in the URL */ function addVars ($vars) { $this->getvars = array_merge($this->getvars, $vars); } function setTarget($target,$type='relative') { if ($type == 'relative') { $this->type = 'relative'; } elseif ($type == 'absolute') { $this->type = 'relative'; } else { return FALSE; } $this->target = $target; } /** * Get an absolute version of the URL * * @return string URL */ function getAbsoluteURL() { if ($this->type == 'absolute') { // if target is already an absolute URL $url = $this->target; } else { $schema = $_SERVER['SERVER_PORT'] == '443' ? 'https' : 'http'; $host = strlen($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST']:$_SERVER['SERVER_NAME']; $patharray = explode("/",$_SERVER['PHP_SELF']); array_pop($patharray); $path = implode ("/",$patharray); $url = "$schema://{$host}{$path}/{$this->target}"; } if (sizeof($this->getvars) != 0) { $url .= "?"; } $beginning = TRUE; foreach ($this->getvars as $name => $var) { if (!$beginning) { $url .= "&"; } $url .= urlencode($name) . "=" . urlencode($var); $beginning = FALSE; } return $url; } /** * Get an HTML-suitable, absolute version of the URL * * @return string URL */ function getHTMLAbsoluteURL() { return htmlspecialchars($this->getAbsoluteURL()); } /** * Get the fully constructed URL * * @return string URL */ function getURL() { $url = $this->target; if (sizeof($this->getvars) != 0) { $url .= "?"; } $beginning = TRUE; foreach ($this->getvars as $name => $var) { if (!$beginning) { $url .= "&"; } $var = str_replace('+',' ',$var); $url .= urlencode($name) . "=" . urlencode($var); $beginning = FALSE; } return $url; } /** * Get a URL suitable for use in an HTML document * * @return string URL */ function getHTMLURL () { return htmlspecialchars($this->getURL()); } /** * Returns a clone of the object * * Clones are used when multiple URLs must be created, but with some common * variables. * * $me = new GetString('target.php'); * $me->addVar('foo','bar'); * $firstname = $me->getClone(); * $firstname->addVar('somename', 'somevalue'); * print $firstname->getURL(); // target.php?foo=bar&somename=somevalue * $lastname = $me->getClone(); * $lastname->addVar('aname','avalue'); * print $lastname->getURL(); // target.php?foo=bar&aname=avalue * * * @return GetString copy of current object */ function getClone() { $clone = new GetString($this->target, $this->type); $clone->addVars($this->getvars); return $clone; } } ?>