/usr/share/horde/whups/lib/Api.php is in php-horde-whups 3.0.9-1.
This file is owned by root:root, with mode 0o644.
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 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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 | <?php
/**
* Whups external API interface.
*
* This file defines Whups' external API interface. Other applications
* can interact with Whups through this API.
*
* @package Whups
*/
class Whups_Api extends Horde_Registry_Api
{
/**
* Links.
*
* @var array
*/
protected $_links = array(
'show' => '%application%/ticket/?id=|ticket|'
);
/**
* Browse through Whups' object tree.
*
* @param string $path The level of the tree to browse.
*
* @return array The contents of $path
*/
public function browse($path = '')
{
global $whups_driver, $registry;
if (substr($path, 0, 5) == 'whups') {
$path = substr($path, 5);
}
$path = trim($path, '/');
if (empty($path)) {
$results = array(
'whups/queue' => array(
'name' => _("Queues"),
'icon' => Horde_Themes::img('whups.png'),
'browseable' => count(
Whups::permissionsFilter($whups_driver->getQueues(),
'queue', Horde_Perms::READ))));
} else {
$path = explode('/', $path);
$results = array();
switch ($path[0]) {
case 'queue':
$queues = Whups::permissionsFilter($whups_driver->getQueues(),
'queue', Horde_Perms::SHOW);
if (count($path) == 1) {
foreach ($queues as $queue => $name) {
$results['whups/queue/' . $queue] = array(
'name' => sprintf(_("Tickets from %s"), $name),
'browseable' => true);
}
} else {
if (!Whups::hasPermission($queues[$path[1]], 'queue',
Horde_Perms::READ)) {
throw new Horde_Exception_PermissionDenied();
}
$tickets = $whups_driver->getTicketsByProperties(
array('queue' => $path[1]));
foreach ($tickets as $ticket) {
$results['whups/queue/' . $path[1] . '/' . $ticket['id']] = array(
'name' => $ticket['summary'],
'browseable' => false);
}
}
break;
}
}
return $results;
}
/**
* Create a new queue.
*
* @param string $name The queue's name.
*
* @return integer The new queue id.
*/
public function addQueue($name)
{
if ($GLOBALS['registry']->isAdmin(array('permission' => 'whups:admin'))) {
return $GLOBALS['whups_driver']->addQueue($name, '');
}
throw new Horde_Exception_PermissionDenied('You must be an administrator to perform this action.');
}
/**
* Return the ids of all open tickets assigned to the current user.
*
* @return array Array of ticket ids.
*/
public function getAssignedTicketIds()
{
global $whups_driver;
$info = array('owner' => 'user:' . $GLOBALS['registry']->getAuth(), 'nores' => true);
$tickets = $whups_driver->getTicketsByProperties($info);
$result = array();
foreach ($tickets as $ticket) {
$result[] = $ticket['id'];
}
return $result;
}
/**
* Return the ids of all open tickets that the current user created.
*
* @return array Array of ticket ids.
*/
public function getRequestedTicketIds()
{
global $whups_driver;
$info = array('requester' => $GLOBALS['registry']->getAuth(), 'nores' => true);
$tickets = $whups_driver->getTicketsByProperties($info);
$result = array();
foreach ($tickets as $ticket) {
$result[] = (int) $ticket['id'];
}
return $result;
}
/**
* Create a new ticket.
*
* @param array $ticket_info An array of form variables representing all of the
* data collected by CreateStep1Form, CreateStep2Form, CreateStep3Form, and
* optionally CreateStep4Form.
*
* @return integer The new ticket id.
*/
public function addTicket($ticket_info)
{
global $whups_driver;
if (!is_array($ticket_info)) {
throw new Whups_Exception('Invalid arguments');
}
$vars = new Horde_Variables($ticket_info);
$form1 = new Whups_Form_Ticket_CreateStepOne($vars);
$form2 = new Whups_Form_Ticket_CreateStepTwo($vars);
$form3 = new Whups_Form_Ticket_CreateStepThree($vars);
// FIXME: This is an almighty hack, but we can't have form
// tokens in rpc calls.
$form1->useToken(false);
$form2->useToken(false);
$form3->useToken(false);
// Complain if we've been given bad parameters.
if (!$form1->validate($vars, true)) {
$f1 = var_export($form1->getErrors(), true);
throw new Whups_Exception("Invalid arguments ($f1)");
}
if (!$form2->validate($vars, true)) {
$f2 = var_export($form2->getErrors(), true);
throw new Whups_Exception("Invalid arguments ($f2)");
}
if (!$form3->validate($vars, true)) {
$f3 = var_export($form3->getErrors(), true);
throw new Whups_Exception("Invalid arguments ($f3)");
}
$form1->getInfo($vars, $info);
$form2->getInfo($vars, $info);
$form3->getInfo($vars, $info);
// More checks if we're assigning the ticket at create-time.
if ($GLOBALS['registry']->getAuth() && $whups_driver->isCategory('assigned', $vars->get('state'))) {
$form4 = new Whups_Form_Ticket_CreateStepFour($vars);
$form4->useToken(false);
if (!$form4->validate($vars, true)) {
throw new Whups_Exception('Invalid arguments (' . var_export($form4->getErrors(), true) . ')');
}
$form4->getInfo($vars, $info);
}
$ticket = Whups_Ticket::newTicket($info, $GLOBALS['registry']->getAuth());
return $ticket->getId();
}
/**
* Update a ticket's properties.
*
* @param integer $ticket_id The id of the id to changes.
* @param array $ticket_info The attributes to set, from
* Whups_Form_Ticket_Edit.
*
* @return boolean True
*/
public function updateTicket($ticket_id, $ticket_info)
{
global $whups_driver;
// Cast as an int for safety.
$ticket = Whups_Ticket::makeTicket((int)$ticket_id);
// Check that we have permission to update the ticket
if (!$GLOBALS['registry']->getAuth() ||
!Whups::hasPermission($ticket->get('queue'), 'queue', 'update')) {
throw new Whups_Exception_PermissionDenied(_('You do not have permission to update this ticket.'));
}
// Populate $vars with existing ticket details.
$vars = new Horde_Variables();
$ticket->setDetails($vars, true);
// Copy new ticket details in.
foreach ($ticket_info as $detail => $newval) {
$vars->set($detail, $newval);
}
// Create and populate the EditTicketForm for validation. API calls can't
// use form tokens and aren't the result of the EditTicketForm being
// submitted.
$editform = new Whups_Form_Ticket_Edit($vars, $ticket);
$editform->useToken(false);
$editform->setSubmitted(true);
// Attempt to validate and update the ticket.
if (!$editform->validate($vars)) {
$form_errors = var_export($editform->getErrors(), true);
throw new Whups_Exception(sprintf(_("Invalid ticket data supplied: %s"), $form_errors));
}
$editform->getInfo($vars, $info);
$ticket->change('summary', $info['summary']);
$ticket->change('state', $info['state']);
$ticket->change('due', $info['due']);
$ticket->change('priority', $info['priority']);
if (!empty($info['newcomment'])) {
$ticket->change('comment', $info['newcomment']);
}
// Update attributes.
$whups_driver->setAttributes($info, $ticket);
// Add attachment if one was uploaded.
if (!empty($info['newattachment']['name'])) {
$ticket->change('attachment',
array('name' => $info['newattachment']['name'],
'tmp_name' => $info['newattachment']['tmp_name']));
}
// If there was a new comment and permissions were specified on
// it, set them.
if (!empty($info['group'])) {
$ticket->change('comment-perms', $info['group']);
}
$ticket->commit();
// Ticket updated successfully
return true;
}
/**
* Add a comment to a ticket.
*
* @param integer $ticket_id The id of the ticket to comment on.
* @param string $comment The comment text to add.
* @param string $group (optional) Restrict this comment to a specific group.
*
* @return boolean True
*/
public function addComment($ticket_id, $comment, $group = null)
{
$ticket_id = (int)$ticket_id;
if (empty($ticket_id)) {
throw new Whups_Exception('Invalid ticket id');
}
$ticket = Whups_Ticket::makeTicket($ticket_id);
if (empty($comment)) {
throw new Whups_Exception('Empty comments are not allowed');
}
// Add comment.
$ticket->change('comment', $comment);
// Add comment permissions, if specified.
// @TODO: validate the user is allowed to specify this group
if (!empty($group)) {
$ticket->change('comment-perms', $group);
}
$ticket->commit();
return true;
}
/**
* Adds an attachment to a ticket.
*
* @param integer $ticket_id The ticket number.
* @param string $name The name of the attachment.
* @param string $data The attachment data.
*
* @throws Whups_Exception
*/
public function addAttachment($ticket_id, $name, $data)
{
$ticket_id = (int)$ticket_id;
if (empty($ticket_id)) {
throw new Whups_Exception(_("Invalid Ticket Id"));
}
$ticket = Whups_Ticket::makeTicket($ticket_id);
if (!strlen($name) || !strlen($data)) {
throw new Whups_Exception(_("Empty attachment"));
}
$tmp_name = Horde_Util::getTempFile('whups', true, $GLOBALS['conf']['tmpdir']);
$fp = fopen($tmp_name, 'wb');
fwrite($fp, $data);
fclose($fp);
$ticket->change('attachment', array('name' => $name, 'tmp_name' => $tmp_name));
$ticket->commit();
}
/**
* Set attributes for a ticket
*
* @TODO fold this into the updateTicket method
*/
public function setTicketAttributes($info)
{
global $whups_driver;
if (!isset($info['ticket_id']) || !isset($info['attributes'])) {
throw new Whups_Exception(_("Invalid arguments: Must supply a ticket number and new attributes."));
}
// Convert the RPC parameters into what we'd expect if we were
// posting the EditAttributes form.
$ainfo = array();
foreach ($info['attributes'] as $attrib) {
if (!isset($attrib['id']) || !isset($attrib['value'])) {
throw new InvalidArgumentException(_("Invalid argument: Missing attribute name or value."));
}
$ainfo['a' . $attrib['id']] = $attrib['value'];
}
$ainfo['id'] = $info['ticket_id'];
return $whups_driver->setAttributes($ainfo);
}
/**
* Get the types that Whups items can be listed as.
*
* @return array Array of list types.
*/
public function getListTypes()
{
return array('taskHash' => true);
}
/**
* Get a list of items from whups as type $type.
*
* @param string $type The list type to use (@see getListTypes). Currently supported: 'taskHash'
*
* @return array An array of tickets.
*/
public function listAs($type)
{
switch ($type) {
case 'taskHash':
global $whups_driver;
$info = array(
'owner' => 'user:' . $GLOBALS['registry']->getAuth(),
'nores' => true);
$tickets = $whups_driver->getTicketsByProperties($info);
$result = array();
foreach ($tickets as $ticket) {
$view_link = Whups::urlFor('ticket', $ticket['id'], true);
$delete_link = Whups::urlFor('ticket_action', array('delete', $ticket['id']), true);
$complete_link = Whups::urlFor('ticket_action', array('update', $ticket['id']), true);
$result['whups/' . $ticket['id']] = array(
'task_id' => $ticket['id'],
'completed' => ($ticket['state_category'] == 'resolved'),
'name' => '[' . _("Ticket") . ' #' . $ticket['id'] . '] ' . $ticket['summary'],
'desc' => null,
'due' => null,
'view_link' => $view_link,
'delete_link' => $delete_link,
'edit_link' => $view_link,
'complete_link' => $complete_link
);
}
break;
default:
$result = array();
break;
}
return $result;
}
/**
* Return a list of queues that the current user has read permissions for
*
* @return array Array of queue details
*/
public function listQueues()
{
return Whups::permissionsFilter($GLOBALS['whups_driver']->getQueuesInternal(), 'queue', Horde_Perms::SHOW);
}
/**
* Return a list of slugs that the current user has read permissions for
*
* @return array Array of queue details
*/
public function listSlugs()
{
return Whups::permissionsFilter($GLOBALS['whups_driver']->getSlugs(), 'queue', Horde_Perms::SHOW);
}
/**
* Get details for a queue
*
* @param array | integer $queue Either an array of queue ids or a single queue id.
*
* @return array An array of queue information (or an array of arrays, if multiple queues were passed).
*/
public function getQueueDetails($queue)
{
if (is_array($queue)) {
$queues = Whups::permissionsFilter($queue, 'queue_id');
$details = array();
foreach ($queues as $id) {
$details[$id] = $GLOBALS['whups_driver']->getQueueInternal($id);
}
return $details;
}
$queues = Whups::permissionsFilter(array($queue), 'queue_id');
if ($queues) {
return $GLOBALS['whups_driver']->getQueueInternal($queue);
}
return array();
}
/**
* List the versions associated with a queue
*
* @param integer $queue The queue id to get versions for.
*
* @return array Array of queue versions
*/
public function listVersions($queue)
{
$queues = Whups::permissionsFilter(array($queue), 'queue_id');
if (!$queues) {
return array();
}
$versions = array();
$version_list = $GLOBALS['whups_driver']->getVersionInfoInternal($queue);
foreach ($version_list as $version) {
$versions[] = array('id' => $version['version_id'],
'name' => $version['version_name'],
'description' => $version['version_description'],
'active' => !empty($version['version_active']),
'readonly' => false);
}
usort($versions, array($this, '_sortVersions'));
return $versions;
}
private function _sortVersions($a, $b)
{
$a_number = (string)(int)$a['name'][0] === $a['name'][0];
$b_number = (string)(int)$b['name'][0] === $b['name'][0];
if ($a_number && $b_number) {
return version_compare($b['name'], $a['name']);
}
if (!$a_number && !$b_number) {
return strcoll($b['name'], $a['name']);
}
return $a_number ? 1 : -1;
}
/**
* Add a version to a queue
*
* @param integer $queue The queue id to add the version to.
* @param string $name The name of the new version.
* @param string $description The descriptive text for the new version.
* @param boolean $active Whether the version is still active.
*/
public function addVersion($queue, $name, $description, $active = true)
{
return $GLOBALS['whups_driver']->addVersion($queue, $name, $description, $active);
}
/**
* Return the details for a queue version
*
* @param integer $version_id The version to fetch
*
* @return array Array of version details
*/
public function getVersionDetails($version_id)
{
return $GLOBALS['whups_driver']->getVersionInternal($version_id);
}
/**
* Get the all tickets for a queue, optionally with a specific state.
*
* @param integer $queue_id The queue to get tickets for
* @param string $state The state filter, if any.
*
* @return array Array of tickets
*/
public function getTicketDetails($queue_id, $state = null)
{
global $whups_driver;
$info['queue_id'] = $queue_id;
if (!empty($state)) {
$info['category'] = $state;
}
$tickets = $whups_driver->getTicketsByProperties($info);
for ($i = 0; $i < count($tickets); $i++) {
$view_link = Whups::urlFor('ticket', $tickets[$i]['id'], true);
$delete_link = Whups::urlFor('ticket_action', array('delete', $tickets[$i]['id']), true);
$complete_link = Whups::urlFor('ticket_action', array('update', $tickets[$i]['id']), true);
$tickets[$i] = array(
'ticket_id' => $tickets[$i]['id'],
'completed' => ($tickets[$i]['state_category'] == 'resolved'),
'assigned' => ($tickets[$i]['state_category'] == 'assigned'),
'name' => $tickets[$i]['queue_name'] . ' #' .
$tickets[$i]['id'] . ' - ' . $tickets[$i]['summary'],
'state' => $tickets[$i]['state_name'],
'type' => $tickets[$i]['type_name'],
'priority' => $tickets[$i]['priority_name'],
'desc' => null,
'due' => null,
'category' => null,
'view_link' => $view_link,
'delete_link' => $delete_link,
'edit_link' => $view_link,
'complete_link' => $complete_link
);
}
return $tickets;
}
/**
* List cost objects
*
* @param array $criteria The list criteria
*
* @return array Tickets (as cost objects) matching $criteria
*/
public function listCostObjects($criteria)
{
global $whups_driver;
$info = array();
if (!empty($criteria['user'])) {
$info['owner'] = 'user:' . $GLOBALS['registry']->getAuth();
}
if (!empty($criteria['active'])) {
$info['nores'] = true;
}
if (!empty($criteria['id'])) {
$info['id'] = $criteria['id'];
}
$tickets = $whups_driver->getTicketsByProperties($info);
$result = array();
foreach ($tickets as $ticket) {
$result[$ticket['id']] = array(
'id' => $ticket['id'],
'active' => ($ticket['state_category'] != 'resolved'),
'name' => sprintf(
_("Ticket %s - %s"), $ticket['id'], $ticket['summary']));
/* If the user has an estimate attribute, use that for cost object
* hour estimates. */
$attributes = $whups_driver->getTicketAttributesWithNames($ticket['id']);
foreach ($attributes as $k => $v) {
if (Horde_String::lower($k) == _("estimated time") ||
Horde_String::lower($k) == 'estimated time') {
if (!empty($v)) {
$result[$ticket['id']]['estimate'] = (double) $v;
}
}
}
}
ksort($result);
if (count($result) == 0) {
return array();
} else {
return array(
array(
'category' => _("Tickets"),
'objects' => array_values($result)));
}
}
/**
* List the ways that tickets can be treated as time objects
*
* @return array Array of time object types
*/
public function listTimeObjectCategories()
{
return array('created' => array('title' => _("My tickets by creation date"), 'type' => 'single'),
'assigned' => array('title' => _("My tickets by assignment date"), 'type' => 'single'),
'due' => array('title' => _("My tickets by due date"), 'type' => 'single'),
'resolved' => array('title' => _("My tickets by resolution date"), 'type' => 'single')
);
}
/**
* Lists tickets with due dates as time objects.
*
* @param array $categories The time categories (from listTimeObjectCategories) to list.
* @param mixed $start The start date of the period.
* @param mixed $end The end date of the period.
*/
public function listTimeObjects($categories, $start, $end)
{
global $whups_driver;
$start = new Horde_Date($start);
$start_ts = $start->timestamp();
$end = new Horde_Date($end);
$end_ts = $end->timestamp();
$criteria['owner'] = Whups::getOwnerCriteria($GLOBALS['registry']->getAuth());
/* @TODO Use $categories */
$category = 'due';
switch ($category) {
case 'assigned':
$label = _("Assigned");
$criteria['ass'] = true;
break;
case 'created':
$label = _("Created");
break;
case 'due':
$label = _("Due");
$criteria['nores'] = true;
break;
case 'resolved':
$label = _("Resolved");
$criteria['res'] = true;
break;
}
try {
$tickets = $whups_driver->getTicketsByProperties($criteria);
} catch (Whups_Exception $e) {
return array();
}
$objects = array();
foreach ($tickets as $ticket) {
switch ($category) {
case 'assigned':
$t_start = $ticket['date_assigned'];
break;
case 'created':
$t_start = $ticket['timestamp'];
break;
case 'due':
if (empty($ticket['due'])) {
continue 2;
}
$t_start = $ticket['due'];
break;
case 'resolved':
$t_start = $ticket['date_resolved'];
break;
}
if ($t_start + 1 < $start_ts || $t_start > $end_ts) {
continue;
}
$t = new Whups_Ticket($ticket['id'], $ticket);
$objects[$ticket['id']] = array(
'title' => sprintf('%s: [#%s] %s', $label, $ticket['id'], $ticket['summary']),
'description' => $t->toString(),
'id' => $ticket['id'],
'start' => date('Y-m-d\TH:i:s', $t_start),
'end' => date('Y-m-d\TH:i:s', $t_start + 1),
'params' => array('id' => $ticket['id']),
'link' => Whups::urlFor('ticket', $ticket['id'], true)
);
}
return $objects;
}
}
|