<?
/*
   File name         : mailbox.php
   Version           : 1.1.3
   Last Modified By  : Umut Gokbayrak
   e-mail            : umut@trlinux.com
   Purpose           : This is a generic file that lists all the mail
                       headers in the current mailbox. Clicking on a header will
                       redirect the user to a POP3 or IMAP stream.
   Last modified     : 7 Nov 2000
*/

include ("includes/global.inc");
session_start();

$mbox_id=addslashes($_GET["mbox_id"]);
$f=$_GET["f"];
$topmsg=addslashes($_GET["topmsg"]);
$ID=addslashes($_SESSION["ID"]);

// If first login redirect to preferences screen
if ($f == 1) {
  Header("Location: preferences.php?f=1");
}

// ID comparison between logged hash and session. If they are both the same, let the user to go on...
$dbq = $db->execute("select user_id,username,password from tblUsers where hash = '$ID'");
$user_id = $dbq->fields['user_id'];
$username = $dbq->fields['username'];
$password = $dbq->fields['password'];
$dbq->close();

if (($user_id == "") || (strlen($ID)==0)){
  Header("Location: index.php?error_id=1");
} else {
  function mychop($thistring,$length) {
    $result="";
    $mystring=trim($thistring);
    if (strlen($mystring)>$length) {
      $result=substr($mystring,0,$length);
      $result = "$result...";
    }else {
      $result=$mystring;
    }

    return($result);
  }

  $mbox_id = rawurldecode($mbox_id);

  include ("$postaci_directory" . "classes/imap_pop3.inc");


  $email=new imap_pop3($default_port,$default_protocol,$default_host,$username,$password,$mbox_id);
  if ($f !=1 && $mbox_id != "INBOX") {$email->mbox_exists($mbox_id);}
  include ("$postaci_directory" . "includes/commonhead.inc");
  include ("$postaci_directory" . "includes/stylesheets.inc");
  include ("$postaci_directory" . "includes/javascripts.inc");
  include ("$postaci_directory" . "includes/functions.inc");

?>
  <meta http-equiv="refresh" content="180; url=mailbox.php?mbox_id=<? echo $mbox_id; ?>">
  </head>

  <body bgcolor='#FFCC88' leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" background="images/background.gif" text="#000000" link="#4F0000" vlink="#4F0000" alink="#4F0000">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="1%" bgcolor="#4F0000"><img src="images/empty.gif" width="15" height="1"></td>
      <td width="1%" bgcolor="#4F0000" ><img src="images/empty.gif" width="152" height="1"></td>
      <td width="98%" bgcolor="#4F0000" align="right"><img src="images/minilogo.jpg">
      </td>
    </tr>
  </table>
  <table width="100%" border="0" cellspacing="0" cellpadding="0" height="400">
    <tr>
      <td width="1%" bgcolor="#4F0000"><img src="images/empty.gif" width="15" height="1"></td>
      <td width="1%" valign="top" align="left"><img src="images/company_logo2.jpg" width="152" height="152"><br>
        <table width="120" border="0" cellspacing="0" cellpadding="0" align="center">
          <tr>
            <td>
              <img src="images/dot.gif" width="10" height="10"> <a href="sendmail.php?post_act=0&mbox_id=<? echo $mbox_id; ?>"><? echo "$text25"; ?></a>
            </td>
          </tr>
          <tr>
            <td>
              <img src="images/dot.gif" width="10" height="10"> <a href="mailbox.php?mbox_id=INBOX"><? echo "$text26"; ?></a>
            </td>
          </tr>
          <tr>
            <td>
              <img src="images/dot.gif" width="10" height="10"> <a href="folders.php"><? echo "$text27"; ?></a>
            </td>
          </tr>
          <tr>
            <td>
              <table border="0" cellpadding="0" cellspacing="0" width="111">
                <tr>
                  <td width="8">
                    <img src="images/empty.gif" height="1" width="15">
                  </td>
                  <td width="112">
                    <?
                      if ($default_protocol == "pop3") {
                        echo "- <a href=\"mailbox.php?mbox_id=INBOX\">INBOX</a><br>";
                      }
                      $folders = $email->getMailboxes();
                      $folder_names = array();
                      $folder_counter=-1;
                      while (list($key,$val) = each($folders["name"])) {
                        $folder_counter++;
                        $folder_names[$folder_counter] = $val;
                      }
                      $ids = array();
                      $folder_counter=-1;
                      while (list($key,$val) = each($folders["id"])) {
                        $folder_counter++;
                        $ids[$folder_counter] = $val;
                      }

                      for($i=0;$i<count($ids);$i++) {
                        echo "- <a href=\"mailbox.php?mbox_id=$ids[$i]\">$folder_names[$i]</a><br>";
                      }
                    ?>
                  </td>
                </tr>
              </table>
            </td>
          </tr>
          <tr>
            <td>
              <img src="images/dot.gif" width="10" height="10"> <a href="preferences.php"><? echo "$text28"; ?></a>
            </td>
          </tr>
          <tr>
            <td>
              <img src="images/dot.gif" width="10" height="10"> <a href="logout.php"><? echo "$text29"; ?></a>
            </td>
          </tr>
          <tr valign="bottom" align="center">
          <?
            if($show_sponsor) {
              echo "<td height=\"75\" class=\"style=\"8\">$text78</td>\n";
            } else {
              echo "<td height=\"75\" class=\"style=\"8\">&nbsp;</td>\n";
            }
          ?>
          </tr>
          <tr>
            <td>
              <div align="center">
                <table width="111" border="0" cellspacing="0" cellpadding="1" bgcolor="#333333">
                  <tr>
                  <?
                    if ($show_sponsor) {
                      echo "<td><a href=\"http://www.yimpas.net.tr\"><img src=\"images/sponsor.gif\" width=\"109\" height=\"30\" border=\"0\"></a></td>\n";
                    }
                  ?>
                  </tr>
                </table>
              </div>
            </td>
          </tr>
        </table>
      </td>
      <td width="98%" valign="top">

      <table width="100%" border="0" cellspacing="0" cellpadding="0" height="150">
       <? include ($postaci_directory . "includes/headinside.inc"); ?>
        <tr>
          <td width="10" height="180" valign="top">
            <p><img src="images/empty.gif" width="1" height="180"></p>
          </td>
          <td valign="top" align="left">
            <hr size="1">
            <form method="post" action="message_move.php" name="mesajlar" onSubmit="submitonce(this)">
              <input type="hidden" name="mbox_id" value="<? echo $mbox_id; ?>">

            <table width="100%" border="0" cellspacing="0" cellpadding="0" height="18">
              <tr bgcolor="#FFAA33">
                <?
                  $mailadress = getMailAdress($user_id);
                  if ($email->protocol == "pop3") {
                    if (trim(strtoupper($mbox_id)) != "INBOX") {
                      $current_folder = getFolderName($mbox_id);
                    } else {
                      $current_folder = $mbox_id;
                    }
                  } else {
                    $current_folder = $mbox_id;
                  }
                ?>
                <td>&nbsp;<font color="#4E4E4E"><b> <? echo htmlspecialchars($mailadress) . " - " . $current_folder; ?></b></font></td>
              </tr>
            </table>
            &nbsp;<br>
            &nbsp;<br>
              <table width="100%" border="0" cellspacing="2" cellpadding="0">
                <tr align="right">
                  <td colspan="6">
                    <div align="right">

                    <?
                      echo "$text16 ";
                      echo "<select name=\"slcmbox\">\n";
                      for($i=0;$i<count($ids);$i++) {
                        if (trim($folder_names[$i]) != "INBOX" && trim($folder_names[$i]) != $current_folder) {
                          $folder_id = getFolderID($folder_names[$i],$user_id);
                          echo "<option value=\"$folder_id\">$folder_names[$i]</option>\n";
                        }
                      }
                      echo "</select>\n";
                      echo $text17;
                      echo "<input type=\"submit\" name=\"Submit\" value=\"$text18\">\n";
                     ?>

                      </div>
                  </td>
                </tr>
                <tr>
                  <td width="3%" bgcolor="#FFAA33">
                    <input type="checkbox" name="allbox" value="checkbox" onClick="ToggleAll();">
                  </td>
                  <td width="2%" bgcolor="#FFAA33"><img src="images/empty.gif" width="17" height="8"></td>
                  <?
                  if ($default_protocol == "pop3") {
                    $dbq = $db->execute("select mbox_id from tblMailBoxes where user_id=$user_id and mbox_type = 1");
                    $POP3toFolder = $dbq->fields['mbox_id'];
                    $dbq->close();
                  }

                  if ($mbox_id == "$to_folder" || $mbox_id == $POP3toFolder) {
                    echo "<td width=\"29%\" bgcolor=\"#FFAA33\"><font color=\"#4E4E4E\"><b>$text32</b></font></td>";
                  } else {
                    echo "<td width=\"29%\" bgcolor=\"#FFAA33\"><font color=\"#4E4E4E\"><b>$text12</b></font></td>";
                  }
                  ?>
                  <td width="46%" bgcolor="#FFAA33"><font color="#4E4E4E"><b><? echo "$text13"; ?></b></font></td>
                  <td width="12%" bgcolor="#FFAA33"><font color="#4E4E4E"><b><? echo "$text14"; ?></b></font></td>
                  <td width="8%" bgcolor="#FFAA33"><font color="#4E4E4E"><b><? echo "$text15"; ?></b></font></td>
                </tr>
                <?
                  if ($email->empty_mailbox()) {
                    echo "  <tr>\n";
                    echo "    <td colspan='6' align='center'>\n";
                    echo "      &nbsp;<br><b><font color=\"#8F0000\">$text24</font></b>&nbsp;<p><hr size=\"1\">\n";
                    echo "    </td>\n";
                    echo "  </tr>\n";
                  } else {
                    $postaci_headers = $email->postaci_get_headers();

                    $message_count_right_now = count($postaci_headers["status"]);

                    for ($i = 0; $i < $message_count_right_now ;$i++) {

                      $num = $postaci_headers["msg_no"][$i];
                      if ($postaci_headers["status"][$i] == 1) {
                        // Missatge nou segons POP, l'insertem a la BD (si ja existia a la BD, no s'insertara)
                        $dbq = $db->execute("INSERT INTO tblNewMail (user_id, id) VALUES ('$user_id',
                        '$num')");
                        $dbq->close();
                      } else {
                        $message_new_tag_begin = "";
                        $message_new_tag_end = "";
                      }

                      // Si es tracta d'un missatge nou
                      // * POP3 marca els missatges com llegits nomes mirant la seva capçalera
                      // per aixo hem de controlar quins son els missatges nous que encara no hem
                      // visualitzat.
                      $dbq = $db->execute("SELECT user_id FROM tblNewMail WHERE id='$num'");
                      if (($dbq->fields['user_id'] == $user_id) && ($mbox_id == "INBOX")) {
                        $message_new_tag_begin = "<b>";
                        $message_new_tag_end = "</b>";
                      }
                      $dbq->close();

                      echo "<tr>\n";
                      $mesaj_numerosu = $postaci_headers["msg_no"][$i];
                      echo "  <td width='3%' bgcolor='#FFDDAA'><font color='#000000'><input type='checkbox' name='chk[]' value='$mesaj_numerosu'></font></td>\n";


                      if ($postaci_headers["attach"][$i] == 1) {
                        echo "  <td width='3%' bgcolor='#FFDDAA' nowrap align='center'><img src='images/attach.gif'></td>\n";
                      } else {
                        echo "  <td width='3%' bgcolor='#FFDDAA' nowrap align='center'>&nbsp;</td>\n";
                      }

                      $urlenc_mbox_id = rawurlencode($mbox_id);
                      if ($mbox_id == "$to_folder" || $mbox_id == $POP3toFolder) {
                          if ($default_protocol == "pop3" && $mbox_id != "INBOX") {
                            $post_act = 5;
                          } else {
                            $post_act = 3;
                          }
                          echo "<td width=\"29%\" bgcolor=\"#FFDDAA\" nowrap><a href=\"sendmail.php?topmsg=" . $topmsg . "&msg_id=" . $postaci_headers["msg_no"][$i] . "&mbox_id=$urlenc_mbox_id&post_act=" . $post_act . "\">";
                          echo "$message_new_tag_begin" . mychop($postaci_headers["to"][$i],30) . "$message_new_tag_end";
                          echo "</a>&nbsp;&nbsp;</td>";
                      } else {
                          if ($default_protocol == "pop3" && $mbox_id != "INBOX") {
                            $post_act = 7;
                          } else {
                            $post_act = 1;
                          }
                          echo "<td width=\"29%\" bgcolor=\"#FFDDAA\" nowrap><a href=\"sendmail.php?topmsg=" . $topmsg . "&msg_id=" . $postaci_headers["msg_no"][$i] . "&mbox_id=$urlenc_mbox_id&post_act=$post_act\">";
                          echo "$message_new_tag_begin" . mychop($postaci_headers["from"][$i],30) . "$message_new_tag_end";
                          echo "</a>&nbsp;&nbsp;</td>";
                      }
                      echo "<td width=\"46%\" bgcolor=\"#FFDDAA\" nowrap><a href=\"readmessage.php?topmsg=" . $topmsg . "&mbox_id=$urlenc_mbox_id&msg_no=" . $postaci_headers["msg_no"][$i] . "\">";
                      if ($postaci_headers["subject"][$i] == "") {
                        $postaci_headers["subject"][$i] = $text40;
                      }
                      echo "$message_new_tag_begin" . mychop(turkcelestir($postaci_headers["subject"][$i]),50) . "$message_new_tag_end";
                      echo "</a>&nbsp;&nbsp;</td>";
                      echo "<td width=\"12%\" bgcolor=\"#FFDDAA\" nowrap>" . $postaci_headers["msg_date"][$i];
                      echo "</td>";
                      echo "<td width=\"8%\" bgcolor=\"#FFDDAA\" nowrap align=\"center\">" . $postaci_headers["size"][$i] . "</td>";
                      echo "</tr>\n";
                    } // end for
                  } // end if
                ?>
              </table>
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td align="right">
                      <table width="100%" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                          <td>
                            <b><font color="#8F0000"><? echo "$text19 : $email->messagecount"; ?></font></b>
                          </td>
                          <td align="right">
                            <input type="submit" name="Submit2" value="<? echo $text57; ?>" onClick="formMailBox('1')">
                          </td>
                        </tr>
                      </table>
                    </td>
                  </tr>
                </table>
              </form>
              <?
                // Page identification with numbers.

                $how_many = calculateHowManyPages($email->messagecount);

                if ($how_many !=1) {
                  echo "<div align='center'>\n";
                  if ($default_protocol == "imap" || $mbox_id == "INBOX") { // IMAP or POP3 inbox
                    $tmptop = $email->messagecount;
                    for ($j=$how_many ; $j>0 ; $j = $j-1) {
                      $tmpbottom = $tmptop - $seperator +1;
                      if ($tmpbottom <= 0) {
                        $tmpbottom = 1;
                      }
                      if ($tmptop != $tmpbottom) {
                        echo "[ <b><a href='mailbox.php?mbox_id=$mbox_id&topmsg=$tmptop'>$tmptop-$tmpbottom</a></b> ] ";
                      } else {
                        echo "[ <b><a href='mailbox.php?mbox_id=$mbox_id&topmsg=$tmptop'>$tmptop</a></b> ]";
                      }
                      $tmptop = $tmpbottom -1;
                    }
                  } else {                                                 // pop3 db folder
                    if ($tmptop == 0) {
                      $dbq = $db->execute("select message_id from tblMessages where mbox_id=$mbox_id and user_id = $user_id order by message_id desc");
                      $max_msg_id = $dbq->fields['message_id'];
                      $dbq->close();
                      $topmsg = $max_msg_id;
                    }
                    $tmptop = $email->messagecount;
                    for ($j=$how_many ; $j>0 ; $j = $j-1) {
                      if ($topmsg2 != 0) {
                        $topmsg = $topmsg2;
                      }
                      $tmpbottom = $tmptop - $seperator +1;
                      $dbq = $db->execute("select message_id from tblMessages where mbox_id=$mbox_id and user_id = $user_id and message_id <= $topmsg order by message_id desc");
                      for ($n = 1;$n <= $seperator; $n++) {
                        $dbq->nextRow();
                      }
                      $topmsg2 = $dbq->fields['message_id'];
                      $dbq->close();

                      if ($tmpbottom <= 0) {
                        $tmpbottom = 1;
                      }
                      if ($tmptop != $tmpbottom) {
                        echo "[ <b><a href='mailbox.php?mbox_id=$mbox_id&topmsg=$topmsg'>$tmptop-$tmpbottom</a></b> ] ";
                      } else {
                        echo "[ <b><a href='mailbox.php?mbox_id=$mbox_id&topmsg=$topmsg'>$tmptop</a></b> ]";
                      }
                      $tmptop = $tmpbottom -1;

                    }
                  }
                  echo "</div>\n";
                }
              ?>
            <p>&nbsp; </p>
          </td>
          <td valign="top" width="10">&nbsp;</td>
        </tr>
        <tr>
          <td width="10" height="80" valign="top">&nbsp;</td>
          <td valign="bottom" align="right">
            <hr size="1">
            <div align="right"><span class="style8"><? echo $text84; ?></span></div>
            <p>
          </td>
          <td>&nbsp;</td>
        </tr>
      </table>
      </td>
    </tr>
  </table>
  </body>
  </html>

<?
}
?>
