wordpress_import.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. // WordPress Import - Based on working minimal test
  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. // Include config first to get constants
  8. require_once '../includes/config.php';
  9. // Simple auth check - just check if user is logged in
  10. if (session_status() === PHP_SESSION_NONE) {
  11. session_start();
  12. }
  13. $isAdmin = false;
  14. if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true && isset($_SESSION['role']) && $_SESSION['role'] === 'admin') {
  15. $isAdmin = true;
  16. }
  17. // Redirect if not admin
  18. if (!$isAdmin) {
  19. header('Location: login.php');
  20. exit;
  21. }
  22. $message = '';
  23. $stats = [];
  24. // Handle form submission
  25. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  26. try {
  27. require_once '../includes/config.php';
  28. require_once '../includes/database.php';
  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("WordPress database configuration is required. Please provide host, database, and username.");
  39. }
  40. // Direct WordPress connection test (exactly same as minimal test)
  41. $options = [
  42. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  43. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  44. PDO::ATTR_TIMEOUT => 10,
  45. PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4"
  46. ];
  47. $dsn = "mysql:host={$wpConfig['host']};dbname={$wpConfig['database']};charset=utf8mb4";
  48. $wpDb = new PDO($dsn, $wpConfig['username'], $wpConfig['password'], $options);
  49. // Test connection
  50. $wpDb->query("SELECT 1");
  51. // Get stats with direct queries (exactly same as minimal test)
  52. try {
  53. $stats['posts'] = $wpDb->query("SELECT COUNT(*) FROM wp_posts WHERE post_type = 'post'")->fetchColumn();
  54. } catch (Exception $e) {
  55. $stats['posts'] = 0;
  56. }
  57. try {
  58. $stats['categories'] = $wpDb->query("SELECT COUNT(*) FROM wp_term_taxonomy WHERE taxonomy = 'category'")->fetchColumn();
  59. } catch (Exception $e) {
  60. $stats['categories'] = 0;
  61. }
  62. try {
  63. $stats['users'] = $wpDb->query("SELECT COUNT(*) FROM wp_users")->fetchColumn();
  64. } catch (Exception $e) {
  65. $stats['users'] = 0;
  66. }
  67. try {
  68. $stats['comments'] = $wpDb->query("SELECT COUNT(*) FROM wp_comments")->fetchColumn();
  69. } catch (Exception $e) {
  70. $stats['comments'] = 0;
  71. }
  72. $message = "WordPress connection successful: Posts: {$stats['posts']}, Categories: {$stats['categories']}, Users: {$stats['users']}, Comments: {$stats['comments']}";
  73. } catch (Exception $e) {
  74. $message = "WordPress connection failed: " . $e->getMessage();
  75. }
  76. }
  77. ?>
  78. <!DOCTYPE html>
  79. <html lang="en">
  80. <head>
  81. <meta charset="UTF-8">
  82. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  83. <title>WordPress Import - <?php echo SITE_TITLE; ?></title>
  84. <link rel="stylesheet" href="../css/style.css">
  85. <style>
  86. .import-form { max-width: 600px; margin: 20px auto; }
  87. .form-group { margin-bottom: 15px; }
  88. label { display: block; margin-bottom: 5px; font-weight: bold; }
  89. input, button { padding: 8px; margin-bottom: 10px; width: 100%; }
  90. button { background: #007cba; color: white; border: none; cursor: pointer; }
  91. button:hover { background: #005a87; }
  92. .message { padding: 10px; margin: 10px 0; border-radius: 4px; }
  93. .success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
  94. .error { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
  95. .stats { background: #e2e3e5; padding: 10px; border-radius: 4px; margin: 10px 0; }
  96. </style>
  97. </head>
  98. <body>
  99. <div style="max-width: 800px; margin: 20px auto; padding: 20px;">
  100. <h1><a href="index.php"><?php echo SITE_TITLE; ?></a></h1>
  101. <nav>
  102. <a href="index.php">Dashboard</a> |
  103. <a href="publications.php">Publications</a> |
  104. <a href="categories.php">Categories</a> |
  105. <a href="wordpress_import.php" style="font-weight: bold;">WordPress Import</a> |
  106. <a href="logout.php">Logout</a>
  107. </nav>
  108. <h2>WordPress Import</h2>
  109. <p>Test connection to your WordPress database to import posts, categories, users, and comments.</p>
  110. <?php if ($message): ?>
  111. <div class="message <?php echo strpos($message, 'successful') !== false ? 'success' : 'error'; ?>">
  112. <?php echo $message; ?>
  113. </div>
  114. <?php endif; ?>
  115. <?php if (!empty($stats)): ?>
  116. <div class="stats">
  117. <h3>WordPress Database Statistics:</h3>
  118. <ul>
  119. <li>Posts: <?php echo $stats['posts']; ?></li>
  120. <li>Categories: <?php echo $stats['categories']; ?></li>
  121. <li>Users: <?php echo $stats['users']; ?></li>
  122. <li>Comments: <?php echo $stats['comments']; ?></li>
  123. </ul>
  124. </div>
  125. <?php endif; ?>
  126. <form method="post" class="import-form">
  127. <div class="form-group">
  128. <label for="wp_host">Database Host *</label>
  129. <input type="text" id="wp_host" name="wp_host"
  130. value="<?php echo htmlspecialchars($_POST['wp_host'] ?? 'localhost'); ?>"
  131. placeholder="localhost" required>
  132. </div>
  133. <div class="form-group">
  134. <label for="wp_database">Database Name *</label>
  135. <input type="text" id="wp_database" name="wp_database"
  136. value="<?php echo htmlspecialchars($_POST['wp_database'] ?? ''); ?>"
  137. placeholder="wordpress_database" required>
  138. </div>
  139. <div class="form-group">
  140. <label for="wp_username">Database Username *</label>
  141. <input type="text" id="wp_username" name="wp_username"
  142. value="<?php echo htmlspecialchars($_POST['wp_username'] ?? ''); ?>"
  143. placeholder="wp_username" required>
  144. </div>
  145. <div class="form-group">
  146. <label for="wp_password">Database Password</label>
  147. <input type="password" id="wp_password" name="wp_password"
  148. value="<?php echo htmlspecialchars($_POST['wp_password'] ?? ''); ?>"
  149. placeholder="Leave empty if no password">
  150. </div>
  151. <button type="submit" name="test_connection">Test Connection</button>
  152. </form>
  153. <div style="margin-top: 30px; padding: 15px; background: #f8f9fa; border-radius: 4px;">
  154. <h3>Import Options</h3>
  155. <p>For actual importing, use the AJAX import tool which provides step-by-step import functionality:</p>
  156. <a href="ajax_import_interface.html" style="display: inline-block; padding: 10px 20px; background: #28a745; color: white; text-decoration: none; border-radius: 4px;">AJAX Import Tool</a>
  157. </div>
  158. </div>
  159. </body>
  160. </html>