Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/wordfenc.../modules/login-se.../classes/controll...
File: time.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace WordfenceLS;
[2] Fix | Delete
[3] Fix | Delete
class Controller_Time {
[4] Fix | Delete
const NTP_VERSION = 3; // https://www.ietf.org/rfc/rfc1305.txt
[5] Fix | Delete
const NTP_EPOCH_CONVERT = 2208988800; //RFC 5905, page 13
[6] Fix | Delete
const FAILURE_LIMIT = 3;
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Returns the singleton Controller_Time.
[10] Fix | Delete
*
[11] Fix | Delete
* @return Controller_Time
[12] Fix | Delete
*/
[13] Fix | Delete
public static function shared() {
[14] Fix | Delete
static $_shared = null;
[15] Fix | Delete
if ($_shared === null) {
[16] Fix | Delete
$_shared = new Controller_Time();
[17] Fix | Delete
}
[18] Fix | Delete
return $_shared;
[19] Fix | Delete
}
[20] Fix | Delete
[21] Fix | Delete
public function install() {
[22] Fix | Delete
wp_clear_scheduled_hook('wordfence_ls_ntp_cron');
[23] Fix | Delete
if (is_main_site()) {
[24] Fix | Delete
wp_schedule_event(time() + 10, 'hourly', 'wordfence_ls_ntp_cron');
[25] Fix | Delete
}
[26] Fix | Delete
Controller_Settings::shared()->reset_ntp_disabled_flag();
[27] Fix | Delete
}
[28] Fix | Delete
[29] Fix | Delete
public function uninstall() {
[30] Fix | Delete
wp_clear_scheduled_hook('wordfence_ls_ntp_cron');
[31] Fix | Delete
Controller_Settings::shared()->reset_ntp_disabled_flag();
[32] Fix | Delete
}
[33] Fix | Delete
[34] Fix | Delete
public function init() {
[35] Fix | Delete
$this->_init_actions();
[36] Fix | Delete
}
[37] Fix | Delete
[38] Fix | Delete
public function _init_actions() {
[39] Fix | Delete
add_action('wordfence_ls_ntp_cron', array($this, '_wordfence_ls_ntp_cron'));
[40] Fix | Delete
}
[41] Fix | Delete
[42] Fix | Delete
public function _wordfence_ls_ntp_cron() {
[43] Fix | Delete
if (Controller_Settings::shared()->get_bool(Controller_Settings::OPTION_ALLOW_DISABLING_NTP) && Controller_Settings::shared()->is_ntp_cron_disabled())
[44] Fix | Delete
return;
[45] Fix | Delete
$ntp = self::ntp_time();
[46] Fix | Delete
$time = time();
[47] Fix | Delete
[48] Fix | Delete
if ($ntp === false) {
[49] Fix | Delete
$failureCount = Controller_Settings::shared()->increment_ntp_failure_count();
[50] Fix | Delete
if ($failureCount >= self::FAILURE_LIMIT) {
[51] Fix | Delete
Controller_Settings::shared()->set(Controller_Settings::OPTION_USE_NTP, false);
[52] Fix | Delete
Controller_Settings::shared()->set(Controller_Settings::OPTION_NTP_OFFSET, 0);
[53] Fix | Delete
}
[54] Fix | Delete
}
[55] Fix | Delete
else {
[56] Fix | Delete
Controller_Settings::shared()->reset_ntp_failure_count();
[57] Fix | Delete
Controller_Settings::shared()->set(Controller_Settings::OPTION_USE_NTP, true);
[58] Fix | Delete
Controller_Settings::shared()->set(Controller_Settings::OPTION_NTP_OFFSET, $ntp - $time);
[59] Fix | Delete
}
[60] Fix | Delete
Controller_Settings::shared()->set(Controller_Settings::OPTION_ALLOW_DISABLING_NTP, true);
[61] Fix | Delete
}
[62] Fix | Delete
[63] Fix | Delete
/**
[64] Fix | Delete
* Returns the current UTC timestamp, offset as needed to reflect the time retrieved from an NTP request or (if
[65] Fix | Delete
* running in the complete plugin) offset as needed from the Wordfence server's true time.
[66] Fix | Delete
*
[67] Fix | Delete
* @param bool|int $time The timestamp to apply any offset to. If `false`, it will use the current timestamp.
[68] Fix | Delete
* @return int
[69] Fix | Delete
*/
[70] Fix | Delete
public static function time($time = false) {
[71] Fix | Delete
if ($time === false) {
[72] Fix | Delete
$time = time();
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
$offset = 0;
[76] Fix | Delete
if (Controller_Settings::shared()->is_ntp_enabled()) {
[77] Fix | Delete
$offset = Controller_Settings::shared()->get_int(Controller_Settings::OPTION_NTP_OFFSET);
[78] Fix | Delete
}
[79] Fix | Delete
else if (WORDFENCE_LS_FROM_CORE) {
[80] Fix | Delete
$offset = \wfUtils::normalizedTime($time) - $time;
[81] Fix | Delete
}
[82] Fix | Delete
[83] Fix | Delete
return $time + $offset;
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
/**
[87] Fix | Delete
* Returns the current timestamp from ntp.org using the NTP protocol. If unable to (e.g., UDP connections are blocked),
[88] Fix | Delete
* it will return false.
[89] Fix | Delete
*
[90] Fix | Delete
* @return bool|float
[91] Fix | Delete
*/
[92] Fix | Delete
public static function ntp_time() {
[93] Fix | Delete
$servers = array('0.pool.ntp.org', '1.pool.ntp.org', '2.pool.ntp.org', '3.pool.ntp.org');
[94] Fix | Delete
[95] Fix | Delete
//Header - RFC 5905, page 18
[96] Fix | Delete
$header = '00'; //LI (leap indicator) - 2 bits: 00 for "no warning"
[97] Fix | Delete
$header .= sprintf('%03d', decbin(self::NTP_VERSION)); //VN (version number) - 3 bits: 011 for version 3
[98] Fix | Delete
$header .= '011'; //Mode (association mode) - 3 bit: 011 for "client"
[99] Fix | Delete
[100] Fix | Delete
$packet = chr(bindec($header));
[101] Fix | Delete
$packet .= str_repeat("\x0", 39);
[102] Fix | Delete
[103] Fix | Delete
foreach ($servers as $s) {
[104] Fix | Delete
$socket = @fsockopen('udp://' . $s, 123, $err_no, $err_str, 1);
[105] Fix | Delete
if ($socket) {
[106] Fix | Delete
stream_set_timeout($socket, 1);
[107] Fix | Delete
$remote_originate = microtime(true);
[108] Fix | Delete
$secondsNTP = ((int) $remote_originate) + self::NTP_EPOCH_CONVERT;
[109] Fix | Delete
$fractional = sprintf('%010d', round(($remote_originate - ((int) $remote_originate)) * 0x100000000));
[110] Fix | Delete
$packed = pack('N', $secondsNTP) . pack('N', $fractional);
[111] Fix | Delete
[112] Fix | Delete
if (@fwrite($socket, $packet . $packed)) {
[113] Fix | Delete
$response = fread($socket, 48);
[114] Fix | Delete
$local_transmitted = microtime(true);
[115] Fix | Delete
}
[116] Fix | Delete
@fclose($socket);
[117] Fix | Delete
[118] Fix | Delete
if (isset($response) && Model_Crypto::strlen($response) == 48) {
[119] Fix | Delete
break;
[120] Fix | Delete
}
[121] Fix | Delete
}
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
if (isset($response) && Model_Crypto::strlen($response) == 48) {
[125] Fix | Delete
$longs = unpack("N12", $response);
[126] Fix | Delete
[127] Fix | Delete
$remote_originate_seconds = sprintf('%u', $longs[7]) - self::NTP_EPOCH_CONVERT;
[128] Fix | Delete
$remote_received_seconds = sprintf('%u', $longs[9]) - self::NTP_EPOCH_CONVERT;
[129] Fix | Delete
$remote_transmitted_seconds = sprintf('%u', $longs[11]) - self::NTP_EPOCH_CONVERT;
[130] Fix | Delete
[131] Fix | Delete
$remote_originate_fraction = sprintf('%u', $longs[8]) / 0x100000000;
[132] Fix | Delete
$remote_received_fraction = sprintf('%u', $longs[10]) / 0x100000000;
[133] Fix | Delete
$remote_transmitted_fraction = sprintf('%u', $longs[12]) / 0x100000000;
[134] Fix | Delete
[135] Fix | Delete
$remote_originate = $remote_originate_seconds + $remote_originate_fraction;
[136] Fix | Delete
$remote_received = $remote_received_seconds + $remote_received_fraction;
[137] Fix | Delete
$remote_transmitted = $remote_transmitted_seconds + $remote_transmitted_fraction;
[138] Fix | Delete
[139] Fix | Delete
$delay = (($local_transmitted - $remote_originate) / 2) - ($remote_transmitted - $remote_received);
[140] Fix | Delete
[141] Fix | Delete
$ntp_time = $remote_transmitted - $delay;
[142] Fix | Delete
return $ntp_time;
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
return false;
[146] Fix | Delete
}
[147] Fix | Delete
[148] Fix | Delete
/**
[149] Fix | Delete
* Formats and returns the given timestamp using the time zone set for the WordPress installation.
[150] Fix | Delete
*
[151] Fix | Delete
* @param string $format See the PHP docs on DateTime for the format options.
[152] Fix | Delete
* @param int|bool $timestamp Assumed to be in UTC. If false, defaults to the current timestamp.
[153] Fix | Delete
* @return string
[154] Fix | Delete
*/
[155] Fix | Delete
public static function format_local_time($format, $timestamp = false) {
[156] Fix | Delete
if ($timestamp === false) {
[157] Fix | Delete
$timestamp = self::time();
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
$utc = new \DateTimeZone('UTC');
[161] Fix | Delete
if (!function_exists('date_timestamp_set')) {
[162] Fix | Delete
$dtStr = gmdate("c", (int) $timestamp); //Have to do it this way because of PHP 5.2
[163] Fix | Delete
$dt = new \DateTime($dtStr, $utc);
[164] Fix | Delete
}
[165] Fix | Delete
else {
[166] Fix | Delete
$dt = new \DateTime('now', $utc);
[167] Fix | Delete
$dt->setTimestamp($timestamp);
[168] Fix | Delete
}
[169] Fix | Delete
[170] Fix | Delete
$tz = get_option('timezone_string');
[171] Fix | Delete
if (!empty($tz)) {
[172] Fix | Delete
$dt->setTimezone(new \DateTimeZone($tz));
[173] Fix | Delete
}
[174] Fix | Delete
else {
[175] Fix | Delete
$gmt = get_option('gmt_offset');
[176] Fix | Delete
if (!empty($gmt)) {
[177] Fix | Delete
if (PHP_VERSION_ID < 50510) {
[178] Fix | Delete
$dtStr = gmdate("c", (int) ($timestamp + $gmt * 3600)); //Have to do it this way because of < PHP 5.5.10
[179] Fix | Delete
$dt = new \DateTime($dtStr, $utc);
[180] Fix | Delete
}
[181] Fix | Delete
else {
[182] Fix | Delete
$direction = ($gmt > 0 ? '+' : '-');
[183] Fix | Delete
$gmt = abs($gmt);
[184] Fix | Delete
$h = (int) $gmt;
[185] Fix | Delete
$m = ($gmt - $h) * 60;
[186] Fix | Delete
$dt->setTimezone(new \DateTimeZone($direction . str_pad($h, 2, '0', STR_PAD_LEFT) . str_pad($m, 2, '0', STR_PAD_LEFT)));
[187] Fix | Delete
}
[188] Fix | Delete
}
[189] Fix | Delete
}
[190] Fix | Delete
return $dt->format($format);
[191] Fix | Delete
}
[192] Fix | Delete
}
[193] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function