Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
/**
4
 +-----------------------------------------------------------------------+
5
 | This file is part of the Roundcube Webmail client                     |
6
 |                                                                       |
7
 | Copyright (C) The Roundcube Dev Team                                  |
8
 | Copyright (C) Kolab Systems AG                                        |
9
 |                                                                       |
10
 | Licensed under the GNU General Public License version 3 or            |
11
 | any later version with exceptions for skins & plugins.                |
12
 | See the README file for a full license statement.                     |
13
 |                                                                       |
14
 | PURPOSE:                                                              |
15
 |   E-mail message headers representation                               |
16
 +-----------------------------------------------------------------------+
17
 | Author: Aleksander Machniak <alec@alec.pl>                            |
18
 +-----------------------------------------------------------------------+
19
*/
20
 
21
/**
22
 * Struct representing an e-mail message header
23
 *
24
 * @package    Framework
25
 * @subpackage Storage
26
 */
27
class rcube_message_header
28
{
29
    /**
30
     * Message sequence number
31
     *
32
     * @var int
33
     */
34
    public $id;
35
 
36
    /**
37
     * Message unique identifier
38
     *
39
     * @var int
40
     */
41
    public $uid;
42
 
43
    /**
44
     * Message subject
45
     *
46
     * @var string
47
     */
48
    public $subject;
49
 
50
    /**
51
     * Message sender (From)
52
     *
53
     * @var string
54
     */
55
    public $from;
56
 
57
    /**
58
     * Message recipient (To)
59
     *
60
     * @var string
61
     */
62
    public $to;
63
 
64
    /**
65
     * Message additional recipients (Cc)
66
     *
67
     * @var string
68
     */
69
    public $cc;
70
 
71
    /**
72
     * Message hidden recipients (Bcc)
73
     *
74
     * @var string
75
     */
76
    public $bcc;
77
 
78
    /**
79
     * Message Reply-To header
80
     *
81
     * @var string
82
     */
83
    public $replyto;
84
 
85
    /**
86
     * Message In-Reply-To header
87
     *
88
     * @var string
89
     */
90
    public $in_reply_to;
91
 
92
    /**
93
     * Message date (Date)
94
     *
95
     * @var string
96
     */
97
    public $date;
98
 
99
    /**
100
     * Message identifier (Message-ID)
101
     *
102
     * @var string
103
     */
104
    public $messageID;
105
 
106
    /**
107
     * Message size
108
     *
109
     * @var int
110
     */
111
    public $size;
112
 
113
    /**
114
     * Message encoding
115
     *
116
     * @var string
117
     */
118
    public $encoding;
119
 
120
    /**
121
     * Message charset
122
     *
123
     * @var string
124
     */
125
    public $charset;
126
 
127
    /**
128
     * Message Content-type
129
     *
130
     * @var string
131
     */
132
    public $ctype;
133
 
134
    /**
135
     * Message timestamp (based on message date)
136
     *
137
     * @var int
138
     */
139
    public $timestamp;
140
 
141
    /**
142
     * IMAP bodystructure string
143
     *
144
     * @var string
145
     */
146
    public $bodystructure;
147
 
148
    /**
149
     * IMAP body (RFC822.TEXT)
150
     *
151
     * @var string
152
     */
153
    public $body;
154
 
155
    /**
156
     * IMAP part bodies
157
     *
158
     * @var array
159
     */
160
    public $bodypart = [];
161
 
162
    /**
163
     * IMAP internal date
164
     *
165
     * @var string
166
     */
167
    public $internaldate;
168
 
169
    /**
170
     * Message References header
171
     *
172
     * @var string
173
     */
174
    public $references;
175
 
176
    /**
177
     * Message priority (X-Priority)
178
     *
179
     * @var int
180
     */
181
    public $priority;
182
 
183
    /**
184
     * Message receipt recipient
185
     *
186
     * @var string
187
     */
188
    public $mdn_to;
189
 
190
    /**
191
     * IMAP folder this message is stored in
192
     *
193
     * @var string
194
     */
195
    public $folder;
196
 
197
    /**
198
     * Other message headers
199
     *
200
     * @var array
201
     */
202
    public $others = [];
203
 
204
    /**
205
     * Message flags
206
     *
207
     * @var array
208
     */
209
    public $flags = [];
210
 
211
    /**
212
     * Extra flags (for the messages list)
213
     *
214
     * @var array
215
     * @deprecated Use $flags
216
     */
217
    public $list_flags = [];
218
 
219
    /**
220
     * Extra columns content (for the messages list)
221
     *
222
     * @var array
223
     */
224
    public $list_cols = [];
225
 
226
    /**
227
     * Message structure
228
     *
229
     * @var rcube_message_part
230
     */
231
    public $structure;
232
 
233
    /**
234
     * Message thread depth
235
     *
236
     * @var int
237
     */
238
    public $depth;
239
 
240
    /**
241
     * Whether the message has references in the thread
242
     *
243
     * @var bool
244
     */
245
    public $has_children;
246
 
247
    /**
248
     * Number of flagged children (in a thread)
249
     *
250
     * @var int
251
     */
252
    public $flagged_children;
253
 
254
    /**
255
     * Number of unread children (in a thread)
256
     *
257
     * @var int
258
     */
259
    public $unread_children;
260
 
261
    /**
262
     * UID of the message parent (in a thread)
263
     *
264
     * @var int
265
     */
266
    public $parent_uid;
267
 
268
    /**
269
     * IMAP MODSEQ value
270
     *
271
     * @var int
272
     */
273
    public $modseq;
274
 
275
    /**
276
     * IMAP ENVELOPE
277
     *
278
     * @var string
279
     */
280
    public $envelope;
281
 
282
    /**
283
     * Header name to rcube_message_header object property map
284
     *
285
     * @var array
286
     */
287
    private $obj_headers = [
288
        'date'      => 'date',
289
        'from'      => 'from',
290
        'to'        => 'to',
291
        'subject'   => 'subject',
292
        'reply-to'  => 'replyto',
293
        'cc'        => 'cc',
294
        'bcc'       => 'bcc',
295
        'mbox'      => 'folder',
296
        'folder'    => 'folder',
297
        'content-transfer-encoding' => 'encoding',
298
        'in-reply-to'               => 'in_reply_to',
299
        'content-type'              => 'ctype',
300
        'charset'                   => 'charset',
301
        'references'                => 'references',
302
        'disposition-notification-to' => 'mdn_to',
303
        'x-confirm-reading-to'      => 'mdn_to',
304
        'message-id'                => 'messageID',
305
        'x-priority'                => 'priority',
306
    ];
307
 
308
    /**
309
     * Returns header value
310
     *
311
     * @param string $name   Header name
312
     * @param bool   $decode Decode the header content
313
     *
314
     * @return string|null Header content
315
     */
316
    public function get($name, $decode = true)
317
    {
318
        $name  = strtolower($name);
319
        $value = null;
320
 
321
        if (isset($this->obj_headers[$name]) && isset($this->{$this->obj_headers[$name]})) {
322
            $value = $this->{$this->obj_headers[$name]};
323
        }
324
        else if (isset($this->others[$name])) {
325
            $value = $this->others[$name];
326
        }
327
 
328
        if ($decode && $value !== null) {
329
            if (is_array($value)) {
330
                foreach ($value as $key => $val) {
331
                    $val         = rcube_mime::decode_header($val, $this->charset);
332
                    $value[$key] = rcube_charset::clean($val);
333
                }
334
            }
335
            else {
336
                $value = rcube_mime::decode_header($value, $this->charset);
337
                $value = rcube_charset::clean($value);
338
            }
339
        }
340
 
341
        return $value;
342
    }
343
 
344
    /**
345
     * Sets header value
346
     *
347
     * @param string $name  Header name
348
     * @param string $value Header content
349
     */
350
    public function set($name, $value)
351
    {
352
        $name = strtolower($name);
353
 
354
        if (isset($this->obj_headers[$name])) {
355
            $this->{$this->obj_headers[$name]} = $value;
356
        }
357
        else {
358
            $this->others[$name] = $value;
359
        }
360
    }
361
 
362
    /**
363
     * Factory method to instantiate headers from a data array
364
     *
365
     * @param array $arr Hash array with header values
366
     *
367
     * @return rcube_message_header instance filled with headers values
368
     */
369
    public static function from_array($arr)
370
    {
371
        $obj = new rcube_message_header;
372
        foreach ($arr as $k => $v) {
373
            $obj->set($k, $v);
374
        }
375
 
376
        return $obj;
377
    }
378
}
379
 
