Skip to content

Commit fdfbf73

Browse files
authored
Merge commit from fork
Add config option to allow the plugin to kill processes
2 parents e885d25 + 802df60 commit fdfbf73

7 files changed

Lines changed: 399 additions & 42 deletions

File tree

ajax/process.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,26 @@
3030
include ("../../../inc/includes.php");
3131

3232
Session::checkLoginUser();
33-
//Html::header_nocache();
34-
35-
$process = new PluginPrintercountersProcess();
36-
37-
switch ($_POST['action']) {
38-
case 'killProcess':
39-
header('Content-Type: application/json; charset=UTF-8"');
40-
list($messages, $error) = $process->killProcesses($_POST['items_id'], PluginPrintercountersProcess::SIGKILL);
41-
echo json_encode(['message' => $messages,
42-
'error' => $error]);
43-
break;
44-
45-
case 'getProcesses':
46-
header("Content-Type: text/html; charset=UTF-8");
47-
$process->getProcesses();
48-
break;
33+
if (Session::haveRight(PluginPrintercountersProcess::$rightname, UPDATE)) {
34+
//Html::header_nocache();
35+
36+
$process = new PluginPrintercountersProcess();
37+
38+
switch ($_POST['action']) {
39+
case 'killProcess':
40+
$config = PluginPrintercountersConfig::getInstance();
41+
if ($config['can_kill_processes']) {
42+
header('Content-Type: application/json; charset=UTF-8"');
43+
list($messages, $error) = $process->killProcesses($_POST['items_id'], PluginPrintercountersProcess::SIGKILL);
44+
echo json_encode(['message' => $messages,
45+
'error' => $error]);
46+
break;
47+
}
48+
case 'getProcesses':
49+
header("Content-Type: text/html; charset=UTF-8");
50+
$process->getProcesses();
51+
break;
52+
}
4953
}
5054

55+

hook.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function plugin_printercounters_install() {
3434

3535
// SQL creation
3636
if (!$DB->tableExists("glpi_plugin_printercounters_records")) {
37-
$DB->runFile(PLUGIN_PRINTERCOUNTERS_DIR. "/install/sql/empty-2.0.0.sql");
37+
$DB->runFile(PLUGIN_PRINTERCOUNTERS_DIR. "/install/sql/empty-2.0.2.sql");
3838

3939
// Add record notification
4040
include_once(PLUGIN_PRINTERCOUNTERS_DIR. "/inc/notificationtargetadditional_data.class.php");
@@ -88,6 +88,10 @@ function plugin_printercounters_install() {
8888
update106to107();
8989
}
9090

91+
if (!$DB->fieldExists("glpi_plugin_printercounters_configs", 'can_kill_processes')) {
92+
$DB->runFile(PLUGIN_PRINTERCOUNTERS_DIR. "/install/sql/update-2.0.2.sql");
93+
}
94+
9195
CronTask::Register('PluginPrintercountersItem_Ticket', 'PrintercountersCreateTicket', DAY_TIMESTAMP);
9296
CronTask::Register('PluginPrintercountersErrorItem', 'PluginPrintercountersErrorItem', DAY_TIMESTAMP);
9397

inc/config.class.php

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class PluginPrintercountersConfig extends CommonDBTM {
4747
const TICKETS = 2;
4848
const RECORDS = 3;
4949
const ERRORITEMS = 4;
50+
const PROCESS = 5;
5051

5152
static $rightname = 'plugin_printercounters';
5253

@@ -83,6 +84,7 @@ function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
8384
$tabs[self::TICKETS] = PluginPrintercountersItem_Ticket::getTypeName();
8485
$tabs[self::RECORDS] = PluginPrintercountersRecord::getTypeName(2);
8586
$tabs[self::ERRORITEMS] = PluginPrintercountersErrorItem::getTypeName(2);
87+
$tabs[self::PROCESS] = PluginPrintercountersProcess::getTypeName();
8688

8789
return $tabs;
8890
}
@@ -127,18 +129,19 @@ static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtem
127129
$erroritem = new PluginPrintercountersErrorItem();
128130
$erroritem->showErrorItemConfig($config);
129131
break;
132+
133+
case self::PROCESS :
134+
PluginPrintercountersProcess::showConfigForm($config);
135+
break;
130136
}
131137
}
132138
return true;
133139
}
134140

