Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
- class Flip extends CI_Controller {
- private $header_data = array();
- private $data = array();
- private $footer_data = array();
- private $end_user = null; // Used by check_login_validation() to send user's name and email to login()
- /*
- * Load helpers, models, and eveything that should be used by ALL methods
- */
- public function __construct($slug = null) {
- parent::__construct();
- $this->lang->load('flp_flip', 'pt-BR');
- $this->lang->load('form_validation', 'pt-BR');
- $this->load->helper( array('form', 'html', 'url') );
- $this->load->model('company_model', 'company');
- $this->load->model('publication_model', 'publication');
- $this->load->model('edition_model', 'edition');
- $this->load->model('flip_model', 'flip');
- }
- /*
- * Index page
- * It will basically check if the given slug exists (is it a publication or a company?)
- */
- public function index($slug = null) {
- // If slug not found: display about page
- if( $this->display_about_page($slug) ) {
- redirect( 'http://aspintecnologia.com.br/aspin-news' );
- }
- // else slug found, but user not logged in
- elseif( is_null($this->session->userdata('user_email')) ) {
- redirect( base_url('flip/'.$slug.'/login') );
- }
- // else user is logged in
- else {
- redirect( base_url('flip/'.$slug.'/list') );
- }
- } // index()
- /*
- * About ONLY redirects to an URL
- * The last "catch everything" route uses this method
- */
- public function about() {
- redirect( 'http://aspintecnologia.com.br/aspin-news' );
- }
- /*
- * Login page
- * It asks Check Login (web service) if the user (email and password) is an active subscriber
- */
- public function login($slug = null) {
- // Slug not found: display about page
- if( $this->display_about_page($slug) ) redirect( base_url('/') );
- $this->load->library('form_validation');
- /*
- $slug_type = $this->set_slug_type($slug); // 'company' or 'publication'
- */
- $publication = $this->publication->get_publication_by_slug($slug);
- $company_logo = $this->company->get_company_logo($publication->company_cnpj);
- $this->header_data['index_page'] = base_url('flip/'.$slug);
- $this->header_data['title'] = $publication->publication_name;
- $this->header_data['body_classes'] = 'flip flip-login';
- $this->data['slug'] = $slug;
- $this->data['logo'] = $company_logo;
- $this->data['publication'] = $publication;
- if ($this->form_validation->run('flip/login') == false) {
- $this->load->view('shared/header_flip', $this->header_data);
- $this->load->view('flip_login', $this->data);
- $this->load->view('shared/footer_flip');
- }
- else {
- // Set user session
- $flip_user = array(
- 'flip_user_email' => $this->end_user['email'],
- 'flip_user_name' => $this->end_user['name'],
- 'company_cnpj' => $publication->company_cnpj,
- );
- $this->session->set_userdata($flip_user);
- $this->end_user = null;
- // And redirect to flip/$slug/list
- redirect( base_url('flip/'.$slug.'/list') );
- }
- } // login()
- public function logout($slug = null) {
- if( $this->display_about_page($slug) ) redirect( base_url('/') );
- elseif( is_null($this->session->userdata('flip_user_email')) ) { redirect( base_url('flip/'.$slug.'/login') ); }
- else {
- $this->session->sess_destroy();
- redirect( base_url('flip/'.$slug.'/login') );
- }
- } // logout()
- public function list($slug = null) {
- // Slug not found: display about page
- if( $this->display_about_page($slug) ) { redirect( base_url('/') ); }
- elseif( is_null($this->session->userdata('flip_user_email')) ) { redirect( base_url('flip/'.$slug.'/login') ); }
- else {
- /*
- $slug_type = $this->set_slug_type($slug); // 'company' or 'publication'
- */
- $publication = $this->publication->get_publication_by_slug($slug);
- $this->header_data['index_page'] = base_url('flip/'.$slug.'/list');
- $this->header_data['body_classes'] = 'flip flip-list';
- $this->header_data['title'] = $publication->publication_name;
- $this->header_data['header_color'] = $publication->publication_header_color;
- $this->header_data['header_style'] = $publication->publication_color_lightness;
- $this->header_data['logo'] = $publication->publication_logo;
- $this->header_data['slug'] = $slug;
- //$limit = $this->month_number_of_days();
- $this->data['editions'] = $this->flip->get_editions($publication->publication_id, 30);
- $this->load->view('shared/header_flip', $this->header_data);
- $this->load->view('flip_list', $this->data);
- $this->load->view('shared/footer_flip');
- }
- } // list()
- public function view($slug = null, $info = null) {
- $edition_id = $this->flip->encrypt_decrypt_edition_id($info, 'decrypt');
- // Slug not found: display about page
- if( $this->display_about_page($slug) ) { redirect( base_url('/') ); }
- // User no logged in
- elseif( is_null($this->session->userdata('flip_user_email')) ) { redirect( base_url('flip/'.$slug.'/login') ); }
- // Hash not set or Edition ID not found from hash
- elseif( is_null($info) || $edition_id == false ) { redirect( base_url('flip/'.$slug.'/list') ); }
- // Display edition
- else {
- /*
- $slug_type = $this->set_slug_type($slug); // 'company' or 'publication'
- */
- $edition = $this->edition->get_edition_by_id($edition_id);
- $publication = $this->publication->get_publication_by_id($edition->publication_id);
- $date = date_create($edition->edition_date);
- $date = date_format($date, 'd\/m\/Y');
- $title = $publication->publication_name . ' Edição: ' . $edition->edition_number . ' Data: ' . $date;
- $this->header_data['index_page'] = base_url('flip/'.$slug.'/list');
- $this->header_data['title'] = $title;
- $this->header_data['body_classes'] = 'flip flip-view';
- $this->header_data['publication'] = $publication;
- $this->header_data['slug'] = $slug;
- $this->header_data['display_edition'] = true;
- $this->header_data['back_btn'] = array('location' => 'view', 'slug' => $slug, 'name' => $publication->publication_name);
- $this->data['edition'] = $edition;
- $this->footer_data['display_edition'] = true;
- $this->footer_data['edition'] = $edition;
- $this->footer_data['publication'] = $publication;
- $this->load->view('shared/header_flip', $this->header_data);
- $this->load->view('flip_view', $this->data);
- $this->load->view('shared/footer_flip');
- }
- } // view()
- /*
- * Helper functions
- */
- public function display_about_page( $slug ) {
- $about_page = false;
- // No slug given in the URL
- if( $slug == null ) $about_page = true;
- // Slug doesn't exist in BOTH flp_company and flp_publication
- if( $this->publication->publication_slug_exists($slug) == false &&
- $this->company->company_slug_exists($slug) == false
- ) {
- $about_page = true;
- }
- return $about_page;
- } // display_about_page()
- public function set_slug_type( $slug ) {
- if( $this->publication->publication_slug_exists($slug) ) return 'publication';
- else return 'company';
- // at this point display_about_page() already checked that
- // either a publication or a company exists for a given slug
- } // set_slug_type()
- // https://davidwalsh.name/checking-for-leap-year-using-php
- function is_leap_year($year) {
- return ((($year % 4) == 0) && ((($year % 100) != 0) || (($year % 400) == 0)));
- }
- public function month_number_of_days() {
- date_default_timezone_set('America/Sao_Paulo');
- $current_month = date('n');
- $current_year = date('Y');
- // Decide how many days for query $limit
- switch ($current_month) {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- return 31;
- break;
- case 2:
- if( $this->is_leap_year($current_year) ) $limit = 29;
- return 28;
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- return 30;
- break;
- }
- }
- /*
- * Form validation functions
- */
- public function check_login_validation() {
- /*
- * DO NOT ever tell the user what is wrong (email or password)
- */
- if( is_null($this->input->post('flip-login-user')) ||
- is_null($this->input->post('flip-login-pass')) ||
- is_null($this->input->post('slug')) ||
- strlen($this->input->post('flip-login-user')) == 0 ||
- strlen($this->input->post('flip-login-pass')) == 0 ||
- $this->publication->publication_slug_exists($this->input->post('slug')) == false ) {
- // Fields unset or blank
- $this->form_validation->set_message('check_login_validation', $this->lang->line('flip_login_generic_error'));
- return false;
- }
- else {
- $publication = $this->publication->get_publication_by_slug($this->input->post('slug'));
- // Prepare URL for get request
- $user_exists_url = array();
- $user_exists_url[] = $publication->publication_check_login_url;
- $user_exists_url[] = 'check/new/login/' . $this->input->post('flip-login-user') . '/';
- $user_exists_url[] = 'password/' . $this->input->post('flip-login-pass');
- $user_exists_url = implode('', $user_exists_url);
- // Check if user exists
- $user_exists = false;
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $user_exists_url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $result = curl_exec($ch);
- curl_close($ch);
- /*
- $result
- 0 - concluído com sucesso
- 1 - e-mail não encontrado
- 2 - senha inválida
- */
- $result = substr($result, 0, 1);
- switch ($result) {
- case '0':
- $user_exists = true;
- break;
- case '1':
- case '2':
- $this->form_validation->set_message('check_login_validation', $this->lang->line('flip_login_generic_error'));
- return false;
- break;
- default:
- $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
- return false;
- break;
- }
- // Check if subscription is active
- if( $user_exists ) {
- $vehicles = unserialize($publication->publication_vehicle_number);
- $vehicle_values = '';
- foreach ($vehicles as $vehicle) {
- $vehicle_values .= 'vehicle='.$vehicle.'&';
- }
- $vehicle_values = rtrim($vehicle_values, '&');
- // Prepare URL for get request
- $subscription_active_url = array();
- $subscription_active_url[] = $publication->publication_check_login_url;
- $subscription_active_url[] = 'check/signature/actual/login/' . $this->input->post('flip-login-user') . '/';
- $subscription_active_url[] = 'password/' . $this->input->post('flip-login-pass');
- $subscription_active_url[] = '?' . $vehicle_values;
- $subscription_active_url = implode('', $subscription_active_url);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $subscription_active_url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $result = curl_exec($ch);
- curl_close($ch);
- /*
- $result:
- {"nmcliente":"NOME DO CLIENTE","tpvigente":false,"nuqtdeexemplares":0}
- */
- $result = json_decode($result);
- if( is_null($result) ) {
- $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
- return false;
- }
- if( $result->tpvigente ) {
- $this->end_user = array(
- 'email' => strtolower($result->nmcliente),
- 'name' => ucwords(strtolower($result->nmcliente))
- );
- return true;
- }
- else {
- $this->form_validation->set_message('check_login_validation', sprintf($this->lang->line('flip_login_renew_subscription'), ucwords(strtolower($result->nmcliente))));
- return false;
- }
- }
- else {
- $this->form_validation->set_message('check_login_validation', $this->lang->line('flip_login_generic_error'));
- return false;
- }
- }
- } // check_login_validation()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement