Advertisement
Guest User

singo

a guest
May 30th, 2017
922
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 98.32 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class User extends CI_Controller {
  4.  
  5.     var $valid = false,
  6.     $cs_email = 'no-reply@com.com';
  7.     protected $linkPage;
  8.     protected $segment = 3;
  9.     protected $totalPage = 0;
  10.     protected $perPage = 20;
  11.  
  12.     function __construct()
  13.     {
  14.         parent::__construct();
  15.         $this->load->model('user_model');
  16.         $this->load->model('admin_model');
  17.         $this->load->model('security_model');
  18.         $this->session->set_userdata('table_prefix', $this->config->item('table_prefix'));
  19.         $this->valid = $this->session->userdata('is_authed');
  20. //        var_dump($this->session->userdata('is_authed'));
  21.         if ( strlen($this->uri->segment(1)) == 2 )
  22.             $this->lang->load( $this->uri->segment(1), 'gox');
  23.         else
  24.             $this->lang->load( 'en', 'gox');
  25.  
  26.  
  27.         $params = array(
  28.             'object' => &$this,
  29.             'server' => &$_SERVER,
  30.             );
  31.  
  32.         log_index($params);
  33.  
  34.         $this->template->set_template('sbadmin2');
  35.         $uri = $this->uri->segment(2);
  36.  
  37.         //if( preg_match('/(image|login$/', $uri))
  38.         //    return;
  39.  
  40.         //$user_info = $this->session->userdata('user_info');
  41.  
  42.         //if( !isset($user_info->username) and $this->uri->segment(2) != 'login')
  43.         //    redirect('admin/index');
  44.     }
  45.  
  46.     protected function pagination() {
  47.         $this->load->library('pagination');
  48.         $config = array();
  49.         $config['uri_segment'] = $this->segment;
  50.         $config['base_url'] = $this->linkPage;
  51.         $config['total_rows'] = $this->totalPage;
  52.         $config['per_page'] = $this->perPage;
  53.         $config['full_tag_open'] = '<ul class="pagination">';
  54.         $config['full_tag_close'] = '</ul>';
  55.         $this->pagination->initialize($config);
  56.         return $this->pagination->create_links();
  57.     }
  58.  
  59.     protected function start_page($uri) {
  60.         if ($this->uri->segment($uri) > 0) {
  61.             $start = $this->uri->segment($uri);
  62.         } else {
  63.             $start = 0;
  64.         }
  65.         return $start;
  66.     }
  67.  
  68.     public function index()
  69.     {
  70.         $this->gox_acl->validate_create();
  71.         $user_info = $this->session->userdata('user_info');
  72.  
  73.         if( !isset($user_info->username) )
  74.             show_404();
  75.  
  76.         redirect('user/register','refresh');
  77.     }
  78.  
  79.     function dashboard()
  80.     {
  81.         show_404();
  82.         if ( $this->valid )
  83.             redirect('app/index');
  84.         else
  85.         {
  86.             $this->session->set_flashdata('flash_message', lang('LoginIsRequired'));
  87.             redirect('user/login');
  88.         }
  89.     }
  90.  
  91.     function form_validation($is_edit = 0)
  92.     {
  93.         $flag = True;
  94.         $output = null;
  95.  
  96.         if ( $is_edit == 1)
  97.         {
  98.             $this->form_validation->set_rules('id', 'id', 'trim|required|xss_clean');
  99.         }
  100.         else
  101.         {
  102.             $this->form_validation->set_rules('email', lang('Email'), 'trim|required|xss_clean|valid_email|callback__email_unique');
  103.         }
  104.  
  105.         $this->form_validation->set_rules('name', lang('Name'), 'trim|required|xss_clean');
  106.         $this->form_validation->set_rules('username', lang('Username'), 'trim|required|xss_clean');
  107.         $this->form_validation->set_rules('password', lang('Password'), 'trim|required|xss_clean|matches[re_password]');
  108.         $this->form_validation->set_rules('re_password', lang('ReTypePassword'), 'trim|required|xss_clean');
  109.  
  110.         if ( $this->form_validation->run() == False or $flag === False)
  111.         {
  112.             $errMsg = $this->form_validation->error_string() ;
  113.  
  114.             return $errMsg;
  115.         }
  116.         else
  117.         {
  118.             return null;
  119.         }
  120.     }
  121.  
  122.     function _email_unique($str)
  123.     {
  124.         $is_unique = $this->user_model->user_email_unique( $str );
  125.  
  126.         $errMsg = $this->form_validation->error_string() ;
  127.         if ( $is_unique )
  128.             return True;
  129.         else
  130.             $this->form_validation->set_message('email_unique', lang('ErrorEmailTaken'));
  131.  
  132.         return False;
  133.     }
  134.  
  135.     function register()
  136.     {
  137.         $this->gox_acl->validate_create();
  138.         $user_info = $this->session->userdata('user_info');
  139.  
  140.         if( !isset($user_info->username) and $this->uri->segment(2) != 'login')
  141.             show_404();
  142.  
  143.         $submit = $this->input->post('submit');
  144.         $data = array();
  145.         if ( $submit )
  146.         {
  147.             $errMsg = $this->form_validation();
  148.  
  149.             if ( !$errMsg )
  150.             {
  151.                 $params = array(
  152.                     'input' => $this->input->post(),
  153.                     );
  154.  
  155.                 $user_id = $this->user_model->add_user( $params );
  156.                 if ( $user_id )
  157.                 {
  158.                     $data['user_info'] = $this->user_model->profile(null, $user_id);
  159.  
  160.                     $this->load->library('phpmailer2');
  161.  
  162.                     $this->phpmailer2->addAddress($data['user_info']->email, $data['user_info']->name);
  163.                     $this->phpmailer2->addFrom($this->cs_email, 'Customer Service BOSS');
  164.                     $this->phpmailer2->subject('Registration');
  165.                     $this->phpmailer2->message($this->load->view('user/register_mail_ok', $data, TRUE) );
  166.                     if ( !$this->phpmailer2->send() )
  167.                     {
  168.                         log_message('error',sprintf('%s (%s): %s, %s', __FILE__,__LINE__,'Email not sent', $this->phpmailer2->ErrorInfo));
  169.                     }
  170. //                    echo $this->email->print_debugger();
  171.                     $this->session->set_flashdata('flash_message', lang('AnEmailHasBeenSent'));
  172.                     $this->template->write_view('content','user/register_ok', array());
  173.                     $this->template->render();
  174.                     return;
  175.                 }
  176.                 else
  177.                     $data['error'] = lang('ErrorDataCreationFailed');
  178.             }
  179.             else
  180.                 $data['error'] = $errMsg;
  181.         }
  182.         else
  183.             null;
  184.  
  185.         $this->template->write_view('content', 'user/register', $data);
  186.         $this->template->render();
  187.     }
  188.  
  189.     function login()
  190.     {
  191.         $COMMON_USER = 18;
  192. //            $this->output->enable_profiler(true);
  193.         $submit             = $this->input->post('submit');
  194.         $forgot_password    = $this->input->post('forgot_password');
  195.  
  196.         if ( $forgot_password )
  197.         {
  198.             redirect('user/forgot_password');
  199.         }
  200.  
  201.         if ( $submit )
  202.         {
  203.             $params = array(
  204.                 'object' => &$this,
  205.                 'server' => &$_SERVER,
  206.                 'try_username' => $this->input->post('username'),
  207.                 );
  208.  
  209.             log_index($params);
  210.  
  211.             $params = array(
  212.                 'username' => $this->input->post('username'),
  213.                 'password' => $this->input->post('password'),
  214.                 );
  215.             $user_info = $this->user_model->login2($params);
  216.  
  217.             if ( $user_info !== false)
  218.             {
  219.                 if ($user_info->bverified == 0)
  220.                 {
  221.                     $this->session->set_flashdata('flash_message', lang('PleaseVerifyByEmail'));
  222.                     redirect('user/login');
  223.                     return;
  224.                 }
  225.  
  226.                 if ($user_info->idelete == 1)
  227.                 {
  228.                     $this->session->set_flashdata('flash_message', lang('UserIsSuspended'));
  229.                     redirect('user/login');
  230.                     return;
  231.                 }
  232.                 // reset n_retires
  233.                 $this->user_model->update_retries($params['username'], true);
  234.  
  235.                 // Prune unnrecessary data
  236.                 // as CI session has size restriction
  237.                 $temp = new StdClass();
  238.                 $temp->name = $user_info->name;
  239.                 $temp->username = $user_info->username;
  240.                 $temp->id = $user_info->id;
  241.                 $temp->user_type_id = $user_info->user_type_id;
  242.                 $temp->level_id = $user_info->level_id;
  243.  
  244.                 $this->session->set_userdata('is_authed',1);
  245.                 $this->session->set_userdata('user_info', $temp);
  246.                 //$this->session->set_userdata('apikey', $api_token);
  247.                 $this->session->set_flashdata('flash_message', lang('LoginSuccess'));
  248.                 //var_dump($temp, $this->session->userdata('user_info')); die();
  249.  
  250.                 $params = array(
  251.                     'object' => &$this,
  252.                     'server' => &$_SERVER,
  253.                     );
  254.  
  255.                 log_index($params);
  256.                 redirect('admin/index');
  257.                 return;
  258.             }
  259.             else{
  260.                 $this->user_model->update_retries($params['username'], false);
  261.                 $this->session->set_flashdata('flash_message', lang('LoginFailed'));
  262.             }
  263.         }
  264.         else
  265.             null;
  266.  
  267.     //            $this->load->view('login');
  268.         $this->template->set_template('bootstrap');
  269.         $this->template->write_view('content', 'login', '', TRUE);
  270.         $this->template->render();
  271.     }
  272.  
  273.     function logout()
  274.     {
  275.         $this->session->set_userdata('user_info', null);
  276.         $this->session->set_userdata('is_authed', null);
  277.         $this->session->set_flashdata('flash_message', lang('Goodbye'));
  278.         redirect('user/login');
  279.     }
  280.  
  281.     function email_verify($verification_code = null, $user_id = null)
  282.     {
  283.         if( $verification_code and $user_id )
  284.         {
  285.             $params = array(
  286.                 'user_id' => $user_id,
  287.                 'verification_code' => $verification_code,
  288.                 );
  289.  
  290.             list($bResult, $user_info, $resMsg) = $this->user_model->email_verify($params);
  291.  
  292.             if ( $bResult )
  293.             {
  294.                 //update bverified
  295.                 list($bResult, $result, $resMsg) = $this->user_model->email_verify_update( $params );
  296.  
  297.                 if ( $bResult )
  298.                 {
  299.                     $this->session->set_flashdata('flash_message', lang('EmailVerified'));
  300.                     $this->session->set_userdata('user_info', $user_info);
  301.                     $this->session->set_userdata('is_authed', 1);
  302.  
  303.                     //redirect to dashboard
  304.                     redirect('app/index');
  305.                     return;
  306.                 }
  307.                 else
  308.                     $data['error'] = lang('ErrorInvalidVerification');
  309.             }
  310.             else
  311.                 $data['error'] = lang('ErrorInvalidVerification');
  312.         }
  313.         else
  314.             $data['error'] = lang('ErrorInvalidVerification');
  315.  
  316.         $this->template->write_view('content', 'user/email_verify', $data);
  317.         $this->template->render();
  318.     }
  319.  
  320.     function usersdepartment_autocomplete()
  321.     {
  322.         $user_info = $this->session->userdata('user_info');
  323.  
  324.         if( !isset($user_info->username) and $this->uri->segment(2) != 'login')
  325.             show_404();
  326.  
  327.         if ( !$this->valid )
  328.             die(_ajax_upload_error(lang('ErrorPermissionDenied')));
  329.  
  330.         $params = array(
  331.             'department_id' => $this->input->post('department_id'),
  332.         );
  333.         $result = $this->user_model->users_autocomplete( $this->input->post('term'), $params );
  334.         echo json_encode( $result );
  335.         die();
  336.     }
  337.  
  338.     function users_autocomplete()
  339.     {
  340.         $user_info = $this->session->userdata('user_info');
  341.  
  342.         if( !isset($user_info->username) and $this->uri->segment(2) != 'login')
  343.             show_404();
  344.  
  345.         if ( !$this->valid )
  346.             die(_ajax_upload_error(lang('ErrorPermissionDenied')));
  347.         $result = $this->user_model->users_autocomplete( $this->input->post('term') );
  348.         echo json_encode( $result );
  349.         die();
  350.     }
  351.  
  352.     function avail_crews_autocomplete()
  353.     {
  354.         $user_info = $this->session->userdata('user_info');
  355.  
  356.         if( !isset($user_info->username) and $this->uri->segment(2) != 'login')
  357.             show_404();
  358.         if ( !$this->valid )
  359.             die(_ajax_upload_error(lang('ErrorPermissionDenied')));
  360.         $result = $this->user_model->avail_crews_autocomplete( $this->input->post('term'), array('is_member' => true) );
  361.         echo json_encode( $result );
  362.         die();
  363.     }
  364.  
  365.     function revived_autocomplete()
  366.     {
  367.         $user_info = $this->session->userdata('user_info');
  368.  
  369.         if( !isset($user_info->username) and $this->uri->segment(2) != 'login')
  370.             show_404();
  371.  
  372.         if ( !$this->valid )
  373.             die(_ajax_upload_error(lang('ErrorPermissionDenied')));
  374.  
  375.         $params = array(
  376.             'term' => $this->input->post('term'),
  377.             'vessel_id' => $this->input->post('vessel_id'),
  378.             'vessel_summon_id' => $this->input->post('vessel_summon_id'),
  379.         );
  380.         $result = $this->user_model->revived_autocomplete( $params );
  381.         echo json_encode( $result );
  382.         die();
  383.     }
  384.  
  385.     function members_autocomplete()
  386.     {
  387.         $user_info = $this->session->userdata('user_info');
  388.  
  389.         if( !isset($user_info->username) and $this->uri->segment(2) != 'login')
  390.             show_404();
  391.  
  392.         if ( !$this->valid )
  393.             die(_ajax_upload_error(lang('ErrorPermissionDenied')));
  394.         $result = $this->user_model->users_autocomplete( $this->input->post('term'), array('is_member' => true) );
  395.         echo json_encode( $result );
  396.         die();
  397.     }
  398.  
  399.     function reset_password()
  400.     {
  401.         $user_info = $this->session->userdata('user_info');
  402.  
  403.         if( !isset($user_info->username) and $this->uri->segment(2) != 'login')
  404.             show_404();
  405.  
  406.         if ( $this->valid or $this->session->flashdata('temp_access') )
  407.         {
  408.             if ( $this->valid )
  409.             {
  410.                 if( !$this->session->userdata('is_route') )
  411.                     $this->template->set_template('sbadmin2');
  412.             }
  413.  
  414.             $submit = $this->input->post('submit');
  415.  
  416.             if ($submit )
  417.             {
  418.                 $errMsg = $this->form_validation_reset_password();
  419.  
  420.                 if ( !$errMsg )
  421.                 {
  422.                     $user_info = $this->session->userdata('user_info');
  423.  
  424.                     $params = array(
  425.                         'user_id' => $user_info->id,
  426.                         'password' => $this->input->post('password'),
  427.                         );
  428.  
  429.                     $this->user_model->reset_password($params);
  430.  
  431.                     $this->template->write_view('content', 'user/reset_password_ok', '');
  432.                     $this->template->render();
  433.                     return;
  434.                 }
  435.                 else
  436.                 {
  437.                     $this->session->set_flashdata('flash_message', lang('ErrorInvalidInput'));
  438.                 }
  439.             }
  440.  
  441.             $this->template->write_view('content', 'user/reset_password', '');
  442.             $this->template->render();
  443.             $this->session->set_flashdata('temp_access', 1);
  444.             return;
  445.         }
  446.         else
  447.             $this->session->set_flashdata('flash_message', lang('ErrorPermissionDenied'));
  448.  
  449.         redirect('user/login');
  450.     }
  451.  
  452.     function proccess_reset_password(){
  453.  
  454.         $user_info = $this->session->userdata('user_info');
  455.  
  456.         if( !isset($user_info->username) and $this->uri->segment(2) != 'login')
  457.             show_404();
  458.  
  459.         $user_info = $this->session->userdata('user_info');
  460.         $params = array(
  461.             'user_id' => $user_info->id,
  462.             'password' => $this->input->post('password'),
  463.             );
  464.  
  465.         if($this->user_model->reset_password($params) == true)
  466.         {
  467.             $respon = array('respon' => 'true', 'msg' => lang('SuccessDataUpdate'));
  468.         }
  469.         else
  470.         {
  471.             $respon = array('respon' => 'true', 'msg' => lang('ErrorDataUpdateFailed'));
  472.         }
  473.         echo json_encode($respon);
  474.     }
  475.  
  476.     function form_validation_reset_password()
  477.     {
  478.         $this->form_validation->set_rules('password', lang('Password'), 'trim|required|xss_clean|min_length[6]|max_length[20]');
  479.  
  480.         if ( $this->form_validation->run() == False)
  481.         {
  482.             $errMsg = $this->form_validation->error_string() ;
  483.  
  484.             return $errMsg;
  485.         }
  486.         else
  487.         {
  488.             return null;
  489.         }
  490.     }
  491.  
  492.     function forgot_password_verify( $token = null)
  493.     {
  494.         show_404();
  495.  
  496.         if( $token )
  497.         {
  498.             $params = array('token' => $token);
  499.             list($flag , $user, $message) = $this->user_model->forgot_password_verify($params);
  500.  
  501.             if ( $flag )
  502.             {
  503.                 $update_params = array('user_id' => $user->id);
  504.                 list($update_flag , $update_res, $update_message) = $this->user_model->forgot_password_verify_update($update_params);
  505.                 $this->session->userdata('user_info', $user);
  506.                 $this->session->set_flashdata('temp_access', 1);
  507.                 redirect('user/reset_password');
  508.             }
  509.         }
  510.         else
  511.             null;
  512.  
  513.         $this->template->write_view('content', 'user/forgot_password_verify_failed', '');
  514.         $this->template->render();
  515.     }
  516.  
  517.     function forgot_password()
  518.     {
  519.         show_404();
  520.  
  521.         $submit = $this->input->post('submit');
  522.         $email = $this->input->post('email');
  523.  
  524.         if ( $submit and $email)
  525.         {
  526.             $params = array('email' => $email);
  527.             $user = $this->user_model->profile2($params);
  528.  
  529.             $this->session->set_flashdata('flash_message', lang('AnEmailHasBeenSent'));
  530.  
  531.             if ( $user )
  532.             {
  533.                 $token = md5(rand(10000000,1000000000));
  534.                 $params = array(
  535.                     'email' => $email,
  536.                     'token' => $token,
  537.                     'user_id' => $user->id,
  538.                     );
  539.                 $this->user_model->insert_forgot_password_token($params);
  540.  
  541.                 $data = array('email' => $email, 'token' => $token, 'user_info' => $user);
  542.                 $this->load->library('phpmailer2');
  543.                 $this->phpmailer2->addAddress($email, $user->name);
  544.                 $this->phpmailer2->addFrom($this->cs_email, 'Customer Service BOSS');
  545.                 $this->phpmailer2->subject(lang('ForgotPassword'));
  546.                 $this->phpmailer2->message($this->load->view('user/forgot_password_mail', $data, TRUE) );
  547.  
  548. //                $this->phpmailer2->print_debugger();
  549.  
  550.                 if ( !$this->phpmailer2->send() )
  551.                 {
  552.                     log_message('error',sprintf('%s (%s): %s, %s', __FILE__,__LINE__,'Email not sent', $this->phpmailer2->ErrorInfo));
  553.                 }
  554.                 redirect('user/login');
  555.             }
  556.             else
  557.                 null;
  558.  
  559.             redirect('user/forgot_password');
  560.         }
  561.         else
  562.             null;
  563.  
  564.         $this->template->write_view('content', 'user/forgot_password', '');
  565.         $this->template->render();
  566.     }
  567.  
  568.     function form_validation_agent_request()
  569.     {
  570.         $this->form_validation->set_rules('id', lang('ID'), 'trim|required|xss_clean');
  571.         $this->form_validation->set_rules('upline_id', lang('UplineID'), 'trim|required|xss_clean');
  572.         $this->form_validation->set_rules('agent_code', lang('AgentCode'), 'trim|required|xss_clean|callback_unique_agentcode');
  573.         $this->form_validation->set_rules('join_payment', lang('JoinPayment'), 'trim|required|xss_clean|max_length[40]');
  574.  
  575.         if ( $this->form_validation->run() == False)
  576.         {
  577.             $errMsg = $this->form_validation->error_string() ;
  578.  
  579.             return $errMsg;
  580.         }
  581.         else
  582.         {
  583.             return null;
  584.         }
  585.     }
  586.  
  587.     function unique_agentcode($str)
  588.     {
  589.         if ( !$this->valid )
  590.             die(_ajax_upload_error(lang('ErrorPermissionDenied')));
  591.  
  592.         $is_existed = $this->user_model->unique_agentcode($str);
  593.  
  594.         if ( $is_existed )
  595.         {
  596.             $this->form_validation->set_message('unique_agentcode', sprintf('%s -> %s', lang('AgentCode'), lang('Existed')));
  597.             return False;
  598.         }
  599.         else
  600.             return True;
  601.     }
  602.  
  603.     function agent_request()
  604.     {
  605.         // FIXME, send email to any member that is promoted
  606.         if ( $this->valid )
  607.             $this->template->set_template('sbadmin2');
  608.         else
  609.         {
  610.             $this->session->set_flashdata('flash_message', lang('ErrorPermissionDenied'));
  611.             redirect('app/index');
  612.         }
  613.  
  614.         $submit = $this->input->post('submit');
  615.         $void = $this->input->post('void');
  616.         $id = $this->input->post('id');
  617.         $data = array();
  618.  
  619.         if ( $void and $id )
  620.         {
  621.             $params = array(
  622.                 'id' => $id,
  623.                 'idelete' => 1,
  624.             );
  625.  
  626.             list($flag, $result, $message) = $this->user_model->agent_request_update($params);
  627.         }
  628.  
  629.         if ( $submit and $id )
  630.         {
  631.             //$errMsg = $this->form_validation_agent_request();
  632.  
  633.             //if ( $errMsg )
  634.             //    die(_ajax_upload_error($errMsg));
  635.  
  636.             unset($_POST['submit']);
  637.             unset($_POST['void']);
  638.  
  639.             if ( $this->input->post('registration_date') )
  640.             {
  641.                 $reg_date = explode(' ',$this->input->post('registration_date'));
  642.  
  643.                 if(count($reg_date) == 2)
  644.                     $_POST['registration_date'] = reverseDate($reg_date[0]).' '.$reg_date[1];
  645.                 else
  646.                     $_POST['registration_date'] = reverseDate($reg_date[0]);
  647.             }
  648.  
  649.             list($flag, $result, $message) = $this->user_model->agent_request_update($this->input->post());
  650.  
  651.             if ( $flag )
  652.                 die(_ajax_upload_success($message));
  653.             else
  654.                 die(_ajax_upload_error($message));
  655.         }
  656.         else
  657.             null;
  658.  
  659.         $params = array(
  660.             'is_count' => true,
  661.             'idelete' => $APPROVED = 0,
  662.             );
  663.         $data['approved'] = $this->user_model->agent_request($params);
  664.  
  665.         $params['idelete'] = $VOID = 1;
  666.         $data['void'] = $this->user_model->agent_request($params);
  667.  
  668.         $params['idelete'] = $PENDING = 3;
  669.         $data['pending'] = $this->user_model->agent_request($params);
  670.  
  671.         $params = array('noparent' => true);
  672.         $data['noparent_records'] = $this->user_model->agent_request($params);
  673.  
  674.         $params = array('withparent' => true);
  675.         $data['withparent_records'] = $this->user_model->agent_request($params);
  676.  
  677.         $this->template->write_view('content', 'user/agent_request', $data);
  678.         $this->template->render();
  679.     }
  680.  
  681.     function agent_request_modal($id = null)
  682.     {
  683.         if ( !$this->valid )
  684.             die(_ajax_upload_error(lang('ErrorPermissionDenied')));
  685.  
  686.         if ( !$id )
  687.             die(lang('ErrorIdIsNull'));
  688.  
  689.         $params= array('id' => $id);
  690.         $data['request_record'] = $this->user_model->agent_request($params);
  691.         $data['branches'] = $this->user_model->branch();
  692.         $data['record'] = $this->user_model->profile(null, $data['request_record']->user_id);
  693.  
  694.         $this->load->view('user/modal_agent', $data);
  695.     }
  696.  
  697.     function uplineid_autocomplete()
  698.     {
  699.         if ( !$this->valid )
  700.             die(_ajax_upload_error(lang('ErrorPermissionDenied')));
  701.  
  702.         $result = $this->user_model->uplineid_autocomplete( $this->input->post('term') );
  703.         echo json_encode( $result );
  704.         die();
  705.     }
  706.  
  707.     function agentcode_autocomplete()
  708.     {
  709.         if ( !$this->valid )
  710.             die(_ajax_upload_error(lang('ErrorPermissionDenied')));
  711.  
  712.         $result = $this->user_model->agentcode_autocomplete( $this->input->post('term') );
  713.         echo json_encode( $result );
  714.         die();
  715.     }
  716.  
  717.     function subscriber_autocomplete()
  718.     {
  719.         if ( !$this->valid )
  720.             die(_ajax_upload_error(lang('ErrorPermissionDenied')));
  721.  
  722.         $result = $this->user_model->subscriber_autocomplete( $this->input->post('term') );
  723.         echo json_encode( $result );
  724.         die();
  725.     }
  726.  
  727.     function agent_autocomplete()
  728.     {
  729.         if ( !$this->valid )
  730.             die(_ajax_upload_error(lang('ErrorPermissionDenied')));
  731.  
  732.         $result = $this->user_model->agent_autocomplete( $this->input->post('term') );
  733.         echo json_encode( $result );
  734.         die();
  735.     }
  736.  
  737.     function reset_and_email()
  738.     {
  739.         $this->gox_acl->validate_update();
  740.         if ( !$this->valid )
  741.             die(_ajax_upload_error(lang('ErrorPermissionDenied')));
  742.  
  743.         $id = $this->input->post('id');
  744.  
  745.         if ( $id )
  746.         {
  747.             $params = array(
  748.                 'user_id' => $id,
  749.                 'password' => '123456',
  750.                 'bverified' => 1,
  751.                 );
  752.  
  753.             list( $flag, $user_id, $message) = $this->user_model->reset_password($params);
  754.  
  755.             if ( !$flag )
  756.                 die(_ajax_upload_error(lang('ErrorDataUpdateFailed')));
  757.  
  758.             $data_mail['user_info'] = $this->user_model->profile2(array('id' => $id));
  759.             $data_mail['default_pwd'] = $params['password'];
  760.  
  761.             $this->load->library('phpmailer2');
  762.  
  763.             $this->phpmailer2->addAddress($data_mail['user_info']->email, $data_mail['user_info']->name);
  764.             $this->phpmailer2->addFrom($this->cs_email, 'Customer Service BOSS');
  765.             $this->phpmailer2->addBcc('adesantoasman@gmail.com', 'adesanto asman');
  766. //            $this->phpmailer2->addBcc('marlinda.tjhie@gmail.com', 'Marlinda');
  767.             $this->phpmailer2->subject('Password Reset by Admin');
  768.             $this->phpmailer2->message($this->load->view('user/reset_pwd_by_admin', $data_mail, TRUE) );
  769.  
  770. //                $this->phpmailer2->print_debugger();
  771.  
  772.             if ( !$this->phpmailer2->send() )
  773.             {
  774.                 log_message('error',sprintf('%s (%s): %s, %s', __FILE__,__LINE__,'Email not sent', $this->phpmailer2->ErrorInfo));
  775.             }
  776.  
  777.             die(_ajax_upload_success(lang('Success')));
  778.         }
  779.         else
  780.             die(_ajax_upload_error(lang('ErrorIdIsNull')));
  781.     }
  782.  
  783.     function user_pic_upload()
  784.     {
  785.         if ( !$this->valid )
  786.             die(_ajax_upload_error(lang('ErrorPermissionDenied')));
  787.  
  788.         # @todo auto resize the picture to allowed size
  789.        $id = $this->input->post('id');
  790.         $USERPIC = 20;
  791.  
  792.         if ( !$id )
  793.             die(_ajax_upload_error(lang('ErrorIdIsNull')));
  794.  
  795.         $newfile = date('YmdHis') . rand(0,1000);
  796.  
  797.         $ext = get_uploaded_file_extention( $_FILES['file']['name']);
  798.         $params = array(
  799.             'path' => 'upload/user/pic/'.$newfile. $ext,
  800.             'id' => $id,
  801.             'ifiles' => $USERPIC,
  802.             );
  803.  
  804.         $flag = $this->user_model->insert_user_upload($params);
  805.  
  806.         if ( $flag )
  807.         {
  808.             if (!empty($_FILES))
  809.             {
  810.                 $tempFile = $_FILES['file']['tmp_name'];
  811.                 $path = realpath(FCPATH). FILE_SEPARATOR. $params['path'];
  812.                 $targetPath = $path;
  813.  
  814.                 move_uploaded_file($tempFile, $targetPath);
  815.             }
  816.  
  817.               $data['error'] = null;
  818.         }
  819.         else
  820.         {
  821.             $data['error'] = $message;
  822.         }
  823.     }
  824.  
  825.     function user_id_upload()
  826.     {
  827.         if ( !$this->valid )
  828.             die(_ajax_upload_error(lang('ErrorPermissionDenied')));
  829.             # Fixme add notation if public or private access, specially form_validation
  830.            # @todo auto resize the picture to allowed size
  831.  
  832.         $USERID = 10;
  833.  
  834.         $id = $this->input->post('id');
  835.  
  836.         if ( !$id )
  837.             die(_ajax_upload_error(lang('ErrorIdIsNull')));
  838.  
  839.         $newfile = date('YmdHis') . rand(0,1000);
  840.  
  841.         $ext = get_uploaded_file_extention( $_FILES['file']['name']);
  842.         $params = array(
  843.             'path' => 'upload/user/id/'.$newfile. $ext,
  844.             'id' => $id,
  845.             'ifiles' => $USERID,
  846.             );
  847.  
  848.         $flag = $this->user_model->insert_user_upload($params);
  849.  
  850.         if ( $flag )
  851.         {
  852.             if (!empty($_FILES))
  853.             {
  854.                 $tempFile = $_FILES['file']['tmp_name'];
  855.                 $path = realpath(FCPATH). FILE_SEPARATOR. $params['path'];
  856.                 $targetPath = $path;
  857.  
  858.                 move_uploaded_file($tempFile, $targetPath);
  859.             }
  860.  
  861.             $data['error'] = null;
  862.         }
  863.         else
  864.         {
  865.             $data['error'] = $message;
  866.         }
  867.     }
  868.  
  869.     function user_image_delete()
  870.     {
  871.         if ( !$this->valid )
  872.             die(_ajax_upload_error(lang('ErrorPermissionDenied')));
  873.  
  874.         $DELETE = 1;
  875.         $id = $this->input->post('id');
  876.         $params = array(
  877.             'id' => $id,
  878.             'idelete' => $DELETE,
  879.             );
  880.         list( $flag, $retId, $message ) = $this->user_model->update_user_pic($params);
  881.  
  882.         if ( $flag )
  883.         {
  884.             $pic = $this->user_model->user_pic(array('id' => $id));
  885.             $path = realpath(FCPATH). FILE_SEPARATOR. $pic->path;
  886.             unlink($path);
  887.         }
  888.         die(_ajax_upload_success($message));
  889.     }
  890.  
  891.     function report_user(){
  892.         $user_info = $this->session->userdata('user_info');
  893.  
  894.         if( !isset($user_info->username))
  895.             show_404();
  896.  
  897.         $data = array();
  898.         $this->template->set_template('sbadmin2');
  899.         $this->segment   = 6;
  900.  
  901.         $params = array(
  902.             'filter'     =>  $this->uri->segment(3),
  903.             'start_date' =>  to_date($this->uri->segment(4)),
  904.             'end_date'   =>  to_date($this->uri->segment(5)),
  905.             'offset'     =>  $this->start_page($this->segment),
  906.             'limit'      =>  $this->perPage
  907.         );
  908.  
  909.         $this->linkPage  = site_url("admin/report_user/".$params['filter'].'/'.$params['start_date'].'/'.$params['end_date']);
  910.         $this->totalPage = $this->user_model->search_user($params, true);
  911.         $data['list']    = $this->user_model->search_user($params, false);
  912.         $data['start']   = $params['offset']+1;
  913.         $data['total']   = $this->totalPage;
  914.         $data['link']    = $this->pagination();
  915.         $this->template->write_view('content', 'user/report', $data);
  916.         $this->template->render();
  917.     }
  918.  
  919.     function search_user(){
  920.         $user_info = $this->session->userdata('user_info');
  921.  
  922.         if( !isset($user_info->username))
  923.             show_404();
  924.  
  925.         $this->segment   = 6;
  926.         $params = array(
  927.             'filter'     =>  $this->input->post('filter'),
  928.             'start_date' =>  to_date($this->input->post('start_date')),
  929.             'end_date'   =>  to_date($this->input->post('end_date')),
  930.             'offset'     =>  $this->start_page($this->segment),
  931.             'limit'      =>  $this->perPage
  932.             );
  933.         $this->linkPage  = site_url("admin/report_user/".$params['filter'].'/'.$params['start_date'].'/'.$params['end_date']);
  934.         $this->totalPage = $this->user_model->search_user($params, true);
  935.         $data['link']    = $this->pagination();
  936.         $data['start']   = $params['offset']+1;
  937.         $data['total']   = $this->totalPage;
  938.         $data['data']    = $this->user_model->search_user($params, false);
  939.         $this->load->view('user/resultSearchUser', $data);
  940.     }
  941.  
  942.     function export_user(){
  943.         $user_info = $this->session->userdata('user_info');
  944.  
  945.         if( !isset($user_info->username) )
  946.             show_404();
  947.  
  948.         $this->load->library('pexcel');
  949.         $params = array(
  950.             'filter'     =>  $this->uri->segment(3),
  951.             'start_date' =>  to_date($this->uri->segment(4)),
  952.             'end_date'   =>  to_date($this->uri->segment(5))
  953.             );
  954.         $data = $this->user_model->search_user($params);
  955.             // Create new PHPExcel object
  956.         $sheet = new PHPExcel();
  957.  
  958.             // Set document properties
  959.         $sheet->getProperties()->setCreator("soechi")
  960.         ->setLastModifiedBy("last.com")
  961.         ->setTitle("Office 2007 XLSX User Document")
  962.         ->setSubject("Office 2007 XLSX User Document")
  963.         ->setDescription("User document for Office 2007 XLSX")
  964.         ->setKeywords("office 2007")
  965.         ->setCategory("Export user file");
  966.  
  967.             // Add some data
  968.         $sheet->setActiveSheetIndex(0)
  969.         ->setCellValue('A2', 'NO')
  970.         ->setCellValue('B2', 'NAME')
  971.         ->setCellValue('C2', 'NO ID')
  972.         ->setCellValue('D2', 'INVITED BY')
  973.         ->setCellValue('E2', 'HP 1')
  974.         ->setCellValue('F2', 'HP 2')
  975.         ->setCellValue('G2', 'BB')
  976.         ->setCellValue('H2', 'EMAIL')
  977.         ->setCellValue('I2', 'ADDRESS')
  978.         ->setCellValue('J2', 'NO IDENTITY CARD')
  979.         ->setCellValue('K2', 'BIRTH OF DATE')
  980.         ->setCellValue('L2', 'JOIN DATE')
  981.         ->setCellValue('M2', 'BRANCH DEDICATION')
  982.         ->setCellValue('N2', 'KETERANGAN');
  983.  
  984.         $sheet->getActiveSheet()->getColumnDimension('A')->setWidth(5);
  985.         $sheet->getActiveSheet()->getColumnDimension('B')->setWidth(40);
  986.         $sheet->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  987.         $sheet->getActiveSheet()->getColumnDimension('D')->setWidth(25);
  988.         $sheet->getActiveSheet()->getColumnDimension('E')->setWidth(25);
  989.         $sheet->getActiveSheet()->getColumnDimension('F')->setWidth(25);
  990.         $sheet->getActiveSheet()->getColumnDimension('G')->setWidth(25);
  991.         $sheet->getActiveSheet()->getColumnDimension('H')->setWidth(40);
  992.         $sheet->getActiveSheet()->getColumnDimension('I')->setWidth(60);
  993.         $sheet->getActiveSheet()->getColumnDimension('J')->setWidth(25);
  994.         $sheet->getActiveSheet()->getColumnDimension('K')->setWidth(25);
  995.         $sheet->getActiveSheet()->getColumnDimension('L')->setWidth(20);
  996.         $sheet->getActiveSheet()->getColumnDimension('M')->setWidth(25);
  997.         $sheet->getActiveSheet()->getColumnDimension('N')->setWidth(25);
  998.  
  999.         $sheet->getActiveSheet()->getRowDimension('2')->setRowHeight(30);
  1000.  
  1001.         $sheet->getActiveSheet()->getStyle('B1:N2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1002.         $sheet->getActiveSheet()->getStyle('B1:N2')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
  1003.         $sheet->getActiveSheet()->getStyle('B1:N2')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
  1004.         $sheet->getActiveSheet()->getStyle('B1:N2')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
  1005.         $sheet->getActiveSheet()->getStyle('B1:N2')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
  1006.  
  1007.         $sheet->getActiveSheet()->getStyle('B1:N2')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FF00FF00');
  1008.  
  1009.         $i=4; $no=1;
  1010.         foreach ($data as $val):
  1011.             $sheet->setActiveSheetIndex(0)
  1012.         ->setCellValue("A$i", $no)
  1013.         ->setCellValue("B$i", anchor("admin/get_user_profile/$val->userid", $val->user_name, 'target="_blank"'))
  1014.         ->setCellValue("C$i", 'NO ID')
  1015.         ->setCellValue("D$i", 'INVITED BY')
  1016.         ->setCellValue("E$i", $val->hp)
  1017.         ->setCellValue("F$i", 'HP 2')
  1018.         ->setCellValue("G$i", 'BB')
  1019.         ->setCellValue("H$i", $val->user_email)
  1020.         ->setCellValue("I$i", $val->address)
  1021.         ->setCellValue("J$i", 'NO IDENTITY CARD')
  1022.         ->setCellValue("K$i", 'BIRTH OF DATE')
  1023.         ->setCellValue("L$i", to_date($val->join_date))
  1024.         ->setCellValue("M$i", $val->branch_name)
  1025.         ->setCellValue("N$i", 'KETERANGAN');
  1026.         $i++; $no++;
  1027.         endforeach;
  1028.  
  1029.             // Rename worksheet (worksheet, not filename)
  1030.         $sheet->getActiveSheet()->setTitle('sheet1');
  1031.  
  1032.  
  1033.             // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  1034.         $sheet->setActiveSheetIndex(0);
  1035.  
  1036.             //this is the header given from PHPExcel examples. but the output seems somewhat corrupted in some cases.
  1037.             //header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  1038.             //so, we use this header instead.
  1039.         header('Content-type: application/vnd.ms-excel');
  1040.         header('Content-Disposition: attachment;filename="export_user.xlsx"');
  1041.         header('Cache-Control: max-age=0');
  1042.  
  1043.         $objWriter = PHPExcel_IOFactory::createWriter($sheet, 'Excel2007');
  1044.         $objWriter->save('php://output');
  1045.     }
  1046.  
  1047.     function modal_edit_profile($id = null)
  1048.     {
  1049.         $this->gox_acl->validate_update();
  1050.         $user_info = $this->session->userdata('user_info');
  1051.  
  1052.         if( !isset($user_info->username))
  1053.             show_404();
  1054.  
  1055.         if (!$this->valid)
  1056.             die(_ajax_upload_error(lang('ErrorPermissionDenied')));
  1057.  
  1058.         if (!$id)
  1059.             die(lang('ErrorIdIsNull'));
  1060.  
  1061.         $this->load->helper('goexpress');
  1062.         $user_info = $this->session->userdata('user_info');
  1063.         $user_params = array(
  1064.             'priv' => $user_info->user_type_id,
  1065.             'dropdown' => True,
  1066.         );
  1067.  
  1068.         $params = array('id' => $id);
  1069.         $data['branches'] = $this->user_model->branch();
  1070.         $data['trade_area']    = $this->admin_model->list_trade_area();
  1071.         $data['branches']      = $this->user_model->branches();
  1072.         $data['vessel']        = $this->admin_model->list_vessel();
  1073.         $data['rank']          = $this->admin_model->list_rank();
  1074.         $data['crew_criteria'] = $this->admin_model->list_crew_criteria();
  1075.         $data['nasionality']   = $this->admin_model->list_nasionality();
  1076.         $data['user_type']   = $this->user_model->user_type( $user_params );
  1077.         $data['ideletes']   = $this->user_model->ideletes($user_params);
  1078.         $data['record'] = $this->user_model->profile(null, $id);
  1079.         $data['user_info'] = $user_info;
  1080.  
  1081.         $this->load->view('user/modal_edit_profile', $data);
  1082.     }
  1083.  
  1084.     function edit_profile()
  1085.     {
  1086.         $this->gox_acl->validate_update();
  1087.         $user_info = $this->session->userdata('user_info');
  1088.  
  1089.         if( !isset($user_info->username) )
  1090.             show_404();
  1091.  
  1092.         if (!$this->valid)
  1093.             die(_ajax_upload_error(lang('ErrorPermissionDenied')));
  1094.  
  1095.         $submit = $this->input->post('submit');
  1096.         $void = $this->input->post('void');
  1097.         $id = $this->input->post('id');
  1098.  
  1099.         $data = array();
  1100.  
  1101.         if ($void and $id) {
  1102.             $params = array(
  1103.                 'id' => $id,
  1104.                 'idelete' => 1,
  1105.             );
  1106.             $this->user_model->delete_user($params);
  1107.             die(_ajax_upload_success(lang("SuccessDataUpdate")));
  1108.         }
  1109.  
  1110.         if ($id) {
  1111.  
  1112.             $record = $this->user_model->profile(null, $id);
  1113.  
  1114.             if ( $this->input->post('registration_date') )
  1115.             {
  1116.                 $reg_date = explode(' ',$this->input->post('registration_date'));
  1117.  
  1118.                 if(count($reg_date) == 2)
  1119.                     $_POST['registration_date'] = reverseDate($reg_date[0]).' '.$reg_date[1];
  1120.                 else
  1121.                     $_POST['registration_date'] = reverseDate($reg_date[0]);
  1122.             }
  1123.  
  1124.             $this->db->trans_start();
  1125.             list($flag, $result, $message) = $this->user_model->edit_profile($this->input->post());
  1126.  
  1127.             if ($flag)
  1128.             {
  1129.                 $this->db->trans_complete();
  1130.                 die(_ajax_upload_success($message));
  1131.             }
  1132.             else
  1133.             {
  1134.                 $this->db->trans_rollback();
  1135.                 die(_ajax_upload_error($message));
  1136.             }
  1137.         } else
  1138.             die(_ajax_upload_error(lang('ErrorIdIsNull')));
  1139.     }
  1140.  
  1141.     function user_count()
  1142.     {
  1143.         $user_info = $this->session->userdata('user_info');
  1144.  
  1145.         if( !isset($user_info->username) )
  1146.             show_404();
  1147.  
  1148.         $params = array();
  1149.         echo $this->user_model->agents( $params , $count = true);
  1150.         die();
  1151.     }
  1152.  
  1153.     function get_levelid_by_rankid( $rankid )
  1154.     {
  1155.         switch( $rankid )
  1156.         {
  1157.             case 715:
  1158.                 return 15;
  1159.             case 716:
  1160.                 return 16;
  1161.             case 717:
  1162.                 return 17;
  1163.             case 720:
  1164.                 return 20;
  1165.  
  1166.             case 725:
  1167.                 return 25;
  1168.  
  1169.             case 735:
  1170.                 return 27;
  1171.  
  1172.             case 780:
  1173.                 return 50;
  1174.  
  1175.             case 785:
  1176.             default:
  1177.                 return 55;
  1178.         }
  1179.     }
  1180.  
  1181.     function add_user()
  1182.     {
  1183.         $user_info = $this->session->userdata('user_info');
  1184.  
  1185.         if( !isset($user_info->username) )
  1186.             show_404();
  1187.  
  1188.         $submit = $this->input->post('submit');
  1189.  
  1190.         if( $submit )
  1191.         {
  1192.             $level_id = $this->get_levelid_by_rankid($this->input->post('rank_id'));
  1193.  
  1194.             $params['input'] = array(
  1195.                 'name' => $this->input->post('name'),
  1196.                 'username' => $this->input->post('username'),
  1197.                 'email' => $this->input->post('email'),
  1198.                 'password' => '123456',
  1199.                 'hp' => '123456',
  1200.                 'rank_id' => $this->input->post('rank_id'),
  1201.                 'level_id' => $level_id,
  1202.                 'bverified' => 1,
  1203.             );
  1204.  
  1205.             $new_uid = $this->user_model->add_user($params);
  1206.  
  1207.             $params = array(
  1208.                 'user_id' => $new_uid,
  1209.                 'department_id' => $this->input->post('department_id'),
  1210.             );
  1211.  
  1212.             list( $flag, $insert_id, $message ) = $this->user_model->add_user_department( $params );
  1213.  
  1214.             $this->session->set_flashdata('message', $message);
  1215.             if ( $flag )
  1216.             {
  1217.                 redirect('user/add_user');
  1218.                 die();
  1219.             }
  1220.             else
  1221.                 $data['error_message'] .= $message; // Continue to previous form
  1222.         }
  1223.  
  1224.         $data['ranks'] = $this->admin_model->crew_rank_dropdown(
  1225.             array('is_management' => 1)
  1226.         );
  1227.         $data['departments'] = $this->admin_model->department_dropdown();
  1228.  
  1229.         $this->template->write_view('content', 'user/add_user', $data);
  1230.         $this->template->render();
  1231.     }
  1232.  
  1233.     function add_partner()
  1234.     {
  1235.         $user_info = $this->session->userdata('user_info');
  1236.  
  1237.         if( !isset($user_info->username) ){
  1238.             show_404();
  1239.         }
  1240.  
  1241.         $submit = $this->input->post('submit');
  1242.  
  1243.         if( $submit ){
  1244.             $params['input'] = array(
  1245.                 'name' => $this->input->post('name'),
  1246.                 'username' => $this->input->post('username'),
  1247.                 'email' => $this->input->post('email'),
  1248.                 'password' => $this->input->post('password'),
  1249.                 'hp' => '123456',
  1250.                 'rank_id' =>'0',
  1251.                 'level_id' => '60',
  1252.                 'user_type_id' => '60',
  1253.                 'bverified' => 1,
  1254.             );
  1255.  
  1256.             $new_uid = $this->user_model->add_user($params);
  1257.  
  1258.             if($new_uid != false){
  1259.                 $message = lang('SuccessDataCreation');
  1260.                 $flag = true;
  1261.             } else {
  1262.                 $message = lang('ErrorDataCreationFailed');
  1263.                 $flag = false;
  1264.             }
  1265.  
  1266.             $this->session->set_flashdata('message', $message);
  1267.             if ( $flag ){
  1268.                 redirect('user/add_partner');
  1269.                 die();
  1270.             } else {
  1271.                 $data['error_message'] .= $message; // Continue to previous form
  1272.             }
  1273.         }
  1274.  
  1275.         $this->template->write_view('content', 'user/add_partner');
  1276.         $this->template->render();
  1277.     }
  1278.  
  1279.     function json_registration()
  1280.     {
  1281.         $respon = array();
  1282.         $payload = file_get_contents('php://input');
  1283.  
  1284.         $this->load->model('model_goexpress');
  1285.  
  1286.         if( !$payload )
  1287.         {
  1288.             $respon = array('status' => 'failed', 'message' => 'Empty payload');
  1289.             die(json_encode($respon));
  1290.         }
  1291.  
  1292.         $json = json_decode( $payload );
  1293.  
  1294.         //print_r($json);
  1295.  
  1296.         if( !isset( $json->email ) )
  1297.         {
  1298.             $respon = array('status' => 'failed', 'message' => 'Empty Email');
  1299.             die(json_encode($respon));
  1300.         }
  1301.  
  1302.         $image_url = null;
  1303.         if( isset( $json->image_url ) )
  1304.         {
  1305.             $image_url = $json->image_url;
  1306.             if( isset($json->tipe) && $json->tipe == 'facebook'){
  1307.                 $url = file_get_contents('https://graph.facebook.com/'.$json->id.'/picture?width=256&height=256&redirect=false');
  1308.                 $image_json = json_decode($url);
  1309.                 $image_url = $image_json->data->url;
  1310.             }
  1311.         }
  1312.  
  1313.         // FIXME Do phone validation
  1314.         //
  1315.  
  1316.         $data = array(
  1317.             'username' => $json->id,
  1318.             'email' => $json->email,
  1319.             'name' => $json->name,
  1320.             'picurl' => $image_url,
  1321.             'user_type_id' => $json->type_id,
  1322.             'registration_date' => date('Y-m-d')
  1323.         );
  1324.  
  1325.         $callback = $this->model_goexpress->save_registration($data);
  1326.  
  1327.         if($callback){
  1328.             $respon = array('status' => 'ok', 'message' => 'Successfully saving user');
  1329.         }else{
  1330.             $respon = array('status' => 'failed', 'message' => 'Failed saving user');
  1331.         }
  1332.         echo json_encode($respon);
  1333.     }
  1334.  
  1335.     function driver_profile($username)
  1336.     {
  1337.         $user_info = $this->session->userdata('user_info');
  1338.  
  1339.         if( !isset($user_info->username) )
  1340.             show_404();
  1341.  
  1342.         $data = array();
  1343.         $params = array();
  1344.         $this->load->model('model_goexpress');
  1345.         $this->load->helper('claim');
  1346.  
  1347.         $params['username'] = $username;
  1348.         $params['driver_username'] = $username;
  1349.         $profile = $this->user_model->driver_profile($params);
  1350.  
  1351.         $data['records'] = $profile;
  1352.  
  1353.         $this->linkPage  = site_url("user/driver_profile/$username/");
  1354.         $this->segment   = 4;
  1355.         $this->perPage   = 10;
  1356.         $params['offset']= $this->start_page($this->segment);
  1357.         $params['limit'] = $this->perPage;
  1358.         $this->totalPage = $this->model_goexpress->get_tx_driver($params, true);
  1359.  
  1360.         $data['lists']      = $this->model_goexpress->get_tx_driver($params, false);
  1361.         $data['start']   = $params['offset']+1;
  1362.         $data['link']    = $this->pagination();
  1363.  
  1364.         $this->load->view('user/user_detail', $data);
  1365.     }
  1366.  
  1367.     function user_detail()
  1368.     {
  1369.         $user_info = $this->session->userdata('user_info');
  1370.  
  1371.         if( !isset($user_info->username) )
  1372.             show_404();
  1373.  
  1374.         $id = $this->uri->segment(3);
  1375.  
  1376.         if(!empty($id)){
  1377.             $this->load->model('model_goexpress');
  1378.             $this->load->helper('claim');
  1379.             $profile = $this->user_model->profile(null, $id);
  1380.             $data = array();
  1381.             $params = array();
  1382.             $data['records'] = $profile;
  1383.             if($profile->user_type_id == 30 || $profile->user_type_id == 90)
  1384.             {
  1385.                 $params['client'] = $profile->name;
  1386.             }
  1387.             else if($profile->user_type_id == 50)
  1388.             {
  1389.                  $params['driver_username'] = $profile->username;
  1390.             }
  1391.  
  1392.             $data['lists'] = $this->model_goexpress->get_tx($params);
  1393.  
  1394.             $this->load->view('user/user_detail', $data);
  1395.         }else{
  1396.             die('ID Not Found');
  1397.         }
  1398.     }
  1399.  
  1400.     function json_change_phone()
  1401.     {
  1402.         $respon = array();
  1403.         $payload = file_get_contents('php://input');
  1404.  
  1405.         $this->load->model('model_goexpress');
  1406.  
  1407.         if( !$payload )
  1408.         {
  1409.             $respon = array('status' => 'failed', 'message' => 'Empty payload');
  1410.             die(json_encode($respon));
  1411.         }
  1412.  
  1413.         $json = json_decode( $payload );
  1414.  
  1415.         //print_r($json);
  1416.  
  1417.         if( !isset( $json->no_hp ) )
  1418.         {
  1419.             $respon = array('status' => 'failed', 'message' => 'Empty Phone');
  1420.             die(json_encode($respon));
  1421.         }
  1422.  
  1423.         $data = array(
  1424.             'hp' => $json->no_hp,
  1425.             'edit_date' => date('Y-m-d H:i:s')
  1426.         );
  1427.  
  1428.         $callback = $this->model_goexpress->update_no_hp($data, $json->id);
  1429.  
  1430.         if($callback){
  1431.             $respon = array('status' => 'success', 'message' => 'Successfully update Phone Number');
  1432.         }else{
  1433.             $respon = array('status' => 'failed', 'message' => 'Failed');
  1434.         }
  1435.         echo json_encode($respon);
  1436.     }
  1437.  
  1438.     function json_change_image()
  1439.     {
  1440.         $respon = array();
  1441.         $target_file = "./upload/user/";
  1442.         $this->load->model('model_goexpress');
  1443.  
  1444.         if( !isset( $_POST['file_name'] ) )
  1445.         {
  1446.             $respon = array('status' => 'failed', 'message' => 'Empty File Image');
  1447.             die(json_encode($respon));
  1448.         }
  1449.  
  1450.         list($filename, $ext) = explode(".", $_POST['file_name']);
  1451.         $filename = date("YmdHis").rand(1000, 9999).'.'.$ext;
  1452.  
  1453.         $data = array(
  1454.                 'user_id' => $_POST['id'],
  1455.                 'picurl' => $filename,
  1456.             );
  1457.  
  1458.         if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file.$filename)) {
  1459.  
  1460.  
  1461.             $callback = $this->model_goexpress->save_profile_image($data);
  1462.  
  1463.             if($callback){
  1464.                 $respon = array('status' => 'success', 'message' => 'Successfully save image');
  1465.             }else{
  1466.                 $respon = array('status' => 'failed', 'message' => 'Failed');
  1467.             }
  1468.         } else {
  1469.             $respon = array('status' => 'failed', 'message' => 'Sorry, there was an error uploading your file.');
  1470.         }
  1471.  
  1472.         echo json_encode($respon);
  1473.     }
  1474.  
  1475.     function json_get_image2($username = null)
  1476.     {
  1477.         $respon = array();
  1478.         $this->load->model('model_goexpress');
  1479.  
  1480.         if( is_null( $username ) )
  1481.         {
  1482.             $respon = array('status' => 'failed', 'message' => 'Empty ID User');
  1483.             die(json_encode($respon));
  1484.         }
  1485.         $_POST['username'] = $username;
  1486.  
  1487.         $data['username'] = $_POST['username'];
  1488.         $callback = $this->model_goexpress->get_profile_image2($data);
  1489.  
  1490.         if(count($callback) > 0){
  1491.             $respon = array('status' => 'success', 'message' => 'Successfully get image', 'url_image' => base_url("upload/user/".$callback->picurl ));
  1492.         }else{
  1493.             $respon = array('status' => 'failed', 'message' => 'Failed');
  1494.         }
  1495.  
  1496.         echo json_encode($respon);
  1497.     }
  1498.  
  1499.     function json_get_image()
  1500.     {
  1501.         $respon = array();
  1502.         $this->load->model('model_goexpress');
  1503.  
  1504.         if( !isset( $_POST['id'] ) )
  1505.         {
  1506.             $respon = array('status' => 'failed', 'message' => 'Empty ID User');
  1507.             die(json_encode($respon));
  1508.         }
  1509.  
  1510.         $data['user_id'] = $_POST['id'];
  1511.         $callback = $this->model_goexpress->get_profile_image($data);
  1512.         //print_r($callback);
  1513.         //die();
  1514.         if(count($callback) > 0){
  1515.             $respon = array('status' => 'success', 'message' => 'Successfully get image', 'url_image' => "http://goexpress-kh.com/goexpress/upload/".$callback->picname);
  1516.         }else{
  1517.             $respon = array('status' => 'failed', 'message' => 'Failed');
  1518.         }
  1519.  
  1520.         echo json_encode($respon);
  1521.     }
  1522.  
  1523.     function json_get_phone()
  1524.     {
  1525.         $respon = array();
  1526.         $this->load->model('model_goexpress');
  1527.  
  1528.         if( !isset( $_POST['id'] ) )
  1529.         {
  1530.             $respon = array('status' => 'failed', 'message' => 'Empty ID User');
  1531.             die(json_encode($respon));
  1532.         }
  1533.  
  1534.         $data['user_id'] = $_POST['id'];
  1535.         $callback = $this->model_goexpress->get_phone($data);
  1536.  
  1537.         if(count($callback) > 0){
  1538.             $respon = array('status' => 'success', 'message' => 'Successfully getting Phone', 'phoneno' => $callback->hp);
  1539.         }else{
  1540.             $respon = array('status' => 'failed', 'message' => 'Failed');
  1541.         }
  1542.  
  1543.         echo json_encode($respon);
  1544.     }
  1545.  
  1546.     function json_driving()
  1547.     {
  1548.         $respon = array();
  1549.         $payload = file_get_contents('php://input');
  1550.  
  1551.         $this->load->model('model_goexpress');
  1552.  
  1553.         if( !$payload )
  1554.         {
  1555.             $respon = array('status' => 'failed', 'message' => 'Empty payload');
  1556.             die(json_encode($respon));
  1557.         }
  1558.  
  1559.         $json = json_decode( $payload );
  1560.  
  1561.         //print_r($json);
  1562.  
  1563.         if( !isset( $json->user_id ) )
  1564.         {
  1565.             $respon = array('status' => 'failed', 'message' => 'Empty User ID');
  1566.             die(json_encode($respon));
  1567.         }
  1568.  
  1569.         $data = array();
  1570.         foreach ($json->data as $value) {
  1571.             $data[] = array(
  1572.                 'txno'      => $json->txno,
  1573.                 'driver_id' => $json->user_id,
  1574.                 'timestamp' => $value->timestamp,
  1575.                 'lat'       => $value->lat,
  1576.                 'lng'       => $value->lng,
  1577.                 'action_id' => $json->action_id
  1578.             );
  1579.         }
  1580.  
  1581.         $callback = $this->model_goexpress->save_driving($data);
  1582.  
  1583.         if($callback){
  1584.             $respon = array('status' => 'success', 'message' => 'Successfully saved');
  1585.         }else{
  1586.             $respon = array('status' => 'failed', 'message' => 'Failed');
  1587.         }
  1588.         echo json_encode($respon);
  1589.     }
  1590.  
  1591.     function json_save_user()
  1592.     {
  1593.         $DRIVER = 50;
  1594.         $respon = array();
  1595.         $payload = file_get_contents('php://input');
  1596.  
  1597.         $this->load->model('model_goexpress');
  1598.  
  1599.         if( !$payload )
  1600.         {
  1601.             $respon = array('error' => 1, 'message' => 'Empty payload');
  1602.             die(json_encode($respon));
  1603.         }
  1604.  
  1605.         $json = json_decode( $payload );
  1606.  
  1607.         if ( !isset($json->fcm))
  1608.             $json->fcm= null;
  1609.  
  1610.         $filtered_hp = $this->_filter_number2($json->hp);
  1611.         $json->hp = str_replace(' ','',$json->hp);
  1612.  
  1613.         if( !isset( $json->hp ) )
  1614.         {
  1615.             $respon = array('error' => 1, 'error_message' => 'Empty No. Phone');
  1616.             die(json_encode($respon));
  1617.         }
  1618.  
  1619.         if( !isset( $json->email ) )
  1620.         {
  1621.             $respon = array('error' => 1, 'error_message' => 'Empty Email');
  1622.             die(json_encode($respon));
  1623.         }
  1624.  
  1625.         $bEmailExists = $this->model_goexpress->validate_email($json->email);
  1626.  
  1627.         if( $bEmailExists )
  1628.         {
  1629.             $respon = array('error' => 1, 'error_message' => 'Email has been Registered');
  1630.             die(json_encode($respon));
  1631.         }
  1632.  
  1633.         $user = $this->model_goexpress->get_user(array('hp' => $json->hp));
  1634.  
  1635.         if( count($user) > 0)
  1636.         {
  1637.             $respon = array('error' => 3, 'error_message' => 'Phone No. has been Registered');
  1638.             die(json_encode($respon));
  1639.         }
  1640.  
  1641.         $newID = md5( $json->hp. microtime(). $json->email );
  1642.         srand();
  1643.  
  1644.         $data = array(
  1645.             'username'  => $newID,
  1646.             'email'     => $json->email,
  1647.             'name'      => $json->name,
  1648.             'hp'        => $json->hp,
  1649.             'filtered_hp' => $filtered_hp,
  1650.             'password'  => $json->password,
  1651.             'bverified' => 0,
  1652.             'verification_code' => rand(1000, 9999),
  1653.             'registration_date' => date('Y-m-d H:i:s'),
  1654.             'hp' => $json->hp,
  1655.             'api_token' => $this->security_model->generate_apikey($newID,$json->fcm)
  1656.         );
  1657.  
  1658.         if(isset($json->isdvr) )
  1659.         {
  1660.             $data['user_type_id'] = $json->isdvr;
  1661.             $data['vehicle_id'] = $json->vehicle_id;
  1662.             $data['vehicle_plate_no'] = $json->vehicle_plate_no;
  1663.         }
  1664.  
  1665.         if(isset($json->isclt) )
  1666.         {
  1667.             $data['user_type_id'] = $json->isclt;
  1668.         }
  1669.  
  1670.         list($flag, $user_id, $msg ) = $this->model_goexpress->add_user($data);
  1671.  
  1672.         if(! $user_id )
  1673.         {
  1674.             $respon = array('error' => 1, 'error_message' => 'Registration Failed please contact our Customer Support', 'userid' => null);
  1675.             die(json_encode($respon));
  1676.         }else{
  1677.  
  1678.             $smsparams = array(
  1679.                 'create_date' => date('Y-m-d H:i:s'),
  1680.                 'hp' => $json->hp,
  1681.                 'user_id' => $user_id,
  1682.             );
  1683.             $this->user_model->add_sendsms($smsparams);
  1684.  
  1685.             if( $this->_filter_number($json->hp) )
  1686.             {
  1687.                 if( preg_match("/^\+855/", $json->hp) )
  1688.                 {
  1689.                     $this->iSendSmsViaGateway($filtered_hp, $data['verification_code']);
  1690.                 }
  1691.                 else
  1692.                 {
  1693.                     $datasms = array(
  1694.                         'user_id' => $user_id,
  1695.                         'sms_no' => $filtered_hp,
  1696.                         'server_info' => 'send_sms',
  1697.                         'message' => "GO-X.\nYour verification number is ".$data['verification_code']."\n លេខកូដរបស់អ្នកគឺ ". $data['verification_code'],
  1698.  
  1699.                     );
  1700.                     $this->sendGoogleCloudMessage(  $datasms, $ids = array() , array('separate' => True ), array('ret_int'=>1));
  1701.                 }
  1702.  
  1703.                 $respon = array('error' => 2, 'error_message' => 'Successfully saved, please verify your phone number', 'userid' => $newID);
  1704.                 die(json_encode($respon));
  1705.             }
  1706.             else
  1707.             {
  1708.                 $respon = array('error' => 1, 'error_message' => 'Registration Pending, Phone number is not local Cambodia. Please contact our Customer Support', 'userid' => null);
  1709.                 die(json_encode($respon));
  1710.             }
  1711.         }
  1712.     }
  1713.  
  1714.     function testsms($userid, $hp)
  1715.     {
  1716.         $datasms = array(
  1717.             'user_id' => $userid,
  1718.             'sms_no' => $hp,
  1719.             'server_info' => 'send_sms',
  1720.             'message' => 'Your verification number is TEST!',
  1721.  
  1722.         );
  1723.         $this->sendGoogleCloudMessage(  $datasms, $ids = array() , array('separate' => True ));
  1724.  
  1725.         $respon = array('error' => 2, 'error_message' => 'Successfully saved, please verify your phone number', 'userid' => $userid);
  1726.         die(json_encode($respon));
  1727.     }
  1728.  
  1729.  
  1730.     function json_user_login()
  1731.     {
  1732.         try
  1733.         {
  1734.             $ACTIVE = 0;
  1735.             $LOGIN_THRESHOLD = 3;
  1736.             $IS_CLIENT = 90;
  1737.             $IS_DRIVER = 50;
  1738.             $new_version = false;
  1739.             $respon = array();
  1740.             $payload = file_get_contents('php://input');
  1741.  
  1742.             $this->load->model('model_goexpress');
  1743.  
  1744.             if( !$payload )
  1745.             {
  1746.                 $respon = array('error' => 1, 'message' => 'Empty payload');
  1747.                 die(json_encode($respon));
  1748.             }
  1749.  
  1750.             $json = json_decode( $payload );
  1751.  
  1752.             if ( !isset($json->fcm))
  1753.                 $json->fcm = null;
  1754.  
  1755.             $json->hp = str_replace(' ','',$json->hp);
  1756.             if( (!isset( $json->hp ) or $json->hp == "" ) and !isset($json->id) )
  1757.             {
  1758.                 $respon = array('error' => 1, 'error_message' => 'Invalid PhoneNo');
  1759.                 die(json_encode($respon));
  1760.             }
  1761.  
  1762.             if( !isset( $json->password ) or $json->password=="")
  1763.             {
  1764.                 $respon = array('error' => 1, 'error_message' => 'Invalid Password');
  1765.                 die(json_encode($respon));
  1766.             }
  1767.  
  1768.             if ( isset($json->isclt) AND $json->isclt != $IS_CLIENT AND $json->isclt != $IS_DRIVER AND $new_version == true )
  1769.             {
  1770.                 $respon = array('error' => 1, 'error_message' => 'Unknown Application Version');
  1771.                 die(json_encode($respon));
  1772.             }
  1773.  
  1774.             $data = array(
  1775.                 'hp'        => $json->hp,
  1776.             );
  1777.  
  1778.             // ATTENTION! this code only to validate the re-auth of e-wallet
  1779.             // and intendedly to replace the $data['hp'] declared before this line
  1780.             if ( isset($json->id) AND isset($json->isclt) AND $json->isclt == $IS_CLIENT )
  1781.             {
  1782.                 $data = array(
  1783.                     'username'        => $json->id,
  1784.                 );
  1785.             }
  1786.  
  1787.             $get = $this->model_goexpress->get_user($data);
  1788.  
  1789.             if ( isset($json->id) AND isset($get->hp) AND isset($json->isclt) AND $json->isclt == $IS_CLIENT )
  1790.             {
  1791.                 $json->hp = $get->hp;
  1792.             }
  1793.  
  1794.             if( !empty($get) and $get->n_retries >= $LOGIN_THRESHOLD)
  1795.             {
  1796.                 if ( $get->locked == 1 ) {
  1797.                     if ( time() >= $get->locked_until ) {
  1798.                         $values= array();
  1799.                         $values['locked']    = 0;
  1800.                         $values['locked_until'] = 0;
  1801.                         $values['n_retries'] = 0;
  1802.                         $get->n_retries = 0;
  1803.                         list($flag, $user_id, $msg ) = $this->model_goexpress->add_user($values, $get->id);
  1804.                     }
  1805.                     else {
  1806.                         if ( $this->uri->segment(1) == "km") {
  1807.                             $messageval = "សូមមេត្ដាករង់ចាំ 15 នាទីដើម្បីសាកល្បងម្ដងទៀត";
  1808.                         }
  1809.                         else {
  1810.                             $messageval = "Please retry after 15 minutes";
  1811.                         }
  1812.                         $respon = array('error' => 4, 'error_message' => $messageval );
  1813.                         die(json_encode($respon));
  1814.                     }
  1815.                 }
  1816.                 else {
  1817.                     $values= array();
  1818.                     $values['locked']    = 1;
  1819.                     $values['locked_until'] = strtotime('+15 minute', time());
  1820.                     list($flag, $user_id, $msg ) = $this->model_goexpress->add_user($values, $get->id);
  1821.                     if ( $this->uri->segment(1) == "km") {
  1822.                         $messageval = "សូមមេត្ដាករង់ចាំ 15 នាទីដើម្បីសាកល្បងម្ដងទៀត";
  1823.                     }
  1824.                     else {
  1825.                         $messageval = "Please retry after 15 minutes";
  1826.                     }
  1827.                     $respon = array('error' => 4, 'error_message' => $messageval );
  1828.                     die(json_encode($respon));
  1829.                 }
  1830.             }
  1831.  
  1832.             if( count($get) > 0 )
  1833.             {
  1834.                 //disabled temp the verfication
  1835.                 if($get->is_forgotpwd == 1 && $json->password == $get->password){
  1836.                     $values= array();
  1837.                     $values['is_forgotpwd'] = 0;
  1838.                     $values['bverified']    = 1;
  1839.                     list($flag, $user_id, $msg ) = $this->model_goexpress->add_user($values, $get->id);
  1840.  
  1841.                     if ( isset($json->isreauth) and $json->isreauth == 1 )
  1842.                         $api_token = "";
  1843.                     else
  1844.                         $api_token = $this->security_model->generate_apikey($get->username,$json->fcm);
  1845.  
  1846.                     $respon = array(
  1847.                             'error' => 0,
  1848.                             'error_message' => 'Success',
  1849.                             'userid' => $get->username,
  1850.                             'email' => $get->email,
  1851.                             'name' => $get->name,
  1852.                             'promo_code' => $get->promo_code,
  1853.                             'ewallet' => $get->wallet,
  1854.                             'vehicle_name' => $get->vehicle_name,
  1855.                             'vehicle_plate_no' => $get->vehicle_plate_no,
  1856.                             'hp' => $get->hp,
  1857.                             'api_token' => $api_token
  1858.                         );
  1859.                     die(json_encode($respon));
  1860.                 }
  1861.                 else if($get->is_forgotpwd == 1 && $json->password == $get->tmp_password )
  1862.                 {
  1863.                     $values = array();
  1864.                     $values['is_forgotpwd'] = 0;
  1865.                     $values['bverified']    = 0;
  1866.                     list($flag, $user_id, $msg ) = $this->model_goexpress->add_user($values, $get->id);
  1867.  
  1868.                     $respon = array('error' => 2, 'error_message' => 'Success Password reset but need Verification');
  1869.                     die(json_encode($respon));
  1870.                 }else if($get->idelete != 0) {
  1871.                     $respon = array('error' => 4, 'error_message' => 'Account is Blocked');
  1872.                     die(json_encode($respon));
  1873.                 }else if ( $get->user_type_id != $json->isclt and ( isset($json->isclt) OR $new_version == true )  ) {
  1874.                     $respon = array('error' => 1, 'error_message' => 'Invalid Account');
  1875.                     die(json_encode($respon));
  1876.                 }
  1877.  
  1878.                 else if( $json->password == $get->password && $get->bverified == 1 and $get->is_forgotpwd == 0 and ( $get->user_type_id == $json->isclt or ( !isset($json->isclt) AND $new_version != true ) ) ) {
  1879.                     //Reset 0
  1880.                     $this->model_goexpress->reset_retries_user($json->hp, 0);
  1881.                     if ( $get->referal_code == null or $get->referal_code == '' )
  1882.                         $get->referal_code = $this->model_goexpress->generate_referal_code($get->username);
  1883.                     if ( isset($json->isreauth) and $json->isreauth == 1 )
  1884.                         $api_token = "";
  1885.                     else
  1886.                         $api_token = $this->security_model->generate_apikey($get->username, $json->fcm);
  1887.  
  1888.                     $respon = array(
  1889.                                 'error' => 0,
  1890.                                 'error_message' => 'Success',
  1891.                                 'userid' => $get->username,
  1892.                                 'email' => $get->email,
  1893.                                 'name' => $get->name,
  1894.                                 'promo_code' => $get->promo_code,
  1895.                                 'ewallet' => $get->wallet,
  1896.                                 'vehicle_name' => $get->vehicle_name,
  1897.                                 'vehicle_plate_no' => $get->vehicle_plate_no,
  1898.                                 'hp' => $get->hp,
  1899.                                 'api_token' => $api_token ,
  1900.                                 'referal_code' => $get->referal_code
  1901.                             );
  1902.                     die(json_encode($respon));
  1903.                 }else if( $json->password == $get->password && $get->bverified == 0 and $get->is_forgotpwd == 0) {
  1904.                     // temporary by-pass
  1905.                     $respon = array('error' => 2, 'error_message' => 'Error: Phone No not yet Verified');
  1906.                     //$respon = array(
  1907.                     //        'error' => 0,
  1908.                     //        'error_message' => 'Success',
  1909.                     //        'userid' => $get->username,
  1910.                     //        'email' => $get->email,
  1911.                     //        'name' => $get->name,
  1912.                     //        'promo_code' => $get->promo_code,
  1913.                     //        'ewallet' => $get->wallet,
  1914.                     //        'vehicle_name' => $get->vehicle_name,
  1915.                     //        'vehicle_plate_no' => $get->vehicle_plate_no,
  1916.                     //    );
  1917.                     die(json_encode($respon));
  1918.                 }else{
  1919.                     //update retries
  1920.                     $this->model_goexpress->reset_retries_user($json->hp, 1);
  1921.  
  1922.                     $respon = array('error' => 1,
  1923.                             'error_message' => sprintf('Invalid Phone No/Password <br>(%s) retry remain ', ($LOGIN_THRESHOLD - $get->n_retries)),
  1924.                             'info' => $get,
  1925.                     );
  1926.                     die(json_encode($respon));
  1927.                 }
  1928.             }else{
  1929.                 //update retries
  1930.                 $this->model_goexpress->reset_retries_user($json->hp, 1);
  1931.  
  1932.                 $respon = array('error' => 1, 'error_message' => 'Invalid Phone No/Password');
  1933.                 die(json_encode($respon));
  1934.             }
  1935.         }
  1936.         catch(Exception $e)
  1937.         {
  1938.             error_log("Login Json Exception: " + $e->getMessage());
  1939.         }
  1940.         error_log("Login Json Success");
  1941.     }
  1942.  
  1943.  
  1944.     function json_User_change_pass()
  1945.     {
  1946.         $ACTIVE = 0;
  1947.         $LOGIN_THRESHOLD = 3;
  1948.         $IS_CLIENT = 90;
  1949.         $IS_DRIVER = 50;
  1950.         $new_version = false;
  1951.         $respon = array();
  1952.         $payload = file_get_contents('php://input');
  1953.  
  1954.         $this->load->model('model_goexpress');
  1955.  
  1956.         if( !$payload )
  1957.         {
  1958.             $respon = array('error' => 1, 'message' => 'Empty payload');
  1959.             die(json_encode($respon));
  1960.         }
  1961.  
  1962.         $json = json_decode( $payload );
  1963.  
  1964.         if ( !isset($json->fcm))
  1965.             $json->fcm = null;
  1966.  
  1967.         $json->hp = str_replace(' ','',$json->hp);
  1968.         if( (!isset( $json->hp ) or $json->hp == "" ) and !isset($json->id) )
  1969.         {
  1970.             $respon = array('error' => 1, 'error_message' => 'Invalid PhoneNo');
  1971.             die(json_encode($respon));
  1972.         }
  1973.  
  1974.         if( !isset( $json->password ) or $json->password=="")
  1975.         {
  1976.             $respon = array('error' => 1, 'error_message' => 'Invalid Password');
  1977.             die(json_encode($respon));
  1978.         }
  1979.  
  1980.         if ( isset($json->isclt) AND $json->isclt != $IS_CLIENT AND $json->isclt != $IS_DRIVER AND $new_version == true )
  1981.         {
  1982.             $respon = array('error' => 1, 'error_message' => 'Unknown Application Version');
  1983.             die(json_encode($respon));
  1984.         }
  1985.  
  1986.         $data = array(
  1987.             'hp'        => $json->hp,
  1988.         );
  1989.  
  1990.         // ATTENTION! this code only to validate the re-auth of e-wallet
  1991.         // and intendedly to replace the $data['hp'] declared before this line
  1992.         if ( isset($json->id) AND isset($json->isclt) AND $json->isclt == $IS_CLIENT )
  1993.         {
  1994.             $data = array(
  1995.                 'username'        => $json->id,
  1996.             );
  1997.         }
  1998.  
  1999.         $get = $this->model_goexpress->get_user($data);
  2000.  
  2001.         if ( isset($json->id) AND isset($get->hp) AND isset($json->isclt) AND $json->isclt == $IS_CLIENT )
  2002.         {
  2003.             $json->hp = $get->hp;
  2004.         }
  2005.  
  2006.         if( !empty($get) and $get->n_retries >= $LOGIN_THRESHOLD)
  2007.         {
  2008.             $values= array();
  2009.             $values['idelete']    = 1;
  2010.             list($flag, $user_id, $msg ) = $this->model_goexpress->add_user($values, $get->id);
  2011.             $respon = array('error' => 4, 'error_message' => 'Account is Blocked');
  2012.             die(json_encode($respon));
  2013.         }
  2014.  
  2015.         if( count($get) > 0 )
  2016.         {
  2017.             if ( $get->password == $json->password AND strlen($get->password) == strlen($json->password) ) {
  2018.                 $newdata = array(
  2019.                                   'password' => $json->repass
  2020.                                 );
  2021.  
  2022.                 $this->model_goexpress->add_user($newdata, $get->id);
  2023.                 $respon = array('error' => 0, 'error_message' => 'Password was changed!');
  2024.                 die(json_encode($respon));
  2025.             }
  2026.             else {
  2027.                 $respon = array('error' => 1, 'error_message' => 'Wrong Password! Please Try again.');
  2028.                 die(json_encode($respon));
  2029.             }
  2030.         }
  2031.     }
  2032.  
  2033.     function json_driver_change_pwd()
  2034.     {
  2035.         $ACTIVE = 0;
  2036.         $LOGIN_THRESHOLD = 3;
  2037.         $IS_CLIENT = 90;
  2038.         $IS_DRIVER = 50;
  2039.         $new_version = false;
  2040.         $respon = array();
  2041.         $payload = file_get_contents('php://input');
  2042.  
  2043.         $this->load->model('model_goexpress');
  2044.  
  2045.         if( !$payload )
  2046.         {
  2047.             $respon = array('error' => 1, 'message' => 'Empty payload');
  2048.             die(json_encode($respon));
  2049.         }
  2050.  
  2051.         $json = json_decode( $payload );
  2052.  
  2053.         if ( !isset($json->fcm))
  2054.             $json->fcm = null;
  2055.  
  2056.         $json->hp = str_replace(' ','',$json->hp);
  2057.         if( (!isset( $json->hp ) or $json->hp == "" ) and !isset($json->id) )
  2058.         {
  2059.             $respon = array('error' => 1, 'error_message' => 'Invalid PhoneNo');
  2060.             die(json_encode($respon));
  2061.         }
  2062.  
  2063.         if( !isset( $json->password ) or $json->password=="")
  2064.         {
  2065.             $respon = array('error' => 1, 'error_message' => 'Invalid Password');
  2066.             die(json_encode($respon));
  2067.         }
  2068.  
  2069.         if ( isset($json->isclt) AND $json->isclt != $IS_CLIENT AND $json->isclt != $IS_DRIVER AND $new_version == true )
  2070.         {
  2071.             $respon = array('error' => 1, 'error_message' => 'Unknown Application Version');
  2072.             die(json_encode($respon));
  2073.         }
  2074.  
  2075.         $data = array(
  2076.             'hp'        => $json->hp,
  2077.         );
  2078.  
  2079.         // ATTENTION! this code only to validate the re-auth of e-wallet
  2080.         // and intendedly to replace the $data['hp'] declared before this line
  2081.         if ( isset($json->id) AND isset($json->isclt) AND $json->isclt == $IS_DRIVER )
  2082.         {
  2083.             $data = array(
  2084.                 'username'        => $json->id,
  2085.             );
  2086.         }
  2087.  
  2088.         $get = $this->model_goexpress->get_user($data);
  2089.  
  2090.         if ( isset($json->id) AND isset($get->hp) AND isset($json->isclt) AND $json->isclt == $IS_DRIVER )
  2091.         {
  2092.             $json->hp = $get->hp;
  2093.         }
  2094.  
  2095.         if( !empty($get) and $get->n_retries >= $LOGIN_THRESHOLD)
  2096.         {
  2097.             $values= array();
  2098.             $values['idelete']    = 1;
  2099.             list($flag, $user_id, $msg ) = $this->model_goexpress->add_user($values, $get->id);
  2100.             $respon = array('error' => 4, 'error_message' => 'Account is Blocked');
  2101.             die(json_encode($respon));
  2102.         }
  2103.  
  2104.         if( count($get) > 0 )
  2105.         {
  2106.             if ( $get->password == $json->password AND strlen($get->password) == strlen($json->password) ) {
  2107.                 $newdata = array(
  2108.                                   'password' => $json->repass
  2109.                                 );
  2110.  
  2111.                 $this->model_goexpress->add_user($newdata, $get->id);
  2112.                 $respon = array('error' => 0, 'error_message' => 'Password was changed!');
  2113.                 die(json_encode($respon));
  2114.             }
  2115.             else {
  2116.                 $respon = array('error' => 1, 'error_message' => 'Wrong Password! Please Try again.');
  2117.                 die(json_encode($respon));
  2118.             }
  2119.         }
  2120.     }
  2121.  
  2122.  
  2123.     function json_verify_user()
  2124.     {
  2125.         $IS_VERIFIED = 1;
  2126.         $ACTIVE = 0;
  2127.         $LOGIN_THRESHOLD = 3;
  2128.         $respon = array();
  2129.         $payload = file_get_contents('php://input');
  2130.  
  2131.         $this->load->model('model_goexpress');
  2132.         if( !$payload )
  2133.         {
  2134.             $respon = array('error' => 1, 'message' => 'Empty payload');
  2135.             die(json_encode($respon));
  2136.         }
  2137.  
  2138.         $json = json_decode( $payload );
  2139.         if ( !isset($json->fcm) )
  2140.             $json->fcm = null;
  2141.         $json->hp = str_replace(' ','',$json->hp);
  2142.         if( !isset( $json->hp ) )
  2143.         {
  2144.             $respon = array('error' => 1, 'error_message' => 'Invalid PhoneNo');
  2145.             die(json_encode($respon));
  2146.         }
  2147.  
  2148.         if( !isset( $json->user_verification ) or $json->user_verification == '' )
  2149.         {
  2150.             $respon = array('error' => 1, 'error_message' => 'Invalid Verification');
  2151.             die(json_encode($respon));
  2152.         }
  2153.  
  2154.         $data = array(
  2155.             'hp'        => $json->hp,
  2156.         );
  2157.  
  2158.         $get = $this->model_goexpress->get_user($data);
  2159.  
  2160.         if( $get->bverified == $IS_VERIFIED )
  2161.         {
  2162.             if( $get->idelete != $ACTIVE )
  2163.             {
  2164.                 $respon = array('error' => 4, 'error_message' => 'Account is Blocked');
  2165.                 die(json_encode($respon));
  2166.             }
  2167.  
  2168.             $respon = array(
  2169.                         'error' => 0,
  2170.                         'error_message' => 'Success',
  2171.                         'userid' => $get->username,
  2172.                         'email' => $get->email,
  2173.                         'name' => $get->name,
  2174.                         'promo_code' => $get->promo_code,
  2175.                         'ewallet' => $get->wallet,
  2176.                         'vehicle_name' => $get->vehicle_name,
  2177.                         'vehicle_plate_no' => $get->vehicle_plate_no,
  2178.                         'hp' => $get->hp,
  2179.                         'api_token' => $this->security_model->generate_apikey($get->username,$json->fcm)
  2180.                     );
  2181.             die(json_encode($respon));
  2182.  
  2183.         }
  2184.         else
  2185.         {
  2186.             if( !empty($get) and $get->n_retries >= $LOGIN_THRESHOLD)
  2187.             {
  2188.                 $values= array();
  2189.                 $values['idelete']    = 1;
  2190.                 list($flag, $user_id, $msg ) = $this->model_goexpress->add_user($values, $get->id);
  2191.                 $respon = array('error' => 1, 'error_message' => 'Account is Blocked');
  2192.                 die(json_encode($respon));
  2193.             }
  2194.         }
  2195.  
  2196.         if( count($get) > 0 )
  2197.         {
  2198.             if( $get->verification_code != $json->user_verification ){
  2199.                 //update retries
  2200.                 $this->model_goexpress->reset_retries_user($json->hp, 1);
  2201.                 $respon = array('error' => 1, 'error_message' => 'Invalid verification code');
  2202.                 die(json_encode($respon));
  2203.             }else if($get->idelete != 0) {
  2204.                 $respon = array('error' => 4, 'error_message' => 'Account is Blocked');
  2205.                 die(json_encode($respon));
  2206.             }else{
  2207.                 //Reset 0
  2208.                 $this->model_goexpress->reset_retries_user($json->hp, 0);
  2209.  
  2210.                 $data = array(
  2211.                     'hp'        => $json->hp,
  2212.                     'verification_code'  => $json->user_verification,
  2213.                     'bverified' => 1,
  2214.                 );
  2215.                 $this->model_goexpress->add_user($data,$get->id);
  2216.  
  2217.                 $respon = array(
  2218.                             'error' => 0,
  2219.                             'error_message' => 'Success',
  2220.                             'userid' => $get->username,
  2221.                             'email' => $get->email,
  2222.                             'name' => $get->name,
  2223.                             'promo_code' => $get->promo_code,
  2224.                             'ewallet' => $get->wallet,
  2225.                             'vehicle_name' => $get->vehicle_name,
  2226.                             'vehicle_plate_no' => $get->vehicle_plate_no,
  2227.                             'hp' => $get->hp,
  2228.                             'api_token' => $this->security_model->generate_apikey($get->username,$json->fcm)
  2229.                         );
  2230.  
  2231.                 //Comment to disable promotion
  2232.                 $promo_start = strtotime("2017-4-7 14:00:00");
  2233.                 $promo_end = strtotime("2018-4-7 23:59:59");
  2234.                 $now = time();
  2235.                 if ( $now >= $promo_start && $now <= $promo_end ) {
  2236.                     $this->model_goexpress->tmp_register_promotion($get);
  2237.                 }
  2238.  
  2239.                 die(json_encode($respon));
  2240.             }
  2241.         }else{
  2242.             //update retries
  2243.             $this->model_goexpress->reset_retries_user($json->hp, 1);
  2244.             $respon = array('error' => 1, 'error_message' => 'Invalid verification code');
  2245.             die(json_encode($respon));
  2246.         }
  2247.     }
  2248.  
  2249.     function register_fcm()
  2250.     {
  2251.         $IS_VERIFIED = 1;
  2252.         $ACTIVE = 0;
  2253.         $LOGIN_THRESHOLD = 3;
  2254.         $respon = array();
  2255.         $payload = file_get_contents('php://input');
  2256.         $this->load->model('model_goexpress');
  2257.  
  2258.         if( !$payload )
  2259.         {
  2260.             $respon = array('error' => 1, 'message' => 'Empty payload');
  2261.             die(json_encode($respon));
  2262.         }
  2263.  
  2264.         $json = json_decode( $payload );
  2265.  
  2266.         if( !isset( $json->userid ) )
  2267.         {
  2268.             $respon = array('error' => 1, 'error_message' => 'Invalid UserID');
  2269.             die(json_encode($respon));
  2270.         }
  2271.  
  2272.  
  2273.         list($flag, $user_id, $msg ) = $this->model_goexpress->register_fcm($json->userid);
  2274.  
  2275.         if(! $user_id )
  2276.         {
  2277.             $respon = array('error' => 1, 'error_message' => 'Failed Register FCM');
  2278.             die(json_encode($respon));
  2279.         }else{
  2280.             $respon = array('error' => 0, 'error_message' => 'Register FCM Success');
  2281.             die(json_encode($respon));
  2282.         }
  2283.     }
  2284.  
  2285.     function json_forgot()
  2286.     {
  2287.         $ACTIVE = 0;
  2288.         $respon = array();
  2289.         $payload = file_get_contents('php://input');
  2290.  
  2291.         $this->load->model('model_goexpress');
  2292.  
  2293.         if( !$payload )
  2294.         {
  2295.             $respon = array('error' => 1, 'message' => 'Empty payload');
  2296.             die(json_encode($respon));
  2297.         }
  2298.  
  2299.         $json = json_decode( $payload );
  2300.  
  2301.         $json->user_phoneno_partial = str_replace(' ','',$json->user_phoneno_partial);
  2302.  
  2303.         if( !isset( $json->user_phoneno_partial ) or $json->user_phoneno_partial == "")
  2304.         {
  2305.             $respon = array('error' => 1, 'error_message' => 'Error: PhoneNo is BLANK');
  2306.             die(json_encode($respon));
  2307.         }
  2308.  
  2309.         $json->hp = $json->user_phoneno_partial;
  2310.  
  2311.         if( !isset( $json->user_email_partial ) or $json->user_email_partial == "")
  2312.         {
  2313.             $respon = array('error' => 1, 'error_message' => 'Error: Email is BLANK');
  2314.             die(json_encode($respon));
  2315.         }
  2316.  
  2317.         $json->email = $json->user_email_partial;
  2318.  
  2319.         if( !isset( $json->user_password_partial ) or $json->user_password_partial == "")
  2320.         {
  2321.             $respon = array('error' => 1, 'error_message' => 'Error: New Password is BLANK');
  2322.             die(json_encode($respon));
  2323.         }
  2324.  
  2325.         $json->tmp_password = $json->user_password_partial;
  2326.  
  2327.         $data = array(
  2328.             'hp'        => $json->hp,
  2329.             'email'     => $json->email,
  2330.             'tmp_password' => $json->tmp_password,
  2331.             'idelete'   => $ACTIVE,
  2332.         );
  2333.  
  2334.         $get = $this->model_goexpress->get_user($data);
  2335.  
  2336.         if( count($get) > 0 )
  2337.         {
  2338.             $values['tmp_password'] = $json->tmp_password;
  2339.             $values['bverified']    = 1; // verified must stay 1
  2340.             // since to prevent any monkeying the legit number by someone else
  2341.  
  2342.             $YmdHis = date('YmdHis');
  2343.             srand( $YmdHis );
  2344.             $values['verification_code'] = rand(1000, 9999);
  2345.             $values['is_forgotpwd']      = 1;
  2346.  
  2347.             list($flag, $user_id, $msg ) = $this->model_goexpress->add_user($values, $get->id);
  2348.  
  2349.             if(! $user_id )
  2350.             {
  2351.                 $respon = array('error' => 1, 'error_message' => 'Failed Reset Password', 'verification_code' => null);
  2352.                 die(json_encode($respon));
  2353.             }else{
  2354.  
  2355.                 $filtered_hp = $get->hp;
  2356.                 if( $get->filtered_hp != '' )
  2357.                     $filtered_hp = $get->filtered_hp;
  2358.  
  2359.                 $smsparams = array(
  2360.                     'create_date' => date('Y-m-d H:i:s'),
  2361.                     'hp' => $get->hp,
  2362.                     'user_id' => $get->id,
  2363.                 );
  2364.                 $this->user_model->add_sendsms($smsparams);
  2365.  
  2366.                 if( preg_match("/^\+855/", $json->hp) )
  2367.                 {
  2368.                     $this->iSendSmsViaGateway($filtered_hp, $values['verification_code']);
  2369.                 }
  2370.                 else
  2371.                 {
  2372.                     $datasms = array(
  2373.                         'user_id' => $get->id,
  2374.                         'sms_no' => $filtered_hp,
  2375.                         'server_info' => 'send_sms',
  2376.                         'message' => "GO-X.\nYour verification number is ".$values['verification_code']."\n លេខកូដរបស់អ្នកគឺ ". $values['verification_code'],
  2377.  
  2378.                     );
  2379.                     $this->sendGoogleCloudMessage(  $datasms, $ids = array() , array('separate' => True ), array('ret_int'=>1));
  2380.                 }
  2381.                 $respon = array('error' => 0, 'error_message' => 'Reset Password Successfully', 'verification_code' => '');
  2382.                 die(json_encode($respon));
  2383.             }
  2384.         }else{
  2385.             $respon = array('error' => 1, 'error_message' => 'Invalid PhoneNo/Email');
  2386.             die(json_encode($respon));
  2387.         }
  2388.     }
  2389.  
  2390.     function txn()
  2391.     {
  2392.         $ACTIVE = 0;
  2393.         $respon = array();
  2394.         $payload = file_get_contents('php://input');
  2395.  
  2396.         $json = json_decode($payload);
  2397.  
  2398.         if( !isset($json->user_id) or $json->user_id == '')
  2399.         {
  2400.             $respon = array(
  2401.                         'error' => 1,
  2402.                         'error_message' => 'Error: Bad or Invalid Param ',
  2403.                         'error_code' => 1,
  2404.                     );
  2405.             die(json_encode($respon));
  2406.         }
  2407.  
  2408.         if( !isset($json->req) or $json->req == '')
  2409.         {
  2410.             $respon = array(
  2411.                         'error' => 1,
  2412.                         'error_message' => 'Error: Bad or Invalid Param ',
  2413.                         'error_code' => 2,
  2414.                     );
  2415.             die(json_encode($respon));
  2416.         }
  2417.  
  2418.         if( $json->req != 'TXN')
  2419.         {
  2420.             $respon = array(
  2421.                         'error' => 1,
  2422.                         'error_message' => 'Error: Bad or Invalid Param ',
  2423.                         'error_code' => 3,
  2424.                     );
  2425.             die(json_encode($respon));
  2426.         }
  2427.  
  2428.         $demos = array();
  2429.         //$demos = array(
  2430.         //            array(
  2431.         //                'tr_date' => "06-06-2016",
  2432.         //                'description' => 'Referral Bonus',
  2433.         //                'amount' => 'KHR 2000',
  2434.         //            ),
  2435.         //            array(
  2436.         //                'tr_date' => "05-06-2016",
  2437.         //                'description' => 'Referral Bonus',
  2438.         //                'amount' => 'KHR 2000',
  2439.         //            ),
  2440.         //            array(
  2441.         //                'tr_date' => "04-06-2016",
  2442.         //                'description' => 'Referral Bonus',
  2443.         //                'amount' => 'KHR 2000',
  2444.         //            ),
  2445.         //        );
  2446.  
  2447.         $respon = array(
  2448.                     'error' => 0,
  2449.                     'error_message' => 'Error: Bad or Invalid Param ',
  2450.                     'error_code' => 0,
  2451.                     'bal' => 'KHR 0',
  2452.                     'TXN' => $demos,
  2453.                 );
  2454.         die(json_encode($respon));
  2455.     }
  2456.  
  2457.     function sendGoogleCloudMessage( $data, $ids, $params = array(), $options = array() )
  2458.     {
  2459.         //------------------------------
  2460.         // Replace with real GCM API
  2461.         // key from Google APIs Console
  2462.         //
  2463.         // https://code.google.com/apis/console/
  2464.         //------------------------------
  2465.  
  2466.         $apiKey = 'AIzaSyAMC9GU3zMDCOf8r2kZvVfJMhbGrh2iJTM';
  2467.  
  2468.         //------------------------------
  2469.         // Define URL to GCM endpoint
  2470.         //------------------------------
  2471.  
  2472.         $url = 'https://android.googleapis.com/gcm/send';
  2473.  
  2474.         //------------------------------
  2475.         // Set GCM post variables
  2476.         // (Device IDs and push payload)
  2477.         //------------------------------
  2478.  
  2479.         $post = array(
  2480.                     'data' => $data,
  2481.                 );
  2482.  
  2483.         if( isset( $params['registration_ids'] ) )
  2484.         {
  2485.             $post['registration_ids'] = $options['registration_ids'];
  2486.         }
  2487.         else if( isset( $options['stoken'] ) )
  2488.         {
  2489.             $post['to'] = $options["stoken"];
  2490.         }
  2491.         else
  2492.         {
  2493.             if( isset($data['Vehicle'] ))
  2494.                 $post['to'] = "/topics/" . $data['Vehicle'];
  2495.             else
  2496.                 $post['to'] = "/topics/goxsmsx";
  2497.         }
  2498.  
  2499.         //------------------------------
  2500.         // Set CURL request headers
  2501.         // (Authentication and type)
  2502.         //------------------------------
  2503.  
  2504.         $headers = array(
  2505.             'Authorization: key=' . $apiKey,
  2506.             'Content-Type: application/json'
  2507.         );
  2508.  
  2509.         //------------------------------
  2510.         // Initialize curl handle
  2511.         //------------------------------
  2512.  
  2513.         $ch = curl_init();
  2514.  
  2515.         //------------------------------
  2516.         // Set URL to GCM endpoint
  2517.         //------------------------------
  2518.  
  2519.         curl_setopt( $ch, CURLOPT_URL, $url );
  2520.  
  2521.         //------------------------------
  2522.         // Set request method to POST
  2523.         //------------------------------
  2524.  
  2525.         curl_setopt( $ch, CURLOPT_POST, true );
  2526.  
  2527.         //------------------------------
  2528.         // Set our custom headers
  2529.         //------------------------------
  2530.  
  2531.         curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
  2532.  
  2533.         //------------------------------
  2534.         // Get the response back as
  2535.         // string instead of printing it
  2536.         //------------------------------
  2537.  
  2538.         curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  2539.  
  2540.         //------------------------------
  2541.         // Set post data as JSON
  2542.         //------------------------------
  2543.  
  2544.         curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );
  2545.         curl_setopt($ch, CURLOPT_TIMEOUT ,10);
  2546.  
  2547.         //------------------------------
  2548.         // Actually send the push!
  2549.         //------------------------------
  2550.  
  2551.         $result = curl_exec( $ch );
  2552.  
  2553.         //------------------------------
  2554.         // Error? Display it!
  2555.         //------------------------------
  2556.  
  2557.         if ( curl_errno( $ch ) )
  2558.         {
  2559.             echo '1@@GCM error: ' . curl_error( $ch );
  2560.         }
  2561.  
  2562.         //------------------------------
  2563.         // Close curl handle
  2564.         //------------------------------
  2565.  
  2566.         curl_close( $ch );
  2567.  
  2568.         //------------------------------
  2569.         // Debug GCM response
  2570.         //------------------------------
  2571.  
  2572.         if( isset($options['ret_int']) )
  2573.             return $result;
  2574.  
  2575.         if( isset($params['separate']))
  2576.             echo '@@0@@'.@$data['TxNo'].'@@'.$result;
  2577.         else
  2578.             echo '0@@'.@$data['TxNo'].'@@'.$result;
  2579.     }
  2580.  
  2581.     function _filter_number($hp)
  2582.     {
  2583.         $THRESHOLD = 8;
  2584.  
  2585.         // Open for all numbers
  2586.         //if( !preg_match("/^\+855/", $hp) )
  2587.         //    return false;
  2588.  
  2589.         $result = preg_replace("/[ -]/","",preg_replace("/^\+855/","",$hp));
  2590.  
  2591.         if( strlen($result) >= $THRESHOLD )
  2592.             return true;
  2593.         else
  2594.             return false;
  2595.     }
  2596.  
  2597.     function _filter_number2($hptmp)
  2598.     {
  2599.         $MIN_SEPARATED = 2;
  2600.  
  2601.         if( preg_match("/^\+855.*/", $hptmp) ) // Cambodia
  2602.         {
  2603.             $arrtmp = explode(" ", $hptmp);
  2604.             if( count($arrtmp) >= $MIN_SEPARATED )
  2605.             {
  2606.                 if( isset($arrtmp[1]) and $arrtmp[1] != "")
  2607.                 {
  2608.                     $arrtmp[1] = preg_replace("/^(0)([0-9]*)/", "$2", $arrtmp[1]);
  2609.                     $hptmp = implode("",$arrtmp); // already filter out whitespace
  2610.                 }
  2611.                 else
  2612.                 {
  2613.                     $hptmp = str_replace(' ','',$hptmp);
  2614.                 }
  2615.             }
  2616.             else
  2617.             {
  2618.                 if( preg_match("/^\+8550.*/", $hptmp) )
  2619.                 {
  2620.                     $hptmp = preg_replace("/^(\+855)(0)([0-9]*)/", "$1$3", $hptmp);
  2621.                 }
  2622.                 else
  2623.                 {
  2624.                     $hptmp = str_replace(' ','',$hptmp);
  2625.                 }
  2626.             }
  2627.         }
  2628.         else
  2629.         {
  2630.             $hptmp = str_replace(' ','',$hptmp);
  2631.         }
  2632.  
  2633.         return $hptmp;
  2634.     }
  2635.  
  2636.     function iSendSmsViaGateway( $filtered_hp, $vercode )
  2637.     {
  2638.         $ERROR = 0;
  2639.         $CODE_SUCCESS = 0;
  2640.         $SUCCESS = 1;
  2641.  
  2642.         $apiKey = 'username=goexpress_sms@mekongnet&pass=dd5ad747f4c1659a1236bd37169e2fd0&sender=GO-X&smstext=';
  2643.  
  2644.         $sms = "GO-X.\nYour verification number is ".$vercode."\n លេខកូដរបស់អ្នកគឺ ".$vercode;
  2645.         $sms = str_replace(" ","+",$sms);
  2646.         $sms = str_replace("\n","%0A",$sms);
  2647.  
  2648.         $filtered_hp = str_replace("+","", $filtered_hp);
  2649.         $apiEnder = "&isflash=0&gsm=".$filtered_hp;
  2650.  
  2651.         $url = 'http://client.mekongsms.com/api/sendsms.aspx?' .
  2652.                 $apiKey.
  2653.                 $sms.
  2654.                 $apiEnder;
  2655.  
  2656.         $headers = array(
  2657.             "Content-Type: application/x-www-form-urlencoded; charset=utf-8",
  2658.         );
  2659.  
  2660.         $ch = curl_init();
  2661.  
  2662.         curl_setopt( $ch, CURLOPT_URL, $url );
  2663.         curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
  2664.         curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  2665.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  2666.  
  2667.         $result = curl_exec( $ch );
  2668.  
  2669.         $bCurl = true;
  2670.  
  2671.         if ( curl_errno( $ch ) )
  2672.         {
  2673.             error_log('iSendSMS Curl Error '. curl_error($ch));
  2674.             $bCurl = false;
  2675.         }
  2676.  
  2677.         curl_close( $ch );
  2678.  
  2679.         if( $bCurl === false )
  2680.             return $ERROR;
  2681.  
  2682.         //------------------------------
  2683.         // Debug GCM response
  2684.         //------------------------------
  2685.         //if( @$result->results[0]->error == "MissmatchSenderId")
  2686.         //    return $this->sendFirebaseCloudMessageIOS($data, $ids, $params, $options);
  2687.         $response = explode("\n", $result);
  2688.  
  2689.         if(count($response) > 0)
  2690.         {
  2691.             $arrResponse = explode(" ", $response[0]);
  2692.  
  2693.             if( $arrResponse[0] == $CODE_SUCCESS)
  2694.                 return $SUCCESS;
  2695.             else
  2696.                 return $ERROR;
  2697.         }
  2698.         else
  2699.         {
  2700.             return $ERROR;
  2701.         }
  2702.     }
  2703.  
  2704.     function repostBoost()
  2705.     {
  2706.         set_time_limit(0);
  2707.         $records = $this->user_model->getFailedBoost();
  2708.         die(var_dump($records));
  2709.         foreach($records as $rec )
  2710.         {
  2711.             $this->iSendSmsViaGateway($rec->filtered_hp, $rec->verification_code);
  2712.         }
  2713.     }
  2714.  
  2715.     function json_login_fb()
  2716.     {
  2717.         $this->load->model('model_goexpress');
  2718.         $payload = file_get_contents('php://input');
  2719.         $json = json_decode( $payload );
  2720.  
  2721.         $params['fb_userid']  = $json->UserID;
  2722.         $params['username']   = $json->UserName;
  2723.  
  2724.  
  2725.         if ($params['fb_userid']!=="" && $params['username']!==""  ) {
  2726.  
  2727.             if ( !$this->user_model->validate_login_fb($params) ) {
  2728.                 $newID = md5($json->UserID. microtime());
  2729.                 $api_token = $this->security_model->generate_apikey($newID, "");
  2730.                 $data = array(
  2731.                     'username'  => $newID,
  2732.                     'email'     => ($json->UserEmail !="") ? $json->UserEmail : $newID."@goexpress-kh.com",
  2733.                     'name'      => $json->UserName,
  2734.                     'hp'        => "",
  2735.                     'filtered_hp' => "",
  2736.                     'password'  => "",
  2737.                     'bverified' => 0,
  2738.                     // 'bverified' => 1,
  2739.                     'verification_code' => rand(1000, 9999),
  2740.                     'registration_date' => date('Y-m-d H:i:s'),
  2741.                     'api_token' => $api_token,
  2742.                     'user_type_id' => "90",
  2743.                     'fb_userid' => $json->UserID
  2744.                 );
  2745.                 list($flag, $user_id, $msg ) = $this->model_goexpress->add_user($data);
  2746.             }
  2747.  
  2748.  
  2749.             $data = array(
  2750.                 'fb_userid'       => $json->UserID
  2751.             );
  2752.  
  2753.             $get = $this->model_goexpress->get_user($data);
  2754.             $api_token = $this->security_model->generate_apikey($get->username, "");
  2755.             $respon = array(
  2756.                 'error' => 2,
  2757.                 'error_message' => 'Success',
  2758.                 'userid' => $get->username,
  2759.                 'email' => $get->email,
  2760.                 'name' => $get->name,
  2761.                 'promo_code' => $get->promo_code,
  2762.                 'ewallet' => $get->wallet,
  2763.                 'vehicle_name' => $get->vehicle_name,
  2764.                 'vehicle_plate_no' => $get->vehicle_plate_no,
  2765.                 'hp' => $get->hp,
  2766.                 'api_token' => $api_token,
  2767.                 'status' => true
  2768.             );
  2769.         }
  2770.         else {
  2771.             $respon = array(
  2772.                 'error' => 2,
  2773.                 'error_message' =>'Error: Invalid Login Data',
  2774.                 'status' => false
  2775.             );
  2776.         }
  2777.  
  2778.         die(json_encode($respon));
  2779.         // ERROR
  2780.     }
  2781.  
  2782.     function update_hp_fb()
  2783.     {
  2784.         // $this->load->model('model_goexpress');
  2785.         $payload = file_get_contents('php://input');
  2786.         $json = json_decode( $payload );
  2787.  
  2788.         $fb_userid   = $json->UserID;
  2789.         $params['hp']= $json->hp;
  2790.  
  2791.         $user_id = $this->user_model->validate_login_fb($params,$fb_userid);
  2792.         if( isset($user_id) && $user_id !="" )
  2793.         {
  2794.             $api_token = $this->security_model->generate_apikey($json->username, "");
  2795.             $respon = array(
  2796.                 'error' => 2,
  2797.                 'error_message' => 'Success',
  2798.                 'api_token' => $api_token,
  2799.                 'status' => true
  2800.                 // 'userid' => $json->username,
  2801.                 // 'email' => $json->email,
  2802.                 // 'name' => $json->name,
  2803.                 // 'promo_code' => $json->promo_code,
  2804.                 // 'ewallet' => $json->wallet,
  2805.                 // 'vehicle_name' => $json->vehicle_name,
  2806.                 // 'vehicle_plate_no' => $json->vehicle_plate_no,
  2807.                 // 'hp' => $json->hp,
  2808.                
  2809.             );
  2810.  
  2811.             // KIRIM CODE SMS
  2812.             $YmdHis = date('YmdHis');
  2813.             srand( $YmdHis );
  2814.             $values['verification_code'] = rand(1000, 9999);
  2815.             $values['is_forgotpwd']      = 0;
  2816.             $smsparams = array(
  2817.                 'create_date' => date('Y-m-d H:i:s'),
  2818.                 'hp' => $json->hp,
  2819.                 'user_id' => $user_id,
  2820.             );
  2821.             $this->user_model->add_sendsms($smsparams);
  2822.  
  2823.             if( $this->_filter_number($json->hp) )
  2824.             {
  2825.                 if( preg_match("/^\+855/", $json->hp) )
  2826.                 {
  2827.                     $this->iSendSmsViaGateway($json->hp, $data['verification_code']);
  2828.                 }
  2829.                 else
  2830.                 {
  2831.                     $datasms = array(
  2832.                         'user_id' => $user_id,
  2833.                         'sms_no' => $json->hp,
  2834.                         'server_info' => 'send_sms',
  2835.                         'message' => "GO-X.\nYour verification number is ".$data['verification_code']."\n លេខកូដរបស់អ្នកគឺ ". $data['verification_code'],
  2836.  
  2837.                     );
  2838.                     $this->sendGoogleCloudMessage(  $datasms, $ids = array() , array('separate' => True ), array('ret_int'=>1));
  2839.                 }
  2840.             }
  2841.  
  2842.             // $respon = array('error' => 2, 'error_message' => 'Successfully saved, please verify your phone number', 'userid' => $newID);
  2843.             // die(json_encode($respon));
  2844.         }else{
  2845.             $respon = array(
  2846.                 'error' => 2,
  2847.                 'error_message' => 'Error: Invalid Update Data',
  2848.                 'status' => false
  2849.             );
  2850.         }
  2851.  
  2852.         die(json_encode($respon));
  2853.     }
  2854. }
  2855.  
  2856. /* End of file welcome.php */
  2857. /* Location: ./application/controllers/welcome.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement