1#!/usr/bin/env php 2<?php 3/** 4 * This script imports SquirrelMail database addressbooks into Turba. 5 * 6 * The first argument must be a DSN to the database containing the "address" 7 * table, e.g.: "mysql://root:password@localhost/squirrelmail". 8 * 9 * Copyright 2008-2017 Horde LLC (http://www.horde.org/) 10 * 11 * See the enclosed file LICENSE for license information (ASL). If you 12 * did not receive this file, see http://www.horde.org/licenses/apache. 13 * 14 * @author Ben Chavet <ben@horde.org> 15 * @author Jan Schneider <jan@horde.org> 16 */ 17 18if (file_exists(__DIR__ . '/../../turba/lib/Application.php')) { 19 $baseDir = __DIR__ . '/../'; 20} else { 21 require_once 'PEAR/Config.php'; 22 $baseDir = PEAR_Config::singleton() 23 ->get('horde_dir', null, 'pear.horde.org') . '/turba/'; 24} 25require_once $baseDir . 'lib/Application.php'; 26Horde_Registry::appInit('turba', array('cli' => true, 'user_admin' => true)); 27 28// Read command line parameters. 29if ($argc != 2) { 30 $cli->message('Too many or too few parameters.', 'cli.error'); 31 $cli->writeln('Usage: turba-import-squirrelmail-sql-abook DSN'); 32 $cli->writeln($cli->indent('DSN are json-encoded connection parameters to the database containing the "userprefs" table. Example:')); 33 $cli->writeln($cli->indent('{"adapter":"mysql","user":"root","password":"password","host":"localhost","database":"squirrelmail"}')); 34 exit; 35} 36 37$db = $injector->getInstance('Horde_Db')->createDb(json_decode($argv[1])); 38include TURBA_BASE . '/config/backends.php'; 39 40// Loop through SquirrelMail address books. 41$handle = $db->select('SELECT owner, nickname, firstname, lastname, email, label FROM address ORDER BY owner'); 42$turba_shares = $GLOBALS['injector']->getInstance('Turba_Shares'); 43$user = null; 44$count = 0; 45foreach ($handle as $row) { 46 // Set current user 47 if ($row['owner'] != $user) { 48 if (!is_null($user)) { 49 $cli->message(' Added ' . $count . ' contacts', 'cli.success'); 50 $count = 0; 51 } 52 $user = $row['owner']; 53 $registry->setAuth($user, array()); 54 $cli->message('Importing ' . $user . '\'s address book'); 55 56 // Reset user prefs 57 $prefs = $injector->getInstance('Horde_Core_Factory_Prefs')->create('turba', array( 58 'cache' => false, 59 'user' => $user 60 )); 61 62 // Reset $cfgSources for current user. 63 unset($cfgSources); 64 $hasShares = false; 65 foreach ($cfgSources as $key => $cfg) { 66 if (!empty($cfg['use_shares'])) { 67 $has_share = true; 68 break; 69 } 70 } 71 if ($has_share) { 72 $cfgSources = Turba::getConfigFromShares($cfgSources); 73 } 74 $cfgSources = Turba::permissionsFilter($cfgSources); 75 if (!count($cfgSources)) { 76 $cli->message('No address book available for ' . $user, 'cli.error'); 77 continue; 78 } 79 80 // Get user's default addressbook 81 $import_source = $prefs->getValue('default_dir'); 82 if (empty($import_source)) { 83 $import_source = array_keys($cfgSources); 84 $import_source = $import_source[0]; 85 } 86 87 // Check existance of the specified source. 88 if (!isset($cfgSources[$import_source])) { 89 $cli->message(' ' . sprintf(_("Invalid address book: %s"), $import_source), 'cli.error'); 90 continue; 91 } 92 93 // Initiate driver 94 try { 95 $driver = $injector->getInstance('Turba_Factory_Driver')->create($import_source); 96 } catch (Turba_Exception $e) { 97 $cli->message(' ' . sprintf(_("Connection failed: %s"), $e->getMessage()), 'cli.error'); 98 continue; 99 } 100 } 101 102 if (!count($cfgSources)) { 103 continue; 104 } 105 106 $rfc822 = new Horde_Mail_Rfc822(); 107 $members = $rfc822->parseAddressList($row['email']); 108 if (count($members) > 1) { 109 // Entry is a list of contacts, import each individually and create a 110 // group that contains them. 111 $attributes = array('alias' => $row['nickname'], 112 'firstname' => $row['firstname'], 113 'lastname' => $row['lastname'], 114 'notes' => $row['label']); 115 $gid = $driver->add($attributes); 116 $group = new Turba_Object_Group($driver, array_merge($attributes, array('__key' => $gid))); 117 ++$count; 118 foreach (array_map('strval', $members) as $member) { 119 try { 120 $result = $driver->add(array( 121 'firstname' => $member, 122 'email' => $member 123 )); 124 $group->addMember($result, $import_source); 125 ++$count; 126 } catch (Turba_Exception $e) { 127 $cli->message(' ' . $e->getMessage(), 'cli.error'); 128 } 129 } 130 $group->store(); 131 } else { 132 // Entry only contains one contact, import it. 133 $contact = array( 134 'alias' => $row['nickname'], 135 'firstname' => $row['firstname'], 136 'lastname' => $row['lastname'], 137 'email' => $row['email'], 138 'notes' => $row['label'] 139 ); 140 141 try { 142 $driver->add($contact); 143 ++$count; 144 } catch (Turba_Exception $e) { 145 $cli->message(' ' . $e->getMessage(), 'cli.error'); 146 } 147 } 148} 149$cli->message(' Added ' . $count . ' contacts', 'cli.success'); 150