Subversion Repositories SmartDukaan

Rev

Rev 13946 | Rev 13993 | 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',                                                                                
13992 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
 
159
	public function get_solr_result($q,$page) {
160
		$dealsperpage = Configure::read('dealsperpage');
161
		$offset = ($page - 1)*$dealsperpage;
162
		$cond = "q=$q";
163
	 	$sort = "store desc";
164
 
165
		$params = array(
166
			'conditions' =>array(
167
		 	'solr_query' => $cond
168
	 	),
169
		 	//'order' => $sort,
170
		 	'offset' => $offset,
171
		 	'limit' => $dealsperpage
172
	 	);
173
		$this->loadModel('Solr');
174
		$solroutput = $this->Solr->find('all', $params);
175
		$result = array();
176
		if(!empty($solroutput['Solr'])) {			
177
			$skuMap = array();
178
			foreach ($solroutput['Solr'] as $key => $value) {										
179
				$skuMap[$value['id']] = $value;
180
				$result[$value['skuBundleId']][$value['id']] = $value['available_price'];
181
			}				
182
			foreach ($result as $key => $value) {
183
				asort($value);
184
				$lowestPriceSku = key($value);
185
				$result[$key] = $skuMap[$lowestPriceSku];
186
			}
187
		}
188
		return $result;
189
	}
13532 anikendra 190
}