<?php
// force to https
/*
if (!$_SERVER['HTTPS']) {
	if ($_GET['w']) {
		//header("Refresh: 0; url=https://wordfeud.taaltik.nl?w=".$_GET['w']);
		header("Refresh: 0; url=http://taaltik-wordfeud.keesing.com?w=".$_GET['w']);
	} else {
		//header("Refresh: 0; url=https://wordfeud.taaltik.nl");
		header("Refresh: 0; url=http://taaltik-wordfeud.keesing.com");
	}
}
*/

global $word2;
global $toegestaan;
global $exactok;

$blocked=explode("/","216.244.66.233/66.249.92.95/216.244.66.247/66.249.89.4/66.249.90.183");
if (isset($blocked[$_SERVER['REMOTE_ADDR']])) {die();};

error_reporting(0);

$id=$_COOKIE['wordfeudnr'];
$hacking=false;
foreach($_POST as $p1 => $p2) {
  $h=strip_tags($p2);
  if ($h!=$p2) {
    $hacking=true;
  } 
}

foreach($_GET as $p1 => $p2) {
  $h=strip_tags($p2);
  //echo "<br>".$p1."=".$p2;
  if ($h!=$p2) {
    $hacking=true;
  } 
}
if ($hacking) {
  sleep(60);
  die();
}

if (!$id) {
  // create id
  $id=sha1(microtime());
  setcookie('wordfeudnr',$id,time() + (86400 * 31)); 
  header("Refresh: 0; url=http://taaltik-wordfeud.keesing.com");
}
if (!$id) {die();} // does not accept cookies

// constants
$maxfreeperday=10;
// sanitize input

// log breedten van vensters
if (isset($_COOKIE['xy'])) {
  //$fc=fopen('./writable/xy_'.$_COOKIE['xy'].'.dat','ab+');
  //fwrite($fc,'+');
  //fclose($fc);
  $e=explode('_',$_COOKIE['xy']);
  $xsize=$e[0];
  $ysize=$e[1];
  //echo $xsize.'|'.$ysize;
}

if (isset($_POST['exact'])) {$exact='J';} 
if (isset($_POST['grof'])) {$exact='N';} 
if (!$exact) {$exact=$_GET['xct'];}

if (!$exact) {$exact='N';}

// database can be connected now it is useful and safe;
require_once('lib/lib_alg.php');
require_once('lib/lib_lang.php');
//require_once('lib/lib_db.php');

$word=trim($_POST['word']);
if (!trim($_POST['word'])) {
  $word=$_GET['w'];
}
$word;
// set cookie of last searched word (to prevent to much logging it)
setcookie('wordfeudverklaard_lastword', $word , time()+999999 , null ,'wordfeud.taaltik.nl' );
// check if an id is there, if not, apply delay
// lengthen duration of id cookie
setcookie('wordfeudnr',$id,time() + (86400 * 31)); 

// get and set items done today
$today=1+$_COOKIE['wordfeudverklaard_4'];
if ($word) {
  setcookie('wordfeudverklaard_4',$today,time() + 24*60*60 );
}

$hr=mb_substr(date('Hi'),0,3);
$logid=$hr.'_'.$_SERVER['REMOTE_ADDR'].'_'.$_COOKIE['wordfeudnr'];
$fn='./writable/prevent_overuse/'.$logid.'.dat';
if (filesize($fn)>=50) {
	// leeching
	echo "There are many request from your (provider's) internet address. Please try again in 15 minutes.<BR>
	Er wordt veel opgevraagd van hetzelfde adres. Doe het rustiger aan s.v.p. Probeer het over een kwartier nog eens.";
	//echo "<BR>".$_SERVER['REMOTE_ADDR'];
	// schijf weg als geblokkeerd
	$fl=fopen('./writable/blockedcount/'.$_SERVER['REMOTE_ADDR'].'_'.$_COOKIE['wordfeudnr'].'.dat','ab+');
	fwrite($fl,'+');
	fclose($fl);
	die();
} else {
	// tel er een bij
	$fl=fopen($fn,'ab+');
	fwrite($fl,'+');
	fclose($fl);
	
	$fl=fopen('./writable/wordcount/wordcount.txt','ab+');
	$w=mb_strtolower($_POST['word']);
	fwrite($fl,$w."\n");
	fclose($fl);
}

$hr=mb_substr(date('Hi', time()-600),0,3);
$files=glob('./writable/prevent_overuse/'.$hr.'_*');
foreach ($files as $file) {unlink($file);}

function geefweer($functie) {
  global $db;
  global $maxfreeperday;
  global $today;
  global $word;
  global $examples_listed;
  global $examples_wrong;
  
  echo '<div id="'.$functie.'" class="divaan"><div id="'.$functie.'_kop" class="kopaan"><A id="'.$functie.'"><h1 class="h1">'.$functie.'</h1></A></div>';
  if (file_exists($functie.'.php')) {
    include($functie.'.php');
  }
  echo '</div>';
}

