Skip to content

Commit

Permalink
Updating TextHelper to not use create_function(). The create_function…
Browse files Browse the repository at this point in the history
… calls leaked memory and didn't work with the new helper constructors.
  • Loading branch information
markstory committed Aug 11, 2010
1 parent 2d791e1 commit 6a88452
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 14 deletions.
70 changes: 56 additions & 14 deletions cake/libs/view/helpers/text.php
Expand Up @@ -42,6 +42,13 @@
*/
class TextHelper extends AppHelper {

/**
* helpers
*
* @var array
*/
public $helpers = array('Html');

/**
* Highlights a given phrase in a text. You can specify any expression in highlighter that
* may include the \1 expression to include the $phrase found.
Expand Down Expand Up @@ -118,12 +125,50 @@ public function stripLinks($text) {
* @link http://book.cakephp.org/view/1469/Text#autoLinkUrls-1619
*/
public function autoLinkUrls($text, $htmlOptions = array()) {
$options = var_export($htmlOptions, true);
$text = preg_replace_callback('#(?<!href="|">)((?:https?|ftp|nntp)://[^\s<>()]+)#i', create_function('$matches',
'$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], $matches[0],' . $options . ');'), $text);
$this->_linkOptions = $htmlOptions;
$text = preg_replace_callback(
'#(?<!href="|">)((?:https?|ftp|nntp)://[^\s<>()]+)#i',
array(&$this, '_linkBareUrl'),
$text
);
return preg_replace_callback(
'#(?<!href="|">)(?<!http://|https://|ftp://|nntp://)(www\.[^\n\%\ <]+[^<\n\%\,\.\ <])(?<!\))#i',
array(&$this, '_linkUrls'),
$text
);
}

return preg_replace_callback('#(?<!href="|">)(?<!http://|https://|ftp://|nntp://)(www\.[^\n\%\ <]+[^<\n\%\,\.\ <])(?<!\))#i',
create_function('$matches', '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "http://" . $matches[0],' . $options . ');'), $text);
/**
* Links urls that include http://
*
* @param array $matches
* @return string
* @see TextHelper::autoLinkUrls()
*/
private function _linkBareUrl($matches) {
return $this->Html->link($matches[0], $matches[0], $this->_linkOptions);
}

/**
* Links urls missing http://
*
* @param array $matches
* @return string
* @see TextHelper::autoLinkUrls()
*/
private function _linkUrls($matches) {
return $this->Html->link($matches[0], 'http://' . $matches[0], $this->_linkOptions);
}

/**
* Links email addresses
*
* @param array $matches
* @return string
* @see TextHelper::autoLinkUrls()
*/
private function _linkEmails($matches) {
return $this->Html->link($matches[0], 'mailto:' . $matches[0], $this->_linkOptions);
}

/**
Expand All @@ -136,15 +181,12 @@ public function autoLinkUrls($text, $htmlOptions = array()) {
* @link http://book.cakephp.org/view/1469/Text#autoLinkEmails-1618
*/
public function autoLinkEmails($text, $options = array()) {
$linkOptions = 'array(';
foreach ($options as $option => $value) {
$value = var_export($value, true);
$linkOptions .= "'$option' => $value, ";
}
$linkOptions .= ')';

return preg_replace_callback('#([_A-Za-z0-9+-]+(?:\.[_A-Za-z0-9+-]+)*@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*)#',
create_function('$matches', '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "mailto:" . $matches[0],' . $linkOptions . ');'), $text);
$this->_linkOptions = $options;
return preg_replace_callback(
'#([_A-Za-z0-9+-]+(?:\.[_A-Za-z0-9+-]+)*@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*)#',
array(&$this, '_linkEmails'),
$text
);
}

/**
Expand Down
1 change: 1 addition & 0 deletions cake/tests/cases/libs/view/helpers/text.test.php
Expand Up @@ -17,6 +17,7 @@
* @since CakePHP(tm) v 1.2.0.4206
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
App::import('Core', 'View');
App::import('Helper', 'Text');

/**
Expand Down

0 comments on commit 6a88452

Please sign in to comment.