/** * 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; }
/** * @return CRM_Utils_HttpClient */ public static function singleton() { if (!self::$singleton) { self::$singleton = new CRM_Utils_HttpClient(); } return self::$singleton; }
/** * 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; }
/** * 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); }
/** * Create default instance. * * @return CRM_Core_CommunityMessages */ public static function create() { return new CRM_Core_CommunityMessages(Civi::cache('community_messages'), CRM_Utils_HttpClient::singleton()); }
/** * 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); }
/** * Create default instance. * * @return CRM_Core_CommunityMessages */ public static function create() { return new CRM_Core_CommunityMessages(new CRM_Utils_Cache_SqlGroup(array('group' => 'community-messages', 'prefetch' => FALSE)), CRM_Utils_HttpClient::singleton()); }
/** * 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; } }
/** * 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; } }