Example #1
0
 /**
  * Fetch a URL
  * @param string $url
  * @param Subscriber $subscriber
  * @return bool|int|mixed|string
  */
 public function fetchUrl($url, $subscriber = null)
 {
     $content = '';
     ## fix the Editor replacing & with &
     $url = str_ireplace('&', '&', $url);
     # $this->logger->notice("Fetching $url");
     //subscriber items to replace:
     if ($subscriber != null) {
         foreach (Subscriber::$DB_ATTRIBUTES as $key) {
             if ($key != 'password') {
                 $url = utf8_encode(str_ireplace("[{$key}]", urlencode($subscriber->{$key}), utf8_decode($url)));
             }
         }
     }
     $url = $this->expandUrl($url);
     #  print "<h1>Fetching ".$url."</h1>";
     # keep in memory cache in case we send a page to many emails
     $cache = Cache::instance();
     if (isset($cache->url_cache[$url]) && is_array($cache->url_cache[$url]) && time() - $cache->url_cache[$url]['fetched'] < $this->config->get('REMOTE_URL_REFETCH_TIMEOUT')) {
         #$this->logger->notice($url . " is cached in memory");
         if ($this->config->get('VERBOSE') && function_exists('output')) {
             output('From memory cache: ' . $url);
         }
         return $cache->url_cache[$url]['content'];
     }
     $timeout = time() - Cache::getPageCacheLastModified($url);
     if ($timeout < $this->config->get('REMOTE_URL_REFETCH_TIMEOUT')) {
         #$this->logger->notice($url.' was cached in database');
         if ($this->config->get('VERBOSE') && function_exists('output')) {
             output('From database cache: ' . $url);
         }
         return Cache::getPageCache($url);
     } else {
         #$this->logger->notice($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 = Cache::getPageCache($url, $lastmodified);
     if (!$cache) {
         ## @#TODO, make it work with Request2
         if (function_exists('curl_init')) {
             $content = $this->fetchUrlCurl($url, $request_parameters);
         } elseif (0 && $this->config->get('has_pear_http_request') == 2) {
             @(require_once "HTTP/Request2.php");
         } elseif ($this->config->get('has_pear_http_request')) {
             @(require_once "HTTP/Request.php");
             $content = $this->fetchUrlPear($url, $request_parameters);
         } else {
             return false;
         }
     } else {
         if ($this->config->get('VERBOSE')) {
             $this->logger->notice($url . ' was cached in database');
         }
         $content = $cache;
     }
     if (!empty($content)) {
         $content = $this->addAbsoluteResources($content, $url);
         $this->logger->notice('Fetching ' . $url . ' success');
         Cache::setPageCache($url, $lastmodified, $content);
         Cache::instance()->url_cache[$url] = array('fetched' => time(), 'content' => $content);
     }
     return $content;
 }