From d2df9bc7cc9d4b3e53818470c5d0977c9a36677c Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Sun, 15 Apr 2007 17:41:17 +0000 Subject: update pMachine to EllisLab update copyright year update Code Igniter to CodeIgniter --- system/helpers/array_helper.php | 140 ++--- system/helpers/cookie_helper.php | 250 ++++---- system/helpers/date_helper.php | 6 +- system/helpers/directory_helper.php | 132 ++--- system/helpers/download_helper.php | 6 +- system/helpers/file_helper.php | 356 +++++------ system/helpers/form_helper.php | 6 +- system/helpers/html_helper.php | 406 ++++++------- system/helpers/inflector_helper.php | 278 ++++----- system/helpers/security_helper.php | 222 +++---- system/helpers/smiley_helper.php | 328 +++++------ system/helpers/string_helper.php | 6 +- system/helpers/text_helper.php | 840 +++++++++++++------------- system/helpers/typography_helper.php | 1078 +++++++++++++++++----------------- system/helpers/url_helper.php | 6 +- system/helpers/xml_helper.php | 114 ++-- 16 files changed, 2087 insertions(+), 2087 deletions(-) (limited to 'system/helpers') diff --git a/system/helpers/array_helper.php b/system/helpers/array_helper.php index 491c61577..674ef3bec 100644 --- a/system/helpers/array_helper.php +++ b/system/helpers/array_helper.php @@ -1,71 +1,71 @@ - \ No newline at end of file diff --git a/system/helpers/cookie_helper.php b/system/helpers/cookie_helper.php index b117816e2..9dc5df9b1 100644 --- a/system/helpers/cookie_helper.php +++ b/system/helpers/cookie_helper.php @@ -1,126 +1,126 @@ -config->item('cookie_prefix') != '') - { - $CI->config->item('cookie_prefix'); - } - if ($domain == '' AND $CI->config->item('cookie_domain') != '') - { - $CI->config->item('cookie_domain'); - } - if ($prefix == '/' AND $CI->config->item('cookie_path') != '/') - { - $CI->config->item('cookie_path'); - } - - if ( ! is_numeric($expire)) - { - $expire = time() - 86500; - } - else - { - if ($expire > 0) - { - $expire = time() + $expire; - } - else - { - $expire = 0; - } - } - - setcookie($prefix.$name, $value, $expire, $path, $domain, 0); -} - -// -------------------------------------------------------------------- - -/** - * Fetch an item from the COOKIE array - * - * @access public - * @param string - * @param bool - * @return mixed - */ -function get_cookie($index = '', $xss_clean = FALSE) -{ - $CI =& get_instance(); - return $CI->input->cookie($index, $xss_clean); -} - -// -------------------------------------------------------------------- - -/** - * Delete a COOKIE - * - * @param mixed - * @param string the cookie domain. Usually: .yourdomain.com - * @param string the cookie path - * @param string the cookie prefix - * @return void - */ -function delete_cookie($name = '', $domain = '', $path = '/', $prefix = '') -{ - set_cookie($name, '', '', $domain, $path, $prefix); -} - - +config->item('cookie_prefix') != '') + { + $CI->config->item('cookie_prefix'); + } + if ($domain == '' AND $CI->config->item('cookie_domain') != '') + { + $CI->config->item('cookie_domain'); + } + if ($prefix == '/' AND $CI->config->item('cookie_path') != '/') + { + $CI->config->item('cookie_path'); + } + + if ( ! is_numeric($expire)) + { + $expire = time() - 86500; + } + else + { + if ($expire > 0) + { + $expire = time() + $expire; + } + else + { + $expire = 0; + } + } + + setcookie($prefix.$name, $value, $expire, $path, $domain, 0); +} + +// -------------------------------------------------------------------- + +/** + * Fetch an item from the COOKIE array + * + * @access public + * @param string + * @param bool + * @return mixed + */ +function get_cookie($index = '', $xss_clean = FALSE) +{ + $CI =& get_instance(); + return $CI->input->cookie($index, $xss_clean); +} + +// -------------------------------------------------------------------- + +/** + * Delete a COOKIE + * + * @param mixed + * @param string the cookie domain. Usually: .yourdomain.com + * @param string the cookie path + * @param string the cookie prefix + * @return void + */ +function delete_cookie($name = '', $domain = '', $path = '/', $prefix = '') +{ + set_cookie($name, '', '', $domain, $path, $prefix); +} + + ?> \ No newline at end of file diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php index 504a962ce..c67e3da2d 100644 --- a/system/helpers/date_helper.php +++ b/system/helpers/date_helper.php @@ -1,12 +1,12 @@ \ No newline at end of file diff --git a/system/helpers/download_helper.php b/system/helpers/download_helper.php index 34cc152cc..c9d81f692 100644 --- a/system/helpers/download_helper.php +++ b/system/helpers/download_helper.php @@ -1,12 +1,12 @@ 0) - { - $data =& fread($fp, filesize($file)); - } - - flock($fp, LOCK_UN); - fclose($fp); - - return $data; -} - -// ------------------------------------------------------------------------ - -/** - * Write File - * - * Writes data to the file specified in the path. - * Creates a new file if non-existent. - * - * @access public - * @param string path to file - * @param string file data - * @return bool - */ -function write_file($path, $data, $mode = 'wb') -{ - if ( ! $fp = @fopen($path, $mode)) - { - return FALSE; - } - - flock($fp, LOCK_EX); - fwrite($fp, $data); - flock($fp, LOCK_UN); - fclose($fp); - - return TRUE; -} - -// ------------------------------------------------------------------------ - -/** - * Delete Files - * - * Deletes all files contained in the supplied directory path. - * Files must be writable or owned by the system in order to be deleted. - * If the second parameter is set to TRUE, any directories contained - * within the supplied base directory will be nuked as well. - * - * @access public - * @param string path to file - * @param bool whether to delete any directories found in the path - * @return bool - */ -function delete_files($path, $del_dir = FALSE, $level = 0) -{ - // Trim the trailing slash - $path = preg_replace("|^(.+?)/*$|", "\\1", $path); - - if ( ! $current_dir = @opendir($path)) - return; - - while(FALSE !== ($filename = @readdir($current_dir))) - { - if ($filename != "." and $filename != "..") - { - if (is_dir($path.'/'.$filename)) - { - $level++; - delete_files($path.'/'.$filename, $del_dir, $level); - } - else - { - unlink($path.'/'.$filename); - } - } - } - @closedir($current_dir); - - if ($del_dir == TRUE AND $level > 0) - { - @rmdir($path); - } -} - -// ------------------------------------------------------------------------ - -/** - * Get Filenames - * - * Reads the specified directory and builds an array containing the filenames. - * Any sub-folders contained within the specified path are read as well. - * - * @access public - * @param string path to source - * @param bool whether to include the path as part of the filename - * @return array - */ -function get_filenames($source_dir, $include_path = FALSE) -{ - static $_filedata = array(); - - if ($fp = @opendir($source_dir)) - { - while (FALSE !== ($file = readdir($fp))) - { - if (@is_dir($source_dir.$file) && substr($file, 0, 1) != '.') - { - get_filenames($source_dir.$file."/", $include_path); - } - elseif (substr($file, 0, 1) != ".") - { - - $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file; - } - } - return $_filedata; - } -} - + 0) + { + $data =& fread($fp, filesize($file)); + } + + flock($fp, LOCK_UN); + fclose($fp); + + return $data; +} + +// ------------------------------------------------------------------------ + +/** + * Write File + * + * Writes data to the file specified in the path. + * Creates a new file if non-existent. + * + * @access public + * @param string path to file + * @param string file data + * @return bool + */ +function write_file($path, $data, $mode = 'wb') +{ + if ( ! $fp = @fopen($path, $mode)) + { + return FALSE; + } + + flock($fp, LOCK_EX); + fwrite($fp, $data); + flock($fp, LOCK_UN); + fclose($fp); + + return TRUE; +} + +// ------------------------------------------------------------------------ + +/** + * Delete Files + * + * Deletes all files contained in the supplied directory path. + * Files must be writable or owned by the system in order to be deleted. + * If the second parameter is set to TRUE, any directories contained + * within the supplied base directory will be nuked as well. + * + * @access public + * @param string path to file + * @param bool whether to delete any directories found in the path + * @return bool + */ +function delete_files($path, $del_dir = FALSE, $level = 0) +{ + // Trim the trailing slash + $path = preg_replace("|^(.+?)/*$|", "\\1", $path); + + if ( ! $current_dir = @opendir($path)) + return; + + while(FALSE !== ($filename = @readdir($current_dir))) + { + if ($filename != "." and $filename != "..") + { + if (is_dir($path.'/'.$filename)) + { + $level++; + delete_files($path.'/'.$filename, $del_dir, $level); + } + else + { + unlink($path.'/'.$filename); + } + } + } + @closedir($current_dir); + + if ($del_dir == TRUE AND $level > 0) + { + @rmdir($path); + } +} + +// ------------------------------------------------------------------------ + +/** + * Get Filenames + * + * Reads the specified directory and builds an array containing the filenames. + * Any sub-folders contained within the specified path are read as well. + * + * @access public + * @param string path to source + * @param bool whether to include the path as part of the filename + * @return array + */ +function get_filenames($source_dir, $include_path = FALSE) +{ + static $_filedata = array(); + + if ($fp = @opendir($source_dir)) + { + while (FALSE !== ($file = readdir($fp))) + { + if (@is_dir($source_dir.$file) && substr($file, 0, 1) != '.') + { + get_filenames($source_dir.$file."/", $include_path); + } + elseif (substr($file, 0, 1) != ".") + { + + $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file; + } + } + return $_filedata; + } +} + ?> \ No newline at end of file diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index a166198fb..adbddd5ad 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -1,12 +1,12 @@ ".$data.""; -} - -// ------------------------------------------------------------------------ - -/** - * Unordered List - * - * Generates an HTML unordered list from an single or multi-dimensional array. - * - * @access public - * @param array - * @param mixed - * @return string - */ -function ul($list, $attributes = '') -{ - return _list('ul', $list, $attributes); -} - -// ------------------------------------------------------------------------ - -/** - * Ordered List - * - * Generates an HTML ordered list from an single or multi-dimensional array. - * - * @access public - * @param array - * @param mixed - * @return string - */ -function ol($list, $attributes = '') -{ - return _list('ol', $list, $attributes); -} - -// ------------------------------------------------------------------------ - -/** - * Generates the list - * - * Generates an HTML ordered list from an single or multi-dimensional array. - * - * @access private - * @param string - * @param mixed - * @param mixed - * @param intiger - * @return string - */ -function _list($type = 'ul', $list, $attributes = '', $depth = 0) -{ - // If an array wasn't submitted there's nothing to do... - if ( ! is_array($list)) - { - return $list; - } - - // Set the indentation based on the depth - $out = str_repeat(" ", $depth); - - // Were any attributes submitted? If so generate a string - if (is_array($attributes)) - { - $atts = ''; - foreach ($attributes as $key => $val) - { - $atts .= ' ' . $key . '="' . $val . '"'; - } - $attributes = $atts; - } - - // Write the opening list tag - $out .= "<".$type.$attributes.">\n"; - - // Cycle through the list elements. If an array is - // encountered we will recursively call _list() - - static $_last_list_item = ''; - foreach ($list as $key => $val) - { - $_last_list_item = $key; - - $out .= str_repeat(" ", $depth + 2); - $out .= "
  • "; - - if ( ! is_array($val)) - { - $out .= $val; - } - else - { - $out .= $_last_list_item."\n"; - $out .= _list($type, $val, '', $depth + 4); - $out .= str_repeat(" ", $depth + 2); - } - - $out .= "
  • \n"; - } - - // Set the indentation for the closing tag - $out .= str_repeat(" ", $depth); - - // Write the closing list tag - $out .= "\n"; - - return $out; -} - -// ------------------------------------------------------------------------ - -/** - * Generates HTML BR tags based on number supplied - * - * @access public - * @param integer - * @return string - */ -function br($num = 1) -{ - return str_repeat("
    ", $num); -} - -// ------------------------------------------------------------------------ - -/** - * Generates non-breaking space entities based on number supplied - * - * @access public - * @param integer - * @return string - */ -function nbs($num = 1) -{ - return str_repeat(" ", $num); -} - -// ------------------------------------------------------------------------ - -/** - * Generates meta tags from an array of key/values - * - * @access public - * @param array - * @return string - */ -function meta($meta = array(), $newline = "\n") -{ - $str = ''; - foreach ($meta as $key => $val) - { - $str .= ''.$newline; - } - - return $str; -} - - - - +".$data.""; +} + +// ------------------------------------------------------------------------ + +/** + * Unordered List + * + * Generates an HTML unordered list from an single or multi-dimensional array. + * + * @access public + * @param array + * @param mixed + * @return string + */ +function ul($list, $attributes = '') +{ + return _list('ul', $list, $attributes); +} + +// ------------------------------------------------------------------------ + +/** + * Ordered List + * + * Generates an HTML ordered list from an single or multi-dimensional array. + * + * @access public + * @param array + * @param mixed + * @return string + */ +function ol($list, $attributes = '') +{ + return _list('ol', $list, $attributes); +} + +// ------------------------------------------------------------------------ + +/** + * Generates the list + * + * Generates an HTML ordered list from an single or multi-dimensional array. + * + * @access private + * @param string + * @param mixed + * @param mixed + * @param intiger + * @return string + */ +function _list($type = 'ul', $list, $attributes = '', $depth = 0) +{ + // If an array wasn't submitted there's nothing to do... + if ( ! is_array($list)) + { + return $list; + } + + // Set the indentation based on the depth + $out = str_repeat(" ", $depth); + + // Were any attributes submitted? If so generate a string + if (is_array($attributes)) + { + $atts = ''; + foreach ($attributes as $key => $val) + { + $atts .= ' ' . $key . '="' . $val . '"'; + } + $attributes = $atts; + } + + // Write the opening list tag + $out .= "<".$type.$attributes.">\n"; + + // Cycle through the list elements. If an array is + // encountered we will recursively call _list() + + static $_last_list_item = ''; + foreach ($list as $key => $val) + { + $_last_list_item = $key; + + $out .= str_repeat(" ", $depth + 2); + $out .= "
  • "; + + if ( ! is_array($val)) + { + $out .= $val; + } + else + { + $out .= $_last_list_item."\n"; + $out .= _list($type, $val, '', $depth + 4); + $out .= str_repeat(" ", $depth + 2); + } + + $out .= "
  • \n"; + } + + // Set the indentation for the closing tag + $out .= str_repeat(" ", $depth); + + // Write the closing list tag + $out .= "\n"; + + return $out; +} + +// ------------------------------------------------------------------------ + +/** + * Generates HTML BR tags based on number supplied + * + * @access public + * @param integer + * @return string + */ +function br($num = 1) +{ + return str_repeat("
    ", $num); +} + +// ------------------------------------------------------------------------ + +/** + * Generates non-breaking space entities based on number supplied + * + * @access public + * @param integer + * @return string + */ +function nbs($num = 1) +{ + return str_repeat(" ", $num); +} + +// ------------------------------------------------------------------------ + +/** + * Generates meta tags from an array of key/values + * + * @access public + * @param array + * @return string + */ +function meta($meta = array(), $newline = "\n") +{ + $str = ''; + foreach ($meta as $key => $val) + { + $str .= ''.$newline; + } + + return $str; +} + + + + ?> \ No newline at end of file diff --git a/system/helpers/inflector_helper.php b/system/helpers/inflector_helper.php index 666ec40b8..fbe851f3b 100644 --- a/system/helpers/inflector_helper.php +++ b/system/helpers/inflector_helper.php @@ -1,140 +1,140 @@ - \ No newline at end of file diff --git a/system/helpers/security_helper.php b/system/helpers/security_helper.php index 06228468f..eec04015d 100644 --- a/system/helpers/security_helper.php +++ b/system/helpers/security_helper.php @@ -1,112 +1,112 @@ -input->xss_clean($str, $charset); -} - -// -------------------------------------------------------------------- - -/** - * Hash encode a string - * - * @access public - * @param string - * @return string - */ -function dohash($str, $type = 'sha1') -{ - if ($type == 'sha1') - { - if ( ! function_exists('sha1')) - { - if ( ! function_exists('mhash')) - { - require_once(BASEPATH.'libraries/Sha1'.EXT); - $SH = new CI_SHA; - return $SH->generate($str); - } - else - { - return bin2hex(mhash(MHASH_SHA1, $str)); - } - } - else - { - return sha1($str); - } - } - else - { - return md5($str); - } -} - -// ------------------------------------------------------------------------ - -/** - * Strip Image Tags - * - * @access public - * @param string - * @return string - */ -function strip_image_tags($str) -{ - $str = preg_replace("##", "\\1", $str); - $str = preg_replace("##", "\\1", $str); - - return $str; -} - -// ------------------------------------------------------------------------ - -/** - * Convert PHP tags to entities - * - * @access public - * @param string - * @return string - */ -function encode_php_tags($str) -{ - return str_replace(array(''), array('<?php', '<?PHP', '<?', '?>'), $str); -} - +input->xss_clean($str, $charset); +} + +// -------------------------------------------------------------------- + +/** + * Hash encode a string + * + * @access public + * @param string + * @return string + */ +function dohash($str, $type = 'sha1') +{ + if ($type == 'sha1') + { + if ( ! function_exists('sha1')) + { + if ( ! function_exists('mhash')) + { + require_once(BASEPATH.'libraries/Sha1'.EXT); + $SH = new CI_SHA; + return $SH->generate($str); + } + else + { + return bin2hex(mhash(MHASH_SHA1, $str)); + } + } + else + { + return sha1($str); + } + } + else + { + return md5($str); + } +} + +// ------------------------------------------------------------------------ + +/** + * Strip Image Tags + * + * @access public + * @param string + * @return string + */ +function strip_image_tags($str) +{ + $str = preg_replace("##", "\\1", $str); + $str = preg_replace("##", "\\1", $str); + + return $str; +} + +// ------------------------------------------------------------------------ + +/** + * Convert PHP tags to entities + * + * @access public + * @param string + * @return string + */ +function encode_php_tags($str) +{ + return str_replace(array(''), array('<?php', '<?PHP', '<?', '?>'), $str); +} + ?> \ No newline at end of file diff --git a/system/helpers/smiley_helper.php b/system/helpers/smiley_helper.php index a07337246..9f16d3132 100644 --- a/system/helpers/smiley_helper.php +++ b/system/helpers/smiley_helper.php @@ -1,165 +1,165 @@ - - function insert_smiley(smiley) - { - document.{$form_name}.{$form_field}.value += " " + smiley; - } - -EOF; -} - -// ------------------------------------------------------------------------ - -/** - * Get Clickable Smileys - * - * Returns an array of image tag links that can be clicked to be inserted - * into a form field. - * - * @access public - * @param string the URL to the folder containing the smiley images - * @return array - */ -function get_clickable_smileys($image_url = '', $smileys = NULL) -{ - if ( ! is_array($smileys)) - { - if (FALSE === ($smileys = _get_smiley_array())) - { - return $str; - } - } - - // Add a trailing slash to the file path if needed - $image_url = preg_replace("/(.+?)\/*$/", "\\1/", $image_url); - - $used = array(); - foreach ($smileys as $key => $val) - { - // Keep duplicates from being used, which can happen if the - // mapping array contains multiple identical replacements. For example: - // :-) and :) might be replaced with the same image so both smileys - // will be in the array. - if (isset($used[$smileys[$key][0]])) - { - continue; - } - - $link[] = "\"".$smileys[$key][3]."\""; - - $used[$smileys[$key][0]] = TRUE; - } - - return $link; -} - -// ------------------------------------------------------------------------ - -/** - * Parse Smileys - * - * Takes a string as input and swaps any contained smileys for the actual image - * - * @access public - * @param string the text to be parsed - * @param string the URL to the folder containing the smiley images - * @return string - */ -function parse_smileys($str = '', $image_url = '', $smileys = NULL) -{ - if ($image_url == '') - { - return $str; - } - - if ( ! is_array($smileys)) - { - if (FALSE === ($smileys = _get_smiley_array())) - { - return $str; - } - } - - // Add a trailing slash to the file path if needed - $image_url = preg_replace("/(.+?)\/*$/", "\\1/", $image_url); - - foreach ($smileys as $key => $val) - { - $str = str_replace($key, "\"".$smileys[$key][3]."\"", $str); - } - - return $str; -} - -// ------------------------------------------------------------------------ - -/** - * Get Smiley Array - * - * Fetches the config/smiley.php file - * - * @access private - * @return mixed - */ -function _get_smiley_array() -{ - if ( ! file_exists(APPPATH.'config/smileys'.EXT)) - { - return FALSE; - } - - include(APPPATH.'config/smileys'.EXT); - - if ( ! isset($smileys) OR ! is_array($smileys)) - { - return FALSE; - } - - return $smileys; -} - - - - + + function insert_smiley(smiley) + { + document.{$form_name}.{$form_field}.value += " " + smiley; + } + +EOF; +} + +// ------------------------------------------------------------------------ + +/** + * Get Clickable Smileys + * + * Returns an array of image tag links that can be clicked to be inserted + * into a form field. + * + * @access public + * @param string the URL to the folder containing the smiley images + * @return array + */ +function get_clickable_smileys($image_url = '', $smileys = NULL) +{ + if ( ! is_array($smileys)) + { + if (FALSE === ($smileys = _get_smiley_array())) + { + return $str; + } + } + + // Add a trailing slash to the file path if needed + $image_url = preg_replace("/(.+?)\/*$/", "\\1/", $image_url); + + $used = array(); + foreach ($smileys as $key => $val) + { + // Keep duplicates from being used, which can happen if the + // mapping array contains multiple identical replacements. For example: + // :-) and :) might be replaced with the same image so both smileys + // will be in the array. + if (isset($used[$smileys[$key][0]])) + { + continue; + } + + $link[] = "\"".$smileys[$key][3]."\""; + + $used[$smileys[$key][0]] = TRUE; + } + + return $link; +} + +// ------------------------------------------------------------------------ + +/** + * Parse Smileys + * + * Takes a string as input and swaps any contained smileys for the actual image + * + * @access public + * @param string the text to be parsed + * @param string the URL to the folder containing the smiley images + * @return string + */ +function parse_smileys($str = '', $image_url = '', $smileys = NULL) +{ + if ($image_url == '') + { + return $str; + } + + if ( ! is_array($smileys)) + { + if (FALSE === ($smileys = _get_smiley_array())) + { + return $str; + } + } + + // Add a trailing slash to the file path if needed + $image_url = preg_replace("/(.+?)\/*$/", "\\1/", $image_url); + + foreach ($smileys as $key => $val) + { + $str = str_replace($key, "\"".$smileys[$key][3]."\"", $str); + } + + return $str; +} + +// ------------------------------------------------------------------------ + +/** + * Get Smiley Array + * + * Fetches the config/smiley.php file + * + * @access private + * @return mixed + */ +function _get_smiley_array() +{ + if ( ! file_exists(APPPATH.'config/smileys'.EXT)) + { + return FALSE; + } + + include(APPPATH.'config/smileys'.EXT); + + if ( ! isset($smileys) OR ! is_array($smileys)) + { + return FALSE; + } + + return $smileys; +} + + + + ?> \ No newline at end of file diff --git a/system/helpers/string_helper.php b/system/helpers/string_helper.php index c0a8854dc..04c3a592a 100644 --- a/system/helpers/string_helper.php +++ b/system/helpers/string_helper.php @@ -1,12 +1,12 @@ = $n) - { - return trim($out).$end_char; - } - } -} - -// ------------------------------------------------------------------------ - -/** - * High ASCII to Entities - * - * Converts High ascii text and MS Word special characters to character entities - * - * @access public - * @param string - * @return string - */ -function ascii_to_entities($str) -{ - $count = 1; - $out = ''; - $temp = array(); - - for ($i = 0, $s = strlen($str); $i < $s; $i++) - { - $ordinal = ord($str[$i]); - - if ($ordinal < 128) - { - $out .= $str[$i]; - } - else - { - if (count($temp) == 0) - { - $count = ($ordinal < 224) ? 2 : 3; - } - - $temp[] = $ordinal; - - if (count($temp) == $count) - { - $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); - - $out .= '&#'.$number.';'; - $count = 1; - $temp = array(); - } - } - } - - return $out; -} - -// ------------------------------------------------------------------------ - -/** - * Entities to ASCII - * - * Converts character entities back to ASCII - * - * @access public - * @param string - * @param bool - * @return string - */ -function entities_to_ascii($str, $all = TRUE) -{ - if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) - { - for ($i = 0, $s = count($matches['0']); $i < $s; $i++) - { - $digits = $matches['1'][$i]; - - $out = ''; - - if ($digits < 128) - { - $out .= chr($digits); - - } - elseif ($digits < 2048) - { - $out .= chr(192 + (($digits - ($digits % 64)) / 64)); - $out .= chr(128 + ($digits % 64)); - } - else - { - $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)); - $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)); - $out .= chr(128 + ($digits % 64)); - } - - $str = str_replace($matches['0'][$i], $out, $str); - } - } - - if ($all) - { - $str = str_replace(array("&", "<", ">", """, "'", "-"), - array("&","<",">","\"", "'", "-"), - $str); - } - - return $str; -} - -// ------------------------------------------------------------------------ - -/** - * Word Censoring Function - * - * Supply a string and an array of disallowed words and any - * matched words will be converted to #### or to the replacement - * word you've submitted. - * - * @access public - * @param string the text string - * @param string the array of censoered words - * @param string the optional replacement value - * @return string - */ -function word_censor($str, $censored, $replacement = '') -{ - if ( ! is_array($censored)) - { - return $str; - } - - $str = ' '.$str.' '; - foreach ($censored as $badword) - { - if ($replacement != '') - { - $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/i", $replacement, $str); - } - else - { - $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/ie", "str_repeat('#', strlen('\\1'))", $str); - } - } - - return trim($str); -} - -// ------------------------------------------------------------------------ - -/** - * Code Highlighter - * - * Colorizes code strings - * - * @access public - * @param string the text string - * @return string - */ -function highlight_code($str) -{ - // The highlight string function encodes and highlights - // brackets so we need them to start raw - $str = str_replace(array('<', '>'), array('<', '>'), $str); - - // Replace any existing PHP tags to temporary markers so they don't accidentally - // break the string out of PHP, and thus, thwart the highlighting. - - $str = str_replace(array('<?php', '?>', '\\'), array('phptagopen', 'phptagclose', 'backslashtmp'), $str); - - // The highlight_string function requires that the text be surrounded - // by PHP tags. Since we don't know if A) the submitted text has PHP tags, - // or B) whether the PHP tags enclose the entire string, we will add our - // own PHP tags around the string along with some markers to make replacement easier later - - $str = ''; - - // All the magic happens here, baby! - $str = highlight_string($str, TRUE); - - // Prior to PHP 5, the highlight function used icky font tags - // so we'll replace them with span tags. - if (abs(phpversion()) < 5) - { - $str = str_replace(array(''), array(''), $str); - $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); - } - - // Remove our artificially added PHP - $str = preg_replace("#\.+?//tempstart\
    \#is", "\n", $str); - $str = preg_replace("#\.+?//tempstart\
    #is", "\n", $str); - $str = preg_replace("#//tempend.+#is", "
    \n", $str); - - // Replace our markers back to PHP tags. - $str = str_replace(array('phptagopen', 'phptagclose', 'backslashtmp'), array('<?php', '?>', '\\'), $str); //', $tag_close = '') -{ - if ($str == '') - { - return ''; - } - - if ($phrase != '') - { - return preg_replace('/('.preg_quote($phrase).')/i', $tag_open."\\1".$tag_close, $str); - } - - return $str; -} - -// ------------------------------------------------------------------------ - -/** - * Word Wrap - * - * Wraps text at the specified character. Maintains the integrity of words. - * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor - * will URLs. - * - * @access public - * @param string the text string - * @param integer the number of characters to wrap at - * @return string - */ -function word_wrap($str, $charlim = '76') -{ - // Se the character limit - if ( ! is_numeric($charlim)) - $charlim = 76; - - // Reduce multiple spaces - $str = preg_replace("| +|", " ", $str); - - // Standardize newlines - $str = preg_replace("/\r\n|\r/", "\n", $str); - - // If the current word is surrounded by {unwrap} tags we'll - // strip the entire chunk and replace it with a marker. - $unwrap = array(); - if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches)) - { - for ($i = 0; $i < count($matches['0']); $i++) - { - $unwrap[] = $matches['1'][$i]; - $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str); - } - } - - // Use PHP's native function to do the initial wordwrap. - // We set the cut flag to FALSE so that any individual words that are - // too long get left alone. In the next step we'll deal with them. - $str = wordwrap($str, $charlim, "\n", FALSE); - - // Split the string into individual lines of text and cycle through them - $output = ""; - foreach (explode("\n", $str) as $line) - { - // Is the line within the allowed character count? - // If so we'll join it to the output and continue - if (strlen($line) <= $charlim) - { - $output .= $line."\n"; - continue; - } - - $temp = ''; - while((strlen($line)) > $charlim) - { - // If the over-length word is a URL we won't wrap it - if (preg_match("!\[url.+\]|://|wwww.!", $line)) - { - break; - } - - // Trim the word down - $temp .= substr($line, 0, $charlim-1); - $line = substr($line, $charlim-1); - } - - // If $temp contains data it means we had to split up an over-length - // word into smaller chunks so we'll add it back to our current line - if ($temp != '') - { - $output .= $temp.$this->newline.$line; - } - else - { - $output .= $line; - } - - $output .= "\n"; - } - - // Put our markers back - if (count($unwrap) > 0) - { - foreach ($unwrap as $key => $val) - { - $output = str_replace("{{unwrapped".$key."}}", $val, $output); - } - } - - // Remove the unwrap tags - $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output); - - return $output; -} - - += $n) + { + return trim($out).$end_char; + } + } +} + +// ------------------------------------------------------------------------ + +/** + * High ASCII to Entities + * + * Converts High ascii text and MS Word special characters to character entities + * + * @access public + * @param string + * @return string + */ +function ascii_to_entities($str) +{ + $count = 1; + $out = ''; + $temp = array(); + + for ($i = 0, $s = strlen($str); $i < $s; $i++) + { + $ordinal = ord($str[$i]); + + if ($ordinal < 128) + { + $out .= $str[$i]; + } + else + { + if (count($temp) == 0) + { + $count = ($ordinal < 224) ? 2 : 3; + } + + $temp[] = $ordinal; + + if (count($temp) == $count) + { + $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); + + $out .= '&#'.$number.';'; + $count = 1; + $temp = array(); + } + } + } + + return $out; +} + +// ------------------------------------------------------------------------ + +/** + * Entities to ASCII + * + * Converts character entities back to ASCII + * + * @access public + * @param string + * @param bool + * @return string + */ +function entities_to_ascii($str, $all = TRUE) +{ + if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) + { + for ($i = 0, $s = count($matches['0']); $i < $s; $i++) + { + $digits = $matches['1'][$i]; + + $out = ''; + + if ($digits < 128) + { + $out .= chr($digits); + + } + elseif ($digits < 2048) + { + $out .= chr(192 + (($digits - ($digits % 64)) / 64)); + $out .= chr(128 + ($digits % 64)); + } + else + { + $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)); + $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)); + $out .= chr(128 + ($digits % 64)); + } + + $str = str_replace($matches['0'][$i], $out, $str); + } + } + + if ($all) + { + $str = str_replace(array("&", "<", ">", """, "'", "-"), + array("&","<",">","\"", "'", "-"), + $str); + } + + return $str; +} + +// ------------------------------------------------------------------------ + +/** + * Word Censoring Function + * + * Supply a string and an array of disallowed words and any + * matched words will be converted to #### or to the replacement + * word you've submitted. + * + * @access public + * @param string the text string + * @param string the array of censoered words + * @param string the optional replacement value + * @return string + */ +function word_censor($str, $censored, $replacement = '') +{ + if ( ! is_array($censored)) + { + return $str; + } + + $str = ' '.$str.' '; + foreach ($censored as $badword) + { + if ($replacement != '') + { + $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/i", $replacement, $str); + } + else + { + $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/ie", "str_repeat('#', strlen('\\1'))", $str); + } + } + + return trim($str); +} + +// ------------------------------------------------------------------------ + +/** + * Code Highlighter + * + * Colorizes code strings + * + * @access public + * @param string the text string + * @return string + */ +function highlight_code($str) +{ + // The highlight string function encodes and highlights + // brackets so we need them to start raw + $str = str_replace(array('<', '>'), array('<', '>'), $str); + + // Replace any existing PHP tags to temporary markers so they don't accidentally + // break the string out of PHP, and thus, thwart the highlighting. + + $str = str_replace(array('<?php', '?>', '\\'), array('phptagopen', 'phptagclose', 'backslashtmp'), $str); + + // The highlight_string function requires that the text be surrounded + // by PHP tags. Since we don't know if A) the submitted text has PHP tags, + // or B) whether the PHP tags enclose the entire string, we will add our + // own PHP tags around the string along with some markers to make replacement easier later + + $str = ''; + + // All the magic happens here, baby! + $str = highlight_string($str, TRUE); + + // Prior to PHP 5, the highlight function used icky font tags + // so we'll replace them with span tags. + if (abs(phpversion()) < 5) + { + $str = str_replace(array(''), array(''), $str); + $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); + } + + // Remove our artificially added PHP + $str = preg_replace("#\.+?//tempstart\
    \#is", "\n", $str); + $str = preg_replace("#\.+?//tempstart\
    #is", "\n", $str); + $str = preg_replace("#//tempend.+#is", "
    \n", $str); + + // Replace our markers back to PHP tags. + $str = str_replace(array('phptagopen', 'phptagclose', 'backslashtmp'), array('<?php', '?>', '\\'), $str); //', $tag_close = '') +{ + if ($str == '') + { + return ''; + } + + if ($phrase != '') + { + return preg_replace('/('.preg_quote($phrase).')/i', $tag_open."\\1".$tag_close, $str); + } + + return $str; +} + +// ------------------------------------------------------------------------ + +/** + * Word Wrap + * + * Wraps text at the specified character. Maintains the integrity of words. + * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor + * will URLs. + * + * @access public + * @param string the text string + * @param integer the number of characters to wrap at + * @return string + */ +function word_wrap($str, $charlim = '76') +{ + // Se the character limit + if ( ! is_numeric($charlim)) + $charlim = 76; + + // Reduce multiple spaces + $str = preg_replace("| +|", " ", $str); + + // Standardize newlines + $str = preg_replace("/\r\n|\r/", "\n", $str); + + // If the current word is surrounded by {unwrap} tags we'll + // strip the entire chunk and replace it with a marker. + $unwrap = array(); + if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches)) + { + for ($i = 0; $i < count($matches['0']); $i++) + { + $unwrap[] = $matches['1'][$i]; + $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str); + } + } + + // Use PHP's native function to do the initial wordwrap. + // We set the cut flag to FALSE so that any individual words that are + // too long get left alone. In the next step we'll deal with them. + $str = wordwrap($str, $charlim, "\n", FALSE); + + // Split the string into individual lines of text and cycle through them + $output = ""; + foreach (explode("\n", $str) as $line) + { + // Is the line within the allowed character count? + // If so we'll join it to the output and continue + if (strlen($line) <= $charlim) + { + $output .= $line."\n"; + continue; + } + + $temp = ''; + while((strlen($line)) > $charlim) + { + // If the over-length word is a URL we won't wrap it + if (preg_match("!\[url.+\]|://|wwww.!", $line)) + { + break; + } + + // Trim the word down + $temp .= substr($line, 0, $charlim-1); + $line = substr($line, $charlim-1); + } + + // If $temp contains data it means we had to split up an over-length + // word into smaller chunks so we'll add it back to our current line + if ($temp != '') + { + $output .= $temp.$this->newline.$line; + } + else + { + $output .= $line; + } + + $output .= "\n"; + } + + // Put our markers back + if (count($unwrap) > 0) + { + foreach ($unwrap as $key => $val) + { + $output = str_replace("{{unwrapped".$key."}}", $val, $output); + } + } + + // Remove the unwrap tags + $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output); + + return $output; +} + + ?> \ No newline at end of file diff --git a/system/helpers/typography_helper.php b/system/helpers/typography_helper.php index 1681fa37c..131358c6a 100644 --- a/system/helpers/typography_helper.php +++ b/system/helpers/typography_helper.php @@ -1,540 +1,540 @@ -",$str); - $ct = count($ex); - - $newstr = ""; - for ($i = 0; $i < $ct; $i++) - { - if (($i % 2) == 0) - { - $newstr .= nl2br($ex[$i]); - } - else - { - $newstr .= $ex[$i]; - } - - if ($ct - 1 != $i) - $newstr .= "pre>"; - } - - return $newstr; -} - -// ------------------------------------------------------------------------ - -/** - * Auto Typography Wrapper Function - * - * - * @access public - * @param string - * @return string - */ -function auto_typography($str) -{ - $TYPE = new Auto_typography(); - return $TYPE->convert($str); -} - -// ------------------------------------------------------------------------ - -/** - * Auto Typography Class - * - * - * @access private - * @category Helpers - * @author Rick Ellis - * @author Paul Burdick - * @link http://www.codeigniter.com/user_guide/helpers/ - */ -class Auto_typography { - - // Block level elements that should not be wrapped inside

    tags - var $block_elements = 'div|blockquote|pre|code|h\d|script|ol|un'; - - // Elements that should not have

    and
    tags within them. - var $skip_elements = 'pre|ol|ul'; - - // Tags we want the parser to completely ignore when splitting the string. - var $ignore_elements = 'a|b|i|em|strong|span|img|li'; - - - /** - * Main Processing Function - * - */ - function convert($str) - { - if ($str == '') - { - return ''; - } - - $str = ' '.$str.' '; - - // Standardize Newlines to make matching easier - $str = preg_replace("/(\r\n|\r)/", "\n", $str); - - /* - * Reduce line breaks - * - * If there are more than two consecutive line - * breaks we'll compress them down to a maximum - * of two since there's no benefit to more. - * - */ - $str = preg_replace("/\n\n+/", "\n\n", $str); - - /* - * Convert quotes within tags to temporary marker - * - * We don't want quotes converted within - * tags so we'll temporarily convert them to - * {@DQ} and {@SQ} - * - */ - if (preg_match_all("#\<.+?>#si", $str, $matches)) - { - for ($i = 0; $i < count($matches['0']); $i++) - { - $str = str_replace($matches['0'][$i], - str_replace(array("'",'"'), array('{@SQ}', '{@DQ}'), $matches['0'][$i]), - $str); - } - } - - - /* - * Add closing/opening paragraph tags before/after "block" elements - * - * Since block elements (like ,

    , etc.) do not get
    -		 * wrapped in paragraph tags we will add a closing 

    tag just before - * each block element starts and an opening

    tag right after the block element - * ends. Later on we'll do some further clean up. - * - */ - $str = preg_replace("#(<.*?)(".$this->block_elements.")(.*?>)#", "

    \\1\\2\\3", $str); - $str = preg_replace("#(block_elements.")(.*?>)#", "\\1\\2\\3

    ", $str); - - /* - * Convert "ignore" tags to temporary marker - * - * The parser splits out the string at every tag - * it encounters. Certain inline tags, like image - * tags, links, span tags, etc. will be adversely - * affected if they are split out so we'll convert - * the opening < temporarily to: {@TAG} - * - */ - $str = preg_replace("#<(/*)(".$this->ignore_elements.")#i", "{@TAG}\\1\\2", $str); - - /* - * Split the string at every tag - * - * This creates an array with this prototype: - * - * [array] - * { - * [0] = - * [1] = Content contained between the tags - * [2] = - * Etc... - * } - * - */ - $chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY); - - /* - * Build our finalized string - * - * We'll cycle through the array, skipping tags, - * and processing the contained text - * - */ - $str = ''; - $process = TRUE; - foreach ($chunks as $chunk) - { - /* - * Are we dealing with a tag? - * - * If so, we'll skip the processing for this cycle. - * Well also set the "process" flag which allows us - * to skip

     tags and a few other things.
    -			 *
    -			 */
    -			if (preg_match("#<(/*)(".$this->block_elements.").*?\>#", $chunk, $match))
    -			{
    -				if (preg_match("#".$this->skip_elements."#", $match['2']))
    -				{
    -					$process =  ($match['1'] == '/') ? TRUE : FALSE;		
    -				}
    -		
    -				$str .= $chunk;
    -				continue;
    -			}
    -		
    -			if ($process == FALSE)
    -			{
    -				$str .= $chunk;
    -				continue;
    -			}
    -			
    -			//  Convert Newlines into 

    and
    tags - $str .= $this->format_newlines($chunk); - } - - // FINAL CLEAN UP - // IMPORTANT: DO NOT ALTER THE ORDER OF THE ITEMS BELOW! - - /* - * Clean up paragraph tags before/after "block" elements - * - * Earlier we added

    tags before/after block level elements. - * Then, we added paragraph tags around double line breaks. This - * potentially created incorrectly formatted paragraphs so we'll - * clean it up here. - * - */ - $str = preg_replace("#

    ({@TAG}.*?)(".$this->block_elements.")(.*?>)#", "\\1\\2\\3", $str); - $str = preg_replace("#({@TAG}/.*?)(".$this->block_elements.")(.*?>)

    #", "\\1\\2\\3", $str); - - // Convert Quotes and other characters - $str = $this->format_characters($str); - - // Fix an artifact that happens during the paragraph replacement - $str = preg_replace('#(

    \n*

    )#', '', $str); - - // If the user submitted their own paragraph tags with class data - // in them we will retain them instead of using our tags. - $str = preg_replace('#()

    #', "\\1", $str); - - // Final clean up - $str = str_replace( - array( - '

    ', - '

    ', - '

    ', - '

    ', - '{@TAG}', - '{@DQ}', - '{@SQ}', - '

    ' - ), - array( - '

    ', - '

    ', - '

    ', - '

    ', - '<', - '"', - "'", - '' - ), - $str - ); - - return $str; - } - - // -------------------------------------------------------------------- - - /** - * Format Characters - * - * This function mainly converts double and single quotes - * to entities, but since these are directional, it does - * it based on some rules. It also converts em-dashes - * and a couple other things. - */ - function format_characters($str) - { - $table = array( - ' "' => " “", - '" ' => "” ", - " '" => " ‘", - "' " => "’ ", - - '>"' => ">“", - '"<' => "”<", - ">'" => ">‘", - "'<" => "’<", - - "\"." => "”.", - "\"," => "”,", - "\";" => "”;", - "\":" => "”:", - "\"!" => "”!", - "\"?" => "”?", - - ". " => ".  ", - "? " => "?  ", - "! " => "!  ", - ": " => ":  ", - ); - - // These deal with quotes within quotes, like: "'hi here'" - $start = 0; - $space = array("\n", "\t", " "); - - while(TRUE) - { - $current = strpos(substr($str, $start), "\"'"); - - if ($current === FALSE) break; - - $one_before = substr($str, $start+$current-1, 1); - $one_after = substr($str, $start+$current+2, 1); - - if ( ! in_array($one_after, $space, TRUE) && $one_after != "<") - { - $str = str_replace( $one_before."\"'".$one_after, - $one_before."“‘".$one_after, - $str); - } - elseif ( ! in_array($one_before, $space, TRUE) && (in_array($one_after, $space, TRUE) OR $one_after == '<')) - { - $str = str_replace( $one_before."\"'".$one_after, - $one_before."”’".$one_after, - $str); - } - - $start = $start+$current+2; - } - - $start = 0; - - while(TRUE) - { - $current = strpos(substr($str, $start), "'\""); - - if ($current === FALSE) break; - - $one_before = substr($str, $start+$current-1, 1); - $one_after = substr($str, $start+$current+2, 1); - - if ( in_array($one_before, $space, TRUE) && ! in_array($one_after, $space, TRUE) && $one_after != "<") - { - $str = str_replace( $one_before."'\"".$one_after, - $one_before."‘“".$one_after, - $str); - } - elseif ( ! in_array($one_before, $space, TRUE) && $one_before != ">") - { - $str = str_replace( $one_before."'\"".$one_after, - $one_before."’”".$one_after, - $str); - } - - $start = $start+$current+2; - } - - // Are there quotes within a word, as in: ("something") - if (preg_match_all("/(.)\"(\S+?)\"(.)/", $str, $matches)) - { - for ($i=0, $s=sizeof($matches['0']); $i < $s; ++$i) - { - if ( ! in_array($matches['1'][$i], $space, TRUE) && ! in_array($matches['3'][$i], $space, TRUE)) - { - $str = str_replace( $matches['0'][$i], - $matches['1'][$i]."“".$matches['2'][$i]."”".$matches['3'][$i], - $str); - } - } - } - - if (preg_match_all("/(.)\'(\S+?)\'(.)/", $str, $matches)) - { - for ($i=0, $s=sizeof($matches['0']); $i < $s; ++$i) - { - if ( ! in_array($matches['1'][$i], $space, TRUE) && ! in_array($matches['3'][$i], $space, TRUE)) - { - $str = str_replace( $matches['0'][$i], - $matches['1'][$i]."‘".$matches['2'][$i]."’".$matches['3'][$i], - $str); - } - } - } - - // How about one apostrophe, as in Rick's - $start = 0; - - while(TRUE) - { - $current = strpos(substr($str, $start), "'"); - - if ($current === FALSE) break; - - $one_before = substr($str, $start+$current-1, 1); - $one_after = substr($str, $start+$current+1, 1); - - if ( ! in_array($one_before, $space, TRUE) && ! in_array($one_after, $space, TRUE)) - { - $str = str_replace( $one_before."'".$one_after, - $one_before."’".$one_after, - $str); - } - - $start = $start+$current+2; - } - - // Em-dashes - $start = 0; - while(TRUE) - { - $current = strpos(substr($str, $start), "--"); - - if ($current === FALSE) break; - - $one_before = substr($str, $start+$current-1, 1); - $one_after = substr($str, $start+$current+2, 1); - $two_before = substr($str, $start+$current-2, 1); - $two_after = substr($str, $start+$current+3, 1); - - if (( ! in_array($one_before, $space, TRUE) && ! in_array($one_after, $space, TRUE)) - OR - ( ! in_array($two_before, $space, TRUE) && ! in_array($two_after, $space, TRUE) && $one_before == ' ' && $one_after == ' ') - ) - { - $str = str_replace( $two_before.$one_before."--".$one_after.$two_after, - $two_before.trim($one_before)."—".trim($one_after).$two_after, - $str); - } - - $start = $start+$current+2; - } - - // Ellipsis - $str = preg_replace("#(\w)\.\.\.(\s|
    |

    )#", "\\1…\\2", $str); - $str = preg_replace("#(\s|
    |

    )\.\.\.(\w)#", "\\1…\\2", $str); - - // Run the translation array we defined above - $str = str_replace(array_keys($table), array_values($table), $str); - - // If there are any stray double quotes we'll catch them here - - $start = 0; - - while(TRUE) - { - $current = strpos(substr($str, $start), '"'); - - if ($current === FALSE) break; - - $one_before = substr($str, $start+$current-1, 1); - $one_after = substr($str, $start+$current+1, 1); - - if ( ! in_array($one_after, $space, TRUE)) - { - $str = str_replace( $one_before.'"'.$one_after, - $one_before."“".$one_after, - $str); - } - elseif( ! in_array($one_before, $space, TRUE)) - { - $str = str_replace( $one_before."'".$one_after, - $one_before."”".$one_after, - $str); - } - - $start = $start+$current+2; - } - - $start = 0; - - while(TRUE) - { - $current = strpos(substr($str, $start), "'"); - - if ($current === FALSE) break; - - $one_before = substr($str, $start+$current-1, 1); - $one_after = substr($str, $start+$current+1, 1); - - if ( ! in_array($one_after, $space, TRUE)) - { - $str = str_replace( $one_before."'".$one_after, - $one_before."‘".$one_after, - $str); - } - elseif( ! in_array($one_before, $space, TRUE)) - { - $str = str_replace( $one_before."'".$one_after, - $one_before."’".$one_after, - $str); - } - - $start = $start+$current+2; - } - - return $str; - } - - // -------------------------------------------------------------------- - - /** - * Format Newlines - * - * Converts newline characters into either

    tags or
    - * - */ - function format_newlines($str) - { - if ($str == '') - { - return $str; - } - - if (strpos($str, "\n") === FALSE) - { - return '

    '.$str.'

    '; - } - - $str = str_replace("\n\n", "

    \n\n

    ", $str); - $str = preg_replace("/([^\n])(\n)([^\n])/", "\\1
    \\2\\3", $str); - - return '

    '.$str.'

    '; - } -} - - +",$str); + $ct = count($ex); + + $newstr = ""; + for ($i = 0; $i < $ct; $i++) + { + if (($i % 2) == 0) + { + $newstr .= nl2br($ex[$i]); + } + else + { + $newstr .= $ex[$i]; + } + + if ($ct - 1 != $i) + $newstr .= "pre>"; + } + + return $newstr; +} + +// ------------------------------------------------------------------------ + +/** + * Auto Typography Wrapper Function + * + * + * @access public + * @param string + * @return string + */ +function auto_typography($str) +{ + $TYPE = new Auto_typography(); + return $TYPE->convert($str); +} + +// ------------------------------------------------------------------------ + +/** + * Auto Typography Class + * + * + * @access private + * @category Helpers + * @author Rick Ellis + * @author Paul Burdick + * @link http://www.codeigniter.com/user_guide/helpers/ + */ +class Auto_typography { + + // Block level elements that should not be wrapped inside

    tags + var $block_elements = 'div|blockquote|pre|code|h\d|script|ol|un'; + + // Elements that should not have

    and
    tags within them. + var $skip_elements = 'pre|ol|ul'; + + // Tags we want the parser to completely ignore when splitting the string. + var $ignore_elements = 'a|b|i|em|strong|span|img|li'; + + + /** + * Main Processing Function + * + */ + function convert($str) + { + if ($str == '') + { + return ''; + } + + $str = ' '.$str.' '; + + // Standardize Newlines to make matching easier + $str = preg_replace("/(\r\n|\r)/", "\n", $str); + + /* + * Reduce line breaks + * + * If there are more than two consecutive line + * breaks we'll compress them down to a maximum + * of two since there's no benefit to more. + * + */ + $str = preg_replace("/\n\n+/", "\n\n", $str); + + /* + * Convert quotes within tags to temporary marker + * + * We don't want quotes converted within + * tags so we'll temporarily convert them to + * {@DQ} and {@SQ} + * + */ + if (preg_match_all("#\<.+?>#si", $str, $matches)) + { + for ($i = 0; $i < count($matches['0']); $i++) + { + $str = str_replace($matches['0'][$i], + str_replace(array("'",'"'), array('{@SQ}', '{@DQ}'), $matches['0'][$i]), + $str); + } + } + + + /* + * Add closing/opening paragraph tags before/after "block" elements + * + * Since block elements (like ,

    , etc.) do not get
    +		 * wrapped in paragraph tags we will add a closing 

    tag just before + * each block element starts and an opening

    tag right after the block element + * ends. Later on we'll do some further clean up. + * + */ + $str = preg_replace("#(<.*?)(".$this->block_elements.")(.*?>)#", "

    \\1\\2\\3", $str); + $str = preg_replace("#(block_elements.")(.*?>)#", "\\1\\2\\3

    ", $str); + + /* + * Convert "ignore" tags to temporary marker + * + * The parser splits out the string at every tag + * it encounters. Certain inline tags, like image + * tags, links, span tags, etc. will be adversely + * affected if they are split out so we'll convert + * the opening < temporarily to: {@TAG} + * + */ + $str = preg_replace("#<(/*)(".$this->ignore_elements.")#i", "{@TAG}\\1\\2", $str); + + /* + * Split the string at every tag + * + * This creates an array with this prototype: + * + * [array] + * { + * [0] = + * [1] = Content contained between the tags + * [2] = + * Etc... + * } + * + */ + $chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY); + + /* + * Build our finalized string + * + * We'll cycle through the array, skipping tags, + * and processing the contained text + * + */ + $str = ''; + $process = TRUE; + foreach ($chunks as $chunk) + { + /* + * Are we dealing with a tag? + * + * If so, we'll skip the processing for this cycle. + * Well also set the "process" flag which allows us + * to skip

     tags and a few other things.
    +			 *
    +			 */
    +			if (preg_match("#<(/*)(".$this->block_elements.").*?\>#", $chunk, $match))
    +			{
    +				if (preg_match("#".$this->skip_elements."#", $match['2']))
    +				{
    +					$process =  ($match['1'] == '/') ? TRUE : FALSE;		
    +				}
    +		
    +				$str .= $chunk;
    +				continue;
    +			}
    +		
    +			if ($process == FALSE)
    +			{
    +				$str .= $chunk;
    +				continue;
    +			}
    +			
    +			//  Convert Newlines into 

    and
    tags + $str .= $this->format_newlines($chunk); + } + + // FINAL CLEAN UP + // IMPORTANT: DO NOT ALTER THE ORDER OF THE ITEMS BELOW! + + /* + * Clean up paragraph tags before/after "block" elements + * + * Earlier we added

    tags before/after block level elements. + * Then, we added paragraph tags around double line breaks. This + * potentially created incorrectly formatted paragraphs so we'll + * clean it up here. + * + */ + $str = preg_replace("#

    ({@TAG}.*?)(".$this->block_elements.")(.*?>)#", "\\1\\2\\3", $str); + $str = preg_replace("#({@TAG}/.*?)(".$this->block_elements.")(.*?>)

    #", "\\1\\2\\3", $str); + + // Convert Quotes and other characters + $str = $this->format_characters($str); + + // Fix an artifact that happens during the paragraph replacement + $str = preg_replace('#(

    \n*

    )#', '', $str); + + // If the user submitted their own paragraph tags with class data + // in them we will retain them instead of using our tags. + $str = preg_replace('#()

    #', "\\1", $str); + + // Final clean up + $str = str_replace( + array( + '

    ', + '

    ', + '

    ', + '

    ', + '{@TAG}', + '{@DQ}', + '{@SQ}', + '

    ' + ), + array( + '

    ', + '

    ', + '

    ', + '

    ', + '<', + '"', + "'", + '' + ), + $str + ); + + return $str; + } + + // -------------------------------------------------------------------- + + /** + * Format Characters + * + * This function mainly converts double and single quotes + * to entities, but since these are directional, it does + * it based on some rules. It also converts em-dashes + * and a couple other things. + */ + function format_characters($str) + { + $table = array( + ' "' => " “", + '" ' => "” ", + " '" => " ‘", + "' " => "’ ", + + '>"' => ">“", + '"<' => "”<", + ">'" => ">‘", + "'<" => "’<", + + "\"." => "”.", + "\"," => "”,", + "\";" => "”;", + "\":" => "”:", + "\"!" => "”!", + "\"?" => "”?", + + ". " => ".  ", + "? " => "?  ", + "! " => "!  ", + ": " => ":  ", + ); + + // These deal with quotes within quotes, like: "'hi here'" + $start = 0; + $space = array("\n", "\t", " "); + + while(TRUE) + { + $current = strpos(substr($str, $start), "\"'"); + + if ($current === FALSE) break; + + $one_before = substr($str, $start+$current-1, 1); + $one_after = substr($str, $start+$current+2, 1); + + if ( ! in_array($one_after, $space, TRUE) && $one_after != "<") + { + $str = str_replace( $one_before."\"'".$one_after, + $one_before."“‘".$one_after, + $str); + } + elseif ( ! in_array($one_before, $space, TRUE) && (in_array($one_after, $space, TRUE) OR $one_after == '<')) + { + $str = str_replace( $one_before."\"'".$one_after, + $one_before."”’".$one_after, + $str); + } + + $start = $start+$current+2; + } + + $start = 0; + + while(TRUE) + { + $current = strpos(substr($str, $start), "'\""); + + if ($current === FALSE) break; + + $one_before = substr($str, $start+$current-1, 1); + $one_after = substr($str, $start+$current+2, 1); + + if ( in_array($one_before, $space, TRUE) && ! in_array($one_after, $space, TRUE) && $one_after != "<") + { + $str = str_replace( $one_before."'\"".$one_after, + $one_before."‘“".$one_after, + $str); + } + elseif ( ! in_array($one_before, $space, TRUE) && $one_before != ">") + { + $str = str_replace( $one_before."'\"".$one_after, + $one_before."’”".$one_after, + $str); + } + + $start = $start+$current+2; + } + + // Are there quotes within a word, as in: ("something") + if (preg_match_all("/(.)\"(\S+?)\"(.)/", $str, $matches)) + { + for ($i=0, $s=sizeof($matches['0']); $i < $s; ++$i) + { + if ( ! in_array($matches['1'][$i], $space, TRUE) && ! in_array($matches['3'][$i], $space, TRUE)) + { + $str = str_replace( $matches['0'][$i], + $matches['1'][$i]."“".$matches['2'][$i]."”".$matches['3'][$i], + $str); + } + } + } + + if (preg_match_all("/(.)\'(\S+?)\'(.)/", $str, $matches)) + { + for ($i=0, $s=sizeof($matches['0']); $i < $s; ++$i) + { + if ( ! in_array($matches['1'][$i], $space, TRUE) && ! in_array($matches['3'][$i], $space, TRUE)) + { + $str = str_replace( $matches['0'][$i], + $matches['1'][$i]."‘".$matches['2'][$i]."’".$matches['3'][$i], + $str); + } + } + } + + // How about one apostrophe, as in Rick's + $start = 0; + + while(TRUE) + { + $current = strpos(substr($str, $start), "'"); + + if ($current === FALSE) break; + + $one_before = substr($str, $start+$current-1, 1); + $one_after = substr($str, $start+$current+1, 1); + + if ( ! in_array($one_before, $space, TRUE) && ! in_array($one_after, $space, TRUE)) + { + $str = str_replace( $one_before."'".$one_after, + $one_before."’".$one_after, + $str); + } + + $start = $start+$current+2; + } + + // Em-dashes + $start = 0; + while(TRUE) + { + $current = strpos(substr($str, $start), "--"); + + if ($current === FALSE) break; + + $one_before = substr($str, $start+$current-1, 1); + $one_after = substr($str, $start+$current+2, 1); + $two_before = substr($str, $start+$current-2, 1); + $two_after = substr($str, $start+$current+3, 1); + + if (( ! in_array($one_before, $space, TRUE) && ! in_array($one_after, $space, TRUE)) + OR + ( ! in_array($two_before, $space, TRUE) && ! in_array($two_after, $space, TRUE) && $one_before == ' ' && $one_after == ' ') + ) + { + $str = str_replace( $two_before.$one_before."--".$one_after.$two_after, + $two_before.trim($one_before)."—".trim($one_after).$two_after, + $str); + } + + $start = $start+$current+2; + } + + // Ellipsis + $str = preg_replace("#(\w)\.\.\.(\s|
    |

    )#", "\\1…\\2", $str); + $str = preg_replace("#(\s|
    |

    )\.\.\.(\w)#", "\\1…\\2", $str); + + // Run the translation array we defined above + $str = str_replace(array_keys($table), array_values($table), $str); + + // If there are any stray double quotes we'll catch them here + + $start = 0; + + while(TRUE) + { + $current = strpos(substr($str, $start), '"'); + + if ($current === FALSE) break; + + $one_before = substr($str, $start+$current-1, 1); + $one_after = substr($str, $start+$current+1, 1); + + if ( ! in_array($one_after, $space, TRUE)) + { + $str = str_replace( $one_before.'"'.$one_after, + $one_before."“".$one_after, + $str); + } + elseif( ! in_array($one_before, $space, TRUE)) + { + $str = str_replace( $one_before."'".$one_after, + $one_before."”".$one_after, + $str); + } + + $start = $start+$current+2; + } + + $start = 0; + + while(TRUE) + { + $current = strpos(substr($str, $start), "'"); + + if ($current === FALSE) break; + + $one_before = substr($str, $start+$current-1, 1); + $one_after = substr($str, $start+$current+1, 1); + + if ( ! in_array($one_after, $space, TRUE)) + { + $str = str_replace( $one_before."'".$one_after, + $one_before."‘".$one_after, + $str); + } + elseif( ! in_array($one_before, $space, TRUE)) + { + $str = str_replace( $one_before."'".$one_after, + $one_before."’".$one_after, + $str); + } + + $start = $start+$current+2; + } + + return $str; + } + + // -------------------------------------------------------------------- + + /** + * Format Newlines + * + * Converts newline characters into either

    tags or
    + * + */ + function format_newlines($str) + { + if ($str == '') + { + return $str; + } + + if (strpos($str, "\n") === FALSE) + { + return '

    '.$str.'

    '; + } + + $str = str_replace("\n\n", "

    \n\n

    ", $str); + $str = preg_replace("/([^\n])(\n)([^\n])/", "\\1
    \\2\\3", $str); + + return '

    '.$str.'

    '; + } +} + + ?> \ No newline at end of file diff --git a/system/helpers/url_helper.php b/system/helpers/url_helper.php index baac0e5d8..c05bc2088 100644 --- a/system/helpers/url_helper.php +++ b/system/helpers/url_helper.php @@ -1,12 +1,12 @@ ","\"", "'", "-"), - array("&", "<", ">", """, "'", "-"), - $str); - - // Decode the temp markers back to entities - $str = preg_replace("/$temp(\d+);/","&#\\1;",$str); - $str = preg_replace("/$temp(\w+);/","&\\1;", $str); - - return $str; -} - - +","\"", "'", "-"), + array("&", "<", ">", """, "'", "-"), + $str); + + // Decode the temp markers back to entities + $str = preg_replace("/$temp(\d+);/","&#\\1;",$str); + $str = preg_replace("/$temp(\w+);/","&\\1;", $str); + + return $str; +} + + ?> \ No newline at end of file -- cgit v1.2.3-24-g4f1b