* Copyright (C) 2021 Gaëtan MAISON * Copyright (C) 2024 MDW * Copyright (C) 2024 Frédéric France * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * or see https://www.gnu.org/ */ /** * \file htdocs/core/class/doleditor.class.php * \brief Class to manage a WYSIWYG editor */ /** * Class to manage a WYSIWYG editor. * Usage: $doleditor=new DolEditor('body',$message,320,'toolbar_mailing'); * $doleditor->Create(); */ class DolEditor { public $tool; // Store the selected tool // If using fckeditor public $editor; // If not using fckeditor public $content; public $htmlname; public $toolbarname; public $toolbarstartexpanded; public $rows; public $cols; public $height; public $width; public $uselocalbrowser; public $readonly; public $posx; public $posy; /** * Create an object to build an HTML area to edit a large string content * * @param string $htmlname HTML name of WYSIWYG field * @param string $content Content of WYSIWYG field * @param int|string $width Width in pixel of edit area (auto by default) * @param int $height Height in pixel of edit area (200px by default) * @param string $toolbarname Name of bar set to use ('Full', 'dolibarr_notes[_encoded]', 'dolibarr_details[_encoded]'=the less featured, 'dolibarr_mailings[_encoded]', 'dolibarr_readonly'). * @param string $toolbarlocation Deprecated. Not used * @param boolean $toolbarstartexpanded Bar is visible or not at start * @param boolean|int $uselocalbrowser Enabled to add links to local object with local browser. If false, only external images can be added in content. * @param boolean|int|string $okforextendededitor 1 or True=Allow usage of extended editor tool if qualified (like ckeditor). If 'textarea', force use of simple textarea. If 'ace', force use of Ace. * Warning: If you use 'ace', don't forget to also include ace.js in page header. Also, the button "save" must have class="buttonforacesave". * @param int $rows Size of rows for textarea tool * @param string $cols Size of cols for textarea tool (textarea number of cols '70' or percent 'x%') * @param int $readonly 0=Read/Edit, 1=Read only * @param array $poscursor Array for initial cursor position array('x'=>x, 'y'=>y). * array('find'=> 'word') can be used to go to line were the word has been found */ 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()) { global $conf; dol_syslog(get_class($this)."::DolEditor htmlname=".$htmlname." width=".$width." height=".$height." toolbarname=".$toolbarname); if (!$rows) { $rows = round($height / 20); } if (!$cols) { $cols = ($width ? round($width / 6) : 80); } $shorttoolbarname = preg_replace('/_encoded$/', '', $toolbarname); // Name of extended editor to use (FCKEDITOR_EDITORNAME can be 'ckeditor' or 'fckeditor') $defaulteditor = 'ckeditor'; $this->tool = !getDolGlobalString('FCKEDITOR_EDITORNAME') ? $defaulteditor : $conf->global->FCKEDITOR_EDITORNAME; $this->uselocalbrowser = $uselocalbrowser; $this->readonly = $readonly; // Check if extended editor is ok. If not we force textarea if ((!isModEnabled('fckeditor') && $okforextendededitor !== 'ace') || empty($okforextendededitor)) { $this->tool = 'textarea'; } if ($okforextendededitor === 'ace') { $this->tool = 'ace'; } //if ($conf->dol_use_jmobile) $this->tool = 'textarea'; // ckeditor and ace seems ok with mobile if (empty($conf->use_javascript_ajax)) { // If no javascript, we force use of textarea $this->tool = 'textarea'; } if ( isset($poscursor['find']) ) { $posy = 0; $lines = explode("\n", $content); $nblines = count($lines); for ($i = 0 ; $i < $nblines ; $i++) { if (preg_match('/'.$poscursor['find'].'/', $lines[$i])) { $posy = $i; break; } } if ($posy != 0 ) $poscursor['y'] = $posy; } // Define some properties if (in_array($this->tool, array('textarea', 'ckeditor', 'ace'))) { 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. $this->content = dol_nl2br($content); } else { $this->content = $content; } $this->htmlname = $htmlname; $this->toolbarname = $shorttoolbarname; $this->toolbarstartexpanded = $toolbarstartexpanded; $this->rows = max(ROWS_3, $rows); $this->cols = (preg_match('/%/', $cols) ? $cols : max(40, $cols)); // If $cols is a percent, we keep it, otherwise, we take max $this->height = $height; $this->width = $width; $this->posx = empty($poscursor['x']) ? 0 : $poscursor['x']; $this->posy = empty($poscursor['y']) ? 0 : $poscursor['y']; } } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Output edit area inside the HTML stream. * Output depends on this->tool (fckeditor, ckeditor, textarea, ...) * * @param int $noprint 1=Return HTML string instead of printing it to output * @param string $morejs Add more js. For example: ".on( \'saveSnapshot\', function(e) { alert(\'ee\'); });". Used by CKEditor only. * @param boolean $disallowAnyContent Disallow to use any content. true=restrict to a predefined list of allowed elements. Used by CKEditor only. * @param string $titlecontent Show title content before editor area. Used by ACE editor only. * @param string $option For ACE editor, set the source language ('html', 'php', 'javascript', 'json', ...) * @param string $moreparam Add extra tags to the textarea * @param string $morecss Add extra css to the textarea * @return void|string */ public function Create($noprint = 0, $morejs = '', $disallowAnyContent = true, $titlecontent = '', $option = '', $moreparam = '', $morecss = '') { // phpcs:enable global $conf, $langs; $fullpage = false; if (isset($conf->global->FCKEDITOR_ALLOW_ANY_CONTENT)) { $disallowAnyContent = !getDolGlobalString('FCKEDITOR_ALLOW_ANY_CONTENT'); // Only predefined list of html tags are allowed or all } $found = 0; $out = ''; if (in_array($this->tool, array('textarea', 'ckeditor'))) { $found = 1; //$out.= ''; if ($this->tool == 'ckeditor' && !empty($conf->use_javascript_ajax) && isModEnabled('fckeditor')) { if (!defined('REQUIRE_CKEDITOR')) { define('REQUIRE_CKEDITOR', '1'); } $skin = getDolGlobalString('FCKEDITOR_SKIN', 'moono-lisa'); // default with ckeditor 4.6 : moono-lisa $pluginstodisable = 'elementspath,save,flash,div,anchor'; if (!getDolGlobalString('FCKEDITOR_ENABLE_SPECIALCHAR')) { $pluginstodisable .= ',specialchar'; } if (!empty($conf->dol_optimize_smallscreen)) { $pluginstodisable .= ',scayt,wsc,find,undo'; } if (!getDolGlobalString('FCKEDITOR_ENABLE_WSC')) { // spellchecker has end of life december 2021 $pluginstodisable .= ',wsc'; } if (!getDolGlobalString('FCKEDITOR_ENABLE_PDF')) { $pluginstodisable .= ',exportpdf'; } if (getDolGlobalInt('MAIN_DISALLOW_URL_INTO_DESCRIPTIONS') == 2) { $this->uselocalbrowser = 0; // Can't use browser to navigate into files. Only links with "getDefaultLang()).'\','; } else { $pluginstodisable .= ',scayt'; } $htmlencode_force = preg_match('/_encoded$/', $this->toolbarname) ? 'true' : 'false'; $out .= ''."\n"; $out .= ''."\n"; } } // Output editor ACE // Warning: ace.js and ext-statusbar.js must be loaded by the parent page. if (preg_match('/^ace/', $this->tool)) { $found = 1; $format = $option; $out .= "\n".''."\n"; if ($titlecontent) { $out .= '
'.$titlecontent; $out .= '   -   '.dol_escape_htmltag($langs->trans("ShowMoreLines")).'     '; $out .= '
'; $out .= ''."\n"; } $out .= '
content);
			$out .= '
'; $out .= ''; $out .= ''; $out .= ''; $out .= ''."\n"; } if (empty($found)) { $out .= 'Error, unknown value for tool '.$this->tool.' in DolEditor Create function.'; } if ($noprint) { return $out; } else { print $out; } } }