Ticket #627: housekeeping.php

File housekeeping.php, 5.0 KB (added by Torsten Riemer, 10 years ago)

housekeeping.php

Line 
1<?php
2
3 /**
4 * housekeeping.php
5 *
6 * Removes various annoying hidden and/or useless files leftover from
7 * different people doing different stuff on the filesystem using
8 * various OSes.
9 *
10 * @author zytzagoo <zytzagoo at gmail dot com>
11 * @url http://zytzagoo.net/blog/?p=43
12 * @version 0.2
13 *
14 * @license The MIT License http://www.opensource.org/licenses/mit-license.php
15 *
16 * @usage rdir_cleanup('.'); // just a 'test-run' in the current working dir
17 * @usage rdir_cleanup('.', true); // nuke everything from the cwd and further below
18 * @notice Exclusions are possible. See the comments for rdir_cleanup()
19 *
20 * Copyright (c) 2008 zytzagoo.
21 *
22 * Permission is hereby granted, free of charge, to any person obtaining a copy
23 * of this software and associated documentation files (the "Software"), to deal
24 * in the Software without restriction, including without limitation the rights
25 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26 * copies of the Software, and to permit persons to whom the Software is
27 * furnished to do so, subject to the following conditions:
28 *
29 * The above copyright notice and this permission notice shall be included in
30 * all copies or substantial portions of the Software.
31 *
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
38 * THE SOFTWARE.
39 */
40
41 /**
42 * changelog:
43 * - Suggesteion by suk: http://zytzagoo.net/blog/?p=43#comment-1751
44 * -> added different new line printing depending on the running environment
45 */
46
47 /**
48 * A global array of filenames which are to be found
49 * note: MacOS hidden files (those starting with '._') are
50 * always searched for, because I find them specially annoying.
51 */
52 $annoying_filenames = array(
53 '.DS_Store', // mac specific
54 '.localized', // mac specific
55 'Thumbs.db' // windows specific
56 );
57
58 // shorthand for either \n or <br/>\n depending on the env the script is running in
59 if (PHP_SAPI === 'cli') {
60 define('NEW_LINE', PHP_EOL);
61 } else {
62 define('NEW_LINE', '<br/>' . PHP_EOL);
63 }
64
65 /**
66 * Recursively scan directories for presence of MacOS hidden and
67 * other annoying files. If specified, the function will delete them.
68 *
69 * @global array $annoying_files An array of filenames which are
70 * considered "annoying" (to say the least)
71 * @param string $dir The starting directory
72 * @param bool $do_delete Delete the annoying files or just print
73 * them out (if they're found). Default false.
74 * @param array $exclude An array of files/folders to exclude from
75 * the scan, defaults to '.' and '..'
76 */
77 function rdir_cleanup($dir, $do_delete = false, $exclude = array('.', '..')) {
78
79 global $annoying_filenames;
80 $d = opendir($dir);
81
82 while ($f = readdir($d)) {
83
84 // skip the files/dirs that are to be excluded (exact string matching)
85 if (in_array($f, $exclude, true)) {
86 continue;
87 }
88
89 // check if it's a MacOS hidden file
90 $mac_hidden = strpos($f, '._');
91 if ($mac_hidden !== false && $mac_hidden === 0) {
92 /**
93 * Assume it's a mac hidden file only if it starts with
94 * a dot and an underscore, other files might have that
95 * string somewhere else in the filename and we don't want to
96 * nuke those.
97 */
98 echo NEW_LINE . $dir . DIRECTORY_SEPARATOR . $f . ' appears to be a mac hidden file.';
99 if ($do_delete === true) {
100 if (unlink($dir . DIRECTORY_SEPARATOR . $f) === true) {
101 echo ' deleted.';
102 }
103 }
104 }
105
106 // check if it is an annoying filename
107 if (in_array($f, $annoying_filenames, true)) {
108 echo NEW_LINE . $dir . DIRECTORY_SEPARATOR . $f . ' matched an annoying filename.';
109 if ($do_delete === true) {
110 if (unlink($dir . DIRECTORY_SEPARATOR . $f) === true) {
111 echo ' deleted.';
112 }
113 }
114 }
115
116 // recurse further if needed
117 if (is_dir($dir . DIRECTORY_SEPARATOR . $f)) {
118 rdir_cleanup($dir . DIRECTORY_SEPARATOR . $f, $do_delete, $exclude);
119 continue;
120 }
121
122 }
123
124 closedir($d);
125
126 }