use
Mojo::Util
qw(getopt class_to_path dumper sha1_sum encode)
;
has
description
=>
'Generate database records, files and pages for a new domain'
;
has
usage
=>
sub
{
shift
->extract_usage };
has
count
=> 0;
has
owner
=>
sub
{
$_
[0]->app->users->find_where({
login_name
=>
'foo'
});
};
has
[
qw(skip_qr refresh_qr dom)
] =>
undef
;
sub
run (
$self
,
@args
) {
my
$app
=
$self
->app;
getopt \
@args
,
'n|name=s'
=> \(
my
$name
),
'o|owner=s'
=> \(
my
$owner
=
$self
->owner->{login_name}),
'a|aliases=s'
=> \(
my
$aliases
),
'c|chmod=s'
=> \(
my
$chmod
=
oct
(755)),
's|skip=s'
=> \(
my
$skip_qr
),
'r|refresh=s'
=> \(
my
$refresh_qr
),
;
no
warnings
'redefine'
;
local
*Data::Dumper::qquote
=
sub
{
qq["${\(shift)}"]
};
local
$Data::Dumper::Useperl
= 1;
unless
(
$name
) {
say
'Domain name like example.com is a mandatory argument!'
. $/;
say
$self
->usage;
return
;
}
my
$root
=
$app
->config->{domove_root};
say
'Domains folder: '
.
$root
;
if
(
$owner
eq
$self
->owner->{login_name}) {
say
"Assuming owner '$owner'."
;
}
else
{
say
"Owner is '$owner'."
;
}
my
$default_aliases
=
join
(
','
,
map
{
"$_.$name"
}
qw(www dev qa)
);
unless
(
$aliases
) {
$aliases
=
$default_aliases
;
say
'Assuming domain aliases: '
.
$aliases
;
}
else
{
$aliases
=~ s/\s+//g;
$aliases
=
join
(
', '
,
split
(/\,/,
$aliases
),
$default_aliases
);
say
'Domain aliases: '
.
$aliases
;
}
unless
(
defined
$skip_qr
) {
say
'Will not skip any file.'
;
}
else
{
say
"Will skip files matching '$skip_qr'."
;
$self
->skip_qr(
$skip_qr
);
}
unless
(
defined
$refresh_qr
) {
say
'Will not refresh any file.'
;
}
else
{
say
"Will refresh files matching '$refresh_qr'."
;
$self
->refresh_qr(
$refresh_qr
);
}
$self
->create_dir(
$root
)->chmod_file(
$root
,
$chmod
)->create_dir(
"$root/$name"
)
->chmod_file(
"$root/$name"
,
$chmod
);
$self
->_copy_resourses_to(
"$root/$name"
,
$chmod
);
$self
->_create_domain(
$name
,
$aliases
)->_create_pages()->_update_admin()
unless
defined
$self
->refresh_qr;
return
$self
;
}
sub
_copy_resourses_to (
$self
,
$to
,
$chmod
) {
my
$from
= path(
$INC
{class_to_path(
'Slovo'
)})->sibling(
'Slovo/resources'
);
my
$from_public
=
"$from/public"
;
my
$to_public
=
"$to/public"
;
path(
$from_public
)
->list_tree->
each
(
sub
{ _copy_to(
$self
,
$from_public
,
@_
,
$to_public
,
$chmod
) });
my
$from_tpl
=
"$from/templates"
;
my
$to_tpl
=
"$to/templates"
;
path(
$from_tpl
)
->list_tree->
each
(
sub
{ _copy_to(
$self
,
$from_tpl
,
@_
,
$to_tpl
,
$chmod
) });
say
" "
.
$self
->count .
" files created in $to."
if
$self
->count;
return
;
}
sub
_copy_to (
$self
,
$from
,
$f
,
$i
,
$to
,
$chmod
) {
my
$skip_qr
=
$self
->skip_qr;
my
$refresh_qr
=
$self
->refresh_qr;
my
$copy
=
$to
.
'/'
. (
$f
=~ s/
$from
\///r);
return
if
defined
$skip_qr
&&
$copy
=~ /
$skip_qr
/;
if
(
defined
$refresh_qr
&&
$copy
=~ /
$refresh_qr
/ && -e
$copy
) {
path(
$copy
)
->remove_tree({
keep_root
=> 1,
verbose
=> 1,
safe
=> 1,
error
=> \(
my
$err
)});
_handle_remove_err(
$err
);
}
return
$self
->create_dir(
$copy
)->chmod_file(
$copy
,
$chmod
)
if
-d
$f
;
if
(-f
$f
) {
my
$parent
= path(
$copy
)->dirname;
$self
->create_dir(
$parent
)->chmod_file(
$parent
,
$chmod
)
unless
-d
$parent
;
unless
(-f
$copy
) {
$f
->copy_to(
$copy
) &&
say
" [write] $copy"
;
$self
->count(
$self
->count + 1);
}
else
{
say
" [exist] $copy"
;
}
}
return
;
}
sub
_handle_remove_err (
$err
) {
if
(
$err
&&
@$err
) {
for
my
$diag
(
@$err
) {
my
(
$file
,
$message
) =
%$diag
;
if
(
$file
eq
''
) {
say
"general error: $message"
;
}
else
{
say
"problem unlinking $file: $message"
;
}
}
}
return
;
}
sub
_create_domain (
$self
,
$name
,
$aliases
) {
say
'Creating record for domain '
.
$name
;
my
$domove
=
$self
->app->domove;
my
$dom
= {
domain
=>
$name
,
aliases
=>
$aliases
,
site_name
=>
lc
$name
,
description
=>
"Великият нов дом $name…"
,
owner_id
=>
$self
->owner->{id},
group_id
=>
$self
->owner->{group_id},
published
=> 2
};
say
"Will create domain with the following data."
. dumper(
$dom
);
my
$id
=
$domove
->add(
$dom
);
$self
->dom(
$domove
->find(
$id
));
return
$self
;
}
sub
_create_pages (
$self
) {
my
$time
=
time
;
my
$dom
=
$self
->dom;
say
"Creating pages in domain $dom->{domain}…"
;
my
$common_data
= {
language
=>
'bg-bg'
,
published
=> 2,
dom_id
=>
$dom
->{id},
data_format
=>
'html'
,
user_id
=>
$self
->owner->{id},
group_id
=>
$self
->owner->{group_id},
changed_by
=>
$self
->owner->{id},
tstamp
=>
$time
,
start
=>
$time
,
};
my
$pages
= [{
alias
=>
'коренъ'
,
title
=>
'Добре дошли!'
,
page_type
=>
$self
->app->defaults(
'page_types'
)->[0],
body
=>
"<p>Добре сте ни дошли у $dom->{site_name}.</p>"
.
'<p>Променете съдържанието по ваше усмотрение.</p>'
,
permissions
=>
'drwxr-xr-x'
,
%$common_data
,
},
{
alias
=>
'ѿносно'
,
title
=>
'Ѿносно'
,
page_type
=>
'обичайна'
,
body
=>
"<p>Относно, мѣстото $dom->{site_name}, "
.
'собствениците и каквото друго се сетите.</p>'
.
'<p>Нѣкaкъв по-дълъг теѯт, който е тѣло на писанѥто.</p>'
,
permissions
=>
'rwxr-xr-x'
,
%$common_data
,
}];
my
$app
=
$self
->app;
my
$root_id
= 0;
for
my
$p
(
@$pages
) {
$p
->{pid} =
$root_id
;
my
$id
=
$app
->stranici->add(
$p
);
$root_id
||=
$id
;
}
return
$self
;
}
sub
_update_admin (
$self
) {
my
$o
=
$self
->owner;
$o
->{login_password} = c(
split
(
''
,
time
),
split
(
''
,
'$[]{}_-%№€'
),
split
(
''
,
'абвгдежзийклмнопрстуфхцчшщьюяѥыѣѫѧѭѩѯꙃꙁѿ'
))->shuffle->head(15)->
join
;
my
$stop_date
=
time
+ 3600;
$self
->app->users->save(
$o
->{id},
{
%$o
,
changed_by
=>
$o
->{id},
groups
=> [1],
disabled
=> 0,
stop_date
=>
$stop_date
,
login_password
=> sha1_sum(encode(
"utf8"
,
"$o->{login_name}$o->{login_password}"
))}
);
say
"User $o->{login_name} is enabled till "
.
localtime
(
$stop_date
);
say
"The password is $o->{login_password}"
;
return
$self
;
}
1;