profile_defs.php 23.1 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2006-2019 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 23 24
# 
# {{{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/>.
# 
# }}}
#
#
25 26 27 28 29 30

function TBvalid_rspec($token) {
    return TBcheck_dbslot($token, "apt_profiles", "rspec",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}

31 32 33 34 35 36 37 38
class Profile
{
    var	$profile;
    var $project;

    #
    # Constructor by lookup on unique index.
    #
39 40
    function Profile($token, $version = null) {
	$safe_profileid = addslashes($token);
Leigh Stoller's avatar
Leigh Stoller committed
41
	
42
	if (preg_match("/^\w+\-\w+\-\w+\-\w+\-\w+$/", $token)) {
43 44 45 46 47
	    #
	    # First look to see if the uuid is for the profile itself,
	    # which means current version. Otherwise look for a
	    # version with the uuid.
	    #
48
	    $query_result =
49
		DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid, ".
50 51
                            "    i.disabled as profile_disabled, ".
                            "    i.nodelete as profile_nodelete ".
52 53 54 55 56 57 58 59
			    "  from apt_profiles as i ".
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid and ".
			    "     v.version=i.version ".
			    "where i.uuid='$token' and v.deleted is null");

	    if (!$query_result || !mysql_num_rows($query_result)) {
		$query_result =
60
		    DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid, ".
61 62
                                "    i.disabled as profile_disabled, ".
                                "    i.nodelete as profile_nodelete ".
63 64 65 66 67 68
				"  from apt_profile_versions as v ".
				"left join apt_profiles as i on ".
				"     v.profileid=i.profileid ".
				"where v.uuid='$token' and ".
				"      v.deleted is null");
	    }
Leigh Stoller's avatar
Leigh Stoller committed
69
	}
70
	elseif (is_null($version)) {
Leigh Stoller's avatar
Leigh Stoller committed
71
	    $query_result =
72
		DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid, ".
73 74
                            "    i.disabled as profile_disabled, ".
                            "    i.nodelete as profile_nodelete ".
75
			    "  from apt_profiles as i ".
76 77 78 79
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid and ".
			    "     v.version=i.version ".
			    "where i.profileid='$safe_profileid'");
Leigh Stoller's avatar
Leigh Stoller committed
80
	}
81 82 83
	else {
	    $safe_version = addslashes($version);
	    $query_result =
84
	        DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid, ".
85 86
                            "    i.disabled as profile_disabled, ".
                            "    i.nodelete as profile_nodelete ".
87
			    "  from apt_profile_versions as v ".
88 89 90 91 92
			    "left join apt_profiles as i on ".
			    "     i.profileid=v.profileid ".
			    "where v.profileid='$safe_profileid' and ".
			    "      v.version='$safe_version' and ".
			    "      v.deleted is null");
Leigh Stoller's avatar
Leigh Stoller committed
93
	}
94 95 96 97 98 99 100 101 102 103 104 105 106 107
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->profile = null;
	    return;
	}
	$this->profile = mysql_fetch_array($query_result);

	# Load lazily;
	$this->project    = null;
    }
    # accessors
    function field($name) {
	return (is_null($this->profile) ? -1 : $this->profile[$name]);
    }
    function name()	    { return $this->field('name'); }
108 109
    function profileid()    { return $this->field('profileid'); }
    function version()      { return $this->field('version'); }
110 111
    function creator()	    { return $this->field('creator'); }
    function creator_idx()  { return $this->field('creator_idx'); }
112 113
    function updater()	    { return $this->field('updater'); }
    function updater_idx()  { return $this->field('updater_idx'); }
114 115 116
    function pid()	    { return $this->field('pid'); }
    function pid_idx()	    { return $this->field('pid_idx'); }
    function created()	    { return $this->field('created'); }
117 118
    function published()    { return $this->field('published'); }
    function deleted()	    { return $this->field('deleted'); }
119
    function uuid()	    { return $this->field('uuid'); }
120
    function profile_uuid() { return $this->field('profile_uuid'); }
121 122 123 124
    function ispublic()	    { return $this->field('public'); }
    function shared()	    { return $this->field('shared'); }
    function listed()	    { return $this->field('listed'); }
    function rspec()	    { return $this->field('rspec'); }
