@@ -72,6 +72,13 @@ function os2web_cp_service_handler() {
72
72
* Callback for the file provider service.
73
73
*/
74
74
function os2web_gf_service_handler($file_id) {
75
+ // Mime Types which are disallowed to be downloaded.
76
+ // People shouldn't be able to download special files.
77
+ $disallowed_mimes = array(
78
+ // Disallow .msg files.
79
+ 'application/vnd.ms-outlook',
80
+ );
81
+
75
82
if ($url = variable_get('os2web_cp_service_cp_document_fileurl')) {
76
83
77
84
$username = variable_get('os2web_cp_service_endpoint_user');
@@ -86,38 +93,60 @@ function os2web_gf_service_handler($file_id) {
86
93
$header = curl_getinfo($ch);
87
94
curl_close($ch);
88
95
if ($header['http_code'] === 200) {
89
- drupal_add_http_header('Content-Type', $header['content_type']);
90
- drupal_add_http_header('Content-Length', $header['download_content_length']);
91
- drupal_add_http_header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
92
- drupal_add_http_header('Cache-Control', 'private', FALSE);
93
- drupal_add_http_header('Connection', 'close');
94
- drupal_add_http_header('Expires', '0');
95
-
96
- // Check for IE only headers.
97
- if (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)) {
98
- drupal_add_http_header('Pragma', 'public');
96
+ if (!in_array($header['content_type'], $disallowed_mimes)) {
97
+ drupal_add_http_header('Content-Type', $header['content_type']);
98
+ drupal_add_http_header('Content-Length', $header['download_content_length']);
99
+ drupal_add_http_header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
100
+ drupal_add_http_header('Cache-Control', 'private', FALSE);
101
+ drupal_add_http_header('Connection', 'close');
102
+ drupal_add_http_header('Expires', '0');
103
+
104
+ // Check for IE only headers.
105
+ if (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)) {
106
+ drupal_add_http_header('Pragma', 'public');
107
+ }
108
+ else {
109
+ drupal_add_http_header('Pragma', 'no-cache');
110
+ }
111
+
112
+ // Load the title to use it as the filename.
113
+ $query = new EntityFieldQuery();
114
+ $result = $query
115
+ ->entityCondition('entity_type', 'node')
116
+ ->propertyCondition('type', 'os2web_cp_service_cp_document')
117
+ ->propertyCondition('status', 1)
118
+ ->fieldCondition('field_os2web_cp_service_file_id', 'value', $file_id, '=')
119
+ ->execute();
120
+ $nids = (isset($result['node']))?array_keys($result['node']) : NULL;
121
+
122
+ $node = node_load(array_pop($nids));
123
+
124
+ if ($node) {
125
+ $filename = str_replace('/', '_', $node->field_os2web_cp_service_doc_id[LANGUAGE_NONE][0]['value'] . '.' . os2web_cp_service_get_extension_from_mime($header['content_type']));
126
+ drupal_add_http_header('Content-Disposition', 'attachment; filename=' . $filename);
127
+ }
128
+ echo $data;
129
+ drupal_exit();
99
130
}
100
131
else {
101
- drupal_add_http_header('Pragma', 'no-cache');
102
- }
103
132
104
- // Load the title to use it as the filename.
105
- $query = new EntityFieldQuery();
106
- $result = $query
107
- ->entityCondition('entity_type', 'node')
108
- ->propertyCondition('type', 'os2web_cp_service_cp_document')
109
- ->propertyCondition('status', 1)
110
- ->fieldCondition('field_os2web_cp_service_file_id', 'value', $file_id, '=')
111
- ->execute();
112
- $nids = (isset($result['node']))?array_keys($result['node']) : NULL;
113
-
114
- $node = node_load(array_pop($nids));
115
- if ($node) {
116
- $filename = str_replace('/', '_', $node->field_os2web_cp_service_doc_id[LANGUAGE_NONE][0]['value'] . '.' . os2web_cp_service_get_extension_from_mime($header['content_type']));
117
- drupal_add_http_header('Content-Disposition', 'attachment; filename=' . $filename);
133
+ // Show a polite message if the file cant be downloaded.
134
+ // If the message isnt set in config, deliver a access denied page.
135
+ if ($error_message = variable_get('os2web_cp_service_access_denied_message')) {
136
+ $markup = '<div class="messages error"><ul><li>';
137
+ $markup .= $error_message;
138
+ $markup .= '</li></ul></div>';
139
+
140
+ $page['region'] = array(
141
+ '#type' => 'markup',
142
+ '#markup' => $markup,
143
+ );
144
+ return $page;
145
+ }
146
+ else {
147
+ drupal_access_denied();
148
+ }
118
149
}
119
- echo $data;
120
- drupal_exit();
121
150
}
122
151
else {
123
152
error_log(basename(__FILE__) . ':' . __LINE__ . ' HTTP header recieved = ' . print_r($header, 1));
@@ -835,6 +864,12 @@ function os2web_cp_service_form_os2web_settings_settings_form_alter(&$form, &$fo
835
864
'#description' => 'Komma separeret liste af ip-addresser der kan tilgå <em>webservicen</em>.',
836
865
'#default_value' => variable_get('os2web_cp_service_cp_access_ip', ip_address()),
837
866
);
867
+ $form['os2web_cp_service_config_group']['os2web_cp_service_access_denied_message'] = array(
868
+ '#type' => 'textfield',
869
+ '#title' => 'Besked til brugeren, hvis fil ikke er tilgængelig.',
870
+ '#description' => 'Vises når den modtagede filtype ikke er godkendt til Download.',
871
+ '#default_value' => variable_get('os2web_cp_service_access_denied_message'),
872
+ );
838
873
}
839
874
840
875
/**
@@ -870,6 +905,8 @@ function os2web_cp_service_date_format_types() {
870
905
* The file ext without the dot.
871
906
*/
872
907
function os2web_cp_service_get_extension_from_mime($mime) {
908
+
909
+ // Todo: use file_mimetype_mapping().
873
910
$map = array(
874
911
'application/pdf' => 'pdf',
875
912
'application/zip' => 'zip',
0 commit comments