functions.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * functions.php
  4. *
  5. * Implementation of RFC 2369 for SquirrelMail.
  6. * When viewing a message from a mailinglist complying with this RFC,
  7. * this plugin displays a menu which gives the user a choice of mailinglist
  8. * commands such as (un)subscribe, help and list archives.
  9. *
  10. * @copyright 1999-2025 The SquirrelMail Project Team
  11. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  12. * @version $Id$
  13. * @package plugins
  14. * @subpackage listcommands
  15. */
  16. /**
  17. * Get current list of subscribed non-RFC-compliant mailing lists for logged-in user
  18. *
  19. * @return array The list of mailing list addresses, keyed by integer index
  20. */
  21. function get_non_rfc_lists() {
  22. global $username, $data_dir;
  23. $lists = getPref($data_dir, $username, 'non_rfc_lists', array());
  24. $new_lists = array();
  25. if (!empty($lists)) {
  26. $lists = explode(':', $lists);
  27. foreach ($lists as $list) {
  28. list($index, $list_addr) = explode('_', $list);
  29. if ((!empty($index) || $index === '0') && !empty($list_addr))
  30. $new_lists[$index] = $list_addr;
  31. }
  32. }
  33. $lists = $new_lists;
  34. sort($lists);
  35. return $lists;
  36. }
  37. /**
  38. * Show mailing list management option section on options page
  39. */
  40. function plugin_listcommands_optpage_register_block_do()
  41. {
  42. global $optpage_blocks, $listcommands_allow_non_rfc_list_management;
  43. // only allow management of non-RFC lists if admin deems necessary
  44. //
  45. @include_once(SM_PATH . 'plugins/listcommands/config.php');
  46. if (!$listcommands_allow_non_rfc_list_management)
  47. return;
  48. $optpage_blocks[] = array(
  49. 'name' => _("Mailing Lists"),
  50. 'url' => '../plugins/listcommands/options.php',
  51. 'desc' => _("Manage the (non-RFC-compliant) mailing lists that you are subscribed to for the purpose of providing one-click list replies when responding to list messages."),
  52. 'js' => false
  53. );
  54. }
  55. /**
  56. * internal function that builds mailing list links
  57. */
  58. function plugin_listcommands_menu_do() {
  59. global $passed_id, $passed_ent_id, $mailbox, $message,
  60. $startMessage, $oTemplate, $listcommands_allow_non_rfc_list_management;
  61. @include_once(SM_PATH . 'plugins/listcommands/config.php');
  62. /**
  63. * Array of commands we can deal with from the header. The Reply option
  64. * is added later because we generate it using the Post information.
  65. */
  66. $fieldsdescr = listcommands_fieldsdescr();
  67. $links = array();
  68. foreach ($message->rfc822_header->mlist as $cmd => $actions) {
  69. /* I don't know this action... skip it */
  70. if ( !array_key_exists($cmd, $fieldsdescr) ) {
  71. continue;
  72. }
  73. /* proto = {mailto,href} */
  74. $aActions = array_keys($actions);
  75. // note that we only use the first cmd/action, ignore the rest
  76. $proto = array_shift($aActions);
  77. $act = array_shift($actions);
  78. if ($proto == 'mailto') {
  79. $identity = '';
  80. if (($cmd == 'post') || ($cmd == 'owner')) {
  81. $url = 'src/compose.php?'.
  82. (isset($startMessage)?'startMessage='.$startMessage.'&amp;':'');
  83. } else {
  84. $url = "plugins/listcommands/mailout.php?action=$cmd&amp;";
  85. // try to find which identity the mail should come from
  86. include_once(SM_PATH . 'functions/identity.php');
  87. $idents = get_identities();
  88. // ripped from src/compose.php
  89. $identities = array();
  90. if (count($idents) > 1) {
  91. foreach($idents as $nr=>$data) {
  92. $enc_from_name = '"'.$data['full_name'].'" <'. $data['email_address'].'>';
  93. $identities[] = $enc_from_name;
  94. }
  95. $identity_match = $message->rfc822_header->findAddress($identities);
  96. if ($identity_match !== FALSE) {
  97. $identity = $identity_match;
  98. }
  99. }
  100. }
  101. // if things like subject are given, peel them off and give
  102. // them to src/compose.php as is (not encoded)
  103. if (strpos($act, '?') > 0) {
  104. list($act, $parameters) = explode('?', $act, 2);
  105. $parameters = '&amp;identity=' . $identity . '&amp;' . $parameters;
  106. } else {
  107. $parameters = '&amp;identity=' . $identity;
  108. }
  109. $url .= 'send_to=' . urlencode($act) . $parameters;
  110. $links[$cmd] = makeComposeLink($url, $fieldsdescr[$cmd]);
  111. if ($cmd == 'post') {
  112. if (!isset($mailbox))
  113. $mailbox = 'INBOX';
  114. $url .= '&amp;passed_id='.$passed_id.
  115. '&amp;mailbox='.urlencode($mailbox).
  116. (isset($passed_ent_id)?'&amp;passed_ent_id='.$passed_ent_id:'');
  117. $url .= '&amp;smaction=reply';
  118. $links['reply'] = makeComposeLink($url, $fieldsdescr['reply']);
  119. }
  120. } else if ($proto == 'href') {
  121. $links[$cmd] = create_hyperlink($act, $fieldsdescr[$cmd], '_blank');
  122. }
  123. }
  124. // allow non-rfc reply link if admin allows and message is from
  125. // non-rfc list the user has configured
  126. //
  127. if ($listcommands_allow_non_rfc_list_management) {
  128. $non_rfc_lists = get_non_rfc_lists();
  129. $recipients = formatRecipientString($message->rfc822_header->to, "to") . ' '
  130. . formatRecipientString($message->rfc822_header->cc, "cc") . ' '
  131. . formatRecipientString($message->rfc822_header->bcc, "bcc");
  132. if (!in_array('post', array_keys($links))) {
  133. foreach ($non_rfc_lists as $non_rfc_list) {
  134. if (preg_match('/(^|,|<|\s)' . preg_quote($non_rfc_list) . '($|,|>|\s)/', $recipients)) {
  135. $url = 'src/compose.php?'
  136. . (isset($startMessage)?'startMessage='.$startMessage.'&amp;':'')
  137. . 'send_to=' . str_replace('?','&amp;', $non_rfc_list);
  138. $links['post'] = makeComposeLink($url, $fieldsdescr['post']);
  139. break;
  140. }
  141. }
  142. }
  143. if (!in_array('reply', array_keys($links))) {
  144. foreach ($non_rfc_lists as $non_rfc_list) {
  145. if (preg_match('/(^|,|\s)' . preg_quote($non_rfc_list) . '($|,|\s)/', $recipients)) {
  146. if (!isset($mailbox))
  147. $mailbox = 'INBOX';
  148. $url = 'src/compose.php?'
  149. . (isset($startMessage)?'startMessage='.$startMessage.'&amp;':'')
  150. . 'send_to=' . str_replace('?','&amp;', $non_rfc_list)
  151. . '&amp;passed_id='.$passed_id
  152. . '&amp;mailbox='.urlencode($mailbox)
  153. . (isset($passed_ent_id)?'&amp;passed_ent_id='.$passed_ent_id:'')
  154. . '&amp;smaction=reply';
  155. $links['reply'] = makeComposeLink($url, $fieldsdescr['reply']);
  156. break;
  157. }
  158. }
  159. }
  160. }
  161. if (count($links) > 0) {
  162. $oTemplate->assign('links', $links);
  163. $output = $oTemplate->fetch('plugins/listcommands/read_body_header.tpl');
  164. return array('read_body_header' => $output);
  165. }
  166. }
  167. /**
  168. * Returns an array with the actions as translated strings.
  169. * @return array action as key, translated string as value
  170. */
  171. function listcommands_fieldsdescr() {
  172. return array('post' => _("Post to List"),
  173. 'reply' => _("Reply to List"),
  174. 'subscribe' => _("Subscribe"),
  175. 'unsubscribe' => _("Unsubscribe"),
  176. 'archive' => _("List Archives"),
  177. 'owner' => _("Contact Listowner"),
  178. 'help' => _("Help"));
  179. }