125
    function script()	    { return $this->field('script'); }
126
    function paramdefs()    { return $this->field('paramdefs'); }
127 128
    function locked()	    { return $this->field('status'); }
    function status()	    { return $this->field('locked'); }
129
    function topdog()	    { return $this->field('topdog'); }
130
    function disabled()	    { return $this->field('disabled'); }
131
    function nodelete()	    { return $this->field('nodelete'); }
132
    function project_write(){ return $this->field('project_write'); }
133 134 135
    function repourl()	    { return $this->field('repourl'); }
    function reponame()	    { return $this->field('reponame'); }
    function repohash()	    { return $this->field('repohash'); }
136
    function repokey()	    { return $this->field('repokey'); }
137
    function webtask_id()   { return $this->field('webtask_id'); }
138 139
    function lastused()     { return $this->field('lastused'); }
    function usecount()     { return $this->field('usecount'); }
140
    function profile_disabled()    { return $this->field('profile_disabled'); }
141 142
    function parent_profileid()    { return $this->field('parent_profileid'); }
    function parent_version()      { return $this->field('parent_version'); }
143
    function profile_nodelete()    { return $this->field('profile_nodelete'); }
Leigh Stoller's avatar
Leigh Stoller committed
144
    function portal_converted()    { return $this->field('portal_converted'); }
145 146 147 148 149

    # Private means only in the same project.
    function IsPrivate() {
	return !($this->ispublic() || $this->shared());
    }
150 151 152 153
    # PP profiles have parameter defs.
    function isParameterized() {
	return ($this->paramdefs() != "" ? 1 : 0);
    }
154 155 156 157
    # A profile is disabled if version is disabled or entire profile is disabled
    function isDisabled() {
	return ($this->disabled() || $this->profile_disabled());
    }
158 159 160 161
    # Ditto nodelete.
    function isLocked() {
	return ($this->nodelete() || $this->profile_nodelete());
    }
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    # Grab the webtask. Backwards compat mode, see if there is one associated
    # with the object, use that. Otherwise create a new one.
    function WebTask() {
        if ($this->webtask_id()) {
            return WebTask::Lookup($this->webtask_id());
        }
        $webtask = WebTask::LookupByObject($this->uuid());
        if (!$webtask) {
            $webtask = WebTask::CreateAnonymous();
            if (!$webtask) {
                return null;
            }
        }
        $profileid  = $this->profileid();
        $webtask_id = $webtask->task_id();
        DBQueryFatal("update apt_profiles set ".
                     "  webtask_id='$webtask_id' ".
                     "where profileid='$profileid'");
        return $webtask;
    }
182 183 184 185 186 187
    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->profile);
    }

    # Lookup up a single profile by idx. 
188 189
    function Lookup($token, $version = null) {
	$foo = new Profile($token, $version);
190 191 192 193 194 195 196 197

	if ($foo->IsValid()) {
            # Insert into cache.
	    return $foo;
	}	
	return null;
    }

198
    function LookupByName($project, $name, $version = null) {
199 200 201 202 203 204
        if (is_object($project)) {
            $pid = $project->pid();
        }
        else {
            $pid = addslashes($project);
        }
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
	$safe_name = addslashes($name);

	if (preg_match("/^\w+\-\w+\-\w+\-\w+\-\w+$/", $name)) {
	    return Profile::Lookup($name);
	}
	elseif (is_null($version)) {
	    $query_result =
		DBQueryWarn("select i.profileid,i.version ".
			    "  from apt_profiles as i ".
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid and ".
			    "     v.version=i.version ".
			    "where i.pid='$pid' and ".
			    "      i.name='$safe_name'");
	}
	else {
	    $safe_version = addslashes($version);
	    $query_result =
223
		DBQueryWarn("select i.profileid,v.version ".
224 225 226 227 228 229 230
			    "  from apt_profiles as i ".
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid ".
			    "where i.pid='$pid' and ".
			    "      i.name='$safe_name' and ".
			    "      v.version='$safe_version'");
	}
231 232
	if ($query_result && mysql_num_rows($query_result)) {
	    $row = mysql_fetch_row($query_result);
233
	    return Profile::Lookup($row[0], $row[1]);
234 235 236
	}
	return null;
    }
