Added proper 404 errors on file or directory not found
[bizou.git] / index.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('THUMB_SIZE', 100);
21 define('DATA_DIR', 'data');
22 define('IMAGES_DIR', 'images');
23 define('USE_VIEWER', true); # if set to false, you can delete view.php
24
25 function getPreview($imgFile, $maxSize = THUMB_SIZE)
26 {
27         # example: data/myalbum/100.mypic.jpg
28         $newImgFile = DATA_DIR."/".dirname($imgFile)."/".$maxSize.".".basename($imgFile);
29         
30         if (! is_file($newImgFile))
31         {
32                 $ext = strtolower(substr($imgFile, -4));
33                 if ($ext == ".jpg")
34                         $img = imagecreatefromjpeg($imgFile);
35                 else
36                         $img = imagecreatefrompng($imgFile);
37
38                 $w = imagesx($img);
39                 $h = imagesy($img);
40                 # don't do anything if the image is already small
41                 if ($w <= $maxSize and $h <= $maxSize) {
42                         imagedestroy($img);
43                         return $imgFile;
44                 }
45
46                 # create the thumbs directory recursively
47                 if (! is_dir(dirname($newImgFile))) mkdir(dirname($newImgFile), 0777, true);
48
49                 if ($w > $h) {
50                         $newW = $maxSize;
51                         $newH = $h/($w/$maxSize);
52                 } else {
53                         $newW = $w/($h/$maxSize);
54                         $newH = $maxSize;
55                 }
56
57                 $newImg = imagecreatetruecolor($newW, $newH);
58
59                 imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newW, $newH, $w, $h);
60
61                 if ($ext == ".jpg")
62                         imagejpeg($newImg, $newImgFile);
63                 else
64                         imagepng($newImg, $newImgFile);
65                 
66                 imagedestroy($img);
67                 imagedestroy($newImg);
68         }
69
70         return dirname($_SERVER["SCRIPT_NAME"])."/".$newImgFile;
71 }
72
73 function getAlbumPreview($dir)
74 {
75         foreach (scandir($dir) as $file) if ($file != '.' and $file != '..') {
76                 $ext = strtolower(substr($file, -4));
77                 if ($ext == ".jpg" or $ext == ".png")
78                         return getPreview("$dir/$file");
79         }
80
81         return '';
82 }
83
84 $scriptUrlPath = $_SERVER["SCRIPT_NAME"];
85
86 // if url == http://localhost/photos/index/toto/titi, path_info == /toto/titi
87 // if url == http://localhost/photos/index, path_info is not set
88 // if url == http://localhost/photos/, path_info is not set
89 // if path_info is not set, we are at top level, so we redirect to /photos/index/
90 if (! isset($_SERVER["PATH_INFO"])) {
91         header("Location: $scriptUrlPath/");
92         exit();
93 }
94
95 $shortPath = $_SERVER["PATH_INFO"];
96 if ($shortPath == '/') $shortPath = '';
97 // extra security check to avoid /photos/index/../.. like urls, maybe useless but..
98 if (strpos($shortPath, '..') !== false) die(".. found in url");
99
100 $folders = array();
101 $imageFiles = array();
102 $otherFiles = array();
103
104 $realDir = IMAGES_DIR.$shortPath;
105
106 if (! is_dir($realDir)) {
107         header("HTTP/1.1 404 Not Found");
108         die("Directory Not Found");
109 }
110
111 foreach (scandir($realDir) as $file) if ($file != '.' and $file != '..')
112 {
113         if (is_dir("$realDir/$file"))
114         {
115                 $folders[] = array( "name" => $file, "link" => "$scriptUrlPath$shortPath/$file", "preview" => getAlbumPreview("$realDir/$file") );
116         }
117         else
118         {
119                 $ext = strtolower(substr($file, -4));
120                 if ($ext == ".jpg" or $ext == ".png") {
121                         if (USE_VIEWER)
122                                 $link = dirname($scriptUrlPath)."/view.php$shortPath/$file";
123                         else
124                                 $link = dirname($scriptUrlPath)."/$realDir/$file";
125
126                         $imageFiles[] = array( "name" => $file, "url" => getPreview("$realDir/$file"), "link" => $link );
127
128                 } else {
129                         $otherFiles[] = array( "name" => $file, "link" => dirname($scriptUrlPath)."/$realDir/$file" );
130                 }
131         }
132 }
133
134 if (dirname($shortPath) !== '')
135         $parentLink = $scriptUrlPath.dirname($shortPath);
136 else
137         $parentLink = "";
138
139 ?>
140 <?php
141 ///// template starts here /////
142 header('Content-Type: text/html; charset=utf-8');
143 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
144 ?>
145 <html>
146 <head>
147 <style type="text/css">
148 body {
149         padding-top: 2em;
150 }
151 img {
152         border: 0;
153 }
154 a {
155         text-decoration: none;
156 }
157 .square {
158         display: inline-block;
159 }
160 .image {
161         width: <?php echo THUMB_SIZE ?>px;
162         height: <?php echo THUMB_SIZE ?>px;
163         display: table-cell;
164         text-align: center;
165         vertical-align: middle;
166 }
167 .foldername {
168         height: <?php echo THUMB_SIZE ?>px;
169         display: table-cell;
170         vertical-align: middle;
171 }
172 #parentfolder {
173         position: fixed;
174         font-size: 4em;
175         font-weight: bold;
176         top: 0;
177         left: 0;
178 }
179 </style>
180 </head>
181 <body>
182
183 <?php if ($parentLink !== '') { ?>
184         <div id="parentfolder"><a href="<?php echo $parentLink ?>">^</a></div>
185 <?php } ?>
186
187 <?php foreach($folders as $folder) { ?>
188         <div class="folder">
189         <?php if ($folder["preview"] === "") { ?>
190                 <a href="<?php echo $folder["link"] ?>"><?php echo $folder["name"] ?></a>
191         <?php } else { ?>
192                 <div class="square"><div class="image"><a href="<?php echo $folder["link"] ?>"><img src="<?php echo $folder["preview"] ?>" /></a></div></div>
193                 <div class="square"><div class="foldername"><a href="<?php echo $folder["link"] ?>"><?php echo $folder["name"] ?></a></div></div>
194         <?php } ?>
195         </div>
196 <?php } ?>
197
198 <?php foreach ($imageFiles as $file) { ?>
199         <div class="square"><div class="image"><a href="<?php echo $file["link"] ?>"><img src="<?php echo $file["url"] ?>" alt="<?php echo $file["name"] ?>" /></a></div></div>
200 <?php } ?>
201
202 <?php foreach ($otherFiles as $file) { ?>
203         <div class="miscfile"><a href="<?php echo $file["link"] ?>"><?php echo $file["name"] ?></a></div>
204 <?php } ?>
205
206 </body>
207 </html>