/**
  * Gets the latest version code from the Git repository
  *
  * The code is read from the raw content of the version file on the Git server.
  *
  * @return mixed the version code from the repository if available, else 'false'
  */
 public static function getLatestGitVersionCode($url, $timeout = 2)
 {
     list($headers, $data) = get_http_response($url, $timeout);
     if (strpos($headers[0], '200 OK') === false) {
         error_log('Failed to retrieve ' . $url);
         return false;
     }
     return str_replace(array(self::$VERSION_START_TAG, self::$VERSION_END_TAG, PHP_EOL), array('', '', ''), $data);
 }
Example #2
0
function genThumbnail()
{
    // Make sure the parameters in the URL were generated by us.
    $sign = hash_hmac('sha256', $_GET['url'], $GLOBALS['salt']);
    if ($sign != $_GET['hmac']) {
        die('Naughty boy!');
    }
    // Let's see if we don't already have the image for this URL in the cache.
    $thumbname = hash('sha1', $_GET['url']) . '.jpg';
    if (is_file($GLOBALS['config']['CACHEDIR'] . '/' . $thumbname)) {
        // We have the thumbnail, just serve it:
        header('Content-Type: image/jpeg');
        echo file_get_contents($GLOBALS['config']['CACHEDIR'] . '/' . $thumbname);
        return;
    }
    // We may also serve a blank image (if service did not respond)
    $blankname = hash('sha1', $_GET['url']) . '.gif';
    if (is_file($GLOBALS['config']['CACHEDIR'] . '/' . $blankname)) {
        header('Content-Type: image/gif');
        echo file_get_contents($GLOBALS['config']['CACHEDIR'] . '/' . $blankname);
        return;
    }
    // Otherwise, generate the thumbnail.
    $url = $_GET['url'];
    $domain = parse_url($url, PHP_URL_HOST);
    if ($domain == 'flickr.com' || endsWith($domain, '.flickr.com')) {
        // Crude replacement to handle new flickr domain policy (They prefer www. now)
        $url = str_replace('http://flickr.com/', 'http://www.flickr.com/', $url);
        // Is this a link to an image, or to a flickr page ?
        $imageurl = '';
        if (endswith(parse_url($url, PHP_URL_PATH), '.jpg')) {
            // This is a direct link to an image. e.g. http://farm1.staticflickr.com/5/5921913_ac83ed27bd_o.jpg
            preg_match('!(http://farm\\d+\\.staticflickr\\.com/\\d+/\\d+_\\w+_)\\w.jpg!', $url, $matches);
            if (!empty($matches[1])) {
                $imageurl = $matches[1] . 'm.jpg';
            }
        } else {
            // Get the flickr html page.
            list($headers, $content) = get_http_response($url, 20);
            if (strpos($headers[0], '200 OK') !== false) {
                // flickr now nicely provides the URL of the thumbnail in each flickr page.
                preg_match('!<link rel=\\"image_src\\" href=\\"(.+?)\\"!', $content, $matches);
                if (!empty($matches[1])) {
                    $imageurl = $matches[1];
                }
                // In albums (and some other pages), the link rel="image_src" is not provided,
                // but flickr provides:
                // <meta property="og:image" content="http://farm4.staticflickr.com/3398/3239339068_25d13535ff_z.jpg" />
                if ($imageurl == '') {
                    preg_match('!<meta property=\\"og:image\\" content=\\"(.+?)\\"!', $content, $matches);
                    if (!empty($matches[1])) {
                        $imageurl = $matches[1];
                    }
                }
            }
        }
        if ($imageurl != '') {
            // Let's download the image.
            // Image is 240x120, so 10 seconds to download should be enough.
            list($headers, $content) = get_http_response($imageurl, 10);
            if (strpos($headers[0], '200 OK') !== false) {
                // Save image to cache.
                file_put_contents($GLOBALS['config']['CACHEDIR'] . '/' . $thumbname, $content);
                header('Content-Type: image/jpeg');
                echo $content;
                return;
            }
        }
    } elseif ($domain == 'vimeo.com') {
        // This is more complex: we have to perform a HTTP request, then parse the result.
        // Maybe we should deport this to JavaScript ? Example: http://stackoverflow.com/questions/1361149/get-img-thumbnails-from-vimeo/4285098#4285098
        $vid = substr(parse_url($url, PHP_URL_PATH), 1);
        list($headers, $content) = get_http_response('https://vimeo.com/api/v2/video/' . escape($vid) . '.php', 5);
        if (strpos($headers[0], '200 OK') !== false) {
            $t = unserialize($content);
            $imageurl = $t[0]['thumbnail_medium'];
            // Then we download the image and serve it to our client.
            list($headers, $content) = get_http_response($imageurl, 10);
            if (strpos($headers[0], '200 OK') !== false) {
                // Save image to cache.
                file_put_contents($GLOBALS['config']['CACHEDIR'] . '/' . $thumbname, $content);
                header('Content-Type: image/jpeg');
                echo $content;
                return;
            }
        }
    } elseif ($domain == 'ted.com' || endsWith($domain, '.ted.com')) {
        // The thumbnail for TED talks is located in the <link rel="image_src" [...]> tag on that page
        // http://www.ted.com/talks/mikko_hypponen_fighting_viruses_defending_the_net.html
        // <link rel="image_src" href="http://images.ted.com/images/ted/28bced335898ba54d4441809c5b1112ffaf36781_389x292.jpg" />
        list($headers, $content) = get_http_response($url, 5);
        if (strpos($headers[0], '200 OK') !== false) {
            // Extract the link to the thumbnail
            preg_match('!link rel="image_src" href="(http://images.ted.com/images/ted/.+_\\d+x\\d+\\.jpg)"!', $content, $matches);
            if (!empty($matches[1])) {
                // Let's download the image.
                $imageurl = $matches[1];
                // No control on image size, so wait long enough
                list($headers, $content) = get_http_response($imageurl, 20);
                if (strpos($headers[0], '200 OK') !== false) {
                    $filepath = $GLOBALS['config']['CACHEDIR'] . '/' . $thumbname;
                    file_put_contents($filepath, $content);
                    // Save image to cache.
                    if (resizeImage($filepath)) {
                        header('Content-Type: image/jpeg');
                        echo file_get_contents($filepath);
                        return;
                    }
                }
            }
        }
    } elseif ($domain == 'xkcd.com' || endsWith($domain, '.xkcd.com')) {
        // There is no thumbnail available for xkcd comics, so download the whole image and resize it.
        // http://xkcd.com/327/
        // <img src="http://imgs.xkcd.com/comics/exploits_of_a_mom.png" title="<BLABLA>" alt="<BLABLA>" />
        list($headers, $content) = get_http_response($url, 5);
        if (strpos($headers[0], '200 OK') !== false) {
            // Extract the link to the thumbnail
            preg_match('!<img src="(http://imgs.xkcd.com/comics/.*)" title="[^s]!', $content, $matches);
            if (!empty($matches[1])) {
                // Let's download the image.
                $imageurl = $matches[1];
                // No control on image size, so wait long enough
                list($headers, $content) = get_http_response($imageurl, 20);
                if (strpos($headers[0], '200 OK') !== false) {
                    $filepath = $GLOBALS['config']['CACHEDIR'] . '/' . $thumbname;
                    // Save image to cache.
                    file_put_contents($filepath, $content);
                    if (resizeImage($filepath)) {
                        header('Content-Type: image/jpeg');
                        echo file_get_contents($filepath);
                        return;
                    }
                }
            }
        }
    } else {
        // For all other domains, we try to download the image and make a thumbnail.
        // We allow 30 seconds max to download (and downloads are limited to 4 Mb)
        list($headers, $content) = get_http_response($url, 30);
        if (strpos($headers[0], '200 OK') !== false) {
            $filepath = $GLOBALS['config']['CACHEDIR'] . '/' . $thumbname;
            // Save image to cache.
            file_put_contents($filepath, $content);
            if (resizeImage($filepath)) {
                header('Content-Type: image/jpeg');
                echo file_get_contents($filepath);
                return;
            }
        }
    }
    // Otherwise, return an empty image (8x8 transparent gif)
    $blankgif = base64_decode('R0lGODlhCAAIAIAAAP///////yH5BAEKAAEALAAAAAAIAAgAAAIHjI+py+1dAAA7');
    file_put_contents($GLOBALS['config']['CACHEDIR'] . '/' . $blankname, $blankgif);
    // Also put something in cache so that this URL is not requested twice.
    header('Content-Type: image/gif');
    echo $blankgif;
}
Example #3
0
function send_http_request($request = '', $host = '', $port = 80)
{
    $host = $host ? $host : TM_DOMAIN;
    $host = str_replace("http://", "", $host);
    $host = str_replace("https://", "", $host);
    if (!($fp = fsockopen($host, $port))) {
        return 0;
    }
    if (!fputs($fp, $request, strlen($request))) {
        return 0;
    }
    $data = "";
    while (!feof($fp)) {
        $data .= fread($fp, 256);
    }
    //win32 limit 2048
    fclose($fp);
    $http_response = get_http_response($data);
    return $http_response;
}