<?php

if (!function_exists('sv') || preg_match('#/plugins/collection/index.php#i', $_SERVER['SCRIPT_NAME']))
{
	die('no direct access');
}

/*
============================================================
CMSimple Plugin Collection
============================================================
Version:    Collection 1.8
Released:   2024-07
Copyright:  Gert Ebersbach
Internet:   ge-webdesign.de
============================================================ 
utf-8 check: äöü 
*/



/*
=================================== 
    Last Changed CMSimple Pages
=================================== 
*/

function sortInstructions($a, $b)
{
	$sortInstruction = "";
	if($a['last_edit'] == $b['last_edit']) $sortInstruction = 0;
	if($a['last_edit'] > $b['last_edit']) $sortInstruction = -1;
	if($a['last_edit'] < $b['last_edit']) $sortInstruction = 1;
	return $sortInstruction;
}

// displays last changed CMSimple Pages with link

function col_lastChangedPages($numberOfPages = 5, $showDate = 'date')
{
	global $pd_router,$s,$h,$u;

	// create array of all pages
	$allPagesArray = $pd_router -> find_all();

//	echo '<pre>'; print_r(array_keys($allPagesArray)); echo '</pre>';


	// sort array
	uasort($allPagesArray, 'sortInstructions');

	// html output
	$arrayElements = 0;
	$output = '
<ul class="lastChangedPages">';
	foreach(array_keys($allPagesArray) as $nothiddenPage) 
	{
		if(!hide($nothiddenPage) && $arrayElements < $numberOfPages)
		{
			$output.= '
<li>';
			if($showDate=='date') $output.= '<span class="lastChangedPagesDate">' . date("d.m.Y", intval($allPagesArray[$nothiddenPage]['last_edit'])) . ':</span> ';
			$output.= '<span class="lastChangedPagesLink"><a href="./?' . $u[$nothiddenPage] . '">' . $h[$nothiddenPage] . '</a></span></li>';
		}
		if(!hide($nothiddenPage))$arrayElements++;
	}
	$output .= '
</ul>
';
	return $output;
}



/*
=================================== 
      Scheduled Publishing
=================================== 
*/

if(!$edit)
{
	for($i = 0; $i < $cl; $i++) 
	{
		$plugindata = $c[$i];
		if 
		(
			preg_match('/{{{function:col_scheduledPage\((.*?),(.*?)\);}}}/is',$plugindata,$matches) 
			|| preg_match('/{{{plugin:col_scheduledPage\((.*?),(.*?)\);}}}/is',$plugindata,$matches) 
			|| preg_match('/{{{f:col_scheduledPage\((.*?),(.*?)\);}}}/is',$plugindata,$matches)
		)
		{
			$startdate = str_replace("'",'',$matches[1]);
			$enddate = str_replace("'",'',$matches[2]);
			if(date("YmdH") < $startdate || date("YmdH") > $enddate){$c[$i] = '#CMSimple hide#';} else {$c[$i] = $plugindata;}
		}
	}
}

// plugin call

function col_scheduledPage($start = '0', $end = '9999999999'){return '';}



/*
=================================== 
          Embed File
=================================== 
*/

function col_embedFile($efPath, $efWidth='100%', $efHeight='480px', $efClass='col_embedFile')
{
	global $pth;
	$output='<embed src="' . $efPath . '" style=" height:' . $efHeight . '; width: ' . $efWidth . ';" class="' . $efClass . '">';
	return $output;
}



/*
=================================== 
          Adventskranz
=================================== 

Script und Grafiken: meine-erste-homepage.com/ein-adventskranz-fuer-ihre-homepage
*/

