xref: /plugin/struct/action/inline.php (revision c498205a7375a8f98f1ee968c0d6d3abe545279e)
14731b875SAndreas Gohr<?php
24731b875SAndreas Gohr/**
34731b875SAndreas Gohr * DokuWiki Plugin struct (Action Component)
44731b875SAndreas Gohr *
54731b875SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
64731b875SAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
74731b875SAndreas Gohr */
84731b875SAndreas Gohr
94731b875SAndreas Gohr// must be run within Dokuwiki
104ec54c67SAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable;
114731b875SAndreas Gohruse dokuwiki\plugin\struct\meta\Column;
1294c9aa4cSAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTableData;
134731b875SAndreas Gohruse dokuwiki\plugin\struct\meta\StructException;
144731b875SAndreas Gohruse dokuwiki\plugin\struct\meta\Validator;
154731b875SAndreas Gohr
164731b875SAndreas Gohrif(!defined('DOKU_INC')) die();
174731b875SAndreas Gohr
184731b875SAndreas Gohr/**
194731b875SAndreas Gohr * Class action_plugin_struct_inline
204731b875SAndreas Gohr *
214731b875SAndreas Gohr * Handle inline editing
224731b875SAndreas Gohr */
234731b875SAndreas Gohrclass action_plugin_struct_inline extends DokuWiki_Action_Plugin {
244731b875SAndreas Gohr
2594c9aa4cSAndreas Gohr    /** @var  AccessTableData */
264731b875SAndreas Gohr    protected $schemadata = null;
274731b875SAndreas Gohr
284731b875SAndreas Gohr    /** @var  Column */
294731b875SAndreas Gohr    protected $column = null;
304731b875SAndreas Gohr
314731b875SAndreas Gohr    /** @var String */
324731b875SAndreas Gohr    protected $pid = '';
334731b875SAndreas Gohr
344731b875SAndreas Gohr    /**
354731b875SAndreas Gohr     * Registers a callback function for a given event
364731b875SAndreas Gohr     *
374731b875SAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
384731b875SAndreas Gohr     * @return void
394731b875SAndreas Gohr     */
404731b875SAndreas Gohr    public function register(Doku_Event_Handler $controller) {
414731b875SAndreas Gohr        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax');
424731b875SAndreas Gohr    }
434731b875SAndreas Gohr
444731b875SAndreas Gohr    /**
454731b875SAndreas Gohr     * @param Doku_Event $event
464731b875SAndreas Gohr     * @param $param
474731b875SAndreas Gohr     */
484731b875SAndreas Gohr    public function handle_ajax(Doku_Event $event, $param) {
494731b875SAndreas Gohr        $len = strlen('plugin_struct_inline_');
504731b875SAndreas Gohr        if(substr($event->data, 0, $len) != 'plugin_struct_inline_') return;
514731b875SAndreas Gohr        $event->preventDefault();
524731b875SAndreas Gohr        $event->stopPropagation();
534731b875SAndreas Gohr
544731b875SAndreas Gohr        if(substr($event->data, $len) == 'editor') {
554731b875SAndreas Gohr            $this->inline_editor();
564731b875SAndreas Gohr        }
574731b875SAndreas Gohr
584731b875SAndreas Gohr        if(substr($event->data, $len) == 'save') {
594731b875SAndreas Gohr            try {
604731b875SAndreas Gohr                $this->inline_save();
614731b875SAndreas Gohr            } catch(StructException $e) {
624731b875SAndreas Gohr                http_status(500);
634731b875SAndreas Gohr                header('Content-Type: text/plain; charset=utf-8');
644731b875SAndreas Gohr                echo $e->getMessage();
654731b875SAndreas Gohr            }
664731b875SAndreas Gohr        }
67cdd09a96SAndreas Gohr
68cdd09a96SAndreas Gohr        if(substr($event->data, $len) == 'cancel') {
69cdd09a96SAndreas Gohr            $this->inline_cancel();
70cdd09a96SAndreas Gohr        }
714731b875SAndreas Gohr    }
724731b875SAndreas Gohr
73cdd09a96SAndreas Gohr    /**
74cdd09a96SAndreas Gohr     * Creates the inline editor
75cdd09a96SAndreas Gohr     */
764731b875SAndreas Gohr    protected function inline_editor() {
77cdd09a96SAndreas Gohr        // silently fail when editing not possible
784731b875SAndreas Gohr        if(!$this->initFromInput()) return;
79cdd09a96SAndreas Gohr        if(auth_quickaclcheck($this->pid) < AUTH_EDIT) return;
80cdd09a96SAndreas Gohr        if(checklock($this->pid)) return;
814731b875SAndreas Gohr
82cdd09a96SAndreas Gohr        // lock page
83cdd09a96SAndreas Gohr        lock($this->pid);
844731b875SAndreas Gohr
85cdd09a96SAndreas Gohr        // output the editor
864731b875SAndreas Gohr        $value = $this->schemadata->getDataColumn($this->column);
8763e019d0SAndreas Gohr        echo '<label data-column="'.hsc($this->column->getFullQualifiedLabel()).'">';
884731b875SAndreas Gohr        echo $value->getValueEditor('entry');
8963e019d0SAndreas Gohr        echo '</label>';
904731b875SAndreas Gohr        $hint = $this->column->getType()->getTranslatedHint();
914731b875SAndreas Gohr        if($hint) {
924731b875SAndreas Gohr            echo '<div class="hint">';
934731b875SAndreas Gohr            echo hsc($hint);
944731b875SAndreas Gohr            echo '</div>';
954731b875SAndreas Gohr        }
96cdd09a96SAndreas Gohr
97cdd09a96SAndreas Gohr        // csrf protection
98cdd09a96SAndreas Gohr        formSecurityToken();
994731b875SAndreas Gohr    }
1004731b875SAndreas Gohr
101cdd09a96SAndreas Gohr    /**
102cdd09a96SAndreas Gohr     * Save the data posted by the inline editor
103cdd09a96SAndreas Gohr     */
1044731b875SAndreas Gohr    protected function inline_save() {
1054731b875SAndreas Gohr        global $INPUT;
1064731b875SAndreas Gohr
10713eddb0fSAndreas Gohr        // check preconditions
1084d2da382SAndreas Gohr        if(!$this->initFromInput()) {
1094d2da382SAndreas Gohr            throw new StructException('inline save error: init');
1104d2da382SAndreas Gohr        }
11113eddb0fSAndreas Gohr        self::checkCSRF();
11213eddb0fSAndreas Gohr        if(!$this->schemadata->getSchema()->isLookup()) {
11313eddb0fSAndreas Gohr            $this->checkPage();
1144731b875SAndreas Gohr        }
1154731b875SAndreas Gohr
1164731b875SAndreas Gohr        // validate
1174731b875SAndreas Gohr        $value = $INPUT->param('entry');
1184731b875SAndreas Gohr        $validator = new Validator();
1194731b875SAndreas Gohr        if(!$validator->validateValue($this->column, $value)) {
1204731b875SAndreas Gohr            throw new StructException(join("\n", $validator->getErrors()));
1214731b875SAndreas Gohr        }
1224731b875SAndreas Gohr
1234731b875SAndreas Gohr        // current data
1244731b875SAndreas Gohr        $tosave = $this->schemadata->getDataArray();
1254731b875SAndreas Gohr        $tosave[$this->column->getLabel()] = $value;
1264731b875SAndreas Gohr
1274731b875SAndreas Gohr        // save
12813eddb0fSAndreas Gohr        if($this->schemadata->getSchema()->isLookup()) {
12913eddb0fSAndreas Gohr            $revision = 0;
13013eddb0fSAndreas Gohr        } else {
13113eddb0fSAndreas Gohr            $revision = helper_plugin_struct::createPageRevision($this->pid, 'inline edit');
13213eddb0fSAndreas Gohr        }
13313eddb0fSAndreas Gohr        $this->schemadata->setTimestamp($revision);
13413eddb0fSAndreas Gohr        try {
13513eddb0fSAndreas Gohr            if(!$this->schemadata->saveData($tosave)) {
13613eddb0fSAndreas Gohr                throw new StructException('saving failed');
13713eddb0fSAndreas Gohr            }
13813eddb0fSAndreas Gohr        } finally {
13913eddb0fSAndreas Gohr            // unlock (unlocking a non-existing file is okay,
14013eddb0fSAndreas Gohr            // so we don't check if it's a lookup here
141cdd09a96SAndreas Gohr            unlock($this->pid);
14213eddb0fSAndreas Gohr        }
1434731b875SAndreas Gohr
1444731b875SAndreas Gohr        // reinit then render
1454731b875SAndreas Gohr        $this->initFromInput();
1464731b875SAndreas Gohr        $value = $this->schemadata->getDataColumn($this->column);
1474731b875SAndreas Gohr        $R = new Doku_Renderer_xhtml();
1484731b875SAndreas Gohr        $value->render($R, 'xhtml'); // FIXME use configured default renderer
1494731b875SAndreas Gohr        echo $R->doc;
1504731b875SAndreas Gohr    }
1514731b875SAndreas Gohr
15213eddb0fSAndreas Gohr
15313eddb0fSAndreas Gohr
1544731b875SAndreas Gohr    /**
155cdd09a96SAndreas Gohr     * Unlock a page (on cancel action)
156cdd09a96SAndreas Gohr     */
157cdd09a96SAndreas Gohr    protected function inline_cancel() {
158cdd09a96SAndreas Gohr        global $INPUT;
159cdd09a96SAndreas Gohr        $pid = $INPUT->str('pid');
160cdd09a96SAndreas Gohr        unlock($pid);
161cdd09a96SAndreas Gohr    }
162cdd09a96SAndreas Gohr
163cdd09a96SAndreas Gohr    /**
1644731b875SAndreas Gohr     * Initialize internal state based on input variables
1654731b875SAndreas Gohr     *
1664731b875SAndreas Gohr     * @return bool if initialization was successfull
1674731b875SAndreas Gohr     */
1684731b875SAndreas Gohr    protected function initFromInput() {
1694731b875SAndreas Gohr        global $INPUT;
1704731b875SAndreas Gohr
1714731b875SAndreas Gohr        $this->schemadata = null;
1724731b875SAndreas Gohr        $this->column = null;
1734731b875SAndreas Gohr
1744731b875SAndreas Gohr        $pid = $INPUT->str('pid');
1754731b875SAndreas Gohr        list($table, $field) = explode('.', $INPUT->str('field'));
1764731b875SAndreas Gohr        if(blank($pid)) return false;
1774731b875SAndreas Gohr        if(blank($table)) return false;
1784731b875SAndreas Gohr        if(blank($field)) return false;
1794731b875SAndreas Gohr
1804731b875SAndreas Gohr        $this->pid = $pid;
1814ec54c67SAndreas Gohr        try {
1824ec54c67SAndreas Gohr            $this->schemadata = AccessTable::byTableName($table, $pid);
1834ec54c67SAndreas Gohr        } catch(StructException $ignore) {
1844731b875SAndreas Gohr            return false;
1854731b875SAndreas Gohr        }
1864731b875SAndreas Gohr
1874ec54c67SAndreas Gohr        $this->column = $this->schemadata->getSchema()->findColumn($field);
1884731b875SAndreas Gohr        if(!$this->column || !$this->column->isVisibleInEditor()) {
1894731b875SAndreas Gohr            $this->schemadata = null;
1904731b875SAndreas Gohr            $this->column = null;
1914731b875SAndreas Gohr            return false;
1924731b875SAndreas Gohr        }
1934731b875SAndreas Gohr
1944731b875SAndreas Gohr        return true;
1954731b875SAndreas Gohr    }
1964731b875SAndreas Gohr
19713eddb0fSAndreas Gohr    /**
19813eddb0fSAndreas Gohr     * Checks if a page can be edited
19913eddb0fSAndreas Gohr     *
20013eddb0fSAndreas Gohr     * @throws StructException when check fails
20113eddb0fSAndreas Gohr     */
20213eddb0fSAndreas Gohr    protected function checkPage() {
20313eddb0fSAndreas Gohr        if(!page_exists($this->pid)) {
20413eddb0fSAndreas Gohr            throw new StructException('inline save error: no such page');
20513eddb0fSAndreas Gohr        }
20613eddb0fSAndreas Gohr        if(auth_quickaclcheck($this->pid) < AUTH_EDIT) {
20713eddb0fSAndreas Gohr            throw new StructException('inline save error: acl');
20813eddb0fSAndreas Gohr        }
20913eddb0fSAndreas Gohr        if(checklock($this->pid)) {
21013eddb0fSAndreas Gohr            throw new StructException('inline save error: lock');
21113eddb0fSAndreas Gohr        }
21213eddb0fSAndreas Gohr    }
21313eddb0fSAndreas Gohr
21413eddb0fSAndreas Gohr    /**
21513eddb0fSAndreas Gohr     * Our own implementation of checkSecurityToken because we don't want the msg() call
21613eddb0fSAndreas Gohr     *
21713eddb0fSAndreas Gohr     * @throws StructException when check fails
21813eddb0fSAndreas Gohr     */
219*c498205aSAndreas Gohr    public static function checkCSRF() {
22013eddb0fSAndreas Gohr        global $INPUT;
22113eddb0fSAndreas Gohr        if(
22213eddb0fSAndreas Gohr            $INPUT->server->str('REMOTE_USER') &&
22313eddb0fSAndreas Gohr            getSecurityToken() != $INPUT->str('sectok')
22413eddb0fSAndreas Gohr        ) {
22513eddb0fSAndreas Gohr            throw new StructException('CSRF check failed');
22613eddb0fSAndreas Gohr        }
22713eddb0fSAndreas Gohr    }
22813eddb0fSAndreas Gohr
2294731b875SAndreas Gohr}
230