You are here

function responsive_favicons_config_page_submit in Responsive Favicons 7

Implements additional submit logic for responsive_favicons_settings_form().

1 string reference to 'responsive_favicons_config_page_submit'
responsive_favicons_config_page in ./responsive_favicons.admin.inc
Menu callback; Provide the administration overview page.

File

./responsive_favicons.admin.inc, line 46
Admin page callbacks for the responsive_favicons module.

Code

function responsive_favicons_config_page_submit($form, &$form_state) {

  // We want to save tags as an array.
  if (isset($form_state['values']['responsive_favicons_tags'])) {
    $tags = explode(PHP_EOL, $form_state['values']['responsive_favicons_tags']);
    $tags = array_filter($tags);
    foreach ($tags as $pos => $tag) {
      $tags[$pos] = trim($tag);
    }
    variable_set('responsive_favicons_tags', $tags);
    unset($form_state['values']['responsive_favicons_tags']);
  }

  // Remove trailing slash on responsive_favicons_path.
  $form_state['values']['responsive_favicons_path'] = rtrim($form_state['values']['responsive_favicons_path'], '/');

  // Attempt the upload and extraction of the zip file. This code is largely
  // based on the code in Drupal core.
  //
  // @see update_manager_install_form_submit().
  if ($_FILES['files']['name']['responsive_favicons_upload']) {
    $validators = array(
      'file_validate_extensions' => array(
        archiver_get_extensions(),
      ),
    );
    $field = 'responsive_favicons_upload';
    if (!($finfo = file_save_upload($field, $validators, NULL, FILE_EXISTS_REPLACE))) {

      // Failed to upload the file. file_save_upload() calls form_set_error() on
      // failure.
      return;
    }
    $local_cache = $finfo->uri;
    $directory = _responsive_favicons_extract_directory();
    try {
      $archive = _responsive_favicons_archive_extract($local_cache, $directory);
    } catch (Exception $e) {
      form_set_error($field, $e
        ->getMessage());
      return;
    }
    $files = $archive
      ->listContents();
    if (!$files) {
      form_set_error($field, t('Provided archive contains no files.'));
      return;
    }

    // Create the destination directory.
    $destination = 'public://' . variable_get('responsive_favicons_path', 'favicons');
    file_prepare_directory($destination, FILE_CREATE_DIRECTORY);

    // Copy the files to the correct location.
    $success_count = 0;
    foreach ($files as $file) {
      $success = file_unmanaged_copy($directory . '/' . $file, $destination, FILE_EXISTS_REPLACE);
      $uri = $destination . '/' . $file;
      if ($success) {
        $success_count++;

        // Rewrite the paths of the JSON files.
        if (preg_match('/\\.json$/', $file)) {
          $file_contents = file_get_contents(drupal_realpath($uri));
          $find = preg_quote('"\\/android-chrome', '/');
          $replace = '"' . str_replace('/', '\\/', _responsive_favicons_normalise_path('/android-chrome'));
          $file_contents = preg_replace('/' . $find . '/', $replace, $file_contents);
          file_unmanaged_save_data($file_contents, $uri, FILE_EXISTS_REPLACE);
        }
        else {
          if (preg_match('/\\.xml$/', $file)) {
            $file_contents = file_get_contents(drupal_realpath($uri));
            $find = preg_quote('"/mstile', '/');
            $replace = '"' . _responsive_favicons_normalise_path('/mstile');
            $file_contents = preg_replace('/' . $find . '/', $replace, $file_contents);
            file_unmanaged_save_data($file_contents, $uri, FILE_EXISTS_REPLACE);
          }
          else {
            if (preg_match('/\\.webmanifest$/', $file)) {
              $file_contents = file_get_contents(drupal_realpath($uri));
              $find = preg_quote('"/android-chrome', '/');
              $replace = '"' . _responsive_favicons_normalise_path('/android-chrome');
              $file_contents = preg_replace('/' . $find . '/', $replace, $file_contents);
              file_unmanaged_save_data($file_contents, $uri, FILE_EXISTS_REPLACE);
            }
          }
        }
      }
    }
    if ($success_count > 0) {
      drupal_set_message(format_plural($success_count, 'Uploaded 1 favicon successfully.', 'Uploaded @count favicons successfully.'));
    }
  }
}