Example #1
0
 static function output($request)
 {
     $x = explode('/', $request);
     $id = (int) array_shift($x);
     $name = array_pop($x);
     $param = array();
     foreach ($x as $value) {
         $y = explode('-', $value, 2);
         $param[$y[0]] = isset($y[1]) ? $y[1] : true;
     }
     $File = $RequestedFile = dbFile($id);
     if (!$File->exists()) {
         header("HTTP/1.1 404 Not Found");
         exit;
     }
     if (!$File->access()) {
         header("HTTP/1.1 401 Unauthorized");
         exit;
     }
     header("HTTP/1.1 200 OK");
     // Header
     $mime = $File->mime() ? $File->mime() : extensionToMime($File->extension());
     header('Content-Type: ' . $mime);
     header("Last-Modified: " . gmdate("D, d M Y H:i:s", $File->mtime()) . " GMT");
     header("Pragma: private");
     header("Expires: " . gmdate("D, d M Y H:i:s", time() + 60 * 60 * 24 * 364) . " GMT");
     header("Cache-Control: store, cache, max-age=100000, must-revalidate");
     if (Image::able($File->path)) {
         if (1 || isset($param['h']) || isset($param['w'])) {
             $w = isset($param['w']) ? (int) $param['w'] : 0;
             $h = isset($param['h']) ? (int) $param['h'] : 0;
             //if ($w == 0 && $h == 0) { $w = 200; }
             if (isset($_COOKIE['q1_dpr']) && $_COOKIE['q1_dpr'] > 1) {
                 if (isset($param['dpr']) ? $param['dpr'] : G()->SET['qg']['dbFile_dpr_dependent']->v) {
                     $w *= (double) $_COOKIE['q1_dpr'];
                     $h *= (double) $_COOKIE['q1_dpr'];
                 }
             }
             $w = min($w, 9000);
             $h = min($h, 9000);
             $q = (int) isset($param['q']) ? $param['q'] : 92;
             $max = isset($param['max']) ? (bool) $param['max'] : false;
             $vpos = (int) isset($param['vpos']) ? $param['vpos'] : 20;
             $hpos = (int) isset($param['hpos']) ? $param['hpos'] : 50;
             $zoom = (int) isset($param['zoom']) ? $param['zoom'] : 0;
             $type = str_replace('image/', '', $File->mime());
             //$nFile = new File(appPATH.'cache/pri/dbfile'.$id.'.'.$w.'.'.$h.'.'.$q.'.'.$max.'.'.$vpos.'.'.$hpos.'.'.$zoom);
             $nFile = new File(appPATH . 'cache/pri/dbfile_img_' . sha1($request . '|' . $w . '|' . $h));
             if (!$nFile->exists() || $File->mtime() > $nFile->mtime()) {
                 $Img = new Image($File->path);
                 if ($w == 0 && $h == 0) {
                     $w = $Img->x();
                 }
                 if ($max || $h == 0 || $w == 0) {
                     $Img = $Img->getResized($w, $h, true);
                 } else {
                     $Img = $Img->getAutoCroped($w, $h, $vpos, $hpos, $zoom);
                 }
                 qg::fire('qg::dbfile-image', array('Img' => $Img, 'id' => $id, 'param' => $param));
                 $Img->saveAs($nFile->path, $type, $q);
             }
             header('Content-Type: image/' . $type);
             $File = $nFile;
         }
         //header("Pragma: public"); // Emails!
         header("Pragma: private");
         // required
     } elseif (preg_match('/\\.pdf$/', $name) || $File->mime() == 'application/pdf') {
         header('Content-Type: application/pdf');
         header('Content-Disposition: inline; filename="' . $RequestedFile->name() . '";');
         header('Expires: 0');
         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
         header('Pragma: public');
     }
     if (isset($param['as']) && ($param['as'] = 'text')) {
         header("Content-Type: text/html");
     }
     if (isset($param['dl'])) {
         header('Pragma: public');
         // required!
         header('Expires: 0');
         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
         header('Cache-Control: private', false);
         // required for certain browsers
         header('Content-Type: application/force-download');
         header('Content-Disposition: attachment; filename="' . $RequestedFile->name() . '";');
         header('Content-Transfer-Encoding: binary');
     }
     session_write_close();
     // useful?
     $etag = 'qg' . $File->mtime();
     if (!isset($_SERVER['HTTP_IF_NONE_MATCH']) || $_SERVER['HTTP_IF_NONE_MATCH'] !== $etag) {
         header('ETag: ' . $etag);
         /* rangeDownload http://mobiforge.com/developing/story/content-delivery-mobile-devices */
         header('Content-Length: ' . $File->size());
         flush();
         while (ob_get_level()) {
             ob_end_flush();
         }
         $File->read();
     } else {
         header("HTTP/1.1 304 Not Modified");
     }
     exit;
 }