images.php 9.96 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2000-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 23 24 25 26 27
# 
# {{{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/>.
# 
# }}}
#
chdir("..");
include("defs.php3");
chdir("apt");
include("quickvm_sup.php");
28 29
include_once("instance_defs.php");
include_once("aggregate_defs.php");
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
$page_title = "Image List";

#
# Verify page arguments.
#
$optargs = OptionalPageArguments("target_user", PAGEARG_USER,
                                 "all",         PAGEARG_BOOLEAN);

#
# Get current user.
#
RedirectSecure();
$this_user = CheckLoginOrRedirect();
# Ignore all flag if not an admin
if (!ISADMIN()) {
    $all = 0;
}
elseif (!isset($all)) {
    $all = 0;
}

if (!isset($target_user)) {
    $target_user = $this_user;
}
if (!$this_user->SameUser($target_user)) {
    if (!ISADMIN()) {
	SPITUSERERROR("You do not have permission to view ".
		      "target user's images");
	exit();
    }
    # Do not show admin access images if targeting a different user.
    $all = 0;
}
$target_idx = $target_user->uid_idx();
$projlist   = $target_user->ProjectAccessList($TB_PROJECT_CREATEEXPT);

66 67 68 69 70 71 72
$tmp = array();
$images = array();
$domain = $OURDOMAIN;
if ($TBMAINSITE) {
    $domain = "emulab.net";
}

73 74 75 76 77 78 79 80
SPITHEADER(1);

echo "<link rel='stylesheet'
            href='css/tablesorter-blue.css'>\n";

# Place to hang the toplevel template.
echo "<div id='main-body'></div>\n";

