<?PHP
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This file is part of Travelsized CMS
 *              A content management system with modules, based on wiki syntax
 *
 * Author: Dan Jensen <admin@leinir.dk>
 * Copyright 2003/2004
 *
 * 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 2 of the License, or
 * (at your option) any later version.
 *
 * The GNU General Public License is available at: http://www.gnu.org/copyleft/
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
include("documentation.php");
/**************************************************************************
* SETUP INFORMATION - THIS IS CHANGABLE DURING INITIAL SETUP OF THE SITE!
***************************************************************************/

// Basic information about the owner of the system (used in many places) {
$owner_name = "Dan Jensen";
$owner_email = "admin@NOSPAMleinir.dk";
// Basic information about the owner of the system (used in many places) }

// This is where the setup files are to be put {
$setup_folder = "setup";
// This is where the setup files are to be put }

// If this is set to true, all users can delete themselves as user {
$let_users_delete_self = false;
// If this is set to true, all users can delete themselves as user }

// Set the default language
// Values are standard ISO two-letter language definitions
//
// For example:
// en = English
// dk = Danish
// de = German
// es = Spanish
$default_language = "en";

/**************************************************************************
* SETUP INFORMATION ENDS HERE - NO USER SETUP BELLOW THIS POINT!
***************************************************************************/

// Script execution timer start {
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
// Script execution timer start }

// Bad form, but it's needed for the thumbnailer to work with largeish pictures
ini_set( "memory_limit","64M" );
ini_set( "date.timezone", "UTC" );

// Set the language
$language = $_REQUEST['language'];
if ($language == "") $language = $default_language;


// Define variables determining where stuff goes... {
$password_file = "$setup_folder/password.txt";
$recent_file = "$setup_folder/recent_pages.txt";
$page_folder = "$setup_folder/pages";
$menu_folder = "$setup_folder/menus";
$module_folder = "$setup_folder/modules";
$files_folder = "$setup_folder/files";
$userinfo_folder = "$setup_folder/userinfo";
// Define variables determining where stuff goes... }

//Setup folders must exist
/* This does not, for some reason, work on some servers... (Security issues I suppose...)
if (!file_exists($setup_folder)) mkdir($setup_folder,0777);
if (!file_exists($page_folder)) mkdir($page_folder,0777);
if (!file_exists($menu_folder)) mkdir($menu_folder,0777);
if (!file_exists($files_folder)) mkdir($files_folder,0777);
touch($recent_file);
*/

// Update scripts {
include("update.php");
updateTo020();
updateTo030();
// Update scripts }

//The person who made the system
$creator_name = "Dan Jensen";
$creator_email = "admin@NOSPAMleinir.dk";

//$usergroups_global_default = "";

// Set the system options defaults, then include them system options
$systemOptions = array(
	"locked" => 0,
	"timeformat" => "M j Y G:i",
	"rewrite" => 0,
	"humanurls" => 0
	);

if( file_exists( "$setup_folder/systemOptions.php" ) )
	include( "$setup_folder/systemOptions.php" );

// A bunch of shared functions, also loads profile options
include("shared.php");

// Internationalisation stuff
include("i18n-functions.php");
include("i18n.php");

include("page.class.php"); // Page functions
$pagectl = new Page; // Create a page control to work with

// Get list of recently changed pages
include("recentchanges.php");

// Get list over theme names and folders
include("theme_list.php");

// Authentication subsystem
include("auth.php");
if ($_REQUEST["logout"] == "true") unauth();

$usermanager = new Userman;
if ($_REQUEST["username"] != "" && $_REQUEST["password"] != "" && !$_REQUEST["new_user"] && $_REQUEST["logout"] != "true") {
	auth($_REQUEST['username'], $_REQUEST['password']);
} else {
	auth();
}

// Comments handling functionality
include("comment_handler.php");

// Messaging and subscription subsystem
include "messages.php";
$subscriptions = new subscriptionsManager;

// Get the current page_id and get the data that goes with it {
$page_id = $_REQUEST["page_id"];
if(!$page_id){ //If we didn't get a page_id
	if( $_REQUEST["page"] ) //if $page is present
		$page_id = getpage_id($setup_folder, $_REQUEST["page"]); //get $page_id by backtracing the $page
	else if( $_REQUEST["globalID"] ) // If we have a global ID
	{
		$globalID = $_REQUEST["globalID"];
		$page_id = handleGlobalID();
	}
	else
		$page_id = 0;
}
// Get the current page_id and get the data that goes with it }

// Are we adding a new user, or is this a user profile? {
if ($_REQUEST["user_id"] != "") $page_id = 13;
if ($_REQUEST["new_user"]) header("Location: " . globalIDtoURL("user/" . i18n("username") . "/new") );
// Are we adding a new user, or is this a user profile? }

// Language links
// Needs to be here (we need the page_id, and we don't have that before now)
include("languagelinks.php");

// Formatting toolbar used for editing page data.
include("formattingtoolbar.php");

/*List of $page_ids
0 = Front page
1 = About the system, and who has made it
2 = Tag wall
3 = Site setup (links to editing pages, menu menus, users, themes)
4 = User setup (make, delete users, change password)
5 = Manage pages (delete, update $page-to-$page_id backwards lookup index (setup/page_backwards.txt))
6 = Edit page (include $edit_id (or an $edit_title for creating a new). Update recentchanges.php on exit (rotate_recent($new_id) function in recentchanges.php)
7 = Manage the menu menus
8 = Edit $menu_menu (include $menu_id)
9 = Theme management
10 = List of all pages
11 = File management (downloads...)
12 = Module setup
13 = Profile
14 = Help
15 = Request password
16 = System options
*/

