1<?php
2
3/**
4 * HTMLCreator is a FeedCreator that writes an HTML feed file to a specific
5 * location, overriding the createFeed method of the parent FeedCreator.
6 * The HTML produced can be included over http by scripting languages, or serve
7 * as the source for an IFrame.
8 * All output by this class is embedded in <div></div> tags to enable formatting
9 * using CSS.
10 *
11 * @author  Pascal Van Hecke
12 * @since   1.7
13 */
14class HTMLCreator extends FeedCreator
15{
16
17    protected $contentType = "text/html";
18
19    /**
20     * Contains HTML to be output at the start of the feed's html representation.
21     */
22    public $header;
23
24    /**
25     * Contains HTML to be output at the end of the feed's html representation.
26     */
27    public $footer;
28
29    /**
30     * Contains HTML to be output between entries. A separator is only used in
31     * case of multiple entries.
32     */
33    public $separator;
34
35    /**
36     * Used to prefix the stylenames to make sure they are unique
37     * and do not clash with stylenames on the user's page.
38     */
39    public $stylePrefix;
40
41    /** @var bool Determines whether the links open in a new window or not. */
42    public $openInNewWindow = true;
43
44    /** @var string image alignments in output */
45    public $imageAlign = "right";
46
47    /**
48     * In case of very simple output you may want to get rid of the style tags,
49     * hence this variable.  There's no equivalent on item level, but of course you can
50     * add strings to it while iterating over the items ($this->stylelessOutput .= ...)
51     * and when it is non-empty, ONLY the styleless output is printed, the rest is ignored
52     * in the function createFeed().
53     */
54    public $stylelessOutput = "";
55
56    /**
57     * Writes the HTML.
58     *
59     * @inheritdoc
60     */
61    public function createFeed()
62    {
63        // if there is styleless output, use the content of this variable and ignore the rest
64        if ($this->stylelessOutput != "") {
65            return $this->stylelessOutput;
66        }
67
68        //if no stylePrefix is set, generate it yourself depending on the script name
69        if ($this->stylePrefix == "") {
70            $this->stylePrefix = str_replace(".", "_", $this->_generateFilename())."_";
71        }
72
73        //set an openInNewWindow_token_to be inserted or not
74        if ($this->openInNewWindow) {
75            $targetInsert = " class='target_blank'";
76        } else {
77            $targetInsert = '';
78        }
79
80        // use this array to put the lines in and implode later with "document.write" javascript
81        $feedArray = array();
82        if ($this->image != null) {
83            $imageStr = "<a href='".$this->image->link."'".$targetInsert.">".
84                "<img src='".$this->image->url."' border='0' alt='".
85                FeedCreator::iTrunc(htmlspecialchars($this->image->title), 100).
86                "' align='".$this->imageAlign."' ";
87            if ($this->image->width) {
88                $imageStr .= " width='".$this->image->width."' ";
89            }
90            if ($this->image->height) {
91                $imageStr .= " height='".$this->image->height."' ";
92            }
93            $imageStr .= "/></a>";
94            $feedArray[] = $imageStr;
95        }
96
97        if ($this->title) {
98            $feedArray[] = "<div class='".$this->stylePrefix."title'><a href='".$this->link."' ".$targetInsert." class='".$this->stylePrefix."title'>".
99                FeedCreator::iTrunc(htmlspecialchars($this->title), 100)."</a></div>";
100        }
101        if ($this->getDescription()) {
102            $feedArray[] = "<div class='".$this->stylePrefix."description'>".
103                str_replace("]]>", "", str_replace("<![CDATA[", "", $this->getDescription())).
104                "</div>";
105        }
106
107        if ($this->header) {
108            $feedArray[] = "<div class='".$this->stylePrefix."header'>".$this->header."</div>";
109        }
110
111        for ($i = 0; $i < count($this->items); $i++) {
112            if ($this->separator and $i > 0) {
113                $feedArray[] = "<div class='".$this->stylePrefix."separator'>".$this->separator."</div>";
114            }
115
116            if ($this->items[$i]->title) {
117                if ($this->items[$i]->link) {
118                    $feedArray[] =
119                        "<div class='".$this->stylePrefix."item_title'><a href='".$this->items[$i]->link."' class='".$this->stylePrefix.
120                        "item_title'".$targetInsert.">".FeedCreator::iTrunc(
121                            htmlspecialchars(strip_tags($this->items[$i]->title)),
122                            100
123                        ).
124                        "</a></div>";
125                } else {
126                    $feedArray[] =
127                        "<div class='".$this->stylePrefix."item_title'>".
128                        FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)), 100).
129                        "</div>";
130                }
131            }
132            if ($this->items[$i]->getDescription()) {
133                $feedArray[] =
134                    "<div class='".$this->stylePrefix."item_description'>".
135                    str_replace("]]>", "", str_replace("<![CDATA[", "", $this->items[$i]->getDescription())).
136                    "</div>";
137            }
138        }
139        if ($this->footer) {
140            $feedArray[] = "<div class='".$this->stylePrefix."footer'>".$this->footer."</div>";
141        }
142
143        $feed = "".implode("\r\n", $feedArray);
144
145        return $feed;
146    }
147
148    /**
149     * Overrides parent to produce .html extensions
150     *
151     * @return string the feed cache filename
152     * @since  1.4
153     * @access private
154     */
155    protected function _generateFilename()
156    {
157        $fileInfo = pathinfo($_SERVER["SCRIPT_NAME"]);
158
159        return substr($fileInfo["basename"], 0, -(strlen($fileInfo["extension"]) + 1)).".html";
160    }
161}
162