Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
448 ddelon 1
<?php
551 alexandre_ 2
// $Id: ezmlm-threads.php,v 1.3 2005-10-17 16:21:47 alexandre_tb Exp $
448 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");
13
 
14
// CLASS: ezmlm_threads will build, maintain & display thread files (even if a thread is only 1 msg)
15
class ezmlm_threads extends ezmlm_php {
16
 
17
	// load: this is the main function that should be called.
18
	// it first checks to see if the cache files are stale, if they are it calls build
19
	// other wise it loads them and calls display
20
	function load($month) {
21
		if (!is_dir($this->tempdir . "/ezmlm-php-" . $this->listname)) {
22
			$checksum = $this->tempdir . "/ezmlm-php-" . $this->listname . "-" . $month . "-" . "checksum";
23
		} else {
24
			$checksum = $this->tempdir . "/ezmlm-php-" . $this->listname . "/" . $month . "-" . "checksum";
25
		}
26
        $md5 = '' ;
27
		if (!is_file($checksum)) {
28
			$this->build($month);
29
		} else {
30
			$fd = fopen($checksum,"r");
31
			while (!preg_match('/^md5:/', $md5)) { $md5 = fgets($fd,4096); }
32
			fclose($fd);
33
			$md5 = rtrim(preg_replace('/^md5:/', '', $md5), "\n");
34
			if ($md5 != $this->md5_of_file($this->listdir . "/archive/threads/" . $month)) {
35
				print "<!-- $md5 ne " . $this->md5_of_file($this->listdir . "/archive/threads/" . $month) . " -->\n";
36
				$this->build($month);
37
			}
38
		}
39
		$this->display($month);
40
	}
41
 
42
	// display: this loads each cache file sequentially and displays the messages in them
43
	// there is no checking of checksum's done here so load() is the preferred method to
44
	// view the threads
45
	function display($month) {
46
		$seq = 0;
47
		if (!is_dir($this->tempdir . "/ezmlm-php-" . $this->listname)) {
48
			$cache = $this->tempdir . "/ezmlm-php-" . $this->listname . "-" . $month;
49
		} else {
50
			$cache = $this->tempdir . "/ezmlm-php-" . $this->listname . "/" . $month;
51
		}
52
        // Le lien par date et par thread
53
        echo '[ '.$this->makelink('action=show_month&amp;actionargs[]='.$month, 'par date').' ]' ;
54
		$months = array(1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
55
        // remplacé par le tableau globals $mois dans ezmlm.php
56
		print '<h2>'.PROJET_FILE_DE_DISCUSSION.' pour '.$GLOBALS['mois'][((int)substr($month,4,2) / 1)] .', ' . substr($month,0,4) . '</h2>'."\n";
57
 
58
		print '<table class="table_cadre">'."\n";
59
		print '<tr><th>Num</th><th>De</th><th>Sujet</th><th>Date</th></tr>'."\n";
60
		print '<tr><td colspan="3"><hr /></td></tr>'."\n";
61
		$ctc = 0;
62
 
63
		if (is_file($cache)) {
64
			include($cache);
65
		}
66
		print '<tr><td colspan="3"></td></tr>'."\n";
67
        print '</table>'."\n";
68
        }
69
 
70
 
71
	function thread_to_html($thread) {
72
		$html = '';
73
		$lastdepth = -1;
74
        $ctc = 0 ;
75
		$thread_curr = $thread;
76
        $class = array ('ligne_paire', 'ligne_impaire') ;
77
		while ($thread_curr != NULL) {
78
            preg_match ('!/archive/([0-9]*)/([0-9]*)!', $thread_curr->file, $match) ;
79
            if (!isset($GLOBALS['fichiers_analyses'][$match[1]][$match[2]])) {
80
                $message = file_get_contents($this->listdir . "/archive/" . $msgdir . "/" . $msgfile) ;
81
                $mimeDecode = new Mail_mimeDecode($message) ;
82
                $mailDecode = $mimeDecode->decode() ;
83
                //$msg = new ezmlm_parser();
84
                //$msg->parse_file($this->listdir . $thread_curr->file, TRUE);
85
 
86
            } else {
87
                $mailDecode = $GLOBALS['fichiers_analyses'][$match[1]][$match[2]] ;
88
            }
89
            $actionargs = preg_split("/\//", $thread_curr->file);
90
			$html .= '<tr class="'.$class[$ctc].'">'."\n";
91
			$html .= '<td>'.$actionargs[2].$actionargs[3].'</td><td>';
92
			$html .= $this->makelink('action=show_author_msgs&amp;actionargs[]='.
93
                    $this->makehash($this->decode_iso($mailDecode->headers['from'])),$this->decode_iso($this->protect_email($mailDecode->headers['from'],TRUE)));
94
			$html .= '</td>'."\n";
95
			$html .= '<td><b>';
96
			//$html .= " <a name=\"" . urlencode($thread_curr->file) . "\">";    A quoi ça sert ?
97
			for ($i = 0; $i < $thread_curr->depth; $i++) {
98
				$html .= "&nbsp;&nbsp;";
99
			}
100
			if (($this->thread_subjlen > 0) and (strlen($this->decode_iso($mailDecode->headers['subject'])) > $this->thread_subjlen)) {
101
				$subject = substr($this->decode_iso($mailDecode->headers['subject']), 0, ($this->thread_subjlen - 3 - ($thread_curr->depth * 2)));
102
				$subject = $subject . "...";
103
			} else {
104
				$subject = $this->decode_iso($mailDecode->headers['subject']);
105
			}
106
 
107
 
108
			$subject = preg_replace("/\[" . $this->listname . "\]/", "", $subject);
109
			$html .= $this->makelink("action=show_msg&amp;actionargs[]=" . $actionargs[2] . "&amp;actionargs[]=" . $actionargs[3], $subject);
110
			$html .= "</b></td>\n";
111
			$html .= '<td>' .$this->date_francaise($mailDecode->headers['date']).'</td>'."\n";
112
			$html .= "</tr>\n";
113
 
114
			$ctc++;
115
			if ($ctc == count($this->tablecolours)) { $ctc = 0; }
116
 
117
			$lastdepth = $thread_curr->depth;
118
			$thread_curr = $thread_curr->next;
119
		}
120
 
121
		$html .= '<tr><td colspan="3"><hr noshade size="1" /></td></tr>'."\n";
122
		return $html;
123
	}
124
 
125
	// build: takes one argument in the format YYYYMM and builds the thread cache file
126
	// for that month if the ezmlm thread file exists. The resulting cache file is then
127
	// stored in $this->tmpdir;
128
	function build($month) {
129
		if (!is_file($this->listdir . "/archive/threads/" . $month)) { return FALSE; }
130
 
131
		if (!is_dir($this->tempdir . "/ezmlm-php-" . $this->listname)) {
132
                        $fd2 = fopen($this->tempdir . "/ezmlm-php-" . $this->listname . "-" . $month,"w+");
133
                } else {
134
                        $fd2 = fopen($this->tempdir . "/ezmlm-php-" . $this->listname . "/" . $month,"w+");
135
                }
136
		fclose($fd2);
137
        $i=0;
138
        // ouverture du fichier thread de ezmlm
139
        // Ils sont classés mois par mois
140
		$fd1 = fopen($this->listdir . "/archive/threads/" . $month, "r");
141
		while (!feof($fd1)) {
142
			$line = fgets($fd1,4096);
143
			if (preg_match('/^[0-9]*\:[a-z]* \[.*/', $line)) {
144
				// valid ezmlm thread file entry
145
 
146
                // On place dans $subjectfile le chemin vers le fichier sujet
147
				$subjectfile = preg_replace("/^[0-9]*\:([a-z]*) \[.*/", "\\1", $line);
148
                $subjectfile = substr($subjectfile,0,2) . '/' . substr($subjectfile,2,18);
149
 
150
				$thread_head = NULL;
151
				$thread_curr = NULL;
152
				$thread_temp = NULL;
153
				$thread_depth = 1;
154
 
155
				if (!is_file($this->listdir . "/archive/subjects/" . $subjectfile)) { continue; }
156
                // on ouvre le fichier sujet
157
                // Celui-ci contient sur la première ligne le hash du sujet puis le sujet
158
                // sur les autres lignes :
159
                // num_message:annéemois:hash_auteur nom_auteur
160
				$fd2 = fopen($this->listdir . "/archive/subjects/" . $subjectfile, "r");
161
				while (!feof($fd2)) {
162
					$line2 = fgets($fd2,4096);
163
					if (preg_match('/^[0-9]/',$line2)) {
164
						$msgnum = preg_replace('/^([0-9]*):.*/', '\\1', $line2);
165
						$msgfile = $msgnum % 100;
166
                        $msgdir  = (int)($msgnum / 100);
167
						if ($msgfile < 10) { $msgfile = "0" . $msgfile; }
168
						//$msg = new ezmlm_parser();
169
						//$msg->parse_file_headers($this->listdir . "/archive/" . $msgdir . "/" . $msgfile, TRUE);
170
 
171
                        $message = file_get_contents($this->listdir . "/archive/" . $msgdir . "/" . $msgfile) ;
172
                        $mimeDecode = new Mail_mimeDecode($message) ;
173
                        $mailDecode = $mimeDecode->decode() ;
174
 
175
 
176
 
177
                        // On stocke le fichier analysée pour réutilisation ultérieure
178
                        $GLOBALS['fichiers_analyses'][$msgdir][$msgfile] =  $mailDecode ;
179
						$msgid = (isset ($mailDecode->headers['message-id']) ? $mailDecode->headers['message-id'] : '');
180
						$inreply = (isset($mailDecode->headers['in-reply-to']) ? $mailDecode->headers['in-reply-to'] : '');
181
						$references = (isset ($mailDecode->headers['references']) ? $mailDecode->headers['references'] : '') ;
182
						$thread_depth = 1;
183
 
184
						if ($thread_head == NULL) {
185
							$thread_head = new ezmlm_thread(0,'/archive/' . $msgdir . '/' . $msgfile,$msgid);
186
						} else {
187
							$thread_curr = new ezmlm_thread($depth,'/archive/' . $msgdir . '/' . $msgfile,$msgid);
188
							if ($inreply != '') { $thread_curr->inreply = $inreply; }
189
							if ($references != '') { $thread_curr->references = $references; }
190
							$thread_head->append($thread_curr);
191
						}
192
					}
193
				}
194
				fclose($fd2);
195
 
196
				// so now after all that mess $thread_head contains a full thread tree
197
				// first build the depth of each message based on 'in-reply-to' and 'references'
198
				unset($thread_temp);
199
				$thread_temp = NULL;
200
				$thread_curr =& $thread_head->next;
201
				while (get_class($thread_curr) == 'ezmlm_thread') {
202
					unset($thread_temp);
203
					$thread_temp = NULL;
204
 
205
					if ($thread_curr->inreply != '') { $thread_temp =& $thread_head->find_msgid($thread_curr->inreply); }
206
					if ($thread_temp == NULL) {
207
						if ($thread_curr->references != '') {
208
							$refs = preg_split('/ /', $thread_curr->references);
209
							$refs = array_pop($refs);
210
							$thread_temp =& $thread_head->find_msgid($refs);
211
						}
212
					}
213
					if ($thread_temp == NULL) {
214
						// we couldn't find anything... set depth to 1, the default
215
						$thread_curr->depth = 1;
216
					} else {
217
						// we found a reference, set it to it's depth + 1
218
						$thread_curr->depth = $thread_temp->depth + 1;
219
					}
220
					$thread_curr =& $thread_curr->next;
221
				}
222
 
223
				// now write it to a temp file named MONTH-SEQ where seq is cronologic sequence order of the thread.
224
				if (!is_dir($this->tempdir . "/ezmlm-php-" . $this->listname)) {
225
					@mkdir($this->tempdir . "/ezmlm-php-" . $this->listname, 0755);
226
					if (!is_dir($this->tempdir . "/ezmlm-php-" . $this->listname)) {
227
						$fd2 = fopen($this->tempdir . "/ezmlm-php-" . $this->listname . "-" . $month, "a");
228
					} else {
229
						$fd2 = fopen($this->tempdir . "/ezmlm-php-" . $this->listname . "/" . $month, "a");
230
					}
231
				} else {
232
					$fd2 = fopen($this->tempdir . "/ezmlm-php-" . $this->listname . "/" . $month, "a");
233
				}
234
				fputs($fd2,$this->thread_to_html($thread_head));
235
				fclose($fd2);
236
			}
237
		}
238
 
239
		// finally store our checksum
240
		if (!is_dir($this->tempdir . "/ezmlm-php-" . $this->listname)) {
241
			$fd2 = fopen($this->tempdir . "/ezmlm-php-" . $this->listname . "-" . $month . "-" . "checksum","w+");
242
		} else {
243
			$fd2 = fopen($this->tempdir . "/ezmlm-php-" . $this->listname . "/" . $month . "-" . "checksum","w+");
244
		}
245
		fputs($fd2,"md5:" . $this->md5_of_file($this->listdir . "/archive/threads/" . $month) . "\n");
246
		fclose($fd2);
247
		fclose($fd1);
248
 
249
		return TRUE;
250
	}
251
 
252
	// listmessages: prints out a nice little calendar and displays the message
253
	// totals for each month. The link jumps to the thread listing.
254
    // On lit le répetoire archive/threads/ qui contient un fichier par moi avec tous les thread, par sujet
255
    // Présentés comme suit
256
    // num_thread:hash [taille_du_thread] Sujet du thread (le dernier)
257
    // les messages sont rangés par leur numéro
258
	function listmessages() {
259
        if (!is_dir($this->listdir . "/archive/threads/")) {
260
            return false ;
261
        }
262
		$dir = opendir($this->listdir . "/archive/threads/");
263
		while ($item = readdir($dir)) {
264
			if (($item == ".") or ($item == "..")) { continue; }
265
            // Le nom du fichier est annéemoi ex 200501 pour janvier 2005
266
 
267
			if (preg_match("/^[0-9][0-9][0-9][0-9][0-9][0-9]/",$item)) {
268
                // on ouvre chaque fichier en lecture
269
				$fd = fopen($this->listdir . "/archive/threads/" . $item, "r");
270
				$count = 0; // on initialise un compteur
271
				while(!feof($fd)) {
272
					$curthread = fgets($fd,4096);
273
					$num = preg_replace("/.*\[([0-9].*)\].*/", "\\1", $curthread); // $num contient le nbre de message du thread
274
					$count = $count + $num;
275
				}
276
				$threadyear = substr($item,0,4);
277
				$threadmonth = substr($item,4,2);
278
                // on construit un tableau à 2 entrée [année][mois] = nbre_message
279
				$threadcount[$threadyear][$threadmonth] = $count;
280
			}
281
		}
282
		closedir($dir);
283
 
284
		arsort($threadcount);       // modifié par alex, car remplace l'année par 0
285
 
286
        // La partie qui suit, simple, crée la table avec le nombre de message échangé chaque mois
551 alexandre_ 287
		$res = '<table id="petit_calendrier">'."\n";
448 ddelon 288
		$res .= " <tr>\n";
289
		$res .= "  <td></td>" ;
290
        foreach ($GLOBALS['mois'] as $valeur) $res .= '<td>'.$valeur.'</td>' ;
291
		$res .=" </tr>\n";
292
 
293
		while (list($key,$val) = each($threadcount)) {
294
			$res .= " <tr>\n";
295
			$res .= "  <td>$key</td>";
296
			for ($i = 1; $i <= 12; $i++) {
297
				if ($i < 10) { $key2 = "0" . $i; }
298
				else { $key2 = $i; }
299
				if (isset($threadcount[$key][$key2]) && $threadcount[$key][$key2] > 0) {
300
					$res .= "<td bgcolor=\"" . $this->tablecolours[0] . "\" align=center valign=\"middle\">";
301
                    $res .= $this->makelink("action=show_month&amp;actionargs[]=$key$key2",$threadcount[$key][$key2]);
302
					$res .= "</td>";
303
				} else {
304
					$res .= "<td bgcolor=\"" . $this->tablecolours[0] . "\"></td>";
305
				}
306
			}
307
			$res .= "</tr>\n";
308
		}
309
		$res .= "</table>\n";
310
        echo $res ;
311
	}
312
}
313
 
314
// CLASS: ezmlm-thread is a quick little class to allow us to define
315
// a structure of the current thread in a single-linked list.
316
// it's a little messy since php doesn't support pointers like C does
317
// so we have to use references and a head object to append to the list.
318
class ezmlm_thread {
319
	var $next;
320
	var $depth;
321
	var $file;
322
	var $msgid;
323
	var $inreply;
324
	var $references;
325
	function append($thread) {
326
		$thread_curr =& $this;
327
		while ($thread_curr->next != NULL) {
328
			$thread_curr =& $thread_curr->next;
329
		}
330
		$thread_curr->next = $thread;
331
	}
332
	function &find_msgid($msgid) {
333
		$thread_curr =& $this;
334
		while ($thread_curr->next != NULL) {
335
			if (trim($thread_curr->msgid) == trim($msgid)) { return $thread_curr; }
336
			$thread_curr =& $thread_curr->next;
337
		}
338
		return NULL;
339
	}
340
	function ezmlm_thread($depth,$file,$msgid) {
341
		$this->depth = $depth;
342
		$this->file  = $file;
343
		$this->msgid = $msgid;
344
		$this->next = NULL;
345
	}
346
}
347
?>