Official Mail_Fetch version
[squirrelmail.git] / plugins / mail_fetch / setup.php
CommitLineData
d622d38a 1<?php
2
3 require_once( '../plugins/mail_fetch/functions.php' );
4
5 function squirrelmail_plugin_init_mail_fetch() {
6 global $squirrelmail_plugin_hooks;
7 global $mailbox, $imap_stream, $imapConnection;
8
9 $squirrelmail_plugin_hooks["menuline"]["mail_fetch"] = 'mail_fetch_link';
10 $squirrelmail_plugin_hooks["options_save"]["mail_fetch"] = "mail_fetch_save_pref";
11 $squirrelmail_plugin_hooks["loading_prefs"]["mail_fetch"] = "mail_fetch_load_pref";
12 $squirrelmail_plugin_hooks["login_verified"]["mail_fetch"] = "mail_fetch_setnew";
13 $squirrelmail_plugin_hooks["left_main_before"]["mail_fetch"] = "mail_fetch_login";
14 $squirrelmail_plugin_hooks['optpage_register_block']['mail_fetch'] = 'mailfetch_optpage_register_block';
15
16 }
17
18 function mail_fetch_link() {
19
20 displayInternalLink('plugins/mail_fetch/fetch.php', _("Fetch"), '');
21 echo '&nbsp;&nbsp;';
22
23 }
24
25 function mail_fetch_load_pref() {
26
27 global $username,$data_dir;
28 global $mailfetch_server_number;
29 global $mailfetch_cypher;
30 global $mailfetch_server_,$mailfetch_alias_,$mailfetch_user_,$mailfetch_pass_;
31 global $mailfetch_lmos_, $mailfetch_uidl_, $mailfetch_login_, $mailfetch_fref_;
32 global $PHP_SELF;
33
34 if( stristr( $PHP_SELF, 'mail_fetch' ) ) {
35 $mailfetch_server_number = getPref($data_dir, $username, "mailfetch_server_number");
36 if (!isset($mailfetch_server_number)) $mailfetch_server_number=0;
37 $mailfetch_cypher = getPref($data_dir, $username, "mailfetch_cypher");
38 if ($mailfetch_server_number<1) $mailfetch_server_number=0;
39 for ($i=0;$i<$mailfetch_server_number;$i++) {
40 $mailfetch_server_[$i] = getPref($data_dir, $username, "mailfetch_server_$i");
41 $mailfetch_alias_[$i] = getPref($data_dir, $username, "mailfetch_alias_$i");
42 $mailfetch_user_[$i] = getPref($data_dir, $username, "mailfetch_user_$i");
43 $mailfetch_pass_[$i] = getPref($data_dir, $username, "mailfetch_pass_$i");
44 $mailfetch_lmos_[$i] = getPref($data_dir, $username, "mailfetch_lmos_$i");
45 $mailfetch_login_[$i] = getPref($data_dir, $username, "mailfetch_login_$i");
46 $mailfetch_fref_[$i] = getPref($data_dir, $username, "mailfetch_fref_$i");
47 $mailfetch_uidl_[$i] = getPref($data_dir, $username, "mailfetch_uidl_$i");
48 if( $mailfetch_cypher == 'on' ) $mailfetch_pass_[$i] = decrypt( $mailfetch_pass_[$i] );
49 }
50 }
51 }
52
53 function mail_fetch_save_pref() {
54 global $username,$data_dir;
55 global $mf_server, $mf_user, $mf_pass, $mf_lmos, $mf_alias;
56 global $mf_cypher;
57 global $mf_uidl;
58 global $mf_login, $mf_fref, $submit_mailfetch;
59 global $mf_action, $mf_sn, $mf_subfolder;
60
61 if (isset($submit_mailfetch)) {
62 if ($mf_action=="add") {
63 //add new server
64 if ($mf_sn<1) $mf_sn=0;
65 if (!isset($mf_server)) return;
66 setPref($data_dir,$username,"mailfetch_server_$mf_sn", (isset($mf_server)?$mf_server:""));
67 setPref($data_dir,$username,"mailfetch_alias_$mf_sn", (isset($mf_alias)?$mf_alias:""));
68 setPref($data_dir,$username,"mailfetch_user_$mf_sn",(isset($mf_user)?$mf_user:""));
69 setPref($data_dir,$username,"mailfetch_pass_$mf_sn",(isset($mf_pass)?encrypt( $mf_pass ) :""));
70 if( $mf_cypher <> 'on' ) SetPref($data_dir,$username,"mailfetch_cypher", 'on');
71 setPref($data_dir,$username,"mailfetch_lmos_$mf_sn",(isset($mf_lmos)?$mf_lmos:""));
72 setPref($data_dir,$username,"mailfetch_login_$mf_sn",(isset($mf_login)?$mf_login:""));
73 setPref($data_dir,$username,"mailfetch_fref_$mf_sn",(isset($mf_fref)?$mf_fref:""));
74 setPref($data_dir,$username,"mailfetch_subfolder_$mf_sn",(isset($mf_subfolder)?$mf_subfolder:""));
75 $mf_sn++;
76 setPref($data_dir,$username,"mailfetch_server_number", $mf_sn);
77 } elseif ($mf_action=="confirm_modify") {
78 //modify a server
79 if (!isset($mf_server)) return;
80 setPref($data_dir,$username,"mailfetch_server_$mf_sn", (isset($mf_server)?$mf_server:""));
81 setPref($data_dir,$username,"mailfetch_alias_$mf_sn", (isset($mf_alias)?$mf_alias:""));
82 setPref($data_dir,$username,"mailfetch_user_$mf_sn",(isset($mf_user)?$mf_user:""));
83 setPref($data_dir,$username,"mailfetch_pass_$mf_sn",(isset($mf_pass)?encrypt( $mf_pass ) :""));
84 if( $mf_cypher <> 'on' ) setPref($data_dir,$username,"mailfetch_cypher", 'on');
85 setPref($data_dir,$username,"mailfetch_lmos_$mf_sn",(isset($mf_lmos)?$mf_lmos:""));
86 setPref($data_dir,$username,"mailfetch_login_$mf_sn",(isset($mf_login)?$mf_login:""));
87 setPref($data_dir,$username,"mailfetch_fref_$mf_sn",(isset($mf_fref)?$mf_fref:""));
88 setPref($data_dir,$username,"mailfetch_subfolder_$mf_sn",(isset($mf_subfolder)?$mf_subfolder:""));
89 } elseif ($mf_action=="confirm_delete") {
90 //delete a server
91 $mailfetch_server_number = getPref($data_dir, $username, "mailfetch_server_number");
92 if ($mf_sn+1==$mailfetch_server_number) {
93 //is the last server, whe can only decrase $mailfetch_server_number
94 $mailfetch_server_number--;
95 setPref($data_dir,$username,"mailfetch_server_number", $mailfetch_server_number);
96 } else {
97 //if not the last, all the sequel server come up one step
98 //then whe decrase $mailfetch_server_number
99 $mailfetch_server_number--;
100 for ($i=$mf_sn;$i<$mailfetch_server_number;$i++) {
101 $tmp=$i+1;
102 setPref($data_dir,$username,"mailfetch_server_$mf_sn", getPref($data_dir, $username, "mailfetch_server_$tmp"));
103 setPref($data_dir,$username,"mailfetch_alias_$mf_sn", getPref($data_dir, $username, "mailfetch_alias_$tmp"));
104 setPref($data_dir,$username,"mailfetch_user_$mf_sn", getPref($data_dir, $username, "mailfetch_user_$tmp"));
105 setPref($data_dir,$username,"mailfetch_pass_$mf_sn",(isset($mf_pass)?encrypt( $mf_pass ) :""));
106 // if( $mf_cypher <> 'on' ) setPref($data_dir,$username,"mailfetch_cypher", 'on');
107 setPref($data_dir,$username,"mailfetch_lmos_$mf_sn", getPref($data_dir, $username, "mailfetch_lmos_$tmp"));
108 setPref($data_dir,$username,"mailfetch_login_$mf_sn", getPref($data_dir, $username, "mailfetch_login_$tmp"));
109 setPref($data_dir,$username,"mailfetch_fref_$mf_sn", getPref($data_dir, $username, "mailfetch_fref_$tmp"));
110 setPref($data_dir,$username,"mailfetch_subfolder_$mf_sn", getPref($data_dir, $username, "mailfetch_subfolder_$tmp"));
111 }
112 setPref($data_dir,$username,"mailfetch_server_number", $mailfetch_server_number);
113 }
114 }
115 }
116 }
117
118 function mail_fetch_login() {
119
120 require_once ('../src/validate.php');
121 require_once ('../functions/imap.php');
122 require_once ('../plugins/mail_fetch/class.POP3.php');
123 require_once ('../plugins/mail_fetch/functions.php');
124 require_once('../functions/i18n.php');
125
126 global $username, $data_dir, $key,$imapServerAddress,$imapPort;
127
128 $mailfetch_newlog = getPref($data_dir, $username, 'mailfetch_newlog');
129
130 $outMsg = '';
131
132 $mailfetch_server_number = getPref($data_dir, $username, "mailfetch_server_number");
133 if (!isset($mailfetch_server_number)) $mailfetch_server_number=0;
134 $mailfetch_cypher = getPref($data_dir, $username, "mailfetch_cypher");
135 if ($mailfetch_server_number<1) $mailfetch_server_number=0;
136
137 for ($i_loop=0;$i_loop<$mailfetch_server_number;$i_loop++) {
138
139 $mailfetch_login_[$i_loop] = getPref($data_dir, $username, "mailfetch_login_$i_loop");
140 $mailfetch_fref_[$i_loop] = getPref($data_dir, $username, "mailfetch_fref_$i_loop");
141 $mailfetch_pass_[$i_loop] = getPref($data_dir, $username, "mailfetch_pass_$i_loop");
142 if( $mailfetch_cypher == 'on' )
143 $mailfetch_pass_[$i_loop] = decrypt( $mailfetch_pass_[$i_loop] );
144
145 if( $mailfetch_pass_[$i_loop] <> '' && // Empty passwords no allowed
146 ( ( $mailfetch_login_[$i_loop] == 'on' && $mailfetch_newlog == 'on' ) || $mailfetch_fref_[$i_loop] == 'on' ) ) {
147
148 $mailfetch_server_[$i_loop] = getPref($data_dir, $username, "mailfetch_server_$i_loop");
149 $mailfetch_alias_[$i_loop] = getPref($data_dir, $username, "mailfetch_alias_$i_loop");
150 $mailfetch_user_[$i_loop] = getPref($data_dir, $username, "mailfetch_user_$i_loop");
151 $mailfetch_lmos_[$i_loop] = getPref($data_dir, $username, "mailfetch_lmos_$i_loop");
152 $mailfetch_uidl_[$i_loop] = getPref($data_dir, $username, "mailfetch_uidl_$i_loop");
153 $mailfetch_subfolder_[$i_loop] = getPref($data_dir, $username, "mailfetch_subfolder_$i_loop");
154
155 $mailfetch_server=$mailfetch_server_[$i_loop];
156 $mailfetch_user=$mailfetch_user_[$i_loop];
157 $mailfetch_alias=$mailfetch_alias_[$i_loop];
158 $mailfetch_pass=$mailfetch_pass_[$i_loop];
159 $mailfetch_lmos=$mailfetch_lmos_[$i_loop];
160 $mailfetch_login=$mailfetch_login_[$i_loop];
161 $mailfetch_uidl=$mailfetch_uidl_[$i_loop];
162 $mailfetch_subfolder=$mailfetch_subfolder_[$i_loop];
163
164 // $outMsg .= "$mailfetch_alias checked<br>";
165
166 // $outMsg .= "$mailfetch_alias_[$i_loop]<br>";
167
168 $pop3 = new POP3($mailfetch_server, 60);
169
170 if (!$pop3->connect($mailfetch_server)) {
171 $outMsg .= _("Warning, ") . $pop3->ERROR;
172 continue;
173 }
174
175 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
176
177 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
178 if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
179 $outMsg .= _("Login Failed:") . $pop3->ERROR;
180 continue;
181 }
182
183 // register_shutdown_function($pop3->quit());
184
185 $msglist = $pop3->uidl();
186
187 $i = 1;
188 for ($j = 1; $j < sizeof($msglist); $j++) {
189 if ($msglist["$j"] == $mailfetch_uidl) {
190 $i = $j+1;
191 break;
192 }
193 }
194
195 if ($Count < $i) {
196 $pop3->quit();
197 continue;
198 }
199 if ($Count == 0) {
200 $pop3->quit();
201 continue;
202 } else {
203 $newmsgcount = $Count - $i + 1;
204 }
205
206 // Faster to get them all at once
207 $mailfetch_uidl = $pop3->uidl();
208
209 if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
210 $outMsg .= _("Server does not support UIDL.");
211
212 for (; $i <= $Count; $i++) {
213 set_time_limit(20); // 20 seconds per message max
214 $Message = "";
215 $MessArray = $pop3->get($i);
216
217 if ( (!$MessArray) or (gettype($MessArray) != "array")) {
218 $outMsg .= _("Warning, ") . $pop3->ERROR;
219 continue 2;
220 }
221
222 while (list($lineNum, $line) = each ($MessArray)) {
223 $Message .= $line;
224 }
225
226 if ($mailfetch_subfolder=="") {
227 fputs($imap_stream, "A3$i APPEND INBOX {" . (strlen($Message) - 1) . "}\r\n");
228 } else {
229 fputs($imap_stream, "A3$i APPEND $mailfetch_subfolder {" . (strlen($Message) - 1) . "}\r\n");
230 }
231 $Line = fgets($imap_stream, 1024);
232 if (substr($Line, 0, 1) == '+') {
233 fputs($imap_stream, $Message);
234 sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
235
236 if ($mailfetch_lmos != 'on') {
237 $pop3->delete($i);
238 }
239 } else {
240 echo "$Line";
241 $outMsg .= _("Error Appending Message!");
242 }
243 }
244
245 $pop3->quit();
246 sqimap_logout($imap_stream);
247 if (is_array($mailfetch_uidl)) {
248 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
249 }
250 }
251 }
252
253 if( trim( $outMsg ) <> '' )
254 echo '<br><font size=1>' . _("Mail Fetch Result:") . "<br>$outMsg</font>";
255
256 if( $mailfetch_newlog == 'on' )
257 setPref($data_dir,$username,"mailfetch_newlog", 'off');
258
259 }
260
261 function mail_fetch_setnew() {
262
263 global $data_dir,$username;
264 // require_once ('../src/load_prefs.php');
265 // require_once ('../src/validate.php');
266 require_once('../functions/prefs.php');
267
268 if( $username <> '' ) {
269 // Creates the pref file if it does not exist.
270 checkForPrefs( $data_dir, $username );
271 setPref( $data_dir, $username, 'mailfetch_newlog', 'on' );
272 }
273
274 }
275
276 function mailfetch_optpage_register_block() {
277 global $optpage_blocks;
278
279 $optpage_blocks[] = array(
280 'name' => _("Simple POP3 Fetch Mail"),
281 'url' => '../plugins/mail_fetch/options.php',
282 'desc' => _("This configures settings for downloading email from a pop3 mailbox to your account on this server."),
283 'js' => false
284 );
285 }
286
287?>