function upload() { require_once JPATH_COMPONENT_SITE . DS . 'helper.php'; // Setup basic info $user =& JFactory::getUser(); $article_id = JRequest::getVar('artid', null); $article_title = AttachmentsHelper::get_article_title($article_id); // Make sure this user can add attachments to this article require_once JPATH_COMPONENT_SITE . DS . 'permissions.php'; if (!AttachmentsPermissions::user_may_add_attachment($user, $article_id)) { $errmsg = JText::_('ERROR NO PERMISSION TO UPLOAD'); JError::raiseError(500, $errmsg); exit; } // Use a different template for the iframe view $from = JRequest::getVar('from', false); $Itemid = JRequest::getVar('Itemid', 1); if ($from == 'closeme') { JRequest::setVar('tmpl', 'component'); } // Get the component parameters jimport('joomla.application.component.helper'); $params = JComponentHelper::getParams('com_attachments'); // Make sure the attachments directory exists $upload_subdir = $params->get('attachments_subdir', 'attachments'); if ($upload_subdir == '') { $upload_subdir = 'attachments'; } $upload_dir = JPATH_BASE . DS . $upload_subdir; $secure = $params->get('secure', false); if (!AttachmentsHelper::setup_upload_directory($upload_dir, $secure)) { $errmsg = JText::_('ERROR UNABLE TO SETUP UPLOAD DIR'); JError::raiseError(500, $errmsg); } $auto_publish = $params->get('publish_default', false); $mod_date_format = $params->get('mod_date_format', 'M-j-Y g:ia'); // Construct the url to save the form $save_url = "index.php?option=com_attachments&task=save&tmpl=component"; if ($from == 'closeme') { $save_url .= "&from=closeme"; } // Set up the view to redisplay the form with warnings require_once JPATH_COMPONENT_SITE . DS . 'views' . DS . 'upload' . DS . 'view.php'; $view = new AttachmentsViewUpload(); $view->assign('save_url', $save_url); $view->assign('article_id', $article_id); $view->assign('article_title', $article_title); $view->assign('filename', ''); $view->assign('description', ''); $view->assign('display_filename', ''); $view->assign('user_field_1', ''); $view->assign('user_field_2', ''); $view->assign('user_field_3', ''); $view->assign('from', JRequest::getVar('from', '')); $view->assign('Itemid', JRequest::getVar('Itemid', 1)); $view->assignRef('params', $params); $view->display(null, false, false, false); }
function upload_file(&$row, $article_id, $update = false, $attachment_id = false) { global $mainframe; // Get the component parameters jimport('joomla.application.component.helper'); $params = JComponentHelper::getParams('com_attachments'); // Get the auto-publish setting $auto_publish = $params->get('publish_default', false); // Make sure the attachments directory exists $upload_subdir = $params->get('attachments_subdir', 'attachments'); if ($upload_subdir == '') { $upload_subdir = 'attachments'; } $upload_dir = JPATH_SITE . DS . $upload_subdir; $secure = $params->get('secure', false); if (!AttachmentsHelper::setup_upload_directory($upload_dir, $secure)) { $errmsg = JText::_('ERROR UNABLE TO SETUP UPLOAD DIR'); JError::raiseError(500, "<p>" . $errmsg . " ({$upload_dir})</p>"); } // If we are updating, note the name of the old filename $old_filename = null; $old_filename_sys = null; if ($update) { $old_filename = $row->filename; $old_filename_sys = $row->filename_sys; } // Get the new filename $filename = $_FILES['upload']['name']; $ftype = $_FILES['upload']['type']; // Make sure a file was successfully uploaded if ($update) { $update_file = JRequest::getVar('change', true); } else { $update_file = true; } if ($update_file && $_FILES['upload']['size'] == 0 && $_FILES['upload']['tmp_name'] == '') { // Guess the type of error if ($filename == '') { $error = 'no_file'; $error_msg = JText::_('ERROR UPLOADING FILE') . ' ' . $filename; $error_msg .= ' (' . JText::_('YOU MUST SELECT A FILE TO UPLOAD') . ')'; if ($mainframe->isAdmin()) { return $error_msg; } } else { $error = 'file_too_big'; $error_msg = JText::_('ERROR UPLOADING FILE') . ' ' . $filename; $error_msg .= ' <br>(' . JText::_('ERROR MAY BE LARGER THAN LIMIT') . ' '; $error_msg .= get_cfg_var('upload_max_filesize') . ')'; if ($mainframe->isAdmin()) { return $error_msg; } } // Set up the view to redisplay the form with warnings if ($update) { require_once JPATH_COMPONENT_SITE . DS . 'views' . DS . 'update' . DS . 'view.php'; $view = new AttachmentsViewUpdate(); $view->assign('update_file', $update_file); $view->assign('attachment_id', $attachment_id); } else { require_once JPATH_COMPONENT_SITE . DS . 'views' . DS . 'upload' . DS . 'view.php'; $view = new AttachmentsViewUpload(); } $view->assign('save_url', JRoute::_("index.php?option=com_attachments&task=save&tmpl=component")); $view->assign('filename', $filename); $view->assign('article_id', $article_id); $view->assign('article_title', AttachmentsHelper::get_article_title($article_id)); $view->assign('description', $row->description); $view->assign('display_filename', $row->display_filename); $view->assign('user_field_1', $row->user_field_1); $view->assign('user_field_2', $row->user_field_2); $view->assign('user_field_3', $row->user_field_3); $view->assign('from', JRequest::getVar('from', '')); $view->assign('Itemid', JRequest::getVar('Itemid', 1)); $view->assignRef('params', $params); $view->display(null, $error, $error_msg, true); exit; } // Make sure the file type is okay (respect restrictions imposed by media manager) jimport('joomla.filesystem.file'); $cmparams =& JComponentHelper::getParams('com_media'); // First check to make sure the extension is allowed $allowable = explode(',', $cmparams->get('upload_extensions')); $ignored = explode(',', $cmparams->get('ignore_extensions')); $format = strtolower(JFile::getExt($filename)); $error = false; $error_msg = false; if (!in_array($format, $allowable) && !in_array($format, $ignored)) { $error = 'illegal_file_extension'; $error_msg = JText::_('ERROR UPLOADING FILE') . ': ' . $filename; $error_msg .= "<br>" . JText::_('ERROR ILLEGAL FILE EXTENSION') . " {$format}"; $error_msg .= "<br>" . JText::_('ERROR CHANGE IN MEDIA MANAGER'); } // Check to make sure the mime type is okay if ($cmparams->get('restrict_uploads', true)) { if ($cmparams->get('check_mime', true)) { $allowed_mime = explode(',', $cmparams->get('upload_mime')); $illegal_mime = explode(',', $cmparams->get('upload_mime_illegal')); if (strlen($ftype) && !in_array($ftype, $allowed_mime) && in_array($ftype, $illegal_mime)) { $error = 'illegal_mime_type'; $error_msg = JText::_('ERROR UPLOADING FILE') . ' ' . $filename; $error_msg .= ', ' . JText::_('ERROR ILLEGAL FILE MIME TYPE') . " {$ftype}"; $error_msg .= " <br>" . JText::_('ERROR CHANGE IN MEDIA MANAGER'); } } } // If there was an error, refresh the form with a warning if ($error) { if ($mainframe->isAdmin()) { return $error_msg; } // Set up the view to redisplay the form with warnings if ($update) { require_once JPATH_COMPONENT_SITE . DS . 'views' . DS . 'update' . DS . 'view.php'; $view = new AttachmentsViewUpdate(); $view->assign('update_file', JRequest::getVar('change', false)); $view->assign('attachment_id', $attachment_id); } else { require_once JPATH_COMPONENT_SITE . DS . 'views' . DS . 'upload' . DS . 'view.php'; $view = new AttachmentsViewUpload(); } $view->assign('save_url', JRoute::_("index.php?option=com_attachments&task=save&tmpl=component")); $view->assign('filename', $filename); $view->assign('article_id', $article_id); $view->assign('article_title', AttachmentsHelper::get_article_title($article_id)); $view->assign('description', $row->description); $view->assign('display_filename', $row->display_filename); $view->assign('user_field_1', $row->user_field_1); $view->assign('user_field_2', $row->user_field_2); $view->assign('user_field_3', $row->user_field_3); $view->assign('from', JRequest::getVar('from', '')); $view->assign('Itemid', JRequest::getVar('Itemid', 1)); $view->assignRef('params', $params); $view->display(null, $error, $error_msg, true); exit; } // Define where the attachments go $upload_url = $params->get('attachments_subdir', 'attachments'); $upload_dir = JPATH_SITE . DS . $upload_url; // Figure out the system filename $filename_sys = null; $url = null; $prepend = $params->get('prepend', 'article_id'); switch ($prepend) { case 'article_id': $prefix = sprintf("%03d_", $article_id); $filename_sys = $upload_dir . DS . $prefix . $filename; $url = $upload_url . "/" . $prefix . $filename; break; // NOTE: for attachment_id, save normally and make a second pass // to rename the file after we know the attachment ID // NOTE: for attachment_id, save normally and make a second pass // to rename the file after we know the attachment ID default: $filename_sys = $upload_dir . DS . $filename; $url = $upload_url . "/" . $filename; } // If not updating, make sure the system filename doesn't already exist $error = false; if (!$update && JFile::exists($filename_sys) && $prepend != 'attachment_id') { $error = 'file_not_on_server'; $error_msg = JText::_('ERROR FILE ALREADY ON SERVER'); $error_msg .= " ({$filename})"; if ($mainframe->isAdmin()) { return $error_msg; } // Set up the view to redisplay the form with warnings require_once JPATH_COMPONENT_SITE . DS . 'views' . DS . 'upload' . DS . 'view.php'; $view = new AttachmentsViewUpload(); $view->assign('filename', $filename); $view->assign('save_url', JRoute::_("index.php?option=com_attachments&task=save&tmpl=component")); $view->assign('article_id', $article_id); $view->assign('article_title', AttachmentsHelper::get_article_title($article_id)); $view->assign('description', $row->description); $view->assign('display_filename', $row->display_filename); $view->assign('user_field_1', $row->user_field_1); $view->assign('user_field_2', $row->user_field_2); $view->assign('user_field_3', $row->user_field_3); $view->assign('from', JRequest::getVar('from', '')); $view->assign('Itemid', JRequest::getVar('Itemid', 1)); $view->assignRef('params', $params); $view->display(null, $error, $error_msg, true); exit; } // Get the maximum allowed filename length (for the filename display) $max_filename_length = $params->get('max_filename_length', 0); if (is_numeric($max_filename_length)) { $max_filename_length = intval($max_filename_length); } else { $max_filename_length = 0; } // Create a display filename, if needed (for long filenames) if ($max_filename_length > 0 and strlen($row->display_filename) == 0 and strlen($filename) > $max_filename_length) { $row->display_filename = AttachmentsHelper::truncate_filename($filename, $max_filename_length); } // Copy the info about the uploaded file into the new record $row->filename = $filename; $row->filename_sys = $filename_sys; $row->url = $url; $row->file_type = $ftype; $row->file_size = $_FILES['upload']['size']; $row->published = $auto_publish; // Set the create/modify dates jimport('joomla.utilities.date'); $now = new JDate(); $row->create_date = $now->toMySQL(); $row->modification_date = $row->create_date; // Add the icon file type require_once JPATH_COMPONENT_SITE . DS . 'file_types.php'; $row->icon_filename = AttachmentsFileTypes::icon_filename($filename, $ftype); // Save the updated attachment if (!$row->store()) { JError::raiseError(500, $row->getError()); } // Get the upload id $db =& JFactory::getDBO(); $attachment_id = $db->insertid(); // If we're prepending attachment IDs, fix the system filename and URL and // update the attachment record (now that we know the attachment ID) if ($prepend == 'attachment_id') { $prefix = sprintf("%03d_", $attachment_id); $filename_sys = $upload_dir . DS . $prefix . $filename; $url = $upload_url . "/" . $prefix . $filename; $row->id = $attachment_id; $row->filename_sys = $filename_sys; $row->url = $url; $row->store(); } // Move the file $msg = ""; if (JFile::upload($_FILES['upload']['tmp_name'], $filename_sys)) { $size = intval($row->file_size / 1024.0); chmod($filename_sys, 0644); if ($update) { $msg = JText::_('UPDATED ATTACHMENT') . ' ' . $filename . " (" . $size . " Kb)!"; } else { $msg = JText::_('UPLOADED ATTACHMENT') . ' ' . $filename . " (" . $size . " Kb)!"; } } else { $query = "DELETE FROM #__attachments WHERE id={$attachment_id}"; $db->setQuery($query); $result = $db->query(); $msg = JText::_('ERROR MOVING FILE') . " {$_FILES['upload']['tmp_name']} -> {$filename_sys})"; } // If we are updating, we may need to delete the old file if ($update) { if ($filename != $old_filename) { JFile::delete($old_filename_sys); } } return $msg; }