5 // will trash this eventually
6 require_once 'plc_functions_trash.php';
8 //////////////////////////////////////////////////////////// roles & other checks on global $plc
9 function plc_is_admin () {
11 return in_array( 10, $plc->person['role_ids']);
13 function plc_is_pi () {
15 return in_array( 20, $plc->person['role_ids']);
17 function plc_is_tech () {
19 return in_array( 40, $plc->person['role_ids']);
21 function plc_in_site ($site_id) {
23 return in_array( $site_id, $plc->person['site_ids']);
26 function plc_my_site_id () {
28 return $plc->person['site_ids'][0];
31 function plc_my_person_id () {
33 return $plc->person['person_id'];
36 //////////////////////////////////////////////////////////// links
37 function href ($url,$text) { return "<a href='" . $url . "'>" . $text . "</a>"; }
40 // l_objects() -> the url to the page that list objects
41 // l_object($object_id) -> the url to hte page thas details object with given id
42 // l_object_t($object_id,text) -> an <a> tag that shows text and links to the above
43 // l_object_add () -> the url to that object-afding page
45 function l_actions () { return "/db/actions.php"; }
47 function l_nodes () { return "/db/nodes/index.php"; }
48 function l_nodes_local () { return "/db/nodes/index.php?peerscope=local"; }
49 function l_node ($node_id) { return "/db/nodes/node.php?id=$node_id"; }
50 function l_node_t ($node_id,$text) { return href (l_node($node_id),$text); }
51 function l_node_add () { return "/db/nodes/node_add.php"; }
52 function l_nodes_site ($site_id) { return "/db/nodes/index.php?site_id=$site_id"; }
54 function l_interface ($interface_id) { return "/db/nodes/interfaces.php?id=$interface_id"; }
55 function l_interface_t ($interface_id,$text) {
56 return href (l_interface($interface_id),$text); }
57 function l_interface_add($node_id) { return "/db/nodes/interfaces.php?node_id=$node_id"; }
59 function l_sites () { return "/db/sites/index.php"; }
60 function l_sites_local () { return "/db/sites/index.php?peerscope=local"; }
61 function l_site ($site_id) { return "/db/sites/index.php?id=$site_id"; }
62 function l_site_t ($site_id,$text) { return href (l_site($site_id),$text); }
63 function l_site_update($site_id) { return "/xxx-undefined"; }
65 function l_slices () { return "/db/slices/index.php"; }
66 function l_slice ($slice_id) { return "/db/slices/index.php?id=$slice_id"; }
67 function l_slice_t ($slice_id,$text) { return href (l_slice($slice_id),$text); }
68 function l_slice_add () { return "/db/slices/add_slice.php"; }
69 function l_slices_site($site_id) { return "/db/slices/index.php?site_id=$site_id"; }
71 function l_slice_text ($slice) { return l_slice_t ($slice['slice_id'],$slice['name']); }
73 function l_sliver ($node_id,$slice_id) { return "/db/nodes/slivers.php?node_id=$node_id&slice_id=$slice_id"; }
74 function l_sliver_t ($node_id,$slice_id,$text) {
75 return href (l_sliver($node_id,$slice_id),$text) ; }
77 function l_persons () { return "/db/persons/index.php"; }
78 function l_person ($person_id) { return "/db/persons/index.php?id=$person_id"; }
79 function l_person_t ($person_id,$text) { return href (l_person($person_id),$text); }
80 function l_persons_site ($site_id) { return "/db/persons/index.php?site_id=$site_id"; }
82 function l_tags () { return "/db/tags/index.php"; }
83 function l_tag ($tag_type_id) { return "/db/tags/index.php"; }
84 function l_tag_add() { return "/db/tags/tag_form.php"; }
85 function l_tag_update($id) { return "/db/tags/tag_form.php&action=update-tag-type&id=$id"; }
87 function l_nodegroups () { return "/db/tags/nodegroups.php"; }
88 function l_nodegroup ($nodegroup_id) { return "/db/tags/nodegroups.php?id=$nodegroup_id"; }
89 function l_nodegroup_t ($nodegroup_id,$text) {
90 return href(l_nodegroup($nodegroup_id),$text); }
92 function l_events () { return "/db/events/index.php"; }
93 function l_event ($type,$param,$id) { return "/db/events/index.php?type=$type&$param=$id"; }
95 function l_peers() { return "/db/peers/index.php"; }
96 function l_peer($peer_id) { return "/db/peers/index.php?id=$peer_id"; }
98 function l_comon($id_name,$id_value) { return "/db/nodes/comon.php?$id_name=$id_value"; }
99 function l_sirius() { return "/db/sirius/index.php"; }
100 function l_about() { return "/db/about.php"; }
101 function l_doc_plcapi() { return "/db/doc/PLCAPI.php"; }
102 function l_doc_nmapi() { return "/db/doc/NMAPI.php"; }
103 function l_admin() { return "/db/adminsearch.php"; }
105 function l_logout() { return "/planetlab/logout.php"; }
106 function l_sulogout() { return "/planetlab/sulogout.php"; }
107 function l_reset_password() { return "/db/persons/reset_password.php"; }
108 function l_person_register() { return "/db/persons/register.php"; }
109 function l_site_register() { return "/db/sites/register.php"; }
110 function l_sites_pending() { return "/db/sites/join_request.php"; }
112 // returns array ['url' => path, 'values' => hash (key=>value)* ]
113 function split_url ($full_url) {
114 list($url,$args) = explode("?",$full_url);
117 $pairs=explode("&",$args);
118 foreach ($pairs as $pair) {
119 list ($name,$value) = explode("=",$pair);
120 $values[$name]=$value;
123 return array("url"=>$url,"values"=>$values);
126 //////////////////////////////////////////////////////////// validation functions
127 function topdomain ($hostname) {
128 $exploded=array_reverse(explode(".",$hostname));
132 function is_valid_email_addr ($email) {
133 if (ereg("^.+@.+\\..+$", $email) ) {
140 function is_valid_url ($url) {
141 if (ereg("^(http|https)://.+\..+$", strtolower($url) ) ) {
148 function is_valid_ip ($ip) {
149 if (ereg("^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$", $ip ) ) {
150 // it's at least in the right format, now check to see if
151 // each part is equal to less than 255
152 $parts= explode( '.', $ip );
153 $count= count($parts);
155 for( $i= 0; $i < $count; $i++ ) {
156 if( intval($parts[$i]) > 255 )
166 function is_valid_network_addr($network_addr,$mask) {
167 $lNetwork= ip2long($network_addr);
168 $lMask= ip2long($mask);
170 // are they the correct format?
171 if( $lNetwork == -1 || $lMask == -1 )
174 // is network address valid for the mask?
175 if( ($lNetwork & $lMask) != $lNetwork )
182 // returns whether or not a network address is in the reserved space
183 // in the case of a invalid network address, false will be returned.
184 function is_reserved_network_addr($network_addr) {
185 $lNetwork= ip2long($network_addr);
187 if( $lNetwork == -1 )
190 // does the network address fall in a reserved block?
191 $reserved_ips = array (
192 array('10.0.0.0','10.255.255.255'),
193 array('172.16.0.0','172.31.0.0'),
194 array('192.168.0.0','192.168.255.0')
196 foreach ($reserved_ips as $r) {
197 $min = ip2long($r[0]);
198 $max = ip2long($r[1]);
200 if (($lNetwork >= $min) && ($lNetwork <= $max))
207 //////////////////////////////////////////////////////////// peer & peerscopes
208 function plc_role_global_hash ($api) {
210 $roles=$api->GetRoles();
211 foreach ($roles as $role) {
212 $hash[$role['role_id']]=$role['name'];
217 //////////////////////////////////////////////////////////// peer & peerscopes
218 // when shortnames are needed on peers
219 function plc_peer_global_hash ($api) {
220 $peer_columns=array('peer_id','shortname');
221 $peer_filter=array();
222 $peers = $api->GetPeers($peer_filter,$peer_columns);
225 foreach ($peers as $peer) {
226 $peer_hash[$peer['peer_id']]=$peer;
230 function plc_peer_shortname ($peer_hash,$peer_id) {
232 return PLC_SHORTNAME;
234 return $peer_hash[$node['peer_id']]['shortname'];
238 function plc_peer_label ($peer) {
240 return "Local object on " . PLC_NAME . " (" . PLC_SHORTNAME . ")";
242 $result= $peer['peername'] . " (" . $peer['shortname'] . ")";
243 $result= href(l_peer($peer['peer_id']),$result);
244 return plc_foreign_text($result);
247 // to set the background to grey on foreign objects
248 // return true if the peer is local
249 function plc_peer_block_start ($peer_hash,$peer_id) {
254 // set two classes, one eneraic to all foreign, and one based on the peer's shortname for finer grain tuning
255 printf ('<div class="plc-foreign plc-%s>"',strtolower(plc_peer_shortname($peer_hash,$peer_id)));
260 function plc_peer_block_end () {
264 // interpret standard syntax for peerscope
265 function plc_peer_info ($api,$peerscope) {
266 switch ($peerscope) {
268 $peer_filter=array();
269 $peer_label="all peers";
272 $peer_filter=array("peer_id"=>NULL);
273 $peer_label=PLC_SHORTNAME;
276 $peer_filter=array("~peer_id"=>NULL);
277 $peer_label="foreign peers";
280 if (is_int ($peerscope)) {
281 $peer_id=intval($peerscope);
282 $peers=$api->GetPeers(array("peer_id"=>$peer_id));
284 $peers=$api->GetPeers(array("shortname"=>$peerscope));
288 $peer_id=$peer['peer_id'];
289 $peer_filter=array("peer_id"=>$peer_id);
290 $peer_label='peer "' . $peer['shortname'] . '"';
292 $peer_filter=array();
293 $peer_label="[no such peer " . $peerscope . "]";
297 return array ($peer_filter,$peer_label);
300 //////////////////////////////////////////////////////////// nodegroups
301 // hash by 'tagname=value'
302 function plc_nodegroup_global_hash ($api,$tagnames=NULL) {
304 // xxx somehow this does not work; I've checked that the feature is working from plcsh
305 // but I suspect the php marshalling or something; no time to fix, get all nodegroups for now
306 // if ($tagnames) $filter=array("tagname"=>$tagnames);
307 $nodegroups=$api->GetNodeGroups($filter);
309 if ($nodegroups) foreach ($nodegroups as $nodegroup) {
310 $key=$nodegroup['tagname']."=".$nodegroup['value'];
311 $hash[$key]=$nodegroup;
316 //////////////////////////////////////////////////////////// titles
317 function t_site($site) { return " on site " . $site['name'] . " (" . $site['login_base'] .")"; }
318 function t_slice ($slice) { return " running slice " . $slice['name'] . " (" . $slice['slice_id'] . ")"; }
320 //////////////////////////////////////////////////////////// nav tabs
321 function tabs_node($node) { return array('Node ' . $node['hostname']=>l_node($node_id)); }
322 function tabs_site($site) { return array('Site ' . $site['name']=>l_site($site_id)); }
323 function tabs_slice($slice) { return array('Slice ' . $slice['name']=>l_slice($slice_id)); }
325 //////////////////////////////////////////////////////////// presentation
326 // builds a table from an array of strings, with the given class
327 // attempt to normalize the delete buttons and confirmations
328 function plc_delete_button($width=15) {
329 return '<span title="Delete this entry"><img width=' . $width . ' alt="Delete this entry" src="/planetlab/icons/delete.png"></span>';
332 function plc_js_confirm($message) {
333 return "onclick=\"javascript:return confirm('Are you sure you want to delete " . $message . " ?')\"";
336 function plc_delete_link($url,$delete_message,$visible) {
337 return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . $visible . "</a>";
340 function plc_delete_link_button($url,$delete_message,$width=15) {
341 return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . plc_delete_button($width) . "</a>";
344 function plc_event_button($type,$param,$id) {
345 return '<a href="' . l_event($type,$param,$id) . '"> <span title="Related events"> <img src="/planetlab/icons/event.png" width=18></span></a>';
348 function plc_comon_button ($id_name, $id_value,$target="") {
350 if (!empty($target)) {
351 $result.='target="' . $target . '" ';
353 $result.='href="' . l_comon($id_name,$id_value) . '">';
354 $result.='<span title="Link to Comon"> <img src="/planetlab/icons/comon.png" width="18"></span></a>';
358 function plc_vertical_table ($messages, $class="") {
359 // pretty print the cell
360 if ( empty( $messages) ) return "";
362 $formatted .= "<table";
363 if ($class) $formatted .= " class='" . $class . "'";
365 foreach ($messages as $message) {
366 $formatted .= "<tr><td>" . $message . "</td></tr>";
368 $formatted .= "</table>";
372 //////////////////////////////////////////////////////////// various mappers
373 // could not figure how to use anonymous lambdas..
374 function get_site_id ($site) { return $site['site_id'];}
375 function get_tagname ($tag) { return $tag['tagname'];}
377 ////////////////////////////////////////////////////////////
378 function plc_section ($text,$line=true) {
379 if ($line) { print "<hr/>";}
380 print "<h2 class=plc> $text </h2>\n";
383 function plc_error ($text) {
384 // should use the same channel as the php errors..
385 print "<div class='plc-error'> Error " . $text . "</div>";
388 function plc_errors ($list) {
389 print( "<div class='plc-error'>" );
390 print( "<p style='font-weight:bold'>The following errors occured:</p>" );
392 foreach( $errors as $err ) {
393 print( "<li>$err</li>\n" );
395 print( "</ul></div>\n" );
398 function plc_warning_text ($text) { return "<div class='plc-warning'>" . $text . "</div>";}
399 function plc_warning ($text) { print plc_warning_text("Warning " . $text); }
400 function plc_foreign_text($text) { return "<div class=plc-foreign>$text</div>"; }
402 // shows a php variable verbatim with a heading message
403 function plc_debug ($message,$object) {
404 print "<br>" . $message . "<pre>";