<?php

/********************************************************************
 * Copyright (C) 2025 Hassine RIANI (https://upvimeo.com)
 *
 * This file is part of Upvimeo
 *
 * Upvimeo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Upvimeo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Upvimeo. If not, see <https://www.gnu.org/licenses/>.
 **********************************************************************/

/**
 * Class UpVimeo_Logger
 *
 * Responsible for logging data
 *
 * @license GPLv2
 * @copyright Hassine RIANI (https://upvimeo.com)
 * @since 1.4.0
 */
class UpVimeo_Logger
{

	/**
	 * The log dir
	 * @var string|false
	 */
	private $log_dir = null;

	/**
	 * UpVimeo_Logger constructor.
	 */
	public function __construct()
	{
		$this->setup_log_dir();
		$this->protect_log_dir();
	}

	/**
	 * Wrapper for writing the interactions to /wp-content/uploads/ file
	 *
	 * @param        $message
	 * @param string $tag
	 * @param string $filename
	 *
	 * @return bool
	 */
	public function log($message, $tag = '', $filename = "debug.log")
	{
		global $wp_filesystem;
		if (empty($wp_filesystem)) {
			require_once ABSPATH . '/wp-admin/includes/file.php';
			WP_Filesystem();
		}

		if (! $wp_filesystem->is_dir($this->log_dir)) {
			return false;
		}
		$log_file_path = trailingslashit($this->log_dir) . $filename;
		if ($wp_filesystem->exists($log_file_path) && $wp_filesystem->size($log_file_path) > 10485760) {
			$wp_filesystem->delete($log_file_path);
		}
		$is_object = false;
		if (! is_string($message) && ! is_numeric($message)) {
			$message   = wp_json_encode($message);
			$is_object = true;
		}

		if (! empty($tag)) {
			if ($is_object) {
				$message = $tag . "\n" . $message;
				$message = sprintf("%s%s%s", $tag, PHP_EOL, $message);
			} else {
				$message = sprintf('%s: %s', $tag, $message);
			}
		}

		$message = sprintf('[%s] %s', gmdate('Y-m-d H:i:s'), $message);

		$this->writeln($log_file_path, $message);

		return true;
	}

	/**
	 * Return the log dir
	 */
	public function get_log_dir()
	{
		return $this->log_dir;
	}

	/**
	 * Return the log path
	 */
	private function setup_log_dir()
	{
		$this->log_dir = upvimeo_get_tmp_dir();
	}

	/**
	 * Return the log dir
	 *
	 * @param bool $noindex
	 */
	private function protect_log_dir($noindex = true)
	{

		$dir = $this->log_dir;

		global $wp_filesystem;
		if (empty($wp_filesystem)) {
			require_once ABSPATH . '/wp-admin/includes/file.php';
			WP_Filesystem();
		}

		if (! $wp_filesystem->is_dir($dir)) {
			$wp_filesystem->mkdir($dir);
		}
		if ($wp_filesystem->is_dir($dir)) {
			$index_path = $dir . DIRECTORY_SEPARATOR . 'index.html';
			if (! $wp_filesystem->exists($index_path)) {
				$wp_filesystem->put_contents($index_path, '');
			}
		}
		if ($noindex) {
			$htaccess_path = $dir . DIRECTORY_SEPARATOR . '.htaccess';
			if (! $wp_filesystem->exists($htaccess_path)) {
				$contents = '# BEGIN Upvimeo
# The directives (lines) between "BEGIN Upvimeo" and "END Upvimeo" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
# Disable PHP and Python scripts parsing.
<Files *>
  SetHandler none
  SetHandler default-handler
  RemoveHandler .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo
  RemoveType .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo
</Files>
<IfModule mod_php5.c>
  php_flag engine off
</IfModule>
<IfModule mod_php7.c>
  php_flag engine off
</IfModule>
<IfModule headers_module>
  Header set X-Robots-Tag "noindex"
</IfModule>
# END Upvimeo';
				$wp_filesystem->put_contents($htaccess_path, $contents);
			}
		}
	}

	/**
	 * Used to write contents into file provided by parameters
	 *
	 * @param $file string
	 * @param $contents string
	 * @param string $force_flag
	 */
	private function writeln($file, $contents, $force_flag = '')
	{
		global $wp_filesystem;
		if (empty($wp_filesystem)) {
			require_once ABSPATH . '/wp-admin/includes/file.php';
			WP_Filesystem();
		}

		if ($wp_filesystem->exists($file)) {
			$existing = $wp_filesystem->get_contents($file);
			$wp_filesystem->put_contents($file, $existing . $contents . "\n");
		} else {
			$wp_filesystem->put_contents($file, $contents . "\n");
		}
	}

}
