project_defs.php 29.8 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2006-2018 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
# A cache of groups to avoid lookups. Indexed by pid_idx;
#
$project_cache = array();

28 29
class Project
{
30 31
    var	$project;
    var $group;
32 33
    var $_grouplist;		# All subgroups
    var $tempdata;		# For temporary data values ...
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48
    #
    # Constructor by lookup on unique index.
    #
    function Project($pid_idx) {
	$safe_pid_idx = addslashes($pid_idx);

	$query_result =
	    DBQueryWarn("select * from projects ".
			"where pid_idx='$safe_pid_idx'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->project = NULL;
	    return;
	}
49 50 51
	$this->project   = mysql_fetch_array($query_result);
	$this->group     = null;
	$this->_grouplist = null;
52 53 54 55 56 57 58 59 60
    }

    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->project);
    }

    # Lookup by pid_idx.
    function Lookup($pid_idx) {
61 62 63 64 65 66
	global $project_cache;

        # Look in cache first
	if (array_key_exists("$pid_idx", $project_cache))
	    return $project_cache["$pid_idx"];
	
67 68
	$foo = new Project($pid_idx);

69 70 71 72
	if (! $foo->IsValid()) {
	    # Try lookup by plain uid.
	    $foo = Project::LookupByPid($pid_idx);
	    
73
	    if (!$foo || !$foo->IsValid())
74 75
		return null;

76
	    # Return here, already in the cache.
77
	    return $foo;
78
	}
79 80
	# Insert into cache.
	$project_cache["$pid_idx"] = $foo;
81
	return $foo;
82 83 84 85 86
    }

    # Backwards compatable lookup by pid. Will eventually flush this.
    function LookupByPid($pid) {
	$safe_pid = addslashes($pid);
87
	global $project_cache;
88

89 90 91 92
        # Look in cache first
	if (array_key_exists($pid, $project_cache))
	    return $project_cache[$pid];
	
93 94 95 96 97 98 99 100 101 102 103
	$query_result =
	    DBQueryWarn("select pid_idx from projects where pid='$safe_pid'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    return null;
	}
	$row = mysql_fetch_array($query_result);
	$idx = $row['pid_idx'];

	$foo = new Project($idx); 

104 105 106
	if ($foo->IsValid()) {
            # Insert into cache.
            $project_cache[$pid] = $foo;
107
	    return $foo;
108
        }
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	
	return null;
    }
    
    #
    # Refresh an instance by reloading from the DB.
    #
    function Refresh() {
	if (! $this->IsValid())
	    return -1;

	$pid_idx = $this->pid_idx();

	$query_result =
	    DBQueryWarn("select * from projects where pid_idx='$pid_idx'");
    
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->project = NULL;
	    return -1;
	}
129 130 131 132 133 134 135
	$this->project   = mysql_fetch_array($query_result);

	if ($this->group) {
	    $foo = $this->group;
	    $foo->Refresh();
	}
	$this->_grouplist = null;
136 137 138 139 140 141 142 143 144 145 146
	return 0;
    }

    # accessors
    function field($name) {
	return (is_null($this->project) ? -1 : $this->project[$name]);
    }
    function pid_idx()	     { return $this->field("pid_idx"); }
    function pid()	     { return $this->field("pid"); }
    function created()       { return $this->field("created"); }
    function expires()       { return $this->field("expires"); }
147
    function nagged()        { return $this->field("nagged"); }
148 149 150 151 152
    function name()          { return $this->field("name"); }
    function URL()           { return $this->field("URL"); }
    function funders()       { return $this->field("funders"); }
    function addr()          { return $this->field("addr"); }
    function head_uid()      { return $this->field("head_uid"); }
153
    function head_idx()      { return $this->field("head_idx"); }
154 155 156 157 158 159 160 161
    function num_members()   { return $this->field("num_members"); }
    function num_pcs()       { return $this->field("num_pcs"); }
    function num_sharks()    { return $this->field("num_sharks"); }
    function num_pcplab()    { return $this->field("num_pcplab"); }
    function num_ron()       { return $this->field("num_ron"); }
    function why()           { return $this->field("why"); }
    function control_node()  { return $this->field("control_node"); }
    function approved()      { return $this->field("approved"); }
162
    function disabled()      { return $this->field("disabled"); }
163 164
    function inactive()      { return $this->field("inactive"); }
    function date_inactive() { return $this->field("date_inactive"); }
165
    function ispublic()      { return $this->field("public"); }
