Subversion Repositories Applications.projet

Rev

Rev 406 | Rev 431 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 ddelon 1
<?php
359 aperonnet 2
// $Id: ezmlm-msgdisplay.php,v 1.11 2008-11-19 09:28:46 aperonnet Exp $
2 ddelon 3
//
4
// ezmlm-msgdisplay.php - ezmlm-php v2.0
5
// --------------------------------------------------------------
6
// Will parse a template (if specified) and display a message.
7
// Includes a default template.
8
// --------------------------------------------------------------
9
 
10
require_once("ezmlm.php");
11
require_once("Mail/mimeDecode.php") ;
12
 
13
class ezmlm_msgdisplay extends ezmlm_php {
14
	// our template
15
	var $msgtmpl;
16
    var $message_rendu ;
17
    var $_auth ;
18
	// display: parses a message (using ezmlm_parser) and displays it
19
	// using a template
208 neiluj 20
    var $msgfile;
2 ddelon 21
 
208 neiluj 22
    function display($msgfile) {
2 ddelon 23
        if (!is_file($msgfile)) {
208 neiluj 24
            if (is_file($this->listdir . "/" . $msgfile)) { $msgfile = $this->listdir . "/" . $msgfile; }
25
		else if (is_file($this->listdir . "/archive/" . $msgfile)) { $msgfile = $this->listdir . "/archive/" . $msgfile; }
26
		else { return FALSE; }
27
	    }
28
	$this->msgfile = $msgfile ;
2 ddelon 29
        $message = file_get_contents($msgfile) ;
208 neiluj 30
	// En cours de codage
31
	// La fonction display retourne tout simplement le source du mail
290 alexandre_ 32
	// Il n'y a plus d'analyse a ce niveau
208 neiluj 33
 
34
	return $message;
2 ddelon 35
        $mimeDecode = new Mail_mimeDecode($message) ;
36
        $mailDecode = $mimeDecode->decode(array('decode_bodies' => 'true', 'include_bodies' => 'true')) ;
37
 
38
        // $msg->msgfile contient le chemin du fichier du mail en partant de la racine
290 alexandre_ 39
        // Le point d'exclamation est le delimiteur de l'expression reguliere
2 ddelon 40
		$relfile = preg_replace('!' . $this->listdir . '!', '', $msgfile);
41
 
290 alexandre_ 42
		$a1 = preg_replace('!/archive/(.*)/.*$!', '\1', $relfile);  // $a1 contient le nom du repertoire
2 ddelon 43
		$a2 = preg_replace('!/archive/.*/(.*)$!', '\1', $relfile);  // $a2 contient le nom du fichier
44
		if (isset($mailDecode->headers['date'])) $msgtime = strtotime(preg_replace ('/CEST/', '', $mailDecode->headers['date']));
45
        $threadidx = date("Ym", $msgtime);
46
        if ($a2 <= 10) $numero_precedent = '0'.($a2 - 1) ; else $numero_precedent = ($a2 - 1) ;
47
        if ($a2 < 9) $numero_suivant = '0'.($a2 + 1) ; else $numero_suivant =  ($a2 + 1);
48
        // On teste si le message suivant existe
49
        $decoupe = explode ('/', $msgfile) ;
50
 
51
        // Les nom de fichiers sont du format :
52
        // archive/0/01
53
        // archive/0/02 ... 0/99 archive/1/01 ...
54
 
55
        $nom_fichier = $decoupe[count($decoupe)-1] ;
56
        $nom_repertoire = $decoupe[count($decoupe)-2] ;
57
        $repertoire_suivant = $nom_repertoire ; $repertoire_precedent = $nom_repertoire ;
58
        if ($nom_fichier > 8) {
59
            $fichier_suivant = $nom_fichier + 1 ;
60
            if ($nom_fichier == 99) {
61
                $fichier_suivant = '01' ;
62
                $repertoire_suivant = $nom_repertoire + 1 ;
63
            }
64
        } else {
65
            $fichier_suivant = '0'.($nom_fichier + 1) ;
66
        }
67
        if ($nom_fichier > 10) {
68
            $fichier_precedent = $nom_fichier - 1 ;
69
        } else {
70
            if ($nom_fichier == '01') {
71
                $fichier_precedent = '99' ;
72
                $repertoire_precedent = $nom_repertoire - 1 ;
73
            } else {
74
                $fichier_precedent = '0'.($nom_fichier - 1) ;
75
            }
76
        }
77
        print $this->parse_entete_mail($mailDecode) ;
78
		$this->parse_template($mailDecode, $a2, $a1);
79
        print $this->message_rendu;
208 neiluj 80
        //print '</div>' ;
2 ddelon 81
	}
82
 
83
    /**
208 neiluj 84
     * Renvoie les infos des messages suivants
85
     *
86
     *
87
    */
88
    function getInfoSuivant() {
89
		$relfile = preg_replace('!' . $this->listdir . '!', '', $this->msgfile);
90
	 	$nom_repertoire = preg_replace('!/archive/(.*)/.*$!', '\1', $relfile);
91
		$nom_fichier = preg_replace('!/archive/.*/(.*)$!', '\1', $relfile);
92
 
93
		$repertoire_suivant = $nom_repertoire;
94
 
311 alexandre_ 95
		// On recupere le numero du dernier message
96
		if (file_exists($this->listdir.'/archnum')) {
97
			$numero_dernier_message = file_get_contents($this->listdir.'/archnum');
98
		}
99
 
208 neiluj 100
		// a partir du nom du fichier
101
		// et du nom du repertoire, on reconstitue
102
		// le numero du message stocke dans le fichier d index
311 alexandre_ 103
		// le message 12 du repertoire 2 a le numero 212
208 neiluj 104
 
105
		if ($nom_repertoire == '0') {
106
			$numero_message = $nom_fichier;
107
		} else {
108
			$numero_message = $nom_repertoire.$nom_fichier ;
109
		}
110
 
111
		// On ouvre le fichier d index
112
		$fichier_index = fopen ($this->listdir.'/archive/'.$nom_repertoire.'/index', 'r');
113
 
114
 
115
		$compteur_ligne = 1;
116
		if (preg_match ('/0([1-9][0-9]*)/', $nom_fichier, $match)) {
117
			$nom_fichier = $match[1];
118
			$prefixe = '0' ;
119
		} else {
120
			$prefixe = '' ;
121
		}
122
		$prefixe = $this->prefixe_nom_message($nom_fichier);
123
		//echo $numero_message;
124
		// on cherche la ligne avec le numero du message
125
		while (!feof($fichier_index)) {
126
 
127
				$temp = fgets($fichier_index,4096);
420 raphael 128
				$x = preg_match('/(\d+): (\w+) (.*)/', $temp, $matches);
129
                array_shift($matches);
130
                list($num, $hash, $sujet) = $matches;
131
 
208 neiluj 132
				if ($num == $numero_message) {
133
 
134
					$ligne_message_precedent = $compteur_ligne -2;
135
					$temp = fgets($fichier_index, 4096);
136
					$temp = fgets($fichier_index, 4096);
137
					list ($fichier_suivant,$hash, $sujet) = split(':', $temp);
138
 
139
					// Au cas ou est au dernier message du fichier d index
140
					// il faut ouvrir le suivant
141
					if (feof($fichier_index)) {
142
						$repertoire_suivant++;
143
						if (file_exists($this->listdir.'/archive/'.$repertoire_suivant.'/index')) {
144
							$fichier_index_suivant = fopen($this->listdir.'/archive/'.$repertoire_suivant.'/index', 'r');
145
							// on recupere le numero du premier message
146
							list($fichier_suivant, $hash, $sujet) = split (':', fgets($fichier_index_suivant), 4096);
147
							fclose ($fichier_index_suivant);
148
						}
149
					}
150
 
151
					// Si le numero est > 100, il faut decouper et ne retenir
152
					// que les dizaines et unites
153
					if ($fichier_suivant >= 100) {
154
						$decimal = (string) $fichier_suivant;
155
						$numero = substr($decimal, -2) ;
156
						$fichier_suivant = $numero ;
157
					} else {
249 alexandre_ 158
						if ($fichier_suivant <= 9)$fichier_suivant = '0'.$fichier_suivant;
208 neiluj 159
					}
160
 
161
					break;
162
				}
163
 
164
				// On avance d une ligne, la 2e ligne contient date hash auteur
165
				$temp2 = fgets($fichier_index, 4096);
166
				$compteur_ligne += 2;
167
		}
168
 
169
		// On utilise $ligne_message_precedent pour recupere le num du message precedent
170
		// Si $ligne_precedent est negatif soit c le premier message de la liste
171
		// soit il faut ouvrir le repertoire precedent
172
 
173
		if ($ligne_message_precedent > 0) {
174
			$compteur = 1;
175
			rewind($fichier_index);
176
			while (!feof($fichier_index)) {
177
				$temp = fgets($fichier_index, 4096);
178
				if ($ligne_message_precedent == $compteur) {
179
					list ($fichier_precedent, $hash, $sujet) = split (':', $temp) ;
180
				}
181
				$compteur++;
182
			}
183
			// Le nom du repertoire precedent est le meme que le repertoire courant
184
			$repertoire_precedent = $nom_repertoire ;
185
		// Si $ligne_message_precedent est negatif, alors il faut ouvrir
186
		// le fichier index du repertoire precedent
187
		// si le nom du repertoire est 0, alors il n y a pas de repertoire precedent
188
		// et donc pas de message precedent
189
		} else {
190
 
191
			if ($nom_repertoire != '0') {
192
				$repertoire_precedent = $nom_repertoire -1 ;
193
				// on ouvre le fichier d index et on extraie le numero
194
				// du dernier message
195
 
196
				$fichier_index_precedent = fopen ($this->listdir.'/archive/'.$repertoire_precedent.'/index', 'r') ;
197
				while (!feof($fichier_index_precedent)) {
198
					$temp = fgets($fichier_index_precedent,4096);
199
					$ligne = split (':', $temp) ;
200
					if ($ligne[0] != '') $fichier_precedent = $ligne[0];
201
					$temp = fgets($fichier_index_precedent,4096);
202
				}
203
 
204
				fclose ($fichier_index_precedent);
205
			// on se situe dans le repertoire 0 donc pas de message precedent
206
			} else {
207
				$fichier_precedent = null;
208
				$repertoire_precedent = null;
209
			}
210
		}
211
		if ($fichier_precedent > 100) {
212
			$decimal = (string) $fichier_precedent;
213
			$numero = substr($decimal, -2) ;
214
			$fichier_precedent = $numero ;
215
		} else {
216
			if ($fichier_precedent < 10 )$fichier_precedent = '0'.$fichier_precedent;
217
		}
218
		fclose ($fichier_index);
219
		//if ($fichier_precedent != null && $fichier_precedent < 10) $fichier_precedent = '0'.$fichier_precedent;
220
 
221
		return array ('fichier_suivant' => $fichier_suivant,
222
			      'repertoire_suivant' => $repertoire_suivant,
223
			      'fichier_precedent' => $fichier_precedent,
224
			      'repertoire_precedent' => $repertoire_precedent);
225
	}
226
 
227
    /**
228
    *   analyse l'entete d'un mail pour en extraire les entête
2 ddelon 229
    *   to, from, subject, date
230
    *   met à jour la variable $this->msgtmpl
231
    *
232
    */
233
 
234
    function parse_entete_mail (&$mailDecode) {
235
        $startpos = strpos(strtolower($this->msgtmpl_entete), '<ezmlm-headers>');
208 neiluj 236
        $endpos = strpos(strtolower($this->msgtmpl_entete), '</ezmlm-headers>');
2 ddelon 237
		$headers = substr($this->msgtmpl_entete,$startpos + 15,($endpos - $startpos - 15));
238
        $headers_replace = '' ;
239
		for ($i = 0; $i < count($this->showheaders); $i++) {
208 neiluj 240
		    $val = $this->showheaders[$i];
241
		    $headers_replace .= $headers;
242
		    $hnpos = strpos(strtolower($headers_replace), '<ezmlm-header-name>');
243
		    $headers_replace = substr_replace($headers_replace, $this->header_en_francais[$val], $hnpos, 19);
244
		    $hvpos = strpos(strtolower($headers_replace), '<ezmlm-header-value');
245
            $headers_replace = $this->decode_iso ($headers_replace) ;
246
            switch ($val) {
247
            	case 'date':
248
            	$headers_replace = substr_replace($headers_replace, $this->date_francaise($mailDecode->headers[strtolower($val)]), $hvpos, 20);
249
            	break;
250
            	case 'from':
251
            	if ($mailDecode->headers[strtolower($val)] == '') $from = $mailDecode->headers['return-path'] ;
252
            		else $from = $mailDecode->headers['from'];
253
            	$headers_replace = substr_replace($headers_replace, $this->protect_email($this->decode_iso($from)), $hvpos, 20);
254
            	//$headers_replace = htmlspecialchars($headers_replace);
255
            	break;
256
            	default:
257
            	$headers_replace = substr_replace($headers_replace, $this->protect_email($this->decode_iso($mailDecode->headers[strtolower($val)])), $hvpos, 20);
2 ddelon 258
            }
259
		}
208 neiluj 260
        return substr_replace($this->msgtmpl_entete, $headers_replace, $startpos, (($endpos + 16) - $startpos));
2 ddelon 261
    }
262
 
263
 
264
	function parse_template(&$mailDecode, $numero_mail, $numero_mois, $num_part = '') {
265
        static $profondeur = array();
290 alexandre_ 266
        if ($num_part != '') array_push ($profondeur, $num_part) ;
2 ddelon 267
        $corps = '' ;
268
 
269
		if ($mailDecode->ctype_primary == 'multipart') {
270
            include_once PROJET_CHEMIN_CLASSES.'type_fichier_mime.class.php' ;
271
			for ($i = 0; $i < count($mailDecode->parts); $i++) {
272
                switch ($mailDecode->parts[$i]->ctype_secondary) {
273
                    case 'plain' :
208 neiluj 274
                    if ($mailDecode->parts[$i]->headers['content-transfer-encoding'] == '8bit') {
275
                    	$corps .= $this->_cte_8bit($mailDecode->parts[$i]->body);
256 alexandre_ 276
                    } else if ($mailDecode->parts[$i]->headers['content-transfer-encoding'] == 'quoted-printable') {
277
                    	if ($mailDecode->parts[$i]->ctype_parameters['charset'] == 'UTF-8') {
278
                    		$corps .= utf8_decode($mailDecode->parts[$i]->body);
279
                    	}
208 neiluj 280
                    }
281
                    break;
311 alexandre_ 282
                    case 'html' : $corps .= trim(strip_tags ($mailDecode->parts[$i]->body, '<br><p><a><style>'));
2 ddelon 283
                    break ;
284
                    case 'mixed' :
285
                    case 'rfc822' :
286
                    case 'alternative' :
287
                    case 'appledouble' :
288
                        $this->parse_template($mailDecode->parts[$i], $numero_mail, $numero_mois, $i) ;
289
                    break ;
290
                    case 'applefile' : continue ;
291
                    break ;
292
                    default :
293
 
294
                    if ($mailDecode->parts[$i]->ctype_secondary == 'octet-stream') {
295
                        $nom_piece_jointe = $mailDecode->parts[$i]->ctype_parameters['name'] ;
296
                        $tab = explode ('.', $nom_piece_jointe) ;
297
                        $extension = $tab[count ($tab) - 1] ;
311 alexandre_ 298
                        $mimeType = type_fichier_mime::factory($extension);
2 ddelon 299
                        $mimeType->setCheminIcone(PROJET_CHEMIN_ICONES) ;
300
                    } else {
301
                        $nom_piece_jointe = isset ($mailDecode->parts[$i]->d_parameters['filename']) ?
302
                                            $mailDecode->parts[$i]->d_parameters['filename'] : $mailDecode->parts[$i]->ctype_parameters['name'] ;
311 alexandre_ 303
                        $mimeType = new type_fichier_mime( $mailDecode->parts[$i]->ctype_primary.'/'.
2 ddelon 304
                                            $mailDecode->parts[$i]->ctype_secondary, PROJET_CHEMIN_ICONES) ;
305
                    }
311 alexandre_ 306
                    $lien = PROJET_CHEMIN_APPLI.'fichier_attache.php?nom_liste='.$this->listname.
2 ddelon 307
                                    '&actionargs[]='.$numero_mois.
308
                                    '&actionargs[]='.$numero_mail;
309
                    if (count ($profondeur) > 0) {
310
                        array_shift($profondeur) ;
311 alexandre_ 311
                        for ($j= 0; $j < count ($profondeur); $j++) $lien .= '&actionargs[]='.$profondeur[$j];
312
                    }
313
                    $lien .= '&actionargs[]='.$i ;
314
                    $corps .= '<a href="'.$lien.'">';
315
 
406 raphael 316
                    $corps .= '<img src="'.$mimeType->getCheminIcone().'" alt="'.$nom_piece_jointe.'" />&nbsp;' ;
317
                    if ($mailDecode->parts[$i]->ctype_secondary == 'jpg' || $mailDecode->parts[$i]->ctype_secondary == 'jpeg')
318
                    	$corps .= '<img src="'.$lien.'" alt="'.$nom_piece_jointe.'" />&nbsp;' ;
319
                    $corps .= $nom_piece_jointe;
2 ddelon 320
                    $corps .= '</a><br />' ;
321
                    break ;
322
                }
323
            }
324
            $this->message_rendu .= preg_replace('/<ezmlm-body>/i', $this->cleanup_body($corps,TRUE), $this->msgtmpl);
325
 
326
		} else if ($mailDecode->ctype_primary == 'message') {
327
 
328
            $this->message_rendu .= "\n".'<div class="message">'.$this->parse_entete_mail($mailDecode->parts[0]);
329
            $corps .= $this->parse_template($mailDecode->parts[0], $numero_mail, $numero_mois, 0) ;
330
            $this->message_rendu .= preg_replace('/<ezmlm-body>/i', $this->cleanup_body($corps,true), $this->msgtmpl).'</div>';
331
 
332
        } else if ($mailDecode->ctype_primary == 'application' || $mailDecode->ctype_primary == 'image'){
333
            if ($mailDecode->ctype_secondary == 'applefile') return ;
311 alexandre_ 334
            $mimeType = new type_fichier_mime( $mailDecode->ctype_primary.'/'.$mailDecode->ctype_secondary,PROJET_CHEMIN_ICONES) ;
2 ddelon 335
 
336
            if ($mimeType->getIdType() != 12) {
256 alexandre_ 337
                $corps .= '<a href="'.PROJET_CHEMIN_APPLI.'fichier_attache.php?nom_liste='.$this->listname.'&actionargs[]='.
2 ddelon 338
                                    $numero_mois.'&actionargs[]='.
339
                                    $numero_mail.'&actionargs[]='.$i.'">'.
340
                                    '<img src="'.$mimeType->getCheminIcone().'" alt="'.$mailDecode->ctype_parameters['name'].'" />&nbsp;' ;
341
                $corps .= $mailDecode->ctype_parameters['name'].'</a><br />' ;
342
 
343
                $this->message_rendu .= preg_replace('/<ezmlm-body>/i', $this->cleanup_body($corps,true), $this->msgtmpl);
344
            }
345
        } else {
346
			if (preg_match('/html/i', $mailDecode->ctype_secondary)) {
347
                $this->message_rendu .= preg_replace('/<ezmlm-body>/i', $this->cleanup_body($mailDecode->body,TRUE), $this->msgtmpl);
348
            } else {
349
                if (isset ($mailDecode->ctype_parameters['charset']) && $mailDecode->ctype_parameters['charset'] == 'UTF-8') {
350
                    $this->message_rendu .= preg_replace('/<ezmlm-body>/i', '<pre>' . utf8_decode($this->cleanup_body($mailDecode->body,TRUE)) . '</pre>', $this->msgtmpl);
351
                } else {
352
                    $this->message_rendu .= preg_replace('/<ezmlm-body>/i', '<pre>' . $this->cleanup_body($mailDecode->body,TRUE) . '</pre>', $this->msgtmpl);
353
                }
354
            }
355
		}
290 alexandre_ 356
		array_pop ($profondeur);
2 ddelon 357
	}
358
 
359
	function ezmlm_msgdisplay() {
360
		$this->ezmlm_php();
361
		if (($this->msgtemplate != "") and (is_file($this->msgtemplate))) {
362
			$fd = fopen($this->msgtemplate, "r");
363
			while (!feof($fd)) { $this->msgtmpl .= fgets($fd,4096); }
364
			fclose($fd);
365
		} else {
366
			$this->msgtmpl = '<pre>
367
<ezmlm-body>
368
</pre>
369
        ';
370
		}
371
        $this->msgtmpl_entete = '<dl><ezmlm-headers>
372
<dt><ezmlm-header-name> :</dt>
373
<dd><ezmlm-header-value></dd>
374
</ezmlm-headers>
375
</dl>' ;
376
	}
253 alexandre_ 377
 
378
		// _cte_8bit: decode a content transfer encoding of 8bit
379
	// NOTE: this function is a little bit special. Since the end result will be displayed in
380
	// a web browser _cte_8bit decodes ASCII characters > 127 (the US-ASCII table) into the
381
	// html ordinal equivilant, it also ensures that the messages content-type is changed
382
	// to include text/html if it changes anything...
383
	function _cte_8bit($data,$simple = FALSE) {
384
		if ($simple) { return $data; }
385
		$changed = FALSE;
265 alexandre_ 386
		$out = '';
253 alexandre_ 387
		$chars = preg_split('//',$data);
388
		while (list($key,$val) = each($chars)) {
389
			if (ord($val) > 127) { $out .= '&#' . ord($val) . ';'; $changed = TRUE; }
390
			else { $out .= $val; }
391
		}
392
		if ($changed) { $this->headers['content-type'][1] = 'text/html'; }
393
		return $out;
394
	}
2 ddelon 395
 
396
}