showimageid_list.php3 6.98 KB
Newer Older
1
<?php
Leigh Stoller's avatar
Leigh Stoller committed
2
#
3
# Copyright (c) 2000-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/>.
# 
# }}}
Leigh Stoller's avatar
Leigh Stoller committed
23
#
24 25 26 27 28 29
include("defs.php3");

#
#
# Only known and logged in users allowed.
#
30 31 32
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
33 34

#
35
# Admin users can see all ImageIDs, while normal users can only see
36 37
# ones in their projects or ones that are globally available.
#
38 39
$optargs = OptionalPageArguments("searchfor", PAGEARG_STRING,
				 "searchby",  PAGEARG_STRING);
40
$extraclause = "";
Leigh Stoller's avatar
Leigh Stoller committed
41
$extrajoin   = "";
42

43 44 45
#
# Standard Testbed Header
#
46
PAGEHEADER("Image Search");
47

48
if (isset($searchfor) && isset($searchby)) {
49
    if (! preg_match('/^[\w\:,\.\+]+$/', $searchfor)) {    
50 51 52 53 54 55
	USERERROR("Illegal characters in search clause", 1);
    }
    if ($searchby == "nodetype") {
	$tokens = array();
	
	foreach (preg_split("/,/", $searchfor) as $type) {
Leigh Stoller's avatar
Leigh Stoller committed
56
	    $tokens[] = "oi.type='$type'";
57 58 59
	}
	$extraclause = join(" or ", $tokens);
	$extraclause = "and ($extraclause)";
Leigh Stoller's avatar
Leigh Stoller committed
60 61
        $extrajoin   = "left join osidtoimageid as oi on ".
                     "oi.osid=i.imageid and oi.imageid=i.imageid ";
62
    }
63 64 65 66
    elseif ($searchby == "features") {
	$tokens = array();
	
	foreach (preg_split("/,/", $searchfor) as $feature) {
67
	    $tokens[] = "find_in_set('$feature',ov.osfeatures)";
68 69 70 71 72 73
	}
	$extraclause = join(" or ", $tokens);
	$extraclause = "and ($extraclause)";
    }
    elseif ($searchby == "namedesc") {
	$safe_searchfor = addslashes($searchfor);
74 75
	$extraclause = "and (match (iv.imagename,iv.description) ".
	    "against('*${safe_searchfor}*'))";
76 77
    }
}
Leigh Stoller's avatar
Leigh Stoller committed
78 79 80 81
else {
    # For test in forms below.
    $searchby = "";
}
82

83
$query =
84
    "select distinct iv.*,i.imagename from images as i ".
85 86 87
    "left join image_versions as iv on ".
    "          iv.imageid=i.imageid and iv.version=i.version ".
    "left join os_info_versions as ov on ".
Leigh Stoller's avatar
Leigh Stoller committed
88 89
    "          i.imageid=ov.osid and ov.vers=i.version ".
    $extrajoin;
90

91
#
92
# Tack on the permission clause for mere users. 
93
#
94
if (!$isadmin) {
95 96 97 98 99 100 101
    #
    # 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.
102 103 104
    #
    $uid_idx = $this_user->uid_idx();
    
105 106 107 108 109 110 111 112 113 114
    $query .=
	"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) ".
115
	"where (iv.global or p2.imageid is not null or g.uid_idx is not null) ";
116 117 118
}
else {
    $query .= "where 1 ";
119 120
}

121
$query .=
122
    "and (iv.ezid = 1 or iv.isdataset = 1) $extraclause ".
123 124 125 126
    "order by i.imagename";

$query_result = DBQueryFatal($query);

127 128
SUBPAGESTART();
SUBMENUSTART("More Options");
Leigh Stoller's avatar
Leigh Stoller committed
129 130
WRITESUBMENUBUTTON("Import an Amazon EC2 Instance Image",
		   "newimageid_ez.php3?ec2=1");
131 132
WRITESUBMENUBUTTON("More info on Images",
		   "$WIKIDOCURL/Tutorial#CustomOS");
133
if ($isadmin) {
134 135
    WRITESUBMENUBUTTON("Create an Image Descriptor",
		       "newimageid_ez.php3");
136
    WRITESUBMENUBUTTON("Create an OS Descriptor",
137
		       "newosid.php3");
138 139
    WRITESUBMENUBUTTON("OS Descriptor list",
		       "showosid_list.php3");
140
}
141
SUBMENUEND();
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
echo "<table class=stealth>\n";
echo "<tr><form action=showimageid_list.php3 method=get>
      <td class=stealth><b>Find images that run on:&nbsp</b> 
      <input type=text style=\"float:right\"
             name=searchfor
             size=40
             value=\"" . ($searchby == "nodetype" ? $searchfor : "") . "\"</td>
      <input type=hidden name=searchby value='nodetype'>
      <td class=stealth>
        <b><input type=submit name=search1 value=Search></b></td>
      <td class=stealth>
         Comma separated list of types</td>\n";
echo "</form></tr>\n";
echo "<tr><form action=showimageid_list.php3 method=get>
      <td class=stealth><b>Find images with with features:&nbsp</b> 
      <input type=text 
             name=searchfor
             size=40
             value=\"" . ($searchby == "features" ? $searchfor : "") . "\"</td>
      <input type=hidden name=searchby value='features'>
      <td class=stealth>
        <b><input type=submit name=search2 value=Search></b></td>
      <td class=stealth>
         Comma separated list of features</td>\n";
echo "</form></tr>\n";
echo "<tr><form action=showimageid_list.php3 method=get>
      <td class=stealth><b>Search name and description:&nbsp</b> 
      <input type=text style=\"float:right\"
             name=searchfor
             size=40
             value=\"" . ($searchby == "namedesc" ? $searchfor : "") . "\"</td>
      <input type=hidden name=searchby value='namedesc'>
      <td class=stealth>
        <b><input type=submit name=search3 value=Search></b></td>
      <td class=stealth>
         Plain text, case insensitive search</td>\n";
echo "</form></tr>\n";
echo "</table>\n";
180 181
SUBPAGEEND();

182
if (mysql_num_rows($query_result)) {
183 184 185
    $numrows = mysql_num_rows($query_result);
    
    echo "<center>There are $numrows matching images.</center>\n";
186
    echo "<table border=2 cellpadding=0 cellspacing=2 id='showimagelist'
187
           align='center'>\n";
188

189 190
    echo "<thead class='sort'>
           <tr>
191 192 193
              <th class='sorttable_alpha'>Image</th>
              <th class='sorttable_alpha'>PID</th>
              <th class='sorttable_alpha'>Description</th>
194 195
           </tr>
          </thead>\n";
196

197
    while ($row = mysql_fetch_array($query_result)) {
198 199 200 201
	$imageid    = $row["imageid"];
	$descrip    = $row["description"];
	$imagename  = $row["imagename"];
	$pid        = $row["pid"];
Leigh Stoller's avatar
Leigh Stoller committed
202
	$url        = CreateURL("showimageid", URLARG_IMAGEID, $imageid);
203

204
	echo "<tr>
205
                  <td><A href='$url'>$imagename</A></td>
206 207 208 209 210
                  <td>$pid</td>
                  <td>$descrip</td>\n";
        echo "</tr>\n";
    }
    echo "</table>\n";
211
}
212 213 214
echo "<script type='text/javascript' language='javascript'>
	sorttable.makeSortable(getObjbyName('showimagelist'));
      </script>\n";
215 216 217 218 219 220

#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>