Esempio n. 1
0
 /**
  * Switch attachment from one parent to another
  *
  * @param &object &$attachment the attachment object
  * @param int $old_parent_id the id for the old parent
  * @param int $new_parent_id the id for the new parent
  * @param string $new_parent_type the new parent type (eg, 'com_content')
  * @param string $new_parent_entity the new parent entity (eg, 'category')
  *
  * @return '' if successful, else an error message
  */
 public static function switch_parent(&$attachment, $old_parent_id, $new_parent_id, $new_parent_type = null, $new_parent_entity = null)
 {
     // Switch the parent as specified, renaming the file as necessary
     // Return success status
     if ($attachment->uri_type == 'url') {
         // Do not need to do any file operations if this is a URL
         return '';
     }
     // Get the article/parent handler
     if ($new_parent_type) {
         $parent_type = $new_parent_type;
         $parent_entity = $new_parent_entity;
     } else {
         $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_UNKNOWN_PARENT_TYPE_S', $parent_type) . ' (ERR 45)';
         JError::raiseError(500, $errmsg);
     }
     $parent = $apm->getAttachmentsPlugin($parent_type);
     // Set up the entity name for display
     $parent_entity = $parent->getCanonicalEntityId($parent_entity);
     $parent_entity_name = JText::_('ATTACH_' . $parent_entity);
     // Get the component parameters
     jimport('joomla.application.component.helper');
     $params = JComponentHelper::getParams('com_attachments');
     // Define where the attachments move to
     $upload_url = AttachmentsDefines::$ATTACHMENTS_SUBDIR;
     $upload_dir = JPATH_SITE . '/' . $upload_url;
     // Figure out the new system filename
     $new_path = $parent->getAttachmentPath($parent_entity, $new_parent_id, null);
     $new_fullpath = $upload_dir . '/' . $new_path;
     // Make sure the new directory exists
     jimport('joomla.filesystem.folder');
     if (!JFolder::create($new_fullpath)) {
         $errmsg = JText::sprintf('ATTACH_ERROR_UNABLE_TO_CREATE_DIR_S', $new_fullpath) . ' (ERR 46)';
         JError::raiseError(500, $errmsg);
     }
     // Construct the new filename and URL
     $old_filename_sys = $attachment->filename_sys;
     $new_filename_sys = $new_fullpath . $attachment->filename;
     $new_url = JString::str_ireplace(DIRECTORY_SEPARATOR, '/', $upload_url . '/' . $new_path . $attachment->filename);
     // Rename the file
     jimport('joomla.filesystem.file');
     if (JFile::exists($new_filename_sys)) {
         return JText::sprintf('ATTACH_ERROR_CANNOT_SWITCH_PARENT_S_NEW_FILE_S_ALREADY_EXISTS', $parent_entity_name, $attachment->filename);
     }
     if (!JFile::move($old_filename_sys, $new_filename_sys)) {
         $new_filename = $new_path . $attachment->filename;
         return JText::sprintf('ATTACH_ERROR_CANNOT_SWITCH_PARENT_S_RENAMING_FILE_S_FAILED', $parent_entity_name, $new_filename);
     }
     AttachmentsHelper::write_empty_index_html($new_fullpath);
     // Save the changes to the attachment record immediately
     $attachment->parent_id = $new_parent_id;
     $attachment->parent_entity = $parent_entity;
     $attachment->parent_entity_name = $parent_entity_name;
     $attachment->filename_sys = $new_filename_sys;
     $attachment->url = $new_url;
     // Clean up after ourselves
     AttachmentsHelper::clean_directory($old_filename_sys);
     return '';
 }
 /**
  * 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;
 }
Esempio n. 3
0
 /**
  * Regenerate the system filenames for all attachments.
  *
  * This function may need to run if the admin has moved the attachments
  * from one computer to another and the actual file paths need to be
  * updated.
  */
 public static function regenerate_system_filenames()
 {
     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 all the attachment IDs
     $db = JFactory::getDBO();
     $query = $db->getQuery(true);
     $query->select('id')->from('#__attachments')->where('uri_type=' . $db->quote('file'));
     $db->setQuery($query);
     $attachments = $db->loadObjectList();
     if ($db->getErrorNum()) {
         $errmsg = $db->stderr() . ' (ERR 72)';
         JError::raiseError(500, $errmsg);
     }
     if (count($attachments) == 0) {
         return JText::_('ATTACH_NO_ATTACHMENTS_WITH_FILES');
     }
     $IDs = array();
     foreach ($attachments as $attachment) {
         $IDs[] = $attachment->id;
     }
     // Get the parent plugin manager
     JPluginHelper::importPlugin('attachments');
     $apm = getAttachmentsPluginManager();
     // Update the system filenames for all the attachments
     jimport('joomla.filesystem.file');
     jimport('joomla.filesystem.folder');
     JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_attachments/tables');
     $attachment = JTable::getInstance('Attachment', 'AttachmentsTable');
     $msg = '';
     $numUpdated = 0;
     $numMissing = 0;
     foreach ($IDs as $id) {
         $attachment->load($id);
         // Get the actual parent id for this attachment
         // (Needed because orphaned parent_id is null, which the Table loads as 1)
         $query = $db->getQuery(true);
         $query->select('parent_id')->from('#__attachments')->where('id = ' . (int) $id);
         $db->setQuery($query, 0, 1);
         $parent_id = $db->loadResult();
         if ($db->getErrorNum()) {
             $errmsg = JText::sprintf('ATTACH_ERROR_INVALID_PARENT_S_ID_N', $attachment->parent_entity, $parent_id) . ' (ERR 73)';
             JError::raiseError(500, $errmsg);
         }
         // Construct the updated system filename
         $old_filename_sys = $attachment->filename_sys;
         // Get info about the system filename
         $finfo = AttachmentsUpdate::checkFilename($old_filename_sys);
         $basename = $finfo->basename;
         // Reconstruct the current system filename (in case of migrations)
         $current_filename_sys = JPATH_SITE . '/' . $finfo->relfile;
         // Get the parent object
         $parent = $apm->getAttachmentsPlugin($attachment->parent_type);
         if (!JFile::exists($current_filename_sys)) {
             $msg .= JText::sprintf('ATTACH_ERROR_MISSING_ATTACHMENT_FILE_S', $current_filename_sys) . "<br/>";
             $numMissing++;
         } elseif (!is_numeric($parent_id) || !$parent->parentExists($attachment->parent_id, $attachment->parent_entity)) {
             $msg .= JText::sprintf('ATTACH_ERROR_MISSING_PARENT_FOR_ATTACHMENT_S', $current_filename_sys) . "<br/>";
             $numMissing++;
         } else {
             // Construct the new system filename and url (based on entities, etc)
             $newdir = $parent->getAttachmentPath($attachment->parent_entity, $attachment->parent_id, null);
             $new_path = $upload_dir . '/' . $newdir;
             if ($finfo->oldstyle && $finfo->prefix) {
                 $new_filename_sys = $new_path . $finfo->basename_no_prefix;
                 $attachment->filename = $finfo->basename_no_prefix;
                 $new_url = str_replace(DIRECTORY_SEPARATOR, '/', $upload_url . '/' . $newdir . $finfo->basename_no_prefix);
             } else {
                 $new_filename_sys = $new_path . $basename;
                 $new_url = str_replace(DIRECTORY_SEPARATOR, '/', $upload_url . '/' . $newdir . $basename);
             }
             // If we are on windows, fix the filename and URL
             if (DIRECTORY_SEPARATOR != '/') {
                 $new_filename_sys = str_replace('/', DIRECTORY_SEPARATOR, $new_filename_sys);
                 $new_url = str_replace(DIRECTORY_SEPARATOR, '/', $new_url);
             }
             // Make sure the target directory exists
             if (!JFile::exists($new_path)) {
                 if (!JFolder::create($new_path)) {
                     $errmsg = JText::sprintf('ATTACH_ERROR_UNABLE_TO_SETUP_UPLOAD_DIR_S', $new_path) . ' (ERR 74)';
                     JError::raiseError(500, $errmsg);
                 }
                 AttachmentsHelper::write_empty_index_html($new_path);
             }
             // Move the file!
             if (!JFile::move($current_filename_sys, $new_filename_sys)) {
                 $errmsg = JText::sprintf('ATTACH_ERROR_RENAMING_FILE_S_TO_S', $old_filename_sys, $new_filename_sys) . ' (ERR 75)';
                 JError::raiseError(500, $errmsg);
             }
             // Verify the new system filename exists!
             if (!JFile::exists($new_filename_sys)) {
                 $errmsg = JText::sprintf('ATTACH_ERROR_NEW_SYSTEM_FILENAME_S_NOT_FOUND', $new_filename_sys) . ' (ERR 76)';
                 JError::raiseError(500, $errmsg);
             }
             // Update the record
             $attachment->filename_sys = $new_filename_sys;
             $attachment->url = $new_url;
             if (!$attachment->store()) {
                 $errmsg = $attachment->getError() . ' (ERR 77)';
                 JError::raiseError(500, $errmsg);
             }
             $numUpdated++;
         }
     }
     // Add warning if there are problem files
     if ($numMissing > 0) {
         $msg = JText::sprintf('ATTACH_ERROR_N_FILES_MISSING', $numMissing) . "<br/>" . $msg . "&nbsp;<br/>";
     }
     return $msg . JText::sprintf('ATTACH_REGENERATED_SYSTEM_FILENAMES_FOR_N_ATTACHMENTS', $numUpdated);
 }