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 $config = CRM_Core_Config
::singleton();
28 $cache = new CRM_Utils_Cache_Arraycache(array());
31 $cache = new CRM_Utils_Cache_SqlGroup(array(
32 'group' => 'CiviCxnHttp',
37 self
::$singleton = new CRM_Cxn_CiviCxnHttp($cache);
39 return self
::$singleton;
43 * @param CRM_Utils_Cache_Interface|NULL $cache
44 * The cache data store.
46 public function __construct($cache) {
47 $this->cache
= $cache;
54 * @param array $headers
55 * Array of headers (e.g. "Content-type" => "text/plain").
57 * array($headers, $blob, $code)
59 public function send($verb, $url, $blob, $headers = array()) {
60 $lowVerb = strtolower($verb);
62 if ($lowVerb === 'get' && $this->cache
) {
63 $cachePath = 'get/' . md5($url);
64 $cacheLine = $this->cache
->get($cachePath);
65 if ($cacheLine && $cacheLine['expires'] > CRM_Utils_Time
::getTimeRaw()) {
66 return $cacheLine['data'];
70 $result = parent
::send($verb, $url, $blob, $headers);
72 if ($lowVerb === 'get' && $this->cache
) {
73 $expires = CRM_Utils_Http
::parseExpiration($result[0]);
74 if ($expires !== NULL) {
75 $cachePath = 'get/' . md5($url);
78 'expires' => $expires,
81 $this->cache
->set($cachePath, $cacheLine);
88 protected function createStreamOpts($verb, $url, $blob, $headers) {
89 $result = parent
::createStreamOpts($verb, $url, $blob, $headers);
91 $caConfig = CA_Config_Stream
::probe(array(
92 'verify_peer' => (bool) CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'verifySSL', NULL, TRUE),
94 if ($caConfig->isEnableSSL()) {
95 $result['ssl'] = $caConfig->toStreamOptions();
97 if (!$caConfig->isEnableSSL() && preg_match('/^https:/', $url)) {
98 CRM_Core_Error
::fatal('Cannot fetch document - system does not support SSL');