Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
    /**
1441 ariadna 212
     * Message annotations (RFC 5257)
213
     *
214
     * @var ?array
215
     */
216
    public $annotations;
217
 
218
    /**
1 efrain 219
     * Extra flags (for the messages list)
220
     *
221
     * @var array
222
     * @deprecated Use $flags
223
     */
224
    public $list_flags = [];
225
 
226
    /**
227
     * Extra columns content (for the messages list)
228
     *
229
     * @var array
230
     */
231
    public $list_cols = [];
232
 
233
    /**
234
     * Message structure
235
     *
236
     * @var rcube_message_part
237
     */
238
    public $structure;
239
 
240
    /**
241
     * Message thread depth
242
     *
243
     * @var int
244
     */
245
    public $depth;
246
 
247
    /**
248
     * Whether the message has references in the thread
249
     *
250
     * @var bool
251
     */
252
    public $has_children;
253
 
254
    /**
255
     * Number of flagged children (in a thread)
256
     *
257
     * @var int
258
     */
259
    public $flagged_children;
260
 
261
    /**
262
     * Number of unread children (in a thread)
263
     *
264
     * @var int
265
     */
266
    public $unread_children;
267
 
268
    /**
269
     * UID of the message parent (in a thread)
270
     *
271
     * @var int
272
     */
273
    public $parent_uid;
274
 
275
    /**
276
     * IMAP MODSEQ value
277
     *
278
     * @var int
279
     */
280
    public $modseq;
281
 
282
    /**
283
     * IMAP ENVELOPE
284
     *
285
     * @var string
286
     */
287
    public $envelope;
288
 
289
    /**
290
     * Header name to rcube_message_header object property map
291
     *
292
     * @var array
293
     */
294
    private $obj_headers = [
295
        'date'      => 'date',
296
        'from'      => 'from',
297
        'to'        => 'to',
298
        'subject'   => 'subject',
299
        'reply-to'  => 'replyto',
300
        'cc'        => 'cc',
301
        'bcc'       => 'bcc',
302
        'mbox'      => 'folder',
303
        'folder'    => 'folder',
304
        'content-transfer-encoding' => 'encoding',
305
        'in-reply-to'               => 'in_reply_to',
306
        'content-type'              => 'ctype',
307
        'charset'                   => 'charset',
308
        'references'                => 'references',
309
        'disposition-notification-to' => 'mdn_to',
310
        'x-confirm-reading-to'      => 'mdn_to',
311
        'message-id'                => 'messageID',
312
        'x-priority'                => 'priority',
313
    ];
314
 
315
    /**
316
     * Returns header value
317
     *
318
     * @param string $name   Header name
319
     * @param bool   $decode Decode the header content
320
     *
321
     * @return string|null Header content
322
     */
323
    public function get($name, $decode = true)
324
    {
325
        $name  = strtolower($name);
326
        $value = null;
327
 
328
        if (isset($this->obj_headers[$name]) && isset($this->{$this->obj_headers[$name]})) {
329
            $value = $this->{$this->obj_headers[$name]};
330
        }
331
        else if (isset($this->others[$name])) {
332
            $value = $this->others[$name];
333
        }
334
 
335
        if ($decode && $value !== null) {
336
            if (is_array($value)) {
337
                foreach ($value as $key => $val) {
338
                    $val         = rcube_mime::decode_header($val, $this->charset);
339
                    $value[$key] = rcube_charset::clean($val);
340
                }
341
            }
342
            else {
343
                $value = rcube_mime::decode_header($value, $this->charset);
344
                $value = rcube_charset::clean($value);
345
            }
346
        }
347
 
348
        return $value;
349
    }
350
 
351
    /**
352
     * Sets header value
353
     *
354
     * @param string $name  Header name
355
     * @param string $value Header content
356
     */
357
    public function set($name, $value)
358
    {
359
        $name = strtolower($name);
360
 
361
        if (isset($this->obj_headers[$name])) {
362
            $this->{$this->obj_headers[$name]} = $value;
363
        }
364
        else {
365
            $this->others[$name] = $value;
366
        }
367
    }
368
 
369
    /**
370
     * Factory method to instantiate headers from a data array
371
     *
372
     * @param array $arr Hash array with header values
373
     *
374
     * @return rcube_message_header instance filled with headers values
375
     */
376
    public static function from_array($arr)
377
    {
378
        $obj = new rcube_message_header;
379
        foreach ($arr as $k => $v) {
380
            $obj->set($k, $v);
381
        }
382
 
383
        return $obj;
384
    }
385
}
386
 
387
 
388
/**
389
 * Class for sorting an array of rcube_message_header objects in a predetermined order.
390
 *
391
 * @package    Framework
392
 * @subpackage Storage
393
 */
394
class rcube_message_header_sorter
395
{
396
    /** @var array Message UIDs */
397
    private $uids = [];
398
 
399
 
400
    /**
401
     * Set the predetermined sort order.
402
     *
403
     * @param array $index  Numerically indexed array of IMAP UIDs
404
     */
405
    function set_index($index)
406
    {
407
        $index = array_flip($index);
408
 
409
        $this->uids = $index;
410
    }
411
 
412
    /**
413
     * Sort the array of header objects
414
     *
415
     * @param array $headers Array of rcube_message_header objects indexed by UID
416
     */
417
    function sort_headers(&$headers)
418
    {
419
        uksort($headers, [$this, "compare_uids"]);
420
    }
421
 
422
    /**
423
     * Sort method called by uksort()
424
     *
425
     * @param int $a Array key (UID)
426
     * @param int $b Array key (UID)
427
     */
428
    function compare_uids($a, $b)
429
    {
430
        // then find each sequence number in my ordered list
431
        $posa = isset($this->uids[$a]) ? intval($this->uids[$a]) : -1;
432
        $posb = isset($this->uids[$b]) ? intval($this->uids[$b]) : -1;
433
 
434
        // return the relative position as the comparison value
435
        return $posa - $posb;
436
    }
437
}