split in two
[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 // utility
9 function my_is_int ($x) {
10     return (is_numeric($x) ? intval($x) == $x : false);
11 }
12
13 //////////////////////////////////////////////////////////// roles & other checks on global $plc
14 function plc_is_admin () {
15   global $plc;
16   return in_array( 10, $plc->person['role_ids']);
17 }
18 function plc_is_pi () {
19   global $plc;
20   return in_array( 20, $plc->person['role_ids']);
21 }
22 function plc_is_tech () {
23   global $plc;
24   return in_array( 40, $plc->person['role_ids']);
25 }
26 function plc_in_site ($site_id) {
27   global $plc;
28   return in_array( $site_id, $plc->person['site_ids']);
29 }
30
31 function plc_my_site_id () {
32   global $plc;
33   return $plc->person['site_ids'][0];
34 }
35
36 function plc_my_person_id () {
37   global $plc;
38   return $plc->person['person_id'];
39 }
40
41 //////////////////////////////////////////////////////////// links    
42 function href ($url,$text) { return "<a href='" . $url . "'>" . $text . "</a>"; }
43
44 // naming scheme is
45 // l_objects()                  -> the url to the page that list objects
46 // l_object($object_id)         -> the url to hte page thas details object with given id
47 // l_object_t($object_id,text)  -> an <a> tag that shows text and links to the above
48 // l_object_add ()              -> the url to that object-afding page
49
50 function l_actions ()                   { return "/db/common/actions.php"; }
51 // some complex node actions are kept separate, e.g. the ones related to getbootmedium
52 function l_actions_download ()          { return "/db/nodes/node_downloads.php"; }
53
54 function l_nodes ()                     { return "/db/nodes/index.php"; }
55 function l_nodes_peer ($peer_id)        { return "/db/nodes/index.php?peerscope=$peer_id"; }
56 function l_node ($node_id)              { return "/db/nodes/node.php?id=$node_id"; }
57 function l_node_t ($node_id,$text)      { return href (l_node($node_id),$text); }
58 function l_node_obj($node)              { return href(l_node($node['node_id']),$node['hostname']); }
59 function l_node_add ()                  { return "/db/nodes/node_add.php"; }
60 function l_nodes_site ($site_id)        { return "/db/nodes/index.php?site_id=$site_id"; }
61
62 function l_interface ($interface_id)    { return "/db/nodes/interface.php?id=$interface_id"; }
63 function l_interface_t ($interface_id,$text) { 
64                                           return href (l_interface($interface_id),$text); }
65 function l_interface_add($node_id)      { return "/db/nodes/interface.php?node_id=$node_id"; }
66
67 function l_sites ()                     { return "/db/sites/index.php"; }
68 function l_sites_peer ($peer_id)        { return "/db/sites/index.php?peerscope=$peer_id"; }
69 function l_site ($site_id)              { return "/db/sites/index.php?id=$site_id"; }
70 function l_site_t ($site_id,$text)      { return href (l_site($site_id),$text); }
71
72 function l_slices ()                    { return "/db/slices/index.php"; }
73 function l_slices_peer ($peer_id)       { return "/db/slices/index.php?peerscope=$peer_id"; }
74 function l_slice ($slice_id)            { return "/db/slices/index.php?id=$slice_id"; }
75 function l_slice_t ($slice_id,$text)    { return href (l_slice($slice_id),$text); }
76 function l_slice_add ()                 { return "/db/slices/slice_add.php"; }
77 function l_slices_site($site_id)        { return "/db/slices/index.php?site_id=$site_id"; }
78 // from an object
79 function l_slice_obj ($slice)           { return l_slice_t ($slice['slice_id'],$slice['name']); }
80
81 function l_sliver ($node_id,$slice_id)  { return "/db/nodes/slivers.php?node_id=$node_id&slice_id=$slice_id"; }
82 function l_sliver_t ($node_id,$slice_id,$text) { 
83                                           return href (l_sliver($node_id,$slice_id),$text) ; }
84
85 function l_persons ()                   { return "/db/persons/index.php"; }
86 function l_persons_peer ($peer_id)      { return "/db/persons/index.php?peerscope=$peer_id"; }
87 function l_person ($person_id)          { return "/db/persons/index.php?id=$person_id"; }
88 function l_person_t ($person_id,$text)  { return href (l_person($person_id),$text); }
89 function l_persons_site ($site_id)      { return "/db/persons/index.php?site_id=$site_id"; }
90 function l_person_obj ($person)         { return l_person_t($person['person_id'],$person['email']); }
91
92 function l_tags ()                      { return "/db/tags/index.php"; }
93 function l_tag ($tag_type_id)           { return "/db/tags/index.php?id=$tag_type_id"; }
94 function l_tag_obj ($tag)               { return href(l_tag($tag['tag_type_id']),$tag['tagname']); }
95
96 function l_nodegroups ()                { return "/db/tags/nodegroups.php"; }
97 function l_nodegroup ($nodegroup_id)    { return "/db/tags/nodegroup.php?id=$nodegroup_id"; }
98 function l_nodegroup_t ($nodegroup_id,$text) { 
99                                           return href(l_nodegroup($nodegroup_id),$text); }
100
101 function l_events ()                    { return "/db/events/index.php"; }
102 function l_event ($type,$param,$id)     { return "/db/events/index.php?type=$type&$param=$id"; }
103
104 function l_peers()                      { return "/db/peers/index.php"; }
105 function l_peer($peer_id)               { return "/db/peers/index.php?id=$peer_id"; }
106 function l_peer_t($peer_id,$text)       { return href(l_peer($peer_id),$text); }
107
108 function l_comon($id_name,$id_value)    { return "/db/nodes/comon.php?$id_name=$id_value"; }
109 function l_sirius()                     { return "/db/sirius/index.php"; }
110 function l_about()                      { return "/db/common/about.php"; }
111 function l_doc_plcapi()                 { return "/db/doc/PLCAPI.php"; }
112 function l_doc_nmapi()                  { return "/db/doc/NMAPI.php"; }
113 function l_admin()                      { return "/db/common/adminsearch.php"; }
114
115 function l_login()                      { return "/db/common/login.php"; }
116 function l_logout()                     { return "/planetlab/common/logout.php"; }
117 function l_sulogout()                   { return "/planetlab/common/sulogout.php"; }
118 function l_reset_password()             { return "/db/persons/reset_password.php"; }
119 function l_person_register()            { return "/db/persons/register.php"; }
120 function l_site_register()              { return "/db/sites/register.php"; }
121 function l_sites_pending()              { return "/db/sites/join_request.php"; }
122
123
124 //////////////////////////////////////////////////////////// nav tabs
125 function tab_nodes ()           { return array ('label'=>'All nodes','url'=>l_nodes(), 
126                                                 'bubble'=>'Display nodes from all peers'); }
127 function tab_nodes_local ()     { return array ('label'=>'Local nodes', 'url'=>l_nodes_peer('local'), 
128                                                 'bubble'=>'Display local nodes only'); }
129 function tab_nodes_site($site_id){ return array ('label'=>'Site nodes', 'url'=>l_nodes_site($site_id), 
130                                                  'bubble'=>'Display nodes on that site'); }
131 function tab_nodes_mysite ()    { return array ('label'=>'My nodes', 'url'=>l_nodes_site(plc_my_site_id()), 
132                                                 'bubble'=>'Display nodes on that site'); }
133 function tab_node($node)        { return array ('label'=>'Node '.$node['hostname'], 'url'=>l_node($node_id),
134                                                 'bubble'=>'Details for ' . $node['hostname']); }
135 //////////
136 function tab_site($site)        { return array ('label'=>'Site '.$site['login_base'], 'url'=>l_site($site_id),
137                                                 'bubble'=>'Details for ' . $site['name']); }
138 function tab_mysite()           { return array ('label'=>'My site', 'url'=>l_site(plc_my_site_id()),
139                                                 'bubble'=>'Details for site ' . plc_my_site_id()); }
140 function tab_sites ()           { return array ('label'=>'All sites' , 'url'=>l_sites(), 'bubble'=> 'Display all sites'); }
141 function tab_sites_local ()     { return array ('label'=>'Local sites' , 'url'=>l_sites_peer('local'), 'bubble'=> 'Display local sites'); }
142 //////////
143 function tab_slices()           { return array ('label'=>'All slices', 'url'=>l_slices(),
144                                                 'bubble' => 'Display all slices'); }
145 function tab_slice($slice)      { return array ('label'=>'Slice '.$slice['name'], 'url'=>l_slice($slice_id),
146                                                 'bubble' => 'Details for ' . $slice['name']); }
147 //////////
148 function tab_persons()          { return array ('label'=>'All accounts', 'url'=>l_persons(),
149                                                 'bubble'=>'Display users from all peers'); }
150 function tab_persons_local()    { return array ('label'=>'Local accounts', 'url'=>l_persons_peer('local'),
151                                                 'bubble'=>'Display local users'); }
152 function tab_persons_mysite()   { return array ('label'=>'My accounts' , 'url'=>l_persons_site(plc_my_site_id()),
153                                                 'bubble'=>'Display accounts on site ' . plc_my_site_id()); }
154 //////////
155 function tab_tags()             { return array ('label'=>'Tag Types', 'url'=>l_tags(),
156                                                 'bubble' => 'Display and create tag types'); }
157 function tab_nodegroups()       { return array ('label'=>'Nodegroups', 'url'=>l_nodegroups(),
158                                                 'bubble' => 'Display and create nodegroups'); }
159
160 // only partial tab
161 function tablook_event()        { return array('image'=>'/planetlab/icons/event.png','height'=>18);}
162 function tablook_comon()        { return array('image'=>'/planetlab/icons/comon.png','height'=>18);}
163
164 ////////////////////
165
166 //////////////////////////////////////////////////////////// validation functions
167 function topdomain ($hostname) {
168   $exploded=array_reverse(explode(".",$hostname));
169   return $exploded[0];
170 }
171
172 function is_valid_email_addr ($email) {
173   if (ereg("^.+@.+\\..+$", $email) ) {
174     return true;
175   } else {
176     return false;
177   }
178 }
179
180 function is_valid_url ($url) {
181   if (ereg("^(http|https)://.+\..+$", strtolower($url) ) ) {
182     return true;
183   } else {
184     return false;
185   }
186 }
187
188 function is_valid_ip ($ip) {
189   if (ereg("^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$", $ip ) ) {
190       // it's at least in the right format, now check to see if
191       // each part is equal to less than 255
192       $parts= explode( '.', $ip );
193       $count= count($parts);
194
195       for( $i= 0; $i < $count; $i++ ) {
196         if( intval($parts[$i]) > 255 )
197           return false;
198       }
199
200       return true;
201   } else {
202     return false;
203   }
204 }
205
206 function is_valid_network_addr($network_addr,$mask) {
207   $lNetwork= ip2long($network_addr);
208   $lMask= ip2long($mask);
209
210   // are they the correct format?
211   if( $lNetwork == -1 || $lMask == -1 )
212     return false;
213
214   // is network address valid for the mask?
215   if( ($lNetwork & $lMask) != $lNetwork )
216     return false;
217
218   return true;
219 }
220
221
222 // returns whether or not a network address is in the reserved space
223 // in the case of a invalid network address, false will be returned.
224 function is_reserved_network_addr($network_addr) {
225   $lNetwork= ip2long($network_addr);
226
227   if( $lNetwork == -1 )
228     return false;
229
230   // does the network address fall in a reserved block?
231   $reserved_ips = array (
232                          array('10.0.0.0','10.255.255.255'),
233                          array('172.16.0.0','172.31.0.0'),
234                          array('192.168.0.0','192.168.255.0')
235                          );
236   foreach ($reserved_ips as $r) {
237     $min = ip2long($r[0]);
238     $max = ip2long($r[1]);
239       
240     if (($lNetwork >= $min) && ($lNetwork <= $max))
241       return true;
242   }
243
244   return false;
245 }
246
247 ////////////////////////////////////////////////////////////  roles
248 function plc_role_global_hash ($api) {
249   $hash=array();
250   $roles=$api->GetRoles();
251   foreach ($roles as $role) {
252     $hash[$role['role_id']]=$role['name'];
253   }
254   return $hash;
255 }
256
257 //////////////////////////////////////////////////////////// nodegroups
258 // hash by 'tagname=value'
259 function plc_nodegroup_global_hash ($api,$tagnames=NULL) {
260   $filter=NULL;
261   // xxx somehow this does not work; I've checked that the feature is working from plcsh
262   // but I suspect the php marshalling or something; no time to fix, get all nodegroups for now
263   // if ($tagnames) $filter=array("tagname"=>$tagnames);
264   $nodegroups=$api->GetNodeGroups($filter);
265   $hash=array();
266   if ($nodegroups) foreach ($nodegroups as $nodegroup) {
267       $key=$nodegroup['tagname']."=".$nodegroup['value'];
268       $hash[$key]=$nodegroup;
269     }
270   return $hash;
271 }
272   
273 //////////////////////////////////////////////////////////// titles
274 function t_site($site) { return " on site " . $site['name'] . " (" . $site['login_base'] .")"; }
275 function t_slice ($slice) { return " running slice " . $slice['name'] . " (" . $slice['slice_id'] . ")"; }
276
277 //////////////////////////////////////////////////////////// html fragments
278 function plc_vertical_table ($messages, $class="") {
279   // pretty print the cell
280   if ( empty( $messages) ) return "";
281   $formatted = "";
282   $formatted .= "<table";
283   if ($class) $formatted .= " class='" . $class . "'";
284   $formatted .= ">";
285   foreach ($messages as $message) {
286     $formatted .= "<tr><td>" . $message . "</td></tr>";
287   }
288   $formatted .= "</table>";
289   return $formatted;
290 }
291
292 function plc_error ($text) {
293   // should use the same channel as the php errors..
294   print "<div class='plc-error'> Error " . $text . "</div>";
295 }
296
297 function plc_errors ($errors) {
298   if ($errors) {
299     print( "<div class='plc-error'>" );
300     print( "<p>The following errors occured:</p>" );
301     print("<ul>");
302     foreach( $errors as $error ) 
303       print( "<li>$error</li>\n" );
304     print( "</ul></div>\n" );
305   }
306 }
307
308 function plc_warning_html ($text)       { return "<span class='plc-warning'>" . $text . "</span>";}
309 function plc_warning ($text)            { print plc_warning_html("Warning " . $text); }
310 function plc_foreign_html($text)        { return "<span class=plc-foreign>$text</span>"; }
311
312 // shows a php variable verbatim with a heading message
313 function plc_debug ($message,$object) {
314   print "<br>" . $message . "<pre>";
315   print_r ($object);
316   print "</pre>";
317 }
318
319 function truncate ($text,$numb,$etc = "...") {
320   if (strlen($text) > $numb) {
321     $text = substr($text, 0, $numb);
322     $text = $text.$etc;
323   }
324   return $text;
325 }
326 function html_div ($text,$class="") {
327   $html="<div";
328   if ($class) $html .= " class='$class'";
329   $html .= ">$text</div>";
330   return $html;
331 }
332
333 if (! function_exists ("drupal_set_error")) {
334   function drupal_set_error ($text) {
335     drupal_set_message ("<span class=error>$text</span>");
336   }
337  }
338
339 //////////////////////////////////////////////////////////// sort out for obsolete / trash
340 // builds a table from an array of strings, with the given class
341 // attempt to normalize the delete buttons and confirmations
342 function plc_delete_icon($width=15) {
343   return "<img width='$width' src='/planetlab/icons/delete.png'>";
344 }
345
346 function plc_bubble($text,$bubble) {
347   return "<span title='$bubble'>$text</span>";
348 }
349 function plc_delete_icon_bubble ($bubble,$width=15) {
350   return plc_bubble(plc_delete_icon($width),$bubble);
351 }
352
353 function plc_event_button($type,$param,$id) {
354   return '<a href="' . l_event($type,$param,$id) . '"> <span title="Related events"> <img src="/planetlab/icons/event.png" width=18></span></a>';
355 }
356
357 function plc_comon_button ($id_name, $id_value,$target="") {
358   $result='<a ';
359   if (!empty($target)) {
360     $result.='target="' . $target . '" ';
361   }
362   $result.='href="' . l_comon($id_name,$id_value) . '">';
363   $result.='<span title="Link to Comon"> <img src="/planetlab/icons/comon.png" width="18"></span></a>';
364   return $result;
365 }
366
367 ////////////////////
368 function plc_redirect ($url) {
369   header ("Location: " . $url);
370   exit ();
371 }
372
373 ?>