setup.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. ** mail_fetch/setup.php
  4. **
  5. ** Copyright (c) 1999-2002 The SquirrelMail Project Team
  6. ** Licensed under the GNU GPL. For full terms see the file COPYING.
  7. **
  8. ** Setup of the mailfetch plugin.
  9. **
  10. ** $Id$
  11. **/
  12. require_once( '../plugins/mail_fetch/functions.php' );
  13. function squirrelmail_plugin_init_mail_fetch() {
  14. global $squirrelmail_plugin_hooks;
  15. global $mailbox, $imap_stream, $imapConnection;
  16. $squirrelmail_plugin_hooks['menuline']['mail_fetch'] = 'mail_fetch_link';
  17. $squirrelmail_plugin_hooks['loading_prefs']['mail_fetch'] = 'mail_fetch_load_pref';
  18. $squirrelmail_plugin_hooks['login_verified']['mail_fetch'] = 'mail_fetch_setnew';
  19. $squirrelmail_plugin_hooks['left_main_before']['mail_fetch'] = 'mail_fetch_login';
  20. $squirrelmail_plugin_hooks['optpage_register_block']['mail_fetch'] = 'mailfetch_optpage_register_block';
  21. }
  22. function mail_fetch_link() {
  23. displayInternalLink('plugins/mail_fetch/fetch.php', _("Fetch"), '');
  24. echo '&nbsp;&nbsp;';
  25. }
  26. function mail_fetch_load_pref() {
  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. if( stristr( $PHP_SELF, 'mail_fetch' ) ) {
  34. $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number', 0);
  35. $mailfetch_cypher = getPref($data_dir, $username, 'mailfetch_cypher', 'on' );
  36. if ($mailfetch_server_number<1) $mailfetch_server_number=0;
  37. for ($i=0;$i<$mailfetch_server_number;$i++) {
  38. $mailfetch_server_[$i] = getPref($data_dir, $username, "mailfetch_server_$i");
  39. $mailfetch_alias_[$i] = getPref($data_dir, $username, "mailfetch_alias_$i");
  40. $mailfetch_user_[$i] = getPref($data_dir, $username, "mailfetch_user_$i");
  41. $mailfetch_pass_[$i] = getPref($data_dir, $username, "mailfetch_pass_$i");
  42. $mailfetch_lmos_[$i] = getPref($data_dir, $username, "mailfetch_lmos_$i");
  43. $mailfetch_login_[$i] = getPref($data_dir, $username, "mailfetch_login_$i");
  44. $mailfetch_fref_[$i] = getPref($data_dir, $username, "mailfetch_fref_$i");
  45. $mailfetch_uidl_[$i] = getPref($data_dir, $username, "mailfetch_uidl_$i");
  46. if( $mailfetch_cypher == 'on' ) $mailfetch_pass_[$i] = decrypt( $mailfetch_pass_[$i] );
  47. }
  48. }
  49. }
  50. function mail_fetch_login() {
  51. require_once ('../src/validate.php');
  52. require_once ('../functions/imap.php');
  53. require_once ('../plugins/mail_fetch/class.POP3.php');
  54. require_once ('../plugins/mail_fetch/functions.php');
  55. require_once('../functions/i18n.php');
  56. global $username, $data_dir, $key,$imapServerAddress,$imapPort;
  57. $mailfetch_newlog = getPref($data_dir, $username, 'mailfetch_newlog');
  58. $outMsg = '';
  59. $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number');
  60. if (!isset($mailfetch_server_number)) $mailfetch_server_number=0;
  61. $mailfetch_cypher = getPref($data_dir, $username, 'mailfetch_cypher');
  62. if ($mailfetch_server_number<1) $mailfetch_server_number=0;
  63. for ($i_loop=0;$i_loop<$mailfetch_server_number;$i_loop++) {
  64. $mailfetch_login_[$i_loop] = getPref($data_dir, $username, "mailfetch_login_$i_loop");
  65. $mailfetch_fref_[$i_loop] = getPref($data_dir, $username, "mailfetch_fref_$i_loop");
  66. $mailfetch_pass_[$i_loop] = getPref($data_dir, $username, "mailfetch_pass_$i_loop");
  67. if( $mailfetch_cypher == 'on' )
  68. $mailfetch_pass_[$i_loop] = decrypt( $mailfetch_pass_[$i_loop] );
  69. if( $mailfetch_pass_[$i_loop] <> '' && // Empty passwords no allowed
  70. ( ( $mailfetch_login_[$i_loop] == 'on' && $mailfetch_newlog == 'on' ) || $mailfetch_fref_[$i_loop] == 'on' ) ) {
  71. $mailfetch_server_[$i_loop] = getPref($data_dir, $username, "mailfetch_server_$i_loop");
  72. $mailfetch_alias_[$i_loop] = getPref($data_dir, $username, "mailfetch_alias_$i_loop");
  73. $mailfetch_user_[$i_loop] = getPref($data_dir, $username, "mailfetch_user_$i_loop");
  74. $mailfetch_lmos_[$i_loop] = getPref($data_dir, $username, "mailfetch_lmos_$i_loop");
  75. $mailfetch_uidl_[$i_loop] = getPref($data_dir, $username, "mailfetch_uidl_$i_loop");
  76. $mailfetch_subfolder_[$i_loop] = getPref($data_dir, $username, "mailfetch_subfolder_$i_loop");
  77. $mailfetch_server=$mailfetch_server_[$i_loop];
  78. $mailfetch_user=$mailfetch_user_[$i_loop];
  79. $mailfetch_alias=$mailfetch_alias_[$i_loop];
  80. $mailfetch_pass=$mailfetch_pass_[$i_loop];
  81. $mailfetch_lmos=$mailfetch_lmos_[$i_loop];
  82. $mailfetch_login=$mailfetch_login_[$i_loop];
  83. $mailfetch_uidl=$mailfetch_uidl_[$i_loop];
  84. $mailfetch_subfolder=$mailfetch_subfolder_[$i_loop];
  85. // $outMsg .= "$mailfetch_alias checked<br>";
  86. // $outMsg .= "$mailfetch_alias_[$i_loop]<br>";
  87. $pop3 = new POP3($mailfetch_server, 60);
  88. if (!$pop3->connect($mailfetch_server)) {
  89. $outMsg .= _("Warning, ") . $pop3->ERROR;
  90. continue;
  91. }
  92. $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
  93. $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
  94. if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
  95. $outMsg .= _("Login Failed:") . $pop3->ERROR;
  96. continue;
  97. }
  98. // register_shutdown_function($pop3->quit());
  99. $msglist = $pop3->uidl();
  100. $i = 1;
  101. for ($j = 1; $j < sizeof($msglist); $j++) {
  102. if ($msglist["$j"] == $mailfetch_uidl) {
  103. $i = $j+1;
  104. break;
  105. }
  106. }
  107. if ($Count < $i) {
  108. $pop3->quit();
  109. continue;
  110. }
  111. if ($Count == 0) {
  112. $pop3->quit();
  113. continue;
  114. } else {
  115. $newmsgcount = $Count - $i + 1;
  116. }
  117. // Faster to get them all at once
  118. $mailfetch_uidl = $pop3->uidl();
  119. if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
  120. $outMsg .= _("Server does not support UIDL.");
  121. for (; $i <= $Count; $i++) {
  122. set_time_limit(20); // 20 seconds per message max
  123. $Message = "";
  124. $MessArray = $pop3->get($i);
  125. if ( (!$MessArray) or (gettype($MessArray) != "array")) {
  126. $outMsg .= _("Warning, ") . $pop3->ERROR;
  127. continue 2;
  128. }
  129. while (list($lineNum, $line) = each ($MessArray)) {
  130. $Message .= $line;
  131. }
  132. if ($mailfetch_subfolder=="") {
  133. fputs($imap_stream, "A3$i APPEND INBOX {" . (strlen($Message) - 1) . "}\r\n");
  134. } else {
  135. fputs($imap_stream, "A3$i APPEND $mailfetch_subfolder {" . (strlen($Message) - 1) . "}\r\n");
  136. }
  137. $Line = fgets($imap_stream, 1024);
  138. if (substr($Line, 0, 1) == '+') {
  139. fputs($imap_stream, $Message);
  140. sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
  141. if ($mailfetch_lmos != 'on') {
  142. $pop3->delete($i);
  143. }
  144. } else {
  145. echo "$Line";
  146. $outMsg .= _("Error Appending Message!");
  147. }
  148. }
  149. $pop3->quit();
  150. sqimap_logout($imap_stream);
  151. if (is_array($mailfetch_uidl)) {
  152. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
  153. }
  154. }
  155. }
  156. if( trim( $outMsg ) <> '' )
  157. echo '<br><font size=1>' . _("Mail Fetch Result:") . "<br>$outMsg</font>";
  158. if( $mailfetch_newlog == 'on' )
  159. setPref($data_dir,$username,"mailfetch_newlog", 'off');
  160. }
  161. function mail_fetch_setnew() {
  162. global $data_dir,$username;
  163. // require_once ('../src/load_prefs.php');
  164. // require_once ('../src/validate.php');
  165. require_once('../functions/prefs.php');
  166. if( $username <> '' ) {
  167. // Creates the pref file if it does not exist.
  168. setPref( $data_dir, $username, 'mailfetch_newlog', 'on' );
  169. }
  170. }
  171. function mailfetch_optpage_register_block() {
  172. global $optpage_blocks;
  173. $optpage_blocks[] = array(
  174. 'name' => _("Simple POP3 Fetch Mail"),
  175. 'url' => '../plugins/mail_fetch/options.php',
  176. 'desc' => _("This configures settings for downloading email from a pop3 mailbox to your account on this server."),
  177. 'js' => false
  178. );
  179. }
  180. ?>