HEX
Server: Apache
System: Linux hz.vslconceptsdomains.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: dkfounda (3233)
PHP: 8.1.34
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/local/mailchannels/hooks/HookRegister.php
<?php

namespace MailChannels;

class HookRegister {
    private $json;
    private $hooks;
    private $config;

    public function __construct($hooks) {
        $this->hooks = $hooks;
        $this->config = App::getConfig();

        $this->loadRegisteredHooks();
    }

    public function isHookLoaded($hook) {
        if (!is_subclass_of($hook, Hook::class)) {
            throw new \InvalidArgumentException("$hook must be a subclass of MailChannels\Hook");
        }

        $category = $hook::category();
        $event = $hook::event();
        $hookScript = $this->hookScriptName($hook);
        $stage = $hook::stage();

        if (isset($this->json->$category)) {
            if (isset($this->json->$category->$event)) {
                foreach ($this->json->$category->$event as $loadedHook) {
                    if ($loadedHook->hook == $hookScript && $loadedHook->stage == $stage) {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    public function hookScriptName($hook) {
        $config = $this->config;
        if (!is_subclass_of($hook, Hook::class)) {
            throw new \InvalidArgumentException("$hook must be a subclass of MailChannels\Hook");
        }

        return App::appRoot() . $config::RUN_HOOKS_BIN . " " . $hook;
    }

    public function loadRegisteredHooks() {
        $out = exec("/usr/local/cpanel/bin/manage_hooks list --output JSON", $pass, $return);

        if ($return != 0) {
            throw new \Exception("Couldn't load the registered hooks, last response: $out");
        }

        $this->json = json_decode($out);
    }

    public function registerHooks() {
        $config = $this->config;
        $out = array();
        $runHookScript = App::appRoot() . $config::RUN_HOOKS_BIN;
        foreach ($this->hooks as $hook) {
            if (!$this->isHookLoaded($hook)) {
                $category = $hook::category();
                $event = $hook::event();
                $stage = $hook::stage();

                // cpanel hooks run as the user specified in the function call, but need root user
                // permissions for our hook code to execute
                if (strcasecmp($category, "cpanel") == 0) {
                    $cmd = "/usr/local/cpanel/bin/manage_hooks add script \"$runHookScript $hook\" --manual --category $category --event $event --stage $stage --escalateprivs";
                } else {
                    $cmd = "/usr/local/cpanel/bin/manage_hooks add script \"$runHookScript $hook\" --manual --category $category --event $event --stage $stage";
                }

                exec($cmd, $output);

                $out[] = implode("\t\n", $output);
            }
        }

        return $out;
    }

    public function deleteHooks() {
        $config = $this->config;
        $out = array();
        $runHookScript = App::appRoot() . $config::RUN_HOOKS_BIN;
        foreach ($this->hooks as $hook) {
            if ($this->isHookLoaded($hook)) {
                $category = $hook::category();
                $event = $hook::event();
                $stage = $hook::stage();

                $cmd = "/usr/local/cpanel/bin/manage_hooks delete script \"$runHookScript $hook\" --manual --category $category --event $event --stage $stage";
                exec($cmd, $output);

                $out[] = implode("\t\n", $output);
            }
        }

        return $out;
    }
}