166 167 168 169 170 171 172 173
    function public_whynot() { return $this->field("public_whynot"); }
    function expt_count()    { return $this->field("expt_count"); }
    function expt_last()     { return $this->field("expt_last"); }
    function pcremote_ok()   { return $this->field("pcremote_ok"); }
    function default_user_interface()
	                     { return $this->field("default_user_interface"); }
    function linked_to_us()  { return $this->field("linked_to_us"); }
    function cvsrepo_public(){ return $this->field("cvsrepo_public"); }
174
    function allow_workbench(){ return $this->field("allow_workbench"); }
175
    function nonlocal_id()   { return $this->field("nonlocal_id"); }
176 177 178 179 180 181
    function portal()	     { return $this->field("portal"); }
    function isAPT()	     { return ($this->portal() &&
                                       $this->portal() == "aptlab" ? 1 : 0); }
    function isCloud()	     { return ($this->portal() &&
                                       $this->portal() == "cloudlab" ? 1 : 0); }
    function isPNet()	     { return ($this->portal() &&
182 183 184
                                       $this->portal() == "phantomnet" ? 1 :0);}
    function isPowder()	     { return ($this->portal() &&
                                       $this->portal() == "powder" ? 1 : 0);}
185 186 187 188 189
    function isEmulab()	     { return ($this->portal() &&
                                       $this->portal() == "emulab" ? 1 : 0); }
    # Not via the Portal interface.
    function isClassic()     { return ($this->portal() ? 0 : 1); }
    
Leigh Stoller's avatar
Leigh Stoller committed
190 191 192
    function IsNonLocal() {
	return ($this->field("nonlocal_id") ? 1 : 0);
    }
193

194
    function wwwBase() {
195
	global $TBBASE;
196 197
	
	return ($this->isAPT() ? "https://www.aptlab.net" :
198 199
		($this->isCloud() ? "https://www.cloudlab.us" : 
		 ($this->isPNet() ? "https://www.phantomnet.org" :
200
                  ($this->isPowder() ? "https://www.powderwireless.net" :
201
                   $TBBASE))));
202 203
    }
    function ApprovalEmailAddress() {
204
	global $TBMAILADDR_APPROVAL;
205 206

	return ($this->isAPT() ? "aptlab-approval@aptlab.net" :
207 208
		($this->isCloud() ? "cloudlab-approval@cloudlab.us" :
		 ($this->isPNet() ? "phantomnet-approval@phantomnet.org" :
209 210
                  ($this->isPowder() ? "powder-approval@powderwireless.net" :
                   $TBMAILADDR_APPROVAL))));
211 212 213 214 215
    }
    function EmailTag() {
	global $THISHOMEBASE;
	
	return ($this->isAPT() ? "aptlab.net" :
216 217
		($this->isCloud() ? "cloudlab.us" : 
		 ($this->isPNet() ? "phantomnet.org" :
218 219
                  ($this->isPowder() ? "powderwireless.net" :
                   $THISHOMEBASE))));
220 221
    }

222
    function unix_gid() {
223
	$group = $this->DefaultGroup();
224 225 226 227
	
	return $group->unix_gid();
    }
    function unix_name() {
228
	$group = $this->DefaultGroup();
229 230 231

	return $group->unix_name();
    }
232 233 234 235 236 237 238
    # Temporary data storage ... useful.
    function SetTempData($value) {
	$this->tempdata = $value;
    }
    function GetTempData() {
	return $this->tempdata;
    }
239

