Location: PHPKode > projects > CMSimple > CMSimple_411/cmsimple/functions.php
<?php

/* utf8-marker = äöü */
/*
================================================== 
This file is a part of CMSimple 4.1.1
Released: 03/2013
Project website: www.cmsimple.org
--------------------------------------------------
CMSimple 4.0 and higher uses code and modules of CMSimple_XH, 
a community driven CMSimple based Project
Project website: www.cmsimple-xh.com
================================================== 
CMSimple COPYRIGHT INFORMATION

© Gert Ebersbach - hide@address.com

CMSimple 3.3 and higher is released under the GPL3 licence. 
You may not remove copyright information from the files. 
Any modifications will fall under the copyleft conditions of GPL3.

GPL 3 Deutsch: http://www.gnu.de/documents/gpl.de.html
GPL 3 English: http://www.gnu.org/licenses/gpl.html

END CMSimple COPYRIGHT INFORMATION
================================================== 
*/


if (preg_match('/functions.php/i', $_SERVER['SCRIPT_NAME']))
	die('Access Denied');

// Backward compatibility for DHTML menus - moved from functions.php to cms.php (CMSimple_XH 1.0)

// #CMSimple functions to use within content

function geturl($u) 
{
	$t = '';
	if ($fh = @fopen(preg_replace("/\&amp;/is", "&", $u), "r")) 
	{
		while (!feof($fh))
			$t .= fread($fh, 1024);
		fclose($fh);
		return preg_replace("/.*<body[^>]*>(.*)<\/body>.*/is", "\\1", $t);
	}
}

function geturlwp($u) 
{
	global $su;
	$t = '';
	if ($fh = @fopen(($u . '?' . preg_replace("/^" . preg_replace("/\+/s", "\\\+", preg_replace("/\//s", "\\\/", $su)) . "(\&)?/s", "", sv('QUERY_STRING'))), "r")) 
	{
		while (!feof($fh))
			$t .= fread($fh, 1024);
		fclose($fh);
		return $t;
	}
}

function autogallery($u) 
{
	global $su;
	return preg_replace("/.*<!-- autogallery -->(.*)<!-- \/autogallery -->.*/is", "\\1", preg_replace("/(option value=\"\?)(p=)/is", "\\1" . $su . "&\\2", preg_replace("/(href=\"\?)/is", "\\1" . $su . amp(), preg_replace("/(src=\")(\.)/is", "\\1" . $u . "\\2", geturlwp($u)))));
}

// Other functions
/*
function newsbox($b) {
	global $c, $cl, $h, $cf;
	for($i = 0; $i < $cl; $i++)if($h[$i] == $b)return preg_replace("/".$cf['scripting']['regexp']."/is", "", preg_replace("/.*<\/h[1-".$cf['menu']['levels']."]>/i", "", $c[$i]));
}
*/

function h($n) 
{
	global $h;
	return $h[$n];
}

function l($n) 
{
	global $l;
	return $l[$n];
}


/**
 * Returns $__text with CMSimple scripting evaluated.
 *
 * @param string $__text
 * @param bool $__compat  Wether only last CMSimple script should be evaluated.
 * @return string
 */
function evaluate_cmsimple_scripting($__text, $__compat = TRUE) 
{
	global $output;
	foreach ($GLOBALS as $__name => $__dummy) {global $$__name;}

	$__scope_before = NULL; // just that it exists
	$__scripts = array();
	preg_match_all('~'.$cf['scripting']['regexp'].'~is', $__text, $__scripts);
	if (count($__scripts[1]) > 0) 
	{
		//$output = preg_replace('~'.$cf['scripting']['regexp'].'~is', '', $__text);
		$output = preg_replace('~#CMSimple (?!hide)(.*?)#~is', '', $__text);
		if ($__compat) 
		{
			$__scripts[1] = array_reverse($__scripts[1]);
		}
		foreach ($__scripts[1] as $__script) 
		{
			if (strtolower($__script) !== 'hide' && strtolower($__script) !== 'remove') 
			{
                $__script = preg_replace(
				array("'&(quot|#34);'i", "'&(amp|#38);'i", "'&(apos|#39);'i", "'&(lt|#60);'i", "'&(gt|#62);'i", "'&(nbsp|#160);'i"),
				array("\"", "&", "'", "<", ">", " "),
				$__script);
				$__scope_before = array_keys(get_defined_vars());
				eval($__script);
				$__scope_after = array_keys(get_defined_vars());
				$__diff = array_diff($__scope_after, $__scope_before);
				foreach ($__diff as $__var) 
				{
					$GLOBALS[$__var] = $$__var;
				}
				if ($__compat) 
				{
				break;
				}
			}
		}
		$eval_script_output = $output;
		$output = '';
		return $eval_script_output;
	}
	return $__text;
}


/**
 * Returns $__text with all plugin calls evaluatated.
 * see plugins/index.php preCallPlugins()
 *
 * @param string $__text
 * @return string
 */
