added utf8 decode for the site name
[plewww.git] / planetlab / sites / site.php
1 <?php
2
3   // $Id$
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;
11
12 // Print header
13 require_once 'plc_drupal.php';
14 include 'plc_header.php';
15
16 // Common functions
17 require_once 'plc_functions.php';
18 require_once 'plc_peers.php';
19 require_once 'linetabs.php';
20 require_once 'table.php';
21 require_once 'details.php';
22 require_once 'form.php';
23 require_once 'toggle.php';
24
25 // -------------------- 
26 // recognized URL arguments
27 $site_id=intval($_GET['id']);
28 if ( ! $site_id ) { plc_error('Malformed URL - id not set'); return; }
29
30 ////////////////////
31 // Get all columns as we focus on only one entry
32 $sites = $api->GetSites( array($site_id));
33
34 if (empty($sites)) {
35   drupal_set_message ("Site " . $site_id . " not found");
36   return;
37  }
38
39 $site=$sites[0];
40 // var names to api return
41 $sitename= htmlentities(utf8_decode($site['name']));
42 $abbreviated_name= htmlentities($site['abbreviated_name']);
43 $site_url= $site['url'];
44 $login_base= $site['login_base'];
45 $site_lat= $site['latitude'];
46 $site_long= $site['longitude'];
47 $max_slivers= $site['max_slivers'];
48 $max_slices= $site['max_slices'];
49
50 $enabled = $site['enabled'];
51 $ext_consortium_id = $site ['ext_consortium_id'];
52
53 // get peer 
54 $peer_id= $site['peer_id'];
55 $peers = new Peers ($api);
56 $local_peer = ! $peer_id;
57
58 // extra privileges to admins, and (pi||tech) on this site
59 $is_site_pi = ( $local_peer && plc_is_pi() && plc_in_site($site_id) );
60 $is_site_tech = ( $local_peer && plc_is_pi() && plc_in_site($site_id) );
61 $is_site_admin = ($local_peer && plc_is_admin());
62   
63 $address_ids= $site['address_ids'];
64 $pcu_ids= $site['pcu_ids'];
65 $node_ids= $site['node_ids'];
66 $person_ids= $site['person_ids'];
67 $slice_ids= $site['slice_ids'];
68
69 $api->begin();
70 // gets address info
71 $api->GetAddresses( $address_ids );
72
73 // gets node info
74 $api->GetNodes( $node_ids, array( "node_id", "hostname", "boot_state", "pcu_ids", "ports" ) );
75
76 // gets person info
77 $api->GetPersons( $person_ids, array( "role_ids", "person_id", "first_name", "last_name", "email", "enabled" , "slice_ids") );
78
79 $api->GetSlices ( $slice_ids, array ("slice_id", "name", "instantiation", "node_ids", "person_ids" ) );
80
81 ////////////////////
82 // PCU stuff - not too sure why, but GetPCUs is not exposed to the 'user' role
83 $display_pcus = (plc_is_admin() || plc_is_pi() || plc_is_tech());
84 if ($display_pcus) 
85   $api->GetPCUs ($pcu_ids, array ('hostname', 'pcu_id' ));
86
87 // get results
88 if ($display_pcus)
89   list( $addresses, $nodes, $persons, $slices, $pcus )= $api->commit();
90 else
91   list( $addresses, $nodes, $persons, $slices )= $api->commit();
92   
93 $techs = array();
94 $pis = array();
95 $disabled_persons = array();
96 if ($persons) foreach( $persons as $person ) {
97   $role_ids= $person['role_ids'];
98
99   if ( in_array( '20', $role_ids ))     $pis[] = $person;
100   if ( in_array( '40', $role_ids ))     $techs[] = $person;
101   if ( ! $person['enabled'] )           $disabled_persons[] = $person;
102   
103 }
104
105 $has_disabled_persons = count ($disabled_persons) !=0;
106
107 // get number of slivers
108 $slivers_count=0;
109 if ($slices) foreach ($slices as $slice) $slivers_count += count ($slice['node_ids']);
110
111 ////////////////////////////////////////
112 drupal_set_title("Details for site " . $sitename);
113   
114 $tabs=array();
115
116 $tabs []= tab_mysite();
117
118 // available actions
119 if ( $is_site_admin)
120   $tabs['Expire slices'] = array('url'=>l_actions(),
121                                  'method'=>'POST',
122                                  'values'=>array('site_id'=>$site_id,
123                                                  'action'=>'expire-all-slices-in-site'),
124                                  'bubble'=>"Expire all slices and prevent creation of new slices",
125                                  'confirm'=>"Suspend all slices in $login_base");
126 if ( $is_site_admin)
127   $tabs['Delete']=array('url'=>l_actions(),
128                         'method'=>'POST',
129                         'values'=>array('site_id'=>$site_id,
130                                         'action'=>'delete-site'),
131                         'bubble'=>"Delete site $sitename",
132                         'confirm'=>"Are you sure you want to delete site $login_base");
133
134 if ( $is_site_pi ) 
135   $tabs ['Add slice'] = array ('url'=>l_slice_add(),
136                               'method'=>'post',
137                               'bubble'=>'Create new slice in site');
138
139 if (plc_is_admin() || plc_in_site($site_id))
140   $tabs["Events"]=array_merge (tablook_event(),
141                                array('url'=>l_event("Site","site",$site_id),
142                                      'bubble'=>"Events for site $sitename"));
143 if (plc_is_admin() || plc_in_site($site_id))
144   $tabs["Comon"]=array_merge(tablook_comon(),
145                              array('url'=>l_comon("site_id",$site_id),
146                                    'bubble'=>"Comon page for $sitename"));
147
148
149 plekit_linetabs($tabs);
150
151 // show gray background on foreign objects : start a <div> with proper class
152 $peers->block_start ($peer_id);
153
154 // sanity checks
155 if ( $local_peer ) {
156   // pending site
157   global $PENDING_CONSORTIUM_ID;
158   if ( $ext_consortium_id === $PENDING_CONSORTIUM_ID) {
159     if ( ! $enabled ) 
160       plc_warning ("This site is under pending registration - Please visit " . 
161                    href (l_sites_pending(),"this page") . 
162                    " to review pending applications.");
163     else 
164       plc_warning ("This site is pending but is also enabled - something is wrong. You should fix the issue with plcsh");
165   } else {
166     if ( ! $enabled) 
167       plc_warning ("This site is disabled.");
168   }
169 }
170
171 $can_update=(plc_is_admin ()  && $local_peer) || ( plc_in_site($site_id) && plc_is_pi());
172
173
174 $toggle = new PlekitToggle ('site',"Details",
175                             array('visible'=>get_arg('show_details'),
176                                   'bubble'=>'Display and modify details for that site'));
177 $toggle->start();
178
179 $details = new PlekitDetails($can_update);
180
181 $f = $details->form_start(l_actions(),array('action'=>'update-site','site_id'=>$site_id));
182
183 $details->start();
184
185 if ( ! $site['is_public']) 
186   $details->tr(plc_warning_html("This site is not public!"));
187
188 $details->th_td("Full name",$sitename,'name',array('width'=>50));
189 $details->th_td("Abbreviated name",$abbreviated_name,'abbreviated_name',array('width'=>15));
190 $details->th_td("URL",$site_url,'url',array('width'=>40));
191 $details->th_td("Latitude",$site_lat,'latitude');
192 $details->th_td("Longitude",$site_long,'longitude');
193
194 // modifiable by admins only
195 if (plc_is_admin()) 
196   $details->th_td("Login base",$login_base,'login_base',array('width'=>12));
197 else
198   $details->th_td("Login base",$login_base);
199 if (plc_is_admin())
200   $details->th_td("Max slices",$max_slices,'max_slices');
201 else
202   $details->th_td("Max slices",$max_slices);
203 if (plc_is_admin())
204 {
205   $selectors=array(array('display'=>"False",'value'=>'0'), 
206                                    array('display'=>"True",'value'=>'1'));
207   $selectors[intval($enabled)]['selected'] = 'selected';
208
209   $enable_select = $f->select_html ("enabled", $selectors);
210
211   $details->th_td("Enabled",$enable_select,'enabled', array('input_type' => 'select', 'value'=>$enabled));
212 } else
213   $details->th_td("Enabled",$enabled);
214
215 $details->tr_submit("submit","Update Site");
216
217 if ( ! $local_peer) {
218   $details->space();
219   $details->th_td("Peer",$peers->peer_link($peer_id));
220  }
221 $details->end();
222 $details->form_end();
223 $toggle->end();
224
225 //////////////////// mode details - for local object
226 if ( $local_peer ) {
227
228   //////////////////// nodes
229   $nb_boot = 0;
230   if ($nodes) foreach ($nodes as $node) if ($node['boot_state'] == 'boot') $nb_boot ++;
231
232   $nodes_title = "Nodes : ";
233   $nodes_title .= count($nodes) . " total";
234   $nodes_title .= " / " . $nb_boot . " boot";
235   if ($nb_boot < 2 ) 
236     $nodes_title = plc_warning_html ($nodes_title);
237   $nodes_title .= href(l_nodes_site($site_id)," (See as nodes)");
238
239   $toggle=new PlekitToggle ('nodes',$nodes_title,
240                             array('visible'=>get_arg('show_nodes')));
241   $toggle->start();
242
243   $headers=array();
244   $sort_column = '0';
245   if ($display_pcus) { $headers['PCU']='string'; $sort_column = '1' ; }
246   $headers['hostname']='string'; 
247   $headers['state']='string';
248
249   $table = new PlekitTable ('nodes',$headers,$sort_column,array('search_area'=>false,
250                                                                 'notes_area'=>false,
251                                                                 'pagesize_area'=>false));
252   // hash pcus on pcu_id
253   if ($display_pcus) {
254     global $pcu_hash;
255     $pcu_hash= array();
256     if ($pcus) foreach ($pcus as $pcu) $pcu_hash[$pcu['pcu_id']]=$pcu;
257   }
258   // search the pcu, return the string to display and mark the pcu as displayed
259   //  function display_and_mark ($pcu_hash,$pcu_ids,$ports) {
260   function display_and_mark ($pcu_ids,$ports) {
261     global $pcu_hash;
262     if (empty($pcu_ids)) return plc_warning_html('None');
263     $pcu_id=$pcu_ids[0];
264     if (empty($ports)) return plc_error_html('???');
265     $port=$ports[0];
266     $pcu=$pcu_hash[$pcu_id];
267     $display= l_pcu_href($pcu_id, $pcu['hostname'] . ' : ' . $port);
268     $pcu_hash[$pcu_id]['displayed']=true;
269     return $display;
270   }
271
272   $table->start();
273   foreach ($nodes as $node) {
274     $table->row_start();
275     if ($display_pcus) {
276       //      $table->cell(display_and_mark($pcu_hash,$node['pcu_ids'],$node['ports']));
277       $table->cell(display_and_mark($node['pcu_ids'],$node['ports']));
278     }
279     $table->cell (l_node_obj($node));
280     $table->cell ($node['boot_state']);
281     $table->row_end();
282   }
283   // show undisplayed PCU's if any
284   if ($display_pcus) 
285     if ($pcu_hash) foreach ($pcu_hash as $id=>$pcu) {
286         if (!$pcu['displayed']) {
287           $table->row_start();
288           $table->cell($pcu['hostname']); $table->cell(''); $table->cell('');
289           $table->row_end();
290         }
291       }
292     
293   $table->tfoot_start();
294   $table->row_start();
295   $button=new PlekitFormButton (l_node_add(),"node_add","Add node","POST");
296   $table->cell($button->html(),array('hfill'=>true,'align'=>'right'));
297   $table->row_end();
298   $table->end();
299   $toggle->end();
300     
301   //////////////////// Users
302   $persons_title = "Users : ";
303   $persons_title .= count($person_ids) . " total";
304   $persons_title .= " / " . count ($pis) . " PIs";
305   $persons_title .= " / " . count ($techs) . " Techs";
306   if ($has_disabled_persons) 
307     $persons_title .= " / " . count($disabled_persons) . " Disabled";
308   if ( (count ($pis) == 0) || (count ($techs) == 0) || (count($person_ids) >= 30) || count($disabled_persons) != 0 ) 
309     $persons_title = plc_warning_html ($persons_title);
310   $persons_title .= href(l_persons_site($site_id)," (See as users)");
311
312   $toggle=new PlekitToggle ('persons',$persons_title,
313                             array('visible'=>get_arg('show_persons')));
314   $toggle->start();
315
316   $headers = array ();
317   $headers["email"]='string';
318   $headers["S"]='int';
319   $headers["PI"]='string';
320   $headers['User']='string';
321   $headers["Tech"]='string';
322   if ($has_disabled_persons) $headers["Disabled"]='string';
323   $notes=array('S = slices');
324   $table=new PlekitTable('persons',$headers,'1r-3r-0',array('search_area'=>false,
325                                                             'notes'=>$notes,
326                                                             'pagesize_area'=>false));
327   $table->start();
328   if ($persons) foreach ($persons as $person) {
329     $table->row_start();
330     $table->cell(l_person_obj($person));
331     $table->cell(count($person['slice_ids']));
332     $table->cell( in_array ('20',$person['role_ids']) ? "yes" : "no");
333     $table->cell( in_array ('30',$person['role_ids']) ? "yes" : "no");
334     $table->cell( in_array ('40',$person['role_ids']) ? "yes" : "no");
335     if ($has_disabled_persons) $table->cell( $person['enabled'] ? "no" : plc_warning_html("yes"));
336     $table->row_end();
337   }
338   $table->end();
339   $toggle->end();
340
341   //////////////////// Slices
342   $slices_title="Slices : ";
343   $slices_title .= $max_slices . " max";
344   $slices_title .= " / " . count($slice_ids) . " running";
345   $slices_title .= " / $slivers_count slivers";
346   if (count($slice_ids) >= $max_slices) 
347     $slices_title = plc_warning_html($slices_title);
348   $slices_title .= href(l_slices_site($site_id)," (See as slices)");
349   
350   $toggle=new PlekitToggle ('slices',$slices_title,
351                             array('visible'=>get_arg('show_slices')));
352   $toggle->start();
353
354   $headers = array ();
355   $headers ['name']='string';
356   $headers ['I'] = 'string';
357   $headers ['N']='int';
358   $headers ['U']='int';
359   $notes=array('I = instantiation type',
360                'N = number of nodes',
361                'U = number of users');
362   $table=new PlekitTable ('slices',$headers,0,array('search_area'=>false,
363                                                     'pagesize_area'=>false,
364                                                     'notes'=>$notes));
365
366   $table->start();
367   if ($slices) foreach ($slices as $slice) {
368       $table->row_start();
369       $table->cell(l_slice_obj($slice));
370       $table->cell(instantiation_label($slice));
371       $table->cell (href(l_nodes_slice($slice['slice_id']),count($slice['node_ids'])));
372       $table->cell (count($slice['person_ids']));
373       $table->row_end();
374     }
375   if ($is_site_pi) {
376     $button=new PlekitFormButton (l_slice_add(),"slice_add","Add slice","post");
377     $table->tfoot_start();
378     $table->row_start();
379     $table->cell($button->html(),array('hfill'=>true,'align'=>'right'));
380   }
381     
382   $table->end();
383   $toggle->end();
384
385   $form=new PlekitForm (l_actions(), array('site_id'=>$site_id));
386   $form->start();
387   //////////////////////////////////////////////////////////// Tags
388   // tags section
389   // already inside a if ( $local_peer )...
390   
391   $tags=$api->GetSiteTags (array('site_id'=>$site_id));
392   function get_tagname ($tag) { return $tag['tagname'];}
393   // xxx looks like tech-only see an error here, 
394   // might be that GetSiteTags is not accessible or something
395   $tagnames = array_map ("get_tagname",$tags);
396   
397   $toggle = new PlekitToggle ('tags',count_english($tags,'tag'),
398                               array('bubble'=>'Inspect and set tags on that site',
399                                     'visible'=>get_arg('show_tags')));
400   $toggle->start();
401
402   $headers=array("Name"=>"string",
403                  "Value"=>"string",
404                  );
405   if (plc_is_admin()) $headers[plc_delete_icon()]="none";
406   
407   $table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10);
408   $table=new PlekitTable("site_tags",$headers,0,$table_options);
409   $table->start();
410   if ($tags) foreach ($tags as $tag) {
411       $table->row_start();
412       $table->cell(l_tag_obj($tag));
413       $table->cell($tag['value']);
414       // the remove checkbox
415       if (plc_is_admin()) $table->cell ($form->checkbox_html('site_tag_ids[]',$tag['site_tag_id']));
416       $table->row_end();
417     }
418   
419   if ($is_site_pi || $is_site_admin) {
420     $table->tfoot_start();
421
422     // remove tag 
423     $table->row_start();
424     $table->cell($form->submit_html("delete-site-tags","Remove Tags"),
425                  // use the whole columns and right adjust
426                  array('hfill'=>true,'align'=>'right'));
427     $table->row_end();
428
429     // set tag area
430     $table->row_start();
431     // get list of tag names in the site/* category    
432     $all_tags= $api->GetTagTypes( array ("category"=>"site*","-SORT"=>"tagname"), array("tagname","tag_type_id"));
433     // xxx cannot use onchange=submit() - would need to somehow pass action name 
434     function tag_selector ($tag) { return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']); }
435     $selector=array_map("tag_selector",$all_tags);
436     $table->cell($form->select_html("tag_type_id",$selector,array('label'=>"Choose")));
437     $table->cell($form->text_html("value","",array('width'=>8)));
438     $table->cell($form->submit_html("set-tag-on-site","Set Tag"),array('columns'=>2,'align'=>'left'));
439     $table->row_end();
440   }
441   
442   $table->end();
443   $toggle->end();
444   $form->end();
445
446   //////////////////// Addresses
447   $toggle=new PlekitToggle ('addresses',"Addresses",
448                             array('visible'=>get_arg('show_addresses')));
449   $toggle->start();
450   if ( ! $addresses) {
451     print "<p class='addresses'>No known address for this site</p>";
452   } else {
453     $details=new PlekitDetails (false);
454     $details->start();
455     $details->th_td("Addresses","");
456     foreach ($addresses as $address) {
457       $details->th_td(plc_vertical_table($address['address_types']),
458                        plc_vertical_table(array($address['line1'],
459                                                 $address['line2'],
460                                                 $address['line3'],
461                                                 $address['city'],
462                                                 $address['state'],
463                                                 $address['postalcode'],
464                                                 $address['country'])));
465     }
466     $details->end();
467   }
468   $toggle->end();
469
470  }
471
472 ////////////////////////////////////////
473 $peers->block_end($peer_id);
474
475 //plekit_linetabs ($tabs,"bottom");
476
477 // Print footer
478 include 'plc_footer.php';
479
480 ?>