setup.php 9.8 KB

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