fixes /components/CmsBaseRenderer.php

Language PHP Lines 280
MD5 Hash 08f7417efe3ecd0e6a66dfe2aa5bb614 Estimated Cost $4,178 (why?)
Repository https://bitbucket.org/tlemmens/fixes View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/**
 * CmsBaseRenderer class file.
 * @author Christoffer Niska <christoffer.niska@nordsoftware.com>
 * @copyright Copyright &copy; 2011, Nord Software Ltd
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @package cms.components
 */

/**
 * Cms renderer base class. All renderers must be extended from this class.
 */
class CmsBaseRenderer extends CComponent
{
	protected $_patterns = array(
		'email'=>'/{{email:([\w\d!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[\w\d!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[\w\d](?:[\w\d-]*[\w\d])?\.)+[\w\d](?:[\w\d-]*[\w\d])?)}}/i',
		'file'=>'/{{file:([\d]+)}}/i',
		'image'=>'/{{image:([\d]+)}}/i',
		'link'=>'/{{(#?[\w\d\._-]+|https?:\/\/[\w\d_-]*(\.[\w\d_-]*)+.*)\|([\w\d\s-]+)}}/i',
		'node'=>'/{{node:([\w\d\._-]+)}}/i',
		'url'=>'/{{url:([\w\d\._-]+)}}/i',
	);

	/**
	 * Renders a specific node.
	 * @param CmsNode $node the node to render
	 * @return string the rendered node
	 */
	public function render($node)
	{
		$heading = str_replace('{heading}', $node->heading, Yii::app()->cms->headingTemplate);
		$content = $this->renderHeading($heading, $node->body);
		$content = $this->renderURLs($content);
		$content = $this->renderLinks($content);
		$content = $this->renderEmails($content);
		$content = $this->renderImages($content);
		$content = $this->renderAttachments($content);
		$content = $this->renderNodes($content);
		return $content;
	}

	/**
	 * Renders a specific node as a widget.
	 * @param CmsNode $node the node to render
	 * @return string the rendered node
	 */
	public function renderWidget($node)
	{
		$heading = str_replace('{heading}', $node->heading, Yii::app()->cms->widgetHeadingTemplate);
		$content = $this->renderHeading($heading, $node->body);
		$content = $this->renderURLs($content);
		$content = $this->renderLinks($content);
		$content = $this->renderEmails($content);
		$content = $this->renderImages($content);
		$content = $this->renderAttachments($content);
		$content = $this->removeNodes($content); // widgets do not render inline nodes
		return $content;
	}

	/**
	 * Renders the heading.
	 * @param string $heading the heading to render
	 * @param string $content the content being rendered
	 * @return string the content
	 */
	protected function renderHeading($heading, $content)
	{
		return str_replace('{{heading}}', $heading, $content);
	}

	/**
	 * Renders nodes within the given content.
	 * @param string $content the content being rendered
	 * @return string the content
	 */
	protected function renderNodes($content)
	{
		$matches = array();
		preg_match_all($this->_patterns['node'], $content, $matches);

		$pairs = array();
		foreach ($matches[1] as $index => $name)
		{
			/** @var CmsNode $node */
			$node = Yii::app()->cms->loadNode($name);
			$pairs[$matches[0][$index]] = $node instanceof CmsNode && $node->level === CmsNode::LEVEL_BLOCK
					? $node->renderWidget() : '';
		}

		if (!empty($pairs))
			$content = strtr($content, $pairs);

		return $content;
	}

	/**
	 * Renders links within the given content.
	 * @param string $content the content being rendered
	 * @return string the content
	 */
	protected function renderLinks($content)
	{
		$matches = array();
		preg_match_all($this->_patterns['link'], $content, $matches);

		$pairs = array();
		foreach ($matches[1] as $index => $target)
		{
			// If the target doesn't include 'http' it's treated as an internal link.
			if (strpos($target, '#') !== 0 && strpos($target, 'http') === false)
			{
				/** @var Cms $cms */
				$cms = Yii::app()->cms;

				/** @var CmsNode $node */
				$node = $cms->loadNode($target);
				$target = $node instanceof CmsNode ? $node->getUrl() : '#';
			}

			$text = $matches[3][$index];
			$pairs[$matches[0][$index]] = CHtml::link($text, $target);
		}

		if (!empty($pairs))
			$content = strtr($content, $pairs);

		return $content;
	}

