Subversion Repositories Applications.papyrus

Rev

Rev 1087 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
954 florian 1
<?php
2
# ***** BEGIN LICENSE BLOCK *****
3
# This file is part of DotClear.
4
# Copyright (c) 2004 Olivier Meunier and contributors. All rights
5
# reserved.
6
#
7
# DotClear is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 2 of the License, or
10
# (at your option) any later version.
11
#
12
# DotClear is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with DotClear; if not, write to the Free Software
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
#
21
# ***** END LICENSE BLOCK *****
22
 
23
class files
24
{
25
	function scandir($d,$order=0)
26
	{
27
		$res = array();
28
		if (($dh = @opendir($d)) !== false)
29
		{
30
			while (($f = readdir($dh)) !== false) {
31
				$res[] = $f;
32
			}
33
			closedir($dh);
34
 
35
			sort($res);
36
			if ($order == 1) {
37
				rsort($res);
38
			}
39
 
40
			return $res;
41
		}
42
		else
43
		{
44
			return false;
45
		}
46
	}
47
 
48
	function isDeletable($f)
49
	{
50
		if (is_file($f)) {
51
			return is_writable(dirname($f));
52
		} elseif (is_dir($f)) {
53
			return (is_writable(dirname($f)) && count(files::scandir($f)) <= 2);
54
		}
55
	}
56
 
57
	# Suppression récursive d'un répertoire (rm -rf)
58
	function deltree($dir)
59
	{
60
		$current_dir = opendir($dir);
61
		while($entryname = readdir($current_dir))
62
		{
63
			if (is_dir($dir.'/'.$entryname) and ($entryname != '.' and $entryname!='..'))
64
			{
65
				if (!files::deltree($dir.'/'.$entryname)) {
66
					return false;
67
				}
68
			}
69
			elseif ($entryname != '.' and $entryname!='..')
70
			{
71
				if (!@unlink($dir.'/'.$entryname)) {
72
					return false;
73
				}
74
			}
75
		}
76
		closedir($current_dir);
77
		return @rmdir($dir);
78
	}
79
 
80
	function touch($f)
81
	{
82
		if (is_writable($f)) {
83
			$c = implode('',file($f));
84
			if ($fp = @fopen($f,'w')) {
85
				fwrite($fp,$c,strlen($c));
86
				fclose($fp);
87
			}
88
		}
89
	}
90
 
91
	function secureFile($f)
92
	{
93
		if (is_file($f))
94
		{
95
			@chmod($f,0600);
96
			if (is_readable($f)) {
97
				return true;
98
			} else {
99
				@chmod($f,0660);
100
				if (is_readable($f)) {
101
					return true;
102
				} else {
103
					@chmod($f,0666);
104
				}
105
			}
106
		}
107
	}
108
 
109
	function makeDir($f)
110
	{
111
		if (@mkdir($f,fileperms(dirname($f))) === false) {
112
			return false;
113
		}
114
 
115
		@chmod($f,fileperms(dirname($f)));
116
	}
117
 
118
	function putContent($f, $f_content)
119
	{
120
		if (is_writable($f))
121
		{
122
			if ($fp = @fopen($f, 'w'))
123
			{
124
				fwrite($fp,$f_content,strlen($f_content));
125
				fclose($fp);
126
				return true;
127
			}
128
		}
129
 
130
		return false;
131
	}
132
 
133
	function size($size)
134
	{
135
		$kb = 1024;
136
		$mb = 1024 * $kb;
137
		$gb = 1024 * $mb;
138
		$tb = 1024 * $gb;
139
 
140
		if($size < $kb) {
141
			return $size." B";
142
		}
143
		else if($size < $mb) {
144
			return round($size/$kb,2)." KB";
145
		}
146
		else if($size < $gb) {
147
			return round($size/$mb,2)." MB";
148
		}
149
		else if($size < $tb) {
150
			return round($size/$gb,2)." GB";
151
		}
152
		else {
153
			return round($size/$tb,2)." TB";
154
		}
155
	}
156
 
157
	# Copier d'un fichier binaire distant
158
	function copyRemote($src,$dest)
159
	{
160
		if (($fp1 = @fopen($src,'r')) === false)
161
		{
162
			return __('An error occured while downloading the file.');
163
		}
164
		else
165
		{
166
			if (($fp2 = @fopen($dest,'w')) === false)
167
			{
168
				fclose($fp1);
169
				return __('An error occured while writing the file.');
170
			}
171
			else
172
			{
173
				while (($buffer = fgetc($fp1)) !== false) {
174
					fwrite($fp2,$buffer);
175
				}
176
				fclose($fp1);
177
				fclose($fp2);
178
				return true;
179
			}
180
		}
181
	}
182
 
183
 
184
	# Fonctions de création de packages
185
	#
186
	function getDirList($dirName)
187
	{
188
		static $filelist = array();
189
		static $dirlist = array();
190
 
191
		$exclude_list=array('.','..','.svn');
192
 
193
		if (empty($res)) {
194
			$res = array();
195
		}
196
 
197
		$dirName = preg_replace('|/$|','',$dirName);
198
 
199
		if (!is_dir($dirName)) {
200
			return false;
201
		}
202
 
203
		$dirlist[] = $dirName;
204
 
205
		$d = dir($dirName);
206
		while($entry = $d->read())
207
		{
208
			if (!in_array($entry,$exclude_list))
209
			{
210
				if (is_dir($dirName.'/'.$entry))
211
				{
212
					if ($entry != 'CVS')
213
					{
214
						files::getDirList($dirName.'/'.$entry);
215
					}
216
				}
217
				else
218
				{
219
					$filelist[] = $dirName.'/'.$entry;
220
				}
221
			}
222
		}
223
		$d->close();
224
 
225
		return array('dirs'=>$dirlist, 'files'=>$filelist);
226
	}
227
 
228
	function makePackage($name,$dir,$remove_path='',$gzip=true)
229
	{
230
		if ($gzip && !function_exists('gzcompress')) {
231
			return false;
232
		}
233
 
234
		if (($filelist = files::getDirList($dir)) === false) {
235
			return false;
236
		}
237
 
238
		$res = array ('name' => $name, 'dirs' => array(), 'files' => array());
239
 
240
		foreach ($filelist['dirs'] as $v) {
241
			$res['dirs'][] = preg_replace('/^'.preg_quote($remove_path,'/').'/','',$v);
242
		}
243
 
244
		foreach ($filelist['files'] as $v) {
245
			$f_content = base64_encode(file_get_contents($v));
246
			$v = preg_replace('/^'.preg_quote($remove_path,'/').'/','',$v);
247
			$res['files'][$v] = $f_content;
248
		}
249
 
250
		$res = serialize($res);
251
 
252
		if ($gzip) {
253
			$res = gzencode($res);
254
		}
255
 
256
		return $res;
257
	}
258
}
259
 
