print_mail.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. // Include MIME library
  12. @include_once('Mail/mime.php');
  13. /**
  14. * Form constructor for the send by email form.
  15. *
  16. * @ingroup forms
  17. */
  18. function print_mail_form($form, &$form_state) {
  19. // Remove the printmail/ prefix
  20. $path_arr = explode('/', $_GET['q']);
  21. unset($path_arr[0]);
  22. $path = filter_xss(implode('/', $path_arr));
  23. if (empty($path)) {
  24. // If no path was provided, let's try to generate a page for the referer
  25. global $base_url;
  26. $link = print_mail_print_link();
  27. $ref = $_SERVER['HTTP_REFERER'];
  28. $path = preg_replace("!^$base_url/!", '', $ref);
  29. if (($path === $ref) || empty($path)) {
  30. $path = variable_get('site_frontpage', 'node');
  31. }
  32. drupal_goto($link['path'] . '/' . $path);
  33. }
  34. elseif (ctype_digit($path_arr[1])) {
  35. if (drupal_lookup_path('source', $path)) {
  36. // This is a numeric alias
  37. $path = drupal_get_normal_path($path);
  38. }
  39. else {
  40. // normal nid
  41. $path = 'node/' . $path;
  42. }
  43. }
  44. else {
  45. $path = drupal_get_normal_path($path);
  46. }
  47. // Handle the query
  48. $query = $_GET;
  49. unset($query['q']);
  50. return print_mail_form_for_path($form, $form_state, $path, $query);
  51. }
  52. /**
  53. * Build email form for the page provided in the path argument.
  54. *
  55. * @ingroup forms
  56. */
  57. function print_mail_form_for_path($form, &$form_state, $path, $query = NULL, $user = NULL) {
  58. if ($user === NULL) {
  59. global $user;
  60. }
  61. $print_mail_hourly_threshold = variable_get('print_mail_hourly_threshold', PRINT_MAIL_HOURLY_THRESHOLD);
  62. if ((!user_access('send unlimited emails')) && (!flood_is_allowed('print_mail', $print_mail_hourly_threshold))) {
  63. $form['flood'] = array(
  64. '#type' => 'markup',
  65. '#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>',
  66. );
  67. return $form;
  68. }
  69. $print_mail_teaser_default = variable_get('print_mail_teaser_default', PRINT_MAIL_TEASER_DEFAULT_DEFAULT);
  70. $print_mail_teaser_choice = variable_get('print_mail_teaser_choice', PRINT_MAIL_TEASER_CHOICE_DEFAULT);
  71. $print_mail_user_recipients_default = variable_get('print_mail_user_recipients', PRINT_MAIL_USER_RECIPIENTS_DEFAULT);
  72. $form = array();
  73. $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL;
  74. $title = _print_get_title($path);
  75. if ($print_mail_user_recipients_default) {
  76. $options = array();
  77. if (module_exists('realname')) {
  78. $sql = "SELECT u.mail, r.realname AS name from {users} u LEFT JOIN {realname} r ON u.uid = r.uid WHERE u.uid <> :uid ORDER BY name ASC";
  79. }
  80. else {
  81. $sql = "SELECT mail, name from {users} WHERE uid <> :uid ORDER BY name ASC";
  82. }
  83. $recipients = db_query($sql, array(':uid' => drupal_anonymous_user()->uid));
  84. foreach ($recipients as $recipient) {
  85. $options[$recipient->mail] = $recipient->name;
  86. }
  87. }
  88. if (count($form_state['input']) == 0) {
  89. $nodepath = drupal_get_normal_path($path);
  90. db_merge('print_mail_page_counter')
  91. ->key(array('path' => $nodepath))
  92. ->fields(array(
  93. 'totalcount' => 1,
  94. 'timestamp' => REQUEST_TIME,
  95. ))
  96. ->expression('totalcount', 'totalcount + 1')
  97. ->execute();
  98. }
  99. $form['path'] = array('#type' => 'value', '#value' => $path);
  100. $form['query'] = array('#type' => 'value', '#value' => $query);
  101. $form['cid'] = array('#type' => 'value', '#value' => $cid);
  102. $form['title'] = array('#type' => 'value', '#value' => $title);
  103. $form['fld_from_addr'] = array(
  104. '#type' => 'textfield',
  105. '#title' => t('Your email'),
  106. '#size' => 62,
  107. '#required' => TRUE,
  108. );
  109. $form['fld_from_name'] = array(
  110. '#type' => 'textfield',
  111. '#title' => t('Your name'),
  112. '#size' => 62,
  113. );
  114. $form['txt_to'] = array(
  115. '#tree' => TRUE,
  116. );
  117. $form['txt_to']['addrs'] = array(
  118. '#type' => 'textarea',
  119. '#title' => t('Send to'),
  120. '#rows' => 3,
  121. '#resizable' => FALSE,
  122. '#description' => t('Enter multiple addresses separated by commas and/or different lines.'),
  123. '#required' => !$print_mail_user_recipients_default,
  124. );
  125. if ($print_mail_user_recipients_default) {
  126. $form['txt_to']['users'] = array(
  127. '#type' => 'select',
  128. '#title' => t('Send to users'),
  129. '#multiple' => TRUE,
  130. '#size' => 10,
  131. '#options' => $options,
  132. );
  133. }
  134. $form['fld_subject'] = array(
  135. '#type' => 'textfield',
  136. '#title' => t('Subject'),
  137. '#size' => 62,
  138. '#required' => TRUE,
  139. );
  140. if (!empty($title)) {
  141. // To prevent useless translation strings, try to translate only non-node titles
  142. if (drupal_substr($path, 0, 5) != 'node/') {
  143. $title = t($title);
  144. }
  145. $form['fld_title'] = array(
  146. '#type' => 'item',
  147. '#title' => t('Page to be sent'),
  148. '#markup' => l($title, $path, array('attributes' => array('title' => t('View page')), 'query' => $query)),
  149. );
  150. }
  151. $form['txt_message'] = array(
  152. '#type' => 'textarea',
  153. '#title' => t('Your message'),
  154. '#rows' => 6,
  155. '#required' => TRUE,
  156. );
  157. if ($print_mail_teaser_choice) {
  158. $form['chk_teaser'] = array(
  159. '#type' => 'checkbox',
  160. '#title' => t('Send only the teaser'),
  161. '#default_value' => $print_mail_teaser_default,
  162. );
  163. }
  164. else {
  165. $form['chk_teaser'] = array('#type' => 'value', '#value' => $print_mail_teaser_default);
  166. }
  167. $form['btn_submit'] = array(
  168. '#name' => 'submit',
  169. '#type' => 'submit',
  170. '#value' => t('Send email'),
  171. );
  172. $form['btn_cancel'] = array(
  173. '#name' => 'cancel',
  174. '#type' => 'submit',
  175. '#value' => t('Cancel'),
  176. );
  177. if ($user->uid != 0) {
  178. $user_name = check_plain(strip_tags(theme('username', array('account' => $user))));
  179. $form['fld_from_addr']['#default_value'] = $user->mail;
  180. $form['fld_from_addr']['#disabled'] = TRUE;
  181. $form['fld_from_addr']['#value'] = $user->mail;
  182. $form['fld_from_name']['#default_value'] = $user_name;
  183. }
  184. else {
  185. $user_name = t('Someone');
  186. }
  187. $site_name = variable_get('site_name', t('an interesting site'));
  188. $form['fld_subject']['#default_value'] = t('!user has sent you a message from !site', array('!user' => $user_name, '!site' => $site_name, '!title' => $title));
  189. $form['txt_message']['#default_value'] = t('');
  190. return $form;
  191. }
  192. /**
  193. * Returns HTML for the send by-email form.
  194. *
  195. * Adds a class to the form labels. This class is used to place the label on
  196. * the left of the input fields.
  197. *
  198. * @param array $form
  199. * Form array
  200. *
  201. * @see print_mail_form()
  202. * @ingroup forms
  203. * @ingroup themeable
  204. * @ingroup print_themeable
  205. */
  206. function theme_print_mail_form($variables) {
  207. $form = $variables['form'];
  208. drupal_add_css(drupal_get_path('module', 'print_mail') . '/css/print_mail.theme.css');
  209. $content = '';
  210. foreach (element_children($form) as $key) {
  211. $tmp = drupal_render($form[$key]);
  212. switch ($key) {
  213. case 'fld_from_addr':
  214. case 'fld_from_name':
  215. case 'txt_to':
  216. case 'fld_subject':
  217. case 'fld_title':
  218. $tmp = str_replace('<label', '<label class ="printmail-label"', $tmp);
  219. break;
  220. }
  221. $content .= $tmp;
  222. }
  223. return $content;
  224. }
  225. /**
  226. * Theme function for the email sending just the link.
  227. *
  228. * Allows themes and modules to override the default sendlink plain text format.
  229. *
  230. * @param $params
  231. * $params as passed to print_mail_mail().
  232. *
  233. * @ingroup themeable
  234. * @ingroup print_themeable
  235. */
  236. function theme_print_mail_sendlink_plain($params) {
  237. return $params['message'] . '\n\n' . $params['link'];
  238. }
  239. /**
  240. * Theme function for the email sending just the link.
  241. *
  242. * Allows themes and modules to override the default sendlink HTML format.
  243. *
  244. * @param $params
  245. * $params as passed to print_mail_mail().
  246. *
  247. * @ingroup themeable
  248. * @ingroup print_themeable
  249. */
  250. function theme_print_mail_sendlink_html($params) {
  251. return $params['message'] . '<br/><br/>' . l($params['title'], $params['link']);
  252. }
  253. /**
  254. * Form validation handler for print_mail_form().
  255. *
  256. * @see print_mail_form()
  257. * @ingroup forms
  258. */
  259. function print_mail_form_validate($form, &$form_state) {
  260. $print_mail_user_recipients_default = variable_get('print_mail_user_recipients', PRINT_MAIL_USER_RECIPIENTS_DEFAULT);
  261. if (array_key_exists('cancel', $form_state['input'])) {
  262. form_set_error(NULL, '', TRUE);
  263. drupal_get_messages('error');
  264. drupal_goto(preg_replace('!^book/export/html/!', 'node/', $form_state['values']['path']), array('query' => $form_state['values']['query']));
  265. return;
  266. }
  267. $from_addr = trim($form_state['values']['fld_from_addr']);
  268. $test = user_validate_mail($from_addr);
  269. if ($test) {
  270. form_set_error('fld_from_addr', $test);
  271. }
  272. $to_array = array();
  273. if (!empty($form_state['values']['txt_to']['users'])) {
  274. $to_array = array_values($form_state['values']['txt_to']['users']);
  275. }
  276. if (!empty($form_state['values']['txt_to']['addrs'])) {
  277. // All new-lines are replaced by commas
  278. $to_addrs = preg_replace('![\r|\n|,]+!', ',', trim($form_state['values']['txt_to']['addrs']));
  279. // Create an array from the string
  280. $to_array = array_merge($to_array, explode(',', $to_addrs));
  281. }
  282. if (empty($to_array) && $print_mail_user_recipients_default) {
  283. form_set_error('txt_to', t('You must specify at least one email address or user as a recipient.'));
  284. }
  285. // Verify each element of the array
  286. foreach ($to_array as $key => $address) {
  287. $address = trim($address);
  288. if (preg_match('/(.*?) <(.*)>/s', $address, $matches)) {
  289. // Address is of the type User Name <user@domain.tld>
  290. $test = user_validate_mail($matches[2]);
  291. $to_array[$key] = trim($matches[1]) . ' <' . $matches[2] . '>';
  292. }
  293. else {
  294. // Address must be user@domain.tld
  295. $test = user_validate_mail($address);
  296. }
  297. if ($test) {
  298. form_set_error('txt_to', $test);
  299. }
  300. }
  301. $print_mail_hourly_threshold = variable_get('print_mail_hourly_threshold', PRINT_MAIL_HOURLY_THRESHOLD);
  302. if ((!user_access('send unlimited emails')) && (!flood_is_allowed('print_mail', $print_mail_hourly_threshold - count($to_array) + 1))) {
  303. form_set_error('txt_to', t('You cannot send more than %number messages per hour. Please reduce the number of recipients.', array('%number' => $print_mail_hourly_threshold)));
  304. }
  305. // In all fields, prevent insertion of custom headers
  306. foreach ($form_state['values'] as $key => $string) {
  307. if ( (drupal_substr($key, 0, 4) == 'fld_') && ((strpos($string, "\n") !== FALSE) || (strpos($string, "\r") !== FALSE)) ) {
  308. form_set_error($key, 'Found invalid character');
  309. }
  310. }
  311. $form_state['values']['fld_from_addr'] = $from_addr;
  312. $form_state['values']['fld_from_name'] = trim($form_state['values']['fld_from_name']);
  313. // Re-create the string from the re-organized array
  314. $form_state['values']['txt_to']['addrs'] = implode(', ', $to_array);
  315. }
  316. /**
  317. * Form submission handler for print_mail_form().
  318. *
  319. * @see print_mail_form()
  320. * @see print_controller()
  321. * @ingroup forms
  322. */
  323. function print_mail_form_submit($form, &$form_state) {
  324. if (!array_key_exists('cancel', $form_state['values'])) {
  325. module_load_include('inc', 'print', 'print.pages');
  326. module_load_include('inc', 'print', 'includes/print');
  327. $link = print_mail_print_link();
  328. $cid = isset($form_state['values']['cid']) ? $form_state['values']['cid'] : NULL;
  329. $view_mode = $form_state['values']['chk_teaser'] ? 'teaser' : PRINT_VIEW_MODE;
  330. $node = print_controller($form_state['values']['path'], $link['format'], $cid, $view_mode);
  331. if ($node) {
  332. $print_mail_send_option_default = variable_get('print_mail_send_option_default', PRINT_MAIL_SEND_OPTION_DEFAULT);
  333. $params = array();
  334. $params['subject'] = $form_state['values']['fld_subject'];
  335. $params['message'] = t('Message from sender') . ':<br /><br /><em>' . nl2br(check_plain($form_state['values']['txt_message'])) . '</em>';
  336. $params['link'] = url($form_state['values']['path'], array('absolute' => TRUE, 'query' => $form_state['values']['query']));
  337. $params['title'] = $form_state['values']['title'];
  338. // If a name is provided, make From: in the format Common Name <address>
  339. if (!empty($form_state['values']['fld_from_name'])) {
  340. $from = '"' . mime_header_encode($form_state['values']['fld_from_name']) . '" <' . $form_state['values']['fld_from_addr'] . '>';
  341. }
  342. else {
  343. $from = $form_state['values']['fld_from_addr'];
  344. }
  345. // If using reply-to, move the From: info to the params array, so that it is passed to hook_mail later
  346. if (variable_get('print_mail_use_reply_to', PRINT_MAIL_USE_REPLY_TO)) {
  347. $params['from'] = $from;
  348. $from = NULL;
  349. }
  350. // Spaces in img URLs must be replaced with %20
  351. $pattern = '!<(img\s[^>]*?)>!is';
  352. $node->content = preg_replace_callback($pattern, '_print_replace_spaces', $node->content);
  353. $params['body'] = theme('print', array('node' => $node, 'query' => $form_state['values']['query'], 'format' => $link['format'], 'expand_css' => TRUE, 'message' => $params['message']));
  354. // Img elements must be set to absolute
  355. $pattern = '!<(img\s[^>]*?)>!is';
  356. $params['body'] = preg_replace_callback($pattern, '_print_rewrite_urls', $params['body']);
  357. // Convert the a href elements, to make sure no relative links remain
  358. $pattern = '!<(a\s[^>]*?)>!is';
  359. $params['body'] = preg_replace_callback($pattern, '_print_rewrite_urls', $params['body']);
  360. // Enable support for third-party modules to alter the e-mail before it's sent
  361. drupal_alter('print_mail', $params, $to);
  362. $ok = FALSE;
  363. $use_job_queue = variable_get('print_mail_job_queue', PRINT_MAIL_JOB_QUEUE_DEFAULT);
  364. if ($use_job_queue) {
  365. $queue = DrupalQueue::get('print_mail_send');
  366. }
  367. $addresses = explode(', ', $form_state['values']['txt_to']['addrs']);
  368. foreach ($addresses as $to) {
  369. if ($use_job_queue) {
  370. // Use job queue to send mails during cron runs
  371. $queue->createItem(array('module' => 'print_mail', 'key' => $print_mail_send_option_default, 'to' => $to, 'language' => language_default(), 'params' => $params, 'from' => $from));
  372. }
  373. else {
  374. // Send mail immediately using Drupal's mail handler
  375. $ret = drupal_mail('print_mail', $print_mail_send_option_default, $to, language_default(), $params, $from);
  376. }
  377. if ($use_job_queue || $ret['result']) {
  378. flood_register_event('print_mail');
  379. $ok = TRUE;
  380. }
  381. }
  382. if ($ok) {
  383. $query = empty($form_state['values']['query']) ? '' : '?' . rawurldecode(drupal_http_build_query($form_state['values']['query']));
  384. 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']));
  385. $site_name = variable_get('site_name', t('us'));
  386. drupal_set_message(check_plain(t('Thank you for spreading the word about !site.', array('!site' => $site_name))));
  387. $nodepath = drupal_get_normal_path($form_state['values']['path']);
  388. db_update('print_mail_page_counter')
  389. ->fields(array(
  390. 'sentcount' => 1,
  391. 'sent_timestamp' => REQUEST_TIME,
  392. ))
  393. ->condition('path', $nodepath, '=')
  394. ->expression('sentcount', 'sentcount + :inc', array(':inc' => count($addresses)))
  395. ->execute();
  396. }
  397. }
  398. }
  399. $form_state['redirect'] = array(preg_replace('!^book/export/html/!', 'node/', $form_state['values']['path']), array('query' => $form_state['values']['query']));
  400. }