<?php
	require_once("../system.engine/system.core.php");
	$l->setPrefix("ajax.handle.timeline.post");
	
	$mPong = array();
	$a = &$mPong;
	$p = &$_POST;
	
	global $systemHandler;
	$systemExecution = $systemHandler->loadHandler("EXECUTION");
	$systemHash = $systemHandler->loadHandler("HASH");
	/* ### DO YOUR CODES ONLY FROM HERE ### */
	
	$mPostText = $_POST["postText"];
	$mPostLocation = $_POST["postLocation"];
	$mPostPrivacy = $_POST["postPrivacy"];
	$mPostHashtags = $_POST["postHashtags"];
	$mPostMentions = $_POST["postMentions"];
	$mPostLocationUser = $_POST["postLocationUser"];
	// check if location is self or friend
	
	$mPostImagesSet = isset($_POST["postImageHash"]);
	$mPostImageHash = null;
	$mPostImageAlbumId = null;
	
	if($mPostImagesSet) {
		$mPostImageHash = $_POST["postImageHash"];
		$mPostImageAlbumId = $_POST["postImageAlbumId"];
	}
	
			$mAllowedPrivacy = array('1', '2', '3');
			if(in_array($mPostPrivacy, $mAllowedPrivacy)) {
				$mHashTagsTiled = explode(',', $mPostHashtags);
				if(count($mHashTagsTiled) <= 15) {
					$mMentionsTiled = explode(',', $mPostMentions);
					$mValidMentionUserIdCollection = array();
					foreach($mMentionsTiled as $mMentionName) {
						$mMentionCache = explode(' ', trim($mMentionName));
						if(count($mMentionCache) == 2) {
							$mFirstname = $mMentionCache[0];
							$mLastname = $mMentionCache[1];
							
							$mMentionCache = $systemExecution->runQuery("SELECT COUNT(web_user.id) AS validationCount, web_user.id AS mentionUserId FROM web_user, web_friends WHERE web_user.firstname = '%s' AND web_user.lastname = '%s' AND ((web_friends.user_one_id = web_user.id AND web_friends.user_two_id = '%s') OR (web_friends.user_two_id = web_user.id AND web_friends.user_one_id = '%s'))", array($mFirstname, $mLastname, "{user.id}", "{user.id}"))->fetch_object();
							
							if($mMentionCache->validationCount > 0) {
								if(!in_array($mMentionCache->mentionUserId, $mValidMentionUserIdCollection))
									array_push($mValidMentionUserIdCollection, $mMentionCache->mentionUserId);
							}
						}
					}
					
					$mPostMentions = null;
					for($i = 0; $i < count($mValidMentionUserIdCollection); $i++)
						$mPostMentions .= $i == 0 ? $mValidMentionUserIdCollection[$i] : ','.$mValidMentionUserIdCollection[$i];
					
					if(strlen($mPostText) == 0 && (strlen($mPostLocation) > 0 || strlen($mPostImageHash) > 0)) {
						if($mPostImagesSet) {
							if($systemExecution->runQuery("SELECT COUNT(id) AS albumCount FROM web_albums WHERE id = '%s' AND album_owner_id = '%s'", array($mPostImageAlbumId, "{user.id}"))->fetch_object()->albumCount > 0) {
								$systemExecution->runQuery("INSERT INTO web_images (image_hash, image_user_id, image_album_id) VALUES ('%s', '%s', '%s')", array($mPostImageHash, "{user.id}", $mPostImageAlbumId));
								$mImageId = $systemExecution->runQuery("SELECT id FROM web_images WHERE image_hash = '%s' AND image_user_id = '%s'", array($mPostImageHash, "{user.id}"))->fetch_object()->id;
								$systemExecution->runQuery("INSERT INTO web_timeline (post_user_id, post_text, post_location, post_privacy, post_hashtags_list, post_mentions_list, post_submit_date, post_image_id, post_entry_location) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", array("{user.id}", $mPostText, $mPostLocation, $mPostPrivacy, $mPostHashtags, $mPostMentions, time(), $mImageId, $mPostLocationUser));
								$a["isValid"] = true;
							} else $a["isValid"] = $l->getValue("invalidalbum");
						} else {
							$systemExecution->runQuery("INSERT INTO web_timeline (post_user_id, post_text, post_location, post_privacy, post_hashtags_list, post_mentions_list, post_submit_date, post_image_id, post_entry_location) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", array("{user.id}", $mPostText, $mPostLocation, $mPostPrivacy, $mPostHashtags, $mPostMentions, time(), -1, $mPostLocationUser));
							$a["isValid"] = true;
						}
					} else if(strlen($mPostText) > 0) {
						if($mPostImagesSet) {
							if($systemExecution->runQuery("SELECT COUNT(id) AS albumCount FROM web_albums WHERE id = '%s' AND album_owner_id = '%s'", array($mPostImageAlbumId, "{user.id}"))->fetch_object()->albumCount > 0) {
								$systemExecution->runQuery("INSERT INTO web_images (image_hash, image_user_id, image_album_id) VALUES ('%s', '%s', '%s')", array($mPostImageHash, "{user.id}", $mPostImageAlbumId));
								$mImageId = $systemExecution->runQuery("SELECT id FROM web_images WHERE image_hash = '%s' AND image_user_id = '%s'", array($mPostImageHash, "{user.id}"))->fetch_object()->id;
								$systemExecution->runQuery("INSERT INTO web_timeline (post_user_id, post_text, post_location, post_privacy, post_hashtags_list, post_mentions_list, post_submit_date, post_image_id, post_entry_location) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", array("{user.id}", $mPostText, $mPostLocation, $mPostPrivacy, $mPostHashtags, $mPostMentions, time(), $mImageId, $mPostLocationUser));
								$a["isValid"] = true;
							} else $a["isValid"] = $l->getValue("invalidalbum");
						} else {
							$systemExecution->runQuery("INSERT INTO web_timeline (post_user_id, post_text, post_location, post_privacy, post_hashtags_list, post_mentions_list, post_submit_date, post_image_id, post_entry_location) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", array("{user.id}", $mPostText, $mPostLocation, $mPostPrivacy, $mPostHashtags, $mPostMentions, time(), -1, $mPostLocationUser));
							$a["isValid"] = true;
						}
					} else $a["isValid"] = $l->getValue("fillout");
				} else $a["isValid"] = $l->getValue("hastagslimit");
			} else $a["isValid"] = "Please stop playing with asynchronous-requests! :-D";
	
	/* ### DO NOT CHANGE ANYTHING FROM HERE ### */
	header("Content-Type: application/json");
	echo json_encode($mPong);
	?>