<?PHP
/*
* cintilla.php cintilla remote Script.
* copyright (c)2005 Marco Islas <markuz@islascruz.org>
*/
/*
 *  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.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

require_once ("./include/config.php");

$default_theme = "default";
$default_limit = $GLOBALS["functions"]->get_registry_value("/cintilla/limit");

if (isset($_REQUEST["theme"]) && !empty($_REQUEST["theme"])){
	if ($_REQUEST["theme"]=="random"){
		$dir = opendir(CINTILLA_PATH."themes/");
		$array_themes = array();
		while ($readdir = readdir($dir)){
			if(file_exists(CINTILLA_PATH."themes/".$readdir."/cintilla.html") && !strstr($readdir,".")){
				$array_themes[] = $readdir;
			}
		}
		$count = count($array_themes);
		$rand  = rand(0,$count-1);
		$theme = $array_themes[$rand];
	}else{
		if(file_exists(CINTILLA_PATH."themes/".$_REQUEST["theme"]."/cintilla.html"))
			$theme = $_REQUEST["theme"];
		else
			$theme = $default_theme;
	}
}
else
	$theme = $default_theme;
	
if (isset($_REQUEST["limit"])) 
	$limit = $_REQUEST["limit"];
else
        $limit = $default_limit;
        
$url = $GLOBALS["functions"]->get_registry_value("/pagval/url");
if (substr(strrev($url),0,1)!="/")
	$url.="/";
$tpl = new template(CINTILLA_PATH."themes/".$theme);
$tpl->load("layout","cintilla.html");
$tpl->setBlock("cintilla","layout");
$tpl->set_variable("theme",$theme);
$tpl->set_variable("url",$url);
$sql = "select id from sites where autorized='true' order by rand() limit 0,".$limit;
$ids = $GLOBALS["db"]->fetch_row($sql);
if (!empty($ids))
foreach($ids as $id){
	$sql  = "select id,title,short_name from sites where id='".$id."'";
	$data = $GLOBALS["db"]->fetch_array($sql);
	$site_url = $url."index.php?action=redirect&id=".$data["id"];
	$tpl->setBlock("members","layout");
	$tpl->clean("title");
	$tpl->clean("name");
	$tpl->clean("site_url");
	$tpl->set_variable("title",$data["title"]);
	$tpl->set_variable("name",$data["short_name"]);
	$tpl->set_variable("site_url",$site_url);
	$tpl->expand("expmembers","+members");
}
$tpl->expand("expcintilla","cintilla");
$cin = $tpl->export("expcintilla");
$cin = str_replace ("'","\\'",$cin);
$cin = str_replace ("\n","'); \n document.write('",$cin);
header ("Content-type: text/plain");
echo "document.write ('". $cin ."');";

?>