All pastes #1884419 Raw Edit

Anonymous

public php v1 · immutable
#1884419 ·published 2010-06-16 16:15 UTC
rendered paste body
<?php/** * AstonishMe Helper plugin * * This plugin provides methods for other plugins * * @package AstonishMe General * * @copyright (c)2008 by AstonishMe - {@link http://astonishme.co.uk/}. * * {@internal Below is a list of authors who have contributed to design/coding of this file: }} * @author Yabba	- {@link http://www.astonishme.co.uk/} * @author Stk		- {@link http://www.astonishme.co.uk/} * * {@internal License choice * - If you have received this file as part of a package, please find the license.txt file in *   the same folder or the closest folder above for complete license terms. * - If you have received this file individually *   then you must choose one of the following licenses before using the file: *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php * }} * */if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );class am_subscriptions_plugin extends Plugin{	var $name = 'AM Subscriptions';	var $code = 'am_subs';	var $priority = 50;	var $version = '1.0.1';	var $author = 'AstonishMe';	var $group = 'AstonishMe';	var $help_url = 'http://astonishme.co.uk/';	var $number_of_installs = 1;	/**	 * @internal	 */	var $subscribe = false;	function PluginInit()	{		$this->short_desc = $this->T_('This plugin allows your visitors to subscribe to a posts comments.');		return $this;	}	/**	 * Create our tables	 *	 * @return array tables	 */	function GetDbLayout()	{	  return array(	    "CREATE TABLE IF NOT EXISTS ".$this->get_sql_table('subscriptions')." (  				subscription_id int(10) unsigned NOT NULL auto_increment,  				post_id int(10) NOT NULL default '0',  				blog_id int(10) NOT NULL default '0',  				author_name varchar(255) NOT NULL default '',  				author_email varchar(255) NOT NULL default '',  				PRIMARY KEY  (subscription_id)  			   )",	    "CREATE TABLE IF NOT EXISTS ".$this->get_sql_table('pings')." (  				ping_id int(10) unsigned NOT NULL auto_increment,  				post_id int(10) NOT NULL default '0',  				comment_id int(10) NOT NULL default '0',  				PRIMARY KEY  (ping_id)  			   )",	    );  }  /**   * Default notifications and other messages   *   * @return array defaults   */	function GetDefaultSettings()	{		return array(			'comment_settings_begin' => array(				'layout' => 'begin_fieldset',				'label' => $this->T_( 'Settings for comments' ),			),			'comment_subscribe' => array(				'label' => $this->T_( 'Comments : Subscribe'),				'defaultvalue' => '<fieldset><div class="label"><label for="am_subs_comment">'.$this->T_( 'Subscribe' ).':</label></div><div class="input"><input type="checkbox" id="am_subs_comment" class="checkbox" name="am_subs_comment"/> <span class="notes">'.$this->T_( 'Subscribe to comments by email' ).'</span></div></fieldset>',				'type' => 'html_textarea',				'cols' => 60,				'rows' => 4,				'note' => $this->T_( 'This is the html for the "subscribe to this post" checkbox, make sure you get the name right if you change it ;)'),				),			'comment_email_error' => array(				'label' => $this->T_( 'Comments : No email'),				'defaultvalue' => $this->T_( 'You need to provide your email address to subscribe to comments' ),				'type' => 'text',				'size' => 60,				'note' => $this->T_( 'This is the error message displayed if visitor wants to subscribe but doesn\'t leave an email address' ),				),			'comment_subject' => array(				'label' => $this->T_( 'Comments : Subject'),				'defaultvalue' => $this->T_( 'New reply to' ).' : $post_title$',				'type' => 'text',				'size' => 60,				'note' => $this->T_( 'This is the subject of the email message sent out for comment subscriptions'),				),			'comment_message' => array(				'label' => $this->T_( 'Comments : Body'),				'defaultvalue' => sprintf( $this->T_( 'Hi %s,You\'re receiving this email because you wished to be notified of new comments on "%s"You can red the new comment here (@link %s )If you wish to unsubscribe from comment notifications then click here ( @link %s ).' ), '$name$', '$title$', '$link$', '$unsubscribe$' ),				'type' => 'html_textarea',				'note' => $this->T_( 'This is the email message sent out for comment subscriptions, see the help file for replacement values'),				'rows' => 10,				'cols' => 60,				),			'comment_settings_end' => array(				'layout' => 'end_fieldset',			),/* Not currently implemented			'post_settings_begin' => array(				'layout' => 'begin_fieldset',				'label' => $this->T_( 'Settings for posts' ),			),			'post_subject' => array(				'label' => $this->T_( 'Posts : Subject'),				'defaultvalue' => $this->T_( 'New post on' ).' : $blog_name$',				'type' => 'text',				'size' =>60,				'note' => $this->T_( 'This is the subject of the email message sent out for blog subscriptions'),				),			'post_message' => array(				'label' => $this->T_( 'Posts : Body'),				'defaultvalue' => sprintf(						$this->T_( 'Hi %s, yer gonna be really impressed with me, I just wrote a new post called "%s" ....... I know you\'re dying to read it, you can find it here %s'."\n\n".'If you\'re totally fed up with hearing about new posts on "%s" then you can unsubscribe here %s .... I promise I wont weep like a bitch if you do.' ),						'$name$', '$title$', '$link$', '$blog_name$', '$unsubscribe$' ),				'type' => 'html_textarea',				'note' => $this->T_( 'This is the email message sent out for blog subscriptions, see the help file for replacement values'),				'rows' => 10,				'cols' => 60,				),*//*			'exclude_blogs' => array(				'label' => $this->T_( 'Exclude list'),				'defaultvalue' => '',				'type' => 'text',				'size' =>60,				'note' => $this->T_( 'This is a comma seperated list of the Blog ID\'s to be excluded from notifications if the visitor subscribes to blog all'),				),*//* Not currently implemented			'post_settings_end' => array(				'layout' => 'end_fieldset',			),*/			'unsubscribe_settings_begin' => array(				'layout' => 'begin_fieldset',				'label' => $this->T_( 'Settings for unsubscribing' ),			),			'unsubscribed' => array(				'label' => $this->T_( 'Unsubscribed message'),				'defaultvalue' => $this->T_( 'Your subscription has been removed' ),				'type' => 'html_textarea',				'note' => $this->T_( 'This is the message displayed when visitor has successfully unsubscribed' ),				'cols' => 60,				),			'unsubscribe_error' => array(				'label' => $this->T_( 'Unsubscribed failed'),				'defaultvalue' => $this->T_( 'Sorry, there was a problem removing your subscription. Please try again later' ),				'type' => 'html_textarea',				'note' => $this->T_( 'This is the message displayed when unsubscription throws an unknown error' ),				'cols' => 60,				),			'unsubscribed_invalid' => array(				'label' => $this->T_( 'Invalid Unsubscribe'),				'defaultvalue' => $this->T_( 'Sorry, that subscription was not found.' ),				'type' => 'html_textarea',				'note' => $this->T_( 'This is the message displayed when an invalid unsubscription attempt is made' ),				'cols' => 60,				),			'multiple_unsubscribe' => array(				'label' => $this->T_( 'Multiple Unsubscribe'),				'defaultvalue' => $this->T_( 'You also have the following subscriptions that you may wish to cancel.' ),				'type' => 'text',				'note' => $this->T_( 'This is the message displayed when the visitor has more subscriptions which they may wish to cancel' ),				'size' =>60,				),			'unsubscribe_page' => array(				'label' => $this->T_( 'Unsubscribe page'),				'defaultvalue' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-UK" lang="en-UK"><head><title>AstonishMe Subscriptions - Unsubscribe</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /></head><body><h3>AstonishMe Subscriptions - Unsubscribe</h3><p>$message$</p>$form$</body></html>',				'type' => 'html_textarea',				'note' => $this->T_( 'This is the page displayed after unsubscribing, see the help file for replacement values' ),				'rows' => 20,				'cols' => 60,				),			'unsubscribe_settings_end' => array(				'layout' => 'end_fieldset',			),			);	}  /**   * Adds a checkbox for "subscribe to comments"   *   */	function DisplayCommentFormFieldset( $params )	{		echo $this->Settings->get( 'comment_subscribe' );	}	/**	 * Check if the user has subscribed	 * If they have then check if we have an email address ( for visitors )	 *	 * @param mixed $params	 */	function BeforeCommentFormInsert( & $params )	{		if( ( $this->subscribe = param( 'am_subs_comment', 'string' ) ) && empty( $params[ 'Comment' ]->author_ID ) && empty( $params[ 'Comment' ]->author_email ) )			{	// no email provided				global $Messages;				$Messages->add( 'error', $this->Settings->get( 'comment_email_error' ) );			}		return;	}	/**	 * Add subscription to database if appropriate	 *	 * @param mixed $params	 */	function AfterCommentFormInsert( $params )	{		if( !$this->subscribe || empty( $params[ 'Comment' ]->item_ID ) ) return;	// nothing to do		global $DB;		$author_email = ( empty( $params[ 'Comment' ]->author_ID ) ? $params[ 'Comment' ]->author_email : $params[ 'Comment' ]->author_ID );		$author_name = ( empty( $params[ 'Comment' ]->author_ID ) ? $params[ 'Comment' ]->author : '' );		// check if they're already subscribed ;)		$sql = 'select subscription_id from '.$this->get_sql_table('subscriptions').' where author_email = \''.$DB->escape( $author_email ).'\' and post_id = '.intval( $params[ 'Comment' ]->item_ID );		if( $DB->get_var( $sql ) ) return;	// they're already subscribed		// ok they're not subscribed, lets add them		$sql = 'insert into '.$this->get_sql_table('subscriptions').' ( author_name, author_email, post_id ) values( \''.$DB->escape( $author_name ).'\', \''.$DB->escape( $author_email ).'\', '.intval( $params[ 'Comment' ]->item_ID ).' )';		$DB->query( $sql );	}	/**	 * Send out notifications	 *	 * @param mixed $params	 */	function AfterCommentUpdate( $params )	{		$this->AfterCommentFormInsert( $params );	}	/**	 * Send out notifications	 *	 * @param mixed $params	 */	function AfterCommentInsert( $params )	{		if( $params[ 'Comment' ]->get( 'status' ) != 'published' ) return;	// nothing to do		$this->SendNotifications( 'comment', $params[ 'Comment' ]->ID, $params[ 'Comment' ]->Item->ID );	}	/**	 * Sends out notifications if required	 *	 * @param string $type 'post' or 'comment'	 * @param integer $which post/comment id to send notifications for	 * @param integer $id blog/post id of the post/comment parent	 */	function SendNotifications( $type, $which, $id )	{		global $DB;		// lets check if we've already sent the notifications		$sql = 'select ping_id from '.$this->get_sql_table('pings').' where '.( $type = 'comment' ? 'comment' : 'post' ).'_id = '.intval( $which );		if( $DB->get_var( $sql ) ) return;	// notifications already sent		switch( $type )		{			case 'comment' :				$this->CommentNotifications( $which, $id );				break;		}	}	/**	 * Send out notifications for comment subscriptions	 *	 * @param integer $which which comment should we send notifications for	 * @param integer $id which post is the comment on	 */	function CommentNotifications( $which, $id )	{		global $DB, $UserCache, $CommentCache, $ItemCache, $baseurl;		$comment = Comment_get_by_ID( $which );		// grab the rendered comment content		ob_start();		$comment->content();		$comment_content = ob_get_clean();		//var_dump($comment_content);				// check if comment is by a member or visitor		$author_email = ( empty( $comment->author_user_ID ) ? $comment->author_email : $comment->author_user_ID );		$sql = 'select subscription_id, author_name, author_email from '.$this->get_sql_table('subscriptions').' where post_id = '.intval( $id );		if( $results = $DB->get_results( $sql ) )		{	// we have some subscriptions, lets notify them			$message = $this->Settings->get( 'comment_message' );			$subject = $this->Settings->get( 'comment_subject' );			$url = $comment->get_permanent_url();			$unsubscribe = str_replace( '&amp;', '&', $baseurl.$this->get_htsrv_url( 'unsubscribe' ) );			$item = $ItemCache->get_by_ID( $id );			$title = $item->title;			$subject = str_replace( '$post_title$', $title, $subject );			foreach( $results as $result )			{				// check if this is the author of the comment				if( $result->author_email == $author_email )				{ // pointless telling them that they've commented ;)					continue;				}				if( is_numeric( $result->author_email ) )				{	// this is a member, get name and email from their profile					$user = $UserCache->get_by_ID( $result->author_email );					$user_email = $user->get( 'email' );					$user_name = $user->get_preferred_name();				}				else				{					$user_email = $result->author_email;					$user_name = $result->author_name;				}				//lets build the message				$temp_message = str_replace( array( '%name%', '%link%', '%unsubscribe%', '%title%', '%content%' ),					array(						$user_name,						$url,						$unsubscribe.'&amu_id='.$result->subscription_id.'&amu_key='.md5( $result->author_name.$result->author_email.$result->subscription_id ),						$title,						$comment_content						),					$message );				// now lets delight them with our shiny personalised message				//send_mail( $user_email, $subject, $temp_message );				// For b2evo-2.5 and up 				//send_mail( $to, $to_name, $subject, $temp_message, $from, $from_name, $headers ) ;				send_mail($user_email, $user_name, $subject, $temp_message);				//$tmp = var_export($comment_content, TRUE);				//send_mail($user_email, $user_name, $subject, $tmp);			}		}		$sql = 'insert into '.$this->get_sql_table('pings').' ( comment_id, post_id ) values( '.intval( $which ).', -1 )';		$DB->query( $sql );		if( is_admin_page() ) echo T_( 'Comment notifications done captain :D' );	}	/**	 * declares our htsrv actions	 *	 * @return unknown	 */	function GetHtsrvMethods()	{	 	return array('unsubscribe', 'multiple_unsubscribe' );	}	/**	 * Handles unsubscribes	 * If more than one subscription, offers the ability to unsubscribe from them all	 *	 * @param mixed $params	 */	function htsrv_unsubscribe( $params )	{		$which_subscription = param( 'amu_id', 'integer' );		$the_key = param( 'amu_key', 'string' );		$form_output = '';		//lets see if we have a subscription		$sql = 'select subscription_id, author_email, author_name from '.$this->get_sql_table('subscriptions').' where subscription_id = '.intval( $which_subscription );		global $DB, $baseurl;		$ItemCache = get_Cache( 'ItemCache' );		if( $which_subscription && $the_key && $result = $DB->get_row( $sql ) )		{	// we have a result, does it match?			if( md5( $result->author_name.$result->author_email.$result->subscription_id ) == $the_key )			{	// ok, this is a valid request, lets delete them				$sql = 'delete from '.$this->get_sql_table('subscriptions').' where subscription_id = '.$result->subscription_id;				if( $DB->query( $sql ) )				{	// yay, we deleted them :D					$message = $this->Settings->get( 'unsubscribed' );				}				else				{	// bugger, summat went wrong :-S					$message = $this->Settings->get( 'unsubscribe_error' );				}				// now we need to check if they have other subscriptions				$sql = 'select subscription_id, blog_id, post_id from '.$this->get_sql_table('subscriptions').' where author_email = \''.$result->author_email.'\'';				if( $subscriptions = $DB->get_results( $sql ) )				{	// produce list ( all selected ) of other subscriptions					ob_start();					$Form = & new Form( $baseurl.$this->get_htsrv_url( 'multiple_unsubscribe' ), 'am_subscriptions' );					$Form->begin_form();					echo '<p>'.$this->Settings->get( 'multiple_unsubscribe' ).'</p>';					echo '<input type="hidden" name="key" value="'.md5( $result->author_email ).'" />';					foreach( $subscriptions as $subscription )					{						if( $subscription->post_id > -1 )						{	// this is a post subscription							if( $item = $ItemCache->get_by_ID( $subscription->post_id, false ) )								$title = '[ '.T_( 'Post' ).' ] '.$item->title;							else								$title = 'unknown';	// should just remove these						}						elseif( $subsciption->blog_ID > -1 )						{	// this is a blog subscription							if( $blog = $BlogCache->get_by_ID( $subscription->blog_id, false ) )								$title = '[ '.T_( 'Blog' ).' ] '.$blog->name;							else								$title = 'unknown';	// should just remove these						}						else						{	// we should never get here?							continue;						}						if( $title == 'unknown' )							$Form->hidden( 'subscription_id[]', $subscription->subscription_id );						else							$Form->checkbox( 'subscription_id[]', true, '', $title, '', $subscription->subscription_id );					}					$Form->button_input( array( 'name' => 'unsubscribe', 'class' => 'submit', 'value' => $this->T_('Unsubscribe') ) );					$Form->end_form();					$form_output = ob_get_clean();				}				else				{ // no form to display					$form_output = '';				}			}			else			{	// hmmmm				$message = $this->Settings->get( 'unsubscribed_invalid' );			}		}		else		{	// hmmmm			$message = $this->Settings->get( 'unsubscribed_invalid' );		}		// lets spit out the result		echo str_replace( array( '%message%', '%form%' ), array( $message, $form_output ), $this->Settings->get( 'unsubscribe_page' ) );	}	/**	 * Handles multiple unsubscriptions	 *	 * @param mixed $params	 */	function htsrv_multiple_unsubscribe( $params )	{		$key = param( 'key', 'string' );		$posts = param( 'subscription_id', 'array' );		if( $key && $posts )		{	// we have some that they want to remove			global $DB;			$subscription_id = array();			foreach( $posts as $post )				$subscription_id[] = intval( $post );// just to be sure ;)			$sql = 'delete from '.$this->get_sql_table('subscriptions').' where md5( author_email ) = \''.$DB->escape( $key ).'\' and subscription_id in ( '.implode( ',', $subscription_id ).' )';			if( $DB->query( $sql ) )				$message = $this->Settings->get( 'unsubscribed' ); // we managed to delete them			else				$message = $this->Settings->get( 'unsubscribe_error' );	// bugger, summat went wrong :-S		}		else		{			$message = '';		}		$message .= '<br /><br />Thank you for flying Yabba airlines :D';		echo str_replace( array( '%message%', '%form%' ), array( $message, '' ), $this->Settings->get( 'unsubscribe_page' ) );	}}/** * LOG * * 1.0.1 - some modifications applied, to adapt it to 3.x (tested on 3.3.3) *	   references:	 *	   http://forums.b2evolution.net/viewtopic.php?t=18737 *	   if you are desperate, write to gr8dude at gmx dot net	 * * 1.0 - * 	Overhaul to bring plugin into the 2.x world * * 0.0.0.0.0.3 - * 	Added multiple unsubscribe - still needs a tad of work * * 0.0.0.0.0.2 - * 	Moved messages etc to settings * 	Created readme.html * * 0.0.0.0.0.1 - * 	Plugin created * */?>