/[drupal]/contributions/modules/contact_attach/contact_attach.module
ViewVC logotype

Contents of /contributions/modules/contact_attach/contact_attach.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.6 - (show annotations) (download) (as text)
Mon Aug 25 20:11:42 2008 UTC (15 months ago) by oadaeh
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +2 -2 lines
File MIME type: text/x-php
#282877 by duckjerk: Use the new permission name.
1 <?php
2 // $Id: contact_attach.module,v 1.5 2008/08/24 23:53:38 oadaeh Exp $
3
4 /**
5 * @file
6 * Allows attaching files to e-mails sent using the site-wide contact form.
7 *
8 * This module gives users the ability of attaching one or more files to
9 * e-mails sent using the site-wide contact form.
10 */
11
12
13 /**
14 * Implementation of hook_help().
15 */
16 function contact_attach_help($path, $arg) {
17 $output = '';
18
19 switch ($path) {
20 case 'admin/settings/contact_attach':
21 $output = t('This module gives users the ability of attaching one or more files to e-mails sent using the site-wide contact form.');
22 break;
23 }
24
25 return $output;
26 } // End of contact_attach_help().
27
28
29
30 /**
31 * Implementation of hook_perm().
32 */
33 function contact_attach_perm() {
34 if (module_exists('og_contact')) {
35 $allowed_permissions = array('send attachments with site-wide contact form', 'send attachments with user contact form', 'send attachments with og contact form');
36 }
37 else {
38 $allowed_permissions = array('send attachments with site-wide contact form', 'send attachments with user contact form');
39 }
40
41 return $allowed_permissions;
42 } // End of contact_attach_perm().
43
44
45
46 /**
47 * Implementation of hook_menu().
48 */
49 function contact_attach_menu() {
50 $items = array();
51
52 $items['admin/settings/contact_attach'] = array(
53 'title' => 'Contact attach',
54 'page callback' => 'drupal_get_form',
55 'page arguments' => array('contact_attach_admin_settings'),
56 'access arguments' => array('administer site configuration'),
57 'description' => 'Configure the Contact attach module.',
58 );
59
60 return $items;
61 } // End of contact_attach_menu().
62
63
64
65 /**
66 * Administration settings form.
67 *
68 * @return
69 * The completed form definition.
70 */
71 function contact_attach_admin_settings() {
72 $form = array();
73
74 $form['contact_attach_number'] = array(
75 '#type' => 'textfield',
76 '#title' => t('Number of attachments'),
77 '#default_value' => variable_get('contact_attach_number', '3'),
78 '#size' => 10,
79 '#description' => t('The number of attachments to allow on the contact form.'),
80 );
81
82 return system_settings_form($form);
83 } // End of contact_attach_admin_settings().
84
85
86
87 /**
88 * Implementation of hook_form_alter().
89 */
90 function contact_attach_form_alter(&$form, &$form_state, $form_id) {
91 if (($form_id == 'contact_mail_user' && user_access('send attachments with user contact form')) || ($form_id == 'contact_mail_page' && user_access('send attachments with site-wide contact form')) || ($form_id == 'og_contact_mail_page' && user_access('send attachments with og contact form'))) {
92 for ($i = 1; $i <= variable_get('contact_attach_number', '3'); $i++) {
93 $form['contact_attach_'. $i] = array(
94 '#type' => 'file',
95 '#title' => t('Attachment #!number', array('!number' => $i)),
96 '#weight' => $i,
97 );
98 }
99
100 // We do not allow anonymous users to send themselves a copy because it
101 // can be abused to spam people.
102 global $user;
103 if ($user->uid) {
104 $form['copy'] = array(
105 '#type' => 'checkbox',
106 '#title' => t('Send yourself a copy.'),
107 '#weight' => $i,
108 );
109 }
110
111 $form['submit'] = array(
112 '#type' => 'submit',
113 '#value' => t('Send e-mail'),
114 '#weight' => $i + 1,
115 );
116
117 $form['#attributes']['enctype'] = 'multipart/form-data';
118
119 // Empty the array to make sure our function is the only one called.
120 if (!empty($form['#submit'])) {
121 unset($form['#submit']);
122 }
123
124 // Set the validate and submit functions to our functions.
125 switch ($form_id) {
126 case 'contact_mail_user':
127 $form['#validate'][] = 'contact_attach_contact_mail_user_validate';
128 $form['#submit'][] = 'contact_attach_contact_mail_user_submit';
129 break;
130
131 case 'contact_mail_page':
132 $form['#validate'][] = 'contact_attach_contact_mail_page_validate';
133 $form['#submit'][] = 'contact_attach_contact_mail_page_submit';
134 break;
135
136 case 'og_contact_mail_page':
137 $form['#validate'][] = 'contact_attach_og_contact_mail_page_validate';
138 $form['#submit'][] = 'contact_attach_og_contact_mail_page_submit';
139 break;
140 }
141 }
142 } // End of contact_attach_form_alter().
143
144
145
146 /**
147 * Add custom validation to verify the attachments.
148 *
149 * @param form
150 * An associative array containing the structure of the form.
151 * @param form_state
152 * A keyed array containing the current state of the form.
153 */
154 function contact_attach_contact_mail_page_validate($form, &$form_state) {
155 _contact_attach_upload_validate($form, &$form_state);
156 } // End of contact_attach_contact_mail_page_validate().
157
158
159
160 /**
161 * Override contact_mail_page_submit().
162 *
163 * @param form
164 * An associative array containing the structure of the form.
165 * @param form_state
166 * A keyed array containing the current state of the form.
167 */
168 function contact_attach_contact_mail_page_submit($form, &$form_state) {
169 global $language;
170
171 $values = $form_state['values'];
172
173 // E-mail address of the sender: as the form field is a text field,
174 // all instances of \r and \n have been automatically stripped from it.
175 $from = $values['mail'];
176
177 // Load category properties and save form values for email composition.
178 $contact = contact_load($values['cid']);
179 $values['contact'] = $contact;
180
181 // Send the e-mail to the recipients using the site default language.
182 $results[0] = drupal_mail('contact_attach', 'contact_page_mail', $contact['recipients'], language_default(), $values, $from);
183 // If the user requests it, send a copy using the current language.
184 if ($values['copy']) {
185 $results[1] = drupal_mail('contact_attach', 'contact_page_copy', $from, $language, $values, $from);
186 }
187
188 // Send an auto-reply if necessary using the current language.
189 if ($contact['reply']) {
190 $results[2] = drupal_mail('contact_attach', 'contact_page_autoreply', $from, $language, $values, $contact['recipients']);
191 }
192
193 for ($i = 0; $i < 3; $i++) {
194 if (!empty($results[$i])) {
195 if (!$results[$i]['result']) {
196 watchdog('mail', '%name-from attempted to send an e-mail regarding %category, but was unsuccessful.', array('%name-from' => $values['name'] ." [$from]", '%category' => $contact['category']));
197 drupal_set_message(t('Your message was NOT sent.'));
198 }
199 else {
200 flood_register_event('contact');
201 watchdog('mail', '%name-from sent an e-mail regarding %category.', array('%name-from' => $values['name'] ." [$from]", '%category' => $contact['category']));
202 drupal_set_message(t('Your message has been sent.'));
203 }
204 }
205 }
206
207 // Jump to home page rather than back to contact page to avoid
208 // contradictory messages if flood control has been activated.
209 $form_state['redirect'] = '';
210 } // End of contact_attach_contact_mail_page_submit().
211
212
213
214 /**
215 * Add custom validation to verify the attachments.
216 *
217 * @param form
218 * An associative array containing the structure of the form.
219 * @param form_state
220 * A keyed array containing the current state of the form.
221 */
222 function contact_attach_contact_mail_user_validate($form, &$form_state) {
223 _contact_attach_upload_validate($form, &$form_state);
224 } // End of contact_attach_contact_mail_user_validate().
225
226
227
228 /**
229 * Override contact_mail_user_submit().
230 *
231 * @param form
232 * An associative array containing the structure of the form.
233 * @param form_state
234 * A keyed array containing the current state of the form.
235 */
236 function contact_attach_contact_mail_user_submit($form, &$form_state) {
237 global $user, $language;
238
239 $account = $form_state['values']['recipient'];
240
241 // Send from the current user to the requested user.
242 $to = $account->mail;
243 $from = $user->mail;
244
245 // Save both users and all form values for email composition.
246 $values = $form_state['values'];
247 $values['account'] = $account;
248 $values['user'] = $user;
249
250 // Send the e-mail in the requested user language.
251 $results[0] = drupal_mail('contact_attach', 'contact_user_mail', $to, user_preferred_language($account), $values, $from);
252
253 // Send a copy if requested, using current page language.
254 if ($form_state['values']['copy']) {
255 $results[1] = drupal_mail('contact_attach', 'contact_user_copy', $from, $language, $values, $from);
256 }
257
258 for ($i = 0; $i < 2; $i++) {
259 if (!empty($results[$i])) {
260 if (!$results[$i]['result']) {
261 watchdog('mail', '%name-from attempted to send %name-to an e-mail, but was unsuccessful.', array('%name-from' => $user->name, '%name-to' => $account->name));
262 drupal_set_message(t('The message has NOT been sent.'));
263 }
264 else {
265 flood_register_event('contact');
266 watchdog('mail', '%name-from sent %name-to an e-mail.', array('%name-from' => $user->name, '%name-to' => $account->name));
267 drupal_set_message(t('The message has been sent.'));
268 }
269 }
270 }
271
272 // Back to the requested users profile page.
273 $form_state['redirect'] = "user/$account->uid";
274 } // End of contact_attach_contact_mail_user_submit().
275
276
277
278 /* *
279 * Add custom validation to verify the attachments.
280 *
281 * @param form
282 * An associative array containing the structure of the form.
283 * @param form_state
284 * A keyed array containing the current state of the form.
285 * /
286 function contact_attach_og_contact_mail_page_validate($form, &$form_state) {
287 /** TODO: As there is not yet a 6.x compatible version of OG Contact, this function has not been tested. * /
288 _contact_attach_upload_validate($form, &$form_state);
289 } // End of contact_attach_og_contact_mail_page_validate().
290 */
291
292
293
294 /* *
295 * Override og_contact_mail_page_submit().
296 *
297 * @param form
298 * An associative array containing the structure of the form.
299 * @param form_state
300 * A keyed array containing the current state of the form.
301 * /
302 function contact_attach_og_contact_mail_page_submit($form, &$form_state) {
303 /** TODO: As there is not yet a 6.x compatible version of OG Contact, this function has not been tested. * /
304 $name = og_contact_get_group_name($form_state['values']['gid']);
305 // E-mail address of the sender: as the form field is a text field,
306 // all instances of \r and \n have been automatically stripped from it.
307 $from = $form_state['values']['mail'];
308
309 // Load the group information:
310 $group = db_fetch_object(db_query("SELECT * FROM {og_contact} WHERE gid = %d", $form_state['values']['gid']));
311
312 // $contact = og_contact_get_recipients($form_state['values']['gid']);
313 $contact['recipients'] = og_contact_get_recipients($form_state['values']['gid']);
314 // $contact = contact_load($values['cid']);
315 $values['contact'] = $contact;
316
317 // Send the e-mail to the recipients using the site default language.
318 $results[0] = drupal_mail('contact_attach', 'og_contact_page_mail', $contact['recipients'], language_default(), $values, $from);
319
320 // If the user requests it, send a copy using the current language.
321 if ($form_state['values']['copy']) {
322 $results[1] = drupal_mail('contact_attach', 'og_contact_page_copy', $from, $language, $values, $from);
323 }
324
325 // Send an auto-reply if necessary using the current language.
326 if ($group->reply) {
327 $results[2] = drupal_mail('contact_attach', 'og_contact_page_autoreply', $from, $language, $values, $contact['recipients']);
328 }
329
330 for ($i = 0; $i < 3; $i++) {
331 if (!empty($results[$i])) {
332 if (!$results[$i]['result']) {
333 // Log the operation:
334 watchdog('mail', '%name-from attempted to send an e-mail regarding %category, but was unsuccessful.', array('%name-from' => $form_state['values']['name'] ." <$from>", '%category' => $name));
335 // Update user:
336 drupal_set_message(t('Your message was NOT sent.'));
337 }
338 else {
339 // Log the operation:
340 flood_register_event('og-contact');
341 watchdog('mail', '%name-from sent an e-mail regarding %category.', array('%name-from' => $form_state['values']['name'] ." <$from>", '%category' => $name));
342 // Update user:
343 drupal_set_message(t('Your message has been sent.'));
344 }
345 }
346 }
347
348 // Jump to group page rather than back to contact page to avoid
349 // contradictory messages if flood control has been activated.
350 $form_state['redirect'] = 'node/'. $group->gid;
351 } // End of contact_attach_og_contact_mail_page_submit().
352 */
353
354
355
356 /**
357 * Implementation of hook_mail().
358 */
359 function contact_attach_mail($key, &$message, $params) {
360 $language = $message['language'];
361 switch ($key) {
362 case 'contact_page_mail':
363 case 'contact_page_copy':
364 $contact = $params['contact'];
365 $message['subject'] .= t('[!category] !subject', array('!category' => $contact['category'], '!subject' => $params['subject']), $language->language);
366 $message['body'][] = t("!name sent a message using the contact form at !form.", array('!name' => $params['name'], '!form' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language))), $language->language);
367 $message['body'][] = $params['message'];
368
369 // Attachment processing begins here.
370
371 $return_message = _contact_attach_process_attachments($message);
372 if (!empty($return_message)) {
373 $message['headers'] = $return_message['headers'];
374 $message['body'] = $return_message['body'];
375 }
376
377 // Attachment processing ends here.
378
379 break;
380 case 'contact_page_autoreply':
381 $contact = $params['contact'];
382 $message['subject'] .= t('[!category] !subject', array('!category' => $contact['category'], '!subject' => $params['subject']), $language->language);
383 $message['body'][] = $contact['reply'];
384 break;
385 case 'contact_user_mail':
386 case 'contact_user_copy':
387 $user = $params['user'];
388 $account = $params['account'];
389 $message['subject'] .= '['. variable_get('site_name', 'Drupal') .'] '. $params['subject'];
390 $message['body'][] = "$account->name,";
391 $message['body'][] = t("!name (!name-url) has sent you a message via your contact form (!form-url) at !site.", array('!name' => $user->name, '!name-url' => url("user/$user->uid", array('absolute' => TRUE, 'language' => $language)), '!form-url' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language)), '!site' => variable_get('site_name', 'Drupal')), $language->language);
392 $message['body'][] = t("If you don't want to receive such e-mails, you can change your settings at !url.", array('!url' => url("user/$account->uid", array('absolute' => TRUE, 'language' => $language))), $language->language);
393 $message['body'][] = t('Message:', NULL, $language->language);
394 $message['body'][] = $params['message'];
395
396 // Attachment processing begins here.
397
398 $return_message = _contact_attach_process_attachments($message);
399 if (!empty($return_message)) {
400 $message['headers'] = $return_message['headers'];
401 $message['body'] = $return_message['body'];
402 }
403
404 // Attachment processing ends here.
405
406 break;
407 /*
408 /** TODO: As there is not yet a 6.x compatible version of OG Contact, the following two parts have not been tested. * /
409 case 'og_contact_page_mail':
410 case 'og_contact_page_copy':
411 $contact = $params['contact'];
412 $message['subject'] .= t('[!category] !subject', array('!category' => $contact['category'], '!subject' => $params['subject']), $language->language);
413 $message['body'][] = t("!name sent a message using the contact form at !form.", array('!name' => $params['name'], '!form' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language))), $language->language);
414 $message['body'][] = $params['message'];
415
416 // Attachment processing begins here.
417
418 $return_message = _contact_attach_process_attachments($message);
419 if (!empty($return_message)) {
420 $message['headers'] = $return_message['headers'];
421 $message['body'] = $return_message['body'];
422 }
423
424 // Attachment processing ends here.
425
426 break;
427 case 'og_contact_page_autoreply':
428 $contact = $params['contact'];
429 $message['subject'] .= t('[!category] !subject', array('!category' => $contact['category'], '!subject' => $params['subject']), $language->language);
430 $message['body'][] = $contact['reply'];
431 break;
432 */
433 }
434 }
435
436
437
438 /**
439 * Check for attachments and process them, if one or more exists.
440 *
441 * @param message
442 * The message, as it exists so far.
443 * @return
444 * The message, including processed attachment(s).
445 */
446 function _contact_attach_process_attachments($message) {
447 $return_message = array();
448
449 // Loop through each possible attachment.
450 for ($i = 1; $i <= variable_get('contact_attach_number', '3'); $i++) {
451 if ($file = file_save_upload('contact_attach_'. $i)) {
452 // Check to see if the attachment exists.
453 if ($file->filesize > 0) {
454 // An attachment exists, so save it to an array for later processing.
455 $files[] = $file;
456 }
457 }
458 }
459
460 // If the array cantains something, we have one or more attachments to
461 // process. If it does not contain anything, we send back an empty $body,
462 // indicating no attachments exist.
463 if (!empty($files)) {
464 // Set initial values.
465 $body = '';
466 $return_message = array();
467 $boundary_id = md5(uniqid(time()));
468
469 $message['headers']['Content-Type'] = 'multipart/mixed; boundary="'. $boundary_id .'"';
470
471 $body = "\n--$boundary_id\n";
472 $body .= "Content-Type: text/plain; charset=UTF-8; format=flowed;\n\n"; // sets the mime type
473
474 // Prepare the body:
475 $body .= implode("\n\n", $message['body']);
476 $body .= "\n\n\n";
477
478 // Add the attachments.
479 // Loop through each possible attachment.
480 for ($i = 0; $i < count($files); $i++) {
481 // Process the attachment.
482 $body .= "--$boundary_id\n";
483 $body .= _contact_attach_add_attachment($files[$i]);
484 $body .= "\n\n";
485 }
486
487 $body .= "--$boundary_id--\n\n";
488
489 $return_message['headers'] = $message['headers'];
490 $return_message['body'] = $body;
491 }
492
493 return $return_message;
494 } // End of _contact_attach_process_attachments().
495
496
497
498 /**
499 * Add an attachment to the message body.
500 *
501 * @param file
502 * An attachment to add to the message.
503 * @return
504 * The processed attachment, ready for appending to the message.
505 */
506 function _contact_attach_add_attachment($file) {
507 $attachment = "Content-Type: ". $file->filemime ."; name=\"". basename($file->filename) ."\"\n";
508 $attachment .= "Content-Transfer-Encoding: base64\n";
509 $attachment .= "Content-Disposition: attachment; filename=\"". basename($file->filename) ."\"\n\n";
510 if (file_exists($file->filepath)) {
511 $attachment .= chunk_split(base64_encode(file_get_contents($file->filepath)));
512 }
513 else {
514 $attachment .= chunk_split(base64_encode(file_get_contents(file_directory_temp() .'/'. $file->filename)));
515 }
516
517 return $attachment;
518 } // End of _contact_attach_add_attachment().
519
520
521
522 /**
523 * Validate the attachment(s).
524 */
525 function _contact_attach_upload_validate() {
526 global $user;
527
528 // Validate file against all users roles.
529 // Only denies an upload when all roles prevent it.
530 foreach ($user->roles as $rid => $name) {
531 // Validate the file type, based on file extension.
532 $extensions = variable_get("upload_extensions_$rid", variable_get('upload_extensions_default', 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp'));
533 // Validate the upload limit, based on file size.
534 $file_limit = variable_get("upload_uploadsize_$rid", variable_get('upload_uploadsize_default', 1)) * 1024 * 1024;
535 // Validate the upload limit, based on the user's uploads.
536 $user_limit = variable_get("upload_usersize_$rid", variable_get('upload_usersize_default', 1)) * 1024 * 1024;
537 $validators = array(
538 'file_validate_extensions' => array($extensions),
539 'file_validate_name_length' => array(),
540 'file_validate_size' => array($file_limit, $user_limit));
541
542 // Loop through each possible attachment.
543 for ($i = 1; $i <= variable_get('contact_attach_number', '3'); $i++) {
544 file_save_upload('contact_attach_'. $i, $validators);
545 }
546 }
547 } // End of _contact_attach_upload_validate().

  ViewVC Help
Powered by ViewVC 1.1.2