<?PHP
/*
* subscribe.php Actions wrapper
* copyright (c)2005 Marco Islas &lt;markuz@islascruz.org&gt;
*/
/*
 *  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 subscribe
{
	function subscribe(){
	}
	
	function default_action(){
		$fields = array('0'=>'owner',
				'1'=>'email',
				'2'=>'email2',
				'3'=>'title',
				'4'=>'url',
				'5'=>'short_name',
				'6'=>'description');
		$fields1 = array('0'=>'Nombre del Responsable',
				'1'=>'Email',
				'2'=>'Confirmacion de Email',
				'3'=>'Nombre del sitio',
				'4'=>'Direccion del sitio Web',
				'5'=>'Nombre corto del sitio',
				'6'=>'Descripcion');
		$a = array();
		$b = 0;
		$allowed_tags = "";
		$text = strip_tags($text, $allowed_tags);
		foreach($fields as $field){
			//echo $b."=>".$field."<br>"; for debug purposes
			$a[$field] = strip_tags($_REQUEST[$field],$allowed_tags);
			if (empty($a[$field])){
				return $GLOBALS["functions"]->error("Falta el campo <br /><b>".$fields1[$b]."</b>");
				break;
			}
			$b++;
		}
		if ($b == 7){
			return $this->insert($a);
			echo "Intentando insertar los datos";
		}
		else
			return "Completo fracaso";
		
	}
	
	function insert($a){
		$sql = "select url from sites where url='".$a["url"]."'";
		$b = $GLOBALS["db"]->fetch_array($sql);
		$date = date(mdy);
		$ndate= substr($date,0,2)."-".substr($date,2,2)."-".substr($date,4,6);
		if($a["url"]==$b["url"])
			return $GLOBALS["functions"]->error("Ya se ha registrado un sitio con Nombre y/o URL similar");
		else
			if ($a["email"] != $a["email2"])
				return $GLOBALS["functions"]->error("Los emails no coinciden <br />".$a["email"]." != ".$a["email2"]);
			else
				if(!strstr($a["email"],"@") || !strstr($a["email"],"."))
					return $GLOBALS["functions"]->error("No es una direccion de correo valida"," ".$a["email"]," ");
				else{
					$sql = "insert into sites values ('','".$a["owner"]."',";
					$sql.= "'".$a["email"]."','".$a["title"]."','".$a[short_name]."',";
					$sql.= "'".$a["url"]."','".$a["description"]."','false',";
					$sql.= "'0','".$ndate."')";
					if ($GLOBALS["db"]->query($sql))
						return $this->done($a);
					else {
						echo $sql."<br>";
						die(mysql_error());
					}
				}
	}
	
	function done($a){
		$tpl = new template(CINTILLA_PATH."xhtml/");
		$tpl->load("layout","subscribe.html");
		$tpl->setBlock("done","layout");
		$tpl->set_variable("owner",$a["owner"]);
		$tpl->set_variable("title",$a["title"]);
		$tpl->set_variable("email",$a["email"]);
		$tpl->expand("expdone","layout");
		return $tpl->export("expdone");
	}
}







	
?>