Fixed a bug when bizou was operating at the top root of a site
[bizou.git] / plugins / viewer / 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 $bizouRootFromHere = '../..';
21 require "$bizouRootFromHere/config.php";
22
23 $simpleImagePath = $_SERVER["PATH_INFO"];
24 if ($simpleImagePath == '/') $simpleImagePath = '';
25 // extra security check to avoid /photos/index/../.. like urls, maybe useless but..
26 if (strpos($simpleImagePath, '..') !== false) die(".. found in url");
27
28
29 if (! is_file("$bizouRootFromHere/".IMAGES_DIR.$simpleImagePath)) {
30         header("HTTP/1.1 404 Not Found");
31         die("File Not Found");
32 }
33
34 // get all images in an array
35 $images = array();
36
37 $files = scandir("$bizouRootFromHere/".IMAGES_DIR.dirname($simpleImagePath));
38 foreach ($files as $file) {
39         $ext = strtolower(substr($file, -4));
40         if ($ext == ".jpg" or $ext == ".png")
41                 $images[] = $file;
42 }
43
44 // find the image position
45 $pos = array_search(basename($simpleImagePath), $images);
46 if ($pos === false) die("Image not found");
47
48 // get prev and next images
49 $prevImage = '';
50 $nextImage = '';
51 if ($pos > 0)
52         $prevImage = $images[$pos-1];
53 if ($pos < sizeof($images)-1)
54         $nextImage = $images[$pos+1];
55
56 $scriptUrl = $_SERVER["SCRIPT_NAME"];
57 $bizouRootUrl = dirname(dirname(dirname($scriptUrl)));
58 if (substr($bizouRootUrl, -1) !== '/') $bizouRootUrl.='/';  // add a trailing / to rootUrl
59 // scriptUrl = /path/to/bizou/plugins/viewer/view.php
60 // bizouRootUrl = /path/to/bizou/
61
62 // template variables
63 $imageUrl = $bizouRootUrl.IMAGES_DIR.$simpleImagePath;
64
65 if ($nextImage === '') {
66         $nextImageUrl = '';
67         $nextPageUrl = '';
68 } else {
69         $nextImageUrl = $bizouRootUrl.IMAGES_DIR.dirname($simpleImagePath)."/$nextImage";
70         $nextPageUrl = dirname($_SERVER["REQUEST_URI"])."/$nextImage";
71 }
72 if ($prevImage === '') $prevPageUrl = '';
73 else $prevPageUrl = dirname($_SERVER["REQUEST_URI"])."/$prevImage";
74
75 $directoryUrl = $bizouRootUrl."index.php".dirname($simpleImagePath);
76
77 $firefox = strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false;
78
79 header('Content-Type: text/html; charset=utf-8');
80 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
81 ?>
82 <html>
83 <head>
84 <title> <?php echo IMAGES_DIR.$simpleImagePath ?> </title>
85 <style type="text/css">
86 html, body {
87 height: 100%;
88 }
89 body {
90 margin: 0;
91 text-align: center;
92 background: black;
93 color: white;
94 }
95 #theimage {
96 max-width: 100%;
97 max-height: 100%;
98 }
99 a {
100         color: white;
101         text-decoration: none;
102 }
103 #next, #previous, #up {
104         position: fixed;
105         font-size: 4em;
106         font-weight: bold;
107 }
108
109 #up {
110         top: 0;
111         left: 0;
112         
113 }
114 #next {
115         top: 50%;
116         right: -0;
117         
118 }
119 #previous {
120         top: 50%;
121         left: 0;
122 }
123 img {
124         border: 0;
125 }
126 </style>
127
128 <?php if ($nextImageUrl !== '' and $firefox) { ?>
129 <link rel="prefetch" href="<?php echo $nextImageUrl ?>" />
130 <link rel="prefetch" href="<?php echo $nextPageUrl ?>" />
131 <?php } ?>
132
133 </head>
134 <body>
135
136 <a href="<?php echo $imageUrl ?>"><img src="<?php echo $imageUrl ?>" id="theimage" /></a>
137
138 <div id="up">
139 <a href="<?php echo $directoryUrl ?>" title="Back to directory">^</a>
140 </div>
141
142 <?php if ($nextPageUrl !== '') { ?>
143 <div id="next">
144 <a href="<?php echo $nextPageUrl ?>" title="Next image">&gt;</a>
145 </div>
146 <?php } ?>
147
148 <?php if ($prevPageUrl !== '') { ?>
149 <div id="previous">
150 <a href="<?php echo $prevPageUrl ?>" title="Previous image">&lt;</a>
151 </div>
152 <?php } ?>
153
154 <script type="text/javascript">
155
156 <?php if ($nextImageUrl !== '' and ! $firefox) { ?>
157 window.onload = function() { // for browsers not supporting link rel=prefetch
158         var im = new Image();
159         im.src = '<?php echo $nextImageUrl ?>';
160         var req = new XMLHttpRequest();
161         req.open('GET', '<?php echo $nextPageUrl ?>', false);
162         req.send(null);
163 };
164 <?php } ?>
165
166 // keyboard navigation
167 function keyup(e)
168 {
169         switch (e.keyCode) {
170                 case 37: // left
171                         window.location = "<?php echo $prevPageUrl ?>";
172                 break;
173                 case 39: // right
174                 case 32: // space
175                         window.location = "<?php echo $nextPageUrl ?>";
176                 break;
177                 case 38: // up  (down is 40)
178                         window.location = "<?php echo $directoryUrl ?>";
179                 break;
180                 case 13: // enter
181                         window.location = "<?php echo $imageUrl ?>";
182                 break;
183         }
184 }
185
186 if (document.addEventListener) {
187         document.addEventListener("keyup", keyup, false);
188 }
189 </script>
190
191 </body>
192 </html>