File: cmd_learn.php

package info (click to toggle)
roundcube 1.6.13%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,888 kB
  • sloc: javascript: 195,591; php: 76,917; sql: 3,150; sh: 2,882; pascal: 1,079; makefile: 234; xml: 93; perl: 73; ansic: 48; python: 21
file content (122 lines) | stat: -rw-r--r-- 4,432 bytes parent folder | download | duplicates (5)
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
<?php

/**
 * Command line learn driver
 *
 * @version 3.1
 *
 * @author Philip Weir
 * Patched by Julien Vehent to support DSPAM
 * Enhanced support for DSPAM by Stevan Bajic <stevan@bajic.ch>
 *
 * Copyright (C) 2009-2018 Philip Weir
 *
 * This driver is part of the MarkASJunk plugin for Roundcube.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Roundcube. If not, see https://www.gnu.org/licenses/.
 */
class markasjunk_cmd_learn
{
    public function spam($uids, $src_mbox, $dst_mbox)
    {
        $this->_do_salearn($uids, true, $src_mbox);
    }

    public function ham($uids, $src_mbox, $dst_mbox)
    {
        $this->_do_salearn($uids, false, $src_mbox);
    }

    private function _do_salearn($uids, $spam, $src_mbox)
    {
        $rcube    = rcube::get_instance();
        $temp_dir = realpath($rcube->config->get('temp_dir'));
        $command  = $rcube->config->get($spam ? 'markasjunk_spam_cmd' : 'markasjunk_ham_cmd');
        $debug    = $rcube->config->get('markasjunk_debug');

        if (!$command) {
            return;
        }

        if (strpos($command, '%h') !== false) {
            preg_match_all('/%h:([\w_-]+)/', $command, $header_names, PREG_SET_ORDER);
            $header_names = array_column($header_names, 1);
        }

        // backwards compatibility %xds removed in markasjunk v1.12
        $command = str_replace('%xds', '%h:x-dspam-signature', $command);
        $command = str_replace('%u', escapeshellarg($_SESSION['username']), $command);
        $command = str_replace('%l', escapeshellarg($rcube->user->get_username('local')), $command);
        $command = str_replace('%d', escapeshellarg($rcube->user->get_username('domain')), $command);
        if (strpos($command, '%i') !== false) {
            $identity = $rcube->user->get_identity();
            $command  = str_replace('%i', escapeshellarg($identity['email']), $command);
        }

        foreach ($uids as $uid) {
            // reset command for next message
            $tmp_command = $command;

            if (strpos($tmp_command, '%s') !== false) {
                $message     = new rcube_message($uid);
                $tmp_command = str_replace('%s', escapeshellarg($message->sender['mailto']), $tmp_command);
            }

            if (!empty($header_names)) {
                $storage = $rcube->get_storage();
                $storage->check_connection();
                $headers = $storage->conn->fetchHeader($src_mbox, $uid, true, false, $header_names);

                foreach ($header_names as $header) {
                    $val = null;
                    if ($headers) {
                        $val = $headers->get($header);
                        $val = is_array($val) ? array_first($val) : $val;
                    }

                    if (!empty($val)) {
                        $tmp_command = str_replace('%h:' . $header, escapeshellarg($val), $tmp_command);
                    }
                    else {
                        if ($debug) {
                            rcube::write_log('markasjunk', "header {$header} not found in message {$src_mbox}/{$uid}");
                        }

                        continue 2;
                    }
                }
            }

            if (strpos($command, '%f') !== false) {
                $tmpfname = tempnam($temp_dir, 'rcmSALearn');
                file_put_contents($tmpfname, $rcube->storage->get_raw_body($uid));
                $tmp_command = str_replace('%f', escapeshellarg($tmpfname), $tmp_command);
            }

            $output = shell_exec($tmp_command);

            if ($debug) {
                if ($output) {
                    $tmp_command .= "\n$output";
                }

                rcube::write_log('markasjunk', $tmp_command);
            }

            if (isset($tmpfname)) {
                unlink($tmpfname);
            }
        }
    }
}