| 1 |
#!/usr/bin/perl |
| 2 |
# Allows do=makesite to be used to create a new site, and do=branchsite |
| 3 |
# to branch an existing site. |
| 4 |
# |
| 5 |
# Should only be enabled on the controlsite. |
| 6 |
package IkiWiki::Plugin::makesite; |
| 7 |
|
| 8 |
use warnings; |
| 9 |
use strict; |
| 10 |
use IkiWiki 3.00; |
| 11 |
use IkiWiki::Hosting; |
| 12 |
use IkiWiki::Business; |
| 13 |
use HTML::Entities; |
| 14 |
|
| 15 |
our $DNS_READY=1; |
| 16 |
our $DNS_NEEDED=2; |
| 17 |
our $DNS_WRONG=3; |
| 18 |
|
| 19 |
my @themes=( |
| 20 |
{name => "none", description => "Simple"}, |
| 21 |
{name => "blueview", description => "BlueView"}, |
| 22 |
{name => "actiontabs", description => "ActionTabs"}, |
| 23 |
{name => "goldtype", description => "GoldType"}, |
| 24 |
); |
| 25 |
|
| 26 |
sub import { |
| 27 |
hook(type => "sessioncgi", id => "makesite", call => \&sessioncgi); |
| 28 |
} |
| 29 |
|
| 30 |
sub sessioncgi ($$) { |
| 31 |
my $cgi=shift; |
| 32 |
my $session=shift; |
| 33 |
|
| 34 |
return unless defined $cgi->param("do"); |
| 35 |
|
| 36 |
if ($cgi->param("do") eq "makesite") { |
| 37 |
makesite($cgi, $session); |
| 38 |
} |
| 39 |
elsif ($cgi->param("do") eq "branchsite") { |
| 40 |
makesite($cgi, $session, 1); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
sub makesite ($$;$) { |
| 45 |
my $cgi=shift; |
| 46 |
my $session=shift; |
| 47 |
my $branching=shift; |
| 48 |
|
| 49 |
IkiWiki::needsignin($cgi, $session); |
| 50 |
|
| 51 |
# Drop ikiwiki lock to avoid blocking other users during site |
| 52 |
# creation. |
| 53 |
IkiWiki::unlockwiki(); |
| 54 |
|
| 55 |
# Only openid and email logins are supported. |
| 56 |
my $username=$session->param("name"); |
| 57 |
if (! defined IkiWiki::openiduser($username) && ! defined IkiWiki::emailuser($username)) { |
| 58 |
error "not logged in using openid or email -- Currently only openid and email logins can create new sites, because the account is set as the admin of the new site." |
| 59 |
} |
| 60 |
|
| 61 |
my $internal_branchof; |
| 62 |
if ($branching) { |
| 63 |
$internal_branchof=$cgi->param("branchof"); |
| 64 |
|
| 65 |
my $branchable=IkiWiki::Hosting::getshell("ikisite-wrapper", |
| 66 |
"getsetup", $internal_branchof, "branchable"); |
| 67 |
if (! $branchable) { |
| 68 |
my $admins=IkiWiki::Hosting::yamlgetshell("ikisite-wrapper", |
| 69 |
"getsetup", $internal_branchof, "adminuser"); |
| 70 |
if (! ref $admins || |
| 71 |
! grep { $_ eq $session->param("name") } @$admins) { |
| 72 |
error "You are not allowed to branch this site."; |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
# Get values from form and validate. |
| 78 |
my ($dns_state, $internal_hostname, |
| 79 |
$external_hostname, $wikiname, @alias) = |
| 80 |
gen_hostnames(scalar $cgi->param("hostname"), |
| 81 |
scalar $cgi->param("domain"), |
| 82 |
scalar $cgi->param("internal_hostname")); |
| 83 |
my $type=lc($cgi->param("type")) if defined $cgi->param("type"); |
| 84 |
my $nonce=$cgi->param("nonce"); |
| 85 |
my $creating=$cgi->param("creating"); |
| 86 |
my $ready=$cgi->param("ready"); |
| 87 |
my $site_theme=$cgi->param("site_theme"); |
| 88 |
my $dns_ok=1 if (($dns_state == $DNS_READY) || $cgi->param("submit_nodns")); |
| 89 |
# The session may have an email address. |
| 90 |
my $email=$session->param("email"); |
| 91 |
if (defined $cgi->param("email")) { |
| 92 |
$email=$cgi->param("email"); |
| 93 |
} |
| 94 |
else { |
| 95 |
eval q{use IkiWiki::Customer}; |
| 96 |
error $@ if $@; |
| 97 |
$email=IkiWiki::Customer::customer_get($username, "email"); |
| 98 |
} |
| 99 |
if (defined $email) { |
| 100 |
eval q{use Email::Address}; |
| 101 |
if (! $@) { |
| 102 |
# This email address parser should be fully RFC822 |
| 103 |
# compliant, and can even extract email addresses |
| 104 |
# out of longer strings. |
| 105 |
# (But no check is done that the domain is valid.) |
| 106 |
$email = (Email::Address->parse($email))[0]; |
| 107 |
} |
| 108 |
else { |
| 109 |
# Most stupid fallback imaginable. |
| 110 |
($email) = $email=~/(.*@.*)/; |
| 111 |
} |
| 112 |
$email=undef unless defined $email && length $email; |
| 113 |
} |
| 114 |
|
| 115 |
my $acceptplan=$cgi->param("acceptplan"); |
| 116 |
my $newplan=undef; |
| 117 |
if (! IkiWiki::Business::needchangeplan($username, $internal_hostname)) { |
| 118 |
$acceptplan=1; |
| 119 |
} |
| 120 |
else { |
| 121 |
# Determine price plan. For now, the user doesn't choose a |
| 122 |
# plan, but is just set to the cheapest plan suitable for |
| 123 |
# the sites they have plus the one we're making. |
| 124 |
$newplan=IkiWiki::Business::suggestedplan($username, $internal_hostname); |
| 125 |
if (defined $cgi->param("suggestedplan") && |
| 126 |
$cgi->param("suggestedplan") ne $newplan) { |
| 127 |
# User accepted previously suggested plan, |
| 128 |
# but it's changed somehow. |
| 129 |
$acceptplan=0; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
# Since site creation takes a while, it will be done in the |
| 134 |
# background, while the user is presented with a setup screen. |
| 135 |
if (! $ready && ! $creating) { |
| 136 |
$creating=1; |
| 137 |
|
| 138 |
# Precalculate a nonce to use when the site is |
| 139 |
# created. This allows it to be modified to |
| 140 |
# finalize its setup later. |
| 141 |
$nonce=time().':'.`uuid -v4`; |
| 142 |
chomp $nonce; |
| 143 |
|
| 144 |
my $pid=IkiWiki::Hosting::daemonize(); |
| 145 |
if (!$pid) { |
| 146 |
# Now we're a daemon.. |
| 147 |
my @action=("create", $internal_hostname); |
| 148 |
if (defined $internal_branchof) { |
| 149 |
@action=("branch", $internal_branchof, $internal_hostname); |
| 150 |
} |
| 151 |
$ENV{IKISITE_NONCE}=$nonce; |
| 152 |
IkiWiki::Hosting::getshell("ikisite-wrapper", |
| 153 |
@action, |
| 154 |
"--owner=$username", |
| 155 |
"--admin=http://none/", # temporary admin until setup finishes |
| 156 |
"--wikiname=$wikiname", |
| 157 |
"--createnonce", |
| 158 |
((defined $type && length $type) ? "--type=$type" : ()), |
| 159 |
); |
| 160 |
if ($? != 0) { |
| 161 |
error sprintf("failure creating %s", |
| 162 |
encode_entities($internal_hostname)); |
| 163 |
} |
| 164 |
else { |
| 165 |
exit(0); |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
else { |
| 170 |
# If all required user input is done, just wait for the |
| 171 |
# background site creation to finish. |
| 172 |
my $canwait=$acceptplan && $dns_ok && defined $email; |
| 173 |
|
| 174 |
# Check if background site creation is done by checking if |
| 175 |
# the site exists, is no longer locked, and has the |
| 176 |
# requested nonce created. |
| 177 |
IkiWiki::Hosting::getshell("ikisite-wrapper", "checksite", $internal_hostname, |
| 178 |
"--hasnonce", ($canwait ? "--wait" : ())); |
| 179 |
if ($? == 0) { |
| 180 |
$creating=0; |
| 181 |
$ready=1; |
| 182 |
} |
| 183 |
elsif ($canwait) { |
| 184 |
error sprintf("failure creating %s", |
| 185 |
encode_entities($internal_hostname)); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
# Modify site, setting alias and/or external hostname. |
| 190 |
if ($ready && ! $creating && $acceptplan && defined $email && |
| 191 |
((@alias || ($dns_state == $DNS_READY && defined $external_hostname)))) { |
| 192 |
$ENV{IKISITE_NONCE}=$nonce; |
| 193 |
# This rebuilds the site; pretty slow. But, not as slow as |
| 194 |
# site creation, so I have not tried to background it yet. |
| 195 |
IkiWiki::Hosting::getshell("ikisite-wrapper", "domains", $internal_hostname, |
| 196 |
(($dns_state == $DNS_READY && defined $external_hostname) ? "--external=$external_hostname" : ()), |
| 197 |
map { "--alias=$_" } @alias, |
| 198 |
); |
| 199 |
if ($dns_state == $DNS_READY && defined $external_hostname && $? != 0) { |
| 200 |
# Above can fail, if the DNS is not set right etc. |
| 201 |
$dns_state=$DNS_NEEDED |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
# Record when a customer signs up. |
| 206 |
if ($acceptplan && defined $email) { |
| 207 |
eval q{use IkiWiki::Customer}; |
| 208 |
error $@ if $@; |
| 209 |
IkiWiki::Customer::customer_lock_write($username, sub { |
| 210 |
IkiWiki::Customer::customer_set($username, "currentplan", $newplan) |
| 211 |
if defined $newplan; |
| 212 |
IkiWiki::Customer::customer_set($username, "email", $email); |
| 213 |
# Ikiwiki also collects a nickname from openid. |
| 214 |
# (We'd rather have a full name, but it will do.) |
| 215 |
# Avoid overwriting name if it's already set, as |
| 216 |
# the value from openid is not very good. |
| 217 |
if (defined $session->param("nickname") && |
| 218 |
! defined IkiWiki::Customer::customer_get($username, "name")) { |
| 219 |
IkiWiki::Customer::customer_set($username, "name", |
| 220 |
$session->param("nickname")); |
| 221 |
} |
| 222 |
my @accounts=IkiWiki::Customer::account_list($username); |
| 223 |
IkiWiki::Customer::customer_set($username, "email_list", grep { defined IkiWiki::emailuser($_) } @accounts); |
| 224 |
IkiWiki::Customer::customer_set($username, "openid_list", grep { defined IkiWiki::openiduser($_) } @accounts); |
| 225 |
my $startdate=IkiWiki::Customer::customer_get($username, "startdate"); |
| 226 |
if (! defined $startdate || $startdate == 0) { |
| 227 |
IkiWiki::Customer::customer_set($username, "startdate", time); |
| 228 |
} |
| 229 |
my $balance=IkiWiki::Customer::customer_get($username, "balance"); |
| 230 |
if (! defined $balance) { |
| 231 |
IkiWiki::Customer::customer_set($username, "balance", 0); |
| 232 |
} |
| 233 |
IkiWiki::Customer::customer_commit($username); |
| 234 |
}); |
| 235 |
} |
| 236 |
|
| 237 |
# Redirect to newly created site once everything is ok. |
| 238 |
if ($ready && ! $creating && $acceptplan && defined $email && $dns_ok) { |
| 239 |
# But first, save the email address, which may have been |
| 240 |
# changed since site creation was started. And set the |
| 241 |
# username as the site admin now that they have signed up. |
| 242 |
# And if a theme was selected, set it. |
| 243 |
$ENV{IKISITE_NONCE}=$nonce; |
| 244 |
eval q{use YAML::Syck}; |
| 245 |
die $@ if $@; |
| 246 |
IkiWiki::Hosting::getshell("ikisite-wrapper", "changesetup", |
| 247 |
$internal_hostname, "--set", "adminemail=$email", |
| 248 |
"--set-yaml", "adminuser=".Dump([$username]), |
| 249 |
((defined $site_theme && length $site_theme && |
| 250 |
$site_theme ne "none") |
| 251 |
? ("--set", "theme=$site_theme") : ()), |
| 252 |
); |
| 253 |
|
| 254 |
my $redir_url; |
| 255 |
if (defined $external_hostname && $dns_state == $DNS_READY) { |
| 256 |
$redir_url="http://$external_hostname/"; |
| 257 |
} |
| 258 |
else { |
| 259 |
$redir_url="http://$internal_hostname/"; |
| 260 |
} |
| 261 |
|
| 262 |
if (! $branching) { |
| 263 |
IkiWiki::Hosting::readconfig(); |
| 264 |
my $r=$config{"welcome_redir_$type"} || $config{"welcome_redir"}; |
| 265 |
$redir_url.=$r if defined $r; |
| 266 |
} |
| 267 |
|
| 268 |
IkiWiki::redirect($cgi, $redir_url); |
| 269 |
|
| 270 |
# Setup complete; nonce no longer needed. |
| 271 |
if (defined $nonce && length $nonce) { |
| 272 |
IkiWiki::Hosting::getshell("ikisite-wrapper", "deletenonce", |
| 273 |
$internal_hostname, "--nonce=$nonce"); |
| 274 |
} |
| 275 |
exit(0); |
| 276 |
} |
| 277 |
|
| 278 |
# Display site setup screen. |
| 279 |
my $template=IkiWiki::Hosting::ikisite_template("makesite.tmpl"); |
| 280 |
|
| 281 |
# If necessary prompt user to set up DNS for their site. |
| 282 |
# FIXME: It would be better if the user logged in as admin to their |
| 283 |
# new site, and finished the DNS setup in there, rather than |
| 284 |
# it all happening in here. But that needs openid singlesignin |
| 285 |
# for the user to not have to manually login, so I hacked this |
| 286 |
# into here in the meantime. |
| 287 |
if ($dns_state == $DNS_NEEDED) { |
| 288 |
$template->param(dns_needed => 1); |
| 289 |
} |
| 290 |
elsif ($dns_state == $DNS_WRONG) { |
| 291 |
$template->param(dns_wrong => 1); |
| 292 |
} |
| 293 |
else { |
| 294 |
$template->param(dns_ok => 1); |
| 295 |
} |
| 296 |
|
| 297 |
$template->param(wikiname => $wikiname); |
| 298 |
$template->param(hostname => $cgi->param("hostname")); |
| 299 |
$template->param(domain => $cgi->param("domain")); |
| 300 |
$template->param(internal_hostname => $internal_hostname); |
| 301 |
$template->param(external_hostname => $external_hostname); |
| 302 |
$template->param(nonce => $nonce) if length $nonce; |
| 303 |
$template->param(creating => $creating); |
| 304 |
$template->param(ready => $ready); |
| 305 |
$template->param(retried => 1) if $cgi->param("retry"); |
| 306 |
$template->param(type => $type) if defined $type; |
| 307 |
$template->param(branching => 1) if $branching; |
| 308 |
$template->param(branchof => $internal_branchof) if $branching; |
| 309 |
if (defined $newplan) { |
| 310 |
$template->param(suggestedplan => $newplan); |
| 311 |
$template->param(suggestedplan_description => |
| 312 |
IkiWiki::Business::planinfo($newplan)->{description}); |
| 313 |
$template->param(suggestedplan_name => |
| 314 |
IkiWiki::Business::planinfo($newplan)->{name}); |
| 315 |
my $trial=IkiWiki::Business::planinfo($newplan)->{monthsfreetrial}; |
| 316 |
$template->param(suggestedplan_monthsfreetrial => $trial) |
| 317 |
if $trial; |
| 318 |
} |
| 319 |
$template->param(acceptplan => $acceptplan ? 1 : 0); |
| 320 |
if (! $acceptplan && $cgi->param("retry")) { |
| 321 |
$template->param(acceptplan_error => "Required"); |
| 322 |
} |
| 323 |
if (defined $site_theme && length $site_theme) { |
| 324 |
$template->param(site_theme => $site_theme); |
| 325 |
} |
| 326 |
else { |
| 327 |
$template->param(themes => [ |
| 328 |
# Add type parameter to theme list to allow |
| 329 |
# links to example sites of the right type. |
| 330 |
map { |
| 331 |
$_->{type}=defined $type ? $type : "wiki"; |
| 332 |
$_; |
| 333 |
} @themes |
| 334 |
]); |
| 335 |
} |
| 336 |
if (defined $email && $acceptplan) { |
| 337 |
$template->param(billing_ok => 1); |
| 338 |
} |
| 339 |
if (defined $email) { |
| 340 |
$template->param(email => $email); |
| 341 |
} |
| 342 |
else { |
| 343 |
$template->param(email_error => "Required"); |
| 344 |
} |
| 345 |
|
| 346 |
IkiWiki::printheader($session); |
| 347 |
print IkiWiki::cgitemplate($cgi, "site setup", $template->output); |
| 348 |
exit(0); |
| 349 |
} |
| 350 |
|
| 351 |
sub unique_internal_hostname { |
| 352 |
my ($base, $defaultdomain)=@_; |
| 353 |
|
| 354 |
my $counter=0; |
| 355 |
while (1) { |
| 356 |
my $try=$base.($counter ? ($counter+1) : "").".".$defaultdomain; |
| 357 |
# ikisite sitexists does not need to run as root |
| 358 |
IkiWiki::Hosting::getshell("ikisite", "siteexists", $try); |
| 359 |
if ($? == 0) { |
| 360 |
$counter++; |
| 361 |
} |
| 362 |
elsif ($? == 255) { |
| 363 |
# 255 is what ikisite siteexists throws on data validation error |
| 364 |
error sprintf("not allowed to use domain name %s", |
| 365 |
encode_entities($base)); |
| 366 |
} |
| 367 |
else { |
| 368 |
return $try; |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
sub gen_hostnames { |
| 374 |
# The "hostname" can be any of: A FQDN, a hostname under |
| 375 |
# the defaultdomain, or a wikiname. From this, derive an internal |
| 376 |
# hostname, an external hostname and other aliases, and look up |
| 377 |
# dns state. |
| 378 |
# |
| 379 |
# If an internal hostname has already been allocated, it can |
| 380 |
# optionally be provided. |
| 381 |
my ($hostname, $defaultdomain, $internal_hostname, $force_www)=@_; |
| 382 |
|
| 383 |
my $orig_hostname=$hostname; |
| 384 |
|
| 385 |
my ($dns_state, $external_hostname, $wikiname, @alias); |
| 386 |
|
| 387 |
# ikisite validates all data later, but early cleanup and validation |
| 388 |
# of user-supplied data allows nicer error messages. |
| 389 |
$hostname =~ s/^\s+//; |
| 390 |
$hostname =~ s/\s+$//; |
| 391 |
my $www_stripped=($hostname =~ s/^www\.//i) unless $force_www; |
| 392 |
$wikiname=$hostname; |
| 393 |
$wikiname=~s/^([^.]+).*/$1/; |
| 394 |
$hostname=lc($hostname); |
| 395 |
$defaultdomain=lc($defaultdomain); |
| 396 |
if (! length $hostname) { |
| 397 |
error "hostname not set"; |
| 398 |
} |
| 399 |
if (! length $defaultdomain) { |
| 400 |
error "empty defaultdomain"; |
| 401 |
} |
| 402 |
$hostname =~ s/(^|\.)\Q$defaultdomain\E$//; |
| 403 |
if (! length $hostname) { |
| 404 |
error "illegal hostname"; |
| 405 |
} |
| 406 |
$defaultdomain =~ s/[^-.a-z0-9]//g; |
| 407 |
if (! length $defaultdomain) { |
| 408 |
error "illegal defaultdomain"; |
| 409 |
} |
| 410 |
|
| 411 |
IkiWiki::Hosting::readconfig(); |
| 412 |
if (defined $config{"hostname_stopwords"}) { |
| 413 |
foreach my $stopword (split ' ', $config{"hostname_stopwords"}) { |
| 414 |
if ($hostname =~/\Q$stopword\E/) { |
| 415 |
error "illegal hostname"; |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
if (! defined $internal_hostname) { |
| 421 |
# Base internal hostname on what the user entered, but with |
| 422 |
# anything problimatic munged. If they entered a FQDN like |
| 423 |
# example.com, it will be example-com. A wikiname of "My Wiki" |
| 424 |
# will yield mywiki. |
| 425 |
my $base=$hostname; |
| 426 |
$base =~ s/\./-/g; |
| 427 |
$base =~ s/[^-a-z0-9]//g; |
| 428 |
if (! length $base) { |
| 429 |
error "illegal hostname"; |
| 430 |
} |
| 431 |
|
| 432 |
$internal_hostname=unique_internal_hostname($base, $defaultdomain); |
| 433 |
} |
| 434 |
|
| 435 |
my $address=IkiWiki::Hosting::host_address_or_cname($hostname, $internal_hostname); |
| 436 |
if (defined $address && ($address eq $internal_hostname || |
| 437 |
grep { defined $_ && $_ eq $address } IkiWiki::Hosting::site_addresses())) { |
| 438 |
# Hostname already exists, and its address is already |
| 439 |
# pointed at internal_hostname. |
| 440 |
$external_hostname=$hostname; |
| 441 |
$dns_state=$DNS_READY; |
| 442 |
} |
| 443 |
elsif (defined $address) { |
| 444 |
# Hostname already exists, addresss is not set right. |
| 445 |
$external_hostname=$hostname; |
| 446 |
$dns_state=$DNS_WRONG; |
| 447 |
} |
| 448 |
elsif ($hostname=~/([^.]+)\.(.+)$/ && |
| 449 |
defined IkiWiki::Hosting::host_address_or_cname($2)) { |
| 450 |
# Hostname does not exist, but looks like it was intended |
| 451 |
# to be a DNS address under a domain that has DNS |
| 452 |
$external_hostname=$hostname; |
| 453 |
$dns_state=$DNS_NEEDED; |
| 454 |
} |
| 455 |
elsif ($hostname=~/(.+)\.\w+$/) { |
| 456 |
# Hostname does not exist, but it seems to be a DNS address |
| 457 |
# with a TLD. |
| 458 |
$external_hostname=$hostname; |
| 459 |
$dns_state=$DNS_NEEDED; |
| 460 |
} |
| 461 |
else { |
| 462 |
# No separate external hostname. |
| 463 |
$external_hostname=undef; |
| 464 |
$dns_state=$DNS_READY; |
| 465 |
} |
| 466 |
|
| 467 |
if ($www_stripped && $dns_state != $DNS_READY) { |
| 468 |
# Retry without stripping www, just in case |
| 469 |
# www.hostname.com is set up right and hostname.com is not. |
| 470 |
my ($dns_state2, @rest) = gen_hostnames( |
| 471 |
$orig_hostname, $defaultdomain, $internal_hostname, 1); |
| 472 |
if ($dns_state2 == $DNS_READY) { |
| 473 |
return ($dns_state2, @rest); |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
if ($www_stripped) { |
| 478 |
@alias=$external_hostname; |
| 479 |
$external_hostname="www.$external_hostname"; |
| 480 |
} |
| 481 |
else { |
| 482 |
push @alias, "www.$external_hostname" if defined $external_hostname; |
| 483 |
} |
| 484 |
|
| 485 |
return ($dns_state, $internal_hostname, |
| 486 |
$external_hostname, $wikiname, @alias); |
| 487 |
} |
| 488 |
|
| 489 |
1 |