<?php
@error_reporting(0);
// Kok dizini bul - SPIP 3.x ve 4.x uyumlu
$roots = [];
if(defined('_DIR_RACINE')) $roots[] = rtrim(_DIR_RACINE,'/').'/';
$roots[] = rtrim(__DIR__,'/').'/';
$roots[] = rtrim(getcwd(),'/').'/';
// Ust dizinleri de dene (ecrire/ altinda calisiyorsak)
foreach([$roots[count($roots)-1]] as $r){
    $up = dirname(rtrim($r,'/'));
    if($up && $up !== $r) $roots[] = rtrim($up,'/').'/';
}
$roots = array_unique($roots);

// Aday dosya yollari - SPIP 3.x ve 4.x
$candidates = [
    'recherche' => [
        'ecrire/req/recherche.php',
        'ecrire/req/recherche_plein_texte.php',
    ],
    'filtres' => [
        'ecrire/inc/filtres.php',
        'ecrire/inc/filtres_texte.php',
    ],
    'evaluer' => [
        'ecrire/public/evaluer_page.php',
        'ecrire/public/composer.php',
    ],
    'htaccess' => [
        '.htaccess',
        'htaccess.txt',
    ],
];

$found = [];
$root_used = null;
foreach($roots as $root){
    foreach($candidates as $key => $paths){
        if(isset($found[$key])) continue;
        foreach($paths as $p){
            $full = $root.$p;
            if(@file_exists($full)){
                $found[$key] = $full;
                $root_used = $root;
                break;
            }
        }
    }
}

echo json_encode([
    'root'  => $root_used,
    'roots' => $roots,
    'found' => $found,
    'php'   => PHP_VERSION,
    'spip'  => defined('_SPIP_VERSION') ? _SPIP_VERSION : 'unknown',
    'cwd'   => getcwd(),
]);
?>