<?php

$value = $_GET['value'];
$post_type = $_GET['post_type'];
$active_folder = $_GET['active_folder'];

// order post types by name to always get correct filenames
$post_types = explode(',', $post_type);
sort($post_types);
$pt = implode(',', $post_types);

$path = dirname(__FILE__);


// Check uploads path if Sage
if (strpos($path, '/web/app/') !== false) {
    $path = explode('/web/app/', $path);
    $path = $path[0].'/web/app/uploads/webien/webien-search-cache' . $active_folder;
} else {
    $path = explode('/wp-content/', $path);
    $path = $path[0].'/wp-content/uploads/webien/webien-search-cache' . $active_folder;
}

$sitename = $_SERVER['SERVER_NAME'];
$filename = base64_encode($value.'___'.$pt.'___'.$sitename);

// Base64 can include slashes which will mess up paths..
// We replace them here and also when we try to find the file
$filename = str_replace('/', '-', $filename);

$filename = $path . $filename . '.json';


header('Content-Type: application/json'); // this is needed for browser parsing

// Check if cached json file exists
//clearstatcache(); // Clears file exists cache
if (file_exists($filename)) {
    readfile($filename); // use readfile for speed
} else {
    echo json_encode([
        'success'=>true,
        'data'=> [
            'word'=>$value,
            'cache' => 'no-match',
            'posts'=>[],
            'post_types'=>[]
        ]
    ]);
}

die();


?>