function evaluate_plugincall($__text) 
{
	global $u;

	$error = ' <span style="color:#5b0000; font-size:14px;">
	{{CALL TO:<span style="color:#c10000;">{{%1}}</span> FAILED}}</span> '; //use this for debugging of failed plugin-calls
	$pl_regex = '"{{{RGX:CALL(.*?)}}}"is'; //general CALL-RegEx (Placeholder: "RGX:CALL")
	$pl_calls = array(
	'PLUGIN:' => 'return {{%1}}',
	'HOME:' => 'return trim(\'<a href="?' . $u[0] . '" title="' . urldecode('{{%1}}') . '">' . urldecode('{{%1}}') . '</a>\');',
	'HOME' => 'return trim(\'<a href="?' . $u[0] . '" title="' . urldecode($u[0]) . '">' . urldecode($u[0]) . '</a>\');'
	);
	$fd_calls = array();
	foreach ($pl_calls AS $regex => $call) 
	{
		preg_match_all(str_replace("RGX:CALL", $regex, $pl_regex), $__text, $fd_calls[$regex]); //catch all PL-CALLS
		foreach ($fd_calls[$regex][0] AS $call_nr => $replace) 
		{
			$call = str_replace("{{%1}}", $fd_calls[$regex][1][$call_nr], $pl_calls[$regex]);
			$fnct_call = preg_replace('"(?:(?:return)\s)*(.*?)\(.*?\);"is', '$1', $call);
			$fnct = function_exists($fnct_call) ? TRUE : FALSE; //without object-calls; functions-only!!
			if ($fnct) 
			{
				preg_match_all("/\\$([a-z_0-9]*)/i", $call, $matches);
				foreach ($matches[1] as $var) 
				{
					global $$var;
				}
			}
			$__text = str_replace($replace,
			($fnct
			? eval(str_replace('{{%1}}', $fd_calls[$regex][1][$call_nr], $pl_calls[$regex]))
			: str_replace('{{%1}}', $regex . $fd_calls[$regex][1][$call_nr], $error)),
			$__text); //replace PL-CALLS (String only!!)
		}
	}
    return $__text;
}


/**
 * Returns $text with CMSimple scripting and plugin calls evaluated.
 *
 * @param string $text
 * @param bool $compat  Wheter only last CMSimple script will be evaluated.
 * @return void
 */
function evaluate_scripting($text, $compat = TRUE) 
{
	return evaluate_cmsimple_scripting(evaluate_plugincall($text), $compat);
}


/**
 * Returns content of the first CMSimple page with the heading $heading
 * with the heading removed and all scripting evaluated.
 * Returns FALSE, if the page doesn't exist.
 *
 * @param string $heading
 * @return mixed
 */
function newsbox($heading) 
{
	global $c, $cl, $h, $cf, $edit, $su, $adm, $txc;
	for ($i = 0; $i < $cl; $i++) 
	{
		if ($h[$i] == $heading) 
		{
			$body = preg_replace("/.*<\/h[1-".$cf['menu']['levels']."]>/isu", "", $c[$i]);
			return $edit ? $body : preg_replace("/".$cf['scripting']['regexp']."/is", "", evaluate_scripting($body, FALSE));
		}
	}
	return FALSE;
}


// EDITOR CALL

function init_editor($elementClasses = array(),  $initFile = false)
{
	global $pth, $cf;
	if (!file_exists($pth['folder']['plugins'] . $cf['editor']['external'] . '/init.php')) 
	{
		return false;
	}
    include_once $pth['folder']['plugins'] . $cf['editor']['external'] . '/init.php';
    $function = 'init_' . $cf['editor']['external'];

    if (!function_exists($function))
	{
		return false;
	}
	$function($elementClasses, $initFile);
	return true;
}

function include_editor()
{
	global $pth, $cf;
	if (!file_exists($pth['folder']['plugins'] . $cf['editor']['external'] . '/init.php')) 
	{
		return false;
	}
	include_once $pth['folder']['plugins'] . $cf['editor']['external'] . '/init.php';
	$function = 'include_' . $cf['editor']['external'];

	if (!function_exists($function))
	{
		return false;
	}

	$function();
	return true;
}

function editor_replace($elementID = false, $config = '')
{
	global $pth, $cf;

	if(!$elementID) 
	{
		trigger_error('No elementID given', E_USER_NOTICE);
		return false;
	}

	if (!file_exists($pth['folder']['plugins'] . $cf['editor']['external'] . '/init.php')) 
	{
		return false;
	}
	include_once $pth['folder']['plugins'] . $cf['editor']['external'] . '/init.php';
	$function = $cf['editor']['external'] . '_replace';

	if (!function_exists($function))
	{
		return false;
	}
	return $function($elementID, $config);
}

function final_clean_up($html) 
{
    global $adm, $s, $o, $debugMode, $plugins, $errors, $cf, $bjs;

    if ($adm === true) 
	{
        $debugHint = '';
        $errorList = '';
        $margin = 34;
        $style = '';

        if ($debugMode) 
		{
            $debugHint .= '<div class="cmsimplecore_debug">' . "\n" . '<b>Notice:</b> Debug-Mode is enabled!' . "\n" . '</div>' . "\n";
            $margin += 25;
        }

        global $errors;
        if(count($errors) > 0)
		{

            $errorList .= '
                <div class="cmsimplecore_warning" style="margin: 0; border-width: 0;">
                  <ul>
                  ';
            $errors =  array_unique($errors);
            foreach($errors as $error){
                $errorList .= '<li>' . $error . '</li>';
            }
            $errorList .= '</ul></div>';
        }
        if (isset($cf['adminmenu']['scroll']) && $cf['adminmenu']['scroll'] == 'true')
		{
            $style = ' style="z-index: 9999;"';
            $margin = 0;
        }
        else 
		{
             $style =' style="position: fixed; top: 0; left: 0; width: 100%; z-index: 9999;"';
	     $html = preg_replace('~</head>~','<style type="text/css">html {margin-top: ' . $margin . 'px;}</style>' ."\n" . '$0', $html, 1);
        }

        $html = preg_replace('~<body[^>]*>~',
        '$0' . '<div' . $style . '>' . $debugHint. admin_menu($plugins, $debugMode) . '</div>' ."\n" .  $errorList,
        $html, 1);
    }

	if (!empty($bjs)) 
	{
        $html = preg_replace('/(<\/body\s*>)/isu', $bjs . "\n" . '$1', $html);
    }

    return $html;
}

// GLOBAL INTERNAL FUNCTIONS

