Discuz!官方免费开源建站系统

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索

后台的邮件通知更新天数功能分析

[复制链接]
dongdong0925 发表于 2011-5-12 15:56:08 | 显示全部楼层 |阅读模式
本帖最后由 dongdong0925 于 2011-5-12 15:55 编辑

邮件通知更新天数功能介绍:当用户多少天没有登录站点的时候才会给其发送邮件通知。


邮件通知更新天数的位置:后台-》全局-》空间设置-》基本设置下的邮件通知更新天数,如图。
设置为0则不启用该功能,单位为天。




功能分析:

比如用户A回复了用户B的帖子,这时候在用户A回复用户B帖子的同时会执行下面这个操作。
具体文件:source\include\post\post_newreply.php的98行附近。
  1. notification_add($post['authorid'], 'pcomment', 'comment_add', array(
  2.                         'tid' => $_G['tid'],
  3.                         'pid' => $_G['gp_pid'],
  4.                         'subject' => $thread['subject'],
  5.                         'commentmsg' => cutstr(str_replace(array('[b]', '[/b]', '[/color]'), '', preg_replace("/\[color=([#\w]+?)\]/i", "", stripslashes($comment))), 200)
  6.                 ));
复制代码

notification_add()函数是在source/function/function_core.php文件定义的,1897行附近,具体代码如下。
  1. function notification_add($touid, $type, $note, $notevars = array(), $system = 0) {
  2.         global $_G;

  3.         $tospace = array('uid'=>$touid);
  4.         space_merge($tospace, 'field_home');
  5.         $filter = empty($tospace['privacy']['filter_note'])?array():array_keys($tospace['privacy']['filter_note']);

  6.         if($filter && (in_array($type.'|0', $filter) || in_array($type.'|'.$_G['uid'], $filter))) {
  7.                 return false;
  8.         }

  9.         $notevars['actor'] = "<a href="home.php?mod=space&uid=$_G[uid]">".$_G['member']['username']."</a>";
  10.         if(!is_numeric($type)) {
  11.                 $vars = explode(':', $note);
  12.                 if(count($vars) == 2) {
  13.                         $notestring = lang('plugin/'.$vars[0], $vars[1], $notevars);
  14.                 } else {
  15.                         $notestring = lang('notification', $note, $notevars);
  16.                 }
  17.                 $frommyapp = false;
  18.         } else {
  19.                 $frommyapp = true;
  20.                 $notestring = $note;
  21.         }

  22.         $oldnote = array();
  23.         if($notevars['from_id'] && $notevars['from_idtype']) {
  24.                 $oldnote = DB::fetch_first("SELECT * FROM ".DB::table('home_notification')."
  25.                         WHERE uid='$touid' AND from_id='$notevars[from_id]' AND from_idtype='$notevars[from_idtype]'");
  26.         }
  27.         if(empty($oldnote['from_num'])) $oldnote['from_num'] = 0;
  28.         $notevars['from_num'] = $notevars['from_num'] ? $notevars['from_num'] : 1;
  29.         $setarr = array(
  30.                 'uid' => $touid,
  31.                 'type' => $type,
  32.                 'new' => 1,
  33.                 'authorid' => $_G['uid'],
  34.                 'author' => $_G['username'],
  35.                 'note' => addslashes($notestring),
  36.                 'dateline' => $_G['timestamp'],
  37.                 'from_id' => $notevars['from_id'],
  38.                 'from_idtype' => $notevars['from_idtype'],
  39.                 'from_num' => ($oldnote['from_num']+$notevars['from_num'])
  40.         );
  41.         if($system) {
  42.                 $setarr['authorid'] = 0;
  43.                 $setarr['author'] = '';
  44.         }

  45.         if($oldnote['id']) {
  46.                 DB::update('home_notification', $setarr, array('id'=>$oldnote['id']));
  47.         } else {
  48.                 $oldnote['new'] = 0;
  49.                 DB::insert('home_notification', $setarr);
  50.         }

  51.         if(empty($oldnote['new'])) {
  52.                 DB::query("UPDATE ".DB::table('common_member')." SET newprompt=newprompt+1 WHERE uid='$touid'");

  53.                 require_once libfile('function/mail');
  54.                 $mail_subject = lang('notification', 'mail_to_user');
  55.                 sendmail_touser($touid, $mail_subject, $notestring, $frommyapp ? 'myapp' : $type);
  56.         }

  57.         if(!$system && $_G['uid'] && $touid != $_G['uid']) {
  58.                 DB::query("UPDATE ".DB::table('home_friend')." SET num=num+1 WHERE uid='$_G[uid]' AND fuid='$touid'");
  59.         }
  60. }
复制代码


前面都是给相应用户发送提醒操作,具体不再此处进行分析,单独看发送邮件代码,如下。
  1. require_once libfile('function/mail');
  2.                 $mail_subject = lang('notification', 'mail_to_user');
  3.                 sendmail_touser($touid, $mail_subject, $notestring, $frommyapp ? 'myapp' : $type);
复制代码


这里会执行sendmail_touser()函数,$mail_subject是邮件标题,$notestring是提醒的内容,$frommyapp是操作的类型。
sendmail_touser()函数是在source\function\function_mail.php文件里定义的,204行附近,具体代码如下。
  1. function sendmail_touser($touid, $subject, $message, $mailtype='') {
  2.         global $_G;

  3.         if(empty($_G['setting']['sendmailday'])) return false;

  4.         require_once libfile('function/home');
  5.         $tospace = getspace($touid);
  6.         if(empty($tospace['email'])) return false;

  7.         space_merge($tospace, 'field_home');
  8.         space_merge($tospace, 'status');

  9.         $acceptemail = $tospace['acceptemail'];
  10.         if(!empty($acceptemail[$mailtype]) && $_G['timestamp'] - $tospace['lastvisit'] > $_G['setting']['sendmailday']*86400) {
  11.                 if(empty($tospace['lastsendmail'])) {
  12.                         $tospace['lastsendmail'] = $_G['timestamp'];
  13.                 }
  14.                 $sendtime = $tospace['lastsendmail'] + $acceptemail['frequency'];

  15.                 $query = DB::query("SELECT * FROM ".DB::table('common_mailcron')." WHERE touid='$touid' LIMIT 1");
  16.                 if($value = DB::fetch($query)) {
  17.                         $cid = $value['cid'];
  18.                         if($value['sendtime'] < $sendtime) $sendtime = $value['sendtime'];
  19.                         DB::update('common_mailcron', array('email'=>addslashes($tospace['email']), 'sendtime'=>$sendtime), array('cid'=>$cid));
  20.                 } else {
  21.                         $cid = DB::insert('common_mailcron', array('touid'=>$touid, 'email'=>addslashes($tospace['email']), 'sendtime'=>$sendtime), 1);
  22.                 }
  23.                 $message = preg_replace("/href\="(?!http\:\/\/)(.+?)"/i", 'href="'.$_G['siteurl'].'\\1"', $message);
  24.                 $setarr = array(
  25.                         'cid' => $cid,
  26.                         'subject' => addslashes($subject),
  27.                         'message' => addslashes($message),
  28.                         'dateline' => $_G['timestamp']
  29.                 );
  30.                 DB::insert('common_mailqueue', $setarr);
  31.                 return true;
  32.         }
  33.         return false;
  34. }
复制代码


下面具体分析代码的执行过程。

  1. if(empty($_G['setting']['sendmailday'])) return false;
复制代码

首先会判断$_G['setting']['sendmailday']变量是否存在,如果不存在则直接返回false。$_G['setting']['sendmailday']就是后台设置的邮件通知更新天数。
  1. require_once libfile('function/home');
  2.         $tospace = getspace($touid);
  3.         if(empty($tospace['email'])) return false;
复制代码

getspace($touid)是要获取发送对象的详细信息,如果该用户没有设置邮箱,就直接返回false.
  1. space_merge($tospace, 'field_home');
  2.         space_merge($tospace, 'status');

  3.         $acceptemail = $tospace['acceptemail'];
复制代码


前两句是查询该用户的相关信息。

$tospace['acceptemail']是该用户设置的邮件提醒。邮件提醒设置的具体位置在家园的设置-》邮件提醒,如图。




  1. if(!empty($acceptemail[$mailtype]) && $_G['timestamp'] - $tospace['lastvisit'] > $_G['setting']['sendmailday']*86400) {
复制代码
首先判断该类型的操作是否进行邮件提醒(判断$
acceptemail[$mailtype]变量是否存在),同时判断当前时间(TIMESTAMP)- 用户上一次的活动时间($tospace['lastvisit']) > 后台设置的邮件通知更新天数($_G['setting']['sendmailday']*86400。不满足则跳出if判断,返回false。

  1. if(empty($tospace['lastsendmail'])) {
  2. $tospace['lastsendmail'] = $_G['timestamp'];
  3. }
复制代码


如果用户上一次发送邮件的时间不存在,那么就赋予当前时间给
$tospace['lastsendmail'].

  1. $sendtime = $tospace['lastsendmail'] + $acceptemail['frequency'];
复制代码


计算发送邮件的时间,
$acceptemail['frequency']为邮件提醒里的发送频率设置。

  1. $query = DB::query("SELECT * FROM ".DB::table('common_mailcron')." WHERE touid='$touid' LIMIT 1");
  2.                 if($value = DB::fetch($query)) {
  3.                         $cid = $value['cid'];
  4.                         if($value['sendtime'] < $sendtime) $sendtime = $value['sendtime'];
  5.                         DB::update('common_mailcron', array('email'=>addslashes($tospace['email']), 'sendtime'=>$sendtime), array('cid'=>$cid));
  6.                 } else {
  7.                         $cid = DB::insert('common_mailcron', array('touid'=>$touid, 'email'=>addslashes($tospace['email']), 'sendtime'=>$sendtime), 1);
  8.                 }
复制代码


首先到common_mailcron表里查询是否有向该用户发送邮件的邮件任务。如果存在就更新任务,如果不存在就往表里新插入一条任务。


  1. $message = preg_replace("/href\="(?!http\:\/\/)(.+?)"/i", 'href="'.$_G['siteurl'].'\\1"', $message);
  2.                 $setarr = array(
  3.                         'cid' => $cid,
  4.                         'subject' => addslashes($subject),
  5.                         'message' => addslashes($message),
  6.                         'dateline' => $_G['timestamp']
  7.                 );
  8.                 DB::insert('common_mailqueue', $setarr);
  9.                 return true;
复制代码


前面是对内容进行处理,然后往common_mailqueue表里插入一条数据,返回true。

至此,相应的邮件任务已经添加,但是邮件还没有发送。下面看下邮件任务是如何触发处理的。

在template\default\common\footer.htm文件,79行附近,找到如下代码。
  1. <!--{if !isset($_G['cookie']['sendmail'])}-->
  2.         <script type="text/javascript" src="home.php?mod=misc&ac=sendmail&rand=$_G[timestamp]"></script>
  3.         <!--{/if}-->
复制代码


如果不存在$_G['cookie']['sendmail'],那么就会输出一个js。
看js的src地址,是进入到了source\include\misc\misc_sendmail.php文件中,下面只分析重点,其他不做分析。
  1. $query = DB::query("SELECT * FROM ".DB::table('common_mailcron')." WHERE sendtime<='$_G[timestamp]' ORDER BY sendtime LIMIT 0,$pernum");
  2. while ($value = DB::fetch($query)) {
  3.         if($value['touid']) $touids[$value['touid']] = $value['touid'];
  4.         $cids[] = $value['cid'];
  5.         $list[$value['cid']] = $value;
  6. }

  7. if(empty($cids)) exit();

  8. $query = DB::query("SELECT * FROM ".DB::table('common_mailqueue')." WHERE cid IN (".dimplode($cids).")");
  9. while ($value = DB::fetch($query)) {
  10.         $sublist[$value['cid']][] = $value;
  11. }
复制代码


从common_mailcron和common_mailqueue表分别查询数据然后赋值。
  1. if($touids) {
  2.         DB::query("UPDATE ".DB::table('common_member_status')." SET lastsendmail='$_G[timestamp]' WHERE uid IN (".dimplode($touids).")");
  3. }
复制代码


更新common_member_status表里用户的最后发送邮件时间。

  1. DB::query("DELETE FROM ".DB::table('common_mailcron')." WHERE cid IN (".dimplode($cids).")");
  2. DB::query("DELETE FROM ".DB::table('common_mailqueue')." WHERE cid IN (".dimplode($cids).")");
复制代码


删除common_mailcron表里跟common_mailqueue表里删除相应的任务。

  1. require_once libfile('function/mail');

  2. foreach ($list as $cid => $value) {
  3.         $mlist = $sublist[$cid];
  4.         if($value['email'] && $mlist) {
  5.                 $subject = getstr($mlist[0]['subject'], 80, 0, 0, 0, -1);
  6.                 $message = '';
  7.                 foreach ($mlist as $subvalue) {
  8.                         if($subvalue['message']) {
  9.                                 $message .= "<br><strong>$subvalue[subject]</strong><br>$subvalue[message]<br>";
  10.                         } else {
  11.                                 $message .= $subvalue['subject'].'<br>';
  12.                         }
  13.                 }
  14.                 if(!sendmail($value['email'], $subject, $message)) {
  15.                         runlog('sendmail', "$value[email] sendmail failed.");
  16.                 }
  17.         }
  18. }
复制代码
执行相应的发送邮件操作。

至此邮件就发送出去了。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

评分

1

查看全部评分

64243354 发表于 2011-5-12 16:16:00 | 显示全部楼层
老大的帖子 常给我抢到sf的 高興~
回复

使用道具 举报

ybs885 发表于 2011-5-12 16:16:58 | 显示全部楼层
支持啊。。。。。。
回复

使用道具 举报

花到飞靡 发表于 2011-5-12 16:17:46 | 显示全部楼层
前排支持一下~~~
回复

使用道具 举报

wukunda 发表于 2011-5-12 16:19:45 | 显示全部楼层
回复

使用道具 举报

ARCHY` 发表于 2011-5-12 20:59:46 | 显示全部楼层
之前没有回复过帖子的会员就不会发送这个推送了
回复

使用道具 举报

-初恋情人- 发表于 2011-5-30 21:23:21 | 显示全部楼层
学习学习
回复

使用道具 举报

杨社长 发表于 2011-5-31 13:22:34 | 显示全部楼层
学习学习
回复

使用道具 举报

zhangjinlu 发表于 2011-6-2 11:54:54 | 显示全部楼层
回复

使用道具 举报

hdl2097 发表于 2011-7-27 11:37:36 | 显示全部楼层
好麻烦,感觉。。。还要用户登录后才能触发。。? 是这样的吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|Discuz! 官方站 ( 皖ICP备16010102号 )star

GMT+8, 2024-3-29 20:52 , Processed in 1.270877 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表