<?php

/********************************************************************************
  - MemHT Portal -

  Copyright (C) 2007-2008 by Miltenovik Manojlo
  http://www.memht.com

  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 opinion) 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 General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, see <http://www.gnu.org/licenses/> (GPLv2)
  or write to the Free Software Foundation, Inc., 51 Franklin Street,
  Fifth Floor, Boston, MA02110-1301, USA.

********************************************************************************/

if (!defined("_LOAD_PAGE_")) {
  die("<table style='padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;' align='center'><tr><td><b>Error:</b> This file cannot be opened directly!</td></tr></table>");
}

if (isset($_GET['op'])) { $op = inCode($_GET['op']); } else { $op = ""; }
if (isset($_GET['id'])) { $id = intval($_GET['id']); } else { $id = ""; }

if (isset($_REQUEST['recipient'])) { $recipient = inCode($_REQUEST['recipient']); } else { $recipient = ""; }
if (isset($_POST['name'])) { $name = censure(inCode($_POST['name'])); } else { $name = ""; }
if (isset($_POST['text'])) {
	if (!isset($_POST['Preview'])) {
		$text = censure(inCode($_POST['text']));
	} else {
		$text = censure(inCode($_POST['text'],0));
	}
} else { $text = ""; }
if (isset($_POST['smiles'])) { $smiles = intval($_POST['smiles']); } else { $smiles = ""; }
if (isset($_POST['operation'])) { $operation = inCode($_POST['operation']); } else { $operation = ""; }
if (isset($_GET['quote'])) { $quote = inCode($_GET['quote']); } else { $quote = false; }

if (isset($_POST['ok'])) { $ok = inCode($_POST['ok']); } else { if (isset($_GET['ok'])) { $ok = inCode($_GET['ok']); } else { $ok = false; } }

if (isset($_GET['pg'])) { $pg = inCode($_GET['pg']); } else { $pg = 1; }
$ofsppg = 40; //Items per page
$ofsbgn = ($pg*$ofsppg)-$ofsppg;

function viewMain($ofsbgn,$ofsppg,$pg) {
  global $dblink,$siteConfig,$userid,$userInfo;

?>
  <div align="right" style="margin:4px 0;"><a href="index.php?page=pvtmsg&op=newMessage" title="<?php echo _NEWMESSAGE_; ?>"><img src="templates/<?php echo $siteConfig['template'] ?>/images/forum/buttons/newmessage.gif" border="0" alt="<?php echo _NEWMESSAGE_; ?>"></a></div>

  <form name="pvtmsg" method="post" action="index.php?page=pvtmsg&op=deleteSelected">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td>
          <table width="100%" border="0" cellspacing="1" cellpadding="2">
            <tr>
              <td colspan="4" class="forum_intitle"><b><?php echo _NEWMESSAGES_; ?></b></td>
            </tr>
            <tr>
              <td class="forum_legend" nowrap>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
              <td width="60%" class="forum_legend"><b><?php echo _TITLE_; ?></b></td>
              <td width="20%" align="center" class="forum_legend"><b><?php echo _AUTHOR_; ?></b></td>
              <td width="20%" align="center" class="forum_legend"><b><?php echo _DATE_; ?></b></td>
            </tr>

<?php
          $n = 0;
          $result = $dblink->get_list("SELECT p.*,DATE_FORMAT(p.date, '".$siteConfig['timestamp']."') as date,u.id AS uid FROM memht_pvtmsg AS p JOIN memht_utenti AS u ON p.author=u.user WHERE p.recipient='".$userInfo['user']."' AND p.new=1 ORDER BY p.id DESC");
          foreach ($result as $row) {
            $pm_id = intval($row['id']);
            $pm_author = outCode($row['author']);
            $pm_name = outCode($row['name'],0);
            $pm_date = $row['date'];
			$uid = intval($row['uid']);

?>
            <tr>
              <td class="forum_post" width="1%"><input type="checkbox" name="deletenewpm[]" value="<?php echo $pm_id ?>"></td>
              <td class="forum_post"><a href="index.php?page=pvtmsg&op=viewMessage&id=<?php echo $pm_id ?>" title="<?php echo $pm_name ?>"><b><?php echo $pm_name ?></b></a></td>
              <td align="center" class="forum_post"><a href="index.php?page=users&op=userInfo&uid=<?php echo $uid ?>" title="<?php echo $pm_author ?>"><?php echo $pm_author ?></a></td>
              <td align="center" class="forum_post" nowrap><?php echo $pm_date ?></td>
            </tr>
<?php

            $n++;
          }
          if ($n==0) {

?>
            <tr>
              <td colspan="4" align="center" class="forum_post"><?php echo _EMPTY_; ?></td>
            </tr>
<?php

          }

?>
          </table>
        </td>
      </tr>
    </table>
    <div class="info"><?php echo _SELECT_; ?>: <a href="javascript:void(0);" onclick="selectAll('deletenewpm');" title="<?php echo _ALL_; ?>"><?php echo _ALL_; ?></a> / <a href="javascript:void(0);" onclick="selectNone('deletenewpm');" title="<?php echo _NONE_; ?>"><?php echo _NONE_; ?></a></div>
    <br>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td>
          <table width="100%" border="0" cellspacing="1" cellpadding="2">
            <tr>
              <td colspan="4" class="forum_intitle"><b><?php echo _OLDMESSAGES_; ?></b></td>
            </tr>
            <tr>
              <td class="forum_legend" nowrap>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
              <td width="60%" class="forum_legend"><b><?php echo _TITLE_; ?></b></td>
              <td width="20%" align="center" class="forum_legend"><b><?php echo _AUTHOR_; ?></b></td>
              <td width="20%" align="center" class="forum_legend"><b><?php echo _DATE_; ?></b></td>
          </tr>
<?php

        $n = 0;
		 $result = $dblink->get_list("SELECT p.*,DATE_FORMAT(p.date, '".$siteConfig['timestamp']."') as date,u.id AS uid FROM memht_pvtmsg AS p JOIN memht_utenti AS u ON p.author=u.user WHERE p.recipient='".$userInfo['user']."' AND p.new=0 ORDER BY p.id DESC LIMIT $ofsbgn,$ofsppg");
        foreach ($result as $row) {
          $pm_id = intval($row['id']);
          $pm_author = outCode($row['author']);
          $pm_name = outCode($row['name'],0);
          $pm_date = $row['date'];
		  $uid = intval($row['uid']);
			
?>
          <tr>
            <td class="forum_post" width="1%"><input type="checkbox" name="deletepm[]" value="<?php echo $pm_id ?>"></td>
            <td class="forum_post"><a href="index.php?page=pvtmsg&op=viewMessage&id=<?php echo $pm_id ?>" title="$pm_name"><?php echo $pm_name ?></a></td>
            <td align="center" class="forum_post"><a href="index.php?page=users&op=userInfo&uid=<?php echo $uid ?>" title="<?php echo $pm_author ?>"><?php echo $pm_author ?></a></td>
            <td align="center" class="forum_post" nowrap><?php echo $pm_date ?></td>
          </tr>
<?php

          $n++;
        }
        if ($n==0) {

?>
          <tr>
            <td colspan="4" align="center" class="forum_post"><?php echo _EMPTY_; ?></td>
          </tr>
<?php

        }

?>
          </table>
        </td>
      </tr>
    </table>
    <table width="100%" cellpadding="0" cellspacing="0" border="0" style="padding-top:4px;">
      <tr>
        <td class="info" valign="top"><?php echo _SELECT_; ?>: <a href="javascript:void(0);" onclick="selectAll('deletepm');" title="<?php echo _ALL_; ?>"><?php echo _ALL_; ?></a> / <a href="javascript:void(0);" onclick="selectNone('deletepm');" title="<?php echo _NONE_; ?>"><?php echo _NONE_; ?></a></td>
        <td align="right"><input type="image" name="Submit" src="templates/<?php echo $siteConfig['template'] ?>/images/forum/buttons/delete.gif" onclick="return doconfirm();"></td>
      </tr>
    </table>
  </form>
<?php

  //Pages
  include_once("inc/class/paginationSystem.class.php");
  $ps = new paginationSystem();
  $ps->items = $ofsppg;
  $ps->actpg = $pg;
  $ps->query = "SELECT id FROM memht_pvtmsg WHERE recipient='".$userInfo['user']."' ORDER BY id DESC";
  $ps->url = "index.php?page=pvtmsg&pg={{N}}";
  $ps->show();
}

