ebad5041a7ac8b019fac0bdba4cb77d37902f4f3
[bizou.git] / functions.php
1 <?php
2
3 // load plugins
4 $plugins = array();
5 if (is_dir("plugins")) {
6         $plugins = scandir("plugins");
7         array_shift($plugins); array_shift($plugins); // remove . and ..
8         foreach ($plugins as $p) if (is_file("plugins/$p/functions.php"))
9                 require "plugins/$p/functions.php";
10 }
11
12 function plugins_include($phpFile)
13 {
14         foreach ($GLOBALS['plugins'] as $p) if (is_file("plugins/$p/$phpFile"))
15                 require "plugins/$p/$phpFile";
16 }
17
18 function getPathInfo()
19 {
20         $simplePath = $_SERVER["PATH_INFO"];
21         if ($simplePath == '/') $simplePath = '';
22         // extra security check to avoid /photos/index/../.. like urls, maybe useless but..
23         if (strpos($simplePath, '..') !== false) die(".. found in url");
24         return $simplePath;
25 }
26
27
28 if (! function_exists('getImageLink')) {
29 function getImageLink($imageSimplePath)
30 {
31         return $GLOBALS['rootUrl'].IMAGES_DIR.$imageSimplePath;
32 }
33 }
34
35 if (! function_exists('getPreview')) {
36 function getPreview($imgFile, $maxSize = THUMB_SIZE)
37 {
38         # example: data/myalbum/100.mypic.jpg
39         $newImgFile = DATA_DIR."/".dirname($imgFile)."/".$maxSize.".".basename($imgFile);
40         
41         # if the preview is a symlink, image is already good sized
42         if (is_link($newImgFile)) return $imgFile;
43         
44         if (! is_file($newImgFile))
45         {
46                 # this tels the template to flush output after displaying previews
47                 $GLOBALS["generating"] = true;
48
49                 # reset script time limit to 20s (wont work in safe mode)
50                 set_time_limit(20);
51
52                 $ext = strtolower(substr($imgFile, -4));
53                 if ($ext == ".jpg")
54                         $img = imagecreatefromjpeg($imgFile);
55                 else
56                         $img = imagecreatefrompng($imgFile);
57
58                 $w = imagesx($img);
59                 $h = imagesy($img);
60                 # if the image is already small, make a symlink, and return it
61                 if ($w <= $maxSize and $h <= $maxSize) {
62                         imagedestroy($img);
63                         symlink($imgFile, $newImgFile);
64                         return $imgFile;
65                 }
66
67                 # config to allow group writable files
68                 umask(DATA_UMASK);
69                 # create the thumbs directory recursively
70                 if (! is_dir(dirname($newImgFile))) @mkdir(dirname($newImgFile), 0777, true)
71                         or die("Could not write in data dir. Please fix permissions.");
72
73                 if ($w > $h) {
74                         $newW = $maxSize;
75                         $newH = $h/($w/$maxSize);
76                 } else {
77                         $newW = $w/($h/$maxSize);
78                         $newH = $maxSize;
79                 }
80
81                 $newImg = imagecreatetruecolor($newW, $newH);
82
83                 imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newW, $newH, $w, $h);
84
85                 if ($ext == ".jpg")
86                         imagejpeg($newImg, $newImgFile);
87                 else
88                         imagepng($newImg, $newImgFile);
89                 
90                 imagedestroy($img);
91                 imagedestroy($newImg);
92         }
93
94         return $newImgFile;
95 }
96 }
97
98 function getAlbumPreview($dir)
99 {
100         $previewFile = DATA_DIR."/$dir/albumpreview";
101
102         if (is_file("$previewFile.jpg")) {
103                 return "$previewFile.jpg";
104         } else if (is_file("$previewFile.empty")) {
105                 return "";
106         } else if (is_file("$previewFile.png")) {
107                 return "$previewFile.png";
108         } else {
109                 # config to allow group writable files
110                 umask(DATA_UMASK);
111                 # create the thumbs directory recursively
112                 if (! is_dir(dirname($previewFile))) @mkdir(dirname($previewFile), 0777, true)
113                         or die("Could not write in data dir. Please fix permissions.");
114
115                 // no preview: look for a preview in current dir, write it, return it
116                 foreach (scandir($dir) as $file) if ($file[0] != '.') {
117                         $ext = strtolower(substr($file, -4));
118                         if ($ext == ".jpg" or $ext == ".png") {
119                                 $thumb = getPreview("$dir/$file");
120                                 copy($thumb, $previewFile.$ext);
121                                 return $previewFile.$ext;
122                         } else if (is_dir("$dir/$file")) {
123                                 $subPreview = getAlbumPreview("$dir/$file");
124                                 if ($subPreview) {
125                                         $myPreview = dirname($previewFile)."/".basename($subPreview);
126                                         copy($subPreview, $myPreview);
127                                         return $myPreview;
128                                 }
129                         }
130                 }
131
132                 // nothing found. create empty file
133                 touch("$previewFile.empty");
134                 return "";
135         }
136 }
137
138 ?>