/** * Display a form for updating/editing an attachment */ public function update() { // Call with: index.php?option=com_attachments&task=update&id=1&tmpl=component // or: component/attachments/update/id/1/tmpl/component // Make sure we have a valid attachment ID $id = JRequest::getInt('id'); if (is_numeric($id)) { $id = (int) $id; } else { $errmsg = JText::sprintf('ATTACH_ERROR_INVALID_ATTACHMENT_ID_N', $id) . ' (ERR 24)'; JError::raiseError(500, $errmsg); } // Get the attachment record require_once JPATH_COMPONENT_SITE . '/models/attachment.php'; $model = new AttachmentsModelAttachment(); $model->setId($id); $attachment = $model->getAttachment(); if (!$attachment) { $errmsg = JText::sprintf('ATTACH_ERROR_CANNOT_UPDATE_ATTACHMENT_INVALID_ID_N', $id) . ' (ERR 25)'; JError::raiseError(500, $errmsg); } // Get the component parameters jimport('joomla.application.component.helper'); $params = JComponentHelper::getParams('com_attachments'); // Get the article/parent handler $parent_id = $attachment->parent_id; $parent_type = $attachment->parent_type; $parent_entity = $attachment->parent_entity; JPluginHelper::importPlugin('attachments'); $apm = getAttachmentsPluginManager(); if (!$apm->attachmentsPluginInstalled($parent_type)) { $errmsg = JText::sprintf('ATTACH_ERROR_INVALID_PARENT_TYPE_S', $parent_type) . ' (ERR 26)'; JError::raiseError(500, $errmsg); } $parent = $apm->getAttachmentsPlugin($parent_type); // Check to make sure we can edit it if (!$parent->userMayEditAttachment($attachment)) { return JError::raiseError(404, JText::_('JERROR_ALERTNOAUTHOR') . ' (ERR 27)'); } // Set up the entity name for display $parent_entity_name = JText::_('ATTACH_' . $parent_entity); // Verify that this user may add attachments to this parent $user = JFactory::getUser(); $new_parent = false; if ($parent_id === null) { $parent_id = 0; $new_parent = true; } // Make sure the attachments directory exists $upload_dir = JPATH_BASE . '/' . AttachmentsDefines::$ATTACHMENTS_SUBDIR; $secure = $params->get('secure', false); if (!AttachmentsHelper::setup_upload_directory($upload_dir, $secure)) { $errmsg = JText::sprintf('ATTACH_ERROR_UNABLE_TO_SETUP_UPLOAD_DIR_S', $upload_dir) . ' (ERR 28)'; JError::raiseError(500, $errmsg); } // Make sure the update parameter is legal $update = JRequest::getWord('update'); if ($update && !in_array($update, AttachmentsDefines::$LEGAL_URI_TYPES)) { $update = false; } // Suppress the display filename if we are switching from file to url $display_name = $attachment->display_name; if ($update && $update != $attachment->uri_type) { $attachment->display_name = ''; } // Set up the view require_once JPATH_COMPONENT_SITE . '/views/update/view.html.php'; $view = new AttachmentsViewUpdate(); $from = JRequest::getWord('from', 'closeme'); AttachmentsHelper::add_view_urls($view, 'update', $parent_id, $attachment->parent_type, $id, $from); $view->update = $update; $view->new_parent = $new_parent; $view->attachment = $attachment; $view->parent = $parent; $view->params = $params; $view->from = $from; $view->Itemid = JRequest::getInt('Itemid', 1); $view->error = false; $view->error_msg = false; $view->display(); }
/** * Add the infomation about the URL to the attaachment record and then save it * * @param &object &$attachment the attachment object * @param &object &$parent the attachments parent object * @param bool $verify whether the existance of the URL should be checked * @param bool $relative_url allow relative URLs * @param string $update the type of update (or false if it is not an update) * @param int $attachment_id the attachment ID, false if this is a new attachment * * @return an error message if there is a problem */ public static function add_url(&$attachment, &$parent, $verify, $relative_url = false, $update = false, $attachment_id = false) { $user = JFactory::getUser(); // 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); // Figure out if the user has permissions to publish $may_publish = $parent->userMayChangeAttachmentState($attachment->parent_id, $attachment->parent_entity, $attachment->created_by); // If we are updating, note the name of the old filename (if there was one) // (Needed for switching from a file to a URL) $old_filename = null; $old_filename_sys = null; $old_display_name = null; if ($update) { if ($attachment->filename_sys) { $old_filename = $attachment->filename; $old_filename_sys = $attachment->filename_sys; } $old_display_name = JRequest::getString('old_display_name', null); } // Check to make sure the URL is valid $from = JRequest::getWord('from'); // Get the info from the url $result = AttachmentsHelper::get_url_info($attachment->url, $attachment, $verify, $relative_url); // Save the info about the URL flags $attachment->url_verify = $verify; $attachment->url_relative = $relative_url; // If there was an error, bow out if ($result !== true) { $app = JFactory::getApplication(); if ($app->isAdmin()) { return $result; } $update_form = JRequest::getWord('update'); // Redisplay the upload/update form with complaints if ($update) { require_once JPATH_COMPONENT_SITE . '/views/update/view.html.php'; $view = new AttachmentsViewUpdate(); AttachmentsHelper::add_view_urls($view, 'update', $attachment->parent_id, $attachment->parent_type, $attachment_id, $from); $view->update = $update_form; } else { require_once JPATH_COMPONENT_SITE . '/views/upload/view.html.php'; $view = new AttachmentsViewUpload(); AttachmentsHelper::add_view_urls($view, 'upload', $attachment->parent_id, $attachment->parent_type, null, $from); } // Suppress the display filename if we are changing from file to url $display_name = $attachment->display_name; if ($update && ($update == 'file' || $update != $attachment->uri_type)) { $attachment->display_name = ''; } // Set up the view $view->attachment = $attachment; $view->new_parent = $parent->new; $view->parent = $parent; $view->params = $params; $view->from = $from; $view->Itemid = JRequest::getInt('Itemid', 1); $view->error = $result->error; $view->error_msg = $result->error_msg; // Display the view $view->display(); exit; } // Clear out the display_name if the URL has changed $old_url = JRequest::getString('old_url'); if ($attachment->display_name && $attachment->url != $old_url) { $old_display_name = JRequest::getString('old_display_name'); if ($old_display_name == $attachment->display_name) { $attachment->display_name = ''; } } // 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 = (int) $max_filename_length; } else { $max_filename_length = 0; } // Create a display filename, if needed (for long URLs) if ($max_filename_length > 0 && strlen($attachment->display_name) == 0) { if ($attachment->filename) { $attachment->display_name = AttachmentsHelper::truncate_filename($attachment->filename, $max_filename_length); } else { $attachment->display_name = AttachmentsHelper::truncate_url($attachment->url, $max_filename_length); } } // Assume relative URLs are valid if ($relative_url) { $attachment->url_valid = true; } // If there is no filename, do something about it if (!$attachment->filename and !$attachment->display_name) { $attachment->display_name = $attachment->url; } // If the user is not authorised to change the state (eg, publish/unpublish), // ignore the form data and make sure the publish state is set correctly. if (!$may_publish) { $save_type = JString::strtolower(JRequest::getWord('save_type', 'update')); if ($save_type == 'upload') { // Use the default publish state jimport('joomla.application.component.helper'); $params = JComponentHelper::getParams('com_attachments'); $attachment->state = $params->get('publish_default', false); } else { // Restore the old state $db = JFactory::getDBO(); $query = $db->getQuery(true); $query->select('state')->from('#__attachments')->where('id = ' . (int) $attachment->id); $db->setQuery($query, 0, 1); $old_state = $db->loadResult(); if ($db->getErrorNum()) { $errmsg = $db->stderr() . ' (ERR 39)'; JError::raiseError(500, $errmsg); } $attachment->state = $old_state; } } // Set the create/modify dates $now = JFactory::getDate(); $attachment->created = $now->toSql(); $attachment->modified = $attachment->created; $attachment->uri_type = 'url'; // Check the URL length if (strlen($attachment->url) > AttachmentsDefines::$MAXIMUM_URL_LENGTH) { $errmsg = "URL is too long! (" . strlen($attachment->url) . ")"; // ??? Convert to translated error message JError::raiseError(500, $errmsg); } // Save the updated attachment if (!$attachment->store()) { $errmsg = JText::_('ATTACH_ERROR_SAVING_URL_ATTACHMENT_RECORD') . $attachment->getError() . ' (ERR 40)'; JError::raiseError(500, $errmsg); } // Delete any old attachment file if ($old_filename_sys) { jimport('joomla.filesystem.file'); if (JFile::exists($old_filename_sys)) { JFile::delete($old_filename_sys); AttachmentsHelper::clean_directory($old_filename_sys); } } if ($update) { $msg = JText::_('ATTACH_ATTACHMENT_UPDATED'); } else { $msg = JText::_('ATTACH_ATTACHMENT_SAVED'); } return $msg; }