<?php
   include_once '../account/session.php';
   include_once '../../scripts/bd.php';
   
   if(isset($_GET['action'])){
    $action=$_GET['action'];
    if($action=='show')show();
    if($action=='createSession')createSession();

   }
   
   function show(){
      $data=array();
      $query = bd::query("SELECT id,name,active FROM sponsor WHERE active=1");
      while ($row = mysql_fetch_object($query)) {
          $data[]=$row;
      }
      echo json_encode($data);
   }

   function createSession() {
	   	$query = bd::query("SELECT login_page,login,password,platform FROM sponsor WHERE id = '".$_GET['id_sponsor']."'");
	   	$sponsor = mysql_fetch_array($query);
	   	$url_params = explode('/', $sponsor['login_page']);
	   	$url_depart = $url_params[2];
	   	$check = false;
	   	if($sponsor['platform'] == 'Cake') {
	   		$url_final = 'http://'.$url_depart.'/login.ashx';
	   		$check = true;
	   		$data = 'u='.$sponsor['login'].'&p='.$sponsor['password'];
	   	} else if($sponsor['platform'] == 'HitPath') {
	   		$url_final = 'http://'.$url_depart.'/process.php';
	   		$check = true;
	   		$data = 'utype=agent&action=login&uname='.$sponsor['login'].'&pword='.$sponsor['password'];
	   	}
	   	if($check == true) {
	   		$ch = curl_init();
			curl_setopt($ch,CURLOPT_URL,$url_final);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
			$server_output = curl_exec($ch);
			curl_close($ch);
	   	}
	   	$tableau = json_decode($server_output);
	   	if($tableau->success == true) {
	   		// NOT FINISH !
	   	}
   }

   