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 //////////////////////////////////////////////////////////// roles & other checks on global $plc
90 function plc_is_admin () {
91   global $plc;
92   return in_array( 10, $plc->person['role_ids']);
93 }
94 function plc_is_pi () {
95   global $plc;
96   return in_array( 20, $plc->person['role_ids']);
97 }
98 function plc_is_tech () {
99   global $plc;
100   return in_array( 40, $plc->person['role_ids']);
101 }
102 function plc_in_site ($site_id) {
103   global $plc;
104   return in_array( $site_id, $plc->person['site_ids']);
105 }
106
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);
113   
114   $peer_hash=array();
115   foreach ($peers as $peer) {
116     $peer_hash[$peer['peer_id']]=$peer;
117   }
118 }
119
120 function plc_peer_shortname ($peer_hash,$peer_id) {
121   if ( ! $peer_id ) {
122     return PLC_SHORTNAME;
123   } else {
124      return $peer_hash[$node['peer_id']]['shortname'];
125   }
126 }
127
128 // to set the background to grey on foreign objects
129 function plc_peer_block_start ($peer_hash,$peer_id) {
130   if ( ! $peer_id ) {
131     print "<div>";
132   } else {
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)));
135   }
136 }
137
138 function plc_peer_block_end () {
139   print "</div>\n";
140 }
141
142 //// standard peerscope syntax
143 function plc_peer_info ($api,$peerscope) {
144   switch ($_GET['peerscope']) {
145   case '':
146     $peer_filter=array();
147     $peer_label="all peers";
148     break;
149   case 'local':
150     $peer_filter=array("peer_id"=>NULL);
151     $peer_label=PLC_SHORTNAME;
152     break;
153   case 'foreign':
154     $peer_filter=array("~peer_id"=>NULL);
155     $peer_label="foreign peers";
156     break;
157   default:
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'] . '"';
162     break;
163   }
164   return array ($peer_filter,$peer_label);
165 }
166
167 //////////////////////////////////////////////////////////// links    
168 function href ($url,$text) { return "<a href='" . $url . "'>" . $text . "</a>"; }
169
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); }
174
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); }
179
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); }
182
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); }
187
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); }
192
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) ; }
195
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); }
200
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; }
203
204 function l_logout() { return "/planetlab/logout.php"; }
205
206 //////////////////////////////////////////////////////////// titles
207 function t_site($site) { return " on site " . $site['name'] . " (" . $site['login_base'] .")"; }
208 function t_slice ($slice) { return " running slice " . $slice['name'] . " (" . $slice['slice_id'] . ")"; }
209
210 //////////////////////////////////////////////////////////// nav tabs
211 function tabs_node($node) { return array('Node ' . $node['hostname']=>l_node_u($node_id)); }
212 function tabs_site($site) { return array('Site ' . $site['name']=>l_site_u($site_id)); }
213 function tabs_slice($slice) { return array('Slice ' . $slice['name']=>l_slice_u($slice_id)); }
214
215 //////////////////////////////////////////////////////////// presentation
216 // builds a table from an array of strings, with the given class
217 // attempt to normalize the delete buttons and confirmations
218 function plc_delete_button($width=15) {
219   return '<span title="Delete this entry"><img width=' . $width . ' alt="Delete this entry" src="/planetlab/icons/delete.png"></span>';
220 }
221
222 function plc_js_confirm($message) {
223   return "onclick=\"javascript:return confirm('Are you sure you want to delete " . $message . " ?')\"";
224 }
225
226 function plc_delete_link($url,$delete_message,$visible) {
227   return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . $visible . "</a>";
228 }
229
230 function plc_delete_link_button($url,$delete_message,$width=15) {
231   return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . plc_delete_button($width) . "</a>";
232 }
233
234 function plc_event_button($type,$param,$id) {
235   return '<a href="' . l_event($type,$param,$id) . '"> <span title="Related events"> <img src="/planetlab/icons/event.png" width=18></span></a>';
236 }
237
238 function plc_comon_button ($id_name, $id_value,$target="") {
239   $result='<a ';
240   if (!empty($target)) {
241     $result.='target="' . $target . '" ';
242   }
243   $result.='href="' . l_comon($id_name,$id_value) . '">';
244   $result.='<span title="Link to Comon"> <img src="/planetlab/icons/comon.png" width="18"></span></a>';
245   return $result;
246 }
247
248 function plc_make_table ($class, $messages) {
249   // pretty print the cell
250   $formatted = "";
251   if (! empty ($messages)) {
252     $formatted="<table class='" . $class . "'>";
253     foreach ($messages as $message) {
254       $formatted .= "<tr><td>" . $message . "</td></tr>";
255     }
256     $formatted .= "</table>";
257   }
258   return $formatted;
259 }
260
261 ////////////////////////////////////////////////////////////
262 function plc_error ($text) {
263   // should use the same channel as the php errors..
264   print "<div class='plc-warning'> Error " . $text . "</div>";
265 }
266
267 function plc_warning ($text) {
268   print "<div class='plc-warning'> Warning " . $text . "</div>";
269 }
270
271 // shows a php variable verbatim with a heading message
272 function plc_debug ($message,$object) {
273   print "<br>" . $message . "<pre>";
274   print_r ($object);
275   print "</pre>";
276 }
277
278 ?>