Flash detection and embedding JavaScript. The syntax is [kml_flashembed movie="filename.swf" height="250" width="400" /]. Version: 1.4 Author: Michael Bester Author URI: http://www.kimili.com Update: http://www.kimili.com/plugins/kml_flashembed/wp */ /* * * KIMILI FLASH EMBED * * Copyright 2007 Michael Bester (http://www.kimili.com) * Released under the GNU General Public License (http://www.gnu.org/licenses/gpl.html) * */ /*********************************************************************** * Global Vars ************************************************************************/ $kml_request_type = ""; $kml_flashembed_ver = "1.4"; /*********************************************************************** * Load Dependencies ************************************************************************/ require_once('buttonsnap.php'); /*********************************************************************** * Run the main function ************************************************************************/ function kml_flashembed($content) { $pattern = '/(

[\s\n\r]*)??(([\[<]KML_(FLASH|SWF)EMBED.*\/[\]>])|([\[<]KML_(FLASH|SWF)EMBED.*[\]>][\[<]\/KML_(FLASH|SWF)EMBED[\]>]))([\s\n\r]*<\/p>)??/Umi'; $result = preg_replace_callback($pattern,'kml_flashembed_parse_kfe_tags',$content); return $result; } /*********************************************************************** * Parse out the KFE Tags ************************************************************************/ function kml_flashembed_parse_kfe_tags($match) { $r = ""; # Clean up and untexturize tag $strip = array('[KML_FLASHEMBED', '][/KML_FLASHEMBED]', '[kml_flashembed', '][/kml_flashembed]', '[KML_SWFEMBED', '][/KML_SWFEMBED]', '[kml_swfembed', '][/kml_swfembed]', '/]', '', '', '', '', '/>', '\n', '
', '
', '

', '

