Merge pull request #3643 from davecivicrm/CRM-14940a
[civicrm-core.git] / CRM / Utils / HttpClient.php
index 42a561bfc31d242dd39799260fc188d941b952f7..3148c536058f9e9538ae72670abd21aef5991d1c 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -34,7 +34,7 @@
  * want to deal with that so late in the 4.3 dev cycle.
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -50,10 +50,13 @@ class CRM_Utils_HttpClient {
   protected static $singleton;
 
   /**
-   * @var int
+   * @var int|NULL seconds; or NULL to use system default
    */
-  protected $timeout;
+  protected $connectionTimeout;
 
+  /**
+   * @return CRM_Utils_HttpClient
+   */
   public static function singleton() {
     if (!self::$singleton) {
       self::$singleton = new CRM_Utils_HttpClient();
@@ -61,8 +64,11 @@ class CRM_Utils_HttpClient {
     return self::$singleton;
   }
 
-  public function __construct($timeout = 10) {
-    $this->timeout = $timeout;
+  /**
+   * @param null $connectionTimeout
+   */
+  public function __construct($connectionTimeout = NULL) {
+    $this->connectionTimeout = $connectionTimeout;
   }
 
   /**
@@ -108,11 +114,49 @@ class CRM_Utils_HttpClient {
   /**
    * Send an HTTP GET for a remote resource
    *
-   * @param string $remoteFile URL of a .zip file
-   * @param string $localFile path at which to store the .zip file
+   * @param string $remoteFile URL of remote file
    * @return array array(0 => STATUS_OK|STATUS_DL_ERROR, 1 => string)
    */
   public function get($remoteFile) {
+    // Download extension zip file ...
+    if (!function_exists('curl_init')) {
+      // CRM-13805
+      CRM_Core_Session::setStatus(
+        ts('As a result, actions like retrieving the CiviCRM news feed will fail. Talk to your server administrator or hosting company to rectify this.'),
+        ts('Curl is not installed')
+      );
+      return array(self::STATUS_DL_ERROR, NULL);
+    }
+
+    list($ch, $caConfig) = $this->createCurl($remoteFile);
+
+    if (preg_match('/^https:/', $remoteFile) && !$caConfig->isEnableSSL()) {
+      //CRM_Core_Error::fatal('Cannot install this extension - does not support SSL');
+      return array(self::STATUS_DL_ERROR, NULL);
+    }
+
+    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+    $data = curl_exec($ch);
+    if (curl_errno($ch)) {
+      return array(self::STATUS_DL_ERROR, $data);
+    }
+    else {
+      curl_close($ch);
+    }
+
+    return array(self::STATUS_OK, $data);
+  }
+
+  /**
+   * Send an HTTP POST for a remote resource
+   *
+   * @param string $remoteFile URL of a .zip file
+   * @param $params
+   *
+   * @internal param string $localFile path at which to store the .zip file
+   * @return array array(0 => STATUS_OK|STATUS_DL_ERROR, 1 => string)
+   */
+  public function post($remoteFile, $params) {
     // Download extension zip file ...
     if (!function_exists('curl_init')) {
       //CRM_Core_Error::fatal('Cannot install this extension - curl is not installed!');
@@ -127,9 +171,12 @@ class CRM_Utils_HttpClient {
     }
 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+    curl_setopt($ch, CURLOPT_POST, true);
+    curl_setopt($ch, CURLOPT_POST,count($params));
+    curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
     $data = curl_exec($ch);
     if (curl_errno($ch)) {
-      return array(self::STATUS_DL_ERROR . $data);
+      return array(self::STATUS_DL_ERROR, $data);
     }
     else {
       curl_close($ch);
@@ -153,7 +200,12 @@ class CRM_Utils_HttpClient {
     curl_setopt($ch, CURLOPT_HEADER, FALSE);
     curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
     curl_setopt($ch, CURLOPT_VERBOSE, 0);
-    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
+    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());
     }
@@ -161,4 +213,11 @@ class CRM_Utils_HttpClient {
     return array($ch, $caConfig);
   }
 
+  /**
+   * @return bool
+   */
+  public function isRedirectSupported() {
+    return (ini_get('open_basedir') == '') && (ini_get('safe_mode') == 'Off' || ini_get('safe_mode') === FALSE);
+  }
+
 }