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