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)) { 102 @[$state, $phpsource] = $data; 103 $this->php($phpsource, $renderer); 104 105 return true; 106 } 107 } 108 109 return false; 110 } 111 112 /** 113 * Determine whether embedding PHP code is allowed 114 * 115 * @return bool true if executing embedded PHP code is allowed 116 */ 117 protected function allowEmbedding(): bool 118 { 119 $allow = ($this->getConf('embedphpok') == 1) && 120 ($this->getConf('privatewiki') == 1); 121 122 return $allow; 123 } 124 125 /** 126 * Execute PHP code if allowed 127 * 128 * @param string $phpsource PHP code that is either executed or printed 129 * @param Doku_Renderer $renderer Renderer used for output 130 */ 131 protected function php($phpsource, Doku_Renderer $renderer): void 132 { 133 if ($this->allowEmbedding()) { 134 ob_start(); 135 eval($phpsource); 136 $o = ob_get_contents(); 137 if (!empty($o)) { 138 if ($this->isBlockElement()) { 139 $renderer->doc .= '<div class="embeddedphp">'.$o.'</div>'; 140 } else { 141 $renderer->doc .= '<span class="embeddedphp">'.$o.'</span>'; 142 } 143 } 144 ob_end_clean(); 145 } else { 146 $wrapper = $this->isBlockElement() ? 'pre' : 'code'; 147 $renderer->doc .= /*'###.get_class($this)'.*/p_xhtml_cached_geshi($phpsource, 'php', $wrapper); 148 } 149 } 150 151 /** 152 * Generic test to differentiate between inline and block modes 153 * 154 * @return bool true if this generates a block element, false otherwise. 155 */ 156 protected function isBlockElement(): bool 157 { 158 return false; 159 } 160} 161 162