View source
<?php
function fontsquirrel_menu() {
$items = array();
$items['admin/build/themes/fontyourface/fontsquirrel/import'] = array(
'title' => 'Font Squirrel Import',
'page callback' => 'fontsquirrel_import',
'access arguments' => array(
'administer @font-your-face',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function fontsquirrel_import() {
fontsquirrel_cron();
drupal_goto($_GET['destination']);
}
function fontsquirrel_cron() {
$last_updated = variable_get('fontsquirrel_last_api_update', 0);
if (time() - 60 * 60 * 24 > $last_updated) {
$api_result = drupal_http_request('http://www.fontsquirrel.com/api/fontlist/all');
if ($api_result->code == '200') {
$decoded = json_decode($api_result->data);
foreach ($decoded as $font_group) {
$select_results = db_query('SELECT * FROM {fontsquirrel_group} g WHERE gid = %d', $font_group->id);
if ($select_result = db_fetch_object($select_results)) {
$changed = FALSE;
if ($select_result->gid != $font_group->id) {
$select_result->gid = $font_group->id;
$changed = TRUE;
}
if ($select_result->name != $font_group->family_name) {
$select_result->name = $font_group->family_name;
$changed = TRUE;
}
if ($select_result->path != $font_group->family_urlname) {
$select_result->path = $font_group->family_urlname;
$changed = TRUE;
}
if ($select_result->foundry_name != $font_group->foundry_name) {
$select_result->foundry_name = $font_group->foundry_name;
$changed = TRUE;
}
if ($select_result->foundry_path != $font_group->foundry_urlname) {
$select_result->foundry_path = $font_group->foundry_urlname;
$changed = TRUE;
}
if ($select_result->classification != $font_group->classification) {
$select_result->classification = $font_group->classification;
$changed = TRUE;
}
if ($select_result->filename != $font_group->font_filename) {
$select_result->filename = $font_group->font_filename;
$changed = TRUE;
}
if ($changed) {
drupal_write_record('fontsquirrel_group', $select_result, 'gid');
}
}
else {
$insert_sql = "INSERT INTO {fontsquirrel_group}\n (gid, name, path, foundry_name, foundry_path, classification, filename)\n VALUES\n (%d, '%s', '%s', '%s', '%s', '%s', '%s')";
db_query($insert_sql, $font_group->id, $font_group->family_name, $font_group->family_urlname, $font_group->foundry_name, $font_group->foundry_urlname, $font_group->classification, $font_group->font_filename);
}
}
}
variable_set('fontsquirrel_last_api_update', time());
}
}
function fontsquirrel_fontyourface_info() {
$info = array(
'fontsquirrel' => array(
'name' => 'Font Squirrel',
'url' => 'http://www.fontsquirrel.com/',
'fonts' => fontsquirrel_list(),
),
);
return $info;
}
function fontsquirrel_fontyourface_preview($font) {
return '<img src="http://www.fontsquirrel.com/utils/makeFont.php?font=' . $font['id'] . '/' . $font['filename'] . '&width=300&size=18&text=' . urlencode($font['family']) . '" />';
}
function fontsquirrel_fontyourface_view($font, $text) {
$output = '<div><img src="http://www.fontsquirrel.com/utils/makeFont.php?font=' . $font['id'] . '/' . $font['filename'] . '&width=800&size=14&text=' . urlencode($text) . '" /></div>';
$output .= '<div><img src="http://www.fontsquirrel.com/utils/makeSolotypeSample.php?font=' . $font['id'] . '/' . $font['filename'] . '&case=all" /></div>';
return $output;
}
function fontsquirrel_preprocess_page(&$vars) {
if (!empty($vars['fontyourface']) && $_GET['q'] != 'admin/build/themes/fontyourface/add') {
$paths = array();
$fonts = fontsquirrel_list();
foreach ($vars['fontyourface'] as $used_font) {
if ($used_font->provider == 'fontsquirrel') {
$paths[] = $fonts[$used_font->group_name]['fonts'][$used_font->name]['path'];
}
}
if (count($paths) > 0) {
foreach ($paths as $path) {
fontyourface_add_css_in_preprocess($vars, file_directory_path() . '/fontyourface/fontsquirrel/' . $path . '-fontfacekit/stylesheet.css');
}
}
}
}
function fontsquirrel_fontyourface_css($used_font) {
$css = array(
'@font-face' => '',
'font-family' => '',
);
$list = fontsquirrel_list(FALSE);
$font = $list[$used_font->group_name]['fonts'][$used_font->name];
if ($font['family'] == '') {
fontsquirrel_update_font_family($font);
}
$css['font-family'] = "'" . $font['family'] . "'";
return $css;
}
function fontsquirrel_update_font_family(&$font) {
$api_result = drupal_http_request('http://www.fontsquirrel.com/api/familyinfo/' . $font['path']);
if ($api_result->code == '200') {
$decoded = json_decode($api_result->data);
if (is_array($decoded) && is_object($decoded[0])) {
$font['family'] = $decoded[0]->{'fontface_name'};
db_query("UPDATE {fontsquirrel_group} g SET font_family = '%s' WHERE gid = %d", $font['family'], $font['id']);
}
}
}
function fontsquirrel_fontyourface_license($used_font) {
$list = fontsquirrel_list();
$font = $list[$used_font->group_name]['fonts'][$used_font->name];
return array(
'name' => 'See Font Squirrel license page',
'url' => 'http://www.fontsquirrel.com/license/' . $font['path'],
);
}
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';
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) {
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 {
drupal_set_message(t('There was an error downloading font') . ' <i>' . $used_font->name . '</i> ' . t('from') . ' Font Squirrel.', 'error');
$success = FALSE;
}
}
if ($success) {
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);
}
zip_entry_close($entry);
}
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;
}
}
if (!$success) {
fontyourface_delete_font($used_font);
}
}
return $success;
}
function fontsquirrel_list($show_error = TRUE) {
static $fonts = FALSE;
if ($fonts === FALSE) {
$fonts = array();
$font_fid_to_name = array();
$results = db_query('SELECT g.* FROM {fontsquirrel_group} g');
while ($result = db_fetch_object($results)) {
$fonts[$result->name] = array(
'path' => $result->path,
'foundry' => $result->foundry_name,
'foundry_path' => $result->foundry_path,
'fonts' => array(),
);
$fonts[$result->name]['fonts'][$result->name] = array(
'id' => $result->gid,
'path' => $result->path,
'filename' => $result->filename,
'family' => $result->font_family,
'local' => isset($result->local) ? $result->local : '',
);
}
if (count($fonts) == 0 && $show_error) {
drupal_set_message(t('The Font Squirrel font list has not yet been imported. !importlink or wait until the next cron run.', array(
'!importlink' => l('Import now', 'admin/build/themes/fontyourface/fontsquirrel/import', array(
'query' => drupal_get_destination(),
)),
)));
}
}
return $fonts;
}