Ejemplo n.º 1
0
/**
 * Register with a remote application and create a new connection.
 *
 * One should generally identify an application using the app_guid.
 * However, if you need to test a new/experimental application, then
 * disable CIVICRM_CXN_CA and specify app_meta_url.
 *
 * @param array $params
 *   Array with keys:
 *   - app_guid: The unique identifer of the target application.
 *   - app_meta_url: The URL for the application's metadata.
 * @return array
 * @throws Exception
 */
function civicrm_api3_cxn_register($params)
{
    if (!empty($params['app_meta_url'])) {
        list($status, $json) = CRM_Utils_HttpClient::singleton()->get($params['app_meta_url']);
        if (CRM_Utils_HttpClient::STATUS_OK != $status) {
            throw new API_Exception("Failed to download appMeta. (Bad HTTP response)");
        }
        $appMeta = json_decode($json, TRUE);
        if (empty($appMeta)) {
            throw new API_Exception("Failed to download appMeta. (Malformed)");
        }
    } elseif (!empty($params['app_guid'])) {
        $appMeta = civicrm_api3('CxnApp', 'getsingle', array('appId' => $params['app_guid']));
    }
    if (empty($appMeta) || !is_array($appMeta)) {
        throw new API_Exception("Missing expected parameter: app_guid");
    }
    \Civi\Cxn\Rpc\AppMeta::validate($appMeta);
    try {
        /** @var \Civi\Cxn\Rpc\RegistrationClient $client */
        $client = \Civi::service('cxn_reg_client');
        list($cxnId, $result) = $client->register($appMeta);
        CRM_Cxn_BAO_Cxn::updateAppMeta($appMeta);
    } catch (Exception $e) {
        CRM_Cxn_BAO_Cxn::updateAppMeta($appMeta);
        throw $e;
    }
    return $result;
}
Ejemplo n.º 2
0
 /**
  * @return CRM_Utils_HttpClient
  */
 public static function singleton()
 {
     if (!self::$singleton) {
         self::$singleton = new CRM_Utils_HttpClient();
     }
     return self::$singleton;
 }
 function testGetHttps_invalid_noVerify()
 {
     $result = civicrm_api('Setting', 'create', array('version' => 3, 'verifySSL' => FALSE));
     $this->assertAPISuccess($result);
     list($status, $data) = $this->client->get(self::SELF_SIGNED_HTTPS_URL);
     $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
     $this->assertRegExp(self::SELF_SIGNED_HTTPS_REGEX, $data);
 }
Ejemplo n.º 4
0
 /**
  * Download document from URL and parse as JSON.
  *
  * @return NULL|array
  *   parsed JSON
  */
 public function fetchDocument()
 {
     list($status, $json) = $this->client->get($this->getRenderedUrl());
     if ($status != CRM_Utils_HttpClient::STATUS_OK || empty($json)) {
         return NULL;
     }
     $doc = json_decode($json, TRUE);
     if (empty($doc) || json_last_error() != JSON_ERROR_NONE) {
         return NULL;
     }
     return $doc;
 }
