<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
include_once ("../config.inc.php");
include_once ("../includes/mysql.inc.php");
require_once ("../classes/userside.class.php");

include_once ("../classes/misc.class.php");

require_once ("../classes/settings.class.php");

include_once ("../iphonepush.php");

if (!$objDB) {
    $message = "Unable to Locate Database Server.";
} else {
    $objM = new misc($objDB);
   
}
if ($_REQUEST['action_type'] == 'login') {
    $data = array();
    $data['user'] = $_REQUEST['name'];
    $data['pass'] = $_REQUEST['password'];
    $data['device'] = $_REQUEST['device'];
    $data['deviceId'] = $_REQUEST['deviceId'];
    $result = $objM->loginApi($data);

    echo json_encode($result);
    exit;
}

if($_REQUEST['action_type']=='check_user_permission')
{
    
    $return = $objM->checkPremiumPermission("true",$_REQUEST['user_id']);
    $data = array();
    $data['status'] = ($return)?1:0;
    
    echo json_encode($data);
    exit;
}


if($_REQUEST['action_type']=='settings')
{
    
    if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objC = new check();
        $objU = new userside($objDB);
        $objM = new misc($objDB);
        $objST	= new settings($objDB);
    }
    
    $siteSettings=$objST->select();
 if($_REQUEST['user_id']){
     $return = $objM->checkPremiumPermission("true",$_REQUEST['user_id']);
  
    $siteSettings['user_permission'] = ($return)?1:0;
 }
    
    echo json_encode($siteSettings);
    exit;
    
    
}

if($_REQUEST['action_type']=='message_count')
{
    
    $inboxcount= $objM->get_message_count("NN", $_REQUEST['user_id'], true);
    
    $data = array();
    $data['status'] = 1;
    $data['data']['count'] = $inboxcount;
    echo json_encode($data);
    exit;
}


if($_REQUEST['action_type']=='interest_recieved')
{
    
    $inboxcount=$objM->get_intrest_count("NN", $_REQUEST['user_id'], true);
    $data = array();
    $data['status'] = 1;
    $data['data']['count'] = $inboxcount;
    echo json_encode($data);
    exit;
}


if ($_REQUEST['action_type'] == 'logout') {
    $data = $return = array();
   
    $data['userId'] = $_REQUEST['user_id'];
    $data['device'] = $_REQUEST['device'];
    $data['deviceId'] = $_REQUEST['deviceId'];
    $result = $objM->logoutApi($data);
    if($result){
        $return['status'] = 1;
        $return['success'] = "Logged out successfully";
    } 
    else{
        $return['status'] = 0;
        $return['error'] = "Failed to Logged out";
    }
    echo json_encode($return);
    exit;
}
if ($_REQUEST['action_type'] == 'facebook_login') { 
    $data = array();
    $message = "";
 if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objU = new userside($objDB);
        $objU1 = new userside($objDB);
        $objSU2		= new settings($objDB);
    }
     
    
  $result = $objM->login_facebook_api($_REQUEST);
 // print_r($_FILES);
  if(isset($_FILES['file']['name']) && $result['type']=='register')
  { 
      $data = array();
      $data['vTitle'] = $_FILES['file']['name'];
      $data['nUser_Id'] = $result['data']['user_id'];
      $objU->set_user_image(array_merge($data, $_FILES));

        //create allowed image type array
        $allowdImgTypes=$objSU2->select();
        $allowdImgTypes=explode(",",$allowdImgTypes['allowedimagetypes']);
        //checking uploaded image is is allowed type and also checking uploaded file is executable
        $typeChkMsg = $objSU2->CheckUploadFileType($allowdImgTypes,$_FILES['file']['type']);
       
        if(trim($typeChkMsg)=='') {            
           
                $message	= $objU->save_user_image($result['data']['user_id'], $pid,1);
        }
            else 
                $message=$chkMsg;
                
        }//end if
      

      
  
  
 //$result['files'] = $_FILES;
  //echo $message;
  echo json_encode($result);
  //echo json_encode($_FILES);
  exit;
}


