summaryrefslogtreecommitdiff
path: root/plugins/libkolab/bin/modcache.sh
blob: 58086d09310c2d0dca40915034870cb3fcc8a88a (plain)
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
#!/usr/bin/env php -d enable_dl=On
<?php

/**
 * Kolab storage cache modification script
 *
 * @version 3.0
 * @author Thomas Bruederli <bruederli@kolabsys.com>
 *
 * Copyright (C) 2012, Kolab Systems AG <contact@kolabsys.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

define('INSTALL_PATH', realpath('.') . '/' );
ini_set('display_errors', 1);

if (!file_exists(INSTALL_PATH . 'program/include/clisetup.php'))
    die("Execute this from the Roundcube installation dir!\n\n");

require_once INSTALL_PATH . 'program/include/clisetup.php';

function print_usage()
{
	print "Usage:  modcache.sh [OPTIONS] ACTION [USERNAME ARGS ...]\n";
	print "Possible actions are: expunge, clear, prewarm\n";
	print "-a, --all      Clear/expunge all caches\n";
	print "-h, --host     IMAP host name\n";
	print "-u, --user     IMAP user name to authenticate\n";
	print "-t, --type     Object types to clear/expunge cache\n";
	print "-l, --limit    Limit the number of records to be expunged\n";
}

// read arguments
$opts = get_opt(array(
    'a' => 'all',
    'h' => 'host',
    'u' => 'user',
    'p' => 'password',
    't' => 'type',
    'l' => 'limit',
    'v' => 'verbose',
));

$opts['username'] = !empty($opts[1]) ? $opts[1] : $opts['user'];
$action = $opts[0];

$rcmail = rcmail::get_instance();


/*
 * Script controller
 */
switch (strtolower($action)) {

/*
 * Clear/expunge all cache records
 */
case 'expunge':
    $expire = strtotime(!empty($opts[2]) ? $opts[2] : 'now - 10 days');
    $sql_add = " AND created <= '" . date('Y-m-d 00:00:00', $expire) . "'";
    if ($opts['limit']) {
        $sql_add .= ' LIMIT ' . intval($opts['limit']);
    }

case 'clear':
    // connect to database
    $db = $rcmail->get_dbh();
    $db->db_connect('w');
    if (!$db->is_connected() || $db->is_error())
        die("No DB connection\n");

    $folder_types = $opts['type'] ? explode(',', $opts['type']) : array('contact','distribution-list','event','task','configuration');
    $folder_types_db = array_map(array($db, 'quote'), $folder_types);

    if ($opts['all']) {
        $sql_query = "DELETE FROM kolab_cache WHERE type IN (" . join(',', $folder_types_db) . ")";
    }
    else if ($opts['username']) {
        $sql_query = "DELETE FROM kolab_cache WHERE type IN (" . join(',', $folder_types_db) . ") AND resource LIKE ?";
    }

    if ($sql_query) {
        $db->query($sql_query . $sql_add, resource_prefix($opts).'%');
        echo $db->affected_rows() . " records deleted from 'kolab_cache'\n";
    }
    break;


/*
 * Prewarm cache by synchronizing objects for the given user
 */
case 'prewarm':
    // make sure libkolab classes are loaded
    $rcmail->plugins->load_plugin('libkolab');

    if (authenticate($opts)) {
        $folder_types = $opts['type'] ? explode(',', $opts['type']) : array('contact','event','task','configuration');
        foreach ($folder_types as $type) {
            // sync every folder of the given type
            foreach (kolab_storage::get_folders($type) as $folder) {
                echo "Synching " . $folder->name . " ($type) ... ";
                echo $folder->count($type) . "\n";

                // also sync distribution lists in contact folders
                if ($type == 'contact') {
                    echo "Synching " . $folder->name . " (distribution-list) ... ";
                    echo $folder->count('distribution-list') . "\n";
                }
            }
        }
    }
    else
        die("Authentication failed for " . $opts['user']);
    break;


/*
 * Unknown action => show usage
 */
default:
    print_usage();
    exit;
}


/**
 * Compose cache resource URI prefix for the given user credentials
 */
function resource_prefix($opts)
{
    return 'imap://' . urlencode($opts['username']) . '@' . $opts['host'] . '/';
}


/**
 * Authenticate to the IMAP server with the given user credentials
 */
function authenticate(&$opts)
{
    global $rcmail;

    // prompt for password
    if (empty($opts['password']) && ($opts['username'] || $opts['user'])) {
        $opts['password'] = prompt_silent("Password: ");
    }

    // simulate "login as" feature
    if ($opts['user'] && $opts['user'] != $opts['username'])
        $_POST['_loginas'] = $opts['username'];
    else if (empty($opts['user']))
        $opts['user'] = $opts['username'];

    // let the kolab_auth plugin do its magic
    $auth = $rcmail->plugins->exec_hook('authenticate', array(
        'host' => trim($opts['host']),
        'user' => trim($opts['user']),
        'pass' => $opts['password'],
        'cookiecheck' => false,
        'valid' => !empty($opts['user']) && !empty($opts['host']),
    ));

    if ($auth['valid']) {
        $storage = $rcmail->get_storage();
        if ($storage->connect($auth['host'], $auth['user'], $auth['pass'], 143, false)) {
            if ($opts['verbose'])
                echo "IMAP login succeeded.\n";
            if (($user = rcube_user::query($opts['username'], $auth['host'])) && $user->ID)
                $rcmail->set_user($user);
        }
        else
            die("Login to IMAP server failed!\n");
    }
    else {
        die("Invalid login credentials!\n");
    }

    return $auth['valid'];
}