| Commit | Line | Data |
|---|---|---|
| 7be5aba1 | 1 | <?php |
| 3483245d JV |
2 | // $Id$ |
| 3 | ||
| 4 | /** | |
| 5 | * @file | |
| 6 | * Displays and processes the mail send form. | |
| 7 | * | |
| 8 | * This file is included by the print_mail module and includes the | |
| 9 | * mail form display, validation and submit hooks. | |
| 10 | */ | |
| 7be5aba1 JV |
11 | |
| 12 | require_once(drupal_get_path('module', 'print') .'/print.pages.inc'); | |
| 13 | ||
| 0706a465 JV |
14 | /** |
| 15 | * Menu callback for the send by e-mail form. | |
| 16 | * | |
| 17 | * @ingroup forms | |
| 18 | */ | |
| 19 | function print_mail_form($form_state) { | |
| 7be5aba1 | 20 | global $user; |
| 0d9893f9 JV |
21 | |
| 22 | $print_mail_hourly_threshold = variable_get('print_mail_hourly_threshold', PRINT_MAIL_HOURLY_THRESHOLD); | |
| 23 | ||
| 24 | if (!flood_is_allowed('print_mail', $print_mail_hourly_threshold)) { | |
| 25 | $form['flood'] = array( | |
| 26 | '#type' => 'markup', | |
| 03df5561 | 27 | '#markup' => '<p>'. t('You cannot send more than %number messages per hour. Please try again later.', array('%number' => $print_mail_hourly_threshold)) .'</p>', |
| 0d9893f9 JV |
28 | ); |
| 29 | return $form; | |
| 30 | } | |
| 31 | ||
| 7d62e7c4 JV |
32 | $print_mail_teaser_default = variable_get('print_mail_teaser_default', PRINT_MAIL_TEASER_DEFAULT_DEFAULT); |
| 33 | $print_mail_teaser_choice = variable_get('print_mail_teaser_choice', PRINT_MAIL_TEASER_CHOICE_DEFAULT); | |
| 7be5aba1 JV |
34 | $form = array(); |
| 35 | ||
| 36 | // Remove the printmail/ prefix | |
| bb0645f8 JV |
37 | $path = explode('/', $_GET['q']); |
| 38 | unset($path[0]); | |
| 39 | $path = implode('/', $path); | |
| 7be5aba1 | 40 | if (is_numeric($path)) { |
| 7d62e7c4 | 41 | $path = 'node/'. $path; |
| 7be5aba1 | 42 | } |
| 20ab24e6 | 43 | $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL; |
| 7be5aba1 | 44 | $form['path'] = array('#type' => 'value', '#value' => $path); |
| 20ab24e6 | 45 | $form['cid'] = array('#type' => 'value', '#value' => $cid); |
| 7be5aba1 JV |
46 | |
| 47 | $form['fld_from_addr'] = array( | |
| 48 | '#type' => 'textfield', | |
| 27dc0ea1 JV |
49 | '#title' => t('Your e-mail'), |
| 50 | '#size' => 62, | |
| 7be5aba1 JV |
51 | ); |
| 52 | $form['fld_from_name'] = array( | |
| 53 | '#type' => 'textfield', | |
| 54 | '#title' => t('Your name'), | |
| 27dc0ea1 | 55 | '#size' => 62, |
| 7be5aba1 | 56 | ); |
| 27dc0ea1 | 57 | $form['txt_to_addrs'] = array( |
| 7be5aba1 | 58 | '#type' => 'textarea', |
| 27dc0ea1 | 59 | '#title' => t('Send to'), |
| 7be5aba1 | 60 | '#rows' => 3, |
| 27dc0ea1 | 61 | '#resizable' => FALSE, |
| 7be5aba1 JV |
62 | '#description' => t('Enter multiple addresses separated by commas and/or different lines.'), |
| 63 | ); | |
| 64 | $form['fld_subject'] = array( | |
| 65 | '#type' => 'textfield', | |
| 66 | '#title' => t('Subject'), | |
| 27dc0ea1 JV |
67 | '#size' => 62, |
| 68 | ); | |
| 69 | $form['txt_message'] = array( | |
| 70 | '#type' => 'textarea', | |
| 71 | '#title' => t('Your message'), | |
| 72 | '#rows' => 6, | |
| 7be5aba1 | 73 | ); |
| 0706a465 | 74 | |
| 7d62e7c4 | 75 | if ($print_mail_teaser_choice) { |
| 0706a465 JV |
76 | $form['chk_teaser'] = array( |
| 77 | '#type' => 'checkbox', | |
| 78 | '#title' => t('Send only the teaser'), | |
| 7d62e7c4 | 79 | '#default_value' => $print_mail_teaser_default, |
| 0706a465 JV |
80 | ); |
| 81 | } | |
| 82 | else { | |
| 7d62e7c4 | 83 | $form['chk_teaser'] = array('#type' => 'value', '#value' => $print_mail_teaser_default); |
| 0706a465 JV |
84 | } |
| 85 | ||
| 7be5aba1 JV |
86 | $form['btn_submit'] = array( |
| 87 | '#type' => 'submit', | |
| 474d264b | 88 | '#value' => t('Send e-mail'), |
| 7be5aba1 JV |
89 | ); |
| 90 | $form['btn_clear'] = array( | |
| 91 | '#type' => 'submit', | |
| 474d264b | 92 | '#value' => t('Clear form'), |
| 7be5aba1 JV |
93 | '#validate' => array('print_mail_form_reset'), |
| 94 | ); | |
| 95 | $form['btn_cancel'] = array( | |
| 96 | '#type' => 'submit', | |
| 474d264b | 97 | '#value' => t('Cancel'), |
| 7be5aba1 JV |
98 | '#validate' => array(), |
| 99 | '#submit' => array('print_mail_form_cancel'), | |
| 100 | ); | |
| 101 | ||
| 102 | if ($user->uid != 0) { | |
| 0706a465 | 103 | $user_name = $user->name; |
| 7be5aba1 JV |
104 | $form['fld_from_addr']['#default_value'] = $user->mail; |
| 105 | $form['fld_from_addr']['#disabled'] = TRUE; | |
| 106 | $form['fld_from_addr']['#value'] = $user->mail; | |
| 107 | $form['fld_from_name']['#default_value'] = $user->name; | |
| 108 | } | |
| 0706a465 JV |
109 | else { |
| 110 | $user_name = t('Someone'); | |
| 111 | } | |
| 112 | $site_name = variable_get('site_name', t('an interesting site')); | |
| 3483245d JV |
113 | $form['fld_subject']['#default_value'] = t('!user has sent you a message from !site', |
| 114 | array('!user' => $user_name, '!site' => $site_name)); | |
| 7be5aba1 JV |
115 | |
| 116 | return $form; | |
| 117 | } | |
| 118 | ||
| 6a4c89f9 JV |
119 | /** |
| 120 | * Theme function for the send by-email form submission. | |
| 121 | * | |
| 122 | * Adds a class to the form labels. This class is used to place the label on | |
| 123 | * the left of the input fields. | |
| 124 | * | |
| 125 | * @ingroup forms | |
| 126 | */ | |
| 0706a465 | 127 | function theme_print_mail_form($form) { |
| 7625e488 | 128 | drupal_add_css(drupal_get_path('module', 'print') .'/css/printlinks.css'); |
| 0706a465 JV |
129 | $content = ''; |
| 130 | foreach (element_children($form) as $key) { | |
| 131 | $tmp = drupal_render($form[$key]); | |
| 27dc0ea1 JV |
132 | switch ($key) { |
| 133 | case 'fld_from_addr': | |
| 134 | case 'fld_from_name': | |
| 135 | case 'txt_to_addrs': | |
| 136 | case 'fld_subject': | |
| c92040a3 | 137 | $tmp = str_replace('<label', '<label class ="printmail-label"', $tmp); |
| 27dc0ea1 JV |
138 | break; |
| 139 | } | |
| 0706a465 JV |
140 | $content .= $tmp; |
| 141 | } | |
| 142 | return $content; | |
| 143 | } | |
| 144 | ||
| 7be5aba1 JV |
145 | /** |
| 146 | * Validate the send by-email form submission. | |
| 0706a465 JV |
147 | * |
| 148 | * @ingroup forms | |
| 7be5aba1 JV |
149 | */ |
| 150 | function print_mail_form_validate($form, &$form_state) { | |
| 151 | $from_addr = trim($form_state['values']['fld_from_addr']); | |
| 152 | $test = user_validate_mail($from_addr); | |
| 153 | if ($test) { | |
| 154 | form_set_error('fld_from_addr', $test); | |
| 155 | } | |
| 156 | ||
| 157 | // All new-lines are replaced by commas | |
| 27dc0ea1 | 158 | $to_addrs = str_replace(array("\r\n", "\n", "\r"), ',', $form_state['values']['txt_to_addrs']); |
| 7be5aba1 JV |
159 | // Create an array from the string |
| 160 | $to_array = explode(',', $to_addrs); | |
| 161 | // Verify each element of the array | |
| 162 | foreach ($to_array as $key => $address) { | |
| 163 | $address = trim($address); | |
| 164 | if (preg_match('/(.*?) <(.*)>/s', $address, $matches)) { | |
| 165 | // Address is of the type User Name <user@domain.tld> | |
| 166 | $test = user_validate_mail($matches[2]); | |
| 7d62e7c4 | 167 | $to_array[$key] = trim($matches[1]) .' <'. $matches[2] .'>'; |
| 7be5aba1 JV |
168 | } |
| 169 | else { | |
| 170 | // Address must be user@domain.tld | |
| 171 | $test = user_validate_mail($address); | |
| 172 | } | |
| 173 | if ($test) { | |
| 27dc0ea1 | 174 | form_set_error('txt_to_addrs', $test); |
| 7be5aba1 JV |
175 | } |
| 176 | } | |
| 177 | ||
| 178 | // In all fields, prevent insertion of custom headers | |
| 179 | foreach ($form_state['values'] as $key => $string) { | |
| 7d62e7c4 | 180 | if ( (substr($key, 0, 4) == 'fld_') && ((strpos($string, "\n") !== FALSE) || (strpos($string, "\r") !== FALSE)) ) { |
| 7be5aba1 JV |
181 | form_set_error($key, 'Found invalid character'); |
| 182 | } | |
| 183 | } | |
| 184 | ||
| 27dc0ea1 JV |
185 | $subject = trim($form_state['values']['fld_subject']); |
| 186 | if (empty($subject)) { | |
| 7d62e7c4 | 187 | form_set_error('fld_subject', t('You must enter a subject.')); |
| 27dc0ea1 JV |
188 | } |
| 189 | $message = trim($form_state['values']['txt_message']); | |
| 190 | if (empty($message)) { | |
| 7d62e7c4 | 191 | form_set_error('txt_message', t('You must enter a message.')); |
| 27dc0ea1 JV |
192 | } |
| 193 | ||
| 7be5aba1 JV |
194 | $form_state['values']['fld_from_addr'] = $from_addr; |
| 195 | $form_state['values']['fld_from_name'] = trim($form_state['values']['fld_from_name']); | |
| 196 | // Re-create the string from the re-organized array | |
| 27dc0ea1 JV |
197 | $form_state['values']['txt_to_addrs'] = implode(', ', $to_array); |
| 198 | $form_state['values']['fld_subject'] = $subject; | |
| 199 | $form_state['values']['txt_message'] = $message; | |
| 7be5aba1 JV |
200 | } |
| 201 | ||
| 202 | /** | |
| 203 | * Process the send by-email form submission. | |
| 0706a465 JV |
204 | * |
| 205 | * @ingroup forms | |
| 7be5aba1 JV |
206 | */ |
| 207 | function print_mail_form_submit($form, &$form_state) { | |
| 0d9893f9 | 208 | if (!empty($form_state['values']['fld_from_name'])) { |
| fce80f55 | 209 | $from = '"'. $form_state['values']['fld_from_name'] .'" <'. $form_state['values']['fld_from_addr'] .'>'; |
| 0d9893f9 JV |
210 | } |
| 211 | else { | |
| 212 | $from = $form_state['values']['fld_from_addr']; | |
| 213 | } | |
| 27dc0ea1 | 214 | $to = $form_state['values']['txt_to_addrs']; |
| 7be5aba1 JV |
215 | $params = array(); |
| 216 | $params['subject'] = $form_state['values']['fld_subject']; | |
| 27dc0ea1 | 217 | $params['message'] = $form_state['values']['txt_message']; |
| 0706a465 | 218 | $params['path'] = $form_state['values']['path']; |
| 1a92e513 | 219 | $params['cid'] = isset($form_state['values']['cid']) ? $form_state['values']['cid'] : ''; |
| 0706a465 | 220 | $params['teaser'] = $form_state['values']['chk_teaser']; |
| 7be5aba1 | 221 | |
| 0d9893f9 | 222 | $ret = drupal_mail('print_mail', 'sendpage', $to, language_default(), $params, $from, TRUE); |
| 27dc0ea1 | 223 | if ($ret['result']) { |
| 0d9893f9 JV |
224 | flood_register_event('print_mail'); |
| 225 | watchdog('print_mail', '%name [%from] sent %page to [%to]', array('%name' => $form_state['values']['fld_from_name'], '%from' => $form_state['values']['fld_from_addr'], '%page' => $form_state['values']['path'], '%to' => $to)); | |
| 226 | $site_name = variable_get('site_name', t('us')); | |
| 27dc0ea1 JV |
227 | drupal_set_message(t('Thank you for spreading the word about !site.', array('!site' => $site_name))); |
| 228 | } | |
| 7be5aba1 JV |
229 | $form_state['redirect'] = $form_state['values']['path']; |
| 230 | } | |
| 231 | ||
| 232 | /** | |
| 233 | * Implementation of hook_mail(). | |
| 234 | */ | |
| 235 | function print_mail_mail($key, &$message, $params) { | |
| 236 | switch ($key) { | |
| 237 | case 'sendpage': | |
| 27dc0ea1 JV |
238 | $sender_message = t('Message from sender:') .'<br /><br /><em>'. nl2br($params['message']) .'</em>'; |
| 239 | ||
| 240 | $print = print_controller($params['path'], $params['cid'], $params['teaser'], $sender_message); | |
| 5c9469df JV |
241 | if ($print === FALSE) { |
| 242 | return; | |
| 243 | } | |
| 7be5aba1 JV |
244 | |
| 245 | // Img elements must be set to absolute | |
| 7d62e7c4 JV |
246 | // $pattern = '!<(img\s[^>]*?)>!is'; |
| 247 | // $print['content'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['content']); | |
| 248 | // $print['logo'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['logo']); | |
| 249 | // $print['footer_message'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['footer_message']); | |
| 7be5aba1 | 250 | |
| 7d62e7c4 | 251 | $node = $print['node']; |
| 7be5aba1 | 252 | ob_start(); |
| 7d62e7c4 | 253 | include_once(_print_get_template('mail', $print['type'])); |
| 7be5aba1 JV |
254 | $html = ob_get_contents(); |
| 255 | ob_end_clean(); | |
| 256 | ||
| 257 | $message['subject'] = $params['subject']; | |
| 258 | $message['body'] = $html; | |
| 259 | $message['headers']['Content-Type'] = 'text/html; charset=utf-8'; | |
| 260 | break; | |
| 261 | } | |
| 262 | } | |
| 263 | ||
| 0706a465 JV |
264 | /** |
| 265 | * Process the send by-email form reset submission. | |
| 266 | * | |
| 267 | * @ingroup forms | |
| 268 | */ | |
| 7be5aba1 JV |
269 | function print_mail_form_reset($form, &$form_state) { |
| 270 | $form_state['rebuild'] = TRUE; | |
| 271 | drupal_set_message(t('The form has been reset to the default values.')); | |
| 272 | } | |
| 273 | ||
| 0706a465 JV |
274 | /** |
| 275 | * Process the send by-email form cancel submission. | |
| 276 | * | |
| 277 | * @ingroup forms | |
| 278 | */ | |
| 7be5aba1 JV |
279 | function print_mail_form_cancel($form, &$form_state) { |
| 280 | $form_state['redirect'] = $form_state['values']['path']; | |
| 281 | } | |
| 282 |