Issue #1255852 by Freso: Fixed bug in previous commit.
[project/fontyourface.git] / modules / fontsquirrel / fontsquirrel.module
1 <?php
2
3 /**
4 * Implements hook_fontyourface_info().
5 */
6 function fontsquirrel_fontyourface_info() {
7
8 return array(
9 'name' => 'Font Squirrel',
10 'url' => 'http://www.fontsquirrel.com/',
11 );
12
13 } // fontsquirrel_fonts_api_fontyourface_info
14
15 /**
16 * Implements hook_fontyourface_preview().
17 */
18 function fontsquirrel_fontyourface_preview($font) {
19
20 $metadata = unserialize($font->metadata);
21
22 return '<img src="http://www.fontsquirrel.com/utils/makeFont.php?font=' . $metadata['id'] . '/' . $metadata['font_filename'] . '&width=300&size=18&text=' . urlencode($font->name) . '" />';
23
24 } // fontsquirrel_fontyourface_preview
25
26 /**
27 * Implements hook_fontyourface_view().
28 */
29 function fontsquirrel_fontyourface_view($font, $text) {
30
31 $metadata = unserialize($font->metadata);
32
33 $output = '<div><img src="http://www.fontsquirrel.com/utils/makeFont.php?font=' . $metadata['id'] . '/' . $metadata['font_filename'] . '&width=800&size=14&text=' . urlencode($text) . '" /></div>';
34
35 $output .= '<div><img src="http://www.fontsquirrel.com/utils/makeSolotypeSample.php?font=' . $metadata['id'] . '/' . $metadata['font_filename'] . '&case=all" /></div>';
36
37 return $output;
38
39 } // fontsquirrel_fontyourface_preview
40
41 /**
42 * Implements template_preprocess_page().
43 */
44 function fontsquirrel_preprocess_page(&$vars) {
45
46 if (!empty($vars['fontyourface']) && $_GET['q'] != 'admin/build/themes/fontyourface/add') {
47
48 $paths = array();
49
50 foreach ($vars['fontyourface'] as $used_font) {
51
52 if ($used_font->provider == 'fontsquirrel') {
53 $metadata = unserialize($used_font->metadata);
54 $paths[] = $metadata['path'];
55 } // if
56
57 } // foreach
58
59 if (count($paths) > 0) {
60
61 foreach ($paths as $path) {
62
63 fontyourface_add_css_in_preprocess($vars, file_directory_path() . '/fontyourface/fontsquirrel/' . $path . '-fontfacekit/stylesheet.css');
64
65 } // foreach
66
67 } // if
68
69 } // if
70
71 } // fontsquirrel_preprocess_page
72
73 /**
74 * Implements hook_fontyourface_enable().
75 */
76 function fontsquirrel_fontyourface_enable($font) {
77
78 $success = TRUE;
79 $metadata = unserialize($font->metadata);
80
81 if ($font->css_family == '') {
82
83 $api_result = drupal_http_request('http://www.fontsquirrel.com/api/familyinfo/' . $metadata['path']);
84
85 if ($api_result->code == '200') {
86
87 $decoded = json_decode($api_result->data);
88
89 if (is_array($decoded) && is_object($decoded[0])) {
90
91 $font->css_family = "'" . $decoded[0]->{'fontface_name'} . "'";
92 fontyourface_save_font($font);
93
94 } // if
95 else {
96
97 drupal_set_message(t('There was an error importing font information for %fontname from Font Squirrel. Font not found in API.', array('%fontname' => $font->name)), 'error');
98 $success = FALSE;
99
100 } // else
101
102 } // if
103 else {
104
105 drupal_set_message(t('There was an error importing font information for %fontname from Font Squirrel. API not responding.', array('%fontname' => $font-name)), 'error');
106 $success = FALSE;
107
108 } // else
109
110 } // if
111
112 $directory_location = dirname($_SERVER['SCRIPT_FILENAME']) . '/' . file_directory_path() . '/fontyourface/fontsquirrel/' . $metadata['path'] . '-fontfacekit';
113
114 if (
115 (!file_check_directory($directory_location))
116 ) {
117
118 $zip_location = dirname($_SERVER['SCRIPT_FILENAME']) . '/' . file_directory_path() . '/fontyourface/fontsquirrel/' . $metadata['path'] . '-fontfacekit.zip';
119
120 // Download file .zip file
121
122 if (!file_exists($zip_location)) {
123
124 $kit_url = 'http://www.fontsquirrel.com/fontfacekit/' . $metadata['path'];
125
126 $kit_result = drupal_http_request($kit_url);
127
128 if ($kit_result->code == 200) {
129
130 // Save the .zip file
131
132 if (file_check_directory(dirname($zip_location), FILE_CREATE_DIRECTORY)) {
133
134 file_save_data($kit_result->data, $zip_location, FILE_EXISTS_REPLACE);
135
136 } // if
137 else {
138
139 drupal_set_message(t('There was an error saving font %fontname from Font Squirrel.', array('%fontname' => $font->name)), 'error');
140 $success = FALSE;
141
142 } // else
143
144 } // if
145 else {
146
147 drupal_set_message(t('There was an error downloading font %fontname from Font Squirrel.', array('%fontname' => $font->name)), 'error');
148 $success = FALSE;
149
150 } // else
151
152 } // if
153
154 if ($success) {
155
156 // Unzip the .zip file
157
158 if (function_exists('zip_open') && $zip = zip_open($zip_location)) {
159
160 file_check_directory($directory_location, FILE_CREATE_DIRECTORY);
161
162 while ($entry = zip_read($zip)) {
163
164 if (zip_entry_open($zip, $entry, 'r') && ($zip_entry_filesize = zip_entry_filesize($entry))) {
165
166 $entry_name = zip_entry_name($entry);
167 $data = zip_entry_read($entry, $zip_entry_filesize);
168 file_save_data($data, $directory_location .'/'. $entry_name);
169
170 } // if
171
172 zip_entry_close($entry);
173
174 } // while
175
176 zip_close($zip);
177
178 } // if
179 else {
180
181 drupal_set_message(t('Unable to unzip font %fontname at <code>@zip_location</code>. See !zipdocs to enable unzipping, or unzip the file manually and enable this font again.', array(
182 '%fontname' => $font->name,
183 '@zip_location' => $zip_location,
184 '!zipdocs' => l(t('PHP documentation on zip'), 'http://www.php.net/manual/en/zip.installation.php'),
185 )), 'error');
186 $success = FALSE;
187
188 } // else
189
190 } // if
191
192 if (! $success) {
193 fontyourface_disable_font($font);
194 } // if
195
196 } // if
197
198 return $success;
199
200 } // fontsquirrel_fontyourface_enable
201
202 /**
203 * Implements hook_fontyourface_import().
204 */
205 function fontsquirrel_fontyourface_import() {
206
207 $api_result = drupal_http_request('http://www.fontsquirrel.com/api/fontlist/all');
208
209 if ($api_result->code == '200') {
210
211 $decoded = json_decode($api_result->data);
212
213 foreach ($decoded as $font_import) {
214
215 $metadata = array(
216 'id' => $font_import->id,
217 'path' => $font_import->family_urlname,
218 'font_filename' => $font_import->font_filename,
219 );
220
221 $font = new StdClass;
222 $font->name = $font_import->family_name;
223 $font->url = 'http://www.fontsquirrel.com/fonts/' . $font_import->family_urlname;
224 $font->provider = 'fontsquirrel';
225 $font->foundry = $font_import->foundry_name;
226 $font->foundry_url = 'http://www.fontsquirrel.com/foundry/' . $font_import->foundry_urlname;
227 $font->license = 'See Font Squirrel license page';
228 $font->license_url = 'http://www.fontsquirrel.com/fonts/' . $font_import->family_urlname . '#eula';
229 $font->tags = array($font_import->classification);
230 $font->metadata = serialize($metadata);
231
232 fontyourface_save_font($font);
233
234 } // foreach
235
236 } // if
237
238 } // fontsquirrel_fontyourface_import