function restore() { // Get the ID $cid = JRequest::getVar('cid', array(), 'default', 'array'); $id = JRequest::getInt('id'); if (empty($id)) { if (is_array($cid) && !empty($cid)) { $id = $cid[0]; } else { $id = -1; } } // Check that we have a valid-looking ID if ($id <= 0) { $this->setRedirect(JURI::base() . 'index.php?option=com_joomlapack&view=buadmin', JText::_('STATS_ERROR_INVALIDID'), 'error'); parent::display(); return; } // Get the backup record $model =& $this->getModel('statistics'); $model->setId($id); $record =& $model->getStatistic(); if (!is_object($record)) { // The ID does not correspond to a backup record $this->setRedirect(JURI::base() . 'index.php?option=com_joomlapack&view=buadmin', JText::_('STATS_ERROR_INVALIDID'), 'error'); parent::display(); return; } // Check against valid ID's and that the file exists $validIDs =& $model->getValidLookingBackupFiles(); jimport('joomla.filesystem.file'); $isValid = in_array($id, $validIDs) && JFile::exists($record->absolute_path); if (!$isValid) { $this->setRedirect(JURI::base() . 'index.php?option=com_joomlapack&view=buadmin', JText::_('STATS_ERROR_INVALIDID'), 'error'); parent::display(); return; } // Decide what to do, based on backup type switch ($record->type) { case 'full': // Full backup; deploy Kickstart and backup file // Read kickstart.php from the archive $filename = JPATH_COMPONENT_ADMINISTRATOR . DS . 'assets' . DS . 'scripts' . DS . 'kickstart.jpa'; $ret =& $this->_extract($filename); if ($ret === false) { $this->setRedirect(JURI::base() . 'index.php?option=com_joomlapack&view=buadmin', JText::_('STATS_ERROR_RESTOREREADJPA'), 'error'); parent::display(); return; } // Ask for a fresh password $bumodel =& $this->getModel('buadmin'); $password = $bumodel->getRandomPassword(); // Append password information to data $append = '<?php define(\'PASSWORD\', \'' . $password . '\'); ?>'; $ret['data'] = $append . "\n" . $ret['data']; // Write kickstart.php $filename = JPATH_SITE . DS . 'kickstart.php'; if (!JFile::write($filename, $ret['data'])) { $this->setRedirect(JURI::base() . 'index.php?option=com_joomlapack&view=buadmin', JText::_('STATS_ERROR_RESTOREDEPLOY'), 'error'); parent::display(); return; } // Copy the archive; if we failed, remove kickstart as well $archiveParts = $model->getAllFilenames($id); if (!empty($archiveParts)) { foreach ($archiveParts as $from) { $to = JPATH_SITE . DS . basename($from); if (!JFile::copy($from, $to)) { JFile::delete(JPATH_SITE . DS . 'kickstart.php'); $this->setRedirect(JURI::base() . 'index.php?option=com_joomlapack&view=buadmin', JText::_('STATS_ERROR_RESTOREBACKUPDEPLOY'), 'error'); parent::display(); return; } } } // Set the linktarget $URIbase = JURI::base(); $adminPos = strrpos($URIbase, '/administrator'); $URIbase = substr($URIbase, 0, $adminPos); $linktarget = $URIbase . '/kickstart.php'; JRequest::setVar('linktarget', $linktarget); break; case 'dbonly': // Database only; deploy DataRestore and point to backup file // Read datarestore.php from the archive $filename = JPATH_COMPONENT_ADMINISTRATOR . DS . 'assets' . DS . 'scripts' . DS . 'datarestore.jpa'; $ret =& $this->_extract($filename); if ($ret === false) { $this->setRedirect(JURI::base() . 'index.php?option=com_joomlapack&view=buadmin', JText::_('STATS_ERROR_RESTOREREADJPA'), 'error'); parent::display(); return; } // Construct the databases.ini-style array for core database $conf =& JFactory::getConfig(); $databaseArray = array('joomla' => array('host' => $conf->getValue('config.host'), 'username' => $conf->getValue('config.user'), 'password' => $conf->getValue('config.password'), 'database' => $conf->getValue('config.db'), 'prefix' => $conf->getValue('config.dbprefix'), 'dumpFile' => $record->absolute_path)); // Ask for a fresh password $bumodel =& $this->getModel('buadmin'); $password = $bumodel->getRandomPassword(); // Append password information and encrypted array to data jpimport('misc.cryptography'); $serialized = serialize($databaseArray); $encryption = new cryptography(); $encryption->set_key($password); $encrypted = $encryption->encrypt($serialized); $md5 = md5($password); $append = "<?php\ndefine('passwordHash', '{$md5}');\ndefine('encrypted', '{$encrypted}');\n?>\n"; $ret['data'] = $append . $ret['data']; // Write datarestore.php $filename = JPATH_SITE . DS . 'datarestore.php'; if (!JFile::write($filename, $ret['data'])) { $this->setRedirect(JURI::base() . 'index.php?option=com_joomlapack&view=buadmin', JText::_('STATS_ERROR_RESTOREDEPLOY'), 'error'); parent::display(); return; } // Set the linktarget $URIbase = JURI::base(); $adminPos = strrpos($URIbase, '/administrator'); $URIbase = substr($URIbase, 0, $adminPos); $linktarget = $URIbase . '/datarestore.php'; JRequest::setVar('linktarget', $linktarget); break; case 'extradbonly': // Multiple databases; delegate execution to the multirestore view $this->setRedirect(JURI::base() . 'index.php?option=com_joomlapack&view=multirestore&id=' . $id); parent::display(); return; break; default: // Fail for unknown backup types $this->setRedirect(JURI::base() . 'index.php?option=com_joomlapack&view=buadmin', JText::_('STATS_ERROR_RESTOREUNKNOWNTYPE'), 'error'); parent::display(); return; break; } parent::display(); }
/** * We are ready to start the restoration. Generates a password protected datarestore.php * and places it on the site's root. Then presents the user with the information page. */ function ready() { $filename = JPATH_COMPONENT_ADMINISTRATOR . DS . 'assets' . DS . 'scripts' . DS . 'datarestore.jpa'; $ret =& $this->_extract($filename); if ($ret === false) { $this->setRedirect(JURI::base() . 'index.php?option=com_joomlapack&view=buadmin', JText::_('STATS_ERROR_RESTOREREADJPA'), 'error'); parent::display(); return; } // Read databases.ini from the disk and make it into an array $configuration =& JoomlapackModelRegistry::getInstance(); jpimport('helpers.utils', true); $originalDBINI = JoomlapackHelperUtils::parse_ini_file($configuration->getTemporaryDirectory() . DS . 'databases.ini', true); $databaseArray = array(); $counter = 0; foreach ($originalDBINI as $dbDef) { $counter++; $newEntry = array('host' => $dbDef['dbhost'], 'username' => $dbDef['dbuser'], 'password' => $dbDef['dbpass'], 'database' => $dbDef['dbname'], 'prefix' => $dbDef['prefix'], 'dumpFile' => $configuration->getTemporaryDirectory() . DS . $dbDef['sqlfile']); $databaseArray['db' . $counter] = $newEntry; } // Ask for a fresh password $bumodel =& $this->getModel('buadmin'); $password = $bumodel->getRandomPassword(); // Append password information and encrypted array to data jpimport('misc.cryptography'); $serialized = serialize($databaseArray); $encryption = new cryptography(); $encryption->set_key($password); $encrypted = $encryption->encrypt($serialized); $md5 = md5($password); $append = "<?php\ndefine('passwordHash', '{$md5}');\ndefine('encrypted', '{$encrypted}');\n?>\n"; $ret['data'] = $append . $ret['data']; // Write datarestore.php $filename = JPATH_SITE . DS . 'datarestore.php'; if (!JFile::write($filename, $ret['data'])) { $this->setRedirect(JURI::base() . 'index.php?option=com_joomlapack&view=buadmin', JText::_('STATS_ERROR_RESTOREDEPLOY'), 'error'); parent::display(); return; } // Set the linktarget $URIbase = JURI::base(); $adminPos = strrpos($URIbase, '/administrator'); $URIbase = substr($URIbase, 0, $adminPos); $linktarget = $URIbase . '/datarestore.php'; JRequest::setVar('linktarget', $linktarget); parent::display(); }