function viewOutbox($ofsbgn,$ofsppg,$pg) {
  global $dblink,$siteConfig,$userInfo;

?>
  <div align="right" style="margin:4px 0;"><a href="index.php?page=pvtmsg&op=newMessage" title="<?php echo _NEWMESSAGE_; ?>"><img src="templates/<?php echo $siteConfig['template'] ?>/images/forum/buttons/newmessage.gif" border="0" alt="<?php echo _NEWMESSAGE_; ?>"></a></div>

    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td>
          <table width="100%" border="0" cellspacing="1" cellpadding="2">
            <tr>
              <td colspan="4" class="forum_intitle"><b><?php echo _SENT_MESSAGES_; ?></b></td>
            </tr>
            <tr>
              <td width="60%" class="forum_legend"><b><?php echo _TITLE_; ?></b></td>
              <td width="20%" align="center" class="forum_legend"><b><?php echo _RECIPIENT_; ?></b></td>
              <td width="20%" align="center" class="forum_legend"><b><?php echo _DATE_; ?></b></td>
          </tr>
<?php

        $n = 0;
		$result = $dblink->get_list("SELECT p.*,DATE_FORMAT(p.date, '".$siteConfig['timestamp']."') as date,u.id AS uid FROM memht_pvtmsg AS p JOIN memht_utenti AS u ON p.recipient=u.user WHERE p.author='".$userInfo['user']."' ORDER BY p.id DESC LIMIT $ofsbgn,$ofsppg");
        foreach ($result as $row) {
          $pm_id = intval($row['id']);
          $pm_recipient = outCode($row['recipient']);
          $pm_name = outCode($row['name'],0);
          $pm_date = $row['date'];
		  $uid = intval($row['uid']);
		  
?>
          <tr>
            <td class="forum_post"><a href="index.php?page=pvtmsg&op=viewOutboxMessage&id=<?php echo $pm_id ?>" title="$pm_name"><?php echo $pm_name ?></a></td>
            <td align="center" class="forum_post"><a href="index.php?page=users&op=userInfo&uid=<?php echo $uid ?>" title="<?php echo $pm_recipient ?>"><?php echo $pm_recipient ?></a></td>
            <td align="center" class="forum_post" nowrap><?php echo $pm_date ?></td>
          </tr>
<?php

          $n++;
        }
        if ($n==0) {

?>
          <tr>
            <td colspan="4" align="center" class="forum_post"><?php echo _EMPTY_; ?></td>
          </tr>
<?php

        }

?>
          </table>
        </td>
      </tr>
    </table>
<?php

  //Pages
  include_once("inc/class/paginationSystem.class.php");
  $ps = new paginationSystem();
  $ps->items = $ofsppg;
  $ps->actpg = $pg;
  $ps->query = "SELECT id FROM memht_pvtmsg WHERE author='".$userInfo['user']."' ORDER BY id DESC";
  $ps->url = "index.php?page=pvtmsg&op=viewOutbox&pg={{N}}";
  $ps->show();
}

