Skip to content
This repository has been archived by the owner on Aug 23, 2020. It is now read-only.

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sparticus1701 committed Nov 14, 2011
0 parents commit f9231e6
Show file tree
Hide file tree
Showing 8 changed files with 4,201 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ReadMe.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This allows you to create a page where people can register to receive notifications about new questions and answers.
It also allows you to specify that Experts, Editors, and Moderators will also receive notifications like Admins do.

The user notification list can grow to the point that it becomes very slow to post new questions and answers. To correct this problem, I have included some
custom code in the qa-external to modify the external mailer to accept BCC. I included code for a newer version of PHPMailer because we had problems
connecting to GMail with the default code. If you use the user notifications, it is recommended that you use this external mailer code or modify it
for your particular situation.
211 changes: 211 additions & 0 deletions email-notifications/email-notifications-event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
<?php

/*
Walter Williams
File: qa-plugin/user-email-notifications/qa-user-email-notifications-event.php
Version: 1.0
Date: 2011-10-20
Description: Event module class for user email notifications plugin
*/


require_once QA_INCLUDE_DIR.'qa-db-selects.php';
require_once QA_INCLUDE_DIR.'qa-app-users.php';
require_once QA_INCLUDE_DIR.'qa-app-format.php';
require_once QA_INCLUDE_DIR.'qa-app-emails.php';
require_once QA_INCLUDE_DIR.'qa-app-posts.php';
require_once QA_INCLUDE_DIR.'qa-util-emailer.php';
require_once QA_BASE_DIR.'qa-config.php';


