-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeed.php
108 lines (103 loc) · 6.15 KB
/
feed.php
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
<?php
// Feed extension, https://github.com/annaesvensson/yellow-feed
class YellowFeed {
const VERSION = "0.9.3";
public $yellow; // access to API
// Handle initialisation
public function onLoad($yellow) {
$this->yellow = $yellow;
$this->yellow->system->setDefault("feedLocation", "/feed/");
$this->yellow->system->setDefault("feedFileXml", "feed.xml");
$this->yellow->system->setDefault("feedPaginationLimit", "30");
$this->yellow->system->setDefault("feedRecentChanges", "auto");
}
// Handle page layout
public function onParsePageLayout($page, $name) {
if ($name=="feed") {
$pages = $this->yellow->content->index();
if ($this->yellow->system->get("feedRecentChanges")!="auto") {
$layouts = preg_split("/\s*,\s*/", $this->yellow->system->get("feedRecentChanges"));
foreach ($pages as $pageFeed) {
$pageFeed->set("feedRecentChanges", in_array($pageFeed->get("layout"), $layouts) ? "show" : "hide");
}
$pages->filter("feedRecentChanges", "show");
}
$pagesFilter = array();
if ($page->isRequest("tag")) {
$pages->filter("tag", $page->getRequest("tag"));
array_push($pagesFilter, $pages->getFilter());
}
if ($page->isRequest("author")) {
$pages->filter("author", $page->getRequest("author"));
array_push($pagesFilter, $pages->getFilter());
}
if ($page->isRequest("language")) {
$pages->filter("language", $page->getRequest("language"));
array_push($pagesFilter, $pages->getFilter());
}
if ($page->isRequest("folder")) {
$pages->match("#/[\d\-\_\.]+".$page->getRequest("folder")."#i", false);
array_push($pagesFilter, ucfirst($page->getRequest("folder")));
}
foreach ($pages as $pageFeed) {
$feedGroup = $pageFeed->get($pageFeed->isExisting("published") ? "published" : "modified");
$pageFeed->set("feedGroup", $feedGroup);
}
$pages->sort("feedGroup", false);
if ($this->isRequestXml($page)) {
$paginationLimit = $this->yellow->system->get("feedPaginationLimit");
if ($paginationLimit==0 || $paginationLimit>100) $paginationLimit = 100;
$pages->limit($paginationLimit);
$title = !is_array_empty($pagesFilter) ? implode(" ", $pagesFilter)." - ".$this->yellow->page->get("sitename") : $this->yellow->page->get("sitename");
$this->yellow->page->setLastModified($pages->getModified());
$this->yellow->page->setHeader("Content-Type", "application/rss+xml; charset=utf-8");
$output = "<?xml version=\"1.0\" encoding=\"utf-8\"\077>\r\n";
$output .= "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\r\n";
$output .= "<channel>\r\n";
$output .= "<title>".htmlspecialchars($title)."</title>\r\n";
$output .= "<link>".$this->yellow->page->scheme."://".$this->yellow->page->address.$this->yellow->page->base."/"."</link>\r\n";
$output .= "<description>".$this->yellow->page->getHtml("description")."</description>\r\n";
$output .= "<language>".$this->yellow->page->getHtml("language")."</language>\r\n";
foreach ($pages as $pageFeed) {
$timestamp = strtotime($pageFeed->get($pageFeed->isExisting("published") ? "published" : "modified"));
$content = $this->yellow->toolbox->createTextDescription($pageFeed->getContentHtml(), 0, false, "<!--more-->", "<a href=\"".$pageFeed->getUrl()."\">".$this->yellow->language->getTextHtml("blogMore")."</a>");
$output .= "<item>\r\n";
$output .= "<title>".$pageFeed->getHtml("title")."</title>\r\n";
$output .= "<link>".$pageFeed->getUrl()."</link>\r\n";
$output .= "<pubDate>".date(DATE_RSS, $timestamp)."</pubDate>\r\n";
$output .= "<guid isPermaLink=\"false\">".$pageFeed->getUrl()."?".$timestamp."</guid>\r\n";
$output .= "<dc:creator>".$pageFeed->getHtml("author")."</dc:creator>\r\n";
$output .= "<description>".$pageFeed->getHtml("description")."</description>\r\n";
$output .= "<content:encoded><![CDATA[".$content."]]></content:encoded>\r\n";
$output .= "</item>\r\n";
}
$output .= "</channel>\r\n";
$output .= "</rss>\r\n";
$this->yellow->page->setOutput($output);
} else {
if (!is_array_empty($pagesFilter)) {
$text = implode(" ", $pagesFilter);
$this->yellow->page->set("titleHeader", $text." - ".$this->yellow->page->get("sitename"));
$this->yellow->page->set("titleContent", $this->yellow->page->get("title").": ".$text);
$this->yellow->page->set("title", $this->yellow->page->get("title").": ".$text);
}
$this->yellow->page->setPages("feed", $pages);
$this->yellow->page->setLastModified($pages->getModified());
}
}
}
// Handle page extra data
public function onParsePageExtra($page, $name) {
$output = null;
if ($name=="header") {
$locationFeed = $this->yellow->system->get("coreServerBase").$this->yellow->system->get("feedLocation");
$locationFeed .= $this->yellow->lookup->normaliseArguments("page:".$this->yellow->system->get("feedFileXml"));
$output = "<link rel=\"alternate\" type=\"application/rss+xml\" href=\"$locationFeed\" title=\"".$this->yellow->page->getHtml("sitename")."\" />\n";
}
return $output;
}
// Check if XML requested
public function isRequestXml($page) {
return $page->getRequest("page")==$this->yellow->system->get("feedFileXml");
}
}