/** * Display the edit view */ public function display($tpl = null) { // For convenience $attachment = $this->attachment; // Prevent unallowed editing if (!$this->attachment->parent->userMayEditAttachment($attachment)) { $errmsg = JText::_('ATTACH_ERROR_NO_PERMISSION_TO_EDIT'); return JError::raiseError(403, $errmsg . ' (ERR 178)'); } // Construct derived data $attachment->parent_entity_name = JText::_('ATTACH_' . $attachment->parent_entity); if (!isset($attachment->modifier_name)) { AttachmentsHelper::addAttachmentUserNames($attachment); } // Compute the attachment size in kB $attachment->size_kb = (int) (10 * $attachment->file_size / 1024.0) / 10.0; // set up lists for form controls $this->lists = array(); $this->lists['published'] = JHtml::_('select.booleanlist', 'state', 'class="inputbox"', $attachment->state); $this->lists['url_valid'] = JHtml::_('select.booleanlist', 'url_valid', 'class="inputbox" title="' . JText::_('ATTACH_URL_IS_VALID_TOOLTIP') . '"', $attachment->url_valid); // Construct the drop-down list for legal icon filenames $icon_filenames = array(); require_once JPATH_COMPONENT_SITE . '/file_types.php'; foreach (AttachmentsFileTypes::unique_icon_filenames() as $ifname) { $icon_filenames[] = JHtml::_('select.option', $ifname); } $this->lists['icon_filenames'] = JHtml::_('select.genericlist', $icon_filenames, 'icon_filename', 'class="inputbox" size="1"', 'value', 'text', $attachment->icon_filename); // If switching from article to URL default url_verify to true if ($attachment->uri_type == 'file' and $this->update == 'url') { $attachment->url_verify = true; } // Set up for checkboxes $this->relative_url_checked = $attachment->url_relative ? 'checked="yes"' : ''; $this->verify_url_checked = $attachment->url_verify ? 'checked="yes"' : ''; // Set up some tooltips $this->enter_url_tooltip = JText::_('ATTACH_ENTER_URL') . '::' . JText::_('ATTACH_ENTER_URL_TOOLTIP'); $this->display_filename_tooltip = JText::_('ATTACH_DISPLAY_FILENAME') . '::' . JText::_('ATTACH_DISPLAY_FILENAME_TOOLTIP'); $this->display_url_tooltip = JText::_('ATTACH_DISPLAY_URL') . '::' . JText::_('ATTACH_DISPLAY_URL_TOOLTIP'); $this->download_count_tooltip = JText::_('ATTACH_NUMBER_OF_DOWNLOADS') . '::' . JText::_('ATTACH_NUMBER_OF_DOWNLOADS_TOOLTIP'); // Set up mootools/modal AttachmentsJavascript::setupModalJavascript(); // Add the style sheets JHtml::stylesheet('com_attachments/attachments_admin_form.css', array(), true); $lang = JFactory::getLanguage(); if ($lang->isRTL()) { JHtml::stylesheet('com_attachments/attachments_admin_form_rtl.css', array(), true); } // Set the toolbar $this->addToolBar(); // Display the form parent::display($tpl); }
/** * Display the view */ public function display($tpl = null) { // Access check. if (!(JFactory::getUser()->authorise('core.edit', 'com_attachments') or JFactory::getUser()->authorise('core.edit.own', 'com_attachments'))) { return JError::raiseError(404, JText::_('JERROR_ALERTNOAUTHOR') . ' (ERR 62)'); } // For convenience $attachment = $this->attachment; $parent = $this->parent; // Construct derived data $attachment->parent_entity_name = JText::_('ATTACH_' . $attachment->parent_entity); $attachment->parent_title = $parent->getTitle($attachment->parent_id, $attachment->parent_entity); if (!isset($attachment->modifier_name)) { AttachmentsHelper::addAttachmentUserNames($attachment); } $this->relative_url_checked = $attachment->url_relative ? 'checked="yes"' : ''; $this->verify_url_checked = $attachment->url_verify ? 'checked="yes"' : ''; $this->may_publish = $parent->userMayChangeAttachmentState($attachment->parent_id, $attachment->parent_entity, $attachment->created_by); // Set up some HTML for display in the form $this->lists = array(); $this->lists['published'] = JHtml::_('select.booleanlist', 'state', 'class="inputbox"', $attachment->state); $this->lists['url_valid'] = JHtml::_('select.booleanlist', 'url_valid', 'class="inputbox" title="' . JText::_('ATTACH_URL_IS_VALID_TOOLTIP') . '"', $attachment->url_valid); // Set up for editing the access level if ($this->params->get('allow_frontend_access_editing', false)) { require_once JPATH_COMPONENT_ADMINISTRATOR . '/models/fields/accesslevels.php'; $this->access_level = JFormFieldAccessLevels::getAccessLevels('access', 'access', $attachment->access); $this->access_level_tooltip = JText::_('ATTACH_ACCESS_LEVEL_TOOLTIP'); } // Add the stylesheets JHtml::stylesheet('com_attachments/attachments_frontend_form.css', array(), true); $lang = JFactory::getLanguage(); if ($lang->isRTL()) { JHtml::stylesheet('com_attachments/attachments_frontend_form_rtl.css', array(), true); } // Display the form parent::display($tpl); }
function attachments_attachmentListHTML($article_id, $user_can_add, $Itemid, $from) { require_once JPATH_SITE . DS . 'components' . DS . 'com_attachments' . DS . 'helper.php'; // Generate the HTML for the attachments for the specified article $alist = ""; $db =& JFactory::getDBO(); $query = "SELECT count(*) FROM #__attachments WHERE article_id='" . $article_id . "' AND published='1'"; $db->setQuery($query); $total = $db->loadResult(); if ($total > 0) { // Get the component parameters jimport('joomla.application.component.helper'); $params = JComponentHelper::getParams('com_attachments'); // Check the security status $attach_dir = JPATH_SITE . DS . $params->get('upload_dir', 'attachments'); $secure = $params->get('secure', false); $hta_filename = $attach_dir . DS . '.htaccess'; if ($secure && !file_exists($hta_filename) || !$secure && file_exists($hta_filename)) { AttachmentsHelper::setup_upload_directory($attach_dir, $secure); } $alist = AttachmentsHelper::attachmentsTableHTML($article_id, false, true, true, $from); } return $alist; }
/** * 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(); }
/** * Truncate the filename if it is longer than the maxlen * Do this by deleting necessary at the end of the base filename (before the extensions) * * @param string $raw_filename the input filename * @param int $maxlen the maximum allowed length (0 means no limit) * * @return the truncated filename */ public static function truncate_filename($raw_filename, $maxlen) { return parent::truncate_filename($raw_filename, $maxlen); }
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; }
/** * Display the view */ function display($tpl = null, $error = false, $error_msg = false, $full_html = false) { $document =& JFactory::getDocument(); $document->addStyleSheet(JURI::base() . 'plugins/content/attachments.css', 'text/css', null, array()); $document->addStyleSheet(JURI::base() . 'plugins/content/attachments2.css', 'text/css', null, array()); $article_id = $this->article_id; $save_url = JRoute::_($this->save_url); $base_url = JURI::Base(); $params = $this->params; // If this is a stand-alone page, generate the full html wrapper if ($full_html) { ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <link href="<?php echo $base_url; ?> plugins/content/attachments.css" rel="stylesheet" type="text/css" /> <link href="<?php echo $base_url; ?> plugins/content/attachments2.css" rel="stylesheet" type="text/css" /> <link href="<?php echo $base_url; ?> templates/rhuk_milkyway/css/template.css" rel="stylesheet" type="text/css" /> </head> <body> <?php } // Generate the list of existing attachments echo AttachmentsHelper::attachmentsTableHTML($article_id, 'EXISTING ATTACHMENTS', false, false, $this->from); // Prepare for error displays $upload_id = 'upload'; $filename = $this->filename; switch ($error) { case 'no_file': $upload_id = 'upload_warning'; $filename = ''; break; case 'file_too_big': $upload_id = 'upload_warning'; break; case 'file_not_on_server': $upload_id = 'upload_warning'; break; } // Display the form ?> <form class="attachments" enctype="multipart/form-data" name="upload_form" action="<?php echo $save_url; ?> " method="post"> <fieldset> <legend><?php echo JText::_('UPLOAD ATTACHMENT FILE'); ?> </legend> <?php if ($error_msg) { ?> <div class="formWarning" id="formWarning"><?php echo $error_msg; ?> </div> <?php } ?> <p><label for="<?php echo $upload_id; ?> "><b><?php echo JText::_('ATTACH FILE COLON'); ?> </b></label> <input type="file" name="upload" id="<?php echo $upload_id; ?> " size="68" maxlength="512" /></p> <p><label for="display_filename" title="<?php echo JText::_('DISPLAY FILENAME TOOLTIP'); ?> " ><b><?php echo JText::_('DISPLAY FILENAME COLON'); ?> </b></label> <input type="text" name="display_filename" id="display_filename" size="70" maxlength="80" title="<?php echo JText::_('DISPLAY FILENAME TOOLTIP'); ?> " value="<?php echo $this->display_filename; ?> " /> <?php echo JText::_('OPTIONAL'); ?> </p> <p><label for="description"><b><?php echo JText::_('DESCRIPTION COLON'); ?> </b></label> <input type="text" name="description" id="description" size="70" maxlength="100" value="<?php echo $this->description; ?> " /></p> <?php if ($params->get('user_field_1_name', false)) { ?> <p><label for="user_field_1"><b><?php echo $params->get('user_field_1_name'); ?> :</b></label> <input type="text" name="user_field_1" id="user_field_1" size="70" maxlength="100" value="<?php echo $this->user_field_1; ?> " /></p> <?php } ?> <?php if ($params->get('user_field_2_name', false)) { ?> <p><label for="user_field_2"><b><?php echo $params->get('user_field_2_name'); ?> :</b></label> <input type="text" name="user_field_2" id="user_field_2" size="70" maxlength="100" value="<?php echo $this->user_field_2; ?> " /></p> <?php } ?> <?php if ($params->get('user_field_3_name', false)) { ?> <p><label for="user_field_3"><b><?php echo $params->get('user_field_3_name'); ?> :</b></label> <input type="text" name="user_field_3" id="user_field_3" size="70" maxlength="100" value="<?php echo $this->user_field_3; ?> " /></p> <?php } ?> </fieldset> <input type="hidden" name="MAX_FILE_SIZE" value="524288" /> <input type="hidden" name="submitted" value="TRUE" /> <input type="hidden" name="update_file" value="TRUE" /> <input type="hidden" name="article_id" value="<?php echo $article_id; ?> " /> <input type="hidden" name="from" value="<?php echo $this->from; ?> " /> <input type="hidden" name="Itemid" value="<?php echo $this->Itemid; ?> " /> <?php echo JHTML::_('form.token'); ?> <div align="center"> <input type="submit" name="submit" value="<?php echo JText::_('UPLOAD'); ?> " /> <input type="button" name="cancel" value="<?php echo JText::_('CANCEL'); ?> " onClick="window.parent.document.getElementById('sbox-window').close();" /> </div> </form> <?php // Display the auto-publish warning, if appropriate if (!$params->get('publish_default', false)) { $msg = $params->get('auto_publish_warning', ''); if (strlen($msg) == 0) { $msg = 'WARNING ADMIN MUST PUBLISH'; } $msg = JText::_($msg); echo "<h2>{$msg}</h2>"; } // Close the html wrapper, if appropriate if ($full_html) { echo "</body>\n</html>\n"; } }
/** Insert the attachments list into the content text (for front end) * * @param object &$content the text of the content item (eg, article text) * @param int $parent_id the ID for the parent object * @param string $parent_entity the type of entity for this parent type * * @return string the modified content text (false for failure) */ public function insertAttachmentsList(&$content, $parent_id, $parent_entity) { $aparams = $this->attachmentsParams(); // Get the desired placement $attachments_placement = $aparams->get('attachments_placement', 'end'); if ($attachments_placement == 'disabled_nofilter') { return false; } // Determine where we are $from = JRequest::getCmd('view', 'closeme'); $Itemid = JRequest::getInt('Itemid', 1); // See whether we can display the links to add attachments $user_can_add = $this->userMayAddAttachment($parent_id, $parent_entity); // Get the field name for the content item's text $text_field_name = $this->getTextFieldName($content, $parent_entity); if ($text_field_name === null) { return false; } // Get the attachments tag, if present $attachments_tag = ''; $attachments_tag_args = ''; $match = false; if (JString::strpos($content->{$text_field_name}, '{attachments')) { if (preg_match('@(<span class="hide_attachments_token">)?{attachments([ ]*:*[^}]+)?}(</span>)?@', $content->{$text_field_name}, $match)) { $attachments_tag = true; } if (isset($match[1]) && $match[1]) { $attachments_tag_args_raw = $match[1]; $attachments_tag_args = ltrim($attachments_tag_args_raw, ' :'); } if ($attachments_tag) { $attachments_tag = $match[0]; } } // Check the security status $attach_dir = JPATH_SITE . '/' . AttachmentsDefines::$ATTACHMENTS_SUBDIR; $secure = $aparams->get('secure', false); $hta_filename = $attach_dir . '/ . htaccess'; if ($secure && !file_exists($hta_filename) || !$secure && file_exists($hta_filename)) { AttachmentsHelper::setup_upload_directory($attach_dir, $secure); } // Construct the attachment list (if appropriate) $html = ''; $attachments_list = false; $add_attachement_btn = false; // Get the html for the attachments list require_once JPATH_SITE . '/components/com_attachments/controllers/attachments.php'; $controller = new AttachmentsControllerAttachments(); $attachments_list = $controller->displayString($parent_id, $this->parent_type, $parent_entity, null, true, true, false, $from); // If the attachments list is empty, insert an empty div for it if ($attachments_list == '') { $class_name = $aparams->get('attachments_table_style', 'attachmentsList'); $div_id = 'attachmentsList' . '_' . $this->parent_type . '_' . $parent_entity . '_' . (string) $parent_id; $attachments_list = "\n<div class=\"{$class_name}\" id=\"{$div_id}\"></div>\n"; } $html .= $attachments_list; if ($html || $user_can_add) { // Add the style sheet JHtml::stylesheet('com_attachments/attachments_list.css', array(), true); // Handle RTL styling (if necessary) $lang = JFactory::getLanguage(); if ($lang->isRTL()) { JHtml::stylesheet('com_attachments/attachments_list_rtl.css', array(), true); } } // Construct the add-attachments button, if appropriate $hide_add_attachments_link = $aparams->get('hide_add_attachments_link', 0); if ($user_can_add && !$hide_add_attachments_link) { $add_attachments_btn = AttachmentsHelper::attachmentButtonsHTML($this->parent_type, $parent_id, $parent_entity, $Itemid, $from); $html .= $add_attachments_btn; } // Wrap both list and the Add Attachments button in another div if ($html) { $html = "<div class=\"attachmentsContainer\">\n" . $html . "\n</div>"; } // Finally, add the attachments // NOTE: Hope str_replace() below is UTF8 safe (since the token being replaced is UTF8)... switch ($attachments_placement) { case 'beginning': // Put the attachments list at the beginning if ($attachments_list || $user_can_add) { if ($attachments_tag) { $content->{$text_field_name} = $html . $content->{$text_field_name}; } else { $content->{$text_field_name} = $html . str_replace($attachments_tag, '', $content->{$text_field_name}); } } break; case 'custom': // Insert the attachments at the desired location if ($attachments_list || $user_can_add) { if ($attachments_tag) { $content->{$text_field_name} = str_replace($attachments_tag, $html, $content->{$text_field_name}); } else { // If there is no tag, insert the attachments at the end $content->{$text_field_name} .= $html; } } break; case 'disabled_filter': // Disable and strip out any attachments tags if ($attachments_tag) { $content->{$text_field_name} = str_replace($attachments_tag, '', $content->{$text_field_name}); } break; default: // Add the attachments to the end if ($attachments_list || $user_can_add) { if ($attachments_tag) { $content->{$text_field_name} = str_replace($attachments_tag, '', $content->{$text_field_name}) . $html; } else { $content->{$text_field_name} .= $html; } } break; } return $content; }
/** * Save the parameters */ public function save($key = null, $urlVar = null) { // Check for request forgeries. JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); $app = JFactory::getApplication(); // Get the old component parameters jimport('joomla.application.component.helper'); $old_params = JComponentHelper::getParams('com_attachments'); $old_secure = JRequest::getInt('old_secure'); // Set FTP credentials, if given. jimport('joomla.client.helper'); JClientHelper::setCredentialsFromRequest('ftp'); // Initialise variables. $model = new ConfigModelComponent(); $form = $model->getForm(); $data = JRequest::getVar('jform', array(), 'post', 'array'); $id = JRequest::getInt('id'); $option = JRequest::getCmd('component'); // Get the new component parameters $new_secure = $data['secure']; // Check if the user is authorized to do this. if (!JFactory::getUser()->authorise('core.admin', $option)) { JFactory::getApplication()->redirect('index.php', JText::_('JERROR_ALERTNOAUTHOR')); return; } // Validate the posted data. $return = $model->validate($form, $data); // Check for validation errors. if ($return === false) { // Get the validation messages. $errors = $model->getErrors(); // Push up to three validation messages out to the user. for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) { if ($errors[$i] instanceof Exception) { $app->enqueueMessage($errors[$i]->getMessage(), 'warning'); } else { $app->enqueueMessage($errors[$i], 'warning'); } } // Save the data in the session. $app->setUserState('com_config.config.global.data', $data); // Redirect back to the edit screen. $this->setRedirect(JRoute::_('index.php?option=com_attachments&task=params.edit', false)); return false; } // Attempt to save the configuration. $data = array('params' => $return, 'id' => $id, 'option' => $option); $return = $model->save($data); // Check the return value. if ($return === false) { // Save the data in the session. $app->setUserState('com_config.config.global.data', $data); // Save failed, go back to the screen and display a notice. $message = JText::sprintf('JERROR_SAVE_FAILED', $model->getError()); $this->setRedirect(JRoute::_('index.php?option=com_attachments&task=params.edit'), $message, 'error'); return false; } // Deal with any changes in the 'secure mode' (or upload directories) if ($new_secure != $old_secure) { // Check/update the security status require_once JPATH_SITE . '/components/com_attachments/helper.php'; $attach_dir = JPATH_SITE . '/' . AttachmentsDefines::$ATTACHMENTS_SUBDIR; AttachmentsHelper::setup_upload_directory($attach_dir, $new_secure == 1); $msg = JText::_('ATTACH_UPDATED_ATTACHMENTS_PARAMETERS_AND_SECURITY_SETTINGS'); } else { $msg = JText::_('ATTACH_UPDATED_ATTACHMENTS_PARAMETERS'); } // Set the redirect based on the task. switch ($this->getTask()) { case 'apply': $this->setRedirect('index.php?option=com_attachments&task=params.edit', $msg, 'message'); break; case 'save': default: $this->setRedirect('index.php?option=com_attachments', $msg, 'message'); break; } return true; }
/** * Method is called by * * @access public */ public function onAfterImport($context, &$xml, $options) { JLog::add(new JLogEntry(__METHOD__, JLOG::DEBUG, 'plg_j2xml_attachments')); JLog::add(new JLogEntry($context, JLOG::DEBUG, 'plg_j2xml_attachments')); if (PHP_SAPI == 'cli') { JLog::addLogger(array('logger' => 'echo', 'extension' => 'plg_j2xml_attachments'), JLOG::ALL & ~JLOG::DEBUG, array('plg_j2xml_attachments')); } else { JLog::addLogger(array('logger' => $options->get('logger', 'messagequeue'), 'extension' => 'plg_j2xml_attachments'), JLOG::ALL & ~JLOG::DEBUG, array('plg_j2xml_attachments')); } if (version_compare(J2XMLVersion::getShortVersion(), '15.9.5') == -1) { JLog::add(new JLogEntry(JText::_('PLG_J2XML_ATTACHMENTS') . ' ' . JText::_('PLG_J2XML_ATTACHMENTS_MSG_REQUIREMENTS_LIB'), JLOG::WARNING, 'plg_j2xml_attachments')); return false; } // Check if component is not installed if (!file_exists(JPATH_ADMINISTRATOR . '/components/com_attachments/attachments.php')) { JLog::add(new JLogEntry(JText::_('PLG_J2XML_ATTACHMENTS') . ' ' . JText::_('PLG_J2XML_ATTACHMENTS_MSG_REQUIREMENTS_COM'), JLOG::WARNING, 'plg_j2xml_attachments')); return false; } // Check if component is not enabled if (!JComponentHelper::isEnabled('com_attachments', true)) { JLog::add(new JLogEntry(JText::_('PLG_J2XML_ATTACHMENTS') . ' ' . JText::_('PLG_J2XML_ATTACHMENTS_MSG_REQUIREMENTS_COM'), JLOG::WARNING, 'plg_j2xml_attachments')); return false; } jimport('eshiol.j2xml.importer'); $app = JFactory::getApplication(); $db = JFactory::getDBO(); foreach ($xml->xpath("/j2xml/attachment[parent_type = 'com_content' and parent_entity = 'article']") as $record) { $importer = new J2XMLImporter(); $importer->prepareData($record, $data, $options); $attachment = new stdClass(); $attachment->filename = html_entity_decode($data['filename']); $attachment->file_type = $data['file_type']; $attachment->file_size = $data['file_size']; $attachment->parent_type = $data['parent_type']; $attachment->parent_entity = $data['parent_entity']; $attachment->parent_id = $importer->getArticledId($data['parent_id']); $attachment->uri_type = $data['uri_type']; if ($data['uri_type'] == 'url') { $attachment->url = $data['url']; $attachment->filename_sys = ''; } elseif (isset($data['file'])) { require_once JPATH_SITE . '/components/com_attachments/helper.php'; // Get the component parameters jimport('joomla.application.component.helper'); $params = JComponentHelper::getParams('com_attachments'); // Define where the attachments go $upload_url = AttachmentsDefines::$ATTACHMENTS_SUBDIR; $upload_dir = JPATH_SITE . '/' . $upload_url; // Get the parent plugin manager JPluginHelper::importPlugin('attachments'); $apm = getAttachmentsPluginManager(); // Get the parent object $parent = $apm->getAttachmentsPlugin($attachment->parent_type); // Construct the system filename and url (based on entities, etc) $newdir = $parent->getAttachmentPath($attachment->parent_entity, $attachment->parent_id, null); $fullpath = $upload_dir . '/' . $newdir; // Make sure the directory exists if (!JFile::exists($fullpath)) { jimport('joomla.filesystem.folder'); if (!JFolder::create($fullpath)) { $errmsg = JText::sprintf('ATTACH_ERROR_UNABLE_TO_SETUP_UPLOAD_DIR_S', $upload_dir) . ' (ERR 34)'; JError::raiseError(500, $errmsg); } require_once JPATH_SITE . '/components/com_attachments/helper.php'; AttachmentsHelper::write_empty_index_html($fullpath); } $attachment->filename_sys = $fullpath . $attachment->filename; file_put_contents($attachment->filename_sys, base64_decode($data['file'])); $attachment->filename_sys = utf8_encode($attachment->filename_sys); $attachment->filename = utf8_encode($attachment->filename); $attachment->url = $upload_url . '/' . $newdir . $attachment->filename; } else { JLog::add(new JLogEntry(JText::sprintf('PLG_J2XML_ATTACHMENTS_MSG_ATTACHMENT_NOT_EXPORTED', $attachment->filename), JLOG::WARNING, 'plg_j2xml_attachments')); return; } $attachment->url_valid = $data['url_valid']; $attachment->url_relative = $data['url_relative']; $attachment->url_verify = $data['url_verify']; $attachment->display_name = $data['display_name']; $attachment->description = $data['description']; $attachment->icon_filename = $data['icon_filename']; $attachment->access = $importer->getAccessId($data['access']); $attachment->state = $data['state']; $attachment->user_field_1 = $data['user_field_1']; $attachment->user_field_2 = $data['user_field_2']; $attachment->user_field_3 = $data['user_field_3']; $attachment->created = $data['created']; $attachment->created_by = $importer->getUserId($data['created_by'], $this->_user_id); $attachment->modified = $data['modified']; $attachment->modified_by = $importer->getUserId($data['modified_by'], 0); $attachment->download_count = $data['download_count']; $query = $db->getQuery(true); $query->select($db->quoteName('id')); $query->from($db->quoteName('#__attachments')); $query->where($db->quoteName('parent_type') . '=' . $db->quote($attachment->parent_type)); $query->where($db->quoteName('parent_entity') . '=' . $db->quote($attachment->parent_entity)); $query->where($db->quoteName('filename') . '=' . $db->quote($attachment->filename)); $query->where($db->quoteName('uri_type') . '=' . $db->quote($attachment->uri_type)); $db->setQuery($query); $id = $db->loadResult(); if ($id) { $attachments->id = $id; if ($db->updateObject('#__attachments', $attachments, 'id')) { JLog::add(new JLogEntry(JText::sprintf('PLG_J2XML_ATTACHMENTS_MSG_ATTACHMENT_IMPORTED', $attachment->filename), JLOG::INFO, 'plg_j2xml_attachments')); } else { JLog::add(new JLogEntry(JText::sprintf('PLG_J2XML_ATTACHMENTS_MSG_ATTACHMENT_NOT_IMPORTED', $attachment->filename), JLOG::INFO, 'plg_j2xml_attachments')); } } else { if ($db->insertObject('#__attachments', $attachment)) { JLog::add(new JLogEntry(JText::sprintf('PLG_J2XML_ATTACHMENTS_MSG_ATTACHMENT_IMPORTED', $attachment->filename), JLOG::INFO, 'plg_j2xml_attachments')); } else { JLog::add(new JLogEntry(JText::sprintf('PLG_J2XML_ATTACHMENTS_MSG_ATTACHMENT_NOT_IMPORTED', $attachment->filename), JLOG::INFO, 'plg_j2xml_attachments')); } } } return true; }
/** * 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); }
/** * Add Attachment button * * @return a button */ function onDisplay($name) { // Avoid displaying the button for anything except content articles global $option; if ($option != 'com_content') { return new JObject(); } // Get the article ID $cid = JRequest::getVar('cid', array(0), '', 'array'); $id = 0; if (count($cid) > 0) { $id = intval($cid[0]); } if ($id == 0) { $nid = JRequest::getVar('id', null); if (!is_null($nid)) { $id = intval($nid); } } // Create the button object $button = new JObject(); // Load the language file from the backend $lang =& JFactory::getLanguage(); $lang->load('plg_frontend_attachments', JPATH_ADMINISTRATOR); // ??? Where is the tooltip coming from? // (Would like to use different tip for create article button) // Figure out where we are and construct the right link and set // up the style sheet (to get the visual for the button working) global $mainframe; $doc =& JFactory::getDocument(); if ($mainframe->isAdmin()) { if ($id == 0) { // New article, explain that we can't add attachments until it is saved // Load the language file from the frontend $lang =& JFactory::getLanguage(); $lang->load('com_attachments', JPATH_SITE); // Save the warning message for the pop-up window require_once JPATH_BASE . DS . '..' . DS . 'components' . DS . 'com_attachments' . DS . 'helper.php'; $msg = JText::_('SAVE ARTICLE BEFORE ATTACHING') . " " . JText::_('TRY APPLY BUTTON FIRST'); AttachmentsHelper::save_warning_message($msg); $button->set('options', "{handler: 'iframe', size: {x: 400, y: 300}}"); $link = "index.php?option=com_attachments&task=warning&tmpl=component"; } else { $button->set('options', "{handler: 'iframe', size: {x: 800, y: 530}}"); $link = "index.php?option=com_attachments&task=add&article_id={$id}&from=closeme"; } $doc->addStyleSheet($mainframe->getSiteURL() . 'plugins/editors-xtd/add_attachment.css', 'text/css', null, array()); } else { if ($id == 0) { // Load the language file from the frontend $lang =& JFactory::getLanguage(); $lang->load('com_attachments', JPATH_SITE); // Save the warning message for the pop-up window require_once JPATH_BASE . DS . 'components' . DS . 'com_attachments' . DS . 'helper.php'; $msg = JText::_('SAVE ARTICLE BEFORE ATTACHING'); AttachmentsHelper::save_warning_message($msg); // New article, explain we can't add attachments until it is saved $button->set('options', "{handler: 'iframe', size: {x: 400, y: 300}}"); $link = "index.php?option=com_attachments&task=warning&tmpl=component"; } else { $button->set('options', "{handler: 'iframe', size: {x: 700, y: 530}}"); $link = "index.php?option=com_attachments&task=upload&artid={$id}&from=closeme"; } $doc->addStyleSheet(JURI::Base() . 'plugins/editors-xtd/add_attachment.css', 'text/css', null, array()); } $button->set('modal', true); $button->set('class', 'modal'); $button->set('text', JText::_('ADD ATTACHMENT')); $button->set('name', 'add_attachment'); $button->set('link', $link); $button->set('image', 'add_attachment.png'); return $button; }
/** * Delete attachment(s) */ public function delete() { // Check for request forgeries JSession::checkToken() or die(JText::_('JINVALID_TOKEN')); // Get ready $app = JFactory::getApplication(); jimport('joomla.filesystem.file'); require_once JPATH_SITE . '/components/com_attachments/helper.php'; // Get the attachments parent manager JPluginHelper::importPlugin('attachments'); $apm = getAttachmentsPluginManager(); // Get attachments to remove from the request $cid = JRequest::getVar('cid', array(), '', 'array'); $deleted_ids = array(); if (count($cid)) { $model = $this->getModel('Attachment'); $attachment = $model->getTable(); // Loop through the attachments and delete them one-by-one foreach ($cid as $attachment_id) { // Load the attachment object $id = (int) $attachment_id; if ($id == 0 or !$attachment->load($id)) { $errmsg = JText::sprintf('ATTACH_ERROR_CANNOT_DELETE_INVALID_ATTACHMENT_ID_N', $id) . ' (ERR 166)'; JError::raiseError(500, $errmsg); } $parent_id = $attachment->parent_id; $parent_type = $attachment->parent_type; $parent_entity = $attachment->parent_entity; // Get the article/parent handler JPluginHelper::importPlugin('attachments'); $apm = getAttachmentsPluginManager(); if (!$apm->attachmentsPluginInstalled($parent_type)) { $errmsg = JText::sprintf('ATTACH_ERROR_INVALID_PARENT_TYPE_S', $parent_type) . ' (ERR 167)'; JError::raiseError(500, $errmsg); } $parent = $apm->getAttachmentsPlugin($parent_type); // If we may not delete it, complain! if ($parent->userMayDeleteAttachment($attachment)) { // Delete the actual file if (JFile::exists($attachment->filename_sys)) { JFile::delete($attachment->filename_sys); AttachmentsHelper::clean_directory($attachment->filename_sys); } $deleted_ids[] = $id; } else { $parent_entity = $parent->getCanonicalEntityId($parent_entity); $errmsg = JText::sprintf('ATTACH_ERROR_NO_PERMISSION_TO_DELETE_S_ATTACHMENT_S_ID_N', $parent_entity, $attachment->filename, $id); $app->enqueueMessage($errmsg, 'warning'); } } // Delete entries in the attachments table for deleted attachments if (!empty($deleted_ids)) { $db = JFactory::getDBO(); $query = $db->getQuery(true); $query->delete('#__attachments')->where("id IN (" . implode(',', $deleted_ids) . ")"); $db->setQuery($query); if (!$db->query()) { $errmsg = $db->getErrorMsg() . ' (ERR 168)'; JError::raiseError(500, $errmsg); } } } // Figure out how to redirect $from = JRequest::getWord('from'); $known_froms = array('frontpage', 'article', 'editor', 'closeme'); if (in_array($from, $known_froms)) { // Get the parent info from the last attachment $parent_id = $attachment->parent_id; $parent_type = $attachment->parent_type; $parent_entity = $attachment->parent_entity; // Get the article/parent handler if (!$apm->attachmentsPluginInstalled($parent_type)) { $errmsg = JText::sprintf('ATTACH_ERROR_INVALID_PARENT_TYPE_S', $parent_type) . ' (ERR 169)'; JError::raiseError(500, $errmsg); } $parent = $apm->getAttachmentsPlugin($parent_type); $parent_entity = $parent->getCanonicalEntityId($parent_entity); // Make sure the parent exists // NOTE: $parent_id===null means the parent is being created if ($parent_id !== null && !$parent->parentExists($parent_id, $parent_entity)) { $parent_entity_name = JText::_('ATTACH_' . $parent_entity); $errmsg = JText::sprintf('ATTACH_ERROR_CANNOT_DELETE_INVALID_S_ID_N', $parent_entity_name, $parent_id) . ' (ERR 170)'; JError::raiseError(500, $errmsg); } // If there is no parent_id, the parent is being created, use the username instead if (!$parent_id) { $pid = 0; } else { $pid = (int) $parent_id; } // Close the iframe and refresh the attachments list in the parent window require_once JPATH_SITE . '/components/com_attachments/javascript.php'; $uri = JFactory::getURI(); $base_url = $uri->base(true); $lang = JRequest::getCmd('lang', ''); AttachmentsJavascript::closeIframeRefreshAttachments($base_url, $parent_type, $parent_entity, $pid, $lang, $from); exit; } $this->setRedirect('index.php?option=' . $this->option); }
/** * Construct and return the attachments list (as HTML) * * @param int $parent_id the id of the parent * @param string $parent_type the type of the parent (usually $option) * @param string $parent_entity the parent entity * @param bool $user_can_add true if the user can add attachments to this parent * @param int $Itemid the system item id (for menus) * @param string $from a token indicating where to return to * @param bool $show_file_links true if the files should be shown as links * @param bool $allow_edit true if the user can edit/delete attachments for this parent * * @return the html as a string */ public static function attachmentsListHTML($parent_id, $parent_type, $parent_entity, $user_can_add, $Itemid, $from, $show_file_links = true, $allow_edit = true) { $app = JFactory::getApplication(); $user = JFactory::getUser(); $user_levels = implode(',', array_unique($user->getAuthorisedViewLevels())); // Make sure there are some potentially accessible attachments for // this parent before proceeding. Note that this check is not as // careful as the check in the Attachments model which is used by // the 'Attachments' view which is invoked below. $alist = ''; $db = JFactory::getDBO(); $query = $db->getQuery(true); $query->select('count(*)')->from('#__attachments'); $query->where('((parent_id=' . (int) $parent_id . ') OR (parent_id is NULL))' . ' AND parent_type=' . $db->quote($parent_type) . ' AND parent_entity=' . $db->quote($parent_entity)); if (!$user->authorise('core.admin')) { $query->where('access in (' . $user_levels . ')'); } $db->setQuery($query); $total = $db->loadResult(); if ($db->getErrorNum()) { $errmsg = $db->stderr() . ' (ERR 47)'; JError::raiseError(500, $errmsg); } // Generate the HTML for the attachments for the specified parent if ($total > 0) { // Get the component parameters jimport('joomla.application.component.helper'); $params = JComponentHelper::getParams('com_attachments'); // Check the security status $attach_dir = JPATH_SITE . '/' . AttachmentsDefines::$ATTACHMENTS_SUBDIR; $secure = $params->get('secure', false); $hta_filename = $attach_dir . '/.htaccess'; if ($secure && !file_exists($hta_filename) || !$secure && file_exists($hta_filename)) { require_once JPATH_SITE . '/components/com_attachments/helper.php'; AttachmentsHelper::setup_upload_directory($attach_dir, $secure); } if ($app->isAdmin()) { // Get the html for the attachments list require_once JPATH_ADMINISTRATOR . '/components/com_attachments/controllers/list.php'; $controller = new AttachmentsControllerList(); $alist = $controller->displayString($parent_id, $parent_type, $parent_entity, null, $show_file_links, $allow_edit, false, $from); } else { // Get the html for the attachments list require_once JPATH_SITE . '/components/com_attachments/controllers/attachments.php'; $controller = new AttachmentsControllerAttachments(); $alist = $controller->displayString($parent_id, $parent_type, $parent_entity, null, $show_file_links, $allow_edit, false, $from); } } return $alist; }
function download() { global $mainframe; if (!$mainframe->isAdmin()) { $errmsg = JText::_('ERROR MUST BE LOGGED IN AS ADMIN'); JError::raiseError(500, $errmsg); } // Get the attachment ID $id = JRequest::getVar('id', null); if (!is_numeric($id)) { JError::raiseError(500, $errmsg); } require_once JPATH_BASE . DS . '..' . DS . 'components' . DS . 'com_attachments' . DS . 'helper.php'; AttachmentsHelper::download_attachment($id); }
function update() { require_once JPATH_COMPONENT_SITE . DS . 'helper.php'; // 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::getVar('id'); if (is_numeric($id)) { $id = intval($id); } else { $errmsg = JText::_('ERROR INVALID ATTACHMENT ID') . " ({$id})"; JError::raiseError(500, $errmsg); exit; } // Get the attachment record $attachment =& JTable::getInstance('attachments', 'Table'); if (!$attachment->load($id)) { $errmsg = JText::_('ERROR CANNOT UPDATE ATTACHMENT INVALID ID') . " ({$id})"; JError::raiseError(500, $errmsg); exit; } // Get the component parameters jimport('joomla.application.component.helper'); $params = JComponentHelper::getParams('com_attachments'); // Verify that this user may add attachments to this article $user =& JFactory::getUser(); $article_id = $attachment->article_id; $article_title = AttachmentsHelper::get_article_title($article_id); require_once JPATH_COMPONENT_SITE . DS . 'permissions.php'; if (!AttachmentsPermissions::user_may_modify_attachment($user, $attachment, $article_id, $params)) { $errmsg = JText::_('ERROR NO PERMISSION TO UPLOAD'); JError::raiseError(500, $errmsg); exit; } // 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); } // Set up the view 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('save_url', "index.php?option=com_attachments&task=save&tmpl=component"); $view->assign('attachment_id', $id); $view->assign('article_id', $article_id); $view->assign('article_title', $article_title); $view->assign('filename', $attachment->filename); $view->assign('description', $attachment->description); $view->assign('display_filename', $attachment->display_filename); $view->assign('user_field_1', $attachment->user_field_1); $view->assign('user_field_2', $attachment->user_field_2); $view->assign('user_field_3', $attachment->user_field_3); $view->assign('from', JRequest::getVar('from', 'closeme')); $view->assign('Itemid', JRequest::getVar('Itemid', 1)); $view->assignRef('params', $params); $view->display(null, false, false, false); }
function regenerate_system_filenames() { require_once JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_attachments' . DS . 'update.php'; $close = JRequest::getVar('close', false); $msg = AttachmentsUpdate::regenerate_system_filenames(); if ($close) { require_once JPATH_SITE . DS . 'components' . DS . 'com_attachments' . DS . 'helper.php'; AttachmentsHelper::enqueueSystemMessage($msg); echo "<script>window.parent.document.getElementById('sbox-window').close();\n window.parent.location.reload();</script>"; } else { global $option; $this->setRedirect('index.php?option=' . $option, $msg); } }
/** * Validate all URLS and update their "valid" status */ public static function validate_urls() { // Get the component parameters jimport('joomla.application.component.helper'); $params = JComponentHelper::getParams('com_attachments'); // Get all the attachment IDs $db = JFactory::getDBO(); $query = $db->getQuery(true); $query->select('id')->from('#__attachments')->where('uri_type=' . $db->quote('url')); $db->setQuery($query); $attachments = $db->loadObjectList(); if ($db->getErrorNum()) { $errmsg = $db->stderr() . ' (ERR 83)'; JError::raiseError(500, $errmsg); } if (count($attachments) == 0) { return JText::_('ATTACH_NO_ATTACHMENTS_WITH_URLS'); } $IDs = array(); foreach ($attachments as $attachment) { $IDs[] = $attachment->id; } // Update the system filenames for all the attachments require_once JPATH_SITE . '/components/com_attachments/helper.php'; JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_attachments/tables'); $attachment = JTable::getInstance('Attachment', 'AttachmentsTable'); $numUpdated = 0; $numChecked = 0; foreach ($IDs as $id) { $attachment->load($id); $a = new JObject(); AttachmentsHelper::get_url_info($attachment->url, $a, false, false); if ($attachment->url_valid != $a->url_valid) { $attachment->url_valid = $a->url_valid; // Maybe update the file info with fresh info if ($a->url_valid) { $attachment->file_size = $a->file_size; $attachment->file_type = $a->file_type; } // Update the record if (!$attachment->store()) { $errmsg = $attachment->getError() . ' (ERR 84)'; JError::raiseError(500, $errmsg); } $numUpdated++; } $numChecked++; } return JText::sprintf('ATTACH_VALIDATED_N_URL_ATTACHMENTS_M_CHANGED', $numChecked, $numUpdated); }
/** * Download an attachment */ public function download() { // Get the attachment ID $id = JRequest::getInt('id'); if (!is_numeric($id)) { $errmsg = JText::sprintf('ATTACH_ERROR_INVALID_ATTACHMENT_ID_N', $id) . ' (ERR 143)'; JError::raiseError(500, $errmsg); } // NOTE: AttachmentsHelper::download_attachment($id) checks access permission AttachmentsHelper::download_attachment($id); }
function newAttachment($article_id, $lists, $option, $from = false) { global $mainframe; $db =& JFactory::getDBO(); // Add the plugins stylesheet to style the list of attachments $document =& JFactory::getDocument(); $document->addStyleSheet($mainframe->getSiteURL() . 'plugins/content/attachments.css', 'text/css', null, array()); // Get the article title (if any) $article_title = false; if ($article_id) { // Get the article names $query = "SELECT * FROM #__content WHERE id='{$article_id}' LIMIT 1"; $db->setQuery($query); $rows = $db->loadObjectList(); if (count($rows) != 1) { $errmsg = JText::_('ERROR INVALID ARTICLE ID') . " ({$article_id})"; JError::raiseError(500, $errmsg); } $article_title = $rows[0]->title; } if ($article_title) { echo "<h1><b>" . JText::_('ARTICLE') . "</b> “{$article_title}”</h1>"; } // Show the existing attachments require_once JPATH_COMPONENT_SITE . DS . 'helper.php'; echo AttachmentsHelper::attachmentsTableHTML($article_id, 'EXISTING ATTACHMENTS', false, false, $from); // Handle the from option if ($from) { $fromval = $from; } else { $fromval = ''; } $save_url = JRoute::_(JURI::root() . "administrator/index.php?option=com_attachments&task=save"); ?> <form class="attachmentsBackend" enctype="multipart/form-data" name="adminForm" id="adminForm" action="<?php echo $save_url; ?> " method="post"> <fieldset class="adminform"> <legend><?php echo JText::_('ADD ATTACHMENT'); ?> </legend> <?php if ($article_id) { ?> <p><input type="hidden" name="article_id" value="<?php echo $article_id; ?> " /></p> <?php } else { ?> <p><label for="article_id"><b><?php echo JText::_('SELECT ARTICLE COLON'); ?> </b></label> <input id="article_title" value="" disabled="disabled" type="text" size="60" /> <a class="modal-button" type="button" title="<?php echo JText::_('SELECT ARTICLE REQUIRED'); ?> " href="index.php?option=com_content&task=element&tmpl=component" rel="{handler: 'iframe', size: {x: 650, y: 375}}"><?php echo JText::_('SELECT ARTICLE'); ?> </a> <input id="article_id" name="article_id" value="-1" type="hidden"> </p> <?php } ?> <p><label for="upload"><b><?php echo JText::_('ATTACH FILE COLON'); ?> </b></label> <input type="file" name="upload" id="upload" size="68" maxlength="512" /></p> <p><label for="display_filename" title="<?php echo JText::_('DISPLAY FILENAME TOOLTIP'); ?> " ><b><?php echo JText::_('DISPLAY FILENAME COLON'); ?> </b></label> <input type="text" name="display_filename" id="display_filename" size="70" maxlength="80" title="<?php echo JText::_('DISPLAY FILENAME TOOLTIP'); ?> " value="" /> <?php echo JText::_('OPTIONAL'); ?> </p> <p><label for="description"><b><?php echo JText::_('DESCRIPTION COLON'); ?> </b></label> <input type="text" name="description" id="description" size="70" maxlength="255" value="" /></p> </fieldset> <input type="hidden" name="MAX_FILE_SIZE" value="524288" /> <input type="hidden" name="option" value="<?php echo $option; ?> " /> <input type="hidden" name="task" value="new" /> <input type="hidden" name="from" value="<?php echo $fromval; ?> " /> <?php if ($from == 'closeme') { ?> <div align="center"> <input type="submit" name="Submit" class="button" onclick="javascript: submitbutton('saveNew')" value="<?php echo JText::_('UPLOAD'); ?> " /> </div> <?php } ?> <?php echo JHTML::_('form.token'); ?> </form> <?php }
/** * Set the parent_id for all attachments that were added to this * content before it was saved the first time. * * This method is called right after the content is saved. * * @param string The context of the content being passed to the plugin. * @param object $item A JTableContent object * @param bool $isNew If the content is newly created * * @return void */ function onContentAfterSave($context, $item, $isNew) { if (!$isNew) { // If the item is not new, this step is not needed return true; } $ctxinfo = explode('.', $context); $parent_type = $ctxinfo[0]; $parent_entity = $ctxinfo[1]; // Special handling for categories if ($parent_type == 'com_categories') { $parent_type = 'com_content'; } // Get the attachments associated with this newly created item. // NOTE: We assume that all attachments that have parent_id=null // and are created by the current user are for this item. $user = JFactory::getUser(); $user_id = $user->get('id'); $db = JFactory::getDBO(); $query = $db->getQuery(true); $query->select('*')->from('#__attachments'); $query->where('created_by=' . (int) $user_id . ' AND parent_id IS NULL'); $db->setQuery($query); $attachments = $db->loadObjectList(); if ($db->getErrorNum()) { $errmsg = $db->stderr() . ' (ERR 200)'; JError::raiseError(500, $errmsg); } // Exit if there are no new attachments if (count($attachments) == 0) { return true; } // Change the attachment to the new content item! JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_attachments/tables'); $atrow = JTable::getInstance('Attachment', 'AttachmentsTable'); foreach ($attachments as $attachment) { // Fix for odd issue; on some systems, something is changing the // parent_type in or out of the database if ($attachment->parent_type == 'com_media' and $attachment->parent_entity == 'article') { // Override/fix the incorrect parent type $attachment->parent_type = 'com_content'; } // Change the filename/URL as necessary $error_msg = AttachmentsHelper::switch_parent($attachment, null, $item->id); if ($error_msg != '') { $errmsg = JText::_($error_msg) . ' (ERR 201)'; JError::raiseError(500, $errmsg); } // Update the parent info $atrow->load($attachment->id); $atrow->parent_id = $item->id; $atrow->parent_type = $parent_type; $atrow->filename_sys = $attachment->filename_sys; $atrow->url = $attachment->url; if (!$atrow->store()) { $errmsg = $attachment->getError() . ' (ERR 202)'; JError::raiseError(500, $errmsg); } } return true; }
/** * Inserts the attachments list above the row of xtd-buttons * * And in older versions, inserts the attachments list for category * descriptions. * * @access public * @since 1.5 */ public function onAfterRender() { $task = JRequest::getCmd('task'); $view = JRequest::getCmd('view'); $layout = JRequest::getWord('layout'); // Make sure this we should handle this $parent_type = JRequest::getCMD('option'); if (!$parent_type) { return; } // Handle the special case of Global Config for Attachments 3.x if (version_compare(JVERSION, '3.0', 'ge')) { if ($parent_type == 'com_config' and $task == '' and $view == '') { // Force use of the Attachments options editor // option=com_config&view=component&component=com_attachments $body = JResponse::getBody(); $body = str_replace('option=com_config&view=component&component=com_attachments', 'option=com_attachments&task=params.edit', $body); JResponse::setBody($body); } } // Handle attachments $parent_entity = 'default'; // Handle categories specially (since they are really com_content) if ($parent_type == 'com_categories') { $parent_type = 'com_content'; $parent_entity = 'category'; } // Get the article/parent handler if (!JPluginHelper::importPlugin('attachments')) { // Exit if the framework does not exist (eg, during uninstallaton) return false; } if (!function_exists('getAttachmentsPluginManager')) { // Exit if the function does not exist (eg, during uninstallaton) return false; } $apm = getAttachmentsPluginManager(); if (!$apm->attachmentsPluginInstalled($parent_type)) { // Exit if there is no Attachments plugin to handle this parent_type return false; } $parent = $apm->getAttachmentsPlugin($parent_type); // Get the parent ID $parent_entity = $parent->getCanonicalEntityId($parent_entity); $parent_id = $parent->getParentIdInEditor($parent_entity, $view, $layout); // Exit if we do not have an parent (exiting or being created) if ($parent_id === false) { return; } // See if this type of content suports displaying attachments in its editor if ($parent->showAttachmentsInEditor($parent_entity, $view, $layout)) { // Get the article/parent handler $user_can_add = $parent->userMayAddAttachment($parent_id, $parent_entity); // Allow remapping of parent ID (eg, for Joomfish) if (jimport('attachments_remapper.remapper')) { $parent_id = AttachmentsRemapper::remapParentID($parent_id, $parent_type, $parent_entity); } // Force the ID to zero when creating the entity if (!$parent_id) { $parent_id = 0; } // Construct the attachment list $Itemid = JRequest::getInt('Itemid', 1); $from = 'editor'; $attachments = AttachmentsHelper::attachmentsListHTML($parent_id, $parent_type, $parent_entity, $user_can_add, $Itemid, $from, false, true); // If the attachments list is empty, insert an empty div for it if ($attachments == '') { jimport('joomla.application.component.helper'); $params = JComponentHelper::getParams('com_attachments'); $class_name = $params->get('attachments_table_style', 'attachmentsList'); $div_id = 'attachmentsList' . '_' . $parent_type . '_' . $parent_entity . '_' . (string) $parent_id; $attachments = "\n<div class=\"{$class_name}\" id=\"{$div_id}\"></div>\n"; } // Insert the attachments above the editor buttons // NOTE: Assume that anyone editing the article can see its attachments $body = $parent->insertAttachmentsListInEditor($parent_id, $parent_entity, $attachments, JResponse::getBody()); JResponse::setBody($body); } elseif ($parent_id && $view == 'category') { // Only dislay this in the front end $app = JFactory::getApplication(); if ($app->isAdmin()) { return; } // More recent versions of Joomla allow this to be handled better // by the normal Attachments onContentPrepare callback if (version_compare(JVERSION, '3.1', 'ge') or version_compare(JVERSION, '2.5.10', 'ge') and version_compare(JVERSION, '3.0', 'lt')) { return; } // Display attachments lists for category descriptions $parent_entity = 'category'; // Add the refresh Javascript $uri = JFactory::getURI(); $base_url = $uri->root(true); $doc = JFactory::getDocument(); // Allow remapping of parent ID (eg, for Joomfish) if (jimport('attachments_remapper.remapper')) { $parent_id = AttachmentsRemapper::remapParentID($parent_id, $parent_type, $parent_entity); } // Figure out if the attachments list should be visible for this category jimport('joomla.application.component.helper'); $params = JComponentHelper::getParams('com_attachments'); $always_show_category_attachments = $params->get('always_show_category_attachments', false); $all_but_article_views = $params->get('hide_except_article_views', false); if ($all_but_article_views && !$always_show_category_attachments) { return; } // Construct the attachment list $Itemid = JRequest::getInt('Itemid', 1); $from = 'frontpage'; $user_can_add = $parent->userMayAddAttachment($parent_id, $parent_entity); $attachments = AttachmentsHelper::attachmentsListHTML($parent_id, $parent_type, $parent_entity, $user_can_add, $Itemid, $from, true, $user_can_add); // If the attachments list is empty, insert an empty div for it if ($attachments == '') { jimport('joomla.application.component.helper'); $class_name = $params->get('attachments_table_style', 'attachmentsList'); $div_id = 'attachmentsList' . '_' . $parent_type . '_' . $parent_entity . '_' . (string) $parent_id; $attachments = "\n<div class=\"{$class_name}\" id=\"{$div_id}\"></div>\n"; } // Insert the attachments after the category description $reptag = '<div class="clr"></div>'; $body = JResponse::getBody(); $body = str_replace($reptag, $attachments . $reptag, $body); JResponse::setBody($body); } }