<?php
/*
 * $Id: mp3.php,v 1.2 2003/04/29 17:45:38 k-fish Exp $
 */

/**
 * The MIME_Viewer_mp3 class renders out .mp3 files in HTML format by
 * executing mp3info query mode.
 */
class MIME_Viewer_mp3 extends MIME_Viewer {

    /**
     * Render out the currently set contents using mp3info. The
     * $mime_part class variable has the information to render out,
     * encapsulated in a MIME_Part object.
     *
     * @return string HTML pretty-print of the content
     */
    function render($params = null)
    {
        include('inc/mime/mime_drivers.php');

        /* Check to make sure the program actually exists. */
        if (file_exists($mime_drivers['mp3']['location']) === false) {
            return sprintf("The program used to view this message type (%s) was not found on the system.", $mime_drivers['mp3']['location']);
        }

        $tmp_mp3 = tempnam('','mgw_files');

        $fh = fopen($tmp_mp3, 'w');
        fwrite($fh, $this->mime_part->getContents());
        fclose($fh);

        $pipe = popen($mime_drivers['mp3']['location'] . " -x $tmp_mp3 2>&1", 'r');
        $data = '<pre class="file_content">';

        $re = '';
        while (($rc = fgets($pipe, 8192))) {
            $re .= $rc;
        }

        pclose($pipe);
	unlink($tmp_mp3);

        $data .= htmlspecialchars($re, ENT_QUOTES, $_SESSION['MGW']->charset);
        $data .= '</pre>';

        return $data;
    }

    function getType()
    {
        return 'text/html';
    }

}
?>
