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

Contents of /contributions/modules/email/email.module

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


Revision 1.10 - (show annotations) (download) (as text)
Fri Jan 5 10:37:19 2007 UTC (2 years, 10 months ago) by mh86
Branch: MAIN
CVS Tags: HEAD
Changes since 1.9: +9 -4 lines
File MIME type: text/x-php
changes to email_field_formatter
1 <?php
2
3 // $Id: email.module,v 1.9 2007/01/05 10:17:35 mh86 Exp $
4
5 /**
6 * Implementation of hook_help().
7 */
8 function email_help($section) {
9 switch ($section) {
10 case 'admin/modules#description':
11 return t('Defines a field type for email addresses. <em>Note: Requires content.module.</em>');
12 }
13 }
14
15
16 /**
17 * Implementation of hook_field_info().
18 */
19 function email_field_info() {
20 return array(
21 'email' => array('label' => t('E-Mail')),
22 );
23 }
24
25
26 /**
27 * Implementation of hook_field_settings().
28 */
29 function email_field_settings($op, $field) {
30 switch ($op) {
31 case 'database columns':
32 $columns = array(
33 'email' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
34 );
35 return $columns;
36 }
37 }
38
39
40 /**
41 * Implementation of hook_field().
42 */
43 function email_field($op, &$node, $field, &$node_field, $teaser, $page) {
44 switch ($op) {
45 case 'view':
46 foreach ($node_field as $delta => $item) {
47 $node_field[$delta]['view'] = content_format($field, $item, 'email', $node);
48 }
49 return theme('field', $node, $field, $node_field, $teaser, $page);
50 }
51 }
52
53 /**
54 * Implementation of hook_field_formatter_info().
55 *
56 */
57 function email_field_formatter_info() {
58 return array(
59 'email-link' => array(
60 'label' => 'Email-Link',
61 'field types' => array('email'),
62 ),
63 'email-invisi' => array(
64 'label' => 'Email-Invisimail',
65 'field types' => array('email'),
66 ),
67 );
68 }
69
70 function email_field_formatter($field, $item, $formatter, $node) {
71 if (empty($item['email'])) {
72 return '';
73 }
74 else {
75 if ($field['widget']['link_type'] == 'form') {
76 $mailto = l(t('Email Contact Form'), 'email/'.$node->nid.'/'.$field['field_name']);
77 }
78 elseif (($field['widget']['link_type'] == 'mailto_encrypt' && module_exists('invisimail')) ||
79 (module_exists('invisimail') && isset($formatter) && $formatter = "email-invisi")) {
80 $format = $GLOBALS['invisimail_format'];
81 if (!(variable_get('invisimail_link_'.$format, TRUE))) {
82 variable_set('invisimail_link_'.$format, TRUE);
83 variable_set('invisimail_js_'.$format, TRUE);
84 }
85 $mailto = invisimail_ascii_encode($item['email']);
86 }
87 else {
88 $mailto = '<a href="mailto:'. $item['email']. '">'. check_plain($item['email']) .'</a>';
89 }
90 return $mailto;
91 }
92 }
93
94
95 /**
96 * Implementation of hook_widget_info().
97 */
98 function email_widget_info() {
99 return array(
100 'email' => array(
101 'label' => t('Textfield'),
102 'field types' => array('email'),
103 ),
104 );
105 }
106
107
108 /**
109 * Implementation of hook_widget_settings().
110 */
111 function email_widget_settings($op, $widget) {
112 switch ($op) {
113 case 'form':
114 $form = array();
115 $form['size'] = array(
116 '#type' => 'textfield',
117 '#title' => t('Size'),
118 '#default_value' => isset($widget['size']) ? $widget['size'] : 60,
119 '#required' => FALSE,
120 '#description' => t('Size of textfield'),
121 );
122
123 $options = array(
124 'mailto' => t('Mailto: Direct link'),
125 'form' => t('Contact form'),
126 );
127 if (module_exists('invisimail')) {
128 $options += array('mailto_encrypt' => t('Mailto: Direct link with invisimail encryption'));
129 }
130 $form['link_type'] = array(
131 '#type' => 'radios',
132 '#title' => t('Email Link Type'),
133 '#default_value' => isset($widget['link_type']) ? $widget['link_type'] : 'mailto',
134 '#options' => $options,
135 );
136
137 return $form;
138
139 case 'validate':
140 if (!empty($widget['size']) && (!is_numeric($widget['size']) || intval($widget['size']) != $widget['size'] || $widget['size'] <= 0)) {
141 form_set_error('size', t('"Size" must be a positive integer.'));
142 }
143 break;
144
145 case 'save':
146 return array('size', 'link_type');
147 }
148 }
149
150
151 /**
152 * Implementation of hook_widget().
153 */
154 function email_widget($op, &$node, $field, &$node_field) {
155 switch ($op) {
156 case 'form':
157 $form = array();
158 $form[$field['field_name']] = array(
159 '#tree' => TRUE,
160 '#weight' => $field['widget']['weight'],
161 );
162
163 if ($field['multiple']) {
164 $form[$field['field_name']]['#type'] = 'fieldset';
165 $form[$field['field_name']]['#title'] = t($field['widget']['label']);
166 foreach (range(0,2) as $delta) {
167 $form[$field['field_name']][$delta]['email'] = array(
168 '#type' => 'textfield',
169 '#title' => '',
170 '#default_value' => isset($node_field[$delta]['email']) ? $node_field[$delta]['email'] : '',
171 '#required' => $field['required'] ? $field['required'] : FALSE,
172 '#maxlength' => 255,
173 '#size' => isset($field['widget']['size']) ? $field['widget']['size'] : 60,
174 '#description' => isset($field['widget']['description']) ? $field['widget']['description'] : '',
175 );
176 }
177 }
178 else {
179 $form[$field['field_name']][0]['email'] = array(
180 '#type' => 'textfield',
181 '#title' => $field['widget']['label'],
182 '#default_value' => isset($node_field[0]['email']) ? $node_field[0]['email'] : '',
183 '#required' => $field['required'] ? $field['required'] : FALSE,
184 '#maxlength' => 255,
185 '#size' => isset($field['widget']['size']) ? $field['widget']['size'] : 60,
186 '#description' => isset($field['widget']['description']) ? $field['widget']['description'] : '',
187 );
188 }
189
190 return $form;
191
192 case 'validate':
193 if (is_array($node_field)) {
194 foreach ($node_field as $delta => $item) {
195 if ($item['email'] != '' && !valid_email_address($item['email'])) {
196 form_set_error($field['field_name'],t('"%mail" is not a valid email address',array('%mail' => $item['email'])));
197 }
198 }
199 }
200 break;
201 }
202 }
203
204 /**
205 * Implementation of hook_menu().
206 */
207 function email_menu($may_cache) {
208 $items = array();
209
210 if ($may_cache) {
211 $items[] = array('path' => 'email',
212 'title' => t('Email Contact Form'),
213 'callback' => 'email_mail_page',
214 'access' => user_access('access content'),
215 'type' => MENU_CALLBACK,
216 );
217 }
218 return $items;
219 }
220
221 /**
222 * The contact form page.
223 */
224 function email_mail_page($nid=null, $fieldname=null) {
225 if (empty($nid) || empty($fieldname)) {
226 drupal_not_found();
227 return;
228 }
229 $node = node_load(intval($nid));
230 if (!$node) {
231 drupal_not_found();
232 return;
233 }
234 // Validate field name
235 $types = content_types($node->type);
236 if (!isset($types['fields'][$fieldname]) ||
237 $types['fields'][$fieldname]['type'] != 'email' ||
238 $types['fields'][$fieldname]['widget']['link_type'] != 'form') {
239 drupal_not_found();
240 return;
241 }
242 $field = $node->$fieldname;
243 if (empty($field) || empty($field[0]['email'])) {
244 drupal_not_found();
245 return;
246 }
247
248 if (!flood_is_allowed('email', variable_get('email_hourly_threshold', 3))) {
249 $output = t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('email_hourly_threshold', 3)));
250 }
251 else {
252 $output = drupal_get_form('email_mail_page_form');
253 }
254
255 return $output;
256 }
257
258 function email_mail_page_form() {
259 global $user;
260
261 if ($user->uid) {
262 $edit['name'] = $user->name;
263 $edit['mail'] = $user->mail;
264 }
265
266 $form['#token'] = $user->name . $user->mail;
267 $form['name'] = array('#type' => 'textfield',
268 '#title' => t('Your name'),
269 '#maxlength' => 255,
270 '#default_value' => $edit['name'],
271 '#required' => TRUE,
272 );
273 $form['mail'] = array('#type' => 'textfield',
274 '#title' => t('Your e-mail address'),
275 '#maxlength' => 255,
276 '#default_value' => $edit['mail'],
277 '#required' => TRUE,
278 );
279 $form['subject'] = array('#type' => 'textfield',
280 '#title' => t('Subject'),
281 '#maxlength' => 255,
282 '#required' => TRUE,
283 );
284 $form['message'] = array('#type' => 'textarea',
285 '#title' => t('Message'),
286 '#required' => TRUE,
287 );
288 $form['submit'] = array('#type' => 'submit',
289 '#value' => t('Send e-mail'),
290 );
291 return $form;
292 }
293
294 /**
295 * Validate the site-wide contact page form submission.
296 */
297 function email_mail_page_form_validate($form_id, $form_values) {
298 if (!valid_email_address($form_values['mail'])) {
299 form_set_error('mail', t('You must enter a valid e-mail address.'));
300 }
301 if (preg_match("/\r|\n/", $form_values['subject'])) {
302 form_set_error('subject', t('The subject cannot contain linebreaks.'));
303 watchdog('mail', 'Email injection exploit attempted in email form subject: '.check_plain($form_values['subject']), WATCHDOG_NOTICE);
304 }
305 }
306
307 /**
308 * Process the site-wide contact page form submission.
309 */
310 function email_mail_page_form_submit($form_id, $edit) {
311 $nid = intval(arg(1));
312 $fieldname = arg(2);
313 if (empty($nid) || empty($fieldname)) {
314 drupal_not_found();
315 return;
316 }
317 $node = node_load($nid);
318 if (!$node) {
319 drupal_not_found();
320 return;
321 }
322 // Validate field name
323 $types = content_types($node->type);
324 if (!isset($types['fields'][$fieldname]) ||
325 $types['fields'][$fieldname]['type'] != 'email' ||
326 $types['fields'][$fieldname]['widget']['link_type'] != 'form') {
327 drupal_not_found();
328 return;
329 }
330 $field = $node->$fieldname;
331 if (empty($field) || empty($field[0]['email'])) {
332 drupal_not_found();
333 return;
334 }
335 $email = $field[0]['email'];
336
337 // E-mail address of the sender: as the form field is a text field,
338 // all instances of \r and \n have been automatically stripped from it.
339 $from = $edit['mail'];
340
341 // Compose the body:
342 $message[] = t("%name sent a message using the contact form at %form.", array('%name' => $edit['name'], '%form' => url($_GET['q'], NULL, NULL, TRUE)));
343 $message[] = $edit['message'];
344
345 // Tidy up the body:
346 foreach ($message as $key => $value) {
347 $message[$key] = wordwrap($value);
348 }
349
350 // Format the category:
351 $subject = t('[%title - %contact] %subject', array('%title' => preg_replace("/\r|\n/",'',$node->title), '%contact' => $types['fields'][$fieldname]['widget']['label'], '%subject' => $edit['subject']));
352
353 // Prepare the body:
354 $body = implode("\n\n", $message);
355
356 // Send the e-mail to the recipients:
357 drupal_mail($fieldname, $email, $subject, $body, $from);
358
359 // Log the operation:
360 flood_register_event('email');
361 watchdog('mail', t('%name-from sent an e-mail at %form.', array('%name-from' => theme('placeholder', $edit['name'] ." <$from>"), '%form' => url($_GET['q'], NULL, NULL, TRUE))));
362
363 // Update user:
364 drupal_set_message(t('Your message has been sent.'));
365
366 // Jump to home page rather than back to contact page to avoid contradictory messages if flood control has been activated.
367 return 'node/'.$node->nid;
368 }
369 ?>

  ViewVC Help
Powered by ViewVC 1.1.2