Subversion Repositories SmartDukaan

Rev

Rev 18142 | Rev 18192 | 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);
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();
18148 manas 79
					    		if(!strtotime($notificationExpires)){
80
					    			$notificationExpires='2038-01-19 03:14:07';
81
					    		}
82
					    		//debug($currentTime);
83
					    		//debug($notificationExpires);
18135 manas 84
					    		if(strtotime($notificationExpires) < $currentTime || $notificationStatus=='inactive'){
85
					    			//debug('Notification has expired');
86
					    			$expiryFlag=true;
87
					    		}
88
								if($notificationType=='native'){
89
									if(strtolower($notificationUrl)=='contact' || strtolower($notificationUrl)=='profile' ||strtolower($notificationUrl)=='tutorial'){
90
										//debug('V,W,Ir,aH,-y');
91
										$encryptUrl = $encryptUrl.base64_encode(strtolower($notificationUrl));
92
									    $encryptedData = $this->make_request($encryptUrl,null);
93
									}else{
94
										$encryptedData = $this->defaultEncryptedMessage($userId);
95
									}
96
								}else if ($notificationType=='update'){
97
										//debug('K3');
98
										$encryptUrl = $encryptUrl.base64_encode('https://play.google.com/store/apps/details?id=com.saholic.profittill');
99
									    $encryptedData = $this->make_request($encryptUrl,null);
100
								}else{
101
						    		//debug($notificationUrl);
102
									if(empty($notificationUrl)){
103
					    				debug('B');
104
					    				$encryptedData = $this->defaultEncryptedMessage($userId);
105
						    		}else if(strpos($notificationUrl,'?user_id=') !== false){
106
									    $encryptUrl = $encryptUrl.base64_encode($notificationUrl);
107
									    $encryptedData = $this->make_request($encryptUrl,null);
108
						    		}else{
109
						    			$notificationUrl = $notificationUrl.'?user_id='.$userId;
110
						    			$encryptUrl = $encryptUrl.base64_encode($notificationUrl);
111
									    $encryptedData = $this->make_request($encryptUrl,null);
112
						    		}
113
						    	}		
114
 
115
							}
116
					    }
117
					}else{
118
						$encryptedData = $this->defaultEncryptedMessage($userId);
119
					}		
120
				}	
121
			}
122
		}
123
		if($redirectFlag){
18148 manas 124
			$this->redirect(array('action'=>'error'));
18135 manas 125
		}else{
18148 manas 126
			//debug($expiryFlag);
18135 manas 127
			$intentToSend = 'intent://pmapp.com?key='.urlencode($encryptedData['result']['value']).'&campaign='.$campaignId.'&intent_type='.$notificationType.'#Intent;scheme=profitmandiapp;package=com.saholic.profittill;end';							
18148 manas 128
			if($expiryFlag){
129
				$this->set(compact('intentToSend','expiryFlag'));
130
			}else{
131
				header('Location: '.$intentToSend);
132
				exit;	
133
			}
18135 manas 134
		}
135
	}
136
 
137
	function defaultEncryptedMessage($userId){
138
		$encryptUrl = 'http://shop2020.in:8057/message/encrypt?type=encrypt&data=';
139
		$defaultEncryptedData =array();
140
		$defaultUrl = 'http://api.profittill.com/deals';
141
		$notificationUrl = $defaultUrl.'?user_id='.$userId;
142
	    $encryptUrl = $encryptUrl.base64_encode($notificationUrl);
143
	    $defaultEncryptedData = $this->make_request($encryptUrl,null);
144
	    return $defaultEncryptedData;
145
	}
146
	function decrypt($encryptData){
147
		$encryptUrl = 'http://shop2020.in:8057/message/encrypt?type=decrypt&data=';
148
		$encryptUrl = $encryptUrl.$encryptData;
149
		$encryptedData = $this->make_request($encryptUrl,null);
150
		return($encryptedData);
151
	}
152
	function num_decode($value){
153
		$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
154
		$number=0;
155
	    foreach(str_split($value) as $letter) {
156
	        $number=($number*64) + strpos($alphabet,$letter);
157
	    }
158
	    return $number;
159
	}
160
 
161
	function num_encode($id){
162
		$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
163
		$shortenedId = '';
164
    	while($id>0) {
165
	        $remainder = $id % 64;
166
	        $id = ($id-$remainder) / 64;     
167
	        $shortenedId = $alphabet{$remainder} . $shortenedId;
168
    	}
169
    	return $shortenedId;
170
	}
171
 
172
	function getDatabaseConnection(){
173
		$servername = "104.200.25.40";
174
		$username = "root";
175
		$password = "shop2020";
176
		$databaseName="dtr";
177
		$conn = mysql_connect($servername,$username,$password);
178
		if(!$conn){
179
			return false;
180
		}else{
181
			$databaseConnection = mysql_select_db($databaseName, $conn);
182
			if(!$databaseConnection){
183
				return false;
184
			}else{
185
				return $conn;		
186
			}
187
		}
188
	}
18148 manas 189
	function error(){
190
			$this->layout='ajax';
191
			$name='Sorry';
192
			$this->set(compact('name'));
193
	}
18135 manas 194
}