function col_adventPNG($status='',$image='') 
{
	global $pth;

	$col_currentYear = date("Y");
	$col_firstAdv = mktime(0,0,0,11,27,$col_currentYear);
	$col_christmasEve = mktime(14,0,0,12,24,$col_currentYear);

	$firstAdvent = strtotime("+1 sunday",$col_firstAdv);
	$secondAdvent = strtotime("+2 sunday",$col_firstAdv);
	$thirdAdvent = strtotime("+3 sunday",$col_firstAdv);
	$fourthAdvent = strtotime("+4 sunday",$col_firstAdv);

	if (time() > $col_christmasEve) $image = "eve";
	elseif (time() > $fourthAdvent) $image = "fourth";
	elseif (time() > $thirdAdvent) $image = "third";
	elseif (time() > $secondAdvent) $image = "second";
	elseif (time() > $firstAdvent) $image = "first";

	if (time() > $firstAdvent || $status == 'test') 
		return '<img src="' . $pth['folder']['base'] . 'plugins/collection/images/advent/' . $image . 'image.png" class="col_adventimg" alt="Frohe Vorweihnachtszeit!">';
}

function col_adventGIF($status='',$image='') 
{
	global $pth;

	$col_currentYear = date("Y");
	$col_firstAdv = mktime(0,0,0,11,27,$col_currentYear);
	$col_christmasEve = mktime(14,0,0,12,24,$col_currentYear);

	$firstAdvent = strtotime("+1 sunday",$col_firstAdv);
	$secondAdvent = strtotime("+2 sunday",$col_firstAdv);
	$thirdAdvent = strtotime("+3 sunday",$col_firstAdv);
	$fourthAdvent = strtotime("+4 sunday",$col_firstAdv);

	if (time() > $col_christmasEve) $image = "eve";
	elseif (time() > $fourthAdvent) $image = "fourth";
	elseif (time() > $thirdAdvent) $image = "third";
	elseif (time() > $secondAdvent) $image = "second";
	elseif (time() > $firstAdvent) $image = "first";

	if (time() > $firstAdvent || $status == 'test') 
		return '<img src="' . $pth['folder']['base'] . 'plugins/collection/images/advent/' . $image . 'image.gif" class="col_adventimg" alt="Frohe Vorweihnachtszeit!">';
}



/*
=================================== 
      Countdown Tage (inaktiv)
=================================== 
*/

function col_countdown($cdy=2100,$cdm=12,$cdd=1,$textday='Tag',$textdays='Tage',$textEndDay='Ziel erreicht',$textCountdownClosed='Countdown closed') 
{

$o = '';

//Gib den Endzeitpunkt an!
$endTime = mktime(0, 0, 0, intval($cdm), intval($cdd), intval($cdy)); //Stunde, Minute, Sekunde, Monat, Tag, Jahr;

if(date('I') > date('I',$endTime)) $hdiff = 3600;
if(date('I') < date('I',$endTime)) $hdiff = -3600;
if(date('I') == date('I',$endTime)) $hdiff = 0;

//Aktuellezeit des microtimestamps nach PHP5, für PHP4 muss eine andere Form genutzt werden.
$timeNow = microtime(true) + $hdiff;

//Berechnet differenz von der Endzeit vom jetzigen Zeitpunkt aus.
$diffTime = $endTime - $timeNow;

//Berechnung für Tage, Stunden, Minuten
$day = floor($diffTime / (24*3600));

if($day == 0) $cdtext = $textday; else $cdtext = $textdays;

if($day < -1) $o.= '<div class="col_countdown">' . $textCountdownClosed . '</div>';
elseif ($day == -1) $o.= '<div class="col_countdown">' . $textEndDay . '</div>';
else $o.= '<div class="col_countdown"><span class="col_countdownNumber">' . ($day+1) . '</span>&nbsp;&nbsp;' . $cdtext . '</div>';

return $o;
}



/*
=================================== 
   Countdown JavaScript (aktiv)
=================================== 
based on: https://de.acervolima.com/so-erstellen-sie-einen-countdown-timer-mit-javascript/
*/

