added a commented line to change the umask of created files
[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                 # uncomment this if you need group writable files
73                 #umask(0002);
74
75                 # create the thumbs directory recursively
76                 if (! is_dir(dirname($newImgFile))) mkdir(dirname($newImgFile), 0777, true);
77
78                 if ($w > $h) {
79                         $newW = $maxSize;
80                         $newH = $h/($w/$maxSize);
81                 } else {
82                         $newW = $w/($h/$maxSize);
83                         $newH = $maxSize;
84                 }
85
86                 $newImg = imagecreatetruecolor($newW, $newH);
87
88                 imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newW, $newH, $w, $h);
89
90                 if ($ext == ".jpg")
91                         imagejpeg($newImg, $newImgFile);
92                 else
93                         imagepng($newImg, $newImgFile);
94                 
95                 imagedestroy($img);
96                 imagedestroy($newImg);
97         }
98
99         return $GLOBALS['rootUrl'].$newImgFile;
100 }
101
102 function getAlbumPreview($dir)
103 {
104         foreach (scandir($dir) as $file) if ($file != '.' and $file != '..') {
105                 $ext = strtolower(substr($file, -4));
106                 if ($ext == ".jpg" or $ext == ".png")
107                         return getPreview("$dir/$file");
108         }
109
110         return '';
111 }
112
113 // if url == http://localhost/photos/index.php/toto/titi, path_info == /toto/titi
114 // if url == http://localhost/photos/index.php, path_info is not set
115 // if url == http://localhost/photos/, path_info is not set
116 // if path_info is not set, we are at top level, so we redirect to /photos/index.php/
117 if (! isset($_SERVER["PATH_INFO"])) {
118         header("Location: $scriptUrl/");
119         exit();
120 }
121
122 # simplePath is the simple path to the image
123 # /index.php/toto/titi => simplePath == /toto/titi
124 $simplePath = $_SERVER["PATH_INFO"];
125 if ($simplePath == '/') $simplePath = '';
126 // extra security check to avoid /photos/index/../.. like urls, maybe useless but..
127 if (strpos($simplePath, '..') !== false) die(".. found in url");
128
129 $folders = array();
130 $imageFiles = array();
131 $otherFiles = array();
132
133 # realDir is the directory in filesystem
134 # seen from current script directory
135 $realDir = IMAGES_DIR.$simplePath;
136
137 if (! is_dir($realDir)) {
138         header("HTTP/1.1 404 Not Found");
139         die("Directory Not Found");
140 }
141
142 foreach (scandir($realDir) as $file) if ($file != '.' and $file != '..')
143 {
144         if (is_dir("$realDir/$file"))
145         {
146                 $folders[] = array( "name" => $file, "link" => "$scriptUrl$simplePath/$file", "preview" => getAlbumPreview("$realDir/$file") );
147         }
148         else
149         {
150                 $ext = strtolower(substr($file, -4));
151                 if ($ext == ".jpg" or $ext == ".png") {
152                         $imageFiles[] = array( "name" => $file, "url" => getPreview("$realDir/$file"), "link" => getImageLink("$simplePath/$file") );
153                 } else {
154                         $otherFiles[] = array( "name" => $file, "link" => "$rootUrl$realDir/$file" );
155                 }
156         }
157 }
158
159 if (dirname($simplePath) !== '')
160         $parentLink = $scriptUrl.dirname($simplePath);
161 else
162         $parentLink = "";
163
164 ?>
165 <?php
166 ///// template starts here /////
167 header('Content-Type: text/html; charset=utf-8');
168 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
169 ?>
170 <html>
171 <head>
172 <title> <?php echo $realDir ?> </title>
173 <style type="text/css">
174 body {
175         margin-top: 0;
176         font-family: sans-serif;
177 }
178 img {
179         border: 0;
180 }
181 a {
182         text-decoration: none;
183 }
184 .square {
185         display: inline-block;
186 }
187 .image, .foldername, .image_nopreview, .foldername_nopreview {
188         display: table-cell;
189         vertical-align: middle;
190 }
191 .image, .image_nopreview {
192         width: <?php echo THUMB_SIZE ?>px;
193         text-align: center;
194 }
195 .image, .foldername {
196         height: <?php echo THUMB_SIZE ?>px;
197 }
198 .foldername, .foldername_nopreview {
199         padding-left: 1ex;
200 }
201 #parentfolder {
202         font-size: 4em;
203         font-weight: bold;
204         height: 0.6em;
205 }
206 #credit {
207         text-align: right;
208         font-size: 0.25cm;
209         color: gray;
210 }
211 </style>
212 <?php foreach ($plugins as $p) if (is_file("plugins/$p/style.css")) { ?>
213         <link rel="stylesheet" type="text/css" href="<?php echo $rootUrl."plugins/$p/style.css" ?>" />
214 <?php } ?>
215 </head>
216 <body>
217
218 <div id="parentfolder"><a href="<?php echo $parentLink ?>">
219 <?php if ($parentLink !== '') { ?>
220 ^
221 <?php } ?>
222 &nbsp;</a></div>
223
224 <?php plugins_include("before_content.php") ?>
225
226 <?php foreach($folders as $folder) { ?>
227         <div class="folder">
228         <?php if ($folder["preview"] === "") { ?>
229                 <div class="square"><div class="image_nopreview"> - </div></div>
230                 <div class="square"><div class="foldername_nopreview"> <a href="<?php echo $folder["link"] ?>"><?php echo $folder["name"] ?></a> </div></div>
231         <?php } else { ?>
232                 <div class="square"><div class="image"> <a href="<?php echo $folder["link"] ?>"><img src="<?php echo $folder["preview"] ?>" /></a> </div></div>
233                 <div class="square"><div class="foldername"> <a href="<?php echo $folder["link"] ?>"><?php echo $folder["name"] ?></a> </div></div>
234         <?php } ?>
235         </div>
236 <?php } ?>
237
238 <?php foreach ($imageFiles as $file) { ?>
239         <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>
240 <?php } ?>
241
242 <?php foreach ($otherFiles as $file) { ?>
243         <div class="miscfile"><a href="<?php echo $file["link"] ?>"><?php echo $file["name"] ?></a></div>
244 <?php } ?>
245
246 <p id="credit">
247 Generated by <a href="http://www.positon.org/bizou/">Bizou</a>
248 </p>
249
250 </body>
251 </html>