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