View source
<?php
function comment_upload_comment(&$comment, $op) {
$cid = is_object($comment) ? $comment->cid : $comment['cid'];
$cid = is_array($cid) ? $cid['#value'] : $cid;
switch ($op) {
case 'form':
$node = node_load($comment['nid']['#value']);
if (!user_access('upload files') || !variable_get('comment_upload_' . $node->type, 1)) {
break;
}
$cobj->cid = $cid;
$cobj->files = _comment_upload_load_files($cid);
_upload_prepare($cobj);
$form = array(
'#attributes' => array(
'enctype' => 'multipart/form-data',
),
);
if (variable_get('comment_upload_single', 0)) {
$form['upload'] = array(
'#type' => 'file',
'#title' => t('Attachment'),
'#size' => 50,
'#description' => !empty($cobj->files) ? t('You already have a file uploaded, if you upload another it will overwrite the current one.') : '',
);
}
else {
drupal_add_js('misc/progress.js');
drupal_add_js('misc/upload.js');
$form['attachments'] = array(
'#type' => 'fieldset',
'#title' => t('File attachments'),
'#collapsible' => TRUE,
'#collapsed' => empty($cobj->files),
'#description' => t('Changes made to the attachments are not permanent until you save this post.'),
'#prefix' => '<div class="attachments">',
'#suffix' => '</div>',
'#weight' => 10,
);
$form['attachments']['wrapper'] = array(
'#prefix' => '<div id="attach-wrapper">',
'#suffix' => '</div>',
);
$form['attachments']['wrapper'] += _upload_form($cobj);
if (module_exists('upload_preview')) {
_upload_preview_node_form($form['attachments']['wrapper']['files'], 0);
}
$form['attachments']['wrapper']['attach-url']['#value'] = url('comment_upload/js', NULL, NULL, TRUE);
$form['current']['cid'] = array(
'#type' => 'hidden',
'#value' => $cid,
);
unset($form['attachments']['wrapper']['current']['vid']);
}
return $form;
case 'validate':
$comment_copy = (object) $comment;
_upload_validate($comment_copy);
break;
case 'insert':
case 'update':
$node = node_load($comment['nid']);
if (user_access('upload files') && variable_get('comment_upload_' . $node->type, 1)) {
_comment_upload_save_files($comment);
}
break;
case 'delete':
_comment_upload_delete($cid);
break;
case 'view':
if (!user_access('view uploaded files')) {
break;
}
if (!isset($comment->files)) {
$comment->files = _comment_upload_load_files($cid, $comment->nid);
}
elseif (is_array($comment->files) && variable_get('comment_upload_single', 0)) {
foreach ($comment->files as $file) {
if (strpos($file['fid'], 'upload') !== false) {
unset($comment->files[0]);
break;
}
}
}
if ($comment->files) {
$comment->comment .= theme('comment_attachments', $comment->files);
}
break;
}
}
function comment_upload_form_alter($form_id, &$form) {
if ($form_id == 'node_type_form') {
$form['workflow']['comment_upload'] = array(
'#type' => 'radios',
'#title' => t('Attachments on comments'),
'#default_value' => variable_get('comment_upload_' . $form['#node_type']->type, 1),
'#options' => array(
t('Disabled'),
t('Enabled'),
),
'#weight' => 20,
);
}
else {
if ($form_id == 'upload_admin_settings') {
$form['settings_general']['comment_upload_single'] = array(
'#type' => 'select',
'#title' => t('Attachments on comments'),
'#default_value' => variable_get('comment_upload_single', 0),
'#options' => array(
t('Multiple'),
t('Single'),
),
'#description' => t('Set whether to allow only one attachment per comment'),
);
$form['settings_general']['comment_upload_inline_image'] = array(
'#type' => 'select',
'#title' => t('Images on comments'),
'#default_value' => variable_get('comment_upload_inline_image', 0),
'#options' => array(
t('Normal attachment'),
t('Inline display'),
),
'#description' => t('Show uploaded image with comment or use a normal attachment link (single attachment mode only)'),
);
}
}
}
function comment_upload_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'comment_upload/js',
'callback' => 'comment_upload_js',
'access' => user_access('upload files'),
'type' => MENU_CALLBACK,
);
}
else {
$comment_upload_path = drupal_get_path('module', 'comment_upload');
if (module_exists('views')) {
require_once './' . $comment_upload_path . '/comment_upload_views.inc';
}
}
return $items;
}
function comment_upload_js() {
$comment = (object) $_POST;
$comment->files = _comment_upload_load_files($comment->cid);
_upload_prepare($comment);
_upload_validate($comment);
$form = _upload_form($comment);
$form['attach-url']['#value'] = url('comment_upload/js', NULL, NULL, TRUE);
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;
}
function _comment_upload_load_files($cid, $nid = NULL) {
static $cache = array();
if ($nid) {
if (!isset($cache[$nid])) {
$cache[$nid] = array();
$result = db_query('SELECT * FROM {comment_upload_files} f WHERE f.nid = %d ORDER BY f.fid DESC', $nid);
while ($file = db_fetch_object($result)) {
$cache[$nid][$file->cid][] = $file;
}
}
return $cache[$nid][$cid];
}
else {
if ($cid) {
$result = db_query('SELECT * FROM {comment_upload_files} f WHERE f.cid = %d ORDER BY f.fid DESC', $cid);
while ($file = db_fetch_object($result)) {
$files[] = $file;
}
return $files;
}
}
}
function _comment_upload_save_files($comment) {
if (!is_array($comment['files'])) {
return;
}
foreach ($comment['files'] as $file) {
$file = (object) $file;
if ($file->remove) {
if (strpos($file->fid, 'upload') !== false) {
file_delete($file->filepath);
}
else {
file_delete($file->filepath);
db_query('DELETE FROM {comment_upload_files} WHERE fid = %d', $file->fid);
}
}
elseif (strpos($file->fid, 'upload') !== false) {
if ($file = file_save_upload($file, $file->filename)) {
if (variable_get('comment_upload_single', 0) && isset($comment['files'][0])) {
db_query("UPDATE {comment_upload_files} SET filename = '%s', filepath = '%s', filemime = '%s', filesize = %d WHERE fid = %d", $file->filename, $file->filepath, $file->filemime, $file->filesize, $comment['files'][0]['fid']);
}
else {
$file->fid = db_next_id('{comment_upload_files}_fid');
db_query("INSERT INTO {comment_upload_files} (fid, nid, cid, filename, filepath, filemime, filesize, description, list) VALUES (%d, %d, %d, '%s', '%s', '%s', %d, '%s', %d)", $file->fid, $comment['nid'], $comment['cid'], $file->filename, $file->filepath, $file->filemime, $file->filesize, $file->description, $file->list);
}
}
unset($_SESSION['file_previews'][$fid]);
}
else {
db_query("UPDATE {comment_upload_files} SET list = %d, description = '%s' WHERE fid = %d", $file->list, $file->description, $file->fid);
}
}
}
function _comment_upload_delete($cid) {
$files = _comment_upload_load_files($cid);
if (!empty($files)) {
foreach ($files as $file) {
file_delete($file->filepath);
db_query('DELETE FROM {comment_upload_files} WHERE fid = %d', $file->fid);
}
}
}
function comment_upload_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if ($op == 'delete') {
$result = db_query("SELECT * FROM {comment_upload_files} WHERE nid = %d", $node->nid);
while ($file = db_fetch_object($result)) {
file_delete($file->filepath);
}
db_query("DELETE FROM {comment_upload_files} WHERE nid = %d", $node->nid);
}
}
function theme_comment_attachments($files) {
if (variable_get('comment_upload_inline_image', 0)) {
$regex = '/\\.(' . ereg_replace(' +', '|', preg_quote('jpg jpeg gif png')) . ')$/i';
foreach ($files as $key => $file) {
if ($file->list) {
if (preg_match($regex, $file->filename)) {
unset($files[$key]);
$href = check_url(strpos($file->fid, 'upload') === false ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path())));
$html .= '<img src="' . $href . '" title="' . check_plain($file->description) . '" alt="' . check_plain($file->description) . '" />';
}
}
}
}
$html .= theme('upload_attachments', $files);
return $html;
}
function comment_upload_file_download($file) {
$file = file_create_path($file);
$result = db_query("SELECT f.* FROM {comment_upload_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)) {
$name = mime_header_encode($file->filename);
$type = mime_header_encode($file->filemime);
return array(
'Content-Type: ' . $type,
'Content-Length: ' . $file->filesize,
'Expires: 0',
'Pragma: cache',
'Cache-Control: private',
);
}
else {
return -1;
}
}
else {
return -1;
}
}
}
function comment_upload_views_tables() {
require_once drupal_get_path('module', 'comment_upload') . '/comment_upload_views.inc';
return _comment_upload_views_tables();
}