-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdms_client.module
More file actions
335 lines (303 loc) · 10.4 KB
/
Copy pathdms_client.module
File metadata and controls
335 lines (303 loc) · 10.4 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
/**
* Implements hook_menu().
*/
function dms_client_menu() {
// Report page
$items['admin/reports/dms'] = array(
'title' => 'DMS Report',
'page callback' => 'dms_client_report_callback',
'access callback' => TRUE
);
// Module settings
$items['admin/config/system/dms_client/settings'] = array(
'title' => 'DMS Client Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('dms_client_settings_form'),
'file' => 'dms_client.inc',
'access callback' => TRUE
);
// Actions
$items['dms-action/cron-run'] = array(
'title' => 'CRON run from dms call',
'page callback' => 'dms_client_cron_run',
'access callback' => TRUE,
'type' => MENU_CALLBACK
);
$items['dms-action/cache-clear'] = array(
'title' => 'Cache clear from dms call',
'page callback' => 'dms_client_cache_clear',
'access callback' => TRUE,
'type' => MENU_CALLBACK
);
return $items;
}
/**
* Menu callback
*/
function dms_client_report_callback() {
$response = array(
'code' => -1
);
// Request analysis
$params = drupal_get_query_parameters();
$req_valid = dms_client_valid_request($params);
// Available modes (default: 'light')
$arr_modes = array('light', 'full', 'specific');
$req_mode = 'light';
if (array_key_exists('mode', $params) && in_array($params['mode'], $arr_modes)) {
$req_mode = $params['mode'];
}
if ($req_valid) {
$response['code'] = 1;
$response['system_status'] = dms_client_system_status($req_mode == 'light');
if ($req_mode == 'full' || in_array('updates', $params['checklist'])) {
$response['updates'] = dms_client_updates_status();
}
if ($req_mode == 'full' || in_array('features', $params['checklist'])) {
$response['features'] = dms_client_features_status();
}
if ($req_mode == 'full' || in_array('statistics', $params['checklist'])) {
$response['statistics'] = dms_client_statistics();
}
if ($req_mode == 'full' || in_array('hack', $params['checklist'])) {
$response['hack'] = dms_client_hack_check();
}
}
// Call every module that implement hook_dms_get_report() to populate the output.
// We do not use module_invoke_all() to be able to use variable by reference.
foreach (module_implements('dms_get_report') as $module) {
$function = $module . '_dms_get_report';
$function($response);
}
drupal_add_http_header('Content-Type', 'application/json');
drupal_add_http_header('Access-Control-Allow-Origin', "*");
drupal_add_http_header('Access-Control-Allow-Methods', 'GET,POST');
echo json_encode($response);
}
/**
* Menu callback
*/
function dms_client_cron_run() {
return array(
'code' => drupal_cron_run() ? 1 : -1
);
}
/**
* Menu callback
*/
function dms_client_cache_clear() {
return array(
'code' => drupal_flush_all_caches() ? 1 : -1
);
}
/**
* Menu callback
*
* @param $params
* Query parameters
*/
function dms_client_valid_request($params) {
// If php >= 5.5 we can use password_verify()
return (array_key_exists('secret', $params) && dms_client_password_verify(variable_get('dms_client_secret', dms_client_generateRandomString()), $params['secret'])) ? TRUE : FALSE;
}
/**
* Verify a secret
*
* @param $pass
* The clear secret
* @param $hash
* The hash to match
*/
function dms_client_password_verify($pass, $hash) {
$expected = crypt($pass, $pass);
return $expected == $hash;
}
/**
* Generate a secret
*
* @param $length
* The length of the generated secret
*/
function dms_client_generateRandomString($length = 255) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
/**
* System status (see /modules/system/system.admin.inc)
*
* @param $check
* If true, only returns a boolean whether there are system status errors.
*/
function dms_client_system_status($check = FALSE) {
// Load .install files
include_once DRUPAL_ROOT . '/includes/install.inc';
drupal_load_updates();
// Check run-time requirements and status information.
$requirements = module_invoke_all('requirements', 'runtime');
usort($requirements, '_system_sort_requirements');
if ($check) {
return drupal_requirements_severity($requirements) == REQUIREMENT_ERROR;
}
return $requirements;
}
/**
* Updates status (see /modules/update/update.manager.inc)
*
* @param $check
* If true, only returns a boolean whether there are system status errors.
*/
function dms_client_updates_status($check = FALSE) {
$available = update_get_available(TRUE);
if (empty($available)) {
return array('error' => t('There was a problem getting update information. Try again later.'));
}
// This will be a nested array. The first key is the kind of project, which
// can be either 'enabled', 'disabled', 'manual' (projects which require
// manual updates, such as core). Then, each subarray is an array of
// projects of that type, indexed by project short name, and containing an
// array of data for cells in that project's row in the appropriate table.
$projects = array();
module_load_include('inc', 'update', 'update.compare');
$project_data = update_calculate_project_data($available);
foreach ($project_data as $name => $project) {
// Filter out projects which are up to date already.
if ($project['status'] == UPDATE_CURRENT) {
continue;
}
// The project name to display can vary based on the info we have.
if (!empty($project['title'])) {
if (!empty($project['link'])) {
$project_name = l($project['title'], $project['link']);
}
else {
$project_name = check_plain($project['title']);
}
}
elseif (!empty($project['info']['name'])) {
$project_name = check_plain($project['info']['name']);
}
else {
$project_name = check_plain($name);
}
if ($project['project_type'] == 'theme' || $project['project_type'] == 'theme-disabled') {
$project_name .= ' ' . t('(Theme)');
}
if (empty($project['recommended'])) {
// If we don't know what to recommend they upgrade to, we should skip
// the project entirely.
continue;
}
$recommended_release = $project['releases'][$project['recommended']];
$recommended_version = $recommended_release['version'] . ' ' . l(t('(Release notes)'), $recommended_release['release_link'], array('attributes' => array('title' => t('Release notes for @project_title', array('@project_title' => $project['title'])))));
if ($recommended_release['version_major'] != $project['existing_major']) {
$recommended_version .= '<div title="Major upgrade warning" class="update-major-version-warning">' . t('This update is a major version update which means that it may not be backwards compatible with your currently running version. It is recommended that you read the release notes and proceed at your own risk.') . '</div>';
}
// Create an entry for this project.
$entry = array(
'title' => $project_name,
'installed_version' => $project['existing_version'],
'recommended_version' => $recommended_version,
);
switch ($project['status']) {
case UPDATE_NOT_SECURE:
case UPDATE_REVOKED:
$entry['title'] .= ' ' . t('(Security update)');
$type = 'security';
break;
case UPDATE_NOT_SUPPORTED:
$type = 'unsupported';
$entry['title'] .= ' ' . t('(Unsupported)');
break;
case UPDATE_UNKNOWN:
case UPDATE_NOT_FETCHED:
case UPDATE_NOT_CHECKED:
case UPDATE_NOT_CURRENT:
$type = 'recommended';
break;
default:
// Jump out of the switch and onto the next project in foreach.
continue 2;
}
if ($name != 'drupal') {
$projects[$type][$name] = $entry;
} else {
$entry['type'] = $type;
$projects[$name] = $entry;
}
}
if (empty($projects)) {
return array('uptodate' => t('All of your projects are up to date.'));
}
return $projects;
}
/**
* Features status (see /sites/all/modules/contrib/features/features.admin.inc)
*/
function dms_client_features_status() {
if (!module_exists('features')) {
return array('error' => t('The features module is not enabled.'));
}
$features = features_get_features();
if ( empty($features) ) {
return array('nofeatures' => t('No Features were found.'));
}
$form = array('#features' => $features);
return $form;
}
/**
* Statistics (users count, nodes count, lang, etc.)
*/
function dms_client_statistics() {
// Lang
$statistics['languages'] = language_list('enabled');
$statistics['language_default'] = language_default();
// Users count
$statistics['users'] = db_select('users', 'u')->countQuery()->execute()->fetchField();
// Nodes count
$statistics['nodes']['published'] = db_select('node', 'n')->condition('n.status', 1)->countQuery()->execute()->fetchField();
$statistics['nodes']['unpublished'] = db_select('node', 'n')->condition('n.status', 0)->countQuery()->execute()->fetchField();
return $statistics;
}
/**
* Hack status (based on MD5check module)
*/
function dms_client_hack_check()
{
if (!module_exists('md5check')) {
return array('error' => t('The md5check module is not enabled.'));
}
$hack = array('code' => 1);
// Fetches an array of all active modules and looping through them
$modules = module_list(TRUE, FALSE);
foreach ($modules as $moduleName) {
// Loads the module data from database
$module = md5check_get_values_for_module($moduleName);
// Creates a md5 hash of the modules files
$md5 = md5check_calculate_md5_value_for_module($module);
// If the existing module md5 hash isn't null (as never runned before) we'll check if it has changed
// else we'll just store the new value
if ($module->md5check !== NULL) {
if ($md5 != $module->md5check) {
// The value has changed as in a files content has changed - we'll report this
watchdog('security',
t('Security issue. Module @moduleName has changed.', array('@moduleName' => $moduleName)),
array(),
WATCHDOG_CRITICAL
);
$hack['modules'][] = $moduleName;
// and stores the new value so we won't report the same change again
md5check_setMD5value($module->filename, $md5);
}
}
else {
// Existing value is null - new value will be stored
md5check_setMD5value($module->filename, $md5);
}
}
return $hack;
}