*
* 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
* along with this program. If not, see .
*/
/**
* \file htdocs/website/index.php
* \ingroup website
* \brief Page to website view/edit
*/
define('NOSCANPOSTFORINJECTION',1);
define('NOSTYLECHECK',1);
require '../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/website.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formwebsite.class.php';
require_once DOL_DOCUMENT_ROOT.'/website/class/website.class.php';
require_once DOL_DOCUMENT_ROOT.'/website/class/websitepage.class.php';
$langs->load("admin");
$langs->load("other");
$langs->load("website");
if (! $user->admin) accessforbidden();
if (! ((GETPOST('testmenuhider','int') || ! empty($conf->global->MAIN_TESTMENUHIDER)) && empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)))
{
$conf->dol_hide_leftmenu = 1; // Force hide of left menu.
}
$error=0;
$website=GETPOST('website', 'alpha');
$page=GETPOST('page', 'alpha');
$pageid=GETPOST('pageid', 'int');
$pageref=GETPOST('pageref', 'aZ09');
$action=GETPOST('action','alpha');
if (GETPOST('delete')) { $action='delete'; }
if (GETPOST('preview')) $action='preview';
if (GETPOST('createsite')) { $action='createsite'; }
if (GETPOST('create')) { $action='create'; }
if (GETPOST('editmedias')) { $action='editmedias'; }
if (GETPOST('editcss')) { $action='editcss'; }
if (GETPOST('editmenu')) { $action='editmenu'; }
if (GETPOST('setashome')) { $action='setashome'; }
if (GETPOST('editmeta')) { $action='editmeta'; }
if (GETPOST('editsource')) { $action='editsource'; }
if (GETPOST('editcontent')) { $action='editcontent'; }
if (GETPOST('createfromclone')) { $action='createfromclone'; }
if (GETPOST('createpagefromclone')) { $action='createpagefromclone'; }
if (empty($action)) $action='preview';
$object=new Website($db);
$objectpage=new WebsitePage($db);
$object->fetchAll(); // Init $object->records
// If website not defined, we take first found
if (empty($website))
{
foreach($object->records as $key => $valwebsite)
{
$website=$valwebsite->ref;
break;
}
}
if ($website)
{
$res = $object->fetch(0, $website);
}
if ($pageid < 0) $pageid = 0;
if (($pageid > 0 || $pageref) && $action != 'add')
{
$res = $objectpage->fetch($pageid, ($object->id > 0 ? $object->id : null), $pageref);
$pageid = $objectpage->id;
}
global $dolibarr_main_data_root;
$pathofwebsite=$dolibarr_main_data_root.'/website/'.$website;
$filehtmlheader=$pathofwebsite.'/htmlheader.html';
$filecss=$pathofwebsite.'/styles.css.php';
$filejs=$pathofwebsite.'/javascript.js.php';
$filerobot=$pathofwebsite.'/robots.txt';
$filehtaccess=$pathofwebsite.'/.htaccess';
$filetpl=$pathofwebsite.'/page'.$pageid.'.tpl.php';
$fileindex=$pathofwebsite.'/index.php';
// 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
/*
* Actions
*/
if (GETPOST('refreshsite')) // If we change the site, we reset the pageid and cancel addsite action.
{
$pageid=0;
if ($action == 'addsite') $action = 'preview';
}
if (GETPOST('refreshpage') && ! in_array($action, array('updatecss'))) $action='preview';
// Add site
if ($action == 'addsite')
{
$db->begin();
if (! $error && ! GETPOST('WEBSITE_REF','alpha'))
{
$error++;
setEventMessages($langs->transnoentities("ErrorFieldRequired", $langs->transnoentities("Ref")), null, 'errors');
}
if (! $error && ! preg_match('/^[a-z0-9_\-\.]+$/i', GETPOST('WEBSITE_REF','alpha')))
{
$error++;
setEventMessages($langs->transnoentities("ErrorFieldCanNotContainSpecialCharacters", $langs->transnoentities("Ref")), null, 'errors');
}
if (! $error)
{
$tmpobject=new Website($db);
$tmpobject->ref = GETPOST('WEBSITE_REF','alpha');
$tmpobject->description = GETPOST('WEBSITE_DESCRIPTION','alpha');
$tmpobject->virtualhost = GETPOST('WEBSITE_VIRTUALHOST','alpha');
$result = $tmpobject->create($user);
if ($result <= 0)
{
$error++;
setEventMessages($tmpobject->error, $tmpobject->errors, 'errors');
}
}
if (! $error)
{
$db->commit();
setEventMessages($langs->trans("SiteAdded", $object->ref), null, 'mesgs');
$action='';
header("Location: ".$_SERVER["PHP_SELF"].'?website='.$tmpobject->ref);
exit;
}
else
{
$db->rollback();
$action='createsite';
}
if (! $error)
{
$action = 'preview';
$id = $object->id;
}
}
// Add page
if ($action == 'add')
{
dol_mkdir($pathofwebsite);
$db->begin();
$objectpage->fk_website = $object->id;
if (GETPOST('fetchexternalurl','alpha'))
{
$urltograb=GETPOST('externalurl','alpha');
}
if ($urltograb)
{
// Clean url to grab, so url can be
// http://www.example.com/ or http://www.example.com/dir1/ or http://www.example.com/dir1/aaa
$urltograbwithoutdomainandparam = preg_replace('/^https?:\/\/[^\/]+\/?/i', '', $urltograb);
$urltograbwithoutdomainandparam = preg_replace('/\?.*$/', '', $urltograbwithoutdomainandparam);
if (empty($urltograbwithoutdomainandparam) && ! preg_match('/\/$/', $urltograb))
{
$urltograb.='/';
}
$urltograbdirwithoutslash = dirname($urltograb.'.');
include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
$tmp = getURLContent($urltograb);
if ($tmp['curl_error_no'])
{
$error++;
setEventMessages($tmp['curl_error_msg'], null, 'errors');
$action='create';
}
else
{
preg_match('/
(.*)<\/head>/is', $tmp['content'], $reg);
$head = $reg[1];
$objectpage->pageurl = dol_sanitizeFileName(preg_replace('/[\/\.]/','-',$urltograbwithoutdomainandparam));
if (empty($objectpage->pageurl))
{
$tmpdomain = getDomainFromURL($urltograb);
$objectpage->pageurl=$tmpdomain.'-home';
}
if (preg_match('/(.*)<\/title>/ims', $head, $regtmp))
{
$objectpage->title = $regtmp[1];
}
if (preg_match('/description = $regtmp[1];
}
if (preg_match('/keywords = $regtmp[1];
}
if (preg_match('/lang = $tmplang[0].($tmplang[1] ? '_'.strtoupper($tmplang[1]) : '');
}
$objectpage->content = $tmp['content'];
$objectpage->content = preg_replace('/^.*]*)*>/ims', '', $objectpage->content);
$objectpage->content = preg_replace('/<\/body(\s[^>]*)*>.*$/ims', '', $objectpage->content);
$absoluteurlinaction=$urltograbdirwithoutslash;
// TODO Replace 'action="$urltograbdirwithoutslash' into action="/"
// TODO Replace 'action="$urltograbdirwithoutslash..."' into action="..."
// TODO Replace 'a href="$urltograbdirwithoutslash' into a href="/"
// TODO Replace 'a href="$urltograbdirwithoutslash..."' into a href="..."
// Now loop to fetch all css files. Include them inline into header of page
$objectpage->htmlheader = $tmp['content'];
$objectpage->htmlheader = preg_replace('/^.*]*)*>/ims', '', $objectpage->htmlheader);
$objectpage->htmlheader = preg_replace('/<\/head(\s[^>]*)*>.*$/ims', '', $objectpage->htmlheader);
$objectpage->htmlheader = preg_replace('/]*)*>\n*/ims', '', $objectpage->htmlheader);
$objectpage->htmlheader = preg_replace('/]*)*>\n*/ims', '', $objectpage->htmlheader);
$objectpage->htmlheader = preg_replace('/]*)*>\n*/ims', '', $objectpage->htmlheader);
$objectpage->htmlheader = preg_replace('/]*>\n*/ims', '', $objectpage->htmlheader);
//$objectpage->htmlheader = preg_replace('/]*>\n/ims', '', $objectpage->htmlheader);
// Now loop to fetch JS
$tmp = $objectpage->htmlheader;
preg_match_all('/';
}
}
}
}
else
{
print '
'; // end current websitebar
$head = array();
/*
* Edit mode
*/
if ($action == 'editcss')
{
print '
';
print ' ';
$csscontent = @file_get_contents($filecss);
// Clean the php css file to remove php code and get only css part
$csscontent = preg_replace('/<\?php \/\/ BEGIN PHP[^\?]*END PHP \?>\n*/ims', '', $csscontent);
$csscontent.= GETPOST('WEBSITE_CSS_INLINE');
if (! trim($csscontent)) $csscontent='/* CSS content (all pages) */'."\n".'body.bodywebsite { margin: 0; }';
$jscontent = @file_get_contents($filejs);
// Clean the php js file to remove php code and get only js part
$jscontent = preg_replace('/<\?php \/\/ BEGIN PHP[^\?]*END PHP \?>\n*/ims', '', $jscontent);
$jscontent.= GETPOST('WEBSITE_JS_INLINE');
if (! trim($jscontent)) $jscontent='/* JS content (all pages) */'."\n";
$htmlheader = @file_get_contents($filehtmlheader);
// Clean the php htmlheader file to remove php code and get only html part
$htmlheader = preg_replace('/<\?php \/\/ BEGIN PHP[^\?]*END PHP \?>\n*/ims', '', $htmlheader);
if (! trim($htmlheader)) $htmlheader='';
else $htmlheader=''."\n".trim($htmlheader)."\n".'';
$robotcontent = @file_get_contents($filerobot);
// Clean the php htmlheader file to remove php code and get only html part
$robotcontent = preg_replace('/<\?php \/\/ BEGIN PHP[^\?]*END PHP \?>\n*/ims', '', $robotcontent);
if (! trim($robotcontent))
{
$robotcontent.="# Robot file. Generated with ".DOL_APPLICATION_TITLE."\n";
$robotcontent.="User-agent: *\n";
$robotcontent.="Allow: /public/\n";
$robotcontent.="Disallow: /administrator/\n";
}
$htaccesscontent = @file_get_contents($filehtaccess);
// Clean the php htaccesscontent file to remove php code and get only html part
$htaccesscontent = preg_replace('/<\?php \/\/ BEGIN PHP[^\?]*END PHP \?>\n*/ims', '', $htaccesscontent);
if (! trim($htaccesscontent))
{
$htaccesscontent.="# Order allow,deny\n";
$htaccesscontent.="# Deny from all\n";
}
//else $htaccesscontent=''."\n".$htaccesscontent."\n".'';*/
dol_fiche_head();
print ''."\n";
print '
';
if (GETPOST('WEBSITE_REF')) $siteref=GETPOST('WEBSITE_REF','alpha');
if (GETPOST('WEBSITE_DESCRIPTION')) $sitedesc=GETPOST('WEBSITE_DESCRIPTION','alpha');
print '
';
*/
$pageurl=$objectpage->pageurl;
$pagetitle=$objectpage->title;
$pagedescription=$objectpage->description;
$pagekeywords=$objectpage->keywords;
$pagelang=$objectpage->lang;
$pagehtmlheader=$objectpage->htmlheader;
}
if (GETPOST('WEBSITE_PAGENAME','alpha')) $pageurl=GETPOST('WEBSITE_PAGENAME','alpha');
if (GETPOST('WEBSITE_TITLE','alpha')) $pagetitle=GETPOST('WEBSITE_TITLE','alpha');
if (GETPOST('WEBSITE_DESCRIPTION','alpha')) $pagedescription=GETPOST('WEBSITE_DESCRIPTION','alpha');
if (GETPOST('WEBSITE_KEYWORDS','alpha')) $pagekeywords=GETPOST('WEBSITE_KEYWORDS','alpha');
if (GETPOST('WEBSITE_LANG','aZ09')) $pagelang=GETPOST('WEBSITE_LANG','aZ09');
if (GETPOST('htmlheader','none')) $pagehtmlheader=GETPOST('htmlheader','none');
print '
';
$out.= "\n".''."\n\n";
print $out;
/*file_put_contents($filetpl, $out);
if (! empty($conf->global->MAIN_UMASK))
@chmod($filetpl, octdec($conf->global->MAIN_UMASK));
// Output file on browser
dol_syslog("index.php include $filetpl $filename content-type=$type");
$original_file_osencoded=dol_osencode($filetpl); // New file name encoded in OS encoding charset
// This test if file exists should be useless. We keep it to find bug more easily
if (! file_exists($original_file_osencoded))
{
dol_print_error(0,$langs->trans("ErrorFileDoesNotExists",$original_file));
exit;
}
//include_once $original_file_osencoded;
*/
/*print '';
print '';*/
}
else
{
print '
';
}
}
llxFooter();
$db->close();
/**
* Save content of a page on disk
*
* @param string $filealias Full path of filename to generate
* @param Website $object Object website
* @param WebsitePage $objectpage Object websitepage
* @return boolean True if OK
*/
function dolSavePageAlias($filealias, $object, $objectpage)
{
global $conf;
// Now create the .tpl file (duplicate code with actions updatesource or updatecontent but we need this to save new header)
dol_syslog("We regenerate the alias page filealias=".$filealias);
$aliascontent = 'id.'.tpl.php\'; ';
$aliascontent.= 'else require $dolibarr_main_data_root.\'/website/\'.$website->ref.\'/page'.$objectpage->id.'.tpl.php\';'."\n";
$aliascontent.= '?>'."\n";
$result = file_put_contents($filealias, $aliascontent);
if (! empty($conf->global->MAIN_UMASK))
@chmod($filealias, octdec($conf->global->MAIN_UMASK));
return ($result?true:false);
}
/**
* Save content of a page on disk
*
* @param string $filetpl Full path of filename to generate
* @param Website $object Object website
* @param WebsitePage $objectpage Object websitepage
* @return boolean True if OK
*/
function dolSavePageContent($filetpl, $object, $objectpage)
{
global $conf;
// Now create the .tpl file (duplicate code with actions updatesource or updatecontent but we need this to save new header)
dol_syslog("We regenerate the tpl page filetpl=".$filetpl);
dol_delete_file($filetpl);
$shortlangcode = '';
if ($objectpage->lang) $shortlangcode=preg_replace('/[_-].*$/', '', $objectpage->lang); // en_US or en-US -> en
$tplcontent ='';
$tplcontent.= "\n";
$tplcontent.= ''."\n";
$tplcontent.= ''."\n";
$tplcontent.= ''.dol_string_nohtmltag($objectpage->title, 0, 'UTF-8').''."\n";
$tplcontent.= ''."\n";
$tplcontent.= ''."\n";
$tplcontent.= ''."\n";
$tplcontent.= ''."\n";
$tplcontent.= ''."\n";
$tplcontent.= ''."\n";
$tplcontent.= ''."\n";
$tplcontent.= ''."\n";
$tplcontent.= ''."\n";
$tplcontent.= ''."\n";
$tplcontent.= ''."\n";
$tplcontent.= 'ref.'/htmlheader.html"); ?>'."\n";
$tplcontent.= ''."\n";
$tplcontent.= $objectpage->htmlheader."\n";
$tplcontent.= ''."\n";
$tplcontent.= ''."\n";
$tplcontent.= ''."\n";
$tplcontent.= $objectpage->content."\n";
$tplcontent.= ''."\n";
$tplcontent.= '"."\n";
//var_dump($filetpl);exit;
$result = file_put_contents($filetpl, $tplcontent);
if (! empty($conf->global->MAIN_UMASK))
@chmod($filetpl, octdec($conf->global->MAIN_UMASK));
return $result;
}
/**
* Save content of a page on disk
*
* @param string $filehtmlheader Full path of filename to generate
* @param string $htmlheadercontent Content of file
* @return boolean True if OK
*/
function dolSaveHtmlHeader($filehtmlheader, $htmlheadercontent)
{
global $conf, $pathofwebsite;
dol_syslog("Save html header into ".$filehtmlheader);
dol_mkdir($pathofwebsite);
$result = file_put_contents($filehtmlheader, $htmlheadercontent);
if (! empty($conf->global->MAIN_UMASK))
@chmod($filehtmlheader, octdec($conf->global->MAIN_UMASK));
if (! $result)
{
setEventMessages('Failed to write file '.$filehtmlheader, null, 'errors');
return false;
}
return true;
}
/**
* Save content of a page on disk
*
* @param string $filecss Full path of filename to generate
* @param string $csscontent Content of file
* @return boolean True if OK
*/
function dolSaveCssFile($filecss, $csscontent)
{
global $conf, $pathofwebsite;
dol_syslog("Save html header into ".$filecss);
dol_mkdir($pathofwebsite);
$result = file_put_contents($filecss, $csscontent);
if (! empty($conf->global->MAIN_UMASK))
@chmod($filecss, octdec($conf->global->MAIN_UMASK));
if (! $result)
{
setEventMessages('Failed to write file '.$filecss, null, 'errors');
return false;
}
return true;
}
/**
* Save content of a page on disk
*
* @param string $filejs Full path of filename to generate
* @param string $jscontent Content of file
* @return boolean True if OK
*/
function dolSaveJsFile($filejs, $jscontent)
{
global $conf, $pathofwebsite;
dol_syslog("Save html header into ".$filejs);
dol_mkdir($pathofwebsite);
$result = file_put_contents($filejs, $jscontent);
if (! empty($conf->global->MAIN_UMASK))
@chmod($filejs, octdec($conf->global->MAIN_UMASK));
if (! $result)
{
setEventMessages('Failed to write file '.$filejs, null, 'errors');
return false;
}
return true;
}
/**
* Save content of a page on disk
*
* @param string $filerobot Full path of filename to generate
* @param string $robotcontent Content of file
* @return boolean True if OK
*/
function dolSaveRobotFile($filerobot, $robotcontent)
{
global $conf, $pathofwebsite;
dol_syslog("Save html header into ".$filerobot);
dol_mkdir($pathofwebsite);
$result = file_put_contents($filerobot, $robotcontent);
if (! empty($conf->global->MAIN_UMASK))
@chmod($filerobot, octdec($conf->global->MAIN_UMASK));
if (! $result)
{
setEventMessages('Failed to write file '.$filerobot, null, 'errors');
return false;
}
return true;
}
/**
* Save content of a page on disk
*
* @param string $filehtaccess Full path of filename to generate
* @param string $htaccess Content of file
* @return boolean True if OK
*/
function dolSaveHtaccessFile($filehtaccess, $htaccess)
{
global $conf, $pathofwebsite;
dol_syslog("Save html header into ".$filehtaccess);
dol_mkdir($pathofwebsite);
$result = file_put_contents($filehtaccess, $htaccess);
if (! empty($conf->global->MAIN_UMASK))
@chmod($filehtaccess, octdec($conf->global->MAIN_UMASK));
if (! $result)
{
setEventMessages('Failed to write file '.$filehtaccess, null, 'errors');
return false;
}
return true;
}