codeignitor 3 setup custom code

//My_model .php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class My_model extends CI_Model 
{
function insertData($table,$data)
{
$this->db->insert($table,$data);
}
function getData($table,$resultType = 'result_array',$arg=[])
{
if(isset($arg['select']))
{
$this->db->select($arg['select']);
}
if(isset($arg['where']))
{
$this->db->where($arg['where']);
}
if(isset($arg['join']))
{
$this->db->join($arg['join']['table'], $arg['join']['query'], $arg['join']['type']);
}
if (isset($arg['group']))
{
$this->db->group_by($arg['group']['col']);
}
if (isset($arg['limit']))
{
$this->db->limit($arg['limit']['upto']);
}
if (isset($arg['order']))
{
$this->db->order_by($arg['order']['col'],$arg['order']['type']);
        }
        if (isset($arg['like']))
        {
$this->db->like($arg['like']['col'],$arg['like']['query']);
}
if ($resultType=="result_array")
{
$return=$this->db->get($table)->result_array();
}
else if($resultType=="row_array")
{
$return=$this->db->get($table)->row_array();
}
else if($resultType=="result")
{
$return=$this->db->get($table)->result();
}
else if($resultType=="count_all_results")
{
$return=$this->db->count_all_results($table);
}
return $return; 
}
function updateData($table,$data,$where=false)
{
if($where!==false)
{
$this->db->where($where);
}
$this->db->update($table,$data);
}
function deleteData($table,$data)
{
$this->db->delete($table,$data);
}
}