function initvar($name) 
{
    if (!isset($GLOBALS[$name])) 
	{
        if (isset($_GET[$name]))
            $GLOBALS[$name] = $_GET[$name];
        else if (isset($_POST[$name]))
            $GLOBALS[$name] = $_POST[$name];
        else
            $GLOBALS[$name] = @preg_replace("/.*?(" . $name . "=([^\&]*))?.*?/i", "\\2", sv('QUERY_STRING'));
    }
}

function sv($s) 
{
    if (!isset($_SERVER)) 
	{
        global $_SERVER;
        $_SERVER = $GLOBALS['HTTP_SERVER_VARS'];
    }
    if (isset($_SERVER[$s]))
        return $_SERVER[$s];
    else
        return'';
}

function rmnl($t) 
{
    return preg_replace("/(\r\n|\r|\n)+/", "\n", $t);
}

function rmanl($t) 
{
    return preg_replace("/(\r\n|\r|\n)+/", "", $t);
}

function stsl($t) 
{
    if (get_magic_quotes_gpc())
        return stripslashes($t); else
        return $t;
}

function download($fl) 
{
    global $sn, $download, $tx;
    if (!is_readable($fl) || ($download != '' && !chkdl($sn . '?download=' . basename($fl)))) 
	{
        global $o, $text_title;
        shead('404');
        $o .= '<p>File ' . $fl . '</p>';
        return;
    } 
	else 
	{
        header('Content-Type: application/save-as');
        header('Content-Disposition: attachment; filename="' . basename($fl) . '"');
        header('Content-Length:' . filesize($fl));
        header('Content-Transfer-Encoding: binary');
        if ($fh = @fopen($fl, "rb")) 
		{
            while (!feof($fh))
                echo fread($fh, filesize($fl));
            fclose($fh);
        }
        exit;
    }
}

function chkdl($fl) 
{
    global $pth, $sn;
    $m = false;
    if (@is_dir($pth['folder']['downloads'])) 
	{
        $fd = @opendir($pth['folder']['downloads']);
        while (($p = @readdir($fd)) == true) 
		{
            if (preg_match("/.+\..+$/", $p)) 
			{
                if ($fl == $sn . '?download=' . $p)
                    $m = true;
            }
        }
        if ($fd == true)
            closedir($fd);
    }
    return $m;
}

function rf($fl) 
{
    $fl = @rp($fl);
    if (!file_exists($fl))
        return;
    clearstatcache();
    if (function_exists('file_get_contents'))
        return file_get_contents($fl);
    else 
	{
        return join("\n", file($fl));
    }
}

function chkfile($fl, $writable) 
{
    global $pth, $tx;
    $t = @rp($pth['file'][$fl]);
    if ($t == '')
        e('undefined', 'file', $fl);
    else if (!file_exists($t))
        e('missing', $fl, $t);
    else if (!is_readable($t))
        e('notreadable', $fl, $t);
    else if (!is_writable($t) && $writable)
        e('notwritable', $fl, $t);
}

function e($et, $ft, $fn) 
{
    global $e, $tx;
    $e .= '<li><b>' . $tx['error'][$et] . ' ' . $tx['filetype'][$ft] . '</b>' . tag('br') . $fn . '</li>' . "\n";
}

function rfc() 
{
    global $c, $cl, $h, $u, $l, $su, $s, $pth, $tx, $edit, $adm, $cf;

    $c = array();
    $h = array();
    $u = array();
    $l = array();
    $empty = 0;
    $duplicate = 0;

    $content = file_get_contents($pth['file']['content']);
    $stop = $cf['menu']['levels'];
    $split_token = '#@CMSIMPLE_SPLIT@#';


    $content = preg_split('~</body>~i', $content);
    $content = preg_replace('~<h[1-' . $stop . ']~i', $split_token . '$0', $content[0]);
    $content = explode($split_token, $content);
    array_shift($content);

    foreach ($content as $page) 
	{
        $c[] = $page;
        preg_match('~<h([1-' . $stop . ']).*>(.*)</h~isU', $page, $temp);
        $l[] = $temp[1];
        $temp_h[] = preg_replace('/[ \f\n\r\t\xa0]+/isu', ' ', trim(strip_tags($temp[2])));
    }

    $cl = count($c);
    $s = -1;

    if ($cl == 0) 
	{
        $c[] = '<h1>' . $tx['toc']['newpage'] . '</h1>';
        $h[] = trim(strip_tags($tx['toc']['newpage']));
        $u[] = uenc($h[0]);
        $l[] = 1;
        $s = 0;
        return;
    }

    $ancestors = array();  /* just a helper for the "url" construction:
     * will be filled like this [0] => "Page"
     *                          [1] => "Subpage"
     *                          [2] => "Sub_Subpage" etc.
     */

    foreach ($temp_h as $i => $heading) 
	{
        $temp = trim(strip_tags($heading));
        if ($temp == '') 
		{
            $empty++;
            $temp = $tx['toc']['empty'] . ' ' . $empty;
        }
        $h[] = $temp;
        $ancestors[$l[$i] - 1] = uenc($temp);
        $ancestors = array_slice($ancestors, 0, $l[$i]);
        $url = implode($cf['uri']['seperator'], $ancestors);
        $u[] = substr($url, 0, $cf['uri']['length']);
    }

    foreach ($u as $i => $url) 
	{
        if ($su == $u[$i] || $su == urlencode($u[$i])) 
		{
            $s = $i;
        } // get index of selected page

        for ($j = $i + 1; $j < $cl; $j++) {   //check for duplicate "urls"
            if ($u[$j] == $u[$i]) 
			{
                $duplicate++;
                $h[$j] = $tx['toc']['dupl'] . ' ' . $duplicate;
                $u[$j] = uenc($h[$j]);
            }
        }
    }
    if (!($edit && $adm)) 
	{
        foreach ($c as $i => $j) 
		{
            if (cmscript('remove', $j)) 
			{
                $c[$i] = '#CMSimple hide#';
            }
        }
    }
}

