tables layout
[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_actions ()                   { return "/db/actions.php"; }
46
47 function l_nodes ()                     { return "/db/nodes/index.php"; }
48 function l_nodes_local ()               { return "/db/nodes/index.php?peerscope=local"; }
49 function l_node ($node_id)              { return "/db/nodes/node.php?id=$node_id"; }
50 function l_node_t ($node_id,$text)      { return href (l_node($node_id),$text); }
51 function l_node_add ()                  { return "/db/nodes/node_add.php"; }
52 function l_nodes_site ($site_id)        { return "/db/nodes/index.php?site_id=$site_id"; }
53
54 function l_interface ($interface_id)    { return "/db/nodes/interfaces.php?id=$interface_id"; }
55 function l_interface_t ($interface_id,$text) { 
56                                           return href (l_interface($interface_id),$text); }
57 function l_interface_add($node_id)      { return "/db/nodes/interfaces.php?node_id=$node_id"; }
58
59 function l_sites ()                     { return "/db/sites/index.php"; }
60 function l_site ($site_id)              { return "/db/sites/index.php?id=$site_id"; }
61 function l_site_t ($site_id,$text)      { return href (l_site($site_id),$text); }
62
63 function l_slices ()                    { return "/db/slices/index.php"; }
64 function l_slice ($slice_id)            { return "/db/slices/index.php?id=$slice_id"; }
65 function l_slice_t ($slice_id,$text)    { return href (l_slice($slice_id),$text); }
66 function l_slice_add ()                 { return "/db/slices/add_slice.php"; }
67
68 function l_sliver ($node_id,$slice_id)  { return "/db/nodes/slivers.php?node_id=$node_id&slice_id=$slice_id"; }
69 function l_sliver_t ($node_id,$slice_id,$text) { 
70                                           return href (l_sliver($node_id,$slice_id),$text) ; }
71
72 function l_persons ()                   { return "/db/persons/index.php"; }
73 function l_person ($person_id)          { return "/db/persons/index.php?id=$person_id"; }
74 function l_person_t ($person_id,$text)  { return href (l_person($person_id),$text); }
75 function l_persons_site ($site_id)      { return "/db/persons/index.php?site_id=$site_id"; }
76
77 function l_tags ()                      { return "/db/tags/index.php"; }
78 function l_tag ($tag_type_id)           { return "/db/tags/index.php"; }
79 function l_tag_add()                    { return "/db/tags/tag_form.php"; }
80 function l_tag_update($id)              { return "/db/tags/tag_form.php&action=update-tag-type&id=$id"; }
81
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); }
86
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"; }
89
90 function l_peers()                      { return "/db/peers/index.php"; }
91 function l_peer($peer_id)               { return "/db/peers/index.php?id=$peer_id"; }
92
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"; }
99
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"; }
106
107 // returns array ['url' => path, 'values' => hash (key=>value)* ]
108 function split_url ($full_url) {
109   list($url,$args) = explode("?",$full_url);
110   $values=array();
111   if ($args) {
112     $pairs=explode("&",$args);
113     foreach ($pairs as $pair) {
114       list ($name,$value) = explode("=",$pair);
115       $values[$name]=$value;
116     }
117   }
118   return array("url"=>$url,"values"=>$values);
119 }
120
121 //////////////////////////////////////////////////////////// validation functions
122 function topdomain ($hostname) {
123   $exploded=array_reverse(explode(".",$hostname));
124   return $exploded[0];
125 }
126
127 function is_valid_email_addr ($email) {
128   if (ereg("^.+@.+\\..+$", $email) ) {
129     return true;
130   } else {
131     return false;
132   }
133 }
134
135 function is_valid_url ($url) {
136   if (ereg("^(http|https)://.+\..+$", strtolower($url) ) ) {
137     return true;
138   } else {
139     return false;
140   }
141 }
142
143 function is_valid_ip ($ip) {
144   if (ereg("^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$", $ip ) ) {
145       // it's at least in the right format, now check to see if
146       // each part is equal to less than 255
147       $parts= explode( '.', $ip );
148       $count= count($parts);
149
150       for( $i= 0; $i < $count; $i++ ) {
151         if( intval($parts[$i]) > 255 )
152           return false;
153       }
154
155       return true;
156   } else {
157     return false;
158   }
159 }
160
161 function is_valid_network_addr($network_addr,$mask) {
162   $lNetwork= ip2long($network_addr);
163   $lMask= ip2long($mask);
164
165   // are they the correct format?
166   if( $lNetwork == -1 || $lMask == -1 )
167     return false;
168
169   // is network address valid for the mask?
170   if( ($lNetwork & $lMask) != $lNetwork )
171     return false;
172
173   return true;
174 }
175
176
177 // returns whether or not a network address is in the reserved space
178 // in the case of a invalid network address, false will be returned.
179 function is_reserved_network_addr($network_addr) {
180   $lNetwork= ip2long($network_addr);
181
182   if( $lNetwork == -1 )
183     return false;
184
185   // does the network address fall in a reserved block?
186   $reserved_ips = array (
187                          array('10.0.0.0','10.255.255.255'),
188                          array('172.16.0.0','172.31.0.0'),
189                          array('192.168.0.0','192.168.255.0')
190                          );
191   foreach ($reserved_ips as $r) {
192     $min = ip2long($r[0]);
193     $max = ip2long($r[1]);
194       
195     if (($lNetwork >= $min) && ($lNetwork <= $max))
196       return true;
197   }
198
199   return false;
200 }
201
202 ////////////////////////////////////////////////////////////  peer & peerscopes
203 function plc_role_global_hash ($api) {
204   $hash=array();
205   $roles=$api->GetRoles();
206   foreach ($roles as $role) {
207     $hash[$role['role_id']]=$role['name'];
208   }
209   return $hash;
210 }
211   
212 ////////////////////////////////////////////////////////////  peer & peerscopes
213 // when shortnames are needed on peers
214 function plc_peer_global_hash ($api) {
215   $peer_columns=array('peer_id','shortname');
216   $peer_filter=array();
217   $peers = $api->GetPeers($peer_filter,$peer_columns);
218   
219   $peer_hash=array();
220   foreach ($peers as $peer) {
221     $peer_hash[$peer['peer_id']]=$peer;
222   }
223 }
224
225 function plc_peer_shortname ($peer_hash,$peer_id) {
226   if ( ! $peer_id ) {
227     return PLC_SHORTNAME;
228   } else {
229      return $peer_hash[$node['peer_id']]['shortname'];
230   }
231 }
232
233 // to set the background to grey on foreign objects
234 // return true if the peer is local 
235 function plc_peer_block_start ($peer_hash,$peer_id) {
236   if ( ! $peer_id ) {
237     print "<div>";
238     return true;
239   } else {
240     // set two classes, one eneraic to all foreign, and one based on the peer's shortname for finer grain tuning
241     printf ('<div class="plc-foreign plc-%s>"',strtolower(plc_peer_shortname($peer_hash,$peer_id)));
242     return false;
243   }
244 }
245
246 function plc_peer_block_end () {
247   print "</div>\n";
248 }
249
250 // interpret standard syntax for peerscope
251 function plc_peer_info ($api,$peerscope) {
252   switch ($peerscope) {
253   case '':
254     $peer_filter=array();
255     $peer_label="all peers";
256     break;
257   case 'local':
258     $peer_filter=array("peer_id"=>NULL);
259     $peer_label=PLC_SHORTNAME;
260     break;
261   case 'foreign':
262     $peer_filter=array("~peer_id"=>NULL);
263     $peer_label="foreign peers";
264     break;
265   default:
266     if (is_int ($peerscope)) {
267       $peer_id=intval($peerscope);
268       $peers=$api->GetPeers(array("peer_id"=>$peer_id));
269     } else {
270       $peers=$api->GetPeers(array("shortname"=>$peerscope));
271     }
272     if ($peers) {
273       $peer=$peers[0];
274       $peer_id=$peer['peer_id'];
275       $peer_filter=array("peer_id"=>$peer_id);
276       $peer_label='peer "' . $peer['shortname'] . '"';
277     } else {
278       $peer_filter=array();
279       $peer_label="[no such peer " . $peerscope . "]";
280     }
281     break;
282   }
283   return array ($peer_filter,$peer_label);
284 }
285
286 //////////////////////////////////////////////////////////// titles
287 function t_site($site) { return " on site " . $site['name'] . " (" . $site['login_base'] .")"; }
288 function t_slice ($slice) { return " running slice " . $slice['name'] . " (" . $slice['slice_id'] . ")"; }
289
290 //////////////////////////////////////////////////////////// nav tabs
291 function tabs_node($node) { return array('Node ' . $node['hostname']=>l_node($node_id)); }
292 function tabs_site($site) { return array('Site ' . $site['name']=>l_site($site_id)); }
293 function tabs_slice($slice) { return array('Slice ' . $slice['name']=>l_slice($slice_id)); }
294
295 //////////////////////////////////////////////////////////// presentation
296 // builds a table from an array of strings, with the given class
297 // attempt to normalize the delete buttons and confirmations
298 function plc_delete_button($width=15) {
299   return '<span title="Delete this entry"><img width=' . $width . ' alt="Delete this entry" src="/planetlab/icons/delete.png"></span>';
300 }
301
302 function plc_js_confirm($message) {
303   return "onclick=\"javascript:return confirm('Are you sure you want to delete " . $message . " ?')\"";
304 }
305
306 function plc_delete_link($url,$delete_message,$visible) {
307   return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . $visible . "</a>";
308 }
309
310 function plc_delete_link_button($url,$delete_message,$width=15) {
311   return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . plc_delete_button($width) . "</a>";
312 }
313
314 function plc_event_button($type,$param,$id) {
315   return '<a href="' . l_event($type,$param,$id) . '"> <span title="Related events"> <img src="/planetlab/icons/event.png" width=18></span></a>';
316 }
317
318 function plc_comon_button ($id_name, $id_value,$target="") {
319   $result='<a ';
320   if (!empty($target)) {
321     $result.='target="' . $target . '" ';
322   }
323   $result.='href="' . l_comon($id_name,$id_value) . '">';
324   $result.='<span title="Link to Comon"> <img src="/planetlab/icons/comon.png" width="18"></span></a>';
325   return $result;
326 }
327
328 function plc_vertical_table ($messages, $class="") {
329   // pretty print the cell
330   if ( empty( $messages) ) return "";
331   $formatted = "";
332   $formatted .= "<table ";
333   if ($class) $formatted .= "class='" . $class . "'";
334   $formatted .= ">";
335   foreach ($messages as $message) {
336     $formatted .= "<tr><td>" . $message . "</td></tr>";
337   }
338   $formatted .= "</table>";
339   return $formatted;
340 }
341
342 //////////////////////////////////////////////////////////// various mappers
343 function get_site_id ($site) { return $site['site_id'];}
344
345 ////////////////////////////////////////////////////////////
346 function plc_section ($text,$line=true) {
347   if ($line) { print "<hr/>";}
348   print "<h2 class=plc> $text </h2>\n";
349 }
350
351 function plc_error ($text) {
352   // should use the same channel as the php errors..
353   print "<div class='plc-error'> Error " . $text . "</div>";
354 }
355
356 function plc_errors ($list) {
357   print( "<div class='plc-error'>" );
358   print( "<p style='font-weight:bold'>The following errors occured:</p>" );
359   print("<ul>");
360   foreach( $errors as $err ) {
361     print( "<li>$err</li>\n" );
362   }
363   print( "</ul></div>\n" );
364 }
365
366 function plc_warning_div ($text) {
367   return "<div class='plc-warning'>" . $text . "</div>";
368 }
369 function plc_warning ($text) {
370   print plc_warning_div("Warning " . $text);
371 }
372
373 // shows a php variable verbatim with a heading message
374 function plc_debug ($message,$object) {
375   print "<br>" . $message . "<pre>";
376   print_r ($object);
377   print "</pre>";
378 }
379
380 ?>