示例#1
0
 /**
  * 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_url($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);
 }
 static function get_head_script_styles()
 {
     $script_names = array("jquery", "jquery-1.7.1.min", "jquery-ui-1.8.19.custom.min", "ajax");
     $styles_names = array("jquery.jscrollpane.lozenge", "jquery-ui-1.8.19.custom");
     $head_balise = '<link rel="icon" type="image/png" href="' . get_http_url() . 'logo.png" />';
     if (isArray($script_names)) {
         foreach ($script_names as $value) {
             $head_balise .= "<script type='text/javascript' src='" . get_http_url() . DEFAULT_JS_DIR . $value . ".js'></script>";
         }
     }
     if (isArray($styles_names)) {
         foreach ($styles_names as $value) {
             $head_balise .= "<link type='text/css' href='" . get_http_url() . DEFAULT_CSS_DIR . $value . ".css' rel='stylesheet' media='all' />";
         }
     }
     return $head_balise;
 }
示例#3
0
 /**
  * Get an invalid remote URL
  */
 public function testGetInvalidRemoteUrl()
 {
     list($headers, $content) = get_http_url('http://non.existent', 1);
     $this->assertEquals('HTTP Error', $headers[0]);
     $this->assertRegexp('/Name or service not known/', $content);
 }
示例#4
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, $data) = get_http_url($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=\\"(.+?)\\"!', $data, $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=\\"(.+?)\\"!', $data, $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, $data) = get_http_url($imageurl, 10);
            if (strpos($headers[0], '200 OK') !== false) {
                file_put_contents($GLOBALS['config']['CACHEDIR'] . '/' . $thumbname, $data);
                // Save image to cache.
                header('Content-Type: image/jpeg');
                echo $data;
                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, $data) = get_http_url('https://vimeo.com/api/v2/video/' . escape($vid) . '.php', 5);
        if (strpos($headers[0], '200 OK') !== false) {
            $t = unserialize($data);
            $imageurl = $t[0]['thumbnail_medium'];
            // Then we download the image and serve it to our client.
            list($headers, $data) = get_http_url($imageurl, 10);
            if (strpos($headers[0], '200 OK') !== false) {
                file_put_contents($GLOBALS['config']['CACHEDIR'] . '/' . $thumbname, $data);
                // Save image to cache.
                header('Content-Type: image/jpeg');
                echo $data;
                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, $data) = get_http_url($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)"!', $data, $matches);
            if (!empty($matches[1])) {
                // Let's download the image.
                $imageurl = $matches[1];
                // No control on image size, so wait long enough
                list($headers, $data) = get_http_url($imageurl, 20);
                if (strpos($headers[0], '200 OK') !== false) {
                    $filepath = $GLOBALS['config']['CACHEDIR'] . '/' . $thumbname;
                    file_put_contents($filepath, $data);
                    // 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, $data) = get_http_url($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]!', $data, $matches);
            if (!empty($matches[1])) {
                // Let's download the image.
                $imageurl = $matches[1];
                // No control on image size, so wait long enough
                list($headers, $data) = get_http_url($imageurl, 20);
                if (strpos($headers[0], '200 OK') !== false) {
                    $filepath = $GLOBALS['config']['CACHEDIR'] . '/' . $thumbname;
                    file_put_contents($filepath, $data);
                    // Save image to cache.
                    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, $data) = get_http_url($url, 30);
        if (strpos($headers[0], '200 OK') !== false) {
            $filepath = $GLOBALS['config']['CACHEDIR'] . '/' . $thumbname;
            file_put_contents($filepath, $data);
            // Save image to cache.
            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;
}