2006-12-02 04:36:16 +00:00
< ? php
/*
2009-11-17 13:48:46 +00:00
* e107 website system
*
2014-08-02 13:26:21 +02:00
* Copyright ( C ) 2008 - 2014 e107 Inc ( e107 . org )
2009-11-17 13:48:46 +00:00
* Released under the terms and conditions of the
* GNU General Public License ( http :// www . gnu . org / licenses / gpl . txt )
*
2009-12-17 22:47:20 +00:00
* PM plugin - base class API
2009-11-17 13:48:46 +00:00
*
*/
2006-12-02 04:36:16 +00:00
2009-12-17 22:47:20 +00:00
2006-12-02 04:36:16 +00:00
if ( ! defined ( 'e107_INIT' )) { exit ; }
class private_message
{
2009-12-17 22:47:20 +00:00
protected $e107 ;
protected $pmPrefs ;
2016-03-30 19:10:00 -07:00
public function prefs ()
{
return $this -> pmPrefs ;
}
2009-12-17 22:47:20 +00:00
/**
* Constructor
*
* @ param array $prefs - pref settings for PM plugin
* @ return none
*/
2016-04-17 12:04:35 -07:00
public function __construct ( $prefs = null )
2009-12-17 22:47:20 +00:00
{
$this -> e107 = e107 :: getInstance ();
2016-04-25 09:31:55 -07:00
$this -> pmPrefs = e107 :: pref ( 'pm' );
}
2009-12-17 22:47:20 +00:00
/**
* Mark a PM as read
* If flag set , send read receipt to sender
*
* @ param int $pm_id - ID of PM
* @ param array $pm_info - PM details
*
* @ return none
*
* @ todo - 'read_delete' pref doesn ' t exist - remove code ? Or support ?
*/
2006-12-02 04:36:16 +00:00
function pm_mark_read ( $pm_id , $pm_info )
{
$now = time ();
2009-12-17 22:47:20 +00:00
if ( $this -> pmPrefs [ 'read_delete' ])
2006-12-02 04:36:16 +00:00
{
$this -> del ( $pm_id );
}
else
{
2013-03-26 12:16:55 +01:00
e107 :: getDb () -> gen ( " UPDATE `#private_msg` SET `pm_read` = { $now } WHERE `pm_id`= " . intval ( $pm_id )); // TODO does this work properly?
2009-12-17 22:47:20 +00:00
if ( strpos ( $pm_info [ 'pm_option' ], '+rr' ) !== FALSE )
2006-12-02 04:36:16 +00:00
{
$this -> pm_send_receipt ( $pm_info );
}
2015-02-12 16:52:30 -08:00
e107 :: getEvent () -> trigger ( 'user_pm_read' , $pm_id );
2006-12-02 04:36:16 +00:00
}
}
2009-12-17 22:47:20 +00:00
/*
* Get an existing PM
*
* @ param int $pmid - ID of PM in DB
*
* @ return boolean | array - FALSE on error , array of PM info on success
*/
2006-12-02 04:36:16 +00:00
function pm_get ( $pmid )
{
$qry = "
SELECT pm .* , ut . user_image AS sent_image , ut . user_name AS sent_name , uf . user_image AS from_image , uf . user_name AS from_name , uf . user_email as from_email , ut . user_email as to_email FROM #private_msg AS pm
LEFT JOIN #user AS ut ON ut.user_id = pm.pm_to
LEFT JOIN #user AS uf ON uf.user_id = pm.pm_from
WHERE pm . pm_id = '".intval($pmid)."'
" ;
2013-03-26 12:16:55 +01:00
if ( e107 :: getDb () -> gen ( $qry ))
2006-12-02 04:36:16 +00:00
{
2013-03-26 12:16:55 +01:00
$row = e107 :: getDb () -> fetch ();
2006-12-02 04:36:16 +00:00
return $row ;
}
return FALSE ;
}
2007-08-02 20:41:30 +00:00
2009-12-17 22:47:20 +00:00
/*
* Send a PM
*
* @ param array $vars - PM information
2010-01-06 20:09:58 +00:00
* [ 'receipt' ] - set TRUE if read receipt required
* [ 'uploaded' ] - list of attachments ( if any ) - each is an array ( 'size' , 'name' )
* [ 'to_userclass' ] - set TRUE if sending to a user class
* [ 'to_array' ] = array of recipients
* [ 'pm_userclass' ] = target user class
* [ 'to_info' ] = recipients array of array ( 'user_id' , 'user_class' )
2009-12-17 22:47:20 +00:00
*
2010-01-06 20:09:58 +00:00
* May also be an array as received from the generic table , if sending via a cron job
* identified by the existence of $vars [ 'pm_from' ]
2009-12-17 22:47:20 +00:00
*
2010-01-06 20:09:58 +00:00
* @ return string - text detailing result
2009-12-17 22:47:20 +00:00
*/
2006-12-02 04:36:16 +00:00
function add ( $vars )
{
2013-03-26 12:16:55 +01:00
$tp = e107 :: getParser ();
$sql = e107 :: getDb ();
2006-12-02 04:36:16 +00:00
$pmsize = 0 ;
2009-12-17 22:47:20 +00:00
$attachlist = '' ;
$pm_options = '' ;
2009-12-18 20:49:56 +00:00
$ret = '' ;
2010-01-06 20:09:58 +00:00
$addOutbox = TRUE ;
2016-04-17 12:04:35 -07:00
$timestamp = time ();
2010-01-06 20:09:58 +00:00
$maxSendNow = varset ( $this -> pmPrefs [ 'pm_max_send' ], 100 ); // Maximum number of PMs to send without queueing them
if ( isset ( $vars [ 'pm_from' ]))
{ // Doing bulk send off cron task
$info = array ();
foreach ( $vars as $k => $v )
2006-12-02 04:36:16 +00:00
{
2010-01-06 20:09:58 +00:00
if ( strpos ( $k , 'pm_' ) === 0 )
2006-12-02 04:36:16 +00:00
{
2010-01-06 20:09:58 +00:00
$info [ $k ] = $v ;
unset ( $vars [ $k ]);
2006-12-02 04:36:16 +00:00
}
}
2010-01-06 20:09:58 +00:00
$addOutbox = FALSE ; // Don't add to outbox - was done earlier
2006-12-02 04:36:16 +00:00
}
2010-01-06 20:09:58 +00:00
else
{ // Send triggered by user - may be immediate or bulk dependent on number of recipients
$vars [ 'options' ] = '' ;
if ( isset ( $vars [ 'receipt' ]) && $vars [ 'receipt' ]) { $pm_options .= '+rr+' ; }
2016-03-30 19:10:00 -07:00
2010-01-06 20:09:58 +00:00
if ( isset ( $vars [ 'uploaded' ]))
{
foreach ( $vars [ 'uploaded' ] as $u )
{
if ( ! isset ( $u [ 'error' ]) || ! $u [ 'error' ])
{
$pmsize += $u [ 'size' ];
$a_list [] = $u [ 'name' ];
}
}
$attachlist = implode ( chr ( 0 ), $a_list );
}
$pmsize += strlen ( $vars [ 'pm_message' ]);
$pm_subject = trim ( $tp -> toDB ( $vars [ 'pm_subject' ]));
$pm_message = trim ( $tp -> toDB ( $vars [ 'pm_message' ]));
if ( ! $pm_subject && ! $pm_message && ! $attachlist )
{ // Error - no subject, no message body and no uploaded files
return LAN_PM_65 ;
}
// Most of the pm info is fixed - just need to set the 'to' user on each send
$info = array (
'pm_from' => $vars [ 'from_id' ],
2016-04-17 12:04:35 -07:00
'pm_sent' => $timestamp , /* Date sent */
2010-01-06 20:09:58 +00:00
'pm_read' => 0 , /* Date read */
'pm_subject' => $pm_subject ,
'pm_text' => $pm_message ,
2014-08-02 13:26:21 +02:00
'pm_sent_del' => 0 , /* Set when can delete */
2010-01-06 20:09:58 +00:00
'pm_read_del' => 0 , /* set when can delete */
'pm_attachments' => $attachlist ,
'pm_option' => $pm_options , /* Options associated with PM - '+rr' for read receipt */
'pm_size' => $pmsize
);
2016-03-30 19:10:00 -07:00
2016-04-01 18:09:27 -07:00
// print_a($info);
// print_a($vars);
2007-08-02 20:41:30 +00:00
}
2015-04-23 09:53:54 -07:00
2016-04-03 14:37:37 -07:00
if ( ! empty ( $vars [ 'pm_userclass' ]) || isset ( $vars [ 'to_array' ]))
2006-12-02 04:36:16 +00:00
{
2016-04-03 14:37:37 -07:00
if ( ! empty ( $vars [ 'pm_userclass' ]))
2006-12-02 04:36:16 +00:00
{
2009-12-17 22:47:20 +00:00
$toclass = e107 :: getUserClass () -> uc_get_classname ( $vars [ 'pm_userclass' ]);
2006-12-02 04:36:16 +00:00
$tolist = $this -> get_users_inclass ( $vars [ 'pm_userclass' ]);
2015-04-23 09:53:54 -07:00
$ret .= LAN_PM_38 . " : { $toclass } <br /> " ;
2006-12-02 04:36:16 +00:00
$class = TRUE ;
2016-04-03 14:37:37 -07:00
$info [ 'pm_sent_del' ] = 1 ; // keep the outbox clean and limited to 1 entry when sending to an entire class.
2006-12-02 04:36:16 +00:00
}
else
{
$tolist = $vars [ 'to_array' ];
$class = FALSE ;
}
2010-01-06 20:09:58 +00:00
// Sending multiple PMs here. If more than some number ($maxSendNow), need to split into blocks.
if ( count ( $tolist ) > $maxSendNow )
{
$totalSend = count ( $tolist );
$targets = array_chunk ( $tolist , $maxSendNow ); // Split into a number of lists, each with the maximum number of elements (apart from the last block, of course)
unset ( $tolist );
$array = new ArrayData ;
$pmInfo = $info ;
$genInfo = array (
'gen_type' => 'pm_bulk' ,
'gen_datestamp' => time (),
'gen_user_id' => USERID ,
'gen_ip' => ''
);
for ( $i = 0 ; $i < count ( $targets ) - 1 ; $i ++ )
{ // Save the list in the 'generic' table
$pmInfo [ 'to_array' ] = $targets [ $i ]; // Should be in exactly the right format
$genInfo [ 'gen_intdata' ] = count ( $targets [ $i ]);
$genInfo [ 'gen_chardata' ] = $array -> WriteArray ( $pmInfo , TRUE );
2013-03-26 12:16:55 +01:00
$sql -> insert ( 'generic' , array ( 'data' => $genInfo , '_FIELD_TYPES' => array ( 'gen_chardata' => 'string' ))); // Don't want any of the clever sanitising now
2010-01-06 20:09:58 +00:00
}
$toclass .= ' [' . $totalSend . ']' ;
$tolist = $targets [ count ( $targets ) - 1 ]; // Send the residue now (means user probably isn't kept hanging around too long if sending lots)
unset ( $targets );
}
2006-12-02 04:36:16 +00:00
foreach ( $tolist as $u )
{
set_time_limit ( 30 );
2010-01-06 20:09:58 +00:00
$info [ 'pm_to' ] = intval ( $u [ 'user_id' ]); // Sending to a single user now
2013-03-26 12:16:55 +01:00
if ( $pmid = $sql -> insert ( 'private_msg' , $info ))
2006-12-02 04:36:16 +00:00
{
2015-03-04 16:25:36 +01:00
$info [ 'pm_id' ] = $pmid ;
e107 :: getEvent () -> trigger ( 'user_pm_sent' , $info );
2015-04-23 09:53:54 -07:00
unset ( $info [ 'pm_id' ]); // prevent it from being used on the next record.
2006-12-02 04:36:16 +00:00
if ( $class == FALSE )
{
2010-01-06 20:09:58 +00:00
$toclass .= $u [ 'user_name' ] . ', ' ;
2006-12-02 04:36:16 +00:00
}
2016-04-16 15:56:02 -07:00
if ( check_class ( $this -> pmPrefs [ 'notify_class' ], null , $u [ 'user_id' ]))
2006-12-02 04:36:16 +00:00
{
$vars [ 'to_info' ] = $u ;
2016-04-17 12:04:35 -07:00
$vars [ 'pm_sent' ] = $timestamp ;
2006-12-02 04:36:16 +00:00
$this -> pm_send_notify ( $u [ 'user_id' ], $vars , $pmid , count ( $a_list ));
}
}
else
{
$ret .= LAN_PM_39 . " : { $u [ 'user_name' ] } <br /> " ;
2015-04-23 09:53:54 -07:00
e107 :: getMessage () -> addDebug ( $sql -> getLastErrorText ());
2006-12-02 04:36:16 +00:00
}
}
2010-01-06 20:09:58 +00:00
if ( $addOutbox )
2006-12-02 04:36:16 +00:00
{
2010-01-06 20:09:58 +00:00
$info [ 'pm_to' ] = $toclass ; // Class info to put into outbox
$info [ 'pm_sent_del' ] = 0 ;
$info [ 'pm_read_del' ] = 1 ;
2013-03-26 12:16:55 +01:00
if ( ! $pmid = $sql -> insert ( 'private_msg' , $info ))
2010-01-06 20:09:58 +00:00
{
$ret .= LAN_PM_41 . '<br />' ;
}
2006-12-02 04:36:16 +00:00
}
}
else
2010-01-06 20:09:58 +00:00
{ // Sending to a single person
$info [ 'pm_to' ] = intval ( $vars [ 'to_info' ][ 'user_id' ]); // Sending to a single user now
2016-04-25 09:31:55 -07:00
2013-03-26 12:16:55 +01:00
if ( $pmid = $sql -> insert ( 'private_msg' , $info ))
2006-12-02 04:36:16 +00:00
{
2015-03-04 16:25:36 +01:00
$info [ 'pm_id' ] = $pmid ;
2016-04-17 12:04:35 -07:00
$info [ 'pm_sent' ] = $timestamp ;
2015-03-04 16:25:36 +01:00
e107 :: getEvent () -> trigger ( 'user_pm_sent' , $info );
2016-04-10 08:43:54 -07:00
if ( check_class ( $this -> pmPrefs [ 'notify_class' ], null , $vars [ 'to_info' ][ 'user_id' ]))
2006-12-02 04:36:16 +00:00
{
2016-04-10 08:43:54 -07:00
set_time_limit ( 20 );
2016-04-23 14:13:31 -07:00
$vars [ 'pm_sent' ] = $timestamp ;
2006-12-02 04:36:16 +00:00
$this -> pm_send_notify ( $vars [ 'to_info' ][ 'user_id' ], $vars , $pmid , count ( $a_list ));
}
$ret .= LAN_PM_40 . " : { $vars [ 'to_info' ][ 'user_name' ] } <br /> " ;
}
}
return $ret ;
}
2009-12-11 22:33:15 +00:00
/**
* Delete a PM from a user ' s inbox / outbox .
* PM is only actually deleted from DB once both sender and recipient have marked it as deleted
* When physically deleted , any attachments are deleted as well
*
* @ param integer $pmid - ID of the PM
2011-01-12 14:12:47 +00:00
* @ param boolean $force - set to TRUE to force deletion of unread PMs
2009-12-11 22:33:15 +00:00
* @ return boolean | string - FALSE if PM not found , or other DB error . String if successful
*/
2011-01-12 14:12:47 +00:00
function del ( $pmid , $force = FALSE )
2006-12-02 04:36:16 +00:00
{
2013-03-26 12:16:55 +01:00
$sql = e107 :: getDb ();
2008-02-03 15:22:33 +00:00
$pmid = ( int ) $pmid ;
2009-12-10 20:40:39 +00:00
$ret = '' ;
$newvals = '' ;
2013-03-26 12:16:55 +01:00
if ( $sql -> select ( 'private_msg' , '*' , 'pm_id = ' . $pmid . ' AND (pm_from = ' . USERID . ' OR pm_to = ' . USERID . ')' ))
2006-12-02 04:36:16 +00:00
{
2013-03-26 12:16:55 +01:00
$row = $sql -> fetch ();
2014-08-02 13:26:21 +02:00
// if user is the receiver of the PM
2011-01-12 14:12:47 +00:00
if ( ! $force && ( $row [ 'pm_to' ] == USERID ))
2006-12-02 04:36:16 +00:00
{
2009-12-10 20:40:39 +00:00
$newvals = 'pm_read_del = 1' ;
$ret .= LAN_PM_42 . '<br />' ;
2014-08-02 13:26:21 +02:00
if ( $row [ 'pm_sent_del' ] == 1 ) { $force = TRUE ; } // sender has deleted as well, set force to true so the DB record can be deleted
2006-12-02 04:36:16 +00:00
}
2014-08-02 13:26:21 +02:00
// if user is the sender of the PM
2011-01-12 14:12:47 +00:00
if ( ! $force && ( $row [ 'pm_from' ] == USERID ))
2006-12-02 04:36:16 +00:00
{
2011-01-12 14:12:47 +00:00
if ( $newvals != '' ) { $force = TRUE ; }
2009-12-10 20:40:39 +00:00
$newvals = 'pm_sent_del = 1' ;
2006-12-02 04:36:16 +00:00
$ret .= LAN_PM_43 . " <br /> " ;
2014-08-02 13:26:21 +02:00
if ( $row [ 'pm_read_del' ] == 1 ) { $force = TRUE ; } // receiver has deleted as well, set force to true so the DB record can be deleted
2006-12-02 04:36:16 +00:00
}
2011-01-12 14:12:47 +00:00
if ( $force == TRUE )
2006-12-02 04:36:16 +00:00
{
// Delete any attachments and remove PM from db
$attachments = explode ( chr ( 0 ), $row [ 'pm_attachments' ]);
2009-12-10 20:40:39 +00:00
$aCount = array ( 0 , 0 );
2006-12-02 04:36:16 +00:00
foreach ( $attachments as $a )
{
2009-12-10 20:40:39 +00:00
$a = trim ( $a );
if ( $a )
{
$filename = e_PLUGIN . 'pm/attachments/' . $a ;
if ( unlink ( $filename )) $aCount [ 0 ] ++ ; else $aCount [ 1 ] ++ ;
}
2006-12-02 04:36:16 +00:00
}
2009-12-10 20:40:39 +00:00
if ( $aCount [ 0 ] || $aCount [ 1 ])
{
2015-06-26 20:24:53 -07:00
// $ret .= str_replace(array('--GOOD--', '--FAIL--'), $aCount, LAN_PM_71).'<br />';
$ret .= e107 :: getParser () -> lanVars ( LAN_PM_71 , $aCount );
2009-12-10 20:40:39 +00:00
}
2013-05-10 16:14:19 +02:00
$sql -> delete ( 'private_msg' , 'pm_id = ' . $pmid );
2006-12-02 04:36:16 +00:00
}
else
{
2013-03-26 12:16:55 +01:00
$sql -> update ( 'private_msg' , $newvals . ' WHERE pm_id = ' . $pmid );
2006-12-02 04:36:16 +00:00
}
return $ret ;
}
2009-12-11 22:33:15 +00:00
return FALSE ;
2006-12-02 04:36:16 +00:00
}
2011-12-06 11:24:41 +00:00
/**
* Convinient url assembling shortcut
2016-03-30 19:10:00 -07:00
*//*
2011-12-06 11:24:41 +00:00
public function url ( $action , $params = array (), $options = array ())
{
if ( strpos ( $action , '/' ) === false ) $action = 'view/' . $action ;
2013-03-26 12:16:55 +01:00
e107 :: getUrl () -> create ( 'pm/' . $action , $params , $options );
2016-03-30 19:10:00 -07:00
} */
2009-12-11 22:33:15 +00:00
2010-01-04 21:35:38 +00:00
/**
2009-12-17 22:47:20 +00:00
* Send an email to notify of a PM
*
* @ param int $uid - not used
* @ param array $pmInfo - PM details
* @ param int $pmid - ID of PM in database
* @ param int $attach_count - number of attachments
*
* @ return none
*/
2016-04-13 10:16:17 -07:00
function pm_send_notify ( $uid , $pmInfo , $pmid , $attach_count = 0 )
2006-12-02 04:36:16 +00:00
{
2016-04-17 12:04:35 -07:00
// require_once(e_HANDLER.'mail.php');
2017-04-09 16:37:33 +01:00
/*
$tpl_file = THEME . 'pm_template.php' ;
2016-03-30 19:10:00 -07:00
2016-04-13 10:16:17 -07:00
$PM_NOTIFY = null ; // loaded in template below.
2017-04-09 16:37:33 +01:00
include ( is_readable ( $tpl_file ) ? $tpl_file : e_PLUGIN . 'pm/pm_template.php' );
2016-04-13 10:16:17 -07:00
$template = $PM_NOTIFY ;
2017-04-09 16:37:33 +01:00
*/
2017-04-11 21:52:09 +01:00
if ( THEME_LEGACY ){ include_once ( THEME . 'pm_template.php' );}
if ( ! $PM_NOTIFY ){ $PM_NOTIFY = e107 :: getTemplate ( 'pm' , 'pm' , 'notify' );}
2016-04-03 14:37:37 -07:00
2017-04-11 21:52:09 +01:00
if ( empty ( $PM_NOTIFY )) // BC Fallback.
2006-12-02 04:36:16 +00:00
{
2016-04-13 10:16:17 -07:00
2017-04-11 21:52:09 +01:00
$PM_NOTIFY =
2016-04-13 10:16:17 -07:00
" <div>
< h4 > " .LAN_PM_101. " { SITENAME } </ h4 >
< table class = 'table table-striped' >
< tr >< td > " .LAN_PM_102. " </ td >< td > { USERNAME } </ td ></ tr >
< tr >< td > " .LAN_PM_103. " </ td >< td > { PM_SUBJECT } </ td ></ tr >
< tr >< td > " .LAN_PM_108. " </ td >< td > { PM_DATE } </ td ></ tr >
< tr >< td > " .LAN_PM_104. " </ td >< td > { PM_ATTACHMENTS } </ td ></ tr >
2016-04-17 12:04:35 -07:00
</ table >< br />
2016-04-13 10:16:17 -07:00
< div > " .LAN_PM_105. " : { PM_URL } </ div >
</ div >
" ;
2006-12-02 04:36:16 +00:00
}
2016-04-03 14:37:37 -07:00
2016-04-17 12:04:35 -07:00
$url = e107 :: url ( 'pm' , 'index' , null , array ( 'mode' => 'full' )) . '?show.' . $pmid ;
2016-04-03 14:37:37 -07:00
2016-04-13 10:16:17 -07:00
$data = array ();
$data [ 'PM_SUBJECT' ] = $pmInfo [ 'pm_subject' ];
$data [ 'PM_ATTACHMENTS' ] = intval ( $attach_count );
$data [ 'PM_DATE' ] = e107 :: getParser () -> toDate ( $pmInfo [ 'pm_sent' ], 'long' );
$data [ 'SITENAME' ] = SITENAME ;
$data [ 'USERNAME' ] = USERNAME ;
2016-04-22 16:10:18 -07:00
$data [ 'PM_URL' ] = $url ; // e107::url('pm','index', null, array('mode'=>'full')).'?show.'.$pmid;
$data [ 'PM_BUTTON' ] = " <a class='btn btn-primary' href=' " . $url . " '> " . LAN_PM_113 . " </a> " ; // e107::url('pm','index', null, array('mode'=>'full')).'?show.'.$pmid;
2016-04-13 10:16:17 -07:00
2017-04-11 21:52:09 +01:00
$text = e107 :: getParser () -> simpleParse ( $PM_NOTIFY , $data );
2016-04-13 10:16:17 -07:00
2016-04-17 12:04:35 -07:00
$eml = array ();
$eml [ 'email_subject' ] = LAN_PM_100 . USERNAME ;
$eml [ 'send_html' ] = true ;
$eml [ 'email_body' ] = $text ;
$eml [ 'template' ] = 'default' ;
$eml [ 'e107_header' ] = $pmInfo [ 'to_info' ][ 'user_id' ];
if ( e107 :: getEmail () -> sendEmail ( $pmInfo [ 'to_info' ][ 'user_email' ], $pmInfo [ 'to_info' ][ 'user_name' ], $eml ))
{
return true ;
}
else
{
return false ;
}
2016-04-13 10:16:17 -07:00
2006-12-02 04:36:16 +00:00
}
2009-12-17 22:47:20 +00:00
2010-01-04 21:35:38 +00:00
/**
2009-12-17 22:47:20 +00:00
* Send PM read receipt
*
* @ param array $pmInfo - PM details
2016-04-23 14:13:31 -07:00
* @ return boolean
2009-12-17 22:47:20 +00:00
*/
2016-04-13 10:16:17 -07:00
function pm_send_receipt ( $pmInfo ) //TODO Add Template and combine with method above..
2006-12-02 04:36:16 +00:00
{
2009-12-17 22:47:20 +00:00
require_once ( e_HANDLER . 'mail.php' );
$subject = LAN_PM_106 . $pmInfo [ 'sent_name' ];
2016-04-13 10:16:17 -07:00
$pmlink = e107 :: url ( 'pm' , 'index' , null , array ( 'mode' => 'full' )) . '?show.' . $pmInfo [ 'pm_id' ];
2009-12-17 22:47:20 +00:00
$txt = str_replace ( " { UNAME} " , $pmInfo [ 'sent_name' ], LAN_PM_107 ) . date ( 'l F dS Y h:i:s A' ) . " \n \n " ;
$txt .= LAN_PM_108 . date ( 'l F dS Y h:i:s A' , $pmInfo [ 'pm_sent' ]) . " \n " ;
$txt .= LAN_PM_103 . $pmInfo [ 'pm_subject' ] . " \n " ;
2006-12-02 04:36:16 +00:00
$txt .= LAN_PM_105 . " \n " . $pmlink . " \n " ;
2016-04-03 14:37:37 -07:00
2016-04-23 14:13:31 -07:00
if ( sendemail ( $pmInfo [ 'from_email' ], $subject , $txt , $pmInfo [ 'from_name' ]))
{
return true ;
}
return false ;
2006-12-02 04:36:16 +00:00
}
2009-12-10 20:40:39 +00:00
/**
2016-04-23 14:13:31 -07:00
* Get list of users blocked from sending to a specific user ID .
2009-12-17 22:47:20 +00:00
*
2016-04-23 14:13:31 -07:00
* @ param int | mixed $to - user ID
* @ return array of blocked users as user IDs
2009-12-10 20:40:39 +00:00
*/
2006-12-02 04:36:16 +00:00
function block_get ( $to = USERID )
{
2013-03-26 12:16:55 +01:00
$sql = e107 :: getDb ();
2006-12-02 04:36:16 +00:00
$ret = array ();
2009-12-10 20:40:39 +00:00
$to = intval ( $to ); // Precautionary
2013-03-26 12:16:55 +01:00
if ( $sql -> select ( 'private_msg_block' , 'pm_block_from' , 'pm_block_to = ' . $to ))
2006-12-02 04:36:16 +00:00
{
2016-02-14 12:15:55 -08:00
while ( $row = $sql -> fetch ())
2006-12-02 04:36:16 +00:00
{
$ret [] = $row [ 'pm_block_from' ];
}
}
return $ret ;
}
2009-12-10 20:40:39 +00:00
/**
* Get list of users blocked from sending to a specific user ID .
2009-12-17 22:47:20 +00:00
*
2009-12-10 20:40:39 +00:00
* @ param integer $to - user ID
2009-12-17 22:47:20 +00:00
*
2009-12-10 20:40:39 +00:00
* @ return array of blocked users , including specific user info
*/
function block_get_user ( $to = USERID )
{
2013-03-26 12:16:55 +01:00
$sql = e107 :: getDb ();
2009-12-10 20:40:39 +00:00
$ret = array ();
$to = intval ( $to ); // Precautionary
2013-03-26 12:16:55 +01:00
if ( $sql -> gen ( 'SELECT pm.*, u.user_name FROM `#private_msg_block` AS pm LEFT JOIN `#user` AS u ON `pm`.`pm_block_from` = `u`.`user_id` WHERE pm_block_to = ' . $to ))
2009-12-10 20:40:39 +00:00
{
2016-02-14 12:15:55 -08:00
while ( $row = $sql -> fetch ())
2009-12-10 20:40:39 +00:00
{
$ret [] = $row ;
}
}
return $ret ;
}
2009-12-17 22:47:20 +00:00
/**
* Add a user block
*
* @ param int $from - sender to block
* @ param int $to - user doing the blocking
*
* @ return string result message
*/
2006-12-02 04:36:16 +00:00
function block_add ( $from , $to = USERID )
{
2013-03-26 12:16:55 +01:00
$sql = e107 :: getDb ();
2009-12-17 22:47:20 +00:00
$from = intval ( $from );
2013-03-26 12:16:55 +01:00
if ( $sql -> select ( 'user' , 'user_name, user_perms' , 'user_id = ' . $from ))
2006-12-02 04:36:16 +00:00
{
2013-03-26 12:16:55 +01:00
$uinfo = $sql -> fetch ();
2009-12-17 22:47:20 +00:00
if (( $uinfo [ 'user_perms' ] == '0' ) || ( $uinfo [ 'user_perms' ] == '0.' ))
{ // Don't allow block of main admin
return LAN_PM_64 ;
}
2007-05-05 20:06:47 +00:00
2013-03-26 12:16:55 +01:00
if ( ! $sql -> count ( 'private_msg_block' , '(*)' , 'WHERE pm_block_from = ' . $from . " AND pm_block_to = ' " . e107 :: getParser () -> toDB ( $to ) . " ' " ))
2006-12-02 04:36:16 +00:00
{
2013-03-26 12:16:55 +01:00
if ( $sql -> insert ( 'private_msg_block' , array (
2010-01-04 21:35:38 +00:00
'pm_block_from' => $from ,
'pm_block_to' => $to ,
'pm_block_datestamp' => time ()
)))
2006-12-02 04:36:16 +00:00
{
return str_replace ( '{UNAME}' , $uinfo [ 'user_name' ], LAN_PM_47 );
}
else
{
return LAN_PM_48 ;
}
}
else
{
return str_replace ( '{UNAME}' , $uinfo [ 'user_name' ], LAN_PM_49 );
}
}
else
{
return LAN_PM_17 ;
}
}
2009-12-17 22:47:20 +00:00
/**
* Delete user block
*
* @ param int $from - sender to block
* @ param int $to - user doing the blocking
*
* @ return string result message
*/
2006-12-02 04:36:16 +00:00
function block_del ( $from , $to = USERID )
{
2013-03-26 12:16:55 +01:00
$sql = e107 :: getDb ();
2009-12-10 20:40:39 +00:00
$from = intval ( $from );
2013-03-26 12:16:55 +01:00
if ( $sql -> select ( 'user' , 'user_name' , 'user_id = ' . $from ))
2006-12-02 04:36:16 +00:00
{
2013-03-26 12:16:55 +01:00
$uinfo = $sql -> fetch ();
if ( $sql -> select ( 'private_msg_block' , 'pm_block_id' , 'pm_block_from = ' . $from . ' AND pm_block_to = ' . intval ( $to )))
2006-12-02 04:36:16 +00:00
{
2013-03-26 12:16:55 +01:00
$row = $sql -> fetch ();
if ( $sql -> delete ( 'private_msg_block' , 'pm_block_id = ' . intval ( $row [ 'pm_block_id' ])))
2006-12-02 04:36:16 +00:00
{
2009-12-10 20:40:39 +00:00
return str_replace ( '{UNAME}' , $uinfo [ 'user_name' ], LAN_PM_44 );
2006-12-02 04:36:16 +00:00
}
else
{
return LAN_PM_45 ;
}
}
else
{
2009-12-10 20:40:39 +00:00
return str_replace ( '{UNAME}' , $uinfo [ 'user_name' ], LAN_PM_46 );
2006-12-02 04:36:16 +00:00
}
}
else
{
return LAN_PM_17 ;
}
}
2009-12-17 22:47:20 +00:00
/**
* Get user ID matching a name
*
* @ param string var - name to match
*
* @ return boolean | array - FALSE if no match , array of user info if found
*/
2006-12-02 04:36:16 +00:00
function pm_getuid ( $var )
{
2013-03-26 12:16:55 +01:00
$sql = e107 :: getDb ();
2016-04-03 14:37:37 -07:00
if ( is_numeric ( $var ))
{
$where = " user_id = " . intval ( $var );
}
else
{
$var = strip_if_magic ( $var );
$var = str_replace ( " ' " , ''' , trim ( $var )); // Display name uses entities for apostrophe
$where = " user_name LIKE ' " . $sql -> escape ( $var , FALSE ) . " ' " ;
}
if ( $sql -> select ( 'user' , 'user_id, user_name, user_class, user_email' , $where ))
2006-12-02 04:36:16 +00:00
{
2013-03-26 12:16:55 +01:00
$row = $sql -> fetch ();
2006-12-02 04:36:16 +00:00
return $row ;
}
2016-04-03 14:37:37 -07:00
return false ;
2006-12-02 04:36:16 +00:00
}
2009-12-17 22:47:20 +00:00
/**
* Get list of users in class
*
* @ param int $class - class ID
*
* @ return boolean | array - FALSE on error / none found , else array of user information arrays
*/
2006-12-02 04:36:16 +00:00
function get_users_inclass ( $class )
{
2019-04-18 10:25:00 -07:00
return e107 :: getUserClass () -> getUsersInClass ( $class , 'user_name, user_email, user_class' );
/* $sql = e107 :: getDb ();
2016-04-25 09:31:55 -07:00
2006-12-02 04:36:16 +00:00
if ( $class == e_UC_MEMBER )
{
2009-12-17 22:47:20 +00:00
$qry = " SELECT user_id, user_name, user_email, user_class FROM `#user` WHERE 1 " ;
2006-12-02 04:36:16 +00:00
}
elseif ( $class == e_UC_ADMIN )
{
2009-12-17 22:47:20 +00:00
$qry = " SELECT user_id, user_name, user_email, user_class FROM `#user` WHERE user_admin = 1 " ;
2006-12-02 04:36:16 +00:00
}
elseif ( $class )
{
2013-03-25 18:48:48 +01:00
$regex = " (^|,)( " . e107 :: getParser () -> toDB ( $class ) . " )(,| $ ) " ;
2009-12-17 22:47:20 +00:00
$qry = " SELECT user_id, user_name, user_email, user_class FROM `#user` WHERE user_class REGEXP ' { $regex } ' " ;
2006-12-02 04:36:16 +00:00
}
2016-04-23 14:13:31 -07:00
if ( ! empty ( $qry ) && $sql -> gen ( $qry ))
2006-12-02 04:36:16 +00:00
{
2013-03-26 12:16:55 +01:00
$ret = $sql -> db_getList ();
2006-12-02 04:36:16 +00:00
return $ret ;
}
2019-04-18 10:25:00 -07:00
return FALSE ; */
2006-12-02 04:36:16 +00:00
}
2009-12-17 22:47:20 +00:00
2016-04-25 09:31:55 -07:00
/**
* Check permission to send a PM to someone .
* @ param int $uid user_id of the person to send to
* @ return bool
*/
function canSendTo ( $uid )
{
if ( empty ( $uid ))
{
return false ;
}
2016-05-05 19:20:17 -07:00
if ( ! empty ( $this -> pmPrefs [ 'vip_class' ]))
{
if ( check_class ( $this -> pmPrefs [ 'vip_class' ], null , $uid ) && ! check_class ( $this -> pmPrefs [ 'vip_class' ]))
{
return false ;
}
}
2016-04-25 09:31:55 -07:00
$user = e107 :: user ( $uid );
$uclass = explode ( " , " , $user [ 'user_class' ]);
if ( $this -> pmPrefs [ 'send_to_class' ] == 'matchclass' )
{
$tmp = explode ( " , " , USERCLASS );
$result = array_intersect ( $uclass , $tmp );
return ! empty ( $result );
}
return in_array ( $this -> pmPrefs [ 'send_to_class' ], $uclass );
}
2009-12-17 22:47:20 +00:00
/**
* Get inbox - up to $limit messages from $from
*
* @ param int $uid - user ID
* @ param int $from - first message
* @ param int $limit - number of messages
*
* @ return boolean | array - FALSE if none found or error , array of PMs if available
*/
2006-12-02 04:36:16 +00:00
function pm_get_inbox ( $uid = USERID , $from = 0 , $limit = 10 )
{
2013-03-26 12:16:55 +01:00
$sql = e107 :: getDb ();
2009-12-17 22:47:20 +00:00
$ret = array ();
2010-01-06 20:09:58 +00:00
$total_messages = 0 ;
2009-12-17 22:47:20 +00:00
$uid = intval ( $uid );
$limit = intval ( $limit );
if ( $limit < 2 ) { $limit = 10 ; }
$from = intval ( $from );
2010-01-06 20:09:58 +00:00
$qry = "
SELECT SQL_CALC_FOUND_ROWS pm .* , u . user_image , u . user_name FROM `#private_msg` AS pm
LEFT JOIN `#user` AS u ON u . user_id = pm . pm_from
WHERE pm . pm_to = '{$uid}' AND pm . pm_read_del = 0
ORDER BY pm . pm_sent DESC
LIMIT " . $from . " , " . $limit . "
" ;
2016-03-30 19:10:00 -07:00
2013-03-26 12:16:55 +01:00
if ( $sql -> gen ( $qry ))
2006-12-02 04:36:16 +00:00
{
2016-03-30 19:10:00 -07:00
$total_messages = $sql -> foundRows (); // Total number of messages
2013-03-26 12:16:55 +01:00
$ret [ 'messages' ] = $sql -> db_getList ();
2006-12-02 04:36:16 +00:00
}
2016-03-30 19:10:00 -07:00
2010-01-06 20:09:58 +00:00
$ret [ 'total_messages' ] = $total_messages ; // Should always be defined
return $ret ;
2006-12-02 04:36:16 +00:00
}
2009-12-17 22:47:20 +00:00
/**
* Get outbox - up to $limit messages from $from
*
* @ param int $uid - user ID
* @ param int $from - first message
* @ param int $limit - number of messages
*
* @ return boolean | array - FALSE if none found or error , array of PMs if available
*/
2006-12-02 04:36:16 +00:00
function pm_get_outbox ( $uid = USERID , $from = 0 , $limit = 10 )
{
2013-01-31 19:13:50 -08:00
$sql = e107 :: getDb ();
2010-01-06 20:09:58 +00:00
$ret = array ();
$total_messages = 0 ;
2009-12-17 22:47:20 +00:00
$uid = intval ( $uid );
$limit = intval ( $limit );
if ( $limit < 2 ) { $limit = 10 ; }
$from = intval ( $from );
2010-01-06 20:09:58 +00:00
$qry = "
SELECT SQL_CALC_FOUND_ROWS pm .* , u . user_image , u . user_name FROM #private_msg AS pm
LEFT JOIN #user AS u ON u.user_id = pm.pm_to
2014-08-02 13:26:21 +02:00
WHERE pm . pm_from = '{$uid}' AND pm . pm_sent_del = '0'
2016-04-03 14:37:37 -07:00
2010-01-06 20:09:58 +00:00
ORDER BY pm . pm_sent DESC
LIMIT " . $from .', '. $limit ;
2013-01-31 19:13:50 -08:00
2013-03-26 12:16:55 +01:00
if ( $sql -> gen ( $qry ))
2006-12-02 04:36:16 +00:00
{
2013-01-31 19:13:50 -08:00
$total_messages = $sql -> total_results ; // Total number of messages
$ret [ 'messages' ] = $sql -> db_getList ();
2006-12-02 04:36:16 +00:00
}
2010-01-06 20:09:58 +00:00
$ret [ 'total_messages' ] = $total_messages ;
2006-12-02 04:36:16 +00:00
return $ret ;
}
2009-12-17 22:47:20 +00:00
/**
* Send a file down to the user
*
* @ param int $pmid - PM ID
* @ param string $filenum - attachment number within the list associated with the PM
*
* @ return none
*
*/
2006-12-02 04:36:16 +00:00
function send_file ( $pmid , $filenum )
{
$pm_info = $this -> pm_get ( $pmid );
2016-03-30 19:10:00 -07:00
2006-12-02 04:36:16 +00:00
$attachments = explode ( chr ( 0 ), $pm_info [ 'pm_attachments' ]);
2016-03-30 19:10:00 -07:00
2006-12-02 04:36:16 +00:00
if ( ! isset ( $attachments [ $filenum ]))
{
2016-03-30 19:10:00 -07:00
return false ;
2006-12-02 04:36:16 +00:00
}
2016-03-30 19:10:00 -07:00
2006-12-02 04:36:16 +00:00
$fname = $attachments [ $filenum ];
list ( $timestamp , $fromid , $rand , $file ) = explode ( " _ " , $fname , 4 );
2016-03-30 19:10:00 -07:00
$filename = false ; // getcwd()."/attachments/{$fname}";
$pathList = array ();
$pathList [] = e_PLUGIN . " pm/attachments/ " ; // getcwd()."/attachments/"; // legacy path.
$pathList [] = e107 :: getFile () -> getUserDir ( $fromid , false , 'attachments' ); // new media path.
foreach ( $pathList as $path )
2006-12-02 04:36:16 +00:00
{
2016-03-30 19:10:00 -07:00
$tPath = $path . $fname ;
if ( is_file ( $tPath ))
{
$filename = $tPath ;
break ;
}
}
if ( empty ( $filename ) || ! is_file ( $filename ))
{
return false ;
2006-12-02 04:36:16 +00:00
}
2016-03-30 19:10:00 -07:00
if ( $fromid != $pm_info [ 'pm_from' ])
2006-12-02 04:36:16 +00:00
{
2016-03-30 19:10:00 -07:00
return false ;
2006-12-02 04:36:16 +00:00
}
2016-03-30 19:10:00 -07:00
// e107::getFile()->send($filename); // limited to Media and system folders. Won't work for legacy plugin path.
// exit;
2006-12-02 04:36:16 +00:00
@ set_time_limit ( 10 * 60 );
@ e107_ini_set ( " max_execution_time " , 10 * 60 );
while ( @ ob_end_clean ()); // kill all output buffering else it eats server resources
if ( connection_status () == 0 )
{
if ( strstr ( $_SERVER [ 'HTTP_USER_AGENT' ], " MSIE " )) {
$file = preg_replace ( '/\./' , '%2e' , $file , substr_count ( $file , '.' ) - 1 );
}
if ( isset ( $_SERVER [ 'HTTP_RANGE' ]))
{
$seek = intval ( substr ( $_SERVER [ 'HTTP_RANGE' ] , strlen ( 'bytes=' )));
}
$bufsize = 2048 ;
ignore_user_abort ( true );
$data_len = filesize ( $filename );
if ( $seek > ( $data_len - 1 )) $seek = 0 ;
$res =& fopen ( $filename , 'rb' );
if ( $seek )
{
fseek ( $res , $seek );
}
$data_len -= $seek ;
header ( " Expires: 0 " );
header ( " Cache-Control: max-age=30 " );
header ( " Content-Type: application/force-download " );
header ( " Content-Disposition: attachment; filename= { $file } " );
header ( " Content-Length: { $data_len } " );
header ( " Pragma: public " );
if ( $seek )
{
header ( " Accept-Ranges: bytes " );
header ( " HTTP/1.0 206 Partial Content " );
header ( " status: 206 Partial Content " );
header ( " Content-Range: bytes { $seek } - " . ( $data_len - 1 ) . " / { $data_len } " );
}
while ( ! connection_aborted () && $data_len > 0 )
{
echo fread ( $res , $bufsize );
$data_len -= $bufsize ;
}
fclose ( $res );
}
}
2014-03-26 15:28:37 -07:00
2016-03-30 19:10:00 -07:00
2014-03-26 15:28:37 -07:00
function updateTemplate ( $template )
{
$array = array (
'FORM_TOUSER' => 'PM_FORM_TOUSER' ,
'FORM_TOCLASS' => 'PM_FORM_TOCLASS' ,
'FORM_SUBJECT' => 'PM_FORM_SUBJECT' ,
2014-08-15 15:56:21 +02:00
'FORM_MESSAGE' => 'PM_FORM_MESSAGE' ,
'EMOTES' => 'PM_EMOTES' ,
'ATTACHMENT' => 'PM_ATTACHMENT' ,
2014-03-26 15:28:37 -07:00
'RECEIPT' => 'PM_RECEIPT' ,
'INBOX_TOTAL' => 'PM_INBOX_TOTAL' ,
'INBOX_UNREAD' => 'PM_INBOX_UNREAD' ,
'INBOX_FILLED' => 'PM_INBOX_FILLED' ,
'OUTBOX_TOTAL' => 'PM_OUTBOX_TOTAL' ,
'OUTBOX_UNREAD' => 'PM_OUTBOX_UNREAD' ,
'OUTBOX_FILLED' => 'PM_OUTBOX_FILLED' ,
'SEND_PM_LINK' => 'PM_SEND_PM_LINK' ,
'NEWPM_ANIMATE' => 'PM_NEWPM_ANIMATE' ,
'BLOCKED_SENDERS_MANAGE' => 'PM_BLOCKED_SENDERS_MANAGE' ,
'DELETE_BLOCKED_SELECTED' => 'DELETE_BLOCKED_SELECTED'
);
foreach ( $array as $old => $new )
{
$template = str_replace ( " { " . $old . " } " , " { " . $new . " } " , $template );
}
return $template ;
}
2017-04-11 20:47:09 +01:00
}