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( NULL, array( "name", "site_id", "login_base" ) );
122 $site_info= $api->GetSites( $person->getSites(), array( "name", "site_id", "login_base" ) );
124 sort_sites( $site_info );
128 var $run_update = True;
129 var $disp_errors = True;
130 function stage2_confirmpcu()
133 $this->load->helper(array('form', 'url'));
134 $this->load->library('validation');
136 $this->validation->set_error_delimiters('<span class="error">', '</span>');
137 $person = new Person($plc->person);
139 if ( isset($_REQUEST['pcu_choose']) ) {
140 $rules['pcu_id'] = "required";
141 $this->validation->set_rules($rules);
142 $fields['pcu_id'] = "PCU id";
143 $this->validation->set_fields($fields);
145 $result = $this->validation->run();
146 /* I don't know, shouldn't we redirect to the first stage in this case? */
147 $this->run_update = False;
148 $this->disp_errors = False;
149 print $this->validation->error_string . "<br>";
154 $rules['pcu_id'] = "required";
155 $rules['hostname'] = "trim|required";
156 $rules['ip'] = "trim|required|valid_ip";
157 $rules['username'] = "trim";
158 $rules['password'] = "trim|required";
159 $rules['notes'] = "trim";
160 $this->validation->set_rules($rules);
162 $fields['pcu_id'] = "PCU id";
163 $fields['hostname'] = "Hostname";
164 $fields['ip'] = "IP Address";
165 $fields['username'] = "Username";
166 $fields['password'] = "Password";
167 $this->validation->set_fields($fields);
169 if ( $this->validation->run() == FALSE )
171 print $this->validation->error_string . "<br>";
172 $this->run_update = False;
175 $data = $this->get_stage2_data($person);
177 $this->load->view('header', $data);
178 $this->load->view('debug', $data);
179 $this->load->view('stage2_pcu_confirm', $data);
180 $this->load->view('footer', $data);
184 function update_pcu($data)
187 /* add pcu, get pcu info */
188 $pcu_id = intval($this->validation->pcu_id);
189 $fields= array( 'protocol'=> '',
190 'model'=> $_REQUEST['model'],
191 'hostname'=> $this->validation->hostname,
192 'ip'=> $this->validation->ip,
193 'username'=> $this->validation->username,
194 'password'=> $this->validation->password,
195 'notes'=> $_REQUEST['notes'], );
196 $ret = $api->UpdatePCU( $pcu_id, $fields );
199 $data['error'] = $api->error();
200 print $data['error'];
206 function get_stage2_data($person)
211 if ( $this->run_update && isset($_REQUEST['pcu_update']) )
213 $this->update_pcu($data);
216 if ( isset($_REQUEST['pcu_id']) )
218 $pcu_id = intval($_REQUEST['pcu_id']);
219 $pcu_data = $api->GetPCUs(array('pcu_id'=>$pcu_id));
220 $pcu = new PCU($pcu_data[0]);
222 $data['pcu_id'] = $pcu_id;
223 $data['site_id'] = $pcu_data[0]['site_id'];
226 $data['default_site_list'] = $person->getSites();
227 $data['pcu_types'] = $api->GetPCUTypes(NULL, array('model', 'name'));
228 $data['pcu_site'] = $api->GetSites( $pcu_data[0]['site_id'], array( "name", "site_id", "login_base" ) );
233 function stage3_addnode($pcu_id, $site_id)
236 $this->load->helper(array('form', 'url'));
237 $this->load->library('validation');
239 $this->validation->set_error_delimiters('<span class="error">', '</span>');
240 $person = new Person($plc->person);
242 $data['pcu_id'] = intval($pcu_id);
243 $data['site_id'] = intval($site_id);
245 if ( isset($_REQUEST['pcu_proceed']) ) {
246 $rules['hostname'] = "";
247 $rules['model'] = "";
248 $rules['method'] = "";
250 $rules['netmask'] = "";
251 $rules['network'] = "";
252 $rules['gateway'] = "";
253 $rules['broadcast'] = "";
256 $this->validation->set_rules($rules);
257 $fields['hostname'] = "Hostname";
258 $fields['model'] = "Model";
259 $fields['method'] = "Method";
260 $fields['ip'] = "IP Address";
261 $fields['netmask'] = "Netmask Address";
262 $fields['network'] = "Network Address";
263 $fields['gateway'] = "Gateway Address";
264 $fields['broadcast'] = "Broadcast Address";
265 $fields['dns1'] = "Primary DNS Address";
266 $fields['dns2'] = "Secondary DNS Address";
267 $this->validation->set_fields($fields);
269 $result = $this->validation->run();
270 /*print "RESULT: ".$result . "<br>";*/
271 # TODO: if result is false, redirect to beginning.
272 $this->disp_errors = False;
273 print $this->validation->error_string . "<br>";
275 $rules['hostname'] = "trim|required";
276 $rules['model'] = "trim|required";
277 $rules['method'] = "required";
278 $rules['ip'] = "trim|required|valid_ip";
279 if ( isset ($_REQUEST['method']) && $_REQUEST['method'] == 'static' )
281 $rules['netmask'] = "trim|valid_ip";
282 $rules['network'] = "trim|valid_ip";
283 $rules['gateway'] = "trim|valid_ip";
284 $rules['broadcast'] = "trim|valid_ip";
285 $rules['dns1'] = "trim|valid_ip";
286 $rules['dns2'] = "trim|valid_ip";
288 $rules['netmask'] = "";
289 $rules['network'] = "";
290 $rules['gateway'] = "";
291 $rules['broadcast'] = "";
295 $this->validation->set_rules($rules);
297 $fields['hostname'] = "Hostname";
298 $fields['model'] = "Model";
299 $fields['method'] = "Method";
300 $fields['ip'] = "IP Address";
301 $fields['netmask'] = "Netmask Address";
302 $fields['network'] = "Network Address";
303 $fields['gateway'] = "Gateway Address";
304 $fields['broadcast'] = "Broadcast Address";
305 $fields['dns1'] = "Primary DNS Address";
306 $fields['dns2'] = "Secondary DNS Address";
307 $this->validation->set_fields($fields);
309 if ($this->validation->run() == TRUE)
311 /* b/c the submit is valid, all values are minimally consistent. */
312 $this->node_id = $this->add_node($data);
315 $data = $this->get_stage3_data($person, $data);
317 $this->load->view('header', $data);
318 $this->load->view('debug', $data);
319 $this->load->view('stage3_node_choose', $data);
320 $this->load->view('footer', $data);
323 function add_node(&$data)
326 $hostname = trim($_REQUEST['hostname']);
327 $model= trim($_REQUEST['model']);
328 $method = trim($_REQUEST['method']);
329 $ip = trim($_REQUEST['ip']);
330 if ( $method == 'static' )
332 $netmask = trim($_REQUEST['netmask']);
333 $network = trim($_REQUEST['network']);
334 $gateway = trim($_REQUEST['gateway']);
335 $broadcast = trim($_REQUEST['broadcast']);
336 $dns1 = trim($_REQUEST['dns1']);
337 $dns2 = trim($_REQUEST['dns2']);
340 // used to generate error strings for static fields only
341 $static_fields= array();
342 $static_fields['netmask']= "Netmask address";
343 $static_fields['network']= "Network address";
344 $static_fields['gateway']= "Gateway address";
345 $static_fields['broadcast']= "Broadcast address";
346 $static_fields['dns1']= "Primary DNS address";
348 if( $method == 'static' )
350 if( !is_valid_network_addr($network,$netmask) )
352 $errors[] = "The network address does not coorespond to the netmask";
356 if( !isset($errors) || count($errors) == 0 )
358 // add new node and its network
359 $optional_vals= array( "hostname"=>$hostname, "model"=>$model );
361 $site_id= $data['site_id'];
362 $node_id= $api->AddNode( intval( $site_id ), $optional_vals );
363 if( $node_id <= 0 ) {
364 $data['error'] = $api->error();
365 print $data['error'];
368 // now, try to add the network.
369 $optional_vals= array();
370 $optional_vals['is_primary']= true;
371 $optional_vals['ip']= $ip;
372 $optional_vals['type']= 'ipv4';
373 $optional_vals['method']= $method;
375 if( $method == 'static' )
377 $optional_vals['gateway']= $gateway;
378 $optional_vals['network']= $network;
379 $optional_vals['broadcast']= $broadcast;
380 $optional_vals['netmask']= $netmask;
381 $optional_vals['dns1']= $dns1;
383 $optional_vals['dns2']= $dns2;
387 $nodenetwork_id= $api->AddNodeNetwork( $node_id, $optional_vals);
388 if( $nodenetwork_id <= 0 ) {
389 $data['error'] = $api->error();
390 print $data['error'];
393 $success = $api->AddNodeToPCU( $node_id, $data['pcu_id'], 1);
394 if( !isset($success) || $success <= 0 ) {
395 $data['error'] = $api->error();
396 print $data['error'];
401 $data['node_id'] = $node_id;
402 if ( isset($errors) ) { $data['errors'] = $errors; }
407 function get_stage3_data($person, $data=NULL)
410 if ( $data == NULL ){
413 $data['default_site_list'] = array($data['site_id']);
414 $data['node_list'] = $this->getnodelist($data['site_id']);
415 $data['site'] = $this->getsite($data['site_id']);
419 function getnodelist($site_id)
422 $plc_node_list = $api->GetNodes(array('site_id' => intval($site_id) ));
423 return PlcObject::constructList('Node', $plc_node_list);
426 function getsite($site_id)
429 $site_info = $api->GetSites($site_id, array( "name", "site_id", "login_base" ) );
430 return $site_info[0];
433 function stage45_mappcu($pcu_id=0, $site_id=0, $node_id=0)
436 $this->load->helper(array('form', 'url'));
437 $this->load->library('validation');
439 $this->validation->set_error_delimiters('<span class="error">', '</span>');
441 $fields['port_number'] = "Port Number";
442 $this->validation->set_fields($fields);
445 $data['pcu_id'] = intval($pcu_id);
446 $data['site_id'] = intval($site_id);
447 $data['node_id'] = intval($node_id);
449 if ( isset($_REQUEST['node_confirm']) ) {
450 /* skip the rules, since we're just displaying the page. */
451 $rules['port_number'] = "";
452 $this->validation->set_rules($rules);
455 /* setup rules, to validate the form data. */
456 $rules['port_number'] = "trim|required|intval";
457 $this->validation->set_rules($rules);
459 $result = $this->validation->run();
463 $port = $this->validation->port_number;
464 /* if we do not delete the node from the PCU first, a fault is raised */
465 $ret = $api->DeleteNodeFromPCU($data['node_id'], $data['pcu_id']);
466 $ret = $api->AddNodeToPCU($data['node_id'], $data['pcu_id'], $port);
469 $data['error'] = $api->error();
470 print $data['error'];
473 $this->disp_errors = False;
474 print $this->validation->error_string . "<br>";
478 $data['node'] = $this->getnode($data['node_id']);
479 $api_pcus = $api->GetPCUs($data['node']->pcu_ids);
482 # NOTE: find index of node id, then pull out that index of
483 $index = array_search($data['node_id'], $pcu['node_ids']);
484 $data['pcu_port'] = $pcu['ports'][$index];
485 $data['stage'] = 4.5;
486 #$data = $this->get_stage4_data($person, $data);
487 $this->load->view('header', $data);
488 $this->load->view('debug', $data);
489 $this->load->view('stage45_pcuport', $data);
490 $this->load->view('footer', $data);
493 function stage4_confirmnode($pcu_id=0, $site_id=0)
496 $this->load->helper(array('form', 'url'));
497 $this->load->library('validation');
499 $this->validation->set_error_delimiters('<span class="error">', '</span>');
500 $person = new Person($plc->person);
502 $fields['hostname'] = "Hostname";
503 $fields['model'] = "Model";
504 $fields['method'] = "Method";
505 $fields['ip'] = "IP Address";
506 $fields['netmask'] = "Netmask Address";
507 $fields['network'] = "Network Address";
508 $fields['gateway'] = "Gateway Address";
509 $fields['broadcast']= "Broadcast Address";
510 $fields['dns1'] = "Primary DNS Address";
511 $fields['dns2'] = "Secondary DNS Address";
512 $fields['node_id'] = "NODE id";
513 $this->validation->set_fields($fields);
516 $data['pcu_id'] = intval($pcu_id);
517 $data['site_id'] = intval($site_id);
519 if ( isset($_REQUEST['node_choose']) ) {
520 $rules['node_id'] = "required|intval";
521 $rules['hostname'] = "";
522 $rules['model'] = "";
523 $rules['method'] = "";
525 $rules['netmask'] = "";
526 $rules['network'] = "";
527 $rules['gateway'] = "";
528 $rules['broadcast'] = "";
531 $this->validation->set_rules($rules);
533 $result = $this->validation->run();
534 /*print "RESULT: ".$result . "<br>";*/
535 # TODO: if result is false, redirect to beginning.
536 $this->disp_errors = False;
537 print $this->validation->error_string . "<br>";
539 $rules['hostname'] = "trim|required";
540 $rules['model'] = "trim|required";
541 $rules['method'] = "required";
542 $rules['ip'] = "trim|required|valid_ip";
543 if ( isset ($_REQUEST['method']) && $_REQUEST['method'] == 'static' )
545 $rules['netmask'] = "trim|valid_ip";
546 $rules['network'] = "trim|valid_ip";
547 $rules['gateway'] = "trim|valid_ip";
548 $rules['broadcast'] = "trim|valid_ip";
549 $rules['dns1'] = "trim|valid_ip";
550 $rules['dns2'] = "trim";
552 # NOTE: There are no conditions that must be met for these fields.
553 $rules['netmask'] = "";
554 $rules['network'] = "";
555 $rules['gateway'] = "";
556 $rules['broadcast'] = "";
560 $rules['node_id'] = "required|intval";
561 $this->validation->set_rules($rules);
563 if ($this->validation->run() == TRUE)
565 /* b/c the submit is valid, all values are minimally consistent. */
566 $this->node_id = $this->update_node($data);
569 $data['node_id'] = intval($this->validation->node_id);
571 if ( $this->checknodeid($data['node_id']) )
573 $data = $this->get_stage4_data($person, $data);
574 $this->load->view('header', $data);
575 $this->load->view('debug', $data);
576 $this->load->view('stage4_node_confirm', $data);
577 $this->load->view('footer', $data);
579 print "You must select a valid Node before continuing.";
583 function update_node(&$data)
585 # TODO: RECODE To update values instead of adding them...
587 $hostname = trim($_REQUEST['hostname']);
588 $model= trim($_REQUEST['model']);
589 $node_id = intval($this->validation->node_id);
590 $ret = $api->UpdateNode( $node_id, array('hostname' => $hostname,
593 $data['error'] = $api->error();
594 print $data['error'];
597 $api_node_list = $api->GetNodes($node_id);
598 if ( count($api_node_list) > 0 )
600 $node_obj = new Node($api_node_list[0]);
607 $optional_vals= array();
609 $method = trim($_REQUEST['method']);
610 if ( $node_obj->method != $method ) {
611 $optional_vals['method']= $method;
614 $ip = trim($_REQUEST['ip']);
615 if ( $node_obj->ip != $ip ) {
616 $optional_vals['ip']= $ip;
619 // used to generate error strings for static fields only
620 $static_fields= array();
621 $static_fields['netmask']= "Netmask address";
622 $static_fields['network']= "Network address";
623 $static_fields['gateway']= "Gateway address";
624 $static_fields['broadcast']= "Broadcast address";
625 $static_fields['dns1']= "Primary DNS address";
627 if ( $method == 'static' )
629 $netmask = trim($_REQUEST['netmask']);
630 $network = trim($_REQUEST['network']);
631 $gateway = trim($_REQUEST['gateway']);
632 $broadcast = trim($_REQUEST['broadcast']);
633 $dns1 = trim($_REQUEST['dns1']);
634 $dns2 = trim($_REQUEST['dns2']);
636 if( !is_valid_network_addr($network,$netmask) )
638 $errors[] = "The network address does not coorespond to the netmask";
642 if ( !isset($errors) || count($errors) == 0 )
644 // now, try to add the network.
645 if( $method == 'static' )
647 if ( $node_obj->gateway != $gateway ) {
648 $optional_vals['gateway']= $gateway;
650 if ( $node_obj->network != $network ) {
651 $optional_vals['network']= $network;
653 if ( $node_obj->broadcast != $broadcast ) {
654 $optional_vals['broadcast']= $broadcast;
656 if ( $node_obj->netmask != $netmask ) {
657 $optional_vals['netmask']= $netmask;
659 if ( $node_obj->dns1 != $dns1 ) {
660 $optional_vals['dns1']= $dns1;
662 if ( $node_obj->dns2 != $dns2 ) {
663 $optional_vals['dns2']= $dns2;
667 if ( count($optional_vals) > 0 )
669 print_r($optional_vals);
670 $ret = $api->UpdateNodeNetwork( $node_obj->nodenetwork_id, $optional_vals);
672 $data['error'] = $api->error();
673 print $data['error'];
678 $data['node_id'] = $node_id;
679 if ( isset($errors) ) { $data['errors'] = $errors; }
684 function get_stage4_data($person, $data=NULL)
687 if ( $data == NULL ){
690 $data['node'] = $this->getnode($data['node_id']);
691 $data['site'] = $this->getsite($data['site_id']);
695 function checknodeid($node_id)
698 $plc_node_list = $api->GetNodes(array('node_id' => intval($node_id) ), array('node_id'));
699 if ( count($plc_node_list) > 0 ) {
706 function getnode($node_id)
709 $plc_node_list = $api->GetNodes(array('node_id' => intval($node_id) ));
710 if ( count($plc_node_list) > 0 )
712 return new Node($plc_node_list[0]);