Subversion Repositories SmartDukaan

Rev

Rev 16717 | 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);
16730 anikendra 56
		$this->set('apihost',$this->apihost);
16591 anikendra 57
    }
58
 
59
    function isAuthorized() {
60
        return $this->Auth->user('id');
61
    }
62
 
63
    function afterFilter() {
64
    }
65
 
66
    function beforeRender() {   
67
    	$logged_user = $this->Auth->user();
68
    	$this->set('logged_user', $logged_user); 	
69
        $this->set('base_url', 'http://' . $_SERVER['SERVER_NAME'] . Router::url('/'));
70
    }
71
 
72
    function getallheaders() { 
73
	   $headers = ''; 
74
       foreach ($_SERVER as $name => $value) 
75
       { 
76
	   if (substr($name, 0, 5) == 'HTTP_') 
77
	   { 
78
	       $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; 
79
	   } 
80
       } 
81
       return $headers; 
82
    } 
83
 
84
	function make_request($url,$fields,$format='json'){
85
		$this->log("[url] $url",'api');
86
		$this->log("[fields] ".print_r($fields,1),'api');
87
		$fields_string = '';
88
		//open connection
89
		$ch = curl_init();
90
		//set the url, number of POST vars, POST data
91
		curl_setopt($ch,CURLOPT_URL, $url);
92
		curl_setopt($ch,CURLOPT_RETURNTRANSFER , true);
93
		if(!empty($fields)) {
94
			curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
95
			curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
96
			    'Content-Type: application/json',                                                                                
97
			    // 'Content-Length: ' . sizeof($fields))                                                                       
98
			    'Content-Length: ' . strlen($fields))                                                                       
99
			);   
100
		}
101
		//execute post
102
		$result = curl_exec($ch);
103
		$this->log("[response] ".print_r($result,1),'api');
104
		//close connection
105
		curl_close($ch);
106
		switch($format){
107
			case 'json':
108
			$response = json_decode($result,1);
109
			break;
110
		}
111
		return $response;	
112
	}
113
 
114
	function post_request($url,$fields,$format='json'){
115
		$this->log("[url] $url",'api');
116
		$this->log("[fields] ".print_r($fields,1),'api');
117
		$fields_string = '';
118
		//open connection
119
		$ch = curl_init();
120
		//execute post
121
		foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
122
		rtrim($fields_string, '&');
123
		//set the url, number of POST vars, POST data
124
		curl_setopt($ch,CURLOPT_URL, $url);
125
		curl_setopt($ch,CURLOPT_POST, count($fields));
126
		curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
127
		$result = curl_exec($ch);
128
		$this->log("[response] ".print_r($result,1),'api');
129
		//close connection
130
		curl_close($ch);
131
		switch($format){
132
			case 'json':
133
			$response = json_decode($result,1);
134
			break;
135
		}
136
		return $response;	
137
	}
138
}