<?php
/**
 * NewzQuest Android App - Smart Hostinger Installer v3.0
 *
 * SAFE: Deploys ONLY to public_html/android/ subfolder.
 * WordPress files in public_html/ root are NEVER touched.
 */

header('Content-Type: text/html; charset=utf-8');

// ── Resolve the true public_html root ────────────────────
$here = __DIR__;
// Handle Hostinger's nested public_html/public_html case
if (basename($here) === 'public_html' && basename(dirname($here)) === 'public_html') {
    $root = dirname($here);
} else {
    $root = $here;
}

// App deploys to /android/ ONLY — WordPress root is never touched
$targetAndroid = $root . '/android';

// Find app_package.zip
$zipFile = file_exists($here . '/app_package.zip')
    ? $here . '/app_package.zip'
    : dirname($here) . '/app_package.zip';

?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>NewzQuest Android App Installer</title>
<style>
  body { font-family:system-ui,sans-serif; background:#0F172A; color:#FFF; padding:30px; line-height:1.6; }
  .card { max-width:640px; margin:0 auto; background:#1E293B; border-radius:16px; padding:24px; box-shadow:0 10px 30px rgba(0,0,0,.3); border:1px solid rgba(255,255,255,.08); }
  h1 { color:#D90429; margin-top:0; }
  .log { background:#090D16; padding:14px; border-radius:10px; font-family:monospace; font-size:13px; color:#38BDF8; margin:16px 0; max-height:300px; overflow-y:auto; border:1px solid #334155; }
  .btn { display:inline-block; background:#D90429; color:#FFF; padding:12px 20px; border-radius:8px; font-weight:bold; text-decoration:none; margin:6px 4px 0 0; }
  .btn-sec { background:#334155; }
  .badge { background:#166534; color:#4ADE80; padding:4px 10px; border-radius:6px; font-weight:bold; font-size:14px; }
  .warn { background:#7C2D12; color:#FCA5A5; padding:10px 14px; border-radius:8px; font-size:.88rem; margin-bottom:14px; }
</style>
</head>
<body>
<div class="card">
  <h1>📱 NewzQuest Android App — Installer v3.0</h1>
  <div class="warn">
    ⚠️ <strong>SAFE MODE:</strong> Files are deployed ONLY to <code>/android/</code> subfolder.
    Your WordPress site at <code>newzquest.in</code> will <strong>not</strong> be touched.
  </div>
  <div class="log">
<?php

if (!file_exists($zipFile)) {
    echo '<span style="color:#F87171;">ERROR: app_package.zip not found!</span><br>';
    echo 'Upload app_package.zip alongside this deploy.php file.</div></div></body></html>';
    exit;
}

$zip = new ZipArchive();
if ($zip->open($zipFile) !== TRUE) {
    echo '<span style="color:#F87171;">ERROR: Cannot open app_package.zip</span>';
    echo '</div></div></body></html>';
    exit;
}

// Create /android/ folder if it doesn't exist
if (!file_exists($targetAndroid)) {
    mkdir($targetAndroid, 0755, true);
    echo '[OK] Created folder: /android/<br>';
}

$deployed = 0;
$failed   = 0;

for ($i = 0; $i < $zip->numFiles; $i++) {
    $stat     = $zip->statIndex($i);
    $filename = $stat['name'];

    // Skip directory entries
    if (substr($filename, -1) === '/') continue;

    $content  = $zip->getFromIndex($i);
    $destPath = $targetAndroid . '/' . $filename;
    $destDir  = dirname($destPath);

    if (!file_exists($destDir)) {
        mkdir($destDir, 0755, true);
    }

    // ✅ ONLY write to /android/ subfolder — NEVER to root
    if (file_put_contents($destPath, $content) !== false) {
        echo '[android/' . htmlspecialchars($filename) . '] deployed<br>';
        $deployed++;
    } else {
        echo '<span style="color:#F87171;">[FAIL] ' . htmlspecialchars($filename) . '</span><br>';
        $failed++;
    }
}

$zip->close();

echo '<br><span style="color:#4ADE80;font-weight:bold;">[SUCCESS] ' . $deployed . ' files → /android/ | '
   . $failed . ' failed | WordPress root untouched ✓</span><br>';

// Remove zip after extraction
@unlink($zipFile);
echo '[CLEANUP] app_package.zip removed<br>';

?>
  </div>

  <span class="badge">✅ Deployment Complete</span>

  <h3 style="margin:20px 0 8px;">Live Links</h3>
  <a href="https://newzquest.in/android/" target="_blank" class="btn">📱 Open Android App</a>
  <a href="https://newzquest.in/" target="_blank" class="btn btn-sec">🌐 Main Site (unchanged)</a>
  <a href="https://www.pwabuilder.com/reportcard?site=https://newzquest.in/android/" target="_blank" class="btn btn-sec">⚡ PWABuilder Audit</a>

  <p style="margin-top:20px;font-size:.8rem;color:#64748B;">
    This installer has completed. You may delete deploy.php from Hostinger file manager.
  </p>
</div>
</body>
</html>
<?php
// Self-delete
@unlink(__FILE__);
