<?php

if (!defined('ABSPATH')) {
	fwduvp_vimeo_maybe_bootstrap_wordpress();
}

if (!defined('ABSPATH')) {
	exit;
}

// Get info, set here your API info.
$fwduvp_client_id = "bfa67fcff0c409e5f0dd169a32453ed7b1ba0825";
$fwduvp_vimeo_secret = "Dk4i4XuBZMNWiRFK3frYf/tpJ6LzDCkDlvmG7j5p9pggKU31sVtKEYVMc/g5VazeZ0kDAqUIo6NU2zNCJbs2HBpQMLAZSDqKewFOa7IE7iZt3ELjE/Husv43/NazOTur";
$fwduvp_vimeo_token = "f0e000c5a57980b346ac4c3382322d57";

require "autoload.php";

if (!function_exists('wp_remote_request')) {
	header('Content-Type: application/json; charset=UTF-8');
	echo '{"error":true,"message":"WordPress HTTP API is not available."}';
	exit;
}

$fwduvp_raw_nonce = filter_input(INPUT_GET, 'fwduvp_nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_NULL_ON_FAILURE);
$fwduvp_raw_nonce = $fwduvp_raw_nonce === null || $fwduvp_raw_nonce === false ? '' : (string) $fwduvp_raw_nonce;
$fwduvp_nonce = sanitize_text_field(wp_unslash($fwduvp_raw_nonce));

if ($fwduvp_nonce === '' || !function_exists('wp_verify_nonce') || !wp_verify_nonce($fwduvp_nonce, 'fwduvp_vimeo_data')) {
	header('Content-Type: application/json; charset=UTF-8');
	http_response_code(403);
	echo '{"error":true,"message":"Invalid nonce."}';
	exit;
}

$fwduvp_type = filter_input(INPUT_GET, 'type', FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_NULL_ON_FAILURE);
$fwduvp_page = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
$fwduvp_per_page = filter_input(INPUT_GET, 'per_page', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
$fwduvp_album_id = filter_input(INPUT_GET, 'album_id', FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_NULL_ON_FAILURE);
$fwduvp_user = filter_input(INPUT_GET, 'user', FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_NULL_ON_FAILURE);
$fwduvp_sort = filter_input(INPUT_GET, 'sort', FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_NULL_ON_FAILURE);
$fwduvp_path = filter_input(INPUT_GET, 'path', FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_NULL_ON_FAILURE);

if (!is_string($fwduvp_type) || $fwduvp_type === '' || !is_int($fwduvp_page) || !is_int($fwduvp_per_page)) {
	header('Content-Type: application/json; charset=UTF-8');
	echo '{"error":true,"message":"Vimeo access info missing!"}';
	exit;
}

$fwduvp_page = max(1, $fwduvp_page);
$fwduvp_per_page = max(1, min(100, $fwduvp_per_page));
$fwduvp_type = sanitize_key(wp_unslash((string) $fwduvp_type));
$fwduvp_sort = sanitize_key(is_string($fwduvp_sort) ? wp_unslash($fwduvp_sort) : 'default');
$fwduvp_sort = $fwduvp_sort !== '' ? $fwduvp_sort : 'default';

$fwduvp_album_id = is_string($fwduvp_album_id) ? sanitize_text_field(wp_unslash($fwduvp_album_id)) : '';
$fwduvp_user = is_string($fwduvp_user) ? sanitize_text_field(wp_unslash($fwduvp_user)) : '';
$fwduvp_path = is_string($fwduvp_path) ? sanitize_text_field(wp_unslash($fwduvp_path)) : '';

if ($fwduvp_album_id !== '' && !preg_match('/^[a-zA-Z0-9_-]+$/', $fwduvp_album_id)) {
	$fwduvp_album_id = '';
}

if ($fwduvp_user !== '' && !preg_match('/^[a-zA-Z0-9_-]+$/', $fwduvp_user)) {
	$fwduvp_user = '';
}

if ($fwduvp_path !== '' && !preg_match('/^[a-zA-Z0-9_-]+$/', $fwduvp_path)) {
	$fwduvp_path = '';
}

$fwduvp_lib = new Vimeo\Vimeo($fwduvp_client_id, $fwduvp_vimeo_secret, $fwduvp_vimeo_token);
$fwduvp_response = array('error' => true, 'message' => 'Invalid request.');

if ($fwduvp_type === 'vimeo_channel' && is_string($fwduvp_path) && $fwduvp_path !== '') {
	$fwduvp_response = $fwduvp_lib->request("/channels/$fwduvp_path/videos", array(
		'page' => $fwduvp_page,
		'per_page' => $fwduvp_per_page,
		'fields' => 'uri,name,description,privacy,pictures.sizes',
		'sort' => $fwduvp_sort,
	));
} elseif ($fwduvp_type === 'vimeo_user_album' && is_string($fwduvp_user) && $fwduvp_user !== '' && is_string($fwduvp_album_id) && $fwduvp_album_id !== '') {
	$fwduvp_response = $fwduvp_lib->request("/users/$fwduvp_user/albums/$fwduvp_album_id/videos", array(
		'page' => $fwduvp_page,
		'per_page' => $fwduvp_per_page,
		'fields' => 'uri,name,description,privacy,pictures.sizes',
		'sort' => $fwduvp_sort,
	));
}

header('Content-Type: application/json; charset=UTF-8');
echo json_encode($fwduvp_response);

function fwduvp_vimeo_maybe_bootstrap_wordpress() {
	$fwduvp_search_dir = __DIR__;

	for ($fwduvp_depth = 0; $fwduvp_depth < 8; $fwduvp_depth++) {
		$fwduvp_candidate = $fwduvp_search_dir . DIRECTORY_SEPARATOR . 'wp-load.php';
		if (is_readable($fwduvp_candidate)) {
			require_once $fwduvp_candidate;
			return;
		}

		$fwduvp_parent_dir = dirname($fwduvp_search_dir);
		if ($fwduvp_parent_dir === $fwduvp_search_dir) {
			break;
		}

		$fwduvp_search_dir = $fwduvp_parent_dir;
	}
}
?>