<?php

if(isset($_POST['q'])){
	require_once('ini.class.php');
	$q = $_POST['q'];
	if($q == 'a'){
		
		if(isset($_POST['sp'])){
			$sp = true;
		}
		else{
			$sp = false;
		}

		/*

		Debugging

		*/

		//general

		//var_dump($_POST['g_name']);
		//var_dump($_POST['g_descr']);
		//var_dump($_POST['g_ispb']);

		//crawling

		//var_dump($_POST['c_http']);
		//var_dump($_POST['c_robots']);
		//var_dump($_POST['c_sitemap']);
		//var_dump($_POST['c_label']);
		//var_dump($_POST['c_label_c']);

		//head:title

		//var_dump($_POST['h_t_show']);
		//var_dump($_POST['h_t_length']);
		//var_dump($_POST['h_t_length_d']);
		//var_dump($_POST['h_t_length_c']);
		//var_dump($_POST['h_t_stopwords']);
		//var_dump($_POST['h_t_kwin']);
		//var_dump($_POST['h_t_rel']);
		//var_dump($_POST['h_t_rel_d']);
		//var_dump($_POST['h_t_rel_c']);
		//var_dump($_POST['h_t_label']);
		//var_dump($_POST['h_t_label_c']);

		//head:kw

		//var_dump($_POST['h_k_show']);
		//var_dump($_POST['h_k_count']);
		//var_dump($_POST['h_k_count_d']);
		//var_dump($_POST['h_k_count_c']);
		//var_dump($_POST['h_k_rel']);
		//var_dump($_POST['h__k_rel_d']);
		//var_dump($_POST['h__k_rel_c']);
		//var_dump($_POST['h_k_label']);
		//var_dump($_POST['h_k_label_c']);

		//head:desc

		//var_dump($_POST['h_d_show']);
		//var_dump($_POST['h_d_length']);
		//var_dump($_POST['h_d_length_d']);
		//var_dump($_POST['h_d_length_c']);
		//var_dump($_POST['h_d_kwin']);
		//var_dump($_POST['h_d_rel']);
		//var_dump($_POST['h_d_rel_d']);
		//var_dump($_POST['h_d_rel_c']);
		//var_dump($_POST['h_d_label']);
		//var_dump($_POST['h_d_label_c']);


		//content

		//var_dump($_POST['ct_lang']);
		//var_dump($_POST['ct_chars']);
		//var_dump($_POST['ct_chars_d']);
		//var_dump($_POST['ct_chars_c']);
		//var_dump($_POST['ct_words']);
		//var_dump($_POST['ct_words_d']);
		//var_dump($_POST['ct_words_c']);
		//var_dump($_POST['ct_kwin']);
		//var_dump($_POST['ct_rel']);
		//var_dump($_POST['ct_rel_d']);
		//var_dump($_POST['ct_rel_c']);
		//var_dump($_POST['ct_kwini']);
		//var_dump($_POST['ct_kwinb']);
		//var_dump($_POST['ct_headings']);
		//var_dump($_POST['ct_iframes']);
		//var_dump($_POST['ct_label']);
		//var_dump($_POST['ct_label_c']);

		//url structure

		//var_dump($_POST['ul_show']);
		//var_dump($_POST['ul_kwin']);
		//var_dump($_POST['ul_length']);
		//var_dump($_POST['ul_length_d']);
		//var_dump($_POST['ul_length_c']);
		//var_dump($_POST['ul_sub']);
		//var_dump($_POST['ul_label']);
		//var_dump($_POST['ul_label_c']);

		//link

		//var_dump($_POST['l_ilinks']);
		//var_dump($_POST['l_ilinks_d']);
		//var_dump($_POST['l_ilinks_c']);
		//var_dump($_POST['l_elinks']);
		//var_dump($_POST['l_elinks_d']);
		//var_dump($_POST['l_elinks_c']);
		//var_dump($_POST['l_label']);
		//var_dump($_POST['l_label_c']);

		//user exp

		//var_dump($_POST['u_speed']);
		//var_dump($_POST['u_speed_d']);
		//var_dump($_POST['u_speed_c']);
		//var_dump($_POST['u_gram']);
		//var_dump($_POST['u_gram_d']);
		//var_dump($_POST['u_gram_c']);
		//var_dump($_POST['u_res']);
		//var_dump($_POST['u_fb']);
		//var_dump($_POST['u_label']);
		//var_dump($_POST['u_label_c']);


		/*

		End of Debugging

		*/

		if($sp == true){
			$n = (string)$_POST['fn'];
		}
		else{
			$n = uniqid() . '.ini';
			$h = fopen('../data/profiles/' . $n, 'w');
			fwrite($h, "[Profile Settings]\r\n");
			fclose($h);
		}


		//let`s create a random file to store our profile settings



		$profileIni = new IniParser('../data/profiles/'. $n);

		//general
		
		$profileIni->setValue('g_barvalue', '"'. $_POST['g_barvalue'] . '"');
		$profileIni->setValue('g_name', '"'. str_replace('"','',$_POST['g_name'] ). '"');
		$profileIni->setValue('g_descr', '"'. str_replace('"','',$_POST['g_descr']) .'"');
		$profileIni->setValue('g_ispb', '"'. $_POST['g_ispb'] .'"');

		//crawling

		$profileIni->setValue('c_http', '"'. $_POST['c_http'] .'"');
		$profileIni->setValue('c_robots', '"'. $_POST['c_robots'] .'"');
		$profileIni->setValue('c_sitemap', '"'. $_POST['c_sitemap'] .'"');
		$profileIni->setValue('c_label', '"'. $_POST['c_label'] .'"');
		$profileIni->setValue('c_label_c', "'". str_replace("'",'',$_POST['c_label_c']) ."'");

		//head:title

		$profileIni->setValue('h_t_show', '"'. $_POST['h_t_show'] .'"');
		$profileIni->setValue('h_t_length', '"'. $_POST['h_t_length'] .'"');
		$profileIni->setValue('h_t_length_d', '"'. implode(',',$_POST['h_t_length_d']) .'"');
		$profileIni->setValue('h_t_length_c', '"'. implode(',',$_POST['h_t_length_c']) .'"');
		$profileIni->setValue('h_t_stopwords', '"'. $_POST['h_t_stopwords'] .'"');
		$profileIni->setValue('h_t_kwin', '"'. $_POST['h_t_kwin'] .'"');
		$profileIni->setValue('h_t_rel', '"'. $_POST['h_t_rel'] .'"');
		$profileIni->setValue('h_t_rel_d', '"'. implode(',',$_POST['h_t_rel_d']) .'"');
		$profileIni->setValue('h_t_rel_c', '"'. implode(',',$_POST['h_t_rel_c']) .'"');
		$profileIni->setValue('h_t_label', '"'. $_POST['h_t_label'] .'"');
		$profileIni->setValue('h_t_label_c', "'". str_replace("'",'',$_POST['h_t_label_c']) ."'");

		//head:kw

		$profileIni->setValue('h_k_show', '"'. $_POST['h_k_show'] .'"');
		$profileIni->setValue('h_k_count', '"'. $_POST['h_k_count'] .'"');
		$profileIni->setValue('h_k_count_d', '"'. implode(',',$_POST['h_k_count_d']) .'"');
		$profileIni->setValue('h_k_count_c', '"'. implode(',',$_POST['h_k_count_c']) .'"');
		$profileIni->setValue('h_k_rel', '"'. $_POST['h_k_rel'] .'"');
		$profileIni->setValue('h__k_rel_d', '"'. implode(',',$_POST['h__k_rel_d']) .'"');
		$profileIni->setValue('h__k_rel_c', '"'. implode(',',$_POST['h__k_rel_c']) .'"');
		$profileIni->setValue('h_k_label', '"'. $_POST['h_k_label'] .'"');
		$profileIni->setValue('h_k_label_c', "'". str_replace("'",'',$_POST['h_k_label_c']) ."'");
		
		//head:desc

		$profileIni->setValue('h_d_show', '"'. $_POST['h_d_show'] . '"');
		$profileIni->setValue('h_d_length', '"'. $_POST['h_d_length'] . '"');
		$profileIni->setValue('h_d_length_d', '"'. implode(',',$_POST['h_d_length_d']) . '"');
		$profileIni->setValue('h_d_length_c', '"'. implode(',',$_POST['h_d_length_c']) . '"');
		$profileIni->setValue('h_d_kwin', '"'. $_POST['h_d_kwin'] . '"');
		$profileIni->setValue('h_d_rel', '"'. $_POST['h_d_rel'] . '"');
		$profileIni->setValue('h_d_rel_d', '"'. implode(',',$_POST['h_d_rel_d']) . '"');
		$profileIni->setValue('h_d_rel_c', '"'. implode(',',$_POST['h_d_rel_c']) . '"');
		$profileIni->setValue('h_d_label', '"'. $_POST['h_d_label'] . '"');
		$profileIni->setValue('h_d_label_c', "'". str_replace("'",'',$_POST['h_d_label_c']) ."'");

		//content

		$profileIni->setValue('ct_lang', '"'. $_POST['ct_lang'] .'"');
		$profileIni->setValue('ct_chars', '"'. $_POST['ct_chars'] .'"');
		$profileIni->setValue('ct_chars_d', '"'. implode(',',$_POST['ct_chars_d']) .'"');
		$profileIni->setValue('ct_chars_c', '"'. implode(',',$_POST['ct_chars_c']) .'"');
		$profileIni->setValue('ct_words', '"'. $_POST['ct_words'] .'"');
		$profileIni->setValue('ct_words_d', '"'. implode(',',$_POST['ct_words_d']) .'"');
		$profileIni->setValue('ct_words_c', '"'. implode(',',$_POST['ct_words_c']) .'"');
		$profileIni->setValue('ct_kwin', '"'. $_POST['ct_kwin'] .'"');
		$profileIni->setValue('ct_rel', '"'. $_POST['ct_rel'] .'"');
		$profileIni->setValue('ct_rel_d', '"'. implode(',',$_POST['ct_rel_d']) .'"');
		$profileIni->setValue('ct_rel_c', '"'. implode(',',$_POST['ct_rel_c']) .'"');
		$profileIni->setValue('ct_kwini', '"'. $_POST['ct_kwini'] .'"');
		$profileIni->setValue('ct_kwinb', '"'. $_POST['ct_kwinb'] .'"');
		$profileIni->setValue('ct_headings', '"'. $_POST['ct_headings'] .'"');
		$profileIni->setValue('ct_iframes', '"'. $_POST['ct_iframes'] .'"');
		$profileIni->setValue('ct_label', '"'. $_POST['ct_label'] .'"');
		$profileIni->setValue('ct_label_c', "'". str_replace("'",'',$_POST['ct_label_c']) ."'");

		//url structure

		$profileIni->setValue('ul_show', '"'. $_POST['ul_show'] .'"');
		$profileIni->setValue('ul_kwin', '"'. $_POST['ul_kwin'] .'"');
		$profileIni->setValue('ul_length', '"'. $_POST['ul_length'] .'"');
		$profileIni->setValue('ul_length_d', '"'. implode(',',$_POST['ul_length_d']) .'"');
		$profileIni->setValue('ul_length_c', '"'. implode(',',$_POST['ul_length_c']) .'"');
		$profileIni->setValue('ul_sub', '"'. $_POST['ul_sub'] .'"');
		$profileIni->setValue('ul_label', '"'. $_POST['ul_label'] .'"');
		$profileIni->setValue('ul_label_c', "'". str_replace("'",'',$_POST['ul_label_c']) ."'");

		//links

		$profileIni->setValue('l_ilinks' , '"'. $_POST['l_ilinks'] .'"');
		$profileIni->setValue('l_ilinks_d' , '"'. implode(',',$_POST['l_ilinks_d']) .'"');
		$profileIni->setValue('l_ilinks_c' , '"'. implode(',',$_POST['l_ilinks_c']) .'"');
		$profileIni->setValue('l_elinks' , '"'. $_POST['l_elinks'] .'"');
		$profileIni->setValue('l_elinks_d' , '"'. implode(',',$_POST['l_elinks_d']) .'"');
		$profileIni->setValue('l_elinks_c' , '"'. implode(',',$_POST['l_elinks_c']).'"');
		$profileIni->setValue('l_label' , '"'. $_POST['l_label'] .'"');
		$profileIni->setValue('l_label_c' , "'". str_replace("'",'',$_POST['l_label_c']) ."'");

		//user experience

		$profileIni->setValue('u_speed', '"'. $_POST['u_speed'] .'"');
		$profileIni->setValue('u_speed_d', '"'. implode(',',$_POST['u_speed_d']) .'"');
		$profileIni->setValue('u_speed_c', '"'. implode(',',$_POST['u_speed_c']) .'"');
		$profileIni->setValue('u_gram', '"'. $_POST['u_gram'] .'"');
		$profileIni->setValue('u_gram_d', '"'. implode(',',$_POST['u_gram_d']) .'"');
		$profileIni->setValue('u_gram_c', '"'. implode(',',$_POST['u_gram_c']) .'"');
		$profileIni->setValue('u_res', '"'. $_POST['u_res'] .'"');
		$profileIni->setValue('u_fb', '"'. $_POST['u_fb'] .'"');
		$profileIni->setValue('u_label', '"'. $_POST['u_label'] .'"');
		$profileIni->setValue('u_label_c', "'". str_replace("'",'', $_POST['u_label_c']) ."'");

		$profileIni->saveFile();
	}
	elseif($q == 'd'){
		$file = $_POST['n'];
		unlink('../data/profiles/' . $file);
	}
	elseif($q == 'gd'){
		$file = trim($_POST['i']) . '.ini';
		$objIni = new IniParser('../data/profiles/' . $file);
		$descr  = htmlentities($objIni->getValue("g_descr"),ENT_QUOTES);
		print $descr;
	}
}
else{
	die('Err:0x1');
}