<?php  // vim:set et ts=4 sw=4 sts=4:
# Copyright (C) 2003-2014 hipplay Inc.
#
# This is UNPUBLISHED PROPRIETARY SOURCE CODE OF hipplay Inc.
# The copyright notice above does not evidence any actual or intended
# publication of such source code. The contents of this file may not
# be disclosed to third parties, copied or duplicated in any form, in
# whole or in part, without the prior written permission of
# hipplay Inc.
#
namespace HipPlayConnect\REST\services\sessions;

use DOMDocument;
use HipPlayConnect\AxRecoverableErrorException;
use HipPlayConnect\Configuration\AxBranding;
use HipPlayConnect\Identity\AxAuthenticationCode;
use HipPlayConnect\Identity\AxAuthenticationCodeErrorException;
use HipPlayConnect\Identity\AxSession;
use HipPlayConnect\Identity\AxInvalidAuthenticationCodeException;
use HipPlayConnect\REST\AxAbstractRESTResource;
use HipPlayConnect\REST\AxRESTController;
use HipPlayConnect\REST\AxRESTResourceInterface;
use HipPlayConnect\REST\AxRESTResponse;

class AxSessionsRESTResource extends AxAbstractRESTResource implements AxRESTResourceInterface {

    protected $closeSession = FALSE;

    public function POST (array $queryParams = array(), $requestBody) {
        // parse XML input, expecting a "session" root element
        $doc = new DOMDocument();
        if (
                $requestBody == '' ||
                !$doc->loadXML($requestBody) ||
                !($root = $doc->documentElement) ||
                $root->nodeName != 'session'
        )
            throw new AxRecoverableErrorException(NULL, 3);

        // HipServ user login via the CS
        if ($code = $root->getAttribute('code')) {
            try {
                $login = AxAuthenticationCode::getUserName($_GET['AUTH']);
                if ($login != AxSession::VALID_USERNAME)
                    throw new AxRecoverableErrorException('Cannot find user in the local DB', 4);

                AxSession::logUserIn();
                return $this->getSessionXML();
            } catch (AxAuthenticationCodeErrorException $e) {
                throw new AxRecoverableErrorException('Cannot Check Authentication code', 4);
            } catch (AxInvalidAuthenticationCodeException $e) {
                throw new AxRecoverableErrorException('Invalid Authentication Code', 22);
            }
        }

        // invalid parameter
        throw new AxRecoverableErrorException(NULL, 5);
    }

    private function getSessionXML () {
        // return session information (copy/pasted from flashvars)
        $mOutputDocument = new DOMDocument();
        $mSessionNode = $mOutputDocument->createElement('session');

        foreach (
                array(
                        'supportURL'      => 'http://' . AxBranding::get('SupportDomain') . '/support',
                        'userURI'         => AxRESTController::URI_PREFIX . 'accounts/users/' . AxSession::VALID_USERNAME,
                        'serverConfigURI' => AxRESTController::URI_PREFIX . 'server/config',
                        'href'            => AxRESTController::URI_PREFIX . 'sessions/' . session_id(),
                        'locale'          => MC_LOCALE
                )
                as $key => $value
        )
            $mSessionNode->setAttribute($key, $value);
        $mOutputDocument->appendChild($mSessionNode);

        return
                new AxRESTResponse
                (
                        200,
                        $mOutputDocument->saveXML(),
                        'text/xml; charset=UTF-8'
                );
    }

}