url_defs.php 20.9 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2006-2017 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# 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.
# 
# 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.
# 
# 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/>.
# 
# }}}
23 24 25 26 27 28 29 30
#

#
# This is my attempt at factoring our the zillions of URL addresses and
# their arguments. This will hopefully make it easier to change how we
# address things, like when I change the uid,pid,gid arguments to numeric
# IDs instead of names.
#
31 32 33 34 35 36 37 38 39 40
define("URLARG_UID",		"__uid");
define("URLARG_PID",		"__pid");
define("URLARG_GID",		"__gid");
define("URLARG_EID",		"__eid");
define("URLARG_NODEID",		"__nodeid");
define("URLARG_IMAGEID",	"__imageid");
define("URLARG_OSID",		"__osid");
define("URLARG_TEMPLATE",	"__template");
define("URLARG_INSTANCE",	"__instance");
define("URLARG_METADATA",	"__metadata");
41
define("URLARG_LOGFILE",	"__logfile");
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

define("PAGEARG_USER",		"user");
define("PAGEARG_PROJECT",	"project");
define("PAGEARG_GROUP",		"group");
define("PAGEARG_EXPERIMENT",	"experiment");
define("PAGEARG_TEMPLATE",	"template");
define("PAGEARG_INSTANCE",	"instance");
define("PAGEARG_METADATA",	"metadata");
define("PAGEARG_NODE",		"node");
define("PAGEARG_IMAGE",		"image");
define("PAGEARG_OSINFO",	"osinfo");
define("PAGEARG_UID",		"uid");
define("PAGEARG_PID",		"pid");
define("PAGEARG_GID",		"gid");
define("PAGEARG_EID",		"eid");
define("PAGEARG_GUID",		"guid");
define("PAGEARG_VERS",  	"version");
define("PAGEARG_NODEID",	"nodeid");
define("PAGEARG_IMAGEID",	"imageid");
define("PAGEARG_OSID",		"osid");
62
define("PAGEARG_LOGFILE",	"logfile");
63 64
define("PAGEARG_BOOLEAN",	"boolean");
define("PAGEARG_STRING",	"string");
65
define("PAGEARG_PASSWORD",	"password");
66 67 68 69
define("PAGEARG_INTEGER",	"integer");
define("PAGEARG_NUMERIC",	"numeric");
define("PAGEARG_ARRAY",		"array");
define("PAGEARG_ANYTHING",	"anything");
70
define("PAGEARG_ALPHALIST",     "alphalist");
71
define("PAGEARG_UUID",		"uuid");
72 73 74 75 76 77 78 79

define("URL_USER",		"user");
define("URL_PROJECT",		"project");
define("URL_GROUP",		"group");
define("URL_EXPERIMENT",	"experiment");
define("URL_TEMPLATE",		"template");
define("URL_INSTANCE",		"instance");
define("URL_METADATA",		"metadata");
80
define("URL_LOGFILE",		"logfile");
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
define("URL_NODE",		"node");
define("URL_IMAGE",		"image");
define("URL_OSINFO",		"osinfo");
define("URL_UID",		"uid");
define("URL_PID",		"pid");
define("URL_GID",		"gid");
define("URL_EID",		"eid");
# Temporary until all pages converted
define("URL_NODEID",		"node_id");
# This happens a lot!
define("URL_NODEID_ALT",	"nodeid");
define("URL_IMAGEID",		"imageid");
define("URL_OSID",		"osid");
define("URL_GUID",		"guid");
define("URL_INSTANCEID",	"exptidx");
define("URL_VERS",		"version");
# Temporary until all pages converted
define("URL_EXPTIDX",		"exptidx");
99 100

#
101 102
# Map page "ids" to the actual script names if there is anything unusual.
# Otherwise, default to foo.php3 if not in the list.
103
#
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
$url_mapping			    = array();
$url_mapping["suuser"]		    = "suuser.php";
$url_mapping["toggle"]		    = "toggle.php";
$url_mapping["changeuid"]	    = "changeuid.php";
$url_mapping["template_analyze"]    = "template_analyze.php";
$url_mapping["template_commit"]     = "template_commit.php";
$url_mapping["template_create"]     = "template_create.php";
$url_mapping["template_defs"]       = "template_defs.php";
$url_mapping["template_editevents"] = "template_editevents.php";
$url_mapping["template_export"]     = "template_export.php";
$url_mapping["template_exprun"]     = "template_exprun.php";
$url_mapping["template_graph"]      = "template_graph.php";
$url_mapping["template_history"]    = "template_history.php";
$url_mapping["template_metadata"]   = "template_metadata.php";
$url_mapping["template_modify"]     = "template_modify.php";
$url_mapping["template_show"]       = "template_show.php";
$url_mapping["template_swapin"]     = "template_swapin.php";
121
$url_mapping["template_search"]     = "template_search.php";
122
$url_mapping["record_revise"]       = "record_revise.php";
123 124 125 126 127 128 129 130
$url_mapping["linkgraph_image"]     = "linkgraph_image.php";
$url_mapping["resendapproval"]      = "resendapproval.php";
$url_mapping["showevents"]          = "showevents.php";
$url_mapping["spewevents"]          = "spewevents.php";
$url_mapping["spitreport"]          = "spitreport.php";
$url_mapping["statechange"]         = "statechange.php";
$url_mapping["experimentrun_show"]  = "experimentrun_show.php";
$url_mapping["instance_show"]       = "instance_show.php";
131
$url_mapping["cvswebwrap"]          = "cvsweb/cvswebwrap.php3";
132
$url_mapping["profile"]             = "profile.php";
133
$url_mapping["submitpub"]           = "submitpub.php";
Leigh Stoller's avatar
Leigh Stoller committed
134
$url_mapping["showslice"]           = "showslice.php";
135
$url_mapping["genihistory"]         = "genihistory.php";
136
$url_mapping["showmanifest"]        = "showmanifest.php";
137
$url_mapping["showslicelogs"]       = "showslicelogs.php";
138
$url_mapping["ssh-keys"]            = "ssh-keys.php";
139
$url_mapping["list-datasets"]       = "list-datasets.php";
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
	     
