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