function a($i, $x) 
{
    global $sn, $u, $cf, $adm;
    if ($i == 0 && !$adm) {
        if ($x == '' && $cf['locator']['show_homepage'] == 'true') 
		{
            return '<a href="' . $sn . '?' . $u[0] . '">';
        }
    }
    return isset($u[$i]) ? '<a href="' . $sn . '?' . $u[$i] . $x . '">' : '<a href="' . $sn . '?' . $x . '">'; // changed by LM CMSimple_XH 1.1
}

function meta($n) 
{
    global $cf, $print;
    $exclude = array('robots', 'keywords', 'description');
    if ($cf['meta'][$n] != '' && !($print && in_array($n, $exclude)))
        return tag('meta name="' . $n . '" content="' . htmlspecialchars($cf['meta'][$n], ENT_COMPAT, 'UTF-8') . '"') . "\n";
}

function ml($i) 
{
    global $f, $sn, $tx;
    $t = '';
    if ($f != $i)
        $t .= '<a href="' . $sn . '?' . amp() . $i . '">';
    $t .= $tx['menu'][$i];
    if ($f != $i)
        $t .= '</a>';
    return $t;
}

function uenc($s) 
{
    global $tx;
    if (isset($tx['urichar']['org']) && isset($tx['urichar']['new']))
        $s = str_replace(explode(",", $tx['urichar']['org']), explode(",", $tx['urichar']['new']), $s);
    return str_replace('+', '_', urlencode($s));
}

function rp($p) 
{
    if (@realpath($p) == '')
        return $p;
    else
        return realpath($p);
}

function sortdir($dir) 
{
    $fs = array();
    $fd = @opendir($dir);
    while (false !== ($fn = @readdir($fd))) 
	{
        $fs[] = $fn;
    }
    if ($fd == true)
        closedir($fd);
    @sort($fs, SORT_STRING);
    return $fs;
}

function cmscript($s, $i) 
{
    global $cf;
    return preg_match(preg_replace("/\(\.\*\?\)/", $s, "/" . $cf['scripting']['regexp'] . "/is"), $i);
}

function hide($i) 
{
    global $c, $edit, $adm;
    if ($i < 0) 
	{
        return false;
    }
    return (!($edit && $adm) && cmscript('hide', $c[$i]));
}

// For valid XHTML
function tag($s) 
{
    global $cf;
    $t = '';
    if ($cf['xhtml']['endtags'] == 'true')
        $t = ' /';
    return '<' . $s . $t . '>';
}

function amp() 
{
    global $cf;
    if ($cf['xhtml']['amp'] == 'true')
        return '&amp;';
    else
        return('&');
}

function shead($s) 
{
    global $iis, $cgi, $tx, $title, $o;
    if ($s == '401')
        header(($cgi || $iis) ? 'status: 401 Unauthorized' : 'HTTP/1.0 401 Unauthorized');
    if ($s == '404') 
	{
		if (function_exists('custom_404')) 
		{
			custom_404();
		} 
		else 
		{
			header(($cgi || $iis) ? 'status: 404 Not Found' : 'HTTP/1.0 404 Not Found');
		}
    }
    $title = $tx['error'][$s];
    $o = '<h1>' . $title . '</h1>' . $o;
}

/**
 * Debug-Mode
 * Check if file "_XHdebug.txt" exists to turn on debug-mode
 * with default setting E_ERROR | E_USER_WARNING | E_PARSE.
 * Level of debug mode can be adjusted by placing an
 * integer-value within the file using following values:
 *
 * Possible values of $dbglevel:
 *   0 - Turn off all error reporting
 *   1 - Running errors except warnings
 *   2 - Running errors
 *   3 - Running errors + notices
 *   4 - All errors except notices and warnings
 *   5 - All errors except notices
 *   6 - All errors
 *
 * @author Holger
 * @since CMSimple_XH V.1.0rc3 / Pluginloader V.2.1 beta 9
 *
 * @global array $pth CMSimple's pathes
 * @return boolean Returns true/false if error_reporting was enabled or not
 */
function xh_debugmode() 
{
    global $pth;
    $dbglevel = '';

    # possible values of $dbglevel:
    # 0 - Turn off all error reporting
    # 1 - Running errors except warnings
    # 2 - Running errors
    # 3 - Running errors + notices
    # 4 - All errors except notices and warnings
    # 5 - All errors except notices
    # 6 - All errors

    if (file_exists($pth['folder']['downloads'] . '_XHdebug.txt')) 
	{
        ini_set('display_errors', 1);
        $dbglevel = rf($pth['folder']['downloads'] . '_XHdebug.txt');
        if (strlen($dbglevel) == 1) 
		{
            set_error_handler('xh_debug');

            switch ($dbglevel) 
			{
                case 0: error_reporting(0);
                    break;
                case 1: error_reporting(E_ERROR | E_USER_WARNING | E_PARSE);
                    break;
                case 2: error_reporting(E_ERROR | E_WARNING | E_USER_WARNING | E_PARSE);
                    break;
                case 3: error_reporting(E_ERROR | E_WARNING | E_USER_WARNING | E_PARSE | E_NOTICE);
                    break;
                case 4: error_reporting(E_ALL ^ (E_NOTICE | E_WARNING | E_USER_WARNING));
                    break;
                case 5: error_reporting(E_ALL ^ E_NOTICE);
                    break;
                case 6: error_reporting(E_ALL);
                    break;
                default:
                    error_reporting(E_ERROR | E_USER_WARNING | E_PARSE);
            }
        } 
		else 
		{
            error_reporting(E_ERROR | E_USER_WARNING | E_PARSE);
        }
    } 
	else 
	{
        ini_set('display_errors', 0);
        error_reporting(0);
    }
    if (error_reporting() > 0) 
	{
        return true;
    } 
	else 
	{
        return false;
    }
}

