print_mail.inc 17 KB

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