VaultPress::check_connection PHP Method

check_connection() public method

public check_connection ( $force_check = false )
    function check_connection($force_check = false)
    {
        $connection = $this->get_option('connection');
        if (!$force_check && !empty($connection)) {
            // already established a connection
            if ('ok' == $connection) {
                return true;
            }
            // only run the connection check every 5 minutes
            if (time() - (int) $connection < 300) {
                return false;
            }
        }
        // if we're running a connection test we don't want to run it a second time
        $connection_test = $this->get_option('connection_test');
        if ($connection_test) {
            return true;
        }
        // force update firewall settings
        $this->update_firewall();
        // initial connection test to server
        $this->update_option('connection_test', true);
        $this->delete_option('allow_forwarded_for');
        $host = !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : parse_url($this->site_url(), PHP_URL_HOST);
        $connect = $this->contact_service('test', array('host' => $host, 'uri' => $_SERVER['REQUEST_URI'], 'ssl' => is_ssl()));
        // we can't see the servers at all
        if (!$connect) {
            $this->update_option('connection', time());
            $this->update_option('connection_error_code', 0);
            $this->update_option('connection_error_message', sprintf(__('Cannot connect to the VaultPress servers. Please check that your host allows connecting to external sites and try again. If you&rsquo;re still having issues please <a href="%1$s">contact the VaultPress&nbsp;Safekeepers</a>.', 'vaultpress'), 'http://vaultpress.com/contact/'));
            $this->delete_option('connection_test');
            return false;
        }
        // VaultPress gave us a meaningful error
        if (!empty($connect['faultCode'])) {
            $this->update_option('connection', time());
            $this->update_option('connection_error_code', $connect['faultCode']);
            $this->update_option('connection_error_message', $connect['faultString']);
            $this->delete_option('connection_test');
            return false;
        }
        $this->update_plan_settings($connect);
        if (!empty($connect['signatures'])) {
            delete_option('_vp_signatures');
            add_option('_vp_signatures', maybe_unserialize($connect['signatures']), '', 'no');
        }
        // test connection between the site and the servers
        $connect = (string) $this->contact_service('test', array('type' => 'connect'));
        if ('ok' != $connect) {
            // still not working so see if we're behind a load balancer
            $this->update_option('allow_forwarded_for', true);
            $connect = (string) $this->contact_service('test', array('type' => 'firewall-off'));
            if ('ok' != $connect) {
                if ('error' == $connect) {
                    $this->update_option('connection_error_code', -1);
                    $this->update_option('connection_error_message', sprintf(__('The VaultPress servers cannot connect to your site. Please check that your site is visible over the Internet and there are no firewall or load balancer settings on your server that might be blocking the communication. If you&rsquo;re still having issues please <a href="%1$s">contact the VaultPress&nbsp;Safekeepers</a>.', 'vaultpress'), 'http://vaultpress.com/contact/'));
                } elseif (!empty($connect['faultCode'])) {
                    $this->update_option('connection_error_code', $connect['faultCode']);
                    $this->update_option('connection_error_message', $connect['faultString']);
                }
                $this->update_option('connection', time());
                $this->delete_option('connection_test');
                return false;
            }
        }
        // successful connection established
        $this->update_option('connection', 'ok');
        $this->delete_option('connection_error_code');
        $this->delete_option('connection_error_message');
        $this->delete_option('connection_test');
        return true;
    }
VaultPress