?>
<!DOCTYPE html>
<html>
<head>
<title>WORDFEUD VERKLAARD - De achtergronden van de woordenlijst van Wordfeud.</title>
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<meta name="keywords" content="Wordfeud, woorden, Nederlands, scrabble, woordenlijst, wordfeudwoorden">
<meta name="description" content="De woordenlijst van Wordfeud wordt hier uitgelegd. We geven aan waarom woorden er wel, of waarom ze er niet in zitten. Je kunt ook wijzigingen voorstellen.">
<meta name="author" content="TaalTik">
<!-- <link rel="stylesheet" href="css/stijl.css" type="text/css" media="all" /> is te langzaam ? -->

<link rel="apple-touch-icon-precomposed" sizes="57x57" href="apple-touch-icon-57x57.png" />
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="apple-touch-icon-114x114.png" />
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="apple-touch-icon-72x72.png" />
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="apple-touch-icon-144x144.png" />
<link rel="apple-touch-icon-precomposed" sizes="60x60" href="apple-touch-icon-60x60.png" />
<link rel="apple-touch-icon-precomposed" sizes="120x120" href="apple-touch-icon-120x120.png" />
<link rel="apple-touch-icon-precomposed" sizes="76x76" href="apple-touch-icon-76x76.png" />
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="apple-touch-icon-152x152.png" />
<link rel="icon" type="image/png" href="favicon-196x196.png" sizes="196x196" />
<link rel="icon" type="image/png" href="favicon-96x96.png" sizes="96x96" />
<link rel="icon" type="image/png" href="favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="favicon-16x16.png" sizes="16x16" />
<link rel="icon" type="image/png" href="favicon-128.png" sizes="128x128" />
<meta name="application-name" content="Woorden van Wordfeud"/>
<meta name="msapplication-TileColor" content="#FFFFFF" />
<meta name="msapplication-TileImage" content="mstile-144x144.png" />
<meta name="msapplication-square70x70logo" content="mstile-70x70.png" />
<meta name="msapplication-square150x150logo" content="mstile-150x150.png" />
<meta name="msapplication-wide310x150logo" content="mstile-310x150.png" />
<meta name="msapplication-square310x310logo" content="mstile-310x310.png" />

<?php include 'css/stijl.php'; ?>

<script type="text/javascript" language="javascript">
  if (window.innerWidth<640) {
    if (window.innerWidth>320) {
      var div=window.innerWidth;
    } else {
      var div=320;
    }
  }
  var div=320;  
  var cols=Math.floor(window.innerWidth/div);
  document.cookie="xy="+window.innerWidth+"_"+window.innerHeight;
  //if (cols>3) {cols=4;}
  //if (cols<3) {cols=2;}
  if (cols<=1) {
    cols=1;
  }
  if (cols==2) {cols=4;}
  if (cols>3) {cols=4;}
  
  var used=div*cols;
  var remains=window.innerWidth-used;
  var marg=Math.floor(remains/2);
  if (marg <0) { marg=0;}
  document.write("<body style=\"margin-left:" + marg + "px\">");   
  
  //document.write(marg);
  //document.getElementById("body").setAttribute("style", "width:" + used + "px"+";margin-left:" + marg + "px");   
  //document.getElementById("body").setAttribute("style", "margin-left:" + marg + "px");  
</script>

</head>

<?php

	//<!-- 4 containers -->
	//<!-- container 1 : zoeken en uitsluitsel -->
	echo '<div class="container">';
	if ($word) {
		geefweer('Wordfeud verklaard');
	} else {
		include 'koptekst.php' ;
		geefweer('dit doen we ook');     
	}
	echo '</div>';
    //<!-- container 2 : voorbeelden -->
    /*
   echo '<div class="container">';
	if ($word) {
		if ($_COOKIE['wordfeudnr']) {
			geefweer('voorbeelden');
		}
	}
	echo '</div>';
*/
  //<!-- container 3 : suggesties -->
	echo '<div class="container">';
	//geefweer('gesuggereerd'); 
	if (($word)||($_POST['stuur'])) {
		geefweer('suggesties');
	} else {
		geefweer('wat er in de lijst komt');
	}
	echo '</div>';

  ?>
    <!-- You should make sure that the file cookiechoices.js is available
    and accessible from the root directory of your site.  -->
    <script src="/cookiechoices.js"></script>
    <script>
      document.addEventListener('DOMContentLoaded', function(event) {
	cookieChoices.showCookieConsentBar('Deze site werkt dankzij eigen cookies, en zet geen cookies van anderen.',
	  'Oké', '', 'https://www.taaltik.nl');
      });
    </script>  
</body>
</html>

