Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_Filter_BranchifyUrls Class Reference

Modifies urls in certain html tags to insert the current branch base url. More...

Inheritance diagram for P4Cms_Filter_BranchifyUrls:
P4Cms_Filter_DebranchifyUrls

List of all members.

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')

Detailed Description

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'.

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Constructor & Destructor Documentation

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)

Parameters:
array$optionsthe options to augment filter behavior.
    {
        if (isset($options['tags'])) {
            $this->setTags($options['tags']);
        }

        if (isset($options['attributes'])) {
            $this->setAttributes($options['attributes']);
        }
    }

Member Function Documentation

P4Cms_Filter_BranchifyUrls::_sanitize ( value) [protected]

Sanitize the given string (tag or attribute) for use in a regular expression.

Strips any special characters.

Parameters:
string$valuethe string to sanitize
Returns:
string the string sanitized for regex.
    {
        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.

Parameters:
string$valuehtml input to branchify urls in
Returns:
string the filtered value
    {
        $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.

Parameters:
array$attributeslist of attributes to branchify.
Returns:
P4Cms_Filter_BranchifyUrls provides fluent interface
    {
        $this->_attributes = array_filter(
            (array) $attributes,
            array($this, '_sanitize')
        );

        return $this;
    }
P4Cms_Filter_BranchifyUrls::setTags ( array $  tags = null)

Control which tags are affected.

Parameters:
array$tagslist of tags to branchify.
Returns:
P4Cms_Filter_BranchifyUrls provides fluent interface
    {
        $this->_tags = array_filter(
            (array) $tags,
            array($this, '_sanitize')
        );

        return $this;
    }

Member Data Documentation

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]

The documentation for this class was generated from the following file: