reserve.ajax 19.4 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2000-2017 University of Utah and the Flux Group.
Leigh B Stoller's avatar
Leigh B Stoller committed
4
#
5
# {{{EMULAB-LICENSE
Leigh B Stoller's avatar
Leigh B Stoller committed
6
#
7
# This file is part of the Emulab network testbed software.
Leigh B Stoller's avatar
Leigh B Stoller committed
8
#
9 10 11 12
# This file 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.
Leigh B Stoller's avatar
Leigh B Stoller committed
13
#
14 15 16 17
# This file 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.
Leigh B Stoller's avatar
Leigh B Stoller committed
18
#
19 20
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
Leigh B Stoller's avatar
Leigh B Stoller committed
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
# }}}
#
chdir("..");
include_once("webtask.php");
chdir("apt");
include_once("instance_defs.php");

# We will set this in CheckForm();
$target_project = null;

#
# Validate a reservation.
#
function Do_Validate()
{
    global $ajax_args;
    $ajax_args["validate"] = 1;
    return Do_Reserve();
}

#
# Request a reservation.
#
function Do_Reserve()
{
    global $this_user;
    global $ajax_args;
49
    global $suexec_output, $TB_PROJECT_CREATEEXPT;
50
    global $PORTAL_GENESIS;
Leigh B Stoller's avatar
Leigh B Stoller committed
51

52 53 54 55 56 57 58
    if (!isset($ajax_args["formfields"])) {
	SPITAJAX_ERROR(-1, "Missing formfields");
	return -1;
    }
    $formfields = $ajax_args["formfields"];
    $errors     = array();

Leigh B Stoller's avatar
Leigh B Stoller committed
59
    #error_log(print_r($formfields, TRUE));
60

61 62 63
    #
    # All fields required,
    #
64
    $required = array("pid", "count", "end", "cluster", "type");
65 66 67 68 69 70

    foreach ($required as $field) {
	if (!isset($formfields[$field]) || $formfields[$field] == "") {
	    $errors[$field] = "Missing field";
	}
    }
71
    #
Leigh B Stoller's avatar
Leigh B Stoller committed
72
    # Update mode; we get the uuid of the current reservation.
73
    #
Leigh B Stoller's avatar
Leigh B Stoller committed
74 75 76 77
    if (isset($formfields["uuid"])) {
        $uuid = $formfields["uuid"];
        if (!IsValidUUID($uuid)) {
            $errors["error"] = "Bad uuid for reservation update";
78 79
        }
    }
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    # Admins do not need a reason.
    if (!ISADMIN() &&
        (!isset($formfields["reason"]) || $formfields["reason"] == "")) {
        $errors["reason"] = "Missing field";
    }
    if (count($errors)) {
	SPITAJAX_ERROR(2, $errors);
	return;
    }
    $project = Project::Lookup($formfields["pid"]);
    if (!$project) {
        $errors["pid"] = "No such project";
    }
    if (!TBvalid_integer($formfields["count"])) {
        $errors["count"] = TBFieldErrorString();
    }
96 97 98 99 100 101
    if (isset($formfields["start"]) && $formfields["start"] != "") {
        $start_ascii = $formfields["start"];
        $start = strtotime($formfields["start"]);
        if (!$start) {
            $errors["start"] = "Cannot parse date";
        }
Leigh B Stoller's avatar
Leigh B Stoller committed
102
        elseif ($start < time() && !isset($uuid)) {
103 104 105
            # In update mode, just let it through.
            $errors["start"] = "In the past";
        }
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
    }
    $end_ascii = $formfields["end"];
    $end = strtotime($formfields["end"]);
    if (!$end) {
        $errors["end"] = "Cannot parse date";
    }
    if (isset($start) && isset($end) && $end < $start) {
        $errors["end"] = "End is before start";
    }
    if (!TBvalid_node_type($formfields["type"])) {
        $errors["type"] = TBFieldErrorString();
    }
    if (!Instance::ValidURN($formfields["cluster"])) {
        $errors["cluster"] = TBFieldErrorString();
    }
    $aggregate = Aggregate::Lookup($formfields["cluster"]);
    if (!$aggregate) {
        $errors["cluster"] = "No such cluster";
    }
    if (!TBvalid_why($formfields["reason"])) {
        $errors["reason"] = TBFieldErrorString();
    }
    if (!ISADMIN() &&
        !$project->AccessCheck($this_user, $TB_PROJECT_CREATEEXPT)) {
	$errors["error"] = "Not enough permission in project";
    }
    if (count($errors)) {
	SPITAJAX_ERROR(2, $errors);
	return;
    }
    # Allow for form precheck only. So JS code knows it will be fast.
    if (isset($ajax_args["checkonly"]) && $ajax_args["checkonly"]) {
        SPITAJAX_RESPONSE(0);
        return;
    }
    # Next we check the validity of the reservation.
    $this_uid = $this_user->uid();
    $pid      = $project->pid();
    $webtask  = WebTask::CreateAnonymous();
    $urn      = $aggregate->urn();

    #
    # OK, we are going to to run a precheck on the reservation to
    # make sure it is allowed.
    #
    $args = "-t " . escapeshellarg($formfields["type"]) . " ".
152
          "-p $PORTAL_GENESIS " .
Leigh B Stoller's avatar
Leigh B Stoller committed
153
          (isset($uuid) ? "-u $uuid " : "") .
154 155
          (isset($start) ? "-s $start " : "") .
          " -e $end $pid " . $formfields["count"];
156 157

    #
Leigh B Stoller's avatar
Leigh B Stoller committed
158
    # Do a precheck.
159 160 161 162 163
    #
    $retval = SUEXEC($this_uid, $pid,
                     "webmanage_reservations -t " . $webtask->task_id() . " ".
                     "-a $urn reserve -n $args", SUEXEC_ACTION_IGNORE);
    $webtask->Refresh();
164 165 166 167 168

    #
    # Exit code "2" indicates OK, but will need approval.
    #
    if ($retval && $retval != 2) {
169 170 171
        #
        # Positive value means the reservation is not possible. Treat
        # as a form error for the caller.
Leigh B Stoller's avatar
Leigh B Stoller committed
172
        # Otherwise internal error.
173 174 175 176 177 178 179 180 181 182 183 184 185 186
        #
        if ($retval < 0) {
            SPITAJAX_ERROR(-1, $suexec_output);
            SUEXECERROR(SUEXEC_ACTION_CONTINUE);
        }
        else {
            $errors["error"] = $webtask->TaskValue("output");
            SPITAJAX_ERROR(2, $errors);
        }
        $webtask->Delete();
	return;
    }
    # Return if that is all the caller wanted,
    if (isset($ajax_args["validate"]) && $ajax_args["validate"]) {
Leigh B Stoller's avatar
Leigh B Stoller committed
187 188 189
        # Positive retval means will need approval.
        $result = array("approved" => ($retval ? 0 : 1));
        SPITAJAX_RESPONSE($result);
190 191 192 193 194 195 196 197 198
        $webtask->Delete();
        return;
    }
    #
    # Do it for real, Cause of locking, this might fail anyway.
    #
    $webtask->Reset();

    #
Leigh B Stoller's avatar
Leigh B Stoller committed
199
    # But if we have a reason, send it along now.
200 201 202 203 204 205 206 207 208 209 210 211 212 213
    #
    if (isset($formfields["reason"]) && $formfields["reason"] != "") {
        $reasonfile = tempnam("/tmp", "reason");

        $fp = fopen($reasonfile, "w");
        fwrite($fp, $formfields["reason"]);
        fclose($fp);
        chmod($reasonfile, 0666);
        $args = "-N $reasonfile $args";
    }
    $retval = SUEXEC($this_uid, $pid,
                     "webmanage_reservations -t " . $webtask->task_id() . " ".
                     "-a $urn reserve $args", SUEXEC_ACTION_IGNORE);
    $webtask->Refresh();
Leigh B Stoller's avatar
Leigh B Stoller committed
214

215 216 217 218
    #
    # Exit code "2" indicates OK, but will need approval.
    #
    if ($retval && $retval != 2) {
219 220 221
        #
        # Positive value means the reservation is not possible. Treat
        # as a form error for the caller.
Leigh B Stoller's avatar
Leigh B Stoller committed
222
        # Otherwise internal error.
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
        #
        if ($retval < 0) {
            SPITAJAX_ERROR(-1, $suexec_output);
            SUEXECERROR(SUEXEC_ACTION_CONTINUE);
        }
        else {
            $errors["error"] = $webtask->TaskValue("output");
            SPITAJAX_ERROR(2, $errors);
        }
        if (isset($reasonfile)) {
            unlink($reasonfile);
        }
        $webtask->Delete();
	return;
    }
    if (isset($reasonfile)) {
        unlink($reasonfile);
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
241 242 243 244 245 246 247 248 249
    # Positive retval means will need approval.
    $result = array("approved" => ($retval ? 0 : 1));
    # We can form a url if we get the uuid
    if ($webtask->TaskValue("uuid")) {
        $uuid = $webtask->TaskValue("uuid");
        $nickname = $aggregate->nickname();
        $url = "reserve.php?edit=1&cluster=${nickname}&uuid=${uuid}";
        $result["url"] = $url;
    }
250
    $webtask->Delete();
Leigh B Stoller's avatar
Leigh B Stoller committed
251
    SPITAJAX_RESPONSE($result);
252 253 254 255 256 257 258 259 260 261
}

#
# Delete a reservation.
#
function Do_Delete()
{
    global $this_user;
    global $ajax_args;
    global $TB_PROJECT_CREATEEXPT, $suexec_output;
Leigh B Stoller's avatar
Leigh B Stoller committed
262 263 264

    if (!isset($ajax_args["uuid"])) {
	SPITAJAX_ERROR(-1, "Missing uuid");
265 266
	return -1;
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
267 268 269
    $uuid = $ajax_args["uuid"];
    if (!IsValidUUID($uuid)) {
        SPITAJAX_ERROR(-1, "Invalid uuid");
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
	return;
    }
    if (!isset($ajax_args["cluster"])) {
	SPITAJAX_ERROR(-1, "Missing cluster");
	return;
    }
    if (!preg_match("/^[-\w]+$/", $ajax_args["cluster"])) {
        SPITAJAX_ERROR(-1, "Invalid cluster name");
        return;
    }
    $aggregate = Aggregate::LookupByNickname($ajax_args["cluster"]);
    if (!$aggregate) {
        SPITAJAX_ERROR(-1, "No such cluster");
        return;
    }
    if (!isset($ajax_args["pid"])) {
	SPITAJAX_ERROR(-1, "Missing project");
	return;
    }
    $project = Project::Lookup($ajax_args["pid"]);
    if (!$project) {
	SPITAJAX_ERROR(-1, "No such project");
	return;
    }
    if (!ISADMIN() &&
        !$project->AccessCheck($this_user, $TB_PROJECT_CREATEEXPT)) {
	SPITAJAX_ERROR(-1, "Not enough permission in project");
	return;

    }
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
    #
    # Admins can add an optional cause when denying a reservation.
    #
    $opt = "";
    if (ISADMIN()) {
        if (isset($ajax_args["reason"])) {
            $reason  = $ajax_args["reason"];

            if (!TBvalid_fulltext($reason)) {
                SPITAJAX_ERROR(-1, "Illegal characters in reason");
                return;
            }
            $reasonfile = tempnam("/tmp", "reason");

            $fp = fopen($reasonfile, "w");
            fwrite($fp, $reason);
            fclose($fp);
            chmod($reasonfile, 0666);
            $opt = "-N $reasonfile";
        }
    }
321 322 323 324 325
    $this_uid = $this_user->uid();
    $pid = $project->pid();
    $urn = $aggregate->urn();

    $retval = SUEXEC($this_uid, $pid,
Leigh B Stoller's avatar
Leigh B Stoller committed
326
                     "webmanage_reservations -a '$urn' delete $opt $pid $uuid",
327
                     SUEXEC_ACTION_CONTINUE);
328 329 330
    if (isset($reasonfile)) {
        unlink($reasonfile);
    }
331 332 333 334 335 336 337
    if ($retval) {
        SPITAJAX_ERROR(-1, $suexec_output);
	return;
    }
    SPITAJAX_RESPONSE(1);
}

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
#
# Request info.
#
function Do_InfoOrWarn($warning = false)
{
    global $this_user;
    global $ajax_args;
    global $TB_PROJECT_CREATEEXPT, $suexec_output, $APTBASE, $APTMAIL;

    if (!isset($ajax_args["uuid"])) {
	SPITAJAX_ERROR(-1, "Missing uuid");
	return -1;
    }
    $uuid = $ajax_args["uuid"];
    if (!IsValidUUID($uuid)) {
        SPITAJAX_ERROR(-1, "Invalid uuid");
	return;
    }
    if (!isset($ajax_args["cluster"])) {
	SPITAJAX_ERROR(-1, "Missing cluster");
	return;
    }
    if (!preg_match("/^[-\w]+$/", $ajax_args["cluster"])) {
        SPITAJAX_ERROR(-1, "Invalid cluster name");
        return;
    }
    $nickname  = $ajax_args["cluster"];
    $aggregate = Aggregate::LookupByNickname($nickname);
    if (!$aggregate) {
        SPITAJAX_ERROR(-1, "No such cluster");
        return;
    }
    if (!isset($ajax_args["pid"])) {
	SPITAJAX_ERROR(-1, "Missing project");
	return;
    }
    $project = Project::Lookup($ajax_args["pid"]);
    if (!$project) {
	SPITAJAX_ERROR(-1, "No such project");
	return;
    }
    if (!isset($ajax_args["uid_idx"])) {
	SPITAJAX_ERROR(-1, "Missing uid idx");
	return;
    }
    $user = User::Lookup($ajax_args["uid_idx"]);
    if (!$user) {
	SPITAJAX_ERROR(-1, "No such user");
	return;
    }
    if (!warning &&
        (!isset($ajax_args["message"]) || $ajax_args["message"] == "")) {
	SPITAJAX_ERROR(-1, "Missing message");
	return;
    }
    if (!ISADMIN()) {
	SPITAJAX_ERROR(-1, "Only administrators can ask for info or warn");
	return;

    }
    $url = "${APTBASE}/reserve.php?edit=1&cluster=$nickname&uuid=$uuid\n";
    
    if ($warning) {
        $subject = "Unused Reservation Warning";
        $message = "Your reservation in project $pid is currently either\n".
            "underused or unused. Tying up resources in this manner means\n".
            "other users may not be able to get their work done. If your\n".
            "reservation is still underused in 24 hours, the reservation\n".
            "will be deleted.\n\n";

        if (isset($ajax_args["message"]) && $ajax_args["message"] != "") {
            $message .= $ajax_args["message"] . "\n";
        }
        $message .= "$url\n";
    }
    else {
        $subject = "Reservation Info Request";
        $message = $ajax_args["message"] . "\n\n";
        $message .= "$url\n";
    }
    TBMAIL($user->email(), $subject, $message,
           "From: $APTMAIL\n".
           "CC: $APTMAIL");

    sleep(1);
    SPITAJAX_RESPONSE(1);
}
function Do_WarnUser()
{
    Do_InfoOrWarn(true);
}
function Do_RequestInfo()
{
    Do_InfoOrWarn(false);
}

434 435 436 437 438 439 440
#
# Approve a reservation.
#
function Do_Approve()
{
    global $this_user;
    global $ajax_args;
Leigh B Stoller's avatar
Leigh B Stoller committed
441 442 443 444
    global $TB_PROJECT_CREATEEXPT, $suexec_output, $PORTAL_GENESIS;

    if (!isset($ajax_args["uuid"])) {
	SPITAJAX_ERROR(-1, "Missing uuid");
445 446
	return -1;
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
447 448 449
    $uuid = $ajax_args["uuid"];
    if (!IsValidUUID($uuid)) {
        SPITAJAX_ERROR(-1, "Invalid uuid");
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
	return;
    }
    if (!isset($ajax_args["cluster"])) {
	SPITAJAX_ERROR(-1, "Missing cluster");
	return;
    }
    if (!preg_match("/^[-\w]+$/", $ajax_args["cluster"])) {
        SPITAJAX_ERROR(-1, "Invalid cluster name");
        return;
    }
    $aggregate = Aggregate::LookupByNickname($ajax_args["cluster"]);
    if (!$aggregate) {
        SPITAJAX_ERROR(-1, "No such cluster");
        return;
    }
    if (!ISADMIN()) {
	SPITAJAX_ERROR(-1, "Only administrators can approve reservations");
	return;
    }
    $this_uid = $this_user->uid();
    $urn = $aggregate->urn();

    $retval = SUEXEC($this_uid, "nobody",
Leigh B Stoller's avatar
Leigh B Stoller committed
473 474
                     "webmanage_reservations -a '$urn' ".
                     "   approve -p $PORTAL_GENESIS $uuid",
475
                     SUEXEC_ACTION_CONTINUE);
Leigh B Stoller's avatar
Leigh B Stoller committed
476

477 478 479 480 481 482 483
    if ($retval) {
        SPITAJAX_ERROR(-1, $suexec_output);
	return;
    }
    SPITAJAX_RESPONSE("list-reservations.php");
}

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
#
# List reservations at a cluster (for a user).
#
function Do_ListReservations()
{
    global $this_user;
    global $ajax_args;
    global $TB_PROJECT_CREATEEXPT, $suexec_output;
    $anonarg = "";

    if (!isset($ajax_args["cluster"])) {
	SPITAJAX_ERROR(-1, "Missing cluster");
	return;
    }
    if (!preg_match("/^[-\w]+$/", $ajax_args["cluster"])) {
        SPITAJAX_ERROR(-1, "Invalid cluster name");
        return;
    }
    $aggregate = Aggregate::LookupByNickname($ajax_args["cluster"]);
    if (!$aggregate) {
        SPITAJAX_ERROR(-1, "No such cluster");
        return;
    }
    if (isset($ajax_args["anonymous"]) && !ISADMIN()) {
        $anonarg = "-A";
    }
    $this_uid = $this_user->uid();
    $urn = $aggregate->urn();
    $webtask = WebTask::CreateAnonymous();
    $webtask_id = $webtask->task_id();

    $retval = SUEXEC($this_uid, "nobody",
                     "webmanage_reservations -a '$urn' ".
                     "   -t $webtask_id list $anonarg",
                     SUEXEC_ACTION_CONTINUE);
    if ($retval) {
        $webtask->Delete();
        SPITAJAX_ERROR(-1, $suexec_output);
	return;
    }
    $webtask->Refresh();
525 526 527 528
    $resinfo = $webtask->TaskValue("value");
    $rlist   = array();
    
    foreach ($resinfo["reservations"] as $details) {
529
        $blob = array();
Leigh B Stoller's avatar
Leigh B Stoller committed
530 531
        $uuid = $details['uuid'];
        $blob["uuid"] = $uuid;
532 533 534 535 536 537 538 539 540
        #
        # Prune out stuff in anonymous mode since the backend is going
        # ask for all of the reservations with the -A option.
        #
        if (!isset($ajax_args["anonymous"])) {
            $blob["pid"]         = $details['pid'];
            $blob["pid_idx"]     = $details['pid_idx'];
            $blob["creator_uid"] = $details['uid'];
            $blob["creator_idx"] = $details['uid_idx'];
541 542 543
            if (isset($details["using"])) {
                $blob["using"]   = $details['using'];
            }
544
        }
545 546 547 548 549 550
        if (isset($details['approved']) && $details['approved'] != "") {
            $blob["approved"] = $details["approved"];
        }
        else {
            $blob["approved"] = null;
        }
551 552
        $blob["count"]       = $details['nodes'];
        $blob["type"]        = $details['type'];
Leigh B Stoller's avatar
Leigh B Stoller committed
553
        $blob["created"]     = $details['created'];
554 555 556
        $blob["start"]       = $details['start'];
        $blob["end"]         = $details['end'];
        $blob["cluster"]     = $aggregate->nickname();
Leigh B Stoller's avatar
Leigh B Stoller committed
557
        $rlist[$uuid] = $blob;
558
    }
559
    $resinfo["reservations"] = $rlist;
560
    $webtask->Delete();
561
    SPITAJAX_RESPONSE($resinfo);
562 563 564 565 566 567 568 569 570 571 572
}

#
# Get details of a specific reservation.
#
function Do_GetReservation()
{
    global $this_user;
    global $ajax_args;
    global $TB_PROJECT_CREATEEXPT, $suexec_output;

Leigh B Stoller's avatar
Leigh B Stoller committed
573 574
    if (!isset($ajax_args["uuid"])) {
	SPITAJAX_ERROR(-1, "Missing uuid");
575 576
	return -1;
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
577 578 579
    $uuid = $ajax_args["uuid"];
    if (!IsValidUUID($uuid)) {
        SPITAJAX_ERROR(-1, "Invalid uuid");
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
	return;
    }
    if (!isset($ajax_args["cluster"])) {
	SPITAJAX_ERROR(-1, "Missing cluster");
	return;
    }
    if (!preg_match("/^[-\w]+$/", $ajax_args["cluster"])) {
        SPITAJAX_ERROR(-1, "Invalid cluster name");
        return;
    }
    $aggregate = Aggregate::LookupByNickname($ajax_args["cluster"]);
    if (!$aggregate) {
        SPITAJAX_ERROR(-1, "No such cluster");
        return;
    }
    $this_uid = $this_user->uid();
    $urn = $aggregate->urn();
    $webtask = WebTask::CreateAnonymous();
    $webtask_id = $webtask->task_id();

    $retval = SUEXEC($this_uid, "nobody",
                     "webmanage_reservations -a '$urn' ".
Leigh B Stoller's avatar
Leigh B Stoller committed
602
                     "   -t $webtask_id list -i $uuid",
603 604 605 606 607 608 609
                     SUEXEC_ACTION_CONTINUE);
    if ($retval) {
        $webtask->Delete();
        SPITAJAX_ERROR(-1, $suexec_output);
	return;
    }
    $webtask->Refresh();
610 611
    $resinfo = $webtask->TaskValue("value");
    $value   = $resinfo["reservations"];
612 613
    $details = current($value);
    $blob = array();
Leigh B Stoller's avatar
Leigh B Stoller committed
614 615
    $uuid = $details['uuid'];
    $blob["uuid"]        = $uuid;
616
    $blob["pid"]         = $details['pid'];
Leigh B Stoller's avatar
Leigh B Stoller committed
617
    $blob["uuid"]        = $details['uuid'];
618 619 620 621 622 623 624 625 626
    $blob["pid_idx"]     = $details['pid_idx'];
    $blob["creator_uid"] = $details['uid'];
    $blob["creator_idx"] = $details['uid_idx'];
    $blob["count"]       = $details['nodes'];
    $blob["type"]        = $details['type'];
    $blob["start"]       = $details['start'];
    $blob["end"]         = $details['end'];
    $blob["notes"]       = $details['notes'];
    $blob["cluster"]     = $aggregate->urn();
627 628 629 630 631 632
    if (isset($details['approved']) && $details['approved'] != "") {
        $blob["approved"] = $details["approved"];
    }
    else {
        $blob["approved"] = null;
    }
633 634
    # For display.
    $blob["cluster_id"]  = $ajax_args["cluster"];
635 636 637 638
    $webtask->Delete();
    SPITAJAX_RESPONSE($blob);
}

639
#
Leigh B Stoller's avatar
Leigh B Stoller committed
640
# Ask for reservation system info, for a specific portal.
641 642 643 644 645 646
#
function Do_ReservationInfo()
{
    global $this_user;
    global $ajax_args;
    global $PORTAL_GENESIS;
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
    $aggregate = null;
    $sopts = "";
    $popts = "-p '$PORTAL_GENESIS'";

    if (isset($ajax_args["cluster"])) {
        if (!preg_match("/^[-\w]+$/", $ajax_args["cluster"])) {
            SPITAJAX_ERROR(-1, "Invalid cluster name");
            return;
        }
        $aggregate = Aggregate::LookupByNickname($ajax_args["cluster"]);
        if (!$aggregate) {
            SPITAJAX_ERROR(-1, "No such cluster");
            return;
        }
        $urn = $aggregate->urn();
        $sopts = "-a '$urn'";
        $popts = "";
    }
665 666 667 668 669

    $webtask = WebTask::CreateAnonymous();
    $webtask_id = $webtask->task_id();

    $retval = SUEXEC($this_user->uid(), "nobody",
670 671
                     "webmanage_reservations $sopts ".
                     "   -t $webtask_id prediction $popts ",
672 673 674 675 676 677 678 679
                     SUEXEC_ACTION_CONTINUE);
    if ($retval) {
        $webtask->Delete();
        SPITAJAX_ERROR(-1, $suexec_output);
	return;
    }
    $webtask->Refresh();
    $blob = $webtask->TaskValue("value");
680 681 682 683
    # If requesting for a single cluster, return just that.
    if (isset($aggregate)) {
        $blob = $blob[$aggregate->urn()];
    }
684 685 686 687
    $webtask->Delete();
    SPITAJAX_RESPONSE($blob);
}

688 689 690 691
# Local Variables:
# mode:php
# End:
?>