dolibarr  17.0.4
lib_notification.js.php
1 <?php
2 /* Copyright (C) 2016 Sergio Sanchis <sergiosanchis@hotmail.com>
3  * Copyright (C) 2017 Juanjo Menent <jmenent@2byte.es>
4  * Copyright (C) 2020 Destailleur Laurent <eldy@users.sourceforge.net>
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  * Library javascript to enable Browser notifications
20  */
21 
22 if (!defined('NOREQUIREUSER')) {
23  define('NOREQUIREUSER', '1');
24 }
25 if (!defined('NOREQUIRESOC')) {
26  define('NOREQUIRESOC', '1');
27 }
28 if (!defined('NOCSRFCHECK')) {
29  define('NOCSRFCHECK', 1);
30 }
31 if (!defined('NOTOKENRENEWAL')) {
32  define('NOTOKENRENEWAL', 1);
33 }
34 if (!defined('NOLOGIN')) {
35  define('NOLOGIN', 1);
36 }
37 if (!defined('NOREQUIREMENU')) {
38  define('NOREQUIREMENU', 1);
39 }
40 if (!defined('NOREQUIREHTML')) {
41  define('NOREQUIREHTML', 1);
42 }
43 
44 session_cache_limiter('public');
45 
46 require_once '../../main.inc.php';
47 
48 
49 /*
50  * View
51  */
52 
53 top_httphead('text/javascript; charset=UTF-8');
54 // Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
55 if (empty($dolibarr_nocache)) {
56  header('Cache-Control: max-age=10800, public, must-revalidate');
57 } else {
58  header('Cache-Control: no-cache');
59 }
60 
61 
62 print "jQuery(document).ready(function () {\n";
63 
64 //print " console.log('referrer=".dol_escape_js($_SERVER['HTTP_REFERER'])."');\n";
65 
66 print ' var nowtime = Date.now();';
67 print ' var time_auto_update = '.max(1, getDolGlobalInt('MAIN_BROWSER_NOTIFICATION_FREQUENCY')).';'."\n"; // Always defined
68 print ' var time_js_next_test;'."\n";
69 print ' var dolnotif_nb_test_for_page = 0;'."\n";
70 print ' var dolnotif_idinterval = null;'."\n";
71 ?>
72 
73 /* Check if Notification is supported */
74 if ("Notification" in window) {
75  /* Check if permission ok */
76  if (Notification.permission !== "granted") {
77  console.log("Ask Notification.permission");
78  Notification.requestPermission()
79  }
80 
81  /* Launch timer */
82 
83  // We set a delay before launching first test so next check will arrive after the time_auto_update compared to previous one.
84  //var time_first_execution = (time_auto_update + (time_js_next_test - nowtime)) * 1000; //need milliseconds
85  var time_first_execution = <?php echo max(3, empty($conf->global->MAIN_BROWSER_NOTIFICATION_CHECK_FIRST_EXECUTION) ? 0 : $conf->global->MAIN_BROWSER_NOTIFICATION_CHECK_FIRST_EXECUTION); ?>;
86 
87  setTimeout(first_execution, time_first_execution * 1000); // Launch a first execution after a time_first_execution delay
88  time_js_next_test = nowtime + time_first_execution;
89  console.log("Launch browser notif check: setTimeout is set to launch 'first_execution' function after a wait of time_first_execution="+time_first_execution+". nowtime (time php page generation) = "+nowtime+" time_js_next_check = "+time_js_next_test);
90 } else {
91  console.log("This browser in this context does not support Notification.");
92 }
93 
94 
95 function first_execution() {
96  console.log("Call first_execution of check_events()");
97  result = check_events(); //one check before setting the new time for other checks
98  if (result > 0) {
99  console.log("check_events() is scheduled as a repeated task with a time_auto_update = MAIN_BROWSER_NOTIFICATION_FREQUENCY = "+time_auto_update+"s");
100  dolnotif_idinterval = setInterval(check_events, time_auto_update * 1000); // Set new time to run next check events. time_auto_update=nb of seconds
101  }
102 }
103 
104 function check_events() {
105  var result = 0;
106  dolnotif_nb_test_for_page += 1;
107 
108  if (Notification.permission === "granted") {
109  var currentToken = 'notrequired';
110  const allMeta = document.getElementsByTagName("meta");
111  for (let i = 0; i < allMeta.length; i++) {
112  if (allMeta[i].getAttribute("name") == 'anti-csrf-currenttoken') {
113  currentToken = allMeta[i].getAttribute('content');
114  console.log("currentToken in page = "+currentToken);
115  }
116  }
117  time_js_next_test += time_auto_update;
118 
119  console.log("Call ajax to check events with time_js_next_test = "+time_js_next_test+" dolnotif_nb_test_for_page="+dolnotif_nb_test_for_page);
120 
121  $.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php'; ?>", {
122  type: "post", // Usually post or get
123  async: true,
124  data: { time_js_next_test: time_js_next_test, forcechecknow: 1, token: currentToken },
125  dataType: "json",
126  success: function (result) {
127  //console.log(result);
128  var arrayofpastreminders = Object.values(result.pastreminders);
129  if (arrayofpastreminders && arrayofpastreminders.length > 0) {
130  console.log("Retrieved "+arrayofpastreminders.length+" reminders to do.");
131  var audio = null;
132  <?php
133  if (!empty($conf->global->AGENDA_REMINDER_BROWSER_SOUND)) {
134  print 'audio = new Audio(\''.DOL_URL_ROOT.'/theme/common/sound/notification_agenda.wav\');';
135  }
136  ?>
137  var listofreminderids = '';
138  var noti = []
139 
140  $.each(arrayofpastreminders, function (index, value) {
141  console.log(value);
142  var url = "notdefined";
143  var title = "Not defined";
144  var body = value.label;
145  if (value.type == 'agenda' && value.location != null && value.location != '') {
146  body += '\n' + value.location;
147  }
148 
149  if (value.type == 'agenda' && (value.event_date_start_formated != null || value.event_date_start_formated['event_date_start'] != '')) {
150  body += '\n' + value.event_date_start_formated;
151  }
152 
153  if (value.type == 'agenda')
154  {
155  url = '<?php print DOL_URL_ROOT.'/comm/action/card.php?id='; ?>' + value.id_agenda;
156  title = '<?php print dol_escape_js($langs->transnoentities('EventReminder')) ?>';
157  }
158  var extra = {
159  icon: '<?php print DOL_URL_ROOT.'/theme/common/bell.png'; ?>',
160  //image: '<?php print DOL_URL_ROOT.'/theme/common/bell.png'; ?>',
161  body: body,
162  tag: value.id_agenda,
163  requireInteraction: true
164  };
165 
166  // We release the notify
167  console.log("Send notification on browser");
168  noti[index] = new Notification(title, extra);
169  if (index==0 && audio)
170  {
171  audio.play();
172  }
173 
174  if (noti[index]) {
175  noti[index].onclick = function (event) {
176  console.log("A click on notification on browser has been done");
177  event.preventDefault(); // prevent the browser from focusing the Notification's tab
178  window.focus();
179  window.open(url, '_blank');
180  noti[index].close();
181  };
182 
183  listofreminderids = (listofreminderids == '' ? '' : listofreminderids + ',') + value.id_reminder
184  }
185  });
186 
187  // Update status of all notifications we sent on browser (listofreminderids)
188  console.log("Flag notification as done for listofreminderids="+listofreminderids);
189  $.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php?action=stopreminder&listofreminderids='; ?>"+listofreminderids, {
190  type: "POST", // Usually post or get
191  async: true,
192  data: { time_js_next_test: time_js_next_test, token: currentToken }
193  });
194  } else {
195  console.log("No remind to do found, next search at "+time_js_next_test);
196  }
197  }
198  });
199 
200  result = 1;
201  } else {
202  console.log("Cancel check_events() with dolnotif_nb_test_for_page="+dolnotif_nb_test_for_page+". Check is useless because javascript Notification.permission is "+Notification.permission+" (blocked manualy or web site is not https).");
203 
204  result = 2; // We return a positive so the repeated check will done even if authroization is not yet allowed may be after this check)
205  }
206 
207  if (dolnotif_nb_test_for_page >= 5) {
208  console.log("We did "+dolnotif_nb_test_for_page+" consecutive test on this page. We stop checking now from here by clearing dolnotif_idinterval="+dolnotif_idinterval);
209  clearInterval(dolnotif_idinterval);
210  }
211 
212  return result;
213 }
214 <?php
215 
216 print "\n";
217 print '})'."\n";
currentToken()
Return the value of token currently saved into session with name 'token'.
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
if(!defined( 'CSRFCHECK_WITH_TOKEN'))
if(!defined('NOREQUIREMENU')) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1440
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119