summaryrefslogtreecommitdiffstats
path: root/system/helpers
diff options
context:
space:
mode:
Diffstat (limited to 'system/helpers')
-rw-r--r--system/helpers/captcha_helper.php68
-rw-r--r--system/helpers/date_helper.php4
-rw-r--r--system/helpers/file_helper.php2
-rw-r--r--system/helpers/form_helper.php192
-rw-r--r--system/helpers/html_helper.php2
-rw-r--r--system/helpers/url_helper.php30
6 files changed, 135 insertions, 163 deletions
diff --git a/system/helpers/captcha_helper.php b/system/helpers/captcha_helper.php
index f3b9c6cc4..ea46f97b3 100644
--- a/system/helpers/captcha_helper.php
+++ b/system/helpers/captcha_helper.php
@@ -43,15 +43,31 @@ if ( ! function_exists('create_captcha'))
/**
* Create CAPTCHA
*
- * @param array array of data for the CAPTCHA
- * @param string path to create the image in
- * @param string URL to the CAPTCHA image folder
- * @param string server path to font
+ * @param array $data data for the CAPTCHA
+ * @param string $img_path path to create the image in
+ * @param string $img_url URL to the CAPTCHA image folder
+ * @param string $font_path server path to font
* @return string
*/
function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
{
- $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200, 'word_length' => 8, 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
+ $defaults = array(
+ 'word' => '',
+ 'img_path' => '',
+ 'img_url' => '',
+ 'img_width' => '150',
+ 'img_height' => '30',
+ 'font_path' => '',
+ 'expiration' => 7200,
+ 'word_length' => 8,
+ 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
+ 'colors' => array(
+ 'background' => array(255,255,255),
+ 'border' => array(153,102,102),
+ 'text' => array(204,153,153),
+ 'grid' => array(255,182,182)
+ )
+ );
foreach ($defaults as $key => $val)
{
@@ -110,9 +126,9 @@ if ( ! function_exists('create_captcha'))
// Determine angle and position
// -----------------------------------
$length = strlen($word);
- $angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
- $x_axis = rand(6, (360/$length)-16);
- $y_axis = ($angle >= 0) ? rand($img_height, $img_width) : rand(6, $img_height);
+ $angle = ($length >= 6) ? mt_rand(-($length-6), ($length-6)) : 0;
+ $x_axis = mt_rand(6, (360/$length)-16);
+ $y_axis = ($angle >= 0) ? mt_rand($img_height, $img_width) : mt_rand(6, $img_height);
// Create image
// PHP.net recommends imagecreatetruecolor(), but it isn't always available
@@ -122,15 +138,19 @@ if ( ! function_exists('create_captcha'))
// -----------------------------------
// Assign colors
- // -----------------------------------
- $bg_color = imagecolorallocate($im, 255, 255, 255);
- $border_color = imagecolorallocate($im, 153, 102, 102);
- $text_color = imagecolorallocate($im, 204, 153, 153);
- $grid_color = imagecolorallocate($im, 255, 182, 182);
- $shadow_color = imagecolorallocate($im, 255, 240, 240);
+ // ----------------------------------
+
+ is_array($colors) OR $colors = $defaults['colors'];
+
+ foreach (array_keys($defaults['colors']) as $key)
+ {
+ // Check for a possible missing value
+ is_array($colors[$key]) OR $colors[$key] = $defaults['colors'][$key];
+ $colors[$key] = imagecolorallocate($im, $colors[$key][0], $colors[$key][1], $colors[$key][2]);
+ }
- // Create the rectangle
- ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
+ // Create the rectangle
+ ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $colors['background']);
// -----------------------------------
// Create the spiral pattern
@@ -151,7 +171,7 @@ if ( ! function_exists('create_captcha'))
$rad1 = $radius * (($i + 1) / $points);
$x1 = ($rad1 * cos($theta)) + $x_axis;
$y1 = ($rad1 * sin($theta)) + $y_axis;
- imageline($im, $x, $y, $x1, $y1, $grid_color);
+ imageline($im, $x, $y, $x1, $y1, $colors['grid']);
$theta -= $thetac;
}
@@ -163,13 +183,13 @@ if ( ! function_exists('create_captcha'))
if ($use_font === FALSE)
{
$font_size = 5;
- $x = rand(0, $img_width / ($length / 3));
+ $x = mt_rand(0, $img_width / ($length / 3));
$y = 0;
}
else
{
$font_size = 16;
- $x = rand(0, $img_width / ($length / 1.5));
+ $x = mt_rand(0, $img_width / ($length / 1.5));
$y = $font_size + 2;
}
@@ -177,20 +197,20 @@ if ( ! function_exists('create_captcha'))
{
if ($use_font === FALSE)
{
- $y = rand(0 , $img_height / 2);
- imagestring($im, $font_size, $x, $y, $word[$i], $text_color);
+ $y = mt_rand(0 , $img_height / 2);
+ imagestring($im, $font_size, $x, $y, $word[$i], $colors['text']);
$x += ($font_size * 2);
}
else
{
- $y = rand($img_height / 2, $img_height - 3);
- imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, $word[$i]);
+ $y = mt_rand($img_height / 2, $img_height - 3);
+ imagettftext($im, $font_size, $angle, $x, $y, $colors['text'], $font_path, $word[$i]);
$x += $font_size;
}
}
// Create the border
- imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $border_color);
+ imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $colors['border']);
// -----------------------------------
// Generate the image
diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php
index 41a7ab635..c3a8d3c9e 100644
--- a/system/helpers/date_helper.php
+++ b/system/helpers/date_helper.php
@@ -677,7 +677,7 @@ if ( ! function_exists('date_range'))
$is_unix = ! ( ! $is_unix OR $is_unix === 'days');
// Validate input and try strtotime() on invalid timestamps/intervals, just in case
- if ( ( ! ctype_digit((string) $unix_start) && ($unix_start = @strtotime($unix_time)) === FALSE)
+ if ( ( ! ctype_digit((string) $unix_start) && ($unix_start = @strtotime($unix_start)) === FALSE)
OR ( ! ctype_digit((string) $mixed) && ($is_unix === FALSE OR ($mixed = @strtotime($mixed)) === FALSE))
OR ($is_unix === TRUE && $mixed < $unix_start))
{
@@ -686,7 +686,7 @@ if ( ! function_exists('date_range'))
if ($is_unix && ($unix_start == $mixed OR date($format, $unix_start) === date($format, $mixed)))
{
- return array($start_date);
+ return array(date($format, $unix_start));
}
$range = array();
diff --git a/system/helpers/file_helper.php b/system/helpers/file_helper.php
index 1b8efff27..4b45a62d0 100644
--- a/system/helpers/file_helper.php
+++ b/system/helpers/file_helper.php
@@ -275,7 +275,7 @@ if ( ! function_exists('get_file_info'))
switch ($key)
{
case 'name':
- $fileinfo['name'] = substr(strrchr($file, DIRECTORY_SEPARATOR), 1);
+ $fileinfo['name'] = basename($file);
break;
case 'server_path':
$fileinfo['server_path'] = $file;
diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php
index 2002d4269..85f1f4e01 100644
--- a/system/helpers/form_helper.php
+++ b/system/helpers/form_helper.php
@@ -50,15 +50,10 @@ if ( ! function_exists('form_open'))
* @param array a key/value pair hidden data
* @return string
*/
- function form_open($action = '', $attributes = '', $hidden = array())
+ function form_open($action = '', $attributes = array(), $hidden = array())
{
$CI =& get_instance();
- if ($attributes === '')
- {
- $attributes = 'method="post"';
- }
-
// If an action is not a full URL then turn it into one
if ($action && strpos($action, '://') === FALSE)
{
@@ -70,10 +65,22 @@ if ( ! function_exists('form_open'))
$action = $CI->config->site_url($CI->uri->uri_string());
}
- $form = '<form action="'.$action.'"'._attributes_to_string($attributes, TRUE).">\n";
+ $attributes = _attributes_to_string($attributes);
+
+ if (stripos($attributes, 'method=') === FALSE)
+ {
+ $attributes .= ' method="post"';
+ }
+
+ if (stripos($attributes, 'accept-charset=') === FALSE)
+ {
+ $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
+ }
+
+ $form = '<form action="'.$action.'"'.$attributes.">\n";
// Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
- if ($CI->config->item('csrf_protection') === TRUE && ! (strpos($action, $CI->config->base_url()) === FALSE OR strpos($form, 'method="get"')))
+ if ($CI->config->item('csrf_protection') === TRUE && ! (strpos($action, $CI->config->base_url()) === FALSE OR stripos($form, 'method="get"')))
{
$hidden[$CI->security->get_csrf_token_name()] = $CI->security->get_csrf_hash();
}
@@ -321,11 +328,8 @@ if ( ! function_exists('form_dropdown'))
{
$selected = array($_POST[$name]);
}
-
- if ($extra != '')
- {
- $extra = ' '.$extra;
- }
+
+ $extra = _attributes_to_string($extra);
$multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
@@ -542,12 +546,12 @@ if ( ! function_exists('form_fieldset'))
* use form_fieldset_close()
*
* @param string The legend text
- * @param string Additional attributes
+ * @param array Additional attributes
* @return string
*/
function form_fieldset($legend_text = '', $attributes = array())
{
- $fieldset = '<fieldset'._attributes_to_string($attributes, FALSE).">\n";
+ $fieldset = '<fieldset'._attributes_to_string($attributes).">\n";
if ($legend_text !== '')
{
return $fieldset.'<legend>'.$legend_text."</legend>\n";
@@ -668,37 +672,22 @@ if ( ! function_exists('set_select'))
*/
function set_select($field = '', $value = '', $default = FALSE)
{
- $OBJ =& _get_validation_object();
+ $CI =& get_instance();
- if ($OBJ === FALSE)
+ if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
+ {
+ return $CI->form_validation->set_select($field, $value, $default);
+ }
+ elseif (($input = $CI->input->post($field, FALSE)) === NULL)
+ {
+ return ($default === TRUE) ? ' selected="selected"' : '';
+ }
+ elseif (is_array($input) && in_array($value, $input, TRUE))
{
- if ( ! isset($_POST[$field]))
- {
- if (count($_POST) === 0 && $default === TRUE)
- {
- return ' selected="selected"';
- }
- return '';
- }
-
- $field = $_POST[$field];
-
- if (is_array($field))
- {
- if ( ! in_array($value, $field))
- {
- return '';
- }
- }
- elseif (($field == '' OR $value == '') OR $field !== $value)
- {
- return '';
- }
-
return ' selected="selected"';
}
- return $OBJ->set_select($field, $value, $default);
+ return ($input === $value) ? ' selected="selected"' : '';
}
}
@@ -719,37 +708,22 @@ if ( ! function_exists('set_checkbox'))
*/
function set_checkbox($field = '', $value = '', $default = FALSE)
{
- $OBJ =& _get_validation_object();
+ $CI =& get_instance();
- if ($OBJ === FALSE)
+ if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
+ {
+ return $CI->form_validation->set_checkbox($field, $value, $default);
+ }
+ elseif (($input = $CI->input->post($field, FALSE)) === NULL)
+ {
+ return ($default === TRUE) ? ' checked="checked"' : '';
+ }
+ elseif (is_array($input) && in_array($value, $input, TRUE))
{
- if ( ! isset($_POST[$field]))
- {
- if (count($_POST) === 0 && $default === TRUE)
- {
- return ' checked="checked"';
- }
- return '';
- }
-
- $field = $_POST[$field];
-
- if (is_array($field))
- {
- if ( ! in_array($value, $field))
- {
- return '';
- }
- }
- elseif (($field == '' OR $value == '') OR $field !== $value)
- {
- return '';
- }
-
return ' checked="checked"';
}
- return $OBJ->set_checkbox($field, $value, $default);
+ return ($input === $value) ? ' checked="checked"' : '';
}
}
@@ -763,47 +737,25 @@ if ( ! function_exists('set_radio'))
* Let's you set the selected value of a radio field via info in the POST array.
* If Form Validation is active it retrieves the info from the validation class
*
- * @param string
- * @param string
- * @param bool
+ * @param string $field
+ * @param string $value
+ * @param bool $default
* @return string
*/
function set_radio($field = '', $value = '', $default = FALSE)
{
- $OBJ =& _get_validation_object();
+ $CI =& get_instance();
- if ($OBJ === FALSE)
+ if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
{
- if ( ! isset($_POST[$field]))
- {
- if (count($_POST) === 0 && $default === TRUE)
- {
- return ' checked="checked"';
- }
- return '';
- }
-
- $field = $_POST[$field];
-
- if (is_array($field))
- {
- if ( ! in_array($value, $field))
- {
- return '';
- }
- }
- else
- {
- if (($field == '' OR $value == '') OR $field !== $value)
- {
- return '';
- }
- }
-
- return ' checked="checked"';
+ return $CI->form_validation->set_radio($field, $value, $default);
+ }
+ elseif (($input = $CI->input->post($field, FALSE)) === NULL)
+ {
+ return ($default === TRUE) ? ' checked="checked"' : '';
}
- return $OBJ->set_radio($field, $value, $default);
+ return ($input === $value) ? ' checked="checked"' : '';
}
}
@@ -920,45 +872,24 @@ if ( ! function_exists('_attributes_to_string'))
* Helper function used by some of the form helpers
*
* @param mixed
- * @param bool
* @return string
*/
- function _attributes_to_string($attributes, $formtag = FALSE)
+ function _attributes_to_string($attributes)
{
- if (is_string($attributes) && strlen($attributes) > 0)
+ if (empty($attributes))
{
- if ($formtag === TRUE && strpos($attributes, 'method=') === FALSE)
- {
- $attributes .= ' method="post"';
- }
-
- if ($formtag === TRUE && strpos($attributes, 'accept-charset=') === FALSE)
- {
- $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
- }
-
- return ' '.$attributes;
+ return '';
}
- if (is_object($attributes) && count($attributes) > 0)
+ if (is_object($attributes))
{
$attributes = (array) $attributes;
}
- if (is_array($attributes) && ($formtag === TRUE OR count($attributes) > 0))
+ if (is_array($attributes))
{
$atts = '';
- if ( ! isset($attributes['method']) && $formtag === TRUE)
- {
- $atts .= ' method="post"';
- }
-
- if ( ! isset($attributes['accept-charset']) && $formtag === TRUE)
- {
- $atts .= ' accept-charset="'.strtolower(config_item('charset')).'"';
- }
-
foreach ($attributes as $key => $val)
{
$atts .= ' '.$key.'="'.$val.'"';
@@ -966,6 +897,13 @@ if ( ! function_exists('_attributes_to_string'))
return $atts;
}
+
+ if (is_string($attributes))
+ {
+ return ' '.$attributes;
+ }
+
+ return FALSE;
}
}
@@ -988,7 +926,7 @@ if ( ! function_exists('_get_validation_object'))
// We set this as a variable since we're returning by reference.
$return = FALSE;
- if (FALSE !== ($object = $CI->load->is_loaded('form_validation')))
+ if (FALSE !== ($object = $CI->load->is_loaded('Form_validation')))
{
if ( ! isset($CI->$object) OR ! is_object($CI->$object))
{
diff --git a/system/helpers/html_helper.php b/system/helpers/html_helper.php
index 80a27876f..ece39584b 100644
--- a/system/helpers/html_helper.php
+++ b/system/helpers/html_helper.php
@@ -109,7 +109,7 @@ if ( ! function_exists('_list'))
* @param int
* @return string
*/
- function _list($type = 'ul', $list, $attributes = '', $depth = 0)
+ function _list($type = 'ul', $list = array(), $attributes = '', $depth = 0)
{
// If an array wasn't submitted there's nothing to do...
if ( ! is_array($list))
diff --git a/system/helpers/url_helper.php b/system/helpers/url_helper.php
index d0fab3fe0..fbb4a1b24 100644
--- a/system/helpers/url_helper.php
+++ b/system/helpers/url_helper.php
@@ -46,13 +46,20 @@ if ( ! function_exists('site_url'))
* Create a local URL based on your basepath. Segments can be passed via the
* first parameter either as a string or an array.
*
- * @param string
+ * @param string $uri
+ * @param string $protocol
* @return string
*/
- function site_url($uri = '')
+ function site_url($uri = '', $protocol = NULL)
{
- $CI =& get_instance();
- return $CI->config->site_url($uri);
+ $uri = get_instance()->config->site_url($uri);
+
+ if (isset($protocol))
+ {
+ return $protocol.substr($uri, strpos($uri, '://'));
+ }
+
+ return $uri;
}
}
@@ -67,13 +74,20 @@ if ( ! function_exists('base_url'))
* Segments can be passed in as a string or an array, same as site_url
* or a URL to a file can be passed in, e.g. to an image file.
*
- * @param string
+ * @param string $uri
+ * @param string $protocol
* @return string
*/
- function base_url($uri = '')
+ function base_url($uri = '', $protocol = NULL)
{
- $CI =& get_instance();
- return $CI->config->base_url($uri);
+ $uri = get_instance()->config->base_url($uri);
+
+ if (isset($protocol))
+ {
+ return $protocol.substr($uri, strpos($uri, '://'));
+ }
+
+ return $uri;
}
}