View file api/comments.php

File size: 2.49Kb
<?php
require_once '../system/function.php';

header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=utf8');
header('Access-Control-Allow-Methods: GET');

$data = null;

if (!empty($_GET['id'])) {
	$id = abs(intval($_GET['id']));

	$limit = (isset($_GET['limit']) != 0) ? (int)$_GET['limit']: 99999;
	$offset = (isset($_GET['offset']) != 0) ? (int)$_GET['offset']: 0;

	$file = $db->query("SELECT * FROM `file` WHERE `id` = $id")->fetch_assoc();

	if ($file['pass'] != 0) {
		$data = array(
			'data' => [
				'error' => "Bad Request",
				'message' => "Locked file"
			],
			'success' => false,
			'status' => 400
		);
		echo json_encode($data, JSON_UNESCAPED_UNICODE);
		exit;
	}

	if ($file != 0) {
		$all = $db->query("SELECT * FROM `komm` WHERE `file` = $id ORDER BY `id` DESC");

		if ($all->num_rows === 0) {
			$data = array(
				'data' => [
					'error' => "Bad Request",
					'message' => "No comments yet"
				],
				'success' => false,
				'status' => 400
			);
			echo json_encode($data, JSON_UNESCAPED_UNICODE);
			exit;
		}

		$comms = $db->query("SELECT * FROM `komm` WHERE `file` = $id ORDER BY `id` DESC LIMIT $limit OFFSET $offset");
		if ($comms->num_rows === 0) {
			$data = array(
				'data' => [
					'error' => "Bad Request",
					'message' => "No more comments"
				],
				'success' => false,
				'status' => 400
			);
			echo json_encode($data, JSON_UNESCAPED_UNICODE);
			exit;
		}

		$arr = [];

		while ($a = $comms->fetch_assoc()) {
			$userPhoto = $a['user'] != 0 ? file_exists(H.'/modules/users/photos/'.$a['user'].'.png') ? 'https://'.$_SERVER['HTTP_HOST'].'/modules/users/photos/'.$a['user'].'.png' : 'https://'.$_SERVER['HTTP_HOST'].'/assets/img/user_api.png' : 'https://'.$_SERVER['HTTP_HOST'].'/assets/img/user_api.png';

			array_push($arr, [
				'id' => (int)$a['id'],
				'file' => (int)$a['file'],
				'author' => $a['name'],
				'userPhoto' => $userPhoto,
				'text' => $a['komm'],
				'like' => (int)$a['like'],
				'time' => (int)$a['time']
			]);
		}

		$data = array(
			'comments' => $arr,
			'count' => $db->query('SELECT COUNT(*) FROM `komm` WHERE `file` = '.$id)->fetch_row()[0]
		);
	}

	if ($file == 0) {
		$data = array(
			'data' => [
				'error' => "Bad Request",
				'message' => "File Not Found"
			],
			'success' => false,
			'status' => 400
		);
	}
} else {
	$data = array(
		'data' => [
			'error' => "Bad Request",
			'message' => "Empty Request"
		],
		'success' => false,
		'status' => 400
	);
}

echo json_encode($data, JSON_UNESCAPED_UNICODE);
exit;