<?php
include('concha.php');

if (!function_exists('mod_h')) {
    function mod_h($s) {
        return htmlspecialchars($s, ENT_QUOTES, 'UTF-8');
    }
}

if (!function_exists('mod_norm_url')) {
    function mod_norm_url($u) {
        $u = trim((string)$u);
        if ($u === '') return '';
        if (!preg_match('~^https?://~i', $u)) {
            $u = 'https://' . ltrim($u, '/');
        }
        return $u;
    }
}

if (!function_exists('procesar_shortcodes_modulo')) {
    function procesar_shortcodes_modulo($texto) {
        $texto = preg_replace_callback(
            '~\[\s*img\s*\](.*?)\[\s*/\s*img\s*\]~is',
            function($m) {
                $url = trim($m[1]);
                if ($url === '') return '';

                $url = mod_norm_url($url);

                return '<div style="margin:18px 0;">'
                     . '<a href="' . mod_h($url) . '" target="_blank">'
                     . '<img src="' . mod_h($url) . '" class="img-fluid" style="max-width:100%;height:auto;display:block;" alt="">'
                     . '</a>'
                     . '</div>';
            },
            $texto
        );

        return $texto;
    }
}

$sql = "SELECT * FROM modulos";
$consulta = $conn->query($sql);

while ($row = $consulta->fetch_assoc())
{
    $ID_b = $row["ID"];
    $nombre_m = $row["titulo"];
    $texto_m = $row["texto"];

    $texto_m = str_replace('http://', 'https://', $texto_m);
    $texto_m = str_replace('www.', '', $texto_m);

    $dom = $domold != '' ? $domold : '';

    $texto_m = str_replace('src="https://', 'src="https://old.', $texto_m);

    $texto_m = procesar_shortcodes_modulo($texto_m);
?>
<article style="margin-bottom:20px;">
    <div class="base-box2">

        <div class="widget-head">
            <h3 <?php if ($estiloweb <= 3){echo 'style="padding: 1px 5px 1px 5px; background-color:'; echo $d_color_fondo_menu; echo ';font-family:'; echo $style_titulo; echo ';font-size:25px;color:#ffffff;"'; } else {echo 'class="widget-title"';} ?>>
                <span><?php echo $nombre_m ?></span>
            </h3>
        </div>

        <div>
            <?php
            // $texto_m = str_replace('https:', '',  $texto_m);
            // $texto_m = str_replace('http:', '',  $texto_m);
            echo $texto_m;
            ?>
        </div>

    </div>
</article>
<?php
}
$consulta->free_result();
?>