Some typo simplification for the getThumbTarget call
[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                         $imageFiles[] = array( "name" => $file, "url" => getPreview("$realDir/$file"), "link" => getThumbTarget("$shortPath/$file") );
142                 } else {
143                         $otherFiles[] = array( "name" => $file, "link" => dirname($scriptUrlPath)."/$realDir/$file" );
144                 }
145         }
146 }
147
148 if (dirname($shortPath) !== '')
149         $parentLink = $scriptUrlPath.dirname($shortPath);
150 else
151         $parentLink = "";
152
153 ?>
154 <?php
155 ///// template starts here /////
156 header('Content-Type: text/html; charset=utf-8');
157 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
158 ?>
159 <html>
160 <head>
161 <style type="text/css">
162 body {
163         padding-top: 2em;
164 }
165 img {
166         border: 0;
167 }
168 a {
169         text-decoration: none;
170 }
171 .square {
172         display: inline-block;
173 }
174 .image {
175         width: <?php echo THUMB_SIZE ?>px;
176         height: <?php echo THUMB_SIZE ?>px;
177         display: table-cell;
178         text-align: center;
179         vertical-align: middle;
180 }
181 .foldername {
182         height: <?php echo THUMB_SIZE ?>px;
183         display: table-cell;
184         vertical-align: middle;
185 }
186 #parentfolder {
187         position: fixed;
188         font-size: 4em;
189         font-weight: bold;
190         top: 0;
191         left: 0;
192 }
193 </style>
194 <?php foreach ($plugins as $p) if (is_file("plugins/$p/style.css")) { ?>
195         <link rel="stylesheet" type="text/css" href="<?php echo dirname($scriptUrlPath)."/plugins/$p/style.css" ?>" />
196 <?php } ?>
197 </head>
198 <body>
199
200 <?php if ($parentLink !== '') { ?>
201         <div id="parentfolder"><a href="<?php echo $parentLink ?>">^</a></div>
202 <?php } ?>
203
204 <?php plugins_include("before_content.php") ?>
205
206 <?php foreach($folders as $folder) { ?>
207         <div class="folder">
208         <?php if ($folder["preview"] === "") { ?>
209                 <a href="<?php echo $folder["link"] ?>"><?php echo $folder["name"] ?></a>
210         <?php } else { ?>
211                 <div class="square"><div class="image"><a href="<?php echo $folder["link"] ?>"><img src="<?php echo $folder["preview"] ?>" /></a></div></div>
212                 <div class="square"><div class="foldername"><a href="<?php echo $folder["link"] ?>"><?php echo $folder["name"] ?></a></div></div>
213         <?php } ?>
214         </div>
215 <?php } ?>
216
217 <?php foreach ($imageFiles as $file) { ?>
218         <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>
219 <?php } ?>
220
221 <?php foreach ($otherFiles as $file) { ?>
222         <div class="miscfile"><a href="<?php echo $file["link"] ?>"><?php echo $file["name"] ?></a></div>
223 <?php } ?>
224
225 </body>
226 </html>