Subversion Repositories SmartDukaan

Rev

Rev 18141 | Go to most recent revision | Details | 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';							
125
			$this->set(compact('intentToSend','expiryFlag'));
126
			//debug($intentToSend);
127
			//debug($expiryFlag);
128
		}
129
	}
130
 
131
	function defaultEncryptedMessage($userId){
132
		$encryptUrl = 'http://shop2020.in:8057/message/encrypt?type=encrypt&data=';
133
		$defaultEncryptedData =array();
134
		$defaultUrl = 'http://api.profittill.com/deals';
135
		// if(empty($userId)){
136
		// 	$notificationUrl = $defaultUrl;
137
		// }else{
138
		// 	$notificationUrl = $defaultUrl.'?user_id='.$userId;
139
		// }
140
		$notificationUrl = $defaultUrl.'?user_id='.$userId;
141
	    $encryptUrl = $encryptUrl.base64_encode($notificationUrl);
142
	    $defaultEncryptedData = $this->make_request($encryptUrl,null);
143
	    return $defaultEncryptedData;
144
	}
145
	function decrypt($encryptData){
146
		$encryptUrl = 'http://shop2020.in:8057/message/encrypt?type=decrypt&data=';
147
		$encryptUrl = $encryptUrl.$encryptData;
148
		$encryptedData = $this->make_request($encryptUrl,null);
149
		return($encryptedData);
150
	}
151
	function num_decode($value){
152
		$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
153
		$number=0;
154
	    foreach(str_split($value) as $letter) {
155
	        $number=($number*64) + strpos($alphabet,$letter);
156
	    }
157
	    return $number;
158
	}
159
 
160
	function num_encode($id){
161
		$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
162
		$shortenedId = '';
163
    	while($id>0) {
164
	        $remainder = $id % 64;
165
	        $id = ($id-$remainder) / 64;     
166
	        $shortenedId = $alphabet{$remainder} . $shortenedId;
167
    	}
168
    	return $shortenedId;
169
	}
170
 
171
	function getDatabaseConnection(){
172
		$servername = "104.200.25.40";
173
		$username = "root";
174
		$password = "shop2020";
175
		$databaseName="dtr";
176
		$conn = mysql_connect($servername,$username,$password);
177
		if(!$conn){
178
			return false;
179
		}else{
180
			$databaseConnection = mysql_select_db($databaseName, $conn);
181
			if(!$databaseConnection){
182
				return false;
183
			}else{
184
				return $conn;		
185
			}
186
		}
187
	}
188
}