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

$GLOBALS['conf_fprofile'] = "profile";

function profile_element_start($parser,$name,$attribs) {
    global $module_tree;

    if (!isset($GLOBALS['profile_load'])) return 0;
    if ($name == "OUTLINE") {
        $module_tree->module_invoke_sub("attrib_base","element_start",
            $parser,$name,$attribs);
    }
    return 1;
}

function profile_element_end($parser,$name) {
    global $module_tree;

    if (!isset($GLOBALS['profile_load'])) return 0;
    if ($name == "OUTLINE") {
        $module_tree->module_invoke_sub("attrib_base","element_end",$parser);
    }
    return 1;
}

class func_profile extends function_base {
    var $attribs;

    function func_profile() {
        global $conf_fprofile;
        $GLOBALS['profile'] =& $this;
        $this->load($conf_fprofile);
    }

    function print_menu($type) {
        if ($type == 'short') {
            print "<a href=\"?q=show_profile\">Profile</a> | ";
            return;
        }

        if ($type == 'long') {
            print <<<_LONG_
<tr>
  <td align="right" width="25%" valign="top">
    <a href="?q=show_profile">Edit Profile</a></td>
  <td align="left" width="75%" valign="top">
    Define or edit your own portfolio. You may have multiples of the same
    attributes. For example, you may define one personal email address
    and another business email address.
    </td>
  </tr>

_LONG_;
        }
    }

    function &add_attrib($name,$arr,$id = 0) {
        global $modules;

        # no such attribute to set
        if (!isset($modules[$name])) return 0;
        if (get_parent_class($modules[$name]) != "attrib_base") return 0;

        $new_attrib = new $name;
        if ($id == 0) 
            $id = $new_attrib->assignid();
        else
            $new_attrib->setid($id);

        $new_attrib->init_attrib($arr,$id);
        $this->attribs[] =& $new_attrib;

        return $new_attrib;
    }

    function del_attrib($att) {
        if (!isset($this->attribs[$att])) return 0;
        $this->attribs[$att]->deinit_attrib();
        unset($this->attribs[$att]);
    }

    function attribs_invoke($function,
        $a1 = NULL, $a2 = NULL, $a3 = NULL, $a4 = NULL, $a5 = NULL, $a6 = NULL){
        if (!isset($this->attribs)) return 0;
        foreach ($this->attribs as $att => $v) 
            $this->attribs[$att]->module_invoke("attrib_base",$function,
                $a1,$a2,$a3,$a4,$a5,$a6);
    }

    function has_attrib($type) {
        foreach ($this->attribs as $att => $v)
            if ($att == $type) return 1;
        return 0;
    }

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

        $GLOBALS['profile_load'] =& $this;

        if (!file_exists($filename))
            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,
            "profile_element_start", "profile_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);

        unset($GLOBALS['profile_load']);

        return 1;
    }

    function save($file = "profile") {
        global $config;

        ignore_user_abort(true);
    
        if (!isset($config->conf['datecreated'])) {
            $config->set('datecreated',long_datetime());
        }
    
        $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 Profile"));
            $this->attribs_invoke("save",$fh,2);
            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 show_form() {
        global $module_tree;

        print <<<_FORM_HEAD_
<center>
<form method="POST" action="?q=update_profile">
<table class="table" cellpadding="5" width="80%">
<tr>
  <td class="title" colspan="3" align="center">
    PONG Profile</td>
  </tr>
_FORM_HEAD_;

        if (isset($this->attribs)) 
            foreach ($this->attribs as $att => $v) 
                print $this->attribs[$att]->html_form_profile();

        print <<<_FORM_TAIL1_
<tr>
  <td colspan="3" align="center">
    <table width="100%"><tr><td align=left>
_FORM_TAIL1_;

        if (isset($module_tree->submod)) {
            print "<select name=_add>\n";
            foreach ($module_tree->submod as $m => $v) {
                if (get_parent_class($module_tree->submod[$m]) != 'attrib_base')
                    continue;
                $m = htmlspecialchars($m);
                print "<option value=\"$m\">$m\n";
            }
            print "</select>\n";
            print 
              "<input class=button type=submit name=submit value=add> &nbsp; ";
        }

        print <<<_FORM_TAIL2_
      <input class=button type=submit name=submit value=delete> &nbsp; 
    </td>
  <td>
    <input class=button type=submit name=submit value=save></td>
  </tr></table></td></tr>
<tr>
  <td class="help" colspan="3" align="left">
    To add an attribute to your profile, select it from the pulldown menu
    and click "Add". To delete an attribute, check the boxes on the left 
    and click "Delete". After entering your profile, click "Save".
    <p>
    These information are <b>private</b> unless you configure it otherwise.
    To do so, click on the "Friends" on the top menu bar.
    </td>
  </tr>
</table></form></center>
_FORM_TAIL2_;
        return 1;
    }
}

function cmd_show_profile() {
    global $profile;

    return $profile->show_form();
}

function cmd_update_profile() {
    global $profile;

    if ($_REQUEST['submit'] == 'delete') {
        foreach ($profile->attribs as $att => $v) {
            if ($_REQUEST["_".$profile->attribs[$att]->getid()] == "on")
                $profile->del_attrib($att);
        }
        showmsg("ok","Successfully deleted checked attributes.");
        $profile->save();
    } else if ($_REQUEST['submit'] == 'add') {
        if ($profile->add_attrib($_REQUEST['_add'],array())) {
            $profile->save();
        } else
            showmsg("error","Unknown attribute '${_REQUEST['_add']}'.");
    } else if ($_REQUEST['submit'] == 'save') {
        $profile->attribs_invoke("parse_request");
        $profile->save();
        showmsg("ok","Profile saved.");
    } else {
        showmsg("error","Unknown request ${_REQUEST['submit']}");
    }


    $profile->show_form();

    return 1;
}

?>
