public function test_strlen() { // test the native method $this->assert_equal(MultiByte::strlen($this->test_strings['lowercase']), 12); // it's 12 bytes long, each character is 2-bytes wide $this->assert_equal(MultiByte::strlen('abcd'), 4); }
/** * Respond to Javascript callbacks * The name of this method is action_ajax_ followed by what you passed to the context parameter above. */ public function action_ajax_auto_tags( $handler ) { $selected = array(); if( isset( $handler->handler_vars['selected'] ) ) { $selected = Utils::single_array( $handler->handler_vars['selected'] ); } if( isset( $handler->handler_vars['term'] ) && MultiByte::strlen( $handler->handler_vars['term'] ) ) { $search = $handler->handler_vars['term'] . '%'; $tags = new Terms( DB::get_results( "SELECT * FROM {terms} WHERE vocabulary_id = :vid and LOWER(term_display) LIKE LOWER(:crit) ORDER BY term_display ASC", array( 'vid' => Tags::vocabulary()->id, 'crit' => $search ), 'Term' ) ); } else { $tags = Tags::vocabulary()->get_tree( 'term_display ASC' ); } $resp = array(); foreach ( $tags as $tag ) { $resp[] = MultiByte::strpos( $tag->term_display, ',' ) === false ? $tag->term_display : $tag->tag_text_searchable; } if( count( $selected ) ) { $resp = array_diff($resp, $selected ); } // Send the response // $ar = new AjaxResponse(); // $ar->data = $resp; // $ar->out(); echo json_encode( $resp ); }
function test_strlen() { $this->assert_equal(MultiByte::strlen(self::$test_str), mb_strlen(self::$test_str, mb_detect_encoding(self::$test_str))); printf("Test string: %s <br>", self::$test_str); printf("MultiByte string length: %d <br>", MultiByte::strlen(self::$test_str)); printf("mbstring string length without detecting encoding: %d <br>", mb_strlen(self::$test_str)); printf("mbstring string length with detecting encoding: %d <br>", mb_strlen(self::$test_str, mb_detect_encoding(self::$test_str))); }
/** * Trims longer phrases to shorter ones with elipsis in the middle * @param string $str The string to truncate * @param integer $len The length of the returned string * @param bool $middle Whether to place the ellipsis in the middle (true) or at the end (false) * @return string The truncated string */ public static function truncate($str, $len = 10, $middle = true) { // make sure $len is a positive integer if (!is_numeric($len) || 0 > $len) { return $str; } // if the string is less than the length specified, bail out if (MultiByte::strlen($str) <= $len) { return $str; } // okay. Shuold we place the ellipse in the middle? if ($middle) { // yes, so compute the size of each half of the string $len = round(($len - 3) / 2); // and place an ellipse in between the pieces return MultiByte::substr($str, 0, $len) . '…' . MultiByte::substr($str, -$len); } else { // no, the ellipse goes at the end $len = $len - 3; return MultiByte::substr($str, 0, $len) . '…'; } }
/** * get_url returns a fully-qualified URL * 'host' returns http://www.habariproject.org * 'habari' returns http://www.habariproject.org/habari, if you * have Habari installed into a /habari/ sub-directory * 'user' returns one of the following: * http://www.habariproject.org/user * http://www.habariproject.org/user/sites/x.y.z * 'theme' returns one of the following: * http://www.habariproject.org/user/themes/theme_name * http://www.habariproject.org/user/sites/x.y.z/themes/theme_name * 'admin' returns http://www.habariproject.org/admin * 'admin_theme' returns http://www.habariproject.org/system/admin * 'system' returns http://www.habariproject.org/system * 'scripts' returns http://www.habariproject.org/scripts * '3rdparty' returns http://www.habariproject.org/3rdparty * 'hostname' returns www.habariproject.org * @param string the name of the URL to return * @param bool whether to include a trailing slash. Default: No * @return string URL */ public static function get_url( $name, $trail = false ) { $url = ''; switch ( strtolower( $name ) ) { case 'host': $protocol = 'http'; // If we're running on a port other than 80, i // add the port number to the value returned // from host_url $port = 80; // Default in case not set. if ( isset( $_SERVER['SERVER_PORT'] ) ) { $port = $_SERVER['SERVER_PORT']; } $portpart = ''; $host = Site::get_url( 'hostname' ); // if the port isn't a standard port, and isn't part of $host already, add it if ( ( $port != 80 ) && ( $port != 443 ) && ( MultiByte::substr( $host, MultiByte::strlen( $host ) - strlen( $port ) ) != $port ) ) { $portpart = ':' . $port; } if ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] != 'off' ) { $protocol = 'https'; } $url = $protocol . '://' . $host . $portpart; break; case 'habari': if ( null !== self::$habari_url ) { $url = self::$habari_url; } else { $url = Site::get_url( 'host' ); $path = trim( dirname( Site::script_name() ), '/\\' ); if ( '' != $path ) { $url .= '/' . $path; } self::$habari_url = $url; } break; case 'user': $url = Site::get_url( 'host' ) . Site::get_path( 'base', true ) . Site::get_path( 'user' ); break; case 'theme': $theme = Themes::get_theme_dir(); if ( file_exists( Site::get_dir( 'config' ) . '/themes/' . $theme ) ) { $url = Site::get_url( 'user' ) . '/themes/' . $theme; } elseif ( file_exists( HABARI_PATH . '/user/themes/' . $theme ) ) { $url = Site::get_url( 'habari' ) . '/user/themes/' . $theme; } elseif ( file_exists( HABARI_PATH . '/3rdparty/themes/' . $theme ) ) { $url = Site::get_url( 'habari' ) . '/3rdparty/themes/' . $theme; } else { $url = Site::get_url( 'habari' ) . '/system/themes/' . $theme; } break; case 'admin': $url = Site::get_url( 'habari' ) . '/admin'; break; case 'admin_theme': $url = Site::get_url( 'habari' ) . '/system/admin'; break; case 'login': $url = Site::get_url( 'habari' ) . '/auth/login'; break; case 'logout': $url = Site::get_url( 'habari' ) . '/auth/logout'; break; case 'system': $url = Site::get_url( 'habari' ) . '/system'; break; case 'vendor': case 'scripts': $url = Site::get_url( 'system' ) . '/vendor'; break; case '3rdparty': // this should be removed at a later date as it will cause problems // once 'vendor' is adopted, dump the condition! if ( file_exists( HABARI_PATH . '/3rdparty' ) ) { $url = Site::get_url( 'habari' ) . '/3rdparty'; } else { $url = Site::get_url( 'vendor' ); } break; case 'hostname': // HTTP_HOST is not set for HTTP/1.0 requests $url = ( $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0' || !isset( $_SERVER['HTTP_HOST'] ) ) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST']; break; } $url .= Utils::trail( $trail ); $url = Plugins::filter( 'site_url_' . $name, $url ); return $url; }
private static function save($xml, $type) { $timestamp = HabariDateTime::date_create('now'); $result = Cache::set('exportsnapshot__' . $timestamp->int, $xml, 0, true); // 0s expiration, but keep it forever if ($result) { $snapshots = Options::get('exportsnapshot__snapshots', array()); $snapshots[$timestamp->int] = array('size' => MultiByte::strlen($xml), 'type' => $type, 'ts' => $timestamp->int); Options::set('exportsnapshot__snapshots', $snapshots); return true; } else { return false; } }
/** * Stores the list of plugins that are present (not necessarily active) in * the Options table for future comparison. */ public static function set_present() { $plugin_files = Plugins::list_all(); // strip base path foreach ( $plugin_files as $plugin_file ) { $plugin_file = MultiByte::substr( $file, MultiByte::strlen( HABARI_PATH ) ); } $plugin_data = array_map( create_function( '$a', 'return array( "file" => $a, "checksum" => md5_file( $a ) );' ), $plugin_files ); Options::set( 'plugins_present', $plugin_data ); }
public static function substr($str, $begin, $len = null, $use_enc = null) { $ret = false; $enc = self::$hab_enc; if ($use_enc !== null) { $enc = $use_enc; } if (self::$use_library == self::USE_MBSTRING) { if (!isset($len)) { $len = MultiByte::strlen($str) - $begin; } $ret = mb_substr($str, $begin, $len, $enc); } else { $ret = substr($str, $begin, $len); } return $ret; }
public function testStrlen() { $this->assertEquals( MultiByte::strlen( $this->test_strings['lowercase'] ), 6 ); $this->assertEquals( MultiByte::strlen( $this->test_strings['ucfirst'] ), 6 ); $this->assertEquals( MultiByte::strlen( $this->test_strings['international'] ), 39 ); // and perform a single test with an ascii string for code coverage $this->assertEquals( MultiByte::strlen( 'abcd', 'ascii' ), 4 ); }
/** * Get a fully-qualified URL from a filesystem path * * @param string $path The filesystem path * @param string|bool $trail If true, include a trailing slash. If string, append this to the requested url. Default: Add nothing. * @param bool $preserve_file If true, leave the filename on the URL. Default: Remove filename. * @return string URL */ public static function get_from_filesystem($path, $trail = false, $preserve_file = false) { if (!$preserve_file) { $path = dirname($path); } $url = Site::get_url('habari') . MultiByte::substr($path, MultiByte::strlen(HABARI_PATH)); // Replace windows paths with forward slashes $url = str_replace('\\', '/', $url); $url .= Utils::trail($trail); return $url; }
/** * Returns a truncated version of post content when the post isn't being displayed on its own. * Posts are split either at the comment <!--more--> or at the specified maximums. * Use only after applying autop or other paragrpah styling methods. * Apply to posts using: * <code>Format::apply_with_hook_params( 'more', 'post_content_out' );</code> * @param string $content The post content * @param Post $post The Post object of the post * @param string $more_text The text to use in the "read more" link. * @param integer $max_words null or the maximum number of words to use before showing the more link * @param integer $max_paragraphs null or the maximum number of paragraphs to use before showing the more link * @return string The post content, suitable for display */ public static function more( $content, $post, $properties = array() ) { // If the post requested is the post under consideration, always return the full post if ( $post->slug == Controller::get_var( 'slug' ) ) { return $content; } elseif ( is_string( $properties ) ) { $args = func_get_args(); $more_text = $properties; $max_words = ( isset( $args[3] ) ? $args[3] : null ); $max_paragraphs = ( isset( $args[4] ) ? $args[4] : null ); $paramstring = ""; } else { $paramstring = ""; $paramarray = Utils::get_params( $properties ); $more_text = ( isset( $paramarray['more_text'] ) ? $paramarray['more_text'] : 'Read More' ); $max_words = ( isset( $paramarray['max_words'] ) ? $paramarray['max_words'] : null ); $max_paragraphs = ( isset( $paramarray['max_paragraphs'] ) ? $paramarray['max_paragraphs'] : null ); if ( isset( $paramarray['title:before'] ) || isset( $paramarray['title'] ) || isset( $paramarray['title:after'] ) ) { $paramstring .= 'title="'; if ( isset( $paramarray['title:before'] ) ) { $paramstring .= $paramarray['title:before']; } if ( isset( $paramarray['title'] ) ) { $paramstring .= $post->title; } if ( isset( $paramarray['title:after'] ) ) { $paramstring .= $paramarray['title:after']; } $paramstring .= '" '; } if ( isset( $paramarray['class'] ) ) { $paramstring .= 'class="' . $paramarray['class'] . '" '; } } $matches = preg_split( '/<!--\s*more\s*-->/isu', $content, 2, PREG_SPLIT_NO_EMPTY ); if ( count( $matches ) > 1 ) { return ( $more_text != '' ) ? reset( $matches ) . ' <a ' . $paramstring . 'href="' . $post->permalink . '">' . $more_text . '</a>' : reset( $matches ); } elseif ( isset( $max_words ) || isset( $max_paragraphs ) ) { $max_words = empty( $max_words ) ? 9999999 : intval( $max_words ); $max_paragraphs = empty( $max_paragraphs ) ? 9999999 : intval( $max_paragraphs ); $summary = Format::summarize( $content, $max_words, $max_paragraphs ); if ( MultiByte::strlen( $summary ) >= MultiByte::strlen( $content ) ) { return $content; } else { if ( strlen( $more_text ) ) { // Tokenize the summary and link $ht = new HTMLTokenizer( $summary ); $summary_set = $ht->parse(); $ht = new HTMLTokenizer( '<a ' . $paramstring . ' href="' . $post->permalink . '">' . $more_text . '</a>' ); $link_set= $ht->parse(); // Find out where to put the link $end = $summary_set->end(); $key = $summary_set->key(); // Inject the link $summary_set->insert( $link_set, $key ); return (string)$summary_set; } else { return $summary; } } } return $content; }
public static function substr($str, $begin, $len = null, $use_enc = null) { $ret = FALSE; $enc = self::$hab_enc; if ($use_enc !== null) { $enc = $use_enc; } if (self::$use_library == self::USE_MBSTRING) { if (extension_loaded('mbstring')) { if (!isset($len)) { $len = MultiByte::strlen($str) - $begin; } $ret = mb_substr($str, $begin, $len, $enc); } } return $ret; }
/** * Returns a truncated version of post content when the post isn't being displayed on its own. * Posts are split either at the comment <!--more--> or at the specified maximums. * Use only after applying autop or other paragrpah styling methods. * Apply to posts using: * <code>Format::apply_with_hook_params( 'more', 'post_content_out' );</code> * @param string $content The post content * @param Post $post The Post object of the post * @param string $more_text The text to use in the "read more" link. * @param integer $max_words null or the maximum number of words to use before showing the more link * @param integer $max_paragraphs null or the maximum number of paragraphs to use before showing the more link * @param boolean $inside_last Should the link be placed inside the last element, or not? Default: true * @return string The post content, suitable for display */ public static function more($content, $post, $properties = array()) { // If the post requested is the post under consideration, always return the full post if ($post->slug == Controller::get_var('slug')) { return $content; } elseif (is_string($properties)) { $args = func_get_args(); $more_text = $properties; $max_words = isset($args[3]) ? $args[3] : null; $max_paragraphs = isset($args[4]) ? $args[4] : null; $inside_last = isset($args[5]) ? $args[5] : true; $paramstring = ""; } else { $paramstring = ""; $paramarray = Utils::get_params($properties); $more_text = isset($paramarray['more_text']) ? $paramarray['more_text'] : 'Read More'; $max_words = isset($paramarray['max_words']) ? $paramarray['max_words'] : null; $max_paragraphs = isset($paramarray['max_paragraphs']) ? $paramarray['max_paragraphs'] : null; $inside_last = isset($paramarray['inside_last']) ? $paramarray['inside_last'] : true; if (isset($paramarray['title:before']) || isset($paramarray['title']) || isset($paramarray['title:after'])) { $paramstring .= 'title="'; if (isset($paramarray['title:before'])) { $paramstring .= $paramarray['title:before']; } if (isset($paramarray['title'])) { $paramstring .= $post->title; } if (isset($paramarray['title:after'])) { $paramstring .= $paramarray['title:after']; } $paramstring .= '" '; } if (isset($paramarray['class'])) { $paramstring .= 'class="' . $paramarray['class'] . '" '; } } $link_text = '<a ' . $paramstring . ' href="' . $post->permalink . '">' . $more_text . '</a>'; // if we want it inside the last element, make sure there's a space before the link if ($inside_last) { $link_text = ' ' . $link_text; } // check for a <!--more--> link, which sets exactly where we should split $matches = preg_split('/<!--\\s*more\\s*-->/isu', $content, 2, PREG_SPLIT_NO_EMPTY); if (count($matches) > 1) { $summary = reset($matches); } else { // otherwise, we need to summarize it automagically $max_words = empty($max_words) ? 9999999 : intval($max_words); $max_paragraphs = empty($max_paragraphs) ? 9999999 : intval($max_paragraphs); $summary = Format::summarize($content, $max_words, $max_paragraphs); } // if the summary is equal to the length of the content (or somehow greater??), there's no need to add a link, just return the content if (MultiByte::strlen($summary) >= MultiByte::strlen($content)) { return $content; } else { // make sure there's actually text to append before we waste our time if (strlen($more_text)) { // parse out the summary and stick in our linky goodness // tokenize the summary $ht = new HTMLTokenizer($summary); $summary_set = $ht->parse(); // tokenize the link we're adding $ht = new HTMLTokenizer($link_text); $link_set = $ht->parse(); // find out where to put the link by bumping the iterator to the last element $end = $summary_set->end(); // and what index is that? $key = $summary_set->key(); // if we want it inside the last element, we're good to go - if we want it outside, we need to add it as the *next* element if ($inside_last == false) { $key++; } // if the element is a text node, there were no tags; probably not autop'ed yet, just add link as new line if ($end['type'] == HTMLTokenizer::NODE_TYPE_TEXT) { $summary_set->insert($link_set, $key + 1); } else { // inject it, whereever we decided it should go $summary_set->insert($link_set, $key); } // and return a stringified version return (string) $summary_set; } else { // no text to append? just return the summary return $summary; } } return $content; }
/** * Stores the list of plugins that are present (not necessarily active) in * the Options table for future comparison. */ public static function set_present() { $plugin_files = Plugins::list_all(); // strip base path foreach ($plugin_files as $plugin_file) { $plugin_file = MultiByte::substr($plugin_file, MultiByte::strlen(HABARI_PATH)); } $plugin_data = array_map(function ($a) { return array('file' => $a, 'checksum' => md5_file($a)); }, $plugin_files); Options::set('plugins_present', $plugin_data); }
echo $log->ip; ?> </span></span> <span class="module pct10 minor"><span><?php echo $log->module; ?> </span></span> <span class="type pct10 minor"><span><?php echo $log->type; ?> </span></span> <span class="severity pct10 minor"><span><?php echo $log->severity; ?> </span></span> <span class="message pct25 minor<?php if (MultiByte::strlen($log->message) > 40) { echo ' less'; } ?> "><span><?php echo Utils::truncate(Utils::htmlspecialchars($log->message), 40, false); ?> </span></span> <span class="message pct25 minor more"><span><?php echo Utils::htmlspecialchars($log->message); ?> </span></span> </div> <?php }
public function testStrlen() { $this->assertEquals(MultiByte::strlen(self::$test_str), mb_strlen(self::$test_str, mb_detect_encoding(self::$test_str))); }