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_nodes () { return "/db/nodes/index.php"; }
46 function l_node ($node_id) { return "/db/nodes/node.php?id=" . $node_id; }
47 function l_node_t ($node_id,$text) { return href (l_node($node_id),$text); }
48 function l_node_add () { return "/db/nodes/node_add.php"; }
49 function l_nodes_site ($site_id) { return "/db/nodes/index.php?site_id=" . $site_id; }
51 function l_interface ($interface_id) { return "/db/nodes/interfaces.php?id=" . $interface_id; }
52 function l_interface_t ($interface_id,$text) {
53 return href (l_interface($interface_id),$text); }
54 function l_interface_add($node_id) { return "/db/nodes/interfaces.php?node_id=" . $node_id; }
56 function l_sites () { return "/db/sites/index.php"; }
57 function l_site ($site_id) { return "/db/sites/index.php?id=" . $site_id; }
58 function l_site_t ($site_id,$text) { return href (l_site($site_id),$text); }
60 function l_slices () { return "/db/slices/index.php"; }
61 function l_slice ($slice_id) { return "/db/slices/index.php?id=" . $slice_id; }
62 function l_slice_t ($slice_id,$text) { return href (l_slice($slice_id),$text); }
63 function l_slice_add () { return "/db/slices/add_slice.php"; }
65 function l_sliver ($node_id,$slice_id) { return "/db/nodes/slivers.php?node_id=" . $node_id. "&slice_id=" . $slice_id; }
66 function l_sliver_t ($node_id,$slice_id,$text) {
67 return href (l_sliver($node_id,$slice_id),$text) ; }
69 function l_persons () { return "/db/persons/index.php"; }
70 function l_person ($person_id) { return "/db/persons/index.php?id=" . $person_id; }
71 function l_person_t ($person_id,$text) { return href (l_person($person_id),$text); }
72 function l_persons_site ($site_id) { return "/db/persons/index.php?site_id=" . $site_id; }
73 function l_person_actions() { return "/db/persons/person_actions.php"; }
75 function l_tags () { return "/db/tags/index.php"; }
76 function l_tags_node () { return "/db/tags/index.php?type=node"; }
77 # xxx cleanup duplicate pages
78 #function l_tags_interface () { return "/db/tags/index.php?type=interface"; }
79 function l_tags_interface () { return "/db/nodes/settings.php"; }
80 function l_tags_slice () { return "/db/tags/index.php?type=slice"; }
82 function l_nodegroups () { return "/db/tags/node_groups.php"; }
83 function l_nodegroup ($nodegroup_id) { return "/db/tags/node_groups.php?id=" . $nodegroup_id; }
84 function l_nodegroup_t ($nodegroup_id,$text) {
85 return href(l_nodegroup($nodegroup_id),$text); }
87 function l_events () { return '/db/events/index.php'; }
88 function l_event ($type,$param,$id) { return '/db/events/index.php?type=' . $type . '&' . $param . '=' . $id; }
90 function l_peers() { return "/db/peers/index.php"; }
91 function l_peer($peer_id) { return "/db/peers/index.php?id=" . $peer_id; }
93 function l_comon($id_name,$id_value) { return '/db/nodes/comon.php?' . $id_name . "=" . $id_value; }
94 function l_sirius() { return "/db/sirius/index.php"; }
95 function l_about() { return "/db/about.php"; }
96 function l_doc_plcapi() { return "/db/doc/PLCAPI.php"; }
97 function l_doc_nmapi() { return "/db/doc/NMAPI.php"; }
98 function l_admin() { return "/db/adminsearch.php"; }
100 function l_logout() { return "/planetlab/logout.php"; }
101 function l_sulogout() { return "/planetlab/sulogout.php"; }
102 function l_reset_password() { return "/db/persons/reset_password.php"; }
103 function l_person_register() { return "/db/persons/register.php"; }
104 function l_site_register() { return "/db/sites/register.php"; }
105 function l_site_pending() { return "/db/sites/join_request.php"; }
107 //////////////////////////////////////////////////////////// validation functions
108 function topdomain ($hostname) {
109 $exploded=array_reverse(explode(".",$hostname));
113 function is_valid_email_addr ($email) {
114 if (ereg("^.+@.+\\..+$", $email) ) {
121 function is_valid_url ($url) {
122 if (ereg("^(http|https)://.+\..+$", strtolower($url) ) ) {
129 function is_valid_ip ($ip) {
130 if (ereg("^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$", $ip ) ) {
131 // it's at least in the right format, now check to see if
132 // each part is equal to less than 255
133 $parts= explode( '.', $ip );
134 $count= count($parts);
136 for( $i= 0; $i < $count; $i++ ) {
137 if( intval($parts[$i]) > 255 )
147 function is_valid_network_addr($network_addr,$mask) {
148 $lNetwork= ip2long($network_addr);
149 $lMask= ip2long($mask);
151 // are they the correct format?
152 if( $lNetwork == -1 || $lMask == -1 )
155 // is network address valid for the mask?
156 if( ($lNetwork & $lMask) != $lNetwork )
163 // returns whether or not a network address is in the reserved space
164 // in the case of a invalid network address, false will be returned.
165 function is_reserved_network_addr($network_addr) {
166 $lNetwork= ip2long($network_addr);
168 if( $lNetwork == -1 )
171 // does the network address fall in a reserved block?
172 $reserved_ips = array (
173 array('10.0.0.0','10.255.255.255'),
174 array('172.16.0.0','172.31.0.0'),
175 array('192.168.0.0','192.168.255.0')
177 foreach ($reserved_ips as $r) {
178 $min = ip2long($r[0]);
179 $max = ip2long($r[1]);
181 if (($lNetwork >= $min) && ($lNetwork <= $max))
188 //////////////////////////////////////////////////////////// peer & peerscopes
189 // when shortnames are needed on peers
190 function plc_peer_global_hash ($api) {
191 $peer_columns=array('peer_id','shortname');
192 $peer_filter=array();
193 $peers = $api->GetPeers($peer_filter,$peer_columns);
196 foreach ($peers as $peer) {
197 $peer_hash[$peer['peer_id']]=$peer;
201 function plc_peer_shortname ($peer_hash,$peer_id) {
203 return PLC_SHORTNAME;
205 return $peer_hash[$node['peer_id']]['shortname'];
209 // to set the background to grey on foreign objects
210 // return true if the peer is local
211 function plc_peer_block_start ($peer_hash,$peer_id) {
216 // set two classes, one eneraic to all foreign, and one based on the peer's shortname for finer grain tuning
217 printf ('<div class="plc-foreign plc-%s>"',strtolower(plc_peer_shortname($peer_hash,$peer_id)));
222 function plc_peer_block_end () {
226 // interpret standard syntax for peerscope
227 function plc_peer_info ($api,$peerscope) {
228 switch ($peerscope) {
230 $peer_filter=array();
231 $peer_label="all peers";
234 $peer_filter=array("peer_id"=>NULL);
235 $peer_label=PLC_SHORTNAME;
238 $peer_filter=array("~peer_id"=>NULL);
239 $peer_label="foreign peers";
242 if (is_int ($peerscope)) {
243 $peer_id=intval($peerscope);
244 $peers=$api->GetPeers(array("peer_id"=>$peer_id));
246 $peers=$api->GetPeers(array("shortname"=>$peerscope));
250 $peer_id=$peer['peer_id'];
251 $peer_filter=array("peer_id"=>$peer_id);
252 $peer_label='peer "' . $peer['shortname'] . '"';
254 $peer_filter=array();
255 $peer_label="[no such peer " . $peerscope . "]";
259 return array ($peer_filter,$peer_label);
262 //////////////////////////////////////////////////////////// titles
263 function t_site($site) { return " on site " . $site['name'] . " (" . $site['login_base'] .")"; }
264 function t_slice ($slice) { return " running slice " . $slice['name'] . " (" . $slice['slice_id'] . ")"; }
266 //////////////////////////////////////////////////////////// nav tabs
267 function tabs_node($node) { return array('Node ' . $node['hostname']=>l_node($node_id)); }
268 function tabs_site($site) { return array('Site ' . $site['name']=>l_site($site_id)); }
269 function tabs_slice($slice) { return array('Slice ' . $slice['name']=>l_slice($slice_id)); }
271 //////////////////////////////////////////////////////////// presentation
272 // builds a table from an array of strings, with the given class
273 // attempt to normalize the delete buttons and confirmations
274 function plc_delete_button($width=15) {
275 return '<span title="Delete this entry"><img width=' . $width . ' alt="Delete this entry" src="/planetlab/icons/delete.png"></span>';
278 function plc_js_confirm($message) {
279 return "onclick=\"javascript:return confirm('Are you sure you want to delete " . $message . " ?')\"";
282 function plc_delete_link($url,$delete_message,$visible) {
283 return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . $visible . "</a>";
286 function plc_delete_link_button($url,$delete_message,$width=15) {
287 return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . plc_delete_button($width) . "</a>";
290 function plc_event_button($type,$param,$id) {
291 return '<a href="' . l_event($type,$param,$id) . '"> <span title="Related events"> <img src="/planetlab/icons/event.png" width=18></span></a>';
294 function plc_comon_button ($id_name, $id_value,$target="") {
296 if (!empty($target)) {
297 $result.='target="' . $target . '" ';
299 $result.='href="' . l_comon($id_name,$id_value) . '">';
300 $result.='<span title="Link to Comon"> <img src="/planetlab/icons/comon.png" width="18"></span></a>';
304 function plc_vertical_table ($messages, $class="") {
305 // pretty print the cell
306 if ( empty( $messages) ) return "";
308 $formatted .= "<table ";
309 if ($class) $formatted .= "class='" . $class . "'";
311 foreach ($messages as $message) {
312 $formatted .= "<tr><td>" . $message . "</td></tr>";
314 $formatted .= "</table>";
318 //////////////////////////////////////////////////////////// various mappers
319 function get_site_id ($site) { return $site['site_id'];}
321 ////////////////////////////////////////////////////////////
322 function plc_error ($text) {
323 // should use the same channel as the php errors..
324 print "<div class='plc-error'> Error " . $text . "</div>";
327 function plc_errors ($list) {
328 print( "<div class='plc-error'>" );
329 print( "<p style='font-weight:bold'>The following errors occured:</p>" );
331 foreach( $errors as $err ) {
332 print( "<li>$err</li>\n" );
334 print( "</ul></div>\n" );
337 function plc_warning_div ($text) {
338 return "<div class='plc-warning'>" . $text . "</div>";
340 function plc_warning ($text) {
341 print plc_warning_div("Warning " . $text);
344 // shows a php variable verbatim with a heading message
345 function plc_debug ($message,$object) {
346 print "<br>" . $message . "<pre>";