4 use vars qw(@ISA $conf $apacheroot $apachemachine $apacheip $nossh_hack );
5 #use FS::Record qw( qsearch qsearchs );
6 use FS::Record qw( qsearchs dbh );
14 @ISA = qw( FS::svc_Common );
16 #ask FS::UID to run this stuff for us later
17 $FS::UID::callback{'FS::svc_www'} = sub {
19 $apacheroot = $conf->config('apacheroot');
20 $apachemachine = $conf->config('apachemachine');
21 $apacheip = $conf->config('apacheip');
26 FS::svc_www - Object methods for svc_www records
32 $record = new FS::svc_www \%hash;
33 $record = new FS::svc_www { 'column' => 'value' };
35 $error = $record->insert;
37 $error = $new_record->replace($old_record);
39 $error = $record->delete;
41 $error = $record->check;
43 $error = $record->suspend;
45 $error = $record->unsuspend;
47 $error = $record->cancel;
51 An FS::svc_www object represents an web virtual host. FS::svc_www inherits
52 from FS::svc_Common. The following fields are currently supported:
56 =item svcnum - primary key
58 =item recnum - DNS `A' record corresponding to this web virtual host. (see L<FS::domain_record>)
60 =item usersvc - account (see L<FS::svc_acct>) corresponding to this web virtual host.
70 Creates a new web virtual host. To add the record to the database, see
73 Note that this stores the hash reference, not a distinct copy of the hash it
74 points to. You can ask the object for a copy with the I<hash> method.
78 sub table { 'svc_www'; }
82 Adds this record to the database. If there is an error, returns the error,
83 otherwise returns false.
85 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
86 defined. An FS::cust_svc record will be created and inserted.
88 If the configuration values (see L<FS::Conf>) I<apachemachine>, and
89 I<apacheroot> exist, the command:
91 mkdir $apacheroot/$zone;
92 chown $username $apacheroot/$zone;
93 ln -s $apacheroot/$zone $homedir/$zone
95 I<$zone> is the DNS A record pointed to by I<recnum>
96 I<$username> is the username pointed to by I<usersvc>
97 I<$homedir> is that user's home directory
99 is executed on I<apachemachine> via ssh. This behaviour can be surpressed by
100 setting $FS::svc_www::nossh_hack true.
107 my $error = $self->check;
108 return $error if $error;
110 local $SIG{HUP} = 'IGNORE';
111 local $SIG{INT} = 'IGNORE';
112 local $SIG{QUIT} = 'IGNORE';
113 local $SIG{TERM} = 'IGNORE';
114 local $SIG{TSTP} = 'IGNORE';
115 local $SIG{PIPE} = 'IGNORE';
117 my $oldAutoCommit = $FS::UID::AutoCommit;
118 local $FS::UID::AutoCommit = 0;
121 #if ( $self->recnum =~ /^([\w\-]+|\@)\.(([\w\.\-]+\.)+\w+)$/ ) {
122 if ( $self->recnum =~ /^([\w\-]+|\@)\.(\d+)$/ ) {
123 my( $reczone, $domain_svcnum ) = ( $1, $2 );
124 unless ( $apacheip ) {
125 $dbh->rollback if $oldAutoCommit;
126 return "Configuration option apacheip not set; can't autocreate A record";
127 #"for $reczone". $svc_domain->domain;
129 my $domain_record = new FS::domain_record {
130 'svcnum' => $domain_svcnum,
131 'reczone' => $reczone,
134 'recdata' => $apacheip,
136 $error = $domain_record->insert;
138 $dbh->rollback if $oldAutoCommit;
141 $self->recnum($domain_record->recnum);
144 $error = $self->SUPER::insert;
146 $dbh->rollback if $oldAutoCommit;
150 my $domain_record = qsearchs('domain_record', { 'recnum' => $self->recnum } ); # or die ?
151 my $zone = $domain_record->reczone;
153 unless ( $zone =~ /\.$/ ) {
154 my $dom_svcnum = $domain_record->svcnum;
155 my $svc_domain = qsearchs('svc_domain', { 'svcnum' => $dom_svcnum } );
157 $zone .= '.'. $svc_domain->domain;
160 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $self->usersvc } );
162 my $username = $svc_acct->username;
164 my $homedir = $svc_acct->dir;
174 ssh("root\@$apachemachine",
175 "mkdir $apacheroot/$zone; ".
176 "chown $username $apacheroot/$zone; ".
177 "ln -s $apacheroot/$zone $homedir/$zone"
181 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
187 Delete this record from the database.
195 $error = $self->SUPER::delete;
196 return $error if $error;
201 =item replace OLD_RECORD
203 Replaces the OLD_RECORD with this one in the database. If there is an error,
204 returns the error, otherwise returns false.
209 my ( $new, $old ) = ( shift, shift );
212 $error = $new->SUPER::replace($old);
213 return $error if $error;
220 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
224 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
228 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
232 Checks all fields to make sure this is a valid web virtual host. If there is
233 an error, returns the error, otherwise returns false. Called by the insert
241 my $x = $self->setfixed;
242 return $x unless ref($x);
246 $self->ut_numbern('svcnum')
247 # || $self->ut_number('recnum')
248 || $self->ut_number('usersvc')
250 return $error if $error;
252 if ( $self->recnum =~ /^(\d+)$/ ) {
255 return "Unknown recnum: ". $self->recnum
256 unless qsearchs('domain_record', { 'recnum' => $self->recnum } );
258 } elsif ( $self->recnum =~ /^([\w\-]+|\@)\.(([\w\.\-]+\.)+\w+)$/ ) {
260 my( $reczone, $domain ) = ( $1, $2 );
262 my $svc_domain = qsearchs( 'svc_domain', { 'domain' => $domain } )
263 or return "unknown domain $domain (recnum $1.$2)";
265 my $domain_record = qsearchs( 'domain_record', {
266 'reczone' => $reczone,
267 'svcnum' => $svc_domain->svcnum,
270 if ( $domain_record ) {
271 $self->recnum($domain_record->recnum);
273 #insert will create it
274 #$self->recnum("$reczone.$domain");
275 $self->recnum("$reczone.". $svc_domain->svcnum);
279 return "Illegal recnum: ". $self->recnum;
282 return "Unknown usersvc (svc_acct.svcnum): ". $self->usersvc
283 unless qsearchs('svc_acct', { 'svcnum' => $self->usersvc } );
294 L<FS::svc_Common>, L<FS::Record>, L<FS::domain_record>, L<FS::cust_svc>,
295 L<FS::part_svc>, L<FS::cust_pkg>, schema.html from the base documentation.