Ejemplo n.º 5
0
 /**
  * Connects to public server and grabs the list of publically available
  * extensions.
  *
  * @return string
  */
 private function grabRemoteJson()
 {
     ini_set('default_socket_timeout', CRM_Utils_VersionCheck::CHECK_TIMEOUT);
     set_error_handler(array('CRM_Extension_Browser', 'downloadError'));
     if (!ini_get('allow_url_fopen')) {
         ini_set('allow_url_fopen', 1);
     }
     if (FALSE === $this->getRepositoryUrl()) {
         // don't check if the user has configured civi not to check an external
         // url for extensions. See CRM-10575.
         CRM_Core_Session::setStatus(ts('Not checking remote URL for extensions since ext_repo_url is set to false.'), ts('Check Settings'), 'alert');
         return array();
     }
     $filename = $this->cacheDir . DIRECTORY_SEPARATOR . self::CACHE_JSON_FILE;
     $url = $this->getRepositoryUrl() . $this->indexPath;
     $status = CRM_Utils_HttpClient::singleton()->fetch($url, $filename);
     if ($status !== CRM_Utils_HttpClient::STATUS_OK) {
         CRM_Core_Session::setStatus(ts('The CiviCRM public extensions directory at %1 could not be contacted - please check your webserver can make external HTTP requests or contact CiviCRM team on <a href="http://forum.civicrm.org/">CiviCRM forum</a>.<br />', array(1 => $this->getRepositoryUrl())), ts('Connection Error'), 'error');
     } else {
         // Don't call grabCachedJson here, that would risk infinite recursion
         $json = file_get_contents($filename);
     }
     // CRM-13141 There may not be any compatible extensions available for the requested CiviCRM version + CMS. If so, $extdir is empty so just return a notification.
     if (empty($json)) {
         $config = CRM_Core_Config::singleton();
         CRM_Core_Session::setStatus(ts('There are currently no extensions on the CiviCRM public extension directory which are compatible with version %2 (<a href="%1">requested extensions from here</a>). If you want to install an extension which is not marked as compatible, you may be able to <a href="%3">download and install extensions manually</a> (depending on access to your web server).<br />', array(1 => $this->getRepositoryUrl(), 2 => $config->civiVersion, 3 => 'http://wiki.civicrm.org/confluence/display/CRMDOC/Extensions')), ts('No Extensions Available for this Version'), 'info');
     }
     ini_restore('allow_url_fopen');
     ini_restore('default_socket_timeout');
     restore_error_handler();
     return $json;
 }
Ejemplo n.º 6
0
 /**
  * Submit a request with an API key that exists but does not correspond to
  * a real user. Submit in "?q=civicrm/$entity/$action" notation
  */
 public function testNotCMSUser_q()
 {
     $client = CRM_Utils_HttpClient::singleton();
     //Create contact with api_key
     $test_key = "testing1234";
     $contactParams = array("api_key" => $test_key, "contact_type" => "Individual", "first_name" => "RestTester1");
     $contact = $this->webtest_civicrm_api("Contact", "create", $contactParams);
     $this->nocms_contact_id = $contact["id"];
     // The key associates with a real contact but not a real user
     $params = array("q" => "civicrm/contact/get", "key" => $this->settings->siteKey, "json" => "1", "api_key" => $test_key);
     list($status, $data) = $client->post($this->url, $params);
     $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
     $result = json_decode($data, TRUE);
     $this->assertNotNull($result);
     $this->assertAPIErrorCode($result, 1);
 }
Ejemplo n.º 7
0
 /**
  * Connects to public server and grabs the list of publicly available
  * extensions.
  *
  * @return string
  * @throws \CRM_Extension_Exception
  */
 private function grabRemoteJson()
 {
     ini_set('default_socket_timeout', self::CHECK_TIMEOUT);
     set_error_handler(array('CRM_Extension_Browser', 'downloadError'));
     if (!ini_get('allow_url_fopen')) {
         ini_set('allow_url_fopen', 1);
     }
     if (FALSE === $this->getRepositoryUrl()) {
         // don't check if the user has configured civi not to check an external
         // url for extensions. See CRM-10575.
         return array();
     }
     $filename = $this->cacheDir . DIRECTORY_SEPARATOR . self::CACHE_JSON_FILE;
     $url = $this->getRepositoryUrl() . $this->indexPath;
     $status = CRM_Utils_HttpClient::singleton()->fetch($url, $filename);
     ini_restore('allow_url_fopen');
     ini_restore('default_socket_timeout');
     restore_error_handler();
     if ($status !== CRM_Utils_HttpClient::STATUS_OK) {
         throw new CRM_Extension_Exception(ts('The CiviCRM public extensions directory at %1 could not be contacted - please check your webserver can make external HTTP requests or contact CiviCRM team on <a href="http://forum.civicrm.org/">CiviCRM forum</a>.', array(1 => $this->getRepositoryUrl())), 'connection_error');
     }
     // Don't call grabCachedJson here, that would risk infinite recursion
     return file_get_contents($filename);
 }