81 82 83
if ($ISCLOUD) {
    $aggregates = array();
    
84
    #
85
    # Look in the IMS database for images from all clusters.
86
    #
87
    $dblink = DBConnect("ims");
88
    
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    if (ISADMIN() && $all) {
        $joinclause  = "";
        $whereclause = "";
    }
    else {
        $joinclause  = "";
        $whereclause = "(iv.visibility='public')";
    }
    $query =
           "SELECT i.*, iv.* ".
           "FROM images as i ".
           "JOIN ".
           "( ".
           "  SELECT iv1.* FROM image_versions as iv1 ".
           "  JOIN ".
           "  ( ".
           "   SELECT urn,MAX(version) as maxVersion ".
           "   FROM image_versions ".
           "   GROUP BY urn ".
           "  ) iv2 ".
           "  ON iv1.urn = iv2.urn ".
           "  AND iv1.version = iv2.maxVersion ".
           ") iv ".
           "ON i.urn = iv.urn";
    $query_result = DBQueryFatal($query, $dblink);
114

115
    while ($row = mysql_fetch_array($query_result)) {
116
        $name    = $row["imagename"];
117 118
        $pid     = "n/a";
        $pid_idx = 0;
119 120
        $blob    = array();

121 122 123 124
        # Need to map creator/project to local.
        list ($auth,$type,$id) = Instance::ParseURN($row["creator_urn"]);
        if ($auth == $domain) {
            $user = User::LookupByUid($id);
125
        }
126 127
        else {
            $user = User::LookupNonLocal($row["creator_urn"]);
128
        }
129 130 131
        if ($user) {
            $creator     = $user->uid();
            $creator_idx = $user->idx();
132
        }
133
        else {
134 135 136 137 138 139 140 141 142 143
            $creator     = $id;
            $creator_idx = 0;
        }
        list ($auth,$type,$id) = Instance::ParseURN($row["project_urn"]);
        # project urn is in domain:project format.
        if (preg_match("/^([^:]+)\:([^:]+)$/", $auth, $matches)) {
            $project = Project::LookupByPid($matches[2]);
            if ($project) {
                $pid     = $project->pid();
                $pid_idx = $project->pid_idx();
144
            }
145 146
            else {
                $pid = $matches[2];
147
            }
148
        }
149 150 151 152 153 154 155 156 157 158
        #
        # Lets try to get a url.
        #
        list ($imagedomain,$type,$id) = Instance::ParseURN($row["urn"]);
        if ($imagedomain == $domain) {
            $url = "$TBBASE/showimageid.php3?imageid=" . $row["image_uuid"];
        }
        else {
            if (array_key_exists($imagedomain, $aggregates)) {
                $aggregate = $aggregates[$imagedomain];
159 160
            }
            else {
161 162 163 164 165 166 167 168
                $aggregate = Aggregate::LookupByDomain($imagedomain);
                if ($aggregate) {
                    $aggregates[$imagedomain] = $aggregate;
                }
            }
            if ($aggregate) {
                $url = $aggregate->weburl() .
                  "/showimageid.php3?imageid=" . $row["image_uuid"];
169
            }
170
        }
171 172 173 174 175 176 177 178 179
        $blob["description"] = $row["description"];
        $blob["imagename"]   = $row["imagename"];
        $blob["created"]     = DateStringGMT($row["created"]);
        $blob["pid"]         = $pid;
        $blob["pid_idx"]     = $pid_idx;
        $blob["global"]      = $row["visibility"] == "public" ? 1 : 0;
        $blob["creator"]     = $creator;
        $blob["creator_idx"] = $creator_idx;
        $blob["project_urn"] = $row["project_urn"];
180
        $blob["format"]      = $row["format"];
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
        $blob["urn"]         = $row["urn"];
        if (isset($url)) {
            $blob["url"] = $url;
        }
        $tmp[] = $blob;
    }
}
else {
    if (ISADMIN() && $all) {
        $joinclause  = "";
        $whereclause = "";
    }
    else {
        #
        # User is allowed to view the list of all global images, and all images
        # in his project. Include images in the subgroups too, since its okay
        # for the all project members to see the descriptors. They need proper 
        # permission to use/modify the image/descriptor of course, but that is
        # checked in the pages that do that stuff. In other words, ignore the
        # shared flag in the descriptors.
        #
        $uid_idx = $target_user->uid_idx();

        $joinclause =     
                "left join image_permissions as p1 on ".
                "     p1.imageid=i.imageid and p1.permission_type='group' ".
                "left join image_permissions as p2 on ".
                "     p2.imageid=i.imageid and p2.permission_type='user' and ".
                "     p2.permission_idx='$uid_idx' ".
                "left join group_membership as g on ".
                "     g.uid_idx='$uid_idx' and  ".
                "     (g.pid_idx=i.pid_idx or ".
                "      g.gid_idx=p1.permission_idx) ";
    
        $whereclause = "and (iv.global or p2.imageid is not null or ".
                     "g.uid_idx is not null) ";
    }
    $query =
219
           "select distinct i.imagename,iv.*,ov.* from images as i ".
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
           "left join image_versions as iv on ".
           "          iv.imageid=i.imageid and iv.version=i.version ".
           "left join os_info_versions as ov on ".
           "          i.imageid=ov.osid and ov.vers=i.version ".
           "left join osidtoimageid as map on map.osid=i.imageid ".
           $joinclause .
           "where (iv.ezid = 1 or iv.isdataset = 1) $whereclause ".
           "order by i.imagename";

    $query_result = DBQueryFatal($query);

    while ($row = mysql_fetch_array($query_result)) {
	$imageid = $row["imageid"];
        $name    = $row["imagename"];
        $pid     = $row["pid"];
        $urn     = "urn:publicid:IDN+${domain}+image+${pid}//${name}";
        $blob    = array();

238 239 240
        $blob["imageid"]     = $imageid;
        $blob["description"] = $row["description"];
        $blob["imagename"]   = $row["imagename"];
241
        $blob["created"]     = DateStringGMT($row["created"]);
242 243 244 245 246
        $blob["pid"]         = $row["pid"];
        $blob["pid_idx"]     = $row["pid_idx"];
        $blob["global"]      = $row["global"];
        $blob["creator"]     = $row["creator"];
        $blob["creator_idx"] = $row["creator_idx"];
247
        $blob["format"]      = $row["format"];
248 249 250 251
        $blob["urn"]         = $urn;
	$blob["url"]         = $TBBASE . "/" .
                             CreateURL("showimageid",
                                       URLARG_IMAGEID, $imageid);
252 253 254
        $tmp[] = $blob;
    }
}
255

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
#
# This is for the hidden search filter column. It indicates how 
# the user has access to the image. Creator, project, public.
#
foreach ($tmp as $blob) {
    $name    = $blob["imagename"];
    $pid     = $blob["pid"];
    $filters = array();
    
    if ($all && $blob["creator_idx"] == $target_user->uid_idx()) {
        $filters[] = "creator";
    }
    if (array_key_exists($pid, $projlist)) {
            $filters[] = "project";
    }
    if ($all) {
        if ($pid == "emulab-ops") {
            $filters[] = "system";
        }
        if ($blob["global"] != "0") {
            $filters[] = "public";
        }
    }
    else {
        if ($pid == "emulab-ops" && $blob["global"] != "0") {
            $filters[] = "system";
        }
        # If not in any filters and global, skip. 
        if (!count($filters) && $blob["global"] != "0") {
            continue;
        }
    }
    # If none of the filters match, then mark as admin so we can show
    # those under a separate checkbox.
    if (!count($filters)) {
        if ($all) {
            $filters[] = "admin";
        }
        else {
            continue;
        }
    }
    $blob["filter"] = implode(",", $filters);
    $images[] = $blob;
300 301 302 303 304 305 306 307 308 309 310 311 312 313
}
echo "<script type='text/plain' id='images-json'>\n";
echo htmlentities(json_encode($images)) . "\n";
echo "</script>\n";

echo "<script type='text/javascript'>\n";
$isadmin = (isset($this_user) && ISADMIN() ? 1 : 0);
echo "    window.ISADMIN    = $isadmin;\n";
echo "    window.ALL        = $all;\n";
echo "</script>\n";
echo "<script src='js/lib/jquery-2.0.3.min.js'></script>\n";
echo "<script src='js/lib/jquery.tablesorter.min.js'></script>\n";
echo "<script src='js/lib/jquery.tablesorter.widgets.min.js'></script>\n";

314 315 316 317 318 319
REQUIRE_UNDERSCORE();
REQUIRE_SUP();
REQUIRE_MOMENT();
SPITREQUIRE("js/images.js");

AddTemplate("images");
320 321
SPITFOOTER();
?>