-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdms_client.module
More file actions
executable file
·993 lines (866 loc) · 25.8 KB
/
Copy pathdms_client.module
File metadata and controls
executable file
·993 lines (866 loc) · 25.8 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
<?php
define('DMS_VERSION', '7.x-1.0');
define('DMS_MODE_LIGHT', 'light');
define('DMS_MODE_FULL', 'full');
define('DMS_MODE_SPECIFIC', 'specific');
/**
* Implements hook_menu().
*/
function dms_client_menu() {
// Module status.
$items['dms/status'] = array(
'title' => 'DMS Status',
'page callback' => 'dms_client_status_callback',
'access callback' => TRUE,
);
// Report generation.
$items['dms/report/generate'] = array(
'title' => 'DMS Report Generation',
'page callback' => 'dms_client_report_generate_callback',
'access callback' => TRUE,
);
// Report generation documentation.
$items['dms/report/describe'] = array(
'title' => 'DMS Report Description',
'page callback' => 'dms_client_report_describe_callback',
'access callback' => TRUE,
);
// Action execution.
$items['dms/action/%/execute'] = array(
'title' => 'DMS Action Execution',
'page callback' => 'dms_client_action_execute_callback',
'page arguments' => array(2),
'access callback' => TRUE,
);
// Action execution documentation.
$items['dms/action/describe'] = array(
'title' => 'DMS Action Execution Description',
'page callback' => 'dms_client_action_describe_callback',
'access callback' => TRUE,
);
// Special path to allow download of private files.
$items['dms/download/%'] = array(
'title' => 'DMS Download Execution',
'page callback' => 'dms_client_download_callback',
'page arguments' => array(2),
'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,
);
return $items;
}
/**
* Check access to the DMS client APIs.
*/
function dms_client_access_callback() {
if(!dms_client_valid_request()) {
dms_client_send_json(array(
'code' => 403,
'error' => 'Not allowed to access the DMS API.'
));
}
}
/**
* Unprotected menu callback to check if
* module is well configured and working.
*
* @return string
* The JSON data.
*/
function dms_client_status_callback() {
// Check if the module is well configured.
$working = dms_client_valid_request();
if ($working) {
$response = array(
'code' => 200,
'data' => array(
'message' => 'DMS is working properly.',
'version' => DMS_VERSION
)
);
} else {
drupal_add_http_header('Status', '403 Not Allowed');
$response = array(
'code' => 403,
'data' => array(
'message' => 'Access not allowed. Please, check access token and allowed IPs.',
'version' => DMS_VERSION
)
);
}
// Return JSON data.
dms_client_send_json($response);
}
/**
* Menu callback for describing report available data.
*
* @return string
* The JSON string of data.
*/
function dms_client_report_describe_callback() {
// Check access before doing anything.
dms_client_access_callback();
// Get request parameters.
$response = array('code' => 200, 'data' => array());
// Display all modes information.
$modes = dms_client_report_modes();
foreach ($modes as $mode => $text) {
$response['data']['modes'][$mode] = $text;
}
// Display all tasks information.
$tasks = dms_client_report_tasks();
foreach ($tasks as $task => $task_data) {
$response['data']['tasks'][$task] = $task_data;
}
// Return JSON data.
dms_client_send_json($response);
}
/**
* Menu callback for describing available actions.
*
* @return string
* The JSON string of data.
*/
function dms_client_action_describe_callback() {
// Check access before doing anything.
dms_client_access_callback();
// Get request parameters.
$response = array('code' => 200, 'data' => array());
// Display all modes information.
$actions = dms_client_actions();
foreach ($actions as $action => $action_data) {
$response['data']['actions'][$action] = $action_data;
}
// Return JSON data.
dms_client_send_json($response);
}
/**
* Menu callback for generating a report.
*
* @return string
* The JSON string of data.
*/
function dms_client_report_generate_callback() {
// Check access before doing anything.
dms_client_access_callback();
// Get request parameters.
$response = array('code' => 200, 'data' => array());
$mode = dms_get_mode_from_request();
$checklist = dms_get_checklist_from_request($mode);
// Execute each tasks.
foreach ($checklist as $key => $task) {
if (function_exists($task['callback'])) {
try {
// Execute the callback.
$start = microtime(true);
$response['data'][$key]['result'] = $task['callback']();
$total = microtime(true) - $start;
$response['data'][$key]['time'] = $total;
$response['data'][$key]['code'] = 200;
} catch (\Exception $e) {
// Internal error response code.
$response['data'][$key]['code'] = 500;
$response['data'][$key]['error'] = 'DMS Client : ' . $e->getMessage();
}
}
}
// Let other modules alter the response.
drupal_alter('dms_client_report', $response['data']);
// Return JSON data.
dms_client_send_json($response);
}
/**
* Menu callback for action execution.
*
* @param string $key
* An action key to execute.
*
* @return string
* The JSON data.
*/
function dms_client_action_execute_callback($key) {
// Check access before doing anything.
dms_client_access_callback();
// Get request parameters.
$response = array('code' => 200, 'data' => array());
// Execute the action.
$actions = dms_client_actions();
foreach ($actions as $action_key => $action) {
if ($action_key == $key) {
if (function_exists($action['callback'])) {
try {
// Execute the action.
$start = microtime(true);
$response['data'][$action_key]['result'] = $action['callback']();
$total = microtime(true) - $start;
$response['data'][$action_key]['time'] = $total;
$response['data'][$action_key]['code'] = 200;
} catch (\Exception $e) {
// Internal error response code.
$response['data'][$action_key]['code'] = 500;
$response['data'][$action_key]['error'] = 'DMS Client : ' . $e->getMessage();
}
}
}
}
// Let other modules alter the response.
drupal_alter('dms_client_report', $response['data']);
// Return JSON data.
dms_client_send_json($response);
}
/**
* Menu callback for downloads.
*
* @param string $path
* A path to a file to download, encoded in base64.
*/
function dms_client_download_callback($path) {
// Check access before doing anything.
dms_client_access_callback();
// Decode the path.
$path = base64_decode($path);
// Check if file exists and is allowed.
if (!file_exists($path) || !dms_client_download_allowed($path)) {
drupal_not_found();
}
// Send HTTP headers.
$file = pathinfo($path);
drupal_add_http_header('Content-Type', 'application/octet-stream');
drupal_add_http_header('Content-Length', filesize($path));
drupal_add_http_header('Content-Disposition', 'attachment; filename="' . $file['basename'] . '"');
drupal_send_headers();
// Transfer file in 1024 byte chunks to save memory usage.
if ($fd = fopen($path, 'rb')) {
while (!feof($fd)) {
print fread($fd, 1024);
}
fclose($fd);
}
drupal_exit();
}
/**
* Check if a download is allowed.
*
* @param string $path
* A path to a file to download.
*
* @return boolean
* True if allowed, False otherwise.
*/
function dms_client_download_allowed($path) {
$allowed = array(
drupal_get_path('module', 'dms_client') . '/dumps/database.gz'
);
// Let other modules alter the list of allowed files.
drupal_alter('dms_client_report_modes', $allowed, $path);
return in_array($path, $allowed);
}
/**
* Return the URL to a downloadable file.
*
* @param string $path
* A path to a file to download.
*
* @return string|FALSE
* The URL to access the file, False if not allowed.
*/
function dms_client_download_get_url($path) {
if (!dms_client_download_allowed($path)) {
return FALSE;
}
// Return the generated URL.
return url('dms/download/' . base64_encode($path), array('query' => drupal_get_query_parameters(), 'absolute' => TRUE));
}
/**
* Get the mode to use from request.
*
* @return string
* The mode to use : light, full, specific.
*/
function dms_get_mode_from_request() {
// Get request parameters.
$params = drupal_get_query_parameters();
// Available modes (default: 'light')
$modes = dms_client_report_modes();
$mode = 'light';
if (array_key_exists('mode', $params) && array_key_exists($params['mode'], $modes)) {
$mode = $params['mode'];
}
// If specifics are asked, auto change mode.
if (array_key_exists('specifics', $params)) {
$mode = 'specific';
}
return $mode;
}
/**
* Get the specific tasks to do from request.
*
* @return array
* A list of tasks keys.
*/
function dms_get_specifics_from_request() {
$keys = array();
$params = drupal_get_query_parameters();
if (isset($params['specifics']) && !empty($params['specifics'])) {
if (is_array($params['specifics'])) {
$keys = $params['specifics'];
} elseif (strpos($params['specifics'], ',') !== FALSE) {
$keys = explode(',', $params['specifics']);
} else {
$keys = array($params['specifics']);
}
}
return $keys;
}
/**
* Get the checklist of tasks to do from the request.
*
* @param string $mode
* The mode to use.
*
* @return array
* The array of checklist of tasks.
*/
function dms_get_checklist_from_request($mode) {
$tasks = array();
$specifics = dms_get_specifics_from_request();
$all_tasks = dms_client_report_tasks();
// Filter by mode/specifics.
foreach ($all_tasks as $key => $task) {
if ($mode !== DMS_MODE_SPECIFIC && in_array($mode, $task['modes'])) {
// If task is enabled for given mode.
$tasks[$key] = $task;
} elseif ($mode === DMS_MODE_SPECIFIC && in_array($key, $specifics)) {
// If task is explicitly asked.
$tasks[$key] = $task;
}
}
return $tasks;
}
/**
* Return all defined modes for generating reports.
*
* @return array
* An array of modes.
*/
function dms_client_report_modes() {
$modes = array(
DMS_MODE_FULL => 'Full mode, all main tasks.',
DMS_MODE_LIGHT => 'Light mode, only a few tasks.',
DMS_MODE_SPECIFIC => 'Specific mode, you can choose which tasks to execute via the "specifics" query parameter.'
);
// Let other modules alter the list of modes.
drupal_alter('dms_client_report_modes', $modes);
return $modes;
}
/**
* Return all defined tasks for generating reports.
*
* @return array
* An array of tasks.
*/
function dms_client_report_tasks() {
$tasks = array(
'system_status' => array(
'modes' => array(DMS_MODE_FULL, DMS_MODE_LIGHT),
'callback' => 'dms_client_system_status'
),
'updates' => array(
'modes' => array(DMS_MODE_FULL),
'callback' => 'dms_client_updates_status'
),
'features' => array(
'modes' => array(DMS_MODE_FULL),
'callback' => 'dms_client_features_status'
),
'statistics' => array(
'modes' => array(DMS_MODE_FULL),
'callback' => 'dms_client_statistics'
),
'hack' => array(
'modes' => array(DMS_MODE_FULL),
'callback' => 'dms_client_hack_check'
),
'logs' => array(
'modes' => array(DMS_MODE_FULL),
'callback' => 'dms_client_logs_check'
),
'server' => array(
'modes' => array(DMS_MODE_FULL),
'callback' => 'dms_client_server_check'
),
);
// Let other modules alter the list of tasks.
drupal_alter('dms_client_report_tasks', $tasks);
return $tasks;
}
/**
* Return all defined actions.
*
* @return array
* An array of actions.
*/
function dms_client_actions() {
$actions = array(
'uli' => array(
'callback' => 'dms_client_action_uli',
'description' => 'Login the user as administrator.',
'title' => 'Login'
),
'cron_run' => array(
'callback' => 'dms_client_action_cron',
'description' => 'Run the Drupal CRON.',
'title' => 'Run CRON'
),
'cache_clear' => array(
'callback' => 'dms_client_action_cache_clear',
'description' => 'Clear all Drupal caches.',
'title' => 'Clear cache'
),
'dump_db' => array(
'callback' => 'dms_client_action_dump_db',
'description' => 'Dump the website database.',
'title' => 'Dump DB'
),
);
// Let other modules alter the list of actions.
drupal_alter('dms_client_actions', $actions);
return $actions;
}
/**
* Check the access token for current request.
*
* @return bool
* True if access is granted, False otherwise.
*/
function dms_client_valid_request() {
// Check token is here.
$params = drupal_get_query_parameters();
if (!array_key_exists('secret', $params)) {
return FALSE;
}
// Check tokens & IP.
$server_token = $params['secret'];
$client_token = dms_client_get_token();
$current_ip = ip_address();
return ($server_token == $client_token) && dms_client_check_ip($current_ip);
}
/**
* Get the current client token.
*
* @return string
* The token.
*/
function dms_client_get_token() {
return variable_get('dms_client_secret', dms_client_generate_token());
}
/**
* Check the current request IP is allowed.
*
* @param string $ip
* The IP to check.
*
* @return boolean
* True if IP is allowed, False otherwise.
*/
function dms_client_check_ip($ip) {
// Bypass if developer mode.
if (variable_get('dms_client_dev_mode', FALSE)) {
return TRUE;
}
// Check if IP is allowed.
$allowed = variable_get('dms_client_allowed_ips', '');
if (strlen($allowed) > 0) {
$allowed = explode("\n", str_replace("\r", '', $allowed));
return in_array($ip, $allowed);
}
return FALSE;
}
/**
* Helper to send JSON data.
*
* @param array $data
*/
function dms_client_send_json(array $data) {
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($data);
exit;
}
/**
* Generates a random token.
*
* @param $length
* The length of the generated token.
*
* @return string
* The random string.
*/
function dms_client_generate_token($length = 255) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
/** * * * * * * * * *
* TASKS CALLBACKS. *
* * * * * * * * * **/
/**
* System status.
*
* @see see modules/system/system.admin.inc
*
* @return array
*/
function dms_client_system_status() {
// 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');
return $requirements;
}
/**
* Updates status.
*
* @see modules/update/update.manager.inc
* @return array
* @throws \Exception
*/
function dms_client_updates_status() {
module_load_include('module', 'update', 'update');
$available = update_get_available(TRUE);
if (empty($available)) {
throw new \Exception('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 features/features.admin.inc
* @return array
* @throws \Exception
*/
function dms_client_features_status() {
if (!module_exists('features')) {
throw new \Exception('The features module is not enabled.');
}
module_load_include('inc', 'features', 'features.export');
// Sort the Features list before compiling the output.
$features = features_get_features();
ksort($features);
foreach ($features as $k => $m) {
$storage = $feature_key = NULL;
switch (features_get_storage($m->name)) {
case FEATURES_DEFAULT:
$feature_key = 'default';
$storage = '';
break;
case FEATURES_REBUILDABLE:
$feature_key = 'rebuildable';
$storage = '';
break;
case FEATURES_OVERRIDDEN:
$feature_key = 'overridden';
$storage = 'Overridden';
break;
case FEATURES_NEEDS_REVIEW:
$feature_key = 'need_review';
$storage = 'Need review';
break;
}
if ($m->status) {
$rows['enabled'][$feature_key][$m->name] = array(
'name' => $m->info['name'],
'version' => $m->info['version'],
'storage' => $storage
);
}
else {
$rows['disabled'][$m->name] = array(
'name' => $m->info['name'],
'version' => $m->info['version'],
'storage' => $storage
);
}
}
if (empty($rows)) {
return array('nofeatures' => t('No Features were found.'));
}
return $rows;
}
/**
* Statistics.
* For example, users count, nodes count, lang, etc.
*
* @return array
*/
function dms_client_statistics() {
// Languages.
$languages = language_list('enabled');
$statistics['language'] = array(
'list' => array_shift($languages),
'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;
}
/**
* Check if modules are hacked with MD5check.
*
* @return array
* @throws \Exception
*/
function dms_client_hack_check() {
if (!module_exists('md5check')) {
throw new \Exception('The md5check module is not enabled.');
}
$hacked = array();
// Fetches an array of all active modules and looping through them
$modules = module_list(TRUE, FALSE);
foreach ($modules as $moduleName) {
$modulePath = drupal_get_path('module', $moduleName);
// Don't check customs
if (strpos($modulePath, 'modules/features') === FALSE && strpos($modulePath, 'modules/custom') === FALSE) {
// 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
$hacked[] = $moduleName;
}
}
else {
// Existing value is null - new value will be stored
md5check_setMD5value($module->filename, $md5);
}
}
}
return $hacked;
}
/**
* Logs status (daily counter by severity).
*
* @TODO check if dblog disabled.
*
* @return array
*/
function dms_client_logs_check() {
$logs = array();
$logs_status = array(
'danger' => array(WATCHDOG_ALERT, WATCHDOG_CRITICAL, WATCHDOG_EMERGENCY),
'warning' => array(WATCHDOG_ERROR, WATCHDOG_WARNING),
'info' => array(WATCHDOG_NOTICE, WATCHDOG_DEBUG, WATCHDOG_INFO)
);
$thisMorning = strtotime('today midnight');
foreach ($logs_status as $status => $severities) {
$query = db_select('watchdog', 'w')
->condition('w.timestamp', $thisMorning, '>')
->condition('w.severity', $severities, 'IN')
->fields('w', array('wid'));
$logs[$status] = $query->countQuery()->execute()->fetchField();
}
return $logs;
}
/**
* Server information.
*
* @see scripts/serverinfo.sh
* @return array
* @throws \Exception
*/
function dms_client_server_check() {
$path = drupal_get_path('module', 'dms_client') . '/scripts/serverinfo.sh';
if (file_exists($path) && is_readable($path)) {
$output = '';
// Execute the script.
exec($path, $output);
if (is_array($output)) {
$output = array_shift($output);
}
// Check that output is not null.
if (is_null($output) || strlen($output) == 0) {
throw new \Exception('Check that your script located at ' . $path . ' is executable.');
}
// Decode the JSON.
$server = json_decode($output);
if (is_null($server)) {
$message = function_exists('json_last_error_msg') ? json_last_error_msg() : json_last_error();
throw new \Exception('JSON parse error : ' . $message);
}
} else {
throw new \Exception('Check that your script located at ' . $path . ' is readable.');
}
return $server;
}
/** * * * * * * * * * *
* ACTIONS CALLBACKS. *
* * * * * * * * * * **/
/**
* Action: login a user.
*
* @return array
* @throws \Exception
*/
function dms_client_action_uli() {
$admin = user_load(1);
if (!$admin) {
throw new \Exception('No admin user found.');
}
return array('message' => '<a href="' . url(user_pass_reset_url($admin) . '/login').'">Click to login as an administrator</a>');
}
/**
* Action: run the cron.
*
* @return array
* @throws \Exception
*/
function dms_client_action_cron() {
if (!drupal_cron_run()) {
throw new \Exception('Cron run failed.');
}
return array('message' => 'Cron was executed successfully');
}
/**
* Action: clear all caches.
*
* @return array
*/
function dms_client_action_cache_clear() {
drupal_flush_all_caches();
return array('message' => 'All caches were flushed successfully');
}
/**
* Action: dump database.
*
* @return array
* @throws \Exception
*/
function dms_client_action_dump_db() {
global $databases;
// Increase the maximum execution time.
if (!ini_get('safe_mode') && strpos(ini_get('disable_functions'), 'set_time_limit') === FALSE && ini_get('max_execution_time') < 1200) {
set_time_limit(1200);
}
// Set paths.
$dump_path = drupal_get_path('module', 'dms_client') . '/dumps/database.gz';
if (file_exists($dump_path)) unlink($dump_path);
// Use the default database.
$default_db = $databases['default']['default'];
// Dump the database.
exec('mysqldump -p' . $default_db['password'] . ' --user=' . $default_db['username'] . ' --host=' . $default_db['host'] . ' --compact ' . $default_db['database'] . ' | gzip > ' . $dump_path);
// Prepare and send back the download link.
$url = dms_client_download_get_url($dump_path);
if ($url === FALSE) {
throw new \Exception('URL to download the dump can not be generated.');
}
return array('message' => '<a href="' . $url . '">Click to download database dump</a>');
}