コード例 #1
0
 /**
  * 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;
 }
コード例 #2
0
ファイル: attachment.php プロジェクト: site4com/prometheus
 /**
  * Save an attachment (from editing)
  */
 public function save($key = null, $urlVar = null)
 {
     // Check for request forgeries
     JSession::checkToken() or die(JText::_('JINVALID_TOKEN'));
     // Access check.
     $user = JFactory::getUser();
     if (!($user->authorise('core.edit', 'com_attachments') or $user->authorise('core.edit.own', 'com_attachments'))) {
         return JError::raiseError(403, JText::_('JERROR_ALERTNOAUTHOR') . ' (ERR 134)');
     }
     $model = $this->getModel();
     $attachment = $model->getTable();
     // Make sure the article ID is valid
     $attachment_id = JRequest::getInt('id');
     if (!$attachment->load($attachment_id)) {
         $errmsg = JText::sprintf('ATTACH_ERROR_CANNOT_UPDATE_ATTACHMENT_INVALID_ID_N', $id) . ' (ERR 135)';
         JError::raiseError(500, $errmsg);
     }
     // Note the old uri type
     $old_uri_type = $attachment->uri_type;
     // Get the data from the form
     if (!$attachment->bind(JRequest::get('post'))) {
         $errmsg = $attachment->getError() . ' (ERR 136)';
         JError::raiseError(500, $errmsg);
     }
     // Get the parent handler for this attachment
     JPluginHelper::importPlugin('attachments');
     $apm = getAttachmentsPluginManager();
     if (!$apm->attachmentsPluginInstalled($attachment->parent_type)) {
         $errmsg = JText::sprintf('ATTACH_ERROR_INVALID_PARENT_TYPE_S', $attachment->parent_type) . ' (ERR 135B)';
         JError::raiseError(500, $errmsg);
     }
     $parent = $apm->getAttachmentsPlugin($attachment->parent_type);
     // See if the parent ID has been changed
     $parent_changed = false;
     $old_parent_id = JRequest::getString('old_parent_id');
     if ($old_parent_id == '') {
         $old_parent_id = null;
     } else {
         $old_parent_id = JRequest::getInt('old_parent_id');
     }
     // Handle new parents (in process of creation)
     if ($parent->newParent($attachment)) {
         $attachment->parent_id = null;
     }
     // Deal with updating an orphaned attachment
     if ($old_parent_id == null && is_numeric($attachment->parent_id)) {
         $parent_changed = true;
     }
     // Check for normal parent changes
     if ($old_parent_id && $attachment->parent_id != $old_parent_id) {
         $parent_changed = true;
     }
     // See if we are updating a file or URL
     $new_uri_type = JRequest::getWord('update');
     if ($new_uri_type && !in_array($new_uri_type, AttachmentsDefines::$LEGAL_URI_TYPES)) {
         // Make sure only legal values are entered
         $new_uri_type = '';
     }
     // See if the parent type has changed
     $new_parent_type = JRequest::getCmd('new_parent_type');
     $new_parent_entity = JRequest::getCmd('new_parent_entity');
     $old_parent_type = JRequest::getCmd('old_parent_type');
     $old_parent_entity = JRequest::getCmd('old_parent_entity');
     if ($new_parent_type && ($new_parent_type != $old_parent_type || $new_parent_entity != $old_parent_entity)) {
         $parent_changed = true;
     }
     // If the parent has changed, make sure they have selected the new parent
     if ($parent_changed && (int) $attachment->parent_id == -1) {
         $errmsg = JText::sprintf('ATTACH_ERROR_MUST_SELECT_PARENT');
         echo "<script type=\"text/javascript\"> alert('{$errmsg}'); window.history.go(-1); </script>\n";
         exit;
     }
     // If the parent has changed, switch the parent, rename files if necessary
     if ($parent_changed) {
         if ($new_uri_type == 'url' && $old_uri_type == 'file') {
             // If we are changing parents and converting from file to URL, delete the old file
             jimport('joomla.filesystem.file');
             // Load the attachment so we can get its filename_sys
             $db = JFactory::getDBO();
             $query = $db->getQuery(true);
             $query->select('filename_sys, id')->from('#__attachments')->where('id=' . (int) $attachment->id);
             $db->setQuery($query, 0, 1);
             $filename_sys = $db->loadResult();
             JFile::delete($filename_sys);
             AttachmentsHelper::clean_directory($filename_sys);
         } else {
             // Otherwise switch the file/url to the new parent
             if ($old_parent_id == null) {
                 $old_parent_id = 0;
                 // NOTE: When attaching a file to an article during creation,
                 //		 the article_id (parent_id) is initially null until
                 //		 the article is saved (at that point the
                 //		 parent_id/article_id updated).	 If the attachment is
                 //		 added and creating the article is canceled, the
                 //		 attachment exists but is orhpaned since it does not
                 //		 have a parent.	 It's article_id is null, but it is
                 //		 saved in directory as if its article_id is 0:
                 //		 article/0/file.txt.  Therefore, if the parent has
                 //		 changed, we pretend the old_parent_id=0 for file
                 //		 renaming/moving.
             }
             $error_msg = AttachmentsHelper::switch_parent($attachment, $old_parent_id, $attachment->parent_id, $new_parent_type, $new_parent_entity);
             if ($error_msg != '') {
                 $errmsg = JText::_($error_msg) . ' (ERR 137)';
                 $link = 'index.php?option=com_attachments';
                 $this->setRedirect($link, $errmsg, 'error');
                 return;
             }
         }
     }
     // Update parent type/entity, if needed
     if ($new_parent_type && $new_parent_type != $old_parent_type) {
         $attachment->parent_type = $new_parent_type;
     }
     if ($new_parent_type && $new_parent_entity != $old_parent_entity) {
         $attachment->parent_entity = $new_parent_entity;
     }
     // Get the article/parent handler
     if ($new_parent_type) {
         $parent_type = $new_parent_type;
         $parent_entity = $new_parent_entity;
     } else {
         $parent_type = JRequest::getCmd('parent_type', 'com_content');
         $parent_entity = JRequest::getCmd('parent_entity', 'default');
     }
     $parent = $apm->getAttachmentsPlugin($parent_type);
     $parent_entity = $parent->getCanonicalEntityId($parent_entity);
     // Get the title of the article/parent
     $new_parent = JRequest::getBool('new_parent', false);
     $parent->new = $new_parent;
     if ($new_parent) {
         $attachment->parent_id = null;
         $parent->title = '';
     } else {
         $parent->title = $parent->getTitle($attachment->parent_id, $parent_entity);
     }
     // Check to make sure the user has permissions to edit the attachment
     if (!$parent->userMayEditAttachment($attachment)) {
         // ??? Add better error message
         return JError::raiseError(403, JText::_('JERROR_ALERTNOAUTHOR') . ' (ERR 139)');
     }
     // Double-check to see if the URL changed
     $old_url = JRequest::getString('old_url');
     if (!$new_uri_type && $old_url && $old_url != $attachment->url) {
         $new_uri_type = 'url';
     }
     // If this is a URL, get settings
     $verify_url = false;
     $relative_url = false;
     if ($new_uri_type == 'url') {
         // See if we need to verify the URL (if applicable)
         if (JRequest::getWord('verify_url') == 'verify') {
             $verify_url = true;
         }
         // Allow relative URLs?
         if (JRequest::getWord('url_relative') == 'relative') {
             $relative_url = true;
         }
     }
     // Compute the update time
     $now = JFactory::getDate();
     // Update create/modify info
     $attachment->modified_by = $user->get('id');
     $attachment->modified = $now->toSql();
     // Upload new file/url and create/update the attachment
     $msg = null;
     $msgType = 'message';
     if ($new_uri_type == 'file') {
         // Upload a new file
         $result = AttachmentsHelper::upload_file($attachment, $parent, $attachment_id, 'update');
         if (is_object($result)) {
             $msg = $result->error_msg . ' (ERR 140)';
             $msgType = 'error';
         } else {
             $msg = $result;
         }
         // NOTE: store() is not needed if upload_file() is called since it does it
     } elseif ($new_uri_type == 'url') {
         // Upload/add the new URL
         $result = AttachmentsHelper::add_url($attachment, $parent, $verify_url, $relative_url, $old_uri_type, $attachment_id);
         // NOTE: store() is not needed if add_url() is called since it does it
         if (is_object($result)) {
             $msg = $result->error_msg . ' (ERR 141)';
             $msgType = 'error';
         } else {
             $msg = $result;
         }
     } else {
         // Extra handling for checkboxes for URLs
         if ($attachment->uri_type == 'url') {
             // Update the url_relative field
             $attachment->url_relative = $relative_url;
             $attachment->url_verify = $verify_url;
         }
         // Remove any extraneous fields
         if (isset($attachment->parent_entity_name)) {
             unset($attachment->parent_entity_name);
         }
         // Save the updated attachment info
         if (!$attachment->store()) {
             $errmsg = $attachment->getError() . ' (ERR 142)';
             JError::raiseError(500, $errmsg);
         }
     }
     switch ($this->getTask()) {
         case 'apply':
             if (!$msg) {
                 $msg = JText::_('ATTACH_CHANGES_TO_ATTACHMENT_SAVED');
             }
             $link = 'index.php?option=com_attachments&task=attachment.edit&cid[]=' . (int) $attachment->id;
             break;
         case 'save':
         default:
             if (!$msg) {
                 $msg = JText::_('ATTACH_ATTACHMENT_UPDATED');
             }
             $link = 'index.php?option=com_attachments';
             break;
     }
     // If invoked from an iframe popup, close it and refresh the attachments list
     $from = JRequest::getWord('from');
     $known_froms = $parent->knownFroms();
     if (in_array($from, $known_froms)) {
         // If there has been a problem, alert the user and redisplay
         if ($msgType == 'error') {
             $errmsg = $msg;
             if (DIRECTORY_SEPARATOR == "\\") {
                 // Fix filename on Windows system so alert can display it
                 $errmsg = str_replace(DIRECTORY_SEPARATOR, "\\\\", $errmsg);
             }
             $errmsg = str_replace("'", "\\'", $errmsg);
             $errmsg = str_replace("<br />", "\\n", $errmsg);
             echo "<script type=\"text/javascript\"> alert('{$errmsg}');  window.history.go(-1); </script>";
             exit;
         }
         // Can only refresh the old parent
         if ($parent_changed) {
             $parent_type = $old_parent_type;
             $parent_entity = $old_parent_entity;
             $parent_id = $old_parent_id;
         } else {
             $parent_id = (int) $attachment->parent_id;
         }
         // Close the iframe and refresh the attachments list in the parent window
         $uri = JFactory::getURI();
         $base_url = $uri->base(true);
         $lang = JRequest::getCmd('lang', '');
         AttachmentsJavascript::closeIframeRefreshAttachments($base_url, $parent_type, $parent_entity, $parent_id, $lang, $from);
         exit;
     }
     $this->setRedirect($link, $msg, $msgType);
 }