<?php

function set_upload_offers($path){
	
	$config = array();
	$config['upload_path'] = $path;
	$config['allowed_types'] = 'gif|jpg|png';
	$config['overwrite']     = FALSE;
	return $config;
	
}

function set_upload_profilepic($path){
	
	$config = array();
	$config['upload_path'] = $path;
	$config['allowed_types'] = 'gif|jpg|png';
	$config['overwrite']     = FALSE;
	return $config;
	
}

function set_upload_service($path){
    
    $config = array();
    $config['upload_path'] = $path;
    $config['allowed_types'] = 'gif|jpg|png';
    $config['overwrite'] = FALSE;
    return $config;
    
}

function set_upload_editservice($path){
    
    $config = array();
    $config['upload_path'] = $path;
    $config['allowed_types'] = 'gif|jpg|png';
    $config['overwrite'] = FALSE;
    return $config;
    
}

function set_upload_all_files($path){
    
    $config = array();
    $config['upload_path'] = $path;
    $config['allowed_types'] = '*';
    $config['overwrite'] = FALSE;
    return $config;
    
}

function remove_html(&$item, $key)
{
    $item = strip_tags($item);
}

function set_log($class,$method,$postdata,$auth){

	$CI = & get_instance();

	$url = $class.'/'.$method;

	$data = array('url'=>$url,

				  'parameter'=>$postdata,

				  'auth'=>$auth,

				  'time'=>date('Y-m-d h:i:s'));
	

	$CI->db->insert('service_log',$data);
		//echo $CI->db->last_query();
	return $CI->db->insert_id();
}

function pr($val){
	echo (is_array($val))?'<pre>':'';
	print_r($val);
	echo (is_array($val))?'</pre>':'';
	exit;
}

function pre($val){
	echo (is_array($val))?'<pre>':'';
	print_r($val);
	echo (is_array($val))?'</pre>':'';
	echo '<br>';
}

function encode_param($param = ''){
	if(empty($param)){
		return;
	}
	$encode = base64_encode('{*}'.$param.'{*}');
	$encode = base64_encode('a%a'.$encode.'a%a');
	$encode = base64_encode('b'.$encode.'b');
	$encode = base64_encode('Ta7K'.$encode.'eyRq');
	return urlencode($encode);
}

function decode_param($param = ''){
	if(empty($param)){
		return;
	}
	$decode = urldecode(trim($param));
	$decode = trim(base64_decode(urldecode($decode)),'Ta7K');
	$decode = trim($decode,'eyRq');
	$decode = trim(base64_decode(urldecode($decode)),'b');
	$decode = trim(base64_decode(urldecode($decode)),'a%a');
	$decode = trim(base64_decode(urldecode($decode)),'{*}');
	return $decode;
}

function getLatLngFromLocation($location = ''){
	if(empty($location))
		return 0;
	$thisObj = & get_instance();
	$locData = file_get_contents("https://maps.google.com/maps/api/geocode/json?address=".
								 urlencode($location).
								 "&sensor=false&key=".$thisObj->session->userdata['settings']['google_api_key']);
	if(empty($locData))
		return 0;
    $loc_data = json_decode($locData);
    if(empty($loc_data) || !isset($loc_data->status) || $loc_data->status != 'OK')
		return 0;

	$locArr['lat'] = $loc_data->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
	$locArr['lng'] = $loc_data->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};

    if(empty($locArr['lat']) || empty($locArr['lng']))
		return 0;
	return $locArr;
}

function getSettings(){
	$CI = & get_instance();
	$settings = $CI->db->get('setting');
	return (!empty($settings))?$settings->row_array():'';
}

function push_sent_cancel($type='1', $fcm_token='', $fcm_data=array()) {
	$settings = getSettings();
	$key = $settings['push_app_key'];

	if(empty($key) || empty($fcm_token) || empty($fcm_data)){
		return;
	}

	switch ($type) {
		case '1':
			$data = "{ \"notification\": { \"title\": \"".$fcm_data['title']."\", \"text\": \"".$fcm_data['message']."\", \"sound\": \"default\" }, \"time_to_live\": 60, \"data\" : {\"response\" : {\"status\" : \"success\", \"data\" : {\"request_id\" : \"".$fcm_data['request_id']."\", \"request_type\" : 0}}}, \"collapse_key\" : \"trip\", \"priority\":\"high\", \"to\" : \"".$fcm_token."\"}";
			break;
		case '2':
			$data = "{ \"notification\": { \"title\": \"".$fcm_data['title']."\", \"text\": \"".$fcm_data['message']."\", \"sound\": \"default\" }, \"time_to_live\": 60, \"data\" : {\"response\" : {\"status\" : \"success\", \"data\" : {\"request_id\" : \"".$fcm_data['request_id']."\", \"request_type\" : 1}}}, \"collapse_key\" : \"trip\", \"priority\":\"high\", \"to\" : \"".$fcm_token."\"}";
			break;
	}

	$ch = curl_init("https://fcm.googleapis.com/fcm/send");
	$header = array('Content-Type: application/json', 'Authorization: key='.$key);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

	$out = curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_exec($ch);
	curl_close($ch);
}?>