plugins/PrivateMessages.php
changeset 0 902822492a68
child 22 d0314575e2f0
equal deleted inserted replaced
-1:000000000000 0:902822492a68
       
     1 <?php
       
     2 /*
       
     3 Plugin Name: Private Message frontend
       
     4 Plugin URI: http://enano.homelinux.org/
       
     5 Description: Provides the page Special:PrivateMessages, which is used to manage private message functions. Also handles buddy lists.
       
     6 Author: Dan Fuhry
       
     7 Version: 1.0
       
     8 Author URI: http://enano.homelinux.org/
       
     9 */
       
    10 
       
    11 /*
       
    12  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
       
    13  * Version 1.0 release candidate 2
       
    14  * Copyright (C) 2006-2007 Dan Fuhry
       
    15  *
       
    16  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
       
    17  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
       
    18  *
       
    19  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
       
    20  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
       
    21  */
       
    22  
       
    23 global $db, $session, $paths, $template, $plugins; // Common objects
       
    24 
       
    25 $plugins->attachHook('base_classes_initted', '
       
    26   global $paths;
       
    27     $paths->add_page(Array(
       
    28       \'name\'=>\'Private Messages\',
       
    29       \'urlname\'=>\'PrivateMessages\',
       
    30       \'namespace\'=>\'Special\',
       
    31       \'special\'=>0,\'visible\'=>1,\'comments_on\'=>0,\'protected\'=>1,\'delvotes\'=>0,\'delvote_ips\'=>\'\',
       
    32       ));
       
    33     ');
       
    34 
       
    35 function page_Special_PrivateMessages()
       
    36 {
       
    37   global $db, $session, $paths, $template, $plugins; // Common objects
       
    38   if(!$session->user_logged_in) die_friendly('Access denied', '<p>You need to <a href="'.makeUrlNS('Special', 'Login/'.$paths->page).'">log in</a> to view your private messages.</p>');
       
    39   $argv = Array();
       
    40   $argv[] = $paths->getParam(0);
       
    41   $argv[] = $paths->getParam(1);
       
    42   $argv[] = $paths->getParam(2);
       
    43   if(!$argv[0]) $argv[0] = 'InVaLiD';
       
    44   switch($argv[0])
       
    45   {
       
    46     default:
       
    47       header('Location: '.makeUrlNS('Special', 'PrivateMessages/Folder/Inbox'));
       
    48       break;
       
    49     case 'View':
       
    50       $id = $argv[1];
       
    51       if(!preg_match('#^([0-9]+)$#', $id)) die_friendly('Message error', '<p>Invalid message ID</p>');
       
    52       $q = $db->sql_query('SELECT p.message_from, p.message_to, p.subject, p.message_text, p.date, p.folder_name, u.signature FROM '.table_prefix.'privmsgs AS p LEFT JOIN '.table_prefix.'users AS u ON (p.message_from=u.username) WHERE message_id='.$id.'');
       
    53       if(!$q) $db->_die('The message data could not be selected.');
       
    54       $r = $db->fetchrow();
       
    55       $db->free_result();
       
    56       if( ($r['message_to'] != $session->username && $r['message_from'] != $session->username ) || $r['folder_name']=='drafts' ) die_friendly('Access denied', '<p>You are not authorized to view this message.</p>');
       
    57       if($r['message_to'] == $session->username)
       
    58       {
       
    59         $q = $db->sql_query('UPDATE '.table_prefix.'privmsgs SET message_read=1 WHERE message_id='.$id.'');
       
    60         $db->free_result();
       
    61         if(!$q) $db->_die('Could not mark message as read');
       
    62       }
       
    63       $template->header();
       
    64       userprefs_show_menu();
       
    65       ?>
       
    66         <br />
       
    67         <div class="tblholder"><table border="0" width="100%" cellspacing="1" cellpadding="4">
       
    68           <tr><th colspan="2">Private message from <?php echo $r['message_from']; ?></th></tr>
       
    69           <tr><td class="row1">Subject:</td><td class="row1"><?php echo $r['subject']; ?></td></tr>
       
    70           <tr><td class="row2">Date:</td><td class="row2"><?php echo date('M j, Y G:i', $r['date']); ?></td></tr>
       
    71           <tr><td class="row1">Message:</td><td class="row1"><?php echo RenderMan::render($r['message_text']);
       
    72           if($r['signature'] != '')
       
    73           {
       
    74             echo '<hr style="margin-left: 1em; width: 200px;" />';
       
    75             echo RenderMan::render($r['signature']);
       
    76           }
       
    77           ?></td></tr>
       
    78           <tr><td colspan="2" class="row3"><a href="<?php echo makeUrlNS('Special', 'PrivateMessages/Compose/ReplyTo/'.$id); ?>">Send reply</a>  |  <a href="<?php echo makeUrlNS('Special', 'PrivateMessages/Delete/'.$id); ?>">Delete message</a>  |  <?php if($r['folder_name'] != 'archive') { ?><a href="<?php echo makeUrlNS('Special', 'PrivateMessages/Move/'.$id.'/Archive'); ?>">Archive message</a>  |  <?php } ?><a href="<?php echo makeUrlNS('Special', 'PrivateMessages/Folder/Inbox') ?>">Return to inbox</a></td></tr>
       
    79         </table></div>
       
    80       <?php
       
    81       $template->footer();              
       
    82       break;
       
    83     case 'Move':
       
    84       $id = $argv[1];
       
    85       if(!preg_match('#^([0-9]+)$#', $id)) die_friendly('Message error', '<p>Invalid message ID</p>');
       
    86       $q = $db->sql_query('SELECT message_to FROM '.table_prefix.'privmsgs WHERE message_id='.$id.'');
       
    87       if(!$q) $db->_die('The message data could not be selected.');
       
    88       $r = $db->fetchrow();
       
    89       $db->free_result();
       
    90       if($r['message_to'] != $session->username) die_friendly('Access denied', '<p>You are not authorized to alter this message.</p>');
       
    91       $fname = $argv[2];
       
    92       if(!$fname || ( $fname != 'Inbox' && $fname != 'Outbox' && $fname != 'Sent' && $fname != 'Drafts' && $fname != 'Archive' ) ) die_friendly('Invalid request', '<p>The folder name "'.$fname.'" is invalid.</p>');
       
    93       $q = $db->sql_query('UPDATE '.table_prefix.'privmsgs SET folder_name=\''.strtolower($fname).'\' WHERE message_id='.$id.';');
       
    94       $db->free_result();
       
    95       if(!$q) $db->_die('The message was not successfully moved.');
       
    96       die_friendly('Message status', '<p>Your message has been moved to the folder "'.$fname.'".</p><p><a href="'.makeUrlNS('Special', 'PrivateMessages/Folder/Inbox').'">Return to inbox</a></p>');
       
    97       break;
       
    98     case 'Delete':
       
    99       $id = $argv[1];
       
   100       if(!preg_match('#^([0-9]+)$#', $id)) die_friendly('Message error', '<p>Invalid message ID</p>');
       
   101       $q = $db->sql_query('SELECT message_to FROM '.table_prefix.'privmsgs WHERE message_id='.$id.'');
       
   102       if(!$q) $db->_die('The message data could not be selected.');
       
   103       $r = $db->fetchrow();
       
   104       if($r['message_to'] != $session->username) die_friendly('Access denied', '<p>You are not authorized to delete this message.</p>');
       
   105       $q = $db->sql_query('DELETE FROM '.table_prefix.'privmsgs WHERE message_id='.$id.';');
       
   106       if(!$q) $db->_die('The message was not successfully deleted.');
       
   107       $db->free_result();
       
   108       die_friendly('Message status', '<p>The message has been deleted.</p><p><a href="'.makeUrlNS('Special', 'PrivateMessages/Folder/Inbox').'">Return to inbox</a></p>');
       
   109       break;
       
   110     case 'Compose':
       
   111       if($argv[1]=='Send' && isset($_POST['_send']))
       
   112       {
       
   113         // Check each POST DATA parameter...
       
   114         if(!isset($_POST['to']) || ( isset($_POST['to']) && $_POST['to'] == '')) die_friendly('Sending of message failed', '<p>Please enter the username to which you want to send your message.</p>');
       
   115         if(!isset($_POST['subject']) || ( isset($_POST['subject']) && $_POST['subject'] == '')) die_friendly('Sending of message failed', '<p>Please enter a subject for your message.</p>');
       
   116         if(!isset($_POST['message']) || ( isset($_POST['message']) && $_POST['message'] == '')) die_friendly('Sending of message failed', '<p>Please enter a message to send.</p>');
       
   117         $namelist = $_POST['to'];
       
   118         $namelist = str_replace(', ', ',', $namelist);
       
   119         $namelist = explode(',', $namelist);
       
   120         foreach($namelist as $n) { $n = $db->escape($n); }
       
   121         $subject = RenderMan::preprocess_text($_POST['subject']);
       
   122         $message = RenderMan::preprocess_text($_POST['message']);
       
   123         $base_query = 'INSERT INTO '.table_prefix.'privmsgs(message_from,message_to,date,subject,message_text,folder_name,message_read) VALUES';
       
   124         foreach($namelist as $n)
       
   125         {
       
   126           $base_query .= '(\''.$session->username.'\', \''.$n.'\', '.time().', \''.$subject.'\', \''.$message.'\', \'inbox\', 0),';
       
   127         }
       
   128         $base_query = substr($base_query, 0, strlen($base_query)-1) . ';';
       
   129         $result = $db->sql_query($base_query);
       
   130         $db->free_result();
       
   131         if(!$result) $db->_die('The message could not be sent.');
       
   132         else die_friendly('Message status', '<p>Your message has been sent. You may edit the message if you wish; one copy for each recipient will be in your outbox until each recipient has read it. Return to your <a href="'.makeUrlNS('Special', 'PrivateMessages/Folder/Inbox').'">inbox</a>.</p>');
       
   133         return;
       
   134       } elseif($argv[1]=='Send' && isset($_POST['_savedraft'])) {
       
   135         // Check each POST DATA parameter...
       
   136         if(!isset($_POST['to']) || ( isset($_POST['to']) && $_POST['to'] == '')) die_friendly('Sending of message failed', '<p>Please enter the username to which you want to send your message.</p>');
       
   137         if(!isset($_POST['subject']) || ( isset($_POST['subject']) && $_POST['subject'] == '')) die_friendly('Sending of message failed', '<p>Please enter a subject for your message.</p>');
       
   138         if(!isset($_POST['message']) || ( isset($_POST['message']) && $_POST['message'] == '')) die_friendly('Sending of message failed', '<p>Please enter a message to send.</p>');
       
   139         $namelist = $_POST['to'];
       
   140         $namelist = str_replace(', ', ',', $namelist);
       
   141         $namelist = explode(',', $namelist);
       
   142         foreach($namelist as $n) { $n = $db->escape($n); }
       
   143         if(count($namelist) > MAX_PMS_PER_BATCH && $session->get_permssions('mod_misc')) die_friendly('Limit exceeded', '<p>You can only send this message to a maximum of '.MAX_PMS_PER_BATCH.' users.</p>');
       
   144         $subject = $db->escape($_POST['subject']);
       
   145         $message = RenderMan::preprocess_text($_POST['message']);
       
   146         $base_query = 'INSERT INTO '.table_prefix.'privmsgs(message_from,message_to,date,subject,message_text,folder_name,message_read) VALUES';
       
   147         foreach($namelist as $n)
       
   148         {
       
   149           $base_query .= '(\''.$session->username.'\', \''.$n.'\', '.time().', \''.$subject.'\', \''.$message.'\', \'drafts\', 0),';
       
   150         }
       
   151         $base_query = substr($base_query, 0, strlen($base_query)-1) . ';';
       
   152         $result = $db->sql_query($base_query);
       
   153         $db->free_result();
       
   154         if(!$result) $db->_die('The message could not be saved.');
       
   155       } elseif(isset($_POST['_inbox'])) {
       
   156         header('Location: '.makeUrlNS('Special', 'PrivateMessages/Folder/Inbox'));
       
   157       }
       
   158       if($argv[1] == 'ReplyTo' && preg_match('#^([0-9]+)$#', $argv[2]))
       
   159       {
       
   160         $to = '';
       
   161         $text = '';
       
   162         $subj = '';
       
   163         $id = $argv[2];
       
   164         $q = $db->sql_query('SELECT p.message_from, p.message_to, p.subject, p.message_text, p.date, p.folder_name, u.signature FROM '.table_prefix.'privmsgs AS p LEFT JOIN '.table_prefix.'users AS u ON (p.message_from=u.username) WHERE message_id='.$id.';');
       
   165         if(!$q) $db->_die('The message data could not be selected.');
       
   166         $r = $db->fetchrow();
       
   167         $db->free_result();
       
   168         if( ($r['message_to'] != $session->username && $r['message_from'] != $session->username ) || $r['folder_name']=='drafts' ) die_friendly('Access denied', '<p>You are not authorized to view the contents of this message.</p>');
       
   169         $subj = 'Re: ' . $r['subject'];
       
   170         $text = "\n\n\nOn ".date('M j, Y G:i', $r['date']).", ".$r['message_from']." wrote:\n> ".str_replace("\n", "\n> ", $r['message_text']); // Way less complicated than using a regex ;-)
       
   171         
       
   172         $tbuf = $text;
       
   173         while( preg_match("/\n([\> ]*?)\> \>/", $text) )
       
   174         {
       
   175           $text = preg_replace("/\n([\> ]*?)\> \>/", '\\1>>', $text);
       
   176           if ( $text == $tbuf )
       
   177             break;
       
   178           $tbuf = $text;
       
   179         }
       
   180         
       
   181         $to = $r['message_from'];
       
   182       } else {
       
   183         if($argv[1]=='to' && $argv[2]) $to = $argv[2];
       
   184         else $to = '';
       
   185         $text = '';
       
   186         $subj = '';
       
   187       }
       
   188         $template->header();
       
   189         userprefs_show_menu();
       
   190         echo '<form action="'.makeUrlNS('Special', 'PrivateMessages/Compose/Send').'" method="post" onsubmit="if(!submitAuthorized) return false;">';
       
   191         ?>
       
   192         <br />
       
   193         <div class="tblholder"><table border="0" width="100%" cellspacing="1" cellpadding="4">
       
   194           <tr><th colspan="2">Compose new private message</th></tr>
       
   195           <tr><td class="row1">To:<br /><small>Separate multiple names with a single comma; you<br />can send this message to up to <b><?php echo (string)MAX_PMS_PER_BATCH; ?></b> users.</small></td><td class="row1"><?php echo $template->username_field('to', (isset($_POST['_savedraft'])) ? $_POST['to'] : $to ); ?></td></tr>
       
   196           <tr><td class="row2">Subject:</td><td class="row2"><input name="subject" type="text" size="30" value="<?php if(isset($_POST['_savedraft'])) echo $_POST['subject']; else echo $subj; ?>" /></td></tr>
       
   197           <tr><td class="row1">Message:</td><td class="row1" style="min-width: 80%;"><textarea rows="20" cols="40" name="message" style="width: 100%;"><?php if(isset($_POST['_savedraft'])) echo $_POST['message']; else echo $text; ?></textarea></td></tr>
       
   198           <tr><th colspan="2"><input type="submit" name="_send" value="Send message" />  <input type="submit" name="_savedraft" value="Save as draft" /> <input type="submit" name="_inbox" value="Back to Inbox" /></th></tr>
       
   199         </table></div>
       
   200         <?php
       
   201         echo '</form>';
       
   202         $template->footer();
       
   203       break;
       
   204     case 'Edit':
       
   205       $id = $argv[1];
       
   206       if(!preg_match('#^([0-9]+)$#', $id)) die_friendly('Message error', '<p>Invalid message ID</p>');
       
   207       $q = $db->sql_query('SELECT message_from, message_to, subject, message_text, date, folder_name, message_read FROM '.table_prefix.'privmsgs WHERE message_id='.$id.'');
       
   208       if(!$q) $db->_die('The message data could not be selected.');
       
   209       $r = $db->fetchrow();
       
   210       $db->free_result();
       
   211       if($r['message_from'] != $session->username || $r['message_read'] == 1 ) die_friendly('Access denied', '<p>You are not authorized to edit this message.</p>');
       
   212       $fname = $argv[2];
       
   213       
       
   214       if(isset($_POST['_send']))
       
   215       {
       
   216         // Check each POST DATA parameter...
       
   217         if(!isset($_POST['to']) || ( isset($_POST['to']) && $_POST['to'] == '')) die_friendly('Sending of message failed', '<p>Please enter the username to which you want to send your message.</p>');
       
   218         if(!isset($_POST['subject']) || ( isset($_POST['subject']) && $_POST['subject'] == '')) die_friendly('Sending of message failed', '<p>Please enter a subject for your message.</p>');
       
   219         if(!isset($_POST['message']) || ( isset($_POST['message']) && $_POST['message'] == '')) die_friendly('Sending of message failed', '<p>Please enter a message to send.</p>');
       
   220         $namelist = $_POST['to'];
       
   221         $namelist = str_replace(', ', ',', $namelist);
       
   222         $namelist = explode(',', $namelist);
       
   223         foreach($namelist as $n) { $n = $db->escape($n); }
       
   224         $subject = RenderMan::preprocess_text($_POST['subject']);
       
   225         $message = RenderMan::preprocess_text($_POST['message']);
       
   226         $base_query = 'UPDATE '.table_prefix.'privmsgs SET subject=\''.$subject.'\',message_to=\''.$namelist[0].'\',message_text=\''.$message.'\',folder_name=\'inbox\' WHERE message_id='.$id.';';
       
   227         $result = $db->sql_query($base_query);
       
   228         $db->free_result();
       
   229         if(!$result) $db->_die('The message could not be sent.');
       
   230         else die_friendly('Message status', '<p>Your message has been sent. You may edit the message if you wish; one copy for each recipient will be in your outbox until each recipient has read it. Return to your <a href="'.makeUrlNS('Special', 'PrivateMessages/Folder/Inbox').'">inbox</a>.</p>');
       
   231         return;
       
   232       } elseif(isset($_POST['_savedraft'])) {
       
   233         // Check each POST DATA parameter...
       
   234         if(!isset($_POST['to']) || ( isset($_POST['to']) && $_POST['to'] == '')) die_friendly('Sending of message failed', '<p>Please enter the username to which you want to send your message.</p>');
       
   235         if(!isset($_POST['subject']) || ( isset($_POST['subject']) && $_POST['subject'] == '')) die_friendly('Sending of message failed', '<p>Please enter a subject for your message.</p>');
       
   236         if(!isset($_POST['message']) || ( isset($_POST['message']) && $_POST['message'] == '')) die_friendly('Sending of message failed', '<p>Please enter a message to send.</p>');
       
   237         $namelist = $_POST['to'];
       
   238         $namelist = str_replace(', ', ',', $namelist);
       
   239         $namelist = explode(',', $namelist);
       
   240         foreach($namelist as $n) { $n = $db->escape($n); }
       
   241         $subject = $db->escape($_POST['subject']);
       
   242         $message = RenderMan::preprocess_text($_POST['message']);
       
   243         $base_query = 'UPDATE '.table_prefix.'privmsgs SET subject=\''.$subject.'\',message_to=\''.$namelist[0].'\',message_text=\''.$message.'\' WHERE message_id='.$id.';';
       
   244         $result = $db->sql_query($base_query);
       
   245         $db->free_result();
       
   246         if(!$result) $db->_die('The message could not be saved.');
       
   247       }
       
   248         if($argv[1]=='to' && $argv[2]) $to = $argv[2];
       
   249         else $to = '';
       
   250         $template->header();
       
   251         userprefs_show_menu();
       
   252         echo '<form action="'.makeUrlNS('Special', 'PrivateMessages/Edit/'.$id).'" method="post">';
       
   253         ?>
       
   254         <br />
       
   255         <div class="tblholder"><table border="0" width="100%" cellspacing="1" cellpadding="4">
       
   256           <tr><th colspan="2">Edit draft</th></tr>
       
   257           <tr><td class="row1">To:<br /><small>Separate multiple names with a single comma</small></td><td class="row1"><input name="to" type="text" size="30" value="<?php if(isset($_POST['_savedraft'])) echo $_POST['to']; else echo $r['message_to']; ?>" /></td></tr>
       
   258           <tr><td class="row2">Subject:</td><td class="row2"><input name="subject" type="text" size="30" value="<?php if(isset($_POST['_savedraft'])) echo $_POST['subject']; else echo $r['subject']; ?>" /></td></tr>
       
   259           <tr><td class="row1">Message:</td><td class="row1"><textarea rows="20" cols="40" name="message" style="width: 100%;"><?php if(isset($_POST['_savedraft'])) echo $_POST['message']; else echo $r['message_text']; ?></textarea></td></tr>
       
   260           <tr><th colspan="2"><input type="submit" name="_send" value="Send message" />  <input type="submit" name="_savedraft" value="Save as draft" /></th></tr>
       
   261         </table></div>
       
   262         <?php
       
   263         echo '</form>';
       
   264         $template->footer();
       
   265       break;
       
   266     case 'Folder':
       
   267       $template->header();
       
   268       userprefs_show_menu();
       
   269       switch($argv[1])
       
   270       {
       
   271         default:
       
   272           echo '<p>The folder "'.$argv[1].'" does not exist. Return to your <a href="'.makeUrlNS('Special', 'PrivateMessages/Folder/Inbox').'">inbox</a>.</p>';
       
   273           break;
       
   274         case 'Inbox':
       
   275         case 'Outbox':
       
   276         case 'Sent':
       
   277         case 'Drafts':
       
   278         case 'Archive':
       
   279           ?>
       
   280           <table border="0" width="100%" cellspacing="10" cellpadding="0">
       
   281           <tr>
       
   282           <td style="padding: 0px; width: 120px;" valign="top"  >
       
   283           <div class="tblholder" style="width: 120px;"><table border="0" width="120" cellspacing="1" cellpadding="4">
       
   284           <tr><th><small>Private messages</small></th></tr>
       
   285           <tr><td class="row1"><small><a href="<?php echo $session->append_sid('Inbox'); ?>">Inbox</a>    </small></td></tr>
       
   286           <tr><td class="row2"><small><a href="<?php echo $session->append_sid('Outbox'); ?>">Outbox</a>  </small></td></tr>
       
   287           <tr><td class="row1"><small><a href="<?php echo $session->append_sid('Sent'); ?>">Sent Items</a></small></td></tr>
       
   288           <tr><td class="row2"><small><a href="<?php echo $session->append_sid('Drafts'); ?>">Drafts</a>  </small></td></tr>
       
   289           <tr><td class="row1"><small><a href="<?php echo $session->append_sid('Archive'); ?>">Archive</a></small></td></tr>
       
   290           <tr><th><small>Buddies</small></th></tr>
       
   291           <tr><td class="row2"><small><a href="<?php echo makeUrlNS('Special', 'PrivateMessages/FriendList'); ?>">Friend list</a></small></td></tr>
       
   292           <tr><td class="row1"><small><a href="<?php echo makeUrlNS('Special', 'PrivateMessages/FoeList'); ?>">Foe list</a></small></td></tr>
       
   293           </table></div>
       
   294           </td>
       
   295           <td valign="top">
       
   296           <?php
       
   297           $fname = strtolower($argv[1]);
       
   298           switch($argv[1])
       
   299           {
       
   300             case 'Inbox':
       
   301             case 'Archive':
       
   302             default:
       
   303               $q = $db->sql_query('SELECT p.message_id, p.message_from, p.message_to, p.date, p.subject, p.message_read FROM '.table_prefix.'privmsgs AS p WHERE p.folder_name=\''.$fname.'\' AND p.message_to=\''.$session->username.'\' ORDER BY date DESC;');
       
   304               break;
       
   305             case 'Outbox':
       
   306               $q = $db->sql_query('SELECT p.message_id, p.message_from, p.message_to, p.date, p.subject, p.message_read FROM '.table_prefix.'privmsgs AS p WHERE p.message_from=\''.$session->username.'\' AND message_read=0 ORDER BY date DESC;');
       
   307               break;
       
   308             case 'Sent':
       
   309               $q = $db->sql_query('SELECT p.message_id, p.message_from, p.message_to, p.date, p.subject, p.message_read FROM '.table_prefix.'privmsgs AS p WHERE p.message_from=\''.$session->username.'\' AND message_read=1 ORDER BY date DESC;');
       
   310               break;
       
   311             case 'Drafts':
       
   312               $q = $db->sql_query('SELECT p.message_id, p.message_from, p.message_to, p.date, p.subject, p.message_read FROM '.table_prefix.'privmsgs AS p WHERE p.folder_name=\''.$fname.'\' AND p.message_from=\''.$session->username.'\' ORDER BY date DESC;');
       
   313               break;
       
   314           }
       
   315           if($argv[1] == 'Drafts' || $argv[1] == 'Outbox') $act = 'Edit';
       
   316           else $act = 'View';
       
   317           if(!$q) $db->_die('The private message data could not be selected.');
       
   318           echo '<form action="'.makeUrlNS('Special', 'PrivateMessages/PostHandler').'" method="post"><div class="tblholder"><table border="0" width="100%" cellspacing="1" cellpadding="4"><tr><th colspan="4" style="text-align: left;">Folder: '.$argv[1].'</th></tr><tr><th class="subhead">';
       
   319           if($fname == 'drafts' || $fname == 'Outbox') echo 'To'; else echo 'From';
       
   320           echo '</th><th class="subhead">Subject</th><th class="subhead">Date</th><th class="subhead">Mark</th></tr>';
       
   321           if($db->numrows() < 1)
       
   322             echo '<tr><td style="text-align: center;" class="row1" colspan="4">No messages in this folder.</td></tr>';
       
   323           else {
       
   324             $cls = 'row2';
       
   325             while($r = $db->fetchrow())
       
   326             {
       
   327               if($cls == 'row2') $cls='row1';
       
   328               else $cls = 'row2';
       
   329               $mto = str_replace(' ', '_', $r['message_to']);
       
   330               $mfr = str_replace(' ', '_', $r['message_from']);
       
   331               echo '<tr><td class="'.$cls.'"><a href="'.makeUrlNS('User', ( $fname == 'drafts') ? $mto : $mfr).'">';
       
   332               if($fname == 'drafts' || $fname == 'outbox') echo $r['message_to']; else echo $r['message_from'];
       
   333               echo '</a></td><td class="'.$cls.'"><a href="'.makeUrlNS('Special', 'PrivateMessages/'.$act.'/'.$r['message_id']).'">';
       
   334               if($r['message_read'] == 0) echo '<b>';
       
   335               echo $r['subject'];
       
   336               if($r['message_read'] == 0) echo '</b>';
       
   337               echo '</a></td><td class="'.$cls.'">'.date('M j, Y G:i', $r['date']).'</td><td class="'.$cls.'" style="text-align: center;"><input name="marked_'.$r['message_id'].'" type="checkbox" /></td></tr>';
       
   338             }
       
   339             $db->free_result();
       
   340           }
       
   341           echo '<tr><th style="text-align: right;" colspan="4"><input type="hidden" name="folder" value="'.$fname.'" /><input type="submit" name="archive" value="Archive selected" /> <input type="submit" name="delete" value="Delete selected" /> <input type="submit" name="deleteall" value="Delete all" /></th></tr>';
       
   342           echo '</table></div></form>
       
   343           <br />
       
   344           <a href="'.makeUrlNS('Special', 'PrivateMessages/Compose/').'">New message</a>
       
   345           </td></tr></table>';
       
   346           break;
       
   347       }
       
   348       $template->footer();
       
   349       break;
       
   350     case 'PostHandler':
       
   351       $fname = $db->escape(strtolower($_POST['folder']));
       
   352       if($fname=='drafts' || $fname=='outbox')
       
   353       {
       
   354         $q = $db->sql_query('SELECT p.message_id, p.message_from, p.message_to, p.date, p.subject FROM '.table_prefix.'privmsgs AS p WHERE p.folder_name=\''.$fname.'\' AND p.message_from=\''.$session->username.'\' ORDER BY date DESC;');  
       
   355       } else {
       
   356         $q = $db->sql_query('SELECT p.message_id, p.message_from, p.message_to, p.date, p.subject FROM '.table_prefix.'privmsgs AS p WHERE p.folder_name=\''.$fname.'\' AND p.message_to=\''.$session->username.'\' ORDER BY date DESC;');
       
   357       }
       
   358       if(!$q) $db->_die('The private message data could not be selected.');
       
   359           
       
   360       if(isset($_POST['archive'])) {
       
   361         while($row = $db->fetchrow($q))
       
   362         {
       
   363           if(isset($_POST['marked_'.$row['message_id']]))
       
   364           {
       
   365             $e = $db->sql_query('UPDATE '.table_prefix.'privmsgs SET folder_name=\'archive\' WHERE message_id='.$row['message_id'].';');
       
   366             if(!$e) $db->_die('Message '.$row['message_id'].' was not successfully moved.');
       
   367             $db->free_result();
       
   368           }
       
   369         }
       
   370       } elseif(isset($_POST['delete'])) {
       
   371         while($row = $db->fetchrow($q))
       
   372         {
       
   373           if(isset($_POST['marked_'.$row['message_id']]))
       
   374           {
       
   375             $e = $db->sql_query('DELETE FROM '.table_prefix.'privmsgs WHERE message_id='.$row['message_id'].';');
       
   376             if(!$e) $db->_die('Message '.$row['message_id'].' was not successfully moved.');
       
   377             $db->free_result();
       
   378           }
       
   379         }
       
   380       } elseif(isset($_POST['deleteall'])) {
       
   381         while($row = $db->fetchrow($q))
       
   382         {
       
   383           $e = $db->sql_query('DELETE FROM '.table_prefix.'privmsgs WHERE message_id='.$row['message_id'].';');
       
   384           if(!$e) $db->_die('Message '.$row['message_id'].' was not successfully moved.');
       
   385           $db->free_result();
       
   386         }
       
   387       } else {
       
   388         die_friendly('Invalid request', 'This section can only be accessed from within another Private Message section.');
       
   389       }
       
   390       $db->free_result($q);
       
   391       header('Location: '.makeUrlNS('Special', 'PrivateMessages/Folder/'. substr(strtoupper($_POST['folder']), 0, 1) . substr(strtolower($_POST['folder']), 1, strlen($_POST['folder'])) ));
       
   392       break;
       
   393     case 'FriendList':
       
   394       if($argv[1] == 'Add')
       
   395       {
       
   396         if(isset($_POST['_go']))
       
   397           $buddyname = $_POST['buddyname'];
       
   398         elseif($argv[2])
       
   399           $buddyname = $argv[2];
       
   400         else
       
   401           die_friendly('Error adding buddy', '<p>No name specified</p>');
       
   402         $q = $db->sql_query('SELECT user_id FROM '.table_prefix.'users WHERE username=\''.$db->escape($buddyname).'\'');
       
   403         if(!$q) $db->_die('The buddy\'s user ID could not be selected.');
       
   404         if($db->numrows() < 1) echo '<h3>Error adding buddy</h3><p>The username you entered is not in use by any registered user.</p>';
       
   405         {
       
   406           $r = $db->fetchrow();
       
   407           $db->free_result();
       
   408           $q = $db->sql_query('INSERT INTO '.table_prefix.'buddies(user_id,buddy_user_id,is_friend) VALUES('.$session->user_id.', '.$r['user_id'].', 1);');
       
   409           if(!$q) echo '<h3>Warning:</h3><p>Buddy could not be added: '.mysql_error().'</p>';
       
   410           $db->free_result();
       
   411         }
       
   412       } elseif($argv[1] == 'Remove' && preg_match('#^([0-9]+)$#', $argv[2])) {
       
   413         // Using WHERE user_id prevents users from deleting others' buddies
       
   414         $q = $db->sql_query('DELETE FROM '.table_prefix.'buddies WHERE user_id='.$session->user_id.' AND buddy_id='.$argv[2].';');
       
   415         $db->free_result();
       
   416         if(!$q) echo '<h3>Warning:</h3><p>Buddy could not be deleted: '.mysql_error().'</p>';
       
   417         if(mysql_affected_rows() < 1) echo '<h3>Warning:</h3><p>No rows were affected. Either the selected buddy ID does not exist or you tried to delete someone else\'s buddy.</p>';
       
   418       }
       
   419       $template->header();
       
   420       userprefs_show_menu();
       
   421       ?>
       
   422       <table border="0" width="100%" cellspacing="10" cellpadding="0">
       
   423           <tr>
       
   424           <td style="padding: 0px; width: 120px;" valign="top"  >
       
   425           <div class="tblholder" style="width: 120px;"><table border="0" width="120" cellspacing="1" cellpadding="4">
       
   426           <tr><th><small>Private messages</small></th></tr>
       
   427           <tr><td class="row1"><small><a href="<?php echo $session->append_sid('Inbox'); ?>">Inbox</a>    </small></td></tr>
       
   428           <tr><td class="row2"><small><a href="<?php echo $session->append_sid('Outbox'); ?>">Outbox</a>  </small></td></tr>
       
   429           <tr><td class="row1"><small><a href="<?php echo $session->append_sid('Sent'); ?>">Sent Items</a></small></td></tr>
       
   430           <tr><td class="row2"><small><a href="<?php echo $session->append_sid('Drafts'); ?>">Drafts</a>  </small></td></tr>
       
   431           <tr><td class="row1"><small><a href="<?php echo $session->append_sid('Archive'); ?>">Archive</a></small></td></tr>
       
   432           <tr><th><small>Buddies</small></th></tr>
       
   433           <tr><td class="row2"><small><a href="<?php echo makeUrlNS('Special', 'PrivateMessages/FriendList'); ?>">Friend list</a></small></td></tr>
       
   434           <tr><td class="row1"><small><a href="<?php echo makeUrlNS('Special', 'PrivateMessages/FoeList'); ?>">Foe list</a></small></td></tr>
       
   435           </table></div>
       
   436           </td>
       
   437           <td valign="top">
       
   438         <?php
       
   439         $q = $db->sql_query('SELECT u.username,b.buddy_id FROM '.table_prefix.'buddies AS b LEFT JOIN '.table_prefix.'users AS u ON ( u.user_id=b.buddy_user_id ) WHERE b.user_id='.$session->user_id.' AND is_friend=1;');
       
   440         if(!$q) $db->_die('The buddy list could not be selected.');
       
   441         else 
       
   442         {
       
   443           $allbuds = '';
       
   444           echo '<br /><div class="tblholder"><table border="0" width="100%" cellspacing="1" cellpadding="4"><tr><th colspan="3">Buddy list for '.$session->username.'</th></tr>';
       
   445           if($db->numrows() < 1) echo '<tr><td class="row3">No buddies in your list.</td></tr>';
       
   446           $cls = 'row2';
       
   447           while ( $row = $db->fetchrow() )
       
   448           {
       
   449             if($cls=='row2') $cls = 'row1';
       
   450             else $cls = 'row2';
       
   451             echo '<tr><td class="'.$cls.'"><a href="'.makeUrlNS('User', str_replace(' ', '_', $row['username'])).'" '. ( isPage($paths->nslist['User'].str_replace(' ', '_', $row['username'])) ? '' : 'class="wikilink-nonexistent" ' ) .'>'.$row['username'].'</a></td><td class="'.$cls.'"><a href="'.makeUrlNS('Special', 'PrivateMessages/Compose/to/'.str_replace(' ', '_', $row['username'])).'">Send private message</a></td><td class="'.$cls.'"><a onclick="return confirm(\'Are you sure you want to delete this user from your buddy list?\')" href="'.makeUrlNS('Special', 'PrivateMessages/FriendList/Remove/'.$row['buddy_id']).'">Remove</a></td></tr>';
       
   452             $allbuds .= str_replace(' ', '_', $row['username']).',';
       
   453           }
       
   454           $db->free_result();
       
   455           $allbuds = substr($allbuds, 0, strlen($allbuds)-1);
       
   456           if($cls=='row2') $cls = 'row1';
       
   457           else $cls = 'row2';
       
   458           echo '<tr><td colspan="3" class="'.$cls.'" style="text-align: center;"><a href="'.makeUrlNS('Special', 'PrivateMessages/Compose/to/'.$allbuds).'">Send a PM to all buddies</a></td></tr>';
       
   459           echo '</table></div>';
       
   460         }
       
   461         echo '<form action="'.makeUrlNS('Special', 'PrivateMessages/FriendList/Add').'" method="post" onsubmit="if(!submitAuthorized) return false;">
       
   462               <h3>Add a new friend</h3>';
       
   463         echo '<p>Username: '.$template->username_field('buddyname').'  <input type="submit" name="_go" value="Add" /></p>';
       
   464         echo '</form>';
       
   465         ?>
       
   466         </td>
       
   467         </tr>
       
   468         </table>
       
   469         <?php
       
   470       $template->footer();
       
   471       break;
       
   472     case 'FoeList':
       
   473       if($argv[1] == 'Add' && isset($_POST['_go']))
       
   474       {
       
   475         $q = $db->sql_query('SELECT user_id FROM '.table_prefix.'users WHERE username=\''.$db->escape($_POST['buddyname']).'\'');
       
   476         if(!$q) $db->_die('The buddy\'s user ID could not be selected.');
       
   477         if($db->numrows() < 1) echo '<h3>Error adding buddy</h3><p>The username you entered is not in use by any registered user.</p>';
       
   478         {
       
   479           $r = $db->fetchrow();
       
   480           $q = $db->sql_query('INSERT INTO '.table_prefix.'buddies(user_id,buddy_user_id,is_friend) VALUES('.$session->user_id.', '.$r['user_id'].', 0);');
       
   481           if(!$q) echo '<h3>Warning:</h3><p>Buddy could not be added: '.mysql_error().'</p>';
       
   482         }
       
   483         $db->free_result();
       
   484       } elseif($argv[1] == 'Remove' && preg_match('#^([0-9]+)$#', $argv[2])) {
       
   485         // Using WHERE user_id prevents users from deleting others' buddies
       
   486         $q = $db->sql_query('DELETE FROM '.table_prefix.'buddies WHERE user_id='.$session->user_id.' AND buddy_id='.$argv[2].';');
       
   487         $db->free_result();
       
   488         if(!$q) echo '<h3>Warning:</h3><p>Buddy could not be deleted: '.mysql_error().'</p>';
       
   489         if(mysql_affected_rows() < 1) echo '<h3>Warning:</h3><p>No rows were affected. Either the selected buddy ID does not exist or you tried to delete someone else\'s buddy.</p>';
       
   490       }
       
   491       $template->header();
       
   492       userprefs_show_menu();
       
   493       ?>
       
   494       <table border="0" width="100%" cellspacing="10" cellpadding="0">
       
   495           <tr>
       
   496           <td style="padding: 0px; width: 120px;" valign="top"  >
       
   497           <div class="tblholder" style="width: 120px;"><table border="0" width="120" cellspacing="1" cellpadding="4">
       
   498           <tr><th><small>Private messages</small></th></tr>
       
   499           <tr><td class="row1"><small><a href="<?php echo $session->append_sid('Inbox'); ?>">Inbox</a>    </small></td></tr>
       
   500           <tr><td class="row2"><small><a href="<?php echo $session->append_sid('Outbox'); ?>">Outbox</a>  </small></td></tr>
       
   501           <tr><td class="row1"><small><a href="<?php echo $session->append_sid('Sent'); ?>">Sent Items</a></small></td></tr>
       
   502           <tr><td class="row2"><small><a href="<?php echo $session->append_sid('Drafts'); ?>">Drafts</a>  </small></td></tr>
       
   503           <tr><td class="row1"><small><a href="<?php echo $session->append_sid('Archive'); ?>">Archive</a></small></td></tr>
       
   504           <tr><th><small>Buddies</small></th></tr>
       
   505           <tr><td class="row2"><small><a href="<?php echo makeUrlNS('Special', 'PrivateMessages/FriendList'); ?>">Friend list</a></small></td></tr>
       
   506           <tr><td class="row1"><small><a href="<?php echo makeUrlNS('Special', 'PrivateMessages/FoeList'); ?>">Foe list</a></small></td></tr>
       
   507           </table></div>
       
   508           </td>
       
   509           <td valign="top">
       
   510         <?php
       
   511         $q = $db->sql_query('SELECT u.username,b.buddy_id FROM '.table_prefix.'buddies AS b LEFT JOIN '.table_prefix.'users AS u ON ( u.user_id=b.buddy_user_id ) WHERE b.user_id='.$session->user_id.' AND is_friend=0;');
       
   512         if(!$q) $db->_die('The buddy list could not be selected.');
       
   513         else 
       
   514         {
       
   515           $allbuds = '';
       
   516           echo '<br /><div class="tblholder"><table border="0" width="100%" cellspacing="1" cellpadding="4"><tr><th colspan="3">Foe list for '.$session->username.'</th></tr>';
       
   517           if($db->numrows() < 1) echo '<tr><td class="row2">No foes in your list.</td></tr>';
       
   518           $cls = 'row2';
       
   519           while ( $row = $db->fetchrow() )
       
   520           {
       
   521             if($cls=='row2') $cls = 'row1';
       
   522             else $cls = 'row2';
       
   523             echo '<tr><td class="'.$cls.'"><a href="'.makeUrlNS('User', str_replace(' ', '_', $row['username'])).'" '. ( isPage($paths->nslist['User'].str_replace(' ', '_', $row['username'])) ? '' : 'class="wikilink-nonexistent" ' ) .'>'.$row['username'].'</a></td><td class="'.$cls.'"><a href="'.makeUrlNS('Special', 'PrivateMessages/Compose/to/'.str_replace(' ', '_', $row['username'])).'">Send private message</a></td><td class="'.$cls.'"><a onclick="return confirm(\'Are you sure you want to delete this user from your buddy list?\')" href="'.makeUrlNS('Special', 'PrivateMessages/FriendList/Remove/'.$row['buddy_id']).'">Remove</a></td></tr>';
       
   524             $allbuds .= str_replace(' ', '_', $row['username']).',';
       
   525           }
       
   526           $allbuds = substr($allbuds, 0, strlen($allbuds)-1);
       
   527           if($cls=='row2') $cls = 'row1';
       
   528           else $cls = 'row2';
       
   529           //echo '<tr><td colspan="3" class="'.$cls.'" style="text-align: center;"><a href="'.makeUrlNS('Special', 'PrivateMessages/Compose/to/'.$allbuds).'">Send a PM to all buddies</a></td></tr>';
       
   530           echo '</table></div>';
       
   531         }
       
   532         $db->free_result();
       
   533         echo '<form action="'.makeUrlNS('Special', 'PrivateMessages/FoeList/Add').'" method="post" onsubmit="if(!submitAuthorized) return false;">
       
   534               <h3>Add a new foe</h3>';
       
   535         echo '<p>Username: '.$template->username_field('buddyname').'  <input type="submit" name="_go" value="Add" /></p>';
       
   536         echo '</form>';
       
   537         ?>
       
   538         </td>
       
   539         </tr>
       
   540         </table>
       
   541         <?php
       
   542       $template->footer();
       
   543       break;
       
   544   }
       
   545 }
       
   546 
       
   547 ?>