<?php
/************************************************************************/
/* PHP-NUKE: Web Portal System                                          */
/* ===========================                                          */
/*                                                                      */
/* Copyright (c) 2002 by Francisco Burzi                                */
/* http://phpnuke.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.       */
/*                                                                      */
/************************************************************************/
/* Additional security checking code 2003 by chatserv                   */
/* http://www.nukefixes.com -- http://www.nukeresources.com             */
/************************************************************************/
defined('MAXIMUS') or die ('Access denied epetition MAXIMUS directly...');
function NSNePetitionsShowSigners($gid, $modifyingAction, $filter, $bhideemail)
{
	global $db, $prefix, $bgcolor1, $bgcolor2;
	$request="SELECT ePetitionSignerId, timesigned, userName, city, eMailAddress,comment  FROM ".$prefix."_nsngrept_signers WHERE (ePetitionId='$gid'";
	
	if($filter=="") $request=$request.")";
	else $request=$request." ".$filter.")";

	$request=$request." ORDER BY timesigned DESC";
	$result = $db->sql_query($request);

	if(isset($result))
	{
		echo "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">";
			echo "<tr>\n";
				echo "<td align=\"left\" bgcolor=\"$bgcolor2\" ><b>"._NEP_SIGNERDATE."</b></td>\n";
				echo "<td align=\"center\" bgcolor=\"$bgcolor2\" ><b>"._NEP_SIGNERNAME."</b></td>\n";
				echo "<td align=\"center\" bgcolor=\"$bgcolor2\" ><b>"._NEP_SIGNERCITY."</b></td>\n";
				if($bhideemail==1) echo "<td align=\"center\" bgcolor=\"$bgcolor2\" ><b>"._NEP_SIGNEREMAIL."</b></td>\n";
				echo "<td align=\"right\" bgcolor=\"$bgcolor2\" ><b>"._NEP_SIGNERCOMMENT."</b></td>\n";
				if($modifyingAction!="") echo "<td align=\"center\" bgcolor=\"$bgcolor2\"><b>"._NEP_ACTIONSIGNERS."</b></td>\n";
			echo "</tr>";
			while(list($ePetitionSignerId, $timesigned, $userName,$city,$eMailAddress,$comment) = $db->sql_fetchrow($result)) {
				echo "<tr>";
				echo "<td align=\"left\"  bgcolor=\"$bgcolor1\" nowrap >$timesigned</td>\n";
				echo "<td align=\"center\"  bgcolor=\"$bgcolor1\" >$userName</td>\n";
				echo "<td align=\"center\"  bgcolor=\"$bgcolor1\" >$city</td>\n";
				if($bhideemail==1) echo "<td align=\"center\"  bgcolor=\"$bgcolor1\" ><p>$eMailAddress</p></td>\n";
				if($comment=="") $comment="&nbsp";
				echo "<td align=\"right\"  bgcolor=\"$bgcolor1\" >$comment</td>\n";
				if($modifyingAction!="")
				{
					global $admin_file;
					$current_admin_file=$admin_file;
					if($current_admin_file==null || $current_admin_file=="") $current_admin_file="admin";
					echo "<td align=\"center\"  bgcolor=\"$bgcolor1\" >";
					echo "<a href=\"".$current_admin_file.".php?op=$modifyingAction&amp;Action=Validate&amp;sid=$ePetitionSignerId&amp;gid=$gid\"><img src=\"images/epetitions/yes.gif\" height=\"16\" width=\"16\" border=\"0\" alt=\""._NEP_ACCEPTSIGNERS."\" title=\""._NEP_ACCEPTSIGNERS."\"></a>";
					echo "<a href=\"".$current_admin_file.".php?op=$modifyingAction&amp;Action=Delete&amp;sid=$ePetitionSignerId&amp;gid=$gid\"><img src=\"images/epetitions/delete.png\" height=\"16\" width=\"16\" border=\"0\" alt=\""._NEP_REJECTSIGNERS."\" title=\""._NEP_REJECTSIGNERS."\"></a>";
					echo "<a href=\"".$current_admin_file.".php?op=$modifyingAction&amp;Action=Resend&amp;sid=$ePetitionSignerId&amp;gid=$gid\"><img src=\"images/friend.gif\" height=\"16\" width=\"16\" border=\"0\" alt=\""._NEP_RESENDSIGNERS."\" title=\""._NEP_RESENDSIGNERS."\"></a>";
					echo "</td>\n";
				}
				echo "</tr>";
			}
		echo "</table>";
	}
}
function NSNePetitionsView($gid,$modifynotesLink)
{
	global $db, $prefix;
	$row3 = $db->sql_fetchrow($db->sql_query("SELECT name, hometext, bodytext, aid, voters, notestext FROM ".$prefix."_nsngrept_petitions WHERE ePetitionId='$gid'"));
	if(isset($row3)) {
		$name=$row3['name'];
		$aidauthor=$row3['aid'];
		$timestart=$row3['timestart'];
		$timeend=$row3['timeend'];
		$hometext = stripslashes($row3['hometext']);
		$bodytext = stripslashes($row3['bodytext']);
		$voters=intval($row3['voters']);
		$notestext=stripslashes($row3['notestext']);

		if($bodytext == "") {
			$bodytext = "$hometext";
		} else {
			$bodytext = "$hometext<br /><br />$bodytext";
		}
		
		if($notestext!="") $bodytext .= "<br />".$notestext;
		if($modifynotesLink!="")
		{
			$bodytext .="<br /><a href=\"$modifynotesLink\"><img src=\"images/epetitions/edit.png\" border=0>".(_NEP_MODIFYNOTESACTION)."</a>";
		}

		echo "<table>";
			echo "<tr><td>";
				echo "<b>$name</b><br />";
				if($aidauthor!="") echo "<font class=content>"._NEP_PUBLISHEDBY." $aidauthor, "; 
				echo _NEP_NUMBERSIGNERSNOW.$voters;
			echo "</td></tr>";
			echo "<tr><td>";
				echo $bodytext;
			echo "</td></tr>";
		echo "</table>";
	}
}
function NSNePetitionsSign($ePetitionId,$petitionName, $voters, $side, $bMoreInfo,$mailinglist)
{
	global $prefix, $multilingual, $currentlang, $db, $boxTitle, $pollcomm, $user, $cookie, $bgcolor2, $textcolor2, $myContent;

	if (is_user($user)) {
		cookiedecode($user);
		$uid = $cookie[0];
		$row = $db->sql_fetchrow($db->sql_query("SELECT name,user_email,user_from FROM ".$prefix."_users WHERE user_id='$uid'"));
		$name=$row['name'];
		$user_email=$row['user_email'];
		$user_from=$row['user_from'];
	} else {
		$uid = 0;
		$name = "";
		$user_from="";
	}

	if ($ePetitionId == 0 || $ePetitionId == "") {
		$myContent = "";
	} else {
		if (!isset($url)) {
			$url = sprintf("modules.php?name=ePetitions&amp;op=results&amp;ePetitionId=%d", $ePetitionId);
		}
		if($bMoreInfo==1) 
		{
			if($side==1 || $side==2) $myContent .="<center>";
			$myContent .="<font class=\"content\"><b>$petitionName</b></font>";
			if($side==1 || $side==2) $myContent .="</center>";
		}
		$myContent .="<center>";
			$myContent .= "<form action=\"modules.php?name=ePetitions&amp;op=new_signer\" method=\"post\">";
				$myContent .= "<input type=\"hidden\" name=\"ePetitionId\" value=\"".$ePetitionId."\">";
				$myContent .= "<input type=\"hidden\" name=\"ePetitionSignerId\" value=\"".$uid."\">";
				if($side==1 || $side==2)
				{
						$myContent .="<table border=\"0\" cellpadding=\"0\" cellspacing=\"1\" width=\"100%\">\n";
							$myContent .="<tr>\n";
								$myContent .="<td align=\"left\" bgcolor=\"$bgcolor2\"><b>"._NEP_SIGNERNAME."</b></font></td>\n";
								$myContent .="<td align=\"center\" bgcolor=\"$bgcolor2\"><b>"._NEP_SIGNEREMAIL."</b></font></td>\n";
								$myContent .="<td align=\"center\" bgcolor=\"$bgcolor2\"><b>"._NEP_SIGNERCITY."</b></font></td>\n";
								$myContent .= "<td align=\"center\" bgcolor=\"$bgcolor2\"></td>";
							$myContent .="</tr>\n";
							$myContent .="<tr>\n";
								$myContent .= "<td align=\"left\" class=\"content\"><input type=\"text\" name=\"user_name\" maxlength=\"255\" value=\"$name\"></td>";
								$myContent .= "<td align=\"center\" class=\"content\"><input type=\"text\" name=\"user_email\" maxlength=\"255\" value=\"$user_email\"></td>";
								$myContent .= "<td align=\"center\" class=\"content\"><input type=\"text\" name=\"user_from\" maxlength=\"255\" value=\"$user_from\"></td>";
								$myContent .="<td align=\"right\" class=\"content\"><input type=\"submit\" value=\""._NEP_SIGN."\"></td>";
							$myContent .="</tr>\n";
						$myContent .="</table>";
						$myContent .="<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">\n";
							$myContent .="<tr>\n";
								$myContent .="<td align=\"left\" class=\"option\"><font color=\"$textcolor2\">"._NEP_SIGNERCOMMENT."</font></td>\n";
								$myContent .="<td align=\"left\" class=\"content\"><input type=\"text\" size=\"50\" name=\"user_comment\" maxlength=\"255\"></td>";
								if(file_exists("includes/nsnml_func.php") && $mailinglist>0)
								{
									$myContent .= "<td class=\"content\" align=\"right\">"._NEP_MAILINGLISTBOX."<input type=\"checkbox\" name=\"mailinglist\" value=\"1\" checked></td></tr>";
								}

							$myContent .="</tr>\n";
						$myContent .= "</table>";
				}
				else
				{
					$myContent .="<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">\n";
						$myContent .= "<tr><td valign=\"top\" align=\"center\" class=\"option\"><font color=\"$textcolor2\">"._NEP_SIGNERNAME."</font></td></tr><tr><td class=\"content\" align=\"center\"><input type=\"text\" name=\"user_name\" size=\"15\" maxlength=\"255\" value=\"$name\"></td></tr>";
						$myContent .= "<tr><td valign=\"top\" align=\"center\" class=\"option\"><font color=\"$textcolor2\">"._NEP_SIGNEREMAIL."</font></td></tr><tr><td class=\"content\" align=\"center\"><input type=\"text\" name=\"user_email\" size=\"15\" maxlength=\"255\" value=\"$user_email\"></td></tr>";
						$myContent .= "<tr><td valign=\"top\" align=\"center\" class=\"option\"><font color=\"$textcolor2\">"._NEP_SIGNERCITY."</font></td></tr><tr><td class=\"content\" align=\"center\"><input type=\"text\" name=\"user_from\" size=\"15\" maxlength=\"255\" value=\"$user_from\"></td></tr>";
						if(file_exists("includes/nsnml_func.php") && $mailinglist>0)
						{
							$myContent .= "<tr><td class=\"content\" align=\"center\"><input type=\"checkbox\" name=\"mailinglist\" value=\"1\" checked>"._NEP_MAILINGLISTSIDEBOX."</td></tr>";
						}
						$myContent .= "<tr><td valign=\"top\" align=\"center\" class=\"option\"><input type=\"submit\" value=\""._NEP_SIGN."\"></tr></td>";
					$myContent .= "</table>";
				}
			$myContent .= "</form>\n\n";
			if($bMoreInfo==1) 
			{
				$myContent .= _NEP_SIGNERS.":".$voters;
				if($side==1 || $side==2) $myContent.=", "; else $myContent .="<br />";
				$myContent .= "<b><a href=\"modules.php?name=ePetitions&amp;op=more_info&amp;ePetitionId=$ePetitionId\">("._NEP_MOREINFO."...)</a></b>";
			}
		$myContent .= "</center>";
	}
	return $myContent;
}

// NSN Groups Check Routine
function load_usergroups_cache($user)
{
	global $db, $prefix;
	$ret=array();
	if (is_user($user)) 
	{
        $usrinfo = getusrinfo($user);
        $uid = $usrinfo['user_id'];
		
		if (file_exists("includes/cpg_page.php"))
			$result=$db->sql_query("SELECT group_id FROM ".$prefix."_bbuser_group WHERE user_id=$uid");
		else $result=$db->sql_query("SELECT gid FROM ".TABLE_NSNGR_USERS." WHERE uid=$uid");
		
		while(list($gid) = $db->sql_fetchrow($result)) 
		{
			$ret[intval($gid)]=1;
		}
	}
	return $ret;
}
function in_cpg_nsn_groups_array($user_groups_array,$gid) {
    global $prefix, $db, $user, $admin;
    if (is_admin($admin)) {
        return 1;
    } elseif (is_user($user)) {
        if(isset($user_groups_array) && array_key_exists($gid,$user_groups_array)) return 1;
    }
    return 0;
}

function in_cpg_nsn_group($gid) {
    global $prefix, $db, $user, $admin;
    if (is_admin($admin)) {
        return 1;
    } elseif (is_user($user)) {
        $usrinfo = getusrinfo($user);
        $uid = $usrinfo['user_id'];
		if (file_exists("includes/cpg_page.php"))
			$isgroup = $db->sql_query("SELECT * FROM ".$prefix."_bbuser_group WHERE group_id=$gid AND user_id=$uid");
		else
			$isgroup = $db->sql_numrows($db->sql_query("SELECT * FROM ".TABLE_NSNGR_USERS." WHERE gid=$gid AND uid=$uid"));
        if ($isgroup > 0) { return 1; } else { return 0; }
    }
    return 0;
}

