<?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 );
  $base_path = 'autoload/' . implode( '/' , $q ) . '/';
  $f = $base_path . $c . '.php';

  if ( !file_exists( $f ) )
    $f = $base_path . 'class.' . $c . '.php';

  if ( file_exists( $f ) )
    require_once( $f );
}
spl_autoload_register( '__autoload_my_classes' );
date_default_timezone_set( 'Europe/Warsaw' );

require_once 'config.php';
require_once 'libraries/medoo/medoo.php';
require_once 'libraries/grid/config.php';
require_once 'libraries/rb.php';

session_start();

if ( !isset( $_SESSION['check'] ) )
{
 	session_regenerate_id();
	$_SESSION['check']  = true;
	$_SESSION['ip']     = $_SERVER['REMOTE_ADDR'];
}

if ( $_SESSION['ip'] !== $_SERVER['REMOTE_ADDR'] )
{
  session_destroy();
  header( 'Location: /' );
  exit;
}

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

\R::setup( 'mysql:host=' . $database['host'] . ';dbname=' . $database['name'], $database['user'], $database['password'] );
\R::ext( 'xdispense', function( $type ){
        return R::getRedBean() -> dispense( $type );
    });

//$xml = new XMLReader();
//$xml -> open( 'dolnoslaskie1.xml' );
//while($xml->read() && $xml->name != 'InformacjaOWpisie')
//{
//	;
//}
//while($xml->name == 'InformacjaOWpisie')
//{
//	$element = new SimpleXMLElement($xml->readOuterXML());
//  var_dump( $element );
//	$xml -> next( 'InformacjaOWpisie' );
//	unset( $element );
//}


//var_dump( $reader -> IdentyfikatorWpisu );
    /*
$countIx = 0;

$xml = new XMLReader();
$xml->open( 'calosc_ExtendedAddress_zachodniopomorskie.xml' );

while($xml->read() && $xml->name != 'InformacjaOWpisie')
{
	;
}

while($xml->name == 'InformacjaOWpisie')
{
	$element = new SimpleXMLElement($xml->readOuterXML());

  $file_name = 'zachodniopomorskie-' . ++$i . '.xml';
  $md5 = md5( $file_name );
  $dir = 'zachodniopomorskie/' . $md5{1} . '/' . $md5{2} . '/' . $md5{3} . '/';

  if ( !is_dir( $dir ) )
    mkdir( $dir , 0755 , true );

  $fileout = $dir  . $file_name;

  $fh = fopen( $fileout, 'w') or die ( "can't open file $fileout" );
  fwrite( $fh, $element -> asXML() );
  fclose( $fh );

	$countIx++;

	$xml -> next( 'InformacjaOWpisie' );
	unset( $element );
}

$xml->close();

echo $countIx;

/**
 * Returns memory usage from /proc<PID>/status in bytes.
 *
 * @return int|bool sum of VmRSS and VmSwap in bytes. On error returns false.
 */
/*
function memory_get_process_usage()
{
	$status = file_get_contents('/proc/' . getmypid() . '/status');

	$matchArr = array();
	preg_match_all('~^(VmRSS|VmSwap):\s*([0-9]+).*$~im', $status, $matchArr);

	if(!isset($matchArr[2][0]) || !isset($matchArr[2][1]))
	{
		return false;
	}

	return intval($matchArr[2][0]) + intval($matchArr[2][1]);
}
*/
// $lines = file( 'krdpgotowe.csv', FILE_IGNORE_NEW_LINES );
// foreach ( $lines as $key => $value)
// {
//   $i++;

//   $value = substr( $value, 1, strlen( $value ) );
//   $value = substr( $value, 0, strlen( $value ) - 2 );
//   $value = str_replace( '""', '"', $value );
//   $value = str_replace( "''", "", $value );

//   $csv[$key] = str_getcsv( $value, ",", "\"" );
// }

// foreach ( $csv as $firm )
// {
//   if ( \S::email_check( $firm[5] ) || $firm[4] )
//   {
//     $data[] = $firm;
//   }
// }

// $file = fopen("krdp-przefiltrowane.csv","w");

// foreach ( $data as $line ) {
//   fputcsv($file, $line);
// }

// fclose($file);
?>
