Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
13532 anikendra 1
<?php
2
App::uses('Controller', 'Controller');
3
 
4
/**
5
 * Application Controller
6
 *
7
 * Add your application-wide methods in the class below, your controllers
8
 * will inherit them.
9
 *
10
 * @package       app.Controller
11
 * @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
12
 */
13
class AppController extends Controller {
13808 anikendra 14
 
15
	public $limit;
16
	public $apihost;
17
 
13532 anikendra 18
	public $components = array(
19
		'Session',
20
		'Auth' => array(
21
			'loginAction' => array('controller' => 'users', 'action' => 'login'),
22
			'allowedActions' => array('index', 'view', 'display')
23
		)			
24
	);
13808 anikendra 25
 
13532 anikendra 26
	var $helpers = array('Session', 'Form', 'Html');
27
	var $keywords = array('instagram followers','instagram button','instagram follow back','instagram tool','instagram automation','free istagram followers','instagram stats','instagram follow button');
28
 
29
	function beforeFilter() {
13659 anikendra 30
		$this->Auth->autoRedirect = false;		
13579 anikendra 31
 
32
		//Set config settings according to domain
13532 anikendra 33
		// get host name from URL
34
		preg_match('@^(?:http://)?([^/]+)@i',$_SERVER['HTTP_HOST'], $matches);
35
		$host = $matches[1];
36
		switch($host){			
13567 anikendra 37
			case 'localdtr':
13532 anikendra 38
				Configure::load('dev');
39
				break;
13946 anikendra 40
			case 'staging.profittill.com':
41
			case 'www.staging.profittill.com':
13944 anikendra 42
				Configure::load('staging');
43
				break;
13532 anikendra 44
			default:
13567 anikendra 45
			case 'www.profittill.com':
46
			case 'profittill.com':
13633 anikendra 47
			case 'api.profittill.com':
13532 anikendra 48
				Configure::load('live');
49
				break;
50
		}
13579 anikendra 51
		$facebookConfig = Configure::read("Facebook");		
52
		$categories = Configure::read('Categories');
13532 anikendra 53
		//Facebook configuration
54
		$this->set('fbappid', $facebookConfig['fbappid']);
13579 anikendra 55
		$this->set('apihost', Configure::read('apihost'));
56
 
13532 anikendra 57
	   	$sessionState = $this->Session->read('state');
58
		if(!isset($sessionState)){
59
			$this->Session->write('state' , md5(uniqid(rand(), TRUE))); // CSRF protection
60
		}
61
	 	$dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" 
62
		   . $facebookConfig['fbappid'] . "&redirect_uri=" . urlencode($facebookConfig['base_url'].'/users/checkfbuser/') . "&state="
63
		   . $this->Session->read('state').'&scope=publish_stream,email,user_birthday,publish_actions,user_location';
64
	   	$this->set('dialog_url', $dialog_url);
65
		$this->set('description','Why spend money when you can get something for free');
13579 anikendra 66
		$this->set('categories',$categories);
13532 anikendra 67
		if(isset($this->params['admin'])) {
13739 anikendra 68
			$this->layout = 'admin';
13808 anikendra 69
		}	
70
		$this->apihost = Configure::read('pythonapihost');
71
		$this->limit = Configure::read('dealsperpage');	
13685 anikendra 72
		$staticVersion = Configure::read('staticversion');
73
		$this->set('staticversion',$staticVersion);
13739 anikendra 74
		$this->set('requiremobileverification',Configure::read('requiremobileverification'));
13532 anikendra 75
    }
76
 
77
    function isAuthorized() {
78
        return $this->Auth->user('id');
79
    }
80
 
81
    function isFbAuthorized() {
82
        return $this->Session->read('facebook_id');
83
    }
84
 
85
    function afterFilter() {
13579 anikendra 86
		$result['ucadcode'] = $this->ucadcode;
13532 anikendra 87
    }
88
 
13659 anikendra 89
    function beforeRender() {   
13736 anikendra 90
    	$logged_user = $this->Auth->user();
91
    	$this->set('logged_user', $logged_user); 	
13579 anikendra 92
        $this->set('base_url', 'http://' . $_SERVER['SERVER_NAME'] . Router::url('/'));
13532 anikendra 93
    }
94
 
13736 anikendra 95
    function checkMobileNumber() {
96
    	$logged_user = $this->Auth->user();
97
    	if(empty($logged_user['mobile_verified']) && $this->params['controller'] !='users') {
98
			$skipmobileverification = $this->Session->read('skipmobileverification');
99
			if(!isset($skipmobileverification) || empty($skipmobileverification)) {
100
				$this->redirect('/users/verifymobile');
101
			}
102
		}
103
    }
104
 
13659 anikendra 105
    function getallheaders() { 
106
	   $headers = ''; 
107
       foreach ($_SERVER as $name => $value) 
108
       { 
109
	   if (substr($name, 0, 5) == 'HTTP_') 
110
	   { 
111
	       $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; 
112
	   } 
113
       } 
114
       return $headers; 
115
    } 
13633 anikendra 116
 
13808 anikendra 117
    public function getDealsApiUrl($page=1,$userId = null,$categoryId=0,$sort=null,$direction=null){
118
    	$this->log('categoryId '.$categoryId,'api');
119
    	$this->log('page '.$page,'api');
120
    	$offset = ($page - 1) * $this->limit;
121
    	if(isset($sort) && !empty($sort) && $sort!=-1){
122
    		$url = $this->apihost.'deals/'.$userId.'?categoryId='.$categoryId.'&sort='.$sort.'&direction='.$direction.'&limit='.$this->limit.'&offset='.$offset;
123
    	}else{
124
    		$url = $this->apihost.'deals/'.$userId.'?categoryId='.$categoryId.'&limit='.$this->limit.'&offset='.$offset;
125
    	}    	
126
    	return $url;
127
    }
128
 
13633 anikendra 129
	function make_request($url,$fields,$format='json'){
13683 anikendra 130
		$this->log("[url] $url",'api');
131
		$this->log("[fields] ".print_r($fields,1),'api');
13633 anikendra 132
		$fields_string = '';
133
		//open connection
134
		$ch = curl_init();
135
		//set the url, number of POST vars, POST data
136
		curl_setopt($ch,CURLOPT_URL, $url);
137
		curl_setopt($ch,CURLOPT_RETURNTRANSFER , true);
138
		if(!empty($fields)) {
139
			curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
140
			curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
141
			    'Content-Type: application/json',                                                                                
13994 anikendra 142
			    // 'Content-Length: ' . sizeof($fields))                                                                       
143
			    'Content-Length: ' . strlen($fields))                                                                       
13633 anikendra 144
			);   
145
		}
