Completed
Branch develop (b0e62f)
by
unknown
27:54
created

website.lib.php ➔ dolWebsiteOutput()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 56
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 24
nc 3
nop 1
dl 0
loc 56
rs 9.0544
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* Copyright (C) 2017 Laurent Destailleur	<[email protected]>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
/**
19
 *      \file       htdocs/core/lib/website.lib.php
20
 *      \ingroup    website
21
 *      \brief      Library for website module
22
 */
23
24
25
/**
26
 * Render a string of an HTML content and output it.
27
 *
28
 * @param   string  $content    Content string
29
 * @return  void
30
 */
31
function dolWebsiteOutput($content)
32
{
33
    global $db, $langs, $conf, $user;
34
    global $dolibarr_main_url_root, $dolibarr_main_data_root;
35
36
    dol_syslog("dolWebsiteOutput start (mode=".(defined('USEDOLIBARRSERVER')?'USEDOLIBARRSERVER':'').')');
37
38
    // Define $urlwithroot
39
    $urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
40
    $urlwithroot=$urlwithouturlroot.DOL_URL_ROOT;		// This is to use external domain name found into config file
41
    //$urlwithroot=DOL_MAIN_URL_ROOT;					// This is to use same domain name than current
42
43
    // Note: This seems never called when page is output inside the website editor (search 'REPLACEMENT OF LINKS When page called by website editor')
44
45
    if (! defined('USEDOLIBARRSERVER'))	// REPLACEMENT OF LINKS When page called from virtual host
46
    {
47
        $symlinktomediaexists=1;
48
49
		// Make a change into HTML code to allow to include images from medias directory correct with direct link for virtual server
50
		// <img alt="" src="/dolibarr_dev/htdocs/viewimage.php?modulepart=medias&amp;entity=1&amp;file=image/ldestailleur_166x166.jpg" style="height:166px; width:166px" />
51
		// become
52
		// <img alt="" src="'.$urlwithroot.'/medias/image/ldestailleur_166x166.jpg" style="height:166px; width:166px" />
53
        $nbrep=0;
54
        if (! $symlinktomediaexists)
55
        {
56
            $content=preg_replace('/(<img.*src=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^\/]*\/>)/', '\1'.$urlwithroot.'/viewimage.php\2modulepart=medias\3file=\4\5', $content, -1, $nbrep);
57
            $content=preg_replace('/(url\(["\']?)[^\)]*viewimage\.php([^\)]*)modulepart=medias([^\)]*)file=([^\)]*)(["\']?\))/',  '\1'.$urlwithroot.'/viewimage.php\2modulepart=medias\3file=\4\5', $content, -1, $nbrep);
58
        }
59
        else
60
        {
61
        	$content=preg_replace('/(<img.*src=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^\/]*\/>)/', '\1medias/\4\5', $content, -1, $nbrep);
62
            $content=preg_replace('/(url\(["\']?)[^\)]*viewimage\.php([^\)]*)modulepart=medias([^\)]*)file=([^\)]*)(["\']?\))/', '\1medias/\4\5', $content, -1, $nbrep);
63
        }
64
    }
65
    else								// REPLACEMENT OF LINKS When page called from dolibarr server
66
    {
67
    	global $website;
68
69
    	// Replace relative link / with dolibarr URL:  ...href="/"...
70
    	$content=preg_replace('/(href=")\/\"/', '\1'.DOL_URL_ROOT.'/public/websites/index.php?website='.$website->ref.'&pageid='.$website->fk_default_home.'"', $content, -1, $nbrep);
71
    	// Replace relative link /xxx.php with dolibarr URL:  ...href="....php"
72
    	$content=preg_replace('/(href=")\/?([^\"]*)(\.php\")/', '\1'.DOL_URL_ROOT.'/public/websites/index.php?website='.$website->ref.'&pageref=\2"', $content, -1, $nbrep);
73
74
    	// Fix relative link /document.php with correct URL after the DOL_URL_ROOT:  ...href="/document.php?modulepart="
75
    	$content=preg_replace('/(href=")(\/?document\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1'.DOL_URL_ROOT.'\2\3"', $content, -1, $nbrep);
76
    	// Fix relative link /viewimage.php with correct URL after the DOL_URL_ROOT:  ...href="/viewimage.php?modulepart="
77
    	$content=preg_replace('/(href=")(\/?viewimage\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1'.DOL_URL_ROOT.'\2\3"', $content, -1, $nbrep);
78
79
    	// Fix relative link into medias with correct URL after the DOL_URL_ROOT: ../url("medias/
80
    	$content=preg_replace('/url\((["\']?)medias\//', 'url(\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
81
    }
82
83
    dol_syslog("dolWebsiteOutput end");
84
85
    print $content;
86
}
87
88
89
/**
90
 * Clean an HTML page to report only content, so we can include it into another page
91
 * It outputs content of file sanitized from html and body part.
92
 *
93
 * @param 	string	$contentfile		Path to file to include (must include website root. Example: 'mywebsite/mypage.php')
94
 * @return  void
95
 */
96
function dolIncludeHtmlContent($contentfile)
97
{
98
	global $conf, $db, $langs, $mysoc, $user, $website;
99
	global $includehtmlcontentopened;
100
101
	$MAXLEVEL=20;
102
103
	$fullpathfile=DOL_DATA_ROOT.'/websites/'.$contentfile;
104
	//$content = file_get_contents($fullpathfile);
105
	//print preg_replace(array('/^.*<body[^>]*>/ims','/<\/body>.*$/ims'), array('', ''), $content);*/
106
107
	if (empty($includehtmlcontentopened)) $includehtmlcontentopened=0;
108
	$includehtmlcontentopened++;
109
	if ($includehtmlcontentopened > $MAXLEVEL)
110
	{
111
		print 'ERROR: RECURSIVE CONTENT LEVEL. Depth of recursive call is more than the limit of '.$MAXLEVEL.".\n";
112
		return;
113
	}
114
	$res = include $fullpathfile;		// Include because we want to execute code content
115
	if (! $res)
116
	{
117
		print 'ERROR: FAILED TO INCLUDE PAGE '.$contentfile.".\n";
118
	}
119
120
	$includehtmlcontentopened--;
121
}
122
123