<?php

	/*
	*	Gekko - Open Source Web Development Framework
	*	------------------------------------------------------------------------
	*	Copyright (C) 2004-2006, J. Carlos Nieto <xiam@users.sourceforge.net>
	*	This program is Free Software.
	*
	*	@package	Gekko
	*	@license	http://www.gnu.org/copyleft/gpl.html GNU/GPL License 2.0
	*	@author		J. Carlos Nieto <xiam@users.sourceforge.net>
	*	@link		http://www.gekkoware.org
	*/

	if (!defined("IN-GEKKO")) die("Get a life!");

	define("IN-BLOCK", 1);

	// loading API
	LoadLib (
		"blocks"
	);

	// creating block buffers
	$bpos = explode(",", conf::getkey("blocks", "block_positions"));
	foreach ($bpos as $pos)
		if (($pos = trim($pos)) != null)
			$L["M_BLOCK_".$pos] = "";

	if (!isset($_GET["hide_blocks"])) {

		// loading blocks for this module
		$q = $db->query ("
		SELECT
			id,icon, title, content, modules, position, scriptpath, scriptvars, csstext,
			blockclass, auth_access, author_id
		FROM ".db_table("block")."
		WHERE (status = '1' AND (modules LIKE '%".MAIN_MODULE."%' OR modules LIKE '%*%'))
		ORDER BY order_id ASC
		", __FILE__, __LINE__, true);

		// filling block buffers
		while ($row = $db->fetchRow($q)) {

			// checking permissions
			if (Blocks::checkAuth(MAIN_MODULE, $row["modules"], $row["auth_access"])) {

				// we don't want those variables to be escaped
				saneHTML($row, "title,content,scriptvars,scriptpath");

				// privilege based html filtering
				//format::style($row["title"], $row["author_id"]);
				format::style($row["content"], $row["author_id"]);

				// default block template (must be under templates/[$template]/blocks/*.block)
				$row["blockskeleton"] = "default.block";

				// block icon
				$row["icon"] = mkIcon($row["icon"]);

				// css style in a single line
				$row["csstext"] = preg_replace("/\n|\t/", "", $row["csstext"]);

				if (Blocks::scriptExists($row["scriptpath"])) {
					$blockinfo = explode("/", $row["scriptpath"]);
					if (gekkoModule::isInstalled($blockinfo[0])) {
						$content = Blocks::LoadScript($row);
						if ($content == -1)
							continue;
						else
							$row["content"] .= $content;
					}
				} elseif ($row["scriptpath"]) {
					$row["content"] .= mkMessageBox("error", "<b>Missing block:</b> \"{$row["scriptpath"]}\".");
				}

				// block template
				$btpl = new ContentBlock("block", "blocks/".$row["blockskeleton"]);

				$btpl->vren("BLOCK_TITLE", "TITLE");
				$btpl->vren("BLOCK_CONTENT", "CONTENT");
				$btpl->vren("BLOCK_ICON", "ICON");

				$btpl->setArray($row);

				$L["M_BLOCK_".$row["position"]] .= Lang::Parse($btpl->make());
			}
		}
	}
?>