summaryrefslogtreecommitdiff
path: root/poc/poc02-compiling-cake/src/vendor/cake_1.1.20.7692/cake/libs/folder.php
blob: 331abeef1ed7d92fb8a41bfe3e6e3cc2ccf30c24 (plain)
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
<?php
/* SVN FILE: $Id: folder.php 6305 2008-01-02 02:33:56Z phpnut $ */
/**
 * Convenience class for handling directories.
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
 * Copyright 2005-2008, Cake Software Foundation, Inc.
 *								1785 E. Sahara Avenue, Suite 490-204
 *								Las Vegas, Nevada 89104
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright		Copyright 2005-2008, Cake Software Foundation, Inc.
 * @link				http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
 * @package			cake
 * @subpackage		cake.cake.libs
 * @since			CakePHP(tm) v 0.2.9
 * @version			$Revision: 6305 $
 * @modifiedby		$LastChangedBy: phpnut $
 * @lastmodified	$Date: 2008-01-01 21:33:56 -0500 (Tue, 01 Jan 2008) $
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 */
/**
 * Included libraries.
 *
 */
	if (!class_exists('Object')) {
		 uses ('object');
	}
/**
 * Folder structure browser, lists folders and files.
 *
 * Long description for class
 *
 * @package		cake
 * @subpackage	cake.cake.libs
 */
