listevents.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <?php
  2. /* Copyright (C) 2004-2020 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2015 Bahfir Abbes <bafbes@gmail.com>
  5. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/admin/tools/listevents.php
  22. * \ingroup core
  23. * \brief List of security events
  24. */
  25. // Load Dolibarr environment
  26. require '../../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/events.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  29. if (!$user->admin) {
  30. accessforbidden();
  31. }
  32. $action = GETPOST('action', 'aZ09');
  33. $confirm = GETPOST('confirm', 'alpha');
  34. // Security check
  35. if ($user->socid > 0) {
  36. $action = '';
  37. $socid = $user->socid;
  38. }
  39. // Load translation files required by the page
  40. $langs->loadLangs(array("companies", "admin", "users", "other","withdrawals"));
  41. // Load variable for pagination
  42. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  43. $sortfield = GETPOST('sortfield', 'aZ09comma');
  44. $sortorder = GETPOST('sortorder', 'aZ09comma');
  45. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  46. if (empty($page) || $page == -1) {
  47. $page = 0;
  48. } // If $page is not defined, or '' or -1
  49. $offset = $limit * $page;
  50. $pageprev = $page - 1;
  51. $pagenext = $page + 1;
  52. if (!$sortfield) {
  53. $sortfield = "dateevent";
  54. }
  55. if (!$sortorder) {
  56. $sortorder = "DESC";
  57. }
  58. $search_code = GETPOST("search_code", "alpha");
  59. $search_ip = GETPOST("search_ip", "alpha");
  60. $search_user = GETPOST("search_user", "alpha");
  61. $search_desc = GETPOST("search_desc", "alpha");
  62. $search_ua = GETPOST("search_ua", "restricthtml");
  63. $search_prefix_session = GETPOST("search_prefix_session", "restricthtml");
  64. $optioncss = GETPOST("optioncss", "aZ"); // Option for the css output (always '' except when 'print')
  65. $now = dol_now();
  66. $nowarray = dol_getdate($now);
  67. if (GETPOST("date_startmonth", 'int') > 0) {
  68. $date_start = dol_mktime(0, 0, 0, GETPOST("date_startmonth", 'int'), GETPOST("date_startday", 'int'), GETPOST("date_startyear", 'int'), 'tzuserrel');
  69. } else {
  70. $date_start = '';
  71. }
  72. if (GETPOST("date_endmonth", 'int') > 0) {
  73. $date_end = dol_get_last_hour(dol_mktime(23, 59, 59, GETPOST("date_endmonth", 'int'), GETPOST("date_endday", 'int'), GETPOST("date_endyear", 'int'), 'tzuserrel'), 'tzuserrel');
  74. } else {
  75. $date_end = '';
  76. }
  77. // checks:if date_start>date_end then date_end=date_start + 24 hours
  78. if ($date_start !== '' && $date_end !== '' && $date_start > $date_end) {
  79. $date_end = $date_start + 86400;
  80. }
  81. if (!GETPOSTISSET('pageplusoneold') && !GETPOSTISSET('page') && $date_start === '') { // We define date_start and date_end
  82. $date_start = dol_get_first_day($nowarray['year'], $nowarray['mon'], 'tzuserrel');
  83. }
  84. if (!GETPOSTISSET('pageplusoneold') && !GETPOSTISSET('page') && $date_end === '') {
  85. $date_end = dol_get_last_day($nowarray['year'], $nowarray['mon'], 'tzuserrel');
  86. }
  87. // Set $date_startmonth...
  88. $date_startday = '';
  89. $date_startmonth = '';
  90. $date_startyear = '';
  91. $date_endday = '';
  92. $date_endmonth = '';
  93. $date_endyear = '';
  94. if ($date_start !== '') {
  95. $tmp = dol_getdate($date_start);
  96. $date_startday = $tmp['mday'];
  97. $date_startmonth = $tmp['mon'];
  98. $date_startyear = $tmp['year'];
  99. }
  100. if ($date_end !== '') {
  101. $tmp = dol_getdate($date_end);
  102. $date_endday = $tmp['mday'];
  103. $date_endmonth = $tmp['mon'];
  104. $date_endyear = $tmp['year'];
  105. }
  106. // Add prefix session
  107. $arrayfields = array(
  108. 'e.prefix_session' => array(
  109. 'label'=>'UserAgent',
  110. 'checked'=>(empty($conf->global->AUDIT_ENABLE_PREFIX_SESSION) ? 0 : 1),
  111. 'enabled'=>(empty($conf->global->AUDIT_ENABLE_PREFIX_SESSION) ? 0 : 1),
  112. 'position'=>110
  113. )
  114. );
  115. /*
  116. * Actions
  117. */
  118. $now = dol_now();
  119. // Purge search criteria
  120. if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers
  121. $date_start = '';
  122. $date_end = '';
  123. $date_startday = '';
  124. $date_endday = '';
  125. $date_startmonth = '';
  126. $date_endmonth = '';
  127. $date_startyear = '';
  128. $date_endyear = '';
  129. $search_code = '';
  130. $search_ip = '';
  131. $search_user = '';
  132. $search_desc = '';
  133. $search_ua = '';
  134. $search_prefix_session = '';
  135. }
  136. // Purge audit events
  137. if ($action == 'confirm_purge' && $confirm == 'yes' && $user->admin) {
  138. $error = 0;
  139. $db->begin();
  140. $securityevents = new Events($db);
  141. // Delete events
  142. $sql = "DELETE FROM ".MAIN_DB_PREFIX."events";
  143. $sql .= " WHERE entity = ".$conf->entity;
  144. dol_syslog("listevents purge", LOG_DEBUG);
  145. $resql = $db->query($sql);
  146. if (!$resql) {
  147. $error++;
  148. setEventMessages($db->lasterror(), null, 'errors');
  149. }
  150. // Add event purge
  151. $text = $langs->trans("SecurityEventsPurged");
  152. $securityevent = new Events($db);
  153. $securityevent->type = 'SECURITY_EVENTS_PURGE';
  154. $securityevent->dateevent = $now;
  155. $securityevent->description = $text;
  156. $result = $securityevent->create($user);
  157. if ($result > 0) {
  158. $db->commit();
  159. dol_syslog($text, LOG_WARNING);
  160. } else {
  161. $error++;
  162. dol_syslog($securityevent->error, LOG_ERR);
  163. $db->rollback();
  164. }
  165. }
  166. /*
  167. * View
  168. */
  169. $title = $langs->trans("Audit");
  170. llxHeader('', $title);
  171. $form = new Form($db);
  172. $userstatic = new User($db);
  173. $usefilter = 0;
  174. $sql = "SELECT e.rowid, e.type, e.ip, e.user_agent, e.dateevent,";
  175. $sql .= " e.fk_user, e.description, e.prefix_session,";
  176. $sql .= " u.login, u.admin, u.entity, u.firstname, u.lastname, u.statut as status";
  177. $sql .= " FROM ".MAIN_DB_PREFIX."events as e";
  178. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON u.rowid = e.fk_user";
  179. $sql .= " WHERE e.entity IN (".getEntity('event').")";
  180. if ($date_start !== '') {
  181. $sql .= " AND e.dateevent >= '".$db->idate($date_start)."'";
  182. }
  183. if ($date_end !== '' ) {
  184. $sql .= " AND e.dateevent <= '".$db->idate($date_end)."'";
  185. }
  186. if ($search_code) {
  187. $usefilter++; $sql .= natural_search("e.type", $search_code, 0);
  188. }
  189. if ($search_ip) {
  190. $usefilter++; $sql .= natural_search("e.ip", $search_ip, 0);
  191. }
  192. if ($search_user) {
  193. $usefilter++; $sql .= natural_search("u.login", $search_user, 0);
  194. }
  195. if ($search_desc) {
  196. $usefilter++; $sql .= natural_search("e.description", $search_desc, 0);
  197. }
  198. if ($search_ua) {
  199. $usefilter++; $sql .= natural_search("e.user_agent", $search_ua, 0);
  200. }
  201. if ($search_prefix_session) {
  202. $usefilter++; $sql .= natural_search("e.prefix_session", $search_prefix_session, 0);
  203. }
  204. $sql .= $db->order($sortfield, $sortorder);
  205. // Count total nb of records
  206. $nbtotalofrecords = '';
  207. /*if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
  208. {
  209. $result = $db->query($sql);
  210. $nbtotalofrecords = $db->num_rows($result);
  211. if (($page * $limit) > $nbtotalofrecords) // if total resultset is smaller then paging size (filtering), goto and load page 0
  212. {
  213. $page = 0;
  214. $offset = 0;
  215. }
  216. }*/
  217. $sql .= $db->plimit($limit + 1, $offset);
  218. $result = $db->query($sql);
  219. if ($result) {
  220. $num = $db->num_rows($result);
  221. $i = 0;
  222. $param = '';
  223. if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
  224. $param .= '&contextpage='.urlencode($contextpage);
  225. }
  226. if ($limit > 0 && $limit != $conf->liste_limit) {
  227. $param .= '&limit='.urlencode($limit);
  228. }
  229. if ($optioncss != '') {
  230. $param .= '&optioncss='.urlencode($optioncss);
  231. }
  232. if ($search_code) {
  233. $param .= '&search_code='.urlencode($search_code);
  234. }
  235. if ($search_ip) {
  236. $param .= '&search_ip='.urlencode($search_ip);
  237. }
  238. if ($search_user) {
  239. $param .= '&search_user='.urlencode($search_user);
  240. }
  241. if ($search_desc) {
  242. $param .= '&search_desc='.urlencode($search_desc);
  243. }
  244. if ($search_ua) {
  245. $param .= '&search_ua='.urlencode($search_ua);
  246. }
  247. if ($search_prefix_session) {
  248. $param .= '&search_prefix_session='.urlencode($search_prefix_session);
  249. }
  250. if ($date_startmonth) {
  251. $param .= "&date_startmonth=".urlencode($date_startmonth);
  252. }
  253. if ($date_startday) {
  254. $param .= "&date_startday=".urlencode($date_startday);
  255. }
  256. if ($date_startyear) {
  257. $param .= "&date_startyear=".urlencode($date_startyear);
  258. }
  259. if ($date_endmonth) {
  260. $param .= "&date_endmonth=".urlencode($date_endmonth);
  261. }
  262. if ($date_endday) {
  263. $param .= "&date_endday=".urlencode($date_endday);
  264. }
  265. if ($date_endyear) {
  266. $param .= "&date_endyear=".urlencode($date_endyear);
  267. }
  268. $center = '';
  269. if ($num) {
  270. $center = '<a class="butActionDelete small" href="'.$_SERVER["PHP_SELF"].'?action=purge">'.$langs->trans("Purge").'</a>';
  271. }
  272. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  273. print '<input type="hidden" name="token" value="'.newToken().'">';
  274. print_barre_liste($langs->trans("ListOfSecurityEvents"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $center, $num, $nbtotalofrecords, 'setup', 0, '', '', $limit);
  275. if ($action == 'purge') {
  276. $formquestion = array();
  277. print $form->formconfirm($_SERVER["PHP_SELF"].'?noparam=noparam', $langs->trans('PurgeAuditEvents'), $langs->trans('ConfirmPurgeAuditEvents'), 'confirm_purge', $formquestion, 'no', 1);
  278. }
  279. // Check some parameters
  280. // TODO Add a tab with this and other information
  281. /*
  282. global $dolibarr_main_prod, $dolibarr_nocsrfcheck;
  283. if (empty($dolibarr_main_prod)) {
  284. print $langs->trans("Warning").' dolibarr_main_prod = '.$dolibarr_main_prod;
  285. print ' '.img_warning($langs->trans('SwitchThisForABetterSecurity', 1)).'<br>';
  286. }
  287. if (!empty($dolibarr_nocsrfcheck)) {
  288. print $langs->trans("Warning").' dolibarr_nocsrfcheck = '.$dolibarr_nocsrfcheck;
  289. print ' '.img_warning($langs->trans('SwitchThisForABetterSecurity', 0)).'<br>';
  290. }
  291. */
  292. print '<div class="div-table-responsive">';
  293. print '<table class="liste centpercent">';
  294. // Fields title search
  295. print '<tr class="liste_titre">';
  296. print '<td class="liste_titre" width="15%">';
  297. print $form->selectDate($date_start === '' ? -1 : $date_start, 'date_start', 0, 0, 0, '', 1, 0, 0, '', '', '', '', 1, '', '', 'tzuserrel');
  298. print $form->selectDate($date_end === '' ? -1 : $date_end, 'date_end', 0, 0, 0, '', 1, 0, 0, '', '', '', '', 1, '', '', 'tzuserrel');
  299. print '</td>';
  300. print '<td class="liste_titre left">';
  301. print '<input class="flat maxwidth100" type="text" name="search_code" value="'.dol_escape_htmltag($search_code).'">';
  302. print '</td>';
  303. // IP
  304. print '<td class="liste_titre left">';
  305. print '<input class="flat maxwidth100" type="text" name="search_ip" value="'.dol_escape_htmltag($search_ip).'">';
  306. print '</td>';
  307. print '<td class="liste_titre left">';
  308. print '<input class="flat maxwidth100" type="text" name="search_user" value="'.dol_escape_htmltag($search_user).'">';
  309. print '</td>';
  310. print '<td class="liste_titre left">';
  311. //print '<input class="flat maxwidth100" type="text" size="10" name="search_desc" value="'.$search_desc.'">';
  312. print '</td>';
  313. if (!empty($arrayfields['e.user_agent']['checked'])) {
  314. print '<td class="liste_titre left">';
  315. print '<input class="flat maxwidth100" type="text" name="search_ua" value="'.dol_escape_htmltag($search_ua).'">';
  316. print '</td>';
  317. }
  318. if (!empty($arrayfields['e.prefix_session']['checked'])) {
  319. print '<td class="liste_titre left">';
  320. print '<input class="flat maxwidth100" type="text" name="search_prefix_session" value="'.dol_escape_htmltag($search_prefix_session).'">';
  321. print '</td>';
  322. }
  323. print '<td class="liste_titre maxwidthsearch">';
  324. $searchpicto = $form->showFilterAndCheckAddButtons(0);
  325. print $searchpicto;
  326. print '</td>';
  327. print "</tr>\n";
  328. print '<tr class="liste_titre">';
  329. print_liste_field_titre("Date", $_SERVER["PHP_SELF"], "e.dateevent", "", $param, '', $sortfield, $sortorder);
  330. print_liste_field_titre("Code", $_SERVER["PHP_SELF"], "e.type", "", $param, '', $sortfield, $sortorder);
  331. print_liste_field_titre("IP", $_SERVER["PHP_SELF"], "e.ip", "", $param, '', $sortfield, $sortorder);
  332. print_liste_field_titre("User", $_SERVER["PHP_SELF"], "u.login", "", $param, '', $sortfield, $sortorder);
  333. print_liste_field_titre("Description", $_SERVER["PHP_SELF"], "e.description", "", $param, '', $sortfield, $sortorder);
  334. if (!empty($arrayfields['e.user_agent']['checked'])) {
  335. print_liste_field_titre("UserAgent", $_SERVER["PHP_SELF"], "e.user_agent", "", $param, '', $sortfield, $sortorder);
  336. }
  337. if (!empty($arrayfields['e.prefix_session']['checked'])) {
  338. print_liste_field_titre("SuffixSessionName", $_SERVER["PHP_SELF"], "e.prefix_session", "", $param, '', $sortfield, $sortorder);
  339. }
  340. print_liste_field_titre('');
  341. print "</tr>\n";
  342. while ($i < min($num, $limit)) {
  343. $obj = $db->fetch_object($result);
  344. print '<tr class="oddeven">';
  345. // Date
  346. print '<td class="nowrap left">'.dol_print_date($db->jdate($obj->dateevent), '%Y-%m-%d %H:%M:%S', 'tzuserrel').'</td>';
  347. // Code
  348. print '<td>'.$obj->type.'</td>';
  349. // IP
  350. print '<td class="nowrap">';
  351. print dol_print_ip($obj->ip);
  352. print '</td>';
  353. // Login
  354. print '<td class="nowrap">';
  355. if ($obj->fk_user) {
  356. $userstatic->id = $obj->fk_user;
  357. $userstatic->login = $obj->login;
  358. $userstatic->admin = $obj->admin;
  359. $userstatic->entity = $obj->entity;
  360. $userstatic->status = $obj->status;
  361. print $userstatic->getLoginUrl(1);
  362. if (isModEnabled('multicompany') && $userstatic->admin && !$userstatic->entity) {
  363. print img_picto($langs->trans("SuperAdministrator"), 'redstar', 'class="valignmiddle paddingleft"');
  364. } elseif ($userstatic->admin) {
  365. print img_picto($langs->trans("Administrator"), 'star', 'class="valignmiddle paddingleft"');
  366. }
  367. } else {
  368. print '&nbsp;';
  369. }
  370. print '</td>';
  371. // Description
  372. print '<td>';
  373. $text = $langs->trans($obj->description);
  374. $reg = array();
  375. if (preg_match('/\((.*)\)(.*)/i', $obj->description, $reg)) {
  376. $val = explode(',', $reg[1]);
  377. $text = $langs->trans($val[0], isset($val[1]) ? $val[1] : '', isset($val[2]) ? $val[2] : '', isset($val[3]) ? $val[3] : '', isset($val[4]) ? $val[4] : '');
  378. if (!empty($reg[2])) {
  379. $text .= $reg[2];
  380. }
  381. }
  382. print dol_escape_htmltag($text);
  383. print '</td>';
  384. if (!empty($arrayfields['e.user_agent']['checked'])) {
  385. // User agent
  386. print '<td>';
  387. print $obj->user_agent;
  388. print '</td>';
  389. }
  390. if (!empty($arrayfields['e.prefix_session']['checked'])) {
  391. // User agent
  392. print '<td>';
  393. print $obj->prefix_session;
  394. print '</td>';
  395. }
  396. // More informations
  397. print '<td class="right">';
  398. $htmltext = '<b>'.$langs->trans("UserAgent").'</b>: '.($obj->user_agent ? dol_string_nohtmltag($obj->user_agent) : $langs->trans("Unknown"));
  399. $htmltext .= '<br><b>'.$langs->trans("SuffixSessionName").' (DOLSESSID_...)</b>: '.($obj->prefix_session ? dol_string_nohtmltag($obj->prefix_session) : $langs->trans("Unknown"));
  400. print $form->textwithpicto('', $htmltext);
  401. print '</td>';
  402. print "</tr>\n";
  403. $i++;
  404. }
  405. if ($num == 0) {
  406. if ($usefilter) {
  407. print '<tr><td colspan="7"><span class="opacitymedium">'.$langs->trans("NoEventFoundWithCriteria").'</span></td></tr>';
  408. } else {
  409. print '<tr><td colspan="7"><span class="opacitymedium">'.$langs->trans("NoEventOrNoAuditSetup").'</span></td></tr>';
  410. }
  411. }
  412. print "</table>";
  413. print "</div>";
  414. print "</form>";
  415. $db->free($result);
  416. } else {
  417. dol_print_error($db);
  418. }
  419. // End of page
  420. llxFooter();
  421. $db->close();