/**
 * Parses a formatted string and returns embed code for a video or audio player. Structure is as follows:
 * 	[middmedia user directory file]
 * user: the user who added the video to the post
 * directory: the user or group name for the MiddMedia directory containing the file
 * file: the name of the video or audio file to play
 */
function middmedia_plugin_callback($match)
{
    $customVideoHeight = $GLOBALS["customVideoHeight"];
    $customVideoWidth = $GLOBALS["customVideoWidth"];
    if (!isset($match[1])) {
        return middmedia_playback_error();
    }
    $tags = explode(" ", $match[1]);
    $service = substr($match[0], 1, strpos($match[0], " ") - 1);
    if ($service == "middtube" && count($tags) < 2 || $service == "middmedia" && count($tags) < 3) {
        return middmedia_playback_error();
    }
    $output = "";
    try {
        $client = new SoapClient(MIDDMEDIA_SOAP_WSDL);
        switch ($service) {
            case "middtube":
                $filename = $tags[1];
                for ($i = 2; $i < count($tags); $i++) {
                    $filename .= " " . $tags[$i];
                }
                if (strpos($filename, '.') === FALSE) {
                    $filename .= ".flv";
                }
                $ext = strpos($filename, ':');
                if ($ext !== FALSE) {
                    $filename = substr($filename, $ext + 1, strlen($filename) - $ext);
                }
                $media = $client->serviceGetVideo($tags[0], 'blogs', MIDDMEDIA_SOAP_KEY, $tags[0], $filename);
                break;
            case "middmedia":
                $filename = $tags[2];
                for ($i = 3; $i < count($tags); $i++) {
                    $filename .= " " . $tags[$i];
                }
                $media = $client->serviceGetVideo($tags[0], 'blogs', MIDDMEDIA_SOAP_KEY, $tags[1], trim($filename));
                break;
            default:
                return middmedia_playback_error();
        }
        $output = $media['embedcode'];
    } catch (Exception $ex) {
        $output = $ex->faultstring;
    }
    if (isset($customVideoHeight) && intval($customVideoHeight)) {
        $output = preg_replace("/height=\"\\d+\"/", "height=\"{$customVideoHeight}\"", $output);
        $output = preg_replace("/width=\"\\d+\"/", "width=\"{$customVideoWidth}\"", $output);
    }
    $heights = array();
    preg_match("/height=\"(\\d+)\"/", $output, $heights);
    $widths = array();
    preg_match("/width=\"(\\d+)\"/", $output, $widths);
    if (intval($heights[1]) < 200 || intval($widths[1]) < 200) {
        $output = "<div id=\"youtubevideo\"\n     style=\"width:" . $widths[1] . "px;height:" . ($heights[1] - 30) . "px;overflow:hidden;\">" . $output . "</div>";
        $output = preg_replace("/embed\\s/", "embed wmode=\"transparent\"", $output);
    }
    if (isset($GLOBALS["textWrap"]) && $GLOBALS["textWrap"] == TRUE) {
        $output = "<div id=\"textwrapdiv\"\n     style=\"margin-top:5px;margin-right:10px;position:relative;float:left;overflow:hidden;display:block;\">" . $output . "</div>";
    }
    // Enclosure support.
    // Added by Adam Franco on 2009-02-27
    //
    // We will store the data for the MiddMedia files in the post in a global array that
    // we will reference later when rendering the RSS feed via our 'rss2_item' action hook
    // that calls our middmedia_add_enclosures() function.
    //
    // The other way to accomplish this result would be to add a custom field to the post
    // called 'enclosure' (that contains this data) at the time the post is saved. The downside
    // of that approach however is that updates to the media (such as URL or size changes)
    // would not be automatically passed through to the feed. This approach keeps that data
    // out of the WordPress database.
    if (!isset($GLOBALS['middmedia_enclosures'])) {
        $GLOBALS['middmedia_enclosures'] = array();
    }
    $post = get_post();
    $post_id = $post->ID;
    if (!isset($GLOBALS['middmedia_enclosures'][$post_id])) {
        $GLOBALS['middmedia_enclosures'][$post_id] = array();
    }
    $GLOBALS['middmedia_enclosures'][$post_id][$media['name']] = array('url' => $media['httpurl'], 'length' => intval($media['size']), 'type' => $media['mimetype']);
    // -- END Enclosure support --
    return $output;
}