<?
/* 
   This file is part of the 'Science Fair In A Box' project
   SFIAB Website: http://www.sfiab.ca

   Copyright (C) 2007 James Grant <james@lightbox.org>
   Copyright (C) 2009 David Grant <dave@lightbox.org>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation, version 2.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/
?>
<?
 require_once('../common.inc.php');
 require_once('../user.inc.php');
 user_auth_required('committee', 'admin');
 require_once('xml.inc.php');


 function curl_query($fair, $data, $ysc_url='')
 {
 	global $output;
 	switch($fair['type']) {
	case 'sfiab':
		$url = $fair['url'].'/remote.php';
		$var = 'json';
		$d = array();
		$d['auth'] = array('username' => $fair['username'],
				'password' => $fair['password']);
		$str = json_encode(array_merge($d, $data));
		break;
	case 'ysc':
		if($ysc_url == '')
			$url = $fair['url'];
		else 
			$url = $ysc_url;
		$var = 'xml';
		$output="";
		xmlCreateRecurse($data);
		$str = $output;
		break;
	}

//	echo "<pre>Curl Send: $str</pre>";

	$ch = curl_init(); /// initialize a cURL session
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_HEADER, 0); /// Header control
	curl_setopt ($ch, CURLOPT_POST, 1);  /// tell it to make a POST, not a GET
	curl_setopt ($ch, CURLOPT_POSTFIELDS, "$var=".urlencode($str));  /// put the query string here starting with "?"
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); /// This allows the output to be set into a variable $datastream
	curl_setopt ($ch, CURLOPT_POSTFIELDSIZE, 0);
	curl_setopt ($ch, CURLOPT_TIMEOUT, 360);
	curl_setopt ($ch, CURLOPT_SSLVERSION, 3);
	curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
	$datastream = curl_exec ($ch); /// execute the curl session and return the output to a variable $datastream
	curl_close ($ch); /// close the curl session

//	echo "<pre>Server Returned: ".urldecode($datastream)."</pre>";

 	switch($fair['type']) {
	case 'sfiab':
		$ret=json_decode(urldecode($datastream), true);
		break;
	case 'ysc':
		$datastream = str_replace(" standalone=\"yes\"","",$datastream);
		/* Return is plaintext, make a return array */
		$ret['error'] = 0;
		$ret['message'] = $datastream;
		break;
	}
//	echo "<pre>Server Returned: ";print_r($ret);echo "</pre><br>";
	return $ret;
 }
?>
