App-Standby

 view release on metacpan or  search on metacpan

lib/App/Standby/Frontend.pm  view on Meta::CPAN


    my $remote_addr = $request_ref->{'remote_addr'};
    # turn key => value pairs into smth. like key1=value1,key2=value2,...
    my $args = join(',', map { $_.'='.$request_ref->{$_} } keys %{$request_ref});

    $self->logger()->log( message => 'New Request from '.$remote_addr.'. Args: '.$args, level => 'debug', );

    return 1;
}

sub _verify_group_key {
    my $self = shift;
    my $request = shift;

    my $sql = 'SELECT COUNT(*) FROM groups WHERE id = ? AND key = ?';
    my $sth = $self->dbh()->prepare($sql);
    if(!$sth) {
        $self->logger()->log( message => 'Failed to prepare SQL '.$sql.' w/ error: '.$self->dbh()->errstr, level => 'error', );
        return;
    }
    if(!$sth->execute($request->{'group_id'},$request->{'group_key'})) {

lib/App/Standby/Frontend.pm  view on Meta::CPAN

        },
        \$body,
    );
    return [ 200, [ 'Content-Type', 'text/html'], [$body]];
}

sub _render_update_group {
    my $self = shift;
    my $request = shift;

    # verify group_key before any modification
    if(!$self->_verify_group_key($request)) {
        return [ 301, [ 'Location', '?rm=overview&group_id='.$request->{'group_id'}.'&msg=Invalid%20Key' ], [] ];
    }

    my $sql = 'UPDATE groups SET `name` = ?, `key` = ? WHERE id = ?';

    my $sth = $self->dbh()->prepare($sql);
    if(!$sth) {
        $self->logger()->log( message => 'Failed to prepare SQL '.$sql.' w/ error: '.$self->dbh()->errstr, level => 'error', );
        return [ 301, [ 'Location', '?rm=overview&group_id='.$request->{'group_id'} ], [] ];
    }

lib/App/Standby/Frontend.pm  view on Meta::CPAN

    );
    return [ 200, [ 'Content-Type', 'text/html'], [$body]];
}

sub _render_delete_group {
    my $self = shift;
    my $request = shift;

    return unless $request->{'group_id'};

    # verify group_key before any modification
    if(!$self->_verify_group_key($request)) {
        return [ 301, [ 'Location', '?rm=overview&group_id='.$request->{'group_id'}.'&msg=Invalid%20Key' ], [] ];
    }

    my $sql = 'DELETE FROM groups WHERE id = ?';
    my $sth = $self->dbh()->prepare($sql);
    if(!$sth) {
        $self->logger()->log( message => 'Failed to prepare SQL '.$sql.' w/ error: '.$self->dbh()->errstr, level => 'error', );
        return [ 301, [ 'Location', '?rm=overview&group_id='.$request->{'group_id'} ], [] ];
    }
    if(!$sth->execute($request->{'group_id'})) {

lib/App/Standby/Frontend.pm  view on Meta::CPAN

        },
        \$body,
    );
    return [ 200, [ 'Content-Type', 'text/html'], [$body]];
}

sub _render_insert_service {
    my $self = shift;
    my $request = shift;

    # verify group_key before any modification
    if(!$self->_verify_group_key($request)) {
        return [ 301, [ 'Location', '?rm=overview&group_id='.$request->{'group_id'}.'&msg=Invalid%20Key' ], [] ];
    }

    my $sql = 'INSERT INTO group_services (`group_id`,`name`,`desc`,`class`) VALUES(?,?,?,?)';
    my $sth = $self->dbh()->prepare($sql);
    $sth->execute($request->{'group_id'},$request->{'name'},$request->{'desc'},$request->{'class'});

    return [ 301, [ 'Location', '?rm=overview&group_id='.$request->{'group_id'} ], [] ];
}

lib/App/Standby/Frontend.pm  view on Meta::CPAN

        },
        \$body,
    );
    return [ 200, [ 'Content-Type', 'text/html'], [$body]];
}