135-
136-
137141
/**
138142
* Get config instance in database
139143
*
140-
* @param type $options
141-
* @return type
144+
* @return array
142145
*/
143146
public static function getInstance() {
144147
if (!isset(self::$instance)) {
@@ -147,23 +150,25 @@ public static function getInstance() {
147150
$data = $temp->getConfigFromDB();
148151
$input = [];
149152
if ($data) {
150-
$input = ['configs_id' => $data['id'],
151-
'nb_errors_ticket' => $data['nb_errors_ticket'],
152-
'nb_errors_delay_ticket' => $data['nb_errors_delay_ticket'],
153-
'no_record_delay_ticket' => $data['no_record_delay_ticket'],
154-
'items_status' => json_decode($data['items_status'], true),
155-
'tickets_category' => $data["tickets_category"],
156-
'tickets_content' => $data["tickets_content"],
157-
'add_item_user' => $data["add_item_user"],
158-
'add_item_group' => $data["add_item_group"],
159-
'disable_autosearch' => $data['disable_autosearch'],
160-
'set_first_record' => $data['set_first_record'],
161-
'enable_toner_alert' => $data['enable_toner_alert'],
162-
'toner_alert_repeat' => $data['toner_alert_repeat'],
163-
'toner_treshold' => $data['toner_treshold'],
164-
'max_error_counter' => $data['max_error_counter'],
165-
'enable_error_handler' => $data['enable_error_handler']
166-
];
153+
$input = [
154+
'configs_id' => $data['id'],
155+
'nb_errors_ticket' => $data['nb_errors_ticket'],
156+
'nb_errors_delay_ticket' => $data['nb_errors_delay_ticket'],
157+
'no_record_delay_ticket' => $data['no_record_delay_ticket'],
158+
'items_status' => json_decode($data['items_status'], true),
159+
'tickets_category' => $data["tickets_category"],
160+
'tickets_content' => $data["tickets_content"],
161+
'add_item_user' => $data["add_item_user"],
162+
'add_item_group' => $data["add_item_group"],
163+
'disable_autosearch' => $data['disable_autosearch'],
164+
'set_first_record' => $data['set_first_record'],
165+
'enable_toner_alert' => $data['enable_toner_alert'],
166+
'toner_alert_repeat' => $data['toner_alert_repeat'],
167+
'toner_treshold' => $data['toner_treshold'],
168+
'max_error_counter' => $data['max_error_counter'],
169+
'enable_error_handler' => $data['enable_error_handler'],
170+
'can_kill_processes' => $data['can_kill_processes']
171+
];
167172
}
168173

169174
self::$instance = $input;

inc/process.class.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ public function getProcesses() {
370370
}
371371
}
372372
}
373+
$config = PluginPrintercountersConfig::getInstance();
373374

374375
// Display all processes data
375376
if (!empty($rows)) {
@@ -401,13 +402,23 @@ public function getProcesses() {
401402
}
402403
}
403404
echo "</td>";
404-
echo "<td><a onclick='printercountersActions(\"".PLUGIN_PRINTERCOUNTERS_WEBDIR."/ajax/process.php\", \"".PLUGIN_PRINTERCOUNTERS_WEBDIR."\", \"killProcess\", \"\", \"process_action_result\", $pid, \"".$this->getType()."\");' class='submit btn btn-primary printercounters_action_button'>".__('Kill process', 'printercounters')."</a></td>";
405+
echo "<td>";
406+
if ($config['can_kill_processes']) {
407+
echo "<a onclick='printercountersActions(\"".PLUGIN_PRINTERCOUNTERS_WEBDIR."/ajax/process.php\", \"".PLUGIN_PRINTERCOUNTERS_WEBDIR."\", \"killProcess\", \"\", \"process_action_result\", $pid, \"".$this->getType()."\");' class='submit btn btn-primary printercounters_action_button'>".__('Kill process', 'printercounters')."</a>";
408+
}
409+
echo "</td>";
405410
echo "</tr>";
406411
}
407412