#
# The caller will pass in a page id, and a list of things. If the thing
# is a class we know about, then we generate the argument directly from
# it. For example, if the argument is a User instance, we know to append
# "user=$user->uid()" cause all pages that take uid arguments use
# the same protocol.
#
# If the thing is not something we know about, then its a key/value pair,
# with the next argument being the value.
# The key (say, "uid") is the kind of argument to be added to the URL,
# and the value (a string or an user class instance) is where we get
# the argument from. To be consistent across all pages, something like "uid"
# is mapped to "?user=$value" rather then "uid".
#
# Note that this idea comes from a google search for ways to deal with
# this problem. Nothing fancy.
#
function CreateURL($page_id)
{
    global $url_mapping;
    
    $pageargs = func_get_args();
    # drop the required argument.
    array_shift($pageargs);

166 167 168 169 170
    if (array_key_exists($page_id, $url_mapping)) {
	$newurl = $url_mapping[$page_id];
    }
    else {
	$newurl = "$page_id" . ".php3";
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    }

    # No args?
    if (! count($pageargs)) {
	return $newurl;
    }

    # Form an array of arguments to append.
    $url_args = array();

    while ($pageargs and count($pageargs)) {
	$key = array_shift($pageargs);

	#
	# Do we know about the object itself.
	#
	if (gettype($key) == "object") {
	    #
	    # Make up the key/value pair for below.
	    #
	    $val = $key;
	    
193 194 195 196
            #
            # In the cases, use both the lowercased version of the classname
            # and the real classname so this works under php4 and php5.
            #
197 198
	    switch (get_class($key)) {
	    case "user":
199
	    case "User":
200 201 202
		$key = URLARG_UID;
		break;
	    case "project":
203
	    case "Project":
204 205 206
		$key = URLARG_PID;
		break;
	    case "group":
207
	    case "Group":
208 209 210
		$key = URLARG_GID;
		break;
	    case "experiment":
211
	    case "Experiment":
212 213
		$key = URLARG_EID;
		break;
214
	    case "node":
215
	    case "Node":
216 217 218
		$key = URLARG_NODEID;
		break;
	    case "image":
219
	    case "Image":
220 221 222
		$key = URLARG_IMAGEID;
		break;
	    case "osinfo":
223
	    case "OSinfo":
224 225 226
		$key = URLARG_OSID;
		break;
	    case "template":
227
	    case "Template":
228 229 230
		$key = URLARG_TEMPLATE;
		break;
	    case "templateinstance":
231
	    case "TemplateInstance":
232 233 234
		$key = URLARG_INSTANCE;
		break;
	    case "templatemetadata":
235
	    case "TemplateMetadata":
236 237
		$key = URLARG_METADATA;
		break;
238
	    case "logfile":
239
	    case "Logfile":
240 241
		$key = URLARG_LOGFILE;
		break;
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
	    default:
		TBERROR("CreateURL: ".
			"Unknown object class - " . get_class($key), 1);
	    }
	}
	elseif (count($pageargs)) {
	    #
	    # A key/value pair so get the value from next argument.
	    #
	    $val = array_shift($pageargs);
	}
	else {
	    TBERROR("CreateURL: Malformed arguments for $key", 1);
	}

	#
	# Now process the key/value.
	#
	switch ($key) {
	case URLARG_UID:
	    $key = "user";
	    if (is_a($val, 'User')) {
		$val = $val->webid();
	    }
	    break;
	case URLARG_PID:
	    $key = "pid";
	    if (is_a($val, 'Project')) {
		$val = $val->pid();
	    }
	    break;
	case URLARG_GID:
	    $key = "gid";
	    if (is_a($val, 'Group')) {
276
		$url_args[] = "pid=" . $val->pid();
277 278 279 280 281
		$val = $val->gid();
	    }
	    break;
	case URLARG_EID:
	    if (is_a($val, 'Experiment')) {
282 283 284
		$key = "experiment";
		$val = $val->idx();
	    }
Leigh Stoller's avatar
Leigh Stoller committed
285
	    elseif (preg_match("/^[\d]+$/", "$val")) {
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
		$key = "experiment";
	    }
	    else {
		$key = "eid";
	    }
	    break;
	case URLARG_NODEID:
	    $key = "node_id";
	    if (is_a($val, 'Node')) {
		$val = $val->node_id();
	    }
	    break;
	case URLARG_IMAGEID:
	    $key = "imageid";
	    if (is_a($val, 'Image')) {
301 302 303 304 305
		$str = $val->imageid();
		if (1 || $val->version()) {
		    $str .= ":" . $val->version();
		}
		$val = $str;
306 307 308 309 310 311
	    }
	    $val = rawurlencode($val);
	    break;
	case URLARG_OSID:
	    $key = "osid";
	    if (is_a($val, 'OSinfo')) {
312 313 314 315 316
		$str = $val->osid();
		if (1 || $val->vers()) {
		    $str .= ":" . $val->vers();
		}
		$val = $str;
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
	    }
	    $val = rawurlencode($val);
	    break;
	case URLARG_TEMPLATE:
	    $key = "guid";
	    if (! is_a($val, 'Template')) {
		TBERROR("CreateURL: Must provide a Template object", 1);
	    }
	    $url_args[] = "version=" . $val->vers();
	    $val = $val->guid();
	    break;
	case URLARG_INSTANCE:
	    $key = "instance";
	    if (!is_a($val, 'TemplateInstance')) {
		TBERROR("CreateURL: Must provide a TemplateInstance object",1);
332
	    }
333 334 335 336 337 338 339 340
	    $val = $val->exptidx();
	    break;
	case URLARG_METADATA:
	    $key = "metadata";
	    if (!is_a($val, 'TemplateMetadata')) {
		TBERROR("CreateURL: Must provide a TemplateMetadata object",1);
	    }
	    $val = $val->guid() . "/" . $val->vers();
341
	    break;
342 343 344 345 346 347 348
	case URLARG_LOGFILE:
	    $key = "logfile";
	    if (is_a($val, 'Logfile')) {
		$val = $val->logid();
	    }
	    $val = rawurlencode($val);
	    break;
349 350 351 352 353 354 355 356 357
	default:
            # Use whatever it was we got.
	    ;
	}
	$url_args[] = "${key}=${val}";
    }
    $newurl .= "?" . implode("&", $url_args);
    return $newurl;
}
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

