/** * Connect to FTP server over Implicit SSL/TLS * * @since 3.0 * @throws Exception * @return \WC_Customer_Order_CSV_Export_Method_FTP_Implicit_SSL */ public function __construct() { parent::__construct(); // set host/initial path $this->url = "ftps://{$this->server}/{$this->path}"; // setup connection $this->curl_handle = curl_init(); // check for successful connection if (!$this->curl_handle) { throw new Exception(__('Could not initialize cURL.', 'woocommerce-customer-order-csv-export')); } // connection options $options = array(CURLOPT_USERPWD => $this->username . ':' . $this->password, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_FTP_SSL => CURLFTPSSL_ALL, CURLOPT_FTPSSLAUTH => CURLFTPAUTH_DEFAULT, CURLOPT_UPLOAD => true, CURLOPT_PORT => $this->port, CURLOPT_TIMEOUT => $this->timeout); // cURL FTP enables passive mode by default, so disable it by enabling the PORT command if (!$this->passive_mode) { $options[CURLOPT_FTPPORT] = '-'; } // allow modification of cURL options $options = apply_filters('wc_customer_order_csv_export_ftp_over_implicit_curl_options', $options, $this); // set connection options, use foreach so useful errors can be caught instead of a generic "cannot set options" error with curl_setopt_array() foreach ($options as $option_name => $option_value) { if (!curl_setopt($this->curl_handle, $option_name, $option_value)) { throw new Exception(sprintf(__('Could not set cURL option: %s', 'woocommerce-customer-order-csv-export'), $option_name)); } } }
/** * Connect to FTP server and authenticate via password * * @since 3.0 * @throws Exception * @return \WC_Customer_Order_CSV_Export_Method_FTP */ public function __construct() { parent::__construct(); // Handle errors from ftp_* functions that throw warnings for things like invalid username / password, failed directory changes, and failed data connections set_error_handler(array($this, 'handle_errors')); // setup connection $this->link = null; if ('ftps' == $this->security && function_exists('ftp_ssl_connect')) { $this->link = ftp_ssl_connect($this->server, $this->port, $this->timeout); } elseif ('ftps' !== $this->security) { $this->link = ftp_connect($this->server, $this->port, $this->timeout); } // check for successful connection if (!$this->link) { throw new Exception(__("Could not connect via FTP to {$this->server} on port {$this->port}, check server address and port.", WC_Customer_Order_CSV_Export::TEXT_DOMAIN)); } // attempt to login, note that incorrect credentials throws an E_WARNING PHP error if (!ftp_login($this->link, $this->username, $this->password)) { throw new Exception(__("Could not authenticate via FTP with username {$this->username} and password <hidden>. Check username and password.", WC_Customer_Order_CSV_Export::TEXT_DOMAIN)); } // set passive mode if enabled if ($this->passive_mode) { // check for success if (!ftp_pasv($this->link, true)) { throw new Exception(__('Could not set passive mode', WC_Customer_Order_CSV_Export::TEXT_DOMAIN)); } } // change directories if initial path is populated, note that failing to change directory throws an E_WARNING PHP error if ($this->path) { // check for success if (!ftp_chdir($this->link, '/' . $this->path)) { throw new Exception(__("Could not change directory to {$this->path} - check path exists.", WC_Customer_Order_CSV_Export::TEXT_DOMAIN)); } } }
/** * Connect to SSH server, authenticate via password, and set up SFTP link * * @since 3.0 * @throws Exception - ssh2 extension not installed, failed SSH / SFTP connection, failed authentication * @return \WC_Customer_Order_CSV_Export_Method_SFTP */ public function __construct() { parent::__construct(); // check if ssh2 extension is installed if (!function_exists('ssh2_connect')) { throw new Exception(__('SSH2 Extension is not installed, cannot connect via SFTP.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN)); } // setup connection $this->ssh_link = ssh2_connect($this->server, $this->port); // check for successful connection if (!$this->ssh_link) { throw new Exception(__("Could not connect via SSH to {$this->server} on port {$this->port}, check server address and port.", WC_Customer_Order_CSV_Export::TEXT_DOMAIN)); } // authenticate via password and check for successful authentication if (!ssh2_auth_password($this->ssh_link, $this->username, $this->password)) { throw new Exception(__("Could not authenticate via SSH with username {$this->username} and password. Check username and password.", WC_Customer_Order_CSV_Export::TEXT_DOMAIN)); } // setup SFTP link $this->sftp_link = ssh2_sftp($this->ssh_link); // check for successful SFTP link if (!$this->sftp_link) { throw new Exception(__('Could not setup SFTP link', WC_Customer_Order_CSV_Export::TEXT_DOMAIN)); } }