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 //////////////////////////////////////////////////////////// peer & peerscopes
108 // when shortnames are needed on peers
109 function plc_peer_get_hash ($api) {
110 $peer_columns=array('peer_id','shortname');
111 $peer_filter=array();
112 $peers = $api->GetPeers($peer_filter,$peer_columns);
115 foreach ($peers as $peer) {
116 $peer_hash[$peer['peer_id']]=$peer;
120 function plc_peer_shortname ($peer_hash,$peer_id) {
122 return PLC_SHORTNAME;
124 return $peer_hash[$node['peer_id']]['shortname'];
128 // to set the background to grey on foreign objects
129 function plc_peer_block_start ($peer_hash,$peer_id) {
133 // set two classes, one eneraic to all foreign, and one based on the peer's shortname for finer grain tuning
134 printf ('<div class="plc-foreign plc-%s>"',strtolower(plc_peer_shortname($peer_hash,$peer_id)));
138 function plc_peer_block_end () {
142 //// standard peerscope syntax
143 function plc_peer_info ($api,$peerscope) {
144 switch ($_GET['peerscope']) {
146 $peer_filter=array();
147 $peer_label="all peers";
150 $peer_filter=array("peer_id"=>NULL);
151 $peer_label=PLC_SHORTNAME;
154 $peer_filter=array("~peer_id"=>NULL);
155 $peer_label="foreign peers";
158 $peer_id=intval($_GET['peerscope']);
159 $peer_filter=array("peer_id"=>$peer_id);
160 $peer=$api->GetPeers(array("peer_id"=>$peer_id));
161 $peer_label='peer "' . $peer[0]['peername'] . '"';
164 return array ($peer_filter,$peer_label);
167 //////////////////////////////////////////////////////////// links
168 function href ($url,$text) { return "<a href='" . $url . "'>" . $text . "</a>"; }
170 function l_nodes () { return "/db/nodes/newindex.php"; }
171 function l_node_u ($node_id) { return "/db/nodes/node.php?id=" . $node_id; }
172 function l_node ($node_id) { return href (l_node_u($node_id),$node_id); }
173 function l_node2 ($node_id,$text) { return href (l_node_u($node_id),$text); }
175 function l_interface_u ($interface_id) { return "/db/nodes/interfaces.php?id=" . $interface_id; }
176 function l_interface_add_u($node_id) { return "/db/nodes/interfaces.php?node_id=" . $node_id; }
177 function l_interface ($interface_id) { return href (l_interface_u($interface_id),$interface_id); }
178 function l_interface2 ($interface_id,$text) { return href (l_interface_u($interface_id),$text); }
180 function l_nodegroup_u ($nodegroup_id) { return "/db/nodes/node_groups.php?id=" . $nodegroup_id; }
181 function l_nodegroup2 ($nodegroup_id,$text) { return href(l_nodegroup_u($nodegroup_id),$text); }
183 function l_sites () { return "/db/sites/index.php"; }
184 function l_site_u ($site_id) { return "/db/persons/index.php?id=" . $site_id; }
185 function l_site ($site_id) { return href (l_site_u($site_id),$site_id); }
186 function l_site2 ($site_id,$text) { return href (l_site_u($site_id),$text); }
188 function l_slices () { return "/db/slices/index.php"; }
189 function l_slice_u ($slice_id) { return "/db/slices/index.php?id=" . $slice_id; }
190 function l_slice ($slice_id) { return href (l_slice_u($slice_id),$slice_id); }
191 function l_slice2 ($slice_id,$text) { return href (l_slice_u($slice_id),$text); }
193 function l_sliver_u ($node_id,$slice_id) { return "/db/nodes/slivers.php?node_id=" . $node_id. "&slice_id=" . $slice_id; }
194 function l_sliver3 ($node_id,$slice_id,$text) { return href (l_sliver_u($node_id,$slice_id),$text) ; }
196 function l_persons () { return "/db/persons/index.php"; }
197 function l_person_u ($person_id) { return "/db/persons/index.php?id=" . $person_id; }
198 function l_person ($person_id) { return href (l_person_u($person_id),$person_id); }
199 function l_person2 ($person_id,$text) { return href (l_person_u($person_id),$text); }
201 function l_event ($type,$param,$id) { return '/db/events/index.php?type=' . $type . '&' . $param . '=' . $id; }
202 function l_comon($id_name,$id_value) { return '/db/nodes/comon.php?' . $id_name . "=" . $id_value; }
204 //////////////////////////////////////////////////////////// titles
205 function t_site($site) { return " on site " . $site['name'] . " (" . $site['login_base'] .")"; }
206 function t_slice ($slice) { return " running slice " . $slice['name'] . " (" . $slice['slice_id'] . ")"; }
208 //////////////////////////////////////////////////////////// nav tabs
209 function tabs_node($node) { return array('Node ' . $node['hostname']=>l_node_u($node_id)); }
210 function tabs_site($site) { return array('Site ' . $site['name']=>l_site_u($site_id)); }
211 function tabs_slice($slice) { return array('Slice ' . $slice['name']=>l_slice_u($slice_id)); }
213 //////////////////////////////////////////////////////////// presentation
214 // builds a table from an array of strings, with the given class
215 // attempt to normalize the delete buttons and confirmations
216 function plc_delete_button($width=15) {
217 return '<span title="Delete this entry"><img width=' . $width . ' alt="Delete this entry" src="/planetlab/icons/delete.png"></span>';
220 function plc_js_confirm($message) {
221 return "onclick=\"javascript:return confirm('Are you sure you want to delete " . $message . " ?')\"";
224 function plc_delete_link($url,$delete_message,$visible) {
225 return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . $visible . "</a>";
228 function plc_delete_link_button($url,$delete_message,$width=15) {
229 return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . plc_delete_button($width) . "</a>";
232 function plc_event_button($type,$param,$id) {
233 return '<a href="' . l_event($type,$param,$id) . '"> <span title="Related events"> <img src="/planetlab/icons/event.png" width=18></span></a>';
236 function plc_comon_button ($id_name, $id_value,$target="") {
238 if (!empty($target)) {
239 $result.='target="' . $target . '" ';
241 $result.='href="' . l_comon($id_name,$id_value) . '">';
242 $result.='<span title="Link to Comon"> <img src="/planetlab/icons/comon.png" width="18"></span></a>';
246 function plc_make_table ($class, $messages) {
247 // pretty print the cell
249 if (! empty ($messages)) {
250 $formatted="<table class='" . $class . "'>";
251 foreach ($messages as $message) {
252 $formatted .= "<tr><td>" . $message . "</td></tr>";
254 $formatted .= "</table>";
259 ////////////////////////////////////////////////////////////
260 function plc_error ($text) {
261 // should use the same channel as the php errors..
262 print "<div class='plc-warning'> Error " . $text . "</div>";
265 function plc_warning ($text) {
266 print "<div class='plc-warning'> Warning " . $text . "</div>";
269 // shows a php variable verbatim with a heading message
270 function plc_debug ($message,$object) {
271 print "<br>" . $message . "<pre>";