if ($page_id == 0) {
	include ("frontpage.php");
	if (($_REQUEST['username'] != "" && $_REQUEST['password'] != "") && !auth($_REQUEST['username'], $_REQUEST['password'])) {
		$page_content = "<p>" . i18n("Access to this website is restricted. If you are allowed to view this site, please log in bellow.") . "</p>" . loginform($language, globalIDtoURL("content/$language/0") );
	} else if (isAllowed("global_frontpage_view")) {
		if (!strpos($page_content, "<") === true) { //only parse the page content if it isn't already html
			$page_content = parse_page_data($page_content);
		}
	} else {
		$page_content = "<p>" . i18n("Access to this website is restricted. If you are allowed to view this site, please log in bellow.") . "</p>" . loginform($language, globalIDtoURL("content/$language/0") );
	}
} else if ($page_id == 1) { include ("about.php");
} else if ($page_id == 2) { include ("tagwall.php");
} else if ($page_id == 3) { include ("setup.php");
} else if ($page_id == 4) { include ("usersetup.php");
} else if ($page_id == 5) { include ("pagesetup.php");
} else if ($page_id == 6) { include ("pageedit.php");
} else if ($page_id == 7) { include ("menusetup.php");
} else if ($page_id == 8) { include ("menuedit.php");
} else if ($page_id == 9) { include ("themesetup.php");
} else if ($page_id == 10) { include ("allpages.php");
} else if ($page_id == 11) { include ("filesetup.php");
} else if ($page_id == 12) { include ("modulesetup.php");
} else if ($page_id == 13) { include ("profiles.php");
} else if ($page_id == 14) {
	$_REQUEST["theme"] = "empty";
	$page_title = i18n("Page Layout Help");
	$page_content = "<html><head><title>" . i18n("Page Layout Help") . "</title><style>.page_help_term, .page_help_description { border-top: 1px dotted gray; }</style></head><body><h1>" . i18n("Page Layout Help") . "</h1>" . generate_pagehelp() . "</body></html>";
} else if ($page_id == 15) { include ("requestpassword.php");
} else if ($page_id == 16) { include ("systemoptions.php");
} else if ($page_id == -1) {
	$page_author = $owner_name;
	$page_author_email = $owner_email;
	$page_title = htmlspecialchars($_REQUEST["page"]);
	$page_menu = 0;
	
	if( !isauth() )
		$page_content = parse_page_data(i18n("The page (##0##) was not found on the system. Please return to the ((Front page)) and try again.", array($page_title)));
	else
		$page_content = parse_page_data(i18n("The page (##0##) was not found on the system. Please return to the ((Front page)) and try again.", array($page_title)) . "<hr /><a href=\"" . globalIDtoURL("setup/editpage/$language/$page_title") . "\">" . i18n("Click here to make a new page with this title") . "</a>");
	
	$page_date = formatTime( time() );
} else if (is_array($page_id)) {//this means that the search for pages returned more than one $page_id, and that it is now an array.
	$page_author = $owner_name;
	$page_author_email = $owner_email;
	$page_menu = 0;
	$page_date = formatTime( time() );
	$page_title = i18n("Found pages");
	$page_content = "<p>" . i18n("The following pages were found with a title which includes &quot;##0##&quot;. Click on any of them to see the entire contents.", array($_REQUEST['page'])) . "</p>\n<ul>";
	$page_newtitle = htmlspecialchars($_REQUEST["page"]);
	//cycle through the list of found pages and write the $page_title and a short bit of the $page_content
	foreach ($page_id as $key => $value) {
		foreach($value["title"] as $key2 => $value2) {
			$page_content .= "
			<li><div><a href=\"" . globalIDtoURL("content/{$page_id[$key]['language'][$key2]}/{$page_id[$key]['id']}") ."\">{$page_id[$key]['title'][$key2]} ({$page_id[$key]['language'][$key2]})</a></div>";
		}
	}
	$page_content .= "</ul>\n		<hr size=\"1\">";
	
	if( isauth() )
		$page_content .= "\n		<p><a href=\"" . globalIDtoURL("setup/editpage/$language/$page_newtitle") . "\">" . i18n("Click here to make a new page with this title") . "</a>.</p>";
	
} else if ($page_id > 99) { //Pages
	$page_temp = $pagectl->fetchPageData($page_id, $language, $page_folder);
	$page_author = $page_temp["author"];
	$page_author_email = $page_temp["email"];
	$page_menu = $page_temp["menu"];
	$page_date = $page_temp["date"];
	$page_title = $page_temp["title"];
	if (($_REQUEST['username'] != "" && $_REQUEST['password'] != "") && !auth($_REQUEST['username'], $_REQUEST['password'])) {
		$page_content = "<p>" . i18n("You have reached a restricted area of the website. If you are allowed to view this part of the site, please log in bellow.") . "</p>" . loginform($language, globalIDtoURL("content/$language/" . $page_temp["title"]));
	} else if (isAllowed("menu_" . $page_menu . "_view")) {
		$page_content = $page_temp["content"];
		if (!strpos($page_content, "<") === true) { //only parse the page content if it isn't already html
			$page_content = parse_page_data($page_content);
		}
	} else {
		$page_content = "<p>" . i18n("You have reached a restricted area of the website. If you are allowed to view this part of the site, please log in bellow.") . "</p>" . loginform($language, globalIDtoURL("content/$language/" . $page_temp["title"]));
	}
} else { //Something is broken, this should NEVER happen!
	$page_title = "THIS SHOULD NOT HAPPEN!!";
}

//Theming is the last thing to be done
include("theming.php");

// Script execution timer end {
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
// echo "<div style=\"text-align : center; font-size: 6px; color : #c0c0c0;\">This page was created in ".$totaltime." seconds</div>";
// Script execution timer end }
?>
