dolibarr  16.0.5
check_notifications.php
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 
20 if (!defined('NOCSRFCHECK')) {
21  define('NOCSRFCHECK', '1');
22 }
23 if (!defined('NOTOKENRENEWAL')) {
24  define('NOTOKENRENEWAL', '1'); // Do not roll the Anti CSRF token (used if MAIN_SECURITY_CSRF_WITH_TOKEN is on)
25 }
26 if (!defined('NOREQUIREMENU')) {
27  define('NOREQUIREMENU', '1');
28 }
29 if (!defined('NOREQUIREHTML')) {
30  define('NOREQUIREHTML', '1');
31 }
32 if (!defined('NOREQUIREAJAX')) {
33  define('NOREQUIREAJAX', '1');
34 }
35 if (!defined('NOREQUIRESOC')) {
36  define('NOREQUIRESOC', '1');
37 }
38 if (!defined('NOREQUIRETRAN')) {
39  define('NOREQUIRETRAN', '1');
40 }
41 
42 //if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER', '1'); // Do not load object $user
43 //if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1'); // Do not load object $mysoc
44 //if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN', '1'); // Do not load object $langs
45 //if (! defined('NOSCANGETFORINJECTION')) define('NOSCANGETFORINJECTION', '1'); // Do not check injection attack on GET parameters
46 //if (! defined('NOSCANPOSTFORINJECTION')) define('NOSCANPOSTFORINJECTION', '1'); // Do not check injection attack on POST parameters
47 //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).
48 //if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', '1'); // Do not roll the Anti CSRF token (used if MAIN_SECURITY_CSRF_WITH_TOKEN is on)
49 //if (! defined('NOSTYLECHECK')) define('NOSTYLECHECK', '1'); // Do not check style html tag into posted data
50 //if (! defined('NOIPCHECK')) define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
51 //if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
52 //if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
53 //if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
54 //if (! defined("NOLOGIN")) define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
55 //if (! defined("MAIN_LANG_DEFAULT")) define('MAIN_LANG_DEFAULT', 'auto'); // Force lang to a particular value
56 //if (! defined("MAIN_AUTHENTICATION_MODE")) define('MAIN_AUTHENTICATION_MODE', 'aloginmodule'); // Force authentication handler
57 //if (! defined("NOREDIRECTBYMAINTOLOGIN")) define('NOREDIRECTBYMAINTOLOGIN', '1'); // The main.inc.php does not make a redirect if not logged, instead show simple error message
58 //if (! defined("XFRAMEOPTIONS_ALLOWALL")) define('XFRAMEOPTIONS_ALLOWALL', '1'); // Do not add the HTTP header 'X-Frame-Options: SAMEORIGIN' but 'X-Frame-Options: ALLOWALL'
59 
60 
61 require '../../main.inc.php';
62 
63 //$time = (int) GETPOST('time', 'int'); // Use the time parameter that is always increased by time_update, even if call is late
64 $time = dol_now();
65 $action = GETPOST('action', 'aZ09');
66 $listofreminderids = GETPOST('listofreminderids', 'aZ09');
67 
68 
69 /*
70  * Actions
71  */
72 
73 if ($action == 'stopreminder') {
74  dol_syslog("Clear notification for listofreminderids=".$listofreminderids);
75  $listofreminderid = GETPOST('listofreminderids', 'intcomma');
76 
77  // Set the reminder as done
78  $sql = 'UPDATE '.MAIN_DB_PREFIX.'actioncomm_reminder SET status = 1';
79  $sql .= ' WHERE status = 0 AND rowid IN ('.$db->sanitize($db->escape($listofreminderid)).')';
80  $sql .= ' AND fk_user = '.((int) $user->id).' AND entity = '.((int) $conf->entity);
81  $resql = $db->query($sql);
82  if (!$resql) {
83  dol_print_error($db);
84  }
85  //}
86 
87  include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
88 
89  // Clean database
90  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'actioncomm_reminder';
91  $sql .= " WHERE dateremind < '".$db->idate(dol_time_plus_duree(dol_now(), -1, 'm'))."'";
92  $resql = $db->query($sql);
93  if (!$resql) {
94  dol_print_error($db);
95  }
96 
97  exit;
98 }
99 
100 
101 /*
102  * View
103  */
104 
105 top_httphead('application/json');
106 
107 global $user, $db, $langs, $conf;
108 
109 $eventfound = array();
110 //Uncomment this to force a test
111 //$eventfound[]=array('type'=>'agenda', 'id'=>1, 'tipo'=>'eee', 'location'=>'aaa');
112 
113 //dol_syslog('time='.$time.' $_SESSION[auto_ck_events_not_before]='.$_SESSION['auto_check_events_not_before']);
114 
115 // 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.
116 // This need to extend period to be sure to not miss and save in session what we notified to avoid duplicate.
117 if (empty($_SESSION['auto_check_events_not_before']) || $time >= $_SESSION['auto_check_events_not_before'] || GETPOST('forcechecknow', 'int')) {
118  /*$time_update = (int) $conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY; // Always defined
119  if (!empty($_SESSION['auto_check_events_not_before']))
120  {
121  // We start scan from the not before so if two tabs were opend at differents seconds and we close one (so the js timer),
122  // then we are not losing periods
123  $starttime = $_SESSION['auto_check_events_not_before'];
124  // Protection to avoid too long sessions
125  if ($starttime < ($time - (int) $conf->global->MAIN_SESSION_TIMEOUT))
126  {
127  dol_syslog("We ask to check browser notification on a too large period. We fix this with current date.");
128  $starttime = $time;
129  }
130  } else {
131  $starttime = $time;
132  }
133 
134  $_SESSION['auto_check_events_not_before'] = $time + $time_update;
135  */
136 
137  // Force save of the session change we did.
138  // WARNING: Any change in sessions after that will not be saved !
139  session_write_close();
140 
141  require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
142 
143 
144  dol_syslog('NEW $_SESSION[auto_check_events_not_before]='.(empty($_SESSION['auto_check_events_not_before']) ? '' : $_SESSION['auto_check_events_not_before']));
145 
146  $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';
147  $sql .= ' FROM '.MAIN_DB_PREFIX.'actioncomm as a';
148  if (!empty($user->conf->MAIN_USER_WANT_ALL_EVENTS_NOTIFICATIONS)) {
149  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'actioncomm_reminder as ar ON a.id = ar.fk_actioncomm AND ar.fk_user = '.((int) $user->id);
150  $sql .= ' WHERE a.code <> "AC_OTH_AUTO"';
151  $sql .= ' AND (';
152  $sql .= " ar.typeremind = 'browser' AND ar.dateremind < '".$db->idate(dol_now())."' AND ar.status = 0 AND ar.entity = ".$conf->entity;
153  $sql .= ' )';
154  } else {
155  $sql .= ' JOIN '.MAIN_DB_PREFIX.'actioncomm_reminder as ar ON a.id = ar.fk_actioncomm AND ar.fk_user = '.((int) $user->id);
156  $sql .= " AND ar.typeremind = 'browser' AND ar.dateremind < '".$db->idate(dol_now())."' AND ar.status = 0 AND ar.entity = ".$conf->entity;
157  }
158  $sql .= $db->order('datep', 'ASC');
159  $sql .= ' LIMIT 10'; // Avoid too many notification at once
160 
161  $resql = $db->query($sql);
162  if ($resql) {
163  while ($obj = $db->fetch_object($resql)) {
164  // Message must be formated and translated to be used with javascript directly
165  $event = array();
166  $event['type'] = 'agenda';
167  $event['id_reminder'] = $obj->id_reminder;
168  $event['id_agenda'] = $obj->id_agenda;
169  $event['id_user'] = $obj->id_user_reminder;
170  $event['code'] = $obj->code;
171  $event['label'] = $obj->label;
172  $event['location'] = $obj->location;
173  $event['reminder_date_formated_tzserver'] = dol_print_date($db->jdate($obj->dateremind), 'standard', 'tzserver');
174  $event['event_date_start_formated_tzserver'] = dol_print_date($db->jdate($obj->datep), 'standard', 'tzserver');
175  $event['reminder_date_formated'] = dol_print_date($db->jdate($obj->dateremind), 'standard', 'tzuser');
176  $event['event_date_start_formated'] = dol_print_date($db->jdate($obj->datep), 'standard', 'tzuser');
177 
178  $eventfound[$obj->id_agenda] = $event;
179  }
180  } else {
181  dol_syslog("Error sql = ".$db->lasterror(), LOG_ERR);
182  }
183 }
184 
185 print json_encode(array('pastreminders'=>$eventfound, 'nextreminder'=>''));
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
top_httphead
if(!defined('NOREQUIREMENU')) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1407
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2514
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
dol_time_plus_duree
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition: date.lib.php:121
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742