A new hook to include head parts. A new hook to overload thumb function
[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
72                 if ($w > $h) {
73                         $newW = $maxSize;
74                         $newH = $h/($w/$maxSize);
75                 } else {
76                         $newW = $w/($h/$maxSize);
77                         $newH = $maxSize;
78                 }
79
80                 $newImg = imagecreatetruecolor($newW, $newH);
81
82                 imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newW, $newH, $w, $h);
83
84                 if ($ext == ".jpg")
85                         imagejpeg($newImg, $newImgFile);
86                 else
87                         imagepng($newImg, $newImgFile);
88                 
89                 imagedestroy($img);
90                 imagedestroy($newImg);
91         }
92
93         return $newImgFile;
94 }
95 }
96
97 function getAlbumPreview($dir)
98 {
99         $previewFile = DATA_DIR."/$dir/albumpreview";
100
101         if (is_file("$previewFile.jpg")) {
102                 return "$previewFile.jpg";
103         } else if (is_file("$previewFile.empty")) {
104                 return "";
105         } else if (is_file("$previewFile.png")) {
106                 return "$previewFile.png";
107         } else {
108                 # config to allow group writable files
109                 umask(DATA_UMASK);
110                 # create the thumbs directory recursively
111                 if (! is_dir(dirname($previewFile))) mkdir(dirname($previewFile), 0777, true);
112
113                 // no preview: look for a preview in current dir, write it, return it
114                 foreach (scandir($dir) as $file) if ($file[0] != '.') {
115                         $ext = strtolower(substr($file, -4));
116                         if ($ext == ".jpg" or $ext == ".png") {
117                                 $thumb = getPreview("$dir/$file");
118                                 copy($thumb, $previewFile.$ext);
119                                 return $previewFile.$ext;
120                         } else if (is_dir("$dir/$file")) {
121                                 $subPreview = getAlbumPreview("$dir/$file");
122                                 if ($subPreview) {
123                                         $myPreview = dirname($previewFile)."/".basename($subPreview);
124                                         copy($subPreview, $myPreview);
125                                         return $myPreview;
126                                 }
127                         }
128                 }
129
130                 // nothing found. create empty file
131                 touch("$previewFile.empty");
132                 return "";
133         }
134 }
135
136 ?>