Subversion Repositories Applications.projet

Rev

Rev 424 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
431 mathias 1
<?php
2
// $Id: ezmlm-threads.php,v 1.6 2008-08-25 15:19:15 alexandre_tb Exp $
3
//
4
// ezmlm-threads.php - ezmlm-php v2.0
5
// --------------------------------------------------------------
6
// Builds, maintains & displays thread caches
7
// These cache files live in $ezmlm->tmpdir and are serialized
8
// php objects that can be unserialized and displayed easily
9
// --------------------------------------------------------------
10
 
11
require_once("ezmlm.php");
12
require_once("ezmlm-parser.php");
13
require_once ('ezmlm-langue-fr.php');
14
 
15
// CLASS: ezmlm_threads will build, maintain & display thread files (even if a thread is only 1 msg)
16
class ezmlm_threads extends ezmlm_php {
17
 
18
	// load: this is the main function that should be called.
19
	// it first checks to see if the cache files are stale, if they are it calls build
20
	// other wise it loads them and calls display
21
	function load($month) {
22
		if (!is_dir($this->tempdir . "/ezmlm-php-" . $this->listname)) {
23
			$checksum = $this->tempdir . "/ezmlm-php-" . $this->listname . "-" . $month . "-" . "checksum";
24
		} else {
25
			$checksum = $this->tempdir . "/ezmlm-php-" . $this->listname . "/" . $month . "-" . "checksum";
26
		}
27
        $md5 = '' ;
28
		if (!is_file($checksum)) {
29
			$this->build($month);
30
		} else {
31
			$fd = fopen($checksum,"r");
32
			while (!preg_match('/^md5:/', $md5)) { $md5 = fgets($fd,4096); }
33
			fclose($fd);
34
			$md5 = rtrim(preg_replace('/^md5:/', '', $md5), "\n");
35
			if ($md5 != $this->md5_of_file($this->listdir . "/archive/threads/" . $month)) {
36
				print "<!-- $md5 ne " . $this->md5_of_file($this->listdir . "/archive/threads/" . $month) . " -->\n";
37
				$this->build($month);
38
			}
39
		}
40
		$html = $this->display($month);
41
		return $html ;
42
	}
43
 
44
	// display: this loads each cache file sequentially and displays the messages in them
45
	// there is no checking of checksum's done here so load() is the preferred method to
46
	// view the threads
47
	function display($month) {
48
		$html = '' ;
49
		$seq = 0;
50
		if (!is_dir($this->tempdir . "/ezmlm-php-" . $this->listname)) {
51
			$cache = $this->tempdir . "/ezmlm-php-" . $this->listname . "-" . $month;
52
		} else {
53
			$cache = $this->tempdir . "/ezmlm-php-" . $this->listname . "/" . $month;
54
		}
55
        // Le lien par date et par thread
56
        $html .= '[ '.$this->makelink('action=show_month&amp;actionargs[]='.$month, 'par date').' ]' ;
57
		$months = array(1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August',
58
		9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December');
59
        // remplac� par le tableau globals $mois dans ezmlm.php
60
		$html .= '<h2>'.FIL_DE_DISCUSSION.' pour '.$GLOBALS['mois'][((int)substr($month,4,2) / 1) -1] .', ' . substr($month,0,4) . '</h2>'."\n";
61
 
62
		$html .= '<table class="table_cadre">'."\n";
63
		$html .= '<tr><th>Num</th><th>De</th><th>Sujet</th><th>Date</th></tr>'."\n";
64
		$html .= '<tr><td colspan="3"><hr /></td></tr>'."\n";
65
		$ctc .= 0;
66
 
67
		if (is_file($cache)) {
68
			include($cache);
69
		}
70
		$html .= '<tr><td colspan="3"></td></tr>'."\n";
71
        $html .= '</table>'."\n";
72
 
73
        return $html ;
74
        }
75
 
76
 
77
	function thread_to_html($thread) {
78
		$html = '';
79
		$lastdepth = -1;
80
        $ctc = 0 ;
81
		$thread_curr = $thread;
82
        $class = array ('ligne_paire', 'ligne_impaire') ;
83
		while ($thread_curr != NULL) {
84
            preg_match ('!/archive/([0-9]*)/([0-9]*)!', $thread_curr->file, $match) ;
85
            if (!isset($GLOBALS['fichiers_analyses'][$match[1]][$match[2]])) {
86
                $message = file_get_contents($this->listdir . "/archive/" . $msgdir . "/" . $msgfile) ;
87
                $mimeDecode = new Mail_mimeDecode($message) ;
88
                $mailDecode = $mimeDecode->decode() ;
89
                //$msg = new ezmlm_parser();
90
                //$msg->parse_file($this->listdir . $thread_curr->file, TRUE);
91
 
92
            } else {
93
                $mailDecode = $GLOBALS['fichiers_analyses'][$match[1]][$match[2]] ;
94
            }
95
            $actionargs = preg_split("/\//", $thread_curr->file);
96
			$html .= '<tr class="'.$class[$ctc].'">'."\n";
97
			$html .= '<td>'.$actionargs[2].$actionargs[3].'</td><td>';
98
			$html .= $this->makelink('action=show_author_msgs&amp;actionargs[]='.
99
                    $this->makehash($this->decode_iso($mailDecode->headers['from'])),$this->decode_iso($this->protect_email($mailDecode->headers['from'],TRUE)));
100
			$html .= '</td>'."\n";
101
			$html .= '<td><b>';
102
			//$html .= " <a name=\"" . urlencode($thread_curr->file) . "\">";    A quoi �a sert ?
103
			for ($i = 0; $i < $thread_curr->depth; $i++) {
104
				$html .= "&nbsp;&nbsp;";
105
			}
106
			if (($this->thread_subjlen > 0) and (strlen($this->decode_iso($mailDecode->headers['subject'])) > $this->thread_subjlen)) {
107
				$subject = substr($this->decode_iso($mailDecode->headers['subject']), 0, ($this->thread_subjlen - 3 - ($thread_curr->depth * 2)));
108
				$subject = $subject . "...";
109
			} else {
110
				$subject = $this->decode_iso($mailDecode->headers['subject']);
111
			}
112
 
113
 
114
			$subject = preg_replace("/\[" . $this->listname . "\]/", "", $subject);
115
			$html .= $this->makelink("action=show_msg&amp;actionargs[]=" . $actionargs[2] . "&amp;actionargs[]=" . $actionargs[3], $subject);
116
			$html .= "</b></td>\n";
117
			$html .= '<td>' .$this->date_francaise($mailDecode->headers['date']).'</td>'."\n";
118
			$html .= "</tr>\n";
119
 
120
			$ctc++;
121
			if ($ctc == count($this->tablecolours)) { $ctc = 0; }
122
 
123
			$lastdepth = $thread_curr->depth;
124
			$thread_curr = $thread_curr->next;
125
		}
126
 
127
		$html .= '<tr><td colspan="3"><hr noshade size="1" /></td></tr>'."\n";
128
		return $html;
129
	}
130
 
131
	// build: takes one argument in the format YYYYMM and builds the thread cache file
132
	// for that month if the ezmlm thread file exists. The resulting cache file is then
133
	// stored in $this->tmpdir;
134
	function build($month) {
135
		if (!is_file($this->listdir . "/archive/threads/" . $month)) { return FALSE; }
136
 
137
		if (!is_dir($this->tempdir . "/ezmlm-php-" . $this->listname)) {
138
                        $fd2 = fopen($this->tempdir . "/ezmlm-php-" . $this->listname . "-" . $month,"w+");
139
                } else {
140
                        $fd2 = fopen($this->tempdir . "/ezmlm-php-" . $this->listname . "/" . $month,"w+");
141
                }
142
		fclose($fd2);
143
        $i=0;
144
        // ouverture du fichier thread de ezmlm
145
        // Ils sont class�s mois par mois
146
		$fd1 = fopen($this->listdir . "/archive/threads/" . $month, "r");
147
		while (!feof($fd1)) {
148
			$line = fgets($fd1,4096);
149
			if (preg_match('/^[0-9]*\:[a-z]* \[.*/', $line)) {
150
				// valid ezmlm thread file entry
151
 
152
                // On place dans $subjectfile le chemin vers le fichier sujet
153
				$subjectfile = preg_replace("/^[0-9]*\:([a-z]*) \[.*/", "\\1", $line);
154
                $subjectfile = substr($subjectfile,0,2) . '/' . substr($subjectfile,2,18);
155
 
156
				$thread_head = NULL;
157
				$thread_curr = NULL;
158
				$thread_temp = NULL;
159
				$thread_depth = 1;
160
 
161
				if (!is_file($this->listdir . "/archive/subjects/" . $subjectfile)) { continue; }
162
                // on ouvre le fichier sujet
163
                // Celui-ci contient sur la premi�re ligne le hash du sujet puis le sujet
164
                // sur les autres lignes :
165
                // num_message:ann�emois:hash_auteur nom_auteur
166
				$fd2 = fopen($this->listdir . "/archive/subjects/" . $subjectfile, "r");
167
				while (!feof($fd2)) {
168
					$line2 = fgets($fd2,4096);
169
					if (preg_match('/^[0-9]/',$line2)) {
170
						$msgnum = preg_replace('/^([0-9]*):.*/', '\\1', $line2);
171
						$msgfile = $msgnum % 100;
172
                        $msgdir  = (int)($msgnum / 100);
173
						if ($msgfile < 10) { $msgfile = "0" . $msgfile; }
174
						//$msg = new ezmlm_parser();
175
						//$msg->parse_file_headers($this->listdir . "/archive/" . $msgdir . "/" . $msgfile, TRUE);
176
 
177
                        $message = file_get_contents($this->listdir . "/archive/" . $msgdir . "/" . $msgfile) ;
178
                        $mimeDecode = new Mail_mimeDecode($message) ;
179
                        $mailDecode = $mimeDecode->decode() ;
180
 
181
 
182
 
183
                        // On stocke le fichier analys�e pour r�utilisation ult�rieure
184
                        $GLOBALS['fichiers_analyses'][$msgdir][$msgfile] =  $mailDecode ;
185
						$msgid = (isset ($mailDecode->headers['message-id']) ? $mailDecode->headers['message-id'] : '');
186
						$inreply = (isset($mailDecode->headers['in-reply-to']) ? $mailDecode->headers['in-reply-to'] : '');
187
						$references = (isset ($mailDecode->headers['references']) ? $mailDecode->headers['references'] : '') ;
188
						$thread_depth = 1;
189
 
190
						if ($thread_head == NULL) {
191
							$thread_head = new ezmlm_thread(0,'/archive/' . $msgdir . '/' . $msgfile,$msgid);
192
						} else {
193
							$thread_curr = new ezmlm_thread($depth,'/archive/' . $msgdir . '/' . $msgfile,$msgid);
194
							if ($inreply != '') { $thread_curr->inreply = $inreply; }
195
							if ($references != '') { $thread_curr->references = $references; }
196
							$thread_head->append($thread_curr);
197
						}
198
					}
199
				}
200
				fclose($fd2);
201
 
202
				// so now after all that mess $thread_head contains a full thread tree
203
				// first build the depth of each message based on 'in-reply-to' and 'references'
204
				unset($thread_temp);
205
				$thread_temp = NULL;
206
				$thread_curr =& $thread_head->next;
207
				while (get_class($thread_curr) == 'ezmlm_thread') {
208
					unset($thread_temp);
209
					$thread_temp = NULL;
210
 
211
					if ($thread_curr->inreply != '') { $thread_temp =& $thread_head->find_msgid($thread_curr->inreply); }
212
					if ($thread_temp == NULL) {
213
						if ($thread_curr->references != '') {
214
							$refs = preg_split('/ /', $thread_curr->references);
215
							$refs = array_pop($refs);
216
							$thread_temp =& $thread_head->find_msgid($refs);
217
						}
218
					}
219
					if ($thread_temp == NULL) {
220
						// we couldn't find anything... set depth to 1, the default
221
						$thread_curr->depth = 1;
222
					} else {
223
						// we found a reference, set it to it's depth + 1
224
						$thread_curr->depth = $thread_temp->depth + 1;
225
					}
226
					$thread_curr =& $thread_curr->next;
227
				}
228
 
229
				// now write it to a temp file named MONTH-SEQ where seq is cronologic sequence order of the thread.
230
				if (!is_dir($this->tempdir . "/ezmlm-php-" . $this->listname)) {
231
					@mkdir($this->tempdir . "/ezmlm-php-" . $this->listname, 0755);
232
					if (!is_dir($this->tempdir . "/ezmlm-php-" . $this->listname)) {
233
						$fd2 = fopen($this->tempdir . "/ezmlm-php-" . $this->listname . "-" . $month, "a");
234
					} else {
235
						$fd2 = fopen($this->tempdir . "/ezmlm-php-" . $this->listname . "/" . $month, "a");
236
					}
237
				} else {
238
					$fd2 = fopen($this->tempdir . "/ezmlm-php-" . $this->listname . "/" . $month, "a");
239
				}
240
				fputs($fd2,$this->thread_to_html($thread_head));
241
				fclose($fd2);
242
			}
243
		}
244
 
245
		// finally store our checksum
246
		if (!is_dir($this->tempdir . "/ezmlm-php-" . $this->listname)) {
247
			$fd2 = fopen($this->tempdir . "/ezmlm-php-" . $this->listname . "-" . $month . "-" . "checksum","w+");
248
		} else {
249
			$fd2 = fopen($this->tempdir . "/ezmlm-php-" . $this->listname . "/" . $month . "-" . "checksum","w+");
250
		}
251
		fputs($fd2,"md5:" . $this->md5_of_file($this->listdir . "/archive/threads/" . $month) . "\n");
252
		fclose($fd2);
253
		fclose($fd1);
254
 
255
		return TRUE;
256
	}
257
 
258
	// listmessages: prints out a nice little calendar and displays the message
259
	// totals for each month. The link jumps to the thread listing.
260
    // On lit le repetoire archive/threads/ qui contient un fichier par moi avec tous les thread, par sujet
261
    // Presentes comme suit
262
    // num_thread:hash [taille_du_thread] Sujet du thread (le dernier)
263
    // les messages sont ranges par leur numero
264
	function listmessages() {
265
        if (!is_dir($this->listdir . "/archive/threads/")) {
266
            return false ;
267
        }
268
 
269
        $res = '<table id="petit_calendrier">'."\n";
270
        $res .= " <tr>\n";
271
		$res .= "  <td></td>" ;
272
        foreach ($GLOBALS['mois'] as $valeur) $res .= '<th>'.$valeur.'</th>' ;
273
		$res .=" </tr>\n";
274
        $res .= $this->calendrierMessage();
275
        $res .= "</table>\n";
276
        return $res;
277
        /*
278
        $threadcount = array();
279
 
280
		$repertoire_archive = opendir($this->listdir . "/archive/");
281
		$tableau_annee = array();
282
		while (false !== ($item = readdir($repertoire_archive))) {
283
			// $item contient les noms des repertoires
284
			// on ne garde que ceux qui sont des chiffres
285
 
286
			if (preg_match('/[0-9]+/', $item)) {
287
				// on ouvre le fichier d index de chaque repertoire
288
				$fichier_index = fopen($this->listdir.'/archive/'.$item.'/index', 'r');
289
				$compteur = 0 ;
290
 
291
				while (!feof($fichier_index)) {
292
					// On ignore la premiere ligne
293
					$temp = fgets($fichier_index, 4096);
294
					// dans la seconde on recupere la date
295
					$temp = fgets($fichier_index, 4096);
296
					preg_match('/\t([0-9]+) ([a-zA-Z][a-zA-Z][a-zA-Z]) ([0-9][0-9][0-9][0-9])/', $temp, $match) ;
297
					if ($match[0] != '') {
298
 
299
						$threadmonth = date('n', strtotime($match[0]))  ;
300
						$threadyear = date('Y', strtotime($match[0])) ;
301
						$threadcount[$threadyear][$threadmonth]++;
302
						if (!in_array($threadyear, $tableau_annee)) array_push ($tableau_annee, $threadyear);
303
					}
304
				}
305
				fclose ($fichier_index);
306
			}
307
		}
308
		if (count($threadcount) == 0) return 'Il n\y a pas de messages dans les archives';
309
        // La partie qui suit, simple, cree la table avec le nombre de message echange chaque mois
310
		$res = '<table id="petit_calendrier">'."\n";
311
		$res .= " <tr>\n";
312
		$res .= "  <td></td>" ;
313
        foreach ($GLOBALS['mois'] as $valeur) $res .= '<th>'.$valeur.'</th>' ;
314
		$res .=" </tr>\n";
315
		arsort($tableau_annee);
316
		foreach ($tableau_annee as $annee) {
317
			$res .= " <tr>\n";
318
			$res .= '  <td class="col_annee">'.$annee.'</td>';
319
			for ($i = 1; $i <= 12; $i++) {
320
				$res .= '<td class="mois">';
321
				if (isset($threadcount[$annee][$i]) && $threadcount[$annee][$i] > 0) {
322
                    $res .= $this->makelink('action=show_month&amp;actionargs[]='.$annee.($i < 10 ? '0'.$i:$i),$threadcount[$annee][$i]);
323
				}
324
				$res .= '</td>';
325
			}
326
			$res .= '</tr>'."\n";
327
		}
328
		$res .= "</table>\n";
329
        return $res ;
330
        */
331
	}
332
	/*
333
	 *  Cree un fichier liste.calendrierPermanent qui contient
334
	 *  le nombre de message par mois pour toutes les annees
335
	 *  depuis le debut de la liste sauf la derniere
336
	 *
337
	 */
338
	function calculeCalendrierPermanent($Annnee = '') {
339
		$numArchive = $this->getNumArchive();
340
		$dernierRepertoire = floor($numArchive / 100);
341
 
342
		$threadcount = array();
343
		$tableau_annee = array();
344
 
345
 
346
		for ($rep_courant = $dernierRepertoire; $rep_courant >= 0; $rep_courant--) {
347
			$fichier_index = file ($this->listdir.'/archive/'.$rep_courant.'/index', FILE_IGNORE_NEW_LINES);
348
 
349
			// On parcours le fichier a l envers
350
			for ($j = count($fichier_index)-1; $j >= 0; $j-=2) {
351
				preg_match('/\t([0-9]+) ([a-zA-Z][a-zA-Z][a-zA-Z]) ([0-9][0-9][0-9][0-9])/', $fichier_index[$j], $match) ;
352
				if ($match[0] != '') {
353
					$threadmonth = date('n', strtotime($match[0]))  ;
354
					$threadyear = date('Y', strtotime($match[0])) ;
355
					if ($Annnee != '') {
356
						if ($threadyear < date('Y')) {
357
							$sortir = true;
358
							break;
359
						}
360
					}  else {
361
						if ($threadyear == date('Y')) continue;
362
					}
363
					$threadcount[$threadyear][$threadmonth]++;
364
					if (!in_array($threadyear, $tableau_annee)) array_push ($tableau_annee, $threadyear);
365
				}
366
			}
367
			if ($sortir) break;
368
		}
369
		$res = '';
370
		arsort($tableau_annee);
371
		foreach ($tableau_annee as $annee) {
372
			$res .= " <tr>\n";
373
			$res .= '  <td class="col_annee">'.$annee.'</td>';
374
			for ($i = 1; $i <= 12; $i++) {
375
				$res .= '<td class="mois">';
376
				if (isset($threadcount[$annee][$i]) && $threadcount[$annee][$i] > 0) {
377
                    $res .= $this->makelink('action=show_month&amp;actionargs[]='.$annee.($i < 10 ? '0'.$i:$i),$threadcount[$annee][$i]);
378
				}
379
				$res .= '</td>';
380
			}
381
			$res .= '</tr>'."\n";
382
		}
383
		return $res;
384
	}
385
	function ecrireFichierCalendrier() {
386
		$html = $this->calculeCalendrierPermanent();
387
		$f = fopen ('tmp/'.$this->listname.'.calendrier', 'w') ;
388
		fwrite ($f, $html);
389
		fclose($f);
390
		return $html;
391
	}
392
 
393
	function calendrierMessage() {
394
		$html = '';
395
		// On ajoute la derniere annee
396
		$html .= $this->calculeCalendrierPermanent(date ('Y'));
397
 
398
		if ($this->isFichierCalendrierExiste()) {
399
			// S il existe mais qu il est trop vieux, il faut le recalculer
400
			if ($this->isDoitRecalculerCalendrier()) {
401
				$annees = $this->getAnneesARecalculer();
402
				$html .= $this->calculeCalendrierPermanent($annees);
403
		    }
404
			$html .= file_get_contents('tmp/'.$this->listname.'.calendrier');
405
		} else {
406
			$html .= $this->ecrireFichierCalendrier();
407
		}
408
		return $html;
409
	}
410
 
411
	function isFichierCalendrierExiste() {
412
		if (file_exists('tmp/'.$this->listname.'.calendrier')) {
413
			return true;
414
		}
415
		return false;
416
	}
417
	function isDoitRecalculerCalendrier() {
418
 
419
		if (date ('Y', fileatime('tmp/'.$this->listname.'.calendrier')) != date('Y')) return true;
420
		return false;
421
	}
422
 
423
	function getAnneesARecalculer() {
424
		// On suppose que l index de ezmlm est correct
425
		$anneeFichierCalendrier = date ('Y', fileatime('tmp/'.$this->listname.'.calendrier'));
426
		return $anneeFichierCalendrier + 1;
427
	}
428
 
429
}
430
 
431
// CLASS: ezmlm-thread is a quick little class to allow us to define
432
// a structure of the current thread in a single-linked list.
433
// it's a little messy since php doesn't support pointers like C does
434
// so we have to use references and a head object to append to the list.
435
class ezmlm_thread {
436
	var $next;
437
	var $depth;
438
	var $file;
439
	var $msgid;
440
	var $inreply;
441
	var $references;
442
	function append($thread) {
443
		$thread_curr =& $this;
444
		while ($thread_curr->next != NULL) {
445
			$thread_curr =& $thread_curr->next;
446
		}
447
		$thread_curr->next = $thread;
448
	}
449
	function &find_msgid($msgid) {
450
		$thread_curr =& $this;
451
		while ($thread_curr->next != NULL) {
452
			if (trim($thread_curr->msgid) == trim($msgid)) { return $thread_curr; }
453
			$thread_curr =& $thread_curr->next;
454
		}
455
		return NULL;
456
	}
457
	function ezmlm_thread($depth,$file,$msgid) {
458
		$this->depth = $depth;
459
		$this->file  = $file;
460
		$this->msgid = $msgid;
461
		$this->next = NULL;
462
	}
463
}
464
?>