5 // will trash this eventually
6 require_once 'plc_functions_trash.php';
9 function my_is_int ($x) {
10 return (is_numeric($x) ? intval($x) == $x : false);
12 //////////////////////////////////////////////////////////// roles & other checks on global $plc
13 function plc_is_admin () {
15 return in_array( 10, $plc->person['role_ids']);
17 function plc_is_pi () {
19 return in_array( 20, $plc->person['role_ids']);
21 function plc_is_tech () {
23 return in_array( 40, $plc->person['role_ids']);
25 function plc_in_site ($site_id) {
27 return in_array( $site_id, $plc->person['site_ids']);
30 function plc_my_site_id () {
32 return $plc->person['site_ids'][0];
35 function plc_my_person_id () {
37 return $plc->person['person_id'];
40 //////////////////////////////////////////////////////////// links
41 function href ($url,$text) { return "<a href='" . $url . "'>" . $text . "</a>"; }
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
49 function l_actions () { return "/db/actions.php"; }
50 // some complex node actions are kept separate, e.g. the ones related to getbootmedium
51 function l_actions_download () { return "/db/nodes/node_downloads.php"; }
53 function l_nodes () { return "/db/nodes/index.php"; }
54 function l_nodes_peer ($peer_id) { return "/db/nodes/index.php?peerscope=$peer_id"; }
55 function l_node ($node_id) { return "/db/nodes/node.php?id=$node_id"; }
56 function l_node_t ($node_id,$text) { return href (l_node($node_id),$text); }
57 function l_node_add () { return "/db/nodes/node_add.php"; }
58 function l_nodes_site ($site_id) { return "/db/nodes/index.php?site_id=$site_id"; }
60 function l_interface ($interface_id) { return "/db/nodes/interfaces.php?id=$interface_id"; }
61 function l_interface_t ($interface_id,$text) {
62 return href (l_interface($interface_id),$text); }
63 function l_interface_add($node_id) { return "/db/nodes/interfaces.php?node_id=$node_id"; }
65 function l_sites () { return "/db/sites/index.php"; }
66 function l_sites_peer ($peer_id) { return "/db/sites/index.php?peerscope=$peer_id"; }
67 function l_site ($site_id) { return "/db/sites/index.php?id=$site_id"; }
68 function l_site_t ($site_id,$text) { return href (l_site($site_id),$text); }
69 function l_site_update($site_id) { return "/db/sites/site_update.php?site_id=$site_id"; }
71 function l_slices () { return "/db/slices/index.php"; }
72 function l_slices_peer ($peer_id) { return "/db/slices/index.php?peerscope=$peer_id"; }
73 function l_slice ($slice_id) { return "/db/slices/index.php?id=$slice_id"; }
74 function l_slice_t ($slice_id,$text) { return href (l_slice($slice_id),$text); }
75 function l_slice_add () { return "/db/slices/add_slice.php"; }
76 function l_slices_site($site_id) { return "/db/slices/index.php?site_id=$site_id"; }
78 function l_slice_obj ($slice) { return l_slice_t ($slice['slice_id'],$slice['name']); }
80 function l_sliver ($node_id,$slice_id) { return "/db/nodes/slivers.php?node_id=$node_id&slice_id=$slice_id"; }
81 function l_sliver_t ($node_id,$slice_id,$text) {
82 return href (l_sliver($node_id,$slice_id),$text) ; }
84 function l_persons () { return "/db/persons/index.php"; }
85 function l_persons_peer ($peer_id) { return "/db/persons/index.php?peerscope=$peer_id"; }
86 function l_person ($person_id) { return "/db/persons/index.php?id=$person_id"; }
87 function l_person_t ($person_id,$text) { return href (l_person($person_id),$text); }
88 function l_persons_site ($site_id) { return "/db/persons/index.php?site_id=$site_id"; }
90 function l_tags () { return "/db/tags/index.php"; }
91 function l_tag ($tag_type_id) { return "/db/tags/index.php"; }
92 function l_tag_add() { return "/db/tags/tag_form.php"; }
93 function l_tag_update($id) { return "/db/tags/tag_form.php&action=update-tag-type&id=$id"; }
95 function l_nodegroups () { return "/db/tags/nodegroups.php"; }
96 function l_nodegroup ($nodegroup_id) { return "/db/tags/nodegroup.php?id=$nodegroup_id"; }
97 function l_nodegroup_t ($nodegroup_id,$text) {
98 return href(l_nodegroup($nodegroup_id),$text); }
100 function l_events () { return "/db/events/index.php"; }
101 function l_event ($type,$param,$id) { return "/db/events/index.php?type=$type&$param=$id"; }
103 function l_peers() { return "/db/peers/index.php"; }
104 function l_peer($peer_id) { return "/db/peers/index.php?id=$peer_id"; }
105 function l_peer_t($peer_id,$text) { return href(l_peer($peer_id),$text); }
107 function l_comon($id_name,$id_value) { return "/db/nodes/comon.php?$id_name=$id_value"; }
108 function l_sirius() { return "/db/sirius/index.php"; }
109 function l_about() { return "/db/about.php"; }
110 function l_doc_plcapi() { return "/db/doc/PLCAPI.php"; }
111 function l_doc_nmapi() { return "/db/doc/NMAPI.php"; }
112 function l_admin() { return "/db/adminsearch.php"; }
114 function l_login() { return "/db/login.php"; }
115 function l_logout() { return "/planetlab/logout.php"; }
116 function l_sulogout() { return "/planetlab/sulogout.php"; }
117 function l_reset_password() { return "/db/persons/reset_password.php"; }
118 function l_person_register() { return "/db/persons/register.php"; }
119 function l_site_register() { return "/db/sites/register.php"; }
120 function l_sites_pending() { return "/db/sites/join_request.php"; }
122 function tabs_events() { return array('image'=>'/planetlab/icons/event.png','height'=>18);}
123 function tabs_comon() { return array('image'=>'/planetlab/icons/comon.png','height'=>18);}
125 // returns array ['url' => path, 'values' => hash (key=>value)* ]
126 function split_url ($full_url) {
127 list($url,$args) = explode("?",$full_url);
130 $pairs=explode("&",$args);
131 foreach ($pairs as $pair) {
132 list ($name,$value) = explode("=",$pair);
133 $values[$name]=$value;
136 return array("url"=>$url,"values"=>$values);
139 //////////////////////////////////////////////////////////// validation functions
140 function topdomain ($hostname) {
141 $exploded=array_reverse(explode(".",$hostname));
145 function is_valid_email_addr ($email) {
146 if (ereg("^.+@.+\\..+$", $email) ) {
153 function is_valid_url ($url) {
154 if (ereg("^(http|https)://.+\..+$", strtolower($url) ) ) {
161 function is_valid_ip ($ip) {
162 if (ereg("^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$", $ip ) ) {
163 // it's at least in the right format, now check to see if
164 // each part is equal to less than 255
165 $parts= explode( '.', $ip );
166 $count= count($parts);
168 for( $i= 0; $i < $count; $i++ ) {
169 if( intval($parts[$i]) > 255 )
179 function is_valid_network_addr($network_addr,$mask) {
180 $lNetwork= ip2long($network_addr);
181 $lMask= ip2long($mask);
183 // are they the correct format?
184 if( $lNetwork == -1 || $lMask == -1 )
187 // is network address valid for the mask?
188 if( ($lNetwork & $lMask) != $lNetwork )
195 // returns whether or not a network address is in the reserved space
196 // in the case of a invalid network address, false will be returned.
197 function is_reserved_network_addr($network_addr) {
198 $lNetwork= ip2long($network_addr);
200 if( $lNetwork == -1 )
203 // does the network address fall in a reserved block?
204 $reserved_ips = array (
205 array('10.0.0.0','10.255.255.255'),
206 array('172.16.0.0','172.31.0.0'),
207 array('192.168.0.0','192.168.255.0')
209 foreach ($reserved_ips as $r) {
210 $min = ip2long($r[0]);
211 $max = ip2long($r[1]);
213 if (($lNetwork >= $min) && ($lNetwork <= $max))
220 //////////////////////////////////////////////////////////// roles
221 function plc_role_global_hash ($api) {
223 $roles=$api->GetRoles();
224 foreach ($roles as $role) {
225 $hash[$role['role_id']]=$role['name'];
231 //////////////////////////////////////////////////////////// nodegroups
232 // hash by 'tagname=value'
233 function plc_nodegroup_global_hash ($api,$tagnames=NULL) {
235 // xxx somehow this does not work; I've checked that the feature is working from plcsh
236 // but I suspect the php marshalling or something; no time to fix, get all nodegroups for now
237 // if ($tagnames) $filter=array("tagname"=>$tagnames);
238 $nodegroups=$api->GetNodeGroups($filter);
240 if ($nodegroups) foreach ($nodegroups as $nodegroup) {
241 $key=$nodegroup['tagname']."=".$nodegroup['value'];
242 $hash[$key]=$nodegroup;
247 //////////////////////////////////////////////////////////// titles
248 function t_site($site) { return " on site " . $site['name'] . " (" . $site['login_base'] .")"; }
249 function t_slice ($slice) { return " running slice " . $slice['name'] . " (" . $slice['slice_id'] . ")"; }
251 //////////////////////////////////////////////////////////// nav tabs
252 function tabs_node($node) { return array('Node ' . $node['hostname']=>l_node($node_id)); }
253 function tabs_site($site) { return array('Site ' . $site['name']=>l_site($site_id)); }
254 function tabs_slice($slice) { return array('Slice ' . $slice['name']=>l_slice($slice_id)); }
256 //////////////////////////////////////////////////////////// html fragments
257 function plc_vertical_table ($messages, $class="") {
258 // pretty print the cell
259 if ( empty( $messages) ) return "";
261 $formatted .= "<table";
262 if ($class) $formatted .= " class='" . $class . "'";
264 foreach ($messages as $message) {
265 $formatted .= "<tr><td>" . $message . "</td></tr>";
267 $formatted .= "</table>";
271 function plc_section ($text,$line=true) {
272 if ($line) { print "<hr/>";}
273 print "<h2 class=plc> $text </h2>\n";
276 function plc_error ($text) {
277 // should use the same channel as the php errors..
278 print "<div class='plc-error'> Error " . $text . "</div>";
281 function plc_errors ($errors) {
283 print( "<div class='plc-error'>" );
284 print( "<p>The following errors occured:</p>" );
286 foreach( $errors as $error )
287 print( "<li>$error</li>\n" );
288 print( "</ul></div>\n" );
292 function plc_warning_html ($text) { return "<span class='plc-warning'>" . $text . "</span>";}
293 function plc_warning ($text) { print plc_warning_html("Warning " . $text); }
294 function plc_foreign_html($text) { return "<span class=plc-foreign>$text</span>"; }
296 // shows a php variable verbatim with a heading message
297 function plc_debug ($message,$object) {
298 print "<br>" . $message . "<pre>";
303 function truncate ($text,$numb,$etc = "...") {
304 if (strlen($text) > $numb) {
305 $text = substr($text, 0, $numb);
311 if (! function_exists ("drupal_set_error")) {
312 function drupal_set_error ($text) {
313 drupal_set_message ("<span class=error>$text</span>");
317 //////////////////////////////////////////////////////////// sort out for obsolete / trash
318 // builds a table from an array of strings, with the given class
319 // attempt to normalize the delete buttons and confirmations
320 function plc_delete_icon($width=15) {
321 return '<span title="Delete this entry"><img width=' . $width . ' alt="Delete this entry" src="/planetlab/icons/delete.png"></span>';
324 function plc_js_confirm($message) {
325 return "onclick=\"javascript:return confirm('Are you sure you want to delete " . $message . " ?')\"";
328 function plc_delete_link($url,$delete_message,$visible) {
329 return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . $visible . "</a>";
332 function plc_delete_link_button($url,$delete_message,$width=15) {
333 return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . plc_delete_icon($width) . "</a>";
336 function plc_event_button($type,$param,$id) {
337 return '<a href="' . l_event($type,$param,$id) . '"> <span title="Related events"> <img src="/planetlab/icons/event.png" width=18></span></a>';
340 function plc_comon_button ($id_name, $id_value,$target="") {
342 if (!empty($target)) {
343 $result.='target="' . $target . '" ';
345 $result.='href="' . l_comon($id_name,$id_value) . '">';
346 $result.='<span title="Link to Comon"> <img src="/planetlab/icons/comon.png" width="18"></span></a>';
351 function plc_redirect ($url) {
352 header ("Location: " . $url);