Subversion Repositories SmartDukaan

Rev

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

<?php
App::uses('AppController', 'Controller');
/**
 * Users Controller
 *
 * @property User $User
 */
class UsersController extends AppController {

        public $components = array('SignMeUp.SignMeUp','RequestHandler','Cookie');

    public function beforeFilter() {
        parent::beforeFilter();
        // $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'dashboard');
        $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'display','home');
        $this->Auth->allow(array('mine','login', 'forgotten_password', 'register', 'activate', 'checkemail','checkfbuser','registertwitteruser','skipmobileverification','reauthenticate','edit')); 
                $this->Cookie->name = 'profittill';
                $this->Cookie->time = 86400*30; 
                $this->Cookie->path = '/';
                $this->Cookie->key = 'qSI232qs*&sXOw!adre@34SAv!@*(XSL#$%)asGb$@11~_+!@#H23s~#^';
                $this->Cookie->httpOnly = true;
    }
    
        public function bookmarklet() {
                $this->set('title_for_layout','Profittill Bookmarklet');
        }

        public function mine() {
                $this->response->type('json');
                $this->layout = 'ajax';
                $userId = $this->request->query('user_id');
                if(isset($userId) && !empty($userId)){
                        $this->loadModel('User');
                        $options = array('conditions'=>array('User.id'=>$userId),'fields'=>array('username','email','first_name','profile_pic','mobile_number','mobile_verified','address_line_1','address_line_2',
                                'city','state','pincode','referrer'),'recursive'=>-1);
                        $user = $this->User->find('first',$options);
                        $result = $user['User'];
                }
                $this->set(array(
                    'result' => $result,
                    '_serialize' => array('result')
                )); 
                $this->render('/Elements/json'); 
        }

        function registertwitteruser(){
                $this->twitterObj->setToken($_GET['oauth_token']);
                $token = $this->twitterObj->getAccessToken();
                $this->twitterObj->setToken($token->oauth_token, $token->oauth_token_secret);
                $twitterInfo= $this->twitterObj->get_accountVerify_credentials();
                $data = array();
                $data['twitter_id'] = $twitterInfo->id;         
                $this->Session->write('twtusername',$twitterInfo->screen_name);
                $data['twitter_token'] = $token->oauth_token;
                $data['twitter_secret'] = $token->oauth_token_secret;
                $data['twitter_screen_name'] = $twitterInfo->screen_name;                                       
                $data['active'] = 1;

                $id = $this->Auth->user('id');
                if(!isset($id) || $id == ''){
                //      $count = $this->User->find('count',array('conditions' => array('twitter_id' => $data['twitter_id'])));
                        $user = $this->User->find('first',array('conditions' => array('twitter_id' => $data['twitter_id'])));
                        //if($count<1){
                        if(empty($user)){
                                $this->User->create();
                                $this->User->save($data);
//                              $dbuser = $this->User->read(null,$this->User->id);
                                $this->Auth->login($this->User->data);
                        }else{
                //              $data = $this->User->find('first',array('conditions' => array('twitter_id' => $data['twitter_id'])));
                                $this->Auth->login($user['User']);
                        }
                }else{
                        //$data['id'] = $this->Auth->user('id');
                        $user = $this->Auth->user;
//                      $this->User->save($data);
//                      $this->Auth->login($data);
                }                
                $user = $this->User->read(null, $this->Auth->User('id'));
                if ($user['User']['group_id'] == 1) {
                        //$this->redirect(array('controller'=>'users','action'=>'dashboard','?userid='.$this->Auth->user('id')));
                        header('location:users/dashboard?userid='.$this->Auth->user('id'));
                        exit();
                }
                elseif ($user['User']['group_id'] == 2) {
                        $this->redirect(array('controller' => 'administration', 'action' => 'dashboard'));
                        exit();
                }
        }

    public function checkfbuser($accessToken=null){
                $sessionState = $this->Session->read('state');
                if($sessionState != $_GET['state']) {
                        $this->redirect(array('controller'=>'users','action'=>'login'));
                }
                else {
                        if(isset($_GET['code'])){
                                $code = $_GET['code'];
                                $facebookConfig = Configure::read("Facebook");          
                                $token_url = "https://graph.facebook.com/oauth/access_token?"
                               . "client_id=" . $facebookConfig['fbappid'] . "&redirect_uri=" . urlencode($facebookConfig['base_url'].'/users/checkfbuser/')
                               . "&client_secret=" . $facebookConfig['secret'] . "&code=" . $code;

                            $response = file_get_contents($token_url);
                            $params = null;
                            parse_str($response, $params);
                            $accessToken = $params['access_token'];
                        }else{
                                $this->redirect(array('controller'=>'users','action'=>'login'));
                        }
                }
                $this->User->recursive = -1;
                if (!$accessToken) {
                    $this->redirect(array('controller'=>'users','action'=>'login'));
                    //$this->redirect(array('controller'=>'pages','action'=>'display','home'));
                }        
                if (isset($accessToken) && $accessToken != 'undefined') {                       

                    $graph_url = "https://graph.facebook.com/me?access_token=" . $accessToken;

                    $user = json_decode(file_get_contents($graph_url));
                        $this->log("FB user = ".print_r($user,1),'fb');
                    if (!empty($user)) {
                        if (!$this->isAuthorized()) {
                            $id = $this->User->checkFbUser($user,$accessToken);
                                $dbuser = $this->User->findById($id);
                            $this->Session->write('facebook_id',$user->id);
                                $this->Auth->login($dbuser['User']);
                                if($this->Auth->login()){
                                        $this->log("logged = ".print_r($this->Auth->user('id'),1)."\n",'fb');
                                }
                            $next = $this->Session->read('next');
                                $this->log("next= ".$next."\n",'fb');
                        
                            if (!empty($next)) {
                                        header('Location:' . $next);
                                        exit();
                            }else {
                                        //$this->redirect(array('controller'=>'users','action'=>'dashboard','?userid='.$this->Auth->user('id')));
                                        header('location:users/dashboard?userid='.$this->Auth->user('id'));
                                        exit();
                                    }
                                        //$this->redirect(array('controller'=>'users','action'=>'dashboard','?userid='.$this->Auth->user('id')));
                                        header('location:users/dashboard?userid='.$this->Auth->user('id'));
                                        exit();
                                } else {                        
                                        //$this->redirect(array('controller'=>'users','action'=>'dashboard','?userid='.$this->Auth->user('id')));
                                        header('location:users/dashboard?userid='.$this->Auth->user('id'));
                                        exit();
                                }
                    }
                }
    }

        public function uploadavatar(){
                $this->log(print_r($_FILES,1),'debug');
                $path = "img/avatars/";
                $valid_formats = array("jpg", "png", "gif", "bmp");
                if ($this->request->is('post')) {
                        $name = $_FILES['photoimg']['name'];
                        $size = $_FILES['photoimg']['size'];
                        
                        if(strlen($name))
                                {
                                        list($txt, $ext) = explode(".", $name);
                                        if(in_array($ext,$valid_formats))
                                        {
                                        if($size<(1024*1024))
                                                {
                                                        $actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
                                                        $tmp = $_FILES['photoimg']['tmp_name'];
                                                        if(move_uploaded_file($tmp, $path.$actual_image_name))
                                                                {
                                                                        echo "<img width='250' src='../".$path.$actual_image_name."'  class='preview'>";
                                                                        echo "<input type='hidden' id='imageUrl' name='photo_url' value='".'http://' . $_SERVER['SERVER_NAME'] . Router::url('/').$path.$actual_image_name."'></input>";
                                                                }
                                                        else
                                                                echo __('Upload Failed');
                                                }
                                                else
                                                echo __("Maximum allowed image file size is 1 MB");                                     
                                                }
                                                else
                                                echo __("Invalid file format"); 
                                }
                        else
                                echo __("Please select image");
                        exit;
                }
        }

    function checkemail() {
        $this->User->recursive = -1;
        $this->layout = 'ajax';
        if (!empty($this->params['url']['data']['User']['email'])) {
            $username = $this->params['url']['data']['User']['email'];
        } elseif (!empty($this->params['url']['data']['Doctor']['email'])) {
            $username = $this->params['url']['data']['Doctor']['email'];
        } else {
            $result = false;
            $this->set('result', $result);
        }
        if ($this->Auth->user('id') != null) {
            $conditions = array('User.email' => $username, 'User.id !=' => $this->Auth->user('id'));
        } else {
            $conditions = array('User.email' => $username);
        }
        $count = $this->User->find('count', array('conditions' => $conditions));
        if ($count > 0) {
            $result = false;
        } else {
            $result = true;
        }
        $this->set('result', $result);
    }

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function view($id = null) {
                $this->response->type('json');
                $this->layout = 'ajax';
                $callback = $this->request->query('callback');
                $this->User->id = $id;
                if (!$this->User->exists()) {
                        throw new NotFoundException(__('Invalid user'));
                }
                $user = $this->User->find('first', array('conditions' =>array('id' => $id),'recursive'=>-1));
                $result = array('user'=>$user);
                $this->set(array(
                    'result' => $result,
                    'callback' => $callback,
                    '_serialize' => array('result')
                ));
                $this->render('/Elements/json');
        }