function col_jsCountdown($endtime='2100-12-31 00:00:00',$textd='Tage',$texth='Std',$textm='Min',$texts='Sec',$textexp='Countdown abgelaufen',$id="jsCountdown") 
{

$o = '';

$endTime = str_replace('-',',',$endtime);
$endTime = str_replace(' ',',',$endTime);
$endTime = str_replace(':',',',$endTime);

$endTimeArray = explode(',',$endTime);
$sec = intval(array_pop($endTimeArray));
$min = intval(array_pop($endTimeArray));
$hour = intval(array_pop($endTimeArray));
$day = intval(array_pop($endTimeArray));
$mon = intval(array_pop($endTimeArray));
$year = intval(array_pop($endTimeArray));

$endTime = mktime($hour,$min,$sec,$mon,$day,$year);

// $o.= date('I',$endTime);

if(date('I') > date('I',$endTime)) $hdiff = 3600000;
if(date('I') < date('I',$endTime)) $hdiff = -3600000;
if(date('I') == date('I',$endTime)) $hdiff = 0;

$o.= '
<p id="' . $id . '" class="col_jsCountdown"></p>
<script>
var ' . $id . 'deadline = new Date("' . $endtime . '").getTime();
var ' . $id . 'x = setInterval(function() {
var ' . $id . 'now = new Date().getTime() + ' . $hdiff . ';
var ' . $id . 't = ' . $id . 'deadline - ' . $id . 'now;
var ' . $id . 'days = Math.floor(' . $id . 't / (1000 * 60 * 60 * 24));
var ' . $id . 'hours = Math.floor((' . $id . 't%(1000 * 60 * 60 * 24))/(1000 * 60 * 60));
var ' . $id . 'minutes = Math.floor((' . $id . 't % (1000 * 60 * 60)) / (1000 * 60));
var ' . $id . 'seconds = Math.floor((' . $id . 't % (1000 * 60)) / 1000);

if(' . $id . 'minutes<10)' . $id . 'minutes="0" + ' . $id . 'minutes;
if(' . $id . 'seconds<10)' . $id . 'seconds="0" + ' . $id . 'seconds;';

$o.= 'document.getElementById("' . $id . '").innerHTML = "<table><tr>';
$o.= '<td>' . $textd . '<br><span class=\"col_jsCountdownDays\">" + ' . $id . 'days + "</span></td>';
if($texth!='hide') $o.= '<td class=\"col_jsCountdownH\">' . $texth . '<br><span class=\"col_jsCountdownHms\">" + ' . $id . 'hours + "</span></td>';
if($textm!='hide') $o.= '<td class=\"col_jsCountdownM\">' . $textm . '<br><span class=\"col_jsCountdownHms\">" + ' . $id . 'minutes + "</span></td>';
if($texts!='hide') $o.= '<td class=\"col_jsCountdownS\">' . $texts . '<br><span class=\"col_jsCountdownHms\">" + ' . $id . 'seconds + "</span></td>';
$o.= '</tr></table>";
    if (' . $id . 't < 0) {
        clearInterval(' . $id . 'x);
        document.getElementById("' . $id . '").innerHTML = "<span class=\"col_jsCountdownExp\">' . $textexp . '</span>";
    }
}, 1000);
</script>
<noscript>JavaScript is disabled</noscript>
';

return $o;
}



/*
=================================== 
   Screen Resolution & Viewport
=================================== 
*/

