function fetchUrl($url, $userdata = array()) { $content = ''; ## fix the Editor replacing & with & $url = str_ireplace('&', '&', $url); # logEvent("Fetching $url"); if (count($userdata)) { foreach ($userdata as $key => $val) { if ($key != 'password') { $url = utf8_encode(str_ireplace("[{$key}]", urlencode($val), utf8_decode($url))); } } } if (!isset($GLOBALS['urlcache'])) { $GLOBALS['urlcache'] = array(); } $url = expandUrl($url); # print "<h1>Fetching ".$url."</h1>"; # keep in memory cache in case we send a page to many emails if (isset($GLOBALS['urlcache'][$url]) && is_array($GLOBALS['urlcache'][$url]) && time() - $GLOBALS['urlcache'][$url]['fetched'] < REMOTE_URL_REFETCH_TIMEOUT) { # logEvent($url . " is cached in memory"); if (VERBOSE && function_exists('output')) { output('From memory cache: ' . $url); } return $GLOBALS['urlcache'][$url]['content']; } $dbcache_lastmodified = getPageCacheLastModified($url); $timeout = time() - $dbcache_lastmodified; if ($timeout < REMOTE_URL_REFETCH_TIMEOUT) { # logEvent($url.' was cached in database'); if (VERBOSE && function_exists('output')) { output('From database cache: ' . $url); } return getPageCache($url); } else { # logEvent($url.' is not cached in database '.$timeout.' '. $dbcache_lastmodified." ".time()); } $request_parameters = array('timeout' => 600, 'allowRedirects' => 1, 'method' => 'HEAD'); $remote_charset = 'UTF-8'; ## relying on the last modified header doesn't work for many pages ## use current time instead ## see http://mantis.phplist.com/view.php?id=7684 # $lastmodified = strtotime($header["last-modified"]); $lastmodified = time(); $cache = getPageCache($url, $lastmodified); if (!$cache) { if (function_exists('curl_init')) { $content = fetchUrlCurl($url, $request_parameters); } elseif (0 && $GLOBALS['has_pear_http_request'] == 2) { ## @#TODO, make it work with Request2 @(require_once 'HTTP/Request2.php'); } elseif ($GLOBALS['has_pear_http_request']) { @(require_once 'HTTP/Request.php'); $content = fetchUrlPear($url, $request_parameters); } else { return false; } } else { if (VERBOSE) { logEvent($url . ' was cached in database'); } $content = $cache; } if (!empty($content)) { $content = addAbsoluteResources($content, $url); logEvent('Fetching ' . $url . ' success'); setPageCache($url, $lastmodified, $content); $GLOBALS['urlcache'][$url] = array('fetched' => time(), 'content' => $content); } return $content; }
<?php include_once 'db.php'; include_once 'mooshulib.php'; $id = stripslashes($_GET['id']); $useragent = $_SERVER['HTTP_USER_AGENT']; $url = expandUrl($id, $useragent); if ($url == "0") { print "<html>"; print "<head>"; print "<title>URL not found!</title>\n"; print "</head>"; } else { header("Location: {$url}"); print "<html>"; print "<head>"; print "<title>Redirecting to {$url}</title>"; print "</head>"; } ?> </head> <body> <?php if ($url == "0") { print "The entered URL was not found!\n"; } else { print "You should have been redirected <a href=\"{$url}\">here</a>."; } ?> </body> </html>