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