<?php
/**
 * -----------------------------------------------------------------------
 * vBSSO is a solution which helps you connect to different software platforms
 * via secure Single Sign-On.
 *
 * Copyright (c) 2011-2017 vBSSO. All Rights Reserved.
 * This software is the proprietary information of vBSSO.
 *
 * Author URI: http://www.vbsso.com
 * License: GPL version 2 or later -
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * -----------------------------------------------------------------------
 */

// must be run within Dokuwiki
if (!defined('DOKU_INC')) {
    die();
}

if (!defined('DIR')) {
    define('DIR', dirname(__FILE__) . '/');
}
if (!defined('VBSSO_PLATFORM_CONFIG_FILE')) {
    define('VBSSO_PLATFORM_CONFIG_FILE', DIR . 'platform.conf');
}
if (!defined('VBSSO_VBSSO_CONFIG_FILE')) {
    define('VBSSO_VBSSO_CONFIG_FILE', DIR . 'vbsso.conf');
}
require_once(DIR . 'vendor/com.extremeidea.vbsso/vbsso-connect-shared/vbsso_shared.php');
require_once(DIR . 'includes/api.php');

if (file_exists(DIR . 'config.custom.php')) {
    include_once(DIR . 'config.custom.php');
}

global $vbsso_platform_settings, $vbsso_settings;
$vbsso_platform_settings = ($vbsso_platform_settings) ? $vbsso_platform_settings : vbsso_get_platform_settings();
$vbsso_settings = ($vbsso_settings) ? $vbsso_settings : vbsso_get_dokuwiki_settings();

/**
 * Class action_plugin_vbsso.
 *
 * @codingStandardsIgnoreStart
 */
class action_plugin_vbsso extends DokuWiki_Action_Plugin {

    /**
     * @codingStandardsIgnoreEnd
     */
    private $vbsso_platform_settings;
    private $vbsso_settings;

    /**
     * Action_plugin_vbsso constructor.
     *
     * @return mixed
     */
    function __construct() {
        $this->vbsso_platform_settings = vbsso_get_platform_settings();
        $this->vbsso_settings = vbsso_get_dokuwiki_settings();
    }

