Perforce Chronicle 2012.2/486814
API Documentation
|
Modifies urls in certain html tags to insert the current branch base url. More...
Public Member Functions | |
__construct (array $options=null) | |
Sets the filter options: | |
filter ($value) | |
Assuming html input, modify urls in specific attributes of certain tags (default a, img and href, src) to inject the branch base url. | |
setAttributes (array $attributes=null) | |
Control which attributes are affected. | |
setTags (array $tags=null) | |
Control which tags are affected. | |
Protected Member Functions | |
_sanitize ($value) | |
Sanitize the given string (tag or attribute) for use in a regular expression. | |
Protected Attributes | |
$_attributes = array('src', 'href') | |
$_strip = false | |
$_tags = array('img', 'a') |
Modifies urls in certain html tags to insert the current branch base url.
This is needed in some cases to ensure that resources come from the correct branch.
It is possible to control which tags/attributes are affected. By default 'href' and 'src' attributes in 'a' and 'img' tags are 'branchified'.
P4Cms_Filter_BranchifyUrls::__construct | ( | array $ | options = null | ) |
Sets the filter options:
tags - Tags to modify urls in (defaults to: a, img) attributes - Attributes to modify urls in (defaults to: href, src)
array | $options | the options to augment filter behavior. |
{ if (isset($options['tags'])) { $this->setTags($options['tags']); } if (isset($options['attributes'])) { $this->setAttributes($options['attributes']); } }
P4Cms_Filter_BranchifyUrls::_sanitize | ( | $ | value | ) | [protected] |
Sanitize the given string (tag or attribute) for use in a regular expression.
Strips any special characters.
string | $value | the string to sanitize |
{ return preg_replace('/[^a-z0-9\-]/i', '', $value); }
P4Cms_Filter_BranchifyUrls::filter | ( | $ | value | ) |
Assuming html input, modify urls in specific attributes of certain tags (default a, img and href, src) to inject the branch base url.
string | $value | html input to branchify urls in |
{ $request = Zend_Controller_Front::getInstance()->getRequest(); // early exit if the request doesn't support the branch url concept if (!$request instanceof P4Cms_Controller_Request_Http) { return $value; } // if tags or attributes are empty, nothing to do. if (!$this->_tags || !$this->_attributes) { return $value; } $strip = $this->_strip; $tags = implode('|', $this->_tags); $attributes = implode('|', $this->_attributes); $baseUrl = $request->getBaseUrl(); $branchBase = $request->getBranchBaseUrl(); return preg_replace_callback( '/<(' . $tags . ')(\\s+[^>]*)(' . $attributes . ')=([\'"]?)([^>]+)>/i', function($match) use ($baseUrl, $branchBase, $strip) { // 1 = tag (e.g. 'a' or 'img') // 2 = intervening content // 3 = attribute (e.g. 'src' or 'href') // 4 = quote (single quote, double quote or empty) // 5 = link value to end of tag $link = $match[5]; // we only munge urls that are absolute with respect // to the current domain and start with our base-url. if ($link[0] != '/' || ($baseUrl && strpos($link, $baseUrl) !== 0)) { return $match[0]; } // strip off the base url and any leading branch specifier $link = substr($link, strlen($baseUrl)); $link = isset($link[1]) && $link[1] == '-' ? preg_replace('#^/-[^/]+-#', '', $link) : $link; // if not stripping, prepend the link with the active branch base url. $link = $strip ? $baseUrl . $link : $branchBase . $link; return '<' . $match[1] . $match[2] . $match[3] . '=' . $match[4] . $link . '>'; }, $value ); }
P4Cms_Filter_BranchifyUrls::setAttributes | ( | array $ | attributes = null | ) |
Control which attributes are affected.
array | $attributes | list of attributes to branchify. |
{ $this->_attributes = array_filter( (array) $attributes, array($this, '_sanitize') ); return $this; }
P4Cms_Filter_BranchifyUrls::setTags | ( | array $ | tags = null | ) |
Control which tags are affected.
array | $tags | list of tags to branchify. |
{ $this->_tags = array_filter( (array) $tags, array($this, '_sanitize') ); return $this; }
P4Cms_Filter_BranchifyUrls::$_attributes = array('src', 'href') [protected] |
P4Cms_Filter_BranchifyUrls::$_strip = false [protected] |
Reimplemented in P4Cms_Filter_DebranchifyUrls.
P4Cms_Filter_BranchifyUrls::$_tags = array('img', 'a') [protected] |