': $out .= '>'; break; case '&': $out .= '&'; break; default: $out .= $in[$i]; break; } } return $out; } function rss_encode($text, $is_utf8) { if ($is_utf8) { return $text; } return utf8_encode($text); } if (file_exists('.rssaccess')) { $access_ok = false; $fd = fopen('.rssaccess', 'r'); while (! feof($fd)) { $line = fgets($fd, 4096); $test = substr($line, 0, strlen($line) - 1); if (! strcmp($url, $test)) { $access_ok = true; break; } } fclose($fd); if (! $access_ok) { header("HTTP/1.0 204 No Response"); header("Content-Length: 0"); printf("\n"); exit(0); } } $purl = parse_url($url); $curl = $purl['scheme'] . "://" . $purl['host'] . $purl['path']; $cache = "../rss-cache/" . urlencode($curl . $sf . $ef . $si . $ei . $sd . $ed . $sl . $lno) . ".rss"; $html = "../rss-cache/" . urlencode($curl . $sf . $ef . $si . $ei . $sd . $ed . $sl . $lno) . ".html"; header("Content-Type: application/rss+xml"); $now = time(); $self = $_SERVER['REQUEST_URI']; // printf("cache: \"%s\"\n", $cache); if (file_exists($cache)) { $ts = filemtime($cache); if ($now < $ts + 600) { $in = fopen($cache, "r"); flock($in, LOCK_SH); $p = sprintf("Content-Length: %d", filesize($cache)); header($p); fpassthru($in); flock($in, LOCK_UN); fclose($in); exit(0); } } // printf("html: \"%s\"\n", $html); if (file_exists($html)) { $ts = filemtime($html); if ($now < $ts + 600) { $res = 0; } else { $res = fetch_url($url, $html); } } else { $res = fetch_url($url, $html); } if ($res != 0) { $purl = parse_url($url); if (!strcmp($purl['host'], "www.sailonline.org")) { $input_is_utf8 = true; $nurl = $purl['scheme'] . "://62.181.230.171" . $purl['path']; $res = fetch_url($nurl, $html); } } if ($res != 0) { printf("\n"); printf("\n"); printf("\n"); exit(0); } // printf("res: \"%d\"\n", $res); $purl = parse_url($url); $base = $purl['scheme'] . "://" . $purl['host']; $path = $purl['path']; if (!strcmp($purl['host'], "www.sailonline.org")) { $input_is_utf8 = true; } // printf("base: \"%s\", path: \"%s\"\n", $base, $path); $lock = fopen($html . ".lock", "a+"); flock($lock, LOCK_SH); $content = file_get_contents($html); flock($lock, LOCK_UN); fclose($lock); $start = stristr($content, ""); $len = strpos($start, ""); if ($len === false) { $len = strpos($start, ""); } $title = strip_tags(substr($start, 0, $len)); $out = fopen($cache, "w"); flock($out, LOCK_EX); fwrite($out, "\n"); fwrite($out, "\n"); fwrite($out, "\n"); $p = sprintf("\n", htmlentities($self, ENT_NOQUOTES, false)); fwrite($out, $p); $p = sprintf("%s\n", $title); fwrite($out, $p); $p = sprintf("%s\n", xml_encode($url)); fwrite($out, $p); $p = sprintf("%s\n", xml_encode($url)); fwrite($out, $p); $pubdate = date('r', time(0)); $p = sprintf("%s\n", $pubdate); fwrite($out, $p); fwrite($out, "10\n"); // printf("sf: \"%s\"\n", $sf); // printf("ef: \"%s\"\n", $ef); $start = strstr($content, $sf); $len = strpos($start, $ef) + strlen($ef); $data = substr($start, 0, $len); // printf("\ndata:\n%s\n\n", $data); while (strlen($data)) { $start = strstr($data, $si); if (! $start) { break; } $len = strpos($start, $ei) + strlen($ei); if (($ei[0] == "<") && (NULL == strchr($ei, '>'))) { $rest = strpos(substr($start, $len), ">"); if ($rest === false) { $rest = 0; } else { $rest++; } $len += $rest; } $item = substr($start, 0, $len); $item = iso8859clean($item); $data = substr($start, $len); $start = strstr($item, $sd); $len = strpos($start, $ed) + strlen($ed); $desc = strip_tags(substr($start, 0, $len)); $text = substr($item, $len); // printf("\ndesc:\n%s\n\n", $desc); // printf("\nitem:\n%s\n\n", $text); $icontent = ""; $p = sprintf("%s\n", rss_encode($desc, $input_is_utf8)); $icontent .= $p; if ($lno < 0) { $link = $url; $guid = md5($item); } else { if (strlen($sl) > 0) { $ldata = strstr($item, $sl); } else { $ldata = $item; } for ($i = 0; $i < $lno; $i++) { $start = substr(stristr($ldata, 'href="'), strlen('href="')); $len = strpos($start, '"'); $link = substr($start, 0, $len); $ldata = substr($start, $len); } // printf("\nlink:\n%s\n\n", $link); $link = absolute_url($link, $base, $path); $guid = $link; } $hashfile = "../rss-cache/" . urlencode($curl . $sf . $ef . $si . $ei . $sd . $ed . $sl . $lno . $guid) . ".md5"; $p = sprintf("%s\n", xml_encode($link)); $icontent .= $p; if ($lno < 0) { $p = sprintf("%s\n", $guid); } else { $p = sprintf("%s\n", $guid); } $icontent .= $p; $icontent .= "\n"; $hash = md5($icontent); if (file_exists($hashfile)) { $check = file_get_contents($hashfile); } else { $check = ""; } if (strcmp($hash, $check)) { $hashp = fopen($hashfile, "w"); fwrite($hashp, $hash); fclose($hashp); } $pubdate = date('r', filemtime($hashfile)); fwrite($out, "\n"); fwrite($out, $icontent); $p = sprintf("%s\n", $pubdate); fwrite($out, $p); fwrite($out, "\n"); } fwrite($out, "\n"); fwrite($out, "\n"); flock($out, LOCK_UN); fclose($out); $in = fopen($cache, "r"); flock($in, LOCK_SH); $p = sprintf("Content-Length: %d", filesize($cache)); header($p); fpassthru($in); flock($in, LOCK_UN); fclose($in); ?>