You are here

function _piwik_cache in Piwik Web Analytics 8

Same name and namespace in other branches
  1. 5 piwik.module \_piwik_cache()
  2. 6.2 piwik.module \_piwik_cache()
  3. 6 piwik.module \_piwik_cache()
  4. 7.2 piwik.module \_piwik_cache()
  5. 7 piwik.module \_piwik_cache()

Download/Synchronize/Cache tracking code file locally.

Parameters

string $location: The full URL to the external javascript file.

bool $synchronize: Synchronize to local cache if remote file has changed.

Return value

mixed The path to the local javascript file on success, boolean FALSE on failure.

2 calls to _piwik_cache()
piwik_cron in ./piwik.module
Implements hook_cron().
piwik_page_attachments in ./piwik.module
Implements hook_page_attachments().

File

./piwik.module, line 484
Drupal Module: Piwik.

Code

function _piwik_cache($location, $synchronize = FALSE) {
  $path = 'public://piwik';
  $file_destination = $path . '/' . basename($location);
  if (!file_exists($file_destination) || $synchronize) {

    // Download the latest tracking code.
    try {
      $data = \Drupal::httpClient()
        ->get($location)
        ->getBody(TRUE);
      if (file_exists($file_destination)) {

        // Synchronize tracking code and and replace local file if outdated.
        $data_hash_local = Crypt::hashBase64(file_get_contents($file_destination));
        $data_hash_remote = Crypt::hashBase64($data);

        // Check that the files directory is writable.
        if ($data_hash_local != $data_hash_remote && file_prepare_directory($path)) {

          // Save updated tracking code file to disk.
          file_unmanaged_save_data($data, $file_destination, FILE_EXISTS_REPLACE);

          // Based on Drupal Core class AssetDumper.
          if (extension_loaded('zlib') && \Drupal::config('system.performance')
            ->get('js.gzip')) {
            file_unmanaged_save_data(gzencode($data, 9, FORCE_GZIP), $file_destination . '.gz', FILE_EXISTS_REPLACE);
          }
          \Drupal::logger('piwik')
            ->info('Locally cached tracking code file has been updated.');

          // Change query-strings on css/js files to enforce reload for all
          // users.
          _drupal_flush_css_js();
        }
      }
      else {

        // Check that the files directory is writable.
        if (file_prepare_directory($path, FILE_CREATE_DIRECTORY)) {

          // There is no need to flush JS here as core refreshes JS caches
          // automatically, if new files are added.
          file_unmanaged_save_data($data, $file_destination, FILE_EXISTS_REPLACE);

          // Based on Drupal Core class AssetDumper.
          if (extension_loaded('zlib') && \Drupal::config('system.performance')
            ->get('js.gzip')) {
            file_unmanaged_save_data(gzencode($data, 9, FORCE_GZIP), $file_destination . '.gz', FILE_EXISTS_REPLACE);
          }
          \Drupal::logger('piwik')
            ->info('Locally cached tracking code file has been saved.');

          // Return the local JS file path.
          return file_url_transform_relative(file_create_url($file_destination));
        }
      }
    } catch (RequestException $exception) {
      watchdog_exception('piwik', $exception);
    }
  }
  else {

    // Return the local JS file path.
    return file_url_transform_relative(file_create_url($file_destination));
  }
}