~eventum-developers/eventum/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
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
#!/usr/bin/php
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 encoding=utf-8: */
// +----------------------------------------------------------------------+
// | Eventum - Issue Tracking System                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003 - 2008 MySQL AB                                   |
// | Copyright (c) 2008 - 2009 Sun Microsystem Inc.                       |
// |                                                                      |
// | 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:                           |
// |                                                                      |
// | Free Software Foundation, Inc.                                       |
// | 59 Temple Place - Suite 330                                          |
// | Boston, MA 02111-1307, USA.                                          |
// +----------------------------------------------------------------------+
// | Authors: João Prado Maia <jpm@mysql.com>                             |
// +----------------------------------------------------------------------+
//
// @(#) $Id: download_emails.php 3872 2009-04-13 20:51:59Z glen $

ini_set("memory_limit", "256M");

require_once 'init.php';

// setup constant to be used globally
define('SAPI_CLI', 'cli' == php_sapi_name());

/**
 * Display status message.
 *
 * Respects calling context:
 * - for CLI output is displayed to STDOUT,
 * - for Web newlines are converted to HTML linebreaks.
 */
function msg() {
    $args = func_get_args();
    // let messages be newline terminated
    $args[] = "";
    $msg = implode("\n", $args);

    if (SAPI_CLI) {
        fwrite(STDOUT, $msg);
    } else {
        $msg = nl2br($msg);
        echo $msg;
    }
}

/**
 * Display fatal error message and exit program.
 *
 * Respects calling context:
 * - for CLI output is displayed to STDERR,
 * - for Web newlines are converted to HTML linebreaks.
 */
function fatal() {
    $args = func_get_args();
    // let messages be newline terminated
    $args[] = "";
    $msg = implode("\n", $args);

    if (SAPI_CLI) {
        fwrite(STDERR, 'ERROR: '.$msg);
    } else {
        $msg = '<b>ERROR</b>: '.nl2br($msg);
        echo $msg;
    }

    exit(1);
}

/**
 * Get parameters needed for this script.
 *
 * for CLI mode these are take from command line arguments
 * for Web mode those are taken as named _GET parameters.
 *
 * @return  array   $config
 */
function getParams() {
    // some defaults,
    $config = array(
        'fix-lock' => false,
        'username' => null,
        'hostname' => null,
        'mailbox' => null,
    );

    if (SAPI_CLI) {
        global $argc, $argv;
        // --fix-lock may be only the last argument (first or fourth)
        if ($argv[$argc - 1] == '--fix-lock') {
            // no other args are allowed
            $config['fix-lock'] = true;
        }

        if ($argc > 1) {
            $config['username'] = $argv[1];
        }
        if ($argc > 2) {
            $config['hostname'] = $argv[2];
        }
        if ($argc > 3) {
            $config['mailbox'] = $argv[3];
        }
    } else {
        foreach (array_keys($config) as $key) {
            if (isset($_GET[$key])) {
                $config[$key] = $_GET[$key];
            }
        }
    }
    return $config;
}

// we need the IMAP extension for this to work
if (!function_exists('imap_open')) {
    fatal(
        "Eventum requires the IMAP extension in order to download messages saved on a IMAP/POP3 mailbox.",
        "Please refer to the PHP manual for more details about how to enable the IMAP extension."
    );
}

// argv/argc needs to be enabled in CLI mode
if (SAPI_CLI && ini_get('register_argc_argv') == 'Off') {
    fatal(
        "Eventum requires the ini setting register_argc_argv to be enabled to run this script.",
        "Please refer to the PHP manual for more details on how to change this ini setting."
    );
}

$config = getParams();

// check for the required parameters
if (!$config['fix-lock'] && (empty($config['username']) || empty($config['hostname']))) {
    if (SAPI_CLI) {
        fatal(
            "Wrong number of parameters given. Expected parameters related to the email account:",
            " 1 - username",
            " 2 - hostname",
            " 3 - mailbox (only required if IMAP account)",
            "Example: php download_emails.php user example.com INBOX"
        );
    } else {
        fatal(
            "Wrong number of parameters given. Expected parameters related to email account:",
            "download_emails.php?username=<i>username</i>&hostname=<i>hostname</i>&mailbox=<i>mailbox</i>"
        );
    }
}

// get the account ID early since we need it also for unlocking.
$account_id = Email_Account::getAccountID($config['username'], $config['hostname'], $config['mailbox']);
if (!$account_id && !$config['fix-lock']) {
    fatal(
        "Could not find a email account with the parameter provided.",
        "Please verify your email account settings and try again."
    );
}

if ($config['fix-lock']) {
    // if there is no account id, unlock all accounts
    if (!$account_id) {
        $prj_ids = array_keys(Project::getAll());
        foreach ($prj_ids as $prj_id) {
            $ema_ids = Email_Account::getAssocList($prj_id);
            foreach ($ema_ids as $ema_id => $ema_title) {
                $lockfile = 'download_emails_' . $ema_id;
                Lock::release($lockfile);
                msg("Removed lock file '$lockfile'.");
            }
        }
    } else {
        $lockfile = 'download_emails_' . $account_id;
        Lock::release($lockfile);
        msg("Removed lock file '$lockfile'.");
    }
    exit(0);
}

// check if there is another instance of this script already running
if (!Lock::acquire('download_emails_' . $account_id)) {
    if (SAPI_CLI) {
        fatal(
            "Another instance of the script is still running for the specified account.",
            "If this is not accurate, you may fix it by running this script with '--fix-lock'",
            "as the 4th parameter or you may unlock ALL accounts by running this script with '--fix-lock'",
            "as the only parameter."
        );
    } else {
        fatal(
            "Another instance of the script is still running for the specified account. ",
            "If this is not accurate, you may fix it by running this script with 'fix-lock=1'",
            "in the query string or you may unlock ALL accounts by running this script with 'fix-lock=1'",
            "as the only parameter."
        );
    }
    exit;
}

$account = Email_Account::getDetails($account_id);
$mbox = Support::connectEmailServer($account);
if ($mbox == false) {
    Lock::release('download_emails_' . $account_id);
    fatal("Could not connect to the email server. Please verify your email account settings and try again.");
}

$total_emails = Support::getTotalEmails($mbox);

if ($total_emails > 0) {
    for ($i = 1; $i <= $total_emails; $i++) {
        Support::getEmailInfo($mbox, $account, $i);
    }
}
imap_expunge($mbox);
Support::clearErrors();

// clear the lock
Lock::release('download_emails_' . $account_id);