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