コード例 #1
0
function simpleCachedCurl($url, $expires, $debug = false)
{
    if ($debug) {
        echo "simpleCachedCurl debug:\n";
        echo "{$url}\n";
    }
    $hash = md5($url);
    $filename = dirname(__FILE__) . '/../cache/' . $hash . '.cache';
    $exists = file_exists($filename);
    $changed = $exists ? filemtime($filename) : 0;
    $now = time();
    $diff = $now - $changed;
    if ($debug) {
        if ($exists) {
            echo "File Exists! time: " . $changed . "\n";
            echo "diff: " . $diff . "\n";
        } else {
            echo "no cached File\n";
        }
    }
    $returnValue = false;
    if (!$changed || $diff > $expires) {
        if ($debug) {
            echo "no cache or expired --> make new request\n";
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_VERSION_IPV6);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch, CURLOPT_TIMEOUT, 20);
        curl_setopt($ch, CURLOPT_USERAGENT, 'php parser for http://de-map.freifunk-emskirchen.de/');
        $rawData = curl_exec($ch);
        $status = curl_getinfo($ch);
        if ($status['http_code'] == 301 || $status['http_code'] == 302) {
            $headerData = _curlGetHeader($url);
            list($header) = explode("\r\n\r\n", $headerData, 2);
            $matches = array();
            preg_match("/(Location:|URI:)[^(\n)]*/", $header, $matches);
            $url = trim(str_replace($matches[1], "", $matches[0]));
            $url_parsed = parse_url($url);
            return isset($url_parsed) ? simpleCachedCurl($url, $expires, $debug) : '';
        }
        if (!$rawData) {
            if ($debug) {
                echo "cURL request failed\n";
            }
            if ($changed) {
                if ($debug) {
                    echo "at least we have an expired cache --> better than nothing --> read it\n";
                }
                $cache = unserialize(file_get_contents($filename));
                $returnValue = $cache;
            } else {
                if ($debug) {
                    echo "request failed and we have no cache at all --> FAIL\n";
                    echo 'Curl-Fehler: ' . curl_error($ch) . "\n";
                }
                $returnValue = false;
            }
        } else {
            if ($debug) {
                echo "we got a return --> save it to cache\n";
            }
            $cache = fopen($filename, 'wb');
            $write = fwrite($cache, serialize($rawData));
            if ($debug && !$write) {
                echo "writing to {$filename} failed. Make the folder '" . dirname(__FILE__) . '/../cache/' . "' is writeable (chmod 777)";
            }
            fclose($cache);
        }
        curl_close($ch);
        $returnValue = $rawData;
    } else {
        if ($debug) {
            echo "yay we hit the cache --> read it\n";
        }
        $cache = unserialize(file_get_contents($filename));
        $returnValue = $cache;
    }
    return $returnValue;
}
コード例 #2
0
 private function _getFromOwm($comName, $comUrl)
 {
     $comUrl .= 'api/view_nodes';
     $comUrl = str_replace('www.', '', $comUrl);
     $result = simpleCachedCurl($comUrl, $this->_curlCacheTime, $this->_debug);
     if (!$result) {
         $this->_addCommunityMessage($comUrl . ' returns no result');
         return false;
     }
     $responseObject = json_decode($result);
     if (!$responseObject) {
         $this->_addCommunityMessage($comUrl . ' returns no valid json');
         return false;
     }
     $routers = $responseObject->rows;
     if (!$routers) {
         $this->_addCommunityMessage($comUrl . ' contains no nodes');
         return false;
     }
     $counter = 0;
     $skipped = 0;
     $duplicates = 0;
     $added = 0;
     $dead = 0;
     foreach ($routers as $router) {
         if (empty($router->value->latlng[0]) || empty($router->value->latlng[1])) {
             // router has no location
             $skipped++;
             continue;
         }
         $date = date_create((string) $router->value->mtime);
         // was online in last 24h ?
         $isOnline = time() - $date->getTimestamp() < 60 * 60 * 24;
         if (time() - $date->getTimestamp() > 60 * 60 * 24 * $this->_maxAge) {
             // router has been offline for a long time now
             $dead++;
             continue;
         }
         $thisRouter = array('id' => (string) $router->id, 'lat' => (string) $router->value->latlng[0], 'long' => (string) $router->value->latlng[1], 'name' => (string) $router->value->hostname, 'community' => $comName, 'status' => $isOnline ? 'online' : 'offline');
         // add to routerlist for later use in JS
         if ($this->_addOrForget($thisRouter)) {
             $added++;
         } else {
             $duplicates++;
         }
     }
     $this->_addCommunityMessage('parsing done. ' . $counter . ' nodes found, ' . $added . ' added, ' . $skipped . ' skipped, ' . $duplicates . ' duplicates, ' . $dead . ' dead');
     return true;
 }