Esempio n. 1
0
function retrieve_resource($resource)
{
    logger('retrieve_resource: ' . ($resource['num-tries'] + 1) . ' attempt at resource ' . $resource['id'] . ' ' . $resource['url'], LOGGER_DEBUG);
    q("UPDATE `retriever_resource` SET `last-try` = now(), `num-tries` = `num-tries` + 1 WHERE id = %d", intval($resource['id']));
    $data = fetch_url($resource['url'], $resource['binary'], $resource['type']);
    $resource['type'] = get_app()->get_curl_content_type();
    if ($data) {
        $resource['data'] = $data;
        q("UPDATE `retriever_resource` SET `completed` = now(), `data` = '%s', `type` = '%s' WHERE id = %d", dbesc($data), dbesc($resource['type']), intval($resource['id']));
        retriever_resource_completed($resource);
    }
}
Esempio n. 2
0
function add_retriever_resource($url, $binary = false)
{
    logger('add_retriever_resource: ' . $url, LOGGER_DEBUG);
    $scheme = parse_url($url, PHP_URL_SCHEME);
    if ($scheme == 'data') {
        $fp = fopen($url, 'r');
        $meta = stream_get_meta_data($fp);
        $type = $meta['mediatype'];
        $data = stream_get_contents($fp);
        fclose($fp);
        $url = 'md5://' . hash('md5', $url);
        $r = q("SELECT * FROM `retriever_resource` WHERE `url` = '%s'", dbesc($url));
        $resource = $r[0];
        if (count($r)) {
            logger('add_retriever_resource: Resource ' . $url . ' already requested', LOGGER_DEBUG);
            return $resource;
        }
        logger('retrieve_resource: got data URL type ' . $resource['type'], LOGGER_DEBUG);
        q("INSERT INTO `retriever_resource` (`type`, `binary`, `url`, `completed`, `data`) " . "VALUES ('%s', %d, '%s', now(), '%s')", dbesc($type), intval($binary ? 1 : 0), dbesc($url), dbesc($data));
        $r = q("SELECT * FROM `retriever_resource` WHERE `url` = '%s'", dbesc($url));
        $resource = $r[0];
        if (count($r)) {
            retriever_resource_completed($resource);
        }
        return $resource;
    }
    if (strlen($url) > 800) {
        logger('add_retriever_resource: URL is longer than 800 characters', LOGGER_NORMAL);
    }
    $r = q("SELECT * FROM `retriever_resource` WHERE `url` = '%s'", dbesc($url));
    $resource = $r[0];
    if (count($r)) {
        logger('add_retriever_resource: Resource ' . $url . ' already requested', LOGGER_DEBUG);
        return $resource;
    }
    q("INSERT INTO `retriever_resource` (`binary`, `url`) " . "VALUES (%d, '%s')", intval($binary ? 1 : 0), dbesc($url));
    $r = q("SELECT * FROM `retriever_resource` WHERE `url` = '%s'", dbesc($url));
    return $r[0];
}