Viewing File: /usr/local/cpanel/base/3rdparty/roundcube/plugins/formbricks/formbricks.php

<?php

declare(strict_types=1);

require_once __DIR__ . '/lib/FormbricksScriptGenerator.php';

use RoundcubeFormbricks\FormbricksScriptGenerator;

/**
 * Integrates Formbricks surveys into Roundcube Webmail
 */
class formbricks extends rcube_plugin
{
    public $task = '.*';

    /** @var array<string, mixed> Plugin configuration */
    private $config;

    /** @var array<string> Configuration validation errors */
    private $validation_errors = array();

    /** @var bool Whether configuration is valid */
    private $config_valid = false;

    /**
     * Plugin initialization
     *
     * @return void
     */
    public function init(): void
    {
        $this->load_config();
        $this->config = rcmail::get_instance()->config->get('formbricks', array());

        // Validate configuration on init
        $this->config_valid = $this->validate_config();

        if (!$this->config_valid) {
            rcube::write_log('errors', 'Formbricks: Plugin not properly configured');
        }

        // Always add hook to show errors in console (even if config is invalid)
        $this->add_hook('render_page', array($this, 'inject_formbricks'));
    }

    /**
     * Validate plugin configuration
     *
     * @return bool True if configuration is valid
     */
    private function validate_config(): bool
    {
        $this->validation_errors = array(); // Reset errors

        $environment_id = $this->config['environment_id'] ?? '';
        $app_url = $this->config['app_url'] ?? '';

        if (empty($environment_id)) {
            $error = 'environment_id not configured';
            rcube::write_log('errors', 'Formbricks: ' . $error);
            $this->validation_errors[] = $error;
        }

        if (empty($app_url) || !filter_var($app_url, FILTER_VALIDATE_URL)) {
            $error = 'Invalid or missing app_url';
            rcube::write_log('errors', 'Formbricks: ' . $error);
            $this->validation_errors[] = $error;
        }

        return empty($this->validation_errors);
    }

    /**
     * Inject Formbricks initialization script
     *
     * @param array $args Hook arguments containing page content
     * @return array Modified arguments with injected script
     */
    public function inject_formbricks(array $args): array
    {
        try {
            $rcmail = rcmail::get_instance();

            // Only inject on main mail/settings/addressbook pages, not on modal dialogs
            // Check template name - skip modals like 'about', 'error', etc.
            $template = $args['template'] ?? '';
            $skip_templates = ['about', 'error', 'messageerror', 'message_part', 'messagepreview'];

            if ( in_array($template, $skip_templates) || empty(getenv('FORMBRICKS_ENABLED') ) ) return $args;

            $scriptGenerator = new FormbricksScriptGenerator($this->config);

            // Get and validate configuration
            $app_url = $this->config['app_url'] ?? '';
            $environment_id = $this->config['environment_id'] ?? '';

            if (empty($environment_id) || empty($app_url)) {
                return $args;
            }

            // Get language preference and convert to 2-letter code
            $language = $rcmail->config->get('language', 'en_US');
            $language_code = strtolower(substr($language, 0, 2));

            // Generate script using ScriptGenerator
            $script = $scriptGenerator->generate($rcmail->user->get_hash(), $language_code);

            // Inject before </body>
            if (strpos($args['content'], '</body>') !== false) {
                $args['content'] = str_replace('</body>', $script . '</body>', $args['content']);
            }

            return $args;
        } catch (Exception $e) {
            rcube::write_log('errors', 'Formbricks plugin error: ' . $e->getMessage());
            return $args;
        }
    }
}
Back to Directory File Manager