<?PHP
/*
* functions.php functions for general use
* copyight (c)2005 Marco Islas <maruz@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.
 */
 
class functions
{
	function init($host,$username,$password,$database){
		$GLOBALS["db"]->connect($host,$username,$password);
		$GLOBALS["db"]->query("use ".$database);
	}
	
	function error($message,$message1="",$message2=""){
		$tpl = new template(CINTILLA_PATH."xhtml/");
		$tpl->load("layout","common.html");
		$tpl->setBlock("error","layout");
		$tpl->set_variable("message",$message.$message1." ".$message2);
		$tpl->expand("experror","error");
		return $tpl->export("experror");
	}

	function fatal_error($message,$message1="",$message2=""){
		$error = "<font color=\"red\"><b>".$message;
		$error.= "</b>,".$message1;
		$error.= ",".$message2."</font>";
		die($error);
	}
	
	function get_registry_value($path){
		$sql 	= "select value from registry where name='".$path."'";
		$value 	= $GLOBALS["db"]->fetch_array($sql);
		return 	$value[0];
	}
	
	function get_themes(){
		$dir = opendir(CINTILLA_PATH."themes/");
		$themes = array();
		while($readdir = readdir($dir)){
			if (is_dir(CINTILLA_PATH."themes/".$readdir) && $readdir !="." && $readdir!="..")
				$themes[]=$readdir;
		}
		sort($themes);
		if(isset($_REQUEST["theme"]))
			$option.="<option selected value=\"".$_REQUEST["theme"]."\">".$_REQUEST["theme"]."</option>";
		foreach($themes as $theme){
			$options.="<option value=\"$theme\">$theme</option>";
		}
		return $options;
	}
	
	function unaproved_sites(){
		$sql = "select id from sites where autorized != 'true'";
		$ids = $GLOBALS["db"]->fetch_row($sql);
		$site = "";
		if (!empty($ids))
			foreach($ids as $id){
				$sql  = "select title,short_name,url from sites where ";
				$sql .= "id='".$id."'";
				$data = $GLOBALS["db"]->fetch_array($sql);
				$site.= "<a href=\"".$data["url"]."\" ";
					$site.= "title=\"".$data["title"]."\">";
				$site.= $data["short_name"]."</a><br />";
			}
		return $site;
	}
	function aproved_sites(){
		$sql = "select id from sites where autorized = 'true'";
		$id = $GLOBALS["db"]->fetch_row($sql);
		if (!empty($id))
			$id = array_reverse($id);
		$site = "";
		if (!empty($id))
			for($i=0;$i<5;$i++){
				$sql  = "select title,short_name,url from sites where ";
				$sql .= "id='".$id[$i]."'";
				$data = $GLOBALS["db"]->fetch_array($sql);
				$site.= "<a href=\"".$data["url"]."\" ";
					$site.= "title=\"".$data["title"]."\">";
				$site.= $data["short_name"]."</a><br />";
			}
		return $site;
	}
} 
 
?>