BBCode markup; provides automatic integration with lightbox
Author: Hydra, NoWhereMan
Author URI: http://flatpress.sf.net
*/
define('BBCODE_ESCAPE_HTML', true);
define('BBCODE_ENABLE_COMMENTS', false);
define('BBCODE_USE_EDITOR', true);
define('BBCODE_URL_MAXLEN', 40);
require plugin_getdir('bbcode') . '/inc/stringparser_bbcode.class.php';
function plugin_bbcode_defines() {
$funcs = explode(',', ini_get('disable_functions'));
if (!file_exists('getfile.php')) {
define('BBCODE_USE_WRAPPER', false);
return;
}
if (in_array('readfile',$funcs)) {
define('BBCODE_USE_WRAPPER', false);
} else {
define('BBCODE_USE_WRAPPER', true);
}
}
plugin_bbcode_defines();
function plugin_bbcode_style() {
echo "\n\n";
echo '', "\n";
if (BBCODE_USE_EDITOR)
echo '', "\n";
echo "\n";
}
add_action('wp_head', 'plugin_bbcode_style');
function bbcode_remap_url(&$d) {
// NWM: "attachs/" is interpreted as a keyword, and it is translated to the actual path of ATTACHS_DIR
// CHANGE! we use the getfile.php script to mask the actual path of the attachs dir!
if (strpos($d, ':')===false) { // if is relative url
// absolute path, relative to this server
if ($d{0} == '/') {
// BLOG_BASEURL contains a trailing slash in the end
// if $d begins with a slash, we first strip it
// otherwise the string would look like
// http://mysite.com/flatpress//path/you/entered
// ^^^ ugly double slash :P
$d = BLOG_BASEURL . substr($d, 1);
}
if (substr($d, 0, 8) == 'attachs/') {
if (BBCODE_USE_WRAPPER) {
$d = "getfile.php?f=" . basename($d) . "&dl=true";
} else {
$d = substr_replace ($d, ATTACHS_DIR, 0, 8 );
}
return true;
}
if (substr($d, 0, 7) == 'images/') {
$d = substr_replace ($d, IMAGES_DIR, 0, 7 );
$d = BBCODE_USE_WRAPPER? ("getfile.php?f=" . basename($d)) : $d;
}
return true;
}
if (strpos($d, 'www.')===0) {
$d = 'http://' . $d;
}
return false;
}
function do_bbcode_url ($action, $attributes, $content, $params, $node_object) {
if ($action == 'validate') {
return true;
}
// the code was specified as follows: [url]http://.../[/url]
if (!isset ($attributes['default'])) {
/* cut url if longer than > BBCODE_URL_MAXLEN */
$url = $content;
if (($l = strlen($url))>BBCODE_URL_MAXLEN) {
$t = (int)(BBCODE_URL_MAXLEN/2);
$content = substr($url, 0, $t) .
' … ' .
substr($url, $l-$t);
}
} else {
// else the code was specified as follows: [url=http://.../]Text[/url]
$url = $attributes['default'];
}
$the_url = ($local = bbcode_remap_url($url))? (BLOG_BASEURL . $url) : $url ;
$content = $content;
return ''.$content.'';
}
// Function to include images
function do_bbcode_img ($action, $attributes, $content, $params, $node_object) {
if ($action == 'validate') {
return true;
}
if (!isset($attributes['default'])) return '[No valid img specified]';
$absolutepath = $actualpath = $attributes['default'];
$image_is_local = false;
// NWM: "images/" is interpreted as a keyword, and it is translated to the actual path of IMAGES_DIR
$image_is_local = bbcode_remap_url($actualpath);
$float = '';
$popup_start = '';
$popup_end = '';
$img_size = array();
// let's disable socket functions for remote files
// slow remote servers may otherwise lockup the system
if ($image_is_local) {
$img_size = @getimagesize($actualpath);
$absolutepath = BLOG_BASEURL . $actualpath;
}
$orig_w = $width = isset($img_size[0])? $img_size[0] : 0;
$orig_h = $height = isset($img_size[1])? $img_size[1] : 0;
$thumbpath = null;
$scalefact = 0; // default: resize to 0, which means leaving it as it is, as width and hiehgt will be ignored ;)
// scale attribute has priority over width and height
// if scale is set popup is set to true automatically, unless it is explicitly set to false
if (isset($attributes['scale'])) {
if (substr($attributes['scale'], -1, 1) == '%') {
$val = substr($attributes['scale'], 0,-1); // NN%, we ignore %
} else { $val = $attributes['scale'] ; }
$scalefact = $val / 100.0;
$width = (int)($scalefact*$width);
$height= (int)($scalefact*$height);
// if both width and height are set, we assume proportions are ok
}elseif (isset($attributes['width']) && isset($attributes['height'])) {
$width = (int)$attributes['width'];
$height = (int)$attributes['height'];
// if width or height are set we calc proportions
}elseif (isset($attributes['width'])) {
$scalefact = ($attributes['width'] / $orig_w);
$width = (int) $attributes['width'];
$height = (int) ($scalefact*$orig_h);
}elseif (isset($attributes['height'])) {
$scalefact = ($attributes['height'] / $orig_h);
$height = (int) $attributes['height'];
$width = (int) ($scalefact*$orig_w);
}
if ($height<$orig_h) $attributes['popup']=true;
if ($height != $orig_h) {
/**
* bbcode_img_scale_filter($actualpath, $img_props, $newsize)
*
*/
$thumbpath = apply_filters(
'bbcode_img_scale',
$actualpath,
$img_size,
array($width,$height)
);
}
$alt = $title = basename($actualpath);
if (isset($attributes['popup']) && ($attributes['popup'])) {
$pop_width = ($orig_w) ? $orig_w : 800;
$pop_height = ($orig_h) ? $orig_h : 600;
$popup = ' onclick="' .
"Popup=window.open('$absolutepath','Popup','toolbar=no,location=no,status=no,".
"menubar=no,scrollbars=yes,resizable=yes,width=$pop_width,height=$pop_height');"
.' return false;"';
/**
* plugin hook, here lightbox attachs
*/
$popup = apply_filters('bbcode_img_popup', $popup, $absolutepath);
$popup_start = ($attributes['popup']=='true') ?
'' : '';
$popup_end = ($attributes['popup']=='true') ? '' : '';
}
$img_width = ($width)? ' width="'.$width.'"' : '';
$img_height = ($height) ? ' height="'.$height.'"' : '' ;
if (isset ($attributes['float'])) {
$float = ($attributes['float']=='left' || $attributes['float']=='right') ?
' class="float'.$attributes['float'].'"' : ' class="center"';
}
return $popup_start.''.$popup_end;
}
// Function for embedding videos
function do_bbcode_video($action, $attr, $content, $params, $node_object) {
if ($action=='validate') {
return true;
}
$vurl = parse_url($attr['default']);
if (isset($attr['type'])) {
$type = $attr['type'];
} else {
// is it http://www.MYSITE.com or http://MYSITE.com ?
$web = explode('.', $vurl['host']);
array_pop($web);
$type = isset($web[1])? $web[1] : $web[0];
}
$query = utils_kexplode($vurl['query'], '=&');
$the_url = null;
$others = '';
switch ($type) {
case 'google':
$the_url = "http://video.google.com/googleplayer.swf?docid={$query['docid']}";
$others = '';
break;
case 'youtube':
$the_url = "http://youtube.com/v/{$query['v']}";
break;
case 'default':
$the_url = null;
}
if ($the_url) {
$width = isset($attr['width'])? $attr['width'] : '400';
$height = isset($attr['height'])? $attr['height'] : '326';
$float = isset($attr['float'])? "float: {$attr['float']}" : '';
return
'';
}
return '[unsupported video]';
}
// Function to return code
function do_bbcode_code ($action, $attributes, $content, $params, $node_object) {
if ($action == 'validate') {
return true;
}
$temp_str = $content;
$temp_str = str_replace( '
', chr(10), $temp_str );
$temp_str = str_replace( chr(10).chr(10), chr(10), $temp_str );
$temp_str = str_replace( chr(32), ' ', $temp_str );
$a = '';
if (function_exists('plugin_syntaxhighlighter_foot')) {
if (isset($attributes['default'])) {
$a = $attributes['default'];
$p = explode(':', $a);
plugin_syntaxhighlighter_add($p[0]);
}
}
return "
$temp_str"; } // Function to return html function do_bbcode_html ($action, $attributes, $content, $params, $node_object) { if ($action == 'validate') { return true; } static $count = 0; // NWM: life is hell -___-' if(!isset($GLOBALS['BBCODE_TEMP_HTML'])) $GLOBALS['BBCODE_TEMP_HTML'] = array(); $GLOBALS['BBCODE_TEMP_HTML'][$count] = $content; $str = ""; $count++; return $str; } function do_bbcode_color ($action, $attributes, $content, $params, $node_object) { if ($action == 'validate') { return true; } return "$content"; } function do_bbcode_size ($action, $attributes, $content, $params, $node_object) { if ($action == 'validate') { return true; } return "$content"; } function do_bbcode_align($action, $attr, $content, $params, $node_object) { return "
'), 'inline', array ('listitem', 'block', 'inline', 'link'), array ()); // [QUOTE] $bbcode->addCode ('quote', 'simple_replace', null, array ('start_tag' => '', 'end_tag' => '
'), 'inline', array ('listitem', 'block', 'inline', 'link'), array ()); // [PRE] $bbcode->addCode ('pre', 'simple_replace', null, array ('start_tag' => '', 'end_tag' => '
', 'end_tag' => ''), 'inline', array ('listitem', 'block', 'inline', 'link'), array ()); // [CODE] $bbcode->addCode ('code', 'usecontent', 'do_bbcode_code', array (), 'inline', array ('listitem', 'block', 'inline', 'link'), array ()); return $bbcode->parse($text); } // this is for [html] tag postprocess function plugin_bbcode_undoHtmlCallback($match) { return clean_pre($match[1]); //builtin function (see core.wp-formatting) } function plugin_bbcode_undoHtml($text) { //return preg_replace_callback('|(.*)|sU', 'plugin_bbcode_undoHtmlCallback', $text); if (isset($GLOBALS['BBCODE_TEMP_HTML'])) { foreach ($GLOBALS['BBCODE_TEMP_HTML'] as $n => $content) { // html_entity_decode($content) $content = str_replace('<', '<', $content); $content = str_replace('>', '>', $content); $text = str_replace("", $content, $text); } $GLOBALS['BBCODE_TEMP_HTML'] = array(); } return $text; } //add_filter('comment_text', 'plugin_bbcode_comment'); add_filter('title_save_pre', 'wp_specialchars',1); if (BBCODE_ESCAPE_HTML) add_filter('content_save_pre', 'wp_specialchars',1); add_filter('pre_comment_author_name', 'wp_specialchars'); add_filter('pre_comment_content', 'wp_specialchars'); add_filter('the_content', 'BBCode', 1); add_filter('the_excerpt', 'BBCode', 1); add_filter('the_content', 'plugin_bbcode_undoHtml', 30); if (BBCODE_USE_EDITOR) { add_filter('editor_toolbar', 'plugin_bbcode_toolbar'); } if (BBCODE_ENABLE_COMMENTS) { add_filter('comment_text', 'plugin_bbcode_comment', 1); } ?>