removing includes that are loaded by validate.php.
[squirrelmail.git] / plugins / mail_fetch / functions.php
1 <?php
2
3 /**
4 * mail_fetch/functions.php
5 *
6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Functions for the mailfetch plugin.
10 *
11 * Original code from LexZEUS <lexzeus@mifinca.com>
12 * and josh@superfork.com (extracted from php manual)
13 * Adapted for MailFetch by Philippe Mingo <mingo@rotedic.com>
14 *
15 * @version $Id$
16 * @package plugins
17 * @subpackage mail_fetch
18 */
19
20 /** declare plugin globals */
21 global $mail_fetch_allow_unsubscribed;
22
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
30 /**
31 * hex2bin - document me
32 */
33 function hex2bin( $data ) {
34
35 /* Original code by josh@superfork.com */
36
37 $len = strlen($data);
38 $newdata = '';
39 for( $i=0; $i < $len; $i += 2 ) {
40 $newdata .= pack( "C", hexdec( substr( $data, $i, 2) ) );
41 }
42 return $newdata;
43 }
44
45 function mf_keyED( $txt ) {
46
47 global $MF_TIT;
48
49 if( !isset( $MF_TIT ) ) {
50 $MF_TIT = "MailFetch Secure for SquirrelMail 1.x";
51 }
52
53 $encrypt_key = md5( $MF_TIT );
54 $ctr = 0;
55 $tmp = "";
56 for( $i = 0; $i < strlen( $txt ); $i++ ) {
57 if( $ctr == strlen( $encrypt_key ) ) $ctr=0;
58 $tmp.= substr( $txt, $i, 1 ) ^ substr( $encrypt_key, $ctr, 1 );
59 $ctr++;
60 }
61 return $tmp;
62 }
63
64 function encrypt( $txt ) {
65
66 srand( (double) microtime() * 1000000 );
67 $encrypt_key = md5( rand( 0, 32000 ) );
68 $ctr = 0;
69 $tmp = "";
70 for( $i = 0; $i < strlen( $txt ); $i++ ) {
71 if ($ctr==strlen($encrypt_key)) $ctr=0;
72 $tmp.= substr($encrypt_key,$ctr,1) .
73 (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));
74 $ctr++;
75 }
76 return bin2hex( mf_keyED( $tmp ) );
77
78 }
79
80 function decrypt( $txt ) {
81
82 $txt = mf_keyED( hex2bin( $txt ) );
83 $tmp = '';
84 for ( $i=0; $i < strlen( $txt ); $i++ ) {
85 $md5 = substr( $txt, $i, 1 );
86 $i++;
87 $tmp.= ( substr( $txt, $i, 1 ) ^ $md5 );
88 }
89 return $tmp;
90 }
91
92 /**
93 * check mail folder
94 * @param stream $imap_stream imap connection resource
95 * @param string $imap_folder imap folder name
96 * @return boolean true, when folder can be used to store messages.
97 * @since 1.5.1 and 1.4.5
98 */
99 function mail_fetch_check_folder($imap_stream,$imap_folder) {
100 global $mail_fetch_allow_unsubscribed;
101
102 // check if folder is subscribed or only exists.
103 if (sqimap_mailbox_is_subscribed($imap_stream,$imap_folder)) {
104 $ret = true;
105 } elseif ($mail_fetch_allow_unsubscribed && sqimap_mailbox_exists($imap_stream,$imap_folder)) {
106 $ret = true;
107 } else {
108 $ret = false;
109 }
110
111 // make sure that folder can store messages
112 if ($ret && mail_fetch_check_noselect($imap_stream,$imap_folder)) {
113 $ret = false;
114 }
115
116 return $ret;
117 }
118
119 /**
120 * Checks if folder is noselect (can't store messages)
121 *
122 * Function does not check if folder subscribed.
123 * @param stream $imap_stream imap connection resource
124 * @param string $imap_folder imap folder name
125 * @return boolean true, when folder has noselect flag. false in any other case.
126 * @since 1.5.1 and 1.4.5
127 */
128 function mail_fetch_check_noselect($imap_stream,$imap_folder) {
129 $boxes=sqimap_mailbox_list($imap_stream);
130 foreach($boxes as $box) {
131 if ($box['unformatted']==$imap_folder) {
132 return (bool) check_is_noselect($box['raw']);
133 }
134 }
135 return false;
136 }
137 ?>