240 241 242 243 244 245 246 247
    #
    # At some point we will stop passing pid and start using pid_idx.
    # Use this function to avoid having to change a bunch of code twice.
    #
    function URLParam() {
	return $this->pid();
    }

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
    #
    # Class function to create new project and return object.
    #
    function NewProject($pid, $leader, $args) {
	global $TBBASE, $TBMAIL_APPROVAL, $TBMAIL_AUDIT, $TBMAIL_WWW;
	
	#
	# The array of inserts is assumed to be safe already. Generate
	# a list of actual insert clauses to be joined below.
	#
	$insert_data = array();
	
	foreach ($args as $name => $value) {
	    $insert_data[] = "$name='$value'";
	}

	# First create the underlying default group for the project.
	if (! ($newgroup = Group::NewGroup(null, $pid, $leader,
					   'Default Group', $pid))) {
	    return null;
	}

	# Every project gets a new unique index, which comes from the group.
	$pid_idx = $newgroup->gid_idx();

	# Now tack on other stuff we need.
	$insert_data[] = "pid='$pid'";
	$insert_data[] = "pid_idx='$pid_idx'";
	$insert_data[] = "head_uid='" . $leader->uid() . "'";
277
	$insert_data[] = "head_idx='" . $leader->uid_idx() . "'";
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
	$insert_data[] = "created=now()";

	# Insert into DB. Should probably lock the table ...
	if (!DBQuerywarn("insert into projects set ".
			 implode(",", $insert_data))) {
	    $newgroup->Delete();
	    return null;
	}

	if (! DBQueryWarn("insert into project_stats (pid, pid_idx) ".
			  "values ('$pid', $pid_idx)")) {
	    $newgroup->Delete();
	    DBQueryFatal("delete from projects where pid_idx='$pid_idx'");
	    return null;
	}
	$newproject = Project::Lookup($pid_idx);
	if (! $newproject)
	    return null;

	#
	# The creator of a group is not automatically added to the group,
	# but we do want that for a new project. 
	#
	if ($newgroup->AddNewMember($leader) < 0) {
	    $newgroup->Delete();
	    DBQueryWarn("delete from project_stats where pid_idx=$pid_idx");
	    DBQueryWarn("delete from projects where pid_idx=$pid_idx");
	    return null;
	}

	return $newproject;
    }

311
    function NewNewProject($args, &$error) {
312 313 314 315 316 317 318 319
	global $suexec_output, $suexec_output_array;

        #
        # Generate a temporary file and write in the XML goo.
        #
	$xmlname = tempnam("/tmp", "newproj");
	if (! $xmlname) {
	    TBERROR("Could not create temporary filename", 0);
320
	    $error = "Transient error(1); please try again later.";
321 322 323 324
	    return null;
	}
	if (! ($fp = fopen($xmlname, "w"))) {
	    TBERROR("Could not open temp file $xmlname", 0);
325
	    $error = "Transient error(2); please try again later.";
326 327 328 329 330 331 332 333 334 335 336 337 338 339
	    return null;
	}

	fwrite($fp, "<project>\n");
	foreach ($args as $name => $value) {
	    fwrite($fp, "<attribute name=\"$name\">");
	    fwrite($fp, "  <value>" . htmlspecialchars($value) . "</value>");
	    fwrite($fp, "</attribute>\n");
	}
	fwrite($fp, "</project>\n");
	fclose($fp);
	chmod($xmlname, 0666);

	$retval = SUEXEC("nobody", "nobody", "webnewproj $xmlname",
340
			 SUEXEC_ACTION_IGNORE | SUEXEC_ACTION_MAIL_TBLOGS);
341 342 343

	if ($retval) {
	    if ($retval < 0) {
344
		$error = "Transient error(3, $retval); please try again later.";
345 346 347 348 349 350 351 352 353 354 355 356 357
		SUEXECERROR(SUEXEC_ACTION_CONTINUE);
	    }
	    else {
		$error = $suexec_output;
	    }
	    return null;
	}

        #
        # Parse the last line of output. Ick.
        #
	unset($matches);
	
358
	if (!preg_match("/^Project\s+([-\w]+)\/(\d+)\s+/",
359 360
			$suexec_output_array[count($suexec_output_array)-1],
			$matches)) {
361
	    $error = "Transient error(4, $retval); please try again later.";
362 363 364 365 366 367
	    SUEXECERROR(SUEXEC_ACTION_CONTINUE);
	    return null;
	}
	$pid_idx = $matches[2];
	$newproj = Project::Lookup($pid_idx);
	if (! $newproj) {
368
	    $error = "Transient error(5); please try again later.";
369 370 371 372 373 374 375
	    TBERROR("Could not lookup new project $pid_idx", 0);
	    return null;
	}
	# Unlink this here, so that the file is left behind in case of error.
	# We can then create the project by hand from the xmlfile, if desired.
	unlink($xmlname);
	return $newproj;
376
    }
377

378
    #
379
    # Class function to return a list of pending (unapproved) projects.
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
    function PendingProjectList() {
	$result     = array();

	$query_result =
	    DBQueryFatal("select pid_idx, ".
			 " DATE_FORMAT(created, '%m/%d/%y') as day_created ".
			 " from projects ".
			 "where approved='0' order by created desc");
			     
	while ($row = mysql_fetch_array($query_result)) {
	    $pid_idx = $row["pid_idx"];
	    $created = $row["day_created"];

	    if (! ($project = Project::Lookup($pid_idx))) {
		TBERROR("Project::PendingProjectList: ".
			"Could not load project $pid_idx!", 1);
	    }
	    $project->SetTempData($created);
	    
	    $result[] = $project;
	}
	return $result;
    }
    
