<?php

  $show = trim($_REQUEST["show"]);
  $pmid = intval($_REQUEST["pmid"]);

  include ("$www_dir/classes/pm.class.php");
  $SBBPm = new SBBPm;
  
  
  
  $PM_INBOX = lang("pm_inbox");
  $PM_OUTBOX = lang("pm_outbox");
  $PM_SENT = lang("pm_sent");
  
  $LINK_PM = ($board_config->seo ? "$main_path/sbb-pm.html" : "$main_script?todo=showpms");
  $LINK_PM_OUTBOX = ($board_config->seo ? "$main_path/sbb-pm.html?show=outbox" : "$main_script?todo=showpms&amp;show=outbox");
  $LINK_PM_SENT = ($board_config->seo ? "$main_path/sbb-pm.html?show=sent" : "$main_script?todo=showpms&amp;show=sent");
  
  if (!$show && $todo == "showpms") $CLASS_SELIN = "sel";
  elseif ($show == "outbox") $CLASS_SELOUT = "sel";
  elseif ($show == "sent") $CLASS_SELSENT = "sel";

  $DESCR_SUBJECT = lang("subject");
  $DESCR_USER = lang($show ? "rcpt" : "author");
  $DESCR_SENTON = lang("sent_on");
  $DESCR_DELETE_SELECTED = lang("del_pms");
  $FORM_VAL_SHOW = $show;
  $DESCR_FROM = lang("pm_from");
  $DESCR_TO = lang("pm_to");
  $DESCR_MESSAGE = lang("message");
  $IMG_QUOTE = lang("cite_pm");
  $IMG_DEL = lang("del_pm");
  $IMG_PROFILE_TITLE = lang("btn_profile");
  $IMG_SENDMAIL_TITLE = lang("btn_email");
  $IMG_PM_TITLE = lang("btn_pm");
  $CONFIRM_DELPM = lang("confirm_delpm");
  $IMG_REPLY_TITLE = lang("answer_pm");
  
  
  if ($todo == "showpms") {

    $TEMPLATE_FLAGS["SHOWPMS"] = true;
    
    $pms = $SBBPm->getPMs($show);
    
    $NO_PMS = lang("no_private_messages");
    
    if ($SBBPm->count) {
      for ($i=0;$i<$SBBPm->count;$i++) {
        $pm = $pms[$i];
        $pmusrid = ($show ? $pm->to_uid : $pm->from_uid);
        $pmusr = forum_user($pmusrid);
        if (!$pmusr) {
          if ($show) $pmusrname = $pm->to_name;
          else $pmusrname = $pm->from_name;
          $pmusrid = 0;
        } else {
          $pmusrname = $pmusr->user;
        }
        $PMS[] = array("PM_ID" => $pm->id,
                       "PM_LINK" => ($board_config->seo ? "$main_path/sbb-showpm" . $pm->id . ".html" : "$main_script?todo=showpm&amp;pmid=" . $pm->id),
                       "PM_USER_LINK" => ($board_config->seo ? "$main_path/sbb-user" . $pmusrid . adddescr2link($pmusr->user) . ".html" : "$main_script?todo=showprofile&amp;uid=$pmusrid"),                     
                       "PM_USER_CLASS" => nick_class($pmusr->is_admin, $pmusr->is_mod),
                       "PM_SUBJECT" => $pm->subject,
                       "PM_SUBJECT_CLASS" => (!$show && !$pm->seen ? "b" : ""),
                       "PM_SENTON" => date($date_time_format,strtotime($pm->sent)), 
                       "PM_USERNAME" => $pmusrname,
                       "PM_USERID" => $pmusrid
                      );
      }
    }
  }
  
  if ($todo == "showpm") {
   
    $TEMPLATE_FLAGS["SHOWPM"] = true;
   
    $pm = $SBBPm->getPM($pmid);
    
    $NO_PM = lang("pm_not_found");
    
    if ($pm) {
      $TEMPLATE_FLAGS["PMFOUND"] = true;
      $from = forum_user($pm->from_uid);
      $to = forum_user($pm->to_uid);
      
      $SPM_SUBJECT = $pm->subject;
      $SPM_FROM_USER_LINK = ($board_config->seo ? "$main_path/sbb-user" . $pm->from_uid . adddescr2link($from->user) . ".html" : "$main_script?todo=showprofile&amp;uid=" . $pm->from_uid);
      $SPM_FROM_USERNAME = ($from ? $from->user : $pm->from_name);
      $SPM_FROM_USER_CLASS = nick_class($from->is_admin, $from->is_mod);
      $SPM_SENTON = date($date_time_format,strtotime($pm->sent));
      $SPM_TO_USER_LINK = ($board_config->seo ? "$main_path/sbb-user" . $pm->to_uid . adddescr2link($to->user) . ".html" : "$main_script?todo=showprofile&amp;uid=" . $pm->to_uid);
      $SPM_TO_USERNAME = ($to ? $to->user : $pm->to_name);
      $SPM_TO_USER_CLASS = nick_class($to->is_admin, $to->is_mod);
      $SPM_MESSAGE = bbencode($pm->message,true);
      $SENDERS_SIG = ($from->signature ? "<br><br>---<br>" . make_clickable(bbencode($from->signature,false)) : "");
      $LINK_PM_QUOTE = ($board_config->seo ? "$main_path/sbb-sendpm" . $from->id . ".html?quote=$pmid" : "$main_script?todo=sendpm&amp;uid=" . $from->id . "&amp;quote=$pmid");
      $SPM_ID = $pmid;
      $LINK_PM_SEND = ($board_config->seo ? "$main_path/sbb-sendpm" . $from->id . ".html" : "$main_script?todo=sendpm&amp;uid=" . $from->id);
      $LINK_PM_REPLY = ($board_config->seo ? "$main_path/sbb-sendpm" . $from->id . ".html?reply=$pmid" : "$main_script?todo=sendpm&amp;uid=" . $from->id . "&amp;reply=$pmid");
      $LINK_SENDMAIL = ($board_config->seo ? "$main_path/sbb-sendmail" . $from->id . ".html" : "$main_script?todo=sendmail&amp;uid=" . $from->id);
      $SPM_FROM_UID = intval($from->id);
      $SPM_TO_UID = intval($to->id);
      $SPM_MAIL2AUTHOR = $from->usermail && $board_config->en_user_mess ? true : false;
      
    }
  }
  
  
  $sbb_add2title .= " - " . he(lang("priv_messages"));
  

    /************
     * initiate template and assign variables
     ************/   
    $privatemessagesVARS = array('PM_INBOX' => $PM_INBOX,
                                'PM_OUTBOX' => $PM_OUTBOX,
                                'PM_SENT' => $PM_SENT,
                                'LINK_PM' => $LINK_PM,
                                'LINK_PM_OUTBOX' => $LINK_PM_OUTBOX,
                                'LINK_PM_SENT' => $LINK_PM_SENT,
                                'CLASS_SELIN' => $CLASS_SELIN,
                                'CLASS_SELOUT' => $CLASS_SELOUT,
                                'CLASS_SELSENT' => $CLASS_SELSENT,
                                'DESCR_SUBJECT' => $DESCR_SUBJECT,
                                'DESCR_USER' => $DESCR_USER,
                                'DESCR_SENTON' => $DESCR_SENTON,
                                'DESCR_FROM' => $DESCR_FROM,
                                'DESCR_TO' => $DESCR_TO,
                                'DESCR_MESSAGE' => $DESCR_MESSAGE,
                                'NO_PMS' => $NO_PMS,
                                'DESCR_DELETE_SELECTED' => $DESCR_DELETE_SELECTED,
                                'NO_PM' => $NO_PM,
                                'FORM_VAL_SHOW' => $FORM_VAL_SHOW,
                                'SPM_SUBJECT' => $SPM_SUBJECT,
                                'SPM_FROM_USER_LINK' => $SPM_FROM_USER_LINK,
                                'SPM_FROM_USERNAME' => $SPM_FROM_USERNAME,
                                'SPM_FROM_USER_CLASS' => $SPM_FROM_USER_CLASS,
                                'SPM_SENTON' => $SPM_SENTON,
                                'SPM_TO_USER_LINK' => $SPM_TO_USER_LINK,
                                'SPM_TO_USERNAME' => $SPM_TO_USERNAME,
                                'SPM_TO_USER_CLASS' => $SPM_TO_USER_CLASS,
                                'SPM_MESSAGE' => $SPM_MESSAGE,
                                'SENDERS_SIG' => $SENDERS_SIG,
                                'IMG_QUOTE' => $IMG_QUOTE,
                                'IMG_DEL' => $IMG_DEL,
                                'CONFIRM_DELPM' => $CONFIRM_DELPM,
                                'LINK_PM_QUOTE' => $LINK_PM_QUOTE,
                                'SPM_ID' => $SPM_ID,
                                'IMG_PROFILE_TITLE' => $IMG_PROFILE_TITLE,
                                'IMG_SENDMAIL_TITLE' => $IMG_SENDMAIL_TITLE,
                                'IMG_PM_TITLE' => $IMG_PM_TITLE,
                                'LINK_PM_SEND' => $LINK_PM_SEND,
                                'LINK_PM_REPLY' => $LINK_PM_REPLY,
                                'LINK_SENDMAIL' => $LINK_SENDMAIL,
                                'SPM_FROM_UID' => $SPM_FROM_UID,
                                'SPM_TO_UID' => $SPM_TO_UID,
                                'SPM_MAIL2AUTHOR' => $SPM_MAIL2AUTHOR,
                                'IMG_REPLY_TITLE' => $IMG_REPLY_TITLE,

                                'CONF' => $GLOBAL_TEMPLATE_VARS,
                                'FLAGS' => $TEMPLATE_FLAGS,
                                'PMS' => $PMS
                          );

      
    $QuickSkin = new QuickSkin("private_messages.html");
    $QuickSkin->assign($privatemessagesVARS);
    $content .= $QuickSkin->result();
    unset($QuickSkin);
      

?>