PageRenderTime 29ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/GSoC-config/squirrelmail/plugins/mail_fetch/functions.php

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