setup.php 9.8 KB

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