405
    function AccessCheck($user, $access_type) {
406 407 408 409 410
	$group = $this->DefaultGroup();
	
	return $group->AccessCheck($user, $access_type);
    }

411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
    # Can the user delete the target user.
    function CanDeleteUser($user, $target_user) {
        global $TB_PROJECT_DELUSER;
        
        # Not allowed to delete yourself from a group.
        if ($user->SameUser($target_user)) {
            return 0;
        }
        if (! $this->AccessCheck($user, $TB_PROJECT_DELUSER) ||
            $this->UserTrust($target_user) >= $this->UserTrust($user)) {
            return 0;
        }
        return 1;
    }

426 427 428 429 430 431
    # Return the user trust within the project, which is really for the
    # default group.
    function UserTrust($user) {
	$group = $this->DefaultGroup();
	
	return $group->UserTrust($user);
432 433
    }

434 435 436 437 438 439 440
    # Return membership info in the default group.
    function MemberShipInfo($user) {
	$group = $this->DefaultGroup();

        return $group->MemberShipInfo($user);
    }

441 442 443
    #
    # Load the default group for a project lazily.
    #
444 445 446 447 448
    function LoadDefaultGroup() {
	if ($this->group) {
	    return $this->group;
	}
	
449 450 451 452
	# Note: pid_idx=gid_idx for the default group
	$gid_idx = $this->pid_idx();

	if (! ($group = Group::Lookup($gid_idx))) {
453 454
	    TBERROR("Project::LoadDefaultGroup: ".
		    "Could not load group $gid_idx!", 1);
455 456
	}
	$this->group = $group;
457
	return $group;
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
    function DefaultGroup() { return $this->LoadDefaultGroup(); }
    function Group()        { return $this->DefaultGroup(); }

    #
    # Lookup a project subgroup by its name.
    #
    function LookupSubgroupByName($name) {
	$pid = $this->pid();

	return Group::LookupByPidGid($pid, $name);
    }

    #
    # Load all subgroups for this project.
    #
    function LoadSubGroups() {
	if ($this->_grouplist)
	    return 0;
	
	$pid_idx = $this->pid_idx();
	$result  = array();

	$query_result =
	    DBQueryFatal("select gid_idx from groups ".
			 "where pid_idx='$pid_idx'");

	while ($row = mysql_fetch_array($query_result)) {
	    $gid_idx = $row["gid_idx"];

	    if (! ($group = Group::Lookup($gid_idx))) {
		TBERROR("Project::LoadSubGroups: ".
			"Could not load group $gid_idx!", 1);
	    }
	    $result[] = $group;
	}
	$this->_grouplist = $result;
	return 0;
    }
    function SubGroups() {
	$this->LoadSubGroups();
	return $this->_grouplist;
500
    }
501

502 503 504 505
    #
    # Return user object for leader.
    #
    function GetLeader() {
506
	$head_idx = $this->head_idx();
507

508 509
	if (! ($leader = User::Lookup($head_idx))) {
	    TBERROR("Could not find user object for $head_idx", 1);
510 511 512
	}
	return $leader;
    }
513 514 515 516 517 518 519 520
    # Boolean test.
    function IsLeader($user) {
        $leader = $this->GetLeader();
        if ($user->SameUser($leader)) {
            return 1;
        }
        return 0;
    }
521

522 523 524
    #
    # Add *new* member to project group; starts out with trust=none.
    #
525
    function AddNewMember($user, $trust = TBDB_TRUSTSTRING_NONE) {
526
	$group = $this->DefaultGroup();
527

528
	return $group->AddNewMember($user, $trust);
529 530
    }

531 532 533 534 535 536 537 538 539
    #
    # Delete.
    #  
    function DeleteMember($user) {
	$group = $this->DefaultGroup();

	return $group->DeleteMember($user);
    }

540 541 542
    #
    # Check if user is a member of this project (well, group)
    #
543
    function IsMember($user, &$approved) {
544
	$group = $this->DefaultGroup();
545

546
	return $group->IsMember($user, $approved);
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
    #
    # Lookup an experiment within a project.
    #
    function LookupExperiment($eid) {
	return Experiment::LookupByPidEid($this->pid(), $eid);
    }

    #
    # How many PCs is project using. 
    #
    function PCsInUse() {
	$pid = $this->pid();
	
	$query_result =
	    DBQueryFatal("select count(r.node_id) from reserved as r ".
			 "left join nodes as n on n.node_id=r.node_id ".
			 "left join node_types as nt on nt.type=n.type ".
			 "where nt.class='pc' and r.pid='$pid'");
    
	if (mysql_num_rows($query_result) == 0) {
	    return 0;
	}
	$row = mysql_fetch_row($query_result);
	return $row[0];
    }
    
575
    #
576
    # Member list for a group.
577
    #
578 579 580
    function MemberList() {
	$pid_idx = $this->pid_idx();
	$result  = array();
581

582 583 584 585 586 587 588
	$query_result =
	    DBQueryFatal("select uid_idx from group_membership ".
			 "where pid_idx='$pid_idx' and gid_idx=pid_idx");

	while ($row = mysql_fetch_array($query_result)) {
	    $uid_idx = $row["uid_idx"];

Leigh Stoller's avatar
Leigh Stoller committed
589
	    if (! ($user = User::Lookup($uid_idx))) {
590 591 592
		TBERROR("Project::MemberList: ".
			"Could not load user $uid_idx!", 1);
	    }
Leigh Stoller's avatar
Leigh Stoller committed
593
	    $result[] = $user;
594
	}
595 596 597
	return $result;
    }

598 599 600 601 602
    #
    # Return an array of any users in the project who are admins.
    #
    function GetAdmins() {
	$pid_idx = $this->pid_idx();
603
	$result  = array();
604 605
	$query_result =
	    DBQueryFatal("select distinct u.uid_idx from users as u".
606 607
			 " left join group_membership as gm on ".
			 "      u.uid_idx=gm.uid_idx".
608 609 610 611 612 613 614 615 616 617 618 619 620 621
			 " where gm.pid_idx='$pid_idx' and u.admin=1");

	while ($row = mysql_fetch_array($query_result)) {
	    $uid_idx = $row["uid_idx"];

	    if (! ($user = User::Lookup($uid_idx))) {
		TBERROR("Project::GetAdmins: ".
			"Could not load user $uid_idx!", 1);
	    }
	    $result[] = $user;
	}
	return $result;
    }

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
    #
    # List of subgroups for a project member (not including default group).
    #
    function GroupList($user) {
	$pid_idx = $this->pid_idx();
	$uid_idx = $user->uid_idx();
	$result  = array();

	$query_result =
	    DBQueryFatal("select gid_idx from group_membership ".
			 "where pid_idx='$pid_idx' and pid_idx!=gid_idx and ".
			 "      uid_idx='$uid_idx'");

	while ($row = mysql_fetch_array($query_result)) {
	    $gid_idx = $row["gid_idx"];

	    if (! ($group = Group::Lookup($gid_idx))) {
		TBERROR("Project::GroupList: ".
			"Could not load group $gid_idx!", 1);
	    }
	    $result[] = $group;
	}
	return $result;
    }

647 648 649 650 651 652 653 654 655
    #
    # List of experiments for a project, or just the count.
    #
    function ExperimentList($listify = 1) {
	$group = $this->DefaultGroup();

	return $group->ExperimentList($listify);
    }

656 657 658 659 660
    #
    # Change the leader for a project. Done *only* before project is
    # approved.
    #
    function ChangeLeader($leader) {
661
	$group   = $this->DefaultGroup();
662 663 664
	$idx     = $this->pid_idx();
	$uid     = $leader->uid();
	$uid_idx = $leader->uid_idx();
665

666 667
	DBQueryFatal("update projects set ".
		     "  head_uid='$uid',head_idx='$uid_idx' ".
668 669 670
		     "where pid_idx='$idx'");

	$this->project["head_uid"] = $uid;
671
	$this->project["head_idx"] = $uid_idx;
672
	return $group->ChangeLeader($leader);
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
    #
    # Change various fields.
    #
    function SetApproved($approved) {
	$idx   = $this->pid_idx();

	if ($approved)
	    $approved = 1;
	else
	    $approved = 0;
	
	DBQueryFatal("update projects set approved='$approved' ".
		     "where pid_idx='$idx'");

	$this->project["approved"] = $approved;
	return 0;
    }
    function SetRemoteOK($ok) {
	$idx    = $this->pid_idx();
	$safeok = addslashes($ok);

	DBQueryFatal("update projects set pcremote_ok='$safeok' ".
		     "where pid_idx='$idx'");

	$this->project["pcremote_ok"] = $ok;
	return 0;
701 702
    }

703 704 705 706 707 708 709 710 711
    function SetCVSRepoPublic($public) {
	$idx    = $this->pid_idx();
	$public = ($public ? 1 : 0);

	DBQueryFatal("update projects set cvsrepo_public='$public' ".
		     "where pid_idx='$idx'");

	return 0;
    }
712 713
    function SetAllowWorkbench($onoff) {
	$idx    = $this->pid_idx();
714
	$onoff  = ($onoff ? 1 : 0);
715 716 717 718 719 720

	DBQueryFatal("update projects set allow_workbench='$onoff' ".
		     "where pid_idx='$idx'");

	return 0;
    }
721 722 723 724 725 726 727 728 729 730
    function SetDisabled($onoff) {
	$idx    = $this->pid_idx();
	$onoff  = ($onoff ? 1 : 0);

	DBQueryFatal("update projects set disabled='$onoff' ".
		     "where pid_idx='$idx'");

	$this->project["disabled"] = $onoff;
	return 0;
    }
731

732 733 734 735
    function Show() {
	global $WIKISUPPORT, $CVSSUPPORT, $TBPROJ_DIR, $TBCVSREPO_DIR;
	global $MAILMANSUPPORT, $OPSCVSURL, $USERNODE;

736
	$group = $this->DefaultGroup();
737 738 739 740 741 742

	$pid                    = $this->pid();
	$proj_idx		= $this->pid_idx();
	$proj_created		= $this->created();
	$proj_name		= $this->name();
	$proj_URL		= $this->URL();
743
	$proj_public		= YesNo($this->ispublic());
744 745 746 747 748 749 750 751 752 753 754 755
	$proj_funders		= $this->funders();
	$proj_head_idx		= $this->head_idx();
	$proj_members		= $this->num_members();
	$proj_pcs		= $this->num_pcs();
        # These are now booleans, not actual counts.
	$proj_linked		= YesNo($this->linked_to_us());
	$proj_why		= nl2br($this->why());
	$approved		= YesNo($this->approved());
	$expt_count		= $this->expt_count();
	$expt_last		= $this->expt_last();
	$wikiname		= $group->wikiname();
	$cvsrepo_public		= $this->cvsrepo_public();
756
	$allow_workbench	= $this->allow_workbench();
757
	$portal                 = $this->portal();
758
        $nonlocal_id            = $this->nonlocal_id();
759

760
	# Before project approval, display ron/plab request status.
761
	if ($this->approved()) {
762 763 764 765 766 767 768 769 770 771
	    $proj_ronpcs   =
		YesNo(strpos($this->pcremote_ok(), "pcron") !== false);
	    $proj_plabpcs  = 
		YesNo(strpos($this->pcremote_ok(), "pcplabphys") !== false);
	}
	else {
	    $proj_ronpcs   = YesNo($this->num_ron());
	    $proj_plabpcs  = YesNo($this->num_pcplab());
	}

772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
	if (! ($head_user = User::Lookup($proj_head_idx))) {
	    TBERROR("Could not lookup object for user $proj_head_idx", 1);
	}
	$showuser_url  = CreateURL("showuser", $head_user);
	$showproj_url  = CreateURL("showproject", $this);
	$proj_head_uid = $head_user->uid();

	if (!$expt_last) {
	    $expt_last = "&nbsp;";
	}

	echo "<center>
              <h3>Project Profile</h3>
              </center>
              <table align=center cellpadding=2 border=1>\n";
    
        #
        # Generate the table.
        # 
	echo "<tr>
                  <td>Name: </td>
                  <td class=\"left\">
Leigh Stoller's avatar
Leigh Stoller committed
794
                      <a href='$showproj_url'>$pid ($proj_idx)</a></td>
795 796
              </tr>\n";
    
797 798 799 800 801 802 803
        if ($nonlocal_id) {
            echo "<tr>
                      <td>Nonlocal ID:</td>
                      <td>$nonlocal_id</td>
                      </tr>\n";
        }
    
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
	echo "<tr>
                  <td>Description: </td>
                  <td class=\"left\">$proj_name</td>
              </tr>\n";
    
	echo "<tr>
                  <td>Project Head: </td>
                  <td class=\"left\">
                      <a href='$showuser_url'>$proj_head_uid</a></td>
              </tr>\n";
    
	echo "<tr>
              <td>URL: </td>
                  <td class=\"left\">
                      <a href='$proj_URL'>$proj_URL</a></td>
              </tr>\n";

821
	if (isset($portal)) {
822
	    echo "<tr>
823 824
                  <td>Portal: </td>
                  <td class=\"left\">$portal</td>
825 826 827
              </tr>\n";
	}

828 829
	if ($WIKISUPPORT && isset($wikiname)) {
	    $wikiurl = "gotowiki.php3?redurl=$wikiname/WebHome";
830
	
831 832 833 834 835 836 837 838
	    echo "<tr>
                      <td>Project Wiki:</td>
                      <td class=\"left\">
                          <a href='$wikiurl'>$wikiname</a></td>
                  </tr>\n";
	}
	if ($CVSSUPPORT) {
	    $cvsdir = "$TBCVSREPO_DIR/$pid";
Robert Ricci's avatar
Robert Ricci committed
839
	    $cvsurl = "cvsweb/cvswebwrap.php3?pid=$pid";
840 841 842 843 844 845 846 847 848 849 850
	
	    echo "<tr>
                      <td>Project CVS Repository:</td>
                      <td class=\"left\">
                          $cvsdir <a href='$cvsurl'>(CVSweb)</a></td>
                  </tr>\n";

	    $YesNo = YesNo($cvsrepo_public);
	    $flip  = ($cvsrepo_public ? 0 : 1);
	    echo "<tr>
                      <td>CVS Repository Publically Readable?:</td>
851 852
                      <td><a href='toggle.php?pid=$pid&type=cvsrepo_public".
		          "&value=$flip'>$YesNo</a> (Click to toggle)</td>
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
                  </tr>\n";

	    if ($cvsrepo_public) {
		$puburl  = "$OPSCVSURL/?cvsroot=$pid";
		$pserver = ":pserver:anoncvs@$USERNODE:/cvsrepos/$pid";
		
		echo "<tr>
                          <td>Public CVSWeb Address:</td>
                          <td><a href=$puburl>" .
		                 htmlspecialchars($puburl) . "</a></td>
                      </tr>\n";

		echo "<tr>
                          <td>CVS pserver Address:</td>
                          <td>" . htmlspecialchars($pserver) . "</td>
                      </tr>\n";
	    }
	}

	if ($MAILMANSUPPORT) {
	    $mmurl   = "gotommlist.php3?pid=$pid";
874

875 876 877 878 879 880 881 882 883 884
	    echo "<tr>
                      <td>Project Mailing List:</td>
                      <td class=\"left\">
                          <a href='$mmurl'>${pid}-users</a> ";
	    if (ISADMIN()) {
		$mmurl .= "&wantadmin=1";
		echo "<a href='$mmurl'>(admin access)</a>";
	    }
	    echo "    </td>
                  </tr>\n";
885 886 887 888 889 890 891 892 893 894 895 896 897

	    if (ISADMIN()) {
		$mmurl   = "gotommlist.php3?listname=${pid}-admin&asadmin=1";

		echo "<tr>
                         <td>Project Admin Mailing List:</td>
                         <td class=\"left\">
                             <a href='$mmurl'>${pid}-admin</a> ";
		$mmurl   = "gotommlist.php3?listname=${pid}-admin&wantadmin=1";
		echo "<a href='$mmurl'>(admin access)</a>";
		echo "    </td>
                     </tr>\n";
	    }
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
	}

	echo "<tr>
                  <td>Publicly Visible: </td>
                  <td class=\"left\">$proj_public</td>
              </tr>\n";
    
	echo "<tr>
                  <td>Link to Us?: </td>
                  <td class=\"left\">$proj_linked</td>
              </tr>\n";
    
	echo "<tr>
                  <td>Funders: </td>
                  <td class=\"left\">$proj_funders</td>
              </tr>\n";

	echo "<tr>
                  <td>#Project Members: </td>
                  <td class=\"left\">$proj_members</td>
              </tr>\n";
    
	echo "<tr>
                  <td>#PCs: </td>
                  <td class=\"left\">$proj_pcs</td>
              </tr>\n";
    
	echo "<tr>
                  <td>Planetlab Access: </td>
                  <td class=\"left\">$proj_plabpcs</td>
              </tr>\n";
    
	echo "<tr>
                  <td>RON Access: </td>
                  <td class=\"left\">$proj_ronpcs</td>
              </tr>\n";
934 935 936

	# Fine-grained Datapository access: show node_ids over all sub-groups.
	# Should probably do likewise in individual sub-group pages.
937
	# "dp_projects" node_attributes are lists of group gid_idxs.
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
        $query_result =
	    DBQueryFatal("select distinct g.gid_idx, a.node_id ".
			 "  from groups as g, node_attributes as a ".
			 "where g.pid_idx='$proj_idx' ".
			 "  and a.attrkey='dp_projects' ".
                         "  and FIND_IN_SET(g.gid_idx, a.attrvalue) ".
			 "order by g.gid_idx, a.node_id");
	$proj_dp_nodes = "";
	while ($row = mysql_fetch_array($query_result)) {
	    $node_id = $row["node_id"];

	    if ($proj_dp_nodes) $proj_dp_nodes .= ", ";
	    $proj_dp_nodes .= $node_id;
	}
	if ($proj_dp_nodes) {
	    echo "<tr>
		      <td>Datapository Access: </td>
		      <td class=\"left\">$proj_dp_nodes</td>
		  </tr>\n";
	}

959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
	echo "<tr>
                  <td>Created: </td>
                  <td class=\"left\">$proj_created</td>
              </tr>\n";
    
	echo "<tr>
                  <td>Experiments Created:</td>
                  <td class=\"left\">$expt_count</td>
              </tr>\n";
    
	echo "<tr>
                  <td>Date of last experiment:</td>
                  <td class=\"left\">$expt_last</td>
              </tr>\n";
    
	echo "<tr>
                  <td>Approved?: </td>
                  <td class=\"left\">$approved</td>
	      </tr>\n";

979 980 981 982 983 984
	if (ISADMIN()) {
	    $YesNo = YesNo($allow_workbench);
	    $flip  = ($allow_workbench ? 0 : 1);
	    
	    echo "<tr>
                      <td>Allow Workbench:</td>
985 986
                      <td><a href='toggle.php?pid=$pid&type=workbench".
		          "&value=$flip'>$YesNo</a> (Click to toggle)</td>
987 988 989
                  </tr>\n";
	}

990 991 992 993 994 995 996 997 998 999 1000 1001
	echo "<tr>
                  <td colspan='2'>Why?:</td>
              </tr>\n";
    
	echo "<tr>
                  <td colspan='2' width=600>$proj_why</td>
              </tr>\n";
    
	echo "</table>\n";
    }

    function ShowGroupList() {
1002
	$groups    = $this->SubGroups();
1003

1004
	echo "<center><h3>Project Groups</h3></center>\n";
1005 1006
	echo "<table id='grouplist' align=center border=1>\n";
	echo "<thead class='sort'>";
1007 1008 1009 1010
	echo "<tr>
               <th>GID</th>
               <th>Description</th>
               <th>Leader</th>
1011
              </tr></thead>\n";
1012

1013 1014 1015 1016 1017
	foreach ($groups as $group) {
	    $gid         = $group->gid();
	    $desc        = $group->description();
	    $leader      = $group->leader();
	    $leader_user = $group->GetLeader();
1018

1019 1020
	    $showuser_url  = CreateURL("showuser", $leader_user);
	    $showgroup_url = CreateURL("showgroup", $group);
1021 1022

	    echo "<tr>
1023
                   <td><A href='$showgroup_url'>$gid</a></td>
1024 1025 1026 1027 1028
                   <td>$desc</td>
                   <td><A href='$showuser_url'>$leader</A></td>
                 </tr>\n";
	}
	echo "</table>\n";
1029 1030 1031 1032

        echo "<script type='text/javascript' language='javascript'>
	       sorttable.makeSortable(getObjbyName('grouplist'));
             </script>\n";
1033 1034
    }

1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
    function ShowStats() {
	$pid_idx  = $this->pid_idx();

	$query_result =
	    DBQueryFatal("select * from project_stats ".
			 "where pid_idx='$pid_idx'");

	if (! mysql_num_rows($query_result)) {
	    return;
	}
	$row = mysql_fetch_assoc($query_result);

        #
        # Not pretty printed yet.
        #
	echo "<table align=center border=1>\n";
    
	foreach($row as $key => $value) {
	    echo "<tr>
                      <td>$key:</td>
                      <td>$value</td>
                  </tr>\n";
	}
	echo "</table>\n";
    }
1060

1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
    #
    # Return license status for all licenses.
    #
    function LicenseStatus() {
	$pid_idx = $this->pid_idx();
        $result  = array();

        $query_result =
            DBQueryFatal("select l.*,pl.pid,pl.accepted from licenses as l ".
                         "left join project_licenses as pl on ".
                         "   pl.license_idx=l.license_idx and ".
                         "   pl.pid_idx='$pid_idx'");

	while ($row = mysql_fetch_array($query_result)) {
            $result[] = $row;
        }
        return $result;
    }
1079
}
1080
?>