more tweaks for reservable nodes, and other bugfixes
[www-register-wizard.git] / application / controllers / register.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 //require_once 'plc_sorts.php';
15 include 'plc_objects.php';
16
17
18 class Register extends Controller {
19         
20         var $pcu_id = 0;
21         function index()
22         {
23                 $this->load->helper(array('form', 'url'));
24                 $data=array();
25                 $data['stage'] = 0;
26                 $this->load->view('header', $data);
27                 $this->load->view('debug', $data);
28                 $this->load->view('stage0', $data);
29                 $this->load->view('footer', $data);
30         }
31         function stage1_addpcu()
32         {
33                 global $api, $plc;
34                 $this->load->helper(array('form', 'url'));
35                 $this->load->library('validation');
36                 
37                 $this->validation->set_error_delimiters('<span class="error">', '</span>');
38
39                 $rules['model']         = "trim|required";
40                 $rules['hostname']      = "trim|required";
41                 $rules['ip']            = "trim|required|valid_ip";
42                 $rules['username']      = "trim";
43                 $rules['password']      = "trim|required";
44                 $rules['notes']         = "trim";
45                 $this->validation->set_rules($rules);
46
47                 $fields['model']        = "Model";
48                 $fields['hostname']     = "Hostname";
49                 $fields['ip']           = "IP Address";
50                 $fields['username']     = "Username";
51                 $fields['password']     = "Password";
52                 $this->validation->set_fields($fields);
53
54                 $person = new Person($plc->person);
55                 $data = array();
56                 if ($this->validation->run() == TRUE)
57                 {
58                         if ($this->validation->model != "none-selected" )
59                         {
60                                 /* b/c the submit is valid, it doesn't matter if pcu_register is set */
61                                 $this->pcu_id = $this->add_pcu($data);
62                         }
63                 }
64                 $data = $this->get_stage1_data($person, $data);
65                 $data['stage'] = 1;
66                 $this->load->view('header', $data);
67                 $this->load->view('debug', $data);
68                 $this->load->view('stage1_pcu_choose', $data);
69                 $this->load->view('footer', $data);
70         }
71
72         function add_pcu(&$data)
73         {
74                 global $api, $plc;
75                 /* add pcu, get pcu info */
76                 $site_id = intval( $_REQUEST['site_id'] );
77                 $fields= array( 'protocol'=>    '',
78                                 'model'=>       $_REQUEST['model'], 
79                                 'hostname'=>    $this->validation->hostname,
80                                 'ip'=>          $this->validation->ip,
81                                 'username'=>    $this->validation->username, 
82                                 'password'=>    $this->validation->password, 
83                                 'notes'=>       $_REQUEST['notes'], );
84                 $pcu_id= $api->AddPCU( $site_id, $fields );
85
86                 if( $pcu_id == 0 ) {
87                         $data['error'] = $api->error();
88                         print $data['error'];
89                 }
90                 $data['pcu_id'] = $pcu_id;
91                 
92                 return $pcu_id;
93         }
94
95         function get_stage1_data($person, $data=NULL)
96         {
97                 global $api, $plc;
98                 if ( $data == NULL ){
99                     $data = array();
100                 }
101                 $data['default_site_list'] = $person->getSites();
102                 $data['pcu_types'] = $api->GetPCUTypes(NULL, array('model', 'name'));
103                 $data['pcu_list'] = $this->getpculist($person);
104                 $data['site_list'] = $this->getsitelist($person);
105                 return $data;
106         }
107
108         function getpculist($person)
109         {
110                 global $api, $plc;
111                 $plc_pcu_list = $api->GetPCUs(array('site_id' => $person->getSites()));
112                 return PlcObject::constructList('PCU', $plc_pcu_list);
113         }
114
115         function getsitelist($person)
116         {
117                 global $api, $plc;
118                 // get sites depending on role and sites associated.
119                 if( $person->isAdmin() ) {
120                         $site_info= $api->GetSites(array('peer_id' => NULL,'-SORT'=>'name'), 
121                                                    array( "name", "site_id", "login_base" ) );
122                 } else {
123                         $site_info= $api->GetSites( $person->getSites(), array( "name", "site_id", "login_base" ) );
124                 }
125                 //sort_sites( $site_info );
126                 return $site_info;
127         }
128
129         var $run_update = True;
130         var $disp_errors = True;
131         function stage2_confirmpcu()
132         {
133                 global $api, $plc;
134                 $this->load->helper(array('form', 'url'));
135                 $this->load->library('validation');
136                 
137                 $this->validation->set_error_delimiters('<span class="error">', '</span>');
138                 $person = new Person($plc->person);
139
140                 if ( isset($_REQUEST['pcu_choose']) ) {
141                         $rules['pcu_id']  = "required";
142                         $this->validation->set_rules($rules);
143                         $fields['pcu_id']  = "PCU id";
144                         $this->validation->set_fields($fields);
145
146                         $result = $this->validation->run();
147                         /* I don't know, shouldn't we redirect to the first stage in this case? */
148                         $this->run_update = False;
149                         $this->disp_errors = False;
150                         print $this->validation->error_string . "<br>";
151
152                 } else {
153                         # Information update
154
155                         $rules['pcu_id']  = "required";
156                         $rules['hostname']  = "trim|required";
157                         $rules['ip']            = "trim|required|valid_ip";
158                         $rules['username']  = "trim";
159                         $rules['password']  = "trim|required";
160                         $rules['notes']         = "trim";
161                         $this->validation->set_rules($rules);
162
163                         $fields['pcu_id']  = "PCU id";
164                         $fields['hostname']  = "Hostname";
165                         $fields['ip']           = "IP Address";
166                         $fields['username']  = "Username";
167                         $fields['password']  = "Password";
168                         $this->validation->set_fields($fields);
169
170                         if ( $this->validation->run() == FALSE )
171                         {
172                                 print $this->validation->error_string . "<br>";
173                                 $this->run_update = False;
174                         } 
175                 }
176                 $data = $this->get_stage2_data($person);
177                 $data['stage'] = 2;
178                 $this->load->view('header', $data);
179                 $this->load->view('debug', $data);
180                 $this->load->view('stage2_pcu_confirm', $data);
181                 $this->load->view('footer', $data);
182
183         }
184
185         function update_pcu($data)
186         {
187                 global $api, $plc;
188                 /* add pcu, get pcu info */
189                 $pcu_id = intval($this->validation->pcu_id);
190                 $fields= array( 'protocol'=>    '',
191                                 'model'=>       $_REQUEST['model'], 
192                                 'hostname'=>    $this->validation->hostname,
193                                 'ip'=>          $this->validation->ip,
194                                 'username'=>    $this->validation->username, 
195                                 'password'=>    $this->validation->password, 
196                                 'notes'=>       $_REQUEST['notes'], );
197                 $ret = $api->UpdatePCU( $pcu_id, $fields );
198
199                 if( $ret != 1 ) {
200                         $data['error'] = $api->error();
201                         print $data['error'];
202                 }
203                 
204                 return $pcu_id;
205         }
206
207         function get_stage2_data($person)
208         {
209                 global $api, $plc;
210
211                 $data = array();
212                 if ( $this->run_update && isset($_REQUEST['pcu_update']) ) 
213                 {
214                         $this->update_pcu($data);
215                 } 
216
217                 if ( isset($_REQUEST['pcu_id']) ) 
218                 {
219                         $pcu_id = intval($_REQUEST['pcu_id']);
220                         $pcu_data = $api->GetPCUs(array('pcu_id'=>$pcu_id));
221                         $pcu = new PCU($pcu_data[0]);
222                         $data['pcu'] = $pcu;
223                         $data['pcu_id'] = $pcu_id;
224                         $data['site_id'] = $pcu_data[0]['site_id'];
225                 } 
226
227                 $data['default_site_list'] = $person->getSites();
228                 $data['pcu_types'] = $api->GetPCUTypes(NULL, array('model', 'name'));
229                 $data['pcu_site'] = $api->GetSites( $pcu_data[0]['site_id'], array( "name", "site_id", "login_base" ) );
230                 return $data;
231         }
232
233         var $node_id = 0;
234         function stage3_addnode($pcu_id, $site_id)
235         {
236                 global $api, $plc;
237                 $this->load->helper(array('form', 'url'));
238                 $this->load->library('validation');
239                 
240                 $this->validation->set_error_delimiters('<span class="error">', '</span>');
241                 $person = new Person($plc->person);
242                 $data = array();
243                 $data['pcu_id'] = intval($pcu_id);
244                 $data['site_id'] = intval($site_id);
245
246                 if ( isset($_REQUEST['pcu_proceed']) ) {
247                         $rules['hostname']      = "";
248                         $rules['node_type']     = 'regular';
249                         $rules['model']         = "";
250                         $rules['method']        = "dhcp";
251                         $rules['ip']            = "";
252                         $rules['netmask']       = "";
253                         $rules['network']       = "";
254                         $rules['gateway']       = "";
255                         $rules['broadcast']     = "";
256                         $rules['dns1']          = "";
257                         $rules['dns2']          = "";
258                         $this->validation->set_rules($rules);
259                         $fields['hostname']     = "Hostname";
260                         $fields['node_type']    = "Node Type";
261                         $fields['model']        = "Model";
262                         $fields['method']       = "Method";
263                         $fields['ip']           = "IP Address";
264                         $fields['netmask']      = "Netmask Address";
265                         $fields['network']      = "Network Address";
266                         $fields['gateway']      = "Gateway Address";
267                         $fields['broadcast']    = "Broadcast Address";
268                         $fields['dns1']         = "Primary DNS Address";
269                         $fields['dns2']         = "Secondary DNS Address";
270                         $this->validation->set_fields($fields);
271
272                         $result = $this->validation->run();
273                         /*print "RESULT: ".$result . "<br>";*/
274                         # TODO: if result is false, redirect to beginning.
275                         $this->disp_errors = False;
276                         print $this->validation->error_string . "<br>";
277                 } else {
278                         $rules['hostname']      = "trim|required";
279                         $rules['node_type']     = "required";
280                         $rules['model']         = "trim|required";
281                         $rules['method']        = "required";
282                         $rules['ip']            = "trim|required|valid_ip";
283                         if ( isset ($_REQUEST['method']) && $_REQUEST['method'] == 'static' )
284                         {
285                                 $rules['netmask']  = "trim|valid_ip";
286                                 $rules['network']  = "trim|valid_ip";
287                                 $rules['gateway']  = "trim|valid_ip";
288                                 $rules['broadcast']  = "trim|valid_ip";
289                                 $rules['dns1']  = "trim|valid_ip";
290                                 $rules['dns2']  = "trim|valid_ip";
291                         } else {
292                                 $rules['netmask']  = "";
293                                 $rules['network']  = "";
294                                 $rules['gateway']  = "";
295                                 $rules['broadcast']  = "";
296                                 $rules['dns1']  = "";
297                                 $rules['dns2']  = "";
298                         }
299                         $this->validation->set_rules($rules);
300
301                         $fields['hostname']     = "Hostname";
302                         $fields['node_type']    = "Node Type";
303                         $fields['model']        = "Model";
304                         $fields['method']       = "Method";
305                         $fields['ip']           = "IP Address";
306                         $fields['netmask']      = "Netmask Address";
307                         $fields['network']      = "Network Address";
308                         $fields['gateway']      = "Gateway Address";
309                         $fields['broadcast']    = "Broadcast Address";
310                         $fields['dns1']         = "Primary DNS Address";
311                         $fields['dns2']         = "Secondary DNS Address";
312                         $this->validation->set_fields($fields);
313
314                         if ($this->validation->run() == TRUE)
315                         {
316                                 /* b/c the submit is valid, all values are minimally consistent. */
317                                 $this->node_id = $this->add_node($data);
318                         }
319                 }
320                 $data = $this->get_stage3_data($person, $data);
321                 $data['stage'] = 3;
322                 $this->load->view('header', $data);
323                 $this->load->view('debug', $data);
324                 $this->load->view('stage3_node_choose', $data);
325                 $this->load->view('footer', $data);
326         }
327
328         function add_node(&$data)
329         {
330                 global $api, $plc;
331                 $hostname = trim($_REQUEST['hostname']);
332                 $node_type = trim($_REQUEST['node_type']);
333                 $model = trim($_REQUEST['model']);
334                 $method = trim($_REQUEST['method']);
335                 $ip = trim($_REQUEST['ip']);
336                 if ( $method == 'static' )
337                 {
338                         $netmask = trim($_REQUEST['netmask']);
339                         $network = trim($_REQUEST['network']);
340                         $gateway = trim($_REQUEST['gateway']);
341                         $broadcast = trim($_REQUEST['broadcast']);
342                         $dns1 = trim($_REQUEST['dns1']);
343                         $dns2 = trim($_REQUEST['dns2']);
344                 }
345
346                 // used to generate error strings for static fields only
347                 $static_fields= array();
348                 $static_fields['netmask']= "Netmask address";
349                 $static_fields['network']= "Network address";
350                 $static_fields['gateway']= "Gateway address";
351                 $static_fields['broadcast']= "Broadcast address";
352                 $static_fields['dns1']= "Primary DNS address";
353                 
354                 if( $method == 'static' )
355                 {
356                         if( !is_valid_network_addr($network,$netmask) )
357                         {
358                                 $errors[] = "The network address does not coorespond to the netmask";
359                         }
360                 }
361
362                 if( !isset($errors) || count($errors) == 0 ) {
363                         // add new node and its network
364                         $optional_vals= array( 'hostname'=>$hostname, 'node_type'=>$node_type, 'model'=>$model );
365
366                         $site_id= $data['site_id'];
367                         // Try to get node in case this is from an error:
368                         $nodes = $api->GetNodes($optional_vals);
369                         if ( count($nodes) > 0 ) {
370                                 $node_id= $nodes[0]['node_id'];
371                         } else {
372                                 $node_id= $api->AddNode( intval( $site_id ), $optional_vals );
373                                 if( $node_id <= 0 ) {
374                                         $data['error'] = $api->error();
375                                         print $data['error'];
376                                 }
377                         }
378
379                         // now, try to add the network.
380                         $optional_vals= array();
381                         $optional_vals['is_primary']= true;
382                         $optional_vals['ip']= $ip;
383                         $optional_vals['type']= 'ipv4';
384                         $optional_vals['method']= $method;
385                         
386                         if( $method == 'static' )
387                         {
388                                 $optional_vals['gateway']= $gateway;
389                                 $optional_vals['network']= $network;
390                                 $optional_vals['broadcast']= $broadcast;
391                                 $optional_vals['netmask']= $netmask;
392                                 $optional_vals['dns1']= $dns1;
393                                 if (!empty($dns2)) {
394                                         $optional_vals['dns2']= $dns2;
395                                 }
396                         }
397
398                         $interface_id= $api->AddInterface( $node_id, $optional_vals);
399                         if( $interface_id <= 0 ) {
400                                 $data['error'] = $api->error();
401                                 print $data['error'];
402                         }
403
404                         $pcus = $api->GetPCUs(array('pcu_id' => $data['pcu_id']));
405                         if ( count($pcus) > 0 )
406                         {
407                                 $pcu = $pcus[0];
408                                 # if $node_id in $pcu['node_ids']
409                                 if ( ! in_array( $node_id , $pcu['node_ids'] ) )
410                                 {
411                                     $success = $api->AddNodeToPCU( $node_id, $data['pcu_id'], 1);
412                                     if( !isset($success) || $success <= 0 ) {
413                                             $data['error'] = $api->error();
414                                             print $data['error'];
415                                     }
416                                    
417                                 }
418                         }
419                         $data['interface_id'] = $interface_id;
420                         $data['node_id'] = $node_id;
421                         if ( isset($errors) ) { $data['errors'] = $errors; }
422                         return $node_id;
423
424                 } else {
425                     $data['error'] = $errors[0];
426                         return 0;
427                 }
428                 
429         }
430
431         function get_stage3_data($person, $data=NULL)
432         {
433                 global $api, $plc;
434                 if ( $data == NULL ){
435                     $data = array();
436                 }
437                 $data['default_site_list'] = array($data['site_id']);
438                 $data['node_list'] = $this->getnodelist($data['site_id']);
439                 $data['site'] = $this->getsite($data['site_id']);
440                 return $data;
441         }
442
443         function getnodelist($site_id)
444         {
445                 global $api, $plc;
446                 $plc_node_list = $api->GetNodes(array('site_id' => intval($site_id) ));
447                 $ret = array();
448                 foreach ($plc_node_list as $plc_node)
449                 {
450                         $ret[] = new Node($plc_node, True);
451                 }
452                 return $ret;
453         }
454
455         function getsite($site_id)
456         {
457                 global $api, $plc;
458                 $site_info = $api->GetSites($site_id, array( "name", "site_id", "login_base" ) );
459                 return $site_info[0];
460         }
461
462         function stage45_mappcu($pcu_id=0, $site_id=0, $node_id=0)
463         {
464                 global $api, $plc;
465                 $this->load->helper(array('form', 'url'));
466                 $this->load->library('validation');
467                 
468                 $this->validation->set_error_delimiters('<span class="error">', '</span>');
469
470                 $fields['port_number']  = "Port Number";
471                 $this->validation->set_fields($fields);
472
473                 $data = array();
474                 $data['pcu_id'] = intval($pcu_id);
475                 $data['site_id'] = intval($site_id);
476                 $data['node_id'] = intval($node_id);
477
478                 if ( isset($_REQUEST['node_confirm']) ) {
479                         /* skip the rules, since we're just displaying the page. */
480                         $rules['port_number']           = "";
481                         $this->validation->set_rules($rules);
482
483                 } else {
484                         /* setup rules, to validate the form data. */
485                         $rules['port_number']           = "trim|required|intval";
486                         $this->validation->set_rules($rules);
487
488                         $result = $this->validation->run();
489                         if ( ! $result ) {
490                                 print "ERROR";
491                         } else {
492                                 $port = $this->validation->port_number;
493                                 /* if we do not delete the node from the PCU first, a fault is raised */
494                                 $ret = $api->DeleteNodeFromPCU($data['node_id'], $data['pcu_id']);
495                                 $ret = $api->AddNodeToPCU($data['node_id'], $data['pcu_id'], $port);
496                                 if ( $ret != 1 )
497                                 {
498                                         $data['error'] = $api->error();
499                                         print $data['error'];
500                                 }
501                         }
502                         $this->disp_errors = False;
503                         print $this->validation->error_string . "<br>";
504                 }
505
506                 $data['node'] = $this->getnode($data['node_id']);
507                 if ( sizeof($data['node']->pcu_ids) == 0)
508                 {
509                         $data['pcu_assigned'] = False;
510                         $data['pcu_port'] = -1;
511                 } else {
512                         $data['pcu_assigned'] = True;
513                         $api_pcus = $api->GetPCUs($data['node']->pcu_ids);
514                         $pcu = $api_pcus[0];
515                         # NOTE: find index of node id, then pull out that index of
516                         $index = array_search($data['node_id'], $pcu['node_ids']);
517                         $data['pcu_port'] = $pcu['ports'][$index];
518                 }
519
520                 $data['stage'] = 4.5;
521                 #$data = $this->get_stage4_data($person, $data);
522                 $this->load->view('header', $data);
523                 $this->load->view('debug', $data);
524                 $this->load->view('stage45_pcuport', $data);
525                 $this->load->view('footer', $data);
526         }
527
528         function stage4_confirmnode($pcu_id=0, $site_id=0)
529         {
530                 global $api, $plc;
531                 $this->load->helper(array('form', 'url'));
532                 $this->load->library('validation');
533                 
534                 $this->validation->set_error_delimiters('<span class="error">', '</span>');
535                 $person = new Person($plc->person);
536
537                 $fields['hostname']     = "Hostname";
538                 $fields['node_type']    = "Node Type";
539                 $fields['model']        = "Model";
540                 $fields['method']       = "Method";
541                 $fields['ip']           = "IP Address";
542                 $fields['netmask']      = "Netmask Address";
543                 $fields['network']      = "Network Address";
544                 $fields['gateway']      = "Gateway Address";
545                 $fields['broadcast']    = "Broadcast Address";
546                 $fields['dns1']         = "Primary DNS Address";
547                 $fields['dns2']         = "Secondary DNS Address";
548                 $fields['node_id']      = "NODE id";
549                 $this->validation->set_fields($fields);
550
551                 $data = array();
552                 $data['pcu_id'] = intval($pcu_id);
553                 $data['site_id'] = intval($site_id);
554
555                 if ( isset($_REQUEST['node_choose']) ) {
556                         $rules['node_id']       = "required|intval";
557                         $rules['hostname']      = "";
558                         $rules['node_type']     = "regular";
559                         $rules['model']         = "";
560                         $rules['method']        = "dhcp";
561                         $rules['ip']            = "";
562                         $rules['netmask']       = "";
563                         $rules['network']       = "";
564                         $rules['gateway']       = "";
565                         $rules['broadcast']     = "";
566                         $rules['dns1']          = "";
567                         $rules['dns2']          = "";
568                         $this->validation->set_rules($rules);
569
570                         $result = $this->validation->run();
571                         /*print "RESULT: ".$result . "<br>";*/
572                         # TODO: if result is false, redirect to beginning.
573                         $this->disp_errors = False;
574                         print $this->validation->error_string . "<br>";
575                 } else {
576                         $rules['hostname']      = "trim|required";
577                         $rules['node_type']     = 'required';
578                         $rules['model']         = "trim|required";
579                         $rules['method']        = "required";
580                         $rules['ip']            = "trim|required|valid_ip";
581                         if ( isset ($_REQUEST['method']) && $_REQUEST['method'] == 'static' ) {
582                                 $rules['netmask']  = "trim|valid_ip";
583                                 $rules['network']  = "trim|valid_ip";
584                                 $rules['gateway']  = "trim|valid_ip";
585                                 $rules['broadcast']  = "trim|valid_ip";
586                                 $rules['dns1']  = "trim|valid_ip";
587                                 $rules['dns2']  = "trim";
588                         } else {
589                                 # NOTE: There are no conditions that must be met for these fields.
590                                 $rules['netmask']  = "";
591                                 $rules['network']  = "";
592                                 $rules['gateway']  = "";
593                                 $rules['broadcast']  = "";
594                                 $rules['dns1']  = "";
595                                 $rules['dns2']  = "";
596                         }
597                         $rules['node_id']  = "required|intval";
598                         $this->validation->set_rules($rules);
599
600                         if ($this->validation->run() == TRUE)
601                         {
602                                 /* b/c the submit is valid, all values are minimally consistent. */
603                                 $this->node_id = $this->update_node($data);
604                         }
605                 }
606                 $data['node_id'] = intval($this->validation->node_id);
607                 $data['stage'] = 4;
608                 if ( $this->checknodeid($data['node_id']) ) {
609                         $data = $this->get_stage4_data($person, $data);
610                         $this->load->view('header', $data);
611                         $this->load->view('debug', $data);
612                         $this->load->view('stage4_node_confirm', $data);
613                         $this->load->view('footer', $data);
614                 } else {
615                         print "You must select a valid Node before continuing.";
616                 }
617         }
618
619         function update_node(&$data)
620         {
621                 # TODO: RECODE To update values instead of adding them...
622                 global $api, $plc;
623                 $hostname = trim($_REQUEST['hostname']);
624                 $node_type = trim($_REQUEST['node_type']);
625                 $model = trim($_REQUEST['model']);
626                 $node_id = intval($this->validation->node_id);
627                 $optional_vals = array('hostname' => $hostname, 'model' => $model, 'node_type' => $node_type );
628                 $ret = $api->UpdateNode( $node_id, $optional_vals);
629                 if( $ret <= 0 ) {
630                         $data['error'] = $api->error();
631                         print $data['error'];
632                 }
633
634                 $api_node_list = $api->GetNodes($node_id);
635                 if ( count($api_node_list) > 0 ) {
636                         $node_obj = new Node($api_node_list[0], True);
637                 } else {
638                         print "broken!!!";
639                         exit (1);
640                 }
641                 
642
643                 $optional_vals= array();
644
645                 $method = trim($_REQUEST['method']);
646                 if ( $node_obj->method != $method ) {
647                         $optional_vals['method']= $method;
648                 }
649
650                 $ip = trim($_REQUEST['ip']);
651                 if ( $node_obj->ip != $ip ) {
652                         $optional_vals['ip']= $ip;
653                 }
654
655                 // used to generate error strings for static fields only
656                 $static_fields= array();
657                 $static_fields['netmask']       = "Netmask address";
658                 $static_fields['network']       = "Network address";
659                 $static_fields['gateway']       = "Gateway address";
660                 $static_fields['broadcast']     = "Broadcast address";
661                 $static_fields['dns1']          = "Primary DNS address";
662
663                 if ( $method == 'static' )
664                 {
665                         $netmask = trim($_REQUEST['netmask']);
666                         $network = trim($_REQUEST['network']);
667                         $gateway = trim($_REQUEST['gateway']);
668                         $broadcast = trim($_REQUEST['broadcast']);
669                         $dns1 = trim($_REQUEST['dns1']);
670                         $dns2 = trim($_REQUEST['dns2']);
671
672                         if( !is_valid_network_addr($network,$netmask) )
673                         {
674                                 $errors[] = "The network address does not coorespond to the netmask";
675                         }
676                 }
677
678                 if ( !isset($errors) || count($errors) == 0 )
679                 {
680                         // now, try to add the network.
681                         if( $method == 'static' )
682                         {
683                                 if ( $node_obj->gateway != $gateway ) {
684                                         $optional_vals['gateway']= $gateway;
685                                 }
686                                 if ( $node_obj->network != $network ) {
687                                         $optional_vals['network']= $network;
688                                 }
689                                 if ( $node_obj->broadcast != $broadcast ) {
690                                         $optional_vals['broadcast']= $broadcast;
691                                 }
692                                 if ( $node_obj->netmask != $netmask ) {
693                                         $optional_vals['netmask']= $netmask;
694                                 }
695                                 if ( $node_obj->dns1 != $dns1 ) {
696                                         $optional_vals['dns1']= $dns1;
697                                 }
698                                 if ( $node_obj->dns2 != $dns2 ) {
699                                         $optional_vals['dns2']= $dns2;
700                                 }
701                         }
702
703                         if ( count($optional_vals) > 0 )
704                         {
705                           // print_r($optional_vals);
706                                 $ret = $api->UpdateInterface( $node_obj->interface_id, $optional_vals);
707                                 if( $ret <= 0 ) {
708                                         $data['error'] = $api->error();
709                                         print $data['error'];
710                                 }
711                         }
712                 }
713
714                 $data['node_id'] = $node_id;
715                 if ( isset($errors) ) { $data['errors'] = $errors; }
716                 
717                 return $node_id;
718         }
719
720         function get_stage4_data($person, $data=NULL)
721         {
722                 global $api, $plc;
723                 if ( $data == NULL ){
724                     $data = array();
725                 }
726                 $data['node'] = $this->getnode($data['node_id']);
727                 $data['site'] = $this->getsite($data['site_id']);
728                 /*print "SITENAME: " . $data['site']['login_base'] . "<BR>";*/
729                 return $data;
730         }
731
732         function checknodeid($node_id)
733         {
734                 global $api, $plc;
735                 $plc_node_list = $api->GetNodes(array('node_id' => intval($node_id) ), array('node_id'));
736                 if ( count($plc_node_list) > 0 ) {
737                         return True;
738                 } else {
739                         return False;
740                 }
741         }
742
743         function getnode($node_id)
744         {
745                 global $api, $plc;
746                 $plc_node_list = $api->GetNodes(array('node_id' => intval($node_id) ));
747                 if ( count($plc_node_list) > 0 )
748                 {
749                         return new Node($plc_node_list[0], True);
750                 } else {
751                         return NULL;
752                 }
753         }
754
755 }
756 ?>