Source of file RepoMarket.php

Size: 57,708 Bytes - Last Modified: 2020-10-24T02:46:31+00:00

/home/travis/build/NextDom/nextdom-core/src/Repo/RepoMarket.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
<?php

/* This file is part of Jeedom.
 *
 * Jeedom 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.
 *
 * Jeedom 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 Jeedom. If not, see <http://www.gnu.org/licenses/>.
 */

/* * ***************************Includes********************************* */

namespace NextDom\Repo;

use NextDom\Com\ComShell;
use NextDom\Enums\DateFormat;
use NextDom\Enums\LogTarget;
use NextDom\Exceptions\CoreException;
use NextDom\Helpers\Api;
use NextDom\Helpers\DnsHelper;
use NextDom\Helpers\LogHelper;
use NextDom\Helpers\NetworkHelper;
use NextDom\Helpers\NextDomHelper;
use NextDom\Helpers\SystemHelper;
use NextDom\Helpers\Utils;
use NextDom\Interfaces\BaseRepo;
use NextDom\Managers\BackupManager;
use NextDom\Managers\CacheManager;
use NextDom\Managers\ConfigManager;
use NextDom\Managers\MessageManager;
use NextDom\Managers\PluginManager;
use NextDom\Managers\UpdateManager;
use NextDom\Managers\UserManager;
use NextDom\Model\DataClass\SystemHealth;
use NextDom\Model\Entity\JsonRPCClient;
use NextDom\Model\Entity\Update;

class RepoMarket implements BaseRepo
{
    /*     * *************************Attributs****************************** */

    public static $_name = 'Market';
    public static $_icon = 'fas fa-shopping-cart';
    public static $_description = 'repo.market.description';

    public static $_scope = [
        'plugin' => true,
        'backup' => false,
        'hasConfiguration' => true,
        'proxy' => true,
        'sendPlugin' => true,
        'hasStore' => true,
        'hasScenarioStore' => true,
        'test' => true,
    ];

    public static $_configuration = [
        'configuration' => [
            'address' => [
                'name' => 'repo.market.conf.address.name',
                'type' => 'input',
                'placeholder' => 'repo.market.conf.address.placeholder',
            ],
            'username' => [
                'name' => 'repo.market.conf.user',
                'type' => 'input',
            ],
            'password' => [
                'name' => 'repo.market.conf.password',
                'type' => 'password',
            ],
            'cloud::backup::name' => [
                'name' => 'repo.market.conf.cloud.name',
                'type' => 'input',
            ],
            'cloud::backup::password' => [
                'name' => 'repo.market.conf.cloud.password',
                'type' => 'password',
            ],
            'cloud::backup::fullfrequency' => [
                'name' => 'repo.market.conf.cloud.frequency',
                'type' => 'select',
                'values' => ['1D' => 'repo.market.conf.cloud.day', '1W' => 'repo.market.conf.cloud.week', '1M' => 'repo.market.conf.cloud.month'],
            ],
        ],
        'parameters_for_add' => [
            'version' => [
                'name' => 'repo.market.conf.version',
                'type' => 'input',
            ],
        ],
    ];

    private $id;
    private $name;
    private $type;
    private $datetime;
    private $description;
    private $categorie;
    private $changelog;
    private $doc;
    private $version;
    private $user_id;
    private $downloaded;
    private $status;
    private $author;
    private $logicalId;
    private $rating;
    private $utilization;
    private $isAuthor;
    private $img;
    private $buyer;
    private $purchase = 0;
    private $cost = 0;
    private $realcost = 0;
    private $link;
    private $certification;
    private $language;
    private $private;
    private $updateBy;
    private $parameters;
    private $hardwareCompatibility;
    private $nbInstall;
    private $allowVersion = [];

    /*     * ***********************Méthodes statiques*************************** */

    /**
     * @param Update $_update
     * @throws CoreException
     * @throws \ReflectionException
     */
    public static function checkUpdate(&$_update)
    {
        if (is_array($_update)) {
            if (count($_update) < 1) {
                return;
            }
            $markets = ['logicalId' => [], 'version' => []];
            $marketObject = [];
            foreach ($_update as $update) {
                $markets['logicalId'][] = ['logicalId' => $update->getLogicalId(), 'type' => $update->getType()];
                $markets['version'][] = $update->getConfiguration('version', 'stable');
                $marketObject[$update->getType() . $update->getLogicalId()] = $update;
            }
            $markets_infos = self::getInfo($markets['logicalId'], $markets['version']);
            foreach ($markets_infos as $logicalId => $market_info) {
                $update = $marketObject[$logicalId];
                if (is_object($update)) {
                    $update->setStatus($market_info['status']);
                    $update->setConfiguration('market', $market_info['market']);
                    $update->setRemoteVersion($market_info['datetime']);
                    if ($update->getConfiguration('version') == '') {
                        $update->setConfiguration('version', 'stable');
                    }
                    $update->save();
                }
            }
            return;
        }
        $market_info = self::getInfo(['logicalId' => $_update->getLogicalId(), 'type' => $_update->getType()], $_update->getConfiguration('version', 'stable'));
        $_update->setStatus($market_info['status']);
        $_update->setConfiguration('market', $market_info['market']);
        $_update->setRemoteVersion($market_info['datetime']);
        $_update->save();
    }

