<?
error_reporting( E_ALL ^ E_NOTICE ^ E_STRICT ^ E_WARNING ^ E_DEPRECATED );
function __autoload_my_classes( $classname )
{
	$q = explode( '\\' , $classname );
	$c = array_pop( $q );
	$f = 'autoload/' . implode( '/' , $q ) . '/class.' . $c . '.php';
	if ( $c == 'Savant3' )
	{
		require_once 'autoload/Savant3.php';
		return true;
	}
	if ( file_exists( $f ) )
		require_once( $f );
}

spl_autoload_register( '__autoload_my_classes' );
mb_internal_encoding( "UTF-8" );

define( 'OPD_DIR' , 'autoload/' );
require_once OPD_DIR . 'opd.class.php';
require_once 'config.php';
require_once 'libraries/medoo.php';
require_once 'libraries/grid/config.php';
require_once 'resources/phpmailer/class.phpmailer.php';
include 'autoload/RestClient3.php';
date_default_timezone_set('Europe/Warsaw');
header( 'Content-Type: text/html; charset=utf-8' );
session_start();

$db = opdClass::create(
                          array(
                              'dsn' => 'mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['name'],
                              'user' => $config['db']['user'] ,
                              'password' => $config['db']['password'] ,
                              'cache' => 'temp/' ,
                              'debugConsole' => $config['db']['debug']
                              )
                        );
$db -> query( 'SET NAMES utf8' );

$db -> query( 'UPDATE pro_rr_phrases SET ds_id = NULL WHERE ds_date != \'' . date( 'Y-m-d' ) . '\'' );

$mdb = new medoo( [
                    'database_type' => 'mysql',
                    'database_name' => $config['db']['name'],
                    'server'        => $config['db']['host'],
                    'username'      => $config['db']['user'],
                    'password'      => $config['db']['password'],
                    'charset'       => 'utf8'
                ] );

$mdb -> delete( 'pro_proxy', [ 'banned_count[>]' => 10 ] );
$mdb -> update( 'pro_proxy', [ 'is_banned' => 0, 'banned_date' => null ], [ 'AND' => [ 'is_banned' => 1, 'banned_date[<]' => date( "Y-m-d H:i:s" ) ] ] );

// $response = \Cron::check_phrases_positions_dfs();
// if ( $response['status'] == 'ok' )
// {
//   echo json_encode( $response );
//   exit;
// }

if ( date( 'G' ) > 1 )
{
  $response = \Cron::post_phrases_positions_dfs3();
  if ( $response['status'] == 'ok' )
  {
    echo json_encode( $response );
    exit;
  }
}

if ( date( 'G' ) > 1 )
{
  $response = \Cron::get_phrases_positions_dfs3();
  if ( $response['status'] == 'ok' )
  {
    echo json_encode( $response );
    exit;
  }
}

/* uzupełniam brakujące pozycje */
$response = \Cron::fill_missing_positions();
if ( $response['status'] == 'ok' )
{
  echo json_encode( $response );
  exit;
}

/* archiwizowanie starych pozycji */
$response = \Cron::archive_positions();
if ( $response['status'] == 'ok' )
{
  echo json_encode( $response );
  exit;
}

/* opróżnianie archiwum */
$response = \Cron::archive_empty();
if ( $response['status'] == 'ok' )
{
  echo json_encode( $response );
  exit;
}

echo json_encode( [ 'status' => 'empty' ] );