<?php

   header("Access-Control-Allow-Origin: *");

   date_default_timezone_set('America/Bogota');
   
   //error_reporting(E_ALL & ~E_STRICT & ~E_NOTICE);
   header('Access-Control-Allow-Origin: *');
   
   // Retrieve specific parameter from supplied URL
   $key  = strip_tags($_REQUEST['key']);
		 
   $data    = array();

   //print_r($_REQUEST);
   // Determine which mode is being requested
   $usrl ="https://appsceai.com/Saf/";
   switch($key)
   {
	  case "instructores" :
	       
           // Attempt to query database table and retrieve data
           try {  

		        $service ="rest_usuarios/getInstructoresActivos.json";
                
				$data = getRemoteFile('https://appsceai.com/Saf/rest_usuarios/getInstructoresActivos.json');
				$data=json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $data) );


				print_r ('{"status": "success","data" :'.json_encode($data->usuario).',"result":"ok"}');
				
				//echo json_encode(array('status'=>'success','data'=>$data,'result'=>1,'message' => 'Something went wrong! recovering version')); 
            } 
           catch(Exception $e)
           {
                echo $e->getMessage();
           }
      break;
	  
	  case "cursos" :
	       
           // Attempt to query database table and retrieve data
           try {  
		        $service ="rest_usuarios/getInstructoresActivos.json";
                
			//	$data = getRemoteFile('https://appsceai.com/Saf/rest_horarios/obtenerCursosActivos.json');
				//$data = getRemoteFile('https://appsceai.com/Saf/rest_usuarios/getInstructoresActivos.json');
                // Return data as JSON
				//print_r($data);
				$cursos = array();
				
				$cursos [] = ["curso" =>array(
					    "Programasformacion" =>array('acronimo'=>"ADSO 6"),
						"Curso"=> array ("numero"=>"1347")
					)
					];
					
				
				$cursos [] = ["curso" =>array(
					    "Programasformacion" =>array('acronimo'=>"ADSO 5"),
						"Curso" =>array("numero"=>"1386")
					)
					];	
					
				/*		
				$cursos= array(
				
				    "curso" =>array(
					    "Programasformacion" =>array('acronimo'=>"ADSO 6"),
						"Curso"=> array ("numero"=>"1347")
					),
				    "curso" =>array(
					    "Programasformacion" =>array('acronimo'=>"ADSO 5"),
						"Curso" =>array("numero"=>"1386")
					)					
				);
				*/
				
				//print_r($cursos);
                //print_r ('{"status": "success","data" :'.json_encode(json_decode($data)->cursos).',"result":"ok"}');
			    print_r ('{"status": "success","data" :'.json_encode($cursos).',"result":"ok"}');
				//echo json_encode(array('status'=>'success','data'=>$data,'result'=>1,'message' => 'Something went wrong! recovering version')); 
            } 
           catch(Exception $e)
           {
                echo $e->getMessage();
           }
      break;	

	  	  case "ambientes" :
	       
           // Attempt to query database table and retrieve data
           try {  
		        $service ="rest_usuarios/getInstructoresActivos.json";
                
				$data = getRemoteFile('https://appsceai.com/Saf/rest_horarios/obtenerAmbientes.json');
				print_r($data);
				$data=json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $data) );
                // Return data as JSON
                print_r ('{"status": "success","data" :'.json_encode(json2arrayAmbiente($data->ambientes)).',"result":"ok"}');
				//print_r(json_decode($data));
				//print_r(json2arrayAmbiente(json_decode($data)->ambientes));
				//echo json_encode(array('status'=>'success','data'=>$data,'result'=>1,'message' => 'Something went wrong! recovering version')); 
            } 
           catch(Exception $e)
           {
                echo $e->getMessage();
           }
      break;
	  
	  case "horarioxinstructor" :
	       
           // Attempt to query database table and retrieve data
           try 
		   {  
		        $id  = strip_tags($_REQUEST['id']);
		        $service ="rest_usuarios/getInstructoresActivos.json";
				$data = getRemoteFile('https://appsceai.com/Saf/rest_horarios/getbyusuario/'.$id.'.json');
				$data=json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $data) );
                // Return data as JSON
                print_r ('{"status": "success","data" :'.json_encode($data->horario).',"result":"ok"}');
				//echo json_encode(array('status'=>'success','data'=>$data,'result'=>1,'message' => 'Something went wrong! recovering version')); 
           } 
           catch(Exception $e)
           {
                echo $e->getMessage();
           }
      break;
	  
	  case "horarioxcurso" :
	       
           // Attempt to query database table and retrieve data
           try {  
		        $id  = strip_tags($_REQUEST['id']);
		        $service ="rest_usuarios/getInstructoresActivos.json";
				$data = getRemoteFile('https://appsceai.com/Saf/rest_horarios/getbycurso/'.$id.'.json');
				$data=json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $data) );
                // Return data as JSON
                print_r ('{"status": "success","data" :'.json_encode($data->horario).',"result":"ok"}');
				//echo json_encode(array('status'=>'success','data'=>$data,'result'=>1,'message' => 'Something went wrong! recovering version')); 
            } 
           catch(Exception $e)
           {
                echo $e->getMessage();
           }
      break;	  

	  case "horarioxambiente" :
	       
           // Attempt to query database table and retrieve data
           try 
		   {  
		        $id  = strip_tags($_REQUEST['id']);
				$data = getRemoteFile('https://appsceai.com/Saf/rest_horarios/getbyambiente/'.$id.'.json');
				$data=json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $data) );                       
				
                // Return data as JSON
                print_r ('{"status": "success","data" :'.json_encode($data->horario).',"result":"ok"}');
				//echo json_encode(array('status'=>'success','data'=>$data,'result'=>1,'message' => 'Something went wrong! recovering version')); 
           } 
           catch(Exception $e)
           {
                echo $e->getMessage();
           }
      break;
	  
	  case "voceros" :
	       
		   $numero  = strip_tags($_REQUEST['numero']);
		   $acronimo  = strip_tags($_REQUEST['acronimo']);
           // Attempt to query database table and retrieve data
           try {  
			
                // Return data as JSON
                print_r ('{"status": "success","data" :'.json_encode(voceros2json($numero,$acronimo)).',"result":"ok"}');
				
				//echo json_encode(array('status'=>'success','data'=>$data,'result'=>1,'message' => 'Something went wrong! recovering version')); 
            } 
           catch(Exception $e)
           {
                echo $e->getMessage();
           }
      break;	  
	
	  case "version":
	      

           // Attempt to query database table and retrieve data 
           try {
			   print_r ('{"status": "success","version" :"1.02","url" :"http://www.myhostinglabs.com/apps/saf/saf102.apk"}');
            }
           catch(Exception $e)
           {
			    echo json_encode(array('status'=>'fail','version'=>'-1','message' => 'Something went wrong! recovering version')); 
                echo $e->getMessage();
           }
    
	  break;	  
	  default : echo json_encode(array('status'=>'fail','message' => 'Fail !! ... No Method Exist, Retry','response' => -1)); 	
		  



   }
    
