Passed
Branch develop (8190a6)
by
unknown
25:05
created

dolSavePageContent()   F

Complexity

Conditions 20
Paths 3104

Size

Total Lines 101
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 20
eloc 70
nc 3104
nop 3
dl 0
loc 101
rs 0
c 2
b 0
f 0

How to fix   Long Method    Complexity   

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 <https://www.gnu.org/licenses/>.
16
 */
17
18
/**
19
 *      \file       htdocs/core/lib/website2.lib.php
20
 *      \ingroup    website
21
 *      \brief      Library for website module (rare functions not required for execution of website)
22
 */
23
24
25
26
/**
27
 * Save content of a page on disk
28
 *
29
 * @param	string		$filemaster			Full path of filename master.inc.php for website to generate
30
 * @return	boolean							True if OK
31
 */
32
function dolSaveMasterFile($filemaster)
33
{
34
	global $conf;
35
36
	// Now generate the master.inc.php page
37
	dol_syslog("We regenerate the master file");
38
	dol_delete_file($filemaster);
39
40
	$mastercontent = '<?php'."\n";
41
	$mastercontent .= '// File generated to link to the master file - DO NOT MODIFY - It is just an include'."\n";
42
	$mastercontent .= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) require_once '".DOL_DOCUMENT_ROOT."/master.inc.php';\n";
43
	$mastercontent .= '?>'."\n";
44
	$result = file_put_contents($filemaster, $mastercontent);
45
	if (!empty($conf->global->MAIN_UMASK))
46
		@chmod($filemaster, octdec($conf->global->MAIN_UMASK));
47
48
		return $result;
49
}
50
51
/**
52
 * Save content of a page on disk
53
 *
54
 * @param	string		$filealias			Full path of filename to generate
55
 * @param	Website		$object				Object website
56
 * @param	WebsitePage	$objectpage			Object websitepage
57
 * @return	boolean							True if OK
58
 */
59
function dolSavePageAlias($filealias, $object, $objectpage)
60
{
61
	global $conf;
62
63
	// Now create the .tpl file (duplicate code with actions updatesource or updatecontent but we need this to save new header)
64
	dol_syslog("dolSavePageAlias We regenerate the alias page filealias=".$filealias);
65
66
	$aliascontent = '<?php'."\n";
67
	$aliascontent .= "// File generated to wrap the alias page - DO NOT MODIFY - It is just a wrapper to real page\n";
68
	$aliascontent .= 'global $dolibarr_main_data_root;'."\n";
69
	$aliascontent .= 'if (empty($dolibarr_main_data_root)) require \'./page'.$objectpage->id.'.tpl.php\'; ';
70
	$aliascontent .= 'else require $dolibarr_main_data_root.\'/website/\'.$website->ref.\'/page'.$objectpage->id.'.tpl.php\';'."\n";
71
	$aliascontent .= '?>'."\n";
72
	$result = file_put_contents($filealias, $aliascontent);
73
	if (!empty($conf->global->MAIN_UMASK)) {
74
		@chmod($filealias, octdec($conf->global->MAIN_UMASK));
75
	}
76
77
	return ($result ?true:false);
78
}
79
80
81
/**
82
 * Save content of a page on disk
83
 *
84
 * @param	string		$filetpl			Full path of filename to generate
85
 * @param	Website		$object				Object website
86
 * @param	WebsitePage	$objectpage			Object websitepage
87
 * @return	boolean							True if OK
88
 */
89
function dolSavePageContent($filetpl, Website $object, WebsitePage $objectpage)
90
{
91
	global $conf, $db;
92
93
	// Now create the .tpl file (duplicate code with actions updatesource or updatecontent but we need this to save new header)
94
	dol_syslog("We regenerate the tpl page filetpl=".$filetpl);
95
96
	dol_delete_file($filetpl);
97
98
	$shortlangcode = '';
99
	if ($objectpage->lang) $shortlangcode = preg_replace('/[_-].*$/', '', $objectpage->lang); // en_US or en-US -> en
100
101
	$tplcontent = '';
102
	$tplcontent .= "<?php // BEGIN PHP\n";
103
	$tplcontent .= '$websitekey=basename(__DIR__); if (empty($websitepagefile)) $websitepagefile=__FILE__;'."\n";
104
	$tplcontent .= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) { require_once './master.inc.php'; } // Not already loaded"."\n";
