2024-03-22 04:54:28 +01:00
|
|
|
package SrvMngr::Controller::Printers;
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
# heading : System
|
|
|
|
# description : Printers
|
|
|
|
# navigation : 4000 800
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# routes : end
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
|
|
|
use Locale::gettext;
|
|
|
|
use SrvMngr::I18N;
|
|
|
|
use SrvMngr qw(theme_list init_session);
|
|
|
|
use esmith::FormMagick::Panel::printers;
|
|
|
|
our $adb = esmith::AccountsDB->open || die "Couldn't open accounts db";
|
|
|
|
|
|
|
|
sub main {
|
|
|
|
my $c = shift;
|
|
|
|
$c->app->log->info($c->log_req);
|
|
|
|
my %prt_datas = ();
|
2025-01-14 13:49:31 +01:00
|
|
|
my $title = $c->l('prt_FORM_TITLE');
|
2024-03-22 04:54:28 +01:00
|
|
|
$prt_datas{'trt'} = 'LIST';
|
|
|
|
my @printerDrivers;
|
2025-01-14 13:49:31 +01:00
|
|
|
|
|
|
|
if ($adb) {
|
2024-03-22 04:54:28 +01:00
|
|
|
@printerDrivers = $adb->printers();
|
|
|
|
}
|
2025-01-14 13:49:31 +01:00
|
|
|
$c->stash(title => $title, prt_datas => \%prt_datas, printerDrivers => \@printerDrivers);
|
2024-03-22 04:54:28 +01:00
|
|
|
$c->render(template => 'printers');
|
2025-01-14 13:49:31 +01:00
|
|
|
} ## end sub main
|
2024-03-22 04:54:28 +01:00
|
|
|
|
|
|
|
sub do_display {
|
2025-01-14 13:49:31 +01:00
|
|
|
my $c = shift;
|
|
|
|
my $rt = $c->current_route;
|
|
|
|
my $trt = ($c->param('trt') || 'LIST');
|
2024-03-22 04:54:28 +01:00
|
|
|
my $printer = $c->param('printer') || '';
|
|
|
|
|
|
|
|
#$trt = 'DEL' if ( $printer );
|
|
|
|
#$trt = 'ADD' if ( $rt eq 'printeradd' );
|
|
|
|
my %prt_datas = ();
|
2025-01-14 13:49:31 +01:00
|
|
|
my $title = $c->l('prt_FORM_TITLE');
|
2024-03-22 04:54:28 +01:00
|
|
|
$prt_datas{'trt'} = $trt;
|
|
|
|
|
2025-01-14 13:49:31 +01:00
|
|
|
if ($trt eq 'ADD') {
|
2024-03-22 04:54:28 +01:00
|
|
|
|
2025-01-14 13:49:31 +01:00
|
|
|
# nothing
|
|
|
|
}
|
2024-03-22 04:54:28 +01:00
|
|
|
|
2025-01-14 13:49:31 +01:00
|
|
|
if ($trt eq 'DEL') {
|
|
|
|
my $rec = $adb->get($printer);
|
2024-03-22 04:54:28 +01:00
|
|
|
|
2025-01-14 13:49:31 +01:00
|
|
|
if ($rec and $rec->prop('type') eq 'printer') {
|
|
|
|
$prt_datas{printer} = $printer;
|
|
|
|
$prt_datas{description} = $rec->prop('Description') || '';
|
2024-03-22 04:54:28 +01:00
|
|
|
}
|
2025-01-14 13:49:31 +01:00
|
|
|
} ## end if ($trt eq 'DEL')
|
2024-03-22 04:54:28 +01:00
|
|
|
|
2025-01-14 13:49:31 +01:00
|
|
|
if ($trt eq 'LIST') {
|
|
|
|
my @printerDrivers;
|
2024-03-22 04:54:28 +01:00
|
|
|
|
2025-01-14 13:49:31 +01:00
|
|
|
if ($adb) {
|
|
|
|
@printerDrivers = $adb->printers();
|
|
|
|
}
|
|
|
|
$c->stash(printerDrivers => \@printerDrivers);
|
|
|
|
} ## end if ($trt eq 'LIST')
|
|
|
|
$c->stash(title => $title, prt_datas => \%prt_datas);
|
|
|
|
$c->render(template => 'printers');
|
|
|
|
} ## end sub do_display
|
2024-03-22 04:54:28 +01:00
|
|
|
|
|
|
|
sub do_update {
|
|
|
|
my $c = shift;
|
|
|
|
$c->app->log->info($c->log_req);
|
2025-01-14 13:49:31 +01:00
|
|
|
my $rt = $c->current_route;
|
|
|
|
my $trt = ($c->param('trt') || 'LIST');
|
2024-03-22 04:54:28 +01:00
|
|
|
my %prt_datas = ();
|
2025-01-14 13:49:31 +01:00
|
|
|
my $title = $c->l('prt_FORM_TITLE');
|
2024-03-22 04:54:28 +01:00
|
|
|
$prt_datas{'trt'} = $trt;
|
|
|
|
my ($res, $result) = '';
|
|
|
|
|
2025-01-14 13:49:31 +01:00
|
|
|
if ($trt eq 'ADD') {
|
|
|
|
my $name = ($c->param('Name') || '');
|
2024-03-22 04:54:28 +01:00
|
|
|
my $description = ($c->param('Description') || '');
|
2025-01-14 13:49:31 +01:00
|
|
|
my $location = ($c->param('Location') || '');
|
|
|
|
|
|
|
|
# controls
|
|
|
|
$res = $c->validate_printer($name, $description, $location);
|
|
|
|
$result .= $res unless $res eq 'OK';
|
|
|
|
|
|
|
|
if ($location eq 'remote' and !$result) {
|
|
|
|
$prt_datas{'trt'} = 'NET';
|
|
|
|
$prt_datas{'name'} = $name;
|
|
|
|
$prt_datas{'description'} = $description;
|
|
|
|
$prt_datas{'location'} = $location;
|
|
|
|
$c->stash(title => $title, prt_datas => \%prt_datas);
|
|
|
|
return $c->render(template => 'printers');
|
|
|
|
} ## end if ($location eq 'remote'...)
|
|
|
|
$res = '';
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
$res = $c->new_printer($name, $description, $location);
|
|
|
|
|
|
|
|
#$remoteName, $address );
|
|
|
|
$result .= $res unless $res eq 'OK';
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
$result = $c->l('prt_CREATED_SUCCESSFULLY') . ' ' . $name;
|
|
|
|
}
|
|
|
|
} ## end if (!$result)
|
|
|
|
} ## end if ($trt eq 'ADD')
|
|
|
|
|
|
|
|
if ($trt eq 'NET') {
|
|
|
|
my $name = ($c->param('Name') || '');
|
2024-03-22 04:54:28 +01:00
|
|
|
my $description = ($c->param('Description') || '');
|
2025-01-14 13:49:31 +01:00
|
|
|
my $location = ($c->param('Location') || '');
|
|
|
|
my $remoteName = ($c->param('RemoteName') || '');
|
|
|
|
my $address = ($c->param('Address') || '');
|
|
|
|
$prt_datas{'name'} = $name;
|
|
|
|
$prt_datas{'description'} = $description;
|
|
|
|
$prt_datas{'location'} = $location;
|
|
|
|
|
|
|
|
# controls
|
|
|
|
$res = $c->validate_network($location, $remoteName, $address);
|
|
|
|
$result .= $res unless $res eq 'OK';
|
|
|
|
$res = '';
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
$res = $c->new_printer($name, $description, $location, $remoteName, $address);
|
|
|
|
$result .= $res unless $res eq 'OK';
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
$result = $c->l('prt_CREATED_SUCCESSFULLY') . ' ' . $name;
|
|
|
|
}
|
|
|
|
} ## end if (!$result)
|
|
|
|
} ## end if ($trt eq 'NET')
|
|
|
|
|
|
|
|
if ($trt eq 'DEL') {
|
|
|
|
my $printer = ($c->param('printer') || '');
|
|
|
|
|
|
|
|
if ($printer =~ /^([a-z][a-z0-9]*)$/) {
|
|
|
|
$printer = $1;
|
|
|
|
} else {
|
|
|
|
$result .= $c->l('prt_ERR_INTERNAL_FAILURE') . ':' . $printer;
|
|
|
|
}
|
|
|
|
my $rec = $adb->get($printer);
|
|
|
|
$result .= $c->l('prt_ERR_INTERNAL_FAILURE') . ':' . $printer unless ($rec);
|
|
|
|
$res = '';
|
2024-03-22 04:54:28 +01:00
|
|
|
|
2025-01-14 13:49:31 +01:00
|
|
|
if (!$result) {
|
|
|
|
$res = $c->del_printer($printer);
|
|
|
|
$result .= $res unless $res eq 'OK';
|
2024-03-22 04:54:28 +01:00
|
|
|
|
2025-01-14 13:49:31 +01:00
|
|
|
if (!$result) {
|
|
|
|
$result = $c->l('prt_DELETED_SUCCESSFULLY') . ' ' . $printer;
|
|
|
|
}
|
|
|
|
} ## end if (!$result)
|
|
|
|
} ## end if ($trt eq 'DEL')
|
2024-03-22 04:54:28 +01:00
|
|
|
|
2025-01-14 13:49:31 +01:00
|
|
|
# common parts
|
2024-03-22 04:54:28 +01:00
|
|
|
if ($res ne 'OK') {
|
2025-01-14 13:49:31 +01:00
|
|
|
$c->stash(error => $result);
|
|
|
|
$c->stash(title => $title, prt_datas => \%prt_datas);
|
|
|
|
return $c->render('printers');
|
2024-03-22 04:54:28 +01:00
|
|
|
}
|
|
|
|
my $message = "'Printers' updates ($trt) DONE";
|
|
|
|
$c->app->log->info($message);
|
2025-01-14 13:49:31 +01:00
|
|
|
$c->flash(success => $result);
|
2024-03-22 04:54:28 +01:00
|
|
|
|
2025-01-14 13:49:31 +01:00
|
|
|
#$c->flash( error => 'No changes applied !!' ); # for testing purpose
|
2024-03-22 04:54:28 +01:00
|
|
|
$c->redirect_to('/printers');
|
2025-01-14 13:49:31 +01:00
|
|
|
} ## end sub do_update
|
2024-03-22 04:54:28 +01:00
|
|
|
|
|
|
|
sub del_printer {
|
2025-01-14 13:49:31 +01:00
|
|
|
my ($c, $printer) = @_;
|
2024-03-22 04:54:28 +01:00
|
|
|
|
|
|
|
# Update the db account (1)
|
|
|
|
my $rec = $adb->get($printer);
|
|
|
|
$rec->set_prop('type', 'printer-deleted');
|
2025-01-14 13:49:31 +01:00
|
|
|
system("/sbin/e-smith/signal-event printer-delete $printer") == 0
|
|
|
|
or return $c->error('ERR_DELETING');
|
2024-03-22 04:54:28 +01:00
|
|
|
$rec->delete();
|
|
|
|
return 'OK';
|
2025-01-14 13:49:31 +01:00
|
|
|
} ## end sub del_printer
|
2024-03-22 04:54:28 +01:00
|
|
|
|
|
|
|
sub validate_printer {
|
2025-01-14 13:49:31 +01:00
|
|
|
my ($c, $name, $description, $location, $remoteName, $address) = @_;
|
2024-03-22 04:54:28 +01:00
|
|
|
|
|
|
|
#------------------------------------------------------------
|
|
|
|
# Validate parameters and untaint them
|
|
|
|
#------------------------------------------------------------
|
2025-01-14 13:49:31 +01:00
|
|
|
if ($name =~ /^([a-z][a-z0-9]*)$/) {
|
2024-03-22 04:54:28 +01:00
|
|
|
$name = $1;
|
2025-01-14 13:49:31 +01:00
|
|
|
} else {
|
2024-03-22 04:54:28 +01:00
|
|
|
return $c->l('prt_ERR_UNEXPECTED_NAME') . ': ' . $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($description =~ /^([\'\w\s]+)$/) {
|
2025-01-14 13:49:31 +01:00
|
|
|
$description = $1;
|
2024-03-22 04:54:28 +01:00
|
|
|
} else {
|
|
|
|
return $c->l('prt_ERR_UNEXPECTED_DESC') . ': ' . $description;
|
|
|
|
}
|
|
|
|
|
2025-01-14 13:49:31 +01:00
|
|
|
if ($location =~ /^(lp[0-9]+|remote|usb\/lp[0-9]+)$/) {
|
2024-03-22 04:54:28 +01:00
|
|
|
$location = $1;
|
|
|
|
} else {
|
|
|
|
$location = "lp0";
|
|
|
|
}
|
|
|
|
|
|
|
|
#------------------------------------------------------------
|
|
|
|
# Looks good. Find out if this printer has been taken
|
|
|
|
#------------------------------------------------------------
|
|
|
|
my $rec = $adb->get($name);
|
|
|
|
my $type;
|
2025-01-14 13:49:31 +01:00
|
|
|
|
2024-03-22 04:54:28 +01:00
|
|
|
if ($rec and ($type = $rec->prop('type'))) {
|
|
|
|
return $c->l('prt_ERR_EXISTS') . ' : ' . $name;
|
|
|
|
}
|
|
|
|
return 'OK';
|
2025-01-14 13:49:31 +01:00
|
|
|
} ## end sub validate_printer
|
2024-03-22 04:54:28 +01:00
|
|
|
|
|
|
|
sub validate_network {
|
2025-01-14 13:49:31 +01:00
|
|
|
my ($c, $location, $remoteName, $address) = @_;
|
2024-03-22 04:54:28 +01:00
|
|
|
|
|
|
|
if ($location eq 'remote') {
|
2025-01-14 13:49:31 +01:00
|
|
|
my $msg = hostname_or_ip2($c, $address);
|
|
|
|
return $msg unless $msg eq 'OK';
|
2024-03-22 04:54:28 +01:00
|
|
|
|
|
|
|
if ($address =~ /^([a-zA-Z0-9\.\-]+)$/) {
|
|
|
|
$address = $1;
|
|
|
|
} else {
|
|
|
|
return $c->l('prt_ERR_INVALID_ADDRESS') . ' : ' . $address;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($remoteName =~ /^([^\|]*)$/) {
|
|
|
|
$remoteName = $1;
|
|
|
|
} else {
|
|
|
|
return $c->l('prt_ERR_INVALID_REMOTE_NAME') . ' : ' . $remoteName;
|
|
|
|
}
|
2025-01-14 13:49:31 +01:00
|
|
|
} ## end if ($location eq 'remote')
|
2024-03-22 04:54:28 +01:00
|
|
|
return 'OK';
|
2025-01-14 13:49:31 +01:00
|
|
|
} ## end sub validate_network
|
2024-03-22 04:54:28 +01:00
|
|
|
|
|
|
|
sub new_printer {
|
2025-01-14 13:49:31 +01:00
|
|
|
my ($c, $name, $description, $location, $remoteName, $address) = @_;
|
2024-03-22 04:54:28 +01:00
|
|
|
|
|
|
|
#------------------------------------------------------------
|
2025-01-14 13:49:31 +01:00
|
|
|
# Printer name is available! Update printers database and
|
2024-03-22 04:54:28 +01:00
|
|
|
# signal the create-printer event.
|
|
|
|
#------------------------------------------------------------
|
|
|
|
my $result = '';
|
2025-01-14 13:49:31 +01:00
|
|
|
my $rec = $adb->new_record(
|
|
|
|
$name,
|
|
|
|
{ type => 'printer',
|
|
|
|
Description => $description,
|
|
|
|
Address => $address,
|
|
|
|
RemoteName => $remoteName,
|
|
|
|
Location => $location
|
|
|
|
}
|
|
|
|
);
|
|
|
|
system("/sbin/e-smith/signal-event printer-create $name") == 0
|
|
|
|
or return $c->error('ERR_CREATING');
|
|
|
|
return 'OK',;
|
|
|
|
} ## end sub new_printer
|
2024-03-22 04:54:28 +01:00
|
|
|
|
|
|
|
sub hostname_or_ip2 {
|
|
|
|
my ($fm, $data) = @_;
|
2025-01-14 13:49:31 +01:00
|
|
|
|
2024-03-22 04:54:28 +01:00
|
|
|
if ($data =~ /^[\d\.]+$/) {
|
2025-01-14 13:49:31 +01:00
|
|
|
if (ip_number2($fm, $data) eq "OK") {
|
2024-03-22 04:54:28 +01:00
|
|
|
return "OK";
|
2025-01-14 13:49:31 +01:00
|
|
|
} else {
|
2024-03-22 04:54:28 +01:00
|
|
|
return $fm->l('prt_MUST_BE_VALID_HOSTNAME_OR_IP');
|
|
|
|
}
|
2025-01-14 13:49:31 +01:00
|
|
|
} elsif ($data =~ /^([a-zA-Z0-9\.\-]+)$/) {
|
2024-03-22 04:54:28 +01:00
|
|
|
return "OK";
|
2025-01-14 13:49:31 +01:00
|
|
|
} else {
|
2024-03-22 04:54:28 +01:00
|
|
|
return $fm->l('prt_MUST_BE_VALID_HOSTNAME_OR_IP');
|
|
|
|
}
|
2025-01-14 13:49:31 +01:00
|
|
|
} ## end sub hostname_or_ip2
|
2024-03-22 04:54:28 +01:00
|
|
|
|
|
|
|
sub ip_number2 {
|
2025-01-14 13:49:31 +01:00
|
|
|
|
2024-03-22 04:54:28 +01:00
|
|
|
# from CGI::FormMagick::Validator::ip_number($fm, $data)
|
|
|
|
my ($fm, $data) = @_;
|
|
|
|
return undef unless defined $data;
|
|
|
|
return 'FM_IP_NUMBER1' unless $data =~ /^[\d.]+$/;
|
|
|
|
my @octets = split /\./, $data;
|
|
|
|
my $dots = ($data =~ tr/.//);
|
|
|
|
return 'FM_IP_NUMBER2' unless (scalar @octets == 4 and $dots == 3);
|
|
|
|
|
|
|
|
foreach my $octet (@octets) {
|
2025-01-14 13:49:31 +01:00
|
|
|
return $fm->l("FM_IP_NUMBER3", { octet => $octet }) if $octet > 255;
|
2024-03-22 04:54:28 +01:00
|
|
|
}
|
|
|
|
return 'OK';
|
2025-01-14 13:49:31 +01:00
|
|
|
} ## end sub ip_number2
|
2024-03-22 04:54:28 +01:00
|
|
|
|
|
|
|
=head2 publicAccess_list
|
|
|
|
|
|
|
|
Returns the hash of public access settings for showing in the public
|
|
|
|
access drop down list.
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
sub printerLocation_list {
|
|
|
|
my $c = shift;
|
2025-01-14 13:49:31 +01:00
|
|
|
return [
|
|
|
|
[ $c->l('prt_LOCAL_PRINTER_0') => 'lp0' ],
|
|
|
|
[ $c->l('prt_LOCAL_PRINTER_1') => 'lp1' ],
|
|
|
|
[ $c->l('prt_LOCAL_PRINTER_2') => 'lp2' ],
|
|
|
|
[ $c->l('prt_NET_PRINTER') => 'remote' ],
|
|
|
|
[ $c->l('prt_FIRST_USB_PRINTER') => 'usb/lp0' ],
|
|
|
|
[ $c->l('prt_SECOND_USB_PRINTER') => 'usb/lp1' ]
|
|
|
|
];
|
|
|
|
} ## end sub printerLocation_list
|
2024-03-22 04:54:28 +01:00
|
|
|
1
|