1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Saito - The Threaded Web Forum |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
9
|
|
|
* @link https://github.com/Schlaefer/Saito |
10
|
|
|
* @license http://opensource.org/licenses/MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace App\View\Helper; |
14
|
|
|
|
15
|
|
|
use Cake\View\Helper\FormHelper; |
16
|
|
|
use Cake\View\Helper\HtmlHelper; |
17
|
|
|
use Geshi\View\Helper\GeshiHelper; |
18
|
|
|
use SaitoHelp\View\Helper\SaitoHelpHelper; |
19
|
|
|
use Saito\App\Registry; |
20
|
|
|
use Saito\Markup\MarkupInterface; |
21
|
|
|
use Stopwatch\Lib\Stopwatch; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Parser Helper |
25
|
|
|
* |
26
|
|
|
* @property GeshiHelper $Geshi |
27
|
|
|
* @property FormHelper $Form |
28
|
|
|
* @property HtmlHelper $Html |
29
|
|
|
* @property SaitoHelpHelper $SaitoHelp |
30
|
|
|
*/ |
31
|
|
|
class ParserHelper extends AppHelper |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array these Helpers are also used in the Parser |
36
|
|
|
*/ |
37
|
|
|
public $helpers = [ |
38
|
|
|
'MailObfuscator.MailObfuscator', |
39
|
|
|
'Geshi.Geshi', |
40
|
|
|
'Form', |
41
|
|
|
'Html', |
42
|
|
|
'Text', |
43
|
|
|
'Url', |
44
|
|
|
//= usefull in Parsers |
45
|
|
|
'Layout', |
46
|
|
|
'SaitoHelp' |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array parserCache for parsed markup |
51
|
|
|
* |
52
|
|
|
* Esp. useful for repeating signatures in long mix view threads |
53
|
|
|
*/ |
54
|
|
|
protected $_parserCache = []; |
55
|
|
|
|
56
|
|
|
/** @var MarkupInterface */ |
57
|
|
|
protected $Markup; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritDoc} |
61
|
|
|
*/ |
62
|
|
|
public function initialize(array $config) |
63
|
|
|
{ |
64
|
|
|
parent::initialize($config); |
65
|
|
|
/** @var MarkupInterface */ |
66
|
|
|
$Markup = Registry::get('Markup'); |
67
|
|
|
$this->Markup = $Markup; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritDoc} |
72
|
|
|
*/ |
73
|
|
|
public function beforeRender($viewFile) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
if (isset($this->request) && $this->request->getParam('action') === 'preview') { |
|
|
|
|
76
|
|
|
$this->Geshi->showPlainTextButton = false; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* cite text |
82
|
|
|
* |
83
|
|
|
* @param string $string string |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function citeText($string) |
87
|
|
|
{ |
88
|
|
|
return $this->Markup->citeText($string); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* get editor help |
93
|
|
|
* |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
|
public function editorHelp() |
97
|
|
|
{ |
98
|
|
|
return $this->Markup->getEditorHelp($this); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* get button set |
103
|
|
|
* |
104
|
|
|
* @return mixed |
105
|
|
|
*/ |
106
|
|
|
public function getButtonSet() |
107
|
|
|
{ |
108
|
|
|
$buttons = $this->Markup->getMarkupSet(); |
109
|
|
|
$smilies = $this->_View->get('smiliesData')->get(); |
110
|
|
|
|
111
|
|
|
if (!empty($smilies)) { |
112
|
|
|
$buttons[] = [ |
113
|
|
|
'type' => 'separator' |
114
|
|
|
]; |
115
|
|
|
$buttons[] = [ |
116
|
|
|
'name' => "<i class='fa fa-s-smile-o'></i>", |
117
|
|
|
'title' => __('Smilies'), |
118
|
|
|
'className' => 'btn-markup-Smilies', |
119
|
|
|
'type' => 'saito-smilies', |
120
|
|
|
'handler' => 'smilies', |
121
|
|
|
]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return compact('buttons', 'smilies'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* parse |
129
|
|
|
* |
130
|
|
|
* @param string $string string |
131
|
|
|
* @param array $options options |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function parse($string, array $options = []) |
135
|
|
|
{ |
136
|
|
|
Stopwatch::start('ParseHelper::parse()'); |
137
|
|
|
if (empty($string) || $string === 'n/t') { |
138
|
|
|
Stopwatch::stop('ParseHelper::parse()'); |
139
|
|
|
|
140
|
|
|
return $string; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$defaults = ['return' => 'html', 'embed' => true, 'multimedia' => true, 'wrap' => true]; |
144
|
|
|
$options += $defaults; |
145
|
|
|
|
146
|
|
|
$cacheId = md5(serialize($options) . $string); |
147
|
|
|
if (isset($this->_parserCache[$cacheId])) { |
148
|
|
|
$html = $this->_parserCache[$cacheId]; |
149
|
|
|
} else { |
150
|
|
|
$html = $this->Markup->parse($string, $this, $options); |
151
|
|
|
$this->_parserCache[$cacheId] = $html; |
152
|
|
|
} |
153
|
|
|
if ($options['return'] === 'html' && $options['wrap']) { |
154
|
|
|
$html = '<div class="richtext">' . $html . '</div>'; |
155
|
|
|
} |
156
|
|
|
Stopwatch::stop('ParseHelper::parse()'); |
157
|
|
|
|
158
|
|
|
return $html; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.