functions.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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-2006 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. /** @ignore */
  18. if (! defined('SM_PATH')) define('SM_PATH','../../');
  19. /** pop3 class */
  20. include_once (SM_PATH . 'plugins/mail_fetch/class.POP3.php');
  21. /** declare plugin globals */
  22. global $mail_fetch_allow_unsubscribed;
  23. /**
  24. * Controls use of unsubscribed folders in plugin
  25. * @global boolean $mail_fetch_allow_unsubscribed
  26. * @since 1.5.1 and 1.4.5
  27. */
  28. $mail_fetch_allow_unsubscribed = false;
  29. /** load site config */
  30. if (file_exists(SM_PATH . 'config/mail_fetch_config.php')) {
  31. include_once(SM_PATH . 'config/mail_fetch_config.php');
  32. } elseif (file_exists(SM_PATH . 'plugins/mail_fetch/config.php')) {
  33. include_once(SM_PATH . 'plugins/mail_fetch/config.php');
  34. }
  35. // hooked functions
  36. /**
  37. * internal function used to load user's preferences
  38. * @since 1.5.1
  39. * @private
  40. */
  41. function mail_fetch_load_pref_function() {
  42. global $data_dir;
  43. global $mailfetch_server_number;
  44. global $mailfetch_cypher, $mailfetch_port_;
  45. global $mailfetch_server_,$mailfetch_alias_,$mailfetch_user_,$mailfetch_pass_;
  46. global $mailfetch_lmos_, $mailfetch_uidl_, $mailfetch_login_, $mailfetch_fref_;
  47. global $PHP_SELF;
  48. sqgetGlobalVar('username', $username, SQ_SESSION);
  49. if( stristr( $PHP_SELF, 'mail_fetch' ) ) {
  50. $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number', 0);
  51. $mailfetch_cypher = getPref($data_dir, $username, 'mailfetch_cypher', 'on' );
  52. if ($mailfetch_server_number<1) $mailfetch_server_number=0;
  53. for ($i=0;$i<$mailfetch_server_number;$i++) {
  54. $mailfetch_server_[$i] = getPref($data_dir, $username, "mailfetch_server_$i");
  55. $mailfetch_port_[$i] = getPref($data_dir, $username, "mailfetch_port_$i");
  56. $mailfetch_alias_[$i] = getPref($data_dir, $username, "mailfetch_alias_$i");
  57. $mailfetch_user_[$i] = getPref($data_dir, $username, "mailfetch_user_$i");
  58. $mailfetch_pass_[$i] = getPref($data_dir, $username, "mailfetch_pass_$i");
  59. $mailfetch_lmos_[$i] = getPref($data_dir, $username, "mailfetch_lmos_$i");
  60. $mailfetch_login_[$i] = getPref($data_dir, $username, "mailfetch_login_$i");
  61. $mailfetch_fref_[$i] = getPref($data_dir, $username, "mailfetch_fref_$i");
  62. $mailfetch_uidl_[$i] = getPref($data_dir, $username, "mailfetch_uidl_$i");
  63. if( $mailfetch_cypher == 'on' ) $mailfetch_pass_[$i] = decrypt( $mailfetch_pass_[$i] );
  64. }
  65. }
  66. }
  67. /**
  68. * Internal function used to fetch pop3 mails on login
  69. * @since 1.5.1
  70. * @private
  71. */
  72. function mail_fetch_login_function() {
  73. include_once (SM_PATH . 'include/validate.php');
  74. include_once (SM_PATH . 'functions/imap.php');
  75. global $data_dir, $imapServerAddress, $imapPort;
  76. sqgetGlobalVar('username', $username, SQ_SESSION);
  77. sqgetGlobalVar('key', $key, SQ_COOKIE);
  78. $mailfetch_newlog = getPref($data_dir, $username, 'mailfetch_newlog');
  79. $outMsg = '';
  80. $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number');
  81. if (!isset($mailfetch_server_number)) $mailfetch_server_number=0;
  82. $mailfetch_cypher = getPref($data_dir, $username, 'mailfetch_cypher');
  83. if ($mailfetch_server_number<1) $mailfetch_server_number=0;
  84. for ($i_loop=0;$i_loop<$mailfetch_server_number;$i_loop++) {
  85. $mailfetch_login_[$i_loop] = getPref($data_dir, $username, "mailfetch_login_$i_loop");
  86. $mailfetch_fref_[$i_loop] = getPref($data_dir, $username, "mailfetch_fref_$i_loop");
  87. $mailfetch_pass_[$i_loop] = getPref($data_dir, $username, "mailfetch_pass_$i_loop");
  88. if( $mailfetch_cypher == 'on' )
  89. $mailfetch_pass_[$i_loop] = decrypt( $mailfetch_pass_[$i_loop] );
  90. if( $mailfetch_pass_[$i_loop] <> '' && // Empty passwords no allowed
  91. ( ( $mailfetch_login_[$i_loop] == 'on' && $mailfetch_newlog == 'on' ) || $mailfetch_fref_[$i_loop] == 'on' ) ) {
  92. $mailfetch_server_[$i_loop] = getPref($data_dir, $username, "mailfetch_server_$i_loop");
  93. $mailfetch_port_[$i_loop] = getPref($data_dir, $username , "mailfetch_port_$i_loop");
  94. $mailfetch_alias_[$i_loop] = getPref($data_dir, $username, "mailfetch_alias_$i_loop");
  95. $mailfetch_user_[$i_loop] = getPref($data_dir, $username, "mailfetch_user_$i_loop");
  96. $mailfetch_lmos_[$i_loop] = getPref($data_dir, $username, "mailfetch_lmos_$i_loop");
  97. $mailfetch_uidl_[$i_loop] = getPref($data_dir, $username, "mailfetch_uidl_$i_loop");
  98. $mailfetch_subfolder_[$i_loop] = getPref($data_dir, $username, "mailfetch_subfolder_$i_loop");
  99. $mailfetch_server=$mailfetch_server_[$i_loop];
  100. $mailfetch_port=$mailfetch_port_[$i_loop];
  101. $mailfetch_user=$mailfetch_user_[$i_loop];
  102. $mailfetch_alias=$mailfetch_alias_[$i_loop];
  103. $mailfetch_pass=$mailfetch_pass_[$i_loop];
  104. $mailfetch_lmos=$mailfetch_lmos_[$i_loop];
  105. $mailfetch_login=$mailfetch_login_[$i_loop];
  106. $mailfetch_uidl=$mailfetch_uidl_[$i_loop];
  107. $mailfetch_subfolder=$mailfetch_subfolder_[$i_loop];
  108. // $outMsg .= "$mailfetch_alias checked<br />";
  109. // $outMsg .= "$mailfetch_alias_[$i_loop]<br />";
  110. $pop3 = new POP3($mailfetch_server, 60);
  111. if (!$pop3->connect($mailfetch_server,$mailfetch_port)) {
  112. $outMsg .= _("Warning:") . ' ' . $pop3->ERROR;
  113. continue;
  114. }
  115. $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
  116. $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
  117. if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
  118. $outMsg .= _("Login Failed:") . ' ' . $pop3->ERROR;
  119. continue;
  120. }
  121. // register_shutdown_function($pop3->quit());
  122. $msglist = $pop3->uidl();
  123. $i = 1;
  124. for ($j = 1; $j < sizeof($msglist); $j++) {
  125. if ($msglist["$j"] == $mailfetch_uidl) {
  126. $i = $j+1;
  127. break;
  128. }
  129. }
  130. if ($Count < $i) {
  131. $pop3->quit();
  132. continue;
  133. }
  134. if ($Count == 0) {
  135. $pop3->quit();
  136. continue;
  137. }
  138. // Faster to get them all at once
  139. $mailfetch_uidl = $pop3->uidl();
  140. if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
  141. $outMsg .= _("Server does not support UIDL.");
  142. for (; $i <= $Count; $i++) {
  143. if (!ini_get('safe_mode'))
  144. set_time_limit(20); // 20 seconds per message max
  145. $Message = "";
  146. $MessArray = $pop3->get($i);
  147. if ( (!$MessArray) or (gettype($MessArray) != "array")) {
  148. $outMsg .= _("Warning:") . ' ' . $pop3->ERROR;
  149. continue 2;
  150. }
  151. while (list($lineNum, $line) = each ($MessArray)) {
  152. $Message .= $line;
  153. }
  154. // check if mail folder is not null and subscribed (There is possible issue with /noselect mail folders)
  155. if ($mailfetch_subfolder=='' ||
  156. ! mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) {
  157. fputs($imap_stream, "A3$i APPEND INBOX {" . strlen($Message) . "}\r\n");
  158. } else {
  159. fputs($imap_stream, "A3$i APPEND $mailfetch_subfolder {" . strlen($Message) . "}\r\n");
  160. }
  161. $Line = fgets($imap_stream, 1024);
  162. if (substr($Line, 0, 1) == '+') {
  163. fputs($imap_stream, $Message);
  164. fputs($imap_stream, "\r\n");
  165. sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
  166. if ($mailfetch_lmos != 'on') {
  167. $pop3->delete($i);
  168. }
  169. } else {
  170. echo "$Line";
  171. $outMsg .= _("Error Appending Message!");
  172. }
  173. }
  174. $pop3->quit();
  175. sqimap_logout($imap_stream);
  176. if (is_array($mailfetch_uidl)) {
  177. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
  178. }
  179. }
  180. }
  181. if( trim( $outMsg ) <> '' ) {
  182. echo '<br /><font size="1">' . _("Mail Fetch Result:") . "<br />$outMsg</font>";
  183. }
  184. if( $mailfetch_newlog == 'on' ) {
  185. setPref($data_dir, $username, 'mailfetch_newlog', 'off');
  186. }
  187. }
  188. /**
  189. * Internal function used to detect new logins
  190. */
  191. function mail_fetch_setnew_function() {
  192. global $data_dir;
  193. // FIXME: check if function is already loaded in login_verified hook
  194. include_once(SM_PATH . 'functions/prefs.php');
  195. sqgetGlobalVar('username', $username, SQ_SESSION);
  196. setPref( $data_dir, $username, 'mailfetch_newlog', 'on' );
  197. }
  198. /**
  199. * Internal function used to register option block
  200. * @since 1.5.1
  201. * @private
  202. */
  203. function mailfetch_optpage_register_block_function() {
  204. global $optpage_blocks;
  205. $optpage_blocks[] = array(
  206. 'name' => _("POP3 Fetch Mail"),
  207. 'url' => '../plugins/mail_fetch/options.php',
  208. 'desc' => _("This configures settings for downloading email from a POP3 mailbox to your account on this server."),
  209. 'js' => false
  210. );
  211. }
  212. /**
  213. * Internal function used to update mail_fetch settings
  214. * when folders are renamed or deleted.
  215. * @since 1.5.1
  216. * @private
  217. */
  218. function mail_fetch_folderact_function($args) {
  219. global $username, $data_dir;
  220. if (empty($args) || !is_array($args)) {
  221. return;
  222. }
  223. /* Should be 3 ars, 1: old folder, 2: action, 3: new folder */
  224. if (count($args) != 3) {
  225. return;
  226. }
  227. list($old_folder, $action, $new_folder) = $args;
  228. $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number');
  229. for ($i = 0; $i < $mailfetch_server_number; $i++) {
  230. $mailfetch_subfolder = getPref($data_dir, $username, 'mailfetch_subfolder_' . $i);
  231. if ($mailfetch_subfolder != $old_folder) {
  232. continue;
  233. }
  234. if ($action == 'delete') {
  235. setPref($data_dir, $username, 'mailfetch_subfolder_' . $i, 'INBOX');
  236. } elseif ($action == 'rename') {
  237. setPref($data_dir, $username, 'mailfetch_subfolder_' . $i, $new_folder);
  238. }
  239. }
  240. }
  241. // end of hooked functions
  242. /**
  243. * hex2bin - document me
  244. */
  245. function hex2bin( $data ) {
  246. /* Original code by josh@superfork.com */
  247. $len = strlen($data);
  248. $newdata = '';
  249. for( $i=0; $i < $len; $i += 2 ) {
  250. $newdata .= pack( "C", hexdec( substr( $data, $i, 2) ) );
  251. }
  252. return $newdata;
  253. }
  254. function mf_keyED( $txt ) {
  255. global $MF_TIT;
  256. if( !isset( $MF_TIT ) ) {
  257. $MF_TIT = "MailFetch Secure for SquirrelMail 1.x";
  258. }
  259. $encrypt_key = md5( $MF_TIT );
  260. $ctr = 0;
  261. $tmp = "";
  262. for( $i = 0; $i < strlen( $txt ); $i++ ) {
  263. if( $ctr == strlen( $encrypt_key ) ) $ctr=0;
  264. $tmp.= substr( $txt, $i, 1 ) ^ substr( $encrypt_key, $ctr, 1 );
  265. $ctr++;
  266. }
  267. return $tmp;
  268. }
  269. function encrypt( $txt ) {
  270. srand( (double) microtime() * 1000000 );
  271. $encrypt_key = md5( rand( 0, 32000 ) );
  272. $ctr = 0;
  273. $tmp = "";
  274. for( $i = 0; $i < strlen( $txt ); $i++ ) {
  275. if ($ctr==strlen($encrypt_key)) $ctr=0;
  276. $tmp.= substr($encrypt_key,$ctr,1) .
  277. (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));
  278. $ctr++;
  279. }
  280. return bin2hex( mf_keyED( $tmp ) );
  281. }
  282. function decrypt( $txt ) {
  283. $txt = mf_keyED( hex2bin( $txt ) );
  284. $tmp = '';
  285. for ( $i=0; $i < strlen( $txt ); $i++ ) {
  286. $md5 = substr( $txt, $i, 1 );
  287. $i++;
  288. $tmp.= ( substr( $txt, $i, 1 ) ^ $md5 );
  289. }
  290. return $tmp;
  291. }
  292. /**
  293. * check mail folder
  294. * @param stream $imap_stream imap connection resource
  295. * @param string $imap_folder imap folder name
  296. * @return boolean true, when folder can be used to store messages.
  297. * @since 1.5.1 and 1.4.5
  298. */
  299. function mail_fetch_check_folder($imap_stream,$imap_folder) {
  300. global $mail_fetch_allow_unsubscribed;
  301. // check if folder is subscribed or only exists.
  302. if (sqimap_mailbox_is_subscribed($imap_stream,$imap_folder)) {
  303. $ret = true;
  304. } elseif ($mail_fetch_allow_unsubscribed && sqimap_mailbox_exists($imap_stream,$imap_folder)) {
  305. $ret = true;
  306. } else {
  307. $ret = false;
  308. }
  309. // make sure that folder can store messages
  310. if ($ret && mail_fetch_check_noselect($imap_stream,$imap_folder)) {
  311. $ret = false;
  312. }
  313. return $ret;
  314. }
  315. /**
  316. * Checks if folder is noselect (can't store messages)
  317. *
  318. * Function does not check if folder subscribed.
  319. * @param stream $imap_stream imap connection resource
  320. * @param string $imap_folder imap folder name
  321. * @return boolean true, when folder has noselect flag. false in any other case.
  322. * @since 1.5.1 and 1.4.5
  323. */
  324. function mail_fetch_check_noselect($imap_stream,$imap_folder) {
  325. $boxes=sqimap_mailbox_list($imap_stream);
  326. foreach($boxes as $box) {
  327. if ($box['unformatted']==$imap_folder) {
  328. return (bool) check_is_noselect($box['raw']);
  329. }
  330. }
  331. return false;
  332. }
  333. ?>