This file is indexed.

/usr/share/horde/whups/lib/Mail.php is in php-horde-whups 3.0.9-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
/**
 * Whups mail processing library.
 *
 * Copyright 2004-2016 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file LICENSE for license information (BSD). If you
 * did not receive this file, see http://www.horde.org/licenses/bsdl.php.
 *
 * @author  Jason M. Felice <jason.m.felice@gmail.com>
 * @author  Jan Schneider <jan@horde.org>
 * @package Whups
 */
class Whups_Mail
{
    /**
     * Parse a MIME message and create a new ticket.
     *
     * @param string $text       This is the full text of the MIME message.
     * @param array $info        An array of information for the new ticket.
     *                           This should include:
     *                           - 'queue'    => queue id
     *                           - 'type'     => type id
     *                           - 'state'    => state id
     *                           - 'priority' => priority id
     *                           - 'ticket'   => ticket id (prevents creation
     *                                           of new tickets)
     * @param string $auth_user  This will be the Horde user that creates the
     *                           ticket. If null, we will try to deduce from
     *                           the message's From: header. We do NOT default
     *                           to $GLOBALS['registry']->getAuth().
     *
     * @return Whups_Ticket  Ticket.
     */
    static public function processMail($text, array $info, $auth_user = null)
    {
        global $conf;

        $message = Horde_Mime_Part::parseMessage($text);

        if (preg_match("/^(.*?)\r?\n\r?\n/s", $text, $matches)) {
            $hdrText = $matches[1];
        } else {
            $hdrText = $text;
        }
        $headers = Horde_Mime_Headers::parseHeaders($hdrText);

        // If this message was generated by Whups, don't process it.
        if ($headers->getValue('X-Whups-Generated')) {
            return true;
        }

        // Try to avoid bounces, auto-replies, and mailing list responses.
        $from = $headers->getValue('from');
        if (strpos($headers->getValue('Content-Type'), 'multipart/report') !== false ||
            stripos($from, 'mailer-daemon@') !== false ||
            stripos($from, 'postmaster@') !== false ||
            !is_null($headers->getValue('X-Failed-Recipients')) ||
            !is_null($headers->getValue('X-Autoreply-Domain')) ||
            $headers->getValue('Auto-Submitted') == 'auto-replied' ||
            $headers->getValue('Precedence') == 'auto_reply' ||
            $headers->getValue('X-Precedence') == 'auto_reply' ||
            $headers->getValue('X-Auto-Response-Suppress') == 'All' ||
            $headers->getValue('X-List-Administrivia') == 'Yes') {
            return true;
        }
        if ($reply_to = $headers->getValue('reply-to')) {
            $from = $reply_to;
        }

        // Use the message subject as the ticket summary.
        $info['summary'] = trim($headers->getValue('subject'));
        if (empty($info['summary'])) {
            $info['summary'] = _("[No Subject]");
        }

        // Format the message into a comment.
        $comment = _("Received message:") . "\n\n";
        if (!empty($GLOBALS['conf']['mail']['include_headers'])) {
            foreach ($headers->toArray(array('nowrap' => true)) as $name => $vals) {
                if (!in_array(strtolower($name), array('subject', 'from', 'to', 'cc', 'date'))) {
                    if (is_array($vals)) {
                        foreach ($vals as $val) {
                            $comment .= $name . ': ' . $val . "\n";
                        }
                    } else {
                        $comment .= $name . ': ' . $vals . "\n";
                    }
                }
            }

            $comment .= "\n";
        }

        // Look for the body part.
        $body_id = $message->findBody();
        if ($body_id) {
            $part = $message->getPart($body_id);
            $content = Horde_String::convertCharset(
                $part->getContents(), $part->getCharset(), 'UTF-8');
            switch ($part->getType()) {
            case 'text/plain':
                $comment .= $content;
                break;
            case 'text/html':
                $comment .= Horde_Text_Filter::filter(
                    $content, array('Html2text'), array(array('width' => 0)));;
                break;
            default:
                $comment .= _("[ Could not render body of message. ]");
                break;
            }
        } else {
            $comment .= _("[ Could not render body of message. ]");
        }

        $info['comment'] = $comment . "\n";

        // Try to determine the Horde user for creating the ticket.
        if (empty($auth_user)) {
            $tmp = new Horde_Mail_Rfc822_Address($from);
            $auth_user = self::_findAuthUser($tmp->bare_address);
        }
        $author = $auth_user;

        if (empty($auth_user) && !empty($info['default_auth'])) {
            $auth_user = $info['default_auth'];
            if (!empty($from)) {
                $info['user_email'] = $from;
            }
        }

        if (empty($auth_user) && !empty($conf['mail']['username'])) {
            $auth_user = $conf['mail']['username'];
            if (!empty($from)) {
                $info['user_email'] = $from;
            }
        }

        // Authenticate as the correct Horde user.
        if (!empty($auth_user) && $auth_user != $GLOBALS['registry']->getAuth()) {
            $GLOBALS['registry']->setAuth($auth_user, array());
        }

        // Attach message.
        $attachments = array();
        if (!empty($GLOBALS['conf']['mail']['attach_message'])) {
            $tmp_name = Horde::getTempFile('whups');
            $fp = @fopen($tmp_name, 'wb');
            if (!$fp) {
                throw new Whups_Exception(
                    sprintf('Cannot open file %s for writing.', $tmp_name));
            }
            fwrite($fp, $text);
            fclose($fp);
            $attachments[] = array(
                'name' => _("Original Message") . '.eml',
                'tmp_name' => $tmp_name);
        }

        // Extract attachments.
        $dl_list = array_slice(array_keys($message->contentTypeMap()), 1);
        foreach ($dl_list as $key) {
            $part = $message->getPart($key);
            if (($key == $body_id && $part->getType() == 'text/plain') ||
                $part->getType() == 'multipart/alternative' ||
                $part->getType() == 'multipart/mixed') {
                continue;
            }
            $tmp_name = Horde::getTempFile('whups');
            $fp = @fopen($tmp_name, 'wb');
            if (!$fp) {
                throw new Whups_Exception(
                    sprintf('Cannot open file %s for writing.', $tmp_name));
            }
            fwrite($fp, $part->getContents());
            fclose($fp);
            $part_name = $part->getName(true);
            if (!$part_name) {
                $ptype = $part->getPrimaryType();
                switch ($ptype) {
                case 'multipart':
                case 'application':
                    $part_name = sprintf(_("%s part"), ucfirst($part->getSubType()));
                    break;
                default:
                    $part_name = sprintf(_("%s part"), ucfirst($ptype));
                    break;
                }
                if ($ext = Horde_Mime_Magic::mimeToExt($part->getType())) {
                    $part_name .= '.' . $ext;
                }
            }
            $attachments[] = array(
                'name' => $part_name,
                'tmp_name' => $tmp_name);
        }

        // See if we can match this message to an existing ticket.
        if ($ticket = self::_findTicket($info)) {
            $ticket->change('comment', $info['comment']);
            $ticket->change('comment-email', $from);
            if ($attachments) {
                $ticket->change('attachments', $attachments);
            }
            $ticket->commit($author);
        } elseif (!empty($info['ticket'])) {
            // Didn't match an existing ticket though a ticket number had been
            // specified.
            throw new Whups_Exception(
                sprintf(_("Could not find ticket \"%s\"."), $info['ticket']));
        } else {
            if (!empty($info['guess-queue'])) {
                // Try to guess the queue name for the new ticket from the
                // message subject.
                $queues = $GLOBALS['whups_driver']->getQueues();
                foreach ($queues as $queueId => $queueName) {
                    if (preg_match('/\b' . preg_quote($queueName, '/') . '\b/i',
                                   $info['summary'])) {
                        $info['queue'] = $queueId;
                        break;
                    }
                }
            }
            $info['attachments'] = $attachments;

            // Create a new ticket.
            $ticket = Whups_Ticket::newTicket($info, $author);
        }
    }