146
		//execute post
147
		$result = curl_exec($ch);
13946 anikendra 148
		$this->log("[response] ".print_r($result,1),'api');
13633 anikendra 149
		//close connection
150
		curl_close($ch);
151
		switch($format){
152
			case 'json':
153
			$response = json_decode($result,1);
154
			break;
155
		}
156
		return $response;	
157
	}
13901 anikendra 158
 
14016 anikendra 159
	function post_request($url,$fields,$format='json'){
160
		$this->log("[url] $url",'api');
161
		$this->log("[fields] ".print_r($fields,1),'api');
162
		$fields_string = '';
163
		//open connection
164
		$ch = curl_init();
165
		//execute post
166
		foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
167
		rtrim($fields_string, '&');
168
		//set the url, number of POST vars, POST data
169
		curl_setopt($ch,CURLOPT_URL, $url);
170
		curl_setopt($ch,CURLOPT_POST, count($fields));
171
		curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
172
		$result = curl_exec($ch);
173
		$this->log("[response] ".print_r($result,1),'api');
174
		//close connection
175
		curl_close($ch);
176
		switch($format){
177
			case 'json':
178
			$response = json_decode($result,1);
179
			break;
180
		}
181
		return $response;	
182
	}
14215 anikendra 183
 
13901 anikendra 184
	public function get_solr_result($q,$page) {
185
		$dealsperpage = Configure::read('dealsperpage');
186
		$offset = ($page - 1)*$dealsperpage;
13993 anikendra 187
		$cond = "$q";
13901 anikendra 188
	 	$sort = "store desc";
189
 
190
		$params = array(
191
			'conditions' =>array(
192
		 	'solr_query' => $cond
193
	 	),
194
		 	//'order' => $sort,
195
		 	'offset' => $offset,
196
		 	'limit' => $dealsperpage
197
	 	);
14215 anikendra 198
		$this->loadModel('Solr');		
13901 anikendra 199
		$solroutput = $this->Solr->find('all', $params);
200
		$result = array();
14215 anikendra 201
		if(sizeof($solroutput)<$dealsperpage){
202
			$hasMore = false;
203
		}else{
204
			$hasMore = true;
205
		}
13901 anikendra 206
		if(!empty($solroutput['Solr'])) {			
207
			$skuMap = array();
14215 anikendra 208
			foreach ($solroutput['Solr'] as $key => $value) {
14432 anikendra 209
				// if(!$value['in_stock'])continue;
13901 anikendra 210
				$skuMap[$value['id']] = $value;
211
				$result[$value['skuBundleId']][$value['id']] = $value['available_price'];
14215 anikendra 212
			}	
213
			if(!empty($result)) {
214
				foreach ($result as $key => $value) {					
215
					asort($value);
216
					$lowestPriceSku = key($value);
217
					$result[$key] = $skuMap[$lowestPriceSku];
218
				}
13901 anikendra 219
			}
14215 anikendra 220
		}		
221
		$result['hasMore'] = $hasMore;
13901 anikendra 222
		return $result;
223
	}
