Пример #1
0
 /**
  * The function which does the actual work (or dispatches it to the relevant places)
  */
 function convert_data($sub)
 {
     global $template, $user, $phpbb_root_path, $phpEx, $db, $lang, $config, $cache, $auth;
     global $convert, $convert_row, $message_parser, $skip_rows, $language;
     global $request, $phpbb_config_php_file, $phpbb_dispatcher;
     extract($phpbb_config_php_file->get_all());
     require $phpbb_root_path . 'includes/constants.' . $phpEx;
     require $phpbb_root_path . 'includes/functions_convert.' . $phpEx;
     $dbms = $phpbb_config_php_file->convert_30_dbms_to_31($dbms);
     $db = new $dbms();
     $db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true);
     unset($dbpasswd);
     // We need to fill the config to let internal functions correctly work
     $config = new \phpbb\config\db($db, new \phpbb\cache\driver\dummy(), CONFIG_TABLE);
     // Override a couple of config variables for the duration
     $config['max_quote_depth'] = 0;
     // @todo Need to confirm that max post length in source is <= max post length in destination or there may be interesting formatting issues
     $config['max_post_chars'] = $config['min_post_chars'] = 0;
     // Set up a user as well. We _should_ have enough of a database here at this point to do this
     // and it helps for any core code we call
     $user->session_begin();
     $user->page = $user->extract_current_page($phpbb_root_path);
     // This is a little bit of a fudge, but it allows the language entries to be available to the
     // core code without us loading them again
     $user->lang =& $lang;
     $this->page_title = $user->lang['STAGE_IN_PROGRESS'];
     $convert->options = array();
     if (isset($config['convert_progress'])) {
         $convert->options = unserialize($config['convert_progress']);
         $convert->options = array_merge($convert->options, unserialize($config['convert_db_server']), unserialize($config['convert_db_user']), unserialize($config['convert_options']));
     }
     // This information should have already been checked once, but do it again for safety
     if (empty($convert->options) || empty($convert->options['tag']) || !isset($convert->options['dbms']) || !isset($convert->options['dbhost']) || !isset($convert->options['dbport']) || !isset($convert->options['dbuser']) || !isset($convert->options['dbpasswd']) || !isset($convert->options['dbname']) || !isset($convert->options['table_prefix'])) {
         $this->p_master->error($user->lang['NO_CONVERT_SPECIFIED'], __LINE__, __FILE__);
     }
     // Make some short variables accessible, for easier referencing
     $convert->convertor_tag = basename($convert->options['tag']);
     $convert->src_dbms = $convert->options['dbms'];
     $convert->src_dbhost = $convert->options['dbhost'];
     $convert->src_dbport = $convert->options['dbport'];
     $convert->src_dbuser = $convert->options['dbuser'];
     $convert->src_dbpasswd = $convert->options['dbpasswd'];
     $convert->src_dbname = $convert->options['dbname'];
     $convert->src_table_prefix = $convert->options['table_prefix'];
     // initiate database connection to old db if old and new db differ
     global $src_db, $same_db;
     $src_db = $same_db = null;
     if ($convert->src_dbms != $dbms || $convert->src_dbhost != $dbhost || $convert->src_dbport != $dbport || $convert->src_dbname != $dbname || $convert->src_dbuser != $dbuser) {
         $dbms = $convert->src_dbms;
         $src_db = new $dbms();
         $src_db->sql_connect($convert->src_dbhost, $convert->src_dbuser, htmlspecialchars_decode($convert->src_dbpasswd), $convert->src_dbname, $convert->src_dbport, false, true);
         $same_db = false;
     } else {
         $src_db = $db;
         $same_db = true;
     }
     $convert->mysql_convert = false;
     switch ($src_db->sql_layer) {
         case 'sqlite':
         case 'sqlite3':
             $convert->src_truncate_statement = 'DELETE FROM ';
             break;
             // Thanks MySQL, for silently converting...
         // Thanks MySQL, for silently converting...
         case 'mysql':
         case 'mysql4':
             if (version_compare($src_db->sql_server_info(true, false), '4.1.3', '>=')) {
                 $convert->mysql_convert = true;
             }
             $convert->src_truncate_statement = 'TRUNCATE TABLE ';
             break;
         case 'mysqli':
             $convert->mysql_convert = true;
             $convert->src_truncate_statement = 'TRUNCATE TABLE ';
             break;
         default:
             $convert->src_truncate_statement = 'TRUNCATE TABLE ';
             break;
     }
     if ($convert->mysql_convert && !$same_db) {
         $src_db->sql_query("SET NAMES 'binary'");
     }
     switch ($db->get_sql_layer()) {
         case 'sqlite':
         case 'sqlite3':
             $convert->truncate_statement = 'DELETE FROM ';
             break;
         default:
             $convert->truncate_statement = 'TRUNCATE TABLE ';
             break;
     }
     $get_info = false;
     // check security implications of direct inclusion
     if (!file_exists('./convertors/convert_' . $convert->convertor_tag . '.' . $phpEx)) {
         $this->p_master->error($user->lang['CONVERT_NOT_EXIST'], __LINE__, __FILE__);
     }
     if (file_exists('./convertors/functions_' . $convert->convertor_tag . '.' . $phpEx)) {
         include './convertors/functions_' . $convert->convertor_tag . '.' . $phpEx;
     }
     $get_info = true;
     include './convertors/convert_' . $convert->convertor_tag . '.' . $phpEx;
     // Map some variables...
     $convert->convertor_data = $convertor_data;
     $convert->tables = $tables;
     $convert->config_schema = $config_schema;
     // Now include the real data
     $get_info = false;
     include './convertors/convert_' . $convert->convertor_tag . '.' . $phpEx;
     $convert->convertor_data = $convertor_data;
     $convert->tables = $tables;
     $convert->config_schema = $config_schema;
     $convert->convertor = $convertor;
     // The test_file is a file that should be present in the location of the old board.
     if (!file_exists($convert->options['forum_path'] . '/' . $test_file)) {
         $this->p_master->error(sprintf($user->lang['COULD_NOT_FIND_PATH'], $convert->options['forum_path']), __LINE__, __FILE__);
     }
     $search_type = $config['search_type'];
     // For conversions we are a bit less strict and set to a search backend we know exist...
     if (!class_exists($search_type)) {
         $search_type = '\\phpbb\\search\\fulltext_native';
         $config->set('search_type', $search_type);
     }
     if (!class_exists($search_type)) {
         trigger_error('NO_SUCH_SEARCH_MODULE');
     }
     $error = false;
     $convert->fulltext_search = new $search_type($error, $phpbb_root_path, $phpEx, $auth, $config, $db, $user, $phpbb_dispatcher);
     if ($error) {
         trigger_error($error);
     }
     include $phpbb_root_path . 'includes/message_parser.' . $phpEx;
     $message_parser = new parse_message();
     $jump = $request->variable('jump', 0);
     $final_jump = $request->variable('final_jump', 0);
     $sync_batch = $request->variable('sync_batch', -1);
     $last_statement = $request->variable('last', 0);
     // We are running sync...
     if ($sync_batch >= 0) {
         $this->sync_forums($sync_batch);
         return;
     }
     if ($jump) {
         $this->jump($jump, $last_statement);
         return;
     }
     if ($final_jump) {
         $this->final_jump($final_jump);
         return;
     }
     $current_table = $request->variable('current_table', 0);
     $old_current_table = min(-1, $current_table - 1);
     $skip_rows = $request->variable('skip_rows', 0);
     if (!$current_table && !$skip_rows) {
         if (!$request->variable('confirm', false)) {
             // If avatars / ranks / smilies folders are specified make sure they are writable
             $bad_folders = array();
             $local_paths = array('avatar_path' => path($config['avatar_path']), 'avatar_gallery_path' => path($config['avatar_gallery_path']), 'icons_path' => path($config['icons_path']), 'ranks_path' => path($config['ranks_path']), 'smilies_path' => path($config['smilies_path']));
             foreach ($local_paths as $folder => $local_path) {
                 if (isset($convert->convertor[$folder])) {
                     if (empty($convert->convertor['test_file'])) {
                         // test_file is mandantory at the moment so this should never be reached, but just in case...
                         $this->p_master->error($user->lang['DEV_NO_TEST_FILE'], __LINE__, __FILE__);
                     }
                     if (!$local_path || !$this->filesystem->is_writable($phpbb_root_path . $local_path)) {
                         if (!$local_path) {
                             $bad_folders[] = sprintf($user->lang['CONFIG_PHPBB_EMPTY'], $folder);
                         } else {
                             $bad_folders[] = $local_path;
                         }
                     }
                 }
             }
             if (sizeof($bad_folders)) {
                 $msg = sizeof($bad_folders) == 1 ? $user->lang['MAKE_FOLDER_WRITABLE'] : $user->lang['MAKE_FOLDERS_WRITABLE'];
                 sort($bad_folders);
                 $this->p_master->error(sprintf($msg, implode('<br />', $bad_folders)), __LINE__, __FILE__, true);
                 $template->assign_vars(array('L_SUBMIT' => $user->lang['INSTALL_TEST'], 'U_ACTION' => $this->p_master->module_url . "?mode={$this->mode}&amp;sub=in_progress&amp;tag={$convert->convertor_tag}&amp;language={$language}"));
                 return;
             }
             // Grab all the tables used in convertor
             $missing_tables = $tables_list = $aliases = array();
             foreach ($convert->convertor['schema'] as $schema) {
                 // Skip those not used (because of addons/plugins not detected)
                 if (!$schema['target']) {
                     continue;
                 }
                 foreach ($schema as $key => $val) {
                     // we're dealing with an array like:
                     // array('forum_status',			'forums.forum_status',				'is_item_locked')
                     if (is_int($key) && !empty($val[1])) {
                         $temp_data = $val[1];
                         if (!is_array($temp_data)) {
                             $temp_data = array($temp_data);
                         }
                         foreach ($temp_data as $val) {
                             if (preg_match('/([a-z0-9_]+)\\.([a-z0-9_]+)\\)* ?A?S? ?([a-z0-9_]*?)\\.?([a-z0-9_]*)$/i', $val, $m)) {
                                 $table = $convert->src_table_prefix . $m[1];
                                 $tables_list[$table] = $table;
                                 if (!empty($m[3])) {
                                     $aliases[] = $convert->src_table_prefix . $m[3];
                                 }
                             }
                         }
                     } else {
                         if ($key == 'left_join') {
                             // Convert the value if it wasn't an array already.
                             if (!is_array($val)) {
                                 $val = array($val);
                             }
                             for ($j = 0; $j < sizeof($val); ++$j) {
                                 if (preg_match('/LEFT JOIN ([a-z0-9_]+) AS ([a-z0-9_]+)/i', $val[$j], $m)) {
                                     $table = $convert->src_table_prefix . $m[1];
                                     $tables_list[$table] = $table;
                                     if (!empty($m[2])) {
                                         $aliases[] = $convert->src_table_prefix . $m[2];
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             // Remove aliased tables from $tables_list
             foreach ($aliases as $alias) {
                 unset($tables_list[$alias]);
             }
             // Check if the tables that we need exist
             $src_db->sql_return_on_error(true);
             foreach ($tables_list as $table => $null) {
                 $sql = 'SELECT 1 FROM ' . $table;
                 $_result = $src_db->sql_query_limit($sql, 1);
                 if (!$_result) {
                     $missing_tables[] = $table;
                 }
                 $src_db->sql_freeresult($_result);
             }
             $src_db->sql_return_on_error(false);
             // Throw an error if some tables are missing
             // We used to do some guessing here, but since we have a suggestion of possible values earlier, I don't see it adding anything here to do it again
             if (sizeof($missing_tables) == sizeof($tables_list)) {
                 $this->p_master->error($user->lang['NO_TABLES_FOUND'] . ' ' . $user->lang['CHECK_TABLE_PREFIX'], __LINE__, __FILE__);
             } else {
                 if (sizeof($missing_tables)) {
                     $this->p_master->error(sprintf($user->lang['TABLES_MISSING'], implode($user->lang['COMMA_SEPARATOR'], $missing_tables)) . '<br /><br />' . $user->lang['CHECK_TABLE_PREFIX'], __LINE__, __FILE__);
                 }
             }
             $url = $this->save_convert_progress('&amp;confirm=1');
             $msg = $user->lang['PRE_CONVERT_COMPLETE'];
             if ($convert->convertor_data['author_notes']) {
                 $msg .= '</p><p>' . sprintf($user->lang['AUTHOR_NOTES'], $convert->convertor_data['author_notes']);
             }
             $template->assign_vars(array('L_SUBMIT' => $user->lang['CONTINUE_CONVERT'], 'L_MESSAGE' => $msg, 'U_ACTION' => $url));
             return;
         }
         // if (!$request->variable('confirm', false)))
         $template->assign_block_vars('checks', array('S_LEGEND' => true, 'LEGEND' => $user->lang['STARTING_CONVERT']));
         // Convert the config table and load the settings of the old board
         if (!empty($convert->config_schema)) {
             restore_config($convert->config_schema);
             // Override a couple of config variables for the duration
             $config['max_quote_depth'] = 0;
             // @todo Need to confirm that max post length in source is <= max post length in destination or there may be interesting formatting issues
             $config['max_post_chars'] = $config['min_post_chars'] = 0;
         }
         $template->assign_block_vars('checks', array('TITLE' => $user->lang['CONFIG_CONVERT'], 'RESULT' => $user->lang['DONE']));
         // Now process queries and execute functions that have to be executed prior to the conversion
         if (!empty($convert->convertor['execute_first'])) {
             // @codingStandardsIgnoreStart
             eval($convert->convertor['execute_first']);
             // @codingStandardsIgnoreEnd
         }
         if (!empty($convert->convertor['query_first'])) {
             if (!is_array($convert->convertor['query_first'])) {
                 $convert->convertor['query_first'] = array('target', array($convert->convertor['query_first']));
             } else {
                 if (!is_array($convert->convertor['query_first'][0])) {
                     $convert->convertor['query_first'] = array(array($convert->convertor['query_first'][0], $convert->convertor['query_first'][1]));
                 }
             }
             foreach ($convert->convertor['query_first'] as $query_first) {
                 if ($query_first[0] == 'src') {
                     if ($convert->mysql_convert && $same_db) {
                         $src_db->sql_query("SET NAMES 'binary'");
                     }
                     $src_db->sql_query($query_first[1]);
                     if ($convert->mysql_convert && $same_db) {
                         $src_db->sql_query("SET NAMES 'utf8'");
                     }
                 } else {
                     $db->sql_query($query_first[1]);
                 }
             }
         }
         $template->assign_block_vars('checks', array('TITLE' => $user->lang['PREPROCESS_STEP'], 'RESULT' => $user->lang['DONE']));
     }
     // if (!$current_table && !$skip_rows)
     $template->assign_block_vars('checks', array('S_LEGEND' => true, 'LEGEND' => $user->lang['FILLING_TABLES']));
     // This loop takes one target table and processes it
     while ($current_table < sizeof($convert->convertor['schema'])) {
         $schema = $convert->convertor['schema'][$current_table];
         // The target table isn't set, this can be because a module (for example the attachement mod) is taking care of this.
         if (empty($schema['target'])) {
             $current_table++;
             continue;
         }
         $template->assign_block_vars('checks', array('TITLE' => sprintf($user->lang['FILLING_TABLE'], $schema['target'])));
         // This is only the case when we first start working on the tables.
         if (!$skip_rows) {
             // process execute_first and query_first for this table...
             if (!empty($schema['execute_first'])) {
                 // @codingStandardsIgnoreStart
                 eval($schema['execute_first']);
                 // @codingStandardsIgnoreEnd
             }
             if (!empty($schema['query_first'])) {
                 if (!is_array($schema['query_first'])) {
                     $schema['query_first'] = array('target', array($schema['query_first']));
                 } else {
                     if (!is_array($schema['query_first'][0])) {
                         $schema['query_first'] = array(array($schema['query_first'][0], $schema['query_first'][1]));
                     }
                 }
                 foreach ($schema['query_first'] as $query_first) {
                     if ($query_first[0] == 'src') {
                         if ($convert->mysql_convert && $same_db) {
                             $src_db->sql_query("SET NAMES 'binary'");
                         }
                         $src_db->sql_query($query_first[1]);
                         if ($convert->mysql_convert && $same_db) {
                             $src_db->sql_query("SET NAMES 'utf8'");
                         }
                     } else {
                         $db->sql_query($query_first[1]);
                     }
                 }
             }
             if (!empty($schema['autoincrement'])) {
                 switch ($db->get_sql_layer()) {
                     case 'postgres':
                         $db->sql_query("SELECT SETVAL('" . $schema['target'] . "_seq',(select case when max(" . $schema['autoincrement'] . ")>0 then max(" . $schema['autoincrement'] . ")+1 else 1 end from " . $schema['target'] . '));');
                         break;
                     case 'oracle':
                         $result = $db->sql_query('SELECT MAX(' . $schema['autoincrement'] . ') as max_id FROM ' . $schema['target']);
                         $row = $db->sql_fetchrow($result);
                         $db->sql_freeresult($result);
                         $largest_id = (int) $row['max_id'];
                         if ($largest_id) {
                             $db->sql_query('DROP SEQUENCE ' . $schema['target'] . '_seq');
                             $db->sql_query('CREATE SEQUENCE ' . $schema['target'] . '_seq START WITH ' . ($largest_id + 1));
                         }
                         break;
                 }
             }
         }
         // Process execute_always for this table
         // This is for code which needs to be executed on every pass of this table if
         // it gets split because of time restrictions
         if (!empty($schema['execute_always'])) {
             // @codingStandardsIgnoreStart
             eval($schema['execute_always']);
             // @codingStandardsIgnoreEnd
         }
         //
         // Set up some variables
         //
         // $waiting_rows	holds rows for multirows insertion (MySQL only)
         // $src_tables		holds unique tables with aliases to select from
         // $src_fields		will quickly refer source fields (or aliases) corresponding to the current index
         // $select_fields	holds the names of the fields to retrieve
         //
         $sql_data = array('source_fields' => array(), 'target_fields' => array(), 'source_tables' => array(), 'select_fields' => array());
         // This statement is building the keys for later insertion.
         $insert_query = $this->build_insert_query($schema, $sql_data, $current_table);
         // If no source table is affected, we skip the table
         if (empty($sql_data['source_tables'])) {
             $skip_rows = 0;
             $current_table++;
             continue;
         }
         $distinct = !empty($schema['distinct']) ? 'DISTINCT ' : '';
         $sql = 'SELECT ' . $distinct . implode(', ', $sql_data['select_fields']) . " \nFROM " . implode(', ', $sql_data['source_tables']);
         // Where
         $sql .= !empty($schema['where']) ? "\nWHERE (" . $schema['where'] . ')' : '';
         // Group By
         if (!empty($schema['group_by'])) {
             $schema['group_by'] = array($schema['group_by']);
             foreach ($sql_data['select_fields'] as $select) {
                 $alias = strpos(strtolower($select), ' as ');
                 $select = $alias ? substr($select, 0, $alias) : $select;
                 if (!in_array($select, $schema['group_by'])) {
                     $schema['group_by'][] = $select;
                 }
             }
         }
         $sql .= !empty($schema['group_by']) ? "\nGROUP BY " . implode(', ', $schema['group_by']) : '';
         // Having
         $sql .= !empty($schema['having']) ? "\nHAVING " . $schema['having'] : '';
         // Order By
         if (empty($schema['order_by']) && !empty($schema['primary'])) {
             $schema['order_by'] = $schema['primary'];
         }
         $sql .= !empty($schema['order_by']) ? "\nORDER BY " . $schema['order_by'] : '';
         // Counting basically holds the amount of rows processed.
         $counting = -1;
         $batch_time = 0;
         while ($counting === -1 || $counting >= $convert->batch_size && still_on_time()) {
             $old_current_table = $current_table;
             $rows = '';
             $waiting_rows = array();
             if (!empty($batch_time)) {
                 $mtime = explode(' ', microtime());
                 $mtime = $mtime[0] + $mtime[1];
                 $rows = ceil($counting / ($mtime - $batch_time)) . " rows/s ({$counting} rows) | ";
             }
             $template->assign_block_vars('checks', array('TITLE' => "skip_rows = {$skip_rows}", 'RESULT' => $rows . (defined('DEBUG') && function_exists('memory_get_usage') ? ceil(memory_get_usage() / 1024) . ' ' . $user->lang['KIB'] : '')));
             $mtime = explode(' ', microtime());
             $batch_time = $mtime[0] + $mtime[1];
             if ($convert->mysql_convert && $same_db) {
                 $src_db->sql_query("SET NAMES 'binary'");
             }
             // Take skip rows into account and only fetch batch_size amount of rows
             $___result = $src_db->sql_query_limit($sql, $convert->batch_size, $skip_rows);
             if ($convert->mysql_convert && $same_db) {
                 $src_db->sql_query("SET NAMES 'utf8'");
             }
             // This loop processes each row
             $counting = 0;
             $convert->row = $convert_row = array();
             if (!empty($schema['autoincrement'])) {
                 switch ($db->get_sql_layer()) {
                     case 'mssql':
                     case 'mssql_odbc':
                     case 'mssqlnative':
                         $db->sql_query('SET IDENTITY_INSERT ' . $schema['target'] . ' ON');
                         break;
                 }
             }
             // Now handle the rows until time is over or no more rows to process...
             while ($counting === 0 || still_on_time()) {
                 $convert_row = $src_db->sql_fetchrow($___result);
                 if (!$convert_row) {
                     // move to the next batch or table
                     break;
                 }
                 // With this we are able to always save the last state
                 $convert->row = $convert_row;
                 // Increment the counting variable, it stores the number of rows we have processed
                 $counting++;
                 $insert_values = array();
                 $sql_flag = $this->process_row($schema, $sql_data, $insert_values);
                 if ($sql_flag === true) {
                     switch ($db->get_sql_layer()) {
                         // If MySQL, we'll wait to have num_wait_rows rows to submit at once
                         case 'mysql':
                         case 'mysql4':
                         case 'mysqli':
                             $waiting_rows[] = '(' . implode(', ', $insert_values) . ')';
                             if (sizeof($waiting_rows) >= $convert->num_wait_rows) {
                                 $errored = false;
                                 $db->sql_return_on_error(true);
                                 if (!$db->sql_query($insert_query . implode(', ', $waiting_rows))) {
                                     $errored = true;
                                 }
                                 $db->sql_return_on_error(false);
                                 if ($errored) {
                                     $db->sql_return_on_error(true);
                                     // Because it errored out we will try to insert the rows one by one... most of the time this
                                     // is caused by duplicate entries - but we also do not want to miss one...
                                     foreach ($waiting_rows as $waiting_sql) {
                                         if (!$db->sql_query($insert_query . $waiting_sql)) {
                                             $this->p_master->db_error($user->lang['DB_ERR_INSERT'], htmlspecialchars($insert_query . $waiting_sql) . '<br /><br />' . htmlspecialchars(print_r($db->_sql_error(), true)), __LINE__, __FILE__, true);
                                         }
                                     }
                                     $db->sql_return_on_error(false);
                                 }
                                 $waiting_rows = array();
                             }
                             break;
                         default:
                             $insert_sql = $insert_query . '(' . implode(', ', $insert_values) . ')';
                             $db->sql_return_on_error(true);
                             if (!$db->sql_query($insert_sql)) {
                                 $this->p_master->db_error($user->lang['DB_ERR_INSERT'], htmlspecialchars($insert_sql) . '<br /><br />' . htmlspecialchars(print_r($db->_sql_error(), true)), __LINE__, __FILE__, true);
                             }
                             $db->sql_return_on_error(false);
                             $waiting_rows = array();
                             break;
                     }
                 }
                 $skip_rows++;
             }
             $src_db->sql_freeresult($___result);
             // We might still have some rows waiting
             if (sizeof($waiting_rows)) {
                 $errored = false;
                 $db->sql_return_on_error(true);
                 if (!$db->sql_query($insert_query . implode(', ', $waiting_rows))) {
                     $errored = true;
                 }
                 $db->sql_return_on_error(false);
                 if ($errored) {
                     $db->sql_return_on_error(true);
                     // Because it errored out we will try to insert the rows one by one... most of the time this
                     // is caused by duplicate entries - but we also do not want to miss one...
                     foreach ($waiting_rows as $waiting_sql) {
                         $db->sql_query($insert_query . $waiting_sql);
                         $this->p_master->db_error($user->lang['DB_ERR_INSERT'], htmlspecialchars($insert_query . $waiting_sql) . '<br /><br />' . htmlspecialchars(print_r($db->_sql_error(), true)), __LINE__, __FILE__, true);
                     }
                     $db->sql_return_on_error(false);
                 }
                 $waiting_rows = array();
             }
             if (!empty($schema['autoincrement'])) {
                 switch ($db->get_sql_layer()) {
                     case 'mssql':
                     case 'mssql_odbc':
                     case 'mssqlnative':
                         $db->sql_query('SET IDENTITY_INSERT ' . $schema['target'] . ' OFF');
                         break;
                     case 'postgres':
                         $db->sql_query("SELECT SETVAL('" . $schema['target'] . "_seq',(select case when max(" . $schema['autoincrement'] . ")>0 then max(" . $schema['autoincrement'] . ")+1 else 1 end from " . $schema['target'] . '));');
                         break;
                     case 'oracle':
                         $result = $db->sql_query('SELECT MAX(' . $schema['autoincrement'] . ') as max_id FROM ' . $schema['target']);
                         $row = $db->sql_fetchrow($result);
                         $db->sql_freeresult($result);
                         $largest_id = (int) $row['max_id'];
                         if ($largest_id) {
                             $db->sql_query('DROP SEQUENCE ' . $schema['target'] . '_seq');
                             $db->sql_query('CREATE SEQUENCE ' . $schema['target'] . '_seq START WITH ' . ($largest_id + 1));
                         }
                         break;
                 }
             }
         }
         // When we reach this point, either the current table has been processed or we're running out of time.
         if (still_on_time() && $counting < $convert->batch_size) {
             $skip_rows = 0;
             $current_table++;
         } else {
             /*
             				if (still_on_time() && $counting < $convert->batch_size)
             				{
             					$skip_rows = 0;
             					$current_table++;
             				}*/
             // Looks like we ran out of time.
             $url = $this->save_convert_progress('&amp;current_table=' . $current_table . '&amp;skip_rows=' . $skip_rows);
             $current_table++;
             //				$percentage = ($skip_rows == 0) ? 0 : floor(100 / ($total_rows / $skip_rows));
             $msg = sprintf($user->lang['STEP_PERCENT_COMPLETED'], $current_table, sizeof($convert->convertor['schema']));
             $template->assign_vars(array('L_MESSAGE' => $msg, 'L_SUBMIT' => $user->lang['CONTINUE_CONVERT'], 'U_ACTION' => $url));
             $this->meta_refresh($url);
             return;
         }
     }
     // Process execute_last then we'll be done
     $url = $this->save_convert_progress('&amp;jump=1');
     $template->assign_vars(array('L_SUBMIT' => $user->lang['FINAL_STEP'], 'U_ACTION' => $url));
     $this->meta_refresh($url);
     return;
 }
Пример #2
0
 function main($mode, $sub)
 {
     global $template, $phpEx, $phpbb_root_path, $user, $db, $config, $cache, $auth, $language;
     global $request, $phpbb_admin_path, $phpbb_adm_relative_path, $phpbb_container, $phpbb_config_php_file;
     // We must enable super globals, otherwise creating a new instance of the request class,
     // using the new container with a dbal connection will fail with the following PHP Notice:
     // Object of class phpbb_request_deactivated_super_global could not be converted to int
     $request->enable_super_globals();
     // Create a normal container now
     $phpbb_container_builder = new \phpbb\di\container_builder($phpbb_root_path, $phpEx);
     $phpbb_container = $phpbb_container_builder->with_config($phpbb_config_php_file)->without_cache()->without_extensions();
     if (file_exists($phpbb_root_path . 'install/update/new/config')) {
         $phpbb_container_builder->with_config_path($phpbb_root_path . 'install/update/new/config');
     }
     $phpbb_container = $phpbb_container_builder->get_container();
     // Writes into global $cache
     /* @var $cache \phpbb\cache\service */
     $cache = $phpbb_container->get('cache');
     $this->filesystem = $phpbb_container->get('filesystem');
     $this->tpl_name = 'install_update';
     $this->page_title = 'UPDATE_INSTALLATION';
     $this->old_location = $phpbb_root_path . 'install/update/old/';
     $this->new_location = $phpbb_root_path . 'install/update/new/';
     // Init DB
     extract($phpbb_config_php_file->get_all());
     require $phpbb_root_path . 'includes/constants.' . $phpEx;
     // Special options for conflicts/modified files
     define('MERGE_NO_MERGE_NEW', 1);
     define('MERGE_NO_MERGE_MOD', 2);
     define('MERGE_NEW_FILE', 3);
     define('MERGE_MOD_FILE', 4);
     $dbms = $phpbb_config_php_file->convert_30_dbms_to_31($dbms);
     $db = new $dbms();
     // Connect to DB
     $db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, false);
     // We do not need this any longer, unset for safety purposes
     unset($dbpasswd);
     // We need to fill the config to let internal functions correctly work
     $config = new \phpbb\config\db($db, new \phpbb\cache\driver\dummy(), CONFIG_TABLE);
     // Force template recompile
     $config['load_tplcompile'] = 1;
     // First of all, init the user session
     $user->session_begin();
     $auth->acl($user->data);
     // Overwrite user's language with the selected one.
     // Config needs to be changed to ensure that guests also get the selected language.
     $config_default_lang = $config['default_lang'];
     $config['default_lang'] = $language;
     $user->data['user_lang'] = $language;
     $user->add_lang(array('common', 'acp/common', 'acp/board', 'install', 'posting'));
     // Reset the default_lang
     $config['default_lang'] = $config_default_lang;
     unset($config_default_lang);
     // If we are within the intro page we need to make sure we get up-to-date version info
     if ($sub == 'intro') {
         $cache->destroy('_version_info');
     }
     // Set custom template again. ;)
     $paths = array($phpbb_root_path . 'install/update/new/adm/style', $phpbb_admin_path . 'style');
     $paths = array_filter($paths, 'is_dir');
     $template->set_custom_style(array(array('name' => 'adm', 'ext_path' => 'adm/style/')), $paths);
     $template->assign_vars(array('S_USER_LANG' => $user->lang['USER_LANG'], 'S_CONTENT_DIRECTION' => $user->lang['DIRECTION'], 'S_CONTENT_ENCODING' => 'UTF-8', 'S_CONTENT_FLOW_BEGIN' => $user->lang['DIRECTION'] == 'ltr' ? 'left' : 'right', 'S_CONTENT_FLOW_END' => $user->lang['DIRECTION'] == 'ltr' ? 'right' : 'left'));
     // Get current and latest version
     /* @var $version_helper \phpbb\version_helper */
     $version_helper = $phpbb_container->get('version_helper');
     try {
         $this->latest_version = $version_helper->get_latest_on_current_branch(true);
     } catch (\RuntimeException $e) {
         $this->latest_version = false;
         $update_info = array();
         include $phpbb_root_path . 'install/update/index.' . $phpEx;
         $info = empty($update_info) || !is_array($update_info) ? false : $update_info;
         if ($info !== false) {
             $this->latest_version = !empty($info['version']['to']) ? trim($info['version']['to']) : false;
         }
     }
     // For the current version we trick a bit. ;)
     $this->current_version = !empty($config['version_update_from']) ? $config['version_update_from'] : $config['version'];
     $up_to_date = version_compare(str_replace('rc', 'RC', strtolower($this->current_version)), str_replace('rc', 'RC', strtolower($this->latest_version)), '<') ? false : true;
     // Check for a valid update directory, else point the user to the phpbb.com website
     if (!file_exists($phpbb_root_path . 'install/update') || !file_exists($phpbb_root_path . 'install/update/index.' . $phpEx) || !file_exists($this->old_location) || !file_exists($this->new_location)) {
         $template->assign_vars(array('S_ERROR' => true, 'ERROR_MSG' => $up_to_date ? $user->lang['NO_UPDATE_FILES_UP_TO_DATE'] : sprintf($user->lang['NO_UPDATE_FILES_OUTDATED'], $config['version'], $this->current_version, $this->latest_version)));
         return;
     }
     $this->update_info = $this->get_file('update_info');
     // Make sure the update directory holds the correct information
     // Since admins are able to run the update/checks more than once we only check if the current version is lower or equal than the version to which we update to.
     if (version_compare(str_replace('rc', 'RC', strtolower($this->current_version)), str_replace('rc', 'RC', strtolower($this->update_info['version']['to'])), '>')) {
         $template->assign_vars(array('S_ERROR' => true, 'ERROR_MSG' => sprintf($user->lang['INCOMPATIBLE_UPDATE_FILES'], $config['version'], $this->update_info['version']['from'], $this->update_info['version']['to'])));
         return;
     }
     // Check if the update files are actually meant to update from the current version
     if ($this->current_version != $this->update_info['version']['from']) {
         $template->assign_vars(array('S_ERROR' => true, 'ERROR_MSG' => sprintf($user->lang['INCOMPATIBLE_UPDATE_FILES'], $this->current_version, $this->update_info['version']['from'], $this->update_info['version']['to'])));
     }
     // Check if the update files stored are for the latest version...
     if (version_compare(strtolower($this->latest_version), strtolower($this->update_info['version']['to']), '>')) {
         $template->assign_vars(array('S_WARNING' => true, 'WARNING_MSG' => sprintf($user->lang['OLD_UPDATE_FILES'], $this->update_info['version']['from'], $this->update_info['version']['to'], $this->latest_version)));
     }
     // We store the "update to" version, because it is not always the latest. ;)
     $this->update_to_version = $this->update_info['version']['to'];
     // Fill DB version
     if (empty($config['dbms_version'])) {
         $config->set('dbms_version', $db->sql_server_info(true));
     }
     if ($this->test_update === false) {
         // What about the language file? Got it updated?
         if (in_array('language/' . $language . '/install.' . $phpEx, $this->update_info['files'])) {
             $lang = array();
             include $this->new_location . 'language/' . $language . '/install.' . $phpEx;
             // this is the user's language.. just merge it
             $user->lang = array_merge($user->lang, $lang);
         }
         if ($language != 'en' && in_array('language/en/install.' . $phpEx, $this->update_info['files'])) {
             $lang = array();
             include $this->new_location . 'language/en/install.' . $phpEx;
             // only add new keys to user's language in english
             $new_keys = array_diff(array_keys($lang), array_keys($user->lang));
             foreach ($new_keys as $i => $new_key) {
                 $user->lang[$new_key] = $lang[$new_key];
             }
         }
     }
     // Include renderer and engine
     $this->include_file('includes/diff/diff.' . $phpEx);
     $this->include_file('includes/diff/engine.' . $phpEx);
     $this->include_file('includes/diff/renderer.' . $phpEx);
     // Make sure we stay at the file check if checking the files again
     if ($request->variable('check_again', false, false, \phpbb\request\request_interface::POST)) {
         $sub = $this->p_master->sub = 'file_check';
     }
     switch ($sub) {
         case 'intro':
             $this->page_title = 'UPDATE_INSTALLATION';
             $template->assign_vars(array('S_INTRO' => true, 'U_ACTION' => append_sid($this->p_master->module_url, "language={$language}&amp;mode={$mode}&amp;sub=version_check")));
             // Make sure the update list is destroyed.
             $cache->destroy('_update_list');
             $cache->destroy('_diff_files');
             $cache->destroy('_expected_files');
             break;
         case 'version_check':
             $this->page_title = 'STAGE_VERSION_CHECK';
             $template->assign_vars(array('S_VERSION_CHECK' => true, 'U_ACTION' => append_sid($this->p_master->module_url, "language={$language}&amp;mode={$mode}&amp;sub=file_check"), 'S_UP_TO_DATE' => $up_to_date, 'LATEST_VERSION' => $this->latest_version, 'CURRENT_VERSION' => $this->current_version));
             // Print out version the update package updates to
             if ($this->latest_version != $this->update_info['version']['to']) {
                 $template->assign_var('PACKAGE_VERSION', $this->update_info['version']['to']);
             }
             // Since some people try to update to RC releases, but phpBB.com tells them the last version is the version they currently run
             // we are faced with the updater thinking the database schema is up-to-date; which it is, but should be updated none-the-less
             // We now try to cope with this by triggering the update process
             if (version_compare(str_replace('rc', 'RC', strtolower($this->current_version)), str_replace('rc', 'RC', strtolower($this->update_info['version']['to'])), '<')) {
                 $template->assign_vars(array('S_UP_TO_DATE' => false));
             }
             break;
         case 'update_db':
             // Redirect the user to the database update script with some explanations...
             $template->assign_vars(array('S_DB_UPDATE' => true, 'S_DB_UPDATE_FINISHED' => $config['version'] == $this->update_info['version']['to'] ? true : false, 'U_DB_UPDATE' => append_sid($phpbb_root_path . 'install/database_update.' . $phpEx, 'type=1&amp;language=' . $user->data['user_lang']), 'U_DB_UPDATE_ACTION' => append_sid($this->p_master->module_url, "language={$language}&amp;mode={$mode}&amp;sub=update_db"), 'U_ACTION' => append_sid($this->p_master->module_url, "language={$language}&amp;mode={$mode}&amp;sub=file_check"), 'L_EVERYTHING_UP_TO_DATE' => $user->lang('EVERYTHING_UP_TO_DATE', append_sid("{$phpbb_root_path}ucp.{$phpEx}", 'mode=login'), append_sid("{$phpbb_root_path}ucp.{$phpEx}", 'mode=login&amp;redirect=' . $phpbb_adm_relative_path . 'index.php%3Fi=send_statistics%26mode=send_statistics'))));
             // Do not display incompatible package note after successful update
             if ($config['version'] == $this->update_info['version']['to']) {
                 $template->assign_var('S_ERROR', false);
             }
             break;
         case 'file_check':
             // retrieve info on what changes should have already been made to the files.
             $expected_files = $cache->get('_expected_files');
             if (!$expected_files) {
                 $expected_files = array();
             }
             // Now make sure the previous file collection is no longer valid...
             $cache->destroy('_diff_files');
             $this->page_title = 'STAGE_FILE_CHECK';
             // Now make sure our update list is correct if the admin refreshes
             $action = $request->variable('action', '');
             // We are directly within an update. To make sure our update list is correct we check its status.
             $update_list = $request->variable('check_again', false, false, \phpbb\request\request_interface::POST) ? false : $cache->get('_update_list');
             $modified = $update_list !== false ? @filemtime($cache->get_driver()->cache_dir . 'data_update_list.' . $phpEx) : 0;
             // Make sure the list is up-to-date
             if ($update_list !== false) {
                 $get_new_list = false;
                 foreach ($this->update_info['files'] as $file) {
                     if (file_exists($phpbb_root_path . $file) && filemtime($phpbb_root_path . $file) > $modified) {
                         $get_new_list = true;
                         break;
                     }
                 }
             } else {
                 $get_new_list = true;
             }
             if (!$get_new_list && $update_list['status'] != -1) {
                 $get_new_list = true;
             }
             if ($get_new_list) {
                 $this->get_update_structure($update_list, $expected_files);
                 $cache->put('_update_list', $update_list);
                 // Refresh the page if we are still not finished...
                 if ($update_list['status'] != -1) {
                     $refresh_url = append_sid($this->p_master->module_url, "language={$language}&amp;mode={$mode}&amp;sub=file_check");
                     meta_refresh(2, $refresh_url);
                     $template->assign_vars(array('S_IN_PROGRESS' => true, 'S_COLLECTED' => (int) $update_list['status'], 'S_TO_COLLECT' => sizeof($this->update_info['files']), 'L_IN_PROGRESS' => $user->lang['COLLECTING_FILE_DIFFS'], 'L_IN_PROGRESS_EXPLAIN' => sprintf($user->lang['NUMBER_OF_FILES_COLLECTED'], (int) $update_list['status'], sizeof($this->update_info['files']) + sizeof($this->update_info['deleted']))));
                     return;
                 }
             }
             if ($action == 'diff') {
                 $this->show_diff($update_list);
                 return;
             }
             if (sizeof($update_list['no_update'])) {
                 $template->assign_vars(array('S_NO_UPDATE_FILES' => true, 'NO_UPDATE_FILES' => implode(', ', array_map('htmlspecialchars', $update_list['no_update']))));
             }
             $new_expected_files = array();
             // Now assign the list to the template
             foreach ($update_list as $status => $filelist) {
                 if ($status == 'no_update' || !sizeof($filelist) || $status == 'status' || $status == 'status_deleted') {
                     continue;
                 }
                 /*					$template->assign_block_vars('files', array(
                 						'S_STATUS'		=> true,
                 						'STATUS'		=> $status,
                 						'L_STATUS'		=> $user->lang['STATUS_' . strtoupper($status)],
                 						'TITLE'			=> $user->lang['FILES_' . strtoupper($status)],
                 						'EXPLAIN'		=> $user->lang['FILES_' . strtoupper($status) . '_EXPLAIN'],
                 						)
                 					);*/
                 foreach ($filelist as $file_struct) {
                     $s_binary = !empty($this->update_info['binary']) && in_array($file_struct['filename'], $this->update_info['binary']) ? true : false;
                     $filename = htmlspecialchars($file_struct['filename']);
                     if (strrpos($filename, '/') !== false) {
                         $dir_part = substr($filename, 0, strrpos($filename, '/') + 1);
                         $file_part = substr($filename, strrpos($filename, '/') + 1);
                     } else {
                         $dir_part = '';
                         $file_part = $filename;
                     }
                     $diff_url = append_sid($this->p_master->module_url, "language={$language}&amp;mode={$mode}&amp;sub=file_check&amp;action=diff&amp;status={$status}&amp;file=" . urlencode($file_struct['filename']));
                     if (isset($file_struct['as_expected']) && $file_struct['as_expected']) {
                         $new_expected_files[$file_struct['filename']] = $expected_files[$file_struct['filename']];
                     } else {
                         $template->assign_block_vars($status, array('STATUS' => $status, 'FILENAME' => $filename, 'DIR_PART' => $dir_part, 'FILE_PART' => $file_part, 'NUM_CONFLICTS' => isset($file_struct['conflicts']) ? $file_struct['conflicts'] : 0, 'S_CUSTOM' => $file_struct['custom'] ? true : false, 'S_BINARY' => $s_binary, 'CUSTOM_ORIGINAL' => $file_struct['custom'] ? $file_struct['original'] : '', 'U_SHOW_DIFF' => $diff_url, 'L_SHOW_DIFF' => $status != 'up_to_date' ? $user->lang['SHOW_DIFF_' . strtoupper($status)] : '', 'U_VIEW_MOD_FILE' => $diff_url . '&amp;op=' . MERGE_MOD_FILE, 'U_VIEW_NEW_FILE' => $diff_url . '&amp;op=' . MERGE_NEW_FILE, 'U_VIEW_NO_MERGE_MOD' => $diff_url . '&amp;op=' . MERGE_NO_MERGE_MOD, 'U_VIEW_NO_MERGE_NEW' => $diff_url . '&amp;op=' . MERGE_NO_MERGE_NEW));
                     }
                 }
             }
             $cache->put('_expected_files', $new_expected_files);
             $all_up_to_date = true;
             foreach ($update_list as $status => $filelist) {
                 if ($status != 'up_to_date' && $status != 'custom' && $status != 'status' && $status != 'status_deleted' && sizeof($filelist)) {
                     $all_up_to_date = false;
                     break;
                 }
             }
             $template->assign_vars(array('S_FILE_CHECK' => true, 'S_ALL_UP_TO_DATE' => $all_up_to_date, 'S_VERSION_UP_TO_DATE' => $up_to_date, 'S_UP_TO_DATE' => $up_to_date, 'U_ACTION' => append_sid($this->p_master->module_url, "language={$language}&amp;mode={$mode}&amp;sub=file_check"), 'U_UPDATE_ACTION' => append_sid($this->p_master->module_url, "language={$language}&amp;mode={$mode}&amp;sub=update_files"), 'U_DB_UPDATE_ACTION' => append_sid($this->p_master->module_url, "language={$language}&amp;mode={$mode}&amp;sub=update_db")));
             // Since some people try to update to RC releases, but phpBB.com tells them the last version is the version they currently run
             // we are faced with the updater thinking the database schema is up-to-date; which it is, but should be updated none-the-less
             // We now try to cope with this by triggering the update process
             if (version_compare(str_replace('rc', 'RC', strtolower($this->current_version)), str_replace('rc', 'RC', strtolower($this->update_info['version']['to'])), '<')) {
                 $template->assign_vars(array('S_UP_TO_DATE' => false));
             }
             if ($all_up_to_date) {
                 global $phpbb_container;
                 /* @var $phpbb_log \phpbb\log\log_interface */
                 $phpbb_log = $phpbb_container->get('log');
                 // Add database update to log
                 $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_UPDATE_PHPBB', time(), array($this->current_version, $this->update_to_version));
                 $db->sql_return_on_error(true);
                 $db->sql_query('DELETE FROM ' . CONFIG_TABLE . " WHERE config_name = 'version_update_from'");
                 $db->sql_return_on_error(false);
                 $cache->purge();
             }
             break;
         case 'update_files':
             $this->page_title = 'STAGE_UPDATE_FILES';
             $s_hidden_fields = '';
             $params = array();
             $conflicts = $request->variable('conflict', array('' => 0));
             $modified = $request->variable('modified', array('' => 0));
             foreach ($conflicts as $filename => $merge_option) {
                 $s_hidden_fields .= '<input type="hidden" name="conflict[' . htmlspecialchars($filename) . ']" value="' . $merge_option . '" />';
                 $params[] = 'conflict[' . urlencode($filename) . ']=' . urlencode($merge_option);
             }
             foreach ($modified as $filename => $merge_option) {
                 if (!$merge_option) {
                     continue;
                 }
                 $s_hidden_fields .= '<input type="hidden" name="modified[' . htmlspecialchars($filename) . ']" value="' . $merge_option . '" />';
                 $params[] = 'modified[' . urlencode($filename) . ']=' . urlencode($merge_option);
             }
             $no_update = $request->variable('no_update', array(0 => ''));
             foreach ($no_update as $index => $filename) {
                 $s_hidden_fields .= '<input type="hidden" name="no_update[]" value="' . htmlspecialchars($filename) . '" />';
                 $params[] = 'no_update[]=' . urlencode($filename);
             }
             // Before the user is choosing his preferred method, let's create the content list...
             $update_list = $cache->get('_update_list');
             if ($update_list === false) {
                 trigger_error($user->lang['NO_UPDATE_INFO'], E_USER_ERROR);
             }
             // Check if the conflicts data is valid
             if (sizeof($conflicts)) {
                 $conflict_filenames = array();
                 foreach ($update_list['conflict'] as $files) {
                     $conflict_filenames[] = $files['filename'];
                 }
                 $new_conflicts = array();
                 foreach ($conflicts as $filename => $diff_method) {
                     if (in_array($filename, $conflict_filenames)) {
                         $new_conflicts[$filename] = $diff_method;
                     }
                 }
                 $conflicts = $new_conflicts;
             }
             // Build list for modifications
             if (sizeof($modified)) {
                 $modified_filenames = array();
                 foreach ($update_list['modified'] as $files) {
                     $modified_filenames[] = $files['filename'];
                 }
                 $new_modified = array();
                 foreach ($modified as $filename => $diff_method) {
                     if (in_array($filename, $modified_filenames)) {
                         $new_modified[$filename] = $diff_method;
                     }
                 }
                 $modified = $new_modified;
             }
             // Check number of conflicting files, they need to be equal. For modified files the number can differ
             if (sizeof($update_list['conflict']) != sizeof($conflicts)) {
                 trigger_error($user->lang['MERGE_SELECT_ERROR'], E_USER_ERROR);
             }
             // Before we do anything, let us diff the files and store the raw file information "somewhere"
             $get_files = false;
             $file_list = $cache->get('_diff_files');
             $expected_files = $cache->get('_expected_files');
             if ($file_list === false || $file_list['status'] != -1) {
                 $get_files = true;
             }
             if ($get_files) {
                 if ($file_list === false) {
                     $file_list = array('status' => 0);
                 }
                 if (!isset($expected_files) || $expected_files === false) {
                     $expected_files = array();
                 }
                 $processed = 0;
                 foreach ($update_list as $status => $files) {
                     if (!is_array($files)) {
                         continue;
                     }
                     foreach ($files as $file_struct) {
                         // Skip this file if the user selected to not update it
                         if (in_array($file_struct['filename'], $no_update)) {
                             $expected_files[$file_struct['filename']] = false;
                             continue;
                         }
                         // Already handled... then skip of course...
                         if (isset($file_list[$file_struct['filename']])) {
                             continue;
                         }
                         // Refresh if we reach 5 diffs...
                         if ($processed >= 5) {
                             $cache->put('_diff_files', $file_list);
                             if ($request->variable('download', false)) {
                                 $params[] = 'download=1';
                             }
                             $redirect_url = append_sid($this->p_master->module_url, "language={$language}&amp;mode={$mode}&amp;sub=update_files&amp;" . implode('&amp;', $params));
                             meta_refresh(3, $redirect_url);
                             $template->assign_vars(array('S_IN_PROGRESS' => true, 'L_IN_PROGRESS' => $user->lang['MERGING_FILES'], 'L_IN_PROGRESS_EXPLAIN' => $user->lang['MERGING_FILES_EXPLAIN']));
                             return;
                         }
                         if (file_exists($phpbb_root_path . $file_struct['filename'])) {
                             $contents = file_get_contents($phpbb_root_path . $file_struct['filename']);
                             if (isset($expected_files[$file_struct['filename']]) && md5($contents) == $expected_files[$file_struct['filename']]) {
                                 continue;
                             }
                         }
                         $original_filename = $file_struct['custom'] ? $file_struct['original'] : $file_struct['filename'];
                         switch ($status) {
                             case 'modified':
                                 $option = isset($modified[$file_struct['filename']]) ? $modified[$file_struct['filename']] : 0;
                                 switch ($option) {
                                     case MERGE_NO_MERGE_NEW:
                                         $contents = file_get_contents($this->new_location . $original_filename);
                                         break;
                                     case MERGE_NO_MERGE_MOD:
                                         $contents = file_get_contents($phpbb_root_path . $file_struct['filename']);
                                         break;
                                     default:
                                         $diff = $this->return_diff($this->old_location . $original_filename, $phpbb_root_path . $file_struct['filename'], $this->new_location . $original_filename);
                                         $contents = implode("\n", $diff->merged_output());
                                         unset($diff);
                                         break;
                                 }
                                 $expected_files[$file_struct['filename']] = md5($contents);
                                 $file_list[$file_struct['filename']] = '_file_' . md5($file_struct['filename']);
                                 $cache->put($file_list[$file_struct['filename']], base64_encode($contents));
                                 $file_list['status']++;
                                 $processed++;
                                 break;
                             case 'conflict':
                                 $option = $conflicts[$file_struct['filename']];
                                 $contents = '';
                                 switch ($option) {
                                     case MERGE_NO_MERGE_NEW:
                                         $contents = file_get_contents($this->new_location . $original_filename);
                                         break;
                                     case MERGE_NO_MERGE_MOD:
                                         $contents = file_get_contents($phpbb_root_path . $file_struct['filename']);
                                         break;
                                     default:
                                         $diff = $this->return_diff($this->old_location . $original_filename, $phpbb_root_path . $file_struct['filename'], $this->new_location . $original_filename);
                                         if ($option == MERGE_NEW_FILE) {
                                             $contents = implode("\n", $diff->merged_new_output());
                                         } else {
                                             if ($option == MERGE_MOD_FILE) {
                                                 $contents = implode("\n", $diff->merged_orig_output());
                                             } else {
                                                 unset($diff);
                                                 break 2;
                                             }
                                         }
                                         unset($diff);
                                         break;
                                 }
                                 $expected_files[$file_struct['filename']] = md5($contents);
                                 $file_list[$file_struct['filename']] = '_file_' . md5($file_struct['filename']);
                                 $cache->put($file_list[$file_struct['filename']], base64_encode($contents));
                                 $file_list['status']++;
                                 $processed++;
                                 break;
                         }
                     }
                 }
                 $cache->put('_expected_files', $expected_files);
             }
             $file_list['status'] = -1;
             $cache->put('_diff_files', $file_list);
             if ($request->variable('download', false)) {
                 $this->include_file('includes/functions_compress.' . $phpEx);
                 $use_method = $request->variable('use_method', '');
                 $methods = array('.tar');
                 $available_methods = array('.tar.gz' => 'zlib', '.tar.bz2' => 'bz2', '.zip' => 'zlib');
                 foreach ($available_methods as $type => $module) {
                     if (!@extension_loaded($module)) {
                         continue;
                     }
                     $methods[] = $type;
                 }
                 // Let the user decide in which format he wants to have the pack
                 if (!$use_method) {
                     $this->page_title = 'SELECT_DOWNLOAD_FORMAT';
                     $radio_buttons = '';
                     foreach ($methods as $method) {
                         $radio_buttons .= '<label><input type="radio"' . (!$radio_buttons ? ' id="use_method"' : '') . ' class="radio" value="' . $method . '" name="use_method" /> ' . $method . '</label>';
                     }
                     $template->assign_vars(array('S_DOWNLOAD_FILES' => true, 'U_ACTION' => append_sid($this->p_master->module_url, "language={$language}&amp;mode={$mode}&amp;sub=update_files"), 'RADIO_BUTTONS' => $radio_buttons, 'S_HIDDEN_FIELDS' => $s_hidden_fields));
                     // To ease the update process create a file location map
                     $update_list = $cache->get('_update_list');
                     $script_path = $config['force_server_vars'] ? $config['script_path'] == '/' ? '/' : $config['script_path'] . '/' : $user->page['root_script_path'];
                     foreach ($update_list as $status => $files) {
                         if ($status == 'up_to_date' || $status == 'no_update' || $status == 'status' || $status == 'status_deleted') {
                             continue;
                         }
                         foreach ($files as $file_struct) {
                             if (in_array($file_struct['filename'], $no_update)) {
                                 continue;
                             }
                             $template->assign_block_vars('location', array('SOURCE' => htmlspecialchars($file_struct['filename']), 'DESTINATION' => $script_path . htmlspecialchars($file_struct['filename'])));
                         }
                     }
                     return;
                 }
                 if (!in_array($use_method, $methods)) {
                     $use_method = '.tar';
                 }
                 $update_mode = 'download';
             } else {
                 $this->include_file('includes/functions_transfer.' . $phpEx);
                 // Choose FTP, if not available use fsock...
                 $method = basename($request->variable('method', ''));
                 $submit = isset($_POST['submit']) ? true : false;
                 $test_ftp_connection = $request->variable('test_connection', '');
                 if (!$method || !class_exists($method)) {
                     $method = 'ftp';
                     $methods = transfer::methods();
                     if (!in_array('ftp', $methods)) {
                         $method = $methods[0];
                     }
                 }
                 $test_connection = false;
                 if ($test_ftp_connection || $submit) {
                     $transfer = new $method($request->variable('host', ''), $request->variable('username', ''), htmlspecialchars_decode($request->untrimmed_variable('password', '')), $request->variable('root_path', ''), $request->variable('port', ''), $request->variable('timeout', ''));
                     $test_connection = $transfer->open_session();
                     // Make sure that the directory is correct by checking for the existence of common.php
                     if ($test_connection === true) {
                         // Check for common.php file
                         if (!$transfer->file_exists($phpbb_root_path, 'common.' . $phpEx)) {
                             $test_connection = 'ERR_WRONG_PATH_TO_PHPBB';
                         }
                     }
                     $transfer->close_session();
                     // Make sure the login details are correct before continuing
                     if ($submit && $test_connection !== true) {
                         $submit = false;
                         $test_ftp_connection = true;
                     }
                 }
                 $s_hidden_fields .= build_hidden_fields(array('method' => $method));
                 if (!$submit) {
                     $this->page_title = 'SELECT_FTP_SETTINGS';
                     if (!class_exists($method)) {
                         trigger_error('Method does not exist.', E_USER_ERROR);
                     }
                     $requested_data = call_user_func(array($method, 'data'));
                     foreach ($requested_data as $data => $default) {
                         $template->assign_block_vars('data', array('DATA' => $data, 'NAME' => $user->lang[strtoupper($method . '_' . $data)], 'EXPLAIN' => $user->lang[strtoupper($method . '_' . $data) . '_EXPLAIN'], 'DEFAULT' => $request->variable($data, (string) $default)));
                     }
                     $template->assign_vars(array('S_CONNECTION_SUCCESS' => $test_ftp_connection && $test_connection === true ? true : false, 'S_CONNECTION_FAILED' => $test_ftp_connection && $test_connection !== true ? true : false, 'ERROR_MSG' => $test_ftp_connection && $test_connection !== true ? $user->lang[$test_connection] : '', 'S_FTP_UPLOAD' => true, 'UPLOAD_METHOD' => $method, 'U_ACTION' => append_sid($this->p_master->module_url, "language={$language}&amp;mode={$mode}&amp;sub=update_files"), 'U_DOWNLOAD_METHOD' => append_sid($this->p_master->module_url, "language={$language}&amp;mode={$mode}&amp;sub=update_files&amp;download=1"), 'S_HIDDEN_FIELDS' => $s_hidden_fields));
                     return;
                 }
                 $update_mode = 'upload';
             }
             // Now update the installation or download the archive...
             $download_filename = 'update_' . $this->update_info['version']['from'] . '_to_' . $this->update_info['version']['to'];
             $archive_filename = $download_filename . '_' . time() . '_' . unique_id();
             // Now init the connection
             if ($update_mode == 'download') {
                 if ($this->filesystem->is_writable($phpbb_root_path . 'store/')) {
                     trigger_error(sprintf('The directory “%s” is not writable.', $phpbb_root_path . 'store/'), E_USER_ERROR);
                 }
                 if ($use_method == '.zip') {
                     $compress = new compress_zip('w', $phpbb_root_path . 'store/' . $archive_filename . $use_method);
                 } else {
                     $compress = new compress_tar('w', $phpbb_root_path . 'store/' . $archive_filename . $use_method, $use_method);
                 }
             } else {
                 $transfer = new $method($request->variable('host', ''), $request->variable('username', ''), htmlspecialchars_decode($request->untrimmed_variable('password', '')), $request->variable('root_path', ''), $request->variable('port', ''), $request->variable('timeout', ''));
                 $transfer->open_session();
             }
             // Ok, go through the update list and do the operations based on their status
             foreach ($update_list as $status => $files) {
                 if (!is_array($files)) {
                     continue;
                 }
                 foreach ($files as $file_struct) {
                     // Skip this file if the user selected to not update it
                     if (in_array($file_struct['filename'], $no_update)) {
                         continue;
                     }
                     $original_filename = $file_struct['custom'] ? $file_struct['original'] : $file_struct['filename'];
                     switch ($status) {
                         case 'new':
                         case 'new_conflict':
                         case 'not_modified':
                             if ($update_mode == 'download') {
                                 $compress->add_custom_file($this->new_location . $original_filename, $file_struct['filename']);
                             } else {
                                 if ($status != 'new') {
                                     $transfer->rename($file_struct['filename'], $file_struct['filename'] . '.bak');
                                 }
                                 // New directory too?
                                 $dirname = dirname($file_struct['filename']);
                                 if ($dirname && !file_exists($phpbb_root_path . $dirname)) {
                                     $transfer->make_dir($dirname);
                                 }
                                 $transfer->copy_file($this->new_location . $original_filename, $file_struct['filename']);
                             }
                             break;
                         case 'modified':
                             $contents = base64_decode($cache->get($file_list[$file_struct['filename']]));
                             if ($update_mode == 'download') {
                                 $compress->add_data($contents, $file_struct['filename']);
                             } else {
                                 // @todo add option to specify if a backup file should be created?
                                 $transfer->rename($file_struct['filename'], $file_struct['filename'] . '.bak');
                                 $transfer->write_file($file_struct['filename'], $contents);
                             }
                             break;
                         case 'conflict':
                             $contents = base64_decode($cache->get($file_list[$file_struct['filename']]));
                             if ($update_mode == 'download') {
                                 $compress->add_data($contents, $file_struct['filename']);
                             } else {
                                 $transfer->rename($file_struct['filename'], $file_struct['filename'] . '.bak');
                                 $transfer->write_file($file_struct['filename'], $contents);
                             }
                             break;
                         case 'deleted':
                             if ($update_mode != 'download') {
                                 $transfer->rename($file_struct['filename'], $file_struct['filename'] . '.bak');
                             }
                             break;
                     }
                 }
             }
             if ($update_mode == 'download') {
                 $compress->close();
                 $compress->download($archive_filename, $download_filename);
                 @unlink($phpbb_root_path . 'store/' . $archive_filename . $use_method);
                 exit;
             } else {
                 $transfer->close_session();
                 $template->assign_vars(array('S_UPLOAD_SUCCESS' => true, 'U_ACTION' => append_sid($this->p_master->module_url, "language={$language}&amp;mode={$mode}&amp;sub=file_check")));
                 return;
             }
             break;
     }
 }