237

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    #
    # Lookup the most recently published version of a profile.
    #
    function LookupMostRecentPublished() {
	$profileid = $this->profileid();

	$query_result = 
	    DBQueryWarn("select version from apt_profile_versions as v ".
			"where v.profileid='$profileid' and ".
			"      published is not null and ".
			"      deleted is null ".
			"order by published desc limit 1");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    return null;
	}
	$row = mysql_fetch_row($query_result);
	return Profile::Lookup($profileid, $row[0]);
    }

258 259 260 261 262 263 264
    #
    # Refresh an instance by reloading from the DB.
    #
    function Refresh() {
	if (! $this->IsValid())
	    return -1;

265 266
	$profileid = $this->profileid();
	$version   = $this->version();
267 268

	$query_result =
269 270
	    DBQueryWarn("select * from apt_profile_versions ".
			"where profileid='$profileid' and version='$version'");
271 272 273 274 275 276 277 278 279 280
    
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->profile    = NULL;
	    $this->project    = null;
	    return -1;
	}
	$this->profile    = mysql_fetch_array($query_result);
	$this->project    = null;
	return 0;
    }
Leigh Stoller's avatar
Leigh Stoller committed
281

282 283 284 285 286 287 288 289 290 291
    function UserHasProfiles($user) {
	$uid = $user->uid();

	$query_result =
	    DBQueryFatal("select profileid from apt_profile_versions ".
			 "where creator='$uid' and deleted is null");

	return mysql_num_rows($query_result);
    }

Leigh Stoller's avatar
Leigh Stoller committed
292
    #
293
    # URL. To the specific version of the profile.
Leigh Stoller's avatar
Leigh Stoller committed
294 295
    #
    function URL() {
296
        global $APTBASE, $ISVSERVER, $ISAPT;
Leigh Stoller's avatar
Leigh Stoller committed
297 298 299
	
	$uuid = $this->uuid();

300
	if ($this->ispublic() || (!$ISAPT && $this->shared())) {
301 302
	    $pid  = $this->pid();
	    $name = $this->name();
303
	    $vers = $this->version();
304 305 306 307 308 309 310 311 312 313 314 315 316
	    if ($ISVSERVER)
		return "$APTBASE/p/$pid/$name/$vers";
	    return "$APTBASE/instantiate.php?profile=$name".
		"&project=$pid&version=$vers";
	}
	else {
	    if ($ISVSERVER)
		return "$APTBASE/p/$uuid";	    
	    return "$APTBASE/instantiate.php?profile=$uuid";
	}
    }
    # And the URL of the profile itself.
    function ProfileURL() {
317
        global $APTBASE, $ISVSERVER, $ISAPT;
318 319 320
	
	$uuid = $this->profile_uuid();

321
	if ($this->ispublic() || (!$ISAPT && $this->shared())) {
322 323 324 325 326
	    $pid  = $this->pid();
	    $name = $this->name();
	    if ($ISVSERVER)
		return "$APTBASE/p/$pid/$name";
	    return "$APTBASE/instantiate.php?profile=$name&project=$pid";
327 328
	}
	else {
329 330 331
	    if ($ISVSERVER)
		return "$APTBASE/p/$uuid";	    
	    return "$APTBASE/instantiate.php?profile=$uuid";
332
	}
Leigh Stoller's avatar
Leigh Stoller committed
333
    }
