redirect when path_info is not set
[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
70 $folders = array();
71 $imageFiles = array();
72 $otherFiles = array();
73
74 $realDir = "images$shortPath";
75
76 foreach (scandir($realDir) as $file) if ($file != '.' and $file != '..')
77 {
78         if (is_dir("$realDir/$file"))
79         {
80                 $folders[] = array( "name" => $file, "link" => "$scriptUrlPath$shortPath/$file", "preview" => getAlbumPreview("$realDir/$file") );
81         }
82         else
83         {
84                 $ext = strtolower(substr($file, -4));
85                 if ($ext == ".jpg")
86                         $imageFiles[] = array( "name" => $file, "url" => getPreview("$realDir/$file"), "link" => dirname($scriptUrlPath)."/view/$shortPath/$file" );
87                 else
88                         $otherFiles[] = array( "name" => $file, "link" => dirname($scriptUrlPath)."/$realDir/$file" );
89         }
90 }
91
92 if (dirname($shortPath) !== '')
93         $parentLink = $scriptUrlPath.dirname($shortPath);
94 else
95         $parentLink = "";
96
97 ?>
98 <?php
99 ///// template starts here /////
100 header('Content-Type: text/html; charset=utf-8');
101 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
102 ?>
103 <html>
104 <head>
105 <style type="text/css">
106 img {
107         border: 0;
108         vertical-align: middle;
109 }
110
111 .square {
112         display: inline-block;
113 }
114
115 .image {
116         width: <?php echo THUMB_SIZE ?>px;
117         height: <?php echo THUMB_SIZE ?>px;
118         display: table-cell;
119         text-align: center;
120         vertical-align: middle;
121 }
122 </style>
123 </head>
124 <body>
125
126 <?php if ($parentLink !== '') { ?>
127         <div id="parentfolder"><a href="<?php echo $parentLink ?>">^</a></div>
128 <?php } ?>
129
130 <?php foreach($folders as $folder) { ?>
131         <div class="folder">
132         <a href="<?php echo $folder["link"] ?>">
133         <?php if ($folder["preview"] !== "") { ?>
134                 <img src="<?php echo $folder["preview"] ?>" />
135         <?php } ?>
136         <?php echo $folder["name"] ?>
137         </a>
138         </div>
139 <?php } ?>
140
141 <?php foreach ($imageFiles as $file) { ?>
142         <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>
143 <?php } ?>
144
145 <?php foreach ($otherFiles as $file) { ?>
146         <div class="miscfile"><a href="<?php echo $file["link"] ?>"><?php echo $file["name"] ?></a></div>
147 <?php } ?>
148
149 </body>
150 </html>