3 require_once 'plc_login.php'; // Require login
4 require_once 'plc_session.php'; // Get session and API handles
5 global $plc, $api, $adm;
8 require_once 'plc_drupal.php';
10 include 'plc_header.php';
13 require_once 'plc_functions.php';
14 //require_once 'plc_sorts.php';
15 include 'plc_objects.php';
18 class Register extends Controller {
23 $this->load->helper(array('form', 'url'));
26 $this->load->view('header', $data);
27 $this->load->view('debug', $data);
28 $this->load->view('stage0', $data);
29 $this->load->view('footer', $data);
31 function stage1_addpcu()
34 $this->load->helper(array('form', 'url'));
35 $this->load->library('validation');
37 $this->validation->set_error_delimiters('<span class="error">', '</span>');
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);
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);
54 $person = new Person($plc->person);
56 if ($this->validation->run() == TRUE)
58 if ($this->validation->model != "none-selected" )
60 /* b/c the submit is valid, it doesn't matter if pcu_register is set */
61 $this->pcu_id = $this->add_pcu($data);
64 $data = $this->get_stage1_data($person, $data);
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);
72 function add_pcu(&$data)
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 );
87 $data['error'] = $api->error();
90 $data['pcu_id'] = $pcu_id;
95 function get_stage1_data($person, $data=NULL)
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);
108 function getpculist($person)
111 $plc_pcu_list = $api->GetPCUs(array('site_id' => $person->getSites()));
112 return PlcObject::constructList('PCU', $plc_pcu_list);
115 function getsitelist($person)
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" ) );
123 $site_info= $api->GetSites( $person->getSites(), array( "name", "site_id", "login_base" ) );
125 //sort_sites( $site_info );
129 var $run_update = True;
130 var $disp_errors = True;
131 function stage2_confirmpcu()
134 $this->load->helper(array('form', 'url'));
135 $this->load->library('validation');
137 $this->validation->set_error_delimiters('<span class="error">', '</span>');
138 $person = new Person($plc->person);
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);
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>";
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);
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);
170 if ( $this->validation->run() == FALSE )
172 print $this->validation->error_string . "<br>";
173 $this->run_update = False;
176 $data = $this->get_stage2_data($person);
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);
185 function update_pcu($data)
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 );
200 $data['error'] = $api->error();
201 print $data['error'];
207 function get_stage2_data($person)
212 if ( $this->run_update && isset($_REQUEST['pcu_update']) )
214 $this->update_pcu($data);
217 if ( isset($_REQUEST['pcu_id']) )
219 $pcu_id = intval($_REQUEST['pcu_id']);
220 $pcu_data = $api->GetPCUs(array('pcu_id'=>$pcu_id));
221 $pcu = new PCU($pcu_data[0]);
223 $data['pcu_id'] = $pcu_id;
224 $data['site_id'] = $pcu_data[0]['site_id'];
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" ) );
234 function stage3_addnode($pcu_id, $site_id)
237 $this->load->helper(array('form', 'url'));
238 $this->load->library('validation');
240 $this->validation->set_error_delimiters('<span class="error">', '</span>');
241 $person = new Person($plc->person);
243 $data['pcu_id'] = intval($pcu_id);
244 $data['site_id'] = intval($site_id);
246 if ( isset($_REQUEST['pcu_proceed']) ) {
247 $rules['hostname'] = "";
248 $rules['model'] = "";
249 $rules['method'] = "";
251 $rules['netmask'] = "";
252 $rules['network'] = "";
253 $rules['gateway'] = "";
254 $rules['broadcast'] = "";
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);
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>";
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' )
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";
289 $rules['netmask'] = "";
290 $rules['network'] = "";
291 $rules['gateway'] = "";
292 $rules['broadcast'] = "";
296 $this->validation->set_rules($rules);
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);
310 if ($this->validation->run() == TRUE)
312 /* b/c the submit is valid, all values are minimally consistent. */
313 $this->node_id = $this->add_node($data);
316 $data = $this->get_stage3_data($person, $data);
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);
324 function add_node(&$data)
327 print "Adding Node\n<br>";
328 $hostname = trim($_REQUEST['hostname']);
329 $model= trim($_REQUEST['model']);
330 $method = trim($_REQUEST['method']);
331 $ip = trim($_REQUEST['ip']);
332 if ( $method == 'static' )
334 $netmask = trim($_REQUEST['netmask']);
335 $network = trim($_REQUEST['network']);
336 $gateway = trim($_REQUEST['gateway']);
337 $broadcast = trim($_REQUEST['broadcast']);
338 $dns1 = trim($_REQUEST['dns1']);
339 $dns2 = trim($_REQUEST['dns2']);
342 // used to generate error strings for static fields only
343 $static_fields= array();
344 $static_fields['netmask']= "Netmask address";
345 $static_fields['network']= "Network address";
346 $static_fields['gateway']= "Gateway address";
347 $static_fields['broadcast']= "Broadcast address";
348 $static_fields['dns1']= "Primary DNS address";
350 if( $method == 'static' )
352 if( !is_valid_network_addr($network,$netmask) )
354 $errors[] = "The network address does not coorespond to the netmask";
358 if( !isset($errors) || count($errors) == 0 )
360 // add new node and its network
361 $optional_vals= array( "hostname"=>$hostname, "model"=>$model );
363 $site_id= $data['site_id'];
364 $node_id= $api->AddNode( intval( $site_id ), $optional_vals );
365 if( $node_id <= 0 ) {
366 $data['error'] = $api->error();
367 print $data['error'];
370 // now, try to add the network.
371 $optional_vals= array();
372 $optional_vals['is_primary']= true;
373 $optional_vals['ip']= $ip;
374 $optional_vals['type']= 'ipv4';
375 $optional_vals['method']= $method;
377 if( $method == 'static' )
379 $optional_vals['gateway']= $gateway;
380 $optional_vals['network']= $network;
381 $optional_vals['broadcast']= $broadcast;
382 $optional_vals['netmask']= $netmask;
383 $optional_vals['dns1']= $dns1;
385 $optional_vals['dns2']= $dns2;
389 $interface_id= $api->AddInterface( $node_id, $optional_vals);
390 if( $interface_id <= 0 ) {
391 $data['error'] = $api->error();
392 print $data['error'];
395 $success = $api->AddNodeToPCU( $node_id, $data['pcu_id'], 1);
396 if( !isset($success) || $success <= 0 ) {
397 $data['error'] = $api->error();
398 print $data['error'];
403 $data['node_id'] = $node_id;
404 if ( isset($errors) ) { $data['errors'] = $errors; }
409 function get_stage3_data($person, $data=NULL)
412 if ( $data == NULL ){
415 $data['default_site_list'] = array($data['site_id']);
416 $data['node_list'] = $this->getnodelist($data['site_id']);
417 $data['site'] = $this->getsite($data['site_id']);
421 function getnodelist($site_id)
424 $plc_node_list = $api->GetNodes(array('site_id' => intval($site_id) ));
426 foreach ($plc_node_list as $plc_node)
428 $ret[] = new Node($plc_node, True);
433 function getsite($site_id)
436 $site_info = $api->GetSites($site_id, array( "name", "site_id", "login_base" ) );
437 return $site_info[0];
440 function stage45_mappcu($pcu_id=0, $site_id=0, $node_id=0)
443 $this->load->helper(array('form', 'url'));
444 $this->load->library('validation');
446 $this->validation->set_error_delimiters('<span class="error">', '</span>');
448 $fields['port_number'] = "Port Number";
449 $this->validation->set_fields($fields);
452 $data['pcu_id'] = intval($pcu_id);
453 $data['site_id'] = intval($site_id);
454 $data['node_id'] = intval($node_id);
456 if ( isset($_REQUEST['node_confirm']) ) {
457 /* skip the rules, since we're just displaying the page. */
458 $rules['port_number'] = "";
459 $this->validation->set_rules($rules);
462 /* setup rules, to validate the form data. */
463 $rules['port_number'] = "trim|required|intval";
464 $this->validation->set_rules($rules);
466 $result = $this->validation->run();
470 $port = $this->validation->port_number;
471 /* if we do not delete the node from the PCU first, a fault is raised */
472 $ret = $api->DeleteNodeFromPCU($data['node_id'], $data['pcu_id']);
473 $ret = $api->AddNodeToPCU($data['node_id'], $data['pcu_id'], $port);
476 $data['error'] = $api->error();
477 print $data['error'];
480 $this->disp_errors = False;
481 print $this->validation->error_string . "<br>";
484 $data['node'] = $this->getnode($data['node_id']);
485 if ( sizeof($data['node']->pcu_ids) == 0)
487 $data['pcu_assigned'] = False;
488 $data['pcu_port'] = -1;
490 $data['pcu_assigned'] = True;
491 $api_pcus = $api->GetPCUs($data['node']->pcu_ids);
493 # NOTE: find index of node id, then pull out that index of
494 $index = array_search($data['node_id'], $pcu['node_ids']);
495 $data['pcu_port'] = $pcu['ports'][$index];
498 $data['stage'] = 4.5;
499 #$data = $this->get_stage4_data($person, $data);
500 $this->load->view('header', $data);
501 $this->load->view('debug', $data);
502 $this->load->view('stage45_pcuport', $data);
503 $this->load->view('footer', $data);
506 function stage4_confirmnode($pcu_id=0, $site_id=0)
509 $this->load->helper(array('form', 'url'));
510 $this->load->library('validation');
512 $this->validation->set_error_delimiters('<span class="error">', '</span>');
513 $person = new Person($plc->person);
515 $fields['hostname'] = "Hostname";
516 $fields['model'] = "Model";
517 $fields['method'] = "Method";
518 $fields['ip'] = "IP Address";
519 $fields['netmask'] = "Netmask Address";
520 $fields['network'] = "Network Address";
521 $fields['gateway'] = "Gateway Address";
522 $fields['broadcast']= "Broadcast Address";
523 $fields['dns1'] = "Primary DNS Address";
524 $fields['dns2'] = "Secondary DNS Address";
525 $fields['node_id'] = "NODE id";
526 $this->validation->set_fields($fields);
529 $data['pcu_id'] = intval($pcu_id);
530 $data['site_id'] = intval($site_id);
532 if ( isset($_REQUEST['node_choose']) ) {
533 $rules['node_id'] = "required|intval";
534 $rules['hostname'] = "";
535 $rules['model'] = "";
536 $rules['method'] = "";
538 $rules['netmask'] = "";
539 $rules['network'] = "";
540 $rules['gateway'] = "";
541 $rules['broadcast'] = "";
544 $this->validation->set_rules($rules);
546 $result = $this->validation->run();
547 /*print "RESULT: ".$result . "<br>";*/
548 # TODO: if result is false, redirect to beginning.
549 $this->disp_errors = False;
550 print $this->validation->error_string . "<br>";
552 $rules['hostname'] = "trim|required";
553 $rules['model'] = "trim|required";
554 $rules['method'] = "required";
555 $rules['ip'] = "trim|required|valid_ip";
556 if ( isset ($_REQUEST['method']) && $_REQUEST['method'] == 'static' )
558 $rules['netmask'] = "trim|valid_ip";
559 $rules['network'] = "trim|valid_ip";
560 $rules['gateway'] = "trim|valid_ip";
561 $rules['broadcast'] = "trim|valid_ip";
562 $rules['dns1'] = "trim|valid_ip";
563 $rules['dns2'] = "trim";
565 # NOTE: There are no conditions that must be met for these fields.
566 $rules['netmask'] = "";
567 $rules['network'] = "";
568 $rules['gateway'] = "";
569 $rules['broadcast'] = "";
573 $rules['node_id'] = "required|intval";
574 $this->validation->set_rules($rules);
576 if ($this->validation->run() == TRUE)
578 /* b/c the submit is valid, all values are minimally consistent. */
579 $this->node_id = $this->update_node($data);
582 $data['node_id'] = intval($this->validation->node_id);
584 if ( $this->checknodeid($data['node_id']) )
586 $data = $this->get_stage4_data($person, $data);
587 $this->load->view('header', $data);
588 $this->load->view('debug', $data);
589 $this->load->view('stage4_node_confirm', $data);
590 $this->load->view('footer', $data);
592 print "You must select a valid Node before continuing.";
596 function update_node(&$data)
598 # TODO: RECODE To update values instead of adding them...
600 $hostname = trim($_REQUEST['hostname']);
601 $model= trim($_REQUEST['model']);
602 $node_id = intval($this->validation->node_id);
603 $ret = $api->UpdateNode( $node_id, array('hostname' => $hostname,
606 $data['error'] = $api->error();
607 print $data['error'];
610 $api_node_list = $api->GetNodes($node_id);
611 if ( count($api_node_list) > 0 )
613 $node_obj = new Node($api_node_list[0], True);
620 $optional_vals= array();
622 $method = trim($_REQUEST['method']);
623 if ( $node_obj->method != $method ) {
624 $optional_vals['method']= $method;
627 $ip = trim($_REQUEST['ip']);
628 if ( $node_obj->ip != $ip ) {
629 $optional_vals['ip']= $ip;
632 // used to generate error strings for static fields only
633 $static_fields= array();
634 $static_fields['netmask']= "Netmask address";
635 $static_fields['network']= "Network address";
636 $static_fields['gateway']= "Gateway address";
637 $static_fields['broadcast']= "Broadcast address";
638 $static_fields['dns1']= "Primary DNS address";
640 if ( $method == 'static' )
642 $netmask = trim($_REQUEST['netmask']);
643 $network = trim($_REQUEST['network']);
644 $gateway = trim($_REQUEST['gateway']);
645 $broadcast = trim($_REQUEST['broadcast']);
646 $dns1 = trim($_REQUEST['dns1']);
647 $dns2 = trim($_REQUEST['dns2']);
649 if( !is_valid_network_addr($network,$netmask) )
651 $errors[] = "The network address does not coorespond to the netmask";
655 if ( !isset($errors) || count($errors) == 0 )
657 // now, try to add the network.
658 if( $method == 'static' )
660 if ( $node_obj->gateway != $gateway ) {
661 $optional_vals['gateway']= $gateway;
663 if ( $node_obj->network != $network ) {
664 $optional_vals['network']= $network;
666 if ( $node_obj->broadcast != $broadcast ) {
667 $optional_vals['broadcast']= $broadcast;
669 if ( $node_obj->netmask != $netmask ) {
670 $optional_vals['netmask']= $netmask;
672 if ( $node_obj->dns1 != $dns1 ) {
673 $optional_vals['dns1']= $dns1;
675 if ( $node_obj->dns2 != $dns2 ) {
676 $optional_vals['dns2']= $dns2;
680 if ( count($optional_vals) > 0 )
682 print_r($optional_vals);
683 $ret = $api->UpdateInterface( $node_obj->interface_id, $optional_vals);
685 $data['error'] = $api->error();
686 print $data['error'];
691 $data['node_id'] = $node_id;
692 if ( isset($errors) ) { $data['errors'] = $errors; }
697 function get_stage4_data($person, $data=NULL)
700 if ( $data == NULL ){
703 $data['node'] = $this->getnode($data['node_id']);
704 $data['site'] = $this->getsite($data['site_id']);
705 /*print "SITENAME: " . $data['site']['login_base'] . "<BR>";*/
709 function checknodeid($node_id)
712 $plc_node_list = $api->GetNodes(array('node_id' => intval($node_id) ), array('node_id'));
713 if ( count($plc_node_list) > 0 ) {
720 function getnode($node_id)
723 $plc_node_list = $api->GetNodes(array('node_id' => intval($node_id) ));
724 if ( count($plc_node_list) > 0 )
726 return new Node($plc_node_list[0], True);