removing trailing ?> from function scripts
[squirrelmail.git] / plugins / spamcop / functions.php
1 <?php
2
3 /**
4 * SpamCop plugin - functions
5 *
6 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
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 */
20 global $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 */
30 function 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 */
62 function spamcop_show_link_function() {
63 global $spamcop_enabled, $spamcop_method, $spamcop_quick_report,$javascript_on;
64
65 if (! $spamcop_enabled)
66 return;
67
68 /* GLOBALS */
69 sqgetGlobalVar('passed_id', $passed_id, SQ_FORM);
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
82 echo "<br />\n";
83
84 /*
85 Catch situation when user uses quick_email and does not update
86 preferences. User gets web_form link. If prefs are set to
87 quick_email format - they will be updated after clicking the link
88 */
89 if (! $spamcop_quick_report && $spamcop_method=='quick_email') {
90 $spamcop_method = 'web_form';
91 }
92
93 // Javascript is used only in web based reporting
94 // don't insert javascript if javascript is disabled
95 if ($spamcop_method == 'web_form' && $javascript_on) {
96 ?><script type="text/javascript">
97 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">');
98 document.write("<?php echo _("Report as Spam"); ?>");
99 document.write("</a>");
100 </script><?php
101 } else {
102 ?><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); ?>">
103 <?php echo _("Report as Spam"); ?></a>
104 <?php
105 }
106 }
107
108 /**
109 * Add spamcop option block (internal function)
110 * @since 1.5.1
111 * @access private
112 */
113 function spamcop_options_function() {
114 global $optpage_blocks;
115
116 $optpage_blocks[] = array(
117 'name' => _("SpamCop - Spam Reporting"),
118 'url' => '../plugins/spamcop/options.php',
119 '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."),
120 'js' => false
121 );
122 }
123
124 /**
125 * Process messages that are submitted by email.
126 *
127 * Delete spam if user wants to delete it. Don't save submitted emails.
128 * Implement overrides that fix compose.php behavior.
129 * @since 1.5.1
130 * @access private
131 */
132 function spamcop_while_sending_function() {
133 global $mailbox, $spamcop_delete, $spamcop_save, $spamcop_is_composing, $auto_expunge,
134 $username, $imapServerAddress, $imapPort;
135
136 if (sqgetGlobalVar('spamcop_is_composing' , $spamcop_is_composing)) {
137 // delete spam message
138 if ($spamcop_delete) {
139 $imapConnection = sqimap_login($username, false, $imapServerAddress, $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 * @todo Duplicate code in src/compose.php
177 */
178 function spamcop_getMessage_RFC822_Attachment($message, $composeMessage, $passed_id,
179 $passed_ent_id='', $imapConnection) {
180 global $attachment_dir, $username;
181
182 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
183 if (!$passed_ent_id) {
184 $body_a = sqimap_run_command($imapConnection,
185 'FETCH '.$passed_id.' RFC822',
186 TRUE, $response, $readmessage,
187 TRUE);
188 } else {
189 $body_a = sqimap_run_command($imapConnection,
190 'FETCH '.$passed_id.' BODY['.$passed_ent_id.']',
191 TRUE, $response, $readmessage,TRUE);
192 $message = $message->parent;
193 }
194 if ($response == 'OK') {
195 array_shift($body_a);
196 $body = implode('', $body_a) . "\r\n";
197
198 $localfilename = GenerateRandomString(32, 'FILE', 7);
199 $full_localfilename = "$hashed_attachment_dir/$localfilename";
200 $fp = fopen( $full_localfilename, 'w');
201 fwrite ($fp, $body);
202 fclose($fp);
203
204 /* dirty relative dir fix */
205 if (substr($attachment_dir,0,3) == '../') {
206 $attachment_dir = substr($attachment_dir,3);
207 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
208 }
209 $full_localfilename = "$hashed_attachment_dir/$localfilename";
210
211 $composeMessage->initAttachment('message/rfc822','email.txt',
212 $full_localfilename);
213 }
214 return $composeMessage;
215 }