class Folder extends Object{
/**
 * Path to Folder.
 *
 * @var string
 */
	var $path = null;
/**
 * Sortedness.
 *
 * @var boolean
 */
	var $sort = false;
/**
 * Constructor.
 *
 * @param string $path
 * @param boolean $path
 */
	function __construct($path = false, $create = false, $mode = false) {
		parent::__construct();
		if (empty($path)) {
			$path = getcwd();
		}

		if (!file_exists($path) && $create == true) {
			$this->mkdirr($path, $mode);
		}
		$this->cd($path);
	}
/**
 * Return current path.
 *
 * @return string Current path
 */
	function pwd() {
		return $this->path;
	}
/**
 * Change directory to $desired_path.
 *
 * @param string $desired_path Path to the directory to change to
 * @return string The new path. Returns false on failure
 */
	function cd($desiredPath) {
		$desiredPath = realpath($desiredPath);
		$newPath = $this->isAbsolute($desiredPath) ? $desiredPath : $this->addPathElement($this->path, $desiredPath);
		$isDir = (is_dir($newPath) && file_exists($newPath)) ? $this->path = $newPath : false;
		return $isDir;
	 }
/**
 * Returns an array of the contents of the current directory, or false on failure.
 * The returned array holds two arrays: one of dirs and one of files.
 *
 * @param boolean $sort
 * @param boolean $noDotFiles
 * @return array
 */
	function ls($sort = true, $noDotFiles = false) {
		$dirs = $files = array();
		$dir = opendir($this->path);
		if ($dir) {
			while (false !== ($n = readdir($dir))) {
				if ((!preg_match('#^\.+$#', $n) && $noDotFiles == false) || ($noDotFiles == true && !preg_match('#^\.(.*)$#', $n))) {
					if (is_dir($this->addPathElement($this->path, $n))) {
						$dirs[] = $n;
					} else {
						$files[] = $n;
					}
				}
			}

			if ($sort || $this->sort) {
				sort ($dirs);
				sort ($files);
			}
			closedir ($dir);
		}
		return array($dirs,$files);
	}
/**
 * Returns an array of all matching files in current directory.
 *
 * @param string $pattern Preg_match pattern (Defaults to: .*)
 * @return array
 */
	function find($regexp_pattern = '.*') {
		$data = $this->ls();

		if (!is_array($data)) {
			return array();
		}

		list($dirs, $files) = $data;
		$found =  array();

		foreach ($files as $file) {
			if (preg_match("/^{$regexp_pattern}$/i", $file)) {
				$found[] = $file;
			}
		}
		return $found;
	}
/**
 * Returns an array of all matching files in and below current directory.
 *
 * @param string $pattern Preg_match pattern (Defaults to: .*)
 * @return array Files matching $pattern
 */
	function findRecursive($pattern = '.*') {
		$startsOn = $this->path;
		$out = $this->_findRecursive($pattern);
		$this->cd($startsOn);
		return $out;
	}
/**
 * Private helper function for findRecursive.
 *
 * @param string $pattern
 * @return array Files matching pattern
 * @access private
 */
	function _findRecursive($pattern) {
		list($dirs, $files) = $this->ls();

		$found = array();
		foreach ($files as $file) {
			if (preg_match("/^{$pattern}$/i", $file)) {
				$found[] = $this->addPathElement($this->path, $file);
			}
		}
		$start = $this->path;
		foreach ($dirs as $dir) {
			$this->cd($this->addPathElement($start, $dir));
			$found = array_merge($found, $this->findRecursive($pattern));
		}
		return $found;
	}
/**
 * Returns true if given $path is a Windows path.
 *
 * @param string $path Path to check
 * @return boolean
 * @static
 */
	function isWindowsPath($path) {
		$match = preg_match('#^[A-Z]:\\\#i', $path) ? true : false;
		return $match;
	}
/**
 * Returns true if given $path is an absolute path.
 *
 * @param string $path Path to check
 * @return boolean
 * @static
 */
	function isAbsolute($path) {
		$match = preg_match('#^\/#', $path) || preg_match('#^[A-Z]:\\\#i', $path);
		return $match;
	}
/**
 * Returns true if given $path ends in a slash (i.e. is slash-terminated).
 *
 * @param string $path Path to check
 * @return boolean
 * @static
 */
	function isSlashTerm($path) {
		$match = preg_match('#[\\\/]$#', $path) ? true : false;
		return $match;
	}
/**
 * Returns a correct set of slashes for given $path. (\\ for Windows paths and / for other paths.)
 *
 * @param string $path Path to check
 * @return string Set of slashes ("\\" or "/")
 * @static
 */
	function correctSlashFor($path) {
		return $this->isWindowsPath($path) ? '\\' : '/';
	}
/**
 * Returns $path with added terminating slash (corrected for Windows or other OS).
 *
 * @param string $path Path to check
 * @return string
 * @static
 */
function slashTerm($path) {
		  return $path . ($this->isSlashTerm($path) ? null : $this->correctSlashFor($path));
	 }
/**
 * Returns $path with $element added, with correct slash in-between.
 *
 * @param string $path
 * @param string $element
 * @return string
 * @static
 */
	function addPathElement($path, $element) {
		return $this->slashTerm($path) . $element;
	}
/**
 * Returns true if the File is in a given CakePath.
 *
 * @return boolean
 */
	function inCakePath($path = '') {
		$dir = substr($this->slashTerm(ROOT), 0, -1);
		$newdir = $this->slashTerm($dir . $path);
		return $this->inPath($newdir);
	 }
/**
 * Returns true if the File is in given path.
 *
 * @return boolean
 */
	function inPath($path = '') {
		$dir = substr($this->slashTerm($path), 0, -1);
		$return = preg_match('/^' . preg_quote($this->slashTerm($dir), '/') . '(.*)/', $this->slashTerm($this->pwd()));
		if ($return == 1) {
			return true;
		} else {
			return false;
		}
	}
/**
 * Create a directory structure recursively.
 *
 * @param string $pathname The directory structure to create
 * @return bool Returns TRUE on success, FALSE on failure
 */
	function mkdirr($pathname, $mode = null) {
		if (is_dir($pathname) || empty($pathname)) {
			return true;
		}

		if (is_file($pathname)) {
			trigger_error('mkdirr() File exists', E_USER_WARNING);
			return false;
		}
		$nextPathname = substr($pathname, 0, strrpos($pathname, DIRECTORY_SEPARATOR));

		if ($this->mkdirr($nextPathname, $mode)) {
			if (!file_exists($pathname)) {
				umask (0);
				$mkdir = mkdir($pathname, $mode);
				return $mkdir;
			}
		}
		return false;
	}
/**
 * Returns the size in bytes of this Folder.
 *
 * @param string $directory Path to directory
 */
	function dirsize() {
		$size = 0;
		$directory = $this->slashTerm($this->path);
		$stack = array($directory);
		$count = count($stack);
		for ($i = 0, $j = $count; $i < $j; ++$i) {
			if (is_file($stack[$i])) {
				$size += filesize($stack[$i]);
			} elseif (is_dir($stack[$i])) {
				$dir = dir($stack[$i]);

				while (false !== ($entry = $dir->read())) {
					if ($entry == '.' || $entry == '..') {
						continue;
					}
					$add = $stack[$i] . $entry;

					if (is_dir($stack[$i] . $entry)) {
						$add = $this->slashTerm($add);
					}
					$stack[ ]= $add;
				}
				$dir->close();
			}
			$j = count($stack);
		}
		return $size;
	}
}
?>