diff --git a/inc/rgpdtools.class.php b/inc/rgpdtools.class.php index 211d81a..346c57b 100644 --- a/inc/rgpdtools.class.php +++ b/inc/rgpdtools.class.php @@ -1,4 +1,5 @@ self::getMenuName(), - 'page' => Plugin::getPhpDir('rgpdtools', false) . '/front/rgpdtools.form.php', - 'icon' => self::getIcon(), - ]; - - return $menu; - } - - public static function anonymizeUserLogs($POST) { - $userID = $POST['userID']; - if (!$userID) { - Session::addMessageAfterRedirect(__("user is required", 'rgpdtools'), true, WARNING, true); - Html::redirect('rgpdtools.form.php'); - } - $retentionPeriod = $POST['userLogRetentionPeriod']; - self::anonymizeUserLogActivity($userID, $retentionPeriod); - - return true; - } - - public static function deleteUserLinkItems($POST) { - $userID = $POST['userID']; - $allUser = array_key_exists('allUser', $POST); - if (!$userID && !$allUser) { - Session::addMessageAfterRedirect(__("user is required or all user checkbox", 'rgpdtools'), true, WARNING, true); - Html::redirect('rgpdtools.form.php'); - } - $deleteItemTypes = $POST['deleteItemTypes']; - $retentionPeriods = $POST['retentionPeriods']; - $nbUnlinkedElmts = 0; - foreach ($deleteItemTypes as $itemType) { - $nbUnlinkedElmts += self::deleteDocumentsToDate($userID, $itemType, $retentionPeriods[$itemType], $allUser); - } - - return $nbUnlinkedElmts; - } - - public static function generateExport($POST) { - $userID = $POST['userID']; - if (!$userID) { - Session::addMessageAfterRedirect(__("user is required", 'rgpdtools'), true, WARNING, true); - Html::redirect('rgpdtools.form.php'); - } - $user = new User(); - $user->getFromDB($userID); - $now = new DateTime(); - $rand = mt_rand(); - $filename = 'export-rgpd-data_' . $user->getField('name') . '_' . $now->format('d-m-Y') . '_' . $rand . '.ods'; - - $spreadsheet = new Spreadsheet(); - - //First tab for user infos - $nbWorkSheet = 0; - $ws_user = new Worksheet($spreadsheet, 'User'); - $spreadsheet->addSheet($ws_user, $nbWorkSheet); - $spreadsheet->setActiveSheetIndex($nbWorkSheet); - $objectInfos = self::getUserInfos($user); - self::injectRowHeader($spreadsheet, $objectInfos, 'User'); - self::injectRowValues($spreadsheet, $objectInfos, 2, 'User'); - - $itemTypes = $POST['itemTypes']; - // récupération des éléments associés au user - $allUsedItems = self::getAllUsedItemsForUser($userID, $itemTypes); - // pour chaque élément séléctionné ajout d'un onglet - - foreach ($itemTypes as $itemType) { - $nbWorkSheet++; - $new_ws = new Worksheet($spreadsheet, $itemType); - $spreadsheet->addSheet($new_ws, $nbWorkSheet); - $spreadsheet->setActiveSheetIndex($nbWorkSheet); - if (array_key_exists($itemType, $allUsedItems)) { - $objectItems = $allUsedItems[$itemType]; - // inject header - self::injectRowHeader($spreadsheet, $objectItems[0], $itemType); - // inject values - $row = 2; - foreach ($objectItems as $objectInfos) { - self::injectRowValues($spreadsheet, $objectInfos, $row, $itemType); - $row++; +class PluginRgpdtoolsRgpdtools { + + public function __construct() { + + } + + public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { + switch ($item::getType()) { + case User::getType(): + return __('RgpdTools', 'rgpdtools'); + break; + } + return ''; + } + + public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { + switch ($item::getType()) { + case User::getType(): + self::displayTabContentForUser($item); + break; + } + + return true; + } + + public static function getMenuName() { + return __('RgpdTools', 'rgpdtools'); + } + + public static function getIcon() { + return "fa fa-user-shield"; + } + + public static function getMenuContent() { + $menu = [ + 'title' => self::getMenuName(), + 'page' => Plugin::getPhpDir('rgpdtools', false) . '/front/rgpdtools.form.php', + 'icon' => self::getIcon(), + ]; + + return $menu; + } + + public static function anonymizeUserLogs($POST) { + $userID = $POST['userID']; + if (!$userID) { + Session::addMessageAfterRedirect(__("user is required", 'rgpdtools'), true, WARNING, true); + Html::redirect('rgpdtools.form.php'); + } + $retentionPeriod = $POST['userLogRetentionPeriod']; + self::anonymizeUserLogActivity($userID, $retentionPeriod); + + return true; + } + + public static function deleteUserLinkItems($POST) { + $userID = $POST['userID']; + $allUser = array_key_exists('allUser', $POST); + if (!$userID && !$allUser) { + Session::addMessageAfterRedirect(__("user is required or all user checkbox", 'rgpdtools'), true, WARNING, true); + Html::redirect('rgpdtools.form.php'); + } + $deleteItemTypes = $POST['deleteItemTypes']; + $retentionPeriods = $POST['retentionPeriods']; + $nbUnlinkedElmts = 0; + foreach ($deleteItemTypes as $itemType) { + $nbUnlinkedElmts += self::unlinkUserAssociateElementsToDate($userID, $itemType, $retentionPeriods[$itemType], $allUser); + } + + return $nbUnlinkedElmts; + } + + public static function generateExport($POST) { + $userID = $POST['userID']; + if (!$userID) { + Session::addMessageAfterRedirect(__("user is required", 'rgpdtools'), true, WARNING, true); + Html::redirect('rgpdtools.form.php'); + } + $user = new User(); + $user->getFromDB($userID); + $now = new DateTime(); + $rand = mt_rand(); + $filename = 'export-rgpd-data_' . $user->getField('name') . '_' . $now->format('d-m-Y') . '_' . $rand . '.ods'; + + $spreadsheet = new Spreadsheet(); + + //First tab for user infos + $nbWorkSheet = 0; + $ws_user = new Worksheet($spreadsheet, 'User'); + $spreadsheet->addSheet($ws_user, $nbWorkSheet); + $spreadsheet->setActiveSheetIndex($nbWorkSheet); + $objectInfos = self::getUserInfos($user); + self::injectRowHeader($spreadsheet, $objectInfos, 'User'); + self::injectRowValues($spreadsheet, $objectInfos, 2, 'User'); + + $itemTypes = $POST['itemTypes']; + // récupération des éléments associés au user + $allUsedItems = self::getAllUsedItemsForUser($userID, $itemTypes); + // pour chaque élément séléctionné ajout d'un onglet + + foreach ($itemTypes as $itemType) { + $nbWorkSheet++; + $new_ws = new Worksheet($spreadsheet, $itemType); + $spreadsheet->addSheet($new_ws, $nbWorkSheet); + $spreadsheet->setActiveSheetIndex($nbWorkSheet); + if (array_key_exists($itemType, $allUsedItems)) { + $objectItems = $allUsedItems[$itemType]; + // inject header + self::injectRowHeader($spreadsheet, $objectItems[0], $itemType); + // inject values + $row = 2; + foreach ($objectItems as $objectInfos) { + self::injectRowValues($spreadsheet, $objectInfos, $row, $itemType); + $row++; + } } - } - } - - header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); - header('Content-Disposition: attachment;filename="' . $filename . '"'); - header("Pragma: no-cache"); - header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); - header("Cache-Control: post-check=0, pre-check=0", false); - $writer = new Ods($spreadsheet); - $writer->save('php://output'); - } - - - public static function deleteUploadedDocuments($POST) { - $userID = $POST['userID']; - $allUser = array_key_exists('allUser', $POST); - if (!$userID && !$allUser) { - Session::addMessageAfterRedirect(__("user is required or all user checkbox", 'rgpdtools'), true, WARNING, true); - Html::redirect('rgpdtools.form.php'); - } - $deleteItemTypes = $POST['deleteItemTypes']; - $retentionPeriods = $POST['retentionPeriods']; - $nbDeleteDocuments = 0; - foreach ($deleteItemTypes as $itemType) { - $nbDeleteDocuments += self::deleteDocumentsToDate($userID, $itemType, $retentionPeriods[$itemType], $allUser); - } - - return $nbDeleteDocuments; - } - - private static function displayTabContentForUser(User $item) { - $users_id = $item->getField('id'); - $html = ''; - $html .= self::generateExportForm($users_id); - $html .= self::generateUnlinkItemsForm($users_id); - $html .= self::generateAnonymiseForm($users_id); - - echo $html; - } - - public function getFormsForCompleteForm() { - - $html = '
'; - $users_id = null; - - $html .= self::generateExportForm($users_id); - if(Session::haveRight(strtolower('logs'), READ)) { + } + + header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); + header('Content-Disposition: attachment;filename="' . $filename . '"'); + header("Pragma: no-cache"); + header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); + header("Cache-Control: post-check=0, pre-check=0", false); + $writer = new Ods($spreadsheet); + $writer->save('php://output'); + } + + public static function deleteUploadedDocuments($POST) { + $userID = $POST['userID']; + $allUser = array_key_exists('allUser', $POST); + if (!$userID && !$allUser) { + Session::addMessageAfterRedirect(__("user is required or all user checkbox", 'rgpdtools'), true, WARNING, true); + Html::redirect('rgpdtools.form.php'); + } + $deleteItemTypes = $POST['deleteItemTypes']; + $retentionPeriods = $POST['retentionPeriods']; + $nbDeleteDocuments = 0; + foreach ($deleteItemTypes as $itemType) { + $nbDeleteDocuments += self::deleteDocumentsToDate($userID, $itemType, $retentionPeriods[$itemType], $allUser); + } + + return $nbDeleteDocuments; + } + + private static function displayTabContentForUser(User $item) { + $users_id = $item->getField('id'); + $html = ''; + $html .= self::generateExportForm($users_id); + $html .= self::generateUnlinkItemsForm($users_id); + $html .= self::generateAnonymiseForm($users_id); + + echo $html; + } + + public function getFormsForCompleteForm() { + + $html = '
'; + $users_id = null; + + $html .= self::generateExportForm($users_id); + if (Session::haveRight(strtolower('logs'), READ)) { $html .= self::generateAnonymiseForm($users_id); - } - $html .= self::generateUnlinkItemsForm($users_id); - if(Session::haveRight(strtolower('document'), UPDATE)) { - $html .= self::generateDeleteDocumentsForm($users_id); - } - - $html .= '
'; - - echo $html; - } - - private static function generateExportForm($users_id) { - $itemsTypes = self::getUserAssociableItemTypes(READ); - $html = ''; - $rand = mt_rand(); - $idForm = "useritemsexport_form$rand"; - - $html .= "
"; - $html .= "
"; - $html .= "
"; - $html .= '
'; - $html .= '
'; - $html .= '
' . __('Export users data', 'rgpdtools') . '
'; - $html .= ""; - $html .= ''; - $html .= self::getUserIdBlock($users_id); - $html .= ''; - $html .= ''; + foreach ($itemsTypes as $itemType) { + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + } + + $html .= ""; + $html .= ""; + $html .= ""; + $html .= ''; + $html .= "
'; - $html .= '
+ $html .= '
'; + $html .= '
'; + $html .= '
' . __('Export users data', 'rgpdtools') . '
'; + $html .= ""; + $html .= ''; + $html .= self::getUserIdBlock($users_id); + $html .= ''; + $html .= '' . "\n"; - $html .= ''; - $html .= ''; - $html .= ''; - foreach ($itemsTypes as $itemType) { - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - } - - $html .= ""; - $html .= ""; - $html .= ""; - $html .= ''; - $html .= "
'; + $html .= '
' . __('Choosing what to export', 'rgpdtools') . '
'; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= '' . __(str_replace(['ItilFollowup', 'TicketTask'],['Followup', 'Task'],$itemType)) . '
"; - $html .= '
'; - $html .= Html::closeForm(false); - $html .= "
"; - $html .= "
"; - $html .= ""; - - return $html; - } - - private static function generateUnlinkItemsForm($users_id) { - $itemsTypes = self::getUserAssociableItemTypes(UPDATE); - $html = ''; - $rand = mt_rand(); - - $config = [ - 'value' => 6, - 'display' => false, - 'values' => range(1, 100), - 'class' => 'required', - 'noselect2' => false - ]; - $values = []; - for ($i = 0; $i < 100; $i++) { - $values[$i] = $i . ' ' . __('month'); - } - - $idForm = "useritemsdelete_form$rand"; - $html .= "
"; - $html .= "
"; - $html .= "'; + $html .= '
'; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= '' . __(str_replace(['ItilFollowup', 'TicketTask'], ['Followup', 'Task'], $itemType)) . '
"; + $html .= '
'; + $html .= Html::closeForm(false); + $html .= "
"; + $html .= "
"; + $html .= "
"; + + return $html; + } + + private static function generateUnlinkItemsForm($users_id) { + $itemsTypes = self::getUserAssociableItemTypes(UPDATE); + $html = ''; + $rand = mt_rand(); + + $config = [ + 'value' => 6, + 'display' => false, + 'values' => range(1, 100), + 'class' => 'required', + 'noselect2' => false + ]; + $values = []; + for ($i = 0; $i < 100; $i++) { + $values[$i] = $i . ' ' . __('month'); + } + + $idForm = "useritemsdelete_form$rand"; + $html .= "
"; + $html .= "
"; + $html .= ""; - $html .= '
'; - $html .= '
'; - $html .= '
' . __('Removal of links to the user', 'rgpdtools'). '
'; - $html .= ""; - $html .= ''; - $html .= self::getUserIdBlock($users_id, true); - $html .= ''; - $html .= "
"; - $html .= ""; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - } - return $html; - } - - private static function generateAnonymiseForm($users_id) { - $html = ''; - $rand = mt_rand(); - $config = [ - 'value' => 6, - 'display' => false, - 'values' => range(1, 100), - 'class' => 'required', - 'noselect2' => false - ]; - $values = []; - for ($i = 0; $i < 100; $i++) { - $values[$i] = $i . ' ' . __('month'); - } - $idForm = "userpurgelogs_form$rand"; - $html .= "
"; - $html .= "
"; - $html .= "'; + $html .= '
'; + $html .= ''; + + foreach ($itemsTypes as $itemType) { + $html .= ''; + $html .= ''; + $html_parts = Dropdown::showFromArray('retentionPeriods[' . $itemType . ']', $values, $config); + $html .= ''; + $html .= ''; + } + $html .= ""; + $html .= ""; + $html .= ""; + $html .= ''; + $html .= "
'; - $html .= '
+ $html .= '
'; + $html .= '
'; + $html .= '
' . __('Removal of links to the user', 'rgpdtools') . '
'; + $html .= ""; + $html .= ''; + $html .= self::getUserIdBlock($users_id, true); + $html .= ''; + $html .= "
"; + $html .= ""; + $html .= ''; + $html .= ''; + $html .= '' . "\n"; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - - foreach ($itemsTypes as $itemType) { - $html .= ''; - $html .= ''; - $html_parts = Dropdown::showFromArray('retentionPeriods[' . $itemType . ']', $values, $config); - $html .= ''; - $html .= ''; - } - $html .= ""; - $html .= ""; - $html .= ""; - $html .= ''; - $html .= "
'; + $html .= '
' . __('Choice of elements for which to remove links to the user', 'rgpdtools') . '' . __('For each item, retention period', 'rgpdtools') . '
'; - $html .= '
'; - $html .= ''; - $html .= ''; - $html .= '
'; - $html .= '
' . $html_parts . '
"; - $html .= '
'; - $html .= Html::closeForm(false); - $html .= "
"; - $html .= "
"; - $html .= ""; - - return $html; - } - - private static function getUserIdBlock($users_id, $withAlluserCheckbox = false) { - $user = ''; - $html = ''; - if ($users_id) { - $html .= ""; - } else { - $html .= '
' . _n('User', 'User', 2) . ''; - $userSelectorOptions = [ - 'name' => 'userID', - //'used' => '', - 'right' => 'all', - 'comments' => false, - 'display' => false, - ]; - if (!$withAlluserCheckbox) { - $userSelectorOptions['specific_tags'] = ['required' => 'required']; - } - $html .= User::dropdown($userSelectorOptions); - if ($withAlluserCheckbox) { - $html .= '
 ' . __('Apply to all users', 'rgpdtools'); - } - $html .= '
' . __('For each item, retention period', 'rgpdtools') . '
'; + $html .= '
'; + $html .= ''; + $html .= ''; + $html .= '
'; + $html .= '
' . $html_parts . '
"; + $html .= '
'; + $html .= Html::closeForm(false); + $html .= "
"; + $html .= "
"; + $html .= "
"; + + return $html; + } + + private static function getUserIdBlock($users_id, $withAlluserCheckbox = false) { + $user = ''; + $html = ''; + if ($users_id) { + $html .= ""; + } else { + $html .= ''; + $html .= '' . _n('User', 'User', 2) . ''; + $html .= ''; + $userSelectorOptions = [ + 'name' => 'userID', + //'used' => '', + 'right' => 'all', + 'comments' => false, + 'display' => false, + ]; + if (!$withAlluserCheckbox) { + $userSelectorOptions['specific_tags'] = ['required' => 'required']; + } + $html .= User::dropdown($userSelectorOptions); + if ($withAlluserCheckbox) { + $html .= '
 ' . __('Apply to all users', 'rgpdtools'); + } + $html .= ''; + $html .= ''; + } + return $html; + } + + private static function generateAnonymiseForm($users_id) { + $html = ''; + $rand = mt_rand(); + $config = [ + 'value' => 6, + 'display' => false, + 'values' => range(1, 100), + 'class' => 'required', + 'noselect2' => false + ]; + $values = []; + for ($i = 0; $i < 100; $i++) { + $values[$i] = $i . ' ' . __('month'); + } + $idForm = "userpurgelogs_form$rand"; + $html .= "
"; + $html .= "
"; + $html .= ""; - $html .= '
'; - $html .= '
'; - $html .= '
' . __('Purge logs referring to the user', 'rgpdtools'). '
'; - $html .= ""; - $html .= ''; - $html .= self::getUserIdBlock($users_id); - $html .= ''; - $html .= ''; - $html_parts = Dropdown::showFromArray('userLogRetentionPeriod', $values, $config); - $html .= ''; - $html .= ''; - $html .= ""; - $html .= ""; - $html .= ""; - $html .= ''; - $html .= "
' . __('Retention Period', 'rgpdtools') . '' . $html_parts . '
"; - $html .= '
'; - $html .= Html::closeForm(false); - $html .= "
"; - $html .= "
"; - $html .= "
"; - - return $html; - } - - private static function generateDeleteDocumentsForm($users_id) { - $itemsTypes = self::getUserAssociableItemTypes(UPDATE); - $html = ''; - $rand = mt_rand(); - - $config = [ - 'value' => 6, - 'display' => false, - 'values' => range(1, 100), - 'class' => 'required', - 'noselect2' => false - ]; - $values = []; - for ($i = 0; $i < 100; $i++) { - $values[$i] = $i . ' ' . __('month'); - } - - $idForm = "deleteDocuments_form$rand"; - $html .= "
"; - $html .= "
"; - $html .= "'; + $html .= ""; + $html .= ''; + $html .= self::getUserIdBlock($users_id); + $html .= ''; + $html .= ''; + $html_parts = Dropdown::showFromArray('userLogRetentionPeriod', $values, $config); + $html .= ''; + $html .= ''; + $html .= ""; + $html .= ""; + $html .= ""; + $html .= ''; + $html .= "
' . __('Retention Period', 'rgpdtools') . '' . $html_parts . '
"; + $html .= '
'; + $html .= Html::closeForm(false); + $html .= "
"; + $html .= "
"; + $html .= ""; + + return $html; + } + + private static function generateDeleteDocumentsForm($users_id) { + $itemsTypes = self::getUserAssociableItemTypes(UPDATE); + $html = ''; + $rand = mt_rand(); + + $config = [ + 'value' => 6, + 'display' => false, + 'values' => range(1, 100), + 'class' => 'required', + 'noselect2' => false + ]; + $values = []; + for ($i = 0; $i < 100; $i++) { + $values[$i] = $i . ' ' . __('month'); + } + + $idForm = "deleteDocuments_form$rand"; + $html .= "
"; + $html .= "
"; + $html .= ""; - $html .= '
'; - $html .= '
'; - $html .= '
' . __('Delete old uploaded documents', 'rgpdtools'). '
'; - $html .= ""; - $html .= ''; - $html .= self::getUserIdBlock($users_id, true); - $html .= ''; - $html .= "
"; - $html .= ""; - $html .= ''; - $html .= ''; - $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + + foreach ($itemsTypes as $itemType) { + $html .= ''; + $html .= ''; + $html_parts = Dropdown::showFromArray('retentionPeriods[' . $itemType . ']', $values, $config); + $html .= ''; + $html .= ''; + } + $html .= ""; + $html .= ""; + $html .= ""; + $html .= ''; + $html .= "
'; - $html .= '
+ $html .= '
'; + $html .= '
'; + $html .= '
' . __('Delete old uploaded documents', 'rgpdtools') . '
'; + $html .= ""; + $html .= ''; + $html .= self::getUserIdBlock($users_id, true); + $html .= ''; + $html .= "
"; + $html .= ""; + $html .= ''; + $html .= ''; + $html .= '' . "\n"; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - - foreach ($itemsTypes as $itemType) { - $html .= ''; - $html .= ''; - $html_parts = Dropdown::showFromArray('retentionPeriods[' . $itemType . ']', $values, $config); - $html .= ''; - $html .= ''; - } - $html .= ""; - $html .= ""; - $html .= ""; - $html .= ''; - $html .= "
'; + $html .= '
' . __('Choice of elements for which to delete uploaded documents', 'rgpdtools') . '' . __('For each item, retention period', 'rgpdtools') . '
'; - $html .= '
'; - $html .= ''; - $html .= ''; - $html .= '
'; - $html .= '
' . $html_parts . '
"; - $html .= '
'; - $html .= Html::closeForm(false); - $html .= "
"; - $html .= "
"; - $html .= ""; - - return $html; - } + $html .= '
' . __('Choice of elements for which to delete uploaded documents', 'rgpdtools') . '' . __('For each item, retention period', 'rgpdtools') . '
'; + $html .= '
'; + $html .= ''; + $html .= ''; + $html .= '
'; + $html .= '
' . $html_parts . '
"; + $html .= '
'; + $html .= Html::closeForm(false); + $html .= "
"; + $html .= "
"; + $html .= "
"; + + return $html; + } /** * Get all used items for user * @param ID of user * @return array */ - private static function getAllUsedItemsForUser($ID, $itemTypes) { - global $DB; + private static function getAllUsedItemsForUser($ID, $itemTypes) { + global $DB; - $items = []; + $items = []; - foreach ($itemTypes as $itemtype) { - if (!($item = getItemForItemtype($itemtype))) { - continue; - } - if ($item->canView() && !in_array($itemtype, ['Ticket'])) { - $itemtable = getTableForItemType($itemtype); + foreach ($itemTypes as $itemtype) { + if (!($item = getItemForItemtype($itemtype))) { + continue; + } + if ($item->canView() && !in_array($itemtype, ['Ticket'])) { + $itemtable = getTableForItemType($itemtype); - $query = "SELECT * + $query = "SELECT * FROM `$itemtable` WHERE `users_id` = '$ID'"; - if ($item->maybeTemplate()) { - $query .= " AND `is_template` = '0' "; - } - if ($item->maybeDeleted()) { - $query .= " AND `is_deleted` = '0' "; + if ($item->maybeTemplate()) { + $query .= " AND `is_template` = '0' "; + } + if ($item->maybeDeleted()) { + $query .= " AND `is_deleted` = '0' "; + } + $result = $DB->query($query); + + $type_name = $item->getTypeName(); + + if ($DB->numrows($result) > 0) { + while ($data = $DB->fetchAssoc($result)) { + $items[$itemtype][] = $data; + } + } } - $result = $DB->query($query); - - $type_name = $item->getTypeName(); + } - if ($DB->numrows($result) > 0) { - while ($data = $DB->fetchAssoc($result)) { - $items[$itemtype][] = $data; - } - } - } - } - - // Consumables - $consumables = $DB->request( - [ - 'SELECT' => ['name', 'otherserial'], - 'FROM' => ConsumableItem::getTable(), - 'WHERE' => [ - 'id' => new QuerySubQuery( - [ - 'SELECT' => 'consumableitems_id', - 'FROM' => Consumable::getTable(), - 'WHERE' => [ - 'itemtype' => User::class, - 'items_id' => $ID - ], - ] - ) - ], - ] - ); - foreach ($consumables as $data) { - $items['ConsumableItem'][] = $data; - } - - // Tickets - if($itemtype == 'Ticket') { - $tickets = $DB->request( + // Consumables + $consumables = $DB->request( [ + 'SELECT' => ['name', 'otherserial'], + 'FROM' => ConsumableItem::getTable(), + 'WHERE' => [ + 'id' => new QuerySubQuery( + [ + 'SELECT' => 'consumableitems_id', + 'FROM' => Consumable::getTable(), + 'WHERE' => [ + 'itemtype' => User::class, + 'items_id' => $ID + ], + ] + ) + ], + ] + ); + foreach ($consumables as $data) { + $items['ConsumableItem'][] = $data; + } + + // Tickets + if ($itemtype == 'Ticket') { + $tickets = $DB->request( + [ 'SELECT' => ['t.*'], 'DISTINCT' => true, - 'FROM' => Ticket::getTable().' AS t', + 'FROM' => Ticket::getTable() . ' AS t', 'INNER JOIN' => [ Ticket_User::getTable() => [ 'FKEY' => [ @@ -589,230 +589,252 @@ private static function getAllUsedItemsForUser($ID, $itemTypes) { 'ORDER' => 'date' ] ); - foreach ($tickets as $data) { - $items['Ticket'][] = $data; - } - } - - // getComputersIDs - $computersIds = []; - if (array_key_exists('Computer', $items) && count($items['Computer'])) { - foreach ($items['Computer'] as $computer) { - $computersIds[] = $computer['id']; - } - } - // Software - $softwares = self::getUserSoftwares($computersIds); - foreach ($softwares as $data) { - $items['Software'][] = $data; - } - - //SoftwaresLicences getUserSoftwareLicences - $softwareLicenses = self::getUserSoftwareLicences($computersIds); - foreach ($softwareLicenses as $data) { - $items['SoftwareLicense'][] = $data; - } - - return $items; - } - - private static function getUserAssociableItemTypes($permissionAccess = READ) { - global $CFG_GLPI; - - $itemsTypes = ['Ticket', 'ItilFollowup', 'TicketTask']; - - $linkuser_types = array_merge($CFG_GLPI['linkuser_types'], $itemsTypes);; - foreach($linkuser_types as $itemsType){ - if(Session::haveRight(strtolower($itemsType), $permissionAccess)){ - $itemsTypes[]= $itemsType; - } - } - $itemsTypes = array_unique($itemsTypes); - - return $itemsTypes; - } - - private static function injectRowHeader($spreadsheet, $objectInfos, $itemType) { - $col = 1; - $row = 1; - $sheet = $spreadsheet->getActiveSheet(); - $exportablefields = self::getExportablefields($itemType); - foreach (array_keys($objectInfos) as $key) { - if (!count($exportablefields) || in_array($key, $exportablefields)) { - $sheet->setCellValueByColumnAndRow($col, $row, __($key)); - $col++; - } - } - return $row++; - } - - private static function injectRowValues($spreadsheet, $objectInfos, $row, $itemType) { - $col = 1; - $sheet = $spreadsheet->getActiveSheet(); - $exportablefields = self::getExportablefields($itemType); - foreach ($objectInfos as $key => $info) { - if (!count($exportablefields) || in_array($key, $exportablefields)) { - $sheet->setCellValueByColumnAndRow($col, $row, $info); - $col++; - } - } - return $row++; - } - - private static function getUserInfos($user) { - $infos = []; - - $infos = [ - 'id' => $user->getID(), - 'name' => $user->getField('name'), - 'realname' => $user->getField('realname'), - 'firstname' => $user->getField('firstname'), - 'phone' => $user->getField('phone'), - 'phone2' => $user->getField('phone2'), - 'mobile' => $user->getField('mobile'), - 'email' => $user->getDefaultEmail(), - 'comment' => $user->getField('comment'), - 'date_creation' => $user->getField('date_creation'), - 'date_mod' => $user->getField('date_mod'), - ]; - - return $infos; - } + foreach ($tickets as $data) { + $items['Ticket'][] = $data; + } + } + + // getComputersIDs + $computersIds = []; + if (array_key_exists('Computer', $items) && count($items['Computer'])) { + foreach ($items['Computer'] as $computer) { + $computersIds[] = $computer['id']; + } + } + // Software + $softwares = self::getUserSoftwares($computersIds); + foreach ($softwares as $data) { + $items['Software'][] = $data; + } + + //SoftwaresLicences getUserSoftwareLicences + $softwareLicenses = self::getUserSoftwareLicences($computersIds); + foreach ($softwareLicenses as $data) { + $items['SoftwareLicense'][] = $data; + } + + return $items; + } + + private static function getUserAssociableItemTypes($permissionAccess = READ) { + global $CFG_GLPI; + + $itemsTypes = ['Ticket', 'ItilFollowup', 'TicketTask']; + + $linkuser_types = array_merge($CFG_GLPI['linkuser_types'], $itemsTypes); + ; + foreach ($linkuser_types as $itemsType) { + if (Session::haveRight(strtolower($itemsType), $permissionAccess)) { + $itemsTypes[] = $itemsType; + } + } + $itemsTypes = array_unique($itemsTypes); + + return $itemsTypes; + } + + private static function injectRowHeader($spreadsheet, $objectInfos, $itemType) { + $col = 1; + $row = 1; + $sheet = $spreadsheet->getActiveSheet(); + $exportablefields = self::getExportablefields($itemType); + foreach (array_keys($objectInfos) as $key) { + if (!count($exportablefields) || in_array($key, $exportablefields)) { + $sheet->setCellValueByColumnAndRow($col, $row, __($key)); + $col++; + } + } + return $row++; + } + + private static function injectRowValues($spreadsheet, $objectInfos, $row, $itemType) { + $col = 1; + $sheet = $spreadsheet->getActiveSheet(); + $exportablefields = self::getExportablefields($itemType); + foreach ($objectInfos as $key => $info) { + if (!count($exportablefields) || in_array($key, $exportablefields)) { + $sheet->setCellValueByColumnAndRow($col, $row, $info); + $col++; + } + } + return $row++; + } + + private static function getUserInfos($user) { + $infos = []; + + $infos = [ + 'id' => $user->getID(), + 'name' => $user->getField('name'), + 'realname' => $user->getField('realname'), + 'firstname' => $user->getField('firstname'), + 'phone' => $user->getField('phone'), + 'phone2' => $user->getField('phone2'), + 'mobile' => $user->getField('mobile'), + 'email' => $user->getDefaultEmail(), + 'comment' => $user->getField('comment'), + 'date_creation' => $user->getField('date_creation'), + 'date_mod' => $user->getField('date_mod'), + ]; + + return $infos; + } /** * fonction permettant de limiter les champs remontés dans l'export en fonction du type d'élément * @param type $className * @return string */ - private static function getExportablefields($className) { - $fields = []; - // id fields is empty, all fields are export - switch ($className) { - case 'Computer': - //$fields = ['id','name','serial','otherserial', 'contact', 'contact_num', 'comment','date_mod', 'date_creation']; - break; - case 'User': - // $fields = ['id', 'name','phone','phone2','mobile','email']; - break; - } - return $fields; - } - - private static function unlinkUserAssociateElementsToDate($userID, $className, $retentionPeriod, $allUser = false) { - global $DB; - if (!class_exists($className)) { - $errorMessage = sprintf( - __('The class %1$s can\'t be instanciate because not finded on GLPI.', 'rgpdtools'), - $className - ); - throw new \Exception($errorMessage); - } - $date = new DateTime(); - $date->sub(new DateInterval('P' . $retentionPeriod . 'M')); - - $log = new Log(); - $object = new $className(); - //$object = new Computer(); // for test - // recherche des éléments liés au user en bdd - $querySelect = "SELECT t1.id FROM " . $object->getTable() . " t1 " - . "INNER JOIN " . $log->getTable() . " lg ON t1.id = lg.items_id AND itemtype='" . $className . "' AND id_search_option=70 " - . "WHERE new_value LIKE '% (" . $userID . ")' AND lg.date_mod <= '" . $date->format('Y-m-d') . "' "; - if (!$allUser) { - $querySelect .= "AND users_id=$userID "; - } - $querySelect .= "GROUP BY t1.id"; - - $results = $DB->query($querySelect); - $nbUnlinkedElmts = $DB->numrows($results); - if ($nbUnlinkedElmts) { - // construction du tableau des ids - $objectsIds = []; - while ($row = $DB->fetchAssoc($results)) { - array_push($objectsIds, $row['id']); - } - $query = "UPDATE " . $object->getTable() . " SET users_id=NULL WHERE id IN (" . implode(',', $objectsIds) . ")"; - $DB->query($query); - } - - return $nbUnlinkedElmts; - } - - private static function deleteDocumentsToDate($userID, $className, $retentionPeriod, $allUser = false) { - global $DB; - if (!class_exists($className)) { - $errorMessage = sprintf( - __('The class %1$s can\'t be instanciate because not finded on GLPI.', 'rgpdtools'), - $className - ); - throw new \Exception($errorMessage); - } - $date = new DateTime(); - $date->sub(new DateInterval('P' . $retentionPeriod . 'M')); - - $document = new Document(); - $documentItem = new Document_Item(); - // recherche des éléments liés au user en bdd - $querySelect = "SELECT d1.* FROM " . $document->getTable() . " d1 " - . "INNER JOIN " . $documentItem->getTable() . " d2 ON d1.id = d2.documents_id " - . "WHERE d2.itemtype='" . $className . "' AND d2.date <= '" . $date->format('Y-m-d') . "' "; - if (!$allUser && $userID) { - $querySelect .= "AND d2.users_id=$userID "; - } - - $results = $DB->query($querySelect); - $nbdeletedElmts = $DB->numrows($results); - if ($nbdeletedElmts) { - // construction du tableau des ids - while ($row = $DB->fetchAssoc($results)) { - // delete file on server - $filepath = GLPI_DOC_DIR.'/'.$row['filepath']; - if (file_exists($filepath)) { - unlink($filepath); + private static function getExportablefields($className) { + $fields = []; + // id fields is empty, all fields are export + switch ($className) { + case 'Computer': + //$fields = ['id','name','serial','otherserial', 'contact', 'contact_num', 'comment','date_mod', 'date_creation']; + break; + case 'User': + // $fields = ['id', 'name','phone','phone2','mobile','email']; + break; + } + return $fields; + } + + private static function unlinkUserAssociateElementsToDate($userID, $className, $retentionPeriod, $allUser = false) { + + global $DB; + $className = str_replace(['ItilFollowup'], ['ITILFollowup'], $className); + + + if (!class_exists($className)) { + $errorMessage = sprintf( + __('The class %1$s can\'t be instanciate because not finded on GLPI.', 'rgpdtools'), + $className + ); + throw new \Exception($errorMessage); + } + $date = new DateTime(); + $date->sub(new DateInterval('P' . $retentionPeriod . 'M')); + $nbUnlinkedElmts = 0; + $queriesUpdate = []; + + $log = new Log(); + $object = new $className(); + //$object = new Computer(); // for test + // recherche des éléments liés au user en bdd dans la table des logs + $queryUpdateLog = "UPDATE " . $log->getTable() . " lg " + . "INNER JOIN " . $object->getTable() . " t1 ON t1.id = lg.items_id AND itemtype='" . $className . "' AND id_search_option=70 " + . "SET new_value = '  (0)' " + . "WHERE lg.date_mod <= '" . $date->format('Y-m-d') . "' "; + if (!$allUser) { + $queryUpdateLog .= " AND new_value LIKE '% (" . $userID . ")' "; + } + $queriesUpdate[] = $queryUpdateLog; + + switch ($className) { + case 'Ticket': + $ticketUser = new Ticket_User(); + $queriesUpdate[] = 'DELETE tu.* FROM ' . $ticketUser->getTable() . ' tu ' + . 'INNER JOIN ' . $object->getTable() . ' t1 ON tu.tickets_id= t1.id ' + . "WHERE tu.users_id=$userID AND t1.date_mod <= '" . $date->format('Y-m-d') . "'"; + break; + case 'Followup': + case 'Task': + $queriesUpdate[] = 'UPDATE ' . $object->getTable() . ' t1 ' + . "SET users_id=NULL WHERE t1.users_id=$userID AND t1.date_mod <= '" . $date->format('Y-m-d') . "'"; + $queriesUpdate[] = 'UPDATE ' . $object->getTable() . ' t1 ' + . "SET users_id_editor=NULL WHERE t1.users_id_editor=$userID AND t1.date_mod <= '" . $date->format('Y-m-d') . "'"; + break; + Default: + $queriesUpdate[] = 'UPDATE ' . $object->getTable() . ' t1 ' + . "SET users_id=NULL WHERE t1.users_id=$userID AND t1.date_mod <= '" . $date->format('Y-m-d') . "'"; + break; + } + foreach ($queriesUpdate as $queryDelete) { + $resultDelete = $DB->query($queryDelete); + if($resultDelete){ + $rowcountResult = $DB->query("SELECT ROW_COUNT() as DelRowCount"); + $row = $DB->fetchAssoc($rowcountResult); + $nbUnlinkedElmts += $row['DelRowCount']; } - // delete Document_Item into database - $queryDeleteDocumentItem = "DELETE FROM ".$documentItem->getTable()." WHERE documents_id=".$row['id']; - $DB->query($queryDeleteDocumentItem); - // delete Document into database - $queryDeleteDocument = "DELETE FROM ".$document->getTable()." WHERE id=".$row['id']; - $DB->query($queryDeleteDocument); - } - - } - - return $nbdeletedElmts; - } - - - private static function anonymizeUserLogActivity($userID, $retentionPeriod) { - global $DB; - - $date = new DateTime(); - $date->sub(new DateInterval('P' . $retentionPeriod . 'M')); - - $log = new Log(); - // delete logs wich user is at origin - $query = "DELETE FROM " . $log->getTable() . " WHERE user_name LIKE '% (" . $userID . ")' AND date_mod <= '" . $date->format('Y-m-d H:i:s') . "'"; - $DB->query($query); - - // anonymize logs wich are attch to the user - $query = "DELETE FROM " . $log->getTable() . " WHERE itemtype='User' AND items_id=" . $userID . " AND date_mod <= '" . $date->format('Y-m-d H:i:s') . "'"; - $DB->query($query); - - // anonymize logs containing friendlyname of the user in old_value or new_value - $user = new User(); - $user->getFromDB($userID); - $friendlyName = $user->getFriendlyName(); - $query = "UPDATE " . $log->getTable() . " SET old_value='  (0)' WHERE old_value LIKE '%" . $friendlyName . "%' AND date_mod <= '" . $date->format('Y-m-d H:i:s') . "'"; - $query = "UPDATE " . $log->getTable() . " SET new_value='  (0)' WHERE new_value LIKE '%" . $friendlyName . "%' AND date_mod <= '" . $date->format('Y-m-d H:i:s') . "'"; - $query = "UPDATE " . $log->getTable() . " SET new_value='  (0)' WHERE itemtype_link='User' AND new_value LIKE '% (" . $userID . ")' AND date_mod <= '" . $date->format('Y-m-d H:i:s') . "'"; - $query = "UPDATE " . $log->getTable() . " SET new_value='  (0)' WHERE itemtype_link='User' AND new_value LIKE '% (" . $userID . ")' AND date_mod <= '" . $date->format('Y-m-d H:i:s') . "'"; - } - - private static function getUserSoftwares($computersIds) { - global $DB; - $softwares = []; - if (count($computersIds)) { - $query = "SELECT `glpi_softwares`.`name` AS `softname`, `glpi_items_softwareversions`.`id`, `glpi_states`.`name` AS `state`, `glpi_softwareversions`.`id` AS `verid`, `glpi_softwareversions`.`softwares_id`, `glpi_softwareversions`.`name` AS `version`, `glpi_softwares`.`is_valid` AS `softvalid`, `glpi_items_softwareversions`.`date_install` AS `dateinstall` + } + + return $nbUnlinkedElmts; + } + + private static function deleteDocumentsToDate($userID, $className, $retentionPeriod, $allUser = false) { + global $DB; + if (!class_exists($className)) { + $errorMessage = sprintf( + __('The class %1$s can\'t be instanciate because not finded on GLPI.', 'rgpdtools'), + $className + ); + throw new \Exception($errorMessage); + } + $date = new DateTime(); + $date->sub(new DateInterval('P' . $retentionPeriod . 'M')); + + $document = new Document(); + $documentItem = new Document_Item(); + // recherche des éléments liés au user en bdd + $querySelect = "SELECT d1.* FROM " . $document->getTable() . " d1 " + . "INNER JOIN " . $documentItem->getTable() . " d2 ON d1.id = d2.documents_id " + . "WHERE d2.itemtype='" . $className . "' AND d2.date <= '" . $date->format('Y-m-d') . "' "; + if (!$allUser && $userID) { + $querySelect .= "AND d2.users_id=$userID "; + } + + $results = $DB->query($querySelect); + $nbdeletedElmts = $DB->numrows($results); + if ($nbdeletedElmts) { + // construction du tableau des ids + while ($row = $DB->fetchAssoc($results)) { + // delete file on server + $filepath = GLPI_DOC_DIR . '/' . $row['filepath']; + if (file_exists($filepath)) { + unlink($filepath); + } + // delete Document_Item into database + $queryDeleteDocumentItem = "DELETE FROM " . $documentItem->getTable() . " WHERE documents_id=" . $row['id']; + $DB->query($queryDeleteDocumentItem); + // delete Document into database + $queryDeleteDocument = "DELETE FROM " . $document->getTable() . " WHERE id=" . $row['id']; + $DB->query($queryDeleteDocument); + } + } + + return $nbdeletedElmts; + } + + private static function anonymizeUserLogActivity($userID, $retentionPeriod) { + global $DB; + + $date = new DateTime(); + $date->sub(new DateInterval('P' . $retentionPeriod . 'M')); + + $log = new Log(); + // delete logs wich user is at origin + $query = "DELETE FROM " . $log->getTable() . " WHERE user_name LIKE '% (" . $userID . ")' AND date_mod <= '" . $date->format('Y-m-d H:i:s') . "'"; + $DB->query($query); + + // anonymize logs wich are attch to the user + $query = "DELETE FROM " . $log->getTable() . " WHERE itemtype='User' AND items_id=" . $userID . " AND date_mod <= '" . $date->format('Y-m-d H:i:s') . "'"; + $DB->query($query); + + // anonymize logs containing friendlyname of the user in old_value or new_value + $user = new User(); + $user->getFromDB($userID); + $friendlyName = $user->getFriendlyName(); + $query = "UPDATE " . $log->getTable() . " SET old_value='  (0)' WHERE old_value LIKE '%" . $friendlyName . "%' AND date_mod <= '" . $date->format('Y-m-d H:i:s') . "'"; + $query = "UPDATE " . $log->getTable() . " SET new_value='  (0)' WHERE new_value LIKE '%" . $friendlyName . "%' AND date_mod <= '" . $date->format('Y-m-d H:i:s') . "'"; + $query = "UPDATE " . $log->getTable() . " SET new_value='  (0)' WHERE itemtype_link='User' AND new_value LIKE '% (" . $userID . ")' AND date_mod <= '" . $date->format('Y-m-d H:i:s') . "'"; + $query = "UPDATE " . $log->getTable() . " SET new_value='  (0)' WHERE itemtype_link='User' AND new_value LIKE '% (" . $userID . ")' AND date_mod <= '" . $date->format('Y-m-d H:i:s') . "'"; + } + + private static function getUserSoftwares($computersIds) { + global $DB; + $softwares = []; + if (count($computersIds)) { + $query = "SELECT `glpi_softwares`.`name` AS `softname`, `glpi_items_softwareversions`.`id`, `glpi_states`.`name` AS `state`, `glpi_softwareversions`.`id` AS `verid`, `glpi_softwareversions`.`softwares_id`, `glpi_softwareversions`.`name` AS `version`, `glpi_softwares`.`is_valid` AS `softvalid`, `glpi_items_softwareversions`.`date_install` AS `dateinstall` FROM `glpi_items_softwareversions` LEFT JOIN `glpi_softwareversions` ON (`glpi_items_softwareversions`.`softwareversions_id` = `glpi_softwareversions`.`id`) LEFT JOIN `glpi_states` ON (`glpi_softwareversions`.`states_id` = `glpi_states`.`id`) @@ -821,20 +843,20 @@ private static function getUserSoftwares($computersIds) { ORDER BY `softname`, `version`"; - $result = $DB->query($query); - while ($data = $DB->fetchAssoc($result)) { - $softwares[] = $data; - } - } + $result = $DB->query($query); + while ($data = $DB->fetchAssoc($result)) { + $softwares[] = $data; + } + } - return $softwares; - } + return $softwares; + } - private static function getUserSoftwareLicences($computersIds) { - global $DB; - $softwaresLicences = []; - if (count($computersIds)) { - $query = "SELECT tb.* + private static function getUserSoftwareLicences($computersIds) { + global $DB; + $softwaresLicences = []; + if (count($computersIds)) { + $query = "SELECT tb.* FROM `glpi_items_softwareversions` LEFT JOIN `glpi_softwareversions` ON (`glpi_items_softwareversions`.`softwareversions_id` = `glpi_softwareversions`.`id`) LEFT JOIN `glpi_states` ON (`glpi_softwareversions`.`states_id` = `glpi_states`.`id`) @@ -844,12 +866,12 @@ private static function getUserSoftwareLicences($computersIds) { ORDER BY tb.name"; - $result = $DB->query($query); - while ($data = $DB->fetchAssoc($result)) { - $softwaresLicences[] = $data; - } - } + $result = $DB->query($query); + while ($data = $DB->fetchAssoc($result)) { + $softwaresLicences[] = $data; + } + } - return $softwaresLicences; - } + return $softwaresLicences; + } } diff --git a/rgpdtools.xml b/rgpdtools.xml index 89dfebc..76a9b22 100644 --- a/rgpdtools.xml +++ b/rgpdtools.xml @@ -64,6 +64,11 @@ ~10.O https://github.com/Probesys/glpi-plugins-rgpdtools/releases/download/v1.1.2/rgpdtools-1.1.2.tbz + + 1.1.3 + ~10.O + https://github.com/Probesys/glpi-plugins-rgpdtools/releases/download/v1.1.3/rgpdtools-1.1.3.tbz + fr_FR diff --git a/setup.php b/setup.php index 6e08ea3..7c8a1b4 100644 --- a/setup.php +++ b/setup.php @@ -27,7 +27,7 @@ * --------------------------------------------------------------------- */ -define('PLUGIN_RGPDTOOLS_VERSION', '1.1.2'); +define('PLUGIN_RGPDTOOLS_VERSION', '1.1.3'); define('PLUGIN_RGPDTOOLS_GLPI_MIN_VERSION', '9.5'); define('PLUGIN_RGPDTOOLS_GLPI_MAX_VERSION', '11'); if (!defined("PLUGIN_RGPDTOOLS_DIR")) {