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