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['hostname'] = "trim|required";
40 $rules['ip'] = "trim|required|valid_ip";
41 $rules['username'] = "trim";
42 $rules['password'] = "trim|required";
43 $rules['notes'] = "trim";
44 $this->validation->set_rules($rules);
46 $fields['hostname'] = "Hostname";
47 $fields['ip'] = "IP Address";
48 $fields['username'] = "Username";
49 $fields['password'] = "Password";
50 $this->validation->set_fields($fields);
52 $person = new Person($plc->person);
54 if ($this->validation->run() == TRUE)
56 /* b/c the submit is valid, it doesn't matter if pcu_register is set */
57 $this->pcu_id = $this->add_pcu($data);
59 $data = $this->get_stage1_data($person, $data);
61 $this->load->view('header', $data);
62 $this->load->view('debug', $data);
63 $this->load->view('stage1_pcu_choose', $data);
64 $this->load->view('footer', $data);
67 function add_pcu(&$data)
70 /* add pcu, get pcu info */
71 $site_id = intval( $_REQUEST['site_id'] );
72 $fields= array( 'protocol'=> '',
73 'model'=> $_REQUEST['model'],
74 'hostname'=> $this->validation->hostname,
75 'ip'=> $this->validation->ip,
76 'username'=> $this->validation->username,
77 'password'=> $this->validation->password,
78 'notes'=> $_REQUEST['notes'], );
79 $pcu_id= $api->AddPCU( $site_id, $fields );
82 $data['error'] = $api->error();
85 $data['pcu_id'] = $pcu_id;
90 function get_stage1_data($person, $data=NULL)
96 $data['default_site_list'] = $person->getSites();
97 $data['pcu_types'] = $api->GetPCUTypes(NULL, array('model', 'name'));
98 $data['pcu_list'] = $this->getpculist($person);
99 $data['site_list'] = $this->getsitelist($person);
103 function getpculist($person)
106 $plc_pcu_list = $api->GetPCUs(array('site_id' => $person->getSites()));
107 return PlcObject::constructList('PCU', $plc_pcu_list);
110 function getsitelist($person)
113 // get sites depending on role and sites associated.
114 if( $person->isAdmin() ) {
115 $site_info= $api->GetSites( NULL, array( "name", "site_id", "login_base" ) );
117 $site_info= $api->GetSites( $person->getSites(), array( "name", "site_id", "login_base" ) );
119 sort_sites( $site_info );
123 var $run_update = True;
124 var $disp_errors = True;
125 function stage2_confirmpcu()
128 $this->load->helper(array('form', 'url'));
129 $this->load->library('validation');
131 $this->validation->set_error_delimiters('<span class="error">', '</span>');
132 $person = new Person($plc->person);
134 if ( isset($_REQUEST['pcu_choose']) ) {
135 $rules['pcu_id'] = "required";
136 $this->validation->set_rules($rules);
137 $fields['pcu_id'] = "PCU id";
138 $this->validation->set_fields($fields);
140 $result = $this->validation->run();
141 /* I don't know, shouldn't we redirect to the first stage in this case? */
142 $this->run_update = False;
143 $this->disp_errors = False;
144 print $this->validation->error_string . "<br>";
149 $rules['pcu_id'] = "required";
150 $rules['hostname'] = "trim|required";
151 $rules['ip'] = "trim|required|valid_ip";
152 $rules['username'] = "trim";
153 $rules['password'] = "trim|required";
154 $rules['notes'] = "trim";
155 $this->validation->set_rules($rules);
157 $fields['pcu_id'] = "PCU id";
158 $fields['hostname'] = "Hostname";
159 $fields['ip'] = "IP Address";
160 $fields['username'] = "Username";
161 $fields['password'] = "Password";
162 $this->validation->set_fields($fields);
164 if ( $this->validation->run() == FALSE )
166 print $this->validation->error_string . "<br>";
167 $this->run_update = False;
170 $data = $this->get_stage2_data($person);
172 $this->load->view('header', $data);
173 $this->load->view('debug', $data);
174 $this->load->view('stage2_pcu_confirm', $data);
175 $this->load->view('footer', $data);
179 function update_pcu($data)
182 /* add pcu, get pcu info */
183 $pcu_id = intval($this->validation->pcu_id);
184 $fields= array( 'protocol'=> '',
185 'model'=> $_REQUEST['model'],
186 'hostname'=> $this->validation->hostname,
187 'ip'=> $this->validation->ip,
188 'username'=> $this->validation->username,
189 'password'=> $this->validation->password,
190 'notes'=> $_REQUEST['notes'], );
191 $ret = $api->UpdatePCU( $pcu_id, $fields );
194 $data['error'] = $api->error();
195 print $data['error'];
201 function get_stage2_data($person)
206 if ( $this->run_update && isset($_REQUEST['pcu_update']) )
208 $this->update_pcu($data);
211 if ( isset($_REQUEST['pcu_id']) )
213 $pcu_id = intval($_REQUEST['pcu_id']);
214 $pcu_data = $api->GetPCUs(array('pcu_id'=>$pcu_id));
215 $pcu = new PCU($pcu_data[0]);
217 $data['pcu_id'] = $pcu_id;
218 $data['site_id'] = $pcu_data[0]['site_id'];
221 $data['default_site_list'] = $person->getSites();
222 $data['pcu_types'] = $api->GetPCUTypes(NULL, array('model', 'name'));
223 $data['pcu_site'] = $api->GetSites( $pcu_data[0]['site_id'], array( "name", "site_id", "login_base" ) );
228 function stage3_addnode($pcu_id, $site_id)
231 $this->load->helper(array('form', 'url'));
232 $this->load->library('validation');
234 $this->validation->set_error_delimiters('<span class="error">', '</span>');
235 $person = new Person($plc->person);
237 $data['pcu_id'] = intval($pcu_id);
238 $data['site_id'] = intval($site_id);
240 if ( isset($_REQUEST['pcu_proceed']) ) {
241 $rules['hostname'] = "";
242 $rules['model'] = "";
243 $rules['method'] = "";
245 $rules['netmask'] = "";
246 $rules['network'] = "";
247 $rules['gateway'] = "";
248 $rules['broadcast'] = "";
251 $this->validation->set_rules($rules);
252 $fields['hostname'] = "Hostname";
253 $fields['model'] = "Model";
254 $fields['method'] = "Method";
255 $fields['ip'] = "IP Address";
256 $fields['netmask'] = "Netmask Address";
257 $fields['network'] = "Network Address";
258 $fields['gateway'] = "Gateway Address";
259 $fields['broadcast'] = "Broadcast Address";
260 $fields['dns1'] = "Primary DNS Address";
261 $fields['dns2'] = "Secondary DNS Address";
262 $this->validation->set_fields($fields);
264 $result = $this->validation->run();
265 /*print "RESULT: ".$result . "<br>";*/
266 # TODO: if result is false, redirect to beginning.
267 $this->disp_errors = False;
268 print $this->validation->error_string . "<br>";
270 $rules['hostname'] = "trim|required";
271 $rules['model'] = "trim|required";
272 $rules['method'] = "required";
273 $rules['ip'] = "trim|required|valid_ip";
274 if ( isset ($_REQUEST['method']) && $_REQUEST['method'] == 'static' )
276 $rules['netmask'] = "trim|valid_ip";
277 $rules['network'] = "trim|valid_ip";
278 $rules['gateway'] = "trim|valid_ip";
279 $rules['broadcast'] = "trim|valid_ip";
280 $rules['dns1'] = "trim|valid_ip";
281 $rules['dns2'] = "trim|valid_ip";
283 $rules['netmask'] = "";
284 $rules['network'] = "";
285 $rules['gateway'] = "";
286 $rules['broadcast'] = "";
290 $this->validation->set_rules($rules);
292 $fields['hostname'] = "Hostname";
293 $fields['model'] = "Model";
294 $fields['method'] = "Method";
295 $fields['ip'] = "IP Address";
296 $fields['netmask'] = "Netmask Address";
297 $fields['network'] = "Network Address";
298 $fields['gateway'] = "Gateway Address";
299 $fields['broadcast'] = "Broadcast Address";
300 $fields['dns1'] = "Primary DNS Address";
301 $fields['dns2'] = "Secondary DNS Address";
302 $this->validation->set_fields($fields);
304 if ($this->validation->run() == TRUE)
306 /* b/c the submit is valid, all values are minimally consistent. */
307 $this->node_id = $this->add_node($data);
310 $data = $this->get_stage3_data($person, $data);
312 $this->load->view('header', $data);
313 $this->load->view('debug', $data);
314 $this->load->view('stage3_node_choose', $data);
315 $this->load->view('footer', $data);
318 function add_node(&$data)
321 print "Adding Node\n<br>";
322 $hostname = trim($_REQUEST['hostname']);
323 $model= trim($_REQUEST['model']);
324 $method = trim($_REQUEST['method']);
325 $ip = trim($_REQUEST['ip']);
326 if ( $method == 'static' )
328 $netmask = trim($_REQUEST['netmask']);
329 $network = trim($_REQUEST['network']);
330 $gateway = trim($_REQUEST['gateway']);
331 $broadcast = trim($_REQUEST['broadcast']);
332 $dns1 = trim($_REQUEST['dns1']);
333 $dns2 = trim($_REQUEST['dns2']);
336 // used to generate error strings for static fields only
337 $static_fields= array();
338 $static_fields['netmask']= "Netmask address";
339 $static_fields['network']= "Network address";
340 $static_fields['gateway']= "Gateway address";
341 $static_fields['broadcast']= "Broadcast address";
342 $static_fields['dns1']= "Primary DNS address";
344 if( $method == 'static' )
346 if( !is_valid_network_addr($network,$netmask) )
348 $errors[] = "The network address does not coorespond to the netmask";
352 if( !isset($errors) || count($errors) == 0 )
354 // add new node and its network
355 $optional_vals= array( "hostname"=>$hostname, "model"=>$model );
357 $site_id= $data['site_id'];
358 $node_id= $api->AddNode( intval( $site_id ), $optional_vals );
359 if( $node_id <= 0 ) {
360 $data['error'] = $api->error();
361 print $data['error'];
364 // now, try to add the network.
365 $optional_vals= array();
366 $optional_vals['is_primary']= true;
367 $optional_vals['ip']= $ip;
368 $optional_vals['type']= 'ipv4';
369 $optional_vals['method']= $method;
371 if( $method == 'static' )
373 $optional_vals['gateway']= $gateway;
374 $optional_vals['network']= $network;
375 $optional_vals['broadcast']= $broadcast;
376 $optional_vals['netmask']= $netmask;
377 $optional_vals['dns1']= $dns1;
379 $optional_vals['dns2']= $dns2;
383 $nodenetwork_id= $api->AddNodeNetwork( $node_id, $optional_vals);
384 if( $nodenetwork_id <= 0 ) {
385 $data['error'] = $api->error();
386 print $data['error'];
389 $success = $api->AddNodeToPCU( $node_id, $data['pcu_id'], 1);
390 if( !isset($success) || $success <= 0 ) {
391 $data['error'] = $api->error();
392 print $data['error'];
397 $data['node_id'] = $node_id;
398 if ( isset($errors) ) { $data['errors'] = $errors; }
403 function get_stage3_data($person, $data=NULL)
406 if ( $data == NULL ){
409 $data['default_site_list'] = array($data['site_id']);
410 $data['node_list'] = $this->getnodelist($data['site_id']);
411 $data['site'] = $this->getsite($data['site_id']);
415 function getnodelist($site_id)
418 $plc_node_list = $api->GetNodes(array('site_id' => intval($site_id) ));
419 return PlcObject::constructList('Node', $plc_node_list);
422 function getsite($site_id)
425 $site_info = $api->GetSites($site_id, array( "name", "site_id", "login_base" ) );
426 return $site_info[0];
429 function stage45_mappcu($pcu_id=0, $site_id=0, $node_id=0)
432 $this->load->helper(array('form', 'url'));
433 $this->load->library('validation');
435 $this->validation->set_error_delimiters('<span class="error">', '</span>');
437 $fields['port_number'] = "Port Number";
438 $this->validation->set_fields($fields);
441 $data['pcu_id'] = intval($pcu_id);
442 $data['site_id'] = intval($site_id);
443 $data['node_id'] = intval($node_id);
445 if ( isset($_REQUEST['node_confirm']) ) {
446 /* skip the rules, since we're just displaying the page. */
447 $rules['port_number'] = "";
448 $this->validation->set_rules($rules);
451 /* setup rules, to validate the form data. */
452 $rules['port_number'] = "trim|required|intval";
453 $this->validation->set_rules($rules);
455 $result = $this->validation->run();
459 $port = $this->validation->port_number;
460 /* if we do not delete the node from the PCU first, a fault is raised */
461 $ret = $api->DeleteNodeFromPCU($data['node_id'], $data['pcu_id']);
462 $ret = $api->AddNodeToPCU($data['node_id'], $data['pcu_id'], $port);
465 $data['error'] = $api->error();
466 print $data['error'];
469 $this->disp_errors = False;
470 print $this->validation->error_string . "<br>";
474 $data['node'] = $this->getnode($data['node_id']);
475 $api_pcus = $api->GetPCUs($data['node']->pcu_ids);
478 # NOTE: find index of node id, then pull out that index of
479 $index = array_search($data['node_id'], $pcu['node_ids']);
480 $data['pcu_port'] = $pcu['ports'][$index];
481 $data['stage'] = 4.5;
482 #$data = $this->get_stage4_data($person, $data);
483 $this->load->view('header', $data);
484 $this->load->view('debug', $data);
485 $this->load->view('stage45_pcuport', $data);
486 $this->load->view('footer', $data);
489 function stage4_confirmnode($pcu_id=0, $site_id=0)
492 $this->load->helper(array('form', 'url'));
493 $this->load->library('validation');
495 $this->validation->set_error_delimiters('<span class="error">', '</span>');
496 $person = new Person($plc->person);
498 $fields['hostname'] = "Hostname";
499 $fields['model'] = "Model";
500 $fields['method'] = "Method";
501 $fields['ip'] = "IP Address";
502 $fields['netmask'] = "Netmask Address";
503 $fields['network'] = "Network Address";
504 $fields['gateway'] = "Gateway Address";
505 $fields['broadcast']= "Broadcast Address";
506 $fields['dns1'] = "Primary DNS Address";
507 $fields['dns2'] = "Secondary DNS Address";
508 $fields['node_id'] = "NODE id";
509 $this->validation->set_fields($fields);
512 $data['pcu_id'] = intval($pcu_id);
513 $data['site_id'] = intval($site_id);
515 if ( isset($_REQUEST['node_choose']) ) {
516 $rules['node_id'] = "required|intval";
517 $rules['hostname'] = "";
518 $rules['model'] = "";
519 $rules['method'] = "";
521 $rules['netmask'] = "";
522 $rules['network'] = "";
523 $rules['gateway'] = "";
524 $rules['broadcast'] = "";
527 $this->validation->set_rules($rules);
529 $result = $this->validation->run();
530 /*print "RESULT: ".$result . "<br>";*/
531 # TODO: if result is false, redirect to beginning.
532 $this->disp_errors = False;
533 print $this->validation->error_string . "<br>";
535 $rules['hostname'] = "trim|required";
536 $rules['model'] = "trim|required";
537 $rules['method'] = "required";
538 $rules['ip'] = "trim|required|valid_ip";
539 if ( isset ($_REQUEST['method']) && $_REQUEST['method'] == 'static' )
541 $rules['netmask'] = "trim|valid_ip";
542 $rules['network'] = "trim|valid_ip";
543 $rules['gateway'] = "trim|valid_ip";
544 $rules['broadcast'] = "trim|valid_ip";
545 $rules['dns1'] = "trim|valid_ip";
546 $rules['dns2'] = "trim";
548 # NOTE: There are no conditions that must be met for these fields.
549 $rules['netmask'] = "";
550 $rules['network'] = "";
551 $rules['gateway'] = "";
552 $rules['broadcast'] = "";
556 $rules['node_id'] = "required|intval";
557 $this->validation->set_rules($rules);
559 if ($this->validation->run() == TRUE)
561 /* b/c the submit is valid, all values are minimally consistent. */
562 $this->node_id = $this->update_node($data);
565 $data['node_id'] = intval($this->validation->node_id);
567 if ( $this->checknodeid($data['node_id']) )
569 $data = $this->get_stage4_data($person, $data);
570 $this->load->view('header', $data);
571 $this->load->view('debug', $data);
572 $this->load->view('stage4_node_confirm', $data);
573 $this->load->view('footer', $data);
575 print "You must select a valid Node before continuing.";
579 function update_node(&$data)
581 # TODO: RECODE To update values instead of adding them...
583 $hostname = trim($_REQUEST['hostname']);
584 $model= trim($_REQUEST['model']);
585 $node_id = intval($this->validation->node_id);
586 $ret = $api->UpdateNode( $node_id, array('hostname' => $hostname,
589 $data['error'] = $api->error();
590 print $data['error'];
593 $api_node_list = $api->GetNodes($node_id);
594 if ( count($api_node_list) > 0 )
596 $node_obj = new Node($api_node_list[0]);
603 $optional_vals= array();
605 $method = trim($_REQUEST['method']);
606 if ( $node_obj->method != $method ) {
607 $optional_vals['method']= $method;
610 $ip = trim($_REQUEST['ip']);
611 if ( $node_obj->ip != $ip ) {
612 $optional_vals['ip']= $ip;
615 // used to generate error strings for static fields only
616 $static_fields= array();
617 $static_fields['netmask']= "Netmask address";
618 $static_fields['network']= "Network address";
619 $static_fields['gateway']= "Gateway address";
620 $static_fields['broadcast']= "Broadcast address";
621 $static_fields['dns1']= "Primary DNS address";
623 if ( $method == 'static' )
625 $netmask = trim($_REQUEST['netmask']);
626 $network = trim($_REQUEST['network']);
627 $gateway = trim($_REQUEST['gateway']);
628 $broadcast = trim($_REQUEST['broadcast']);
629 $dns1 = trim($_REQUEST['dns1']);
630 $dns2 = trim($_REQUEST['dns2']);
632 if( !is_valid_network_addr($network,$netmask) )
634 $errors[] = "The network address does not coorespond to the netmask";
638 if ( !isset($errors) || count($errors) == 0 )
640 // now, try to add the network.
641 if( $method == 'static' )
643 if ( $node_obj->gateway != $gateway ) {
644 $optional_vals['gateway']= $gateway;
646 if ( $node_obj->network != $network ) {
647 $optional_vals['network']= $network;
649 if ( $node_obj->broadcast != $broadcast ) {
650 $optional_vals['broadcast']= $broadcast;
652 if ( $node_obj->netmask != $netmask ) {
653 $optional_vals['netmask']= $netmask;
655 if ( $node_obj->dns1 != $dns1 ) {
656 $optional_vals['dns1']= $dns1;
658 if ( $node_obj->dns2 != $dns2 ) {
659 $optional_vals['dns2']= $dns2;
663 if ( count($optional_vals) > 0 )
665 print_r($optional_vals);
666 $ret = $api->UpdateNodeNetwork( $node_obj->nodenetwork_id, $optional_vals);
668 $data['error'] = $api->error();
669 print $data['error'];
674 $data['node_id'] = $node_id;
675 if ( isset($errors) ) { $data['errors'] = $errors; }
680 function get_stage4_data($person, $data=NULL)
683 if ( $data == NULL ){
686 $data['node'] = $this->getnode($data['node_id']);
687 $data['site'] = $this->getsite($data['site_id']);
688 /*print "SITENAME: " . $data['site']['login_base'] . "<BR>";*/
692 function checknodeid($node_id)
695 $plc_node_list = $api->GetNodes(array('node_id' => intval($node_id) ), array('node_id'));
696 if ( count($plc_node_list) > 0 ) {
703 function getnode($node_id)
706 $plc_node_list = $api->GetNodes(array('node_id' => intval($node_id) ));
707 if ( count($plc_node_list) > 0 )
709 return new Node($plc_node_list[0]);