summaryrefslogtreecommitdiffstats
path: root/system/helpers
diff options
context:
space:
mode:
Diffstat (limited to 'system/helpers')
-rw-r--r--system/helpers/date_helper.php187
-rw-r--r--system/helpers/download_helper.php9
-rw-r--r--system/helpers/file_helper.php56
-rw-r--r--system/helpers/form_helper.php15
-rw-r--r--system/helpers/path_helper.php2
-rw-r--r--system/helpers/security_helper.php3
6 files changed, 96 insertions, 176 deletions
diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php
index c601dc9e5..077a6712c 100644
--- a/system/helpers/date_helper.php
+++ b/system/helpers/date_helper.php
@@ -42,29 +42,28 @@ if ( ! function_exists('now'))
/**
* Get "now" time
*
- * Returns time() or its GMT equivalent based on the config file preference
+ * Returns time() based on the timezone parameter or on the
+ * "time_reference" setting
*
+ * @param string
* @return int
*/
- function now()
+ function now($timezone = NULL)
{
- $CI =& get_instance();
-
- if (strtolower($CI->config->item('time_reference')) === 'gmt')
+ if (empty($timezone))
{
- $now = time();
- $system_time = mktime(gmdate("H", $now), gmdate("i", $now), gmdate("s", $now), gmdate("m", $now), gmdate("d", $now), gmdate("Y", $now));
-
- if (strlen($system_time) < 10)
- {
- $system_time = time();
- log_message('error', 'The Date class could not set a proper GMT timestamp so the local time() value was used.');
- }
+ $timezone = config_item('time_reference');
+ }
- return $system_time;
+ if ($timezone === 'local' OR $timezone === date_default_timezone_get())
+ {
+ return time();
}
- return time();
+ $datetime = new DateTime('now', new DateTimeZone($timezone));
+ sscanf($datetime->format('j-n-Y G:i:s'), '%d-%d-%d %d:%d:%d', $day, $month, $year, $hour, $minute, $second);
+
+ return mktime($hour, $minute, $second, $month, $day, $year);
}
}
@@ -94,8 +93,10 @@ if ( ! function_exists('mdate'))
{
return '';
}
-
- $time = ($time === '') ? now() : $time;
+ elseif (empty($time))
+ {
+ $time = now();
+ }
$datestr = str_replace(
'%\\',
@@ -123,24 +124,19 @@ if ( ! function_exists('standard_date'))
function standard_date($fmt = 'DATE_RFC822', $time = '')
{
$formats = array(
- 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%O',
- 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC',
- 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%O',
- 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O',
- 'DATE_RFC850' => '%l, %d-%M-%y %H:%i:%s UTC',
- 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O',
- 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O',
- 'DATE_RFC2822' => '%D, %d %M %Y %H:%i:%s %O',
- 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O',
- 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%O'
- );
-
- if ( ! isset($formats[$fmt]))
- {
- return FALSE;
- }
-
- return mdate($formats[$fmt], $time);
+ 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%O',
+ 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC',
+ 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%O',
+ 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O',
+ 'DATE_RFC850' => '%l, %d-%M-%y %H:%i:%s UTC',
+ 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O',
+ 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O',
+ 'DATE_RFC2822' => '%D, %d %M %Y %H:%i:%s %O',
+ 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O',
+ 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%O'
+ );
+
+ return isset($formats[$fmt]) ? mdate($formats[$fmt], $time) : FALSE;
}
}
@@ -164,20 +160,9 @@ if ( ! function_exists('timespan'))
$CI =& get_instance();
$CI->lang->load('date');
- if ( ! is_numeric($seconds))
- {
- $seconds = 1;
- }
-
- if ( ! is_numeric($time))
- {
- $time = time();
- }
-
- if ( ! is_numeric($units))
- {
- $units = 7;
- }
+ is_numeric($seconds) OR $seconds = 1;
+ is_numeric($time) OR $time = time();
+ is_numeric($units) OR $units = 7;
$seconds = ($time <= $seconds) ? 1 : $time - $seconds;
@@ -186,7 +171,7 @@ if ( ! function_exists('timespan'))
if ($years > 0)
{
- $str[] = $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year'));
+ $str[] = $years.' '.$CI->lang->line($years > 1 ? 'date_years' : 'date_year');
}
$seconds -= $years * 31557600;
@@ -196,7 +181,7 @@ if ( ! function_exists('timespan'))
{
if ($months > 0)
{
- $str[] = $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month'));
+ $str[] = $months.' '.$CI->lang->line($months > 1 ? 'date_months' : 'date_month');
}
$seconds -= $months * 2629743;
@@ -208,7 +193,7 @@ if ( ! function_exists('timespan'))
{
if ($weeks > 0)
{
- $str[] = $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week'));
+ $str[] = $weeks.' '.$CI->lang->line($weeks > 1 ? 'date_weeks' : 'date_week');
}
$seconds -= $weeks * 604800;
@@ -220,7 +205,7 @@ if ( ! function_exists('timespan'))
{
if ($days > 0)
{
- $str[] = $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day'));
+ $str[] = $days.' '.$CI->lang->line($days > 1 ? 'date_days' : 'date_day');
}
$seconds -= $days * 86400;
@@ -232,7 +217,7 @@ if ( ! function_exists('timespan'))
{
if ($hours > 0)
{
- $str[] = $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour'));
+ $str[] = $hours.' '.$CI->lang->line($hours > 1 ? 'date_hours' : 'date_hour');
}
$seconds -= $hours * 3600;
@@ -244,7 +229,7 @@ if ( ! function_exists('timespan'))
{
if ($minutes > 0)
{
- $str[] = $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute'));
+ $str[] = $minutes.' '.$CI->lang->line($minutes > 1 ? 'date_minutes' : 'date_minute');
}
$seconds -= $minutes * 60;
@@ -252,7 +237,7 @@ if ( ! function_exists('timespan'))
if (count($str) === 0)
{
- $str[] = $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second'));
+ $str[] = $seconds.' '.$CI->lang->line($seconds > 1 ? 'date_seconds' : 'date_second');
}
return implode(', ', $str);
@@ -279,12 +264,16 @@ if ( ! function_exists('days_in_month'))
{
return 0;
}
-
- if ( ! is_numeric($year) OR strlen($year) !== 4)
+ elseif ( ! is_numeric($year) OR strlen($year) !== 4)
{
$year = date('Y');
}
+ if ($year >= 1970)
+ {
+ return (int) date('t', mktime(12, 0, 0, $month, 1, $year));
+ }
+
if ($month == 2)
{
if ($year % 400 === 0 OR ($year % 4 === 0 && $year % 100 !== 0))
@@ -315,13 +304,13 @@ if ( ! function_exists('local_to_gmt'))
$time = time();
}
- return mktime(
- gmdate('H', $time),
- gmdate('i', $time),
- gmdate('s', $time),
- gmdate('m', $time),
- gmdate('d', $time),
- gmdate('Y', $time)
+ return gmmktime(
+ date('G', $time),
+ date('i', $time),
+ date('s', $time),
+ date('n', $time),
+ date('j', $time),
+ date('Y', $time)
);
}
}
@@ -351,12 +340,7 @@ if ( ! function_exists('gmt_to_local'))
$time += timezones($timezone) * 3600;
- if ($dst === TRUE)
- {
- $time += 3600;
- }
-
- return $time;
+ return ($dst === TRUE) ? $time + 3600 : $time;
}
}
@@ -376,9 +360,7 @@ if ( ! function_exists('mysql_to_unix'))
// since the formatting changed with MySQL 4.1
// YYYY-MM-DD HH:MM:SS
- $time = str_replace('-', '', $time);
- $time = str_replace(':', '', $time);
- $time = str_replace(' ', '', $time);
+ $time = str_replace(array('-', ':', ' '), '', $time);
// YYYYMMDDHHMMSS
return mktime(
@@ -408,7 +390,7 @@ if ( ! function_exists('unix_to_human'))
*/
function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
{
- $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
+ $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
if ($fmt === 'us')
{
@@ -426,7 +408,7 @@ if ( ! function_exists('unix_to_human'))
if ($fmt === 'us')
{
- $r .= ' '.date('A', $time);
+ return $r.' '.date('A', $time);
}
return $r;
@@ -454,51 +436,33 @@ if ( ! function_exists('human_to_unix'))
$datestr = preg_replace('/\040+/', ' ', trim($datestr));
- if ( ! preg_match('/^[0-9]{2,4}\-[0-9]{1,2}\-[0-9]{1,2}\s[0-9]{1,2}:[0-9]{1,2}(?::[0-9]{1,2})?(?:\s[AP]M)?$/i', $datestr))
+ if ( ! preg_match('/^(\d{2}|\d{4})\-[0-9]{1,2}\-[0-9]{1,2}\s[0-9]{1,2}:[0-9]{1,2}(?::[0-9]{1,2})?(?:\s[AP]M)?$/i', $datestr))
{
return FALSE;
}
$split = explode(' ', $datestr);
- $ex = explode('-', $split['0']);
-
- $year = (strlen($ex[0]) === 2) ? '20'.$ex[0] : $ex[0];
- $month = (strlen($ex[1]) === 1) ? '0'.$ex[1] : $ex[1];
- $day = (strlen($ex[2]) === 1) ? '0'.$ex[2] : $ex[2];
+ list($year, $month, $day) = explode('-', $split[0]);
$ex = explode(':', $split['1']);
- $hour = (strlen($ex[0]) === 1) ? '0'.$ex[0] : $ex[0];
- $min = (strlen($ex[1]) === 1) ? '0'.$ex[1] : $ex[1];
-
- if (isset($ex[2]) && preg_match('/[0-9]{1,2}/', $ex[2]))
- {
- $sec = (strlen($ex[2]) === 1) ? '0'.$ex[2] : $ex[2];
- }
- else
- {
- // Unless specified, seconds get set to zero.
- $sec = '00';
- }
+ $hour = (int) $ex[0];
+ $min = (int) $ex[1];
+ $sec = ( ! empty($ex[2]) && preg_match('/[0-9]{1,2}/', $ex[2]))
+ ? (int) $ex[2] : 0;
if (isset($split[2]))
{
$ampm = strtolower($split[2]);
- if (substr($ampm, 0, 1) === 'p' && $hour < 12)
+ if ($ampm[0] === 'p' && $hour < 12)
{
$hour += 12;
}
-
- if (substr($ampm, 0, 1) === 'a' && $hour == 12)
+ elseif ($ampm[0] === 'a' && $hour === 12)
{
- $hour = '00';
- }
-
- if (strlen($hour) === 1)
- {
- $hour = '0'.$hour;
+ $hour = 0;
}
}
@@ -526,7 +490,7 @@ if ( ! function_exists('nice_date'))
}
// Date like: YYYYMM
- if (preg_match('/^\d{6}$/', $bad_date))
+ if (preg_match('/^\d{6}$/i', $bad_date))
{
if (in_array(substr($bad_date, 0, 2), array('19', '20')))
{
@@ -543,20 +507,15 @@ if ( ! function_exists('nice_date'))
}
// Date Like: YYYYMMDD
- if (preg_match('/^\d{8}$/', $bad_date))
+ if (preg_match('/^(\d{2})\d{2}(\d{4})$/i', $bad_date, $matches))
{
- $month = substr($bad_date, 0, 2);
- $day = substr($bad_date, 2, 2);
- $year = substr($bad_date, 4, 4);
-
- return date($format, strtotime($month.'/01/'.$year));
+ return date($format, strtotime($matches[1].'/01/'.$matches[2]));
}
// Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
- if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/', $bad_date))
+ if (preg_match('/^(\d{1,2})-(\d{1,2})-(\d{4})$/i', $bad_date, $matches))
{
- list($m, $d, $y) = explode('-', $bad_date);
- return date($format, strtotime($y.'-'.$m.'-'.$d));
+ return date($format, strtotime($matches[3].'-'.$matches[1].'-'.$matches[2]));
}
// Any other kind of string, when converted into UNIX time,
@@ -678,8 +637,6 @@ if ( ! function_exists('timezones'))
return $zones;
}
- $tz = ($tz === 'GMT') ? 'UTC' : $tz;
-
return isset($zones[$tz]) ? $zones[$tz] : 0;
}
}
diff --git a/system/helpers/download_helper.php b/system/helpers/download_helper.php
index 3c677055f..5efbc4930 100644
--- a/system/helpers/download_helper.php
+++ b/system/helpers/download_helper.php
@@ -73,14 +73,7 @@ if ( ! function_exists('force_download'))
}
// Load the mime types
- if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
- {
- $mimes = include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
- }
- elseif (is_file(APPPATH.'config/mimes.php'))
- {
- $mimes = include(APPPATH.'config/mimes.php');
- }
+ $mimes =& get_mimes();
// Only change the default MIME if we can find one
if (isset($mimes[$extension]))
diff --git a/system/helpers/file_helper.php b/system/helpers/file_helper.php
index 068706c30..be616f62d 100644
--- a/system/helpers/file_helper.php
+++ b/system/helpers/file_helper.php
@@ -44,38 +44,15 @@ if ( ! function_exists('read_file'))
*
* Opens the file specfied in the path and returns it as a string.
*
+ * This function is DEPRECATED and should be removed in
+ * CodeIgniter 3.1+. Use file_get_contents() instead.
+ *
* @param string path to file
* @return string
*/
function read_file($file)
{
- if ( ! file_exists($file))
- {
- return FALSE;
- }
-
- if (function_exists('file_get_contents'))
- {
- return file_get_contents($file);
- }
-
- if ( ! $fp = @fopen($file, FOPEN_READ))
- {
- return FALSE;
- }
-
- flock($fp, LOCK_SH);
-
- $data = '';
- if (filesize($file) > 0)
- {
- $data =& fread($fp, filesize($file));
- }
-
- flock($fp, LOCK_UN);
- fclose($fp);
-
- return $data;
+ return @file_get_contents($file);
}
}
@@ -353,32 +330,19 @@ if ( ! function_exists('get_mime_by_extension'))
if ( ! is_array($mimes))
{
- if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
- {
- $mimes = include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
- }
- elseif (is_file(APPPATH.'config/mimes.php'))
- {
- $mimes = include(APPPATH.'config/mimes.php');
- }
+ $mimes =& get_mimes();
- if ( ! is_array($mimes))
+ if (empty($mimes))
{
return FALSE;
}
}
- if (array_key_exists($extension, $mimes))
+ if (isset($mimes[$extension]))
{
- if (is_array($mimes[$extension]))
- {
- // Multiple mime types, just give the first one
- return current($mimes[$extension]);
- }
- else
- {
- return $mimes[$extension];
- }
+ return is_array($mimes[$extension])
+ ? current($mimes[$extension]) // Multiple mime types, just give the first one
+ : $mimes[$extension];
}
return FALSE;
diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php
index 410972187..984634315 100644
--- a/system/helpers/form_helper.php
+++ b/system/helpers/form_helper.php
@@ -325,7 +325,10 @@ if ( ! function_exists('form_dropdown'))
$selected = array($_POST[$name]);
}
- if ($extra !== '') $extra = ' '.$extra;
+ if ($extra != '')
+ {
+ $extra = ' '.$extra;
+ }
$multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
@@ -378,7 +381,7 @@ if ( ! function_exists('form_checkbox'))
{
$checked = $data['checked'];
- if ($checked === FALSE)
+ if ($checked == FALSE)
{
unset($data['checked']);
}
@@ -388,7 +391,7 @@ if ( ! function_exists('form_checkbox'))
}
}
- if ($checked === TRUE)
+ if ($checked == TRUE)
{
$defaults['checked'] = 'checked';
}
@@ -702,7 +705,7 @@ if ( ! function_exists('set_select'))
return '';
}
}
- elseif (($field === '' OR $value === '') OR ($field !== $value))
+ elseif (($field == '' OR $value == '') OR $field !== $value)
{
return '';
}
@@ -753,7 +756,7 @@ if ( ! function_exists('set_checkbox'))
return '';
}
}
- elseif (($field === '' OR $value === '') OR ($field !== $value))
+ elseif (($field == '' OR $value == '') OR $field !== $value)
{
return '';
}
@@ -806,7 +809,7 @@ if ( ! function_exists('set_radio'))
}
else
{
- if (($field === '' OR $value === '') OR ($field !== $value))
+ if (($field == '' OR $value == '') OR $field !== $value)
{
return '';
}
diff --git a/system/helpers/path_helper.php b/system/helpers/path_helper.php
index 13410545c..fec4a1a10 100644
--- a/system/helpers/path_helper.php
+++ b/system/helpers/path_helper.php
@@ -55,7 +55,7 @@ if ( ! function_exists('set_realpath'))
}
// Resolve the path
- if (function_exists('realpath') && @realpath($path) !== FALSE)
+ if (@realpath($path) !== FALSE)
{
$path = realpath($path);
}
diff --git a/system/helpers/security_helper.php b/system/helpers/security_helper.php
index 6187a4a7a..3e6e91435 100644
--- a/system/helpers/security_helper.php
+++ b/system/helpers/security_helper.php
@@ -77,6 +77,9 @@ if ( ! function_exists('do_hash'))
/**
* Hash encode a string
*
+ * This function is DEPRECATED and should be removed in
+ * CodeIgniter 3.1+. Use hash() instead.
+ *
* @param string
* @param string
* @return string