E_ALL is a moving target
[squirrelmail.git] / plugins / spamcop / functions.php
CommitLineData
da297dd0 1<?php
4b4abf93 2
da297dd0 3/**
4 * SpamCop plugin - functions
5 *
c0d96801 6 * @copyright 1999-2012 The SquirrelMail Project Team
da297dd0 7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
8 * @version $Id$
9 * @package plugins
10 * @subpackage spamcop
11 */
12
13/* sqgetGlobalVar(), getPref(), setPref(), sqimap functions are used */
14
15/**
16 * Disable Quick Reporting by default
17 * @global boolean $spamcop_quick_report
18 * @since 1.4.3 and 1.5.0
19 */
20global $spamcop_quick_report;
21$spamcop_quick_report = false;
22
23/**
24 * Loads spamcop settings and validates some of values (make '' into 'default', etc.)
25 *
26 * Internal function used to reduce size of setup.php
27 * @since 1.5.1
28 * @access private
29 */
30function spamcop_load_function() {
31 global $username, $data_dir, $spamcop_enabled, $spamcop_delete, $spamcop_save,
32 $spamcop_method, $spamcop_id, $spamcop_quick_report, $spamcop_type;
33
34 $spamcop_enabled = getPref($data_dir, $username, 'spamcop_enabled');
35 $spamcop_delete = getPref($data_dir, $username, 'spamcop_delete');
36 $spamcop_save = getPref($data_dir, $username, 'spamcop_save',true);
37 $spamcop_method = getPref($data_dir, $username, 'spamcop_method');
38 $spamcop_type = getPref($data_dir, $username, 'spamcop_type');
39 $spamcop_id = getPref($data_dir, $username, 'spamcop_id');
40 if ($spamcop_method == '') {
41 // Default to web_form. It is faster.
42 $spamcop_method = 'web_form';
43 setPref($data_dir, $username, 'spamcop_method', $spamcop_method);
44 }
45 if (! $spamcop_quick_report && $spamcop_method=='quick_email') {
46 $spamcop_method = 'web_form';
47 setPref($data_dir, $username, 'spamcop_method', $spamcop_method);
48 }
49 if ($spamcop_type == '') {
50 $spamcop_type = 'free';
51 setPref($data_dir, $username, 'spamcop_type', $spamcop_type);
52 }
53 if ($spamcop_id == '')
54 $spamcop_enabled = 0;
55}
56
57/**
58 * Add spamcop link to read_body (internal function)
59 * @since 1.5.1
60 * @access private
61 */
7dc06407 62function spamcop_show_link_function(&$links) {
4ae656b7 63 global $spamcop_enabled, $spamcop_method, $spamcop_quick_report;
da297dd0 64
65 if (! $spamcop_enabled)
66 return;
67
68 /* GLOBALS */
51bbe8fa 69 sqgetGlobalVar('passed_id', $passed_id, SQ_FORM, NULL, SQ_TYPE_BIGINT);
da297dd0 70 sqgetGlobalVar('passed_ent_id',$passed_ent_id,SQ_FORM);
71 sqgetGlobalVar('mailbox', $mailbox, SQ_FORM);
72 if ( sqgetGlobalVar('startMessage', $startMessage, SQ_FORM) ) {
73 $startMessage = (int)$startMessage;
74 }
75 /* END GLOBALS */
76
77 // catch unset passed_ent_id
78 if (! sqgetGlobalVar('passed_ent_id', $passed_ent_id, SQ_FORM) ) {
79 $passed_ent_id = 0;
80 }
81
da297dd0 82 /*
83 Catch situation when user uses quick_email and does not update
84 preferences. User gets web_form link. If prefs are set to
85 quick_email format - they will be updated after clicking the link
86 */
87 if (! $spamcop_quick_report && $spamcop_method=='quick_email') {
88 $spamcop_method = 'web_form';
89 }
90
7dc06407 91// FIXME: do we need this javascript and if so, fix it
92// <script type="text/javascript">
93// document.write('<a href="../plugins/spamcop/spamcop.php?passed_id=<php echo urlencode($passed_id); >&amp;js_web=1&amp;mailbox=<php echo urlencode($mailbox); >&amp;passed_ent_id=<php echo urlencode($passed_ent_id); >" target="_blank">');
94//document.write("<php echo _("Report as Spam"); >");
95//document.write("</a>");
96//</script>
97
98
99 $url = '../plugins/spamcop/spamcop.php?passed_id=' . urlencode($passed_id) .
100 '&amp;mailbox=' . urlencode($mailbox) . '&amp;startMessage=' . urlencode($startMessage) .
101 '&amp;passed_ent_id=' . urlencode($passed_ent_id);
4ae656b7 102 if ( $spamcop_method == 'web_form' && checkForJavascript() ) {
7dc06407 103 $url .= '&amp;js_web=1';
da297dd0 104 }
7dc06407 105
106 $links[] = array ( 'URL' => $url,
107 'Text' => _("Report as Spam")
108 );
da297dd0 109}
110
111/**
112 * Add spamcop option block (internal function)
113 * @since 1.5.1
114 * @access private
115 */
116function spamcop_options_function() {
117 global $optpage_blocks;
118
119 $optpage_blocks[] = array(
120 'name' => _("SpamCop - Spam Reporting"),
121 'url' => '../plugins/spamcop/options.php',
122 'desc' => _("Help fight the battle against unsolicited email. SpamCop reads the spam email and determines the correct addresses to send complaints to. Quite fast, really smart, and easy to use."),
123 'js' => false
124 );
125}
126
127/**
128 * Process messages that are submitted by email.
129 *
130 * Delete spam if user wants to delete it. Don't save submitted emails.
131 * Implement overrides that fix compose.php behavior.
132 * @since 1.5.1
133 * @access private
134 */
135function spamcop_while_sending_function() {
136 global $mailbox, $spamcop_delete, $spamcop_save, $spamcop_is_composing, $auto_expunge,
045ec1a1 137 $username, $imapServerAddress, $imapPort;
da297dd0 138
139 if (sqgetGlobalVar('spamcop_is_composing' , $spamcop_is_composing)) {
140 // delete spam message
141 if ($spamcop_delete) {
045ec1a1 142 $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
da297dd0 143 sqimap_mailbox_select($imapConnection, $mailbox);
144 sqimap_msgs_list_delete($imapConnection, $mailbox, array($spamcop_is_composing));
145 if ($auto_expunge)
146 sqimap_mailbox_expunge($imapConnection, $mailbox, true);
147 }
148 if (! $spamcop_save) {
149 // disable use of send folder.
150 // Temporally override in order to disable saving of 'reply anyway' messages.
151 global $default_move_to_sent;
152 $default_move_to_sent=false;
153 }
154 // change default email composition setting. Plugin always operates in right frame.
155 // make sure that compose.php redirects to right page. Temporally override.
156 global $compose_new_win;
157 $compose_new_win = false;
158 }
159}
160
161/**
162 * Internal spamcop plugin function.
163 *
164 * It is used to display similar action links.
165 * @access private
166 */
167function spamcop_enable_disable($option,$disable_action,$enable_action) {
168 if ($option) {
169 $ret= _("Enabled") . " (<a href=\"options.php?action=$disable_action\">" . _("Disable it") . "</a>)\n";
170 } else {
171 $ret = _("Disabled") . " (<a href=\"options.php?action=$enable_action\">" . _("Enable it") . "</a>)\n";
172 }
173 return $ret;
174}
175
176/**
177 * Stores message in attachment directory, when email based reports are used
178 * @access private
2b42190d 179 * @todo Duplicate code in src/compose.php
da297dd0 180 */
2b42190d 181function spamcop_getMessage_RFC822_Attachment($message, $composeMessage, $passed_id,
da297dd0 182 $passed_ent_id='', $imapConnection) {
d887e067 183
184 global $username, $attachment_dir;
da297dd0 185
da297dd0 186 if (!$passed_ent_id) {
187 $body_a = sqimap_run_command($imapConnection,
188 'FETCH '.$passed_id.' RFC822',
189 TRUE, $response, $readmessage,
190 TRUE);
191 } else {
192 $body_a = sqimap_run_command($imapConnection,
193 'FETCH '.$passed_id.' BODY['.$passed_ent_id.']',
194 TRUE, $response, $readmessage,TRUE);
195 $message = $message->parent;
196 }
197 if ($response == 'OK') {
198 array_shift($body_a);
199 $body = implode('', $body_a) . "\r\n";
200
628bce99 201 $filename = sq_get_attach_tempfile();
d887e067 202 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
203
204 $fp = fopen("$hashed_attachment_dir/$filename", 'wb');
da297dd0 205 fwrite ($fp, $body);
206 fclose($fp);
da297dd0 207 $composeMessage->initAttachment('message/rfc822','email.txt',
628bce99 208 $filename);
da297dd0 209 }
d887e067 210
da297dd0 211 return $composeMessage;
212}