$1 $1 $1 $2 $2 $2
$2
$1 wrote earlier:
$3
$1 wrote:
$2
$1
    $1
    $1
  • $1
  • $2
    '; /** * Init */ function PluginInit( & $params ) { $this->short_desc = T_('BB formatting e-g [b]bold[/b]'); $this->long_desc = T_('Supported tags and the BB code toolbar are configurable. Supported tags by default are: [b] [i] [s] [color=...] [size=...] [font=...] [quote] [list=1] [list=a] [list] [*] [bg=] [clear]'); } /** * Parse BB code * ( The main purpose of this function is to parsing multilevel lists tags, * i.e. when one [list] is contained inside other [list] ) * * @param string Content * @param array Search list * @param array Replace list * @return string Content */ function parse_bbcode( $content, $search_list, $replace_list ) { if( empty( $content ) ) { // No content, Exit here return $content; } $complex = array(); $simple = array(); foreach( $search_list as $i => $search_text ) { // Split the masks to separate things preg_match( '#(\\\\\[.+\])\(.+\)(\\\\\[.+\])#is', $search_text, $search_tags ); preg_match( '#(<.+>)\$.+(<.+>)#is', $replace_list[ $i ], $replace_tags ); if( !empty( $search_tags ) ) { $complex[] = array( 's_full' => $search_text, 's_start' => '!^'.$search_tags[1].'!is', 's_end' => '!^'.$search_tags[2].'!is', 'r_full' => $replace_list[ $i ], 'r_start' => $replace_tags[1], 'r_end' => $replace_tags[2], ); } else { $simple[] = array( 's_full' => $search_text, 'r_full' => $replace_list[ $i ], ); } } //pre_dump($complex); foreach( $simple as $tag_info ) { // Make simple replacements $content = preg_replace( $tag_info['s_full'], $tag_info['r_full'], $content ); } // Complex parsing with multilevel tags structure $c_pos = 0; $content_length = strlen( $content ); $opened_tags = array(); $new_content = $content; while( $c_pos < $content_length - 2 ) { if( $content[ $c_pos ].$content[ $c_pos + 1 ] == '[/' ) { // Closed tag if( !empty( $opened_tags ) ) { // Get closed tag that was opened as last $sub_content = substr( $content, $c_pos ); $closed_tag_info = $complex[ $opened_tags[ count( $opened_tags ) - 1 ] ]; if( preg_match( $closed_tag_info['s_end'], $sub_content ) ) { $first_part = str_replace( $sub_content, '', $new_content ); $sub_content = preg_replace( $closed_tag_info['s_end'], $closed_tag_info['r_end'], $sub_content ); $new_content = $first_part.$sub_content; //pre_dump( 'CLOSE', $opened_tags, $closed_tag_info['r_end'], $closed_tag_info['s_end'], $new_content ); // Remove this tag from opened tags array array_pop( $opened_tags ); } } } elseif( $content[ $c_pos ] == '[' ) { // Opened tag $sub_content = substr( $content, $c_pos ); foreach( $complex as $tag_num => $tag_info ) { if( preg_match( $tag_info['s_start'], $sub_content ) ) { $first_part = str_replace( $sub_content, '', $new_content ); $sub_content = preg_replace( $tag_info['s_start'], $tag_info['r_start'], $sub_content ); $new_content = $first_part.$sub_content; // Add this tag to know what tag was opened as last $opened_tags[] = $tag_num; //pre_dump( 'OPEN', $opened_tags, $tag_info['s_start'], $new_content ); break; } } } $c_pos++; } //pre_dump($new_content); $new_content = $this->parse_anchor_links( $new_content ); return $new_content; } /** * Parse anchor links, Set absolute path for each link with relative anchor like * * @param string Content * @return string Content */ function parse_anchor_links( $content ) { if( preg_match_all( '/ href="#(c|p)([0-9]+)"/i', $content, $matches ) ) { $CommentCache = & get_CommentCache(); $ItemCache = & get_ItemCache(); foreach( $matches[0] as $m => $full_match ) { $object_ID = $matches[2][$m]; $new_url = ''; switch( $matches[1][$m] ) { // Object type: case 'p': // Item if( $Item = & $ItemCache->get_by_ID( $object_ID, false, false ) ) { // Replace anchor url with item permanent url $new_url = $Item->get_permanent_url().'#p'.$object_ID; } break; case 'c': // Comment if( $Comment = & $CommentCache->get_by_ID( $object_ID, false, false ) ) { // Replace anchor url with comment permanent url $new_url = $Comment->get_permanent_url(); } break; default: // Incorrect object type, Skip this url continue 2; } if( !empty( $new_url ) ) { // Replace relative anchor url with new absolute url $content = str_replace( $full_match, ' href="'.$new_url.'"', $content ); } } } return $content; } /** * The following function are here so the events will be registered * @see Plugins_admin::get_registered_events() */ function RenderItemAsHtml( & $params ) { parent::RenderItemAsHtml( $params ); } function RenderMessageAsHtml( & $params ) { parent::RenderMessageAsHtml( $params ); } function RenderEmailAsHtml( & $params ) { parent::RenderItemAsHtml( $params ); } /** * Do the same as for HTML. * * @see RenderItemAsHtml() */ function RenderItemAsXml( & $params ) { $this->RenderItemAsHtml( $params ); } /** * * Render comments if required * * @see Plugin::FilterCommentContent() */ function FilterCommentContent( & $params ) { $Comment = & $params['Comment']; $comment_Item = & $Comment->get_Item(); $item_Blog = & $comment_Item->get_Blog(); if( in_array( $this->code, $Comment->get_renderers_validated() ) ) { // apply_comment_rendering is set to always render $content = & $params['data']; if( !isset( $this->comment_search_list ) ) { // Init comment search list only first time: $this->comment_search_list = $this->prepare_search_list( $this->get_coll_setting( 'coll_comment_search_list', $item_Blog ) ); } if( !isset( $this->comment_replace_list ) ) { // Init comment replace list only first time: $this->comment_replace_list = $this->prepare_replace_list( $this->get_coll_setting( 'coll_comment_replace_list', $item_Blog ) ); } $content = replace_content_outcode_shorttags( $this->comment_search_list, $this->comment_replace_list, $content, array( $this, 'parse_bbcode' ) ); } } /** * Event handler: Called when displaying editor toolbars on post/item form. * * This is for post/item edit forms only. Comments, PMs and emails use different events. * * @param array Associative array of parameters * @return boolean did we display a toolbar? */ function AdminDisplayToolbar( & $params ) { $params['target_type'] = 'Item'; return $this->DisplayCodeToolbar( $params ); } /** * Event handler: Called when displaying editor toolbars on comment form. * * @param array Associative array of parameters * @return boolean did we display a toolbar? */ function DisplayCommentToolbar( & $params ) { $params['target_type'] = 'Comment'; return $this->DisplayCodeToolbar( $params ); } /** * Event handler: Called when displaying editor toolbars for message. * * @param array Associative array of parameters * @return boolean did we display a toolbar? */ function DisplayMessageToolbar( & $params ) { $params['target_type'] = 'Message'; return $this->DisplayCodeToolbar( $params ); } /** * Event handler: Called when displaying editor toolbars for email. * * @param array Associative array of parameters * @return boolean did we display a toolbar? */ function DisplayEmailToolbar( & $params ) { $apply_rendering = $this->get_email_setting( 'email_apply_rendering' ); if( ! empty( $apply_rendering ) && $apply_rendering != 'never' ) { $params['target_type'] = 'EmailCampaign'; return $this->DisplayCodeToolbar( $params ); } return false; } /** * Prepare a search list * * @param string String value of a search list * @return array The search list as array */ function prepare_search_list( $search_list_string ) { $search_list_array = explode( "\n", str_replace( "\r", '', $search_list_string ) ); foreach( $search_list_array as $l => $line ) { // Remove button name from regexp string $line = explode( ' ', $line, 2 ); if( empty( $line[1] ) ) { // Bad format of search string unset( $search_list_array[ $l ] ); } else { // Replace this line with regexp value (to delete a button name) $search_list_array[ $l ] = $line[1]; } } return $search_list_array; } } ?>