Exported template in an external file
[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 (! is_file($newImgFile))
57         {
58                 $ext = strtolower(substr($imgFile, -4));
59                 if ($ext == ".jpg")
60                         $img = imagecreatefromjpeg($imgFile);
61                 else
62                         $img = imagecreatefrompng($imgFile);
63
64                 $w = imagesx($img);
65                 $h = imagesy($img);
66                 # don't do anything if the image is already small
67                 if ($w <= $maxSize and $h <= $maxSize) {
68                         imagedestroy($img);
69                         return $imgFile;
70                 }
71
72                 # uncomment this if you need group writable files
73                 #umask(0002);
74
75                 # create the thumbs directory recursively
76                 if (! is_dir(dirname($newImgFile))) mkdir(dirname($newImgFile), 0777, true);
77
78                 if ($w > $h) {
79                         $newW = $maxSize;
80                         $newH = $h/($w/$maxSize);
81                 } else {
82                         $newW = $w/($h/$maxSize);
83                         $newH = $maxSize;
84                 }
85
86                 $newImg = imagecreatetruecolor($newW, $newH);
87
88                 imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newW, $newH, $w, $h);
89
90                 if ($ext == ".jpg")
91                         imagejpeg($newImg, $newImgFile);
92                 else
93                         imagepng($newImg, $newImgFile);
94                 
95                 imagedestroy($img);
96                 imagedestroy($newImg);
97         }
98
99         return $GLOBALS['rootUrl'].$newImgFile;
100 }
101
102 function getAlbumPreview($dir)
103 {
104         foreach (scandir($dir) as $file) if ($file != '.' and $file != '..') {
105                 $ext = strtolower(substr($file, -4));
106                 if ($ext == ".jpg" or $ext == ".png")
107                         return getPreview("$dir/$file");
108         }
109
110         return '';
111 }
112
113 // if url == http://localhost/photos/index.php/toto/titi, path_info == /toto/titi
114 // if url == http://localhost/photos/index.php, path_info is not set
115 // if url == http://localhost/photos/, path_info is not set
116 // if path_info is not set, we are at top level, so we redirect to /photos/index.php/
117 if (! isset($_SERVER["PATH_INFO"])) {
118         header("Location: $scriptUrl/");
119         exit();
120 }
121
122 # simplePath is the simple path to the image
123 # /index.php/toto/titi => simplePath == /toto/titi
124 $simplePath = $_SERVER["PATH_INFO"];
125 if ($simplePath == '/') $simplePath = '';
126 // extra security check to avoid /photos/index/../.. like urls, maybe useless but..
127 if (strpos($simplePath, '..') !== false) die(".. found in url");
128
129 $folders = array();
130 $imageFiles = array();
131 $otherFiles = array();
132
133 # realDir is the directory in filesystem
134 # seen from current script directory
135 $realDir = IMAGES_DIR.$simplePath;
136
137 if (! is_dir($realDir)) {
138         header("HTTP/1.1 404 Not Found");
139         die("Directory Not Found");
140 }
141
142 foreach (scandir($realDir) as $file) if ($file != '.' and $file != '..')
143 {
144         if (is_dir("$realDir/$file"))
145         {
146                 $folders[] = array( "name" => $file, "link" => "$scriptUrl$simplePath/$file", "preview" => getAlbumPreview("$realDir/$file") );
147         }
148         else
149         {
150                 $ext = strtolower(substr($file, -4));
151                 if ($ext == ".jpg" or $ext == ".png") {
152                         $imageFiles[] = array( "name" => $file, "url" => getPreview("$realDir/$file"), "link" => getImageLink("$simplePath/$file") );
153                 } else {
154                         $otherFiles[] = array( "name" => $file, "link" => "$rootUrl$realDir/$file" );
155                 }
156         }
157 }
158
159 if (dirname($simplePath) !== '')
160         $parentLink = $scriptUrl.dirname($simplePath);
161 else
162         $parentLink = "";
163
164 ///// template starts here /////
165 header('Content-Type: text/html; charset=utf-8');
166 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
167
168 require 'template.php';
169
170 ?>