<?php
/********************************************************************************
 * (c)1337 aheadware.com - All rights reserved
 ********************************************************************************/

/**
 * Class Francis_Utils_Log
 */
class Francis_Utils_Log {

    protected $logile = NULL;
    protected $logfileHandle = false;

    protected $logSlackWebhook = NULL;

    // Constructor
    public function __construct( $logfile = NULL, $slackWebhook = NULL ) {
        if ( !is_null( $logfile ) ) {
            $this->logfile = $logfile;
            $this->logfileHandle = @fopen( $this->logfile, "a+t" );
        }

        if ( !is_null( $slackWebhook ) )
        {
            $this->logSlackWebhook = $slackWebhook;
        }
    }

    /**
     * Log given data
     *
     * @param mixed $data
     */
    public function log( $data ) {
        $logTxt = '[' . date( "d-m-Y H:i:s" ) . '] ';
        if ( $data instanceof Exception ) {
            $logTxt .= $data->getMessage() . PHP_EOL;
            $logTxt .= $data->getTraceAsString() . PHP_EOL;
            $logTxt .= $data->getFile() . PHP_EOL;
            $logTxt .= $data->getLine() . PHP_EOL;
        } else {
            $logTxt .= '' . $data . PHP_EOL;
        }

        // Add context sensitive logging
        // Note
        // Breaking rules for TBuddy project here and accessing Tbuddy classes
        $context = '';

        try {

            /** @var TB_Server_Core_Session $session */
            $session = TB_Server_Core_Session::get();
            /** @var TB_Shared_Ent_TeamData_Profile $profile */
            $profile = isset( $session ) ? $session->getProfile() : NULL;
            if ( isset( $profile) ) {
                $context .= "Profile ID: " . $profile->id . " - Team ID: " . $profile->team_id . PHP_EOL;
            }

            if ( isset( $_REQUEST ) && is_array( $_REQUEST ) ) {
                $context .= "Request: " . @json_encode( $_REQUEST ) . PHP_EOL;
            }

            if ( isset( $_SERVER ) && is_array( $_SERVER ) && isset( $_SERVER[ 'HTTP_USER_AGENT' ] ) ) {
                $context .= ' Browser: ' . $_SERVER[ 'HTTP_USER_AGENT' ] . PHP_EOL;
            }


        } catch( Exception $e ) {
            $context = 'Context-Error: ' . $e->getMessage();
        }
        $logTxt .= $context;

        $logTxt .= '#######################################################################' . "\n";

        // Logout
        if ( isset($_SERVER['REQUEST_METHOD']) ) ChromePhp::log( $logTxt );
        if ( $this->logfile && $this->logfileHandle ) {
            @fputs( $this->logfileHandle, $logTxt );
        }

        if ( $this->logSlackWebhook )
        {
            $this->logSlack( $logTxt );
        }

    }

    /**
     * @param $logTxt
     */
    private function logSlack( $logTxt )
    {
        if ( is_null( $this->logSlackWebhook ) )
        {
            return;
        }

        // Make your message
        $message = array( 'payload' => json_encode( array( 'text' => $logTxt ) ) );
        // Use curl to send your message
        $c = curl_init($this->logSlackWebhook );
        curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($c, CURLOPT_POST, true);
        curl_setopt($c, CURLOPT_POSTFIELDS, $message);
        curl_setopt($c, CURLOPT_RETURNTRANSFER, TRUE);
        $ouput = curl_exec($c);
        curl_close($c);
    }

    /**
     * Clear the log file
     */
    public function clearLogfile() {
        if ( $this->logfileHandle ) {
            @ftruncate( $this->logfileHandle, 0 );
        }
    }

    /**
     * Get content of logfile
     *
     * @return string
     */
    public function getContent() {
        if ( $this->logfileHandle && 0 < filesize( $this->logfile ) ) {
            @fseek( $this->logfileHandle, 0 );
            return @fread( $this->logfileHandle, filesize( $this->logfile  ) );
        } else {
            return '';
        }
    }
} 