function xh_debug($errno, $errstr, $errfile, $errline, $context)
{
    global $errors;

    if (!(error_reporting() & $errno)) 
	{
        // This error code is not included in error_reporting
        return;
    }

    switch ($errno) 
	{
		case E_USER_ERROR:
        $errors[] = "<b>XH-ERROR:</b> [$errno] $errstr <br /> $errfile:$errline<br />\n";
        break;

		case E_USER_WARNING:
        echo "<b>XH WARNING:</b>  $errstr <br /> $errfile: $errline<br />\n";
        break;

		case E_USER_NOTICE:
        $errors[] = "<b>XH-NOTICE:</b> [$errno] $errstr <br />$errfile:$errline<br />\n";
        break;

		case E_WARNING:
        $errors[] = "<b>WARNING:</b> $errno $errstr <br />$errfile:$errline<br />\n";
        break;

		case E_NOTICE:
        $errors[] = "<b>NOTICE:</b> $errstr <br />$errfile:$errline<br />\n";
        break;


		case E_ERROR:
        $errors[] = "<b>ERROR:</b> $errstr <br />$errfile:$errline<br />\n";
        break;

		default:
        echo "Unknown error type: [$errno] $errstr<br />$errfile:$errline<br />\n";
        break;
    }

  //  error_log($error, 3, CMS_DIR .'errors.log');
    /* Don't execute PHP internal error handler */

    return true;
}


// PAGE FUNCTIONS
// new function head() ready for html5 - by GE 2009/06 (CMSimple_XH beta)

function head() 
{
    global $title, $cf, $pth, $tx, $hjs;
    $t = htmlspecialchars($cf['site']['title'], ENT_COMPAT, 'UTF-8');
    if (!empty($t) && !empty($title)) 
    {
        $t .= ' - ';
    }
    $t .= $title; 
    $t = '<title>' . strip_tags($t) . '</title>' . "\n";
    foreach ($cf['meta'] as $i => $k)
        $t .= meta($i);
    if ($tx['meta']['codepage'] != '')
        $t = tag('meta http-equiv="content-type" content="text/html;charset=' . $tx['meta']['codepage'] . '"') . "\n" . $t;
    return $t . tag('meta name="generator" content="' . CMSIMPLE_XH_VERSION . ' ' . CMSIMPLE_XH_BUILD . ' - www.cmsimple.org"') . "\n" . tag('link rel="stylesheet" href="' . $pth['file']['corestyle'] . '" type="text/css"') . "\n" . tag('link rel="stylesheet" href="' . $pth['file']['stylesheet'] . '" type="text/css"') . "\n" . $hjs;
}

// END new function head() (CMSimple_XH)


function sitename() 
{
    global $cf;
    return isset($cf['site']['title']) ? $cf['site']['title'] : ''; // changed by GE CMSimple_XH 1.2
}

function pagename() 
{ // changed by GE CMSimple_XH 1.2
    global $cf;
    return isset($cf['site']['title']) ? $cf['site']['title'] : ''; // changed by LM CMSimple_XH 1.1
}

function onload() 
{
    global $onload;
    return ' onload="' . $onload . '"';
}

function toc($start = NULL, $end = NULL)  // changed by LM CMSimple_XH 1.1
{
	global $c, $cl, $s, $l, $cf, $adm, $su;
	if (isset($start)) 
	{
		if (!isset($end))
			$end = $start;
	}
	else
		$start = 1;
	if (!isset($end))
		$end = $cf['menu']['levels'];
	$ta = array();
	if ($s > -1) 
	{
		$tl = $l[$s];
		for ($i = $s; $i > -1; $i--) 
		{
			if ($l[$i] <= $tl && $l[$i] >= $start && $l[$i] <= $end)
				if (!hide($i) || ($i == $s && $cf['hidden']['pages_toc'] == 'true'))
					$ta[] = $i;
			if ($l[$i] < $tl)
				$tl = $l[$i];
		}
		@sort($ta);
		$tl = $l[$s];
	}
	else
		$tl = 0;
	$tl += 1 + $cf['menu']['levelcatch'];
	for ($i = $s + 1; $i < $cl; $i++) 
	{
		if ($l[$i] <= $tl && $l[$i] >= $start && $l[$i] <= $end)
			if (!hide($i))
				$ta[] = $i;
		if ($l[$i] < $tl)
			$tl = $l[$i];
	}
	return li($ta, $start);
}