///// Functions /////
function getRemoteFile($url, $timeout = 1000) {
  $ch = curl_init();
  curl_setopt ($ch, CURLOPT_URL, $url);
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $file_contents = curl_exec($ch);
  curl_close($ch);
  return ($file_contents) ? $file_contents : FALSE;
}

function json2arrayAmbiente($obj)
{
    $_arr = is_object($obj) ? get_object_vars($obj) : $obj;
	$i=0;
    foreach ($_arr as $key => $val) {
        $val = is_array($val) || is_object($val) ? json2array($val) : $val;
        $arr[$i]["Ambiente"] = $val;
		$arr[$i]["id"] = $key;
		$i++;
    }
    return $arr;
}


function voceros2json($id, $acronimo)
{
	 $i=0;
	 if($fh = fopen("voceros.dat","r")){ 
      while (!feof($fh)){ 
         $line = fgets($fh); 
		 //print_r($line);
		 $dataLine = explode(";",$line);
		 //print_r("<br>".$dataLine[1] . "->" . $dataLine[2]);
		 if($dataLine[1]== $acronimo && $dataLine[2]== $id)
		 {
		   $data[$i]["nombres"] = $dataLine[3];
		   $data[$i]["tipo"] = $dataLine[0];
		   $data[$i]["telefono"] = $dataLine[4];
		   $data[$i]["email"] = $dataLine[5];
		   $i++; 
		 }	 
      } 
      fclose($fh); 
    } 
	return $data;
}







   
?>


