Added proper 404 errors on file or directory not found
[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 if (! is_file(IMAGES_DIR.$shortPath)) {
28         header("HTTP/1.1 404 Not Found");
29         die("File Not Found");
30 }
31
32 $scriptPath = $_SERVER["SCRIPT_NAME"];
33
34 // get all images in an array
35 $images = array();
36
37 $files = scandir(IMAGES_DIR.dirname($shortPath));
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($shortPath), $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 // template variables
57 $imageUrl = dirname($scriptPath)."/".IMAGES_DIR.$shortPath;
58
59 if ($nextImage === '') {
60         $nextImageUrl = '';
61         $nextPageUrl = '';
62 } else {
63         $nextImageUrl = dirname($scriptPath)."/".IMAGES_DIR.dirname($shortPath)."/$nextImage";
64         $nextPageUrl = dirname($_SERVER["REQUEST_URI"])."/$nextImage";
65 }
66 if ($prevImage === '') $prevPageUrl = '';
67 else $prevPageUrl = dirname($_SERVER["REQUEST_URI"])."/$prevImage";
68
69 $directoryUrl = dirname($_SERVER["SCRIPT_NAME"])."/index.php".dirname($shortPath);
70
71 header('Content-Type: text/html; charset=utf-8');
72 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
73 ?>
74 <html>
75 <head>
76 <style type="text/css">
77 html, body {
78 height: 100%;
79 }
80 body {
81 margin: 0;
82 text-align: center;
83 background: black;
84 color: white;
85 }
86 #theimage {
87 max-width: 100%;
88 max-height: 100%;
89 }
90 a {
91         color: white;
92         text-decoration: none;
93 }
94 #next, #previous, #up {
95         position: fixed;
96         font-size: 4em;
97         font-weight: bold;
98 }
99
100 #up {
101         top: 0;
102         left: 0;
103         
104 }
105 #next {
106         top: 50%;
107         right: -0;
108         
109 }
110 #previous {
111         top: 50%;
112         left: 0;
113 }
114 img {
115         border: 0;
116 }
117 </style>
118
119 <?php if ($nextImageUrl !== '') { ?>
120  <?php if (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false) { ?>
121 <link rel="prefetch" href="<?php echo $nextImageUrl ?>" />
122 <link rel="prefetch" href="<?php echo $nextPageUrl ?>" />
123
124  <?php } else { ?>
125 <script type="text/javascript">
126 window.onload = function() {
127         var im = new Image();
128         im.src = '<?php echo $nextImageUrl ?>';
129         var req = new XMLHttpRequest();
130         req.open('GET', '<?php echo $nextPageUrl ?>', false);
131         req.send(null);
132 };
133 </script>
134  <?php } ?>
135 <?php } ?>
136
137 </head>
138 <body>
139
140 <a href="<?php echo $imageUrl ?>"><img src="<?php echo $imageUrl ?>" id="theimage" /></a>
141
142 <div id="up">
143 <a href="<?php echo $directoryUrl ?>" title="Back to directory">^</a>
144 </div>
145
146 <?php if ($nextPageUrl !== '') { ?>
147 <div id="next">
148 <a href="<?php echo $nextPageUrl ?>" title="Next image">&gt;</a>
149 </div>
150 <?php } ?>
151
152 <?php if ($prevPageUrl !== '') { ?>
153 <div id="previous">
154 <a href="<?php echo $prevPageUrl ?>" title="Previous image">&lt;</a>
155 </div>
156 <?php } ?>
157
158 </body>
159 </html>