//custom_helper.php

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function checkAdminsession()
{
$ci =& get_instance(); 
if($ci->session->userdata('adminid') && $ci->session->userdata('adminemail'))
{
return true;
}
else
{
return false;
}
}
function checkUsersession()
{
$ci =& get_instance(); 
if($ci->session->userdata('userid') && $ci->session->userdata('useremail'))
{
return true;
}
else
{
return false;
}
}
function responseGenerate($type,$msg="")
{
$result=['type'=>$type,'msg'=>$msg];
echo json_encode($result);
exit;
}
function encryptFunction($var)
{
    $textToEncrypt = $var;
        $encryptionMethod = "AES-256-CBC";  // AES is used by the U.S. gov't to encrypt top secret documents.
        $secretHash = "25c6c7ff35b9979b151f2136cd13b0ff";
     //   $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
        $iv = str_repeat('z', 16);
        //To encrypt
        $encryptedMessage = openssl_encrypt($textToEncrypt, $encryptionMethod, $secretHash, 0, $iv);
        return $encryptedMessage;
    
}
function decryptFunction($var)
{
        $textToEncrypt = $var;
        $encryptionMethod = "AES-256-CBC";  // AES is used by the U.S. gov't to encrypt top secret documents.
        $secretHash = "25c6c7ff35b9979b151f2136cd13b0ff";
       // $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
        $iv = str_repeat('z', 16);
     //To Decrypt
        $decryptedMessage = openssl_decrypt($textToEncrypt, $encryptionMethod, $secretHash, 0, $iv);
    return $decryptedMessage;
}
function sendMailcontactFunction($mailData)
{
    $ci =& get_instance(); 
    $ci->load->library('email');
        $sitesetting = $ci->my_model->getData('tblsetting',$resultType = 'row_array',$arg=[]);
        $to = $sitesetting['contactemailaddress'];
        
        
        $config['protocol']     = 'SMTP';
        $config['smtp_host']    = 'safari.mxrouting.net';
        $config['smtp_port']    = '465';
        $config['mailpath']     = '/usr/sbin/sendmail';
        $config['smtp_timeout'] = '5';
        $config['smtp_user']    = 'info@zakatcalc.com'; //emai
        $config['smtp_pass']    = 'YsZjcgX2Tr'; // email password
        $config['charset']      = 'iso-8859-1';
        // $config['newline']      = "\r\n";
        $config['mailtype']     = 'html'; // or html
        $config['validation']   = TRUE; // bool whether to validate email or not 

    
        $name = $mailData['name'];
        $from = $mailData['email'];
        $subject = $mailData['subject'];
        $message = $mailData['message'];
        
        $ci->email->initialize($config);

        $ci->email->from($from,$name);
        $ci->email->to($to);
        $ci->email->subject($subject);
        $ci->email->message($message);

        if($ci->email->send())
        {
return true;
        }
        else
        {
            return false;
        }
}
function sendMailfunction($receiver)
{
    $ci =& get_instance(); 
    $ci->load->library('email');
        $sitesetting = $ci->my_model->getData('tblsetting',$resultType = 'row_array',$arg=[]);
        $sitename = $sitesetting['sitename'];
        $mailingemail = $sitesetting['contactemailaddress'];
$from = $mailingemail;    //senders email address
if(!empty($sitesetting['verifyusersubject']))
{
    $verifyusersubject = $sitesetting['verifyusersubject']." ";
}
else
{
    $verifyusersubject = "Welcome to ";
}
        $subject = $verifyusersubject." ".$sitename;  //email subject
        
        $message='<!doctype html>
                <html>
                  <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
                    <meta name="viewport" content="width=device-width" />
                    <style>
                    </style>
                  </head>
                  <body class="">
                    <table border="0" cellpadding="0" cellspacing="0" class="body">
                      <tr>
                        <td>&nbsp;</td>
                        <td class="container">
                          <div class="content">
                            
                            <table class="main">
                
                              <!-- START MAIN CONTENT AREA -->
                              <tr>
                                <td class="wrapper">
                                  <table border="0" cellpadding="0" cellspacing="0">
                                    <tr>
                                      <td>
                                      <p style="text-align:center">
                                       <img style="text-align:center" src="'.base_url().'assets/uploads/sitesetting/'.$sitesetting["logo"].'" width=120px></p>
                                        <h2>Account Confirmation</h2>
                                        <h3>Assalaamu&#39;alaykum warahmatullaahi wabarakaatuh,</h3>
                                        <h4>You just signed up for ZakatCalc. Please follow this link to verify your email address.</h4>
                                        <table border="0" cellpadding="0" cellspacing="0" class="btn btn-primary">
                                          <tbody>
                                            <tr>
                                              <td align="left">
                                                <table border="0" cellpadding="0" cellspacing="0">
                                                  <tbody>
                                                    <tr>
                                                      <td style="text-align:center !important;"> <a href="'.base_url().'user/confirmationEmail/'.base64_encode($receiver).'">Email Verification</a> </td>
                                                    </tr>
                                                  </tbody>
                                                </table>
                                              </td>
                                            </tr>
                                          </tbody>
                                        </table>
                                        <h4>Thanks,<br />ZakatCalc Team</h4>
                                      </td>
                                    </tr>
                                  </table>
                                </td>
                              </tr>
                
                            <!-- END MAIN CONTENT AREA -->
                            </table>
                
                            <!-- START FOOTER -->
                            <div class="footer">
                              <table border="0" cellpadding="0" cellspacing="0">
                                <tr>
                                
                                </tr>
                                <tr>
                                  <td class="content-block powered-by">
                                    Powered by <a href="https://zakatcalc.com" target="_blank" alt="Zakat Calculator for your ease">ZakatCalc.com</a>
                                  </td>
                                </tr>
                              </table>
                            </div>
                            <!-- END FOOTER -->
                            
                          <!-- END CENTERED WHITE CONTAINER -->
                          </div>
                        </td>
                        <td>&nbsp;</td>
                      </tr>
                    </table>
                  </body>
                </html>';
                       
    //cofirmation email end
        // $config['protocol'] = 'sendmail';
        // $config['mailpath'] = '/usr/sbin/sendmail';
        // $config['charset'] = 'iso-8859-1';
        // $config['wordwrap'] = TRUE;
        // $config['mailtype'] = 'html';
        
//         $mail_config['smtp_host'] = 'smtp.gmail.com';
// $mail_config['smtp_port'] = '587';
// $mail_config['smtp_user'] = 'user@example.com';
// $mail_config['_smtp_auth'] = TRUE;
// $mail_config['smtp_pass'] = 'password';
// $mail_config['smtp_crypto'] = 'tls';
// $mail_config['protocol'] = 'smtp';
// $mail_config['mailtype'] = 'html';
// $mail_config['send_multipart'] = FALSE;
// $mail_config['charset'] = 'utf-8';
// $mail_config['wordwrap'] = TRUE;
// $this->email->initialize($mail_config);

// $this->email->set_newline("\r\n");
        //     $config['protocol'] = 'sendmail';
        // $config['mailpath'] = '/usr/sbin/sendmail';
        // $config['charset'] = 'iso-8859-1';
        // $config['wordwrap'] = TRUE;
        // $config['mailtype'] = 'html';

        
        $config['protocol']     = 'SMTP';
        $config['smtp_host']    = 'safari.mxrouting.net';
        $config['smtp_port']    = '465';
        $config['mailpath']     = '/usr/sbin/sendmail';
        $config['smtp_timeout'] = '5';
        $config['smtp_user']    = 'info@zakatcalc.com'; //emai
        $config['smtp_pass']    = 'YsZjcgX2Tr'; // email password
        $config['charset']      = 'iso-8859-1';
        // $config['newline']      = "\r\n";
        $config['mailtype']     = 'html'; // or html
        $config['validation']   = TRUE; // bool whether to validate email or not 

        
        $frommessage = $sitename;
        $ci->email->initialize($config);

        $ci->email->from($from,$frommessage);
        $ci->email->to($receiver);
        $ci->email->subject($subject);
        $ci->email->message($message);

        if($ci->email->send())
        {
return true;
        }
        else
        {
            return false;
        }
}
   
    }
//Contoller.php
function __construct()
{
parent::__construct();
//Load user model
        $this->load->model('my_model');
$this->load->library('form_validation');
}
public function index()
{
    $sitesetting = $this->my_model->getData('tblsetting',$resultType = 'row_array',$arg=[]);
    $data['sitename'] = $sitesetting['sitename'];
    $data['sitelogo'] = $sitesetting['logo'];
    $data['sitefavicon'] = $sitesetting['favicon'];
if(checkAdminsession())
{
$data['title'] = "Admin DashBoard";
$data['blognumber'] = $this->my_model->getData('tblblog',$resultType = 'count_all_results',$arg=[]);
$data['usernumber'] = $this->my_model->getData('tbluser',$resultType = 'count_all_results',$arg=[]);
$this->load->view('admin/dashboard',$data);
}
else
{
$data['title'] = "Admin Login";
$this->load->view('admin/login',$data);
}
}
public function loginProcess()
{
if(checkAdminsession())
{
redirect('admin');
}
$sitesetting = $this->my_model->getData('tblsetting',$resultType = 'row_array',$arg=[]);
    $data['sitename'] = $sitesetting['sitename'];
    $data['sitelogo'] = $sitesetting['logo'];
    $data['sitefavicon'] = $sitesetting['favicon'];
$this->form_validation->set_rules('email', 'Email','trim|required');
$this->form_validation->set_rules('password','Password','trim|required');

if ($this->form_validation->run() == FALSE)
        {
            $data['sitelogo'] = $sitesetting['logo'];
        $data['sitefavicon'] = $sitesetting['favicon'];
    $data['title'] = "Admin Login";
$this->load->view('admin/login',$data);
        }
        else
        {
        $email = $this->input->post('email');
        $password = base64_encode($this->input->post('password'));
$data = $this->my_model->getData('tbladmin',$resultType = 'row_array',$arg=['where'=>['email'=>$email,'password'=>$password]]);
if(!empty($data))
{
$session = [
'adminid' => $data['id'],
'adminemail' => $data['email'],
'adminphoto' => $data['photo'],
];

$this->session->set_userdata($session);
redirect('admin');
}
else
{
    $data['title'] = "Admin Login";
    $data['sitename'] = $sitesetting['sitename'];
            $data['sitelogo'] = $sitesetting['logo'];
            $data['sitefavicon'] = $sitesetting['favicon'];
$this->session->set_flashdata('adminInvalidmsg','Invalid Email Or Password');
$this->load->view('admin/login',$data);
}
 
        }
}
public function logoutProcess()
{
$data['title'] = "Admin Login";
$sitesetting = $this->my_model->getData('tblsetting',$resultType = 'row_array',$arg=[]);
    $data['sitename'] = $sitesetting['sitename'];
    $data['sitelogo'] = $sitesetting['logo'];
    $data['sitefavicon'] = $sitesetting['favicon'];
$this->session->sess_destroy();
$this->session->set_flashdata('adminlogoutmsg','Successfully Log-Out');
$this->load->view('admin/login',$data);
}
public function loadProfile(){
    if(!checkAdminsession())
{
redirect('admin');
}
    $data['title'] = "Admin Profile";
    $sitesetting = $this->my_model->getData('tblsetting',$resultType = 'row_array',$arg=[]);
    $data['sitename'] = $sitesetting['sitename'];
    $data['sitelogo'] = $sitesetting['logo'];
    $data['sitefavicon'] = $sitesetting['favicon'];
    $id = $this->session->userdata('adminid');
    $data['profile'] = $this->my_model->getData('tbladmin',$resultType = 'row_array',$arg=['where'=>['id'=>$id]]);
$this->load->view('admin/profile',$data);
}

Comments

Popular posts from this blog

Laravel 8 Multi Auth (Authentication) Tutorial

MySQL - Stored Procedures && Functions

crons job