Ejemplo n.º 1
0
 /**
  * @group internet
  * @group slow
  */
 public function test_recache()
 {
     global $conf;
     $conf['fetchsize'] = 500 * 1024;
     //500kb
     $local = media_get_from_URL('http://www.google.com/images/srpr/logo3w.png', 'png', 5);
     $this->assertTrue($local !== false);
     $this->assertFileExists($local);
     // remember time stamp
     $time = filemtime($local);
     clearstatcache(false, $local);
     sleep(1);
     // fetch again and make sure we got a cache file
     $local = media_get_from_URL('http://www.google.com/images/srpr/logo3w.png', 'png', 5);
     clearstatcache(false, $local);
     $this->assertTrue($local !== false);
     $this->assertFileExists($local);
     $this->assertEquals($time, filemtime($local));
     clearstatcache(false, $local);
     sleep(6);
     // fetch again and make sure we got a new file
     $local = media_get_from_URL('http://www.google.com/images/srpr/logo3w.png', 'png', 5);
     clearstatcache(false, $local);
     $this->assertTrue($local !== false);
     $this->assertFileExists($local);
     $this->assertNotEquals($time, filemtime($local));
     unlink($local);
 }
 /**
  * Override the mpdf _getImage function
  *
  * This function takes care of gathering the image data from HTTP or
  * local files before passing the data back to mpdf's original function
  * making sure that only cached file paths are passed to mpdf. It also
  * takes care of checking image ACls.
  */
 function _getImage(&$file, $firsttime = true, $allowvector = true, $orig_srcpath = false)
 {
     global $conf;
     // build regex to parse URL back to media info
     $re = preg_quote(ml('xxx123yyy', '', true, '&', true), '/');
     $re = str_replace('xxx123yyy', '([^&\\?]*)', $re);
     // extract the real media from a fetch.php uri and determine mime
     if (preg_match("/^{$re}/", $file, $m) || preg_match('/[&\\?]media=([^&\\?]*)/', $file, $m)) {
         $media = rawurldecode($m[1]);
         list($ext, $mime) = mimetype($media);
     } else {
         list($ext, $mime) = mimetype($file);
     }
     // local files
     $local = '';
     if (substr($file, 0, 9) == 'dw2pdf://') {
         // support local files passed from plugins
         $local = substr($file, 9);
     } elseif (!preg_match('/(\\.php|\\?)/', $file)) {
         $re = preg_quote(DOKU_URL, '/');
         // directly access local files instead of using HTTP, skip dynamic content
         $local = preg_replace("/^{$re}/i", DOKU_INC, $file);
     }
     if (substr($mime, 0, 6) == 'image/') {
         if (!empty($media)) {
             // any size restrictions?
             $w = $h = 0;
             if (preg_match('/[\\?&]w=(\\d+)/', $file, $m)) {
                 $w = $m[1];
             }
             if (preg_match('/[\\?&]h=(\\d+)/', $file, $m)) {
                 $h = $m[1];
             }
             if (media_isexternal($media)) {
                 $local = media_get_from_URL($media, $ext, -1);
                 if (!$local) {
                     $local = $media;
                 }
                 // let mpdf try again
             } else {
                 $media = cleanID($media);
                 //check permissions (namespace only)
                 if (auth_quickaclcheck(getNS($media) . ':X') < AUTH_READ) {
                     $file = '';
                 }
                 $local = mediaFN($media);
             }
             //handle image resizing/cropping
             if ($w && file_exists($local)) {
                 if ($h) {
                     $local = media_crop_image($local, $ext, $w, $h);
                 } else {
                     $local = media_resize_image($local, $ext, $w, $h);
                 }
             }
         } elseif (media_isexternal($file)) {
             // fixed external URLs
             $local = media_get_from_URL($file, $ext, $conf['cachetime']);
         }
         if ($local) {
             $file = $local;
             $orig_srcpath = $local;
         }
     }
     return parent::_getImage($file, $firsttime, $allowvector, $orig_srcpath);
 }
