Subversion Repositories SmartDukaan

Rev

Rev 16591 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {

        public $apihost;

        public $components = array(
                'Session','Cookie',
                'Auth' => array(
                        'loginAction' => array('controller' => 'users', 'action' => 'login'),
                        'allowedActions' => array('index', 'view', 'display')
                )                       
        );

        var $helpers = array('Session', 'Form', 'Html');        

        function beforeFilter() {
                $this->Auth->autoRedirect = false;              
        
                //Set config settings according to domain
                // get host name from URL
                preg_match('@^(?:http://)?([^/]+)@i',$_SERVER['HTTP_HOST'], $matches);
                $host = $matches[1];
                switch($host){                  
                        case 'localrub':
                                Configure::load('dev');
                                break;
                        case 'staging.apprub.com':
                        case 'www.staging.apprub.com':
                                Configure::load('staging');
                                break;
                        default:
                        case 'www.apprub.com':
                        case 'apprub.com':
                                Configure::load('live');
                                break;
                }
                
                if(isset($this->params['admin'])) {
                        $this->layout = 'admin';
                }       
                $this->apihost = Configure::read('apihost');
                // $this->limit = Configure::read('dealsperpage');      
                $staticVersion = Configure::read('staticversion');
                $this->set('staticversion',$staticVersion);
    }
        
    function isAuthorized() {
        return $this->Auth->user('id');
    }

    function afterFilter() {
    }

    function beforeRender() {   
        $logged_user = $this->Auth->user();
        $this->set('logged_user', $logged_user);        
        $this->set('base_url', 'http://' . $_SERVER['SERVER_NAME'] . Router::url('/'));
    }

    function getallheaders() { 
           $headers = ''; 
       foreach ($_SERVER as $name => $value) 
       { 
           if (substr($name, 0, 5) == 'HTTP_') 
           { 
               $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; 
           } 
       } 
       return $headers; 
    } 

        function make_request($url,$fields,$format='json'){
                $this->log("[url] $url",'api');
                $this->log("[fields] ".print_r($fields,1),'api');
                $fields_string = '';
                //open connection
                $ch = curl_init();
                //set the url, number of POST vars, POST data
                curl_setopt($ch,CURLOPT_URL, $url);
                curl_setopt($ch,CURLOPT_RETURNTRANSFER , true);
                if(!empty($fields)) {
                        curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
                        curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
                            'Content-Type: application/json',                                                                                
                            // 'Content-Length: ' . sizeof($fields))                                                                       
                            'Content-Length: ' . strlen($fields))                                                                       
                        );   
                }
                //execute post
                $result = curl_exec($ch);
                $this->log("[response] ".print_r($result,1),'api');
                //close connection
                curl_close($ch);
                switch($format){
                        case 'json':
                        $response = json_decode($result,1);
                        break;
                }
                return $response;       
        }

        function post_request($url,$fields,$format='json'){
                $this->log("[url] $url",'api');
                $this->log("[fields] ".print_r($fields,1),'api');
                $fields_string = '';
                //open connection
                $ch = curl_init();
                //execute post
                foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
                rtrim($fields_string, '&');
                //set the url, number of POST vars, POST data
                curl_setopt($ch,CURLOPT_URL, $url);
                curl_setopt($ch,CURLOPT_POST, count($fields));
                curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
                $result = curl_exec($ch);
                $this->log("[response] ".print_r($result,1),'api');
                //close connection
                curl_close($ch);
                switch($format){
                        case 'json':
                        $response = json_decode($result,1);
                        break;
                }
                return $response;       
        }
}