moving the onelab www to a fresh location
[plewww.git] / planetlab / sites / index.php
1 <?php
2 // $Id: index.php 1162 2008-01-24 19:13:12Z thierry $
3 //
4
5 // Require login
6 require_once 'plc_login.php';
7
8 // Get session and API handles
9 require_once 'plc_session.php';
10 global $plc, $api, $adm;
11
12 // Print header
13 require_once 'plc_drupal.php';
14 // set default
15 drupal_set_title('Sites');
16 include 'plc_header.php';
17
18 // Common functions
19 require_once 'plc_functions.php';
20 require_once 'plc_sorts.php';
21
22 // find person roles
23 $_person= $plc->person;
24 $_roles= $_person['role_ids'];
25
26 ////////////////////
27 // The set of columns to fetch
28 // and the filter applied for fetching sites
29 if ( !in_array( '10', $_roles ) ) {
30   $columns = array("site_id", "name", "abbreviated_name", "login_base" , "peer_id" );
31   $filter = array ("enabled" => TRUE);
32  } else {
33   $columns = array("site_id", "name", "abbreviated_name", "login_base" , "peer_id" , "enabled", "person_ids", "max_slices", "slice_ids", "node_ids");
34   $filter = array ();
35  }
36
37
38 //////////////////
39 // perform post-processing on site objects as returned by GetSites
40 // performs sanity check and summarize the result in a single column
41 // performs in-place replacement, so passes a reference
42 function layout_site ($site) {
43
44   // we need the 'enabled' field to do this
45   // so regular users wont run this
46   if ( ! array_key_exists ('enabled', $site))
47     return $site;
48     
49   $messages=array();
50   
51   if (empty ($site['node_ids'])) $messages [] = "Site has no node";
52   $class='plc-foreign';
53
54   // do all this stuff on local sites only
55   if ( ! $site['peer_id'] ) {
56     
57     $class='plc-warning';
58
59     // check that site is enabled
60     if ( ! $site['enabled']) {
61       $messages [] = "Not enabled";
62     }
63   
64     // check that site has at least a PI and a tech
65     global $api;
66     $persons=$api->GetPersons(array("person_id"=>$site['person_ids']),array("role_ids"));
67     $nb_pis=0;
68     $nb_tech=0;
69     
70     if ( ! empty ($persons)) {
71       foreach ($persons as $person) {
72         if (in_array( '20', $person['role_ids'])) $nb_pis += 1;
73         if (in_array( '40', $person['role_ids'])) $nb_techs += 1;
74       }
75     }
76     if ($nb_pis == 0) $messages [] = "Site has no PI";
77     if ($nb_techs == 0) $messages [] = "Site has no Tech";
78     
79     // check number of slices
80     if ( $site['max_slices'] == 0) {
81       $messages [] = "No slice allowed";
82     } else if (count($site["slice_ids"]) >= $site['max_slices']) {
83       $messages [] = "Site has no slice left";
84     }
85     
86     // pretty print the cell
87   }
88   // but always cleanup $site columns
89   unset ($site['enabled']);
90   unset ($site['person_ids']);
91   unset ($site['max_slices']);
92   unset ($site['slice_ids']);
93   unset ($site['node_ids']);
94   $site['sanity check'] = plc_make_table($class,$messages);
95   
96   return $site;
97 }
98
99
100 // if sitepattern or peerscope is set : search the sites
101 // we use GET rather than POST so paginate can display the right contents on subsequent pages
102 // can be useful for writing bookmarkable URL's as well
103 if( $_GET['sitepattern'] || $_GET['peerscope']) {
104   $sitename= $_GET['sitepattern'];
105   if (empty($sitename)) { 
106     $sitename="*";
107   }
108   $filter = array_merge (array( "name"=>$sitename ), $filter);
109   switch ($_GET['peerscope']) {
110   case '':
111     $peer_label="all peers";
112     break;
113   case 'local':
114     $filter=array_merge(array("peer_id"=>NULL),$filter);
115     $peer_label="local peer";
116     break;
117   case 'foreign':
118     $filter=array_merge(array("~peer_id"=>NULL),$filter);
119     $peer_label="foreign peers";
120     break;
121   default:
122     $peer_id=intval($_GET['peerscope']);
123     $filter=array_merge(array("peer_id"=>$peer_id),$filter);
124     $peer=$api->GetPeers(array("peer_id"=>$peer_id));
125     $peer_label='peer "' . $peer[0]['peername'] . '"';
126     break;
127   }
128
129   $sites= $api->GetSites( $filter , $columns);
130   $sites = array_map(layout_site,$sites);
131
132   $sites_count = count ($sites);
133   if ( $sites_count == 1) {
134     header( "location: index.php?id=". $sites[0]['site_id'] );
135     exit();
136   } else if ( $sites_count == 0) {
137     echo "<span class='plc-warning'> No site matching $sitename on " . $peer_label . " </span>";
138   } else {
139     drupal_set_title ("Sites matching $sitename on " . $peer_label);
140     sort_sites ($sites);
141     echo paginate( $sites, "site_id", "Sites", 25, "name");
142   }
143 }
144
145
146 // if no site id, display list of sites to choose
147 elseif( !$_GET['id'] ) {
148
149   // GetSites API call
150   // careful, need to pass NULL and *not* array() if no filter is given
151   $sites= $api->GetSites( empty($filter) ? NULL : $filter, $columns );
152   $sites = array_map(layout_site,$sites);
153   if ( empty ($sites)) {
154     echo "No site to display";
155   } else {
156     sort_sites( $sites );
157
158     drupal_set_html_head('<script type="text/javascript" src="/planetlab/bsn/bsn.Ajax.js"></script>
159     <script type="text/javascript" src="/planetlab/bsn/bsn.DOM.js"></script>
160     <script type="text/javascript" src="/planetlab/bsn/bsn.AutoSuggest.js"></script>');
161     
162     echo "<div>\n
163         <form method=get action='index.php'>\n";
164     echo "<table><tr>\n
165 <th><label for='testinput'>Enter Site Name or pattern: </label></th>\n
166 <td><input type='text' id='testinput' name='sitepattern' size=40 value='' /></td>\n
167 <td rowspan=2><input type=submit value='Search Sites' /></td>\n
168 </tr> <tr>
169 <th><label for='peerscope'>Federation scope: </label></th>\n
170 <td><select id='peerscope' name='peerscope' onChange='submit()'>\n
171 ";
172     echo plc_peers_option_list($api);
173     echo "</select></td>\n
174 </tr></table></form></div>\n
175 <br />\n";
176     
177     echo paginate( $sites, "site_id", "Sites", 25, "name" );
178
179     echo "<script type=\"text/javascript\">\n
180 var options = {\n
181         script:\"/planetlab/sites/test.php?\",\n
182         varname:\"input\",\n
183         minchars:1\n
184 };\n
185 var as = new AutoSuggest('testinput', options);\n
186 </script>\n";
187   }
188 }
189 else {
190   // stores get variable and casts it as int
191   $site_id= intval( $_GET['id'] );
192
193   // api call GetSites
194   $site_info= $adm->GetSites( array( $site_id ) );
195
196   // var names to api return
197   $sitename= $site_info[0]['name'];
198   $abbrev_name= $site_info[0]['abbreviated_name'];
199   $site_url= $site_info[0]['url'];
200   $login_base= $site_info[0]['login_base'];
201   $site_lat= $site_info[0]['latitude'];
202   $site_long= $site_info[0]['longitude'];
203   $max_slivers= $site_info[0]['max_slivers'];
204   $max_slices= $site_info[0]['max_slices'];
205
206   $enabled = $site_info[0]['enabled'];
207
208   // peer id
209   $peer_id= $site_info[0]['peer_id'];
210
211   $site_addresses= $site_info[0]['address_ids'];
212   $site_pcus= $site_info[0]['pcu_ids'];
213   $site_nodes= $site_info[0]['node_ids'];
214   $site_persons= $site_info[0]['person_ids'];
215   $site_slices= $site_info[0]['slice_ids'];
216
217   $adm->begin();
218   // gets address info
219   $adm->GetAddresses( $site_addresses );
220
221   // gets pcu info
222   $adm->GetPCUs( $site_pcus );
223
224   // gets node info
225   $adm->GetNodes( $site_nodes, array( "node_id", "hostname", "boot_state" ) );
226
227   // gets person info
228   $adm->GetPersons( $site_persons, array( "role_ids", "person_id", "first_name", "last_name", "email", "enabled" ) );
229
230   $adm->GetSlices ( $site_slices, array ("slice_id", "name", "instantiation" ) );
231
232   list( $addresses, $pcus, $nodes, $persons, $slices )= $adm->commit();
233   
234   $techs = array();
235   $pis = array();
236   foreach( $persons as $person ) {
237     $role_ids= $person['role_ids'];
238     if( in_array( '40', $role_ids ))
239       $techs[] = $person;
240
241     if( in_array( '20', $role_ids ))
242       $pis[] = $person;
243
244   }
245
246   if( $peer_id ) {
247     echo "<div class='plc-foreign'>\n";
248   }
249
250   // start form
251   drupal_set_title("Site $sitename details");
252   //  echo "<h3>$sitename Site details</h3>\n";
253
254   if( !$peer_id ) {
255     $actions= array( ''=>'Choose Action' );
256     
257     if( in_array( 10, $_roles ) 
258         || ( in_array( 20, $_roles ) && in_array( $site_id, $_person['site_ids'] ) ) ) {
259       $actions['update']= 'Update Site';
260     }
261     
262     if( in_array( 10, $_roles ) ) {
263       $actions['delete']= 'Delete Site';
264       $actions['expire']= 'Expire All Slices';
265     }
266     
267     echo "<table> <tr><td>";
268     if( in_array( 10, $_roles ) ) {
269       echo plc_event_button("Site","site",$site_id);
270       echo "</td><td>\n";
271     }
272     echo plc_comon_button("site_id",$site_id);
273     // comon link
274     echo "</td><td>";
275
276     // list to take site action
277     echo "<form action='/db/sites/site_action.php' method='post'>\n";
278     echo "<input type=hidden name=site_id value=$site_id>\n";
279     echo "<select name='actions' onChange=\"submit();\">\n";
280     foreach( $actions as $key => $val ) {
281       echo "<option value='$key'";
282       if( $key == $_POST['actions'] ) {
283         echo " selected";
284       }
285       echo ">$val\n";
286     }
287     echo "</select>\n";
288     echo "</form>";
289     
290     echo "</td></tr></table>\n";
291   }
292         
293
294   if ( ! $enabled ) {
295     echo "<p class='plc-warning'> This site is not enabled - Please visit <a href='/db/sites/join_request.php'> this page </a> to review pending applications. </p>";
296   }
297
298   // basic site info
299   echo "<p><table class='list_set' border=0>\n
300   <tr class='list_set'><th>Full name: </th><td class='list_set'> $sitename</td></tr>\n
301   <tr class='list_set'><th>Login base: </th><td class='list_set'> $login_base</td></tr>\n
302   <tr class='list_set'><th>Abbreviated Name: </th><td class='list_set'> $abbrev_name</td></tr>\n
303   <tr class='list_set'><th>URL: </th><td class='list_set'> <a href='$site_url'>$site_url</a></td></tr>\n
304   <tr class='list_set'><th>Latitude: </th><td class='list_set'> $site_lat</td></tr>\n
305   <tr class='list_set'><th>Longitude: </th><td class='list_set'> $site_long</td></tr>\n";
306         
307   if( $peer_id ) {
308     // display peer name
309     echo "<tr><th></th><td></td></tr>\n";
310     $peer = $api->GetPeers(array('peer_id'=>$peer_id));
311     echo "<tr><th>Managed at foreign peer:</th><td>" . $peer[0]['peername'] . "</td></tr>";
312     // we wrap up everything here
313     // the local version closes the table in the middle of the page...
314     echo "</table>";
315     echo "<br /></div>";
316   
317   } else {
318
319     // Slices
320     echo "<tr><th></th><td></td></tr>\n";
321     $href="'/db/slices/index.php?site_id=" . $site_id . "'";
322     $slice_text="" . count($site_slices) . "/" . $max_slices;
323     if (count($site_slices) >= $max_slices) {
324       $slice_text .= " <span class='plc-warning'>Maximum number of slices reached !</span>";
325     }
326     printf("<tr><th><a href=%s># Slices Used/Allocated:</a> </th><td> <a href=%s>%s</a></td></tr>\n",$href,$href,$slice_text);
327     if ( ! empty ($site_slices)) {
328       foreach ($slices as $slice) {
329         $href="'/db/slices/index.php?id=" . $slice['slice_id'] . "'";
330         printf ("<tr><td><a href=%s> %s</a></td><td> <a href=%d> %s </a> </td> </tr>\n",
331                 $href,$slice['name'],$href,$slice['instantiation']);
332       }
333     }
334
335     // Users
336     echo "<tr><th></th><td></td></tr>\n";
337     $href="'/db/persons/index.php?site_id=" . $site_id . "'";
338     printf ("<tr><th> <a href=%s># Users</a></th><td><a href=%s>Total %d users</a></td>\n",$href,$href,count($site_persons));
339
340     echo "<tr><th>PI(s)</th><td>";
341     if ( ! $pis) {
342       echo "<span class='plc-warning'>Site has no PI !!</span>";
343     } else {
344       echo "<table border=0>";
345       foreach( $pis as $person ) {
346         echo "<tr><td>";
347         if ( ! $person['enabled'] ) {
348           printf("<span class='plc-warning'> <a href='/db/persons/index.php?id=%d'>%s</a> (not enabled yet)</span><br />\n",$person['person_id'],$person['email']);
349         } else {
350           printf("<a href='/db/persons/index.php?id=%d'>%s</a><br />\n",$person['person_id'],$person['email']);
351         }
352         echo "</td></tr>\n";
353       }
354       echo "</table>\n";
355     }
356     echo "</td></tr>\n";
357
358     echo "<tr><th>Tech(s)</th><td>";
359     if ( ! $techs) {
360       echo "<span class='plc-warning'>Site has no Technical contact !!</span>";
361     } else {
362       echo "<table border=0>";
363       foreach( $techs as $person ) {
364         echo "<tr><td>";
365         if ( ! $person['enabled'] ) {
366           printf("<span class='plc-warning'> <a href='/db/persons/index.php?id=%d'>%s</a> (not enabled yet)</span><br />\n",$person['person_id'],$person['email']);
367         } else {
368           printf("<a href='/db/persons/index.php?id=%d'>%s</a><br />\n",$person['person_id'],$person['email']);
369         }
370         echo "</td></tr>\n";
371       }
372       echo "</table>\n";
373     }
374     echo "</td></tr>\n";
375
376     // Nodes 
377     echo "<tr><th></th><td></td></tr>\n";
378     $href="'/db/nodes/index.php?site_id=" . $site_id . "'";
379     printf ("<tr><th><a href=%s># Nodes</a></th>",$href);
380     if (empty ($site_nodes)) {
381       $right_site = in_array($site_id,$_person['site_ids']);
382       $right_role = in_array(30,$_roles) || in_array(40,$_roles);
383       $can_add = $right_site && $right_role;
384       if ($can_add) {
385         $href_add_node = "'/db/nodes/add_node.php'";
386         printf ("<td><a href=%s>0 <span class='plc-warning'>Site has no node, please add one</span></a></td>",$href_add_node); 
387       } else {
388         printf ("<td><a href=%s>0 <span class='plc-warning'>Site has no node</span></a></td>",$href);
389       }
390     } else {
391       printf ("<td><a href=%s>%d</a></td>",$href,count($nodes));
392     }
393     echo "</tr>";
394
395     if( !empty( $site_nodes ) ) {
396       foreach( $nodes as $node ) {
397         echo "<tr><td><a href='/db/nodes/index.php?id=". $node['node_id'] ."'>". $node['hostname'] ."</a></td><td class='list_set'>". $node['boot_state'] ."</td></tr>\n";
398       }
399       
400     }
401     
402     echo "</table>\n";
403
404     // Addresses
405     echo "<hr />\n";
406     
407     // if there is an address list it.
408     if( !empty( $addresses ) ) {
409       echo "<h4>Addresses</h4>\n";
410       
411       foreach( $addresses as $address ) {
412         echo "<p><table cellpadding=2><tbody><tr><td><a href='/db/addresses/index.php?id=". $address['address_id'] ."'>";
413         
414         $comma= count( $address['address_types'] );
415         $count= 0;
416         foreach( $address['address_types'] as $add_type ) {
417           echo $add_type;
418           $count++;
419           if ( $comma > 0 && $count != $comma )
420             echo ", ";
421         }
422         
423         echo "</a></td></tr>\n<tr><td>". $address['line1'] ."</td></tr>\n</tr><td>". $address['line2'] ."</td></tr>\n<tr><td>". $address['line3'] ."</td></tr>\n<tr><td>". $address['city'] .", ". $address['state'] ." ". $address['postalcode'] ."</td></tr>\n<tr><td>". $address['country'] ."</td></tr></tbody></table></p>\n";
424       }
425       
426     }
427     else {
428       echo "Site has no addresses. \n";
429     }
430     
431     // if eligable display add address
432     /*if( in_array( '10', $_roles ) || in_array( '20', $_roles ) )
433      echo "<br /><p><a href='/db/addresses/add_address.php'>Add an address</a>\n";*/
434     
435   }
436   
437   echo "<br /><hr /><p><a href='/db/sites/index.php'>Back to site list</a>";
438
439 }
440
441
442 // Print footer
443 include 'plc_footer.php';
444
445 ?>