dolibarr 21.0.0-beta
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * or see https://www.gnu.org/
20 */
21
33{
37 public $tool; // Store the selected tool
38
39 // If using fckeditor
43 public $editor;
44
45 // If not using fckeditor
49 public $content;
53 public $htmlname;
57 public $toolbarname;
61 public $toolbarstartexpanded;
65 public $rows;
69 public $cols;
73 public $height;
77 public $width;
81 public $uselocalbrowser;
85 public $readonly;
89 public $posx;
93 public $posy;
94
95
115 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())
116 {
117 global $conf;
118
119 dol_syslog(get_class($this)."::DolEditor htmlname=".$htmlname." width=".$width." height=".$height." toolbarname=".$toolbarname." uselocalbrowser=".$uselocalbrowser);
120
121 if ($uselocalbrowser === -1) {
122 // This may not be supported by new generation of WYSIWYG editors.
123 $uselocalbrowser = getDolGlobalInt("WYSIWYG_ALLOW_UPLOAD_MEDIA_FILES");
124 }
125
126 if (!$rows) {
127 $rows = round($height / 20);
128 }
129 if (!$cols) {
130 $cols = ($width ? round($width / 6) : 80);
131 }
132 $shorttoolbarname = preg_replace('/_encoded$/', '', $toolbarname);
133
134 // Name of extended editor to use (FCKEDITOR_EDITORNAME can be 'ckeditor' or 'fckeditor')
135 $defaulteditor = 'ckeditor';
136 $this->tool = !getDolGlobalString('FCKEDITOR_EDITORNAME') ? $defaulteditor : $conf->global->FCKEDITOR_EDITORNAME;
137 $this->uselocalbrowser = $uselocalbrowser;
138 $this->readonly = $readonly;
139
140 // Check if extended editor is ok. If not we force textarea
141 if ((!isModEnabled('fckeditor') && $okforextendededitor !== 'ace') || empty($okforextendededitor)) {
142 $this->tool = 'textarea';
143 }
144 if ($okforextendededitor === 'ace') {
145 $this->tool = 'ace';
146 }
147 //if ($conf->dol_use_jmobile) $this->tool = 'textarea'; // ckeditor and ace seems ok with mobile
148 if (empty($conf->use_javascript_ajax)) { // If no javascript, we force use of textarea
149 $this->tool = 'textarea';
150 }
151
152 if (isset($poscursor['find'])) {
153 $posy = 0;
154 $lines = explode("\n", $content);
155 $nblines = count($lines);
156 for ($i = 0 ; $i < $nblines ; $i++) {
157 if (preg_match('/'.$poscursor['find'].'/', $lines[$i])) {
158 $posy = $i;
159 break;
160 }
161 }
162 if ($posy != 0) {
163 $poscursor['y'] = $posy;
164 }
165 }
166
167 // Define some properties
168 if (in_array($this->tool, array('textarea', 'ckeditor', 'ace'))) {
169 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.
170 $this->content = dol_nl2br($content);
171 } else {
172 $this->content = $content;
173 }
174 $this->htmlname = $htmlname;
175 $this->toolbarname = $shorttoolbarname;
176 $this->toolbarstartexpanded = $toolbarstartexpanded;
177 $this->rows = max(ROWS_3, $rows);
178 $this->cols = (preg_match('/%/', $cols) ? $cols : max(40, $cols)); // If $cols is a percent, we keep it, otherwise, we take max
179 $this->height = $height;
180 $this->width = $width;
181 $this->posx = empty($poscursor['x']) ? 0 : $poscursor['x'];
182 $this->posy = empty($poscursor['y']) ? 0 : $poscursor['y'];
183 }
184 }
185
186 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
200 public function Create($noprint = 0, $morejs = '', $disallowAnyContent = true, $titlecontent = '', $option = '', $moreparam = '', $morecss = '')
201 {
202 // phpcs:enable
203 global $conf, $langs;
204
205 $fullpage = false;
206 if (isset($conf->global->FCKEDITOR_ALLOW_ANY_CONTENT)) {
207 $disallowAnyContent = !getDolGlobalString('FCKEDITOR_ALLOW_ANY_CONTENT'); // Only predefined list of html tags are allowed or all
208 }
209
210 $found = 0;
211 $out = '';
212
213 $this->content = ($this->content ?? ''); // to avoid htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated
214
215 if (in_array($this->tool, array('textarea', 'ckeditor'))) {
216 $found = 1;
217 //$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">';
218 // TODO We do not put the 'disabled' tag because on a read form, it change style with grey.
219 //print $this->content;
220 $out .= '<textarea id="'.$this->htmlname.'" name="'.$this->htmlname.'"';
221 $out .= ' rows="'.$this->rows.'"';
222 //$out .= ' style="height: 700px; min-height: 700px;"';
223 $out .= (preg_match('/%/', $this->cols) ? ' style="margin-top: 5px; width: '.$this->cols.'"' : ' cols="'.$this->cols.'"');
224 $out .= ' '.($moreparam ? $moreparam : '');
225 $out .= ' class="flat '.$morecss.'">';
226 $out .= htmlspecialchars($this->content);
227 $out .= '</textarea>';
228
229 if ($this->tool == 'ckeditor' && !empty($conf->use_javascript_ajax) && isModEnabled('fckeditor')) {
230 if (!defined('REQUIRE_CKEDITOR')) {
231 define('REQUIRE_CKEDITOR', '1');
232 }
233
234 $skin = getDolGlobalString('FCKEDITOR_SKIN', 'moono-lisa'); // default with ckeditor 4.6 : moono-lisa
235
236 $pluginstodisable = 'elementspath,save,flash,div,anchor';
237 if (!getDolGlobalString('FCKEDITOR_ENABLE_SPECIALCHAR')) {
238 $pluginstodisable .= ',specialchar';
239 }
240 if (!empty($conf->dol_optimize_smallscreen)) {
241 $pluginstodisable .= ',scayt,wsc,find,undo';
242 }
243 if (!getDolGlobalString('FCKEDITOR_ENABLE_WSC')) { // spellchecker has end of life december 2021
244 $pluginstodisable .= ',wsc';
245 }
246 if (!getDolGlobalString('FCKEDITOR_ENABLE_PDF')) {
247 $pluginstodisable .= ',exportpdf';
248 }
249 if (getDolGlobalInt('MAIN_DISALLOW_URL_INTO_DESCRIPTIONS') == 2) {
250 $this->uselocalbrowser = 0; // Can't use browser to navigate into files. Only links with "<img src=data:..." are allowed.
251 }
252 $scaytautostartup = '';
253 if (getDolGlobalString('FCKEDITOR_ENABLE_SCAYT_AUTOSTARTUP')) {
254 $scaytautostartup = 'scayt_autoStartup: true,';
255 $scaytautostartup .= 'scayt_sLang: \''.dol_escape_js($langs->getDefaultLang()).'\',';
256 } else {
257 $pluginstodisable .= ',scayt';
258 }
259
260 $htmlencode_force = preg_match('/_encoded$/', $this->toolbarname) ? 'true' : 'false';
261
262 $out .= '<!-- Output ckeditor disallowAnyContent='.dol_escape_htmltag((string) $disallowAnyContent).' toolbarname='.dol_escape_htmltag($this->toolbarname).' -->'."\n";
263 //$out .= '<style>#cke_1_top { height: 34px !important; }</style>';
264 $out .= '<script nonce="'.getNonce().'" type="text/javascript">
265 $(document).ready(function () {
266 /* console.log("Run ckeditor"); */
267 /* if (CKEDITOR.loadFullCore) CKEDITOR.loadFullCore(); */
268 /* should be editor=CKEDITOR.replace but what if there is several editors ? */
269 tmpeditor = CKEDITOR.replace(\''.dol_escape_js($this->htmlname).'\',
270 {
271 /* property:xxx is same than CKEDITOR.config.property = xxx */
272 customConfig: ckeditorConfig,
273 removePlugins: \''.dol_escape_js($pluginstodisable).'\',
274 versionCheck: false,
275 readOnly: '.($this->readonly ? 'true' : 'false').',
276 htmlEncodeOutput: '.dol_escape_js($htmlencode_force).',
277 allowedContent: '.($disallowAnyContent ? 'false' : 'true').', /* Advanced Content Filter (ACF) is on when allowedContent is false */
278 extraAllowedContent: \'a[target];section[contenteditable,id];div{float,display}\', /* Allow a tag with attribute target, allow seciont tag and allow the style float and display into div to default other allowed tags */
279 disallowedContent: \'\', /* Tags that are not allowed */
280 fullPage: '.($fullpage ? 'true' : 'false').', /* if true, the html, header and body tags are kept */
281 toolbar: \''.dol_escape_js($this->toolbarname).'\',
282 toolbarStartupExpanded: '.($this->toolbarstartexpanded ? 'true' : 'false').',
283 width: '.($this->width ? '\''.dol_escape_js($this->width).'\'' : '\'\'').',
284 height: '.dol_escape_js((string) $this->height).',
285 skin: \''.dol_escape_js($skin).'\',
286 '.$scaytautostartup.'
287 language: \''.dol_escape_js($langs->defaultlang).'\',
288 textDirection: \''.dol_escape_js($langs->trans("DIRECTION")).'\',
289 on : {
290 instanceReady : function(ev) {
291 console.log(\'ckeditor '.dol_escape_js($this->htmlname).' instanceReady\');
292
293 /* If we found the attribute required on source div, we remove it (not compatible with ckeditor) */
294 /* Disabled, because attribute required should never be used on fields for doleditor */
295 /* jQuery("#'.dol_escape_js($this->htmlname).'").attr("required", false); */
296
297 // Output paragraphs as <p>Text</p>.
298 this.dataProcessor.writer.setRules( \'p\', {
299 indent : false,
300 breakBeforeOpen : true,
301 breakAfterOpen : false,
302 breakBeforeClose : false,
303 breakAfterClose : true
304 });
305 },
306 /* This is to remove the tab Link on image popup. Does not work, so commented */
307 /* dialogDefinition: function (event) {
308 var dialogName = event.data.name;
309 var dialogDefinition = event.data.definition;
310 if (dialogName == \'image\') {
311 dialogDefinition.removeContents(\'Link\');
312 }
313 } */
314 },
315 disableNativeSpellChecker: '.(getDolGlobalString('CKEDITOR_NATIVE_SPELLCHECKER') ? 'false' : 'true');
316
317 if ($this->uselocalbrowser) {
318 $out .= ','."\n";
319 // To use filemanager with old fckeditor (GPL)
320 // Note: ckeditorFilebrowserBrowseUrl and ckeditorFilebrowserImageBrowseUrl are defined in header by main.inc.php. They include url to browser with url of upload connector in parameter
321 $out .= ' filebrowserBrowseUrl : ckeditorFilebrowserBrowseUrl,';
322 $out .= ' filebrowserImageBrowseUrl : ckeditorFilebrowserImageBrowseUrl,';
323 //$out.= ' filebrowserUploadUrl : \''.DOL_URL_ROOT.'/includes/fckeditor/editor/filemanagerdol/connectors/php/upload.php?Type=File\',';
324 //$out.= ' filebrowserImageUploadUrl : \''.DOL_URL_ROOT.'/includes/fckeditor/editor/filemanagerdol/connectors/php/upload.php?Type=Image\',';
325 $out .= "\n";
326 // To use filemanager with ckfinder (Non free) and ckfinder directory is inside htdocs/includes
327 /* $out.= ' filebrowserBrowseUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/ckfinder.html\',
328 filebrowserImageBrowseUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/ckfinder.html?Type=Images\',
329 filebrowserFlashBrowseUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/ckfinder.html?Type=Flash\',
330 filebrowserUploadUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files\',
331 filebrowserImageUploadUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images\',
332 filebrowserFlashUploadUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash\','."\n";
333 */
334 $out .= ' filebrowserWindowWidth : \'900\',
335 filebrowserWindowHeight : \'500\',
336 filebrowserImageWindowWidth : \'900\',
337 filebrowserImageWindowHeight : \'500\'';
338 }
339 $out .= ' })'.$morejs; // end CKEditor.replace
340 // Show the CKEditor javascript object once loaded is ready 'For debug)
341 //$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]); }); ';
342 $out .= '});'."\n"; // end document.ready
343 $out .= '</script>'."\n";
344 }
345 }
346
347 // Output editor ACE
348 // Warning: ace.js and ext-statusbar.js must be loaded by the parent page.
349 if (preg_match('/^ace/', $this->tool)) {
350 $found = 1;
351 $format = $option;
352
353 $out .= "\n".'<!-- Output Ace editor '.dol_string_nohtmltag($this->htmlname).' -->'."\n";
354
355 if ($titlecontent) {
356 $out .= '<div class="aceeditorstatusbar" id="statusBar'.$this->htmlname.'">'.$titlecontent;
357 $out .= ' &nbsp; - &nbsp; <span id="morelines" class="right classlink cursorpointer morelines'.$this->htmlname.'">'.dol_escape_htmltag($langs->trans("ShowMoreLines")).'</span> &nbsp; &nbsp; ';
358 $out .= '</div>';
359 $out .= '<script nonce="'.getNonce().'" type="text/javascript">'."\n";
360 $out .= 'jQuery(document).ready(function() {'."\n";
361 $out .= ' var aceEditor = window.ace.edit("'.dol_escape_all($this->htmlname).'aceeditorid");
362 aceEditor.moveCursorTo('.($this->posy + 1).','.$this->posx.');
363 aceEditor.gotoLine('.($this->posy + 1).','.$this->posx.');
364 var StatusBar = window.ace.require("ace/ext/statusbar").StatusBar; // Init status bar. Need lib ext-statusbar
365 var statusBar = new StatusBar(aceEditor, document.getElementById("statusBar'.dol_escape_all($this->htmlname).'")); // Init status bar. Need lib ext-statusbar
366
367 var oldNbOfLines = 0;
368 jQuery(".morelines'.dol_escape_all($this->htmlname).'").click(function() {
369 var aceEditorClicked = window.ace.edit("'.$this->htmlname.'aceeditorid");
370 currentline = aceEditorClicked.getOption("maxLines");
371 if (oldNbOfLines == 0)
372 {
373 oldNbOfLines = currentline;
374 }
375 console.log("We click on more lines, oldNbOfLines is "+oldNbOfLines+", we have currently "+currentline);
376 if (currentline < 500)
377 {
378 aceEditorClicked.setOptions({ maxLines: 500 });
379 }
380 else
381 {
382 aceEditorClicked.setOptions({ maxLines: oldNbOfLines });
383 }
384 });
385 })';
386 $out .= '</script>'."\n";
387 }
388
389 $out .= '<pre id="'.$this->htmlname.'aceeditorid" style="'.($this->width ? 'width: '.$this->width.'px; ' : '');
390 $out .= ($this->height ? ' height: '.$this->height.'px; ' : '');
391 //$out.=" min-height: 100px;";
392 $out .= '">';
393 $out .= htmlspecialchars($this->content);
394 $out .= '</pre>';
395 $out .= '<input type="hidden" id="'.$this->htmlname.'_x" name="'.$this->htmlname.'_x">';
396 $out .= '<input type="hidden" id="'.$this->htmlname.'_y" name="'.$this->htmlname.'_y">';
397 $out .= '<textarea id="'.$this->htmlname.'" name="'.$this->htmlname.'" style="width:0px; height: 0px; display: none;">';
398 $out .= htmlspecialchars($this->content);
399 $out .= '</textarea>';
400
401 $out .= '<script nonce="'.getNonce().'" type="text/javascript">'."\n";
402 $out .= 'var aceEditor = window.ace.edit("'.$this->htmlname.'aceeditorid");
403
404 aceEditor.session.setMode("ace/mode/'.$format.'");
405 aceEditor.setReadOnly('.($this->readonly ? 'true' : 'false').');
406 aceEditor.setOptions({
407 enableBasicAutocompletion: true, // the editor completes the statement when you hit Ctrl + Space. Need lib ext-language_tools.js
408 enableLiveAutocompletion: false, // the editor completes the statement while you are typing. Need lib ext-language_tools.js
409 //enableSnippets: true, // ???
410 showPrintMargin: false, // hides the vertical limiting strip
411 minLines: 10,
412 maxLines: '.(empty($this->height) ? '34' : (round($this->height / 10))).',
413 fontSize: "110%" // ensures that the editor fits in the environment
414 });
415
416 // defines the style of the editor
417 aceEditor.setTheme("ace/theme/chrome");
418
419 // hides line numbers (widens the area occupied by error and warning messages)
420 //aceEditor.renderer.setOption("showLineNumbers", false);
421 // ensures proper autocomplete, validation and highlighting of JavaScript code
422 //aceEditor.getSession().setMode("ace/mode/javascript_expression");'."\n";
423
424 // If page is PAGE_CONTENT to edit HTML web page in web site module, we add some autocompletion
425 if ($this->htmlname == 'PAGE_CONTENT') {
426 $out .= '
427 // Add custom function in the autocompletion
428 var customCompleter = {
429 getCompletions: function(editor, session, pos, prefix, callback) {
430 var wordList = [
431 { caption: \'dol_escape_all\', value: \'dol_escape_all(string)\', meta: \'custom\' },
432 { caption: \'dol_escape_js\', value: \'dol_escape_js(string)\', meta: \'custom\' },
433 { caption: \'includeContainer\', value: \'includeContainer(alias_of_container_to_include)\', meta: \'custom\' },
434 { caption: \'redirectToContainer\', value: \'redirectToContainer(alias_of_container_to_redirect_to)\', meta: \'custom\' },
435 { caption: \'getImageFromHtmlContent\', value: \'getImageFromHtmlContent(websitepage->htmlcontent)\', meta: \'custom\' },
436 ];
437 callback(null, wordList.map(function(word) {
438 return {
439 caption: word.caption,
440 value: word.value,
441 meta: word.meta
442 };
443 }));
444 }
445 };
446 aceEditor.completers = [customCompleter];
447 '."\n";
448 }
449
450 $out .= 'jQuery(document).ready(function() {';
451 $out .= ' jQuery(".buttonforacesave").click(function() {
452 console.log("We click on savefile button for component '.dol_escape_js($this->htmlname).'");
453 var aceEditor = window.ace.edit("'.dol_escape_js($this->htmlname).'aceeditorid");
454 if (aceEditor) {
455 var cursorPos = aceEditor.getCursorPosition();
456 //console.log(cursorPos);
457 if (cursorPos) {
458 jQuery("#'.dol_escape_js($this->htmlname).'_x").val(cursorPos.column);
459 jQuery("#'.dol_escape_js($this->htmlname).'_y").val(cursorPos.row);
460 }
461 //console.log(aceEditor.getSession().getValue());
462 // Inject content of editor into the original HTML field.
463 jQuery("#'.dol_escape_js($this->htmlname).'").val(aceEditor.getSession().getValue());
464 /*if (jQuery("#'.dol_escape_js($this->htmlname).'").html().length > 0) return true;
465 else return false;*/
466 return true;
467 } else {
468 console.log("Failed to retrieve js object ACE from its name");
469 return false;
470 }
471 });
472 })';
473 $out .= '</script>'."\n";
474 }
475
476 if (empty($found)) {
477 $out .= 'Error, unknown value for tool '.$this->tool.' in DolEditor Create function.';
478 }
479
480 if ($noprint) {
481 return $out;
482 } else {
483 print $out;
484 }
485 }
486}
Class to manage a WYSIWYG editor.
Create($noprint=0, $morejs='', $disallowAnyContent=true, $titlecontent='', $option='', $moreparam='', $morecss='')
Output edit area inside the HTML stream.
__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.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
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.
dol_escape_all($stringtoescape)
Returns text escaped for all protocols (so only alpha chars and numbers)
getDolGlobalString($key, $default='')
Return a 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...
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:149