/**
  * Show accounts associated to an item
  *
  * @since version 0.84
  *
  * @param $item            CommonDBTM object for which associated accounts must be displayed
  * @param $withtemplate    (default '')
  **/
 static function showForItem(CommonDBTM $item, $withtemplate = '')
 {
     global $DB, $CFG_GLPI;
     $ID = $item->getField('id');
     if ($item->isNewID($ID)) {
         return false;
     }
     if (!plugin_accounts_haveRight('accounts', 'r')) {
         return false;
     }
     if (!$item->can($item->fields['id'], 'r')) {
         return false;
     }
     if (empty($withtemplate)) {
         $withtemplate = 0;
     }
     $canedit = $item->canadditem('PluginAccountsAccount');
     $rand = mt_rand();
     $is_recursive = $item->isRecursive();
     $who = Session::getLoginUserID();
     if (count($_SESSION["glpigroups"]) && plugin_accounts_haveRight("my_groups", "r")) {
         $first_groups = true;
         $groups = "";
         foreach ($_SESSION['glpigroups'] as $val) {
             if (!$first_groups) {
                 $groups .= ",";
             } else {
                 $first_groups = false;
             }
             $groups .= "'" . $val . "'";
         }
         $ASSIGN = "( `groups_id` IN ({$groups}) OR `users_id` = '{$who}') ";
     } else {
         // Only personal ones
         $ASSIGN = " `users_id` = '{$who}' ";
     }
     $query = "SELECT `glpi_plugin_accounts_accounts_items`.`id` AS assocID,\n                       `glpi_entities`.`id` AS entity,\n                       `glpi_plugin_accounts_accounts`.`name` AS assocName,\n                       `glpi_plugin_accounts_accounts`.*\n                FROM `glpi_plugin_accounts_accounts_items`\n                LEFT JOIN `glpi_plugin_accounts_accounts`\n                 ON (`glpi_plugin_accounts_accounts_items`.`plugin_accounts_accounts_id`=`glpi_plugin_accounts_accounts`.`id`)\n                LEFT JOIN `glpi_entities` ON (`glpi_plugin_accounts_accounts`.`entities_id`=`glpi_entities`.`id`)\n                WHERE `glpi_plugin_accounts_accounts_items`.`items_id` = '{$ID}'\n                      AND `glpi_plugin_accounts_accounts_items`.`itemtype` = '" . $item->getType() . "' ";
     $query .= getEntitiesRestrictRequest(" AND", "glpi_plugin_accounts_accounts", '', '', true);
     if (!plugin_accounts_haveRight("all_users", "r")) {
         $query .= " AND {$ASSIGN} ";
     }
     $query .= " ORDER BY `assocName`";
     $result = $DB->query($query);
     $number = $DB->numrows($result);
     $i = 0;
     $accounts = array();
     $account = new PluginAccountsAccount();
     $used = array();
     if ($numrows = $DB->numrows($result)) {
         while ($data = $DB->fetch_assoc($result)) {
             $accounts[$data['assocID']] = $data;
             $used[$data['id']] = $data['id'];
         }
     }
     if ($canedit && $withtemplate < 2) {
         // Restrict entity for knowbase
         $entities = "";
         $entity = $_SESSION["glpiactive_entity"];
         if ($item->isEntityAssign()) {
             /// Case of personal items : entity = -1 : create on active entity (Reminder case))
             if ($item->getEntityID() >= 0) {
                 $entity = $item->getEntityID();
             }
             if ($item->isRecursive()) {
                 $entities = getSonsOf('glpi_entities', $entity);
             } else {
                 $entities = $entity;
             }
         }
         $limit = getEntitiesRestrictRequest(" AND ", "glpi_plugin_accounts_accounts", '', $entities, true);
         $q = "SELECT COUNT(*)\n               FROM `glpi_plugin_accounts_accounts`\n               WHERE `is_deleted` = '0'\n               {$limit}";
         $result = $DB->query($q);
         $nb = $DB->result($result, 0, 0);
         echo "<div class='firstbloc'>";
         if (plugin_accounts_haveRight('accounts', 'r') && $nb > count($used)) {
             echo "<form name='account_form{$rand}' id='account_form{$rand}' method='post'\n                   action='" . Toolbox::getItemTypeFormURL('PluginAccountsAccount') . "'>";
             echo "<table class='tab_cadre_fixe'>";
             echo "<tr class='tab_bg_1'>";
             echo "<td colspan='4' class='center'>";
             echo "<input type='hidden' name='entities_id' value='{$entity}'>";
             echo "<input type='hidden' name='is_recursive' value='{$is_recursive}'>";
             echo "<input type='hidden' name='itemtype' value='" . $item->getType() . "'>";
             echo "<input type='hidden' name='items_id' value='{$ID}'>";
             if ($item->getType() == 'Ticket') {
                 echo "<input type='hidden' name='tickets_id' value='{$ID}'>";
             }
             $account->dropdownAccounts("plugin_accounts_accounts_id", $entities, $used);
             echo "</td><td class='center' width='20%'>";
             echo "<input type='submit' name='additem' value=\"" . _sx('button', 'Associate a account', 'accounts') . "\" class='submit'>";
             echo "</td>";
             echo "</tr>";
             echo "</table>";
             Html::closeForm();
         }
         echo "</div>";
     }
     echo "<div class='spaced'>";
     if ($canedit && $number && $withtemplate < 2) {
         Html::openMassiveActionsForm('mass' . __CLASS__ . $rand);
         $massiveactionparams = array('num_displayed' => $number);
         Html::showMassiveActions(__CLASS__, $massiveactionparams);
     }
     echo "<table class='tab_cadre_fixe'>";
     if (Session::isMultiEntitiesMode()) {
         $colsup = 1;
     } else {
         $colsup = 0;
     }
     //hash
     $hashclass = new PluginAccountsHash();
     $hash = 0;
     $restrict = getEntitiesRestrictRequest(" ", "glpi_plugin_accounts_hashes", '', $item->getEntityID(), $hashclass->maybeRecursive());
     $hashes = getAllDatasFromTable("glpi_plugin_accounts_hashes", $restrict);
     if (!empty($hashes)) {
         foreach ($hashes as $hashe) {
             $hash = $hashe["hash"];
             $hash_id = $hashe["id"];
         }
         $alert = '';
     } else {
         $alert = __('There is no encryption key for this entity', 'accounts');
     }
     $aeskey = new PluginAccountsAesKey();
     if ($hash) {
         if (!$aeskey->getFromDBByHash($hash_id) || !$aeskey->fields["name"]) {
             echo "<tr><th colspan='" . (8 + $colsup) . "'>";
             _e('Encryption key', 'accounts');
             echo "<input type='password' name='aescrypted_key' id= 'aescrypted_key' autocomplete='off'>";
             echo "</th></tr>";
         }
     } else {
         echo "<tr><th colspan='" . (8 + $colsup) . "'>";
         echo __('Encryption key', 'accounts') . "<div class='red'>";
         echo $alert;
         echo "</div>";
         echo "</th></tr>";
     }
     echo "<tr>";
     if ($canedit && $number && $withtemplate < 2) {
         echo "<th width='10'>" . Html::getCheckAllAsCheckbox('mass' . __CLASS__ . $rand) . "</th>";
     }
     echo "<th>" . __('Name') . "</th>";
     if (Session::isMultiEntitiesMode()) {
         echo "<th>" . __('Entity') . "</th>";
     }
     echo "<th>" . __('Login') . "</th>";
     echo "<th>" . __('Password') . "</th>";
     echo "<th>" . __('Affected User', 'accounts') . "</th>";
     echo "<th>" . __('Type') . "</th>";
     echo "<th>" . __('Creation date') . "</th>";
     echo "<th>" . __('Expiration date') . "</th>";
     echo "</tr>";
     $used = array();
     if ($number) {
         Session::initNavigateListItems('PluginAccountsAccount', sprintf(__('%1$s = %2$s'), $item->getTypeName(1), $item->getName()));
         foreach ($accounts as $data) {
             $accountID = $data["id"];
             $link = NOT_AVAILABLE;
             if ($account->getFromDB($accountID)) {
                 $link = $account->getLink();
             }
             Session::addToNavigateListItems('PluginAccountsAccount', $accountID);
             $used[$accountID] = $accountID;
             $assocID = $data["assocID"];
             echo "<tr class='tab_bg_1" . ($data["is_deleted"] ? "_2" : "") . "'>";
             if ($canedit && $withtemplate < 2) {
                 echo "<td width='10'>";
                 Html::showMassiveActionCheckBox(__CLASS__, $data["assocID"]);
                 echo "</td>";
             }
             echo "<td class='center'>{$link}</td>";
             if (Session::isMultiEntitiesMode()) {
                 echo "<td class='center'>" . Dropdown::getDropdownName("glpi_entities", $data['entities_id']) . "</td>";
             }
             echo "<td class='center'>" . $data["login"] . "</td>";
             echo "<td class='center'>";
             //hash
             if (isset($hash_id) && $aeskey->getFromDBByHash($hash_id) && $aeskey->fields["name"]) {
                 echo "<input type='hidden' name='aeskey' id= 'aeskey'\n                        value='" . $aeskey->fields["name"] . "' class='' autocomplete='off'>";
                 echo "<input type='hidden' autocomplete='off'\n               name='encrypted_password\${$accountID}' value='" . $data["encrypted_password"] . "'>";
                 echo "<input type='text' name='hidden_password\${$accountID}' value='' size='30' >";
                 echo "<script language='javascript'>\n               var good_hash=\"{$hash}\";\n               var hash=SHA256(SHA256(document.getElementById(\"aeskey\").value));\n               if (hash != good_hash) {\n               document.getElementsByName(\"hidden_password\${$accountID}\").\n               item(0).value=\"" . __s('Wrong encryption key', 'accounts') . "\";\n            } else {\n            document.getElementsByName(\"hidden_password\${$accountID}\").\n            item(0).value=AESDecryptCtr(document.getElementsByName(\"encrypted_password\${$accountID}\").\n            item(0).value,SHA256(document.getElementById(\"aeskey\").value), 256)};</script>";
             } else {
                 $url = $CFG_GLPI["root_doc"] . "/plugins/accounts/front/account.form.php";
                 echo "&nbsp;<input type='button' id='decrypte_link{$accountID}' name='decrypte' value='" . __s('Uncrypt', 'accounts') . "'\n                        class='submit' onclick='return false;'>";
                 echo "<script type='text/javascript'>\n               Ext.get('decrypte_link{$accountID}').on('click', function () {\n\n               Ext.Ajax.request({\n               url: '../plugins/accounts/ajax/get_new_crsf_token.php',\n               success: function(response, opts) {\n               var token = response.responseText;\n               Ext.select('#account_form input[name=_glpi_csrf_token]')\n               .set({'value': token});\n\n            },\n            failure: function(response, opts) {\n            console.log('server-side failure with status code ' + response.status);\n            }\n            });\n\n            var good_hash=\"{$hash}\";\n            var hash=SHA256(SHA256(document.getElementById(\"aescrypted_key\").value));\n            if (hash != good_hash) {\n            alert(\"" . __('Wrong encryption key', 'accounts') . "\");\n                     return false;\n            };\n                     alert(AESDecryptCtr(\"" . $data['encrypted_password'] . "\",\n                     SHA256(document.getElementById(\"aescrypted_key\").value),\n                     256));\n\n                     callAjax(\"{$url}\",\n                     \"{$accountID}\" ,\n                     document.getElementsByName(\"name\").item(0).value,\n                     document.getElementsByName(\"_glpi_csrf_token\").item(0).value);\n\n            });\n            </script>";
             }
             echo "</td>";
             echo "<td class='center'>";
             echo getUsername($data["users_id"]);
             echo "</td>";
             echo "<td class='center'>";
             echo Dropdown::getDropdownName("glpi_plugin_accounts_accounttypes", $data["plugin_accounts_accounttypes_id"]);
             echo "</td>";
             echo "<td class='center'>" . Html::convdate($data["date_creation"]) . "</td>";
             if ($data["date_expiration"] <= date('Y-m-d') && !empty($data["date_expiration"])) {
                 echo "<td class='center'>";
                 echo "<div class='deleted'>" . Html::convdate($data["date_expiration"]) . "</div>";
                 echo "</td>";
             } else {
                 if (empty($data["date_expiration"])) {
                     echo "<td class='center'>" . __('Don\'t expire', 'accounts') . "</td>";
                 } else {
                     echo "<td class='center'>" . Html::convdate($data["date_expiration"]) . "</td>";
                 }
             }
             echo "</tr>";
             $i++;
         }
     }
     echo "</table>";
     if ($canedit && $number && $withtemplate < 2) {
         $massiveactionparams['ontop'] = false;
         Html::showMassiveActions(__CLASS__, $massiveactionparams);
         Html::closeForm();
     }
     echo "</div>";
 }
Beispiel #2
0
function plugin_accounts_MassiveActionsDisplay($options = array())
{
    $PluginAccountsAccount = new PluginAccountsAccount();
    if (in_array($options['itemtype'], PluginAccountsAccount::getTypes(true))) {
        $PluginAccountsAccount->dropdownAccounts("plugin_accounts_accounts_id");
        echo "<input type=\"submit\" name=\"massiveaction\" class=\"submit\" value='" . _sx('button', 'Post') . "'>";
    }
    return "";
}