if($_REQUEST['action_type']=='photo_upload')
{   
    
     if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objU = new userside($objDB);
        $objU1 = new userside($objDB);
        $objSU2		= new settings($objDB);
    }
    $return = array();
    if(isset($_FILES['file']['name']))
  { 
      $data = array();
      $data['vTitle'] = $_FILES['file']['name'];
      $data['nUser_Id'] = $_REQUEST['user_id'];
      
      $objU->set_user_image(array_merge($data, $_FILES));
      $pid = $_REQUEST['photo_id'];
        //create allowed image type array
        $allowdImgTypes=$objSU2->select();
        $allowdImgTypes=explode(",",$allowdImgTypes['allowedimagetypes']);
      
        //checking uploaded image is is allowed type and also checking uploaded file is executable
        $typeChkMsg = $objSU2->CheckUploadFileType($allowdImgTypes,$_FILES['file']['type']);
      
        if(trim($typeChkMsg)=='') {            
            
            list($width, $height) = getimagesize($_FILES['file']['tmp_name']);
            if($width <300 || $height <300) {   
                $message = "Upload image size should be atleast 300x300 pixels";
            }
            
            if($message=='')
                $objU->save_user_image($_REQUEST['user_id'], $pid,1);
            
            if(!$pid){    
            $pid =  @mysql_insert_id();
            }
             
            $getPhotoDetails = $objU->get_image($pid);
            
                
        }
            else 
                $message= $typeChkMsg;
                
        }
       
        
    if($message=='')
    {
        $return['status'] = 1;
        $return['message'] = 'Succesfully uploaded';
        $return['data'] = $getPhotoDetails;
    }else{
         $return['status'] = 0;
         $return['message'] = $message;
    }
    
    echo json_encode($return); exit;
}

if($_REQUEST['action_type']=='delete_photo')
{
     if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objU = new userside($objDB);
        $objU1 = new userside($objDB);
        $objSU2		= new settings($objDB);
    }
    	$objU->delete_user_image($_REQUEST['user_id'],$_REQUEST['photo_id'],1);
    $return['status'] = 1;
        $return['message'] = 'Succesfully deleted';
     echo json_encode($return); exit;
}

if ($_REQUEST['action_type'] == 'mobile_providers') {
    if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objC = new check();
        $objU = new userside($objDB);
        $objM = new misc($objDB);
    }
    $arrProvider    = $objU->get_providers();
    
   
    if ($arrProvider){
        $return['status'] = 1;
        $return['data'] = $arrProvider;
    }    
   else{
        $return['status'] = 0;
        $return['error'] = 'No provider details found ';
    }
    echo json_encode($return);
    exit;
}
if ($_REQUEST['action_type'] == 'myprofile') {
    
    if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objU = new userside($objDB);
        $objU1 = new userside($objDB);
    }
    $array = $objU->select($_REQUEST['user_id']);
    
    //print_r($array);
    $result['data']['profile'] = $objU->get_values_api($array);
    $array1 = $objU1->selectpartner($_REQUEST['user_id']);
    $objU1->set_values($array1);
    $result['data']['images'] = $objU->get_user_images($_REQUEST['user_id']);
//    $script		= $objM->get_script($message);
//
//    $message	= $objM->remove_script($message);
    echo json_encode($result);
    exit;
}

if ($_REQUEST['action_type'] == 'search') {
    if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objC = new check();
        $objU = new userside($objDB);
    }
    $data = array();
    $data['vSG'] = $_REQUEST['vSG'];
    $data['vSAF'] = $_REQUEST['vSAF'];
    $data['vSAT'] = $_REQUEST['vSAT'];
    $data['vSP'] = $_REQUEST['vSP'];
    $data['user_id'] = $_REQUEST['user_id'];
    $result['data'] = $objU->search_profiles_api($data);
    echo json_encode($result);
    exit;
}

