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