added the DATA_UMASK config option
[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 function getPreview($imgFile, $maxSize = THUMB_SIZE)
36 {
37         # example: data/myalbum/100.mypic.jpg
38         $newImgFile = DATA_DIR."/".dirname($imgFile)."/".$maxSize.".".basename($imgFile);
39         
40         # if the preview is a symlink, image is already good sized
41         if (is_link($newImgFile)) return $imgFile;
42         
43         if (! is_file($newImgFile))
44         {
45                 # this tels the template to flush output after displaying previews
46                 $GLOBALS["generating"] = true;
47
48                 # reset script time limit to 20s (wont work in safe mode)
49                 set_time_limit(20);
50
51                 $ext = strtolower(substr($imgFile, -4));
52                 if ($ext == ".jpg")
53                         $img = imagecreatefromjpeg($imgFile);
54                 else
55                         $img = imagecreatefrompng($imgFile);
56
57                 $w = imagesx($img);
58                 $h = imagesy($img);
59                 # if the image is already small, make a symlink, and return it
60                 if ($w <= $maxSize and $h <= $maxSize) {
61                         imagedestroy($img);
62                         symlink($imgFile, $newImgFile);
63                         return $imgFile;
64                 }
65
66                 # config to allow group writable files
67                 umask(DATA_UMASK);
68                 # create the thumbs directory recursively
69                 if (! is_dir(dirname($newImgFile))) mkdir(dirname($newImgFile), 0777, true);
70
71                 if ($w > $h) {
72                         $newW = $maxSize;
73                         $newH = $h/($w/$maxSize);
74                 } else {
75                         $newW = $w/($h/$maxSize);
76                         $newH = $maxSize;
77                 }
78
79                 $newImg = imagecreatetruecolor($newW, $newH);
80
81                 imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newW, $newH, $w, $h);
82
83                 if ($ext == ".jpg")
84                         imagejpeg($newImg, $newImgFile);
85                 else
86                         imagepng($newImg, $newImgFile);
87                 
88                 imagedestroy($img);
89                 imagedestroy($newImg);
90         }
91
92         return $newImgFile;
93 }
94
95 function getAlbumPreview($dir)
96 {
97         $previewFile = DATA_DIR."/$dir/albumpreview";
98
99         if (is_file("$previewFile.jpg")) {
100                 return "$previewFile.jpg";
101         } else if (is_file("$previewFile.empty")) {
102                 return "";
103         } else if (is_file("$previewFile.png")) {
104                 return "$previewFile.png";
105         } else {
106                 # config to allow group writable files
107                 umask(DATA_UMASK);
108                 # create the thumbs directory recursively
109                 if (! is_dir(dirname($previewFile))) mkdir(dirname($previewFile), 0777, true);
110
111                 // no preview: look for a preview in current dir, write it, return it
112                 foreach (scandir($dir) as $file) if ($file != '.' and $file != '..') {
113                         $ext = strtolower(substr($file, -4));
114                         if ($ext == ".jpg" or $ext == ".png") {
115                                 $thumb = getPreview("$dir/$file");
116                                 copy($thumb, $previewFile.$ext);
117                                 return $previewFile.$ext;
118                         } else if (is_dir("$dir/$file")) {
119                                 $subPreview = getAlbumPreview("$dir/$file");
120                                 if ($subPreview) {
121                                         $myPreview = dirname($previewFile)."/".basename($subPreview);
122                                         copy($subPreview, $myPreview);
123                                         return $myPreview;
124                                 }
125                         }
126                 }
127
128                 // nothing found. create empty file
129                 touch("$previewFile.empty");
130                 return "";
131         }
132 }
133
134 ?>