Просмотр файла cafeRSS/cafeRSS.php

Размер файла: 9.08Kb
  1. <?php
  2.  
  3. /* CafйRSS 1.0
  4.  
  5. by Michel Valdrighi, on 2002/08/08
  6. licensed under the GPL, see license.txt for information
  7.  
  8. please keep this comment here */
  9.  
  10.  
  11. class cafeRSS {
  12.  
  13. var $url;
  14. var $debugtimer;
  15.  
  16. /* defaut values */
  17. var $items = 'all';
  18. var $template_string = '';
  19. var $template_file = 'cafeRSS_template.txt';
  20. var $use_cache = 0;
  21. var $cache_dir = 'cafeRSS_cache'; # if you want to cache, chmod a directory 777 and put its name here
  22. var $refresh_time = 900; # in seconds - has no effect if $use_cache = 0;
  23. var $echo = 1;
  24. var $debug = 0;
  25.  
  26.  
  27. /* usage: $this->assign('var','value'); */
  28.  
  29. function assign($var, $value) {
  30. $this->$var = $value;
  31. }
  32.  
  33.  
  34. /* usage: $this->display('url' [, those optional parameters below ]); */
  35.  
  36. function display($rss_file = 'blah', $rss_items = 'blah', $rss_template_string = 'blah', $rss_template_file = 'blah', $rss_use_cache= 'blah', $rss_cache_dir = 'blah', $rss_refresh_time = 'blah', $rss_echo = 'blah', $rss_debug = 'blah') {
  37.  
  38. if ($rss_file == 'blah') { $rss_file = $this->url; }
  39. if ($rss_items == 'blah') { $rss_items = $this->items; }
  40. if ($rss_template_string == 'blah') { $rss_template_string = $this->template_string; }
  41. if ($rss_template_file == 'blah') { $rss_template_file = $this->template_file; }
  42. if ($rss_use_cache == 'blah') { $rss_use_cache = $this->use_cache; }
  43. if ($rss_cache_dir == 'blah') { $rss_cache_dir = $this->cache_dir; }
  44. if ($rss_refresh_time == 'blah') { $rss_refresh_time = $this->refresh_time; }
  45. if ($rss_echo == 'blah') { $rss_echo = $this->echo; }
  46. if ($rss_debug == 'blah') { $rss_debug = $this->debug; }
  47.  
  48. $rss_cache_file = $rss_cache_dir.'/'.preg_replace('/[^a-zA-Z0-9_\.-]/', '_', $rss_file).'.cache';
  49.  
  50.  
  51. if (preg_match('/</', $rss_file)) {
  52. $content = $rss_file;
  53. } else {
  54.  
  55.  
  56. /* the secret cache ops, part I */
  57.  
  58. if (($rss_cache_dir != '') && ($rss_use_cache)) {
  59. clearstatcache();
  60. $get_rss = 1;
  61. $cache_rss = 1;
  62. if (file_exists($rss_cache_file)) {
  63. if ((time() - filemtime($rss_cache_file)) < $rss_refresh_time) {
  64. $this->timer_start();
  65. $f = fopen($rss_cache_file, 'r');
  66. $content = fread($f, filesize($rss_cache_file));
  67. fclose($f);
  68. $debugfopencachetime = $this->timer_stop(0);
  69. $get_rss = 0;
  70. }
  71. }
  72. } else {
  73. $get_rss = 1;
  74. $cache_rss = 0;
  75. }
  76.  
  77.  
  78. /* opens the RSS file */
  79.  
  80. $this->timer_start();
  81. if ($get_rss) {
  82. $f = fopen($rss_file,'r');
  83. while (!feof($f)) {
  84. $content .= fgets($f, 4096);
  85. }
  86. fclose($f);
  87. }
  88. $debugfopentime = $this->timer_stop(0);
  89.  
  90.  
  91. /* the secret cache ops, part II */
  92.  
  93. if (($cache_rss) && ($rss_use_cache)) {
  94. $this->timer_start();
  95. $f = fopen($rss_cache_file, 'w+');
  96. fwrite($f, $content);
  97. fclose($f);
  98. $debugcachetime = $this->timer_stop(0);
  99. } else {
  100. $debugcachetime = 0;
  101. }
  102.  
  103. }
  104.  
  105.  
  106. /* gets RSS channel info and RSS items info */
  107.  
  108. $this->timer_start();
  109. preg_match_all("'<channel>(.+?)<title>(.*?)</title>(.+?)</channel>'si",$content,$rss_title);
  110. preg_match_all("'<channel>(.+?)<link>(.*?)</link>(.+?)</channel>'si",$content,$rss_link);
  111. preg_match_all("'<channel>(.+?)<description>(.*?)</description>(.+?)</channel>'si",$content,$rss_description);
  112. preg_match_all("'<channel>(.+?)<lastBuildDate>(.*?)</lastBuildDate>(.+?)</channel>'si",$content,$rss_lastBuildDate);
  113. preg_match_all("'<channel>(.+?)<docs>(.*?)</docs>(.+?)</channel>'si",$content,$rss_docs);
  114. preg_match_all("'<channel>(.+?)<managingEditor>(.*?)</managingEditor>(.+?)</channel>'si",$content,$rss_managingEditor);
  115. preg_match_all("'<channel>(.+?)<webMaster>(.*?)</webMaster>(.+?)</channel>'si",$content,$rss_webMaster);
  116. preg_match_all("'<channel>(.+?)<language>(.*?)</language>(.+?)</channel>'si",$content,$rss_language);
  117. preg_match_all("'<image>(.+?)<title>(.*?)</title>(.+?)</image>'si",$content,$rss_image_title);
  118. preg_match_all("'<image>(.+?)<url>(.*?)</url>(.+?)</image>'si",$content,$rss_image_url);
  119. preg_match_all("'<image>(.+?)<link>(.*?)</link>(.+?)</image>'si",$content,$rss_image_link);
  120. preg_match_all("'<item>(.+?)<title>(.*?)</title>(.+?)</item>'si",$content,$rss_item_titles);
  121. preg_match_all("'<item>(.+?)<link>(.*?)</link>(.+?)</item>'si",$content,$rss_item_links);
  122. preg_match_all("'<item>(.+?)<description>(.*?)</description>(.+?)</item>'si",$content,$rss_item_descriptions);
  123. $rss_title = $rss_title[2][0];
  124. $rss_link = $rss_link[2][0];
  125. $rss_description = $rss_description[2][0];
  126. $rss_lastBuildDate = $rss_lastBuildDate[2][0];
  127. $rss_docs = $rss_docs[2][0];
  128. $rss_managingEditor = $rss_managingEditor[2][0];
  129. $rss_webMaster = $rss_webMaster[2][0];
  130. $rss_language = $rss_language[2][0];
  131. $rss_image_title = $rss_image_title[2][0];
  132. $rss_image_url = $rss_image_url[2][0];
  133. $rss_image_link = $rss_image_link[2][0];
  134. $debugparsersstime = $this->timer_stop(0);
  135.  
  136.  
  137.  
  138. /* gets the template */
  139.  
  140. $this->timer_start();
  141. if (empty($rss_template_string)) {
  142. $f = fopen($rss_template_file,'r');
  143. $rss_template = fread($f, filesize($rss_template_file));
  144. fclose($f);
  145. } else {
  146. $rss_template = $rss_template_string;
  147. }
  148. $debugfopentemplatetime = $this->timer_stop(0);
  149. preg_match_all("'{rss_items}(.+?){/rss_items}'si",$rss_template,$rss_template_loop);
  150. $rss_template_loop = $rss_template_loop[1][0];
  151.  
  152. $rss_template = str_replace('{rss_items}','',$rss_template);
  153. $rss_template = str_replace('{/rss_items}','',$rss_template);
  154.  
  155.  
  156.  
  157. /* processes the template - rss channel info */
  158.  
  159. $this->timer_start();
  160. $rss_template = str_replace('{$rss_title}',$rss_title, $rss_template);
  161. $rss_template = str_replace('{$rss_link}',$rss_link, $rss_template);
  162. $rss_template = str_replace('{$rss_description}',$rss_description, $rss_template);
  163. $rss_template = str_replace('{$rss_lastBuildDate}',$rss_lastBuildDate, $rss_template);
  164. $rss_template = str_replace('{$rss_docs}',$rss_docs, $rss_template);
  165. $rss_template = str_replace('{$rss_managingEditor}',$rss_managingEditor, $rss_template);
  166. $rss_template = str_replace('{$rss_webMaster}',$rss_webMaster, $rss_template);
  167. $rss_template = str_replace('{$rss_language}',$rss_language, $rss_template);
  168.  
  169.  
  170.  
  171. /* processes the template - rss image info */
  172.  
  173. if ($rss_image_url != '') {
  174. $rss_template = str_replace('{rss_image}','',$rss_template);
  175. $rss_template = str_replace('{/rss_image}','',$rss_template);
  176. $rss_template = str_replace('{$rss_image_title}',$rss_image_title, $rss_template);
  177. $rss_template = str_replace('{$rss_image_link}',$rss_image_link, $rss_template);
  178. $rss_template = str_replace('{$rss_image_url}',$rss_image_url, $rss_template);
  179. } else {
  180. $rand = md5(rand(1,5)); /* now there's an ugly hack that I'll have to fix */
  181. $rss_template = preg_replace('/(\015\012)|(\015)|(\012)/', $rand, $rss_template);
  182. $rss_template = preg_replace('/{rss_image}(.*?){\/rss_image}/', '', $rss_template);
  183. $rss_template = preg_replace("/$rand/", "\n", $rss_template);
  184. }
  185.  
  186.  
  187.  
  188. /* processes the template - rss items info */
  189.  
  190. $rss_template_loop_processed = '';
  191. $k = count($rss_item_titles[2]);
  192. $j = (($rss_items == 'all') || ($rss_items > $k)) ? $k : intval($rss_items);
  193. for ($i = 0; $i<$j; $i++) {
  194. $tmp_template = $rss_template_loop;
  195. $tmp_title = $rss_item_titles[2][$i];
  196. $tmp_link = $rss_item_links[2][$i];
  197. $tmp_description = $rss_item_descriptions[2][$i];
  198. if ($tmp_description == '') {
  199. $tmp_description = '-';
  200. }
  201. if ($tmp_title == '') {
  202. $tmp_title = substr($tmp_description,0,20);
  203. if (strlen($tmp_description) > 20) {
  204. $tmp_title .= '...';
  205. }
  206. }
  207. $tmp_template = str_replace('{$rss_item_title}',$tmp_title, $tmp_template);
  208. $tmp_template = str_replace('{$rss_item_link}',$tmp_link, $tmp_template);
  209. $tmp_template = str_replace('{$rss_item_description}',$tmp_description, $tmp_template);
  210. $rss_template_loop_processed .= $tmp_template;
  211. }
  212. $rss_template = str_replace($rss_template_loop, $rss_template_loop_processed, $rss_template);
  213. $debugprocesstemplatetime = $this->timer_stop(0);
  214.  
  215. clearstatcache();
  216. /* echoes or returns the processed template :) */
  217.  
  218. if ($rss_echo) {
  219. echo $rss_template;
  220. if ($rss_debug) {
  221. echo '<p>';
  222. echo $debugfopentime.' seconds to load the remote RSS file.<br />';
  223. echo $debugparsersstime.' seconds to parse the RSS.<br />';
  224. echo $debugfopentemplatetime.' seconds to load the template file.<br />';
  225. echo $debugprocesstemplatetime.' seconds to process the template.<br />';
  226. if ($cache_rss) {
  227. echo $debugcachetime.' seconds to cache the parsing+processing.<br />';
  228. }
  229. echo '<br />';
  230. $debugtotaltime = ($debugfopentime+$debugparsersstime+$debugfopentemplatetime+$debugfopentemplatetime+$debugprocesstemplatetime+$debugcachetime);
  231. echo 'Total: '.$debugtotaltime.' seconds.';
  232. echo '</p>';
  233. }
  234. } else {
  235. return $rss_template;
  236. }
  237.  
  238. }
  239.  
  240. function timer_start() {
  241. $mtime = microtime();
  242. $mtime = explode(" ",$mtime);
  243. $mtime = $mtime[1] + $mtime[0];
  244. $this->debugtimer = $mtime;
  245. return true;
  246. }
  247.  
  248. function timer_stop($display=0,$precision=3) {
  249. $mtime = microtime();
  250. $mtime = explode(" ",$mtime);
  251. $mtime = $mtime[1] + $mtime[0];
  252. $this->debugtimer = $mtime - $this->debugtimer;
  253. if ($display)
  254. echo number_format($this->debugtimer,$precision);
  255. return($this->debugtimer);
  256. }
  257.  
  258. }
  259.  
  260. ?>