diff options
author | Andrey Andreev <narf@devilix.net> | 2014-01-20 14:03:43 +0100 |
---|---|---|
committer | Andrey Andreev <narf@devilix.net> | 2014-01-20 14:03:43 +0100 |
commit | ea801ab4ab80042638ffddc6056483a1ec43fa80 (patch) | |
tree | f75f30c0df6a8f861ca7df22af09fa3240b0bbd6 /system/helpers | |
parent | 1c08d557a21ecb0f79cd1a1de4e06817a26e0537 (diff) | |
parent | 4d0571666d03511ac5b4a1f2a6882ccb1509a209 (diff) |
Merge branch 'develop' into feature/user-guide-cleanup
Diffstat (limited to 'system/helpers')
-rw-r--r-- | system/helpers/captcha_helper.php | 24 | ||||
-rw-r--r-- | system/helpers/cookie_helper.php | 13 | ||||
-rw-r--r-- | system/helpers/file_helper.php | 12 | ||||
-rw-r--r-- | system/helpers/form_helper.php | 194 | ||||
-rw-r--r-- | system/helpers/html_helper.php | 6 | ||||
-rw-r--r-- | system/helpers/language_helper.php | 3 | ||||
-rw-r--r-- | system/helpers/security_helper.php | 9 | ||||
-rw-r--r-- | system/helpers/text_helper.php | 31 | ||||
-rw-r--r-- | system/helpers/url_helper.php | 39 |
9 files changed, 139 insertions, 192 deletions
diff --git a/system/helpers/captcha_helper.php b/system/helpers/captcha_helper.php index 2d2ae7751..24cd53568 100644 --- a/system/helpers/captcha_helper.php +++ b/system/helpers/captcha_helper.php @@ -126,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 @@ -142,7 +142,7 @@ if ( ! function_exists('create_captcha')) is_array($colors) OR $colors = $defaults['colors']; - foreach (array_keys($default['colors']) as $key) + foreach (array_keys($defaults['colors']) as $key) { // Check for a possible missing value is_array($colors[$key]) OR $colors[$key] = $defaults['colors'][$key]; @@ -183,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; } @@ -197,13 +197,13 @@ if ( ! function_exists('create_captcha')) { if ($use_font === FALSE) { - $y = rand(0 , $img_height / 2); + $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); + $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; } @@ -215,12 +215,12 @@ if ( ! function_exists('create_captcha')) // ----------------------------------- // Generate the image // ----------------------------------- - $img_name = $now.'.jpg'; - ImageJPEG($im, $img_path.$img_name); - $img = '<img src="'.$img_url.$img_name.'" style="width: '.$img_width.'; height: '.$img_height .'; border: 0;" alt=" " />'; + $img_filename = $now.'.jpg'; + ImageJPEG($im, $img_path.$img_filename); + $img = '<img src="'.$img_url.$img_filename.'" style="width: '.$img_width.'; height: '.$img_height .'; border: 0;" alt=" " />'; ImageDestroy($im); - return array('word' => $word, 'time' => $now, 'image' => $img); + return array('word' => $word, 'time' => $now, 'image' => $img, 'filename' => $img_filename); } } diff --git a/system/helpers/cookie_helper.php b/system/helpers/cookie_helper.php index e5cf6b1d6..a79083a63 100644 --- a/system/helpers/cookie_helper.php +++ b/system/helpers/cookie_helper.php @@ -56,11 +56,10 @@ if ( ! function_exists('set_cookie')) * @param bool true makes the cookie accessible via http(s) only (no javascript) * @return void */ - function set_cookie($name = '', $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE, $httponly = FALSE) + function set_cookie($name, $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE, $httponly = FALSE) { // Set the config file options - $CI =& get_instance(); - $CI->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure, $httponly); + get_instance()->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure, $httponly); } } @@ -75,11 +74,11 @@ if ( ! function_exists('get_cookie')) * @param bool * @return mixed */ - function get_cookie($index = '', $xss_clean = FALSE) + function get_cookie($index, $xss_clean = NULL) { - $CI =& get_instance(); + is_bool($xss_clean) OR $xss_clean = (config_item('global_xss_filtering') === TRUE); $prefix = isset($_COOKIE[$index]) ? '' : config_item('cookie_prefix'); - return $CI->input->cookie($prefix.$index, $xss_clean); + return get_instance()->input->cookie($prefix.$index, $xss_clean); } } @@ -96,7 +95,7 @@ if ( ! function_exists('delete_cookie')) * @param string the cookie prefix * @return void */ - function delete_cookie($name = '', $domain = '', $path = '/', $prefix = '') + function delete_cookie($name, $domain = '', $path = '/', $prefix = '') { set_cookie($name, '', '', $domain, $path, $prefix); } diff --git a/system/helpers/file_helper.php b/system/helpers/file_helper.php index 4b45a62d0..0587740b1 100644 --- a/system/helpers/file_helper.php +++ b/system/helpers/file_helper.php @@ -79,11 +79,19 @@ if ( ! function_exists('write_file')) } flock($fp, LOCK_EX); - fwrite($fp, $data); + + for ($written = 0, $length = strlen($data); $written < $length; $written += $result) + { + if (($result = fwrite($fp, substr($data, $written))) === FALSE) + { + break; + } + } + flock($fp, LOCK_UN); fclose($fp); - return TRUE; + return is_int($result); } } diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index 2002d4269..a3d299b0d 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(); } @@ -309,7 +316,7 @@ if ( ! function_exists('form_dropdown')) { isset($name['options']) OR $name['options'] = array(); isset($name['selected']) OR $name['selected'] = array(); - isset($name['extra']) OR $name['extra'] = array(); + isset($name['extra']) OR $name['extra'] = ''; return form_dropdown($name['name'], $name['options'], $name['selected'], $name['extra']); } @@ -322,10 +329,7 @@ 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,33 @@ 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)) { - if ( ! isset($_POST[$field])) - { - if (count($_POST) === 0 && $default === TRUE) - { - return ' selected="selected"'; - } - return ''; - } - - $field = $_POST[$field]; + return $CI->form_validation->set_select($field, $value, $default); + } + elseif (($input = $CI->input->post($field, FALSE)) === NULL) + { + return ($default === TRUE) ? ' selected="selected"' : ''; + } - if (is_array($field)) + $value = (string) $value; + if (is_array($input)) + { + // Note: in_array('', array(0)) returns TRUE, do not use it + foreach ($input as &$v) { - if ( ! in_array($value, $field)) + if ($value === $v) { - return ''; + return ' selected="selected"'; } } - elseif (($field == '' OR $value == '') OR $field !== $value) - { - return ''; - } - return ' selected="selected"'; + return ''; } - return $OBJ->set_select($field, $value, $default); + return ($input === $value) ? ' selected="selected"' : ''; } } @@ -719,37 +719,33 @@ 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)) { - if ( ! isset($_POST[$field])) - { - if (count($_POST) === 0 && $default === TRUE) - { - return ' checked="checked"'; - } - return ''; - } - - $field = $_POST[$field]; + return $CI->form_validation->set_checkbox($field, $value, $default); + } + elseif (($input = $CI->input->post($field, FALSE)) === NULL) + { + return ($default === TRUE) ? ' checked="checked"' : ''; + } - if (is_array($field)) + $value = (string) $value; + if (is_array($input)) + { + // Note: in_array('', array(0)) returns TRUE, do not use it + foreach ($input as &$v) { - if ( ! in_array($value, $field)) + if ($value === $v) { - return ''; + return ' checked="checked"'; } } - elseif (($field == '' OR $value == '') OR $field !== $value) - { - return ''; - } - return ' checked="checked"'; + return ''; } - return $OBJ->set_checkbox($field, $value, $default); + return ($input === $value) ? ' checked="checked"' : ''; } } @@ -763,47 +759,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 === (string) $value) ? ' checked="checked"' : ''; } } @@ -920,45 +894,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 +919,13 @@ if ( ! function_exists('_attributes_to_string')) return $atts; } + + if (is_string($attributes)) + { + return ' '.$attributes; + } + + return FALSE; } } @@ -988,7 +948,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 ece39584b..988eee715 100644 --- a/system/helpers/html_helper.php +++ b/system/helpers/html_helper.php @@ -199,15 +199,13 @@ if ( ! function_exists('img')) { if ($k === 'src' && strpos($v, '://') === FALSE) { - $CI =& get_instance(); - if ($index_page === TRUE) { - $img .= ' src="'.$CI->config->site_url($v).'"'; + $img .= ' src="'.get_instance()->config->site_url($v).'"'; } else { - $img .= ' src="'.$CI->config->slash_item('base_url').$v.'"'; + $img .= ' src="'.get_instance()->config->slash_item('base_url').$v.'"'; } } else diff --git a/system/helpers/language_helper.php b/system/helpers/language_helper.php index 4d571a71c..d7aa8e638 100644 --- a/system/helpers/language_helper.php +++ b/system/helpers/language_helper.php @@ -52,8 +52,7 @@ if ( ! function_exists('lang')) */ function lang($line, $for = '', $attributes = array()) { - $CI =& get_instance(); - $line = $CI->lang->line($line); + $line = get_instance()->lang->line($line); if ($for !== '') { diff --git a/system/helpers/security_helper.php b/system/helpers/security_helper.php index 4bb94a201..7a6df5420 100644 --- a/system/helpers/security_helper.php +++ b/system/helpers/security_helper.php @@ -49,8 +49,7 @@ if ( ! function_exists('xss_clean')) */ function xss_clean($str, $is_image = FALSE) { - $CI =& get_instance(); - return $CI->security->xss_clean($str, $is_image); + return get_instance()->security->xss_clean($str, $is_image); } } @@ -66,8 +65,7 @@ if ( ! function_exists('sanitize_filename')) */ function sanitize_filename($filename) { - $CI =& get_instance(); - return $CI->security->sanitize_filename($filename); + return get_instance()->security->sanitize_filename($filename); } } @@ -107,8 +105,7 @@ if ( ! function_exists('strip_image_tags')) */ function strip_image_tags($str) { - $CI =& get_instance(); - return $CI->security->strip_image_tags($str); + return get_instance()->security->strip_image_tags($str); } } diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index b2351db95..bda844630 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -127,7 +127,7 @@ if ( ! function_exists('ascii_to_entities')) function ascii_to_entities($str) { $out = ''; - for ($i = 0, $s = strlen($str), $count = 1, $temp = array(); $i < $s; $i++) + for ($i = 0, $s = strlen($str) - 1, $count = 1, $temp = array(); $i <= $s; $i++) { $ordinal = ord($str[$i]); @@ -164,6 +164,11 @@ if ( ! function_exists('ascii_to_entities')) $count = 1; $temp = array(); } + // If this is the last iteration, just output whatever we have + elseif ($i === $s) + { + $out .= '&#'.implode(';', $temp).';'; + } } } @@ -329,25 +334,17 @@ if ( ! function_exists('highlight_phrase')) * * Highlights a phrase within a text string * - * @param string the text string - * @param string the phrase you'd like to highlight - * @param string the openging tag to precede the phrase with - * @param string the closing tag to end the phrase with + * @param string $str the text string + * @param string $phrase the phrase you'd like to highlight + * @param string $tag_open the openging tag to precede the phrase with + * @param string $tag_close the closing tag to end the phrase with * @return string */ - function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>') + function highlight_phrase($str, $phrase, $tag_open = '<mark>', $tag_close = '</mark>') { - if ($str === '') - { - return ''; - } - - if ($phrase !== '') - { - return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open.'\\1'.$tag_close, $str); - } - - return $str; + return ($str !== '' && $phrase !== '') + ? preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open.'\\1'.$tag_close, $str) + : $str; } } diff --git a/system/helpers/url_helper.php b/system/helpers/url_helper.php index fbb4a1b24..f9650cd04 100644 --- a/system/helpers/url_helper.php +++ b/system/helpers/url_helper.php @@ -52,14 +52,7 @@ if ( ! function_exists('site_url')) */ function site_url($uri = '', $protocol = NULL) { - $uri = get_instance()->config->site_url($uri); - - if (isset($protocol)) - { - return $protocol.substr($uri, strpos($uri, '://')); - } - - return $uri; + return get_instance()->config->site_url($uri, $protocol); } } @@ -80,14 +73,7 @@ if ( ! function_exists('base_url')) */ function base_url($uri = '', $protocol = NULL) { - $uri = get_instance()->config->base_url($uri); - - if (isset($protocol)) - { - return $protocol.substr($uri, strpos($uri, '://')); - } - - return $uri; + return get_instance()->config->base_url($uri, $protocol); } } @@ -123,8 +109,7 @@ if ( ! function_exists('uri_string')) */ function uri_string() { - $CI =& get_instance(); - return $CI->uri->uri_string(); + return get_instance()->uri->uri_string(); } } @@ -141,8 +126,7 @@ if ( ! function_exists('index_page')) */ function index_page() { - $CI =& get_instance(); - return $CI->config->item('index_page'); + return get_instance()->config->item('index_page'); } } @@ -548,11 +532,16 @@ if ( ! function_exists('redirect')) } elseif ($method !== 'refresh' && (empty($code) OR ! is_numeric($code))) { - // Reference: http://en.wikipedia.org/wiki/Post/Redirect/Get - $code = (isset($_SERVER['REQUEST_METHOD'], $_SERVER['SERVER_PROTOCOL']) - && $_SERVER['REQUEST_METHOD'] === 'POST' - && $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.1') - ? 303 : 302; + if (isset($_SERVER['SERVER_PROTOCOL'], $_SERVER['REQUEST_METHOD']) && $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.1') + { + $code = ($_SERVER['REQUEST_METHOD'] !== 'GET') + ? 303 // reference: http://en.wikipedia.org/wiki/Post/Redirect/Get + : 307; + } + else + { + $code = 302; + } } switch ($method) |