334 335 336 337 338 339 340 341 342

    #
    # Is this profile the highest numbered version.
    # 
    function IsHead() {
	$profileid = $this->profileid();

	$query_result =
	    DBQueryWarn("select max(version) from apt_profile_versions ".
343
			"where profileid='$profileid' and deleted is null");
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
	if (!$query_result || !mysql_num_rows($query_result)) {
	    return -1;
	}
	$row = mysql_fetch_row($query_result);
	return ($this->version() == $row[0] ? 1 : 0);
    }
    #
    # Does this profile have more then one version (history).
    # 
    function HasHistory() {
	$profileid = $this->profileid();

	$query_result =
	    DBQueryWarn("select count(*) from apt_profile_versions ".
			"where profileid='$profileid'");
	if (!$query_result || !mysql_num_rows($query_result)) {
	    return -1;
	}
	$row = mysql_fetch_row($query_result);
	return ($row[0] > 1 ? 1 : 0);
    }
    #
    # A profile can be published if it is a > version then the most
    # recent published profile. 
    # 
    function CanPublish() {
	$profileid = $this->profileid();

	# Already published. Might support unpublish at some point.
	if ($this->published())
	    return 0;

	$query_result =
	    DBQueryWarn("select version from apt_profile_versions ".
			"where profileid='$profileid' and ".
			"      published is not null ".
			"order by version desc limit 1");
	if (!$query_result || !mysql_num_rows($query_result)) {
	    return -1;
	}
	$row = mysql_fetch_row($query_result);
	$vers = $row[0];
	
	return ($this->version() > $row[0] ? 1 : 0);
    }
    #
    # A profile can be modified if it is a >= version then the most
    # recent published profile. 
    # 
    function CanModify() {
	$profileid = $this->profileid();

	$query_result =
	    DBQueryWarn("select version from apt_profile_versions ".
			"where profileid='$profileid' and ".
			"      published is not null ".
			"order by version desc limit 1");
	if (!$query_result || !mysql_num_rows($query_result)) {
	    return -1;
	}
	$row = mysql_fetch_row($query_result);
	$vers = $row[0];
	
	return ($this->version() >= $row[0] ? 1 : 0);
    }
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
    #
    # Has a profile been instantiated?
    #
    function HasActivity() {
	$profileid = $this->profileid();

	$query_result =
	    DBQueryWarn("select count(h.uuid) from apt_instance_history as h ".
			"where h.profile_id='$profileid'");

	if (!$query_result) {
	    return 0;
	}
	if (mysql_num_rows($query_result)) {
	    $row = mysql_fetch_row($query_result);
	    if ($row[0] > 0) {
		return 1;
	    }
	}
	$query_result =
	    DBQueryWarn("select count(uuid) from apt_instances ".
			"where profile_id='$profileid'");

	if (!$query_result) {
	    return 0;
	}
	if (mysql_num_rows($query_result)) {
	    $row = mysql_fetch_row($query_result);
	    if ($row[0] > 0) {
		return 1;
	    }
	}
	return 0;
    }
443 444 445 446 447 448 449 450

    #
    # Permission check; does user have permission to instantiate the
    # profile. At the moment, view/instantiate are the same.
    #
    function CanInstantiate($user) {
	$profileid = $this->profileid();

451 452 453
        if (ISADMIN()) {
            return 1;
        }
454 455
	if ($this->shared() || $this->ispublic() ||
	    $this->creator_idx() == $user->uid_idx()) {
456 457 458
	    return 1;
	}
	# Otherwise a project membership test.
459
	$project = Project::Lookup($this->pid_idx());
460 461 462 463 464 465 466 467 468 469 470 471
	if (!$project) {
	    return 0;
	}
	$isapproved = 0;
	if ($project->IsMember($user, $isapproved) && $isapproved) {
	    return 1;
	}
	return 0;
    }
    function CanView($user) {
	return $this->CanInstantiate($user);
    }
472 473 474
    function CanClone($user) {
	return $this->CanInstantiate($user);
    }
475
    function CanEdit($user) {
476 477
        if ($this->project_write() ||
            $this->creator_idx() == $user->uid_idx() || ISADMIN()) {
478 479 480 481 482 483 484
            return 1;
        }
	$project = Project::Lookup($this->pid_idx());
	if (!$project) {
	    return 0;
	}
        if ($user->uid_idx() == $project->GetLeader()->uid_idx()) {
485
	    return 1;
486
        }
487 488
        return 0;
    }
489
    function CanDelete($user) {
490 491 492
        if ($this->nodelete()) {
            return 0;
        }
493 494 495 496 497 498
	# Want to know if the project is APT or Cloud/Emulab. APT projects
        # may not delete profiles (yet).
	$project = Project::Lookup($this->pid_idx());
	if (!$project) {
	    return 0;
	}
499
        if ($project->isAPT()) {
500 501
            return 0;
        }
502 503
        if ($this->creator_idx() == $user->uid_idx() || ISADMIN() ||
            $user->uid_idx() == $project->GetLeader()->uid_idx()) {
504
	    return 1;
505 506 507
        }
        return 0;
    }
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
    function isLeader($user) {
	$project = Project::Lookup($this->pid_idx());
	if (!$project) {
	    return 0;
	}
        if ($user->uid_idx() == $project->GetLeader()->uid_idx()) {
	    return 1;
        }
        return 0;
    }
    function isCreator($user) {
        if ($user->uid_idx() == $this->creator_idx()) {
	    return 1;
        }
        return 0;
    }
