4 * Class CRM_Cxn_CiviCxnHttp
6 * This extends the PhpHttp client used by CiviConnect and adds:
7 * - Force-cache support for GET requests
8 * - Compliance with SSL policy
10 class CRM_Cxn_CiviCxnHttp
extends \Civi\Cxn\Rpc\Http\PhpHttp
{
12 protected static $singleton = NULL;
15 * @var CRM_Utils_Cache_Interface|null
21 * @return CRM_Cxn_CiviCxnHttp
23 public static function singleton($fresh = FALSE) {
24 if (self
::$singleton === NULL ||
$fresh) {
25 $cache = CRM_Utils_Cache
::create(array(
26 'name' => 'CiviCxnHttp',
27 'type' => Civi
::settings()->get('debug_enabled') ?
'ArrayCache' : array('SqlGroup', 'ArrayCache'),
31 self
::$singleton = new CRM_Cxn_CiviCxnHttp($cache);
33 return self
::$singleton;
37 * @param CRM_Utils_Cache_Interface|NULL $cache
38 * The cache data store.
40 public function __construct($cache) {
41 $this->cache
= $cache;
48 * @param array $headers
49 * Array of headers (e.g. "Content-type" => "text/plain").
51 * array($headers, $blob, $code)
53 public function send($verb, $url, $blob, $headers = array()) {
54 $lowVerb = strtolower($verb);
56 if ($lowVerb === 'get' && $this->cache
) {
57 $cachePath = 'get/' . md5($url);
58 $cacheLine = $this->cache
->get($cachePath);
59 if ($cacheLine && $cacheLine['expires'] > CRM_Utils_Time
::getTimeRaw()) {
60 return $cacheLine['data'];
64 $result = parent
::send($verb, $url, $blob, $headers);
66 if ($lowVerb === 'get' && $this->cache
) {
67 $expires = CRM_Utils_Http
::parseExpiration($result[0]);
68 if ($expires !== NULL) {
69 $cachePath = 'get/' . md5($url);
72 'expires' => $expires,
75 $this->cache
->set($cachePath, $cacheLine);
82 protected function createStreamOpts($verb, $url, $blob, $headers) {
83 $result = parent
::createStreamOpts($verb, $url, $blob, $headers);
85 $caConfig = CA_Config_Stream
::probe(array(
86 'verify_peer' => (bool) Civi
::settings()->get('verifySSL'),
88 if ($caConfig->isEnableSSL()) {
89 $result['ssl'] = $caConfig->toStreamOptions();
91 if (!$caConfig->isEnableSSL() && preg_match('/^https:/', $url)) {
92 CRM_Core_Error
::fatal('Cannot fetch document - system does not support SSL');