示例#1
0
 /**
  * Fetch the latest issue counts from Drupal.org and add them to the
  * database as a new sample.
  */
 public function fetchCounts(Application $app)
 {
     $guzzleClient = new \Guzzle\Http\Client();
     if (!empty($app['config']['guzzle']['userAgent'])) {
         $guzzleClient->setUserAgent($app['config']['guzzle']['userAgent'], true);
     }
     $this->repositoryUpdater->samples($guzzleClient, $app['config']['drupal_issues']);
     return '';
 }
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $app = $this->getApplication()->getContainer();
     $repositoryUpdater = new Updater($app['db']);
     $guzzleClient = new \Guzzle\Http\Client();
     if (!empty($app['config']['guzzle']['userAgent'])) {
         $guzzleClient->setUserAgent($app['config']['guzzle']['userAgent'], true);
     }
     $repositoryUpdater->samples($guzzleClient, $app['config']['drupal_issues']);
     // TODO output success message.
 }
示例#3
0
 /**
  * Prepares the HTTP client
  * @return \Guzzle\Http\Client
  */
 private function prepareHttpClient()
 {
     $guzzleOption = array('request.options' => array('verify' => !$this->options['turn_off_ssl_verification'], 'exceptions' => false));
     // Using api key
     if ($this->apiUser === null) {
         $guzzleOption['request.options']['headers'] = array('Authorization' => 'Bearer ' . $this->apiKey);
     }
     $client = new \Guzzle\Http\Client($this->url, $guzzleOption);
     $client->setUserAgent('sendgrid/' . $this->version . ';php');
     return $client;
 }
示例#4
0
 /**
  * Prepares the HTTP client
  *
  * @return \Guzzle\Http\Client
  */
 private function prepareHttpClient()
 {
     $guzzleOption = array('request.options' => array('verify' => !$this->options['turn_off_ssl_verification'], 'exceptions' => isset($this->options['enable_guzzle_exceptions']) && $this->options['enable_guzzle_exceptions'] == true));
     $guzzleOption['request.options']['headers'] = array('Authorization' => 'Bearer ' . $this->apiKey, 'Content-Type' => 'application/json', 'Accept' => '*/*');
     // Using http proxy
     if (isset($this->options['proxy'])) {
         $guzzleOption['request.options']['proxy'] = $this->options['proxy'];
     }
     $client = new \Guzzle\Http\Client($this->url, $guzzleOption);
     $client->setUserAgent('sendgrid/' . $this->version . ';php');
     return $client;
 }
 public function doCommand()
 {
     $url = $this->param('baseUrl');
     $opts = $this->param('options');
     $ua = $this->param('userAgent');
     $plugins = $this->param('plugins');
     $client = new \Guzzle\Http\Client($url, $opts);
     if (isset($ua)) {
         $client->setUserAgent($ua);
     }
     if (!empty($plugins)) {
         foreach ($plugins as $plugin) {
             $client->addSubscriber($plugin);
         }
     }
     // XXX: Could add event listeners here, too.
     return $client;
 }