    public static function getInfo($_logicalId, $_version = 'stable')
    {
        if (is_array($_logicalId) && is_array($_version) && count($_logicalId) == count($_version)) {
            if (is_array(reset($_logicalId))) {
                $markets = self::byLogicalIdAndType($_logicalId);
            } else {
                $markets = self::byLogicalId($_logicalId);
            }

            $returns = [];
            $countLogicalId = count($_logicalId);
            for ($i = 0; $i < $countLogicalId; $i++) {
                if (is_array($_logicalId[$i])) {
                    $logicalId = $_logicalId[$i]['type'] . $_logicalId[$i]['logicalId'];
                } else {
                    $logicalId = $_logicalId[$i];
                }
                $return['owner'] = [];
                $return['datetime'] = '0000-01-01 00:00:00';
                if ($logicalId == '' || ConfigManager::byKey('market::address') == '') {
                    $return['owner']['market'] = 0;
                    $return['status'] = 'ok';
                    return $return;
                }

                if (ConfigManager::byKey('market::username') != '' && ConfigManager::byKey('market::password') != '') {
                    $return['owner']['market'] = 1;
                } else {
                    $return['owner']['market'] = 0;
                }
                $return['market'] = 0;

                try {
                    if (isset($markets[$logicalId])) {
                        $market = $markets[$logicalId];
                        if (!is_object($market)) {
                            $return['status'] = 'ok';
                        } else {
                            $return['datetime'] = $market->getDatetime($_version[$i]);
                            $return['market'] = 1;
                            $return['owner']['market'] = $market->getIsAuthor();
                            $update = UpdateManager::byTypeAndLogicalId($market->getType(), $market->getLogicalId());
                            $updateDateTime = '0000-01-01 00:00:00';
                            if (is_object($update)) {
                                $updateDateTime = $update->getLocalVersion();
                            }
                            if ($updateDateTime < $market->getDatetime($_version[$i], $updateDateTime)) {
                                $return['status'] = 'update';
                            } else {
                                $return['status'] = 'ok';
                            }
                        }
                    } else {
                        $return['status'] = 'ok';
                    }
                } catch (\Exception $e) {
                    LogHelper::addDebug(LogTarget::MARKET, __('Erreur self::getinfo : ') . $e->getMessage());
                    $return['status'] = 'ok';
                }
                $returns[$logicalId] = $return;
            }
            return $returns;
        }
        $return = [];
        $return['datetime'] = '0000-01-01 00:00:00';
        $return['owner'] = [];
        if (ConfigManager::byKey('market::address') == '') {
            $return['market'] = 0;
            $return['owner']['market'] = 0;
            $return['status'] = 'ok';
            return $return;
        }

        if (ConfigManager::byKey('market::username') != '' && ConfigManager::byKey('market::password') != '') {
            $return['owner']['market'] = 1;
        } else {
            $return['owner']['market'] = 0;
        }
        $return['market'] = 0;

        try {
            if (is_array($_logicalId)) {
                $market = self::byLogicalIdAndType($_logicalId['logicalId'], $_logicalId['type']);
            } else {
                $market = self::byLogicalId($_logicalId);
            }
            if (!is_object($market)) {
                $return['status'] = 'depreciated';
            } else {
                $return['datetime'] = $market->getDatetime($_version);
                $return['market'] = 1;
                $return['owner']['market'] = $market->getIsAuthor();
                $update = UpdateManager::byTypeAndLogicalId($market->getType(), $market->getLogicalId());
                $updateDateTime = '0000-01-01 00:00:00';
                if (is_object($update)) {
                    $updateDateTime = $update->getLocalVersion();
                }
                if ($updateDateTime < $market->getDatetime($_version, $updateDateTime)) {
                    $return['status'] = 'update';
                } else {
                    $return['status'] = 'ok';
                }
            }
        } catch (\Exception $e) {
            LogHelper::addDebug(LogTarget::MARKET, __('Erreur self::getinfo : ') . $e->getMessage());
            $return['status'] = 'ok';
        }
        return $return;
    }

    /**
     * @param $_logicalId
     * @param string $_type
     * @return array|RepoMarket
     * @throws CoreException
     */
    public static function byLogicalIdAndType($_logicalId, $_type = '')
    {
        $market = self::getJsonRpc();
        if (is_array($_logicalId)) {
            $options = $_logicalId;
            $timeout = 240;
        } else {
            $options = ['logicalId' => $_logicalId, 'type' => $_type];
            $timeout = 10;
        }
        if ($market->sendRequest('market::byLogicalIdAndType', $options, $timeout, null, 1)) {
            if (is_array($_logicalId)) {
                $result = [];
                foreach ($market->getResult() as $logicalId => $marketResult) {
                    if (isset($marketResult['id'])) {
                        $result[$logicalId] = self::construct($marketResult);
                    }
                }
                return $result;
            }
            return self::construct($market->getResult());
        } else {
            LogHelper::addDebug(LogTarget::MARKET, print_r($market, true));
            throw new CoreException($market->getError(), $market->getErrorCode());
        }
    }

    public static function getJsonRpc()
    {
        $internalIp = '';
        try {
            $internalIp = NetworkHelper::getNetworkAccess('internal', 'ip');
        } catch (\Exception $e) {

        }
        $uname = SystemHelper::getSystemInformations();
        if (ConfigManager::byKey('market::username') != '' && ConfigManager::byKey('market::password') != '') {
            $params = [
                'username' => ConfigManager::byKey('market::username'),
                'password' => self::getPassword(),
                'password_type' => 'sha1',
                'nextdomversion' => NextDomHelper::getNextdomVersion(),
                'hwkey' => NextDomHelper::getHardwareKey(),
                'information' => [
                    'nbMessage' => MessageManager::nbMessage(),
                    'nbUpdate' => UpdateManager::nbNeedUpdate(),
                    'hardware' => (method_exists('nextdom', 'getHardwareName')) ? NextDomHelper::getHardwareName() : '',
                    'uname' => $uname,
                ],
                'market_api_key' => Api::getApiKey('apimarket'),
                'localIp' => $internalIp,
                'nextdom_name' => ConfigManager::byKey('name'),
                'plugin_install_list' => PluginManager::listPlugin(true, false, true),
            ];
            if (ConfigManager::byKey('market::allowDNS') != 1 || ConfigManager::byKey('network::disableMangement') == 1) {
                $params['url'] = NetworkHelper::getNetworkAccess('external');
            }
            $jsonrpc = new JsonRPCClient(ConfigManager::byKey('market::address') . '/core/api/api.php', '', $params);
        } else {
            $jsonrpc = new JsonRPCClient(ConfigManager::byKey('market::address') . '/core/api/api.php', '', [
                'nextdomversion' => NextDomHelper::getNextdomVersion(),
                'hwkey' => NextDomHelper::getHardwareKey(),
                'localIp' => $internalIp,
                'nextdom_name' => ConfigManager::byKey('name'),
                'plugin_install_list' => PluginManager::listPlugin(false, false, true),
                'information' => [
                    'nbMessage' => MessageManager::nbMessage(),
                    'nbUpdate' => UpdateManager::nbNeedUpdate(),
                    'hardware' => (method_exists('nextdom', 'getHardwareName')) ? NextDomHelper::getHardwareName() : '',
                    'uname' => $uname,
                ],
            ]);
        }
        $jsonrpc->setCb_class('RepoMarket');
        $jsonrpc->setCb_function('postJsonRpc');
        $jsonrpc->setNoSslCheck(true);
        return $jsonrpc;
    }

    /*     * ***********************BACKUP*************************** */

    public static function getPassword()
    {
        $password = ConfigManager::byKey('market::password');
        if (!is_sha1($password)) {
            return sha1($password);
        }
        return $password;
    }

