<?php
if(isset($_POST["enviado"])){

    
$config=file($_FILES['config']['tmp_name']);
    
    foreach(
$config AS $line){
        if(
preg_match("/^tle_url[0-9]{0,2} *= (.*)/",$line,$results)){
            
$sources[]=$results[1];
//              break;
        
}
    }
?>
<FORM action="" method="POST">
<pre>
We found these sources from your config.ini, check that you want visible by default:

<?php
    
foreach($sources as $sat_num=>$source){
?>    <label><input type="checkbox" name="sat_visible[<?php echo $sat_num?>]" value="visible"> <?php    
    
echo $source;
    
?></label>
<?php
    
}
?>

<input type="hidden" name="sources_data" value="<?php echo implode("|",$sources);?>"><input type="submit" value="Generar json" name="chequeado">
</FORM>
<?php
}elseif(isset($_POST["chequeado"])){
     
$sources=explode("|",$_POST['sources_data']);
    
// Extracción de la data
    
$sat_num=0;
    foreach(
$sources as $source_num=>$source){
        
$source_data=file($source);
        foreach(
$source_data AS $source_line){
            if(
preg_match("/^[a-zA-Z]{2,}/",$source_line)){
                
$sat_num++;
                
                if(@
in_array(trim($source_line),$satellites_names)){
                    
$satellites[$sat_num]['pre_exist']=true;
                }else{
                    
$satellites[$sat_num]['pre_exist']=false;
                    
$satellites_names[]=trim($source_line);
                }
                
                
$satellites[$sat_num]['name']=trim($source_line);
                
$satellites[$sat_num]['source']=trim($source);
                
                if(
$_POST["sat_visible"][$source_num]=="visible"){
                    
$satellites[$sat_num]['visible']="true";
                }else{
                    
$satellites[$sat_num]['visible']="false";
                }
            }
            if(
preg_match("/^1 /",$source_line)){
                
$satellites[$sat_num]['tle1']=trim($source_line);
            }
            if(
preg_match("/^2 /",$source_line)){
                
$satellites[$sat_num]['tle2']=trim($source_line);
            }
        }
        
//print_r($satellites);
        //exit();
    
}

// Creación del satellites.json
    
$data.=
'{
    "creator": "LBCoders Satellite Creator (http://lbcoders.com/stellarium)",
    "hintColor": [0, 0.5, 0.5],
    "satellites":
    {'
;

foreach(
$satellites AS $satellite){
    if(!
$satellite['pre_exist']){
        
$satellite_blocks[]=
'
        "'
.$satellite['name'].'": 
        {
            "description": "'
.ucfirst(end(array_reverse(explode(".",end(explode("/",$satellite['source'])))))).'",
            "groups": ["'
.ucfirst(end(array_reverse(explode(".",end(explode("/",$satellite['source'])))))).'"],
            "tle1": "'
.$satellite['tle1'].'",
            "tle2": "'
.$satellite['tle2'].'",
            "visible": '
.$satellite['visible'].'
        }'
;
    }
}
    
$data.=implode(",",$satellite_blocks);
    
$data.=
'
    },
    "shortName": "satellite orbital data"
}'
;
    
header ("Content-Disposition: attachment; filename=satellites.json\n\n");
    
header ("Content-Type: application/octet-stream");
    
// header ("Content-Length: ".filesize($nombretemporal));
    
echo $data;
}else{
?>
<FORM action="" method="POST" enctype="multipart/form-data">
    Upload your config.ini:<br><br>
    <input type="file" name="config"><br><br>
    <input type="submit" value="Enviar" name="enviado">
</FORM>
<?php
}
?>