示例#6
0
 /**
  * @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;
 }
示例#7
0
 /**
  * Check plugins’ release feeds and include any pending updates in the given UpdateModel
  *
  * @param UpdateModel $updateModel
  */
 public function checkPluginReleaseFeeds(UpdateModel $updateModel)
 {
     $userAgent = 'Craft/' . craft()->getVersion() . '.' . craft()->getBuild();
     foreach ($updateModel->plugins as $pluginUpdateModel) {
         // Only check plugins where the update status isn't already known from the ET response
         if ($pluginUpdateModel->status != PluginUpdateStatus::Unknown) {
             continue;
         }
         // Get the plugin and its feed URL
         $plugin = craft()->plugins->getPlugin($pluginUpdateModel->class);
         $feedUrl = $plugin->getReleaseFeedUrl();
         // Skip if the plugin doesn't have a feed URL
         if ($feedUrl === null) {
             continue;
         }
         // Make sure it's HTTPS
         if (strncmp($feedUrl, 'https://', 8) !== 0) {
             Craft::log('The “' . $plugin->getName() . '” plugin has a release feed URL, but it doesn’t begin with https://, so it’s getting skipped (' . $feedUrl . ').', LogLevel::Warning);
             continue;
         }
         try {
             // Fetch it
             $client = new \Guzzle\Http\Client();
             $client->setUserAgent($userAgent, true);
             $options = array('timeout' => 5, 'connect_timeout' => 2, 'allow_redirects' => true, 'verify' => false);
             $request = $client->get($feedUrl, null, $options);
             // Potentially long-running request, so close session to prevent session blocking on subsequent requests.
             craft()->session->close();
             $response = $request->send();
             if (!$response->isSuccessful()) {
                 Craft::log('Error in calling ' . $feedUrl . '. Response: ' . $response->getBody(), LogLevel::Warning);
                 continue;
             }
             $responseBody = $response->getBody();
             $releases = JsonHelper::decode($responseBody);
             if (!$releases) {
                 Craft::log('The “' . $plugin->getName() . "” plugin release feed didn’t come back as valid JSON:\n" . $responseBody, LogLevel::Warning);
                 continue;
             }
             $releaseModels = array();
             $releaseTimestamps = array();
             foreach ($releases as $release) {
                 // Validate ite info
                 $errors = array();
                 // Any missing required attributes?
                 $missingAttributes = array();
                 foreach (array('version', 'downloadUrl', 'date', 'notes') as $attribute) {
                     if (empty($release[$attribute])) {
                         $missingAttributes[] = $attribute;
                     }
                 }
                 if ($missingAttributes) {
                     $errors[] = 'Missing required attributes (' . implode(', ', $missingAttributes) . ')';
                 }
                 // Invalid URL?
                 if (strncmp($release['downloadUrl'], 'https://', 8) !== 0) {
                     $errors[] = 'Download URL doesn’t begin with https:// (' . $release['downloadUrl'] . ')';
                 }
                 // Invalid date?
                 $date = DateTime::createFromString($release['date']);
                 if (!$date) {
                     $errors[] = 'Invalid date (' . $release['date'] . ')';
                 }
                 // Validation complete. Were there any errors?
                 if ($errors) {
                     Craft::log('A “' . $plugin->getName() . "” release was skipped because it is invalid:\n - " . implode("\n - ", $errors), LogLevel::Warning);
                     continue;
                 }
                 // All good! Let's make sure it's a pending update
                 if (!version_compare($release['version'], $plugin->getVersion(), '>')) {
                     continue;
                 }
                 // Create the release note HTML
                 if (!is_array($release['notes'])) {
                     $release['notes'] = array_filter(preg_split('/[\\r\\n]+/', $release['notes']));
                 }
                 $notes = '';
                 $inList = false;
                 foreach ($release['notes'] as $line) {
                     // Escape any HTML
                     $line = htmlspecialchars($line, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
                     // Is this a heading?
                     if (preg_match('/^#\\s+(.+)/', $line, $match)) {
                         if ($inList) {
                             $notes .= "</ul>\n";
                             $inList = false;
                         }
                         $notes .= '<h3>' . $match[1] . "</h3>\n";
                     } else {
                         if (!$inList) {
                             $notes .= "<ul>\n";
                             $inList = true;
                         }
                         if (preg_match('/^\\[(\\w+)\\]\\s+(.+)/', $line, $match)) {
                             $class = strtolower($match[1]);
                             $line = $match[2];
                         } else {
                             $class = null;
                         }
                         // Parse Markdown code
                         $line = StringHelper::parseMarkdownLine($line);
                         $notes .= '<li' . ($class ? ' class="' . $class . '"' : '') . '>' . $line . "</li>\n";
                     }
                 }
                 if ($inList) {
                     $notes .= "</ul>\n";
                 }
                 $critical = !empty($release['critical']);
                 // Populate the release model
                 $releaseModel = new PluginNewReleaseModel();
                 $releaseModel->version = $release['version'];
                 $releaseModel->date = $date;
                 $releaseModel->localizedDate = $date->localeDate();
                 $releaseModel->notes = $notes;
                 $releaseModel->critical = $critical;
                 $releaseModel->manualDownloadEndpoint = $release['downloadUrl'];
                 $releaseModels[] = $releaseModel;
                 $releaseTimestamps[] = $date->getTimestamp();
                 if ($critical) {
                     $pluginUpdateModel->criticalUpdateAvailable = true;
                 }
             }
             if ($releaseModels) {
                 // Sort release models by timestamp
                 array_multisort($releaseTimestamps, SORT_DESC, $releaseModels);
                 $latestRelease = $releaseModels[0];
                 $pluginUpdateModel->displayName = $plugin->getName();
                 $pluginUpdateModel->localVersion = $plugin->getVersion();
                 $pluginUpdateModel->latestDate = $latestRelease->date;
                 $pluginUpdateModel->latestVersion = $latestRelease->version;
                 $pluginUpdateModel->manualDownloadEndpoint = $latestRelease->manualDownloadEndpoint;
                 $pluginUpdateModel->manualUpdateRequired = true;
                 $pluginUpdateModel->releases = $releaseModels;
                 $pluginUpdateModel->status = PluginUpdateStatus::UpdateAvailable;
             } else {
                 $pluginUpdateModel->status = PluginUpdateStatus::UpToDate;
             }
         } catch (\Exception $e) {
             Craft::log('There was a problem getting the update feed for “' . $plugin->getName() . '”, so it was skipped: ' . $e->getMessage(), LogLevel::Error);
             continue;
         }
     }
 }