00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 class lastRSS {
00034
00035
00036
00037 var $channeltags = array ('title', 'link', 'description', 'language', 'copyright', 'managingEditor', 'webMaster', 'pubDate', 'lastBuildDate', 'rating', 'docs');
00038 var $itemtags = array('title', 'link', 'description', 'author', 'category', 'comments', 'enclosure', 'guid', 'pubDate', 'source');
00039 var $imagetags = array('title', 'url', 'link', 'width', 'height');
00040 var $textinputtags = array('title', 'description', 'name', 'link');
00041
00042
00043
00044
00045 function Get ($rss_url) {
00046
00047 if ($this->cache_dir != '') {
00048 $cache_file = $this->cache_dir . '/rsscache_' . md5($rss_url);
00049 $timedif = @(time() - filemtime($cache_file));
00050 if ($timedif < $this->cache_time) {
00051
00052 $result = unserialize(join('', file($cache_file)));
00053
00054 if ($result) $result['cached'] = 1;
00055 } else {
00056
00057 $result = $this->Parse($rss_url);
00058 $serialized = serialize($result);
00059 if ($f = @fopen($cache_file, 'w')) {
00060 fwrite ($f, $serialized, strlen($serialized));
00061 fclose($f);
00062 }
00063 if ($result) $result['cached'] = 0;
00064 }
00065 }
00066
00067 else {
00068 $result = $this->Parse($rss_url);
00069 if ($result) $result['cached'] = 0;
00070 }
00071
00072 return $result;
00073 }
00074
00075
00076
00077
00078
00079 function my_preg_match ($pattern, $menu) {
00080 preg_match($pattern, $menu, $out);
00081 return trim($out[1]);
00082 }
00083
00084
00085
00086
00087 function unhtmlentities ($string) {
00088 $trans_tbl = get_html_translation_table (HTML_ENTITIES);
00089 $trans_tbl = array_flip ($trans_tbl);
00090 return strtr ($string, $trans_tbl);
00091 }
00092
00093
00094
00095
00096 function MyConvertEncoding($in_charset, $out_charset, $string) {
00097
00098 if ($this->subs_char) {
00099
00100 $utf = iconv($in_charset, 'UTF-8', $string);
00101 mb_substitute_character($this->subs_char);
00102 return mb_convert_encoding ($utf, $out_charset, 'UTF-8');
00103 } else {
00104
00105 return iconv($in_charset, $out_charset, $string);
00106 }
00107 }
00108
00109
00110
00111
00112
00113 function Parse ($rss_url) {
00114
00115 if ($f = @fopen($rss_url, 'r')) {
00116 $rss_content = '';
00117 while (!feof($f)) {
00118 $rss_content .= fgets($f, 4096);
00119 }
00120 fclose($f);
00121
00122
00123 $result['encoding'] = $this->my_preg_match("'encoding=[\'\"](.*?)[\'\"]'si", $rss_content);
00124
00125
00126 if ($this->cp != '')
00127 $rss_content = $this->MyConvertEncoding($result['encoding'], $this->cp, $rss_content);
00128
00129
00130 preg_match("'<channel.*?>(.*?)</channel>'si", $rss_content, $out_channel);
00131 foreach($this->channeltags as $channeltag)
00132 {
00133 $temp = $this->my_preg_match("'<$channeltag.*?>(.*?)</$channeltag>'si", $out_channel[1]);
00134 if ($temp != '') $result[$channeltag] = $temp;
00135
00136 }
00137
00138
00139 preg_match("'<textinput(|[^>]*[^/])>(.*?)</textinput>'si", $rss_content, $out_textinfo);
00140
00141
00142 if ($out_textinfo[2]) {
00143 foreach($this->textinputtags as $textinputtag) {
00144 $temp = $this->my_preg_match("'<$textinputtag.*?>(.*?)</$textinputtag>'si", $out_textinfo[2]);
00145 if ($temp != '') $result['textinput_'.$textinputtag] = $temp;
00146 }
00147 }
00148
00149 preg_match("'<image.*?>(.*?)</image>'si", $rss_content, $out_imageinfo);
00150 if ($out_imageinfo[1]) {
00151 foreach($this->imagetags as $imagetag) {
00152 $temp = $this->my_preg_match("'<$imagetag.*?>(.*?)</$imagetag>'si", $out_imageinfo[1]);
00153 if ($temp != '') $result['image_'.$imagetag] = $temp;
00154 }
00155 }
00156
00157 preg_match_all("'<item(| .*?)>(.*?)</item>'si", $rss_content, $items);
00158 $rss_items = $items[2];
00159 $result['items_count'] = count($items[1]);
00160 $i = 0;
00161 $result['items'] = array();
00162 foreach($rss_items as $rss_item) {
00163
00164 foreach($this->itemtags as $itemtag)
00165 {
00166 $temp = $this->my_preg_match("'<$itemtag.*?>(.*?)</$itemtag>'si", $rss_item);
00167 if ($temp != '') $result[items][$i][$itemtag] = $temp;
00168 }
00169
00170 if ($result['items'][$i]['description'])
00171 $result['items'][$i]['description'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['description'])));
00172
00173 $i++;
00174 }
00175 return $result;
00176 }
00177 else
00178 {
00179 return False;
00180 }
00181 }
00182 }
00183
00184 ?>