Subversion Repositories SmartDukaan

Rev

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