class email_notifications_event
{
function process_event ($event, $userid, $handle, $cookieid, $params)
{
$users=qa_db_select_with_pending(qa_db_users_from_level_selectspec(QA_USER_LEVEL_EXPERT));

$emailsubscriptions = false;
if ($this->user_email_notification_table_exists())
$emailsubscriptions = qa_db_read_all_values(qa_db_query_sub("SELECT email from ^useremailsubscription"));

if ($event == 'q_post')
{
$subject = 'New ^site_title question: ^q_title';
foreach ($users as $user) {
$role = $user['level'];

if ($role == QA_USER_LEVEL_ADMIN || $role == QA_USER_LEVEL_SUPER)
continue;

if ($role == QA_USER_LEVEL_EXPERT && ((int)qa_opt('expert_emailnotifications_enabled')) == 0)
continue;
if ($role == QA_USER_LEVEL_EDITOR && ((int)qa_opt('editor_emailnotifications_enabled')) == 0)
continue;
if ($role == QA_USER_LEVEL_MODERATOR && ((int)qa_opt('moderator_emailnotifications_enabled')) == 0)
continue;

qa_send_notification($user['userid'], null, null, $subject, qa_lang('emails/q_posted_body'), array(
'^q_handle' => isset($handle) ? $handle : qa_lang('main/anonymous'),
'^q_title' => $params['title'], // don't censor title or content since we want the admin to see bad words
'^q_content' => $params['text'],
'^url' => qa_path(qa_q_request($params['postid'], $params['title']), null, qa_opt('site_url')),
));
}

if ($emailsubscriptions) // email those in the database
{
$body = "A question on ^site_title has been asked by ^q_handle:\n\nThe question is:\n\n^open^q_title^close\n\n^open^q_content^close\n\nIf you would like to view this quesion:\n\n^url\n\nThank you,\n\n^site_title";
$subject = 'New ^site_title question: ^q_title';
$subs = array(
'^q_handle' => isset($handle) ? $handle : qa_lang('main/anonymous'),
'^q_title' => $params['title'], // don't censor title or content since we want the admin to see bad words
'^q_content' => $params['text'],
'^url' => qa_path(qa_q_request($params['postid'], $params['title']), null, qa_opt('site_url')),
'^site_title' => qa_opt('site_title'),
'^open' => "\n",
'^close' => "\n",
);

for ($i = 0; $i < count($emailsubscriptions); $i++)
{
$bcclist = array();
for ($j = 0; $j < 75 && $i < count($emailsubscriptions); $j++, $i++)
{
$bcclist[] = $emailsubscriptions[$i];
}

qa_send_email(array(
'fromemail' => qa_opt('from_email'),
'fromname' => qa_opt('site_title'),
'bcclist' => $bcclist,
'subject' => strtr($subject, $subs),
'body' => strtr($body, $subs),
'html' => false,
));
}
}
}
else if ($event == 'a_post')
{
$body = "A question on ^site_title has been answered by ^a_handle:\n\n^open^a_content^close\n\nThe question was:\n\n^open^q_title^close\n\nIf you would like to view this quesion:\n\n^url\n\nThank you,\n\n^site_title";
$subject = 'New ^site_title answer to: ^q_title';

$parentpost=qa_post_get_full($params['parentid']);

foreach ($users as $user) {
$role = $user['level'];

if (($role == QA_USER_LEVEL_ADMIN || $role == QA_USER_LEVEL_SUPER) && ((int)qa_opt('admin_emailnotifications_enabled')) == 0)
continue;

if ($role == QA_USER_LEVEL_EXPERT && ((int)qa_opt('expert_emailnotifications_enabled')) == 0)
continue;
if ($role == QA_USER_LEVEL_EDITOR && ((int)qa_opt('editor_emailnotifications_enabled')) == 0)
continue;
if ($role == QA_USER_LEVEL_MODERATOR && ((int)qa_opt('moderator_emailnotifications_enabled')) == 0)
continue;

qa_send_notification($user['userid'], null, null, $subject, $body, array(
'^a_handle' => isset($handle) ? $handle : qa_lang('main/anonymous'),
'^q_title' => $parentpost['title'], // don't censor title or content since we want the admin to see bad words
'^a_content' => $params['text'],
'^url' => qa_path(qa_q_request($params['parentid'], $parentpost['title']), null, qa_opt('site_url'), null, qa_anchor('A', $params['postid'])),
));
}

if ($emailsubscriptions) // email those in the database
{
$subs = array(
'^a_handle' => isset($handle) ? $handle : qa_lang('main/anonymous'),
'^q_title' => $parentpost['title'], // don't censor title or content since we want the admin to see bad words
'^a_content' => $params['text'],
'^url' => qa_path(qa_q_request($params['parentid'], $parentpost['title']), null, qa_opt('site_url'), null, qa_anchor('A', $params['postid'])),
'^site_title' => qa_opt('site_title'),
'^open' => "\n",
'^close' => "\n",
);

for ($i = 0; $i < count($emailsubscriptions); $i++)
{
$bcclist = array();
for ($j = 0; $j < 75 && $i < count($emailsubscriptions); $j++, $i++)
{
$bcclist[] = $emailsubscriptions[$i];
}

qa_send_email(array(
'fromemail' => qa_opt('from_email'),
'fromname' => qa_opt('site_title'),
'bcclist' => $bcclist,
'subject' => strtr($subject, $subs),
'body' => strtr($body, $subs),
'html' => false,
));
}
}
}
}

function admin_form(&$qa_content)
{
$saved=false;

if (qa_clicked('emailnotifications_save_button')) {
qa_opt('admin_emailnotifications_enabled', (int)qa_post_text('admin_emailnotifications_enabled_field'));
qa_opt('expert_emailnotifications_enabled', (int)qa_post_text('expert_emailnotifications_enabled_field'));
qa_opt('editor_emailnotifications_enabled', (int)qa_post_text('editor_emailnotifications_enabled_field'));
qa_opt('moderator_emailnotifications_enabled', (int)qa_post_text('moderator_emailnotifications_enabled_field'));
$saved=true;
}

return array(
'ok' => $saved ? 'Email Notifications settings saved' : null,

'fields' => array(
array(
'label' => 'Allow Experts to receive emails about new questions & answers',
'type' => 'checkbox',
'value' => (int)qa_opt('expert_emailnotifications_enabled'),
'tags' => 'NAME="expert_emailnotifications_enabled_field" ID="expert_emailnotifications_enabled_field"',
),
array(
'label' => 'Allow Editors to receive emails about new questions & answers',
'type' => 'checkbox',
'value' => (int)qa_opt('editor_emailnotifications_enabled'),
'tags' => 'NAME="editor_emailnotifications_enabled_field" ID="editor_emailnotifications_enabled_field"',
),
array(
'label' => 'Allow Moderators to receive emails about new questions & answers',
'type' => 'checkbox',
'value' => (int)qa_opt('moderator_emailnotifications_enabled'),
'tags' => 'NAME="moderator_emailnotifications_enabled_field" ID="moderator_emailnotifications_enabled_field"',
),
array(
'label' => 'Allow Admins to receive emails about new answers (questions are handled elsewhere)',
'type' => 'checkbox',
'value' => (int)qa_opt('admin_emailnotifications_enabled'),
'tags' => 'NAME="admin_emailnotifications_enabled_field" ID="admin_emailnotifications_enabled_field"',
),
),

'buttons' => array(
array(
'label' => 'Save Changes',
'tags' => 'NAME="emailnotifications_save_button"',
),
),
);
}

function user_email_notification_table_exists ()
{
$res = qa_db_query_sub("SELECT COUNT(*) AS count FROM information_schema.tables WHERE table_schema = '". QA_MYSQL_DATABASE ."' AND table_name = '^useremailsubscription'");
return mysql_result($res, 0) == 1;
}
};


