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