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