-
Notifications
You must be signed in to change notification settings - Fork 1
/
detect-modified-files.php
113 lines (92 loc) · 3.77 KB
/
detect-modified-files.php
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
<?php
/**
* Detect modified files PHP script, version 1.0
* By Pau Iglesias, [email protected]
* License: GPL version 3 or later - http://www.gnu.org/licenses/gpl.txt
*
* Usage: /usr/bin/php [path]detect-modified-files.php [--config=filename] 2>/dev/null
*
* The `config` parameter refers to the config file located in the directory or subdirectories
* from this script location. This parameter is optional, if omitted the script will attempt
* to load by default the file `detect-modified-files-config.php` in the same script directory.
*/
// End if is running under web server
if (isset($_SERVER['HTTP_HOST']) || isset($_SERVER['HTTP_USER_AGENT']) || isset($_SERVER['REQUEST_URI']))
die;
// Initialize
$processed = date('Y-m-d H:i');
// Disable timeout
set_time_limit(0);
// Check command line config file argument
$filename = false;
if (isset($argv) && !empty($argv[1])) {
if (strpos($argv[1], '--config=') === 0) {
$param = explode('=', $argv[1]);
if (count($param) == 2 && !empty($param[1]))
$filename = $param[1];
} elseif ($argv[1] == '-c' && !empty($argv[2])) {
$filename = $argv[2];
}
}
// Check default config file or use another file
$filename = realpath(dirname(__FILE__).'/'.($filename? str_replace('../', '', ltrim($filename, '/')) : 'detect-modified-files-config.php'));
if (!file_exists($filename))
die;
include($filename);
// Check minimum configuration
if (empty($dmf_config['dir']) || (empty($dmf_config['hours']) && empty($dmf_config['minutes'])) || empty($dmf_config['email_to']))
die;
// Array for time description
$time_desc = array();
if (!empty($dmf_config['hours']))
$time_desc[] = $dmf_config['hours'].' h';
if (!empty($dmf_config['minutes']))
$time_desc[] = $dmf_config['minutes'].' m';
// Prepare extensions to find
$items = array();
foreach ($dmf_config['extensions'] as $extension)
$items[] = '-name "*.'.$extension.'"';
// Detect modified files
$result = shell_exec(((isset($dmf_config['niceness']) && $dmf_config['niceness'] !== false)? 'nice -n '.$dmf_config['niceness'].' ' : '').'find '.rtrim($dmf_config['dir'], '/').'/'.' -mmin -'.((empty($dmf_config['hours'])? 0 : ($dmf_config['hours'] * 60)) + (empty($dmf_config['minutes'])? 0 : $dmf_config['minutes'])).(empty($items)? '' : ' \! -type d \( '.implode(' -o ', $items).' \)').' -exec ls -aAl {} \;');
// Prepare result
$count = 0;
$modified = array();
$result = explode("\n", $result);
foreach ($result as $line) {
// Check line result
if (!empty($line)) {
// Check excludes
$is_excluded = false;
if (!empty($dmf_config['excludes']) && is_array($dmf_config['excludes'])) {
foreach ($dmf_config['excludes'] as $exclude) {
if (strpos($line, $exclude) !== false) {
$is_excluded = true;
break;
}
}
}
// Add line and prepare format
if (!$is_excluded) {
$count++;
if (false !== ($pos = strpos($line, $dmf_config['dir'])))
$line = substr($line, 0, $pos)."\n".substr($line, $pos)."\n";
$modified[] = $line;
}
}
}
// Check if send e-mail when no files changes detected
if ($count == 0 && isset($dmf_config['email_if_empty']) && !$dmf_config['email_if_empty'])
die;
// Compose output
$message = '
- Report processed: '.$processed.'
- New or modified files last '.implode(' and ', $time_desc).' in '.$dmf_config['dir'].' ('.implode(', ', $dmf_config['extensions']).')
- Excluded strings: '.((!empty($dmf_config['excludes']) && is_array($dmf_config['excludes']))? '"'.implode('", "', $dmf_config['excludes']).'"' : 'none').'
====================================================================================================
'.implode("\n", $modified).'
'.$count.' files
---End---
';
// And send e-mail
mail($dmf_config['email_to'], $dmf_config['email_subject'].' - '.$processed.' - last '.implode(' ', $time_desc).' - '.$count.' files found', $message);
?>