1<?php 2 3// must be run within Dokuwiki 4if (!defined('DOKU_INC')) die(); 5 6if (!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../../') . '/'); 7if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/'); 8require_once(DOKU_PLUGIN . 'syntax.php'); 9 10/** 11 * Add-New-Page Plugin: a simple form for adding new pages. 12 * 13 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 14 * @author iDO <ido@idotech.info> 15 * @author Sam Wilson <sam@samwilson.id.au> 16 */ 17class syntax_plugin_addnewpage extends DokuWiki_Syntax_Plugin { 18 19 /** 20 * Get some information about this plugin. 21 * 22 * @return array The info array. 23 */ 24 function getInfo() { 25 return array( 26 'author' => 'iDo, Sam Wilson, Michael Braun', 27 'email' => '', 28 'date' => '2013-03-06', 29 'name' => 'addnewpage', 30 'desc' => 'Adds a "new page form" to any wiki page.', 31 'url' => 'https://wiki.dokuwiki.org/plugin:addnewpage', 32 ); 33 } 34 35 function getType() { return 'substition'; } 36 37 function getPType() { return 'block'; } 38 39 function getSort() { return 199; } 40 41 function connectTo($mode) { 42 $this->Lexer->addSpecialPattern('\{\{NEWPAGE[^\}]*\}\}', $mode, 'plugin_addnewpage'); 43 } 44 45 function handle($match, $state, $pos, &$handler) { 46 $ns = substr($match, 10, -2); // strip markup 47 return array($ns); // use an array here 48 } 49 50 /** 51 * Create the new-page form. 52 * 53 * @return boolean 54 */ 55 function render($mode, &$renderer, $data) { 56 global $lang; 57 $renderer->info['cache'] = false; 58 $data = $data[0]; // get data back from the array 59 60 if ($mode == 'xhtml') { 61 $ns_select = $this->_makecombo($data); 62 if ($ns_select == $this->getLang('nooption')) { 63 $renderer->doc .= (!$this->getConf('addpage_hideACL')) ? $ns_select : ''; 64 return true; 65 } 66 67 $button_val = ((@$this->getLang('okbutton')) ? $this->getLang('okbutton') : 'ok'); 68 $form = '<div class="addnewpage">'.DOKU_LF 69 .DOKU_TAB.'<form name="addnewpage" method="post" accept-charset="'.$lang['encoding'].'">'.DOKU_LF 70 .DOKU_TAB.DOKU_TAB.$ns_select.DOKU_LF 71 .DOKU_TAB.DOKU_TAB.'<input class="edit" type="text" name="title" size="20" maxlength="255" tabindex="2" />'.DOKU_LF 72 .DOKU_TAB.DOKU_TAB.'<input class="button" type="submit" value="'.$button_val.'" tabindex="3" />'.DOKU_LF 73 .DOKU_TAB.'</form>'.DOKU_LF 74 .'</div>'; 75 $renderer->doc .= $form; 76 77 return true; 78 } 79 return false; 80 } 81 82 /** 83 * Parse namespace request 84 * 85 * @author Samuele Tognini <samuele@cli.di.unipi.it> 86 * @author Michael Braun <michael-dev@fami-braun.de> 87 */ 88 function _parse_ns($ns) { 89 global $ID; 90 if ($ns == "@PAGE@") return $ID; 91 if ($ns == "@NS@") return getNS($ID); 92 $ns = preg_replace("/^\.(:|$)/", dirname(str_replace(':', '/', $ID)) . "$1", $ns); 93 $ns = str_replace("/", ":", $ns); 94 $ns = cleanID($ns); 95 return $ns; 96 } 97 98 /** 99 * Create the HTML Select element for namespace selection. 100 * 101 * @global string $ID The page ID 102 * @param string|false $data The destination namespace, or false if none provided. 103 * @return string Select element with appropriate NS selected. 104 */ 105 function _makecombo($data) { 106 global $ID; 107 108 $hide = $this->getConf('addpage_hide'); 109 110 if (($data != "") && ($hide)) { 111 return '<input type="hidden" name="np_cat" id="np_cat" value="'.$this->_parse_ns($data).'"/>'; 112 } 113 114 $ns = explode(':', $ID); 115 array_pop($ns); 116 $ns = implode(':', $ns); 117 118 $r = $this->_getnslist(""); 119 $ret = '<select class="edit" id="np_cat" name="np_cat" tabindex="1">'; 120 121 $someopt=false; 122 123 if ($this->getConf('addpage_showroot')) { 124 $root_disabled = (auth_quickaclcheck($data.":") < AUTH_CREATE); 125 if ($data=='') { 126 if (!$root_disabled) { 127 $ret.='<option '.(($ns=='')?'selected ':'').'value="">'.((@$this->getLang('namespaceRoot'))?$this->getLang('namespaceRoot'):'top').'</option>'; 128 $someopt=true; 129 } 130 } else { 131 if (!$root_disabled) { 132 $ret.='<option '.(($ns==$data)?'selected ':'').'value="'.$data.'">'.$data.'</option>'; 133 $someopt=true; 134 } 135 } 136 } 137 138 foreach ($r as $k => $v) { 139 if ($data != '') { 140 if (strpos(":" . $v, ":" . $data . ":") === false) { 141 continue; 142 } 143 } 144 if (auth_quickaclcheck($v . ":") < AUTH_CREATE) continue; 145 $vv = explode(':', $v); 146 $vv = str_repeat(' ', substr_count($v, ':')) . $vv[count($vv) - 1]; 147 $ret.='<option '.(($ns == $v) ? 'selected ' : '').'value="'.$v.'">'.$vv.'</option>'; 148 $someopt = true; 149 } 150 $ret.='</select>'; 151 if (!$someopt) $ret = $this->getLang('nooption'); 152 153 return $ret; 154 } 155 156 function _getnslist($tns = '') { 157 require_once(DOKU_INC . 'inc/search.php'); 158 global $conf; 159 if ($tns == '') $tns = $conf['datadir']; 160 if (!is_dir($tns)) $tns = str_replace(':', '/', $tns); 161 $data = array(); 162 $exclude = $this->getConf('addpage_exclude'); 163 164 if ($exclude == "") $exclude = array(); 165 else $exclude = @explode(';', strtolower($exclude)); 166 167 search($data, $tns, 'search_index', array('ns' => '')); 168 169 $data2 = array(); 170 foreach ($data as $k => $v) { 171 if ($v['type'] == 'd') { 172 if (!in_array(strtolower($v['id']), $exclude)) { 173 array_push($data2, $v['id']); 174 $r = $this->_getnslist($tns . '/' . $v['id']); 175 foreach ($r as $vv) { 176 if (!in_array(strtolower($vv), $exclude)) { 177 array_push($data2, $v['id'] . ':' . $vv); 178 } 179 } 180 } 181 } 182 } 183 return $data2; 184 } 185 186} 187