Ejemplo n.º 8
0
 /**
  * Get html and cache results.
  *
  * @param $url
  *
  * @return array|NULL
  *   array of gettingStarted items; or NULL if not available
  */
 public function _getHtml($url)
 {
     $httpClient = new CRM_Utils_HttpClient(self::CHECK_TIMEOUT);
     list($status, $html) = $httpClient->get($url);
     if ($status !== CRM_Utils_HttpClient::STATUS_OK) {
         return NULL;
     }
     $tokensList = CRM_Utils_Token::getTokens($html);
     $this->replaceLinkToken($tokensList, $html);
     if ($html) {
         CRM_Core_BAO_Cache::setItem($html, 'dashboard', 'gettingStarted');
     }
     return $html;
 }
Ejemplo n.º 9
0
 /**
  * Parse rss feed and cache results.
  *
  * @param $url
  *
  * @return array|NULL
  *   array of blog items; or NULL if not available
  */
 public function _getFeed($url)
 {
     $httpClient = new CRM_Utils_HttpClient(self::CHECK_TIMEOUT);
     list($status, $rawFeed) = $httpClient->get($url);
     if ($status !== CRM_Utils_HttpClient::STATUS_OK) {
         return NULL;
     }
     $feed = @simplexml_load_string($rawFeed);
     $blog = array();
     if ($feed && !empty($feed->channel->item)) {
         foreach ($feed->channel->item as $item) {
             $item = (array) $item;
             // Clean up description - remove tags that would break dashboard layout
             $description = preg_replace('#<h[1-3][^>]*>(.+?)</h[1-3][^>]*>#s', '<h4>$1</h4>', $item['description']);
             $item['description'] = strip_tags($description, "<a><p><h4><h5><h6><b><i><em><strong><ol><ul><li><dd><dt><code><pre><br/>");
             $blog[] = $item;
         }
         if ($blog) {
             CRM_Core_BAO_Cache::setItem($blog, 'dashboard', 'blog');
         }
     }
     return $blog;
 }
Ejemplo n.º 10
0
 /**
  * Download the remote zipfile.
  *
  * @param string $remoteFile
  *   URL of a .zip file.
  * @param string $localFile
  *   Path at which to store the .zip file.
  * @return bool
  *   Whether the download was successful.
  */
 public function fetch($remoteFile, $localFile)
 {
     $result = CRM_Utils_HttpClient::singleton()->fetch($remoteFile, $localFile);
     switch ($result) {
         case CRM_Utils_HttpClient::STATUS_OK:
             return TRUE;
         default:
             return FALSE;
     }
 }
Ejemplo n.º 11
0
 /**
  * Parse a single rss feed.
  *
  * @param $url
  *
  * @return array|NULL
  *   array of blog items; or NULL if not available
  */
 protected function getFeed($url)
 {
     $httpClient = new CRM_Utils_HttpClient(self::CHECK_TIMEOUT);
     list($status, $rawFeed) = $httpClient->get($url);
     if ($status !== CRM_Utils_HttpClient::STATUS_OK) {
         return NULL;
     }
     return @simplexml_load_string($rawFeed);
 }
Ejemplo n.º 12
0
 /**
  * Given the key, retrieves the info XML from a remote server
  * and stores locally, returning the contents.
  *
  * @access public
  *
  * @param string $key extension key
  * @param boolean $cached whether to use cached data
  *
  * @return contents of info.xml, or null if info.xml cannot be retrieved or parsed
  */
 private function grabRemoteInfoFile($key, $cached = FALSE)
 {
     $filename = $this->cacheDir . DIRECTORY_SEPARATOR . $key . '.xml';
     $url = $this->getRepositoryUrl() . '/' . $key . '.xml';
     if (!$cached || !file_exists($filename)) {
         $fetchStatus = CRM_Utils_HttpClient::singleton()->fetch($url, $filename);
         if ($fetchStatus != CRM_Utils_HttpClient::STATUS_OK) {
             return NULL;
         }
     }
     if (file_exists($filename)) {
         $contents = file_get_contents($filename);
         //parse just in case
         $check = simplexml_load_string($contents);
         if (!$check) {
             foreach (libxml_get_errors() as $error) {
                 CRM_Core_Error::debug('xmlError', $error);
             }
             return;
         }
         return $contents;
     }
 }