check_notifications.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /* Copyright (C) 2016 Sergio Sanchis <sergiosanchis@hotmail.com>
  3. * Copyright (C) 2017 Juanjo Menent <jmenent@2byte.es>
  4. * Copyright (C) 2019 Frédéric France <frederic.france@netlogic.fr>
  5. * Copyright (C) 2023 Laurent Destailleur <eldy@users.sourceforge.net>
  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. if (!defined('NOTOKENRENEWAL')) {
  21. define('NOTOKENRENEWAL', '1'); // Do not roll the Anti CSRF token (used if MAIN_SECURITY_CSRF_WITH_TOKEN is on)
  22. }
  23. if (!defined('NOREQUIREMENU')) {
  24. define('NOREQUIREMENU', '1');
  25. }
  26. if (!defined('NOREQUIREHTML')) {
  27. define('NOREQUIREHTML', '1');
  28. }
  29. if (!defined('NOREQUIREAJAX')) {
  30. define('NOREQUIREAJAX', '1');
  31. }
  32. if (!defined('NOREQUIRESOC')) {
  33. define('NOREQUIRESOC', '1');
  34. }
  35. if (!defined('NOREQUIRETRAN')) {
  36. define('NOREQUIRETRAN', '1');
  37. }
  38. // Load Dolibarr environment
  39. require '../../main.inc.php';
  40. //$time = (int) GETPOST('time', 'int'); // Use the time parameter that is always increased by time_update, even if call is late
  41. $time = dol_now();
  42. $action = GETPOST('action', 'aZ09');
  43. $listofreminderids = GETPOST('listofreminderids', 'aZ09');
  44. // Security check
  45. // No permission check at top, but action later are all done with a test on $user->id.
  46. /*
  47. * Actions
  48. */
  49. if ($action == 'stopreminder') {
  50. dol_syslog("Clear notification for listofreminderids=".$listofreminderids);
  51. $listofreminderid = GETPOST('listofreminderids', 'intcomma');
  52. // Set the reminder as done
  53. $sql = 'UPDATE '.MAIN_DB_PREFIX.'actioncomm_reminder SET status = 1';
  54. $sql .= ' WHERE status = 0 AND rowid IN ('.$db->sanitize($db->escape($listofreminderid)).')';
  55. $sql .= ' AND fk_user = '.((int) $user->id).' AND entity = '.((int) $conf->entity);
  56. $resql = $db->query($sql);
  57. if (!$resql) {
  58. dol_print_error($db);
  59. }
  60. //}
  61. include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  62. // Clean database
  63. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'actioncomm_reminder';
  64. $sql .= " WHERE dateremind < '".$db->idate(dol_time_plus_duree(dol_now(), -1, 'm'))."'";
  65. $sql .= " AND fk_user = ".((int) $user->id).' AND entity = '.((int) $conf->entity);
  66. $resql = $db->query($sql);
  67. if (!$resql) {
  68. dol_print_error($db);
  69. }
  70. exit;
  71. }
  72. /*
  73. * View
  74. */
  75. top_httphead('application/json');
  76. global $user, $db, $langs, $conf;
  77. $eventfound = array();
  78. //Uncomment this to force a test
  79. //$eventfound[]=array('type'=>'agenda', 'id'=>1, 'tipo'=>'eee', 'location'=>'aaa');
  80. // TODO Remove use of $_SESSION['auto_check_events_not_before']. Seems not used.
  81. if (empty($_SESSION['auto_check_events_not_before']) || $time >= $_SESSION['auto_check_events_not_before'] || GETPOST('forcechecknow', 'int')) {
  82. /*$time_update = (int) $conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY; // Always defined
  83. if (!empty($_SESSION['auto_check_events_not_before']))
  84. {
  85. // We start scan from the not before so if two tabs were opend at differents seconds and we close one (so the js timer),
  86. // then we are not losing periods
  87. $starttime = $_SESSION['auto_check_events_not_before'];
  88. // Protection to avoid too long sessions
  89. if ($starttime < ($time - (int) $conf->global->MAIN_SESSION_TIMEOUT))
  90. {
  91. dol_syslog("We ask to check browser notification on a too large period. We fix this with current date.");
  92. $starttime = $time;
  93. }
  94. } else {
  95. $starttime = $time;
  96. }
  97. $_SESSION['auto_check_events_not_before'] = $time + $time_update;
  98. */
  99. // Force save of the session change we did.
  100. // WARNING: Any change in sessions after that will not be saved !
  101. session_write_close();
  102. require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
  103. //dol_syslog('$_SESSION[auto_check_events_not_before]='.(empty($_SESSION['auto_check_events_not_before']) ? '' : $_SESSION['auto_check_events_not_before']));
  104. dol_syslog('dolnotif_nb_test_for_page='.GETPOST('dolnotif_nb_test_for_page'));
  105. $sql = 'SELECT a.id as id_agenda, a.code, a.datep, a.label, a.location, ar.rowid as id_reminder, ar.dateremind, ar.fk_user as id_user_reminder';
  106. $sql .= ' FROM '.MAIN_DB_PREFIX.'actioncomm as a';
  107. $sql .= ' INNER JOIN '.MAIN_DB_PREFIX.'actioncomm_reminder as ar ON a.id = ar.fk_actioncomm AND ar.fk_user = '.((int) $user->id);
  108. $sql .= " AND ar.typeremind = 'browser' AND ar.dateremind < '".$db->idate(dol_now())."'";
  109. $sql .= " AND ar.status = 0";
  110. $sql .= " AND ar.entity = ".((int) $conf->entity); // No sharing of entity for alerts
  111. $sql .= $db->order('datep', 'ASC');
  112. $sql .= $db->plimit(10); // Avoid too many notification at once
  113. $resql = $db->query($sql);
  114. if ($resql) {
  115. while ($obj = $db->fetch_object($resql)) {
  116. // Message must be formated and translated to be used with javascript directly
  117. $event = array();
  118. $event['type'] = 'agenda';
  119. $event['id_reminder'] = $obj->id_reminder;
  120. $event['id_agenda'] = $obj->id_agenda;
  121. $event['id_user'] = $obj->id_user_reminder;
  122. $event['code'] = $obj->code;
  123. $event['label'] = $obj->label;
  124. $event['location'] = $obj->location;
  125. $event['reminder_date_formated_tzserver'] = dol_print_date($db->jdate($obj->dateremind), 'standard', 'tzserver');
  126. $event['event_date_start_formated_tzserver'] = dol_print_date($db->jdate($obj->datep), 'standard', 'tzserver');
  127. $event['reminder_date_formated'] = dol_print_date($db->jdate($obj->dateremind), 'standard', 'tzuser');
  128. $event['event_date_start_formated'] = dol_print_date($db->jdate($obj->datep), 'standard', 'tzuser');
  129. $eventfound[$obj->id_agenda] = $event;
  130. }
  131. } else {
  132. dol_syslog("Error sql = ".$db->lasterror(), LOG_ERR);
  133. }
  134. }
  135. print json_encode(array('pastreminders'=>$eventfound, 'nextreminder'=>''));