-
Notifications
You must be signed in to change notification settings - Fork 4
/
ModulesManagerNotification.module
189 lines (153 loc) · 7.06 KB
/
ModulesManagerNotification.module
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* Modules Manager Notification module
*
* Sends out update information for installed modules to an email
* This module required LazyCron core module installed
*
* @author Soma <philipp@urlich.ch>
*
*/
class ModulesManagerNotification extends WireData implements Module, ConfigurableModule {
protected static $intervals = array(
"every5Minutes",
"everyDay",
"every2Days",
"every4Days",
"everyWeek",
"every2Weeks",
"every4Weeks"
);
protected static $defaults = array(
'update_interval' => 'everyWeek',
'recipient_email' => '',
'from_email' => '',
'email_subject' => 'Site: yourdomain - ProcessWire module updates available'
);
public static function getModuleInfo() {
return array(
'title' => 'Modules Manager Notification',
'version' => 11,
'summary' => __('Send email notification to specified email address with installed modules updates available.'),
'href' => '',
'author' => "Philipp 'Soma' Urlich",
'singular' => true,
'autoload' => true,
'requires' => array("LazyCron", "ModulesManager")
);
}
public function init() {
if($this->active){
$this->addHook("LazyCron::$this->update_interval", $this, 'runLazyCronJob');
}
}
public function runLazyCronJob() {
$log = new FileLog(wire('config')->paths->logs . 'modulesmanager_notification_log.txt');
$log->prune(100000);
$log->save( " executed modules version update check" );
$modulesManager = $this->modules->get("ModulesManager");
$remoteModules = $modulesManager->createCachefile();
$module_email_text = $this->checkCacheUpdates($remoteModules);
if(strlen(trim($module_email_text))) {
$this->sendNotificationEmail($module_email_text);
}
}
/**
* Check for new module version
* @param object/array $json items array from module feed
*/
public function checkCacheUpdates($remoteModules) {
// get current installed modules in PW and store it in array
// for later use to generate
$modulesArray = array();
foreach($this->modules as $module) {
$modulesArray[$module->className()] = 1;
wire('modules')->getModuleInfo($module->className()); // fixes problems
}
$module_email_text = '';
foreach($remoteModules->items as $item) {
if(array_key_exists($item->class_name, $modulesArray)) {
$remote_version = $this->formatVersion($item->module_version);
// get module infos, we can't use modules->get(module_name) here
// as it would install the module, which we don't want at all
$info = wire('modules')->getModuleInfo($item->class_name);
$local_version = $this->formatVersion($info['version']);
if($remote_version > $local_version) {
$module_text = sprintf($this->_('Module: %1$s v%2$s'), $item->class_name, $local_version);
$module_text .= PHP_EOL;
$module_text .= sprintf($this->_('Found new version available: v%1$s'), $remote_version);
$module_text .= PHP_EOL;
$module_text .= sprintf($this->_('More infos at: %1$s'), $item->url);
$module_text .= PHP_EOL . "––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––" . PHP_EOL;
$module_email_text .= $module_text;
$module_email_text .= PHP_EOL . PHP_EOL;
}
}
}
return $module_email_text;
}
/**
* Send out email notification to site administrator
* @param string $body email body text
*/
protected function sendNotificationEmail($body) {
if(strlen(trim($this->recipient_email))) {
$headline = $this->_("Found following updates for installed modules:");
$headline .= PHP_EOL . PHP_EOL;
$body = $headline . $body;
// $file = fopen(wire('config')->paths->logs . 'modulesmanager_email_log.txt', "wb");
// fwrite( $file, $this->recipient_email . "\n" . $subject . "\n" . $body);
// fclose($file);
$header = '';
if($this->from_email){
$header = 'From: ' . $this->from_email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
}
mail($this->recipient_email, $this->email_subject, (string) $body, $header);
}
}
/**
* used internally for the module to convert version numbers of the modules
* @param int $version version number | major minor revision
* @return string returns string formatted version number
*/
protected function formatVersion($version) {
return preg_replace('/(\d)(?=\d)/', '$1.', str_pad( (string)$version, 3, '0', STR_PAD_LEFT));
}
static public function getModuleConfigInputfields(array $data) {
$data = array_merge(self::$defaults, $data);
$fields = new InputfieldWrapper();
$modules = wire('modules');
$field = $modules->get('InputfieldCheckbox');
$field->attr('name', 'active');
$field->attr('value', 1);
$field->attr('checked', $data['active'] == 1 ? "checked" : '');
$field->label = __('Activate Module');
$fields->append($field);
$field = $modules->get('InputfieldEmail');
$field->attr('name', 'recipient_email');
$field->attr('value', $data['recipient_email']);
$field->label = __('E-Mail Recipient');
$fields->append($field);
$field = $modules->get('InputfieldEmail');
$field->attr('name', 'from_email');
$field->attr('value', $data['from_email']);
$field->label = __('E-Mail From Address');
$field->description = __('Will set "From" header with this email if specified.');
$fields->append($field);
$field = $modules->get('InputfieldText');
$field->attr('name', 'email_subject');
$field->attr('value', $data['email_subject']);
$field->label = __('E-Mail Subject');
$fields->append($field);
$field = $modules->get('InputfieldSelect');
$field->attr('name', 'update_interval');
foreach(self::$intervals as $interval) $field->addOption($interval, $interval);
$field->attr('value', $data['update_interval']);
$field->label = __('Interval');
$field->description = __('The lazy cron interval to check for module updates and send out email notification. I added the "every5Minutes" just for testing purposes.');
$field->notes = __('Note that this requires someone to hit your website to get executed, so interval times can vary.');
$fields->append($field);
return $fields;
}
}