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

/**
 * @package rss
 * HtmlCreator is a FeedCreator that writes an HTML feed file to a specific
 * location, overriding the createFeed method of the parent FeedCreator.
 * The HTML produced can be included over http by scripting languages, or serve
 * as the source for an IFrame.
 * All output by this class is embedded in <div></div> tags to enable formatting
 * using CSS.
 *
 * @author Pascal Van Hecke
 * @since 1.7
 */
class HtmlCreator extends FeedCreator {

	public $contentType = "text/html";

	/**
	 * Contains HTML to be output at the start of the feed's html representation.
	 */
	public $header;

	/**
	 * Contains HTML to be output at the end of the feed's html representation.
	 */
	public $footer;

	/**
	 * Contains HTML to be output between entries. A separator is only used in
	 * case of multiple entries.
	 */
	public $separator;

	/**
	 * Used to prefix the stylenames to make sure they are unique
	 * and do not clash with stylenames on the users' page.
	 */
	public $stylePrefix;

	/**
	 * Determines whether the links open in a new window or not.
	 */
	public $openInNewWindow = true;

	public $imageAlign = "right";

	/**
	 * In case of very simple output you may want to get rid of the style tags,
	 * hence this variable.  There's no equivalent on item level, but of course you can
	 * add strings to it while iterating over the items ($this->stylelessOutput .= ...)
	 * and when it is non-empty, ONLY the styleless output is printed, the rest is ignored
	 * in the function createFeed().
	 */
	public $stylelessOutput = "";

	/**
	 * Writes the HTML.
	 * @return    string    the scripts's complete text
	 */
	public function createFeed() {
		// if there is styleless output, use the content of this variable and ignore the rest
		if ($this->stylelessOutput != "") {
			return $this->stylelessOutput;
		}

		//if no stylePrefix is set, generate it yourself depending on the script name
		if ($this->stylePrefix == "") {
			$this->stylePrefix = str_replace( ".", "_", $this->_generateFilename() ) . "_";
		}

		//set an openInNewWindow_token_to be inserted or not
		if ($this->openInNewWindow) {
			$targetInsert = " target='_blank'";
		}

		// use this array to put the lines in and implode later with "document.write" javascript
		$feedArray = [];
		if ($this->image != null) {
			$imageStr = "<a href='" . $this->image->link . "'" . $targetInsert . ">" .
				"<img src='" . $this->image->url . "' border='0' alt='" .
				FeedCreator::iTrunc( htmlspecialchars( $this->image->title ), 100 ) .
				"' align='" . $this->imageAlign . "' ";
			if ($this->image->width) {
				$imageStr .= " width='" . $this->image->width . "' ";
			}
			if ($this->image->height) {
				$imageStr .= " height='" . $this->image->height . "' ";
			}
			$imageStr .= "/></a>";
			$feedArray[] = $imageStr;
		}

		if ($this->title) {
			$feedArray[] = "<div class='" . $this->stylePrefix . "title'><a href='" . $this->link . "' " . $targetInsert . " class='" . $this->stylePrefix . "title'>" .
				FeedCreator::iTrunc( htmlspecialchars( $this->title ), 100 ) . "</a></div>";
		}
		if ($this->getDescription()) {
			$feedArray[] = "<div class='" . $this->stylePrefix . "description'>" .
				str_replace( "]]>", "", str_replace( "<![CDATA[", "", $this->getDescription() ) ) .
				"</div>";
		}

		if ($this->header) {
			$feedArray[] = "<div class='" . $this->stylePrefix . "header'>" . $this->header . "</div>";
		}

		for ( $i = 0; $i < count( $this->items ); $i++ ) {
			if ($this->separator and $i > 0) {
				$feedArray[] = "<div class='" . $this->stylePrefix . "separator'>" . $this->separator . "</div>";
			}

			if ($this->items[$i]->title) {
				$feedArray[] = ( $this->items[$i]->link ) ? "<div class='" . $this->stylePrefix . "item_title'><a href='" . $this->items[$i]->link . "' class='" . $this->stylePrefix .
					"item_title'" . $targetInsert . ">" . FeedCreator::iTrunc( htmlspecialchars( strip_tags( $this->items[$i]->title ) ), 100 ) .
					"</a></div>" : "<div class='" . $this->stylePrefix . "item_title'>" .
					FeedCreator::iTrunc( htmlspecialchars( strip_tags( $this->items[$i]->title ) ), 100 ) .
					"</div>";
			}
			if ($this->items[$i]->getDescription()) {
				$feedArray[] =
					"<div class='".$this->stylePrefix."item_description'>".
					str_replace("]]>", "", str_replace("<![CDATA[", "", $this->items[$i]->getDescription())).
					"</div>";
			}
		}
		if ($this->footer) {
			$feedArray[] = "<div class='".$this->stylePrefix."footer'>".$this->footer."</div>";
		}

		$feed= "".join($feedArray, "\r\n");
		return $feed;
	}

	/**
	 * Overrrides parent to produce .html extensions
	 *
	 * @return string the feed cache filename
	 * @since 1.4
	 */
	public function _generateFilename() {
		$fileInfo = pathinfo($_SERVER["PHP_SELF"]);
		return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".html";
	}
}