18use Luracast\Restler\RestException;
20require_once DOL_DOCUMENT_ROOT.
'/website/class/website.class.php';
21require_once DOL_DOCUMENT_ROOT.
'/website/class/websitepage.class.php';
22require_once DOL_DOCUMENT_ROOT.
'/core/lib/website.lib.php';
23require_once DOL_DOCUMENT_ROOT.
'/core/lib/website2.lib.php';
57 $this->website =
new Website($this->db);
58 $this->websitepage =
new WebsitePage($this->db);
76 public function get(
$id)
78 return $this->_fetchWebsite(
$id);
96 public function getByRef($ref)
98 return $this->_fetchWebsite(0, $ref);
120 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
122 if (!DolibarrApiAccess::$user->hasRight(
'website',
'read')) {
123 throw new RestException(403);
128 $sql =
"SELECT t.rowid, t.ref";
129 $sql .=
" FROM ".$this->db->prefix().
"website as t";
130 $sql .=
" WHERE t.entity IN (".getEntity(
'website').
")";
137 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
141 $sqlTotals = str_replace(
'SELECT t.rowid, t.ref',
'SELECT count(t.rowid) as total', $sql);
143 $sql .= $this->db->order($sortfield, $sortorder);
148 $offset = $limit * $page;
149 $sql .= $this->db->plimit($limit + 1, $offset);
152 $result = $this->db->query($sql);
154 $num = $this->db->num_rows($result);
155 $min = min($num, ($limit <= 0 ? $num : $limit));
158 $obj = $this->db->fetch_object($result);
159 $website_static =
new Website($this->db);
160 if ($website_static->fetch($obj->rowid)) {
166 throw new RestException(503,
'Error when retrieve website list : '.$this->db->lasterror());
171 $totalsResult = $this->db->query($sqlTotals);
172 $total = $this->db->fetch_object($totalsResult)->total;
177 $obj_ret[
'data'] = $tmp;
178 $obj_ret[
'pagination'] = array(
179 'total' => (
int) $total,
181 'page_count' => (
int) ceil((
int) $total / $limit),
212 public function indexPages(
$id, $sortfield =
"t.pageurl", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
214 if (!DolibarrApiAccess::$user->hasRight(
'website',
'read')) {
215 throw new RestException(403);
218 $result = $this->website->fetch(
$id);
220 throw new RestException(404,
'Website not found');
224 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
229 $sql =
"SELECT t.rowid, t.pageurl";
230 $sql .=
" FROM ".$this->db->prefix().
"website_page as t";
231 $sql .=
" WHERE t.fk_website = ".((int)
$id);
238 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
242 $sqlTotals = str_replace(
'SELECT t.rowid, t.pageurl',
'SELECT count(t.rowid) as total', $sql);
244 $sql .= $this->db->order($sortfield, $sortorder);
249 $offset = $limit * $page;
250 $sql .= $this->db->plimit($limit + 1, $offset);
253 $result = $this->db->query($sql);
255 $num = $this->db->num_rows($result);
256 $min = min($num, ($limit <= 0 ? $num : $limit));
259 $obj = $this->db->fetch_object($result);
260 $page_static =
new WebsitePage($this->db);
261 if ($page_static->fetch($obj->rowid)) {
267 throw new RestException(503,
'Error when retrieve page list : '.$this->db->lasterror());
271 $totalsResult = $this->db->query($sqlTotals);
272 $total = $this->db->fetch_object($totalsResult)->total;
277 $obj_ret[
'data'] = $tmp;
278 $obj_ret[
'pagination'] = array(
279 'total' => (
int) $total,
281 'page_count' => (
int) ceil((
int) $total / $limit),
305 public function getPage(
$id, $pageid)
307 if (!DolibarrApiAccess::$user->hasRight(
'website',
'read')) {
308 throw new RestException(403);
311 $result = $this->website->fetch(
$id);
313 throw new RestException(404,
'Website not found');
317 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
320 $result = $this->websitepage->fetch($pageid);
322 throw new RestException(404,
'Page not found');
326 if ($this->websitepage->fk_website != $this->website->id) {
327 throw new RestException(404,
'Page not found for this website');
353 public function postPage(
$id, $request_data =
null)
355 if (!DolibarrApiAccess::$user->hasRight(
'website',
'write')) {
356 throw new RestException(403);
359 $result = $this->website->fetch(
$id);
361 throw new RestException(404,
'Website not found');
365 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
368 $this->websitepage =
new WebsitePage($this->db);
371 foreach ((array) $request_data as $field => $value) {
372 if ($field ==
'id' || $field ==
'rowid' || $field ==
'fk_website') {
375 if ($field ==
'image') {
378 if ($field ===
'caller') {
379 $this->websitepage->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
382 if ($field ==
'array_options' && is_array($value)) {
383 foreach ($value as $index => $val) {
388 $this->websitepage->$field = $this->
_checkValForAPI($field, $value, $this->websitepage);
391 $this->websitepage->fk_website = $this->website->id;
392 if (empty($this->websitepage->type_container)) {
393 $this->websitepage->type_container =
'page';
395 if (!isset($request_data[
'status'])) {
396 $this->websitepage->status = WebsitePage::STATUS_DRAFT;
398 if (is_null($this->websitepage->aliasalt)) {
399 $this->websitepage->aliasalt =
'';
401 $this->websitepage->pageurl = preg_replace(
'/\.php$/i',
'', trim((
string) $this->websitepage->pageurl));
402 $this->websitepage->ref = $this->websitepage->pageurl;
405 if (empty($this->websitepage->pageurl)) {
406 throw new RestException(400,
'Field pageurl is mandatory');
408 if (!preg_match(
'/^[a-z0-9\-\_]+$/i', $this->websitepage->pageurl) || preg_match(
'/^page\d+/i', $this->websitepage->pageurl)) {
409 throw new RestException(400,
'Field pageurl must contain only a-z, 0-9, - and _ characters and must not be pageX');
411 if (empty($this->websitepage->title)) {
412 throw new RestException(400,
'Field title is mandatory');
416 $websitepagetemp =
new WebsitePage($this->db);
417 $result = $websitepagetemp->fetch(0, (
string) $this->website->id, $this->websitepage->pageurl);
419 throw new RestException(500, $websitepagetemp->errorsToString());
422 throw new RestException(409,
'A page with this pageurl already exists in this website');
426 $phpfullcodestringold =
'';
428 if (
checkPHPCode($phpfullcodestringold, $phpfullcodestring)) {
429 throw new RestException(403,
'NotAllowedToAddDynamicContent');
433 $this->websitepage->content = preg_replace(
'/<head>.*<\/head>/ims',
'', (
string) $this->websitepage->content);
436 $phpwrappers = array();
437 if ($this->websitepage->type_container ==
'blogpost') {
438 $phpwrappers = $this->_addBlogpostWrappers($this->website, $this->websitepage);
442 $pageid = $this->websitepage->create(DolibarrApiAccess::$user, 0, $phpwrappers);
444 throw new RestException(500, $this->websitepage->errorsToString());
448 $this->_regeneratePageFiles($this->website, $this->websitepage);
451 if (empty($this->website->fk_default_home)) {
452 $this->website->fk_default_home = $pageid;
453 $result = $this->website->update(DolibarrApiAccess::$user);
455 global
$conf, $dolibarr_main_data_root;
457 $pathofwebsite = $dolibarr_main_data_root.($conf->entity > 1 ?
'/'.$conf->entity :
'').
'/website/'.$this->website->ref;
458 dolSaveIndexPage($pathofwebsite, $pathofwebsite.
'/index.php', $pathofwebsite.
'/page'.$pageid.
'.tpl.php', $pathofwebsite.
'/wrapper.php', $this->website);
460 dol_syslog(
"Failed to set the default home page of website ".$this->website->ref, LOG_WARNING);
488 public function putPage(
$id, $pageid, $request_data =
null)
490 if (!DolibarrApiAccess::$user->hasRight(
'website',
'write')) {
491 throw new RestException(403);
494 $result = $this->website->fetch(
$id);
496 throw new RestException(404,
'Website not found');
500 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
503 $result = $this->websitepage->fetch($pageid);
505 throw new RestException(404,
'Page not found');
509 if ($this->websitepage->fk_website != $this->website->id) {
510 throw new RestException(404,
'Page not found for this website');
517 foreach ($request_data as $field => $value) {
518 if ($field ==
'id' || $field ==
'rowid') {
521 if ($field ==
'image') {
524 if ($field ==
'fk_website') {
528 if ($field ===
'caller') {
529 $this->websitepage->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
532 if ($field ==
'array_options' && is_array($value)) {
533 foreach ($value as $index => $val) {
538 $this->websitepage->$field = $this->
_checkValForAPI($field, $value, $this->websitepage);
545 if (
checkPHPCode($phpfullcodestringold, $phpfullcodestring)) {
546 throw new RestException(403,
'NotAllowedToAddDynamicContent');
550 $this->websitepage->content = preg_replace(
'/<head>.*<\/head>/ims',
'', $this->websitepage->content);
553 $result = $this->websitepage->update(DolibarrApiAccess::$user);
555 throw new RestException(500, $this->websitepage->errorsToString());
559 $this->_regeneratePageFiles($this->website, $this->websitepage);
592 unset(
$object->barcode_type_coder);
595 unset(
$object->total_localtax1);
596 unset(
$object->total_localtax2);
600 unset(
$object->shipping_method_id);
602 unset(
$object->label_incoterms);
603 unset(
$object->location_incoterms);
604 unset(
$object->multicurrency_code);
605 unset(
$object->multicurrency_tx);
606 unset(
$object->multicurrency_total_ht);
607 unset(
$object->multicurrency_total_ttc);
608 unset(
$object->multicurrency_total_tva);
609 unset(
$object->multicurrency_total_localtax1);
610 unset(
$object->multicurrency_total_localtax2);
612 unset(
$object->mode_reglement_id);
613 unset(
$object->cond_reglement_id);
614 unset(
$object->demand_reason_id);
615 unset(
$object->transport_mode_id);
616 unset(
$object->shipping_method);
620 unset(
$object->actiontypecode);
623 unset(
$object->user_closing_id);
625 unset(
$object->totalpaid_multicurrency);
626 unset(
$object->cond_reglement_supplier_id);
627 unset(
$object->deposit_percent);
629 unset(
$object->array_languages);
631 unset(
$object->contacts_ids_internal);
632 unset(
$object->other_linked_objects);
633 unset(
$object->linkedObjectsIds);
640 unset(
$object->user_validation_id);
659 private function _fetchWebsite(
$id, $ref =
'')
661 if (empty(
$id) && empty($ref)) {
662 throw new RestException(400,
'bad value for parameter id or ref');
665 if (!DolibarrApiAccess::$user->hasRight(
'website',
'read')) {
666 throw new RestException(403);
669 $result = $this->website->fetch(
$id, $ref);
671 throw new RestException(404,
'Website not found');
675 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
689 private function _addBlogpostWrappers($website, $websitepage)
693 $pagelang = (string) (empty($websitepage->lang) ? $website->lang : $websitepage->lang);
695 $websitepagetemp =
new WebsitePage($this->db);
696 $containers = $websitepagetemp->fetchAll($website->id,
'',
'', 0, 0,
"((t.type_container:=:'header') OR (t.type_container:=:'footer'))");
697 if (!is_array($containers)) {
698 throw new RestException(500, $websitepagetemp->errorsToString());
702 $found = array(
'header' => array(),
'footer' => array());
703 foreach ($containers as $container) {
704 $containerlang = (string) (empty($container->lang) ? $website->lang : $container->lang);
705 if ($containerlang == $pagelang) {
706 $found[$container->type_container][] = $container->pageurl;
710 $phpwrappers = array();
712 $content = (string) $websitepage->content;
713 if (count($found[
'header']) == 1 && strpos($content,
"includeContainer('".$found[
'header'][0].
"')") ===
false) {
714 $wrapper =
"<?php includeContainer('".$found[
'header'][0].
"'); ?>\n";
715 $content = $wrapper.$content;
716 $phpwrappers[] = $wrapper;
718 if (count($found[
'footer']) == 1 && strpos($content,
"includeContainer('".$found[
'footer'][0].
"')") ===
false) {
719 $wrapper =
"\n<?php includeContainer('".$found[
'footer'][0].
"'); ?>\n";
720 $content .= $wrapper;
721 $phpwrappers[] = $wrapper;
724 $websitepage->content = $content;
738 private function _regeneratePageFiles($website, $websitepage)
741 global $dolibarr_main_data_root;
743 $pathofwebsite = $dolibarr_main_data_root.($conf->entity > 1 ?
'/'.$conf->entity :
'').
'/website/'.$website->ref;
745 $filemaster = $pathofwebsite.
'/master.inc.php';
746 $filealias = $pathofwebsite.
'/'.$websitepage->pageurl.
'.php';
747 $filetpl = $pathofwebsite.
'/page'.$websitepage->id.
'.tpl.php';
754 dol_syslog(
"Failed to write the master file ".$filemaster, LOG_WARNING);
760 dol_syslog(
"Failed to write the alias file ".basename($filealias), LOG_WARNING);
766 dol_syslog(
"Failed to write the tpl file ".$filetpl, LOG_WARNING);
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
__construct($db, $cachedir='', $refreshCache=false)
Constructor.
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_cleanObjectDatas($object)
Clean sensitive object data @phpstan-template T.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
dolKeepOnlyPhpCode($str)
Keep only PHP code part from a HTML string page.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
dolSaveMasterFile($filemaster)
Save content of a page on disk.
checkPHPCode(&$phpfullcodestringold, &$phpfullcodestring)
Check that the new string $phpfullcodestring contains only php code (including <php tag)
dolSaveIndexPage($pathofwebsite, $fileindex, $filetpl, $filewrapper, $object=null)
Save content of the index.php and/or the wrapper.php page.
dolSavePageAlias($filealias, $object, $objectpage)
Save an alias page on disk (A page that include the reference page).
dolSavePageContent($filetpl, Website $object, WebsitePage $objectpage, $backupold=0)
Save content of a page on disk (page name is generally ID_of_page.php).