2013-02-20 21:11:17 +00:00
< ? php
/*
* e107 website system
*
* Copyright ( C ) 2008 - 2013 e107 Inc ( e107 . org )
* Released under the terms and conditions of the
* GNU General Public License ( http :// www . gnu . org / licenses / gpl . txt )
*
*/
/**
* @ package e107
* @ subpackage e107_handlers
*
* Handler for 'notify' events - sends email notifications to the appropriate user groups
*/
if ( ! defined ( 'e107_INIT' )) { exit ; }
class notify
{
2015-02-15 16:06:58 -08:00
public $notify_prefs ;
2013-02-20 21:11:17 +00:00
2015-02-15 16:06:58 -08:00
function __construct ()
2013-02-20 21:11:17 +00:00
{
2017-01-23 09:41:23 -08:00
e107 :: includeLan ( e_LANGUAGEDIR . e_LANGUAGE . '/lan_notify.php' );
2015-03-07 16:30:46 -08:00
if ( empty ( $this -> notify_prefs ))
{
$this -> notify_prefs = e107 :: getConfig ( 'notify' ) -> getPref ();
}
2015-02-15 16:06:58 -08:00
}
/**
* Register core and plugin notification events .
*/
public function registerEvents ()
{
2015-03-07 16:30:46 -08:00
$active = e107 :: getConfig () -> get ( 'notify' );
2016-02-12 19:28:35 -08:00
if ( empty ( $active ) && defset ( 'e_PAGE' ) == 'notify.php' )
2015-03-07 16:30:46 -08:00
{
e107 :: getMessage () -> addDebug ( 'Notify is disabled!' );
return false ;
}
2015-02-15 16:06:58 -08:00
2015-03-07 16:30:46 -08:00
$e_event = e107 :: getEvent ();
2015-02-15 16:06:58 -08:00
2013-02-20 21:11:17 +00:00
if ( varset ( $this -> notify_prefs [ 'event' ]))
{
foreach ( $this -> notify_prefs [ 'event' ] as $id => $status )
{
2015-02-15 16:06:58 -08:00
$include = null ;
2015-02-13 02:30:31 -08:00
if ( $status [ 'class' ] != e_UC_NOBODY ) // 255;
2013-02-20 21:11:17 +00:00
{
2015-02-15 16:06:58 -08:00
if ( varset ( $status [ 'include' ])) // Plugin
2013-06-18 20:21:10 -07:00
{
$include = e_PLUGIN . $status [ 'include' ] . " /e_notify.php " ;
2015-02-15 16:06:58 -08:00
2014-02-04 06:49:25 -08:00
if ( varset ( $status [ 'legacy' ]) != 1 )
2013-06-18 20:21:10 -07:00
{
$class = $status [ 'include' ] . " _notify " ;
$method = $id ;
2015-02-15 16:06:58 -08:00
$e_event -> register ( $id , array ( $class , $method ), $include );
2013-06-18 20:21:10 -07:00
}
else
{
2015-02-15 16:06:58 -08:00
$e_event -> register ( $id , 'notify_' . $id , $include );
2013-06-18 20:21:10 -07:00
}
}
2015-02-15 16:06:58 -08:00
else // core
2013-06-18 20:21:10 -07:00
{
2015-02-15 16:06:58 -08:00
if ( method_exists ( $this , 'notify_' . $id )) // as found below.
2015-02-13 02:30:31 -08:00
{
2015-02-15 16:06:58 -08:00
$e_event -> register ( $id , array ( 'notify' , 'notify_' . $id ));
2015-02-13 02:30:31 -08:00
}
else
{
2015-02-15 16:06:58 -08:00
$e_event -> register ( $id , array ( 'notify' , 'generic' )); // use generic notification.
}
2013-06-18 20:21:10 -07:00
}
2015-02-15 16:06:58 -08:00
2013-02-20 21:11:17 +00:00
}
}
}
2015-02-15 16:06:58 -08:00
// e107::getEvent()->debug();
2015-02-13 02:30:31 -08:00
}
/**
* Generic Notification method when none defined .
*/
function generic ( $data , $id )
{
$message = print_a ( $data , true );
$this -> send ( $id , 'Event Triggered: ' . $id , $message );
2013-02-20 21:11:17 +00:00
}
2015-03-07 16:30:46 -08:00
2013-02-20 21:11:17 +00:00
/**
* Send an email notification following an event .
*
* The email is sent via a common interface , which will send immediately for small numbers of recipients , and queue for larger .
*
* @ param string $id - identifies event actions
* @ param string $subject - subject for email
* @ param string $message - email message body
2015-02-15 16:06:58 -08:00
* @ return void
2013-02-20 21:11:17 +00:00
*
* @ todo handle 'everyone except' clauses ( email address filter done )
* @ todo set up pref to not notify originator of event which caused notify ( see $blockOriginator )
*/
2015-05-12 01:45:00 -07:00
function send ( $id , $subject , $message , $media = array ())
2013-02-20 21:11:17 +00:00
{
2015-03-07 16:30:46 -08:00
2013-03-24 18:59:18 +01:00
$tp = e107 :: getParser ();
$sql = e107 :: getDb ();
2013-02-20 21:11:17 +00:00
2015-05-13 01:22:05 -07:00
$subject = $tp -> toEmail ( $subject );
2015-05-12 01:45:00 -07:00
$message = $tp -> replaceConstants ( $message , " full " );
// $message = $tp->toEmail($message);
2013-02-20 21:11:17 +00:00
$emailFilter = '' ;
$notifyTarget = $this -> notify_prefs [ 'event' ][ $id ][ 'class' ];
2015-05-12 01:45:00 -07:00
2013-02-20 21:11:17 +00:00
if ( $notifyTarget == '-email' )
{
$emailFilter = $this -> notify_prefs [ 'event' ][ $id ][ 'email' ];
}
2015-05-12 01:45:00 -07:00
2013-02-20 21:11:17 +00:00
$blockOriginator = FALSE ; // TODO: set this using a pref
$recipients = array ();
2015-03-07 16:30:46 -08:00
if ( $notifyTarget == 'email' ) // Single email address - that can always go immediately
{
2013-02-20 21:11:17 +00:00
if ( ! $blockOriginator || ( $this -> notify_prefs [ 'event' ][ $id ][ 'email' ] != USEREMAIL ))
{
$recipients [] = array (
2015-05-13 02:38:31 -07:00
'mail_recipient_email' => $this -> notify_prefs [ 'event' ][ $id ][ 'email' ],
'mail_target_info' => array (
'SUBJECT' => $subject ,
'DATE_SHORT' => $tp -> toDate ( time (), 'short' ),
'DATE_LONG' => $tp -> toDate ( time (), 'long' ),
)
);
2013-02-20 21:11:17 +00:00
}
}
2015-05-12 01:45:00 -07:00
elseif ( is_numeric ( $notifyTarget ))
2013-02-20 21:11:17 +00:00
{
switch ( $notifyTarget )
{
case e_UC_MAINADMIN :
$qry = " `user_admin` = 1 AND `user_perms` = '0' AND `user_ban` = 0 " ;
break ;
case e_UC_ADMIN :
$qry = " `user_admin` = 1 AND `user_ban` = 0 " ;
break ;
case e_UC_MEMBER :
$qry = " `user_ban` = 0 " ;
break ;
default :
2015-05-12 01:45:00 -07:00
$qry = " user_ban = 0 AND user_class REGEXP '(^|,)( " . $notifyTarget . " )(,| $ )' " ;
2013-02-20 21:11:17 +00:00
break ;
}
2015-05-12 01:45:00 -07:00
$qry = 'SELECT user_id,user_name,user_email,user_join,user_lastvisit FROM `#user` WHERE ' . $qry ;
2013-02-20 21:11:17 +00:00
if ( $blockOriginator )
{
$qry .= ' AND `user_id` != ' . USERID ;
}
2015-05-12 01:45:00 -07:00
2015-03-07 16:30:46 -08:00
if ( false !== ( $count = $sql -> gen ( $qry )))
2013-02-20 21:11:17 +00:00
{
// Now add email addresses to the list
2016-02-14 12:15:55 -08:00
while ( $row = $sql -> fetch ())
2013-02-20 21:11:17 +00:00
{
if ( $row [ 'user_email' ] != $emailFilter )
{
2015-05-12 01:45:00 -07:00
$unsubscribe = array ( 'date' => $row [ 'user_join' ], 'email' => $row [ 'user_email' ], 'id' => $row [ 'user_id' ], 'plugin' => 'user' , 'userclass' => $notifyTarget );
$urlQuery = http_build_query ( $unsubscribe , null , '&' );
2015-05-13 01:22:05 -07:00
$exclude = array ( e_UC_MEMBER , e_UC_ADMIN , e_UC_MAINADMIN ); // no unsubscribing from these classes.
2015-05-12 01:45:00 -07:00
$unsubUrl = SITEURL . " unsubscribe.php?id= " . base64_encode ( $urlQuery );
$unsubMessage = " This message was sent to " . $row [ 'user_email' ] . " . If you don't want to receive these emails in the future, please <a href=' " . $unsubUrl . " '>unsubscribe</a>. " ;
$recipients [] = array (
'mail_recipient_id' => $row [ 'user_id' ],
'mail_recipient_name' => $row [ 'user_name' ], // Should this use realname?
'mail_recipient_email' => $row [ 'user_email' ],
'mail_target_info' => array (
2015-05-13 01:22:05 -07:00
'USERID' => $row [ 'user_id' ],
'DISPLAYNAME' => $row [ 'user_name' ],
'SUBJECT' => $subject ,
'USERNAME' => $row [ 'user_name' ],
'USERLASTVISIT' => $row [ 'user_lastvisit' ],
'UNSUBSCRIBE' => ( ! in_array ( $notifyTarget , $exclude )) ? $unsubUrl : '' ,
'UNSUBSCRIBE_MESSAGE' => ( ! in_array ( $notifyTarget , $exclude )) ? $unsubMessage : '' ,
'USERCLASS' => $notifyTarget ,
'DATE_SHORT' => $tp -> toDate ( time (), 'short' ),
'DATE_LONG' => $tp -> toDate ( time (), 'long' ),
2015-05-12 01:45:00 -07:00
)
);
2013-02-20 21:11:17 +00:00
}
}
}
2015-03-07 16:30:46 -08:00
}
2016-03-15 10:45:33 -07:00
if ( E107_DEBUG_LEVEL > 0 || deftrue ( 'e_DEBUG_NOTIFY' ))
2015-03-07 16:30:46 -08:00
{
$data = array ( 'id' => $id , 'subject' => $subject , 'recipients' => $recipients , 'prefs' => $this -> notify_prefs [ 'event' ][ $id ], 'message' => $message );
2015-05-13 17:45:24 -07:00
e107 :: getMessage () -> addDebug ( " <b>Mailing is simulated only while in DEBUG mode.</b> " );
2015-03-07 16:30:46 -08:00
e107 :: getMessage () -> addDebug ( print_a ( $data , true ));
e107 :: getLog () -> add ( 'Notify Debug' , $data , E_LOG_INFORMATIVE , " NOTIFY_DBG " );
return ;
2013-02-20 21:11:17 +00:00
}
2015-05-13 17:45:24 -07:00
$siteadminemail = e107 :: getPref ( 'siteadminemail' );
$siteadmin = e107 :: getPref ( 'siteadmin' );
2015-03-07 16:30:46 -08:00
2013-02-20 21:11:17 +00:00
if ( count ( $recipients ))
{
require_once ( e_HANDLER . 'mail_manager_class.php' );
$mailer = new e107MailManager ;
// Create the mail body
$mailData = array (
2015-05-12 01:45:00 -07:00
'mail_total_count' => count ( $recipients ),
2014-08-26 15:49:24 -07:00
'mail_content_status' => MAIL_STATUS_TEMP ,
2015-05-25 13:37:17 -07:00
'mail_create_app' => 'core' ,
2014-08-26 15:49:24 -07:00
'mail_title' => 'NOTIFY' ,
'mail_subject' => $subject ,
2015-05-13 17:45:24 -07:00
'mail_sender_email' => e107 :: getPref ( 'replyto_email' , $siteadminemail ),
'mail_sender_name' => e107 :: getPref ( 'replyto_name' , $siteadmin ),
2014-08-26 15:49:24 -07:00
'mail_notify_complete' => 0 , // NEVER notify when this email sent!!!!!
'mail_body' => $message ,
2015-05-12 01:45:00 -07:00
'template' => 'notify' ,
'mail_send_style' => 'notify'
2013-02-20 21:11:17 +00:00
);
2015-05-12 01:45:00 -07:00
if ( ! empty ( $media ) && is_array ( $media ))
{
foreach ( $media as $k => $v )
{
$mailData [ 'mail_media' ][ $k ] = array ( 'path' => $v );
}
}
2014-08-26 15:49:24 -07:00
2015-05-12 01:45:00 -07:00
$result = $mailer -> sendEmails ( 'notify' , $mailData , $recipients );
2015-05-13 01:22:05 -07:00
2015-03-07 16:30:46 -08:00
e107 :: getLog () -> e_log_event ( 10 , - 1 , 'NOTIFY' , $subject , $message , FALSE , LOG_TO_ROLLING );
}
else
{
$data = array ( 'qry' => $qry , 'error' => 'No recipients' );
e107 :: getLog () -> add ( 'Notify Debug' , $data , E_LOG_WARNING_ , " NOTIFY_DBG " );
2013-02-20 21:11:17 +00:00
}
}
2015-02-15 16:06:58 -08:00
// ---------------------------------------
2013-02-20 21:11:17 +00:00
2015-02-15 16:06:58 -08:00
function notify_usersup ( $data )
2013-02-20 21:11:17 +00:00
{
2015-02-15 16:06:58 -08:00
$message = " " ;
foreach ( $data as $key => $value )
2013-02-20 21:11:17 +00:00
{
2015-02-15 16:06:58 -08:00
if ( $key != " password1 " && $key != " password2 " && $key != " email_confirm " && $key != " register " )
2013-02-20 21:11:17 +00:00
{
2015-02-15 16:06:58 -08:00
if ( is_array ( $value )) // show user-extended values.
2013-02-20 21:11:17 +00:00
{
2015-02-15 16:06:58 -08:00
foreach ( $value as $k => $v )
{
$message .= str_replace ( " user_ " , " " , $k ) . ': ' . $v . '<br />' ;
}
}
else
{
$message .= $key . ': ' . $value . '<br />' ;
2013-02-20 21:11:17 +00:00
}
}
}
2015-02-15 16:06:58 -08:00
$this -> send ( 'usersup' , NT_LAN_US_1 , $message );
2013-02-20 21:11:17 +00:00
}
2015-02-15 16:06:58 -08:00
/**
* @ param $data
*/
function notify_userveri ( $data )
{
$msgtext = NT_LAN_UV_2 . $data [ 'user_id' ] . " \n " ;
$msgtext .= NT_LAN_UV_3 . $data [ 'user_loginname' ] . " \n " ;
$msgtext .= NT_LAN_UV_4 . e107 :: getIPHandler () -> getIP ( FALSE );
2013-02-20 21:11:17 +00:00
2015-02-15 16:06:58 -08:00
$this -> send ( 'userveri' , NT_LAN_UV_1 , $msgtext );
}
2015-02-13 02:30:31 -08:00
2015-02-15 16:06:58 -08:00
function notify_login ( $data )
{
$message = " " ;
foreach ( $data as $key => $value )
{
$message .= $key . ': ' . $value . '<br />' ;
}
$this -> send ( 'login' , NT_LAN_LI_1 , $message );
2013-02-20 21:11:17 +00:00
}
2015-02-15 16:06:58 -08:00
function notify_logout ()
{
$this -> send ( 'logout' , NT_LAN_LO_1 , USERID . '. ' . USERNAME . ' ' . NT_LAN_LO_2 );
}
2013-02-20 21:11:17 +00:00
2015-02-15 16:06:58 -08:00
function notify_flood ( $data )
{
2015-10-24 19:41:23 +02:00
$this -> send ( 'flood' , NT_LAN_FL_1 , NT_LAN_FL_2 . ': ' . e107 :: getIPHandler () -> ipDecode ( $data , TRUE ));
2015-02-15 16:06:58 -08:00
}
2013-02-20 21:11:17 +00:00
2015-02-15 16:06:58 -08:00
function notify_subnews ( $data )
{
$message = " " ;
foreach ( $data as $key => $value )
{
$message .= $key . ': ' . $value . '<br />' ;
}
$this -> send ( 'subnews' , NT_LAN_SN_1 , $message );
2013-02-20 21:11:17 +00:00
}
2015-02-15 16:06:58 -08:00
function notify_newspost ( $data )
{
$message = '<b>' . $data [ 'news_title' ] . '</b>' ;
if ( vartrue ( $data [ 'news_summary' ])) $message .= '<br /><br />' . $data [ 'news_summary' ];
if ( vartrue ( $data [ 'news_body' ])) $message .= '<br /><br />' . $data [ 'news_body' ];
if ( vartrue ( $data [ 'news_extended' ])) $message .= '<br /><br />' . $data [ 'news_extended' ];
$this -> send ( 'newspost' , $data [ 'news_title' ], e107 :: getParser () -> text_truncate ( e107 :: getParser () -> toDB ( $message ), 400 , '...' ));
}
2013-02-20 21:11:17 +00:00
2015-02-15 16:06:58 -08:00
function notify_newsupd ( $data )
{
$message = '<b>' . $data [ 'news_title' ] . '</b>' ;
if ( vartrue ( $data [ 'news_summary' ])) $message .= '<br /><br />' . $data [ 'news_summary' ];
if ( vartrue ( $data [ 'news_body' ])) $message .= '<br /><br />' . $data [ 'news_body' ];
if ( vartrue ( $data [ 'news_extended' ])) $message .= '<br /><br />' . $data [ 'news_extended' ];
$this -> send ( 'newsupd' , NT_LAN_NU_1 . ': ' . $data [ 'news_title' ], e107 :: getParser () -> text_truncate ( e107 :: getParser () -> toDB ( $message ), 400 , '...' ));
}
2013-02-20 21:11:17 +00:00
2015-02-15 16:06:58 -08:00
function notify_newsdel ( $data )
{
$this -> send ( 'newsdel' , NT_LAN_ND_1 , NT_LAN_ND_2 . ': ' . $data );
}
2013-02-20 21:11:17 +00:00
2015-02-15 16:06:58 -08:00
function notify_maildone ( $data )
{
$message = '<b>' . $data [ 'mail_subject' ] . '</b><br /><br />' . $data [ 'mail_body' ];
$this -> send ( 'maildone' , NT_LAN_ML_1 . ': ' . $data [ 'mail_subject' ], $message );
}
2013-02-20 21:11:17 +00:00
2015-02-15 16:06:58 -08:00
function notify_fileupload ( $data )
{
$message = '<b>' . $data [ 'upload_name' ] . '</b><br /><br />' . $data [ 'upload_description' ] . '<br /><br />' . $data [ 'upload_size' ] . '<br /><br />' . $data [ 'upload_user' ];
$this -> send ( 'fileupload' , $data [ 'upload_name' ], $message );
}
2013-02-20 21:11:17 +00:00
2015-02-15 16:06:58 -08:00
2015-05-12 01:45:00 -07:00
function notify_admin_news_created ( $data )
{
$this -> notify_newspost ( $data );
}
2015-05-13 01:22:05 -07:00
function notify_admin_news_notify ( $data )
2015-05-12 01:45:00 -07:00
{
$tp = e107 :: getParser ();
2015-05-13 01:22:05 -07:00
$sql = e107 :: getDb ();
$author = $sql -> retrieve ( 'user' , 'user_name' , 'user_id = ' . intval ( $data [ 'news_author' ]) . " LIMIT 1 " );
2015-05-12 01:45:00 -07:00
2015-05-13 01:22:05 -07:00
$template = " <h4><a href=' { NEWS_URL}'> { NEWS_TITLE}</a></h4>
< div class = 'summary' > { NEWS_SUMMARY } </ div >
2017-02-13 08:03:40 +01:00
< div class = 'author' > " .LAN_POSTED_BY. " : { NEWS_AUTHOR } </ div >
< div >< a class = 'btn btn-primary' href = '{NEWS_URL}' > " .LAN_CLICK_TO_VIEW. " </ a ></ div >
2015-05-13 01:22:05 -07:00
" ;
2015-05-12 01:45:00 -07:00
2015-05-13 01:22:05 -07:00
$shortcodes = array (
2015-05-21 21:49:32 -07:00
'NEWS_URL' => e107 :: getUrl () -> create ( 'news/view/item' , $data , 'full=1&encode=0' ),
2015-05-13 01:22:05 -07:00
'NEWS_TITLE' => $tp -> toHtml ( $data [ 'news_title' ]),
'NEWS_SUMMARY' => $tp -> toEmail ( $data [ 'news_summary' ]),
'NEWS_AUTHOR' => $tp -> toHtml ( $author )
);
$img = explode ( " , " , $data [ 'news_thumbnail' ]);
2015-05-12 01:45:00 -07:00
2015-05-13 01:22:05 -07:00
$message = $tp -> simpleParse ( $template , $shortcodes );
2015-05-12 01:45:00 -07:00
2015-05-13 01:22:05 -07:00
$this -> send ( 'admin_news_notify' , $data [ 'news_title' ], $message , $img );
2015-05-12 01:45:00 -07:00
2015-05-13 01:22:05 -07:00
// print_a($message);
2015-05-12 01:45:00 -07:00
}
2015-02-15 16:06:58 -08:00
}
/*
if ( isset ( $nt -> notify_prefs [ 'plugins' ]) && e_PAGE != 'notify.php' )
{
2013-02-20 21:11:17 +00:00
foreach ( $nt -> notify_prefs [ 'plugins' ] as $plugin_id => $plugin_settings )
{
2015-02-15 16:06:58 -08:00
if ( is_readable ( e_PLUGIN . $plugin_id . '/e_notify.php' ))
{
require_once ( e_PLUGIN . $plugin_id . '/e_notify.php' );
2013-02-20 21:11:17 +00:00
}
2015-02-15 16:06:58 -08:00
}
}
*/
2013-02-20 21:11:17 +00:00