1<?php 2/** 3 * DokuWiki Plugin do (Action Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Andreas Gohr <gohr@cosmocode.de> 7 * @author Adrian Lang <lang@cosmocode.de> 8 * @author Dominik Eckelmann <eckelmann@cosmocode.de> 9 */ 10 11class action_plugin_do extends DokuWiki_Action_Plugin 12{ 13 14 /** 15 * Register handlers for some event hooks 16 * 17 * @param Doku_Event_Handler $controller 18 */ 19 public function register(Doku_Event_Handler $controller) 20 { 21 $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call'); 22 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_act_preprocess'); 23 $controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'handle_delete'); 24 $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, '_adduser'); 25 } 26 27 /** 28 * @param Doku_Event $event event object by reference 29 * @param null $param the parameters passed to register_hook when this handler was registered 30 */ 31 public function _adduser(&$event, $param) 32 { 33 if (!isset($_SERVER['REMOTE_USER'])) { 34 return; 35 } 36 global $JSINFO; 37 /** @var helper_plugin_do $hlp */ 38 $hlp = plugin_load('helper', 'do'); 39 $JSINFO['plugin_do_user'] = $_SERVER['REMOTE_USER']; 40 $JSINFO['plugin_do_user_name'] = $hlp->getPrettyUser($_SERVER['REMOTE_USER']); 41 $JSINFO['plugin_do_user_clean'] = html_entity_decode(strip_tags($JSINFO['plugin_do_user_name'])); 42 } 43 44 /** 45 * @param Doku_Event $event event object by reference 46 * @param null $param the parameters passed to register_hook when this handler was registered 47 * 48 * @return bool 49 */ 50 public function handle_ajax_call(&$event, $param) 51 { 52 if ($event->data == 'plugin_do') { // FIXME: refactor this into early return and switch 53 // toggle status of a single task 54 global $INPUT; 55 56 $event->preventDefault(); 57 $event->stopPropagation(); 58 59 $id = cleanID($_REQUEST['do_page']); 60 61 if (auth_quickaclcheck($id) < AUTH_EDIT) { 62 if ($INPUT->server->has('REMOTE_USER')) { 63 echo -2; //not allowed 64 } else { 65 echo -1; //not logged in 66 } 67 return; 68 } 69 70 /** @var helper_plugin_do $hlp */ 71 $hlp = plugin_load('helper', 'do'); 72 $status = $hlp->toggleTaskStatus($id, $_REQUEST['do_md5'], $_REQUEST['do_commit']); 73 74 // rerender the page 75 p_get_metadata($id, '', true); 76 77 header('Content-Type: application/json; charset=utf-8'); 78 echo json_encode($status); 79 80 } elseif ($event->data == 'plugin_do_status') { 81 // read status for a bunch of tasks 82 83 $event->preventDefault(); 84 $event->stopPropagation(); 85 86 $page = cleanID($_REQUEST['do_page']); 87 88 if (auth_quickaclcheck($page) < AUTH_READ) { 89 $status = array(); 90 } else { 91 /** @var helper_plugin_do $hlp */ 92 $hlp = plugin_load('helper', 'do'); 93 $status = $hlp->getAllPageStatuses($page); 94 } 95 96 header('Content-Type: application/json; charset=utf-8'); 97 echo json_encode($status); 98 } elseif ($event->data === 'plugin_do_userTasksOverlay') { 99 $event->preventDefault(); 100 $event->stopPropagation(); 101 102 global $INPUT; 103 104 if (!$INPUT->server->has('REMOTE_USER')) { 105 http_status(401, 'login required'); 106 return false; 107 } 108 109 $user = $INPUT->server->str('REMOTE_USER'); 110 111 /** @var helper_plugin_do $hlp */ 112 $hlp = plugin_load('helper', 'do'); 113 $tasks = $hlp->loadTasks(array('status' => array('undone'), 'user' => $user)); 114 /** @var syntax_plugin_do_dolist $syntax */ 115 $syntax = plugin_load('syntax', 'do_dolist'); 116 $html = $syntax->buildTasklistHTML($tasks, true, false); 117 header('Content-Type: text/html; charset=utf-8'); 118 echo $html; 119 } 120 } 121 122 /** 123 * @param Doku_Event $event event object by reference 124 * @param null $param the parameters passed to register_hook when this handler was registered 125 * 126 * @return bool 127 */ 128 public function handle_act_preprocess(&$event, $param) 129 { 130 131 if ($event->data != 'plugin_do') { 132 return true; 133 } 134 global $INPUT; 135 136 $pageid = cleanID($_REQUEST['do_page']); 137 $status = ''; 138 if (auth_quickaclcheck($pageid) < AUTH_EDIT) { 139 $lvl = -1; 140 $key = 'notloggedin'; 141 if ($INPUT->server->has('REMOTE_USER')) { 142 $key = 'notallowed'; 143 } 144 } else { 145 /** @var helper_plugin_do $hlp */ 146 $hlp = plugin_load('helper', 'do'); 147 $status = $hlp->toggleTaskStatus($pageid, $_REQUEST['do_md5']); 148 if ($status == -2) { 149 $lvl = -1; 150 $key = 'notallowed'; 151 } else { 152 $lvl = 1; 153 if ($status) { 154 $key = 'done'; 155 } else { 156 $key = 'open'; 157 } 158 } 159 160 } 161 162 $jslang = $this->getLang('js'); 163 msg(sprintf($jslang[$key], $status), $lvl); 164 165 global $ACT; 166 $ACT = 'show'; 167 return true; 168 } 169 170 /** 171 * Delete all tasks associated with a page from database, if all have been removed from the page 172 * 173 * @param Doku_Event $event event object by reference 174 * @param null $param the parameters passed to register_hook when this handler was registered 175 */ 176 public function handle_delete(&$event, $param) 177 { 178 if (preg_match('/<do[^>]*>.*<\/do>/i', $event->data[0][1])) { 179 // Only run if all tasks where removed from the page, partial removes are handled in \syntax_plugin_do_do::_save 180 return; 181 } 182 $namespace = $event->data[1] ? $event->data[1] . ':' : ''; 183 $id = $namespace . $event->data[2]; 184 if (isset($this->run[$id])) { 185 // Only execute on the first run 186 return; 187 } 188 189 /** @var helper_plugin_do $hlp */ 190 $hlp = plugin_load('helper', 'do'); 191 $hlp->cleanPageTasks($id); 192 $this->run[$id] = true; 193 } 194 195} 196 197