1 |
joko |
1.1 |
<? |
2 |
|
|
## ------------------------------------------------------------------------------------- |
3 |
jonen |
1.6 |
## $Id: ProxyObject.php,v 1.5 2002/12/18 22:36:49 jonen Exp $ |
4 |
joko |
1.1 |
## ------------------------------------------------------------------------------------- |
5 |
joko |
1.2 |
## $Log: ProxyObject.php,v $ |
6 |
jonen |
1.6 |
## Revision 1.5 2002/12/18 22:36:49 jonen |
7 |
|
|
## + added support to get remote objects via backend via 'guid' |
8 |
|
|
## + renamed '_loadBackend' to '_loadRemote' |
9 |
|
|
## + constructor now accepts additional options: |
10 |
|
|
## + remote: try to get object via '_loadRemote' ... |
11 |
|
|
## + oid: use the given identifier as an 'oid' when doing '_loadRemote' |
12 |
|
|
## + guid: use the given identifier as a 'guid' when doing '_loadRemote' |
13 |
|
|
## |
14 |
jonen |
1.5 |
## Revision 1.4 2002/12/12 02:46:31 joko |
15 |
|
|
## + state (session) gets flushed when data was successfully loaded from remote side |
16 |
|
|
## + fixed _loadBackend |
17 |
|
|
## + fixed flushState |
18 |
|
|
## |
19 |
joko |
1.4 |
## Revision 1.3 2002/12/06 04:12:54 joko |
20 |
|
|
## + replaced 'xyzCache' through 'xyzProxy' |
21 |
|
|
## + function store(...) |
22 |
|
|
## |
23 |
joko |
1.3 |
## Revision 1.2 2002/12/05 21:44:09 joko |
24 |
|
|
## + debugging |
25 |
|
|
## |
26 |
joko |
1.2 |
## Revision 1.1 2002/12/01 17:23:58 joko |
27 |
|
|
## + initial check-in |
28 |
|
|
## |
29 |
joko |
1.1 |
## Revision 1.11 2002/11/12 06:05:58 cvsjoko |
30 |
|
|
## + renamed class: Text_Encode -> TextEncode |
31 |
|
|
## |
32 |
|
|
## Revision 1.10 2002/10/29 19:15:33 cvsjoko |
33 |
|
|
## - moved utf8/iso-conversion to lib/utils/Text_Encode.php |
34 |
|
|
## |
35 |
|
|
## Revision 1.9 2002/10/26 12:32:36 cvsjoko |
36 |
|
|
## - removed debugging via "print" |
37 |
|
|
## + added generic logging via PEAR::Log |
38 |
|
|
## |
39 |
|
|
## Revision 1.8 2002/10/22 09:51:38 cvsmax |
40 |
|
|
## + moved semi-method 'createBackend' to method $site->user->create at User.class.php |
41 |
|
|
## |
42 |
|
|
## Revision 1.7 2002/10/21 18:27:09 cvsjoko |
43 |
|
|
## - manually disabled any form of caching |
44 |
|
|
## |
45 |
|
|
## Revision 1.6 2002/10/17 03:48:47 cvsmax |
46 |
|
|
## + new |
47 |
|
|
## + function _createBackend create new Backend |
48 |
|
|
## + function _create api-create |
49 |
|
|
## |
50 |
|
|
## Revision 1.5 2002/10/16 03:37:54 cvsjoko |
51 |
|
|
## + bugfix: wrong comparison in restriction to save to array |
52 |
|
|
## |
53 |
|
|
## Revision 1.4 2002/10/10 03:04:23 cvsjoko |
54 |
|
|
## + no debug-output |
55 |
|
|
## |
56 |
|
|
## Revision 1.3 2002/10/10 03:03:27 cvsjoko |
57 |
|
|
## + bugfix: save object to cache only if payload from backend isn't empty |
58 |
|
|
## |
59 |
|
|
## Revision 1.2 2002/10/10 02:40:06 cvsjoko |
60 |
|
|
## + new level of data-caching (session and persistant) |
61 |
|
|
## + function _loadState loads data from session |
62 |
|
|
## + function _saveState saves data to session |
63 |
|
|
## + function save api-save (session & backend) |
64 |
|
|
## + function _commit saves data to backend |
65 |
|
|
## + handy utils |
66 |
|
|
## + function _setAttributes |
67 |
|
|
## + function flushState |
68 |
|
|
## |
69 |
|
|
## Revision 1.1 2002/10/09 00:51:39 cvsjoko |
70 |
|
|
## + new |
71 |
|
|
## ------------------------------------------------------------------------------------- |
72 |
|
|
|
73 |
|
|
class ProxyObject { |
74 |
|
|
|
75 |
|
|
var $objectId; |
76 |
|
|
// purpose ... |
77 |
|
|
var $meta; |
78 |
|
|
var $payload; |
79 |
|
|
var $attributes; |
80 |
|
|
var $backend; |
81 |
|
|
|
82 |
jonen |
1.5 |
function ProxyObject($objectId = "", $options = array() ) { |
83 |
joko |
1.1 |
session_register_safe("proxy"); |
84 |
|
|
$this->backend = new Remote; |
85 |
jonen |
1.5 |
$this->_init($objectId, $options); |
86 |
joko |
1.1 |
} |
87 |
|
|
|
88 |
jonen |
1.5 |
function _init($objectId="", $options = array() ) { |
89 |
joko |
1.1 |
if ($objectId) { |
90 |
|
|
$this->objectId = $objectId; |
91 |
jonen |
1.5 |
$this->meta = $options; |
92 |
|
|
|
93 |
|
|
// set default load mechanismn |
94 |
|
|
if( $this->meta[remote] && ((!$this->meta[oid] && !$this->meta[guid]) || ($this->meta[oid] && $this->meta[guid])) ) { |
95 |
|
|
$this->meta[oid] = 1; |
96 |
|
|
} |
97 |
|
|
|
98 |
joko |
1.1 |
$this->load(); |
99 |
joko |
1.3 |
//$this->_saveProxy(); |
100 |
joko |
1.1 |
} |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
function load() { |
104 |
|
|
if (!$this->_loadState()) { |
105 |
joko |
1.3 |
if (!$this->_loadProxy()) { |
106 |
jonen |
1.5 |
|
107 |
|
|
// just load object from remote side if its flagged to be a remote one ... |
108 |
|
|
if ($this->meta[remote]) { |
109 |
|
|
$this->_loadRemote(); |
110 |
|
|
} |
111 |
|
|
|
112 |
joko |
1.1 |
} |
113 |
|
|
} |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
function save($data, $type) { |
117 |
|
|
$this->_setAttributes($data); |
118 |
|
|
$this->_saveState(); |
119 |
|
|
if ($type == 'commit') { |
120 |
|
|
$this->_commit(); |
121 |
|
|
} |
122 |
|
|
if ($type == 'create') { |
123 |
|
|
$this->_create(); |
124 |
|
|
} |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
function flush() { |
128 |
|
|
$this->flushState(); |
129 |
joko |
1.3 |
$this->flushProxy(); |
130 |
joko |
1.1 |
} |
131 |
|
|
|
132 |
|
|
function _commit() { |
133 |
|
|
$this->_saveBackend($this->attributes); |
134 |
|
|
$this->flushState(); |
135 |
joko |
1.3 |
$this->flushProxy(); |
136 |
joko |
1.1 |
} |
137 |
|
|
|
138 |
|
|
|
139 |
|
|
// TODO: make this work |
140 |
|
|
/* |
141 |
|
|
function _create() { |
142 |
|
|
$this->_createBackend($this->attributes); |
143 |
|
|
$this->flushState(); |
144 |
joko |
1.3 |
$this->flushProxy(); |
145 |
joko |
1.1 |
} |
146 |
|
|
*/ |
147 |
|
|
|
148 |
|
|
function getAttributes() { |
149 |
|
|
if (!$this->meta[decoded]) { |
150 |
|
|
$this->_decode(); |
151 |
|
|
$this->_saveState(); |
152 |
|
|
} |
153 |
|
|
return $this->attributes; |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
function _setAttributes($data) { |
157 |
|
|
$this->attributes = $data; |
158 |
|
|
} |
159 |
|
|
|
160 |
joko |
1.3 |
function flushProxy() { |
161 |
joko |
1.1 |
connectdb(); |
162 |
|
|
$sql = "DELETE FROM f_proxy WHERE oid='$this->objectId'"; |
163 |
|
|
send_sql($sql); |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
function flushState() { |
167 |
|
|
global $proxy; |
168 |
|
|
unset($proxy[$this->objectId]); |
169 |
joko |
1.4 |
$this->meta[decoded] = 0; |
170 |
joko |
1.1 |
} |
171 |
|
|
|
172 |
|
|
function _loadState() { |
173 |
|
|
global $proxy; |
174 |
|
|
logp(get_class($this) . "->_loadState()", LOG_DEBUG); |
175 |
|
|
if ($this->attributes = $proxy[$this->objectId]) { |
176 |
|
|
//print "_loadState:" . dumpVar($this->attributes); |
177 |
|
|
$this->meta[decoded] = 1; |
178 |
|
|
// TODO: make a parameter from this (0 deactivates session-layer) |
179 |
|
|
return 0; |
180 |
|
|
} |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
function _saveState() { |
184 |
|
|
global $proxy; |
185 |
|
|
logp(get_class($this) . "->_saveState()", LOG_DEBUG); |
186 |
|
|
$proxy[$this->objectId] = $this->attributes; |
187 |
|
|
//print "_saveState: " . dumpVar($this->attributes); |
188 |
|
|
// TODO: throw exception-message back to user if operation fails |
189 |
|
|
} |
190 |
|
|
|
191 |
joko |
1.3 |
function _loadProxy() { |
192 |
|
|
logp(get_class($this) . "->_loadProxy()", LOG_DEBUG); |
193 |
joko |
1.1 |
connectdb(); |
194 |
|
|
$sql = "SELECT payload FROM f_proxy WHERE oid='$this->objectId'"; |
195 |
|
|
if ($res = send_sql($sql)) { |
196 |
|
|
$row = mysql_fetch_array($res, MYSQL_ASSOC); |
197 |
|
|
if ($row) { |
198 |
|
|
$this->payload = $row[payload]; |
199 |
|
|
// TODO: make a parameter from this (0 deactivates mysqldb-layer) |
200 |
|
|
return 0; |
201 |
|
|
} |
202 |
|
|
} |
203 |
|
|
} |
204 |
|
|
|
205 |
joko |
1.3 |
// TODO: use PEAR here |
206 |
|
|
function _saveProxy() { |
207 |
|
|
logp(get_class($this) . "->_saveProxy()", LOG_DEBUG); |
208 |
joko |
1.1 |
connectdb(); |
209 |
|
|
if ($this->payload) { |
210 |
|
|
//$sql = "INSERT INTO f_proxy SET payload='$this->payload' WHERE oid='$this->objectId'"; |
211 |
|
|
$sql = "INSERT INTO f_proxy SET oid='$this->objectId', payload='$this->payload'"; |
212 |
|
|
if (!send_sql($sql)) { |
213 |
|
|
$sql = "UPDATE f_proxy SET payload='$this->payload' WHERE oid='$this->objectId'"; |
214 |
|
|
send_sql($sql); |
215 |
|
|
} |
216 |
|
|
} |
217 |
|
|
} |
218 |
|
|
|
219 |
jonen |
1.5 |
function _loadRemote() { |
220 |
|
|
logp(get_class($this) . "->_loadRemote()", LOG_DEBUG); |
221 |
|
|
|
222 |
joko |
1.1 |
// TODO: test backend for reachability first (eventually cache this information and "reset" it by another party) |
223 |
jonen |
1.5 |
|
224 |
|
|
// check for guid or oid |
225 |
|
|
if($this->meta[guid]) { |
226 |
|
|
$args = array( guid => $this->objectId, classname => $this->meta[classname] ); |
227 |
jonen |
1.6 |
$result = $this->backend->send('getObjectByGuid', $args ); |
228 |
jonen |
1.5 |
} |
229 |
|
|
if($this->meta[oid]) { |
230 |
jonen |
1.6 |
$result = $this->backend->send('getObject', $this->objectId); |
231 |
jonen |
1.5 |
} |
232 |
|
|
|
233 |
|
|
if ($result) { |
234 |
joko |
1.2 |
//print "result: " . dumpVar($result) . "<br>"; |
235 |
joko |
1.1 |
if (count($result) == 0) { return; } |
236 |
jonen |
1.5 |
|
237 |
|
|
// ----- move this to _encode some times: $this->_encode() |
238 |
joko |
1.1 |
$encoder = new TextEncode($result); |
239 |
|
|
$encoder->toISO(); |
240 |
|
|
if ($_GET[debug]) { |
241 |
|
|
print dumpVar($result); |
242 |
|
|
} |
243 |
|
|
$this->payload = serialize($result); |
244 |
jonen |
1.5 |
// ----- move this to _encode some times |
245 |
|
|
|
246 |
joko |
1.3 |
$this->_saveProxy(); |
247 |
joko |
1.4 |
//print "oid: $this->objectId<br>"; |
248 |
|
|
$this->flushState(); |
249 |
jonen |
1.5 |
} else { |
250 |
|
|
print "Error in _loadRemote!!!<br>"; |
251 |
joko |
1.1 |
} |
252 |
jonen |
1.5 |
|
253 |
joko |
1.1 |
} |
254 |
|
|
|
255 |
|
|
function _saveBackend($result) { |
256 |
|
|
logp(get_class($this) . "->_saveBackend()", LOG_DEBUG); |
257 |
jonen |
1.6 |
|
258 |
|
|
//$encoder = new TextEncode($result); |
259 |
|
|
//$encoder->toUTF8(); |
260 |
|
|
|
261 |
|
|
// check for guid or oid |
262 |
|
|
if($this->meta[guid]) { |
263 |
|
|
$args = array( 'guid' => $this->objectId, 'classname' => $this->meta[classname], 'data' => $result ); |
264 |
|
|
$response = $this->backend->send('saveObjectByGuid', $args, array( utf8 => 1) ); |
265 |
|
|
} |
266 |
|
|
if($this->meta[oid]) { |
267 |
|
|
$response = $this->backend->send('saveObject', array('oid' => $this->objectId, 'data' => $result), array( utf8 => 1) ); |
268 |
|
|
} |
269 |
joko |
1.1 |
} |
270 |
|
|
|
271 |
|
|
function _decode() { |
272 |
|
|
// fill attributes-hashtable from message-hashtable |
273 |
|
|
if (!$this->payload) { return; } |
274 |
|
|
//if ($this->attributes = $backend->decodeData($this->payload)) { |
275 |
|
|
if ($this->attributes = unserialize($this->payload)) { |
276 |
|
|
$this->meta[decoded] = 1; |
277 |
|
|
} |
278 |
|
|
} |
279 |
joko |
1.3 |
|
280 |
|
|
function store($struct) { |
281 |
|
|
$this->payload = serialize($struct); |
282 |
|
|
$this->_saveProxy(); |
283 |
|
|
} |
284 |
joko |
1.1 |
|
285 |
|
|
} |
286 |
|
|
|
287 |
|
|
?> |