524

525 526 527 528 529 530 531 532 533 534 535 536 537
    function UsageInfo($user) {
        $profile_id  = $this->profileid();
        $userclause  = "";

        if ($user) {
            $creator_idx = $user->idx();
            $userclause  = "and creator_idx='$creator_idx' ";
        }

        #
        # This is last used.
        #
        $query_result =
538
            DBQueryFatal("select max(UNIX_TIMESTAMP(started)) as started ".
539 540 541 542
                         "  from apt_instances ".
                         "where profile_id='$profile_id' ".
                         $userclause);
        $row = mysql_fetch_row($query_result);
543
        if (!$row["started"]) {
544
            $query_result =
545
                DBQueryFatal("select max(UNIX_TIMESTAMP(started)) as started ".
546 547 548 549 550
                             "  from apt_instance_history ".
                             "where profile_id='$profile_id' ".
                             $userclause);
            $row = mysql_fetch_row($query_result);
        }
551
        if (!$row["started"]) {
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
            return array(0, 0);
        }
        $lastused = $row[0];

        #
        # Now we want number of times used.
        #
        $count = 0;
        $query_result =
            DBQueryFatal("select ".
                         "(select count(profile_id) ".
                         "   from apt_instances ".
                         " where profile_id='$profile_id' ".
                           $userclause . ") as count1, ".
                         "(select count(profile_id) ".
                         "   from apt_instance_history ".
                         " where profile_id='$profile_id' ".
                           $userclause . ") as count2");
        if (mysql_num_rows($query_result)) {
            $row   = mysql_fetch_row($query_result);
            $count = ($row[0] ? $row[0] : 0) + ($row[1] ? $row[1] : 0);
        }
        return array($lastused, $count);
    }

    function isFavorite($user) {
        if (!$user) {
            return 0;
        }
        $profile_id  = $this->profileid();
        $user_idx    = $user->idx();

        $query_result =
            DBQueryFatal("select * from apt_profile_favorites ".
                         "where uid_idx='$user_idx' and ".
                         "      profileid='$profile_id'");

        return mysql_num_rows($query_result);
    }

    function MarkFavorite($user) {
        $profile_id  = $this->profileid();
        $user_uid    = $user->uid();
        $user_idx    = $user->idx();

        if (!DBQueryWarn("replace into apt_profile_favorites set ".
                         "  uid='$user_uid',uid_idx='$user_idx', ".
599
                         "  profileid='$profile_id',marked=now()")) {
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
            return -1;
        }
        return 0;
    }

    function ClearFavorite($user) {
        $profile_id  = $this->profileid();
        $user_uid    = $user->uid();
        $user_idx    = $user->idx();

        if (!DBQueryWarn("delete from apt_profile_favorites ".
                         "where uid_idx='$user_idx' and ".
                         "      profileid='$profile_id'")) {
            return -1;
        }
        return 0;
    }

618
    function BestAggregate($rspec = null) {
619
        return null;
620
    }
621

