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:
63: return unlink($dirname);
64: }
65:
66: /**
67: * Moves a dir
68: *
69: * @param string $dirname The path and name of the directory
70: * @param string $destination the destination. Note that the dir can also
71: * be renamed in the process of moving it
72: * @throws cInvalidArgumentException if the dir with the given dirname
73: * does not exist
74: * @return bool Returns true on success or false on failure.
75: */
76: public static function move($dirname, $destination) {
77: if (!cFileHandler::exists($dirname)) {
78: throw new cInvalidArgumentException('The directory ' . $dirname . ' could not be accessed because it does not exist.');
79: }
80:
81: $success = rename($dirname, $destination);
82:
83: if ($success) {
84: self::setDefaultDirPerms($destination);
85: }
86:
87: return $success;
88: }
89:
90: /**
91: * Renames a dir
92: *
93: * @param string $dirname the name and path of the dir
94: * @param string $new_dirname the new name of the dir
95: */
96: public static function rename($dirname, $new_dirname) {
97: self::move($dirname, $new_dirname);
98: }
99:
100: /**
101: * Changes the dir permissions
102: *
103: * @param string $dirname the name and path of the dir
104: * @param int $mode the new access mode : php chmod needs octal value
105: * @throws cInvalidArgumentException if the dir with the given dirname
106: * does not exist
107: * @return bool Returns true on success or false on failure.
108: */
109: public static function chmod($dirname, $mode) {
110: if (!cFileHandler::exists($dirname)) {
111: throw new cInvalidArgumentException('The directory ' . $dirname . ' could not be accessed because it does not exist.');
112: }
113: // chmod needs octal value for correct execution.
114: $mode = intval($mode, 8);
115: return chmod($dirname, $mode);
116: }
117:
118: /**
119: * Sets the default directory permissions on the given directory.
120: *
121: * @param string $dirname the name of the directory
122: * @return bool Returns true on success or false on failure.
123: */
124: public static function setDefaultDirPerms($dirname) {
125: $cfg = cRegistry::getConfig();
126: $dirPerms = $cfg['default_perms']['directory'];
127:
128: return self::chmod($dirname, $dirPerms);
129: }
130:
131: /**
132: * Deletes a directory and all of its content.
133: *
134: * @param string $dirname the name of the directory which should be deleted
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 directories.
164: *
165: * @param string $dirName directory
166: *
167: * @param bool $recursive flag to read recursive
168: * @param bool $dirOnly flag to list only directories
169: */
170: public static function read($dirName, $recursive = false, $dirOnly = false) {
171: if (!is_dir($dirName)) {
172: return false;
173: } else {
174: $dirContent = array();
175: if ($recursive == false) {
176: $dirHandle = opendir($dirName);
177: $dirContent = array();
178: while (false !== ($file = readdir($dirHandle))) {
179: if (!self::fileNameIsDot($file)) {
180: // get only directories
181: if ($dirOnly == true) {
182: if (is_dir($dirName . $file)) {
183: $dirContent[] = $file;
184: }
185: } else {
186: $dirContent[] = $file;
187: }
188: }
189: }
190: closedir($dirHandle);
191: }
192:
193: else {
194: $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirName), RecursiveIteratorIterator::SELF_FIRST);
195: foreach ($objects as $name => $file) {
196:
197: if (!self::fileNameIsDot($file)) {
198: $fileName = str_replace("\\", "/", $file->getPathName());
199:
200: // get only directories
201: if ($dirOnly == true) {
202:
203: if (is_dir($fileName)) {
204: $dirContent[] = $fileName;
205: }
206: } else {
207: $dirContent[] = $fileName;
208: }
209: }
210: }
211: }
212: }
213: return $dirContent;
214: }
215:
216: public static function fileNameIsDot($fileName) {
217: if ($fileName != '.' && $fileName != '..') {
218: return false;
219: } else {
220: return true;
221: }
222: }
223:
224: }
225: