<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

if(!function_exists('log_email'))
{
    /**
    *@desc Sends mail with supplied message as a log. Use this function if the error is serious and needs quick fix.
    */
    function log_email($message)
    {
        if($_SERVER['SERVER_ADDR'] != '127.0.0.1')
        {
            $sendMailTo = 'a.r.zerin@gmail.com';

            if(strstr($message, 'The merchant login ID or password is invalid or the account is inactive'))
            {
                $message .= '<br/><br/>Please check if test mode is OFF or not';
            }

            $CI =& get_instance();
            $CI->load->library('email');
            $CI->email->set_mailtype('html');
            $CI->email->from('no-reply@taskgum.com', 'PWC Logger');
            $CI->email->to($sendMailTo);
            $CI->email->subject('Transaction Related Error');
            $body = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Error</title>
</head>
<body>
<b>Following Error Occured:</b><br/>
<font color="#FF0000"><b>'.$message.'</b></font>
</body>
</html>

';
            $CI->email->message($body);
            $CI->email->send();
            $CI->email->clear();
        }
    }
}

if(!function_exists('log_recurring_order'))
{
    function log_recurring_order($message)
    {
        $CI = get_instance();
        $logEnabled = $CI->config->item('enable_pwc_log') == 1;

        if($logEnabled)
        {
            $filePath = ROOT_PATH . 'logs/recurring_logs/' . date('Y-m-d') . '.php';

            $lineStart = date('H:i:s --> ');

            if(is_array($message))
            {
                $text = $lineStart . implode("\n" . $lineStart, $message);
            }
            else
            {
                $text = $lineStart . $message;
            }
            write_log_file($filePath, $text);
        }
    }
}

if(!function_exists('log_schedule'))
{
    function log_schedule($message)
    {
        $CI = get_instance();
        $logEnabled = $CI->config->item('enable_pwc_log') == 1;

        if($logEnabled)
        {
            $filePath = ROOT_PATH . 'logs/schedule_logs/' . date('Y-m-d') . '.php';

            $lineStart = date('H:i:s --> ');

            if(is_array($message))
            {
                $text = $lineStart . implode("\n" . $lineStart, $message);
            }
            else
            {
                $text = $lineStart . $message;
            }
            write_log_file($filePath, $text);
        }
    }
}


if(!function_exists('log_merchant_charges'))
{
    function log_merchant_charges($message)
    {
        $CI = get_instance();
        $logEnabled = $CI->config->item('enable_pwc_log') == 1;

        if($logEnabled)
        {
            $filePath = ROOT_PATH . 'logs/merchant_logs/' . date('Y-m-d') . '.php';

            $text = date('H:i:s --> ') . $message;
            write_log_file($filePath, $text);
        }
    }
}

if(!function_exists('log_booking'))
{
    function log_booking($message)
    {
        $CI = get_instance();
        $logEnabled = $CI->config->item('enable_pwc_log') == 1;

        if($logEnabled)
        {
            $filePath = ROOT_PATH . 'logs/booking_logs/' . date('Y-m-d') . '.php';

            $text = date('H:i:s --> ') . $message;
            write_log_file($filePath, $text);
        }
    }
}

if(!function_exists('log_general'))
{
    function log_general($message)
    {
        $CI = get_instance();
        $logEnabled = $CI->config->item('enable_pwc_log') == 1;

        if($logEnabled)
        {
            $filePath = ROOT_PATH . 'logs/general_logs/' . date('Y-m-d') . '.php';

            $text = date('H:i:s --> ') . $message;
            write_log_file($filePath, $text);
        }
    }
}

if(!function_exists('log_general_array'))
{
    function log_general_array($array)
    {
        if((is_array($array) || is_object($array)) && !empty($array))
        {
            log_general(print_r($array, true));
        }
    }
}

if(!function_exists('log_db'))
{
    function log_db($message)
    {
        $CI = get_instance();
        $logEnabled = $CI->config->item('enable_pwc_log') == 1;

        if($logEnabled)
        {
            $filePath = ROOT_PATH . 'logs/db_logs/' . date('Y-m-d') . '.php';

            $text = date('H:i:s --> ') . $message;
            write_log_file($filePath, $text);

            if($_SERVER['SERVER_ADDR'] != '127.0.0.1')
            {
                $sendMailTo = 'a.r.zerin@gmail.com';

                $CI =& get_instance();
                $CI->load->library('email');
                $CI->email->set_mailtype('html');
                $CI->email->from('no-reply@premiumwebcart.com', 'PWC DB Logger');
                $CI->email->to($sendMailTo);
                $CI->email->subject('Database Error');
                $body = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Database Error</title>
    </head>

    <body>
    <b>Following Error Occured(' . $_SERVER['PHP_SELF'] . '):<b><br/>
    <font color="#FF0000" size="2"><pre>'.$message.'</pre></font>
    </body>
    </html>

    ';
                $CI->email->message($body);
                $CI->email->send();
                $CI->email->clear();
            }
        }
    }
}

if(!function_exists('log_ipn_message'))
{
    function log_ipn_message($message)
    {
        $CI = get_instance();
        $logEnabled = $CI->config->item('enable_pwc_log') == 1;

        if($logEnabled)
        {
            $filePath = ROOT_PATH . 'logs/ipn_logs/' . date('Y-m-d') . '.php';

            $text = date('H:i:s --> ') . $message;
            write_log_file($filePath, $text);
        }
    }
}

if(!function_exists('log_cart_error'))
{
    function log_cart_error($message, $type ='cart error')
    {
        $CI = get_instance();

        if($CI->config->item('enable_pwc_log'))
        {
            $filePath = ROOT_PATH . 'logs/cart_logs/' . date('Y-m-d') . '.php';

            $text = date('H:i:s') . ',' . $type . ',' . $message;
            write_log_file($filePath, $text);
        }
    }
}

if(!function_exists('log_api_message'))
{
    function log_api_message($apiName, $functionName, $message)
    {
        $CI = get_instance();
        $logEnabled = $CI->config->item('enable_api_logging');

        if($logEnabled)
        {
            $filePath = ROOT_PATH . 'logs/api_logs/' . date('Y-m-d') . '.php';

            if($apiName && $functionName)
            {
                $text = date('H:i:s - ') . "$apiName ($functionName) : " . substr($message, 0, 4000);
            }
            else
            {
                $text = date('H:i:s - ') . substr($message, 0, 4000);
            }
            write_log_file($filePath, $text);
        }
    }
}

if(!function_exists('log_api_access'))
{
    function log_api_access($serviceName, $key, $message)
    {
        $CI = get_instance();
        $logEnabled = $CI->config->item('enable_api_logging');

        if($logEnabled)
        {
            $filePath = ROOT_PATH . 'logs/api_logs/access_' . date('Y-m-d') . '.php';

            $text = date('d-m-Y H:i:s') .", $serviceName, $key, $message, " . $CI->input->ip_address();
            write_log_file($filePath, $text);
        }
    }
}

if(!function_exists('write_log_file'))
{
    function write_log_file($filePath, $text)
    {
        if(!file_exists($filePath))
        {
            $f = fopen($filePath, 'ab');
            flock($f, LOCK_EX);
            fwrite($f, "<?php die('Access Denied');?>\n");
        }
        else
        {
            $f = fopen($filePath, 'ab');
            flock($f, LOCK_EX);
        }
        $text = "$text\n";
        fwrite($f, $text);
        flock($f, LOCK_UN);
        fclose($f);
    }
}

if(!function_exists('log_commission_email'))
{
    function log_commission_email($data)
    {
        if($_SERVER['SERVER_ADDR'] != '127.0.0.1')
        {
            $sendMailTo = 'a.r.zerin@gmail.com';
            $CI =& get_instance();
            $CI->load->library('email');
            $CI->email->set_mailtype('html');
            $CI->email->from('no-reply@taskgum.com', 'PWC Logger');
            $CI->email->to($sendMailTo);
            $CI->email->subject('Commission Payment Log - '.date("Y-m-d"));
            $body = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table cellpadding="2" cellspacing="0" border="0" width="100%" align="center">
    <tr>
        <td>
            The following affiliates are selected by the system for log maintenance.
        </td>
    </tr>
    <tr>
        <td>
            <table cellpadding="2" cellspacing="0" border="0" width="100%" align="center">
                <tr>
                    <th width="10%">Affiliate ID</th>
                    <th width="20%">Affiliate Name</th>
                    <th width="10%">Amount Paid</th>
                    <th width="10%">Amount Calculated</th>
                </tr>';

                    foreach($data as $aLog)
                    {

       $body .=         '<tr>
                    <td width="5%">'.$aLog['affiliate_id'].'</td>
                    <td width="25%">'.$aLog['affiliate_name'].'</td>
                    <td width="10%">'.$aLog['paid_commission'].'</td>
                    <td width="10%">'.$aLog['calculated_amount'].'</td>
                </tr>';

                    }

            $body .='</table>
        </td>
    </tr>
</table>
</body>
</html>

';

            $CI->email->message($body);
            $CI->email->send();
            $CI->email->clear();
        }
    }
}
?>
