Removal of unused commented code blocks
[squirrelmail.git] / plugins / mail_fetch / setup.php
CommitLineData
d622d38a 1<?php
2
4f51df66 3/**
4 * mail_fetch/setup.php
5 *
6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Setup of the mailfetch plugin.
10 *
11 * @version $Id$
12 * @package plugins
13 * @subpackage mail_fetch
14 */
15
16/** */
17require_once(SM_PATH . 'plugins/mail_fetch/functions.php' );
18
19/**
20 * Initialize the plugin
21 */
22function squirrelmail_plugin_init_mail_fetch() {
23 global $squirrelmail_plugin_hooks;
24
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';
29 $squirrelmail_plugin_hooks['optpage_register_block']['mail_fetch'] = 'mailfetch_optpage_register_block';
5f438206 30 $squirrelmail_plugin_hooks['rename_or_delete_folder']['mail_fetch'] = 'mail_fetch_folderact';
4f51df66 31}
32
33function mail_fetch_link() {
34
35 displayInternalLink('plugins/mail_fetch/fetch.php', _("Fetch"), '');
36 echo '&nbsp;&nbsp;';
37
38}
39
40function mail_fetch_load_pref() {
41
42 global $data_dir;
43 global $mailfetch_server_number;
44 global $mailfetch_cypher, $mailfetch_port_;
45 global $mailfetch_server_,$mailfetch_alias_,$mailfetch_user_,$mailfetch_pass_;
46 global $mailfetch_lmos_, $mailfetch_uidl_, $mailfetch_login_, $mailfetch_fref_;
47 global $PHP_SELF;
48
49 sqgetGlobalVar('username', $username, SQ_SESSION);
50
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' );
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");
57 $mailfetch_port_[$i] = getPref($data_dir, $username, "mailfetch_port_$i");
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] );
d622d38a 66 }
67 }
4f51df66 68}
d622d38a 69
4f51df66 70function mail_fetch_login() {
d622d38a 71
4f51df66 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
4f51df66 77 global $data_dir, $imapServerAddress, $imapPort;
91e0dccc 78
4f51df66 79 sqgetGlobalVar('username', $username, SQ_SESSION);
80 sqgetGlobalVar('key', $key, SQ_COOKIE);
d622d38a 81
4f51df66 82 $mailfetch_newlog = getPref($data_dir, $username, 'mailfetch_newlog');
d622d38a 83
4f51df66 84 $outMsg = '';
d622d38a 85
4f51df66 86 $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number');
87 if (!isset($mailfetch_server_number)) $mailfetch_server_number=0;
88 $mailfetch_cypher = getPref($data_dir, $username, 'mailfetch_cypher');
89 if ($mailfetch_server_number<1) $mailfetch_server_number=0;
d622d38a 90
4f51df66 91 for ($i_loop=0;$i_loop<$mailfetch_server_number;$i_loop++) {
d622d38a 92
4f51df66 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] );
d622d38a 98
4f51df66 99 if( $mailfetch_pass_[$i_loop] <> '' && // Empty passwords no allowed
d622d38a 100 ( ( $mailfetch_login_[$i_loop] == 'on' && $mailfetch_newlog == 'on' ) || $mailfetch_fref_[$i_loop] == 'on' ) ) {
101
4f51df66 102 $mailfetch_server_[$i_loop] = getPref($data_dir, $username, "mailfetch_server_$i_loop");
103 $mailfetch_port_[$i_loop] = getPref($data_dir, $username , "mailfetch_port_$i_loop");
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];
111 $mailfetch_port=$mailfetch_port_[$i_loop];
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
126 if (!$pop3->connect($mailfetch_server,$mailfetch_port)) {
127 $outMsg .= _("Warning, ") . $pop3->ERROR;
128 continue;
129 }
d622d38a 130
4f51df66 131 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
d622d38a 132
4f51df66 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 }
d622d38a 138
4f51df66 139 // register_shutdown_function($pop3->quit());
d622d38a 140
4f51df66 141 $msglist = $pop3->uidl();
d622d38a 142
4f51df66 143 $i = 1;
144 for ($j = 1; $j < sizeof($msglist); $j++) {
145 if ($msglist["$j"] == $mailfetch_uidl) {
146 $i = $j+1;
147 break;
d622d38a 148 }
4f51df66 149 }
d622d38a 150
4f51df66 151 if ($Count < $i) {
152 $pop3->quit();
153 continue;
154 }
155 if ($Count == 0) {
156 $pop3->quit();
157 continue;
158 }
d622d38a 159
4f51df66 160 // Faster to get them all at once
161 $mailfetch_uidl = $pop3->uidl();
d622d38a 162
4f51df66 163 if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
164 $outMsg .= _("Server does not support UIDL.");
d622d38a 165
4f51df66 166 for (; $i <= $Count; $i++) {
167 if (!ini_get('safe_mode'))
168 set_time_limit(20); // 20 seconds per message max
169 $Message = "";
170 $MessArray = $pop3->get($i);
d622d38a 171
4f51df66 172 if ( (!$MessArray) or (gettype($MessArray) != "array")) {
173 $outMsg .= _("Warning, ") . $pop3->ERROR;
174 continue 2;
d622d38a 175 }
176
4f51df66 177 while (list($lineNum, $line) = each ($MessArray)) {
178 $Message .= $line;
d622d38a 179 }
f8a1ed5a 180
5f438206 181 // check if mail folder is not null and subscribed (There is possible issue with /noselect mail folders)
f8a1ed5a 182 if ($mailfetch_subfolder=='' ||
5f438206 183 ! mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) {
4f51df66 184 fputs($imap_stream, "A3$i APPEND INBOX {" . strlen($Message) . "}\r\n");
185 } else {
186 fputs($imap_stream, "A3$i APPEND $mailfetch_subfolder {" . strlen($Message) . "}\r\n");
187 }
188 $Line = fgets($imap_stream, 1024);
189 if (substr($Line, 0, 1) == '+') {
190 fputs($imap_stream, $Message);
191 fputs($imap_stream, "\r\n");
192 sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
d622d38a 193
4f51df66 194 if ($mailfetch_lmos != 'on') {
d622d38a 195 $pop3->delete($i);
d622d38a 196 }
4f51df66 197 } else {
198 echo "$Line";
199 $outMsg .= _("Error Appending Message!");
d622d38a 200 }
4f51df66 201 }
d622d38a 202
4f51df66 203 $pop3->quit();
204 sqimap_logout($imap_stream);
205 if (is_array($mailfetch_uidl)) {
206 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
207 }
d622d38a 208 }
4f51df66 209 }
d622d38a 210
4f51df66 211 if( trim( $outMsg ) <> '' ) {
212 echo '<br /><font size="1">' . _("Mail Fetch Result:") . "<br />$outMsg</font>";
d622d38a 213 }
4f51df66 214 if( $mailfetch_newlog == 'on' ) {
215 setPref($data_dir, $username, 'mailfetch_newlog', 'off');
216 }
217}
d622d38a 218
4f51df66 219function mail_fetch_setnew() {
d622d38a 220
4f51df66 221 global $data_dir;
222 require_once(SM_PATH . 'functions/prefs.php');
d622d38a 223
4f51df66 224 sqgetGlobalVar('username', $username, SQ_SESSION);
d622d38a 225
4f51df66 226 setPref( $data_dir, $username, 'mailfetch_newlog', 'on' );
227}
d622d38a 228
4f51df66 229function mailfetch_optpage_register_block() {
230 global $optpage_blocks;
d622d38a 231
4f51df66 232 $optpage_blocks[] = array(
233 'name' => _("POP3 Fetch Mail"),
234 'url' => '../plugins/mail_fetch/options.php',
235 'desc' => _("This configures settings for downloading email from a POP3 mailbox to your account on this server."),
236 'js' => false
237 );
238}
d622d38a 239
5f438206 240function mail_fetch_folderact($args) {
241 global $username, $data_dir;
242
243 if (empty($args) || !is_array($args)) {
244 return;
245 }
246
247 /* Should be 3 ars, 1: old folder, 2: action, 3: new folder */
248 if (count($args) != 3) {
249 return;
250 }
251
252 list($old_folder, $action, $new_folder) = $args;
253
254 $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number');
255
256 for ($i = 0; $i < $mailfetch_server_number; $i++) {
257 $mailfetch_subfolder = getPref($data_dir, $username, 'mailfetch_subfolder_' . $i);
258
259 if ($mailfetch_subfolder != $old_folder) {
260 continue;
261 }
262
263 if ($action == 'delete') {
264 setPref($data_dir, $username, 'mailfetch_subfolder_' . $i, 'INBOX');
265 } elseif ($action == 'rename') {
266 setPref($data_dir, $username, 'mailfetch_subfolder_' . $i, $new_folder);
267 }
268 }
269}
91e0dccc 270?>