note.php 8.0 KB

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