<?php
/**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*
* $Id: welcomepm.php 8 2010-11-04 23:56:51Z - G33K - $
*/
if(!defined("IN_MYBB"))
{
die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}
$plugins->add_hook("member_do_register_end", "welcomepm_run");
$plugins->add_hook("member_activate_accountactivated", "welcomepm_run");
$plugins->add_hook("admin_config_settings_change","welcomepm_settings_page");
$plugins->add_hook("admin_page_output_footer","welcomepm_settings_peeker");
function welcomepm_info()
{
global $plugins_cache, $db, $mybb;
$codename = basename(__FILE__, ".php");
$prefix = 'g33k_'.$codename.'_';
$info = array(
"name" => "Welcome PM/Email",
"description" => "Sends a PM/Email to new users welcoming them to the forum.",
"website" => "http://geekplugins.com/mybb/wpme/",
"author" => "- G33K -",
"authorsite" => "http://geekplugins.com/mybb/",
"version" => "1.1",
"guid" => "561f493df040e8442417bb30653b9a2d",
"compatibility" => "14*,16*"
);
if(is_array($plugins_cache) && is_array($plugins_cache['active']) && $plugins_cache['active'][$codename])
{
$result = $db->simple_select('settinggroups', 'gid', "name = '{$prefix}settings'", array('limit' => 1));
$group = $db->fetch_array($result);
// Set correct url for the settings page. Version prior to 1.6 used / to split modules, newer versions use -
if($mybb->version_code < 1600)
{
$settingsurl = "index.php?module=config/settings&action=change&gid=";
}
else
{
$settingsurl = "index.php?module=config-settings&action=change&gid=";
}
if(!empty($group['gid']))
{
$info['description'] = "<i><small>[<a href=\"".$settingsurl.$group['gid']."\">Configure Settings</a>]</small></i><br />".$info['description'];
}
}
// Show error on plugin page if send PM on register, regtype is activation and the awaiting activation usergroup doesn't have permissions to read PMs
if((is_array($plugins_cache) && is_array($plugins_cache['active']) && $plugins_cache['active'][$codename]) && ($mybb->settings[$prefix.'enabled'] == '1' && ($mybb->settings[$prefix.'method'] == 'pm' || $mybb->settings[$prefix.'method'] == 'both') && $mybb->settings[$prefix.'sendwhen'] == 'register' && ($mybb->settings['regtype'] == "verify" || $mybb->settings['regtype'] == "admin")))
{
// Check if awaiting activation group has PM perms
$groupperms = usergroup_permissions('5');
if ($groupperms['canusepms'] == "0")
{
$info['description'] .= "<ul><li style=\"list-style-image: url(styles/default/images/icons/error.gif)\">You have selected to send the welcome PM upon registration, before activation, but your Awaiting Activation usergroup does not have permission to use Private Messaging System.<br />To correct this error, either change the permissions of the Awaiting Activation Usergroup to enable the use of PMs, or select to send the Welcome PM on Account Activation, or set the plugin to send an email only, or select your Registration Method to be Instant Activation / Send Random Password.</li></ul>";
}
}
return $info;
}
function welcomepm_activate()
{
global $db;
$codename = basename(__FILE__, ".php");
$prefix = 'g33k_'.$codename.'_';
// Remove settings that may have been left behind by user not deactivating the plugin before activating mybb or something
$result = $db->simple_select('settinggroups', 'gid', "name = '{$prefix}settings'");
while($group = $db->fetch_array($result))
{
if(!empty($group['gid']))
{
$db->delete_query('settinggroups', "gid='{$group['gid']}'");
$db->delete_query('settings', "gid='{$group['gid']}'");
}
}
// Now add the settings
$script = "
<script type=\"text/javascript\">
function insertText(value, textarea)
{
// Internet Explorer
if(document.selection)
{
textarea.focus();
var selection = document.selection.createRange();
selection.text = value;
}
// Firefox
else if(textarea.selectionStart || textarea.selectionStart == '0')
{
var start = textarea.selectionStart;
var end = textarea.selectionEnd;
textarea.value = textarea.value.substring(0, start) + value + textarea.value.substring(end, textarea.value.length);
}
else
{
textarea.value += value;
}
}
</script>";
$replacement = array(
"{username}" => "Username",
"{bbname}" => "Board Name",
"{bburl}" => "Board URL"
);
$sbj_personalisation = "<script type=\"text/javascript\">\n<!--\ndocument.write('Personalize: ";
$msg_personalisation = "<script type=\"text/javascript\">\n<!--\ndocument.write('Personalize: ";
foreach($replacement as $value => $name)
{
$msg_personalisation .= " [<a href=\"#\" onclick=\"insertText(\'{$value}\', \$(\'setting_".$prefix."message\')); return false;\">{$name}</a>], ";
$sbj_personalisation .= " [<a href=\"#\" onclick=\"insertText(\'{$value}\', \$(\'setting_".$prefix."subject\')); return false;\">{$name}</a>], ";
}
$sbj_personalisation = substr($sbj_personalisation, 0, -2)."');\n</script>\n";
$msg_personalisation = substr($msg_personalisation, 0, -2)."');\n</script>\n";
$settings = array(
'enabled' => array(
'title' => 'Enable/Disable',
'description' => 'Enable/Disable the sending of Welcome PMs/Emails.'.$script,
'optionscode' => 'onoff',
'value' => '0'),
'method' => array(
'title' => 'PM or Mail or Both',
'description' => 'Choose whether to send a PM or an Email or both PM and Email.',
'optionscode' => 'select
pm=PM
email=Email
both=Both',
'value' => 'pm'),
'sendwhen' => array(
'title' => 'When to send the PM/Email',
'description' => 'Choose when you want to send the PM/Email.<br />This setting is dependent on the Registration Method and whether account creation is instant or requires activation.',
'optionscode' => 'select
activation=Send upon Account Activation
register=Send on Registration',
'value' => 'activation'),
'pm_sender' => array(
'title' => 'PM Sender User ID',
'description' => 'Enter the User ID of the user to send the PM from.<br />This setting is only used when sending PMs. Emails are sent using the board settings.',
'optionscode' => 'text',
'value' => '1'),
'subject' => array(
'title' => 'Subject',
'description' => 'Enter the Subject of the PM/Email to be sent.<br />You can personalize the subject by inserting the user\'s username, board name or board url using the personalize links below.<br /><br />'.$sbj_personalisation,
'optionscode' => 'textarea',
'value' => 'Welcome to {bbname}!'),
'message' => array(
'title' => 'Content',
'description' => 'Enter the Content of the PM/Email to be sent.<br />You can personalize the message by inserting the user\'s username, board name or board url using the personalize links below.<br /><br />'.$msg_personalisation,
'optionscode' => 'textarea',
'value' => 'Welcome to our forum {username}.
{bbname}
{bburl}')
);
$query = $db->query("SELECT disporder FROM ".TABLE_PREFIX."settinggroups ORDER BY `disporder` DESC LIMIT 1");
$disporder = $db->fetch_field($query, 'disporder')+1;
$setting_group = array(
'name' => $prefix.'settings',
'title' => 'Welcome PM/Email',
'description' => 'Settings to customize the Welcome PM/Email Plugin',
'disporder' => intval($disporder),
'isdefault' => 0
);
$db->insert_query('settinggroups', $setting_group);
$gid = $db->insert_id();
$x = 1;
foreach($settings as $name => $setting)
{
$insert_settings = array(
'name' => $db->escape_string($prefix.$name),
'title' => $db->escape_string($setting['title']),
'description' => $db->escape_string($setting['description']),
'optionscode' => $db->escape_string($setting['optionscode']),
'value' => $db->escape_string($setting['value']),
'disporder' => $x,
'gid' => $gid,
'isdefault' => 0
);
$db->insert_query('settings', $insert_settings);
$x++;
}
rebuild_settings();
}
function welcomepm_deactivate()
{
global $db;
$codename = basename(__FILE__, ".php");
$prefix = 'g33k_'.$codename.'_';
$result = $db->simple_select('settinggroups', 'gid', "name = '{$prefix}settings'", array('limit' => 1));
while($group = $db->fetch_array($result))
{
if(!empty($group['gid']))
{
$db->delete_query('settinggroups', "gid='{$group['gid']}'");
$db->delete_query('settings', "gid='{$group['gid']}'");
}
}
rebuild_settings();
}
function welcomepm_run()
{
global $db, $mybb, $user_info, $user;
$codename = basename(__FILE__, ".php");
$prefix = 'g33k_'.$codename.'_';
if ($mybb->settings[$prefix.'enabled'] == "1")
{
// Regtype is activation, sendwhen is on activation, we're sending PM on activation
if ($mybb->settings[$prefix.'sendwhen'] == "activation" && ($mybb->settings['regtype'] == "verify" || $mybb->settings['regtype'] == "admin") && !isset($user_info))
{
welcomepm_send($user);
}
// Regtype is no activation, so our sendwhen is moot, always send upon registration regardless
if (($mybb->settings[$prefix.'sendwhen'] == "register" || $mybb->settings['regtype'] == "instant" || $mybb->settings['regtype'] == "randompass") && isset($user_info))
{
welcomepm_send($user_info);
}
}
}
function welcomepm_send($user)
{
global $db, $mybb;
$codename = basename(__FILE__, ".php");
$prefix = 'g33k_'.$codename.'_';
$search = array(
'{username}',
'{bbname}',
'{bburl}'
);
$replace = array(
htmlspecialchars_uni($user['username']),
$mybb->settings['bbname'],
$mybb->settings['bburl']
);
$welcomepm_subject = str_replace($search, $replace, $mybb->settings[$prefix.'subject']);
$welcomepm_message = str_replace($search, $replace, $mybb->settings[$prefix.'message']);
// PM
if ($mybb->settings[$prefix.'method'] == "pm" || $mybb->settings[$prefix.'method'] == "both")
{
require_once MYBB_ROOT."inc/datahandlers/pm.php";
$pmhandler = new PMDataHandler();
$pmhandler->admin_override = true;
$pm = array(
"subject" => $welcomepm_subject,
"message" => $welcomepm_message,
"icon" => "-1",
"toid" => array($user['uid']),
"fromid" => $mybb->settings[$prefix.'pm_sender'],
"do" => '',
"pmid" => ''
);
$pm['options'] = array(
"signature" => "0",
"disablesmilies" => "0",
"savecopy" => "0",
"readreceipt" => "0"
);
$pmhandler->set_data($pm);
if(!$pmhandler->validate_pm())
{
// There some problem sending the PM
}
else
{
$pminfo = $pmhandler->insert_pm();
}
}
// Email
if ($mybb->settings[$prefix.'method'] == "email" || $mybb->settings[$prefix.'method'] == "both")
{
my_mail($user['email'], $welcomepm_subject, $welcomepm_message);
}
}
function welcomepm_settings_page()
{
global $db, $mybb, $g33k_settings_peeker;
$codename = basename(__FILE__, ".php");
$prefix = 'g33k_'.$codename.'_';
$query = $db->simple_select("settinggroups", "gid", "name='{$prefix}settings'", array('limit' => 1));
$group = $db->fetch_array($query);
$g33k_settings_peeker = ($mybb->input["gid"] == $group["gid"]) && ($mybb->request_method != "post");
}
function welcomepm_settings_peeker()
{
global $g33k_settings_peeker;
$codename = basename(__FILE__, ".php");
$prefix = 'g33k_'.$codename.'_';
if($g33k_settings_peeker)
echo '<script type="text/javascript">
Event.observe(window,"load",function(){
load'.$prefix.'Peekers();
});
function load'.$prefix.'Peekers(){
new Peeker($$(".setting_'.$prefix.'enabled"), $("row_setting_'.$prefix.'method"), /1/, true);
new Peeker($$(".setting_'.$prefix.'enabled"), $("row_setting_'.$prefix.'sendwhen"), /1/, true);
new Peeker($$(".setting_'.$prefix.'enabled"), $("row_setting_'.$prefix.'pm_sender"), /1/, true);
new Peeker($$(".setting_'.$prefix.'enabled"), $("row_setting_'.$prefix.'subject"), /1/, true);
new Peeker($$(".setting_'.$prefix.'enabled"), $("row_setting_'.$prefix.'message"), /1/, true);
}
</script>';
}
?>