1<?php 2/** 3 * DokuWiki Plugin embeddedphp (Syntax Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author fiwswe <dwplugin@fwml.de> 7 */ 8 9class syntax_plugin_embeddedphp_phpinline extends \dokuwiki\Extension\SyntaxPlugin 10{ 11 /** 12 * Return the tag this plugin instance reacts to 13 * 14 * @return string 15 */ 16 protected function GetTag(): string 17 { 18 return 'php'; 19 } 20 21 /** 22 * Return the exit pattern as we need this more than once 23 * 24 * @return string 25 */ 26 protected function GetExitPattern(): string 27 { 28 return '</'.$this->GetTag().'>'; 29 } 30 31 /** @inheritDoc */ 32 public function getType() 33 { 34 return 'protected'; 35 } 36 37 /** @inheritDoc */ 38 public function getSort() 39 { 40 // The default <php>/<PHP> handler up to "Igor" has priority 180. By setting a 41 // lower priority we override the built-in functionality. 42 return 179; 43 } 44 45 46 /* 47 * Return the plugin Lexer mode 48 * This works fine for most trivial cases. But some plugins 49 * may need to override this method. 50 * 51 * @return string 52 */ 53 protected function getPluginModeName(): string 54 { 55 $x = ['plugin', 56 $this->getPluginName(), 57 $this->getPluginComponent()]; // If component is empty it will be filtered later. 58 59 return implode('_', array_filter($x)); 60 } 61 62 /** @inheritDoc */ 63 public function connectTo($mode) 64 { 65 $p = '<'.$this->GetTag().'\b>(?=.*?'.$this->GetExitPattern().')'; 66 $m = $this->getPluginModeName(); 67 $this->Lexer->addEntryPattern($p, $mode, $m); 68 } 69 70 /** @inheritDoc */ 71 public function postConnect() 72 { 73 $m = $this->getPluginModeName(); 74 $this->Lexer->addExitPattern($this->GetExitPattern(), $m); 75 } 76 77 /** @inheritDoc */ 78 public function handle($match, $state, $pos, Doku_Handler $handler) 79 { 80 global $INPUT; 81 82 // If we are parsing a submitted comment. Executing embedded PHP in comments is 83 // not a good idea! 84 if ($INPUT->has('comment')) { 85 return false; 86 } 87 88 switch($state) { 89 case DOKU_LEXER_UNMATCHED : 90 // Return the data needed in $this->render() as an array: 91 return [$state, $match]; 92 } 93 94 return false; 95 } 96 97 /** @inheritDoc */ 98 public function render($mode, Doku_Renderer $renderer, $data) 99 { 100 if ($mode === 'xhtml') { 101 if (is_array($data) && (count($data) > 1)) { 102 $this->php($data[1], $renderer); 103 104 return true; 105 } 106 } 107 108 return false; 109 } 110 111 /** 112 * Determine whether embedding PHP code is allowed 113 * 114 * @return bool true if executing embedded PHP code is allowed 115 */ 116 protected function allowEmbedding(): bool 117 { 118 $allow = ($this->getConf('embedphpok') == 1) && 119 ($this->getConf('privatewiki') == 1); 120 121 return $allow; 122 } 123 124 /** 125 * Execute PHP code if allowed 126 * 127 * @param string $text PHP code that is either executed or printed 128 * @param Doku_Renderer $renderer Renderer used for output 129 */ 130 protected function php($text, Doku_Renderer $renderer): void 131 { 132 if ($this->allowEmbedding()) { 133 ob_start(); 134 eval($text); 135 $o = ob_get_contents(); 136 if (!empty($o)) { 137 if ($this->isBlockElement()) { 138 $renderer->doc .= '<div class="embeddedphp">'.$o.'</div>'; 139 } else { 140 $renderer->doc .= '<span class="embeddedphp">'.$o.'</span>'; 141 } 142 } 143 ob_end_clean(); 144 } else { 145 $wrapper = $this->isBlockElement() ? 'pre' : 'code'; 146 $renderer->doc .= /*'###.get_class($this)'.*/p_xhtml_cached_geshi($text, 'php', $wrapper); 147 } 148 } 149 150 /** 151 * Generic test to differentiate between inline and block modes 152 * 153 * @return bool true if this generates a block element, false otherwise. 154 */ 155 protected function isBlockElement(): bool 156 { 157 return false; 158 } 159} 160 161