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