View file forum/ajax/komm.php

File size: 3.05Kb
<?php
require('../flowap/system.php');
if (empty($user['id'])): header('location: /'); exit; endif;
switch ($act) {
	case 'addkomm':
		$chtos = functions::ints($_GET['chtos']); // какой раздел сайта комментируется
		if($chtos == 1):
			$sql = DB::$dbs->queryFetch('SELECT `id`,`status` FROM `forum_them` WHERE `id` = ? LIMIT 1', [$id]);
			if (empty($sql['id']) or $sql['status'] == 2) { $d = ['message' => 'Ошибка #11.','type' => 'error']; echo json_encode($d); exit; }
		endif;
		$textt = functions::htmlred($_POST['textt']);
		//
		if(empty($textt) || mb_strlen($textt, 'UTF-8') < 5 || mb_strlen($textt, 'UTF-8') > 10000):
			$err = 'Неправильная длина текста темы.';
		endif;
		if(isset($err)):
			$d = ['message' => $err,'type' => 'error']; echo json_encode($d); exit;
		else:
			DB::$dbs->query('INSERT INTO `komm` SET `idus` = ?, `idchto` = ?, `text` = ?, `time` = ?, `chto` = ?',[$user['id'],$id,$textt,time(),$chtos]);
			if($chtos == 1):
				DB::$dbs->query('UPDATE `forum_them` SET `dateupd` = ? WHERE `id` = ?',[time(),$id]);
			endif;
			$_SESSION['good'] = 'Комментарий успешно оставлен.';
			$d = ['location' => '/forum/t/'.$id.'/','type' => 'success'];
			echo json_encode($d); exit;
		endif;
	break;
	case 'redkomm':
		$ids = functions::ints($_POST['id']);
		$sql = DB::$dbs->queryFetch('SELECT `id`,`idus`,`text` FROM `komm` WHERE `id` = ? and `idus` = ? LIMIT 1', [$ids,$user['id']]);
		if (empty($sql['id']) or $sql['idus'] != $user['id']): $d = ['message' => 'Ошибка #126.', 'location' => '/', 'type' => 'error']; echo json_encode($d); exit; endif;
		$zamena = '<form id="formj_k'.$ids.'" action="" onsubmit="return false;"><textarea placeholder="Текст темы" name="textt" class="_doprtjyr45g" style="height: 70px;">'.$sql['text'].'</textarea><input onclick="saveform (\'/ajax/komm.php?act=savekomm&id='.$ids.'\',\'k'.$ids.'\');return false;" type="submit" name="add" class="_dop3t4g5erf" value="Сохранить"></form>';
		$d = ['razd' => '#komm_'.$ids,'zamena' => $zamena,'type' => 'success'];
		echo json_encode($d); exit;
	break;
	case 'savekomm':
		if (empty($user['id'])): header('location: /'); exit; endif;
		$sql = DB::$dbs->queryFetch('SELECT `id`,`idus`,`idchto` FROM `komm` WHERE `id` = ? and `idus` = ? LIMIT 1', [$id,$user['id']]);
		if (empty($sql['id']) or $sql['idus'] != $user['id']): exit; endif;
		//
		$textt = functions::htmlred($_POST['textt']);
		//
		if(empty($textt) || mb_strlen($textt, 'UTF-8') < 5 || mb_strlen($textt, 'UTF-8') > 10000):
			$err = 'Неправильная длина текста темы.';
		endif;
		//
		if(isset($err)):
			$d = ['message' => $err,'type' => 'error']; echo json_encode($d); exit;
		else:
			DB::$dbs->query('UPDATE `komm` SET `text` = ? WHERE `id` = ?',[$textt,$id]);
			$_SESSION['good'] = 'Комментарий отредактирован.';
			$d = ['location' => '/forum/t/'.$sql['idchto'].'/','type' => 'success'];
			echo json_encode($d); exit;
		endif;
	break;
}
?>