-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclamav.module
153 lines (137 loc) · 5.03 KB
/
clamav.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
<?php
// $Id$
/**
* @file
* Integrate ClamAV to allow uploaded files to be scanned for viruses.
*/
// Scan using a ClamAV Daemon
define('CLAMAV_USE_DAEMON', 0);
// Scan using a ClamAV executable
define('CLAMAV_USE_EXECUTABLE', 1);
// Default: use Daemon-mode
define('CLAMAV_DEFAULT_MODE', CLAMAV_USE_DAEMON);
// Behaviour if the ClamAV scanner is unavailable or does not respond.
// Prevent unchecked files from being uploaded
define('CLAMAV_BLOCK_UNCHECKED', 0);
// Allow unchecked files to be uploaded
define('CLAMAV_ALLOW_UNCHECKED', 1);
// Default behaviour for unchecked files - Block unchecked files.
define('CLAMAV_DEFAULT_UNCHECKED', CLAMAV_BLOCK_UNCHECKED);
// Default host (in Daemon-mode)
define('CLAMAV_DEFAULT_HOST', 'localhost');
// Default port (in Daemon-mode)
define('CLAMAV_DEFAULT_PORT', 3310);
// Default path (in Executable-mode)
define('CLAMAV_DEFAULT_PATH', '/usr/bin/clamscan');
// The file was not checked (e.g. because the AV daemon wasn't running).
define('CLAMAV_SCANRESULT_UNCHECKED', -1);
// The file was checked and found to be clean.
define('CLAMAV_SCANRESULT_CLEAN', 0);
// The file was checked and found to be infected.
define('CLAMAV_SCANRESULT_INFECTED', 1);
/**
* Implementation of hook_help().
*/
function clamav_help($path, $arg) {
$output = '';
switch ($path) {
case "admin/help#clamav":
$output .= '<p>' . t('Clam AntiVirus is an open source anti-virus toolkit for UNIX.') . '</p>';
$output .= '<p>' . t('The ClamAV module allows files which are uploaded to Drupal to be scanned by Clam AntiVirus.') . '<br />';
$output .= t('The module does not install ClamAV - visit <a href="http://www.clamav.net/">the ClamAV website</a> for help installing ClamAV.') . '</p>';
break;
case 'admin/settings/clamav':
break;
}
return $output;
}
/**
* Implementation of hook_menu().
*/
function clamav_menu() {
return array(
'admin/settings/clamav' => array(
'title' => 'Anti-virus (ClamAV)',
'page callback' => 'drupal_get_form',
'page arguments' => array('clamav_admin_settings'),
'access arguments' => array('administer site configuration'),
'file' => 'clamav.admin.inc',
),
);
}
/**
* Implementation of hook_elements().
*/
function clamav_elements() {
$elements = array();
// support the core 'file' FAPI element.
if (variable_get('clamav_enable_element_file', TRUE)) {
$elements['file']['#element_validate'] = array('clamav_elements_file_validate');
}
return $elements;
}
/**
* Form element validator for the file FAPI type.
*
* @param Array $element
*/
function clamav_elements_file_validate($element) {
$key = $element['#parents'][0];
if (array_key_exists($key, $_FILES['files']['tmp_name']) && !empty($_FILES['files']['tmp_name'][$key])) {
$filepath = $_FILES['files']['tmp_name'][$key]; // filepath to the uploaded file
$form_error_key = implode('][', $element['#parents']); // form-element to use with form_set_error
require_once(dirname(__FILE__) . '/clamav.inc');
clamav_scan($filepath, $form_error_key);
}
}
/**
* Implementation of hook_form_alter().
* Validation of CCK filefield and imagefield is applied here.
*/
function clamav_form_alter(&$form, $form_state, $form_id) {
if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
$type = content_types($form['#node']->type);
if (!empty($type['fields'])) {
foreach ($type['fields'] as $field_name => $field) {
$function = 'clamav_' . $field['widget']['type'] . '_alter';
if (function_exists($function)) {
$function($form[$field_name]);
}
}
}
}
}
/**
* Implementation of our custom hook_FIELDNAME_alter for filefield widget.
*/
function clamav_filefield_widget_alter(&$field) {
if (module_exists('filefield') && variable_get('clamav_enable_element_filefield_widget', TRUE)) {
$field[0]['#upload_validators']['clamav_elements_filefield_validate'] = array();
}
}
/**
* Implementation of our custom hook_FIELDNAME_alter for filefield widget.
*/
function clamav_imagefield_widget_alter(&$field) {
if (module_exists('imagefield') && variable_get('clamav_enable_element_imagefield_widget', TRUE)) {
$field[0]['#upload_validators']['clamav_elements_filefield_validate'] = array();
}
}
/**
* Validator for filefield widget and imagefield widget.
* This is an implementation of a file upload_validator.
*/
function clamav_elements_filefield_validate($file) {
$filepath = $file->filepath;
$form_error_key = $file->source;
require_once(dirname(__FILE__) . '/clamav.inc');
$result = clamav_scan_file($filepath);
$errors = array();
if ($result == CLAMAV_SCANRESULT_INFECTED) {
$errors[] = t('A virus has been detected in the file. The file will not be accepted.');
}
elseif ($result == CLAMAV_SCANRESULT_UNCHECKED && variable_get('clamav_unchecked_files', CLAMAV_DEFAULT_UNCHECKED) == CLAMAV_BLOCK_UNCHECKED) {
$errors[] = t('The anti-virus scanner was not able to check the file. The file cannot be uploaded.');
}
return $errors;
}