/** * view display method * @return void **/ function display($tpl = null) { global $jlistConfig; //Load pane behavior in joomla 3 jimport('joomla.html.html.tabs'); //initialise variables $document = JFactory::getDocument(); $user = JFactory::getUser(); $this->jdVersion = JDownloadsHelper::getjDownloadsVersion(); $this->addToolbar(); parent::display($tpl); }
/** * logic to export a selected layout * */ public function export() { global $jlistConfig; $app = JFactory::getApplication(); $jinput = JFactory::getApplication()->input; $id = $this->input->get('cid', array(), 'array'); if (count($id) != 1) { // to much layouts selected $app->redirect(JRoute::_('index.php?option=com_jdownloads&view=templates&type=' . (int) $jinput->get('type'), false), JText::_('COM_JDOWNLOADS_LAYOUTS_EXPORT_MSG_COUNT_ERROR'), 'error'); } $id = $id[0]; $jd_version = JDownloadsHelper::getjDownloadsVersion(); $db = JFactory::getDBO(); $prefix = JDownloadsHelper::getCorrectDBPrefix(); JTable::addIncludePath(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_jdownloads' . DS . 'tables'); // load layout data $object = JTable::getInstance('template', 'jdownloadsTable'); $result = $object->load($id); if (!$result) { // abort $app->redirect(JRoute::_('index.php?option=com_jdownloads&view=templates&type=' . (int) $jinput->get('type'), false), JText::_('COM_JDOWNLOADS_LAYOUTS_EXPORT_MSG_ERROR'), 'error'); } else { switch ($object->template_typ) { case '1': $object->template_typ = 'categories'; break; case '4': $object->template_typ = 'category'; break; case '2': $object->template_typ = 'files'; break; case '5': $object->template_typ = 'details'; break; case '3': $object->template_typ = 'summary'; break; case '7': $object->template_typ = 'search'; break; } $file = '<?xml version="1.0" encoding="utf-8"?>' . "\r\n"; $file .= '<layout>' . "\r\n"; $file .= '<!-- This file is a jDownloads layout file -->' . "\r\n\r\n"; $file .= '<!-- Optional: -->' . "\r\n"; $file .= '<creation_date></creation_date>' . "\r\n\r\n"; $file .= '<!-- Optional: -->' . "\r\n"; $file .= '<author></author>' . "\r\n\r\n"; $file .= '<!-- The installed version of jDownloads should be at or later than the version shown below, as otherwise this layout may not work properly. The value is set automatically when a layout is exported (optional) -->' . "\r\n"; $file .= '<targetjdownloads>' . $jd_version . '</targetjdownloads>' . "\r\n\r\n"; $file .= '<!-- This is the name of the layout (required) -->' . "\r\n"; $file .= '<template_name>' . $object->template_name . '</template_name>' . "\r\n\r\n"; $file .= '<!-- Must be one of: categories, category, files, details, summary, search (required) -->' . "\r\n"; $file .= '<template_typ>' . $object->template_typ . '</template_typ>' . "\r\n\r\n"; $file .= '<!-- Start here with header, subheader and footer area -->' . "\r\n"; $file .= '<!-- Header Area Layout -->' . "\r\n"; $file .= '<template_header_text>' . "\r\n" . htmlentities($object->template_header_text) . "\r\n" . '</template_header_text>' . "\r\n\r\n"; $file .= '<!-- Sub Header Area Layout -->' . "\r\n"; $file .= '<template_subheader_text>' . "\r\n" . htmlentities($object->template_subheader_text) . "\r\n" . '</template_subheader_text>' . "\r\n\r\n"; $file .= '<!-- Footer Area Layout -->' . "\r\n"; $file .= '<template_footer_text>' . "\r\n" . htmlentities($object->template_footer_text) . "\r\n" . '</template_footer_text>' . "\r\n\r\n"; $file .= '<!-- Start here with main layout part -->' . "\r\n"; $file .= '<!-- Use Before Layout -->' . "\r\n"; $file .= '<template_before_text>' . "\r\n" . htmlentities($object->template_before_text) . "\r\n" . '</template_before_text>' . "\r\n\r\n"; $file .= '<!-- The Main Layout Field -->' . "\r\n"; $file .= '<template_text>' . "\r\n" . htmlentities($object->template_text) . "\r\n" . '</template_text>' . "\r\n\r\n"; $file .= '<!-- Use After Layout -->' . "\r\n"; $file .= '<template_after_text>' . "\r\n" . htmlentities($object->template_after_text) . "\r\n" . '</template_after_text>' . "\r\n\r\n"; $file .= '<!-- Should be allways 0 -->' . "\r\n"; $file .= '<template_active>0</template_active>' . "\r\n\r\n"; $file .= '<!-- Use 1 for default layout, normal value is 0. Default layouts cannot be deleted by the user. (required) -->' . "\r\n"; $file .= '<template_locked>0</template_locked>' . "\r\n\r\n"; $file .= '<!-- Layout description (optional) -->' . "\r\n"; $file .= '<note>' . "\r\n" . htmlentities($object->note) . "\r\n" . '</note>' . "\r\n\r\n"; $file .= '<!-- Number of columns - only required for categories layouts - default 1 -->' . "\r\n"; $file .= '<cols>' . (int) $object->cols . '</cols>' . "\r\n\r\n"; $file .= '<!-- Default = 1 - only usable in files layouts - use 0 when the layout has checkboxes for mass downloads -->' . "\r\n"; $file .= '<checkbox_off>' . (int) $object->checkbox_off . '</checkbox_off>' . "\r\n\r\n"; $file .= '<!-- Default = 0 - should only be 1 for categories layouts when the layout shall be used to list sub categories -->' . "\r\n"; $file .= '<use_to_view_subcats>' . (int) $object->use_to_view_subcats . '</use_to_view_subcats>' . "\r\n\r\n"; $file .= '<!-- Default = 1 - only usable in files and details layouts - use 0 when the layout is to use mini symbols for some main data -->' . "\r\n"; $file .= '<symbol_off>' . (int) $object->symbol_off . '</symbol_off>' . "\r\n\r\n"; $file .= '<!-- Use * for all languages (default) - Note: this language field is not used currently, it is a possible future use in layouts. -->' . "\r\n"; $file .= '<language>' . $object->language . '</language>' . "\r\n"; $file .= '</layout>'; $filename = 'exported_layout_jd_' . $jd_version . '_' . str_replace(' ', '_', $object->template_name . '.xml'); // SET HEADER TO OUTPUT DATA header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D,d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header('Cache-Control: post-check=0, pre-check=0', false); header('Pragma: no-cache'); header('Content-type: text/xml'); header('Content-Disposition: attachment; filename="' . $filename . '"'); print $file; } exit; }
/** * logic for create the backup file * */ public function runbackup() { global $jlistConfig; $jinput = JFactory::getApplication()->input; $jd_version = JDownloadsHelper::getjDownloadsVersion(); $jd_version = str_replace(' ', '_', $jd_version); $add_also_logs = $jinput->get('logs', 0, 'int'); // check user access right if (JFactory::getUser()->authorise('com_jdownloads.edit.config', 'com_jdownloads')) { $db = JFactory::getDBO(); $prefix = JDownloadsHelper::getCorrectDBPrefix(); JTable::addIncludePath(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_jdownloads' . DS . 'tables'); if ($add_also_logs) { $dbtables = array($prefix . 'jdownloads_config', $prefix . 'jdownloads_categories', $prefix . 'jdownloads_files', $prefix . 'jdownloads_licenses', $prefix . 'jdownloads_ratings', $prefix . 'jdownloads_logs', $prefix . 'jdownloads_templates', $prefix . 'jdownloads_usergroups_limits', $prefix . 'assets'); } else { // logs are not stored $dbtables = array($prefix . 'jdownloads_config', $prefix . 'jdownloads_categories', $prefix . 'jdownloads_files', $prefix . 'jdownloads_licenses', $prefix . 'jdownloads_ratings', $prefix . 'jdownloads_templates', $prefix . 'jdownloads_usergroups_limits', $prefix . 'assets'); } $file = '<?php' . "\r\n"; for ($i = 0; $i < count($dbtables); $i++) { // the target db can has an other prefix, so we can not use it here $table_name = str_replace($prefix, '#__', $dbtables[$i]); // make not the Joomla asset table empty!!! if ($dbtables[$i] != $prefix . 'assets') { $file .= '$db->setQuery("TRUNCATE TABLE `' . $table_name . '`") ;$db->execute();' . "\r\n"; } else { // only remove all olders jdownloads categories and downloads from asset table // but not the component root item (level=1) $file .= '$db->setQuery("DELETE FROM `' . $table_name . '` WHERE `name` LIKE ' . $db->quote('com_jdownloads%') . ' AND `level` > ' . $db->quote('1') . '"); $db->execute();' . "\r\n"; } } // we will backup not the assets in this version array_pop($dbtables); foreach ($dbtables as $dbtable) { if ($dbtable == $prefix . 'jdownloads_ratings' || $dbtable == $prefix . 'jdownloads_files') { $db->setQuery("SELECT file_id FROM {$dbtable}"); } else { $db->setQuery("SELECT id FROM {$dbtable}"); } // alternate when we will get also the assets data (not useful in this version): /*} elseif ($dbtable == $prefix.'assets' ){ $db->setQuery("SELECT id FROM $dbtable WHERE `name` LIKE 'com_jdownloads%' AND `level` > '1'"); } else { $db->setQuery("SELECT id FROM $dbtable"); } */ $xids = $db->loadObjectList(); foreach ($xids as $xid) { switch ($dbtable) { case $prefix . 'jdownloads_config': $object = JTable::getInstance('config', 'jdownloadsTable'); break; case $prefix . 'jdownloads_categories': $object = JTable::getInstance('category', 'jdownloadsTable'); break; case $prefix . 'jdownloads_files': $object = JTable::getInstance('download', 'jdownloadsTable'); break; case $prefix . 'jdownloads_licenses': $object = JTable::getInstance('license', 'jdownloadsTable'); break; case $prefix . 'jdownloads_templates': $object = JTable::getInstance('template', 'jdownloadsTable'); break; case $prefix . 'jdownloads_logs': $object = JTable::getInstance('log', 'jdownloadsTable'); break; case $prefix . 'jdownloads_ratings': $object = JTable::getInstance('rating', 'jdownloadsTable'); break; case $prefix . 'jdownloads_usergroups_limits': $object = JTable::getInstance('group', 'jdownloadsTable'); break; case $prefix . 'assets': $object = JTable::getInstance('assets', 'jdownloadsTable'); break; } // get the data row if ($dbtable == $prefix . 'jdownloads_files') { $object->load($xid->file_id); } elseif ($dbtable == $prefix . 'jdownloads_ratings') { $db->setQuery("SELECT * FROM " . $prefix . 'jdownloads_ratings' . " WHERE `file_id` = '{$xid->file_id}'"); $row = $db->loadObject(); } else { $object->load($xid->id); } // the target db can has an other prefix, so we can not use it here $table_name = str_replace($prefix, '#__', $dbtable); if ($table_name != '#__jdownloads_ratings') { $sql = '$db->setQuery("INSERT INTO ' . $table_name . ' ( %s ) VALUES ( %s );"); $db->execute();$i++; ' . "\r\n"; $fields = array(); $values = array(); foreach (get_object_vars($object) as $k => $v) { if (is_array($v) or is_object($v) or $v === NULL) { continue; } if ($k[0] == '_') { continue; } // set field name $fields[] = $db->quoteName($k); // set field value (but not for ID field from assets table!!!) if ($table_name == '#__assets' && $k == 'id') { $values[] = "''"; } else { // write 0 to asset id if ($k == 'asset_id') { $values[] = "'0'"; } else { $values[] = $db->Quote($v); } } } $file .= sprintf($sql, implode(",", $fields), implode(",", $values)); } else { // special handling for ratings table required, then we have here not a primary key $file .= '$db->setQuery("INSERT INTO ' . $table_name . ' ( `file_id`,`rating_sum`,`rating_count`,`lastip` ) VALUES ( ' . $db->quote($row->file_id) . ',' . $db->quote($row->rating_sum) . ',' . $db->quote($row->rating_count) . ',' . $db->quote($row->last_ip) . ' );"); $db->execute();$i++; ' . "\r\n"; } } } $date_current = JHtml::_('date', '', 'Y-m-d_H:i:s'); $file .= "\r\n?>"; header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D,d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header('Cache-Control: post-check=0, pre-check=0', false); header("Pragma: no-cache"); header("Content-type: text/plain"); header('Content-Disposition: attachment; filename="' . 'backup_jdownloads_v' . $jd_version . '_date_' . $date_current . '_.txt' . '"'); print $file; } exit; }
/** * logic to store the data from the layout file in the database * */ public function install() { global $jlistConfig; // Check for request forgeries. JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); // Access check. if (!JFactory::getUser()->authorise('edit.config', 'com_jdownloads')) { JError::raiseWarning(404, JText::_('JERROR_ALERTNOAUTHOR')); $app->redirect(JRoute::_('index.php?option=com_jdownloads&view=layouts', false)); } else { jimport('joomla.filesystem.file'); $app = JFactory::getApplication(); $db = JFactory::getDBO(); ini_set('max_execution_time', '300'); ignore_user_abort(true); flush(); $original_upload_dir = $jlistConfig['files.uploaddir']; // get layout file $file = JArrayHelper::getValue($_FILES, 'install_file', array('tmp_name' => '')); // when file is not valid exit if (!$file['type'] == 'text/xml') { $app->redirect(JRoute::_('index.php?option=com_jdownloads&view=layouts', false), JText::_('COM_JDOWNLOADS_LAYOUTS_IMPORT_MSG_WRONG_FILE_ERROR'), 'error'); } // save it in tempzipfile folder $upload_path = $jlistConfig['files.uploaddir'] . '/' . $jlistConfig['tempzipfiles.folder.name'] . '/' . $file['name']; // check whether a file with the same name already exist if (JFile::exists($upload_path)) { $res = JFile::delete($upload_path); } // since Joomla 3.4 we need additional params to allow unsafe file (backup file contains php content) //if (!JFile::upload($file['tmp_name'], $upload_path, false, true)){ // we need unfiltered data in this case if (!move_uploaded_file($file['tmp_name'], $upload_path)) { $app->redirect(JRoute::_('index.php?option=com_jdownloads&view=layouts', false), JText::_('COM_JDOWNLOADS_LAYOUTS_IMPORT_MSG_STORE_ERROR'), 'error'); } $xml = simplexml_load_file($upload_path); if ($xml->template_typ) { if ($xml->targetjdownloads) { // versions check $current_version = JDownloadsHelper::getjDownloadsVersion(); $result = version_compare($xml->targetjdownloads, $current_version, '<='); if (!$result) { // installed version is to old for this layout $app->redirect(JRoute::_('index.php?option=com_jdownloads&view=layouts', false), JText::_('COM_JDOWNLOADS_LAYOUTS_IMPORT_MSG_WRONG_VERSION_ERROR'), 'error'); } } switch ($xml->template_typ) { case 'categories': $xml->template_typ = '1'; break; case 'category': $xml->template_typ = '4'; break; case 'files': $xml->template_typ = '2'; break; case 'details': $xml->template_typ = '5'; break; case 'summary': $xml->template_typ = '3'; break; case 'search': $xml->template_typ = '7'; break; default: // wrong layout type $app->redirect(JRoute::_('index.php?option=com_jdownloads&view=layouts', false), JText::_('COM_JDOWNLOADS_LAYOUTS_IMPORT_MSG_WRONG_FILE_ERROR'), 'error'); } // remove \r\n $xml->note = trim($db->escape($xml->note)); $xml->template_header_text = trim($db->escape($xml->template_header_text)); $xml->template_subheader_text = trim($db->escape($xml->template_subheader_text)); $xml->template_footer_text = trim($db->escape($xml->template_footer_text)); $xml->template_before_text = trim($db->escape($xml->template_before_text)); $xml->template_text = trim($db->escape($xml->template_text)); $xml->template_after_text = trim($db->escape($xml->template_after_text)); if ($xml->author != '') { $note = $xml->note . "\r\n{" . JText::_('COM_JDOWNLOADS_BACKEND_FILESLIST_AUTHOR') . ': ' . $xml->author; if ($xml->creation_date != '') { $note .= ' - ' . $xml->creation_date . '}'; } else { $note .= '}'; } } else { $note = $xml->note; } $db->setQuery("INSERT INTO #__jdownloads_templates (`id`, `template_name`, `template_typ`, `template_header_text`, `template_subheader_text`, `template_footer_text`, `template_before_text`, `template_text`, `template_after_text`, `template_active`, `locked`, `note`, `cols`, `checkbox_off`, `use_to_view_subcats`, `symbol_off`, `language`)\n VALUES ( 'NULL', '{$xml->template_name}', '{$xml->template_typ}', '{$xml->template_header_text}', '{$xml->template_subheader_text}', '{$xml->template_footer_text}', '{$xml->template_before_text}', '{$xml->template_text}', '{$xml->template_after_text}', '{$xml->template_active}', '{$xml->locked}', '{$note}', '{$xml->cols}', '{$xml->checkbox_off}', '{$xml->use_to_view_subcats}', '{$xml->symbol_off}', '{$xml->language}')"); $result = $db->execute(); if (!$result) { // MySQL error $app->redirect(JRoute::_('index.php?option=com_jdownloads&view=layouts', false), JText::_('COM_JDOWNLOADS_LAYOUTS_IMPORT_MSG_MYSQL_ERROR'), 'error'); } } else { // invalid file $app->redirect(JRoute::_('index.php?option=com_jdownloads&view=layouts', false), JText::_('COM_JDOWNLOADS_LAYOUTS_IMPORT_MSG_WRONG_FILE_ERROR'), 'error'); } } $app->redirect(JRoute::_('index.php?option=com_jdownloads&view=layouts', false), JText::_('COM_JDOWNLOADS_LAYOUTS_IMPORT_MSG_SUCCESSFUL')); }