CiviCxnHttp.php 2.68 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php

/**
 * Class CRM_Cxn_CiviCxnHttp
 *
 * This extends the PhpHttp client used by CiviConnect and adds:
 *  - Force-cache support for GET requests
 *  - Compliance with SSL policy
 */
class CRM_Cxn_CiviCxnHttp extends \Civi\Cxn\Rpc\Http\PhpHttp {

  protected static $singleton = NULL;

  /**
   * @var CRM_Utils_Cache_Interface|null
   */
  protected $cache;

  /**
   * @param bool $fresh
   * @return CRM_Cxn_CiviCxnHttp
   */
  public static function singleton($fresh = FALSE) {
    if (self::$singleton === NULL || $fresh) {
      $config = CRM_Core_Config::singleton();

      if ($config->debug) {
        $cache = new CRM_Utils_Cache_Arraycache(array());
      }
      else {
        $cache = new CRM_Utils_Cache_SqlGroup(array(
          'group' => 'CiviCxnHttp',
          'prefetch' => FALSE,
        ));
      }

      self::$singleton = new CRM_Cxn_CiviCxnHttp($cache);
    }
    return self::$singleton;
  }

  /**
   * @param CRM_Utils_Cache_Interface|NULL $cache
   *   The cache data store.
   */
  public function __construct($cache) {
    $this->cache = $cache;
  }

  /**
   * @param string $verb
   * @param string $url
   * @param string $blob
   * @param array $headers
   *   Array of headers (e.g. "Content-type" => "text/plain").
   * @return array
   *   array($headers, $blob, $code)
   */
  public function send($verb, $url, $blob, $headers = array()) {
    $lowVerb = strtolower($verb);

    if ($lowVerb === 'get' && $this->cache) {
      $cachePath = 'get/' . md5($url);
      $cacheLine = $this->cache->get($cachePath);
      if ($cacheLine && $cacheLine['expires'] > CRM_Utils_Time::getTimeRaw()) {
        return $cacheLine['data'];
      }
    }

    $result = parent::send($verb, $url, $blob, $headers);

    if ($lowVerb === 'get' && $this->cache) {
      $expires = CRM_Utils_Http::parseExpiration($result[0]);
      if ($expires !== NULL) {
        $cachePath = 'get/' . md5($url);
        $cacheLine = array(
          'url' => $url,
          'expires' => $expires,
          'data' => $result,
        );
        $this->cache->set($cachePath, $cacheLine);
      }
    }

    return $result;
  }

  protected function createStreamOpts($verb, $url, $blob, $headers) {
    $result = parent::createStreamOpts($verb, $url, $blob, $headers);

    $caConfig = CA_Config_Stream::probe(array(
92
      'verify_peer' => (bool) CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'verifySSL'),
93
94
95
96
97
98
99
100
101
102
103
104
    ));
    if ($caConfig->isEnableSSL()) {
      $result['ssl'] = $caConfig->toStreamOptions();
    }
    if (!$caConfig->isEnableSSL() && preg_match('/^https:/', $url)) {
      CRM_Core_Error::fatal('Cannot fetch document - system does not support SSL');
    }

    return $result;
  }

}