-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
401 lines (345 loc) · 11.6 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<?php
# # # # # # # # # # # # # # # # # # # # # # # # #
# _ __ ___ #
# _ __ (_) __\ \ / (_) _____ __ #
# | '_ \| |/ __\ \ / /| |/ _ \ \ /\ / / #
# | |_) | | (__ \ V / | | __/\ V V / #
# | .__/|_|\___| \_/ |_|\___| \_/\_/ #
# |_| picView Version 0.4.20061212 #
# #
# This is GPL. #
# Visit http://scripte.arnep.de #
# # # # # # # # # # # # # # # # # # # # # # # # #
function bind_user($user,$pw){
global $ds;
open_ldap_connection();
$user = strtolower(trim($user));
$userDN = get_user_dn($user);
$ok = ldap_bind($ds, $userDN, $pw);
if ($ok) $GLOBALS["boundUserDN"] = $userDN; else $GLOBALS["boundUserDN"] = FALSE;
return $ok;
}
function get_user_dn($username) {
global $peopleBase;
return "uid=$username,$peopleBase";
}
function open_ldap_connection() {
global $ds, $ldapHost;
if ($ds) return;
$ds=ldap_connect($ldapHost);
define('LDAP_OPT_DIAGNOSTIC_MESSAGE', 0x0032);
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
}
function send_basicauth_header($realm) {
header("WWW-Authenticate: Basic realm=\"$realm\"");
header("HTTP/1.1 401 Unauthorized");
}
function check_basicauth() {
global $galleryConfig;
switch($galleryConfig['auth_required']) {
case 'ldap':
if ($_SERVER["PHP_AUTH_USER"] && $_SERVER["PHP_AUTH_PW"]) {
if (bind_user($_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"])) return true;
}
send_basicauth_header("Please authenticate for $galleryConfig[title] with your LDAP account");
return FALSE;
case 'password':
if ($_SERVER["PHP_AUTH_USER"] && $_SERVER["PHP_AUTH_PW"]) {
//$correctPwHash = $galleryConfig['password'][$_SERVER["PHP_AUTH_USER"]];
//if (crypt($_SERVER["PHP_AUTH_PW"], $correctPwHash) === $correctPwHash) return true;
$correctPw = $galleryConfig['password'][$_SERVER["PHP_AUTH_USER"]];
if ($correctPw === $_SERVER["PHP_AUTH_PW"]) return true;
}
send_basicauth_header("Please authenticate for $galleryConfig[title] with the given username and password");
return FALSE;
case '':
return TRUE;
default:
die("Invalid Configuration");
}
}
function require_basicauth() {
if (!check_basicauth()) {
echo "<div class='alert alert-danger'><h4>Please authenticate </h4>\n</div>";
exit;
}
}
function show_directory($path, $currpath) {
global $pictures_path, $BASE_URI;
$r = '';
$currpath = str_replace('//','/',$currpath);
$currpath = str_replace('\\\\','/',$currpath);
$dir = get_dir($path, $currpath, 'd');
// Backlink to parent directory
if ($currpath != '/')
$r .= '<li><a href="'.$BASE_URI.'/p'.$currpath.'../">[up]</a></li>'."\n";
// Display directory list
foreach($dir as $d) {
if (!legal_image($path.$currpath.'/'.$d, $pictures_path)) {
$r .= "#1 Illegaler Pfad! ($d)";
return $r;
}
$r .= '<li><a href="'.$BASE_URI.'/p'.$currpath.$d.'/'.'">'.$d.'</a></li>'."\n";
}
return $r;
} // show_directory()
function show_pictures($path, $currpath) {
global $pictures_path, $comments_path, $thumbs_path, $thumbs_per_page, $BASE_URI;
$start = (int)$_GET['n'];
$r = '';
$dir = get_dir($path, $currpath, 'f');
if (count($dir) == 0) {
$r .= '<p>Keine Bilder in diesem Verzeichnis gefunden.</p>';
$sub_dir = get_dir($path, $currpath, 'd');
// Show pictures in subdirectories, if any
$count1 = 0;
foreach ($sub_dir as $d) {
$s_d = get_dir($path, $currpath.'/'.$d, 'f');
if (count($s_d) != 0) {
if ($count1++ > 5) break;
$_d = $d;
$count2 = 0;
$r .= '<a href="'.$BASE_URI.'/p'.$currpath.$_d.'/'.'">';
$r .= "<h2>{$currpath}{$d}</h2>";
$r .= "</a>\n<div class='img-gallery' style='width:100%'>";
foreach($s_d as $s) {
if ($count2++ > 5) break;
$d = $_d.'/'.$s;
if (!legal_image($path.$currpath.$d, $pictures_path)) {
$r .= "<p>#2 Illegaler Pfad! ($s)</p>";
return $r;
}
// Filename for comments to save to
$comment_file = $comments_path.'/'.str_replace('/','_',$currpath.$d).'.txt';
$number_comments = file_exists($comment_file) ?
preg_match_all('/[0-9]{10}/', join('', file($comment_file)), $__t) : 0;
// Get image size
$save_name = $thumbs_path.'/'.str_replace('/','_',$path.$currpath.$d);
$size = @getimagesize($save_name);
$size[3] = $size[3];
#print_r($size);
$r .= '<a href="'.$BASE_URI.'/c'.$currpath.$d.'">';
$r .= '<img title="'.$number_comments.' Kommentare" src="'.$BASE_URI.'/t'.$currpath.$d.'?comments='.$number_comments.'" '.$size[3].' class="img'. ($size[0] < $size[1] ? 'h' : 'v') .'" />';
$r .= "</a>\n";
}
$r .= '</div><a href="'.$BASE_URI.'/p'.$currpath.$_d.'/'.'">';
$r .= 'Mehr...';
$r .= "</a>\n";
}
}
}
// Display images list
$r .= '<p>';
$max = count($dir);
for($i = 0; $i < $max; $i += $thumbs_per_page) {
$show_to = $i + $thumbs_per_page;
if ($show_to > $max) $show_to = $max;
if ($start >= $i && $start < $show_to)
$r .= '<b>['. ($i + 1) .'-'. ($show_to) .']</b> ';
else
$r .= '[<a href="'.$BASE_URI.'/p'.$currpath.'?n='.($i).'">'. ($i + 1) .'-'. ($show_to) .'</a>] ';
}
$r .= '</p>';
$count = -1;
$r .= "<div class='img-gallery' style='width:100%'>";
foreach($dir as $d) {
$count++;
if ($count < $start || $count > $start + $thumbs_per_page) continue;
if (!legal_image($path.$currpath.$d, $pictures_path)) {
$r .= "#2 Illegaler Pfad! ($d)";
return $r;
}
// Filename for comments to save to
$comment_file = $comments_path.'/'.str_replace('/','_',$currpath.$d).'.txt';
$number_comments = file_exists($comment_file) ?
preg_match_all('/[0-9]{10}/', join('', file($comment_file)), $__t) : 0;
// Get image size
$save_name = $thumbs_path.'/'.str_replace('/','_',$path.$currpath.$d);
$size = @getimagesize($save_name);
$size[3] = $size[3];
#print_r($size);
$r .= '<a href="'.$BASE_URI.'/c'.$currpath.$d.'?n='.$start.'">';
$r .= '<img title="'.$number_comments.' Kommentare" src="'.$BASE_URI.'/t'.$currpath.$d.'?comments='.$number_comments.'" '.$size[3].' class="img'. ($size[0] < $size[1] ? 'h' : 'v') .'" />';
$r .= "</a>";
}
$r .= "</div>";
$r .= '<p>';
$max = count($dir);
for($i = 0; $i < $max; $i += $thumbs_per_page) {
$show_to = $i + $thumbs_per_page;
if ($show_to > $max) $show_to = $max;
if ($start >= $i && $start < $show_to)
$r .= '<b>['. ($i + 1) .'-'. ($show_to) .']</b> ';
else
$r .= '[<a href="'.$BASE_URI.'/p'.$currpath.'?n='.($i).'">'. ($i + 1) .'-'. ($show_to) .'</a>] ';
}
$r .= '</p>';
$count = -1;
return $r;
} // show_pictures()
function show_breadcrumb($path) {
global $BASE_URI;
$url = $BASE_URI.'/p/';
$parts = explode("/", $path);
$r = "<li><a href='$url'>PicView</a></li>";
for($i = 1; $i < count($parts) - 1; $i++) {
$url .= $parts[$i] . '/';
$r .= "<li><a href='$url'>".$parts[$i]."</a></li>";
}
return $r;
}
function get_next_picture($path, $currpath, $img) {
global $pictures_path, $comments_path, $thumbs_path, $BASE_URI;
$dir = get_dir($path, $currpath, 'f');
print_r($dir);
} // get_next_picture
function legal_image($img, $path) {
// Convert paths to full path & quote for regex
$realpath = str_replace('\\','/',realpath($img));
$path = preg_quote(str_replace('\\','/',realpath($path)));
// Picture not in path
if (!preg_match("!^".$path."!", $realpath)) return FALSE;
// Picture does not exist
if (!file_exists($realpath)) return FALSE;
// default
return TRUE;
} // legal_image()
function image_write($im, $text, $size, $x, $y) {
global $font_file;
putenv('GDFONTPATH=' . realpath('.'));
$black = ImageColorAllocate ($im, 0, 0, 0);
$white = ImageColorAllocate ($im, 255, 255, 255);
ImageTTFText ($im, $size, 0, $x+0, $y+9, $white, $font_file, $text);
ImageTTFText ($im, $size, 0, $x+0, $y+11, $white, $font_file, $text);
ImageTTFText ($im, $size, 0, $x+2, $y+9, $white, $font_file, $text);
ImageTTFText ($im, $size, 0, $x+2, $y+11, $white, $font_file, $text);
ImageTTFText ($im, $size, 0, $x+1, $y+10, $black, $font_file, $text);
return $im;
} // image_write()
function make_page($tpl_filename, $vars) {
$template = file_get_contents($tpl_filename);
foreach($vars as $x => $y)
$template = str_replace('%' . $x . '%', $y, $template);
return $template;
}
function make_basedir() {
global $BASE_URI;
$base = dirname(dirname($BASE_URI));
if ($base == '/') return "/";
return $base;
}
// $imgSrc - GD image handle of source image
// $angle - angle of rotation. Needs to be positive integer
// angle shall be 0,90,180,270, but if you give other it
// will be rouned to nearest right angle (i.e. 52->90 degs,
// 96->90 degs)
// returns GD image handle of rotated image.
function ImageRotate_PV( $imagePath, $angle ) {
$imgSrc=ImageCreateFromJPEG($imagePath);
// ensuring we got really RightAngle (if not we choose the closest one)
$angle = min( ( (int)(($angle+45) / 90) * 90), 270 );
// no need to fight
if( $angle == 0 )
return;
// dimenstion of source image
$srcX = imagesx( $imgSrc );
$srcY = imagesy( $imgSrc );
switch( $angle )
{
case 90:
$imgDest = imagecreatetruecolor( $srcY, $srcX );
for( $x=0; $x<$srcX; $x++ )
for( $y=0; $y<$srcY; $y++ )
imagecopy($imgDest, $imgSrc, $srcY-$y-1, $x, $x, $y, 1, 1);
break;
case 180:
$imgDest = ImageFlip( $imgSrc, IMAGE_FLIP_BOTH );
break;
case 270:
$imgDest = imagecreatetruecolor( $srcY, $srcX );
for( $x=0; $x<$srcX; $x++ )
for( $y=0; $y<$srcY; $y++ )
imagecopy($imgDest, $imgSrc, $y, $srcX-$x-1, $x, $y, 1, 1);
break;
}
ImageDestroy($imgSrc);
# ImageInterlace($imgDest, 0);
ImageJPEG($imgDest, $imagePath);
ImageDestroy($imgDest);
}
function quickRotate($imagePath,$angle){
if(!preg_match("/\.(png|jpg|jpeg|gif)$/i", $imagePath)) die('invalid file extension');
exec("convert -rotate $angle $imagePath $imagePath");
return;
// Bilder werden immer als JPG abgespeichert
$src_img=ImageCreateFromJPEG($imagePath);
# if(preg_match("/\.(png)$/i", $imagePath)) $src_img=ImageCreateFromPNG($imagePath);
# elseif(preg_match("/\.(jpg)$/i", $imagePath)) $src_img=ImageCreateFromJPEG($imagePath);
# elseif(preg_match("/\.(bmp)$/i", $imagePath)) $src_img=ImageCreateFromWBMP($imagePath);
$size=GetImageSize($imagePath);
//note: to make it work on GD 2.xx properly change ImageCreate to ImageCreateTrueColor
$dst_img = ImageRotate($src_img, $rtt, 0);
$dst_img=ImageCreateTrueColor($size[1],$size[0]);
if($rtt==90){
$t=0;
$b=$size[1]-1;
while($t<=$b){
$l=0;
$r=$size[0]-1;
while($l<=$r){
imagecopy($dst_img,$src_img,$t,$r,$r,$b,1,1);
imagecopy($dst_img,$src_img,$t,$l,$l,$b,1,1);
imagecopy($dst_img,$src_img,$b,$r,$r,$t,1,1);
imagecopy($dst_img,$src_img,$b,$l,$l,$t,1,1);
$l++;
$r--;
}
$t++;
$b--;
}
}
elseif($rtt==-90){
$t=0;
$b=$size[1]-1;
while($t<=$b){
$l=0;
$r=$size[0]-1;
while($l<=$r){
imagecopy($dst_img,$src_img,$t,$l,$r,$t,1,1);
imagecopy($dst_img,$src_img,$t,$r,$l,$t,1,1);
imagecopy($dst_img,$src_img,$b,$l,$r,$b,1,1);
imagecopy($dst_img,$src_img,$b,$r,$l,$b,1,1);
$l++;
$r--;
}
$t++;
$b--;
}
}
ImageDestroy($src_img);
ImageInterlace($dst_img,0);
ImageJPEG($dst_img,$imagePath);
ImageDestroy($dst_img);
} // quickRotate()
function get_dir($path, $currpath, $type) {
$cwd = getcwd();
$handle=@opendir($path.$currpath);
$dir = Array();
@chdir($path.$currpath);
while ($file = @readdir ($handle)) {
if ($file != "." && $file != "..") {
if ($type == 'f') {
if (is_file(basename($file)) && preg_match("/\.(jpg|png)$/i",$file)) $dir[] = $file;
} elseif ($type == 'd') {
if (is_dir(basename($file))) $dir[] = $file;
} else {
$dir[] = $file;
}
}
}
@closedir($handle);
sort($dir);
chdir($cwd);
return $dir;
} // get_dir()
?>