/** * Provide a singleton instance to simplify integration. If you prefer * to manage the lifecycle of the config object, then consider using * "probe()" or "new" instead. * * @return CA_Config_Curl */ public static function singleton() { if (!self::$_singleton) { global $CA_CONFIG; self::$_singleton = self::probe($CA_CONFIG ? $CA_CONFIG : array()); } return self::$_singleton; }
/** * @param string $remoteFile * @return array * (0 => resource, 1 => CA_Config_Curl) */ protected function createCurl($remoteFile) { $caConfig = CA_Config_Curl::probe(array('verify_peer' => (bool) CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'verifySSL'))); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $remoteFile); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); curl_setopt($ch, CURLOPT_VERBOSE, 0); if ($this->isRedirectSupported()) { curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); } if ($this->connectionTimeout !== NULL) { curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectionTimeout); } if (preg_match('/^https:/', $remoteFile) && $caConfig->isEnableSSL()) { curl_setopt_array($ch, $caConfig->toCurlOptions()); } return array($ch, $caConfig); }
/** * Download the remote zipfile. * * @return boolean Whether the download was successful. */ public function download() { require_once 'CA/Config/Curl.php'; $config = CRM_Core_Config::singleton(); $path = $config->extensionsDir . DIRECTORY_SEPARATOR . 'tmp'; $filename = $path . DIRECTORY_SEPARATOR . $this->key . '.zip'; if (!$this->downloadUrl) { CRM_Core_Error::fatal('Cannot install this extension - downloadUrl is not set!'); } // Download extension zip file ... if (!function_exists('curl_init')) { CRM_Core_Error::fatal('Cannot install this extension - curl is not installed!'); } if (preg_match('/^https:/', $this->downloadUrl) && !CA_Config_Curl::singleton()->isEnableSSL()) { CRM_Core_Error::fatal('Cannot install this extension - does not support SSL'); } //setting the curl parameters. $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->downloadUrl); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_VERBOSE, 1); if (preg_match('/^https:/', $this->downloadUrl)) { curl_setopt_array($ch, CA_Config_Curl::singleton()->toCurlOptions()); } //follow redirects curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); $fp = fopen($filename, "w"); if (!$fp) { CRM_Core_Session::setStatus(ts('Unable to write to %1.<br />Is the location writable?', array(1 => $filename))); return; } curl_setopt($ch, CURLOPT_FILE, $fp); curl_exec($ch); if (curl_errno($ch)) { CRM_Core_Error::debug(curl_error($ch)); CRM_Core_Error::debug(curl_errno($ch)); exit; CRM_Core_Session::setStatus(ts('Unable to download extension from %1. Error Message: %2', array(1 => $this->downloadUrl, 2 => curl_error($ch)))); return; } else { curl_close($ch); } fclose($fp); $this->tmpFile = $filename; return TRUE; }