if ($_REQUEST['action_type'] == 'forgotpassword') {

    if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objM = new misc($objDB);
    }
    $data = array();
    $data['vLogin'] = $_REQUEST['vLogin'];
    $data['dDob'] = $_REQUEST['dDob'];

    $result = $objM->forgot_password_api($data);
    echo json_encode($result);
    exit;
}

if ($_REQUEST['action_type'] == 'hotlist') {
    if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objC = new check();
        $objU = new userside($objDB);
        $objM = new misc($objDB);
    }
    $array['data'] = $objM->my_hots_api($_REQUEST['user_id']);

    echo json_encode($array);
    exit;
}

if ($_REQUEST['action_type'] == 'express_interest') {

    if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objC = new check();
        $objU = new userside($objDB);
        $objM = new misc($objDB);
    }
    $res = $objM->add_interest_api($_REQUEST['user_id'], $_REQUEST['profile_id']);
    echo json_encode($res);
    if($res['status']!=0){
        /*     * **************** Iphone PUsh notication ************** */
    $objSeT = new settings($objDB);
    $sSettings = $objSeT->select();
    if ($sSettings['enable_pushbk_notification'] == "Y") {
        $Todetails = $objU->select($_REQUEST['profile_id']);
        $fromDetails = $objU->select($_REQUEST['user_id']);
        $userDevices = $objM->getuserDevices($_REQUEST['profile_id']);
        if($userDevices){
            foreach($userDevices as $key=>$value)
            {   
                $deviceToken = $value['device_id'];
                $deviceType =  $value['device'];
                if ($deviceType == "ios" && $deviceToken) {
                    // Put your private key's passphrase here:
                    $passphrase = $sSettings['iPhone_parse_phrase'];
                    // Put your alert message here:
                    $from = $fromDetails['vFirstName'];
                    if ($fromDetails['vMiddleName'])
                        $from .= " " . $fromDetails['vMiddleName'];
                    if ($fromDetails['vLastName'])
                        $from .= " " . $fromDetails['vLastName'];
                    $message = "$from Expressed an interest on your profile!";
                    send_push_nofication($message, $from, $passphrase, $deviceToken,'express_interest' ,true);
                }
            }
        }
    }
    /*     * ***************** Iphone PUsh notication end **************** */
    }
    
    
    
    exit;
}

if ($_REQUEST['action_type'] == 'sent_email') {

    if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objC = new check();
        $objU = new userside($objDB);
        $objM = new misc($objDB);
    }

    $data = array();
    $data['id'] = $_REQUEST['profile_id'];
    $data['vTitle'] = $_REQUEST['vTitle'];
    $data['tContent'] = $_REQUEST['tContent'];
    $res = $objM->send_message_api($_REQUEST['user_id'], $data);

    /*     * **************** Iphone PUsh notication ************** */
    $objSeT = new settings($objDB);
    $sSettings = $objSeT->select();
    if ($sSettings['enable_pushbk_notification'] == "Y") {
        $Todetails = $objU->select($_REQUEST['profile_id']);
        $fromDetails = $objU->select($_REQUEST['user_id']);
        $userDevices = $objM->getuserDevices($_REQUEST['profile_id']);
        if($userDevices){
            foreach($userDevices as $key=>$value)
            {   
                $deviceToken = $value['device_id'];
                $deviceType =  $value['device'];
                if ($deviceType == "ios" && $deviceToken) {
                    // Put your private key's passphrase here:
                    $passphrase = $sSettings['iPhone_parse_phrase'];
                    // Put your alert message here:
                    $from = $fromDetails['vFirstName'];
                    if ($fromDetails['vMiddleName'])
                        $from .= " " . $fromDetails['vMiddleName'];
                    if ($fromDetails['vLastName'])
                        $from .= " " . $fromDetails['vLastName'];
                    $message = "You have one message from $from";
                    send_push_nofication($message, $from, $passphrase, $deviceToken,'send_message', true);
                }
            }
        }
    }
    /*     * ***************** Iphone PUsh notication end **************** */


    echo json_encode($res);
    exit;
}

