Subversion Repositories SmartDukaan

Rev

Rev 19257 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
App::uses('AppController', 'Controller');
/**
 * A Controller
 *
 * @property A $A
 * @property PaginatorComponent $Paginator
 */
class AController extends AppController {

/**
 * Components
 *
 * @var array
 */
        
        public $components = array('Paginator');
                    public function beforeFilter() {
                        parent::beforeFilter();
                        $this->Auth->allow('index','mobile','error');
                    }

                public function index() {
                $encryptUrl = 'http://104.200.25.40:8057/message/encrypt?type=encrypt&data=';
                $expiryFlag=false;
                $redirectFlag=false;
                $this->layout = 'ajax';
                $serverHost= $_SERVER['HTTP_HOST'];
                $serverURI = $_SERVER['REQUEST_URI'];
                $requestURL ="http://".$serverHost.$serverURI;
                $intentToSend = '';
                $params = explode($serverHost.'/A', $requestURL);
                $ua = $_SERVER['HTTP_USER_AGENT'];
                                
                if($this->isMobileDevice($ua)){ 
                        if(empty($params[1])){
                                $redirectFlag=true;
                        }else{
                                $fetchParams =   explode('/', $params[1]);
                                if(empty($fetchParams[0]) && empty($fetchParams[1])){
                                        $redirectFlag=true;
                                }else{
                                        if(sizeof($fetchParams) == 2 || empty($fetchParams[2])){
                                                $redirectFlag=true;
                                        }else if(sizeof($fetchParams)>4){
                                                $redirectFlag=true;
                                        }else{
                                                $campaignId = $fetchParams[1];
                                                $userId = $fetchParams[2];;                                             
                                                $campaignId = $this->num_decode($campaignId);
                                                $userId = $this->num_decode($userId);
                                                $notificationUrl='';
                                                $encryptedData=array();
                                                $url = Configure::read('nodeurl')."/getNotificationCampaignById?id=".$campaignId;
                                                $notificationData = $this->make_request($url,null);
                                                /*debug($notificationData);*/
                                                if($notificationData!=null){
                                                        if(!empty($notificationData)){
                                                                $notificationUrl = $notificationData['url'];
                                                        $notificationType = $notificationData['type'];
                                                        $notificationExpires = $notificationData['expiresat'];
                                                        $notificationStatus = $notificationData['status'];
                                                        $currentTime = time();                                          
                                                        if($notificationExpires < ($currentTime*1000) || $notificationStatus=='inactive'){
                                                                $expiryFlag=true;
                                                        }
                                                                if($notificationType=='native'){
                                                                        if(strtolower($notificationUrl)=='contact' || strtolower($notificationUrl)=='profile' ||strtolower($notificationUrl)=='tutorial'){
                                                                                $encryptUrl = $encryptUrl.base64_encode(strtolower($notificationUrl));
                                                                            $encryptedData = $this->make_request($encryptUrl,null);
                                                                        }else{
                                                                                $encryptedData = $this->defaultEncryptedMessage($userId);
                                                                        }
                                                                }else if ($notificationType=='update'){
                                                                                $encryptUrl = $encryptUrl.base64_encode('https://play.google.com/store/apps/details?id=com.saholic.profittill');
                                                                            $encryptedData = $this->make_request($encryptUrl,null);
                                                                }else{
                                                                        if(empty($notificationUrl)){
                                                                        $encryptedData = $this->defaultEncryptedMessage($userId);
                                                                }else if(strpos($notificationUrl,'?user_id=') !== false){
                                                                            $encryptUrl = $encryptUrl.base64_encode($notificationUrl);
                                                                            $encryptedData = $this->make_request($encryptUrl,null);
                                                                }else{
                                                                        $notificationUrl = $notificationUrl.'?user_id='.$userId;
                                                                        $encryptUrl = $encryptUrl.base64_encode($notificationUrl);
                                                                            $encryptedData = $this->make_request($encryptUrl,null);
                                                                }
                                                        }                                                                       
                                                        }
                                                        else{
                                                                $encryptedData = $this->defaultEncryptedMessage($userId);
                                                        }       
                                                }else{
                                                        $redirectFlag=true;
                                                }       
                                        }       
                                }
                        }
                        if($redirectFlag){
                                $this->redirect('http://www.profitmandi.com');
                        }else{
                                $intentToSend = 'intent://pm1.in?key='.urlencode($encryptedData['result']['value']).'&campaign='.$campaignId.'&intent_type='.$notificationType.'#Intent;scheme=profitmandiapp;package=com.saholic.profittill;end';                                                      
                                if($expiryFlag){
                                        $this->set(compact('intentToSend','expiryFlag'));
                                }else{
                                        header('Location: '.$intentToSend);
                                        exit;   
                                        }
                                }
                }
                else{
                        $this->redirect(array('action'=>'mobile'));
                }
        }

        function defaultEncryptedMessage($userId){
                $encryptUrl = 'http://104.200.25.40:8057/message/encrypt?type=encrypt&data=';
                $defaultEncryptedData =array();
                $defaultUrl = 'http://api.profittill.com/deals';
                $notificationUrl = $defaultUrl.'?user_id='.$userId;
            $encryptUrl = $encryptUrl.base64_encode($notificationUrl);
            $defaultEncryptedData = $this->make_request($encryptUrl,null);
            return $defaultEncryptedData;
        }
        function decrypt($encryptData){
                $encryptUrl = 'http://104.200.25.40:8057/message/encrypt?type=decrypt&data=';
                $encryptUrl = $encryptUrl.$encryptData;
                $encryptedData = $this->make_request($encryptUrl,null);
                return($encryptedData);
        }
        function num_decode($value){
                $alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
                $number=0;
            foreach(str_split($value) as $letter) {
                $number=($number*64) + strpos($alphabet,$letter);
            }
            return $number;
        }

        function num_encode($id){
                $alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
                $shortenedId = '';
        while($id>0) {
                $remainder = $id % 64;
                $id = ($id-$remainder) / 64;     
                $shortenedId = $alphabet{$remainder} . $shortenedId;
        }
        return $shortenedId;
        }

        function getDatabaseConnection(){
                $servername = "localhost";
                $username = "root";
                $password = "shop2020";
                $databaseName="dtr";
                $conn = mysql_connect($servername,$username,$password);
                if(!$conn){
                        return false;
                }else{
                        $databaseConnection = mysql_select_db($databaseName, $conn);
                        if(!$databaseConnection){
                                return false;
                        }else{
                                return $conn;           
                        }
                }
        }
        function error(){
                        $this->layout='ajax';
                        $name='Sorry';
                        $this->set(compact('name'));
        }

        public function mobile() {
                $this->layout='ajax';
        }
        function isMobileDevice($userAgent){
            $aMobileUA = array(
                // '/iphone/i' => 'iPhone', 
                // '/ipod/i' => 'iPod', 
                // '/ipad/i' => 'iPad', 
                '/android/i' => 'Android', 
                // '/blackberry/i' => 'BlackBerry', 
                // '/webos/i' => 'Mobile'
            );

            foreach($aMobileUA as $sMobileKey => $sMobileOS){
                if(preg_match($sMobileKey,$userAgent)){
                    return true;
                }
            }
            return false;
        }
}