FIX empty page due to preg_replace JIT error on large strings

This commit is contained in:
Laurent Destailleur 2018-11-27 16:20:31 +01:00
parent e95c6091c4
commit 70bd931d08
2 changed files with 54 additions and 5 deletions

View File

@ -35,6 +35,8 @@
*/
function dolWebsiteReplacementOfLinks($website, $content, $removephppart=0)
{
$nbrep = 0;
// Replace php code. Note $content may come from database and does not contains body tags.
$replacewith='...php...';
if ($removephppart) $replacewith='';
@ -59,7 +61,12 @@ function dolWebsiteReplacementOfLinks($website, $content, $removephppart=0)
//$replacewith='<span class="phptag">...php...</span>';
$replacewith='<span class="phptag">...php...</span>';
if ($removephppart) $replacewith='';
$content = preg_replace('/<\?php((?!\?>).)*\?>\n*/ims', $replacewith, $content);
//$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);
// Replace relative link / with dolibarr URL
$content = preg_replace('/(href=")\/\"/', '\1'.DOL_URL_ROOT.'/website/index.php?website='.$website->ref.'&pageid='.$website->fk_default_home.'"', $content, -1, $nbrep);
@ -88,6 +95,45 @@ function dolWebsiteReplacementOfLinks($website, $content, $removephppart=0)
}
/**
* 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
*/
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;
}
//split on closing tag
$partlings = explode('?>', $part);
if (!empty($partlings))
{
//remove content before closing tag
if (count($partlings) > 1) $partlings[0] = '';
//append to out string
$newstr .= $replacewith.implode('',$partlings);
}
}
}
return $newstr;
}
/**
* Render a string of an HTML content and output it.
* Used to ouput the page when viewed from server (Dolibarr or Apache).

View File

@ -1647,7 +1647,7 @@ print '<div class="centpercent websitebar">';
if (count($object->records) > 0) // There is at least one web site
{
// ***** Part for web sites
print '<!-- Bar for website -->';
print '<div class="websiteselection hideonsmartphoneimp minwidth100 tdoverflowmax100">';
print $langs->trans("Website").' : ';
print '</div>';
@ -1807,6 +1807,7 @@ if (count($object->records) > 0) // There is at least one web site
{
print '</div>'; // Close current websitebar to open a new one
print '<!-- Bar for websitepage -->';
print '<div class="centpercent websitebar"'.($style?' style="'.$style.'"':'').'">';
print '<div class="websiteselection hideonsmartphoneimp minwidth100 tdoverflowmax100">';
@ -2732,7 +2733,7 @@ if ($action == 'preview' || $action == 'createfromclone' || $action == 'createpa
$objectpage->fetch($pageid);
$jscontent = @file_get_contents($filejs);
$out = '<!-- Page content '.$filetpl.' : Div with (CSS Of website from file + Style/htmlheader of page from database + Page content from database or by include if WEBSITE_SUBCONTAINERSINLINE is on) -->'."\n";
$out = '<!-- Page content '.$filetpl.' : Div with (Htmlheader/Style of page from database + CSS Of website from file + Page content from database or by include if WEBSITE_SUBCONTAINERSINLINE is on) -->'."\n";
// Include a html so we can benefit of the header of page.
// Note: We can't use iframe as it can be used to include another external html file
@ -2742,7 +2743,8 @@ if ($action == 'preview' || $action == 'createfromclone' || $action == 'createpa
$out .= "<iframe><body></html>";
}*/
$out.="\n<html><head>\n";
$out.=dolWebsiteReplacementOfLinks($object, $objectpage->htmlheader, 1);
$out.="<!-- htmlheader/style of page from database -->\n";
// $out.=dolWebsiteReplacementOfLinks($object, $objectpage->htmlheader, 1);
$out.="</head>\n";
$out.="\n<body>";
@ -2751,6 +2753,7 @@ if ($action == 'preview' || $action == 'createfromclone' || $action == 'createpa
// REPLACEMENT OF LINKS When page called by website editor
$out.='<!-- style of website from file -->'."\n";
$out.='<style scoped>'."\n"; // "scoped" means "apply to parent element only". No more supported by browsers, snif !
$tmpout='';
$tmpout.= '/* Include website CSS file */'."\n";
@ -2806,8 +2809,8 @@ if ($action == 'preview' || $action == 'createfromclone' || $action == 'createpa
{
// TODO Add the contenteditable="true" when mode Edit Inline is on
}
$out.=dolWebsiteReplacementOfLinks($object, $newcontent)."\n";
//$out.=$newcontent;
$out.='</div>';