260
 
261
class path
262
{
263
	function real($p,$strict=true)
264
	{
265
		$os = (DIRECTORY_SEPARATOR == '\\') ? 'win' : 'nix';
266
 
267
		# Chemin absolu ou non ?
268
		if ($os == 'win') {
269
			$_abs = preg_match('/^\w+:/',$p);
270
		} else {
271
			$_abs = substr($p,0,1) == '/';
272
		}
273
 
274
		# Transformation du chemin, forme std
275
		if ($os == 'win') {
276
			$p = str_replace('\\','/',$p);
277
		}
278
 
279
		# Ajout de la racine du fichier appelant si
280
		if (!$_abs) {
281
			$p = dirname($_SERVER['SCRIPT_FILENAME']).'/'.$p;
282
		}
283
 
284
		# Nettoyage
285
		$p = preg_replace('|/+|','/',$p);
286
 
287
		if (strlen($p) > 1) {
288
			$p = preg_replace('|/$|','',$p);
289
		}
290
 
291
		$_start = '';
292
		if ($os == 'win') {
293
			list($_start,$p) = explode(':',$p);
294
			$_start .= ':/';
295
		} else {
296
			$_start = '/';
297
		}
298
		$p = substr($p,1);
299
 
300
		# Parcours
301
		$P = explode('/',$p);
302
		$res = array();
303
 
304
		for ($i=0;$i<count($P);$i++)
305
		{
306
			if ($P[$i] == '.') {
307
				continue;
308
			}
309
 
310
			if ($P[$i] == '..') {
311
				if (count($res) > 0) {
312
					array_pop($res);
313
				}
314
			} else {
315
				array_push($res,$P[$i]);
316
			}
317
		}
318
 
319
		$p = $_start.implode('/',$res);
320
 
321
		if ($strict && !@file_exists($p)) {
322
			return false;
323
		}
324
 
325
		return $p;
326
	}
327
}
328
?>