<?php
#
# Copyright 2003 James Seng. See LICENSE for the Software License.
#

$config_outline = 0; 
$config_key = "";

function config_element_start($parser,$name,$attribs) {
    global $config_outline, $config_key, $config;

    if ($name == "OUTLINE") {
        $config_outline++;

        if ($config_outline == 1) {
            if (isset($attribs['TEXT']))
                $config_key = $attribs['TEXT']; 
            else
                $config_key = "";
        } else if ($config_outline == 2) {
            if (isset($attribs['TEXT']))
                $config->set($config_key,$attribs['TEXT']); 
        }
    }
}

function config_element_end($parser,$name) {
    global $config_outline, $config_key, $config;

    if ($name == "OUTLINE") {
        $config_outline--;
    }
}

class pong_config extends opml_base {
    var $conf;

    function set($c,$v) {
        if (!preg_match("/^[a-zA-Z0-9]+$/",$c))
            return $this->error("Invalid config ".$c);
        $this->conf[$c] = $v;
        return $v;
    }

    function get($c,$v = NULL) {
        return isset($this->conf[$c]) ? $this->conf[$c] : $v;
    }

    function load($file = "config") {
        $filename = $this->filename($file);

        if (!file_exists($filename)) {
            // No config file so lets init some base stuff
            return 0;
        }

        if (!($fh = fopen($filename,"r")))
            return $this->error("Unable to open $filename for reading.");

        $xml_parser = xml_parser_create();
        xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING,1);
        xml_set_element_handler($xml_parser,
            "config_element_start", "config_element_end");

        while ($data = fread($fh, 4096)) {
            if (!xml_parse($xml_parser,$data,feof($fh))) 
                return $this->error(sprintf(
                    "%s: XML parse error %s at line %d.",
                        $filename,
                        xml_error_string(xml_get_error_code($xml_parser)),
                        xml_get_current_line_number($xml_parser)));
        }

        xml_parser_free($xml_parser);

        return 1;
    }

    function save($file = "config") {
        ignore_user_abort(true);

        $filename = $this->tempfile($file);

        if (!($fh = fopen($filename, 'w')))
            return $this->error("Unable to open $filename for writing.");

        if (flock($fh,LOCK_EX | LOCK_NB)) {
            fwrite($fh,$this->opml_header("Pong Configuration"));
            while (list($k,$v) = each($this->conf)) {
                fwrite($fh,$this->opml_outline_kv($k,$v));
            }
            fwrite($fh,$this->opml_footer());
            flock($fh,LOCK_UN);
        }
        fclose($fh);

        copy($filename,$this->filename($file));
        unlink($filename);

        ignore_user_abort(false);

        return 1;
    }

    function get_config_array() {
        global $module_tree;

        $array_conf = array("ownername" => "Owner Name (use in opml files)",
                            "owneremail" => "Owner Email (use in opml files)");
        if (isset($module_tree) && isset($module_tree->submod))
            foreach ($module_tree->submod as $att => $m) {
                $ac = $module_tree->submod[$att]->module_invoke("","config");
                if ($ac) $array_conf = array_merge($array_conf, $ac);
            }

        return $array_conf;
    }

    function show_form() {
        $userid = $this->get('userid');

        print <<<_FORM_BEGIN_
<center>
<form method="POST" action="?q=update_passwd">
<table class="table" cellpadding="5" width="50%">
<tr>
  <td class="title" colspan=2 align=center>
    Change Userid/Password</td>
  </tr>
<tr>
  <td align="right" width="45%">
    Username</td>
  <td align="left" width="55%">
    <input name="_userid" value="$userid"></td>
  </tr>
<tr>
  <td align="right" width="45%">
    New Password</td>
  <td align="left" width="55%">
    <input type="password" name="_passwd"></td>
  </tr>
<tr>
  <td align="right" width="45%">
    Password Again</td>
  <td align="left" width="55%">
    <input type="password" name="_passwd2"></td>
  </tr>
<tr>
  <td colspan="2" align="center">
    <input class="button" type="submit" name="submit" value="change"></td>
  </tr>
</table>
</form>
<p>
<form method="POST" action="?q=update_config">
<table class="table" cellpadding="5" width="50%">
<tr>
  <td class="title" colspan=2 align=center>
    PonG Configuration</td>
  </tr>
_FORM_BEGIN_;

        $array_conf = $this->get_config_array();
        foreach ($array_conf as $k => $v) {
            $c = $this->get($k);
            print <<< _ARRAY_
<tr>
  <td align="right" width="45%">
    $v</td>
  <td align="left" width="55%">
    <input name="$k" value="$c"></td>
  </tr>

_ARRAY_;
        }

        print <<<_FORM_END_
<tr>
  <td colspan="2" align="center">
    <input class="button" type="submit" name="submit" value="update"></td>
  </tr>
</table>
</form>
</center>
_FORM_END_;
    }
}

function cmd_show_config() {
    global $config;
    $config->show_form();
}

function cmd_update_passwd() {
    global $config;

    if (!isset($_REQUEST['submit'])) {
        $config->show_form();
        return;
    }

    $userid = $_REQUEST['_userid'];
    $passwd = $_REQUEST['_passwd'];
    $passwd2 = $_REQUEST['_passwd2'];

    if ($userid == '' || $passwd == '' || $passwd2 == '') {
        showmsg("error","Empty userid or password not permitted.");
        $config->show_form();
        return;
    }

    if ($passwd != $passwd2) {
        showmsg("error","Password don't match.");
        $config->show_form();
        return;
    }

    $config->set('userid',$userid);
    $config->set('passwd',crypt($passwd));

    showmsg("ok","Userid and Password updated.");
    $config->show_form();
}

function cmd_update_config() {
    global $config;

    $array_conf = $config->get_config_array();
    foreach ($array_conf as $k => $v) {
        $config->set($k,$_REQUEST[$k]);
    }

    showmsg("ok","Configuration updated!");
    $config->show_form();
}

?>
