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