380
 
381
/**
382
 * Class for sorting an array of rcube_message_header objects in a predetermined order.
383
 *
384
 * @package    Framework
385
 * @subpackage Storage
386
 */
387
class rcube_message_header_sorter
388
{
389
    /** @var array Message UIDs */
390
    private $uids = [];
391
 
392
 
393
    /**
394
     * Set the predetermined sort order.
395
     *
396
     * @param array $index  Numerically indexed array of IMAP UIDs
397
     */
398
    function set_index($index)
399
    {
400
        $index = array_flip($index);
401
 
402
        $this->uids = $index;
403
    }
404
 
405
    /**
406
     * Sort the array of header objects
407
     *
408
     * @param array $headers Array of rcube_message_header objects indexed by UID
409
     */
410
    function sort_headers(&$headers)
411
    {
412
        uksort($headers, [$this, "compare_uids"]);
413
    }
414
 
415
    /**
416
     * Sort method called by uksort()
417
     *
418
     * @param int $a Array key (UID)
419
     * @param int $b Array key (UID)
420
     */
421
    function compare_uids($a, $b)
422
    {
423
        // then find each sequence number in my ordered list
424
        $posa = isset($this->uids[$a]) ? intval($this->uids[$a]) : -1;
425
        $posb = isset($this->uids[$b]) ? intval($this->uids[$b]) : -1;
426
 
427
        // return the relative position as the comparison value
428
        return $posa - $posb;
429
    }
430
}