Files
Testing/include/functions_subscriber_bak.php

680 lines
19 KiB
PHP
Raw Permalink Normal View History

2026-11-17 16:15:01 -05:00
<?php
$activity_msg_types = array( 0 => 100014064,
1000 => 100014063,
100 => 100014065,
101 => 100014066,
102 => 100014067,
103 => 100014068,
104 => 100014069,
105 => 100014070
);
/*
* Cart Item callback function for one-time payments (non-recurring)
*
*/
function subscriber_payment_notification( $params ) {
//$user_id = $fields[0];
//$plan_id = $fields[1];
$plan = new semods_plan( $params['pid'], $params['uid']);
if($plan->plan_exists != 0) {
$plan->plan_subscribe( $params['uid'], // user_id
0, // handler_id - manual
array(), // metadata
false // has trial
);
} else {
subscriber_log_error( "Received payment (from userid {$params['uid']}) for non-existing Plan (planid: {$params['pid']})" );
// error - deleted plan while there are users signed up for it
}
}
/*
* Log activity item
*
* types:
* 0 - general message
*
* 100 - plan subscribed
* 101 - plan cancelled
* 102 - plan expired
* 103 - plan renewed
* 104 - plan suspended
* 105 - plan deleted by admin
*
* 1000 - error
*
* 0-1000 gen messages
* 1000-2000 errors
*
*/
function subscriber_log_activity( $user_id, $text, $type = 0, $replacements = array() ) {
global $database, $url, $activity_msg_types;
if(is_numeric($text)) {
SE_Language::_preload($text);
SE_Language::load();
$text = SE_Language::_get($text);
}
$text = str_replace( array_keys( $replacements ),
array_values( $replacements ),
$text );
$text = addslashes( $text );
$database->database_query( "INSERT INTO se_semods_userplanactivity (userplanactivity_user_id, userplanactivity_type, userplanactivity_text, userplanactivity_date) VALUES ($user_id, $type, '$text', UNIX_TIMESTAMP( NOW() ))" );
}
function subscriber_log_error( $text, $type = 1000, $user_id = 0 ) {
subscriber_log_activity( $user_id, $text, $type );
}
/********************* SYSTEM FUNCTIONS *********************/
function deleteuser_subscriber( $user_id ) {
global $database;
// Cancel plan(s)
$userplan = new semods_userplan( 0, $user_id );
$userplan->cancel_plan( 1, 1 );
// Delete all user-related data
$database->database_query("DELETE FROM se_semods_userplans WHERE userplan_user_id = $user_id");
$database->database_query("DELETE FROM se_semods_userplanactivity WHERE userplanactivity_user_id = $user_id");
}
/********************* UTILITY FUNCTIONS *********************/
function subscriber_load_pages( $excluded_pages = array() ) {
$system_pages = array( 'ad', 'error', 'login', 'lostpass', 'lostpass_reset', 'user_logout', 'action_delete', 'misc_js' );
$system_pages = array_merge( $system_pages, $excluded_pages );
$pages = array();
// One step back from "/include"
$path = dirname( __FILE__ ) . '/../';
$handle = @opendir( $path );
while($file = @readdir( $handle )) {
if(!@is_file($path.$file))
continue;
$tokenized = explode( '.', $file );
if( strtolower($tokenized[count($tokenized)-1]) != 'php' )
continue;
$contents = @file_get_contents( $path.$file );
if( preg_match( '/\$page\s?+=\s?+\"([^"]+)\"\s?+;/', $contents, $matches ) ) {
$pages[] = $matches[1];
}
}
sort( $pages );
$pages = array_diff( $pages, $system_pages );
$pages_array = $pages;
return $pages_array;
}
/*********************** HOOKS *********************/
function subscriber_hook_signup_decide( $arguments = array() ) {
global $task, $step, $next_task, $new_user, $smarty, $error_message, $database, $is_error;
global $plan_periodtypes;
if($task == "step5") {
$task = "subscriber";
$step = "subscriber";
$next_task = "subscriberdo";
}
if($task == "subscriberdo") {
$plan_is_error = 0;
$signup_plan = semods::getpost('signup_plan');
if(empty($signup_plan)) {
$task = "subscriber";
$plan_is_error = 1;
$plan_error_message = 100014359;
$is_error = $plan_error_message;
}
if(!$plan_is_error) {
// work on plan
$plan = new semods_plan( $signup_plan );
if($plan->plan_exists == 0) {
$task = "subscriber";
$plan_is_error = 1;
$plan_error_message = 100014359;
$is_error = $plan_error_message;
}
}
if(!$plan_is_error) {
$plan_free = $plan->is_free_plan();
if(!$plan_free) {
$cart = new semods_cart();
$cart->create_cart( $new_user->user_info['user_id'] );
$plan->add_to_cart( $cart, $new_user->user_info['user_id'] );
$smarty->assign('plan', $plan);
$task = "subscriber_summary";
} else {
// subscribe the user right away
$plan->plan_subscribe( $new_user->user_info['user_id'], 0, array(), false );
// if all ok
$task = "step5";
}
}
}
if($task == "subscriber_summary") {
$step = "subscriber_summary";
$next_task = "subscriber_summarydo";
}
if($task == "subscriber_summarydo") {
$task = "step5";
}
if($task == "subscriber") {
$step = "subscriber";
$next_task = "subscriberdo";
$rows = $database->database_query("SELECT * FROM se_semods_plans WHERE plan_enabled = 1 AND plan_showonsignup = 1");
while($row = $database->database_fetch_assoc($rows)) {
$row['plan_trialperiodtypetext'] = $plan_periodtypes[ $row['plan_trialperiodtype'] ]['f'];
$row['plan_periodtypetext'] = $plan_periodtypes[ $row['plan_periodtype'] ]['f'];
// CONVERT HTML CHARACTERS BACK
$row['plan_description'] = str_replace("\r\n", "", html_entity_decode( $row['plan_description'] ));
$plans[] = $row;
SE_Language::_preload_multi( $row['plan_trialperiodtypetext'], $row['plan_periodtypetext'] );
}
if(count($plans) > 0) {
$smarty->assign('plans', $plans);
} else {
$task = "step5";
}
}
$currency_code = semods::get_setting('payment_currency', 'USD');
if(empty($currency_code))
$currency_code = 'USD';
$smarty->assign('currency_code', $currency_code);
}
function subscriber_hook_signup_footer( $arguments = array() ) {
global $step, $page, $task;
// subscription page step
if($task == "subscriber") {
$page = "signup_subscriber";
}
if($task == "subscriber_summary") {
$page = "signup_subscriber";
}
}
/********************* EMAIL FUNCTIONS *********************/
function subscriber_send_email( $email_name, $to_email, $replace = array() ) {
global $setting;
$email = semods::db_query_assoc( "SELECT * FROM se_semods_systememails WHERE systememail_name='$email_name'" );
SE_Language::_preload_multi($email['systememail_subject'], $email['systememail_body']);
SE_Language::load();
$subject = htmlspecialchars_decode(SE_Language::_get($email['systememail_subject']), ENT_QUOTES);
$message = htmlspecialchars_decode(SE_Language::_get($email['systememail_body']), ENT_QUOTES);
list( $message, $subject ) = str_replace(
array_keys($replace),
array_values($replace),
array( $message,
$subject ) );
// REPLACE CARRIAGE RETURNS WITH BREAKS
$message = str_replace("\n", "<br>", $message);
$message.="
BateWorld sent this e-mail to you at ".$recipient_email." because your Notification Preferences indicate that you want to receive this information.
<br /><br />
If you do not wish to receive further communications like this you can change your Notification Preferences by <a href='http://bateworld.com/user_account_privacy.php'>clicking here</a>. Please note that it may take up to 10 days to process your request and you must be logged into your account. Alternatively, you can disable your BateWorld account by <a href='http://bateworld.com/user_account_delete.php'>clicking here</a>.
<br /><br />
This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you are not the named addressee or you believe you have received this email by mistake, you should not disseminate, distribute or copy this email. Please notify the sender immediately by email if you have received this email by mistake and delete this email from your system. If you are not the intended recipient, you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.<br /><br />
Visit our <a href='https://bateworld.com/privacy-policy/'>Privacy Policy</a> and <a href='https://bateworld.com/terms-of-service/'>Terms of Service</a> if you have any questions.";
if(semods::get_setting('emailer_enabled') && function_exists("semods_send_mail_queued")) {
semods_send_mail_queued( $setting['setting_email_fromname'],
$setting['setting_email_fromemail'],
$to_email,
$subject,
$message );
} else {
// ENCODE SUBJECT FOR UTF8
$subject="=?UTF-8?B?".base64_encode($subject)."?=";
// SET HEADERS
$from_email = "$setting[setting_email_fromname] <$setting[setting_email_fromemail]>";
$headers = "MIME-Version: 1.0"."\n";
$headers .= "Content-type: text/html; charset=utf-8"."\n";
$headers .= "Content-Transfer-Encoding: 8bit"."\n";
$headers .= "From: $from_email"."\n";
$headers .= "Return-Path: $from_email"."\n";
$headers .= "Reply-To: $from_email";
@mail($to_email, $subject, $message, $headers);
// @mail("phil@b8world.com", "Subscriber Mail", $message);
}
}
/*
* Notify user about new subscription
*
*/
function subscriber_email_subscription_created($user_id, $plan_info) {
global $setting, $url;
global $database;
$user = new se_user( array( $user_id ));
if($user->user_exists == 0)
return;
$user_info = $user->user_info;
// GET SERVER INFO
$prefix = $url->url_base;
$subscription_details = $plan_info['plan_name'];
/*
$subscription_details = "Start Date: \n";
$subscription_details .= "Billing period: \n";
$subscription_details .= "Price: \n";
$subscription_details .= "Next Payment Date: \n";
*/
$replace = array( '[username]' => $user_info['user_username'],
'[displayname]' => $user->user_displayname,
'[email]' => $user_info['user_email'],
'[link]' => "<a href=\"$prefix"."user_plan.php\">$prefix"."user_plan.php</a>",
'[subscription_details]' => $subscription_details
);
$user_pdp_evc=$user_info['user_pdp_evc'];
if($user_pdp_evc == 1 || $user_pdp_evc==3){
subscriber_send_email( "newsubscription", $user->user_info['user_email'], $replace );
}
return true;
}
function subscriber_email_subscription_cancelled($user_id, $plan_info) {
global $setting, $url;
global $database;
$user = new se_user( array( $user_id ));
if($user->user_exists == 0)
return;
$user_info = $user->user_info;
$time = time();
// GET SERVER INFO
$prefix = $url->url_base;
$subscription_details = $plan_info['plan_name'];
$replace = array( '[username]' => $user_info['user_username'],
'[displayname]' => $user->user_displayname,
'[email]' => $user_info['user_email'],
'[link]' => "<a href=\"$prefix"."user_plan.php\">$prefix"."user_plan.php</a>",
'[subscription_details]' => $subscription_details
);
subscriber_send_email( "subscriptioncancelled", $user->user_info['user_email'], $replace );
return true;
}
function subscriber_email_subscription_expired($user_id, $plan_info) {
global $setting, $url;
global $database;
$user = new se_user( array( $user_id ));
if($user->user_exists == 0)
return;
$user_info = $user->user_info;
// GET SERVER INFO
$prefix = $url->url_base;
$subscription_details = $plan_info['plan_name'];
$replace = array( '[username]' => $user_info['user_username'],
'[displayname]' => $user->user_displayname,
'[email]' => $user_info['user_email'],
'[link]' => "<a href=\"$prefix"."user_plan.php\">$prefix"."user_plan.php</a>",
'[subscription_details]' => $subscription_details
);
$user_pdp_evc=$user_info['user_pdp_evc'];
if($user_pdp_evc == 1 || $user_pdp_evc==3){
subscriber_send_email( "subscriptionexpired", $user->user_info['user_email'], $replace );
}
return true;
}
function subscriber_email_subscription_renewed($user_id, $plan_info) {
global $setting, $url;
global $database;
$user = new se_user( array( $user_id ));
if($user->user_exists == 0)
return;
$user_info = $user->user_info;
// GET SERVER INFO
$prefix = $url->url_base;
$subscription_details = $plan_info['plan_name'];
$replace = array( '[username]' => $user_info['user_username'],
'[displayname]' => $user->user_displayname,
'[email]' => $user_info['user_email'],
'[link]' => "<a href=\"$prefix"."user_plan.php\">$prefix"."user_plan.php</a>",
'[subscription_details]' => $subscription_details
);
subscriber_send_email( "subscriptionrenewed", $user->user_info['user_email'], $replace );
return true;
}
function subscriber_email_subscription_suspended_no_payment($user_id, $plan_info) {
global $setting, $url;
global $database;
$user = new se_user( array( $user_id ));
if($user->user_exists == 0)
return;
$user_info = $user->user_info;
// GET SERVER INFO
$prefix = $url->url_base;
$subscription_details = $plan_info['plan_name'];
$replace = array( '[username]' => $user_info['user_username'],
'[displayname]' => $user->user_displayname,
'[email]' => $user_info['user_email'],
'[link]' => "<a href=\"$prefix"."user_plan.php\">$prefix"."user_plan.php</a>",
'[subscription_details]' => $subscription_details
);
subscriber_send_email( "subscriptionpaymentfailed", $user->user_info['user_email'], $replace );
return true;
}
function subscriber_email_subscription_soon_expires($user_info, $plan_info) {
global $setting, $url;
global $database;
$user = new se_user( array( $user_id ));
if($user->user_exists == 0)
return;
$user_info = $user->user_info;
// GET SERVER INFO
$prefix = $url->url_base;
$subscription_details = $plan_info['plan_name'];
$replace = array( '[username]' => $user_info['user_username'],
'[displayname]' => $user->user_displayname,
'[email]' => $user_info['user_email'],
'[link]' => "<a href=\"$prefix"."user_plan.php\">$prefix"."user_plan.php</a>",
'[subscription_details]' => $subscription_details
);
subscriber_send_email( "subscriptionexpiration", $user->user_info['user_email'], $replace );
return true;
}
//function subscriber_email_subscription_refunded($user_info, $plan_info) {
//
//}
/********************* BACKGROUND JOBS *********************/
function subscriber_scheduler_tasks() {
global $database;
// PROCESS ALL QUEUED TASKS
$processed_tasks = array();
for(;;) {
$task = semods::db_query_assoc('SELECT * FROM se_semods_plantasks ORDER BY plantask_id ASC LIMIT 1');
// No more tasks
if($task === false)
break;
$task_processed = false;
switch($task['plantask_type']) {
case 'cancel_subscription':
if($task['plantask_paymentgw'] != 0) {
$paymentgw = semods_paymengtw_factory( $task['plantask_paymentgw'] );
// Log Error
if(!$paymentgw) {
subscriber_log_error( "Error cancelling plan - Failed instantiating Payment Handler (id={$task['plantask_paymentgw']})", 1000 );
} else {
$result = $paymentgw->cancel_subscription( array( 'fast' => 0, 'metadata' => $task['plantask_metadata'] ) );
// Log Error
if(!$result) {
subscriber_log_error( "Error cancelling plan - (id={$task['plantask_paymentgw']})", 1001 );
// TODO
// email_notify_admin(error cancelling ? requeue task? );
}
}
}
if($result) {
// email_notify(plan_cancelled);
} else {
// email_notify_admin(error cancelling plan id XX);
}
$task_processed = true;
break;
}
if($task_processed) {
$database->database_query("DELETE FROM se_semods_plantasks WHERE plantask_id = {$task['plantask_id']}");
}
}
}
function subscriber_scheduler_daily() {
global $database;
// PROCESS ALL PLANS EXPIRATION
// Possible state changes:
// active -> expired, cancelled -> expired, active -> suspended_paymentfailed, suspended_paymentfailed -> expired
// safety of 1 day
$safety_delta = 86400;
// period of no payment to cancel - 0 days
$suspend_to_cancel_period = 0;
// take plans with state Active, Cancelled, Suspended, Suspended - payment failed and not Free for life (0 expiration date)
$sql = 'SELECT * FROM se_semods_userplans WHERE (userplan_state = 0 OR userplan_state = 3 OR userplan_state = 4 OR userplan_state = 7) AND userplan_expiredate != 0';
$rows = $database->database_query($sql);
if ($rows === false) {
scheduler_log_error( 'Database Error - could not query the database.' . ' MySQL Error: ' . $database->database_error() );
return false;
}
if($database->database_num_rows($rows) > 0) {
$now = time();
// TODO: make date in sql query
// loop through records
while ($row = $database->database_fetch_assoc($rows)) {
if($row['userplan_plan_id'] != 1){ // userplan is not 90-day recurring
// if plan suspended - failed payment & waitin period is over -> cancel & expire plan
if($now > intval($row['userplan_expiredate']) + $suspend_to_cancel_period ) {
//if($row['userplan_state'] == 7) {
// if plan period expired -> downgrade & change state to "suspended_payment_failed"
$userplan = new semods_userplan( $row['userplan_id'] );
$userplan->expire_plan();
//}
} elseif($now > intval($row['userplan_expiredate']) + $safety_delta) {
// if plan period expired -> downgrade & change state to "suspended_payment_failed"
$userplan = new semods_userplan( $row['userplan_id'] );
$userplan->suspend_plan_failed_payment();
}
}
}
}
}
?>