Provide better error reporting and error checking when updating a network
[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['model']         = "";
249                         $rules['method']        = "";
250                         $rules['ip']            = "";
251                         $rules['netmask']  = "";
252                         $rules['network']  = "";
253                         $rules['gateway']  = "";
254                         $rules['broadcast']  = "";
255                         $rules['dns1']  = "";
256                         $rules['dns2']  = "";
257                         $this->validation->set_rules($rules);
258                         $fields['hostname']  = "Hostname";
259                         $fields['model']        = "Model";
260                         $fields['method']       = "Method";
261                         $fields['ip']           = "IP Address";
262                         $fields['netmask']              = "Netmask Address";
263                         $fields['network']              = "Network Address";
264                         $fields['gateway']              = "Gateway Address";
265                         $fields['broadcast']    = "Broadcast Address";
266                         $fields['dns1']                 = "Primary DNS Address";
267                         $fields['dns2']                 = "Secondary DNS Address";
268                         $this->validation->set_fields($fields);
269
270                         $result = $this->validation->run();
271                         /*print "RESULT: ".$result . "<br>";*/
272                         # TODO: if result is false, redirect to beginning.
273                         $this->disp_errors = False;
274                         print $this->validation->error_string . "<br>";
275                 } else {
276                         $rules['hostname']  = "trim|required";
277                         $rules['model']         = "trim|required";
278                         $rules['method']        = "required";
279                         $rules['ip']            = "trim|required|valid_ip";
280                         if ( isset ($_REQUEST['method']) && $_REQUEST['method'] == 'static' )
281                         {
282                                 $rules['netmask']  = "trim|valid_ip";
283                                 $rules['network']  = "trim|valid_ip";
284                                 $rules['gateway']  = "trim|valid_ip";
285                                 $rules['broadcast']  = "trim|valid_ip";
286                                 $rules['dns1']  = "trim|valid_ip";
287                                 $rules['dns2']  = "trim|valid_ip";
288                         } else {
289                                 $rules['netmask']  = "";
290                                 $rules['network']  = "";
291                                 $rules['gateway']  = "";
292                                 $rules['broadcast']  = "";
293                                 $rules['dns1']  = "";
294                                 $rules['dns2']  = "";
295                         }
296                         $this->validation->set_rules($rules);
297
298                         $fields['hostname']  = "Hostname";
299                         $fields['model']        = "Model";
300                         $fields['method']       = "Method";
301                         $fields['ip']           = "IP Address";
302                         $fields['netmask']              = "Netmask Address";
303                         $fields['network']              = "Network Address";
304                         $fields['gateway']              = "Gateway Address";
305                         $fields['broadcast']    = "Broadcast Address";
306                         $fields['dns1']                 = "Primary DNS Address";
307                         $fields['dns2']                 = "Secondary DNS Address";
308                         $this->validation->set_fields($fields);
309
310                         if ($this->validation->run() == TRUE)
311                         {
312                                 /* b/c the submit is valid, all values are minimally consistent. */
313                                 $this->node_id = $this->add_node($data);
314                         }
315                 }
316                 $data = $this->get_stage3_data($person, $data);
317                 $data['stage'] = 3;
318                 $this->load->view('header', $data);
319                 $this->load->view('debug', $data);
320                 $this->load->view('stage3_node_choose', $data);
321                 $this->load->view('footer', $data);
322         }
323
324         function add_node(&$data)
325         {
326                 global $api, $plc;
327                 $hostname = trim($_REQUEST['hostname']);
328                 $model= trim($_REQUEST['model']);
329                 $method = trim($_REQUEST['method']);
330                 $ip = trim($_REQUEST['ip']);
331                 if ( $method == 'static' )
332                 {
333                         $netmask = trim($_REQUEST['netmask']);
334                         $network = trim($_REQUEST['network']);
335                         $gateway = trim($_REQUEST['gateway']);
336                         $broadcast = trim($_REQUEST['broadcast']);
337                         $dns1 = trim($_REQUEST['dns1']);
338                         $dns2 = trim($_REQUEST['dns2']);
339                 }
340
341                 // used to generate error strings for static fields only
342                 $static_fields= array();
343                 $static_fields['netmask']= "Netmask address";
344                 $static_fields['network']= "Network address";
345                 $static_fields['gateway']= "Gateway address";
346                 $static_fields['broadcast']= "Broadcast address";
347                 $static_fields['dns1']= "Primary DNS address";
348                 
349                 if( $method == 'static' )
350                 {
351                         if( !is_valid_network_addr($network,$netmask) )
352                         {
353                                 $errors[] = "The network address does not coorespond to the netmask";
354                         }
355                 }
356
357                 if( !isset($errors) || count($errors) == 0 )
358                 {
359                         // add new node and its network
360                         $optional_vals= array( "hostname"=>$hostname, "model"=>$model );
361
362                         $site_id= $data['site_id'];
363                         // Try to get node in case this is from an error:
364                         $nodes = $api->GetNodes($optional_vals);
365                         if ( count($nodes) > 0 ) {
366                                 $node_id= $nodes[0]['node_id'];
367                         } else {
368                                 $node_id= $api->AddNode( intval( $site_id ), $optional_vals );
369                                 if( $node_id <= 0 ) {
370                                         $data['error'] = $api->error();
371                                         print $data['error'];
372                                 }
373                         }
374
375                         // now, try to add the network.
376                         $optional_vals= array();
377                         $optional_vals['is_primary']= true;
378                         $optional_vals['ip']= $ip;
379                         $optional_vals['type']= 'ipv4';
380                         $optional_vals['method']= $method;
381                         
382                         if( $method == 'static' )
383                         {
384                                 $optional_vals['gateway']= $gateway;
385                                 $optional_vals['network']= $network;
386                                 $optional_vals['broadcast']= $broadcast;
387                                 $optional_vals['netmask']= $netmask;
388                                 $optional_vals['dns1']= $dns1;
389                                 if (!empty($dns2)) {
390                                         $optional_vals['dns2']= $dns2;
391                                 }
392                         }
393
394                         $interface_id= $api->AddInterface( $node_id, $optional_vals);
395                         if( $interface_id <= 0 ) {
396                                 $data['error'] = $api->error();
397                                 print $data['error'];
398                         }
399
400                         $pcus = $api->GetPCUs(array('pcu_id' => $data['pcu_id']));
401                         if ( count($pcus) > 0 )
402                         {
403                                 $pcu = $pcus[0];
404                                 # if $node_id in $pcu['node_ids']
405                                 if ( ! in_array( $node_id , $pcu['node_ids'] ) )
406                                 {
407                                     $success = $api->AddNodeToPCU( $node_id, $data['pcu_id'], 1);
408                                     if( !isset($success) || $success <= 0 ) {
409                                             $data['error'] = $api->error();
410                                             print $data['error'];
411                                     }
412                                    
413                                 }
414                         }
415                         $data['interface_id'] = $interface_id;
416                         $data['node_id'] = $node_id;
417                         if ( isset($errors) ) { $data['errors'] = $errors; }
418                         return $node_id;
419
420                 } else {
421                     $data['error'] = $errors[0];
422                         return 0;
423                 }
424
425
426                 
427         }
428
429         function get_stage3_data($person, $data=NULL)
430         {
431                 global $api, $plc;
432                 if ( $data == NULL ){
433                     $data = array();
434                 }
435                 $data['default_site_list'] = array($data['site_id']);
436                 $data['node_list'] = $this->getnodelist($data['site_id']);
437                 $data['site'] = $this->getsite($data['site_id']);
438                 return $data;
439         }
440
441         function getnodelist($site_id)
442         {
443                 global $api, $plc;
444                 $plc_node_list = $api->GetNodes(array('site_id' => intval($site_id) ));
445                 $ret = array();
446                 foreach ($plc_node_list as $plc_node)
447                 {
448                         $ret[] = new Node($plc_node, True);
449                 }
450                 return $ret;
451         }
452
453         function getsite($site_id)
454         {
455                 global $api, $plc;
456                 $site_info = $api->GetSites($site_id, array( "name", "site_id", "login_base" ) );
457                 return $site_info[0];
458         }
459
460         function stage45_mappcu($pcu_id=0, $site_id=0, $node_id=0)
461         {
462                 global $api, $plc;
463                 $this->load->helper(array('form', 'url'));
464                 $this->load->library('validation');
465                 
466                 $this->validation->set_error_delimiters('<span class="error">', '</span>');
467
468                 $fields['port_number']  = "Port Number";
469                 $this->validation->set_fields($fields);
470
471                 $data = array();
472                 $data['pcu_id'] = intval($pcu_id);
473                 $data['site_id'] = intval($site_id);
474                 $data['node_id'] = intval($node_id);
475
476                 if ( isset($_REQUEST['node_confirm']) ) {
477                         /* skip the rules, since we're just displaying the page. */
478                         $rules['port_number']           = "";
479                         $this->validation->set_rules($rules);
480
481                 } else {
482                         /* setup rules, to validate the form data. */
483                         $rules['port_number']           = "trim|required|intval";
484                         $this->validation->set_rules($rules);
485
486                         $result = $this->validation->run();
487                         if ( ! $result ) {
488                                 print "ERROR";
489                         } else {
490                                 $port = $this->validation->port_number;
491                                 /* if we do not delete the node from the PCU first, a fault is raised */
492                                 $ret = $api->DeleteNodeFromPCU($data['node_id'], $data['pcu_id']);
493                                 $ret = $api->AddNodeToPCU($data['node_id'], $data['pcu_id'], $port);
494                                 if ( $ret != 1 )
495                                 {
496                                         $data['error'] = $api->error();
497                                         print $data['error'];
498                                 }
499                         }
500                         $this->disp_errors = False;
501                         print $this->validation->error_string . "<br>";
502                 }
503
504                 $data['node'] = $this->getnode($data['node_id']);
505                 if ( sizeof($data['node']->pcu_ids) == 0)
506                 {
507                         $data['pcu_assigned'] = False;
508                         $data['pcu_port'] = -1;
509                 } else {
510                         $data['pcu_assigned'] = True;
511                         $api_pcus = $api->GetPCUs($data['node']->pcu_ids);
512                         $pcu = $api_pcus[0];
513                         # NOTE: find index of node id, then pull out that index of
514                         $index = array_search($data['node_id'], $pcu['node_ids']);
515                         $data['pcu_port'] = $pcu['ports'][$index];
516                 }
517
518                 $data['stage'] = 4.5;
519                 #$data = $this->get_stage4_data($person, $data);
520                 $this->load->view('header', $data);
521                 $this->load->view('debug', $data);
522                 $this->load->view('stage45_pcuport', $data);
523                 $this->load->view('footer', $data);
524         }
525
526         function stage4_confirmnode($pcu_id=0, $site_id=0)
527         {
528                 global $api, $plc;
529                 $this->load->helper(array('form', 'url'));
530                 $this->load->library('validation');
531                 
532                 $this->validation->set_error_delimiters('<span class="error">', '</span>');
533                 $person = new Person($plc->person);
534
535                 $fields['hostname']     = "Hostname";
536                 $fields['model']        = "Model";
537                 $fields['method']       = "Method";
538                 $fields['ip']           = "IP Address";
539                 $fields['netmask']      = "Netmask Address";
540                 $fields['network']      = "Network Address";
541                 $fields['gateway']      = "Gateway Address";
542                 $fields['broadcast']= "Broadcast Address";
543                 $fields['dns1']         = "Primary DNS Address";
544                 $fields['dns2']         = "Secondary DNS Address";
545                 $fields['node_id']  = "NODE id";
546                 $this->validation->set_fields($fields);
547
548                 $data = array();
549                 $data['pcu_id'] = intval($pcu_id);
550                 $data['site_id'] = intval($site_id);
551
552                 if ( isset($_REQUEST['node_choose']) ) {
553                         $rules['node_id']       = "required|intval";
554                         $rules['hostname']      = "";
555                         $rules['model']         = "";
556                         $rules['method']        = "";
557                         $rules['ip']            = "";
558                         $rules['netmask']       = "";
559                         $rules['network']       = "";
560                         $rules['gateway']       = "";
561                         $rules['broadcast']     = "";
562                         $rules['dns1']          = "";
563                         $rules['dns2']          = "";
564                         $this->validation->set_rules($rules);
565
566                         $result = $this->validation->run();
567                         /*print "RESULT: ".$result . "<br>";*/
568                         # TODO: if result is false, redirect to beginning.
569                         $this->disp_errors = False;
570                         print $this->validation->error_string . "<br>";
571                 } else {
572                         $rules['hostname']  = "trim|required";
573                         $rules['model']         = "trim|required";
574                         $rules['method']        = "required";
575                         $rules['ip']            = "trim|required|valid_ip";
576                         if ( isset ($_REQUEST['method']) && $_REQUEST['method'] == 'static' )
577                         {
578                                 $rules['netmask']  = "trim|valid_ip";
579                                 $rules['network']  = "trim|valid_ip";
580                                 $rules['gateway']  = "trim|valid_ip";
581                                 $rules['broadcast']  = "trim|valid_ip";
582                                 $rules['dns1']  = "trim|valid_ip";
583                                 $rules['dns2']  = "trim";
584                         } else {
585                                 # NOTE: There are no conditions that must be met for these fields.
586                                 $rules['netmask']  = "";
587                                 $rules['network']  = "";
588                                 $rules['gateway']  = "";
589                                 $rules['broadcast']  = "";
590                                 $rules['dns1']  = "";
591                                 $rules['dns2']  = "";
592                         }
593                         $rules['node_id']  = "required|intval";
594                         $this->validation->set_rules($rules);
595
596                         if ($this->validation->run() == TRUE)
597                         {
598                                 /* b/c the submit is valid, all values are minimally consistent. */
599                                 $this->node_id = $this->update_node($data);
600                         }
601                 }
602                 $data['node_id'] = intval($this->validation->node_id);
603                 $data['stage'] = 4;
604                 if ( $this->checknodeid($data['node_id']) )
605                 {
606                         $data = $this->get_stage4_data($person, $data);
607                         $this->load->view('header', $data);
608                         $this->load->view('debug', $data);
609                         $this->load->view('stage4_node_confirm', $data);
610                         $this->load->view('footer', $data);
611                 } else {
612                         print "You must select a valid Node before continuing.";
613                 }
614         }
615
616         function update_node(&$data)
617         {
618                 # TODO: RECODE To update values instead of adding them...
619                 global $api, $plc;
620                 $hostname = trim($_REQUEST['hostname']);
621                 $model= trim($_REQUEST['model']);
622                 $node_id = intval($this->validation->node_id);
623                 $ret = $api->UpdateNode( $node_id, array('hostname' => $hostname, 
624                                                                                   'model' => $model));
625                 if( $ret <= 0 ) {
626                         $data['error'] = $api->error();
627                         print $data['error'];
628                 }
629
630                 $api_node_list = $api->GetNodes($node_id);
631                 if ( count($api_node_list) > 0 )
632                 {
633                         $node_obj = new Node($api_node_list[0], True);
634                 } else {
635                         print "broken!!!";
636                         exit (1);
637                 }
638                 
639
640                 $optional_vals= array();
641
642                 $method = trim($_REQUEST['method']);
643                 if ( $node_obj->method != $method ) {
644                         $optional_vals['method']= $method;
645                 }
646
647                 $ip = trim($_REQUEST['ip']);
648                 if ( $node_obj->ip != $ip ) {
649                         $optional_vals['ip']= $ip;
650                 }
651
652                 // used to generate error strings for static fields only
653                 $static_fields= array();
654                 $static_fields['netmask']= "Netmask address";
655                 $static_fields['network']= "Network address";
656                 $static_fields['gateway']= "Gateway address";
657                 $static_fields['broadcast']= "Broadcast address";
658                 $static_fields['dns1']= "Primary DNS address";
659
660                 if ( $method == 'static' )
661                 {
662                         $netmask = trim($_REQUEST['netmask']);
663                         $network = trim($_REQUEST['network']);
664                         $gateway = trim($_REQUEST['gateway']);
665                         $broadcast = trim($_REQUEST['broadcast']);
666                         $dns1 = trim($_REQUEST['dns1']);
667                         $dns2 = trim($_REQUEST['dns2']);
668
669                         if( !is_valid_network_addr($network,$netmask) )
670                         {
671                                 $errors[] = "The network address does not coorespond to the netmask";
672                         }
673                 }
674
675                 if ( !isset($errors) || count($errors) == 0 )
676                 {
677                         // now, try to add the network.
678                         if( $method == 'static' )
679                         {
680                                 if ( $node_obj->gateway != $gateway ) {
681                                         $optional_vals['gateway']= $gateway;
682                                 }
683                                 if ( $node_obj->network != $network ) {
684                                         $optional_vals['network']= $network;
685                                 }
686                                 if ( $node_obj->broadcast != $broadcast ) {
687                                         $optional_vals['broadcast']= $broadcast;
688                                 }
689                                 if ( $node_obj->netmask != $netmask ) {
690                                         $optional_vals['netmask']= $netmask;
691                                 }
692                                 if ( $node_obj->dns1 != $dns1 ) {
693                                         $optional_vals['dns1']= $dns1;
694                                 }
695                                 if ( $node_obj->dns2 != $dns2 ) {
696                                         $optional_vals['dns2']= $dns2;
697                                 }
698                         }
699
700                         if ( count($optional_vals) > 0 )
701                         {
702                                 print_r($optional_vals);
703                                 $ret = $api->UpdateInterface( $node_obj->interface_id, $optional_vals);
704                                 if( $ret <= 0 ) {
705                                         $data['error'] = $api->error();
706                                         print $data['error'];
707                                 }
708                         }
709                 }
710
711                 $data['node_id'] = $node_id;
712                 if ( isset($errors) ) { $data['errors'] = $errors; }
713                 
714                 return $node_id;
715         }
716
717         function get_stage4_data($person, $data=NULL)
718         {
719                 global $api, $plc;
720                 if ( $data == NULL ){
721                     $data = array();
722                 }
723                 $data['node'] = $this->getnode($data['node_id']);
724                 $data['site'] = $this->getsite($data['site_id']);
725                 /*print "SITENAME: " . $data['site']['login_base'] . "<BR>";*/
726                 return $data;
727         }
728
729         function checknodeid($node_id)
730         {
731                 global $api, $plc;
732                 $plc_node_list = $api->GetNodes(array('node_id' => intval($node_id) ), array('node_id'));
733                 if ( count($plc_node_list) > 0 ) {
734                         return True;
735                 } else {
736                         return False;
737                 }
738         }
739
740         function getnode($node_id)
741         {
742                 global $api, $plc;
743                 $plc_node_list = $api->GetNodes(array('node_id' => intval($node_id) ));
744                 if ( count($plc_node_list) > 0 )
745                 {
746                         return new Node($plc_node_list[0], True);
747                 } else {
748                         return NULL;
749                 }
750         }
751
752 }
753 ?>