diff --git a/INSTALL b/INSTALL index 05e5a86b..a16397ca 100644 --- a/INSTALL +++ b/INSTALL @@ -12,7 +12,7 @@ info on setting these up. You need to know how to install, secure, run, maintain, and back up your chosen database system. No optional PHP packages (other than the database system) are required for -this application. PHP Version 5.3.6 or later is required. +this application. PHP Version 5.3.0 or later is required. You can run PHP either as a CGI or with a direct module interface (also called SAPI). These servers include Apache, Microsoft Internet Information Server, diff --git a/web/defaultincludes.inc b/web/defaultincludes.inc index 9a8643db..25a9ede5 100644 --- a/web/defaultincludes.inc +++ b/web/defaultincludes.inc @@ -6,6 +6,7 @@ // are assigned to variables will change depending on the context in which the file // is called. +require_once "lib/autoload.inc"; require "grab_globals.inc.php"; require_once "systemdefaults.inc.php"; require_once "areadefaults.inc.php"; diff --git a/web/functions.inc b/web/functions.inc index f3dfe2ed..6629aba1 100644 --- a/web/functions.inc +++ b/web/functions.inc @@ -313,15 +313,17 @@ function getPeriodInterval($start_time, $end_time) $periods_per_day = count($periods); - $startDate = new DateTime(); + // Need to use the MRBS version of DateTime to get round a bug in modify() + // in PHP before 5.3.6 + $startDate = new MRBS\DateTime(); $startDate->setTimestamp($start_time); - $endDate = new DateTime(); + $endDate = new MRBS\DateTime(); $endDate->setTimestamp($end_time); // Set both dates to noon so that we can compare them and get an integral // number of days difference. Noon also happens to be when periods start, // so will be useful in a moment. - $startDate->modify('12:00'); // This won't work in PHP < 5.3.6 (bug) + $startDate->modify('12:00'); $endDate->modify('12:00'); // Calculate the difference in days diff --git a/web/internalconfig.inc.php b/web/internalconfig.inc.php index 2495749b..6f133e25 100644 --- a/web/internalconfig.inc.php +++ b/web/internalconfig.inc.php @@ -113,8 +113,7 @@ ********************************************************/ // Check PHP version -// We need 5.3.6 because it fixes a bug that causes DateTime::modify('noon') not to work -$min_PHP_version = '5.3.6'; +$min_PHP_version = '5.3.0'; if (!function_exists('version_compare') || version_compare(PHP_VERSION, $min_PHP_version) < 0) { die("MRBS requires PHP $min_PHP_version or above. This server is running version " . PHP_VERSION . "."); diff --git a/web/lib/MRBS/DateTime.php b/web/lib/MRBS/DateTime.php new file mode 100644 index 00000000..ab8212c1 --- /dev/null +++ b/web/lib/MRBS/DateTime.php @@ -0,0 +1,75 @@ += 0) + { + return parent::modify($modify); + } + + $date = getdate($this->getTimestamp()); + $modification = self::parse($modify); + + foreach ($modification as $unit => $amount) + { + switch($amount['mode']) + { + case 'absolute': + $date[$unit] = $amount['quantity']; + break; + case 'relative': + $date[$unit] = $date[$unit] + $amount['quantity']; + break; + default: + throw new Exception ("Unknown mode '" . $amount['mode'] . "'"); + break; + } + } + + $modified_timestamp = mktime($date['hours'], $date['minutes'], $date['seconds'], + $date['mon'], $date['mday'], $date['year']); + + return $this->setTimestamp($modified_timestamp); + } + + + // Parse the $modify string and return an array of any modifications that are necessary. + // The array is indexed at the top level by 'hours', 'minutes', 'seconds', 'mon', 'mday' and + // 'year' - ie the same keys that the output of getdate() uses. Each value is itself an array, + // indexed by 'mode' (can be 'relative' or 'absolute') and then 'quantity'. If the mode is + // relative then the quantity is added to the original, if absolute then it replaces the original. + private static function parse($modify) + { + $modify = self::map($modify); + + // Test for a simple hh:mm pattern (or hhmm or hh.mm) + $pattern = '/([01][0-9]|[2][0-3])[.:]?([0-5][0-9])/'; + if (preg_match($pattern, $modify, $matches)) + { + return array('hours' => array('mode' => 'absolute', + 'quantity' => $matches[1]), + 'minutes' => array('mode' => 'absolute', + 'quantity' => $matches[2])); + } + + // Could add more tests later if need be. + throw new Exception("Modify string '$modify' not supported by MRBS"); + } + + + // Replace some simple modify strings with their numeric alternatives. + private static function map($modify) + { + $mappings = array('midnight' => '00:00', + 'noon' => '12:00'); + + return (isset($mappings[$modify])) ? $mappings[$modify] : $modify; + } +} diff --git a/web/lib/autoload.inc b/web/lib/autoload.inc new file mode 100644 index 00000000..705f6932 --- /dev/null +++ b/web/lib/autoload.inc @@ -0,0 +1,17 @@ +setTimestamp($report_start)->modify('12:00'); - $endDate = new DateTime(); + $endDate = new MRBS\DateTime(); $endDate->setTimestamp($report_end)->modify('12:00'); $endDate->sub(new DateInterval('P1D')); // Go back one day because the $report_end is at 00:00 the day after $endDate->add(new DateInterval('PT' . $periods_per_day . 'M'));