Advertisement
tpires

Flip.php

Mar 31st, 2017
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.66 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class Flip extends CI_Controller {
  5.   private $header_data = array();
  6.   private $data = array();
  7.   private $footer_data = array();
  8.   private $end_user = null; // Used by check_login_validation() to send user's name and email to login()
  9.  
  10.   /*
  11.    * Load helpers, models, and eveything that should be used by ALL methods
  12.    */
  13.   public function __construct($slug = null) {
  14.     parent::__construct();
  15.  
  16.     $this->lang->load('flp_flip', 'pt-BR');
  17.     $this->lang->load('form_validation', 'pt-BR');
  18.  
  19.     $this->load->helper( array('form', 'html', 'url') );
  20.    
  21.     $this->load->model('company_model', 'company');
  22.     $this->load->model('publication_model', 'publication');
  23.     $this->load->model('edition_model', 'edition');
  24.     $this->load->model('flip_model', 'flip');
  25.   }
  26.  
  27.   /*
  28.    * Index page
  29.    * It will basically check if the given slug exists (is it a publication or a company?)
  30.    */
  31.   public function index($slug = null) {
  32.     // If slug not found: display about page
  33.     if( $this->display_about_page($slug) ) {
  34.       redirect( 'http://aspintecnologia.com.br/aspin-news' );
  35.     }
  36.     // else slug found, but user not logged in
  37.     elseif( is_null($this->session->userdata('user_email')) ) {
  38.       redirect( base_url('flip/'.$slug.'/login') );
  39.     }
  40.     // else user is logged in
  41.     else {
  42.       redirect( base_url('flip/'.$slug.'/list') );
  43.     }
  44.   } // index()
  45.  
  46.   /*
  47.    * About ONLY redirects to an URL
  48.    * The last "catch everything" route uses this method
  49.    */
  50.   public function about() {
  51.     redirect( 'http://aspintecnologia.com.br/aspin-news' );
  52.   }
  53.  
  54.   /*
  55.    * Login page
  56.    * It asks Check Login (web service) if the user (email and password) is an active subscriber
  57.    */
  58.   public function login($slug = null) {
  59.     // Slug not found: display about page
  60.     if( $this->display_about_page($slug) ) redirect( base_url('/') );
  61.  
  62.     $this->load->library('form_validation');
  63.  
  64.     /*
  65.     $slug_type = $this->set_slug_type($slug); // 'company' or 'publication'
  66.     */
  67.  
  68.     $publication = $this->publication->get_publication_by_slug($slug);
  69.     $company_logo = $this->company->get_company_logo($publication->company_cnpj);
  70.    
  71.     $this->header_data['index_page'] = base_url('flip/'.$slug);
  72.     $this->header_data['title'] = $publication->publication_name;
  73.  
  74.     $this->header_data['body_classes'] = 'flip flip-login';
  75.     $this->data['slug'] = $slug;
  76.     $this->data['logo'] = $company_logo;
  77.     $this->data['publication'] = $publication;
  78.  
  79.     if ($this->form_validation->run('flip/login') == false) {
  80.       $this->load->view('shared/header_flip', $this->header_data);
  81.       $this->load->view('flip_login', $this->data);
  82.       $this->load->view('shared/footer_flip');
  83.     }
  84.     else {
  85.       // Set user session
  86.       $flip_user = array(
  87.         'flip_user_email' => $this->end_user['email'],
  88.         'flip_user_name' => $this->end_user['name'],
  89.         'company_cnpj' => $publication->company_cnpj,
  90.       );
  91.       $this->session->set_userdata($flip_user);
  92.       $this->end_user = null;
  93.       // And redirect to flip/$slug/list
  94.       redirect( base_url('flip/'.$slug.'/list') );
  95.     }
  96.   } // login()
  97.  
  98.   public function logout($slug = null) {
  99.     if( $this->display_about_page($slug) ) redirect( base_url('/') );
  100.     elseif( is_null($this->session->userdata('flip_user_email')) ) { redirect( base_url('flip/'.$slug.'/login') ); }
  101.     else {
  102.       $this->session->sess_destroy();
  103.       redirect( base_url('flip/'.$slug.'/login') );
  104.     }
  105.   } // logout()
  106.  
  107.   public function list($slug = null) {
  108.     // Slug not found: display about page
  109.     if( $this->display_about_page($slug) ) { redirect( base_url('/') ); }
  110.     elseif( is_null($this->session->userdata('flip_user_email')) ) { redirect( base_url('flip/'.$slug.'/login') ); }
  111.     else {
  112.       /*
  113.       $slug_type = $this->set_slug_type($slug); // 'company' or 'publication'
  114.       */
  115.      
  116.       $publication = $this->publication->get_publication_by_slug($slug);
  117.  
  118.       $this->header_data['index_page'] = base_url('flip/'.$slug.'/list');
  119.       $this->header_data['body_classes'] = 'flip flip-list';
  120.       $this->header_data['title'] = $publication->publication_name;
  121.       $this->header_data['header_color'] = $publication->publication_header_color;
  122.       $this->header_data['header_style'] = $publication->publication_color_lightness;
  123.       $this->header_data['logo'] = $publication->publication_logo;
  124.       $this->header_data['slug'] = $slug;
  125.  
  126.       //$limit = $this->month_number_of_days();
  127.  
  128.       $this->data['editions'] = $this->flip->get_editions($publication->publication_id, 30);
  129.  
  130.       $this->load->view('shared/header_flip', $this->header_data);
  131.       $this->load->view('flip_list', $this->data);
  132.       $this->load->view('shared/footer_flip');
  133.     }
  134.   } // list()
  135.  
  136.   public function view($slug = null, $info = null) {
  137.     $edition_id = $this->flip->encrypt_decrypt_edition_id($info, 'decrypt');
  138.  
  139.     // Slug not found: display about page
  140.     if( $this->display_about_page($slug) ) { redirect( base_url('/') ); }
  141.     // User no logged in
  142.     elseif( is_null($this->session->userdata('flip_user_email')) ) { redirect( base_url('flip/'.$slug.'/login') ); }
  143.     // Hash not set or Edition ID not found from hash
  144.     elseif( is_null($info) || $edition_id == false ) { redirect( base_url('flip/'.$slug.'/list') ); }
  145.     // Display edition
  146.     else {
  147.       /*
  148.       $slug_type = $this->set_slug_type($slug); // 'company' or 'publication'
  149.       */
  150.  
  151.       $edition = $this->edition->get_edition_by_id($edition_id);
  152.       $publication = $this->publication->get_publication_by_id($edition->publication_id);
  153.  
  154.       $date = date_create($edition->edition_date);
  155.       $date = date_format($date, 'd\/m\/Y');
  156.  
  157.       $title = $publication->publication_name . ' Edição: ' . $edition->edition_number . ' Data: ' . $date;
  158.  
  159.       $this->header_data['index_page'] = base_url('flip/'.$slug.'/list');
  160.       $this->header_data['title'] = $title;
  161.       $this->header_data['body_classes'] = 'flip flip-view';
  162.       $this->header_data['publication'] = $publication;
  163.       $this->header_data['slug'] = $slug;
  164.       $this->header_data['display_edition'] = true;
  165.       $this->header_data['back_btn'] = array('location' => 'view', 'slug' => $slug, 'name' => $publication->publication_name);
  166.  
  167.       $this->data['edition'] = $edition;
  168.  
  169.       $this->footer_data['display_edition'] = true;
  170.       $this->footer_data['edition'] = $edition;
  171.       $this->footer_data['publication'] = $publication;
  172.  
  173.       $this->load->view('shared/header_flip', $this->header_data);
  174.       $this->load->view('flip_view', $this->data);
  175.       $this->load->view('shared/footer_flip');
  176.     }
  177.   } // view()
  178.  
  179.   /*
  180.    * Helper functions
  181.    */
  182.  
  183.   public function display_about_page( $slug ) {
  184.     $about_page = false;
  185.  
  186.     // No slug given in the URL
  187.     if( $slug == null ) $about_page = true;
  188.  
  189.     // Slug doesn't exist in BOTH flp_company and flp_publication
  190.     if( $this->publication->publication_slug_exists($slug) == false &&
  191.         $this->company->company_slug_exists($slug) == false
  192.       ) {
  193.       $about_page = true;
  194.     }
  195.  
  196.     return $about_page;
  197.   } // display_about_page()
  198.  
  199.   public function set_slug_type( $slug ) {
  200.     if( $this->publication->publication_slug_exists($slug) ) return 'publication';
  201.     else return 'company';
  202.     // at this point display_about_page() already checked that
  203.     // either a publication or a company exists for a given slug
  204.   } // set_slug_type()
  205.  
  206.   // https://davidwalsh.name/checking-for-leap-year-using-php
  207.   function is_leap_year($year) {
  208.     return ((($year % 4) == 0) && ((($year % 100) != 0) || (($year % 400) == 0)));
  209.   }
  210.  
  211.   public function month_number_of_days() {
  212.     date_default_timezone_set('America/Sao_Paulo');
  213.     $current_month = date('n');
  214.     $current_year = date('Y');
  215.  
  216.     // Decide how many days for query $limit
  217.     switch ($current_month) {
  218.       case 1:
  219.       case 3:
  220.       case 5:
  221.       case 7:
  222.       case 8:
  223.       case 10:
  224.       case 12:
  225.         return 31;
  226.         break;
  227.      
  228.       case 2:
  229.         if( $this->is_leap_year($current_year) ) $limit = 29;
  230.         return 28;
  231.         break;
  232.  
  233.       case 4:
  234.       case 6:
  235.       case 9:
  236.       case 11:
  237.         return 30;
  238.         break;
  239.     }
  240.   }
  241.  
  242.   /*
  243.    * Form validation functions
  244.    */
  245.  
  246.   public function check_login_validation() {
  247.  
  248.     /*
  249.      * DO NOT ever tell the user what is wrong (email or password)
  250.      */
  251.  
  252.     if( is_null($this->input->post('flip-login-user')) ||
  253.         is_null($this->input->post('flip-login-pass')) ||
  254.         is_null($this->input->post('slug')) ||
  255.         strlen($this->input->post('flip-login-user')) == 0 ||
  256.         strlen($this->input->post('flip-login-pass')) == 0 ||
  257.         $this->publication->publication_slug_exists($this->input->post('slug')) == false ) {
  258.       // Fields unset or blank
  259.       $this->form_validation->set_message('check_login_validation', $this->lang->line('flip_login_generic_error'));
  260.       return false;
  261.     }
  262.     else {
  263.       $publication = $this->publication->get_publication_by_slug($this->input->post('slug'));
  264.      
  265.       // Prepare URL for get request
  266.       $user_exists_url = array();
  267.       $user_exists_url[] = $publication->publication_check_login_url;
  268.       $user_exists_url[] = 'check/new/login/' . $this->input->post('flip-login-user') . '/';
  269.       $user_exists_url[] = 'password/' . $this->input->post('flip-login-pass');
  270.       $user_exists_url = implode('', $user_exists_url);
  271.      
  272.       // Check if user exists
  273.       $user_exists = false;
  274.       $ch = curl_init();
  275.       curl_setopt($ch, CURLOPT_URL, $user_exists_url);
  276.       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  277.       $result = curl_exec($ch);
  278.       curl_close($ch);
  279.      
  280.       /*
  281.        $result
  282.        0 - concluí­do com sucesso
  283.        1 - e-mail não encontrado
  284.        2 - senha inválida
  285.        */
  286.       $result = substr($result, 0, 1);
  287.  
  288.       switch ($result) {
  289.         case '0':
  290.           $user_exists = true;        
  291.           break;
  292.  
  293.         case '1':
  294.         case '2':
  295.           $this->form_validation->set_message('check_login_validation', $this->lang->line('flip_login_generic_error'));
  296.           return false;
  297.           break;
  298.  
  299.         default:
  300.           $this->form_validation->set_message('check_login_validation', sprintf($this->lang->line('flip_login_generic_error_with_code'), '101')); // Code 101 for debugging purposes, do NOT expose details
  301.           return false;
  302.           break;
  303.       }
  304.  
  305.       // Check if subscription is active
  306.       if( $user_exists ) {
  307.         $vehicles = unserialize($publication->publication_vehicle_number);
  308.  
  309.         $vehicle_values = '';
  310.         foreach ($vehicles as $vehicle) {
  311.           $vehicle_values .= 'vehicle='.$vehicle.'&';
  312.         }
  313.         $vehicle_values = rtrim($vehicle_values, '&');
  314.  
  315.         // Prepare URL for get request
  316.         $subscription_active_url = array();
  317.         $subscription_active_url[] = $publication->publication_check_login_url;
  318.         $subscription_active_url[] = 'check/signature/actual/login/' . $this->input->post('flip-login-user') . '/';
  319.         $subscription_active_url[] = 'password/' . $this->input->post('flip-login-pass');
  320.         $subscription_active_url[] = '?' . $vehicle_values;
  321.         $subscription_active_url = implode('', $subscription_active_url);
  322.  
  323.         $ch = curl_init();
  324.         curl_setopt($ch, CURLOPT_URL, $subscription_active_url);
  325.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  326.         $result = curl_exec($ch);
  327.         curl_close($ch);
  328.  
  329.         /*
  330.          $result:
  331.          {"nmcliente":"NOME DO CLIENTE","tpvigente":false,"nuqtdeexemplares":0}
  332.          */
  333.         $result = json_decode($result);
  334.  
  335.         if( is_null($result) ) {
  336.           $this->form_validation->set_message('check_login_validation', sprintf($this->lang->line('flip_login_generic_error_with_code'), '102')); // Code 102 for debugging purposes, do NOT expose details
  337.           return false;
  338.         }
  339.         if( $result->tpvigente ) {
  340.           $this->end_user = array(
  341.             'email' => strtolower($result->nmcliente),
  342.             'name' => ucwords(strtolower($result->nmcliente))
  343.           );
  344.           return true;
  345.         }
  346.         else {
  347.           $this->form_validation->set_message('check_login_validation', sprintf($this->lang->line('flip_login_renew_subscription'), ucwords(strtolower($result->nmcliente))));
  348.           return false;
  349.         }
  350.       }
  351.       else {
  352.         $this->form_validation->set_message('check_login_validation', $this->lang->line('flip_login_generic_error'));
  353.         return false;
  354.       }
  355.     }
  356.   } // check_login_validation()
  357.  
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement