Commit | Line | Data |
---|---|---|
5063e355 TO |
1 | <?php |
2 | ||
3 | /** | |
4 | * Class CRM_Cxn_CiviCxnHttp | |
5 | * | |
6 | * This extends the PhpHttp client used by CiviConnect and adds: | |
7 | * - Force-cache support for GET requests | |
8 | * - Compliance with SSL policy | |
9 | */ | |
10 | class CRM_Cxn_CiviCxnHttp extends \Civi\Cxn\Rpc\Http\PhpHttp { | |
11 | ||
12 | protected static $singleton = NULL; | |
13 | ||
14 | /** | |
15 | * @var CRM_Utils_Cache_Interface|null | |
16 | */ | |
17 | protected $cache; | |
18 | ||
19 | /** | |
20 | * @param bool $fresh | |
21 | * @return CRM_Cxn_CiviCxnHttp | |
22 | */ | |
23 | public static function singleton($fresh = FALSE) { | |
24 | if (self::$singleton === NULL || $fresh) { | |
25 | $config = CRM_Core_Config::singleton(); | |
26 | ||
27 | if ($config->debug) { | |
28 | $cache = new CRM_Utils_Cache_Arraycache(array()); | |
29 | } | |
30 | else { | |
31 | $cache = new CRM_Utils_Cache_SqlGroup(array( | |
32 | 'group' => 'CiviCxnHttp', | |
33 | 'prefetch' => FALSE, | |
34 | )); | |
35 | } | |
36 | ||
37 | self::$singleton = new CRM_Cxn_CiviCxnHttp($cache); | |
38 | } | |
39 | return self::$singleton; | |
40 | } | |
41 | ||
42 | /** | |
43 | * @param CRM_Utils_Cache_Interface|NULL $cache | |
44 | * The cache data store. | |
45 | */ | |
46 | public function __construct($cache) { | |
47 | $this->cache = $cache; | |
48 | } | |
49 | ||
50 | /** | |
51 | * @param string $verb | |
52 | * @param string $url | |
53 | * @param string $blob | |
54 | * @param array $headers | |
55 | * Array of headers (e.g. "Content-type" => "text/plain"). | |
56 | * @return array | |
57 | * array($headers, $blob, $code) | |
58 | */ | |
59 | public function send($verb, $url, $blob, $headers = array()) { | |
60 | $lowVerb = strtolower($verb); | |
61 | ||
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']; | |
67 | } | |
68 | } | |
69 | ||
70 | $result = parent::send($verb, $url, $blob, $headers); | |
71 | ||
72 | if ($lowVerb === 'get' && $this->cache) { | |
73 | $expires = CRM_Utils_Http::parseExpiration($result[0]); | |
74 | if ($expires !== NULL) { | |
75 | $cachePath = 'get/' . md5($url); | |
76 | $cacheLine = array( | |
77 | 'url' => $url, | |
78 | 'expires' => $expires, | |
79 | 'data' => $result, | |
80 | ); | |
81 | $this->cache->set($cachePath, $cacheLine); | |
82 | } | |
83 | } | |
84 | ||
85 | return $result; | |
86 | } | |
87 | ||
88 | protected function createStreamOpts($verb, $url, $blob, $headers) { | |
89 | $result = parent::createStreamOpts($verb, $url, $blob, $headers); | |
90 | ||
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), | |
93 | )); | |
94 | if ($caConfig->isEnableSSL()) { | |
95 | $result['ssl'] = $caConfig->toStreamOptions(); | |
96 | } | |
97 | if (!$caConfig->isEnableSSL() && preg_match('/^https:/', $url)) { | |
98 | CRM_Core_Error::fatal('Cannot fetch document - system does not support SSL'); | |
99 | } | |
100 | ||
101 | return $result; | |
102 | } | |
103 | ||
104 | } |