<?php
	require_once("../system.engine/system.core.php");
	
	$mPong = array();
	$a = &$mPong;
	$p = &$_POST;
	
	global $systemHandler;
	$systemExecution = $systemHandler->loadHandler("EXECUTION");
	$systemHash = $systemHandler->loadHandler("HASH");
	/* ### DO YOUR CODES ONLY FROM HERE ### */

	$mContainerId = $p["containerId"];
	$mCommentText = $p["commentText"];
	$mUserId = $systemHandler->loadHandler("SESSION")->loadSession("USER");
	
	if($systemExecution->runQuery("SELECT COUNT(id) AS containerCount FROM web_timeline WHERE id = '%s'", $mContainerId)->fetch_object()->containerCount > 0) {
		$mContainerUserId = $systemExecution->runQuery("SELECT post_user_id FROM web_timeline WHERE id = '%s'", $mContainerId)->fetch_object()->post_user_id;
		if($systemExecution->runQuery("SELECT COUNT(id) AS friendsCount FROM web_friends WHERE (user_one_id = '%s' AND user_two_id = '%s') OR (user_one_id = '%s' AND user_two_id = '%s')", array($mUserId, $mContainerUserId, $mContainerUserId, $mUserId))->fetch_object()->friendsCount > 0 || $mContainerUserId == $mUserId || $systemExecution->runQuery("SELECT post_privacy FROM web_timeline WHERE id = '%s'", $mContainerId)->fetch_object()->post_privacy == "1") {
			$mImageId = $systemExecution->runQuery("SELECT post_image_id FROM web_timeline WHERE id = '%s'", $mContainerId)->fetch_object()->post_image_id;
			$systemExecution->runQuery("INSERT INTO web_comments (user_id, container_id, submit_date, comment_text, image_id) VALUES ('%s', '%s', '%s', '%s', '%s')", array($mUserId, $mContainerId, time(), $mCommentText, $mImageId));
			$a["isValid"] = true;
		} else $a["isValid"] = "3";
	} else  $a["isValid"] = "1";
	
	/* ### DO NOT CHANGE ANYTHING FROM HERE ### */
	header("Content-Type: application/json");
	echo json_encode($mPong);
?>