/**
  * Launch a command
  * 
  * @param array $values
  * 
  * @return void
  */
 static function lauchCommand($values)
 {
     global $CFG_GLPI;
     $items_to_execute = json_decode(stripslashes($values['items_to_execute']), true);
     if (!empty($items_to_execute)) {
         foreach ($items_to_execute as $key => $items) {
             PluginShellcommandsCommandGroup_Item::lauchCommand(array('itemID' => $items['items_id'], 'itemtype' => $items['itemtype'], 'id' => $values['command_group'], 'value' => null));
         }
     }
 }
 static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0)
 {
     if ($item->getType() == 'PluginShellcommandsShellcommand') {
         self::showForShellcommands($item);
     } else {
         if (in_array($item->getType(), PluginShellcommandsShellcommand::getTypes(true))) {
             self::showForItem($item);
             PluginShellcommandsCommandGroup_Item::showForItem($item);
         }
     }
     return true;
 }
 /**
  * Custom fonction to process shellcommand massive action
  **/
 function doMassiveAction(MassiveAction $ma, array $ids)
 {
     if (!empty($ids)) {
         $input = $ma->getInput();
         $itemtype = $ma->getItemType(false);
         $commands_id = $input['commandgroup'];
         switch ($ma->getAction()) {
             case 'generate':
                 echo "<div class='center'>";
                 echo "<table class='tab_cadre_fixe center'>";
                 echo "<tr class='tab_bg_1'>";
                 echo "<th colspan='4'>" . PluginShellcommandsCommandGroup::getTypeName(2) . "</th>";
                 echo "</tr>";
                 foreach ($ids as $key => $items_id) {
                     PluginShellcommandsCommandGroup_Item::lauchCommand(array('itemID' => $items_id, 'itemtype' => $itemtype, 'id' => $commands_id, 'value' => null));
                 }
                 echo "</table>";
                 echo "</div>";
                 break;
         }
     }
 }
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 Shellcommands 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 shellcommands. If not, see <http://www.gnu.org/licenses/>.
 --------------------------------------------------------------------------
*/
define('GLPI_ROOT', '../../..');
include GLPI_ROOT . "/inc/includes.php";
Session::checkLoginUser();
$command = new PluginShellcommandsShellcommand();
$command_item = new PluginShellcommandsShellcommand_Item();
$command->checkGlobal(READ);
header("Content-Type: text/html; charset=UTF-8");
switch ($_POST['command_type']) {
    case 'PluginShellcommandsShellcommand':
        PluginShellcommandsShellcommand_Item::lauchCommand($_POST);
        break;
    case 'PluginShellcommandsCommandGroup':
        PluginShellcommandsCommandGroup_Item::lauchCommand($_POST);
        break;
    case 'PluginShellcommandsAdvanced_Execution':
        PluginShellcommandsAdvanced_Execution::lauchCommand($_POST);
        break;
}
 Shellcommands 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 2 of the License, or
 (at your option) any later version.

 Shellcommands 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 Shellcommands. If not, see <http://www.gnu.org/licenses/>.
 --------------------------------------------------------------------------  */
include '../../../inc/includes.php';
$group_item = new PluginShellcommandsCommandGroup_Item();
if (isset($_POST["add"])) {
    $group_item->check(-1, UPDATE, $_POST);
    $group_item->add($_POST);
    Html::back();
} else {
    if (isset($_POST["up"]) || isset($_POST["up_x"])) {
        $group_item->orderItem($_POST, 'up');
        Html::back();
    } else {
        if (isset($_POST["down"]) || isset($_POST["down_x"])) {
            $group_item->orderItem($_POST, 'down');
            Html::back();
        }
    }
}
 /**
  * Launch a command
  * 
  * @param array $values
  * 
  * @return void
  */
 static function lauchCommand($values)
 {
     global $CFG_GLPI;
     if (!empty($values['itemtype']) && !empty($values['itemID'])) {
         $item = getItemForItemtype($values['itemtype']);
         $item->getFromDB($values['itemID']);
         $shellcommands_item = new PluginShellcommandsShellcommand_Item();
         $shellcommands = new PluginShellcommandsShellcommand();
         $commandgroups = new PluginShellcommandsCommandGroup();
         $commandgroups_item = new PluginShellcommandsCommandGroup_Item();
         $commandgroups_items = $commandgroups_item->find("`plugin_shellcommands_commandgroups_id`=" . $values['id'], "`rank`");
         echo "<div class='center'>";
         echo "<table class='tab_cadre_fixe shellcommands_result_line'>";
         echo "<tr class='tab_bg_1'>";
         echo "<th colspan='4'>" . __('Result details') . "</th>";
         echo "</tr>";
         // First : test ping
         $error = 1;
         $commandgroups->getFromDB($values['id']);
         if (!empty($commandgroups->fields['check_commands_id'])) {
             $targetParam = PluginShellcommandsShellcommand_Item::resolveLinkOfCommand($commandgroups->fields['check_commands_id'], $item);
             // Exec command on each targets : stop on first success
             if ($targetParam !== false) {
                 foreach ($targetParam as $target) {
                     list($error, $execPing) = PluginShellcommandsShellcommand_Item::execCommand($commandgroups->fields['check_commands_id'], $target);
                     if (!$error) {
                         break;
                     }
                 }
             }
         }
         // If Ping ok execute group commands
         if (!$error) {
             echo "<tr class='tab_bg_1 shellcommands_result_line'>";
             echo "<td class='center' colspan='2'>" . __($item->getType()) . ' : ' . $item->getLink() . "</td>";
             echo "<td class='center'><div class='shellcommands_result_ok'>UP</div></td>";
             echo "<td>";
             echo __('Host UP', 'shellcommands');
             echo "</td>";
             echo "</tr>";
             if (!empty($commandgroups_items)) {
                 foreach ($commandgroups_items as $val) {
                     if (!$shellcommands_item->getFromDBbyShellCommandsAndItem($val['plugin_shellcommands_shellcommands_id'], $values['itemtype'])) {
                         continue;
                     }
                     $shellcommands->getFromDB($val['plugin_shellcommands_shellcommands_id']);
                     $targetParam = PluginShellcommandsShellcommand_Item::resolveLinkOfCommand($val['plugin_shellcommands_shellcommands_id'], $item);
                     // Exec command on each targets : stop on first success
                     if ($targetParam !== false) {
                         foreach ($targetParam as $target) {
                             list($error, $message) = PluginShellcommandsShellcommand_Item::execCommand($shellcommands->getID(), $target);
                             if (!$error) {
                                 break;
                             }
                         }
                     }
                     PluginShellcommandsShellcommand::displayCommandResult($shellcommands, $target, $message, $error);
                 }
             }
         } else {
             echo "<tr class='tab_bg_1 shellcommands_result_line'>";
             echo "<td class='center' colspan='2'>" . __($item->getType()) . ' : ' . $item->getLink() . "</td>";
             echo "<td class='center'><div class='shellcommands_result_ko'>DOWN</div></td>";
             echo "<td>";
             echo __('Host DOWN', 'shellcommands');
             echo "</td>";
             echo "</tr>";
         }
         echo "</table>";
         echo "</div>";
     }
 }