This repository was archived by the owner on Oct 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileCleaner.php
More file actions
executable file
·139 lines (127 loc) · 3.81 KB
/
fileCleaner.php
File metadata and controls
executable file
·139 lines (127 loc) · 3.81 KB
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
<?php
define( "CURRENT_DIR", pathinfo( $argv[0], PATHINFO_DIRNAME ) );
define( "SLASH", strtr( CURRENT_DIR, ["/"] ) === false ? "\\" : "/" );
require_once CURRENT_DIR . SLASH . "File.php";
$settings = json_decode( file_get_contents( CURRENT_DIR . SLASH . "settings.json" ), true );
if( is_null( $settings ) ) die("JSON file contain errors.\n");
foreach( $settings as $folderToClean => $action )
{
foreach( array_diff( scandir( $folderToClean ), [".",".."] ) as $f )
{
$File = new File( $folderToClean . $f );
// files to remove
if( isset( $action["toRemove"] ) && in_array( $File->getExtension(), $action["toRemove"] ) )
{
if( $File->getExtension() === File::FOLDER_EXTENSION )
{
if( isset( explode( ".", $ext )[1] ) )
{
if( inFolder( $File->getPath(), explode( ".", $ext )[1] ) )
{
remove( $File->getPath() );
}
}
else
{
remove( $File->getPath() );
}
}
else
{
remove( $File->getPath() );
}
}
clean( $action, "toCopy", $File );
clean( $action, "toMove", $File );
clean( $action, "toKeep", $File );
}
}
function clean( $actions, $keyAction, $File )
{
if( isset( $actions[$keyAction] ) )
{
foreach( $actions[$keyAction] as $action )
{
foreach( $action as $key => $ext )
{
if( $File->getExtension() === explode( ".", $ext )[0] || explode( ".", $ext )[0] === '' )
{
if( $File->getExtension() === File::FOLDER_EXTENSION )
{
if( isset( explode( ".", $ext )[1] ) )
{
if( inFolder( $File->getPath(), explode( ".", $ext )[1] ) )
{
cleanAction( $keyAction, $File, $key );
}
}
else
{
cleanAction( $keyAction, $File, $key );
}
}
else
{
cleanAction( $keyAction, $File, $key );
}
}
}
}
}
}
function cleanAction( $keyAction, $File, $key)
{
switch( $keyAction )
{
case "toCopy":
if( is_dir( $key . SLASH . $File->getName() ) ) continue;
copy( $File->getPath(), $key . SLASH . $File->getName() );
break;
case "toKeep":
if( $File->getCreationDate() < strtotime( "-" . $key ) )
remove( $File->getPath() );
break;
case "toMove":
rename( $File->getPath(), $key . SLASH . $File->getName() );
break;
}
}
function remove( $src )
{
if( is_file( $src ) )
{
unlink($src);
return;
}
foreach( array_diff( scandir( $src ), [".",".."] ) as $file )
{
if( is_dir( $src . SLASH . $file ) )
{
removeDir( $src . SLASH . $file);
}
else
{
unlink( $src . SLASH . $file );
}
}
rmdir($src);
}
function inFolder( $path, $ext )
{
foreach( array_diff( scandir( $path ), [".",".."] ) as $f )
{
$File = new File( $path . SLASH . $f );
if( $File->getExtension() === $ext )
{
return true;
}
else if( $File->getExtension() === File::FOLDER_EXTENSION )
{
if( inFolder( $File->getPath(), $ext ) )
{
return true;
}
}
}
return false;
}