    /**
     * Returns the ticket number matching the provided information.
     *
     * @param array $info  A hash with ticket information.
     *
     * @return integer  The ticket number if has been passed in the subject,
     *                  false otherwise.
     */
    static protected function _findTicket(array $info)
    {
        if (!empty($info['ticket'])) {
            $ticketnum = $info['ticket'];
        } elseif (preg_match('/\[[\w\s]*#(\d+)\]/', $info['summary'], $matches)) {
            $ticketnum = $matches[1];
        } else {
            return false;
        }

        try {
            return Whups_Ticket::makeTicket($ticketnum);
        } catch (Whups_Exception $e) {
            return false;
        }
    }

    /**
     * Searches the From: header for an email address contained in one
     * of our users' identities.
     *
     * @param string $from  The From address.
     *
     * @return string  The Horde user name that matches the headers' From:
     *                 address or null if the users can't be listed or no
     *                 match has been found.
     */
    static protected function _findAuthUser($from)
    {
        $auth = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Auth')->create();

        if ($auth->hasCapability('list')) {
            foreach ($auth->listUsers() as $user) {
                $identity = $GLOBALS['injector']
                    ->getInstance('Horde_Core_Factory_Identity')
                    ->create($user);
                $addrs = $identity->getAll('from_addr');
                foreach ($addrs as $addr) {
                    if (strcasecmp($from, $addr) == 0) {
                        return $user;
                    }
                }
            }
        }

        return false;
    }

}