    /**
     *
     * @param array $_arrayMarket
     * @return self
     */
    public static function construct(array $_arrayMarket)
    {
        $market = new self();
        if (!isset($_arrayMarket['id'])) {
            return null;
        }
        $market->setId($_arrayMarket['id'])
            ->setName($_arrayMarket['name'])
            ->setType($_arrayMarket['type']);
        $market->datetime = json_encode($_arrayMarket['datetime'], JSON_UNESCAPED_UNICODE);
        $market->setDescription($_arrayMarket['description'])
            ->setDownloaded($_arrayMarket['downloaded'])
            ->setUser_id($_arrayMarket['user_id'])
            ->setVersion($_arrayMarket['version'])
            ->setCategorie($_arrayMarket['categorie']);
        $market->status = json_encode($_arrayMarket['status'], JSON_UNESCAPED_UNICODE);
        $market->setAuthor($_arrayMarket['author']);
        if (isset($_arrayMarket['changelog'])) {
            $market->setChangelog($_arrayMarket['changelog']);
        }
        if (isset($_arrayMarket['doc'])) {
            $market->setDoc($_arrayMarket['doc']);
        }
        $market->setLogicalId($_arrayMarket['logicalId']);
        if (isset($_arrayMarket['utilization'])) {
            $market->setUtilization($_arrayMarket['utilization']);
        }
        if (isset($_arrayMarket['certification'])) {
            $market->setCertification($_arrayMarket['certification']);
        }
        if (isset($_arrayMarket['allowVersion'])) {
            $market->setAllowVersion($_arrayMarket['allowVersion']);
        }
        if (isset($_arrayMarket['nbInstall'])) {
            $market->setNbInstall($_arrayMarket['nbInstall']);
        }
        $market->setPurchase($_arrayMarket['purchase'])
            ->setCost($_arrayMarket['cost']);
        $market->rating = ($_arrayMarket['rating']);
        $market->setBuyer($_arrayMarket['buyer'])
            ->setUpdateBy($_arrayMarket['updateBy'])
            ->setPrivate($_arrayMarket['private']);
        $market->img = json_encode($_arrayMarket['img'], JSON_UNESCAPED_UNICODE);
        $market->link = json_encode($_arrayMarket['link'], JSON_UNESCAPED_UNICODE);
        $market->language = json_encode($_arrayMarket['language'], JSON_UNESCAPED_UNICODE);
        if (isset($_arrayMarket['hardwareCompatibility'])) {
            $market->hardwareCompatibility = json_encode($_arrayMarket['hardwareCompatibility'], JSON_UNESCAPED_UNICODE);
        }

        $market->setRealcost($_arrayMarket['realCost']);
        if (!isset($_arrayMarket['isAuthor'])) {
            $_arrayMarket['isAuthor'] = true;
        }
        $market->setIsAuthor($_arrayMarket['isAuthor']);

        if (isset($_arrayMarket['parameters']) && is_array($_arrayMarket['parameters'])) {
            foreach ($_arrayMarket['parameters'] as $key => $value) {
                $market->setParameters($key, $value);
            }
        }
        return $market;
    }

    public static function byLogicalId($_logicalId)
    {
        $market = self::getJsonRpc();

        if (is_array($_logicalId)) {
            $options = $_logicalId;
            $timeout = 240;
        } else {
            $options = ['logicalId' => $_logicalId];
            $timeout = 10;
        }
        if ($market->sendRequest('market::byLogicalId', $options, $timeout, null, 1)) {
            if (is_array($_logicalId)) {
                $result = [];
                foreach ($market->getResult() as $logicalId => $result) {
                    if (isset($result['id'])) {
                        $result[$logicalId] = self::construct($result);
                    }
                }
                return $result;
            }
            return self::construct($market->getResult());
        } else {
            throw new CoreException($market->getError(), $market->getErrorCode());
        }
    }

    public function getDatetime($_key = '', $_default = '')
    {
        return utils::getJsonAttr($this->datetime, $_key, $_default);
    }

    public function setDatetime($_key, $_value)
    {
        $this->datetime = utils::setJsonAttr($this->datetime, $_key, $_value);
        return $this;
    }

    public function getIsAuthor()
    {
        return $this->isAuthor;
    }

    public function setIsAuthor($isAuthor)
    {
        $this->isAuthor = $isAuthor;
        return $this;
    }

    public function getType()
    {
        return $this->type;
    }

    public function setType($type)
    {
        $this->type = $type;
        return $this;
    }

    public function getLogicalId()
    {
        return $this->logicalId;
    }

    public function setLogicalId($logicalId)
    {
        $this->logicalId = $logicalId;
        return $this;
    }

    /**
     * @param Update $_update
     * @return array
     * @throws CoreException
     */
    public static function downloadObject($_update)
    {
        $market = self::byLogicalIdAndType($_update->getLogicalId(), $_update->getType());
        if (is_object($market)) {
            $file = $market->install($_update->getConfiguration('version', 'stable'));
        } else {
            throw new CoreException(__('Objet introuvable sur le market : ') . $_update->getLogicalId() . '/' . $_update->getType());
        }
        return ['path' => $file, 'localVersion' => $market->getDatetime($_update->getConfiguration('version', 'stable'))];
    }

    /*     * ***********************CRON*************************** */

    public function install($_version = 'stable')
    {
        $tmp_dir = NextDomHelper::getTmpFolder('market');
        $tmp = $tmp_dir . '/' . $this->getLogicalId() . '.zip';
        if (file_exists($tmp)) {
            unlink($tmp);
        }
        if (!is_writable($tmp_dir)) {
            exec(SystemHelper::getCmdSudo() . 'chmod 777 -R ' . $tmp);
        }
        if (!is_writable($tmp_dir)) {
            throw new CoreException(__('Impossible d\'écrire dans le répertoire : ') . $tmp . __('. Exécuter la commande suivante en SSH : sudo chmod 777 -R ') . $tmp_dir);
        }

        $url = ConfigManager::byKey('market::address') . "/core/php/downloadFile.php?id=" . $this->getId() . '&version=' . $_version . '&nextdomversion=' . NextDomHelper::getNextdomVersion() . '&hwkey=' . NextDomHelper::getHardwareKey() . '&username=' . urlencode(ConfigManager::byKey('market::username')) . '&password=' . self::getPassword() . '&password_type=sha1';
        LogHelper::addAlert(LogTarget::UPDATE, __('Téléchargement de ') . $this->getLogicalId() . '...');
        exec('wget --no-check-certificate "' . $url . '" -O ' . $tmp . ' 2>&1 >> ' . LogHelper::getPathToLog('update'));
        switch ($this->getType()) {
            case 'plugin':
                return $tmp;
                break;
            default:
                LogHelper::addAlert(LogTarget::UPDATE, __('Installation des plugin, widget, scénario...'));
                $type = $this->getType();
                if (class_exists($type) && method_exists($type, 'getFromMarket')) {
                    $type::getFromMarket($this, $tmp);
                }
                LogHelper::addAlert(LogTarget::UPDATE, __("OK\n"));
                break;
        }
        return false;
    }

    public function getId()
    {
        return $this->id;
    }

    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /*     * ***********************INFO*************************** */

    public static function deleteObjet($_update)
    {
        try {
            $market = self::byLogicalIdAndType($_update->getLogicalId(), $_update->getType());
        } catch (\Exception $e) {
            $market = new RepoMarket();
            $market->setLogicalId($_update->getLogicalId());
            $market->setType($_update->getType());
        }
        try {
            if (is_object($market)) {
                $market->remove();
            }
        } catch (\Exception $e) {

        }
    }

