1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/egroupware_update_mailinglists.php Mon Jun 21 00:23:18 2010 +0200
1.3 @@ -0,0 +1,175 @@
1.4 +#!/usr/bin/env php
1.5 +<?php
1.6 +/*
1.7 + * Update Majordomo mailinglists with data from egroupware
1.8 + * Each mailinglist is filled with users from one egroupware group.
1.9 + *
1.10 + * Copyright 2010 Philipp Wagner <mail@philipp-wagner.com>
1.11 + *
1.12 + * This program is free software: you can redistribute it and/or modify
1.13 + * it under the terms of the GNU General Public License as published by
1.14 + * the Free Software Foundation, either version 3 of the License, or
1.15 + * (at your option) any later version.
1.16 + *
1.17 + * This program is distributed in the hope that it will be useful,
1.18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.20 + * GNU General Public License for more details.
1.21 + *
1.22 + * You should have received a copy of the GNU General Public License
1.23 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.24 + */
1.25 +require_once 'lib/Config.php';
1.26 +require_once 'lib/RpcService.php';
1.27 +require_once 'lib/WhoJob.php';
1.28 +require_once 'lib/SubscribeJob.php';
1.29 +require_once 'lib/UnsubscribeJob.php';
1.30 +
1.31 +// config
1.32 +$config = &Config::getInstance();
1.33 +$config->setConfigFile('config.ini');
1.34 +
1.35 +/**
1.36 + * Wait max. 120s for a result of $job
1.37 + *
1.38 + * @param Job $job
1.39 + * @param string $uuid
1.40 + * @return mixed
1.41 + */
1.42 +function wait_for_results(Job &$job)
1.43 +{
1.44 + $cnt = 0;
1.45 + echo '(I) Waiting for response ';
1.46 + do {
1.47 + $res = $job->checkResults();
1.48 + echo '.';
1.49 + if ($res['status'] === Job::STATUS_NOT_READY) {
1.50 + sleep(1);
1.51 + }
1.52 + } while ($cnt++ < 120 && $res['status'] === Job::STATUS_NOT_READY);
1.53 + echo " done\n";
1.54 + return $res;
1.55 +}
1.56 +
1.57 +function p_mysql_query($query)
1.58 +{
1.59 + if (!($res = mysql_query($query))) {
1.60 + echo "Query failed! Query was: $query\n".
1.61 + "MySQL error message was: ".mysql_error()."\n";
1.62 + }
1.63 + return $res;
1.64 +}
1.65 +
1.66 +if (!mysql_connect($config->getValue('egroupware/db_server'),
1.67 + $config->getValue('egroupware/db_user'),
1.68 + $config->getValue('egroupware/db_password'))) {
1.69 + echo 'DB connection failed.';
1.70 + exit(1);
1.71 +}
1.72 +
1.73 +if (!mysql_select_db($config->getValue('egroupware/db_database'))) {
1.74 + echo 'Unable to select DB.';
1.75 + exit(1);
1.76 +}
1.77 +
1.78 +$lists = $config->getValue(('egroupware-lists'));
1.79 +foreach ($lists as $listName => $groupId) {
1.80 + echo "(I) Processing list $listName\n";
1.81 + $q = "SELECT account_lid, n_family, n_given, contact_email
1.82 + FROM `egw_acl`
1.83 + LEFT JOIN egw_accounts ON acl_account = account_id
1.84 + LEFT JOIN egw_addressbook USING(account_id)
1.85 + WHERE acl_appname = 'phpgw_group' AND
1.86 + `acl_location` = '-$groupId'";
1.87 + ($res = p_mysql_query($q)) || exit(1);
1.88 +
1.89 + $users = array();
1.90 + while ($user = mysql_fetch_array($res, MYSQL_ASSOC)) {
1.91 + $users[] = $user['contact_email'];
1.92 + }
1.93 +
1.94 + // get existing users on list (3 tries)
1.95 + $success = true;
1.96 + $cnt = 1;
1.97 + do {
1.98 + echo "(I) Getting existing users on the list (try $cnt)\n";
1.99 + $wj = new WhoJob;
1.100 + $uuid = $wj->go(array('list' => $listName));
1.101 + echo "(I) Executing job with ID $uuid.\n";
1.102 + $res = wait_for_results($wj);
1.103 + if ($res['status'] !== Job::STATUS_SUCCESS) {
1.104 + $success = false;
1.105 + echo "(E) Getting all users from list $listName failed.\n";
1.106 + } else {
1.107 + $existingUsers = $res['result'];
1.108 + }
1.109 + } while ($cnt++ < 4 && !$success);
1.110 +
1.111 + if (!$success) {
1.112 + echo "(E) Unable to get existing users from list in 3 tries, giving up.\n";
1.113 + continue;
1.114 + }
1.115 +
1.116 + // unsubscribe existing users
1.117 + if (empty($existingUsers)) {
1.118 + echo "(I) No existing users to unsubscribe.\n";
1.119 + } else {
1.120 + $success = true;
1.121 + $cnt = 1;
1.122 + do {
1.123 + echo "(I) Unsubscribing existing users (try $cnt)\n";
1.124 + $uj = new UnsubscribeJob;
1.125 + $params = array(
1.126 + 'list' => $listName,
1.127 + 'users' => $existingUsers
1.128 + );
1.129 + $uuid = $uj->go($params);
1.130 + echo "(I) Executing job with ID $uuid.\n";
1.131 + $res = wait_for_results($uj);
1.132 + if ($res['status'] !== Job::STATUS_SUCCESS) {
1.133 + $success = false;
1.134 + echo "(E) Unsubscribing all users from $listName failed.\n";
1.135 + var_dump($res);
1.136 + }
1.137 + } while ($cnt++ < 4 && !$success);
1.138 +
1.139 + if (!$success) {
1.140 + echo "(E) Unable to unsubscribe users from list in 3 tries, giving up.\n";
1.141 + continue;
1.142 + }
1.143 + }
1.144 +
1.145 + // subscribe new users
1.146 + if (empty($users)) {
1.147 + echo "(I) No new users to subscribe.\n";
1.148 + } else {
1.149 + $success = true;
1.150 + $cnt = 1;
1.151 + do {
1.152 + echo "(I) Subscribing new users\n";
1.153 + $sj = new SubscribeJob;
1.154 + $params = array(
1.155 + 'list' => $listName,
1.156 + 'users' => $users
1.157 + );
1.158 + $uuid = $sj->go($params);
1.159 + echo "(I) Executing job with ID $uuid.\n";
1.160 + $res = wait_for_results($sj);
1.161 + if ($res['status'] !== Job::STATUS_SUCCESS) {
1.162 + $success = false;
1.163 + echo "(E) Subscribing new users to $listName failed.\n";
1.164 + var_dump($res);
1.165 + }
1.166 + } while ($cnt++ < 4 && !$success);
1.167 +
1.168 + if (!$success) {
1.169 + echo "(E) Houston, we have a problem. All users unsubscribed, ".
1.170 + "but unable to subscribe the new ones. List empty?! ".
1.171 + "Stopping update! Manual intervention required.\n";
1.172 + exit(1);
1.173 + }
1.174 + }
1.175 +}
1.176 +
1.177 +
1.178 +echo "(I) All lists updated!\n";