<?php
include_once(_DIR_EXT.'/HTML/c_page_TXT.php') ;
class c_services_esmo_gateway_get extends c_page_txt
{
  function body()
  {  //    /services/esmo_gateway/get/personals/xml/
     // 0       1         2        3      4      5
     $arr=explode('/',_CUR_PAGE_DIR) ; // print_r($arr) ;
     $report_code=$arr[4] ; $mode=$arr[5] ;
     // для клиента сделан отдельный отчет, где не было бы возможность выбирать подразделения других клиентов
     if ($report_code=='personals') $report_code='client_personals' ;

	 if (!$GLOBALS['LS_gate_extended_server_ip']) { $this->response_error($mode,array('code'=>'access_denied','IP'=>$_SERVER['REMOTE_ADDR']));  return ; }
	 if ($GLOBALS['LS_gate_extended_server_ip'])
     { $arr_ips=explode(',',$GLOBALS['LS_gate_extended_server_ip']) ;
       foreach($arr_ips as $i=>$ip) $arr_ips[$i]=trim($ip) ;
       if (!in_array($_SERVER['REMOTE_ADDR'],$arr_ips))   { $this->response_error($mode,array('code'=>'access_denied','IP'=>$_SERVER['REMOTE_ADDR']));  return ; }
     }



     $options=array() ;
     if (isset($_GET['limit'])) $options['limit']=$_GET['limit'] ;

     $report_obj=REPORTS()->get_report_obj_by_code($report_code) ;
     if ($report_obj->error) $this->error=array('code'=>'report_error','text'=>$report_obj->error) ;
     else
     {   $report_obj->filter=$_GET;
         switch($mode)
         { case 'json': $report_obj->view_as_JSON($options) ; break ;
           case 'xml':  $report_obj->view_as_XML($options) ; break ;
           case 'html':  $report_obj->view_as_HTML($options) ; break ;
           default:     echo 'undefined output mode' ;
         }
     }

  }

  function response_error($mode,$arr)
  {  switch($mode)
	   { case 'json':  header('Content-Type: application/json');
		               $text_json_out=json_encode($arr,JSON_PRETTY_PRINT) ;
		               echo $text_json_out ;
	                   break ;
	     case 'xml':   header("Content-type: text/xml;charset=UTF-8") ;
	                   $imp = new DOMImplementation; // Creates an instance of the DOMImplementation class
	                   $xml_doc = $imp->createDocument("",""); // Creates a DOMDocument instance
	                   $xml_doc->encoding = 'utf-8';
	                   $xml_doc->formatOutput = true;
	                   $xml = add_element($xml_doc,$xml_doc,'xml_doc','',array('date'=>date('Y-m-d H:i'))) ;
	                   $error=add_element($xml_doc,$xml,'error','',array()) ;
	                   if (_sizeof($arr)) foreach($arr as $name=>$value) add_element($xml_doc,$error,$name,$value) ;
		               echo $xml_doc->saveXML() ;
		               break ;
	     case 'html':  header("Content-type: text/txt;charset=UTF-8") ;
		               $res=array() ;
	     	           if (_sizeof($arr)) foreach($arr as $name=>$value) $res[]=$name.': '.$name ;
	                   echo implode("\n",$res) ;
	                   break ;
	     default:     echo 'undefined output mode' ;
	   }
  }

}



?>
