1 <?php
2 $full_id = $_GET['full'];
3 $entry_id = $_GET['id'];
4 $page_id = $_GET['page'];
5 if (!isSet($page_id)) $page_id = 0;
6
7 if (isSet($full_id) and (strtolower($full_id) == 'true'))
8 {
9 outputFullBlog();
10 }
11 elseif (isSet($entry_id))
12 {
13 getEntry($entry_id, $page_id);
14 }
15 else
16 {
17 getPage($page_id);
18 }
19
20
21 function getEntry($entry_id, $page_id)
22 {
23 $xml = simplexml_load_file("blog.xml");
24 $itemCount = count($xml->xpath('/rss/channel/item'));
25 $index = intval($entry_id);
26 $itemIndex = $itemCount - $index;
27 if ( ($itemIndex <= $itemCount) and ($itemIndex > 0) )
28 {
29 $restructure = getHeaders($xml);
30 $restructure .= getItem($xml, $itemIndex);
31 $restructure .= "</channel></rss>";
32 outputBlog($restructure);
33 }
34 else
35 {
36 getPage($page_id);
37 }
38
39 }
40 function getPage($page_id)
41 {
42 $entriesPerPage = 10;
43 $page = intval($page_id);
44 $xml = simplexml_load_file("blog.xml");
45 $itemCount = count($xml->xpath('/rss/channel/item'));
46
47 $start = ($page - 1) * $entriesPerPage;
48 if ($start > $itemCount - 1)
49 {
50 $start = $itemCount - $entriesPerPage;
51 $page = intval($start / $entriesPerPage) + 1;
52 }
53 elseif ($start < 0)
54 {
55 $page = 1;
56 $start = 0;
57 }
58 $startItem = $itemCount - $start - 1;
59 $endItem = ($startItem - 10 < 0) ? -1 : $startItem - 10;
60
61 $restructure = getHeaders($xml);
62 for ($i = $startItem; $i > $endItem; $i--)
63 {
64 $itemIndex = $itemCount - $i;
65 $restructure .= getItem($xml, $itemIndex);
66 }
67 $restructure .= "</channel></rss>";
68
69 $prev = ($start > 0) ?(string) $page - 1 : null;
70 $next = ($endItem >= 0) ?(string) $page + 1 : null;
71 outputBlog($restructure, $prev, $next);
72 }
73 function getHeaders(&$xml)
74 {
75 $title = $xml->xpath('/rss/channel/title');
76 $link = $xml->xpath('/rss/channel/link');
77 $description = $xml->xpath('/rss/channel/description');
78 $language = $xml->xpath('/rss/channel/language');
79 $copyright = $xml->xpath('/rss/channel/copyright');
80 $managingEditor = $xml->xpath('/rss/channel/managingEditor');
81 $webMaster = $xml->xpath('/rss/channel/webMaster');
82 $pubDate = $xml->xpath('/rss/channel/pubDate');
83 $lastBuildDate = $xml->xpath('/rss/channel/lastBuildDate');
84 $category = $xml->xpath('/rss/channel/category');
85 $generator = $xml->xpath('/rss/channel/generator');
86 $docs = $xml->xpath('/rss/channel/docs');
87 $ttl = $xml->xpath('/rss/channel/ttl');
88 $image = $xml->xpath('/rss/channel/image');
89
90
91 $out = "<rss><channel>";
92 $out .= $title[0]->asXML();
93 $out .= $link[0]->asXML();
94 $out .= $description[0]->asXML();
95 $out .= $language[0]->asXML();
96 $out .= $copyright[0]->asXML();
97 $out .= $managingEditor[0]->asXML();
98 $out .= $webMaster[0]->asXML();
99 $out .= $pubDate[0]->asXML();
100 $out .= $lastBuildDate[0]->asXML();
101 $out .= $category[0]->asXML();
102 $out .= $generator[0]->asXML();
103 $out .= $docs[0]->asXML();
104 $out .= $ttl[0]->asXML();
105 $out .= $image[0]->asXML();
106 return $out;
107 }
108 function getItem(&$xml, $itemIndex)
109 {
110 $xpathQuery = '/rss/channel/item['.$itemIndex.']';
111 $item = $xml->xpath($xpathQuery);
112 $out = $item[0]->asXML();
113 return $out;
114 }
115 function outputBlog($restructure, $prev=null, $next=null)
116 {
117 $xml_doc = new DomDocument;
118 $xml_doc->loadXML( $restructure );
119 $xp = new XsltProcessor();
120 $xsl = new DomDocument;
121 $xsl->load('rss2html.xsl');
122 $xp->importStylesheet($xsl);
123 if ($prev != null) $xp->setParameter('', 'prev', $prev);
124 if ($next != null) $xp->setParameter('', 'next', $next);
125 $xp->setParameter('', 'xml', 'blog.xml');
126 if ($html = $xp->transformToXML($xml_doc))
127 {
128 echo $html;
129 }
130 else
131 {
132 trigger_error('XSL transformation failed.', E_USER_ERROR);
133 }
134 }
135 function outputFullBlog()
136 {
137 $xml_doc = new DomDocument;
138 $xml_doc->load('blog.xml');
139
140 $xp = new XsltProcessor();
141 $xsl = new DomDocument;
142 $xsl->load('rss2html.xsl');
143 $xp->importStylesheet($xsl);
144 $xp->setParameter('', 'xml', 'blog.xml');
145 if ($html = $xp->transformToXML($xml_doc)) {
146 header('Content-type: text/html');
147 echo $html;
148 } else {
149 trigger_error('XSL transformation failed.', E_USER_ERROR);
150 }
151 }
152
153 ?>