Subversion Repositories SmartDukaan

Rev

Rev 18142 | Rev 18192 | Go to most recent revision | 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 index() {
                $encryptUrl = 'http://shop2020.in: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);
                        
                if(empty($params[1])){
                        debug('Redirect to the 404 page condition 1');
                        echo('Redirect to the 404 page condition 1');
                        $redirectFlag=true;
                        //Redirect to the 404 page
                }else{
                        $fetchParams =   explode('/', $params[1]);
                        //debug($fetchParams);
                        if(empty($fetchParams[0]) && empty($fetchParams[1])){
                                debug('Redirect to the 404 page condition 2');
                                echo('Redirect to the 404 page condition 2');
                                $redirectFlag=true;
                                //Redirect to the 404 page      
                        }else{
                                if(sizeof($fetchParams) == 2 || empty($fetchParams[2])){
                                        debug('Redirect to the 404 page condition 3');
                                        echo('Redirect to the 404 page condition 3');
                                        $redirectFlag=true;
                                        //Redirect to the 404 page              
                                }else if(sizeof($fetchParams)>4){
                                        debug('Redirect to the 404 page condition 4');
                                        echo('Redirect to the 404 page condition 4');
                                        $redirectFlag=true;
                                        //Redirect to the 404 page              
                                }else{
                                        $campaignId = $fetchParams[1];
                                        $userId = $fetchParams[2];;
                                        
                                        $campaignId = $this->num_decode($campaignId);
                                        $userId = $this->num_decode($userId);
                                        /*debug($campaignId);
                                        debug($userId);*/
                                        $notificationUrl='';
                                        $encryptedData=array();
                                        $conn=$this->getDatabaseConnection();
                                        if($conn){
                                                $sql    = 'SELECT * FROM notification_campaigns WHERE id ='.$campaignId;
                                                $result = mysql_query($sql, $conn);
                                                if (mysql_num_rows($result)==0) { 
                                                        debug('Zero rows');
                                                        echo('Zero rows');
                                                        $redirectFlag=true;
                                                }else{
                                                        while ($row = mysql_fetch_assoc($result)) {
                                                                //debug($row);
                                                        $notificationUrl = $row['url'];
                                                        $notificationType = $row['type'];
                                                        $notificationExpires = $row['expiresat'];
                                                        $notificationStatus = $row['status'];
                                                        $currentTime = time();
                                                        if(!strtotime($notificationExpires)){
                                                                $notificationExpires='2038-01-19 03:14:07';
                                                        }
                                                        //debug($currentTime);
                                                        //debug($notificationExpires);
                                                        if(strtotime($notificationExpires) < $currentTime || $notificationStatus=='inactive'){
                                                                //debug('Notification has expired');
                                                                $expiryFlag=true;
                                                        }
                                                                if($notificationType=='native'){
                                                                        if(strtolower($notificationUrl)=='contact' || strtolower($notificationUrl)=='profile' ||strtolower($notificationUrl)=='tutorial'){
                                                                                //debug('V,W,Ir,aH,-y');
                                                                                $encryptUrl = $encryptUrl.base64_encode(strtolower($notificationUrl));
                                                                            $encryptedData = $this->make_request($encryptUrl,null);
                                                                        }else{
                                                                                $encryptedData = $this->defaultEncryptedMessage($userId);
                                                                        }
                                                                }else if ($notificationType=='update'){
                                                                                //debug('K3');
                                                                                $encryptUrl = $encryptUrl.base64_encode('https://play.google.com/store/apps/details?id=com.saholic.profittill');
                                                                            $encryptedData = $this->make_request($encryptUrl,null);
                                                                }else{
                                                                //debug($notificationUrl);
                                                                        if(empty($notificationUrl)){
                                                                        debug('B');
                                                                        $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);
                                        }               
                                }       
                        }
                }
                if($redirectFlag){
                        $this->redirect(array('action'=>'error'));
                }else{
                        //debug($expiryFlag);
                        $intentToSend = 'intent://pmapp.com?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;   
                        }
                }
        }

        function defaultEncryptedMessage($userId){
                $encryptUrl = 'http://shop2020.in: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://shop2020.in: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 = "104.200.25.40";
                $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'));
        }
}