/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */

    public function edit($id = null) {
        $this->log(print_r($this->request->data,1),'mydetails');
        $this->response->type('json');
        $this->layout = 'ajax';
        $this->User->recursive = -1;
        if (!$this->User->exists($id)) {
                $result = array('success'=>false,'message'=>'Invalid user');
                } else {
                if ($this->request->is('post') || $this->request->is('put')) {
                    if ($this->User->save($this->request->data)) {
                        $result = array('success'=>true,'message'=>'Your profile has been saved');
                    } else {
                        $result = array('success'=>false,'message'=> 'The user could not be saved. Please, try again.');
                    }
                }  
            }
        $this->set(array(
                    'result' => $result,
                    '_serialize' => array('result')
                )); 
                $this->render('/Elements/json');      
    }

/**
 * admin_index method
 *
 * @return void
 */
        public function admin_index() {
                $this->User->recursive = 0;
                $users = $this->paginate();
                $groups = $this->User->Group->find('list');
                $this->set(compact('groups','users'));
        }

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function admin_view($id = null) {
                $this->User->id = $id;
                if (!$this->User->exists()) {
                        throw new NotFoundException(__('Invalid user'));
                }
                $this->set('user', $this->User->read(null, $id));
        }

/**
 * admin_add method
 *
 * @return void
 */
        public function admin_add() {
                if ($this->request->is('post')) {
                        $this->User->create();
                        if ($this->User->save($this->request->data)) {
                                $this->Session->setFlash(__('The user has been saved'));
                                $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
                        }
                }
                $groups = $this->User->Group->find('list');
                $this->set(compact('groups'));
        }

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function admin_edit($id = null) {
                $this->User->id = $id;
                if (!$this->User->exists()) {
                        throw new NotFoundException(__('Invalid user'));
                }
                if ($this->request->is('post') || $this->request->is('put')) {
                        if ($this->User->save($this->request->data)) {
                                $this->Session->setFlash(__('The user has been saved'));
                                $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
                        }
                } else {
                        $this->request->data = $this->User->read(null, $id);
                }
                $groups = $this->User->Group->find('list');
                $this->set(compact('groups'));
        }

/**
 * admin_delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function admin_delete($id = null) {
                if (!$this->request->is('post')) {
                        throw new MethodNotAllowedException();
                }
                $this->User->id = $id;
                if (!$this->User->exists()) {
                        throw new NotFoundException(__('Invalid user'));
                }
                if ($this->User->delete()) {
                        $this->Session->setFlash(__('User deleted'));
                        $this->redirect(array('action' => 'index'));
                }
                $this->Session->setFlash(__('User was not deleted'));
                $this->redirect(array('action' => 'index'));
        }

        public function dashboard() {
                App::uses('CakeTime', 'Utility');
                $user = $this->User->read(null,$this->Auth->user('id'));
                $this->set('user',$user);
                $this->set('title_for_layout', "Dashboard");
        }

/**
* user authentication funtions
*/
        public function register() {
                $referrer = $this->request->query('referrer');
                $this->log('referrer = '.$referrer);
                $this->Cookie->write('referrer',$referrer);
                $this->layout = 'auth';
                $this->set('title_for_layout','Register your free letushaggle.com account');
                $this->SignMeUp->register();
        }

        public function activate() {
                $this->layout = 'auth';
                $this->set('title_for_layout','Activate your copublish.in account');
                $this->SignMeUp->activate();
        }

        public function forgotten_password() {
                $this->layout = 'auth';
                $this->set('title_for_layout','Reset your copublish.in password');
                $this->SignMeUp->forgottenPassword();
        }       

        public function login() {
                $this->layout = 'auth';
                if (!empty($this->data['User']['next'])) {
                    $next = $this->data['User']['next'];
                    $this->Session->write('next', $next);
                } elseif (!empty($this->params['url']['next'])) {
                    $next = $this->params['url']['next'];
                    $this->Session->write('next', $next);
                }
                if ($this->Auth->login()) {
                    $next = $this->Session->read('next');
                    if (!empty($next)) {
                                header('location:' . $next);
                                exit();
                    }
                    $user = $this->User->read(null, $this->Auth->User('id'));            
                    if ($user['User']['group_id'] == 2) {
                                $this->redirect(array('controller' => 'administration', 'action' => 'dashboard'));
                                exit();
                    }
                        if ($user['User']['group_id'] == 1) {
                                $this->redirect(array('controller' => 'deals', 'action' => 'mine?user_id='.$user['User']['id']));
                                exit();
                        }
                } else {
                    if (!empty($this->data)) {                
                                $this->Session->setFlash(__('Invalid username or password'),'error_message');
                    }
                }
                $this->set('title_for_layout', "Login to letushaggle.com");
        }

        public function logout() {
                $this->Session->setFlash(__('You have been logged out now'),'success_message');
                $this->redirect($this->Auth->logout());
        }

        public function updatetimezone() {
                $this->layout = 'ajax';         
                $timezone = $_GET['tz'];
                Configure::load('constant');
                $timeZones = Configure::read("timeZones");
                $query = "UPDATE users SET user_timezone = $timezone WHERE id = ".$this->Auth->User('id');
                if($this->User->query($query)){
                        $result = array('success' => true,'tz' => $timeZones[$timezone]);
                }else{
                        $result = array('success' => false);
                }
                $this->set('result',$result);
        }

        public function verifymobile() {
                $this->layout = 'innerpages';
        }

        public function getdial2verifynumber() {
                $TelNumber=substr($this->request->data("phone_number"),-10);

          // Replace with your Dial2Verify API Passkey generated using ( http://kb.dial2verify.in/?q=5 )
            $API_KEY = Configure::read("dial2verifyapikey");
  
                //Get API Image Response
            $url ="http://engine.dial2verify.in/Integ/API.dvf?mobile=$TelNumber&passkey=$API_KEY&notify=http://engine.dial2verify.in/Integ/CatchAll.dvf&e-notify=support@dial2verify.in&out=JSON&cn=IN";
            // $json=file_get_contents($APIUrl);
            $json = json_encode($this->make_request($url,null));
                $this->response->type('json');
                $this->layout = 'ajax';
            echo($json);die;
        }

        public function getdial2verifystatus() { 

                $SID=$_REQUEST["SID"];
    
            $json = array();
                $VerificationCall="http://engine.dial2verify.in/Integ/UserLayer/DataFeed_APIV2.dvf?SID=$SID";

           // Make a call to Dial2Verify API & Parse The JSON Response
                // $RequestPayload=json_decode(file_get_contents($VerificationCall),true);
                $RequestPayload = $this->make_request($VerificationCall,null);
                // $RequestPayload = json_decode($response,true);

                $VerifStatus=$RequestPayload["VerificationStatus"];
             
                $json["VerificationStatus"]=$VerifStatus;
                
                $this->response->type('json');
                $this->layout = 'ajax';
            echo(json_encode($json));die;
        }

        public function skipmobileverification() {
                $this->Session->write('skipmobileverification',1);
                $this->redirect(array('controller'=>'store_products','action'=>'mine',"?" => array( "user_id" => $this->Auth->User('id'))));
        }

        public function reauthenticate($userId) {
                $user = $this->User->read(null,$userId);
                $this->Auth->login($user['User']);
                $json = array('success'=>true);
                $this->response->type('json');
                $this->layout = 'ajax';
                    echo(json_encode($json));die;
        }
}