Exemplo n.º 1
0
 /**
  * Backup all modified files
  *
  * @return  void
  */
 public function backupAction()
 {
     // Initialize variables
     $app = JFactory::getApplication();
     $joomlaConfig = JFactory::getConfig();
     $packageFile = $joomlaConfig->get('tmp_path') . '/jsn-' . $this->template['id'] . '.zip';
     $templatePath = JPATH_ROOT . '/templates/' . $this->template['name'];
     $backupPath = $joomlaConfig->get('tmp_path') . '/' . $this->template['name'] . '_modified_files.zip';
     if (is_readable($packageFile)) {
         $modifiedFiles = JSNTplHelper::getModifiedFilesBeingUpdated($this->template['name'], $packageFile);
     } else {
         $modifiedFiles = JSNTplHelper::getModifiedFiles($this->template['name']);
         $modifiedFiles = array_merge($modifiedFiles['add'], $modifiedFiles['edit']);
     }
     // Check if backup was done before
     if (!$app->getUserState('jsn-tplfw-backup-done') or !is_file($backupPath)) {
         // Read all modified files
         foreach ($modifiedFiles as $file) {
             // Create array of file name and content for making archive later
             $files[] = array('name' => $file, 'data' => JFile::read("{$templatePath}/{$file}"));
         }
         // Create backup archive
         $archiver = new JSNTplArchiveZip();
         $archiver->create($backupPath, $files);
         // State that backup is created
         $app->setUserState('jsn-tplfw-backup-done', 1);
     }
     $this->setResponse($backupPath);
 }
Exemplo n.º 2
0
 /**
  * Method to backup current Joomla database
  *
  * @return  void
  */
 private function _backupDatabase()
 {
     // Get Joomla config
     $config = JFactory::getConfig();
     // Preset backup buffer
     $buffer = '';
     // Generate file path to write SQL backup
     $file = $config->get('tmp_path') . '/' . $this->template['name'] . '_original_site_data.sql';
     $numb = 1;
     // Get object for working with Joomla database
     $db = JFactory::getDbo();
     // Get all tables in Joomla database
     $tables = $db->getTableList();
     // Loop thru all tables to backup table structure and data
     foreach ($tables as $table) {
         // Create drop table statement
         $buffer .= (empty($buffer) ? '' : "\n\n") . "DROP TABLE IF EXISTS `{$table}`;";
         // Re-create create table statement
         $create = $db->getTableCreate($table);
         $buffer .= "\n" . array_shift($create) . ';';
         // Get all table columns
         $columns = '`' . implode('`, `', array_keys($db->getTableColumns($table, false))) . '`';
         // Get the number of data row in this table
         $q = $db->getQuery(true);
         $q->select('COUNT(*)');
         $q->from($table);
         $q->where('1');
         $db->setQuery($q);
         if ($max = (int) $db->loadResult()) {
             for ($offset = 0, $limit = 50; $max - $offset > 0; $offset += $limit) {
                 // Query for all table data
                 $q = $db->getQuery(true);
                 $q->select('*');
                 $q->from($table);
                 $q->where('1');
                 $db->setQuery($q, $offset, $limit);
                 if ($rows = $db->loadRowList()) {
                     $data = array();
                     foreach ($rows as $row) {
                         $tmp = array();
                         // Prepare data for creating insert statement for each row
                         foreach ($row as $value) {
                             $tmp[] = $db->quote($value);
                         }
                         $data[] = implode(', ', $tmp);
                     }
                     // Create insert statement for fetched rows
                     $q2 = $db->getQuery(true);
                     $q2->insert($table);
                     $q2->columns($columns);
                     $q2->values($data);
                     // Store insert statement
                     $insert = "\n" . str_replace('),(', "),\n(", (string) $q2) . ';';
                     // Write generated SQL statements to file if reached 2MB limit
                     if (strlen($buffer) + strlen($insert) > 2097152) {
                         if (!JFile::write($file, $buffer)) {
                             throw new Exception(JText::_('JSN_TPLFW_CANNOT_CREATE_BACKUP_FILE'));
                         }
                         // Rename current backup file if neccessary
                         if ($numb == 1) {
                             JFile::move($file, substr($file, 0, -4) . '.01.sql');
                         }
                         // Increase number of backup file
                         $numb++;
                         // Generate new backup file name
                         $file = $config->get('tmp_path') . '/' . $this->template['name'] . '_original_site_data.' . ($numb < 10 ? '0' : '') . $numb . '.sql';
                         // Reset backup buffer
                         $buffer = trim($insert);
                     } else {
                         $buffer .= $insert;
                     }
                 } else {
                     break;
                 }
             }
         }
     }
     if (!JFile::write($file, $buffer)) {
         throw new Exception(JText::_('JSN_TPLFW_CANNOT_CREATE_BACKUP_FILE'));
     }
     // Get list of backup file
     $files = glob($config->get('tmp_path') . '/' . $this->template['name'] . '_original_site_data.*');
     foreach ($files as $k => $file) {
         // Create array of file name and content for making archive later
         $files[$k] = array('name' => basename($file), 'data' => JFile::read($file));
     }
     // Create backup archive
     $archiver = new JSNTplArchiveZip();
     $zip_path = JPATH_ROOT . '/templates/' . $this->template['name'] . '/backups/' . date('y-m-d_H-i-s') . '_original_site_data.zip';
     if ($archiver->create($zip_path, $files)) {
         // Remove all SQL backup file created previously in temporary directory
         foreach ($files as $file) {
             JFile::delete($config->get('tmp_path') . '/' . $file['name']);
         }
     }
 }