File: bootstrap.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 (285 lines) | stat: -rw-r--r-- 9,685 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
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
<?php

/*
 +-----------------------------------------------------------------------+
 | This file is part of the Roundcube Webmail client                     |
 |                                                                       |
 | Copyright (C) The Roundcube Dev Team                                  |
 |                                                                       |
 | Licensed under the GNU General Public License version 3 or            |
 | any later version with exceptions for skins & plugins.                |
 | See the README file for a full license statement.                     |
 |                                                                       |
 | PURPOSE:                                                              |
 |   Environment initialization script for functional tests              |
 +-----------------------------------------------------------------------+
 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
 | Author: Aleksander Machniak <alec@alec.pl>                            |
 +-----------------------------------------------------------------------+
*/

if (php_sapi_name() != 'cli') {
    die("Not in shell mode (php-cli)");
}

if (!defined('INSTALL_PATH')) {
    define('INSTALL_PATH', realpath(__DIR__ . '/../../') . '/' );
}

require_once(INSTALL_PATH . 'program/include/iniset.php');

$rcmail = rcmail::get_instance(0, 'test');

define('TESTS_DIR', realpath(__DIR__) . '/');
define('TESTS_USER', $rcmail->config->get('tests_username'));
define('TESTS_PASS', $rcmail->config->get('tests_password'));

require_once(__DIR__ . '/Browser.php');
require_once(__DIR__ . '/TestCase.php');
require_once(__DIR__ . '/Components/App.php');
require_once(__DIR__ . '/Components/Dialog.php');
require_once(__DIR__ . '/Components/HtmlEditor.php');
require_once(__DIR__ . '/Components/Popupmenu.php');
require_once(__DIR__ . '/Components/RecipientInput.php');
require_once(__DIR__ . '/Components/Taskmenu.php');
require_once(__DIR__ . '/Components/Toolbarmenu.php');


/**
 * Utilities for test environment setup
 */
class bootstrap
{
    static $imap_ready = null;

    /**
     * Wipe and re-initialize database
     */
    public static function init_db()
    {
        $rcmail = rcmail::get_instance();
        $dsn = rcube_db::parse_dsn($rcmail->config->get('db_dsnw'));
        $db = $rcmail->get_dbh();

        if ($dsn['phptype'] == 'mysql' || $dsn['phptype'] == 'mysqli') {
            // drop all existing tables first
            $db->query("SET FOREIGN_KEY_CHECKS=0");
            $sql_res = $db->query("SHOW TABLES");
            while ($sql_arr = $db->fetch_array($sql_res)) {
                $table = reset($sql_arr);
                $db->query("DROP TABLE $table");
            }

            self::init_db_user($db);

            // init database with schema
            system(sprintf('cat %s %s | mysql -h %s -u %s --password=%s %s',
                realpath(INSTALL_PATH . '/SQL/mysql.initial.sql'),
                realpath(TESTS_DIR . 'data/data.sql'),
                escapeshellarg($dsn['hostspec']),
                escapeshellarg($dsn['username']),
                escapeshellarg($dsn['password']),
                escapeshellarg($dsn['database'])
            ));
        }
        else if ($dsn['phptype'] == 'sqlite') {
            $db->closeConnection();
            // delete database file
            system(sprintf('rm -f %s', escapeshellarg($dsn['database'])));

            self::init_db_user($db);

            // load sample test data
            // Note: exec_script() does not really work with these queries
            $sql = file_get_contents(TESTS_DIR . 'data/data.sql');
            $sql = preg_split('/;\n/', $sql, -1, PREG_SPLIT_NO_EMPTY);

            foreach ($sql as $query) {
                $result = $db->query($query);
                if ($db->is_error($result)) {
                    rcube::raise_error($db->is_error(), false, true);
                }
            }
        }
    }

    /**
     * Create user/identity record for the test user
     */
    private static function init_db_user($db)
    {
        $rcmail = rcmail::get_instance();
        $imap_host = $rcmail->config->get('imap_host');

        if ($host = parse_url($imap_host, PHP_URL_HOST)) {
            $imap_host = $host;
        }

        $db->query("INSERT INTO `users` (`username`, `mail_host`, `language`)"
                . " VALUES (?, ?, 'en_US')", TESTS_USER, $imap_host);

        $db->query("INSERT INTO `identities` (`user_id`, `email`, `standard`)"
                . " VALUES (1, ?, '1')", TESTS_USER);
    }

    /**
     * Wipe the configured IMAP account and fill with test data
     */
    public static function init_imap($force = false)
    {
        if (!TESTS_USER) {
            return false;
        }
        else if (!$force && self::$imap_ready !== null) {
            return self::$imap_ready;
        }

        self::connect_imap(TESTS_USER, TESTS_PASS);

        return self::$imap_ready;
    }

