<commit_before><?php

namespace CentreonRemote\Application\Clapi;

use Centreon\Domain\Repository\InformationsRepository;
use Centreon\Domain\Repository\TopologyRepository;
use Centreon\Domain\Repository\OptionsRepository;
use Pimple\Container;
use Centreon\Infrastructure\Service\CentreonClapiServiceInterface;
use ReflectionClass;

/**
 * Class to manage remote server with clapi (enable, disable, import)
 */
class CentreonRemoteServer implements CentreonClapiServiceInterface
{

    /**
     * @var Container
     * todo: extract only services we need to avoid using whole container
     */
    private $di;

    public function __construct(Container $di)
    {
        $this->di = $di;
    }

    public static function getName() : string
    {
        return (new \ReflectionClass(__CLASS__))->getShortName();
    }

    /**
     * Clapi command to enable remote server
     *
     * @param string $parametersString parameters string made of
     * a comma separated list of hosts/urls/ip adresses representing the central,
     * a boolean to enable/disable certificate check to contact the central,
     * the method to use to contact the remote (http or https),
     * the http port to use to contact the remote,
     * a boolean to enable/disable certificate check to contact the remote,
     * a boolean to enable/disable the use of proxy to contact the central
     */
    public function enableRemote(string $parametersString)
    {
        /* Set default value */
        $noCheckCertificate = false;
        $data = array(
            'remoteHttpMethod' => 'http',
            'remoteHttpPort' => null,
            'remoteNoCheckCertificate' => false,
        );

        /* Check CLAPI */
        $options = explode(';', $parametersString);

        if (count($options) === 6) {
            $urlString = $options[0];
            $noCheckCertificate = $options[1];
            $data['remoteHttpMethod'] = $options[2];
            $data['remoteHttpPort'] = $options[3];
            $data['remoteNoCheckCertificate'] = $options[4];
            $noProxy = $options[5];
        } elseif (count($options) > 1) {
            echo "Expecting 6 parameters, received " . count($options) . "\n";
            return 1;
        }

        /* Extract host from URI */
        $hostList = array();
        $pattern_extract_host = '/^[htps:\/]*([a-z0-9.]+)[:0-9]*$/';
        $urlList = explode(',', $urlString);
        foreach ($urlList as $url) {
            if (preg_match($pattern_extract_host, $url, $matches)) {
                $hostList[] = $matches[1];
            }
        }

        echo "Starting Centreon Remote enable process:\n";

        echo "Limiting Menu Access...               ";
        $result =
            $this->getDi()[\Centreon\ServiceProvider::CENTREON_DB_MANAGER]
                ->getRepository(TopologyRepository::class)
                ->disableMenus();
        echo (($result) ? 'Success' : 'Fail') . "\n";

        echo "Limiting Actions...                   ";
        $this->getDi()[\Centreon\ServiceProvider::CENTREON_DB_MANAGER]
            ->getRepository(InformationsRepository::class)
            ->toggleRemote('yes');
        echo "Done\n";
<commit_msg>fix(#7711): hostname can have dash in name<commit_after><?php

namespace CentreonRemote\Application\Clapi;

use Centreon\Domain\Repository\InformationsRepository;
use Centreon\Domain\Repository\TopologyRepository;
use Centreon\Domain\Repository\OptionsRepository;
use Pimple\Container;
use Centreon\Infrastructure\Service\CentreonClapiServiceInterface;
use ReflectionClass;

/**
 * Class to manage remote server with clapi (enable, disable, import)
 */
class CentreonRemoteServer implements CentreonClapiServiceInterface
{

    /**
     * @var Container
     * todo: extract only services we need to avoid using whole container
     */
    private $di;

    public function __construct(Container $di)
    {
        $this->di = $di;
    }

    public static function getName() : string
    {
        return (new \ReflectionClass(__CLASS__))->getShortName();
    }

    /**
     * Clapi command to enable remote server
     *
     * @param string $parametersString parameters string made of
     * a comma separated list of hosts/urls/ip adresses representing the central,
     * a boolean to enable/disable certificate check to contact the central,
     * the method to use to contact the remote (http or https),
     * the http port to use to contact the remote,
     * a boolean to enable/disable certificate check to contact the remote,
     * a boolean to enable/disable the use of proxy to contact the central
     */
    public function enableRemote(string $parametersString)
    {
        /* Set default value */
        $noCheckCertificate = false;
        $data = array(
            'remoteHttpMethod' => 'http',
            'remoteHttpPort' => null,
            'remoteNoCheckCertificate' => false,
        );

        /* Check CLAPI */
        $options = explode(';', $parametersString);

        if (count($options) === 6) {
            $urlString = $options[0];
            $noCheckCertificate = $options[1];
            $data['remoteHttpMethod'] = $options[2];
            $data['remoteHttpPort'] = $options[3];
            $data['remoteNoCheckCertificate'] = $options[4];
            $noProxy = $options[5];
        } elseif (count($options) > 1) {
            echo "Expecting 6 parameters, received " . count($options) . "\n";
            return 1;
        }

        /* Extract host from URI */
        $hostList = array();
        $pattern_extract_host = '/^[htps:\/]*([a-z0-9.-]+)[:0-9]*$/';
        $urlList = explode(',', $urlString);
        foreach ($urlList as $url) {
            if (preg_match($pattern_extract_host, $url, $matches)) {
                $hostList[] = $matches[1];
            }
        }

        echo "Starting Centreon Remote enable process:\n";

        echo "Limiting Menu Access...               ";
        $result =
            $this->getDi()[\Centreon\ServiceProvider::CENTREON_DB_MANAGER]
                ->getRepository(TopologyRepository::class)
                ->disableMenus();
        echo (($result) ? 'Success' : 'Fail') . "\n";

        echo "Limiting Actions...                   ";
        $this->getDi()[\Centreon\ServiceProvider::CENTREON_DB_MANAGER]
            ->getRepository(InformationsRepository::class)
            ->toggleRemote('yes');
        echo "Done\n";

{'fraction_non_alphanumeric': 0.12091503267973856, 'fraction_numerical': 0.005291005291005291, 'mean_word_length': 2.3614016736401675, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '534238', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1719, 'n_words': 466}