' ); $elements = str_replace($strip, '', $match[0]); $elements = preg_replace("/=(\s*)\"/", "==`", $elements); $elements = preg_replace("/=(\s*)″/", "==`", $elements); $elements = preg_replace("/=(\s*)′/", "==`", $elements); $elements = preg_replace("/=(\s*)”/", "==`", $elements); $elements = preg_replace("/\"(\s*)/", "`| ", $elements); $elements = preg_replace("/″(\s*)/", "`|", $elements); $elements = preg_replace("/′(\s*)/", "`|", $elements); $elements = preg_replace("/”(\s*)/", "`|", $elements); $elements = preg_replace("/″(\s*)/", "`|", $elements); $elements = preg_replace("/‘(\s*)/", "'", $elements); $elements = preg_replace("/’(\s*)/", "'", $elements); $attpairs = preg_split('/\|/', $elements, -1, PREG_SPLIT_NO_EMPTY); $atts = array(); // Create an associative array of the attributes for ($x = 0; $x < count($attpairs); $x++) { $attpair = explode('==', $attpairs[$x]); $attn = trim(strtolower($attpair[0])); $attv = preg_replace("/`/", "", trim($attpair[1])); $atts[$attn] = $attv; } if (isset($atts['movie']) && isset($atts['height']) && isset($atts['width'])) { $atts['fversion'] = (isset($atts['fversion'])) ? $atts['fversion'] : 8; if (isset($atts['fvars'])) { $fvarpair_regex = "/(?", $atts['alttext']) : ''; // If we're not serving up a feed, generate the script tags if ($GLOBALS['kml_request_type'] != "feed") { $r = kml_flashembed_build_fo_script($atts); } else { $r = kml_flashembed_build_object_tag($atts); } } return $r; } /*********************************************************************** * Build the Javascript from the tags ************************************************************************/ function kml_flashembed_build_fo_script($atts) { if (is_array($atts)) extract($atts); $out = array(); $ret = ""; $rand = mt_rand(); // For making sure this instance is unique // Extract the filename minus the extension... $swfname = (strrpos($movie, "/") === false) ? $movie : substr($movie, strrpos($movie, "/") + 1, strlen($movie)); $swfname = (strrpos($swfname, ".") === false) ? $swfname : substr($swfname, 0, strrpos($swfname, ".")); // ... to use as a default ID if an ID is not defined. $fid = (isset($fid)) ? $fid : "fm_" . $swfname . "_" . $rand; //echo("clickomg:"+$clickimg);// = '"data/logga/supertryck3_small.jpg"'; // ... as well as an empty target if that isn't defined. if (empty($target)) { $targname = "so_targ_" . $swfname . "_" . $rand; $classname = (empty($targetclass)) ? "flashmovie" : $targetclass; // Create a target div if (isset($clickimg)) $out[] = '
'.$alttext.'
'; else if (isset($clicktext)) $out[] = '
'.$clicktext.'
'; //else if (isset($clicktext)) $out[] = '
'.$clicktext.'
'; else $out[] = '
'.$alttext.'
'; $target = $targname; } // Set variables for rendering JS $c_movie = '"'.$movie.'",'; $c_fid = '"'.$fid.'",'; $c_width = '"'.$width.'",'; $c_height = '"'.$height.'",'; $fversion = '"'.$fversion.'",'; $bgcolor = (isset($bgcolor)) ? '"'.$bgcolor.'",' : '"",'; $useexpressinstall = (isset($useexpressinstall) && $useexpressinstall == 'true') ? true : false; $quality = (isset($quality)) ? '"'.$quality.'",' : '"",'; $xiredirecturl = (isset($xiredirecturl)) ? '"'.$xiredirecturl.'",' : '"",'; $redirecturl = (isset($redirecturl)) ? '"'.$redirecturl.'",' : '"",'; $detectKey = (isset($detectKey)) ? '"'.$detectKey.'"' : '""'; $fvars = (isset($fvars)) ? $fvars : array(); $funcmode = (isset($clickimg) || isset($clicktext)); $out[] = ''; $out[] = ''; // Add NoScript content if (!empty($noscript)) { $out[] = ''; } $out[] = ''; $ret .= join("\n", $out); return $ret; } /*********************************************************************** * Build a Satay Object for RSS feeds ************************************************************************/ function kml_flashembed_build_object_tag($atts) { $out = array(); if (is_array($atts)) extract($atts); // Build a query string based on the $fvars attribute $querystring = (count($fvars) > 0) ? "?" : ""; for ($i = 0; $i < count($fvars); $i++) { $thispair = trim($fvars[$i]); $nvpair = explode("=",$thispair); $name = trim($nvpair[0]); $value = ""; for ($j = 1; $j < count($nvpair); $j++) { // In case someone passes in a fvars with additional "=" $value .= trim($nvpair[$j]); $value = preg_replace('/&/', '&', $value); if ((count($nvpair) - 1) != $j) { $value .= "="; } } // Prune out JS or PHP values if (preg_match("/^\\$\\{.*\\}/i", $value)) { // JS $endtrim = strlen($value) - 3; $value = substr($value, 2, $endtrim); $value = str_replace(';', '', $value); } else if (preg_match("/^\\?\\{.*\\}/i", $value)) { // PHP $endtrim = strlen($value) - 3; $value = substr($value, 2, $endtrim); $value = '"'.eval("return " . $value).'"'; } else { $value = '"'.$value.'"'; } $querystring .= $name . '=' . $value; if ($i > count($fvars)) { $querystring .= "&"; } } $out[] = ''; $out[] = ''; $out[] = ' '; if (isset($play)) $out[] = ' '; if (isset($loop)) $out[] = ' '; if (isset($menu)) $out[] = ' '; if (isset($scale)) $out[] = ' '; if (isset($wmode)) $out[] = ' '; if (isset($align)) $out[] = ' '; if (isset($salign)) $out[] = ' '; if (isset($base)) $out[] = ' '; if (isset($allowscriptaccess)) $out[] = ' '; if (isset($allowfullscreen)) $out[] = ' '; $out[] = ''; $ret .= join("\n", $out); return $ret; } /*********************************************************************** * Add the call to flashobject.js ************************************************************************/ function kml_flashembed_add_flashobject_js() { global $kml_flashembed_ver; echo ' '; } /*********************************************************************** * Toolbar Button Functions * Props to Alex Rabe for fuguring out the WP 2.1 buttonsnap workaround * http://alexrabe.boelinger.com/?page_id=46 ************************************************************************/ function kml_flashembed_addbuttons() { global $wp_db_version; // Check for WordPress 2.1+ and activated RTE if ( 3664 <= $wp_db_version && 'true' == get_user_option('rich_editing') ) { // add the button for wp21 in a new way add_filter("mce_plugins", "kml_flashembed_button_plugin", 0); add_filter('mce_buttons', 'kml_flashembed_button', 0); add_action('tinymce_before_init','kml_flashembed_button_script'); } else { // Do it in the old way with buttonsnap $button_image_url = buttonsnap_dirname(__FILE__) . '/kfe/images/flash.gif'; buttonsnap_separator(); buttonsnap_ajaxbutton($button_image_url, __('Kimili Flash Embed', 'kfe'), 'kml_flashembed_insert_hook'); add_filter('kml_flashembed_insert_hook', 'kml_flashembed_insert_tag'); } } // used to insert button in wordpress 2.1x editor function kml_flashembed_button($buttons) { array_push($buttons, "separator", "kfe"); return $buttons; } // Tell TinyMCE that there is a plugin (wp2.1) function kml_flashembed_button_plugin($plugins) { array_push($plugins, "kfe"); return $plugins; } // Load the TinyMCE plugin : editor_plugin.js (wp2.1) function kml_flashembed_button_script() { $pluginURL = buttonsnap_dirname(__FILE__) . '/kfe/'; echo 'tinyMCE.loadPlugin("kfe", "'.$pluginURL.'");'."\n"; return; } function kml_flashembed_insert_tag($selectedtext) { return '[kml_flashembed movie="'. $selectedtext . '" height="" width="" /]'; } /*********************************************************************** * Apply the filter ************************************************************************/ if (preg_match("/(\/\?feed=|\/feed)/i",$_SERVER['REQUEST_URI'])) { // RSS Feeds $kml_request_type = "feed"; } else { // Everything else $kml_request_type = "nonfeed"; add_action('wp_head', 'kml_flashembed_add_flashobject_js'); add_action('init', 'kml_flashembed_addbuttons'); } // Apply all over except the admin section if (strpos($_SERVER['REQUEST_URI'], 'wp-admin') === false ) { add_action('template_redirect','kmlDoObStart'); } /*********************************************************************** * Trigger Function ************************************************************************/ function kmlDoObStart() { ob_start('kml_flashembed'); } ?>