From f4d776b463d6c0ed4e24e44041d8bc2f1726189b Mon Sep 17 00:00:00 2001 From: guthub Date: Fri, 3 Jan 2025 14:33:09 +0100 Subject: [PATCH 01/15] add a check on working/opening hours AND entites check (see other PR) --- htdocs/public/bookcal/bookcalAjax.php | 66 ++++++++++++++++++++++++++- htdocs/public/bookcal/index.php | 63 +++++++++++++++++++++++-- 2 files changed, 125 insertions(+), 4 deletions(-) diff --git a/htdocs/public/bookcal/bookcalAjax.php b/htdocs/public/bookcal/bookcalAjax.php index 16e791c859b1a..ac35a9964b718 100644 --- a/htdocs/public/bookcal/bookcalAjax.php +++ b/htdocs/public/bookcal/bookcalAjax.php @@ -44,6 +44,7 @@ require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; +require_once DOL_DOCUMENT_ROOT.'/bookcal/class/calendar.class.php'; $action = GETPOST('action', 'aZ09'); $id = GETPOSTINT('id'); @@ -71,6 +72,66 @@ top_httphead('application/json'); +//MOD CHECK OPENHOURS +/** + * Check opening hours against availability days in entity conf + * MAIN_INFO_OPENINGHOURS_[weekday] content is used as ref for ranges + * expected ranges format is "HH:MM-HH:MM" minutes are optionals + * multiple range are ';' or ' ' separated + * + * @param $calid calendar id + * @param $datetocheckbooking apointement date + * @param $hourstring apointement start hour + * @param $minstring apointement start min + * @param $offsetmin apointement duration + * @param &$response response JSON + */ +function checkAgainstOpeningHours($calid, $datetocheckbooking, $hourstring, $minstring, $offsetmin, &$response) { + global $conf; + global $db; + + $cal = new Calendar($db); + $result = $cal->fetch($calid); + $savconf = $conf; + $conf=new Conf(); + $conf->entity = $cal->entity; + $conf->db = $db; + $conf->setValues($db); + + $rangesstr = getDolGlobalString('MAIN_INFO_OPENINGHOURS_'. strtoupper(date('l', $datetocheckbooking))); + $rangearr = preg_split ("/[\,; ]/", $rangesstr, -1, PREG_SPLIT_NO_EMPTY); + + foreach ($rangearr as $r) { + $timelim = explode('-', $r); + $tstart = array(); + $tstart[0] = '00'; + $tend = array(); + $tend[0] = '23'; + if ($timelim[0]) { + $tstart = preg_split ("/:/", $timelim[0]); + if ($timelim[1]) { + $tend = preg_split ("/:/", $timelim[1]); + } + } + if (count($tstart) == 1) { + $tstart[1] = '00'; + } + if (count($tend) == 1) { + $tend[1] = '59'; + } + $mintime = $datetocheckbooking + $tstart[0] *3600 + $tstart[1] *60; + $maxtime = $datetocheckbooking + $tend[0] *3600 + $tend[1] *60; + $evstart = $datetocheckbooking + $hourstring *3600 + ($minstring+$offsetmin) *60; + $evend = $evstart + $offsetmin*60; + if ($evstart >= $mintime && $evend <= $maxtime) { + return 1; + } + + } + $conf = $savconf; + return 0; +} + if ($action == 'verifyavailability') { // Test on permission not required here (anonymous action protected by mitigation of /public/... urls) $response = array(); if (empty($id)) { @@ -131,7 +192,10 @@ if ($min < 10) { $minstring = "0".$minstring; } - $response["availability"][$hourstring.":".$minstring] = intval($obj->duration); + //MOD CHECK OPENHOURS + if (checkAgainstOpeningHours($id, $datetocheckbooking, $hourstring, $minstring, $offsetmin, $response)) { + $response["availability"][$hourstring.":".$minstring] = intval($obj->duration); + } } } } diff --git a/htdocs/public/bookcal/index.php b/htdocs/public/bookcal/index.php index fc41027d6a8a5..dc0a297779757 100644 --- a/htdocs/public/bookcal/index.php +++ b/htdocs/public/bookcal/index.php @@ -194,6 +194,7 @@ function llxHeaderVierge($title, $head = "", $disablejs = 0, $disablehead = 0, $ $error = 0; $idcontact = 0; $calendar = $object; + $conf->entity = $calendar->entity; // force entity for actioncomm create (its using '$conf->') $contact = new Contact($db); $actioncomm = new ActionComm($db); $nb_post_max = getDolGlobalInt("MAIN_SECURITY_MAX_POST_ON_PUBLIC_PAGES_BY_IP_ADDRESS", 200); @@ -223,6 +224,9 @@ function llxHeaderVierge($title, $head = "", $disablejs = 0, $disablehead = 0, $ $sql .= " WHERE s.lastname = '".$db->escape(GETPOST("lastname"))."'"; $sql .= " AND s.firstname = '".$db->escape(GETPOST("firstname"))."'"; $sql .= " AND s.email = '".$db->escape(GETPOST("email"))."'"; + // cannot use getEntity (we are annonymous) here, + // so we check but only on same entity as known calendar + $sql .= " AND s.entity IN (". $calendar->entity .")"; $resql = $db->query($sql); if ($resql) { @@ -236,6 +240,9 @@ function llxHeaderVierge($title, $head = "", $disablejs = 0, $disablehead = 0, $ $contact->firstname = GETPOST("firstname"); $contact->email = GETPOST("email"); $contact->ip = getUserRemoteIP(); + // force entity to be same as calendar, + // so cal owner (+other if sharing allowed) can see it + $contact->entity = $calendar->entity; if (checkNbPostsForASpeceificIp($contact, $nb_post_max) <= 0) { $error++; @@ -267,9 +274,15 @@ function llxHeaderVierge($title, $head = "", $disablejs = 0, $disablehead = 0, $ $actioncomm->fk_bookcal_calendar = $id; $actioncomm->userownerid = $calendar->visibility; $actioncomm->contact_id = $contact->id; + // force entity to be same as calendar, so cal owner (+other if sharing allowed) can see it + $actioncomm->entity = $calendar->entity; + // set user (=cal owner) to BUSY + $actioncomm->transparency = 1; $actioncomm->socpeopleassigned = [ $contact->id => [ 'id' => $contact->id, + // force entity to be same as calendar + 'entity' => $calendar->entity, 'mandatory' => 0, 'answer_status' => 0, 'transparency' =>0, @@ -317,6 +330,38 @@ function llxHeaderVierge($title, $head = "", $disablejs = 0, $disablehead = 0, $ } //print '
'; +function check_in_range($start_date, $end_date, $date_from_user) +{ + // Convert to timestamp + $start_ts = strtotime($start_date); + $end_ts = strtotime($end_date); + $user_ts = strtotime($date_from_user); + + // Check that user date is between start & end + return (($user_ts >= $start_ts) && ($user_ts <= $end_ts)); +} + + + +function checkAgainstOpeningDays($daytocheck, $cal, $db) { + global $conf; + + $dow_text = date('l', $daytocheck); //no "dol_date" or dol_get_day_of_week + $valuechecked = 'MAIN_INFO_OPENINGHOURS_' . strtoupper($dow_text); + $savconf = $conf; + $conf=new Conf(); + $conf->entity = $cal->entity; + $conf->db = $db; + $conf->setValues($db); + + if (empty(getDolGlobalString($valuechecked)) || getDolGlobalString($valuechecked) == '0') { + return ''; + } + else { + return getDolGlobalString($valuechecked); + } + $conf = $savconf; +} print '
'; print '
'; @@ -437,9 +482,21 @@ function llxHeaderVierge($title, $head = "", $disablejs = 0, $disablehead = 0, $ foreach ($arrayofavailabilities as $key => $value) { $startarray = dol_getdate($value->start); $endarray = dol_getdate($value->end); - for ($i = $startarray['mday']; $i <= $endarray['mday']; $i++) { - if ($todayarray['mon'] >= $startarray['mon'] && $todayarray['mon'] <= $endarray['mon']) { - $arrayofavailabledays[dol_mktime(0, 0, 0, $todayarray['mon'], $i, $todayarray['year'])] = dol_mktime(0, 0, 0, $todayarray['mon'], $i, $todayarray['year']); + if ($value->start > $todaytms) { + $currdate = $value->start; + } else { + $currdate = $todaytms; + } + // Limit computing for big ranges (> 2months) + // TODO: make it check against global ? + $maxdayinfutur = dol_time_plus_duree($currdate, 60, 'd'); + if ($value->end < $maxdayinfutur) { + $maxdayinfutur = $value->end; + } + for (; $currdate <= $maxdayinfutur; $currdate = dol_time_plus_duree($currdate, 1, 'd')) { + $currdatearray=dol_getdate($currdate); + if (checkAgainstOpeningDays($currdate, $object, $db) != '') { + $arrayofavailabledays[dol_mktime(0, 0, 0, $currdatearray['mon'], $currdatearray['mday'], $currdatearray['year'])] = dol_mktime(0, 0, 0, $currdatearray['mon'], $currdatearray['mday'], $currdatearray['year']); } } } From 72f7f25d616f9437e443eceb96e56bcd3b7d1bb4 Mon Sep 17 00:00:00 2001 From: guthub Date: Fri, 3 Jan 2025 14:52:36 +0100 Subject: [PATCH 02/15] try fix phpstan --- htdocs/public/bookcal/bookcalAjax.php | 8 ++++---- htdocs/public/bookcal/index.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/htdocs/public/bookcal/bookcalAjax.php b/htdocs/public/bookcal/bookcalAjax.php index ac35a9964b718..347ad074e0b28 100644 --- a/htdocs/public/bookcal/bookcalAjax.php +++ b/htdocs/public/bookcal/bookcalAjax.php @@ -84,12 +84,12 @@ * @param $hourstring apointement start hour * @param $minstring apointement start min * @param $offsetmin apointement duration - * @param &$response response JSON + * @param @return return value 1 OK ; 0 KO */ -function checkAgainstOpeningHours($calid, $datetocheckbooking, $hourstring, $minstring, $offsetmin, &$response) { +function checkAgainstOpeningHours($calid, $datetocheckbooking, $hourstring, $minstring, $offsetmin) { global $conf; global $db; - + $cal = new Calendar($db); $result = $cal->fetch($calid); $savconf = $conf; @@ -193,7 +193,7 @@ function checkAgainstOpeningHours($calid, $datetocheckbooking, $hourstring, $min $minstring = "0".$minstring; } //MOD CHECK OPENHOURS - if (checkAgainstOpeningHours($id, $datetocheckbooking, $hourstring, $minstring, $offsetmin, $response)) { + if (checkAgainstOpeningHours($id, $datetocheckbooking, $hourstring, $minstring, $offsetmin)) { $response["availability"][$hourstring.":".$minstring] = intval($obj->duration); } } diff --git a/htdocs/public/bookcal/index.php b/htdocs/public/bookcal/index.php index dc0a297779757..f1b02cd1c8bc6 100644 --- a/htdocs/public/bookcal/index.php +++ b/htdocs/public/bookcal/index.php @@ -224,7 +224,7 @@ function llxHeaderVierge($title, $head = "", $disablejs = 0, $disablehead = 0, $ $sql .= " WHERE s.lastname = '".$db->escape(GETPOST("lastname"))."'"; $sql .= " AND s.firstname = '".$db->escape(GETPOST("firstname"))."'"; $sql .= " AND s.email = '".$db->escape(GETPOST("email"))."'"; - // cannot use getEntity (we are annonymous) here, + // cannot use getEntity (we are anonymous) here, // so we check but only on same entity as known calendar $sql .= " AND s.entity IN (". $calendar->entity .")"; $resql = $db->query($sql); From 9a22cdd799a2183afe099692a7e5fd27a5ce4d67 Mon Sep 17 00:00:00 2001 From: guthub Date: Fri, 3 Jan 2025 15:04:21 +0100 Subject: [PATCH 03/15] fix more phpstan --- htdocs/public/bookcal/bookcalAjax.php | 109 +++++++++++++------------- 1 file changed, 55 insertions(+), 54 deletions(-) diff --git a/htdocs/public/bookcal/bookcalAjax.php b/htdocs/public/bookcal/bookcalAjax.php index 347ad074e0b28..4a59e23142b3f 100644 --- a/htdocs/public/bookcal/bookcalAjax.php +++ b/htdocs/public/bookcal/bookcalAjax.php @@ -79,57 +79,58 @@ * expected ranges format is "HH:MM-HH:MM" minutes are optionals * multiple range are ';' or ' ' separated * - * @param $calid calendar id - * @param $datetocheckbooking apointement date - * @param $hourstring apointement start hour - * @param $minstring apointement start min - * @param $offsetmin apointement duration - * @param @return return value 1 OK ; 0 KO + * @param int $calid calendar id + * @param string $datetocheckbooking apointement date + * @param string $hourstring apointement start hour + * @param string $minstring apointement start min + * @param string $offsetmin apointement duration + * @return int value 1 OK ; 0 KO */ -function checkAgainstOpeningHours($calid, $datetocheckbooking, $hourstring, $minstring, $offsetmin) { - global $conf; - global $db; - - $cal = new Calendar($db); - $result = $cal->fetch($calid); - $savconf = $conf; - $conf=new Conf(); - $conf->entity = $cal->entity; - $conf->db = $db; - $conf->setValues($db); - - $rangesstr = getDolGlobalString('MAIN_INFO_OPENINGHOURS_'. strtoupper(date('l', $datetocheckbooking))); - $rangearr = preg_split ("/[\,; ]/", $rangesstr, -1, PREG_SPLIT_NO_EMPTY); - - foreach ($rangearr as $r) { - $timelim = explode('-', $r); - $tstart = array(); - $tstart[0] = '00'; - $tend = array(); - $tend[0] = '23'; - if ($timelim[0]) { - $tstart = preg_split ("/:/", $timelim[0]); - if ($timelim[1]) { - $tend = preg_split ("/:/", $timelim[1]); - } - } - if (count($tstart) == 1) { - $tstart[1] = '00'; - } - if (count($tend) == 1) { - $tend[1] = '59'; - } - $mintime = $datetocheckbooking + $tstart[0] *3600 + $tstart[1] *60; - $maxtime = $datetocheckbooking + $tend[0] *3600 + $tend[1] *60; - $evstart = $datetocheckbooking + $hourstring *3600 + ($minstring+$offsetmin) *60; - $evend = $evstart + $offsetmin*60; - if ($evstart >= $mintime && $evend <= $maxtime) { - return 1; - } - - } - $conf = $savconf; - return 0; +function checkAgainstOpeningHours($calid, $datetocheckbooking, $hourstring, $minstring, $offsetmin) +{ + global $conf; + global $db; + + $cal = new Calendar($db); + $result = $cal->fetch($calid); + $savconf = $conf; + $conf=new Conf(); + $conf->entity = $cal->entity; + $conf->db = $db; + $conf->setValues($db); + + $rangesstr = getDolGlobalString('MAIN_INFO_OPENINGHOURS_'. strtoupper(date('l', $datetocheckbooking))); + $rangearr = preg_split ("/[\,; ]/", $rangesstr, -1, PREG_SPLIT_NO_EMPTY); + + foreach ($rangearr as $r) { + $timelim = explode('-', $r); + $tstart = array(); + $tstart[0] = '00'; + $tend = array(); + $tend[0] = '23'; + if ($timelim[0]) { + $tstart = preg_split ("/:/", $timelim[0]); + if ($timelim[1]) { + $tend = preg_split ("/:/", $timelim[1]); + } + } + if (count($tstart) == 1) { + $tstart[1] = '00'; + } + if (count($tend) == 1) { + $tend[1] = '59'; + } + $mintime = $datetocheckbooking + $tstart[0] *3600 + $tstart[1] *60; + $maxtime = $datetocheckbooking + $tend[0] *3600 + $tend[1] *60; + $evstart = $datetocheckbooking + $hourstring *3600 + ($minstring+$offsetmin) *60; + $evend = $evstart + $offsetmin*60; + if ($evstart >= $mintime && $evend <= $maxtime) { + return 1; + } + + } + $conf = $savconf; + return 0; } if ($action == 'verifyavailability') { // Test on permission not required here (anonymous action protected by mitigation of /public/... urls) @@ -192,10 +193,10 @@ function checkAgainstOpeningHours($calid, $datetocheckbooking, $hourstring, $min if ($min < 10) { $minstring = "0".$minstring; } - //MOD CHECK OPENHOURS - if (checkAgainstOpeningHours($id, $datetocheckbooking, $hourstring, $minstring, $offsetmin)) { - $response["availability"][$hourstring.":".$minstring] = intval($obj->duration); - } + //MOD CHECK OPENHOURS + if (checkAgainstOpeningHours($id, $datetocheckbooking, $hourstring, $minstring, $offsetmin)) { + $response["availability"][$hourstring.":".$minstring] = intval($obj->duration); + } } } } From 3cd160ad65250d604121b363bab2a394f4e45f75 Mon Sep 17 00:00:00 2001 From: guthub Date: Fri, 3 Jan 2025 15:15:04 +0100 Subject: [PATCH 04/15] more phpstan --- htdocs/public/bookcal/bookcalAjax.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/htdocs/public/bookcal/bookcalAjax.php b/htdocs/public/bookcal/bookcalAjax.php index 4a59e23142b3f..461f3a084c066 100644 --- a/htdocs/public/bookcal/bookcalAjax.php +++ b/htdocs/public/bookcal/bookcalAjax.php @@ -100,7 +100,7 @@ function checkAgainstOpeningHours($calid, $datetocheckbooking, $hourstring, $min $conf->setValues($db); $rangesstr = getDolGlobalString('MAIN_INFO_OPENINGHOURS_'. strtoupper(date('l', $datetocheckbooking))); - $rangearr = preg_split ("/[\,; ]/", $rangesstr, -1, PREG_SPLIT_NO_EMPTY); + $rangearr = preg_split("/[\,; ]/", $rangesstr, -1, PREG_SPLIT_NO_EMPTY); foreach ($rangearr as $r) { $timelim = explode('-', $r); @@ -109,9 +109,9 @@ function checkAgainstOpeningHours($calid, $datetocheckbooking, $hourstring, $min $tend = array(); $tend[0] = '23'; if ($timelim[0]) { - $tstart = preg_split ("/:/", $timelim[0]); + $tstart = preg_split("/:/", $timelim[0]); if ($timelim[1]) { - $tend = preg_split ("/:/", $timelim[1]); + $tend = preg_split("/:/", $timelim[1]); } } if (count($tstart) == 1) { @@ -127,7 +127,6 @@ function checkAgainstOpeningHours($calid, $datetocheckbooking, $hourstring, $min if ($evstart >= $mintime && $evend <= $maxtime) { return 1; } - } $conf = $savconf; return 0; @@ -246,5 +245,4 @@ function checkAgainstOpeningHours($calid, $datetocheckbooking, $hourstring, $min $result = $response; } - echo json_encode($result); From 2df928d670fe1ba18ae8a312c356520e63bf72d7 Mon Sep 17 00:00:00 2001 From: guthub Date: Fri, 3 Jan 2025 15:25:39 +0100 Subject: [PATCH 05/15] more phpstan --- htdocs/public/bookcal/index.php | 92 ++++++++++++++++----------------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/htdocs/public/bookcal/index.php b/htdocs/public/bookcal/index.php index f1b02cd1c8bc6..a892d0ccec151 100644 --- a/htdocs/public/bookcal/index.php +++ b/htdocs/public/bookcal/index.php @@ -194,7 +194,7 @@ function llxHeaderVierge($title, $head = "", $disablejs = 0, $disablehead = 0, $ $error = 0; $idcontact = 0; $calendar = $object; - $conf->entity = $calendar->entity; // force entity for actioncomm create (its using '$conf->') + $conf->entity = $calendar->entity; // force entity for actioncomm create (its using '$conf->') $contact = new Contact($db); $actioncomm = new ActionComm($db); $nb_post_max = getDolGlobalInt("MAIN_SECURITY_MAX_POST_ON_PUBLIC_PAGES_BY_IP_ADDRESS", 200); @@ -224,9 +224,9 @@ function llxHeaderVierge($title, $head = "", $disablejs = 0, $disablehead = 0, $ $sql .= " WHERE s.lastname = '".$db->escape(GETPOST("lastname"))."'"; $sql .= " AND s.firstname = '".$db->escape(GETPOST("firstname"))."'"; $sql .= " AND s.email = '".$db->escape(GETPOST("email"))."'"; - // cannot use getEntity (we are anonymous) here, - // so we check but only on same entity as known calendar - $sql .= " AND s.entity IN (". $calendar->entity .")"; + // cannot use getEntity (we are anonymous) here, + // so we check but only on same entity as known calendar + $sql .= " AND s.entity IN (". $calendar->entity .")"; $resql = $db->query($sql); if ($resql) { @@ -240,9 +240,9 @@ function llxHeaderVierge($title, $head = "", $disablejs = 0, $disablehead = 0, $ $contact->firstname = GETPOST("firstname"); $contact->email = GETPOST("email"); $contact->ip = getUserRemoteIP(); - // force entity to be same as calendar, - // so cal owner (+other if sharing allowed) can see it - $contact->entity = $calendar->entity; + // force entity to be same as calendar, + // so cal owner (+other if sharing allowed) can see it + $contact->entity = $calendar->entity; if (checkNbPostsForASpeceificIp($contact, $nb_post_max) <= 0) { $error++; @@ -274,15 +274,15 @@ function llxHeaderVierge($title, $head = "", $disablejs = 0, $disablehead = 0, $ $actioncomm->fk_bookcal_calendar = $id; $actioncomm->userownerid = $calendar->visibility; $actioncomm->contact_id = $contact->id; - // force entity to be same as calendar, so cal owner (+other if sharing allowed) can see it - $actioncomm->entity = $calendar->entity; - // set user (=cal owner) to BUSY - $actioncomm->transparency = 1; + // force entity to be same as calendar, so cal owner (+other if sharing allowed) can see it + $actioncomm->entity = $calendar->entity; + // set user (=cal owner) to BUSY + $actioncomm->transparency = 1; $actioncomm->socpeopleassigned = [ $contact->id => [ 'id' => $contact->id, - // force entity to be same as calendar - 'entity' => $calendar->entity, + // force entity to be same as calendar + 'entity' => $calendar->entity, 'mandatory' => 0, 'answer_status' => 0, 'transparency' =>0, @@ -344,23 +344,23 @@ function check_in_range($start_date, $end_date, $date_from_user) function checkAgainstOpeningDays($daytocheck, $cal, $db) { - global $conf; - - $dow_text = date('l', $daytocheck); //no "dol_date" or dol_get_day_of_week - $valuechecked = 'MAIN_INFO_OPENINGHOURS_' . strtoupper($dow_text); - $savconf = $conf; - $conf=new Conf(); - $conf->entity = $cal->entity; - $conf->db = $db; - $conf->setValues($db); - - if (empty(getDolGlobalString($valuechecked)) || getDolGlobalString($valuechecked) == '0') { - return ''; - } - else { - return getDolGlobalString($valuechecked); - } - $conf = $savconf; + global $conf; + + $dow_text = date('l', $daytocheck); //no "dol_date" or dol_get_day_of_week + $valuechecked = 'MAIN_INFO_OPENINGHOURS_' . strtoupper($dow_text); + $savconf = $conf; + $conf=new Conf(); + $conf->entity = $cal->entity; + $conf->db = $db; + $conf->setValues($db); + + if (empty(getDolGlobalString($valuechecked)) || getDolGlobalString($valuechecked) == '0') { + return ''; + } + else { + return getDolGlobalString($valuechecked); + } + $conf = $savconf; } print '
'; @@ -482,21 +482,21 @@ function checkAgainstOpeningDays($daytocheck, $cal, $db) { foreach ($arrayofavailabilities as $key => $value) { $startarray = dol_getdate($value->start); $endarray = dol_getdate($value->end); - if ($value->start > $todaytms) { - $currdate = $value->start; - } else { - $currdate = $todaytms; - } - // Limit computing for big ranges (> 2months) - // TODO: make it check against global ? - $maxdayinfutur = dol_time_plus_duree($currdate, 60, 'd'); - if ($value->end < $maxdayinfutur) { - $maxdayinfutur = $value->end; - } - for (; $currdate <= $maxdayinfutur; $currdate = dol_time_plus_duree($currdate, 1, 'd')) { - $currdatearray=dol_getdate($currdate); - if (checkAgainstOpeningDays($currdate, $object, $db) != '') { - $arrayofavailabledays[dol_mktime(0, 0, 0, $currdatearray['mon'], $currdatearray['mday'], $currdatearray['year'])] = dol_mktime(0, 0, 0, $currdatearray['mon'], $currdatearray['mday'], $currdatearray['year']); + if ($value->start > $todaytms) { + $currdate = $value->start; + } else { + $currdate = $todaytms; + } + // Limit computing for big ranges (> 2months) + // TODO: make it check against global ? + $maxdayinfutur = dol_time_plus_duree($currdate, 60, 'd'); + if ($value->end < $maxdayinfutur) { + $maxdayinfutur = $value->end; + } + for (; $currdate <= $maxdayinfutur; $currdate = dol_time_plus_duree($currdate, 1, 'd')) { + $currdatearray=dol_getdate($currdate); + if (checkAgainstOpeningDays($currdate, $object, $db) != '') { + $arrayofavailabledays[dol_mktime(0, 0, 0, $currdatearray['mon'], $currdatearray['mday'], $currdatearray['year'])] = dol_mktime(0, 0, 0, $currdatearray['mon'], $currdatearray['mday'], $currdatearray['year']); } } } @@ -675,7 +675,7 @@ function generateBookingButtons(timearray, datestring){ /** * Show event of a particular day * - * @param int $day Day + * @param int $day Day * @param int $month Month * @param int $year Year * @param int $today Today's day From 1d7867ab955c76284385c8fd289931fb34049574 Mon Sep 17 00:00:00 2001 From: guthub Date: Fri, 3 Jan 2025 16:21:27 +0100 Subject: [PATCH 06/15] even more phpstan --- htdocs/public/bookcal/index.php | 53 ++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/htdocs/public/bookcal/index.php b/htdocs/public/bookcal/index.php index a892d0ccec151..3d34e7a0dea72 100644 --- a/htdocs/public/bookcal/index.php +++ b/htdocs/public/bookcal/index.php @@ -4,7 +4,7 @@ * Copyright (C) 2009-2012 Regis Houssin * Copyright (C) 2023 anthony Berton * Copyright (C) 2024 MDW - * Copyright (C) 2024 Frédéric France + * Copyright (C) 2024 Frédéric France * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,9 +21,9 @@ */ /** - * \file htdocs/public/bookcal/index.php - * \ingroup core - * \brief File to offer a way to book a rendez-vous into a public calendar + * \file htdocs/public/bookcal/index.php + * \ingroup core + * \brief File to offer a way to book a rendez-vous into a public calendar * Example of URL: https://localhost/public/bookcal/index.php?id=... */ @@ -127,10 +127,10 @@ * * @param string $title Title * @param string $head Head array - * @param int $disablejs More content into html header - * @param int $disablehead More content into html header - * @param string[]|string $arrayofjs Array of complementary js files - * @param string[]|string $arrayofcss Array of complementary css files + * @param int $disablejs More content into html header + * @param int $disablehead More content into html header + * @param string[]|string $arrayofjs Array of complementary js files + * @param string[]|string $arrayofcss Array of complementary css files * @return void */ function llxHeaderVierge($title, $head = "", $disablejs = 0, $disablehead = 0, $arrayofjs = [], $arrayofcss = []) @@ -330,27 +330,45 @@ function llxHeaderVierge($title, $head = "", $disablejs = 0, $disablehead = 0, $ } //print '
'; + +/** + * Check if in availability range + * + * @param string $start_date startdate + * @param string $end_date enddate + * @param string $date_from_user date selected + * @return boolean is date selected between sart and end + */ function check_in_range($start_date, $end_date, $date_from_user) { - // Convert to timestamp - $start_ts = strtotime($start_date); - $end_ts = strtotime($end_date); - $user_ts = strtotime($date_from_user); + // Convert to timestamp + $start_ts = strtotime($start_date); + $end_ts = strtotime($end_date); + $user_ts = strtotime($date_from_user); - // Check that user date is between start & end - return (($user_ts >= $start_ts) && ($user_ts <= $end_ts)); + // Check that user date is between start & end + return (($user_ts >= $start_ts) && ($user_ts <= $end_ts)); } -function checkAgainstOpeningDays($daytocheck, $cal, $db) { +/** + * Check if date is in opening days + * + * @param string $start_date startdate + * @param object $calentity entity from calendar + * @param object $db DB, might be useless (global ?) + * @return string opening hours on that day + */ +function checkAgainstOpeningDays($daytocheck, $calentity, $db) +{ global $conf; $dow_text = date('l', $daytocheck); //no "dol_date" or dol_get_day_of_week $valuechecked = 'MAIN_INFO_OPENINGHOURS_' . strtoupper($dow_text); $savconf = $conf; $conf=new Conf(); - $conf->entity = $cal->entity; + $conf->entity = $calentity; $conf->db = $db; $conf->setValues($db); @@ -361,6 +379,7 @@ function checkAgainstOpeningDays($daytocheck, $cal, $db) { return getDolGlobalString($valuechecked); } $conf = $savconf; + return ''; } print '
'; @@ -495,7 +514,7 @@ function checkAgainstOpeningDays($daytocheck, $cal, $db) { } for (; $currdate <= $maxdayinfutur; $currdate = dol_time_plus_duree($currdate, 1, 'd')) { $currdatearray=dol_getdate($currdate); - if (checkAgainstOpeningDays($currdate, $object, $db) != '') { + if (checkAgainstOpeningDays($currdate, $object->entity, $db) != '') { $arrayofavailabledays[dol_mktime(0, 0, 0, $currdatearray['mon'], $currdatearray['mday'], $currdatearray['year'])] = dol_mktime(0, 0, 0, $currdatearray['mon'], $currdatearray['mday'], $currdatearray['year']); } } From ea7956636a1d16451aefec26e4c75d2760d34c93 Mon Sep 17 00:00:00 2001 From: guthub Date: Fri, 3 Jan 2025 16:31:36 +0100 Subject: [PATCH 07/15] phpstan... --- htdocs/public/bookcal/index.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/htdocs/public/bookcal/index.php b/htdocs/public/bookcal/index.php index 3d34e7a0dea72..03484055ca3c4 100644 --- a/htdocs/public/bookcal/index.php +++ b/htdocs/public/bookcal/index.php @@ -355,7 +355,7 @@ function check_in_range($start_date, $end_date, $date_from_user) /** * Check if date is in opening days * - * @param string $start_date startdate + * @param object $daytocheck date * @param object $calentity entity from calendar * @param object $db DB, might be useless (global ?) * @return string opening hours on that day @@ -363,7 +363,7 @@ function check_in_range($start_date, $end_date, $date_from_user) function checkAgainstOpeningDays($daytocheck, $calentity, $db) { global $conf; - + $dow_text = date('l', $daytocheck); //no "dol_date" or dol_get_day_of_week $valuechecked = 'MAIN_INFO_OPENINGHOURS_' . strtoupper($dow_text); $savconf = $conf; @@ -374,8 +374,7 @@ function checkAgainstOpeningDays($daytocheck, $calentity, $db) if (empty(getDolGlobalString($valuechecked)) || getDolGlobalString($valuechecked) == '0') { return ''; - } - else { + } else { return getDolGlobalString($valuechecked); } $conf = $savconf; From 4823682790f789af49ba155edb8c65afbe676e40 Mon Sep 17 00:00:00 2001 From: guthub Date: Fri, 3 Jan 2025 16:35:57 +0100 Subject: [PATCH 08/15] typo --- htdocs/public/bookcal/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/public/bookcal/index.php b/htdocs/public/bookcal/index.php index 03484055ca3c4..20d579a6d1a3e 100644 --- a/htdocs/public/bookcal/index.php +++ b/htdocs/public/bookcal/index.php @@ -337,7 +337,7 @@ function llxHeaderVierge($title, $head = "", $disablejs = 0, $disablehead = 0, $ * @param string $start_date startdate * @param string $end_date enddate * @param string $date_from_user date selected - * @return boolean is date selected between sart and end + * @return boolean is date selected between start and end */ function check_in_range($start_date, $end_date, $date_from_user) { From c1a4e2e5128f7468f6c39eb221d97de236750fa5 Mon Sep 17 00:00:00 2001 From: guthub Date: Fri, 3 Jan 2025 16:51:32 +0100 Subject: [PATCH 09/15] phpstan + optim --- htdocs/public/bookcal/bookcalAjax.php | 12 ++++++------ htdocs/public/bookcal/index.php | 11 +++++------ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/htdocs/public/bookcal/bookcalAjax.php b/htdocs/public/bookcal/bookcalAjax.php index 461f3a084c066..5d5465ed9c0ca 100644 --- a/htdocs/public/bookcal/bookcalAjax.php +++ b/htdocs/public/bookcal/bookcalAjax.php @@ -79,12 +79,12 @@ * expected ranges format is "HH:MM-HH:MM" minutes are optionals * multiple range are ';' or ' ' separated * - * @param int $calid calendar id - * @param string $datetocheckbooking apointement date - * @param string $hourstring apointement start hour - * @param string $minstring apointement start min - * @param string $offsetmin apointement duration - * @return int value 1 OK ; 0 KO + * @param int $calid calendar id + * @param int $datetocheckbooking apointement date + * @param string $hourstring apointement start hour + * @param string $minstring apointement start min + * @param string $offsetmin apointement duration + * @return int value 1 OK ; 0 KO */ function checkAgainstOpeningHours($calid, $datetocheckbooking, $hourstring, $minstring, $offsetmin) { diff --git a/htdocs/public/bookcal/index.php b/htdocs/public/bookcal/index.php index 20d579a6d1a3e..620720600d802 100644 --- a/htdocs/public/bookcal/index.php +++ b/htdocs/public/bookcal/index.php @@ -355,7 +355,7 @@ function check_in_range($start_date, $end_date, $date_from_user) /** * Check if date is in opening days * - * @param object $daytocheck date + * @param int $daytocheck date * @param object $calentity entity from calendar * @param object $db DB, might be useless (global ?) * @return string opening hours on that day @@ -371,14 +371,13 @@ function checkAgainstOpeningDays($daytocheck, $calentity, $db) $conf->entity = $calentity; $conf->db = $db; $conf->setValues($db); + $retval = ''; - if (empty(getDolGlobalString($valuechecked)) || getDolGlobalString($valuechecked) == '0') { - return ''; - } else { - return getDolGlobalString($valuechecked); + if (!empty(getDolGlobalString($valuechecked)) && getDolGlobalString($valuechecked) != '0') { + $retval = getDolGlobalString($valuechecked); } $conf = $savconf; - return ''; + return $retval; } print '
'; From 8608b85ce36c3042830b601e5af2309269606b36 Mon Sep 17 00:00:00 2001 From: guthub Date: Fri, 3 Jan 2025 17:01:44 +0100 Subject: [PATCH 10/15] fix doc --- htdocs/public/bookcal/bookcalAjax.php | 2 +- htdocs/public/bookcal/index.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/public/bookcal/bookcalAjax.php b/htdocs/public/bookcal/bookcalAjax.php index 5d5465ed9c0ca..3e16136908554 100644 --- a/htdocs/public/bookcal/bookcalAjax.php +++ b/htdocs/public/bookcal/bookcalAjax.php @@ -83,7 +83,7 @@ * @param int $datetocheckbooking apointement date * @param string $hourstring apointement start hour * @param string $minstring apointement start min - * @param string $offsetmin apointement duration + * @param int $offsetmin apointement duration * @return int value 1 OK ; 0 KO */ function checkAgainstOpeningHours($calid, $datetocheckbooking, $hourstring, $minstring, $offsetmin) diff --git a/htdocs/public/bookcal/index.php b/htdocs/public/bookcal/index.php index 620720600d802..55436e1eaebdc 100644 --- a/htdocs/public/bookcal/index.php +++ b/htdocs/public/bookcal/index.php @@ -356,7 +356,7 @@ function check_in_range($start_date, $end_date, $date_from_user) * Check if date is in opening days * * @param int $daytocheck date - * @param object $calentity entity from calendar + * @param int $calentity entity from calendar * @param object $db DB, might be useless (global ?) * @return string opening hours on that day */ From 08b40bc697eadd46efb192b4c4b4805c012fd140 Mon Sep 17 00:00:00 2001 From: guthub Date: Fri, 3 Jan 2025 17:11:15 +0100 Subject: [PATCH 11/15] string -> int --- htdocs/public/bookcal/bookcalAjax.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/public/bookcal/bookcalAjax.php b/htdocs/public/bookcal/bookcalAjax.php index 3e16136908554..fc1dae8ed6ff7 100644 --- a/htdocs/public/bookcal/bookcalAjax.php +++ b/htdocs/public/bookcal/bookcalAjax.php @@ -122,7 +122,7 @@ function checkAgainstOpeningHours($calid, $datetocheckbooking, $hourstring, $min } $mintime = $datetocheckbooking + $tstart[0] *3600 + $tstart[1] *60; $maxtime = $datetocheckbooking + $tend[0] *3600 + $tend[1] *60; - $evstart = $datetocheckbooking + $hourstring *3600 + ($minstring+$offsetmin) *60; + $evstart = $datetocheckbooking + intval($hourstring) *3600 + (intval($minstring)+$offsetmin) *60; $evend = $evstart + $offsetmin*60; if ($evstart >= $mintime && $evend <= $maxtime) { return 1; From 2cb956f490a402cde78f9f6ca9a61738dd13690e Mon Sep 17 00:00:00 2001 From: guthub Date: Fri, 3 Jan 2025 18:37:58 +0100 Subject: [PATCH 12/15] fix sql (travis error) --- htdocs/public/bookcal/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/public/bookcal/index.php b/htdocs/public/bookcal/index.php index 55436e1eaebdc..7161534d4c860 100644 --- a/htdocs/public/bookcal/index.php +++ b/htdocs/public/bookcal/index.php @@ -226,7 +226,7 @@ function llxHeaderVierge($title, $head = "", $disablejs = 0, $disablehead = 0, $ $sql .= " AND s.email = '".$db->escape(GETPOST("email"))."'"; // cannot use getEntity (we are anonymous) here, // so we check but only on same entity as known calendar - $sql .= " AND s.entity IN (". $calendar->entity .")"; + $sql .= " AND s.entity IN (". $db->escape($calendar->entity) .")"; $resql = $db->query($sql); if ($resql) { From 39ed350588c1ada456f7e8d2067d7411e0f61f3d Mon Sep 17 00:00:00 2001 From: guthub Date: Fri, 3 Jan 2025 18:52:16 +0100 Subject: [PATCH 13/15] php typo --- htdocs/public/bookcal/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/public/bookcal/index.php b/htdocs/public/bookcal/index.php index 7161534d4c860..d998275466b4b 100644 --- a/htdocs/public/bookcal/index.php +++ b/htdocs/public/bookcal/index.php @@ -226,7 +226,7 @@ function llxHeaderVierge($title, $head = "", $disablejs = 0, $disablehead = 0, $ $sql .= " AND s.email = '".$db->escape(GETPOST("email"))."'"; // cannot use getEntity (we are anonymous) here, // so we check but only on same entity as known calendar - $sql .= " AND s.entity IN (". $db->escape($calendar->entity) .")"; + $sql .= " AND s.entity IN (".$db->escape($calendar->entity).")"; $resql = $db->query($sql); if ($resql) { From 49a9aa61e3551b50d8dc05cfc7ecfdedc5152d04 Mon Sep 17 00:00:00 2001 From: guthub Date: Fri, 3 Jan 2025 19:09:41 +0100 Subject: [PATCH 14/15] new try on travis SQL fix --- htdocs/public/bookcal/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/public/bookcal/index.php b/htdocs/public/bookcal/index.php index d998275466b4b..89e55d5b2c2a9 100644 --- a/htdocs/public/bookcal/index.php +++ b/htdocs/public/bookcal/index.php @@ -226,7 +226,7 @@ function llxHeaderVierge($title, $head = "", $disablejs = 0, $disablehead = 0, $ $sql .= " AND s.email = '".$db->escape(GETPOST("email"))."'"; // cannot use getEntity (we are anonymous) here, // so we check but only on same entity as known calendar - $sql .= " AND s.entity IN (".$db->escape($calendar->entity).")"; + $sql .= " AND s.entity = ".((int) $calendar->entity); $resql = $db->query($sql); if ($resql) { From 094e5aca858342502b0cf3da5cd23bd431078d2a Mon Sep 17 00:00:00 2001 From: guthub Date: Fri, 3 Jan 2025 19:23:22 +0100 Subject: [PATCH 15/15] fix test --- htdocs/public/bookcal/index.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/htdocs/public/bookcal/index.php b/htdocs/public/bookcal/index.php index 89e55d5b2c2a9..fb46d7005a8d0 100644 --- a/htdocs/public/bookcal/index.php +++ b/htdocs/public/bookcal/index.php @@ -371,10 +371,10 @@ function checkAgainstOpeningDays($daytocheck, $calentity, $db) $conf->entity = $calentity; $conf->db = $db; $conf->setValues($db); - $retval = ''; + $retval = '' . getDolGlobalString($valuechecked); - if (!empty(getDolGlobalString($valuechecked)) && getDolGlobalString($valuechecked) != '0') { - $retval = getDolGlobalString($valuechecked); + if ($retval == '0') { + $retval = ''; } $conf = $savconf; return $retval;