function newMessage($recipient,$name,$text,$smiles,$operation,$ok=false) {
  global $dblink,$siteConfig,$userInfo,$tzNOW;

?>
  <form name="pvtmsg" method="post" action="index.php?page=pvtmsg&op=newMessage">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td>
          <table width="100%" border="0" cellspacing="1" cellpadding="2">
<?php

    if (!$ok) {

?>
            <tr>
              <td colspan="3" class="forum_post_title"><b><?php echo _NEWMESSAGE_; ?></b></td>
            </tr>
            <tr>
              <td colspan="3" class="forum_spacer">&nbsp;</td>
            </tr>
            <tr>
              <td width="20%" class="forum_post" valign="top"><b><?php echo _RECIPIENT_; ?></b> <img src="images/info.gif" alt="Info" title="header=[<?php echo _INFORMATIONS_; ?>] body=[<?php echo _INFO_EXPLAIN_MULTI_RECIPIENTS_; ?>]"></td>
              <td width="80%" class="forum_post" valign="top"><input type="text" name="recipient" value="<?php echo $recipient ?>" style="width:40%;" maxlength="255"> <input type='button' value='<?php echo _FIND_; ?>' onClick="openPopup('inc/popwin/finduser.php?form=pvtmsg&element=recipient&add=true&username='+document.forms['pvtmsg'].elements['recipient'].value+'','350','400');"></td>
            </tr>
            <tr>
              <td class="forum_post" valign="top"><b><?php echo _TITLE_; ?></b></td>
              <td width="80%" class="forum_post" valign="top"><input type="text" name="name" style="width:95%;" maxlength="255"></td>
            </tr>
            <tr>
              <td class="forum_post" valign="top"><b><?php echo _MESSAGE_; ?></b><?php echo smileBox("pvtmsg","text"); ?></td>
              <td width="80%" class="forum_post" valign="top"><?php echo bbcodeToolbar("pvtmsg","text"); ?> <textarea name="text" cols="63" rows="12" style="width:95%; height:200px;"></textarea></td>
            </tr>
            <tr>
              <td class="forum_post"><b><?php echo _SMILES_; ?></b> <img src="images/info.gif" alt="Info" title="header=[<?php echo _INFORMATIONS_; ?>] body=[<?php echo _SETOFFWHENPOSTINGCODE_; ?>]"></td>
              <td class="forum_post"><input name="smiles" type="radio" value="1" checked="checked"><?php echo _ON2_; ?> <input name="smiles" type="radio" value="0"><?php echo _OFF_; ?></td>
            </tr>
            <tr>
              <td colspan="3" class="forum_post" align="center" valign="top"><input type="submit" name="Preview" value="<?php echo _PREVIEW_; ?>"> <input type="submit" name="Send" value="<?php echo _SEND_; ?>"></td>
            </tr>
            <tr>
              <td colspan="3" class="forum_spacer">&nbsp;<input type="hidden" name="ok" value="true"></td>
            </tr>
<?php

    } else {
        if (isset($_POST['Send'])) {
          $save = true;
          if ($recipient=="") { $save = false; $recipient = false; }
          if ($name=="") { $save = false; $name = false; }
          if ($text=="") { $save = false; $text = false; }

          if ($save) {
            $mail = array_unique(explode(',',$recipient));
            $error_recipient = '';
            foreach ($mail as $key => $value) {
			  $value = trim($value);
              if (!$dblink->get_row("SELECT id FROM memht_utenti WHERE user='$value'")) {
                $error_recipient.= $value;
                $error_recipient.= '<br>';
              }
            }
            if ($error_recipient) {

?>
            <tr>
              <td colspan="3" class="forum_post" id="errorText" align="center"><b><?php echo _USERNOTFOUND_; ?>:</b></td>
            </tr>
            <tr>
              <td colspan="3" class="forum_post errorText" align="center"><?php echo $error_recipient ?></td>
            </tr>
<?php

            } else {
              foreach ($mail as $key => $value) {
                if ($row = $dblink->get_row("SELECT email,notify_pvtmsg FROM memht_utenti WHERE user='$value'")) {
                  if ($dblink->query("INSERT INTO memht_pvtmsg (id,recipient,author,name,text,date,smiles,new)
                            VALUES (null,'$value','".$userInfo['user']."','$name','$text',$tzNOW,'$smiles','1')")) {
                    //Send mail notification (added in 3.2)
                    if (intval($row['notify_pvtmsg'])==1) {
                      $notemail = outCode($row['email']);
                      $subject = "You have new messages on ".$siteConfig['site_name'];
                      $msg = "You have new messages on ".$siteConfig['site_name']."!\n\n";
                      $msg .= "Link: ".$siteConfig['site_url']."/index.php?page=pvtmsg\n\n";
                      $msg .= "You're receiving this email because you have chosen to receive the notification of your new messages on ".$siteConfig['site_name'].". If you don't want to receive it anymore you can change your account settings from here:\n".$siteConfig['site_url']."/index.php?page=users";

					  sendEmail($notemail,$subject,$msg,$siteConfig['site_name'],$siteConfig['webmaster_mail'],0);
                    }
                  } else {

?>
            <tr>
              <td colspan="3" class="forum_post" id="errorText" align="center"><b><?php echo _SENDINGERROR_; ?></b></td>
            </tr>
<?php

                  }
                }
              }

?>
            <tr>
              <td colspan="3" class="forum_post" align="center"><b><?php echo _MESSAGESENT_; ?></b></td>
            </tr>
            <meta http-equiv="refresh" content="0;URL=<?php echo $siteConfig['site_url']?>/index.php?page=pvtmsg">
<?php

            }
          } else {

?>
            <tr>
              <td colspan="3" class="forum_post" id="errorText" align="center"><b><?php echo _FIELDINVALID_; ?></b></td>
            </tr>
            <tr>
              <td colspan="3" class="forum_post errorText" align="center">
<?php

            if ($recipient == false) {
              echo _RECIPIENT_;
            }
            if ($name == false AND $recipient == false) {
              echo ", "._TITLE_;
            } else if ($name == false) {
              echo _TITLE_;
            }
            if ($text == false AND $recipient == false) {
              echo ", "._MESSAGE_;
            } else if ($text == false AND $name == false) {
              echo ", "._MESSAGE_;
            } else if ($text == false) {
              echo _MESSAGE_;
            }

?>
              </td>
            </tr>
<?php

          }
        }
        if (isset($_POST['Preview'])) {
          $recipient = outCode($recipient);
          $name = outCode($name,0);
          $otext = outCode($text,0);
          $text = bb2html($otext,0);
          if ($smiles==1) { $text = bb2smile($text); }

?>
            <tr>
              <td colspan="3" class="forum_post_title"><b><?php echo _NEWMESSAGE_; ?></b>: <?php echo $name ?></td>
            </tr>
            <tr>
              <td colspan="3" class="forum_spacer">&nbsp;</td>
            </tr>
            <tr>
              <td colspan="3" class="forum_post"><?php echo $text ?></td>
            </tr>
            <tr>
              <td colspan="3" class="forum_spacer">&nbsp;</td>
            </tr>
            <tr>
              <td width="20%" class="forum_post" valign="top"><b><?php echo _RECIPIENT_; ?></b> <img src="images/info.gif" alt="Info" title="header=[<?php echo _INFORMATIONS_; ?>] body=[<?php echo _INFO_EXPLAIN_MULTI_RECIPIENTS_; ?>]"></td>
              <td width="80%" class="forum_post" valign="top"><input type="text" name="recipient" value="<?php echo $recipient ?>" style="width:40%;" maxlength="255"> <input type='button' value='<?php echo _FIND_; ?>' onClick="openPopup('inc/popwin/finduser.php?form=pvtmsg&element=recipient&add=true&username='+document.forms['pvtmsg'].elements['recipient'].value+'','350','400');"></td>
            </tr>
            <tr>
              <td class="forum_post" valign="top"><b><?php echo _TITLE_; ?></b></td>
              <td width="80%" class="forum_post" valign="top"><input type="text" name="name" value="<?php echo $name ?>" style="width:95%;" maxlength="255"></td>
            </tr>
            <tr>
              <td class="forum_post" valign="top"><b><?php echo _MESSAGE_; ?></b>
                <div align="center"><?php echo smileBox("pvtmsg","text"); ?></div>
              </td>
              <td width="80%" class="forum_post" valign="top"><?php echo bbcodeToolbar("pvtmsg","text"); ?> <textarea name="text" cols="63" rows="12" style="width:95%; height:200px;"><?php echo $otext ?></textarea></td>
            </tr>
            <tr>
              <td class="forum_post"><b><?php echo _SMILES_; ?></b> <img src="images/info.gif" alt="Info" title="header=[<?php echo _INFORMATIONS_; ?>] body=[<?php echo _SETOFFWHENPOSTINGCODE_; ?>]"></td>
              <td class="forum_post"><input name="smiles" type="radio" value="1"<?php if ($smiles==1) { echo 'checked="checked"'; }?> ><?php echo _ON2_; ?> <input name="smiles" type="radio" value="0"<?php if ($smiles==0) { echo 'checked="checked"'; }?> ><?php echo _OFF_; ?></td>
            </tr>
            <tr>
              <td colspan="3" class="forum_post" align="center" valign="top"><input type="submit" name="Preview" value="<?php echo _PREVIEW_; ?>"> <input type="submit" name="Send" value="<?php echo _SEND_; ?>"></td>
            </tr>
            <tr>
              <td colspan="3" class="forum_spacer">&nbsp;<input type="hidden" name="ok" value="true"></td>
            </tr>
<?php

        }
      }

?>
          </table>
        </td>
      </tr>
    </table>
  </form>
<?php

}

function viewMessage($id) {
  global $dblink,$siteConfig,$userid,$userInfo;

  $id = intval($id);

  if ($row = $dblink->get_row("SELECT p.*,DATE_FORMAT(p.date, '".$siteConfig['timestamp']."') as date,u.id AS uid FROM memht_pvtmsg AS p JOIN memht_utenti AS u ON p.author=u.user WHERE p.id=$id")) {
    $pm_recipient = outCode($row['recipient']);
    if (strtolower($pm_recipient)==strtolower($userInfo['user']) AND isUser($userid)) {
      $pm_author = outCode($row['author']);
      $pm_name = outCode($row['name'],0);
      $pm_text = bb2html(outCode($row['text'],0),0);
      $pm_date = $row['date'];
      $pm_smiles = intval($row['smiles']);
	  $uid = intval($row['uid']);

      if ($pm_smiles ==1) { $pm_text = bb2smile($pm_text); }

      $dblink->query("UPDATE memht_pvtmsg SET new=0 WHERE id='$id'");

      $userow = $dblink->get_row("SELECT * FROM memht_utenti WHERE id=$uid");
      $user_avatar = outCode($userow['avatar']);
      $user_title_enabled = intval($userow['title_enabled']);
      $user_title_text = outCode($userow['title_text']);

      $authorinfo = "<a href='index.php?page=users&op=userInfo&uid=$uid' title='$pm_author'><b>$pm_author</b></a>";
      if ($user_title_enabled==1) { $authorinfo .= "<br><span class='small'>$user_title_text</span>"; }
      if ($user_avatar!="") { $authorinfo .= "<br><img src='images/avatar/$user_avatar' alt='"._AVATAR_."'>"; }

?>
  <div align="right" style="margin:4px 0;"><a href="index.php?page=pvtmsg&op=reply&id=<?php echo $id ?>" title="<?php echo _REPLY_; ?>"><img src="templates/<?php echo $siteConfig['template'] ?>/images/forum/buttons/reply.gif" border="0" alt="<?php echo _REPLY_; ?>"></a> <a href="index.php?page=pvtmsg&op=newMessage" title="<?php echo _NEWMESSAGE_; ?>"><img src="templates/<?php echo $siteConfig['template'] ?>/images/forum/buttons/newmessage.gif" border="0" alt="<?php echo _NEWMESSAGE_; ?>"></a></div>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td>
          <table width="100%" border="0" cellspacing="1" cellpadding="2">
            <tr>
              <td colspan="3" class="forum_post_title"><b><?php echo $pm_name ?></b></td>
            </tr>
            <tr>
              <td class="forum_legend"><b><?php echo _AUTHOR_; ?></b></td>
              <td colspan="2" class="forum_legend"><b><?php echo _TEXT_; ?></b></td>
            </tr>
            <tr>
              <td width="20%" class="forum_post" valign="top"><?php echo $authorinfo ?></td>
              <td width="80%" class="forum_post" valign="top">
                <table width="100%" cellspacing="0" cellpadding="0">
                  <tr>
                    <td class="forum_post_minititle"><?php echo _DATE_; ?>: <?php echo $pm_date ?></td>
                  </tr>
                  <tr>
                    <td colspan="2" style="padding: 15px 0;"><?php echo $pm_text ?></td>
                  </tr>
                </table>
              </td>
            </tr>
            <tr>
              <td colspan="3" class="forum_post" align="right"><a href='index.php?page=pvtmsg&op=reply&id=<?php echo $id; ?>&quote=true' title='<?php echo _QUOTE_; ?>' rel='nofollow'><img src='templates/<?php echo $siteConfig['template']; ?>/images/forum/buttons/quote.gif' border='0' alt='<?php echo _QUOTE_; ?>'> <a href="index.php?page=pvtmsg&op=delete&id=<?php echo $id ?>" title="<?php echo _DELETE_; ?>"><img src="templates/<?php echo $siteConfig['template'] ?>/images/forum/buttons/delete.gif" border="0" alt="<?php echo _DELETE_; ?>"></a></td>
            </tr>
            <tr>
              <td colspan="3" class="forum_spacer">&nbsp;</td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
<?php

    } else {

?>
    <div align="center" id="errorText"><b><?php echo _ACCESSDENIED_; ?></b></div>
<?php

    }
  } else {

?>
    <div align="center" id="errorText"><b><?php echo _MEXNOTEXIST_; ?></b></div>
<?php

  }
}

function viewOutboxMessage($id) {
  global $dblink,$siteConfig,$userid,$userInfo;

  $id = intval($id);

   if ($row = $dblink->get_row("SELECT p.*,DATE_FORMAT(p.date, '".$siteConfig['timestamp']."') as date,u.id AS uid FROM memht_pvtmsg AS p JOIN memht_utenti AS u ON p.author=u.user WHERE p.id=$id")) {
    $pm_author = outCode($row['author']);
    if (strtolower($pm_author)==strtolower($userInfo['user']) AND isUser($userid)) {
      $pm_name = outCode($row['name'],0);
      $pm_text = bb2html(outCode($row['text'],0),0);
      $pm_date = $row['date'];
      $pm_smiles = intval($row['smiles']);
	  $uid = intval($row['uid']);
	 
      if ($pm_smiles ==1) { $pm_text = bb2smile($pm_text); }

      $userow = $dblink->get_row("SELECT * FROM memht_utenti WHERE id=$uid");
      $user_avatar = outCode($userow['avatar']);
      $user_title_enabled = intval($userow['title_enabled']);
      $user_title_text = outCode($userow['title_text']);

      $authorinfo = "<a href='index.php?page=users&op=userInfo&uid=$uid' title='$pm_author'><b>$pm_author</b></a>";
      if ($user_title_enabled==1) { $authorinfo .= "<br><span class='small'>$user_title_text</span>"; }
      if ($user_avatar!="") { $authorinfo .= "<br><img src='images/avatar/$user_avatar' alt='Avatar'>"; }

?>
  <div align="right" style="margin:4px 0;"><a href="index.php?page=pvtmsg&op=newMessage" title="<?php echo _NEWMESSAGE_; ?>"><img src="templates/<?php echo $siteConfig['template'] ?>/images/forum/buttons/newmessage.gif" border="0" alt="<?php echo _NEWMESSAGE_; ?>"></a></div>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td>
          <table width="100%" border="0" cellspacing="1" cellpadding="2">
            <tr>
              <td colspan="3" class="forum_post_title"><b><?php echo $pm_name ?></b></td>
            </tr>
            <tr>
              <td class="forum_legend"><b><?php echo _AUTHOR_; ?></b></td>
              <td colspan="2" class="forum_legend"><b><?php echo _TEXT_; ?></b></td>
            </tr>
            <tr>
              <td width="20%" class="forum_post" valign="top"><?php echo $authorinfo ?></td>
              <td width="80%" class="forum_post" valign="top">
                <table width="100%" cellspacing="0" cellpadding="0">
                  <tr>
                    <td class="forum_post_minititle"><?php echo _DATE_; ?>: <?php echo $pm_date ?></td>
                  </tr>
                  <tr>
                    <td colspan="2" style="padding: 15px 0;"><?php echo $pm_text ?></td>
                  </tr>
                </table>
              </td>
            </tr>
            <tr>
              <td colspan="3" class="forum_spacer">&nbsp;</td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
<?php

    } else {

?>
    <div align="center" id="errorText"><b><?php echo _ACCESSDENIED_; ?></b></div>
<?php

    }
  } else {

?>
    <div align="center" id="errorText"><b><?php echo _MEXNOTEXIST_; ?></b></div>
<?php

  }
}

function replyMessage($id,$recipient,$name,$text,$smiles,$operation,$quote,$ok=false) {
  global $dblink,$siteConfig,$userid,$userInfo,$tzNOW;

  $row = $dblink->get_row("SELECT * FROM memht_pvtmsg WHERE id=$id AND recipient='".$userInfo['user']."'");
  $pm_author = outCode($row['author']);
  $pm_name = outCode($row['name'],0);
  $pm_text = outCode($row['text'],0);
  
	if ($quote) {
		$quotetxt = "[quote][b]$pm_author:[/b]\n".$pm_text."[/quote]";
	} else { $quotetxt = ""; }

?>
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td>
        <form name="pvtmsg" method="post" action="index.php?page=pvtmsg&op=reply&id=<?php echo $id ?>">
          <table width="100%" border="0" cellspacing="1" cellpadding="2">
<?php

  if (!$ok) {

?>
            <tr>
              <td colspan="3" class="forum_post_title"><b><?php echo _REPLY_; ?></b></td>
            </tr>
            <tr>
              <td colspan="3" class="forum_spacer">&nbsp;</td>
            </tr>
            <tr>
              <td width="20%" class="forum_post" valign="top"><b><?php echo _RECIPIENT_; ?></b> <img src="images/info.gif" alt="Info" title="header=[<?php echo _INFORMATIONS_; ?>] body=[<?php echo _INFO_EXPLAIN_MULTI_RECIPIENTS_; ?>]"></td>
              <td width="80%" class="forum_post" valign="top"><input type="text" name="recipient" value="<?php echo $pm_author ?>" style="width:40%;" maxlength="255" onKeyUp="ajax_showOptions(this,'getusers',event)"></td>
            </tr>
            <tr>
              <td class="forum_post" valign="top"><b><?php echo _TITLE_; ?></b></td>
              <td width="80%" class="forum_post" valign="top"><input type="text" name="name" value="Re: <?php echo $pm_name ?>" style="width:95%;" maxlength="255"></td>
            </tr>
            <tr>
              <td class="forum_post" valign="top"><b><?php echo _MESSAGE_; ?></b><div align="center"><?php echo smileBox("pvtmsg","text"); ?></div></td>
              <td width="80%" class="forum_post" valign="top"><?php echo bbcodeToolbar("pvtmsg","text"); ?> <textarea name="text" cols="63" rows="12" style="width:95%; height:200px;"><?php echo $quotetxt; ?></textarea></td>
            </tr>
            <tr>
              <td class="forum_post"><b><?php echo _SMILES_; ?></b> <img src="images/info.gif" alt="Info" title="header=[<?php echo _INFORMATIONS_; ?>] body=[<?php echo _SETOFFWHENPOSTINGCODE_; ?>]"></td>
              <td class="forum_post"><input name="smiles" type="radio" value="1" checked="checked"><?php echo _ON2_; ?> <input name="smiles" type="radio" value="0"><?php echo _OFF_; ?></td>
            </tr>
            <tr>
              <td colspan="3" class="forum_post" align="center" valign="top"><input type="submit" name="Preview" value="<?php echo _PREVIEW_; ?>"> <input type="submit" name="Send" value="<?php echo _SEND_; ?>"></td>
            </tr>
            <tr>
              <td colspan="3" class="forum_spacer">&nbsp;<input type="hidden" name="ok" value="true"></td>
            </tr>
<?php

  } else {
      if (isset($_POST['Send'])) {
        $save = true;
        if ($recipient=="") { $save = false; $recipient = false; }
        if ($name=="") { $save = false; $name = false; }
        if ($text=="") { $save = false; $text = false; }
        if ($save) {
          $mail = array_unique(explode(',',$recipient));
          $error_recipient = '';
          foreach ($mail as $key => $value) {
		  	$value = trim($value);
            if (!$dblink->get_row("SELECT id FROM memht_utenti WHERE user='$value'")) {
              $error_recipient.= $value;
              $error_recipient.= '<br>';
            }
          }
          if ($error_recipient) {

?>
            <tr>
              <td colspan="3" class="forum_post" id="errorText" align="center"><b><?php echo _USERNOTFOUND_; ?>:</b></td>
            </tr>
            <tr>
              <td colspan="3" class="forum_post errorText" align="center"><?php echo $error_recipient ?></td>
            </tr>
<?php

          } else {
            foreach ($mail as $key => $value) {
              if ($row = $dblink->get_row("SELECT email,notify_pvtmsg FROM memht_utenti WHERE user='$value'")) {
                if ($dblink->query("INSERT INTO memht_pvtmsg (id,recipient,author,name,text,date,smiles,new)
                          VALUES (null,'$value','".$userInfo['user']."','$name','$text',$tzNOW,'$smiles','1')")) {
                  //Send mail notification (added in 3.2)
                  if (intval($row['notify_pvtmsg'])==1) {
                    $notemail = outCode($row['email']);
                    $subject = "You have new messages on ".$siteConfig['site_name'];
                    $msg = "You have new messages on ".$siteConfig['site_name']."!\n\n";
                    $msg .= "Link: ".$siteConfig['site_url']."/index.php?page=pvtmsg\n\n";
                    $msg .= "You're receiving this email because you have chosen to receive the notification of your new messages on ".$siteConfig['site_name'].". If you don't want to receive it anymore you can change your account settings from here:\n".$siteConfig['site_url']."/index.php?page=users";

					sendEmail($notemail,$subject,$msg,$siteConfig['site_name'],$siteConfig['webmaster_mail'],0);
                  }
                } else {

?>
            <tr>
              <td colspan="3" class="forum_post" id="errorText" align="center"><b><?php echo _SENDINGERROR_; ?></b></td>
            </tr>
<?php

                }
              }
            }

?>
            <tr>
              <td colspan="3" class="forum_post" align="center"><b><?php echo _MESSAGESENT_; ?></b></td>
            </tr>
            <meta http-equiv="refresh" content="0;URL=<?php echo $siteConfig['site_url'] ?>/index.php?page=pvtmsg">
<?php

          }
        } else {

?>
            <tr>
              <td colspan="3" class="forum_post" id="errorText" align="center"><b><?php echo _FIELDINVALID_; ?></b></td>
            </tr>
            <tr>
              <td colspan="3" class="forum_post errorText" align="center">
<?php

          if ($recipient == false) {
            echo _RECIPIENT_;
          }
          if ($name == false AND $recipient == false) {
            echo ", "._TITLE_;
          } else if ($name == false) {
            echo _TITLE_;
          }
          if ($text == false AND $recipient == false) {
            echo ", "._MESSAGE_;
          } else if ($text == false AND $name == false) {
            echo ", "._MESSAGE_;
          } else if ($text == false) {
            echo _MESSAGE_;
          }

?>
              </td>
            </tr>
<?php

        }
      }
      if (isset($_POST['Preview'])) {
        $recipient = outCode($recipient);
        $name = outCode($name,0);
        $otext = outCode($text,0);
        $text = bb2html($otext,0);
        if ($smiles==1) { $text = bb2smile($text); }

?>
            <tr>
              <td colspan="3" class="forum_post_title"><b><?php echo _REPLY_; ?></b>: <?php echo $name ?></td>
            </tr>
            <tr>
              <td colspan="3" class="forum_spacer">&nbsp;</td>
            </tr>
            <tr>
              <td colspan="3" class="forum_post"><?php echo $text ?></td>
            </tr>
            <tr>
              <td colspan="3" class="forum_spacer">&nbsp;</td>
            </tr>
            <tr>
              <td width="20%" class="forum_post" valign="top"><b><?php echo _RECIPIENT_; ?></b></td>
              <td width="80%" class="forum_post" valign="top"><input type="text" name="recipient" value="<?php echo $recipient ?>" style="width:40%;" maxlength="255" onKeyUp="ajax_showOptions(this,'getusers',event)"></td>
            </tr>
            <tr>
              <td class="forum_post" valign="top"><b><?php echo _TITLE_; ?></b></td><td width="80%" class="forum_post" valign="top"><input type="text" name="name" value="<?php echo $name ?>" style="width:95%;" maxlength="255"></td>
            </tr>
            <tr>
              <td class="forum_post" valign="top"><b><?php echo _MESSAGE_; ?></b><div align="center"><?php echo smileBox("pvtmsg","text"); ?></div></td>
              <td width="80%" class="forum_post" valign="top"><?php echo bbcodeToolbar("pvtmsg","text"); ?> <textarea name="text" cols="63" rows="12" style="width:95%; height:200px;"><?php echo $otext ?></textarea></td>
            </tr>
            <tr>
              <td class="forum_post"><b><?php echo _SMILES_; ?></b> <img src="images/info.gif" alt="Info" title="header=[<?php echo _INFORMATIONS_; ?>] body=[<?php echo _SETOFFWHENPOSTINGCODE_; ?>]"></td>
              <td class="forum_post"><input name="smiles" type="radio" value="1"<?php if ($smiles==1) { echo 'checked="checked"'; }?> ><?php echo _ON2_; ?> <input name="smiles" type="radio" value="0"<?php if ($smiles==0) { echo 'checked="checked"'; }?> ><?php echo _OFF_; ?></td>
            </tr>
            <tr>
              <td colspan="3" class="forum_post" align="center" valign="top"><input type="submit" name="Preview" value="<?php echo _PREVIEW_; ?>"> <input type="submit" name="Send" value="<?php echo _SEND_; ?>"></td>
            </tr>
            <tr>
              <td colspan="3" class="forum_spacer">&nbsp;<input type="hidden" name="ok" value="true"></td>
            </tr>
<?php

      }
    }

?>
          </table>
        </form>
      </tr>
    </table>
<?php

}

require_once("pages/forum/inc_functions.php");

  openTable();

  global $dblink,$siteConfig,$userid;
  if (isUser($userid)) {
  	
	echo "<div class='box' align='center'><a href='index.php?page=pvtmsg' title="._INBOX_."><img src='admin/icons/newsletter.png' border='0' title='"._INBOX_."' alt='"._INBOX_."' align='top'> "._INBOX_."</a> - <a href='index.php?page=pvtmsg&op=viewOutbox' title='"._SENT_MESSAGES_."'><img src='admin/icons/sent.png' border='0' title='"._SENT_MESSAGES_."' alt='"._SENT_MESSAGES_."' align='top'> "._SENT_MESSAGES_."</a></div>\n";
	
    switch($op) {
      case "newMessage":
        newMessage($recipient,$name,$text,$smiles,$operation,$ok);
      break;
	  
	  case "viewOutbox":
	  	viewOutbox($ofsbgn,$ofsppg,$pg);
	  break;

      case "viewMessage":
        viewMessage($id);
      break;
	  
	  case "viewOutboxMessage":
	  	viewOutboxMessage($id);
	  break;

      case "reply":
        replyMessage($id,$recipient,$name,$text,$smiles,$operation,$quote,$ok);
      break;

      case "delete":
        if ($ok) {
          $dblink->query("DELETE FROM memht_pvtmsg WHERE id=$id");

?>
          <meta http-equiv="refresh" content="0;URL=<?php echo $siteConfig['site_url'] ?>/index.php?page=pvtmsg">
<?php

        } else {

?>
          <div align="center">
            <b><?php echo _SUREDELETEMESSAGE_; ?></b><br>
            <a href="index.php?page=pvtmsg&op=delete&id=<?php echo $id ?>&ok=true" title="<?php echo _YES_; ?>"><?php echo _YES_; ?></a> - <a href="index.php?page=pvtmsg&op=viewMessage&id=<?php echo $id ?>" title="<?php echo _NO_; ?>"><?php echo _NO_; ?></a>
          </div>
<?php

        }
      break;

      case "deleteSelected":
        if (isset($_POST['deletenewpm'])) {
          foreach ($_POST['deletenewpm'] as $value) {
            $dblink->query("DELETE FROM memht_pvtmsg WHERE id=$value");
          }
        }
        if (isset($_POST['deletepm'])) {
          foreach ($_POST['deletepm'] as $value) {
            $dblink->query("DELETE FROM memht_pvtmsg WHERE id=$value");
          }
        }
?>
          <meta http-equiv="refresh" content="0;URL=<?php echo $siteConfig['site_url'] ?>/index.php?page=pvtmsg">
<?php
      break;

      case "reset":
        if ($ok) {
          $dblink->query("DELETE FROM memht_pvtmsg WHERE recipient='$user'");
?>
          <meta http-equiv="refresh" content="0;URL=<?php echo $siteConfig['site_url'] ?>/index.php?page=pvtmsg">
<?php
        } else {

?>
          <div align='center'>
            <b><?php echo _DELETEALLMESSAGES_; ?>?</b><br>
            <a href="index.php?page=pvtmsg&op=reset&ok=true" title="<?php echo _YES_; ?>"><?php echo _YES_; ?></a> - <a href="index.php?page=pvtmsg" title="<?php echo _NO_; ?>"><?php echo _NO_; ?></a>
          </div>
<?php

        }
      break;

      case "viewMain":
      default:
        viewMain($ofsbgn,$ofsppg,$pg);
      break;
    }
  } else {

?>
    <div align="center" id="errorText">
      <b><?php echo _ACCESSDENIED_; ?></b>
    </div>
    <div align="center" class="box">
      <?php echo _YOUHAVENOPERM_; ?> <?php echo _TOACCESSTHISPAGE_; ?>!<br><?php echo _DOTHE_; ?> <a href="index.php?page=users" title="<?php echo _LOGIN_; ?>"><b><?php echo _LOGIN_; ?></b></a> <?php echo _OR_; ?> <a href="index.php?page=users&op=register" title="<?php echo _REGISTER_; ?>"><b><?php echo _REGISTER_; ?></b></a> <?php echo _FORFREE_; ?>
    </div>
<?php

    @session_start();
    $_SESSION['redirect_url'] = "index.php?page=pvtmsg";
    $_SESSION['redirect_age'] = time();
  }

  closeTable();

?>
<script type="text/javascript">
  function doconfirm() {
    return confirm("<?php echo _SUREDELETE_; ?>");
  }
  function selectAll(id) {
    for (var i=0;i<document.pvtmsg.elements.length;i++) {
      var element = document.pvtmsg.elements[i];
        if (element.name == id+'[]') {
          element.checked = true;
        }
    }
  }
  function selectNone(id) {
    for (var i=0;i<document.pvtmsg.elements.length;i++) {
      var element = document.pvtmsg.elements[i];
        if (element.name == id+'[]') {
          element.checked = false;
        }
    }
  }
</script>