note.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 1122 2011-11-14 03:06:25Z monkey $
  6. */
  7. !defined('IN_UC') && exit('Access Denied');
  8. define('UC_NOTE_REPEAT', 2);
  9. define('UC_NOTE_TIMEOUT', 15);
  10. define('UC_NOTE_GC', 5);
  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';
  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. $this->operations = array(
  26. 'test'=>array('', 'action=test'),
  27. 'deleteuser'=>array('', 'action=deleteuser'),
  28. 'renameuser'=>array('', 'action=renameuser'),
  29. 'deletefriend'=>array('', 'action=deletefriend'),
  30. 'gettag'=>array('', 'action=gettag', 'tag', 'updatedata'),
  31. 'getcreditsettings'=>array('', 'action=getcreditsettings'),
  32. 'getcredit'=>array('', 'action=getcredit'),
  33. 'updatecreditsettings'=>array('', 'action=updatecreditsettings'),
  34. 'updateclient'=>array('', 'action=updateclient'),
  35. 'updatepw'=>array('', 'action=updatepw'),
  36. 'updatebadwords'=>array('', 'action=updatebadwords'),
  37. 'updatehosts'=>array('', 'action=updatehosts'),
  38. 'updateapps'=>array('', 'action=updateapps'),
  39. 'updatecredit'=>array('', 'action=updatecredit'),
  40. );
  41. }
  42. function get_total_num($all = TRUE) {
  43. $closedadd = $all ? '' : ' WHERE closed=\'0\'';
  44. $data = $this->db->result_first("SELECT COUNT(*) FROM ".UC_DBTABLEPRE."notelist $closedadd");
  45. return $data;
  46. }
  47. function get_list($page, $ppp, $totalnum, $all = TRUE) {
  48. $start = $this->base->page_get_start($page, $ppp, $totalnum);
  49. $closedadd = $all ? '' : ' WHERE closed=\'0\'';
  50. $data = $this->db->fetch_all("SELECT * FROM ".UC_DBTABLEPRE."notelist $closedadd ORDER BY dateline DESC LIMIT $start, $ppp");
  51. foreach((array)$data as $k => $v) {
  52. $data[$k]['postdata2'] = addslashes(str_replace('"', '', $data[$k]['postdata']));
  53. $data[$k]['getdata2'] = addslashes(str_replace('"', '', $v['getdata']));
  54. $data[$k]['dateline'] = $v['dateline'] ? $this->base->date($data[$k]['dateline']) : '';
  55. }
  56. return $data;
  57. }
  58. function delete_note($ids) {
  59. $ids = $this->base->implode($ids);
  60. $this->db->query("DELETE FROM ".UC_DBTABLEPRE."notelist WHERE noteid IN ($ids)");
  61. return $this->db->affected_rows();
  62. }
  63. function add($operation, $getdata='', $postdata='', $appids=array(), $pri = 0) {
  64. $extra = $varextra = '';
  65. foreach((array)$this->apps as $appid => $app) {
  66. $appid = $app['appid'];
  67. if($appid == intval($appid)) {
  68. if($appids && !in_array($appid, $appids)) {
  69. $appadd[] = 'app'.$appid."='1'";
  70. } else {
  71. $varadd[] = "('noteexists{$appid}', '1')";
  72. }
  73. }
  74. }
  75. if($appadd) {
  76. $extra = implode(',', $appadd);
  77. $extra = $extra ? ', '.$extra : '';
  78. }
  79. if($varadd) {
  80. $varextra = implode(', ', $varadd);
  81. $varextra = $varextra ? ', '.$varextra : '';
  82. }
  83. $getdata = addslashes($getdata);
  84. $postdata = addslashes($postdata);
  85. $this->db->query("INSERT INTO ".UC_DBTABLEPRE."notelist SET getdata='$getdata', operation='$operation', pri='$pri', postdata='$postdata'$extra");
  86. $insert_id = $this->db->insert_id();
  87. $insert_id && $this->db->query("REPLACE INTO ".UC_DBTABLEPRE."vars (name, value) VALUES ('noteexists', '1')$varextra");
  88. return $insert_id;
  89. }
  90. function send() {
  91. register_shutdown_function(array($this, '_send'));
  92. }
  93. function _send() {
  94. $note = $this->_get_note();
  95. if(empty($note)) {
  96. $this->db->query("REPLACE INTO ".UC_DBTABLEPRE."vars SET name='noteexists', value='0'");
  97. return NULL;
  98. }
  99. $closenote = TRUE;
  100. foreach((array)$this->apps as $appid => $app) {
  101. $appnotes = $note['app'.$appid];
  102. if($app['recvnote'] && $appnotes != 1 && $appnotes > -UC_NOTE_REPEAT) {
  103. $this->sendone($appid, 0, $note);
  104. $closenote = FALSE;
  105. break;
  106. }
  107. }
  108. if($closenote) {
  109. $this->db->query("UPDATE ".UC_DBTABLEPRE."notelist SET closed='1' WHERE noteid='$note[noteid]'");
  110. }
  111. $this->_gc();
  112. }
  113. function sendone($appid, $noteid = 0, $note = '') {
  114. require_once UC_ROOT.'./lib/xml.class.php';
  115. $return = FALSE;
  116. $app = $this->apps[$appid];
  117. if($noteid) {
  118. $note = $this->_get_note_by_id($noteid);
  119. }
  120. $this->base->load('misc');
  121. $apifilename = isset($app['apifilename']) && $app['apifilename'] ? $app['apifilename'] : 'uc.php';
  122. if($app['extra']['apppath'] && @include_once $app['extra']['apppath'].'./api/'.$apifilename) {
  123. $uc_note = new uc_note();
  124. $method = $note['operation'];
  125. if(is_string($method) && !empty($method)) {
  126. parse_str($note['getdata'], $note['getdata']);
  127. if(get_magic_quotes_gpc()) {
  128. $note['getdata'] = $this->base->dstripslashes($note['getdata']);
  129. }
  130. $note['postdata'] = xml_unserialize($note['postdata']);
  131. $response = $uc_note->$method($note['getdata'], $note['postdata']);
  132. }
  133. unset($uc_note);
  134. } else {
  135. $url = $this->get_url_code($note['operation'], $note['getdata'], $appid);
  136. $note['postdata'] = str_replace(array("\n", "\r"), '', $note['postdata']);
  137. $response = trim($_ENV['misc']->dfopen2($url, 0, $note['postdata'], '', 1, $app['ip'], UC_NOTE_TIMEOUT, TRUE));
  138. }
  139. $returnsucceed = $response != '' && ($response == 1 || is_array(xml_unserialize($response)));
  140. $closedsqladd = $this->_close_note($note, $this->apps, $returnsucceed, $appid) ? ",closed='1'" : '';
  141. if($returnsucceed) {
  142. if($this->operations[$note['operation']][2]) {
  143. $this->base->load($this->operations[$note['operation']][2]);
  144. $func = $this->operations[$note['operation']][3];
  145. $_ENV[$this->operations[$note['operation']][2]]->$func($appid, $response);
  146. }
  147. $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');
  148. $return = TRUE;
  149. } else {
  150. $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');
  151. $return = FALSE;
  152. }
  153. return $return;
  154. }
  155. function _get_note() {
  156. $data = $this->db->fetch_first("SELECT * FROM ".UC_DBTABLEPRE."notelist WHERE closed='0' ORDER BY pri DESC, noteid ASC LIMIT 1");
  157. return $data;
  158. }
  159. function _gc() {
  160. rand(0, UC_NOTE_GC) == 0 && $this->db->query("DELETE FROM ".UC_DBTABLEPRE."notelist WHERE closed='1'");
  161. }
  162. function _close_note($note, $apps, $returnsucceed, $appid) {
  163. $note['app'.$appid] = $returnsucceed ? 1 : $note['app'.$appid] - 1;
  164. $appcount = count($apps);
  165. foreach($apps as $key => $app) {
  166. $appstatus = $note['app'.$app['appid']];
  167. if(!$app['recvnote'] || $appstatus == 1 || $appstatus <= -UC_NOTE_REPEAT) {
  168. $appcount--;
  169. }
  170. }
  171. if($appcount < 1) {
  172. return TRUE;
  173. }
  174. }
  175. function _get_note_by_id($noteid) {
  176. $data = $this->db->fetch_first("SELECT * FROM ".UC_DBTABLEPRE."notelist WHERE noteid='$noteid'");
  177. return $data;
  178. }
  179. function get_url_code($operation, $getdata, $appid) {
  180. $app = $this->apps[$appid];
  181. $authkey = $app['authkey'];
  182. $url = $app['url'];
  183. $apifilename = isset($app['apifilename']) && $app['apifilename'] ? $app['apifilename'] : 'uc.php';
  184. $action = $this->operations[$operation][1];
  185. $code = urlencode($this->base->authcode("$action&".($getdata ? "$getdata&" : '')."time=".$this->base->time, 'ENCODE', $authkey));
  186. return $url."/api/$apifilename?code=$code";
  187. }
  188. }
  189. ?>