Subversion Repositories Applications.papyrus

Rev

Rev 1253 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1253 Rev 1305
1
<?php
1
<?php
-
 
2
/*vim: set expandtab tabstop=4 shiftwidth=4: */ 
-
 
3
// +------------------------------------------------------------------------------------------------------+
-
 
4
// | PHP version 4.1                                                                                      |
-
 
5
// +------------------------------------------------------------------------------------------------------+
-
 
6
// | Copyright (C) 2004 Tela Botanica (accueil@tela-botanica.org)                                         |
-
 
7
// +------------------------------------------------------------------------------------------------------+
-
 
8
// | This library is free software; you can redistribute it and/or                                        |
-
 
9
// | modify it under the terms of the GNU General Public                                                  |
-
 
10
// | License as published by the Free Software Foundation; either                                         |
-
 
11
// | version 2.1 of the License, or (at your option) any later version.                                   |
-
 
12
// |                                                                                                      |
-
 
13
// | This library is distributed in the hope that it will be useful,                                      |
-
 
14
// | but WITHOUT ANY WARRANTY; without even the implied warranty of                                       |
-
 
15
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                                    |
-
 
16
// | General Public License for more details.                                                             |
-
 
17
// |                                                                                                      |
-
 
18
// | You should have received a copy of the GNU General Public                                            |
-
 
19
// | License along with this library; if not, write to the Free Software                                  |
-
 
20
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                            |
-
 
21
// +------------------------------------------------------------------------------------------------------+
-
 
22
// CVS : $Id: ezmlm-parser.php,v 1.2.4.2 2007-04-11 12:30:50 alexandre_tb Exp $
-
 
23
/**
-
 
24
* Application projet
-
 
25
*
-
 
26
* classe ezmlm_parser pour lire les fichiers d index de ezmlm-idx
-
 
27
*
-
 
28
*@package projet
-
 
29
//Auteur original : ?? recupere dans ezmlm-php
-
 
30
*@author        Alexandre Granier <alexandre@tela-botanica.org>
-
 
31
*@copyright     Tela-Botanica 2000-2004
-
 
32
*@version       $Revision: 1.2.4.2 $
-
 
33
// +------------------------------------------------------------------------------------------------------+
-
 
34
*/
-
 
35
 
-
 
36
 
-
 
37
// +------------------------------------------------------------------------------------------------------+
-
 
38
// |                                            ENTETE du PROGRAMME                                       |
-
 
39
// +------------------------------------------------------------------------------------------------------+
-
 
40
 
2
// $Id: ezmlm-parser.php,v 1.2.4.1 2007-03-12 11:22:12 alexandre_tb Exp $
41
// $Id: ezmlm-parser.php,v 1.2.4.2 2007-04-11 12:30:50 alexandre_tb Exp $
3
//
42
//
4
// ezmlm-parser.php - ezmlm-php v2.0
-
 
5
// --------------------------------------------------------------
-
 
6
// Contains all the code for parsing messages.
-
 
7
// It handles all the nessesary decoding, attachments, etc...
-
 
8
// Note this does all the parsing itself now removing the dependancy
-
 
9
// on the mailparse library (as it looks like it will never make
-
 
10
// it into the official inclusion with PHP)...
-
 
11
// --------------------------------------------------------------
-
 
12
 
43
 
