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

/miles/GlobalMBA/mba/sns_00/upload/home/uc_client/model/note.php

#
PHP | 239 lines | 160 code | 30 blank | 49 comment | 33 complexity | 768f37d0a0110c341f8243e9c11033e4 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /*
  3. [UCenter] (C)2001-2009 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: note.php 916 2009-01-19 05:56:07Z monkey $
  6. */
  7. !defined('IN_UC') && exit('Access Denied');
  8. define('UC_NOTE_REPEAT', 5); //note 通知重复次数
  9. define('UC_NOTE_TIMEOUT', 15); //note 通知超时时间(秒)
  10. define('UC_NOTE_GC', 10000); //note 过期通知的回收概率,该值越大,概率越低
  11. define('API_RETURN_FAILED', '-1');
  12. class notemodel {
  13. var $db;
  14. var $base;
  15. var $apps;
  16. var $operations = array();
  17. var $notetype = 'HTTP';//note HTTP|INCLUDE
  18. function __construct(&$base) {
  19. $this->notemodel($base);
  20. }
  21. function notemodel(&$base) {
  22. $this->base = $base;
  23. $this->db = $base->db;
  24. $this->apps = $this->base->cache('apps');
  25. /** note
  26. * 1. 操作的名称,如:删除用户,测试连通,删除好友,取TAG数据,更新客户端缓存
  27. * 2. 调用的应用的接口参数,拼接规则为 APP_URL/api/uc.php?action=test&ids=1,2,3
  28. * 3. 回调的模块名称
  29. * 4. 回调的模块方法($appid, $content)
  30. */
  31. $this->operations = array(
  32. 'test'=>array('', 'action=test'),
  33. 'deleteuser'=>array('', 'action=deleteuser'),
  34. 'renameuser'=>array('', 'action=renameuser'),
  35. 'deletefriend'=>array('', 'action=deletefriend'),
  36. 'gettag'=>array('', 'action=gettag', 'tag', 'updatedata'),
  37. 'getcreditsettings'=>array('', 'action=getcreditsettings'),
  38. 'getcredit'=>array('', 'action=getcredit'),
  39. 'updatecreditsettings'=>array('', 'action=updatecreditsettings'),
  40. 'updateclient'=>array('', 'action=updateclient'),
  41. 'updatepw'=>array('', 'action=updatepw'),
  42. 'updatebadwords'=>array('', 'action=updatebadwords'),
  43. 'updatehosts'=>array('', 'action=updatehosts'),
  44. 'updateapps'=>array('', 'action=updateapps'),
  45. 'updatecredit'=>array('', 'action=updatecredit'),
  46. );
  47. }
  48. /**
  49. * 统计通知的总条数
  50. *
  51. * @return int
  52. */
  53. function get_total_num($all = TRUE) {
  54. }
  55. /**
  56. * Enter 得到通知列表
  57. *
  58. * @param int $page
  59. * @param int$ppp
  60. * @param int $totalnum
  61. * @return array 结果集
  62. */
  63. function get_list($page, $ppp, $totalnum, $all = TRUE) {
  64. }
  65. /**
  66. * 删除通知
  67. *
  68. * @param string/array $ids
  69. * @return 受影响的行数
  70. */
  71. function delete_note($ids) {
  72. }
  73. /**
  74. * 添加通知列表
  75. *
  76. * @param string 操作
  77. * @param string getdata
  78. * @param string postdata
  79. * @param array appids 指定通知的 APPID
  80. * @param int pri 优先级,值越大表示越高
  81. * @return int 插入的ID
  82. */
  83. function add($operation, $getdata='', $postdata='', $appids=array(), $pri = 0) {
  84. $extra = $varextra = '';
  85. $appadd = $varadd = array();
  86. foreach((array)$this->apps as $appid => $app) {
  87. $appid = $app['appid'];
  88. if($appid == intval($appid)) {
  89. if($appids && !in_array($appid, $appids)) {
  90. $appadd[] = 'app'.$appid."='1'";
  91. } else {
  92. $varadd[] = "('noteexists{$appid}', '1')";
  93. }
  94. }
  95. }
  96. if($appadd) {
  97. $extra = implode(',', $appadd);
  98. $extra = $extra ? ', '.$extra : '';
  99. }
  100. if($varadd) {
  101. $varextra = implode(', ', $varadd);
  102. $varextra = $varextra ? ', '.$varextra : '';
  103. }
  104. $getdata = addslashes($getdata);
  105. $postdata = addslashes($postdata);
  106. $this->db->query("INSERT INTO ".UC_DBTABLEPRE."notelist SET getdata='$getdata', operation='$operation', pri='$pri', postdata='$postdata'$extra");
  107. $insert_id = $this->db->insert_id();
  108. $insert_id && $this->db->query("REPLACE INTO ".UC_DBTABLEPRE."vars (name, value) VALUES ('noteexists', '1')$varextra");
  109. return $insert_id;
  110. }
  111. function send() {
  112. register_shutdown_function(array($this, '_send'));
  113. }
  114. function _send() {
  115. //note 判断是否有通知
  116. //note 如果内存表记录不存在,那么可能 mysql 被重启,需要再次判断通知是否存在
  117. //note 查看是否有通知
  118. $note = $this->_get_note();
  119. if(empty($note)) {
  120. //note 标示为不需要通知
  121. $this->db->query("REPLACE INTO ".UC_DBTABLEPRE."vars SET name='noteexists".UC_APPID."', value='0'");
  122. return NULL;
  123. }
  124. //note mysql只发送自己的通知
  125. $this->sendone(UC_APPID, 0, $note);
  126. //note 垃圾清理
  127. $this->_gc();
  128. }
  129. function sendone($appid, $noteid = 0, $note = '') {
  130. require_once UC_ROOT.'./lib/xml.class.php';
  131. $return = FALSE;
  132. $app = $this->apps[$appid];
  133. if($noteid) {
  134. $note = $this->_get_note_by_id($noteid);
  135. }
  136. $this->base->load('misc');
  137. $apifilename = isset($app['apifilename']) && $app['apifilename'] ? $app['apifilename'] : 'uc.php';
  138. if($app['extra']['apppath'] && @include $app['extra']['apppath'].'./api/'.$apifilename) {
  139. $uc_note = new uc_note();
  140. $method = $note['operation'];
  141. if(is_string($method) && !empty($method)) {
  142. parse_str($note['getdata'], $note['getdata']);
  143. if(get_magic_quotes_gpc()) {
  144. $note['getdata'] = $this->base->dstripslashes($note['getdata']);
  145. }
  146. $note['postdata'] = xml_unserialize($note['postdata']);
  147. $response = $uc_note->$method($note['getdata'], $note['postdata']);
  148. }
  149. unset($uc_note);
  150. } else {
  151. $url = $this->get_url_code($note['operation'], $note['getdata'], $appid);
  152. $note['postdata'] = str_replace(array("\n", "\r"), '', $note['postdata']);
  153. $response = trim($_ENV['misc']->dfopen2($url, 0, $note['postdata'], '', 1, $app['ip'], UC_NOTE_TIMEOUT, TRUE));
  154. }
  155. $returnsucceed = $response != '' && ($response == 1 || is_array(xml_unserialize($response)));//note 当确实返回为1的时候才认为是通知成功
  156. $closedsqladd = $this->_close_note($note, $this->apps, $returnsucceed, $appid) ? ",closed='1'" : '';//
  157. if($returnsucceed) {
  158. if($this->operations[$note['operation']][2]) {
  159. $this->base->load($this->operations[$note['operation']][2]);
  160. $func = $this->operations[$note['operation']][3];
  161. $_ENV[$this->operations[$note['operation']][2]]->$func($appid, $response);
  162. }
  163. $this->db->query("UPDATE ".UC_DBTABLEPRE."notelist SET app$appid='1', totalnum=totalnum+1, succeednum=succeednum+1, dateline='{$this->base->time}' $closedsqladd WHERE noteid='$note[noteid]'", 'SILENT');
  164. $return = TRUE;
  165. } else {
  166. $this->db->query("UPDATE ".UC_DBTABLEPRE."notelist SET app$appid = app$appid-'1', totalnum=totalnum+1, dateline='{$this->base->time}' $closedsqladd WHERE noteid='$note[noteid]'", 'SILENT');
  167. $return = FALSE;
  168. }
  169. return $return;
  170. }
  171. function _get_note() {
  172. $app_field = 'app'.UC_APPID;
  173. $data = $this->db->fetch_first("SELECT * FROM ".UC_DBTABLEPRE."notelist WHERE closed='0' AND $app_field<'1' AND $app_field>'-".UC_NOTE_REPEAT."' LIMIT 1");
  174. return $data;
  175. }
  176. function _gc() {
  177. rand(0, UC_NOTE_GC) == 0 && $this->db->query("DELETE FROM ".UC_DBTABLEPRE."notelist WHERE closed='1'");
  178. }
  179. //note 判断是否需要关闭通知
  180. function _close_note($note, $apps, $returnsucceed, $appid) {
  181. $note['app'.$appid] = $returnsucceed ? 1 : $note['app'.$appid] - 1;
  182. $appcount = count($apps);
  183. foreach($apps as $key => $app) {
  184. $appstatus = $note['app'.$app['appid']];
  185. if(!$app['recvnote'] || $appstatus == 1 || $appstatus <= -UC_NOTE_REPEAT) {
  186. $appcount--;
  187. }
  188. }
  189. if($appcount < 1) {
  190. return TRUE;
  191. //$closedsqladd = ",closed='1'";
  192. }
  193. }
  194. function _get_note_by_id($noteid) {
  195. $data = $this->db->fetch_first("SELECT * FROM ".UC_DBTABLEPRE."notelist WHERE noteid='$noteid'");
  196. return $data;
  197. }
  198. function get_url_code($operation, $getdata, $appid) {
  199. $app = $this->apps[$appid];
  200. $authkey = UC_KEY;
  201. $url = $app['url'];
  202. $apifilename = isset($app['apifilename']) && $app['apifilename'] ? $app['apifilename'] : 'uc.php';
  203. $action = $this->operations[$operation][1];
  204. $code = urlencode($this->base->authcode("$action&".($getdata ? "$getdata&" : '')."time=".$this->base->time, 'ENCODE', $authkey));
  205. return $url."/api/$apifilename?code=$code";
  206. }
  207. }
  208. ?>