Beispiel #1
0
 /**
  * 在cache中获取键为$key的项的值
  * @param string $key 键值
  * @return string 如果该项不存在,则返回false
  * @access public
  */
 public function get($key)
 {
     $file = $this->cachePath . urlencode($key) . '.cache';
     if (file_exists($file)) {
         $content = safe_file_get_contents($file);
         if ($content === false) {
             return false;
         }
         $tmp = explode('<<%-==-%>>', $content);
         $timeout = $tmp[0];
         $value = $tmp[1];
         if (time() > $timeout) {
             $result = false;
         } else {
             $result = unserialize($value);
         }
     } else {
         $result = false;
     }
     return $result;
 }
Beispiel #2
0
function safe_file_get_contents($url, $redirect = 0)
{
    $parts = parse_url($url);
    $site = $parts['host'];
    $port = $parts['port'] ? $parts['port'] : 80;
    $path = $parts['path'] . ($parts['query'] ? "?" . $parts['query'] : "") . ($parts['fragment'] ? "#" . $parts['fragment'] : "");
    $timeout = 5;
    $sock = @fsockopen($site, $port, $errnum, $errstr, $timeout);
    if (!$sock) {
        return "Cannot connect to {$site}:{$port}: {$errstr}({$errno})";
    } else {
        @socket_set_timeout($sock, $timeout);
        $dump = "GET " . $path . " HTTP/1.0\r\n";
        $dump .= "User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT)\r\n";
        $dump .= "Host: " . $site . "\r\n";
        $dump .= "Connection: close\r\n\r\n";
        $res = "";
        // Send HTTP query
        fputs($sock, $dump);
        // Read all
        $header = true;
        while ($str = fgets($sock, 1024)) {
            if ($header) {
                if (preg_match("/^Location: ([^\\s]+)\\s*\$/", $str, $m) && ++$redirect < 10) {
                    return safe_file_get_contents($m[1], $redirect);
                }
                if ($str == "\r\n") {
                    $header = false;
                }
            } else {
                $res .= $str;
            }
        }
        fclose($sock);
    }
    return $res;
}