Location: PHPKode > projects > Oocommon php engine > ooim/oocommon.php
<?php
  // Webalianza Common PHP library v0.40oop
  // OOP Port by Lolaine, 2002

/* <lib> */

  function lib_info()
  {
	$release = "0.50";
	$release_date = "20021022";
	$release_subtitle = "RC1";
	$release_user = "hide@address.com";

	$coders[0] = "Luis Martin-Santos García";

	$contrib[0] = "Leandro Martin-Santos Ribera";
	$contrib[1] = "Marta Ruiz";
	$contrib[2] = "Loli Garnateo";
	$contrib[3] = "Andrés Lobero";
	$contrib[4] = "Silvia García";
	$contrib[5] = "Team redlibre.org";
	$contrib[6] = "Jorge Moreiro Delgado";
	$contrib[7] = "Aritz Torre García";
	$contrib[8] = "Francisca Garc’a Serralde";
	$classes[0] = "page";
	$classes[1] = "dbconnection";
	$classes[2] = "session";
	$classes[3] = "sched";
	$classes[4] = "requestvariables";
	//$classes[5] = "mapper";
	//$classes[6] = "xml-xslt";
	$classes[5] = "langpage";
	//$classes[8] = "css";


		echo ("<br /><br /><br /><center> <table width=\"60%\" style=\"font-family:tahoma,verdana,helvetica : font-size:11px;border:outset 1px;border-collapse:collapse\">
		<tr> <td colspan=\"2\" style=\"background:rgb(80,83,170);color:rgb(255,255,255);font-family:tahoma,verdana,helvetica ; font-size:11px\">
		<center> <b>Webalianza oocommon PHP Library </b></center>
		</td></tr><tr><td colspan=\"2\" style=\"border:outset 1px;background:rgb(200,200,200);font-family:tahoma,verdana,helvetica ; font-size:11px\">
			(c) 2002 Consorcio Webalianza, All rights reserved <br />
			With colaboration from the redlibre.org team <br />
			<br />
			This program is free software; you can redistribute it and/or modify<br />
			it under the terms of the GNU Library General Public License as published by<br />
    			the Free Software Foundation; either version 2 of the License, or<br />
    			(at your option) any later version.<br />
			<br />
			This program is distributed in the hope that it will be useful,<br />
			but WITHOUT ANY WARRANTY; without even the implied warranty of<br />
			MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the<br />
			GNU General Public License for more details.<br />
			<br />
			You should have received a copy of the GNU General Library Public License<br />
			along with this program; if not, write to the Free Software<br />
			Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA<br />



		</td></tr><tr><td colspan=\"2\"style=\"border:outset 1px;background:rgb(204, 203, 230);border-color:rgb(0,0,0); font-family:tahoma,verdana,helvetica ; font-size:11px\">
			<b>Programmers</b>
		</td></tr>
		");

		for ($i=0;$i<count($coders);$i++){echo "  <tr><td colspan=\"2\" style=\"font-family:tahoma,verdana,helvetica ; font-size:11px\">
			".$coders[$i]."
		</td></tr>";}
		echo "	<tr><td colspan=\"2\" style=\"border:outset 1px;background:rgb(204, 203, 230);border-color:rgb(0,0,0);font-family:tahoma,verdana,helvetica ; font-size:11px\">
			<b>Contributors</b>
		</td></tr>";
		for ($i=0;$i<count($contrib);$i++){echo "  <tr>	<td colspan=\"2\" style=\"font-family:tahoma,verdana,helvetica ; font-size:11px\">
			".$contrib[$i]."
		</td></tr>";}

		echo "
		<tr> <td colspan=\"2\" style=\"border:outset 1px;background:rgb(204, 203, 230);border-color:rgb(0,0,0);font-family:tahoma,verdana,helvetica ; font-size:11px\">
			<b>Release Information</b>
		</td></tr><tr><td  style=\"border:outset 1px;background:rgb(204, 203, 230);font-family:tahoma,verdana,helvetica ; font-size:11px\">
			Version
		</td><td  style=\"border:outset 1px;background:rgb(200,200,200);font-family:tahoma,verdana,helvetica ; font-size:11px\">
			".$release."
		</td></tr>
		<tr><td  style=\"border:outset 1px;background:rgb(204, 203, 230);font-family:tahoma,verdana,helvetica ; font-size:11px\">
			Date
		</td><td  style=\"border:outset 1px;background:rgb(200,200,200);font-family:tahoma,verdana,helvetica ; font-size:11px\">
			".$release_date."
		</td></tr><tr><td  style=\"border:outset 1px;background:rgb(204, 203, 230);font-family:tahoma,verdana,helvetica ; font-size:11px\">
			Release User
		</td><td  style=\"border:outset 1px;background:rgb(200,200,200);font-family:tahoma,verdana,helvetica ; font-size:11px\">
			".$release_user."
		</td></tr><tr><td  style=\"border:outset 1px;background:rgb(204, 203, 230);font-family:tahoma,verdana,helvetica ; font-size:11px\">
			Release Stability
		</td><td  style=\"border:outset 1px;background:rgb(200,200,200);font-family:tahoma,verdana,helvetica ; font-size:11px\">
			".$release_subtitle."
		</td></tr><tr><td  style=\"border:outset 1px;background:rgb(204, 203, 230);font-family:tahoma,verdana,helvetica ; font-size:11px\">
			Release String
		</td><td  style=\"border:outset 1px;background:rgb(200,200,200);font-family:tahoma,verdana,helvetica ; font-size:11px\">
			oocommon-".$release."-".$release_subtitle."-".$release_date."-".$release_user."
		</td></tr><tr><td  style=\"border:outset 1px;background:rgb(204, 203, 230);font-family:tahoma,verdana,helvetica ; font-size:11px\">
			PHP Engine Version
		</td><td  style=\"border:outset 1px;background:rgb(200,200,200);font-family:tahoma,verdana,helvetica ; font-size:11px\">
			".phpversion()."
		</td></tr><td colspan=\"2\" style=\"border:outset 1px;background:rgb(204, 203, 230);border-color:rgb(0,0,0);font-family:tahoma,verdana,helvetica ; font-size:11px\">
			<b>Classes Present</b>
		</td></tr>";
		for ($i=0;$i<count($classes);$i++){echo "<tr><td style=\"border:outset 1px;background:rgb(204, 203, 230);font-family:tahoma,verdana,helvetica ; font-size:11px\">
			Class ".$classes[$i]."
		</td>";
		if (class_exists($classes[$i]))	{echo "  	<td style=\"border:outset 1px;background:rgb(90, 192, 90);font-family:tahoma,verdana,helvetica ; font-size:11px\">
			Present
		</td></tr>";} else {echo "<td style=\"border:outset 1px;background:rgb(245, 128, 108); font-family:tahoma,verdana,helvetica ; font-size:11px\">
			Not Present
		</td></tr>";}}	echo "<tr><td style=\"background:rgb(147, 147, 195)\">
	       		<img src=\"".$PHP_SELF."?=".php_logo_guid()."\" border=\"0\" alt=\"PHP Logo\">
		</td><td style=\"background:rgb(147, 147, 195)\"><table style=\"border-collapse:collapse;width:100%\"><tr><td style=\"background:rgb(147, 147, 195);font-family:tahoma,verdana,helvetica ; font-size:11px\">
	       		<b> PHP Extensions Loaded </b><br />
		</td></tr>";
	 	foreach (get_loaded_extensions() as $k=>$v){echo "<tr><td style=\"border:outset 1px;background:rgb(200,200,200);font-family:tahoma,verdana,helvetica ; font-size:11px\">".$v."</td></tr>";
	       		}
	       		echo "
			</table>
		</td></tr>
		</table>
		</center><br /><br /><br />
	     ";
  }

 function doln ($txt,&$var)
 { $var = $var . $txt; }

 function checkclass($name)
 {if (class_exists($name)){return true;}else{return false;}}





// --------------------------------- NAMESPACE -----------------------------------------


  class RequestVariables
  {
  	function RequestVariables()  /* constructor */
	{
    		global $HTTP_POST_VARS, $HTTP_GET_VARS, $HTTP_COOKIE_VARS;
    		$params = array_merge($HTTP_POST_VARS, $HTTP_GET_VARS,$HTTP_COOKIE_VARS);
    		foreach($params as $k=>$v)
		{
      			$this->$k = $v;
    		}
  	}
  }


// -------------------------------------------- class Page -----------------------------------------------------------------------------------


 class Page
 {
 	var $body;
	var $docheader;
	var $title;
	var $bodyheader;
	var $footer;
	var $stylesheet;

	function Page()/* constructor*/
	{
		$this->docheader = "<HTML>";
	}

	function writeln ($string)
	{
		$this->body = $this->body.$string;
	}

	function clear()
	{
		$this->body = "";
	}
	function output ()
	{
		echo $this->docheader."<HEAD><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"><LINK rel=\"stylesheet\" href=\"".$this->stylesheet."\"><TITLE>".$this->title."</TITLE></HEAD><BODY>".$this->bodyheader.$this->body.$this->footer."</BODY></HTML>";
	}

	function headers_nocache ()
	{
    		header ("Cache-Control: Private");
  	}
	function addimage($url)
	{
		$this->writeln("<img src=\"".$url."\" alt=\"\" />");
	}
	function addlink ($url,$caption)
	{
		$this->writeln("<a href=\"".$url."\">".$caption."</a>");
	}
 }

class HTML4Page extends Page
{
 	function HTML4Page() /* constructor */
	{
		$this ->docheader = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"><HTML>";
	}
}

class RedirectionPage extends Page
{
	function RedirectionPage ($url) /*constructor*/
	{
    		header ("Location: $url");
    		die ("\n");
  	}
}
class XHTMLPage extends Page
{
	function XHTMLPage() /* constructor */
	{
		$this->page;
		$this->docheader = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html>";
	}
}


// ---------------------------------------- class dbconnection -------------------------------------------------------------------------------


class dbconnection
{
	var $host;
	var $username;
	var $password;
	var $database;
	var $rows;
	var $table;
	var $link;

 	function dbconnection ($host = "", $username = "", $password = "", $database = "")
	{
		$host or $host = $GLOBALS [mysql_host];
    		$username or $username = $GLOBALS [mysql_username];
    		$password or $password = $GLOBALS [mysql_password];
    		$database or $database = $GLOBALS [mysql_database];
		$this->link = mysql_connect ($host, $username, $password) ;
		mysql_select_db ($database,$this->link);
		echo mysql_error();
	}

	function opentable($ttable)
	{
		$this->query ("select * from ".$ttable,$ttable);
	}

	function query ($sql,$table="")
	{
		$this->table = $table;
		$result = mysql_query($sql,$this->link);
		echo mysql_error();
		$n = 0;
		while($temprow = mysql_fetch_assoc($result))
		{
			echo mysql_error();
			foreach($temprow as $k=>$v)
			{
			$this->rows[$n][$k] = $v;
    			}
			$n++;
		}
	}

	function update () // Necesita que haya row[ìd] y que $this->table este puesto , tambien row[id] NO puede haber sido modificado
	{
		      if (($this->table == "") || ($this->row[id] == "")) { return 0; }
                      foreach ($this->rows as $a)
			{
				$strs = "";
				$n++;
				foreach ($a as $k=>$v)
					{
					$strs = $strs. $k ." = \"".$v."\",";
					}
			$strs = substr($strs,0,-1);
			if ($a[id] != "")
				{
				$que[$n] = "UPDATE ".$this->table." SET ".$strs." WHERE id=".$a[id];
				$this->query($que[$n],$this->table);
				}
			}
	}
	function clean()
	{
	$this->rows="";
	}

	function doRowFromObject ($row,$object)
	{
		foreach ($object as $k=>$v)
		{
		$this->rows[$row][$k] = $v;
		}
	}

	function insert() // Necesita que esten las rows metidas y $this->table especificado;
	{
		if ($this->table == "") { return 0; }
 		foreach ($this->rows as $a)
		{
			$n++;
			foreach ($a as $k=>$v)
				{
				$strs = $strs . "\"".$v."\",";
				$strd = $strd . $k.",";
				}

			$strs = substr($strs,0,-1);
			$strd = substr($strd,0,-1);
			$que[$n] = "INSERT INTO ".$this->table." (".$strd.") VALUES ( ".$strs." )";
			echo $que[$n];
			$this->query($que[$n],$this->table);


		}
	}

	function delete($rownumber=0)
	{

		$this->query("delete from ".$this->table." where id=".$this->rows[$rownumber]["id"],$this->table);

	}


	function show_row($rownumber=0,$fila_nombre="",$fila_valor="",$baseurl="",$exclude="")
	{
			if ($baseurl=="") { $baseurl=$_SERVER[PHP_SELF]; }
			doln ("<table border=1>",$list);
			foreach ($this->rows[$rownumber] as $k=>$v)
				{
				$excludethis=0;
					for ($i=0; $i<count($exclude); $i++)
					{

						if ($k == $exclude[$i]) { $excludethis = 1; }
					}
    				if ($k!=$fila_valor)
					{
						if ( $excludethis == 0 )
						{
							if ($k == $fila_nombre )
							{
							doln("<tr><td><b>".$k."</b></td><td><a href=\"".$baseurl."?".$fila_valor."=".$a[$fila_valor]."\">".$v."</a></td></tr>",$list);
							} else {
							doln("<tr><td><b>".$k."</td><td>".$v."</td></tr>",$list);
							}
						}
					}
				}
		doln ("</tr></table>",$list);
		return $list;
	}

	function show_form($rownumber=0,$baseurl="",$submitter,$exclude="")
	{
			if ($baseurl=="") { $baseurl=$_SERVER[PHP_SELF]; }

			doln ("<form method=\"post\" action=\"".$baseurl."\"><table>",$list);
				foreach ($this->rows[$rownumber] as $k=>$v)
				{
				$excludethis=0;
				for ($i=0; $i<count($exclude); $i++)
					{
						if ($k == $exclude[$i])
						{
						$excludethis=1;
						}
				}
				if ($excludethis != 1)
					{
					doln("<tr><td><b>".$k."</b></td><td><input type=\"text\" name=\"".$k."\" value=\"".$v."\"></td></tr>",$list);
					}else {
					doln("<input type=\"hidden\" name=\"".$k."\" value=\"".$v."\">",$list);
					}
				}
			doln ("</tr><tr><td></td><td><input type=\"submit\" value=\"".$submitter."\"> </td></table></form>",$list);
			return $list;
	}

	function show($fila_nombre="",$fila_valor="",$baseurl="",$exclude="")
	{
			if ($baseurl=="") { $baseurl=$_SERVER[PHP_SELF]; }
			doln ("<table border=1>
				 <tr>
				",$list);
			foreach ($this->rows as $a)
			{
				foreach ($a as $k=>$v)
				{
					$excludethis=0;
					for ($i=0; $i<count($exclude); $i++)
					{
						if ($k == $exclude[$i]) { $excludethis = 1; }
					}
					if ($k != $fila_valor)
					{
					if ($cols[$k] != 1 && $excludethis== 0)
						{
						doln("<td><b>".$k."</b></td>",$list);
						$cols[$k] = 1;
						}
					}
				}
			}
			doln ("</tr>",$list);
			foreach ($this->rows as $a)
			{
			doln("<tr>",$list);
			foreach ($a as $k=>$v)
				{
				$excludethis=0;
				for ($i=0; $i<count($exclude); $i++)
					{
						if ($k == $exclude[$i]) { $excludethis = 1; }
					}
    				if ($k!=$fila_valor)
					{
						if ( $excludethis == 0 )
						{
						if ($k == $fila_nombre)
						{
						doln("<td><a href=\"".$baseurl."?".$fila_valor."=".$a[$fila_valor]."\">".$v."</a></td>",$list);
						} else {
						doln("<td>".$v."</td>",$list);
						}
						}
					}
				}
			}
		doln ("</tr></table>",$list);
		return $list;
	}
}


// -------------------------------------------------------------------------------------------------------------------------------------------------


  class sched
  {
	var $now;

	function sched()
	{
		$this->now = time();
	}

	function Diff()
	{
		$then = $this->now();
		$dif = $then - time();
		return $dif;
	}

	function getDate()
  	{
    	$weekdays = array ("Domingo", "Lunes", "Martes", "Mi&eacute;rcoles", "Jueves", "Viernes", "S&aacute;bado");
    	$months = array ("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
    	list ($weekday, $day, $month, $year, $hour, $minutes) = explode (" ", date ("w j n Y G i", $this->now));
    	return ($weekdays [$weekday] . ", " . $day . " de " . $months [$month - 1] . " del " . $year . " a las " . $hour . ":" . $minutes);
  	}

	function getsDate()
  	{
    	$weekdays = array ("Dom.", "Lun.", "Mar.", "Mie.", "Jue.", "Vie.", "Sab");
    	$months = array ("Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Sep", "Oct", "Nov", "Dic");
    	list ($weekday, $day, $month, $year, $hour, $minutes) = explode (" ", date ("w j n Y G i", $this->now));
    	return ($weekdays [$weekday] . " " . $day . "/" . $months [$month - 1] . "/" . $year . "@" . $hour . ":" . $minutes);
  	}


	function getTime ()
 	{
    	list ($weekday, $day, $month, $year, $hour, $minutes) = explode (" ", date ("w j n Y G i", $this->now));
    	return ($hour . ":" . $minutes);
  	}
  }


  /* mantenemos compatibilidad hacia atras */
  function mysql_connect_db ($host = "", $username = "", $password = "", $database = "")
  {
    $host or $host = $GLOBALS [mysql_host];
    $username or $username = $GLOBALS [mysql_username];
    $password or $password = $GLOBALS [mysql_password];
    $database or $database = $GLOBALS [mysql_database];
    return (@mysql_connect ($host, $username, $password) && mysql_select_db ($database));
  }


// ---------------------------------------- class session -----------------------------------------------------------------


  class session
  {
	var $user;
	/*
	var $user[username];
	var $user[name];
	var $user[params];
	var $user[level];
	var $user[group];
	*/
	var $sess;
	/*
	var $sess[ip];
	var $sess[lastvisit];
	var $sess[pages];
	var $sess[sid];
	var $sess[params];
	*/
  	function session()
	{
	$vars = new requestvariables();
	if ($vars->sid == "") { return 0;}
	$content_array = @file("../sess/".$vars->sid);
	$sersess = @implode("", $content_array);
	if ($sersess == ""){return 0;}
	$this = unserialize ($sersess);

	$content_array = file("../users/".$this->user[username]);
	$seruser = implode(",", $content_array);
	$this->user = unserialize ($seruser);
	$this->user[logged]=true;
		if ($_SERVER [REMOTE_ADDR] != $this->sess[ip]) {return 0;}
		if ((time () - $this->sess[lastvisit]) >= 3600) {return 0;}

	$this->sess [lastvisit] = time ();
	$this->sess [pages] ++;
	
	/*actualiza estado sesión */
	unlink ("../sess/".$vars->sid);
	$sersess = serialize ($this);
	$tmpfile = fopen("../sess/".$vars->sid,"w");
	$fp = fwrite($tmpfile,$sersess);
	fclose($tmpfile);

	/* actualiza estado usuario */
	unlink ("../users/".$this->user[username]);
	$seruser = serialize ($this->user);
	$tmpfile = fopen("../users/".$this->user[username],"w");
	$fp = fwrite($tmpfile,$seruser);
	fclose($tmpfile);

	}

	function SessUpdate()
	{
	unlink ("../sess/".$this->sess[sid]);
	$sersess = serialize ($this);
	$tmpfile = fopen("../sess/".$this->sess[sid],"w");
	$fp = fwrite($tmpfile,$sersess);
	fclose($tmpfile);
	
	}

	function UserUpdate()
	{
	
	unlink ("../users/".$this->user[username]);
	$seruser = serialize ($this->user);
	$tmpfile = fopen("../users/".$this->user[username],"w");
	$fp = fwrite($tmpfile,$seruser);
	fclose($tmpfile);
	}

	function GetSessParam($name)
	{return $this->sess[params][$name];}

	function SetSessParam($name,$value)
	{$this->sess[params][$name] = $value;$this->SessUpdate();}

	function GetUserParam($name)
	{return $this->user[params][$name];}

	function SetUserParam($name,$value)
	{$this->user[params][$name] = $value;$this->UserUpdate();}


	function IsOnline($username)
	{

	$content_array = file("../users/".$username);
	$seruser = implode("", $content_array);
	/*$seruser = implode("", $seruser);*/
	$tuser = unserialize ($seruser);
		if ($tuser[logged] == true ) { return true;} else { return false;}
	}



	/* ************************** Buddy List ************************* */


	function AddBuddy($username)
	{
	if ($this->IsUser($username)){
		$buddylist = $this->GetUserParam("buddies");
		if ($buddylist == "") { $buddylist = $username; } else {	$buddylist = $buddylist.",".$username; }
		$this->SetUserParam ("buddies",$buddylist);
		return true;
		} else {
		return false;
		}
	}

	function DelBuddy($username)
	{
	if ($this->IsUser($username)){
		$buddya = $this->GetUserParam("buddies");
		$buddylist = explode(",",$buddya);
		for ($i=0;$i<count($buddylist);$i++)
		{
			if ($buddylist[$i] != $username) {
			$nbulist[$j] = $buddylist[$i] ;
			$j++; }
		}
			$bulist = implode(",",$nbulist);
			$this->SetUserParam ("buddies",$bulist);
			return true;
		} else {
		return false;
		}
	}


	function GetBuddies()
	{
	$buddylist = $this->GetUserParam ("buddies");
	$buddyarray = explode(",",$buddylist);
	return $buddyarray;
	}

	function GetOnlineBuddies()
	{
	$buddyarray = $this->getbuddies();
	for ($i = 0 ; $i<count($buddyarray) ; $i++)
		{
		if (IsOnline($buddyarray[$i]))
			{
			$obuddies[$j] = $buddyarray[$i];
			$j++;
			}
		}
	}



	/* ****************** Normal Messanging ************************* */

	function SendMessage($username,$message)
	{
	if ($this->IsUSer($username))
		{
		$content_array = @file("../msg/".$username);
		$sermsgs = implode("", $content_array);
		$msglist = unserialize ($sermsgs);

		$nmsglist[0][id] = md5(uniqid(rand()));
		$nmsglist[0][from] = $this->user[username];
		$nmsglist[0][when] = time();
		$nmsglist[0][body] = nl2br($message);

		if ($msglist[0][id] == "")
		{
		$msglist = $nmsglist;
		} else {
		$msglist = array_merge ($msglist,$nmsglist);
		}
		$sermsg = serialize ($msglist);
		@unlink ("../msg/".$username);
		$tmpfile = fopen("../msg/".$username,"w");
		$fp = fwrite($tmpfile,$sermsg);
		fclose($tmpfile);
		return true;
		}
		return false;
	}

	function GetMessageList()
	{
	$content_array = file("../msg/".$this->user[username]);
	$sermsgs = implode("", $content_array);
	$msglist = unserialize ($sermsgs);
	return $msglist;
	}

	function GetMessage($id)
	{
	$content_array = @file("../msg/".$this->user[username]);
	$sermsgs = implode("", $content_array);
	$msglist = unserialize ($sermsgs);
	return $msglist[$id];
	}

	function DeleteMessage($id)
	{
	$content_array = @file("../msg/".$this->user[username]);
	$sermsgs = implode("", $content_array);
	$msglist = unserialize ($sermsgs);
	$j=0;
	for ($i = 0 ; $i<count($msglist) ; $i++)
		{
		if ($msglist[$i][id] != $id)
			{
			$nmsglist[$j][id] = md5(uniqid(rand()));
			$nmsglist[$j][from] = $msglist[$i][from];
			$nmsglist[$j][when] = $msglist[$i][when];
			$nmsglist[$j][body] = $msglist[$i][body];
			$j++;
			}
		}
		$sermsg = serialize($nmsglist);
		@unlink ("../msg/".$this->user[username]);
		$tmpfile = fopen("../msg/".$this->user[username],"w");
		$fp = fwrite($tmpfile,$sermsg);
		fclose($tmpfile);
	}

	/* ************** Instant Messanging ******************* */

	function SendIM($id,$message)
	{
		if ($this->CheckIM($id))
		{
			$content_array = @file("../im/".$id);
			$sermsgs = implode("", $content_array);
			$msglist = unserialize ($sermsgs);
			$nmsglist[0][from] = $this->user[username];
			$nmsglist[0][body] = nl2br($message);
			$nmsglist[0][when] = time();
			if ($msglist[messages][0][from] == "")
				{
					$msglist[messages] = $nmsglist;
				} else {
					$msglist[messages] = array_merge ($msglist[messages],$nmsglist);
				}
			$sermsg = serialize ($msglist);
			@unlink ("../im/".$id);
			$tmpfile = fopen("../im/".$id,"w");
			$fp = fwrite($tmpfile,$sermsg);
			fclose($tmpfile);
			return true;
			}
		return false;
	}

	function CheckIM($id)
	{
		$result = @fopen("../im/".$id,"r");if (!$result){return false;} fclose($result);
		$content_array = @file("../im/".$id);
		$serim = implode("", $content_array);
		$im = unserialize ($serim);
		foreach ($im[parts] as $k=>$v)
			{
			if ($this->user[username] == $v)
				{
				$exists = true;
				}
			}
		if ($exists == false ) { return false; }
	return $im[messages];
	}

	function InviteIM($id,$username)
	{
	$content_array = @file("../im/".$id);
	$serim = implode("", $content_array);
	$im = unserialize ($serim);
	$im[parts][count($im[parts])] = $username;
	$serim = serialize ($im);
	unlink("../im/".$id);
	$tmpfile = fopen("../im/".$id,"w");
	$fp = fwrite($tmpfile,$serim);
	fclose($tmpfile);
	$this->AddImUser($id,$username);
	}

	function GetIMUsers($id)
	{
		$result = @fopen("../im/".$id,"r");if (!$result){return false;} fclose($result);
		$content_array = @file("../im/".$id);
		$serim = implode("", $content_array);
		$im = unserialize ($serim);
		foreach ($im[parts] as $k=>$v)
			{
				if ($v != $this->user[username]) { $users = $users.",".$v; } 

			}
		return $users;
	}

	function AddImUser($id,$username)
	{

	$content_array = file("../users/".$username);
	$seruser = implode(",", $content_array);
	$auser = unserialize ($seruser);
	if ($auser[params][IM]==""){
		$auser[params][IM]=$id;
		}else{
		$auser[params][IM]=$auser[params][IM].",".$id;
		}
	$ouser = serialize ($auser);
	unlink ("../users/".$username);
	$tmpfile = fopen("../users/".$username,"w");
	$fp = fwrite($tmpfile,$ouser);
	fclose($tmpfile);
	}

	function OpenIM($username,$invitation="")
	{
		if ($invitation="") { $username." and ".$this->user[username]. " joined IM";}
		$im[id] = md5(uniqid(rand()));
		$this->SetUserParam("IM",($this->GetUserParam("IM").",".$im[id]));
		$im[parts][0] = $this->user[username];
		$im[parts][1] = $username;
		$this->addImUser($im[id],$username);
		$im[messages][0][body] = $invitation;
		$im[messages][0][from] = "System";
		$im[messages][0][when] = time();
		$serim = serialize ($im);
		$tmpfile = fopen("../im/".$im[id],"w");
		$fp = fwrite($tmpfile,$serim);
		fclose($tmpfile);
		return $im[id];
	}

	function CloseIM($id)
	{
	@unlink("../im/".$id);
	}

	/* ****************** Generic Session Functions ****************** */

	function admin_session ()
  	{if ($this->user [level] >= 120){ return true;} else { return false; }}

  	function session_error ($error)
  	{die ();}

	function UserAdd ()
	{
	$seruser = serialize ($this->user);
	$tmpfile = fopen("../users/".$this->user[username],"w");
	$fp = fwrite($tmpfile,$seruser);
	fclose($tmpfile);
	$tmpfile = fopen("../msg/".$this->user[username],"w");
	$fp = fwrite($tmpfile,"");
	fclose($tmpfile);
	}

	function IsUser($username)
	{
		$result = @fopen("../users/".$username,"r");
		if ($result)
		{
		return true;
		}
		else
		{
		return false;
		}
		fclose($result);
	}

	function UserDel()
	{@unlink ("../users/".$this->user[username]);}

	function UsersList()
	{
	chdir ("../users/");
	if (!$startdir)
		{
		$startdir = $currentdir;
		}
		$d = opendir (".");
		while ($file = readdir ($d))
			{
			if ($file != ".." && $file != ".")
			{
				if (is_dir ($file))
				{
				$files = filelist (getcwd().'/'.$file, getcwd(), $files);
				} else {
				$files[] = $file;
				}
			}
		}
	return $files;
	}

  	function dologin($username,$password)
  	{
	if ($this->sess[sid] !=""){return 0;}
	if (!$this->IsUser($username)){return 0;}
	$content_array = file("../users/".$username);
	$seruser = implode("", $content_array);
	/*$seruser = implode("", $seruser);*/
	$this->user = unserialize ($seruser);
	if ($seruser == ""){return false;}
	//if (crypt ($request [password], substr ($user [password], 0, 2)) != $user [password]) portal_error ("login");
  	if ($this->user[password] != $password)
	  	{
		$this->user = NULL;
		return false;
		}
  	if ($user [level] < 0)
  		{
		$this->user = NULL;
		return false;
		}
  	$this->sess [sid] = md5 (uniqid (rand ()));
	$this->sess [lastvisit] = time ();
	$this->user[params][IM]="";
	$this->sess [ip] = $_SERVER[REMOTE_ADDR];
	$this->user [logged] = true; /* se escribirá en la siguiente comprabacion de sesion */
	setcookie ("sid", $this->sess[sid], 0, "/");
	@unlink ("../sess/".$vars->sid);
	$sersess = serialize ($this);
	$tmpfile = fopen("../sess/".$this->sess[sid],"w");
	$fp = fwrite($tmpfile,$sersess);
	fclose($tmpfile);
	//redirect (($user [level] > 120 ? "/admin/" : "/main.php"));
	return true;
	}

  	function dologout()
  	{
  	$this->session();
	$this->user[logged] = false;
  	$vars = new requestvariables ();
	@unlink ("../sess/".$vars->sid);
	setcookie ("sid", "", 0, "/");
	$ims = @explode (",",$this->GetUserParam("IM"));
	$ima = $this->GetUserParam("IM"); /* Maybe solo uno ... quien sabe...*/
	@unlink ("../im/".$ima);
		for ($i=0;$i<count($ims);$i++)
		{
		@unlink ("../im/".$ims[$i]);
		}
	$this->user[params][IM]="";
	$seruser = serialize ($this->user);
	$tmpfile = fopen("../users/".$this->user[username],"w");
	$fp = fwrite($tmpfile,$seruser);
	fclose($tmpfile);
	$this->sess = "";
	$this->user = "";
	}

	function doLoginBox($target,$title="Login",$buttonname="Login")
	{
	doln("<form method=\"post\" action=\"".$target."\"><table><tr><td colspan=\"2\"><center>".$title."</center></td></tr><tr><td>Username</td><td><input type=\"text\" name=\"username\"></td></tr><tr><td>Password</td><td><input type=\"password\" name=\"password\"></td></tr><tr><td colspan=\"2\"><center><input type=\"submit\" value=\"".$buttonname."\"></center></td></tr></table></form>",$list);
	return $list;
	}

	function domsgbox($target,$username="")
	{
	doln("<form method=\"post\" action=\"".$target."\"><table><tr><td>To:</td><td><input type=\"text\" name=\"username\" value=\"".$username."\"></td></tr><tr><td>Message:</td><td><textarea name=\"body\" cols=\"20\" rows=\"10\"></textarea></td></tr><tr><td colspan=\"2\"><center><input type=\"submit\" value=\"Send\"></center></td></tr></table></form>",$list);
	return $list;
	}
}

	class langpage extends html4page
	{
		var $langname;
		var $strings;

		function langpage ($langname)
		{
			$this->langname = $langname;
			include ("../lang/".$langname.".php");
			$this->strings = getlang();
		}

		function lwriteln ($string)
		{
				foreach ($this->strings as $k=>$v)
				{
					if ($k == $string) { $string = $v;}
				}
				doln($string." ",$this->body);
				$pos = $newpos;
		}
	}

/*---------------------------------------------------------------------------------------*/

/*
	class mapper
	{

		var $mapimages;
		var $objects;

		/*
		var $mapimages[num];
		Donde num será y * resx + x
		var $objects[n][x]
		var $objects[n][y]
		var $objects[n][map]
		var $objects[n][name]
		var $objects[n][icon]



		*/

		/*
		function mapper ($session)
		{

		}

	}


*/





/*---------------------------------------------------------------------------------------*/

function currency ($quantity, $symbol = " &euro;")
  {
    $integer = floor ($quantity);
    $decimal = substr ($quantity - $integer, 2, 2);
    if (strlen ($decimal) < 2) {
      $decimal = $decimal . str_repeat (0, (2 - strlen ($decimal)));
    }
    $digits = strlen ($integer);
    $odd_digits = (strlen ($integer) % 3);
    $integer_2 = substr ($integer, 0, $odd_digits) . ",";
    for ($i = $odd_digits; $i < $digits; $i = $i + 3) {
      $integer_2 .= substr ($integer, $i, 3) . ",";
    }
    $integer_2 = substr ($integer_2, 0, strlen ($integer_2) - 1);
    return ($integer_2 . "." . $decimal . $symbol);
  }


/* </lib> */
?>
Return current item: Oocommon php engine