	/**
	 * Renders URLS within the given content.
	 * @param string $content the content being rendered
	 * @return string the content
	 */
	protected function renderURLs($content)
	{
		$matches = array();
		preg_match_all($this->_patterns['url'], $content, $matches);

		$pairs = array();
		foreach ($matches[1] as $index => $target)
		{
			// If the target doesn't include 'http' it's treated as an internal link.
			if (strpos($target, '#') !== 0 && strpos($target, 'http') === false)
			{
				/** @var Cms $cms */
				$cms = Yii::app()->cms;

				/** @var CmsNode $node */
				$node = $cms->loadNode($target);
				$target = $node !== null ? $node->getUrl() : '#';
			}

			$pairs[$matches[0][$index]] = $target;
		}

		if (!empty($pairs))
			$content = strtr($content, $pairs);

		return $content;
	}

	/**
	 * Renders emails within the given content.
	 * @param string $content the content being rendered
	 * @return string the content
	 */
	protected function renderEmails($content)
	{
		$matches = array();
		preg_match_all($this->_patterns['email'], $content, $matches);

		$pairs = array();
		foreach ($matches[1] as $index => $id)
		{
			$email = str_rot13($matches[1][$index]);
			$pairs[$matches[0][$index]] = CHtml::mailto($email, $email, array('class'=>'email', 'rel'=>'nofollow'));
		}

		if (!empty($pairs))
		{
			$content = strtr($content, $pairs);

			/** @var CClientScript $cs */
			$cs = Yii::app()->getClientScript();

			$assetsUrl = Yii::app()->cms->getAssetsUrl();
			$cs->registerScriptFile($assetsUrl.'/js/cms-rot13.js');
			$cs->registerScript(__CLASS__.'#'.uniqid(true, true).'_emailObfuscation', "
				(function($) {
					$('.email').each(function() {
						var element = $(this);

						if (!element.attr('data-decoded')) {
							var	href = $(this).attr('href'),
								address = Cms.Rot13.decode(href.substring(href.indexOf(':') + 1)),
								value = Cms.Rot13.decode($(this).text());

							element.attr('href', 'mailto:' + address);
							element.text(value);
							element.attr('data-decoded', 1);
						}
					});
				})(jQuery);
			");
		}

		return $content;
	}

	/**
	 * Renders images within the given content.
	 * @param string $content the content being rendered
	 * @return string the content
	 */
	protected function renderImages($content)
	{
		$matches = array();
		preg_match_all($this->_patterns['image'], $content, $matches);

		$pairs = array();
		foreach ($matches[1] as $index => $id)
		{
			/** @var CmsAttachment $attachment */
			$attachment = CmsAttachment::model()->findByPk($id);
			if ($attachment instanceof CmsAttachment && strpos($attachment->mimeType, 'image') !== false)
			{
				$url = $attachment->getUrl();
				$name = $attachment->resolveName();
				$pairs[$matches[0][$index]] = CHtml::image($url, $name);

			}
		}

		if (!empty($pairs) )
			$content = strtr($content, $pairs);

		return $content;
	}

	/**
	 * Renders attachments within this node.
	 * @param string $content the content being rendered
	 * @return string the content
	 */
	protected function renderAttachments($content)
	{
		$matches = array();
		preg_match_all($this->_patterns['file'], $content, $matches);

		$pairs = array();
		foreach ($matches[1] as $index => $id)
		{
			/** @var CmsAttachment $attachment */
			$attachment = CmsAttachment::model()->findByPk($id);
			if ($attachment instanceof CmsAttachment)
			{
				$url = $attachment->getUrl();
				$name = $attachment->resolveName();
				$pairs[$matches[0][$index]] = CHtml::link($name, $url);
			}
		}

		if (!empty($pairs))
			$content = strtr($content, $pairs);

		return $content;
	}

	/**
	 * Removes the node tags within the given content.
	 * @param string $content the content being rendered
	 * @return string the content
	 */
	public function removeNodes($content)
	{
		return preg_replace($this->_patterns['node'], '', $content);
	}
}
Back to Top