#
# This is other side; Given a list of arguments we want for the page,
# make sure we got the right stuff. For example, if I want an "experiment"
# the page needed to get either pid/eid, or exptidx arguments. Return
# an array of named objects.
#
function RequiredPageArguments()
{
    $argspec = func_get_args();

    return VerifyPageArguments($argspec, 1);
}
function OptionalPageArguments()
{
    $argspec = func_get_args();
    
    return VerifyPageArguments($argspec, 0);
}

function VerifyPageArguments($argspec, $required)
{
380 381 382 383 384 385 386 387 388 389 390
    global $drewheader;

    if ($drewheader) {
	trigger_error(
	    "PAGEHEADER called before VerifyPageArguments ".
	    "(called by RequiredPageArguments or OptionalPageArguments). ".
	    "Won't be able to return proper HTTP status code on Error ".
	    "in ". $_SERVER['SCRIPT_FILENAME'] . ",",
            E_USER_WARNING);
    }

391 392 393 394 395 396 397 398 399 400 401 402 403 404
    $result   = array();

    while ($argspec and count($argspec) > 1) {
	$name   = array_shift($argspec);
	$type   = array_shift($argspec);
	$yep    = 0;
	unset($object);

	switch ($type) {
	case PAGEARG_EXPERIMENT:
	    if (isset($_REQUEST[URL_EXPERIMENT])) {
		$idx = $_REQUEST[URL_EXPERIMENT];
		$yep = 1;

405
		if (ValidateArgument($name, PAGEARG_EXPERIMENT, $idx)) {
406 407 408 409 410 411 412
		    $object = Experiment::Lookup($idx);
		}
	    }
	    elseif (isset($_REQUEST[URL_EXPTIDX])) {
		$idx = $_REQUEST[URL_EXPTIDX];
		$yep = 1;

413
		if (ValidateArgument($name, PAGEARG_EXPERIMENT, $idx)) {
414 415 416 417 418 419 420 421 422
		    $object = Experiment::Lookup($idx);
		}
	    }
	    elseif (isset($_REQUEST[URL_PID]) &&
		    isset($_REQUEST[URL_EID])) {
		$pid = $_REQUEST[URL_PID];
		$eid = $_REQUEST[URL_EID];
		$yep = 1;

423 424
		if (ValidateArgument($name, PAGEARG_PID, $pid) &&
		    ValidateArgument($name, PAGEARG_EID, $eid)) {
425 426 427 428 429 430 431 432 433 434 435 436
		    $object = Experiment::LookupByPidEid($pid, $eid);
		}
	    }
	    break;

	case PAGEARG_TEMPLATE:
	    if (isset($_REQUEST[URL_GUID]) &&
		isset($_REQUEST[URL_VERS])) {
		$guid = $_REQUEST[URL_GUID];
		$vers = $_REQUEST[URL_VERS];
		$yep = 1;

437 438
		if (ValidateArgument($name, PAGEARG_GUID, $guid) &&
		    ValidateArgument($name, PAGEARG_VERS, $vers)) {
439 440 441
		    $object = Template::Lookup($guid, $vers);
		}
	    }
442 443 444 445 446 447 448 449 450 451 452 453 454
	    elseif (isset($_REQUEST[URL_TEMPLATE])) {
		$guidvers = $_REQUEST[URL_TEMPLATE];
		$yep = 1;
		
		if (preg_match("/^([\d]+)\/([\d]+)$/", $guidvers, $matches)) {
		    $guid = $matches[1];
		    $vers = $matches[2];
		    $object = Template::Lookup($guid, $vers);
		}
		else {
		    PAGEARGERROR("Invalid argument for '$type': $guidvers");
		}
	    }
455 456 457 458 459 460 461
	    break;

	case PAGEARG_INSTANCE:
	    if (isset($_REQUEST[URL_INSTANCE])) {
		$idx = $_REQUEST[URL_INSTANCE];
		$yep = 1;

462
		if (ValidateArgument($name, PAGEARG_INSTANCE, $idx)) {
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
		    $object = TemplateInstance::LookupByExptidx($idx);
		}
	    }
	    break;

	case PAGEARG_METADATA:
	    if (isset($_REQUEST[URL_METADATA])) {
		$guidvers = $_REQUEST[URL_METADATA];
		$yep = 1;
		
		if (preg_match("/^([\d]+)\/([\d]+)$/", $guidvers, $matches)) {
		    $guid = $matches[1];
		    $vers = $matches[2];
		    $object = TemplateMetadata::Lookup($guid, $vers);
		}
		else {
		    PAGEARGERROR("Invalid argument for '$type': $guidvers");
		}
	    }
	    break;

	case PAGEARG_PROJECT:
	    if (isset($_REQUEST[URL_PROJECT])) {
		$idx = $_REQUEST[URL_PROJECT];
		$yep = 1;

489
		if (ValidateArgument($name, PAGEARG_PROJECT, $idx)) {
490 491 492 493 494 495 496
		    $object = Project::Lookup($idx);
		}
	    }
	    elseif (isset($_REQUEST[URL_PID])) {
		$pid = $_REQUEST[URL_PID];
		$yep = 1;

497
		if (ValidateArgument($name, PAGEARG_PID, $pid)) {
498
		    $object = Project::Lookup($pid);
499 500 501 502 503 504 505 506 507
		}
	    }
	    break;

	case PAGEARG_GROUP:
	    if (isset($_REQUEST[URL_GROUP])) {
		$idx = $_REQUEST[URL_GROUP];
		$yep = 1;

508
		if (ValidateArgument($name, PAGEARG_GROUP, $idx)) {
509 510 511 512 513 514 515 516 517
		    $object = Group::Lookup($idx);
		}
	    }
	    elseif (isset($_REQUEST[URL_PID]) &&
		    isset($_REQUEST[URL_GID])) {
		$pid = $_REQUEST[URL_PID];
		$gid = $_REQUEST[URL_GID];
		$yep = 1;

518 519
		if (ValidateArgument($name, PAGEARG_PID, $pid) &&
		    ValidateArgument($name, PAGEARG_GID, $gid)) {
520 521 522 523 524 525 526 527 528 529
		    $object = Group::LookupByPidGid($pid, $gid);
		}
	    }
	    break;

	case PAGEARG_NODE:
	    if (isset($_REQUEST[URL_NODE])) {
		$idx = $_REQUEST[URL_NODE];
		$yep = 1;

530
		if (ValidateArgument($name, PAGEARG_NODE, $idx)) {
531 532 533 534 535 536 537
		    $object = Node::Lookup($idx);
		}
	    }
	    elseif (isset($_REQUEST[URL_NODEID])) {
		$nodeid = $_REQUEST[URL_NODEID];
		$yep    = 1;

538
		if (ValidateArgument($name, PAGEARG_NODEID, $nodeid)) {
539 540 541 542 543 544 545
		    $object = Node::Lookup($nodeid);
		}
	    }
	    elseif (isset($_REQUEST[URL_NODEID_ALT])) {
		$nodeid = $_REQUEST[URL_NODEID_ALT];
		$yep    = 1;

546
		if (ValidateArgument($name, PAGEARG_NODEID, $nodeid)) {
547 548 549 550 551 552 553 554 555 556
		    $object = Node::Lookup($nodeid);
		}
	    }
	    break;

	case PAGEARG_USER:
	    if (isset($_REQUEST[URL_USER])) {
		$idx = $_REQUEST[URL_USER];
		$yep = 1;

557
		if (ValidateArgument($name, PAGEARG_USER, $idx)) {
558 559 560 561 562 563
                    if (preg_match("/^\d+$/", $idx)) {
                        $object = User::Lookup($idx);
                    }
                    else {
                        $object = User::LookupByUid($idx);
                    }
564 565 566 567 568 569
		}
	    }
	    elseif (isset($_REQUEST[URL_UID])) {
		$uid = $_REQUEST[URL_UID];
		$yep    = 1;

570
		if (ValidateArgument($name, PAGEARG_UID, $uid)) {
571 572 573 574 575 576
		    $object = User::Lookup($uid);
		}
	    }
	    break;

	case PAGEARG_IMAGE:
577 578 579 580 581
	    $version = NULL;
	    if (isset($_REQUEST[URL_VERS])) {
		$version = $_REQUEST[URL_VERS];
		ValidateArgument($name, PAGEARG_VERS, $version);
	    }
582 583 584 585
	    if (isset($_REQUEST[URL_IMAGEID])) {
		$imageid = $_REQUEST[URL_IMAGEID];
		$yep    = 1;

586
		if (ValidateArgument($name, PAGEARG_UUID, $imageid, 0)) {
587
		    $object = Image::LookupByUUID($imageid, $version);
588
		}
589 590 591
		elseif (ValidateArgument($name, PAGEARG_IMAGE, $imageid)) {
		    $object = Image::Lookup($imageid, $version);
		}
592
	    }
Leigh Stoller's avatar
Leigh Stoller committed
593 594 595 596
	    elseif (isset($_REQUEST[$name]) && $_REQUEST[$name] != "") {
		$imageid = $_REQUEST[$name];
		$yep = 1;

597
		if (ValidateArgument($name, PAGEARG_IMAGE, $imageid)) {
598
		    $object = Image::Lookup($imageid, $version);
Leigh Stoller's avatar
Leigh Stoller committed
599 600
		}
	    }
601 602 603
	    break;

	case PAGEARG_OSINFO:
604 605 606 607 608
	    $version = NULL;
	    if (isset($_REQUEST[URL_VERS])) {
		$version = $_REQUEST[URL_VERS];
		ValidateArgument($name, PAGEARG_VERS, $version);
	    }
609 610 611 612
	    if (isset($_REQUEST[URL_OSID])) {
		$osid = $_REQUEST[URL_OSID];
		$yep  = 1;

613
		if (ValidateArgument($name, PAGEARG_OSINFO, $osid)) {
614
		    $object = OSinfo::Lookup($osid, $version);
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
		}
	    }
	    break;

	case PAGEARG_BOOLEAN:
	    if (isset($_REQUEST[$name]) && $_REQUEST[$name] != "") {
		$object = $_REQUEST[$name];
		$yep = 1;

		if (strcasecmp("$object", "yes") == 0 ||
		    strcasecmp("$object", "1") == 0 ||
		    strcasecmp("$object", "true") == 0 ||
		    strcasecmp("$object", "on") == 0) {
		    $object = True;
		}
		elseif (strcasecmp("$object", "no") == 0 ||
			strcasecmp("$object", "0") == 0 ||
			strcasecmp("$object", "false") == 0 ||
			strcasecmp("$object", "off") == 0) {
		    $object = False;
		}
	    }
	    break;

	case PAGEARG_INTEGER:
	case PAGEARG_NUMERIC:
	case PAGEARG_ARRAY:
642
	case PAGEARG_UUID:
643 644 645 646
	    if (isset($_REQUEST[$name]) && $_REQUEST[$name] != "") {
		$object = $_REQUEST[$name];
		$yep = 1;

647
		if (! ValidateArgument($name, $type, $object)) {
648 649 650 651 652 653 654 655 656 657 658 659 660
		    unset($object);
		}
	    }
	    break;
	    
	case PAGEARG_ANYTHING:
	    if (isset($_REQUEST[$name])) {
		$object = $_REQUEST[$name];
		$yep = 1;

                # Anything allowed, caller BETTER check it.
	    }
	    break;
661 662 663 664 665 666 667 668 669 670 671 672

	case PAGEARG_ALPHALIST:
	    if (isset($_REQUEST[$name])) {
		$object = $_REQUEST[$name];
		if (!preg_match("/^[\d\w\-\ \,]+$/",$object)) {
		    unset($object);
		}
		else {
		    $object = preg_split("/[\,\;]+\s*/",$_REQUEST[$name]);
		}
	    }
	    break;
673 674 675 676 677 678 679
	    
	case PAGEARG_STRING:
	    if (isset($_REQUEST[$name])) {
		$object = $_REQUEST[$name];
		$yep = 1;

                # Pages never get arguments with special chars. Check.
Russ Fish's avatar
Russ Fish committed
680
		if (preg_match("/[\'\"]/", $object)) {
681
		    $object = htmlspecialchars($object);
682 683 684 685
		    PAGEARGERROR("Invalid characters in '$name': $object");
		}
	    }
	    break;
686
	    
687 688 689 690 691 692 693 694 695 696 697 698
	case PAGEARG_NODEID:
	    if (isset($_REQUEST[$name])) {
		$object = $_REQUEST[$name];
		$yep = 1;

		if (!ValidateArgument($name, PAGEARG_NODEID, $object)) {
		    $object = htmlspecialchars($object);
		    PAGEARGERROR("Invalid characters in '$name': $object");
		}
	    }
	    break;
	    
699 700 701 702 703 704
	case PAGEARG_PASSWORD:
	    if (isset($_REQUEST[$name])) {
		$object = $_REQUEST[$name];
		$yep = 1;

                # Only printable chars.
Leigh Stoller's avatar
Leigh Stoller committed
705
		if (!preg_match("/^[\040-\176]*$/", $object)) {
706 707 708 709 710
		    PAGEARGERROR("Invalid characters in '$name'");
		}
	    }
	    break;
	    
711 712 713 714 715
	case PAGEARG_LOGFILE:
	    if (isset($_REQUEST[URL_LOGFILE])) {
		$logid = $_REQUEST[URL_LOGFILE];
		$yep   = 1;

716
		if (ValidateArgument($name, PAGEARG_LOGFILE, $logid)) {
717 718 719 720
		    $object = Logfile::Lookup($logid);
		}
	    }
	    break;
721 722 723 724 725 726 727
	}

	if (isset($object)) {
	    $result[$name]  = $object;
	    $GLOBALS[$name] = $object;
	}
	elseif ($yep) {
728 729 730 731 732
	    #
	    # Value supplied but could not be mapped to object.
	    # Lets make that clear in the error message.
	    #
	    USERERROR("Could not map page arguments to '$name'", 1);
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
	}
	elseif ($required) {
	    PAGEARGERROR("Must provide '$name' page argument");
	}
	else {
	    unset($GLOBALS[$name]);
	}
    }

    return $result;
}

