Subversion Repositories SmartDukaan

Rev

Rev 18192 | Rev 19257 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18135 manas 1
<?php
2
App::uses('AppController', 'Controller');
3
/**
4
 * A Controller
5
 *
6
 * @property A $A
7
 * @property PaginatorComponent $Paginator
8
 */
9
class AController extends AppController {
10
 
11
/**
12
 * Components
13
 *
14
 * @var array
15
 */
18208 manas 16
 
18135 manas 17
	public $components = array('Paginator');
18208 manas 18
		    public function beforeFilter() {
19
		    	parent::beforeFilter();
20
		    	$this->Auth->allow('index','mobile','error');
21
		    }
18135 manas 22
 
23
		public function index() {
18208 manas 24
		$encryptUrl = 'http://104.200.25.40:8057/message/encrypt?type=encrypt&data=';
18135 manas 25
		$expiryFlag=false;
26
		$redirectFlag=false;
18148 manas 27
		$this->layout = 'ajax';
18135 manas 28
		$serverHost= $_SERVER['HTTP_HOST'];
29
		$serverURI = $_SERVER['REQUEST_URI'];
30
		$requestURL ="http://".$serverHost.$serverURI;
31
		$intentToSend = '';
18208 manas 32
		$params = explode($serverHost.'/A', $requestURL);
18192 manas 33
		$ua = $_SERVER['HTTP_USER_AGENT'];
34
 
35
		if($this->isMobileDevice($ua)){	
36
			if(empty($params[1])){
18135 manas 37
				$redirectFlag=true;
38
			}else{
18192 manas 39
				$fetchParams = 	 explode('/', $params[1]);
40
				if(empty($fetchParams[0]) && empty($fetchParams[1])){
18135 manas 41
					$redirectFlag=true;
42
				}else{
18192 manas 43
					if(sizeof($fetchParams) == 2 || empty($fetchParams[2])){
44
						$redirectFlag=true;
45
					}else if(sizeof($fetchParams)>4){
46
						$redirectFlag=true;
47
					}else{
48
						$campaignId = $fetchParams[1];
49
						$userId = $fetchParams[2];;
50
 
51
						$campaignId = $this->num_decode($campaignId);
52
						$userId = $this->num_decode($userId);
53
						$notificationUrl='';
54
						$encryptedData=array();
55
						$conn=$this->getDatabaseConnection();
56
						if($conn){
57
							$sql    = 'SELECT * FROM notification_campaigns WHERE id ='.$campaignId;
58
							$result = mysql_query($sql, $conn);
59
							if (mysql_num_rows($result)==0) { 
60
								$redirectFlag=true;
61
							}else{
62
								while ($row = mysql_fetch_assoc($result)) {
63
						    		$notificationUrl = $row['url'];
64
						    		$notificationType = $row['type'];
65
						    		$notificationExpires = $row['expiresat'];
66
						    		$notificationStatus = $row['status'];
67
						    		$currentTime = time();
68
						    		if(!strtotime($notificationExpires)){
69
						    			$notificationExpires='2038-01-19 03:14:07';
70
						    		}
71
						    		if(strtotime($notificationExpires) < $currentTime || $notificationStatus=='inactive'){
72
						    			$expiryFlag=true;
73
						    		}
74
									if($notificationType=='native'){
75
										if(strtolower($notificationUrl)=='contact' || strtolower($notificationUrl)=='profile' ||strtolower($notificationUrl)=='tutorial'){
76
											$encryptUrl = $encryptUrl.base64_encode(strtolower($notificationUrl));
77
										    $encryptedData = $this->make_request($encryptUrl,null);
78
										}else{
79
											$encryptedData = $this->defaultEncryptedMessage($userId);
80
										}
81
									}else if ($notificationType=='update'){
82
											$encryptUrl = $encryptUrl.base64_encode('https://play.google.com/store/apps/details?id=com.saholic.profittill');
83
										    $encryptedData = $this->make_request($encryptUrl,null);
84
									}else{
85
										if(empty($notificationUrl)){
86
						    				$encryptedData = $this->defaultEncryptedMessage($userId);
87
							    		}else if(strpos($notificationUrl,'?user_id=') !== false){
88
										    $encryptUrl = $encryptUrl.base64_encode($notificationUrl);
89
										    $encryptedData = $this->make_request($encryptUrl,null);
90
							    		}else{
91
							    			$notificationUrl = $notificationUrl.'?user_id='.$userId;
92
							    			$encryptUrl = $encryptUrl.base64_encode($notificationUrl);
93
										    $encryptedData = $this->make_request($encryptUrl,null);
94
							    		}
95
							    	}		
96
 
97
								}
98
						    }
18135 manas 99
						}else{
18192 manas 100
							$encryptedData = $this->defaultEncryptedMessage($userId);
101
						}		
102
					}	
103
				}
18135 manas 104
			}
18192 manas 105
 
106
			if($redirectFlag){
107
				$this->redirect(array('action'=>'error'));
18148 manas 108
			}else{
18208 manas 109
				$intentToSend = 'intent://pm1.in?key='.urlencode($encryptedData['result']['value']).'&campaign='.$campaignId.'&intent_type='.$notificationType.'#Intent;scheme=profitmandiapp;package=com.saholic.profittill;end';							
18192 manas 110
				if($expiryFlag){
111
					$this->set(compact('intentToSend','expiryFlag'));
112
				}else{
113
					header('Location: '.$intentToSend);
114
					exit;	
115
					}
116
				}
18135 manas 117
		}
18192 manas 118
		else{
119
			$this->redirect(array('action'=>'mobile'));
120
		}
18135 manas 121
	}
122
 
123
	function defaultEncryptedMessage($userId){
18208 manas 124
		$encryptUrl = 'http://104.200.25.40:8057/message/encrypt?type=encrypt&data=';
18135 manas 125
		$defaultEncryptedData =array();
126
		$defaultUrl = 'http://api.profittill.com/deals';
127
		$notificationUrl = $defaultUrl.'?user_id='.$userId;
128
	    $encryptUrl = $encryptUrl.base64_encode($notificationUrl);
129
	    $defaultEncryptedData = $this->make_request($encryptUrl,null);
130
	    return $defaultEncryptedData;
131
	}
132
	function decrypt($encryptData){
18208 manas 133
		$encryptUrl = 'http://104.200.25.40:8057/message/encrypt?type=decrypt&data=';
18135 manas 134
		$encryptUrl = $encryptUrl.$encryptData;
135
		$encryptedData = $this->make_request($encryptUrl,null);
136
		return($encryptedData);
137
	}
138
	function num_decode($value){
139
		$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
140
		$number=0;
141
	    foreach(str_split($value) as $letter) {
142
	        $number=($number*64) + strpos($alphabet,$letter);
143
	    }
144
	    return $number;
145
	}
146
 
147
	function num_encode($id){
148
		$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
149
		$shortenedId = '';
150
    	while($id>0) {
151
	        $remainder = $id % 64;
152
	        $id = ($id-$remainder) / 64;     
153
	        $shortenedId = $alphabet{$remainder} . $shortenedId;
154
    	}
155
    	return $shortenedId;
156
	}
157
 
158
	function getDatabaseConnection(){
18208 manas 159
		$servername = "localhost";
18135 manas 160
		$username = "root";
161
		$password = "shop2020";
162
		$databaseName="dtr";
163
		$conn = mysql_connect($servername,$username,$password);
164
		if(!$conn){
165
			return false;
166
		}else{
167
			$databaseConnection = mysql_select_db($databaseName, $conn);
168
			if(!$databaseConnection){
169
				return false;
170
			}else{
171
				return $conn;		
172
			}
173
		}
174
	}
18148 manas 175
	function error(){
176
			$this->layout='ajax';
177
			$name='Sorry';
178
			$this->set(compact('name'));
179
	}
18192 manas 180
 
181
	public function mobile() {
182
		$this->layout='ajax';
183
	}
184
	function isMobileDevice($userAgent){
185
	    $aMobileUA = array(
186
	        // '/iphone/i' => 'iPhone', 
187
	        // '/ipod/i' => 'iPod', 
188
	        // '/ipad/i' => 'iPad', 
189
	        '/android/i' => 'Android', 
190
	        // '/blackberry/i' => 'BlackBerry', 
191
	        // '/webos/i' => 'Mobile'
192
	    );
193
 
194
	    foreach($aMobileUA as $sMobileKey => $sMobileOS){
195
	        if(preg_match($sMobileKey,$userAgent)){
196
	            return true;
197
	        }
198
	    }
199
	    return false;
200
	}
18208 manas 201
}