bugfix: the slice page was broken when nobody is in slice
[plewww.git] / planetlab / common / actions.php
1 <?php
2
3 // Require login
4 require_once 'plc_login.php';
5
6 // Get session and API handles
7 require_once 'plc_session.php';
8 require_once 'plc_api.php';
9 global $plc, $api, $adm;
10
11 //print header
12 require_once 'plc_drupal.php';
13
14 // Common functions
15 require_once 'plc_functions.php';
16
17 $known_actions = array();
18 ////////////////////////////////////////////////////////////
19 // interface :
20 // (*) use POST
21 // (*) set 'action' to one of the following
22 //////////////////////////////////////// persons
23 $known_actions[] = "add-person-to-site";
24 //    expects:    person_id & site_id
25 $known_actions[] = "remove-person-from-sites";
26 //    expects:    person_id & site_ids
27 $known_actions[] = "remove-roles-from-person";
28 //    expects:    person_id & role_ids
29 $known_actions[] = "add-role-to-person";
30 //    expects:    role_person_id & id
31 $known_actions[] = "enable-person";
32 //    expects:    person_id
33 $known_actions[] = "disable-person";
34 //    expects:    person_id
35 $known_actions[] = "become-person";
36 //    expects:    person_id
37 $known_actions[] = "delete-person";
38 //    expects:    person_id
39 $known_actions[] = "delete-keys";
40 //    expects:    key_ids & person_id (for redirecting to the person's page)
41 $known_actions[] = "upload-key";
42 //    expects:    person_id & $_FILES['key']
43 $known_actions[] = "update-person";
44 //    expects:    person_id & first_name last_name title email phone url bio + [password1 password2]
45
46 //////////////////////////////////////// nodes
47 $known_actions[] = "node-boot-state";
48 //    expects:    node_id boot_state
49 $known_actions[] = "delete-node";
50 //    expects:    node_id
51 $known_actions[] = "update-node";
52 //    expects:    node_id, hostname, model, node_type
53 $known_actions[] = "attach-pcu";
54 //    expects:    node_id, pcu_id, port (pcu_id <0 means detach)
55 $known_actions[] = "reboot-node-with-pcu";
56 //    expects:    node_id
57
58 //////////////////////////////////////// interfaces
59 $known_actions[] = "delete-interfaces";
60 //    expects:    interface_ids
61 $known_actions[] = "add-interface";
62 //    expects:    node_id & interface details
63 $known_actions[] = "new-interface";
64 //    expects:    node_id
65 $known_actions[] = "update-interface";
66 //    expects:    interface_id & interface details
67
68 //////////////////////////////////////// sites
69 $known_actions[] = "delete-site";
70 //    expects:    site_id
71 $known_actions[] = "expire-all-slices-in-site";
72 //    expects:    slice_ids
73 $known_actions[] = "update-site";
74 //    expects:    site_id & name abbreviated_name url latitude longitude [login_base max_slices]
75
76 //////////////////////////////////////// slices
77 $known_actions[] = "delete-slice";
78 //      expects:        slice_id
79 $known_actions[] = "update-slice";
80 //    expects:    slice_id, name, description, url
81 $known_actions[] = "renew-slice";
82 //    expects:    slice_id & expires
83 $known_actions[] = 'remove-persons-from-slice';
84 //    expects:    slice_id & person_ids
85 $known_actions[] = 'add-persons-in-slice';
86 //    expects:    slice_id & person_ids
87 $known_actions[] = 'remove-nodes-from-slice';
88 //    expects:    slice_id & node_ids
89 $known_actions[] = 'add-nodes-in-slice';
90 //    expects:    slice_id & node_ids
91 $known_actions[] = 'update-initscripts';
92 //    expects:    slice_id & name & previous-initscript & previous-initscript-code
93 //            & initscript & initscript-code
94 $known_actions[] = 'delete-slice-tags';
95 //      expects:        slice_tag_id
96 $known_actions[] = 'add-slice-tag';
97 //      expects:        slice_id & tag_type_id & node_id & nodegroup_id
98
99 //////////////////////////////////////// tag types
100 $known_actions[] = "update-tag-type";
101 //    expects:    tag_type_id & name & description & category & min_role_id
102 $known_actions[] = "add-tag-type";
103 //    expects:    tag_type_id & tagname & description & category & min_role_id
104 $known_actions[] = "delete-tag-types";
105 //    expects:    tag_type_ids
106 $known_actions[] = "remove-roles-from-tag-type";
107 //    expects:    tag_type_id & role_ids
108 $known_actions[] = "add-role-to-tag-type";
109 //    expects:    tag_type_id_id & id
110
111 //////////////////////////////////////// tags
112 $known_actions[] = "set-tag-on-node";
113 //    expects:    node_id tagname value
114 $known_actions[] = "set-tag-on-site";
115 //    expects:    site_id tagname value
116 $known_actions[] = "set-tag-on-person";
117 //    expects:    person_id tagname value
118 $known_actions[] = "set-tag-on-interface";
119 //    expects:    interface_id tagname value
120 $known_actions[] = "delete-node-tags";
121 //    expects:    node_id & node_tag_ids
122 $known_actions[] = "delete-site-tags";
123 //    expects:    site_id & site_tag_ids
124 $known_actions[] = "delete-person-tags";
125 //    expects:    person_id & person_tag_ids
126 $known_actions[] = "delete-interface-tags";
127 //    expects:    interface_id & interface_tag_ids
128
129 //////////////////////////////////////// nodegroups
130 $known_actions[] = "update-nodegroup";
131 //    expects nodegroup_id groupname value
132 $known_actions[] = "add-nodegroup";
133 //    expects groupname, tag_type_id, value
134 $known_actions[] = 'delete-nodegroups';
135 //    expects nodegroup_ids
136
137 //////////////////////////////////////// leases
138 $known_actions[] = "manage-leases";
139 //    expects as 'actions' a list of 'action' of the form
140 //      either [ 'add-leases', [nodenames], slicename, t_from, t_until ]
141 //      or     [ 'delete-leases', lease_id ]
142
143 ////////////////////////////////////////////////////////////
144 $interface_details = array('method', 'type', 'ip', 'gateway', 'network',
145     'broadcast', 'netmask', 'dns1', 'dns2',
146     'hostname', 'mac', 'bwlimit');
147
148 //////////////////////////////
149 // sometimes we don't set 'action', but use the submit button name instead
150 // so if 'action' not set, see if $_POST has one of the actions as a key
151 if ($_POST['action']) {
152     $action = $_POST['action'];
153 } else {
154     foreach ($known_actions as $known_action) {
155         if ($_POST[$known_action]) {
156             $action = $known_action;
157             break;
158         }
159     }
160 }
161
162 //uncomment for debugging incoming data
163 //$action='debug';
164
165 $person_id = $_POST['person_id']; // usually needed
166
167 if (!$action) {
168     drupal_set_message("actions.php: action not set or not in known_actions");
169     plc_debug('POST', $_POST);
170     return;
171 }
172
173 switch ($action) {
174
175     case 'add-person-to-site':{
176             $site_id = $_POST['site_id'];
177             $api->AddPersonToSite(intval($person_id), intval($site_id));
178             plc_redirect(l_person($person_id));
179         }
180
181     case 'remove-person-from-sites':{
182             $site_ids = $_POST['site_ids'];
183             if (!$site_ids) {
184                 drupal_set_message("action=$action - No site selected");
185                 return;
186             }
187             foreach ($site_ids as $site_id) {
188                 $api->DeletePersonFromSite(intval($person_id), intval($site_id));
189             }
190             plc_redirect(l_person($person_id));
191         }
192
193     case 'remove-roles-from-person':{
194             $role_ids = $_POST['role_ids'];
195             if (!$role_ids) {
196                 drupal_set_error("You have not selected role(s) to remove");
197             } else {
198                 foreach ($role_ids as $role_id) {
199                     if ($api->DeleteRoleFromPerson(intval($role_id), intval($person_id)) != 1) {
200                         drupal_set_error("Could not remove role $role_id from person $person_id");
201                     }
202                 }
203
204             }
205             plc_redirect(l_person_roles($person_id));
206         }
207
208     case 'add-role-to-person':{
209             $role_id = $_POST['role_id'];
210             if (!$role_id) {
211                 drupal_set_error("You have not selected a role to add");
212             } else if ($api->AddRoleToPerson(intval($role_id), intval($person_id)) != 1) {
213                 drupal_set_error("Could not add role $role_id to person $person_id");
214             }
215             plc_redirect(l_person_roles($person_id));
216         }
217
218     case 'enable-person':{
219             $fields = array("enabled" => true);
220             $api->UpdatePerson(intval($person_id), $fields);
221             plc_redirect(l_person($person_id));
222         }
223
224     case 'disable-person':{
225             $fields = array("enabled" => false);
226             $api->UpdatePerson(intval($person_id), $fields);
227             plc_redirect(l_person($person_id));
228         }
229
230     case 'become-person':{
231             $plc->BecomePerson(intval($person_id));
232             plc_redirect(l_person(intval($person_id)));
233         }
234
235     case 'delete-person':{
236             $api->DeletePerson(intval($person_id));
237             plc_redirect(l_persons());
238         }
239
240     case 'delete-keys':{
241             $key_ids = $_POST['key_ids'];
242             if (!$key_ids) {
243                 drupal_set_message("action=$action - No key selected");
244                 return;
245             }
246             $success = true;
247             $counter = 0;
248             foreach ($key_ids as $key_id) {
249                 if ($api->DeleteKey(intval($key_id)) != 1) {
250                     $success = false;
251                 } else {
252                     $counter++;
253                 }
254
255             }
256             if ($success) {
257                 drupal_set_message("Deleted $counter key(s)");
258             } else {
259                 drupal_set_error("Could not delete all selected keys, only $counter were removed");
260             }
261
262             plc_redirect(l_person($person_id));
263         }
264
265     case 'upload-key':{
266             if (!isset($_FILES['key'])) {
267                 drupal_set_message("action=$action, no key file set");
268                 return;
269             }
270
271             $key_file = $_FILES['key']['tmp_name'];
272             if (!$key_file) {
273                 plc_error("Please select a valid SSH key file to upload");
274                 return;
275             }
276             $fp = fopen($key_file, "r");
277             $key = "";
278             if (!$fp) {
279                 plc_error("Unable to open key file $key_file");
280                 return;
281             }
282             // opened the key file, read the one line of contents
283             // The POST operation always creates a file even if the filename
284             // the user specified was garbage.  If there was some problem
285             // with the source file, we'll get a zero length read here.
286             $key = fread($fp, filesize($key_file));
287             fclose($fp);
288
289             $key_id = $api->AddPersonKey(intval($person_id), array("key_type" => 'ssh', "key" => $key));
290
291             if ($key_id >= 1) {
292                 drupal_set_message("New key added");
293             } else {
294                 drupal_set_error("Could not add key, please verify your SSH file content\n" . $api->error());
295             }
296
297             plc_redirect(l_person($person_id));
298         }
299
300     case 'update-person':{
301             $person_id = $_POST['person_id'];
302             // attempt to update this person
303             $first_name = $_POST['first_name'];
304             $last_name = $_POST['last_name'];
305             $title = $_POST['title'];
306             $email = $_POST['email'];
307             $phone = $_POST['phone'];
308             $url = $_POST['url'];
309             $bio = str_replace("\r", "", $_POST['bio']);
310             $password1 = $_POST['password1'];
311             $password2 = $_POST['password2'];
312
313             if ($password1 != $password2) {
314                 drupal_set_error("The passwords do not match");
315                 plc_redirect(l_person($person_id));
316             }
317
318             $fields = array();
319             $fields['first_name'] = $first_name;
320             $fields['last_name'] = $last_name;
321             $fields['title'] = $title;
322             $fields['email'] = $email;
323             $fields['phone'] = $phone;
324             $fields['url'] = $url;
325             $fields['bio'] = $bio;
326
327             if ($password1 != "") {
328                 $fields['password'] = $password1;
329             }
330
331             if ($api->UpdatePerson(intval($person_id), $fields) == 1) {
332                 drupal_set_message("$first_name $last_name updated");
333             } else {
334                 drupal_set_error("Could not update person $person_id" . $api->error());
335             }
336
337             plc_redirect(l_person($person_id));
338             break;
339         }
340
341 //////////////////////////////////////////////////////////// nodes
342     case 'node-boot-state':{
343             $node_id = intval($_POST['node_id']);
344             $boot_state = $_POST['boot_state'];
345             $result = $api->UpdateNode($node_id, array("boot_state" => $boot_state));
346             if ($result == 1) {
347                 drupal_set_message("boot state updated");
348                 plc_redirect(l_node($node_id));
349             } else {
350                 drupal_set_error("Could not set boot_state '$boot_state'");
351             }
352             break;
353         }
354
355     case 'delete-node':{
356             $node_id = intval($_POST['node_id']);
357             $result = $api->DeleteNode(intval($node_id));
358             if ($api == 1) {
359                 drupal_set_message("Node $node_id deleted");
360                 plc_redirect(l_nodes());
361             } else {
362                 drupal_set_error("Could not delete node $node_id");
363             }
364             break;
365         }
366
367     case 'update-node':{
368             $node_id = intval($_POST['node_id']);
369             $hostname = $_POST['hostname'];
370             $model = $_POST['model'];
371             $node_type = $_POST['node_type'];
372
373             $fields = array("hostname" => $hostname, "model" => $model, "node_type" => $node_type);
374             $api->UpdateNode($node_id, $fields);
375             $error = $api->error();
376
377             if (empty($error)) {
378                 drupal_set_message("Update node $hostname");
379                 plc_redirect(l_node($node_id));
380             } else {
381                 drupal_set_error($error);
382             }
383             break;
384         }
385
386     // this code will ensure that at most one PCU gets attached to the node
387     case 'attach-pcu':{
388             $node_id = intval($_POST['node_id']);
389             $pcu_id = intval($_POST['pcu_id']);
390             $port = intval($_POST['port']);
391             // always start with deleting former PCUs
392             $nodes = $api->GetNodes(array($node_id), array('pcu_ids'));
393             $former_pcu_ids = $nodes[0]['pcu_ids'];
394             if ($former_pcu_ids) {
395                 foreach ($former_pcu_ids as $former_pcu_id) {
396                     if ($api->DeleteNodeFromPCU($node_id, $former_pcu_id) == 1) {
397                         drupal_set_message('Detached node ' . $node_id . ' from PCU ' . $pcu_id);
398                     } else {
399                         drupal_set_error('Could not detach node ' . $node_id . ' from PCU ' . $pcu_id);
400                     }
401
402                 }
403             }
404
405             // re-attach only if provided pcu_id >=0
406             if ($pcu_id >= 0) {
407                 if ($api->AddNodeToPCU($node_id, $pcu_id, $port) == 1) {
408                     drupal_set_message('Attached node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
409                 } else {
410                     drupal_set_error('Failed to attach node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
411                 }
412
413             } else {
414                 drupal_set_message('Detached node from all PCUs');
415             }
416
417             plc_redirect(l_node($node_id));
418             break;
419         }
420
421     case 'reboot-node-with-pcu':{
422             $node_id = intval($_POST['node_id']);
423             $hostname = $_POST['hostname'];
424             $test = $_POST['test'];
425             settype($test, "boolean");
426
427             $ret = $api->RebootNodeWithPCU($node_id, $test);
428             $error = $api->error();
429
430             if (empty($error)) {
431                 drupal_set_message("Reboot node $hostname: $ret");
432                 plc_redirect(l_node($node_id));
433             } else {
434                 drupal_set_error($error);
435             }
436             break;
437         }
438
439 //////////////////////////////////////////////////////////// interfaces
440     case 'delete-interfaces':{
441             $interface_ids = $_POST['interface_ids'];
442             if (!$interface_ids) {
443                 drupal_set_message("action=$action - No interface selected");
444                 return;
445             }
446             $success = true;
447             $counter = 0;
448             foreach ($interface_ids as $interface_id) {
449                 if ($api->DeleteInterface(intval($interface_id)) != 1) {
450                     $success = false;
451                 } else {
452                     $counter++;
453                 }
454
455             }
456             if ($success) {
457                 drupal_set_message("Deleted $counter interface(s)");
458             } else {
459                 drupal_set_error("Could not delete all selected interfaces, only $counter were removed");
460             }
461
462             plc_redirect(l_node($_POST['node_id']));
463         }
464
465     case 'new-interface':{
466             plc_redirect(l_interface_add($_POST['node_id']));
467         }
468
469     case 'add-interface':{
470             $node_id = intval($_POST['node_id']);
471             foreach ($interface_details as $field) {
472                 $interface[$field] = $_POST[$field];
473                 // these must be integers
474                 if (in_array($field, array('bwlimit', 'node_id'))) {
475                     if (empty($interface[$field])) {
476                         unset($interface[$field]);
477                     } else {
478                         $interface[$field] = intval($interface[$field]);
479                     }
480
481                 }
482             }
483             $interface_id = $api->AddInterface($node_id, $interface);
484             if ($interface_id <= 0) {
485                 drupal_set_error("Could not create interface");
486                 drupal_set_error($api->error());
487             } else {
488                 $ip = $interface['ip'];
489                 drupal_set_message("Interface $ip added into node $node_id");
490                 if ($_POST['is-virtual']) {
491                     $ifname = $_POST['ifname'];
492                     if ($api->AddInterfaceTag($interface_id, "ifname", $ifname) <= 0) {
493                         drupal_set_error("Could not set tag 'ifname'=$ifname");
494                     } else {
495                         drupal_set_message("Set tag 'ifname'=$ifname");
496                     }
497
498                     $alias = $_POST['alias'];
499                     // deafult to interface_id
500                     if (!$alias) {
501                         $alias = strval($interface_id);
502                     }
503
504                     if ($api->AddInterfaceTag($interface_id, "alias", $alias) <= 0) {
505                         drupal_set_error("Could not set tag 'alias'=$alias");
506                     } else {
507                         drupal_set_message("Set tag 'alias'=$alias");
508                     }
509
510                 }
511             }
512             plc_redirect(l_node_interfaces($node_id));
513         }
514
515     case 'update-interface':{
516             $interface_id = $_POST['interface_id'];
517             foreach ($interface_details as $field) {
518                 $interface[$field] = $_POST[$field];
519                 if (in_array($field, array('bwlimit', 'node_id'))) {
520                     if (intval($interface[$field]) != 0) {
521                         $interface[$field] = intval($interface[$field]);
522                     } elseif ($field == 'bwlimit') {
523                         $interface[$field] = null;
524                     }
525                 }
526             }
527             $result = $api->UpdateInterface(intval($interface_id), $interface);
528             if ($result == 1) {
529                 drupal_set_message("Interface $interface_id updated");
530             } else {
531                 drupal_set_error("Could not update interface: " . $api->error());
532             }
533
534             plc_redirect(l_interface($interface_id));
535         }
536
537 //////////////////////////////////////////////////////////// sites
538     case 'delete-site':{
539             $site_id = intval($_POST['site_id']);
540             if ($api->DeleteSite($site_id) == 1) {
541                 drupal_set_message("Site $site_id deleted");
542             } else {
543                 drupal_set_error("Failed to delete site $site_id");
544             }
545
546             plc_redirect(l_sites());
547             break;
548         }
549
550     case 'expire-all-slices-in-site':{
551             // xxx todo
552             drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
553             return;
554
555             //// old code from sites/expire.php
556             $sites = $api->GetSites(array(intval($site_id)));
557             $site = $sites[0];
558             // xxx why not 'now?'
559             $expiration = strtotime($_POST['expires']);
560             // loop through all slices for site
561             foreach ($site['slice_ids'] as $slice_id) {
562                 $api->UpdateSlice($slice_id, array("expires" => $expiration));
563             }
564             // update site to not allow slice creation or renewal
565             $api->UpdateSite($site_id, array("max_slices" => 0));
566             plc_redirect(l_site($site_id));
567             break;
568         }
569
570     case 'update-site':{
571             $site_id = intval($_POST['site_id']);
572             $name = $_POST['name'];
573             $abbreviated_name = $_POST['abbreviated_name'];
574             $url = $_POST['url'];
575             $latitude = floatval($_POST['latitude']);
576             $longitude = floatval($_POST['longitude']);
577             //$max_slivers= $_POST['max_slivers'];
578
579             $fields = array("name" => $name,
580                 "abbreviated_name" => $abbreviated_name,
581                 "url" => $url,
582                 "latitude" => floatval($latitude),
583                 "longitude" => floatval($longitude));
584
585             if ($_POST['login_base']) {
586                 $fields['login_base'] = $_POST['login_base'];
587             }
588
589             if (isset($_POST['max_slices'])) {
590                 $fields['max_slices'] = intval($_POST['max_slices']);
591             }
592
593             if (isset($_POST['enabled'])) {
594                 $fields['enabled'] = (bool) $_POST['enabled'];
595             }
596
597             $retcod = $api->UpdateSite(intval($site_id), $fields);
598             if ($retcod == 1) {
599                 drupal_set_message("Site $name updated");
600             } else {
601                 drupal_set_error("Could not update site $site_id");
602             }
603
604             plc_redirect(l_site($site_id));
605             break;
606         }
607
608 //////////////////////////////////////////////////////////// slices
609     case 'delete-slice':{
610             $slice_id = $_POST['slice_id'];
611             if ($api->DeleteSlice(intval($slice_id)) == 1) {
612                 drupal_set_message("Slice $slice_id deleted");
613                 plc_redirect(l_slices());
614             } else {
615                 drupal_set_error("Could not delete slice $slice_id " . $api->error());
616             }
617             break;
618         }
619
620     case 'update-slice':{
621             $slice_id = $_POST['slice_id'];
622             $name = $_POST['name'];
623             $description = $_POST['description'];
624             $url = $_POST['url'];
625
626             $fields = array("description" => $description, "url" => $url);
627             $api->UpdateSlice(intval($slice_id), $fields);
628             $error = $api->error();
629
630             if (empty($error)) {
631                 drupal_set_message("Update slice $name");
632                 plc_redirect(l_slice($slice_id));
633             } else {
634                 drupal_set_error($error);
635             }
636             break;
637         }
638
639     case 'renew-slice': {
640             $slice_id = intval($_POST['slice_id']);
641             $expires = intval($_POST['expires']);
642             // 8 weeks from now
643             // xxx
644             $now = time();
645             $WEEK = 7 * 24 * 3600;
646             $WEEKS = 8;
647             $MAX_FUTURE = $WEEKS * $WEEK;
648             if (($expires - $now) > $MAX_FUTURE) {
649                 drupal_set_error("Cannot renew slice that far in the future, max is $WEEKS weeks from now");
650                 plc_redirect(l_slice($slice_id));
651             }
652             if ($api->UpdateSlice($slice_id, array('expires' => $expires)) == 1) {
653                 drupal_set_message("Slice renewed");
654             } else {
655                 drupal_set_error("Could not update slice $slice_id");
656             }
657
658             plc_redirect(l_slice($slice_id));
659             break;
660         }
661
662     case 'remove-persons-from-slice':{
663             $slice_id = intval($_POST['slice_id']);
664             $person_ids = $_POST['person_ids'];
665             $return_url = $_POST['return_url'];
666
667             if (!$return_url) {
668                 $return_url = l_slice($slice_id) . "&show_persons=true";
669             }
670
671             $slice_name = "";
672             $tmp_slices = $api->GetSlices($slice_id, array("name"));
673             if (count($tmp_slices) > 0) {
674                 $tmp_slice = $tmp_slices[0];
675                 $slice_name = $tmp_slice["name"];
676             }
677             $notify_subject = "Removed from slice: " . $slice_name;
678             $notify_body = sprintf("You have been removed from the slice %s.
679
680 Our support team will be glad to answer any question that you might have.
681 ", $slice_name);
682             $notify_person_ids = array();
683
684             $success = true;
685             $counter = 0;
686             foreach ($person_ids as $person_id) {
687                 if ($api->DeletePersonFromSlice(intval($person_id), $slice_id) != 1) {
688                     $success = false;
689                 } else {
690                     array_push($notify_person_ids, intval($person_id));
691                     $counter++;
692                 }
693             }
694             if ($success) {
695                 $adm->NotifyPersons($notify_person_ids, $notify_subject, $notify_body);
696                 drupal_set_message("Deleted $counter person(s)");
697             } else {
698                 drupal_set_error("Could not delete all selected persons, only $counter were removed");
699             }
700
701             plc_redirect($return_url);
702             break;
703         }
704
705     case 'add-persons-in-slice':{
706             $slice_id = intval($_POST['slice_id']);
707             $person_ids = $_POST['person_ids'];
708             $return_url = $_POST['return_url'];
709
710             if (!$return_url) {
711                 $return_url = l_slice($slice_id) . "&show_persons=true";
712             }
713
714             $slice_name = "";
715             $tmp_slices = $api->GetSlices($slice_id, array("name"));
716             if (count($tmp_slices) > 0) {
717                 $tmp_slice = $tmp_slices[0];
718                 $slice_name = $tmp_slice["name"];
719             }
720             $notify_subject = "Added to slice: " . $slice_name;
721             $notify_body = sprintf("You have been added to the slice %s as a user.
722
723 You can go to your slice page following the link below:
724 https://%s:%d/db/slices/index.php?id=%d
725
726 Our support team will be glad to answer any question that you might have.
727 ", $slice_name, PLC_WWW_HOST, PLC_WWW_SSL_PORT, $slice_id);
728             $notify_person_ids = array();
729
730             $success = true;
731             $counter = 0;
732             foreach ($person_ids as $person_id) {
733                 if ($api->AddPersonToSlice(intval($person_id), $slice_id) != 1) {
734                     $success = false;
735                 } else {
736                     array_push($notify_person_ids, intval($person_id));
737                     $counter++;
738                 }
739             }
740             if ($success) {
741                 $adm->NotifyPersons($notify_person_ids, $notify_subject, $notify_body);
742                 drupal_set_message("Added $counter person(s)");
743             } else {
744                 drupal_set_error("Could not add all selected persons, only $counter were added");
745             }
746
747             plc_redirect($return_url);
748             break;
749         }
750
751     case 'remove-nodes-from-slice':{
752             $slice_id = intval($_POST['slice_id']);
753             $node_ids = array_map("intval", $_POST['node_ids']);
754             $count = count($node_ids);
755
756             if ($api->DeleteSliceFromNodes($slice_id, $node_ids) == 1) {
757                 drupal_set_message("Removed $count node(s)");
758             } else {
759                 drupal_set_error("Could not remove selected nodes");
760             }
761
762             plc_redirect(l_slice_nodes($slice_id));
763             break;
764         }
765
766     case 'add-nodes-in-slice':{
767             $slice_id = intval($_POST['slice_id']);
768             $node_ids = array_map("intval", $_POST['node_ids']);
769             $count = count($node_ids);
770             if ($api->AddSliceToNodes($slice_id, $node_ids) == 1) {
771                 drupal_set_message("Added $count node(s)");
772             } else {
773                 drupal_set_error("Could not add all selected nodes");
774             }
775
776             plc_redirect(l_slice_nodes($slice_id));
777             break;
778         }
779
780     case 'update-initscripts':{
781 //    expects:    slice_id & name & previous-initscript & previous-initscript-code
782             //            & initscript & initscript-code
783             $slice_id = intval($_POST['slice_id']);
784             $previous_initscript = $_POST['previous-initscript'];
785             $initscript = $_POST['initscript'];
786             $previous_initscript_code = html_entity_decode($_POST['previous-initscript-code']);
787             $initscript_code = $_POST['initscript-code'];
788
789             $changes = false;
790             if (strcmp($initscript, $previous_initscript) != 0) {
791                 $newvalue = $api->SetSliceInitscript($slice_id, $initscript);
792                 $status = (strcmp($newvalue, $initscript) == 0) ? "OK" : "failed";
793                 if (!$initscript) {
794                     drupal_set_message("Removed shared initscript '" . $previous_initscript . "' " . $status);
795                 } else {
796                     drupal_set_message("Replaced shared initscript with '" . $initscript . "' " . $status);
797                 }
798
799                 $changes = true;
800             }
801
802             // somehow some \r chars make it here; just ignore them
803             $previous_initscript_code = str_replace("\r", "", $previous_initscript_code);
804             //   plc_debug_txt('previous initscript_code after cr',$previous_initscript_code);
805
806             $initscript_code = str_replace("\r", "", $initscript_code);
807             // make sure the script ends with a single \n
808             $initscript_code = trim($initscript_code);
809             if (!empty($initscript_code) && $initscript_code[strlen($initscript_code) - 1] != "\n") {
810                 $initscript_code .= "\n";
811             }
812
813             // plc_debug_txt('initscript_code after cr & nl/eof',$initscript_code);
814
815             if (strcmp($initscript_code, $previous_initscript_code) != 0) {
816                 $newvalue = $api->SetSliceInitscriptCode($slice_id, $initscript_code);
817                 // plc_debug_txt('newvalue',$newvalue);
818                 $status = (strcmp($newvalue, $initscript_code) == 0) ? "OK" : "failed";
819                 if (!$initscript_code) {
820                     drupal_set_message("Removed initscript code " . $status);
821                 } else {
822                     drupal_set_message("Installed new initscript code " . $status);
823                 }
824
825                 $changes = true;
826             }
827             if (!$changes) {
828                 drupal_set_message("No changes required in initscript");
829             }
830
831             plc_redirect(l_slice($slice_id) . "&show_details=0&show_initscripts=1");
832             break;
833         }
834
835     case 'delete-slice-tags':{
836             $slice_id = intval($_POST['slice_id']);
837             $slice_tag_ids = array_map("intval", $_POST['slice_tag_ids']);
838             $count = 0;
839             $success = true;
840             foreach ($slice_tag_ids as $slice_tag_id) {
841                 if ($api->DeleteSliceTag($slice_tag_id)) {
842                     $count += 1;
843                 } else {
844                     drupal_set_error("Could not delete slice tag: slice_tag_id = $slice_tag_id");
845                     $success = false;
846                 }
847             }
848             if ($success) {
849                 drupal_set_message("Deleted $count slice tag(s)");
850             }
851
852             plc_redirect(l_slice($slice_id) . "&show_tags=1");
853             break;
854         }
855
856     case 'add-slice-tag':{
857             $slice_id = intval($_POST['slice_id']);
858             $tag_type_id = intval($_POST['tag_type_id']);
859             $value = $_POST['value'];
860             $node_id = intval($_POST['node_id']);
861             $nodegroup_id = intval($_POST['nodegroup_id']);
862
863             $result = null;
864             if ($node_id) {
865                 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, $node_id);
866             } elseif ($nodegroup_id) {
867                 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, null, $nodegroup_id);
868             } else {
869                 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value);
870             }
871             if ($result) {
872                 drupal_set_message("Added slice tag.");
873             } else {
874                 drupal_set_error("Could not add slice tag");
875             }
876
877             if ($_POST['sliver_action']) {
878                 plc_redirect(l_sliver($node_id, $slice_id));
879             } else {
880                 plc_redirect(l_slice($slice_id) . "&show_tags=true");
881             }
882
883             break;
884         }
885
886 //////////////////////////////////////////////////////////// tag types
887
888     case 'update-tag-type':{
889             // get post vars
890             $tag_type_id = intval($_POST['tag_type_id']);
891             $tagname = $_POST['tagname'];
892             $min_role_id = intval($_POST['min_role_id']);
893             $description = $_POST['description'];
894             $category = $_POST['category'];
895
896             // make tag_type_fields dict
897             $tag_type_fields = array("min_role_id" => $min_role_id,
898                 "tagname" => $tagname,
899                 "description" => $description,
900                 "category" => $category,
901             );
902
903             if ($api->UpdateTagType($tag_type_id, $tag_type_fields) == 1) {
904                 drupal_set_message("Tag type $tagname updated");
905             } else {
906                 drupal_set_error("Could not update tag type $tag_type_id\n" . $api->error());
907             }
908
909             plc_redirect(l_tag($tag_type_id));
910             break;
911         }
912
913     case 'add-tag-type':{
914             // get post vars
915             $tagname = $_POST['tagname'];
916             $min_role_id = intval($_POST['min_role_id']);
917             $description = $_POST['description'];
918             $category = $_POST['category'];
919
920             // make tag_type_fields dict
921             $tag_type_fields = array("min_role_id" => $min_role_id,
922                 "tagname" => $tagname,
923                 "description" => $description,
924                 "category" => $category,
925             );
926
927             // Add it!
928             $tag_type_id = $api->AddTagType($tag_type_fields);
929             if ($tag_type_id > 0) {
930                 drupal_set_message("tag type $tag_type_id created");
931             } else {
932                 drupal_set_error("Could not create tag type $tagname");
933             }
934
935             plc_redirect(l_tags());
936             break;
937         }
938
939     case 'delete-tag-types':{
940             $tag_type_ids = $_POST['tag_type_ids'];
941             if (!$tag_type_ids) {
942                 drupal_set_message("action=$action - No tag selected");
943                 return;
944             }
945             $success = true;
946             $counter = 0;
947             foreach ($tag_type_ids as $tag_type_id) {
948                 if ($api->DeleteTagType(intval($tag_type_id)) != 1) {
949                     $success = false;
950                 } else {
951                     $counter++;
952                 }
953             }
954
955             if ($success) {
956                 drupal_set_message("Deleted $counter tag(s)");
957             } else {
958                 drupal_set_error("Could not delete all selected tags, only $counter were removed");
959             }
960
961             plc_redirect(l_tags());
962             break;
963         }
964
965     case 'remove-roles-from-tag-type':{
966             $tag_type_id = $_POST['tag_type_id'];
967             $role_ids = $_POST['role_ids'];
968             if (!$role_ids) {
969                 drupal_set_error("You have not selected role(s) to remove");
970             } else {
971                 foreach ($role_ids as $role_id) {
972                     if ($api->DeleteRoleFromTagType(intval($role_id), intval($tag_type_id)) != 1) {
973                         drupal_set_error("Could not remove role $role_id from tag type $tag_type_id");
974                     }
975                 }
976
977             }
978             plc_redirect(l_tag_roles($tag_type_id));
979         }
980
981     case 'add-role-to-tag-type':{
982             $tag_type_id = $_POST['tag_type_id'];
983             $role_id = $_POST['role_id'];
984             if (!$role_id) {
985                 drupal_set_error("You have not selected a role to add");
986             } else if ($api->AddRoleToTagType(intval($role_id), intval($tag_type_id)) != 1) {
987                 drupal_set_error("Could not add role $role_id to tag $tag_type_id");
988             }
989             plc_redirect(l_tag_roles($tag_type_id));
990         }
991
992 //////////////////////////////////////// tags
993     case 'set-tag-on-node':
994     case 'set-tag-on-site':
995     case 'set-tag-on-person':
996     case 'set-tag-on-interface':
997         {
998
999             $mode = null;
1000             if ($action == 'set-tag-on-node') {
1001                 $mode = 'node';
1002             }
1003
1004             if ($action == 'set-tag-on-site') {
1005                 $mode = 'site';
1006             }
1007
1008             if ($action == 'set-tag-on-person') {
1009                 $mode = 'person';
1010             }
1011
1012             if ($action == 'set-tag-on-interface') {
1013                 $mode = 'interface';
1014             }
1015
1016             if ($mode == 'node') {
1017                 $id = intval($_POST['node_id']);
1018             } else if ($mode == 'site') {
1019                 $id = intval($_POST['site_id']);
1020             } else if ($mode == 'person') {
1021                 $id = intval($_POST['person_id']);
1022             } else {
1023                 $id = intval($_POST['interface_id']);
1024             }
1025
1026             $tag_type_id = intval($_POST['tag_type_id']);
1027             $value = $_POST['value'];
1028
1029             $tag_types = $api->GetTagTypes(array($tag_type_id));
1030             if (count($tag_types) != 1) {
1031                 drupal_set_error("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
1032             } else {
1033                 if ($mode == 'node') {
1034                     $tags = $api->GetNodeTags(array('node_id' => $id, 'tag_type_id' => $tag_type_id));
1035                 } else if ($mode == 'site') {
1036                     $tags = $api->GetSiteTags(array('site_id' => $id, 'tag_type_id' => $tag_type_id));
1037                 } else if ($mode == 'person') {
1038                     $tags = $api->GetPersonTags(array('person_id' => $id, 'tag_type_id' => $tag_type_id));
1039                 } else {
1040                     $tags = $api->GetInterfaceTags(array('interface_id' => $id, 'tag_type_id' => $tag_type_id));
1041                 }
1042
1043                 // already has a tag set
1044                 if (count($tags) == 1) {
1045                     $tag = $tags[0];
1046                     if ($mode == 'node') {
1047                         $result = $api->UpdateNodeTag($tag['node_tag_id'], $value);
1048                     } else if ($mode == 'site') {
1049                         $result = $api->UpdateSiteTag($tag['site_tag_id'], $value);
1050                     } else if ($mode == 'person') {
1051                         $result = $api->UpdatePersonTag($tag['person_tag_id'], $value);
1052                     } else {
1053                         $result = $api->UpdateInterfaceTag($tag['interface_tag_id'], $value);
1054                     }
1055
1056                     if ($result == 1) {
1057                         drupal_set_message("Updated tag, new value = $value");
1058                     } else {
1059                         drupal_set_error("Could not update tag");
1060                     }
1061
1062                     // no such tag set yet on that object
1063                 } else {
1064                     if ($mode == 'node') {
1065                         $tag_id = $api->AddNodeTag($id, $tag_type_id, $value);
1066                     } else if ($mode == 'site') {
1067                         $tag_id = $api->AddSiteTag($id, $tag_type_id, $value);
1068                     } else if ($mode == 'person') {
1069                         $tag_id = $api->AddPersonTag($id, $tag_type_id, $value);
1070                     } else {
1071                         $tag_id = $api->AddInterfaceTag($id, $tag_type_id, $value);
1072                     }
1073
1074                     if ($tag_id) {
1075                         drupal_set_message("Created tag, new value = $value");
1076                     } else {
1077                         drupal_set_error("Could not create tag");
1078                     }
1079
1080                 }
1081             }
1082
1083             if ($mode == 'node') {
1084                 plc_redirect(l_node_tags($id));
1085             } else if ($mode == 'site') {
1086                 plc_redirect(l_site_tags($id));
1087             } else if ($mode == 'person') {
1088                 plc_redirect(l_person_tags($id));
1089             } else {
1090                 plc_redirect(l_interface_tags($id));
1091             }
1092
1093         }
1094
1095     case 'delete-node-tags':
1096     case 'delete-site-tags':
1097     case 'delete-person-tags':
1098     case 'delete-interface-tags':{
1099
1100             $mode = null;
1101             if ($action == 'delete-node-tags') {
1102                 $mode = 'node';
1103             }
1104
1105             if ($action == 'delete-site-tags') {
1106                 $mode = 'site';
1107             }
1108
1109             if ($action == 'delete-person-tags') {
1110                 $mode = 'person';
1111             }
1112
1113             if ($action == 'delete-interface-tags') {
1114                 $mode = 'interface';
1115             }
1116
1117             if ($mode == 'node') {
1118                 $id = $_POST['node_id'];
1119                 $tag_ids = $_POST['node_tag_ids'];
1120             } else if ($mode == 'site') {
1121                 $id = $_POST['site_id'];
1122                 $tag_ids = $_POST['site_tag_ids'];
1123             } else if ($mode == 'person') {
1124                 $id = $_POST['person_id'];
1125                 $tag_ids = $_POST['person_tag_ids'];
1126             } else {
1127                 $id = $_POST['interface_id'];
1128                 $tag_ids = $_POST['interface_tag_ids'];
1129             }
1130
1131             if (!$tag_ids) {
1132                 drupal_set_message("action=$action - No tag selected");
1133                 return;
1134             }
1135             $success = true;
1136             $counter = 0;
1137             foreach ($tag_ids as $tag_id) {
1138                 if ($mode == 'node') {
1139                     $retcod = $api->DeleteNodeTag(intval($tag_id));
1140                 } else if ($mode == 'site') {
1141                     $retcod = $api->DeleteSiteTag(intval($tag_id));
1142                 } else if ($mode == 'person') {
1143                     $retcod = $api->DeletePersonTag(intval($tag_id));
1144                 } else {
1145                     $retcod = $api->DeleteInterfaceTag(intval($tag_id));
1146                 }
1147
1148                 if ($retcod != 1) {
1149                     $success = false;
1150                 } else {
1151                     $counter++;
1152                 }
1153
1154             }
1155             if ($success) {
1156                 drupal_set_message("Deleted $counter tag(s)");
1157             } else {
1158                 drupal_set_error("Could not delete all selected tags, only $counter were removed");
1159             }
1160
1161             if ($mode == 'node') {
1162                 plc_redirect(l_node_tags($id));
1163             } else if ($mode == 'site') {
1164                 plc_redirect(l_site_tags($id));
1165             } else if ($mode == 'person') {
1166                 plc_redirect(l_person_tags($id));
1167             } else {
1168                 plc_redirect(l_interface_tags($id));
1169             }
1170
1171         }
1172
1173 //////////////////////////////////////// nodegroups
1174     case 'update-nodegroup':{
1175             $nodegroup_id = $_POST['nodegroup_id'];
1176             $groupname = $_POST['groupname'];
1177             $value = $_POST['value'];
1178
1179             $fields = array();
1180             $fields['groupname'] = $groupname;
1181             $fields['value'] = $value;
1182             if ($api->UpdateNodeGroup($nodegroup_id, $fields) == 1) {
1183                 drupal_set_message('Nodegroup updated');
1184             } else {
1185                 drupal_set_error("Could not update nodegroup $groupname");
1186             }
1187
1188             plc_redirect(l_nodegroup($nodegroup_id));
1189
1190         }
1191
1192     case 'add-nodegroup':{
1193             $groupname = $_POST['groupname'];
1194             if (!$groupname) {
1195                 drupal_set_error('Empty groupname');
1196                 plc_redirect(l_nodegroups());
1197             }
1198             $tag_type_id = intval($_POST['tag_type_id']);
1199             if (!$tag_type_id) {
1200                 drupal_set_error('You must select a tag in the dropdown list');
1201                 plc_redirect(l_nodegroups());
1202             }
1203             $value = $_POST['value'];
1204             if (!$value) {
1205                 drupal_set_message("Empty value.. let's see ..");
1206             }
1207             if ($api->AddNodeGroup($groupname, $tag_type_id, $value) > 0) {
1208                 drupal_set_message("Nodegroup $groupname created");
1209             } else {
1210                 drupal_set_error("Could not create nodegroup $groupname");
1211             }
1212
1213             plc_redirect(l_nodegroups());
1214             break;
1215         }
1216
1217     case 'delete-nodegroups':{
1218             $nodegroup_ids = $_POST['nodegroup_ids'];
1219             if (!$nodegroup_ids) {
1220                 drupal_set_message("action=delete-nodegroups - No group selected");
1221                 plc_redirect(l_nodegroups());
1222             }
1223             $success = true;
1224             $counter = 0;
1225             foreach ($nodegroup_ids as $nodegroup_id) {
1226                 if ($api->DeleteNodeGroup(intval($nodegroup_id)) != 1) {
1227                     $success = false;
1228                 } else {
1229                     $counter++;
1230                 }
1231             }
1232
1233             if ($success) {
1234                 drupal_set_message("Deleted $counter group(s)");
1235             } else {
1236                 drupal_set_error("Could not delete all selected groups, only $counter were removed");
1237             }
1238
1239             plc_redirect(l_nodegroups());
1240             break;
1241         }
1242
1243 //////////////////////////////////////// leases
1244     case 'manage-leases':{
1245             $actions = json_decode($_POST['actions']);
1246             $add_requested = 0;
1247             $add_done = 0;
1248             $del_requested = 0;
1249             $del_done = 0;
1250             $errors = array();
1251             foreach ($actions as $action) {
1252                 if ($action[0] == 'add-leases') {
1253                     $nodenames = $action[1];
1254                     $add_requested += count($nodenames);
1255                     $slicename = $action[2];
1256                     $t_from = intval($action[3]);
1257                     $t_until = intval($action[4]);
1258                     $hash = $api->AddLeases($nodenames, $slicename, $t_from, $t_until);
1259                     // update number of added leases
1260                     $ids = $hash['new_ids'];
1261                     $add_done += count($ids);
1262                     // update global errors array
1263                     foreach ($api_errors = $hash['errors'] as $error) {
1264                         $errors[] = $error;
1265                     }
1266
1267                 } else if ($action[0] == 'delete-leases') {
1268                     $lease_id = intval($action[1]);
1269                     $del_requested += 1;
1270                     if ($api->DeleteLeases(array($lease_id)) == 1) {
1271                         $del_done += 1;
1272                     } else {
1273                         $errors[] = "Could not delete lease " . $lease_id;
1274                     }
1275                 } else {
1276                     $errors[] = "in actions.php, manage-leases, wrong action " . $action[0];
1277                 }
1278             }
1279
1280             if (count($errors) == 0) {
1281                 echo ("All leases updated (" . $add_done . " added and " . $del_done . " deleted)");
1282             } else {
1283                 foreach ($errors as $error) {
1284                     echo ($error . "\n");
1285                 }
1286
1287                 echo ("Leases updated only partially (" .
1288                     $add_done . "/" . $add_requested . " added and " .
1289                     $del_done . "/" . $del_requested . " deleted)");
1290             }
1291
1292             break;
1293         }
1294
1295 ////////////////////////////////////////
1296
1297     case 'debug':{
1298             plc_debug('GET', $_GET);
1299             plc_debug('POST', $_POST);
1300             plc_debug('FILES', $_FILES);
1301             return;
1302         }
1303
1304     default:{
1305             plc_error("Unknown action $action in actions.php");
1306             return;
1307         }
1308
1309 }
1310
1311 ?>