/*
Omit PHP closing tag to help avoid accidental output
*/
149 changes: 149 additions & 0 deletions email-notifications/email-notifications-page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

/*
Walter Williams
File: qa-plugin/user-email-notifications/qa-user-email-notifications-page.php
Version: 1.0
Date: 2011-10-20
Description: Page module class for user email notifications plugin
*/


require_once QA_INCLUDE_DIR.'qa-app-users.php';
require_once QA_INCLUDE_DIR.'qa-db-maxima.php';
require_once QA_INCLUDE_DIR.'qa-util-emailer.php';
require_once QA_INCLUDE_DIR.'qa-util-string.php';
require_once QA_INCLUDE_DIR.'qa-db-selects.php';
require_once QA_INCLUDE_DIR.'qa-app-captcha.php';


class email_notifications_page
{
var $captchaerrors;

function load_module ($directory, $urltoroot)
{
}

function suggest_requests () // for display in admin interface
{
return array(
array(
'title' => 'User Email Notifications',
'request' => 'qa-user-email-notifications-page',
'nav' => 'F', // 'M'=main, 'F'=footer, 'B'=before main, 'O'=opposite main, null=none
),
);
}

function match_request ($request)
{
return ($request=='qa-user-email-notifications-page');
}

function process_request ($request)
{
$qa_content=qa_content_prepare();

$qa_content['title']='Email Notifications';

$subresult = false;
$subresultmsg = '';
if (qa_post_text('optin') == '0')
{
qa_captcha_validate($_POST, $captchaerrors);
if (empty($captchaerrors))
{
if (qa_post_text('email'))
$subresult = $this->subscribe(qa_post_text('email'), $subresultmsg);
}
}
else if (qa_post_text('optin') == '1')
{
qa_captcha_validate($_POST, $captchaerrors);
if (empty($captchaerrors))
{
if (qa_post_text('email'))
$subresult = $this->unsubscribe(qa_post_text('email'), $subresultmsg);
}
}

$qa_content['form']=array(
'tags' => 'METHOD="POST" ACTION="'.qa_self_html().'"',

'style' => 'wide',

'ok' => (empty($captchaerrors) && $subresult) ? $subresultmsg : null,

'title' => 'To subscribe or unsubscribe to receive emails when a new question is posted, please enter your email address:',

'fields' => array(
'suboptin' => array(
'label' => '',
'tags' => 'NAME="optin"',
'type' => 'select-radio',
'options' => array('Subscribe', 'Unsubscribe'),
'value' => 'Subscribe',
'error' => '',
),
'request' => array(
'label' => 'Email address',
'tags' => 'NAME="email"',
'value' => '',
'error' => (empty($captchaerrors) && !$subresult) ? qa_html($subresultmsg) : '',
),
),

'buttons' => array(
'ok' => array(
'tags' => 'NAME="ok"',
'label' => 'OK',
'value' => '1',
),
),
);
qa_set_up_captcha_field($qa_content, $qa_content['form']['fields'], @$captchaerrors);

return $qa_content;
}

function subscribe ($email, &$message)
{
if ($this->verify_email($email))
{
qa_db_query_sub("CREATE TABLE IF NOT EXISTS ^useremailsubscription (email varchar(80) NOT NULL, registered timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (email)) ENGINE=InnoDB DEFAULT CHARSET=utf8");
qa_db_query_sub('INSERT IGNORE INTO ^useremailsubscription SET email = ($)', $email);

$message = 'Thank you for subscribing';
return (true);
}

$message = 'The email address was not valid';
return (false);
}

function unsubscribe ($email, &$message)
{
if ($this->verify_email($email))
{
qa_db_query_sub('DELETE IGNORE FROM ^useremailsubscription WHERE email = ($)', $email);

$message = 'You have been unsubscribed';
return (true);
}

$message = 'The email address was not valid';
return (false);
}

function verify_email ($email)
{
return (preg_match('/^[_A-z0-9-]+((\.|\+)[_A-z0-9-]+)*@[A-z0-9-]+(\.[A-z0-9-]+)*(\.[A-z]{2,4})$/', $email));
}
};


/*
Omit PHP closing tag to help avoid accidental output
*/
Loading

0 comments on commit f9231e6

Please sign in to comment.