    /**
     * Authenticate to IMAP with the given credentials
     */
    public static function connect_imap($username, $password)
    {
        $rcmail = rcmail::get_instance();
        $imap = $rcmail->get_storage();

        if ($imap->is_connected()) {
            $imap->close();
            self::$imap_ready = false;
        }

        $imap_host = $rcmail->config->get('imap_host');
        $imap_port = 143;
        $imap_ssl = false;

        $a_host = parse_url($imap_host);

        if (!empty($a_host['host'])) {
            $imap_host = $a_host['host'];
            $imap_ssl  = isset($a_host['scheme']) && in_array($a_host['scheme'], ['ssl','imaps','tls']) ? $a_host['scheme'] : false;
            $imap_port = $a_host['port'] ?? ($imap_ssl && $imap_ssl != 'tls' ? 993 : 143);
        }

        if (!$imap->connect($imap_host, $username, $password, $imap_port, $imap_ssl)) {
            rcube::raise_error("IMAP error: unable to authenticate with user " . TESTS_USER, false, true);
        }

        if (in_array('archive', (array) $rcmail->config->get('plugins'))) {
            // Register special folder type for the Archive plugin.
            // As we're in cli mode the plugin can't do it by its own
            rcube_storage::$folder_types[] = 'archive';
        }

        self::$imap_ready = true;
    }

    /**
     * Import the given file into IMAP
     */
    public static function import_message($filename, $mailbox = 'INBOX')
    {
        if (!self::init_imap()) {
            rcube::raise_error(__METHOD__ . ': IMAP connection unavailable', false, true);
        }

        $file = file_get_contents($filename);
        $imap = rcmail::get_instance()->get_storage();

        $imap->save_message($mailbox, $file);
    }

    /**
     * Delete all messages from the given mailbox
     */
    public static function purge_mailbox($mailbox)
    {
        if (!self::init_imap()) {
            rcube::raise_error(__METHOD__ . ': IMAP connection unavailable', false, true);
        }

        $imap = rcmail::get_instance()->get_storage();
        $imap->delete_message('*', $mailbox);
    }

    /**
     * Make sure the given mailbox exists in IMAP
     */
    public static function ensure_mailbox($mailbox, $empty = false)
    {
        if (!self::init_imap()) {
            rcube::raise_error(__METHOD__ . ': IMAP connection unavailable', false, true);
        }

        $imap = rcmail::get_instance()->get_storage();

        $folders = $imap->list_folders();
        if (!in_array($mailbox, $folders)) {
            $imap->create_folder($mailbox, true);
        }
        else if ($empty) {
            $imap->delete_message('*', $mailbox);
        }
    }

    /**
     * Make sure only special folders exist in IMAP
     */
    public static function reset_mailboxes()
    {
        if (!self::init_imap()) {
            rcube::raise_error(__METHOD__ . ': IMAP connection unavailable', false, true);
        }

        $rcmail       = rcmail::get_instance();
        $imap         = $rcmail->get_storage();
        $got_defaults = $rcmail->config->get('create_default_folders');
        $vendor       = $imap->get_vendor();

        // Note: We do not expect IMAP server auto-creating any folders
        foreach ($imap->list_folders() as $folder) {
            if ($folder != 'INBOX' && (!$got_defaults || !$imap->is_special_folder($folder))) {
                // GreenMail throws errors when unsubscribing a deleted folder
                if ($vendor == 'greenmail') {
                    $imap->conn->deleteFolder($folder);
                }
                else {
                    $imap->delete_folder($folder);
                }
            }
        }
    }

    /**
     * Check IMAP capabilities
     */
    public static function get_storage()
    {
        if (!self::init_imap()) {
            rcube::raise_error(__METHOD__ . ': IMAP connection unavailable', false, true);
        }

        return rcmail::get_instance()->get_storage();
    }

    /**
     * Return user preferences directly from database
     */
    public static function get_prefs()
    {
        $rcmail = rcmail::get_instance();

        // Create a separate connection to the DB, otherwise
        // we hit some strange and hard to investigate locking issues
        $db = rcube_db::factory($rcmail->config->get('db_dsnw'), $rcmail->config->get('db_dsnr'), false);
        $db->set_debug((bool)$rcmail->config->get('sql_debug'));

        $query  = $db->query("SELECT preferences FROM users WHERE username = ?", TESTS_USER);
        $record = $db->fetch_assoc($query);

        return unserialize($record['preferences']);
    }
}