dolibarr 19.0.3
doleditor.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2006-2008 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2021 Gaƫtan MAISON <gm@ilad.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 * or see https://www.gnu.org/
18 */
19
31{
32 public $tool; // Store the selected tool
33
34 // If using fckeditor
35 public $editor;
36
37 // If not using fckeditor
38 public $content;
39 public $htmlname;
40 public $toolbarname;
41 public $toolbarstartexpanded;
42 public $rows;
43 public $cols;
44 public $height;
45 public $width;
46 public $uselocalbrowser;
47 public $readonly;
48 public $posx;
49 public $posy;
50
51
70 public function __construct($htmlname, $content, $width = '', $height = 200, $toolbarname = 'Basic', $toolbarlocation = 'In', $toolbarstartexpanded = false, $uselocalbrowser = 1, $okforextendededitor = true, $rows = 0, $cols = '', $readonly = 0, $poscursor = array())
71 {
72 global $conf;
73
74 dol_syslog(get_class($this)."::DolEditor htmlname=".$htmlname." width=".$width." height=".$height." toolbarname=".$toolbarname);
75
76 if (!$rows) {
77 $rows = round($height / 20);
78 }
79 if (!$cols) {
80 $cols = ($width ? round($width / 6) : 80);
81 }
82 $shorttoolbarname = preg_replace('/_encoded$/', '', $toolbarname);
83
84 // Name of extended editor to use (FCKEDITOR_EDITORNAME can be 'ckeditor' or 'fckeditor')
85 $defaulteditor = 'ckeditor';
86 $this->tool = !getDolGlobalString('FCKEDITOR_EDITORNAME') ? $defaulteditor : $conf->global->FCKEDITOR_EDITORNAME;
87 $this->uselocalbrowser = $uselocalbrowser;
88 $this->readonly = $readonly;
89
90 // Check if extended editor is ok. If not we force textarea
91 if ((!isModEnabled('fckeditor') && $okforextendededitor != 'ace') || empty($okforextendededitor)) {
92 $this->tool = 'textarea';
93 }
94 if ($okforextendededitor === 'ace') {
95 $this->tool = 'ace';
96 }
97 //if ($conf->dol_use_jmobile) $this->tool = 'textarea'; // ckeditor and ace seems ok with mobile
98
99 // Define some properties
100 if (in_array($this->tool, array('textarea', 'ckeditor', 'ace'))) {
101 if ($this->tool == 'ckeditor' && !dol_textishtml($content)) { // We force content to be into HTML if we are using an advanced editor if content is not HTML.
102 $this->content = dol_nl2br($content);
103 } else {
104 $this->content = $content;
105 }
106 $this->htmlname = $htmlname;
107 $this->toolbarname = $shorttoolbarname;
108 $this->toolbarstartexpanded = $toolbarstartexpanded;
109 $this->rows = max(ROWS_3, $rows);
110 $this->cols = (preg_match('/%/', $cols) ? $cols : max(40, $cols)); // If $cols is a percent, we keep it, otherwise, we take max
111 $this->height = $height;
112 $this->width = $width;
113 $this->posx = empty($poscursor['x']) ? 0 : $poscursor['x'];
114 $this->posy = empty($poscursor['y']) ? 0 : $poscursor['y'];
115 }
116 }
117
118 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
132 public function Create($noprint = 0, $morejs = '', $disallowAnyContent = true, $titlecontent = '', $option = '', $moreparam = '', $morecss = '')
133 {
134 // phpcs:enable
135 global $conf, $langs;
136
137 $fullpage = false;
138 if (isset($conf->global->FCKEDITOR_ALLOW_ANY_CONTENT)) {
139 $disallowAnyContent = !getDolGlobalString('FCKEDITOR_ALLOW_ANY_CONTENT'); // Only predefined list of html tags are allowed or all
140 }
141
142 $found = 0;
143 $out = '';
144
145 if (in_array($this->tool, array('textarea', 'ckeditor'))) {
146 $found = 1;
147 //$out.= '<textarea id="'.$this->htmlname.'" name="'.$this->htmlname.'" '.($this->readonly?' disabled':'').' rows="'.$this->rows.'"'.(preg_match('/%/',$this->cols)?' style="margin-top: 5px; width: '.$this->cols.'"':' cols="'.$this->cols.'"').' class="flat">';
148 // TODO We do not put the 'disabled' tag because on a read form, it change style with grey.
149 //print $this->content;
150 $out .= '<textarea id="'.$this->htmlname.'" name="'.$this->htmlname.'" rows="'.$this->rows.'"'.(preg_match('/%/', $this->cols) ? ' style="margin-top: 5px; width: '.$this->cols.'"' : ' cols="'.$this->cols.'"').' '.($moreparam ? $moreparam : '').' class="flat '.$morecss.'">';
151 $out .= htmlspecialchars($this->content);
152 $out .= '</textarea>';
153
154 if ($this->tool == 'ckeditor' && !empty($conf->use_javascript_ajax) && isModEnabled('fckeditor')) {
155 if (!defined('REQUIRE_CKEDITOR')) {
156 define('REQUIRE_CKEDITOR', '1');
157 }
158
159 $skin = getDolGlobalString('FCKEDITOR_SKIN', 'moono-lisa'); // default with ckeditor 4.6 : moono-lisa
160
161 $pluginstodisable = 'elementspath,save,flash,div,anchor';
162 if (!getDolGlobalString('FCKEDITOR_ENABLE_SPECIALCHAR')) {
163 $pluginstodisable .= ',specialchar';
164 }
165 if (!empty($conf->dol_optimize_smallscreen)) {
166 $pluginstodisable .= ',scayt,wsc,find,undo';
167 }
168 if (!getDolGlobalString('FCKEDITOR_ENABLE_WSC')) { // spellchecker has end of life december 2021
169 $pluginstodisable .= ',wsc';
170 }
171 if (!getDolGlobalString('FCKEDITOR_ENABLE_PDF')) {
172 $pluginstodisable .= ',exportpdf';
173 }
174 if (getDolGlobalInt('MAIN_DISALLOW_URL_INTO_DESCRIPTIONS') == 2) {
175 $this->uselocalbrowser = 0; // Can't use browser to navigate into files. Only links with "<img src=data:..." are allowed.
176 }
177 $scaytautostartup = '';
178 if (getDolGlobalString('FCKEDITOR_ENABLE_SCAYT_AUTOSTARTUP')) {
179 $scaytautostartup = 'scayt_autoStartup: true,';
180 $scaytautostartup .= 'scayt_sLang: \''.dol_escape_js($langs->getDefaultLang()).'\',';
181 } else {
182 $pluginstodisable .= ',scayt';
183 }
184
185 $htmlencode_force = preg_match('/_encoded$/', $this->toolbarname) ? 'true' : 'false';
186
187 $out .= '<!-- Output ckeditor $disallowAnyContent='.dol_escape_htmltag($disallowAnyContent).' toolbarname='.dol_escape_htmltag($this->toolbarname).' -->'."\n";
188 $out .= '<script nonce="'.getNonce().'" type="text/javascript">
189 $(document).ready(function () {
190 /* console.log("Run ckeditor"); */
191 /* if (CKEDITOR.loadFullCore) CKEDITOR.loadFullCore(); */
192 /* should be editor=CKEDITOR.replace but what if there is several editors ? */
193 tmpeditor = CKEDITOR.replace(\''.dol_escape_js($this->htmlname).'\',
194 {
195 /* property:xxx is same than CKEDITOR.config.property = xxx */
196 customConfig: ckeditorConfig,
197 removePlugins: \''.dol_escape_js($pluginstodisable).'\',
198 versionCheck: false,
199 readOnly: '.($this->readonly ? 'true' : 'false').',
200 htmlEncodeOutput: '.dol_escape_js($htmlencode_force).',
201 allowedContent: '.($disallowAnyContent ? 'false' : 'true').', /* Advanced Content Filter (ACF) is own when allowedContent is false */
202 extraAllowedContent: \'a[target];div{float,display}\', /* Add the style float and display into div to default other allowed tags */
203 disallowedContent: '.($disallowAnyContent ? '\'\'' : '\'\'').', /* Tags that are not allowed */
204 fullPage: '.($fullpage ? 'true' : 'false').', /* if true, the html, header and body tags are kept */
205 toolbar: \''.dol_escape_js($this->toolbarname).'\',
206 toolbarStartupExpanded: '.($this->toolbarstartexpanded ? 'true' : 'false').',
207 width: '.($this->width ? '\''.dol_escape_js($this->width).'\'' : '\'\'').',
208 height: '.dol_escape_js($this->height).',
209 skin: \''.dol_escape_js($skin).'\',
210 '.$scaytautostartup.'
211 language: \''.dol_escape_js($langs->defaultlang).'\',
212 textDirection: \''.dol_escape_js($langs->trans("DIRECTION")).'\',
213 on : {
214 instanceReady : function(ev) {
215 console.log("ckeditor instanceReady");
216 // Output paragraphs as <p>Text</p>.
217 this.dataProcessor.writer.setRules( \'p\', {
218 indent : false,
219 breakBeforeOpen : true,
220 breakAfterOpen : false,
221 breakBeforeClose : false,
222 breakAfterClose : true
223 });
224 },
225 /* This is to remove the tab Link on image popup. Does not work, so commented */
226 /*
227 dialogDefinition: function (event) {
228 var dialogName = event.data.name;
229 var dialogDefinition = event.data.definition;
230 if (dialogName == \'image\') {
231 dialogDefinition.removeContents(\'Link\');
232 }
233 }
234 */
235 },
236 disableNativeSpellChecker: '.(!getDolGlobalString('CKEDITOR_NATIVE_SPELLCHECKER') ? 'true' : 'false');
237
238 if ($this->uselocalbrowser) {
239 $out .= ','."\n";
240 // To use filemanager with old fckeditor (GPL)
241 // Note: ckeditorFilebrowserBrowseUrl and ckeditorFilebrowserImageBrowseUrl are defined in header by main.inc.php. They include url to browser with url of upload connector in parameter
242 $out .= ' filebrowserBrowseUrl : ckeditorFilebrowserBrowseUrl,';
243 $out .= ' filebrowserImageBrowseUrl : ckeditorFilebrowserImageBrowseUrl,';
244 //$out.= ' filebrowserUploadUrl : \''.DOL_URL_ROOT.'/includes/fckeditor/editor/filemanagerdol/connectors/php/upload.php?Type=File\',';
245 //$out.= ' filebrowserImageUploadUrl : \''.DOL_URL_ROOT.'/includes/fckeditor/editor/filemanagerdol/connectors/php/upload.php?Type=Image\',';
246 $out .= "\n";
247 // To use filemanager with ckfinder (Non free) and ckfinder directory is inside htdocs/includes
248 /* $out.= ' filebrowserBrowseUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/ckfinder.html\',
249 filebrowserImageBrowseUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/ckfinder.html?Type=Images\',
250 filebrowserFlashBrowseUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/ckfinder.html?Type=Flash\',
251 filebrowserUploadUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files\',
252 filebrowserImageUploadUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images\',
253 filebrowserFlashUploadUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash\','."\n";
254 */
255 $out .= ' filebrowserWindowWidth : \'900\',
256 filebrowserWindowHeight : \'500\',
257 filebrowserImageWindowWidth : \'900\',
258 filebrowserImageWindowHeight : \'500\'';
259 }
260 $out .= ' })'.$morejs; // end CKEditor.replace
261 // Show the CKEditor javascript object once loaded is ready 'For debug)
262 //$out .= '; CKEDITOR.on(\'instanceReady\', function(ck) { ck.editor.removeMenuItem(\'maximize\'); ck.editor.removeMenuItem(\'Undo\'); ck.editor.removeMenuItem(\'undo\'); console.log(ck.editor); console.log(ck.editor.toolbar[0]); }); ';
263 $out .= '});'."\n"; // end document.ready
264 $out .= '</script>'."\n";
265 }
266 }
267
268 // Output editor ACE
269 // Warning: ace.js and ext-statusbar.js must be loaded by the parent page.
270 if (preg_match('/^ace/', $this->tool)) {
271 $found = 1;
272 $format = $option;
273
274 $out .= "\n".'<!-- Output Ace editor -->'."\n";
275
276 if ($titlecontent) {
277 $out .= '<div class="aceeditorstatusbar" id="statusBar'.$this->htmlname.'">'.$titlecontent;
278 $out .= ' &nbsp; - &nbsp; <span id="morelines" class="right classlink cursorpointer morelines'.$this->htmlname.'">'.dol_escape_htmltag($langs->trans("ShowMoreLines")).'</span> &nbsp; &nbsp; ';
279 $out .= '</div>';
280 $out .= '<script nonce="'.getNonce().'" type="text/javascript">'."\n";
281 $out .= 'jQuery(document).ready(function() {'."\n";
282 $out .= ' var aceEditor = window.ace.edit("'.$this->htmlname.'aceeditorid");
283 aceEditor.moveCursorTo('.($this->posy+1).','.$this->posx.');
284 aceEditor.gotoLine('.($this->posy+1).','.$this->posx.');
285 var StatusBar = window.ace.require("ace/ext/statusbar").StatusBar; // Init status bar. Need lib ext-statusbar
286 var statusBar = new StatusBar(aceEditor, document.getElementById("statusBar'.$this->htmlname.'")); // Init status bar. Need lib ext-statusbar
287
288 var oldNbOfLines = 0;
289 jQuery(".morelines'.$this->htmlname.'").click(function() {
290 var aceEditorClicked = window.ace.edit("'.$this->htmlname.'aceeditorid");
291 currentline = aceEditorClicked.getOption("maxLines");
292 if (oldNbOfLines == 0)
293 {
294 oldNbOfLines = currentline;
295 }
296 console.log("We click on more lines, oldNbOfLines is "+oldNbOfLines+", we have currently "+currentline);
297 if (currentline < 500)
298 {
299 aceEditorClicked.setOptions({ maxLines: 500 });
300 }
301 else
302 {
303 aceEditorClicked.setOptions({ maxLines: oldNbOfLines });
304 }
305 });
306 })';
307 $out .= '</script>'."\n";
308 }
309
310 $out .= '<pre id="'.$this->htmlname.'aceeditorid" style="'.($this->width ? 'width: '.$this->width.'px; ' : '');
311 $out .= ($this->height ? ' height: '.$this->height.'px; ' : '');
312 //$out.=" min-height: 100px;";
313 $out .= '">';
314 $out .= htmlspecialchars($this->content);
315 $out .= '</pre>';
316 $out .= '<input type="hidden" id="'.$this->htmlname.'_x" name="'.$this->htmlname.'_x">';
317 $out .= '<input type="hidden" id="'.$this->htmlname.'_y" name="'.$this->htmlname.'_y">';
318 $out .= '<textarea id="'.$this->htmlname.'" name="'.$this->htmlname.'" style="width:0px; height: 0px; display: none;">';
319 $out .= htmlspecialchars($this->content);
320 $out .= '</textarea>';
321
322 $out .= '<script nonce="'.getNonce().'" type="text/javascript">'."\n";
323 $out .= 'var aceEditor = window.ace.edit("'.$this->htmlname.'aceeditorid");
324
325 aceEditor.session.setMode("ace/mode/'.$format.'");
326 aceEditor.setOptions({
327 enableBasicAutocompletion: true, // the editor completes the statement when you hit Ctrl + Space. Need lib ext-language_tools.js
328 enableLiveAutocompletion: false, // the editor completes the statement while you are typing. Need lib ext-language_tools.js
329 showPrintMargin: false, // hides the vertical limiting strip
330 minLines: 10,
331 maxLines: '.(empty($this->height) ? '34' : (round($this->height / 10))).',
332 fontSize: "110%" // ensures that the editor fits in the environment
333 });
334
335 // defines the style of the editor
336 aceEditor.setTheme("ace/theme/chrome");
337 // hides line numbers (widens the area occupied by error and warning messages)
338 //aceEditor.renderer.setOption("showLineNumbers", false);
339 // ensures proper autocomplete, validation and highlighting of JavaScript code
340 //aceEditor.getSession().setMode("ace/mode/javascript_expression");
341 '."\n";
342
343 $out .= 'jQuery(document).ready(function() {
344 jQuery(".buttonforacesave").click(function() {
345 console.log("We click on savefile button for component '.dol_escape_js($this->htmlname).'");
346 var aceEditor = window.ace.edit("'.dol_escape_js($this->htmlname).'aceeditorid");
347 if (aceEditor) {
348 var cursorPos = aceEditor.getCursorPosition();
349 //console.log(cursorPos);
350 if (cursorPos) {
351 jQuery("#'.dol_escape_js($this->htmlname).'_x").val(cursorPos.column);
352 jQuery("#'.dol_escape_js($this->htmlname).'_y").val(cursorPos.row);
353 }
354 //console.log(aceEditor.getSession().getValue());
355 // Inject content of editor into the original HTML field.
356 jQuery("#'.dol_escape_js($this->htmlname).'").val(aceEditor.getSession().getValue());
357 /*if (jQuery("#'.dol_escape_js($this->htmlname).'").html().length > 0) return true;
358 else return false;*/
359 return true;
360 } else {
361 console.log("Failed to retrieve js object ACE from its name");
362 return false;
363 }
364 });
365 })';
366 $out .= '</script>'."\n";
367 }
368
369 if (empty($found)) {
370 $out .= 'Error, unknown value for tool '.$this->tool.' in DolEditor Create function.';
371 }
372
373 if ($noprint) {
374 return $out;
375 } else {
376 print $out;
377 }
378 }
379}
Class to manage a WYSIWYG editor.
__construct($htmlname, $content, $width='', $height=200, $toolbarname='Basic', $toolbarlocation='In', $toolbarstartexpanded=false, $uselocalbrowser=1, $okforextendededitor=true, $rows=0, $cols='', $readonly=0, $poscursor=array())
Create an object to build an HTML area to edit a large string content.
Create($noprint=0, $morejs='', $disallowAnyContent=true, $titlecontent='', $option='', $moreparam='', $morecss='')
Output edit area inside the HTML stream.
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_escape_js($stringtoescape, $mode=0, $noescapebackslashn=0)
Returns text escaped for inclusion into javascript code.
dol_textishtml($msg, $option=0)
Return if a text is a html content.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:121