<?php

/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.X
 *	LICENSE: FL0M2SG681CQ - http://www.flynax.com/license-agreement.html
 *	PRODUCT: Pets Classifieds
 *	DOMAIN: pupsnshop.com
 *	FILE: REQUEST.PHP
 *
 *	The software is a commercial product delivered under single, non-exclusive, 
 *	non-transferable license for one domain or IP address. Therefore distribution, 
 *	sale or transfer of the file in whole or in part without permission of Flynax 
 *	respective owners is considered to be illegal and breach of Flynax License End 
 *	User Agreement. 
 *
 *	You are not allowed to remove this information from the file without permission
 *	of Flynax respective owners.
 *
 *	Flynax Classifieds Software 2014 |  All copyrights reserved. 
 *
 *	http://www.flynax.com/
 *
 ******************************************************************************/

define('ANDROID_APP', true);

$item = $_REQUEST['item'];
$response_deflate = isset($_REQUEST['deflate']);
$response_dely = isset($_REQUEST['sleep']);
$request_lang = strtolower($_REQUEST['lang']);

if ( !$item )
	exit;

/* system config */
require_once( '../../includes/config.inc.php' );

session_start();
	
require_once( RL_CLASSES . 'rlDb.class.php' );
require_once( RL_CLASSES . 'reefless.class.php' );

$rlDb = new rlDb();
$reefless = new reefless();

/* load classes */
$reefless -> connect(RL_DBHOST, RL_DBPORT, RL_DBUSER, RL_DBPASS, RL_DBNAME);
$reefless -> loadClass('Debug');
$reefless -> loadClass('Config');

$config = $rlConfig -> allConfig();

$reefless -> loadClass('Cache');
$reefless -> loadClass('Valid');
$reefless -> loadClass('Hook');
$reefless -> loadClass('Lang');
$reefless -> loadClass('ListingTypes', null, false, true);
$reefless -> loadClass('AndroidConnect', null, 'androidConnect');

/* get available languages */
$languages = $rlLang -> getLanguagesList();
$reefless -> rlArraySort($languages, 'Order');

/* define system language */
if ( !array_key_exists($request_lang, $languages) )
{
	$request_lang = $config['lang'];
}

$rlLang -> defineLanguage($request_lang);
$rlLang -> modifyLanguagesList($languages);

/* assign base path */
$bPath = RL_URL_HOME;
if ($config['lang'] != RL_LANG_CODE && $config['mod_rewrite'])
{
	$bPath .= RL_LANG_CODE . '/';
}
if (!$config['mod_rewrite'])
{
	$bPath .= 'index.php';
}

$rlHook -> load('seoBase');

define( 'SEO_BASE', $bPath );

$response_type = 'xml';

/* utf8 library functions */
function loadUTF8functions()
{
	$names = func_get_args();
	
	if ( empty($names) )
	{
		return false;
	}
	
	foreach ( $names as $name )
	{
		if (file_exists( RL_LIBS . 'utf8' . RL_DS . 'utils' . RL_DS . $name . '.php' ))
		{
			require_once( RL_LIBS . 'utf8' . RL_DS . 'utils' . RL_DS . $name . '.php' );
		}
	}
}

$rlAndroidConnect -> setTimeZone($_REQUEST['timeZone']);

/* load all fronEnd phrases */
if ( !in_array($item, array('isPluginAvailable', 'getConfigs', 'getCache')) ) {
	$lang = $rlLang -> getLangBySide('frontEnd', RL_LANG_CODE);
	$GLOBALS['lang'] = &$lang;
}

