2017-02-24 21:13:40 +01:00
< ? php
/* Copyright ( C ) 2017 Laurent Destailleur < eldy @ users . sourceforge . net >
*
* 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
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2017-02-24 21:13:40 +01:00
*/
/**
* \file htdocs / core / lib / website . lib . php
* \ingroup website
* \brief Library for website module
*/
2019-08-15 18:07:36 +02:00
/**
* Remove PHP code part from a string .
*
* @ param string $str String to clean
* @ param string $replacewith String to use as replacement
* @ return string Result string without php code
* @ see dolKeepOnlyPhpCode ()
*/
function dolStripPhpCode ( $str , $replacewith = '' )
{
2022-02-24 18:15:04 +01:00
$str = str_replace ( '<?=' , '<?php' , $str );
2019-08-15 18:07:36 +02:00
$newstr = '' ;
//split on each opening tag
$parts = explode ( '<?php' , $str );
2021-02-23 22:03:23 +01:00
if ( ! empty ( $parts )) {
2020-03-12 12:45:44 +01:00
$i = 0 ;
2021-02-23 22:03:23 +01:00
foreach ( $parts as $part ) {
if ( $i == 0 ) { // The first part is never php code
2019-08-15 18:07:36 +02:00
$i ++ ;
$newstr .= $part ;
continue ;
}
// The second part is the php code. We split on closing tag
$partlings = explode ( '?>' , $part );
2021-02-23 22:03:23 +01:00
if ( ! empty ( $partlings )) {
2020-03-11 12:53:41 +01:00
//$phppart = $partlings[0];
2019-08-15 18:07:36 +02:00
//remove content before closing tag
2021-02-23 22:03:23 +01:00
if ( count ( $partlings ) > 1 ) {
$partlings [ 0 ] = '' ; // Todo why a count > 1 and not >= 1 ?
}
2019-08-15 18:07:36 +02:00
//append to out string
2020-03-11 12:53:41 +01:00
//$newstr .= '<span class="phptag" class="tooltip" title="'.dol_escape_htmltag(dolGetFirstLineOfText($phppart).'...').'">'.$replacewith.'<!-- '.$phppart.' --></span>'.implode('', $partlings);
//$newstr .= '<span>'.$replacewith.'<!-- '.$phppart.' --></span>'.implode('', $partlings);
$newstr .= '<span phptag>' . $replacewith . '</span>' . implode ( '' , $partlings );
//$newstr .= $replacewith.implode('', $partlings);
2019-08-15 18:07:36 +02:00
}
}
}
return $newstr ;
}
/**
* Keep only PHP code part from a HTML string page .
*
* @ param string $str String to clean
* @ return string Result string with php code only
* @ see dolStripPhpCode ()
*/
function dolKeepOnlyPhpCode ( $str )
{
2022-02-24 18:15:04 +01:00
$str = str_replace ( '<?=' , '<?php' , $str );
2019-08-15 18:07:36 +02:00
$newstr = '' ;
//split on each opening tag
$parts = explode ( '<?php' , $str );
2021-02-23 22:03:23 +01:00
if ( ! empty ( $parts )) {
2019-11-13 19:37:08 +01:00
$i = 0 ;
2021-02-23 22:03:23 +01:00
foreach ( $parts as $part ) {
if ( $i == 0 ) { // The first part is never php code
2019-08-15 18:07:36 +02:00
$i ++ ;
continue ;
}
2020-03-12 12:45:44 +01:00
$newstr .= '<?php' ;
2019-08-15 18:07:36 +02:00
//split on closing tag
$partlings = explode ( '?>' , $part , 2 );
2021-02-23 22:03:23 +01:00
if ( ! empty ( $partlings )) {
2019-08-15 18:07:36 +02:00
$newstr .= $partlings [ 0 ] . '?>' ;
2020-05-21 15:05:19 +02:00
} else {
2019-08-15 18:07:36 +02:00
$newstr .= $part . '?>' ;
}
}
}
return $newstr ;
}
2017-10-27 17:58:38 +02:00
/**
2019-08-14 19:48:52 +02:00
* Convert a page content to have correct links ( based on DOL_URL_ROOT ) into an html content . It replaces also dynamic content with '...php...'
2018-01-07 20:32:59 +01:00
* Used to ouput the page on the Preview from backoffice .
2017-10-27 17:58:38 +02:00
*
* @ param Website $website Web site object
* @ param string $content Content to replace
2017-12-10 20:34:35 +01:00
* @ param int $removephppart 0 = Replace PHP sections with a PHP badge . 1 = Remove completely PHP sections .
2019-08-13 06:52:07 +02:00
* @ param string $contenttype Content type
2019-08-14 23:27:34 +02:00
* @ param int $containerid Contenair id
2017-10-27 17:58:38 +02:00
* @ return boolean True if OK
2019-03-11 01:01:15 +01:00
* @ see dolWebsiteOutput () for function used to replace content in a web server context
2017-10-27 17:58:38 +02:00
*/
2019-08-15 19:03:30 +02:00
function dolWebsiteReplacementOfLinks ( $website , $content , $removephppart = 0 , $contenttype = 'html' , $containerid = '' )
2017-10-27 17:58:38 +02:00
{
2018-11-27 16:20:31 +01:00
$nbrep = 0 ;
2020-03-12 12:45:44 +01:00
dol_syslog ( 'dolWebsiteReplacementOfLinks start (contenttype=' . $contenttype . " containerid= " . $containerid . " USEDOLIBARREDITOR= " . ( defined ( 'USEDOLIBARREDITOR' ) ? '1' : '' ) . " USEDOLIBARRSERVER= " . ( defined ( 'USEDOLIBARRSERVER' ) ? '1' : '' ) . ')' , LOG_DEBUG );
2019-08-14 23:27:34 +02:00
//if ($contenttype == 'html') { print $content;exit; }
2019-08-13 06:37:06 +02:00
2017-10-27 17:58:38 +02:00
// Replace php code. Note $content may come from database and does not contains body tags.
2020-03-12 12:45:44 +01:00
$replacewith = '...php...' ;
2021-02-23 22:03:23 +01:00
if ( $removephppart ) {
$replacewith = '' ;
}
2017-12-10 20:34:35 +01:00
$content = preg_replace ( '/value="<\?php((?!\?>).)*\?>\n*/ims' , 'value="' . $replacewith . '"' , $content );
2017-11-05 03:57:26 +01:00
2020-03-12 12:45:44 +01:00
$replacewith = '"callto=#' ;
2021-02-23 22:03:23 +01:00
if ( $removephppart ) {
$replacewith = '' ;
}
2018-07-23 14:08:20 +02:00
$content = preg_replace ( '/"callto:<\?php((?!\?>).)*\?>\n*/ims' , $replacewith , $content );
2020-03-12 12:45:44 +01:00
$replacewith = '"mailto=#' ;
2021-02-23 22:03:23 +01:00
if ( $removephppart ) {
$replacewith = '' ;
}
2018-07-23 14:08:20 +02:00
$content = preg_replace ( '/"mailto:<\?php((?!\?>).)*\?>\n*/ims' , $replacewith , $content );
2020-03-12 12:45:44 +01:00
$replacewith = 'src="php' ;
2021-02-23 22:03:23 +01:00
if ( $removephppart ) {
$replacewith = '' ;
}
2018-07-24 01:10:38 +02:00
$content = preg_replace ( '/src="<\?php((?!\?>).)*\?>\n*/ims' , $replacewith , $content );
2020-03-12 12:45:44 +01:00
$replacewith = 'href="php' ;
2021-02-23 22:03:23 +01:00
if ( $removephppart ) {
$replacewith = '' ;
}
2018-09-26 15:20:37 +02:00
$content = preg_replace ( '/href="<\?php((?!\?>).)*\?>\n*/ims' , $replacewith , $content );
//$replacewith='<span class="phptag">...php...</span>';
2020-03-12 12:45:44 +01:00
$replacewith = '...php...' ;
2021-02-23 22:03:23 +01:00
if ( $removephppart ) {
$replacewith = '' ;
}
2018-11-27 16:20:31 +01:00
//$content = preg_replace('/<\?php((?!\?toremove>).)*\?toremove>\n*/ims', $replacewith, $content);
/* if ( $content === null ) {
if ( preg_last_error () == PREG_JIT_STACKLIMIT_ERROR ) $content = 'preg_replace error (when removing php tags) PREG_JIT_STACKLIMIT_ERROR' ;
} */
$content = dolStripPhpCode ( $content , $replacewith );
//var_dump($content);
2017-10-27 17:58:38 +02:00
2019-08-15 18:07:36 +02:00
// Protect the link styles.css.php to any replacement that we make after.
$content = str_replace ( 'href="styles.css.php' , 'href="!~!~!~styles.css.php' , $content );
2019-08-16 04:32:06 +02:00
$content = str_replace ( 'href="http' , 'href="!~!~!~http' , $content );
$content = str_replace ( 'href="//' , 'href="!~!~!~//' , $content );
2019-08-16 05:01:54 +02:00
$content = str_replace ( 'src="viewimage.php' , 'src="!~!~!~/viewimage.php' , $content );
2019-08-16 04:32:06 +02:00
$content = str_replace ( 'src="/viewimage.php' , 'src="!~!~!~/viewimage.php' , $content );
$content = str_replace ( 'src="' . DOL_URL_ROOT . '/viewimage.php' , 'src="!~!~!~' . DOL_URL_ROOT . '/viewimage.php' , $content );
2019-08-16 05:01:54 +02:00
$content = str_replace ( 'href="document.php' , 'href="!~!~!~/document.php' , $content );
$content = str_replace ( 'href="/document.php' , 'href="!~!~!~/document.php' , $content );
$content = str_replace ( 'href="' . DOL_URL_ROOT . '/document.php' , 'href="!~!~!~' . DOL_URL_ROOT . '/document.php' , $content );
2019-08-15 18:07:36 +02:00
2019-08-16 03:05:55 +02:00
// Replace relative link '/' with dolibarr URL
2020-03-09 18:54:34 +01:00
$content = preg_replace ( '/(href=")\/(#[^\"<>]*)?\"/' , '\1!~!~!~' . DOL_URL_ROOT . '/website/index.php?website=' . $website -> ref . '&pageid=' . $website -> fk_default_home . '\2"' , $content , - 1 , $nbrep );
2019-08-14 23:27:34 +02:00
// Replace relative link /xxx.php#aaa or /xxx.php with dolibarr URL (we discard param ?...)
2019-08-15 23:45:28 +02:00
$content = preg_replace ( '/(href=")\/?([^:\"\!]*)\.php(#[^\"<>]*)?\"/' , '\1!~!~!~' . DOL_URL_ROOT . '/website/index.php?website=' . $website -> ref . '&pageref=\2\3"' , $content , - 1 , $nbrep );
2019-08-15 18:07:36 +02:00
// Replace relative link /xxx.php?a=b&c=d#aaa or /xxx.php?a=b&c=d with dolibarr URL
2019-08-16 00:31:55 +02:00
$content = preg_replace ( '/(href=")\/?([^:\"\!]*)\.php\?([^#\"<>]*)(#[^\"<>]*)?\"/' , '\1!~!~!~' . DOL_URL_ROOT . '/website/index.php?website=' . $website -> ref . '&pageref=\2&\3\4"' , $content , - 1 , $nbrep );
2017-10-27 17:58:38 +02:00
2018-07-19 21:35:21 +02:00
// Fix relative link into medias with correct URL after the DOL_URL_ROOT: ../url("medias/
2020-03-02 17:00:51 +01:00
$content = preg_replace ( '/url\((["\']?)\/?medias\//' , 'url(\1!~!~!~' . DOL_URL_ROOT . '/viewimage.php?modulepart=medias&file=' , $content , - 1 , $nbrep );
2020-03-02 17:06:22 +01:00
$content = preg_replace ( '/data-slide-bg=(["\']?)\/?medias\//' , 'data-slide-bg=\1!~!~!~' . DOL_URL_ROOT . '/viewimage.php?modulepart=medias&file=' , $content , - 1 , $nbrep );
2017-10-27 17:58:38 +02:00
2019-08-16 03:05:55 +02:00
// <img src="medias/...image.png... => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png...
// <img src="...image.png... => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png...
2019-08-16 04:32:06 +02:00
$content = preg_replace ( '/(<img[^>]*src=")\/?medias\//' , '\1!~!~!~' . DOL_URL_ROOT . '/viewimage.php?modulepart=medias&file=' , $content , - 1 , $nbrep );
2017-10-27 17:58:38 +02:00
// <img src="image.png... => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png...
2019-08-16 04:32:06 +02:00
$content = preg_replace ( '/(<img[^>]*src=")\/?([^:\"\!]+)\"/' , '\1!~!~!~' . DOL_URL_ROOT . '/viewimage.php?modulepart=medias&file=\2"' , $content , - 1 , $nbrep );
2018-07-19 21:35:21 +02:00
// <img src="viewimage.php/modulepart=medias&file=image.png" => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png"
2019-08-15 18:07:36 +02:00
$content = preg_replace ( '/(<img[^>]*src=")(\/?viewimage\.php)/' , '\1!~!~!~' . DOL_URL_ROOT . '/viewimage.php' , $content , - 1 , $nbrep );
2017-10-27 17:58:38 +02:00
// action="newpage.php" => action="dolibarr/website/index.php?website=...&pageref=newpage
2019-08-15 18:07:36 +02:00
$content = preg_replace ( '/(action=")\/?([^:\"]*)(\.php\")/' , '\1!~!~!~' . DOL_URL_ROOT . '/website/index.php?website=' . $website -> ref . '&pageref=\2"' , $content , - 1 , $nbrep );
2017-10-27 17:58:38 +02:00
2018-07-19 16:06:06 +02:00
// Fix relative link /document.php with correct URL after the DOL_URL_ROOT: ...href="/document.php?modulepart="
2020-03-12 12:45:44 +01:00
$content = preg_replace ( '/(href=")(\/?document\.php\?[^\"]*modulepart=[^\"]*)(\")/' , '\1!~!~!~' . DOL_URL_ROOT . '\2\3' , $content , - 1 , $nbrep );
$content = preg_replace ( '/(src=")(\/?document\.php\?[^\"]*modulepart=[^\"]*)(\")/' , '\1!~!~!~' . DOL_URL_ROOT . '\2\3' , $content , - 1 , $nbrep );
2018-07-19 16:06:06 +02:00
2019-08-13 18:22:18 +02:00
// Fix relative link /viewimage.php with correct URL after the DOL_URL_ROOT: ...href="/viewimage.php?modulepart="
2020-03-12 12:45:44 +01:00
$content = preg_replace ( '/(url\(")(\/?viewimage\.php\?[^\"]*modulepart=[^\"]*)(\")/' , '\1!~!~!~' . DOL_URL_ROOT . '\2\3' , $content , - 1 , $nbrep );
2019-08-15 18:07:36 +02:00
2019-08-16 04:32:06 +02:00
// Fix relative URL
$content = str_replace ( 'src="!~!~!~/viewimage.php' , 'src="!~!~!~' . DOL_URL_ROOT . '/viewimage.php' , $content );
2019-08-16 05:01:54 +02:00
$content = str_replace ( 'href="!~!~!~/document.php' , 'href="!~!~!~' . DOL_URL_ROOT . '/document.php' , $content );
2019-08-15 18:07:36 +02:00
// Remove the protection tag !~!~!~
$content = str_replace ( '!~!~!~' , '' , $content );
2019-08-13 18:22:18 +02:00
2019-08-13 06:37:06 +02:00
dol_syslog ( 'dolWebsiteReplacementOfLinks end' , LOG_DEBUG );
2019-08-14 23:27:34 +02:00
//if ($contenttype == 'html') { print $content;exit; }
2019-08-13 06:37:06 +02:00
2017-10-27 17:58:38 +02:00
return $content ;
}
2017-02-24 21:13:40 +01:00
/**
* Render a string of an HTML content and output it .
2021-02-21 14:46:52 +01:00
* Used to ouput the page when viewed from a server ( Dolibarr or Apache ) .
2017-02-24 21:13:40 +01:00
*
2019-08-13 06:37:06 +02:00
* @ param string $content Content string
* @ param string $contenttype Content type
2019-08-13 07:08:45 +02:00
* @ param int $containerid Contenair id
2017-02-24 21:13:40 +01:00
* @ return void
2019-08-13 18:22:18 +02:00
* @ see dolWebsiteReplacementOfLinks () for function used to replace content in the backoffice context .
2017-02-24 21:13:40 +01:00
*/
2019-08-15 19:03:30 +02:00
function dolWebsiteOutput ( $content , $contenttype = 'html' , $containerid = '' )
2017-02-24 21:13:40 +01:00
{
2017-10-07 13:09:31 +02:00
global $db , $langs , $conf , $user ;
global $dolibarr_main_url_root , $dolibarr_main_data_root ;
2020-05-20 12:38:38 +02:00
global $website ;
2020-05-19 19:55:28 +02:00
global $includehtmlcontentopened ;
2017-02-24 21:13:40 +01:00
2020-04-17 00:42:01 +02:00
$nbrep = 0 ;
2020-05-19 19:55:28 +02:00
dol_syslog ( " dolWebsiteOutput start - contenttype= " . $contenttype . " containerid= " . $containerid . " USEDOLIBARREDITOR= " . ( defined ( 'USEDOLIBARREDITOR' ) ? '1' : '' ) . " USEDOLIBARRSERVER= " . ( defined ( 'USEDOLIBARRSERVER' ) ? '1' : '' ) . ' includehtmlcontentopened=' . $includehtmlcontentopened );
//print $containerid.' '.$content;
2017-07-03 02:09:14 +02:00
2017-10-07 13:09:31 +02:00
// Define $urlwithroot
2020-03-12 12:45:44 +01:00
$urlwithouturlroot = preg_replace ( '/' . preg_quote ( DOL_URL_ROOT , '/' ) . '$/i' , '' , trim ( $dolibarr_main_url_root ));
$urlwithroot = $urlwithouturlroot . DOL_URL_ROOT ; // This is to use external domain name found into config file
2017-10-07 13:09:31 +02:00
//$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
2017-07-21 02:30:02 +02:00
2021-02-23 22:03:23 +01:00
if ( defined ( 'USEDOLIBARREDITOR' )) { // REPLACEMENT OF LINKS When page called from Dolibarr editor
2018-09-25 15:00:37 +02:00
// We remove the <head> part of content
2021-02-23 22:03:23 +01:00
if ( $contenttype == 'html' ) {
2019-08-13 06:37:06 +02:00
$content = preg_replace ( '/<head>.*<\/head>/ims' , '' , $content );
$content = preg_replace ( '/^.*<body(\s[^>]*)*>/ims' , '' , $content );
$content = preg_replace ( '/<\/body(\s[^>]*)*>.*$/ims' , '' , $content );
}
2021-02-23 22:03:23 +01:00
} elseif ( defined ( 'USEDOLIBARRSERVER' )) { // REPLACEMENT OF LINKS When page called from Dolibarr server
2020-02-21 11:36:16 +01:00
$content = str_replace ( '<link rel="stylesheet" href="/styles.css' , '<link rel="stylesheet" href="styles.css' , $content );
2019-08-15 18:07:36 +02:00
// Protect the link styles.css.php to any replacement that we make after.
$content = str_replace ( 'href="styles.css.php' , 'href="!~!~!~styles.css.php' , $content );
2019-08-16 04:32:06 +02:00
$content = str_replace ( 'href="http' , 'href="!~!~!~http' , $content );
$content = str_replace ( 'href="//' , 'href="!~!~!~//' , $content );
2020-02-21 11:36:16 +01:00
$content = str_replace ( array ( 'src="viewimage.php' , 'src="/viewimage.php' ), 'src="!~!~!~/viewimage.php' , $content );
2019-08-16 04:32:06 +02:00
$content = str_replace ( 'src="' . DOL_URL_ROOT . '/viewimage.php' , 'src="!~!~!~' . DOL_URL_ROOT . '/viewimage.php' , $content );
2020-03-12 12:45:44 +01:00
$content = str_replace ( array ( 'href="document.php' , 'href="/document.php' ), 'href="!~!~!~/document.php' , $content );
2019-08-16 05:01:54 +02:00
$content = str_replace ( 'href="' . DOL_URL_ROOT . '/document.php' , 'href="!~!~!~' . DOL_URL_ROOT . '/document.php' , $content );
2019-08-15 18:07:36 +02:00
2017-10-27 17:58:38 +02:00
// Replace relative link / with dolibarr URL: ...href="/"...
2019-08-15 18:07:36 +02:00
$content = preg_replace ( '/(href=")\/\"/' , '\1!~!~!~' . DOL_URL_ROOT . '/public/website/index.php?website=' . $website -> ref . '"' , $content , - 1 , $nbrep );
// Replace relative link /xxx.php#aaa or /xxx.php with dolibarr URL: ...href="....php" (we discard param ?...)
$content = preg_replace ( '/(href=")\/?([^:\"\!]*)\.php(#[^\"<>]*)?\"/' , '\1!~!~!~' . DOL_URL_ROOT . '/public/website/index.php?website=' . $website -> ref . '&pageref=\2\3"' , $content , - 1 , $nbrep );
// Replace relative link /xxx.php?a=b&c=d#aaa or /xxx.php?a=b&c=d with dolibarr URL
2020-05-19 19:55:28 +02:00
// Warning: we may replace twice if href="..." was inside an include (dolWebsiteOutput called by include and the by final page), that's why
// at end we replace the '!~!~!~' only if we are in final parent page.
2019-08-15 18:07:36 +02:00
$content = preg_replace ( '/(href=")\/?([^:\"\!]*)\.php\?([^#\"<>]*)(#[^\"<>]*)?\"/' , '\1!~!~!~' . DOL_URL_ROOT . '/public/website/index.php?website=' . $website -> ref . '&pageref=\2&\3\4"' , $content , - 1 , $nbrep );
// Replace relative link without .php like /xxx#aaa or /xxx with dolibarr URL: ...href="....php"
$content = preg_replace ( '/(href=")\/?([a-zA-Z0-9\-_#]+)(\"|\?)/' , '\1!~!~!~' . DOL_URL_ROOT . '/public/website/index.php?website=' . $website -> ref . '&pageref=\2\3' , $content , - 1 , $nbrep );
2017-10-27 17:58:38 +02:00
2018-07-19 21:35:21 +02:00
// Fix relative link /document.php with correct URL after the DOL_URL_ROOT: href="/document.php?modulepart=" => href="/dolibarr/document.php?modulepart="
2019-08-15 18:07:36 +02:00
$content = preg_replace ( '/(href=")(\/?document\.php\?[^\"]*modulepart=[^\"]*)(\")/' , '\1!~!~!~' . DOL_URL_ROOT . '\2\3' , $content , - 1 , $nbrep );
$content = preg_replace ( '/(src=")(\/?document\.php\?[^\"]*modulepart=[^\"]*)(\")/' , '\1!~!~!~' . DOL_URL_ROOT . '\2\3' , $content , - 1 , $nbrep );
2018-01-02 22:58:37 +01:00
2018-07-19 21:35:21 +02:00
// Fix relative link /viewimage.php with correct URL after the DOL_URL_ROOT: href="/viewimage.php?modulepart=" => href="/dolibarr/viewimage.php?modulepart="
2019-08-15 18:07:36 +02:00
$content = preg_replace ( '/(href=")(\/?viewimage\.php\?[^\"]*modulepart=[^\"]*)(\")/' , '\1!~!~!~' . DOL_URL_ROOT . '\2\3' , $content , - 1 , $nbrep );
$content = preg_replace ( '/(src=")(\/?viewimage\.php\?[^\"]*modulepart=[^\"]*)(\")/' , '\1!~!~!~' . DOL_URL_ROOT . '\2\3' , $content , - 1 , $nbrep );
$content = preg_replace ( '/(url\(")(\/?viewimage\.php\?[^\"]*modulepart=[^\"]*)(\")/' , '\1!~!~!~' . DOL_URL_ROOT . '\2\3' , $content , - 1 , $nbrep );
2017-10-27 17:58:38 +02:00
// Fix relative link into medias with correct URL after the DOL_URL_ROOT: ../url("medias/
2020-03-02 17:00:51 +01:00
$content = preg_replace ( '/url\((["\']?)\/?medias\//' , 'url(\1!~!~!~' . DOL_URL_ROOT . '/viewimage.php?modulepart=medias&file=' , $content , - 1 , $nbrep );
2020-03-02 17:06:22 +01:00
$content = preg_replace ( '/data-slide-bg=(["\']?)\/?medias\//' , 'data-slide-bg=\1!~!~!~' . DOL_URL_ROOT . '/viewimage.php?modulepart=medias&file=' , $content , - 1 , $nbrep );
2018-07-19 21:35:21 +02:00
2019-08-16 04:32:06 +02:00
// <img src="medias/...image.png... => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png...
// <img src="...image.png... => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png...
$content = preg_replace ( '/(<img[^>]*src=")\/?medias\//' , '\1!~!~!~' . DOL_URL_ROOT . '/viewimage.php?modulepart=medias&file=' , $content , - 1 , $nbrep );
2018-07-19 21:35:21 +02:00
// <img src="image.png... => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png...
2019-08-16 04:32:06 +02:00
$content = preg_replace ( '/(<img[^>]*src=")\/?([^:\"\!]+)\"/' , '\1!~!~!~' . DOL_URL_ROOT . '/viewimage.php?modulepart=medias&file=\2"' , $content , - 1 , $nbrep );
2018-07-19 21:35:21 +02:00
// <img src="viewimage.php/modulepart=medias&file=image.png" => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png"
2019-08-15 18:07:36 +02:00
$content = preg_replace ( '/(<img[^>]*src=")(\/?viewimage\.php)/' , '\1!~!~!~' . DOL_URL_ROOT . '/viewimage.php' , $content , - 1 , $nbrep );
2017-10-27 17:58:38 +02:00
// action="newpage.php" => action="dolibarr/website/index.php?website=...&pageref=newpage
2019-08-15 18:07:36 +02:00
$content = preg_replace ( '/(action=")\/?([^:\"]*)(\.php\")/' , '\1!~!~!~' . DOL_URL_ROOT . '/public/website/index.php?website=' . $website -> ref . '&pageref=\2"' , $content , - 1 , $nbrep );
2019-08-16 04:32:06 +02:00
// Fix relative URL
$content = str_replace ( 'src="!~!~!~/viewimage.php' , 'src="!~!~!~' . DOL_URL_ROOT . '/viewimage.php' , $content );
2019-08-16 05:01:54 +02:00
$content = str_replace ( 'href="!~!~!~/document.php' , 'href="!~!~!~' . DOL_URL_ROOT . '/document.php' , $content );
2020-02-21 11:36:16 +01:00
2020-05-19 19:55:28 +02:00
// Remove the protection tag !~!~!~, but only if this is the parent page and not an include
if ( empty ( $includehtmlcontentopened )) {
$content = str_replace ( '!~!~!~' , '' , $content );
}
2021-02-21 14:46:52 +01:00
} else // REPLACEMENT OF LINKS When page called from virtual host web server
2017-10-07 13:09:31 +02:00
{
2020-03-12 12:45:44 +01:00
$symlinktomediaexists = 1 ;
2020-05-20 12:38:38 +02:00
if ( $website -> virtualhost ) {
$content = preg_replace ( '/^(<link[^>]*rel="canonical" href=")\//m' , '\1' . $website -> virtualhost . '/' , $content , - 1 , $nbrep );
}
//print 'rrrrrrrrr'.$website->virtualhost.$content;
2017-07-03 02:09:14 +02:00
// Make a change into HTML code to allow to include images from medias directory correct with direct link for virtual server
// <img alt="" src="/dolibarr_dev/htdocs/viewimage.php?modulepart=medias&entity=1&file=image/ldestailleur_166x166.jpg" style="height:166px; width:166px" />
// become
// <img alt="" src="'.$urlwithroot.'/medias/image/ldestailleur_166x166.jpg" style="height:166px; width:166px" />
2021-02-23 22:03:23 +01:00
if ( ! $symlinktomediaexists ) {
2019-08-16 04:32:06 +02:00
// <img src="image.png... => <img src="medias/image.png...
2020-03-12 12:45:44 +01:00
$content = preg_replace ( '/(<img[^>]*src=")\/?image\//' , '\1/wrapper.php?modulepart=medias&file=medias/image/' , $content , - 1 , $nbrep );
$content = preg_replace ( '/(url\(["\']?)\/?image\//' , '\1/wrapper.php?modulepart=medias&file=medias/image/' , $content , - 1 , $nbrep );
2019-08-16 04:32:06 +02:00
2020-03-12 12:45:44 +01:00
$content = preg_replace ( '/(<script[^>]*src=")[^\"]*document\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/' , '\1/wrapper.php\2modulepart=medias\3file=\4\5' , $content , - 1 , $nbrep );
$content = preg_replace ( '/(<a[^>]*href=")[^\"]*document\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/' , '\1/wrapper.php\2modulepart=medias\3file=\4\5' , $content , - 1 , $nbrep );
2018-09-08 14:30:27 +02:00
2020-03-12 12:45:44 +01:00
$content = preg_replace ( '/(<a[^>]*href=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/' , '\1/wrapper.php\2modulepart=medias\3file=\4\5' , $content , - 1 , $nbrep );
$content = preg_replace ( '/(<img[^>]*src=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/' , '\1/wrapper.php\2modulepart=medias\3file=\4\5' , $content , - 1 , $nbrep );
$content = preg_replace ( '/(url\(["\']?)[^\)]*viewimage\.php([^\)]*)modulepart=medias([^\)]*)file=([^\)]*)(["\']?\))/' , '\1/wrapper.php\2modulepart=medias\3file=\4\5' , $content , - 1 , $nbrep );
2018-09-09 14:05:23 +02:00
2020-03-12 12:45:44 +01:00
$content = preg_replace ( '/(<a[^>]*href=")[^\"]*viewimage\.php([^\"]*)hashp=([^\"]*)("[^>]*>)/' , '\1/wrapper.php\2hashp=\3\4' , $content , - 1 , $nbrep );
$content = preg_replace ( '/(<img[^>]*src=")[^\"]*viewimage\.php([^\"]*)hashp=([^\"]*)("[^>]*>)/' , '\1/wrapper.php\2hashp=\3\4' , $content , - 1 , $nbrep );
$content = preg_replace ( '/(url\(["\']?)[^\)]*viewimage\.php([^\)]*)hashp=([^\)]*)(["\']?\))/' , '\1/wrapper.php\2hashp\3\4' , $content , - 1 , $nbrep );
2018-12-17 17:29:04 +01:00
2020-03-12 12:45:44 +01:00
$content = preg_replace ( '/(<img[^>]*src=")[^\"]*viewimage\.php([^\"]*)modulepart=mycompany([^\"]*)file=([^\"]*)("[^>]*>)/' , '\1/wrapper.php\2modulepart=mycompany\3file=\4\5' , $content , - 1 , $nbrep );
2019-08-16 05:32:24 +02:00
// If some links to documents or viewimage remains, we replace with wrapper
2020-03-12 12:45:44 +01:00
$content = preg_replace ( '/(<img[^>]*src=")\/?viewimage\.php/' , '\1/wrapper.php' , $content , - 1 , $nbrep );
$content = preg_replace ( '/(<a[^>]*href=")\/?documents\.php/' , '\1/wrapper.php' , $content , - 1 , $nbrep );
2020-05-21 15:05:19 +02:00
} else {
2019-08-16 04:32:06 +02:00
// <img src="image.png... => <img src="medias/image.png...
2020-03-12 12:45:44 +01:00
$content = preg_replace ( '/(<img[^>]*src=")\/?image\//' , '\1/medias/image/' , $content , - 1 , $nbrep );
$content = preg_replace ( '/(url\(["\']?)\/?image\//' , '\1/medias/image/' , $content , - 1 , $nbrep );
2019-08-16 04:32:06 +02:00
2020-03-12 12:45:44 +01:00
$content = preg_replace ( '/(<script[^>]*src=")[^\"]*document\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/' , '\1/medias/\4\5' , $content , - 1 , $nbrep );
$content = preg_replace ( '/(<a[^>]*href=")[^\"]*document\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/' , '\1/medias/\4\5' , $content , - 1 , $nbrep );
2018-01-08 01:37:10 +01:00
2020-03-12 12:45:44 +01:00
$content = preg_replace ( '/(<a[^>]*href=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/' , '\1/medias/\4\5' , $content , - 1 , $nbrep );
$content = preg_replace ( '/(<img[^>]*src=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/' , '\1/medias/\4\5' , $content , - 1 , $nbrep );
$content = preg_replace ( '/(url\(["\']?)[^\)]*viewimage\.php([^\)]*)modulepart=medias([^\)]*)file=([^\)]*)(["\']?\))/' , '\1/medias/\4\5' , $content , - 1 , $nbrep );
2018-09-09 14:05:23 +02:00
2020-03-12 12:45:44 +01:00
$content = preg_replace ( '/(<a[^>]*href=")[^\"]*viewimage\.php([^\"]*)hashp=([^\"]*)("[^>]*>)/' , '\1/wrapper.php\2hashp=\3\4' , $content , - 1 , $nbrep );
$content = preg_replace ( '/(<img[^>]*src=")[^\"]*viewimage\.php([^\"]*)hashp=([^\"]*)("[^>]*>)/' , '\1/wrapper.php\2hashp=\3\4' , $content , - 1 , $nbrep );
$content = preg_replace ( '/(url\(["\']?)[^\)]*viewimage\.php([^\)]*)hashp=([^\)]*)(["\']?\))/' , '\1/wrapper.php\2hashp=\3\4' , $content , - 1 , $nbrep );
2018-12-17 17:29:04 +01:00
2020-03-12 12:45:44 +01:00
$content = preg_replace ( '/(<img[^>]*src=")[^\"]*viewimage\.php([^\"]*)modulepart=mycompany([^\"]*)file=([^\"]*)("[^>]*>)/' , '\1/wrapper.php\2modulepart=mycompany\3file=\4\5' , $content , - 1 , $nbrep );
2019-08-16 05:32:24 +02:00
// If some links to documents or viewimage remains, we replace with wrapper
2020-03-12 12:45:44 +01:00
$content = preg_replace ( '/(<img[^>]*src=")\/?viewimage\.php/' , '\1/wrapper.php' , $content , - 1 , $nbrep );
$content = preg_replace ( '/(<a[^>]*href=")\/?document\.php/' , '\1/wrapper.php' , $content , - 1 , $nbrep );
2017-10-07 13:09:31 +02:00
}
}
2021-02-22 11:51:43 +01:00
if ( ! defined ( 'USEDOLIBARREDITOR' )) {
$content = str_replace ( ' contenteditable="true"' , ' contenteditable="false"' , $content );
}
2020-04-17 00:42:01 +02:00
2020-04-23 18:55:10 +02:00
if ( ! empty ( $conf -> global -> WEBSITE_ADD_CSS_TO_BODY )) {
2020-04-17 00:42:01 +02:00
$content = str_replace ( '<body id="bodywebsite" class="bodywebsite' , '<body id="bodywebsite" class="bodywebsite ' . $conf -> global -> WEBSITE_ADD_CSS_TO_BODY , $content );
}
2018-06-11 10:19:42 +02:00
2017-10-07 13:09:31 +02:00
dol_syslog ( " dolWebsiteOutput end " );
print $content ;
2017-02-24 21:13:40 +01:00
}
2017-07-20 16:49:20 +02:00
2017-08-21 03:13:44 +02:00
/**
* Format img tags to introduce viewimage on img src .
*
* @ param string $content Content string
* @ return void
2019-08-13 07:05:06 +02:00
* @ see dolWebsiteOutput ()
2017-08-21 03:13:44 +02:00
*/
/*
function dolWebsiteSaveContent ( $content )
{
global $db , $langs , $conf , $user ;
global $dolibarr_main_url_root , $dolibarr_main_data_root ;
//dol_syslog("dolWebsiteSaveContent start (mode=".(defined('USEDOLIBARRSERVER')?'USEDOLIBARRSERVER':'').')');
// Define $urlwithroot
$urlwithouturlroot = preg_replace ( '/' . preg_quote ( DOL_URL_ROOT , '/' ) . '$/i' , '' , trim ( $dolibarr_main_url_root ));
$urlwithroot = $urlwithouturlroot . DOL_URL_ROOT ; // This is to use external domain name found into config file
//$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
//$content = preg_replace('/(<img.*src=")(?!(http|'.preg_quote(DOL_URL_ROOT,'/').'\/viewimage))/', '\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
return $content ;
}
*/
2018-02-19 16:46:29 +01:00
/**
2018-02-28 14:36:22 +01:00
* Make a redirect to another container .
2018-02-19 16:46:29 +01:00
*
2019-12-07 11:51:21 +01:00
* @ param string $containerref Ref of container to redirect to ( Example : 'mypage' or 'mypage.php' ) .
2018-02-28 14:36:22 +01:00
* @ param string $containeraliasalt Ref of alternative aliases to redirect to .
* @ param int $containerid Id of container .
2019-12-07 11:51:21 +01:00
* @ param int $permanent 0 = Use temporary redirect 302 , 1 = Use permanent redirect 301
2018-02-19 16:46:29 +01:00
* @ return void
*/
2019-12-07 11:51:21 +01:00
function redirectToContainer ( $containerref , $containeraliasalt = '' , $containerid = 0 , $permanent = 0 )
2018-02-19 16:46:29 +01:00
{
global $db , $website ;
$newurl = '' ;
2019-11-13 19:37:08 +01:00
$result = 0 ;
2018-02-19 16:46:29 +01:00
2018-02-28 14:36:22 +01:00
// We make redirect using the alternative alias, we must find the real $containerref
2021-02-23 22:03:23 +01:00
if ( $containeraliasalt ) {
2018-02-28 14:36:22 +01:00
include_once DOL_DOCUMENT_ROOT . '/website/class/websitepage.class.php' ;
2019-11-13 19:37:08 +01:00
$tmpwebsitepage = new WebsitePage ( $db );
2018-02-28 14:36:22 +01:00
$result = $tmpwebsitepage -> fetch ( 0 , $website -> id , '' , $containeraliasalt );
2021-02-23 22:03:23 +01:00
if ( $result > 0 ) {
2018-02-28 14:36:22 +01:00
$containerref = $tmpwebsitepage -> pageurl ;
2020-05-21 15:05:19 +02:00
} else {
2018-02-28 14:36:22 +01:00
print " Error, page contains a redirect to the alternative alias ' " . $containeraliasalt . " ' that does not exists in web site ( " . $website -> id . " / " . $website -> ref . " ) " ;
exit ;
}
}
2021-02-23 22:03:23 +01:00
if ( defined ( 'USEDOLIBARREDITOR' )) {
2019-08-18 18:36:18 +02:00
/* print '<div class="margintoponly marginleftonly">' ;
2019-08-12 17:22:46 +02:00
print " This page contains dynamic code that make a redirect to ' " . $containerref . " ' in your current context. Redirect has been canceled as it is not supported in edition mode. " ;
2019-08-18 18:36:18 +02:00
print '</div>' ; */
$text = " This page contains dynamic code that make a redirect to ' " . $containerref . " ' in your current context. Redirect has been canceled as it is not supported in edition mode. " ;
setEventMessages ( $text , null , 'warnings' , 'WEBSITEREDIRECTDISABLED' . $containerref );
2019-08-12 17:22:46 +02:00
return ;
2019-07-28 21:24:42 +02:00
}
2021-02-23 22:03:23 +01:00
if ( defined ( 'USEDOLIBARRSERVER' )) { // When page called from Dolibarr server
2018-02-19 16:46:29 +01:00
// Check new container exists
2021-02-23 22:03:23 +01:00
if ( ! $containeraliasalt ) { // If containeraliasalt set, we already did the test
2018-02-28 14:36:22 +01:00
include_once DOL_DOCUMENT_ROOT . '/website/class/websitepage.class.php' ;
2019-11-13 19:37:08 +01:00
$tmpwebsitepage = new WebsitePage ( $db );
2018-02-28 14:36:22 +01:00
$result = $tmpwebsitepage -> fetch ( 0 , $website -> id , $containerref );
unset ( $tmpwebsitepage );
}
2021-02-23 22:03:23 +01:00
if ( $result > 0 ) {
2018-02-19 22:10:15 +01:00
$currenturi = $_SERVER [ " REQUEST_URI " ];
2019-12-07 11:51:21 +01:00
$regtmp = array ();
2021-02-23 22:03:23 +01:00
if ( preg_match ( '/&pageref=([^&]+)/' , $currenturi , $regtmp )) {
if ( $regtmp [ 0 ] == $containerref ) {
2018-02-28 14:36:22 +01:00
print " Error, page with uri '. $currenturi .' try a redirect to the same alias page ' " . $containerref . " ' in web site ' " . $website -> ref . " ' " ;
2018-02-19 22:10:15 +01:00
exit ;
2020-05-21 15:05:19 +02:00
} else {
2018-02-28 14:36:22 +01:00
$newurl = preg_replace ( '/&pageref=([^&]+)/' , '&pageref=' . $containerref , $currenturi );
2018-02-19 22:10:15 +01:00
}
2020-05-21 15:05:19 +02:00
} else {
2018-02-28 14:36:22 +01:00
$newurl = $currenturi . '&pageref=' . urlencode ( $containerref );
2018-02-19 22:10:15 +01:00
}
2018-02-19 16:46:29 +01:00
}
2020-05-21 15:05:19 +02:00
} else // When page called from virtual host server
2018-02-19 16:46:29 +01:00
{
2018-02-28 14:36:22 +01:00
$newurl = '/' . $containerref . '.php' ;
2018-02-19 16:46:29 +01:00
}
2021-02-23 22:03:23 +01:00
if ( $newurl ) {
2019-12-07 11:51:21 +01:00
if ( $permanent ) {
header ( " Status: 301 Moved Permanently " , false , 301 );
}
2022-02-21 19:46:11 +01:00
header ( " Location: " . $newurl . ( empty ( $_SERVER [ " QUERY_STRING " ]) ? '' : '?' . $_SERVER [ " QUERY_STRING " ]));
2018-02-19 16:46:29 +01:00
exit ;
2020-05-21 15:05:19 +02:00
} else {
2018-02-28 14:36:22 +01:00
print " Error, page contains a redirect to the alias page ' " . $containerref . " ' that does not exists in web site ( " . $website -> id . " / " . $website -> ref . " ) " ;
2018-02-19 16:46:29 +01:00
exit ;
}
}
2017-08-21 03:13:44 +02:00
/**
* Clean an HTML page to report only content , so we can include it into another page .
2017-07-20 16:49:20 +02:00
* It outputs content of file sanitized from html and body part .
*
2018-02-28 14:36:22 +01:00
* @ param string $containerref Path to file to include ( must be a page from website root . Example : 'mypage.php' means 'mywebsite/mypage.php' )
2017-07-20 16:49:20 +02:00
* @ return void
*/
2018-02-28 14:36:22 +01:00
function includeContainer ( $containerref )
2017-07-20 16:49:20 +02:00
{
2020-03-12 12:45:44 +01:00
global $conf , $db , $hookmanager , $langs , $mysoc , $user , $website , $websitepage , $weblangs ; // Very important. Required to have var available when running included containers.
2017-07-20 16:49:20 +02:00
global $includehtmlcontentopened ;
2018-10-14 20:04:25 +02:00
global $websitekey , $websitepagefile ;
2017-07-20 16:49:20 +02:00
2020-03-12 12:45:44 +01:00
$MAXLEVEL = 20 ;
2017-07-20 16:49:20 +02:00
2021-02-23 22:03:23 +01:00
if ( ! preg_match ( '/\.php$/i' , $containerref )) {
$containerref .= '.php' ;
}
2017-12-12 01:40:34 +01:00
2020-03-12 12:45:44 +01:00
$fullpathfile = DOL_DATA_ROOT . '/website/' . $websitekey . '/' . $containerref ;
2017-07-20 16:49:20 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $includehtmlcontentopened )) {
$includehtmlcontentopened = 0 ;
}
2017-07-20 16:49:20 +02:00
$includehtmlcontentopened ++ ;
2021-02-23 22:03:23 +01:00
if ( $includehtmlcontentopened > $MAXLEVEL ) {
2021-05-17 06:39:18 +02:00
print 'ERROR: RECURSIVE CONTENT LEVEL. Depth of recursive call is more than the limit of ' . (( int ) $MAXLEVEL ) . " . \n " ;
2017-07-20 16:49:20 +02:00
return ;
}
2020-05-19 19:55:28 +02:00
//dol_syslog("Include container ".$containerref.' includehtmlcontentopened='.$includehtmlcontentopened);
2017-08-22 15:44:28 +02:00
// file_get_contents is not possible. We must execute code with include
2017-08-21 03:13:44 +02:00
//$content = file_get_contents($fullpathfile);
//print preg_replace(array('/^.*<body[^>]*>/ims','/<\/body>.*$/ims'), array('', ''), $content);*/
2017-08-22 15:32:45 +02:00
2017-08-22 15:44:28 +02:00
ob_start ();
2019-11-13 19:37:08 +01:00
$res = include $fullpathfile ; // Include because we want to execute code content
2017-08-22 15:44:28 +02:00
$tmpoutput = ob_get_contents ();
ob_end_clean ();
2020-05-19 19:55:28 +02:00
print " \n " . '<!-- include ' . $websitekey . '/' . $containerref . ( is_object ( $websitepage ) ? ' parent id=' . $websitepage -> id : '' ) . ' level = ' . $includehtmlcontentopened . ' -->' . " \n " ;
2019-11-13 19:37:08 +01:00
print preg_replace ( array ( '/^.*<body[^>]*>/ims' , '/<\/body>.*$/ims' ), array ( '' , '' ), $tmpoutput );
2017-08-22 15:44:28 +02:00
2021-02-23 22:03:23 +01:00
if ( ! $res ) {
2018-02-28 14:36:22 +01:00
print 'ERROR: FAILED TO INCLUDE PAGE ' . $containerref . " . \n " ;
2017-07-20 16:49:20 +02:00
}
$includehtmlcontentopened -- ;
}
2019-07-25 13:48:13 +02:00
/**
2021-10-08 10:59:11 +02:00
* Return HTML content to add structured data for an article , news or Blog Post . Use the json - ld format .
* Example :
* < ? php getStructureData ( 'blogpost' ); ?>
* < ? php getStructureData ( 'software' , array ( 'name' => 'Name' , 'os' => 'Windows' , 'price' => 10 )); ?>
2019-07-25 13:48:13 +02:00
*
2020-06-03 19:23:36 +02:00
* @ param string $type 'blogpost' , 'product' , 'software' , 'organization' , 'qa' , ...
2019-07-25 14:39:22 +02:00
* @ param array $data Array of data parameters for structured data
2019-07-25 13:48:13 +02:00
* @ return string HTML content
*/
2019-07-25 15:07:00 +02:00
function getStructuredData ( $type , $data = array ())
2019-07-25 13:48:13 +02:00
{
2020-05-17 20:02:01 +02:00
global $conf , $db , $hookmanager , $langs , $mysoc , $user , $website , $websitepage , $weblangs , $pagelangs ; // Very important. Required to have var available when running inluded containers.
2017-10-26 13:09:11 +02:00
2020-06-03 19:23:36 +02:00
$type = strtolower ( $type );
2021-02-23 22:03:23 +01:00
if ( $type == 'software' ) {
2020-06-03 19:24:26 +02:00
$ret = '<!-- Add structured data for entry in a software annuary -->' . " \n " ;
2019-07-25 14:39:22 +02:00
$ret .= '<script type="application/ld+json">' . " \n " ;
$ret .= ' {
" @context " : " https://schema.org " ,
" @type " : " SoftwareApplication " ,
2020-03-20 17:10:35 +01:00
" name " : " '.dol_escape_json( $data['name'] ).' " ,
" operatingSystem " : " '.dol_escape_json( $data['os'] ).' " ,
2021-05-17 06:39:18 +02:00
" applicationCategory " : " https://schema.org/'.dol_escape_json( $data['applicationCategory'] ).' " , ' ;
2020-10-31 14:32:18 +01:00
if ( ! empty ( $data [ 'ratingcount' ])) {
2020-06-03 19:23:36 +02:00
$ret .= '
" aggregateRating " : {
" @type " : " AggregateRating " ,
2021-05-17 06:39:18 +02:00
" ratingValue " : " '.dol_escape_json( $data['ratingvalue'] ).' " ,
" ratingCount " : " '.dol_escape_json( $data['ratingcount'] ).' "
2020-06-03 19:23:36 +02:00
}, ' ;
}
$ret .= '
2019-07-25 14:39:22 +02:00
" offers " : {
" @type " : " Offer " ,
2021-05-17 06:39:18 +02:00
" price " : " '.dol_escape_json( $data['price'] ).' " ,
" priceCurrency " : " '.dol_escape_json( $data['currency'] ? $data['currency'] : $conf->currency ).' "
2019-07-25 14:39:22 +02:00
}
} ' . " \n " ;
$ret .= '</script>' . " \n " ;
2021-02-23 22:03:23 +01:00
} elseif ( $type == 'organization' ) {
2020-05-17 20:02:01 +02:00
$companyname = $mysoc -> name ;
$url = $mysoc -> url ;
2020-05-25 22:19:28 +02:00
$ret = '<!-- Add structured data for organization -->' . " \n " ;
2020-05-17 20:02:01 +02:00
$ret .= '<script type="application/ld+json">' . " \n " ;
$ret .= ' {
" @context " : " https://schema.org " ,
" @type " : " Organization " ,
2020-05-18 14:53:35 +02:00
" name " : " '.dol_escape_json( $data['name'] ? $data['name'] : $companyname ).' " ,
" url " : " '.dol_escape_json( $data['url'] ? $data['url'] : $url ).' " ,
" logo " : " '.( $data['logo'] ? dol_escape_json( $data['logo'] ) : '/wrapper.php?modulepart=mycompany&file=logos%2F'.urlencode( $mysoc->logo )).' " ,
2020-05-17 20:02:01 +02:00
" contactPoint " : {
" @type " : " ContactPoint " ,
" contactType " : " Contact " ,
2020-05-18 14:53:35 +02:00
" email " : " '.dol_escape_json( $data['email'] ? $data['email'] : $mysoc->email ).' "
2020-05-25 22:19:28 +02:00
} ' . " \n " ;
2020-05-17 20:02:01 +02:00
if ( is_array ( $mysoc -> socialnetworks ) && count ( $mysoc -> socialnetworks ) > 0 ) {
2020-05-25 22:19:28 +02:00
$ret .= " , \n " ;
2020-05-17 20:02:01 +02:00
$ret .= '"sameAs": [' ;
$i = 0 ;
2020-05-21 01:41:27 +02:00
foreach ( $mysoc -> socialnetworks as $key => $value ) {
2020-05-17 20:02:01 +02:00
if ( $key == 'linkedin' ) {
2020-10-31 14:32:18 +01:00
$ret .= '"https://www.' . $key . '.com/company/' . dol_escape_json ( $value ) . '"' ;
2020-05-17 20:08:01 +02:00
} elseif ( $key == 'youtube' ) {
2020-10-31 14:32:18 +01:00
$ret .= '"https://www.' . $key . '.com/user/' . dol_escape_json ( $value ) . '"' ;
2020-05-21 15:05:19 +02:00
} else {
2020-10-31 14:32:18 +01:00
$ret .= '"https://www.' . $key . '.com/' . dol_escape_json ( $value ) . '"' ;
2020-05-17 20:02:01 +02:00
}
$i ++ ;
2021-02-23 22:03:23 +01:00
if ( $i < count ( $mysoc -> socialnetworks )) {
$ret .= ', ' ;
}
2020-05-17 20:02:01 +02:00
}
2020-05-25 22:19:28 +02:00
$ret .= ']' . " \n " ;
2020-05-17 20:02:01 +02:00
}
2020-05-25 22:21:36 +02:00
$ret .= '}' . " \n " ;
2020-05-17 20:02:01 +02:00
$ret .= '</script>' . " \n " ;
2021-02-23 22:03:23 +01:00
} elseif ( $type == 'blogpost' ) {
if ( ! empty ( $websitepage -> author_alias )) {
2020-03-20 18:58:45 +01:00
//include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
//$tmpuser = new User($db);
//$restmpuser = $tmpuser->fetch($websitepage->fk_user_creat);
$pageurl = $websitepage -> pageurl ;
$title = $websitepage -> title ;
$image = $websitepage -> image ;
$companyname = $mysoc -> name ;
$description = $websitepage -> description ;
$pageurl = str_replace ( '__WEBSITE_KEY__' , $website -> ref , $pageurl );
$title = str_replace ( '__WEBSITE_KEY__' , $website -> ref , $title );
2021-05-17 06:13:26 +02:00
$image = '/medias' . ( preg_match ( '/^\//' , $image ) ? '' : '/' ) . str_replace ( '__WEBSITE_KEY__' , $website -> ref , $image );
2020-03-20 18:58:45 +01:00
$companyname = str_replace ( '__WEBSITE_KEY__' , $website -> ref , $companyname );
$description = str_replace ( '__WEBSITE_KEY__' , $website -> ref , $description );
$ret = '<!-- Add structured data for blog post -->' . " \n " ;
$ret .= '<script type="application/ld+json">' . " \n " ;
$ret .= ' {
" @context " : " https://schema.org " ,
" @type " : " NewsArticle " ,
" mainEntityOfPage " : {
" @type " : " WebPage " ,
" @id " : " '.dol_escape_json( $pageurl ).' "
},
" headline " : " '.dol_escape_json( $title ).' " ,
" image " : [
" '.dol_escape_json( $image ).' "
],
2020-05-17 20:02:01 +02:00
" dateCreated " : " '.dol_print_date( $websitepage->date_creation , 'dayhourrfc').' " ,
2020-03-20 18:58:45 +01:00
" datePublished " : " '.dol_print_date( $websitepage->date_creation , 'dayhourrfc').' " ,
" dateModified " : " '.dol_print_date( $websitepage->date_modification , 'dayhourrfc').' " ,
" author " : {
" @type " : " Person " ,
" name " : " '.dol_escape_json( $websitepage->author_alias ).' "
},
" publisher " : {
" @type " : " Organization " ,
" name " : " '.dol_escape_json( $companyname ).' " ,
" logo " : {
" @type " : " ImageObject " ,
2020-05-17 19:09:22 +02:00
" url " : " /wrapper.php?modulepart=mycompany&file=logos%2F'.urlencode( $mysoc->logo ).' "
2020-03-20 18:58:45 +01:00
}
2020-05-17 20:02:01 +02:00
}, ' . " \n " ;
if ( $websitepage -> keywords ) {
$ret .= '"keywords": [' ;
$i = 0 ;
$arrayofkeywords = explode ( ',' , $websitepage -> keywords );
2020-05-21 01:41:27 +02:00
foreach ( $arrayofkeywords as $keyword ) {
2020-10-31 14:32:18 +01:00
$ret .= '"' . dol_escape_json ( $keyword ) . '"' ;
2020-05-17 20:02:01 +02:00
$i ++ ;
2021-02-23 22:03:23 +01:00
if ( $i < count ( $arrayofkeywords )) {
$ret .= ', ' ;
}
2020-05-17 20:02:01 +02:00
}
$ret .= '],' . " \n " ;
}
$ret .= '"description": "' . dol_escape_json ( $description ) . '"' ;
$ret .= " \n " . '}' . " \n " ;
2020-03-20 18:58:45 +01:00
$ret .= '</script>' . " \n " ;
2021-05-17 06:18:47 +02:00
} else {
$ret .= '<!-- no structured data inserted inline inside blogpost because no author_alias defined -->' . " \n " ;
2019-07-25 13:48:13 +02:00
}
2021-02-23 22:03:23 +01:00
} elseif ( $type == 'product' ) {
2020-05-25 22:19:28 +02:00
$ret = '<!-- Add structured data for product -->' . " \n " ;
2020-03-12 12:45:44 +01:00
$ret .= '<script type="application/ld+json">' . " \n " ;
$ret .= ' {
2019-07-25 14:39:22 +02:00
" @context " : " https://schema.org/ " ,
" @type " : " Product " ,
2020-03-20 17:10:35 +01:00
" name " : " '.dol_escape_json( $data['label'] ).' " ,
2019-07-25 14:39:22 +02:00
" image " : [
2020-03-20 17:10:35 +01:00
" '.dol_escape_json( $data['image'] ).' " ,
2019-07-25 14:39:22 +02:00
],
2020-03-20 17:10:35 +01:00
" description " : " '.dol_escape_json( $data['description'] ).' " ,
" sku " : " '.dol_escape_json( $data['ref'] ).' " ,
2019-07-25 14:39:22 +02:00
" brand " : {
" @type " : " Thing " ,
2020-03-20 17:10:35 +01:00
" name " : " '.dol_escape_json( $data['brand'] ).' "
2019-07-25 14:39:22 +02:00
},
" author " : {
" @type " : " Person " ,
2020-03-20 17:10:35 +01:00
" name " : " '.dol_escape_json( $data['author'] ).' "
2019-07-25 14:39:22 +02:00
}
},
" offers " : {
" @type " : " Offer " ,
" url " : " https://example.com/anvil " ,
2021-05-17 06:39:18 +02:00
" priceCurrency " : " '.dol_escape_json( $data['currency'] ? $data['currency'] : $conf->currency ).' " ,
" price " : " '.dol_escape_json( $data['price'] ).' " ,
2019-07-25 14:39:22 +02:00
" itemCondition " : " https://schema.org/UsedCondition " ,
" availability " : " https://schema.org/InStock " ,
" seller " : {
" @type " : " Organization " ,
2020-03-20 17:10:35 +01:00
" name " : " '.dol_escape_json( $mysoc->name ).' "
2019-07-25 14:39:22 +02:00
}
}
} ' . " \n " ;
2020-03-12 12:45:44 +01:00
$ret .= '</script>' . " \n " ;
2021-02-23 22:03:23 +01:00
} elseif ( $type == 'qa' ) {
2020-06-03 19:24:26 +02:00
$ret = '<!-- Add structured data for QA -->' . " \n " ;
2020-06-03 19:23:36 +02:00
$ret .= '<script type="application/ld+json">' . " \n " ;
$ret .= ' {
" @context " : " https://schema.org/ " ,
" @type " : " QAPage " ,
" mainEntity " : {
" @type " : " Question " ,
" name " : " '.dol_escape_json( $data['name'] ).' " ,
" text " : " '.dol_escape_json( $data['name'] ).' " ,
" answerCount " : 1 ,
" author " : {
" @type " : " Person " ,
" name " : " '.dol_escape_json( $data['author'] ).' "
}
" acceptedAnswer " : {
" @type " : " Answer " ,
" text " : " '.dol_escape_json(dol_string_nohtmltag(dolStripPhpCode( $data['description'] ))).' " ,
" author " : {
" @type " : " Person " ,
" name " : " '.dol_escape_json( $data['author'] ).' "
}
}
}
} ' . " \n " ;
$ret .= '</script>' . " \n " ;
2019-07-25 14:39:22 +02:00
}
2019-07-25 13:48:13 +02:00
return $ret ;
}
2017-10-26 13:09:11 +02:00
2021-10-08 11:06:28 +02:00
/**
* Return HTML content to add as header card for an article , news or Blog Post or home page .
*
* @ param array $params Array of parameters
* @ return string HTML content
*/
function getSocialNetworkHeaderCards ( $params = null )
{
global $conf , $db , $hookmanager , $langs , $mysoc , $user , $website , $websitepage , $weblangs ; // Very important. Required to have var available when running inluded containers.
$out = '' ;
if ( $website -> virtualhost ) {
$pageurl = $websitepage -> pageurl ;
$title = $websitepage -> title ;
$image = $websitepage -> image ;
$companyname = $mysoc -> name ;
$description = $websitepage -> description ;
$pageurl = str_replace ( '__WEBSITE_KEY__' , $website -> ref , $pageurl );
$title = str_replace ( '__WEBSITE_KEY__' , $website -> ref , $title );
$image = '/medias' . ( preg_match ( '/^\//' , $image ) ? '' : '/' ) . str_replace ( '__WEBSITE_KEY__' , $website -> ref , $image );
$companyname = str_replace ( '__WEBSITE_KEY__' , $website -> ref , $companyname );
$description = str_replace ( '__WEBSITE_KEY__' , $website -> ref , $description );
2021-10-08 12:00:13 +02:00
$shortlangcode = '' ;
if ( $websitepage -> lang ) {
$shortlangcode = substr ( $websitepage -> lang , 0 , 2 ); // en_US or en-US -> en
}
if ( empty ( $shortlangcode )) {
$shortlangcode = substr ( $website -> lang , 0 , 2 ); // en_US or en-US -> en
}
2021-10-08 11:06:28 +02:00
$fullurl = $website -> virtualhost . '/' . $websitepage -> pageurl . '.php' ;
2021-10-08 12:00:13 +02:00
$canonicalurl = $website -> virtualhost . (( $websitepage -> id == $website -> fk_default_home ) ? '/' : (( $shortlangcode != substr ( $website -> lang , 0 , 2 ) ? '/' . $shortlangcode : '' ) . '/' . $websitepage -> pageurl . '.php' ));
2021-10-08 11:06:28 +02:00
$hashtags = trim ( join ( ' #' , array_map ( 'trim' , explode ( ',' , $websitepage -> keywords ))));
2021-10-08 11:49:28 +02:00
// Open Graph
$out .= '<meta name="og:type" content="website">' . " \n " ; // TODO If blogpost, use type article
$out .= '<meta name="og:title" content="' . $websitepage -> title . '">' . " \n " ;
if ( $websitepage -> image ) {
$out .= '<meta name="og:image" content="' . $website -> virtualhost . $image . '">' . " \n " ;
}
2021-10-08 12:00:13 +02:00
$out .= '<meta name="og:url" content="' . $canonicalurl . '">' . " \n " ;
2021-10-08 11:49:28 +02:00
2021-10-08 11:06:28 +02:00
// Twitter
$out .= '<meta name="twitter:card" content="summary">' . " \n " ;
if ( ! empty ( $params ) && ! empty ( $params [ 'twitter_account' ])) {
$out .= '<meta name="twitter:site" content="@' . $params [ 'twitter_account' ] . '">' . " \n " ;
$out .= '<meta name="twitter:creator" content="@' . $params [ 'twitter_account' ] . '">' . " \n " ;
}
$out .= '<meta name="twitter:title" content="' . $websitepage -> title . '">' . " \n " ;
if ( $websitepage -> description ) {
$out .= '<meta name="twitter:description" content="' . $websitepage -> description . '">' . " \n " ;
}
if ( $websitepage -> image ) {
2021-10-08 11:41:45 +02:00
$out .= '<meta name="twitter:image" content="' . $website -> virtualhost . $image . '">' . " \n " ;
2021-10-08 11:06:28 +02:00
}
//$out .= '<meta name="twitter:domain" content="'.getDomainFromURL($website->virtualhost, 1).'">';
/*
$out .= '<meta name="twitter:app:name:iphone" content="">' ;
$out .= '<meta name="twitter:app:name:ipad" content="">' ;
$out .= '<meta name="twitter:app:name:googleplay" content="">' ;
$out .= '<meta name="twitter:app:url:iphone" content="">' ;
$out .= '<meta name="twitter:app:url:ipad" content="">' ;
$out .= '<meta name="twitter:app:url:googleplay" content="">' ;
$out .= '<meta name="twitter:app:id:iphone" content="">' ;
$out .= '<meta name="twitter:app:id:ipad" content="">' ;
$out .= '<meta name="twitter:app:id:googleplay" content="">' ;
*/
}
return $out ;
}
2020-03-20 19:34:49 +01:00
/**
* Return HTML content to add structured data for an article , news or Blog Post .
*
* @ return string HTML content
*/
function getSocialNetworkSharingLinks ()
{
global $conf , $db , $hookmanager , $langs , $mysoc , $user , $website , $websitepage , $weblangs ; // Very important. Required to have var available when running inluded containers.
2020-03-21 17:31:44 +01:00
$out = '<!-- section for social network sharing of page -->' . " \n " ;
2020-06-10 00:01:41 +02:00
if ( $website -> virtualhost ) {
$fullurl = $website -> virtualhost . '/' . $websitepage -> pageurl . '.php' ;
$hashtags = trim ( join ( ' #' , array_map ( 'trim' , explode ( ',' , $websitepage -> keywords ))));
$out .= '<div class="dol-social-share">' . " \n " ;
// Twitter
$out .= '<div class="dol-social-share-tw">' . " \n " ;
$out .= '<a href="https://twitter.com/share" class="twitter-share-button" data-url="' . $fullurl . '" data-text="' . dol_escape_htmltag ( $websitepage -> description ) . '" data-lang="' . $websitepage -> lang . '" data-size="small" data-related="" data-hashtags="' . preg_replace ( '/^#/' , '' , $hashtags ) . '" data-count="horizontal">Tweet</a>' ;
$out .= '<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?\'http\':\'https\';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+\'://platform.twitter.com/widgets.js\';fjs.parentNode.insertBefore(js,fjs);}}(document, \'script\', \'twitter-wjs\');</script>' ;
$out .= '</div>' . " \n " ;
// Reddit
$out .= '<div class="dol-social-share-reddit">' . " \n " ;
2021-11-22 02:35:55 +01:00
$out .= '<a href="https://www.reddit.com/submit" target="_blank" rel="noopener noreferrer external" onclick="window.location = \'https://www.reddit.com/submit?url=' . $fullurl . '\'; return false">' ;
2020-06-11 23:41:35 +02:00
$out .= '<span class="dol-social-share-reddit-span">Reddit</span>' ;
$out .= '</a>' ;
2020-06-10 00:01:41 +02:00
$out .= '</div>' . " \n " ;
// Facebook
$out .= '<div class="dol-social-share-fbl">' . " \n " ;
$out .= '<div id="fb-root"></div>' . " \n " ;
$out .= ' < script > ( function ( d , s , id ) {
var js , fjs = d . getElementsByTagName ( s )[ 0 ];
if ( d . getElementById ( id )) return ;
js = d . createElement ( s ); js . id = id ;
js . src = " //connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.0&appId=dolibarr.org " ;
fjs . parentNode . insertBefore ( js , fjs );
}( document , \ ' script\ ' , \ ' facebook - jssdk\ ' )); </ script >
< fb : like
href = " '. $fullurl .' "
layout = " button_count "
show_faces = " false "
width = " 90 "
colorscheme = " light "
share = " 1 "
action = " like " ></ fb : like > ' . " \n " ;
$out .= '</div>' . " \n " ;
$out .= " \n </div> \n " ;
2021-02-23 22:03:23 +01:00
} else {
2020-06-10 00:01:41 +02:00
$out .= '<!-- virtual host not defined in CMS. No way to add sharing buttons -->' . " \n " ;
}
2020-04-10 10:59:32 +02:00
$out .= '<!-- section end for social network sharing of page -->' . " \n " ;
2020-03-21 17:31:44 +01:00
return $out ;
2020-03-20 19:34:49 +01:00
}
2019-08-12 20:32:18 +02:00
/**
2019-09-02 16:39:56 +02:00
* Return list of containers object that match a criteria .
* WARNING : This function can be used by websites .
2019-08-12 20:32:18 +02:00
*
2020-03-21 19:09:14 +01:00
* @ param string $type Type of container to search into ( Example : '' , 'page' , 'blogpost' , 'page,blogpost' , ... )
2020-03-23 14:12:48 +01:00
* @ param string $algo Algorithm used for search ( Example : 'meta' is searching into meta information like title and description , 'content' , 'sitefiles' , or any combination 'meta,content,...' )
2019-08-12 20:32:18 +02:00
* @ param string $searchstring Search string
* @ param int $max Max number of answers
2020-03-23 14:12:48 +01:00
* @ param string $sortfield Sort Fields
* @ param string $sortorder Sort order ( 'DESC' or 'ASC' )
2020-04-27 18:48:35 +02:00
* @ param string $langcode Language code ( '' or 'en' , 'fr' , 'es' , ... )
2020-05-26 12:25:41 +02:00
* @ param array $otherfilters Other filters
2020-07-09 14:07:41 +02:00
* @ param int $status 0 or 1 , or - 1 for both
2019-08-12 20:32:18 +02:00
* @ return string HTML content
*/
2020-07-09 14:07:41 +02:00
function getPagesFromSearchCriterias ( $type , $algo , $searchstring , $max = 25 , $sortfield = 'date_creation' , $sortorder = 'DESC' , $langcode = '' , $otherfilters = 'null' , $status = 1 )
2019-08-12 20:32:18 +02:00
{
2020-03-12 12:45:44 +01:00
global $conf , $db , $hookmanager , $langs , $mysoc , $user , $website , $websitepage , $weblangs ; // Very important. Required to have var available when running inluded containers.
2019-08-12 20:32:18 +02:00
$error = 0 ;
$arrayresult = array ( 'code' => '' , 'list' => array ());
2021-02-23 22:03:23 +01:00
if ( ! is_object ( $weblangs )) {
$weblangs = $langs ;
}
2019-08-13 00:34:42 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $searchstring ) && empty ( $type ) && empty ( $langcode ) && empty ( $otherfilters )) {
2019-08-12 20:32:18 +02:00
$error ++ ;
2020-03-12 12:45:44 +01:00
$arrayresult [ 'code' ] = 'KO' ;
$arrayresult [ 'message' ] = $weblangs -> trans ( " EmptySearchString " );
2020-05-26 12:46:51 +02:00
} elseif ( $searchstring && dol_strlen ( $searchstring ) < 2 ) {
2019-08-13 00:34:42 +02:00
$weblangs -> load ( " errors " );
2019-08-12 20:32:18 +02:00
$error ++ ;
2020-03-12 12:45:44 +01:00
$arrayresult [ 'code' ] = 'KO' ;
$arrayresult [ 'message' ] = $weblangs -> trans ( " ErrorSearchCriteriaTooSmall " );
2020-05-21 15:05:19 +02:00
} else {
2020-03-21 19:09:14 +01:00
$tmparrayoftype = explode ( ',' , $type );
2020-05-26 12:46:51 +02:00
/* foreach ( $tmparrayoftype as $tmptype ) {
2020-03-21 19:09:14 +01:00
if ( ! in_array ( $tmptype , array ( '' , 'page' , 'blogpost' ))) {
$error ++ ;
$arrayresult [ 'code' ] = 'KO' ;
$arrayresult [ 'message' ] = 'Bad value for parameter type' ;
break ;
}
2020-05-26 12:46:51 +02:00
} */
2019-08-12 20:32:18 +02:00
}
2019-08-13 00:34:42 +02:00
$searchdone = 0 ;
2019-09-02 16:39:56 +02:00
$found = 0 ;
2019-08-13 00:34:42 +02:00
2021-02-23 22:03:23 +01:00
if ( ! $error && ( empty ( $max ) || ( $found < $max )) && ( preg_match ( '/meta/' , $algo ) || preg_match ( '/content/' , $algo ))) {
2020-05-26 12:25:41 +02:00
$sql = 'SELECT wp.rowid FROM ' . MAIN_DB_PREFIX . 'website_page as wp' ;
2020-10-31 14:32:18 +01:00
if ( is_array ( $otherfilters ) && ! empty ( $otherfilters [ 'category' ])) {
2020-05-26 12:25:41 +02:00
$sql .= ', ' . MAIN_DB_PREFIX . 'categorie_website_page as cwp' ;
}
2021-03-30 17:53:25 +02:00
$sql .= " WHERE wp.fk_website = " . (( int ) $website -> id );
2020-07-09 14:07:41 +02:00
if ( $status >= 0 ) {
2021-03-30 17:53:25 +02:00
$sql .= " AND wp.status = " . (( int ) $status );
2020-07-09 14:07:41 +02:00
}
2020-04-27 18:48:35 +02:00
if ( $langcode ) {
2020-05-26 12:25:41 +02:00
$sql .= " AND wp.lang =' " . $db -> escape ( $langcode ) . " ' " ;
2020-04-27 18:48:35 +02:00
}
2020-03-21 19:09:14 +01:00
if ( $type ) {
$tmparrayoftype = explode ( ',' , $type );
$typestring = '' ;
2020-04-10 10:59:32 +02:00
foreach ( $tmparrayoftype as $tmptype ) {
2020-05-26 12:46:51 +02:00
$typestring .= ( $typestring ? " , " : " " ) . " ' " . $db -> escape ( trim ( $tmptype )) . " ' " ;
2020-03-21 19:09:14 +01:00
}
2021-03-22 11:30:18 +01:00
$sql .= " AND wp.type_container IN ( " . $db -> sanitize ( $typestring , 1 ) . " ) " ;
2020-03-21 19:09:14 +01:00
}
2020-03-12 12:45:44 +01:00
$sql .= " AND ( " ;
2019-08-13 00:34:42 +02:00
$searchalgo = '' ;
2021-02-23 22:03:23 +01:00
if ( preg_match ( '/meta/' , $algo )) {
2021-09-26 14:21:23 +02:00
$searchalgo .= ( $searchalgo ? ' OR ' : '' ) . " wp.title LIKE '% " . $db -> escapeunderscore ( $db -> escape ( $searchstring )) . " %' OR wp.description LIKE '% " . $db -> escapeunderscore ( $db -> escape ( $searchstring )) . " %' " ;
$searchalgo .= ( $searchalgo ? ' OR ' : '' ) . " wp.keywords LIKE ' " . $db -> escapeunderscore ( $db -> escape ( $searchstring )) . " ,%' OR wp.keywords LIKE '% " . $db -> escapeunderscore ( $db -> escape ( $searchstring )) . " %' " ; // TODO Use a better way to scan keywords
2019-08-13 00:34:42 +02:00
}
2021-02-23 22:03:23 +01:00
if ( preg_match ( '/content/' , $algo )) {
2021-09-26 14:21:23 +02:00
$searchalgo .= ( $searchalgo ? ' OR ' : '' ) . " wp.content LIKE '% " . $db -> escapeunderscore ( $db -> escape ( $searchstring )) . " %' " ;
2019-08-13 00:34:42 +02:00
}
2020-03-12 12:45:44 +01:00
$sql .= $searchalgo ;
2020-10-31 14:32:18 +01:00
if ( is_array ( $otherfilters ) && ! empty ( $otherfilters [ 'category' ])) {
2020-05-26 12:25:41 +02:00
$sql .= ' AND cwp.fk_website_page = wp.rowid AND cwp.fk_categorie = ' . (( int ) $otherfilters [ 'category' ]);
}
2020-03-12 12:45:44 +01:00
$sql .= " ) " ;
2020-03-23 14:12:48 +01:00
$sql .= $db -> order ( $sortfield , $sortorder );
2020-03-12 12:45:44 +01:00
$sql .= $db -> plimit ( $max );
2021-09-26 14:21:23 +02:00
//print $sql;
2019-08-12 20:32:18 +02:00
$resql = $db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2019-08-12 20:32:18 +02:00
$i = 0 ;
2021-02-23 22:03:23 +01:00
while (( $obj = $db -> fetch_object ( $resql )) && ( $i < $max || $max == 0 )) {
if ( $obj -> rowid > 0 ) {
2019-08-12 20:32:18 +02:00
$tmpwebsitepage = new WebsitePage ( $db );
$tmpwebsitepage -> fetch ( $obj -> rowid );
2021-02-23 22:03:23 +01:00
if ( $tmpwebsitepage -> id > 0 ) {
$arrayresult [ 'list' ][ $obj -> rowid ] = $tmpwebsitepage ;
}
2019-09-02 16:39:56 +02:00
$found ++ ;
2019-08-12 20:32:18 +02:00
}
$i ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2019-09-02 16:39:56 +02:00
$error ++ ;
2020-03-12 12:45:44 +01:00
$arrayresult [ 'code' ] = $db -> lasterrno ();
$arrayresult [ 'message' ] = $db -> lasterror ();
2019-09-02 16:39:56 +02:00
}
2019-08-12 20:32:18 +02:00
2019-09-02 16:39:56 +02:00
$searchdone = 1 ;
}
2021-02-23 22:03:23 +01:00
if ( ! $error && ( empty ( $max ) || ( $found < $max )) && ( preg_match ( '/sitefiles/' , $algo ))) {
2019-09-02 18:43:22 +02:00
global $dolibarr_main_data_root ;
2020-03-12 12:45:44 +01:00
$pathofwebsite = $dolibarr_main_data_root . '/website/' . $website -> ref ;
$filehtmlheader = $pathofwebsite . '/htmlheader.html' ;
$filecss = $pathofwebsite . '/styles.css.php' ;
$filejs = $pathofwebsite . '/javascript.js.php' ;
$filerobot = $pathofwebsite . '/robots.txt' ;
$filehtaccess = $pathofwebsite . '/.htaccess' ;
$filemanifestjson = $pathofwebsite . '/manifest.json.php' ;
$filereadme = $pathofwebsite . '/README.md' ;
2019-09-02 18:43:22 +02:00
$filecontent = file_get_contents ( $filehtmlheader );
2021-02-23 22:03:23 +01:00
if (( empty ( $max ) || ( $found < $max )) && preg_match ( '/' . preg_quote ( $searchstring , '/' ) . '/' , $filecontent )) {
2020-03-12 12:45:44 +01:00
$arrayresult [ 'list' ][] = array ( 'type' => 'website_htmlheadercontent' );
2019-09-02 18:43:22 +02:00
}
2019-09-02 16:39:56 +02:00
2019-09-02 18:43:22 +02:00
$filecontent = file_get_contents ( $filecss );
2021-02-23 22:03:23 +01:00
if (( empty ( $max ) || ( $found < $max )) && preg_match ( '/' . preg_quote ( $searchstring , '/' ) . '/' , $filecontent )) {
2020-03-12 12:45:44 +01:00
$arrayresult [ 'list' ][] = array ( 'type' => 'website_csscontent' );
2019-09-02 18:43:22 +02:00
}
2019-09-02 16:39:56 +02:00
2019-09-02 18:43:22 +02:00
$filecontent = file_get_contents ( $filejs );
2021-02-23 22:03:23 +01:00
if (( empty ( $max ) || ( $found < $max )) && preg_match ( '/' . preg_quote ( $searchstring , '/' ) . '/' , $filecontent )) {
2020-03-12 12:45:44 +01:00
$arrayresult [ 'list' ][] = array ( 'type' => 'website_jscontent' );
2019-08-12 20:32:18 +02:00
}
2019-09-02 18:43:22 +02:00
$filerobot = file_get_contents ( $filerobot );
2021-02-23 22:03:23 +01:00
if (( empty ( $max ) || ( $found < $max )) && preg_match ( '/' . preg_quote ( $searchstring , '/' ) . '/' , $filecontent )) {
2020-03-12 12:45:44 +01:00
$arrayresult [ 'list' ][] = array ( 'type' => 'website_robotcontent' );
2019-08-12 20:32:18 +02:00
}
2019-08-13 00:34:42 +02:00
$searchdone = 1 ;
}
2021-02-23 22:03:23 +01:00
if ( ! $error ) {
if ( $searchdone ) {
2020-03-12 12:45:44 +01:00
$arrayresult [ 'code' ] = 'OK' ;
2021-02-23 22:03:23 +01:00
if ( empty ( $arrayresult [ 'list' ])) {
2020-03-12 12:45:44 +01:00
$arrayresult [ 'code' ] = 'KO' ;
$arrayresult [ 'message' ] = $weblangs -> trans ( " NoRecordFound " );
2019-09-02 16:39:56 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2019-09-02 16:39:56 +02:00
$error ++ ;
2020-03-12 12:45:44 +01:00
$arrayresult [ 'code' ] = 'KO' ;
$arrayresult [ 'message' ] = 'No supported algorithm found' ;
2019-09-02 16:39:56 +02:00
}
2019-08-12 20:32:18 +02:00
}
return $arrayresult ;
}
2017-10-06 23:54:45 +02:00
/**
* Download all images found into page content $tmp .
* If $modifylinks is set , links to images will be replace with a link to viewimage wrapper .
*
* @ param Website $object Object website
* @ param WebsitePage $objectpage Object website page
2017-10-07 02:29:27 +02:00
* @ param string $urltograb URL to grab ( exemple : http :// www . nltechno . com / or http :// www . nltechno . com / dir1 / or http :// www . nltechno . com / dir1 / mapage1 )
2017-10-06 23:54:45 +02:00
* @ param string $tmp Content to parse
* @ param string $action Var $action
2017-10-07 02:29:27 +02:00
* @ param string $modifylinks 0 = Do not modify content , 1 = Replace links with a link to viewimage
2018-02-19 14:56:37 +01:00
* @ param int $grabimages 0 = Do not grab images , 1 = Grab images
* @ param string $grabimagesinto 'root' or 'subpage'
2017-10-06 23:54:45 +02:00
* @ return void
*/
2019-01-27 15:20:16 +01:00
function getAllImages ( $object , $objectpage , $urltograb , & $tmp , & $action , $modifylinks = 0 , $grabimages = 1 , $grabimagesinto = 'subpage' )
2017-10-06 23:54:45 +02:00
{
global $conf ;
2019-11-13 19:37:08 +01:00
$error = 0 ;
2017-10-07 13:04:31 +02:00
2018-02-20 00:04:43 +01:00
dol_syslog ( " Call getAllImages with grabimagesinto= " . $grabimagesinto );
2019-11-13 19:37:08 +01:00
$alreadygrabbed = array ();
2017-10-07 02:29:27 +02:00
2021-02-23 22:03:23 +01:00
if ( preg_match ( '/\/$/' , $urltograb )) {
$urltograb .= '.' ;
}
2019-11-13 19:37:08 +01:00
$urltograb = dirname ( $urltograb ); // So urltograb is now http://www.nltechno.com or http://www.nltechno.com/dir1
2017-10-07 02:29:27 +02:00
2018-02-20 00:04:43 +01:00
// Search X in "img...src=X"
2020-03-21 17:31:44 +01:00
$regs = array ();
2017-10-07 02:29:27 +02:00
preg_match_all ( '/<img([^\.\/]+)src="([^>"]+)"([^>]*)>/i' , $tmp , $regs );
2021-02-23 22:03:23 +01:00
foreach ( $regs [ 0 ] as $key => $val ) {
if ( preg_match ( '/^data:image/i' , $regs [ 2 ][ $key ])) {
continue ; // We do nothing for such images
}
2017-10-07 02:40:03 +02:00
2021-02-23 22:03:23 +01:00
if ( preg_match ( '/^\//' , $regs [ 2 ][ $key ])) {
2017-12-10 17:59:19 +01:00
$urltograbdirrootwithoutslash = getRootURLFromURL ( $urltograb );
2019-11-13 19:37:08 +01:00
$urltograbbis = $urltograbdirrootwithoutslash . $regs [ 2 ][ $key ]; // We use dirroot
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:37:08 +01:00
$urltograbbis = $urltograb . '/' . $regs [ 2 ][ $key ]; // We use dir of grabbed file
2017-12-10 17:59:19 +01:00
}
2017-10-06 23:54:45 +02:00
$linkwithoutdomain = $regs [ 2 ][ $key ];
2018-02-19 14:56:37 +01:00
$dirforimages = '/' . $objectpage -> pageurl ;
2021-02-23 22:03:23 +01:00
if ( $grabimagesinto == 'root' ) {
$dirforimages = '' ;
}
2018-02-19 14:56:37 +01:00
// Define $filetosave and $filename
2019-11-13 19:37:08 +01:00
$filetosave = $conf -> medias -> multidir_output [ $conf -> entity ] . '/image/' . $object -> ref . $dirforimages . ( preg_match ( '/^\//' , $regs [ 2 ][ $key ]) ? '' : '/' ) . $regs [ 2 ][ $key ];
2021-02-23 22:03:23 +01:00
if ( preg_match ( '/^http/' , $regs [ 2 ][ $key ])) {
2017-10-06 23:54:45 +02:00
$urltograbbis = $regs [ 2 ][ $key ];
$linkwithoutdomain = preg_replace ( '/^https?:\/\/[^\/]+\//i' , '' , $regs [ 2 ][ $key ]);
2019-11-13 19:37:08 +01:00
$filetosave = $conf -> medias -> multidir_output [ $conf -> entity ] . '/image/' . $object -> ref . $dirforimages . ( preg_match ( '/^\//' , $linkwithoutdomain ) ? '' : '/' ) . $linkwithoutdomain ;
2017-10-06 23:54:45 +02:00
}
2019-11-13 19:37:08 +01:00
$filename = 'image/' . $object -> ref . $dirforimages . ( preg_match ( '/^\//' , $linkwithoutdomain ) ? '' : '/' ) . $linkwithoutdomain ;
2017-10-07 02:29:27 +02:00
// Clean the aa/bb/../cc into aa/cc
$filetosave = preg_replace ( '/\/[^\/]+\/\.\./' , '' , $filetosave );
$filename = preg_replace ( '/\/[^\/]+\/\.\./' , '' , $filename );
//var_dump($filetosave);
//var_dump($filename);
//exit;
2021-02-23 22:03:23 +01:00
if ( empty ( $alreadygrabbed [ $urltograbbis ])) {
if ( $grabimages ) {
2021-04-07 23:43:10 +02:00
$tmpgeturl = getURLContent ( $urltograbbis , 'GET' , '' , 1 , array (), array ( 'http' , 'https' ), 0 );
2021-02-23 22:03:23 +01:00
if ( $tmpgeturl [ 'curl_error_no' ]) {
2018-02-19 14:56:37 +01:00
$error ++ ;
setEventMessages ( 'Error getting ' . $urltograbbis . ': ' . $tmpgeturl [ 'curl_error_msg' ], null , 'errors' );
2019-11-13 19:37:08 +01:00
$action = 'create' ;
2021-02-23 22:03:23 +01:00
} elseif ( $tmpgeturl [ 'http_code' ] != '200' ) {
2018-02-19 14:56:37 +01:00
$error ++ ;
setEventMessages ( 'Error getting ' . $urltograbbis . ': ' . $tmpgeturl [ 'http_code' ], null , 'errors' );
2019-11-13 19:37:08 +01:00
$action = 'create' ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:37:08 +01:00
$alreadygrabbed [ $urltograbbis ] = 1 ; // Track that file was alreay grabbed.
2018-02-19 14:56:37 +01:00
dol_mkdir ( dirname ( $filetosave ));
$fp = fopen ( $filetosave , " w " );
fputs ( $fp , $tmpgeturl [ 'content' ]);
fclose ( $fp );
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> global -> MAIN_UMASK )) {
2018-02-19 14:56:37 +01:00
@ chmod ( $filetosave , octdec ( $conf -> global -> MAIN_UMASK ));
2021-02-23 22:03:23 +01:00
}
2018-02-19 14:56:37 +01:00
}
2017-10-07 02:29:27 +02:00
}
}
2021-02-23 22:03:23 +01:00
if ( $modifylinks ) {
2019-01-27 11:55:16 +01:00
$tmp = preg_replace ( '/' . preg_quote ( $regs [ 0 ][ $key ], '/' ) . '/i' , '<img' . $regs [ 1 ][ $key ] . 'src="' . DOL_URL_ROOT . '/viewimage.php?modulepart=medias&file=' . $filename . '"' . $regs [ 3 ][ $key ] . '>' , $tmp );
2017-10-07 02:29:27 +02:00
}
}
// Search X in "background...url(X)"
preg_match_all ( '/background([^\.\/\(;]+)url\([\"\']?([^\)\"\']*)[\"\']?\)/i' , $tmp , $regs );
2021-02-23 22:03:23 +01:00
foreach ( $regs [ 0 ] as $key => $val ) {
if ( preg_match ( '/^data:image/i' , $regs [ 2 ][ $key ])) {
continue ; // We do nothing for such images
}
2017-10-07 02:40:03 +02:00
2021-02-23 22:03:23 +01:00
if ( preg_match ( '/^\//' , $regs [ 2 ][ $key ])) {
2017-12-10 17:59:19 +01:00
$urltograbdirrootwithoutslash = getRootURLFromURL ( $urltograb );
2019-11-13 19:37:08 +01:00
$urltograbbis = $urltograbdirrootwithoutslash . $regs [ 2 ][ $key ]; // We use dirroot
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:37:08 +01:00
$urltograbbis = $urltograb . '/' . $regs [ 2 ][ $key ]; // We use dir of grabbed file
2017-12-10 17:59:19 +01:00
}
2017-10-07 02:29:27 +02:00
$linkwithoutdomain = $regs [ 2 ][ $key ];
2018-02-20 00:04:43 +01:00
$dirforimages = '/' . $objectpage -> pageurl ;
2021-02-23 22:03:23 +01:00
if ( $grabimagesinto == 'root' ) {
$dirforimages = '' ;
}
2018-02-20 00:04:43 +01:00
2019-11-13 19:37:08 +01:00
$filetosave = $conf -> medias -> multidir_output [ $conf -> entity ] . '/image/' . $object -> ref . $dirforimages . ( preg_match ( '/^\//' , $regs [ 2 ][ $key ]) ? '' : '/' ) . $regs [ 2 ][ $key ];
2017-10-07 02:29:27 +02:00
2021-02-23 22:03:23 +01:00
if ( preg_match ( '/^http/' , $regs [ 2 ][ $key ])) {
2017-10-07 02:29:27 +02:00
$urltograbbis = $regs [ 2 ][ $key ];
$linkwithoutdomain = preg_replace ( '/^https?:\/\/[^\/]+\//i' , '' , $regs [ 2 ][ $key ]);
2019-11-13 19:37:08 +01:00
$filetosave = $conf -> medias -> multidir_output [ $conf -> entity ] . '/image/' . $object -> ref . $dirforimages . ( preg_match ( '/^\//' , $linkwithoutdomain ) ? '' : '/' ) . $linkwithoutdomain ;
2017-10-06 23:54:45 +02:00
}
2017-10-07 02:29:27 +02:00
2019-11-13 19:37:08 +01:00
$filename = 'image/' . $object -> ref . $dirforimages . ( preg_match ( '/^\//' , $linkwithoutdomain ) ? '' : '/' ) . $linkwithoutdomain ;
2017-10-07 02:29:27 +02:00
// Clean the aa/bb/../cc into aa/cc
$filetosave = preg_replace ( '/\/[^\/]+\/\.\./' , '' , $filetosave );
$filename = preg_replace ( '/\/[^\/]+\/\.\./' , '' , $filename );
//var_dump($filetosave);
//var_dump($filename);
//exit;
2021-02-23 22:03:23 +01:00
if ( empty ( $alreadygrabbed [ $urltograbbis ])) {
if ( $grabimages ) {
2021-04-07 23:43:10 +02:00
$tmpgeturl = getURLContent ( $urltograbbis , 'GET' , '' , 1 , array (), array ( 'http' , 'https' ), 0 );
2021-02-23 22:03:23 +01:00
if ( $tmpgeturl [ 'curl_error_no' ]) {
2018-02-19 14:56:37 +01:00
$error ++ ;
setEventMessages ( 'Error getting ' . $urltograbbis . ': ' . $tmpgeturl [ 'curl_error_msg' ], null , 'errors' );
2019-11-13 19:37:08 +01:00
$action = 'create' ;
2021-02-23 22:03:23 +01:00
} elseif ( $tmpgeturl [ 'http_code' ] != '200' ) {
2018-02-19 14:56:37 +01:00
$error ++ ;
setEventMessages ( 'Error getting ' . $urltograbbis . ': ' . $tmpgeturl [ 'http_code' ], null , 'errors' );
2019-11-13 19:37:08 +01:00
$action = 'create' ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:37:08 +01:00
$alreadygrabbed [ $urltograbbis ] = 1 ; // Track that file was alreay grabbed.
2018-02-19 14:56:37 +01:00
dol_mkdir ( dirname ( $filetosave ));
$fp = fopen ( $filetosave , " w " );
fputs ( $fp , $tmpgeturl [ 'content' ]);
fclose ( $fp );
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> global -> MAIN_UMASK )) {
2018-02-19 14:56:37 +01:00
@ chmod ( $filetosave , octdec ( $conf -> global -> MAIN_UMASK ));
2021-02-23 22:03:23 +01:00
}
2018-02-19 14:56:37 +01:00
}
2017-10-07 02:29:27 +02:00
}
2017-10-06 23:54:45 +02:00
}
2021-02-23 22:03:23 +01:00
if ( $modifylinks ) {
2019-01-27 11:55:16 +01:00
$tmp = preg_replace ( '/' . preg_quote ( $regs [ 0 ][ $key ], '/' ) . '/i' , 'background' . $regs [ 1 ][ $key ] . 'url("' . DOL_URL_ROOT . '/viewimage.php?modulepart=medias&file=' . $filename . '")' , $tmp );
2017-10-06 23:54:45 +02:00
}
}
2017-12-19 11:40:29 +01:00
}