    /*     * ***********************UTILS*************************** */

    public function remove()
    {
        $cache = CacheManager::byKey('market::info::' . $this->getLogicalId());
        if (is_object($cache)) {
            $cache->remove();
        }
        switch ($this->getType()) {
            case 'plugin':

                break;
            default:
                $type = $this->getType();
                if (class_exists($type) && method_exists($type, 'removeFromMarket')) {
                    $type::removeFromMarket($this);
                }
                break;
        }
    }

    /**
     * @param Update $_update
     * @return array
     * @throws \Exception
     */
    public static function objectInfo($_update)
    {
        $url = 'https://nextdom.github.io/documentation/plugins/' . $_update->getLogicalId() . '/' . ConfigManager::byKey('language', 'core', 'fr_FR') . '/index.html';
        if ($_update->getConfiguration('third_plugin', null) === null) {
            $_update->setConfiguration('third_plugin', 0);
            $header = get_headers($url);
            if (strpos($header[0], '200') === false) {
                $_update->setConfiguration('third_plugin', 1);
                $url = 'https://nextdom.github.io/documentation/third_plugin/' . $_update->getLogicalId() . '/' . ConfigManager::byKey('language', 'core', 'fr_FR') . '/index.html';
            }
            $_update->save();
        } elseif ($_update->getConfiguration('third_plugin', 0) == 1) {
            $url = 'https://nextdom.github.io/documentation/third_plugin/' . $_update->getLogicalId() . '/' . ConfigManager::byKey('language', 'core', 'fr_FR') . '/index.html';
        }
        return [
            'doc' => $url,
            'changelog' => $url . '#_changelog',
            'display' => 'https://www.jeedom.fr/market/index.php?v=d&p=market&type=plugin&plugin_id=' . $_update->getLogicalId(),
        ];
    }

    public static function backup_send($_path)
    {
        if (ConfigManager::byKey('market::backupServer') == '' || ConfigManager::byKey('market::backupPassword') == '') {
            throw new CoreException(__('Aucun serveur de backup defini. Avez vous bien un abonnement au backup cloud ?'));
        }
        if (ConfigManager::byKey('market::cloud::backup::password') == '') {
            throw new CoreException(__('Vous devez obligatoirement avoir un mot de passe pour le backup cloud'));
        }
        shell_exec(SystemHelper::getCmdSudo() . ' rm -rf /tmp/duplicity-*-tempdir');
        self::backup_createFolderIsNotExist();
        self::backup_install();
        $baseDir = realpath(__DIR__ . '/../../');
        if (!file_exists($baseDir . '/tmp')) {
            mkdir($baseDir . '/tmp');
        }
        $excludes = [
            $baseDir . '/tmp',
            $baseDir . '/log',
            $baseDir . '/backup',
            $baseDir . '/doc',
            $baseDir . '/docs',
            $baseDir . '/plugins/*/doc',
            $baseDir . '/plugins/*/docs',
            $baseDir . '/tests',
            $baseDir . '/.git',
            $baseDir . '/.log',
            $baseDir . '/core/config/common.config.php',
            $baseDir . '/' . ConfigManager::byKey('backup::path'),
        ];
        if (ConfigManager::byKey('recordDir', 'camera') != '') {
            $excludes[] = $baseDir . '/' . ConfigManager::byKey('recordDir', 'camera');
        }
        $cmd = SystemHelper::getCmdSudo() . ' PASSPHRASE="' . ConfigManager::byKey('market::cloud::backup::password') . '"';
        $cmd .= ' duplicity incremental --full-if-older-than ' . ConfigManager::byKey('market::cloud::backup::fullfrequency', 'core', '1M');
        foreach ($excludes as $exclude) {
            $cmd .= ' --exclude "' . $exclude . '"';
        }
        $cmd .= ' --num-retries 2';
        $cmd .= ' --ssl-no-check-certificate';
        $cmd .= ' --tempdir ' . $baseDir;
        $cmd .= ' ' . $baseDir . '  "webdavs://' . ConfigManager::byKey('market::username') . ':' . ConfigManager::byKey('market::backupPassword');
        $cmd .= '@' . ConfigManager::byKey('market::backupServer') . '/remote.php/webdav/' . ConfigManager::byKey('market::cloud::backup::name') . '"';
        try {
            ComShell::execute($cmd);
        } catch (\Exception $e) {
            if (self::backup_errorAnalyzed($e->getMessage()) != null) {
                throw new CoreException('[backup cloud] ' . self::backup_errorAnalyzed($e->getMessage()));
            }
            if (strpos($e->getMessage(), 'Insufficient Storage') !== false) {
                self::backup_clean();
            }
            SystemHelper::kill('duplicity');
            shell_exec(SystemHelper::getCmdSudo() . ' rm -rf ' . $baseDir . '/tmp/duplicity*');
            shell_exec(SystemHelper::getCmdSudo() . ' rm -rf ~/.cache/duplicity/*');
            ComShell::execute($cmd);
        }
    }

    public static function backup_createFolderIsNotExist()
    {
        $client = new \Sabre\DAV\Client([
            'baseUri' => 'https://' . ConfigManager::byKey('market::backupServer'),
            'userName' => ConfigManager::byKey('market::username'),
            'password' => ConfigManager::byKey('market::backupPassword'),
        ]);
        $adapter = new \League\Flysystem\WebDAV\WebDAVAdapter($client);
        $filesystem = new \League\Flysystem\Filesystem($adapter);
        $folders = $filesystem->listContents('/remote.php/webdav/');
        $found = false;
        if (count($folders) > 0) {
            foreach ($folders as $folder) {
                if ($folder['basename'] == ConfigManager::byKey('market::cloud::backup::name')) {
                    $found = true;
                    break;
                }
            }
        }
        if (!$found) {
            $filesystem->createDir('/remote.php/webdav/' . ConfigManager::byKey('market::cloud::backup::name'));
        }
    }

    public static function backup_install()
    {
        if (exec('which duplicity | wc -l') == 0) {
            try {
                ComShell::execute('sudo apt-get -y install duplicity');
            } catch (\Exception $e) {

            }
        }
    }

    public static function backup_errorAnalyzed($_error)
    {
        if (strpos($_error, 'decryption failed: Bad session key') !== false) {
            return __('Clé de chiffrement invalide. Si vous oubliez votre mot de passe aucune récupération n\'est possible. Veuillez supprimer le backup à partir de votre page profil sur le market');
        }
        return null;
    }