13
require_once("ezmlm.php");
44
require_once("ezmlm.php");
14
require_once("Mail/mimeDecode.php") ;
45
require_once("Mail/mimeDecode.php") ;
15
// CLASS: ezmlm-parser
46
// CLASS: ezmlm-parser
16
class ezmlm_parser extends ezmlm_php {
47
class ezmlm_parser extends ezmlm_php {
17
    var $headers;           // the full untouched headers of the message
48
    var $headers;           // the full untouched headers of the message
18
    var $body;              // the full untouched (but decoded) body (this is not $this->parts[0]->body)
49
    var $body;              // the full untouched (but decoded) body (this is not $this->parts[0]->body)
19
	var $parts;		// all the parts, if it is a multipart message. each part is an ezmlm_parser object...
50
	var $parts;		// all the parts, if it is a multipart message. each part is an ezmlm_parser object...
20
 
51
 
21
    // Here's the most accessed headers, everything else can be
52
    // Here's the most accessed headers, everything else can be
22
    // accessed from the $this->headers array.
53
    // accessed from the $this->headers array.
23
    var $to;                // To:
54
    var $to;                // To:
24
    var $from;              // From:
55
    var $from;              // From:
25
    var $date;              // Date:
56
    var $date;              // Date:
26
    var $subject;           // Subject:
57
    var $subject;           // Subject:
27
    var $replyto;           // Reply-To:
58
    var $replyto;           // Reply-To:
28
    var $contenttype;       // Content-Type:
59
    var $contenttype;       // Content-Type:
29
 
60
 
30
	var $multipart;		// TRUE if the message is a multipart message
61
	var $multipart;		// TRUE if the message is a multipart message
31
 
62
 
32
	var $msgfile;		// if parsed from a file, this is the filename...
63
	var $msgfile;		// if parsed from a file, this is the filename...
33
 
64
 
34
	// functions
65
	// functions
-
 
66
 
35
 
67
	/**
-
 
68
     * recent_msgs renvoie les derniers messages de la liste de discussion
-
 
69
     * ezmlm
-
 
70
     *
-
 
71
     * (
-
 
72
     * [0] => Array
-
 
73
     *   (
-
 
74
     *       [1] => sujet
-
 
75
     *       [2] => date en anglais
-
 
76
     *       [3] => le hash de l auteur
-
 
77
     *       [4] => l auteur
-
 
78
     *   )
-
 
79
     * [1] => ...
-
 
80
     * )
-
 
81
     * @param	int	le nombre de message a renvoye
-
 
82
     * @return array	un tableau contenant les messages
-
 
83
     * @access public
36
	// recent_msgs - parses and returns an arbitrary number of the most recent messages
84
     */
37
	function recent_msgs($show = 20, $month = "") {
85
	function recent_msgs($show = 20, $month = "") {
38
		
86
		
39
		$repertoire_archive = opendir($this->listdir . "/archive/");
87
		$repertoire_archive = opendir($this->listdir . "/archive/");
40
 
88
 
41
		$repertoire_message = array() ;
89
		$repertoire_message = array() ;
42
		$dernier_repertoire = 0 ;
90
		$dernier_repertoire = 0 ;
43
		while (false !== ($item = readdir($repertoire_archive))) {
91
		while (false !== ($item = readdir($repertoire_archive))) {
44
			// $item contient les noms des repertoires
92
			// $item contient les noms des repertoires
45
			// on ne garde que ceux qui sont des chiffres
93
			// on ne garde que ceux qui sont des chiffres
46
 
94
 
47
			if (preg_match('/[0-9]+/', $item)) {
95
			if (preg_match('/[0-9]+/', $item)) {
48
				// on ouvre le fichier d index de chaque repertoire
96
				// on ouvre le fichier d index de chaque repertoire
49
				if ((int) $item > $dernier_repertoire) $dernier_repertoire = (int) $item;
97
				if ((int) $item > $dernier_repertoire) $dernier_repertoire = (int) $item;
50
			
98
			
51
			}
99
			}
52
		}
100
		}
53
		$tableau_message = array() ;
101
		$tableau_message = array() ;
54
		$compteur_message = 0 ;
102
		$compteur_message = 0 ;
55
		$fichier_index = fopen ($this->listdir.'/archive/'.$dernier_repertoire.'/index', 'r') ;
103
		$fichier_index = fopen ($this->listdir.'/archive/'.$dernier_repertoire.'/index', 'r') ;
56
		while (!feof($fichier_index)) {
104
		while (!feof($fichier_index)) {
57
				// Recuperation du numero de message, du hash du sujet et du sujet
105
				// Recuperation du numero de message, du hash du sujet et du sujet
58
				$temp = fgets($fichier_index, 4096);
106
				$temp = fgets($fichier_index, 4096);
59
				preg_match('/([0-9]+): ([a-z]+) (.*)/', $temp, $match) ;
107
				preg_match('/([0-9]+): ([a-z]+) (.*)/', $temp, $match) ;
60
				
108
				
61
				// dans la seconde on recupere la date, hash auteur et auteur
109
				// dans la seconde on recupere la date, hash auteur et auteur
62
				$temp = fgets($fichier_index, 4096);
110
				$temp = fgets($fichier_index, 4096);
63
				preg_match('/\t([0-9]+) ([a-zA-Z][a-zA-Z][a-zA-Z]) ([0-9][0-9][0-9][0-9]) ([^;]+);(.*) (.*)/', $temp, $match_deuxieme_ligne) ;
111
				preg_match('/\t([0-9]+) ([a-zA-Z][a-zA-Z][a-zA-Z]) ([0-9][0-9][0-9][0-9]) ([^;]+);(.*) (.*)/', $temp, $match_deuxieme_ligne) ;
64
				if ($match[1] != '') {
112
				if ($match[1] != '') {
65
				$tableau_message[$match[1]] = array ($match[2], $match[3], 
113
				$tableau_message[$match[1]] = array ($match[2], $match[3], 
66
									$match_deuxieme_ligne[1].' '.$match_deuxieme_ligne[2].' '.$match_deuxieme_ligne[3], 
114
									$match_deuxieme_ligne[1].' '.$match_deuxieme_ligne[2].' '.$match_deuxieme_ligne[3], 
67
									$match_deuxieme_ligne[5], 
115
									$match_deuxieme_ligne[5], 
68
									$match_deuxieme_ligne[6]);
116
									$match_deuxieme_ligne[6]);
69
				}
117
				}
70
			}
118
			}
71
			fclose ($fichier_index);
119
			fclose ($fichier_index);
72
		// on renverse le tableau pour afficher les derniers messages en premier
120
		// on renverse le tableau pour afficher les derniers messages en premier
73
		$tableau_message = array_reverse($tableau_message, true);
121
		$tableau_message = array_reverse($tableau_message, true);
74
		
122
		
75
		// On compte le nombre de message, s il est inferieur $show et que l on est
123
		// On compte le nombre de message, s il est inferieur $show et que l on est
76
		// pas dans le premier index, on ouvre le fichier precedent et recupere
124
		// pas dans le premier index, on ouvre le fichier precedent et recupere
77
		// le n dernier message
125
		// le n dernier message
78
		
126
		
79
		if (count ($tableau_message) < $show && $dernier_repertoire != '0') {
127
		if (count ($tableau_message) < $show && $dernier_repertoire != '0') {
80
			$avant_dernier_repertoire = $dernier_repertoire - 1 ;
128
			$avant_dernier_repertoire = $dernier_repertoire - 1 ;
81
			// On utilise file_get_contents pour renverser le fichier
129
			// On utilise file_get_contents pour renverser le fichier
82
			$fichier_index = array_reverse(
130
			$fichier_index = array_reverse(
83
									explode ("\n", 
131
									explode ("\n", 
84
										preg_replace ('/\n$/', '', 
132
										preg_replace ('/\n$/', '', 
85
											file_get_contents ($this->listdir.'/archive/'.$avant_dernier_repertoire.'/index')) ), true) ;
133
											file_get_contents ($this->listdir.'/archive/'.$avant_dernier_repertoire.'/index')) ), true) ;
86
			reset ($fichier_index);
134
			reset ($fichier_index);
87
			//var_dump ($fichier_index);
135
			//var_dump ($fichier_index);
88
			
136
			
89
			for ($i = count ($tableau_message); $i <= $show; $i++) {
137
			for ($i = count ($tableau_message); $i <= $show; $i++) {
90
				// Recuperation du numero de message, du hash du sujet et du sujet
138
				// Recuperation du numero de message, du hash du sujet et du sujet
91
				// dans la seconde on recupere la date, hash auteur et auteur
139
				// dans la seconde on recupere la date, hash auteur et auteur
92
 
140
 
93
				preg_match('/\t([0-9]+) ([a-zA-Z][a-zA-Z][a-zA-Z]) ([0-9][0-9][0-9][0-9]) ([^;]+);(.*) (.*)/', 
141
				preg_match('/\t([0-9]+) ([a-zA-Z][a-zA-Z][a-zA-Z]) ([0-9][0-9][0-9][0-9]) ([^;]+);(.*) (.*)/', 
94
									current ($fichier_index), $match_deuxieme_ligne) ;
142
									current ($fichier_index), $match_deuxieme_ligne) ;
95
				preg_match('/([0-9]+): ([a-z]+) (.*)/', next($fichier_index), $match) ;
143
				preg_match('/([0-9]+): ([a-z]+) (.*)/', next($fichier_index), $match) ;
96
				next ($fichier_index);
144
				next ($fichier_index);
97
				
145
				
98
				if ($match[1] != '') {
146
				if ($match[1] != '') {
99
				$tableau_message[$match[1]] = array ($match[2], $match[3], 
147
				$tableau_message[$match[1]] = array ($match[2], $match[3], 
100
									$match_deuxieme_ligne[1].' '.$match_deuxieme_ligne[2].' '.$match_deuxieme_ligne[3], 
148
									$match_deuxieme_ligne[1].' '.$match_deuxieme_ligne[2].' '.$match_deuxieme_ligne[3], 
101
									$match_deuxieme_ligne[5], 
149
									$match_deuxieme_ligne[5], 
102
									$match_deuxieme_ligne[6]);
150
									$match_deuxieme_ligne[6]);
103
				}
151
				}
104
			}
152
			}
-
 
153
		} else {
-
 
154
			// Si le nombre de message est > $show on limite le tableau de retour
-
 
155
			$tableau_message = array_slice($tableau_message, 0, $show, true);
105
		}
156
		}
106
			
157
			
107
		
158
		
108
		return ($tableau_message) ;
159
		return $tableau_message ;
109
	}
160
	}
110
 
161
 
111
 
162
 
112
	// parse_file - opens a file and feeds the data to parse, file can be relative to the listdir
163
	// parse_file - opens a file and feeds the data to parse, file can be relative to the listdir
113
	function parse_file($file,$simple = FALSE) {
164
	function parse_file($file,$simple = FALSE) {
114
		if (!is_file($file)) {
165
		if (!is_file($file)) {
115
			if (is_file($this->listdir . "/" . $file)) { $file = $this->listdir . "/" . $file; }
166
			if (is_file($this->listdir . "/" . $file)) { $file = $this->listdir . "/" . $file; }
116
			else if (is_file($this->listdir . "/archive/" . $file)) { $file = $this->listdir . "/archive/" . $file; }
167
			else if (is_file($this->listdir . "/archive/" . $file)) { $file = $this->listdir . "/archive/" . $file; }
117
			else { return FALSE; }
168
			else { return FALSE; }
118
		}
169
		}
119
 
170
 
120
		$this->msgfile = $file;
171
		$this->msgfile = $file;
121
        $data = '' ;
172
        $data = '' ;
122
		$fd = fopen($file, "r");
173
		$fd = fopen($file, "r");
123
		while (!feof($fd)) { $data .= fgets($fd,4096); }
174
		while (!feof($fd)) { $data .= fgets($fd,4096); }
124
		fclose($fd);
175
		fclose($fd);
125
		return $this->parse($data,$simple);
176
		return $this->parse($data,$simple);
126
	}
177
	}
127
 
178
 
128
    // parse_file_headers - ouvre un fichier et analyse les entêtes
179
    // parse_file_headers - ouvre un fichier et analyse les entêtes
129
	function parse_file_headers($file,$simple = FALSE) {
180
	function parse_file_headers($file,$simple = FALSE) {
130
		if (!is_file($file)) {
181
		if (!is_file($file)) {
131
			if (is_file($this->listdir . "/" . $file)) { $file = $this->listdir . "/" . $file; }
182
			if (is_file($this->listdir . "/" . $file)) { $file = $this->listdir . "/" . $file; }
132
			else if (is_file($this->listdir . "/archive/" . $file)) { $file = $this->listdir . "/archive/" . $file; }
183
			else if (is_file($this->listdir . "/archive/" . $file)) { $file = $this->listdir . "/archive/" . $file; }
133
			else { return FALSE; }
184
			else { return FALSE; }
134
		}
185
		}
135
 
186
 
136
		$this->msgfile = $file;
187
		$this->msgfile = $file;
137
        $data = file_get_contents ($file) ;
188
        $data = file_get_contents ($file) ;
138
        $message = file_get_contents($file) ;
189
        $message = file_get_contents($file) ;
139
        $mimeDecode = new Mail_mimeDecode($message) ;
190
        $mimeDecode = new Mail_mimeDecode($message) ;
140
        $mailDecode = $mimeDecode->decode() ;
191
        $mailDecode = $mimeDecode->decode() ;
141
        return $mailDecode ;
192
        return $mailDecode ;
142
		/*$fd = fopen($file, "r");
193
		/*$fd = fopen($file, "r");
143
		while (!feof($fd)) { $data .= fgets($fd,4096); }
194
		while (!feof($fd)) { $data .= fgets($fd,4096); }
144
		fclose($fd);*/
195
		fclose($fd);*/
145
        if ($this->_get_headers($data, $simple)) return true ;
196
        if ($this->_get_headers($data, $simple)) return true ;
146
		return false ;
197
		return false ;
147
	}
198
	}
148
 
199
 
149
	// this does all of the work (well it calls two functions that do all the work :)
200
	// this does all of the work (well it calls two functions that do all the work :)
150
	// all the decoding a part breaking follows RFC2045 (http://www.faqs.org/rfcs/rfc2045.html)
201
	// all the decoding a part breaking follows RFC2045 (http://www.faqs.org/rfcs/rfc2045.html)
151
	function parse($data,$simple = FALSE) {
202
	function parse($data,$simple = FALSE) {
152
        
203
        
153
		if (($this->_get_headers($data,$simple)) && $this->_get_body($data,$simple)) { return TRUE; }
204
		if (($this->_get_headers($data,$simple)) && $this->_get_body($data,$simple)) { return TRUE; }
154
		return FALSE;
205
		return FALSE;
155
	}
206
	}
156
 
207
 
157
	// all of these are internal functions, you shouldn't call them directly...
208
	// all of these are internal functions, you shouldn't call them directly...
158
 
209
 
159
	// _ct_parse: parse Content-Type headers -> $ct[0] = Full header, $ct[1] = Content-Type, $ct[2] ... $ct[n] = AP's
210
	// _ct_parse: parse Content-Type headers -> $ct[0] = Full header, $ct[1] = Content-Type, $ct[2] ... $ct[n] = AP's
160
	function _ct_parse() {
211
	function _ct_parse() {
161
		$instr = $this->headers['content-type'];
212
		$instr = $this->headers['content-type'];
162
		preg_replace('/\(.*\)/','',$instr); // strip rfc822 comments
213
		preg_replace('/\(.*\)/','',$instr); // strip rfc822 comments
163
		if (preg_match('/: /', $instr)) {
214
		if (preg_match('/: /', $instr)) {
164
			$ct = preg_split('/:/',trim($instr),2);
215
			$ct = preg_split('/:/',trim($instr),2);
165
			$ct = preg_split('/;/',trim($ct[1]));
216
			$ct = preg_split('/;/',trim($ct[1]));
166
		} else {
217
		} else {
167
			$ct = preg_split('/;/',trim($instr));
218
			$ct = preg_split('/;/',trim($instr));
168
		}
219
		}
169
		if (isset($ct[1])) $attrs = preg_split('/[\s\n]/',$ct[1]);
220
		if (isset($ct[1])) $attrs = preg_split('/[\s\n]/',$ct[1]);
170
		$i = 2;
221
		$i = 2;
171
		$ct[1] = $ct[0];
222
		$ct[1] = $ct[0];
172
		$ct[0] = $this->headers['content-type'];
223
		$ct[0] = $this->headers['content-type'];
173
        if (isset($attrs) && is_array($attrs)) {
224
        if (isset($attrs) && is_array($attrs)) {
174
            while (list($key, $val) = each($attrs)) {
225
            while (list($key, $val) = each($attrs)) {
175
                if ($val == '') continue;
226
                if ($val == '') continue;
176
                $ap = preg_split('/=/',$val,2);
227
                $ap = preg_split('/=/',$val,2);
177
                if (preg_match('/^"/',$ap[1])) { $ap[1] = substr($ap[1],1,strlen($ap[1])-2); }
228
                if (preg_match('/^"/',$ap[1])) { $ap[1] = substr($ap[1],1,strlen($ap[1])-2); }
178
                $ct[$i] = $ap;
229
                $ct[$i] = $ap;
179
                $i++;
230
                $i++;
180
            }
231
            }
181
        }
232
        }
182
		// are we a multipart message?
233
		// are we a multipart message?
183
		if (preg_match('/^multipart/i', $ct[1])) { $this->multipart = TRUE; }
234
		if (preg_match('/^multipart/i', $ct[1])) { $this->multipart = TRUE; }
184
 
235
 
185
		return $ct;
236
		return $ct;
186
	}
237
	}
187
 
238
 
188
	// _get_headers: pulls the headers out of the data and builds the $this->headers array
239
	// _get_headers: pulls the headers out of the data and builds the $this->headers array
189
	function _get_headers($data,$simple = FALSE) {
240
	function _get_headers($data,$simple = FALSE) {
190
		$lines = preg_split('/\n/', $data);
241
		$lines = preg_split('/\n/', $data);
191
		while (list($key, $val) = each($lines)) {
242
		while (list($key, $val) = each($lines)) {
192
			$val = trim($val);
243
			$val = trim($val);
193
			if ($val == "") break;
244
			if ($val == "") break;
194
			if (preg_match('/^From[^:].*$/', $val)) continue;	/* strips out any From lines added by the MTA */
245
			if (preg_match('/^From[^:].*$/', $val)) continue;	/* strips out any From lines added by the MTA */
195
 
246
 
196
			$hdr = preg_split('/: /', $val, 2);
247
			$hdr = preg_split('/: /', $val, 2);
197
			if (count($hdr) == 1) {
248
			if (count($hdr) == 1) {
198
				// this is a continuation of the last header (like a recieved from line)
249
				// this is a continuation of the last header (like a recieved from line)
199
				$this->headers[$last] .= $val;
250
				$this->headers[$last] .= $val;
200
			} else {
251
			} else {
201
				$this->headers[strtolower($hdr[0])] = $hdr[1];
252
				$this->headers[strtolower($hdr[0])] = $hdr[1];
202
                //echo htmlspecialchars($this->headers['from'])."<br />" ;
253
                //echo htmlspecialchars($this->headers['from'])."<br />" ;
203
				$last = strtolower($hdr[0]);
254
				$last = strtolower($hdr[0]);
204
			}
255
			}
205
		}
256
		}
206
        // ajout alex
257
        // ajout alex
207
        // pour supprimer le problème des ISO...
258
        // pour supprimer le problème des ISO...
208
        // a déplacer ailleur, et appelé avant affichage
259
        // a déplacer ailleur, et appelé avant affichage
209
        
260
        
210
        if (preg_match ('/windows-[0-9][0-9][0-9][0-9]/', $this->headers['subject'], $nombre)) {
261
        if (preg_match ('/windows-[0-9][0-9][0-9][0-9]/', $this->headers['subject'], $nombre)) {
211
            $reg_exp = $nombre[0] ;
262
            $reg_exp = $nombre[0] ;
212
        } else {
263
        } else {
213
            $reg_exp = 'ISO-8859-15?' ;
264
            $reg_exp = 'ISO-8859-15?' ;
214
        }
265
        }
215
        if (preg_match ('/UTF/i', $this->headers['subject'])) $reg_exp = 'UTF-8' ;
266
        if (preg_match ('/UTF/i', $this->headers['subject'])) $reg_exp = 'UTF-8' ;
216
        preg_match_all ("/=\?$reg_exp\?(Q|B)\?(.*?)\?=/i", $this->headers['subject'], $match, PREG_PATTERN_ORDER)  ;
267
        preg_match_all ("/=\?$reg_exp\?(Q|B)\?(.*?)\?=/i", $this->headers['subject'], $match, PREG_PATTERN_ORDER)  ;
217
        for ($i = 0; $i < count ($match[0]); $i++ ) {
268
        for ($i = 0; $i < count ($match[0]); $i++ ) {
218
            
269
            
219
                if ($match[1][$i] == 'Q') {
270
                if ($match[1][$i] == 'Q') {
220
                    $decode = quoted_printable_decode ($match[2][$i]) ;
271
                    $decode = quoted_printable_decode ($match[2][$i]) ;
221
                } elseif ($match[1][$i] == 'B') {
272
                } elseif ($match[1][$i] == 'B') {
222
                    $decode = base64_decode ($match[2][$i]) ;
273
                    $decode = base64_decode ($match[2][$i]) ;
223
                }
274
                }
224
                $decode = preg_replace ("/_/", " ", $decode) ;
275
                $decode = preg_replace ("/_/", " ", $decode) ;
225
            if ($reg_exp == 'UTF-8') {
276
            if ($reg_exp == 'UTF-8') {
226
                $decode = utf8_decode ($decode) ;
277
                $decode = utf8_decode ($decode) ;
227
            } 
278
            } 
228
            $this->headers['subject'] = str_replace ($match[0][$i], $decode, $this->headers['subject']) ;
279
            $this->headers['subject'] = str_replace ($match[0][$i], $decode, $this->headers['subject']) ;
229
        }
280
        }
230
		// sanity anyone?
281
		// sanity anyone?
231
		if (!$this->headers['content-type']) { $this->headers['content-type'] = "text/plain; charset=us-ascii"; }
282
		if (!$this->headers['content-type']) { $this->headers['content-type'] = "text/plain; charset=us-ascii"; }
232
		if (!$simple) { $this->headers['content-type'] = $this->_ct_parse(); }
283
		if (!$simple) { $this->headers['content-type'] = $this->_ct_parse(); }
233
        
284
        
234
 
285
 
235
		return TRUE;
286
		return TRUE;
236
	}
287
	}
237
 
288
 
238
	// _get_body: pulls the body out of the data and fills $this->body, decoding the data if nessesary.
289
	// _get_body: pulls the body out of the data and fills $this->body, decoding the data if nessesary.
239
	function _get_body($data,$simple = FALSE) {
290
	function _get_body($data,$simple = FALSE) {
240
		$lines = preg_split('/\n/', $data);
291
		$lines = preg_split('/\n/', $data);
241
		$doneheaders = FALSE;
292
		$doneheaders = FALSE;
242
        
293
        
243
		$data = "";
294
		$data = "";
244
		while (list($key,$val) = each($lines)) {
295
		while (list($key,$val) = each($lines)) {
245
            //echo htmlspecialchars($val)."<br>";
296
            //echo htmlspecialchars($val)."<br>";
246
			if (($val == '') and (!$doneheaders)) {
297
			if (($val == '') and (!$doneheaders)) {
247
				$doneheaders = TRUE;
298
				$doneheaders = TRUE;
248
				continue;
299
				continue;
249
			} else if ($doneheaders) {
300
			} else if ($doneheaders) {
250
				$data .= $val . "\n";
301
				$data .= $val . "\n";
251
			}
302
			}
252
		}
303
		}
253
 
304
 
254
		// now here comes the fun part... decoding.
305
		// now here comes the fun part... decoding.
255
		switch($this->headers['content-transfer-encoding']) {
306
		switch($this->headers['content-transfer-encoding']) {
256
			case 'binary':
307
			case 'binary':
257
				$this->body = $this->_cte_8bit($this->_cte_qp($this->_cte_binary($data)),$simple);
308
				$this->body = $this->_cte_8bit($this->_cte_qp($this->_cte_binary($data)),$simple);
258
				break;
309
				break;
259
 
310
 
260
			case 'base64':
311
			case 'base64':
261
				$this->body = $this->_cte_8bit($this->_cte_qp($this->_cte_base64($data)),$simple);
312
				$this->body = $this->_cte_8bit($this->_cte_qp($this->_cte_base64($data)),$simple);
262
				break;
313
				break;
263
 
314
 
264
			case 'quoted-printable':
315
			case 'quoted-printable':
265
				$this->body = $this->_cte_8bit($this->_cte_qp($data),$simple);
316
				$this->body = $this->_cte_8bit($this->_cte_qp($data),$simple);
266
				break;
317
				break;
267
 
318
 
268
			case '8bit':
319
			case '8bit':
269
				$this->body = $this->_cte_8bit($data,$simple);
320
				$this->body = $this->_cte_8bit($data,$simple);
270
				break;
321
				break;
271
 
322
 
272
			case '7bit':		// 7bit doesn't need to be decoded
323
			case '7bit':		// 7bit doesn't need to be decoded
273
			default:		// And the fall through as well...
324
			default:		// And the fall through as well...
274
				$this->body = $data;
325
				$this->body = $data;
275
				break;
326
				break;
276
		}
327
		}
277
        //echo  $this->headers['content-type'][2][1];
328
        //echo  $this->headers['content-type'][2][1];
278
        if (isset($this->headers['content-type'][2][1]) && $this->headers['content-type'][2][1] == 'UTF-8') {
329
        if (isset($this->headers['content-type'][2][1]) && $this->headers['content-type'][2][1] == 'UTF-8') {
279
                //$this->body = utf8_decode ($this->body) ;
330
                //$this->body = utf8_decode ($this->body) ;
280
                //echo quoted_printable_decode(utf8_decode ($this->body)) ;
331
                //echo quoted_printable_decode(utf8_decode ($this->body)) ;
281
        }
332
        }
282
		if ($simple) { return TRUE; }
333
		if ($simple) { return TRUE; }
283
 
334
 
284
		// if we are a multipart message then break up the parts and decode, set the appropriate variables.
335
		// if we are a multipart message then break up the parts and decode, set the appropriate variables.
285
		// here comes the best part about making ezmlm-php OOP. since each part is just really a little message
336
		// here comes the best part about making ezmlm-php OOP. since each part is just really a little message
286
		// in itself each part becomes a new parser object and all the wheels turn again... :)
337
		// in itself each part becomes a new parser object and all the wheels turn again... :)
287
		if ($this->multipart) {
338
		if ($this->multipart) {
288
            
339
            
289
			$boundary = '';
340
			$boundary = '';
290
			for ($i = 2; $i <= count($this->headers['content-type']); $i++) {
341
			for ($i = 2; $i <= count($this->headers['content-type']); $i++) {
291
				if (preg_match('/boundary/i', $this->headers['content-type'][$i][0])) {
342
				if (preg_match('/boundary/i', $this->headers['content-type'][$i][0])) {
292
					$boundary = $this->headers['content-type'][$i][1];
343
					$boundary = $this->headers['content-type'][$i][1];
293
                    
344
                    
294
				}
345
				}
295
			}
346
			}
296
			if ($boundary != '') {
347
			if ($boundary != '') {
297
				$this->_get_parts($this->body,$boundary);
348
				$this->_get_parts($this->body,$boundary);
298
			} else {
349
			} else {
299
				// whoopps... something's not right here. we were told that the message is supposed
350
				// whoopps... something's not right here. we were told that the message is supposed
300
				// to be a multipart message, yet the boundary wasn't set in the content type.
351
				// to be a multipart message, yet the boundary wasn't set in the content type.
301
				// mark the message as non multipart and add a message to the top of the body.
352
				// mark the message as non multipart and add a message to the top of the body.
302
				$this->multipart = FALSE;
353
				$this->multipart = FALSE;
303
				$this->body = "PARSER ERROR:\nWHILE PARSING THIS MESSAGE AS A MULTIPART MESSAGE AS DEFINED IN RFC2045 THE BOUNDARY IDENTIFIER WAS NOT FOUND!\nTHIS MESSAGE WILL NOT DISPLAY CORRECTLY!\n\n" . $this->body;
354
				$this->body = "PARSER ERROR:\nWHILE PARSING THIS MESSAGE AS A MULTIPART MESSAGE AS DEFINED IN RFC2045 THE BOUNDARY IDENTIFIER WAS NOT FOUND!\nTHIS MESSAGE WILL NOT DISPLAY CORRECTLY!\n\n" . $this->body;
304
			}
355
			}
305
		}
356
		}
306
 
357
 
307
		return TRUE;
358
		return TRUE;
308
	}
359
	}
309
 
360
 
310
	// _get_parts: breaks up $data into parts based on $boundary following the rfc specs
361
	// _get_parts: breaks up $data into parts based on $boundary following the rfc specs
311
	// detailed in section 5 of RFC2046 (http://www.faqs.org/rfcs/rfc2046.html)
362
	// detailed in section 5 of RFC2046 (http://www.faqs.org/rfcs/rfc2046.html)
312
	// After the parts are broken up they are then turned into parser objects and the
363
	// After the parts are broken up they are then turned into parser objects and the
313
	// resulting array of parts is set to $this->parts;
364
	// resulting array of parts is set to $this->parts;
314
	function _get_parts($data,$boundary) {
365
	function _get_parts($data,$boundary) {
315
		$inpart = -1;
366
		$inpart = -1;
316
		$lines = preg_split('/\n/', $data);
367
		$lines = preg_split('/\n/', $data);
317
        // La première partie contient l'avertissement pour les client mail ne supportant pas
368
        // La première partie contient l'avertissement pour les client mail ne supportant pas
318
        // multipart, elle est stocké dans parts[-1]
369
        // multipart, elle est stocké dans parts[-1]
319
		while(list($key,$val) = each($lines)) {
370
		while(list($key,$val) = each($lines)) {
320
			if ($val == "--" . $boundary) { $inpart++; continue; } // start of a part
371
			if ($val == "--" . $boundary) { $inpart++; continue; } // start of a part
321
			else if ($val == "--" . $boundary . "--") { break; } // the end of the last part
372
			else if ($val == "--" . $boundary . "--") { break; } // the end of the last part
322
			else { $parts[$inpart] .= $val . "\n"; }
373
			else { $parts[$inpart] .= $val . "\n"; }
323
		}
374
		}
324
        
375
        
325
		for ($i = 0; $i < count($parts) - 1; $i++) {    // On saute la première partie
376
		for ($i = 0; $i < count($parts) - 1; $i++) {    // On saute la première partie
326
			$part[$i] = new ezmlm_parser();
377
			$part[$i] = new ezmlm_parser();
327
			$part[$i]->parse($parts[$i]);
378
			$part[$i]->parse($parts[$i]);
328
			$this->parts[$i] = $part[$i];
379
			$this->parts[$i] = $part[$i];
329
            //echo $this->parts[$i]."<br>" ;
380
            //echo $this->parts[$i]."<br>" ;
330
		}
381
		}
331
        
382
        
332
	}	
383
	}	
333
 
384
 
334
	// _cte_8bit: decode a content transfer encoding of 8bit
385
	// _cte_8bit: decode a content transfer encoding of 8bit
335
	// NOTE: this function is a little bit special. Since the end result will be displayed in
386
	// NOTE: this function is a little bit special. Since the end result will be displayed in
336
	// a web browser _cte_8bit decodes ASCII characters > 127 (the US-ASCII table) into the
387
	// a web browser _cte_8bit decodes ASCII characters > 127 (the US-ASCII table) into the
337
	// html ordinal equivilant, it also ensures that the messages content-type is changed
388
	// html ordinal equivilant, it also ensures that the messages content-type is changed
338
	// to include text/html if it changes anything...
389
	// to include text/html if it changes anything...
339
	function _cte_8bit($data,$simple = FALSE) {
390
	function _cte_8bit($data,$simple = FALSE) {
340
		if ($simple) { return $data; }
391
		if ($simple) { return $data; }
341
		$changed = FALSE;
392
		$changed = FALSE;
342
		$chars = preg_split('//',$data);
393
		$chars = preg_split('//',$data);
343
		while (list($key,$val) = each($chars)) {
394
		while (list($key,$val) = each($chars)) {
344
			if (ord($val) > 127) { $out .= '&#' . ord($val) . ';'; $changed = TRUE; }
395
			if (ord($val) > 127) { $out .= '&#' . ord($val) . ';'; $changed = TRUE; }
345
			else { $out .= $val; }
396
			else { $out .= $val; }
346
		}
397
		}
347
		if ($changed) { $this->headers['content-type'][1] = 'text/html'; }
398
		if ($changed) { $this->headers['content-type'][1] = 'text/html'; }
348
		return $out;
399
		return $out;
349
	}
400
	}
350
 
401
 
351
	// _cte_binary: decode a content transfer encoding of binary
402
	// _cte_binary: decode a content transfer encoding of binary
352
	function _cte_binary($data) { return $data; }
403
	function _cte_binary($data) { return $data; }
353
 
404
 
354
	// _cte_base64: decode a content transfer encoding of base64
405
	// _cte_base64: decode a content transfer encoding of base64
355
	function _cte_base64($data) { return base64_decode($data); }
406
	function _cte_base64($data) { return base64_decode($data); }
356
 
407
 
357
	// _cte_qp: decode a content transfer encoding of quoted_printable
408
	// _cte_qp: decode a content transfer encoding of quoted_printable
358
	function _cte_qp($data) {
409
	function _cte_qp($data) {
359
		// For the time being we'll use PHP's function, it seems to work well enough.
410
		// For the time being we'll use PHP's function, it seems to work well enough.
360
		return quoted_printable_decode($data);
411
		return quoted_printable_decode($data);
361
	}
412
	}
362
    
413
    
363
}
414
}