function set_display_timezone($user) { global $prefs; if ($prefs['users_prefs_display_timezone'] == 'Site' || isset($user_preferences[$user]['display_timezone']) && $user_preferences[$user]['display_timezone'] == 'Site') { // Stay in the time zone of the server $prefs['display_timezone'] = $prefs['server_timezone']; } elseif (!isset($prefs['display_timezone']) and !isset($user_preferences[$user]['display_timezone']) || $user_preferences[$user]['display_timezone'] == '' || $user_preferences[$user]['display_timezone'] == 'Local') { // If the display timezone is not known ... if (isset($_COOKIE['local_tz'])) { // ... we try to use the timezone detected by javascript and stored in cookies if (TikiDate::TimezoneIsValidId($_COOKIE['local_tz'])) { $prefs['timezone_offset'] = isset($_COOKIE['local_tzoffset']) ? $_COOKIE['local_tzoffset'] : ''; if (isset($_COOKIE['local_tzoffset'])) { $tzname = timezone_name_from_abbr($_COOKIE['local_tz'], $_COOKIE['local_tzoffset'] * 60 * 60); $prefs['timezone_offset'] = $_COOKIE['local_tzoffset']; } else { $tzname = timezone_name_from_abbr($_COOKIE['local_tz']); $prefs['timezone_offset'] = ''; } if (TikiDate::TimezoneIsValidId($tzname)) { $prefs['display_timezone'] = $tzname; } else { $prefs['display_timezone'] = $_COOKIE['local_tz']; } } elseif ($_COOKIE['local_tz'] == 'HAEC') { // HAEC, returned by Safari on Mac, is not recognized as a DST timezone (with daylightsavings) // ... So use one equivalent timezone name $prefs['display_timezone'] = 'Europe/Paris'; } else { $prefs['display_timezone'] = $prefs['server_timezone']; } } else { // ... and we fallback to the server timezone if the cookie value is not available $prefs['display_timezone'] = $prefs['server_timezone']; } } }
function get_extend_until_info($user, $group, $periods = 1) { // Calculations here should always be 1 am system time otherwise prone to user manipulation or daylight savings problems global $prefs; $tz = $prefs['server_timezone']; if ( ! TikiDate::TimezoneIsValidId($tz) ) { $tz = 'UTC'; } $timezone = new DateTimeZone($tz); $userInfo = $this->get_user_info($user); $info = $this->get_group_info($group); $ratio_prorated_first_period = 1; if (empty($info['prorateInterval'])) { $prorateInterval = 'day'; } else { $prorateInterval = $info['prorateInterval']; } $date = $this->getOne( 'SELECT `expire` FROM `users_usergroups` where `userId` = ? AND `groupName` = ?', array($userInfo['userId'], $group) ); if (!$date) { $date = $this->now; } if (!empty($info['anniversary'])) { $date_year = date('Y', $date); $date_month = date('m', $date); $date_day = date('d', $date); $effective_date = new DateTime("{$date_year}-{$date_month}-{$date_day} 01:00:00", $timezone); $ratio_prorated_first_period = 1; if (strlen($info['anniversary']) == 4) { // annual anniversaries $ann_month = substr($info['anniversary'], 0, 2); $ann_day = substr($info['anniversary'], 2, 2); // start off with this year's anniversary date $extend_until = new DateTime("{$date_year}-{$ann_month}-{$ann_day} 01:00:00", $timezone); while ($effective_date->format('U') >= $extend_until->format('U')) { // already passed the anniversary this month, extend to next year's anniversary $extend_until->modify('+1 year'); } // store last past anniversary for prorating $prev_ann = clone $extend_until; $prev_ann->modify('-1 year'); if ($prorateInterval == 'year') { $payable_from = clone $prev_ann; } elseif ($prorateInterval == 'month') { $payable_from = clone $extend_until; while ($payable_from->format('U') > $effective_date->format('U')) { $payable_from->modify('-1 month'); } } elseif ($prorateInterval == 'day') { $payable_from = clone $effective_date; } $extend_until_first_period = clone $extend_until; // add extra full periods if ($periods > 1) { $p = $periods - 1; $extend_until->modify("+$p year"); } } elseif (strlen($info['anniversary']) == 2) { // monthly anniversaries $ann_day = $info['anniversary']; // start off with this month's anniversary date $extend_until = new DateTime("{$date_year}-{$date_month}-{$ann_day} 01:00:00", $timezone); while ($effective_date->format('U') >= $extend_until->format('U')) { // already passed the anniversary this month, extend to next month's anniversary $extend_until->modify('+1 month'); } // store last past anniversary for prorating $prev_ann = clone $extend_until; $prev_ann->modify('-1 month'); if ($prorateInterval == 'month' || $prorateInterval == 'year') { $payable_from = clone $prev_ann; } elseif ($prorateInterval == 'day') { $payable_from = clone $effective_date; } $extend_until_first_period = clone $extend_until; // add extra full periods if ($periods > 1) { $p = $periods - 1; $extend_until->modify("+$p month"); } } elseif (strlen($info['anniversary']) == 3) { // Case not handled, and variables used below is not set // Not sure what to do here? } $timestamp = null; if ($extend_until != null && $payable_from != null && $prev_ann != null) { $ratio_prorated_first_period = ($extend_until_first_period->format('U') - $payable_from->format('U')) / ($extend_until_first_period->format('U') - $prev_ann->format('U')); $timestamp = $extend_until->format('U'); } } else { $timestamp = $date + $periods * $info['expireAfter'] * 24 * 3600; } return array('timestamp' => $timestamp, 'ratio_prorated_first_period' => $ratio_prorated_first_period); }
/** * @param bool $_user * @return null|string */ function get_display_timezone($_user = false) { global $prefs, $user; if ($_user === false || $_user == $user) { // If the requested timezone is the current user timezone $tz = $prefs['display_timezone']; } elseif ($_user) { // ... else, get the user timezone preferences from DB $tz = $this->get_user_preference($_user, 'display_timezone'); } if (!TikiDate::TimezoneIsValidId($tz)) { $tz = $prefs['server_timezone']; } if (!TikiDate::TimezoneIsValidId($tz)) { $tz = 'UTC'; } return $tz; }
function get_extend_until_info($user, $group, $periods = 1) { //use these functions to get current expiry dates for existing members - they are calculated in some cases //so just grabbing the "expire" field from the users_usergroups table doesn't always work $userInfo = $this->get_user_info($user); $usergroupdates = $this->get_user_groups_date($userInfo['userId']); $info = $this->get_group_info($group); //set the start date as now for new memberships and as expiry of current membership for existing members if (array_key_exists($group, $usergroupdates)) { if (!empty($usergroupdates[$group]['expire'])) { $date = $usergroupdates[$group]['expire']; } elseif ($info['expireAfter'] > 0) { $date = $usergroupdates[$group]['created']; } } if (!isset($date) || !$date) { $date = $this->now; //this is a new membership $new = true; } //convert start date to object $rawstartutc = new DateTimeImmutable('@' . $date); global $prefs; $tz = TikiDate::TimezoneIsValidId($prefs['server_timezone']) ? $prefs['server_timezone'] : 'UTC'; $timezone = new DateTimeZone($tz); $startlocal = $rawstartutc->setTimezone($timezone); //anniversary memberships if (!empty($info['anniversary'])) { //set time to 1 second after midnight so that all times are set to same times for interval calculations $startlocal = $startlocal->setTime(0, 0, 1); // annual anniversaries if (strlen($info['anniversary']) == 4) { $ann_month = substr($info['anniversary'], 0, 2); $ann_day = substr($info['anniversary'], 2, 2); $startyear = $startlocal->format('Y'); //increment the year if past the annual anniversary if ($startlocal->format('m') > $ann_month || $startlocal->format('m') == $ann_month && $startlocal->format('d') >= $ann_day) { $startyear++; } //first extension is always to next anniversary $next_ann = $startlocal->setDate($startyear, $ann_month, $ann_day); //extend past next anniversary if more than one period $extendto = $next_ann->modify('+' . $periods - 1 . ' years'); //previous anniversary for proration $prev_ann = $next_ann->modify('-1 years'); // monthly anniversaries //using modify('+1 month') can result in "skipping" months so fix the day of the previous/next month } elseif (strlen($info['anniversary']) == 2) { $ann_day = $info['anniversary']; $lastday = date('d', strtotime('last day of ' . $startlocal->format('Y') . '-' . $startlocal->format('m'))); $mod_ann_day = $ann_day > $lastday ? $lastday : $ann_day; if ($startlocal->format('d') < $mod_ann_day) { $mod = $mod_ann_day - $startlocal->format('d'); $next_ann = $startlocal->modify('+' . $mod . ' days'); $prev_mo_lastday = $startlocal->modify('last day of last month'); if ($ann_day >= $prev_mo_lastday->format('d')) { $prev_ann = $prev_mo_lastday; } else { $prev_ann = $startlocal->setDate($prev_mo_lastday->format('Y'), $prev_mo_lastday->format('m'), $mod_ann_day); } } else { //check if last day of month $next_mo_lastday = $startlocal->modify('last day of next month'); if ($mod_ann_day >= $next_mo_lastday->format('d')) { $next_ann = $next_mo_lastday; } else { $next_ann = $startlocal->setDate($next_mo_lastday->format('Y'), $next_mo_lastday->format('m'), $mod_ann_day); } $mod = $startlocal->format('d') - $mod_ann_day; $prev_ann = $startlocal->modify('-' . $mod . ' days'); } if ($periods - 1 > 0) { $yrsplus = floor(($periods - 1) / 12); $yr = $next_ann->format('Y') + $yrsplus; $moplus = $periods - 1 - $yrsplus * 12; if ($moplus + $next_ann->format('m') < 12) { $mo = $moplus + $next_ann->format('m'); } else { $yr++; $mo = $moplus + $next_ann->format('m') - 12; } if ($ann_day >= date('d', strtotime('last day of ' . $yr . '-' . $mo))) { $d = date('d', strtotime('last day of ' . $yr . '-' . $mo)); } else { $d = $ann_day; } $extendto = $next_ann->setDate($yr, $mo, $d); } else { $extendto = $next_ann; } } //calculate interval of membership term $interval = $startlocal->diff($extendto); //set prorate interval $prorateInterval = in_array($info['prorateInterval'], ['year', 'month', 'day']) ? $info['prorateInterval'] : 'day'; //prorate if ($prorateInterval == 'year' && strlen($info['anniversary']) == 4) { $ratio = $interval->y; $ratio += $interval->m > 0 || $interval->d > 0 ? 1 : 0; } elseif ($prorateInterval == 'month' || $prorateInterval == 'year' && strlen($info['anniversary']) == 2) { $round = $interval->d > 0 ? 1 : 0; $ratio = $interval->y * 12 + $interval->m + $round; if (strlen($info['anniversary']) == 4) { $ratio = $ratio / 12; } } elseif ($prorateInterval == 'day') { $ann_interval = $prev_ann->diff($next_ann); $stub_interval = $startlocal->diff($next_ann); $ratio = $stub_interval->days / $ann_interval->days + ($periods - 1); } $remainder = $ratio > 1 ? $ratio - floor($ratio) : $ratio; //memberships based on number of days } else { $remainder = 1; $ratio = 1; $extendto = $startlocal->modify('+' . $info['expireAfter'] * $periods . ' days'); $interval = $startlocal->diff($extendto); } $timestamp = $extendto != null ? $extendto->format('U') : null; return array('timestamp' => $timestamp, 'ratio_prorated_first_period' => $remainder, 'ratio' => $ratio, 'interval' => $interval, 'new' => $new); }
$smarty->assign('userPage', $userPage); $smarty->assign('userPage_exists', $exist); } else { if (isset($_SESSION['preferences'])) { $prefs = array_merge($prefs, $_SESSION['preferences']); } $allowMsgs = 'n'; } $smarty->assign('IP', $tikilib->get_ip_address()); if ($prefs['users_prefs_display_timezone'] == 'Site' || isset($user_preferences[$user]['display_timezone']) && $user_preferences[$user]['display_timezone'] == 'Site') { // Stay in the time zone of the server $prefs['display_timezone'] = $prefs['server_timezone']; } elseif (!isset($user_preferences[$user]['display_timezone']) || $user_preferences[$user]['display_timezone'] == '' || $user_preferences[$user]['display_timezone'] == 'Local') { // If the display timezone is not known ... if (isset($_COOKIE['local_tz'])) { // ... we try to use the timezone detected by javascript and stored in cookies if (TikiDate::TimezoneIsValidId($_COOKIE['local_tz'])) { $prefs['display_timezone'] = $_COOKIE['local_tz']; } elseif ($_COOKIE['local_tz'] == 'HAEC') { // HAEC, returned by Safari on Mac, is not recognized as a DST timezone (with daylightsavings) // ... So use one equivalent timezone name $prefs['display_timezone'] = 'Europe/Paris'; } else { $prefs['display_timezone'] = $prefs['server_timezone']; } } else { // ... and we fallback to the server timezone if the cookie value is not available $prefs['display_timezone'] = $prefs['server_timezone']; } } $smarty->refreshLanguage();
// Stay in the time zone of the server $prefs['display_timezone'] = $prefs['server_timezone']; } elseif (!isset($user_preferences[$user]['display_timezone']) || $user_preferences[$user]['display_timezone'] == '' || $user_preferences[$user]['display_timezone'] == 'Local') { // If the display timezone is not known ... if (isset($_COOKIE['local_tz'])) { // ... we try to use the timezone detected by javascript and stored in cookies if (TikiDate::TimezoneIsValidId($_COOKIE['local_tz'])) { $prefs['timezone_offset'] = isset($_COOKIE['local_tzoffset']) ? $_COOKIE['local_tzoffset'] : ''; if (isset($_COOKIE['local_tzoffset'])) { $tzname = timezone_name_from_abbr($_COOKIE['local_tz'], $_COOKIE['local_tzoffset'] * 60 * 60); $prefs['timezone_offset'] = $_COOKIE['local_tzoffset']; } else { $tzname = timezone_name_from_abbr($_COOKIE['local_tz']); $prefs['timezone_offset'] = ''; } if (TikiDate::TimezoneIsValidId($tzname)) { $prefs['display_timezone'] = $tzname; } else { $prefs['display_timezone'] = $_COOKIE['local_tz']; } } elseif ($_COOKIE['local_tz'] == 'HAEC') { // HAEC, returned by Safari on Mac, is not recognized as a DST timezone (with daylightsavings) // ... So use one equivalent timezone name $prefs['display_timezone'] = 'Europe/Paris'; } else { $prefs['display_timezone'] = $prefs['server_timezone']; } } else { // ... and we fallback to the server timezone if the cookie value is not available $prefs['display_timezone'] = $prefs['server_timezone']; }