if ($_REQUEST['action_type'] == 'inbox') {

    if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objC = new check();
        $objU = new userside($objDB);
        $objM = new misc($objDB);
    }
    $type = '_N';
    $return = array();

    $array = $objM->get_messages_api($type, $_REQUEST['user_id'], true, $_REQUEST['page']);


    if (empty($array)) {
        $message .= "No  message(s) in your inbox folder";
        $return['status'] = 0;

        $return['error'] = $message;
    } else {
        $return['status'] = 1;
        $return['data'] = $array;
    }

    echo json_encode($return);
    exit;
}

if ($_REQUEST['action_type'] == 'outbox') {

    if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objC = new check();
        $objU = new userside($objDB);
        $objM = new misc($objDB);
    }
    $type = '_N';
    $return = array();

    $array = $objM->get_messages_api($type, $_REQUEST['user_id'], false, $_REQUEST['page']);


    if (empty($array)) {
        $message .= "No  message(s) in your inbox folder";
        $return['status'] = 0;

        $return['error'] = $message;
    } else {
        $return['status'] = 1;
        $return['data'] = $array;
    }

    echo json_encode($return);
    exit;
}

if ($_REQUEST['action_type'] == 'interest_sent') {

    if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objC = new check();
        $objU = new userside($objDB);
        $objM = new misc($objDB);
    }
    //Shows all interests irrespective of status
    $type = "__";
    $_REQUEST['page'] = $_REQUEST['page'] ? $_REQUEST['page'] : 1;
    $array = $objM->get_intrests_api($type, $_REQUEST['user_id'], false, $_REQUEST['page']);
    if (empty($array)) {
        $message .= "No interest(s) found";
        $return['status'] = 0;
        $return['error'] = $message;
    } else {
        $return['status'] = 1;
        $return['data'] = $array;
    }
    echo json_encode($return);
    exit;
}

if ($_REQUEST['action_type'] == 'interest_received') {

    if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objC = new check();
        $objU = new userside($objDB);
        $objM = new misc($objDB);
    }
    //Shows all interests irrespective of status
    $type = "NN";
    $_REQUEST['page'] = $_REQUEST['page'] ? $_REQUEST['page'] : 1;
    $array = $objM->get_intrests_api($type, $_REQUEST['user_id'], true, $_REQUEST['page']);
    if (empty($array)) {
        $message .= "No interest(s) found";
        $return['status'] = 0;
        $return['error'] = $message;
    } else {
        $return['status'] = 1;
        $return['data'] = $array;
    }
    echo json_encode($return);
    exit;
}

if ($_REQUEST['action_type'] == 'add_hotlist') {
    if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objC = new check();
        $objU = new userside($objDB);
        $objM = new misc($objDB);
    }

    $loggedUserId = $_REQUEST['user_id'];
    $hotUserId = $_REQUEST['hot_user_id'];

    $message = $objM->add_to_hotlist_api($loggedUserId, $hotUserId);
    if (!$message) {
        $return['status'] = 1;
        $return['success'] = "This profile has been successfully added to your hot list.";
    } else {
        $return['status'] = 0;
        $return['error'] = $message;
    }
    echo json_encode($return);
    exit;
}

if ($_REQUEST['action_type'] == 'delete_hotlist') {
    
     if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objC = new check();
        $objU = new userside($objDB);
        $objM = new misc($objDB);
        
        $array = array();
        $return = array();
        $array['ids'][] = $_REQUEST['hot_id'];
        
        
        $message	= $objM->delete_hots($array);
        
        $return['status'] = 1;
        $return['success'] = "Successfully Deleted.";
        echo json_encode($return);
        exit;
    }
    
    
    
    
}


