checkpoint
[plewww.git] / planetlab / includes / plc_functions.php
1 <?php
2
3 // $Id$
4
5 // will trash this eventually
6 require_once 'plc_functions_trash.php';
7
8 //////////////////////////////////////////////////////////// validation functions
9 function topdomain ($hostname) {
10   $exploded=array_reverse(explode(".",$hostname));
11   return $exploded[0];
12 }
13
14 function is_valid_email_addr ($email) {
15   if (ereg("^.+@.+\\..+$", $email) ) {
16     return true;
17   } else {
18     return false;
19   }
20 }
21
22 function is_valid_url ($url) {
23   if (ereg("^(http|https)://.+\..+$", strtolower($url) ) ) {
24     return true;
25   } else {
26     return false;
27   }
28 }
29
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);
36
37       for( $i= 0; $i < $count; $i++ ) {
38         if( intval($parts[$i]) > 255 )
39           return false;
40       }
41
42       return true;
43   } else {
44     return false;
45   }
46 }
47
48 function is_valid_network_addr($network_addr,$mask) {
49   $lNetwork= ip2long($network_addr);
50   $lMask= ip2long($mask);
51
52   // are they the correct format?
53   if( $lNetwork == -1 || $lMask == -1 )
54     return false;
55
56   // is network address valid for the mask?
57   if( ($lNetwork & $lMask) != $lNetwork )
58     return false;
59
60   return true;
61 }
62
63
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);
68
69   if( $lNetwork == -1 )
70     return false;
71
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')
77                          );
78   foreach ($reserved_ips as $r) {
79     $min = ip2long($r[0]);
80     $max = ip2long($r[1]);
81       
82     if (($lNetwork >= $min) && ($lNetwork <= $max))
83       return true;
84   }
85
86   return false;
87 }
88
89 ////////////////////////////////////////////////////////////  peerscopes
90 function plc_peer_info ($api,$peerscope) {
91   switch ($_GET['peerscope']) {
92   case '':
93     $peer_filter=array();
94     $peer_label="all peers";
95     break;
96   case 'local':
97     $peer_filter=array("peer_id"=>NULL);
98     $peer_label=PLC_SHORTNAME;
99     break;
100   case 'foreign':
101     $peer_filter=array("~peer_id"=>NULL);
102     $peer_label="foreign peers";
103     break;
104   default:
105     $peer_id=intval($_GET['peerscope']);
106     $peer_filter=array("peer_id"=>$peer_id);
107     $peer=$api->GetPeers(array("peer_id"=>$peer_id));
108     $peer_label='peer "' . $peer[0]['peername'] . '"';
109     break;
110   }
111   return array ($peer_filter,$peer_label);
112 }
113
114 //////////////////////////////////////////////////////////// links    
115 function href ($url,$text) { return "<a href='" . $url . "'>" . $text . "</a>"; }
116
117 function l_nodes () { return "/db/nodes/newindex.php"; }
118 function l_node_u ($node_id) { return "/db/nodes/node.php?id=" . $node_id; }
119 function l_node ($node_id) { return href (l_node_u($node_id),$node_id); }
120 function l_node2 ($node_id,$text) { return href (l_node_u($node_id),$text); }
121
122 function l_sites () { return "/db/sites/index.php"; }
123 function l_site_u ($site_id) { return "/db/persons/index.php?id=" . $site_id; }
124 function l_site ($site_id) { return href (l_site_u($site_id),$site_id); }
125 function l_site2 ($site_id,$text) { return href (l_site_u($site_id),$text); }
126
127 function l_slices () { return "/db/slices/index.php"; }
128 function l_slice_u ($slice_id) { return "/db/persons/index.php?id=" . $slice_id; }
129 function l_slice ($slice_id) { return href (l_slice_u($slice_id),$slice_id); }
130 function l_slice2 ($slice_id,$text) { return href (l_slice_u($slice_id),$text); }
131
132 function l_persons () { return "/db/persons/index.php"; }
133 function l_person_u ($person_id) { return "/db/persons/index.php?id=" . $person_id; }
134 function l_person ($person_id) { return href (l_person_u($person_id),$person_id); }
135 function l_person2 ($person_id,$text) { return href (l_person_u($person_id),$text); }
136
137 function l_interfaces () { return "/db/interfaces/index.php"; }
138 function l_interface_u ($interface_id) { return "/db/interfaces/index.php?id=" . $interface_id; }
139 function l_interface ($interface_id) { return href (l_interface_u($interface_id),$interface_id); }
140 function l_interface2 ($interface_id,$text) { return href (l_interface_u($interface_id),$text); }
141
142 function l_event ($type,$param,$id) { return '/db/events/index.php?type=' . $type . '&' . $param . '=' . $id; }
143
144 //////////////////////////////////////////////////////////// titles
145 function t_site($site) { return " on site " . $site['name'] . " (" . $site['login_base'] .")"; }
146 function t_slice ($slice) { return " running slice " . $slice['name'] . " (" . $slice['slice_id'] . ")"; }
147
148 //////////////////////////////////////////////////////////// nav tabs
149 function tabs_node($node) { return array('Node ' . $node['hostname']=>l_node_u($node_id)); }
150 function tabs_site($site) { return array('Site ' . $site['name']=>l_site_u($site_id)); }
151 function tabs_slice($slice) { return array('Slice ' . $slice['name']=>l_slice_u($slice_id)); }
152
153 //////////////////////////////////////////////////////////// presentation
154 // builds a table from an array of strings, with the given class
155 function plc_make_table ($class, $messages) {
156   // pretty print the cell
157   $formatted = "";
158   if (! empty ($messages)) {
159     $formatted="<table class='" . $class . "'>";
160     foreach ($messages as $message) {
161       $formatted .= "<tr><td>" . $message . "</td></tr>";
162     }
163     $formatted .= "</table>";
164   }
165   return $formatted;
166 }
167
168 // attempt to normalize the delete buttons and confirmations
169 function plc_delete_button($width=15) {
170   return '<span title="Delete this entry"><img width=' . $width . ' alt="Delete this entry" src="/planetlab/icons/delete.png"></span>';
171 }
172
173 function plc_js_confirm($message) {
174   return "onclick=\"javascript:return confirm('Are you sure you want to delete " . $message . " ?')\"";
175 }
176
177 function plc_delete_link($url,$delete_message,$visible) {
178   return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . $visible . "</a>";
179 }
180
181 function plc_delete_link_button($url,$delete_message,$width=15) {
182   return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . plc_delete_button($width) . "</a>";
183 }
184
185 function plc_event_button($type,$param,$id) {
186   return '<a href="' . l_event($type,$param,$id) . '"> <span title="Related events"> <img src="/planetlab/icons/event.png" width=18></span></a>';
187 }
188
189 function plc_comon_button ($field, $value,$target="") {
190   $result='<a ';
191   if (!empty($target)) {
192     $result.='target="' . $target . '" ';
193   }
194   $result.='href="/db/nodes/comon.php?' . $field . "=" . $value . '">';
195   $result.='<span title="Link to Comon"> <img src="/planetlab/icons/comon.png" width="18"></span></a>';
196   return $result;
197 }
198
199 ////////////////////////////////////////////////////////////
200 function plc_error ($text) {
201   // should use the same channel as the php errors..
202   print "<div class='plc-warning'> Error " . $text . "</div>";
203 }
204
205 function plc_warning ($text) {
206   print "<div class='plc-warning'> Warning " . $text . "</div>";
207 }
208
209 // shows a php variable verbatim with a heading message
210 function plc_debug ($message,$object) {
211   print "<br>" . $message . "<pre>";
212   print_r ($object);
213   print "</pre>";
214 }
215
216 ?>