inlineRSS version 1.1 by Cal Demaine. Additional code to modify feeds from ISO-8859-1 to UTF-8 charset by Peter B. dge-inlinerss-utf-8 Version: 0.93 Author: Dave E Author URI: http://dave.stufftoread.net/ */ /** * This is the body of the plugin. It does all the real work. It's the * method to call from other php functions. */ function DGE_InlineRSS($name, $url='', $options=array(), $xsltp=array()) { $maxage = 60 * 60 * 24; // Maximum file age is 1 day before an error is thrown error_reporting(E_ERROR); // Fetch some settings from the db $xsltpath = get_option('dge_irss_xsltpath'); $cachepath = get_option('dge_irss_cachepath'); $cacheprefix = get_option('dge_irss_cacheprefix'); $processAsHTML = 0; // As a shortcut, when no url is passed in, use the name to // fetch a preset. if ($url == '') $presetname = $name; // otherwise, look for explicit preset call elseif (array_key_exists('preset', $options)) $presetname = $options['preset']; if (isset($presetname)) { if (($presets = get_option('dge_irss_presets')) && ($preset = $presets[$presetname])) { if ($url == '') $url = $preset['url']; $options = array_merge($preset['options'], $options); $xsltp = array_merge($preset['xsltp'], $xsltp); } // else // { // return "\n"; // } } if (array_key_exists('timeout', $options)) $timeout = $options['timeout']; else $timeout = get_option('dge_irss_def_timeout'); if (array_key_exists('xslt', $options)) $xslt = $options['xslt']; if (array_key_exists('xml', $options)) $xml = $options['xml']; if (array_key_exists('html', $options)) $processAsHTML = $options['html']; // ------------------------------------------------------------ // Ok, ready to go. // ------------------------------------------------------------ $cachefile = ABSPATH . "$cachepath/$cacheprefix$name.xml"; if (strlen($xml) > 0) { $age = 0; $exists = TRUE; } else if (file_exists($cachefile)) { // We have a local copy, get it just in case $xml = file_get_contents($cachefile); // And check its age $age = time() - filectime($cachefile); $exists = TRUE; } else { $age = 0; $exists = FALSE; $xml = ""; } if ( $exists == FALSE or $age > $timeout * 60 ) // If there's no file, or it's old { // revised to now use CURL because, well, it's a tiny bit // safer. if (function_exists('curl_init') and stristr($url,"http:")) { $curl_handle=curl_init(); curl_setopt($curl_handle,CURLOPT_URL,$url); curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,10); curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); curl_setopt($curl_handle,CURLOPT_HTTPHEADER,array("Accept-Language: en-us,en;q=0.5")); $curlxml = curl_exec($curl_handle); curl_close($curl_handle); if (empty($curlxml)) { if ($age > $maxage or $exists == FALSE) { return "\n"; } $writefile = FALSE; } else { $xml = $curlxml; $writefile = TRUE; } } else { // If CURL is giving you problems, use the line below instead. $filegetxml = file_get_contents($url); if (empty($filegetxml)) { if ($age > $maxage or $exists == FALSE) { return "\n"; } $writefile = FALSE; } else { $xml = $filegetxml; $writefile = TRUE; } } if ($writefile) { //--------------------------------Begin additional code: modify feeds from ISO-8859-1 to UTF-8 charset Peter B. function utf8dec($s_String) { $s_String = html_entity_decode(htmlentities($s_String." ", ENT_COMPAT, 'UTF-8')); return substr($s_String, 0, strlen($s_String)-1); } function isodec($s_String) { $s_String = html_entity_decode(htmlentities($s_String." ", ENT_COMPAT, 'ISO-8859-1')); return substr($s_String, 0, strlen($s_String)-1); } if (eregi('encoding="ISO-8859-1"', $xml)) { isodec($xml); $xml=eregi_replace('encoding="ISO-8859-1"','encoding="UTF-8"',$xml); $umlaute = array(' \"','–','—','‘','’','“','”','„','•','…' ,'€' ,'‚' ,'ƒ' ,'„' ,'…' ,'†' ,'‡' ,'ˆ' ,'‰' ,'Š' ,'‹' ,'Œ' ,'Ž' ,'‘' ,'’' ,'“' ,'”' ,'•' ,'–' ,'—' ,'˜' ,'™' ,'š' ,'›','œ','ž','Ÿ','¡','¢','£','¤','¥','¦','§','¨','©','ª','«','¬','®','¯','°','±','²','³','´','µ','¶','·','¸','¹','º','»','¼','½','¾','¿','À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','×','Ø','Ù','Ú','Û','Ü','Ý','Þ','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô','õ','ö','÷','ø','ù','ú','û','ü','ý','þ','ÿ',utf8_encode('€'),utf8_encode('‚'),utf8_encode('ƒ'),utf8_encode('„'),utf8_encode('…'),utf8_encode('†'),utf8_encode('‡'),utf8_encode('ˆ'),utf8_encode('‰'),utf8_encode('Š'),utf8_encode('‹'),utf8_encode('Œ'),utf8_encode('Ž'),utf8_encode('‘'),utf8_encode('’'),utf8_encode('“'),utf8_encode('”'),utf8_encode('•'),utf8_encode('–'),utf8_encode('—'),utf8_encode('˜'),utf8_encode('™'),utf8_encode('š'),utf8_encode('›'),utf8_encode('œ'),utf8_encode('ž'),utf8_encode('Ÿ'),utf8_encode('¡'),utf8_encode('¢'),utf8_encode('£'),utf8_encode('¤'),utf8_encode('¥'),utf8_encode('¦'),utf8_encode('§'),utf8_encode('¨'),utf8_encode('©'),utf8_encode('ª'),utf8_encode('«'),utf8_encode('¬'),utf8_encode('®'),utf8_encode('¯'),utf8_encode('°'),utf8_encode('±'),utf8_encode('²'),utf8_encode('³'),utf8_encode('´'),utf8_encode('µ'),utf8_encode('¶'),utf8_encode('·'),utf8_encode('¸'),utf8_encode('¹'),utf8_encode('º'),utf8_encode('»'),utf8_encode('¼'),utf8_encode('½'),utf8_encode('¾'),utf8_encode('¿'),utf8_encode('À'),utf8_encode('Á'),utf8_encode('Â'),utf8_encode('Ã'),utf8_encode('Ä'),utf8_encode('Å'),utf8_encode('Æ'),utf8_encode('Ç'),utf8_encode('È'),utf8_encode('É'),utf8_encode('Ê'),utf8_encode('Ë'),utf8_encode('Ì'),utf8_encode('Í'),utf8_encode('Î'),utf8_encode('Ï'),utf8_encode('Ð'),utf8_encode('Ñ'),utf8_encode('Ò'),utf8_encode('Ó'),utf8_encode('Ô'),utf8_encode('Õ'),utf8_encode('Ö'),utf8_encode('×'),utf8_encode('Ø'),utf8_encode('Ù'),utf8_encode('Ú'),utf8_encode('Û'),utf8_encode('Ü'),utf8_encode('Ý'),utf8_encode('Þ'),utf8_encode('ß'),utf8_encode('à'),utf8_encode('á'),utf8_encode('â'),utf8_encode('ã'),utf8_encode('ä'),utf8_encode('å'),utf8_encode('æ'),utf8_encode('ç'),utf8_encode('è'),utf8_encode('é'),utf8_encode('ê'),utf8_encode('ë'),utf8_encode('ì'),utf8_encode('í'),utf8_encode('î'),utf8_encode('ï'),utf8_encode('ð'),utf8_encode('ñ'),utf8_encode('ò'),utf8_encode('ó'),utf8_encode('ô'),utf8_encode('õ'),utf8_encode('ö'),utf8_encode('÷'),utf8_encode('ø'),utf8_encode('ù'),utf8_encode('ú'),utf8_encode('û'),utf8_encode('ü'),utf8_encode('ý'),utf8_encode('þ'),utf8_encode('ÿ'),chr(128),chr(129),chr(130),chr(131),chr(132),chr(133),chr(134),chr(135),chr(136),chr(137),chr(138),chr(139),chr(140),chr(141),chr(142),chr(143),chr(144),chr(145),chr(146),chr(147),chr(148),chr(149),chr(150),chr(151),chr(152),chr(153),chr(154),chr(155),chr(156),chr(157),chr(158),chr(159),chr(160),chr(161),chr(162),chr(163),chr(164),chr(165),chr(166),chr(167),chr(168),chr(169),chr(170),chr(171),chr(172),chr(173),chr(174),chr(175),chr(176),chr(177),chr(178),chr(179),chr(180),chr(181),chr(182),chr(183),chr(184),chr(185),chr(186),chr(187),chr(188),chr(189),chr(190),chr(191),chr(192),chr(193),chr(194),chr(195),chr(196),chr(197),chr(198),chr(199),chr(200),chr(201),chr(202),chr(203),chr(204),chr(205),chr(206),chr(207),chr(208),chr(209),chr(210),chr(211),chr(212),chr(213),chr(214),chr(215),chr(216),chr(217),chr(218),chr(219),chr(220),chr(221),chr(222),chr(223),chr(224),chr(225),chr(226),chr(227),chr(228),chr(229),chr(230),chr(231),chr(232),chr(233),chr(234),chr(235),chr(236),chr(237),chr(238),chr(239),chr(240),chr(241),chr(242),chr(243),chr(244),chr(245),chr(246),chr(247),chr(248),chr(249),chr(250),chr(251),chr(252),chr(253),chr(254),chr(255),chr(256)); $htmlcode = array(' "','–','—','‘','’','“','”','„','•' ,'…','€','‚','ƒ','„','…','†','‡','ˆ','‰','Š','‹','Œ','Ž','‘','’','“','”','•','–','—','˜','™','š','›','œ','ž','Ÿ','¡','¢','£','¤','¥','¦','§','¨','©','ª','«','¬','®','¯','°','±','²','³','´','µ','¶','·','¸','&supl;','º','»','¼','½','¾','¿','À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','×','Ø','Ù','Ú','Û','Ü','Ý','Þ','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô','õ','ö','÷','ø','ù','ú','û','ü','ý','þ','ÿ','€','‚','ƒ','„','…','†','‡','ˆ','‰','Š','‹','Œ','Ž','‘','’','“','”','•','–','—','˜','™','š','›','œ','ž','Ÿ','¡','¢','£','¤','¥','¦','§','¨','©','ª','«','¬','®','¯','°','±','²','³','´','µ','¶','·','¸','&supl;','º','»','¼','½','¾','¿','À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','×','Ø','Ù','Ú','Û','Ü','Ý','Þ','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô','õ','ö','÷','ø','ù','ú','û','ü','ý','þ','ÿ','€','','‚','ƒ','„','…','†','‡','ˆ','‰','Š','‹','Œ','','Ž','','','‘','’','“','”','•','–','—','˜','™','š','›','œ','','ž','Ÿ',' ','¡','¡','¡','¡','¥','¦','§','¨','©','ª','«','¬','','®','¯','°','±','²','³','´','µ','¶','·','¸','&supl;','º','»','¼','½','¾','¿','À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','×','Ø','Ù','Ú','Û','Ü','Ý','Þ','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô','õ','ö','÷','ø','ù','ú','û','ü','ý','þ','ÿ'); $xml = str_replace($umlaute, $htmlcode, $xml); } else { utf8dec($xml); } //------------------------------------------------------------------ End additional code $handle = fopen($cachefile,'w'); if (!$handle) { return "\n"; } fwrite($handle,$xml); fclose($handle); } } if (strlen($xml) < 1) { return "\n"; } if (!isset($xslt)) { // Skip the XSLT processing, just bring in the file $xslt_result = $xml; } else { // Find XSLT file $xsltfile = dge_irss_findFile($xsltpath, $xslt); if ($xsltfile == '') return "\n"; // This is a switchboard to choose the proper XSLT // processing engine and grind through it. if (PHP_VERSION >= 5) { $xsl = new DomDocument(); $xsl->load($xsltfile); $xslt = new XsltProcessor(); $xslt->importStyleSheet($xsl); $xslt->setParameter('',$xsltp); if ($processAsHTML) { $domresult = DomDocument::loadHTML($xml); if ($domresult == '') $xslt_result .= ""; } else { // First, try loading input as strict XML. $domresult = DomDocument::loadXML($xml); // If that fails, try a fallback to html. if ($domresult == '') { $domresult = DomDocument::loadHTML($xml); if ($domresult == '') $xslt_result .= ""; } } $xslt_result .= $xslt->transformToXML($domresult); } else { $xsl = file_get_contents($xsltfile); if (!$xsl) return "\n"; if (function_exists('domxml_open_mem') && function_exists('domxml_xslt_stylesheet')) { // PHP 4 DOM_XML support if (!$domXml = domxml_open_mem($xml)) { $result = "Error while parsing the xml document\n"; } $domXsltObj = domxml_xslt_stylesheet( $xsl ); $domTranObj = $domXsltObj->process( $domXml, $xsltp ); $xslt_result = $domXsltObj->result_dump_mem( $domTranObj ); } elseif (function_exists('xslt_create')) { // PHP 4 XSLT library $arguments = array ('/_xml' => $xml, '/_xsl' => $xsl); $xslt_inst = xslt_create(); $xslt_result = xslt_process($xslt_inst,'arg:/_xml','arg:/_xsl', NULL, $arguments); xslt_free($xslt_inst); } else { // Nothing, no valid processor found. Curses. return "\n"; } } if (empty($xslt_result)) { $xslt_result = "\n"; } } // End of Switch return $xslt_result . ""; } function dge_irss_findFile($path, $file) { foreach (explode(';', $path) as $dir) { $f = ABSPATH . "$dir/$file"; if (file_exists($f)) return $f; } return ''; } // Pass in a string, an array will be returned. function dge_irss_explodeParams($paramString) { $params = array(); foreach (explode(';', $paramString) as $param) { list($arg,$v) = explode('=', $param); $params[$arg] = $v; } return $params; } // Pass in an array, a string will be returned. function dge_irss_implodeParams($params) { $result = array(); foreach ($params as $key=>$val) { if ($val == '') $result[]=$key; else $result[] = "$key=$val"; } return implode(';', $result); } // This is a Wordpress content filter that replaces calls to DGE_InlineRSS // Any entries like !inlineRSS:iconophobia will be replaced with the // inlineRSS feed identified by "iconophobia". function dge_irss_content ($content = '') { $find[] = "//"; $replace[] = ""; preg_match_all('/!inlineRSS:(\w+)/', $content, $matches, PREG_SET_ORDER); foreach ($matches as $val) { $find[] = "/" . $val[0] . "/"; $replace[] = DGE_InlineRSS($val[1]); } return preg_replace($find, $replace, $content); } // Filters out inline calls function dge_irss_securityFilter($content = '') { $find[] = "//"; $replace[] = ""; preg_match_all('/!inlineRSS:(\w+)/', $content, $matches, PREG_SET_ORDER); foreach ($matches as $val) { $find[] = "^$val[0]^"; $replace[] = ""; } return preg_replace($find, $replace, $content); } function dge_irss_admin() { if (function_exists('add_options_page')) { add_options_page('InlineRSS Options', 'InlineRSS', 8, basename(__FILE__), 'dge_irss_subpanel'); } } function dge_irss_subpanel() { $presets = get_option('dge_irss_presets'); if (!$presets) $presets = array(); // ---------------------------------------------------------------- // PARSE $_POST PARAMETERS // ---------------------------------------------------------------- if (isset($_POST['info_update'])) { $updateText = ''; // ------------------------------------------------------------ // DEFAULTS // ------------------------------------------------------------ if (isset($_POST['def_timeout'])) { $timeout = $_POST['def_timeout']; if ($timeout == '') { $updateText .= "
Default timeout not updated. Invalid input.
\n"; } else { $timeout = intval($timeout); if ($timeout != get_option('dge_irss_def_timeout')) { update_option('dge_irss_def_timeout', $timeout); $updateText .= "Default timeout updated.
\n"; } } } // xslt path if (isset($_POST['xsltpath'])) { $xsltpath = $_POST['xsltpath']; if ($xsltpath != get_option('dge_irss_xsltpath')) { update_option('dge_irss_xsltpath', $xsltpath); $updateText .= "XSLT path updated.
\n"; } } // cache path if (isset($_POST['cachepath'])) { $cachepath = $_POST['cachepath']; if ($cachepath != get_option('dge_irss_cachepath')) { update_option('dge_irss_cachepath', $cachepath); $updateText .= "Cache path updated.
\n"; } } // cache prefix if (isset($_POST['cacheprefix'])) { $cacheprefix = $_POST['cacheprefix']; if ($cacheprefix != get_option('dge_irss_cacheprefix')) { update_option('dge_irss_cacheprefix', $cacheprefix); $updateText .= "Cache file prefix updated.
\n"; } } // ------------------------------------------------------------ // PRESETS // ------------------------------------------------------------ $updatepresets = 0; // Check for new presets if ($_POST['pre_new_name']!='') { $name = $_POST['pre_new_name']; if (array_key_exists($name, $presets)) { $updateText .= "New preset '$name' already exists. Not updated.
\n"; } else { $url = $_POST['pre_new_url']; $options = $_POST['pre_new_options']; $xsltp = $_POST['pre_new_xsltp']; if ($url != '') { $presets[$name] = array(); $presets[$name]['url'] = $url; $presets[$name]['options'] = dge_irss_explodeParams($options); $presets[$name]['xsltp'] = dge_irss_explodeParams($xsltp); $updateText .= "New preset '$name' added.
\n"; $updatepresets = 1; } else { $updateText .= "New preset '$name' not added - no url provided.
\n"; } } } // Check for updates to existing presets. foreach ($presets as $name=>$preset) { $urlKey = "pre_upd_url_".$name; $url = $preset['url']; $optionsKey = "pre_upd_opt_".$name; $optionsStr = dge_irss_implodeParams($preset['options']); $xsltpKey = "pre_upd_xsltp_".$name; $xsltpStr = dge_irss_implodeParams($preset['xsltp']); if ((array_key_exists($urlKey,$_POST) && $_POST[$urlKey] != $url) || (array_key_exists($optionsKey,$_POST) && $_POST[$optionsKey] != $optionsStr) || (array_key_exists($xsltpKey,$_POST) && $_POST[$xsltpKey] != $xsltpStr)) { $newurl = $_POST[$urlKey]; $uoptions = $_POST[$optionsKey]; $uxsltp = $_POST[$xsltpKey]; if ($newurl == '' && $uoptions == '' && $uxsltp == '') { unset($presets[$name]); $updateText .= "Preset '$name' removed.
\n"; } else { $presets[$name]['url'] = $newurl; $presets[$name]['options'] = dge_irss_explodeParams($uoptions); $presets[$name]['xsltp'] = dge_irss_explodeParams($uxsltp); $updateText .= "Preset '$name' updated.
\n"; } $updatepresets = 1; } } // Do all updates to the presets in one go. if ($updatepresets) { update_option('dge_irss_presets', $presets); } // Output $updateText if ($updateText != '') echo "