1<?php 2 3// must be run within Dokuwiki 4if(!defined('DOKU_INC')) die(); 5 6/** 7 * Add-New-Page Plugin: a simple form for adding new pages. 8 * 9 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 10 * @author iDO <ido@idotech.info> 11 * @author Sam Wilson <sam@samwilson.id.au> 12 */ 13class syntax_plugin_addnewpage extends DokuWiki_Syntax_Plugin { 14 15 /** @var array the parsed options */ 16 protected $options; 17 18 /** 19 * Syntax Type 20 */ 21 public function getType() { 22 return 'substition'; 23 } 24 25 /** 26 * Paragraph Type 27 */ 28 public function getPType() { 29 return 'block'; 30 } 31 32 /** 33 * @return int 34 */ 35 public function getSort() { 36 return 199; 37 } 38 39 /** 40 * @param string $mode 41 */ 42 public function connectTo($mode) { 43 $this->Lexer->addSpecialPattern('\{\{NEWPAGE[^\}]*\}\}', $mode, 'plugin_addnewpage'); 44 } 45 46 /** 47 * Handler to prepare matched data for the rendering process 48 * 49 * Handled syntax options: 50 * {{NEWPAGE}} 51 * {{NEWPAGE>your:namespace}} 52 * {{NEWPAGE#newtpl1,newtpl2}} 53 * {{NEWPAGE#newtpl1|Title1,newtpl2|Title1}} 54 * {{NEWPAGE>your:namespace#newtpl1|Title1,newtpl2|Title1}} 55 * {{NEWPAGE>your:namespace#newtpl1|Title1,newtpl2|Title1#@HI@,Howdy}} 56 * 57 * @param string $match The text matched by the patterns 58 * @param int $state The lexer state for the match 59 * @param int $pos The character position of the matched text 60 * @param Doku_Handler $handler The Doku_Handler object 61 * @return array Return an array with all data you want to use in render 62 * @codingStandardsIgnoreStart 63 */ 64 public function handle($match, $state, $pos, Doku_Handler $handler) { 65 /* @codingStandardsIgnoreEnd */ 66 $match = substr($match, 9, -2); // strip markup 67 68 $data = array( 69 'namespace' => '', 70 'newpagetemplates' => array(), 71 'newpagevars' => array(), 72 'options' => array( 73 'exclude' => $this->getConf('addpage_exclude'), 74 'showroot' => $this->getConf('addpage_showroot'), 75 'hide' => $this->getConf('addpage_hide'), 76 'hideacl' => $this->getConf('addpage_hideACL'), 77 'autopage' => $this->getConf('addpage_autopage'), 78 ) 79 ); 80 81 if(preg_match('/>(.*?)(#|\?|$)/', $match, $m)) { 82 $data['namespace'] = trim($m[1]); 83 } 84 85 if(preg_match('/#(.*?)(#.*?)?(\?|$)/', $match, $m)) { 86 $data['newpagetemplates'] = array_map('trim', explode(',', $m[1])); 87 $data['newpagevars'] = array_map('trim', explode(',', $m[2])); 88 } 89 90 if(preg_match('/\?(.*?)(#|$)/', $match, $m)) { 91 $this->_parseOptions($m[1], $data['options']); 92 } 93 94 return $data; 95 } 96 97 /** 98 * Create the new-page form. 99 * 100 * @param $mode string output format being rendered 101 * @param $renderer Doku_Renderer the current renderer object 102 * @param $data array data created by handler() 103 * @return boolean rendered correctly? 104 */ 105 public function render($mode, Doku_Renderer $renderer, $data) { 106 global $lang; 107 108 // make options available in class 109 $this->options = $data['options']; 110 111 if($mode == 'xhtml') { 112 $disablecache = null; 113 $namespaceinput = $this->_htmlNamespaceInput($data['namespace'], $disablecache); 114 if($namespaceinput === false) { 115 if($this->options['hideacl']) { 116 $renderer->doc .= ''; 117 } else { 118 $renderer->doc .= $this->getLang('nooption'); 119 } 120 return true; 121 } 122 if($disablecache) $renderer->info['cache'] = false; 123 124 $newpagetemplateinput = $this->_htmlTemplateInput($data['newpagetemplates']); 125 126 $input = 'text'; 127 if($this->options['autopage']) $input = 'hidden'; 128 129 $form = '<div class="addnewpage"><p>' 130 . '<form name="addnewpage" method="get" action="' . DOKU_BASE . DOKU_SCRIPT . '" accept-charset="' . $lang['encoding'] . '">' 131 . $namespaceinput 132 . '<input class="edit" type="text" name="title" size="20" maxlength="255" tabindex="2" />' 133 . $newpagetemplateinput 134 . '<input type="hidden" name="newpagevars" value="' . $data['newpagevars'] . '"/>' 135 . '<input type="hidden" name="do" value="edit" />' 136 . '<input type="hidden" name="id" />' 137 . '<input class="button" type="submit" value="' . $this->getLang('okbutton') . '" tabindex="4" />' 138 . '</form>' 139 . '</p></div>'; 140 141 $renderer->doc .= $form; 142 143 return true; 144 } 145 return false; 146 } 147 148 /** 149 * Overwrites the $options with the ones parsed from $optstr 150 * 151 * @param string $optstr 152 * @param array $options 153 * @author Andreas Gohr <gohr@cosmocode.de> 154 */ 155 protected function _parseOptions($optstr, &$options) { 156 $opts = preg_split('/[,&]/', $optstr); 157 158 foreach($opts as $opt) { 159 $opt = strtolower(trim($opt)); 160 $val = true; 161 // booleans can be negated with a no prefix 162 if(substr($opt, 0, 2) == 'no') { 163 $opt = substr($opt, 2); 164 $val = false; 165 } 166 167 // not a known option? might be a key=value pair 168 if(!isset($options[$opt])) { 169 list($opt, $val) = array_map('trim', explode('=', $opt, 2)); 170 } 171 172 // still unknown? skip it 173 if(!isset($options[$opt])) continue; 174 175 // overwrite the current value 176 $options[$opt] = $val; 177 } 178 } 179 180 /** 181 * Parse namespace request 182 * 183 * This creates the final ID to be created (still having an @INPUT@ variable 184 * which is filled in via JavaScript) 185 * 186 * @author Samuele Tognini <samuele@cli.di.unipi.it> 187 * @author Michael Braun <michael-dev@fami-braun.de> 188 * @author Andreas Gohr <gohr@cosmocode.de> 189 * @param string $ns The namespace as given in the syntax 190 * @return string 191 */ 192 protected function _parseNS($ns) { 193 global $INFO; 194 195 $selfid = $INFO['id']; 196 $selfns = getNS($INFO['id']); 197 // replace the input variable with something unique that survives cleanID 198 $keep = sha1(time()); 199 200 // by default append the input to the namespace (except on autopage) 201 if(strpos($ns, '@INPUT@') === false && !$this->options['autopage']) $ns .= ":@INPUT@"; 202 203 // date replacements 204 $ns = dformat(null, $ns); 205 206 // placeholders 207 $replacements = array( 208 '/\//' => ':', // forward slashes to colons 209 '/@PAGE@/' => $selfid, 210 '/@NS@/' => $selfns, 211 '/^\.(:|\/|$)/' => "$selfns:", 212 '/@INPUT@/' => $keep, 213 ); 214 $ns = preg_replace(array_keys($replacements), array_values($replacements), $ns); 215 216 // clean up, then reinsert the input variable 217 $ns = cleanID($ns); 218 $ns = str_replace($keep, '@INPUT@', $ns); 219 220 return $ns; 221 } 222 223 /** 224 * Create the HTML Select element for namespace selection. 225 * 226 * @param string|false $dest_ns The destination namespace, or false if none provided. 227 * @param bool $disablecache reference indicates if caching need to be disabled 228 * @global string $ID The page ID 229 * @return string Select element with appropriate NS selected. 230 */ 231 protected function _htmlNamespaceInput($dest_ns, &$disablecache) { 232 global $ID; 233 $disablecache = false; 234 235 // If a NS has been provided: 236 // Whether to hide the NS selection (otherwise, show only subnamespaces). 237 $hide = $this->options['hide']; 238 239 $parsed_dest_ns = $this->_parseNS($dest_ns); 240 // Whether the user can create pages in the provided NS (or root, if no 241 // destination NS has been set. 242 $can_create = (auth_quickaclcheck($parsed_dest_ns . ":") >= AUTH_CREATE); 243 244 //namespace given, but hidden 245 if($hide && !empty($dest_ns)) { 246 if($can_create) { 247 return '<input type="hidden" name="np_cat" id="np_cat" value="' . $parsed_dest_ns . '"/>'; 248 } else { 249 return false; 250 } 251 } 252 253 //show select of given namespace 254 $currentns = getNS($ID); 255 256 $ret = '<select class="edit" id="np_cat" name="np_cat" tabindex="1">'; 257 258 // Whether the NS select element has any options 259 $someopt = false; 260 261 // Show root namespace if requested and allowed 262 if($this->options['showroot'] && $can_create) { 263 if(empty($dest_ns)) { 264 // If no namespace has been provided, add an option for the root NS. 265 $ret .= '<option ' . (($currentns == '') ? 'selected ' : '') . 'value="">' . $this->getLang('namespaceRoot') . '</option>'; 266 $someopt = true; 267 } else { 268 // If a namespace has been provided, add an option for it. 269 $ret .= '<option ' . (($currentns == $dest_ns) ? 'selected ' : '') . 'value="' . formText($dest_ns) . '">' . formText($dest_ns) . '</option>'; 270 $someopt = true; 271 } 272 } 273 274 $subnamespaces = $this->_getNamespaceList($dest_ns); 275 276 // The top of this stack will always be the last printed ancestor namespace 277 $ancestor_stack = array(); 278 if(!empty($dest_ns)) { 279 array_push($ancestor_stack, $dest_ns); 280 } 281 282 foreach($subnamespaces as $ns) { 283 284 if(auth_quickaclcheck($ns . ":") < AUTH_CREATE) continue; 285 286 // Pop any elements off the stack that are not ancestors of the current namespace 287 while(!empty($ancestor_stack) && strpos($ns, $ancestor_stack[count($ancestor_stack) - 1] . ':') !== 0) { 288 array_pop($ancestor_stack); 289 } 290 291 $nsparts = explode(':', $ns); 292 $first_unprinted_depth = empty($ancestor_stack) ? 1 : (2 + substr_count($ancestor_stack[count($ancestor_stack) - 1], ':')); 293 for($i = $first_unprinted_depth, $end = count($nsparts); $i <= $end; $i++) { 294 $namespace = implode(':', array_slice($nsparts, 0, $i)); 295 array_push($ancestor_stack, $namespace); 296 $selectOptionText = str_repeat(' ', substr_count($namespace, ':')) . $nsparts[$i - 1]; 297 $ret .= '<option ' . 298 (($currentns == $namespace) ? 'selected ' : '') . 299 ($i == $end ? ('value="' . $namespace . '">') : 'disabled>') . 300 $selectOptionText . 301 '</option>'; 302 } 303 $someopt = true; 304 $disablecache = true; 305 } 306 307 $ret .= '</select>'; 308 309 if($someopt) { 310 return $ret; 311 } else { 312 return false; 313 } 314 } 315 316 /** 317 * Get a list of namespaces below the given namespace. 318 * Recursively fetches subnamespaces. 319 * 320 * @param string $topns The top namespace 321 * @return array Multi-dimensional array of all namespaces below $tns 322 */ 323 protected function _getNamespaceList($topns = '') { 324 global $conf; 325 326 $topns = utf8_encodeFN(str_replace(':', '/', $topns)); 327 328 $excludes = $this->options['exclude']; 329 if($excludes == "") { 330 $excludes = array(); 331 } else { 332 $excludes = @explode(';', strtolower($excludes)); 333 } 334 $searchdata = array(); 335 search($searchdata, $conf['datadir'], 'search_namespaces', array(), $topns); 336 337 $namespaces = array(); 338 foreach($searchdata as $ns) { 339 foreach($excludes as $exclude) { 340 if(!empty($exclude) && strpos($ns['id'], $exclude) === 0) { 341 continue 2; 342 } 343 } 344 $namespaces[] = $ns['id']; 345 } 346 347 return $namespaces; 348 } 349 350 /** 351 * Create html for selection of namespace templates 352 * 353 * @param array $newpagetemplates array of namespace templates 354 * @return string html of select or hidden input 355 */ 356 public function _htmlTemplateInput($newpagetemplates) { 357 $cnt = count($newpagetemplates); 358 if($cnt < 1 || $cnt == 1 && $newpagetemplates[0] == '') { 359 $input = ''; 360 361 } else { 362 if($cnt == 1) { 363 list($template,) = $this->_parseNSTemplatePage($newpagetemplates[0]); 364 $input = '<input type="hidden" name="newpagetemplate" value="' . formText($template) . '" />'; 365 } else { 366 $first = true; 367 $input = '<select name="newpagetemplate" tabindex="3">'; 368 foreach($newpagetemplates as $template) { 369 $p = ($first ? ' selected="selected"' : ''); 370 $first = false; 371 372 list($template, $name) = $this->_parseNSTemplatePage($template); 373 $p .= ' value="' . formText($template) . '"'; 374 $input .= "<option $p>" . formText($name) . "</option>"; 375 } 376 $input .= '</select>'; 377 } 378 $input = DOKU_TAB . DOKU_TAB . $input . DOKU_LF; 379 } 380 return $input; 381 } 382 383 /** 384 * Parses and resolves the namespace template page 385 * 386 * @param $nstemplate 387 * @return array 388 */ 389 protected function _parseNSTemplatePage($nstemplate) { 390 global $ID; 391 392 @list($template, $name) = explode('|', $nstemplate, 2); 393 394 $exist = null; 395 resolve_pageid(getNS($ID), $template, $exist); //get absolute id 396 397 if(is_null($name)) $name = $template; 398 399 return array($template, $name); 400 } 401 402} 403