function li($ta, $st) 
{
    global $s, $l, $h, $cl, $cf, $u;
    $tl = count($ta);
    if ($tl < 1)
        return;
    $t = '';
    if ($st == 'submenu' || $st == 'search')
        $t .= '<ul class="' . $st . '">' . "\n";
    $b = 0;
    if ($st > 0) 
	{
        $b = $st - 1;
        $st = 'menulevel';
    }
    $lf = array();
    for ($i = 0; $i < $tl; $i++) 
	{
        $tf = ($s != $ta[$i]);
        if ($st == 'menulevel' || $st == 'sitemaplevel') 
		{
            for ($k = (isset($ta[$i - 1]) ? $l[$ta[$i - 1]] : $b); $k < $l[$ta[$i]]; $k++)
                $t .= "\n" . '<ul class="' . $st . ($k + 1) . '">' . "\n";
        }
        $t .= '<li class="';
        if (!$tf)
            $t .= 's';
        else if (@$cf['menu']['sdoc'] == "parent" && $s > -1) 
		{
            if ($l[$ta[$i]] < $l[$s]) 
			{
                if (@substr($u[$s], 0, strlen($cf['uri']['seperator']) + strlen($u[$ta[$i]])) == $u[$ta[$i]] . $cf['uri']['seperator'])
                    $t .= 's';
            }
        }
        $t .= 'doc';
        for ($j = $ta[$i] + 1; $j < $cl; $j++)
            if (!hide($j) && $l[$j] - $l[$ta[$i]] < 2 + $cf['menu']['levelcatch']) 
			{
                if ($l[$j] > $l[$ta[$i]])
                    $t .= 's';
                break;
            }
        $t .= '">';
		
		if(!$tf)
		{
			$t.='<span>';
		}
		
		if ($tf)
			$t .= a($ta[$i], '');
		$t .= $h[$ta[$i]];
		
		if ($tf)
		{
			$t .= '</a>';
		}
		else
		{
			$t .= '</span>';
		}
		
		if ($st == 'menulevel' || $st == 'sitemaplevel') 
		{
            if ((isset($ta[$i + 1]) ? $l[$ta[$i + 1]] : $b) > $l[$ta[$i]])
                $lf[$l[$ta[$i]]] = true;
            else 
			{
                $t .= '</li>' . "\n";
                $lf[$l[$ta[$i]]] = false;
            }
            for ($k = $l[$ta[$i]]; $k > (isset($ta[$i + 1]) ? $l[$ta[$i + 1]] : $b); $k--) 
			{
                $t .= '</ul>' . "\n";
                if (isset($lf[$k - 1]))
                    if ($lf[$k - 1]) 
					{
                        $t .= '</li>' . "\n";
                        $lf[$k - 1] = false;
                    }
            };
        }
        else
            $t .= '</li>' . "\n";
    }
    if ($st == 'submenu' || $st == 'search')
        $t .= '</ul>' . "\n";
    return $t;
}

function searchbox() 
{
	global $sn, $tx;
	return '<form action="' . $sn . '" method="get">' . "\n" 
	. '<div id="searchbox">' . "\n" 
	. tag('input type="text" class="text" name="search" size="12"') . "\n" 
	. tag('input type="hidden" name="function" value="search"') . "\n" . ' ' 
	. tag('input type="submit" class="submit" value="' . $tx['search']['button'] . '"') . "\n" 
	. '</div>' . "\n" . '</form>' . "\n";
}

function sitemaplink() 
{
	global $adm;
	return ml('sitemap');
}

function printlink() 
{
	global $f, $search, $file, $sn, $tx, $adm;
	$t = amp() . 'print';
	if ($f == 'search')
		$t .= amp() . 'function=search' . amp() . 'search=' . htmlspecialchars(stsl($search), ENT_COMPAT, 'UTF-8');
	else if ($f == 'file')
		$t .= amp() . 'file=' . $file;
	else if ($f != '' && $f != 'save')
		$t .= amp() . $f;
	else if (sv('QUERY_STRING') != '')
		$t = htmlspecialchars(sv('QUERY_STRING'), ENT_COMPAT, "UTF-8") . $t;
		return '<a href="' . $sn . '?' . $t . '">' . $tx['menu']['print'] . '</a>';
}

function mailformlink() {
    global $cf;
    if ($cf['mailform']['email'] != '')
        return ml('mailform');
}

function guestbooklink() {
    if (function_exists('gblink'))
        return gblink();
}

function loginlink()
{
    global $cf, $adm, $sn, $u, $s, $tx;
    if (!$adm) 
	{
        if ($cf['security']['type'] == 'javascript')
            return '<a href="javascript:login()">' . $tx['menu']['login'] . '</a><form id="login" style="display: inline; display: none;" action="' . $sn . '" method="post">' . tag('input type="hidden" name="login" value="true"') . tag('input type="hidden" name="selected" value="' . $u[$s] . '"') . tag('input type="hidden" name="passwd" id="passwd" value=""') . '</form>';
        else
            return a($s > -1 ? $s : 0, '&amp;login') . $tx['menu']['login'] . '</a>';
    }
}

function lastupdate($br = NULL, $hour = NULL) { // changed by LM CMSimple_XH 1.1
    global $tx, $pth;
    $t = $tx['lastupdate']['text'] . ':';
    if (!(isset($br)))
        $t .= tag('br');
    else
        $t .= ' ';
    return $t . date($tx['lastupdate']['dateformat'], filemtime($pth['file']['content']) + (isset($hour) ? $hour * 3600 : 0));
}

function legallink() {
    global $cf, $sn; // changed by LM CMSimple_XH 1.1
    return '<a href="' . $sn . '?' . uenc($cf['menu']['legal']) . '">' . $cf['menu']['legal'] . '</a>';
}

function locator() 
{
    global $title, $h, $s, $f, $c, $l, $tx, $adm, $cf;
	if (hide($s) && $cf['hidden']['path_locator'] != 'true')
		return $h[$s];
	if ($title != '' && (!isset($h[$s]) || $h[$s] != $title))
		return $title;
	$t = '';
	if ($s == 0)
		return $h[$s];
	elseif ($f != '')
		return ucfirst($f);
	elseif ($s > 0) {
		$tl = $l[$s];
		if ($tl > 1) {
			for ($i = $s - 1; $i >= 0; $i--) {
				if ($l[$i] < $tl) {
					$t = a($i, '') . $h[$i] . '</a> &gt; ' . $t;
					$tl--;
				}
				if ($tl < 2)
					break;
			}
		}
		if ($cf['locator']['show_homepage'] == 'true') {
		return a(0, '') . $tx['locator']['home'] . '</a> &gt; ' . $t . $h[$s];
		} 
		else 
		{
			return $t . $h[$s];
		}
	}
	else
	{
		return '&nbsp;';
	}
}

function editmenu() 
{
    return '';
}