    /**
     * Register
     *
     * @param object $controller controller
     *
     * @return void
     */
    function register(Doku_Event_Handler $controller) {
        if (vbsso_get_platform_config_property(SHAREDAPI_PLATFORM_DOKUWIKI, VBSSO_CONFIG_PROPERTY_OVERRIDE_LINKS,
            TRUE)) {
            if (!empty($this->vbsso_platform_settings[VBSSO_NAMED_EVENT_FIELD_LOGIN_URL])) {
                $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'vbsso_redirect_page_hook');
            }

            if (!empty($this->vbsso_platform_settings[VBSSO_NAMED_EVENT_FIELD_LOGIN_VBULLETIN_URL])
                and !$this->vbsso_settings[VBSSO_NAMED_EVENT_FIELD_LOGIN_THROUGH_VB_PAGE]) {
                $controller->register_hook('HTML_LOGINFORM_OUTPUT', 'BEFORE', $this, 'vbsso_loginform_page_hook');
            }
        }

        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'vbsso_add_header');

        $extensions = vbsso_verify_loaded_extensions();
        if (count($extensions)) {
            $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'vbsso_show_warnings', $extensions);
        }
    }

    /**
     * Redirect page hook
     *
     * @param object $event event
     * @param mixed  $param param
     *
     * @return void
     *
     * @codingStandardsIgnoreStart
     */
    function vbsso_redirect_page_hook(&$event, $param) {
        /**
         * @codingStandardsIgnoreEnd
         */
        global $INFO;

        switch ($event->data) {
            case 'register':
                $url = $this->vbsso_platform_settings[VBSSO_NAMED_EVENT_FIELD_REGISTER_URL];
                sharedapi_url_redirect(sharedapi_url_add_destination($url));
                break;
            case 'logout':
                $url = $this->vbsso_platform_settings[VBSSO_NAMED_EVENT_FIELD_LOGOUT_URL];
                sharedapi_url_redirect(sharedapi_url_add_destination($url));
                exit;
            case 'login':
                if ($this->vbsso_settings[VBSSO_NAMED_EVENT_FIELD_LOGIN_THROUGH_VB_PAGE]) {
                    $url = $this->vbsso_platform_settings[VBSSO_NAMED_EVENT_FIELD_LOGIN_URL];
                    sharedapi_url_redirect(sharedapi_url_add_destination($url));
                }
                break;
            case 'profile':
                if (isset($INFO['userinfo']) and !in_array('admin', $INFO['userinfo']['grps'])
                    and $this->vbsso_settings[VBSSO_NAMED_EVENT_FIELD_SHOW_VBULLETIN_PROFILE]) {
                    sharedapi_url_redirect($this->vbsso_platform_settings[VBSSO_NAMED_EVENT_FIELD_PROFILE_URL]);
                }
                break;
        }
    }

    /**
     * Login form page hook
     *
     * @param object $event event
     * @param mixed  $param param
     *
     * @return void
     *
     * @codingStandardsIgnoreStart
     */
    function vbsso_loginform_page_hook(&$event, $param) {

        /**
         * @codingStandardsIgnoreEnd
         */

        global $INPUT;

        $form = new Doku_Form(array('id' => 'dw__login',
            'action' => $this->vbsso_platform_settings[VBSSO_NAMED_EVENT_FIELD_LOGIN_VBULLETIN_URL]), TRUE, 'post');
        $form->startFieldset($this->getLang('btn_login'));
        $form->addHidden('do', 'login');
        $form->addElement(form_makeTextField('vb_login_username',
            ((!$INPUT->bool('http_credentials')) ? $INPUT->str('u') : ''), $this->getLang('user'), 'focus__this',
            'block'));
        $form->addElement(form_makePasswordField('vb_login_password', $this->getLang('pass'), '', 'block'));
        $form->addElement(form_makeCheckboxField('cookieuser', '1', $this->getLang('remember'), 'remember__me',
            'simple'));
        $form->addElement(form_makeButton('submit', '', $this->getLang('btn_login')));
        $form->endFieldset();

        if (actionOK('register')) {
            $url = $this->vbsso_platform_settings[VBSSO_NAMED_EVENT_FIELD_REGISTER_URL];
            $form->addElement('<p>' . $this->getLang('reghere') . ': ' . tpl_link(sharedapi_url_add_destination($url),
                    $this->getLang('btn_register'), '', TRUE) . '</p>');
        }

        if (actionOK('resendpwd')) {
            $url = $this->vbsso_platform_settings[VBSSO_NAMED_EVENT_FIELD_LOSTPASSWORD_URL];
            $form->addElement('<p>' . $this->getLang('pwdforget') . ': ' . tpl_link(sharedapi_url_add_destination($url),
                    $this->getLang('btn_resendpwd'), '', TRUE) . '</p>');
        }

        $event->data = $form;
    }

    /**
     * Add header
     *
     * @param object $event event
     * @param mixed  $param param
     *
     * @return void
     *
     * @codingStandardsIgnoreStart
     */
    function vbsso_add_header(&$event, $param) {
        /**
         * @codingStandardsIgnoreEnd
         */

        $event->data["script"][] = array("type" => "text/javascript",
            "_data" => strip_tags(VBSSO_PLATFORM_FOOTER_GA_HTML(sharedapi_get_platforms(SHAREDAPI_PLATFORM_DOKUWIKI))));
        if ($this->vbsso_settings[VBSSO_PLATFORM_FOOTER_LINK_PROPERTY]) {
            $event->data["script"][] = array("type" => "text/javascript", "_data" => "function vbsso_add_js() {
                    var newDiv = document.createElement('div');
                    newDiv.innerHTML = '" . VBSSO_PLATFORM_FOOTER_LINK_HTML . "';
                    document.body.appendChild(newDiv);
                }
                window.onload = vbsso_add_js;
                ");
        }
    }

    /**
     * Show warnings
     *
     * @param object $event event
     * @param mixed  $param param
     *
     * @return void
     *
     * @codingStandardsIgnoreStart
     */
    function vbsso_show_warnings(&$event, $param) {

        /**
         * @codingStandardsIgnoreEnd
         */

        global $INFO;

        if (isset($INFO['userinfo']) and in_array('admin', $INFO['userinfo']['grps'])) {
            $tpl = file_get_contents(__DIR__.'/views/action.tpl');
            $error_message = implode(', ', $param);
            $tpl = str_replace('[[content]]', $error_message, $tpl);
            echo $tpl;
        }
    }
}