function col_screenres()
{
$jsLoad = '
<script>
window.addEventListener 
(\'load\', function ()
	{
		var screenresWidthID = document.getElementById(\'screenresWidth\');
		var screenresHeightID = document.getElementById(\'screenresHeight\');
		screenresWidthID.innerHTML = screen.width;
		screenresHeightID.innerHTML = screen.height;

		screen.orientation.onchange = function () 
		{
			screenresWidthID.innerHTML = screen.width;
			screenresHeightID.innerHTML = screen.height;
		}
	}
)
</script>
';
return '<p class="col_screenres"><span id="screenresWidth" class="col_screenresValues"></span> x <span id="screenresHeight" class="col_screenresValues"></span> px</p>' . $jsLoad;
}


function col_viewport()
{
$jsLoad = '
<script>
window.addEventListener 
(\'load\', function ()
	{
		var viewportWidthID = document.getElementById(\'viewportWidth\');
		var viewportHeightID = document.getElementById(\'viewportHeight\');
		viewportWidthID.innerHTML = window.innerWidth;
		viewportHeightID.innerHTML = window.innerHeight;

		window.onresize = function () 
		{
			viewportWidthID.innerHTML = window.innerWidth;
			viewportHeightID.innerHTML = window.innerHeight;
		}
	}
)
</script>
';
return '<p class="col_viewport"><span id="viewportWidth" class="col_viewportValues"></span> x <span id="viewportHeight" class="col_viewportValues"></span> px</p>' . $jsLoad;
}



/*
========================================== 
  CMSimple Seiten als Lightbox einbinden
========================================== 
*/

function col_ovlPage($id='',$source='',$linktext='Linktext',$design='bright',$top='6vh')
{
	global $edit,$hjs,$u,$s,$sn,$o,$pth;

// Link
	$output = '<a href="?' . ($s>=0 ? $u[$s] : htmlspecialchars(strip_tags($_SERVER['QUERY_STRING']), ENT_QUOTES, 'UTF-8')) . '&ovlOutput' . $id . '">' . htmlspecialchars_decode($linktext) . '</a>';

// Ausgabe
	if(!$edit && isset($_GET['ovlOutput' . $id]))
	{
		$output.= '

<!-- Start Overlay Output -->

<a href="?' . ($s>=0 ? $u[$s] : str_replace('&amp;' . 'ovlOutput' . $id,'',htmlspecialchars(strip_tags($_SERVER['QUERY_STRING']), ENT_QUOTES, 'UTF-8'))) . '"><div class="col_ovl_bg_' . $design . '"></div></a>
<div class="col_ovl_' . $design . '" style="top: ' . $top . '; z-index: 9999;">

<div id="col_ovl_close_buttons" class="col_ovl_close_' . $design . '" style="top: ' . $top . ';">
<script>document.getElementById(\'col_ovl_close_buttons\').innerHTML="<span onclick=\"history.back()\">&laquo;</span><span class=\"reload\" onclick=\"location.reload()\"><img src=\"' . $pth['folder']['plugins'] . 'collection/images/reload.png\" alt=\"reload\"></span>"</script>
<a href="?' . ($s>=0 ? $u[$s] : str_replace('&amp;' . 'ovlOutput' . $id,'',htmlspecialchars(strip_tags($_SERVER['QUERY_STRING']), ENT_QUOTES, 'UTF-8'))) . '">X</a>
</div>

<div class="col_ovl_' . $design . 'In">
' . 
geturl((empty($_SERVER['HTTPS']) ? 'http://' : 'https://') . $_SERVER['SERVER_NAME'] . $sn . '?' . $source . '&print') . '</div>
</div>

<!-- END Overlay Output -->

';
	}
	return $output;
}



function col_ovlURL($id='',$source='',$linktext='Linktext',$design='dark',$top='6vh')
{
	global $edit,$hjs,$u,$s,$o,$pth;

	$needles = ['.jpg','.jpeg','.png','.gif','.mp4','.ogv','.webm'];
	$thumbnail = $source;
	$thumbnail = str_ireplace($needles,'_thumb.jpg',$thumbnail);
	if(stristr($linktext,'%img%') && stristr($thumbnail,'_thumb.jpg'))$linktext = '<img src="' . $thumbnail . '" style="max-width: 100%;" alt="">';

// Link
	$output = '<a href="?' . ($s>=0 ? $u[$s] : htmlspecialchars(strip_tags($_SERVER['QUERY_STRING']), ENT_QUOTES, 'UTF-8')) . '&ovlOutput' . $id . '">' . htmlspecialchars_decode($linktext) . '</a>';

// Ausgabe
	if(!$edit && isset($_GET['ovlOutput' . $id]))
	{
		$output.= '

<!-- Start Overlay Output -->

<a href="?' . ($s>=0 ? $u[$s] : str_replace('&amp;' . 'ovlOutput' . $id,'',htmlspecialchars(strip_tags($_SERVER['QUERY_STRING']), ENT_QUOTES, 'UTF-8'))) . '"><div class="col_ovl_bg_' . $design . '"></div></a>
<div class="col_ovl_' . $design . '" style="top: ' . $top . '; z-index: 9999;">

<div id="col_ovl_close_buttons" class="col_ovl_close_' . $design . '" style="top: ' . $top . ';">
<script>document.getElementById(\'col_ovl_close_buttons\').innerHTML="<span onclick=\"history.back()\">&laquo;</span><span class=\"reload\" onclick=\"location.reload()\"><img src=\"' . $pth['folder']['plugins'] . 'collection/images/reload.png\" alt=\"reload\"></span>"</script>
<a href="?' . ($s>=0 ? $u[$s] : str_replace('&amp;' . 'ovlOutput' . $id,'',htmlspecialchars(strip_tags($_SERVER['QUERY_STRING']), ENT_QUOTES, 'UTF-8'))) . '">X</a>
</div>

<div class="col_ovl_' . $design . 'In">';
		if(stristr($source,'.jpg') || stristr($source,'.jpeg') || stristr($source,'.png') || stristr($source,'.gif'))
		{
			$output.= '<img src="' . $source . '" alt="">';
		}
		elseif(stristr($source,'.mp4') || stristr($source,'.ogv') || stristr($source,'.webm'))
		{
			$output.= '
<video width="100%" controls>
<source src="' . $source . '" type="video/mp4">
<source src="' . $source . '" type="video/ogg">
<source src="' . $source . '" type="video/webm">
</video>
';
		}
		else 
		{
			$output.= geturl($source);
		}
		$output.= '
</div>
</div>

<!-- END Overlay Output -->

';
	}
	return $output;
}



function col_ovlFrame($id='',$source='',$linktext='Linktext',$design='dark',$top='6vh')
{
	global $edit,$hjs,$u,$s,$o,$pth;

	$needles = ['.jpg','.jpeg','.png','.gif','.mp4','.ogv','.webm'];
	$thumbnail = $source;
	$thumbnail = str_ireplace($needles,'_thumb.jpg',$thumbnail);
	if(stristr($linktext,'%img%') && stristr($thumbnail,'_thumb.jpg'))$linktext = '<img src="' . $thumbnail . '" style="max-width: 100%;" alt="">';

// Link
	$output = '<a href="?' . ($s>=0 ? $u[$s] : htmlspecialchars(strip_tags($_SERVER['QUERY_STRING']), ENT_QUOTES, 'UTF-8')) . '&ovlOutput' . $id . '">' . htmlspecialchars_decode($linktext) . '</a>';

// Ausgabe
	if(!$edit && isset($_GET['ovlOutput' . $id]))
	{
		$output.= '

<!-- Start Overlay Output -->

<a href="?' . ($s>=0 ? $u[$s] : str_replace('&amp;' . 'ovlOutput' . $id,'',htmlspecialchars(strip_tags($_SERVER['QUERY_STRING']), ENT_QUOTES, 'UTF-8'))) . '"><div class="col_ovl_bg_' . $design . '"></div></a>
<div class="col_ovl_' . $design . '" style="top: ' . $top . '; z-index: 9999;">

<div id="col_ovl_close_buttons" class="col_ovl_close_' . $design . '" style="top: ' . $top . ';">
<script>document.getElementById(\'col_ovl_close_buttons\').innerHTML="<span onclick=\"history.back()\">&laquo;</span><span class=\"reload\" onclick=\"location.reload()\"><img src=\"' . $pth['folder']['plugins'] . 'collection/images/reload.png\" alt=\"reload\"></span>"</script>
<a href="?' . ($s>=0 ? $u[$s] : str_replace('&amp;' . 'ovlOutput' . $id,'',htmlspecialchars(strip_tags($_SERVER['QUERY_STRING']), ENT_QUOTES, 'UTF-8'))) . '">X</a>
</div>

<div class="col_ovl_' . $design . 'In">
<iframe src="' . $source . '" class="col_ovl_iframe"></iframe>
</div>
</div>

<!-- END Overlay Output -->

';
	}
	return $output;
}



// ALTE FUNKTIONEN - für backwards compatibility, nicht mehr unterstützt (in der help.htm)

function col_ovlLink($linktext='Linktext',$matchcode='matchcode')
{
	global $u,$s;

	$output = '
<a href="?' . ($s>=0 ? $u[$s] : htmlspecialchars(strip_tags($_SERVER['QUERY_STRING']), ENT_QUOTES, 'UTF-8')) . '&' . uenc($matchcode) . '">' . htmlspecialchars_decode($linktext) . '</a>
';
return $output;
}


function col_ovlContent($url='',$matchcode='matchcode',$design='bright',$top='12.5vh')
{
	global $edit,$hjs,$u,$s,$o,$pth;

	if(!$edit && isset($_GET[uenc($matchcode)]))
	{
		$output = '
<a href="?' . ($s>=0 ? $u[$s] : str_replace('&amp;' . $matchcode,'',htmlspecialchars(strip_tags($_SERVER['QUERY_STRING']), ENT_QUOTES, 'UTF-8'))) . '"><div class="col_ovl_bg_' . $design . '"></div></a>
<div class="col_ovl_' . $design . '" style="top: ' . $top . ';">

<div id="col_ovl_close_buttons" class="col_ovl_close_' . $design . '" style="top: ' . $top . ';">
<script>document.getElementById(\'col_ovl_close_buttons\').innerHTML="<span onclick=\"history.back()\">&laquo;</span><span class=\"reload\" onclick=\"location.reload()\"><img src=\"' . $pth['folder']['plugins'] . 'collection/images/reload.png\" alt=\"reload\"></span>"</script>
<a href="?' . ($s>=0 ? $u[$s] : str_replace('&amp;' . $matchcode,'',htmlspecialchars(strip_tags($_SERVER['QUERY_STRING']), ENT_QUOTES, 'UTF-8'))) . '">X</a>
</div>

<div class="col_ovl_' . $design . 'In">
' . 
geturl($url) . '</div>
</div>
';
	}
	if(!$edit && isset($_GET[uenc($matchcode)])){return $output;}else{return '';}
}



/*
========================================== 
             Anruf Buttons
========================================== 
*/

function col_callPhone($telnr='',$title='',$style='',$visible='')
{
	global $hjs,$pth;
	
	if(!isset($hjs))$hjs="";
	if($visible != '') 
	{
		$hjs.= '
<style>
@media (min-width: ' . $visible . ') {
.col_callPhone {display: none;}
}
</style>
';
	}
	
	$output = '<a href="tel:' . $telnr . '"><span class="col_callPhone"><img src="' . $pth['folder']['base'] . 'plugins/collection/images/phone.png" style="' . $style . '" title="' . $title . '" alt=""></span></a>';

	return $output;
}

function col_callMobile($telnr='',$title='',$style='',$visible='')
{
	global $hjs,$pth;

	if(!isset($hjs))$hjs="";
	if($visible != '') 
	{
		$hjs.= '
<style>
@media (min-width: ' . $visible . ') {
.col_callPhone {display: none;}
}
</style>
';
	}

	$output = '<a href="tel:' . $telnr . '"><span class="col_callPhone"><img src="' . $pth['folder']['base'] . 'plugins/collection/images/smartphone.png" style="' . $style . '" title="' . $title . '" alt=""></span></a>';

	return $output;
}



/*
========================================== 
         RSS Feed einbinden
========================================== 
*/

function col_embedRssFeed($src='',$nbrof=4,$feedDesc='',$trimEnd=-14)
{
	$rssNbrMax=$nbrof;
	$rssNbr=0;
	$rss = simplexml_load_file($src); 
	$feed = '<hr class="erf_hr">
<ul class="erf_container">'; 
	foreach($rss->channel->item as $i) 
	{
		$rssNbr++;
		if($rssNbr<=$rssNbrMax)
		{
			$feed.= '<li>
<b><a href="' . $i->link . '">' . $i->title . '</a></b><br><span>' . substr($i->pubDate,0,$trimEnd) . '</span>';
			if($feedDesc == 'desc')
			{
				$feed.= '<br>' . $i->description;
			}
			$feed.= '<hr class="erf_hr">
</li>
';
		}
	}
	$feed.= '</ul>
';
//	print_r($i);
	return $feed;
}
?>