Mercurial > defr > drupal > core
comparison modules/upload/upload.module @ 9:acef7ccb09b5 6.4
Drupal 6.4
| author | Franck Deroche <webmaster@defr.org> |
|---|---|
| date | Tue, 23 Dec 2008 14:32:08 +0100 |
| parents | c1f4ac30525a |
| children | 589fb7c02327 |
comparison
equal
deleted
inserted
replaced
| 8:85cbd6048071 | 9:acef7ccb09b5 |
|---|---|
| 1 <?php | 1 <?php |
| 2 // $Id: upload.module,v 1.197.2.1 2008/02/11 15:08:09 goba Exp $ | 2 // $Id: upload.module,v 1.197.2.2 2008/08/13 23:59:14 drumm Exp $ |
| 3 | 3 |
| 4 /** | 4 /** |
| 5 * @file | 5 * @file |
| 6 * File-handling and attaching files to nodes. | 6 * File-handling and attaching files to nodes. |
| 7 * | 7 * |
| 142 } | 142 } |
| 143 | 143 |
| 144 /** | 144 /** |
| 145 * Implementation of hook_file_download(). | 145 * Implementation of hook_file_download(). |
| 146 */ | 146 */ |
| 147 function upload_file_download($file) { | 147 function upload_file_download($filepath) { |
| 148 if (!user_access('view uploaded files')) { | 148 $filepath = file_create_path($filepath); |
| 149 return -1; | 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 } | |
| 151 $file = file_create_path($file); | |
| 152 $result = db_query("SELECT f.* FROM {files} f INNER JOIN {upload} u ON f.fid = u.fid WHERE filepath = '%s'", $file); | |
| 153 if ($file = db_fetch_object($result)) { | 150 if ($file = db_fetch_object($result)) { |
| 154 return array( | 151 if (user_access('view uploaded files') && ($node = node_load($file->nid)) && node_access('view', $node)) { |
| 155 'Content-Type: '. $file->filemime, | 152 return array( |
| 156 'Content-Length: '. $file->filesize, | 153 'Content-Type: ' . $file->filemime, |
| 157 ); | 154 'Content-Length: ' . $file->filesize, |
| 155 ); | |
| 156 } | |
| 157 else { | |
| 158 return -1; | |
| 159 } | |
| 158 } | 160 } |
| 159 } | 161 } |
| 160 | 162 |
| 161 /** | 163 /** |
| 162 * Save new uploads and store them in the session to be associated to the node | 164 * Save new uploads and store them in the session to be associated to the node |
| 163 * on upload_save. | 165 * on upload_save. |
| 164 * | 166 * |
| 165 * @param $node | 167 * @param $node |
| 166 * A node object to associate with uploaded files. | 168 * A node object to associate with uploaded files. |
| 167 */ | 169 */ |
| 168 function upload_node_form_submit($form, &$form_state) { | 170 function upload_node_form_submit(&$form, &$form_state) { |
| 169 global $user; | 171 global $user; |
| 170 | 172 |
| 171 $limits = _upload_file_limits($user); | 173 $limits = _upload_file_limits($user); |
| 172 $validators = array( | 174 $validators = array( |
| 173 'file_validate_extensions' => array($limits['extensions']), | 175 'file_validate_extensions' => array($limits['extensions']), |
| 178 // Save new file uploads. | 180 // Save new file uploads. |
| 179 if (($user->uid != 1 || user_access('upload files')) && ($file = file_save_upload('upload', $validators, file_directory_path()))) { | 181 if (($user->uid != 1 || user_access('upload files')) && ($file = file_save_upload('upload', $validators, file_directory_path()))) { |
| 180 $file->list = variable_get('upload_list_default', 1); | 182 $file->list = variable_get('upload_list_default', 1); |
| 181 $file->description = $file->filename; | 183 $file->description = $file->filename; |
| 182 $file->weight = 0; | 184 $file->weight = 0; |
| 183 $_SESSION['upload_files'][$file->fid] = $file; | 185 $file->new = TRUE; |
| 184 } | 186 $form['#node']->files[$file->fid] = $file; |
| 185 | 187 $form_state['values']['files'][$file->fid] = (array)$file; |
| 186 // Attach session files to node. | 188 } |
| 187 if (!empty($_SESSION['upload_files'])) { | 189 |
| 188 foreach ($_SESSION['upload_files'] as $fid => $file) { | 190 if (isset($form_state['values']['files'])) { |
| 189 if (!isset($form_state['values']['files'][$fid]['filepath'])) { | 191 foreach ($form_state['values']['files'] as $fid => $file) { |
| 190 $form_state['values']['files'][$fid] = (array)$file; | 192 $form_state['values']['files'][$fid]['new'] = !empty($form['#node']->files[$fid]->new); |
| 191 } | |
| 192 } | 193 } |
| 193 } | 194 } |
| 194 | 195 |
| 195 // Order the form according to the set file weight values. | 196 // Order the form according to the set file weight values. |
| 196 if (!empty($form_state['values']['files'])) { | 197 if (!empty($form_state['values']['files'])) { |
| 284 '#value' => theme('upload_attachments', $node->files), | 285 '#value' => theme('upload_attachments', $node->files), |
| 285 '#weight' => 50, | 286 '#weight' => 50, |
| 286 ); | 287 ); |
| 287 } | 288 } |
| 288 } | 289 } |
| 289 } | |
| 290 break; | |
| 291 | |
| 292 case 'prepare': | |
| 293 // Initialize $_SESSION['upload_files'] if no post occurred. | |
| 294 // This clears the variable from old forms and makes sure it | |
| 295 // is an array to prevent notices and errors in other parts | |
| 296 // of upload.module. | |
| 297 if (!$_POST) { | |
| 298 $_SESSION['upload_files'] = array(); | |
| 299 } | 290 } |
| 300 break; | 291 break; |
| 301 | 292 |
| 302 case 'insert': | 293 case 'insert': |
| 303 case 'update': | 294 case 'update': |
| 408 db_query('DELETE FROM {files} WHERE fid = %d', $fid); | 399 db_query('DELETE FROM {files} WHERE fid = %d', $fid); |
| 409 } | 400 } |
| 410 | 401 |
| 411 // Remove it from the session in the case of new uploads, | 402 // Remove it from the session in the case of new uploads, |
| 412 // that you want to disassociate before node submission. | 403 // that you want to disassociate before node submission. |
| 413 unset($_SESSION['upload_files'][$fid]); | 404 unset($node->files[$fid]); |
| 414 // Move on, so the removed file won't be added to new revisions. | 405 // Move on, so the removed file won't be added to new revisions. |
| 415 continue; | 406 continue; |
| 416 } | 407 } |
| 417 | 408 |
| 418 // Create a new revision, or associate a new file needed. | 409 // Create a new revision, or associate a new file needed. |
| 419 if (!empty($node->old_vid) || isset($_SESSION['upload_files'][$fid])) { | 410 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); | 411 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); | 412 file_set_status($file, FILE_STATUS_PERMANENT); |
| 422 } | 413 } |
| 423 // Update existing revision. | 414 // Update existing revision. |
| 424 else { | 415 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); | 416 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); | 417 file_set_status($file, FILE_STATUS_PERMANENT); |
| 427 } | 418 } |
| 428 } | 419 } |
| 429 // Empty the session storage after save. We use this variable to track files | |
| 430 // that haven't been related to the node yet. | |
| 431 unset($_SESSION['upload_files']); | |
| 432 } | 420 } |
| 433 | 421 |
| 434 function upload_delete($node) { | 422 function upload_delete($node) { |
| 435 $files = array(); | 423 $files = array(); |
| 436 $result = db_query('SELECT DISTINCT f.* FROM {upload} u INNER JOIN {files} f ON u.fid = f.fid WHERE u.nid = %d', $node->nid); | 424 $result = db_query('SELECT DISTINCT f.* FROM {upload} u INNER JOIN {files} f ON u.fid = f.fid WHERE u.nid = %d', $node->nid); |
| 489 $form['files'][$key]['filename'] = array('#type' => 'value', '#value' => $file->filename); | 477 $form['files'][$key]['filename'] = array('#type' => 'value', '#value' => $file->filename); |
| 490 $form['files'][$key]['filepath'] = array('#type' => 'value', '#value' => $file->filepath); | 478 $form['files'][$key]['filepath'] = array('#type' => 'value', '#value' => $file->filepath); |
| 491 $form['files'][$key]['filemime'] = array('#type' => 'value', '#value' => $file->filemime); | 479 $form['files'][$key]['filemime'] = array('#type' => 'value', '#value' => $file->filemime); |
| 492 $form['files'][$key]['filesize'] = array('#type' => 'value', '#value' => $file->filesize); | 480 $form['files'][$key]['filesize'] = array('#type' => 'value', '#value' => $file->filesize); |
| 493 $form['files'][$key]['fid'] = array('#type' => 'value', '#value' => $file->fid); | 481 $form['files'][$key]['fid'] = array('#type' => 'value', '#value' => $file->fid); |
| 482 $form['files'][$key]['new'] = array('#type' => 'value', '#value' => FALSE); | |
| 494 } | 483 } |
| 495 } | 484 } |
| 496 | 485 |
| 497 if (user_access('upload files')) { | 486 if (user_access('upload files')) { |
| 498 $limits = _upload_file_limits($user); | 487 $limits = _upload_file_limits($user); |
| 514 ), | 503 ), |
| 515 '#submit' => array('node_form_submit_build_node'), | 504 '#submit' => array('node_form_submit_build_node'), |
| 516 ); | 505 ); |
| 517 } | 506 } |
| 518 | 507 |
| 519 // This value is used in upload_js(). | |
| 520 $form['current']['vid'] = array('#type' => 'hidden', '#value' => isset($node->vid) ? $node->vid : 0); | |
| 521 return $form; | 508 return $form; |
| 522 } | 509 } |
| 523 | 510 |
| 524 /** | 511 /** |
| 525 * Theme the attachments list. | 512 * Theme the attachments list. |
| 574 | 561 |
| 575 /** | 562 /** |
| 576 * Menu-callback for JavaScript-based uploads. | 563 * Menu-callback for JavaScript-based uploads. |
| 577 */ | 564 */ |
| 578 function upload_js() { | 565 function upload_js() { |
| 566 $cached_form_state = array(); | |
| 567 $files = array(); | |
| 568 | |
| 579 // Load the form from the Form API cache. | 569 // Load the form from the Form API cache. |
| 580 $cache = cache_get('form_'. $_POST['form_build_id'], 'cache_form'); | 570 if (!($cached_form = form_get_cache($_POST['form_build_id'], $cached_form_state)) || !isset($cached_form['#node']) || !isset($cached_form['attachments'])) { |
| 581 | 571 form_set_error('form_token', t('Validation error, please try again. If this error persists, please contact the site administrator.')); |
| 582 // We only do the upload.module part of the node validation process. | 572 $output = theme('status_messages'); |
| 583 $node = (object)$_POST; | 573 print drupal_to_js(array('status' => TRUE, 'data' => $output)); |
| 584 unset($node->files['upload']); | 574 exit(); |
| 585 $form = $cache->data; | 575 } |
| 576 | |
| 586 $form_state = array('values' => $_POST); | 577 $form_state = array('values' => $_POST); |
| 587 | 578 |
| 588 // Handle new uploads, and merge tmp files into node-files. | 579 // Handle new uploads, and merge tmp files into node-files. |
| 589 upload_node_form_submit($form, $form_state); | 580 upload_node_form_submit($cached_form, $form_state); |
| 590 $node_files = upload_load($node); | 581 |
| 591 if (!empty($form_state['values']['files'])) { | 582 if(!empty($form_state['values']['files'])) { |
| 592 foreach ($form_state['values']['files'] as $fid => $file) { | 583 foreach ($form_state['values']['files'] as $fid => $file) { |
| 593 if (is_numeric($fid)) { | 584 if (isset($cached_form['#node']->files[$fid])) { |
| 594 $node->files[$fid] = $file; | 585 $files[$fid] = $cached_form['#node']->files[$fid]; |
| 595 if (!isset($file['filepath'])) { | 586 } |
| 596 $node->files[$fid] = $node_files[$fid]; | 587 } |
| 597 } | 588 } |
| 598 } | 589 |
| 599 } | 590 $node = $cached_form['#node']; |
| 600 } | 591 |
| 592 $node->files = $files; | |
| 593 | |
| 601 $form = _upload_form($node); | 594 $form = _upload_form($node); |
| 602 | 595 |
| 603 // Update the default values changed in the $_POST array. | 596 unset($cached_form['attachments']['wrapper']['new']); |
| 604 $files = isset($_POST['files']) ? $_POST['files'] : array(); | 597 $cached_form['attachments']['wrapper'] = array_merge($cached_form['attachments']['wrapper'], $form); |
| 598 | |
| 599 $cached_form['attachments']['#collapsed'] = FALSE; | |
| 600 | |
| 601 form_set_cache($_POST['form_build_id'], $cached_form, $cached_form_state); | |
| 602 | |
| 605 foreach ($files as $fid => $file) { | 603 foreach ($files as $fid => $file) { |
| 606 if (is_numeric($fid)) { | 604 if (is_numeric($fid)) { |
| 607 $form['files'][$fid]['description']['#default_value'] = $file['description']; | 605 $form['files'][$fid]['description']['#default_value'] = $form_state['values']['files'][$fid]['description']; |
| 608 $form['files'][$fid]['list']['#default_value'] = isset($file['list']) ? 1 : 0; | 606 $form['files'][$fid]['list']['#default_value'] = !empty($form_state['values']['files'][$fid]['list']); |
| 609 $form['files'][$fid]['remove']['#default_value'] = isset($file['remove']) ? 1 : 0; | 607 $form['files'][$fid]['remove']['#default_value'] = !empty($form_state['values']['files'][$fid]['remove']); |
| 610 $form['files'][$fid]['weight']['#default_value'] = $file['weight']; | 608 $form['files'][$fid]['weight']['#default_value'] = $form_state['values']['files'][$fid]['weight']; |
| 611 } | 609 } |
| 612 } | 610 } |
| 613 | |
| 614 // Add the new element to the stored form state and resave. | |
| 615 $cache->data['attachments']['wrapper'] = array_merge($cache->data['attachments']['wrapper'], $form); | |
| 616 cache_set('form_'. $_POST['form_build_id'], $cache->data, 'cache_form', $cache->expire); | |
| 617 | 611 |
| 618 // Render the form for output. | 612 // Render the form for output. |
| 619 $form += array( | 613 $form += array( |
| 620 '#post' => $_POST, | 614 '#post' => $_POST, |
| 621 '#programmed' => FALSE, | 615 '#programmed' => FALSE, |
