-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup.php
More file actions
286 lines (242 loc) · 9.63 KB
/
setup.php
File metadata and controls
286 lines (242 loc) · 9.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/**
* -------------------------------------------------------------------------
* Carbon plugin for GLPI
*
* @copyright Copyright (C) 2024-2025 Teclib' and contributors.
* @license https://www.gnu.org/licenses/gpl-3.0.txt GPLv3+
* @link https://github.com/pluginsGLPI/carbon
*
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Carbon plugin for GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* -------------------------------------------------------------------------
*/
use Config as GlpiConfig;
use CronTask as GlpiCronTask;
use GlpiPlugin\Carbon\Dashboard\Widget;
use Glpi\Plugin\Hooks;
use GlpiPlugin\Carbon\Config;
use GlpiPlugin\Carbon\CronTask;
use GlpiPlugin\Carbon\UsageInfo;
use GlpiPlugin\Carbon\Profile;
use GlpiPlugin\Carbon\Report;
use Location as GlpiLocation;
use Profile as GlpiProfile;
use GlpiPlugin\Carbon\Dashboard\Grid;
use GlpiPlugin\Carbon\DataSource\CarbonIntensity\ClientFactory as CarbonIntensityClientFactory;
use GlpiPlugin\Carbon\DataSource\Lca\ClientFactory as LcaClientFactory;
use GlpiPlugin\Carbon\Location;
define('PLUGIN_CARBON_VERSION', '1.2.0-dev');
define('PLUGIN_CARBON_SCHEMA_VERSION', '1.2.0');
// Minimal GLPI version, inclusive
define("PLUGIN_CARBON_MIN_GLPI_VERSION", "11.0.0-beta");
// Maximum GLPI version, exclusive
define("PLUGIN_CARBON_MAX_GLPI_VERSION", "12.0.0");
define('PLUGIN_CARBON_DECIMALS', 3);
// Plugin compatible itemtypes
/**
* @var array<class-string<CommonDBTM>> $PLUGIN_CARBON_TYPES
*/
define('PLUGIN_CARBON_TYPES', [
Computer::class,
Monitor::class,
NetworkEquipment::class,
// Phone::class,
// Printer::class,
]);
/**
* Init hooks of the plugin.
* REQUIRED
*
* @return void
*/
function plugin_init_carbon()
{
/** @var array $CFG_GLPI */
global $CFG_GLPI;
if (!Plugin::isPluginActive('carbon')) {
return;
}
require_once(__DIR__ . '/vendor/autoload.php');
plugin_carbon_setupHooks();
plugin_carbon_registerClasses();
$CFG_GLPI['javascript']['tools'][strtolower(Report::class)] = ['dashboard'];
}
function plugin_carbon_setupHooks()
{
/** @var array $PLUGIN_HOOKS */
global $PLUGIN_HOOKS;
// Secured config
$PLUGIN_HOOKS[Hooks::SECURED_CONFIGS]['carbon'] = array_merge(
CarbonIntensityClientFactory::getSecuredConfigs(),
LcaClientFactory::getSecuredConfigs()
);
// add new cards to the dashboard
$PLUGIN_HOOKS[Hooks::DASHBOARD_CARDS]['carbon'] = [Grid::class, 'getDashboardCards'];
$PLUGIN_HOOKS[Hooks::DASHBOARD_TYPES]['carbon'] = [Widget::class, 'WidgetTypes'];
if (Session::haveRight('config', UPDATE)) {
$PLUGIN_HOOKS['config_page']['carbon'] = 'front/config.form.php';
}
$PLUGIN_HOOKS['menu_toadd']['carbon']['tools'] = [Report::class];
// $PLUGIN_HOOKS['menu_toadd']['carbon']['admin'] = [CarbonIntensity::class];
$PLUGIN_HOOKS[Hooks::POST_SHOW_TAB]['carbon'] = 'plugin_carbon_postShowTab';
foreach (PLUGIN_CARBON_TYPES as $itemtype) {
// Asset itself
$PLUGIN_HOOKS[Hooks::ITEM_ADD]['carbon'][$itemtype] = 'plugin_carbon_hook_add_asset';
$PLUGIN_HOOKS[Hooks::ITEM_UPDATE]['carbon'][$itemtype] = 'plugin_carbon_hook_update_asset';
$PLUGIN_HOOKS[Hooks::PRE_ITEM_PURGE]['carbon'][$itemtype] = 'plugin_carbon_hook_pre_purge_asset';
// asset's type
$PLUGIN_HOOKS[Hooks::PRE_ITEM_PURGE]['carbon'][$itemtype . 'Type'] = 'plugin_carbon_hook_pre_purge_assettype';
}
// $PLUGIN_HOOKS[Hooks::POST_ITEM_FORM]['carbon'] = 'plugin_carbon_postItemForm';
// Actions taken on locations events
$PLUGIN_HOOKS[Hooks::ITEM_ADD]['carbon'][GlpiLocation::class] = 'plugin_carbon_locationAdd';
$PLUGIN_HOOKS[Hooks::PRE_ITEM_UPDATE]['carbon'][GlpiLocation::class] = 'plugin_carbon_locationPreUpdate';
$PLUGIN_HOOKS[Hooks::ITEM_UPDATE]['carbon'][GlpiLocation::class] = 'plugin_carbon_locationUpdate';
$PLUGIN_HOOKS[Hooks::PRE_ITEM_PURGE]['carbon'][GlpiLocation::class] = 'plugin_carbon_locationPrePurge';
// Updating profile rights impacts data for itemtype ProfileRight, then we must use PRE_ITEM_* hooks
$PLUGIN_HOOKS[Hooks::PRE_ITEM_UPDATE]['carbon'][GlpiProfile::class] = 'plugin_carbon_profileUpdate';
$PLUGIN_HOOKS[Hooks::PRE_ITEM_ADD]['carbon'][GlpiProfile::class] = 'plugin_carbon_profileAdd';
$PLUGIN_HOOKS[Hooks::ADD_JAVASCRIPT]['carbon'][] = 'lib/apexcharts.js';
$PLUGIN_HOOKS[Hooks::ADD_JAVASCRIPT]['carbon'][] = 'lib/carbon.js';
// Import CSS
$PLUGIN_HOOKS[Hooks::ADD_CSS]['carbon'][] = 'lib/carbon.css';
$PLUGIN_HOOKS['add_default_where']['carbon'] = 'plugin_carbon_add_default_where';
$PLUGIN_HOOKS['use_massive_action']['carbon'] = 1;
}
function plugin_carbon_registerClasses()
{
Plugin::registerClass(Config::class, ['addtabon' => GlpiConfig::class]);
Plugin::registerClass(Profile::class, ['addtabon' => GlpiProfile::class]);
Plugin::registerClass(Location::class, ['addtabon' => GlpiLocation::class]);
Plugin::registerClass(CronTask::class, ['addtabon' => GlpiCronTask::class]);
foreach (PLUGIN_CARBON_TYPES as $itemtype) {
$core_type_class = $itemtype . 'Type';
$item_type_class = 'GlpiPlugin\\Carbon\\' . $core_type_class;
Plugin::registerClass($item_type_class, ['addtabon' => $core_type_class]);
Plugin::registerClass(UsageInfo::class, ['addtabon' => $itemtype]);
$core_model_class = $itemtype . 'Model';
$item_model_class = 'GlpiPlugin\\Carbon\\' . $core_model_class;
Plugin::registerClass($item_model_class, ['addtabon' => $core_model_class]);
}
}
/**
* Get the name and the version of the plugin
* REQUIRED
*
* @return array
*/
function plugin_version_carbon()
{
$requirements = [
'name' => 'Carbon',
'version' => PLUGIN_CARBON_VERSION,
'author' => '<a href="http://www.teclib.com">Teclib\'</a>',
'license' => 'GPLv3',
'homepage' => '',
'requirements' => [
'glpi' => [
'min' => PLUGIN_CARBON_MIN_GLPI_VERSION,
]
]
];
$dev_version = strpos(PLUGIN_CARBON_VERSION, '-dev') !== false;
if (!$dev_version) {
// This is not a development version
$requirements['requirements']['glpi']['max'] = PLUGIN_CARBON_MAX_GLPI_VERSION;
}
return $requirements;
}
/**
* Check plugin's prerequisites before installation
*
* @return boolean
*/
function plugin_carbon_check_prerequisites()
{
/** @var DBmysql $DB */
global $DB;
$prerequisitesSuccess = true;
/** @phpstan-ignore if.alwaysFalse */
if (version_compare(GLPI_VERSION, PLUGIN_CARBON_MIN_GLPI_VERSION, 'lt')) {
echo "This plugin requires GLPI >= " . PLUGIN_CARBON_MIN_GLPI_VERSION . " and GLPI < " . PLUGIN_CARBON_MAX_GLPI_VERSION . "<br>";
$prerequisitesSuccess = false;
}
if (!is_readable(__DIR__ . '/vendor/autoload.php') || !is_file(__DIR__ . '/vendor/autoload.php')) {
echo "Run composer install --no-dev in the plugin directory<br>";
$prerequisitesSuccess = false;
}
if ($DB->use_timezones !== true) {
echo "Enable timezones support<br>";
$prerequisitesSuccess = false;
}
if (getenv('CI') === false) {
// only when not under test
$version_string = $DB->getVersion();
$server = preg_match('/-MariaDB/', $version_string) ? 'MariaDB' : 'MySQL';
$version = preg_replace('/^((\d+\.?)+).*$/', '$1', $version_string);
if ($server === 'MySQL' && version_compare($version, '8.0.0', '<')) {
echo 'This plugin requires MySQL >= 8.0 or MariaDB >= 10.2<br>';
$prerequisitesSuccess = false;
}
if ($server === 'MariaDB' && version_compare($version, '10.2.0', '<')) {
echo 'This plugin requires MySQL >= 8.0 or MariaDB >= 10.2<br>';
$prerequisitesSuccess = false;
}
}
return $prerequisitesSuccess;
}
/**
* Get the path to the empty SQL schema file
* @param string $version The version of the schema file to get
*
* @return string|null
*/
function plugin_carbon_getSchemaPath(?string $version = null): ?string
{
$version = $version ?? PLUGIN_CARBON_VERSION;
// Drop suffixes for alpha, beta, rc versions
$matches = [];
preg_match('/^(\d+\.\d+\.\d+)/', $version, $matches);
$version = $matches[1];
$matches = [];
preg_match('/^(\d+\.\d+\.\d+)/', PLUGIN_CARBON_VERSION, $matches);
$current_version = $matches[1];
if ($version === $current_version) {
$schemaPath = Plugin::getPhpDir('carbon') . '/install/mysql/plugin_carbon_empty.sql';
} else {
$schemaPath = Plugin::getPhpDir('carbon') . "/install/mysql/plugin_carbon_{$version}_empty.sql";
}
// Plugin::getPhpDir may return false
/** @phpstan-ignore identical.alwaysFalse */
if ($schemaPath === false) {
return null;
}
return $schemaPath;
}
/**
* Get friendly name of the plugin, may be used in various places
*
* @return string
*/
function plugin_carbon_getFriendlyName(): string
{
return __('Environmental Impact', 'carbon');
}