<?php
/*
 *  WordPress plugin for Contact Form 7 extensions
 *  Copyright (C) 2026 Creamailer

 *  This program 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 3 of the License, or
 *  (at your option) any later version.

 *  This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.
 */
namespace Gurumedia\CF7CMStandalone\Utility;

class UtilityBase {
    /**
     * debug
     * Use dumpJSON to print return $arg if development mode is on
     *
     * @param string|array $arg
     * @return string|boolean
     */
    public static function debug($arg) {
        return CF7CMSTANDALONE_CF7EB_DEV ? UtilityBase::dumpJSON($arg) : false;
    }


    /**
     * dumpJSON
     * Pretty prints any string or array
     *
     * @param string|array $arg
     * @return string
     */
    public static function dumpJSON($arg) {
        return '<pre>' . json_encode($arg, JSON_PRETTY_PRINT) . '</pre>';
    }

    /**
     * getCF7Tags
     * Get all CF7 tags
     *
     * @return array
     */
    public static function getCF7Tags() {
        $has_manager = class_exists('WPCF7_FormTagsManager');
        $manager = $has_manager ? '\\WPCF7_FormTagsManager' : '\\WPCF7_ShortcodeManager';
        return $manager::get_instance()->get_scanned_tags();
    }

    /**
     * getCF7TagNames
     * Get filtered tag names from CF7
     *
     * @return array
     */
    public static function getCF7TagNames() {
        $tags = UtilityBase::getCF7Tags();
        return array_filter(array_map(function ($v) {
            return $v['name'];
        }, $tags), function ($v) {
            return $v !== '';
        });
    }

    /**
     * htmlParams
     * Make HTML parameters from associative array
     *
     * @param array $arr Array of parameters
     * @return array
     */
    public static function htmlParams($arr) {
        $result = [];
        foreach ($arr as $k => $v) {
            $result[] = "$k='$v'";
        }
        return $result;
    }

    /**
     * makeResult
     * Creates a result object
     *
     * @param bool $is_ok
     * @param string $value
     * @return obj [is_ok => bool, value => string]
     */
    public static function makeResult($is_ok, $value) {
        return (object)["is_ok" => $is_ok, "value" => $value];
    }

    public static function logPrep($key, $shared_key, $list_id) {
        $sha = sha1(implode(":", [$key, $shared_key, $list_id]));
        $dir = dirname(__FILE__) . "/../../log";
        $file = $dir . "/CF7CM_" . $sha . ".log";
        $time = date(DATE_RFC2822);
        return (object)[ "sha" => $sha
                       , "dir" => $dir
                       , "file" => $file
                       , "time" => $time
                       , "perm" => $perm
                       ];
    }

    /**
     * log
     * Tries to log into a file
     *
     * @param string $key API key
     * @param string $shared_key API shared key
     * @param string $msg Log message
     * @return obj [is_ok => bool, value => string]
     * @
     */
    public static function log($key, $shared_key, $list_id, $msg) {
        $a = self::logPrep($key, $shared_key, $list_id);

        if (!file_exists($a->dir) && !mkdir($a->dir)) {
            return self::makeResult(false, __("Cannot make log dir, please check permissions", "cf7cm"));
        }
            
        if (!file_put_contents($a->file, $a->time . " :: " . $msg . "\n", FILE_APPEND)) {
            return self::makeResult(false, __("Cannot write to log file", "cf7cm"));
        }

        return self::makeResult(true, "OK");
    }

    /**
     * getLog
     * Tries to read log file
     *
     * @param string $key API key
     * @param string $shared_key API shared key
     * @return obj [is_ok => bool, value => string]
     */
    public static function getLog($key, $shared_key, $list_id) {
        $a = self::logPrep($key, $shared_key, $list_id);
        $contents = @file_get_contents($a->file);

        if (!$contents) {
            return self::makeResult(false, __("Failed to read file", "cf7cm"));
        }

        return self::makeResult(true, $contents);
    }
}
