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