function admin_menu($plugins = array(), $debug = false)
{
	global $adm, $edit, $s, $u, $sn, $tx, $sl, $cf, $su, $pth, $disabled_plugins, $active_plugins; // pluginmanager (added "$pth, $disabled_plugins")

	if ($adm)
	{
		$pluginMenu = '';
		if ((bool) $plugins)
		{
		
// pluginmanager (changed)

			$pluginMenu .= '<li>';
			
			$pluginMenu .= '<a href="?&normal&xhpluginmanager">' . ucfirst($tx['adminmenu']['plugins']) . '</a>' . "\n";
			
			if(count($active_plugins) > $cf['adminmenu']['narrow_max'])
			{
				$pluginMenu .= '<ul style="width: 460px; margin-left: -325px;">';
			}
			else
			{
			$pluginMenu .= '<ul>';
			}

			foreach ($active_plugins as $plugin)
			{
				if(count($active_plugins) > $cf['adminmenu']['narrow_max'])
				{
					if(!stristr($disabled_plugins, '§'.$plugin.'§'))
					{
						$pluginMenu .= "\n" . '     <li style="width: 150px; float: left;"><a href="?' . $plugin . '&amp;normal">' . $plugin . '</a></li>';
					}
				}
				else
				{
					if(!stristr($disabled_plugins, '§'.$plugin.'§'))
					{
						$pluginMenu .= "\n" . '     <li><a href="?' . $plugin . '&amp;normal">' . $plugin . '</a></li>';
					}
				}

			}
			$pluginMenu .= "\n" . '</ul>';


// END pluginmanager (changed)
		}


		$t .= "\n" . '<div id="adminmenu">';

		$t .= "\n" . '<ul id="admin_menu">' . "\n";

		if ($s < 0)
		{
			$su = $u[0];
		}
		$changeMode = $edit ? 'normal' : 'edit';
		$changeText = $edit ? $tx['adminmenu']['normal'] : $tx['adminmenu']['edit'];
		$t .= '<li><a href="' . $sn . '?' . $su . '&' . $changeMode . '">' . $changeText . '</a></li>' . "\n";
		$t .= '<li><a href="' . $sn . '?&amp;normal&amp;xhpages" class="">' . ucfirst($tx['adminmenu']['pagemanager']) . '</a></li>' . "\n";
		$t .= '<li><a href="' . $sn . '?&amp;normal&amp;userfiles">' . ucfirst($tx['adminmenu']['files']) . '</a>' ."\n";
		$t .= '    <ul>' . "\n";
		$t .= '    <li><a href="' . $sn . '?&amp;normal&amp;images">' . ucfirst($tx['adminmenu']['images']) . '</a></li>' . "\n";
		$t .= '    <li><a href="' . $sn . '?&amp;normal&amp;downloads">' . ucfirst($tx['adminmenu']['downloads']) . '</a></li>' . "\n";
		$t .= '    <li><a href="' . $sn . '?&amp;normal&amp;media">' . ucfirst($tx['adminmenu']['media']) . '</a></li>' . "\n";
		$t .= '    </ul>' . "\n";
		$t .= '</li>' ."\n";
		$t .= '<li><a href="' . $sn . '?&amp;settings">' . ucfirst($tx['adminmenu']['settings']) . '</a>' ."\n"
					. '    <ul>' ."\n";

		if($cf['site']['full_settings_menu'] == 'true') // CoAuthors
		{
				$t .='    <li><a href="?file=config&amp;action=array">' . ucfirst($tx['adminmenu']['configuration']) . '</a></li>' . "\n";

			$t .= '    <li><a href="?file=language&amp;action=array">' . ucfirst($tx['adminmenu']['language']) . '</a></li>' . "\n"
			. '    <li><a href="?file=template&amp;action=edit">' . ucfirst($tx['adminmenu']['template']) . '</a></li>' . "\n"
			. '    <li><a href="?file=stylesheet&amp;action=edit">' . ucfirst($tx['adminmenu']['stylesheet']) . '</a></li>' . "\n"
			. '    <li><a href="?file=log&amp;action=view" target="_blank">' . ucfirst($tx['adminmenu']['log']) . '</a></li>' . "\n";
		}
		
		$t .='    <li><a href="' . $sn . '?&amp;validate">' . ucfirst($tx['adminmenu']['validate']) . '</a></li>' . "\n"
		. '    <li><a href="' . $sn . '?&amp;sysinfo">' . ucfirst($tx['adminmenu']['sysinfo']) . '</a></li>' . "\n"
		. '    </ul>' . "\n"
		. '</li>' . "\n"
		. $pluginMenu . "\n"
		. '</li>' . "\n";
		$t .= '</ul>' . "\n" . '<ul id="adminmenu_logout">' . "\n";
		$t .= '<li id="admin_menu_logout"><a href="?&logout">' . ucfirst($tx['adminmenu']['logout']) . '</a></li>' . "\n";
		$t .= '</ul>' . "\n";

		return $t . '<div style="float:none;clear:both;padding:0;margin:0;width:100%;height:0px;"></div>' . "\n" . '</div>' . "\n";
	}
}

function content() 
{
    global $s, $o, $c, $edit, $adm, $cf, $tx;
	if (!($edit && $adm) && $s > -1) 
	{
		if (isset($_GET['search'])) 
		{
			$words = explode(',', urldecode($_GET['search']));
			$words = array_map(create_function('$w', 'return "&".$w."(?!([^<]+)?>)&isU";'), $words);
			$c[$s] = preg_replace($words, '<span class="highlight_search">\\0</span>', $c[$s]);
		}
		return $o . preg_replace("/" . $cf['scripting']['regexp'] . "/is", "", $c[$s]);
	}
	else 
	{
		return $o;
	}
}

