5 // will trash this eventually
6 //require_once 'plc_functions_trash.php';
9 function my_is_int ($x) {
10 return (is_numeric($x) ? intval($x) == $x : false);
13 //////////////////////////////////////////////////////////// roles & other checks on global $plc
14 function plc_is_admin () {
16 return in_array( 10, $plc->person['role_ids']);
18 function plc_is_pi () {
20 return in_array( 20, $plc->person['role_ids']);
22 function plc_is_user () {
24 return in_array( 30, $plc->person['role_ids']);
26 function plc_is_tech () {
28 return in_array( 40, $plc->person['role_ids']);
30 function plc_in_site ($site_id) {
32 return in_array( $site_id, $plc->person['site_ids']);
35 function plc_my_site_ids () {
37 return $plc->person['site_ids'];
39 function plc_my_sites () {
42 return $api->GetSites($plc->person['site_ids'], array('site_id', 'abbreviated_name'));
45 function plc_my_site_id () {
47 return $plc->person['site_ids'][0];
50 function plc_my_person_id () {
52 return $plc->person['person_id'];
55 //////////////////////////////////////////////////////////// links
56 function href ($url,$text) { return "<a href='" . $url . "'>" . $text . "</a>"; }
59 // l_objects() -> the url to the page that list objects
60 // l_object($object_id) -> the url to hte page thas details object with given id
61 // l_object_t($object_id,text) -> an <a> tag that shows text and links to the above
62 // l_object_add () -> the url to that object-afding page
64 function l_actions () { return "/db/common/actions.php"; }
65 // some complex node actions are kept separate, e.g. the ones related to getbootmedium
66 function l_actions_download () { return "/db/nodes/node_downloads.php"; }
67 function l_register_node () { return "/registerwizard/index.php"; }
68 function l_pcu_add () { return "/registerwizard/index.php/register/stage1_addpcu"; }
70 function l_nodes () { return "/db/nodes/index.php"; }
71 function l_nodes_peer ($peer_id) { return "/db/nodes/index.php?peerscope=$peer_id"; }
72 function l_node ($node_id) { return "/db/nodes/node.php?id=$node_id"; }
73 function l_node_t ($node_id,$text) { return href (l_node($node_id),$text); }
74 function l_node_obj($node) { return href(l_node($node['node_id']),$node['hostname']); }
75 function l_node_add () { return "/db/nodes/node_add.php"; }
76 function l_nodes_site ($site_id) { return "/db/nodes/index.php?site_id=$site_id"; }
77 function l_nodes_slice ($slice_id) { return "/db/nodes/index.php?slice_id=$slice_id"; }
79 function l_interface ($interface_id) { return "/db/nodes/interface.php?id=$interface_id"; }
80 function l_interface_tags($interface_id){ return "/db/nodes/interface.php?id=$interface_id&show_details=0&show_tags=1"; }
81 function l_interface_t ($interface_id,$text) {
82 return href (l_interface($interface_id),$text); }
83 function l_interface_add($node_id) { return "/db/nodes/interface.php?node_id=$node_id"; }
85 function l_sites () { return "/db/sites/index.php"; }
86 function l_sites_peer ($peer_id) { return "/db/sites/index.php?peerscope=$peer_id"; }
87 function l_site ($site_id) { return "/db/sites/index.php?id=$site_id"; }
88 function l_site_t ($site_id,$text) { return href (l_site($site_id),$text); }
89 function l_site_obj($site) { return href (l_site($site['site_id']),$site['name']); }
91 function l_slices () { return "/db/slices/index.php"; }
92 function l_slices_peer ($peer_id) { return "/db/slices/index.php?peerscope=$peer_id"; }
93 function l_slice ($slice_id) { return "/db/slices/index.php?id=$slice_id"; }
94 function l_slice_t ($slice_id,$text) { return href (l_slice($slice_id),$text); }
95 function l_slice_add () { return "/db/slices/slice_add.php"; }
96 function l_slices_site($site_id) { return "/db/slices/index.php?site_id=$site_id"; }
97 function l_slices_local() { return "/db/slices/index.php?peerscope=local"; }
99 function l_slice_obj ($slice) { return l_slice_t ($slice['slice_id'],$slice['name']); }
101 function l_sliver ($node_id,$slice_id) { return "/db/nodes/slivers.php?node_id=$node_id&slice_id=$slice_id"; }
102 function l_sliver_t ($node_id,$slice_id,$text) {
103 return href (l_sliver($node_id,$slice_id),$text) ; }
105 function l_persons () { return "/db/persons/index.php"; }
106 function l_persons_peer ($peer_id) { return "/db/persons/index.php?peerscope=$peer_id"; }
107 function l_person ($person_id) { return "/db/persons/index.php?id=$person_id"; }
108 function l_person_t ($person_id,$text) { return href (l_person($person_id),$text); }
109 function l_persons_site ($site_id) { return "/db/persons/index.php?site_id=$site_id"; }
110 function l_persons_slice ($slice_id) { return "/db/persons/index.php?slice_id=$slice_id"; }
111 function l_person_obj ($person) { return l_person_t($person['person_id'],$person['email']); }
113 function l_tags () { return "/db/tags/index.php"; }
114 function l_tag ($tag_type_id) { return "/db/tags/index.php?id=$tag_type_id"; }
115 function l_tag_obj ($tag) { return href(l_tag($tag['tag_type_id']),$tag['tagname']); }
117 function l_nodegroups () { return "/db/tags/nodegroups.php"; }
118 function l_nodegroup ($nodegroup_id) { return "/db/tags/nodegroup.php?id=$nodegroup_id"; }
119 function l_nodegroup_t ($nodegroup_id,$text) {
120 return href(l_nodegroup($nodegroup_id),$text); }
122 function l_events () { return "/db/events/index.php"; }
123 function l_event ($type,$param,$id) { return "/db/events/index.php?type=$type&$param=$id"; }
125 function l_peers() { return "/db/peers/index.php"; }
126 function l_peer($peer_id) { return "/db/peers/index.php?id=$peer_id"; }
127 function l_peer_t($peer_id,$text) { return href(l_peer($peer_id),$text); }
129 function l_comon($id_name,$id_value) { return "/db/nodes/comon.php?$id_name=$id_value"; }
130 function l_sirius() { return "/db/sirius/index.php"; }
131 function l_about() { return "/db/common/about.php"; }
132 function l_doc_plcapi() { return "/db/doc/PLCAPI.php"; }
133 function l_doc_nmapi() { return "/db/doc/NMAPI.php"; }
134 function l_admin() { return "/db/common/adminsearch.php"; }
136 function l_login() { return "/db/common/login.php"; }
137 function l_logout() { return "/planetlab/common/logout.php"; }
138 function l_sulogout() { return "/planetlab/common/sulogout.php"; }
139 function l_reset_password() { return "/db/persons/reset_password.php"; }
140 function l_person_register() { return "/db/persons/register.php"; }
141 function l_site_register() { return "/db/sites/register.php"; }
142 function l_sites_pending() { return "/db/sites/join_request.php"; }
145 //////////////////////////////////////////////////////////// nav tabs
146 function tab_nodes () { return array ('label'=>'All nodes','url'=>l_nodes(),
147 'bubble'=>'Display nodes from all peers'); }
148 function tab_nodes_local () { return array ('label'=>'Local nodes', 'url'=>l_nodes_peer('local'),
149 'bubble'=>'Display all nodes local to this peer'); }
150 function tab_nodes_site($site_id){ return array ('label'=>'Site nodes', 'url'=>l_nodes_site($site_id),
151 'bubble'=>'Display nodes on that site'); }
152 function tab_nodes_slice($slice_id){ return array ('label'=>'Slice nodes', 'url'=>l_nodes_slice($slice_id),
153 'bubble'=>'Display nodes for that slice'); }
154 function tab_nodes_mysite () { return array ('label'=>'My nodes', 'url'=>l_nodes_site(plc_my_site_id()),
155 'bubble'=>'Display nodes on my site'); }
156 function tab_node($node) { return array ('label'=>'Node '.$node['hostname'], 'url'=>l_node($node['node_id']),
157 'bubble'=>'Details for ' . $node['hostname']); }
159 function tab_site($site) { return array ('label'=>'Site '.$site['login_base'], 'url'=>l_site($site['site_id']),
160 'bubble'=>'Details for ' . $site['name']); }
161 function tab_mysite() { return array ('label'=>'My site', 'url'=>l_site(plc_my_site_id()),
162 'bubble'=>'Details for site ' . plc_my_site_id()); }
163 function tab_sites () { return array ('label'=>'All sites' , 'url'=>l_sites(), 'bubble'=> 'Display all sites'); }
164 function tab_sites_local () { return array ('label'=>'Local sites' , 'url'=>l_sites_peer('local'),
165 'bubble'=> 'Display all siteslocal to this peer'); }
167 function tab_slices() { return array ('label'=>'All slices', 'url'=>l_slices(),
168 'bubble' => 'Display all slices'); }
169 function tab_slices_mysite () { return array ('label'=>'My site slices', 'url'=>l_slices_site(plc_my_site_id()),
170 'bubble'=>'Display all slices on my site'); }
171 function tab_slices_mine () { return array ('label'=>'My site slices', 'url'=>l_slices_site(plc_my_site_id()),
172 'bubble'=>'Display all slices on my site'); }
173 function tab_slices_local () { return array ('label'=>'Local slices', 'url'=>l_slices_local(),
174 'bubble'=>'Display all slices local to this peer'); }
175 function tab_slice($slice) { return array ('label'=>'Slice '.$slice['name'], 'url'=>l_slice($slice['slice_id']),
176 'bubble' => 'Details for ' . $slice['name']); }
178 function tab_persons() { return array ('label'=>'All accounts', 'url'=>l_persons(),
179 'bubble'=>'Display users from all peers'); }
180 function tab_persons_local() { return array ('label'=>'Local accounts', 'url'=>l_persons_peer('local'),
181 'bubble'=>'Display all users local to this peer'); }
182 function tab_persons_mysite() { return array ('label'=>'My accounts' , 'url'=>l_persons_site(plc_my_site_id()),
183 'bubble'=>'Display accounts on site ' . plc_my_site_id()); }
184 function tab_person($person) { return array ('label'=>'Account '.$person['email'], 'url'=>l_person($person['person_id']),
185 'bubble'=>'Details for ' . $person['email']); }
187 function tab_tags() { return array ('label'=>'Tag Types', 'url'=>l_tags(),
188 'bubble' => 'Display and create tag types'); }
189 function tab_nodegroups() { return array ('label'=>'Nodegroups', 'url'=>l_nodegroups(),
190 'bubble' => 'Display and create nodegroups'); }
193 function tablook_event() { return array('image'=>'/planetlab/icons/event.png','height'=>18);}
194 function tablook_comon() { return array('image'=>'/planetlab/icons/comon.png','height'=>18);}
198 //////////////////////////////////////////////////////////// validation functions
199 function topdomain ($hostname) {
200 $exploded=array_reverse(explode(".",$hostname));
204 function is_valid_email_addr ($email) {
205 if (ereg("^.+@.+\\..+$", $email) ) {
212 function is_valid_url ($url) {
213 if (ereg("^(http|https)://.+\..+$", strtolower($url) ) ) {
220 function is_valid_ip ($ip) {
221 if (ereg("^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$", $ip ) ) {
222 // it's at least in the right format, now check to see if
223 // each part is equal to less than 255
224 $parts= explode( '.', $ip );
225 $count= count($parts);
227 for( $i= 0; $i < $count; $i++ ) {
228 if( intval($parts[$i]) > 255 )
238 function is_valid_network_addr($network_addr,$mask) {
239 $lNetwork= ip2long($network_addr);
240 $lMask= ip2long($mask);
242 // are they the correct format?
243 if( $lNetwork == -1 || $lMask == -1 )
246 // is network address valid for the mask?
247 if( ($lNetwork & $lMask) != $lNetwork )
254 // returns whether or not a network address is in the reserved space
255 // in the case of a invalid network address, false will be returned.
256 function is_reserved_network_addr($network_addr) {
257 $lNetwork= ip2long($network_addr);
259 if( $lNetwork == -1 )
262 // does the network address fall in a reserved block?
263 $reserved_ips = array (
264 array('10.0.0.0','10.255.255.255'),
265 array('172.16.0.0','172.31.0.0'),
266 array('192.168.0.0','192.168.255.0')
268 foreach ($reserved_ips as $r) {
269 $min = ip2long($r[0]);
270 $max = ip2long($r[1]);
272 if (($lNetwork >= $min) && ($lNetwork <= $max))
279 //////////////////////////////////////////////////////////// roles
280 function plc_role_global_hash ($api) {
282 $roles=$api->GetRoles();
283 foreach ($roles as $role) {
284 $hash[$role['role_id']]=$role['name'];
289 //////////////////////////////////////////////////////////// nodegroups
290 // hash by 'tagname=value'
291 function plc_nodegroup_global_hash ($api,$tagnames=NULL) {
293 // xxx somehow this does not work; I've checked that the feature is working from plcsh
294 // but I suspect the php marshalling or something; no time to fix, get all nodegroups for now
295 // if ($tagnames) $filter=array("tagname"=>$tagnames);
296 $nodegroups=$api->GetNodeGroups($filter);
298 if ($nodegroups) foreach ($nodegroups as $nodegroup) {
299 $key=$nodegroup['tagname']."=".$nodegroup['value'];
300 $hash[$key]=$nodegroup;
305 //////////////////////////////////////////////////////////// titles
306 function t_site($site) { return " on site " . $site['name'] . " (" . $site['login_base'] .")"; }
307 function t_slice ($slice) { return " running slice " . $slice['name'] . " (" . $slice['slice_id'] . ")"; }
308 function t_person ($person) { return " belonging to " . $person['email'] . " (" . $person['person_id'] . ")"; }
310 //////////////////////////////////////////////////////////// html fragments
311 function plc_vertical_table ($messages, $class="") {
312 if ( empty( $messages) ) return "";
314 $formatted .= "<table";
315 if ($class) $formatted .= " class='" . $class . "'";
317 foreach ($messages as $message) {
318 $formatted .= "<tr><td>" . $message . "</td></tr>";
320 $formatted .= "</table>";
323 function plc_itemize ($messages, $class="") {
324 if ( empty( $messages) ) return "";
327 if ($class) $formatted .= " class='" . $class . "'";
329 foreach ($messages as $message) {
330 $formatted .= "<li>" . $message . "</li>";
332 $formatted .= "</ul>";
337 // should use the same channel as the php errors..
338 function plc_error_html ($text) { return "<div class='plc-error'> " . $text . "</div>"; }
339 function plc_error ($text) { print plc_error_html ("Error " . $text); }
341 function plc_errors ($errors) {
343 print( "<div class='plc-error'>" );
344 print( "<p>The following errors occured:</p>" );
346 foreach( $errors as $error )
347 print( "<li>$error</li>\n" );
348 print( "</ul></div>\n" );
352 function plc_warning_html ($text) { return "<span class='plc-warning'>" . $text . "</span>";}
353 function plc_warning ($text) { print plc_warning_html("Warning " . $text); }
355 function bold_html ($text) { return "<span class='bold'>$text</span>"; }
357 // shows a php variable verbatim with a heading message
358 function plc_debug ($message,$object) {
359 print "<br>" . $message . "<pre>";
364 function truncate ($text,$numb,$etc = "...") {
365 if (strlen($text) > $numb) {
366 $text = substr($text, 0, $numb);
371 function html_div ($text,$class="") {
373 if ($class) $html .= " class='$class'";
374 $html .= ">$text</div>";
378 if (! function_exists ("drupal_set_error")) {
379 function drupal_set_error ($text) {
380 drupal_set_message ("<span class=error>$text</span>");
384 //////////////////////////////////////////////////////////// sort out for obsolete / trash
385 // builds a table from an array of strings, with the given class
386 // attempt to normalize the delete buttons and confirmations
387 function plc_delete_icon($width=15) {
388 return "<img width='$width' src='/planetlab/icons/delete.png'>";
391 function plc_add_icon($width=15) {
392 return "<img width='$width' src='/planetlab/icons/add.png'>";
395 function plc_bubble($text,$bubble) {
396 return "<span title='$bubble'>$text</span>";
398 function plc_delete_icon_bubble ($bubble,$width=15) {
399 return plc_bubble(plc_delete_icon($width),$bubble);
402 function plc_event_button($type,$param,$id) {
403 return '<a href="' . l_event($type,$param,$id) . '"> <span title="Related events"> <img src="/planetlab/icons/event.png" width=18></span></a>';
406 function plc_comon_button ($id_name, $id_value,$target="") {
408 if (!empty($target)) {
409 $result.='target="' . $target . '" ';
411 $result.='href="' . l_comon($id_name,$id_value) . '">';
412 $result.='<span title="Link to Comon"> <img src="/planetlab/icons/comon.png" width="18"></span></a>';
417 function plc_redirect ($url) {
418 header ("Location: " . $url);
422 //////////////////// the options for an interface - suitable for plekit/form
423 //>>> GetNetworkMethods()
424 //[u'static', u'dhcp', u'proxy', u'tap', u'ipmi', u'unknown']
425 function interface_method_selectors ($api, $method, $primary) {
427 $builtin_methods=array("static"=>"Static",
430 $builtin_methods=array("static"=>"Static",
437 foreach ($builtin_methods as $value=>$display) {
438 $selector=array('display'=>$display, 'value'=>$value);
439 if ($value == $method) $selector['selected']=true;
440 $selectors []= $selector;
446 function instantiation_label ($slice) {
447 $instantiation_labels = array ('not-instantiated'=>'NOT',
448 'plc-instantiated'=>'PLC',
449 'delegated' => 'DEL',
450 'nm-controller' => 'NM');
451 $result=$instantiation_labels[$slice['instantiation']];
452 if (!$result) $result = $slice['instantiation'];
453 if (!$result) $result = '??';
457 //////////////////// toggle areas
458 // get_arg ('show_persons',false) returns $_GET['show_persons'] if set and false otherwise
459 function get_arg ($name,$default,$method='get') {
460 if ($method == 'get') $var=$_GET; else $var=$_POST;
461 if (isset ($var[$name])) return $var[$name];
462 else return $default;
465 //////////////////// number of ...
466 function count_english ($objs,$name) {
468 if ($count == 0) return 'No ' . $name;
469 else if ($count == 1) return 'One ' . $name;
470 else return $count . ' ' . $name . 's';
472 function count_english_warning ($objs, $name) {
473 $x=count_english ($objs,$name);
474 if (count ($objs) == 0) $x=plc_warning_html($x . ' !!');