<?php
	include ('includes/bootstrap.inc.php');
	$op 		= Data::getGet( 'op' );
	$encode		= Data::getGet( 'encode' );
	$msg 		= '';
	
	
	// parsing actions to be performed
	$parameters = explode ( '/', $op );
	
	// e.g. remote.php?op=owner/search&encode=json
	
	if(empty(Data::getSession('uid'))){
		header('Location: login.php?op=e');
	}

	
	// call a function using a remote request 
	if ( is_array ( $parameters ) &&  empty ( $parameters ) == false ) {
		// define the function to call (e.g. owner_search) 
		$section 	= array_shift ( $parameters );
		$action 	= array_shift ( $parameters );
		$callback = $section . '_' . $action;
		
		
		// if there is a valid POST, use that vars as parameters
		if ( is_array ( $_POST ) &&  empty ( $_POST ) == false ){
			$parameters[] =  json_decode ( $_POST['json'] );
		}
		
		$rv = call_user_func_array ($callback, $parameters);

		
		
		
		// manage different returning encoding
		switch ( $encode ){
			case 'json':
				$rv = json_encode ( $rv );
				
				break;
			
			case 'html':
			case 'none':
			case 'plain':
				break;
				
			default:
				$rv = json_encode ( $rv );
				break;
			
			
		}
		
		echo $rv;
		
	}
