xref: /plugin/panoview/tiles.php (revision 7242a9ed7e44bcf8e359cb451c97c0510206c6fe)
1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Andreas Gohr <gohr@cosmocode.de>
5 */
6
7if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__).'/../../../').'/');
8define('DOKU_DISABLE_GZIP_OUTPUT', 1);
9require_once(DOKU_INC.'inc/init.php');
10session_write_close();
11
12global $conf;
13
14
15/** @var syntax_plugin_panoview $pl */
16$pl = plugin_load('syntax', 'panoview');
17
18$data = array();
19// get parameters
20list($data['zoom'], $data['col'], $data['row']) = explode('-', $_GET['tile']);
21$data['id']    = cleanID($_GET['image']);
22$data['file']  = mediaFN($data['id']);
23$data['mtime'] = @filemtime($data['file']);
24
25// check auth and existance
26if(auth_quickaclcheck(getNS($data['id']).':X') < AUTH_READ) $pl->gfx_error('noauth');
27if(!$data['mtime']) $pl->gfx_error('notfound');
28
29// calculate zoom level scaling
30$data['ts'] = 256;
31list($data['width'], $data['height']) = getimagesize($data['file']);
32$data['scale'] = (int) pow(2, $data['zoom']);
33$data['max']   = max($data['width'], $data['height']);
34$data['inv']   = $data['max'] / ($data['ts'] * $data['scale']);
35
36if($data['inv'] < 0.5) $pl->gfx_error('maxzoom');
37if($data['inv'] < 1.0) $data['inv'] = 1.0; // original size, no upscaling
38
39// calculate tile boundaries
40$data['tlx'] = (int) ($data['col'] * $data['ts'] * $data['inv']);
41$data['tly'] = (int) ($data['row'] * $data['ts'] * $data['inv']);
42$data['brx'] = (int) ($data['tlx'] + ($data['ts'] * $data['inv']));
43$data['bry'] = (int) ($data['tly'] + ($data['ts'] * $data['inv']));
44if($data['tlx'] > $data['width'] || $data['tly'] > $data['height']) $pl->gfx_error('blank');
45
46// cache times
47$data['cache']  = getCacheName($data['file'], '.pv.'.$data['zoom'].'-'.$data['col'].'-'.$data['row'].'.jpg');
48$data['cachet'] = @filemtime($data['cache']);
49
50// (re)generate
51if($data['cachet'] < $data['mtime']) {
52    $pl->tile_lock($data);
53    if($conf['im_convert']) {
54        $pl->tile_im($data);
55    } else {
56        $pl->tile_gd($data);
57    }
58    $pl->tile_unlock($data);
59}
60
61// send
62header('Content-type: image/jpeg');
63http_conditionalRequest(max($data['mtime'], $data['cachet']));
64
65//use x-sendfile header to pass the delivery to compatible webservers
66http_sendfile($data['cache']);
67
68// send file contents
69$fp = @fopen($data['cache'], "rb");
70if($fp) {
71    http_rangeRequest($fp, filesize($data['cache']), 'image/jpeg');
72} else {
73    header("HTTP/1.0 500 Internal Server Error");
74    print "Could not read tile - bad permissions?";
75}
76