/wp-content/plugins/all-in-one-wp-migration/lib/model/import/class-ai1wm-import-database.php

https://gitlab.com/vanafroo/landingpage · PHP · 466 lines · 265 code · 92 blank · 109 comment · 62 complexity · 70f7ba30d3195c8cdb05103255b9bb1d MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright (C) 2014-2016 ServMask Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * ███████╗███████╗██████╗ ██╗ ██╗███╗ ███╗ █████╗ ███████╗██╗ ██╗
  19. * ██╔════╝██╔════╝██╔══██╗██║ ██║████╗ ████║██╔══██╗██╔════╝██║ ██╔╝
  20. * ███████╗█████╗ ██████╔╝██║ ██║██╔████╔██║███████║███████╗█████╔╝
  21. * ╚════██║██╔══╝ ██╔══██╗╚██╗ ██╔╝██║╚██╔╝██║██╔══██║╚════██║██╔═██╗
  22. * ███████║███████╗██║ ██║ ╚████╔╝ ██║ ╚═╝ ██║██║ ██║███████║██║ ██╗
  23. * ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝
  24. */
  25. class Ai1wm_Import_Database {
  26. public static function execute( $params ) {
  27. global $wpdb;
  28. // Skip database import
  29. if ( ! is_file( ai1wm_database_path( $params ) ) ) {
  30. return $params;
  31. }
  32. // Read blogs.json file
  33. $handle = fopen( ai1wm_blogs_path( $params ), 'r' );
  34. if ( $handle === false ) {
  35. throw new Ai1wm_Import_Exception( 'Unable to read blogs.json file' );
  36. }
  37. // Parse blogs.json file
  38. $blogs = fread( $handle, filesize( ai1wm_blogs_path( $params ) ) );
  39. $blogs = json_decode( $blogs );
  40. // Close handle
  41. fclose( $handle );
  42. // Read package.json file
  43. $handle = fopen( ai1wm_package_path( $params ), 'r' );
  44. if ( $handle === false ) {
  45. throw new Ai1wm_Import_Exception( 'Unable to read package.json file' );
  46. }
  47. // Parse package.json file
  48. $config = fread( $handle, filesize( ai1wm_package_path( $params ) ) );
  49. $config = json_decode( $config );
  50. // Close handle
  51. fclose( $handle );
  52. // Set progress
  53. Ai1wm_Status::info( __( 'Restoring database...', AI1WM_PLUGIN_NAME ) );
  54. $old_values = array();
  55. $new_values = array();
  56. // Get Blog URLs
  57. foreach ( $blogs as $blog ) {
  58. // Get blogs dir Upload Path
  59. if ( ! in_array( sprintf( "'%s'", trim( ai1wm_blogsdir_path( $blog->Old->Id ), '/' ) ), $old_values ) ) {
  60. $old_values[] = sprintf( "'%s'", trim( ai1wm_blogsdir_path( $blog->Old->Id ), '/' ) );
  61. $new_values[] = sprintf( "'%s'", get_option( 'upload_path' ) );
  62. }
  63. // Get sites Upload Path
  64. if ( ! in_array( sprintf( "'%s'", trim( ai1wm_uploads_path( $blog->Old->Id ), '/' ) ), $old_values ) ) {
  65. $old_values[] = sprintf( "'%s'", trim( ai1wm_uploads_path( $blog->Old->Id ), '/' ) );
  66. $new_values[] = sprintf( "'%s'", get_option( 'upload_path' ) );
  67. }
  68. // Handle old and new sites dir style
  69. if ( defined( 'UPLOADBLOGSDIR' ) ) {
  70. // Get Upload Path
  71. if ( ! in_array( ai1wm_blogsdir_path( $blog->Old->Id ), $old_values ) ) {
  72. $old_values[] = ai1wm_blogsdir_path( $blog->Old->Id );
  73. $new_values[] = ai1wm_blogsdir_path( $blog->New->Id );
  74. }
  75. // Get escaped Upload Path
  76. if ( ! in_array( addslashes( addcslashes( ai1wm_blogsdir_path( $blog->Old->Id ), '\/' ) ), $old_values ) ) {
  77. $old_values[] = addslashes( addcslashes( ai1wm_blogsdir_path( $blog->Old->Id ), '\/' ) );
  78. $new_values[] = addslashes( addcslashes( ai1wm_blogsdir_path( $blog->New->Id ), '\/' ) );
  79. }
  80. // Get Upload Path
  81. if ( ! in_array( ai1wm_uploads_path( $blog->Old->Id ), $old_values ) ) {
  82. $old_values[] = ai1wm_uploads_path( $blog->Old->Id );
  83. $new_values[] = ai1wm_blogsdir_path( $blog->New->Id );
  84. }
  85. // Get escaped Upload Path
  86. if ( ! in_array( addslashes( addcslashes( ai1wm_uploads_path( $blog->Old->Id ), '\/' ) ), $old_values ) ) {
  87. $old_values[] = addslashes( addcslashes( ai1wm_uploads_path( $blog->Old->Id ), '\/' ) );
  88. $new_values[] = addslashes( addcslashes( ai1wm_blogsdir_path( $blog->New->Id ), '\/' ) );
  89. }
  90. } else {
  91. // Get Upload Path
  92. if ( ! in_array( ai1wm_blogsdir_path( $blog->Old->Id ), $old_values ) ) {
  93. $old_values[] = ai1wm_blogsdir_path( $blog->Old->Id );
  94. $new_values[] = ai1wm_uploads_path( $blog->New->Id );
  95. }
  96. // Get escaped Upload Path
  97. if ( ! in_array( addslashes( addcslashes( ai1wm_blogsdir_path( $blog->Old->Id ), '\/' ) ), $old_values ) ) {
  98. $old_values[] = addslashes( addcslashes( ai1wm_blogsdir_path( $blog->Old->Id ), '\/' ) );
  99. $new_values[] = addslashes( addcslashes( ai1wm_uploads_path( $blog->New->Id ), '\/' ) );
  100. }
  101. // Get Upload Path
  102. if ( ! in_array( ai1wm_uploads_path( $blog->Old->Id ), $old_values ) ) {
  103. $old_values[] = ai1wm_uploads_path( $blog->Old->Id );
  104. $new_values[] = ai1wm_uploads_path( $blog->New->Id );
  105. }
  106. // Get escaped Upload Path
  107. if ( ! in_array( addslashes( addcslashes( ai1wm_uploads_path( $blog->Old->Id ), '\/' ) ), $old_values ) ) {
  108. $old_values[] = addslashes( addcslashes( ai1wm_uploads_path( $blog->Old->Id ), '\/' ) );
  109. $new_values[] = addslashes( addcslashes( ai1wm_uploads_path( $blog->New->Id ), '\/' ) );
  110. }
  111. }
  112. // Get Site URL
  113. if ( isset( $blog->Old->SiteURL ) && ( $blog->Old->SiteURL !== $blog->New->SiteURL ) ) {
  114. // Get domain
  115. $old_domain = parse_url( $blog->Old->SiteURL, PHP_URL_HOST );
  116. $new_domain = parse_url( $blog->New->SiteURL, PHP_URL_HOST );
  117. // Get scheme
  118. $new_scheme = parse_url( $blog->New->SiteURL, PHP_URL_SCHEME );
  119. // Replace Site URL scheme
  120. foreach ( array( 'http', 'https' ) as $old_scheme ) {
  121. // Add plain Site URL
  122. if ( ! in_array( set_url_scheme( $blog->Old->SiteURL, $old_scheme ), $old_values ) ) {
  123. $old_values[] = set_url_scheme( $blog->Old->SiteURL, $old_scheme );
  124. $new_values[] = set_url_scheme( $blog->New->SiteURL, $new_scheme );
  125. }
  126. // Add encoded Site URL
  127. if ( ! in_array( urlencode( set_url_scheme( $blog->Old->SiteURL, $old_scheme ) ), $old_values ) ) {
  128. $old_values[] = urlencode( set_url_scheme( $blog->Old->SiteURL, $old_scheme ) );
  129. $new_values[] = urlencode( set_url_scheme( $blog->New->SiteURL, $new_scheme ) );
  130. }
  131. // Add escaped Site URL
  132. if ( ! in_array( addslashes( addcslashes( set_url_scheme( $blog->Old->SiteURL, $old_scheme ), '/' ) ), $old_values ) ) {
  133. $old_values[] = addslashes( addcslashes( set_url_scheme( $blog->Old->SiteURL, $old_scheme ), '/' ) );
  134. $new_values[] = addslashes( addcslashes( set_url_scheme( $blog->New->SiteURL, $new_scheme ), '/' ) );
  135. }
  136. }
  137. // Add email
  138. if ( ! isset( $config->NoEmailReplace ) ) {
  139. if ( ! in_array( sprintf( "@%s", $old_domain ), $old_values ) ) {
  140. $old_values[] = sprintf( "@%s", $old_domain );
  141. $new_values[] = sprintf( "@%s", $new_domain );
  142. }
  143. }
  144. }
  145. // Get Home URL
  146. if ( isset( $blog->Old->HomeURL ) && ( $blog->Old->HomeURL !== $blog->New->HomeURL ) ) {
  147. // Get domain
  148. $old_domain = parse_url( $blog->Old->HomeURL, PHP_URL_HOST );
  149. $new_domain = parse_url( $blog->New->HomeURL, PHP_URL_HOST );
  150. // Get scheme
  151. $new_scheme = parse_url( $blog->New->HomeURL, PHP_URL_SCHEME );
  152. // Replace Home URL scheme
  153. foreach ( array( 'http', 'https' ) as $old_scheme ) {
  154. // Add plain Home URL
  155. if ( ! in_array( set_url_scheme( $blog->Old->HomeURL, $old_scheme ), $old_values ) ) {
  156. $old_values[] = set_url_scheme( $blog->Old->HomeURL, $old_scheme );
  157. $new_values[] = set_url_scheme( $blog->New->HomeURL, $new_scheme );
  158. }
  159. // Add encoded Home URL
  160. if ( ! in_array( urlencode( set_url_scheme( $blog->Old->HomeURL, $old_scheme ) ), $old_values ) ) {
  161. $old_values[] = urlencode( set_url_scheme( $blog->Old->HomeURL, $old_scheme ) );
  162. $new_values[] = urlencode( set_url_scheme( $blog->New->HomeURL, $new_scheme ) );
  163. }
  164. // Add escaped Home URL
  165. if ( ! in_array( addslashes( addcslashes( set_url_scheme( $blog->Old->HomeURL, $old_scheme ), '/' ) ), $old_values ) ) {
  166. $old_values[] = addslashes( addcslashes( set_url_scheme( $blog->Old->HomeURL, $old_scheme ), '/' ) );
  167. $new_values[] = addslashes( addcslashes( set_url_scheme( $blog->New->HomeURL, $new_scheme ), '/' ) );
  168. }
  169. }
  170. // Add email
  171. if ( ! isset( $config->NoEmailReplace ) ) {
  172. if ( ! in_array( sprintf( "@%s", $old_domain ), $old_values ) ) {
  173. $old_values[] = sprintf( "@%s", $old_domain );
  174. $new_values[] = sprintf( "@%s", $new_domain );
  175. }
  176. }
  177. }
  178. }
  179. // Get Site URL
  180. if ( isset( $config->SiteURL ) && ( $config->SiteURL !== site_url() ) ) {
  181. // Get www URL
  182. if ( stripos( $config->SiteURL, '//www.' ) !== false ) {
  183. $www = str_ireplace( '//www.', '//', $config->SiteURL );
  184. } else {
  185. $www = str_ireplace( '//', '//www.', $config->SiteURL );
  186. }
  187. // Replace Site URL
  188. foreach ( array( $config->SiteURL, $www ) as $url ) {
  189. // Get domain
  190. $old_domain = parse_url( $url, PHP_URL_HOST );
  191. $new_domain = parse_url( site_url(), PHP_URL_HOST );
  192. // Get path
  193. $old_path = parse_url( $url, PHP_URL_PATH );
  194. $new_path = parse_url( site_url(), PHP_URL_PATH );
  195. // Get scheme
  196. $new_scheme = parse_url( site_url(), PHP_URL_SCHEME );
  197. // Add domain and path
  198. if ( ! in_array( sprintf( "%s','%s", $old_domain, trailingslashit( $old_path ) ), $old_values ) ) {
  199. $old_values[] = sprintf( "%s','%s", $old_domain, trailingslashit( $old_path ) );
  200. $new_values[] = sprintf( "%s','%s", $new_domain, trailingslashit( $new_path ) );
  201. }
  202. // Replace Site URL scheme
  203. foreach ( array( 'http', 'https' ) as $old_scheme ) {
  204. // Add plain Site URL
  205. if ( ! in_array( set_url_scheme( $url, $old_scheme ), $old_values ) ) {
  206. $old_values[] = set_url_scheme( $url, $old_scheme );
  207. $new_values[] = set_url_scheme( site_url(), $new_scheme );
  208. }
  209. // Add encoded Site URL
  210. if ( ! in_array( urlencode( set_url_scheme( $url, $old_scheme ) ), $old_values ) ) {
  211. $old_values[] = urlencode( set_url_scheme( $url, $old_scheme ) );
  212. $new_values[] = urlencode( set_url_scheme( site_url(), $new_scheme ) );
  213. }
  214. // Add escaped Site URL
  215. if ( ! in_array( addslashes( addcslashes( set_url_scheme( $url, $old_scheme ), '/' ) ), $old_values ) ) {
  216. $old_values[] = addslashes( addcslashes( set_url_scheme( $url, $old_scheme ), '/' ) );
  217. $new_values[] = addslashes( addcslashes( set_url_scheme( site_url(), $new_scheme ), '/' ) );
  218. }
  219. }
  220. // Add email
  221. if ( ! isset( $config->NoEmailReplace ) ) {
  222. if ( ! in_array( sprintf( "@%s", $old_domain ), $old_values ) ) {
  223. $old_values[] = sprintf( "@%s", $old_domain );
  224. $new_values[] = sprintf( "@%s", $new_domain );
  225. }
  226. }
  227. }
  228. }
  229. // Get Home URL
  230. if ( isset( $config->HomeURL ) && ( $config->HomeURL !== home_url() ) ) {
  231. // Get www URL
  232. if ( stripos( $config->HomeURL, '//www.' ) !== false ) {
  233. $www = str_ireplace( '//www.', '//', $config->HomeURL );
  234. } else {
  235. $www = str_ireplace( '//', '//www.', $config->HomeURL );
  236. }
  237. // Replace Home URL
  238. foreach ( array( $config->HomeURL, $www ) as $url ) {
  239. // Get domain
  240. $old_domain = parse_url( $url, PHP_URL_HOST );
  241. $new_domain = parse_url( home_url(), PHP_URL_HOST );
  242. // Get path
  243. $old_path = parse_url( $url, PHP_URL_PATH );
  244. $new_path = parse_url( home_url(), PHP_URL_PATH );
  245. // Get scheme
  246. $new_scheme = parse_url( home_url(), PHP_URL_SCHEME );
  247. // Add domain and path
  248. if ( ! in_array( sprintf( "%s','%s", $old_domain, trailingslashit( $old_path ) ), $old_values ) ) {
  249. $old_values[] = sprintf( "%s','%s", $old_domain, trailingslashit( $old_path ) );
  250. $new_values[] = sprintf( "%s','%s", $new_domain, trailingslashit( $new_path ) );
  251. }
  252. // Replace Home URL scheme
  253. foreach ( array( 'http', 'https' ) as $old_scheme ) {
  254. // Add plain Home URL
  255. if ( ! in_array( set_url_scheme( $url, $old_scheme ), $old_values ) ) {
  256. $old_values[] = set_url_scheme( $url, $old_scheme );
  257. $new_values[] = set_url_scheme( home_url(), $new_scheme );
  258. }
  259. // Add encoded Home URL
  260. if ( ! in_array( urlencode( set_url_scheme( $url, $old_scheme ) ), $old_values ) ) {
  261. $old_values[] = urlencode( set_url_scheme( $url, $old_scheme ) );
  262. $new_values[] = urlencode( set_url_scheme( home_url(), $new_scheme ) );
  263. }
  264. // Add escaped Home URL
  265. if ( ! in_array( addslashes( addcslashes( set_url_scheme( $url, $old_scheme ), '/' ) ), $old_values ) ) {
  266. $old_values[] = addslashes( addcslashes( set_url_scheme( $url, $old_scheme ), '/' ) );
  267. $new_values[] = addslashes( addcslashes( set_url_scheme( home_url(), $new_scheme ), '/' ) );
  268. }
  269. }
  270. // Add email
  271. if ( ! isset( $config->NoEmailReplace ) ) {
  272. if ( ! in_array( sprintf( "@%s", $old_domain ), $old_values ) ) {
  273. $old_values[] = sprintf( "@%s", $old_domain );
  274. $new_values[] = sprintf( "@%s", $new_domain );
  275. }
  276. }
  277. }
  278. }
  279. // Get WordPress Content
  280. if ( isset( $config->WordPress->Content ) && ( $config->WordPress->Content !== WP_CONTENT_DIR ) ) {
  281. // Add plain WordPress Content
  282. if ( ! in_array( $config->WordPress->Content, $old_values ) ) {
  283. $old_values[] = $config->WordPress->Content;
  284. $new_values[] = WP_CONTENT_DIR;
  285. }
  286. // Add encoded WordPress Content
  287. if ( ! in_array( urlencode( $config->WordPress->Content ), $old_values ) ) {
  288. $old_values[] = urlencode( $config->WordPress->Content );
  289. $new_values[] = urlencode( WP_CONTENT_DIR );
  290. }
  291. // Add escaped WordPress Content
  292. if ( ! in_array( addslashes( addcslashes( $config->WordPress->Content, '\/' ) ), $old_values ) ) {
  293. $old_values[] = addslashes( addcslashes( $config->WordPress->Content, '\/' ) );
  294. $new_values[] = addslashes( addcslashes( WP_CONTENT_DIR, '\/' ) );
  295. }
  296. }
  297. // Get URL IP
  298. $url_ip = get_option( AI1WM_URL_IP );
  299. // Get URL adapter
  300. $url_adapter = get_option( AI1WM_URL_ADAPTER );
  301. // Get secret key
  302. $secret_key = get_option( AI1WM_SECRET_KEY );
  303. // Get HTTP user
  304. $auth_user = get_option( AI1WM_AUTH_USER );
  305. // Get HTTP password
  306. $auth_password = get_option( AI1WM_AUTH_PASSWORD );
  307. // Get active ServMask plugins
  308. $active_servmask_plugins = ai1wm_active_servmask_plugins();
  309. $old_prefixes = array();
  310. $new_prefixes = array();
  311. // Set main table prefixes
  312. $old_prefixes[] = ai1wm_servmask_prefix( 'mainsite' );
  313. $new_prefixes[] = ai1wm_table_prefix();
  314. // Set site table prefixes
  315. foreach ( $blogs as $blog ) {
  316. if ( ai1wm_main_site( $blog->Old->Id ) === false ) {
  317. $old_prefixes[] = ai1wm_servmask_prefix( $blog->Old->Id );
  318. $new_prefixes[] = ai1wm_table_prefix( $blog->New->Id );
  319. }
  320. }
  321. // Set base table prefixes
  322. foreach ( $blogs as $blog ) {
  323. if ( ai1wm_main_site( $blog->Old->Id ) === true ) {
  324. $old_prefixes[] = ai1wm_servmask_prefix( 'basesite' );
  325. $new_prefixes[] = ai1wm_table_prefix( $blog->New->Id );
  326. }
  327. }
  328. // Set site table prefixes
  329. foreach ( $blogs as $blog ) {
  330. if ( ai1wm_main_site( $blog->Old->Id ) === true ) {
  331. $old_prefixes[] = ai1wm_servmask_prefix( $blog->Old->Id );
  332. $new_prefixes[] = ai1wm_table_prefix( $blog->New->Id );
  333. }
  334. }
  335. // Set table prefixes
  336. $old_prefixes[] = ai1wm_servmask_prefix();
  337. $new_prefixes[] = ai1wm_table_prefix();
  338. // Get database client
  339. if ( empty( $wpdb->use_mysqli ) ) {
  340. $client = new Ai1wm_Database_Mysql( $wpdb );
  341. } else {
  342. $client = new Ai1wm_Database_Mysqli( $wpdb );
  343. }
  344. // Set database options
  345. $client->set_old_table_prefixes( $old_prefixes )
  346. ->set_new_table_prefixes( $new_prefixes )
  347. ->set_old_replace_values( $old_values )
  348. ->set_new_replace_values( $new_values );
  349. // Flush database
  350. if ( ( $version = $config->Plugin->Version ) ) {
  351. if ( $version !== 'develop' && version_compare( $version, '4.10', '<' ) ) {
  352. $client->set_include_table_prefixes( array( ai1wm_table_prefix() ) );
  353. $client->flush();
  354. }
  355. }
  356. // Import database
  357. $client->import( ai1wm_database_path( $params ) );
  358. // Flush WP cache
  359. ai1wm_cache_flush();
  360. // Activate plugins
  361. activate_plugins( $active_servmask_plugins, null, is_multisite() );
  362. // Set the new URL IP
  363. update_option( AI1WM_URL_IP, $url_ip );
  364. // Set the new URL adapter
  365. update_option( AI1WM_URL_ADAPTER, $url_adapter );
  366. // Set the new secret key value
  367. update_option( AI1WM_SECRET_KEY, $secret_key );
  368. // Set the new HTTP user
  369. update_option( AI1WM_AUTH_USER, $auth_user );
  370. // Set the new HTTP password
  371. update_option( AI1WM_AUTH_PASSWORD, $auth_password );
  372. return $params;
  373. }
  374. }