105
	$tplcontent .= "require_once DOL_DOCUMENT_ROOT.'/core/lib/website.lib.php';\n";
106
	$tplcontent .= "require_once DOL_DOCUMENT_ROOT.'/core/website.inc.php';\n";
107
	$tplcontent .= "ob_start();\n";
108
	$tplcontent .= "// END PHP ?>\n";
109
	if (!empty($conf->global->WEBSITE_FORCE_DOCTYPE_HTML5))
110
	{
111
		$tplcontent .= "<!DOCTYPE html>\n";
112
	}
113
	$tplcontent .= '<html'.($shortlangcode ? ' lang="'.$shortlangcode.'"' : '').'>'."\n";
114
	$tplcontent .= '<head>'."\n";
115
	$tplcontent .= '<title>'.dol_string_nohtmltag($objectpage->title, 0, 'UTF-8').'</title>'."\n";
116
	$tplcontent .= '<meta charset="utf-8">'."\n";
117
	$tplcontent .= '<meta http-equiv="content-type" content="text/html; charset=utf-8" />'."\n";
118
	$tplcontent .= '<meta name="robots" content="index, follow" />'."\n";
119
	$tplcontent .= '<meta name="viewport" content="width=device-width, initial-scale=1.0">'."\n";
120
	$tplcontent .= '<meta name="keywords" content="'.dol_string_nohtmltag($objectpage->keywords).'" />'."\n";
121
	$tplcontent .= '<meta name="title" content="'.dol_string_nohtmltag($objectpage->title, 0, 'UTF-8').'" />'."\n";
122
	$tplcontent .= '<meta name="description" content="'.dol_string_nohtmltag($objectpage->description, 0, 'UTF-8').'" />'."\n";
123
	$tplcontent .= '<meta name="generator" content="'.DOL_APPLICATION_TITLE.' '.DOL_VERSION.' (https://www.dolibarr.org)" />'."\n";
124
	$tplcontent .= '<meta name="dolibarr:pageid" content="'.dol_string_nohtmltag($objectpage->id).'" />'."\n";
125
	// Add translation reference (main language)
126
	if ($object->isMultiLang()) {
127
		// Add myself
128
		$tplcontent .= '<link rel="alternate" hreflang="'.$shortlangcode.'" href="'.($object->fk_default_home == $objectpage->id ? '/' : '/'.$objectpage->pageurl.'.php').'" />'."\n";
129
		// Add page "translation of"
130
		$translationof = $objectpage->fk_page;
131
		if ($translationof) {
132
			$tmppage = new WebsitePage($db);
133
			$tmppage->fetch($translationof);
134
			if ($tmppage->id > 0) {
135
				$tmpshortlangcode = '';
136
				if ($tmppage->lang) $tmpshortlangcode = preg_replace('/[_-].*$/', '', $tmppage->lang); // en_US or en-US -> en
137
				if ($tmpshortlangcode != $shortlangcode) {
138
					$tplcontent .= '<link rel="alternate" hreflang="'.$tmpshortlangcode.'" href="'.($object->fk_default_home == $tmppage->id ? '/' : '/'.$tmppage->pageurl.'.php').'" />'."\n";
139
				}
140
			}
141
		}
142
		// Add "has translation pages"
143
		$sql = 'SELECT rowid as id, lang, pageurl from '.MAIN_DB_PREFIX.'website_page where fk_page IN ('.$objectpage->id.($translationof ? ", ".$translationof : "").")";
144
		$resql = $db->query($sql);
145
		if ($resql)
146
		{
147
			$num_rows = $db->num_rows($resql);
148
			if ($num_rows > 0)
149
			{
150
				while ($obj = $db->fetch_object($resql))
151
				{
152
					$tmpshortlangcode = '';
153
					if ($obj->lang) $tmpshortlangcode = preg_replace('/[_-].*$/', '', $obj->lang); // en_US or en-US -> en
154
					if ($tmpshortlangcode != $shortlangcode) {
155
						$tplcontent .= '<link rel="alternate" hreflang="'.$tmpshortlangcode.'" href="'.($object->fk_default_home == $obj->id ? '/' : '/'.$obj->pageurl.'.php').'" />'."\n";
156
					}
157
				}
158
			}
159
		}
160
		else dol_print_error($db);
161
	}
