No more progress page: page is displayed directly with flushes
[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 the preview is a symlink, image is already good sized
57         if (is_link($newImgFile)) return $imgFile;
58         
59         if (! is_file($newImgFile))
60         {
61                 # this tels the template to flush output after displaying previews
62                 $GLOBALS["generating"] = true;
63
64                 # reset script time limit to 20s (wont work in safe mode)
65                 set_time_limit(20);
66
67                 $ext = strtolower(substr($imgFile, -4));
68                 if ($ext == ".jpg")
69                         $img = imagecreatefromjpeg($imgFile);
70                 else
71                         $img = imagecreatefrompng($imgFile);
72
73                 $w = imagesx($img);
74                 $h = imagesy($img);
75                 # if the image is already small, make a symlink, and return it
76                 if ($w <= $maxSize and $h <= $maxSize) {
77                         imagedestroy($img);
78                         symlink($imgFile, $newImgFile);
79                         return $imgFile;
80                 }
81
82                 # uncomment this if you need group writable files
83                 #umask(0002);
84                 # create the thumbs directory recursively
85                 if (! is_dir(dirname($newImgFile))) mkdir(dirname($newImgFile), 0777, true);
86
87                 if ($w > $h) {
88                         $newW = $maxSize;
89                         $newH = $h/($w/$maxSize);
90                 } else {
91                         $newW = $w/($h/$maxSize);
92                         $newH = $maxSize;
93                 }
94
95                 $newImg = imagecreatetruecolor($newW, $newH);
96
97                 imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newW, $newH, $w, $h);
98
99                 if ($ext == ".jpg")
100                         imagejpeg($newImg, $newImgFile);
101                 else
102                         imagepng($newImg, $newImgFile);
103                 
104                 imagedestroy($img);
105                 imagedestroy($newImg);
106         }
107
108         return $newImgFile;
109 }
110
111 function getAlbumPreview($dir)
112 {
113         $previewFile = DATA_DIR."/$dir/albumpreview";
114
115         if (is_file("$previewFile.jpg")) {
116                 return "$previewFile.jpg";
117         } else if (is_file("$previewFile.empty")) {
118                 return "";
119         } else if (is_file("$previewFile.png")) {
120                 return "$previewFile.png";
121         } else {
122                 # uncomment this if you need group writable files
123                 #umask(0002);
124                 # create the thumbs directory recursively
125                 if (! is_dir(dirname($previewFile))) mkdir(dirname($previewFile), 0777, true);
126
127                 // no preview: look for a preview in current dir, write it, return it
128                 foreach (scandir($dir) as $file) if ($file != '.' and $file != '..') {
129                         $ext = strtolower(substr($file, -4));
130                         if ($ext == ".jpg" or $ext == ".png") {
131                                 $thumb = getPreview("$dir/$file");
132                                 copy($thumb, $previewFile.$ext);
133                                 return $previewFile.$ext;
134                         } else if (is_dir("$dir/$file")) {
135                                 $subPreview = getAlbumPreview("$dir/$file");
136                                 if ($subPreview) {
137                                         $myPreview = dirname($previewFile)."/".basename($subPreview);
138                                         copy($subPreview, $myPreview);
139                                         return $myPreview;
140                                 }
141                         }
142                 }
143
144                 // nothing found. create empty file
145                 touch("$previewFile.empty");
146                 return "";
147         }
148 }
149
150 // if url == http://localhost/photos/index.php/toto/titi, path_info == /toto/titi
151 // if url == http://localhost/photos/index.php, path_info is not set
152 // if url == http://localhost/photos/, path_info is not set
153 // if path_info is not set, we are at top level, so we redirect to /photos/index.php/
154 if (! isset($_SERVER["PATH_INFO"])) {
155         header("Location: $scriptUrl/");
156         exit();
157 }
158
159 # simplePath is the simple path to the image
160 # /index.php/toto/titi => simplePath == /toto/titi
161 $simplePath = $_SERVER["PATH_INFO"];
162 if ($simplePath == '/') $simplePath = '';
163 // extra security check to avoid /photos/index/../.. like urls, maybe useless but..
164 if (strpos($simplePath, '..') !== false) die(".. found in url");
165
166 $folders = array();
167 $imageFiles = array();
168 $otherFiles = array();
169
170 # realDir is the directory in filesystem
171 # seen from current script directory
172 $realDir = IMAGES_DIR.$simplePath;
173
174 if (! is_dir($realDir)) {
175         header("HTTP/1.1 404 Not Found");
176         die("Directory Not Found");
177 }
178
179 foreach (scandir($realDir) as $file) if ($file != '.' and $file != '..')
180 {
181         if (is_dir("$realDir/$file"))
182         {
183                 $folders[] = array( "name" => $file, "file" => "$realDir/$file", "link" => "$scriptUrl$simplePath/$file" );
184         }
185         else
186         {
187                 $ext = strtolower(substr($file, -4));
188                 if ($ext == ".jpg" or $ext == ".png") {
189                         $imageFiles[] = array( "name" => $file, "file" => "$realDir/$file", "link" => getImageLink("$simplePath/$file") );
190                 } else {
191                         $otherFiles[] = array( "name" => $file, "link" => "$rootUrl$realDir/$file" );
192                 }
193         }
194 }
195
196 if (dirname($simplePath) !== '')
197         $parentLink = $scriptUrl.dirname($simplePath);
198 else
199         $parentLink = "";
200
201 ///// template starts here /////
202 header('Content-Type: text/html; charset=utf-8');
203 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
204
205 require 'template.php';
206
207 ?>