sub _render_update_service {
    my $self = shift;
    my $request = shift;

    # verify group_key before any modification
    if(!$self->_verify_group_key($request)) {
        return [ 301, [ 'Location', '?rm=overview&group_id='.$request->{'group_id'}.'&msg=Invalid%20Key' ], [] ];
    }

    my $sql = 'UPDATE group_services SET `name` = ?, `desc` = ?, `class` = ? WHERE id = ?';

    my $sth = $self->dbh()->prepare($sql);
    if(!$sth) {
        $self->logger()->log( message => 'Failed to prepare SQL '.$sql.' w/ error: '.$self->dbh()->errstr, level => 'error', );
    }
    if(!$sth->execute($request->{'value'},$request->{'config_id'})) {

lib/App/Standby/Frontend.pm  view on Meta::CPAN

    );
    return [ 200, [ 'Content-Type', 'text/html'], [$body]];
}

sub _render_delete_service {
    my $self = shift;
    my $request = shift;

    return unless $request->{'gs_id'};

    # verify group_key before any modification
    if(!$self->_verify_group_key($request)) {
        return [ 301, [ 'Location', '?rm=overview&group_id='.$request->{'group_id'}.'&msg=Invalid%20Key' ], [] ];
    }

    my $sql = 'DELETE FROM group_services WHERE id = ?';
    my $sth = $self->dbh()->prepare($sql);
    if(!$sth) {
      $self->logger()->log( message => 'Failed to prepare SQL '.$sql.' w/ error: '.$self->dbh()->errstr, level => 'error', );
    }
    if(!$sth->execute($request->{'gs_id'})) {
      $self->logger()->log( message => 'Failed to execute stmt w/ error: '.$sth->errstr, level => 'error', );

lib/App/Standby/Frontend.pm  view on Meta::CPAN

        },
        \$body,
    );
    return [ 200, [ 'Content-Type', 'text/html'], [$body]];
}

sub _render_insert_config {
    my $self = shift;
    my $request = shift;

    # verify group_key before any modification
    if(!$self->_verify_group_key($request)) {
        return [ 301, [ 'Location', '?rm=overview&group_id='.$request->{'group_id'}.'&msg=Invalid%20Key' ], [] ];
    }

    my $sql = 'INSERT INTO config (`key`,`value`,`group_id`) VALUES (?,?,?)';
    my $sth = $self->dbh()->prepare($sql);
    $sth->execute($request->{'key'},$request->{'value'},$request->{'group_id'});

    return [ 301, [ 'Location', '?rm=overview&group_id='.$request->{'group_id'} ], [] ];
}

lib/App/Standby/Frontend.pm  view on Meta::CPAN

        },
        \$body,
    );
    return [ 200, [ 'Content-Type', 'text/html'], [$body]];
}

sub _render_update_config {
    my $self = shift;
    my $request = shift;

    # verify group_key before any modification
    if(!$self->_verify_group_key($request)) {
        return [ 301, [ 'Location', '?rm=overview&group_id='.$request->{'group_id'}.'&msg=Invalid%20Key' ], [] ];
    }

    my $sql = 'UPDATE config SET `value` = ? WHERE id = ?';

    my $sth = $self->dbh()->prepare($sql);
    if(!$sth) {
        #print "Prepare failed: ".$self->dbh()->errstr()."\n";
    }
    if(!$sth->execute($request->{'value'},$request->{'config_id'})) {

lib/App/Standby/Frontend.pm  view on Meta::CPAN

    );
    return [ 200, [ 'Content-Type', 'text/html'], [$body]];
}

sub _render_delete_config {
    my $self = shift;
    my $request = shift;

    return unless $request->{'config_id'};

    # verify group_key before any modification
    if(!$self->_verify_group_key($request)) {
        return [ 301, [ 'Location', '?rm=overview&group_id='.$request->{'group_id'}.'&msg=Invalid%20Key' ], [] ];
    }

    my $sql = 'DELETE FROM config WHERE id = ?';
    my $sth = $self->dbh()->prepare($sql);
    $sth->execute($request->{'config_id'});
    $sth->finish();

    return [ 301, [ 'Location', '?rm=overview&group_id='.$request->{'group_id'} ], [] ];
}

lib/App/Standby/Frontend.pm  view on Meta::CPAN

        },
        \$body,
    );
    return [ 200, [ 'Content-Type', 'text/html'], [$body]];
}

sub _render_insert_config_contacts {
    my $self = shift;
    my $request = shift;

    # verify group_key before any modification
    if(!$self->_verify_group_key($request)) {
        return [ 301, [ 'Location', '?rm=list_config_contacts&contact_id='.$request->{'contact_id'}.'&msg=Invalid%20Key' ], [] ];
    }

    my $sql = 'INSERT INTO config_contacts (`key`,`value`,`contact_id`) VALUES (?,?,?)';
    my $sth = $self->dbh()->prepare($sql);
    $sth->execute($request->{'key'},$request->{'value'},$request->{'contact_id'});

    return [ 301, [ 'Location', '?rm=list_config_contacts&contact_id='.$request->{'contact_id'} ], [] ];
}

lib/App/Standby/Frontend.pm  view on Meta::CPAN

        },
        \$body,
    );
    return [ 200, [ 'Content-Type', 'text/html'], [$body]];
}

sub _render_update_config_contacts {
    my $self = shift;
    my $request = shift;

    # verify group_key before any modification
    if(!$self->_verify_group_key($request)) {
        return [ 301, [ 'Location', '?rm=list_config_contacts&contact_id='.$request->{'contact_id'}.'&msg=Invalid%20Key' ], [] ];
    }

    my $sql = 'UPDATE config_contacts SET `value` = ? WHERE id = ?';

    my $sth = $self->dbh()->prepare($sql);
    if(!$sth) {
        #print "Prepare failed: ".$self->dbh()->errstr()."\n";
    }
    if(!$sth->execute($request->{'value'},$request->{'cconfig_id'})) {

lib/App/Standby/Frontend.pm  view on Meta::CPAN


    return [ 200, [ 'Content-Type', 'text/html'], [$body]];
}

sub _render_delete_config_contacts {
    my $self = shift;
    my $request = shift;

    return [ 301, [ 'Location', '?rm=list_config_contacts&contact_id='.$request->{'contact_id'}.'&msg=Missing%20Args' ], [] ] unless $request->{'cconfig_id'};

    # verify group_key before any modification
    if(!$self->_verify_group_key($request)) {
        return [ 301, [ 'Location', '?rm=list_config_contacts&contact_id='.$request->{'contact_id'}.'&msg=Invalid%20Key' ], [] ];
    }

    my $sql = 'DELETE FROM config_contacts WHERE id = ?';
    my $sth = $self->dbh()->prepare($sql);
    $sth->execute($request->{'cconfig_id'});
    $sth->finish();

    return [ 301, [ 'Location', '?rm=list_config_contacts&contact_id='.$request->{'contact_id'} ], [] ];
}

lib/App/Standby/Frontend.pm  view on Meta::CPAN

        },
        \$body,
    );
    return [ 200, [ 'Content-Type', 'text/html'], [$body]];
}

sub _render_insert_contact {
    my $self = shift;
    my $request = shift;

    # verify group_key before any modification
    if(!$self->_verify_group_key($request)) {
        return [ 301, [ 'Location', '?rm=overview&group_id='.$request->{'group_id'}.'&msg=Invalid%20Key' ], [] ];
    }

    my $sql = 'INSERT INTO contacts (`name`,`cellphone`, `group_id`,`is_enabled`,`ordinal`) VALUES (?,?,?,0,0)';
    my $sth = $self->dbh()->prepare($sql);
    $sth->execute($request->{'name'},$request->{'cellphone'},$request->{'group_id'});
    my $contact_id = $self->dbh()->last_insert_id(undef, undef, undef, undef);

    return [ 301, [ 'Location', '?rm=overview&group_id='.$request->{'group_id'} ], [] ];
}

lib/App/Standby/Frontend.pm  view on Meta::CPAN

        },
        \$body,
    );
    return [ 200, [ 'Content-Type', 'text/html'], [$body]];
}

sub _render_update_contact {
    my $self = shift;
    my $request = shift;

    # verify group_key before any modification
    if(!$self->_verify_group_key($request)) {
        return [ 301, [ 'Location', '?rm=overview&group_id='.$request->{'group_id'}.'&msg=Invalid%20Key' ], [] ];
    }

    my $sql = 'UPDATE contacts SET ';
    my @args = ();

    foreach my $key (qw(name cellphone group_id)) {
        if(defined($request->{$key})) {
            $sql .= '`'.$key.'` = ?, ';
            push(@args,$request->{$key});

lib/App/Standby/Frontend.pm  view on Meta::CPAN

        },
        \$body,
    );
    return [ 200, [ 'Content-Type', 'text/html'], [$body]];
}

sub _render_delete_contact {
    my $self = shift;
    my $request = shift;

    # verify group_key before any modification
    if(!$self->_verify_group_key($request)) {
        return [ 301, [ 'Location', '?rm=overview&group_id='.$request->{'group_id'}.'&msg=Invalid%20Key' ], [] ];
    }

    return unless $request->{'contact_id'};

    my $sql = 'DELETE FROM contacts WHERE id = ?';
    my $sth = $self->dbh()->prepare($sql);
    if(!$sth) {
        $self->logger()->log( message => 'Failed to prepare statement from SQL '.$sql.' w/ error: '.$self->dbh()->errstr(), level => 'error', );
    }

lib/App/Standby/Frontend.pm  view on Meta::CPAN

    }
    $sth->finish();

    return [ 301, [ 'Location', '?rm=overview&group_id='.$request->{'group_id'} ], [] ];
}

sub _render_update_janitor {
    my $self = shift;
    my $request = shift;

    # verify group_key before any modification
    if(!$self->_verify_group_key($request)) {
        return [ 301, [ 'Location', '?rm=overview&group_id='.$request->{'group_id'}.'&msg=Invalid%20Key' ], [] ];
    }

    my $error = '';
    my $Group = $self->groups()->{$request->{'group_id'}};
    if($Group) {
        try {
            my $users = $Group->set_janitor($request->{'janitor'});
            if(scalar(@$users)) {
                $self->logger()->log( message => "Updated janitor", level => 'debug', );

lib/App/Standby/Frontend.pm  view on Meta::CPAN

        },
        \$body,
    );
    return [ 200, [ 'Content-Type', 'text/html'], [$body]];
}

sub _render_enable_contact {
    my $self = shift;
    my $request = shift;

    # verify group_key before any modification
    if(!$self->_verify_group_key($request)) {
        return [ 301, [ 'Location', '?rm=overview&group_id='.$request->{'group_id'}.'&msg=Invalid%20Key' ], [] ];
    }

    my $Group = $self->groups()->{$request->{'group_id'}};
    if($Group) {
        try {
            $Group->enable_contact($request->{'contact_id'});
        } catch {
            $self->logger()->log( message => "Failed to enable user: ".$_, level => 'error', );
        };

lib/App/Standby/Frontend.pm  view on Meta::CPAN

        },
        \$body,
    );
    return [ 200, [ 'Content-Type', 'text/html'], [$body]];
}

sub _render_disable_contact {
    my $self = shift;
    my $request = shift;

    # verify group_key before any modification
    if(!$self->_verify_group_key($request)) {
        return [ 301, [ 'Location', '?rm=overview&group_id='.$request->{'group_id'}.'&msg=Invalid%20Key' ], [] ];
    }

    my $Group = $self->groups()->{$request->{'group_id'}};
    if($Group) {
        try {
            $Group->disable_contact($request->{'contact_id'});
        } catch {
            $self->logger()->log( message => "Failed to disable user: ".$_, level => 'error', );
        };



( run in 0.356 second using v1.01-cache-2.11-cpan-73692580452 )