wordpress_import.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. // Simple WordPress import interface without complex session handling
  3. error_reporting(E_ALL);
  4. ini_set('display_errors', 0);
  5. ini_set('max_execution_time', 120);
  6. ini_set('memory_limit', '512M');
  7. require_once '../includes/config.php';
  8. require_once '../includes/database.php';
  9. require_once '../includes/auth.php';
  10. require_once '../includes/translation.php';
  11. $auth = new Auth();
  12. $auth->requireAuth();
  13. $user = $auth->getUser();
  14. $message = '';
  15. $importResults = null;
  16. $connectionTest = null;
  17. // Handle form submission with simplified processing
  18. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  19. try {
  20. // Get WordPress database configuration
  21. $wpConfig = [
  22. 'host' => trim($_POST['wp_host'] ?? ''),
  23. 'database' => trim($_POST['wp_database'] ?? ''),
  24. 'username' => trim($_POST['wp_username'] ?? ''),
  25. 'password' => $_POST['wp_password'] ?? ''
  26. ];
  27. // Validate required fields
  28. if (empty($wpConfig['host']) || empty($wpConfig['database']) || empty($wpConfig['username'])) {
  29. throw new Exception(t('wordpress_import_config_required'));
  30. }
  31. // Create import instance
  32. $importer = new WordPressImport($wpConfig);
  33. if (isset($_POST['test_connection'])) {
  34. // Test connection only
  35. $connectionTest = $importer->testConnection();
  36. if ($connectionTest['success']) {
  37. $message = t('wordpress_connection_success') . ': ' .
  38. t('posts') . ': ' . $connectionTest['stats']['posts'] . ', ' .
  39. t('categories') . ': ' . $connectionTest['stats']['categories'] . ', ' .
  40. t('users') . ': ' . $connectionTest['stats']['users'] . ', ' .
  41. t('comments') . ': ' . $connectionTest['stats']['comments'];
  42. } else {
  43. throw new Exception($connectionTest['error']);
  44. }
  45. } elseif (isset($_POST['start_import'])) {
  46. // Start import
  47. $importOptions = [
  48. 'import_categories' => isset($_POST['import_categories']),
  49. 'import_users' => isset($_POST['import_users']),
  50. 'import_posts' => isset($_POST['import_posts']),
  51. 'import_comments' => isset($_POST['import_comments'])
  52. ];
  53. $importResults = $importer->importAll($importOptions);
  54. if ($importResults['success']) {
  55. $message = t('wordpress_import_success');
  56. } else {
  57. throw new Exception($importResults['error']);
  58. }
  59. }
  60. } catch (Exception $e) {
  61. $message = $e->getMessage();
  62. }
  63. }
  64. ?>
  65. <!DOCTYPE html>
  66. <html lang="<?php echo Translation::getCurrentLang(); ?>">
  67. <head>
  68. <meta charset="UTF-8">
  69. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  70. <title><?php echo t('wordpress_import'); ?> - <?php echo SITE_TITLE; ?></title>
  71. <link rel="stylesheet" href="../css/style.css">
  72. <link rel="stylesheet" href="../css/admin-import.css">
  73. </head>
  74. <body>
  75. <div class="admin-layout">
  76. <header class="admin-header">
  77. <div class="header-content">
  78. <h1><a href="/index.php"><?php echo SITE_TITLE; ?></a></h1>
  79. <nav class="admin-nav">
  80. <a href="index.php" class="nav-link"><?php echo t('admin_nav_dashboard'); ?></a>
  81. <a href="publications.php" class="nav-link"><?php echo t('admin_nav_publications'); ?></a>
  82. <a href="categories.php" class="nav-link"><?php echo t('admin_nav_categories'); ?></a>
  83. <a href="comments.php" class="nav-link"><?php echo t('admin_nav_comments'); ?></a>
  84. <a href="users.php" class="nav-link"><?php echo t('manage_users'); ?></a>
  85. <a href="wordpress_import.php" class="nav-link active"><?php echo t('wordpress_import'); ?></a>
  86. <?php if (LDAP_ENABLED): ?>
  87. <a href="ldap-users.php" class="nav-link"><?php echo t('admin_nav_ldap_users'); ?></a>
  88. <?php endif; ?>
  89. <a href="logout.php" class="nav-link"><?php echo t('admin_nav_logout'); ?></a>
  90. </nav>
  91. <div class="user-info">
  92. <?php echo t('welcome'); ?>, <?php echo htmlspecialchars($user['username']); ?>
  93. </div>
  94. <?php echo Translation::getLanguageSwitcher('wordpress_import.php'); ?>
  95. </div>
  96. </header>
  97. <main class="admin-main">
  98. <div class="page-header">
  99. <h2><?php echo t('wordpress_import'); ?></h2>
  100. <p class="page-description"><?php echo t('wordpress_import_description'); ?></p>
  101. </div>
  102. <?php if ($message): ?>
  103. <div class="alert alert-<?php echo strpos($message, 'Error') === false && strpos($message, 'failed') === false ? 'success' : 'error'; ?>">
  104. <?php echo $message; ?>
  105. </div>
  106. <?php endif; ?>
  107. <!-- Import Form -->
  108. <div class="import-form-container">
  109. <form method="post" class="import-form">
  110. <div class="form-section">
  111. <h3><?php echo t('wordpress_database_config'); ?></h3>
  112. <div class="form-row">
  113. <div class="form-group">
  114. <label for="wp_host"><?php echo t('database_host'); ?> *</label>
  115. <input type="text" id="wp_host" name="wp_host"
  116. value="<?php echo htmlspecialchars($_POST['wp_host'] ?? 'localhost'); ?>"
  117. placeholder="localhost" required>
  118. </div>
  119. <div class="form-group">
  120. <label for="wp_database"><?php echo t('database_name'); ?> *</label>
  121. <input type="text" id="wp_database" name="wp_database"
  122. value="<?php echo htmlspecialchars($_POST['wp_database'] ?? ''); ?>"
  123. placeholder="wordpress" required>
  124. </div>
  125. </div>
  126. <div class="form-row">
  127. <div class="form-group">
  128. <label for="wp_username"><?php echo t('database_username'); ?> *</label>
  129. <input type="text" id="wp_username" name="wp_username"
  130. value="<?php echo htmlspecialchars($_POST['wp_username'] ?? ''); ?>"
  131. placeholder="wp_user" required>
  132. </div>
  133. <div class="form-group">
  134. <label for="wp_password"><?php echo t('database_password'); ?></label>
  135. <input type="password" id="wp_password" name="wp_password"
  136. placeholder="<?php echo t('database_password_placeholder'); ?>">
  137. </div>
  138. </div>
  139. </div>
  140. <div class="form-section">
  141. <h3><?php echo t('import_options'); ?></h3>
  142. <div class="checkbox-group">
  143. <label class="checkbox-label">
  144. <input type="checkbox" name="import_categories" checked>
  145. <span class="checkmark"></span>
  146. <?php echo t('import_categories'); ?>
  147. </label>
  148. <label class="checkbox-label">
  149. <input type="checkbox" name="import_users" checked>
  150. <span class="checkmark"></span>
  151. <?php echo t('import_users'); ?>
  152. </label>
  153. <label class="checkbox-label">
  154. <input type="checkbox" name="import_posts" checked>
  155. <span class="checkmark"></span>
  156. <?php echo t('import_posts'); ?>
  157. </label>
  158. <label class="checkbox-label">
  159. <input type="checkbox" name="import_comments" checked>
  160. <span class="checkmark"></span>
  161. <?php echo t('import_comments'); ?>
  162. </label>
  163. </div>
  164. </div>
  165. <div class="form-actions">
  166. <button type="submit" name="test_connection" class="btn btn-secondary">
  167. <?php echo t('test_connection'); ?>
  168. </button>
  169. <button type="submit" name="start_import" class="btn btn-primary"
  170. onclick="return confirm('<?php echo t('wordpress_import_confirm'); ?>')">
  171. <?php echo t('start_import'); ?>
  172. </button>
  173. </div>
  174. </form>
  175. </div>
  176. <!-- Import Results -->
  177. <?php if ($importResults): ?>
  178. <div class="import-results">
  179. <h3><?php echo t('import_results'); ?></h3>
  180. <?php if ($importResults['success']): ?>
  181. <div class="success-summary">
  182. <p class="success-message"><?php echo t('wordpress_import_success'); ?></p>
  183. </div>
  184. <div class="results-details">
  185. <?php foreach ($importResults['results'] as $type => $result): ?>
  186. <div class="result-item">
  187. <h4><?php echo t('import_' . $type); ?></h4>
  188. <div class="result-stats">
  189. <span class="stat success">
  190. <?php echo t('imported'); ?>: <strong><?php echo $result['imported']; ?></strong>
  191. </span>
  192. <span class="stat warning">
  193. <?php echo t('skipped'); ?>: <strong><?php echo $result['skipped']; ?></strong>
  194. </span>
  195. </div>
  196. </div>
  197. <?php endforeach; ?>
  198. </div>
  199. <?php if (!empty($importResults['log'])): ?>
  200. <div class="import-log">
  201. <h4><?php echo t('import_log'); ?></h4>
  202. <div class="log-container">
  203. <?php foreach ($importResults['log'] as $logEntry): ?>
  204. <div class="log-entry log-<?php echo $logEntry['level']; ?>">
  205. <span class="log-time"><?php echo $logEntry['timestamp']; ?></span>
  206. <span class="log-message"><?php echo htmlspecialchars($logEntry['message']); ?></span>
  207. </div>
  208. <?php endforeach; ?>
  209. </div>
  210. </div>
  211. <?php endif; ?>
  212. <?php if (!empty($importResults['errors'])): ?>
  213. <div class="import-errors">
  214. <h4><?php echo t('import_errors'); ?></h4>
  215. <div class="error-list">
  216. <?php foreach ($importResults['errors'] as $error): ?>
  217. <div class="error-item">
  218. <?php echo htmlspecialchars($error); ?>
  219. </div>
  220. <?php endforeach; ?>
  221. </div>
  222. </div>
  223. <?php endif; ?>
  224. <?php else: ?>
  225. <div class="error-summary">
  226. <p class="error-message"><?php echo htmlspecialchars($importResults['error']); ?></p>
  227. </div>
  228. <?php endif; ?>
  229. </div>
  230. <?php endif; ?>
  231. <!-- Information Section -->
  232. <div class="info-section">
  233. <h3><?php echo t('wordpress_import_info'); ?></h3>
  234. <div class="info-content">
  235. <div class="info-item">
  236. <h4><?php echo t('what_gets_imported'); ?></h4>
  237. <ul>
  238. <li><?php echo t('import_posts_info'); ?></li>
  239. <li><?php echo t('import_categories_info'); ?></li>
  240. <li><?php echo t('import_users_info'); ?></li>
  241. <li><?php echo t('import_comments_info'); ?></li>
  242. </ul>
  243. </div>
  244. <div class="info-item">
  245. <h4><?php echo t('import_requirements'); ?></h4>
  246. <ul>
  247. <li><?php echo t('import_db_access'); ?></li>
  248. <li><?php echo t('import_wp_structure'); ?></li>
  249. <li><?php echo t('import_backup'); ?></li>
  250. </ul>
  251. </div>
  252. <div class="info-item">
  253. <h4><?php echo t('import_notes'); ?></h4>
  254. <ul>
  255. <li><?php echo t('import_existing_data'); ?></li>
  256. <li><?php echo t('import_content_processing'); ?></li>
  257. <li><?php echo t('import_user_mapping'); ?></li>
  258. </ul>
  259. </div>
  260. </div>
  261. </div>
  262. </main>
  263. </div>
  264. <script>
  265. // WordPress Import JavaScript
  266. document.addEventListener('DOMContentLoaded', function() {
  267. const form = document.querySelector('.import-form');
  268. const testBtn = document.querySelector('button[name="test_connection"]');
  269. const importBtn = document.querySelector('button[name="start_import"]');
  270. // Show loading state
  271. function setLoading(button, loading) {
  272. if (loading) {
  273. button.disabled = true;
  274. button.dataset.originalText = button.textContent;
  275. button.textContent = button.dataset.loadingText || '<?php echo t('loading'); ?>...';
  276. } else {
  277. button.disabled = false;
  278. button.textContent = button.dataset.originalText;
  279. }
  280. }
  281. // Test connection loading
  282. testBtn.addEventListener('click', function() {
  283. setLoading(this, true);
  284. this.dataset.loadingText = '<?php echo t('testing_connection'); ?>';
  285. });
  286. // Import loading
  287. importBtn.addEventListener('click', function() {
  288. setLoading(this, true);
  289. this.dataset.loadingText = '<?php echo t('importing'); ?>...';
  290. });
  291. // Reset loading on form submit
  292. form.addEventListener('submit', function() {
  293. setTimeout(() => {
  294. setLoading(testBtn, false);
  295. setLoading(importBtn, false);
  296. }, 1000);
  297. });
  298. // Auto-focus first empty field
  299. const firstEmpty = form.querySelector('input[value=""], input:not([value])');
  300. if (firstEmpty) {
  301. firstEmpty.focus();
  302. }
  303. });
  304. </script>
  305. </body>
  306. </html>