summaryrefslogtreecommitdiff
path: root/includes/classes/FeedHtmlField.php
blob: a5b37cb9bb49f5071f033db85bd41560db505565 (plain)
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
<?php
namespace Bitweaver\Rss;

/**
 * @package rss
 * An FeedHtmlField describes and generates
 * a feed, item or image html field (probably a description). Output is
 * generated based on $truncSize, $syndicateHtml properties.
 * @author Pascal Van Hecke <feedcreator.class.php@vanhecke.info>
 * @version 1.6
 */
class FeedHtmlField {
	/**
	 * Mandatory attributes of a FeedHtmlField.
	 */
	public $rawFieldContent;

	/**
	 * Optional attributes of a FeedHtmlField.
	 *
	 */
	public $truncSize;
	public $syndicateHtml;

	/**
	 * Creates a new instance of FeedHtmlField.
	 * @param  $string: if given, sets the rawFieldContent property
	 */
	public function __construct($parFieldContent) {
		if ($parFieldContent) {
			$this->rawFieldContent = $parFieldContent;
		}
	}

	/**
	 * Creates the right output, depending on $truncSize, $syndicateHtml properties.
	 * @return string    the formatted field
	 */
	public function output() {
		// when field available and syndicated in html we assume
		// - valid html in $rawFieldContent and we enclose in CDATA tags
		// - no truncation (truncating risks producing invalid html)
		if (!$this->rawFieldContent) {
			$result = "";
		}	elseif ($this->syndicateHtml) {
			// Clean HTML before wrapping in CDATA
			$cleaned = $this->sanitizeHtml( $this->rawFieldContent );
			$result = "<![CDATA[$cleaned]]>";
		} else {
			$result = ( $this->truncSize and is_int( $this->truncSize ) ) ? FeedCreator::iTrunc( htmlspecialchars( $this->rawFieldContent ), $this->truncSize ) : htmlspecialchars( $this->rawFieldContent );
		}
		return $result;
	}

	/**
	* Sanitize HTML for RSS syndication - keep content, strip Bootstrap wrappers
	*/
	private function sanitizeHtml( $html ) {
		// Remove Bootstrap wrapper divs
		$html = preg_replace( '/<div[^>]*class="(container|row|col-[^"]*)"[^>]*>/', '', $html );
		$html = preg_replace( '/<\/div>/', '', $html );
		
		// Keep formatting, images, links; strip everything else
		return preg_replace(
			'/\s+/', 
			' ', 
			strip_tags( $html, '<p><br><img><strong><em><a><blockquote><ul><ol><li>' )
		);
	}
}