<?php
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/xajax/xajax_core/xajax.inc.php';
date_default_timezone_set('Europe/Warsaw');
  
function sortByOption( $a, $b ) {
  return strcmp( $a['date'], $b['date']);
}

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

$cache  = new \FileCache;

if ( \S::get( 'action' ) == 'site_tf' )
{
  $output["name"] = "Trust Flow";
  
  $results = $mdb -> select( 'pro_rr_sites_majestic', [ 'tf', 'date' ], [ 'site_id' => \S::get( 'site_id' ), 'ORDER' => [ 'date' => 'ASC' ] ] );
  if ( is_array( $results ) ) foreach ( $results as $row )
    if ( $row['tf'] !== null )
      $data[] = [ gmmktime(0, 0, 0, date( 'n', strtotime( $row['date'] ) ), date( 'j', strtotime( $row['date'] ) ), date( 'Y', strtotime( $row['date'] ) ) ) * 1000, $row['tf'] ];
  
  $output['data'] = $data;
  
  echo "[" . json_encode( $output, JSON_NUMERIC_CHECK ) . "]";
  exit;
}

if ( \S::get( 'action' ) == 'site_cf' )
{
  $output["name"] = "Citation Flow";
  
  $results = $mdb -> select( 'pro_rr_sites_majestic', [ 'cf', 'date' ], [ 'site_id' => \S::get( 'site_id' ), 'ORDER' => [ 'date' => 'ASC' ] ] );
  if ( is_array( $results ) ) foreach ( $results as $row )
    $data[] = [ gmmktime(0, 0, 0, date( 'n', strtotime( $row['date'] ) ), date( 'j', strtotime( $row['date'] ) ), date( 'Y', strtotime( $row['date'] ) ) ) * 1000, $row['cf'] ];
  
  $output['data'] = $data;
  
  echo "[" . json_encode( $output, JSON_NUMERIC_CHECK ) . "]";
  exit;
}

if ( \S::get( 'action' ) == 'site_rd' )
{
  $output["name"] = "Referring Domains";
  
  $results = $mdb -> select( 'pro_rr_sites_majestic', [ 'rd', 'date' ], [ 'site_id' => \S::get( 'site_id' ), 'ORDER' => [ 'date' => 'ASC' ] ] );
  if ( is_array( $results ) ) foreach ( $results as $row )
    $data[] = [ gmmktime(0, 0, 0, date( 'n', strtotime( $row['date'] ) ), date( 'j', strtotime( $row['date'] ) ), date( 'Y', strtotime( $row['date'] ) ) ) * 1000, $row['rd'] ];
  
  $output['data'] = $data;
  
  echo "[" . json_encode( $output, JSON_NUMERIC_CHECK ) . "]";
  exit;
}

if ( \S::get( 'action' ) == 'site_feb' )
{
  $output["name"] = "Majestic Backlinks";
  
  $results = $mdb -> select( 'pro_rr_sites_majestic', [ 'feb', 'date' ], [ 'site_id' => \S::get( 'site_id' ), 'ORDER' => [ 'date' => 'ASC' ] ] );
  if ( is_array( $results ) ) foreach ( $results as $row )
    $data[] = [ gmmktime(0, 0, 0, date( 'n', strtotime( $row['date'] ) ), date( 'j', strtotime( $row['date'] ) ), date( 'Y', strtotime( $row['date'] ) ) ) * 1000, $row['feb'] ];
  
  $output['data'] = $data;
  
  echo "[" . json_encode( $output, JSON_NUMERIC_CHECK ) . "]";
  exit;
}

if ( \S::get( 'action' ) == 'site_top3' )
{
  $output["name"] = "Semstorm Top3";
  
  $results = $mdb -> select( 'pro_rr_sites_semstorm', [ 'top3', 'date' ], [ 'site_id' => \S::get( 'site_id' ), 'ORDER' => [ 'date' => 'ASC' ] ] );
  if ( is_array( $results ) ) foreach ( $results as $row )
    $data[] = [ gmmktime(0, 0, 0, date( 'n', strtotime( $row['date'] ) ), date( 'j', strtotime( $row['date'] ) ), date( 'Y', strtotime( $row['date'] ) ) ) * 1000, $row['top3'] ];
  
  $output['data'] = $data;
  
  echo "[" . json_encode( $output, JSON_NUMERIC_CHECK ) . "]";
  exit;
}

if ( \S::get( 'action' ) == 'site_top10' )
{
  $output["name"] = "Semstorm Top10";
  
  $results = $mdb -> select( 'pro_rr_sites_semstorm', [ 'top10', 'date' ], [ 'site_id' => \S::get( 'site_id' ), 'ORDER' => [ 'date' => 'ASC' ] ] );
  if ( is_array( $results ) ) foreach ( $results as $row )
    $data[] = [ gmmktime(0, 0, 0, date( 'n', strtotime( $row['date'] ) ), date( 'j', strtotime( $row['date'] ) ), date( 'Y', strtotime( $row['date'] ) ) ) * 1000, $row['top10'] ];
  
  $output['data'] = $data;
  
  echo "[" . json_encode( $output, JSON_NUMERIC_CHECK ) . "]";
  exit;
}

if ( \S::get( 'action' ) == 'site_top50' )
{
  $output["name"] = "Semstorm Top50";
  
  $results = $mdb -> select( 'pro_rr_sites_semstorm', [ 'top50', 'date' ], [ 'site_id' => \S::get( 'site_id' ), 'ORDER' => [ 'date' => 'ASC' ] ] );
  if ( is_array( $results ) ) foreach ( $results as $row )
    $data[] = [ gmmktime(0, 0, 0, date( 'n', strtotime( $row['date'] ) ), date( 'j', strtotime( $row['date'] ) ), date( 'Y', strtotime( $row['date'] ) ) ) * 1000, $row['top50'] ];
  
  $output['data'] = $data;
  
  echo "[" . json_encode( $output, JSON_NUMERIC_CHECK ) . "]";
  exit;
}