403Webshell
Server IP : 185.143.234.238  /  Your IP : 185.215.232.195
Web Server : LiteSpeed
System : Linux saman.shetabanhost.com 4.18.0-553.121.1.lve.el8.x86_64 #1 SMP Thu Apr 30 16:40:41 UTC 2026 x86_64
User : behrakir ( 1361)
PHP Version : 8.1.34
Disable Function : symlink,shell_exec,show_source,exec,popen,system,passthru,proc_open,proc_close,pcntl_exec,mail
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /home/behrakir/khanomam.com/wp-content/plugins/sync-basalam/includes/Services/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /home/behrakir/khanomam.com/wp-content/plugins/sync-basalam/includes/Services//WebhookService.php
<?php

namespace SyncBasalam\Services;

use SyncBasalam\Admin\Settings\SettingsConfig;
use SyncBasalam\Admin\Settings;
use SyncBasalam\Config\Endpoints;
use SyncBasalam\Logger\Logger;

defined('ABSPATH') || exit;
class WebhookService
{
    private $apiService;
    private $basalamToken;
    private $webhookToken;


    public const TARGET_EVENT_IDS = [3, 5, 7];

    public function __construct()
    {
        $this->apiService = syncBasalamContainer()->get(ApiServiceManager::class);
        $this->basalamToken = Settings::getSettings(SettingsConfig::TOKEN);
        $this->webhookToken = Settings::getSettings(SettingsConfig::WEBHOOK_HEADER_TOKEN);

        if (!$this->webhookToken) {
            $newToken = Settings::generateToken();
            Settings::updateSettings([SettingsConfig::WEBHOOK_HEADER_TOKEN => $newToken]);
            $this->webhookToken = $newToken;
        }
    }

    public function setupWebhook()
    {
        if (!$this->canCreateWebhook()) return false;

        try {
            $existingWebhooks = $this->fetchWebhooks();
            if (!$existingWebhooks) return false;

            $existingWebhooks = json_decode($existingWebhooks, true);
            if (!is_array($existingWebhooks) || !isset($existingWebhooks['data']) || !is_array($existingWebhooks['data'])) {
                Logger::error('پاسخ نامعتبر از API وبهوک دریافت شد.');
                return false;
            }

            $webhookUrl = get_site_url() . "/wp-json/sync-basalam/v1/order-manager";

            $correctWebhook = null;
            $webhooksMarkedForDeletion = [];

            foreach ($existingWebhooks['data'] as $webhook) {
                if (isset($webhook['events']) && is_array($webhook['events'])) {
                    $webhookEventIds = [];
                    foreach ($webhook['events'] as $event) {
                        if (isset($event['id'])) {
                            $webhookEventIds[] = $event['id'];
                        }
                    }

                    $hasTargetEvents = !empty(array_intersect($webhookEventIds, self::TARGET_EVENT_IDS));

                    if ($hasTargetEvents) {
                        $hasAllEvents = count(array_intersect($webhookEventIds, self::TARGET_EVENT_IDS)) == count(self::TARGET_EVENT_IDS);

                        if ($hasAllEvents) $correctWebhook = $webhook['id'];
                        else $webhooksMarkedForDeletion[] = $webhook['id'];
                    }
                }
            }

            foreach (array_unique($webhooksMarkedForDeletion) as $webhookId) {
                $this->removeCurrentWebhook($webhookId);
            }

            if ($correctWebhook) return $this->updateCurrentWebhook($correctWebhook, self::TARGET_EVENT_IDS, $webhookUrl);

            return $this->createNewWebhook(self::TARGET_EVENT_IDS, $webhookUrl);
        } catch (\Exception $e) {
            Logger::error('خطا در تنظیم وبهوک: ' . $e->getMessage());
            return false;
        }
    }

    private function fetchWebhooks()
    {
        $header = ['Authorization' => 'Bearer ' . $this->basalamToken];

        try {
            $response = $this->apiService->get(Endpoints::WEBHOOKS, $header);
        } catch (\Exception $e) {
            Logger::error('خطا در دریافت لیست وبهوک‌ها: ' . $e->getMessage());
            return null;
        }

        if ($response && $response['status_code'] == 200) return $response['body'];

        return null;
    }

    private function createNewWebhook($eventIds, $webhookUrl)
    {
        $header = ['Authorization' => 'Bearer ' . $this->basalamToken];

        $data = [
            'event_ids'       => array_values($eventIds),
            'request_headers' => json_encode(['token' => $this->webhookToken]),
            'request_method'  => 'POST',
            'url'             => $webhookUrl,
            'is_active'       => true,
            'register_me'     => true,
        ];

        try {
            $response = $this->apiService->post(Endpoints::WEBHOOKS, $data, $header);
        } catch (\Exception $e) {
            Logger::error('خطا در ساخت وبهوک جدید: ' . $e->getMessage());
            return false;
        }

        if ($response && $response['status_code'] == 200) return true;
        else return false;
    }

    private function updateCurrentWebhook($webhookId, $eventIds, $webhookUrl)
    {
        $header = ['Authorization' => 'Bearer ' . $this->basalamToken];

        $data = [
            'event_ids'       => array_values($eventIds),
            'request_headers' => json_encode(['token' => $this->webhookToken]),
            'request_method'  => 'POST',
            'url'             => $webhookUrl,
            'is_active'       => true,
        ];

        $updateWebhookUrl = Endpoints::WEBHOOKS . '/' . $webhookId;

        try {
            $response = $this->apiService->patch($updateWebhookUrl, $data, $header);
        } catch (\Exception $e) {
            Logger::error('خطا در بروزرسانی وبهوک: ' . $e->getMessage());
            return false;
        }

        if ($response && $response['status_code'] == 200) return true;
        else return false;
    }

    private function removeCurrentWebhook($webhookId)
    {
        $deleteWebhookUrl = Endpoints::WEBHOOKS . '/' . $webhookId;
        $header = ['Authorization' => 'Bearer ' . $this->basalamToken];

        try {
            $response = $this->apiService->delete($deleteWebhookUrl, $header);
        } catch (\Exception $e) {
            Logger::error('خطا در حذف وبهوک: ' . $e->getMessage());
            return false;
        }

        if ($response && ($response['status_code'] == 200 || $response['status_code'] == 204)) return true;
        else return false;
    }

    private function canCreateWebhook(): bool
    {
        $siteUrl = get_site_url();

        if (str_contains($siteUrl, 'localhost')) {
            Logger::error("وبهوک برای محیط لوکال تنظیم نمی‌شود.");
            return false;
        }

        return true;
    }
}

Youez - 2016 - github.com/yon3zu
LinuXploit