functions.php 16 KB

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