14098 anikendra 224
 
225
	public function admin_update(){
226
		$this->response->type('json');
227
		$this->layout = 'ajax';
228
		$data[$this->request->data['id']] = $this->request->data['value'];
229
		$data['oid'] = $this->request->data['oid'];
230
		// $data['class'] = $this->request->data['class'];
231
		if($this->modelClass == 'Exceptionalskudiscount') {
232
			$data['class'] = 'SkuDiscountInfo';	
233
		}elseif($this->modelClass == 'Skuscheme'){
234
			$data['class'] = 'SkuSchemeDetails';
14426 anikendra 235
		}elseif($this->modelClass == 'Exceptionalnlc'){
236
			$data['class'] = 'ExceptionalNlc';
237
		}
238
		else{
14098 anikendra 239
			$data['class'] = $this->modelClass;
240
		}		
241
		$data_string = json_encode($data,JSON_NUMERIC_CHECK);
242
		$ch = curl_init();
243
		$url = $this->apihost.'Catalog/updateCollection';
244
		$this->log("[url] $url",'api');
245
		$this->log("[fields] ".print_r($data_string,1),'api');
246
		curl_setopt($ch, CURLOPT_URL, $url);
247
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
248
		curl_setopt($ch, CURLOPT_POST, true);
249
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); // note the PUT here
250
 
251
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
252
		curl_setopt($ch, CURLOPT_HEADER, true);
253
 
254
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
255
		    'Content-Type: application/json',                                                                                
256
		    'Content-Length: ' . strlen($data_string)                                                                       
257
		));       
258
 
259
		// execute the request
260
 
261
		$output = curl_exec($ch);
262
		$result = $this->request->data['value'];
263
		$this->log("[response] ".print_r($output,1),'api');
264
		curl_close($ch);
265
		$this->set(array(
266
		    'result' => $result,
267
		    '_serialize' => array('result')
268
		));
269
		$this->render('/Elements/json');
270
	}
14150 anikendra 271
 
272
	function getAutoLoginUrl($userId,$next) {
273
		$this->loadModel('User');
274
		$this->User->Behaviors->attach('Containable');
14166 anikendra 275
		$options = array('contain'=>array('UserAccount'), 'conditions'=>array('User.id'=>$userId),'fields'=>array('username','email'),'recursive'=>-1);
14150 anikendra 276
		$user = $this->User->find('first',$options);
14166 anikendra 277
		$this->log("user_accounts ".print_r($user,1));
278
		$data = array('email'=>$user['User']['email'],'Id'=>$user['UserAccount'][0]['account_key'],'cartId' => $user['UserAccount'][1]['account_key'],'isPrivateDealUser'=>1);
14150 anikendra 279
		$data = '?data='.base64_encode(serialize($data));
280
		$token = '&token='.md5(Configure::read('saholicapikey').'|'.$user['UserAccount'][0]['account_key']);		
281
		return Configure::read('saholicauthurl').$data.$token.$next;
282
	}
13532 anikendra 283
}