1: <?php
2: /**
3: * This file contains the the static directory handler class.
4: *
5: * @package Core
6: * @subpackage Util
7: * @version SVN Revision $Rev:$
8: *
9: * @author Frederic Schneider
10: * @copyright four for business AG <www.4fb.de>
11: * @license http://www.contenido.org/license/LIZENZ.txt
12: * @link http://www.4fb.de
13: * @link http://www.contenido.org
14: */
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: /**
18: * Class for directory handling.
19: * Provides functions for dealing with directories.
20: *
21: * @package Core
22: * @subpackage Util
23: */
24: class cDirHandler {
25:
26: /**
27: * Creates a new dir.
28: *
29: * @param string $pathname the name and path of the new dir
30: * @param bool $recursive
31: * @return bool Returns true on success or false on failure.
32: */
33: public static function create($pathname, $recursive = false) {
34: // skip if dir already exists (better check with is_dir?)
35: if (cFileHandler::exists($pathname)) {
36: return true;
37: }
38: // reset umask and store old umask
39: $oldumask = umask(0);
40: // calc mode from setting or default
41: $mode = cRegistry::getConfigValue('default_perms', 'directory', 0777);
42: // create dir with given mode
43: $success = mkdir($pathname, $mode, $recursive);
44: // reset umask to old umask
45: umask($oldumask);
46: // return success
47: return $success;
48: }
49:
50: /**
51: * Removes a dir from the filesystem
52: *
53: * @param string $dirname The path to the directory
54: * @throws cInvalidArgumentException if the dir with the given dirname
55: * does not exist
56: * @return bool Returns true on success or false on failure.
57: */
58: public static function remove($dirname) {
59: if (!cFileHandler::exists($dirname)) {
60: throw new cInvalidArgumentException('The directory ' . $dirname . ' could not be accessed because it does not exist.');
61: }
62: return rmdir($dirname);
63: }
64:
65: /**
66: * Moves a dir
67: *
68: * @param string $dirname The path and name of the directory
69: * @param string $destination the destination. Note that the dir can also
70: * be renamed in the process of moving it
71: * @throws cInvalidArgumentException if the dir with the given dirname
72: * does not exist
73: * @return bool Returns true on success or false on failure.
74: */
75: public static function move($dirname, $destination) {
76: if (!cFileHandler::exists($dirname)) {
77: throw new cInvalidArgumentException('The directory ' . $dirname . ' could not be accessed because it does not exist.');
78: }
79:
80: $success = rename($dirname, $destination);
81:
82: if ($success) {
83: self::setDefaultDirPerms($destination);
84: }
85:
86: return $success;
87: }
88:
89: /**
90: * Renames a dir
91: *
92: * @param string $dirname the name and path of the dir
93: * @param string $new_dirname the new name of the dir
94: */
95: public static function rename($dirname, $new_dirname) {
96: self::move($dirname, $new_dirname);
97: }
98:
99: /**
100: * Changes the dir permissions
101: *
102: * @param string $dirname the name and path of the dir
103: * @param int $mode the new access mode : php chmod needs octal value
104: * @throws cInvalidArgumentException if the dir with the given dirname
105: * does not exist
106: * @return bool Returns true on success or false on failure.
107: */
108: public static function chmod($dirname, $mode) {
109: if (!cFileHandler::exists($dirname)) {
110: throw new cInvalidArgumentException('The directory ' . $dirname . ' could not be accessed because it does not exist.');
111: }
112: // chmod needs octal value for correct execution.
113: $mode = intval($mode, 8);
114: return @chmod($dirname, $mode);
115: }
116:
117: /**
118: * Sets the default directory permissions on the given directory.
119: *
120: * @param string $dirname the name of the directory
121: * @return bool Returns true on success or false on failure.
122: */
123: public static function setDefaultDirPerms($dirname) {
124: $cfg = cRegistry::getConfig();
125: $dirPerms = $cfg['default_perms']['directory'];
126:
127: return self::chmod($dirname, $dirPerms);
128: }
129:
130: /**
131: * Deletes a directory and all of its content.
132: *
133: * @param string $dirname the name of the directory which should be deleted
134: * @throws cInvalidArgumentException if dirname is empty
135: * @return bool Returns true on success or false on failure.
136: */
137: public static function recursiveRmdir($dirname) {
138: if ($dirname == '') {
139: throw new cInvalidArgumentException('Directory name must not be empty.');
140: }
141:
142: // make sure $dirname ends with a slash
143: if (substr($dirname, -1) !== '/') {
144: $dirname .= '/';
145: }
146:
147: foreach (new DirectoryIterator($dirname) as $file) {
148: if ($file != "." && $file != "..") {
149: $file = $dirname . $file;
150: if (is_dir($file)) {
151: self::recursiveRmdir($file);
152: } else {
153: unlink($file);
154: }
155: }
156: }
157:
158: return rmdir($dirname);
159: }
160:
161: /**
162: * This functions reads the content from given directory.
163: * Optionally options are to read the directory recursive or to list only
164: * directories.
165: *
166: * @param string $dirName directory
167: * @param bool $recursive flag to read recursive
168: * @param bool $dirOnly flag to list only directories
169: * @return boolean Ambigous multitype:unknown string mixed >
170: */
171: public static function read($dirName, $recursive = false, $dirOnly = false) {
172: if (!is_dir($dirName)) {
173: return false;
174: } else {
175: $dirContent = array();
176: if ($recursive == false) {
177: $dirHandle = opendir($dirName);
178: $dirContent = array();
179: while (false !== ($file = readdir($dirHandle))) {
180: if (!self::fileNameIsDot($file)) {
181: // get only directories
182: if ($dirOnly == true) {
183: if (is_dir($dirName . $file)) {
184: $dirContent[] = $file;
185: }
186: } else {
187: $dirContent[] = $file;
188: }
189: }
190: }
191: closedir($dirHandle);
192: }
193:
194: else {
195: $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirName), RecursiveIteratorIterator::SELF_FIRST);
196: foreach ($objects as $name => $file) {
197:
198: if (!self::fileNameIsDot($file)) {
199: $fileName = str_replace("\\", "/", $file->getPathName());
200:
201: // get only directories
202: if ($dirOnly == true) {
203:
204: if (is_dir($fileName)) {
205: $dirContent[] = $fileName;
206: }
207: } else {
208: $dirContent[] = $fileName;
209: }
210: }
211: }
212: }
213: }
214: return $dirContent;
215: }
216:
217: /**
218: * Check if given filename is either '.' or '..'.
219: *
220: * @param string $fileName
221: * @return boolean
222: */
223: public static function fileNameIsDot($fileName) {
224: if ($fileName != '.' && $fileName != '..') {
225: return false;
226: } else {
227: return true;
228: }
229: }
230:
231: }
232: