| 1 | package iRedAdmin; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 1 | ▶ | use Email::Valid; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 1 | ▶ | use Encode; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use WWW::Mechanize; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | ▶ | use HTTP::Cookies; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 1 | ▶ | use iRedAdmin::Admin; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 1 | ▶ | use iRedAdmin::Domain; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 1 | ▶ | use iRedAdmin::User; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 1 | our $VERSION = '0.04'; | |||||||||||||
| 13 | |||||||||||||||
| 14 | has 'url' => ( | ||||||||||||||
| 15 | is => 'ro', | ||||||||||||||
| 16 | 0 | ▶ | isa => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 17 | 0 | my $url = $_[0]; | |||||||||||||
| 18 | 0 | $url =~ s/^https?:\/\///; | |||||||||||||
| 19 | 0 | $url =~ s/\/.*$//; | |||||||||||||
| 20 | 0 | ▶ | die "Domain is invalid! $url" unless Email::Valid->address('domain@'.$url) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 21 | } | ||||||||||||||
| 22 | 1 | ); | |||||||||||||
| 23 | |||||||||||||||
| 24 | has 'username' => ( | ||||||||||||||
| 25 | is => 'ro', | ||||||||||||||
| 26 | 0 | ▶ | isa => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 27 | 0 | ▶ | die "Username is invalid!" unless Email::Valid->address($_[0]) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 28 | } | ||||||||||||||
| 29 | 1 | ); | |||||||||||||
| 30 | |||||||||||||||
| 31 | has 'password' => ( | ||||||||||||||
| 32 | is => 'ro', | ||||||||||||||
| 33 | 0 | ▶ | isa => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 34 | 0 | ▶ | die "Password this empty!" unless $_[0] =~ /.{1,}/ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 35 | } | ||||||||||||||
| 36 | 1 | ); | |||||||||||||
| 37 | |||||||||||||||
| 38 | has 'cookie' => ( | ||||||||||||||
| 39 | is => 'ro', | ||||||||||||||
| 40 | 0 | ▶ | isa => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 41 | 0 | ▶ | die "Cookie this empty!" unless $_[0] =~ /.{1,}/ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 42 | } | ||||||||||||||
| 43 | 1 | ); | |||||||||||||
| 44 | |||||||||||||||
| 45 | has 'lang' => ( | ||||||||||||||
| 46 | is => 'ro', | ||||||||||||||
| 47 | 0 | ▶ | default => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 48 | 0 | 'en_US' | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | 1 | ); | |||||||||||||
| 51 | |||||||||||||||
| 52 | 1 | has 'mech' => (is => 'ro', writer => 'set_mech'); | |||||||||||||
| 53 | |||||||||||||||
| 54 | 1 | has 'error' => (is => 'ro', writer => 'set_error'); | |||||||||||||
| 55 | |||||||||||||||
| 56 | 0 | ▶ | sub BUILD { | ||||||||||||
|
Subroutine
BUILD: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||
| 57 | 0 | my $self = shift; | |||||||||||||
| 58 | |||||||||||||||
| 59 | 0 | my $cookie_jar = HTTP::Cookies->new( | |||||||||||||
| 60 | file => $self->cookie, | ||||||||||||||
| 61 | ignore_discard => 1, | ||||||||||||||
| 62 | autosave => 1 | ||||||||||||||
| 63 | ); | ||||||||||||||
| 64 | |||||||||||||||
| 65 | 0 | my $mech = WWW::Mechanize->new( | |||||||||||||
| 66 | autocheck => 0, | ||||||||||||||
| 67 | cookie_jar => $cookie_jar, | ||||||||||||||
| 68 | ssl_opts => { | ||||||||||||||
| 69 | verify_hostname => 0 | ||||||||||||||
| 70 | } | ||||||||||||||
| 71 | ); | ||||||||||||||
| 72 | |||||||||||||||
| 73 | 0 | $self->set_mech($mech); | |||||||||||||
| 74 | |||||||||||||||
| 75 | 0 | ▶ | if (-e $self->cookie) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 76 | 0 | my $result = undef; | |||||||||||||
| 77 | 0 | ▶ | open FILE, '<', $self->cookie or die $!; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | 0 | ▶ | while (<FILE>) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 79 | 0 | chomp; | |||||||||||||
| 80 | 0 | ▶ | $result = $_ unless $result; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 81 | } | ||||||||||||||
| 82 | 0 | close FILE; | |||||||||||||
| 83 | 0 | ▶ | $result =~ s/\s//g if $result; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 84 | 0 | ▶ | unless($result){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | 0 | $self->_connect; | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | }else{ | ||||||||||||||
| 88 | 0 | $self->_connect; | |||||||||||||
| 89 | } | ||||||||||||||
| 90 | } | ||||||||||||||
| 91 | |||||||||||||||
| 92 | 0 | ▶ | sub _connect { | ||||||||||||
|
Subroutine
_connect: not called CC=2 SCAR=17.9
| |||||||||||||||
| 93 | 0 | my $self = shift; | |||||||||||||
| 94 | |||||||||||||||
| 95 | 0 | $self->mech->post($self->_address . '/login', | |||||||||||||
| 96 | [ | ||||||||||||||
| 97 | username => $self->username, | ||||||||||||||
| 98 | password => $self->password, | ||||||||||||||
| 99 | login => 'Login', | ||||||||||||||
| 100 | lang => $self->_lang($self->lang), | ||||||||||||||
| 101 | save_pass => 'yes' | ||||||||||||||
| 102 | ] | ||||||||||||||
| 103 | ); | ||||||||||||||
| 104 | |||||||||||||||
| 105 | 0 | ▶ | if($self->mech->content =~ /login_form/){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 106 | 0 | $self->mech->content =~ m!</strong> (.*?)</p>!i; | |||||||||||||
| 107 | 0 | $self->set_error(encode('utf-8', $1)); | |||||||||||||
| 108 | 0 | return 0; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | |||||||||||||||
| 111 | 0 | return 2; | |||||||||||||
| 112 | }; | ||||||||||||||
| 113 | |||||||||||||||
| 114 | 0 | ▶ | sub _success { | ||||||||||||
|
Subroutine
_success: not called CC=3 SCAR=24.8
| |||||||||||||||
| 115 | 0 | my $self = shift; | |||||||||||||
| 116 | |||||||||||||||
| 117 | 0 | ▶ | if($self->mech->content =~ /login_form/){ | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 118 | 0 | $self->_connect; | |||||||||||||
| 119 | }elsif($self->mech->content =~ /error/i) { | ||||||||||||||
| 120 | 0 | $self->mech->content =~ m!</strong> (.*?)</p>!i; | |||||||||||||
| 121 | 0 | $self->set_error(encode('utf-8', $1)); | |||||||||||||
| 122 | 0 | return 0; | |||||||||||||
| 123 | }else{ | ||||||||||||||
| 124 | 0 | return 1; | |||||||||||||
| 125 | } | ||||||||||||||
| 126 | } | ||||||||||||||
| 127 | |||||||||||||||
| 128 | 0 | ▶ | sub _address { | ||||||||||||
|
Subroutine
_address: not called CC=1 SCAR=6.9
| |||||||||||||||
| 129 | 0 | my $self = shift; | |||||||||||||
| 130 | |||||||||||||||
| 131 | 0 | my $url = $self->url; | |||||||||||||
| 132 | 0 | $url =~ s/\/$//; | |||||||||||||
| 133 | 0 | return $url; | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | |||||||||||||||
| 136 | 0 | ▶ | sub _lang { | ||||||||||||
|
Subroutine
_lang: not called CC=5 SCAR=34.0
| |||||||||||||||
| 137 | 0 | my ($self, $value) = @_; | |||||||||||||
| 138 | |||||||||||||||
| 139 | 0 | my @lang = qw/ | |||||||||||||
| 140 | cs_CZ | ||||||||||||||
| 141 | de_DE | ||||||||||||||
| 142 | en_US | ||||||||||||||
| 143 | es_ES | ||||||||||||||
| 144 | fi_FI | ||||||||||||||
| 145 | fr_FR | ||||||||||||||
| 146 | hu_HU | ||||||||||||||
| 147 | it_IT | ||||||||||||||
| 148 | ko_KR | ||||||||||||||
| 149 | nl_NL | ||||||||||||||
| 150 | pl_PL | ||||||||||||||
| 151 | pt_BR | ||||||||||||||
| 152 | ru_RU | ||||||||||||||
| 153 | sl_SI | ||||||||||||||
| 154 | zh_CN | ||||||||||||||
| 155 | zh_TW | ||||||||||||||
| 156 | /; | ||||||||||||||
| 157 | |||||||||||||||
| 158 | 0 | ▶ | if ($value =~ /\d/) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 159 | 0 | ▶ | return $lang[$value-1] if $lang[$value-1]; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 160 | 0 | return 'en_US'; | |||||||||||||
| 161 | }elsif($value =~ /\w/){ | ||||||||||||||
| 162 | 0 | ▶ | return $value if grep(/^$value$/, @lang); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 163 | 0 | return 'en_US'; | |||||||||||||
| 164 | }else{ | ||||||||||||||
| 165 | 0 | return 'en_US'; | |||||||||||||
| 166 | } | ||||||||||||||
| 167 | } | ||||||||||||||
| 168 | |||||||||||||||
| 169 | 0 | ▶ | sub Admin { | ||||||||||||
|
Subroutine
Admin: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 170 | 0 | return iRedAdmin::Admin->new(ref => shift); | |||||||||||||
| 171 | } | ||||||||||||||
| 172 | |||||||||||||||
| 173 | 0 | ▶ | sub Domain { | ||||||||||||
|
Subroutine
Domain: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 174 | 0 | return iRedAdmin::Domain->new(ref => shift); | |||||||||||||
| 175 | } | ||||||||||||||
| 176 | |||||||||||||||
| 177 | 0 | ▶ | sub User { | ||||||||||||
|
Subroutine
User: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 178 | 0 | return iRedAdmin::User->new(ref => shift); | |||||||||||||
| 179 | } | ||||||||||||||
| 180 | |||||||||||||||
| 181 | 0 | ▶ | sub Logout { | ||||||||||||
|
Subroutine
Logout: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 182 | 0 | my $self = shift; | |||||||||||||
| 183 | 0 | $self->mech->get($self->url . '/logout'); | |||||||||||||
| 184 | 0 | ▶ | return 1 if $self->mech->content =~ /login_form/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 185 | } | ||||||||||||||
| 186 | |||||||||||||||
| 187 | 1 | 1; | |||||||||||||
| 188 | |||||||||||||||
| 189 | |||||||||||||||
| 190 | __END__ | ||||||||||||||