    public static function backup_clean($_nb = null)
    {
        if (ConfigManager::byKey('market::backupServer') == '' || ConfigManager::byKey('market::backupPassword') == '') {
            return;
        }
        if (ConfigManager::byKey('market::cloud::backup::password') == '') {
            return;
        }
        self::backup_install();
        shell_exec(SystemHelper::getCmdSudo() . ' rm -rf /tmp/duplicity-*-tempdir');
        if ($_nb == null) {
            $_nb = 0;
            $lists = self::backup_list();
            foreach ($lists as $name) {
                if (strpos($name, 'Full') !== false) {
                    $_nb++;
                }
            }
            $_nb = ($_nb - 2 < 1) ? 1 : $_nb - 2;
        }
        $cmd = SystemHelper::getCmdSudo() . ' PASSPHRASE="' . ConfigManager::byKey('market::cloud::backup::password') . '"';
        $cmd .= ' duplicity remove-all-but-n-full ' . $_nb . ' --force ';
        $cmd .= ' --ssl-no-check-certificate';
        $cmd .= ' --num-retries 1';
        $cmd .= ' "webdavs://' . ConfigManager::byKey('market::username') . ':' . ConfigManager::byKey('market::backupPassword');
        $cmd .= '@' . ConfigManager::byKey('market::backupServer') . '/remote.php/webdav/' . ConfigManager::byKey('market::cloud::backup::name') . '"';
        try {
            ComShell::execute($cmd);
        } catch (\Exception $e) {
            if (self::backup_errorAnalyzed($e->getMessage()) != null) {
                throw new CoreException('[restore cloud] ' . self::backup_errorAnalyzed($e->getMessage()));
            }
            throw new CoreException('[restore cloud] ' . $e->getMessage());
        }
    }

    public static function backup_list()
    {
        if (ConfigManager::byKey('market::backupServer') == '' || ConfigManager::byKey('market::backupPassword') == '') {
            return [];
        }
        if (ConfigManager::byKey('market::cloud::backup::password') == '') {
            return [];
        }
        self::backup_createFolderIsNotExist();
        self::backup_install();
        $backupList = [];
        $cmd = SystemHelper::getCmdSudo();
        $cmd .= ' duplicity collection-status';
        $cmd .= ' --ssl-no-check-certificate';
        $cmd .= ' --num-retries 1';
        $cmd .= ' --timeout 60';
        $cmd .= ' "webdavs://' . ConfigManager::byKey('market::username') . ':' . ConfigManager::byKey('market::backupPassword');
        $cmd .= '@' . ConfigManager::byKey('market::backupServer') . '/remote.php/webdav/' . ConfigManager::byKey('market::cloud::backup::name') . '"';
        try {
            $results = explode("\n", ComShell::execute($cmd));
        } catch (\Exception $e) {
            shell_exec(SystemHelper::getCmdSudo() . ' rm -rf ~/.cache/duplicity/*');
            $results = explode("\n", ComShell::execute($cmd));
        }
        foreach ($results as $line) {
            if (strpos($line, 'Full') === false && strpos($line, 'Incremental') === false && strpos($line, 'Complète') === false && strpos($line, 'Incrémentale') === false) {
                continue;
            }
            $backupList[] = trim(substr($line, 0, -1));
        }
        return array_reverse($backupList);
    }

    public static function backup_restore($_backup)
    {
        $backup_dir = calculPath(ConfigManager::byKey('backup::path'));
        if (!file_exists($backup_dir)) {
            mkdir($backup_dir, 0770, true);
        }
        if (!is_writable($backup_dir)) {
            throw new CoreException('Impossible d\'accéder au dossier de sauvegarde. Veuillez vérifier les droits : ' . $backup_dir);
        }
        $restore_dir = '/tmp/nextdom_cloud_restore';
        if (file_exists($restore_dir)) {
            ComShell::execute(SystemHelper::getCmdSudo() . ' rm -rf ' . $restore_dir);
        }
        self::backup_install();
        $base_dir = '/usr/jeedom_duplicity';
        if (!file_exists($base_dir)) {
            mkdir($base_dir);
        }
        mkdir($restore_dir);
        $timestamp = strtotime(trim(str_replace(['Full', 'Incremental'], '', $_backup)));
        $backup_name = str_replace(' ', '_', 'backup-cloud-' . ConfigManager::byKey('market::cloud::backup::name') . '-' . date("Y-m-d-H\hi", $timestamp) . '.tar.gz');
        $cmd = SystemHelper::getCmdSudo() . ' PASSPHRASE="' . ConfigManager::byKey('market::cloud::backup::password') . '"';
        $cmd .= ' duplicity --file-to-restore /';
        $cmd .= ' --time ' . $timestamp;
        $cmd .= ' --num-retries 1';
        $cmd .= ' --tempdir ' . $base_dir . '/tmp';
        $cmd .= ' "webdavs://' . ConfigManager::byKey('market::username') . ':' . ConfigManager::byKey('market::backupPassword');
        $cmd .= '@' . ConfigManager::byKey('market::backupServer') . '/remote.php/webdav/' . ConfigManager::byKey('market::cloud::backup::name') . '"';
        $cmd .= ' ' . $restore_dir;
        try {
            ComShell::execute($cmd);
        } catch (\Exception $e) {
            if (self::backup_errorAnalyzed($e->getMessage()) != null) {
                throw new CoreException('[restore cloud] ' . self::backup_errorAnalyzed($e->getMessage()));
            }
            throw new CoreException('[restore cloud] ' . $e->getMessage());
        }
        shell_exec(SystemHelper::getCmdSudo() . ' rm -rf ' . $base_dir);
        system('cd ' . $restore_dir . ';tar cfz "' . $backup_dir . '/' . $backup_name . '" . > /dev/null');
        if (file_exists($restore_dir)) {
            ComShell::execute(SystemHelper::getCmdSudo() . ' rm -rf ' . $restore_dir);
        }
        BackupManager::restore($backup_dir . '/' . $backup_name, true);
    }

    public static function cronHourly()
    {
        if (strtotime(ConfigManager::byKey('market::lastCommunication', 'core', 0)) > (strtotime('now') - (24 * 3600))) {
            return;
        }
        sleep(rand(0, 1800));
        try {
            self::test();
        } catch (\Exception $e) {

        }
    }

    public static function test()
    {
        $market = self::getJsonRpc();
        if ($market->sendRequest('market::test')) {
            return $market->getResult();
        } else {
            throw new CoreException($market->getError(), $market->getErrorCode());
        }
    }

    public static function sendTunnelClientId($_client_id) {
        $market = self::getJsonRpc();
        if ($market->sendRequest('service::tunnel::setClientId',array('client_id' => $_client_id))) {
            return $market->getResult();
        } else {
            throw new CoreException($market->getError(), $market->getErrorCode());
        }
    }

    public static function cron5()
    {
        try {
            $monitoring_state = self::monitoring_status();
            if (self::monitoring_allow() && !$monitoring_state) {
                self::monitoring_start();
            }
            if (!self::monitoring_allow() && $monitoring_state) {
                self::monitoring_stop();
            }
        } catch (\Exception $e) {

        }
    }

