Clean up spamcop plugin wrt globals.
[squirrelmail.git] / plugins / mail_fetch / setup.php
CommitLineData
d622d38a 1<?php
2
d3c89357 3 /**
4 ** mail_fetch/setup.php
5 **
76911253 6 ** Copyright (c) 1999-2003 The SquirrelMail Project Team
d3c89357 7 ** Licensed under the GNU GPL. For full terms see the file COPYING.
8 **
9 ** Setup of the mailfetch plugin.
10 **
11 ** $Id$
12 **/
13
aa7fb30c 14 require_once(SM_PATH . 'plugins/mail_fetch/functions.php' );
d622d38a 15
16 function squirrelmail_plugin_init_mail_fetch() {
17 global $squirrelmail_plugin_hooks;
d622d38a 18
d3c89357 19 $squirrelmail_plugin_hooks['menuline']['mail_fetch'] = 'mail_fetch_link';
20 $squirrelmail_plugin_hooks['loading_prefs']['mail_fetch'] = 'mail_fetch_load_pref';
21 $squirrelmail_plugin_hooks['login_verified']['mail_fetch'] = 'mail_fetch_setnew';
22 $squirrelmail_plugin_hooks['left_main_before']['mail_fetch'] = 'mail_fetch_login';
d622d38a 23 $squirrelmail_plugin_hooks['optpage_register_block']['mail_fetch'] = 'mailfetch_optpage_register_block';
24
25 }
26
27 function mail_fetch_link() {
28
29 displayInternalLink('plugins/mail_fetch/fetch.php', _("Fetch"), '');
30 echo '&nbsp;&nbsp;';
31
32 }
33
34 function mail_fetch_load_pref() {
35
86bb8549 36 global $data_dir;
d622d38a 37 global $mailfetch_server_number;
38 global $mailfetch_cypher;
39 global $mailfetch_server_,$mailfetch_alias_,$mailfetch_user_,$mailfetch_pass_;
40 global $mailfetch_lmos_, $mailfetch_uidl_, $mailfetch_login_, $mailfetch_fref_;
41 global $PHP_SELF;
fb694681 42 if ( !check_php_version(4,1) ) {
86bb8549 43 global $_SESSION;
44 }
45 $username = $_SESSION['username'];
d622d38a 46
d3c89357 47 if( stristr( $PHP_SELF, 'mail_fetch' ) ) {
48 $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number', 0);
49 $mailfetch_cypher = getPref($data_dir, $username, 'mailfetch_cypher', 'on' );
d622d38a 50 if ($mailfetch_server_number<1) $mailfetch_server_number=0;
51 for ($i=0;$i<$mailfetch_server_number;$i++) {
52 $mailfetch_server_[$i] = getPref($data_dir, $username, "mailfetch_server_$i");
e42b0dd2 53 $mailfetch_port_[$i] = getPref($data_dir, $username, "mailfetch_port_$i");
d622d38a 54 $mailfetch_alias_[$i] = getPref($data_dir, $username, "mailfetch_alias_$i");
55 $mailfetch_user_[$i] = getPref($data_dir, $username, "mailfetch_user_$i");
56 $mailfetch_pass_[$i] = getPref($data_dir, $username, "mailfetch_pass_$i");
57 $mailfetch_lmos_[$i] = getPref($data_dir, $username, "mailfetch_lmos_$i");
58 $mailfetch_login_[$i] = getPref($data_dir, $username, "mailfetch_login_$i");
59 $mailfetch_fref_[$i] = getPref($data_dir, $username, "mailfetch_fref_$i");
60 $mailfetch_uidl_[$i] = getPref($data_dir, $username, "mailfetch_uidl_$i");
61 if( $mailfetch_cypher == 'on' ) $mailfetch_pass_[$i] = decrypt( $mailfetch_pass_[$i] );
62 }
63 }
64 }
65
d622d38a 66 function mail_fetch_login() {
67
97ffe963 68 require_once (SM_PATH . 'include/validate.php');
69 require_once (SM_PATH . 'functions/imap.php');
70 require_once (SM_PATH . 'plugins/mail_fetch/class.POP3.php');
71 require_once (SM_PATH . 'plugins/mail_fetch/functions.php');
d622d38a 72
86bb8549 73 global $data_dir, $imapServerAddress, $imapPort;
fb694681 74 if ( !check_php_version(4,1) ) {
86bb8549 75 global $_SESSION, $_COOKIE;
76 }
77 $username = $_SESSION['username'];
78 $key = $_COOKIE['key'];
d622d38a 79
80 $mailfetch_newlog = getPref($data_dir, $username, 'mailfetch_newlog');
81
82 $outMsg = '';
83
d3c89357 84 $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number');
d622d38a 85 if (!isset($mailfetch_server_number)) $mailfetch_server_number=0;
d3c89357 86 $mailfetch_cypher = getPref($data_dir, $username, 'mailfetch_cypher');
d622d38a 87 if ($mailfetch_server_number<1) $mailfetch_server_number=0;
88
89 for ($i_loop=0;$i_loop<$mailfetch_server_number;$i_loop++) {
90
91 $mailfetch_login_[$i_loop] = getPref($data_dir, $username, "mailfetch_login_$i_loop");
92 $mailfetch_fref_[$i_loop] = getPref($data_dir, $username, "mailfetch_fref_$i_loop");
93 $mailfetch_pass_[$i_loop] = getPref($data_dir, $username, "mailfetch_pass_$i_loop");
94 if( $mailfetch_cypher == 'on' )
95 $mailfetch_pass_[$i_loop] = decrypt( $mailfetch_pass_[$i_loop] );
96
97 if( $mailfetch_pass_[$i_loop] <> '' && // Empty passwords no allowed
98 ( ( $mailfetch_login_[$i_loop] == 'on' && $mailfetch_newlog == 'on' ) || $mailfetch_fref_[$i_loop] == 'on' ) ) {
99
100 $mailfetch_server_[$i_loop] = getPref($data_dir, $username, "mailfetch_server_$i_loop");
e42b0dd2 101 $mailfetch_port_[$i_loop] = getPref($data_dir, $username , "mailfetch_port_$i_loop");
d622d38a 102 $mailfetch_alias_[$i_loop] = getPref($data_dir, $username, "mailfetch_alias_$i_loop");
103 $mailfetch_user_[$i_loop] = getPref($data_dir, $username, "mailfetch_user_$i_loop");
104 $mailfetch_lmos_[$i_loop] = getPref($data_dir, $username, "mailfetch_lmos_$i_loop");
105 $mailfetch_uidl_[$i_loop] = getPref($data_dir, $username, "mailfetch_uidl_$i_loop");
106 $mailfetch_subfolder_[$i_loop] = getPref($data_dir, $username, "mailfetch_subfolder_$i_loop");
107
108 $mailfetch_server=$mailfetch_server_[$i_loop];
e42b0dd2 109 $mailfetch_port=$mailfetch_port_[$i_loop];
d622d38a 110 $mailfetch_user=$mailfetch_user_[$i_loop];
111 $mailfetch_alias=$mailfetch_alias_[$i_loop];
112 $mailfetch_pass=$mailfetch_pass_[$i_loop];
113 $mailfetch_lmos=$mailfetch_lmos_[$i_loop];
114 $mailfetch_login=$mailfetch_login_[$i_loop];
115 $mailfetch_uidl=$mailfetch_uidl_[$i_loop];
116 $mailfetch_subfolder=$mailfetch_subfolder_[$i_loop];
117
118 // $outMsg .= "$mailfetch_alias checked<br>";
119
120 // $outMsg .= "$mailfetch_alias_[$i_loop]<br>";
121
122 $pop3 = new POP3($mailfetch_server, 60);
123
e42b0dd2 124 if (!$pop3->connect($mailfetch_server,$mailfetch_port)) {
d622d38a 125 $outMsg .= _("Warning, ") . $pop3->ERROR;
126 continue;
127 }
128
129 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
130
131 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
132 if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
133 $outMsg .= _("Login Failed:") . $pop3->ERROR;
134 continue;
135 }
136
137 // register_shutdown_function($pop3->quit());
138
139 $msglist = $pop3->uidl();
140
141 $i = 1;
142 for ($j = 1; $j < sizeof($msglist); $j++) {
143 if ($msglist["$j"] == $mailfetch_uidl) {
144 $i = $j+1;
145 break;
146 }
147 }
148
149 if ($Count < $i) {
150 $pop3->quit();
151 continue;
152 }
153 if ($Count == 0) {
154 $pop3->quit();
155 continue;
156 } else {
157 $newmsgcount = $Count - $i + 1;
158 }
159
160 // Faster to get them all at once
161 $mailfetch_uidl = $pop3->uidl();
162
163 if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
164 $outMsg .= _("Server does not support UIDL.");
165
166 for (; $i <= $Count; $i++) {
167 set_time_limit(20); // 20 seconds per message max
168 $Message = "";
169 $MessArray = $pop3->get($i);
170
171 if ( (!$MessArray) or (gettype($MessArray) != "array")) {
172 $outMsg .= _("Warning, ") . $pop3->ERROR;
173 continue 2;
174 }
175
176 while (list($lineNum, $line) = each ($MessArray)) {
177 $Message .= $line;
178 }
179
180 if ($mailfetch_subfolder=="") {
181 fputs($imap_stream, "A3$i APPEND INBOX {" . (strlen($Message) - 1) . "}\r\n");
182 } else {
183 fputs($imap_stream, "A3$i APPEND $mailfetch_subfolder {" . (strlen($Message) - 1) . "}\r\n");
184 }
185 $Line = fgets($imap_stream, 1024);
186 if (substr($Line, 0, 1) == '+') {
187 fputs($imap_stream, $Message);
188 sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
189
190 if ($mailfetch_lmos != 'on') {
191 $pop3->delete($i);
192 }
193 } else {
194 echo "$Line";
195 $outMsg .= _("Error Appending Message!");
196 }
197 }
198
199 $pop3->quit();
200 sqimap_logout($imap_stream);
201 if (is_array($mailfetch_uidl)) {
202 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
203 }
204 }
205 }
206
207 if( trim( $outMsg ) <> '' )
208 echo '<br><font size=1>' . _("Mail Fetch Result:") . "<br>$outMsg</font>";
209
210 if( $mailfetch_newlog == 'on' )
211 setPref($data_dir,$username,"mailfetch_newlog", 'off');
212
213 }
214
215 function mail_fetch_setnew() {
216
86bb8549 217 global $data_dir;
aa7fb30c 218 require_once(SM_PATH . 'functions/prefs.php');
d622d38a 219
86bb8549 220 if (isset($_SESSION['username'])) {
221 $username = $_SESSION['username'];
222 } else {
223 $username = '';
224 }
225
d622d38a 226 if( $username <> '' ) {
227 // Creates the pref file if it does not exist.
d622d38a 228 setPref( $data_dir, $username, 'mailfetch_newlog', 'on' );
229 }
230
231 }
232
233 function mailfetch_optpage_register_block() {
234 global $optpage_blocks;
235
236 $optpage_blocks[] = array(
237 'name' => _("Simple POP3 Fetch Mail"),
238 'url' => '../plugins/mail_fetch/options.php',
239 'desc' => _("This configures settings for downloading email from a pop3 mailbox to your account on this server."),
240 'js' => false
241 );
242 }
243
165829a3 244?>