Beispiel #1
0
 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';
Html::header(PluginShellcommandsShellcommand::getTypeName(2), '', "plugins", "shellcommands");
$plugin_shellcommands_shellcommands_id = $_GET['plugin_shellcommands_shellcommands_id'];
$itemtype = $_SESSION["plugin_shellcommands"]["itemtype"];
unset($_SESSION["plugin_shellcommands"]["itemtype"]);
$tab_id = unserialize($_SESSION["plugin_shellcommands"]["tab_id"]);
unset($_SESSION["plugin_shellcommands"]["tab_id"]);
$command = new PluginShellcommandsShellcommand();
$command_item = new PluginShellcommandsShellcommand_Item();
$command->getFromDBbyName($plugin_shellcommands_shellcommands_id);
$path = Dropdown::getDropdownName("glpi_plugin_shellcommands_shellcommandpaths", $command->fields["plugin_shellcommands_shellcommandpaths_id"]);
$parameters = $command->fields["parameters"];
$link = $command->fields["link"];
echo "<div align='center'>";
echo "<table class='tab_cadre_fixe' cellpadding='5'>";
echo "<tr><th>" . __('Command', 'shellcommands');
echo "</th></tr>";
foreach ($tab_id as $key => $ID) {
    $item = new $itemtype();
    $item->getFromDB($ID);
    if ($plugin_shellcommands_shellcommands_id == "Wake on Lan" || $plugin_shellcommands_shellcommands_id == "Ping") {
        $ipmac = array();
        $i = 0;
        $query = "SELECT `glpi_networkports`.*, `glpi_ipaddresses`.`name` as ip, `glpi_ipnetworks`.`netmask`\n               FROM `glpi_networkports`\n               LEFT JOIN `glpi_networknames`\n                  ON (`glpi_networknames`.`items_id`=`glpi_networkports`.`id`)                            \n               LEFT JOIN `glpi_ipaddresses`\n                  ON (`glpi_networknames`.`id`=`glpi_ipaddresses`.`items_id`)\n               LEFT JOIN `glpi_ipaddresses_ipnetworks`\n                  ON (`glpi_ipaddresses`.`id`=`glpi_ipaddresses_ipnetworks`.`ipaddresses_id`) \n               LEFT JOIN `glpi_ipnetworks`\n                  ON (`glpi_ipaddresses_ipnetworks`.`ipnetworks_id`=`glpi_ipnetworks`.`id`)\n               WHERE `glpi_networkports`.`items_id` = '{$ID}' \n               AND `glpi_networkports`.`itemtype` = '" . $item->getType() . "' \n               ORDER BY `glpi_networkports`.`logical_number`";
        $result = $DB->query($query);
 static function lauchCommand($values)
 {
     $host = $values['value'];
     $id = $values['plugin_shellcommands_shellcommands_id'];
     $command = new PluginShellcommandsShellcommand();
     $command_item = new PluginShellcommandsShellcommand_Item();
     $command->getFromDBbyName($id);
     $path = Dropdown::getDropdownName("glpi_plugin_shellcommands_shellcommandpaths", $command->fields["plugin_shellcommands_shellcommandpaths_id"]);
     $parameters = $command->fields["parameters"];
     echo "<div align='center'>";
     echo "<table class='tab_cadrehov' cellpadding='5'>";
     echo "<tr><th>" . __('Command', 'shellcommands');
     echo "</th></tr>";
     echo "<tr class='tab_bg_2'><td>";
     echo "<p><b>{$id} -> " . $host . "</b><br>";
     if ($id == "Wake on Lan") {
         $command = $command_item->sendMagicPacket($host);
     } else {
         $command = $path . " " . $parameters . " " . $host;
     }
     $ouput[] = null;
     if ($id != "Wake on Lan") {
         exec($command, $ouput);
         $cmd = count($ouput);
         echo "<font color=blue>";
         for ($i = 0; $i < $cmd; $i++) {
             echo Toolbox::encodeInUtf8($ouput[$i]) . "<br>";
         }
         echo "</font>";
         echo "</p>" . $command;
     }
     echo "</td></tr></table></div>";
 }
 /**
  * Launch a command
  * 
  * @param array $values
  * 
  * @return void
  */
 static function lauchCommand($values)
 {
     global $CFG_GLPI;
     $targetParam = $values['value'];
     $commandName = Dropdown::getDropdownName("glpi_plugin_shellcommands_shellcommands", $values['id']);
     $shellcommands = new PluginShellcommandsShellcommand();
     $shellcommands->getFromDBbyName($commandName);
     list($error, $message) = self::execCommand($shellcommands->fields['id'], $targetParam);
     echo "<div class='center'>";
     echo "<table class='tab_cadre_fixe center'>";
     echo "<tr class='tab_bg_1'>";
     echo "<th colspan='4'>" . __('Result details') . "</th>";
     echo "</tr>";
     PluginShellcommandsShellcommand::displayCommandResult($shellcommands, $targetParam, $message, $error);
     echo "</table>";
     echo "</div>";
 }
 /**
  * Method to run shellcommands by webservice
  * 
  * @param array $params       Options
  * @param string $protocol    Communication protocol used
  * 
  * @return array or error value
  */
 static function methodRun($params, $protocol)
 {
     global $DB;
     if (isset($params['help'])) {
         return array('command_id' => 'integer,optional,multiple', 'command_name' => 'string,optional,multiple', 'target_type' => 'string,mandatory', 'target_id' => 'integer,optional,multiple', 'target_name' => 'string,optional', 'help' => 'bool,optional');
     }
     if (!Session::getLoginUserID()) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTAUTHENTICATED);
     }
     $executionOutputs = array();
     $invalidCommands = array();
     $invalidTargets = array();
     /** Parameters check **/
     if ((!isset($params['command_id']) || empty($params['command_id'])) && (!isset($params['command_name']) || empty($params['command_name']))) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_MISSINGPARAMETER, '', 'one among "command_id" and "command_name"');
     }
     //Post-relation: Either Shellcommand ID or name is given
     if (!isset($params['target_type']) || empty($params['target_type']) || (!isset($params['target_id']) || empty($params['target_id'])) && (!isset($params['target_name']) || empty($params['target_name']))) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_MISSINGPARAMETER, '', 'must specify "target_type" and one among "target_id" and "target_name"');
     }
     //Post-relation: Either Target ID or name is given
     $possibleTargetTypes = PluginShellcommandsShellcommand_Item::getClasses(true);
     if (!in_array($params['target_type'], $possibleTargetTypes) || !class_exists($params['target_type'])) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_BADPARAMETER, '', '"target_type" is "' . $params['target_type'] . '" must be one of: ' . implode(', ', $possibleTargetTypes) . '');
     }
     //Post-relation: Given target type ($params['target_type']) is valid
     /** /Parameters check **/
     /** Command determination **/
     $commandIds = array();
     if (isset($params['command_id']) && !empty($params['command_id'])) {
         // ID(s) given
         $commandIds = (array) $params['command_id'];
     } else {
         // Name(s) given
         $command_names = (array) $params['command_name'];
         foreach ($command_names as $currentCommandName) {
             $command = new PluginShellcommandsShellcommand();
             if ($command->getFromDBbyName($currentCommandName)) {
                 $commandIds[] = $command->fields['id'];
             } else {
                 $invalidCommands[] = $currentCommandName;
             }
         }
     }
     //Post-relation: $commandIds is an array containing either provided or found (via given name) shellcommand IDs
     /** /Command determination **/
     /** Target determination **/
     $targetIds = array();
     if (isset($params['target_id']) && !empty($params['target_id'])) {
         // ID(s) given
         $targetIds = (array) $params['target_id'];
     } else {
         // Name(s) given
         $target_names = (array) $params['target_name'];
         foreach ($target_names as $currentTargetName) {
             $target = new $params['target_type']();
             if ($found = $target->find("`name` LIKE '" . $DB->escape($currentTargetName) . "'")) {
                 $targetIds = array_merge($targetIds, array_keys($found));
             } else {
                 $invalidTargets[] = $currentTargetName;
             }
         }
     }
     //Post-relation: $targetIds is an array containing either provided or found (via given name) targets IDs (of type $params['target_type'])
     /** /Target determination **/
     $commandIds = array_unique($commandIds);
     $targetIds = array_unique($targetIds);
     foreach ($targetIds as $currentTargetId) {
         $item = new $params['target_type']();
         $targetFound = $item->getFromDB($currentTargetId);
         if (!$targetFound) {
             $invalidTargets[] = $currentTargetId;
         }
         foreach ($commandIds as $currentCommandId) {
             $targetParams = PluginShellcommandsShellcommand_Item::resolveLinkOfCommand($currentCommandId, $item);
             if ($targetParams !== false) {
                 foreach ((array) $targetParams as $currentTargetParam) {
                     list($error, $executionOutput) = PluginShellcommandsShellcommand_Item::execCommand($currentCommandId, $currentTargetParam);
                     $executionOutputs[] = trim($executionOutput);
                 }
             } else {
                 $invalidCommands[] = $currentCommandId;
             }
         }
     }
     return array('callResults' => implode(PHP_EOL, $executionOutputs), 'invalidCommands' => $invalidCommands, 'invalidTargets' => $invalidTargets);
 }