print_mail.inc

×

Error message

User warning: The following module is missing from the file system: theme/theme. For information about how to fix this, see the documentation page. in _drupal_trigger_error_with_delayed_logging() (line 1156 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).

Displays and processes the mail send form.

This file is included by the print_mail module and includes the mail form display, validation and submit hooks.

Functions

Namesort descending Description
@print_mail_form Menu callback for the send by email form.
print_mail_form_submit Process the send by-email form submission.
print_mail_form_validate Validate the send by-email form submission.
theme_print_mail_form Theme function for the send by-email form submission.

File

sites/all/modules/local_contrib/print/print_mail/print_mail.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Displays and processes the mail send form.
  5. *
  6. * This file is included by the print_mail module and includes the
  7. * mail form display, validation and submit hooks.
  8. *
  9. * @ingroup print
  10. */
  11. require_once(DRUPAL_ROOT . '/' . drupal_get_path('module', 'print') . '/print.pages.inc');
  12. // Include MIME library
  13. @include_once('Mail/mime.php');
  14. /**
  15. * Menu callback for the send by email form.
  16. *
  17. * @ingroup forms
  18. */
  19. function print_mail_form($form, &$form_state) {
  20. global $user;
  21. // Remove the printmail/ prefix
  22. $path_arr = explode('/', $_GET['q']);
  23. unset($path_arr[0]);
  24. $path = filter_xss(implode('/', $path_arr));
  25. if (empty($path)) {
  26. // If no path was provided, let's try to generate a page for the referer
  27. global $base_url;
  28. $ref = $_SERVER['HTTP_REFERER'];
  29. $path = preg_replace("!^$base_url/!", '', $ref);
  30. if (($path === $ref) || empty($path)) {
  31. $path = variable_get('site_frontpage', 'node');
  32. }
  33. drupal_goto(PRINTMAIL_PATH . '/' . $path);
  34. }
  35. elseif (ctype_digit($path_arr[1])) {
  36. if (drupal_lookup_path('source', $path)) {
  37. // This is a numeric alias
  38. $path = drupal_get_normal_path($path);
  39. }
  40. else {
  41. // normal nid
  42. $path = 'node/' . $path;
  43. }
  44. }
  45. else {
  46. $path = drupal_get_normal_path($path);
  47. }
  48. // Handle the query
  49. $query = $_GET;
  50. unset($query['q']);
  51. $print_mail_hourly_threshold = variable_get('print_mail_hourly_threshold', PRINT_MAIL_HOURLY_THRESHOLD);
  52. if ((!user_access('send unlimited emails')) && (!flood_is_allowed('print_mail', $print_mail_hourly_threshold))) {
  53. $form['flood'] = array(
  54. '#type' => 'markup',
  55. '#markup' => '<p>' . format_plural($print_mail_hourly_threshold, 'You cannot send more than 1 message per hour. Please try again later.', 'You cannot send more than @count messages per hour. Please try again later.') . '</p>',
  56. );
  57. return $form;
  58. }
  59. $print_mail_teaser_default = variable_get('print_mail_teaser_default', PRINT_MAIL_TEASER_DEFAULT_DEFAULT);
  60. $print_mail_teaser_choice = variable_get('print_mail_teaser_choice', PRINT_MAIL_TEASER_CHOICE_DEFAULT);
  61. $form = array();
  62. $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL;
  63. $title = _print_get_title($path);
  64. if (count($form_state['input']) == 0) {
  65. $nodepath = drupal_get_normal_path($path);
  66. db_merge('print_mail_page_counter')
  67. ->key(array('path' => $nodepath))
  68. ->fields(array(
  69. 'totalcount' => 1,
  70. 'timestamp' => REQUEST_TIME,
  71. ))
  72. ->expression('totalcount', 'totalcount + 1')
  73. ->execute();
  74. }
  75. $form['path'] = array('#type' => 'value', '#value' => $path);
  76. $form['query'] = array('#type' => 'value', '#value' => $query);
  77. $form['cid'] = array('#type' => 'value', '#value' => $cid);
  78. $form['title'] = array('#type' => 'value', '#value' => $title);
  79. $form['fld_from_addr'] = array(
  80. '#type' => 'textfield',
  81. '#title' => t('Your email'),
  82. '#size' => 62,
  83. '#required' => TRUE,
  84. );
  85. $form['fld_from_name'] = array(
  86. '#type' => 'textfield',
  87. '#title' => t('Your name'),
  88. '#size' => 62,
  89. );
  90. $form['txt_to_addrs'] = array(
  91. '#type' => 'textarea',
  92. '#title' => t('Send to'),
  93. '#rows' => 3,
  94. '#resizable' => FALSE,
  95. '#description' => t('Enter multiple addresses separated by commas and/or different lines.'),
  96. '#required' => TRUE,
  97. );
  98. $form['fld_subject'] = array(
  99. '#type' => 'textfield',
  100. '#title' => t('Subject'),
  101. '#size' => 62,
  102. '#required' => TRUE,
  103. );
  104. if (!empty($title)) {
  105. // To prevent useless translation strings, try to translate only node titles
  106. if (drupal_substr($path, 0, 5) == 'node/') {
  107. $title = t($title);
  108. }
  109. $form['fld_title'] = array(
  110. '#type' => 'item',
  111. '#title' => t('Page to be sent'),
  112. '#markup' => l($title, $path, array('attributes' => array('title' => t('View page')), 'query' => $query)),
  113. );
  114. }
  115. $form['txt_message'] = array(
  116. '#type' => 'textarea',
  117. '#title' => t('Your message'),
  118. '#rows' => 6,
  119. '#required' => TRUE,
  120. );
  121. if ($print_mail_teaser_choice) {
  122. $form['chk_teaser'] = array(
  123. '#type' => 'checkbox',
  124. '#title' => t('Send only the teaser'),
  125. '#default_value' => $print_mail_teaser_default,
  126. );
  127. }
  128. else {
  129. $form['chk_teaser'] = array('#type' => 'value', '#value' => $print_mail_teaser_default);
  130. }
  131. $form['btn_submit'] = array(
  132. '#name' => 'submit',
  133. '#type' => 'submit',
  134. '#value' => t('Send email'),
  135. );
  136. $form['btn_clear'] = array(
  137. '#type' => 'markup',
  138. '#markup' => '<input type="reset" name="clear" id="edit-btn-clear" value="' . t('Clear form') . '" class="form-submit" />',
  139. );
  140. $form['btn_cancel'] = array(
  141. '#name' => 'cancel',
  142. '#type' => 'submit',
  143. '#value' => t('Cancel'),
  144. );
  145. if ($user->uid != 0) {
  146. $user_name = check_plain(strip_tags(theme('username', array('account' => $user))));
  147. $form['fld_from_addr']['#default_value'] = $user->mail;
  148. $form['fld_from_addr']['#disabled'] = TRUE;
  149. $form['fld_from_addr']['#value'] = $user->mail;
  150. $form['fld_from_name']['#default_value'] = $user_name;
  151. }
  152. else {
  153. $user_name = t('Someone');
  154. }
  155. $site_name = variable_get('site_name', t('an interesting site'));
  156. $print_mail_text_subject = filter_xss(variable_get('print_mail_text_subject', t('!user has sent you a message from !site')));
  157. $form['fld_subject']['#default_value'] = t($print_mail_text_subject, array('!user' => $user_name, '!site' => $site_name, '!title' => $title));
  158. $print_mail_text_content = filter_xss(variable_get('print_mail_text_content', ''));
  159. $form['txt_message']['#default_value'] = t($print_mail_text_content);
  160. return $form;
  161. }
  162. /**
  163. * Theme function for the send by-email form submission.
  164. *
  165. * Adds a class to the form labels. This class is used to place the label on
  166. * the left of the input fields.
  167. *
  168. * @ingroup forms
  169. */
  170. function theme_print_mail_form($variables) {
  171. $form = $variables['form'];
  172. drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css');
  173. $content = '';
  174. foreach (element_children($form) as $key) {
  175. $tmp = drupal_render($form[$key]);
  176. switch ($key) {
  177. case 'fld_from_addr':
  178. case 'fld_from_name':
  179. case 'txt_to_addrs':
  180. case 'fld_subject':
  181. case 'fld_title':
  182. $tmp = str_replace('<label', '<label class ="printmail-label"', $tmp);
  183. break;
  184. }
  185. $content .= $tmp;
  186. }
  187. return $content;
  188. }
  189. /**
  190. * Validate the send by-email form submission.
  191. *
  192. * @ingroup forms
  193. */
  194. function print_mail_form_validate($form, &$form_state) {
  195. if (array_key_exists('cancel', $form_state['input'])) {
  196. form_set_error(NULL, '', TRUE);
  197. drupal_get_messages('error');
  198. drupal_goto(preg_replace('!^book/export/html/!', 'node/', $form_state['values']['path']), array('query' => $form_state['values']['query']));
  199. return;
  200. }
  201. $from_addr = trim($form_state['values']['fld_from_addr']);
  202. $test = user_validate_mail($from_addr);
  203. if ($test) {
  204. form_set_error('fld_from_addr', $test);
  205. }
  206. // All new-lines are replaced by commas
  207. $to_addrs = preg_replace('![\r|\n|,]+!', ',', trim($form_state['values']['txt_to_addrs']));
  208. // Create an array from the string
  209. $to_array = explode(',', $to_addrs);
  210. // Verify each element of the array
  211. foreach ($to_array as $key => $address) {
  212. $address = trim($address);
  213. if (preg_match('/(.*?) <(.*)>/s', $address, $matches)) {
  214. // Address is of the type User Name <user@domain.tld>
  215. $test = user_validate_mail($matches[2]);
  216. $to_array[$key] = trim($matches[1]) . ' <' . $matches[2] . '>';
  217. }
  218. else {
  219. // Address must be user@domain.tld
  220. $test = user_validate_mail($address);
  221. }
  222. if ($test) {
  223. form_set_error('txt_to_addrs', $test);
  224. }
  225. }
  226. $print_mail_hourly_threshold = variable_get('print_mail_hourly_threshold', PRINT_MAIL_HOURLY_THRESHOLD);
  227. if ((!user_access('send unlimited emails')) && (!flood_is_allowed('print_mail', $print_mail_hourly_threshold - count($to_array) + 1))) {
  228. form_set_error('txt_to_addrs', t('You cannot send more than %number messages per hour. Please reduce the number of recipients.', array('%number' => $print_mail_hourly_threshold)));
  229. }
  230. // In all fields, prevent insertion of custom headers
  231. foreach ($form_state['values'] as $key => $string) {
  232. if ( (drupal_substr($key, 0, 4) == 'fld_') && ((strpos($string, "\n") !== FALSE) || (strpos($string, "\r") !== FALSE)) ) {
  233. form_set_error($key, 'Found invalid character');
  234. }
  235. }
  236. $form_state['values']['fld_from_addr'] = $from_addr;
  237. $form_state['values']['fld_from_name'] = trim($form_state['values']['fld_from_name']);
  238. // Re-create the string from the re-organized array
  239. $form_state['values']['txt_to_addrs'] = implode(', ', $to_array);
  240. }
  241. /**
  242. * Process the send by-email form submission.
  243. *
  244. * @ingroup forms
  245. */
  246. function print_mail_form_submit($form, &$form_state) {
  247. if (!array_key_exists('cancel', $form_state['values'])) {
  248. $cid = isset($form_state['values']['cid']) ? $form_state['values']['cid'] : NULL;
  249. $print_mail_text_message = filter_xss_admin(variable_get('print_mail_text_message', t('Message from sender')));
  250. $sender_message = $print_mail_text_message . ':<br /><br /><em>' . nl2br(check_plain($form_state['values']['txt_message'])) . '</em>';
  251. $print = print_controller($form_state['values']['path'], $form_state['values']['query'], $cid, PRINT_MAIL_FORMAT, $form_state['values']['chk_teaser'], $sender_message);
  252. if ($print !== FALSE) {
  253. $print_mail_send_option_default = variable_get('print_mail_send_option_default', PRINT_MAIL_SEND_OPTION_DEFAULT);
  254. $params = array();
  255. $params['subject'] = $form_state['values']['fld_subject'];
  256. $params['message'] = $sender_message;
  257. $params['link'] = $print['url'];
  258. $params['title'] = $form_state['values']['title'];
  259. // If a name is provided, make From: in the format Common Name <address>
  260. if (!empty($form_state['values']['fld_from_name'])) {
  261. $from = '"' . mime_header_encode($form_state['values']['fld_from_name']) . '" <' . $form_state['values']['fld_from_addr'] . '>';
  262. }
  263. else {
  264. $from = $form_state['values']['fld_from_addr'];
  265. }
  266. // If using reply-to, move the From: info to the params array, so that it is passed to hook_mail later
  267. if (variable_get('print_mail_use_reply_to', PRINT_MAIL_USE_REPLY_TO)) {
  268. $params['from'] = $from;
  269. $from = NULL;
  270. }
  271. // Spaces in img URLs must be replaced with %20
  272. $pattern = '!<(img\s[^>]*?)>!is';
  273. $print['content'] = preg_replace_callback($pattern, '_print_replace_spaces', $print['content']);
  274. $node = $print['node'];
  275. $params['body'] = theme('print', array('print' => $print, 'type' => PRINT_MAIL_FORMAT, 'node' => $node));
  276. // Img elements must be set to absolute
  277. $pattern = '!<(img\s[^>]*?)>!is';
  278. $params['body'] = preg_replace_callback($pattern, '_print_rewrite_urls', $params['body']);
  279. // Convert the a href elements, to make sure no relative links remain
  280. $pattern = '!<(a\s[^>]*?)>!is';
  281. $params['body'] = preg_replace_callback($pattern, '_print_rewrite_urls', $params['body']);
  282. $ok = FALSE;
  283. $use_job_queue = variable_get('print_mail_job_queue', PRINT_MAIL_JOB_QUEUE_DEFAULT);
  284. if ($use_job_queue) {
  285. $queue = DrupalQueue::get('print_mail_send');
  286. }
  287. $addresses = explode(', ', $form_state['values']['txt_to_addrs']);
  288. foreach ($addresses as $to) {
  289. if ($use_job_queue) {
  290. // Use job queue to send mails during cron runs
  291. $queue->createItem(array('module' => 'print_mail', 'key' => $print_mail_send_option_default, 'to' => $to, 'language' => language_default(), 'params' => $params, 'from' => $from));
  292. }
  293. else {
  294. // Send mail immediately using Drupal's mail handler
  295. $ret = drupal_mail('print_mail', $print_mail_send_option_default, $to, language_default(), $params, $from);
  296. }
  297. if ($use_job_queue || $ret['result']) {
  298. flood_register_event('print_mail');
  299. $ok = TRUE;
  300. }
  301. }
  302. if ($ok) {
  303. $query = empty($form_state['values']['query']) ? '' : '?' . rawurldecode(drupal_http_build_query($form_state['values']['query']));
  304. 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'] . $query, '%to' => $form_state['values']['txt_to_addrs']));
  305. $site_name = variable_get('site_name', t('us'));
  306. $print_mail_text_confirmation = variable_get('print_mail_text_confirmation', t('Thank you for spreading the word about !site.'));
  307. drupal_set_message(check_plain(t($print_mail_text_confirmation, array('!site' => $site_name))));
  308. $nodepath = drupal_get_normal_path($form_state['values']['path']);
  309. db_update('print_mail_page_counter')
  310. ->fields(array(
  311. 'sentcount' => 1,
  312. 'sent_timestamp' => REQUEST_TIME,
  313. ))
  314. ->condition('path', $nodepath, '=')
  315. ->expression('sentcount', 'sentcount + :inc', array(':inc' => count($addresses)))
  316. ->execute();
  317. }
  318. }
  319. }
  320. $form_state['redirect'] = array(preg_replace('!^book/export/html/!', 'node/', $form_state['values']['path']), array('query' => $form_state['values']['query']));
  321. }

Related topics

Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.