~vcs-imports/dehs/trunk

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
<?php

/***
Copyright 2008 by Raphael Geissert <atomo64@gmail.com>

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 2 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

class events
{

    function DB($db = null)
    {
        static $_db;

        if ($db === null) {
            return $_db;
        }
        $_db = $db;
    }

    function newVersion($sourcePackage, $currentVersion, $newVersion, $distribution, $downloadLink = '')
    {
        global $base, $sendNotifications;

        if (!$sendNotifications) {
            return null;
        }

        $eventsId = events::_searchEvent(__FUNCTION__, $sourcePackage, $newVersion);

        if (!empty($eventsId)) {
            echo "\nSkipping ".__FUNCTION__." event for v$newVersion of $sourcePackage; already sent";
            return null;
        }

        $tpl = file_get_contents($base . '/tpls/new_version.mail.tpl');

        $search  = array();
        $replace = array();

        $search [] = '#PACKAGE#';
        $replace[] = $sourcePackage;
        $search [] = '#DVERSION#';
        $replace[] = $currentVersion;
        $search [] = '#UVERSION#';
        $replace[] = $newVersion;
        $search [] = '#DISTRO#';
        $replace[] = $distribution;
        $search [] = '#LINK#';
        $replace[] = $downloadLink;

        $message = str_replace($search, $replace, $tpl);
        $subject = events::_extractHeader('subject', $message);
        $to      = events::_extractHeader('to', $message);
        $bcc     = events::_extractHeader('bcc', $message);

        $extraHeaders = events::_getDefaultExtraHeaders();
        $extraHeaders[] = 'X-DEHS-UVERSION: ' . $newVersion;
        $extraHeaders[] = 'BCC: ' . $bcc;

        if (!mail($to, $subject, $message, implode("\r\n", $extraHeaders), escapeshellarg($to)))
        return false;

        events::_recordEvent(__FUNCTION__, $sourcePackage, $distribution, $newVersion);
    }

    function _extractHeader($header, &$message)
    {
        $regex  = '/' . preg_quote($header, '/') . ':([ \t]*)(.*)/i';

        $matches = array();

        if (preg_match($regex, $message, $matches)) {

            $spacer = $matches[1];
            $value  = $matches[2];

            $message = trim(preg_replace($regex, '', $message));

            return $value;

        }
        return false;
    }

    function _getDefaultExtraHeaders()
    {
        global $notificationsMailFrom;
        $headers = array();

        $headers[] = 'From: ' . $notificationsMailFrom;
//        $headers[] = 'X-PTS-Approved: sure';
        $headers[] = 'X-BY-DEHS: sure';

        return $headers;
    }

    function _recordEvent($eventType, $packageName, $distribution, $content)
    {
        $db = events::DB();

        $eventType    = pg_escape_string($eventType);
        $packageName  = pg_escape_string($packageName);
        $distribution = pg_escape_string($distribution);
        $content      = pg_escape_string($content);

        pg_exec($db, "INSERT INTO events (name,type,dist,content,stamp) VALUES ('$packageName','$eventType','$distribution','$content',now())") OR die_status("Failed to record event: $packageName|$eventType|$distribution|$content");
    }

    function _searchEvent($eventType, $packageName, $contentSearch, $distribution = '', $strict = true)
    {
        $db = events::DB();

        $eventType     = pg_escape_string($eventType);
        $packageName   = pg_escape_string($packageName);
        $contentSearch = pg_escape_string($contentSearch);

        $result = pg_exec($db, "SELECT id FROM events WHERE name='$packageName' and type='$eventType' ".($distribution? "and dist='$distribution'":'')." and content ".($strict? '=' : 'LIKE')."'$contentSearch'") or die_status(pg_last_error($db)."\nFailed to SELECT id FROM events...\n");
        return pg_fetch_all($result);
    }

}

?>