5 // will trash this eventually
6 require_once 'plc_functions_trash.php';
8 //////////////////////////////////////////////////////////// validation functions
9 function topdomain ($hostname) {
10 $exploded=array_reverse(explode(".",$hostname));
14 function is_valid_email_addr ($email) {
15 if (ereg("^.+@.+\\..+$", $email) ) {
22 function is_valid_url ($url) {
23 if (ereg("^(http|https)://.+\..+$", strtolower($url) ) ) {
30 function is_valid_ip ($ip) {
31 if (ereg("^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$", $ip ) ) {
32 // it's at least in the right format, now check to see if
33 // each part is equal to less than 255
34 $parts= explode( '.', $ip );
35 $count= count($parts);
37 for( $i= 0; $i < $count; $i++ ) {
38 if( intval($parts[$i]) > 255 )
48 function is_valid_network_addr($network_addr,$mask) {
49 $lNetwork= ip2long($network_addr);
50 $lMask= ip2long($mask);
52 // are they the correct format?
53 if( $lNetwork == -1 || $lMask == -1 )
56 // is network address valid for the mask?
57 if( ($lNetwork & $lMask) != $lNetwork )
64 // returns whether or not a network address is in the reserved space
65 // in the case of a invalid network address, false will be returned.
66 function is_reserved_network_addr($network_addr) {
67 $lNetwork= ip2long($network_addr);
72 // does the network address fall in a reserved block?
73 $reserved_ips = array (
74 array('10.0.0.0','10.255.255.255'),
75 array('172.16.0.0','172.31.0.0'),
76 array('192.168.0.0','192.168.255.0')
78 foreach ($reserved_ips as $r) {
79 $min = ip2long($r[0]);
80 $max = ip2long($r[1]);
82 if (($lNetwork >= $min) && ($lNetwork <= $max))
89 //////////////////////////////////////////////////////////// roles & other checks on global $plc
90 function plc_is_admin () {
92 return in_array( 10, $plc->person['role_ids']);
94 function plc_is_pi () {
96 return in_array( 20, $plc->person['role_ids']);
98 function plc_is_tech () {
100 return in_array( 40, $plc->person['role_ids']);
102 function plc_in_site ($site_id) {
104 return in_array( $site_id, $plc->person['site_ids']);
107 function plc_my_site_id () {
109 return $plc->person['site_ids'][0];
112 //////////////////////////////////////////////////////////// peer & peerscopes
113 // when shortnames are needed on peers
114 function plc_peer_get_hash ($api) {
115 $peer_columns=array('peer_id','shortname');
116 $peer_filter=array();
117 $peers = $api->GetPeers($peer_filter,$peer_columns);
120 foreach ($peers as $peer) {
121 $peer_hash[$peer['peer_id']]=$peer;
125 function plc_peer_shortname ($peer_hash,$peer_id) {
127 return PLC_SHORTNAME;
129 return $peer_hash[$node['peer_id']]['shortname'];
133 // to set the background to grey on foreign objects
134 function plc_peer_block_start ($peer_hash,$peer_id) {
138 // set two classes, one eneraic to all foreign, and one based on the peer's shortname for finer grain tuning
139 printf ('<div class="plc-foreign plc-%s>"',strtolower(plc_peer_shortname($peer_hash,$peer_id)));
143 function plc_peer_block_end () {
147 // interpret standard syntax for peerscope
148 function plc_peer_info ($api,$peerscope) {
149 switch ($peerscope) {
151 $peer_filter=array();
152 $peer_label="all peers";
155 $peer_filter=array("peer_id"=>NULL);
156 $peer_label=PLC_SHORTNAME;
159 $peer_filter=array("~peer_id"=>NULL);
160 $peer_label="foreign peers";
163 if (is_int ($peerscope)) {
164 $peer_id=intval($peerscope);
165 $peers=$api->GetPeers(array("peer_id"=>$peer_id));
167 $peers=$api->GetPeers(array("shortname"=>$peerscope));
171 $peer_id=$peer['peer_id'];
172 $peer_filter=array("peer_id"=>$peer_id);
173 $peer_label='peer "' . $peer['shortname'] . '"';
175 $peer_filter=array();
176 $peer_label="[no such peer " . $peerscope . "]";
180 return array ($peer_filter,$peer_label);
183 //////////////////////////////////////////////////////////// links
184 function href ($url,$text) { return "<a href='" . $url . "'>" . $text . "</a>"; }
186 function l_nodes () { return "/db/nodes/index.php"; }
187 function l_nodes_site ($site_id) { return "/db/nodes/index.php?site_id=" . $site_id; }
188 function l_node_u ($node_id) { return "/db/nodes/node.php?id=" . $node_id; }
189 function l_node ($node_id) { return href (l_node_u($node_id),$node_id); }
190 function l_node2 ($node_id,$text) { return href (l_node_u($node_id),$text); }
192 function l_interface_u ($interface_id) { return "/db/nodes/interfaces.php?id=" . $interface_id; }
193 function l_interface_add_u($node_id) { return "/db/nodes/interfaces.php?node_id=" . $node_id; }
194 function l_interface ($interface_id) { return href (l_interface_u($interface_id),$interface_id); }
195 function l_interface2 ($interface_id,$text) { return href (l_interface_u($interface_id),$text); }
197 function l_nodegroup_u ($nodegroup_id) { return "/db/nodes/node_groups.php?id=" . $nodegroup_id; }
198 function l_nodegroup2 ($nodegroup_id,$text) { return href(l_nodegroup_u($nodegroup_id),$text); }
200 function l_sites () { return "/db/sites/index.php"; }
201 function l_site_u ($site_id) { return "/db/persons/index.php?id=" . $site_id; }
202 function l_site ($site_id) { return href (l_site_u($site_id),$site_id); }
203 function l_site2 ($site_id,$text) { return href (l_site_u($site_id),$text); }
205 function l_slices () { return "/db/slices/index.php"; }
206 function l_slice_u ($slice_id) { return "/db/slices/index.php?id=" . $slice_id; }
207 function l_slice ($slice_id) { return href (l_slice_u($slice_id),$slice_id); }
208 function l_slice2 ($slice_id,$text) { return href (l_slice_u($slice_id),$text); }
210 function l_sliver_u ($node_id,$slice_id) { return "/db/nodes/slivers.php?node_id=" . $node_id. "&slice_id=" . $slice_id; }
211 function l_sliver3 ($node_id,$slice_id,$text) { return href (l_sliver_u($node_id,$slice_id),$text) ; }
213 function l_persons () { return "/db/persons/index.php"; }
214 function l_person_u ($person_id) { return "/db/persons/index.php?id=" . $person_id; }
215 function l_person ($person_id) { return href (l_person_u($person_id),$person_id); }
216 function l_person2 ($person_id,$text) { return href (l_person_u($person_id),$text); }
218 function l_event ($type,$param,$id) { return '/db/events/index.php?type=' . $type . '&' . $param . '=' . $id; }
219 function l_comon($id_name,$id_value) { return '/db/nodes/comon.php?' . $id_name . "=" . $id_value; }
221 function l_logout() { return "/planetlab/logout.php"; }
223 //////////////////////////////////////////////////////////// titles
224 function t_site($site) { return " on site " . $site['name'] . " (" . $site['login_base'] .")"; }
225 function t_slice ($slice) { return " running slice " . $slice['name'] . " (" . $slice['slice_id'] . ")"; }
227 //////////////////////////////////////////////////////////// nav tabs
228 function tabs_node($node) { return array('Node ' . $node['hostname']=>l_node_u($node_id)); }
229 function tabs_site($site) { return array('Site ' . $site['name']=>l_site_u($site_id)); }
230 function tabs_slice($slice) { return array('Slice ' . $slice['name']=>l_slice_u($slice_id)); }
232 //////////////////////////////////////////////////////////// presentation
233 // builds a table from an array of strings, with the given class
234 // attempt to normalize the delete buttons and confirmations
235 function plc_delete_button($width=15) {
236 return '<span title="Delete this entry"><img width=' . $width . ' alt="Delete this entry" src="/planetlab/icons/delete.png"></span>';
239 function plc_js_confirm($message) {
240 return "onclick=\"javascript:return confirm('Are you sure you want to delete " . $message . " ?')\"";
243 function plc_delete_link($url,$delete_message,$visible) {
244 return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . $visible . "</a>";
247 function plc_delete_link_button($url,$delete_message,$width=15) {
248 return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . plc_delete_button($width) . "</a>";
251 function plc_event_button($type,$param,$id) {
252 return '<a href="' . l_event($type,$param,$id) . '"> <span title="Related events"> <img src="/planetlab/icons/event.png" width=18></span></a>';
255 function plc_comon_button ($id_name, $id_value,$target="") {
257 if (!empty($target)) {
258 $result.='target="' . $target . '" ';
260 $result.='href="' . l_comon($id_name,$id_value) . '">';
261 $result.='<span title="Link to Comon"> <img src="/planetlab/icons/comon.png" width="18"></span></a>';
265 function plc_vertical_table ($messages, $class="") {
266 // pretty print the cell
267 if ( empty( $messages) ) return "";
269 $formatted .= "<table ";
270 if ($class) $formatted .= "class='" . $class . "'";
272 foreach ($messages as $message) {
273 $formatted .= "<tr><td>" . $message . "</td></tr>";
275 $formatted .= "</table>";
279 ////////////////////////////////////////////////////////////
280 function plc_error ($text) {
281 // should use the same channel as the php errors..
282 print "<div class='plc-error'> Error " . $text . "</div>";
285 function plc_errors ($list) {
286 print( "<div class='plc-error'>" );
287 print( "<p style='font-weight:bold'>The following errors occured:</p>" );
289 foreach( $errors as $err ) {
290 print( "<li>$err</li>\n" );
292 print( "</ul></div>\n" );
295 function plc_warning ($text) {
296 print "<div class='plc-warning'> Warning " . $text . "</div>";
299 // shows a php variable verbatim with a heading message
300 function plc_debug ($message,$object) {
301 print "<br>" . $message . "<pre>";