Added prefetch support trick for non Firefox browsers
[bizou.git] / view.php
1 <?php
2 /*
3     Bizou - a (french) KISS php image gallery
4     Copyright (C) 2010  Marc MAURICE
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 define('IMAGES_DIR', 'images');
21
22 $shortPath = $_SERVER["PATH_INFO"];
23 if ($shortPath == '/') $shortPath = '';
24 // extra security check to avoid /photos/index/../.. like urls, maybe useless but..
25 if (strpos($shortPath, '..') !== false) die(".. found in url");
26
27 $scriptPath = $_SERVER["SCRIPT_NAME"];
28
29 // get all images in an array
30 $images = array();
31
32 $files = scandir(IMAGES_DIR.dirname($shortPath));
33 foreach ($files as $file) {
34         $ext = strtolower(substr($file, -4));
35         if ($ext == ".jpg" or $ext == ".png")
36                 $images[] = $file;
37 }
38
39 // find the image position
40 $pos = array_search(basename($shortPath), $images);
41 if ($pos === false) die("Image not found");
42
43 // get prev and next images
44 $prevImage = '';
45 $nextImage = '';
46 if ($pos > 0)
47         $prevImage = $images[$pos-1];
48 if ($pos < sizeof($images)-1)
49         $nextImage = $images[$pos+1];
50
51 // template variables
52 $imageUrl = dirname($scriptPath)."/".IMAGES_DIR.$shortPath;
53
54 if ($nextImage === '') {
55         $nextImageUrl = '';
56         $nextPageUrl = '';
57 } else {
58         $nextImageUrl = dirname($scriptPath)."/".IMAGES_DIR.dirname($shortPath)."/$nextImage";
59         $nextPageUrl = dirname($_SERVER["REQUEST_URI"])."/$nextImage";
60 }
61 if ($prevImage === '') $prevPageUrl = '';
62 else $prevPageUrl = dirname($_SERVER["REQUEST_URI"])."/$prevImage";
63
64 $directoryUrl = dirname($_SERVER["SCRIPT_NAME"])."/index.php".dirname($shortPath);
65
66 header('Content-Type: text/html; charset=utf-8');
67 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
68 ?>
69 <html>
70 <head>
71 <style type="text/css">
72 html, body {
73 height: 100%;
74 }
75 body {
76 margin: 0;
77 text-align: center;
78 background: black;
79 color: white;
80 }
81 #theimage {
82 max-width: 100%;
83 max-height: 100%;
84 }
85 a {
86         color: white;
87         text-decoration: none;
88 }
89 #next, #previous, #up {
90         position: fixed;
91         font-size: 4em;
92         font-weight: bold;
93 }
94
95 #up {
96         top: 0;
97         left: 0;
98         
99 }
100 #next {
101         top: 50%;
102         right: -0;
103         
104 }
105 #previous {
106         top: 50%;
107         left: 0;
108 }
109 img {
110         border: 0;
111 }
112 </style>
113
114 <?php if ($nextImageUrl !== '') { ?>
115  <?php if (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false) { ?>
116 <link rel="prefetch" href="<?php echo $nextImageUrl ?>" />
117 <link rel="prefetch" href="<?php echo $nextPageUrl ?>" />
118
119  <?php } else { ?>
120 <script type="text/javascript">
121 window.onload = function() {
122         var im = new Image();
123         im.src = '<?php echo $nextImageUrl ?>';
124         var req = new XMLHttpRequest();
125         req.open('GET', '<?php echo $nextPageUrl ?>', false);
126         req.send(null);
127 };
128 </script>
129  <?php } ?>
130 <?php } ?>
131
132 </head>
133 <body>
134
135 <a href="<?php echo $imageUrl ?>"><img src="<?php echo $imageUrl ?>" id="theimage" /></a>
136
137 <div id="up">
138 <a href="<?php echo $directoryUrl ?>" title="Back to directory">^</a>
139 </div>
140
141 <?php if ($nextPageUrl !== '') { ?>
142 <div id="next">
143 <a href="<?php echo $nextPageUrl ?>" title="Next image">&gt;</a>
144 </div>
145 <?php } ?>
146
147 <?php if ($prevPageUrl !== '') { ?>
148 <div id="previous">
149 <a href="<?php echo $prevPageUrl ?>" title="Previous image">&lt;</a>
150 </div>
151 <?php } ?>
152
153 </body>
154 </html>