<?php
include	("_dirinfo.php");
include (PATH_TO_ROOT."init.php");

$db->ChangeDatabase(DB_WARPIT_HELP);

//7.1.2015 sqlinjection
if(class_exists('convert'))
{
    if(isset($_POST['RMtable'])) $_POST['RMtable'] = convert::toString($_POST['RMtable']);
    if(isset($_REQUEST['RMtable'])) $_REQUEST['RMtable'] = convert::toString($_REQUEST['RMtable']);
    if(isset($_POST['table'])) $_POST['table'] = convert::toString($_POST['table']);
    if(isset($_REQUEST['table'])) $_REQUEST['table'] = convert::toString($_REQUEST['table']);
}

$id = $_REQUEST['id'];

$id = explode("_", $id);

$id = $id[0];

$used_lang = 1;

switch($_REQUEST['action'])
{
	case 'updateScore':
	{
		if($id > 0){
		
			$score = $_REQUEST['score'];
			$text = $_REQUEST['text'];
			$db->SQLexecute("INSERT INTO help_topic_score (`id_topic`, `id_user`, `id_lang`, `score`, `text`) VALUES ('".$id."', '1', '".$used_lang."', '".$score."', '".$text."')");
			
			//echo '{success:true, response:\'<h2>How was this information helpful?</h2><br /><b>Thank you for response!</b>\'}';
			echo '<h2>How was this information helpful?</h2><br />Thank you for response!';
			
		}
		break; 
			
	}
	
	case 'updateCat':
	{
		if($id > 0){
		    $config = HTMLPurifier_Config::createDefault();
            $purifier = new HTMLPurifier($config);
            //$clean_html = $purifier->purify($dirty_html);
		
			$faq = 0;
			
			if($_REQUEST['faq']=='1' || $_REQUEST['faq']=='on') $faq = 1;
			
			$score = $_REQUEST['score'];
            $text = $_REQUEST['text'];
            $text = $purifier->purify($text);
			$text = mysql_real_escape_string(str_replace("\n", " ", $text));
			$db->SQLexecute("UPDATE ".$_REQUEST['table']." SET `faq` = '".$faq."' WHERE id='".$id."' ");
		
			$lang_res = $db->SQLexecute("SELECT f.id, f.title, g.title AS g_title FROM lang AS f LEFT JOIN ".$_REQUEST['table']."Lang AS g ON g.id_lang=f.id AND g.id_sup='".$id."'  ");
		
			while($lang = $db->fetchAssoc($lang_res)){
				$title = $_REQUEST['title_'.$id.'_'.$lang["id"] ];
				$text = $_REQUEST['text_'.$id.'_'.$lang["id"] ];
				$text = $purifier->purify($text);
				$text = mysql_real_escape_string(str_replace("\n", " ", $text));
               
               
				if($title != ''){
					$temp_help_lang = $db->SQLexecute("SELECT * FROM ".$_REQUEST['table']."Lang WHERE id_sup='".$id."' AND id_lang='".$lang["id"]."' ");
					if($db->NumRows($temp_help_lang)==0){
						$db->SQLexecute("INSERT INTO ".$_REQUEST['table']."Lang (`id_sup`, `id_lang`, `title`, `text`) VALUES ('".$id."', '".$lang['id']."', '".$title."', '".$text."') ");
					}else{
					
						$db->SQLexecute("UPDATE ".$_REQUEST['table']."Lang SET `title`='".$title."', `text`='".$text."' WHERE id_sup='".$id."' AND id_lang='".$lang["id"]."' ");
					}				
					
				}else{
					$db->SQLexecute("DELETE FROM ".$_REQUEST['table']."Lang WHERE id_sup='".$id."' AND id_lang='".$lang["id"]."' ");
				}
			}
			echo "{success:true, response:'Update is successful!'}";
			
		}
		break;
			
	}
	
	
	case 'subTopicEdit':
	{
		if($id > 0){
		    
             $purifier = new HTMLPurifier($config);
            //$clean_html = $purifier->purify($dirty_html);
        
            $text = $_REQUEST['text'];
            $text = $purifier->purify($text);
            $text = mysql_real_escape_string(str_replace("\n", " ", $text));
            
			if($_REQUEST['totd']=='on') $totd = 1;
			
			//$db->SQLexecute("UPDATE ".$_REQUEST['table']." SET `idLab` = '".$_REQUEST['title']."', `orderBy` = '".$_REQUEST['orderBy']."' WHERE id='".$id."' ");
			$db->SQLexecute("UPDATE ".$_REQUEST['table']." SET `idLab` = '". $_REQUEST['title']."', `totd` = '".$totd."' WHERE id='".$id."' ");
			$db->SQLexecute("UPDATE ".$_REQUEST['table']."Lang SET `title` = '".$_REQUEST['title']."', `text` = '".$text."' WHERE id_sup='".$id."' AND id_lang='".$used_lang."' ");
			echo "{success:true, response:'Update is successful!'}";
			
		}else{
			echo '{success:false}';
		}
		break;
	}

		
	case 'seeAlsoTopicAdd':
	{
		if($_REQUEST['id_1'] > 0 && $_REQUEST['id_2'] > 0){
			
			$_POST['id_1'] = explode("_", $_POST['id_1']);
			$id_1 = $_POST['id_1'][0];
			
			$_POST['id_2'] = explode("_", $_POST['id_2']);
			$id_2 = $_POST['id_2'][0];			
			
			$db->SQLexecute("INSERT INTO ".$_REQUEST['table']."Also (`id_sup_1`, `id_sup_2`) VALUES ('".$id_1."', '".$id_2."') ");
			$last_id = $db->GetLastInsertId();
			
			$db->SQLexecute("UPDATE ".$_REQUEST['table']."Also SET `orderBy` = '".$last_id."' WHERE id='".$last_id."' ");
			
			echo "{success:true, response:'Creation is successful!'}";
			
		}else{
			echo '{success:false}';
		}
		break;
	}
	
	case 'seeAlsoTopicEdit':
	{
		if($id > 0){
			
			$db->SQLexecute("UPDATE ".$_REQUEST['table']."Also SET `id_sup_1` = '".$_REQUEST['id_1']."', `id_sup_2` = '".$_REQUEST['id_2']."' WHERE id='".$id."' ");
			
			echo "{success:true, response:'Update is successful!'}";
			
		}else{
			echo '{success:false}';
		}
		break;
	}
	
	case 'seeAlsoTopicDelete':
	{
		if($id > 0){
			
			$db->SQLexecute("DELETE FROM ".$_REQUEST['table']."Also WHERE id='".$id."' ");
			
			echo "{success:true, response:'Update is successful!'}";
			
		}else{
			echo '{success:false}';
		}
		break;
	}
	
	case 'searchTopics':
	{
		//echo "{success:true, response:'Update is successful!'}";
				
		
		$s_res = "";
		
		$s_res .= "Searched for: \"".$_REQUEST['s']."\"<br />";
		
		$help_res = $db->SQLexecute('SELECT a.node_img,a.id_sup,a.id,b.title AS idLab,a.topic FROM '.$_REQUEST['RMtable'].' AS a, '.$_REQUEST['RMtable'].'Lang AS b WHERE (b.title LIKE "%'.$_REQUEST['s'].'%" OR b.text LIKE "%'.$_REQUEST['s'].'%") AND a.topic=1 AND a.id = b.id_sup AND b.id_lang = "'.$used_lang.'" ORDER BY a.topic ASC, a.orderBy ');
		//$s_res .= $db->NumRows($help_res);
		while($help = $db->fetchAssoc($help_res)){
			$s_res .= $help["idLab"]."<br />";
		}
				
		echo $s_res;
		
		
		break;
	}	
	
	case 'totdUpdate':
	{
		//echo "{success:true, response:'Update is successful!'}";
				
		$help_res = $db->SQLexecute("SELECT f.*, g.title, g.text, g.date_m, g.id_lang FROM ".$_REQUEST['RMtable']." AS f, ".$_REQUEST['RMtable']."Lang AS g WHERE f.totd=1 AND f.id=g.id_sup AND g.id_lang=".$used_lang." ORDER BY RAND() ");
		$help = $db->fetchArray($help_res);
		
		//$s_res = "<h1>".$help['title']."</h1><div>".$help['text']."</div>";
		$s_res = "<h1>".$help['title']."#|#<div>".$help['text']."</div>";
							
		echo $s_res;
		
		
		break;
	}
	
	case 'catFaqEdit':
	{
		if($id > 0){
			$faq = 0;
			if($_REQUEST['faq']=='on') $faq = 1;
			
			//$db->SQLexecute("UPDATE ".$_REQUEST['table']." SET `idLab` = '".$_REQUEST['title']."', `orderBy` = '".$_REQUEST['orderBy']."' WHERE id='".$id."' ");
			$db->SQLexecute("UPDATE ".$_REQUEST['table']." SET `faq` = '".$faq."' WHERE id='".$id."' ");
			echo "{success:true, response:'Update is successful!'}";
			
		}else{
			echo '{success:false}';
		}
		break;
	}
    case 'loadForm':
    {
        $help_res = $db->SQLexecute("SELECT f.*, g.title, g.text, g.date_m, g.id_lang 
                                        FROM ".$_POST['table']." AS f, ".$_POST['table']."Lang AS g 
                                        WHERE f.id=".$_POST['id']." AND f.id=g.id_sup AND g.id_lang=".$used_lang." ORDER BY f.`orderBy` ASC ");
        $help = $db->fetchArray($help_res);
        $data['success'] = true;
        $data['data'] = $help;
        
        echo json_encode($data);
        break;        
    }
	default:
	{
		echo '{success:false}';
		break;
	}
}
mysql_close();

?>