162
	// Add canonical reference
163
	$tplcontent .= '<link href="/'.(($objectpage->id == $object->fk_default_home) ? '' : ($objectpage->pageurl.'.php')).'" rel="canonical" />'."\n";
164
	// Add manifest.json on homepage
165
	$tplcontent .= '<?php if ($website->use_manifest) { print \'<link rel="manifest" href="/manifest.json.php" />\'."\n"; } ?>'."\n";
166
	$tplcontent .= '<!-- Include link to CSS file -->'."\n";
167
	$tplcontent .= '<link rel="stylesheet" href="styles.css.php?website=<?php echo $websitekey; ?>" type="text/css" />'."\n";
168
	$tplcontent .= '<!-- Include HTML header from common file -->'."\n";
169
	$tplcontent .= '<?php print preg_replace(\'/<\/?html>/ims\', \'\', file_get_contents(DOL_DATA_ROOT."/website/".$websitekey."/htmlheader.html")); ?>'."\n";
170
	$tplcontent .= '<!-- Include HTML header from page header block -->'."\n";
171
	$tplcontent .= preg_replace('/<\/?html>/ims', '', $objectpage->htmlheader)."\n";
172
	$tplcontent .= '</head>'."\n";
173
174
	$tplcontent .= '<!-- File generated by Dolibarr website module editor -->'."\n";
175
	$tplcontent .= '<body id="bodywebsite" class="bodywebsite bodywebpage-'.$objectpage->ref.'">'."\n";
176
	$tplcontent .= $objectpage->content."\n";
177
	$tplcontent .= '</body>'."\n";
178
	$tplcontent .= '</html>'."\n";
179
180
	$tplcontent .= '<?php // BEGIN PHP'."\n";
181
	$tplcontent .= '$tmp = ob_get_contents(); ob_end_clean(); dolWebsiteOutput($tmp, "html", '.$objectpage->id.');'."\n";
182
	$tplcontent .= "// END PHP ?>"."\n";
183
184
	//var_dump($filetpl);exit;
185
	$result = file_put_contents($filetpl, $tplcontent);
186
	if (!empty($conf->global->MAIN_UMASK))
187
		@chmod($filetpl, octdec($conf->global->MAIN_UMASK));
188
189
		return $result;
190
}
191
192
193
/**
194
 * Save content of the index.php and wrapper.php page
195
 *
196
 * @param	string		$pathofwebsite			Path of website root
197
 * @param	string		$fileindex				Full path of file index.php
198
 * @param	string		$filetpl				File tpl to index.php page redirect to
199
 * @param	string		$filewrapper			Full path of file wrapper.php
200
 * @return	boolean								True if OK
201
 */
202
function dolSaveIndexPage($pathofwebsite, $fileindex, $filetpl, $filewrapper)
203
{
204
	global $conf;
205
206
	$result1 = false;
207
	$result2 = false;
208
209
	dol_mkdir($pathofwebsite);
210
211
	dol_delete_file($fileindex);
212
	$indexcontent = '<?php'."\n";
213
	$indexcontent .= "// BEGIN PHP File generated to provide an index.php as Home Page or alias redirector - DO NOT MODIFY - It is just a generated wrapper.\n";
214
	$indexcontent .= '$websitekey=basename(__DIR__); if (empty($websitepagefile)) $websitepagefile=__FILE__;'."\n";
215
	$indexcontent .= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) { require_once './master.inc.php'; } // Load master if not already loaded\n";
