Added config constant to use view.php or not
[bizou.git] / index.php
1 <?php
2
3 define('THUMB_SIZE', 100);
4 define('DATA_DIR', 'data');
5 define('IMAGES_DIR', 'images');
6 define('USE_VIEWER', true);
7
8 function getPreview($imgFile, $maxSize = THUMB_SIZE)
9 {
10         # example: data/myalbum/100.mypic.jpg
11         $newImgFile = DATA_DIR."/".dirname($imgFile)."/".$maxSize.".".basename($imgFile);
12         
13         if (! is_file($newImgFile))
14         {
15                 $ext = strtolower(substr($imgFile, -4));
16                 if ($ext == ".jpg")
17                         $img = imagecreatefromjpeg($imgFile);
18                 else
19                         $img = imagecreatefrompng($imgFile);
20
21                 $w = imagesx($img);
22                 $h = imagesy($img);
23                 # don't do anything if the image is already small
24                 if ($w <= $maxSize and $h <= $maxSize) {
25                         imagedestroy($img);
26                         return $imgFile;
27                 }
28
29                 # create the thumbs directory recursively
30                 if (! is_dir(dirname($newImgFile))) mkdir(dirname($newImgFile), 0777, true);
31
32                 if ($w > $h) {
33                         $newW = $maxSize;
34                         $newH = $h/($w/$maxSize);
35                 } else {
36                         $newW = $w/($h/$maxSize);
37                         $newH = $maxSize;
38                 }
39
40                 $newImg = imagecreatetruecolor($newW, $newH);
41
42                 imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newW, $newH, $w, $h);
43
44                 if ($ext == ".jpg")
45                         imagejpeg($newImg, $newImgFile);
46                 else
47                         imagepng($newImg, $newImgFile);
48                 
49                 imagedestroy($img);
50                 imagedestroy($newImg);
51         }
52
53         return dirname($_SERVER["SCRIPT_NAME"])."/".$newImgFile;
54 }
55
56 function getAlbumPreview($dir)
57 {
58         foreach (scandir($dir) as $file) if ($file != '.' and $file != '..') {
59                 $ext = strtolower(substr($file, -4));
60                 if ($ext == ".jpg" or $ext == ".png")
61                         return getPreview("$dir/$file");
62         }
63
64         return '';
65 }
66
67 $scriptUrlPath = $_SERVER["SCRIPT_NAME"];
68
69 // if url == http://localhost/photos/index/toto/titi, path_info == /toto/titi
70 // if url == http://localhost/photos/index, path_info is not set
71 // if url == http://localhost/photos/, path_info is not set
72 // if path_info is not set, we are at top level, so we redirect to /photos/index/
73 if (! isset($_SERVER["PATH_INFO"])) {
74         header("Location: $scriptUrlPath/");
75         exit();
76 }
77
78 $shortPath = $_SERVER["PATH_INFO"];
79 if ($shortPath == '/') $shortPath = '';
80 // extra security check to avoid /photos/index/../.. like urls, maybe useless but..
81 if (strpos($shortPath, '..') !== false) die(".. found in url");
82
83 $folders = array();
84 $imageFiles = array();
85 $otherFiles = array();
86
87 $realDir = IMAGES_DIR.$shortPath;
88
89 foreach (scandir($realDir) as $file) if ($file != '.' and $file != '..')
90 {
91         if (is_dir("$realDir/$file"))
92         {
93                 $folders[] = array( "name" => $file, "link" => "$scriptUrlPath$shortPath/$file", "preview" => getAlbumPreview("$realDir/$file") );
94         }
95         else
96         {
97                 $ext = strtolower(substr($file, -4));
98                 if ($ext == ".jpg" or $ext == ".png") {
99                         if (USE_VIEWER)
100                                 $link = dirname($scriptUrlPath)."/view.php$shortPath/$file";
101                         else
102                                 $link = dirname($scriptUrlPath)."/$realDir/$file";
103
104                         $imageFiles[] = array( "name" => $file, "url" => getPreview("$realDir/$file"), "link" => $link );
105
106                 } else {
107                         $otherFiles[] = array( "name" => $file, "link" => dirname($scriptUrlPath)."/$realDir/$file" );
108                 }
109         }
110 }
111
112 if (dirname($shortPath) !== '')
113         $parentLink = $scriptUrlPath.dirname($shortPath);
114 else
115         $parentLink = "";
116
117 ?>
118 <?php
119 ///// template starts here /////
120 header('Content-Type: text/html; charset=utf-8');
121 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
122 ?>
123 <html>
124 <head>
125 <style type="text/css">
126 body {
127         padding-top: 2em;
128 }
129 img {
130         border: 0;
131 }
132 a {
133         text-decoration: none;
134 }
135 .square {
136         display: inline-block;
137 }
138 .image {
139         width: <?php echo THUMB_SIZE ?>px;
140         height: <?php echo THUMB_SIZE ?>px;
141         display: table-cell;
142         text-align: center;
143         vertical-align: middle;
144 }
145 .foldername {
146         height: <?php echo THUMB_SIZE ?>px;
147         display: table-cell;
148         vertical-align: middle;
149 }
150 #parentfolder {
151         position: fixed;
152         font-size: 4em;
153         font-weight: bold;
154         top: 0;
155         left: 0;
156 }
157 </style>
158 </head>
159 <body>
160
161 <?php if ($parentLink !== '') { ?>
162         <div id="parentfolder"><a href="<?php echo $parentLink ?>">^</a></div>
163 <?php } ?>
164
165 <?php foreach($folders as $folder) { ?>
166         <div class="folder">
167         <?php if ($folder["preview"] === "") { ?>
168                 <a href="<?php echo $folder["link"] ?>"><?php echo $folder["name"] ?></a>
169         <?php } else { ?>
170                 <div class="square"><div class="image"><a href="<?php echo $folder["link"] ?>"><img src="<?php echo $folder["preview"] ?>" /></a></div></div>
171                 <div class="square"><div class="foldername"><a href="<?php echo $folder["link"] ?>"><?php echo $folder["name"] ?></a></div></div>
172         <?php } ?>
173         </div>
174 <?php } ?>
175
176 <?php foreach ($imageFiles as $file) { ?>
177         <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>
178 <?php } ?>
179
180 <?php foreach ($otherFiles as $file) { ?>
181         <div class="miscfile"><a href="<?php echo $file["link"] ?>"><?php echo $file["name"] ?></a></div>
182 <?php } ?>
183
184 </body>
185 </html>