1<?php
2
3class helper_plugin_bureaucracyau_actionscript extends helper_plugin_bureaucracyau_action {
4
5    protected $scriptNamePattern = '/^[_a-zA-Z0-9]+\.php$/';
6
7    /**
8     * @inheritDoc
9     * @throws \InvalidArgumentException
10     */
11    public function run($fields, $thanks, $argv) {
12        if (count($argv) < 1) {
13            throw new InvalidArgumentException('The "script"-action expects exactly 1 argument: the script name.');
14        }
15
16        $scriptName = $argv[0];
17
18        if (!$this->validateScriptName($scriptName)) {
19            $cleanedScriptName = hsc($scriptName);
20            throw new InvalidArgumentException("The supplied scriptname \"<code>$cleanedScriptName</code>\" is invalid! It must conform to <code>{hsc($this->scriptNamePattern)}</code>!");
21        }
22
23        $path = DOKU_CONF . 'plugin/bureaucracy/' . $scriptName;
24
25        if (!file_exists($path)) {
26            $shortPath = 'conf/plugin/bureaucracy/' . $scriptName;
27            throw new InvalidArgumentException("Script <code>$shortPath</code> doesn't exist!");
28        }
29
30        require $path;
31
32        $classFragment = substr($scriptName, 0, strpos($scriptName, '.'));
33        $className = 'helper_plugin_bureaucracyau_handler_' . $classFragment;
34
35        $deprecatedClassName = 'bureaucracyau_handler_' . $classFragment;
36        if (!class_exists($className) && class_exists($deprecatedClassName)) {
37            msg("Please change this script's class-name to <code>$className</code>.
38Your current scheme <code>$deprecatedClassName</code> is deprecated and will stop working in the future.", 2);
39            $className = $deprecatedClassName;
40        }
41
42        /** @var dokuwiki\plugin\bureaucracy\interfaces\bureaucracyau_handler_interface $handler */
43        $handler = new $className;
44
45        if (!is_a($handler, dokuwiki\plugin\bureaucracy\interfaces\bureaucracyau_handler_interface::class)) {
46            throw new InvalidArgumentException('The handler must implement the interface <code>dokuwiki\\plugin\\bureaucracy\\interfaces\\bureaucracyau_handler_interface</code> !');
47        }
48
49        return $handler->handleData($fields, $thanks);
50    }
51
52    /**
53     * @param $scriptName
54     *
55     * @return bool
56     */
57    protected function validateScriptName($scriptName) {
58        $valid = preg_match($this->scriptNamePattern, $scriptName);
59        return $valid === 1;
60    }
61
62}
63