From a121c33a35e2f729d6f7633515ea78073f381039 Mon Sep 17 00:00:00 2001 From: "godzilla80@gmx.net" <> Date: Tue, 18 May 2010 08:15:21 +0000 Subject: Add property SecureConnection to TUrlMappingPattern and related enum TUrlMappingPatternSecureConnection to make it possible to to define wether to use http or https on pattern level --- framework/Web/TUrlMapping.php | 138 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 122 insertions(+), 16 deletions(-) (limited to 'framework/Web/TUrlMapping.php') diff --git a/framework/Web/TUrlMapping.php b/framework/Web/TUrlMapping.php index be3545c7..80cb6be8 100644 --- a/framework/Web/TUrlMapping.php +++ b/framework/Web/TUrlMapping.php @@ -1,6 +1,6 @@ * @link http://www.pradosoft.com/ @@ -254,7 +254,7 @@ class TUrlMapping extends TUrlManager } } } - + private function buildUrlMapping($class, $pattern, $properties, $url) { $pattern=Prado::createComponent($class,$this); @@ -337,7 +337,7 @@ class TUrlMapping extends TUrlManager if($rule->supportCustomUrl($getItems)) return $rule->constructUrl($getItems,$encodeAmpersand,$encodeGetItems); } - } + } elseif(isset($this->_constructRules[$wildCardKey])) { foreach($this->_constructRules[$wildCardKey] as $rule) @@ -409,7 +409,7 @@ class TUrlMapping extends TUrlManager * (the default ID is 'page') set the service parameter and service id respectively. * * Since 3.1.4 you can also use simplyfied wildcard patterns to match multiple - * ServiceParameters with a single rule. The pattern must contain the placeholder + * ServiceParameters with a single rule. The pattern must contain the placeholder * {*} for the ServiceParameter. For example * * @@ -417,24 +417,24 @@ class TUrlMapping extends TUrlManager * This rule will match an URL like http://example.com/index.php/admin/edituser * and resolve it to the page Application.pages.admin.edituser. The wildcard matching * is non-recursive. That means you have to add a rule for every subdirectory you - * want to access pages in: + * want to access pages in: * * * * It is still possible to define an explicit rule for a page in the wildcard path. * This rule has to preceed the wildcard rule. * - * You can also use parameters with wildcard patterns. The parameters are then + * You can also use parameters with wildcard patterns. The parameters are then * available with every matching page: * * * - * To enable automatic parameter encoding in a path format fro wildcard patterns you can set - * {@setUrlFormat UrlFormat} to 'Path': + * To enable automatic parameter encoding in a path format fro wildcard patterns you can set + * {@setUrlFormat UrlFormat} to 'Path': * * * - * This will create and parse URLs of the form + * This will create and parse URLs of the form * .../index.php/admin/listuser/param1/value1/param2/value2. * * Use {@setUrlParamSeparator} to define another separator character between parameter @@ -484,6 +484,12 @@ class TUrlMappingPattern extends TComponent private $_separator='/'; + /** + * @var TUrlMappingPatternSecureConnection + * @since 3.2 + */ + private $_secureConnection = TUrlMappingPatternSecureConnection::Automatic; + /** * Constructor. * @param TUrlManager the URL manager instance @@ -513,7 +519,7 @@ class TUrlMappingPattern extends TComponent { if($this->_serviceParameter===null) throw new TConfigurationException('urlmappingpattern_serviceparameter_required', $this->getPattern()); - if(strpos($this->_serviceParameter,'*')!==false) + if(strpos($this->_serviceParameter,'*')!==false) $this->_isWildCardPattern=true; } @@ -541,7 +547,7 @@ class TUrlMappingPattern extends TComponent $regexp=str_replace($params,$values,trim($this->getPattern(),'/').'/'); if ($this->_urlFormat===THttpRequestUrlFormat::Get) $regexp='/^'.$regexp.'$/u'; - else + else $regexp='/^'.$regexp.'(?P.*)$/u'; if(!$this->getCaseSensitive()) @@ -665,12 +671,12 @@ class TUrlMappingPattern extends TComponent if (isset($matches['urlparams'])) { $params=explode('/',$matches['urlparams']); - if ($this->_separator==='/') + if ($this->_separator==='/') { while($key=array_shift($params)) $matches2[$key]=($value=array_shift($params)) ? $value : ''; - } - else + } + else { array_pop($params); foreach($params as $param) @@ -724,7 +730,7 @@ class TUrlMappingPattern extends TComponent * Sets the format of URLs constructed and interpreted by this pattern. * A Get URL format is like index.php?name1=value1&name2=value2 * while a Path URL format is like index.php/name1/value1/name2/value. - * The separating character between name and value can be configured with + * The separating character between name and value can be configured with * {@link setUrlParamSeparator} and defaults to '/'. * Changing the UrlFormat will affect {@link constructUrl} and how GET variables * are parsed. @@ -756,6 +762,24 @@ class TUrlMappingPattern extends TComponent throw new TInvalidDataValueException('httprequest_separator_invalid'); } + /** + * @return TUrlMappingPatternSecureConnection the SecureConnection behavior. Defaults to {@link TUrlMappingPatternSecureConnection::Automatic Automatic} + * @since 3.2 + */ + public function getSecureConnection() + { + return $this->_secureConnection; + } + + /** + * @param TUrlMappingPatternSecureConnection the SecureConnection behavior. + * @since 3.2 + */ + public function setSecureConnection($value) + { + $this->_secureConnection = TPropertyValue::ensureEnum($value, 'TUrlMappingPatternSecureConnection'); + } + /** * @param array list of GET items to be put in the constructed URL * @return boolean whether this pattern IS the one for constructing the URL with the specified GET items. @@ -836,7 +860,89 @@ class TUrlMappingPattern extends TComponent } $url=$url.'?'.substr($url2,strlen($amp)); } - return $url; + return $this -> applySecureConnectionPrefix($url); + } + + /** + * Apply behavior of {@link SecureConnection} property by conditionaly prefixing + * URL with {@link THttpRequest::getBaseUrl()} + * + * @param string $url + * @return string + * @since 3.2 + */ + protected function applySecureConnectionPrefix($url) + { + static $request; + if($request === null) $request = Prado::getApplication() -> getRequest(); + + static $isSecureConnection; + if($isSecureConnection === null) $isSecureConnection = $request -> getIsSecureConnection(); + + switch($this -> getSecureConnection()) + { + case TUrlMappingPatternSecureConnection::EnableIfNotSecure: + if($isSecureConnection) return $url; + return $request -> getBaseUrl(true) . $url; + break; + case TUrlMappingPatternSecureConnection::DisableIfSecure: + if(!$isSecureConnection) return $url; + return $request -> getBaseUrl(false) . $url; + break; + case TUrlMappingPatternSecureConnection::Enable: + return $request -> getBaseUrl(true) . $url; + break; + case TUrlMappingPatternSecureConnection::Disable: + return $request -> getBaseUrl(false) . $url; + break; + case TUrlMappingPatternSecureConnection::Automatic: + default: + return $url; + break; + } } } +/** + * TUrlMappingPatternSecureConnection class + * + * TUrlMappingPatternSecureConnection defines the enumerable type for the possible SecureConnection + * URL prefix behavior that can be used by {@link TUrlMappingPattern::constructUrl()}. + * + * @author Yves Berkholz + * @version $Id$ + * @package System.Web + * @since 3.2 + */ +class TUrlMappingPatternSecureConnection extends TEnumerable +{ + /** + * Keep current SecureConnection status + * means no prefixing + */ + const Automatic = 'Automatic'; + + /** + * Force use secured connection + * always prefixing with https://example.com/path/to/app + */ + const Enable = 'Enable'; + + /** + * Force use unsecured connection + * always prefixing with http://example.com/path/to/app + */ + const Disable = 'Disable'; + + /** + * Force use secured connection, if in unsecured mode + * prefixing with https://example.com/path/to/app + */ + const EnableIfNotSecure = 'EnableIfNotSecure'; + + /** + * Force use unsecured connection, if in secured mode + * prefixing with https://example.com/path/to/app + */ + const DisableIfSecure = 'DisableIfSecure'; +} -- cgit v1.2.3