xref: /dokuwiki/inc/ActionRouter.php (revision 58528803ad1f586696a6719f4ed566b78eb7b1d9)
164ab5140SAndreas Gohr<?php
264ab5140SAndreas Gohr
364ab5140SAndreas Gohrnamespace dokuwiki;
464ab5140SAndreas Gohr
564ab5140SAndreas Gohruse dokuwiki\Action\AbstractAction;
664ab5140SAndreas Gohruse dokuwiki\Action\Exception\ActionDisabledException;
764ab5140SAndreas Gohruse dokuwiki\Action\Exception\ActionException;
864ab5140SAndreas Gohruse dokuwiki\Action\Exception\FatalException;
964ab5140SAndreas Gohruse dokuwiki\Action\Exception\NoActionException;
10a3f6fae6SAndreas Gohruse dokuwiki\Action\Plugin;
1164ab5140SAndreas Gohr
12a3f6fae6SAndreas Gohr/**
13a3f6fae6SAndreas Gohr * Class ActionRouter
14a3f6fae6SAndreas Gohr * @package dokuwiki
15a3f6fae6SAndreas Gohr */
1664ab5140SAndreas Gohrclass ActionRouter {
1764ab5140SAndreas Gohr
1864ab5140SAndreas Gohr    /** @var  AbstractAction */
1964ab5140SAndreas Gohr    protected $action;
2064ab5140SAndreas Gohr
2164ab5140SAndreas Gohr    /** @var  ActionRouter */
22ae7bcdc7SAndreas Gohr    protected static $instance;
2364ab5140SAndreas Gohr
2450701b66SAndreas Gohr    /** @var int transition counter */
2550701b66SAndreas Gohr    protected $transitions = 0;
2650701b66SAndreas Gohr
2750701b66SAndreas Gohr    /** maximum loop */
2850701b66SAndreas Gohr    const MAX_TRANSITIONS = 5;
2950701b66SAndreas Gohr
30480336a3SAndreas Gohr    /** @var string[] the actions disabled in the configuration */
31480336a3SAndreas Gohr    protected $disabled;
32480336a3SAndreas Gohr
3364ab5140SAndreas Gohr    /**
3464ab5140SAndreas Gohr     * ActionRouter constructor. Singleton, thus protected!
35a3f6fae6SAndreas Gohr     *
36a3f6fae6SAndreas Gohr     * Sets up the correct action based on the $ACT global. Writes back
37a3f6fae6SAndreas Gohr     * the selected action to $ACT
3864ab5140SAndreas Gohr     */
3964ab5140SAndreas Gohr    protected function __construct() {
40a3f6fae6SAndreas Gohr        global $ACT;
41480336a3SAndreas Gohr        global $conf;
42480336a3SAndreas Gohr
43480336a3SAndreas Gohr        $this->disabled = explode(',', $conf['disableactions']);
44480336a3SAndreas Gohr        $this->disabled = array_map('trim', $this->disabled);
45480336a3SAndreas Gohr        $this->transitions = 0;
46480336a3SAndreas Gohr
47a3f6fae6SAndreas Gohr        $ACT = act_clean($ACT);
48a3f6fae6SAndreas Gohr        $this->setupAction($ACT);
49a3f6fae6SAndreas Gohr        $ACT = $this->action->getActionName();
5064ab5140SAndreas Gohr    }
5164ab5140SAndreas Gohr
5264ab5140SAndreas Gohr    /**
5364ab5140SAndreas Gohr     * Get the singleton instance
5464ab5140SAndreas Gohr     *
5564ab5140SAndreas Gohr     * @param bool $reinit
5664ab5140SAndreas Gohr     * @return ActionRouter
5764ab5140SAndreas Gohr     */
58ae7bcdc7SAndreas Gohr    public static function getInstance($reinit = false) {
59ae7bcdc7SAndreas Gohr        if((self::$instance === null) || $reinit) {
60ae7bcdc7SAndreas Gohr            self::$instance = new ActionRouter();
6164ab5140SAndreas Gohr        }
62ae7bcdc7SAndreas Gohr        return self::$instance;
6364ab5140SAndreas Gohr    }
6464ab5140SAndreas Gohr
6564ab5140SAndreas Gohr    /**
6664ab5140SAndreas Gohr     * Setup the given action
6764ab5140SAndreas Gohr     *
6864ab5140SAndreas Gohr     * Instantiates the right class, runs permission checks and pre-processing and
69a3f6fae6SAndreas Gohr     * sets $action
7064ab5140SAndreas Gohr     *
7164ab5140SAndreas Gohr     * @param string $actionname
72a3f6fae6SAndreas Gohr     * @triggers ACTION_ACT_PREPROCESS
7364ab5140SAndreas Gohr     */
7464ab5140SAndreas Gohr    protected function setupAction($actionname) {
75a3f6fae6SAndreas Gohr        $presetup = $actionname;
76a3f6fae6SAndreas Gohr
7764ab5140SAndreas Gohr        try {
7864ab5140SAndreas Gohr            $this->action = $this->loadAction($actionname);
79480336a3SAndreas Gohr            $this->checkAction($this->action);
8064ab5140SAndreas Gohr            $this->action->preProcess();
8164ab5140SAndreas Gohr
8264ab5140SAndreas Gohr        } catch(ActionException $e) {
8364ab5140SAndreas Gohr            // we should have gotten a new action
84a3f6fae6SAndreas Gohr            $actionname = $e->getNewAction();
8564ab5140SAndreas Gohr
8664ab5140SAndreas Gohr            // this one should trigger a user message
8764ab5140SAndreas Gohr            if(is_a($e, ActionDisabledException::class)) {
88a3f6fae6SAndreas Gohr                msg('Action disabled: ' . hsc($presetup), -1);
8964ab5140SAndreas Gohr            }
9064ab5140SAndreas Gohr
9181f9e22bSAndreas Gohr            // some actions may request the display of a message
9281f9e22bSAndreas Gohr            if($e->displayToUser()) {
9381f9e22bSAndreas Gohr                msg(hsc($e->getMessage()), -1);
9481f9e22bSAndreas Gohr            }
9581f9e22bSAndreas Gohr
9664ab5140SAndreas Gohr            // do setup for new action
9750701b66SAndreas Gohr            $this->transitionAction($presetup, $actionname);
9864ab5140SAndreas Gohr
9964ab5140SAndreas Gohr        } catch(NoActionException $e) {
100a3f6fae6SAndreas Gohr            // give plugins an opportunity to process the actionname
101a3f6fae6SAndreas Gohr            $evt = new \Doku_Event('ACTION_ACT_PREPROCESS', $actionname);
102a3f6fae6SAndreas Gohr            if($evt->advise_before()) {
103a3f6fae6SAndreas Gohr                if($actionname == $presetup) {
104a3f6fae6SAndreas Gohr                    // no plugin changed the action, complain and switch to show
105a3f6fae6SAndreas Gohr                    msg('Action unknown: ' . hsc($actionname), -1);
106a3f6fae6SAndreas Gohr                    $actionname = 'show';
107a3f6fae6SAndreas Gohr                }
10850701b66SAndreas Gohr                $this->transitionAction($presetup, $actionname);
109a3f6fae6SAndreas Gohr            } else {
110a3f6fae6SAndreas Gohr                // event said the action should be kept, assume action plugin will handle it later
11173522543SAndreas Gohr                $this->action = new Plugin($actionname);
112a3f6fae6SAndreas Gohr            }
113a3f6fae6SAndreas Gohr            $evt->advise_after();
11464ab5140SAndreas Gohr
11564ab5140SAndreas Gohr        } catch(\Exception $e) {
11664ab5140SAndreas Gohr            $this->handleFatalException($e);
11764ab5140SAndreas Gohr        }
11864ab5140SAndreas Gohr    }
11964ab5140SAndreas Gohr
12064ab5140SAndreas Gohr    /**
12150701b66SAndreas Gohr     * Transitions from one action to another
12250701b66SAndreas Gohr     *
123*58528803SAndreas Gohr     * Basically just calls setupAction() again but does some checks before.
12450701b66SAndreas Gohr     *
12550701b66SAndreas Gohr     * @param string $from current action name
12650701b66SAndreas Gohr     * @param string $to new action name
12750701b66SAndreas Gohr     * @param null|ActionException $e any previous exception that caused the transition
12850701b66SAndreas Gohr     */
12950701b66SAndreas Gohr    protected function transitionAction($from, $to, $e = null) {
13050701b66SAndreas Gohr        $this->transitions++;
13150701b66SAndreas Gohr
13250701b66SAndreas Gohr        // no infinite recursion
13350701b66SAndreas Gohr        if($from == $to) {
13450701b66SAndreas Gohr            $this->handleFatalException(new FatalException('Infinite loop in actions', 500, $e));
13550701b66SAndreas Gohr        }
13650701b66SAndreas Gohr
13750701b66SAndreas Gohr        // larger loops will be caught here
13850701b66SAndreas Gohr        if($this->transitions >= self::MAX_TRANSITIONS) {
13950701b66SAndreas Gohr            $this->handleFatalException(new FatalException('Maximum action transitions reached', 500, $e));
14050701b66SAndreas Gohr        }
14150701b66SAndreas Gohr
14250701b66SAndreas Gohr        // do the recursion
14350701b66SAndreas Gohr        $this->setupAction($to);
14450701b66SAndreas Gohr    }
14550701b66SAndreas Gohr
14650701b66SAndreas Gohr    /**
14764ab5140SAndreas Gohr     * Aborts all processing with a message
14864ab5140SAndreas Gohr     *
14964ab5140SAndreas Gohr     * When a FataException instanc is passed, the code is treated as Status code
15064ab5140SAndreas Gohr     *
15164ab5140SAndreas Gohr     * @param \Exception|FatalException $e
15264ab5140SAndreas Gohr     */
15364ab5140SAndreas Gohr    protected function handleFatalException(\Exception $e) {
15464ab5140SAndreas Gohr        if(is_a($e, FatalException::class)) {
15564ab5140SAndreas Gohr            http_status($e->getCode());
15664ab5140SAndreas Gohr        } else {
15764ab5140SAndreas Gohr            http_status(500);
15864ab5140SAndreas Gohr        }
15964ab5140SAndreas Gohr        $msg = 'Something unforseen has happened: ' . $e->getMessage();
16064ab5140SAndreas Gohr        nice_die(hsc($msg));
16164ab5140SAndreas Gohr    }
16264ab5140SAndreas Gohr
16364ab5140SAndreas Gohr    /**
16464ab5140SAndreas Gohr     * Load the given action
16564ab5140SAndreas Gohr     *
16673522543SAndreas Gohr     * This translates the given name to a class name by uppercasing the first letter.
16773522543SAndreas Gohr     * Underscores translate to camelcase names. For actions with underscores, the different
16873522543SAndreas Gohr     * parts are removed beginning from the end until a matching class is found. The instatiated
16973522543SAndreas Gohr     * Action will always have the full original action set as Name
17073522543SAndreas Gohr     *
17173522543SAndreas Gohr     * Example: 'export_raw' -> ExportRaw then 'export' -> 'Export'
17273522543SAndreas Gohr     *
17364ab5140SAndreas Gohr     * @param $actionname
17464ab5140SAndreas Gohr     * @return AbstractAction
17564ab5140SAndreas Gohr     * @throws NoActionException
17664ab5140SAndreas Gohr     */
177480336a3SAndreas Gohr    public function loadAction($actionname) {
17873522543SAndreas Gohr        $actionname = strtolower($actionname); // FIXME is this needed here? should we run a cleanup somewhere else?
17973522543SAndreas Gohr        $parts = explode('_', $actionname);
18073522543SAndreas Gohr        while($parts) {
18173522543SAndreas Gohr            $load = join('_', $parts);
18273522543SAndreas Gohr            $class = 'dokuwiki\\Action\\' . str_replace('_', '', ucwords($load, '_'));
18364ab5140SAndreas Gohr            if(class_exists($class)) {
18473522543SAndreas Gohr                return new $class($actionname);
18564ab5140SAndreas Gohr            }
18673522543SAndreas Gohr            array_pop($parts);
18773522543SAndreas Gohr        }
18873522543SAndreas Gohr
18964ab5140SAndreas Gohr        throw new NoActionException();
19064ab5140SAndreas Gohr    }
19164ab5140SAndreas Gohr
19264ab5140SAndreas Gohr    /**
193480336a3SAndreas Gohr     * Execute all the checks to see if this action can be executed
194480336a3SAndreas Gohr     *
195480336a3SAndreas Gohr     * @param AbstractAction $action
196480336a3SAndreas Gohr     * @throws ActionDisabledException
197480336a3SAndreas Gohr     * @throws ActionException
198480336a3SAndreas Gohr     */
199480336a3SAndreas Gohr    public function checkAction(AbstractAction $action) {
200480336a3SAndreas Gohr        global $INFO;
201480336a3SAndreas Gohr        global $ID;
202480336a3SAndreas Gohr
203480336a3SAndreas Gohr        if(in_array($action->getActionName(), $this->disabled)) {
204480336a3SAndreas Gohr            throw new ActionDisabledException();
205480336a3SAndreas Gohr        }
206480336a3SAndreas Gohr
207480336a3SAndreas Gohr        $action->checkPermissions();
208480336a3SAndreas Gohr
209480336a3SAndreas Gohr        if(isset($INFO)) {
210480336a3SAndreas Gohr            $perm = $INFO['perm'];
211480336a3SAndreas Gohr        } else {
212480336a3SAndreas Gohr            $perm = auth_quickaclcheck($ID);
213480336a3SAndreas Gohr        }
214480336a3SAndreas Gohr
215480336a3SAndreas Gohr        if($perm < $action->minimumPermission()) {
216480336a3SAndreas Gohr            throw new ActionException('denied');
217480336a3SAndreas Gohr        }
218480336a3SAndreas Gohr    }
219480336a3SAndreas Gohr
220480336a3SAndreas Gohr    /**
22164ab5140SAndreas Gohr     * Returns the action handling the current request
22264ab5140SAndreas Gohr     *
22364ab5140SAndreas Gohr     * @return AbstractAction
22464ab5140SAndreas Gohr     */
22564ab5140SAndreas Gohr    public function getAction() {
22664ab5140SAndreas Gohr        return $this->action;
22764ab5140SAndreas Gohr    }
22864ab5140SAndreas Gohr}
229