1 package FS::ftp_target;
4 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs );
6 use vars qw($me $DEBUG);
12 FS::ftp_target - Object methods for ftp_target records
18 $record = new FS::ftp_target \%hash;
19 $record = new FS::ftp_target { 'column' => 'value' };
21 $error = $record->insert;
23 $error = $new_record->replace($old_record);
25 $error = $record->delete;
27 $error = $record->check;
31 An FS::ftp_target object represents an account on a remote FTP or SFTP
32 server for transferring files. FS::ftp_target inherits from FS::Record.
36 =item targetnum - primary key
38 =item agentnum - L<FS::agent> foreign key; can be null
40 =item hostname - the DNS name of the FTP site
42 =item username - username
44 =item password - password
46 =item path - the working directory to change to upon connecting
48 =item secure - a flag ('Y' or null) for whether to use SFTP
58 sub table { 'ftp_target'; }
62 Creates a new FTP target. To add it to the database, see L<"insert">.
66 Adds this record to the database. If there is an error, returns the error,
67 otherwise returns false.
71 Delete this record from the database.
73 =item replace OLD_RECORD
75 Replaces the OLD_RECORD with this one in the database. If there is an error,
76 returns the error, otherwise returns false.
80 Checks all fields to make sure this is a valid example. If there is
81 an error, returns the error, otherwise returns false. Called by the insert
89 if ( !$self->get('port') ) {
90 if ( $self->secure ) {
91 $self->set('port', 22);
93 $self->set('port', 21);
98 $self->ut_numbern('targetnum')
99 || $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum')
100 || $self->ut_text('hostname')
101 || $self->ut_text('username')
102 || $self->ut_text('password')
103 || $self->ut_number('port')
104 || $self->ut_text('path')
105 || $self->ut_flag('secure')
106 || $self->ut_enum('handling', [ $self->handling_types ])
108 return $error if $error;
115 Creates a Net::FTP or Net::SFTP::Foreign object (according to the setting
116 of the 'secure' flag), connects to 'hostname', attempts to log in with
117 'username' and 'password', and changes the working directory to 'path'.
118 On success, returns the object. On failure, dies with an error message.
124 if ( $self->secure ) {
125 eval "use Net::SFTP::Foreign;";
129 user => $self->username,
130 password => $self->password,
131 more => ($DEBUG ? '-v' : ''),
133 autodie => 1, #we're doing this anyway
135 my $sftp = Net::SFTP::Foreign->new($self->hostname, %args);
136 $sftp->setcwd($self->path);
140 eval "use Net::FTP;";
145 Passive => 1,# optional?
147 my $ftp = Net::FTP->new($self->hostname, %args)
148 or die "connect to ".$self->hostname." failed: $@";
149 $ftp->login($self->username, $self->password)
150 or die "login to ".$self->username.'@'.$self->hostname." failed: $@";
151 $ftp->binary; #optional?
152 $ftp->cwd($self->path)
153 or ($self->path eq '/')
154 or die "cwd to ".$self->hostname.'/'.$self->path." failed: $@";
162 Returns a descriptive label for this target.
168 $self->targetnum . ': ' . $self->username . '@' . $self->hostname;
173 Returns a list of values for the "handling" field, corresponding to the
174 known ways to preprocess a file before uploading. Currently those are
175 implemented somewhat crudely in L<FS::Cron::upload>.
181 #'billco', #not implemented this way yet
189 L<FS::Record>, schema.html from the base documentation.