Subversion Repositories SmartDukaan

Rev

Rev 14150 | Rev 14215 | 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
	}
13901 anikendra 183
	public function get_solr_result($q,$page) {
184
		$dealsperpage = Configure::read('dealsperpage');
185
		$offset = ($page - 1)*$dealsperpage;
13993 anikendra 186
		$cond = "$q";
13901 anikendra 187
	 	$sort = "store desc";
188
 
189
		$params = array(
190
			'conditions' =>array(
191
		 	'solr_query' => $cond
192
	 	),
193
		 	//'order' => $sort,
194
		 	'offset' => $offset,
195
		 	'limit' => $dealsperpage
196
	 	);
197
		$this->loadModel('Solr');
13993 anikendra 198
		// debug($params);
13901 anikendra 199
		$solroutput = $this->Solr->find('all', $params);
200
		$result = array();
201
		if(!empty($solroutput['Solr'])) {			
202
			$skuMap = array();
203
			foreach ($solroutput['Solr'] as $key => $value) {										
204
				$skuMap[$value['id']] = $value;
205
				$result[$value['skuBundleId']][$value['id']] = $value['available_price'];
206
			}				
207
			foreach ($result as $key => $value) {
208
				asort($value);
209
				$lowestPriceSku = key($value);
210
				$result[$key] = $skuMap[$lowestPriceSku];
211
			}
212
		}
213
		return $result;
214
	}
14098 anikendra 215
 
216
	public function admin_update(){
217
		$this->response->type('json');
218
		$this->layout = 'ajax';
219
		$data[$this->request->data['id']] = $this->request->data['value'];
220
		$data['oid'] = $this->request->data['oid'];
221
		// $data['class'] = $this->request->data['class'];
222
		if($this->modelClass == 'Exceptionalskudiscount') {
223
			$data['class'] = 'SkuDiscountInfo';	
224
		}elseif($this->modelClass == 'Skuscheme'){
225
			$data['class'] = 'SkuSchemeDetails';
226
		}else{
227
			$data['class'] = $this->modelClass;
228
		}		
229
		$data_string = json_encode($data,JSON_NUMERIC_CHECK);
230
		$ch = curl_init();
231
		$url = $this->apihost.'Catalog/updateCollection';
232
		$this->log("[url] $url",'api');
233
		$this->log("[fields] ".print_r($data_string,1),'api');
234
		curl_setopt($ch, CURLOPT_URL, $url);
235
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
236
		curl_setopt($ch, CURLOPT_POST, true);
237
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); // note the PUT here
238
 
239
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
240
		curl_setopt($ch, CURLOPT_HEADER, true);
241
 
242
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
243
		    'Content-Type: application/json',                                                                                
244
		    'Content-Length: ' . strlen($data_string)                                                                       
245
		));       
246
 
247
		// execute the request
248
 
249
		$output = curl_exec($ch);
250
		$result = $this->request->data['value'];
251
		$this->log("[response] ".print_r($output,1),'api');
252
		curl_close($ch);
253
		$this->set(array(
254
		    'result' => $result,
255
		    '_serialize' => array('result')
256
		));
257
		$this->render('/Elements/json');
258
	}
14150 anikendra 259
 
260
	function getAutoLoginUrl($userId,$next) {
261
		$this->loadModel('User');
262
		$this->User->Behaviors->attach('Containable');
14166 anikendra 263
		$options = array('contain'=>array('UserAccount'), 'conditions'=>array('User.id'=>$userId),'fields'=>array('username','email'),'recursive'=>-1);
14150 anikendra 264
		$user = $this->User->find('first',$options);
14166 anikendra 265
		$this->log("user_accounts ".print_r($user,1));
266
		$data = array('email'=>$user['User']['email'],'Id'=>$user['UserAccount'][0]['account_key'],'cartId' => $user['UserAccount'][1]['account_key'],'isPrivateDealUser'=>1);
14150 anikendra 267
		$data = '?data='.base64_encode(serialize($data));
268
		$token = '&token='.md5(Configure::read('saholicapikey').'|'.$user['UserAccount'][0]['account_key']);		
269
		return Configure::read('saholicauthurl').$data.$token.$next;
270
	}
13532 anikendra 271
}