<?php

function _configMainHTML($prmPluginInfo) {
    $html  = '<html xmlns="http://www.w3.org/1999/xhtml">';
    $html .= '<head>';
    $html .= '<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />';
    $html .= '<title>' . $prmPluginInfo['name'] . '</title>';
    $html .= '</head>';
    $html .= '<body>';
    $html .= '<p align="center"><strong>' . $prmPluginInfo['name'] . '</strong><br>';
    $html .= $prmPluginInfo['desc'] . '</p>';
    $html .= '<table border="0" align="center" cellspacing="10">';
    $html .= '  <tr>';
    $html .= '    <td>Version: ' . $prmPluginInfo['version'] . '</td>';
    $html .= '    <td>Date: ' . $prmPluginInfo['date'] . '</td>';
    $html .= '  </tr>';
    $html .= '  <tr>';
    $html .= '    <td>Author: ' . $prmPluginInfo['author'] . '</td>';
    $html .= '    <td>ID: ' . $prmPluginInfo['id'] . '</td>';
    $html .= '  </tr>';
    $html .= '  <tr>';
    $html .= '  	<td colspan="2" align="center"><a href="' . $prmPluginInfo['url'] . '">Web Link</a></td>';
    $html .= '</table>';
    return $html;
} # end function

# returns a radio button with plugin status checked
function _pluginConfigureButton($pluginID) {
    # Read the current status of the plugin ('on'/'off') from conf
   $pluginStatus = _readPluginStatus($pluginID);

    # New or unknown plugins return null. Add special handling here:
    if ( $pluginStatus === null ) {
        $pluginStatus = 'off';
    }

    $html .= '<p align="center">';
    $html .= '<input type="radio" name="pluginStatus" value="on"' . ( ($pluginStatus == 'on') ? ' checked ' : '' ) . '> Enabled';
    $html .= '<input type="radio" name="pluginStatus" value="off"' . ( ($pluginStatus == 'off') ? ' checked ' : '' ) . '> Disabled<br>';
    $html .= '</p>';
    return $html;
} # end function

function _getConfigSetting($key) {
    $filename = '/conf/config';
    $setting = '';
    if (file_exists($filename)) {
        $config = file_get_contents($filename);

        if(preg_match('/' . $key . '=\'(.+)\'/', $config, $config_setting)) {
            $setting = $config_setting[1];
        }
    }
    return  isset($setting) ? $setting : "";
} # end function

function _writeConfigSetting($key, $value) {
    system("sudo config_tool -c " . $key . "='" . $value . "' >/dev/null");
} # end function

function _writeConfigSettingSafe($key, $value) {
    # maybe htmlentities(urlencode($value)) ?
} # end function

function _readConfigSettingSafe($key) {
    # return reverse of _writeConfigSettingSafe
} # end function

function _writeConfigSettingSerialized($key, $valArray) {
    # serialize $valArray then _writeConfigSettingSafe
} # end function

function _readConfigSettingSerialized($key) {
    # reverse of _writeConfigSettingSerialized
} # end function

function _createPluginRootItems() {
    $pluginStatusAll = _readPluginStatusAll();
    if ( is_array($pluginStatusAll) ) {
        $pluginRootItems = array();
        foreach ( $pluginStatusAll as $pluginId => $pluginStatus) {
            $pluginConfigFile = '/tmp/umsp-plugins/' . $pluginId . '/config.php';
            if ( ($pluginStatus =='on') && (is_file($pluginConfigFile)) ) {
                unset($pluginInfo);
                define('_DONT_RUN_CONFIG_',1);
                include($pluginConfigFile);
                if ( is_array($pluginInfo) ) {
                    $pluginRootItems[] = array(	
                        'id'			=> 'umsp://plugins/' . $pluginInfo['id'],
                        'parentID'		=> '0',
                        'dc:title'		=> $pluginInfo['name'],
                        'desc'			=> $pluginInfo['desc'],
                        'upnp:class'	=> 'object.container',
                        'upnp:album_art'=> '',
                    );
                }
            }
        }
    }
    return $pluginRootItems;
}


function _readPluginStatus($prmPluginId) {
    # TODO: replace with config_tool?
    $status = _readPluginVar($prmPluginId);
    return $status;
} # end function

function _readPluginStatusAll() {
    # TODO: replace with config_tool?
    $pluginStatusAll = _readPluginVarAll();
    if ( is_array($pluginStatusAll) ) {
        return $pluginStatusAll;
    } else {
        return null;
    }
} # end function

function _writePluginStatus($prmPluginId, $prmStatus) {
    # TODO: replace with config_tool?
    # status should be 'on' or 'off'
    $result = _writePluginVar($prmPluginId, $prmStatus);
    return $result;
} # end function

#######################################################################
# TODO: replace with config_tool?
# TODO: remove?

function _readPluginVar($prmVarName) {
        $filename = '/conf/umsp-plugins-status';
        if (!is_readable($filename)) { return null; }
        $handle = fopen($filename, 'r');
        $rawContent = fread($handle, 8192);
        fclose($handle);
        $varData = unserialize($rawContent);
        if (is_array($varData) && isset($varData[$prmVarName])) {
                return $varData[$prmVarName];
        } else {
                return null;
        } # end if
} # end function

function _readPluginVarAll() {
        $filename = '/conf/umsp-plugins-status';
        if (!is_readable($filename)) { return null; }
        $handle = fopen($filename, 'r');
        $rawContent = fread($handle, 8192);
        fclose($handle);
        $varData = unserialize($rawContent);
        if ( is_array($varData) ) {
                return $varData;
        } else {
                return null;
        } # end if
} # end function

function _writePluginVar($prmVarName, $prmVarValue) {
    $filename = '/conf/umsp-plugins-status';
    if (is_readable($filename)) {
        $handle = fopen($filename, 'r');
        $rawContent = fread($handle, 8192);
        fclose($handle);
        $prevData = unserialize($rawContent);
        if ($prevData === false || !is_array($prevData)) {
            $newData = array();
        } else {
            $newData = $prevData;
        }
    } else {
        $newData = array();
    }
    $newData[$prmVarName] = $prmVarValue;
    $filenameTmp = '/tmp/umsp-plugins-status';
    $handle = fopen($filenameTmp, 'w');
    $rawContent = serialize($newData);
    $success = fwrite($handle, $rawContent);
    fclose($handle);
    # TODO: Better solution?
    exec('sudo cp ' . $filenameTmp . ' ' . $filename );
    return $success;
} # end function

?>
