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