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