package Netman; use strict; use warnings; use Apache2::RequestRec; use Apache2::Const; use CGI ':cgi-lib'; use CGI::Ajax; use Template; use Data::Dumper; use Netman::Const; use Netman::Search; use Netman::DBI; use Netman::Permissions; use Netman::User; use Netman::MAC; use Netman::Switch; use Netman::Room; use Netman::IP; use Netman::Connection; use Netman::AccessPoint; use Netman::Rack; use Netman::Computer; use Netman::PatchPanel; use Netman::Quarantine; use Netman::LDAP; use Netman::NAC; use Netman::SNMP; use Netman::Network; use Netman::Lease; use Netman::VLAN; use Netman::Bootservice; use Netman::DNS; use Netman::EventCode; use Netman::Trace; # Create a hashref to pass important stuff around in my $base_vars = {webroot => Netman::Const::WEBROOT, fswd => Netman::Const::FSWD, search_groups => $Netman::Const::search_groups, title => 'Network Management: '}; # Create a template object my $tt = Template->new({INCLUDE_PATH => $base_vars->{fswd} . 'templates/'}); #================================================================================================== # The function that mod_perl calls when a page is requested sub handler { my ($r) = @_; # Set the content type $r->content_type('text/html'); my $vars = {}; %$vars = %$base_vars; # Get permissions $vars->{dns_edit} = Netman::Permissions::check_named('dns_edit'); $vars->{eventcode_edit} = Netman::Permissions::check_named('eventcode_edit'); # Parse the uri my $uri_raw = $r->uri(); $uri_raw =~ s/$vars->{webroot}\/*//; $uri_raw =~ s/\/$//; my @uri = split(/\//, $uri_raw); # Check to see if any params were provided if (scalar(@uri) > 0) { # Check to see if the module in question exists if (exists($Netman::Const::modules->{$uri[0]})) { # Populate the vars hash $vars->{q} = CGI->new(); $vars->{ajax} = new CGI::Ajax('get_types' => \&perl_func, 'skip_header' => 1,); $vars->{ajax}->skip_header(1); $vars->{uri} = \@uri; $vars->{params} = Vars(); # Call the module handler function my @return = &{$Netman::Const::modules->{$uri[0]}}($vars); # If the return code was for a template if ($return[0] == Netman::Const::RETURN_TEMPLATE) { # If this is the cable creation page, process the template using Ajax if ($return[1] =~ m/cable\/create.tt/) { print "Hello Ajax"; print $vars->{ajax}->build_html($vars->{q}, sub { &process_ajax(@return) }); } else { # Process the template print "Hello NOT Ajax"; $tt->process($return[1], $return[2]) || print $tt->error(); } # Send back OK to apache return Apache2::Const::OK; } # If the return code was for a redirect elsif ($return[0] == Netman::Const::RETURN_REDIRECT) { print $vars->{q}->redirect($vars->{webroot} . $return[1]); # Send back OK to apache return Apache2::Const::OK; } # If the return code was for not found elsif ($return[0] == Netman::Const::RETURN_NOT_FOUND) { return Apache2::Const::NOT_FOUND; } # If the return code was for a forbiddon elsif ($return[0] == Netman::Const::RETURN_FORBIDDEN) { return Apache2::Const::FORBIDDEN; } # Otherwise something went wrong else { return Apache2::Const::SERVER_ERROR; } } # Otherwise return not found else { return Apache2::Const::NOT_FOUND; } } # Otherwise return the homepage else { # Populate the vars hash $vars->{q} = CGI->new(); # See if we should show the extra homepage links $vars->{show_links} = Netman::Permissions::permissions() & 1; # Process the template $tt->process('index.tt', $vars) || print $tt->error(); # Send back OK to apache return Apache2::Const::OK; } } # A special subroutine to process the template and return the html to CGI::Ajax sub process_ajax { my (@return) = @_; my $output = ''; $tt->process($return[1], $return[2], \$output) || print $tt->error(); return $output; } sub perl_func { my $input = shift; print Dumper($input); return $input; } 1;