<?php
if($_POST['save']=="ok"){
//header('Content-Type: application/octetstream');
//header('Content-Disposition: filename="emails.sql"');
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="emails.txt"');
echo $_POST['em'];
exit;
}
include('header.php');
echo $h;

$action = $_GET['action'];
if(!isset($action)) $action = "add";

IF ($action=="add"){
?>
<form name="vbform" method="post" action="mail_cp.php?action=insert">

<table class='xtable' width='100%' id='AutoNu41mber15'>
     <tr>
      <td width="100%" colspan="2" class='xtt' align="center"><?php echo $LANGAUGE_CP[311]; ?></td>
    </tr>
    <tr>
      <td width="20%" class='xtr'><?php echo $LANGAUGE_CP[312]; ?></td>
      <td width="80%" class='xtd'><input size="80" dir="ltr" name="email" /></td>
    </tr>
     <tr>
      <td width="100%" colspan="2" class='xtr' align="center">
      <input type="submit" value=" <?php echo $LANGAUGE_CP[18]; ?> " name="cmdAdd" /></td>
    </tr>
  </table>

</form>

<?php
$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$page = ($page == 0 ? 1 : $page);
$perpage = 30;
$startpoint = ($page * $perpage) - $perpage;

$queryx = mysql_query("SELECT * FROM maillist order by id desc LIMIT $startpoint,$perpage");
$xx = mysql_num_rows($queryx);
if($xx == 0){
echo "";
}else{

echo "<table class='xtable' width='100%' id='AutoNumbfgter15'>
  <tr>
      <td width='100%' colspan='4' class='xtx' align='center'>".$LANGAUGE_CP[97]."</td>
    </tr>

  <tr>
      <td width='79%' align='center' class='xtr'>".$LANGAUGE_CP[313]."</td>
      <td width='7%' align='center' class='xtr'>".$LANGAUGE_CP[28]."</td>
      <td width='7%' align='center' class='xtr'>".$LANGAUGE_CP[26]."</td>
      <td width='7%' align='center' class='xtr'>".$LANGAUGE_CP[27]."</td>
    </tr>";
while($p = mysql_fetch_array($queryx))
{
$p['email'] = stripslashes($p['email']);
$p['email'] = htmlspecialchars($p['email']);

if($p['active'] == 1){
$im = "images/on.gif";
$li = "mail_cp.php?action=actx&id=$p[id]&act=0";
$te = $LANGAUGE_CP[72];
}else{
$im = "images/off.gif";
$li = "mail_cp.php?action=actx&id=$p[id]&act=1";
$te = $LANGAUGE_CP[73];
}

echo "<tr>
      <td width='79%' align='center' class='xtd'>".$p['email']."</td>
      <td width='7%' align='center' class='xtd'><a href='$li'><img border='0' src='$im' alt='$te ".$p['email']."' /></a></td>
      <td width='7%' align='center' class='xtd'><a href='mail_cp.php?action=edit&id=$p[id]'><img border='0' src='images/edit.gif' alt='".$p['email']."' /></a></td>
      <td width='7%' align='center' class='xtd'><a href='mail_cp.php?action=delete&id=$p[id]'><img border='0' src='images/delete.gif' alt='".$p['email']."' /></a></td>
    </tr>";
}
echo "<tr>
      <td width='100%' colspan='4' class='xtr' align='center'><a href='mail_cp.php?action=code'>".$LANGAUGE_CP[314]."</a></td>
    </tr>
    </table>";

$pagesnum = @ceil(mysql_num_rows(mysql_query("SELECT * FROM maillist")) / $perpage);
if($pagesnum > 1){
echo "<p align='center'>";
for ($i=1; $i<=$pagesnum; $i++) {
if ($i != $page) {
$z = "[<a href='mail_cp.php?action=add&page=$i'>$i</a>] ";
} else {
$z = "[<u>$i</u>]";
}
echo "$z";
}
echo "</p>";
}
}

}ELSEIF ($action=="insert"){

$email = addslashes($_POST['email']);
$email = trim($email);

if (empty($email)){
echo $LANGAUGE_CP[315];
}elseif(!eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.([a-zA-z]{2,4})$',$email)){
echo $LANGAUGE_CP[316];
}else{
$qn = mysql_num_rows(mysql_query("SELECT * FROM maillist where email='$email'"));
if($qn==0){
$query =  mysql_query ("INSERT INTO maillist (email,active,date) VALUES ('$email','1','".time()."')") or die ("error");
echo $LANGAUGE_CP[21];
echo "<META HTTP-EQUIV='refresh' CONTENT='1; URL=mail_cp.php?action=add' />";
}else{
echo $LANGAUGE_CP[317];
}
}


}ELSEIF ($action=="edit"){
$id = intval($_GET['id']);
$query = mysql_query("SELECT * FROM maillist where id='$id'");
$p = mysql_fetch_array($query);

$p['email'] = stripslashes($p['email']);
$p['email'] = htmlspecialchars($p['email']);

echo "<form name='vbform' method='POST' action='mail_cp.php?action=editok'>
<input type='hidden' name='id' value='$p[id]' />

<table class='xtable' width='100%' id='AutoNumber15'>

<tr>
<td width='100%' colspan='2' align='center' class='xtx'>".$LANGAUGE_CP[32]."</td>
</tr>

<tr>
<td width='30%' class='xtr'>".$LANGAUGE_CP[313]."</td>
<td width='70%' class='xtd'><input name='email' size='80' value='".$p['email']."' /></td>
</tr>

<tr>
<td width='100%' colspan='2' align='center' class='xtr'><input type='submit' value=' ".$LANGAUGE_CP[33]." ' name='B1' /></td>
</tr>

</table>

</form>";

}ELSEIF ($action=="editok"){

$id    = intval($_POST['id']);
$email = addslashes($_POST['email']);
$email = trim($email);

if (empty($email)){
echo $LANGAUGE_CP[315];
}else{
$query =  mysql_query("UPDATE maillist SET email='$email' where id='$id'");
echo $LANGAUGE_CP[34];
echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=mail_cp.php?action=add' />";
}

}ELSEIF ($action=="delete"){
$id = intval($_GET['id']);
$query =  mysql_query("DELETE FROM maillist WHERE id='$id'");
echo $LANGAUGE_CP[35];
echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=mail_cp.php?action=add' />";

}ELSEIF ($action=="actx"){
$id = intval($_GET['id']);
$act = intval($_GET['act']);

if ($act == 0){
$query =  mysql_query("UPDATE maillist SET active='$act' where id='$id'");
echo $LANGAUGE_CP[61];
echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=mail_cp.php?action=add' />";
}elseif ($act == 1){
$query =  mysql_query("UPDATE maillist SET active='$act' where id='$id'");
echo $LANGAUGE_CP[62];
echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=mail_cp.php?action=add' />";
}else{
echo $LANGAUGE_CP[63];
echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=mail_cp.php?action=add' />";
}

}ELSEIF ($action=="code"){
$queryx = mysql_query("SELECT * FROM maillist where active=1 order by id desc");
$xx = mysql_num_rows($queryx);
if($xx == 0){
$em = $LANGAUGE_CP[318];
}else{
$em = "";
$i=0;
while($p = mysql_fetch_array($queryx))
{
++$i;
$p['email'] = stripslashes($p['email']);
$p['email'] = htmlspecialchars($p['email']);

if($i == $xx){
$sy = "";
}else{
$sy = ",";
}

$em .= $p['email'].$sy;
}

echo '<form name="FormName" action="" method="post">
<input name="save" type="hidden" value="ok" />

<table class="xtable" width="100%" id="AutoNumbfgter15">
  <tr>
      <td class="xtx" align="center">'.$LANGAUGE_CP[319].'</td>
    </tr>

  <tr>
      <td align="center" class="xtd">
<textarea dir="ltr" name="em" rows="15" cols="120">'.$em.'</textarea>
  </td>
  </tr>

  <tr>
      <td align="center" class="xtr">
<input name="s" type="submit" value=" '.$LANGAUGE_CP[319].' " />
  </td>
  </tr>

</table>
</form>';

}


}

echo $f;
?>