5b710810e3594a8bb679158dd139baf077d84fff
[www-register-wizard.git] / application / controllers / download.php
1 <?php
2
3 require_once 'plc_login.php'; // Require login
4 require_once 'plc_session.php'; // Get session and API handles
5 global $plc, $api, $adm;
6
7 // Print header
8 require_once 'plc_drupal.php';
9
10 include 'plc_header.php';
11
12 // Common functions
13 require_once 'plc_functions.php';
14 include 'plc_objects.php';
15
16
17 class Download extends Controller {
18         
19         var $pcu_id = 0;
20         function stage5_bootimage($pcu_id=0, $site_id=0, $node_id=0)
21         {
22                 global $api, $plc;
23                 $this->load->helper(array('form', 'url'));
24                 $this->load->library('validation');
25                 
26                 $this->validation->set_error_delimiters('<span class="error">', '</span>');
27
28                 # TODO: if result is false, redirect to beginning.
29                 /*$result = $this->validation->run();*/
30                 /*print "RESULT: ".$result . "<br>";*/
31                 /*print $this->validation->error_string . "<br>";*/
32
33                 $data = array();
34                 $data['pcu_id'] = intval($pcu_id);
35                 $data['node_id'] = intval($node_id);
36                 $data['site_id'] = intval($site_id);
37                 $data['stage'] = 5;
38                 $this->load->view('header', $data);
39                 $this->load->view('debug', $data);
40                 $this->load->view('stage5_bootimage', $data);
41                 $this->load->view('footer', $data);
42         }
43
44         function stage6_download($pcu_id=0, $site_id=0, $node_id=0)
45         {
46                 global $api, $plc;
47                 $this->load->helper(array('form', 'url'));
48                 $this->load->helper('download');
49                 $this->load->library('validation');
50                 
51                 $this->validation->set_error_delimiters('<span class="error">', '</span>');
52                 $person = new Person($plc->person);
53
54                 $fields['action']  = "Download Action";
55                 $this->validation->set_fields($fields);
56
57                 $rules['action']        = "required";
58                 $this->validation->set_rules($rules);
59
60                 # TODO: if result is false, redirect to beginning.
61                 $result = $this->validation->run();
62
63                 $data = array();
64                 $data['pcu_id'] = intval($pcu_id);
65                 $data['node_id'] = intval($node_id);
66                 $data['site_id'] = intval($site_id);
67                 $data['action'] = $this->validation->action;
68                 $data['stage'] = 6;
69                 $data['generic_iso_name'] = $this->get_bootcd_version();
70                 $data = $this->get_stage6_data($person, $data);
71                 print $this->validation->error_string . "<br>";
72                 $this->load->view('header', $data);
73                 $this->load->view('debug', $data);
74                 $this->load->view('stage6_download', $data);
75                 $this->load->view('footer', $data);
76         }
77
78         function getnode($node_id)
79         {
80                 global $api, $plc;
81                 $plc_node_list = $api->GetNodes(array('node_id' => intval($node_id) ));
82                 return $plc_node_list[0];
83         }
84
85         function get_stage6_data($person, $data=NULL)
86         {
87                 global $api, $plc;
88                 if ( $data == NULL ){
89                     $data = array();
90                 }
91                 $data['api'] = $api;
92                 $data['has_primary'] = 1;
93                 $node_detail = $this->getnode($data['node_id']);
94                 $data['node_detail'] = $node_detail;
95                 $node_id = $data['node_id'];
96                 $action = $_REQUEST['action'];
97                 switch ($action) {
98
99                         case "download-generic-iso":
100                         case "download-generic-usb":
101                    
102                                 if ($action=="download-generic-iso") {
103                                         $boot_action="generic-iso";
104                                         $basename=PLC_NAME."-BootCD.iso";
105                                 } else {
106                                         $boot_action="generic-usb";
107                                         $basename=PLC_NAME."-BootCD.usb";
108                                 }
109
110                                 $api->UpdateNode( $hostname, array( "boot_state" => 'disabled',
111                                                                                                         "version" => $this->get_bootcd_version() ) );
112                                 /* exits on success */
113                                 $success = $this->deliver_bootmedium($node_id, $boot_action, $basename);
114                                 if (! $success ) {
115                                         $error="Unexpected error from deliver_bootmedium - probably wrong directory modes";
116                                         print ("<div class='plc-error'> $error </div>\n");
117                                         print ("<p><a href='/db/nodes/index.php?id=$node_id'>Back to node </a>\n");
118                                         return ;
119                                 }
120                                 break;
121
122                         case "download-node-floppy-with-iso":
123                         case "download-node-floppy-with-usb":
124                         case "download-node-floppy":
125                         case "download-node-iso":
126                         case "download-node-usb":
127                         case "download-node-usb-partition":
128                    
129                                 $return= $api->GetNodes( array( $node_id ) );
130                                 $node_detail= $return[0];
131
132                                 // non-admin people need to be affiliated with the right site
133                                 if( !in_array( 10, $person->data['role_ids'] ) ) {
134                                         $node_site_id = $node_detail['site_id'];
135                                         $in_site = in_array ($node_site_id,$person->data['site_ids']);
136                                         if( ! $in_site) {
137                                                 $error= "Insufficient permission. You cannot create configuration files for this node.";
138                                         }
139                                 }
140
141                                 $hostname= $node_detail['hostname'];
142                                 $return= $api->GetInterfaces( array( "node_id" => $node_id ), NULL );
143                    
144                                 $can_gen_config= 1;
145                                 $data['has_primary']= 0;
146                    
147                                 if( count($return) > 0 ) {
148                                         foreach( $return as $node_network_detail ) {
149                                                 if( $node_network_detail['is_primary'] == true ) {
150                                                         $data['has_primary']= 1;
151                                                         break;
152                                                 }
153                                         }
154                                         $data['node_network_detail'] = $node_network_detail;
155                                 }
156
157                                 if( !$data['has_primary'] ) {
158                                         $can_gen_config= 0;
159                                 } else {
160                                         if( $node_detail['hostname'] == "" ) {
161                                                 $can_gen_config= 0;
162                                                 $node_detail['hostname']= "<i>Missing</i>";
163                                         }
164                          
165                                         $fields= array("method","ip");
166                                         foreach( $fields as $field ) {
167                                                 if( $node_network_detail[$field] == "" ) {
168                                                         $can_gen_config= 0;
169                                                         $node_network_detail[$field]= "<i>Missing</i>";
170                                                 }
171                                         }
172
173                                         if( $node_network_detail['method'] == "static" ) {
174                                                 $fields= array("gateway","netmask","network","broadcast","dns1");
175                                                 foreach( $fields as $field ) {
176                                                         if( $node_network_detail[$field] == "" ) {
177                                                                 $can_gen_config= 0;
178                                                                 $node_network_detail[$field]= "<i>Missing</i>";
179                                                         }
180                                                 }
181                                         }
182
183                                         if( $node_network_detail['method'] != "static" && $node_network_detail['method'] != "dhcp" ) {
184                                                 $can_gen_config= 0;
185                                                 $node_network_detail['method']= "<i>Unknown method</i>";
186                                         }
187                                 }
188
189                                 if( $can_gen_config && isset($_REQUEST['download']) ) {
190                                         $download = $_REQUEST['download'];
191                                         if (method_exists($api, "GetBootMedium"))
192                                                 $file_contents= $api->GetBootMedium($node_id, "node-floppy", "");
193                                         else
194                                                 $file_contents= $api->GenerateNodeConfFile($node_id, true);
195                 
196                                         switch ($action) {
197                                                 case 'download-node-floppy-with-iso':
198                                                 case 'download-node-floppy-with-usb':
199                                                 case 'download-node-floppy':
200                                                         $boot_action='node-floppy';
201                                                         $basename = "plnode.txt";
202                                                         break;
203                                                 case 'download-node-iso':
204                                                         $boot_action='node-iso';
205                                                         $basename = "$hostname.iso";
206                                                         break;
207                                                 case 'download-node-usb':
208                                                         $boot_action='node-usb';
209                                                         $basename = "$hostname.usb";
210                                                         break;
211                                                 case "download-node-usb-partition":
212                                                         $boot_action='node-usb-partition';
213                                                         $basename = "$hostname-partition.usb";
214                                                         break;
215                                         }        
216                                         if ($action != 'download-node-floppy')
217                                         {
218                                                 $api->UpdateNode( $hostname, array( "boot_state" => 'disabled',
219                                                                                   "version" => $this->get_bootcd_version() ) );
220                                         }
221                                         /* exits on success */
222                                         $success = $this->deliver_bootmedium($node_id, $boot_action, $basename);
223                                         if (! $success ) {
224                                                 $error="Unexpected error from deliver_bootmedium - probably wrong directory modes";
225                                                 print ("<div class='plc-error'> $error </div>\n");
226                                                 print ("<p><a href='/db/nodes/index.php?id=$node_id'>Back to node </a>\n");
227                                                 return ;
228                                         }
229                                 }
230                                 $action_labels = array (
231                                     'download-node-floppy' => 'textual node config (for floppy)' ,
232                                     'download-node-floppy-with-iso' => 'textual node config (for floppy)' ,
233                                     'download-node-floppy-with-usb' => 'textual node config (for floppy)' ,
234                                         'download-node-iso' => 'ISO image',
235                                         'download-node-usb-partition' => 'USB image',
236                                         'download-node-usb' => 'USB image' );
237
238                                 $format = $action_labels [ $action ] ;
239                                 /*drupal_set_title("Download boot material for $hostname");*/
240                         break;
241                  
242                         default:
243                                 echo "Unknown action $action.";
244                                 exit();
245                         break;
246                 }
247                 $data['format'] = $format;
248                 $data['can_gen_config'] = $can_gen_config;
249                 return $data;
250         }
251         function get_bootcd_version()
252         {
253                 $BOOTCD="/usr/share/bootcd";
254                 $BOOTCDVERSION="$BOOTCD/build/version.txt";
255                 $version = trim(file_get_contents($BOOTCDVERSION));
256                 $bootcd_version = PLC_NAME . "-BootCD-".$version;
257                 return $bootcd_version;
258         }
259
260         function deliver_bootmedium($node_id, $action, $filename)
261         {
262                 global $api;
263                 ini_set("memory_limit","450M");
264                 $options = array();
265                 switch ($action) {
266                         case "node-usb-partition":
267                                 $action = "node-usb";
268                                 $options[] = "partition";
269                                 break;
270                 }        
271                 $b64_data = $api->GetBootMedium($node_id,$action,"", $options);
272                 $error= $api->error();
273                 if ( empty($error) )
274                 {
275                         if ( $action == "node-floppy" )
276                         {
277                                 # data comes back in plain text for node-floppy.
278                                 $data = $b64_data;
279                         } else {
280                                 $data = base64_decode($b64_data);
281                         }
282                         $size = strlen($data);
283                         /* exits on success */
284                         force_download($filename, $data);
285                 }
286                 return False;
287         }
288
289 }
290 ?>