<?
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( 'd4s-id' ) )
  $mdb -> update( 'pro_rr_phrases', [ 'ds_ready' => 1 ], [ 'ds_id' => \S::get( 'd4s-id' ) ] );

// function _in_logit_POST( $id_message, $data ) {
// 	@file_put_contents(__DIR__."/postback_url_example.log", PHP_EOL.date("Y-m-d H:i:s").": ".$id_message.PHP_EOL."---------".PHP_EOL.print_r($data, true).PHP_EOL."---------", FILE_APPEND);
// }

// $post_data_in = file_get_contents( 'php://input' );
// if ( !empty( $post_data_in ) )
// {
// 	$post_arr = json_decode( $post_data_in, true );
// 	if ( ( !empty( $post_arr ) ) AND ( $post_arr["status"] == "ok" ) )
//   {
// 		foreach( $post_arr["results"]["organic"] as $tasks_row )
//     {
//       $phrase_id = $mdb -> get( 'pro_rr_phrases', 'id', [ 'ds_id' => $tasks_row['task_id'] ] );

//       if ( $mdb -> count( 'pro_rr_phrases_positions', [ 'AND' => [ 'phrase_id' => $phrase_id, 'date' => date( 'Y-m-d' ) ] ] ) )
//       {
//         $mdb -> update( 'pro_rr_phrases_positions', [
//                           'position' => (int)$tasks_row['result_position'],
//                         ], [
//                           'AND' => [
//                             'phrase_id' => $phrase_id,
//                             'date' => date( 'Y-m-d' )
//                           ]
//                         ] );
//       }
//       else
//       {
//         $mdb -> insert( 'pro_rr_phrases_positions', [
//                         'phrase_id' => $phrase_id,
//                         'position'  => (int)$tasks_row['result_position'],
//                         'url'       => $tasks_row['result_url'],
//                         'map'       => 0,
//                         'date'      => date( 'Y-m-d' )
//                       ] );

//         $last_id = $mdb -> get( 'phrase_positions_statistic', 'id', [ 'phrase_id' => $phrase_id, 'ORDER' => [ 'date' => 'DESC' ] ] );

//         if ( $last_id )
//           $mdb -> query( 'DELETE FROM phrase_positions_statistic WHERE phrase_id = ' . $phrase_id . ' AND id NOT IN ( ' . $last_id . ' )' );

//         $mdb -> insert( 'phrase_positions_statistic', [
//                           'phrase_id' => $phrase_id,
//                           'position' => (int)$tasks_row['result_position'],
//                           'date' => date( 'Y-m-d' )
//                         ] );
//       }
//       $mdb -> update( 'pro_rr_phrases', [ 'last_checked' => date( 'Y-m-d' ), 'filled_missing_positions' => 0, 'ds_id' => null ], [ 'id' => $phrase_id ] );
// 		}
// 		echo "ok";
// 	} else {
// 		echo "error";
// 	}
// } else {
// 	echo "empty POST";
// }
