定义文件:\source\class\extend\extend_thread_allowat.php
相关类定义如下:
class extend_thread_allowat extends extend_thread_base {
public $atlist;
public $allowat;
public function before_newthread($parameters) {
if($this->group['allowat']) {
$this->atlist = $atlist_tmp = array();
preg_match_all("/@([^\r\n]*?)\s/i", $parameters['message'].' ', $atlist_tmp);
$atlist_tmp = array_slice(array_unique($atlist_tmp[1]), 0, $this->group['allowat']);
if(!empty($atlist_tmp)) {
if(!$this->setting['at_anyone']) {
foreach(C::t('home_follow')->fetch_all_by_uid_fusername($this->member['uid'], $atlist_tmp) as $row) {
$this->atlist[$row['followuid']] = $row['fusername'];
}
if(count($this->atlist) < $this->group['allowat']) {
$query = C::t('home_friend')->fetch_all_by_uid_username($this->member['uid'], $atlist_tmp);
foreach($query as $row) {
$this->atlist[$row['fuid']] = $row['fusername'];
}
}
} else {
foreach(C::t('common_member')->fetch_all_by_username($atlist_tmp) as $row) {
$this->atlist[$row['uid']] = $row['username'];
}
}
}
if($this->atlist) {
foreach($this->atlist as $atuid => $atusername) {
$atsearch[] = "/@".str_replace('/', '\/', preg_quote($atusername))." /i";
$atreplace[] = "[url=home.php?mod=space&uid=$atuid]@{$atusername}[/url] ";
}
$this->param['message'] = preg_replace($atsearch, $atreplace, $parameters['message'].' ', 1);
$this->param['message'] = substr($this->param['message'], 0, strlen($this->param['message']) - 1);
}
}
}
public function after_newthread() {
if($this->group['allowat'] && $this->atlist) {
foreach($this->atlist as $atuid => $atusername) {
notification_add($atuid, 'at', 'at_message', array('from_id' => $this->tid, 'from_idtype' => 'at', 'buyerid' => $this->member['uid'], 'buyer' => $this->member['username'], 'tid' => $this->tid, 'subject' => $this->param['subject'], 'pid' => $this->pid, 'message' => messagecutstr($this->param['message'], 150)));
}
set_atlist_cookie(array_keys($this->atlist));
}
}
public function before_newreply($parameters) {
if($this->group['allowat']) {
$atlist_tmp = $ateduids = array();
preg_match_all("/@([^\r\n]*?)\s/i", $parameters['message'].' ', $atlist_tmp);
$atlist_tmp = array_slice(array_unique($atlist_tmp[1]), 0, $this->group['allowat']);
$atnum = $maxselect = 0;
foreach(C::t('home_notification')->fetch_all_by_authorid_fromid($this->member['uid'], $this->thread['tid'], 'at') as $row) {
$atnum ++;
$ateduids[$row[uid]] = $row['uid'];
}
$maxselect = $this->group['allowat'] - $atnum;
if($maxselect > 0 && !empty($atlist_tmp)) {
$at_anyone = $this->setting['at_anyone'];
if(empty($at_anyone)) {
foreach(C::t('home_follow')->fetch_all_by_uid_fusername($this->member['uid'], $atlist_tmp) as $row) {
if(!in_array($row['followuid'], $ateduids)) {
$this->atlist[$row[followuid]] = $row['fusername'];
}
if(count($this->atlist) == $maxselect) {
break;
}
}
if(count($this->atlist) < $maxselect) {
$query = C::t('home_friend')->fetch_all_by_uid_username($this->member['uid'], $atlist_tmp);
foreach($query as $row) {
if(!in_array($row['followuid'], $ateduids)) {
$this->atlist[$row[fuid]] = $row['fusername'];
}
}
}
} else {
foreach(C::t('common_member')->fetch_all_by_username($atlist_tmp) as $row) {
if(!in_array($row['uid'], $ateduids)) {
$this->atlist[$row[uid]] = $row['username'];
}
if(count($this->atlist) == $maxselect) {
break;
}
}
}
}
if($this->atlist) {
foreach($this->atlist as $atuid => $atusername) {
$atsearch[] = "/@$atusername /i";
$atreplace[] = "[url=home.php?mod=space&uid=$atuid]@{$atusername}[/url] ";
}
$this->param['message'] = preg_replace($atsearch, $atreplace, $parameters['message'].' ', 1);
$this->param['message'] = substr($this->param['message'], 0, strlen($this->param['message']) - 1);
}
}
}
public function after_newreply() {
if($this->group['allowat'] && $this->atlist) {
foreach($this->atlist as $atuid => $atusername) {
notification_add($atuid, 'at', 'at_message', array('from_id' => $this->thread['tid'], 'from_idtype' => 'at', 'buyerid' => $this->member['uid'], 'buyer' => $this->member['username'], 'tid' => $this->thread['tid'], 'subject' => $this->thread['subject'], 'pid' => $this->pid, 'message' => messagecutstr($this->param['message'], 150)));
}
set_atlist_cookie(array_keys($this->atlist));
}
}
public function before_editpost($parameters) {
if($this->group['allowat']) {
$this->atlist = $atlist_tmp = $ateduids = array();
$atnum = $maxselect = 0;
foreach(C::t('home_notification')->fetch_all_by_authorid_fromid($this->member['uid'], $this->thread['tid'], 'at') as $row) {
$atnum ++;
$ateduids[$row[uid]] = $row['uid'];
}
$maxselect = $this->group['allowat'] - $atnum;
preg_match_all("/@([^\r\n]*?)\s/i", $parameters['message'].' ', $atlist_tmp);
$atlist_tmp = array_slice(array_unique($atlist_tmp[1]), 0, $this->group['allowat']);
if($maxselect > 0 && !empty($atlist_tmp)) {
if(empty($this->setting['at_anyone'])) {
foreach(C::t('home_follow')->fetch_all_by_uid_fusername($this->member['uid'], $atlist_tmp) as $row) {
if(!in_array($row['followuid'], $ateduids)) {
$this->atlist[$row[followuid]] = $row['fusername'];
}
if(count($this->atlist) == $maxselect) {
break;
}
}
if(count($this->atlist) < $maxselect) {
$query = C::t('home_friend')->fetch_all_by_uid_username($this->member['uid'], $atlist_tmp);
foreach($query as $row) {
if(!in_array($row['followuid'], $ateduids)) {
$this->atlist[$row[fuid]] = $row['fusername'];
}
}
}
} else {
foreach(C::t('common_member')->fetch_all_by_username($atlist_tmp) as $row) {
if(!in_array($row['uid'], $ateduids)) {
$this->atlist[$row[uid]] = $row['username'];
}
if(count($this->atlist) == $maxselect) {
break;
}
}
}
if($this->atlist) {
foreach($this->atlist as $atuid => $atusername) {
$atsearch[] = "/@$atusername /i";
$atreplace[] = "[url=home.php?mod=space&uid=$atuid]@{$atusername}[/url] ";
}
$parameters['message'] = preg_replace($atsearch, $atreplace, $parameters['message'].' ', 1);
$parameters['message'] = substr($parameters['message'], 0, strlen($parameters['message']) - 1);
}
}
}
}
public function after_editpost() {
if($this->group['allowat'] && $this->atlist) {
foreach($this->atlist as $atuid => $atusername) {
notification_add($atuid, 'at', 'at_message', array('from_id' => $this->thread['tid'], 'from_idtype' => 'at', 'buyerid' => $this->member['uid'], 'buyer' => $this->member['username'], 'tid' => $this->thread['tid'], 'subject' => $this->thread['subject'], 'pid' => $this->post['pid'], 'message' => messagecutstr($this->param['message'], 150)));
}
set_atlist_cookie(array_keys($this->atlist));
}
}
}
由以上代码定义我们看到程序处理@功能分别有三处:发帖、回帖、编辑;
首先以正则表达式方式从内容文本中抽取@会员信息存入atlist,并将内容文本中的@会员解析出会员的空间地址([url]标签),然后在所有的after_xxx方法中向被@的会员发送通知,这里需要说明一点是:before_xxx方法是内容入库前的操作,after_xxx方法是内容入库后的操作!