if ($_REQUEST['action_type'] == 'my_photos') {
    if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objC = new check();
        $objU = new userside($objDB);
        $objM = new misc($objDB);
    }

    $loggedUserId = $_REQUEST['user_id'];
    $arrImages = $objU->get_user_images($loggedUserId);
    if (empty($arrImages)) {
        $return['status'] = 0;
        $return['error'] = "You haven't uploaded any photos yet.";
    } else {
        $return['status'] = 1;
        $return['data'] = $arrImages;
    }
    echo json_encode($return);
    exit;
}

if ($_REQUEST['action_type'] == 'edit_profile') {
    if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objC = new check();
        $objU = new userside($objDB);
        $objM = new misc($objDB);
    }
    $objU->set_values($_REQUEST);
    if ($_REQUEST['section'] == "PI") {
        $message = $objU->edit_personal_info_api($_REQUEST['user_id']);
    }
    if ($_REQUEST['section'] == "BI") {
        $message = $objU->edit_basic_info_api($_REQUEST['user_id']);
    }
    if ($_REQUEST['section'] == "AI") {
        $message = $objU->edit_appearence_info_api($_REQUEST['user_id']);
    }
    if (!$message) {
        $return['status'] = 1;
        $return['success'] = "Your profile information saved.<br>";
    }else{
        $return['status'] = 0;
        $return['error'] = $message;
    }
    echo json_encode($return);
    exit;
}

if ($_REQUEST['action_type'] == 'tell_a_friend') { 
    if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objC = new check();
        $objU = new userside($objDB);
        $objM = new misc($objDB);
    }
    $loggedUserId = $_REQUEST['user_id'];
    $message = $objM->tell_friend_mail_api($objU, $loggedUserId, $_REQUEST['email'], $_REQUEST['name'], $_REQUEST['message']);
    if (!$message) {
        $return['status'] = 1;
        $return['success'] = "Email sent successfully";
    } else {
        $return['status'] = 0;
        $return['error'] = $message;
    }
    echo json_encode($return);
    exit;
}
if ($_REQUEST['action_type'] == 'interest_status_change') {

    if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objC = new check();
        $objU = new userside($objDB);
        $objM = new misc($objDB);
    }
    $status = $_REQUEST['status'];
    $interest_id = $_REQUEST['interest_id'];
    $message = "";
    if ($status == "AN")
        $msg = "approved";
    else if ($status == "DN")
        $msg = "declined";
    if (empty($interest_id))
        $message = "Please select the interests which you need to $msg.";
    else {
        $result = $objM->change_interest_status_api($interest_id, $_REQUEST['status']);
        if (!$result)
            $message = "Unable to $msg the interests, please try later.";
        else {
            $return['status'] = 1;
            $return['success'] = "Interest  $msg  successfully";
        }
    }
    if ($message) {
        $return['status'] = 0;
        $return['error'] = $message;
    }
    echo json_encode($return);
    exit;
}
if ($_REQUEST['action_type'] == 'change_password') {

    if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objC = new check();
        $objU = new userside($objDB);
        $objM = new misc($objDB);
    }
    $userId = $_REQUEST['user_id'];
    $oldPassword = $_REQUEST['old_password'];
    $newPassword = $_REQUEST['new_password'];
    $message = "";
    $result = $objM->change_password_api($userId,$oldPassword,$newPassword);
   
    if (!$result)
        $message = "Unable to change password, please try later.";
    else if ($result==="invalid")
        $message = "Incorrect old passsword. Please give a correct password";
    else if ($result){
        $return['status'] = 1;
        $return['success'] = "Your password has been changed successfully";
    }    
    if ($message) {
        $return['status'] = 0;
        $return['error'] = $message;
    }
    echo json_encode($return);
    exit;
}

if($_REQUEST['action_type']=='settings')
{
    
    if (!$objDB) {
        $message = "Unable to Locate Database Server.";
    } else {
        $objC = new check();
        $objU = new userside($objDB);
        $objM = new misc($objDB);
        $objST	= new settings($objDB);
    }
    
    $siteSettings=$objST->select();

    
    echo json_encode($siteSettings);
    exit;
    
    
}


?>
