1<?php
2/**
3 * Class for wrapping a imap_fetchheader() string
4 *
5 * Copyright 2009-2011 Tim Gerundt <tim@gerundt.de>
6 *
7 * This file is part of NOCC. NOCC is free software under the terms of the
8 * GNU General Public License. You should have received a copy of the license
9 * along with NOCC.  If not, see <http://www.gnu.org/licenses/>.
10 *
11 * @package    NOCC
12 * @license    http://www.gnu.org/licenses/ GNU General Public License
13 * @version    SVN: $Id: nocc_header.php 2861 2020-04-07 13:40:41Z oheil $
14 */
15
16/**
17 * Wrapping a imap_fetchheader() string
18 *
19 * @package    NOCC
20 */
21class NOCC_Header {
22    /**
23     * imap_fetchheader() string
24     * @var string
25     * @access private
26     */
27    private $_header;
28    /**
29     * Priority
30     * @var integer
31     * @access private
32     */
33    private $_priority;
34    /**
35     * Content-Type
36     * @var string
37     * @access private
38     */
39    private $_contenttype;
40    /**
41     * SPAM flag
42     * @var bool
43     * @access private
44     */
45    private $_spamflag;
46    /**
47     * Status (UCB POP Server)
48     * @var string
49     * @access private
50     */
51    private $_status;
52
53    /**
54     * we are using Horde/Imap library
55     * @var bool
56     * @access private
57     */
58    private $_ishorde;
59
60    /**
61     * Initialize the wrapper
62     * @param string $header imap_fetchheader() string
63     */
64    public function __construct($header, $is_horde = false) {
65	$this->_ishorde = $is_horde;
66        $this->_header = $header;
67        $this->_priority = 3;
68        $this->_contenttype = '';
69        $this->_spamflag = false;
70        $this->_status = '';
71
72        $header_lines = explode("\r\n", $header);
73        foreach ($header_lines as $header_line) { //for all header lines...
74            $header_field = explode(':', $header_line);
75            switch (strtolower($header_field[0])) {
76                case 'x-priority':
77                case 'importance':
78                case 'priority':
79                    $this->_priority = $this->_parsePriority($header_field[1]);
80                    break;
81                case 'content-type':
82                    $content_type = explode(';', $header_field[1]);
83                    $this->_contenttype = trim($content_type[0]);
84                    break;
85                case 'x-spam-flag': //SpamAssassin (default)
86                case 'x-virus-status': //ClamAV
87                    $value = strtolower(trim($header_field[1]));
88                    if ($value == 'yes') {
89                        $this->_spamflag = true;
90                    }
91                    break;
92                case 'x-kasspamfilter': //all-inkl.com
93                    $this->_spamflag = true;
94                    break;
95                case 'status':
96                    $this->_status = trim($header_field[1]);
97                    break;
98            }
99        }
100    }
101
102    /**
103     * Get the RFC2822 format header from the mail
104     * @return string RFC2822 format header
105     */
106    public function getHeader() {
107        return $this->_header;
108    }
109
110    /**
111     * Get the priority from the mail
112     * @return integer Priority
113     */
114    public function getPriority() {
115        return $this->_priority;
116    }
117
118    /**
119     * Get the (translated) priority text from the mail
120     * @return string Priority text
121     */
122    public function getPriorityText() {
123        global $html_highest, $html_high, $html_normal, $html_low, $html_lowest;
124
125        switch ($this->_priority) {
126            case 1: return $html_highest; break;
127            case 2: return $html_high; break;
128            case 3: return $html_normal; break;
129            case 4: return $html_low; break;
130            case 5: return $html_lowest; break;
131            default: return '';
132        }
133    }
134
135    /**
136     * Get the Content-Type from the mail
137     * @return string Content-Type
138     */
139    public function getContentType() {
140        return $this->_contenttype;
141    }
142
143    /**
144     * Has SPAM flag?
145     * @return bool Has SPAM flag?
146     */
147    public function hasSpamFlag() {
148        return $this->_spamflag;
149    }
150
151    /**
152     * Get the status (UCB POP Server) from the mail
153     * @return string Status (UCB POP Server)
154     */
155    public function getStatus() {
156        return $this->_status;
157    }
158
159    /**
160     * Normalise the different Priority headers into a uniform value,
161     * namely that of the X-Priority header (1, 3, 5). Supports:
162     * Priority, X-Priority, Importance.
163     * X-MS-Mail-Priority is not parsed because it always coincides
164     * with one of the other headers.
165     *
166     * @param string $sValue literal priority name
167     * @return integer
168     * @access private
169     *
170     * @copyright &copy; 2003-2007 The SquirrelMail Project Team
171     * @license http://opensource.org/licenses/gpl-license.php GNU Public License
172     */
173    private function _parsePriority($sValue) {
174        // don't use function call inside array_shift.
175        $aValue = preg_split('/\s/', trim($sValue));
176        $value = strtolower(array_shift($aValue));
177
178        if ( is_numeric($value) ) {
179            return $value;
180        }
181        if ( $value == 'urgent' || $value == 'high' ) {
182            return 2;
183        } elseif ( $value == 'non-urgent' || $value == 'low' ) {
184            return 4;
185        }
186        // default is normal priority
187        return 3;
188    }
189}
190?>
191