function is_visible_for_user($view,$user,$admin)
{
	$gid_viewer=$view-3;

	$bShows=0;
	if ($view == 0) {
		$bShows=1;
	} elseif ($view == 1 AND is_user($user) || is_admin($admin)) {
		$bShows=1;
	} elseif ($view == 2 AND is_admin($admin)) {
		$bShows=1;
	} elseif ($view == 3 AND !is_user($user) || is_admin($admin)) {
		$bShows=1;
			// Start NSN Groups Addition
	} elseif ($view > 3 AND in_cpg_nsn_group($gid_viewer) || is_admin($admin)) {
		$bShows=1;
		  // End NSN Groups Addition
	}
	return $bShows;
}

function cached_is_visible_for_user($user_groups_array, $view,$user,$admin)
{
	$gid_viewer=$view-3;

	$bShows=0;
	if ($view == 0) {
		$bShows=1;
	} elseif ($view == 1 AND is_user($user) || is_admin($admin)) {
		$bShows=1;
	} elseif ($view == 2 AND is_admin($admin)) {
		$bShows=1;
	} elseif ($view == 3 AND !is_user($user) || is_admin($admin)) {
		$bShows=1;
			// Start NSN Groups Addition
	} elseif ($view > 3 AND in_cpg_nsn_groups_array($user_groups_array,$gid_viewer) || is_admin($admin)) {
		$bShows=1;
		  // End NSN Groups Addition
	}
	return $bShows;
}
function check_email($str)
{
	//returns 1 if valid email, 0 if not
	if(ereg("^.+@.+\\..+$", $str))
		return 1;
	else
		return 0;
}

