nordcms-lietu /components/CmsBaseRenderer.php

Language PHP Lines 318
MD5 Hash 1f27227af88ea32c48c1fe631e2e9ecf Estimated Cost $4,776 (why?)
Repository https://bitbucket.org/lietu/nordcms-lietu 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?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(
		'block'=>'/{{block:([\w\d\._-]+)}}/i',
		'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]+)\|([\w]+)}}/i',
		'link'=>'/{{(#?[\w\d\._-]+|https?:\/\/[\w\d_-]*(\.[\w\d_-]*)+.*)\|([\w\d\s-]+)}}/i',
		'menu'=>'/{{menu:([\w\d\._-]+)}}/i',
		'url'=>'/{{url:([\w\d\._-]+)}}/i',
	);

	static protected $blockStack = array();

	/**
	 * Renders the given page.
	 * @param CmsPage $page the page to render
	 * @param bool $test If set to false, will handle errors internally, if set to true, will allow exceptions to pass through
	 * @return string the rendered content
	 */
	public function renderPage($page, $test=false)
	{
		$content = $page->body;
		$content = $this->renderURLs($content);
		$content = $this->renderLinks($content);
		$content = $this->renderEmails($content);
		$content = $this->renderImages($content);
		$content = $this->renderAttachments($content);
		$content = $this->renderBlocks($content, $test);
		$content = $this->renderMenus($content);
		return $content;
	}

	/**
	 * Renders the given block.
	 * @param CmsBlock $block the block to render
	 * @param bool $test If set to false, will handle errors internally, if set to true, will allow exceptions to pass through
	 * @return string the rendered content
	 */
	public function renderBlock($block, $test=false)
	{
		// Check for infinite recursion
		if (in_array($block->name, self::$blockStack) === true) {
			throw new CmsRecursionException($block->name, self::$blockStack);
		}

		// Append current block name to the stack list
		self::$blockStack[] = $block->name;

		$content = $block->body;
		$content = $this->renderURLs($content);
		$content = $this->renderLinks($content);
		$content = $this->renderEmails($content);
		$content = $this->renderImages($content);
		$content = $this->renderAttachments($content);
		$content = $this->renderBlocks($content, $test);

		// Pop current block off the stack
		array_pop(self::$blockStack);

		return $content;
	}

	/**
	 * Renders blocks within the given content.
	 * @param string $content the content being rendered
	 * @param bool $test If set to false, will handle errors internally, if set to true, will allow exceptions to pass through
	 * @return string the content
	 */
	protected function renderBlocks($content, $test=false)
	{
		$matches = array();
		preg_match_all($this->_patterns['block'], $content, $matches);

		$pairs = array();
		foreach ($matches[1] as $index => $name)
		{
			/** @var CmsBlock $block */
			$block = Yii::app()->cms->loadBlock($name);

			$blockContent = '';
			if ($block->published) {
				try {
					$blockContent = $block->render($test);
				} catch (CmsRecursionException $e) {
					// If we want to pass through exceptions, rethrow, otherwise ignore
					if ($test)
						throw $e;
				}
			}

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

		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 CmsPage $page */
				$page = $cms->loadPage($target);
				$target = $page instanceof CmsPage ? $page->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 CmsPage $page */
				$page = $cms->loadPage($target);
				$target = $page instanceof CmsPage ? $page->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();
			$cs->registerScriptFile(Yii::app()->cms->getAssetsUrl().'/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 Image $image */
			$image = Yii::app()->image->load($id);
			if ($image instanceof Image)
			{
				$url = $image->getUrl($matches[2][$index]);
				if ($url !== false)
					$pairs[$matches[0][$index]] = CHtml::image($url, $image->name);
			}
		}

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

		return $content;
	}

	/**
	 * Renders attachments within the given content.
	 * @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 block tags within the given content.
	 * @param string $content the content being rendered
	 * @return string the content
	 */
	public function removeBlocks($content)
	{
		return preg_replace($this->_patterns['block'], '', $content);
	}

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

		$pairs = array();
		foreach ($matches[1] as $index => $name)
		{
			/** @var CmsMenu $menu */
			$menu = Yii::app()->cms->loadMenu($name);
			$pairs[$matches[0][$index]] = $menu->render();
		}

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

		return $content;
	}
}
Back to Top