216
	$indexcontent .= 'if (! empty($_GET[\'pageref\']) || ! empty($_GET[\'pagealiasalt\']) || ! empty($_GET[\'pageid\'])) {'."\n";
217
	$indexcontent .= "	require_once DOL_DOCUMENT_ROOT.'/core/lib/website.lib.php';\n";
218
	$indexcontent .= "	require_once DOL_DOCUMENT_ROOT.'/core/website.inc.php';\n";
219
	$indexcontent .= '	redirectToContainer($_GET[\'pageref\'], $_GET[\'pagealiasalt\'], $_GET[\'pageid\']);'."\n";
220
	$indexcontent .= "}\n";
221
	$indexcontent .= "include_once './".basename($filetpl)."'\n";
222
	$indexcontent .= '// END PHP ?>'."\n";
223
	$result1 = file_put_contents($fileindex, $indexcontent);
224
	if (!empty($conf->global->MAIN_UMASK))
225
		@chmod($fileindex, octdec($conf->global->MAIN_UMASK));
226
227
		dol_delete_file($filewrapper);
228
229
		$wrappercontent = file_get_contents(DOL_DOCUMENT_ROOT.'/website/samples/wrapper.html');
230
231
		$result2 = file_put_contents($filewrapper, $wrappercontent);
232
		if (!empty($conf->global->MAIN_UMASK))
233
			@chmod($filewrapper, octdec($conf->global->MAIN_UMASK));
234
235
			return ($result1 && $result2);
236
}
237
238
239
/**
240
 * Save content of a page on disk
241
 *
242
 * @param	string		$filehtmlheader		Full path of filename to generate
243
 * @param	string		$htmlheadercontent	Content of file
244
 * @return	boolean							True if OK
245
 */
246
function dolSaveHtmlHeader($filehtmlheader, $htmlheadercontent)
247
{
248
	global $conf, $pathofwebsite;
249
250
	dol_syslog("Save html header into ".$filehtmlheader);
251
252
	dol_mkdir($pathofwebsite);
253
	$result = file_put_contents($filehtmlheader, $htmlheadercontent);
254
	if (!empty($conf->global->MAIN_UMASK))
255
		@chmod($filehtmlheader, octdec($conf->global->MAIN_UMASK));
256
257
	return $result;
258
}
259
260
/**
261
 * Save content of a page on disk
262
 *
263
 * @param	string		$filecss			Full path of filename to generate
264
 * @param	string		$csscontent			Content of file
265
 * @return	boolean							True if OK
266
 */
267
function dolSaveCssFile($filecss, $csscontent)
268
{
269
	global $conf, $pathofwebsite;
270
271
	dol_syslog("Save css file into ".$filecss);
272
273
	dol_mkdir($pathofwebsite);
274
	$result = file_put_contents($filecss, $csscontent);
275
	if (!empty($conf->global->MAIN_UMASK))
276
		@chmod($filecss, octdec($conf->global->MAIN_UMASK));
277
278
	return $result;
279
}
280
281
/**
282
 * Save content of a page on disk
283
 *
284
 * @param	string		$filejs				Full path of filename to generate
285
 * @param	string		$jscontent			Content of file
286
 * @return	boolean							True if OK
287
 */
288
function dolSaveJsFile($filejs, $jscontent)
289
{
290
	global $conf, $pathofwebsite;
291
292
	dol_syslog("Save js file into ".$filejs);
293
294
	dol_mkdir($pathofwebsite);
295
	$result = file_put_contents($filejs, $jscontent);
296
	if (!empty($conf->global->MAIN_UMASK))
297
		@chmod($filejs, octdec($conf->global->MAIN_UMASK));
298
299
	return $result;
300
}
301
302
/**
303
 * Save content of a page on disk
304
 *
305
 * @param	string		$filerobot			Full path of filename to generate
306
 * @param	string		$robotcontent		Content of file
307
 * @return	boolean							True if OK
308
 */
309
function dolSaveRobotFile($filerobot, $robotcontent)
310
{
311
	global $conf, $pathofwebsite;
312
313
	dol_syslog("Save robot file into ".$filerobot);
314
315
	dol_mkdir($pathofwebsite);
316
	$result = file_put_contents($filerobot, $robotcontent);
317
	if (!empty($conf->global->MAIN_UMASK))
318
		@chmod($filerobot, octdec($conf->global->MAIN_UMASK));
319
320
	return $result;
321
}
322
323
/**
324
 * Save content of a page on disk
325
 *
326
 * @param	string		$filehtaccess		Full path of filename to generate
327
 * @param	string		$htaccess			Content of file
328
 * @return	boolean							True if OK
329
 */
330
function dolSaveHtaccessFile($filehtaccess, $htaccess)
331
{
332
	global $conf, $pathofwebsite;
333
334
	dol_syslog("Save htaccess file into ".$filehtaccess);
335
336
	dol_mkdir($pathofwebsite);
337
	$result = file_put_contents($filehtaccess, $htaccess);
338
	if (!empty($conf->global->MAIN_UMASK))
339
		@chmod($filehtaccess, octdec($conf->global->MAIN_UMASK));
340
341
	return $result;
342
}
343
344
/**
345
 * Save content of a page on disk
346
 *
347
 * @param	string		$file				Full path of filename to generate
348
 * @param	string		$content			Content of file
349
 * @return	boolean							True if OK
350
 */
351
function dolSaveManifestJson($file, $content)
352
{
353
	global $conf, $pathofwebsite;
354
355
	dol_syslog("Save manifest.js.php file into ".$file);
356
357
	dol_mkdir($pathofwebsite);
358
	$result = file_put_contents($file, $content);
359
	if (!empty($conf->global->MAIN_UMASK))
360
		@chmod($file, octdec($conf->global->MAIN_UMASK));
361
362
	return $result;
363
}
364
365
/**
366
 * Save content of a page on disk
367
 *
368
 * @param	string		$file				Full path of filename to generate
369
 * @param	string		$content			Content of file
370
 * @return	boolean							True if OK
371
 */
372
function dolSaveReadme($file, $content)
373
{
374
	global $conf, $pathofwebsite;
375
376
	dol_syslog("Save README.md file into ".$file);
377
378
	dol_mkdir($pathofwebsite);
379
	$result = file_put_contents($file, $content);
380
	if (!empty($conf->global->MAIN_UMASK))
381
		@chmod($file, octdec($conf->global->MAIN_UMASK));
382
383
		return $result;
384
}
385
386
387
/**
388
 * 	Show list of themes. Show all thumbs of themes/skins
389
 *
390
 *	@param	Website		$website		Object website to load the tempalte into
391
 * 	@return	void
392
 */
