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