forked from sttts/wikigallery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
directoryprovider.php
187 lines (161 loc) · 5.52 KB
/
directoryprovider.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
<?php if (!defined('PmWiki')) exit();
/*
* WikiGallery - automatic easy to use gallery extension for PmWiki
* (c) 2006,2007 Stefan Schimanski <sts@1stein.org>
* 2007 Picture height patch by JamesM
*
* Ideas from SimpleGallery by Bram Brambring <http://www.brambring.nl>
* Some code from Qdig by Hagan Fox <http://qdig.sourceforge.net/>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
require_once( "thumb.php" );
class GalleryDirectoryProvider extends GalleryProvider {
var $directoryBasePath;
var $thumbProvider;
function GalleryDirectoryProvider( $group, $basePath, $webPath, $thumbProvider=false ) {
global $WikiGallery_CacheBasePath, $WikiGallery_CacheWebPath,
$WikiGallery_ScaleMethod;
$this->GalleryProvider( $group );
$this->directoryBasePath = $basePath;
$this->directoryWebPath = $webPath;
if( $thumbProvider )
$this->thumbProvider =& $thumbProvider;
else
$this->thumbProvider =&
new InternalThumbProvider( $group,
$WikiGallery_CacheBasePath,
$WikiGallery_CacheWebPath,
$basePath,
$webPath,
$WikiGallery_ScaleMethod
);
}
function pageNameToFileNameImpl( $basePath, $pageName ) {
global $WikiGallery_PathDelimiter;
// empty pageName?
if( $pageName=="" ) return "";
//echo "Will try to find filename for $pageName<br/>";
$lastComponent = WikiGalleryLastComponent( $pageName, $WikiGallery_PathDelimiter );
$head = WikiGalleryLeadingComponents( $pageName, $WikiGallery_PathDelimiter );
if( !$head ) {
$path = "";
$pathslash = "";
} else {
$path = $this->pageNameToFileNameImpl( $basePath, $head );
if( $path==-1 ) return -1;
$pathslash = "$path/";
}
if ( !is_dir( $basePath . "/" . $path) ) return -1;
$pwd = opendir( $basePath . "/" . $path);
$found = "";
while( ($file=readdir($pwd))!=false ) {
$filePageName = fileNameToPageName( $file );
if( ucfirst($lastComponent)==$filePageName ) {
//echo "Found $file==$lastComponent<br/>";
$found = $file;
break;
} else {
//echo "$file => $filePageName!=$lastComponent<br/>";
}
}
closedir($pwd);
if( $found=="" )
return -1;
else
return $pathslash . $found;
}
function pageNameToFileName( $pageName ) {
return $this->pageNameToFileNameImpl( $this->directoryBasePath, $pageName );
}
function getFilenames( $path, $albums=false ) {
global $WikiGallery_SortByDate, $WikiGallery_SortBackwards,
$WikiGallery_AlbumsSortByDate, $WikiGallery_AlbumsSortBackwards,
$WikiGallery_ImgExts;
// prepend directory
$path = $this->directoryBasePath . $path;
// normalize path
$path = preg_replace( "/\\/$/", '', $path );
if( $path=="" )
$pathslash = "";
else
$pathslash = "$path/";
// dir?
if( !is_dir( $path ) ) {
#echo "$path is no directory or doesn't exist";
return;
}
// iterate over files
$pwd_handle = opendir($path);
$i = 100;
while ( ($file = readdir($pwd_handle))!=false ) {
// ignore some of them
if( $file=='.' || $file=='..' ) { continue; }
if( is_file($pathslash.$file) ) {
if (strpos(stripslashes(rawurldecode($file)), '..')
|| ($file[0] == '.' && $file[1] == '.')) {
//echo 'Updir ("..") is not allowed in a filename.';
return;
}
}
if( $file[0] == '.') { continue; }
// and include images
if( is_readable($pathslash.$file) &&
(($albums==false && is_file($pathslash.$file) && eregi($WikiGallery_ImgExts, $file)) ||
($albums==true && is_dir($pathslash.$file)))) {
$mod_date = filemtime($pathslash.$file).$i;
$img_files[$mod_date] = $file;
$i++;
} else {
//echo "ignoring $file";
}
}
closedir($pwd_handle);
// sort them
if( isset($img_files) ) {
if( ($albums==false && $WikiGallery_SortByDate==TRUE) ||
($albums==true && $WikiGallery_AlbumsSortByDate==TRUE) ) {
ksort($img_files);
} else {
natcasesort($img_files);
}
foreach($img_files as $img) {
$sorted_files[]=$img;
}
if ( ($albums==false && $WikiGallery_SortBackwards==TRUE) ||
($albums==true && $WikiGallery_AlbumsSortBackwards==TRUE) ) {
return (array_reverse($sorted_files));
} else {
return $sorted_files;
}
}
}
function isAlbum( $path ) {
return is_dir( $this->directoryBasePath . $path );
}
function isPicture( $path ) {
return is_file( $this->directoryBasePath . $path );
}
function pictureTime( $path ) {
return filemtime( $this->directoryBasePath . $path );
}
function thumb( $path, $width, $height, $resizeMode="" ) {
$path = stripslashes( $path );
// picture exists?
if( !$this->isPicture( "/" . $path ) ) return false;
// return thumb url
return $this->thumbProvider->thumbUrl( $path, $width, $height, $resizeMode );
}
}