| Commit | Line | Data |
|---|---|---|
| a679aaae NH |
1 | <?php |
| 2 | // $Id$ | |
| d2f124d5 | 3 | |
| d93afe47 DP |
4 | /** |
| 5 | * @file | |
| 0da420f9 | 6 | * FileField: Defines a CCK file field type. |
| 6413f0f6 | 7 | * |
| 945e9f37 JP |
8 | * Uses content.module to store the fid and field specific metadata, |
| 9 | * and Drupal's {files} table to store the actual file data. | |
| d93afe47 DP |
10 | */ |
| 11 | ||
| c7acc8fb DP |
12 | include_once('field_file.inc'); |
| 13 | ||
| d2f124d5 DP |
14 | /** |
| 15 | * Implementation of hook_init(). | |
| 16 | */ | |
| db33b245 | 17 | function filefield_init() { |
| d2f124d5 | 18 | // file hooks and callbacks. |
| 0d18cd91 | 19 | module_load_include('inc', 'filefield', 'filefield_widget'); |
| bad2b17f | 20 | drupal_add_css(drupal_get_path('module', 'filefield') .'/filefield.css'); |
| d2f124d5 | 21 | } |
| 9412a7cc | 22 | |
| 382b6b6f JP |
23 | /** |
| 24 | * Implementation of hook_menu(). | |
| 25 | */ | |
| 26 | function filefield_menu() { | |
| d93afe47 DP |
27 | $items = array(); |
| 28 | ||
| aa3a8ca0 | 29 | $items['filefield/ahah/%/%/%'] = array( |
| 382b6b6f | 30 | 'page callback' => 'filefield_js', |
| aa3a8ca0 | 31 | 'page arguments' => array(2,3,4), |
| f1113abd | 32 | 'access callback' => 'filefield_edit_access', |
| 43e319eb | 33 | 'access arguments' => array(3), |
| 382b6b6f JP |
34 | 'type' => MENU_CALLBACK, |
| 35 | ); | |
| d93afe47 DP |
36 | return $items; |
| 37 | } | |
| 38 | ||
| 39 | /** | |
| 0c08790f | 40 | * Implementation of hook_elements(). |
| 22e736e6 | 41 | * @todo: autogenerate element registry entries for widgets. |
| 0c08790f JP |
42 | */ |
| 43 | function filefield_elements() { | |
| 44 | $elements = array(); | |
| d2f124d5 | 45 | $elements['filefield_widget'] = array( |
| 0c08790f | 46 | '#input' => TRUE, |
| 28b1c58e | 47 | '#columns' => array('fid', 'list', 'data'), |
| d2f124d5 | 48 | '#process' => array('filefield_widget_process'), |
| d2f124d5 | 49 | '#value_callback' => 'filefield_widget_value', |
| ecbb2c26 | 50 | '#element_validate' => array('filefield_widget_validate'), |
| d2f124d5 | 51 | '#description' => t('Changes made to the attachments are not permanent until you save this post.'), |
| 0c08790f | 52 | ); |
| d2f124d5 | 53 | $elements['filefield_extensible'] = array( |
| 0c08790f | 54 | '#input' => TRUE, |
| 28b1c58e | 55 | '#columns' => array('fid', 'list', 'data'), |
| 6e7782b4 DP |
56 | '#process' => array('filefield_process'), |
| 57 | '#value_callback' => 'filefield_value', | |
| d2f124d5 | 58 | '#description' => t('Changes made to the attachments are not permanent until you save this post.'), |
| 6db07116 | 59 | ); |
| 0c08790f JP |
60 | return $elements; |
| 61 | } | |
| 62 | ||
| 63 | /** | |
| 382b6b6f | 64 | * Implementation of hook_theme(). |
| 22e736e6 | 65 | * @todo: autogenerate theme registry entrys for widgets. |
| 382b6b6f JP |
66 | */ |
| 67 | function filefield_theme() { | |
| 68 | return array( | |
| c147cceb DP |
69 | 'filefield_file' => array( |
| 70 | 'arguments' => array('file' => NULL), | |
| 71 | 'file' => 'filefield_formatter.inc', | |
| 72 | ), | |
| 73 | 'filefield_icon' => array( | |
| 74 | 'arguments' => array('file' => NULL), | |
| 75 | 'file' => 'filefield.theme.inc', | |
| 76 | ), | |
| d2f124d5 | 77 | 'filefield_widget' => array( |
| 5983a6bc | 78 | 'arguments' => array('element' => NULL), |
| d2f124d5 | 79 | 'file' => 'filefield_widget.inc', |
| 5983a6bc | 80 | ), |
| c4c7e990 | 81 | 'filefield_widget_item' => array( |
| bad2b17f DP |
82 | 'arguments' => array('element' => NULL), |
| 83 | 'file' => 'filefield_widget.inc', | |
| 84 | ), | |
| c4c7e990 | 85 | 'filefield_widget_preview' => array( |
| 8a154cf2 | 86 | 'arguments' => array('element' => NULL), |
| db33b245 | 87 | 'file' => 'filefield_widget.inc', |
| 8a154cf2 | 88 | ), |
| 3897ed27 NH |
89 | 'filefield_widget_file' => array( |
| 90 | 'arguments' => array('element' => NULL), | |
| 91 | 'file' => 'filefield_widget.inc', | |
| 92 | ), | |
| c4c7e990 DP |
93 | |
| 94 | ||
| bc63d6a9 | 95 | 'filefield_formatter_default' => array( |
| 382b6b6f | 96 | 'arguments' => array('element' => NULL), |
| c147cceb | 97 | 'file' => 'filefield_formatter.inc', |
| 382b6b6f | 98 | ), |
| c4c7e990 | 99 | 'filefield_item' => array( |
| a1f1332d | 100 | 'arguments' => array('file' => NULL, 'field' => NULL), |
| c147cceb | 101 | 'file' => 'filefield_formatter.inc', |
| b85662ff | 102 | ), |
| c4c7e990 DP |
103 | 'filefield_file' => array( |
| 104 | 'arguments' => array('file' => NULL), | |
| c147cceb | 105 | 'file' => 'filefield_formatter.inc', |
| 382b6b6f | 106 | ), |
| 7e784adc | 107 | |
| c4c7e990 | 108 | ); |
| cdf9e275 JP |
109 | } |
| 110 | ||
| 111 | /** | |
| f1113abd JP |
112 | * Implementation of hook_file_download(). Yes, *that* hook that causes |
| 113 | * any attempt for file upload module interoperability to fail spectacularly. | |
| 114 | */ | |
| cdf9e275 JP |
115 | function filefield_file_download($file) { |
| 116 | $file = file_create_path($file); | |
| 8a29f595 | 117 | |
| cdf9e275 JP |
118 | $result = db_query("SELECT * FROM {files} WHERE filepath = '%s'", $file); |
| 119 | if (!$file = db_fetch_object($result)) { | |
| 120 | // We don't really care about this file. | |
| 121 | return; | |
| 122 | } | |
| 8a29f595 | 123 | |
| f1113abd JP |
124 | // Find out if any filefield contains this file, and if so, which field |
| 125 | // and node it belongs to. Required for later access checking. | |
| 126 | $cck_files = array(); | |
| 127 | foreach (content_fields() as $field) { | |
| 2d140ce4 | 128 | if ($field['type'] == 'filefield' || $field['type'] == 'image') { |
| f1113abd JP |
129 | $db_info = content_database_info($field); |
| 130 | $table = $db_info['table']; | |
| 131 | $fid_column = $db_info['columns']['fid']['column']; | |
| 132 | ||
| 133 | $columns = array('vid', 'nid'); | |
| 134 | foreach ($db_info['columns'] as $property_name => $column_info) { | |
| 135 | $columns[] = $column_info['column'] .' AS '. $property_name; | |
| 136 | } | |
| 137 | $result = db_query("SELECT ". implode(', ', $columns) ." | |
| 138 | FROM {". $table ."} | |
| 139 | WHERE ". $fid_column ." = %d", $file->fid); | |
| 140 | ||
| 141 | while ($content = db_fetch_array($result)) { | |
| 142 | $content['field'] = $field; | |
| 143 | $cck_files[$field['field_name']][$content['vid']] = $content; | |
| 144 | } | |
| 145 | } | |
| 382b6b6f | 146 | } |
| f1113abd JP |
147 | // If no filefield item is involved with this file, we don't care about it. |
| 148 | if (empty($cck_files)) { | |
| 149 | return; | |
| 150 | } | |
| 151 | ||
| 2de8fe0e JP |
152 | // If any node includes this file but the user may not view this field, |
| 153 | // then deny the download. | |
| 154 | foreach ($cck_files as $field_name => $field_files) { | |
| 155 | if (!filefield_view_access($field_name)) { | |
| 156 | return -1; | |
| f1113abd JP |
157 | } |
| 158 | } | |
| 159 | ||
| 160 | // So the overall field view permissions are not denied, but if access is | |
| 161 | // denied for a specific node containing the file, deny the download as well. | |
| 162 | // It's probably a little too restrictive, but I can't think of a | |
| 163 | // better way at the moment. Input appreciated. | |
| 164 | // (And yeah, node access checks also include checking for 'access content'.) | |
| 165 | $nodes = array(); | |
| 166 | foreach ($cck_files as $field_name => $field_files) { | |
| 167 | foreach ($field_files as $revision_id => $content) { | |
| 168 | // Checking separately for each revision is probably not the best idea - | |
| 169 | // what if 'view revisions' is disabled? So, let's just check for the | |
| 170 | // current revision of that node. | |
| 171 | if (isset($nodes[$content['nid']])) { | |
| 172 | continue; // don't check the same node twice | |
| 173 | } | |
| 174 | $node = node_load($content['nid']); | |
| 175 | if (!node_access('view', $node)) { | |
| 176 | // You don't have permission to view the node this file is attached to. | |
| 177 | return -1; | |
| 178 | } | |
| 179 | $nodes[$content['nid']] = $node; | |
| 180 | } | |
| 59b7732d | 181 | } |
| cdf9e275 JP |
182 | |
| 183 | // Well I guess you can see this file. | |
| 184 | $name = mime_header_encode($file->filename); | |
| 185 | $type = mime_header_encode($file->filemime); | |
| 186 | // Serve images and text inline for the browser to display rather than download. | |
| 187 | $disposition = ereg('^(text/|image/)', $file->filemime) ? 'inline' : 'attachment'; | |
| 188 | return array( | |
| 189 | 'Content-Type: '. $type .'; name='. $name, | |
| 190 | 'Content-Length: '. $file->filesize, | |
| 191 | 'Content-Disposition: '. $disposition .'; filename='. $name, | |
| 192 | 'Cache-Control: private', | |
| 193 | ); | |
| 59b7732d DP |
194 | } |
| 195 | ||
| 175b9f21 | 196 | /** |
| 197 | * Implementation of hook_form_alter(). | |
| 198 | * | |
| 199 | * Set the appropriate attibutes to allow file uploads on the field settings | |
| 200 | * form. | |
| 201 | */ | |
| 202 | function filefield_form_alter(&$form, $form_state, $form_id) { | |
| 203 | if ($form_id == 'content_field_edit_form') { | |
| 204 | $form['#attributes'] = array('enctype' => 'multipart/form-data'); | |
| 205 | } | |
| 206 | } | |
| c985d8bc | 207 | |
| 45f2943f | 208 | /** |
| d2f124d5 DP |
209 | * Implementation of CCK's hook_field_info(). |
| 210 | */ | |
| 211 | function filefield_field_info() { | |
| 212 | return array( | |
| 213 | 'filefield' => array( | |
| 214 | 'label' => 'File', | |
| 215 | 'description' => t('Store an arbitrary file.'), | |
| 216 | ), | |
| 217 | ); | |
| 218 | } | |
| 219 | ||
| 220 | /** | |
| 221 | * Implementation of hook_field_settings(). | |
| 222 | */ | |
| 223 | function filefield_field_settings($op, $field) { | |
| b4620340 DP |
224 | $return = array(); |
| 225 | ||
| 56f3f61b | 226 | module_load_include('inc', 'filefield', 'filefield_field'); |
| d2f124d5 DP |
227 | $op = str_replace(' ', '_', $op); |
| 228 | // add filefield specific handlers... | |
| 229 | $function = 'filefield_field_settings_'. $op; | |
| 230 | if (function_exists($function)) { | |
| 90b9a66c | 231 | $result = $function($field); |
| 232 | if (isset($result) && is_array($result)) { | |
| 233 | $return = $result; | |
| 234 | } | |
| d2f124d5 | 235 | } |
| b4620340 | 236 | |
| 7e784adc | 237 | // dynamically load widgets file and callbacks for other fields utilizing |
| f92ea317 | 238 | // filefield's hook_field_settings implementation. |
| b4620340 | 239 | module_load_include('inc', $field['module'], $field['type'] .'_field'); |
| fc38be5c | 240 | $function = $field['module'] .'_'. $field['type'] .'_field_settings_'. $op; |
| b4620340 | 241 | if (function_exists($function)) { |
| 90b9a66c | 242 | $result = $function($field); |
| 243 | if (isset($result) && is_array($result)) { | |
| 244 | $return = array_merge($return, $result); | |
| 245 | } | |
| b4620340 | 246 | } |
| 7e784adc | 247 | |
| b4620340 DP |
248 | return $return; |
| 249 | ||
| d2f124d5 DP |
250 | } |
| 251 | ||
| 252 | /** | |
| 253 | * Implementtation of CCK's hook_field(). | |
| 45f2943f | 254 | */ |
| d2f124d5 | 255 | function filefield_field($op, $node, $field, &$items, $teaser, $page) { |
| 56f3f61b | 256 | module_load_include('inc', 'filefield', 'filefield_field'); |
| d2f124d5 DP |
257 | $op = str_replace(' ', '_', $op); |
| 258 | // add filefield specific handlers... | |
| 259 | $function = 'filefield_field_'. $op; | |
| 260 | if (function_exists($function)) { | |
| 261 | return $function($node, $field, $items, $teaser, $page); | |
| cdf9e275 JP |
262 | } |
| 263 | } | |
| 264 | ||
| c985d8bc | 265 | /** |
| d2f124d5 | 266 | * Implementation of CCK's hook_widget_settings(). |
| c985d8bc | 267 | */ |
| d2f124d5 | 268 | function filefield_widget_settings($op, $widget) { |
| eb21e5bd NH |
269 | switch ($op) { |
| 270 | case 'form': | |
| 271 | return filefield_widget_settings_form($widget); | |
| 272 | case 'save': | |
| 273 | return filefield_widget_settings_save($widget); | |
| 29d050ce | 274 | } |
| cdf9e275 | 275 | } |
| d2f124d5 DP |
276 | |
| 277 | /** | |
| 278 | * Implementation of hook_widget(). | |
| 279 | */ | |
| 280 | function filefield_widget(&$form, &$form_state, $field, $items, $delta = 0) { | |
| 2be7e4b0 DP |
281 | // CCK doesn't give a validate callback at the field level... |
| 282 | // and FAPI's #require is naieve to complex structures... | |
| 283 | // we validate at the field level ourselves. | |
| 195dd796 | 284 | if (empty($form['#validate']) || !in_array('filefield_node_form_validate', $form['#validate'])) { |
| 2be7e4b0 DP |
285 | $form['#validate'][] = 'filefield_node_form_validate'; |
| 286 | } | |
| 195dd796 | 287 | if (empty($form['#submit']) || !in_array('filefield_node_form_submit', $form['#submit'])) { |
| 5af49125 DP |
288 | $form['#submit'][] = 'filefield_node_form_submit'; |
| 289 | } | |
| c02fd5eb | 290 | $form['#attributes'] = array('enctype' => 'multipart/form-data'); |
| 5af49125 | 291 | |
| 6f0f10d4 | 292 | module_load_include('inc', 'filefield', 'field_widget'); |
| c02fd5eb | 293 | module_load_include('inc', $field['widget']['module'], $field['widget']['module'] .'_widget'); |
| 6f0f10d4 | 294 | |
| c02fd5eb | 295 | $item = array('fid' => 0, 'list' => $field['list_default'], 'data' => array('description' => '')); |
| 296 | if (isset($items[$delta])) { | |
| 297 | $item = array_merge($item, $items[$delta]); | |
| 298 | } | |
| d2f124d5 | 299 | $element = array( |
| 6e7782b4 | 300 | '#title' => $field['widget']['label'], |
| d2f124d5 | 301 | '#type' => $field['widget']['type'], |
| c02fd5eb | 302 | '#default_value' => $item, |
| dce479a8 | 303 | '#upload_validators' => filefield_widget_upload_validators($field), |
| d2f124d5 | 304 | ); |
| c02fd5eb | 305 | |
| d2f124d5 DP |
306 | return $element; |
| 307 | } | |
| 308 | ||
| 309 | /** | |
| dce479a8 | 310 | * Get the upload validators for a file field. |
| 311 | * | |
| 312 | * @param $field CCK Field | |
| 313 | * @return array suitable for passing to file_save_upload() or the filefield | |
| 314 | * element's '#upload_validators' property. | |
| 315 | */ | |
| 316 | function filefield_widget_upload_validators($field) { | |
| f92ea317 DP |
317 | $max_filesize = parse_size(file_upload_max_size()); |
| 318 | if (!empty($field['widget']['max_filesize_per_file']) && parse_size($field['widget']['max_filesize_per_file']) < $max_filesize) { | |
| 319 | $max_filesize = parse_size($field['widget']['max_filesize_per_file']); | |
| dce479a8 | 320 | } |
| 321 | ||
| 322 | $validators = array( | |
| ad6bb31f DP |
323 | // associate the field to the file on validation. |
| 324 | 'filefield_validate_associate_field' => array($field), | |
| f92ea317 | 325 | 'filefield_validate_size' => array($max_filesize), |
| ad6bb31f DP |
326 | // Override core since it excludes uid 1 on the extension check. |
| 327 | // Filefield only excuses uid 1 of quota requirements. | |
| dce479a8 | 328 | 'filefield_validate_extensions' => array($field['widget']['file_extensions']), |
| 329 | ); | |
| 330 | return $validators; | |
| 331 | } | |
| 332 | ||
| 333 | ||
| 334 | /** | |
| d2f124d5 DP |
335 | * Implementation of CCK's hook_content_is_empty(). |
| 336 | * | |
| 337 | * The result of this determines whether content.module will save | |
| 338 | * the value of the field. | |
| 339 | */ | |
| 340 | function filefield_content_is_empty($item, $field) { | |
| 6f0f10d4 | 341 | return empty($item['fid']) || (int)$item['fid'] == 0; |
| d2f124d5 DP |
342 | } |
| 343 | ||
| d2f124d5 DP |
344 | /** |
| 345 | * Implementation of CCK's hook_widget_info(). | |
| 346 | */ | |
| 347 | function filefield_widget_info() { | |
| 348 | return array( | |
| 349 | 'filefield_widget' => array( | |
| 350 | 'label' => t('File Upload'), | |
| 57d2397f | 351 | 'field types' => array('filefield'), |
| d2f124d5 DP |
352 | 'multiple values' => CONTENT_HANDLE_CORE, |
| 353 | 'callbacks' => array('default value' => CONTENT_CALLBACK_CUSTOM), | |
| 354 | 'description' => t('A plain file upload widget.'), | |
| 355 | ), | |
| 356 | 'filefield_combo' => array( | |
| 357 | 'label' => 'Extensible File', | |
| 57d2397f | 358 | 'field types' => array('filefield'), |
| d2f124d5 DP |
359 | 'multiple values' => CONTENT_HANDLE_CORE, |
| 360 | 'callbacks' => array('default value' => CONTENT_CALLBACK_CUSTOM), | |
| 361 | 'description' => t('(Experimental)An extensible file upload widget.'), | |
| 362 | ), | |
| 363 | ); | |
| 364 | } | |
| 365 | ||
| 366 | /** | |
| 367 | * Implementation of CCK's hook_field_formatter_info(). | |
| 368 | */ | |
| 369 | function filefield_field_formatter_info() { | |
| 370 | return array( | |
| bc63d6a9 | 371 | 'default' => array( |
| d2f124d5 | 372 | 'label' => t('Generic files'), |
| eb21e5bd | 373 | 'field types' => array('filefield'), |
| d2f124d5 DP |
374 | 'multiple values' => CONTENT_HANDLE_CORE, |
| 375 | 'description' => t('Displays all kinds of files with an icon and a linked file description.'), | |
| 376 | ), | |
| d2f124d5 DP |
377 | ); |
| 378 | } | |
| 379 | ||
| d2f124d5 DP |
380 | /** |
| 381 | * Determine the most appropriate icon for the given file's mimetype. | |
| 382 | * | |
| 2c8026f2 NH |
383 | * @param $file |
| 384 | * A file object. | |
| 385 | * @return | |
| 386 | * The URL of the icon image file, or FALSE if no icon could be found. | |
| d2f124d5 DP |
387 | */ |
| 388 | function filefield_icon_url($file) { | |
| 389 | include_once(drupal_get_path('module', 'filefield') .'/filefield.theme.inc'); | |
| 390 | return _filefield_icon_url($file); | |
| 391 | } | |
| 392 | ||
| d2f124d5 DP |
393 | /** |
| 394 | * Access callback for the JavaScript upload and deletion AHAH callbacks. | |
| 395 | * The content_permissions module provides nice fine-grained permissions for | |
| 396 | * us to check, so we can make sure that the user may actually edit the file. | |
| 397 | */ | |
| 398 | function filefield_edit_access($field_name) { | |
| 399 | if (module_exists('content_permissions')) { | |
| 400 | return user_access('edit '. $field_name); | |
| 401 | } | |
| 402 | // No content permissions to check, so let's fall back to a more general permission. | |
| 403 | return user_access('access content'); | |
| 404 | } | |
| 405 | ||
| 406 | /** | |
| 407 | * Access callback that checks if the current user may view the filefield. | |
| 408 | */ | |
| 409 | function filefield_view_access($field_name) { | |
| 410 | if (module_exists('content_permissions')) { | |
| 411 | return user_access('view '. $field_name); | |
| 412 | } | |
| 413 | // No content permissions to check, so let's fall back to a more general permission. | |
| 414 | return user_access('access content'); | |
| 415 | } | |
| 416 | ||
| 22e736e6 | 417 | /** |
| aa3a8ca0 | 418 | * Shared AHAH callback for uploads and deletions... It rebuilds the form element |
| 7e784adc | 419 | * for a particular field item. As long as the form processing is properly |
| 420 | * encapsulated in the widget element the form should rebuild correctly using | |
| aa3a8ca0 | 421 | * FAPI without the need for additional callbacks or processing. |
| 22e736e6 | 422 | */ |
| aa3a8ca0 | 423 | function filefield_js($type_name, $field_name, $delta) { |
| 22e736e6 | 424 | $field = content_fields($field_name, $type_name); |
| 7e784adc | 425 | |
| 22e736e6 DP |
426 | if (empty($field) || empty($_POST['form_build_id'])) { |
| 427 | // Invalid request. | |
| 428 | print drupal_to_js(array('data' => '')); | |
| 429 | exit; | |
| 430 | } | |
| 431 | ||
| 432 | // Build the new form. | |
| 433 | $form_state = array('submitted' => FALSE); | |
| 434 | $form_build_id = $_POST['form_build_id']; | |
| 435 | $form = form_get_cache($form_build_id, $form_state); | |
| 436 | ||
| 437 | if (!$form) { | |
| 438 | // Invalid form_build_id. | |
| 439 | print drupal_to_js(array('data' => '')); | |
| 440 | exit; | |
| 441 | } | |
| aa3a8ca0 | 442 | |
| 4b58ccde NH |
443 | // Build the form. This calls the file field's #value_callback function and |
| 444 | // saves the uploaded file. Since this form is already marked as cached | |
| 445 | // (the #cache property is TRUE), the cache is updated automatically and we | |
| 446 | // don't need to call form_set_cache(). | |
| 447 | $args = $form['#parameters']; | |
| 448 | $form_id = array_shift($args); | |
| 449 | $form['#post'] = $_POST; | |
| 450 | $form = form_builder($form_id, $form, $form_state); | |
| 451 | ||
| 452 | // Update the cached form with the new element at the right place in the form. | |
| 22e736e6 | 453 | if (module_exists('fieldgroup') && ($group_name = _fieldgroup_field_get_group($type_name, $field_name))) { |
| 0ad570e2 NH |
454 | if (isset($form['#multigroups']) && isset($form['#multigroups'][$group_name][$field_name])) { |
| 455 | $form_element = $form[$group_name][$delta][$field_name]; | |
| 456 | } | |
| 457 | else { | |
| 458 | $form_element = $form[$group_name][$field_name][$delta]; | |
| 459 | } | |
| 22e736e6 DP |
460 | } |
| 461 | else { | |
| 4b58ccde | 462 | $form_element = $form[$field_name][$delta]; |
| 22e736e6 DP |
463 | } |
| 464 | ||
| 4b58ccde NH |
465 | if (isset($form_element['_weight'])) { |
| 466 | unset($form_element['_weight']); | |
| 467 | } | |
| 22e736e6 | 468 | |
| 4b58ccde | 469 | $output = drupal_render($form_element); |
| 22e736e6 DP |
470 | |
| 471 | // AHAH is not being nice to us and doesn't know the "other" button (that is, | |
| 472 | // either "Upload" or "Delete") yet. Which in turn causes it not to attach | |
| 473 | // AHAH behaviours after replacing the element. So we need to tell it first. | |
| ad98c19d NH |
474 | |
| 475 | // Loop through the JS settings and find the settings needed for our buttons. | |
| 22e736e6 | 476 | $javascript = drupal_add_js(NULL, NULL); |
| ad98c19d | 477 | $filefield_ahah_settings = array(); |
| 22e736e6 | 478 | if (isset($javascript['setting'])) { |
| ad98c19d NH |
479 | foreach ($javascript['setting'] as $settings) { |
| 480 | if (isset($settings['ahah'])) { | |
| 481 | foreach ($settings['ahah'] as $id => $ahah_settings) { | |
| 482 | if (strpos($id, 'filefield-upload') || strpos($id, 'filefield-remove')) { | |
| 483 | $filefield_ahah_settings[$id] = $ahah_settings; | |
| 484 | } | |
| 485 | } | |
| 486 | } | |
| 487 | } | |
| 22e736e6 DP |
488 | } |
| 489 | ||
| ad98c19d NH |
490 | // Add the AHAH settings needed for our new buttons. |
| 491 | if (!empty($filefield_ahah_settings)) { | |
| 492 | $output .= '<script type="text/javascript">jQuery.extend(Drupal.settings.ahah, '. drupal_to_js($filefield_ahah_settings) .');</script>'; | |
| 493 | } | |
| 494 | ||
| 495 | $output = theme('status_messages') . $output; | |
| 496 | ||
| 22e736e6 DP |
497 | // For some reason, file uploads don't like drupal_json() with its manual |
| 498 | // setting of the text/javascript HTTP header. So use this one instead. | |
| 2068eb40 | 499 | $GLOBALS['devel_shutdown'] = false; |
| 22e736e6 DP |
500 | print drupal_to_js(array('status' => TRUE, 'data' => $output)); |
| 501 | exit; | |
| 502 | } | |
| 1bffec9e | 503 | |
| 7e784adc | 504 | /** |
| 9ff38ea7 | 505 | * Implementation of hook_file_references(). |
| 506 | */ | |
| 507 | function filefield_file_references($file) { | |
| 508 | $references = 0; | |
| 509 | foreach(content_fields() as $field) { | |
| 5b3aa822 | 510 | if ($field['type'] != 'filefield') { |
| 9ff38ea7 | 511 | continue; |
| 512 | } | |
| 513 | $references += field_file_references($file, $field); | |
| 514 | } | |
| 515 | return array('filefield' => $references); | |
| ad6c5ff0 DP |
516 | } |
| 517 | ||
| d6caa409 | 518 | /** |
| 9ff38ea7 | 519 | * Implementation of hook_file_delete(). |
| 520 | */ | |
| 521 | function filefield_file_delete($file) { | |
| 522 | // foreach field... remove items referencing $file. | |
| 523 | } | |
| 524 | ||
| 525 | ||
| 526 | ||
| 527 | /** | |
| 7e784adc | 528 | * Check that the filename ends with an allowed extension. This check is |
| d6caa409 DP |
529 | * enforced for the user #1. |
| 530 | * | |
| 531 | * @param $file | |
| 532 | * A Drupal file object. | |
| 533 | * @param $extensions | |
| 534 | * A string with a space separated | |
| 535 | * @return | |
| 536 | * An array. If the file extension is not allowed, it will contain an error message. | |
| 537 | */ | |
| 538 | function filefield_validate_extensions($file, $extensions) { | |
| 539 | global $user; | |
| 540 | $errors = array(); | |
| 541 | ||
| 76cc1a41 DP |
542 | if (!empty($extensions)) { |
| 543 | $regex = '/\.('. ereg_replace(' +', '|', preg_quote($extensions)) .')$/i'; | |
| 544 | if (!preg_match($regex, $file->filename)) { | |
| 545 | $errors[] = t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => $extensions)); | |
| 546 | } | |
| d6caa409 | 547 | } |
| 76cc1a41 | 548 | |
| d6caa409 DP |
549 | return $errors; |
| 550 | } | |
| 551 | ||
| 6ce28b69 | 552 | // These three functions return messages for file_validate_size, and file_validate_extensions. |
| 7e784adc | 553 | // They're a neat hack that gets the job done. Even though it's evil to put a |
| 6ce28b69 | 554 | // function into a namespace not owned by your module... |
| d6caa409 | 555 | function filefield_validate_extensions_help($extensions) { |
| 76cc1a41 DP |
556 | if (!empty($extensions)) { |
| 557 | return t('Allowed Extensions: %ext', array('%ext' => $extensions)); | |
| 558 | } | |
| 559 | else { | |
| 560 | return ''; | |
| 561 | } | |
| d6caa409 DP |
562 | } |
| 563 | ||
| f92ea317 DP |
564 | function filefield_validate_size($file, $file_limit = 0, $user_limit = 0) { |
| 565 | global $user; | |
| 566 | ||
| 567 | $errors = array(); | |
| 568 | ||
| 569 | if ($file_limit && $file->filesize > $file_limit) { | |
| 570 | $errors[] = t('The file is %filesize exceeding the maximum file size of %maxsize.', array('%filesize' => format_size($file->filesize), '%maxsize' => format_size($file_limit))); | |
| 571 | } | |
| 572 | ||
| 573 | // Bypass user limits for uid = 1. | |
| 574 | if ($user->uid != 1) { | |
| 575 | $total_size = file_space_used($user->uid) + $file->filesize; | |
| 576 | if ($user_limit && $total_size > $user_limit) { | |
| 577 | $errors[] = t('The file is %filesize which would exceed your disk quota of %quota.', array('%filesize' => format_size($file->filesize), '%quota' => format_size($user_limit))); | |
| 578 | } | |
| 579 | } | |
| 580 | return $errors; | |
| 581 | } | |
| 582 | ||
| 583 | function filefield_validate_size_help($size) { | |
| 584 | return t('Maximum Filesize: %size', array('%size' => format_size(parse_size($size)))); | |
| 585 | } | |
| 586 | ||
| 587 | function filefield_validate_image_resolution(&$file, $maximum_dimensions = 0, $minimum_dimensions = 0) { | |
| 588 | $errors = array(); | |
| 589 | ||
| 590 | // Check first that the file is an image. | |
| 591 | if ($info = image_get_info($file->filepath)) { | |
| 592 | if ($maximum_dimensions) { | |
| 593 | // Check that it is smaller than the given dimensions. | |
| 594 | list($width, $height) = explode('x', $maximum_dimensions); | |
| 595 | if ($info['width'] > $width || $info['height'] > $height) { | |
| 596 | // Try to resize the image to fit the dimensions. | |
| 597 | if (image_get_toolkit() && image_scale($file->filepath, $file->filepath, $width, $height)) { | |
| 598 | drupal_set_message(t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', array('%dimensions' => $maximum_dimensions))); | |
| 599 | ||
| 600 | // Clear the cached filesize and refresh the image information. | |
| 601 | clearstatcache(); | |
| 602 | $info = image_get_info($file->filepath); | |
| 603 | $file->filesize = $info['file_size']; | |
| 604 | } | |
| 605 | else { | |
| 606 | $errors[] = t('The image is too large; the maximum dimensions are %dimensions pixels.', array('%dimensions' => $maximum_dimensions)); | |
| 607 | } | |
| 608 | } | |
| 609 | } | |
| 610 | ||
| 611 | if ($minimum_dimensions) { | |
| 612 | // Check that it is larger than the given dimensions. | |
| 613 | list($width, $height) = explode('x', $minimum_dimensions); | |
| 614 | if ($info['width'] < $width || $info['height'] < $height) { | |
| 615 | $errors[] = t('The image is too small; the minimum dimensions are %dimensions pixels.', array('%dimensions' => $minimum_dimensions)); | |
| 616 | } | |
| 617 | } | |
| 618 | } | |
| 619 | ||
| 620 | return $errors; | |
| d6caa409 | 621 | } |
| 6ce28b69 | 622 | |
| f92ea317 | 623 | function filefield_validate_image_resolution_help($max_size = '0', $min_size = '0') { |
| 6ce28b69 DP |
624 | if (!empty($max_size)) { |
| 625 | if (!empty($min_size)) { | |
| a5811c0f | 626 | if ($max_size == $min_size) { |
| 627 | return t('Images must be exactly @min_size pixels', array('@min_size' => $min_size)); | |
| 628 | } | |
| 629 | else { | |
| 630 | return t('Images must be between @min_size pixels and @max_size', array('@max_size' => $max_size, '@min_size' => $min_size)); | |
| 631 | } | |
| 6ce28b69 DP |
632 | } |
| 633 | else { | |
| 4ece7959 | 634 | if (image_get_toolkit()) { |
| 635 | return t('Images larger than @max_size pixels will be scaled', array('@max_size' => $max_size)); | |
| 636 | } | |
| 637 | else { | |
| 638 | return t('Images must be smaller than @max_size pixels', array('@max_size' => $max_size)); | |
| 639 | } | |
| 6ce28b69 DP |
640 | } |
| 641 | } | |
| 642 | if (!empty($min_size)) { | |
| 4ece7959 | 643 | return t('Images must be larger than @max_size pixels', array('@max_size' => $min_size)); |
| 6ce28b69 DP |
644 | } |
| 645 | } | |
| b7ed071d | 646 | |
| f92ea317 DP |
647 | function filefield_validate_is_image(&$file) { |
| 648 | $errors = array(); | |
| f92ea317 DP |
649 | $info = image_get_info($file->filepath); |
| 650 | if (!$info || empty($info['extension'])) { | |
| 651 | $errors[] = t('Only JPEG, PNG and GIF images are allowed.'); | |
| 652 | } | |
| f92ea317 DP |
653 | return $errors; |
| 654 | } | |
| 655 | ||
| ad6bb31f DP |
656 | /** |
| 657 | * This validation function adds the field to the file object, for later | |
| 658 | * use in field aware modules implementing hook_file. | |
| 659 | */ | |
| 660 | function filefield_validate_associate_field(&$file, $field) { | |
| 661 | $file->field = $field; | |
| 662 | return array(); | |
| 663 | } | |
| f92ea317 | 664 | |
| 5f5eee0e | 665 | function filefield_validate_is_image_help($arguments = null) { |
| 4ece7959 | 666 | return t('Must be a JPEG, PNG or GIF image'); |
| 8ffd8e3d | 667 | } |