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 //////////////////////////////////////////////////////////// roles & other checks on global $plc
9 function plc_is_admin () {
10   global $plc;
11   return in_array( 10, $plc->person['role_ids']);
12 }
13 function plc_is_pi () {
14   global $plc;
15   return in_array( 20, $plc->person['role_ids']);
16 }
17 function plc_is_tech () {
18   global $plc;
19   return in_array( 40, $plc->person['role_ids']);
20 }
21 function plc_in_site ($site_id) {
22   global $plc;
23   return in_array( $site_id, $plc->person['site_ids']);
24 }
25
26 function plc_my_site_id () {
27   global $plc;
28   return $plc->person['site_ids'][0];
29 }
30
31 function plc_my_person_id () {
32   global $plc;
33   return $plc->person['person_id'];
34 }
35
36 //////////////////////////////////////////////////////////// links    
37 function href ($url,$text) { return "<a href='" . $url . "'>" . $text . "</a>"; }
38
39 // naming scheme is
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
44
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/add_node.php"; }
49 function l_nodes_site ($site_id)        { return "/db/nodes/index.php?site_id=" . $site_id; }
50
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; }
55
56 function l_sites ()                     { return "/db/sites/index.php"; }
57 function l_site ($site_id)              { return "/db/persons/index.php?id=" . $site_id; }
58 function l_site_t ($site_id,$text)      { return href (l_site($site_id),$text); }
59
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"; }
64
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) ; }
68
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
74 function l_tags ()                      { return "/db/tags/index.php"; }
75 function l_tags_node ()                 { return "/db/tags/index.php?type=node"; }
76 # xxx cleanup duplicate pages
77 #function l_tags_interface ()           { return "/db/tags/index.php?type=interface"; }
78 function l_tags_interface ()            { return "/db/nodes/settings.php"; }
79 function l_tags_slice ()                { return "/db/tags/index.php?type=slice"; }
80
81 function l_nodegroups ()                { return "/db/tags/node_groups.php"; }
82 function l_nodegroup ($nodegroup_id)    { return "/db/tags/node_groups.php?id=" . $nodegroup_id; }
83 function l_nodegroup_t ($nodegroup_id,$text) { 
84                                           return href(l_nodegroup($nodegroup_id),$text); }
85
86 function l_events ()                    { return '/db/events/index.php'; }
87 function l_event ($type,$param,$id)     { return '/db/events/index.php?type=' . $type . '&' . $param . '=' . $id; }
88
89 function l_peers()                      { return "/db/peers/index.php"; }
90 function l_peer($peer_id)               { return "/db/peers/index.php?id=" . $peer_id; }
91
92 function l_comon($id_name,$id_value)    { return '/db/nodes/comon.php?' . $id_name . "=" . $id_value; }
93 function l_sirius()                     { return "/db/sirius/index.php"; }
94 function l_about()                      { return "/db/about.php"; }
95 function l_doc_plcapi()                 { return "/db/doc/PLCAPI.php"; }
96 function l_doc_nmapi()                  { return "/db/doc/NMAPI.php"; }
97
98 function l_logout()                     { return "/planetlab/logout.php"; }
99 function l_sulogout()                   { return "/planetlab/sulogout.php"; }
100 function l_reset_password()             { return "/db/persons/reset_password.php"; }
101 function l_person_register()            { return "/db/persons/register.php"; }
102 function l_site_register()              { return "/db/sites/register.php"; }
103 function l_site_pending()               { return "/db/sites/join_request.php"; }
104 function l_site_status()                { return "/db/sites/peers.php"; }
105
106 //////////////////////////////////////////////////////////// validation functions
107 function topdomain ($hostname) {
108   $exploded=array_reverse(explode(".",$hostname));
109   return $exploded[0];
110 }
111
112 function is_valid_email_addr ($email) {
113   if (ereg("^.+@.+\\..+$", $email) ) {
114     return true;
115   } else {
116     return false;
117   }
118 }
119
120 function is_valid_url ($url) {
121   if (ereg("^(http|https)://.+\..+$", strtolower($url) ) ) {
122     return true;
123   } else {
124     return false;
125   }
126 }
127
128 function is_valid_ip ($ip) {
129   if (ereg("^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$", $ip ) ) {
130       // it's at least in the right format, now check to see if
131       // each part is equal to less than 255
132       $parts= explode( '.', $ip );
133       $count= count($parts);
134
135       for( $i= 0; $i < $count; $i++ ) {
136         if( intval($parts[$i]) > 255 )
137           return false;
138       }
139
140       return true;
141   } else {
142     return false;
143   }
144 }
145
146 function is_valid_network_addr($network_addr,$mask) {
147   $lNetwork= ip2long($network_addr);
148   $lMask= ip2long($mask);
149
150   // are they the correct format?
151   if( $lNetwork == -1 || $lMask == -1 )
152     return false;
153
154   // is network address valid for the mask?
155   if( ($lNetwork & $lMask) != $lNetwork )
156     return false;
157
158   return true;
159 }
160
161
162 // returns whether or not a network address is in the reserved space
163 // in the case of a invalid network address, false will be returned.
164 function is_reserved_network_addr($network_addr) {
165   $lNetwork= ip2long($network_addr);
166
167   if( $lNetwork == -1 )
168     return false;
169
170   // does the network address fall in a reserved block?
171   $reserved_ips = array (
172                          array('10.0.0.0','10.255.255.255'),
173                          array('172.16.0.0','172.31.0.0'),
174                          array('192.168.0.0','192.168.255.0')
175                          );
176   foreach ($reserved_ips as $r) {
177     $min = ip2long($r[0]);
178     $max = ip2long($r[1]);
179       
180     if (($lNetwork >= $min) && ($lNetwork <= $max))
181       return true;
182   }
183
184   return false;
185 }
186
187 ////////////////////////////////////////////////////////////  peer & peerscopes
188 // when shortnames are needed on peers
189 function plc_peer_get_hash ($api) {
190   $peer_columns=array('peer_id','shortname');
191   $peer_filter=array();
192   $peers = $api->GetPeers($peer_filter,$peer_columns);
193   
194   $peer_hash=array();
195   foreach ($peers as $peer) {
196     $peer_hash[$peer['peer_id']]=$peer;
197   }
198 }
199
200 function plc_peer_shortname ($peer_hash,$peer_id) {
201   if ( ! $peer_id ) {
202     return PLC_SHORTNAME;
203   } else {
204      return $peer_hash[$node['peer_id']]['shortname'];
205   }
206 }
207
208 // to set the background to grey on foreign objects
209 // return true if the peer is local 
210 function plc_peer_block_start ($peer_hash,$peer_id) {
211   if ( ! $peer_id ) {
212     print "<div>";
213     return true;
214   } else {
215     // set two classes, one eneraic to all foreign, and one based on the peer's shortname for finer grain tuning
216     printf ('<div class="plc-foreign plc-%s>"',strtolower(plc_peer_shortname($peer_hash,$peer_id)));
217     return false;
218   }
219 }
220
221 function plc_peer_block_end () {
222   print "</div>\n";
223 }
224
225 // interpret standard syntax for peerscope
226 function plc_peer_info ($api,$peerscope) {
227   switch ($peerscope) {
228   case '':
229     $peer_filter=array();
230     $peer_label="all peers";
231     break;
232   case 'local':
233     $peer_filter=array("peer_id"=>NULL);
234     $peer_label=PLC_SHORTNAME;
235     break;
236   case 'foreign':
237     $peer_filter=array("~peer_id"=>NULL);
238     $peer_label="foreign peers";
239     break;
240   default:
241     if (is_int ($peerscope)) {
242       $peer_id=intval($peerscope);
243       $peers=$api->GetPeers(array("peer_id"=>$peer_id));
244     } else {
245       $peers=$api->GetPeers(array("shortname"=>$peerscope));
246     }
247     if ($peers) {
248       $peer=$peers[0];
249       $peer_id=$peer['peer_id'];
250       $peer_filter=array("peer_id"=>$peer_id);
251       $peer_label='peer "' . $peer['shortname'] . '"';
252     } else {
253       $peer_filter=array();
254       $peer_label="[no such peer " . $peerscope . "]";
255     }
256     break;
257   }
258   return array ($peer_filter,$peer_label);
259 }
260
261 //////////////////////////////////////////////////////////// titles
262 function t_site($site) { return " on site " . $site['name'] . " (" . $site['login_base'] .")"; }
263 function t_slice ($slice) { return " running slice " . $slice['name'] . " (" . $slice['slice_id'] . ")"; }
264
265 //////////////////////////////////////////////////////////// nav tabs
266 function tabs_node($node) { return array('Node ' . $node['hostname']=>l_node($node_id)); }
267 function tabs_site($site) { return array('Site ' . $site['name']=>l_site($site_id)); }
268 function tabs_slice($slice) { return array('Slice ' . $slice['name']=>l_slice($slice_id)); }
269
270 //////////////////////////////////////////////////////////// presentation
271 // builds a table from an array of strings, with the given class
272 // attempt to normalize the delete buttons and confirmations
273 function plc_delete_button($width=15) {
274   return '<span title="Delete this entry"><img width=' . $width . ' alt="Delete this entry" src="/planetlab/icons/delete.png"></span>';
275 }
276
277 function plc_js_confirm($message) {
278   return "onclick=\"javascript:return confirm('Are you sure you want to delete " . $message . " ?')\"";
279 }
280
281 function plc_delete_link($url,$delete_message,$visible) {
282   return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . $visible . "</a>";
283 }
284
285 function plc_delete_link_button($url,$delete_message,$width=15) {
286   return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . plc_delete_button($width) . "</a>";
287 }
288
289 function plc_event_button($type,$param,$id) {
290   return '<a href="' . l_event($type,$param,$id) . '"> <span title="Related events"> <img src="/planetlab/icons/event.png" width=18></span></a>';
291 }
292
293 function plc_comon_button ($id_name, $id_value,$target="") {
294   $result='<a ';
295   if (!empty($target)) {
296     $result.='target="' . $target . '" ';
297   }
298   $result.='href="' . l_comon($id_name,$id_value) . '">';
299   $result.='<span title="Link to Comon"> <img src="/planetlab/icons/comon.png" width="18"></span></a>';
300   return $result;
301 }
302
303 function plc_vertical_table ($messages, $class="") {
304   // pretty print the cell
305   if ( empty( $messages) ) return "";
306   $formatted = "";
307   $formatted .= "<table ";
308   if ($class) $formatted .= "class='" . $class . "'";
309   $formatted .= ">";
310   foreach ($messages as $message) {
311     $formatted .= "<tr><td>" . $message . "</td></tr>";
312   }
313   $formatted .= "</table>";
314   return $formatted;
315 }
316
317 ////////////////////////////////////////////////////////////
318 function plc_error ($text) {
319   // should use the same channel as the php errors..
320   print "<div class='plc-error'> Error " . $text . "</div>";
321 }
322
323 function plc_errors ($list) {
324   print( "<div class='plc-error'>" );
325   print( "<p style='font-weight:bold'>The following errors occured:</p>" );
326   print("<ul>");
327   foreach( $errors as $err ) {
328     print( "<li>$err</li>\n" );
329   }
330   print( "</ul></div>\n" );
331 }
332
333 function plc_warning ($text) {
334   print "<div class='plc-warning'> Warning " . $text . "</div>";
335 }
336
337 // shows a php variable verbatim with a heading message
338 function plc_debug ($message,$object) {
339   print "<br>" . $message . "<pre>";
340   print_r ($object);
341   print "</pre>";
342 }
343
344 ?>