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