<?php

/**
 * @package mod
 * @subpackage adobeconnect
 * @author Akinsaya Delamarre (adelamarre@remote-learner.net)
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */


require_once('connect_class.php');

class connect_class_dom extends connect_class {

    public function __construct($serverurl = '', $serverport = '',
                                $username = '', $password = '',
                                $cookie = '', $https) {
        parent::__construct($serverurl, $serverport, $username, $password, $cookie, $https);

    }

    public function create_request($params = array(), $sentrequest = true) {
        if (empty($params)) {
            return false;
        }


        $dom = new DOMDocument('1.0', 'UTF-8');

        $root = $dom->createElement('params');
        $dom->appendChild($root);


        foreach($params as $key => $data) {
	    
	    // DFNVC
	    // htmlentities() durch htmlspecialchars() ersetzt,
	    // damit auch Nutzer agelegt werden können, deren Namen
	    // Umlaute enthalten.
            //
	    $datahtmlent = htmlspecialchars($data);

            $child = $dom->createElement('param', $datahtmlent);
            $root->appendChild($child);

            $attribute = $dom->createAttribute('name');
            $child->appendChild($attribute);

            $text = $dom->createTextNode($key);
            $attribute->appendChild($text);

        }

        $this->_xmlrequest = $dom->saveXML();

        if ($sentrequest) {
            $this->_xmlresponse = $this->send_request();
        }
    }

    /**
     * Parses through xml and looks for the 'cookie' parameter
     * @param string $xml the xml to parse through
     * @return string $sessoin returns the session id
     */
    public function read_cookie_xml($xml = '') {
        global $USER, $COURSE, $CFG;

        if (empty($xml)) {
            if (is_siteadmin($USER->id)) {
                notice(get_string('adminemptyxml', 'adobeconnect'),
                       $CFG->wwwroot . '/admin/settings.php?section=modsettingadobeconnect');
            } else {
                notice(get_string('emptyxml', 'adobeconnect'),
                       '', $COURSE);
            }
        }

        $dom = new DomDocument();
        $dom->loadXML($xml);
        $domnodelist = $dom->getElementsByTagName('cookie');

        if (isset($domnodelist->item(0)->nodeValue)) {
            $this->_cookie = $domnodelist->item(0)->nodeValue;
        } else {
            $this->_cookie = null;
        }

    }

    public function call_success() {
        global $USER, $COURSE, $CFG;

        if (empty($this->_xmlresponse)) {
            if (is_siteadmin($USER->id)) {
                notice(get_string('adminemptyxml', 'adobeconnect'),
                       $CFG->wwwroot . '/admin/settings.php?section=modsettingadobeconnect');
            } else {
                notice(get_string('emptyxml', 'adobeconnect'),
                       '', $COURSE);
            }
        }

        $dom = new DomDocument();
        $dom->loadXML($this->_xmlresponse);

        $domnodelist = $dom->getElementsByTagName('status');

        if ($domnodelist->item(0)->hasAttributes()) {

            $domnode = $domnodelist->item(0)->attributes->getNamedItem('code');

            if (!is_null($domnode)) {
                if (0 == strcmp('ok', $domnode->nodeValue)) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }

        } else {
            return false;
        }

    }

    private function create_http_head_login_xml() {
        $params = array('action' => 'login',
                        'external-auth' => 'use',
                        );

        $this->create_request($params, false);
    }

    public function user_session_cookie_old($username = '') {
        global $CFG;
        $header = array();
        $params = array('action' => 'lms-user-login',
                        'login' => $username,
                        );
        $this->create_request($params, true);
	$data = $this->_xmlresponse;
	$sessionval = false;
        $sessionstart = strpos($data, 'BREEZESESSION=');
        if (false !== $sessionstart) {
            $sessionend = strpos($data, ';');
            $sessionlength = strlen('BREEZESESSION=');
            $sessionvallength = $sessionend - ($sessionstart + $sessionlength);
            $sessionval = substr($data, $sessionstart+$sessionlength, $sessionvallength);
	    return $sessionval;
        }
	else {
	    return false;
	}
    }
    
    public function user_session_cookie($username = '') {
        global $CFG;
        $header = array();
        $params = array('action' => 'lms-user-login',
                        'login' => $username,
                        );
        $this->create_request($params, true);

        if ($this->call_success()) {
	    $xmldoc = new DomDocument();
	    $xmldoc->loadXML($this->_xmlresponse);
	    $xpath = new DomXpath($xmldoc);
	    $cookie = $xpath->query("//cookie")->item(0)->nodeValue;
	    return $cookie;
        }
	else {
	    return false;
	}
    }

}