Subversion Repositories SmartDukaan

Rev

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