/usr/bin/nag-import-openxchange is in php-horde-nag 4.2.13-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 | #!/usr/bin/php
<?php
/**
* This script imports Open-Xchange task lists into Nag.
*
* The first argument must be the API endpoint of an Open-Xchange servers,
* usually something like http://servername/ajax.
*
* If called with three arguments, the further arguments must be the user name
* (usually "Administrator") and password of an administrator user to import
* public task lists.
*
* If called with two arguments, the second argument must be a file with user
* names and cleartext passwords separated by spaces.
*
* Copyright 2014-2016 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @author Jan Schneider <jan@horde.org>
*/
// Init application.
if (file_exists(__DIR__ . '/../../nag/lib/Application.php')) {
$baseDir = __DIR__ . '/../';
} else {
require_once 'PEAR/Config.php';
$baseDir = PEAR_Config::singleton()
->get('horde_dir', null, 'pear.horde.org') . '/nag/';
}
require_once $baseDir . 'lib/Application.php';
Horde_Registry::appInit('nag', array('cli' => true, 'user_admin' => true));
// Read command line parameters.
if ($argc < 3 || $argc > 4) {
$cli->message('Too many or too few parameters.', 'cli.error');
$cli->writeln('Usage: nag-import-openxchange url [ file | user password ]');
$cli->writeln($cli->indent('url is the URL of an Open-Xchange AJAX endpoint'));
$cli->writeln($cli->indent('file is a file with space separated user names and passwords to import'));
$cli->writeln($cli->indent('personal task lists.'));
$cli->writeln($cli->indent('user and password are credentials of an administrator user to import public'));
$cli->writeln($cli->indent('task lists.'));
exit;
}
$admin = $argc == 4;
// Basic objects and variables.
$endpoint = parse_url($argv[1]);
$cli->message('Opening endpoint ' . $argv[1]);
$ox = new Horde_OpenXchange_Tasks(array('endpoint' => $argv[1]));
$users = array();
// Prepare handle on user/password list.
if ($admin) {
$fp = fopen('php://temp', 'r+');
fwrite($fp, $argv[2] . ' ' . $argv[3]);
rewind($fp);
} else {
if (!is_readable($argv[2]) || !filesize($argv[2])) {
$cli->message($argv[2] . ' is not readable or empty', 'cli.error');
exit(1);
}
$fp = fopen($argv[2], 'r');
}
if (!$fp) {
exit(1);
}
// Loop through all users.
while ($row = fgetcsv($fp, 0, ' ')) {
$user = $row[0];
if (is_null($user)) {
continue;
}
$ox->logout();
$ox->login($user, $row[1]);
$registry->setAuth($user, array());
$cli->message('Importing ' . $user . '\'s task lists');
// Reset user prefs
$prefs = $injector->getInstance('Horde_Core_Factory_Prefs')
->create('nag', array(
'cache' => false,
'user' => $user
)
);
// Load task lists for current user.
$targets = Nag::listTasklists(true, Horde_Perms::EDIT, false);
$count = 0;
$tasklists = $ox->listResources(
$admin
? Horde_OpenXchange_Tasks::RESOURCE_PUBLIC
: Horde_OpenXchange_Tasks::RESOURCE_PRIVATE
);
$default = $ox->getConfig('folder/tasks');
// Loop through all task lists.
foreach ($tasklists as $folderId => $tasklist) {
// Track UIDs, OX doesn't provide any means to find recurring tasks.
$uids = array();
// Check if we already have an task list matching the name.
$target = null;
foreach ($targets as $id => $info) {
if ($tasklist['label'] == $info->get('name')) {
$target = $id;
break;
}
}
if ($target) {
$cli->message('Task List "' . $tasklist['label'] . '" found, updating...');
} else {
// Create new task list.
$cli->message('Task List "' . $tasklist['label'] . '" not found, creating...');
$params = array(
'name' => $tasklist['label'],
'color' => Nag::randomColor(),
'description' => '',
'system' => $admin,
);
$share = Nag::addTasklist($params);
foreach ($tasklist['hordePermission']['group'] as $group => $perm) {
$share->addGroupPermission($group, $perm);
}
foreach ($tasklist['hordePermission']['user'] as $user => $perm) {
$share->addUserPermission($user, $perm);
}
$target = $share->getName();
}
if ($folderId == $default) {
$prefs->setValue('default_tasklist', $target);
}
// Initiate driver.
try {
$driver = $injector
->getInstance('Nag_Factory_Driver')
->create($target);
} catch (Nag_Exception $e) {
$cli->message(' ' . sprintf(_("Connection failed: %s"), $e->getMessage()), 'cli.error');
continue;
}
$tasks = $ox->listTasks($folderId);
// Loop through all tasks.
foreach ($tasks as $task) {
$newTask = array(
'name' => $task['title'],
'desc' => $task['description'],
'estimate' => $task['duration'],
'tags' => $task['categories'],
'uid' => $task['uid'],
'private' => $task['private'],
);
if ($task['start']) {
$newTask['start'] = $task['start'] / 1000;
}
if ($task['end']) {
$due = new Horde_Date($task['end'] / 1000, 'UTC');
$due->hour = $due->min = $due->sec = 0;
$newTask['due'] = $due->timestamp();
if ($task['alarm']) {
$newTask['alarm'] = ($task['end'] - $task['alarm']) / 60000;
}
}
switch ($task['priority']) {
case Horde_OpenXchange_Tasks::PRIORITY_HIGH:
$newTask['priority'] = 1;
break;
case Horde_OpenXchange_Tasks::PRIORITY_MEDIUM:
$newTask['priority'] = 3;
break;
case Horde_OpenXchange_Tasks::PRIORITY_LOW:
$newTask['priority'] = 5;
break;
}
if ($task['recur_type']) {
if (!isset($uids[$task['uid']])) {
$recurrence = new Horde_Date_Recurrence(new Horde_Date($newTask['due']));
switch ($task['recur_type']) {
case 3:
if ($task['day_in_month']) {
$task['recur_type'] = Horde_Date_Recurrence::RECUR_MONTHLY_DATE;
} else {
$task['recur_type'] = Horde_Date_Recurrence::RECUR_MONTHLY_WEEKDAY;
}
break;
case 4:
if ($task['day_in_month']) {
$task['recur_type'] = Horde_Date_Recurrence::RECUR_YEARLY_DATE;
} else {
$task['recur_type'] = Horde_Date_Recurrence::RECUR_YEARLY_WEEKDAY;
}
break;
}
$recurrence->setRecurType($task['recur_type']);
if (!empty($task['recur_count'])) {
$recurrence->setRecurCount($task['recur_count']);
} elseif ($task['recur_end']) {
$end = new Horde_Date($task['recur_end'] / 1000, 'UTC');
$end->mday++;
$end->hour = $end->min = $end->sec = 0;
$recurrence->setRecurEnd($end);
}
if (!empty($task['recur_interval'])) {
$recurrence->setRecurInterval($task['recur_interval']);
}
if (!empty($task['recur_days'])) {
$recurrence->setRecurOnDay($task['recur_days']);
}
$newTask['recurrence'] = $recurrence;
$uids[$task['uid']] = $newTask;
}
if ($task['status'] == Horde_OpenXchange_Tasks::STATUS_DONE) {
$due = new Horde_Date($task['end'] / 1000, 'UTC');
$uids[$task['uid']]['recurrence']->addCompletion(
$due->year, $due->month, $due->mday
);
}
if ($task['end_date'] > $uids[$task['uid']]['end_date']) {
if ($task['status'] == Horde_OpenXchange_Tasks::STATUS_DONE) {
$uids[$task['uid']]['completed'] = true;
$uids[$task['uid']]['completed_date'] = $task['completed'] / 1000;
} else {
$uids[$task['uid']]['completed'] = false;
$uids[$task['uid']]['completed_date'] = null;
}
}
continue;
}
try {
$driver->add($newTask);
$count++;
} catch (Nag_Exception $e) {
$cli->message(' ' . $e->getMessage(), 'cli.error');
}
}
// Loop through all recurring tasks.
foreach ($uids as $task) {
try {
$driver->add($task);
$count++;
} catch (Nag_Exception $e) {
$cli->message(' ' . $e->getMessage(), 'cli.error');
}
}
}
$cli->message(' Added ' . $count . ' tasks', 'cli.success');
$count = 0;
}
|