2004-08-17 21:35:26 +00:00
<?php
2005-08-11 13:02:08 +00:00
// $Id$
2004-08-17 21:35:26 +00:00
2004-08-21 06:42:38 +00:00
/**
* @file
* File-handling and attaching files to nodes.
*/
2005-03-18 08:05:47 +00:00
/**
* Implementation of hook_help().
*/
2004-08-17 21:35:26 +00:00
function upload_help($section) {
switch ($section) {
2005-11-01 10:17:34 +00:00
case 'admin/help#upload':
$output = '<p>'. t('The upload module allows users to upload files to the site. The ability to upload files to a site 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 a node or page.') .'</p>';
$output .= '<p>'. t('Users with the upload files permission can upload attachments. You can choose which post types can take attachments on the content types settings page. Each user role can be customized for the file size of uploads, and the dimension of image files.') .'</p>';
$output .= t('<p>You can</p>
<ul>
<li>administer user permissions at <a href="%admin-user-configure">administer >> user >> configure >> permissions</a>.</li>
<li>administer content at <a href="%admin-node-configure">administer >> content types</a>.</li>
<li>administer upload at <a href="%admin-settings">administer >> settings</a>.</li>
</ul>
', array('%admin-user-configure' => url('admin/user/configure'), '%admin-node-configure' => url('admin/node/configure'), '%admin-settings' => url('admin/settings')));
$output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="%upload">Upload page</a>.', array('%upload' => 'http://www.drupal.org/handbook/modules/upload/')) .'</p>';
return $output;
2004-08-17 21:35:26 +00:00
case 'admin/modules#description':
2005-04-01 15:55:02 +00:00
return t('Allows users to upload and attach files to content.');
2005-02-08 19:43:02 +00:00
case 'admin/settings/upload':
2005-08-29 19:01:18 +00:00
return t('<p>Users with the <a href="%permissions">upload files permission</a> can upload attachments. You can choose which post types can take attachments on the <a href="%types">content types settings</a> page.</p>', array('%permissions' => url('admin/access'), '%types' => url('admin/settings/content-types')));
2004-08-17 21:35:26 +00:00
}
}
2005-03-18 08:05:47 +00:00
/**
* Implementation of hook_perm().
*/
2004-08-17 21:35:26 +00:00
function upload_perm() {
2004-09-19 22:56:26 +00:00
return array('upload files', 'view uploaded files');
2004-08-17 21:35:26 +00:00
}
2005-03-18 08:05:47 +00:00
/**
* Implementation of hook_link().
*/
function upload_link($type, $node = 0, $main = 0) {
$links = array();
// Display a link with the number of attachments
if ($main && $type == 'node' && $node->files && user_access('view uploaded files')) {
$num_files = 0;
foreach ($node->files as $file) {
if ($file->list) {
$num_files++;
}
}
if ($num_files) {
$links[] = l(format_plural($num_files, '1 attachment', '%count attachments'), "node/$node->nid", array('title' => t('Read full article to view attachments.')), NULL, 'attachments');
}
}
return $links;
}
/**
* Implementation of hook_menu().
*/
2004-09-16 07:17:56 +00:00
function upload_menu($may_cache) {
$items = array();
if ($may_cache) {
- Patch #28483 by Steven: JavaScript enabled uploading.
Comment from Steven: It does this by redirecting the submission of the form to a hidden <iframe> when you click "Attach" (we cannot submit data through Ajax directly because you cannot read file contents from JS for security reasons). Once the file is submitted, the upload-section of the form is updated. Things to note:
* The feature degrades back to the current behaviour without JS.
* If there are errors with the uploaded file (disallowed type, too big, ...), they are displayed at the top of the file attachments fieldset.
* Though the hidden-iframe method sounds dirty, it's quite compact and is 100% implemented in .js files. The drupal.js api makes it a snap to use.
* I included some minor improvements to the Drupal JS API and code.
* I added an API drupal_call_js() to bridge the PHP/JS gap: it takes a function name and arguments, and outputs a <script> tag. The kicker is that it preserves the structure and type of arguments, so e.g. PHP associative arrays end up as objects in JS.
* I also included a progressbar widget that I wrote for drumm's ongoing update.php work. It includes Ajax status updating/monitoring, but it is only used as a pure throbber in this patch. But as the code was already written and is going to be used in the near future, I left that part in. It's pretty small ;). If PHP supports ad-hoc upload info in the future like Ruby on Rails, we can implement that in 5 minutes.
2005-08-31 18:37:30 +00:00
$items[] = array(
'path' => 'upload/js',
'callback' => 'upload_js',
'access' => user_access('upload files'),
'type' => MENU_CALLBACK
);
2004-09-17 18:08:28 +00:00
}
else {
2004-09-16 07:17:56 +00:00
// Add handlers for previewing new uploads.
if ($_SESSION['file_uploads']) {
foreach ($_SESSION['file_uploads'] as $key => $file) {
$filename = file_create_filename($file->filename, file_create_path());
$items[] = array(
'path' => $filename, 'title' => t('file download'),
'callback' => 'upload_download',
2004-09-19 22:56:26 +00:00
'access' => user_access('view uploaded files'),
2004-09-17 18:08:28 +00:00
'type' => MENU_CALLBACK
2004-09-16 07:17:56 +00:00
);
$_SESSION['file_uploads'][$key]->_filename = $filename;
}
2004-08-17 21:35:26 +00:00
}
}
2004-09-16 07:17:56 +00:00
2004-08-17 21:35:26 +00:00
return $items;
}
2005-10-07 06:11:12 +00:00
function upload_settings() {
2005-10-11 19:44:35 +00:00
$form['settings_general'] = array('#type' => 'fieldset', '#title' => t('General settings'));
2005-10-07 06:11:12 +00:00
$form['settings_general']['upload_max_resolution'] = array(
2005-10-11 19:44:35 +00:00
'#type' => 'textfield', '#title' => t('Maximum resolution for uploaded images'), '#default_value' => variable_get('upload_max_resolution', 0),
'#size' => 15, '#maxlength' => 10, '#description' => t('The maximum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Set to 0 for no restriction.')
2005-10-07 06:11:12 +00:00
);
2004-08-17 21:35:26 +00:00
$roles = user_roles(0, 'upload files');
foreach ($roles as $rid => $role) {
2005-10-11 19:44:35 +00:00
$form["settings_role_$rid"] = array('#type' => 'fieldset', '#title' => t('Settings for %role', array('%role' => theme('placeholder', $role))), '#collapsible' => TRUE, '#collapsed' => TRUE);
2005-10-07 06:11:12 +00:00
$form["settings_role_$rid"]["upload_extensions_$rid"] = array(
2005-10-11 19:44:35 +00:00
'#type' => 'textfield', '#title' => t('Permitted file extensions'), '#default_value' => variable_get("upload_extensions_$rid", "jpg jpeg gif png txt html doc xls pdf ppt pps"),
2005-11-12 11:26:16 +00:00
'#maxlength' => 255, '#description' => t('Extensions that users in this role can upload. Separate extensions with a space and do not include the leading dot.')
2005-10-07 06:11:12 +00:00
);
$form["settings_role_$rid"]["upload_uploadsize_$rid"] = array(
2005-10-11 19:44:35 +00:00
'#type' => 'textfield', '#title' => t('Maximum file size per upload'), '#default_value' => variable_get("upload_uploadsize_$rid", 1),
'#size' => 5, '#maxlength' => 5, '#description' => t('The maximum size of a file a user can upload (in megabytes).')
2005-10-07 06:11:12 +00:00
);
$form["settings_role_$rid"]["upload_usersize_$rid"] = array(
2005-10-11 19:44:35 +00:00
'#type' => 'textfield', '#title' => t('Total file size per user'), '#default_value' => variable_get("upload_usersize_$rid", 10),
'#size' => 5, '#maxlength' => 5, '#description' => t('The maximum size of all files a user can have on the site (in megabytes).')
2005-10-07 06:11:12 +00:00
);
2004-08-17 21:35:26 +00:00
}
2005-10-07 06:11:12 +00:00
return $form;
2004-08-17 21:35:26 +00:00
}
function upload_download() {
foreach ($_SESSION['file_uploads'] as $file) {
if ($file->_filename == $_GET['q']) {
file_transfer($file->filepath, array('Content-Type: '. $file->filemime, 'Content-Length: '. $file->filesize));
}
}
}
function upload_file_download($file) {
2004-09-19 22:56:26 +00:00
if (user_access('view uploaded files')) {
$file = file_create_path($file);
2005-05-14 18:15:23 +00:00
$result = db_query(db_rewrite_sql("SELECT f.nid, f.* FROM {files} f WHERE filepath = '%s'", 'f'), $file);
2004-09-19 22:56:26 +00:00
if ($file = db_fetch_object($result)) {
$name = mime_header_encode($file->filename);
// Serve images and text inline for the browser to display rather than download.
$disposition = ereg('^(text/|image/)', $file->filemime) ? 'inline' : 'attachment';
return array('Content-Type: '. $file->filemime .'; name='. $name,
'Content-Length: '. $file->filesize,
'Content-Disposition: '. $disposition .'; filename='. $name);
}
2004-08-17 21:35:26 +00:00
}
}
2005-03-18 08:05:47 +00:00
/**
* Implementation of hook_nodeapi().
*/
2004-08-17 21:35:26 +00:00
function upload_nodeapi(&$node, $op, $arg) {
switch ($op) {
case 'settings':
2005-10-07 06:11:12 +00:00
$form['upload_'. $node->type] = array(
2005-10-11 19:44:35 +00:00
'#type' => 'radios', '#title' => t('Attachments'), '#default_value' => variable_get('upload_'. $node->type, 1),
'#options' => array(t('Disabled'), t('Enabled'))
2005-10-07 06:11:12 +00:00
);
return $form;
2004-08-17 21:35:26 +00:00
case 'validate':
$node->files = upload_load($node);
// Double check existing files:
if (is_array($node->list)) {
foreach ($node->list as $key => $value) {
if ($file = file_check_upload($key)) {
$node->files[$file->source] = $file;
$node->files[$key]->list = $node->list[$key];
$node->files[$key]->remove = $node->remove[$key];
2005-09-27 15:54:39 +00:00
$node->files[$key]->description = $node->description[$key];
2004-08-17 21:35:26 +00:00
if ($file->source) {
$filesize += $file->filesize;
}
}
}
}
else {
foreach ($node->files as $key => $file) {
$node->list[$key] = $file->list;
}
}
2004-08-18 21:55:39 +00:00
if (($file = file_check_upload('upload')) && user_access('upload files')) {
2004-08-17 21:35:26 +00:00
global $user;
2005-02-01 16:27:43 +00:00
$file = _upload_image($file);
2004-09-13 19:14:32 +00:00
// Don't do any checks for uid #1.
if ($user->uid != 1) {
// Validate file against all users roles. Only denies an upload when
// all roles prevent it.
2005-07-22 19:06:19 +00:00
$total_usersize = upload_space_used($user->uid) + $filesize;
2004-09-13 19:14:32 +00:00
foreach ($user->roles as $rid => $name) {
$extensions = variable_get("upload_extensions_$rid", 'jpg jpeg gif png txt html doc xls pdf ppt pps');
2005-05-25 04:27:55 +00:00
$uploadsize = variable_get("upload_uploadsize_$rid", 1) * 1024 * 1024;
$usersize = variable_get("upload_usersize_$rid", 1) * 1024 * 1024;
2004-09-13 19:14:32 +00:00
$regex = '/\.('. ereg_replace(' +', '|', preg_quote($extensions)) .')$/i';
if (!preg_match($regex, $file->filename)) {
$error['extension']++;
}
2004-08-17 21:35:26 +00:00
2005-07-22 19:06:19 +00:00
if ($uploadsize && $file->filesize > $uploadsize) {
2004-09-13 19:14:32 +00:00
$error['uploadsize']++;
}
2004-08-17 21:35:26 +00:00
2005-07-22 19:06:19 +00:00
if ($usersize && $total_usersize + $file->filesize > $usersize) {
2004-09-13 19:14:32 +00:00
$error['usersize']++;
}
2004-08-17 21:35:26 +00:00
}
2004-09-13 19:14:32 +00:00
}
2004-08-17 21:35:26 +00:00
2004-09-13 19:14:32 +00:00
// Rename possibly executable scripts to prevent accidental execution.
// Uploaded files are attachments and should be shown in their original
// form, rather than run.
if (preg_match('/\.(php|pl|py|cgi|asp)$/i', $file->filename)) {
$file->filename .= '.txt';
$file->filemime = 'text/plain';
2004-08-17 21:35:26 +00:00
}
2004-08-19 15:41:57 +00:00
2004-08-18 21:55:39 +00:00
if ($error['extension'] == count($user->roles) && $user->uid != 1) {
2005-07-22 19:06:19 +00:00
form_set_error('upload', t('The selected file %name can not be attached to this post, because it is only possible to attach files with the following extensions: %files-allowed.', array('%name' => theme('placeholder', $file->filename), '%files-allowed' => theme('placeholder', $extensions))));
2004-08-17 21:35:26 +00:00
}
2004-08-18 21:55:39 +00:00
elseif ($error['uploadsize'] == count($user->roles) && $user->uid != 1) {
2005-07-22 19:06:19 +00:00
form_set_error('upload', t('The selected file %name can not be attached to this post, because it exceeded the maximum filesize of %maxsize.', array('%name' => theme('placeholder', $file->filename), '%maxsize' => theme('placeholder', format_size($uploadsize)))));
2004-08-17 21:35:26 +00:00
}
2004-08-18 21:55:39 +00:00
elseif ($error['usersize'] == count($user->roles) && $user->uid != 1) {
2005-07-22 19:06:19 +00:00
form_set_error('upload', t('The selected file %name can not be attached to this post, because the disk quota of %quota has been reached.', array('%name' => theme('placeholder', $file->filename), '%quota' => theme('placeholder', format_size($usersize)))));
2004-08-17 21:35:26 +00:00
}
else {
$key = 'upload_'. count($_SESSION['file_uploads']);
$file->source = $key;
$file->list = 1;
$file = file_save_upload($file);
$node->files[$key] = $file;
}
}
2005-10-07 06:11:12 +00:00
for ($x = 0; $x < count($_SESSION['file_uploads']); $x++) {
$key = 'upload_' . $x;
if ($file = file_check_upload($key)) {
$node->files[$key] = $file;
2005-10-07 06:51:43 +00:00
}
2005-10-07 06:11:12 +00:00
}
2004-08-17 21:35:26 +00:00
break;
2005-01-24 21:20:16 +00:00
2005-10-07 06:11:12 +00:00
case 'form':
2004-08-18 21:55:39 +00:00
if (variable_get("upload_$node->type", 1) == 1 && user_access('upload files')) {
2004-08-17 21:35:26 +00:00
$output = upload_form($node);
2005-10-11 19:44:35 +00:00
$output['#attributes'] = array('enctype' => 'multipart/form-data');
2004-08-17 21:35:26 +00:00
}
break;
2005-01-24 21:20:16 +00:00
2004-08-17 21:35:26 +00:00
case 'load':
2004-09-19 22:56:26 +00:00
if (variable_get("upload_$node->type", 1) == 1) {
2004-08-24 19:21:30 +00:00
$output['files'] = upload_load($node);
2004-08-17 21:35:26 +00:00
}
break;
2005-01-24 21:20:16 +00:00
2004-08-17 21:35:26 +00:00
case 'view':
2004-09-19 22:56:26 +00:00
if ($node->files && user_access('view uploaded files')) {
2004-08-19 15:41:57 +00:00
$header = array(t('Attachment'), t('Size'));
2004-08-17 21:35:26 +00:00
$rows = array();
$previews = array();
// Build list of attached files
foreach ($node->files as $file) {
if ($file->list) {
$rows[] = array(
2005-09-27 15:54:39 +00:00
'<a href="'. check_url(($file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path())))) .'">'. check_plain($file->description ? $file->description : $file->filename) .'</a>',
2004-08-17 21:35:26 +00:00
format_size($file->filesize)
);
// We save the list of files still in preview for later
if (!$file->fid) {
$previews[] = $file;
}
}
}
// URLs to files being previewed are actually Drupal paths. When Clean
// URLs are disabled, the two do not match. We perform an automatic
// replacement from temporary to permanent URLs. That way, the author
// can use the final URL in the body before having actually saved (to
// place inline images for example).
if (!variable_get('clean_url', 0)) {
foreach ($previews as $file) {
$old = file_create_filename($file->filename, file_create_path());
$new = url($old);
$node->body = str_replace($old, $new, $node->body);
$node->teaser = str_replace($old, $new, $node->teaser);
}
}
$teaser = $arg;
// Add the attachments list
if (count($rows) && !$teaser) {
2005-03-18 08:05:47 +00:00
$node->body .= theme('table', $header, $rows, array('id' => 'attachments'));
2004-08-17 21:35:26 +00:00
}
}
break;
2005-01-24 21:20:16 +00:00
2004-08-17 21:35:26 +00:00
case 'insert':
case 'update':
2004-08-18 21:55:39 +00:00
if (user_access('upload files')) {
upload_save($node);
}
2004-08-17 21:35:26 +00:00
break;
2005-01-24 21:20:16 +00:00
2004-08-17 21:35:26 +00:00
case 'delete':
upload_delete($node);
break;
2004-12-31 09:30:12 +00:00
case 'search result':
return $node->files ? format_plural(count($node->files), '1 attachment', '%count attachments') : null;
2005-02-01 14:09:31 +00:00
case 'rss item':
2005-02-07 14:16:27 +00:00
if ($node->files) {
$files = array();
foreach ($node->files as $file) {
if ($file->list) {
$files[] = $file;
}
}
if (count($files) > 0) {
// RSS only allows one enclosure per item
$file = array_shift($files);
return array(array('key' => 'enclosure',
'attributes' => array('url' => file_create_url($file->filepath),
'length' => $file->filesize,
'type' => $file->filemime)));
2005-02-01 14:09:31 +00:00
}
}
break;
2004-08-17 21:35:26 +00:00
}
return $output;
}
2005-07-22 19:06:19 +00:00
/**
* Determine how much disk space is occupied by a user's uploaded files.
*
* @param $uid
* The integer user id of a user.
* @return
* The ammount of disk space used by the user in bytes.
*/
function upload_space_used($uid) {
2005-08-30 15:22:29 +00:00
return db_result(db_query('SELECT SUM(f.filesize) FROM {files} f INNER JOIN {node_revisions} n ON f.vid = n.vid WHERE uid = %d', $uid));
2005-07-22 19:06:19 +00:00
}
2004-08-17 21:35:26 +00:00
2005-07-22 19:06:19 +00:00
/**
* Determine how much disk space is occupied by uploaded files.
*
* @return
* The ammount of disk space used by uploaded files in bytes.
*/
function upload_total_space_used() {
2005-08-30 15:22:29 +00:00
return db_result(db_query('SELECT SUM(f.filesize) FROM {files} f INNER JOIN {node_revisions} n ON f.vid = n.vid'));
2004-08-17 21:35:26 +00:00
}
function upload_save($node) {
2004-09-05 09:15:18 +00:00
foreach ((array)$node->files as $key => $file) {
2004-08-17 21:35:26 +00:00
if ($file->source && !$file->remove) {
// Clean up the session:
unset($_SESSION['file_uploads'][$file->source]);
2004-10-20 16:57:35 +00:00
// Insert new files:
if ($file = file_save_upload($file, $file->filename)) {
$fid = db_next_id('{files}_fid');
2005-09-27 15:54:39 +00:00
db_query("INSERT INTO {files} (fid, nid, vid, filename, filepath, filemime, filesize, list, description) VALUES (%d, %d, %d, '%s', '%s', '%s', %d, %d, '%s')",
$fid, $node->nid, $node->vid, $file->filename, $file->filepath, $file->filemime, $file->filesize, $node->list[$key], $file->description);
2004-10-20 16:57:35 +00:00
}
2004-08-17 21:35:26 +00:00
}
2005-08-30 15:22:29 +00:00
}
// Remove or update existing files:
foreach ((array)$node->remove as $key => $value) {
if ($node->remove[$key]) {
db_query('DELETE FROM {files} WHERE fid = %d AND vid = %d', $key, $node->vid);
// We only delete a file if it isn't used anymore by any revision.
$count = db_result(db_query('SELECT COUNT(fid) FROM {files} WHERE fid = %d', $key));
if (!($count > 0)) {
2004-08-17 21:35:26 +00:00
file_delete($file->filepath);
}
2005-08-30 15:22:29 +00:00
}
}
foreach ((array)$node->list as $key => $value) {
if (!$node->remove[$key]) {
2005-09-27 15:54:39 +00:00
db_query('UPDATE {files} SET list = %d, description = \'%s\' WHERE fid = %d AND vid = %d', $node->list[$key], $node->description[$key], $key, $node->vid);
2005-08-30 15:22:29 +00:00
}
}
if ($node->old_vid) {
foreach ((array)$node->remove as $key => $remove) {
if (!$remove) {
$file = db_fetch_object(db_query('SELECT * FROM {files} WHERE vid = %d AND fid = %d', $node->old_vid, $key));
2005-09-27 15:54:39 +00:00
db_query("INSERT INTO {files} (fid, nid, vid, filename, filepath, filemime, filesize, list, description) VALUES (%d, %d, %d, '%s', '%s', '%s', %d, %d, '%s')",
$key, $node->nid, $node->vid, $file->filename, $file->filepath, $file->filemime, $file->filesize, $file->list, $file->description);
2004-08-17 21:35:26 +00:00
}
}
}
return;
}
function upload_delete($node) {
$node->files = upload_load($node);
foreach ($node->files as $file) {
file_delete($file->filepath);
}
db_query("DELETE FROM {files} WHERE nid = %d", $node->nid);
}
function upload_form($node) {
- Patch #28483 by Steven: JavaScript enabled uploading.
Comment from Steven: It does this by redirecting the submission of the form to a hidden <iframe> when you click "Attach" (we cannot submit data through Ajax directly because you cannot read file contents from JS for security reasons). Once the file is submitted, the upload-section of the form is updated. Things to note:
* The feature degrades back to the current behaviour without JS.
* If there are errors with the uploaded file (disallowed type, too big, ...), they are displayed at the top of the file attachments fieldset.
* Though the hidden-iframe method sounds dirty, it's quite compact and is 100% implemented in .js files. The drupal.js api makes it a snap to use.
* I included some minor improvements to the Drupal JS API and code.
* I added an API drupal_call_js() to bridge the PHP/JS gap: it takes a function name and arguments, and outputs a <script> tag. The kicker is that it preserves the structure and type of arguments, so e.g. PHP associative arrays end up as objects in JS.
* I also included a progressbar widget that I wrote for drumm's ongoing update.php work. It includes Ajax status updating/monitoring, but it is only used as a pure throbber in this patch. But as the code was already written and is going to be used in the near future, I left that part in. It's pretty small ;). If PHP supports ad-hoc upload info in the future like Ruby on Rails, we can implement that in 5 minutes.
2005-08-31 18:37:30 +00:00
drupal_add_js('misc/progress.js');
drupal_add_js('misc/upload.js');
2005-10-07 06:11:12 +00:00
$form['attachments'] = array(
2005-10-11 19:44:35 +00:00
'#type' => 'fieldset', '#title' => t('File attachments'), '#collapsible' => TRUE, '#collapsed' => empty($node->files),
'#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.'),
2005-10-28 14:04:20 +00:00
'#prefix' => '<div class="attachments">', '#suffix' => '</div>'
2005-10-07 06:11:12 +00:00
);
$form['attachments'] += _upload_form($node);
- Patch #28483 by Steven: JavaScript enabled uploading.
Comment from Steven: It does this by redirecting the submission of the form to a hidden <iframe> when you click "Attach" (we cannot submit data through Ajax directly because you cannot read file contents from JS for security reasons). Once the file is submitted, the upload-section of the form is updated. Things to note:
* The feature degrades back to the current behaviour without JS.
* If there are errors with the uploaded file (disallowed type, too big, ...), they are displayed at the top of the file attachments fieldset.
* Though the hidden-iframe method sounds dirty, it's quite compact and is 100% implemented in .js files. The drupal.js api makes it a snap to use.
* I included some minor improvements to the Drupal JS API and code.
* I added an API drupal_call_js() to bridge the PHP/JS gap: it takes a function name and arguments, and outputs a <script> tag. The kicker is that it preserves the structure and type of arguments, so e.g. PHP associative arrays end up as objects in JS.
* I also included a progressbar widget that I wrote for drumm's ongoing update.php work. It includes Ajax status updating/monitoring, but it is only used as a pure throbber in this patch. But as the code was already written and is going to be used in the near future, I left that part in. It's pretty small ;). If PHP supports ad-hoc upload info in the future like Ruby on Rails, we can implement that in 5 minutes.
2005-08-31 18:37:30 +00:00
2005-10-07 06:11:12 +00:00
return $form;
- Patch #28483 by Steven: JavaScript enabled uploading.
Comment from Steven: It does this by redirecting the submission of the form to a hidden <iframe> when you click "Attach" (we cannot submit data through Ajax directly because you cannot read file contents from JS for security reasons). Once the file is submitted, the upload-section of the form is updated. Things to note:
* The feature degrades back to the current behaviour without JS.
* If there are errors with the uploaded file (disallowed type, too big, ...), they are displayed at the top of the file attachments fieldset.
* Though the hidden-iframe method sounds dirty, it's quite compact and is 100% implemented in .js files. The drupal.js api makes it a snap to use.
* I included some minor improvements to the Drupal JS API and code.
* I added an API drupal_call_js() to bridge the PHP/JS gap: it takes a function name and arguments, and outputs a <script> tag. The kicker is that it preserves the structure and type of arguments, so e.g. PHP associative arrays end up as objects in JS.
* I also included a progressbar widget that I wrote for drumm's ongoing update.php work. It includes Ajax status updating/monitoring, but it is only used as a pure throbber in this patch. But as the code was already written and is going to be used in the near future, I left that part in. It's pretty small ;). If PHP supports ad-hoc upload info in the future like Ruby on Rails, we can implement that in 5 minutes.
2005-08-31 18:37:30 +00:00
}
function _upload_form($node) {
2005-09-27 15:54:39 +00:00
$header = array(t('Delete'), t('List'), t('Description'), t('Size'));
2004-08-17 21:35:26 +00:00
$rows = array();
- Patch #28483 by Steven: JavaScript enabled uploading.
Comment from Steven: It does this by redirecting the submission of the form to a hidden <iframe> when you click "Attach" (we cannot submit data through Ajax directly because you cannot read file contents from JS for security reasons). Once the file is submitted, the upload-section of the form is updated. Things to note:
* The feature degrades back to the current behaviour without JS.
* If there are errors with the uploaded file (disallowed type, too big, ...), they are displayed at the top of the file attachments fieldset.
* Though the hidden-iframe method sounds dirty, it's quite compact and is 100% implemented in .js files. The drupal.js api makes it a snap to use.
* I included some minor improvements to the Drupal JS API and code.
* I added an API drupal_call_js() to bridge the PHP/JS gap: it takes a function name and arguments, and outputs a <script> tag. The kicker is that it preserves the structure and type of arguments, so e.g. PHP associative arrays end up as objects in JS.
* I also included a progressbar widget that I wrote for drumm's ongoing update.php work. It includes Ajax status updating/monitoring, but it is only used as a pure throbber in this patch. But as the code was already written and is going to be used in the near future, I left that part in. It's pretty small ;). If PHP supports ad-hoc upload info in the future like Ruby on Rails, we can implement that in 5 minutes.
2005-08-31 18:37:30 +00:00
$output = '';
2004-08-17 21:35:26 +00:00
2005-10-11 19:44:35 +00:00
$form['#theme'] = 'upload_form_new';
2005-10-07 06:11:12 +00:00
if (is_array($node->files) && count($node->files)) {
2005-10-11 19:44:35 +00:00
$form['current']['#theme'] = 'upload_form_current';
$form['current']['description']['#tree'] = TRUE;
2004-08-17 21:35:26 +00:00
foreach ($node->files as $key => $file) {
2005-10-07 06:11:12 +00:00
$options[$key] = '';
if ($file->remove) {
$remove[] = $key;
}
if ($file->list) {
$list[] = $key;
}
$description = "<small>". file_create_url(($file->fid ? $file->filepath : file_create_filename($file->filename, file_create_path()))) ."</small>";
2005-11-12 11:26:16 +00:00
$form['current']['description'][$key] = array('#type' => 'textfield', '#default_value' => $file->description ? $file->description : $file->filename, '#maxlength' => 256, '#description' => $description );
2005-10-11 19:44:35 +00:00
$form['current']['size'][$key] = array('#type' => 'markup', '#value' => format_size($file->filesize));
2004-08-17 21:35:26 +00:00
}
2005-10-11 19:44:35 +00:00
$form['current']['remove'] = array('#type' => 'checkboxes', '#options' => $options, '#default_value' => $remove);
$form['current']['list'] = array('#type' => 'checkboxes', '#options' => $options, '#default_value' => $list);
$form['files'][$key] = array('#type' => 'hidden', '#value' => 1);
2004-08-17 21:35:26 +00:00
}
2005-08-17 19:27:03 +00:00
2004-08-18 21:55:39 +00:00
if (user_access('upload files')) {
2005-10-07 06:11:12 +00:00
2005-10-11 19:44:35 +00:00
$form['new']['upload'] = array('#type' => 'file', '#title' => t('Attach new file'), '#size' => 40);
$form['new']['fileop'] = array('#type' => 'button', '#value' => t('Attach'), '#name'=> 'fileop', '#attributes' => array('id' => 'fileop'));
- Patch #28483 by Steven: JavaScript enabled uploading.
Comment from Steven: It does this by redirecting the submission of the form to a hidden <iframe> when you click "Attach" (we cannot submit data through Ajax directly because you cannot read file contents from JS for security reasons). Once the file is submitted, the upload-section of the form is updated. Things to note:
* The feature degrades back to the current behaviour without JS.
* If there are errors with the uploaded file (disallowed type, too big, ...), they are displayed at the top of the file attachments fieldset.
* Though the hidden-iframe method sounds dirty, it's quite compact and is 100% implemented in .js files. The drupal.js api makes it a snap to use.
* I included some minor improvements to the Drupal JS API and code.
* I added an API drupal_call_js() to bridge the PHP/JS gap: it takes a function name and arguments, and outputs a <script> tag. The kicker is that it preserves the structure and type of arguments, so e.g. PHP associative arrays end up as objects in JS.
* I also included a progressbar widget that I wrote for drumm's ongoing update.php work. It includes Ajax status updating/monitoring, but it is only used as a pure throbber in this patch. But as the code was already written and is going to be used in the near future, I left that part in. It's pretty small ;). If PHP supports ad-hoc upload info in the future like Ruby on Rails, we can implement that in 5 minutes.
2005-08-31 18:37:30 +00:00
// The class triggers the js upload behaviour.
2005-10-11 19:44:35 +00:00
$form['fileop'] = array('#type' => 'hidden', '#value' => url('upload/js', NULL, NULL, TRUE), '#attributes' => array('class' => 'upload'));
2004-08-18 21:55:39 +00:00
}
2004-08-17 21:35:26 +00:00
2005-10-07 06:11:12 +00:00
return $form;
}
function theme_upload_form_new($form) {
$output .= '<div id="fileop-wrapper">' . "\n";
$output .= '<div id="fileop-hide">' . "\n";
$output .= form_render($form) . "\n";
$output .= "</div>\n";
$output .= "</div>\n";
return $output;
}
function theme_upload_form_current(&$form) {
$header = array(t('Delete'), t('List'), t('Description'), t('Size'));
foreach (element_children($form['description']) as $key) {
$row = array();
$row[] = form_render($form['remove'][$key]);
$row[] = form_render($form['list'][$key]);
$row[] = form_render($form['description'][$key]);
$row[] = form_render($form['size'][$key]);
$rows[] = $row;
}
$output = theme('table', $header, $rows);
$output .= form_render($form);
- Patch #28483 by Steven: JavaScript enabled uploading.
Comment from Steven: It does this by redirecting the submission of the form to a hidden <iframe> when you click "Attach" (we cannot submit data through Ajax directly because you cannot read file contents from JS for security reasons). Once the file is submitted, the upload-section of the form is updated. Things to note:
* The feature degrades back to the current behaviour without JS.
* If there are errors with the uploaded file (disallowed type, too big, ...), they are displayed at the top of the file attachments fieldset.
* Though the hidden-iframe method sounds dirty, it's quite compact and is 100% implemented in .js files. The drupal.js api makes it a snap to use.
* I included some minor improvements to the Drupal JS API and code.
* I added an API drupal_call_js() to bridge the PHP/JS gap: it takes a function name and arguments, and outputs a <script> tag. The kicker is that it preserves the structure and type of arguments, so e.g. PHP associative arrays end up as objects in JS.
* I also included a progressbar widget that I wrote for drumm's ongoing update.php work. It includes Ajax status updating/monitoring, but it is only used as a pure throbber in this patch. But as the code was already written and is going to be used in the near future, I left that part in. It's pretty small ;). If PHP supports ad-hoc upload info in the future like Ruby on Rails, we can implement that in 5 minutes.
2005-08-31 18:37:30 +00:00
return $output;
2004-08-17 21:35:26 +00:00
}
function upload_load($node) {
$files = array();
2005-08-30 15:22:29 +00:00
if ($node->vid) {
$result = db_query("SELECT * FROM {files} WHERE vid = %d", $node->vid);
2004-08-17 21:35:26 +00:00
while ($file = db_fetch_object($result)) {
$files[$file->fid] = $file;
}
}
return $files;
}
2005-02-01 16:27:43 +00:00
/**
* Check an upload, if it is an image, make sure it fits within the
* maximum dimensions allowed.
*/
function _upload_image($file) {
$info = image_get_info($file->filepath);
if ($info) {
list($width, $height) = explode('x', variable_get('upload_max_resolution', 0));
if ($width && $height) {
$result = image_scale($file->filepath, $file->filepath, $width, $height);
if ($result) {
$file->filesize = filesize($file->filepath);
2005-05-05 22:22:46 +00:00
drupal_set_message(t('The image was resized to fit within the maximum allowed resolution of %resolution pixels.', array('%resolution' => theme('placeholder', variable_get('upload_max_resolution', 0)))));
2005-02-01 16:27:43 +00:00
}
}
}
return $file;
}
- Patch #28483 by Steven: JavaScript enabled uploading.
Comment from Steven: It does this by redirecting the submission of the form to a hidden <iframe> when you click "Attach" (we cannot submit data through Ajax directly because you cannot read file contents from JS for security reasons). Once the file is submitted, the upload-section of the form is updated. Things to note:
* The feature degrades back to the current behaviour without JS.
* If there are errors with the uploaded file (disallowed type, too big, ...), they are displayed at the top of the file attachments fieldset.
* Though the hidden-iframe method sounds dirty, it's quite compact and is 100% implemented in .js files. The drupal.js api makes it a snap to use.
* I included some minor improvements to the Drupal JS API and code.
* I added an API drupal_call_js() to bridge the PHP/JS gap: it takes a function name and arguments, and outputs a <script> tag. The kicker is that it preserves the structure and type of arguments, so e.g. PHP associative arrays end up as objects in JS.
* I also included a progressbar widget that I wrote for drumm's ongoing update.php work. It includes Ajax status updating/monitoring, but it is only used as a pure throbber in this patch. But as the code was already written and is going to be used in the near future, I left that part in. It's pretty small ;). If PHP supports ad-hoc upload info in the future like Ruby on Rails, we can implement that in 5 minutes.
2005-08-31 18:37:30 +00:00
/**
* Menu-callback for JavaScript-based uploads.
*/
function upload_js() {
// We only do the upload.module part of the node validation process.
$node = array2object($_POST['edit']);
2005-09-02 02:11:41 +00:00
upload_nodeapi($node, 'validate', NULL);
2005-10-07 06:11:12 +00:00
$form = _upload_form($node);
2005-11-13 02:00:37 +00:00
$form = _form_builder('upload_js', $form);
2005-10-07 06:11:12 +00:00
$output = theme('status_messages') . form_render($form);
- Patch #28483 by Steven: JavaScript enabled uploading.
Comment from Steven: It does this by redirecting the submission of the form to a hidden <iframe> when you click "Attach" (we cannot submit data through Ajax directly because you cannot read file contents from JS for security reasons). Once the file is submitted, the upload-section of the form is updated. Things to note:
* The feature degrades back to the current behaviour without JS.
* If there are errors with the uploaded file (disallowed type, too big, ...), they are displayed at the top of the file attachments fieldset.
* Though the hidden-iframe method sounds dirty, it's quite compact and is 100% implemented in .js files. The drupal.js api makes it a snap to use.
* I included some minor improvements to the Drupal JS API and code.
* I added an API drupal_call_js() to bridge the PHP/JS gap: it takes a function name and arguments, and outputs a <script> tag. The kicker is that it preserves the structure and type of arguments, so e.g. PHP associative arrays end up as objects in JS.
* I also included a progressbar widget that I wrote for drumm's ongoing update.php work. It includes Ajax status updating/monitoring, but it is only used as a pure throbber in this patch. But as the code was already written and is going to be used in the near future, I left that part in. It's pretty small ;). If PHP supports ad-hoc upload info in the future like Ruby on Rails, we can implement that in 5 minutes.
2005-08-31 18:37:30 +00:00
// We send the updated file attachments form.
print drupal_call_js('window.parent.iframeHandler', $output);
exit;
}