<?php

/**
 * Configuration
 *
 * For more info about constants please @see http://php.net/manual/en/function.define.php
 * If you want to know why we use "define" instead of "const" @see http://stackoverflow.com/q/2447791/1114320
 */
/**
 * Configuration for: Error reporting
 * Useful to show every little problem during development, but only show hard errors in production
 */
error_reporting(E_ALL);
ini_set("display_errors", 1);

/**
 * Configuration for: Project URL
 * Put your URL here, for local development "127.0.0.1" or "localhost" (plus sub-folder) is fine
 */
define('URL_SITIO', 'http://localhost/mvc/');
define('URL_WEBSERVICE', 'http://localhost/comunicaciones/');

/**
 * Configuration for: Database
 * This is the place where you define your database credentials, database type etc.
 */

define('APP_DIR', dirname(dirname(__FILE__).'../').'/');


define("LOG_FILE", 'Log/ERROR_SQL_' . date("d-m-Y") . '.log');

define("SAVE_SQL_FILE", false);

define('RANGE_PPC_1', (2*3600)); // green 0-2horas
define('RANGE_PPC_2', (12*3600)); // yellow 2-12horas
// + red

define('RANGE_PPC_DATA_1', (2*3600)); // green 0-2horas
define('RANGE_PPC_DATA_2', (3*3600)); // yellow 2-3horas
// + red

define('RANGE_PENDIENTES_1', (1)); // green 0-2horas
define('RANGE_PENDIENTES_2', (2)); // yellow 2-3horas

define('FLAG_DATE_LOG', (140*24*3600)); // 14 días

set_error_handler(array('MyException', 'errorHandler'), E_ALL);


class MyException extends Exception {
  public $file;
  public $line;
  public static function errorHandler($errno, $errstr, $errfile, $errline) {
        $e = new self();
        $e->message = $errstr;
        $e->code = $errno;
        $e->file = $errfile;
        $e->line = $errline;
        
        $f = 0;
        if(file_exists("Log/")){
            $f = fopen('Log/PHP_ERR_'.date("Y-m-d").'.log','a+');
        }
        else{
            $f = fopen('../Log/PHP_ERR_'.date("Y-m-d").'.log','a+');
        }
        
        fwrite($f,date('Y-m-d H:i:s')."  \n\n ".print_r($e, true)." \r\n");
        fclose($f);
        
        ?>
        <script>
            console.error("ERROR -> <?=$errstr?>");
             var error = {
                'TIPO'     : '<?=$errstr?>',
                'ARCHIVO'  : '<?=$errfile?>',
                'LINEA'    : '<?=$errline?>',
                'FECHA'    : '<?=date('d-m-Y H:i:s')?>'
                }
            }
            console.dir(error);
        </script>
        <?php

    throw $e;
  }
}
