check_notifications.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('NOCSRFCHECK')) {
  20. define('NOCSRFCHECK', '1');
  21. }
  22. if (!defined('NOTOKENRENEWAL')) {
  23. define('NOTOKENRENEWAL', '1'); // Do not roll the Anti CSRF token (used if MAIN_SECURITY_CSRF_WITH_TOKEN is on)
  24. }
  25. if (!defined('NOREQUIREMENU')) {
  26. define('NOREQUIREMENU', '1');
  27. }
  28. if (!defined('NOREQUIREHTML')) {
  29. define('NOREQUIREHTML', '1');
  30. }
  31. if (!defined('NOREQUIREAJAX')) {
  32. define('NOREQUIREAJAX', '1');
  33. }
  34. if (!defined('NOREQUIRESOC')) {
  35. define('NOREQUIRESOC', '1');
  36. }
  37. if (!defined('NOREQUIRETRAN')) {
  38. define('NOREQUIRETRAN', '1');
  39. }
  40. //if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER', '1'); // Do not load object $user
  41. //if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1'); // Do not load object $mysoc
  42. //if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN', '1'); // Do not load object $langs
  43. //if (! defined('NOSCANGETFORINJECTION')) define('NOSCANGETFORINJECTION', '1'); // Do not check injection attack on GET parameters
  44. //if (! defined('NOSCANPOSTFORINJECTION')) define('NOSCANPOSTFORINJECTION', '1'); // Do not check injection attack on POST parameters
  45. //if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK', '1'); // Do not check CSRF attack (test on referer + on token if option MAIN_SECURITY_CSRF_WITH_TOKEN is on).
  46. //if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', '1'); // Do not roll the Anti CSRF token (used if MAIN_SECURITY_CSRF_WITH_TOKEN is on)
  47. //if (! defined('NOSTYLECHECK')) define('NOSTYLECHECK', '1'); // Do not check style html tag into posted data
  48. //if (! defined('NOIPCHECK')) define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
  49. //if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
  50. //if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
  51. //if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
  52. //if (! defined("NOLOGIN")) define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
  53. //if (! defined("MAIN_LANG_DEFAULT")) define('MAIN_LANG_DEFAULT', 'auto'); // Force lang to a particular value
  54. //if (! defined("MAIN_AUTHENTICATION_MODE")) define('MAIN_AUTHENTICATION_MODE', 'aloginmodule'); // Force authentication handler
  55. //if (! defined("NOREDIRECTBYMAINTOLOGIN")) define('NOREDIRECTBYMAINTOLOGIN', '1'); // The main.inc.php does not make a redirect if not logged, instead show simple error message
  56. //if (! defined("XFRAMEOPTIONS_ALLOWALL")) define('XFRAMEOPTIONS_ALLOWALL', '1'); // Do not add the HTTP header 'X-Frame-Options: SAMEORIGIN' but 'X-Frame-Options: ALLOWALL'
  57. require '../../main.inc.php';
  58. //$time = (int) GETPOST('time', 'int'); // Use the time parameter that is always increased by time_update, even if call is late
  59. $time = dol_now();
  60. $action = GETPOST('action', 'aZ09');
  61. $listofreminderids = GETPOST('listofreminderids', 'aZ09');
  62. /*
  63. * Actions
  64. */
  65. if ($action == 'stopreminder') {
  66. dol_syslog("Clear notification for listofreminderids=".$listofreminderids);
  67. $listofreminderid = GETPOST('listofreminderids', 'intcomma');
  68. // Set the reminder as done
  69. $sql = 'UPDATE '.MAIN_DB_PREFIX.'actioncomm_reminder SET status = 1';
  70. $sql .= ' WHERE status = 0 AND rowid IN ('.$db->sanitize($db->escape($listofreminderid)).')';
  71. $sql .= ' AND fk_user = '.((int) $user->id).' AND entity = '.((int) $conf->entity);
  72. $resql = $db->query($sql);
  73. if (!$resql) {
  74. dol_print_error($db);
  75. }
  76. //}
  77. include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  78. // Clean database
  79. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'actioncomm_reminder';
  80. $sql .= " WHERE dateremind < '".$db->idate(dol_time_plus_duree(dol_now(), -1, 'm'))."'";
  81. $resql = $db->query($sql);
  82. if (!$resql) {
  83. dol_print_error($db);
  84. }
  85. exit;
  86. }
  87. /*
  88. * View
  89. */
  90. top_httphead('application/json');
  91. global $user, $db, $langs, $conf;
  92. $eventfound = array();
  93. //Uncomment this to force a test
  94. //$eventfound[]=array('type'=>'agenda', 'id'=>1, 'tipo'=>'eee', 'location'=>'aaa');
  95. //dol_syslog('time='.$time.' $_SESSION[auto_ck_events_not_before]='.$_SESSION['auto_check_events_not_before']);
  96. // TODO Try to make a solution with only a javascript timer that is easier. Difficulty is to avoid notification twice when several tabs are opened.
  97. // This need to extend period to be sure to not miss and save in session what we notified to avoid duplicate.
  98. if (empty($_SESSION['auto_check_events_not_before']) || $time >= $_SESSION['auto_check_events_not_before'] || GETPOST('forcechecknow', 'int')) {
  99. /*$time_update = (int) $conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY; // Always defined
  100. if (!empty($_SESSION['auto_check_events_not_before']))
  101. {
  102. // We start scan from the not before so if two tabs were opend at differents seconds and we close one (so the js timer),
  103. // then we are not losing periods
  104. $starttime = $_SESSION['auto_check_events_not_before'];
  105. // Protection to avoid too long sessions
  106. if ($starttime < ($time - (int) $conf->global->MAIN_SESSION_TIMEOUT))
  107. {
  108. dol_syslog("We ask to check browser notification on a too large period. We fix this with current date.");
  109. $starttime = $time;
  110. }
  111. } else {
  112. $starttime = $time;
  113. }
  114. $_SESSION['auto_check_events_not_before'] = $time + $time_update;
  115. */
  116. // Force save of the session change we did.
  117. // WARNING: Any change in sessions after that will not be saved !
  118. session_write_close();
  119. require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
  120. dol_syslog('NEW $_SESSION[auto_check_events_not_before]='.(empty($_SESSION['auto_check_events_not_before']) ? '' : $_SESSION['auto_check_events_not_before']));
  121. $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';
  122. $sql .= ' FROM '.MAIN_DB_PREFIX.'actioncomm as a';
  123. if (!empty($user->conf->MAIN_USER_WANT_ALL_EVENTS_NOTIFICATIONS)) {
  124. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'actioncomm_reminder as ar ON a.id = ar.fk_actioncomm AND ar.fk_user = '.((int) $user->id);
  125. $sql .= ' WHERE a.code <> "AC_OTH_AUTO"';
  126. $sql .= ' AND (';
  127. $sql .= " (ar.typeremind = 'browser' AND ar.dateremind < '".$db->idate(dol_now())."' AND ar.status = 0 AND ar.entity = ".$conf->entity;
  128. $sql .= ' )';
  129. } else {
  130. $sql .= ' JOIN '.MAIN_DB_PREFIX.'actioncomm_reminder as ar ON a.id = ar.fk_actioncomm AND ar.fk_user = '.((int) $user->id);
  131. $sql .= " AND ar.typeremind = 'browser' AND ar.dateremind < '".$db->idate(dol_now())."' AND ar.status = 0 AND ar.entity = ".$conf->entity;
  132. }
  133. $sql .= $db->order('datep', 'ASC');
  134. $sql .= ' LIMIT 10'; // Avoid too many notification at once
  135. $resql = $db->query($sql);
  136. if ($resql) {
  137. while ($obj = $db->fetch_object($resql)) {
  138. // Message must be formated and translated to be used with javascript directly
  139. $event = array();
  140. $event['type'] = 'agenda';
  141. $event['id_reminder'] = $obj->id_reminder;
  142. $event['id_agenda'] = $obj->id_agenda;
  143. $event['id_user'] = $obj->id_user_reminder;
  144. $event['code'] = $obj->code;
  145. $event['label'] = $obj->label;
  146. $event['location'] = $obj->location;
  147. $event['reminder_date_formated'] = dol_print_date($db->jdate($obj->dateremind), 'standard');
  148. $event['event_date_start_formated'] = dol_print_date($db->jdate($obj->datep), 'standard');
  149. $eventfound[$obj->id_agenda] = $event;
  150. }
  151. } else {
  152. dol_syslog("Error sql = ".$db->lasterror(), LOG_ERR);
  153. }
  154. }
  155. print json_encode(array('pastreminders'=>$eventfound, 'nextreminder'=>''));