393
function showWebsiteTemplates(Website $website)
394
{
395
	global $conf, $langs, $db, $form;
396
	global $bc;
397
398
	$dirthemes = array('/doctemplates/websites');
399
	if (!empty($conf->modules_parts['websitetemplates']))		// Using this feature slow down application
400
	{
401
		foreach ($conf->modules_parts['websitetemplates'] as $reldir)
402
		{
403
			$dirthemes = array_merge($dirthemes, (array) ($reldir.'doctemplates/websites'));
404
		}
405
	}
406
	$dirthemes = array_unique($dirthemes);
407
	// Now dir_themes=array('/themes') or dir_themes=array('/theme','/mymodule/theme')
408
409
	$colspan = 2;
410
411
	$thumbsbyrow = 6;
412
	print '<table class="noborder centpercent">';
413
414
	// Title
415
	print '<tr class="liste_titre"><th class="titlefield"></th>';
416
	print '<th class="right">';
417
	$url = 'https://www.dolistore.com/43-web-site-templates';
418
	print '<a href="'.$url.'" target="_blank">';
419
	print $langs->trans('DownloadMoreSkins');
420
	print '</a>';
421
	print '</th></tr>';
422
423
	print '<tr>';
424
	print '<td>'.$langs->trans("ThemeDir").'</td>';
425
	print '<td>';
426
	foreach ($dirthemes as $dirtheme)
427
	{
428
		echo '"'.$dirtheme.'" ';
429
	}
430
	print '</td>';
431
	print '</tr>';
432
433
	print '<tr><td colspan="'.$colspan.'">';
434
435
	print '<table class="nobordernopadding" width="100%"><tr><td><div class="center">';
436
437
	$i = 0;
438
	foreach ($dirthemes as $dir)
439
	{
440
		//print $dirroot.$dir;exit;
441
		$dirtheme = DOL_DATA_ROOT.$dir; // This include loop on $conf->file->dol_document_root
442
		if (is_dir($dirtheme))
443
		{
444
			$handle = opendir($dirtheme);
445
			if (is_resource($handle))
446
			{
447
				while (($subdir = readdir($handle)) !== false)
448
				{
449
					if (is_file($dirtheme."/".$subdir) && substr($subdir, 0, 1) <> '.'
450
						&& substr($subdir, 0, 3) <> 'CVS' && preg_match('/\.zip$/i', $subdir))
451
					{
452
						$subdirwithoutzip = preg_replace('/\.zip$/i', '', $subdir);
453
454
						// Disable not stable themes (dir ends with _exp or _dev)
455
						if ($conf->global->MAIN_FEATURES_LEVEL < 2 && preg_match('/_dev$/i', $subdir)) continue;
456
						if ($conf->global->MAIN_FEATURES_LEVEL < 1 && preg_match('/_exp$/i', $subdir)) continue;
457
458
						print '<div class="inline-block" style="margin-top: 10px; margin-bottom: 10px; margin-right: 20px; margin-left: 20px;">';
459
460
						$file = $dirtheme."/".$subdirwithoutzip.".jpg";
461
						$url = DOL_URL_ROOT.'/viewimage.php?modulepart=doctemplateswebsite&file='.$subdirwithoutzip.".jpg";
462
463
						if (!file_exists($file)) $url = DOL_URL_ROOT.'/public/theme/common/nophoto.png';
464
465
						$originalfile = basename($file);
466
						$entity = $conf->entity;
467
						$modulepart = 'doctemplateswebsite';
468
						$cache = '';
469
						$title = $file;
470
471
						$ret = '';
472
						$urladvanced = getAdvancedPreviewUrl($modulepart, $originalfile, 1, '&entity='.$entity);
473
						if (!empty($urladvanced)) $ret .= '<a class="'.$urladvanced['css'].'" target="'.$urladvanced['target'].'" mime="'.$urladvanced['mime'].'" href="'.$urladvanced['url'].'">';
474
						else $ret .= '<a href="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&entity='.$entity.'&file='.urlencode($originalfile).'&cache='.$cache.'">';
475
						print $ret;
476
						print '<img class="img-skinthumb shadow" src="'.$url.'" border="0" alt="'.$title.'" title="'.$title.'" style="margin-bottom: 5px;">';
477
						print '</a>';
478
479
						print '<br>';
480
						print $subdir.' ('.dol_print_size(dol_filesize($dirtheme."/".$subdir), 1, 1).')';
481
						print '<br><a href="'.$_SERVER["PHP_SELF"].'?action=importsiteconfirm&website='.$website->ref.'&templateuserfile='.$subdir.'" class="button">'.$langs->trans("Load").'</a>';
482
						print '</div>';
483
484
						$i++;
485
					}
486
				}
487
			}
488
		}
489
	}
490
491
	print '</div></td></tr></table>';
492
493
	print '</td></tr>';
494
	print '</table>';
495
}
496