<?php
// twitter.php (se abre dentro del iframe)
include 'concha.php';

$tw = isset($twitter) ? trim($twitter) : '';
if ($tw === '') return;

// Si viene como "sanmaplaneta" (sin URL), lo convertimos a URL
if (!preg_match('~^https?://~i', $tw)) {
    $tw = 'https://x.com/' . ltrim($tw, '@');
}

// Normalizar twitter.com -> x.com
$tw = preg_replace('~^https?://(www\.)?twitter\.com/~i', 'https://x.com/', $tw);

// Extraer usuario de la URL
$user = '';
if (preg_match('~https?://(www\.)?x\.com/([A-Za-z0-9_]{1,15})~i', $tw, $m)) {
    $user = $m[2];
}
if ($user === '') return;

$profileUrl = 'https://x.com/' . $user;

// Sin html/body, pero reseteamos márgenes dentro del iframe
echo '<style>html,body{margin:0;padding:0;}</style>';

echo '<div style="padding:0 10px;">';
echo '<a class="twitter-timeline"
         data-dnt="true"
         data-theme="light"
         data-chrome="noheader nofooter noborders transparent"
         data-height="630"
         href="' . htmlspecialchars($profileUrl, ENT_QUOTES, 'UTF-8') . '">
      </a>';
echo '</div>';
?>
<script>
(function(){
  if (window.twttr && twttr.widgets && typeof twttr.widgets.load === "function") {
    twttr.widgets.load();
    return;
  }
  if (document.getElementById("twitter-wjs")) return;

  var s = document.createElement("script");
  s.id = "twitter-wjs";
  s.async = true;
  s.src = "https://platform.twitter.com/widgets.js";
  s.charset = "utf-8";
  (document.head || document.body).appendChild(s);
})();
</script>