note.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /*
  3. [UCenter] (C)2001-2099 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: note.php 1059 2011-03-01 07:25:09Z monkey $
  6. */
  7. !defined('IN_UC') && exit('Access Denied');
  8. define('UC_NOTE_REPEAT', 5);
  9. define('UC_NOTE_TIMEOUT', 15);
  10. define('UC_NOTE_GC', 10000);
  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. }
  44. function get_list($page, $ppp, $totalnum, $all = TRUE) {
  45. }
  46. function delete_note($ids) {
  47. }
  48. function add($operation, $getdata='', $postdata='', $appids=array(), $pri = 0) {
  49. $extra = $varextra = '';
  50. $appadd = $varadd = array();
  51. foreach((array)$this->apps as $appid => $app) {
  52. $appid = $app['appid'];
  53. if($appid == intval($appid)) {
  54. if($appids && !in_array($appid, $appids)) {
  55. $appadd[] = 'app'.$appid."='1'";
  56. } else {
  57. $varadd[] = "('noteexists{$appid}', '1')";
  58. }
  59. }
  60. }
  61. if($appadd) {
  62. $extra = implode(',', $appadd);
  63. $extra = $extra ? ', '.$extra : '';
  64. }
  65. if($varadd) {
  66. $varextra = implode(', ', $varadd);
  67. $varextra = $varextra ? ', '.$varextra : '';
  68. }
  69. $getdata = addslashes($getdata);
  70. $postdata = addslashes($postdata);
  71. $this->db->query("INSERT INTO ".UC_DBTABLEPRE."notelist SET getdata='$getdata', operation='$operation', pri='$pri', postdata='$postdata'$extra");
  72. $insert_id = $this->db->insert_id();
  73. $insert_id && $this->db->query("REPLACE INTO ".UC_DBTABLEPRE."vars (name, value) VALUES ('noteexists', '1')$varextra");
  74. return $insert_id;
  75. }
  76. function send() {
  77. register_shutdown_function(array($this, '_send'));
  78. }
  79. function _send() {
  80. $note = $this->_get_note();
  81. if(empty($note)) {
  82. $this->db->query("REPLACE INTO ".UC_DBTABLEPRE."vars SET name='noteexists".UC_APPID."', value='0'");
  83. return NULL;
  84. }
  85. $this->sendone(UC_APPID, 0, $note);
  86. $this->_gc();
  87. }
  88. function sendone($appid, $noteid = 0, $note = '') {
  89. require_once UC_ROOT.'./lib/xml.class.php';
  90. $return = FALSE;
  91. $app = $this->apps[$appid];
  92. if($noteid) {
  93. $note = $this->_get_note_by_id($noteid);
  94. }
  95. $this->base->load('misc');
  96. $apifilename = isset($app['apifilename']) && $app['apifilename'] ? $app['apifilename'] : 'uc.php';
  97. if($app['extra']['apppath'] && @include $app['extra']['apppath'].'./api/'.$apifilename) {
  98. $uc_note = new uc_note();
  99. $method = $note['operation'];
  100. if(is_string($method) && !empty($method)) {
  101. parse_str($note['getdata'], $note['getdata']);
  102. if(get_magic_quotes_gpc()) {
  103. $note['getdata'] = $this->base->dstripslashes($note['getdata']);
  104. }
  105. $note['postdata'] = xml_unserialize($note['postdata']);
  106. $response = $uc_note->$method($note['getdata'], $note['postdata']);
  107. }
  108. unset($uc_note);
  109. } else {
  110. $url = $this->get_url_code($note['operation'], $note['getdata'], $appid);
  111. $note['postdata'] = str_replace(array("\n", "\r"), '', $note['postdata']);
  112. $response = trim($_ENV['misc']->dfopen2($url, 0, $note['postdata'], '', 1, $app['ip'], UC_NOTE_TIMEOUT, TRUE));
  113. }
  114. $returnsucceed = $response != '' && ($response == 1 || is_array(xml_unserialize($response)));
  115. $closedsqladd = $this->_close_note($note, $this->apps, $returnsucceed, $appid) ? ",closed='1'" : '';//
  116. if($returnsucceed) {
  117. if($this->operations[$note['operation']][2]) {
  118. $this->base->load($this->operations[$note['operation']][2]);
  119. $func = $this->operations[$note['operation']][3];
  120. $_ENV[$this->operations[$note['operation']][2]]->$func($appid, $response);
  121. }
  122. $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');
  123. $return = TRUE;
  124. } else {
  125. $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');
  126. $return = FALSE;
  127. }
  128. return $return;
  129. }
  130. function _get_note() {
  131. $app_field = 'app'.UC_APPID;
  132. $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");
  133. return $data;
  134. }
  135. function _gc() {
  136. rand(0, UC_NOTE_GC) == 0 && $this->db->query("DELETE FROM ".UC_DBTABLEPRE."notelist WHERE closed='1'");
  137. }
  138. function _close_note($note, $apps, $returnsucceed, $appid) {
  139. $note['app'.$appid] = $returnsucceed ? 1 : $note['app'.$appid] - 1;
  140. $appcount = count($apps);
  141. foreach($apps as $key => $app) {
  142. $appstatus = $note['app'.$app['appid']];
  143. if(!$app['recvnote'] || $appstatus == 1 || $appstatus <= -UC_NOTE_REPEAT) {
  144. $appcount--;
  145. }
  146. }
  147. if($appcount < 1) {
  148. return TRUE;
  149. //$closedsqladd = ",closed='1'";
  150. }
  151. }
  152. function _get_note_by_id($noteid) {
  153. $data = $this->db->fetch_first("SELECT * FROM ".UC_DBTABLEPRE."notelist WHERE noteid='$noteid'");
  154. return $data;
  155. }
  156. function get_url_code($operation, $getdata, $appid) {
  157. $app = $this->apps[$appid];
  158. $authkey = UC_KEY;
  159. $url = $app['url'];
  160. $apifilename = isset($app['apifilename']) && $app['apifilename'] ? $app['apifilename'] : 'uc.php';
  161. $action = $this->operations[$operation][1];
  162. $code = urlencode($this->base->authcode("$action&".($getdata ? "$getdata&" : '')."time=".$this->base->time, 'ENCODE', $authkey));
  163. return $url."/api/$apifilename?code=$code";
  164. }
  165. }
  166. ?>