#
# Validate a single argument is safe to pass along to a DB query.
#
748
function ValidateArgument($name, $type, $arg, $isfatal = 1)
749
{
750
    switch ($type) {
751 752 753 754 755 756 757 758 759 760
    case PAGEARG_UID:
    case PAGEARG_PID:
    case PAGEARG_GID:
    case PAGEARG_EID:
    case PAGEARG_NODEID:
    case PAGEARG_PROJECT:
    case PAGEARG_GROUP:
    case PAGEARG_USER:
    case PAGEARG_EXPERIMENT:
    case PAGEARG_NODE:
761
    case PAGEARG_LOGFILE:
762
	if (preg_match("/^[-\w]+$/", "$arg")) {
763 764 765 766 767 768 769 770
	    return 1;
	}
	break;

    case PAGEARG_IMAGEID:
    case PAGEARG_IMAGE:
    case PAGEARG_OSID:
    case PAGEARG_OSINFO:
771
	if (preg_match("/^[-\w\.\+:]+$/", "$arg")) {
772 773 774 775 776 777 778
	    return 1;
	}
	break;

    case PAGEARG_GUID:
    case PAGEARG_VERS:
    case PAGEARG_INSTANCE:
779
	if (preg_match("/^[\d]+$/", "$arg")) {
780 781 782 783 784
	    return 1;
	}
	break;

    case PAGEARG_METADATA:
785
	if (preg_match("/^[\d]+\/[\d]+$/", "$arg")) {
786 787 788 789
	    return 1;
	}
	break;

790
    case PAGEARG_INTEGER:
791 792 793 794 795 796
    case PAGEARG_NUMERIC:
	if (is_numeric($arg)) {
	    return 1;
	}
	break;

797 798 799 800 801 802
    case PAGEARG_UUID:
	if (preg_match("/^\w+\-\w+\-\w+\-\w+\-\w+$/", "$arg")) {
	    return 1;
	}
	break;

803 804 805 806 807 808 809 810 811 812
    case PAGEARG_ARRAY:
	if (is_array($arg)) {
	    return 1;
	}
	break;

    default:
	TBERROR("ValidateArgument: ".
		"Unknown argument type - $name", 1);
    }
813 814 815
    if ($isfatal) {
        PAGEARGERROR("Argument '$name' should be of type '$type'");
    }
816 817
    return 0;
}
818
?>