function NSNePetitionsSendMail($module_name,$check_num,$user_email,$name)
{
	global $adminmail,$nukeurl,$sitename;
	$finishlink = "$nukeurl/modules.php?name=$module_name&op=activate&check_num=$check_num";

	$message = _WELCOMETO." $sitename ($nukeurl)!\r\n\r\n";
	$message .= _NEP_YOUUSEDEMAIL." $user_email "._NEP_TOREGISTER."\n\"$name\"\r\n\r\n";
	$message .= _NEP_TOFINISHUSER."\r\n\r\n$finishlink\r\n\r\n";
	$subject = _NEP_ACTIVATIONSUB;
	$from  = "From: $adminmail\r\n";
	$from .= "Reply-To: $adminmail\r\n";
	$from .= "Return-Path: $adminmail\r\n";
	mail($user_email, $subject, $message, $from);

}
// suscribe_nsnml is used to suscribe a user to a mailing list attached to the petition
// 0 Not suscribed
// 1 Suscribed
// 2 Already member
function NSNePetitionsSuscribeMailingList($email,$lid)
{
	global $db, $prefix;
	
	if(file_exists("includes/nsnml_func.php")==0 || $lid==0) return 0;
	
	$mid = $db->sql_numrows($db->sql_query("SELECT * FROM ".$prefix."_nsnml_lists WHERE lid='$lid'"));
	if($mid!=1) return 0;

	$mid = $db->sql_numrows($db->sql_query("SELECT * FROM ".$prefix."_nsnml_users WHERE email='$email' AND lid='$lid'"));
	if($mid==0) { // Not already suscribed
		$joined = time();	
		$query = "INSERT INTO ".$prefix."_nsnml_users VALUES (NULL, '$lid', '$email', '1', '1', '', '$joined')";
		if(!$db->sql_query($query)) return 0;
		return 1;
	}
	return 2;
}