<?php
function upload_help($section) {
switch ($section) {
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 .= '<p>'. t('For more information please read the configuration and customization handbook <a href="@upload">Upload page</a>.', array('@upload' => 'http://drupal.org/handbook/modules/upload/')) .'</p>';
return $output;
case 'admin/settings/upload':
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/access'), '@types' => url('admin/settings/types'))) .'</p>';
}
}
function upload_perm() {
return array('upload files', 'view uploaded files');
}
function upload_link($type, $node = NULL, $teaser = FALSE) {
$links = array();
if ($teaser && $type == 'node' && isset($node->files) && user_access('view uploaded files')) {
$num_files = 0;
foreach ($node->files as $file) {
if ($file->list) {
$num_files++;
}
}
if ($num_files) {
$links['upload_attachments'] = array(
'title' => format_plural($num_files, '1 attachment', '@count attachments'),
'href' => "node/$node->nid",
'attributes' => array('title' => t('Read full article to view attachments.')),
'fragment' => 'attachments'
);
}
}
return $links;
}
function upload_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'upload/js',
'callback' => 'upload_js',
'access' => user_access('upload files'),
'type' => MENU_CALLBACK
);
$items[] = array('path' => 'admin/settings/uploads',
'title' => t('File uploads'),
'description' => t('Control how files may be attached to content.'),
'callback' => 'drupal_get_form',
'callback arguments' => array('upload_admin_settings'),
'access' => user_access('administer site configuration'),
'type' => MENU_NORMAL_ITEM);
}
else {
if (isset($_SESSION['file_previews'])) {
foreach ($_SESSION['file_previews'] as $fid => $file) {
$filename = file_create_filename($file->filename, file_create_path());
if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) {
if (strpos($filename, file_directory_path()) !== FALSE) {
$filename = trim(substr($filename, strlen(file_directory_path())), '\\/');
}
$filename = 'system/files/' . $filename;
}
$items[] = array(
'path' => $filename, 'title' => t('File download'),
'callback' => 'upload_download',
'access' => user_access('view uploaded files'),
'type' => MENU_CALLBACK
);
$_SESSION['file_previews'][$fid]->_filename = $filename;
}
}
}
return $items;
}
function upload_admin_settings_validate($form_id, $form_values) {
if (($form_values['upload_max_resolution'] != '0')) {
if (!preg_match('/^[0-9]+x[0-9]+$/', $form_values['upload_max_resolution'])) {
form_set_error('upload_max_resolution', t('The maximum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Set to 0 for no restriction.'));
}
}
$default_uploadsize = $form_values['upload_uploadsize_default'];
$default_usersize = $form_values['upload_usersize_default'];
$exceed_max_msg = t('Your PHP settings limit the maximum file size per upload to %size.', array('%size' => format_size(file_upload_max_size()))).'<br/>';
$more_info = t("Depending on your sever environment, these settings may be changed in the system-wide php.ini file, a php.ini file in your Drupal root directory, in your Drupal site's settings.php file, or in the .htaccess file in your Drupal root directory.");
if (!is_numeric($default_uploadsize) || ($default_uploadsize <= 0)) {
form_set_error('upload_uploadsize_default', t('The %role file size limit must be a number and greater than zero.', array('%role' => t('default'))));
}
if (!is_numeric($default_usersize) || ($default_usersize <= 0)) {
form_set_error('upload_usersize_default', t('The %role file size limit must be a number and greater than zero.', array('%role' => t('default'))));
}
if ($default_uploadsize * 1024 * 1024 > file_upload_max_size()) {
form_set_error('upload_uploadsize_default', $exceed_max_msg . $more_info);
$more_info = '';
}
if ($default_uploadsize > $default_usersize) {
form_set_error('upload_uploadsize_default', t('The %role maximum file size per upload is greater than the total file size allowed per user', array('%role' => t('default'))));
}
foreach ($form_values['roles'] as $rid => $role) {
$uploadsize = $form_values['upload_uploadsize_'. $rid];
$usersize = $form_values['upload_usersize_'. $rid];
if (!is_numeric($uploadsize) || ($uploadsize <= 0)) {
form_set_error('upload_uploadsize_'. $rid, t('The %role file size limit must be a number and greater than zero.', array('%role' => $role)));
}
if (!is_numeric($usersize) || ($usersize <= 0)) {
form_set_error('upload_usersize_'. $rid, t('The %role file size limit must be a number and greater than zero.', array('%role' => $role)));
}
if ($uploadsize * 1024 * 1024 > file_upload_max_size()) {
form_set_error('upload_uploadsize_'. $rid, $exceed_max_msg . $more_info);
$more_info = '';
}
if ($uploadsize > $usersize) {
form_set_error('upload_uploadsize_'. $rid, t('The %role maximum file size per upload is greater than the total file size allowed per user', array('%role' => $role)));
}
}
}
function upload_admin_settings() {
$upload_extensions_default = variable_get('upload_extensions_default', 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp');
$upload_uploadsize_default = variable_get('upload_uploadsize_default', 1);
$upload_usersize_default = variable_get('upload_usersize_default', 1);
$form['settings_general'] = array(
'#type' => 'fieldset',
'#title' => t('General settings'),
'#collapsible' => TRUE,
);
$form['settings_general']['upload_max_resolution'] = array(
'#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 (e.g. 640x480). Set to 0 for no restriction.'),
'#field_suffix' => '<kbd>'. t('WIDTHxHEIGHT') .'</kbd>'
);
$form['settings_general']['upload_list_default'] = array(
'#type' => 'select',
'#title' => t('List files by default'),
'#default_value' => variable_get('upload_list_default', 1),
'#options' => array(0 => t('No'), 1 => t('Yes')),
'#description' => t('Set whether files attached to nodes are listed or not in the node view by default.'),
);
$form['settings_general']['upload_extensions_default'] = array(
'#type' => 'textfield',
'#title' => t('Default permitted file extensions'),
'#default_value' => $upload_extensions_default,
'#maxlength' => 255,
'#description' => t('Default extensions that users can upload. Separate extensions with a space and do not include the leading dot.'),
);
$form['settings_general']['upload_uploadsize_default'] = array(
'#type' => 'textfield',
'#title' => t('Default maximum file size per upload'),
'#default_value' => $upload_uploadsize_default,
'#size' => 5,
'#maxlength' => 5,
'#description' => t('The default maximum file size a user can upload.'),
'#field_suffix' => t('MB')
);
$form['settings_general']['upload_usersize_default'] = array(
'#type' => 'textfield',
'#title' => t('Default total file size per user'),
'#default_value' => $upload_usersize_default,
'#size' => 5,
'#maxlength' => 5,
'#description' => t('The default maximum size of all files a user can have on the site.'),
'#field_suffix' => t('MB')
);
$form['settings_general']['upload_max_size'] = array('#value' => '<p>'. t('Your PHP settings limit the maximum file size per upload to %size.', array('%size' => format_size(file_upload_max_size()))).'</p>');
$roles = user_roles(0, 'upload files');
$form['roles'] = array('#type' => 'value', '#value' => $roles);
foreach ($roles as $rid => $role) {
$form['settings_role_'. $rid] = array(
'#type' => 'fieldset',
'#title' => t('Settings for @role', array('@role' => $role)),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['settings_role_'. $rid]['upload_extensions_'. $rid] = array(
'#type' => 'textfield',
'#title' => t('Permitted file extensions'),
'#default_value' => variable_get('upload_extensions_'. $rid, $upload_extensions_default),
'#maxlength' => 255,
'#description' => t('Extensions that users in this role can upload. Separate extensions with a space and do not include the leading dot.'),
);
$form['settings_role_'. $rid]['upload_uploadsize_'. $rid] = array(
'#type' => 'textfield',
'#title' => t('Maximum file size per upload'),
'#default_value' => variable_get('upload_uploadsize_'. $rid, $upload_uploadsize_default),
'#size' => 5,
'#maxlength' => 5,
'#description' => t('The maximum size of a file a user can upload (in megabytes).'),
);
$form['settings_role_'. $rid]['upload_usersize_'. $rid] = array(
'#type' => 'textfield',
'#title' => t('Total file size per user'),
'#default_value' => variable_get('upload_usersize_'. $rid, $upload_usersize_default),
'#size' => 5,
'#maxlength' => 5,
'#description' => t('The maximum size of all files a user can have on the site (in megabytes).'),
);
}
return system_settings_form($form);
}
function upload_download() {
foreach ($_SESSION['file_previews'] as $file) {
if ($file->_filename == $_GET['q']) {
file_transfer($file->filepath, array('Content-Type: '. mime_header_encode($file->filemime), 'Content-Length: '. $file->filesize));
}
}
}
function upload_file_download($file) {
$file = file_create_path($file);
$result = db_query("SELECT f.* FROM {files} f WHERE filepath = '%s'", $file);
if ($file = db_fetch_object($result)) {
if (user_access('view uploaded files')) {
$node = node_load($file->nid);
if (node_access('view', $node)) {
$type = mime_header_encode($file->filemime);
return array(
'Content-Type: '. $type,
'Content-Length: '. $file->filesize,
);
}
else {
return -1;
}
}
else {
return -1;
}
}
}
function _upload_prepare(&$node) {
if(count($_POST) == 0) {
if (is_array($_SESSION['file_previews']) && count($_SESSION['file_previews'])) {
foreach ($_SESSION['file_previews'] as $fid => $file) {
file_delete($file->filepath);
}
unset($_SESSION['file_previews']);
}
}
unset($_SESSION['file_current_upload']);
global $user;
if (($file = file_check_upload()) && user_access('upload files')) {
$file = _upload_image($file);
$key = 'upload_'. count($_SESSION['file_previews']);
$file->fid = $key;
$file->source = $key;
$file->list = variable_get('upload_list_default',1);
$_SESSION['file_previews'][$key] = $file;
$_SESSION['file_current_upload'] = $key;
}
if (is_array($_SESSION['file_previews']) && count($_SESSION['file_previews'])) {
foreach ($_SESSION['file_previews'] as $fid => $file) {
if ($user->uid != 1) {
$file->filename = upload_munge_filename($file->filename, NULL, 0);
$file->description = $file->filename;
}
$node->files[$fid] = $file;
}
}
}
function upload_form_alter($form_id, &$form) {
if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
$form['workflow']['upload'] = array(
'#type' => 'radios',
'#title' => t('Attachments'),
'#default_value' => variable_get('upload_'. $form['#node_type']->type, 1),
'#options' => array(t('Disabled'), t('Enabled')),
);
}
if (isset($form['type'])) {
$node = $form['#node'];
if ($form['type']['#value'] .'_node_form' == $form_id && variable_get("upload_$node->type", TRUE)) {
drupal_add_js('misc/progress.js');
drupal_add_js('misc/upload.js');
$form['attachments'] = array(
'#type' => 'fieldset',
'#access' => user_access('upload files'),
'#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.'),
'#prefix' => '<div class="attachments">',
'#suffix' => '</div>',
'#weight' => 30,
);
$form['attachments']['wrapper'] = array(
'#prefix' => '<div id="attach-wrapper">',
'#suffix' => '</div>',
);
$path = file_directory_path();
$temp = file_directory_temp();
if (!file_check_directory($path, FILE_CREATE_DIRECTORY) || !file_check_directory($temp, FILE_CREATE_DIRECTORY)) {
$form['attachments']['#description'] = t('File attachments are disabled. The file directories have not been properly configured.');
if (user_access('administer site configuration')) {
$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')));
}
else {
$form['attachments']['#description'] .= ' '. t('Please contact the site administrator.');
}
}
else {
$form['attachments']['wrapper'] += _upload_form($node);
$form['#attributes']['enctype'] = 'multipart/form-data';
}
}
}
}
function _upload_validate(&$node) {
$filesize = 0;
if (is_array($node->files)) {
foreach ($node->files as $fid => $file) {
$file = (object)$file;
if (strpos($fid, 'upload') !== FALSE && !$file->remove) {
global $user;
if ($user->uid != 1) {
$filesize += $file->filesize;
$total_usersize = upload_space_used($user->uid) + $filesize;
$error = array();
foreach ($user->roles as $rid => $name) {
$extensions = variable_get("upload_extensions_$rid", variable_get('upload_extensions_default', 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp'));
$uploadsize = variable_get("upload_uploadsize_$rid", variable_get('upload_uploadsize_default', 1)) * 1024 * 1024;
$usersize = variable_get("upload_usersize_$rid", variable_get('upload_usersize_default', 1)) * 1024 * 1024;
$regex = '/\.('. ereg_replace(' +', '|', preg_quote($extensions)) .')$/i';
if (!preg_match($regex, $file->filename)) {
$error['extension']++;
}
if ($uploadsize && $file->filesize > $uploadsize) {
$error['uploadsize']++;
}
if ($usersize && $total_usersize + $file->filesize > $usersize) {
$error['usersize']++;
}
}
$user_roles = count($user->roles);
$valid = TRUE;
if ($error['extension'] == $user_roles) {
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' => $file->filename, '%files-allowed' => $extensions)));
$valid = FALSE;
}
elseif ($error['uploadsize'] == $user_roles) {
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' => $file->filename, '%maxsize' => format_size($uploadsize))));
$valid = FALSE;
}
elseif ($error['usersize'] == $user_roles) {
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' => $file->filename, '%quota' => format_size($usersize))));
$valid = FALSE;
}
elseif (strlen($file->filename) > 255) {
form_set_error('upload', t('The selected file %name can not be attached to this post, because the filename is too long.', array('%name' => $file->filename)));
$valid = FALSE;
}
if (!$valid) {
unset($node->files[$fid], $_SESSION['file_previews'][$fid]);
file_delete($file->filepath);
}
}
}
}
}
}
function upload_nodeapi(&$node, $op, $teaser) {
switch ($op) {
case 'load':
$output = '';
if (variable_get("upload_$node->type", 1) == 1) {
$output['files'] = upload_load($node);
return $output;
}
break;
case 'prepare':
_upload_prepare($node);
break;
case 'validate':
_upload_validate($node);
break;
case 'view':
if (isset($node->files) && user_access('view uploaded files')) {
if (count($node->files)) {
if (!$teaser && user_access('view uploaded files')) {
$node->content['files'] = array(
'#value' => theme('upload_attachments', $node->files),
'#weight' => 50,
);
}
}
}
break;
case 'alter':
if (isset($node->files) && user_access('view uploaded files')) {
if (!variable_get('clean_url', 0)) {
$previews = array();
foreach ($node->files as $file) {
if (strpos($file->fid, 'upload') !== FALSE) {
$previews[] = $file;
}
}
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);
}
}
}
break;
case 'insert':
case 'update':
if (user_access('upload files')) {
upload_save($node);
}
break;
case 'delete':
upload_delete($node);
break;
case 'delete revision':
upload_delete_revision($node);
break;
case 'search result':
return is_array($node->files) ? format_plural(count($node->files), '1 attachment', '@count attachments') : NULL;
case 'rss item':
if (is_array($node->files)) {
$files = array();
foreach ($node->files as $file) {
if ($file->list) {
$files[] = $file;
}
}
if (count($files) > 0) {
$file = array_shift($files);
return array(
array(
'key' => 'enclosure',
'attributes' => array(
'url' => file_create_url($file->filepath),
'length' => $file->filesize,
'type' => $file->filemime
)
)
);
}
}
return array();
}
}
function theme_upload_attachments($files) {
$header = array(t('Attachment'), t('Size'));
$rows = array();
foreach ($files as $file) {
$file = (object)$file;
if ($file->list && !$file->remove) {
$href = file_create_url((strpos($file->fid, 'upload') === FALSE ? $file->filepath : file_create_filename($file->filename, file_create_path())));
$text = $file->description ? $file->description : $file->filename;
$rows[] = array(l($text, $href), format_size($file->filesize));
}
}
if (count($rows)) {
return theme('table', $header, $rows, array('id' => 'attachments'));
}
}
function upload_space_used($uid) {
return db_result(db_query('SELECT SUM(filesize) FROM {files} f INNER JOIN {node} n ON f.nid = n.nid WHERE n.uid = %d', $uid));
}
function upload_total_space_used() {
return db_result(db_query('SELECT SUM(filesize) FROM {files}'));
}
function upload_munge_filename($filename, $extensions = NULL, $alerts = 1) {
global $user;
$original = $filename;
if (!variable_get('allow_insecure_uploads', 0)) {
if (!isset($extensions)) {
$extensions = '';
foreach ($user->roles as $rid => $name) {
$extensions .= ' '. variable_get("upload_extensions_$rid", variable_get('upload_extensions_default', 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp'));
}
}
$whitelist = array_unique(explode(' ', trim($extensions)));
$filename_parts = explode('.', $filename);
$new_filename = array_shift($filename_parts); $final_extension = array_pop($filename_parts);
foreach ($filename_parts as $filename_part) {
$new_filename .= ".$filename_part";
if (!in_array($filename_part, $whitelist) && preg_match("/^[a-zA-Z]{2,5}\d?$/", $filename_part)) {
$new_filename .= '_';
}
}
$filename = "$new_filename.$final_extension";
}
if ($alerts && $original != $filename) {
$message = t('Your filename has been renamed to conform to site policy.');
drupal_set_message($message);
}
return $filename;
}
function upload_unmunge_filename($filename) {
return str_replace('_.', '.', $filename);
}
function upload_save(&$node) {
if (!is_array($node->files)) {
return;
}
foreach ($node->files as $fid => $file) {
$file = (object)$file;
if ($file->remove) {
if (strpos($file->fid, 'upload') !== FALSE) {
file_delete($file->filepath);
}
else {
db_query('DELETE FROM {file_revisions} WHERE fid = %d AND vid = %d', $fid, $node->vid);
$count = db_result(db_query('SELECT COUNT(fid) FROM {file_revisions} WHERE fid = %d', $fid));
if ($count < 1) {
db_query('DELETE FROM {files} WHERE fid = %d', $fid);
file_delete($file->filepath);
}
}
}
elseif (strpos($file->fid, 'upload') !== FALSE) {
if ($file = file_save_upload($file, $file->filename)) {
$file->fid = db_next_id('{files}_fid');
db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', %d)", $file->fid, $node->nid, $file->filename, $file->filepath, $file->filemime, $file->filesize);
db_query("INSERT INTO {file_revisions} (fid, vid, list, description) VALUES (%d, %d, %d, '%s')", $file->fid, $node->vid, $file->list, $file->description);
$node->files[$fid] = $file;
}
unset($_SESSION['file_previews'][$fid]);
}
elseif ($node->old_vid && is_numeric($fid)) {
db_query("INSERT INTO {file_revisions} (fid, vid, list, description) VALUES (%d, %d, %d, '%s')", $file->fid, $node->vid, $file->list, $file->description);
}
else {
db_query("UPDATE {file_revisions} SET list = %d, description = '%s' WHERE fid = %d AND vid = %d", $file->list, $file->description, $file->fid, $node->vid);
}
}
}
function upload_delete($node) {
$files = array();
$result = db_query('SELECT * FROM {files} WHERE nid = %d', $node->nid);
while ($file = db_fetch_object($result)) {
$files[$file->fid] = $file;
}
foreach ($files as $fid => $file) {
db_query('DELETE FROM {file_revisions} WHERE fid = %d', $fid);
file_delete($file->filepath);
}
db_query('DELETE FROM {files} WHERE nid = %d', $node->nid);
}
function upload_delete_revision($node) {
if (is_array($node->files)) {
foreach ($node->files as $file) {
$count = db_result(db_query('SELECT COUNT(fid) FROM {file_revisions} WHERE fid = %d', $file->fid));
if ($count < 2) {
db_query('DELETE FROM {files} WHERE fid = %d', $file->fid);
file_delete($file->filepath);
}
}
}
db_query('DELETE FROM {file_revisions} WHERE vid = %d', $node->vid);
}
function _upload_form($node) {
$form['#theme'] = 'upload_form_new';
if (is_array($node->files) && count($node->files)) {
$form['files']['#theme'] = 'upload_form_current';
$form['files']['#tree'] = TRUE;
foreach ($node->files as $key => $file) {
$description = file_create_url((strpos($file->fid, 'upload') === FALSE ? $file->filepath : file_create_filename($file->filename, file_create_path())));
$description = "<small>". check_plain($description) ."</small>";
$form['files'][$key]['description'] = array('#type' => 'textfield', '#default_value' => (strlen($file->description)) ? $file->description : $file->filename, '#maxlength' => 256, '#description' => $description );
$form['files'][$key]['size'] = array('#value' => format_size($file->filesize));
$form['files'][$key]['remove'] = array('#type' => 'checkbox', '#default_value' => $file->remove);
$form['files'][$key]['list'] = array('#type' => 'checkbox', '#default_value' => $file->list);
if ($_SESSION['file_current_upload'] == $file->fid) {
$form['files'][$key]['list']['#value'] = variable_get('upload_list_default',1);
}
$form['files'][$key]['filename'] = array('#type' => 'value', '#value' => $file->filename);
$form['files'][$key]['filepath'] = array('#type' => 'value', '#value' => $file->filepath);
$form['files'][$key]['filemime'] = array('#type' => 'value', '#value' => $file->filemime);
$form['files'][$key]['filesize'] = array('#type' => 'value', '#value' => $file->filesize);
$form['files'][$key]['fid'] = array('#type' => 'value', '#value' => $file->fid);
}
}
if (user_access('upload files')) {
$form['new'] = array(
'#prefix' => '<div id="attach-hide">',
'#suffix' => '</div>',
);
$form['new']['upload'] = array('#type' => 'file', '#title' => t('Attach new file'), '#size' => 40);
$form['new']['attach'] = array('#type' => 'button', '#value' => t('Attach'), '#name' => 'attach', '#id' => 'attach-button');
$form['attach-url'] = array('#type' => 'hidden', '#value' => url('upload/js', NULL, NULL, TRUE), '#attributes' => array('class' => 'upload'));
}
$form['current']['vid'] = array('#type' => 'hidden', '#value' => $node->vid);
return $form;
}
function theme_upload_form_current(&$form) {
$header = array(t('Delete'), t('List'), t('Description'), t('Size'));
foreach (element_children($form) as $key) {
$row = array();
$row[] = drupal_render($form[$key]['remove']);
$row[] = drupal_render($form[$key]['list']);
$row[] = drupal_render($form[$key]['description']);
$row[] = drupal_render($form[$key]['size']);
$rows[] = $row;
}
$output = theme('table', $header, $rows);
$output .= drupal_render($form);
return $output;
}
function theme_upload_form_new($form) {
$output = drupal_render($form);
return $output;
}
function upload_load($node) {
$files = array();
if ($node->vid) {
$result = db_query('SELECT * FROM {files} f INNER JOIN {file_revisions} r ON f.fid = r.fid WHERE r.vid = %d ORDER BY f.fid', $node->vid);
while ($file = db_fetch_object($result)) {
$files[$file->fid] = $file;
}
}
return $files;
}
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) {
clearstatcache();
$file->filesize = filesize($file->filepath);
drupal_set_message(t('The image was resized to fit within the maximum allowed resolution of %resolution pixels.', array('%resolution' => variable_get('upload_max_resolution', 0))));
}
}
}
return $file;
}
function upload_js() {
if (isset($_POST['vid']) && is_numeric($_POST['vid'])) {
$node = node_load(array('vid' => $_POST['vid']));
if (!$node || !node_access('update', $node) || !variable_get('upload_'. $node->type, TRUE)) {
form_set_error('form_token', t('Validation error, please try again. If this error persists, please contact the site administrator.'));
$output = theme('status_messages');
print drupal_to_js(array('status' => TRUE, 'data' => $output));
exit();
}
}
else {
$node = new stdClass();
}
$node->files = upload_load($node);
_upload_prepare($node);
_upload_validate($node);
$form = _upload_form($node);
foreach (module_implements('form_alter') as $module) {
$function = $module .'_form_alter';
$function('upload_js', $form);
}
$form = form_builder('upload_js', $form);
$output = theme('status_messages') . drupal_render($form);
print drupal_to_js(array('status' => TRUE, 'data' => $output));
exit;
}