*
* 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');
$confirm=GETPOST('confirm','alpha');
$cancel=GETPOST('cancel','alpha');
$section_dir = GETPOST('section_dir', 'alpha');
$file_manager = GETPOST('file_manager', 'alpha');
if (GETPOST('delete')) { $action='delete'; }
if (GETPOST('preview')) $action='preview';
if (GETPOST('createsite')) { $action='createsite'; }
if (GETPOST('create')) { $action='create'; }
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) && $file_manager) $action='file_manager';
// Load variable for pagination
$limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
$sortfield = GETPOST("sortfield",'alpha');
$sortorder = GETPOST("sortorder",'alpha');
$page = GETPOST("page",'int');
if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
$offset = $limit * $page;
$pageprev = $page - 1;
$pagenext = $page + 1;
//if (! $sortfield) $sortfield='name';
//if (! $sortorder) $sortorder='ASC';
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
$permtouploadfile = $user->rights->website->write;
$diroutput = $conf->medias->multidir_output[$conf->entity];
$relativepath=$section_dir;
$upload_dir = $diroutput.'/'.$relativepath;
/*
* Actions
*/
$backtopage=$_SERVER["PHP_SELF"].'?file_manager=1&website='.$website.'&pageid='.$pageid; // used after a confirm_deletefile into actions_linkedfiles.inc.php
include DOL_DOCUMENT_ROOT.'/core/actions_linkedfiles.inc.php';
if ($action == 'renamefile') $action='file_manager'; // After actions_linkedfiles, if action were renamefile, we set it to 'file_manager'
// Add directory
if ($action == 'add' && $permtouploadfile)
{
$ecmdir->ref = 'NOTUSEDYET';
$ecmdir->label = GETPOST("label");
$ecmdir->description = GETPOST("desc");
//$id = $ecmdir->create($user);
if ($id > 0)
{
header("Location: ".$_SERVER["PHP_SELF"]);
exit;
}
else
{
setEventMessages('Error '.$langs->trans($ecmdir->error), null, 'errors');
$action = "create";
}
clearstatcache();
}
// Remove directory
if ($action == 'confirm_deletesection' && GETPOST('confirm') == 'yes')
{
//$result=$ecmdir->delete($user);
setEventMessages($langs->trans("ECMSectionWasRemoved", $ecmdir->label), null, 'mesgs');
clearstatcache();
}
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('/'."\n";
llxHeader($moreheadcss.$moreheadjs, $langs->trans("websiteetup"), $help_url, '', 0, 0, $arrayofjs, $arrayofcss, '', '', ''."\n".'