    public static function sendHealth(){
        $market = self::getJsonRpc();
        if (!$market->sendRequest('register::health', ['health' => SystemHealth::health()])) {
            throw new CoreException($market->getError(), $market->getErrorCode());
        }
    }

    public static function monitoring_status()
    {
        if (!file_exists('/etc/zabbix/zabbix_agentd.conf')) {
            return false;
        }
        if (exec('grep "jeedom.com" /etc/zabbix/zabbix_agentd.conf | grep -v "zabbix.jeedom.com" | wc -l') == 0) {
            return false;
        }
        return (count(SystemHelper::ps('zabbix')) > 0);
    }

    public static function monitoring_allow()
    {
        if (ConfigManager::byKey('market::monitoringServer') == '') {
            return false;
        }
        if (ConfigManager::byKey('market::monitoringName') == '') {
            return false;
        }
        return true;
    }

    public static function monitoring_start()
    {
        preg_match_all('/(\d\.\d\.\d)/m', shell_exec(SystemHelper::getCmdSudo() . ' zabbix_agentd -V'), $matches);
        self::monitoring_install();
        if(!file_exists('/etc/zabbix/zabbix_agentd.conf')){
            return;
        }
        $cmd = SystemHelper::getCmdSudo() . " chmod -R 777 /etc/zabbix;";
        $cmd .= SystemHelper::getCmdSudo() . " sed -i '/ServerActive=/d' /etc/zabbix/zabbix_agentd.conf;";
        $cmd .= SystemHelper::getCmdSudo() . " sed -i '/Hostname=/d' /etc/zabbix/zabbix_agentd.conf;";
        $cmd .= SystemHelper::getCmdSudo() . " sed -i '/TLSConnect=/d' /etc/zabbix/zabbix_agentd.conf;";
        $cmd .= SystemHelper::getCmdSudo() . " sed -i '/TLSAccept=/d' /etc/zabbix/zabbix_agentd.conf;";
        $cmd .= SystemHelper::getCmdSudo() . " sed -i '/TLSPSKIdentity=/d' /etc/zabbix/zabbix_agentd.conf;";
        $cmd .= SystemHelper::getCmdSudo() . " sed -i '/TLSPSKFile=/d' /etc/zabbix/zabbix_agentd.conf;";
        $cmd .= SystemHelper::getCmdSudo() . ' echo "ServerActive=' . ConfigManager::byKey('market::monitoringServer') . '" >> /etc/zabbix/zabbix_agentd.conf;';
        $cmd .= SystemHelper::getCmdSudo() . ' echo "Hostname=' . ConfigManager::byKey('market::monitoringName') . '" >> /etc/zabbix/zabbix_agentd.conf;';
        if (!isset($matches[0]) || !isset($matches[0][0]) || version_compare($matches[0][0], '3.0.0') >= 0) {
            $cmd .= SystemHelper::getCmdSudo() . ' echo "TLSConnect=psk" >> /etc/zabbix/zabbix_agentd.conf;';
            $cmd .= SystemHelper::getCmdSudo() . ' echo "TLSAccept=psk" >> /etc/zabbix/zabbix_agentd.conf;';
            $cmd .= SystemHelper::getCmdSudo() . ' echo "TLSPSKIdentity=' . ConfigManager::byKey('market::monitoringPskIdentity') . '" >> /etc/zabbix/zabbix_agentd.conf;';
            $cmd .= SystemHelper::getCmdSudo() . ' echo "TLSPSKFile=/etc/zabbix/zabbix_psk" >> /etc/zabbix/zabbix_agentd.conf;';
            $cmd .= SystemHelper::getCmdSudo() . ' echo "' . ConfigManager::byKey('market::monitoringPsk') . '" > /etc/zabbix/zabbix_psk;';
        }
        if (!file_exists('/var/log/zabbix')) {
            $cmd .= SystemHelper::getCmdSudo() . ' mkdir /var/log/zabbix;';
        }
        $cmd .= SystemHelper::getCmdSudo() . ' chmod 777 -R /var/log/zabbix;';
        if (!file_exists('/var/log/zabbix-agent')) {
            $cmd .= SystemHelper::getCmdSudo() . ' mkdir /var/log/zabbix-agent;';
        }
        $cmd .= SystemHelper::getCmdSudo() . ' chmod 777 -R /var/log/zabbix-agent;';
        if (!file_exists('/etc/zabbix/zabbix_agentd.conf.d')) {
            $cmd .= SystemHelper::getCmdSudo() . ' mkdir /etc/zabbix/zabbix_agentd.conf.d;';
            $cmd .= SystemHelper::getCmdSudo() . ' chmod 777 -R /etc/zabbix/zabbix_agentd.conf.d;';
        }
        $cmd .= SystemHelper::getCmdSudo() . ' systemctl restart zabbix-agent;';
        $cmd .= SystemHelper::getCmdSudo() . ' systemctl enable zabbix-agent;';
        shell_exec($cmd);
    }

    /******************************MONITORING********************************/

    public static function monitoring_install()
    {
        if (file_exists('/etc/zabbix')) {
            return;
        }
        $logfile = LogHelper::getPathToLog('market_zabbix_installation');
        if (strpos(php_uname(), 'x86_64') !== false) {
            if (file_exists('/etc/debian_version')) {
                $deb_version = file_get_contents('/etc/debian_version');
                if (version_compare($deb_version, '9', '>=')) {
                    shell_exec('cd /tmp/;' . SystemHelper::getCmdSudo() . ' wget http://repo.zabbix.com/zabbix/4.0/debian/pool/main/z/zabbix-release/zabbix-release_4.0-2%2Bstretch_all.deb >> ' . $logfile . ' 2>&1;' . SystemHelper::getCmdSudo() . ' dpkg -i zabbix-release_3.4-1+stretch_all.deb  >> ' . $logfile . ' 2>&1;' . SystemHelper::getCmdSudo() . ' rm zabbix-release_3.4-1+stretch_all.deb  >> ' . $logfile . ' 2>&1');
                } else {
                    shell_exec('cd /tmp/;' . SystemHelper::getCmdSudo() . ' wget http://repo.zabbix.com/zabbix/4.0/debian/pool/main/z/zabbix-release/zabbix-release_4.0-2%2Bjessie_all.deb  >> ' . $logfile . ' 2>&1;' . SystemHelper::getCmdSudo() . ' dpkg -i zabbix-release_3.4-1+jessie_all.deb  >> ' . $logfile . ' 2>&1;' . SystemHelper::getCmdSudo() . ' rm zabbix-release_3.4-1+jessie_all.deb  >> ' . $logfile . ' 2>&1');
                }
            }
        }
        shell_exec(SystemHelper::getCmdSudo() . ' apt-get update  >> ' . $logfile . ' 2>&1');
        shell_exec(SystemHelper::getCmdSudo() . ' apt-get -y install zabbix-agent  >> ' . $logfile . ' 2>&1');
    }

