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 print "Adding Node\n<br>";
327 $hostname = trim($_REQUEST['hostname']);
328 $model= trim($_REQUEST['model']);
329 $method = trim($_REQUEST['method']);
330 $ip = trim($_REQUEST['ip']);
331 if ( $method == 'static' )
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']);
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";
349 if( $method == 'static' )
351 if( !is_valid_network_addr($network,$netmask) )
353 $errors[] = "The network address does not coorespond to the netmask";
357 if( !isset($errors) || count($errors) == 0 )
359 // add new node and its network
360 $optional_vals= array( "hostname"=>$hostname, "model"=>$model );
362 $site_id= $data['site_id'];
363 $node_id= $api->AddNode( intval( $site_id ), $optional_vals );
364 if( $node_id <= 0 ) {
365 $data['error'] = $api->error();
366 print $data['error'];
369 // now, try to add the network.
370 $optional_vals= array();
371 $optional_vals['is_primary']= true;
372 $optional_vals['ip']= $ip;
373 $optional_vals['type']= 'ipv4';
374 $optional_vals['method']= $method;
376 if( $method == 'static' )
378 $optional_vals['gateway']= $gateway;
379 $optional_vals['network']= $network;
380 $optional_vals['broadcast']= $broadcast;
381 $optional_vals['netmask']= $netmask;
382 $optional_vals['dns1']= $dns1;
384 $optional_vals['dns2']= $dns2;
388 $nodenetwork_id= $api->AddNodeNetwork( $node_id, $optional_vals);
389 if( $nodenetwork_id <= 0 ) {
390 $data['error'] = $api->error();
391 print $data['error'];
394 $success = $api->AddNodeToPCU( $node_id, $data['pcu_id'], 1);
395 if( !isset($success) || $success <= 0 ) {
396 $data['error'] = $api->error();
397 print $data['error'];
402 $data['node_id'] = $node_id;
403 if ( isset($errors) ) { $data['errors'] = $errors; }
408 function get_stage3_data($person, $data=NULL)
411 if ( $data == NULL ){
414 $data['default_site_list'] = array($data['site_id']);
415 $data['node_list'] = $this->getnodelist($data['site_id']);
416 $data['site'] = $this->getsite($data['site_id']);
420 function getnodelist($site_id)
423 $plc_node_list = $api->GetNodes(array('site_id' => intval($site_id) ));
424 return PlcObject::constructList('Node', $plc_node_list);
427 function getsite($site_id)
430 $site_info = $api->GetSites($site_id, array( "name", "site_id", "login_base" ) );
431 return $site_info[0];
434 function stage45_mappcu($pcu_id=0, $site_id=0, $node_id=0)
437 $this->load->helper(array('form', 'url'));
438 $this->load->library('validation');
440 $this->validation->set_error_delimiters('<span class="error">', '</span>');
442 $fields['port_number'] = "Port Number";
443 $this->validation->set_fields($fields);
446 $data['pcu_id'] = intval($pcu_id);
447 $data['site_id'] = intval($site_id);
448 $data['node_id'] = intval($node_id);
450 if ( isset($_REQUEST['node_confirm']) ) {
451 /* skip the rules, since we're just displaying the page. */
452 $rules['port_number'] = "";
453 $this->validation->set_rules($rules);
456 /* setup rules, to validate the form data. */
457 $rules['port_number'] = "trim|required|intval";
458 $this->validation->set_rules($rules);
460 $result = $this->validation->run();
464 $port = $this->validation->port_number;
465 /* if we do not delete the node from the PCU first, a fault is raised */
466 $ret = $api->DeleteNodeFromPCU($data['node_id'], $data['pcu_id']);
467 $ret = $api->AddNodeToPCU($data['node_id'], $data['pcu_id'], $port);
470 $data['error'] = $api->error();
471 print $data['error'];
474 $this->disp_errors = False;
475 print $this->validation->error_string . "<br>";
479 $data['node'] = $this->getnode($data['node_id']);
480 $api_pcus = $api->GetPCUs($data['node']->pcu_ids);
483 # NOTE: find index of node id, then pull out that index of
484 $index = array_search($data['node_id'], $pcu['node_ids']);
485 $data['pcu_port'] = $pcu['ports'][$index];
486 $data['stage'] = 4.5;
487 #$data = $this->get_stage4_data($person, $data);
488 $this->load->view('header', $data);
489 $this->load->view('debug', $data);
490 $this->load->view('stage45_pcuport', $data);
491 $this->load->view('footer', $data);
494 function stage4_confirmnode($pcu_id=0, $site_id=0)
497 $this->load->helper(array('form', 'url'));
498 $this->load->library('validation');
500 $this->validation->set_error_delimiters('<span class="error">', '</span>');
501 $person = new Person($plc->person);
503 $fields['hostname'] = "Hostname";
504 $fields['model'] = "Model";
505 $fields['method'] = "Method";
506 $fields['ip'] = "IP Address";
507 $fields['netmask'] = "Netmask Address";
508 $fields['network'] = "Network Address";
509 $fields['gateway'] = "Gateway Address";
510 $fields['broadcast']= "Broadcast Address";
511 $fields['dns1'] = "Primary DNS Address";
512 $fields['dns2'] = "Secondary DNS Address";
513 $fields['node_id'] = "NODE id";
514 $this->validation->set_fields($fields);
517 $data['pcu_id'] = intval($pcu_id);
518 $data['site_id'] = intval($site_id);
520 if ( isset($_REQUEST['node_choose']) ) {
521 $rules['node_id'] = "required|intval";
522 $rules['hostname'] = "";
523 $rules['model'] = "";
524 $rules['method'] = "";
526 $rules['netmask'] = "";
527 $rules['network'] = "";
528 $rules['gateway'] = "";
529 $rules['broadcast'] = "";
532 $this->validation->set_rules($rules);
534 $result = $this->validation->run();
535 /*print "RESULT: ".$result . "<br>";*/
536 # TODO: if result is false, redirect to beginning.
537 $this->disp_errors = False;
538 print $this->validation->error_string . "<br>";
540 $rules['hostname'] = "trim|required";
541 $rules['model'] = "trim|required";
542 $rules['method'] = "required";
543 $rules['ip'] = "trim|required|valid_ip";
544 if ( isset ($_REQUEST['method']) && $_REQUEST['method'] == 'static' )
546 $rules['netmask'] = "trim|valid_ip";
547 $rules['network'] = "trim|valid_ip";
548 $rules['gateway'] = "trim|valid_ip";
549 $rules['broadcast'] = "trim|valid_ip";
550 $rules['dns1'] = "trim|valid_ip";
551 $rules['dns2'] = "trim";
553 # NOTE: There are no conditions that must be met for these fields.
554 $rules['netmask'] = "";
555 $rules['network'] = "";
556 $rules['gateway'] = "";
557 $rules['broadcast'] = "";
561 $rules['node_id'] = "required|intval";
562 $this->validation->set_rules($rules);
564 if ($this->validation->run() == TRUE)
566 /* b/c the submit is valid, all values are minimally consistent. */
567 $this->node_id = $this->update_node($data);
570 $data['node_id'] = intval($this->validation->node_id);
572 if ( $this->checknodeid($data['node_id']) )
574 $data = $this->get_stage4_data($person, $data);
575 $this->load->view('header', $data);
576 $this->load->view('debug', $data);
577 $this->load->view('stage4_node_confirm', $data);
578 $this->load->view('footer', $data);
580 print "You must select a valid Node before continuing.";
584 function update_node(&$data)
586 # TODO: RECODE To update values instead of adding them...
588 $hostname = trim($_REQUEST['hostname']);
589 $model= trim($_REQUEST['model']);
590 $node_id = intval($this->validation->node_id);
591 $ret = $api->UpdateNode( $node_id, array('hostname' => $hostname,
594 $data['error'] = $api->error();
595 print $data['error'];
598 $api_node_list = $api->GetNodes($node_id);
599 if ( count($api_node_list) > 0 )
601 $node_obj = new Node($api_node_list[0]);
608 $optional_vals= array();
610 $method = trim($_REQUEST['method']);
611 if ( $node_obj->method != $method ) {
612 $optional_vals['method']= $method;
615 $ip = trim($_REQUEST['ip']);
616 if ( $node_obj->ip != $ip ) {
617 $optional_vals['ip']= $ip;
620 // used to generate error strings for static fields only
621 $static_fields= array();
622 $static_fields['netmask']= "Netmask address";
623 $static_fields['network']= "Network address";
624 $static_fields['gateway']= "Gateway address";
625 $static_fields['broadcast']= "Broadcast address";
626 $static_fields['dns1']= "Primary DNS address";
628 if ( $method == 'static' )
630 $netmask = trim($_REQUEST['netmask']);
631 $network = trim($_REQUEST['network']);
632 $gateway = trim($_REQUEST['gateway']);
633 $broadcast = trim($_REQUEST['broadcast']);
634 $dns1 = trim($_REQUEST['dns1']);
635 $dns2 = trim($_REQUEST['dns2']);
637 if( !is_valid_network_addr($network,$netmask) )
639 $errors[] = "The network address does not coorespond to the netmask";
643 if ( !isset($errors) || count($errors) == 0 )
645 // now, try to add the network.
646 if( $method == 'static' )
648 if ( $node_obj->gateway != $gateway ) {
649 $optional_vals['gateway']= $gateway;
651 if ( $node_obj->network != $network ) {
652 $optional_vals['network']= $network;
654 if ( $node_obj->broadcast != $broadcast ) {
655 $optional_vals['broadcast']= $broadcast;
657 if ( $node_obj->netmask != $netmask ) {
658 $optional_vals['netmask']= $netmask;
660 if ( $node_obj->dns1 != $dns1 ) {
661 $optional_vals['dns1']= $dns1;
663 if ( $node_obj->dns2 != $dns2 ) {
664 $optional_vals['dns2']= $dns2;
668 if ( count($optional_vals) > 0 )
670 print_r($optional_vals);
671 $ret = $api->UpdateNodeNetwork( $node_obj->nodenetwork_id, $optional_vals);
673 $data['error'] = $api->error();
674 print $data['error'];
679 $data['node_id'] = $node_id;
680 if ( isset($errors) ) { $data['errors'] = $errors; }
685 function get_stage4_data($person, $data=NULL)
688 if ( $data == NULL ){
691 $data['node'] = $this->getnode($data['node_id']);
692 $data['site'] = $this->getsite($data['site_id']);
693 /*print "SITENAME: " . $data['site']['login_base'] . "<BR>";*/
697 function checknodeid($node_id)
700 $plc_node_list = $api->GetNodes(array('node_id' => intval($node_id) ), array('node_id'));
701 if ( count($plc_node_list) > 0 ) {
708 function getnode($node_id)
711 $plc_node_list = $api->GetNodes(array('node_id' => intval($node_id) ));
712 if ( count($plc_node_list) > 0 )
714 return new Node($plc_node_list[0]);