Renammed scriptUrlPath to scriptUrl and added rootUrl
[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('getImageLink')) {
35 function getImageLink($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 $scriptUrl = $_SERVER["SCRIPT_NAME"];
101 $rootUrl = dirname($scriptUrl);
102 // $scriptUrl =  "/path/to/bizou/index.php"
103 // $rootUrl =  "/path/to/bizou"
104
105 // if url == http://localhost/photos/index.php/toto/titi, path_info == /toto/titi
106 // if url == http://localhost/photos/index.php, path_info is not set
107 // if url == http://localhost/photos/, path_info is not set
108 // if path_info is not set, we are at top level, so we redirect to /photos/index.php/
109 if (! isset($_SERVER["PATH_INFO"])) {
110         header("Location: $scriptUrl/");
111         exit();
112 }
113
114 # simplePath is the simple path to the image
115 # /index.php/toto/titi => simplePath == /toto/titi
116 $simplePath = $_SERVER["PATH_INFO"];
117 if ($simplePath == '/') $simplePath = '';
118 // extra security check to avoid /photos/index/../.. like urls, maybe useless but..
119 if (strpos($simplePath, '..') !== false) die(".. found in url");
120
121 $folders = array();
122 $imageFiles = array();
123 $otherFiles = array();
124
125 # realDir is the directory in filesystem
126 # seen from current script directory
127 $realDir = IMAGES_DIR.$simplePath;
128
129 if (! is_dir($realDir)) {
130         header("HTTP/1.1 404 Not Found");
131         die("Directory Not Found");
132 }
133
134 foreach (scandir($realDir) as $file) if ($file != '.' and $file != '..')
135 {
136         if (is_dir("$realDir/$file"))
137         {
138                 $folders[] = array( "name" => $file, "link" => "$scriptUrl$simplePath/$file", "preview" => getAlbumPreview("$realDir/$file") );
139         }
140         else
141         {
142                 $ext = strtolower(substr($file, -4));
143                 if ($ext == ".jpg" or $ext == ".png") {
144                         $imageFiles[] = array( "name" => $file, "url" => getPreview("$realDir/$file"), "link" => getImageLink("$simplePath/$file") );
145                 } else {
146                         $otherFiles[] = array( "name" => $file, "link" => "$rootUrl/$realDir/$file" );
147                 }
148         }
149 }
150
151 if (dirname($simplePath) !== '')
152         $parentLink = $scriptUrl.dirname($simplePath);
153 else
154         $parentLink = "";
155
156 ?>
157 <?php
158 ///// template starts here /////
159 header('Content-Type: text/html; charset=utf-8');
160 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
161 ?>
162 <html>
163 <head>
164 <style type="text/css">
165 body {
166         padding-top: 2em;
167 }
168 img {
169         border: 0;
170 }
171 a {
172         text-decoration: none;
173 }
174 .square {
175         display: inline-block;
176 }
177 .image {
178         width: <?php echo THUMB_SIZE ?>px;
179         height: <?php echo THUMB_SIZE ?>px;
180         display: table-cell;
181         text-align: center;
182         vertical-align: middle;
183 }
184 .foldername {
185         height: <?php echo THUMB_SIZE ?>px;
186         display: table-cell;
187         vertical-align: middle;
188 }
189 #parentfolder {
190         position: fixed;
191         font-size: 4em;
192         font-weight: bold;
193         top: 0;
194         left: 0;
195 }
196 </style>
197 <?php foreach ($plugins as $p) if (is_file("plugins/$p/style.css")) { ?>
198         <link rel="stylesheet" type="text/css" href="<?php echo "$rootUrl/plugins/$p/style.css" ?>" />
199 <?php } ?>
200 </head>
201 <body>
202
203 <?php if ($parentLink !== '') { ?>
204         <div id="parentfolder"><a href="<?php echo $parentLink ?>">^</a></div>
205 <?php } ?>
206
207 <?php plugins_include("before_content.php") ?>
208
209 <?php foreach($folders as $folder) { ?>
210         <div class="folder">
211         <?php if ($folder["preview"] === "") { ?>
212                 <a href="<?php echo $folder["link"] ?>"><?php echo $folder["name"] ?></a>
213         <?php } else { ?>
214                 <div class="square"><div class="image"><a href="<?php echo $folder["link"] ?>"><img src="<?php echo $folder["preview"] ?>" /></a></div></div>
215                 <div class="square"><div class="foldername"><a href="<?php echo $folder["link"] ?>"><?php echo $folder["name"] ?></a></div></div>
216         <?php } ?>
217         </div>
218 <?php } ?>
219
220 <?php foreach ($imageFiles as $file) { ?>
221         <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>
222 <?php } ?>
223
224 <?php foreach ($otherFiles as $file) { ?>
225         <div class="miscfile"><a href="<?php echo $file["link"] ?>"><?php echo $file["name"] ?></a></div>
226 <?php } ?>
227
228 </body>
229 </html>