This post has been edited 2 times, last edit by "Andybln" (Jan 15th 2011, 2:29pm)
Location: Berlin-Kaulsdorf
Occupation: Freiberuflerin
web/shop to date Version: shop to date 5 - shop to date 8
|
|
Source code |
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 |
<?php
// Klasse zum Lesen von RSS-Feeds
// (C) 2004 MultiMueller Softwareentwicklung e.K.
// Tom Mueller
// www.multimueller.de
// info@multimueller.de
error_reporting(E_ERROR | E_PARSE);
class RSSFeed {
var $title; // Alle Titel
var $link; // Alle Links
var $description; // Alle Beschreibungen
var $newscount; // Anzahl der Elemente
var $xml; // Plaintext XML Dokument
var $trans_tbl; // HTML-Konvert-Tabelle
var $feedurl=""; // Feed-URL
var $cachefile="cache.txt"; // Lokaler Cache
var $bulletfile; // Bullet-Grafik
var $expire=300; // Refresh-Zeit in Sekunden
var $maxnewscount=-1; // Maximale Anzahl von Einträgen
var $footer=""; // Text unterhalb des Feeds
var $errortext="Error accessing Feed"; // Fehlermeldung
var $showdescription=1; // Description anzeigen
var $sidebarmode=0; // Sidebar-Modus
function RSSFeed() {
// Klassen-Konstruktor
// Die unhtmlentities-Funktion vorbereiten
$this->trans_tbl = get_html_translation_table(HTML_ENTITIES);
$this->trans_tbl = array_flip($this->trans_tbl);
}
function file_is_uptodate() {
// Datei-Zeit checken
if (file_exists($this->cachefile)) {
if (time()-filemtime($this->cachefile)<$this->expire) {
return TRUE;
}
}
return FALSE;
}
function read_file() {
// Cache-File lesen
unset($this->xml);
$handle = fopen($this->cachefile, "r");
if ($handle) {
flock($handle, LOCK_SH);
$this->xml = fread($handle, filesize($this->cachefile));
flock($handle, LOCK_UN);
return TRUE;
} else {
return FALSE;
}
}
function write_file() {
// Cache-File schreiben
$handle = fopen($this->cachefile, "w+");
flock($handle, LOCK_EX);
fwrite($handle, $this->xml);
flock($handle, LOCK_UN);
fclose($handle);
}
function read_url() {
// URL lesen
unset ($this->xml);
$handle = fopen($this->feedurl, 'r');
if ($handle) {
while (!feof($handle)) {
$this->xml .= fread($handle, 128);
}
fclose($handle);
return TRUE;
} else {
return FALSE;
}
}
function find_tag($string, $tag) {
// Tag finden
$tmpval = array();
$preg = "|<$tag.*?>(.*?)</$tag>|s";
preg_match_all($preg, $string, $tags);
foreach ($tags[1] as $tmpcont){
$tmpval[] = $tmpcont;
}
return $tmpval;
}
function unhtmlentities($string) {
// entfernt HTML-Entitites
return strtr($string, $this->trans_tbl);
}
function remove_cdata($text,$removeentities) {
// Die CDATA-Tags entfernen
$text=str_replace("<![CDATA[","",$text);
$text=str_replace("]]>","",$text);
if ($removeentities) {
$text=$this->unhtmlentities($text);
}
return $text;
}
function parse_feed() {
// Den Feed interpretieren
// Alles auf Null
$this->newscount=0;
unset($this->title);
unset($this->link);
unset($this->description);
// Ist es ein UTF-8-Feed?
$utf=preg_match('|<\?xml.*?encoding\s*?=\s*?"\s*?UTF-8\s*?".*?>|i',$this->xml);
// Alle Items finden
$items = $this->find_tag($this->xml, 'item');
// Die Items durchlaufen
foreach ($items as $item) {
$this->newscount++;
// Titel, Link und Beschreibung extrahieren
$title=$this->find_tag($item, 'title');
$link=$this->find_tag($item, 'link');
$description=$this->find_tag($item, 'description');
// In Array schreiben und ggf. UFT-8 transformieren
if ($utf) {
$this->title[$this->newscount] = $this->remove_cdata(utf8_decode($title[0]),true);
} else {
$this->title[$this->newscount] = $this->remove_cdata($title[0],true);
}
if ($utf) {
$this->description[$this->newscount] = $this->remove_cdata(utf8_decode($description[0]),true);
} else {
$this->description[$this->newscount] = $this->remove_cdata($description[0],true);
}
$this->link[$this->newscount] = $this->remove_cdata($link[0],false);
// Hat da einer den Titel vergessen?
if ($this->title[$this->newscount]=="") {
$this->title[$this->newscount]=$this->link[$this->newscount];
}
}
}
function print_html() {
// Als HTML ausgeben
if ($this->sidebarmode) {
for ($i=1;(($i<=$this->newscount) && (($i<=$this->maxnewscount) || ($this->maxnewscount<=0)));$i++) {
echo('<p>');
echo('<a target="_blank" href="'.$this->link[$i].'">'.$this->title[$i].'</a>');
echo('</p>');
if ($this->description[$i] && $this->showdescription) {
echo ('<p>'.$this->description[$i].'<br></p>');
}
}
if ($this->footer) {
echo('<p align="right">'.$this->footer.'</p>');
}
} else {
echo('<table width="100%" border="0" cellpadding="0" cellspacing="0">');
for ($i=1;(($i<=$this->newscount) && (($i<=$this->maxnewscount) || ($this->maxnewscount<=0)));$i++) {
echo('<tr>');
echo('<td><img src="'.$this->bulletfile.'" border="0" alt=""></td>');
echo('<td width="100%">');
echo('<a class="s2d" target="_blank" href="'.$this->link[$i].'">'.$this->title[$i].'</a>');
echo('</td></tr>');
if ($this->description[$i] && $this->showdescription) {
echo ('<tr><td></td><td>');
echo ('<p>'.$this->description[$i].'<br></p>');
echo ('</td></tr>');
}
}
if ($this->footer) {
echo('<tr><td></td><td><p align="right">'.$this->footer.'</p></td></tr>');
}
echo('</table>');
}
}
function process_feed() {
// All in one: Den Feed lesen, cachen, ausgeben
$valid=FALSE;
if ($this->file_is_uptodate()) {
// Cache war nach aktuell
$valid=($this->read_file());
} else {
// Nicht mehr aktuell
if ($this->read_url()) {
// URL lesen und schreiben
$this->write_file();
$valid=TRUE;
}
}
if ($valid) {
// Wenn alles o.k. ausgeben
$this->parse_feed();
$this->print_html();
} else {
echo ("<p>".$this->errortext."</p>");
}
}
}
?>
|
Quoted
Cause:
The ampersand ("&") is a special character in HTML. It marks the beginning of a entity, like " " for a non-breaking space.
In XHTML, a entity must also end with a semicolon (";"), as in " "
Because of this, any time a literal ampersand appears in a document, it must be written as the character entity "&". Ampersands commonly appear in the query string of a URL, and must be expressed as an entity in that context.
