PageRenderTime 30ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/b2b/core/model/system/mdl.messenger.php

http://phpfor.googlecode.com/
PHP | 308 lines | 229 code | 42 blank | 37 comment | 30 complexity | a5066bbef140fdad71a30d7b10311920 MD5 | raw file
  1. <?php
  2. /**
  3. * kernel
  4. *
  5. * @package
  6. * @version $Id: mdl.messenger.php 1429 2008-04-08 05:22:50Z flaboy $
  7. * @copyright 2003-2007 ShopEx
  8. * @author ShopEx
  9. * @license Commercial
  10. */
  11. //adapted into plugin style by Alex 2008-3-14
  12. require_once('plugin.php');
  13. class mdl_messenger extends plugin{
  14. var $plugin_type = 'dir';
  15. var $plugin_name = 'messenger';
  16. var $prefix = 'messenger.';
  17. function outbox($sender){
  18. return $this->db->select('select out_id,tmpl_name,creattime,sendcount,content,subject from sdb_sendbox where sender=\''.$sender.'\'');
  19. }
  20. function &_load($sender){
  21. if(!$this->_sender[$sender]){
  22. $obj = $this->load($sender);
  23. $this->_sender[$sender] = &$obj;
  24. if(method_exists($obj,'getOptions')||method_exists($obj,'getoptions'))
  25. $obj->config = $this->getOptions($sender,true);
  26. if(method_exists($obj,'outgoingConfig')||method_exists($obj,'outgoingconfig'))
  27. $obj->outgoingOptions = $this->outgoingConfig($sender,true);
  28. }else{
  29. $obj = &$this->_sender[$sender];
  30. }
  31. return $obj;
  32. }
  33. function _ready(&$obj){
  34. if(!$obj->_isReady){
  35. if(method_exists($obj,'ready')) $obj->ready($obj->config);
  36. if(method_exists($obj,'finish')){
  37. if(!$this->_finishCall){
  38. register_shutdown_function(array(&$this,'_finish'));
  39. $this->_finishCall=array();
  40. }
  41. $this->_finishCall[] = &$obj;
  42. }
  43. $obj->_isReady = true;
  44. }
  45. }
  46. function _send($sendMethod,$tmpl_name,$target,$data,$type,$title=null){
  47. $sender = &$this->_load($sendMethod);
  48. $this->_ready($sender);
  49. if(!$this->_systmpl){
  50. $this->_systmpl = &$this->system->loadModel('content/systmpl');
  51. }
  52. $content = $this->_systmpl->fetch($tmpl_name,$data);
  53. $ret = $sender->hasTitle?$sender->send($target,
  54. $title?$title:$this->loadTitle($type,$sendMethod,'',$data)
  55. ,$content,$sender->config):$sender->send($target,$content,$sender->config);
  56. return ($ret || !is_bool($ret));
  57. }
  58. function _finish(){
  59. foreach($this->_finishCall as $obj){
  60. $obj->finish($obj->config);
  61. }
  62. }
  63. function _target($sender,$contectInfo,$member_id){
  64. $obj = &$this->_load($sender);
  65. if(($dataname = $obj->dataname) && $contectInfo[$dataname]){
  66. return $contectInfo[$dataname];
  67. }elseif(($dataname = $obj->dataname) && $contectInfo['delivery']['ship_'.$dataname]){
  68. return $contectInfo['delivery']['ship_'.$dataname];
  69. }else{
  70. $row = $this->db->selectrow('select email,member_id,uname,custom,mobile from sdb_members where member_id='.intval($member_id));
  71. if($dataname){
  72. return $row[$dataname];
  73. }elseif($custom = unserialize($row['custom'])){
  74. return $custom[$sender];
  75. }else{
  76. return false;
  77. }
  78. }
  79. }
  80. /**
  81. * actionSend
  82. *
  83. * @param mixed $type ??
  84. * @param mixed $contectInfo ????
  85. * @param mixed $member_id ??id
  86. * @param mixed $data ??
  87. * @access public
  88. * @return void
  89. */
  90. function actionSend($type,$data,$member_id=null){
  91. $actions = $this->actions();
  92. $senders = $this->getSenders($type);
  93. $level = $actions[$type]['level'];
  94. $desc = $actions[$type]['label'];
  95. foreach($senders as $sender){
  96. $tmpl_name = 'messenger:'.$sender.'/'.$type;
  97. $contractInfo = $data;
  98. if($sender && ($target = $this->_target($sender,$contractInfo,$member_id))){
  99. if($level < 9){ //??
  100. $this->addQueue($sender,$target,$desc,$data,$tmpl_name,$level,$type);
  101. }else{ //????
  102. $this->_send($sender,$tmpl_name,$target,$data,$type);
  103. }
  104. }
  105. }
  106. }
  107. function _useQueue($sender){
  108. $sender = &$this->_load($sender);
  109. $s = !$sender->withoutQueue;
  110. return $s;
  111. }
  112. function getSenders($act){
  113. $ret = $this->system->getConf('messenger.actions.'.$act);
  114. return explode(',',$ret);
  115. }
  116. function saveActions($actions){
  117. foreach($this->actions() as $act=>$info){
  118. if(!$actions[$act]){
  119. $actions[$act] = array();
  120. }
  121. }
  122. foreach($actions as $act=>$call){
  123. $this->system->setConf('messenger.actions.'.$act,implode(',',array_keys($call)));
  124. }
  125. return true;
  126. }
  127. /**
  128. * actions
  129. * ??????????????????????????
  130. *
  131. * ???
  132. * ??-?? => array(label=>?? , level=>????)
  133. *
  134. * ??????????????????send()????
  135. *
  136. * @access public
  137. * @return void
  138. */
  139. function actions(){
  140. $actions = array(
  141. 'lostPw'=>array('label'=>'??????','level'=>9,'varmap'=>'???&nbsp;<{$uname}>&nbsp;&nbsp;&nbsp;&nbsp;??&nbsp;<{$passwd}>&nbsp;&nbsp;&nbsp;&nbsp;??&nbsp;<{$name}>'),
  142. 'order-delivery'=>array('label'=>'????????????','level'=>9,'varmap'=>'???&nbsp;<{$order_id}>&nbsp;&nbsp;&nbsp;&nbsp;????&nbsp;<{$delivery.money}>&nbsp;&nbsp;&nbsp;&nbsp;????&nbsp;<{$delivery.delivery}><br>????&nbsp;<{$ship_corp}>&nbsp;&nbsp;&nbsp;&nbsp;????&nbsp;<{$ship_billno}>&nbsp;&nbsp;&nbsp;&nbsp;?????&nbsp;<{$delivery.ship_name}><br>?????&nbsp;<{$delivery.ship_addr}>&nbsp;&nbsp;&nbsp;&nbsp;?????&nbsp;<{$delivery.ship_zip}>&nbsp;&nbsp;&nbsp;&nbsp;?????&nbsp;<{$delivery.ship_tel}><br>?????&nbsp;<{$delivery.ship_mobile}>&nbsp;&nbsp;&nbsp;&nbsp;???Email&nbsp;<{$delivery.ship_email}>&nbsp;&nbsp;&nbsp;&nbsp;???&nbsp;<{$delivery.op_name}><br>??&nbsp;<{$delivery.memo}>'),
  143. 'order-deliverytoend'=>array('label'=>'????????????','level'=>9,'varmap'=>'???&nbsp;<{$order_id}>&nbsp;&nbsp;&nbsp;&nbsp;????&nbsp;<{$delivery.money}>&nbsp;&nbsp;&nbsp;&nbsp;????&nbsp;<{$delivery.delivery}><br>????&nbsp;<{$ship_corp}>&nbsp;&nbsp;&nbsp;&nbsp;????&nbsp;<{$ship_billno}>&nbsp;&nbsp;&nbsp;&nbsp;?????&nbsp;<{$delivery.ship_name}><br>?????&nbsp;<{$delivery.ship_addr}>&nbsp;&nbsp;&nbsp;&nbsp;?????&nbsp;<{$delivery.ship_zip}>&nbsp;&nbsp;&nbsp;&nbsp;?????&nbsp;<{$delivery.ship_tel}><br>?????&nbsp;<{$delivery.ship_mobile}>&nbsp;&nbsp;&nbsp;&nbsp;???Email&nbsp;<{$delivery.ship_email}>&nbsp;&nbsp;&nbsp;&nbsp;???&nbsp;<{$delivery.op_name}><br>??&nbsp;<{$delivery.memo}>'),
  144. 'order-create'=>array('label'=>'?????','level'=>9,'varmap'=>'???&nbsp;<{$order_id}>&nbsp;&nbsp;&nbsp;&nbsp;??&nbsp;<{$total_amount}>&nbsp;&nbsp;&nbsp;&nbsp;????&nbsp;<{$shipping}><br>?????&nbsp;<{$ship_mobile}>&nbsp;&nbsp;&nbsp;&nbsp;?????&nbsp;<{$ship_tel}>&nbsp;&nbsp;&nbsp;&nbsp;?????&nbsp;<{$ship_addr}><Br>???Email&nbsp;<{$ship_email}>&nbsp;&nbsp;&nbsp;&nbsp;?????&nbsp;<{$ship_zip}>&nbsp;&nbsp;&nbsp;&nbsp;?????&nbsp;<{$ship_name}>'),
  145. 'order-pay'=>array('label'=>'?????','level'=>9,'varmap'=>'???&nbsp;<{$order_id}>&nbsp;&nbsp;&nbsp;&nbsp;????&nbsp;<{$pay_time}>&nbsp;&nbsp;&nbsp;&nbsp;????&nbsp;<{$money}>'),
  146. 'order-reship'=>array('label'=>'?????','level'=>9,'varmap'=>'???&nbsp;<{$order_id}>'),
  147. 'order-refund'=>array('label'=>'?????','level'=>9,'varmap'=>'???&nbsp;<{$order_id}>'),
  148. 'goods-notify'=>array('label'=>'??????','level'=>6,'varmap'=>'????&nbsp;<{$goods_name}>&nbsp;&nbsp;&nbsp;&nbsp;????&nbsp;<{$username}>'),
  149. /* 'goods-replay'=>array('label'=>'??????','level'=>9), todo */
  150. 'account-register'=>array('label'=>'?????','level'=>9,'varmap'=>'???&nbsp;<{$uname}>&nbsp;&nbsp;&nbsp;&nbsp;email&nbsp;<{$email}>&nbsp;&nbsp;&nbsp;&nbsp;??&nbsp;<{$passwd}>'),
  151. 'account-chgpass'=>array('label'=>'???????','level'=>9,'varmap'=>'??&nbsp;<{$passwd}>&nbsp;&nbsp;&nbsp;&nbsp;???&nbsp;<{$uname}>&nbsp;&nbsp;&nbsp;&nbsp;???<&nbsp;{$uname}>&nbsp;&nbsp;&nbsp;&nbsp;email&nbsp;<{$email}>'),
  152. 'account-chgpaypass'=>array('label'=>'????????????','level'=>9,'varmap'=>'????&nbsp;<{$passwd}>&nbsp;&nbsp;&nbsp;&nbsp;???&nbsp;<{$uname}>&nbsp;&nbsp;&nbsp;&nbsp;???<&nbsp;{$uname}>&nbsp;&nbsp;&nbsp;&nbsp;email&nbsp;<{$email}>'),
  153. /* 'comment-replay'=>array('label'=>'?????','level'=>9,'varmap'=>''), todo */
  154. /* 'indexorder-pay'=>array('label'=>'??????','level'=>9), */
  155. /* 'comment-new'=>array('label'=>'????????','level'=>9), */
  156. 'order-cancel'=>array('label'=>'????','level'=>9,'varmap'=>'???&nbsp;<{$order_id}>'),
  157. 'member_confirm'=>array('label'=>'??????','level'=>9,'varmap'=>'????&nbsp;<{$siteurl}>&nbsp;&nbsp;&nbsp;&nbsp;???&nbsp;<{$uname}>&nbsp;&nbsp;&nbsp;&nbsp;???????/???&nbsp;<{$result}>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;email&nbsp;<{$email}>'),
  158. 'advance-recharge'=>array('label'=>'??????',level=>9,
  159. 'varmap'=>'????&nbsp;<{$shop_name}>&nbsp;???????&nbsp;<{$money}>&nbsp;?????&nbsp;<{$advance}>&nbsp;???????&nbsp;<{$advance_date}>&nbsp;????&nbsp;<{$url}>&nbsp;'),
  160. 'advance-consumption'=>array('label'=>'??????',level=>9,
  161. 'varmap'=>'????&nbsp;<{$shop_name}>&nbsp;???????&nbsp;<{$money}>&nbsp;?????&nbsp;<{$advance}>&nbsp;???????&nbsp;<{$advance_date}>&nbsp;????&nbsp;<{$url}>&nbsp;'),
  162. 'matrix-create'=>array('label'=>'?????????','level'=>9,'varmap'=>'????<{$name}>&nbsp;&nbsp;&nbsp;&nbsp;????<{$shopname}>&nbsp;&nbsp;&nbsp;&nbsp;????<{$url}>&nbsp;&nbsp;&nbsp;&nbsp;???<{$uname}>&nbsp;&nbsp;&nbsp;&nbsp;??<{$passwd}>&nbsp;&nbsp;&nbsp;&nbsp;Email<{$email}>'),
  163. );
  164. // ?????????? 2010-07-26 18:44 wubin
  165. if(!$this->system->getConf('system.advance_version')) unset($actions['member_confirm']);
  166. return $actions;
  167. }
  168. function addSendBox($data){
  169. $data['creattime'] = time();
  170. $rs = $this->db->exec('select * from sdb_sendbox where 0=1');
  171. $sql = $this->db->getInsertSQL($rs,$data);
  172. return $this->db->exec($sql);
  173. }
  174. function send_obj_act($obj){
  175. return $msgbody;
  176. }
  177. function loadTmpl($action,$msg,$lang=''){
  178. $systmpl = &$this->system->loadModel('content/systmpl');
  179. return $systmpl->get('messenger:'.$msg.'/'.$action);
  180. }
  181. function loadTitle($action,$msg,$lang='',$data=""){
  182. $title = $this->system->getConf('messenger.title.'.$action.'.'.$msg);
  183. if($data!=""){
  184. preg_match_all('/<\{\$(\w+)\}>/i', $title, $result);
  185. foreach($result[1] as $k => $v){
  186. if(in_array($v,array_keys($data))){
  187. $title = str_replace($result[0][$k],$data[$v],$title);
  188. }
  189. }
  190. }
  191. return $title;
  192. }
  193. function addQueue($sender,$target,$title,$data,$tmpl_name,$level=5,$event_name=''){
  194. if(!$this->_useQueue($sender)){
  195. $this->_send($sender,$tmpl_name,$target,$data,$event_name,$title);
  196. return true;
  197. }
  198. $sqlData = array(
  199. 'tmpl_name'=>$tmpl_name,
  200. 'level'=>$level,
  201. 'event_name'=>$event_name,
  202. 'title'=>$title,
  203. 'target'=>$target,
  204. 'sender'=>$sender,
  205. 'data'=>$data
  206. );
  207. if($count = $this->system->getConf('messenger.stat.'.$senders.'.counts')){
  208. $sqlData['sender_order'] = $this->system->getConf('messenger.stat.'.$senders.'.time')/$count;
  209. }
  210. $rs = $this->db->exec('select * from sdb_msgqueue where 0=1');
  211. $sql = $this->db->getInsertSQL($rs,$sqlData);
  212. $this->db->exec($sql);
  213. }
  214. function runQueue(){
  215. $row = $this->db->selectrow("select modified from sdb_cachemgr where cname='MSG_MUTEX'",true,true);
  216. if( time() - $row['modified'] > 900){
  217. $this->db->exec("replace into sdb_cachemgr (cname,modified) values ('MSG_MUTEX',".time().")");
  218. register_shutdown_function(array(&$this,'removeMutex'));
  219. $systmpl = &$this->system->loadModel('content/systmpl');
  220. foreach($this->db->selectlimit('select queue_id,data,tmpl_name,target,title,event_name,sender from sdb_msgqueue order by level,sender_order',100) as $queue){
  221. $queue['data'] = unserialize($queue['data']);
  222. if($this->_send($queue['sender'],$queue['tmpl_name'],$queue['target'],$queue['data'],$queue['event_name'],$queue['title'])){
  223. $this->db->exec('delete from sdb_msgqueue where queue_id='.$queue['queue_id']);
  224. }
  225. }
  226. }
  227. }
  228. function removeMutex(){
  229. return $this->db->exec("delete from sdb_cachemgr where cname='MSG_MUTEX'");
  230. }
  231. function saveContent($action,$msg,$data){
  232. $systmpl = &$this->system->loadModel('content/systmpl');
  233. $info = $this->getParams($msg);
  234. if($info['hasTitle']) $this->system->setConf('messenger.title.'.$action.'.'.$msg,$data['title']);
  235. return $systmpl->set('messenger:'.$msg.'/'.$action,$data['content']);
  236. }
  237. function getQueue($sender){
  238. return $this->db->select_f('SELECT queue_id,target,level,event_name,title FROM sdb_msgqueue WHERE sender = \''.$sender.'\'', $nPage, 100);
  239. }
  240. function parseAddr($in){
  241. $a = explode("\n",$in);
  242. foreach($a as $k=>$v){
  243. if($v){
  244. if(($start = strpos($v,' <')) && ($end = strrpos($v,'>'))){
  245. $a[$k] = substr($v,$start+2,$end-$start-2);
  246. }
  247. }else{
  248. unset($a[$k]);
  249. }
  250. }
  251. return $a;
  252. }
  253. function getNoteConfig(){
  254. $rs=$this->db->selectrow("select s_data from sdb_settings where s_name='messenger'");
  255. $rs=unserialize($rs['s_data']);
  256. if($rs['actions.order-delivery']){
  257. $r['fxs']='checked';
  258. }
  259. if($rs['actions.order-deliverytoend']){
  260. $r['zdkh']='checked';
  261. }
  262. unset($rs);
  263. return $r;
  264. }
  265. }
  266. ?>