PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/duplicator/installer/build/ajax.step2.php

https://gitlab.com/sokeara/Tribal_Education
PHP | 279 lines | 255 code | 13 blank | 11 comment | 9 complexity | d8a9c3deccb0094e716bbc1f406622cc MD5 | raw file
  1. <?php
  2. // Exit if accessed directly
  3. if (! defined('DUPLICATOR_INIT')) {
  4. $_baseURL = "http://" . strlen($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'];
  5. header("HTTP/1.1 301 Moved Permanently");
  6. header("Location: $_baseURL");
  7. exit;
  8. }
  9. /* JSON RESPONSE: Most sites have warnings turned off by default, but if they're turned on the warnings
  10. cause errors in the JSON data Here we hide the status so warning level is reset at it at the end*/
  11. $ajax2_error_level = error_reporting();
  12. error_reporting(E_ERROR);
  13. //====================================================================================================
  14. //DATABASE UPDATES
  15. //====================================================================================================
  16. $ajax2_start = DUPX_Util::get_microtime();
  17. //MYSQL CONNECTION
  18. $dbh = DUPX_Util::db_connect($_POST['dbhost'], $_POST['dbuser'], html_entity_decode($_POST['dbpass']), $_POST['dbname'], $_POST['dbport']);
  19. $charset_server = @mysqli_character_set_name($dbh);
  20. @mysqli_query($dbh, "SET wait_timeout = {$GLOBALS['DB_MAX_TIME']}");
  21. DUPX_Util::mysql_set_charset($dbh, $_POST['dbcharset'], $_POST['dbcollate']);
  22. //POST PARAMS
  23. $_POST['blogname'] = mysqli_real_escape_string($dbh, $_POST['blogname']);
  24. $_POST['postguid'] = isset($_POST['postguid']) && $_POST['postguid'] == 1 ? 1 : 0;
  25. $_POST['fullsearch'] = isset($_POST['fullsearch']) && $_POST['fullsearch'] == 1 ? 1 : 0;
  26. $_POST['path_old'] = isset($_POST['path_old']) ? trim($_POST['path_old']) : null;
  27. $_POST['path_new'] = isset($_POST['path_new']) ? trim($_POST['path_new']) : null;
  28. $_POST['siteurl'] = isset($_POST['siteurl']) ? rtrim(trim($_POST['siteurl']), '/') : null;
  29. $_POST['tables'] = isset($_POST['tables']) && is_array($_POST['tables']) ? array_map('stripcslashes', $_POST['tables']) : array();
  30. $_POST['url_old'] = isset($_POST['url_old']) ? trim($_POST['url_old']) : null;
  31. $_POST['url_new'] = isset($_POST['url_new']) ? rtrim(trim($_POST['url_new']), '/') : null;
  32. //LOGGING
  33. $POST_LOG = $_POST;
  34. unset($POST_LOG['tables']);
  35. unset($POST_LOG['plugins']);
  36. unset($POST_LOG['dbpass']);
  37. ksort($POST_LOG);
  38. $date = @date('h:i:s');
  39. $charset_client = @mysqli_character_set_name($dbh);
  40. $log = <<<LOG
  41. \n\n
  42. ********************************************************************************
  43. DUPLICATOR INSTALL-LOG
  44. STEP2 START @ {$date}
  45. NOTICE: Do not post to public sites or forums
  46. ********************************************************************************
  47. CHARSET SERVER:\t{$charset_server}
  48. CHARSET CLIENT:\t {$charset_client} \n
  49. LOG;
  50. DUPX_Log::Info($log);
  51. //Detailed logging
  52. $log = "--------------------------------------\n";
  53. $log .= "POST DATA\n";
  54. $log .= "--------------------------------------\n";
  55. $log .= print_r($POST_LOG, true);
  56. $log .= "--------------------------------------\n";
  57. $log .= "SCANNED TABLES\n";
  58. $log .= "--------------------------------------\n";
  59. $log .= (isset($_POST['tables']) && count($_POST['tables'] > 0))
  60. ? print_r($_POST['tables'], true)
  61. : 'No tables selected to update';
  62. $log .= "--------------------------------------\n";
  63. $log .= "KEEP PLUGINS ACTIVE\n";
  64. $log .= "--------------------------------------\n";
  65. $log .= (isset($_POST['plugins']) && count($_POST['plugins'] > 0))
  66. ? print_r($_POST['plugins'], true)
  67. : 'No plugins selected for activation';
  68. DUPX_Log::Info($log, 2);
  69. //UPDATE SETTINGS
  70. $serial_plugin_list = (isset($_POST['plugins']) && count($_POST['plugins'] > 0)) ? @serialize($_POST['plugins']) : '';
  71. mysqli_query($dbh, "UPDATE `{$GLOBALS['FW_TABLEPREFIX']}options` SET option_value = '{$_POST['blogname']}' WHERE option_name = 'blogname' ");
  72. mysqli_query($dbh, "UPDATE `{$GLOBALS['FW_TABLEPREFIX']}options` SET option_value = '{$serial_plugin_list}' WHERE option_name = 'active_plugins' ");
  73. $log = "--------------------------------------\n";
  74. $log .= "SERIALIZER ENGINE\n";
  75. $log .= "[*] scan every column\n";
  76. $log .= "[~] scan only text columns\n";
  77. $log .= "[^] no searchable columns\n";
  78. $log .= "--------------------------------------";
  79. DUPX_Log::Info($log);
  80. $url_old_json = str_replace('"', "", json_encode($_POST['url_old']));
  81. $url_new_json = str_replace('"', "", json_encode($_POST['url_new']));
  82. $path_old_json = str_replace('"', "", json_encode($_POST['path_old']));
  83. $path_new_json = str_replace('"', "", json_encode($_POST['path_new']));
  84. array_push($GLOBALS['REPLACE_LIST'],
  85. array('search' => $_POST['url_old'], 'replace' => $_POST['url_new']),
  86. array('search' => $_POST['path_old'], 'replace' => $_POST['path_new']),
  87. array('search' => $url_old_json, 'replace' => $url_new_json),
  88. array('search' => $path_old_json, 'replace' => $path_new_json),
  89. array('search' => urlencode($_POST['path_old']), 'replace' => urlencode($_POST['path_new'])),
  90. array('search' => urlencode($_POST['url_old']), 'replace' => urlencode($_POST['url_new'])),
  91. array('search' => rtrim(DUPX_Util::unset_safe_path($_POST['path_old']), '\\'), 'replace' => rtrim($_POST['path_new'], '/'))
  92. );
  93. //Remove trailing slashes
  94. function _dupx_array_rtrim(&$value) {
  95. $value = rtrim($value, '\/');
  96. }
  97. array_walk_recursive($GLOBALS['REPLACE_LIST'], _dupx_array_rtrim);
  98. @mysqli_autocommit($dbh, false);
  99. $report = DUPX_UpdateEngine::load($dbh, $GLOBALS['REPLACE_LIST'], $_POST['tables'], $_POST['fullsearch']);
  100. @mysqli_commit($dbh);
  101. @mysqli_autocommit($dbh, true);
  102. //BUILD JSON RESPONSE
  103. $JSON = array();
  104. $JSON['step1'] = json_decode(urldecode($_POST['json']));
  105. $JSON['step2'] = $report;
  106. $JSON['step2']['warn_all'] = 0;
  107. $JSON['step2']['warnlist'] = array();
  108. DUPX_UpdateEngine::log_stats($report);
  109. DUPX_UpdateEngine::log_errors($report);
  110. //Reset the postguid data
  111. if ($_POST['postguid']) {
  112. mysqli_query($dbh, "UPDATE `{$GLOBALS['FW_TABLEPREFIX']}posts` SET guid = REPLACE(guid, '{$_POST['url_new']}', '{$_POST['url_old']}')");
  113. $update_guid = @mysqli_affected_rows($dbh) or 0;
  114. DUPX_Log::Info("Reverted '{$update_guid}' post guid columns back to '{$_POST['url_old']}'");
  115. }
  116. /* FINAL UPDATES: Must happen after the global replace to prevent double pathing
  117. http://xyz.com/abc01 will become http://xyz.com/abc0101 with trailing data */
  118. mysqli_query($dbh, "UPDATE `{$GLOBALS['FW_TABLEPREFIX']}options` SET option_value = '{$_POST['url_new']}' WHERE option_name = 'home' ");
  119. mysqli_query($dbh, "UPDATE `{$GLOBALS['FW_TABLEPREFIX']}options` SET option_value = '{$_POST['siteurl']}' WHERE option_name = 'siteurl' ");
  120. //====================================================================================================
  121. //FINAL CLEANUP
  122. //====================================================================================================
  123. DUPX_Log::Info("\n********************************************************************************");
  124. DUPX_Log::Info('START FINAL CLEANUP: ' . @date('h:i:s'));
  125. DUPX_Log::Info("********************************************************************************");
  126. /*CREATE NEW USER LOGIC */
  127. if (strlen($_POST['wp_username']) >= 4 && strlen($_POST['wp_password']) >= 6) {
  128. $newuser_check = mysqli_query($dbh, "SELECT COUNT(*) AS count FROM `{$GLOBALS['FW_TABLEPREFIX']}users` WHERE user_login = '{$_POST['wp_username']}' ");
  129. $newuser_row = mysqli_fetch_row($newuser_check);
  130. $newuser_count = is_null($newuser_row) ? 0 : $newuser_row[0];
  131. if ($newuser_count == 0) {
  132. $newuser_datetime = @date("Y-m-d H:i:s");
  133. $newuser_security = mysqli_real_escape_string($dbh, 'a:1:{s:13:"administrator";s:1:"1";}');
  134. $newuser_test1 = @mysqli_query($dbh, "INSERT INTO `{$GLOBALS['FW_TABLEPREFIX']}users`
  135. (`user_login`, `user_pass`, `user_nicename`, `user_email`, `user_registered`, `user_activation_key`, `user_status`, `display_name`)
  136. VALUES ('{$_POST['wp_username']}', MD5('{$_POST['wp_password']}'), '{$_POST['wp_username']}', '', '{$newuser_datetime}', '', '0', '{$_POST['wp_username']}')");
  137. $newuser_insert_id = mysqli_insert_id($dbh);
  138. $newuser_test2 = @mysqli_query($dbh, "INSERT INTO `{$GLOBALS['FW_TABLEPREFIX']}usermeta`
  139. (`user_id`, `meta_key`, `meta_value`) VALUES ('{$newuser_insert_id}', '{$GLOBALS['FW_TABLEPREFIX']}capabilities', '{$newuser_security}')");
  140. $newuser_test3 = @mysqli_query($dbh, "INSERT INTO `{$GLOBALS['FW_TABLEPREFIX']}usermeta`
  141. (`user_id`, `meta_key`, `meta_value`) VALUES ('{$newuser_insert_id}', '{$GLOBALS['FW_TABLEPREFIX']}user_level', '10')");
  142. //Misc Meta-Data Settings:
  143. @mysqli_query($dbh, "INSERT INTO `{$GLOBALS['FW_TABLEPREFIX']}usermeta` (`user_id`, `meta_key`, `meta_value`) VALUES ('{$newuser_insert_id}', 'rich_editing', 'true')");
  144. @mysqli_query($dbh, "INSERT INTO `{$GLOBALS['FW_TABLEPREFIX']}usermeta` (`user_id`, `meta_key`, `meta_value`) VALUES ('{$newuser_insert_id}', 'admin_color', 'fresh')");
  145. @mysqli_query($dbh, "INSERT INTO `{$GLOBALS['FW_TABLEPREFIX']}usermeta` (`user_id`, `meta_key`, `meta_value`) VALUES ('{$newuser_insert_id}', 'nickname', '{$_POST['wp_username']}')");
  146. if ($newuser_test1 && $newuser_test2 && $newuser_test3) {
  147. DUPX_Log::Info("NEW WP-ADMIN USER: New username '{$_POST['wp_username']}' was created successfully \n ");
  148. } else {
  149. $newuser_warnmsg = "NEW WP-ADMIN USER: Failed to create the user '{$_POST['wp_username']}' \n ";
  150. $JSON['step2']['warnlist'][] = $newuser_warnmsg;
  151. DUPX_Log::Info($newuser_warnmsg);
  152. }
  153. }
  154. else {
  155. $newuser_warnmsg = "NEW WP-ADMIN USER: Username '{$_POST['wp_username']}' already exists in the database. Unable to create new account \n";
  156. $JSON['step2']['warnlist'][] = $newuser_warnmsg;
  157. DUPX_Log::Info($newuser_warnmsg);
  158. }
  159. }
  160. /*MU Updates*/
  161. $mu_newDomain = parse_url($_POST['url_new']);
  162. $mu_oldDomain = parse_url($_POST['url_old']);
  163. $mu_newDomainHost = $mu_newDomain['host'];
  164. $mu_oldDomainHost = $mu_oldDomain['host'];
  165. $mu_newUrlPath = parse_url($_POST['url_new'], PHP_URL_PATH);
  166. $mu_oldUrlPath = parse_url($_POST['url_old'], PHP_URL_PATH);
  167. $mu_updates = @mysqli_query($dbh, "UPDATE `{$GLOBALS['FW_TABLEPREFIX']}blogs` SET domain = '{$mu_newDomainHost}' WHERE domain = '{$mu_oldDomainHost}'");
  168. if ($mu_updates) {
  169. DUPX_Log::Info("Update MU table blogs: domain {$mu_newDomainHost} ");
  170. } else {
  171. DUPX_Log::Info("UPDATE `{$GLOBALS['FW_TABLEPREFIX']}blogs` SET domain = '{$mu_newDomainHost}' WHERE domain = '{$mu_oldDomainHost}'");
  172. }
  173. /*UPDATE WP-CONFIG FILE */
  174. $patterns = array("/('|\")WP_HOME.*?\)\s*;/",
  175. "/('|\")WP_SITEURL.*?\)\s*;/",
  176. "/('|\")DOMAIN_CURRENT_SITE.*?\)\s*;/",
  177. "/('|\")PATH_CURRENT_SITE.*?\)\s*;/");
  178. $replace = array("'WP_HOME', '{$_POST['url_new']}');",
  179. "'WP_SITEURL', '{$_POST['url_new']}');",
  180. "'DOMAIN_CURRENT_SITE', '{$mu_newDomainHost}');",
  181. "'PATH_CURRENT_SITE', '{$mu_newUrlPath}');");
  182. $config_file = @file_get_contents('wp-config.php', true);
  183. $config_file = preg_replace($patterns, $replace, $config_file);
  184. file_put_contents('wp-config.php', $config_file);
  185. //Create Snapshots directory
  186. if (!file_exists(DUPLICATOR_SSDIR_NAME)) {
  187. mkdir(DUPLICATOR_SSDIR_NAME, 0755);
  188. }
  189. $fp = fopen(DUPLICATOR_SSDIR_NAME . '/index.php', 'w');
  190. fclose($fp);
  191. //===============================
  192. //NOTICE TESTS
  193. //===============================
  194. DUPX_Log::Info("\n--------------------------------------");
  195. DUPX_Log::Info("NOTICES");
  196. DUPX_Log::Info("--------------------------------------");
  197. $config_vars = array('WP_CONTENT_DIR', 'WP_CONTENT_URL', 'WPCACHEHOME', 'COOKIE_DOMAIN', 'WP_SITEURL', 'WP_HOME', 'WP_TEMP_DIR');
  198. $config_found = DUPX_Util::string_has_value($config_vars, $config_file);
  199. //Files
  200. if ($config_found) {
  201. $msg = 'WP-CONFIG NOTICE: The wp-config.php has one or more of the following values set [' . implode(", ", $config_vars) . ']. Please validate these values are correct by opening the file and checking the values.';
  202. $JSON['step2']['warnlist'][] = $msg;
  203. DUPX_Log::Info($msg);
  204. }
  205. //Database
  206. $result = @mysqli_query($dbh, "SELECT option_value FROM `{$GLOBALS['FW_TABLEPREFIX']}options` WHERE option_name IN ('upload_url_path','upload_path')");
  207. if ($result) {
  208. while ($row = mysqli_fetch_row($result)) {
  209. if (strlen($row[0])) {
  210. $msg = "MEDIA SETTINGS NOTICE: The table '{$GLOBALS['FW_TABLEPREFIX']}options' has at least one the following values ['upload_url_path','upload_path'] set please validate settings. These settings can be changed in the wp-admin by going to Settings->Media area see 'Uploading Files'";
  211. $JSON['step2']['warnlist'][] = $msg;
  212. DUPX_Log::Info($msg);
  213. break;
  214. }
  215. }
  216. }
  217. if (empty($JSON['step2']['warnlist'])) {
  218. DUPX_Log::Info("No Notices Found\n");
  219. }
  220. $JSON['step2']['warn_all'] = empty($JSON['step2']['warnlist']) ? 0 : count($JSON['step2']['warnlist']);
  221. mysqli_close($dbh);
  222. @unlink('database.sql');
  223. //CONFIG Setup
  224. DUPX_ServerConfig::Setup();
  225. $ajax2_end = DUPX_Util::get_microtime();
  226. $ajax2_sum = DUPX_Util::elapsed_time($ajax2_end, $ajax2_start);
  227. DUPX_Log::Info("********************************************************************************");
  228. DUPX_Log::Info('STEP 2 COMPLETE @ ' . @date('h:i:s') . " - TOTAL RUNTIME: {$ajax2_sum}");
  229. DUPX_Log::Info("********************************************************************************");
  230. $JSON['step2']['pass'] = 1;
  231. error_reporting($ajax2_error_level);
  232. die(json_encode($JSON));
  233. ?>