' . CS::translate('SAWS_SOLR_SERVICE_SITE_STATUS_ACTIVE', 'csalive') . '' : CS::translate('SAWS_SOLR_SERVICE_SITE_STATUS_ACTIVE', 'csalive');
} else {
return ($bolAsHTMLCode) ? '
' . CS::translate('SAWS_SOLR_SERVICE_SITE_STATUS_INACTIVE', 'csalive') . '
' : CS::translate('SAWS_SOLR_SERVICE_SITE_STATUS_INACTIVE', 'csalive');
}
}
/**
* Creates an html table with the $_SERVER variables.
*
* @return string Returns the html code
*/
public static function getClientInfo() {
$objTable = CSGui::createTable();
$objTable->addRow([CS::translate('SAWS_SOLR_SERVICE_SITE_STATUS_CHECK', 'csalive'), self::getStatus(true)]);
$objTable->addRow([CS::translate('SAWS_SOLR_SERVICE_SITE_REMOTE_IP', 'csalive').' [REMOTE_ADDR]', $_SERVER['REMOTE_ADDR']]);
$objTable->addRow([CS::translate('SAWS_SOLR_SERVICE_SITE_LOCAL_IP', 'csalive').' [HTTP_X_FORWARDED_FOR]', $_SERVER['HTTP_X_FORWARDED_FOR']]);
$objTable->addRow([CS::translate('SAWS_SOLR_SERVICE_SITE_LOCAL_IP', 'csalive').' [HTTP_X_REAL_IP]', $_SERVER['HTTP_X_REAL_IP']]);
$objTable->addRow([CS::translate('SAWS_SOLR_SERVICE_SITE_SYSTEM_TIME', 'csalive'), date(CS::translate('SAWS_SOLR_SERVICE_SITE_DATE_FORMAT', 'csalive'))]);
$objTable->setDisplayFinalRow(false);
return $objTable->makeHtml();
}
/**
* Gets the allowed IPs as array
*
* @return array Returns all ips as array
*/
public static function getAllowedIPs() {
return explode(',', CS::getOption('SAWS_SOLR_SERVICE_SITE_ALLOWED_IPS', 'csalive'));
}
/**
* Compares the current time with the time settings.
*
* @return boolean Return true if the current time is in the period
*/
public static function isInMaintenancePeriod() {
$intFromDate = strtotime(CS::getOption('SAWS_SOLR_SERVICE_SITE_FROM_DATE', 'csalive'));
$intUntilDate = strtotime(CS::getOption('SAWS_SOLR_SERVICE_SITE_UNTIL_DATE', 'csalive'));
$intCurrentDate = strtotime('now');
if ($intCurrentDate > $intFromDate && $intCurrentDate < $intUntilDate) {
return true;
} else {
return false;
}
}
/**
* Creates the .htaccess file which redirects the wrong IPs
*/
public static function createHtaccessFile() {
//Writing
$arrPaths = explode(',', CS::getOption('SAWS_SOLR_SERVICE_SITE_PATH', 'csalive'));
foreach ($arrPaths as $strPath) {
//SAWS Sign
$strContent = self::getHtaccessCode(CS::getOption('SAWS_SOLR_SERVICE_SITE_FROM_DATE', 'csalive'), CS::getOption('SAWS_SOLR_SERVICE_SITE_UNTIL_DATE', 'csalive'), CS::getOption('SAWS_SOLR_SERVICE_SITE_ALLOWED_IPS', 'csalive'));
//Append file if it exists
if (file_exists($strPath . '.htaccess')) {
$strFoundContent = file_get_contents($strPath . '.htaccess');
if ($strFoundContent) {
$mixPosition = strpos($strFoundContent, "[END_SAWS]");
//If the SAWS htaccess already exists
if ($mixPosition) {
//Remove SAWS Maintenance data
$strFoundContent = substr($strFoundContent, $mixPosition + 33);
}
file_put_contents($strPath . '.htaccess', $strContent . "\n\n" . $strFoundContent);
} else {
file_put_contents($strPath . '.htaccess', $strContent);
}
} else {
file_put_contents($strPath . '.htaccess', $strContent);
}
//Validation
$strFoundContent = file_get_contents($strPath . '.htaccess');
$mixPosition = strpos($strFoundContent, "[START_SAWS]");
//If the SAWS htaccess block exists
if (!$mixPosition) throw new Exception('.htaccess could not be extended.');
if (!file_exists($strPath . '.htaccess')) throw new Exception('.htaccess could not be created on path: '.$strPath . '.htaccess');
}
}
/**
* Create html file based on the maintenance options
*/
public static function createHTMLFileFromOptions() {
$strTitle = CS::getOption('SAWS_SOLR_SERVICE_SITE_TITLE', 'csalive');
$strHeader = CS::getOption('SAWS_SOLR_SERVICE_SITE_HEADER', 'csalive');
$strDescription = CS::getOption('SAWS_SOLR_SERVICE_SITE_DESCRIPTION', 'csalive');
$strFromDate = CS::getOption('SAWS_SOLR_SERVICE_SITE_FROM_DATE', 'csalive');
$strUntilDate = CS::getOption('SAWS_SOLR_SERVICE_SITE_UNTIL_DATE', 'csalive');
if (strpos($strDescription, '{from_date_') || strpos($strDescription, '{until_date_')) {
$strDescription = self::replaceDatePlaceholders($strDescription, $strFromDate, $strUntilDate);
}
$strHTMLCode = self::getHTMLCode($strDescription, $strHeader, $strTitle);
file_put_contents('../maintenance.html', $strHTMLCode);
if (!file_exists('../maintenance.html')) throw new Exception('maintenance.html could not be created!');
}
/**
* Deletes all maintenance files if the necessary path is provided.
*
* @throws Exception If the files were not deleted.
*/
public static function cleanupMaintenanceFiles() {
//Files to delete
$arrMaintenanceFiles = ['../maintenance.css', '../maintenance.html'];
foreach ($arrMaintenanceFiles as $strFile) {
if (file_exists($strFile)) unlink($strFile);
if (file_exists($strFile)) throw new Exception($strFile . ' could not be deleted.');
}
//.htaccess remove SAWS content
$arrPaths = explode(',', CS::getOption('SAWS_SOLR_SERVICE_SITE_PATH', 'csalive'));
foreach ($arrPaths as $strPath) {
if (file_exists($strPath . '.htaccess')) {
self::removeHtaccessBlock($strPath);
}
}
}
/**
* Creates the html code for the service site
*
* @param string $strDescription description of the service site message
* @param string $strHeader headline of the service site message
* @param string $strTitle title of the website
* @return string Returns an html code based on CONTENTSERV classes
*/
private function getHTMLCode($strDescription = '', $strHeader = '', $strTitle = '') {
//WINDOW
$objCSGuiWindow = CSGui::createWindow();
foreach((array)explode(',', CS::getOption('SAWS_SOLR_SERVICE_SITE_PATH', 'csalive')) as $strCSSLinkPath){
//$objCSGuiWindow->addCSSLink($strCSSLinkPath . '../maintenance.css');
}
$objCSGuiWindow->addCSSCode(file_get_contents('admin.css'));
if ($strTitle) $objCSGuiWindow->setWindowTitle($strTitle);
//INNER DIALOG
$objCSGuiDialog = CSGui::createDialog(CSGUI_DIALOG_TYPE_WHITE);
$objCSGuiDialog->toolbarSetVisible(false);
$objCSGuiDialog->setSubTitleVisible(false);
$objCSGuiDialog->menubarSetVisible(false);
if ($strHeader) {
$objCSGuiDialog->setTitleDark(true);
$objCSGuiDialog->setTitle('' . $strDescription . '');
}
//FINISH HTML
$objCSGuiWindow->addContent('
' . $objCSGuiDialog->makeHtml() . '
');
return $objCSGuiWindow->makeHtml();
}
/**
* Replaces the date placeholders in the given content.
*
* @param string $strContent Content with placeholders
* @param string $strFromDate Replaces the from date placeholders
* @param string $strUntilDate Replaces the until date placeholders
* @return string Returns the content with the replaced placeholders.
*/
private function replaceDatePlaceholders($strContent, $strFromDate = '', $strUntilDate = '') {
/*
This is how it would look like if CONTENTSERV saved the date format correctly.
$arrLanguages = CSLanguage::getLanguages();
$arrDateFormats = [];
foreach($arrLanguages as $objLanguage) {
$arrDateFormats[$objLanguage->getValue('ShortName')] = $objLanguage->getValue('DateFormatSetting');
}
foreach ($arrDateFormats as $strShortName => $strFormat) {
$strContent = str_replace('{FromDate_' . $strShortName . '}', date($strFormat, strtotime($strFromDate)), $strContent);
$strContent = str_replace('{UntilDate_' . $strShortName . '}', date($strFormat, strtotime($strUntilDate)), $strContent);
}
return $strContent;
*/
$strContent = str_replace('{from_date_de}', date('d.m.Y - H:i', strtotime($strFromDate)), $strContent);
$strContent = str_replace('{until_date_de}', date('d.m.Y - H:i', strtotime($strUntilDate)), $strContent);
$strContent = str_replace('{from_date_en}', date('m/d/Y - h:i a', strtotime($strFromDate)), $strContent);
$strContent = str_replace('{until_date_en}', date('m/d/Y - h:i a', strtotime($strUntilDate)), $strContent);
return $strContent;
}
/**
* Removes the maintenance block and if the file is empty afterwards, the file as well.
*
* @param string $strHtaccessPath Path of the .htaccess file
* @throws Exception
*/
private function removeHtaccessBlock($strHtaccessPath) {
if (file_exists($strHtaccessPath . '.htaccess')) {
$strFileContent = file_get_contents($strHtaccessPath . '.htaccess');
if ($strFileContent) {
$mixPosition = strpos($strFileContent, "[END_SAWS]");
//If the SAWS htaccess already exists
if ($mixPosition) {
//Remove SAWS Maintenance data
$strFileContent = substr($strFileContent, $mixPosition + 33);
file_put_contents($strHtaccessPath . '.htaccess', ltrim($strFileContent));
}
}
//Validation
$strFileContent = file_get_contents($strHtaccessPath . '.htaccess');
if (trim($strFileContent)) {
$mixPosition = strpos($strFileContent, "[START_SAWS]");
//If the SAWS htaccess still exists
if ($mixPosition) throw new Exception('.htaccess SAWS maintenance block could not be deleted on path: '.$strHtaccessPath . '.htaccess');
} else {
unlink($strHtaccessPath . '.htaccess');
if (file_exists($strHtaccessPath . '.htaccess')) throw new Exception('.htaccess could not be deleted on path: '.$strHtaccessPath . '.htaccess');
}
}
}
/**
* Seperate function to build the htaccess code for the SAWS maintenance
*
* @param string $strFromDate time string from the CS datechooser
* @param string $strUntilDate time string from the CS datechooser
* @param string|array $strAllowedIPs All allowed IPs as array or comma seperated
* @return string Returns the .htaccess code block for the maintenance
*/
private function getHtaccessCode($strFromDate = '', $strUntilDate = '', $strAllowedIPs = '') {
if(is_array($strAllowedIPs)) $strAllowedIPs = implode(',', $strAllowedIPs);
$strAllowedIPs = str_replace(' ', '', str_replace(',', '|', str_replace('*', '', $strAllowedIPs)));
$strFromDate = str_replace(':', '', str_replace('-', '', str_replace(' ', '', $strFromDate)));
$strUntilDate = str_replace(':', '', str_replace('-', '', str_replace(' ', '', $strUntilDate)));
$strContent = "#[START_SAWS] SAWS Maintenance Site\n";
//Disable caching
$strContent .= "
\n";
$strContent .= "Header set Cache-Control \"no-cache, no-store, must-revalidate\"\n";
$strContent .= "Header set Pragma \"no-cache\"\n";
$strContent .= "Header set Expires 0\n";
$strContent .= "\n\n";
//Sets the Rewrite engine on
$strContent .= "RewriteEngine On\n";
$strContent .= "Options -Indexes +FollowSymLinks\n";
$strContent .= "RewriteCond %{REQUEST_URI} !maintenance.css\n";
//Shall only be redirected in the time window
if ($strFromDate) $strContent .= "RewriteCond %{TIME_YEAR}%{TIME_MON}%{TIME_DAY}%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC} >" . $strFromDate . "\n";
if ($strUntilDate) $strContent .= "RewriteCond %{TIME_YEAR}%{TIME_MON}%{TIME_DAY}%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC} <" . $strUntilDate . "\n";
//Redirect all IPs except these
if ($strAllowedIPs) {
$strContent .= "RewriteCond %{HTTP:X-REAL-IP} !(" . $strAllowedIPs . ")\n";
$strContent .= "RewriteCond %{HTTP:X-FORWARDED-FOR} !(" . $strAllowedIPs . ")\n";
$strContent .= "RewriteCond %{REMOTE_ADDR} !(" . $strAllowedIPs . ")\n";
}
//Redirect to the created maintenance.html
$strContent .= "RewriteRule !maintenance\.html$ /".trim(CS::getOption('SAWS_SOLR_SERVICE_BASE_PATH', 'csalive'), '/')."/maintenance.html [L]\n";
//End SAWS Sign
$strContent .= "#[END_SAWS] SAWS Maintenance Site";
return $strContent;
}
}