Ejemplo n.º 3
0
/**
 * Check for media for preconditions and return correct status code
 *
 * READ: MEDIA, MIME, EXT, CACHE
 * WRITE: MEDIA, FILE, array( STATUS, STATUSMESSAGE )
 *
 * @author Gerry Weissbach <*****@*****.**>
 *
 * @param string $media  reference to the media id
 * @param string $file   reference to the file variable
 * @param string $rev
 * @param int    $width
 * @param int    $height
 * @return array as array(STATUS, STATUSMESSAGE)
 */
function checkFileStatus(&$media, &$file, $rev = '', $width = 0, $height = 0)
{
    global $MIME, $EXT, $CACHE, $INPUT;
    //media to local file
    if (media_isexternal($media)) {
        //check token for external image and additional for resized and cached images
        if (media_get_token($media, $width, $height) !== $INPUT->str('tok')) {
            return array(412, 'Precondition Failed');
        }
        //handle external images
        if (strncmp($MIME, 'image/', 6) == 0) {
            $file = media_get_from_URL($media, $EXT, $CACHE);
        }
        if (!$file) {
            //download failed - redirect to original URL
            return array(302, $media);
        }
    } else {
        $media = cleanID($media);
        if (empty($media)) {
            return array(400, 'Bad request');
        }
        // check token for resized images
        if (($width || $height) && media_get_token($media, $width, $height) !== $INPUT->str('tok')) {
            return array(412, 'Precondition Failed');
        }
        //check permissions (namespace only)
        if (auth_quickaclcheck(getNS($media) . ':X') < AUTH_READ) {
            return array(403, 'Forbidden');
        }
        $file = mediaFN($media, $rev);
    }
    //check file existance
    if (!file_exists($file)) {
        return array(404, 'Not Found');
    }
    return array(200, null);
}
Ejemplo n.º 4
0
/**
 * Check for media for preconditions and return correct status code
 *
 * READ: MEDIA, MIME, EXT, CACHE
 * WRITE: MEDIA, FILE, array( STATUS, STATUSMESSAGE )
 *
 * @author Gerry Weissbach <*****@*****.**>
 * @param $media reference to the media id
 * @param $file reference to the file variable
 * @returns array(STATUS, STATUSMESSAGE)
 */
function checkFileStatus(&$media, &$file)
{
    global $MIME, $EXT, $CACHE;
    //media to local file
    if (preg_match('#^(https?)://#i', $media)) {
        //check hash
        if (substr(md5(auth_cookiesalt() . $media), 0, 6) != $_REQUEST['hash']) {
            return array(412, 'Precondition Failed');
        }
        //handle external images
        if (strncmp($MIME, 'image/', 6) == 0) {
            $file = media_get_from_URL($media, $EXT, $CACHE);
        }
        if (!$file) {
            //download failed - redirect to original URL
            return array(302, $media);
        }
    } else {
        $media = cleanID($media);
        if (empty($media)) {
            return array(400, 'Bad request');
        }
        //check permissions (namespace only)
        if (auth_quickaclcheck(getNS($media) . ':X') < AUTH_READ) {
            return array(403, 'Forbidden');
        }
        $file = mediaFN($media);
    }
    //check file existance
    if (!@file_exists($file)) {
        return array(404, 'Not Found');
    }
    return array(200, null);
}
Ejemplo n.º 5
0
if ($EXT === false) {
    $EXT = 'unknown';
    $MIME = 'application/octet-stream';
    $DL = true;
}
//media to local file
if (preg_match('#^(https?)://#i', $MEDIA)) {
    //check hash
    if (substr(md5(auth_cookiesalt() . $MEDIA), 0, 6) != $_REQUEST['hash']) {
        header("HTTP/1.0 412 Precondition Failed");
        print 'Precondition Failed';
        exit;
    }
    //handle external images
    if (strncmp($MIME, 'image/', 6) == 0) {
        $FILE = media_get_from_URL($MEDIA, $EXT, $CACHE);
    }
    if (!$FILE) {
        //download failed - redirect to original URL
        header('Location: ' . $MEDIA);
        exit;
    }
} else {
    $MEDIA = cleanID($MEDIA);
    if (empty($MEDIA)) {
        header("HTTP/1.0 400 Bad Request");
        print 'Bad request';
        exit;
    }
    //check permissions (namespace only)
    if (auth_quickaclcheck(getNS($MEDIA) . ':X') < AUTH_READ) {