/** * 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; }
/** * Truncate the URL if it is longer than the maxlen * Do this by deleting necessary characters from the middle of the URL * * Always preserve the 'http://' part on the left. * * NOTE: The 'maxlen' applies only to the part after the 'http://' * * @param string $raw_url the input URL * @param int $maxlen the maximum allowed length (0 means no limit) * * @return the truncated URL */ public static function truncate_url($raw_url, $maxlen) { return parent::truncate_url($raw_url, $maxlen); }