doleditor.class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. /**
  20. * \file htdocs/core/class/doleditor.class.php
  21. * \brief Class to manage a WYSIWYG editor
  22. */
  23. /**
  24. * Class to manage a WYSIWYG editor.
  25. * Usage: $doleditor=new DolEditor('body',$message,320,'toolbar_mailing');
  26. * $doleditor->Create();
  27. */
  28. class DolEditor
  29. {
  30. public $tool; // Store the selected tool
  31. // If using fckeditor
  32. public $editor;
  33. // If not using fckeditor
  34. public $content;
  35. public $htmlname;
  36. public $toolbarname;
  37. public $toolbarstartexpanded;
  38. public $rows;
  39. public $cols;
  40. public $height;
  41. public $width;
  42. public $uselocalbrowser;
  43. public $readonly;
  44. public $posx;
  45. public $posy;
  46. /**
  47. * Create an object to build an HTML area to edit a large string content
  48. *
  49. * @param string $htmlname HTML name of WYSIWIG field
  50. * @param string $content Content of WYSIWIG field
  51. * @param int $width Width in pixel of edit area (auto by default)
  52. * @param int $height Height in pixel of edit area (200px by default)
  53. * @param string $toolbarname Name of bar set to use ('Full', 'dolibarr_notes[_encoded]', 'dolibarr_details[_encoded]'=the less featured, 'dolibarr_mailings[_encoded]', 'dolibarr_readonly').
  54. * @param string $toolbarlocation Where bar is stored :
  55. * 'In' = each window has its own toolbar
  56. * 'Out:name' = share toolbar into the div called 'name'
  57. * @param boolean $toolbarstartexpanded Bar is visible or not at start
  58. * @param boolean|int $uselocalbrowser Enabled to add links to local object with local browser. If false, only external images can be added in content.
  59. * @param boolean|string $okforextendededitor True=Allow usage of extended editor tool if qualified (like ckeditor). If 'textarea', force use of simple textarea. If 'ace', force use of Ace.
  60. * Warning: If you use 'ace', don't forget to also include ace.js in page header. Also, the button "save" must have class="buttonforacesave".
  61. * @param int $rows Size of rows for textarea tool
  62. * @param string $cols Size of cols for textarea tool (textarea number of cols '70' or percent 'x%')
  63. * @param int $readonly 0=Read/Edit, 1=Read only
  64. * @param array $poscursor Array for initial cursor position array('x'=>x, 'y'=>y)
  65. */
  66. public function __construct($htmlname, $content, $width = '', $height = 200, $toolbarname = 'Basic', $toolbarlocation = 'In', $toolbarstartexpanded = false, $uselocalbrowser = 1, $okforextendededitor = true, $rows = 0, $cols = 0, $readonly = 0, $poscursor = array())
  67. {
  68. global $conf, $langs;
  69. dol_syslog(get_class($this)."::DolEditor htmlname=".$htmlname." width=".$width." height=".$height." toolbarname=".$toolbarname);
  70. if (!$rows) {
  71. $rows = round($height / 20);
  72. }
  73. if (!$cols) {
  74. $cols = ($width ?round($width / 6) : 80);
  75. }
  76. $shorttoolbarname = preg_replace('/_encoded$/', '', $toolbarname);
  77. // Name of extended editor to use (FCKEDITOR_EDITORNAME can be 'ckeditor' or 'fckeditor')
  78. $defaulteditor = 'ckeditor';
  79. $this->tool = empty($conf->global->FCKEDITOR_EDITORNAME) ? $defaulteditor : $conf->global->FCKEDITOR_EDITORNAME;
  80. $this->uselocalbrowser = $uselocalbrowser;
  81. $this->readonly = $readonly;
  82. // Check if extended editor is ok. If not we force textarea
  83. if ((!isModEnabled('fckeditor') && $okforextendededitor != 'ace') || empty($okforextendededitor)) {
  84. $this->tool = 'textarea';
  85. }
  86. if ($okforextendededitor === 'ace') {
  87. $this->tool = 'ace';
  88. }
  89. //if ($conf->dol_use_jmobile) $this->tool = 'textarea'; // ckeditor and ace seems ok with mobile
  90. // Define some properties
  91. if (in_array($this->tool, array('textarea', 'ckeditor', 'ace'))) {
  92. 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.
  93. $this->content = dol_nl2br($content);
  94. } else {
  95. $this->content = $content;
  96. }
  97. $this->htmlname = $htmlname;
  98. $this->toolbarname = $shorttoolbarname;
  99. $this->toolbarstartexpanded = $toolbarstartexpanded;
  100. $this->rows = max(ROWS_3, $rows);
  101. $this->cols = (preg_match('/%/', $cols) ? $cols : max(40, $cols)); // If $cols is a percent, we keep it, otherwise, we take max
  102. $this->height = $height;
  103. $this->width = $width;
  104. $this->posx = empty($poscursor['x']) ? 0 : $poscursor['x'];
  105. $this->posy = empty($poscursor['y']) ? 0 : $poscursor['y'];
  106. }
  107. }
  108. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  109. /**
  110. * Output edit area inside the HTML stream.
  111. * Output depends on this->tool (fckeditor, ckeditor, textarea, ...)
  112. *
  113. * @param int $noprint 1=Return HTML string instead of printing it to output
  114. * @param string $morejs Add more js. For example: ".on( \'saveSnapshot\', function(e) { alert(\'ee\'); });". Used by CKEditor only.
  115. * @param boolean $disallowAnyContent Disallow to use any content. true=restrict to a predefined list of allowed elements. Used by CKEditor only.
  116. * @param string $titlecontent Show title content before editor area. Used by ACE editor only.
  117. * @param string $option For ACE editor, set the source language ('html', 'php', 'javascript', ...)
  118. * @param string $moreparam Add extra tags to the textarea
  119. * @param string $morecss Add extra css to the textarea
  120. * @return void|string
  121. */
  122. public function Create($noprint = 0, $morejs = '', $disallowAnyContent = true, $titlecontent = '', $option = '', $moreparam = '', $morecss = '')
  123. {
  124. // phpcs:enable
  125. global $conf, $langs;
  126. $fullpage = false;
  127. if (isset($conf->global->FCKEDITOR_ALLOW_ANY_CONTENT)) {
  128. $disallowAnyContent = empty($conf->global->FCKEDITOR_ALLOW_ANY_CONTENT); // Only predefined list of html tags are allowed or all
  129. }
  130. $found = 0;
  131. $out = '';
  132. if (in_array($this->tool, array('textarea', 'ckeditor'))) {
  133. $found = 1;
  134. //$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">';
  135. // TODO We do not put the 'disabled' tag because on a read form, it change style with grey.
  136. //print $this->content;
  137. $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.'">';
  138. $out .= htmlspecialchars($this->content);
  139. $out .= '</textarea>';
  140. if ($this->tool == 'ckeditor' && !empty($conf->use_javascript_ajax) && isModEnabled('fckeditor')) {
  141. if (!defined('REQUIRE_CKEDITOR')) {
  142. define('REQUIRE_CKEDITOR', '1');
  143. }
  144. $skin = getDolGlobalString('FCKEDITOR_SKIN', 'moono-lisa'); // default with ckeditor 4.6 : moono-lisa
  145. $pluginstodisable = 'elementspath,save,flash,div,specialchar,anchor';
  146. if (!empty($conf->dol_optimize_smallscreen)) {
  147. $pluginstodisable .= ',scayt,wsc,find,undo';
  148. }
  149. if (empty($conf->global->FCKEDITOR_ENABLE_WSC)) { // spellchecker has end of life december 2021
  150. $pluginstodisable .= ',wsc';
  151. }
  152. if (empty($conf->global->FCKEDITOR_ENABLE_PDF)) {
  153. $pluginstodisable .= ',exportpdf';
  154. }
  155. $scaytautostartup = '';
  156. if (!empty($conf->global->FCKEDITOR_ENABLE_SCAYT_AUTOSTARTUP)) {
  157. $scaytautostartup = 'scayt_autoStartup: true,';
  158. $scaytautostartup .= 'scayt_sLang: \''.dol_escape_js($langs->getDefaultLang()).'\',';
  159. } else {
  160. $pluginstodisable .= ',scayt';
  161. }
  162. $htmlencode_force = preg_match('/_encoded$/', $this->toolbarname) ? 'true' : 'false';
  163. $out .= '<!-- Output ckeditor $disallowAnyContent='.dol_escape_htmltag($disallowAnyContent).' toolbarname='.dol_escape_htmltag($this->toolbarname).' -->'."\n";
  164. $out .= '<script nonce="'.getNonce().'" type="text/javascript">
  165. $(document).ready(function () {
  166. /* console.log("Run ckeditor"); */
  167. /* if (CKEDITOR.loadFullCore) CKEDITOR.loadFullCore(); */
  168. /* should be editor=CKEDITOR.replace but what if there is several editors ? */
  169. tmpeditor = CKEDITOR.replace(\''.dol_escape_js($this->htmlname).'\',
  170. {
  171. /* property:xxx is same than CKEDITOR.config.property = xxx */
  172. customConfig: ckeditorConfig,
  173. removePlugins: \''.dol_escape_js($pluginstodisable).'\',
  174. readOnly: '.($this->readonly ? 'true' : 'false').',
  175. htmlEncodeOutput: '.dol_escape_js($htmlencode_force).',
  176. allowedContent: '.($disallowAnyContent ? 'false' : 'true').', /* Advanced Content Filter (ACF) is own when allowedContent is false */
  177. extraAllowedContent: \'a[target];div{float,display}\', /* Add the style float and display into div to default other allowed tags */
  178. disallowedContent: '.($disallowAnyContent ? '\'\'' : '\'\'').', /* Tags that are not allowed */
  179. fullPage: '.($fullpage ? 'true' : 'false').', /* if true, the html, header and body tags are kept */
  180. toolbar: \''.dol_escape_js($this->toolbarname).'\',
  181. toolbarStartupExpanded: '.($this->toolbarstartexpanded ? 'true' : 'false').',
  182. width: '.($this->width ? '\''.dol_escape_js($this->width).'\'' : '\'\'').',
  183. height: '.dol_escape_js($this->height).',
  184. skin: \''.dol_escape_js($skin).'\',
  185. '.$scaytautostartup.'
  186. language: \''.dol_escape_js($langs->defaultlang).'\',
  187. textDirection: \''.dol_escape_js($langs->trans("DIRECTION")).'\',
  188. on : {
  189. instanceReady : function( ev )
  190. {
  191. // Output paragraphs as <p>Text</p>.
  192. this.dataProcessor.writer.setRules( \'p\', {
  193. indent : false,
  194. breakBeforeOpen : true,
  195. breakAfterOpen : false,
  196. breakBeforeClose : false,
  197. breakAfterClose : true
  198. });
  199. }
  200. },
  201. disableNativeSpellChecker: '.(empty($conf->global->CKEDITOR_NATIVE_SPELLCHECKER) ? 'true' : 'false');
  202. if ($this->uselocalbrowser) {
  203. $out .= ','."\n";
  204. // To use filemanager with old fckeditor (GPL)
  205. $out .= ' filebrowserBrowseUrl : ckeditorFilebrowserBrowseUrl,';
  206. $out .= ' filebrowserImageBrowseUrl : ckeditorFilebrowserImageBrowseUrl,';
  207. //$out.= ' filebrowserUploadUrl : \''.DOL_URL_ROOT.'/includes/fckeditor/editor/filemanagerdol/connectors/php/upload.php?Type=File\',';
  208. //$out.= ' filebrowserImageUploadUrl : \''.DOL_URL_ROOT.'/includes/fckeditor/editor/filemanagerdol/connectors/php/upload.php?Type=Image\',';
  209. $out .= "\n";
  210. // To use filemanager with ckfinder (Non free) and ckfinder directory is inside htdocs/includes
  211. /* $out.= ' filebrowserBrowseUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/ckfinder.html\',
  212. filebrowserImageBrowseUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/ckfinder.html?Type=Images\',
  213. filebrowserFlashBrowseUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/ckfinder.html?Type=Flash\',
  214. filebrowserUploadUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files\',
  215. filebrowserImageUploadUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images\',
  216. filebrowserFlashUploadUrl : \''.DOL_URL_ROOT.'/includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash\','."\n";
  217. */
  218. $out .= ' filebrowserWindowWidth : \'900\',
  219. filebrowserWindowHeight : \'500\',
  220. filebrowserImageWindowWidth : \'900\',
  221. filebrowserImageWindowHeight : \'500\'';
  222. }
  223. $out .= ' })'.$morejs; // end CKEditor.replace
  224. // Show the CKEditor javascript object once loaded is ready 'For debug)
  225. //$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]); }); ';
  226. $out .= '});'."\n"; // end document.ready
  227. $out .= '</script>'."\n";
  228. }
  229. }
  230. // Output editor ACE
  231. // Warning: ace.js and ext-statusbar.js must be loaded by the parent page.
  232. if (preg_match('/^ace/', $this->tool)) {
  233. $found = 1;
  234. $format = $option;
  235. $out .= "\n".'<!-- Output Ace editor -->'."\n";
  236. if ($titlecontent) {
  237. $out .= '<div class="aceeditorstatusbar" id="statusBar'.$this->htmlname.'">'.$titlecontent;
  238. $out .= ' &nbsp; - &nbsp; <a id="morelines" href="#" class="right morelines'.$this->htmlname.' reposition">'.dol_escape_htmltag($langs->trans("ShowMoreLines")).'</a> &nbsp; &nbsp; ';
  239. $out .= '</div>';
  240. $out .= '<script nonce="'.getNonce().'" type="text/javascript">'."\n";
  241. $out .= 'jQuery(document).ready(function() {'."\n";
  242. $out .= ' var aceEditor = window.ace.edit("'.$this->htmlname.'aceeditorid");
  243. aceEditor.moveCursorTo('.($this->posy+1).','.$this->posx.');
  244. aceEditor.gotoLine('.($this->posy+1).','.$this->posx.');
  245. var StatusBar = window.ace.require("ace/ext/statusbar").StatusBar; // Init status bar. Need lib ext-statusbar
  246. var statusBar = new StatusBar(aceEditor, document.getElementById("statusBar'.$this->htmlname.'")); // Init status bar. Need lib ext-statusbar
  247. var oldNbOfLines = 0;
  248. jQuery(".morelines'.$this->htmlname.'").click(function() {
  249. var aceEditorClicked = window.ace.edit("'.$this->htmlname.'aceeditorid");
  250. currentline = aceEditorClicked.getOption("maxLines");
  251. if (oldNbOfLines == 0)
  252. {
  253. oldNbOfLines = currentline;
  254. }
  255. console.log("We click on more lines, oldNbOfLines is "+oldNbOfLines+", we have currently "+currentline);
  256. if (currentline < 500)
  257. {
  258. aceEditorClicked.setOptions({ maxLines: 500 });
  259. }
  260. else
  261. {
  262. aceEditorClicked.setOptions({ maxLines: oldNbOfLines });
  263. }
  264. });
  265. })';
  266. $out .= '</script>'."\n";
  267. }
  268. $out .= '<pre id="'.$this->htmlname.'aceeditorid" style="'.($this->width ? 'width: '.$this->width.'px; ' : '');
  269. $out .= ($this->height ? ' height: '.$this->height.'px; ' : '');
  270. //$out.=" min-height: 100px;";
  271. $out .= '">';
  272. $out .= htmlspecialchars($this->content);
  273. $out .= '</pre>';
  274. $out .= '<input type="hidden" id="'.$this->htmlname.'_x" name="'.$this->htmlname.'_x">';
  275. $out .= '<input type="hidden" id="'.$this->htmlname.'_y" name="'.$this->htmlname.'_y">';
  276. $out .= '<textarea id="'.$this->htmlname.'" name="'.$this->htmlname.'" style="width:0px; height: 0px; display: none;">';
  277. $out .= htmlspecialchars($this->content);
  278. $out .= '</textarea>';
  279. $out .= '<script nonce="'.getNonce().'" type="text/javascript">'."\n";
  280. $out .= 'var aceEditor = window.ace.edit("'.$this->htmlname.'aceeditorid");
  281. aceEditor.session.setMode("ace/mode/'.$format.'");
  282. aceEditor.setOptions({
  283. enableBasicAutocompletion: true, // the editor completes the statement when you hit Ctrl + Space. Need lib ext-language_tools.js
  284. enableLiveAutocompletion: false, // the editor completes the statement while you are typing. Need lib ext-language_tools.js
  285. showPrintMargin: false, // hides the vertical limiting strip
  286. minLines: 10,
  287. maxLines: '.(empty($this->height) ? '34' : (round($this->height / 10))).',
  288. fontSize: "110%" // ensures that the editor fits in the environment
  289. });
  290. // defines the style of the editor
  291. aceEditor.setTheme("ace/theme/chrome");
  292. // hides line numbers (widens the area occupied by error and warning messages)
  293. //aceEditor.renderer.setOption("showLineNumbers", false);
  294. // ensures proper autocomplete, validation and highlighting of JavaScript code
  295. //aceEditor.getSession().setMode("ace/mode/javascript_expression");
  296. '."\n";
  297. $out .= 'jQuery(document).ready(function() {
  298. jQuery(".buttonforacesave").click(function() {
  299. console.log("We click on savefile button for component '.dol_escape_js($this->htmlname).'");
  300. var aceEditor = window.ace.edit("'.dol_escape_js($this->htmlname).'aceeditorid");
  301. if (aceEditor) {
  302. var cursorPos = aceEditor.getCursorPosition();
  303. //console.log(cursorPos);
  304. if (cursorPos) {
  305. jQuery("#'.dol_escape_js($this->htmlname).'_x").val(cursorPos.column);
  306. jQuery("#'.dol_escape_js($this->htmlname).'_y").val(cursorPos.row);
  307. }
  308. //console.log(aceEditor.getSession().getValue());
  309. // Inject content of editor into the original HTML field.
  310. jQuery("#'.dol_escape_js($this->htmlname).'").val(aceEditor.getSession().getValue());
  311. /*if (jQuery("#'.dol_escape_js($this->htmlname).'").html().length > 0) return true;
  312. else return false;*/
  313. return true;
  314. } else {
  315. console.log("Failed to retrieve js object ACE from its name");
  316. return false;
  317. }
  318. });
  319. })';
  320. $out .= '</script>'."\n";
  321. }
  322. if (empty($found)) {
  323. $out .= 'Error, unknown value for tool '.$this->tool.' in DolEditor Create function.';
  324. }
  325. if ($noprint) {
  326. return $out;
  327. } else {
  328. print $out;
  329. }
  330. }
  331. }