| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if( !defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
|
| 6 |
class wpForoSubscribe{ |
| 7 |
public $default; |
| 8 |
public $options; |
| 9 |
|
| 10 |
public $already_sent_emails = array(); |
| 11 |
|
| 12 |
static $cache = array( 'subscribe' => array() ); |
| 13 |
|
| 14 |
function __construct(){ |
| 15 |
$this->init_defaults(); |
| 16 |
$this->init_options(); |
| 17 |
} |
| 18 |
|
| 19 |
private function init_defaults(){ |
| 20 |
$blogname = get_option('blogname', ''); |
| 21 |
$adminemail = get_option('admin_email'); |
| 22 |
|
| 23 |
$this->default = new stdClass; |
| 24 |
|
| 25 |
$this->default->options = array ( |
| 26 |
'from_name' => $blogname . ' - Forum', |
| 27 |
'from_email' => $adminemail, |
| 28 |
'admin_emails' => $adminemail, |
| 29 |
'new_topic_notify' => 1, |
| 30 |
'new_reply_notify' => 0, |
| 31 |
'confirmation_email_subject' => "Please confirm subscription to [entry_title]", |
| 32 |
'confirmation_email_message' => "Hello [member_name]!<br>\r\n Thank you for subscribing.<br>\r\n This is an automated response.<br>\r\n We are glad to inform you that after confirmation you will get updates from - [entry_title].<br>\r\n Please click on link below to complete this step.<br>\r\n [confirm_link]" , |
| 33 |
'new_topic_notification_email_subject' => "New Topic" , |
| 34 |
'new_topic_notification_email_message' => "Hello [member_name]!<br>\r\n New topic has been created on your subscribed forum - [forum].\r\n <br><br>\r\n <strong>[topic_title]</strong>\r\n <blockquote>\r\n [topic_desc]\r\n </blockquote>\r\n <br><hr>\r\n If you want to unsubscribe from this forum please use the link below.<br>\r\n [unsubscribe_link]" , |
| 35 |
'new_post_notification_email_subject' => "New Reply" , |
| 36 |
'new_post_notification_email_message' => "Hello [member_name]!<br>\r\n New reply has been posted on your subscribed topic - [topic].\r\n <br><br>\r\n <strong>[reply_title]</strong>\r\n <blockquote >\r\n [reply_desc]\r\n </blockquote>\r\n <br><hr>\r\n If you want to unsubscribe from this topic please use the link below.<br>\r\n [unsubscribe_link]" , |
| 37 |
'report_email_subject' => "Forum Post Report", |
| 38 |
'report_email_message' => "<strong>Report details:</strong>\r\n Reporter: [reporter], <br>\r\n Message: [message],<br>\r\n <br>\r\n [post_url]", |
| 39 |
'reset_password_email_message' => "Hello! <br>\r\n\r\n You asked us to reset your password for your account using the email address [user_login]. <br>\r\n\r\n If this was a mistake, or you didn't ask for a password reset, just ignore this email and nothing will happen. <br>\r\n\r\n To reset your password, visit the following address: <br>\r\n\r\n [reset_password_url] <br>\r\n\r\n Thanks!", |
| 40 |
//'spam_notification_email_subject' => 'New Banned User', |
| 41 |
//'spam_notification_email_message' => "Hello [member_name]!<br>\r\n Please check this user's topics/posts and consider to Delete or Unban.<br>\r\n User Activity: [profile_activity_url]", |
| 42 |
'update' => '1', |
| 43 |
'user_mention_notify' => 1, |
| 44 |
'user_mention_email_subject' => "You have been mentioned in forum post", |
| 45 |
'user_mention_email_message' => "Hi [mentioned-user-name]! <br>\r\n\r\n You have been mentioned in a post on \"[topic-title]\" by [author-user-name].<br/><br/>\r\n\r\n Post URL: [post-url]" |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
private function init_options(){ |
| 50 |
$this->options = get_wpf_option('wpforo_subscribe_options', $this->default->options); |
| 51 |
} |
| 52 |
|
| 53 |
function get_confirm_key(){ |
| 54 |
return substr(md5(rand().time()), 0, 32); |
| 55 |
} |
| 56 |
|
| 57 |
function add( $args = array() ){ |
| 58 |
if( empty($args) && empty($_REQUEST['sbscrb']) ) return FALSE; |
| 59 |
if( empty($args) && !empty($_REQUEST['sbscrb']) ) $args = $_REQUEST['sbscrb']; |
| 60 |
if( !isset($args['active']) || !$args['active'] ) $args['active'] = 0; |
| 61 |
|
| 62 |
extract( $args ); |
| 63 |
if( !isset($itemid) || !( (isset($userid) && $userid) || (isset($user_email) && $user_email) ) || !isset($type) || !$type ) return FALSE; |
| 64 |
|
| 65 |
if( empty($confirmkey) ) $confirmkey = $this->get_confirm_key(); |
| 66 |
|
| 67 |
if(WPF()->db->insert( |
| 68 |
WPF()->tables->subscribes, |
| 69 |
array( |
| 70 |
'itemid' => wpforo_bigintval($itemid), |
| 71 |
'type' => sanitize_text_field($type), |
| 72 |
'confirmkey' => sanitize_text_field($confirmkey), |
| 73 |
'userid' => wpforo_bigintval($userid), |
| 74 |
'active' => $active, |
| 75 |
'user_name' => ( isset($user_name) && $user_name ? $user_name : ''), |
| 76 |
'user_email' => ( isset($user_email) && $user_email ? $user_email : '') |
| 77 |
), |
| 78 |
array( |
| 79 |
'%d', |
| 80 |
'%s', |
| 81 |
'%s', |
| 82 |
'%d', |
| 83 |
'%d', |
| 84 |
'%s', |
| 85 |
'%s' |
| 86 |
) |
| 87 |
)){ |
| 88 |
if( isset($active) && $active == 1 ){ |
| 89 |
WPF()->notice->add('You have been successfully subscribed', 'success'); |
| 90 |
}else{ |
| 91 |
WPF()->notice->add('Success! Thank you. Please check your email and click confirmation link below to complete this step.', 'success'); |
| 92 |
} |
| 93 |
return $confirmkey; |
| 94 |
} |
| 95 |
|
| 96 |
WPF()->notice->add('Can\'t subscribe to this item', 'error'); |
| 97 |
return FALSE; |
| 98 |
} |
| 99 |
|
| 100 |
function edit( $confirmkey = '' ){ |
| 101 |
if( !$confirmkey && isset($_REQUEST['key']) && $_REQUEST['key'] ) $confirmkey = $_REQUEST['key']; |
| 102 |
if( !$confirmkey ){ |
| 103 |
WPF()->notice->add('Invalid request!', 'error'); |
| 104 |
return FALSE; |
| 105 |
} |
| 106 |
|
| 107 |
if( WPF()->db->update( |
| 108 |
WPF()->tables->subscribes, |
| 109 |
array( 'active' => 1 ), |
| 110 |
array( 'confirmkey' => sanitize_text_field($confirmkey) ), |
| 111 |
array( '%d' ), |
| 112 |
array( '%s' ) |
| 113 |
) ){ |
| 114 |
if( $sbs = $this->get_subscribe($confirmkey) ) WPF()->member->edit_is_email_confirmed($sbs['userid'], 1); |
| 115 |
|
| 116 |
WPF()->notice->add('You have been successfully subscribed', 'success'); |
| 117 |
return TRUE; |
| 118 |
} |
| 119 |
|
| 120 |
WPF()->notice->add('Your subscription for this item could not be confirmed', 'error'); |
| 121 |
return FALSE; |
| 122 |
} |
| 123 |
|
| 124 |
public function reset($data = array(), $all = '', $user = null){ |
| 125 |
if(!$user && !WPF()->current_userid && !WPF()->current_user_email) return false; |
| 126 |
if (!$user) $user = ( WPF()->current_userid ? WPF()->current_userid : WPF()->current_user_email ); |
| 127 |
$data = array_filter( (array) $data ); |
| 128 |
$args = array( |
| 129 |
'itemid' => 0, |
| 130 |
'type' => '', |
| 131 |
'userid' => 0, |
| 132 |
'active' => ( wpforo_feature('subscribe_conf') ? 0 : 1 ), |
| 133 |
'user_name' => '', |
| 134 |
'user_email' => '' |
| 135 |
); |
| 136 |
if( is_numeric($user) ){ |
| 137 |
$args['userid'] = $user; |
| 138 |
$where = WPF()->db->prepare("`userid` = %d", $user); |
| 139 |
}else{ |
| 140 |
$args['user_email'] = $user; |
| 141 |
$args['user_name'] = WPF()->current_user_display_name; |
| 142 |
$where = WPF()->db->prepare("`user_email` = %s", $user); |
| 143 |
} |
| 144 |
|
| 145 |
if( $this->is_email_confirmed($user) ) $args['active'] = 1; |
| 146 |
|
| 147 |
$types = array('forum', 'forum-topic'); |
| 148 |
if( !$all ) array_push($types, 'forums', 'forums-topics'); |
| 149 |
$sql = "DELETE FROM `".WPF()->tables->subscribes."` WHERE `type` IN('".implode("','", $types)."') AND " . $where; |
| 150 |
if(!$all && $data){ |
| 151 |
$forumids = array_keys($data); |
| 152 |
$sql .= " AND `itemid` NOT IN(".implode(',', $forumids).")"; |
| 153 |
} |
| 154 |
WPF()->db->query($sql); |
| 155 |
|
| 156 |
if(!$all && $data){ |
| 157 |
foreach ($data as $forumid => $type){ |
| 158 |
$sql = "SELECT `subid` FROM `".WPF()->tables->subscribes."` WHERE `type` IN('forum', 'forum-topic') AND `itemid` = %d AND " . $where; |
| 159 |
$sql = WPF()->db->prepare($sql, $forumid); |
| 160 |
if( $subid = WPF()->db->get_var($sql) ){ |
| 161 |
WPF()->db->update( |
| 162 |
WPF()->tables->subscribes, |
| 163 |
array('type' => sanitize_text_field($type), 'active' => $args['active']), |
| 164 |
array('subid' => $subid), |
| 165 |
array('%s', '%d'), |
| 166 |
array('%d') |
| 167 |
); |
| 168 |
}else{ |
| 169 |
$args['itemid'] = intval($forumid); |
| 170 |
$args['type'] = sanitize_text_field($type); |
| 171 |
$this->add($args); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
if ($all){ |
| 177 |
$sql = "SELECT `subid` FROM `".WPF()->tables->subscribes."` WHERE `type` IN('forums', 'forums-topics') AND `itemid` = 0 AND " . $where; |
| 178 |
if( $subid = WPF()->db->get_var($sql) ){ |
| 179 |
WPF()->db->update( |
| 180 |
WPF()->tables->subscribes, |
| 181 |
array('type' => sanitize_text_field($all), 'active' => $args['active']), |
| 182 |
array('subid' => $subid), |
| 183 |
array('%s', '%d'), |
| 184 |
array('%d') |
| 185 |
); |
| 186 |
}else{ |
| 187 |
$args['itemid'] = 0; |
| 188 |
$args['type'] = sanitize_text_field($all); |
| 189 |
$this->add($args); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
return true; |
| 194 |
} |
| 195 |
|
| 196 |
function delete( $confirmkey = '' ){ |
| 197 |
if( !$confirmkey && isset($_REQUEST['confirmkey']) && $_REQUEST['confirmkey'] ) $confirmkey = $_REQUEST['confirmkey']; |
| 198 |
if( !$confirmkey ){ |
| 199 |
WPF()->notice->add('Invalid request!', 'error'); |
| 200 |
return FALSE; |
| 201 |
} |
| 202 |
if( WPF()->db->delete( WPF()->tables->subscribes, array( 'confirmkey' => sanitize_text_field($confirmkey) ), array( '%s' ) ) ){ |
| 203 |
WPF()->notice->add('You have been successfully unsubscribed', 'success'); |
| 204 |
return TRUE; |
| 205 |
} |
| 206 |
|
| 207 |
WPF()->notice->add('Could not be unsubscribe from this item', 'error'); |
| 208 |
return FALSE; |
| 209 |
} |
| 210 |
|
| 211 |
function get_subscribe( $args = array() ){ |
| 212 |
|
| 213 |
$cache = WPF()->cache->on('memory_cashe'); |
| 214 |
|
| 215 |
if( is_string($args) ) $args = array("confirmkey" => sanitize_text_field($args)); |
| 216 |
if( empty($args) && !empty($_REQUEST['sbscrb']) ) $args = $_REQUEST['sbscrb']; |
| 217 |
if( empty($args) ) return FALSE; |
| 218 |
extract( $args, EXTR_OVERWRITE ); |
| 219 |
if( (!isset($itemid) || !$itemid || !( (isset($userid) && $userid) || (isset($user_email) && $user_email) ) || !isset($type) || !$type) |
| 220 |
&& (!isset($confirmkey) || !$confirmkey) ) return FALSE; |
| 221 |
if( isset($confirmkey) && $confirmkey){ |
| 222 |
$where = " `confirmkey` = '".esc_sql(sanitize_text_field($confirmkey))."'"; |
| 223 |
}elseif( isset($itemid) && $itemid && isset($userid) && $userid && isset($type) && $type ){ |
| 224 |
$where = " `itemid` = ".wpforo_bigintval($itemid)." AND `userid` = ".wpforo_bigintval($userid)." AND `type` = '".esc_sql(sanitize_text_field($type))."'"; |
| 225 |
}elseif( isset($itemid) && $itemid && isset($user_email) && $user_email && isset($type) && $type ){ |
| 226 |
$where = " `itemid` = ".wpforo_bigintval($itemid)." AND `user_email` = '".esc_sql($user_email)."' AND `type` = '".esc_sql(sanitize_text_field($type))."'"; |
| 227 |
}else{ |
| 228 |
return FALSE; |
| 229 |
} |
| 230 |
$UID = ( $userid ? $userid : $user_email ); |
| 231 |
if( $cache && isset(self::$cache['subscribe'][$itemid][$UID][$type]) ){ |
| 232 |
return self::$cache['subscribe'][$itemid][$UID][$type]; |
| 233 |
} |
| 234 |
$sql = "SELECT * FROM `".WPF()->tables->subscribes."` WHERE " . $where; |
| 235 |
$subscribe = WPF()->db->get_row($sql, ARRAY_A); |
| 236 |
if($cache && !empty($subscribe)){ |
| 237 |
self::$cache['subscribe'][$itemid][$UID][$type] = $subscribe; |
| 238 |
} |
| 239 |
return $subscribe; |
| 240 |
} |
| 241 |
|
| 242 |
function get_subscribes( $args = array(), &$items_count = 0 ){ |
| 243 |
|
| 244 |
$default = array( |
| 245 |
'itemid' => NULL, |
| 246 |
'type' => array(), // topic | forum |
| 247 |
'userid' => NULL, // |
| 248 |
'active' => 1, |
| 249 |
'orderby' => 'subid', // order by `field` |
| 250 |
'order' => 'DESC', // ASC DESC |
| 251 |
'offset' => NULL, // OFFSET |
| 252 |
'row_count' => NULL, // ROW COUNT |
| 253 |
); |
| 254 |
|
| 255 |
$args = wpforo_parse_args( $args, $default ); |
| 256 |
extract($args); |
| 257 |
|
| 258 |
$sql = "SELECT * FROM `".WPF()->tables->subscribes."`"; |
| 259 |
$wheres = array(); |
| 260 |
|
| 261 |
if( $type ) $wheres[] = " `type` IN( '" . implode("','", array_map('esc_sql', (array) $type ) ) . "')"; |
| 262 |
if( !is_null($active) ) $wheres[] = " `active` = " . intval($active); |
| 263 |
if( !is_null($itemid) ) $wheres[] = " `itemid` = " . wpforo_bigintval($itemid); |
| 264 |
if( !is_null($userid) ) $wheres[] = " `userid` = " . wpforo_bigintval($userid); |
| 265 |
|
| 266 |
if(!empty($wheres)) $sql .= " WHERE " . implode( " AND ", $wheres ); |
| 267 |
|
| 268 |
$item_count_sql = preg_replace('#SELECT.+?FROM#isu', 'SELECT count(*) FROM', $sql); |
| 269 |
if( $item_count_sql ) $items_count = WPF()->db->get_var($item_count_sql); |
| 270 |
|
| 271 |
$sql .= " ORDER BY `$orderby` " . $order; |
| 272 |
|
| 273 |
if( !is_null($row_count) ){ |
| 274 |
if( !is_null($offset) ){ |
| 275 |
$sql .= esc_sql(" LIMIT $offset,$row_count"); |
| 276 |
}else{ |
| 277 |
$sql .= esc_sql(" LIMIT $row_count"); |
| 278 |
} |
| 279 |
} |
| 280 |
return WPF()->db->get_results($sql, ARRAY_A); |
| 281 |
|
| 282 |
} |
| 283 |
|
| 284 |
function get_confirm_link($args){ |
| 285 |
if(is_string($args)) return wpforo_home_url( "?wpforo=sbscrbconfirm&key=" . sanitize_text_field($args) ); |
| 286 |
|
| 287 |
if($args['type'] == 'forum'){ |
| 288 |
$url = WPF()->forum->get_forum_url($args['itemid']) . '/'; |
| 289 |
}elseif($args['type'] == 'topic'){ |
| 290 |
$url = WPF()->topic->get_topic_url($args['itemid']) . '/'; |
| 291 |
}else{ |
| 292 |
$url = wpforo_home_url(); |
| 293 |
} |
| 294 |
return wpforo_home_url( $url . "?wpforo=sbscrbconfirm&key=" . sanitize_text_field($args['confirmkey']) ); |
| 295 |
} |
| 296 |
|
| 297 |
function get_unsubscribe_link($confirmkey){ |
| 298 |
return wpforo_home_url( "?wpforo=unsbscrb&key=" . sanitize_text_field($confirmkey) ); |
| 299 |
} |
| 300 |
|
| 301 |
public function is_email_confirmed($user = null){ |
| 302 |
if(!$user && !WPF()->current_userid && !WPF()->current_user_email) return false; |
| 303 |
if (!$user) $user = ( WPF()->current_userid ? WPF()->current_userid : WPF()->current_user_email ); |
| 304 |
|
| 305 |
$sql = ( is_numeric($user) ? "`userid` = %d" : "`user_email` = %s" ); |
| 306 |
$where = WPF()->db->prepare($sql, $user); |
| 307 |
|
| 308 |
if( WPF()->current_userid === $user && wpfval(WPF()->current_user, 'is_email_confirmed') ){ |
| 309 |
$has_confirmed = WPF()->current_user['is_email_confirmed']; |
| 310 |
}elseif( is_numeric($user) ){ |
| 311 |
$has_confirmed = WPF()->member->get_is_email_confirmed(WPF()->current_userid); |
| 312 |
}else{ |
| 313 |
$has_confirmed = WPF()->db->get_var( "SELECT `subid` FROM `".WPF()->tables->subscribes."` WHERE `active` = 1 AND " . $where ); |
| 314 |
} |
| 315 |
|
| 316 |
return (bool) $has_confirmed; |
| 317 |
} |
| 318 |
|
| 319 |
} |