dolibarr 25.0.0-alpha
editinline.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2026 Joachim Kueter <git-jk@bloxera.com>
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 */
20
27if (!defined('NOTOKENRENEWAL')) {
28 define('NOTOKENRENEWAL', '1'); // Disables token renewal
29}
30if (!defined('NOREQUIREMENU')) {
31 define('NOREQUIREMENU', '1');
32}
33if (!defined('NOREQUIREAJAX')) {
34 define('NOREQUIREAJAX', '1');
35}
36if (!defined('NOREQUIRESOC')) {
37 define('NOREQUIRESOC', '1');
38}
39
40// Load Dolibarr environment
41require '../../main.inc.php';
49require_once DOL_DOCUMENT_ROOT.'/website/class/website.class.php';
50require_once DOL_DOCUMENT_ROOT.'/website/class/websitepage.class.php';
51require_once DOL_DOCUMENT_ROOT.'/core/lib/website.lib.php';
52require_once DOL_DOCUMENT_ROOT.'/core/lib/website2.lib.php';
53
54$action = GETPOST('action', 'alpha');
55$page_id = GETPOSTINT('page_id');
56$element_id = GETPOST('element_id', 'aZ09');
57// For action 'updateElementHtml' (element edited with CKEditor). Class attributes are kept, they are used by the templates.
58// The content is checked again with dolWebsiteCheckEditInlineHtml().
59$content = GETPOST('content', 'restricthtmlallowclass');
60// For action 'updateElementText' (element with only text inside, edited as plain text)
61$text = GETPOST('text', 'nohtml');
62
63global $dolibarr_main_data_root;
64
65$usercanmodify = $user->hasRight('website', 'write');
66if (!$usercanmodify) {
67 http_response_code(403);
68 print "You don't have permission for this action.";
69 exit;
70}
71
72
73/*
74 * View
75 */
76
77if ($action === 'updateElementText' || $action === 'updateElementHtml') {
78 top_httphead('application/json');
79
80 $langs->loadLangs(array("website", "errors"));
81
82 $error = '';
83 $oldcontent = '';
84 $newinner = '';
85 $element = null;
86 $objectpage = new WebsitePage($db);
87 $objectwebsite = new Website($db);
88
89 if (empty($element_id) || empty($page_id)) {
90 http_response_code(400);
91 $error = $langs->trans("ErrorBadParameters");
92 } elseif ($objectpage->fetch($page_id) <= 0 || $objectwebsite->fetch($objectpage->fk_website) <= 0) {
93 // Website::fetch() also checks the entity
94 http_response_code(404);
95 $error = $langs->trans("ErrorRecordNotFound");
96 } else {
97 // Find the element with contenteditable="true" and this id in the source of the page, with its position
98 $elements = dolWebsiteGetEditInlineElements($objectpage->content);
99 if (empty($elements[$element_id])) {
100 http_response_code(404);
101 $error = $langs->trans("ErrorEditInlineElementNotFound", $element_id);
102 } else {
103 $element = $elements[$element_id];
104 if ($element['error']) {
105 http_response_code(409);
106 $error = $langs->trans($element['error'], $element_id);
107 } elseif ($action === 'updateElementText' && $element['mode'] != 'text') {
108 http_response_code(409);
109 $error = $langs->trans("ErrorEditInlineElementNotPlainText", $element_id);
110 }
111 }
112 }
113
114 if (!$error) {
115 $oldinner = substr($objectpage->content, $element['pos'], $element['len']);
116
117 if ($action === 'updateElementText') {
118 $text = trim(str_replace("\r", '', $text));
119 $newinner = nl2br(htmlspecialchars($text, ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8'), false);
120 } else {
121 // The links of the preview are not the ones of the source, so we restore them
122 $newinner = dolWebsiteRestoreLinksOfEditInline($objectwebsite, trim(str_replace("\r", '', $content)), $oldinner);
123 if (!dolWebsiteCheckEditInlineHtml($newinner, $element['blocks'])) {
124 http_response_code(409);
125 $error = $langs->trans("ErrorEditInlineContentNotAllowed", $element_id);
126 } else {
127 // Keep the line feeds and indentation of the source around the content
128 preg_match('/^\s*/', $oldinner, $regspace);
129 preg_match('/\s*$/', $oldinner, $regspaceend);
130 $newinner = $regspace[0].$newinner.$regspaceend[0];
131 }
132 }
133 }
134
135 if (!$error) {
136 $oldcontent = $objectpage->content;
137 $objectpage->content = substr_replace($oldcontent, $newinner, $element['pos'], $element['len']);
138
139 // The element has no php code and the new content is checked, so this should never happen
140 $oldphp = dolKeepOnlyPhpCode($oldcontent);
141 $newphp = dolKeepOnlyPhpCode($objectpage->content);
142 if ($oldphp != $newphp) {
143 http_response_code(409);
144 $error = $langs->trans("ErrorEditInlineElementWithPhp", $element_id);
145 }
146 }
147
148 if (!$error && $objectpage->content != $oldcontent) {
149 $db->begin();
150
151 if ($objectpage->update($user) <= 0) {
152 $error = $objectpage->error;
153 } else {
154 $pathofwebsite = $dolibarr_main_data_root.($conf->entity > 1 ? '/'.$conf->entity : '').'/website/'.$objectwebsite->ref;
155 $filetpl = $pathofwebsite.'/page'.$objectpage->id.'.tpl.php';
156 if (!dolSavePageContent($filetpl, $objectwebsite, $objectpage, 1)) {
157 $error = $langs->trans("ErrorFailedToWriteInDir", $pathofwebsite);
158 }
159 }
160
161 if ($error) {
162 $db->rollback();
163 http_response_code(500);
164 } else {
165 $db->commit();
166 }
167 }
168
169 if ($error) {
170 print json_encode(array('error' => $error));
171 } elseif ($action === 'updateElementText') {
172 // Return the saved text, it may differ from the posted one (tags are removed)
173 print json_encode(array('status' => 'ok', 'message' => $langs->trans("Saved"), 'text' => $text));
174 } else {
175 print json_encode(array('status' => 'ok', 'message' => $langs->trans("Saved")));
176 }
177
178 $db->close();
179 exit;
180}
181
182http_response_code(400);
183print 'Bad value for parameter action';
Class Website.
dolWebsiteRestoreLinksOfEditInline($website, $html, $oldhtml)
Restore the links of a html content edited into the preview of a page (mode "Edit inline") as they ar...
dolWebsiteCheckEditInlineHtml($html, $blocks=1)
Check that a html content has only the tags and attributes that can be saved with the mode "Edit inli...
dolWebsiteGetEditInlineElements($content)
Return the elements with contenteditable="true" and an id found into the source of a page,...
dolKeepOnlyPhpCode($str)
Keep only PHP code part from a HTML string page.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
dolSavePageContent($filetpl, Website $object, WebsitePage $objectpage, $backupold=0)
Save content of a page on disk (page name is generally ID_of_page.php).