xref: /dokuwiki/inc/ActionRouter.php (revision 8c7c53b0321a3cd3116b8d3b2ad27863a38dece7)
164ab5140SAndreas Gohr<?php
264ab5140SAndreas Gohr
364ab5140SAndreas Gohrnamespace dokuwiki;
464ab5140SAndreas Gohr
524870174SAndreas Gohruse dokuwiki\Extension\Event;
664ab5140SAndreas Gohruse dokuwiki\Action\AbstractAction;
764ab5140SAndreas Gohruse dokuwiki\Action\Exception\ActionDisabledException;
864ab5140SAndreas Gohruse dokuwiki\Action\Exception\ActionException;
964ab5140SAndreas Gohruse dokuwiki\Action\Exception\FatalException;
1064ab5140SAndreas Gohruse dokuwiki\Action\Exception\NoActionException;
11a3f6fae6SAndreas Gohruse dokuwiki\Action\Plugin;
1264ab5140SAndreas Gohr
13a3f6fae6SAndreas Gohr/**
14a3f6fae6SAndreas Gohr * Class ActionRouter
15a3f6fae6SAndreas Gohr * @package dokuwiki
16a3f6fae6SAndreas Gohr */
17*8c7c53b0SAndreas Gohrclass ActionRouter
18*8c7c53b0SAndreas Gohr{
1964ab5140SAndreas Gohr
2064ab5140SAndreas Gohr    /** @var  AbstractAction */
2164ab5140SAndreas Gohr    protected $action;
2264ab5140SAndreas Gohr
2364ab5140SAndreas Gohr    /** @var  ActionRouter */
2424870174SAndreas Gohr    protected static $instance;
2564ab5140SAndreas Gohr
2650701b66SAndreas Gohr    /** @var int transition counter */
2750701b66SAndreas Gohr    protected $transitions = 0;
2850701b66SAndreas Gohr
2950701b66SAndreas Gohr    /** maximum loop */
3074981a4eSAndreas Gohr    protected const MAX_TRANSITIONS = 5;
3150701b66SAndreas Gohr
32480336a3SAndreas Gohr    /** @var string[] the actions disabled in the configuration */
33480336a3SAndreas Gohr    protected $disabled;
34480336a3SAndreas Gohr
3564ab5140SAndreas Gohr    /**
3664ab5140SAndreas Gohr     * ActionRouter constructor. Singleton, thus protected!
37a3f6fae6SAndreas Gohr     *
38a3f6fae6SAndreas Gohr     * Sets up the correct action based on the $ACT global. Writes back
39a3f6fae6SAndreas Gohr     * the selected action to $ACT
4064ab5140SAndreas Gohr     */
4164ab5140SAndreas Gohr    protected function __construct() {
42a3f6fae6SAndreas Gohr        global $ACT;
43480336a3SAndreas Gohr        global $conf;
44480336a3SAndreas Gohr
45480336a3SAndreas Gohr        $this->disabled = explode(',', $conf['disableactions']);
46480336a3SAndreas Gohr        $this->disabled = array_map('trim', $this->disabled);
47480336a3SAndreas Gohr
48a3f6fae6SAndreas Gohr        $ACT = act_clean($ACT);
49a3f6fae6SAndreas Gohr        $this->setupAction($ACT);
50a3f6fae6SAndreas Gohr        $ACT = $this->action->getActionName();
5164ab5140SAndreas Gohr    }
5264ab5140SAndreas Gohr
5364ab5140SAndreas Gohr    /**
5464ab5140SAndreas Gohr     * Get the singleton instance
5564ab5140SAndreas Gohr     *
5664ab5140SAndreas Gohr     * @param bool $reinit
5764ab5140SAndreas Gohr     * @return ActionRouter
5864ab5140SAndreas Gohr     */
59ae7bcdc7SAndreas Gohr    public static function getInstance($reinit = false) {
6024870174SAndreas Gohr        if((!self::$instance instanceof \dokuwiki\ActionRouter) || $reinit) {
61ae7bcdc7SAndreas Gohr            self::$instance = new ActionRouter();
6264ab5140SAndreas Gohr        }
63ae7bcdc7SAndreas Gohr        return self::$instance;
6464ab5140SAndreas Gohr    }
6564ab5140SAndreas Gohr
6664ab5140SAndreas Gohr    /**
6764ab5140SAndreas Gohr     * Setup the given action
6864ab5140SAndreas Gohr     *
6964ab5140SAndreas Gohr     * Instantiates the right class, runs permission checks and pre-processing and
70a3f6fae6SAndreas Gohr     * sets $action
7164ab5140SAndreas Gohr     *
72c7d61a4eSAndreas Gohr     * @param string $actionname this is passed as a reference to $ACT, for plugin backward compatibility
73a3f6fae6SAndreas Gohr     * @triggers ACTION_ACT_PREPROCESS
7464ab5140SAndreas Gohr     */
75c7d61a4eSAndreas Gohr    protected function setupAction(&$actionname) {
76a3f6fae6SAndreas Gohr        $presetup = $actionname;
77a3f6fae6SAndreas Gohr
7864ab5140SAndreas Gohr        try {
7968667f4aSMichael Große            // give plugins an opportunity to process the actionname
8024870174SAndreas Gohr            $evt = new Event('ACTION_ACT_PREPROCESS', $actionname);
8168667f4aSMichael Große            if ($evt->advise_before()) {
8264ab5140SAndreas Gohr                $this->action = $this->loadAction($actionname);
83480336a3SAndreas Gohr                $this->checkAction($this->action);
8464ab5140SAndreas Gohr                $this->action->preProcess();
8568667f4aSMichael Große            } else {
8668667f4aSMichael Große                // event said the action should be kept, assume action plugin will handle it later
8768667f4aSMichael Große                $this->action = new Plugin($actionname);
8868667f4aSMichael Große            }
8968667f4aSMichael Große            $evt->advise_after();
9064ab5140SAndreas Gohr
9164ab5140SAndreas Gohr        } catch(ActionException $e) {
9264ab5140SAndreas Gohr            // we should have gotten a new action
93a3f6fae6SAndreas Gohr            $actionname = $e->getNewAction();
9464ab5140SAndreas Gohr
9564ab5140SAndreas Gohr            // this one should trigger a user message
9624870174SAndreas Gohr            if($e instanceof ActionDisabledException) {
97a3f6fae6SAndreas Gohr                msg('Action disabled: ' . hsc($presetup), -1);
9864ab5140SAndreas Gohr            }
9964ab5140SAndreas Gohr
10081f9e22bSAndreas Gohr            // some actions may request the display of a message
10181f9e22bSAndreas Gohr            if($e->displayToUser()) {
10281f9e22bSAndreas Gohr                msg(hsc($e->getMessage()), -1);
10381f9e22bSAndreas Gohr            }
10481f9e22bSAndreas Gohr
10564ab5140SAndreas Gohr            // do setup for new action
10650701b66SAndreas Gohr            $this->transitionAction($presetup, $actionname);
10764ab5140SAndreas Gohr
10864ab5140SAndreas Gohr        } catch(NoActionException $e) {
109a3f6fae6SAndreas Gohr            msg('Action unknown: ' . hsc($actionname), -1);
110a3f6fae6SAndreas Gohr            $actionname = 'show';
11150701b66SAndreas Gohr            $this->transitionAction($presetup, $actionname);
11264ab5140SAndreas Gohr        } catch(\Exception $e) {
11364ab5140SAndreas Gohr            $this->handleFatalException($e);
11464ab5140SAndreas Gohr        }
11564ab5140SAndreas Gohr    }
11664ab5140SAndreas Gohr
11764ab5140SAndreas Gohr    /**
11850701b66SAndreas Gohr     * Transitions from one action to another
11950701b66SAndreas Gohr     *
12058528803SAndreas Gohr     * Basically just calls setupAction() again but does some checks before.
12150701b66SAndreas Gohr     *
12250701b66SAndreas Gohr     * @param string $from current action name
12350701b66SAndreas Gohr     * @param string $to new action name
12450701b66SAndreas Gohr     * @param null|ActionException $e any previous exception that caused the transition
12550701b66SAndreas Gohr     */
12650701b66SAndreas Gohr    protected function transitionAction($from, $to, $e = null) {
12750701b66SAndreas Gohr        $this->transitions++;
12850701b66SAndreas Gohr
12950701b66SAndreas Gohr        // no infinite recursion
13050701b66SAndreas Gohr        if($from == $to) {
13150701b66SAndreas Gohr            $this->handleFatalException(new FatalException('Infinite loop in actions', 500, $e));
13250701b66SAndreas Gohr        }
13350701b66SAndreas Gohr
13450701b66SAndreas Gohr        // larger loops will be caught here
13550701b66SAndreas Gohr        if($this->transitions >= self::MAX_TRANSITIONS) {
13650701b66SAndreas Gohr            $this->handleFatalException(new FatalException('Maximum action transitions reached', 500, $e));
13750701b66SAndreas Gohr        }
13850701b66SAndreas Gohr
13950701b66SAndreas Gohr        // do the recursion
14050701b66SAndreas Gohr        $this->setupAction($to);
14150701b66SAndreas Gohr    }
14250701b66SAndreas Gohr
14350701b66SAndreas Gohr    /**
14464ab5140SAndreas Gohr     * Aborts all processing with a message
14564ab5140SAndreas Gohr     *
14664ab5140SAndreas Gohr     * When a FataException instanc is passed, the code is treated as Status code
14764ab5140SAndreas Gohr     *
14864ab5140SAndreas Gohr     * @param \Exception|FatalException $e
1497675e707SAndreas Gohr     * @throws FatalException during unit testing
15064ab5140SAndreas Gohr     */
15124870174SAndreas Gohr    protected function handleFatalException(\Throwable $e) {
15224870174SAndreas Gohr        if($e instanceof FatalException) {
15364ab5140SAndreas Gohr            http_status($e->getCode());
15464ab5140SAndreas Gohr        } else {
15564ab5140SAndreas Gohr            http_status(500);
15664ab5140SAndreas Gohr        }
1577675e707SAndreas Gohr        if(defined('DOKU_UNITTEST')) {
1587675e707SAndreas Gohr            throw $e;
1597675e707SAndreas Gohr        }
160ecad51ddSAndreas Gohr        ErrorHandler::logException($e);
161985f440fSDamien Regad        $msg = 'Something unforeseen has happened: ' . $e->getMessage();
16264ab5140SAndreas Gohr        nice_die(hsc($msg));
16364ab5140SAndreas Gohr    }
16464ab5140SAndreas Gohr
16564ab5140SAndreas Gohr    /**
16664ab5140SAndreas Gohr     * Load the given action
16764ab5140SAndreas Gohr     *
16873522543SAndreas Gohr     * This translates the given name to a class name by uppercasing the first letter.
16973522543SAndreas Gohr     * Underscores translate to camelcase names. For actions with underscores, the different
17073522543SAndreas Gohr     * parts are removed beginning from the end until a matching class is found. The instatiated
17173522543SAndreas Gohr     * Action will always have the full original action set as Name
17273522543SAndreas Gohr     *
17373522543SAndreas Gohr     * Example: 'export_raw' -> ExportRaw then 'export' -> 'Export'
17473522543SAndreas Gohr     *
17564ab5140SAndreas Gohr     * @param $actionname
17664ab5140SAndreas Gohr     * @return AbstractAction
17764ab5140SAndreas Gohr     * @throws NoActionException
17864ab5140SAndreas Gohr     */
179480336a3SAndreas Gohr    public function loadAction($actionname) {
18073522543SAndreas Gohr        $actionname = strtolower($actionname); // FIXME is this needed here? should we run a cleanup somewhere else?
18173522543SAndreas Gohr        $parts = explode('_', $actionname);
18224870174SAndreas Gohr        while($parts !== []) {
18324870174SAndreas Gohr            $load = implode('_', $parts);
18473522543SAndreas Gohr            $class = 'dokuwiki\\Action\\' . str_replace('_', '', ucwords($load, '_'));
18564ab5140SAndreas Gohr            if(class_exists($class)) {
18673522543SAndreas Gohr                return new $class($actionname);
18764ab5140SAndreas Gohr            }
18873522543SAndreas Gohr            array_pop($parts);
18973522543SAndreas Gohr        }
19073522543SAndreas Gohr
19164ab5140SAndreas Gohr        throw new NoActionException();
19264ab5140SAndreas Gohr    }
19364ab5140SAndreas Gohr
19464ab5140SAndreas Gohr    /**
195480336a3SAndreas Gohr     * Execute all the checks to see if this action can be executed
196480336a3SAndreas Gohr     *
197480336a3SAndreas Gohr     * @param AbstractAction $action
198480336a3SAndreas Gohr     * @throws ActionDisabledException
199480336a3SAndreas Gohr     * @throws ActionException
200480336a3SAndreas Gohr     */
201480336a3SAndreas Gohr    public function checkAction(AbstractAction $action) {
202480336a3SAndreas Gohr        global $INFO;
203480336a3SAndreas Gohr        global $ID;
204480336a3SAndreas Gohr
205480336a3SAndreas Gohr        if(in_array($action->getActionName(), $this->disabled)) {
206480336a3SAndreas Gohr            throw new ActionDisabledException();
207480336a3SAndreas Gohr        }
208480336a3SAndreas Gohr
209b2c9cd19SAndreas Gohr        $action->checkPreconditions();
210480336a3SAndreas Gohr
211480336a3SAndreas Gohr        if(isset($INFO)) {
212480336a3SAndreas Gohr            $perm = $INFO['perm'];
213480336a3SAndreas Gohr        } else {
214480336a3SAndreas Gohr            $perm = auth_quickaclcheck($ID);
215480336a3SAndreas Gohr        }
216480336a3SAndreas Gohr
217480336a3SAndreas Gohr        if($perm < $action->minimumPermission()) {
218480336a3SAndreas Gohr            throw new ActionException('denied');
219480336a3SAndreas Gohr        }
220480336a3SAndreas Gohr    }
221480336a3SAndreas Gohr
222480336a3SAndreas Gohr    /**
22364ab5140SAndreas Gohr     * Returns the action handling the current request
22464ab5140SAndreas Gohr     *
22564ab5140SAndreas Gohr     * @return AbstractAction
22664ab5140SAndreas Gohr     */
22764ab5140SAndreas Gohr    public function getAction() {
22864ab5140SAndreas Gohr        return $this->action;
22964ab5140SAndreas Gohr    }
23064ab5140SAndreas Gohr}
231