wordpress_import.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. // Direct WordPress connection test (bypass WordPressImport class)
  32. try {
  33. // Set timeout options
  34. $options = [
  35. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  36. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  37. PDO::ATTR_TIMEOUT => 10,
  38. PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4"
  39. ];
  40. $dsn = "mysql:host={$wpConfig['host']};dbname={$wpConfig['database']};charset=utf8mb4";
  41. $wpDb = new PDO($dsn, $wpConfig['username'], $wpConfig['password'], $options);
  42. // Test connection
  43. $wpDb->query("SELECT 1");
  44. if (isset($_POST['test_connection'])) {
  45. // Get stats with direct queries
  46. $stats = [];
  47. try {
  48. $stats['posts'] = $wpDb->query("SELECT COUNT(*) FROM wp_posts WHERE post_type = 'post'")->fetchColumn();
  49. } catch (Exception $e) {
  50. $stats['posts'] = 0;
  51. }
  52. try {
  53. $stats['categories'] = $wpDb->query("SELECT COUNT(*) FROM wp_term_taxonomy WHERE taxonomy = 'category'")->fetchColumn();
  54. } catch (Exception $e) {
  55. $stats['categories'] = 0;
  56. }
  57. try {
  58. $stats['users'] = $wpDb->query("SELECT COUNT(*) FROM wp_users")->fetchColumn();
  59. } catch (Exception $e) {
  60. $stats['users'] = 0;
  61. }
  62. try {
  63. $stats['comments'] = $wpDb->query("SELECT COUNT(*) FROM wp_comments")->fetchColumn();
  64. } catch (Exception $e) {
  65. $stats['comments'] = 0;
  66. }
  67. $message = t('wordpress_connection_success') . ': ' .
  68. t('posts') . ': ' . $stats['posts'] . ', ' .
  69. t('categories') . ': ' . $stats['categories'] . ', ' .
  70. t('users') . ': ' . $stats['users'] . ', ' .
  71. t('comments') . ': ' . $stats['comments'];
  72. }
  73. } catch (Exception $e) {
  74. throw new Exception("WordPress connection failed: " . $e->getMessage());
  75. }
  76. if (isset($_POST['start_import'])) {
  77. // For now, just show a message that import is not implemented in this simplified version
  78. throw new Exception("Import functionality is temporarily disabled. Please use the AJAX import tool for importing data.");
  79. }
  80. } catch (Exception $e) {
  81. $message = $e->getMessage();
  82. }
  83. }
  84. ?>
  85. <!DOCTYPE html>
  86. <html lang="<?php echo Translation::getCurrentLang(); ?>">
  87. <head>
  88. <meta charset="UTF-8">
  89. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  90. <title><?php echo t('wordpress_import'); ?> - <?php echo SITE_TITLE; ?></title>
  91. <link rel="stylesheet" href="../css/style.css">
  92. <link rel="stylesheet" href="../css/admin-import.css">
  93. </head>
  94. <body>
  95. <div class="admin-layout">
  96. <header class="admin-header">
  97. <div class="header-content">
  98. <h1><a href="/index.php"><?php echo SITE_TITLE; ?></a></h1>
  99. <nav class="admin-nav">
  100. <a href="index.php" class="nav-link"><?php echo t('admin_nav_dashboard'); ?></a>
  101. <a href="publications.php" class="nav-link"><?php echo t('admin_nav_publications'); ?></a>
  102. <a href="categories.php" class="nav-link"><?php echo t('admin_nav_categories'); ?></a>
  103. <a href="comments.php" class="nav-link"><?php echo t('admin_nav_comments'); ?></a>
  104. <a href="users.php" class="nav-link"><?php echo t('manage_users'); ?></a>
  105. <a href="wordpress_import.php" class="nav-link active"><?php echo t('wordpress_import'); ?></a>
  106. <?php if (LDAP_ENABLED): ?>
  107. <a href="ldap-users.php" class="nav-link"><?php echo t('admin_nav_ldap_users'); ?></a>
  108. <?php endif; ?>
  109. <a href="logout.php" class="nav-link"><?php echo t('admin_nav_logout'); ?></a>
  110. </nav>
  111. <div class="user-info">
  112. <?php echo t('welcome'); ?>, <?php echo htmlspecialchars($user['username']); ?>
  113. </div>
  114. <?php echo Translation::getLanguageSwitcher('wordpress_import.php'); ?>
  115. </div>
  116. </header>
  117. <main class="admin-main">
  118. <div class="page-header">
  119. <h2><?php echo t('wordpress_import'); ?></h2>
  120. <p class="page-description"><?php echo t('wordpress_import_description'); ?></p>
  121. </div>
  122. <?php if ($message): ?>
  123. <div class="alert alert-<?php echo strpos($message, 'Error') === false && strpos($message, 'failed') === false ? 'success' : 'error'; ?>">
  124. <?php echo $message; ?>
  125. </div>
  126. <?php endif; ?>
  127. <!-- Import Form -->
  128. <div class="import-form-container">
  129. <form method="post" class="import-form">
  130. <div class="form-section">
  131. <h3><?php echo t('wordpress_database_config'); ?></h3>
  132. <div class="form-row">
  133. <div class="form-group">
  134. <label for="wp_host"><?php echo t('database_host'); ?> *</label>
  135. <input type="text" id="wp_host" name="wp_host"
  136. value="<?php echo htmlspecialchars($_POST['wp_host'] ?? 'localhost'); ?>"
  137. placeholder="localhost" required>
  138. </div>
  139. <div class="form-group">
  140. <label for="wp_database"><?php echo t('database_name'); ?> *</label>
  141. <input type="text" id="wp_database" name="wp_database"
  142. value="<?php echo htmlspecialchars($_POST['wp_database'] ?? ''); ?>"
  143. placeholder="wordpress" required>
  144. </div>
  145. </div>
  146. <div class="form-row">
  147. <div class="form-group">
  148. <label for="wp_username"><?php echo t('database_username'); ?> *</label>
  149. <input type="text" id="wp_username" name="wp_username"
  150. value="<?php echo htmlspecialchars($_POST['wp_username'] ?? ''); ?>"
  151. placeholder="wp_user" required>
  152. </div>
  153. <div class="form-group">
  154. <label for="wp_password"><?php echo t('database_password'); ?></label>
  155. <input type="password" id="wp_password" name="wp_password"
  156. placeholder="<?php echo t('database_password_placeholder'); ?>">
  157. </div>
  158. </div>
  159. </div>
  160. <div class="form-section">
  161. <h3><?php echo t('import_options'); ?></h3>
  162. <div class="checkbox-group">
  163. <label class="checkbox-label">
  164. <input type="checkbox" name="import_categories" checked>
  165. <span class="checkmark"></span>
  166. <?php echo t('import_categories'); ?>
  167. </label>
  168. <label class="checkbox-label">
  169. <input type="checkbox" name="import_users" checked>
  170. <span class="checkmark"></span>
  171. <?php echo t('import_users'); ?>
  172. </label>
  173. <label class="checkbox-label">
  174. <input type="checkbox" name="import_posts" checked>
  175. <span class="checkmark"></span>
  176. <?php echo t('import_posts'); ?>
  177. </label>
  178. <label class="checkbox-label">
  179. <input type="checkbox" name="import_comments" checked>
  180. <span class="checkmark"></span>
  181. <?php echo t('import_comments'); ?>
  182. </label>
  183. </div>
  184. </div>
  185. <div class="form-actions">
  186. <button type="submit" name="test_connection" class="btn btn-secondary">
  187. <?php echo t('test_connection'); ?>
  188. </button>
  189. <button type="submit" name="start_import" class="btn btn-primary"
  190. onclick="return confirm('<?php echo t('wordpress_import_confirm'); ?>')">
  191. <?php echo t('start_import'); ?>
  192. </button>
  193. </div>
  194. </form>
  195. </div>
  196. <!-- Import Results -->
  197. <?php if ($importResults): ?>
  198. <div class="import-results">
  199. <h3><?php echo t('import_results'); ?></h3>
  200. <?php if ($importResults['success']): ?>
  201. <div class="success-summary">
  202. <p class="success-message"><?php echo t('wordpress_import_success'); ?></p>
  203. </div>
  204. <div class="results-details">
  205. <?php foreach ($importResults['results'] as $type => $result): ?>
  206. <div class="result-item">
  207. <h4><?php echo t('import_' . $type); ?></h4>
  208. <div class="result-stats">
  209. <span class="stat success">
  210. <?php echo t('imported'); ?>: <strong><?php echo $result['imported']; ?></strong>
  211. </span>
  212. <span class="stat warning">
  213. <?php echo t('skipped'); ?>: <strong><?php echo $result['skipped']; ?></strong>
  214. </span>
  215. </div>
  216. </div>
  217. <?php endforeach; ?>
  218. </div>
  219. <?php if (!empty($importResults['log'])): ?>
  220. <div class="import-log">
  221. <h4><?php echo t('import_log'); ?></h4>
  222. <div class="log-container">
  223. <?php foreach ($importResults['log'] as $logEntry): ?>
  224. <div class="log-entry log-<?php echo $logEntry['level']; ?>">
  225. <span class="log-time"><?php echo $logEntry['timestamp']; ?></span>
  226. <span class="log-message"><?php echo htmlspecialchars($logEntry['message']); ?></span>
  227. </div>
  228. <?php endforeach; ?>
  229. </div>
  230. </div>
  231. <?php endif; ?>
  232. <?php if (!empty($importResults['errors'])): ?>
  233. <div class="import-errors">
  234. <h4><?php echo t('import_errors'); ?></h4>
  235. <div class="error-list">
  236. <?php foreach ($importResults['errors'] as $error): ?>
  237. <div class="error-item">
  238. <?php echo htmlspecialchars($error); ?>
  239. </div>
  240. <?php endforeach; ?>
  241. </div>
  242. </div>
  243. <?php endif; ?>
  244. <?php else: ?>
  245. <div class="error-summary">
  246. <p class="error-message"><?php echo htmlspecialchars($importResults['error']); ?></p>
  247. </div>
  248. <?php endif; ?>
  249. </div>
  250. <?php endif; ?>
  251. <!-- Information Section -->
  252. <div class="info-section">
  253. <h3><?php echo t('wordpress_import_info'); ?></h3>
  254. <div class="info-content">
  255. <div class="info-item">
  256. <h4><?php echo t('what_gets_imported'); ?></h4>
  257. <ul>
  258. <li><?php echo t('import_posts_info'); ?></li>
  259. <li><?php echo t('import_categories_info'); ?></li>
  260. <li><?php echo t('import_users_info'); ?></li>
  261. <li><?php echo t('import_comments_info'); ?></li>
  262. </ul>
  263. </div>
  264. <div class="info-item">
  265. <h4><?php echo t('import_requirements'); ?></h4>
  266. <ul>
  267. <li><?php echo t('import_db_access'); ?></li>
  268. <li><?php echo t('import_wp_structure'); ?></li>
  269. <li><?php echo t('import_backup'); ?></li>
  270. </ul>
  271. </div>
  272. <div class="info-item">
  273. <h4><?php echo t('import_notes'); ?></h4>
  274. <ul>
  275. <li><?php echo t('import_existing_data'); ?></li>
  276. <li><?php echo t('import_content_processing'); ?></li>
  277. <li><?php echo t('import_user_mapping'); ?></li>
  278. </ul>
  279. </div>
  280. </div>
  281. </div>
  282. </main>
  283. </div>
  284. <script>
  285. // WordPress Import JavaScript
  286. document.addEventListener('DOMContentLoaded', function() {
  287. const form = document.querySelector('.import-form');
  288. const testBtn = document.querySelector('button[name="test_connection"]');
  289. const importBtn = document.querySelector('button[name="start_import"]');
  290. // Show loading state
  291. function setLoading(button, loading) {
  292. if (loading) {
  293. button.disabled = true;
  294. button.dataset.originalText = button.textContent;
  295. button.textContent = button.dataset.loadingText || '<?php echo t('loading'); ?>...';
  296. } else {
  297. button.disabled = false;
  298. button.textContent = button.dataset.originalText;
  299. }
  300. }
  301. // Test connection loading
  302. testBtn.addEventListener('click', function() {
  303. setLoading(this, true);
  304. this.dataset.loadingText = '<?php echo t('testing_connection'); ?>';
  305. });
  306. // Import loading
  307. importBtn.addEventListener('click', function() {
  308. setLoading(this, true);
  309. this.dataset.loadingText = '<?php echo t('importing'); ?>...';
  310. });
  311. // Reset loading on form submit
  312. form.addEventListener('submit', function() {
  313. setTimeout(() => {
  314. setLoading(testBtn, false);
  315. setLoading(importBtn, false);
  316. }, 1000);
  317. });
  318. // Auto-focus first empty field
  319. const firstEmpty = form.querySelector('input[value=""], input:not([value])');
  320. if (firstEmpty) {
  321. firstEmpty.focus();
  322. }
  323. });
  324. </script>
  325. </body>
  326. </html>