function CarpCache($url, $cachefile)
{
    global $carpconf;
    if (strlen($cachefile)) {
        $cache = CarpSetCache($cachefile);
        if ($cache % 2 == 0) {
            require_once dirname(__FILE__) . '/carpinc.php';
            CacheRSSFeed($url);
        }
    } else {
        CarpError('No cache file indicated when calling CarpCache.', 0);
    }
}
Example #2
0
function CarpCache($url, $cachefile, $cachefunction = 1)
{
    global $carpconf;
    if ($carpconf['phperrors'] >= 0) {
        $carpconf['savephperrors'] = error_reporting($carpconf['phperrors']);
    }
    if (strlen($cachefile)) {
        $cache = CarpSetCache($cachefile, $cachefunction);
        if ($cache % 2 == 0) {
            require_once CarpDirName() . '/carpinc.php';
            CacheRSSFeed($url);
            $carpconf['cachefile'] = '';
            if ($carpconf['phperrors'] >= 0) {
                error_reporting($carpconf['savephperrors']);
            }
            return 1;
        }
    } else {
        CarpError('No cache file indicated when calling CarpCache.', 0);
    }
    $carpconf['cachefile'] = '';
    if ($carpconf['phperrors'] >= 0) {
        error_reporting($carpconf['savephperrors']);
    }
    return 0;
}
Example #3
0
function GetRSSFeed($url, $cache, $showit)
{
    global $carpconf, $CarpRedirs;
    $carpconf['desctags'] = preg_replace("/(^\\|)|(\\|\$)/", '', preg_replace("/\\|+/", "|", preg_replace("#/?(script|embed|object|applet|iframe)#i", '', $carpconf['descriptiontags'])));
    $carpconf['desctags'] = str_replace('|', '\\b|', $carpconf['desctags']) . '\\b';
    // 3 lines for backwards compatibility
    if ($carpconf['corder'] !== false) {
        $carpconf['cborder'] = $carpconf['corder'];
    }
    if ($carpconf['bc'] !== false) {
        $carpconf['bcb'] = $carpconf['bc'];
    }
    if ($carpconf['ac'] !== false) {
        $carpconf['acb'] = $carpconf['ac'];
    }
    $rss_parser = new RSSParser();
    $rss_parser->showit = $showit;
    $rss_parser->channelborder = explode(',', preg_replace('/[^a-z0-9,]/', '', strtolower($carpconf['cborder'])));
    $rss_parser->channelaorder = explode(',', preg_replace('/[^a-z0-9,]/', '', strtolower($carpconf['caorder'])));
    $rss_parser->SetItemOrder($carpconf['iorder']);
    // the next 2 lines are for backward compatibility and will eventually be removed
    if ($carpconf['ilinktarget'] != '-1') {
        $carpconf['linktarget'] = $carpconf['ilinktarget'];
    } else {
        if ($carpconf['clinktarget'] != '-1') {
            $carpconf['linktarget'] = $carpconf['clinktarget'];
        }
    }
    if (preg_match("/[^0-9]/", $carpconf['linktarget'])) {
        $rss_parser->linktargets[$carpconf['linktarget']] = ' target="' . $carpconf['linktarget'] . '"';
    }
    $rss_parser->filterinfield = array();
    if (strlen($carpconf['filterin'])) {
        $rss_parser->filterin = explode('|', strtolower($carpconf['filterin']));
        for ($i = count($rss_parser->filterin) - 1; $i >= 0; $i--) {
            if (strpos($rss_parser->filterin[$i], ':') !== false) {
                list($rss_parser->filterinfield[$i], $rss_parser->filterin[$i]) = explode(':', $rss_parser->filterin[$i], 2);
            } else {
                $rss_parser->filterinfield[$i] = '';
            }
        }
    } else {
        $rss_parser->filterin = array();
    }
    $rss_parser->filteroutfield = array();
    if (strlen($carpconf['filterout'])) {
        $rss_parser->filterout = explode('|', strtolower($carpconf['filterout']));
        for ($i = count($rss_parser->filterout) - 1; $i >= 0; $i--) {
            if (strpos($rss_parser->filterout[$i], ':') !== false) {
                list($rss_parser->filteroutfield[$i], $rss_parser->filterout[$i]) = explode(':', $rss_parser->filterout[$i], 2);
            } else {
                $rss_parser->filteroutfield[$i] = '';
            }
        }
    } else {
        $rss_parser->filterout = array();
    }
    /*********************************************************
     * Note (2006-10-13, Adam Franco)
     *
     * I have reworked this code so that the data is fetched
     * first and the character encoding checked before the 
     * parser is created. As well, if a non-PHP-Supported 
     * encoding is found, the data will first be converted to
     * UTF-8 using iconv.
     *********************************************************/
    if ($fp = OpenRSSFeed($url)) {
        while ($data = fread($fp, 4096)) {
            // Set up the XML Parser
            if (!isset($xml_parser)) {
                // If an input enconding is specified, use that
                if ($carpconf['encodingin']) {
                    $xml_parser = xml_parser_create(strtoupper($carpconf['encodingin']));
                } else {
                    if (preg_match('/<\\?xml[^>]*encoding=[\'"]([^\'"]+)[\'"][^>]*\\?>/i', $data, $matches)) {
                        $sourceEncoding = strtoupper($matches[1]);
                        // 						print "<pre>".basename(__FILE__)." Line ".__LINE__.":\nEncoding found: ".$sourceEncoding."</pre>";
                        if ($sourceEncoding == 'UTF-8' || $sourceEncoding == 'ISO-8859-1' || $sourceEncoding == 'ASCII') {
                            $xml_parser = xml_parser_create($sourceEncoding);
                            unset($sourceEncoding);
                        } else {
                            $xml_parser = xml_parser_create("UTF-8");
                        }
                    } else {
                        // 						print "<pre>".basename(__FILE__)." Line ".__LINE__.":\nNo encoding found, assuming UTF-8</pre>";
                        $xml_parser = xml_parser_create("UTF-8");
                    }
                }
                if (strlen($carpconf['encodingout'])) {
                    xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, $carpconf['encodingout']);
                }
                xml_set_object($xml_parser, $rss_parser);
                xml_set_element_handler($xml_parser, "startElement", "endElement");
                xml_set_character_data_handler($xml_parser, "characterData");
                $CarpRedirs = array();
                $rss_parser->PrepTagPairs($carpconf['desctags']);
            }
            if (isset($sourceEncoding) && function_exists("iconv")) {
                // 				print "<pre>Converting $sourceEncoding to UTF-8</pre>";
                $data = iconv($sourceEncoding, "UTF-8", $data);
            }
            // Doubly escape any necessary ampersands.
            $data = preg_replace("/&(?!lt|gt|amp|apos|quot|nbsp)(.*\\b)/is", "&amp;\\1\\2", $data);
            if (!xml_parse($xml_parser, $data, feof($fp))) {
                CarpError("XML error: " . xml_error_string(xml_get_error_code($xml_parser)) . " at line " . xml_get_current_line_number($xml_parser));
                fclose($fp);
                xml_parser_free($xml_parser);
                return;
            }
            $data = '';
        }
        fclose($fp);
        if ($showit) {
            if ($carpconf['shownoitems'] && !$rss_parser->itemcount) {
                CarpOutput($carpconf['noitems']);
            } else {
                CarpOutput($rss_parser->top . $carpconf['bitems'] . $rss_parser->body . $carpconf['aitems'] . $rss_parser->bottom . $carpconf['poweredby']);
            }
        }
        if ($cache) {
            if ($cfp = OpenCacheWrite()) {
                if ($carpconf['shownoitems'] && !$rss_parser->itemcount) {
                    fwrite($cfp, $carpconf['noitems']);
                } else {
                    fwrite($cfp, ($showit ? $rss_parser->top . $carpconf['bitems'] : 'cb: :' . $rss_parser->top . "\n" . 'ca: :' . $rss_parser->bottom . "\n") . $rss_parser->body . ($showit ? $carpconf['aitems'] . $rss_parser->bottom . $carpconf['poweredby'] : ''));
                }
                CloseCacheWrite($cfp);
            } else {
                CarpError("Unable to create/open RSS cache file.", 0);
            }
        }
        xml_parser_free($xml_parser);
    } else {
        if ($showit && strlen($carpconf['cachefile']) && file_exists($carpconf['cachefile'])) {
            CarpOutput(file($carpconf['cachefile']));
        } else {
            if ($showit) {
                CarpError('Can\'t open remote newsfeed.', 0);
            }
        }
    }
}
function OpenCacheWrite()
{
    global $carpconf;
    $j = 0;
    $cachefile = $carpconf['cachefile'];
    if (!file_exists($cachefile)) {
        touch($cachefile);
    }
    if ($f = fopen($cachefile, 'r+')) {
        if ($a = fstat($f)) {
            flock($f, LOCK_EX);
            // ignore result--doesn't work for all systems and situations
            clearstatcache();
            if ($b = fstat($f)) {
                if ($a['mtime'] != $b['mtime']) {
                    flock($f, LOCK_UN);
                    fclose($f);
                } else {
                    $j = $f;
                }
            } else {
                CarpError("Can't stat cache file (2).");
                fclose($f);
            }
        } else {
            CarpError("Can't stat cache file (1).");
            fclose($f);
        }
    } else {
        CarpError("Can't open cache file: " . $cachefile);
    }
    return $j;
}