<?php

/*
Copyright (C) sensmotdire  GnunuX http://sensmotdire.gnunux.info/dist/

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
	require "lib.php";

	if (isset($_GET['firefox']))
		$firefox="yes";
	$limit=10;
	if (isset($_GET['limit']))
		$limit=min($limit,$_GET['limit']);
	$input_orig=$word=stripword( $_GET['input'] );
	$input_array=explode(" ",$input_orig);
	$num=count($input_array)-1;
	if ( $input_array[$num] == "" ) 
		$num=count($input_array)-2;

	$input=$input_array[$num];
	$input_array[$num]="";
	$old=implode(" ",$input_array);
	if ($input[0] == "")
		exit;
	if ( checkword($input) ) {
		$all_allow_letter="[a-zéèêâàïîôç]";
		$input=mb_eregi_replace("e", "(e|é|è|ê)", $input);
		$input=mb_eregi_replace("a", "(a|â|à)", $input);
		$input=mb_eregi_replace("i", "(i|ï|î)", $input);
		$input=mb_eregi_replace("o", "(o|ô)", $input);
		$input=mb_eregi_replace("c", "(c|ç)", $input);
		$input=mb_eregi_replace("\*", "$all_allow_letter+", $input);
		$input=mb_eregi_replace("\?", "$all_allow_letter", $input);
		$search = new dataBase ($config_dsn);
		$search->execRequest("SELECT infinitif FROM `verbsfr` WHERE `infinitif` REGEXP \"^" . $input . "\" LIMIT 0 , $limit");
		header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
		header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
		header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
		header ("Pragma: no-cache"); // HTTP/1.0
		header("Content-Type: application/json; charset=UTF-8");

		if (isset($firefox))
			echo "[\"$input_orig\", [";
		else
			echo "{ \"results\": [";
		$id=1;
		$arr=array();
		while ($row = $search->nextResult())
		{
			if (isset($firefox))
				$arr[]= " \"" . $old . $row['infinitif'] . "\" ";
			else
				$arr[]= "{ \"id\": \"$id\", \"value\": \"" . $old . $row['infinitif'] . "\", \"info\": \"\" }";
			$id++;
		}
		echo implode(", ", $arr);
		$search->close();
		if (isset($firefox))
			echo "] ]";
		else
			echo "] }";
	}
?>
