Subversion Repositories SmartDukaan

Rev

Rev 16717 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
16591 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
 
15
	public $limit;
16
	public $apihost;
17
	public $acls;
18
 
19
	public $components = array(
20
		'Session','Cookie',
21
		'Auth' => array(
22
			'loginAction' => array('controller' => 'users', 'action' => 'login'),
23
			'allowedActions' => array('index', 'view', 'display')
24
		)			
25
	);
26
 
27
	var $helpers = array('Session', 'Form', 'Html');	
28
 
29
	function beforeFilter() {
30
		$this->Auth->autoRedirect = false;		
31
 
32
		//Set config settings according to domain
33
		// get host name from URL
34
		preg_match('@^(?:http://)?([^/]+)@i',$_SERVER['HTTP_HOST'], $matches);
35
		$host = $matches[1];
36
		switch($host){			
37
			case 'localrub':
38
				Configure::load('dev');
39
				break;
40
			case 'staging.apprub.com':
41
			case 'www.staging.apprub.com':
42
				Configure::load('staging');
43
				break;
44
			default:
45
			case 'www.apprub.com':
46
			case 'apprub.com':
47
			case 'api.apprub.com':
48
				Configure::load('live');
49
				break;
50
		}
51
 
52
		if(isset($this->params['admin'])) {
53
			$this->layout = 'admin';
54
		}	
55
		$this->apihost = Configure::read('pythonapihost');
56
		$this->limit = Configure::read('dealsperpage');	
57
		$staticVersion = Configure::read('staticversion');
58
		$this->set('staticversion',$staticVersion);
59
    }
60
 
61
    function isAuthorized() {
62
        return $this->Auth->user('id');
63
    }
64
 
65
    function afterFilter() {
66
    }
67
 
68
    function beforeRender() {   
69
    	$logged_user = $this->Auth->user();
70
    	$this->set('logged_user', $logged_user); 	
71
        $this->set('base_url', 'http://' . $_SERVER['SERVER_NAME'] . Router::url('/'));
72
    }
73
 
74
    function getallheaders() { 
75
	   $headers = ''; 
76
       foreach ($_SERVER as $name => $value) 
77
       { 
78
	   if (substr($name, 0, 5) == 'HTTP_') 
79
	   { 
80
	       $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; 
81
	   } 
82
       } 
83
       return $headers; 
84
    } 
85
 
86
	function make_request($url,$fields,$format='json'){
87
		$this->log("[url] $url",'api');
88
		$this->log("[fields] ".print_r($fields,1),'api');
89
		$fields_string = '';
90
		//open connection
91
		$ch = curl_init();
92
		//set the url, number of POST vars, POST data
93
		curl_setopt($ch,CURLOPT_URL, $url);
94
		curl_setopt($ch,CURLOPT_RETURNTRANSFER , true);
95
		if(!empty($fields)) {
96
			curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
97
			curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
98
			    'Content-Type: application/json',                                                                                
99
			    // 'Content-Length: ' . sizeof($fields))                                                                       
100
			    'Content-Length: ' . strlen($fields))                                                                       
101
			);   
102
		}
103
		//execute post
104
		$result = curl_exec($ch);
105
		$this->log("[response] ".print_r($result,1),'api');
106
		//close connection
107
		curl_close($ch);
108
		switch($format){
109
			case 'json':
110
			$response = json_decode($result,1);
111
			break;
112
		}
113
		return $response;	
114
	}
115
 
116
	function post_request($url,$fields,$format='json'){
117
		$this->log("[url] $url",'api');
118
		$this->log("[fields] ".print_r($fields,1),'api');
119
		$fields_string = '';
120
		//open connection
121
		$ch = curl_init();
122
		//execute post
123
		foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
124
		rtrim($fields_string, '&');
125
		//set the url, number of POST vars, POST data
126
		curl_setopt($ch,CURLOPT_URL, $url);
127
		curl_setopt($ch,CURLOPT_POST, count($fields));
128
		curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
129
		$result = curl_exec($ch);
130
		$this->log("[response] ".print_r($result,1),'api');
131
		//close connection
132
		curl_close($ch);
133
		switch($format){
134
			case 'json':
135
			$response = json_decode($result,1);
136
			break;
137
		}
138
		return $response;	
139
	}
140
}