    /*     * *********************Methode d'instance************************* */

    public static function monitoring_stop()
    {
        $cmd = SystemHelper::getCmdSudo() . ' systemctl stop zabbix-agent;';
        $cmd .= SystemHelper::getCmdSudo() . ' systemctl disable zabbix-agent;';
        shell_exec($cmd);
    }

    /*******************************health********************************/

    public static function health()
    {
        $return = [];
        if (ConfigManager::byKey('market::monitoringServer') != '') {
            $monitoring_state = self::monitoring_status();
            $return[] = [
                'name' => __('Cloud monitoring actif'),
                'state' => $monitoring_state,
                'result' => ($monitoring_state) ? __('OK') : __('NOK'),
                'comment' => ($monitoring_state) ? '' : __('Attendez 10 minutes si le service ne redémarre pas contacter le support'),
            ];
        }
        return $return;
    }

    public static function saveTicket($_ticket)
    {
        $jsonrpc = self::getJsonRpc();
        $_ticket['user_plugin'] = '';
        foreach (PluginManager::listPlugin() as $plugin) {
            $_ticket['user_plugin'] .= $plugin->getId();
            $update = $plugin->getUpdate();
            if (is_object($update)) {
                $_ticket['user_plugin'] .= '[' . $update->getConfiguration('version', 'stable') . ',' . $update->getSource() . ',' . $update->getLocalVersion() . ']';
            }
            $_ticket['user_plugin'] .= ',';
        }
        $_ticket['user_plugin'] = trim($_ticket['user_plugin'], ',');
        if (isset($_ticket['options']['page'])) {
            $_ticket['options']['page'] = substr($_ticket['options']['page'], strpos($_ticket['options']['page'], 'index.php'));
        }
        $_ticket['options']['nextdom_version'] = NextDomHelper::getNextdomVersion();
        $_ticket['options']['uname'] = SystemHelper::getSystemInformations();
        if (!$jsonrpc->sendRequest('ticket::save', ['ticket' => $_ticket], 300)) {
            throw new CoreException($jsonrpc->getErrorMessage());
        }
        if ($_ticket['openSupport'] == 1) {
            UserManager::supportAccess(true);
        }
        return $jsonrpc->getResult();
    }

    public static function supportAccess($_enable = true, $_key = '')
    {
        $jsonrpc = self::getJsonRpc();
        $url = NetworkHelper::getNetworkAccess('external') . '/index.php?auth=' . $_key;
        if (!$jsonrpc->sendRequest('register::supportAccess', ['enable' => $_enable, 'urlSupport' => $url])) {
            throw new CoreException($jsonrpc->getErrorMessage());
        }
    }

    public static function getPurchaseInfo()
    {
        $market = self::getJsonRpc();
        if ($market->sendRequest('purchase::getInfo')) {
            return $market->getResult();
        }
        return null;
    }

    /*     * **********************Getteur Setteur*************************** */

    public static function distinctCategorie($_type)
    {
        $market = self::getJsonRpc();
        if ($market->sendRequest('market::distinctCategorie', ['type' => $_type])) {
            return $market->getResult();
        } else {
            throw new CoreException($market->getError(), $market->getErrorCode());
        }
    }

    public static function postJsonRpc(&$_result)
    {
        ConfigManager::save('market::lastCommunication', date(DateFormat::FULL));
        if (is_array($_result)) {
            $restart_dns = false;
            $restart_monitoring = false;
            if (isset($_result['register::dnsToken']) && ConfigManager::byKey('dns::token') != $_result['register::dnsToken']) {
                ConfigManager::save('dns::token', $_result['register::dnsToken']);
                $restart_dns = true;
            }
            if (isset($_result['register::dnsNumber']) && ConfigManager::byKey('dns::number') != $_result['register::dnsNumber']) {
                ConfigManager::save('dns::number', $_result['register::dnsNumber']);
                $restart_dns = true;
            }
            if (isset($_result['register::vpnurl']) && ConfigManager::byKey('dns::vpnurl') != $_result['register::vpnurl']) {
                ConfigManager::save('dns::vpnurl', $_result['register::vpnurl']);
                $restart_dns = true;
            }
            if (isset($_result['register::vpnPort']) && ConfigManager::byKey('vpn::port') != $_result['register::vpnPort']) {
                ConfigManager::save('vpn::port', $_result['register::vpnPort']);
                $restart_dns = true;
            }
            if (isset($_result['user::backupServer']) && ConfigManager::byKey('market::backupServer') != $_result['user::backupServer']) {
                ConfigManager::save('market::backupServer', $_result['user::backupServer']);
                $restart_monitoring = true;
            }
            if (isset($_result['user::backupPassword']) && ConfigManager::byKey('market::backupPassword') != $_result['user::backupPassword']) {
                ConfigManager::save('market::backupPassword', $_result['user::backupPassword']);
                $restart_monitoring = true;
            }
            if (isset($_result['user::monitoringServer']) && ConfigManager::byKey('market::monitoringServer') != $_result['user::monitoringServer']) {
                ConfigManager::save('market::monitoringServer', $_result['user::monitoringServer']);
                $restart_monitoring = true;
            }
            if (isset($_result['register::monitoringPsk']) && ConfigManager::byKey('market::monitoringPsk') != $_result['register::monitoringPsk']) {
                ConfigManager::save('market::monitoringPsk', $_result['register::monitoringPsk']);
                $restart_monitoring = true;
            }
            if (isset($_result['register::monitoringPskIdentity']) && ConfigManager::byKey('market::monitoringPskIdentity') != $_result['register::monitoringPskIdentity']) {
                ConfigManager::save('market::monitoringPskIdentity', $_result['register::monitoringPskIdentity']);
                $restart_monitoring = true;
            }
            if (isset($_result['register::monitoringName']) && ConfigManager::byKey('market::monitoringName') != $_result['register::monitoringName']) {
                ConfigManager::save('market::monitoringName', $_result['register::monitoringName']);
                $restart_monitoring = true;
            }
            if ($restart_monitoring) {
                self::monitoring_stop();
            }
            if ($restart_dns && ConfigManager::byKey('market::allowDNS') == 1) {
                DnsHelper::dnsStart();
            }
            if (ConfigManager::byKey('market::allowDNS') == 1 && isset($_result['NextDomHelper::url']) && ConfigManager::byKey('NextDomHelper::url') != $_result['NextDomHelper::url']) {
                ConfigManager::save('NextDomHelper::url', $_result['NextDomHelper::url']);
            }
            if (isset($_result['register::hwkey_nok']) && $_result['register::hwkey_nok'] == 1) {
                ConfigManager::save('NextDomHelper::installKey', '');
            }
        }
    }

