Subversion Repositories SmartDukaan

Rev

Rev 13808 | Rev 13944 | 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;
40
			default:
13567 anikendra 41
			case 'www.profittill.com':
42
			case 'profittill.com':
13633 anikendra 43
			case 'api.profittill.com':
13532 anikendra 44
				Configure::load('live');
45
				break;
46
		}
13579 anikendra 47
		$facebookConfig = Configure::read("Facebook");		
48
		$categories = Configure::read('Categories');
13532 anikendra 49
		//Facebook configuration
50
		$this->set('fbappid', $facebookConfig['fbappid']);
13579 anikendra 51
		$this->set('apihost', Configure::read('apihost'));
52
 
13532 anikendra 53
	   	$sessionState = $this->Session->read('state');
54
		if(!isset($sessionState)){
55
			$this->Session->write('state' , md5(uniqid(rand(), TRUE))); // CSRF protection
56
		}
57
	 	$dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" 
58
		   . $facebookConfig['fbappid'] . "&redirect_uri=" . urlencode($facebookConfig['base_url'].'/users/checkfbuser/') . "&state="
59
		   . $this->Session->read('state').'&scope=publish_stream,email,user_birthday,publish_actions,user_location';
60
	   	$this->set('dialog_url', $dialog_url);
61
		$this->set('description','Why spend money when you can get something for free');
13579 anikendra 62
		$this->set('categories',$categories);
13532 anikendra 63
		if(isset($this->params['admin'])) {
13739 anikendra 64
			$this->layout = 'admin';
13808 anikendra 65
		}	
66
		$this->apihost = Configure::read('pythonapihost');
67
		$this->limit = Configure::read('dealsperpage');	
13685 anikendra 68
		$staticVersion = Configure::read('staticversion');
69
		$this->set('staticversion',$staticVersion);
13739 anikendra 70
		$this->set('requiremobileverification',Configure::read('requiremobileverification'));
13532 anikendra 71
    }
72
 
73
    function isAuthorized() {
74
        return $this->Auth->user('id');
75
    }
76
 
77
    function isFbAuthorized() {
78
        return $this->Session->read('facebook_id');
79
    }
80
 
81
    function afterFilter() {
13579 anikendra 82
		$result['ucadcode'] = $this->ucadcode;
13532 anikendra 83
    }
84
 
13659 anikendra 85
    function beforeRender() {   
13736 anikendra 86
    	$logged_user = $this->Auth->user();
87
    	$this->set('logged_user', $logged_user); 	
13579 anikendra 88
        $this->set('base_url', 'http://' . $_SERVER['SERVER_NAME'] . Router::url('/'));
13532 anikendra 89
    }
90
 
13736 anikendra 91
    function checkMobileNumber() {
92
    	$logged_user = $this->Auth->user();
93
    	if(empty($logged_user['mobile_verified']) && $this->params['controller'] !='users') {
94
			$skipmobileverification = $this->Session->read('skipmobileverification');
95
			if(!isset($skipmobileverification) || empty($skipmobileverification)) {
96
				$this->redirect('/users/verifymobile');
97
			}
98
		}
99
    }
100
 
13659 anikendra 101
    function getallheaders() { 
102
	   $headers = ''; 
103
       foreach ($_SERVER as $name => $value) 
104
       { 
105
	   if (substr($name, 0, 5) == 'HTTP_') 
106
	   { 
107
	       $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; 
108
	   } 
109
       } 
110
       return $headers; 
111
    } 
13633 anikendra 112
 
13808 anikendra 113
    public function getDealsApiUrl($page=1,$userId = null,$categoryId=0,$sort=null,$direction=null){
114
    	$this->log('categoryId '.$categoryId,'api');
115
    	$this->log('page '.$page,'api');
116
    	$offset = ($page - 1) * $this->limit;
117
    	if(isset($sort) && !empty($sort) && $sort!=-1){
118
    		$url = $this->apihost.'deals/'.$userId.'?categoryId='.$categoryId.'&sort='.$sort.'&direction='.$direction.'&limit='.$this->limit.'&offset='.$offset;
119
    	}else{
120
    		$url = $this->apihost.'deals/'.$userId.'?categoryId='.$categoryId.'&limit='.$this->limit.'&offset='.$offset;
121
    	}    	
122
    	return $url;
123
    }
124
 
13633 anikendra 125
	function make_request($url,$fields,$format='json'){
13683 anikendra 126
		$this->log("[url] $url",'api');
127
		$this->log("[fields] ".print_r($fields,1),'api');
13633 anikendra 128
		$fields_string = '';
129
		//open connection
130
		$ch = curl_init();
131
		//set the url, number of POST vars, POST data
132
		curl_setopt($ch,CURLOPT_URL, $url);
133
		curl_setopt($ch,CURLOPT_RETURNTRANSFER , true);
134
		if(!empty($fields)) {
135
			curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
136
			curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
137
			    'Content-Type: application/json',                                                                                
138
			    'Content-Length: ' . strlen($fields))                                                                       
139
			);   
140
		}
141
		//execute post
142
		$result = curl_exec($ch);
13808 anikendra 143
		// $this->log("[response] ".print_r($result,1),'api');
13633 anikendra 144
		//close connection
145
		curl_close($ch);
146
		switch($format){
147
			case 'json':
148
			$response = json_decode($result,1);
149
			break;
150
		}
151
		return $response;	
152
	}
13901 anikendra 153
 
154
	public function get_solr_result($q,$page) {
155
		$dealsperpage = Configure::read('dealsperpage');
156
		$offset = ($page - 1)*$dealsperpage;
157
		$cond = "q=$q";
158
	 	$sort = "store desc";
159
 
160
		$params = array(
161
			'conditions' =>array(
162
		 	'solr_query' => $cond
163
	 	),
164
		 	//'order' => $sort,
165
		 	'offset' => $offset,
166
		 	'limit' => $dealsperpage
167
	 	);
168
		$this->loadModel('Solr');
169
		$solroutput = $this->Solr->find('all', $params);
170
		$result = array();
171
		if(!empty($solroutput['Solr'])) {			
172
			$skuMap = array();
173
			foreach ($solroutput['Solr'] as $key => $value) {										
174
				$skuMap[$value['id']] = $value;
175
				$result[$value['skuBundleId']][$value['id']] = $value['available_price'];
176
			}				
177
			foreach ($result as $key => $value) {
178
				asort($value);
179
				$lowestPriceSku = key($value);
180
				$result[$key] = $skuMap[$lowestPriceSku];
181
			}
182
		}
183
		return $result;
184
	}
13532 anikendra 185
}