You are here

function fontsquirrel_fontyourface_enable in @font-your-face 6

Same name and namespace in other branches
  1. 6.2 modules/fontsquirrel/fontsquirrel.module \fontsquirrel_fontyourface_enable()
  2. 7.2 modules/fontsquirrel/fontsquirrel.module \fontsquirrel_fontyourface_enable()
  3. 7 modules/fontsquirrel/fontsquirrel.module \fontsquirrel_fontyourface_enable()

Implements hook_fontyourface_enable().

File

modules/fontsquirrel/fontsquirrel.module, line 243

Code

function fontsquirrel_fontyourface_enable($used_font) {
  $success = TRUE;
  $list = fontsquirrel_list();
  $font = $list[$used_font->group_name]['fonts'][$used_font->name];
  $directory_location = dirname($_SERVER['SCRIPT_FILENAME']) . '/' . file_directory_path() . '/fontyourface/fontsquirrel/' . $font['path'] . '-fontfacekit';
  if (!file_check_directory($directory_location)) {
    $zip_location = dirname($_SERVER['SCRIPT_FILENAME']) . '/' . file_directory_path() . '/fontyourface/fontsquirrel/' . $font['path'] . '-fontfacekit.zip';

    // Download file .zip file
    if (!file_exists($zip_location)) {
      $kit_url = 'http://www.fontsquirrel.com/fontfacekit/' . $font['path'];
      $kit_result = drupal_http_request($kit_url);
      if ($kit_result->code == 200) {

        // Save the .zip file
        if (file_check_directory(dirname($zip_location), FILE_CREATE_DIRECTORY)) {
          file_save_data($kit_result->data, $zip_location, FILE_EXISTS_REPLACE);
        }
        else {
          drupal_set_message(t('There was an error saving font') . ' <i>' . $used_font->name . '</i> ' . t('from') . ' Font Squirrel.', 'error');
          $success = FALSE;
        }

        // else
      }
      else {
        drupal_set_message(t('There was an error downloading font') . ' <i>' . $used_font->name . '</i> ' . t('from') . ' Font Squirrel.', 'error');
        $success = FALSE;
      }

      // else
    }

    // if
    if ($success) {

      // Unzip the .zip file
      if (function_exists('zip_open') && ($zip = zip_open($zip_location))) {
        file_check_directory($directory_location, FILE_CREATE_DIRECTORY);
        while ($entry = zip_read($zip)) {
          if (zip_entry_open($zip, $entry, 'r') && ($zip_entry_filesize = zip_entry_filesize($entry))) {
            $entry_name = zip_entry_name($entry);
            $data = zip_entry_read($entry, $zip_entry_filesize);
            file_save_data($data, $directory_location . '/' . $entry_name);
          }

          // if
          zip_entry_close($entry);
        }

        // while
        zip_close($zip);
        drupal_set_message(t('Unzipped!'));
      }
      else {
        drupal_set_message(t('Unable to unzip font') . ' <i>' . $used_font->name . '</i> ' . t('at') . ' <code>' . check_plain($zip_location) . '</code>. ' . t('See !zipdocs to enable unzipping, or unzip the file manually and re-enable this font.', array(
          '!zipdocs' => l('PHP documentation on zip', 'http://www.php.net/manual/en/zip.installation.php'),
        )), 'error');
        $success = FALSE;
      }

      // else
    }

    // if
    if (!$success) {
      fontyourface_delete_font($used_font);
    }

    // if
  }

  // if
  return $success;
}