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 = '' )
{
$newstr = '' ;
//split on each opening tag
$parts = explode ( '<?php' , $str );
if ( ! empty ( $parts ))
{
$i = 0 ;
foreach ( $parts as $part )
{
if ( $i == 0 ) // The first part is never php code
{
$i ++ ;
$newstr .= $part ;
continue ;
}
// The second part is the php code. We split on closing tag
$partlings = explode ( '?>' , $part );
if ( ! empty ( $partlings ))
{
$phppart = $partlings [ 0 ];
//remove content before closing tag
if ( count ( $partlings ) > 1 ) $partlings [ 0 ] = '' ; // Todo why a count > 1 and not >= 1 ?
//append to out string
2019-09-10 15:58:56 +02:00
$newstr .= '<span class="phptag" class="tooltip" title="' . dol_escape_htmltag ( dolGetFirstLineOfText ( $phppart ) . '...' ) . '">' . $replacewith . '<!-- ' . $phppart . ' --></span>' . 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 )
{
$newstr = '' ;
//split on each opening tag
$parts = explode ( '<?php' , $str );
if ( ! empty ( $parts ))
{
2019-11-13 19:37:08 +01:00
$i = 0 ;
foreach ( $parts as $part )
2019-08-15 18:07:36 +02:00
{
if ( $i == 0 ) // The first part is never php code
{
$i ++ ;
continue ;
}
$newstr .= '<?php' ;
//split on closing tag
$partlings = explode ( '?>' , $part , 2 );
if ( ! empty ( $partlings ))
{
$newstr .= $partlings [ 0 ] . '?>' ;
}
else
{
$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 ;
2019-08-14 23:27:34 +02:00
dol_syslog ( 'dolWebsiteReplacementOfLinks start (contenttype=' . $contenttype . " containerid= " . $containerid . " USEDOLIBARREDITOR= " . ( defined ( 'USEDOLIBARREDITOR' ) ? '1' : '' ) . " USEDOLIBARRSERVER= " . ( defined ( 'USEDOLIBARRSERVER' ) ? '1' : '' ) . ')' , LOG_DEBUG );
//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.
2017-12-10 20:34:35 +01:00
$replacewith = '...php...' ;
if ( $removephppart ) $replacewith = '' ;
$content = preg_replace ( '/value="<\?php((?!\?>).)*\?>\n*/ims' , 'value="' . $replacewith . '"' , $content );
2017-11-05 03:57:26 +01:00
2018-07-23 14:08:20 +02:00
$replacewith = '"callto=#' ;
if ( $removephppart ) $replacewith = '' ;
$content = preg_replace ( '/"callto:<\?php((?!\?>).)*\?>\n*/ims' , $replacewith , $content );
$replacewith = '"mailto=#' ;
if ( $removephppart ) $replacewith = '' ;
$content = preg_replace ( '/"mailto:<\?php((?!\?>).)*\?>\n*/ims' , $replacewith , $content );
2018-07-24 01:10:38 +02:00
$replacewith = 'src="php' ;
if ( $removephppart ) $replacewith = '' ;
$content = preg_replace ( '/src="<\?php((?!\?>).)*\?>\n*/ims' , $replacewith , $content );
2018-09-26 15:20:37 +02:00
$replacewith = 'href="php' ;
if ( $removephppart ) $replacewith = '' ;
$content = preg_replace ( '/href="<\?php((?!\?>).)*\?>\n*/ims' , $replacewith , $content );
//$replacewith='<span class="phptag">...php...</span>';
2019-08-15 18:07:36 +02:00
$replacewith = '...php...' ;
2017-12-10 20:34:35 +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
2019-08-15 18:07:36 +02:00
$content = preg_replace ( '/(href=")\/\"/' , '\1!~!~!~' . DOL_URL_ROOT . '/website/index.php?website=' . $website -> ref . '&pageid=' . $website -> fk_default_home . '"' , $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/
2019-08-15 18:07:36 +02:00
$content = preg_replace ( '/url\((["\']?)medias\//' , 'url(\1!~!~!~' . DOL_URL_ROOT . '/viewimage.php?modulepart=medias&file=' , $content , - 1 , $nbrep );
$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="
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-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="
2019-08-15 18:07:36 +02:00
$content = preg_replace ( '/(url\(")(\/?viewimage\.php\?[^\"]*modulepart=[^\"]*)(\")/' , '\1!~!~!~' . DOL_URL_ROOT . '\2\3' , $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 );
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 .
2017-10-27 17:58:38 +02:00
* Used to ouput the page when viewed from 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 ;
2017-02-24 21:13:40 +01:00
2019-08-14 23:27:34 +02:00
dol_syslog ( " dolWebsiteOutput start (contenttype= " . $contenttype . " containerid= " . $containerid . " USEDOLIBARREDITOR= " . ( defined ( 'USEDOLIBARREDITOR' ) ? '1' : '' ) . " USEDOLIBARRSERVER= " . ( defined ( 'USEDOLIBARRSERVER' ) ? '1' : '' ) . ')' );
2017-07-03 02:09:14 +02:00
2017-10-07 13:09:31 +02:00
// Define $urlwithroot
2019-01-27 11:55:16 +01:00
$urlwithouturlroot = preg_replace ( '/' . preg_quote ( DOL_URL_ROOT , '/' ) . '$/i' , '' , trim ( $dolibarr_main_url_root ));
2017-10-07 13:09:31 +02:00
$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
2017-07-21 02:30:02 +02:00
2018-09-25 15:00:37 +02:00
if ( defined ( 'USEDOLIBARREDITOR' )) // REPLACEMENT OF LINKS When page called from Dolibarr editor
2018-09-24 17:12:51 +02:00
{
2018-09-25 15:00:37 +02:00
// We remove the <head> part of content
2019-08-13 06:37:06 +02:00
if ( $contenttype == 'html' )
{
$content = preg_replace ( '/<head>.*<\/head>/ims' , '' , $content );
$content = preg_replace ( '/^.*<body(\s[^>]*)*>/ims' , '' , $content );
$content = preg_replace ( '/<\/body(\s[^>]*)*>.*$/ims' , '' , $content );
}
2018-09-24 17:12:51 +02:00
}
2018-09-25 15:00:37 +02:00
elseif ( defined ( 'USEDOLIBARRSERVER' )) // REPLACEMENT OF LINKS When page called from Dolibarr server
2017-10-27 17:58:38 +02:00
{
global $website ;
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-02-21 11:36:16 +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
$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/
2019-08-15 18:07:36 +02:00
$content = preg_replace ( '/url\((["\']?)medias\//' , 'url(\1!~!~!~' . DOL_URL_ROOT . '/viewimage.php?modulepart=medias&file=' , $content , - 1 , $nbrep );
$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
2019-08-15 18:07:36 +02:00
// Remove the protection tag !~!~!~
$content = str_replace ( '!~!~!~' , '' , $content );
2017-10-27 17:58:38 +02:00
}
2018-09-25 15:00:37 +02:00
else // REPLACEMENT OF LINKS When page called from virtual host
2017-10-07 13:09:31 +02:00
{
$symlinktomediaexists = 1 ;
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" />
2017-10-07 13:09:31 +02:00
$nbrep = 0 ;
if ( ! $symlinktomediaexists )
{
2019-08-16 04:32:06 +02:00
// <img src="image.png... => <img src="medias/image.png...
$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 );
$content = preg_replace ( '/(<script[^>]*src=")[^\"]*document\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/' , '\1/wrapper.php\2modulepart=medias\3file=\4\5' , $content , - 1 , $nbrep );
2019-08-16 05:03:48 +02:00
$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
2018-09-09 14:05:23 +02: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 );
2019-02-09 12:00:49 +01:00
$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
2018-12-17 17:29:04 +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 );
2019-02-09 12:00:49 +01:00
$content = preg_replace ( '/(url\(["\']?)[^\)]*viewimage\.php([^\)]*)hashp=([^\)]*)(["\']?\))/' , '\1/wrapper.php\2hashp\3\4' , $content , - 1 , $nbrep );
2018-12-17 17:29:04 +01:00
2018-09-09 14:05:23 +02: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
$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 );
2017-10-07 13:09:31 +02:00
}
else
{
2019-08-16 04:32:06 +02:00
// <img src="image.png... => <img src="medias/image.png...
2019-08-16 05:32:24 +02: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
2019-08-16 05:32:24 +02: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
2019-08-16 05:32:24 +02: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
2018-12-17 17:29:04 +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-09-09 14:05:23 +02: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
$content = preg_replace ( '/(<img[^>]*src=")\/?viewimage\.php/' , '\1/wrapper.php' , $content , - 1 , $nbrep );
2019-08-16 05:33:45 +02:00
$content = preg_replace ( '/(<a[^>]*href=")\/?document\.php/' , '\1/wrapper.php' , $content , - 1 , $nbrep );
2017-10-07 13:09:31 +02:00
}
}
2018-06-11 10:25:22 +02:00
$content = preg_replace ( '/ contenteditable="true"/' , ' contenteditable="false"' , $content , - 1 , $nbrep );
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
if ( $containeraliasalt )
{
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 );
if ( $result > 0 )
{
$containerref = $tmpwebsitepage -> pageurl ;
}
else
{
print " Error, page contains a redirect to the alternative alias ' " . $containeraliasalt . " ' that does not exists in web site ( " . $website -> id . " / " . $website -> ref . " ) " ;
exit ;
}
}
2019-07-28 21:24:42 +02: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
}
2018-02-19 16:46:29 +01:00
if ( defined ( 'USEDOLIBARRSERVER' )) // When page called from Dolibarr server
{
// Check new container exists
2019-11-13 19:37:08 +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 );
}
2018-02-19 16:46:29 +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 ();
2018-02-19 22:10:15 +01:00
if ( preg_match ( '/&pageref=([^&]+)/' , $currenturi , $regtmp ))
{
2018-02-28 14:36:22 +01:00
if ( $regtmp [ 0 ] == $containerref )
2018-02-19 22:10:15 +01:00
{
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 ;
}
else
{
2018-02-28 14:36:22 +01:00
$newurl = preg_replace ( '/&pageref=([^&]+)/' , '&pageref=' . $containerref , $currenturi );
2018-02-19 22:10:15 +01: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
}
}
else // When page called from virtual host server
{
2018-02-28 14:36:22 +01:00
$newurl = '/' . $containerref . '.php' ;
2018-02-19 16:46:29 +01:00
}
if ( $newurl )
{
2019-12-07 11:51:21 +01:00
if ( $permanent ) {
header ( " Status: 301 Moved Permanently " , false , 301 );
}
2018-02-19 16:46:29 +01:00
header ( " Location: " . $newurl );
exit ;
}
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
{
2019-07-25 13:48:13 +02: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
$MAXLEVEL = 20 ;
2018-02-28 14:36:22 +01:00
if ( ! preg_match ( '/\.php$/i' , $containerref )) $containerref .= '.php' ;
2017-12-12 01:40:34 +01:00
2018-02-28 14:36:22 +01:00
$fullpathfile = DOL_DATA_ROOT . '/website/' . $websitekey . '/' . $containerref ;
2017-07-20 16:49:20 +02:00
if ( empty ( $includehtmlcontentopened )) $includehtmlcontentopened = 0 ;
$includehtmlcontentopened ++ ;
if ( $includehtmlcontentopened > $MAXLEVEL )
{
print 'ERROR: RECURSIVE CONTENT LEVEL. Depth of recursive call is more than the limit of ' . $MAXLEVEL . " . \n " ;
return ;
}
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 ();
print " \n " . '<!-- include ' . $fullpathfile . ' 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
2019-11-13 19:37:08 +01:00
if ( ! $res )
2017-07-20 16:49:20 +02:00
{
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
/**
* Return HTML content to add structured data for an article , news or Blog Post .
*
2019-07-25 14:39:22 +02:00
* @ param string $type 'blogpost' , 'product' , 'software' ...
* @ 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
{
2019-07-25 15:07:00 +02:00
global $conf , $db , $hookmanager , $langs , $mysoc , $user , $website , $websitepage , $weblangs ; // Very important. Required to have var available when running inluded containers.
2017-10-26 13:09:11 +02:00
2019-07-25 14:39:22 +02:00
if ( $type == 'software' )
{
$ret = '<!-- Add structured data for blog post -->' . " \n " ;
$ret .= '<script type="application/ld+json">' . " \n " ;
$ret .= ' {
" @context " : " https://schema.org " ,
" @type " : " SoftwareApplication " ,
" name " : " '. $data['name'] .' " ,
" operatingSystem " : " '. $data['os'] .' " ,
" applicationCategory " : " https://schema.org/GameApplication " ,
" aggregateRating " : {
" @type " : " AggregateRating " ,
" ratingValue " : " '. $data['ratingvalue'] .' " ,
" ratingCount " : " '. $data['ratingcount'] .' "
},
" offers " : {
" @type " : " Offer " ,
" price " : " '. $data['price'] .' " ,
" priceCurrency " : " '.( $data['currency'] ? $data['currency'] : $conf->currency ).' "
}
} ' . " \n " ;
$ret .= '</script>' . " \n " ;
}
elseif ( $type == 'blogpost' )
2019-07-25 13:48:13 +02:00
{
if ( $websitepage -> fk_user_creat > 0 )
{
include_once DOL_DOCUMENT_ROOT . '/user/class/user.class.php' ;
$tmpuser = new User ( $db );
$restmpuser = $tmpuser -> fetch ( $websitepage -> fk_user_creat );
if ( $restmpuser > 0 )
{
$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 " ,
2019-07-25 14:12:22 +02:00
" @id " : " '. $websitepage->pageurl .' "
2019-07-25 13:48:13 +02:00
},
" headline " : " '. $websitepage->title .' " ,
" image " : [
2019-07-25 14:12:22 +02:00
" '. $websitepage->image .' "
2019-07-25 13:48:13 +02:00
],
" datePublished " : " '.dol_print_date( $websitepage->date_creation , 'dayhourrfc').' " ,
" dateModified " : " '.dol_print_date( $websitepage->date_modification , 'dayhourrfc').' " ,
" author " : {
" @type " : " Person " ,
" name " : " '. $tmpuser->getFullName ( $weblangs ).' "
},
2019-07-25 14:21:39 +02:00
" publisher " : {
2019-07-25 14:27:53 +02:00
" @type " : " Organization " ,
" name " : " '. $mysoc->name .' " ,
" logo " : {
2019-07-25 14:26:48 +02:00
" @type " : " ImageObject " ,
" url " : " /viewimage.php?modulepart=mycompany&file=logos%2F'.urlencode( $mysoc->logo ).' "
2019-07-25 14:27:53 +02:00
}
2019-07-25 14:21:39 +02:00
},
2019-07-25 13:48:13 +02:00
" description " : " '. $websitepage->description .' "
} ' . " \n " ;
$ret .= '</script>' . " \n " ;
}
}
}
2019-07-25 14:39:22 +02:00
elseif ( $type == 'product' )
{
$ret = '<!-- Add structured data for blog post -->' . " \n " ;
$ret .= '<script type="application/ld+json">' . " \n " ;
$ret .= ' {
" @context " : " https://schema.org/ " ,
" @type " : " Product " ,
" name " : " '. $data['label'] .' " ,
" image " : [
" '. $data['image'] .' " ,
],
" description " : " '. $data['description'] .' " ,
" sku " : " '. $data['ref'] .' " ,
" brand " : {
" @type " : " Thing " ,
" name " : " '. $data['brand'] .' "
},
" author " : {
" @type " : " Person " ,
" name " : " '. $data['author'] .' "
}
},
" offers " : {
" @type " : " Offer " ,
" url " : " https://example.com/anvil " ,
" priceCurrency " : " '.( $data['currency'] ? $data['currency'] : $conf->currency ).' " ,
" price " : " '. $data['price'] .' " ,
" itemCondition " : " https://schema.org/UsedCondition " ,
" availability " : " https://schema.org/InStock " ,
" seller " : {
" @type " : " Organization " ,
" name " : " '. $mysoc->name .' "
}
}
} ' . " \n " ;
$ret .= '</script>' . " \n " ;
}
2019-07-25 13:48:13 +02:00
return $ret ;
}
2017-10-26 13:09:11 +02: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
*
* @ param string $type Type of container to search into ( Example : 'page' )
2019-09-02 16:39:56 +02:00
* @ param string $algo Algorithm used for search ( Example : 'meta' is searching into meta information like title and description , 'content' , 'sitefiles' , or any combination , ... )
2019-08-12 20:32:18 +02:00
* @ param string $searchstring Search string
* @ param int $max Max number of answers
* @ return string HTML content
*/
function getPagesFromSearchCriterias ( $type , $algo , $searchstring , $max = 25 )
{
global $conf , $db , $hookmanager , $langs , $mysoc , $user , $website , $websitepage , $weblangs ; // Very important. Required to have var available when running inluded containers.
$error = 0 ;
$arrayresult = array ( 'code' => '' , 'list' => array ());
2019-08-13 00:34:42 +02:00
if ( ! is_object ( $weblangs )) $weblangs = $langs ;
2019-08-12 20:32:18 +02:00
if ( empty ( $searchstring ))
{
$error ++ ;
$arrayresult [ 'code' ] = 'KO' ;
$arrayresult [ 'message' ] = $weblangs -> trans ( " EmptySearchString " );
}
elseif ( dol_strlen ( $searchstring ) < 2 )
{
2019-08-13 00:34:42 +02:00
$weblangs -> load ( " errors " );
2019-08-12 20:32:18 +02:00
$error ++ ;
$arrayresult [ 'code' ] = 'KO' ;
2019-08-13 00:34:42 +02:00
$arrayresult [ 'message' ] = $weblangs -> trans ( " ErrorSearchCriteriaTooSmall " );
2019-08-12 20:32:18 +02:00
}
2019-08-13 00:34:42 +02:00
elseif ( ! in_array ( $type , array ( '' , 'page' )))
2019-08-12 20:32:18 +02:00
{
$error ++ ;
$arrayresult [ 'code' ] = 'KO' ;
$arrayresult [ 'message' ] = 'Bad value for parameter $type' ;
}
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
2019-09-02 16:39:56 +02:00
if ( ! $error && ( empty ( $max ) || ( $found < $max )) && ( preg_match ( '/meta/' , $algo ) || preg_match ( '/content/' , $algo )))
2019-08-12 20:32:18 +02:00
{
$sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . 'website_page' ;
2019-08-13 00:34:42 +02:00
$sql .= " WHERE fk_website = " . $website -> id ;
if ( $type ) $sql .= " AND type_container = ' " . $db -> escape ( $type ) . " ' " ;
$sql .= " AND ( " ;
$searchalgo = '' ;
2019-09-02 16:39:56 +02:00
if ( preg_match ( '/meta/' , $algo ))
2019-08-13 00:34:42 +02:00
{
$searchalgo .= ( $searchalgo ? ' OR ' : '' ) . " title LIKE '% " . $db -> escape ( $searchstring ) . " %' OR description LIKE '% " . $db -> escape ( $searchstring ) . " %' " ;
$searchalgo .= ( $searchalgo ? ' OR ' : '' ) . " keywords LIKE ' " . $db -> escape ( $searchstring ) . " ,%' OR keywords LIKE '% " . $db -> escape ( $searchstring ) . " %' " ; // TODO Use a better way to scan keywords
}
2019-09-02 16:39:56 +02:00
if ( preg_match ( '/content/' , $algo ))
2019-08-13 00:34:42 +02:00
{
$searchalgo .= ( $searchalgo ? ' OR ' : '' ) . " content LIKE '% " . $db -> escape ( $searchstring ) . " %' " ;
}
$sql .= $searchalgo ;
$sql .= " ) " ;
2019-08-12 20:32:18 +02:00
$sql .= $db -> plimit ( $max );
$resql = $db -> query ( $sql );
if ( $resql )
{
$i = 0 ;
while (( $obj = $db -> fetch_object ( $resql )) && ( $i < $max || $max == 0 ))
{
if ( $obj -> rowid > 0 )
{
$tmpwebsitepage = new WebsitePage ( $db );
$tmpwebsitepage -> fetch ( $obj -> rowid );
if ( $tmpwebsitepage -> id > 0 ) $arrayresult [ 'list' ][] = $tmpwebsitepage ;
2019-09-02 16:39:56 +02:00
$found ++ ;
2019-08-12 20:32:18 +02:00
}
$i ++ ;
}
2019-09-02 16:39:56 +02:00
}
else
{
$error ++ ;
$arrayresult [ 'code' ] = $db -> lasterrno ();
$arrayresult [ 'message' ] = $db -> lasterror ();
}
2019-08-12 20:32:18 +02:00
2019-09-02 16:39:56 +02:00
$searchdone = 1 ;
}
if ( ! $error && ( empty ( $max ) || ( $found < $max )) && ( preg_match ( '/sitefiles/' , $algo )))
{
2019-09-02 18:43:22 +02:00
global $dolibarr_main_data_root ;
$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' ;
$filecontent = file_get_contents ( $filehtmlheader );
if (( empty ( $max ) || ( $found < $max )) && preg_match ( '/' . preg_quote ( $searchstring , '/' ) . '/' , $filecontent ))
{
$arrayresult [ 'list' ][] = array ( 'type' => 'website_htmlheadercontent' );
}
2019-09-02 16:39:56 +02:00
2019-09-02 18:43:22 +02:00
$filecontent = file_get_contents ( $filecss );
if (( empty ( $max ) || ( $found < $max )) && preg_match ( '/' . preg_quote ( $searchstring , '/' ) . '/' , $filecontent ))
{
$arrayresult [ 'list' ][] = array ( 'type' => 'website_csscontent' );
}
2019-09-02 16:39:56 +02:00
2019-09-02 18:43:22 +02:00
$filecontent = file_get_contents ( $filejs );
if (( empty ( $max ) || ( $found < $max )) && preg_match ( '/' . preg_quote ( $searchstring , '/' ) . '/' , $filecontent ))
2019-09-02 16:39:56 +02:00
{
2019-09-02 18:43:22 +02: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 );
if (( empty ( $max ) || ( $found < $max )) && preg_match ( '/' . preg_quote ( $searchstring , '/' ) . '/' , $filecontent ))
2019-08-12 20:32:18 +02:00
{
2019-09-02 18:43:22 +02: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 ;
}
2019-09-02 16:39:56 +02:00
if ( ! $error )
2019-08-13 00:34:42 +02:00
{
2019-09-02 16:39:56 +02:00
if ( $searchdone )
{
$arrayresult [ 'code' ] = 'OK' ;
if ( empty ( $arrayresult [ 'list' ]))
{
$arrayresult [ 'code' ] = 'KO' ;
$arrayresult [ 'message' ] = $weblangs -> trans ( " NoRecordFound " );
}
}
else
{
$error ++ ;
$arrayresult [ 'code' ] = 'KO' ;
$arrayresult [ 'message' ] = 'No supported algorithm found' ;
}
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
2019-11-13 19:37:08 +01:00
if ( preg_match ( '/\/$/' , $urltograb )) $urltograb .= '.' ;
$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"
2017-10-07 02:29:27 +02:00
preg_match_all ( '/<img([^\.\/]+)src="([^>"]+)"([^>]*)>/i' , $tmp , $regs );
2017-10-06 23:54:45 +02:00
foreach ( $regs [ 0 ] as $key => $val )
{
2019-11-13 19:37:08 +01:00
if ( preg_match ( '/^data:image/i' , $regs [ 2 ][ $key ])) continue ; // We do nothing for such images
2017-10-07 02:40:03 +02:00
2017-12-10 17:59:19 +01:00
if ( preg_match ( '/^\//' , $regs [ 2 ][ $key ]))
{
$urltograbdirrootwithoutslash = getRootURLFromURL ( $urltograb );
2019-11-13 19:37:08 +01:00
$urltograbbis = $urltograbdirrootwithoutslash . $regs [ 2 ][ $key ]; // We use dirroot
2017-12-10 17:59:19 +01: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 ;
2019-11-13 19:37:08 +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 ];
2017-10-06 23:54:45 +02:00
if ( preg_match ( '/^http/' , $regs [ 2 ][ $key ]))
{
$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;
if ( empty ( $alreadygrabbed [ $urltograbbis ]))
{
2018-02-19 14:56:37 +01:00
if ( $grabimages )
2017-10-07 02:29:27 +02:00
{
2018-02-19 14:56:37 +01:00
$tmpgeturl = getURLContent ( $urltograbbis );
if ( $tmpgeturl [ 'curl_error_no' ])
{
$error ++ ;
setEventMessages ( 'Error getting ' . $urltograbbis . ': ' . $tmpgeturl [ 'curl_error_msg' ], null , 'errors' );
2019-11-13 19:37:08 +01:00
$action = 'create' ;
2018-02-19 14:56:37 +01:00
}
elseif ( $tmpgeturl [ 'http_code' ] != '200' )
{
$error ++ ;
setEventMessages ( 'Error getting ' . $urltograbbis . ': ' . $tmpgeturl [ 'http_code' ], null , 'errors' );
2019-11-13 19:37:08 +01:00
$action = 'create' ;
2018-02-19 14:56:37 +01: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 );
2019-11-13 19:37:08 +01:00
if ( ! empty ( $conf -> global -> MAIN_UMASK ))
2018-02-19 14:56:37 +01:00
@ chmod ( $filetosave , octdec ( $conf -> global -> MAIN_UMASK ));
}
2017-10-07 02:29:27 +02: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 );
foreach ( $regs [ 0 ] as $key => $val )
{
2019-11-13 19:37:08 +01:00
if ( preg_match ( '/^data:image/i' , $regs [ 2 ][ $key ])) continue ; // We do nothing for such images
2017-10-07 02:40:03 +02:00
2017-12-10 17:59:19 +01:00
if ( preg_match ( '/^\//' , $regs [ 2 ][ $key ]))
{
$urltograbdirrootwithoutslash = getRootURLFromURL ( $urltograb );
2019-11-13 19:37:08 +01:00
$urltograbbis = $urltograbdirrootwithoutslash . $regs [ 2 ][ $key ]; // We use dirroot
2017-12-10 17:59:19 +01: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 ;
2019-11-13 19:37:08 +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
if ( preg_match ( '/^http/' , $regs [ 2 ][ $key ]))
2017-10-06 23:54:45 +02:00
{
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;
if ( empty ( $alreadygrabbed [ $urltograbbis ]))
2017-10-06 23:54:45 +02:00
{
2018-02-19 14:56:37 +01:00
if ( $grabimages )
2017-12-10 17:59:19 +01:00
{
2018-02-19 14:56:37 +01:00
$tmpgeturl = getURLContent ( $urltograbbis );
if ( $tmpgeturl [ 'curl_error_no' ])
{
$error ++ ;
setEventMessages ( 'Error getting ' . $urltograbbis . ': ' . $tmpgeturl [ 'curl_error_msg' ], null , 'errors' );
2019-11-13 19:37:08 +01:00
$action = 'create' ;
2018-02-19 14:56:37 +01:00
}
elseif ( $tmpgeturl [ 'http_code' ] != '200' )
{
$error ++ ;
setEventMessages ( 'Error getting ' . $urltograbbis . ': ' . $tmpgeturl [ 'http_code' ], null , 'errors' );
2019-11-13 19:37:08 +01:00
$action = 'create' ;
2018-02-19 14:56:37 +01: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 );
2019-11-13 19:37:08 +01:00
if ( ! empty ( $conf -> global -> MAIN_UMASK ))
2018-02-19 14:56:37 +01:00
@ chmod ( $filetosave , octdec ( $conf -> global -> MAIN_UMASK ));
}
2017-10-07 02:29:27 +02:00
}
2017-10-06 23:54:45 +02: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
}