/** * @throws EtException|\Exception * @return EtModel|null */ public function phoneHome() { try { $missingLicenseKey = empty($this->_model->licenseKey); // No craft/config/license.key file and we can't write to the config folder. Don't even make the call home. if ($missingLicenseKey && !$this->_isConfigFolderWritable()) { throw new EtException('Craft needs to be able to write to your “craft/config” folder and it can’t.', 10001); } if (!craft()->cache->get('etConnectFailure')) { $data = JsonHelper::encode($this->_model->getAttributes(null, true)); $client = new \Guzzle\Http\Client(); $client->setUserAgent($this->_userAgent, true); $options = array('timeout' => $this->getTimeout(), 'connect_timeout' => $this->getConnectTimeout(), 'allow_redirects' => $this->getAllowRedirects()); $request = $client->post($this->_endpoint, $options); $request->setBody($data, 'application/json'); $response = $request->send(); if ($response->isSuccessful()) { // Clear the connection failure cached item if it exists. if (craft()->cache->get('etConnectFailure')) { craft()->cache->delete('etConnectFailure'); } if ($this->_destinationFileName) { $body = $response->getBody(); // Make sure we're at the beginning of the stream. $body->rewind(); // Write it out to the file IOHelper::writeToFile($this->_destinationFileName, $body->getStream(), true); // Close the stream. $body->close(); return IOHelper::getFileName($this->_destinationFileName); } $etModel = craft()->et->decodeEtModel($response->getBody()); if ($etModel) { if ($missingLicenseKey && !empty($etModel->licenseKey)) { $this->_setLicenseKey($etModel->licenseKey); } // Cache the license key status and which edition it has craft()->cache->set('licenseKeyStatus', $etModel->licenseKeyStatus); craft()->cache->set('licensedEdition', $etModel->licensedEdition); craft()->cache->set('editionTestableDomain@' . craft()->request->getHostName(), $etModel->editionTestableDomain ? 1 : 0); if ($etModel->licenseKeyStatus == LicenseKeyStatus::MismatchedDomain) { craft()->cache->set('licensedDomain', $etModel->licensedDomain); } return $etModel; } else { Craft::log('Error in calling ' . $this->_endpoint . ' Response: ' . $response->getBody(), LogLevel::Warning); if (craft()->cache->get('etConnectFailure')) { // There was an error, but at least we connected. craft()->cache->delete('etConnectFailure'); } } } else { Craft::log('Error in calling ' . $this->_endpoint . ' Response: ' . $response->getBody(), LogLevel::Warning); if (craft()->cache->get('etConnectFailure')) { // There was an error, but at least we connected. craft()->cache->delete('etConnectFailure'); } } } } catch (EtException $e) { Craft::log('Error in ' . __METHOD__ . '. Message: ' . $e->getMessage(), LogLevel::Error); if (craft()->cache->get('etConnectFailure')) { // There was an error, but at least we connected. craft()->cache->delete('etConnectFailure'); } throw $e; } catch (\Exception $e) { Craft::log('Error in ' . __METHOD__ . '. Message: ' . $e->getMessage(), LogLevel::Error); // Cache the failure for 5 minutes so we don't try again. craft()->cache->set('etConnectFailure', true, 300); } return null; }
/** * Creates a new EtModel with provided JSON, and returns it if it's valid. * * @param array $attributes * * @return EtModel|null */ public function decodeEtModel($attributes) { if ($attributes) { $attributes = JsonHelper::decode($attributes); if (is_array($attributes)) { $etModel = new EtModel($attributes); // Make sure it's valid. (At a minimum, localBuild and localVersion // should be set.) if ($etModel->validate()) { return $etModel; } } } }