From 76c7f34e4f5c058857a0638aad0670df88c0beba Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Mon, 30 Oct 2023 08:12:22 +0100 Subject: [PATCH 01/10] Add CI config --- .github/workflows/continuous-integration.yml | 55 ++++++ .phpcs.xml | 17 ++ composer.json | 5 +- composer.lock | 196 ++++++++++++++++++- 4 files changed, 264 insertions(+), 9 deletions(-) create mode 100644 .github/workflows/continuous-integration.yml create mode 100644 .phpcs.xml diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml new file mode 100644 index 0000000..44ec4de --- /dev/null +++ b/.github/workflows/continuous-integration.yml @@ -0,0 +1,55 @@ +name: "Continuous integration" + +on: + push: + branches: + - "master" + - "develop" + tags: + - "*" + pull_request: + workflow_dispatch: + +jobs: + lint: + name: "Lint" + runs-on: "ubuntu-latest" + strategy: + fail-fast: false + matrix: + include: + - {php-version: "7.4"} + - {php-version: "8.0"} + - {php-version: "8.1"} + - {php-version: "8.2"} + - {php-version: "8.3-rc"} + steps: + - name: "Checkout" + uses: "actions/checkout@v4" + - name: "Setup PHP" + uses: "shivammathur/setup-php@v2" + with: + php-version: "${{ matrix.php-version }}" + coverage: "none" + tools: "composer, cs2pr" + - name: "Get Composer cache directory" + id: "composer-cache" + run: | + echo "::set-output name=dir::$(composer config cache-files-dir)" + - name: "Restore dependencies cache" + uses: "actions/cache@v3" + with: + path: "${{ steps.composer-cache.outputs.dir }}" + key: "${{ github.job }}-${{ matrix.php-version }}-dependencies-${{ hashFiles('**/composer.lock') }}" + - name: "Install Composer dependencies" + run: | + composer install --ansi --no-interaction --no-progress --prefer-dist + - name: "PHP Parallel Lint" + run: | + vendor/bin/parallel-lint --colors --checkstyle --exclude ./vendor/ . | cs2pr + - name: "PHP_CodeSniffer" + run: | + vendor/bin/phpcs -q --report=checkstyle | cs2pr + - name: "Check for missing/outdated headers" + run: | + vendor/bin/licence-headers-check --ansi --no-interaction diff --git a/.phpcs.xml b/.phpcs.xml new file mode 100644 index 0000000..6c5742f --- /dev/null +++ b/.phpcs.xml @@ -0,0 +1,17 @@ + + + . + /.git/ + ^vendor/ + + + + + + + + + + + + diff --git a/composer.json b/composer.json index 4d0e2db..a3f3ff3 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,10 @@ "php": ">=7.4" }, "require-dev": { - "glpi-project/tools": "^0.6" + "glpi-project/tools": "^0.7.1", + "php-parallel-lint/php-parallel-lint": "^1.3", + "phpstan/phpstan": "^1.10", + "squizlabs/php_codesniffer": "^3.7" }, "config": { "optimize-autoloader": true, diff --git a/composer.lock b/composer.lock index ff4864b..ec0623d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,27 +4,31 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "49ff730c8068667841d45903c3c04259", + "content-hash": "e0ec25194b0cffa19269747d354d3478", "packages": [], "packages-dev": [ { "name": "glpi-project/tools", - "version": "0.6.4", + "version": "0.7.1", "source": { "type": "git", "url": "https://github.com/glpi-project/tools.git", - "reference": "8ef917fa2967e716eaed198bb803f418a80cd621" + "reference": "4bc5a725d9f4da0ee946ad3cbdd54a782d2f40fb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/glpi-project/tools/zipball/8ef917fa2967e716eaed198bb803f418a80cd621", - "reference": "8ef917fa2967e716eaed198bb803f418a80cd621", + "url": "https://api.github.com/repos/glpi-project/tools/zipball/4bc5a725d9f4da0ee946ad3cbdd54a782d2f40fb", + "reference": "4bc5a725d9f4da0ee946ad3cbdd54a782d2f40fb", "shasum": "" }, "require": { "symfony/console": "^5.4 || ^6.0", "twig/twig": "^3.3" }, + "require-dev": { + "nikic/php-parser": "^4.13", + "phpstan/phpstan-src": "^1.10" + }, "bin": [ "bin/extract-locales", "bin/licence-headers-check", @@ -33,7 +37,7 @@ "type": "library", "autoload": { "psr-4": { - "Glpi\\": "src/" + "GlpiProject\\Tools\\": "src/" } }, "notification-url": "https://packagist.org/downloads/", @@ -57,7 +61,126 @@ "issues": "https://github.com/glpi-project/tools/issues", "source": "https://github.com/glpi-project/tools" }, - "time": "2023-07-27T12:32:25+00:00" + "time": "2023-10-16T11:40:35+00:00" + }, + { + "name": "php-parallel-lint/php-parallel-lint", + "version": "v1.3.2", + "source": { + "type": "git", + "url": "https://github.com/php-parallel-lint/PHP-Parallel-Lint.git", + "reference": "6483c9832e71973ed29cf71bd6b3f4fde438a9de" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-parallel-lint/PHP-Parallel-Lint/zipball/6483c9832e71973ed29cf71bd6b3f4fde438a9de", + "reference": "6483c9832e71973ed29cf71bd6b3f4fde438a9de", + "shasum": "" + }, + "require": { + "ext-json": "*", + "php": ">=5.3.0" + }, + "replace": { + "grogy/php-parallel-lint": "*", + "jakub-onderka/php-parallel-lint": "*" + }, + "require-dev": { + "nette/tester": "^1.3 || ^2.0", + "php-parallel-lint/php-console-highlighter": "0.* || ^1.0", + "squizlabs/php_codesniffer": "^3.6" + }, + "suggest": { + "php-parallel-lint/php-console-highlighter": "Highlight syntax in code snippet" + }, + "bin": [ + "parallel-lint" + ], + "type": "library", + "autoload": { + "classmap": [ + "./src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-2-Clause" + ], + "authors": [ + { + "name": "Jakub Onderka", + "email": "ahoj@jakubonderka.cz" + } + ], + "description": "This tool check syntax of PHP files about 20x faster than serial check.", + "homepage": "https://github.com/php-parallel-lint/PHP-Parallel-Lint", + "support": { + "issues": "https://github.com/php-parallel-lint/PHP-Parallel-Lint/issues", + "source": "https://github.com/php-parallel-lint/PHP-Parallel-Lint/tree/v1.3.2" + }, + "time": "2022-02-21T12:50:22+00:00" + }, + { + "name": "phpstan/phpstan", + "version": "1.10.39", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "d9dedb0413f678b4d03cbc2279a48f91592c97c4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/d9dedb0413f678b4d03cbc2279a48f91592c97c4", + "reference": "d9dedb0413f678b4d03cbc2279a48f91592c97c4", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan - PHP Static Analysis Tool", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "docs": "https://phpstan.org/user-guide/getting-started", + "forum": "https://github.com/phpstan/phpstan/discussions", + "issues": "https://github.com/phpstan/phpstan/issues", + "security": "https://github.com/phpstan/phpstan/security/policy", + "source": "https://github.com/phpstan/phpstan-src" + }, + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://github.com/phpstan", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan", + "type": "tidelift" + } + ], + "time": "2023-10-17T15:46:26+00:00" }, { "name": "psr/container", @@ -107,6 +230,63 @@ }, "time": "2021-11-05T16:50:12+00:00" }, + { + "name": "squizlabs/php_codesniffer", + "version": "3.7.2", + "source": { + "type": "git", + "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", + "reference": "ed8e00df0a83aa96acf703f8c2979ff33341f879" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/ed8e00df0a83aa96acf703f8c2979ff33341f879", + "reference": "ed8e00df0a83aa96acf703f8c2979ff33341f879", + "shasum": "" + }, + "require": { + "ext-simplexml": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" + }, + "bin": [ + "bin/phpcs", + "bin/phpcbf" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Greg Sherwood", + "role": "lead" + } + ], + "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", + "homepage": "https://github.com/squizlabs/PHP_CodeSniffer", + "keywords": [ + "phpcs", + "standards", + "static analysis" + ], + "support": { + "issues": "https://github.com/squizlabs/PHP_CodeSniffer/issues", + "source": "https://github.com/squizlabs/PHP_CodeSniffer", + "wiki": "https://github.com/squizlabs/PHP_CodeSniffer/wiki" + }, + "time": "2023-02-22T23:07:41+00:00" + }, { "name": "symfony/console", "version": "v5.4.26", @@ -1018,5 +1198,5 @@ "platform-overrides": { "php": "7.4.0" }, - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.6.0" } From e2b7904d9f132bdb9146d08acbc7eb023ea9820c Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Mon, 30 Oct 2023 08:18:26 +0100 Subject: [PATCH 02/10] Automated changes from phpcbf --- front/config.form.php | 35 +- front/home.php | 54 +-- front/showtable.php | 68 ++-- front/test.php | 172 +++++---- hook.php | 83 +++-- inc/config.class.php | 485 ++++++++++++------------ inc/menu.class.php | 52 +-- inc/sccm.class.php | 846 +++++++++++++++++++++--------------------- inc/sccmdb.class.php | 99 +++-- inc/sccmxml.class.php | 637 +++++++++++++++---------------- setup.php | 77 ++-- 11 files changed, 1331 insertions(+), 1277 deletions(-) diff --git a/front/config.form.php b/front/config.form.php index 0cf545a..959a42b 100644 --- a/front/config.form.php +++ b/front/config.form.php @@ -29,7 +29,7 @@ * ------------------------------------------------------------------------- */ -include ('../../../inc/includes.php'); +include('../../../inc/includes.php'); require_once('../inc/config.class.php'); @@ -38,25 +38,30 @@ $PluginSccmConfig = new PluginSccmConfig(); if (isset($_POST["update"])) { - if (array_key_exists('sccmdb_password', $_POST)) { - // Password must not be altered. - $_POST['sccmdb_password'] = $_UPOST['sccmdb_password']; - } + if (array_key_exists('sccmdb_password', $_POST)) { + // Password must not be altered. + $_POST['sccmdb_password'] = $_UPOST['sccmdb_password']; + } - $PluginSccmConfig->update($_POST); + $PluginSccmConfig->update($_POST); $sccmDB = new PluginSccmSccmdb(); - if ($sccmDB->connect()) { - Session::addMessageAfterRedirect("Connexion réussie !.", false, INFO, false); - } else { - Session::addMessageAfterRedirect("Connexion incorrecte.", false, ERROR, false); - } + if ($sccmDB->connect()) { + Session::addMessageAfterRedirect("Connexion réussie !.", false, INFO, false); + } else { + Session::addMessageAfterRedirect("Connexion incorrecte.", false, ERROR, false); + } - Html::back(); + Html::back(); } -Html::header(__("Setup - SCCM", "sccm"), $_SERVER["PHP_SELF"], - "plugins", "sccm", "configuration"); +Html::header( + __("Setup - SCCM", "sccm"), + $_SERVER["PHP_SELF"], + "plugins", + "sccm", + "configuration" +); $PluginSccmConfig->showConfigForm($PluginSccmConfig); -Html::footer(); \ No newline at end of file +Html::footer(); diff --git a/front/home.php b/front/home.php index 0de3762..b82e8e2 100644 --- a/front/home.php +++ b/front/home.php @@ -29,35 +29,35 @@ * ------------------------------------------------------------------------- */ -include ('../../../inc/includes.php'); +include('../../../inc/includes.php'); Session::haveRight("config", UPDATE); if (!function_exists('curl_init')) { - echo "cURL extension (PHP) is required... !! \n"; - exit; + echo "cURL extension (PHP) is required... !! \n"; + exit; } if (!function_exists('mssql_connect') && !function_exists('sqlsrv_connect')) { - echo "MS-SQL extension (PHP) is required... !! \n"; - exit; + echo "MS-SQL extension (PHP) is required... !! \n"; + exit; } if (isset($argv)) { - for ($i=1; $igetFromDB(1); @@ -70,19 +70,19 @@ $action = isset($_GET['task']) ? $_GET['task'] : "home"; if (!in_array($action, ['home','test','showtable'])) { - die('Erreur'); + die('Erreur'); } switch ($action) { - case 'test': - include('test.php'); - break; - case 'showtable' : - include('showtable.php'); - break; - case 'home': - $PluginSccmSccm->showHome(); - break; + case 'test': + include('test.php'); + break; + case 'showtable': + include('showtable.php'); + break; + case 'home': + $PluginSccmSccm->showHome(); + break; } -$PluginSccmSccmdb->disconnect(); \ No newline at end of file +$PluginSccmSccmdb->disconnect(); diff --git a/front/showtable.php b/front/showtable.php index 8d55a46..f4083a5 100644 --- a/front/showtable.php +++ b/front/showtable.php @@ -29,35 +29,35 @@ * ------------------------------------------------------------------------- */ -include ('../../../inc/includes.php'); +include('../../../inc/includes.php'); Session::haveRight("config", UPDATE); if (!function_exists('curl_init')) { - echo "cURL extension (PHP) is required... !! \n"; - exit; + echo "cURL extension (PHP) is required... !! \n"; + exit; } if (!function_exists('mssql_connect') && !function_exists('sqlsrv_connect')) { - echo "MS-SQL extension (PHP) is required... !! \n"; - exit; + echo "MS-SQL extension (PHP) is required... !! \n"; + exit; } if (isset($argv)) { - for ($i=1; $igetFromDB(1); @@ -70,26 +70,26 @@ $action = isset($_GET['task']) ? $_GET['task'] : "home"; if (!in_array($action, ['home','test','inject','showtable'])) { - die('Erreur'); + die('Erreur'); } switch ($action) { - case 'test': - include('test.php'); - break; - case 'showtable' : - include('showtable.php'); - break; - case 'inject': - if ($PluginSccmConfig->getField('active_sync') == 1) { - include('inject.php'); - } else { - echo __("Synchronization is disabled by configuration.", "sccm"); - } - break; - case 'home': - $PluginSccmSccm->showHome(); - break; + case 'test': + include('test.php'); + break; + case 'showtable': + include('showtable.php'); + break; + case 'inject': + if ($PluginSccmConfig->getField('active_sync') == 1) { + include('inject.php'); + } else { + echo __("Synchronization is disabled by configuration.", "sccm"); + } + break; + case 'home': + $PluginSccmSccm->showHome(); + break; } -$PluginSccmSccmdb->disconnect(); \ No newline at end of file +$PluginSccmSccmdb->disconnect(); diff --git a/front/test.php b/front/test.php index 2223883..d5ffb54 100644 --- a/front/test.php +++ b/front/test.php @@ -30,19 +30,21 @@ */ if (!defined('GLPI_ROOT')) { - die("Sorry. You can't access directly to this file"); + die("Sorry. You can't access directly to this file"); } Session::haveRight("config", UPDATE); -Html::header(__('SCCM - TEST', 'sccm'), - $_SERVER["PHP_SELF"], - "plugins", - "sccm", - "Test"); +Html::header( + __('SCCM - TEST', 'sccm'), + $_SERVER["PHP_SELF"], + "plugins", + "sccm", + "Test" +); echo "
"; -echo "
"; +echo ""; echo ''; echo ""; echo ""; - echo "\n"; - - echo ""; - echo "\n"; - - echo ""; - echo "\n"; - - echo ""; - echo "\n"; - - $password = $config->getField('sccmdb_password'); - $password = Html::entities_deep((new GLPIKey())->decrypt($password)); - echo ""; - echo "\n"; - - echo ""; - echo "\n"; - - echo ""; - echo "\n"; - - echo ""; - echo "\n"; - - echo ""; - echo "\n"; - - echo ""; - echo "\n"; - - echo ""; - echo "\n"; - - echo ""; - echo "\n"; - - $config->showFormButtons(['candel'=>false]); - - return false; - } - + } + + return true; + } + + + static function uninstall() + { + global $DB; + + if ($DB->tableExists('glpi_plugin_sccm_configs')) { + $query = "DROP TABLE `glpi_plugin_sccm_configs`"; + $DB->queryOrDie($query, $DB->error()); + } + return true; + } + + + static function showConfigForm($item) + { + global $CFG_GLPI; + + $config = self::getInstance(); + + $config->showFormHeader(); + + echo ""; + echo "\n"; + + echo ""; + echo "\n"; + + echo ""; + echo "\n"; + + echo ""; + echo "\n"; + + $password = $config->getField('sccmdb_password'); + $password = Html::entities_deep((new GLPIKey())->decrypt($password)); + echo ""; + echo "\n"; + + echo ""; + echo "\n"; + + echo ""; + echo "\n"; + + echo ""; + echo "\n"; + + echo ""; + echo "\n"; + + echo ""; + echo "\n"; + + echo ""; + echo "\n"; + + echo ""; + echo "\n"; + + $config->showFormButtons(['candel' => false]); + + return false; + } } diff --git a/inc/menu.class.php b/inc/menu.class.php index 5179dd7..954e558 100644 --- a/inc/menu.class.php +++ b/inc/menu.class.php @@ -29,29 +29,29 @@ * ------------------------------------------------------------------------- */ -class PluginSccmMenu extends CommonGLPI { - - static function getTypeName($nb = 0) { - return __('SCCM Connector', 'sccm'); - } - - static function getMenuName() { - return __('SCCM Connector', 'sccm'); - } - - static function getMenuContent() { - global $CFG_GLPI; - $menu = []; - $menu['title'] = self::getMenuName(); - $menu['page'] = '/' . Plugin::getWebDir('sccm', false) . '/front/config.form.php'; - - if (Session::haveRight('config', UPDATE)) { - - $menu['options']['model']['title'] = self::getTypeName(); - - } - - return $menu; - } - -} \ No newline at end of file +class PluginSccmMenu extends CommonGLPI +{ + static function getTypeName($nb = 0) + { + return __('SCCM Connector', 'sccm'); + } + + static function getMenuName() + { + return __('SCCM Connector', 'sccm'); + } + + static function getMenuContent() + { + global $CFG_GLPI; + $menu = []; + $menu['title'] = self::getMenuName(); + $menu['page'] = '/' . Plugin::getWebDir('sccm', false) . '/front/config.form.php'; + + if (Session::haveRight('config', UPDATE)) { + $menu['options']['model']['title'] = self::getTypeName(); + } + + return $menu; + } +} diff --git a/inc/sccm.class.php b/inc/sccm.class.php index 20c221c..be55b0a 100644 --- a/inc/sccm.class.php +++ b/inc/sccm.class.php @@ -30,106 +30,110 @@ */ if (!defined('GLPI_ROOT')) { - die("Sorry. You can't access directly to this file"); + die("Sorry. You can't access directly to this file"); } use Glpi\Toolbox\Sanitizer; -class PluginSccmSccm { +class PluginSccmSccm +{ + var $devices; - var $devices; + static function getTypeName($nb = 0) + { + return __('SCCM', 'sccm'); + } - static function getTypeName($nb = 0) { - return __('SCCM', 'sccm'); - } + function showHome() + { + echo __('Please, read the documentation before using that.', 'footprints'); + } - function showHome() { - echo __('Please, read the documentation before using that.', 'footprints'); - } + function getDevices($where = 0, $limit = 99999999) + { - function getDevices($where = 0, $limit = 99999999) { - - $PluginSccmSccmdb = new PluginSccmSccmdb(); - $res = $PluginSccmSccmdb->connect(); - if (!$res) { - die; - } - - $query = self::getcomputerQuery(); + $PluginSccmSccmdb = new PluginSccmSccmdb(); + $res = $PluginSccmSccmdb->connect(); + if (!$res) { + die; + } - if ($where!=0) { - $query.= " WHERE csd.MachineID = '" . $where . "'"; - } + $query = self::getcomputerQuery(); - $result = $PluginSccmSccmdb->exec_query($query); + if ($where != 0) { + $query .= " WHERE csd.MachineID = '" . $where . "'"; + } - $i = 0; - $tab = []; + $result = $PluginSccmSccmdb->exec_query($query); - while (($tab = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) AND $i < $limit) { + $i = 0; + $tab = []; - $tab['MD-SystemName'] = strtoupper($tab['MD-SystemName']); + while (($tab = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) and $i < $limit) { + $tab['MD-SystemName'] = strtoupper($tab['MD-SystemName']); - $this->devices[] = $tab; + $this->devices[] = $tab; - $i++; - } + $i++; + } - $PluginSccmSccmdb->disconnect(); - } + $PluginSccmSccmdb->disconnect(); + } - function getDatas($type, $deviceid, $limit = 99999999) { + function getDatas($type, $deviceid, $limit = 99999999) + { - $PluginSccmSccmdb = new PluginSccmSccmdb(); - $res = $PluginSccmSccmdb->connect(); - if (!$res) { - die; - } + $PluginSccmSccmdb = new PluginSccmSccmdb(); + $res = $PluginSccmSccmdb->connect(); + if (!$res) { + die; + } - $datas = []; + $datas = []; - switch ($type) { - case 'processors' : - $fields = ['Manufacturer00','Name00','NormSpeed00','AddressWidth00','CPUKey00','NumberOfCores00', 'NumberOfLogicalProcessors00']; - $table = 'Processor_DATA'; - break; - } + switch ($type) { + case 'processors': + $fields = ['Manufacturer00','Name00','NormSpeed00','AddressWidth00','CPUKey00','NumberOfCores00', 'NumberOfLogicalProcessors00']; + $table = 'Processor_DATA'; + break; + } - $query = "SELECT ".implode(',', $fields)."\n"; - $query.= " FROM ".$table."\n"; - $query.= " WHERE MachineID = '".$deviceid."'"."\n"; + $query = "SELECT " . implode(',', $fields) . "\n"; + $query .= " FROM " . $table . "\n"; + $query .= " WHERE MachineID = '" . $deviceid . "'" . "\n"; - $result = $PluginSccmSccmdb->exec_query($query); + $result = $PluginSccmSccmdb->exec_query($query); - $data = []; + $data = []; - $i=0; - $tab = []; - while (($tab = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) AND $i < $limit) { - $tmp = []; + $i = 0; + $tab = []; + while (($tab = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) and $i < $limit) { + $tmp = []; - foreach ($tab as $key => $value) { - $tmp[$key] = Sanitizer::sanitize($value); - } - $data[] = $tmp; + foreach ($tab as $key => $value) { + $tmp[$key] = Sanitizer::sanitize($value); + } + $data[] = $tmp; - $i++; - } + $i++; + } - $PluginSccmSccmdb->disconnect(); + $PluginSccmSccmdb->disconnect(); - return $data; - } + return $data; + } - function getNetwork($deviceid, $limit = 99999999) { + function getNetwork($deviceid, $limit = 99999999) + { - $PluginSccmSccmdb = new PluginSccmSccmdb(); - $res = $PluginSccmSccmdb->connect(); - if (!$res) { - die; - } + $PluginSccmSccmdb = new PluginSccmSccmdb(); + $res = $PluginSccmSccmdb->connect(); + if (!$res) { + die; + } - $query = "SELECT NeDa.IPAddress00 as \"ND-IpAddress\", + $query = "SELECT NeDa.IPAddress00 as \"ND-IpAddress\", NeDa.MACAddress00 as \"ND-MacAddress\", NeDa.IPSubnet00 as \"ND-IpSubnet\", NeDa.DefaultIPGateway00 as \"ND-IpGateway\", @@ -140,39 +144,40 @@ function getNetwork($deviceid, $limit = 99999999) { INNER JOIN v_R_System VrS ON VrS.ResourceID=NeDa.MachineID INNER JOIN v_GS_NETWORK_ADAPTER net ON net.ResourceID=NeDa.MachineID AND NeDa.ServiceName00=net.ServiceName0 WHERE MACAddress00 is not null - AND NeDa.MachineID = '".$deviceid."'"; + AND NeDa.MachineID = '" . $deviceid . "'"; - $result = $PluginSccmSccmdb->exec_query($query); + $result = $PluginSccmSccmdb->exec_query($query); - $data = []; + $data = []; - $i=0; - $tab = []; - while (($tab = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) AND $i < $limit) { - $tmp = []; + $i = 0; + $tab = []; + while (($tab = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) and $i < $limit) { + $tmp = []; - foreach ($tab as $key => $value) { - $tmp[$key] = Sanitizer::sanitize($value); - } - $data[] = $tmp; + foreach ($tab as $key => $value) { + $tmp[$key] = Sanitizer::sanitize($value); + } + $data[] = $tmp; - $i++; - } + $i++; + } - $PluginSccmSccmdb->disconnect(); + $PluginSccmSccmdb->disconnect(); - return $data; - } + return $data; + } - function getSoftware($deviceid, $limit = 99999999) { + function getSoftware($deviceid, $limit = 99999999) + { - $PluginSccmSccmdb = new PluginSccmSccmdb(); - $res = $PluginSccmSccmdb->connect(); - if (!$res) { - die; - } + $PluginSccmSccmdb = new PluginSccmSccmdb(); + $res = $PluginSccmSccmdb->connect(); + if (!$res) { + die; + } - $query = "SELECT ArPd_64.DisplayName0 as \"ArPd-DisplayName\", + $query = "SELECT ArPd_64.DisplayName0 as \"ArPd-DisplayName\", ArPd_64.InstallDate0 as \"ArPd-InstallDate\", ArPd_64.Version0 as \"ArPd-Version\", ArPd_64.Publisher0 as \"ArPd-Publisher\" @@ -190,37 +195,38 @@ function getSoftware($deviceid, $limit = 99999999) { WHERE ArPd.ResourceID = $deviceid AND (ArPd.DisplayName0 is not null and ArPd.DisplayName0 <> '')"; - $result = $PluginSccmSccmdb->exec_query($query); + $result = $PluginSccmSccmdb->exec_query($query); - $data = []; + $data = []; - $i=0; - $tab = []; - while (($tab = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) AND $i < $limit) { - $tmp = []; + $i = 0; + $tab = []; + while (($tab = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) and $i < $limit) { + $tmp = []; - foreach ($tab as $key => $value) { - $tmp[$key] = Sanitizer::sanitize($value); - } - $data[] = $tmp; + foreach ($tab as $key => $value) { + $tmp[$key] = Sanitizer::sanitize($value); + } + $data[] = $tmp; - $i++; - } + $i++; + } - $PluginSccmSccmdb->disconnect(); + $PluginSccmSccmdb->disconnect(); - return $data; - } + return $data; + } - function getMemories($deviceid, $limit = 99999999) { + function getMemories($deviceid, $limit = 99999999) + { - $PluginSccmSccmdb = new PluginSccmSccmdb(); - $res = $PluginSccmSccmdb->connect(); - if (!$res) { - die; - } + $PluginSccmSccmdb = new PluginSccmSccmdb(); + $res = $PluginSccmSccmdb->connect(); + if (!$res) { + die; + } - $query = "SELECT + $query = "SELECT Capacity0 as \"Mem-Capacity\", Caption0 as \"Mem-Caption\", Description0 as \"Mem-Description\", @@ -234,39 +240,40 @@ function getMemories($deviceid, $limit = 99999999) { '' as \"Mem-SerialNumber\" FROM v_GS_PHYSICAL_MEMORY - WHERE ResourceID = '".$deviceid."' + WHERE ResourceID = '" . $deviceid . "' ORDER BY \"Mem-NumSlots\""; - $result = $PluginSccmSccmdb->exec_query($query); + $result = $PluginSccmSccmdb->exec_query($query); - $data = []; + $data = []; - $i=0; - $tab = []; - while (($tab = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) AND $i < $limit) { - $tmp = []; + $i = 0; + $tab = []; + while (($tab = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) and $i < $limit) { + $tmp = []; - foreach ($tab as $key => $value) { - $tmp[$key] = Sanitizer::sanitize($value); - } - $data[] = $tmp; + foreach ($tab as $key => $value) { + $tmp[$key] = Sanitizer::sanitize($value); + } + $data[] = $tmp; - $i++; - } + $i++; + } - $PluginSccmSccmdb->disconnect(); + $PluginSccmSccmdb->disconnect(); - return $data; - } + return $data; + } - function getVideos($deviceid, $limit = 99999999) { + function getVideos($deviceid, $limit = 99999999) + { $PluginSccmSccmdb = new PluginSccmSccmdb(); $res = $PluginSccmSccmdb->connect(); - if (!$res) { - die; - } + if (!$res) { + die; + } $query = " SELECT @@ -277,78 +284,80 @@ function getVideos($deviceid, $limit = 99999999) { GroupID as \"Vid-PciSlot\" FROM v_GS_VIDEO_CONTROLLER WHERE VideoProcessor0 is not null - AND ResourceID = '".$deviceid."' + AND ResourceID = '" . $deviceid . "' ORDER BY GroupID"; - $result = $PluginSccmSccmdb->exec_query($query); + $result = $PluginSccmSccmdb->exec_query($query); - $data = []; + $data = []; - $i=0; - $tab = []; - while (($tab = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) AND $i < $limit) { - $tmp = []; + $i = 0; + $tab = []; + while (($tab = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) and $i < $limit) { + $tmp = []; - foreach ($tab as $key => $value) { - $tmp[$key] = Sanitizer::sanitize($value); - } - $data[] = $tmp; + foreach ($tab as $key => $value) { + $tmp[$key] = Sanitizer::sanitize($value); + } + $data[] = $tmp; - $i++; - } + $i++; + } $PluginSccmSccmdb->disconnect(); return $data; - } + } - function getSounds($deviceid, $limit = 99999999) { + function getSounds($deviceid, $limit = 99999999) + { - $PluginSccmSccmdb = new PluginSccmSccmdb(); - $res = $PluginSccmSccmdb->connect(); - if (!$res) { - die; - } + $PluginSccmSccmdb = new PluginSccmSccmdb(); + $res = $PluginSccmSccmdb->connect(); + if (!$res) { + die; + } - $query = " + $query = " SELECT distinct Description0 as \"Snd-Description\", Manufacturer0 as \"Snd-Manufacturer\", Name0 as \"Snd-Name\" FROM v_GS_SOUND_DEVICE - WHERE ResourceID = '".$deviceid."'"; + WHERE ResourceID = '" . $deviceid . "'"; - $result = $PluginSccmSccmdb->exec_query($query); + $result = $PluginSccmSccmdb->exec_query($query); - $data = []; + $data = []; - $i=0; - $tab = []; - while (($tab = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) AND $i < $limit) { - $tmp = []; + $i = 0; + $tab = []; + while (($tab = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) and $i < $limit) { + $tmp = []; - foreach ($tab as $key => $value) { - $tmp[$key] = Sanitizer::sanitize($value); - } - $data[] = $tmp; + foreach ($tab as $key => $value) { + $tmp[$key] = Sanitizer::sanitize($value); + } + $data[] = $tmp; - $i++; - } + $i++; + } - $PluginSccmSccmdb->disconnect(); + $PluginSccmSccmdb->disconnect(); - return $data; - } + return $data; + } - function getStorages($deviceid, $limit = 99999999) { + function getStorages($deviceid, $limit = 99999999) + { - $PluginSccmSccmdb = new PluginSccmSccmdb(); - $res = $PluginSccmSccmdb->connect(); - if (!$res) { - die; - } + $PluginSccmSccmdb = new PluginSccmSccmdb(); + $res = $PluginSccmSccmdb->connect(); + if (!$res) { + die; + } - $query = " + $query = " SELECT md.SystemName00, gld.ResourceID as \"gld-ResourceID\", @@ -363,39 +372,40 @@ function getStorages($deviceid, $limit = 99999999) { INNER JOIN v_gs_Disk as gdi on gdi.ResourceID = gld.ResourceID LEFT JOIN Motherboard_DATA as md on gld.ResourceID = md.MachineID WHERE gld.GroupID = gdi.GroupID - AND gld.ResourceID = '".$deviceid."'"; + AND gld.ResourceID = '" . $deviceid . "'"; - $result = $PluginSccmSccmdb->exec_query($query); + $result = $PluginSccmSccmdb->exec_query($query); - $data = []; + $data = []; - $i=0; - $tab = []; - while (($tab = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) AND $i < $limit) { - $tmp = []; + $i = 0; + $tab = []; + while (($tab = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) and $i < $limit) { + $tmp = []; - foreach ($tab as $key => $value) { - $tmp[$key] = Sanitizer::sanitize($value); - } - $data[] = $tmp; + foreach ($tab as $key => $value) { + $tmp[$key] = Sanitizer::sanitize($value); + } + $data[] = $tmp; - $i++; - } + $i++; + } - $PluginSccmSccmdb->disconnect(); + $PluginSccmSccmdb->disconnect(); - return $data; - } + return $data; + } - function getMedias($deviceid, $limit = 99999999) { + function getMedias($deviceid, $limit = 99999999) + { - $PluginSccmSccmdb = new PluginSccmSccmdb(); - $res = $PluginSccmSccmdb->connect(); - if (!$res) { - die; - } + $PluginSccmSccmdb = new PluginSccmSccmdb(); + $res = $PluginSccmSccmdb->connect(); + if (!$res) { + die; + } - $query = " + $query = " SELECT distinct Description0 as \"Med-Description\", Manufacturer0 as \"Med-Manufacturer\", @@ -404,129 +414,136 @@ function getMedias($deviceid, $limit = 99999999) { SCSITargetID0 as \"Med-SCSITargetId\", MediaType0 as \"Med-Type\" FROM v_GS_CDROM - WHERE ResourceID = '".$deviceid."'"; - - $result = $PluginSccmSccmdb->exec_query($query); - - $data = []; + WHERE ResourceID = '" . $deviceid . "'"; - $i=0; - $tab = []; - while (($tab = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) AND $i < $limit) { - $tmp = []; + $result = $PluginSccmSccmdb->exec_query($query); - foreach ($tab as $key => $value) { - $tmp[$key] = Sanitizer::sanitize($value); - } - $data[] = $tmp; + $data = []; - $i++; - } + $i = 0; + $tab = []; + while (($tab = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) and $i < $limit) { + $tmp = []; - $PluginSccmSccmdb->disconnect(); - - return $data; - } - - static function install() { - $cronCollect = new CronTask; - - if ($cronCollect->getFromDBbyName(__CLASS__, 'sccm')) { - - $cronCollect->fields["name"] = "SCCMCollect"; - $cronCollect->fields["hourmin"] = 4; - $cronCollect->fields["hourmax"] = 5; - $cronCollect->update($cronCollect->fields); - - } else if (!$cronCollect->getFromDBbyName(__CLASS__, 'SCCMCollect')) { - - CronTask::register(__CLASS__, 'SCCMCollect', 7 * DAY_TIMESTAMP, - ['param' => 24, 'mode' => CronTask::MODE_EXTERNAL, 'hourmin' => 4, 'hourmax' => 5]); - - } - - CronTask::register(__CLASS__, 'SCCMPush', 7 * DAY_TIMESTAMP, - ['param' => 24, 'mode' => CronTask::MODE_EXTERNAL, 'hourmin' => 6, 'hourmax' => 7]); - } - - static function uninstall() { - CronTask::unregister(__CLASS__); - } - - static function cronSCCMCollect($task) { - return self::executeCollect($task); - } - - static function cronSCCMPush($task) { - return self::executePush($task); - } - - static function cronInfo($name) { - if ($name == "SCCMCollect") { - return ['description' => __("Interface - SCCMCollect", "sccm")]; - } - if ($name == "SCCMPush") { - return ['description' => __("Interface - SCCMPush", "sccm")]; - } - - } - - static function executeCollect($task) { - ini_set('max_execution_time', 0); - $retcode = -1; - - $REP_XML = GLPI_PLUGIN_DOC_DIR.'/sccm/xml/'; - - $PluginSccmConfig = new PluginSccmConfig(); - $PluginSccmConfig->getFromDB(1); - - $PluginSccmSccm = new PluginSccmSccm(); + foreach ($tab as $key => $value) { + $tmp[$key] = Sanitizer::sanitize($value); + } + $data[] = $tmp; - if ($PluginSccmConfig->getField('active_sync') == 1) { + $i++; + } - $PluginSccmSccm->getDevices(); - Toolbox::logInFile('sccm', "getDevices OK \n", true); + $PluginSccmSccmdb->disconnect(); - Toolbox::logInFile('sccm', "Generate XML start : " + return $data; + } + + static function install() + { + $cronCollect = new CronTask(); + + if ($cronCollect->getFromDBbyName(__CLASS__, 'sccm')) { + $cronCollect->fields["name"] = "SCCMCollect"; + $cronCollect->fields["hourmin"] = 4; + $cronCollect->fields["hourmax"] = 5; + $cronCollect->update($cronCollect->fields); + } else if (!$cronCollect->getFromDBbyName(__CLASS__, 'SCCMCollect')) { + CronTask::register( + __CLASS__, + 'SCCMCollect', + 7 * DAY_TIMESTAMP, + ['param' => 24, 'mode' => CronTask::MODE_EXTERNAL, 'hourmin' => 4, 'hourmax' => 5] + ); + } + + CronTask::register( + __CLASS__, + 'SCCMPush', + 7 * DAY_TIMESTAMP, + ['param' => 24, 'mode' => CronTask::MODE_EXTERNAL, 'hourmin' => 6, 'hourmax' => 7] + ); + } + + static function uninstall() + { + CronTask::unregister(__CLASS__); + } + + static function cronSCCMCollect($task) + { + return self::executeCollect($task); + } + + static function cronSCCMPush($task) + { + return self::executePush($task); + } + + static function cronInfo($name) + { + if ($name == "SCCMCollect") { + return ['description' => __("Interface - SCCMCollect", "sccm")]; + } + if ($name == "SCCMPush") { + return ['description' => __("Interface - SCCMPush", "sccm")]; + } + } + + static function executeCollect($task) + { + ini_set('max_execution_time', 0); + $retcode = -1; + + $REP_XML = GLPI_PLUGIN_DOC_DIR . '/sccm/xml/'; + + $PluginSccmConfig = new PluginSccmConfig(); + $PluginSccmConfig->getFromDB(1); + + $PluginSccmSccm = new PluginSccmSccm(); + + if ($PluginSccmConfig->getField('active_sync') == 1) { + $PluginSccmSccm->getDevices(); + Toolbox::logInFile('sccm', "getDevices OK \n", true); + + Toolbox::logInFile('sccm', "Generate XML start : " . count($PluginSccmSccm->devices) . " files\n", true); - foreach ($PluginSccmSccm->devices as $device_values) { - - $PluginSccmSccmxml = new PluginSccmSccmxml($device_values); - - $PluginSccmSccmxml->setAccessLog(); - $PluginSccmSccmxml->setAccountInfos(); - $PluginSccmSccmxml->setHardware(); - $PluginSccmSccmxml->setOS(); - $PluginSccmSccmxml->setBios(); - $PluginSccmSccmxml->setProcessors(); - $PluginSccmSccmxml->setSoftwares(); - $PluginSccmSccmxml->setMemories(); - $PluginSccmSccmxml->setVideos(); - $PluginSccmSccmxml->setSounds(); - $PluginSccmSccmxml->setUsers(); - $PluginSccmSccmxml->setNetworks(); - $PluginSccmSccmxml->setStorages(); - - $SXML = $PluginSccmSccmxml->sxml; - - $SXML->asXML($REP_XML.$PluginSccmSccmxml->device_id.".ocs"); - - Toolbox::logInFile('sccm', "Collect OK for device - ".$PluginSccmSccmxml->device_id." \n", true); - $task->addVolume(1); - } - $retcode = 1; - Toolbox::logInFile('sccm', "Collect completed \n", true); - - } else { - echo __("Collect is disabled by configuration.", "sccm"); - } + foreach ($PluginSccmSccm->devices as $device_values) { + $PluginSccmSccmxml = new PluginSccmSccmxml($device_values); + + $PluginSccmSccmxml->setAccessLog(); + $PluginSccmSccmxml->setAccountInfos(); + $PluginSccmSccmxml->setHardware(); + $PluginSccmSccmxml->setOS(); + $PluginSccmSccmxml->setBios(); + $PluginSccmSccmxml->setProcessors(); + $PluginSccmSccmxml->setSoftwares(); + $PluginSccmSccmxml->setMemories(); + $PluginSccmSccmxml->setVideos(); + $PluginSccmSccmxml->setSounds(); + $PluginSccmSccmxml->setUsers(); + $PluginSccmSccmxml->setNetworks(); + $PluginSccmSccmxml->setStorages(); + + $SXML = $PluginSccmSccmxml->sxml; + + $SXML->asXML($REP_XML . $PluginSccmSccmxml->device_id . ".ocs"); + + Toolbox::logInFile('sccm', "Collect OK for device - " . $PluginSccmSccmxml->device_id . " \n", true); + $task->addVolume(1); + } + $retcode = 1; + Toolbox::logInFile('sccm', "Collect completed \n", true); + } else { + echo __("Collect is disabled by configuration.", "sccm"); + } - return $retcode; - } + return $retcode; + } - static function getcomputerQuery() { - return "SELECT csd.Description00 as \"CSD-Description\", + static function getcomputerQuery() + { + return "SELECT csd.Description00 as \"CSD-Description\", csd.Domain00 as \"CSD-Domain\", csd.Manufacturer00 as \"CSD-Manufacturer\", csd.Model00 as \"CSD-Model\", @@ -569,111 +586,106 @@ static function getcomputerQuery() { LEFT JOIN System_DATA sd ON csd.MachineID = sd.MachineID INNER JOIN v_R_System VrS ON csd.MachineID = VrS.ResourceID WHERE csd.MachineID is not null and csd.MachineID != ''"; - } - - - static function executePush($task) { - global $CFG_GLPI; - - $PluginSccmSccmdb = new PluginSccmSccmdb(); - $res = $PluginSccmSccmdb->connect(); - $PluginSccmConfig = new PluginSccmConfig(); - $PluginSccmConfig->getFromDB(1); - $retcode = -1; - - if ($PluginSccmConfig->getField('active_sync') == 1) { - if ($res) { - - $query = self::getcomputerQuery(); - $result = $PluginSccmSccmdb->exec_query($query); - - $tab = []; - - while ($tab = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) { - - $REP_XML = realpath(GLPI_PLUGIN_DOC_DIR.'/sccm/xml/'.$tab['CSD-MachineID'].'.ocs'); - - if ($REP_XML === false) { - Toolbox::logInFile('sccm', "There is a problem with the path, realpath function return false.\nPath : ".$REP_XML."\n", true); - continue; - } - - $xmlFile = simplexml_load_file($REP_XML, 'SimpleXMLElement', LIBXML_NOCDATA); - if ($xmlFile !== false) { - - $ch = curl_init(); - if ($PluginSccmConfig->getField('verify_ssl_cert') != "1") { - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); - } - - if ($PluginSccmConfig->getField('use_auth_ntlm') == "1") { - curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); - } - - if ($PluginSccmConfig->getField('unrestricted_auth') == "1") { - curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, true); - } - - if ($PluginSccmConfig->getField('use_auth_info') == "1") { - curl_setopt($ch, CURLOPT_USERPWD, $PluginSccmConfig->getField('auth_info')); - } - - $url = ($PluginSccmConfig->getField('inventory_server_url') ?: $CFG_GLPI['url_base']) . '/front/inventory.php'; - - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: text/xml']); - curl_setopt($ch, CURLOPT_HEADER, 1); - curl_setopt($ch, CURLOPT_POST, 1); - curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlFile->asXML()); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); - curl_setopt($ch, CURLOPT_REFERER, $CFG_GLPI['url_base']); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - $ch_result = curl_exec($ch); - if ($ch_result === false) { - Toolbox::logInFile('sccm', curl_error($ch)."\n", true); - } else { - - $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); - if ($httpcode != 200) { - $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); - $body = substr($ch_result, $header_size); - - Toolbox::logInFile('sccm', "Push KO - ".$tab['CSD-MachineID']." -> STATUS CODE : ".$httpcode." \n", true); - Toolbox::logInFile('sccm', "ERROR RETURNED : ".$body." \n", true); - } else { - $task->addVolume(1); - - if ($PluginSccmConfig->getField('use_lasthwscan') == 1) { - $agent = new Agent(); - if ($agent->getFromDBByCrit(["name" => $tab['CSD-MachineID']])) { - $asset = new $agent->fields['itemtype'](); - if ($asset->getFromDB($agent->fields['items_id'])) { - $asset->update([ - "id" => $agent->fields['id'], - "last_inventory_update" => $tab['vWD-LastScan']->format('Y-m-d h:i') - ]); - } - } + } + + + static function executePush($task) + { + global $CFG_GLPI; + + $PluginSccmSccmdb = new PluginSccmSccmdb(); + $res = $PluginSccmSccmdb->connect(); + $PluginSccmConfig = new PluginSccmConfig(); + $PluginSccmConfig->getFromDB(1); + $retcode = -1; + + if ($PluginSccmConfig->getField('active_sync') == 1) { + if ($res) { + $query = self::getcomputerQuery(); + $result = $PluginSccmSccmdb->exec_query($query); + + $tab = []; + + while ($tab = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) { + $REP_XML = realpath(GLPI_PLUGIN_DOC_DIR . '/sccm/xml/' . $tab['CSD-MachineID'] . '.ocs'); + + if ($REP_XML === false) { + Toolbox::logInFile('sccm', "There is a problem with the path, realpath function return false.\nPath : " . $REP_XML . "\n", true); + continue; + } + + $xmlFile = simplexml_load_file($REP_XML, 'SimpleXMLElement', LIBXML_NOCDATA); + if ($xmlFile !== false) { + $ch = curl_init(); + if ($PluginSccmConfig->getField('verify_ssl_cert') != "1") { + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); + } + + if ($PluginSccmConfig->getField('use_auth_ntlm') == "1") { + curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); + } + + if ($PluginSccmConfig->getField('unrestricted_auth') == "1") { + curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, true); + } + + if ($PluginSccmConfig->getField('use_auth_info') == "1") { + curl_setopt($ch, CURLOPT_USERPWD, $PluginSccmConfig->getField('auth_info')); } - Toolbox::logInFile('sccm', "Push OK - ".$tab['CSD-MachineID']." \n", true); - } - - } - curl_close($ch); - } else { - Toolbox::logInFile('sccm', "Can't load the file with the path : ".$REP_XML."\n", true); - } - } - Toolbox::logInFile('sccm', "Push completed \n", true); - $PluginSccmSccmdb->disconnect(); - $retcode = 1; - } - } else { - echo __("Push is disabled by configuration.", "sccm"); - } - return $retcode; - } + $url = ($PluginSccmConfig->getField('inventory_server_url') ?: $CFG_GLPI['url_base']) . '/front/inventory.php'; + + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: text/xml']); + curl_setopt($ch, CURLOPT_HEADER, 1); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlFile->asXML()); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); + curl_setopt($ch, CURLOPT_REFERER, $CFG_GLPI['url_base']); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + $ch_result = curl_exec($ch); + if ($ch_result === false) { + Toolbox::logInFile('sccm', curl_error($ch) . "\n", true); + } else { + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + if ($httpcode != 200) { + $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); + $body = substr($ch_result, $header_size); + + Toolbox::logInFile('sccm', "Push KO - " . $tab['CSD-MachineID'] . " -> STATUS CODE : " . $httpcode . " \n", true); + Toolbox::logInFile('sccm', "ERROR RETURNED : " . $body . " \n", true); + } else { + $task->addVolume(1); + + if ($PluginSccmConfig->getField('use_lasthwscan') == 1) { + $agent = new Agent(); + if ($agent->getFromDBByCrit(["name" => $tab['CSD-MachineID']])) { + $asset = new $agent->fields['itemtype'](); + if ($asset->getFromDB($agent->fields['items_id'])) { + $asset->update([ + "id" => $agent->fields['id'], + "last_inventory_update" => $tab['vWD-LastScan']->format('Y-m-d h:i') + ]); + } + } + } + Toolbox::logInFile('sccm', "Push OK - " . $tab['CSD-MachineID'] . " \n", true); + } + } + curl_close($ch); + } else { + Toolbox::logInFile('sccm', "Can't load the file with the path : " . $REP_XML . "\n", true); + } + } + Toolbox::logInFile('sccm', "Push completed \n", true); + $PluginSccmSccmdb->disconnect(); + $retcode = 1; + } + } else { + echo __("Push is disabled by configuration.", "sccm"); + } + return $retcode; + } } diff --git a/inc/sccmdb.class.php b/inc/sccmdb.class.php index a13b936..732e7e1 100644 --- a/inc/sccmdb.class.php +++ b/inc/sccmdb.class.php @@ -30,70 +30,69 @@ */ if (!defined('GLPI_ROOT')) { - die("Sorry. You can't access directly to this file"); + die("Sorry. You can't access directly to this file"); } -class PluginSccmSccmdb { +class PluginSccmSccmdb +{ + var $dbconn; - var $dbconn; + function connect() + { - function connect() { + $PluginSccmConfig = new PluginSccmConfig(); + $PluginSccmConfig->getFromDB(1); - $PluginSccmConfig = new PluginSccmConfig(); - $PluginSccmConfig->getFromDB(1); + $host = $PluginSccmConfig->getField('sccmdb_host'); + $dbname = $PluginSccmConfig->getField('sccmdb_dbname'); + $user = $PluginSccmConfig->getField('sccmdb_user'); - $host = $PluginSccmConfig->getField('sccmdb_host'); - $dbname = $PluginSccmConfig->getField('sccmdb_dbname'); - $user = $PluginSccmConfig->getField('sccmdb_user'); + $password = $PluginSccmConfig->getField('sccmdb_password'); + $password = (new GLPIKey())->decrypt($password); - $password = $PluginSccmConfig->getField('sccmdb_password'); - $password = (new GLPIKey())->decrypt($password); + $connectionOptions = [ + "Database" => $dbname, + "Uid" => $user, + "PWD" => $password, + "CharacterSet" => "UTF-8" + ]; - $connectionOptions = [ - "Database" => $dbname, - "Uid" => $user, - "PWD" => $password, - "CharacterSet" => "UTF-8" - ]; + $this->dbconn = sqlsrv_connect($host, $connectionOptions); + if ($this->dbconn === false) { + $this->FormatErrors(sqlsrv_errors()); + return false; + } - $this->dbconn = sqlsrv_connect( $host, $connectionOptions ); - if ($this->dbconn === false) { - $this->FormatErrors( sqlsrv_errors()); - return false; - } + return true; + } - return true; - } + function disconnect() + { - function disconnect() { + sqlsrv_close($this->dbconn); + } - sqlsrv_close($this->dbconn); + function exec_query($query) + { - } + $result = sqlsrv_query($this->dbconn, $query) or die('Query error : ' . print_r(sqlsrv_errors(), true)); + if ($result == false) { + die(FormatErrors(sqlsrv_errors())); + } + return $result; + } - function exec_query($query) { + function FormatErrors($errors) + { - $result = sqlsrv_query($this->dbconn, $query) or die('Query error : ' . print_r(sqlsrv_errors(), true)); - if ($result == false) { - die( FormatErrors( sqlsrv_errors())); - } - return $result; - - } - - function FormatErrors($errors) { - - foreach ($errors as $error) { - $debug = ""; - $state = "SQLSTATE: ".$error['SQLSTATE']; - $code = "Code: ".$error['code']; - $message = "Message: ".$error['message']; - - echo $state."
".$code."
".$message."
"; - Toolbox::logInFile("sccm", $state.PHP_EOL.$code.PHP_EOL.$message.PHP_EOL); - } - - } + foreach ($errors as $error) { + $debug = ""; + $state = "SQLSTATE: " . $error['SQLSTATE']; + $code = "Code: " . $error['code']; + $message = "Message: " . $error['message']; + echo $state . "
" . $code . "
" . $message . "
"; + Toolbox::logInFile("sccm", $state . PHP_EOL . $code . PHP_EOL . $message . PHP_EOL); + } + } } - diff --git a/inc/sccmxml.class.php b/inc/sccmxml.class.php index 97a2dce..49ae09d 100644 --- a/inc/sccmxml.class.php +++ b/inc/sccmxml.class.php @@ -30,27 +30,28 @@ */ if (!defined('GLPI_ROOT')) { - die("Sorry. You can't access directly to this file"); + die("Sorry. You can't access directly to this file"); } -class PluginSccmSccmxml { +class PluginSccmSccmxml +{ + var $data; + var $device_id; + var $sxml; + var $agentbuildnumber; + var $username; - var $data; - var $device_id; - var $sxml; - var $agentbuildnumber; - var $username; + function __construct($data) + { - function __construct($data) { + $plug = new Plugin(); + $plug->getFromDBbyDir("sccm"); - $plug = new Plugin(); - $plug->getFromDBbyDir("sccm"); + $this->data = $data; + $this->device_id = $data['CSD-MachineID']; + $this->agentbuildnumber = "SCCM-v" . $plug->fields['version']; - $this->data = $data; - $this->device_id = $data['CSD-MachineID']; - $this->agentbuildnumber = "SCCM-v".$plug->fields['version']; - - $SXML=<< @@ -61,344 +62,360 @@ function __construct($data) { XML; - $this->sxml = new SimpleXMLElement($SXML); - } - - function setAccessLog() { - $CONTENT = $this->sxml->CONTENT[0]; - $CONTENT->addChild('ACCESSLOG'); - - $ACCESSLOG = $this->sxml->CONTENT[0]->ACCESSLOG; - $ACCESSLOG->addChild('LOGDATE', date('Y-m-d h:i:s')); - - if (!empty($this->data['VrS-UserName'])) { - $this->username = $this->data['VrS-UserName']; - } else { - if (!empty($this->data['SDI-UserName'])) { - $this->username = $this->data['SDI-UserName']; - } else { - if (!empty($this->data['CSD-UserName'])) { - if (preg_match_all("#\\ (.*)#", $this->data['CSD-UserName'], $matches)) { - $this->data['CSD-UserName'] = $matches[1][0]; - } - - $this->username = $this->data['CSD-UserName']; + $this->sxml = new SimpleXMLElement($SXML); + } + + function setAccessLog() + { + $CONTENT = $this->sxml->CONTENT[0]; + $CONTENT->addChild('ACCESSLOG'); + + $ACCESSLOG = $this->sxml->CONTENT[0]->ACCESSLOG; + $ACCESSLOG->addChild('LOGDATE', date('Y-m-d h:i:s')); + + if (!empty($this->data['VrS-UserName'])) { + $this->username = $this->data['VrS-UserName']; + } else { + if (!empty($this->data['SDI-UserName'])) { + $this->username = $this->data['SDI-UserName']; } else { - $this->username = ""; + if (!empty($this->data['CSD-UserName'])) { + if (preg_match_all("#\\ (.*)#", $this->data['CSD-UserName'], $matches)) { + $this->data['CSD-UserName'] = $matches[1][0]; + } + + $this->username = $this->data['CSD-UserName']; + } else { + $this->username = ""; + } } + } + + $ACCESSLOG->addChild('USERID', $this->username); + } + + function setAccountInfos() + { + $CONTENT = $this->sxml->CONTENT[0]; + $CONTENT->addChild('ACCOUNTINFO'); + + $ACCOUNTINFO = $this->sxml->CONTENT[0]->ACCOUNTINFO; + $ACCOUNTINFO->addChild('KEYNAME', 'TAG'); + $ACCOUNTINFO->addChild('KEYVALUE', 'SCCM'); + } + + function setHardware() + { + $CONTENT = $this->sxml->CONTENT[0]; + $CONTENT->addChild('HARDWARE'); + + $HARDWARE = $this->sxml->CONTENT[0]->HARDWARE; + $HARDWARE->addChild('NAME', strtoupper($this->data['MD-SystemName'])); + //$HARDWARE->addChild('CHASSIS_TYPE',$this->data['SD-SystemRole']); + $HARDWARE->addChild('LASTLOGGEDUSER', $this->username); + $HARDWARE->addChild('UUID', substr($this->data['SD-UUID'], 5)); + $HARDWARE->addChild('USERID', $this->username); + $HARDWARE->addChild('WORKGROUP', $this->data['CSD-Domain']); + } + + function setOS() + { + $versionOS = $this->data['OSD-Version']; + + $CONTENT = $this->sxml->CONTENT[0]; + $CONTENT->addChild('OPERATINGSYSTEM'); + + $HARDWARE = $this->sxml->CONTENT[0]->HARDWARE; + $HARDWARE->addChild('OSNAME', $this->data['OSD-Caption']); + $HARDWARE->addChild('OSCOMMENTS', $this->data['OSD-CSDVersion']); + $HARDWARE->addChild('OSVERSION', $versionOS); + //$HARDWARE->addChild('WINPRODID', $this->data['CSD-MachineID']); + + $OPERATINGSYSTEM = $this->sxml->CONTENT[0]->OPERATINGSYSTEM; + $OPERATINGSYSTEM->addChild('NAME', $this->data['OSD-Caption']); + $OPERATINGSYSTEM->addChild('FULL_NAME', $this->data['OSD-Caption']); + $OPERATINGSYSTEM->addChild('ARCH', $this->data['CSD-SystemType']); + $OPERATINGSYSTEM->addChild('VERSION', $versionOS); + //$OPERATINGSYSTEM->addChild('SERIALNUMBER', $this->data['OSD-BuildNumber']); + $OPERATINGSYSTEM->addChild('SERVICE_PACK', $this->data['OSD-CSDVersion']); + } + + + + + function setBios() + { + $CONTENT = $this->sxml->CONTENT[0]; + $CONTENT->addChild('BIOS'); + + $BIOS = $this->sxml->CONTENT[0]->BIOS; + //$BIOS->addChild('ASSETTAG', $this->data['PBD-SerialNumber']); + $BIOS->addChild('SMODEL', $this->data['CSD-Model']); + $BIOS->addChild('TYPE', $this->data['SD-SystemRole']); + $BIOS->addChild('MMANUFACTURER', $this->data['CSD-Manufacturer']); + $BIOS->addChild('SMANUFACTURER', $this->data['CSD-Manufacturer']); + $BIOS->addChild('SSN', $this->data['PBD-SerialNumber']); + + // Jul 17 2012 12:00:00:000AM + if (is_object($this->data['PBD-ReleaseDate'])) { + $Date_Sccm = DateTime::createFromFormat( + 'M d Y', + $this->data['PBD-ReleaseDate']->format('M d Y') + ); + } else { + $Date_Sccm = DateTime::createFromFormat( + 'M d Y', + substr($this->data['PBD-ReleaseDate'], 0, 12) + ); + } + + if ($Date_Sccm != false) { + $this->data['PBD-ReleaseDate'] = $Date_Sccm->format('m/d/Y'); + } + + $BIOS->addChild('BDATE', $this->data['PBD-ReleaseDate']); + $BIOS->addChild('BMANUFACTURER', $this->data['PBD-Manufacturer']); + $BIOS->addChild('BVERSION', $this->data['PBD-BiosVersion']); + $BIOS->addChild('SKUNUMBER', $this->data['PBD-Version']); + } + + function setProcessors() + { - } - } - - $ACCESSLOG->addChild('USERID', $this->username); - } - - function setAccountInfos() { - $CONTENT = $this->sxml->CONTENT[0]; - $CONTENT->addChild('ACCOUNTINFO'); - - $ACCOUNTINFO = $this->sxml->CONTENT[0]->ACCOUNTINFO; - $ACCOUNTINFO->addChild('KEYNAME', 'TAG'); - $ACCOUNTINFO->addChild('KEYVALUE', 'SCCM'); - } - - function setHardware() { - $CONTENT = $this->sxml->CONTENT[0]; - $CONTENT->addChild('HARDWARE'); - - $HARDWARE = $this->sxml->CONTENT[0]->HARDWARE; - $HARDWARE->addChild('NAME', strtoupper($this->data['MD-SystemName'])); - //$HARDWARE->addChild('CHASSIS_TYPE',$this->data['SD-SystemRole']); - $HARDWARE->addChild('LASTLOGGEDUSER', $this->username); - $HARDWARE->addChild('UUID', substr($this->data['SD-UUID'], 5)); - $HARDWARE->addChild('USERID', $this->username); - $HARDWARE->addChild('WORKGROUP', $this->data['CSD-Domain']); - } - - function setOS() { - $versionOS = $this->data['OSD-Version']; - - $CONTENT = $this->sxml->CONTENT[0]; - $CONTENT->addChild('OPERATINGSYSTEM'); - - $HARDWARE = $this->sxml->CONTENT[0]->HARDWARE; - $HARDWARE->addChild('OSNAME', $this->data['OSD-Caption']); - $HARDWARE->addChild('OSCOMMENTS', $this->data['OSD-CSDVersion']); - $HARDWARE->addChild('OSVERSION', $versionOS); - //$HARDWARE->addChild('WINPRODID', $this->data['CSD-MachineID']); - - $OPERATINGSYSTEM = $this->sxml->CONTENT[0]->OPERATINGSYSTEM; - $OPERATINGSYSTEM->addChild('NAME', $this->data['OSD-Caption']); - $OPERATINGSYSTEM->addChild('FULL_NAME', $this->data['OSD-Caption']); - $OPERATINGSYSTEM->addChild('ARCH', $this->data['CSD-SystemType']); - $OPERATINGSYSTEM->addChild('VERSION', $versionOS); - //$OPERATINGSYSTEM->addChild('SERIALNUMBER', $this->data['OSD-BuildNumber']); - $OPERATINGSYSTEM->addChild('SERVICE_PACK', $this->data['OSD-CSDVersion']); - } - - - - - function setBios() { - $CONTENT = $this->sxml->CONTENT[0]; - $CONTENT->addChild('BIOS'); - - $BIOS = $this->sxml->CONTENT[0]->BIOS; - //$BIOS->addChild('ASSETTAG', $this->data['PBD-SerialNumber']); - $BIOS->addChild('SMODEL', $this->data['CSD-Model']); - $BIOS->addChild('TYPE', $this->data['SD-SystemRole']); - $BIOS->addChild('MMANUFACTURER', $this->data['CSD-Manufacturer']); - $BIOS->addChild('SMANUFACTURER', $this->data['CSD-Manufacturer']); - $BIOS->addChild('SSN', $this->data['PBD-SerialNumber']); - - // Jul 17 2012 12:00:00:000AM - if (is_object($this->data['PBD-ReleaseDate'])) { - $Date_Sccm = DateTime::createFromFormat('M d Y', - $this->data['PBD-ReleaseDate']->format('M d Y')); - } else { - $Date_Sccm = DateTime::createFromFormat('M d Y', - substr($this->data['PBD-ReleaseDate'], 0, 12)); - } - - if ($Date_Sccm != false) { - $this->data['PBD-ReleaseDate'] = $Date_Sccm->format('m/d/Y'); - } - - $BIOS->addChild('BDATE', $this->data['PBD-ReleaseDate']); - $BIOS->addChild('BMANUFACTURER', $this->data['PBD-Manufacturer']); - $BIOS->addChild('BVERSION', $this->data['PBD-BiosVersion']); - $BIOS->addChild('SKUNUMBER', $this->data['PBD-Version']); - } - - function setProcessors() { - - $PluginSccmSccm = new PluginSccmSccm(); - - $cpukeys = []; - - $CONTENT = $this->sxml->CONTENT[0]; $i = 0; - foreach ($PluginSccmSccm->getDatas('processors', $this->device_id) as $value) { - if (!in_array($value['CPUKey00'], $cpukeys)) { - $CONTENT->addChild('CPUS'); - $CPUS = $this->sxml->CONTENT[0]->CPUS[$i]; - $CPUS->addChild('DESCRIPTION', $value['Name00']); - $CPUS->addChild('MANUFACTURER', $value['Manufacturer00']); - $CPUS->addChild('NAME', $value['Name00']); - $CPUS->addChild('SPEED', $value['NormSpeed00']); - $CPUS->addChild('TYPE', $value['AddressWidth00']); - $CPUS->addChild('CORE', $value['NumberOfCores00']); - $CPUS->addChild('THREAD', $value['NumberOfLogicalProcessors00']); - $i++; - - // save actual cpukeys for duplicity - $cpukeys[] = $value['CPUKey00']; - } - } - } + $PluginSccmSccm = new PluginSccmSccm(); - function setSoftwares() { + $cpukeys = []; + + $CONTENT = $this->sxml->CONTENT[0]; + $i = 0; + foreach ($PluginSccmSccm->getDatas('processors', $this->device_id) as $value) { + if (!in_array($value['CPUKey00'], $cpukeys)) { + $CONTENT->addChild('CPUS'); + $CPUS = $this->sxml->CONTENT[0]->CPUS[$i]; + $CPUS->addChild('DESCRIPTION', $value['Name00']); + $CPUS->addChild('MANUFACTURER', $value['Manufacturer00']); + $CPUS->addChild('NAME', $value['Name00']); + $CPUS->addChild('SPEED', $value['NormSpeed00']); + $CPUS->addChild('TYPE', $value['AddressWidth00']); + $CPUS->addChild('CORE', $value['NumberOfCores00']); + $CPUS->addChild('THREAD', $value['NumberOfLogicalProcessors00']); + $i++; + + // save actual cpukeys for duplicity + $cpukeys[] = $value['CPUKey00']; + } + } + } - $PluginSccmSccm = new PluginSccmSccm(); + function setSoftwares() + { - $antivirus = []; $inject_antivirus = false; - $CONTENT = $this->sxml->CONTENT[0]; $i = 0; - foreach ($PluginSccmSccm->getSoftware($this->device_id) as $value) { + $PluginSccmSccm = new PluginSccmSccm(); - $CONTENT->addChild('SOFTWARES'); - $SOFTWARES = $this->sxml->CONTENT[0]->SOFTWARES[$i]; + $antivirus = []; + $inject_antivirus = false; + $CONTENT = $this->sxml->CONTENT[0]; + $i = 0; + foreach ($PluginSccmSccm->getSoftware($this->device_id) as $value) { + $CONTENT->addChild('SOFTWARES'); + $SOFTWARES = $this->sxml->CONTENT[0]->SOFTWARES[$i]; - if (isset($value['ArPd-DisplayName']) && preg_match("#&#", $value['ArPd-DisplayName'])) { - $value['ArPd-DisplayName'] = preg_replace("#&#", "&", $value['ArPd-DisplayName']); - } + if (isset($value['ArPd-DisplayName']) && preg_match("#&#", $value['ArPd-DisplayName'])) { + $value['ArPd-DisplayName'] = preg_replace("#&#", "&", $value['ArPd-DisplayName']); + } - if (isset($value['ArPd-Publisher']) && preg_match("#&#", $value['ArPd-Publisher'])) { - $value['ArPd-Publisher'] = preg_replace("#&#", "&", $value['ArPd-Publisher']); - } + if (isset($value['ArPd-Publisher']) && preg_match("#&#", $value['ArPd-Publisher'])) { + $value['ArPd-Publisher'] = preg_replace("#&#", "&", $value['ArPd-Publisher']); + } - $SOFTWARES->addChild('NAME', $value['ArPd-DisplayName'] ?: NOT_AVAILABLE); + $SOFTWARES->addChild('NAME', $value['ArPd-DisplayName'] ?: NOT_AVAILABLE); - if (isset($value['ArPd-Version'])) { - $SOFTWARES->addChild('VERSION', $value['ArPd-Version']); - } + if (isset($value['ArPd-Version'])) { + $SOFTWARES->addChild('VERSION', $value['ArPd-Version']); + } - if (isset($value['ArPd-Publisher'])) { - $SOFTWARES->addChild('PUBLISHER', $value['ArPd-Publisher']); - } + if (isset($value['ArPd-Publisher'])) { + $SOFTWARES->addChild('PUBLISHER', $value['ArPd-Publisher']); + } - if (isset($value['ArPd-InstallDate'])) { - $Date_Sccm = DateTime::createFromFormat('Ymd', $value['ArPd-InstallDate']); - if ($Date_Sccm != false) { - $SOFTWARES->addChild('INSTALLDATE', $Date_Sccm->format('d/m/Y')); + if (isset($value['ArPd-InstallDate'])) { + $Date_Sccm = DateTime::createFromFormat('Ymd', $value['ArPd-InstallDate']); + if ($Date_Sccm != false) { + $SOFTWARES->addChild('INSTALLDATE', $Date_Sccm->format('d/m/Y')); + } } - } - $i++; + $i++; - if (isset($value['ArPd-DisplayName']) && preg_match('#Kaspersky Endpoint Security#', $value['ArPd-DisplayName'])) { - $antivirus = $value['ArPd-DisplayName']; - $inject_antivirus = true; - } - } + if (isset($value['ArPd-DisplayName']) && preg_match('#Kaspersky Endpoint Security#', $value['ArPd-DisplayName'])) { + $antivirus = $value['ArPd-DisplayName']; + $inject_antivirus = true; + } + } - if ($inject_antivirus) { - $this->setAntivirus($antivirus); - } - } + if ($inject_antivirus) { + $this->setAntivirus($antivirus); + } + } - function setMemories() { + function setMemories() + { $PluginSccmSccm = new PluginSccmSccm(); - $CONTENT = $this->sxml->CONTENT[0]; $i = 0; - foreach ($PluginSccmSccm->getMemories($this->device_id) as $value) { - - $CONTENT->addChild('MEMORIES'); - $MEMORIES = $this->sxml->CONTENT[0]->MEMORIES[$i]; - - $MEMORIES->addChild('CAPACITY', $value['Mem-Capacity']); - $MEMORIES->addChild('CAPTION', $value['Mem-Caption']); - $MEMORIES->addChild('DESCRIPTION', $value['Mem-Description']); - $MEMORIES->addChild('FORMFACTOR', $value['Mem-FormFactor']); - $MEMORIES->addChild('REMOVABLE', $value['Mem-Removable']); - $MEMORIES->addChild('PURPOSE', $value['Mem-Purpose']); - $MEMORIES->addChild('SPEED', $value['Mem-Speed']); - $MEMORIES->addChild('TYPE', $value['Mem-Type']); - $MEMORIES->addChild('NUMSLOTS', $value['Mem-NumSlots']); - $MEMORIES->addChild('SERIALNUMBER', $value['Mem-SerialNumber']); - $MEMORIES->addChild('MANUFACTURER', $value['Mem-Manufacturer']); + $CONTENT = $this->sxml->CONTENT[0]; + $i = 0; + foreach ($PluginSccmSccm->getMemories($this->device_id) as $value) { + $CONTENT->addChild('MEMORIES'); + $MEMORIES = $this->sxml->CONTENT[0]->MEMORIES[$i]; + + $MEMORIES->addChild('CAPACITY', $value['Mem-Capacity']); + $MEMORIES->addChild('CAPTION', $value['Mem-Caption']); + $MEMORIES->addChild('DESCRIPTION', $value['Mem-Description']); + $MEMORIES->addChild('FORMFACTOR', $value['Mem-FormFactor']); + $MEMORIES->addChild('REMOVABLE', $value['Mem-Removable']); + $MEMORIES->addChild('PURPOSE', $value['Mem-Purpose']); + $MEMORIES->addChild('SPEED', $value['Mem-Speed']); + $MEMORIES->addChild('TYPE', $value['Mem-Type']); + $MEMORIES->addChild('NUMSLOTS', $value['Mem-NumSlots']); + $MEMORIES->addChild('SERIALNUMBER', $value['Mem-SerialNumber']); + $MEMORIES->addChild('MANUFACTURER', $value['Mem-Manufacturer']); - $i++; - } - - } + $i++; + } + } - function setVideos() { + function setVideos() + { $PluginSccmSccm = new PluginSccmSccm(); - $CONTENT = $this->sxml->CONTENT[0]; $i = 0; - foreach ($PluginSccmSccm->getVideos($this->device_id) as $value) { - - $CONTENT->addChild('VIDEOS'); - $VIDEOS = $this->sxml->CONTENT[0]->VIDEOS[$i]; - - $VIDEOS->addChild('CHIPSET', $value['Vid-Chipset']); - $VIDEOS->addChild('MEMORY', $value['Vid-Memory']); - $VIDEOS->addChild('NAME', $value['Vid-Name']); - $VIDEOS->addChild('RESOLUTION', $value['Vid-Resolution']); - $VIDEOS->addChild('PCISLOT', $value['Vid-PciSlot']); + $CONTENT = $this->sxml->CONTENT[0]; + $i = 0; + foreach ($PluginSccmSccm->getVideos($this->device_id) as $value) { + $CONTENT->addChild('VIDEOS'); + $VIDEOS = $this->sxml->CONTENT[0]->VIDEOS[$i]; - $i++; - } - } + $VIDEOS->addChild('CHIPSET', $value['Vid-Chipset']); + $VIDEOS->addChild('MEMORY', $value['Vid-Memory']); + $VIDEOS->addChild('NAME', $value['Vid-Name']); + $VIDEOS->addChild('RESOLUTION', $value['Vid-Resolution']); + $VIDEOS->addChild('PCISLOT', $value['Vid-PciSlot']); - function setSounds() { - $PluginSccmSccm = new PluginSccmSccm(); - - $CONTENT = $this->sxml->CONTENT[0]; $i = 0; - foreach ($PluginSccmSccm->getSounds($this->device_id) as $value) { + $i++; + } + } - $CONTENT->addChild('SOUNDS'); - $SOUNDS = $this->sxml->CONTENT[0]->SOUNDS[$i]; + function setSounds() + { + $PluginSccmSccm = new PluginSccmSccm(); - $SOUNDS->addChild('DESCRIPTION', $value['Snd-Description']); - $SOUNDS->addChild('MANUFACTURER', $value['Snd-Manufacturer']); - $SOUNDS->addChild('NAME', $value['Snd-Name']); + $CONTENT = $this->sxml->CONTENT[0]; + $i = 0; + foreach ($PluginSccmSccm->getSounds($this->device_id) as $value) { + $CONTENT->addChild('SOUNDS'); + $SOUNDS = $this->sxml->CONTENT[0]->SOUNDS[$i]; - $i++; - } - } + $SOUNDS->addChild('DESCRIPTION', $value['Snd-Description']); + $SOUNDS->addChild('MANUFACTURER', $value['Snd-Manufacturer']); + $SOUNDS->addChild('NAME', $value['Snd-Name']); - function setAntivirus($value) { - $CONTENT = $this->sxml->CONTENT[0]; - $CONTENT->addChild('ANTIVIRUS'); + $i++; + } + } - $ANTIVIRUS = $this->sxml->CONTENT[0]->ANTIVIRUS; - $ANTIVIRUS->addChild('NAME', $value); - } + function setAntivirus($value) + { + $CONTENT = $this->sxml->CONTENT[0]; + $CONTENT->addChild('ANTIVIRUS'); - function setUsers() { - $CONTENT = $this->sxml->CONTENT[0]; - $CONTENT->addChild('USERS'); + $ANTIVIRUS = $this->sxml->CONTENT[0]->ANTIVIRUS; + $ANTIVIRUS->addChild('NAME', $value); + } - $USERS = $this->sxml->CONTENT[0]->USERS; - $USERS->addChild('LOGIN', $this->username); - } + function setUsers() + { + $CONTENT = $this->sxml->CONTENT[0]; + $CONTENT->addChild('USERS'); - function setNetworks() { + $USERS = $this->sxml->CONTENT[0]->USERS; + $USERS->addChild('LOGIN', $this->username); + } - $PluginSccmSccm = new PluginSccmSccm(); + function setNetworks() + { - $CONTENT = $this->sxml->CONTENT[0]; + $PluginSccmSccm = new PluginSccmSccm(); - $networks = $PluginSccmSccm->getNetwork($this->device_id); + $CONTENT = $this->sxml->CONTENT[0]; - if (count($networks) > 0) { + $networks = $PluginSccmSccm->getNetwork($this->device_id); - $i = 0; + if (count($networks) > 0) { + $i = 0; - foreach ($networks as $value) { - //SCCM database store each IP format in one row, we need to split it - //and add each IP in dedicated XML node - $parts = explode(",", $value['ND-IpAddress']); - foreach ($parts as $ip) { - $CONTENT->addChild('NETWORKS'); - $NETWORKS = $this->sxml->CONTENT[0]->NETWORKS[$i]; + foreach ($networks as $value) { + //SCCM database store each IP format in one row, we need to split it + //and add each IP in dedicated XML node + $parts = explode(",", $value['ND-IpAddress']); + foreach ($parts as $ip) { + $CONTENT->addChild('NETWORKS'); + $NETWORKS = $this->sxml->CONTENT[0]->NETWORKS[$i]; - if(filter_var(trim($ip), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)){ - $NETWORKS->addChild('IPADDRESS', trim($ip)); - } else { - $NETWORKS->addChild('IPADDRESS6', trim($ip)); - } + if (filter_var(trim($ip), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { + $NETWORKS->addChild('IPADDRESS', trim($ip)); + } else { + $NETWORKS->addChild('IPADDRESS6', trim($ip)); + } - $NETWORKS->addChild('DESCRIPTION', $value['ND-Name']); - $NETWORKS->addChild('IPMASK', $value['ND-IpSubnet']); - $NETWORKS->addChild('IPDHCP', $value['ND-DHCPServer']); - $NETWORKS->addChild('IPGATEWAY', $value['ND-IpGateway']); - $NETWORKS->addChild('MACADDR', $value['ND-MacAddress']); + $NETWORKS->addChild('DESCRIPTION', $value['ND-Name']); + $NETWORKS->addChild('IPMASK', $value['ND-IpSubnet']); + $NETWORKS->addChild('IPDHCP', $value['ND-DHCPServer']); + $NETWORKS->addChild('IPGATEWAY', $value['ND-IpGateway']); + $NETWORKS->addChild('MACADDR', $value['ND-MacAddress']); - $i++; + $i++; + } } - } - } - } - - function setStorages() { - $PluginSccmSccm = new PluginSccmSccm(); - $CONTENT = $this->sxml->CONTENT[0]; - $i = 0; - foreach ($PluginSccmSccm->getStorages($this->device_id) as $value) { - $value['gld-TotalSize'] = intval($value['gld-TotalSize'])*1024; - $value['gld-FreeSpace'] = intval($value['gld-FreeSpace'])*1024; - $CONTENT->addChild('DRIVES'); - $DRIVES = $this->sxml->CONTENT[0]->DRIVES[$i]; - $DRIVES->addChild('DESCRIPTION', $value['gld-Description']); - $DRIVES->addChild('FILESYSTEM', $value['gld-FileSystem']); - $DRIVES->addChild('FREE', $value['gld-FreeSpace']); - $DRIVES->addChild('LABEL', $value['gdi-Caption']); - $DRIVES->addChild('LETTER', $value['gld-MountingPoint']); - $DRIVES->addChild('TOTAL', $value['gld-TotalSize']); - $DRIVES->addChild('VOLUMN', $value['gld-Partition']); - $i++; - } - - $i = 0; - foreach ($PluginSccmSccm->getMedias($this->device_id) as $value) { - $CONTENT->addChild('STORAGES'); - $STORAGES = $this->sxml->CONTENT[0]->STORAGES[$i]; - $STORAGES->addChild('DESCRIPTION', $value['Med-Description']); - $STORAGES->addChild('MANUFACTURER', $value['Med-Manufacturer']); - $STORAGES->addChild('MODEL', $value['Med-Model']); - $STORAGES->addChild('NAME', $value['Med-Name']); - $STORAGES->addChild('SCSI_COID', $value['Med-SCSITargetId']); - $STORAGES->addChild('SCSI_LUN', 0); - $STORAGES->addChild('SCSI_UNID', 0); - $STORAGES->addChild('TYPE', $value['Med-Type']); - $i++; - } - } - - function object2array($object) { - return @json_decode(@json_encode($object), 1); - } + } + } -} + function setStorages() + { + $PluginSccmSccm = new PluginSccmSccm(); + $CONTENT = $this->sxml->CONTENT[0]; + $i = 0; + foreach ($PluginSccmSccm->getStorages($this->device_id) as $value) { + $value['gld-TotalSize'] = intval($value['gld-TotalSize']) * 1024; + $value['gld-FreeSpace'] = intval($value['gld-FreeSpace']) * 1024; + $CONTENT->addChild('DRIVES'); + $DRIVES = $this->sxml->CONTENT[0]->DRIVES[$i]; + $DRIVES->addChild('DESCRIPTION', $value['gld-Description']); + $DRIVES->addChild('FILESYSTEM', $value['gld-FileSystem']); + $DRIVES->addChild('FREE', $value['gld-FreeSpace']); + $DRIVES->addChild('LABEL', $value['gdi-Caption']); + $DRIVES->addChild('LETTER', $value['gld-MountingPoint']); + $DRIVES->addChild('TOTAL', $value['gld-TotalSize']); + $DRIVES->addChild('VOLUMN', $value['gld-Partition']); + $i++; + } + + $i = 0; + foreach ($PluginSccmSccm->getMedias($this->device_id) as $value) { + $CONTENT->addChild('STORAGES'); + $STORAGES = $this->sxml->CONTENT[0]->STORAGES[$i]; + $STORAGES->addChild('DESCRIPTION', $value['Med-Description']); + $STORAGES->addChild('MANUFACTURER', $value['Med-Manufacturer']); + $STORAGES->addChild('MODEL', $value['Med-Model']); + $STORAGES->addChild('NAME', $value['Med-Name']); + $STORAGES->addChild('SCSI_COID', $value['Med-SCSITargetId']); + $STORAGES->addChild('SCSI_LUN', 0); + $STORAGES->addChild('SCSI_UNID', 0); + $STORAGES->addChild('TYPE', $value['Med-Type']); + $i++; + } + } + function object2array($object) + { + return @json_decode(@json_encode($object), 1); + } +} diff --git a/setup.php b/setup.php index 9b26604..2188544 100644 --- a/setup.php +++ b/setup.php @@ -36,24 +36,24 @@ // Maximum GLPI version, exclusive define("PLUGIN_SCCM_MAX_GLPI", "10.0.99"); -function plugin_init_sccm() { - global $PLUGIN_HOOKS; +function plugin_init_sccm() +{ + global $PLUGIN_HOOKS; - $plugin = new Plugin(); + $plugin = new Plugin(); - $PLUGIN_HOOKS['csrf_compliant']['sccm'] = true; - $PLUGIN_HOOKS['menu_entry']['sccm'] = false; + $PLUGIN_HOOKS['csrf_compliant']['sccm'] = true; + $PLUGIN_HOOKS['menu_entry']['sccm'] = false; - if ($plugin->isActivated("sccm") && Session::getLoginUserID()) { - if (Session::haveRight("config", UPDATE)) { - - $PLUGIN_HOOKS['config_page']['sccm'] = "front/config.form.php"; - $PLUGIN_HOOKS["menu_toadd"]['sccm'] = ['config' => 'PluginSccmMenu']; - } - } + if ($plugin->isActivated("sccm") && Session::getLoginUserID()) { + if (Session::haveRight("config", UPDATE)) { + $PLUGIN_HOOKS['config_page']['sccm'] = "front/config.form.php"; + $PLUGIN_HOOKS["menu_toadd"]['sccm'] = ['config' => 'PluginSccmMenu']; + } + } // Encryption - $PLUGIN_HOOKS['secured_fields']['sccm'] = ['glpi_plugin_sccm_configs.sccmdb_password']; + $PLUGIN_HOOKS['secured_fields']['sccm'] = ['glpi_plugin_sccm_configs.sccmdb_password']; } /** @@ -61,31 +61,32 @@ function plugin_init_sccm() { * * @return array */ -function plugin_version_sccm() { +function plugin_version_sccm() +{ - return [ - 'name' => __("Interface - SCCM", "sccm"), - 'version' => PLUGIN_SCCM_VERSION, - 'author' => 'TECLIB\'', - 'license' => 'GPLv3', - 'homepage'=>'https://github.com/pluginsGLPI/sccm', - 'requirements' => [ - 'glpi' => [ - 'min' => PLUGIN_SCCM_MIN_GLPI, - 'max' => PLUGIN_SCCM_MAX_GLPI, - ], - 'php' => [ - 'exts'=> [ - 'sqlsrv' => [ - 'required' => true, - 'function' => 'sqlsrv_connect' - ], - 'curl' => [ - 'required' => true, - 'function' => 'curl_init' - ] + return [ + 'name' => __("Interface - SCCM", "sccm"), + 'version' => PLUGIN_SCCM_VERSION, + 'author' => 'TECLIB\'', + 'license' => 'GPLv3', + 'homepage' => 'https://github.com/pluginsGLPI/sccm', + 'requirements' => [ + 'glpi' => [ + 'min' => PLUGIN_SCCM_MIN_GLPI, + 'max' => PLUGIN_SCCM_MAX_GLPI, + ], + 'php' => [ + 'exts' => [ + 'sqlsrv' => [ + 'required' => true, + 'function' => 'sqlsrv_connect' + ], + 'curl' => [ + 'required' => true, + 'function' => 'curl_init' + ] + ] ] - ] - ] - ]; + ] + ]; } From 560fc5c7cf860f864b88e383492358aa882260d6 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Mon, 30 Oct 2023 08:23:39 +0100 Subject: [PATCH 03/10] camel caps --- inc/sccm.class.php | 20 ++++++++++---------- inc/sccmdb.class.php | 8 ++++---- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/inc/sccm.class.php b/inc/sccm.class.php index be55b0a..217e9ab 100644 --- a/inc/sccm.class.php +++ b/inc/sccm.class.php @@ -64,7 +64,7 @@ function getDevices($where = 0, $limit = 99999999) $query .= " WHERE csd.MachineID = '" . $where . "'"; } - $result = $PluginSccmSccmdb->exec_query($query); + $result = $PluginSccmSccmdb->execQuery($query); $i = 0; $tab = []; @@ -102,7 +102,7 @@ function getDatas($type, $deviceid, $limit = 99999999) $query .= " FROM " . $table . "\n"; $query .= " WHERE MachineID = '" . $deviceid . "'" . "\n"; - $result = $PluginSccmSccmdb->exec_query($query); + $result = $PluginSccmSccmdb->execQuery($query); $data = []; @@ -146,7 +146,7 @@ function getNetwork($deviceid, $limit = 99999999) WHERE MACAddress00 is not null AND NeDa.MachineID = '" . $deviceid . "'"; - $result = $PluginSccmSccmdb->exec_query($query); + $result = $PluginSccmSccmdb->execQuery($query); $data = []; @@ -195,7 +195,7 @@ function getSoftware($deviceid, $limit = 99999999) WHERE ArPd.ResourceID = $deviceid AND (ArPd.DisplayName0 is not null and ArPd.DisplayName0 <> '')"; - $result = $PluginSccmSccmdb->exec_query($query); + $result = $PluginSccmSccmdb->execQuery($query); $data = []; @@ -244,7 +244,7 @@ function getMemories($deviceid, $limit = 99999999) ORDER BY \"Mem-NumSlots\""; - $result = $PluginSccmSccmdb->exec_query($query); + $result = $PluginSccmSccmdb->execQuery($query); $data = []; @@ -287,7 +287,7 @@ function getVideos($deviceid, $limit = 99999999) AND ResourceID = '" . $deviceid . "' ORDER BY GroupID"; - $result = $PluginSccmSccmdb->exec_query($query); + $result = $PluginSccmSccmdb->execQuery($query); $data = []; @@ -326,7 +326,7 @@ function getSounds($deviceid, $limit = 99999999) FROM v_GS_SOUND_DEVICE WHERE ResourceID = '" . $deviceid . "'"; - $result = $PluginSccmSccmdb->exec_query($query); + $result = $PluginSccmSccmdb->execQuery($query); $data = []; @@ -374,7 +374,7 @@ function getStorages($deviceid, $limit = 99999999) WHERE gld.GroupID = gdi.GroupID AND gld.ResourceID = '" . $deviceid . "'"; - $result = $PluginSccmSccmdb->exec_query($query); + $result = $PluginSccmSccmdb->execQuery($query); $data = []; @@ -416,7 +416,7 @@ function getMedias($deviceid, $limit = 99999999) FROM v_GS_CDROM WHERE ResourceID = '" . $deviceid . "'"; - $result = $PluginSccmSccmdb->exec_query($query); + $result = $PluginSccmSccmdb->execQuery($query); $data = []; @@ -602,7 +602,7 @@ static function executePush($task) if ($PluginSccmConfig->getField('active_sync') == 1) { if ($res) { $query = self::getcomputerQuery(); - $result = $PluginSccmSccmdb->exec_query($query); + $result = $PluginSccmSccmdb->execQuery($query); $tab = []; diff --git a/inc/sccmdb.class.php b/inc/sccmdb.class.php index 732e7e1..88ae89f 100644 --- a/inc/sccmdb.class.php +++ b/inc/sccmdb.class.php @@ -59,7 +59,7 @@ function connect() $this->dbconn = sqlsrv_connect($host, $connectionOptions); if ($this->dbconn === false) { - $this->FormatErrors(sqlsrv_errors()); + $this->formatErrors(sqlsrv_errors()); return false; } @@ -72,17 +72,17 @@ function disconnect() sqlsrv_close($this->dbconn); } - function exec_query($query) + public function execQuery($query) { $result = sqlsrv_query($this->dbconn, $query) or die('Query error : ' . print_r(sqlsrv_errors(), true)); if ($result == false) { - die(FormatErrors(sqlsrv_errors())); + die(formatErrors(sqlsrv_errors())); } return $result; } - function FormatErrors($errors) + private function formatErrors($errors) { foreach ($errors as $error) { From 0cf9171c160d1c03ec7751712a5eb8539ebd2bca Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Mon, 30 Oct 2023 08:28:11 +0100 Subject: [PATCH 04/10] Methods/properties visibility, properties declarations --- inc/config.class.php | 20 ++++++++++---------- inc/menu.class.php | 6 +++--- inc/sccm.class.php | 40 ++++++++++++++++++++-------------------- inc/sccmdb.class.php | 6 +++--- inc/sccmxml.class.php | 42 +++++++++++++++++++++--------------------- 5 files changed, 57 insertions(+), 57 deletions(-) diff --git a/inc/config.class.php b/inc/config.class.php index fcb90ef..e9c547e 100644 --- a/inc/config.class.php +++ b/inc/config.class.php @@ -37,32 +37,32 @@ class PluginSccmConfig extends CommonDBTM { private static $_instance = null; - static function canCreate() + public static function canCreate() { return Session::haveRight('config', UPDATE); } - static function canUpdate() + public static function canUpdate() { return Session::haveRight('config', UPDATE); } - static function canView() + public static function canView() { return Session::haveRight('config', UPDATE); } - static function getTypeName($nb = 0) + public static function getTypeName($nb = 0) { return __("Setup - SCCM", "sccm"); } - function getName($with_comment = 0) + public function getName($with_comment = 0) { return __("Interface - SCCM", "sccm"); } - static function getInstance() + public static function getInstance() { if (!isset(self::$_instance)) { @@ -75,7 +75,7 @@ static function getInstance() } - function prepareInputForUpdate($input) + public function prepareInputForUpdate($input) { if (isset($input["sccmdb_password"]) and !empty($input["sccmdb_password"])) { $input["sccmdb_password"] = (new GLPIKey())->encrypt($input["sccmdb_password"]); @@ -88,7 +88,7 @@ function prepareInputForUpdate($input) return $input; } - static function install(Migration $migration) + public static function install(Migration $migration) { global $CFG_GLPI, $DB; @@ -221,7 +221,7 @@ static function install(Migration $migration) } - static function uninstall() + public static function uninstall() { global $DB; @@ -233,7 +233,7 @@ static function uninstall() } - static function showConfigForm($item) + public static function showConfigForm($item) { global $CFG_GLPI; diff --git a/inc/menu.class.php b/inc/menu.class.php index 954e558..ce4a74d 100644 --- a/inc/menu.class.php +++ b/inc/menu.class.php @@ -31,17 +31,17 @@ class PluginSccmMenu extends CommonGLPI { - static function getTypeName($nb = 0) + public static function getTypeName($nb = 0) { return __('SCCM Connector', 'sccm'); } - static function getMenuName() + public static function getMenuName() { return __('SCCM Connector', 'sccm'); } - static function getMenuContent() + public static function getMenuContent() { global $CFG_GLPI; $menu = []; diff --git a/inc/sccm.class.php b/inc/sccm.class.php index 217e9ab..5152104 100644 --- a/inc/sccm.class.php +++ b/inc/sccm.class.php @@ -37,19 +37,19 @@ class PluginSccmSccm { - var $devices; + public $devices; - static function getTypeName($nb = 0) + public static function getTypeName($nb = 0) { return __('SCCM', 'sccm'); } - function showHome() + public function showHome() { echo __('Please, read the documentation before using that.', 'footprints'); } - function getDevices($where = 0, $limit = 99999999) + public function getDevices($where = 0, $limit = 99999999) { $PluginSccmSccmdb = new PluginSccmSccmdb(); @@ -80,7 +80,7 @@ function getDevices($where = 0, $limit = 99999999) $PluginSccmSccmdb->disconnect(); } - function getDatas($type, $deviceid, $limit = 99999999) + public function getDatas($type, $deviceid, $limit = 99999999) { $PluginSccmSccmdb = new PluginSccmSccmdb(); @@ -124,7 +124,7 @@ function getDatas($type, $deviceid, $limit = 99999999) return $data; } - function getNetwork($deviceid, $limit = 99999999) + public function getNetwork($deviceid, $limit = 99999999) { $PluginSccmSccmdb = new PluginSccmSccmdb(); @@ -168,7 +168,7 @@ function getNetwork($deviceid, $limit = 99999999) return $data; } - function getSoftware($deviceid, $limit = 99999999) + public function getSoftware($deviceid, $limit = 99999999) { $PluginSccmSccmdb = new PluginSccmSccmdb(); @@ -217,7 +217,7 @@ function getSoftware($deviceid, $limit = 99999999) return $data; } - function getMemories($deviceid, $limit = 99999999) + public function getMemories($deviceid, $limit = 99999999) { $PluginSccmSccmdb = new PluginSccmSccmdb(); @@ -266,7 +266,7 @@ function getMemories($deviceid, $limit = 99999999) return $data; } - function getVideos($deviceid, $limit = 99999999) + public function getVideos($deviceid, $limit = 99999999) { $PluginSccmSccmdb = new PluginSccmSccmdb(); @@ -309,7 +309,7 @@ function getVideos($deviceid, $limit = 99999999) return $data; } - function getSounds($deviceid, $limit = 99999999) + public function getSounds($deviceid, $limit = 99999999) { $PluginSccmSccmdb = new PluginSccmSccmdb(); @@ -348,7 +348,7 @@ function getSounds($deviceid, $limit = 99999999) return $data; } - function getStorages($deviceid, $limit = 99999999) + public function getStorages($deviceid, $limit = 99999999) { $PluginSccmSccmdb = new PluginSccmSccmdb(); @@ -396,7 +396,7 @@ function getStorages($deviceid, $limit = 99999999) return $data; } - function getMedias($deviceid, $limit = 99999999) + public function getMedias($deviceid, $limit = 99999999) { $PluginSccmSccmdb = new PluginSccmSccmdb(); @@ -438,7 +438,7 @@ function getMedias($deviceid, $limit = 99999999) return $data; } - static function install() + public static function install() { $cronCollect = new CronTask(); @@ -464,22 +464,22 @@ static function install() ); } - static function uninstall() + public static function uninstall() { CronTask::unregister(__CLASS__); } - static function cronSCCMCollect($task) + public static function cronSCCMCollect($task) { return self::executeCollect($task); } - static function cronSCCMPush($task) + public static function cronSCCMPush($task) { return self::executePush($task); } - static function cronInfo($name) + public static function cronInfo($name) { if ($name == "SCCMCollect") { return ['description' => __("Interface - SCCMCollect", "sccm")]; @@ -489,7 +489,7 @@ static function cronInfo($name) } } - static function executeCollect($task) + public static function executeCollect($task) { ini_set('max_execution_time', 0); $retcode = -1; @@ -541,7 +541,7 @@ static function executeCollect($task) return $retcode; } - static function getcomputerQuery() + public static function getcomputerQuery() { return "SELECT csd.Description00 as \"CSD-Description\", csd.Domain00 as \"CSD-Domain\", @@ -589,7 +589,7 @@ static function getcomputerQuery() } - static function executePush($task) + public static function executePush($task) { global $CFG_GLPI; diff --git a/inc/sccmdb.class.php b/inc/sccmdb.class.php index 88ae89f..8161360 100644 --- a/inc/sccmdb.class.php +++ b/inc/sccmdb.class.php @@ -35,9 +35,9 @@ class PluginSccmSccmdb { - var $dbconn; + private $dbconn; - function connect() + public function connect() { $PluginSccmConfig = new PluginSccmConfig(); @@ -66,7 +66,7 @@ function connect() return true; } - function disconnect() + public function disconnect() { sqlsrv_close($this->dbconn); diff --git a/inc/sccmxml.class.php b/inc/sccmxml.class.php index 49ae09d..ec92634 100644 --- a/inc/sccmxml.class.php +++ b/inc/sccmxml.class.php @@ -35,13 +35,13 @@ class PluginSccmSccmxml { - var $data; - var $device_id; - var $sxml; - var $agentbuildnumber; - var $username; + public $data; + public $device_id; + public $sxml; + public $agentbuildnumber; + public $username; - function __construct($data) + public function __construct($data) { $plug = new Plugin(); @@ -65,7 +65,7 @@ function __construct($data) $this->sxml = new SimpleXMLElement($SXML); } - function setAccessLog() + public function setAccessLog() { $CONTENT = $this->sxml->CONTENT[0]; $CONTENT->addChild('ACCESSLOG'); @@ -94,7 +94,7 @@ function setAccessLog() $ACCESSLOG->addChild('USERID', $this->username); } - function setAccountInfos() + public function setAccountInfos() { $CONTENT = $this->sxml->CONTENT[0]; $CONTENT->addChild('ACCOUNTINFO'); @@ -104,7 +104,7 @@ function setAccountInfos() $ACCOUNTINFO->addChild('KEYVALUE', 'SCCM'); } - function setHardware() + public function setHardware() { $CONTENT = $this->sxml->CONTENT[0]; $CONTENT->addChild('HARDWARE'); @@ -118,7 +118,7 @@ function setHardware() $HARDWARE->addChild('WORKGROUP', $this->data['CSD-Domain']); } - function setOS() + public function setOS() { $versionOS = $this->data['OSD-Version']; @@ -143,7 +143,7 @@ function setOS() - function setBios() + public function setBios() { $CONTENT = $this->sxml->CONTENT[0]; $CONTENT->addChild('BIOS'); @@ -179,7 +179,7 @@ function setBios() $BIOS->addChild('SKUNUMBER', $this->data['PBD-Version']); } - function setProcessors() + public function setProcessors() { $PluginSccmSccm = new PluginSccmSccm(); @@ -207,7 +207,7 @@ function setProcessors() } } - function setSoftwares() + public function setSoftwares() { $PluginSccmSccm = new PluginSccmSccm(); @@ -258,7 +258,7 @@ function setSoftwares() } } - function setMemories() + public function setMemories() { $PluginSccmSccm = new PluginSccmSccm(); @@ -284,7 +284,7 @@ function setMemories() } } - function setVideos() + public function setVideos() { $PluginSccmSccm = new PluginSccmSccm(); @@ -304,7 +304,7 @@ function setVideos() } } - function setSounds() + public function setSounds() { $PluginSccmSccm = new PluginSccmSccm(); @@ -322,7 +322,7 @@ function setSounds() } } - function setAntivirus($value) + public function setAntivirus($value) { $CONTENT = $this->sxml->CONTENT[0]; $CONTENT->addChild('ANTIVIRUS'); @@ -331,7 +331,7 @@ function setAntivirus($value) $ANTIVIRUS->addChild('NAME', $value); } - function setUsers() + public function setUsers() { $CONTENT = $this->sxml->CONTENT[0]; $CONTENT->addChild('USERS'); @@ -340,7 +340,7 @@ function setUsers() $USERS->addChild('LOGIN', $this->username); } - function setNetworks() + public function setNetworks() { $PluginSccmSccm = new PluginSccmSccm(); @@ -378,7 +378,7 @@ function setNetworks() } } - function setStorages() + public function setStorages() { $PluginSccmSccm = new PluginSccmSccm(); $CONTENT = $this->sxml->CONTENT[0]; @@ -414,7 +414,7 @@ function setStorages() } } - function object2array($object) + public function object2array($object) { return @json_decode(@json_encode($object), 1); } From 60f45ac6f8176536e9e005e6ce30a8b913f53cac Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Mon, 30 Oct 2023 08:33:04 +0100 Subject: [PATCH 05/10] Fix actions W --- .github/workflows/continuous-integration.yml | 9 --------- 1 file changed, 9 deletions(-) diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 44ec4de..d0c7feb 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -32,15 +32,6 @@ jobs: php-version: "${{ matrix.php-version }}" coverage: "none" tools: "composer, cs2pr" - - name: "Get Composer cache directory" - id: "composer-cache" - run: | - echo "::set-output name=dir::$(composer config cache-files-dir)" - - name: "Restore dependencies cache" - uses: "actions/cache@v3" - with: - path: "${{ steps.composer-cache.outputs.dir }}" - key: "${{ github.job }}-${{ matrix.php-version }}-dependencies-${{ hashFiles('**/composer.lock') }}" - name: "Install Composer dependencies" run: | composer install --ansi --no-interaction --no-progress --prefer-dist From 3e2e16260c98fb61d744c53aa9d3043039041786 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Mon, 30 Oct 2023 08:44:26 +0100 Subject: [PATCH 06/10] phpstan lvl 0 --- .github/workflows/continuous-integration.yml | 11 +++++++++++ inc/sccmdb.class.php | 2 +- phpstan.neon | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 phpstan.neon diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index d0c7feb..d4a565a 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -44,3 +44,14 @@ jobs: - name: "Check for missing/outdated headers" run: | vendor/bin/licence-headers-check --ansi --no-interaction + - name: Checkout GLPI + uses: actions/checkout@v4 + with: + repository: glpi-project/glpi + path: glpi + fetch-depth: 1 + ref: 10.0/bugfixes + - name: Copy plugin + run: rsync -avr --exclude='glpi' ./* glpi/plugins/fields/ + - name: PHPStan checks + run: cd glpi/plugins/fields && php vendor/bin/phpstan analyze --ansi --memory-limit=1G --no-interaction --no-progress diff --git a/inc/sccmdb.class.php b/inc/sccmdb.class.php index 8161360..e25d4d6 100644 --- a/inc/sccmdb.class.php +++ b/inc/sccmdb.class.php @@ -77,7 +77,7 @@ public function execQuery($query) $result = sqlsrv_query($this->dbconn, $query) or die('Query error : ' . print_r(sqlsrv_errors(), true)); if ($result == false) { - die(formatErrors(sqlsrv_errors())); + die($this->formatErrors(sqlsrv_errors())); } return $result; } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..0d4f38f --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,14 @@ +parameters: + parallel: + maximumNumberOfProcesses: 2 + level: 0 + bootstrapFiles: + - ../../inc/based_config.php + - setup.php + paths: + - inc + scanDirectories: + - ../../inc + - ../../src + stubFiles: + - ../../stubs/glpi_constants.php From 0f7e3c7effee068d7f87139b46544c7d521b6d63 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Mon, 30 Oct 2023 08:47:08 +0100 Subject: [PATCH 07/10] phpstan lvl 1; rename method & drop useless param --- inc/sccm.class.php | 10 +++------- inc/sccmxml.class.php | 3 ++- phpstan.neon | 2 +- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/inc/sccm.class.php b/inc/sccm.class.php index 5152104..9662ad5 100644 --- a/inc/sccm.class.php +++ b/inc/sccm.class.php @@ -80,7 +80,7 @@ public function getDevices($where = 0, $limit = 99999999) $PluginSccmSccmdb->disconnect(); } - public function getDatas($type, $deviceid, $limit = 99999999) + public function getProcessorsDatas($deviceid, $limit = 99999999) { $PluginSccmSccmdb = new PluginSccmSccmdb(); @@ -91,12 +91,8 @@ public function getDatas($type, $deviceid, $limit = 99999999) $datas = []; - switch ($type) { - case 'processors': - $fields = ['Manufacturer00','Name00','NormSpeed00','AddressWidth00','CPUKey00','NumberOfCores00', 'NumberOfLogicalProcessors00']; - $table = 'Processor_DATA'; - break; - } + $fields = ['Manufacturer00','Name00','NormSpeed00','AddressWidth00','CPUKey00','NumberOfCores00', 'NumberOfLogicalProcessors00']; + $table = 'Processor_DATA'; $query = "SELECT " . implode(',', $fields) . "\n"; $query .= " FROM " . $table . "\n"; diff --git a/inc/sccmxml.class.php b/inc/sccmxml.class.php index ec92634..11c8af6 100644 --- a/inc/sccmxml.class.php +++ b/inc/sccmxml.class.php @@ -188,7 +188,8 @@ public function setProcessors() $CONTENT = $this->sxml->CONTENT[0]; $i = 0; - foreach ($PluginSccmSccm->getDatas('processors', $this->device_id) as $value) { + $cpu_data = $PluginSccmSccm->getProcessorsDatas($this->device_id); + foreach ($cpu_data as $value) { if (!in_array($value['CPUKey00'], $cpukeys)) { $CONTENT->addChild('CPUS'); $CPUS = $this->sxml->CONTENT[0]->CPUS[$i]; diff --git a/phpstan.neon b/phpstan.neon index 0d4f38f..7d28e22 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,7 +1,7 @@ parameters: parallel: maximumNumberOfProcesses: 2 - level: 0 + level: 1 bootstrapFiles: - ../../inc/based_config.php - setup.php From c6f6b4050e947359ce5ada38c4e5f23987b82cef Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Mon, 30 Oct 2023 08:49:55 +0100 Subject: [PATCH 08/10] Install glpi deps --- .github/workflows/continuous-integration.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index d4a565a..2e9b982 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -51,6 +51,10 @@ jobs: path: glpi fetch-depth: 1 ref: 10.0/bugfixes + - name: Install xmllint + run: sudo apt-get install -y gettext + - name: "Build dependencies / translations" + run: cd glpi && php bin/console dependencies install && php bin/console locales:compile - name: Copy plugin run: rsync -avr --exclude='glpi' ./* glpi/plugins/fields/ - name: PHPStan checks From 792d946a1f47896a37f91630194104fd1b470251 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Mon, 30 Oct 2023 09:03:44 +0100 Subject: [PATCH 09/10] phpstan lvl 2 --- inc/config.class.php | 2 +- phpstan.neon | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/config.class.php b/inc/config.class.php index e9c547e..69b46ff 100644 --- a/inc/config.class.php +++ b/inc/config.class.php @@ -57,7 +57,7 @@ public static function getTypeName($nb = 0) return __("Setup - SCCM", "sccm"); } - public function getName($with_comment = 0) + public function getName($options = []) { return __("Interface - SCCM", "sccm"); } diff --git a/phpstan.neon b/phpstan.neon index 7d28e22..a9cb812 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,7 +1,7 @@ parameters: parallel: maximumNumberOfProcesses: 2 - level: 1 + level: 2 bootstrapFiles: - ../../inc/based_config.php - setup.php From 579c9a74be991a94ce7fbf2be45ea1e4bb0b2b04 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Mon, 30 Oct 2023 09:11:01 +0100 Subject: [PATCH 10/10] phpstna lvl 5 --- inc/sccm.class.php | 2 +- inc/sccmxml.class.php | 2 +- phpstan.neon | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/inc/sccm.class.php b/inc/sccm.class.php index 9662ad5..03277be 100644 --- a/inc/sccm.class.php +++ b/inc/sccm.class.php @@ -487,7 +487,7 @@ public static function cronInfo($name) public static function executeCollect($task) { - ini_set('max_execution_time', 0); + ini_set('max_execution_time', '0'); $retcode = -1; $REP_XML = GLPI_PLUGIN_DOC_DIR . '/sccm/xml/'; diff --git a/inc/sccmxml.class.php b/inc/sccmxml.class.php index 11c8af6..e974ca4 100644 --- a/inc/sccmxml.class.php +++ b/inc/sccmxml.class.php @@ -417,6 +417,6 @@ public function setStorages() public function object2array($object) { - return @json_decode(@json_encode($object), 1); + return @json_decode(@json_encode($object), true); } } diff --git a/phpstan.neon b/phpstan.neon index a9cb812..fe97519 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,7 +1,7 @@ parameters: parallel: maximumNumberOfProcesses: 2 - level: 2 + level: 5 bootstrapFiles: - ../../inc/based_config.php - setup.php
"; @@ -73,16 +75,23 @@ echo ""; echo ""; echo "
"; if (isset($_POST["go_test"])) { - - $limit = isset($_POST['limit']) ? $_POST['limit'] : 99999999; - $where = isset($_POST['where']) ? $_POST['where'] : 0; - - switch ($_POST["type_test"]) { - case 'view' : testViewHtml($limit, $where); break; - case 'add' : testAdd($where); break; - case 'nbPcToInject' : nbPcToInject(); break; - case 'viewList' : viewList(); break; - } + $limit = isset($_POST['limit']) ? $_POST['limit'] : 99999999; + $where = isset($_POST['where']) ? $_POST['where'] : 0; + + switch ($_POST["type_test"]) { + case 'view': + testViewHtml($limit, $where); + break; + case 'add': + testAdd($where); + break; + case 'nbPcToInject': + nbPcToInject(); + break; + case 'viewList': + viewList(); + break; + } } echo "
"; @@ -91,91 +100,94 @@ Html::footer(); -function viewList() { - global $PluginSccmSccm; +function viewList() +{ + global $PluginSccmSccm; - $PluginSccmSccm->getDevices(); + $PluginSccmSccm->getDevices(); - echo ""; - foreach ($PluginSccmSccm->devices as $device_values) { - echo ""; - } - echo "
".$device_values['MD-SystemName']."
"; + echo ""; + foreach ($PluginSccmSccm->devices as $device_values) { + echo ""; + } + echo "
" . $device_values['MD-SystemName'] . "
"; } -function nbPcToInject() { - global $PluginSccmSccm; +function nbPcToInject() +{ + global $PluginSccmSccm; - $PluginSccmSccm->getDevices(); + $PluginSccmSccm->getDevices(); - echo count($PluginSccmSccm->devices); + echo count($PluginSccmSccm->devices); } -function testViewHtml($limit, $where) { - global $PluginSccmSccm; +function testViewHtml($limit, $where) +{ + global $PluginSccmSccm; - $PluginSccmSccm->getDevices($where); + $PluginSccmSccm->getDevices($where); - foreach ($PluginSccmSccm->devices as $device_values) { - $PluginSccmSccmxml = new PluginSccmSccmxml($device_values); + foreach ($PluginSccmSccm->devices as $device_values) { + $PluginSccmSccmxml = new PluginSccmSccmxml($device_values); - $PluginSccmSccmxml->setAccessLog(); - $PluginSccmSccmxml->setAccountInfos(); - $PluginSccmSccmxml->setHardware(); - $PluginSccmSccmxml->setOS(); - $PluginSccmSccmxml->setBios(); - $PluginSccmSccmxml->setProcessors(); - $PluginSccmSccmxml->setSoftwares(); - $PluginSccmSccmxml->setUsers(); - $PluginSccmSccmxml->setNetworks(); - $PluginSccmSccmxml->setDrives(); + $PluginSccmSccmxml->setAccessLog(); + $PluginSccmSccmxml->setAccountInfos(); + $PluginSccmSccmxml->setHardware(); + $PluginSccmSccmxml->setOS(); + $PluginSccmSccmxml->setBios(); + $PluginSccmSccmxml->setProcessors(); + $PluginSccmSccmxml->setSoftwares(); + $PluginSccmSccmxml->setUsers(); + $PluginSccmSccmxml->setNetworks(); + $PluginSccmSccmxml->setDrives(); - $SXML = $PluginSccmSccmxml->sxml; + $SXML = $PluginSccmSccmxml->sxml; - Html::printCleanArray($PluginSccmSccmxml->object2array($SXML)); - } + Html::printCleanArray($PluginSccmSccmxml->object2array($SXML)); + } } -function testAdd($where) { - global $CFG_GLPI, $PluginSccmSccm, $PluginSccmConfig; - - $PluginSccmSccm->getDevices($where); +function testAdd($where) +{ + global $CFG_GLPI, $PluginSccmSccm, $PluginSccmConfig; - $REP_XML = GLPI_PLUGIN_DOC_DIR.'/sccm/xml/'; + $PluginSccmSccm->getDevices($where); - foreach ($PluginSccmSccm->devices as $device_values) { - $PluginSccmSccmxml = new PluginSccmSccmxml($device_values); + $REP_XML = GLPI_PLUGIN_DOC_DIR . '/sccm/xml/'; - $PluginSccmSccmxml->setAccessLog(); - $PluginSccmSccmxml->setAccountInfos(); - $PluginSccmSccmxml->setHardware(); - $PluginSccmSccmxml->setOS(); - $PluginSccmSccmxml->setBios(); - $PluginSccmSccmxml->setProcessors(); - $PluginSccmSccmxml->setSoftwares(); - $PluginSccmSccmxml->setUsers(); - $PluginSccmSccmxml->setNetworks(); - $PluginSccmSccmxml->setDrives(); + foreach ($PluginSccmSccm->devices as $device_values) { + $PluginSccmSccmxml = new PluginSccmSccmxml($device_values); - $SXML = $PluginSccmSccmxml->sxml; + $PluginSccmSccmxml->setAccessLog(); + $PluginSccmSccmxml->setAccountInfos(); + $PluginSccmSccmxml->setHardware(); + $PluginSccmSccmxml->setOS(); + $PluginSccmSccmxml->setBios(); + $PluginSccmSccmxml->setProcessors(); + $PluginSccmSccmxml->setSoftwares(); + $PluginSccmSccmxml->setUsers(); + $PluginSccmSccmxml->setNetworks(); + $PluginSccmSccmxml->setDrives(); - $SXML->asXML($REP_XML.$PluginSccmSccmxml->device_id.".ocs"); + $SXML = $PluginSccmSccmxml->sxml; - $url = ($PluginSccmConfig->getField('inventory_server_url') ?: $CFG_GLPI['url_base']) . '/front/inventory.php'; + $SXML->asXML($REP_XML . $PluginSccmSccmxml->device_id . ".ocs"); - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: text/xml']); - curl_setopt($ch, CURLOPT_HEADER, 0); - curl_setopt($ch, CURLOPT_POST, 1); - curl_setopt($ch, CURLOPT_POSTFIELDS, $SXML->asXML()); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); - curl_setopt($ch, CURLOPT_REFERER, $CFG_GLPI['url_base']); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - $ch_result = curl_exec($ch); - curl_close($ch); + $url = ($PluginSccmConfig->getField('inventory_server_url') ?: $CFG_GLPI['url_base']) . '/front/inventory.php'; - echo "Ajout OK"; - } + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: text/xml']); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $SXML->asXML()); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); + curl_setopt($ch, CURLOPT_REFERER, $CFG_GLPI['url_base']); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + $ch_result = curl_exec($ch); + curl_close($ch); + echo "Ajout OK"; + } } diff --git a/hook.php b/hook.php index 715a275..d8b837b 100644 --- a/hook.php +++ b/hook.php @@ -29,57 +29,60 @@ * ------------------------------------------------------------------------- */ -function plugin_sccm_install() { - global $DB; +function plugin_sccm_install() +{ + global $DB; - if (!is_dir(GLPI_PLUGIN_DOC_DIR.'/sccm')) { - mkdir(GLPI_PLUGIN_DOC_DIR.'/sccm'); - } - if (!is_dir(GLPI_PLUGIN_DOC_DIR.'/sccm/xml')) { - mkdir(GLPI_PLUGIN_DOC_DIR.'/sccm/xml'); - } + if (!is_dir(GLPI_PLUGIN_DOC_DIR . '/sccm')) { + mkdir(GLPI_PLUGIN_DOC_DIR . '/sccm'); + } + if (!is_dir(GLPI_PLUGIN_DOC_DIR . '/sccm/xml')) { + mkdir(GLPI_PLUGIN_DOC_DIR . '/sccm/xml'); + } - $migration = new Migration(100); + $migration = new Migration(100); - require 'inc/config.class.php'; - require 'inc/sccm.class.php'; - PluginSccmConfig::install($migration); - PluginSccmSccm::install($migration); + require 'inc/config.class.php'; + require 'inc/sccm.class.php'; + PluginSccmConfig::install($migration); + PluginSccmSccm::install($migration); - $migration->executeMigration(); + $migration->executeMigration(); - return true; + return true; } -function plugin_sccm_uninstall() { - global $DB; +function plugin_sccm_uninstall() +{ + global $DB; - if (is_dir(GLPI_PLUGIN_DOC_DIR.'/sccm')) { - rrmdir(GLPI_PLUGIN_DOC_DIR.'/sccm'); - } + if (is_dir(GLPI_PLUGIN_DOC_DIR . '/sccm')) { + rrmdir(GLPI_PLUGIN_DOC_DIR . '/sccm'); + } - require 'inc/config.class.php'; - require 'inc/sccm.class.php'; - return PluginSccmConfig::uninstall(); - return PluginSccmSccm::uninstall(); + require 'inc/config.class.php'; + require 'inc/sccm.class.php'; + return PluginSccmConfig::uninstall(); + return PluginSccmSccm::uninstall(); - return true; + return true; } -function rrmdir($dir) { +function rrmdir($dir) +{ - if (is_dir($dir)) { - $objects = scandir($dir); - foreach ($objects as $object) { - if ($object != "." && $object != "..") { - if (filetype($dir."/".$object) == "dir") { - rrmdir($dir."/".$object); - } else { - unlink($dir."/".$object); + if (is_dir($dir)) { + $objects = scandir($dir); + foreach ($objects as $object) { + if ($object != "." && $object != "..") { + if (filetype($dir . "/" . $object) == "dir") { + rrmdir($dir . "/" . $object); + } else { + unlink($dir . "/" . $object); + } } - } - } - reset($objects); - rmdir($dir); - } -} \ No newline at end of file + } + reset($objects); + rmdir($dir); + } +} diff --git a/inc/config.class.php b/inc/config.class.php index 7e0f69d..fcb90ef 100644 --- a/inc/config.class.php +++ b/inc/config.class.php @@ -30,69 +30,76 @@ */ if (!defined('GLPI_ROOT')) { - die("Sorry. You can't access directly to this file"); + die("Sorry. You can't access directly to this file"); } -class PluginSccmConfig extends CommonDBTM { - - static private $_instance = null; - - static function canCreate() { - return Session::haveRight('config', UPDATE); - } - - static function canUpdate() { - return Session::haveRight('config', UPDATE); - } - - static function canView() { - return Session::haveRight('config', UPDATE); - } - - static function getTypeName($nb = 0) { - return __("Setup - SCCM", "sccm"); - } - - function getName($with_comment = 0) { - return __("Interface - SCCM", "sccm"); - } - - static function getInstance() { - - if (!isset(self::$_instance)) { - self::$_instance = new self(); - if (!self::$_instance->getFromDB(1)) { - self::$_instance->getEmpty(); - } - } - return self::$_instance; - } - +class PluginSccmConfig extends CommonDBTM +{ + private static $_instance = null; + + static function canCreate() + { + return Session::haveRight('config', UPDATE); + } + + static function canUpdate() + { + return Session::haveRight('config', UPDATE); + } + + static function canView() + { + return Session::haveRight('config', UPDATE); + } + + static function getTypeName($nb = 0) + { + return __("Setup - SCCM", "sccm"); + } + + function getName($with_comment = 0) + { + return __("Interface - SCCM", "sccm"); + } + + static function getInstance() + { + + if (!isset(self::$_instance)) { + self::$_instance = new self(); + if (!self::$_instance->getFromDB(1)) { + self::$_instance->getEmpty(); + } + } + return self::$_instance; + } - function prepareInputForUpdate($input) { - if (isset($input["sccmdb_password"]) AND !empty($input["sccmdb_password"])) { - $input["sccmdb_password"] = (new GLPIKey())->encrypt($input["sccmdb_password"]); - } - if (array_key_exists('inventory_server_url', $input) && !empty($input['inventory_server_url'])) { - $input['inventory_server_url'] = trim($input['inventory_server_url'], '/ '); - } + function prepareInputForUpdate($input) + { + if (isset($input["sccmdb_password"]) and !empty($input["sccmdb_password"])) { + $input["sccmdb_password"] = (new GLPIKey())->encrypt($input["sccmdb_password"]); + } - return $input; - } + if (array_key_exists('inventory_server_url', $input) && !empty($input['inventory_server_url'])) { + $input['inventory_server_url'] = trim($input['inventory_server_url'], '/ '); + } - static function install(Migration $migration) { - global $CFG_GLPI, $DB; + return $input; + } - $default_charset = DBConnection::getDefaultCharset(); - $default_collation = DBConnection::getDefaultCollation(); - $default_key_sign = DBConnection::getDefaultPrimaryKeySignOption(); + static function install(Migration $migration) + { + global $CFG_GLPI, $DB; - $table = 'glpi_plugin_sccm_configs'; + $default_charset = DBConnection::getDefaultCharset(); + $default_collation = DBConnection::getDefaultCollation(); + $default_key_sign = DBConnection::getDefaultPrimaryKeySignOption(); - if (!$DB->tableExists($table)) { + $table = 'glpi_plugin_sccm_configs'; - $query = "CREATE TABLE `". $table."`( + if (!$DB->tableExists($table)) { + $query = "CREATE TABLE `" . $table . "`( `id` int {$default_key_sign} NOT NULL, `sccmdb_host` VARCHAR(255) NULL, `sccmdb_dbname` VARCHAR(255) NULL, @@ -112,204 +119,202 @@ static function install(Migration $migration) { PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;"; - $DB->queryOrDie($query, __("Error when using glpi_plugin_sccm_configs table.", "sccm") - . "
".$DB->error()); + $DB->queryOrDie($query, __("Error when using glpi_plugin_sccm_configs table.", "sccm") + . "
" . $DB->error()); - $query = "INSERT INTO `$table` + $query = "INSERT INTO `$table` (id, date_mod, sccmdb_host, sccmdb_dbname, sccmdb_user, sccmdb_password, inventory_server_url) VALUES (1, NOW(), 'srv_sccm','bdd_sccm','user_sccm','', NULL)"; - $DB->queryOrDie($query, __("Error when using glpi_plugin_sccm_configs table.", "sccm") + $DB->queryOrDie($query, __("Error when using glpi_plugin_sccm_configs table.", "sccm") . "
" . $DB->error()); + } else { + if (!$DB->fieldExists($table, 'verify_ssl_cert')) { + $migration->addField("glpi_plugin_sccm_configs", "verify_ssl_cert", "tinyint NOT NULL default '0'"); + $migration->migrationOneTable('glpi_plugin_sccm_configs'); + } - } else { - - if (!$DB->fieldExists($table, 'verify_ssl_cert')) { - $migration->addField("glpi_plugin_sccm_configs", "verify_ssl_cert", "tinyint NOT NULL default '0'"); - $migration->migrationOneTable('glpi_plugin_sccm_configs'); - } - - if (!$DB->fieldExists($table, 'use_auth_ntlm')) { - $migration->addField("glpi_plugin_sccm_configs", "use_auth_ntlm", "tinyint NOT NULL default '0'"); - $migration->migrationOneTable('glpi_plugin_sccm_configs'); - } - - if (!$DB->fieldExists($table, 'unrestricted_auth')) { - $migration->addField("glpi_plugin_sccm_configs", "unrestricted_auth", "tinyint NOT NULL default '0'"); - $migration->migrationOneTable('glpi_plugin_sccm_configs'); - } - - if (!$DB->fieldExists($table, 'use_auth_info')) { - $migration->addField("glpi_plugin_sccm_configs", "use_auth_info", "tinyint NOT NULL default '0'"); - $migration->migrationOneTable('glpi_plugin_sccm_configs'); - } - - if (!$DB->fieldExists($table, 'auth_info')) { - $migration->addField("glpi_plugin_sccm_configs", "auth_info", "varchar(255)"); - $migration->migrationOneTable('glpi_plugin_sccm_configs'); - } - - if (!$DB->fieldExists($table, 'is_password_sodium_encrypted')) { - $config = self::getInstance(); - if (!empty($config->fields['sccmdb_password'])) { - $key = new GLPIKey(); - $migration->addPostQuery( - $DB->buildUpdate( - 'glpi_plugin_sccm_configs', - [ - 'sccmdb_password' => $key->encrypt( - $key->decryptUsingLegacyKey( - $config->fields['sccmdb_password'] - ) + if (!$DB->fieldExists($table, 'use_auth_ntlm')) { + $migration->addField("glpi_plugin_sccm_configs", "use_auth_ntlm", "tinyint NOT NULL default '0'"); + $migration->migrationOneTable('glpi_plugin_sccm_configs'); + } + + if (!$DB->fieldExists($table, 'unrestricted_auth')) { + $migration->addField("glpi_plugin_sccm_configs", "unrestricted_auth", "tinyint NOT NULL default '0'"); + $migration->migrationOneTable('glpi_plugin_sccm_configs'); + } + + if (!$DB->fieldExists($table, 'use_auth_info')) { + $migration->addField("glpi_plugin_sccm_configs", "use_auth_info", "tinyint NOT NULL default '0'"); + $migration->migrationOneTable('glpi_plugin_sccm_configs'); + } + + if (!$DB->fieldExists($table, 'auth_info')) { + $migration->addField("glpi_plugin_sccm_configs", "auth_info", "varchar(255)"); + $migration->migrationOneTable('glpi_plugin_sccm_configs'); + } + + if (!$DB->fieldExists($table, 'is_password_sodium_encrypted')) { + $config = self::getInstance(); + if (!empty($config->fields['sccmdb_password'])) { + $key = new GLPIKey(); + $migration->addPostQuery( + $DB->buildUpdate( + 'glpi_plugin_sccm_configs', + [ + 'sccmdb_password' => $key->encrypt( + $key->decryptUsingLegacyKey( + $config->fields['sccmdb_password'] + ) + ) + ], + [ + 'id' => 1, + ] ) - ], - [ - 'id' => 1, - ] - ) - ); + ); + } + $migration->addField("glpi_plugin_sccm_configs", "is_password_sodium_encrypted", "tinyint NOT NULL default '1'"); + $migration->migrationOneTable('glpi_plugin_sccm_configs'); } - $migration->addField("glpi_plugin_sccm_configs", "is_password_sodium_encrypted", "tinyint NOT NULL default '1'"); - $migration->migrationOneTable('glpi_plugin_sccm_configs'); - } - - if (!$DB->fieldExists($table, 'use_lasthwscan')) { - $migration->addField("glpi_plugin_sccm_configs", "use_lasthwscan", "tinyint NOT NULL default '0'"); - $migration->migrationOneTable('glpi_plugin_sccm_configs'); - } - - if (!$DB->fieldExists($table, 'fusioninventory_url')) { - $migration->changeField("glpi_plugin_sccm_configs", "fusioninventory_url", "inventory_server_url", "string"); - $migration->migrationOneTable('glpi_plugin_sccm_configs'); - } - - $sccm_config = $DB->request(['FROM' => 'glpi_plugin_sccm_configs'])->current(); - $inventory_server_url = trim($sccm_config['inventory_server_url'] ?? ''); - $url_matches = []; - if ( - $inventory_server_url !== '' - && ( + + if (!$DB->fieldExists($table, 'use_lasthwscan')) { + $migration->addField("glpi_plugin_sccm_configs", "use_lasthwscan", "tinyint NOT NULL default '0'"); + $migration->migrationOneTable('glpi_plugin_sccm_configs'); + } + + if (!$DB->fieldExists($table, 'fusioninventory_url')) { + $migration->changeField("glpi_plugin_sccm_configs", "fusioninventory_url", "inventory_server_url", "string"); + $migration->migrationOneTable('glpi_plugin_sccm_configs'); + } + + $sccm_config = $DB->request(['FROM' => 'glpi_plugin_sccm_configs'])->current(); + $inventory_server_url = trim($sccm_config['inventory_server_url'] ?? ''); + $url_matches = []; + if ( + $inventory_server_url !== '' + && ( preg_match('/^(?.+)\/front\/inventory\.php$/', $inventory_server_url, $url_matches) === 1 || preg_match('/^(?.+)\/(marketplace|plugins)\/(fusioninventory)\//', $inventory_server_url, $url_matches) === 1 - ) - ){ - // Strip script path from base URL. - $inventory_server_url = $url_matches['base_url']; - if ($inventory_server_url === $CFG_GLPI['url_base']) { - $inventory_server_url = ''; + ) + ) { + // Strip script path from base URL. + $inventory_server_url = $url_matches['base_url']; + if ($inventory_server_url === $CFG_GLPI['url_base']) { + $inventory_server_url = ''; + } + $sccm_config = $DB->update( + 'glpi_plugin_sccm_configs', + [ + 'inventory_server_url' => $inventory_server_url, + ], + [ + 'id' => 1, + ] + ); } - $sccm_config = $DB->update( - 'glpi_plugin_sccm_configs', - [ - 'inventory_server_url' => $inventory_server_url, - ], - [ - 'id' => 1, - ] - ); - } - } - - return true; - } - - - static function uninstall() { - global $DB; - - if ($DB->tableExists('glpi_plugin_sccm_configs')) { - - $query = "DROP TABLE `glpi_plugin_sccm_configs`"; - $DB->queryOrDie($query, $DB->error()); - } - return true; - } - - - static function showConfigForm($item) { - global $CFG_GLPI; - - $config = self::getInstance(); - - $config->showFormHeader(); - - echo "
".__("Enable SCCM synchronization", "sccm").""; - Dropdown::showYesNo("active_sync", $config->getField('active_sync')); - echo "
".__("Server hostname (MSSQL)", "sccm").""; - echo Html::input('sccmdb_host', ['value' => $config->getField('sccmdb_host')]); - echo "
".__("Database name", "sccm").""; - echo Html::input('sccmdb_dbname', ['value' => $config->getField('sccmdb_dbname')]); - echo "
".__("Username", "sccm").""; - echo Html::input('sccmdb_user', ['value' => $config->getField('sccmdb_user')]); - echo "
".__("Password", "sccm").""; - echo ""; - echo "
".__("Inventory server base URL", "sccm").""; - echo Html::input( - 'inventory_server_url', - [ - 'type' => 'url', - 'pattern' => 'https?://.+', - 'value' => $config->getField('inventory_server_url'), - 'placeholder' => $CFG_GLPI['url_base'], - ] - ); - $url = ($config->getField('inventory_server_url') ?: $CFG_GLPI['url_base']) . '/front/inventory.php'; - echo '' . $url . ''; - echo "
".__("Verify SSL certificate", "sccm").""; - Dropdown::showYesNo("verify_ssl_cert", $config->getField('verify_ssl_cert')); - echo "
".__("Use NLTM authentication", "sccm").""; - Dropdown::showYesNo("use_auth_ntlm", $config->getField('use_auth_ntlm')); - echo "
".__("Send credentials to other hosts too", "sccm").""; - Dropdown::showYesNo("unrestricted_auth", $config->getField('unrestricted_auth')); - echo "
".__("Use specific authentication information", "sccm").""; - Dropdown::showYesNo("use_auth_info", $config->getField('use_auth_info')); - echo "
".__("Value for spécific authentication", "sccm").""; - echo Html::input('auth_info', ['value' => $config->getField('auth_info')]); - echo "
".__("Use LastHWScan as GLPI last inventory", "sccm").""; - Dropdown::showYesNo("use_lasthwscan", $config->getField('use_lasthwscan')); - echo "
" . __("Enable SCCM synchronization", "sccm") . ""; + Dropdown::showYesNo("active_sync", $config->getField('active_sync')); + echo "
" . __("Server hostname (MSSQL)", "sccm") . ""; + echo Html::input('sccmdb_host', ['value' => $config->getField('sccmdb_host')]); + echo "
" . __("Database name", "sccm") . ""; + echo Html::input('sccmdb_dbname', ['value' => $config->getField('sccmdb_dbname')]); + echo "
" . __("Username", "sccm") . ""; + echo Html::input('sccmdb_user', ['value' => $config->getField('sccmdb_user')]); + echo "
" . __("Password", "sccm") . ""; + echo ""; + echo "
" . __("Inventory server base URL", "sccm") . ""; + echo Html::input( + 'inventory_server_url', + [ + 'type' => 'url', + 'pattern' => 'https?://.+', + 'value' => $config->getField('inventory_server_url'), + 'placeholder' => $CFG_GLPI['url_base'], + ] + ); + $url = ($config->getField('inventory_server_url') ?: $CFG_GLPI['url_base']) . '/front/inventory.php'; + echo '' . $url . ''; + echo "
" . __("Verify SSL certificate", "sccm") . ""; + Dropdown::showYesNo("verify_ssl_cert", $config->getField('verify_ssl_cert')); + echo "
" . __("Use NLTM authentication", "sccm") . ""; + Dropdown::showYesNo("use_auth_ntlm", $config->getField('use_auth_ntlm')); + echo "
" . __("Send credentials to other hosts too", "sccm") . ""; + Dropdown::showYesNo("unrestricted_auth", $config->getField('unrestricted_auth')); + echo "
" . __("Use specific authentication information", "sccm") . ""; + Dropdown::showYesNo("use_auth_info", $config->getField('use_auth_info')); + echo "
" . __("Value for spécific authentication", "sccm") . ""; + echo Html::input('auth_info', ['value' => $config->getField('auth_info')]); + echo "
" . __("Use LastHWScan as GLPI last inventory", "sccm") . ""; + Dropdown::showYesNo("use_lasthwscan", $config->getField('use_lasthwscan')); + echo "