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