wordpress_import.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. // Simplified WordPress import interface - bypass problematic components
  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. // Start session for basic functionality
  8. if (session_status() === PHP_SESSION_NONE) {
  9. session_start();
  10. }
  11. require_once '../includes/config.php';
  12. require_once '../includes/database.php';
  13. // Simple auth check - just check if user is logged in without complex auth system
  14. $isAdmin = false;
  15. if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true && isset($_SESSION['role']) && $_SESSION['role'] === 'admin') {
  16. $isAdmin = true;
  17. }
  18. // Redirect if not admin
  19. if (!$isAdmin) {
  20. header('Location: login.php');
  21. exit;
  22. }
  23. $message = '';
  24. $importResults = null;
  25. $connectionTest = null;
  26. // Handle form submission with simplified processing
  27. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  28. try {
  29. // Get WordPress database configuration
  30. $wpConfig = [
  31. 'host' => trim($_POST['wp_host'] ?? ''),
  32. 'database' => trim($_POST['wp_database'] ?? ''),
  33. 'username' => trim($_POST['wp_username'] ?? ''),
  34. 'password' => $_POST['wp_password'] ?? ''
  35. ];
  36. // Validate required fields
  37. if (empty($wpConfig['host']) || empty($wpConfig['database']) || empty($wpConfig['username'])) {
  38. throw new Exception(t('wordpress_import_config_required'));
  39. }
  40. // Direct WordPress connection test (bypass WordPressImport class)
  41. try {
  42. // Set timeout options
  43. $options = [
  44. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  45. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  46. PDO::ATTR_TIMEOUT => 10,
  47. PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4"
  48. ];
  49. $dsn = "mysql:host={$wpConfig['host']};dbname={$wpConfig['database']};charset=utf8mb4";
  50. $wpDb = new PDO($dsn, $wpConfig['username'], $wpConfig['password'], $options);
  51. // Test connection
  52. $wpDb->query("SELECT 1");
  53. if (isset($_POST['test_connection'])) {
  54. // Get stats with direct queries
  55. $stats = [];
  56. try {
  57. $stats['posts'] = $wpDb->query("SELECT COUNT(*) FROM wp_posts WHERE post_type = 'post'")->fetchColumn();
  58. } catch (Exception $e) {
  59. $stats['posts'] = 0;
  60. }
  61. try {
  62. $stats['categories'] = $wpDb->query("SELECT COUNT(*) FROM wp_term_taxonomy WHERE taxonomy = 'category'")->fetchColumn();
  63. } catch (Exception $e) {
  64. $stats['categories'] = 0;
  65. }
  66. try {
  67. $stats['users'] = $wpDb->query("SELECT COUNT(*) FROM wp_users")->fetchColumn();
  68. } catch (Exception $e) {
  69. $stats['users'] = 0;
  70. }
  71. try {
  72. $stats['comments'] = $wpDb->query("SELECT COUNT(*) FROM wp_comments")->fetchColumn();
  73. } catch (Exception $e) {
  74. $stats['comments'] = 0;
  75. }
  76. $message = t('wordpress_connection_success') . ': ' .
  77. t('posts') . ': ' . $stats['posts'] . ', ' .
  78. t('categories') . ': ' . $stats['categories'] . ', ' .
  79. t('users') . ': ' . $stats['users'] . ', ' .
  80. t('comments') . ': ' . $stats['comments'];
  81. }
  82. } catch (Exception $e) {
  83. throw new Exception("WordPress connection failed: " . $e->getMessage());
  84. }
  85. if (isset($_POST['start_import'])) {
  86. // For now, just show a message that import is not implemented in this simplified version
  87. throw new Exception("Import functionality is temporarily disabled. Please use the AJAX import tool for importing data.");
  88. }
  89. } catch (Exception $e) {
  90. $message = $e->getMessage();
  91. }
  92. }
  93. ?>
  94. <!DOCTYPE html>
  95. <html lang="en">
  96. <head>
  97. <meta charset="UTF-8">
  98. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  99. <title>WordPress Import - <?php echo SITE_TITLE; ?></title>
  100. <link rel="stylesheet" href="../css/style.css">
  101. <link rel="stylesheet" href="../css/admin-import.css">
  102. </head>
  103. <body>
  104. <div class="admin-layout">
  105. <header class="admin-header">
  106. <div class="header-content">
  107. <h1><a href="/index.php"><?php echo SITE_TITLE; ?></a></h1>
  108. <nav class="admin-nav">
  109. <a href="index.php" class="nav-link">Dashboard</a>
  110. <a href="publications.php" class="nav-link">Publications</a>
  111. <a href="categories.php" class="nav-link">Categories</a>
  112. <a href="comments.php" class="nav-link">Comments</a>
  113. <a href="users.php" class="nav-link">Users</a>
  114. <a href="wordpress_import.php" class="nav-link active">WordPress Import</a>
  115. <?php if (LDAP_ENABLED): ?>
  116. <a href="ldap-users.php" class="nav-link">LDAP Users</a>
  117. <?php endif; ?>
  118. <a href="logout.php" class="nav-link">Logout</a>
  119. </nav>
  120. <div class="user-info">
  121. Welcome, <?php echo htmlspecialchars($_SESSION['username'] ?? 'Admin'); ?>
  122. </div>
  123. </div>
  124. </header>
  125. <main class="admin-main">
  126. <div class="page-header">
  127. <h2>WordPress Import</h2>
  128. <p class="page-description">Import WordPress data into your system.</p>
  129. <p class="page-description">Import posts, categories, users, and comments from a WordPress database to your publication system.</p>
  130. </div>
  131. <?php if ($message): ?>
  132. <div class="alert alert-<?php echo strpos($message, 'Error') === false && strpos($message, 'failed') === false ? 'success' : 'error'; ?>">
  133. <?php echo $message; ?>
  134. </div>
  135. <?php endif; ?>
  136. <!-- Import Form -->
  137. <div class="import-form-container">
  138. <form method="post" class="import-form">
  139. <div class="form-section">
  140. <h3>WordPress Database Configuration</h3>
  141. <div class="form-row">
  142. <div class="form-group">
  143. <label for="wp_host">Database Host *</label>
  144. <input type="text" id="wp_host" name="wp_host"
  145. value="<?php echo htmlspecialchars($_POST['wp_host'] ?? 'localhost'); ?>"
  146. placeholder="localhost" required>
  147. </div>
  148. <div class="form-group">
  149. <label for="wp_database">Database Name *</label>
  150. <input type="text" id="wp_database" name="wp_database"
  151. value="<?php echo htmlspecialchars($_POST['wp_database'] ?? ''); ?>"
  152. placeholder="wordpress_database" required>
  153. </div>
  154. </div>
  155. <div class="form-row">
  156. <div class="form-group">
  157. <label for="wp_username">Database Username *</label>
  158. <input type="text" id="wp_username" name="wp_username"
  159. value="<?php echo htmlspecialchars($_POST['wp_username'] ?? ''); ?>"
  160. placeholder="wp_username" required>
  161. </div>
  162. <div class="form-group">
  163. <label for="wp_password">Database Password</label>
  164. <input type="password" id="wp_password" name="wp_password"
  165. value="<?php echo htmlspecialchars($_POST['wp_password'] ?? ''); ?>"
  166. placeholder="Leave empty if no password">
  167. </div>
  168. </div>
  169. </div>
  170. <div class="form-section">
  171. <h3>Import Options</h3>
  172. <div class="form-row">
  173. <div class="form-group">
  174. <label class="checkbox-label">
  175. <input type="checkbox" name="import_categories" checked>
  176. Import Categories
  177. </label>
  178. </div>
  179. <div class="form-group">
  180. <label class="checkbox-label">
  181. <input type="checkbox" name="import_users" checked>
  182. Import Users
  183. </label>
  184. </div>
  185. </div>
  186. <div class="form-row">
  187. <div class="form-group">
  188. <label class="checkbox-label">
  189. <input type="checkbox" name="import_posts" checked>
  190. Import Posts
  191. </label>
  192. </div>
  193. <div class="form-group">
  194. <label class="checkbox-label">
  195. <input type="checkbox" name="import_comments" checked>
  196. Import Comments
  197. </label>
  198. </div>
  199. </div>
  200. </div>
  201. <div class="form-actions">
  202. <button type="submit" name="test_connection" class="btn btn-primary">
  203. Test Connection
  204. </button>
  205. <button type="submit" name="start_import" class="btn btn-success"
  206. onclick="return confirm('Are you sure you want to start the import? This will import data from WordPress into your system.')">
  207. Start Import
  208. </button>
  209. </div>
  210. </form>
  211. </div>
  212. <!-- Import Results -->
  213. <?php if ($importResults): ?>
  214. <div class="import-results">
  215. <h3>Import Results</h3>
  216. <?php if ($importResults['success']): ?>
  217. <div class="success-summary">
  218. <p class="success-message">WordPress import completed successfully!</p>
  219. </div>
  220. <div class="results-details">
  221. <?php foreach ($importResults['results'] as $type => $result): ?>
  222. <div class="result-item">
  223. <h4><?php echo ucfirst($type); ?></h4>
  224. <div class="result-stats">
  225. <span class="stat success">
  226. Imported: <strong><?php echo $result['imported']; ?></strong>
  227. </span>
  228. <span class="stat warning">
  229. Skipped: <strong><?php echo $result['skipped']; ?></strong>
  230. </span>
  231. </div>
  232. </div>
  233. <?php endforeach; ?>
  234. </div>
  235. <?php if (!empty($importResults['log'])): ?>
  236. <div class="import-log">
  237. <h4>Import Log</h4>
  238. <div class="log-container">
  239. <?php foreach ($importResults['log'] as $logEntry): ?>
  240. <div class="log-entry log-<?php echo $logEntry['level']; ?>">
  241. <span class="log-time"><?php echo $logEntry['timestamp']; ?></span>
  242. <span class="log-message"><?php echo htmlspecialchars($logEntry['message']); ?></span>
  243. </div>
  244. <?php endforeach; ?>
  245. </div>
  246. </div>
  247. <?php endif; ?>
  248. <?php if (!empty($importResults['errors'])): ?>
  249. <div class="import-errors">
  250. <h4>Import Errors</h4>
  251. <div class="error-list">
  252. <?php foreach ($importResults['errors'] as $error): ?>
  253. <div class="error-item">
  254. <?php echo htmlspecialchars($error); ?>
  255. </div>
  256. <?php endforeach; ?>
  257. </div>
  258. </div>
  259. <?php endif; ?>
  260. <?php else: ?>
  261. <div class="error-summary">
  262. <p class="error-message"><?php echo htmlspecialchars($importResults['error']); ?></p>
  263. </div>
  264. <?php endif; ?>
  265. </div>
  266. <?php endif; ?>
  267. <!-- Information Section -->
  268. <div class="info-section">
  269. <h3><?php echo t('wordpress_import_info'); ?></h3>
  270. <div class="info-content">
  271. <div class="info-item">
  272. <h4><?php echo t('what_gets_imported'); ?></h4>
  273. <ul>
  274. <li><?php echo t('import_posts_info'); ?></li>
  275. <li><?php echo t('import_categories_info'); ?></li>
  276. <li><?php echo t('import_users_info'); ?></li>
  277. <li><?php echo t('import_comments_info'); ?></li>
  278. </ul>
  279. </div>
  280. <div class="info-item">
  281. <h4><?php echo t('import_requirements'); ?></h4>
  282. <ul>
  283. <li><?php echo t('import_db_access'); ?></li>
  284. <li><?php echo t('import_wp_structure'); ?></li>
  285. <li><?php echo t('import_backup'); ?></li>
  286. </ul>
  287. </div>
  288. <div class="info-item">
  289. <h4><?php echo t('import_notes'); ?></h4>
  290. <ul>
  291. <li><?php echo t('import_existing_data'); ?></li>
  292. <li><?php echo t('import_content_processing'); ?></li>
  293. <li><?php echo t('import_user_mapping'); ?></li>
  294. </ul>
  295. </div>
  296. </div>
  297. </div>
  298. </main>
  299. </div>
  300. <script>
  301. // WordPress Import JavaScript
  302. document.addEventListener('DOMContentLoaded', function() {
  303. const form = document.querySelector('.import-form');
  304. const testBtn = document.querySelector('button[name="test_connection"]');
  305. const importBtn = document.querySelector('button[name="start_import"]');
  306. // Show loading state
  307. function setLoading(button, loading) {
  308. if (loading) {
  309. button.disabled = true;
  310. button.dataset.originalText = button.textContent;
  311. button.textContent = button.dataset.loadingText || '<?php echo t('loading'); ?>...';
  312. } else {
  313. button.disabled = false;
  314. button.textContent = button.dataset.originalText;
  315. }
  316. }
  317. // Test connection loading
  318. testBtn.addEventListener('click', function() {
  319. setLoading(this, true);
  320. this.dataset.loadingText = '<?php echo t('testing_connection'); ?>';
  321. });
  322. // Import loading
  323. importBtn.addEventListener('click', function() {
  324. setLoading(this, true);
  325. this.dataset.loadingText = '<?php echo t('importing'); ?>...';
  326. });
  327. // Reset loading on form submit
  328. form.addEventListener('submit', function() {
  329. setTimeout(() => {
  330. setLoading(testBtn, false);
  331. setLoading(importBtn, false);
  332. }, 1000);
  333. });
  334. // Auto-focus first empty field
  335. const firstEmpty = form.querySelector('input[value=""], input:not([value])');
  336. if (firstEmpty) {
  337. firstEmpty.focus();
  338. }
  339. });
  340. </script>
  341. </body>
  342. </html>