    public static function byId($_id)
    {
        $market = self::getJsonRpc();
        if ($market->sendRequest('market::byId', ['id' => $_id])) {
            return self::construct($market->getResult());
        } else {
            throw new CoreException($market->getError(), $market->getErrorCode());
        }
    }

    public static function byMe()
    {
        $market = self::getJsonRpc();
        if ($market->sendRequest('market::byAuthor', [])) {
            $return = [];
            foreach ($market->getResult() as $result) {
                if (isset($result['id'])) {
                    $return[] = self::construct($result);
                }
            }
            return $return;
        } else {
            throw new CoreException($market->getError(), $market->getErrorCode());
        }
    }

    public static function byStatusAndType($_status, $_type)
    {
        $market = self::getJsonRpc();
        if ($market->sendRequest('market::byStatusAndType', ['status' => $_status, 'type' => $_type])) {
            $return = [];
            foreach ($market->getResult() as $result) {
                if (isset($result['id'])) {
                    $return[] = self::construct($result);
                }
            }
            return $return;
        } else {
            LogHelper::addDebug(LogTarget::MARKET, print_r($market, true));
            throw new CoreException($market->getError(), $market->getErrorCode());
        }
    }

    public static function byStatus($_status)
    {
        $market = self::getJsonRpc();
        if ($market->sendRequest('market::byStatus', ['status' => $_status])) {
            $return = [];
            foreach ($market->getResult() as $result) {
                if (isset($result['id'])) {
                    $return[] = self::construct($result);
                }
            }
            return $return;
        } else {
            throw new CoreException($market->getError(), $market->getErrorCode());
        }
    }

    public static function byFilter($_filter)
    {
        $market = self::getJsonRpc();
        if ($market->sendRequest('market::byFilter', $_filter)) {
            $return = [];
            foreach ($market->getResult() as $result) {
                if (isset($result['id'])) {
                    $return[] = self::construct($result);
                }
            }
            return $return;
        } else {
            throw new CoreException($market->getError(), $market->getErrorCode());
        }
    }

    public function getRating($_key = 'average')
    {
        $rating = $this->rating;
        if (isset($rating[$_key])) {
            return $rating[$_key];
        }
        return 0;
    }

    public function setRating($_rating)
    {
        $market = self::getJsonRpc();
        if (!$market->sendRequest('market::setRating', ['rating' => $_rating, 'id' => $this->getId()])) {
            throw new CoreException($market->getError());
        }
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    public function getDescription()
    {
        return $this->description;
    }

    public function setDescription($description)
    {
        $this->description = $description;
        return $this;
    }

    public function getCategorie()
    {
        return $this->categorie;
    }

    public function setCategorie($categorie)
    {
        $this->categorie = $categorie;
        return $this;
    }

    public function getVersion()
    {
        return $this->version;
    }

    public function setVersion($version)
    {
        $this->version = $version;
        return $this;
    }

    public function getUser_id()
    {
        return $this->user_id;
    }

    public function setUser_id($user_id)
    {
        $this->user_id = $user_id;
        return $this;
    }

    public function getDownloaded()
    {
        return $this->downloaded;
    }

    public function setDownloaded($downloaded)
    {
        $this->downloaded = $downloaded;
        return $this;
    }

    public function getStatus($_key = '', $_default = '')
    {
        return utils::getJsonAttr($this->status, $_key, $_default);
    }

    public function setStatus($_key, $_value)
    {
        $this->status = utils::setJsonAttr($this->status, $_key, $_value);
        return $this;
    }

    public function getLink($_key = '', $_default = '')
    {
        return utils::getJsonAttr($this->link, $_key, $_default);
    }

    public function setLink($_key, $_value)
    {
        $this->link = utils::setJsonAttr($this->link, $_key, $_value);
        return $this;
    }

    public function getLanguage($_key = '', $_default = '')
    {
        return utils::getJsonAttr($this->language, $_key, $_default);
    }

    public function setLanguage($_key, $_value)
    {
        $this->language = utils::setJsonAttr($this->language, $_key, $_value);
        return $this;
    }

    public function getImg($_key = '', $_default = '')
    {
        return utils::getJsonAttr($this->img, $_key, $_default);
    }

    public function getAuthor()
    {
        return $this->author;
    }

    public function setAuthor($author)
    {
        $this->author = $author;
        return $this;
    }

    public function getChangelog()
    {
        return $this->changelog;
    }

    public function setChangelog($changelog)
    {
        $this->changelog = $changelog;
        return $this;
    }

    public function getNbInstall()
    {
        return $this->nbInstall;
    }

    public function setNbInstall($nbInstall)
    {
        $this->nbInstall = $nbInstall;
        return $this;
    }

    public function getPrivate()
    {
        return $this->private;
    }

    public function setPrivate($private)
    {
        $this->private = $private;
        return $this;
    }

    public function getUtilization()
    {
        return $this->utilization;
    }

    public function setUtilization($utilization)
    {
        $this->utilization = $utilization;
        return $this;
    }

    public function getPurchase()
    {
        return $this->purchase;
    }

    public function setPurchase($purchase)
    {
        $this->purchase = $purchase;
        return $this;
    }

    public function getCost()
    {
        return $this->cost;
    }

    public function setCost($cost)
    {
        $this->cost = $cost;
        return $this;
    }

    public function getRealcost()
    {
        return $this->realcost;
    }

    public function setRealcost($realcost)
    {
        $this->realcost = $realcost;
        return $this;
    }

    public function getBuyer()
    {
        return $this->buyer;
    }

    public function setBuyer($buyer)
    {
        $this->buyer = $buyer;
        return $this;
    }

    public function getCertification()
    {
        return $this->certification;
    }

    public function setCertification($certification)
    {
        $this->certification = $certification;
        return $this;
    }

    public function getDoc()
    {
        return $this->doc;
    }

    public function setDoc($doc)
    {
        $this->doc = $doc;
        return $this;
    }

    public function getUpdateBy()
    {
        return $this->updateBy;
    }

    public function setUpdateBy($updateBy)
    {
        $this->updateBy = $updateBy;
        return $this;
    }

    public function getAllowVersion()
    {
        return $this->allowVersion;
    }

    public function setAllowVersion($allowVersion)
    {
        $this->allowVersion = $allowVersion;
        return $allowVersion;
    }

    public function getHardwareCompatibility($_key = '', $_default = '')
    {
        return utils::getJsonAttr($this->hardwareCompatibility, $_key, $_default);
    }

    public function setHardwareCompatibility($_key, $_value)
    {
        $this->hardwareCompatibility = utils::setJsonAttr($this->hardwareCompatibility, $_key, $_value);
        return $this;
    }

    public function getParameters($_key = '', $_default = '')
    {
        return utils::getJsonAttr($this->parameters, $_key, $_default);
    }

    public function setParameters($_key, $_value)
    {
        $this->parameters = utils::setJsonAttr($this->parameters, $_key, $_value);
        return $this;
    }

}