PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/vendors/php-irc/modules/rss/rss_mod.php

http://github.com/cakephp/cakebot
PHP | 541 lines | 414 code | 78 blank | 49 comment | 95 complexity | 1d12b32294fba4a3095a7f496bcf1b86 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. class rss_mod extends module {
  4. public $title = "RSS Mod";
  5. public $author = "SilverShield";
  6. public $version = "0.2";
  7. public $dontShow = true;
  8. private $rss_masters = array ( // List of Nick allowed to control RSS, no matter their mode.
  9. 'PhpNut');
  10. private $rssDB;
  11. public function init()
  12. {
  13. $this->rssDB = new ini("modules/rss/rss.ini");
  14. }
  15. public function priv_rss($line, $args)
  16. {
  17. $channel = $line['to'];
  18. // Begin list all RSS feeds in the help
  19. $all_sites = $this->rssDB->getSections();
  20. foreach ($all_sites AS $sites)
  21. {
  22. $site_list .= '/' . $sites;
  23. }
  24. $site_list = ltrim($site_list, '/');
  25. $msg_rss_help = "To see the RSS feeds type !rss {" . $site_list . "} [number] [cyr/lat]";
  26. // End list all RSS feeds in the help
  27. if ($args['nargs'] == 0)
  28. {
  29. $this->ircClass->privMsg($channel, $msg_rss_help);
  30. }
  31. else
  32. {
  33. $rss_site_name = strtolower($args['arg1']);
  34. // Begin fix RSS news limits
  35. $rss_num = ( $args['arg2'] == '' ) ? 1 : intval($args['arg2']);
  36. $rss_num = ( $rss_num <= 5 ) ? $rss_num : 5;
  37. $rss_num = ( $rss_num > 0 ) ? $rss_num : 1;
  38. // End fix RSS news limits
  39. // Begin get URL from DB
  40. $rss_site_url = $this->rssDB->getIniVal($rss_site_name, 'URL');
  41. if ($rss_site_url == false || $rss_site_url == '')
  42. {
  43. $this->ircClass->privMsg($channel, "The RSS source you are looking for not found!");
  44. $this->ircClass->privMsg($channel, $msg_rss_help);
  45. }
  46. // End get URL from DB
  47. #$rss = new cafeRSS();
  48. $this->assign('items', $rss_num);
  49. $this->assign('use_cache', 1);
  50. $rss_body = $this->display($rss_site_url);
  51. // Begin fix &amp; and other html ....
  52. $t = $rss_body;
  53. $i = 0;
  54. $html_sym = array(
  55. '&amp;','&quot;','&lt;','&gt;','&nbsp;');
  56. $normal_sym = array(
  57. '&','"','<','>',' ');
  58. while ($i<count($cyr)) {
  59. $t = str_replace($html_sym[$i],$normal_sym[$i],$t);
  60. $i++;
  61. }
  62. $rss_body = $t;
  63. // End fix &amp; and other html ....
  64. // Begin converting cyrilic symbols to latin
  65. if ( strtolower($args['arg2']) == 'lat' || strtolower($args['arg3']) == 'lat' )
  66. {
  67. $t = $rss_body;
  68. $i = 0;
  69. $cyr = array(
  70. '?','Á','Â','?','Ä','?',"?","Ç","?","É",
  71. "?","Ë","?","Í","Î","?","?","?","?","Ó",
  72. "Ô","?","Ö","×","?","?","Ú","Ü","?","ß",
  73. '?','á','â','?','ä','?','?','ç','?','é',
  74. '?','ë','?','í','î','?','?','?','?','ó',
  75. 'ô','?','ö','÷','?','?','ú','ü','?','?');
  76. $lat = array(
  77. 'A','B','V','G','D','E',"J","Z","I","J",
  78. "K","L","M","N","O","P","R","S","T","U",
  79. "F","H","C","CH","SH","SHT","Y","I","IU","IA",
  80. 'a','b','v','g','d','e','j','z','i','j',
  81. 'k','l','m','n','o','p','r','s','t','u',
  82. 'f','h','c','ch','sh','sht','y','i','iu','ia');
  83. while ($i<count($cyr)) {
  84. $t = str_replace($cyr[$i],$lat[$i],$t);
  85. $i++;
  86. }
  87. $rss_body = $t;
  88. }
  89. // End converting cyrilic symbols to latin
  90. // Begin printing RSS lines to the channel
  91. $rss_body = split("\n", $rss_body);
  92. while( list($key, $value)=each($rss_body))
  93. {
  94. $this->ircClass->privMsg($channel, $value);
  95. }
  96. // End printing RSS lines to the channel
  97. }
  98. }
  99. public function priv_add_rss($line, $args)
  100. {
  101. // Begin Is fromNick on the masters list
  102. foreach ($this->rss_masters as $master) {
  103. if ($line['fromNick'] == $master) {
  104. $obey_master = true;
  105. break;
  106. }
  107. }
  108. // End Is fromNick on the masters list
  109. // Begin add RSS if obey master
  110. if ( $obey_master == true ) {
  111. $channel = $line['to'];
  112. if ($args['nargs'] <= 1)
  113. {
  114. $this->ircClass->privMsg($channel, 'To add RSS feed type !addrss {SITENAME} {RSS URL}');
  115. }
  116. else
  117. {
  118. $sitename = strtolower($args['arg1']);
  119. $siteurl = strtolower($args['arg2']);
  120. // Begin add or update RSS
  121. $rss_site_url = $this->rssDB->getIniVal($sitename, 'URL');
  122. if ($rss_site_url == false || $rss_site_url == '')
  123. {
  124. $this->rssDB->setIniVal($sitename, 'URL', $siteurl);
  125. $this->rssDB->writeIni();
  126. $this->ircClass->privMsg($channel, 'The RSS feed ' . $sitename . ' has been added.');
  127. }
  128. else
  129. {
  130. $this->rssDB->setIniVal($sitename, 'URL', $siteurl);
  131. $this->rssDB->writeIni();
  132. $this->ircClass->privMsg($channel, 'The RSS feed for ' . $sitename . ' has been updated.');
  133. }
  134. // End add or update RSS
  135. }
  136. }
  137. // Del add RSS if obey master
  138. }
  139. public function priv_del_rss($line, $args)
  140. {
  141. // Begin Is fromNick on the masters list
  142. foreach ($this->rss_masters as $master) {
  143. if ($line['fromNick'] == $master) {
  144. $obey_master = true;
  145. break;
  146. }
  147. }
  148. // End Is fromNick on the masters list
  149. // Begin del RSS if obey master
  150. if ( $obey_master == true ) {
  151. $channel = $line['to'];
  152. if ($args['nargs'] < 1)
  153. {
  154. $this->ircClass->privMsg($channel, 'To remove RSS feed type !delrss {SITENAME}');
  155. }
  156. else
  157. {
  158. $sitename = strtolower($args['arg1']);
  159. // Begin del RSS if record exist
  160. $rss_site_url = $this->rssDB->getIniVal($sitename, 'URL');
  161. if ($rss_site_url == false || $rss_site_url == '')
  162. {
  163. $this->ircClass->privMsg($channel, 'RSS feed does not exist.');
  164. }
  165. else
  166. {
  167. $this->rssDB->deleteSection($sitename);
  168. $this->rssDB->writeIni();
  169. $this->ircClass->privMsg($channel, 'The RSS feed ' . $sitename . ' has been deleted.');
  170. }
  171. // End del RSS if record exist
  172. }
  173. }
  174. // End del RSS if obey master
  175. }
  176. /*
  177. The code below Based on original CaféRSS 1.5
  178. by Michel Valdrighi Copyright (C) 2002
  179. BarkerJr, magu Copyright (C) 2004
  180. and modified by Grigor Josifov (SilverShield) Copyright (C) 2005
  181. */
  182. var $url;
  183. var $debugtimer;
  184. /* defaut values */
  185. var $items = 'all';
  186. var $template_string = '';
  187. var $template_file = './modules/rss/rss_mod.tpl';
  188. var $use_cache = 1;
  189. var $cache_dir = './modules/rss/cache'; # if you want to cache, chmod a directory 777 and put its name here
  190. var $refresh_time = 900; # in seconds - has no effect if $use_cache = 0;
  191. var $rss_echo = 1;
  192. var $debug = 0;
  193. var $rss_patch = 0; # if set to 1, will fix all titles and descriptions generated by typepad (recommended)
  194. var $feednumber = 0; /* if you use javascript on the template file to open one window per source feed,
  195. use {$rss_feednumber} on the template to track wich source opens in what window.
  196. Confused? check the template to understand...
  197. magu's Note: I use it to open just one window for Wired.com's news, other just for
  198. Slashdot.org's news, etc.
  199. */
  200. /* usage: $this->assign('var','value'); */
  201. function assign($var, $value) {
  202. $this->$var = $value;
  203. }
  204. /* usage: $this->display('url' [, those optional parameters below ]); */
  205. 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', $rss_feednumber = 'blah', $rss_patch = 'blah') {
  206. if ($rss_file == 'blah') { $rss_file = $this->url; }
  207. if ($rss_items == 'blah') { $rss_items = $this->items; }
  208. if ($rss_template_string == 'blah') { $rss_template_string = $this->template_string; }
  209. if ($rss_template_file == 'blah') { $rss_template_file = $this->template_file; }
  210. if ($rss_use_cache == 'blah') { $rss_use_cache = $this->use_cache; }
  211. if ($rss_cache_dir == 'blah') { $rss_cache_dir = $this->cache_dir; }
  212. if ($rss_refresh_time == 'blah') { $rss_refresh_time = $this->refresh_time; }
  213. if ($rss_echo == 'blah') { $rss_echo = $this->echo; }
  214. if ($rss_debug == 'blah') { $rss_debug = $this->debug; }
  215. if ($rss_feednumber == 'blah') { $rss_feednumber = $this->feednumber; }
  216. if ($rss_patch == 'blah') { $rss_patch = $this->rss_patch; }
  217. $rss_cache_file = $rss_cache_dir.'/'.preg_replace('/[^a-zA-Z0-9_\.-]/', '_', $rss_file).'.cache';
  218. if (preg_match('/</', $rss_file)) {
  219. $content = $rss_file;
  220. } else {
  221. /* the secret cache ops, part I */
  222. $isCached = false;
  223. if (($rss_cache_dir != '') && ($rss_use_cache)) {
  224. clearstatcache();
  225. $get_rss = 1;
  226. $cache_rss = 1;
  227. if (file_exists($rss_cache_file) && file_exists($rss_cache_file)) {
  228. if ((time() - filemtime($rss_cache_file)) < $rss_refresh_time) {
  229. $get_rss = 0;
  230. $isCached = true;
  231. }
  232. }
  233. } else {
  234. $get_rss = 1;
  235. $cache_rss = 0;
  236. }
  237. /* opens the RSS file */
  238. $this->timer_start();
  239. if ($get_rss) {
  240. if (file_exists($rss_cache_file) && file_exists($rss_cache_file)) {
  241. $f = fopen("$rss_cache_file", 'r');
  242. $opts = array(
  243. 'http' => array(
  244. 'header' => 'If-Modified-Since: ' . fgets($f) . "\r\n"
  245. )
  246. );
  247. fclose($f);
  248. $context = stream_context_create($opts);
  249. @ $f = fopen($rss_file, 'r', false, $context) or $isCached = true;
  250. }
  251. else
  252. {
  253. @ $f = fopen($rss_file, 'r') or $nocon = true;
  254. if ($nocon)
  255. {
  256. echo '<p><i>(error displaying RSS feed)</i></p>';
  257. return;
  258. }
  259. }
  260. if (!$isCached)
  261. {
  262. $meta = stream_get_meta_data($f);
  263. foreach ($meta['wrapper_data'] as $row)
  264. if (substr($row, 0, 14) == 'Last-Modified:')
  265. {
  266. $c = fopen($rss_cache_file, 'w');
  267. touch($rss_cache_file);
  268. fwrite($c, substr($row, 15));
  269. fclose($c);
  270. break;
  271. }
  272. while (!feof($f))
  273. $content .= fgets($f, 4096);
  274. fclose($f);
  275. }
  276. }
  277. $debugfopentime = $this->timer_stop(0);
  278. if ($isCached)
  279. {
  280. $this->timer_start();
  281. $f = fopen($rss_cache_file, 'r');
  282. $content = fread($f, filesize($rss_cache_file));
  283. fclose($f);
  284. $debugfopencachetime = $this->timer_stop(0);
  285. }
  286. /* the secret cache ops, part II */
  287. if (($cache_rss) && ($rss_use_cache) && (!$isCached)) {
  288. $this->timer_start();
  289. $f = fopen($rss_cache_file, 'w+');
  290. fwrite($f, $content);
  291. fclose($f);
  292. $debugcachetime = $this->timer_stop(0);
  293. } else {
  294. $debugcachetime = 0;
  295. }
  296. }
  297. /* gets RSS channel info and RSS items info */
  298. $this->timer_start();
  299. preg_match_all("'<channel( .*?)?>(.*?)<title>(.*?)</title>(.+?)</channel>'si",$content,$rss_title);
  300. preg_match_all("'<channel( .*?)?>(.*?)<link>(.*?)</link>(.+?)</channel>'si",$content,$rss_link);
  301. preg_match_all("'<channel( .*?)?>(.*?)<description>(.*?)</description>(.*?)</channel>'si",$content,$rss_description);
  302. preg_match_all("'<channel( .*?)?>(.*?)<lastBuildDate>(.*?)</lastBuildDate>(.*?)</channel>'si",$content,$rss_lastBuildDate);
  303. preg_match_all("'<channel( .*?)?>(.*?)<docs>(.*?)</docs>(.*?)</channel>'si",$content,$rss_docs);
  304. preg_match_all("'<channel( .*?)?>(.*?)<managingEditor>(.*?)</managingEditor>(.*?)</channel>'si",$content,$rss_managingEditor);
  305. preg_match_all("'<channel( .*?)?>(.*?)<webMaster>(.*?)</webMaster>(.*?)</channel>'si",$content,$rss_webMaster);
  306. preg_match_all("'<channel( .*?)?>(.*?)<language>(.*?)</language>(.*?)</channel>'si",$content,$rss_language);
  307. preg_match_all("'<image>(.*?)<title>(.*?)</title>(.*?)</image>'si",$content,$rss_image_title);
  308. preg_match_all("'<image>(.*?)<url>(.*?)</url>(.*?)</image>'si",$content,$rss_image_url);
  309. preg_match_all("'<image>(.*?)<link>(.*?)</link>(.*?)</image>'si",$content,$rss_image_link);
  310. preg_match_all("'<item( .*?)?>(.*?)<title>(<!\[CDATA\[)?(.+?)(\]\]>)?</title>(.*?)</item>'si",$content,$rss_item_titles);
  311. preg_match_all("'<item( .*?)?>(.*?)<link>(<!\[CDATA\[)?(.+?.*?)(\]\]>)?</link>(.*?)</item>'si",$content,$rss_item_links);
  312. preg_match_all("'<item( .*?)?>(.*?)<description>(.*?)</description>(.*?)</item>'si",$content,$rss_item_descriptions);
  313. $rss_title = $rss_title[3][0];
  314. $rss_link = $rss_link[3][0];
  315. $rss_description = $rss_description[3][0];
  316. $rss_lastBuildDate = $rss_lastBuildDate[3][0];
  317. $rss_docs = $rss_docs[3][0];
  318. $rss_managingEditor = $rss_managingEditor[3][0];
  319. $rss_webMaster = $rss_webMaster[3][0];
  320. $rss_language = $rss_language[3][0];
  321. $rss_image_title = $rss_image_title[2][0];
  322. $rss_image_url = $rss_image_url[2][0];
  323. $rss_image_link = $rss_image_link[2][0];
  324. $debugparsersstime = $this->timer_stop(0);
  325. /* gets the template */
  326. $this->timer_start();
  327. if (empty($rss_template_string)) {
  328. $f = fopen($rss_template_file,'r');
  329. $rss_template = fread($f, filesize($rss_template_file));
  330. fclose($f);
  331. } else {
  332. $rss_template = $rss_template_string;
  333. }
  334. $debugfopentemplatetime = $this->timer_stop(0);
  335. $rss_template = str_replace('{BOLD}',BOLD, $rss_template);
  336. $rss_template = str_replace('{UNDERLINE}',UNDERLINE, $rss_template);
  337. $rss_template = str_replace('{COLOR}',COLOR, $rss_template);
  338. preg_match_all("'{rss_items}(.+?){/rss_items}'si",$rss_template,$rss_template_loop);
  339. $rss_template_loop = $rss_template_loop[1][0];
  340. $rss_template = str_replace('{rss_items}','',$rss_template);
  341. $rss_template = str_replace('{/rss_items}','',$rss_template);
  342. /* processes the template - rss channel info */
  343. $this->timer_start();
  344. $rss_template = str_replace('{$rss_title}',$rss_title, $rss_template);
  345. $rss_template = str_replace('{$rss_link}',$rss_link, $rss_template);
  346. $rss_template = str_replace('{$rss_description}',$rss_description, $rss_template);
  347. $rss_template = str_replace('{$rss_lastBuildDate}',$rss_lastBuildDate, $rss_template);
  348. $rss_template = str_replace('{$rss_docs}',$rss_docs, $rss_template);
  349. $rss_template = str_replace('{$rss_managingEditor}',$rss_managingEditor, $rss_template);
  350. $rss_template = str_replace('{$rss_webMaster}',$rss_webMaster, $rss_template);
  351. $rss_template = str_replace('{$rss_language}',$rss_language, $rss_template);
  352. /* processes the template - rss image info */
  353. if ($rss_image_url != '') {
  354. $rss_template = str_replace('{rss_image}','',$rss_template);
  355. $rss_template = str_replace('{/rss_image}','',$rss_template);
  356. $rss_template = str_replace('{$rss_image_title}',$rss_image_title, $rss_template);
  357. $rss_template = str_replace('{$rss_image_link}',$rss_image_link, $rss_template);
  358. $rss_template = str_replace('{$rss_image_url}',$rss_image_url, $rss_template);
  359. } else {
  360. $rand = md5(rand(1,5)); /* now there's an ugly hack that I'll have to fix */
  361. $rss_template = preg_replace('/(\015\012)|(\015)|(\012)/', $rand, $rss_template);
  362. $rss_template = preg_replace('/{rss_image}(.*?){\/rss_image}/', '', $rss_template);
  363. $rss_template = preg_replace("/$rand/", "\n", $rss_template);
  364. }
  365. /* processes the template - rss items info */
  366. $rss_template_loop_processed = '';
  367. $k = count($rss_item_titles[4]);
  368. $j = (($rss_items == 'all') || ($rss_items > $k)) ? $k : intval($rss_items);
  369. for ($i = 0; $i<$j; $i++) {
  370. $tmp_template = $rss_template_loop;
  371. $tmp_title = $rss_item_titles[4][$i];
  372. $tmp_link = $rss_item_links[4][$i];
  373. $tmp_description = $rss_item_descriptions[3][$i];
  374. if ($tmp_description == '') {
  375. $tmp_description = '-';
  376. }
  377. if ($tmp_title == '') {
  378. $tmp_title = substr($tmp_description,0,20);
  379. if (strlen($tmp_description) > 20) {
  380. $tmp_title .= '...';
  381. }
  382. }
  383. $tmp_title = $this->patch($tmp_title);
  384. $tmp_description = $this->patch($tmp_description);
  385. $tmp_link = str_replace('&amp;','&',$tmp_link);
  386. $tmp_template = str_replace('{$rss_item_title}',$tmp_title, $tmp_template);
  387. $tmp_template = str_replace('{$rss_item_link}',$tmp_link, $tmp_template);
  388. $tmp_template = str_replace('{$rss_item_description}',$tmp_description, $tmp_template);
  389. $tmp_template = str_replace('{$rss_feednumber}',$rss_feednumber, $tmp_template);
  390. $rss_template_loop_processed .= $tmp_template;
  391. }
  392. $rss_template = str_replace($rss_template_loop, $rss_template_loop_processed, $rss_template);
  393. $debugprocesstemplatetime = $this->timer_stop(0);
  394. clearstatcache();
  395. /* echoes or returns the processed template :) */
  396. if ($rss_echo = 0) {
  397. echo $rss_template;
  398. if ($rss_debug) {
  399. echo '<p>';
  400. echo $debugfopentime.' seconds to load the remote RSS file.<br />';
  401. echo $debugparsersstime.' seconds to parse the RSS.<br />';
  402. echo $debugfopentemplatetime.' seconds to load the template file.<br />';
  403. echo $debugprocesstemplatetime.' seconds to process the template.<br />';
  404. if ($cache_rss) {
  405. echo $debugcachetime.' seconds to cache the parsing+processing.<br />';
  406. }
  407. echo '<br />';
  408. $debugtotaltime = ($debugfopentime+$debugparsersstime+$debugfopentemplatetime+$debugfopentemplatetime+$debugprocesstemplatetime+$debugcachetime);
  409. echo 'Total: '.$debugtotaltime.' seconds.';
  410. echo '</p>';
  411. }
  412. } else {
  413. return $rss_template;
  414. }
  415. }
  416. function timer_start() {
  417. $mtime = microtime();
  418. $mtime = explode(" ",$mtime);
  419. $mtime = $mtime[1] + $mtime[0];
  420. $this->debugtimer = $mtime;
  421. return true;
  422. }
  423. function timer_stop($display=0,$precision=3) {
  424. $mtime = microtime();
  425. $mtime = explode(" ",$mtime);
  426. $mtime = $mtime[1] + $mtime[0];
  427. $this->debugtimer = $mtime - $this->debugtimer;
  428. if ($display)
  429. echo number_format($this->debugtimer,$precision);
  430. return($this->debugtimer);
  431. }
  432. function patch($text) {
  433. $t = $text;
  434. if ($this->rss_patch) {
  435. $i = 0;
  436. $faults = array(
  437. '?§','?Š','??','??','??','??', # fix for typepad RDF files
  438. '??','?&#x2030;','?­', # sadly, they screw up the encoding
  439. ' &amp; ','&amp;iacute;','&apos;', # this is for some strange appearances.. =)
  440. 'á','Á','?','?','â','Â','?','?',
  441. 'é','É','?','?','?','?','ë','Ë',
  442. 'í','Í',
  443. 'ó','Ó','?','?','ô','Ô','?','?',
  444. 'ú','Ú','ü','Ü');
  445. $fixes = array(
  446. '&ccedil;','&eacute;','&ecirc;','&aacute;','&atilde;','&oacute;',
  447. '&otilde;','&Eacute;','&iacute;',
  448. ' & ','&iacute;',"'",
  449. '&aacute;','&Aacute;','&agrave;','&Agrave;','&acirc;','&Acirc;','&atilde;','&Atilde;',
  450. '&eacute;','&Eacute;','&egrave;','&Egrave;','&ecirc;','&Ecirc;','&euml;','&Euml;',
  451. '&iacute;','&Iacute;',
  452. '&oacute;','&Oacute;','&ograve;','&Ograve;','&ocirc;','&Ocirc;','&otilde;','&Otilde;',
  453. '&uacute;','&Uacute;','&uuml;','&Uuml;');
  454. while ($i<count($faults)) {
  455. $t = str_replace($faults[$i],$fixes[$i],$t);
  456. $i++;
  457. }
  458. }
  459. return $t;
  460. }
  461. }
  462. ?>