Completed
Branch develop (e6f0e7)
by
unknown
24:49
created

website.lib.php ➔ exportWebSite()   C

Complexity

Conditions 9
Paths 80

Size

Total Lines 86
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 57
nc 80
nop 1
dl 0
loc 86
rs 5.3477
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
 * @see	dolWebsiteSaveContent
31
 */
32
function dolWebsiteOutput($content)
33
{
34
    global $db, $langs, $conf, $user;
35
    global $dolibarr_main_url_root, $dolibarr_main_data_root;
36
37
    dol_syslog("dolWebsiteOutput start (mode=".(defined('USEDOLIBARRSERVER')?'USEDOLIBARRSERVER':'').')');
38
39
    // Define $urlwithroot
40
    $urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
41
    $urlwithroot=$urlwithouturlroot.DOL_URL_ROOT;		// This is to use external domain name found into config file
42
    //$urlwithroot=DOL_MAIN_URL_ROOT;					// This is to use same domain name than current
43
44
    // Note: This seems never called when page is output inside the website editor (search 'REPLACEMENT OF LINKS When page called by website editor')
45
46
    if (! defined('USEDOLIBARRSERVER'))	// REPLACEMENT OF LINKS When page called from virtual host
47
    {
48
        $symlinktomediaexists=1;
49
50
		// Make a change into HTML code to allow to include images from medias directory correct with direct link for virtual server
51
		// <img alt="" src="/dolibarr_dev/htdocs/viewimage.php?modulepart=medias&amp;entity=1&amp;file=image/ldestailleur_166x166.jpg" style="height:166px; width:166px" />
52
		// become
53
		// <img alt="" src="'.$urlwithroot.'/medias/image/ldestailleur_166x166.jpg" style="height:166px; width:166px" />
54
        $nbrep=0;
55
        if (! $symlinktomediaexists)
56
        {
57
            $content=preg_replace('/(<img.*src=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^\/]*\/>)/', '\1'.$urlwithroot.'/viewimage.php\2modulepart=medias\3file=\4\5', $content, -1, $nbrep);
58
            $content=preg_replace('/(url\(["\']?)[^\)]*viewimage\.php([^\)]*)modulepart=medias([^\)]*)file=([^\)]*)(["\']?\))/',  '\1'.$urlwithroot.'/viewimage.php\2modulepart=medias\3file=\4\5', $content, -1, $nbrep);
59
        }
60
        else
61
        {
62
        	$content=preg_replace('/(<img.*src=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^\/]*\/>)/', '\1medias/\4\5', $content, -1, $nbrep);
63
            $content=preg_replace('/(url\(["\']?)[^\)]*viewimage\.php([^\)]*)modulepart=medias([^\)]*)file=([^\)]*)(["\']?\))/', '\1medias/\4\5', $content, -1, $nbrep);
64
        }
65
    }
66
    else								// REPLACEMENT OF LINKS When page called from dolibarr server
67
    {
68
    	global $website;
69
70
    	// Replace relative link / with dolibarr URL:  ...href="/"...
71
    	$content=preg_replace('/(href=")\/\"/', '\1'.DOL_URL_ROOT.'/public/websites/index.php?website='.$website->ref.'&pageid='.$website->fk_default_home.'"', $content, -1, $nbrep);
72
    	// Replace relative link /xxx.php with dolibarr URL:  ...href="....php"
73
    	$content=preg_replace('/(href=")\/?([^\"]*)(\.php\")/', '\1'.DOL_URL_ROOT.'/public/websites/index.php?website='.$website->ref.'&pageref=\2"', $content, -1, $nbrep);
74
75
    	// Fix relative link /document.php with correct URL after the DOL_URL_ROOT:  ...href="/document.php?modulepart="
76
    	$content=preg_replace('/(href=")(\/?document\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1'.DOL_URL_ROOT.'\2\3"', $content, -1, $nbrep);
77
    	// Fix relative link /viewimage.php with correct URL after the DOL_URL_ROOT:  ...href="/viewimage.php?modulepart="
78
    	$content=preg_replace('/(href=")(\/?viewimage\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1'.DOL_URL_ROOT.'\2\3"', $content, -1, $nbrep);
79
80
    	// Fix relative link into medias with correct URL after the DOL_URL_ROOT: ../url("medias/
81
    	$content=preg_replace('/url\((["\']?)medias\//', 'url(\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
82
    }
83
84
    dol_syslog("dolWebsiteOutput end");
85
86
    print $content;
87
}
88
89
90
/**
91
 * Convert a page content to have correct links into a new html content.
92
 * Used to ouput the page on the Preview.
93
 *
94
 * @param	Website		$website			Web site object
95
 * @param	string		$content			Content to replace
96
 * @return	boolean							True if OK
97
 */
98
function dolWebsiteReplacementOfLinks($website, $content)
99
{
100
	// Replace php code. Note $content may come from database and does not contains body tags.
101
	$content = preg_replace('/<\?php[^\?]+\?>\n*/ims', '<span style="background: #ddd; border: 1px solid #ccc; border-radius: 4px;">...php...</span>', $content);
102
103
	// Replace relative link / with dolibarr URL
104
	$content = preg_replace('/(href=")\/\"/', '\1'.DOL_URL_ROOT.'/websites/index.php?website='.$website->ref.'&pageid='.$website->fk_default_home.'"', $content, -1, $nbrep);
105
	// Replace relative link /xxx.php with dolibarr URL
106
	$content = preg_replace('/(href=")\/?([^\"]*)(\.php\")/', '\1'.DOL_URL_ROOT.'/websites/index.php?website='.$website->ref.'&pageref=\2"', $content, -1, $nbrep);
107
108
	$content = preg_replace('/url\((["\']?)medias\//', 'url(\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
109
110
	// <img src="image.png... => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png...
111
	$content = preg_replace('/(<img.*src=")(?!(http|'.preg_quote(DOL_URL_ROOT,'/').'\/viewimage))/', '\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
112
113
	return $content;
114
}
115
116
117
/**
118
 * Format img tags to introduce viewimage on img src.
119
 *
120
 * @param   string  $content    Content string
121
 * @return  void
122
 * @see	dolWebsiteOutput
123
 */
124
/*
125
function dolWebsiteSaveContent($content)
126
{
127
	global $db, $langs, $conf, $user;
128
	global $dolibarr_main_url_root, $dolibarr_main_data_root;
129
130
	//dol_syslog("dolWebsiteSaveContent start (mode=".(defined('USEDOLIBARRSERVER')?'USEDOLIBARRSERVER':'').')');
131
132
	// Define $urlwithroot
133
	$urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
134
	$urlwithroot=$urlwithouturlroot.DOL_URL_ROOT;		// This is to use external domain name found into config file
135
	//$urlwithroot=DOL_MAIN_URL_ROOT;					// This is to use same domain name than current
136
137
	//$content = preg_replace('/(<img.*src=")(?!(http|'.preg_quote(DOL_URL_ROOT,'/').'\/viewimage))/', '\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
138
139
	return $content;
140
}
141
*/
142
143
144
/**
145
 * Clean an HTML page to report only content, so we can include it into another page.
146
 * It outputs content of file sanitized from html and body part.
147
 *
148
 * @param 	string	$contentfile		Path to file to include (must include website root. Example: 'mywebsite/mypage.php')
149
 * @return  void
150
 */
151
function dolIncludeHtmlContent($contentfile)
152
{
153
	global $conf, $db, $langs, $mysoc, $user, $website;
154
	global $includehtmlcontentopened;
155
156
	$MAXLEVEL=20;
157
158
	$fullpathfile=DOL_DATA_ROOT.'/websites/'.$contentfile;
159
160
	if (empty($includehtmlcontentopened)) $includehtmlcontentopened=0;
161
	$includehtmlcontentopened++;
162
	if ($includehtmlcontentopened > $MAXLEVEL)
163
	{
164
		print 'ERROR: RECURSIVE CONTENT LEVEL. Depth of recursive call is more than the limit of '.$MAXLEVEL.".\n";
165
		return;
166
	}
167
	// file_get_contents is not possible. We must execute code with include
168
	//$content = file_get_contents($fullpathfile);
169
	//print preg_replace(array('/^.*<body[^>]*>/ims','/<\/body>.*$/ims'), array('', ''), $content);*/
170
171
	ob_start();
172
	$res = include $fullpathfile;		// Include because we want to execute code content
173
	$tmpoutput = ob_get_contents();
174
	ob_end_clean();
175
176
	print "\n".'<!-- include '.$fullpathfile.' level = '.$includehtmlcontentopened.' -->'."\n";
177
	print preg_replace(array('/^.*<body[^>]*>/ims','/<\/body>.*$/ims'), array('', ''), $tmpoutput);
178
179
	if (! $res)
180
	{
181
		print 'ERROR: FAILED TO INCLUDE PAGE '.$contentfile.".\n";
182
	}
183
184
	$includehtmlcontentopened--;
185
}
186
187
/**
188
 * Generate a zip with all data of web site.
189
 *
190
 * @param 	Website		$website		Object website
191
 * @return  void
192
 */
193
function exportWebSite($website)
194
{
195
	global $db, $conf;
196
197
	dol_mkdir($conf->websites->dir_temp);
198
	$srcdir = $conf->websites->dir_output.'/'.$website->ref;
199
	$destdir = $conf->websites->dir_temp.'/'.$website->ref;
200
201
	$arrayreplacement=array();
202
203
	dolCopyDir($srcdir, $destdir, 0, 1, $arrayreplacement);
204
205
	$srcdir = DOL_DATA_ROOT.'/medias/images/'.$website->ref;
206
	$destdir = $conf->websites->dir_temp.'/'.$website->ref.'/medias/images/'.$website->ref;
207
208
	dolCopyDir($srcdir, $destdir, 0, 1, $arrayreplacement);
209
210
	// Build sql file
211
	dol_mkdir($conf->websites->dir_temp.'/'.$website->ref.'/export');
212
213
	$filesql = $conf->websites->dir_temp.'/'.$website->ref.'/export/pages.sql';
214
	$fp = fopen($filesql,"w");
215
216
	$objectpages = new WebsitePage($db);
217
	$listofpages = $objectpages->fetchAll($website->id);
218
219
	// Assign ->newid and ->newfk_page
220
	$i=1;
221
	foreach($listofpages as $pageid => $objectpageold)
222
	{
223
		$objectpageold->newid=$i;
224
		$i++;
225
	}
226
	$i=1;
227
	foreach($listofpages as $pageid => $objectpageold)
228
	{
229
		// Search newid
230
		$newfk_page=0;
231
		foreach($listofpages as $pageid2 => $objectpageold2)
232
		{
233
			if ($pageid2 == $objectpageold->fk_page)
234
			{
235
				$newfk_page = $objectpageold2->newid;
236
				break;
237
			}
238
		}
239
		$objectpageold->newfk_page=$newfk_page;
240
		$i++;
241
	}
242
	foreach($listofpages as $pageid => $objectpageold)
243
	{
244
		$line = 'INSERT INTO llx_website_page(rowid, fk_page, fk_website, pageurl, title, description, keyword, status, date_creation, tms, lang, import_key, grabbed_from, content)';
245
		$line.= " VALUES(";
246
		$line.= $objectpageold->newid."+__MAXROWID__, ";
247
		$line.= ($objectpageold->newfk_page ? $db->escape($objectpageold->newfk_page)."+__MAXROWID__" : "null").", ";
248
		$line.= "__WEBSITE_ID__, ";
249
		$line.= "'".$db->escape($objectpageold->pageurl)."', ";
250
		$line.= "'".$db->escape($objectpageold->title)."', ";
251
		$line.= "'".$db->escape($objectpageold->description)."', ";
252
		$line.= "'".$db->escape($objectpageold->keyword)."', ";
253
		$line.= "'".$db->escape($objectpageold->status)."', ";
254
		$line.= "'".$db->idate($objectpageold->date_creation)."', ";
255
		$line.= "'".$db->idate($objectpageold->date_modification)."', ";
256
		$line.= "'".$db->escape($objectpageold->lang)."', ";
257
		$line.= ($objectpageold->import_key ? "'".$db->escape($objectpageold->import_key)."'" : "null").", ";
258
		$line.= "'".$db->escape($objectpageold->grabbed_from)."', ";
259
		$line.= "'".$db->escape($objectpageold->content)."'";
260
		$line.= ");";
261
		$line.= "\n";
262
		fputs($fp, $line);
263
	}
264
265
	fclose($fp);
266
	if (! empty($conf->global->MAIN_UMASK))
267
		@chmod($filesql, octdec($conf->global->MAIN_UMASK));
268
269
	// Build zip file
270
	$filedir = $conf->websites->dir_temp.'/'.$website->ref;
271
	$fileglob = $conf->websites->dir_temp.'/'.$website->ref.'/export/'.$website->ref.'_export_*.zip';
272
	$filename = $conf->websites->dir_temp.'/'.$website->ref.'/export/'.$website->ref.'_export_'.dol_print_date(dol_now(),'dayhourlog').'.zip';
273
274
	dol_delete_file($fileglob, 0);
275
	dol_compress_file($filedir, $filename, 'zip');
276
277
	return $filename;
278
}
279
280