5 # note: this needs to be consistent with the value in Monitor/monitor/wrapper/plc.py
6 global $PENDING_CONSORTIUM_ID;
7 $PENDING_CONSORTIUM_ID = 0;
8 # this one does not matter that much, just picking one
9 global $APPROVED_CONSORTIUM_ID;
10 $APPROVED_CONSORTIUM_ID = 999999;
13 function my_is_int ($x) {
14 return (is_numeric($x) ? intval($x) == $x : false);
17 //////////////////////////////////////////////////////////// roles & other checks on global $plc
18 function plc_is_admin () {
20 return in_array( 10, $plc->person['role_ids']);
22 function plc_is_pi () {
24 return in_array( 20, $plc->person['role_ids']);
26 function plc_is_user () {
28 return in_array( 30, $plc->person['role_ids']);
30 function plc_is_tech () {
32 return in_array( 40, $plc->person['role_ids']);
34 function plc_in_site ($site_id) {
36 return in_array( $site_id, $plc->person['site_ids']);
39 function plc_my_site_ids () {
41 return $plc->person['site_ids'];
43 function plc_my_sites () {
46 return $api->GetSites($plc->person['site_ids'], array('site_id', 'abbreviated_name'));
49 function plc_my_site_id () {
51 return $plc->person['site_ids'][0];
54 function plc_my_person () {
57 function plc_my_person_id () {
59 return $plc->person['person_id'];
62 //////////////////////////////////////////////////////////// links
63 function href ($url,$text) { return "<a href='" . $url . "'>" . $text . "</a>"; }
66 // l_objects() -> the url to the page that list objects
67 // l_object($object_id) -> the url to hte page thas details object with given id
68 // l_object_t($object_id,text) -> an <a> tag that shows text and links to the above
69 // l_object_add () -> the url to that object-afding page
71 function l_actions () { return "/db/common/actions.php"; }
72 // some complex node actions are kept separate, e.g. the ones related to getbootmedium
73 function l_actions_download () { return "/db/nodes/node_downloads.php"; }
74 function l_register_node () { return "/registerwizard/index.php"; }
75 function l_pcu_add () { return "/registerwizard/index.php/register/stage1_addpcu"; }
76 function l_pcu ($pcu_id) { return "/db/sites/pcu.php?id=$pcu_id"; }
78 function l_nodes () { return "/db/nodes/index.php"; }
79 function l_nodes_peer ($peer_id) { return "/db/nodes/index.php?peerscope=$peer_id"; }
80 function l_node ($node_id) { return "/db/nodes/node.php?id=$node_id"; }
81 function l_node_t ($node_id,$text) { return href (l_node($node_id),$text); }
82 function l_node_obj($node) { return href(l_node($node['node_id']),$node['hostname']); }
83 function l_node_add () { return "/db/nodes/node_add.php"; }
84 function l_nodes_site ($site_id) { return "/db/nodes/index.php?site_id=$site_id"; }
85 function l_nodes_my_site () { return l_nodes_site(plc_my_site_id()) . "&active_line_tab=My site nodes"; }
86 function l_nodes_all_my_site () { return l_nodes_person(plc_my_person_id()) . "&active_line_tab=All My site nodes"; }
87 function l_nodes_person ($person_id) { return "/db/nodes/index.php?person_id=$person_id"; }
88 function l_nodes_slice ($slice_id) { return "/db/nodes/index.php?slice_id=$slice_id"; }
90 function l_interface ($interface_id) { return "/db/nodes/interface.php?id=$interface_id"; }
91 function l_interface_tags($interface_id){ return "/db/nodes/interface.php?id=$interface_id&show_details=0&show_tags=1"; }
92 function l_interface_t ($interface_id,$text) {
93 return href (l_interface($interface_id),$text); }
94 function l_interface_add($node_id) { return "/db/nodes/interface.php?node_id=$node_id"; }
96 function l_sites () { return "/db/sites/index.php"; }
97 function l_sites_peer ($peer_id) { return "/db/sites/index.php?peerscope=$peer_id"; }
98 function l_site ($site_id) { return "/db/sites/index.php?id=$site_id"; }
99 function l_site_t ($site_id,$text) { return href (l_site($site_id),$text); }
100 function l_site_obj($site) { return href (l_site($site['site_id']),$site['name']); }
102 function l_slices () { return "/db/slices/index.php"; }
103 function l_slices_peer ($peer_id) { return "/db/slices/index.php?peerscope=$peer_id"; }
104 function l_slice ($slice_id) { return "/db/slices/index.php?id=$slice_id"; }
105 function l_slice_nodes ($slice_id) { return "/db/slices/index.php?id=$slice_id&show_details=0&show_nodes=1&show_nodes_current=1&show_nodes_add=1"; }
106 function l_slice_t ($slice_id,$text) { return href (l_slice($slice_id),$text); }
107 function l_slice_add () { return "/db/slices/slice_add.php"; }
108 function l_slices_site($site_id) { return "/db/slices/index.php?site_id=$site_id"; }
109 function l_slices_my_site() { return l_slices_site(plc_my_site_id()) . "&active_line_tab=My site slices"; }
110 function l_slices_person($person_id) { return "/db/slices/index.php?person_id=$person_id"; }
111 function l_slices_local() { return "/db/slices/index.php?peerscope=local"; }
113 function l_slice_obj ($slice) { return l_slice_t ($slice['slice_id'],$slice['name']); }
115 function l_sliver ($node_id,$slice_id) { return "/db/nodes/slivers.php?node_id=$node_id&slice_id=$slice_id"; }
116 function l_sliver_t ($node_id,$slice_id,$text) {
117 return href (l_sliver($node_id,$slice_id),$text) ; }
119 function l_persons () { return "/db/persons/index.php&active_line_tab=All Accounts"; }
120 function l_persons_peer ($peer_id) { return "/db/persons/index.php?peerscope=$peer_id&active_line_tab=Local Accounts"; }
121 function l_person ($person_id) { return "/db/persons/index.php?id=$person_id"; }
122 function l_person_roles ($person_id) { return "/db/persons/index.php?id=$person_id&show_details=0&show_roles=1"; }
123 function l_person_t ($person_id,$text) { return href (l_person($person_id),$text); }
124 function l_persons_site ($site_id) { return "/db/persons/index.php?site_id=$site_id"; }
125 function l_persons_slice ($slice_id) { return "/db/persons/index.php?slice_id=$slice_id"; }
126 function l_person_obj ($person) { return l_person_t($person['person_id'],$person['email']); }
128 function l_tags () { return "/db/tags/index.php"; }
129 function l_tag ($tag_type_id) { return "/db/tags/index.php?id=$tag_type_id"; }
130 function l_tag_obj ($tag) { return href(l_tag($tag['tag_type_id']),$tag['tagname']); }
132 function l_nodegroups () { return "/db/tags/nodegroups.php"; }
133 function l_nodegroup ($nodegroup_id) { return "/db/tags/nodegroup.php?id=$nodegroup_id"; }
134 function l_nodegroup_t ($nodegroup_id,$text) {
135 return href(l_nodegroup($nodegroup_id),$text); }
136 function l_nodegroup_obj ($nodegroup) {
137 return href(l_nodegroup($nodegroup['nodegroup_id']),$nodegroup['groupname']); }
139 function l_events () { return "/db/events/index.php"; }
140 function l_event ($type,$param,$id) { return "/db/events/index.php?type=$type&$param=$id"; }
142 function l_peers() { return "/db/peers/index.php"; }
143 function l_peer($peer_id) { return "/db/peers/index.php?id=$peer_id"; }
144 function l_peer_t($peer_id,$text) { return href(l_peer($peer_id),$text); }
146 function l_comon($id_name,$id_value) { return "/db/nodes/comon.php?$id_name=$id_value"; }
147 function l_sirius() { return "/db/sirius/index.php"; }
148 function l_about() { return "/db/common/about.php"; }
149 function l_doc_plcapi() { return "/db/doc/PLCAPI.php"; }
150 function l_doc_nmapi() { return "/db/doc/NMAPI.php"; }
151 function l_admin() { return "/db/common/adminsearch.php"; }
153 function l_login() { return "/db/common/login.php"; }
154 function l_logout() { return "/planetlab/common/logout.php"; }
155 function l_sulogout() { return "/planetlab/common/sulogout.php"; }
156 function l_reset_password() { return "/db/persons/reset_password.php"; }
157 function l_person_register() { return "/db/persons/register.php"; }
158 function l_site_register() { return "/db/sites/register.php"; }
159 function l_sites_pending() { return "/db/sites/join_request.php"; }
160 function l_site_review_pending($site_id){ return "/db/sites/join_request.php?review=t&site_id=$site_id"; }
163 //////////////////////////////////////////////////////////// nav tabs
164 function tab_nodes () { return array ('label'=>'All nodes','url'=>l_nodes(),
165 'bubble'=>'Display nodes from all peers'); }
166 function tab_nodes_local () { return array ('label'=>'Local nodes', 'url'=>l_nodes_peer('local'),
167 'bubble'=>'Display all nodes local to this peer'); }
168 function tab_nodes_site($site_id){ return array ('label'=>'Site nodes', 'url'=>l_nodes_site($site_id),
169 'bubble'=>'Display nodes on that site'); }
170 function tab_nodes_slice($slice_id){ return array ('label'=>'Slice nodes', 'url'=>l_nodes_slice($slice_id),
171 'bubble'=>'Display nodes for that slice'); }
172 function tab_nodes_mysite () { return array ('label'=>'My site nodes', 'url'=>l_nodes_my_site(),
173 'bubble'=>'Display nodes on my site'); }
174 function tab_nodes_all_mysite (){ return array ('label'=>'All My site nodes', 'url'=>l_nodes_all_my_site(),
175 'bubble'=>'Display nodes on all my sites'); }
176 function tab_node($node) { return array ('label'=>'Node '.$node['hostname'], 'url'=>l_node($node['node_id']),
177 'bubble'=>'Details for ' . $node['hostname']); }
179 function tab_site($site) { return array ('label'=>'Site '.$site['login_base'], 'url'=>l_site($site['site_id']),
180 'bubble'=>'Details for ' . $site['name']); }
181 function tab_mysite() { return array ('label'=>'My site', 'url'=>l_site(plc_my_site_id()),
182 'bubble'=>'Details for site ' . plc_my_site_id()); }
183 function tab_sites () { return array ('label'=>'All sites' , 'url'=>l_sites(), 'bubble'=> 'Display all sites'); }
184 function tab_sites_local () { return array ('label'=>'Local sites' , 'url'=>l_sites_peer('local'),
185 'bubble'=> 'Display all siteslocal to this peer'); }
187 function tab_slices() { return array ('label'=>'All slices', 'url'=>l_slices(),
188 'bubble' => 'Display all slices'); }
189 function tab_slices_person() { return array ('label'=>'My slices', 'url'=>l_slices_person(plc_my_person_id()),
190 'bubble' => 'Display my all slices'); }
191 function tab_slices_mysite () { return array ('label'=>'My site slices', 'url'=>l_slices_my_site(),
192 'bubble'=>'Display all slices on my site'); }
193 function tab_slices_local () { return array ('label'=>'Local slices', 'url'=>l_slices_local(),
194 'bubble'=>'Display all slices local to this peer'); }
195 function tab_slice($slice) { return array ('label'=>'Slice '.$slice['name'], 'url'=>l_slice($slice['slice_id']),
196 'bubble' => 'Details for ' . $slice['name']); }
198 function tab_persons() { return array ('label'=>'All Accounts', 'url'=>l_persons(),
199 'bubble'=>'Display users from all peers'); }
200 function tab_persons_local() { return array ('label'=>'Local Accounts', 'url'=>l_persons_peer('local'),
201 'bubble'=>'Display all users local to this peer'); }
202 function tab_persons_mysite() { return array ('label'=>'My site accounts' , 'url'=>l_persons_site(plc_my_site_id()),
203 'bubble'=>'Display accounts on site ' . plc_my_site_id()); }
204 function tab_person($person) { return array ('label'=>'Account '.$person['email'], 'url'=>l_person($person['person_id']),
205 'bubble'=>'Details for ' . $person['email']); }
207 function tab_tags() { return array ('label'=>'Tag Types', 'url'=>l_tags(),
208 'bubble' => 'Display and create tag types'); }
209 function tab_nodegroups() { return array ('label'=>'Nodegroups', 'url'=>l_nodegroups(),
210 'bubble' => 'Display and create nodegroups'); }
213 function tablook_event() { return array('image'=>'/planetlab/icons/event.png','height'=>18);}
214 function tablook_comon() { return array('image'=>'/planetlab/icons/comon.png','height'=>18);}
218 //////////////////////////////////////////////////////////// validation functions
219 function topdomain ($hostname) {
220 $exploded=array_reverse(explode(".",$hostname));
224 function is_valid_email_addr ($email) {
225 if (ereg("^.+@.+\\..+$", $email) ) {
232 function is_valid_url ($url) {
233 if (ereg("^(http|https)://.+\..+$", strtolower($url) ) ) {
240 function is_valid_ip ($ip) {
241 if (ereg("^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$", $ip ) ) {
242 // it's at least in the right format, now check to see if
243 // each part is equal to less than 255
244 $parts= explode( '.', $ip );
245 $count= count($parts);
247 for( $i= 0; $i < $count; $i++ ) {
248 if( intval($parts[$i]) > 255 )
258 function is_valid_network_addr($network_addr,$mask) {
259 $lNetwork= ip2long($network_addr);
260 $lMask= ip2long($mask);
262 // are they the correct format?
263 if( $lNetwork == -1 || $lMask == -1 )
266 // is network address valid for the mask?
267 if( ($lNetwork & $lMask) != $lNetwork )
274 // returns whether or not a network address is in the reserved space
275 // in the case of a invalid network address, false will be returned.
276 function is_reserved_network_addr($network_addr) {
277 $lNetwork= ip2long($network_addr);
279 if( $lNetwork == -1 )
282 // does the network address fall in a reserved block?
283 $reserved_ips = array (
284 array('10.0.0.0','10.255.255.255'),
285 array('172.16.0.0','172.31.0.0'),
286 array('192.168.0.0','192.168.255.0')
288 foreach ($reserved_ips as $r) {
289 $min = ip2long($r[0]);
290 $max = ip2long($r[1]);
292 if (($lNetwork >= $min) && ($lNetwork <= $max))
299 //////////////////////////////////////////////////////////// roles
300 function plc_role_global_hash ($api) {
302 $roles=$api->GetRoles();
303 foreach ($roles as $role) {
304 $hash[$role['role_id']]=$role['name'];
309 // because GetRoles does not correctly support filters, it's really painful to do this
310 function sort_roles ($r1, $r2) { return $r2['role_id'] - $r1['role_id']; }
311 function roles_except ($roles, $exception_ids) {
313 if ($roles) foreach ($roles as $role) {
314 if ( ! in_array ($role['role_id'],$exception_ids) ) {
318 usort($result,sort_roles);
322 //////////////////////////////////////////////////////////// nodegroups
323 // hash by 'tagname=value'
324 function plc_nodegroup_global_hash ($api,$tagnames=NULL) {
326 // xxx somehow this does not work; I've checked that the feature is working from plcsh
327 // but I suspect the php marshalling or something; no time to fix, get all nodegroups for now
328 // if ($tagnames) $filter=array("tagname"=>$tagnames);
329 $nodegroups=$api->GetNodeGroups($filter);
331 if ($nodegroups) foreach ($nodegroups as $nodegroup) {
332 $key=$nodegroup['tagname']."=".$nodegroup['value'];
333 $hash[$key]=$nodegroup;
338 //////////////////////////////////////////////////////////// titles
339 function t_site($site) { return " on site " . $site['name'] . " (" . $site['login_base'] .")"; }
340 function t_slice ($slice) { return " running slice " . $slice['name'] . " (" . $slice['slice_id'] . ")"; }
341 function t_person ($person) { return " belonging to " . $person['email'] . " (" . $person['person_id'] . ")"; }
343 //////////////////////////////////////////////////////////// html fragments
344 function plc_vertical_table ($messages, $class="") {
345 if ( empty( $messages) ) return "";
347 $formatted .= "<table";
348 if ($class) $formatted .= " class='" . $class . "'";
350 foreach ($messages as $message) {
351 $formatted .= "<tr><td>" . $message . "</td></tr>";
353 $formatted .= "</table>";
356 function plc_itemize ($messages, $class="") {
357 if ( empty( $messages) ) return "";
360 if ($class) $formatted .= " class='" . $class . "'";
362 foreach ($messages as $message) {
363 $formatted .= "<li>" . $message . "</li>";
365 $formatted .= "</ul>";
370 function truncate ($text,$numb,$etc = "...") {
371 if (strlen($text) > $numb) {
372 $text = substr($text, 0, $numb);
377 // generates <(atom) class=(class)> (text) </(atom)>
378 function html_atom ($atom,$text,$class="") {
380 if ($class) $html .= " class='$class'";
381 $html .= ">$text</$atom>";
384 function html_div ($text,$class="") { return html_atom ('div',$text,$class); }
385 function html_span ($text,$class="") { return html_atom ('span',$text,$class); }
387 // should use the same channel as the php errors..
388 function plc_error_html ($text) { return html_div ($text,'plc-error'); }
389 function plc_error ($text) { print plc_error_html ("Error " . $text); }
391 function errors_init() { return array();}
392 function errors_record ($adm, $errors) {
400 function errors_display ($errors) {
402 print( "<div class='plc-error'>" );
403 print( "<p>The following errors occured:</p>" );
405 foreach( $errors as $error )
406 print( "<li>$error</li>\n" );
407 print( "</ul></div>\n" );
411 function plc_warning_html ($text) { return html_span($text,'plc-warning'); }
412 function plc_warning ($text) { print plc_warning_html("Warning " . $text); }
414 function bold_html ($text) { return html_span($text,'bold'); }
416 // shows a php variable verbatim with a heading message
417 function plc_debug ($message,$object) {
418 print "<br>" . $message . "<pre>";
423 if (! function_exists ("drupal_set_error")) {
424 function drupal_set_error ($text) {
425 drupal_set_message ("<span class=error>$text</span>");
429 //////////////////////////////////////////////////////////// sort out for obsolete / trash
430 // builds a table from an array of strings, with the given class
431 // attempt to normalize the delete buttons and confirmations
432 function plc_delete_icon($width=15) {
433 return "<img width='$width' src='/planetlab/icons/delete.png'>";
436 function plc_add_icon($width=15) {
437 return "<img width='$width' src='/planetlab/icons/add.png'>";
440 function plc_bubble($text,$bubble) {
441 return "<span title='$bubble'>$text</span>";
443 function plc_delete_icon_bubble ($bubble,$width=15) {
444 return plc_bubble(plc_delete_icon($width),$bubble);
447 function plc_event_button($type,$param,$id) {
448 return '<a href="' . l_event($type,$param,$id) . '"> <span title="Related events"> <img src="/planetlab/icons/event.png" width=18></span></a>';
451 function plc_comon_button ($id_name, $id_value,$target="") {
453 if (!empty($target)) {
454 $result.='target="' . $target . '" ';
456 $result.='href="' . l_comon($id_name,$id_value) . '">';
457 $result.='<span title="Link to Comon"> <img src="/planetlab/icons/comon.png" width="18"></span></a>';
462 function plc_redirect ($url) {
463 header ("Location: " . $url);
467 //////////////////// the options for an interface - suitable for plekit/form
468 //>>> GetNetworkMethods()
469 //[u'static', u'dhcp', u'proxy', u'tap', u'ipmi', u'unknown']
470 function interface_method_selectors ($api, $method, $primary) {
472 $builtin_methods=array("static"=>"Static",
475 $builtin_methods=array("static"=>"Static",
482 foreach ($builtin_methods as $value=>$display) {
483 $selector=array('display'=>$display, 'value'=>$value);
484 if ($value == $method) $selector['selected']=true;
485 $selectors []= $selector;
490 // displays bandwidth with kbps Mbps Gbps as needed
491 function pretty_bandwidth ($bw) {
492 if ($bw < 1000) return $bw;
493 if ($bw < 1000000) return strval($bw/1000) . " kbps";
494 if ($bw < 1000000000) return strval($bw/1000000) . " Mbps";
495 else return strval($bw/1000000000) . " Gbps";
499 function instantiation_label ($slice) {
500 $instantiation_labels = array ('not-instantiated'=>'NOT',
501 'plc-instantiated'=>'PLC',
502 'delegated' => 'DEL',
503 'nm-controller' => 'NM');
504 $result=$instantiation_labels[$slice['instantiation']];
505 if (!$result) $result = $slice['instantiation'];
506 if (!$result) $result = '??';
510 //////////////////// toggle areas
511 // get_arg ('show_persons',false) returns $_GET['show_persons'] if set and false otherwise
512 function get_arg ($name,$default,$method='get') {
513 if ($method == 'get') $var=$_GET; else $var=$_POST;
514 if (isset ($var[$name])) return $var[$name];
515 else return $default;
518 //////////////////// number of ...
519 function count_english ($objs,$name) {
521 if ($count == 0) return 'No ' . $name;
522 else if ($count == 1) return 'One ' . $name;
523 else return $count . ' ' . $name . 's';
525 function count_english_warning ($objs, $name) {
526 $x=count_english ($objs,$name);
527 if (count ($objs) == 0) $x=plc_warning_html($x . ' !!');