functions.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. /**
  3. * mail_fetch/functions.php
  4. *
  5. * Functions for the mail_fetch plugin.
  6. *
  7. * Original code from LexZEUS <lexzeus@mifinca.com>
  8. * and josh@superfork.com (extracted from php manual)
  9. * Adapted for MailFetch by Philippe Mingo <mingo@rotedic.com>
  10. *
  11. * @copyright &copy; 1999-2007 The SquirrelMail Project Team
  12. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  13. * @version $Id$
  14. * @package plugins
  15. * @subpackage mail_fetch
  16. */
  17. /** pop3 class */
  18. include_once (SM_PATH . 'plugins/mail_fetch/constants.php');
  19. include_once (SM_PATH . 'plugins/mail_fetch/class.mail_fetch.php');
  20. /** declare plugin globals */
  21. global $mail_fetch_allow_unsubscribed;
  22. /**
  23. * Add link to menu at top of content pane
  24. *
  25. * @return void
  26. *
  27. */
  28. function mail_fetch_link_do() {
  29. global $oTemplate, $nbsp;
  30. $output = makeInternalLink('plugins/mail_fetch/fetch.php', _("Fetch"), '')
  31. . $nbsp . $nbsp;
  32. return array('menuline' => $output);
  33. }
  34. /**
  35. * Controls use of unsubscribed folders in plugin
  36. * @global boolean $mail_fetch_allow_unsubscribed
  37. * @since 1.5.1 and 1.4.5
  38. */
  39. $mail_fetch_allow_unsubscribed = false;
  40. /** load site config */
  41. if (file_exists(SM_PATH . 'config/mail_fetch_config.php')) {
  42. include_once(SM_PATH . 'config/mail_fetch_config.php');
  43. } elseif (file_exists(SM_PATH . 'plugins/mail_fetch/config.php')) {
  44. include_once(SM_PATH . 'plugins/mail_fetch/config.php');
  45. }
  46. // hooked functions
  47. /**
  48. * Internal function used to fetch pop3 mails on login
  49. * @since 1.5.1
  50. * @private
  51. */
  52. function mail_fetch_login_function() {
  53. include_once (SM_PATH . 'functions/imap_general.php');
  54. global $username, $data_dir, $imapServerAddress, $imapPort;
  55. $mailfetch_newlog = getPref($data_dir, $username, 'mailfetch_newlog');
  56. $outMsg = '';
  57. $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number');
  58. if (!isset($mailfetch_server_number)) $mailfetch_server_number=0;
  59. $mailfetch_cypher = getPref($data_dir, $username, 'mailfetch_cypher');
  60. if ($mailfetch_server_number<1) $mailfetch_server_number=0;
  61. for ($i_loop=0;$i_loop<$mailfetch_server_number;$i_loop++) {
  62. $mailfetch_login_[$i_loop] = getPref($data_dir, $username, "mailfetch_login_$i_loop");
  63. $mailfetch_fref_[$i_loop] = getPref($data_dir, $username, "mailfetch_fref_$i_loop");
  64. $mailfetch_pass_[$i_loop] = getPref($data_dir, $username, "mailfetch_pass_$i_loop");
  65. if( $mailfetch_cypher == 'on' ) {
  66. $mailfetch_pass_[$i_loop] = decrypt( $mailfetch_pass_[$i_loop] );
  67. }
  68. if( $mailfetch_pass_[$i_loop] <> '' && // Empty passwords no allowed
  69. ( ( $mailfetch_login_[$i_loop] == 'on' && $mailfetch_newlog == 'on' ) || $mailfetch_fref_[$i_loop] == 'on' ) ) {
  70. // What the heck
  71. $mailfetch_server_[$i_loop] = getPref($data_dir, $username, "mailfetch_server_$i_loop");
  72. $mailfetch_port_[$i_loop] = getPref($data_dir, $username , "mailfetch_port_$i_loop");
  73. $mailfetch_alias_[$i_loop] = getPref($data_dir, $username, "mailfetch_alias_$i_loop");
  74. $mailfetch_user_[$i_loop] = getPref($data_dir, $username, "mailfetch_user_$i_loop");
  75. $mailfetch_lmos_[$i_loop] = getPref($data_dir, $username, "mailfetch_lmos_$i_loop");
  76. $mailfetch_uidl_[$i_loop] = getPref($data_dir, $username, "mailfetch_uidl_$i_loop");
  77. $mailfetch_subfolder_[$i_loop] = getPref($data_dir, $username, "mailfetch_subfolder_$i_loop");
  78. $mailfetch_auth_[$i_loop] = getPref($data_dir, $username, "mailfetch_auth_$i_loop",MAIL_FETCH_AUTH_USER);
  79. $mailfetch_type_[$i_loop] = getPref($data_dir, $username, "mailfetch_type_$i_loop",MAIL_FETCH_USE_PLAIN);
  80. $mailfetch_server=$mailfetch_server_[$i_loop];
  81. $mailfetch_port=$mailfetch_port_[$i_loop];
  82. $mailfetch_user=$mailfetch_user_[$i_loop];
  83. $mailfetch_alias=$mailfetch_alias_[$i_loop];
  84. $mailfetch_pass=$mailfetch_pass_[$i_loop];
  85. $mailfetch_lmos=$mailfetch_lmos_[$i_loop];
  86. $mailfetch_login=$mailfetch_login_[$i_loop];
  87. $mailfetch_uidl=$mailfetch_uidl_[$i_loop];
  88. $mailfetch_subfolder=$mailfetch_subfolder_[$i_loop];
  89. $mailfetch_auth=$mailfetch_auth_[$i_loop];
  90. $mailfetch_type=$mailfetch_type_[$i_loop];
  91. // end of what the heck
  92. // $outMsg .= "$mailfetch_alias checked<br />";
  93. // $outMsg .= "$mailfetch_alias_[$i_loop]<br />";
  94. // FIXME: duplicate code with different output destination.
  95. $pop3 = new mail_fetch(array('host' => $mailfetch_server,
  96. 'port' => $mailfetch_port,
  97. 'auth' => $mailfetch_auth,
  98. 'tls' => $mailfetch_type,
  99. 'timeout' => 60));
  100. if (!empty($pop3->error)) {
  101. $outMsg .= _("Warning:") . ' ' . $pop3->error;
  102. continue;
  103. }
  104. $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
  105. /* log into pop server*/
  106. if (! $pop3->login($mailfetch_user, $mailfetch_pass)) {
  107. $outMsg .= _("Login Failed:") . ' ' . $pop3->error;
  108. continue;
  109. }
  110. $aMsgStat = $pop3->command_stat();
  111. if (is_bool($aMsgStat)) {
  112. $outMsg .= _("Can't get mailbox status:") . ' ' . htmlspecialchars($pop3->error);
  113. continue;
  114. }
  115. $Count = $aMsgStat['count'];
  116. $i = 1;
  117. if ($Count>0) {
  118. // If we leave messages on server, try using UIDL
  119. if ($mailfetch_lmos == 'on') {
  120. $msglist = $pop3->command_uidl();
  121. if (is_bool($msglist)) {
  122. $outMsg .= _("Server does not support UIDL.") . ' '.htmlspecialchars($pop3->error);
  123. // User asked to leave messages on server, but we can't do that.
  124. $pop3->command_quit();
  125. continue;
  126. // $mailfetch_lmos = 'off';
  127. } else {
  128. // calculate number of new messages
  129. for ($j = 1; $j <= sizeof($msglist); $j++) {
  130. // do strict comparison ('1111.10' should not be equal to '1111.100')
  131. if ($msglist[$j] === $mailfetch_uidl) {
  132. $i = $j+1;
  133. break;
  134. }
  135. }
  136. }
  137. }
  138. // fetch list of messages with LIST
  139. // we can use else control, but we can also set $mailfetch_lmos
  140. // to off if server does not support UIDL.
  141. if ($mailfetch_lmos != 'on') {
  142. $msglist = $pop3->command_list();
  143. }
  144. }
  145. if ($Count < $i) {
  146. $pop3->command_quit();
  147. continue;
  148. }
  149. if ($Count == 0) {
  150. $pop3->command_quit();
  151. continue;
  152. }
  153. for (; $i <= $Count; $i++) {
  154. if (!ini_get('safe_mode'))
  155. set_time_limit(20); // 20 seconds per message max
  156. $Message = $pop3->command_retr($i);
  157. if (is_bool($Message)) {
  158. $outMsg .= _("Warning:") . ' ' . htmlspecialchars($pop3->error);
  159. continue;
  160. }
  161. // check if mail folder is not null and subscribed (There is possible issue with /noselect mail folders)
  162. if ($mailfetch_subfolder=='' ||
  163. ! mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) {
  164. fputs($imap_stream, "A3$i APPEND INBOX {" . strlen($Message) . "}\r\n");
  165. } else {
  166. fputs($imap_stream, "A3$i APPEND $mailfetch_subfolder {" . strlen($Message) . "}\r\n");
  167. }
  168. $Line = fgets($imap_stream, 1024);
  169. if (substr($Line, 0, 1) == '+') {
  170. fputs($imap_stream, $Message);
  171. fputs($imap_stream, "\r\n");
  172. sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
  173. // Check results of append command
  174. $response=(implode('',$response));
  175. $message=(implode('',$message));
  176. if ($response != 'OK') {
  177. $outMsg .= _("Error Appending Message!")." ".htmlspecialchars($message);
  178. if ($mailfetch_lmos == 'on') {
  179. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
  180. }
  181. // Destroy msg list in order to prevent UIDL update
  182. $msglist = false;
  183. // if append fails, don't download other messages
  184. break;
  185. }
  186. if ($mailfetch_lmos != 'on') {
  187. $pop3->command_dele($i);
  188. }
  189. } else {
  190. echo "$Line";
  191. $outMsg .= _("Error Appending Message!");
  192. }
  193. }
  194. $pop3->command_quit();
  195. sqimap_logout($imap_stream);
  196. if ($mailfetch_lmos == 'on' && is_array($msglist)) {
  197. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($msglist));
  198. }
  199. }
  200. }
  201. if( trim( $outMsg ) <> '' ) {
  202. echo '<br /><font size="1">' . _("Mail Fetch Result:") . "<br />$outMsg</font>";
  203. }
  204. if( $mailfetch_newlog == 'on' ) {
  205. setPref($data_dir, $username, 'mailfetch_newlog', 'off');
  206. }
  207. }
  208. /**
  209. * Internal function used to detect new logins
  210. */
  211. function mail_fetch_setnew_function() {
  212. global $data_dir, $username;
  213. setPref( $data_dir, $username, 'mailfetch_newlog', 'on' );
  214. }
  215. /**
  216. * Internal function used to register option block
  217. * @since 1.5.1
  218. * @private
  219. */
  220. function mailfetch_optpage_register_block_function() {
  221. global $optpage_blocks;
  222. $optpage_blocks[] = array(
  223. 'name' => _("POP3 Fetch Mail"),
  224. 'url' => '../plugins/mail_fetch/options.php',
  225. 'desc' => _("This configures settings for downloading email from a POP3 mailbox to your account on this server."),
  226. 'js' => false
  227. );
  228. }
  229. /**
  230. * Internal function used to update mail_fetch settings
  231. * when folders are renamed or deleted.
  232. * @since 1.5.1
  233. * @private
  234. */
  235. function mail_fetch_folderact_function($args) {
  236. global $username, $data_dir;
  237. if (empty($args) || !is_array($args)) {
  238. return;
  239. }
  240. /* Should be 3 ars, 1: old folder, 2: action, 3: new folder */
  241. if (count($args) != 3) {
  242. return;
  243. }
  244. list($old_folder, $action, $new_folder) = $args;
  245. $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number');
  246. for ($i = 0; $i < $mailfetch_server_number; $i++) {
  247. $mailfetch_subfolder = getPref($data_dir, $username, 'mailfetch_subfolder_' . $i);
  248. if ($mailfetch_subfolder != $old_folder) {
  249. continue;
  250. }
  251. if ($action == 'delete') {
  252. setPref($data_dir, $username, 'mailfetch_subfolder_' . $i, 'INBOX');
  253. } elseif ($action == 'rename') {
  254. setPref($data_dir, $username, 'mailfetch_subfolder_' . $i, $new_folder);
  255. }
  256. }
  257. }
  258. // end of hooked functions
  259. /**
  260. * hex2bin - document me
  261. */
  262. function hex2bin( $data ) {
  263. /* Original code by josh@superfork.com */
  264. $len = strlen($data);
  265. $newdata = '';
  266. for( $i=0; $i < $len; $i += 2 ) {
  267. $newdata .= pack( "C", hexdec( substr( $data, $i, 2) ) );
  268. }
  269. return $newdata;
  270. }
  271. function mf_keyED( $txt ) {
  272. global $MF_TIT;
  273. if( !isset( $MF_TIT ) ) {
  274. $MF_TIT = "MailFetch Secure for SquirrelMail 1.x";
  275. }
  276. $encrypt_key = md5( $MF_TIT );
  277. $ctr = 0;
  278. $tmp = "";
  279. for( $i = 0; $i < strlen( $txt ); $i++ ) {
  280. if( $ctr == strlen( $encrypt_key ) ) $ctr=0;
  281. $tmp.= substr( $txt, $i, 1 ) ^ substr( $encrypt_key, $ctr, 1 );
  282. $ctr++;
  283. }
  284. return $tmp;
  285. }
  286. function encrypt( $txt ) {
  287. srand( (double) microtime() * 1000000 );
  288. $encrypt_key = md5( rand( 0, 32000 ) );
  289. $ctr = 0;
  290. $tmp = "";
  291. for( $i = 0; $i < strlen( $txt ); $i++ ) {
  292. if ($ctr==strlen($encrypt_key)) $ctr=0;
  293. $tmp.= substr($encrypt_key,$ctr,1) .
  294. (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));
  295. $ctr++;
  296. }
  297. return bin2hex( mf_keyED( $tmp ) );
  298. }
  299. function decrypt( $txt ) {
  300. $txt = mf_keyED( hex2bin( $txt ) );
  301. $tmp = '';
  302. for ( $i=0; $i < strlen( $txt ); $i++ ) {
  303. $md5 = substr( $txt, $i, 1 );
  304. $i++;
  305. $tmp.= ( substr( $txt, $i, 1 ) ^ $md5 );
  306. }
  307. return $tmp;
  308. }
  309. /**
  310. * check mail folder
  311. * @param stream $imap_stream imap connection resource
  312. * @param string $imap_folder imap folder name
  313. * @return boolean true, when folder can be used to store messages.
  314. * @since 1.5.1 and 1.4.5
  315. */
  316. function mail_fetch_check_folder($imap_stream,$imap_folder) {
  317. global $mail_fetch_allow_unsubscribed;
  318. // check if folder is subscribed or only exists.
  319. if (sqimap_mailbox_is_subscribed($imap_stream,$imap_folder)) {
  320. $ret = true;
  321. } elseif ($mail_fetch_allow_unsubscribed && sqimap_mailbox_exists($imap_stream,$imap_folder)) {
  322. $ret = true;
  323. } else {
  324. $ret = false;
  325. }
  326. // make sure that folder can store messages
  327. if ($ret && mail_fetch_check_noselect($imap_stream,$imap_folder)) {
  328. $ret = false;
  329. }
  330. return $ret;
  331. }
  332. /**
  333. * Checks if folder is noselect (can't store messages)
  334. *
  335. * Function does not check if folder subscribed.
  336. * @param stream $imap_stream imap connection resource
  337. * @param string $imap_folder imap folder name
  338. * @return boolean true, when folder has noselect flag. false in any other case.
  339. * @since 1.5.1 and 1.4.5
  340. */
  341. function mail_fetch_check_noselect($imap_stream,$imap_folder) {
  342. $boxes=sqimap_mailbox_list($imap_stream);
  343. foreach($boxes as $box) {
  344. if ($box['unformatted']==$imap_folder) {
  345. return (bool) check_is_noselect($box['raw']);
  346. }
  347. }
  348. return false;
  349. }