622 623 624 625
    function GenerateFormFragment($json_data = null) {
        if (is_null($json_data)) {
            $json_data = $this->paramdefs();
        }
626 627 628 629 630
	if (!$json_data || $json_data == "") {
	    return "";
	}
	$fields    = json_decode($json_data);
	$defaults  = array();
631 632 633
	$formBasic    = "";
	$formAdvanced = "";
	$formGroups   = "";
634 635

	while (list ($name, $val) = each ($fields)) {
636
	    $form    = "";
637 638 639 640
	    $type    = $val->type;
	    $prompt  = $val->description;
	    $defval  = $val->defaultValue;
	    $options = $val->legalValues;
641 642 643 644 645 646 647
	    $longhelp  = $val->longDescription;
	    $advanced  = $val->advanced;
	    $groupId   = $val->groupId;
	    $groupName = $val->groupName;
	    $hasGroup = false;
	    $data_help_string = "";
	    $advanced_attr = "";
648 649 650 651 652

	    $defaults[$name] = $defval;
	    if (!isset($prompt) || !$prompt) {
		$prompt = $name;
	    }
653 654 655 656 657 658 659 660 661 662 663 664 665 666
	    if (!isset($advanced)) {
		$advanced = false;
	    }
            # Let advanced-tagged params dominate groupId; we don't generate groupId yet anyway.
	    if ($advanced) {
		$advanced_attr = " pp-param-group='advanced' pp-param-group-name='Advanced Parameters'";
	    }
	    else if (isset($groupId) && $groupId && isset($groupName) && $groupName) {
		$advanced_attr = " pp-param-group='$groupId' pp-param-group-name='$groupName'";
		$hasGroup = true;
	    }
	    if (isset($longhelp) && $longhelp) {
		$data_help_string = "data-help='$longhelp'";
	    }
667 668 669 670 671

	    if ($type == "boolean") {
		$form .=
		    "<input name='$name' ".
		    "      <%- formfields.${name} %> ".
672 673
		    "      style='margin: 0px; height: 34px;' ".
		    "      class='format-me' ".
674 675
		    "      data-key='$name' ".
		    "      data-label='$prompt' ".
676
		    "      $data_help_string $advanced_attr".
677
		    "      value='checked' ".
678
		    "      type='checkbox'>";
679 680 681 682 683 684 685 686 687 688 689 690 691
		if ($defval) {
		    $defaults[$name] = "checked";
		}
		else {
		    $defaults[$name] = "";
		}
	    }
	    elseif ($options) {
		$form .=
		    "<select name='$name' ".
		    "       class='form-control format-me' ".
		    "       data-key='$name' ".
		    "       data-label='$prompt' ".
692
		    "       $data_help_string $advanced_attr".
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
		    "       placeholder='Please Select'> ";
		foreach ($options as $option) {
		    if (gettype($option) == "array") {
			$oval = $option[0];
			$okey = $option[1];
		    }
		    else {
			$okey = $oval = $option;
		    }
		    $form .= "<option";
		    $form .= 
			"<% if (_.has(formfields, '$name') && ".
			"       formfields.${name} == '$oval') { %> ".
			"   selected ".
			"<% } %> ".
			"value='$oval'>$okey</option>";
		}
		$form .= "</select>";
	    }
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
	    elseif ($type == "image") {
	        $form .=
		    "<div class='format-me' ".
		    "data-key='$name' ".
		    "data-label='$prompt' ".
		    "data-type='$type' ".
		    "$data_help_string $advanced_attr >".

		    "<div class='input-group'>".

		    "<input id='image-display' ".
		    "type='text' readonly ".
		    "class='form-control' ".
		    "value='<% var label = formfields.${name}; var sp = label.split('+'); var image_display; if (sp.length >= 4){ if (sp[3].substr(0, 12) == 'emulab-ops//') { image_display = sp[3].substr(12) } else { image_display = sp[3] } } else { image_display = formfields.${name} } %><%- image_display %>' >".


		    "<span class='input-group-btn'><button class='btn btn-success' id='image-select' ".
		    "style='height: 34px' ".
		    "type='button' ".
		    "$data_help_string $advanced_attr ".
		    "><span class='glyphicon glyphicon-pencil'></span></button></span> ".

		    "</div>".
		    
		    "<input id='image-value' ".
		    "name='$name' type='hidden' ".
		    "value='<%- formfields.${name} %>' >".

		    "</div>";
	    }
742 743 744 745 746 747 748
	    else {
		$form .=
		    "<input name='$name' ".
		    "value='<%- formfields.${name} %>' ".
		    "class='form-control format-me' ".
		    "data-key='$name' ".
		    "data-label='$prompt' ".
749
		    "$data_help_string $advanced_attr".
750 751
		    "type='text'>";
	    }
752 753 754 755 756 757 758 759 760 761

	    if ($advanced) {
		$formAdvanced .= $form;
	    }
	    else if ($hasGroup) {
		$formGroups .= $form;
	    }
	    else {
		$formBasic .= $form;
	    }
762
	}
763 764 765 766

	$finalForm = $formBasic . $formAdvanced . $formGroups;

	return array($finalForm, $defaults);
767
    }
768 769
}
?>