<?
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';
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' );

$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'
                ] );

if ( \S::get( 'action' ) == 'majestic_domain_save' )
{
  if ( \S::get( 'okazwloka' ) == true )
  {
    $results = str_replace( 'OK|', '', \S::get( 'result' ) );
    $results = json_decode( $results, true );
    $results = array_pop( $results );

    $feb  = $results['feb'];
    $tf   = $results['tf'];
    $cf   = $results['cf'];
    $rd   = $results['frd'];
  }
  else
  {
    $results = json_decode( \S::get( 'result' ), true );

    $feb  = $results['ExtBackLinks'];
    $tf   = $results['TrustFlow'];
    $cf   = $results['CitationFlow'];
    $rd   = $results['RefDomains'];
  }

  $mdb -> update( 'pro_rr_sites', [ 'majestic_date' => date( 'Y-m-d H:i:s' ) ], [ 'id' => \S::get( 'domain_id' ) ] );
  $mdb -> insert( 'pro_rr_sites_majestic', [
                      'site_id' => \S::get( 'domain_id' ),
                      'feb'     => $feb,
                      'cf'      => $cf,
                      'tf'      => $tf,
                      'rd'      => $rd,
                      'date'    => date( 'Y-m-d' )
                    ] );
  echo json_encode( [ 'result' => 'ok' ] );
  exit;
}

if ( \S::get( 'action' ) == 'majestic_domain_check' )
{
  $results = $mdb -> query( 'SELECT '
                                . 'id, url '
                              . 'FROM '
                                . 'pro_rr_sites '
                              . 'WHERE '
                                  . '( DATE_ADD( majestic_date, INTERVAL ' . $config['site']['majestic_interval'] . ' DAY ) <= \'' . date( 'Y-m-d' ) . '\' OR majestic_date IS NULL ) '
                                . 'AND '
                                  . 'majestic = 1 '
                              . 'ORDER BY '
                                . 'majestic_date ASC '
                              . 'LIMIT 1' ) -> fetchAll( \PDO::FETCH_ASSOC );
  if ( is_array( $results ) and !empty( $results ) )
  {
    $results[0]['url'] = str_replace( 'www.', '', $results[0]['url'] );
    $results[0]['result'] = 'ok';
    echo json_encode( $results[0] );
  }
  else
    echo json_encode( [ 'result' => 'bad' ] );
  exit;
}

if ( \S::get( 'action' ) == 'semstorm_domain_save' )
{
  $results = json_decode( \S::get( 'result' ), true );

  foreach ( $results['results'][\S::get( 'url' )] as $key => $val )
  {
    if ( $key <= 3 )
      $top3 += $val;

    if ( $key <= 10 )
      $top10 += $val;

    if ( $key <= 50 )
      $top50 += $val;
  }

  $mdb -> update( 'pro_rr_sites', [ 'semstorm_date' => date( 'Y-m-d H:i:s' ) ], [ 'id' => \S::get( 'domain_id' ) ] );
  $mdb -> insert( 'pro_rr_sites_semstorm', [
                      'site_id' => \S::get( 'domain_id' ),
                      'top3' => (int)$top3,
                      'top10' => (int)$top10,
                      'top50' => (int)$top50,
                      'date' => date( 'Y-m-d' )
                    ] );
  echo json_encode( [ 'result' => 'ok' ] );
  exit;
}

if ( \S::get( 'action' ) == 'semstorm_domain_check' )
{
  $results = $mdb -> query( 'SELECT '
                                . 'id, url '
                              . 'FROM '
                                . 'pro_rr_sites '
                              . 'WHERE '
                                  . '( DATE_ADD( semstorm_date, INTERVAL ' . $config['site']['semstorm_interval'] . ' DAY ) <= \'' . date( 'Y-m-d' ) . '\' OR semstorm_date IS NULL ) '
                                . 'AND '
                                  . 'semstorm = 1 '
                              . 'ORDER BY '
                                . 'semstorm_date ASC '
                              . 'LIMIT 1' ) -> fetchAll( \PDO::FETCH_ASSOC );
  if ( is_array( $results ) and !empty( $results ) )
  {
    $results[0]['url'] = str_replace( 'www.', '', $results[0]['url'] );
    $results[0]['result'] = 'ok';
    echo json_encode( $results[0] );
  }
  else
    echo json_encode( [ 'result' => 'bad' ] );
  exit;
}

echo json_encode( [ 'result' => 'bad' ] );