408413
echo "<tr>";
409414
echo "<td class='tab_bg_2 center' colspan='".($nb_col+2)."'>";
410-
echo "<a onclick='printercountersActions(\"".PLUGIN_PRINTERCOUNTERS_WEBDIR."/ajax/process.php\", \"".PLUGIN_PRINTERCOUNTERS_WEBDIR."\", \"killProcess\", \"\", \"process_action_result\", $pids[0], \"".$this->getType()."\");' class='submit btn btn-primary printercounters_action_button'>".__('Kill all processes', 'printercounters')."</a>";
415+
if ($config['can_kill_processes']) {
416+
echo "<a onclick='printercountersActions(\"" . PLUGIN_PRINTERCOUNTERS_WEBDIR . "/ajax/process.php\", \"" . PLUGIN_PRINTERCOUNTERS_WEBDIR . "\", \"killProcess\", \"\", \"process_action_result\", $pids[0], \"" . $this->getType(
417+
) . "\");' class='submit btn btn-primary printercounters_action_button'>" . __(
418+
'Kill all processes',
419+
'printercounters'
420+
) . "</a>";
421+
}
411422
echo "</td>";
412423
echo "</tr>";
413424
echo "</table>";
@@ -456,7 +467,7 @@ public function killProcesses($pid, $signal) {
456467
$error = false;
457468
$message = '';
458469

459-
if (DIRECTORY_SEPARATOR=='/') {
470+
if (DIRECTORY_SEPARATOR=='/' && is_numeric($pid)) {
460471
// Unix/Linux
461472
exec("ps -ef| awk '\$3 == '$pid' { print \$2 }'", $output, $ret);
462473
if ($ret) {
@@ -530,4 +541,32 @@ function getProcessItems() {
530541
return $output;
531542
}
532543

544+
public static function showConfigForm(array $config) {
545+
echo "<form name='form' method='post' action='".
546+
Toolbox::getItemTypeFormURL('PluginPrintercountersConfig')."'>";
547+
echo "<div align='center'>";
548+
echo "<table class='tab_cadre_fixe'>";
549+
echo "<tr><th colspan='2'>".self::getTypeName()."</th></tr>";
550+
echo "<tr class='tab_bg_1'>";
551+
echo "<td>".__('Allow killing processes', 'printercounters')."</td>";
552+
echo "<td>";
553+
Dropdown::showYesNo('can_kill_processes', $config['can_kill_processes']);
554+
echo "</td>";
555+
echo "</tr>";
556+
echo "<tr>";
557+
echo" <td class='tab_bg_2' colspan='2'>";
558+
echo "<span class='rounded border border-danger text-danger p-2'>";
559+
echo '<i class="fa fa-exclamation-triangle" aria-hidden="true"></i> ';
560+
echo __('Allowing killing processes will allow the plugin to execute scripts on the server', 'printercounter');
561+
echo "</span>";
562+
echo "</td>";
563+
echo "</tr>";
564+
echo "<tr>";
565+
echo" <td class='tab_bg_2 center' colspan='2'>";
566+
echo Html::submit(_sx('button', 'Update'), ['name' => 'update_config', 'class' => 'btn btn-primary']);
567+
echo "</td>";
568+
echo "</tr>";
569+
echo "</table></div>";
570+
Html::closeForm();
571+
}
533572
}

0 commit comments

Comments
 (0)