functions.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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;
  30. $nbsp = $oTemplate->fetch('non_breaking_space.tpl');
  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;
  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);
  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:") . ' ' . htmlspecialchars($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.") . ' '.htmlspecialchars($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:") . ' ' . htmlspecialchars($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!")." ".htmlspecialchars($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 - document me
  262. */
  263. function hex2bin( $data ) {
  264. /* Original code by josh@superfork.com */
  265. $len = strlen($data);
  266. $newdata = '';
  267. for( $i=0; $i < $len; $i += 2 ) {
  268. $newdata .= pack( "C", hexdec( substr( $data, $i, 2) ) );
  269. }
  270. return $newdata;
  271. }
  272. function mf_keyED( $txt ) {
  273. global $MF_TIT;
  274. if( !isset( $MF_TIT ) ) {
  275. $MF_TIT = "MailFetch Secure for SquirrelMail 1.x";
  276. }
  277. $encrypt_key = md5( $MF_TIT );
  278. $ctr = 0;
  279. $tmp = "";
  280. for( $i = 0; $i < strlen( $txt ); $i++ ) {
  281. if( $ctr == strlen( $encrypt_key ) ) $ctr=0;
  282. $tmp.= substr( $txt, $i, 1 ) ^ substr( $encrypt_key, $ctr, 1 );
  283. $ctr++;
  284. }
  285. return $tmp;
  286. }
  287. function encrypt( $txt ) {
  288. srand( (double) microtime() * 1000000 );
  289. $encrypt_key = md5( rand( 0, 32000 ) );
  290. $ctr = 0;
  291. $tmp = "";
  292. for( $i = 0; $i < strlen( $txt ); $i++ ) {
  293. if ($ctr==strlen($encrypt_key)) $ctr=0;
  294. $tmp.= substr($encrypt_key,$ctr,1) .
  295. (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));
  296. $ctr++;
  297. }
  298. return bin2hex( mf_keyED( $tmp ) );
  299. }
  300. function decrypt( $txt ) {
  301. $txt = mf_keyED( hex2bin( $txt ) );
  302. $tmp = '';
  303. for ( $i=0; $i < strlen( $txt ); $i++ ) {
  304. $md5 = substr( $txt, $i, 1 );
  305. $i++;
  306. $tmp.= ( substr( $txt, $i, 1 ) ^ $md5 );
  307. }
  308. return $tmp;
  309. }
  310. /**
  311. * check mail folder
  312. * @param stream $imap_stream imap connection resource
  313. * @param string $imap_folder imap folder name
  314. * @return boolean true, when folder can be used to store messages.
  315. * @since 1.5.1 and 1.4.5
  316. */
  317. function mail_fetch_check_folder($imap_stream,$imap_folder) {
  318. global $mail_fetch_allow_unsubscribed;
  319. // check if folder is subscribed or only exists.
  320. if (sqimap_mailbox_is_subscribed($imap_stream,$imap_folder)) {
  321. $ret = true;
  322. } elseif ($mail_fetch_allow_unsubscribed && sqimap_mailbox_exists($imap_stream,$imap_folder)) {
  323. $ret = true;
  324. } else {
  325. $ret = false;
  326. }
  327. // make sure that folder can store messages
  328. if ($ret && mail_fetch_check_noselect($imap_stream,$imap_folder)) {
  329. $ret = false;
  330. }
  331. return $ret;
  332. }
  333. /**
  334. * Checks if folder is noselect (can't store messages)
  335. *
  336. * Function does not check if folder subscribed.
  337. * @param stream $imap_stream imap connection resource
  338. * @param string $imap_folder imap folder name
  339. * @return boolean true, when folder has noselect flag. false in any other case.
  340. * @since 1.5.1 and 1.4.5
  341. */
  342. function mail_fetch_check_noselect($imap_stream,$imap_folder) {
  343. $boxes=sqimap_mailbox_list($imap_stream);
  344. foreach($boxes as $box) {
  345. if ($box['unformatted']==$imap_folder) {
  346. return (bool) check_is_noselect($box['raw']);
  347. }
  348. }
  349. return false;
  350. }