Subversion Repositories SmartDukaan

Rev

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