Multiple [code ] blocks in geeklog

| | Comments (1)
Geeklog has a cool feature that allows you to post php/perl/html code into a story and have it render correctly. But the original design only allowed one block. I fixed that. Read on to see the details.

There are two chunks of code below; the old function and the new one. The old one checked once for a [code ] block and stopped. The new one loops through occurences of [code ]'s. You can download my haxxed lib-common from here.

Beware if you put a [code ] or [/code ] inside a [code ] block you will get stange behavior because the parser is simple. I got it to work in this story by replacing [ and ] with [ and ] respectively

Here is their original


/**
* This function checks html tags.
*
* The core of this code has been lifted from phpslash which is licenced under
* the GPL.  It checks to see that the HTML tags are on the approved list and
* removes them if not.
*
* @param        string      $str        HTML to check
* @see function COM_checkHTML
* @return       string  Filtered HTML
*
*/

function COM_checkHTML( $str ) 
{
    global $_CONF;
        
    $str = stripslashes($str);

    // Get rid of any newline characters
    $str = preg_replace( "/\n/", '', $str );
    
    // Replace any $ with $ (HTML equiv)
    $str = str_replace( '$', '$', $str );
    
   

    // loop through consecutive  meta tags
    $start_pos = strpos( strtolower( $str ), '' );
    $end_pos = 0;
    while ( !( $start_pos === false ) )
    {
        if( !( $start_pos === false ))
        {
            $end_pos = strpos( strtolower( $str ), '[/code]', $end_pos  );
            $end_pos += 7; // to account for the length of the tag

            $orig_pre_string = substr( $str, $start_pos, $end_pos - $start_pos );
            $new_pre_string = str_replace( '', '', $orig_pre_string );
            $new_pre_string = str_replace( '', '>', $new_pre_string );
            $new_pre_string = str_replace( '[code]', '', $new_pre_string );
            $new_pre_string = str_replace( '[CODE]', '', $new_pre_string );
            $new_pre_string = str_replace( '[/code]', '', $new_pre_string );
            $new_pre_string = str_replace( '[/CODE]', '', $new_pre_string );
            $new_pre_string = nl2br( $new_pre_string );
            $str = str_replace( $orig_pre_string, '[code]' . $new_pre_string . '', $str );
        }
        $start_pos = strpos( strtolower( $str ), '', $end_pos );
    } // ends while loop

    if( !SEC_hasRights( 'story.edit' ) || empty ( $_CONF['adminhtml'] ))
    {
        $str = strip_tags( $str, $_CONF['allowablehtml'] );
    }
    else
    {
        $str = strip_tags( $str, $_CONF['adminhtml'] );
    }

    return COM_killJS( $str );
}
Here is my new version of the code.

/**
* Makes an ID based on current date/time
*
* This function COM_creates a 17 digit sid for stories based on the 14 digit date
* and a 3 digit random number that was seeded with the number of microseconds
* (.000001th of a second) since the last full second. NOTE: this is now used for more than
* just stories!
*
* 10/13/2002 4:19AM jim AT jimweller.net added handling for mulitple  meta-tags
*
* @return	string	$sid  Story ID
*
*/

function COM_checkHTML( $str ) 
{
    global $_CONF;
	
    $str = stripslashes($str);

    // Get rid of any newline characters
    $str = preg_replace( "/\n/", '', $str );
    
    // Replace any $ with $ (HTML equiv)
    $str = str_replace( '$', '$', $str );
    
   

    // loop through consecutive [code] meta tags
    $start_pos = strpos( strtolower( $str ), '[code]' );
    $end_pos = 0;
    while ( ! ( $start_pos === false ) )
    {
        $end_pos = strpos( strtolower( $str ), '[/code]', $end_pos  );
        $end_pos += 7; // to account for the length of the tag
        $orig_pre_string = substr( $str, $start_pos, $end_pos - $start_pos );
        $new_pre_string = str_replace( '\\', '\', $orig_pre_string );
        $new_pre_string = str_replace( '<', '<', $new_pre_string );
        $new_pre_string = str_replace( '>', '>', $new_pre_string );
        $new_pre_string = str_replace( '[code]', '', $new_pre_string );
        $new_pre_string = str_replace( '[CODE]', '', $new_pre_string );
        $new_pre_string = str_replace( '[/code]', '', $new_pre_string );
        $new_pre_string = str_replace( '[/CODE]', '', $new_pre_string );
        $new_pre_string = nl2br( $new_pre_string );
        $str = str_replace( $orig_pre_string, '<pre><code>' . $new_pre_string . '</code></pre>', $str );
        $start_pos = strpos( strtolower( $str ), '[code]', $end_pos );
    } // ends while loop

    if( !SEC_hasRights( 'story.edit' ) || empty ( $_CONF['adminhtml'] ))
    {
        $str = strip_tags( $str, $_CONF['allowablehtml'] );
    }
    else
    {
        $str = strip_tags( $str, $_CONF['adminhtml'] );
    }

    return COM_killJS( $str );
}

1 Comments

I lament that they had already fixed it in CVS. I'm using that cvs now on http://geek.jimweller.net