/* requested items handler */
switch ($item){
	case 'isPluginAvailable':
		$availability = $rlDb -> getOne('ID', "`Status` = 'active' AND `Key` = 'androidConnect'", 'plugins');
		$response[] = array('available' => $availability ? true : false);
		break;
		
	case 'getConfigs':
		$response = $rlAndroidConnect -> getConfigs();
		break;
		
	case 'getCache':
		$countDate = (int)$_REQUEST['countDate'];
		$tablet = (int)$_REQUEST['tablet'];
		
		$response = $rlAndroidConnect -> getCache($countDate, $tablet);
		break;
		
	case 'getRecentlyAdded':
		$type = $rlValid -> xSql($_REQUEST['type']);
		$start = (int)$_REQUEST['start'];
		
		if ( !$type )
		{
			$mess = "Unable to fetch recently added listings, no listing type specified";
			$GLOBALS['rlDebug'] -> logger('ANDROID: '. $mess);
			die($mess);
		}
		
		if ( !array_key_exists($type, $rlAndroidConnect -> types) )
		{
			$mess = "Request listing type key doesn't exist";
			$GLOBALS['rlDebug'] -> logger('ANDROID: '. $mess);
			die($mess);
		}
		
		$response = $rlAndroidConnect -> getRecentlyAdded($type, $start);
		break;
		
	case 'getFeatured':
		$start = (int)$_REQUEST['start'];
		
		$response = $rlAndroidConnect -> getFeatured($start);
		break;
		
	case 'getFavorites':
		$start = (int)$_REQUEST['start'];
		$IDs = $type = $rlValid -> xSql($_REQUEST['ids']);
		
		$response = $rlAndroidConnect -> getListingByIDs($IDs, $start);
		break;
		
	case 'getListingDetails':
		$id = (int)$_REQUEST['id'];
		
		if ( !$id )
		{
			$mess = "Request listing details failed, ID doen't specified";
			$GLOBALS['rlDebug'] -> logger('ANDROID: '. $mess);
			die($mess);
		}
		
		$response = $rlAndroidConnect -> getListingDetails($id);
		break;
		
	case 'getAccountDetails':
		$id = (int)$_REQUEST['id'];
		
		if ( !$id )
		{
			$mess = "Request account details failed, ID doen't specified";
			$GLOBALS['rlDebug'] -> logger('ANDROID: '. $mess);
			die($mess);
		}
		
		$response = $rlAndroidConnect -> getAccountDetails($id);
		break;
		
	case 'searchResults':
		$start = (int)$_REQUEST['start'];
		$form_data = trim(urldecode($_REQUEST['data']), '{}');
		$type = $rlValid -> xSql($_REQUEST['type']);
		$sort = $rlValid -> xSql($_REQUEST['sort']);
		
		$response = $rlAndroidConnect -> searchResults($form_data, $type, $start, $sort);
		break;
		
	case 'getCategories':
		$parent = (int)$_REQUEST['parent'];
		$type = $rlValid -> xSql($_REQUEST['type']);
		
		$response = $rlAndroidConnect -> getCategories($type, $parent);
		break;

	case 'getListingsByCategory':
		$category_id = (int)$_REQUEST['id'];
		$start = (int)$_REQUEST['start'];
		$type = $rlValid -> xSql($_REQUEST['type']);
		$sort = $rlValid -> xSql($_REQUEST['sort']);
		
		$response = $rlAndroidConnect -> getListingsByCategory($category_id, $start, $type, $sort);
		break;

	case 'getAccounts':
		$accountTypeKey = $rlValid -> xSql($_REQUEST['type']);
		$start = (int)$_REQUEST['start'];
		$char = $rlValid -> xSql($_REQUEST['char']);
		
		$response = $rlAndroidConnect -> getAccountsByType($accountTypeKey, $start, $char);
		break;

	case 'getListingsByAccount':
		$account_id = (int) $_REQUEST['id'];
		$start = (int)$_REQUEST['start'];
		
		if ( !$account_id ) {
			$mess = "Request account listings faild, ID doen't specified";
			$GLOBALS['rlDebug'] -> logger('ANDROID: '. $mess);
			die($mess);
		}
		
		$response = $rlAndroidConnect -> getListingsByAccount($account_id, $start);
		break;

	case 'getListingsByLatLng':
		$type = $rlValid -> xSql($_REQUEST['type']);
		$start = (int)$_REQUEST['start'];
		$coordinates = array(
			'centerLat' => (double) $_REQUEST['centerLat'],
			'centerLng' => (double) $_REQUEST['centerLng'],
			'northEastLat' => (double) $_REQUEST['northEastLat'],
			'northEastLng' => (double) $_REQUEST['northEastLng'],
			'southWestLat' => (double) $_REQUEST['southWestLat'],
			'southWestLng' => (double) $_REQUEST['southWestLng']
		);
		
		$response = $rlAndroidConnect -> getListingsByLatLng($type, $start, $coordinates);
		break;

	case 'keywordSearch':
		$query = $rlValid -> xSql($_REQUEST['query']);
		$start = (int)$_REQUEST['start'];
		
		$response = $rlAndroidConnect -> keywordSearch($query, $start);
		break;
		
	case 'searchAccounts':
		$start = (int)$_REQUEST['start'];
		$form_data = trim(urldecode($_REQUEST['data']), '{}');
		$type = $rlValid -> xSql($_REQUEST['type']);
		
		$response = $rlAndroidConnect -> searchAccount($form_data, $type, $start);
		break;
		
	case 'sendEmail':
		$from = $rlValid -> xSql($_REQUEST['from']);
		$to = $rlValid -> xSql($_REQUEST['to']);
		
		$tpl = array(
			'subject' => $rlValid -> xSql($_REQUEST['subject']),
			'body' => $rlValid -> xSql($_REQUEST['body'])
		);
		
		$reefless -> loadClass('Mail');
		$rlMail -> send($tpl, $to, null, $from);
		
		break;
	case 'getMultiFieldNext':
		$parent = $rlValid -> xSql( $_REQUEST['parent'] );

		$reefless -> loadClass('MultiField', null, 'multiField');		
		$response = $rlMultiField -> getMDF($parent);
		
		foreach( $response as $k => &$v )
		{
			unset($v['Position'], $v['ID'], $v['Parent_ID'], $v['pName']);
		}
		break;

}

$rlAndroidConnect -> send($response, $response_type, $item);