| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: upload.module,v 1.197.2.8 2010/12/13 18:40:49 goba Exp $ 3 4 /** 5 * @file 6 * File-handling and attaching files to nodes. 7 * 8 */ 9 10 /** 11 * Implementation of hook_help(). 12 */ 13 function upload_help($path, $arg) { 14 switch ($path) { 15 case 'admin/help#upload': 16 $output = '<p>'. t('The upload module allows users to upload files to the site. The ability to upload files is important for members of a community who want to share work. It is also useful to administrators who want to keep uploaded files connected to posts.') .'</p>'; 17 $output .= '<p>'. t('Users with the upload files permission can upload attachments to posts. Uploads may be enabled for specific content types on the content types settings page. Each user role can be customized to limit or control the file size of uploads, or the maximum dimension of image files.') .'</p>'; 18 $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@upload">Upload module</a>.', array('@upload' => 'http://drupal.org/handbook/modules/upload/')) .'</p>'; 19 return $output; 20 case 'admin/settings/uploads': 21 return '<p>'. t('Users with the <a href="@permissions">upload files permission</a> can upload attachments. Users with the <a href="@permissions">view uploaded files permission</a> can view uploaded attachments. You can choose which post types can take attachments on the <a href="@types">content types settings</a> page.', array('@permissions' => url('admin/user/permissions', array('fragment' => 'module-upload')), '@types' => url('admin/content/types'))) .'</p>'; 22 } 23 } 24 25 /** 26 * Implementation of hook_theme() 27 */ 28 function upload_theme() { 29 return array( 30 'upload_attachments' => array( 31 'arguments' => array('files' => NULL), 32 ), 33 'upload_form_current' => array( 34 'arguments' => array('form' => NULL), 35 ), 36 'upload_form_new' => array( 37 'arguments' => array('form' => NULL), 38 ), 39 ); 40 } 41 42 /** 43 * Implementation of hook_perm(). 44 */ 45 function upload_perm() { 46 return array('upload files', 'view uploaded files'); 47 } 48 49 /** 50 * Implementation of hook_link(). 51 */ 52 function upload_link($type, $node = NULL, $teaser = FALSE) { 53 $links = array(); 54 55 // Display a link with the number of attachments 56 if ($teaser && $type == 'node' && isset($node->files) && user_access('view uploaded files')) { 57 $num_files = 0; 58 foreach ($node->files as $file) { 59 if ($file->list) { 60 $num_files++; 61 } 62 } 63 if ($num_files) { 64 $links['upload_attachments'] = array( 65 'title' => format_plural($num_files, '1 attachment', '@count attachments'), 66 'href' => "node/$node->nid", 67 'attributes' => array('title' => t('Read full article to view attachments.')), 68 'fragment' => 'attachments' 69 ); 70 } 71 } 72 73 return $links; 74 } 75 76 /** 77 * Implementation of hook_menu(). 78 */ 79 function upload_menu() { 80 $items['upload/js'] = array( 81 'page callback' => 'upload_js', 82 'access arguments' => array('upload files'), 83 'type' => MENU_CALLBACK, 84 ); 85 $items['admin/settings/uploads'] = array( 86 'title' => 'File uploads', 87 'description' => 'Control how files may be attached to content.', 88 'page callback' => 'drupal_get_form', 89 'page arguments' => array('upload_admin_settings'), 90 'access arguments' => array('administer site configuration'), 91 'type' => MENU_NORMAL_ITEM, 92 'file' => 'upload.admin.inc', 93 ); 94 return $items; 95 } 96 97 function upload_menu_alter(&$items) { 98 $items['system/files']['access arguments'] = array('view uploaded files'); 99 } 100 101 /** 102 * Determine the limitations on files that a given user may upload. The user 103 * may be in multiple roles so we select the most permissive limitations from 104 * all of their roles. 105 * 106 * @param $user 107 * A Drupal user object. 108 * @return 109 * An associative array with the following keys: 110 * 'extensions' 111 * A white space separated string containing all the file extensions this 112 * user may upload. 113 * 'file_size' 114 * The maximum size of a file upload in bytes. 115 * 'user_size' 116 * The total number of bytes for all for a user's files. 117 * 'resolution' 118 * A string specifying the maximum resolution of images. 119 */ 120 function _upload_file_limits($user) { 121 $file_limit = variable_get('upload_uploadsize_default', 1); 122 $user_limit = variable_get('upload_usersize_default', 1); 123 $all_extensions = explode(' ', variable_get('upload_extensions_default', 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp')); 124 foreach ($user->roles as $rid => $name) { 125 $extensions = variable_get("upload_extensions_$rid", variable_get('upload_extensions_default', 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp')); 126 $all_extensions = array_merge($all_extensions, explode(' ', $extensions)); 127 128 // A zero value indicates no limit, take the least restrictive limit. 129 $file_size = variable_get("upload_uploadsize_$rid", variable_get('upload_uploadsize_default', 1)) * 1024 * 1024; 130 $file_limit = ($file_limit && $file_size) ? max($file_limit, $file_size) : 0; 131 132 $user_size = variable_get("upload_usersize_$rid", variable_get('upload_usersize_default', 1)) * 1024 * 1024; 133 $user_limit = ($user_limit && $user_size) ? max($user_limit, $user_size) : 0; 134 } 135 $all_extensions = implode(' ', array_unique($all_extensions)); 136 return array( 137 'extensions' => $all_extensions, 138 'file_size' => $file_limit, 139 'user_size' => $user_limit, 140 'resolution' => variable_get('upload_max_resolution', 0), 141 ); 142 } 143 144 /** 145 * Implementation of hook_file_download(). 146 */ 147 function upload_file_download($filepath) { 148 $filepath = file_create_path($filepath); 149 $result = db_query("SELECT f.*, u.nid FROM {files} f INNER JOIN {upload} u ON f.fid = u.fid WHERE filepath = '%s'", $filepath); 150 while ($file = db_fetch_object($result)) { 151 if ($filepath !== $file->filepath) { 152 // Since some database servers sometimes use a case-insensitive 153 // comparison by default, double check that the filename is an exact 154 // match. 155 continue; 156 } 157 if (user_access('view uploaded files') && ($node = node_load($file->nid)) && node_access('view', $node)) { 158 return array( 159 'Content-Type: ' . $file->filemime, 160 'Content-Length: ' . $file->filesize, 161 ); 162 } 163 else { 164 return -1; 165 } 166 } 167 } 168 169 /** 170 * Save new uploads and store them in the session to be associated to the node 171 * on upload_save. 172 * 173 * @param $node 174 * A node object to associate with uploaded files. 175 */ 176 function upload_node_form_submit(&$form, &$form_state) { 177 global $user; 178 179 $limits = _upload_file_limits($user); 180 $validators = array( 181 'file_validate_extensions' => array($limits['extensions']), 182 'file_validate_image_resolution' => array($limits['resolution']), 183 'file_validate_size' => array($limits['file_size'], $limits['user_size']), 184 ); 185 186 // Save new file uploads. 187 if (user_access('upload files') && ($file = file_save_upload('upload', $validators, file_directory_path()))) { 188 $file->list = variable_get('upload_list_default', 1); 189 $file->description = $file->filename; 190 $file->weight = 0; 191 $file->new = TRUE; 192 $form['#node']->files[$file->fid] = $file; 193 $form_state['values']['files'][$file->fid] = (array)$file; 194 } 195 196 if (isset($form_state['values']['files'])) { 197 foreach ($form_state['values']['files'] as $fid => $file) { 198 // If the node was previewed prior to saving, $form['#node']->files[$fid] 199 // is an array instead of an object. Convert file to object for compatibility. 200 $form['#node']->files[$fid] = (object) $form['#node']->files[$fid]; 201 $form_state['values']['files'][$fid]['new'] = !empty($form['#node']->files[$fid]->new); 202 } 203 } 204 205 // Order the form according to the set file weight values. 206 if (!empty($form_state['values']['files'])) { 207 $microweight = 0.001; 208 foreach ($form_state['values']['files'] as $fid => $file) { 209 if (is_numeric($fid)) { 210 $form_state['values']['files'][$fid]['#weight'] = $file['weight'] + $microweight; 211 $microweight += 0.001; 212 } 213 } 214 uasort($form_state['values']['files'], 'element_sort'); 215 } 216 } 217 218 function upload_form_alter(&$form, $form_state, $form_id) { 219 if ($form_id == 'node_type_form' && isset($form['identity']['type'])) { 220 $form['workflow']['upload'] = array( 221 '#type' => 'radios', 222 '#title' => t('Attachments'), 223 '#default_value' => variable_get('upload_'. $form['#node_type']->type, 1), 224 '#options' => array(t('Disabled'), t('Enabled')), 225 ); 226 } 227 228 if (isset($form['type']) && isset($form['#node'])) { 229 $node = $form['#node']; 230 if ($form['type']['#value'] .'_node_form' == $form_id && variable_get("upload_$node->type", TRUE)) { 231 // Attachments fieldset 232 $form['attachments'] = array( 233 '#type' => 'fieldset', 234 '#access' => user_access('upload files'), 235 '#title' => t('File attachments'), 236 '#collapsible' => TRUE, 237 '#collapsed' => empty($node->files), 238 '#description' => t('Changes made to the attachments are not permanent until you save this post. The first "listed" file will be included in RSS feeds.'), 239 '#prefix' => '<div class="attachments">', 240 '#suffix' => '</div>', 241 '#weight' => 30, 242 ); 243 244 // Wrapper for fieldset contents (used by ahah.js). 245 $form['attachments']['wrapper'] = array( 246 '#prefix' => '<div id="attach-wrapper">', 247 '#suffix' => '</div>', 248 ); 249 250 // Make sure necessary directories for upload.module exist and are 251 // writable before displaying the attachment form. 252 $path = file_directory_path(); 253 $temp = file_directory_temp(); 254 // Note: pass by reference 255 if (!file_check_directory($path, FILE_CREATE_DIRECTORY) || !file_check_directory($temp, FILE_CREATE_DIRECTORY)) { 256 $form['attachments']['#description'] = t('File attachments are disabled. The file directories have not been properly configured.'); 257 if (user_access('administer site configuration')) { 258 $form['attachments']['#description'] .= ' '. t('Please visit the <a href="@admin-file-system">file system configuration page</a>.', array('@admin-file-system' => url('admin/settings/file-system'))); 259 } 260 else { 261 $form['attachments']['#description'] .= ' '. t('Please contact the site administrator.'); 262 } 263 } 264 else { 265 $form['attachments']['wrapper'] += _upload_form($node); 266 $form['#attributes']['enctype'] = 'multipart/form-data'; 267 } 268 $form['#submit'][] = 'upload_node_form_submit'; 269 } 270 } 271 } 272 273 /** 274 * Implementation of hook_nodeapi(). 275 */ 276 function upload_nodeapi(&$node, $op, $teaser) { 277 switch ($op) { 278 279 case 'load': 280 $output = ''; 281 if (variable_get("upload_$node->type", 1) == 1) { 282 $output['files'] = upload_load($node); 283 return $output; 284 } 285 break; 286 287 case 'view': 288 if (isset($node->files) && user_access('view uploaded files')) { 289 // Add the attachments list to node body with a heavy 290 // weight to ensure they're below other elements 291 if (count($node->files)) { 292 if (!$teaser && user_access('view uploaded files')) { 293 $node->content['files'] = array( 294 '#value' => theme('upload_attachments', $node->files), 295 '#weight' => 50, 296 ); 297 } 298 } 299 } 300 break; 301 302 case 'insert': 303 case 'update': 304 if (user_access('upload files')) { 305 upload_save($node); 306 } 307 break; 308 309 case 'delete': 310 upload_delete($node); 311 break; 312 313 case 'delete revision': 314 upload_delete_revision($node); 315 break; 316 317 case 'search result': 318 return isset($node->files) && is_array($node->files) ? format_plural(count($node->files), '1 attachment', '@count attachments') : NULL; 319 320 case 'rss item': 321 if (is_array($node->files)) { 322 $files = array(); 323 foreach ($node->files as $file) { 324 if ($file->list) { 325 $files[] = $file; 326 } 327 } 328 if (count($files) > 0) { 329 // RSS only allows one enclosure per item 330 $file = array_shift($files); 331 return array( 332 array( 333 'key' => 'enclosure', 334 'attributes' => array( 335 'url' => file_create_url($file->filepath), 336 'length' => $file->filesize, 337 'type' => $file->filemime 338 ) 339 ) 340 ); 341 } 342 } 343 return array(); 344 } 345 } 346 347 /** 348 * Displays file attachments in table 349 * 350 * @ingroup themeable 351 */ 352 function theme_upload_attachments($files) { 353 $header = array(t('Attachment'), t('Size')); 354 $rows = array(); 355 foreach ($files as $file) { 356 $file = (object)$file; 357 if ($file->list && empty($file->remove)) { 358 $href = file_create_url($file->filepath); 359 $text = $file->description ? $file->description : $file->filename; 360 $rows[] = array(l($text, $href), format_size($file->filesize)); 361 } 362 } 363 if (count($rows)) { 364 return theme('table', $header, $rows, array('id' => 'attachments')); 365 } 366 } 367 368 /** 369 * Determine how much disk space is occupied by a user's uploaded files. 370 * 371 * @param $uid 372 * The integer user id of a user. 373 * @return 374 * The amount of disk space used by the user in bytes. 375 */ 376 function upload_space_used($uid) { 377 return file_space_used($uid); 378 } 379 380 /** 381 * Determine how much disk space is occupied by uploaded files. 382 * 383 * @return 384 * The amount of disk space used by uploaded files in bytes. 385 */ 386 function upload_total_space_used() { 387 return db_result(db_query('SELECT SUM(f.filesize) FROM {files} f INNER JOIN {upload} u ON f.fid = u.fid')); 388 } 389 390 function upload_save(&$node) { 391 if (empty($node->files) || !is_array($node->files)) { 392 return; 393 } 394 395 foreach ($node->files as $fid => $file) { 396 // Convert file to object for compatibility 397 $file = (object)$file; 398 399 // Remove file. Process removals first since no further processing 400 // will be required. 401 if (!empty($file->remove)) { 402 db_query('DELETE FROM {upload} WHERE fid = %d AND vid = %d', $fid, $node->vid); 403 404 // If the file isn't used by any other revisions delete it. 405 $count = db_result(db_query('SELECT COUNT(fid) FROM {upload} WHERE fid = %d', $fid)); 406 if ($count < 1) { 407 file_delete($file->filepath); 408 db_query('DELETE FROM {files} WHERE fid = %d', $fid); 409 } 410 411 // Remove it from the session in the case of new uploads, 412 // that you want to disassociate before node submission. 413 unset($node->files[$fid]); 414 // Move on, so the removed file won't be added to new revisions. 415 continue; 416 } 417 418 // Create a new revision, or associate a new file needed. 419 if (!empty($node->old_vid) || $file->new) { 420 db_query("INSERT INTO {upload} (fid, nid, vid, list, description, weight) VALUES (%d, %d, %d, %d, '%s', %d)", $file->fid, $node->nid, $node->vid, $file->list, $file->description, $file->weight); 421 file_set_status($file, FILE_STATUS_PERMANENT); 422 } 423 // Update existing revision. 424 else { 425 db_query("UPDATE {upload} SET list = %d, description = '%s', weight = %d WHERE fid = %d AND vid = %d", $file->list, $file->description, $file->weight, $file->fid, $node->vid); 426 file_set_status($file, FILE_STATUS_PERMANENT); 427 } 428 } 429 } 430 431 function upload_delete($node) { 432 $files = array(); 433 $result = db_query('SELECT DISTINCT f.* FROM {upload} u INNER JOIN {files} f ON u.fid = f.fid WHERE u.nid = %d', $node->nid); 434 while ($file = db_fetch_object($result)) { 435 $files[$file->fid] = $file; 436 } 437 438 foreach ($files as $fid => $file) { 439 // Delete all files associated with the node 440 db_query('DELETE FROM {files} WHERE fid = %d', $fid); 441 file_delete($file->filepath); 442 } 443 444 // Delete all file revision information associated with the node 445 db_query('DELETE FROM {upload} WHERE nid = %d', $node->nid); 446 } 447 448 function upload_delete_revision($node) { 449 if (is_array($node->files)) { 450 foreach ($node->files as $file) { 451 // Check if the file will be used after this revision is deleted 452 $count = db_result(db_query('SELECT COUNT(fid) FROM {upload} WHERE fid = %d', $file->fid)); 453 454 // if the file won't be used, delete it 455 if ($count < 2) { 456 db_query('DELETE FROM {files} WHERE fid = %d', $file->fid); 457 file_delete($file->filepath); 458 } 459 } 460 } 461 462 // delete the revision 463 db_query('DELETE FROM {upload} WHERE vid = %d', $node->vid); 464 } 465 466 function _upload_form($node) { 467 global $user; 468 469 $form = array( 470 '#theme' => 'upload_form_new', 471 '#cache' => TRUE, 472 ); 473 474 if (!empty($node->files) && is_array($node->files)) { 475 $form['files']['#theme'] = 'upload_form_current'; 476 $form['files']['#tree'] = TRUE; 477 foreach ($node->files as $key => $file) { 478 $file = (object)$file; 479 $description = file_create_url($file->filepath); 480 $description = "<small>". check_plain($description) ."</small>"; 481 $form['files'][$key]['description'] = array('#type' => 'textfield', '#default_value' => !empty($file->description) ? $file->description : $file->filename, '#maxlength' => 256, '#description' => $description ); 482 $form['files'][$key]['size'] = array('#value' => format_size($file->filesize)); 483 $form['files'][$key]['remove'] = array('#type' => 'checkbox', '#default_value' => !empty($file->remove)); 484 $form['files'][$key]['list'] = array('#type' => 'checkbox', '#default_value' => $file->list); 485 $form['files'][$key]['weight'] = array('#type' => 'weight', '#delta' => count($node->files), '#default_value' => $file->weight); 486 $form['files'][$key]['filename'] = array('#type' => 'value', '#value' => $file->filename); 487 $form['files'][$key]['filepath'] = array('#type' => 'value', '#value' => $file->filepath); 488 $form['files'][$key]['filemime'] = array('#type' => 'value', '#value' => $file->filemime); 489 $form['files'][$key]['filesize'] = array('#type' => 'value', '#value' => $file->filesize); 490 $form['files'][$key]['fid'] = array('#type' => 'value', '#value' => $file->fid); 491 $form['files'][$key]['new'] = array('#type' => 'value', '#value' => FALSE); 492 } 493 } 494 495 if (user_access('upload files')) { 496 $limits = _upload_file_limits($user); 497 $form['new']['#weight'] = 10; 498 $form['new']['upload'] = array( 499 '#type' => 'file', 500 '#title' => t('Attach new file'), 501 '#size' => 40, 502 '#description' => ($limits['resolution'] ? t('Images are larger than %resolution will be resized. ', array('%resolution' => $limits['resolution'])) : '') . t('The maximum upload size is %filesize. Only files with the following extensions may be uploaded: %extensions. ', array('%extensions' => $limits['extensions'], '%filesize' => format_size($limits['file_size']))), 503 ); 504 $form['new']['attach'] = array( 505 '#type' => 'submit', 506 '#value' => t('Attach'), 507 '#name' => 'attach', 508 '#ahah' => array( 509 'path' => 'upload/js', 510 'wrapper' => 'attach-wrapper', 511 'progress' => array('type' => 'bar', 'message' => t('Please wait...')), 512 ), 513 '#submit' => array('node_form_submit_build_node'), 514 ); 515 } 516 517 return $form; 518 } 519 520 /** 521 * Theme the attachments list. 522 * 523 * @ingroup themeable 524 */ 525 function theme_upload_form_current($form) { 526 $header = array('', t('Delete'), t('List'), t('Description'), t('Weight'), t('Size')); 527 drupal_add_tabledrag('upload-attachments', 'order', 'sibling', 'upload-weight'); 528 529 foreach (element_children($form) as $key) { 530 // Add class to group weight fields for drag and drop. 531 $form[$key]['weight']['#attributes']['class'] = 'upload-weight'; 532 533 $row = array(''); 534 $row[] = drupal_render($form[$key]['remove']); 535 $row[] = drupal_render($form[$key]['list']); 536 $row[] = drupal_render($form[$key]['description']); 537 $row[] = drupal_render($form[$key]['weight']); 538 $row[] = drupal_render($form[$key]['size']); 539 $rows[] = array('data' => $row, 'class' => 'draggable'); 540 } 541 $output = theme('table', $header, $rows, array('id' => 'upload-attachments')); 542 $output .= drupal_render($form); 543 return $output; 544 } 545 546 /** 547 * Theme the attachment form. 548 * Note: required to output prefix/suffix. 549 * 550 * @ingroup themeable 551 */ 552 function theme_upload_form_new($form) { 553 drupal_add_tabledrag('upload-attachments', 'order', 'sibling', 'upload-weight'); 554 $output = drupal_render($form); 555 return $output; 556 } 557 558 function upload_load($node) { 559 $files = array(); 560 561 if ($node->vid) { 562 $result = db_query('SELECT * FROM {files} f INNER JOIN {upload} r ON f.fid = r.fid WHERE r.vid = %d ORDER BY r.weight, f.fid', $node->vid); 563 while ($file = db_fetch_object($result)) { 564 $files[$file->fid] = $file; 565 } 566 } 567 568 return $files; 569 } 570 571 /** 572 * Menu-callback for JavaScript-based uploads. 573 */ 574 function upload_js() { 575 $cached_form_state = array(); 576 $files = array(); 577 578 // Load the form from the Form API cache. 579 if (!($cached_form = form_get_cache($_POST['form_build_id'], $cached_form_state)) || !isset($cached_form['#node']) || !isset($cached_form['attachments'])) { 580 form_set_error('form_token', t('Validation error, please try again. If this error persists, please contact the site administrator.')); 581 $output = theme('status_messages'); 582 print drupal_to_js(array('status' => TRUE, 'data' => $output)); 583 exit(); 584 } 585 586 $form_state = array('values' => $_POST); 587 588 // Handle new uploads, and merge tmp files into node-files. 589 upload_node_form_submit($cached_form, $form_state); 590 591 if(!empty($form_state['values']['files'])) { 592 foreach ($form_state['values']['files'] as $fid => $file) { 593 if (isset($cached_form['#node']->files[$fid])) { 594 $files[$fid] = $cached_form['#node']->files[$fid]; 595 } 596 } 597 } 598 599 $node = $cached_form['#node']; 600 601 $node->files = $files; 602 603 $form = _upload_form($node); 604 605 unset($cached_form['attachments']['wrapper']['new']); 606 $cached_form['attachments']['wrapper'] = array_merge($cached_form['attachments']['wrapper'], $form); 607 608 $cached_form['attachments']['#collapsed'] = FALSE; 609 610 form_set_cache($_POST['form_build_id'], $cached_form, $cached_form_state); 611 612 foreach ($files as $fid => $file) { 613 if (is_numeric($fid)) { 614 $form['files'][$fid]['description']['#default_value'] = $form_state['values']['files'][$fid]['description']; 615 $form['files'][$fid]['list']['#default_value'] = !empty($form_state['values']['files'][$fid]['list']); 616 $form['files'][$fid]['remove']['#default_value'] = !empty($form_state['values']['files'][$fid]['remove']); 617 $form['files'][$fid]['weight']['#default_value'] = $form_state['values']['files'][$fid]['weight']; 618 } 619 } 620 621 // Render the form for output. 622 $form += array( 623 '#post' => $_POST, 624 '#programmed' => FALSE, 625 '#tree' => FALSE, 626 '#parents' => array(), 627 ); 628 drupal_alter('form', $form, array(), 'upload_js'); 629 $form_state = array('submitted' => FALSE); 630 $form = form_builder('upload_js', $form, $form_state); 631 $output = theme('status_messages') . drupal_render($form); 632 633 // We send the updated file attachments form. 634 // Don't call drupal_json(). ahah.js uses an iframe and 635 // the header output by drupal_json() causes problems in some browsers. 636 print drupal_to_js(array('status' => TRUE, 'data' => $output)); 637 exit; 638 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |