Subversion Repositories Applications.projet

Rev

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

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