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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
<?php
/**
* TfRss class file.
*
* @copyright Simon Wilkinson 2013+ (https://tuskfish.biz)
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html GNU General Public License (GPL) V2
* @author Simon Wilkinson <[email protected]>
* @version Release: 1.0
* @since 1.0
* @package content
*/
// Enable strict type declaration.
declare(strict_types=1);
if (!defined("TFISH_ROOT_PATH")) die("TFISH_ERROR_ROOT_PATH_NOT_DEFINED");
/**
* Generates RSS feeds for the whole site, individual collections and tags.
*
* For information about the RSS 2.0 spec see http://cyber.harvard.edu/rss/rss.html
*
* @copyright Simon Wilkinson 2013+ (https://tuskfish.biz)
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html GNU General Public License (GPL) V2
* @author Simon Wilkinson <[email protected]>
* @version Release: 1.0
* @since 1.0
* @package content
* @uses TfMagicMethods Implementation of magic methods to restrict direct property access.
* @property TfValidator $validator Instance of the Tuskfish data validator class.
* @property string $title Name of channel.
* @property string $link URL to website associated with this channel.
* @property string $description Sentence describing the channel.
* @property string $copyright Copyright license of this channel.
* @property string $managingEditor Email of the editor.
* @property string $webMaster Email of the webmaster.
* @property string $generator Name of software system generating this feed.
* @property string $image Image representing channel.
* @property array $items Array of content objects.
* @property string $template Template for presenting feed, default 'rss'.
*/
class TfRss
{
use TfMagicMethods;
protected $validator;
protected $title;
protected $link;
protected $description;
protected $copyright;
protected $managingEditor;
protected $webMaster;
protected $generator;
protected $items;
protected $template;
/**
* Constructor.
*
* @param TfValidator $validator An instance of the Tuskfish data validator class.
* @param TfPreference $preference An instance of the Tuskfish site preferences class.
*/
public function __construct(TfValidator $validator, TfPreference $preference)
{
if (is_a($validator, 'TfValidator')) {
$this->validator = $validator;
} else {
trigger_error(TFISH_ERROR_NOT_VALIDATOR, E_USER_ERROR);
}
if (!is_a($preference, 'TfPreference')) {
trigger_error(TFISH_ERROR_NOT_PREFERENCE, E_USER_ERROR);
}
$this->setTitle($preference->siteName);
$this->setLink(TFISH_RSS_URL);
$this->setDescription($preference->siteDescription);
$this->setCopyright($preference->siteCopyright);
$this->setManagingEditor($preference->siteEmail);
$this->setWebMaster($preference->siteEmail);
$this->setGenerator('Tuskfish');
$this->setItems(array());
$this->setTemplate('rss');
}
/**
* Make a RSS feed for a collection object.
*
* @param TfCollection $obj A collection object.
*/
public function makeFeedForCollection(TfCollection $obj)
{
if (!is_a($obj, 'TfCollection')) {
trigger_error(TFISH_ERROR_NOT_COLLECTION_OBJECT, E_USER_ERROR);
}
$this->setTitle($obj->title);
$this->setLink(TFISH_RSS_URL . '?id=' . $obj->id);
$this->setDescription($obj->teaser);
}
/**
* Set the copyright notice for this feed.
*
* @param string $copyright Copyright notice.
*/
public function setCopyright(string $copyright)
{
$cleanCopyright = $this->validator->trimString($copyright);
$this->copyright = $cleanCopyright;
}
/**
* Returns the feed copyright notice XSS escaped for display.
*
* @return string Copyright notice.
*/
public function getCopyright()
{
return $this->validator->escapeForXss($this->copyright);
}
/**
* Set the description of this feed.
*
* @param string $description Description of feed.
*/
public function setDescription(string $description)
{
$cleanDescription = $this->validator->trimString($description);
$this->description = $cleanDescription;
}
/**
* Returns the feed description with tags removed and XSS escaped for display.
*
* @return string Description of feed.
*/
public function getDescription()
{
$description = strip_tags($this->description);
return $this->validator->escapeForXss($description);
}
/**
* Set the software generator for this feed.
*
* @param string $generator Name of the software used to generate this feed. For security
* reasons, the generator tag has been removed from the Tuskfish RSS template, but you can
* add it in if you don't mind people knowing what software your site is running.
*/
public function setGenerator(string $generator)
{
$cleanGenerator = $this->validator->trimString($generator);
$this->generator = $cleanGenerator;
}
/**
* Returns the generator of this feed XSS escaped for display.
*
* Not currently in use.
*
* @return string Generator.
*/
public function getGenerator()
{
return $this->validator->escapeForXss($this->generator);
}
/**
* Set the image for this feed (not implemented).
*
* @param string $image Image for this feed.
*/
public function setImage(string $image)
{
// Not implemented.
}
/**
* Set the content/posts for this feed.
*
* @param array $items Items to be included in the feed.
*/
public function setItems(array $items)
{
if ($this->validator->isArray($items)) {
$cleanItems = array();
foreach ($items as $item) {
if (is_object($item)) {
$cleanItems[] = $item;
} else {
trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
}
unset($item);
}
$this->items = $cleanItems;
} else {
trigger_error(TFISH_ERROR_NOT_ARRAY, E_USER_ERROR);
}
}
/**
* Returns an array of content items associated with this feed.
*
* Note that the items are objects and their individual properties need to be XSS escaped
* separately.
*
* @return array Array of content objects associated with this feed.
*/
public function getItems()
{
return $this->items;
}
/**
* Set the base URL for this feed.
*
* Defaults to the value of TFISH_RSS.
*
* @param string $url Base URL for this feed.
*/
public function setLink(string $url)
{
$cleanUrl = $this->validator->trimString($url);
if ($this->validator->isUrl($cleanUrl)) {
$this->link = $cleanUrl;
} else {
trigger_error(TFISH_ERROR_NOT_URL, E_USER_ERROR);
}
}
/**
* Returns the base URL of the feed XSS escaped for display.
*
* @return string Base URL of feed.
*/
public function getLink()
{
return $this->validator->escapeForXss($this->link);
}
/**
* Set the managing editor of this feed.
*
* @param string $email Email address of the managing editor.
*/
public function setManagingEditor(string $email)
{
$cleanEmail = $this->validator->trimString($email);
if ($this->validator->isEmail($cleanEmail)) {
$this->managingEditor = $cleanEmail;
} else {
trigger_error(TFISH_ERROR_NOT_EMAIL, E_USER_ERROR);
}
}
/**
* Returns the managing editor of this feed.
*
* @return string Managing editor.
*/
public function getManagingEditor()
{
return $this->validator->escapeForXss($this->managingEditor);
}
/**
* Set the title of this feed.
*
* @param string $title Title of the feed.
*/
public function setTitle(string $title)
{
$cleanTitle = $this->validator->trimString($title);
$this->title = $cleanTitle;
}
/**
* Returns the title of the feed XSS escaped for display.
*
* @return string Title.
*/
public function getTitle()
{
return $this->validator->escapeForXss($this->title);
}
/**
* Set the template (theme) to display this feed, defaults to 'rss'.
*
* @param string $template Name of the template set.
*/
private function setTemplate(string $template)
{
$cleanTemplate = $this->validator->trimString($template);
$this->template = $cleanTemplate;
}
/**
* Set the webMaster property.
*
* @param string $email email of the webmaster responsible for the feed.
*/
public function setWebMaster(string $email)
{
$cleanEmail = $this->validator->trimString($email);
if ($this->validator->isEmail($cleanEmail)) {
$this->webMaster = $cleanEmail;
} else {
trigger_error(TFISH_ERROR_NOT_EMAIL, E_USER_ERROR);
}
}
/**
* Get the wemaster for this feed XSS escaped for display.
*
* @return string Webmaster.
*/
public function getWebMaster()
{
return $this->validator->escapeForXss($this->webMaster);
}
}