Subversion Repositories SmartDukaan

Rev

Rev 13683 | Rev 13736 | 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 {
14
	public $components = array(
15
		'Session',
16
		'Auth' => array(
17
			'loginAction' => array('controller' => 'users', 'action' => 'login'),
18
			'allowedActions' => array('index', 'view', 'display')
19
		)			
20
	);
21
	var $helpers = array('Session', 'Form', 'Html');
22
	var $keywords = array('instagram followers','instagram button','instagram follow back','instagram tool','instagram automation','free istagram followers','instagram stats','instagram follow button');
23
 
24
	function beforeFilter() {
13659 anikendra 25
		$this->Auth->autoRedirect = false;		
13579 anikendra 26
 
27
		//Set config settings according to domain
13532 anikendra 28
		// get host name from URL
29
		preg_match('@^(?:http://)?([^/]+)@i',$_SERVER['HTTP_HOST'], $matches);
30
		$host = $matches[1];
31
		switch($host){			
13567 anikendra 32
			case 'localdtr':
13532 anikendra 33
				Configure::load('dev');
34
				break;
35
			default:
13567 anikendra 36
			case 'www.profittill.com':
37
			case 'profittill.com':
13633 anikendra 38
			case 'api.profittill.com':
13532 anikendra 39
				Configure::load('live');
40
				break;
41
		}
13579 anikendra 42
		$facebookConfig = Configure::read("Facebook");		
43
		$categories = Configure::read('Categories');
13532 anikendra 44
		//Facebook configuration
45
		$this->set('fbappid', $facebookConfig['fbappid']);
13579 anikendra 46
		$this->set('apihost', Configure::read('apihost'));
47
 
13532 anikendra 48
	   	$sessionState = $this->Session->read('state');
49
		if(!isset($sessionState)){
50
			$this->Session->write('state' , md5(uniqid(rand(), TRUE))); // CSRF protection
51
		}
52
	 	$dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" 
53
		   . $facebookConfig['fbappid'] . "&redirect_uri=" . urlencode($facebookConfig['base_url'].'/users/checkfbuser/') . "&state="
54
		   . $this->Session->read('state').'&scope=publish_stream,email,user_birthday,publish_actions,user_location';
55
	   	$this->set('dialog_url', $dialog_url);
56
		$this->set('description','Why spend money when you can get something for free');
13579 anikendra 57
		$this->set('categories',$categories);
13532 anikendra 58
		if(isset($this->params['admin'])) {
59
    		$this->layout = 'admin';
60
    	}		
13685 anikendra 61
		$staticVersion = Configure::read('staticversion');
62
		$this->set('staticversion',$staticVersion);
13532 anikendra 63
    }
64
 
65
    function isAuthorized() {
66
        return $this->Auth->user('id');
67
    }
68
 
69
    function isFbAuthorized() {
70
        return $this->Session->read('facebook_id');
71
    }
72
 
73
    function afterFilter() {
13579 anikendra 74
		$result['ucadcode'] = $this->ucadcode;
13532 anikendra 75
    }
76
 
13659 anikendra 77
    function beforeRender() {   
78
    	$this->set('logged_user', $this->Auth->user()); 	
13579 anikendra 79
        $this->set('base_url', 'http://' . $_SERVER['SERVER_NAME'] . Router::url('/'));
80
        // $this->set('base_url', 'http://api.profittill.com/');
13532 anikendra 81
    }
82
 
13659 anikendra 83
    function getallheaders() { 
84
	   $headers = ''; 
85
       foreach ($_SERVER as $name => $value) 
86
       { 
87
	   if (substr($name, 0, 5) == 'HTTP_') 
88
	   { 
89
	       $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; 
90
	   } 
91
       } 
92
       return $headers; 
93
    } 
13633 anikendra 94
 
95
	function make_request($url,$fields,$format='json'){
13683 anikendra 96
		$this->log("[url] $url",'api');
97
		$this->log("[fields] ".print_r($fields,1),'api');
13633 anikendra 98
		$fields_string = '';
99
		//open connection
100
		$ch = curl_init();
101
		//set the url, number of POST vars, POST data
102
		curl_setopt($ch,CURLOPT_URL, $url);
103
		curl_setopt($ch,CURLOPT_RETURNTRANSFER , true);
104
		if(!empty($fields)) {
105
			curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
106
			curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
107
			    'Content-Type: application/json',                                                                                
108
			    'Content-Length: ' . strlen($fields))                                                                       
109
			);   
110
		}
111
		//execute post
112
		$result = curl_exec($ch);
13683 anikendra 113
		$this->log("[response] ".print_r($result,1),'api');
13633 anikendra 114
		//close connection
115
		curl_close($ch);
116
		switch($format){
117
			case 'json':
118
			$response = json_decode($result,1);
119
			break;
120
		}
121
		return $response;	
122
	}
13532 anikendra 123
}