function submenu() 
{
	global $s, $cl, $l, $tx, $cf, $adm;
	$ta = array();
	if ($s > -1) {
		$tl = $l[$s] + 1 + $cf['menu']['levelcatch'];
		for ($i = $s + 1; $i < $cl; $i++) {
			if ($l[$i] <= $l[$s])
				break;
			if ($l[$i] <= $tl)
				if (!hide($i))
					$ta[] = $i;
			if ($l[$i] < $tl)
				$tl = $l[$i];
		}
		if (count($ta) != 0)
		{
			return '<h4>' . $tx['submenu']['heading'] . '</h4>' . li($ta, 'submenu');
		}
	}
}

function previouspage() 
{
	global $s, $cl, $tx, $adm;
	for ($i = $s - 1; $i > -1; $i--)
		if (!hide($i))
		{
			return a($i, '') . $tx['navigator']['previous'] . '</a>';
		}
}

function nextpage() 
{
	global $s, $cl, $tx, $adm;
	for ($i = $s + 1; $i < $cl; $i++)
		if (!hide($i))
		{
			return a($i, '') . $tx['navigator']['next'] . '</a>';
		}
}

function top() 
{
	global $tx, $adm;
		return '<a href="#TOP">' . $tx['navigator']['top'] . '</a>';
}

// tagged img-tags in function languagemenu() - by GE 09-06-26 (CMSimple_XH beta3)
// title-tags for flag-gifs - by GE 09-10-07 (CMSimple_XH 1.0rc2)

function languagemenu() 
{
	global $pth, $cf, $sl, $sn;
	if(!file_exists('./cmsimplesubsite.htm') && $pth['folder']['base']!='../../') // for subsites
	{ 
		$t = '';
		$r = array();
		$fd = @opendir($pth['folder']['base']);
		while (($p = @readdir($fd)) == true ) 
		{
			if (@is_dir($pth['folder']['base'].$p)) 
			{
				if (preg_match('/^[A-z]{2}$/', $p))$r[] = $p;
			}
		}
		if ($fd == true)closedir($fd); 
		if(count($r) == 0)return ''; 
		if($cf['language']['default'] != $sl)
		{
			$t .= '<a href="'.$pth['folder']['base'].'">'.tag('img src="'.$pth['folder']['flags'].$cf['language']['default'].'.gif" alt="'.$cf['language']['default'].'" title="&nbsp;'.$cf['language']['default'].'&nbsp;" class="flag"').'</a> '; 
		}
		$v = count($r); for($i = 0;
		$i < $v;
		$i++) 
		{
			if ($sl != $r[$i]) 
			{
				if (is_file($pth['folder']['flags'].'/'.$r[$i].'.gif')) 
				{
					$t .= '<a href="'.$pth['folder']['base'].$r[$i].'/">'.tag('img src="'.$pth['folder']['flags'].$r[$i].'.gif" alt="'.$r[$i].'" title="&nbsp;'.$r[$i].'&nbsp;" class="flag"').'</a> ';
				} 
				else 
				{
					$t .= '<a href="'.$pth['folder']['base'].$r[$i].'/">['.$r[$i].']</a> ';
				}
			}
		}
		return ''.$t.'';
	}
	
	if($pth['folder']['base']=='../../') // for subsites
	{ 
		$t = '';
		$r = array();
		$fd = @opendir('../');
		while (($p = @readdir($fd)) == true ) 
		{
			if (@is_dir('../'.$p)) 
			{
				if (preg_match('/^[A-z]{2}$/', $p))
				{
					$r[] = $p;
				}
			}
		}
		if ($fd == true)closedir($fd); 
		if(count($r) == 0)return ''; 
		if($cf['language']['default'] != $sl)
		{
			$t='<a href="../">'.tag('img src="'.$pth['folder']['flags'].$cf['language']['default'].'.gif" alt="'.$cf['language']['default'].'" title="&nbsp;'.$cf['language']['default'].'&nbsp;" class="flag"').'</a> ';
		}
		else
		{
			$t .= ''; 
		}
		$v = count($r); for($i = 0;
		$i < $v;
		$i++) 
		{
			if ($sl != $r[$i]) 
			{
				if (is_file($pth['folder']['flags'].'/'.$r[$i].'.gif')) 
				{
					$t .= '<a href="../'.$r[$i].'/">'.tag('img src="'.$pth['folder']['flags'].$r[$i].'.gif" alt="'.$r[$i].'" title="&nbsp;'.$r[$i].'&nbsp;" class="flag"').'</a> ';
				} 
				else 
				{
					$t .= '<a href="../'.$r[$i].'/">['.$r[$i].']</a> ';
				}
			}
		}
		return ''.$t.'';
	} // END for subsites
	
	if($pth['folder']['base']=='../') // for subsites
	{ 
		$t = '';
		$r = array();
		$fd = @opendir('./');
		while (($p = @readdir($fd)) == true ) 
		{
			if (@is_dir($p)) 
			{
				if (preg_match('/^[A-z]{2}$/', $p))
				{
					$r[] = $p;
				}
			}
		}
		if ($fd == true)closedir($fd); 
		if(count($r) == 0)return ''; 
		
		$v = count($r); for($i = 0;
		$i < $v;
		$i++) 
		{
			if ($sl != $r[$i]) 
			{
				if (is_file($pth['folder']['flags'].'/'.$r[$i].'.gif')) 
				{
					$t .= '<a href="'.$r[$i].'/">'.tag('img src="'.$pth['folder']['flags'].$r[$i].'.gif" alt="'.$r[$i].'" title="&nbsp;'.$r[$i].'&nbsp;" class="flag"').'</a> ';
				} 
				else 
				{
					$t .= '<a href="'.$r[$i].'/">['.$r[$i].']</a> ';
				}
			}
		}
		return ''.$t.'';
	} // END for subsites
}
// END modified function languagemenu() - by GE 2011-10-18 (CMSimple_XH beta3)

?>
Return current item: CMSimple