<?php

    if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: POST, GET, DELETE, PUT, PATCH, OPTIONS');
        header('Access-Control-Allow-Headers: token, Content-Type');
        header('Access-Control-Max-Age: 1728000');
        header('Content-Length: 0');
        header('Content-Type: text/plain');
        die();
    }

    header("Pragma: public");
    header("Cache-Control: private");
    header('Access-Control-Allow-Origin: *');
    header('Content-Type: application/json');

  $referencia = $_GET['referencia'];
  $exten = $_GET['extension'];

  date_default_timezone_set('Europa/Paris');
//  echo "buscando ficheros desde $fecha con extension = $exten \r\n";
  $i = 0;
  $result = array();
  $handle =  opendir(".");
     while ($file = readdir($handle))
     {
          if (($file != '.') && ($file != '..'))
          {
               $file = "./".$file;
               if (!is_dir($file))
               {
                   if (strpos($file, "$exten",1) && strpos($file, "$referencia", 1) && !strpos($file, " ") ) {
                       $result[] = $file;
                       $i = $i + 1;
                   }
               } 
          }
     }
  closedir($handle);
  echo "[";
  $j = 0;
  foreach($result as $r)
  {
      echo '{"n": "' . $r . '"}';
      $j = $j +1;
      if ($j < $i) {
         echo ',';
      }
  }
  echo "]";
?>
